]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_pci.c
linuxkpi: Add `pci_device_is_present()`
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / src / linux_pci.c
1 /*-
2  * Copyright (c) 2015-2016 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  * Copyright (c) 2020-2022 The FreeBSD Foundation
5  *
6  * Portions of this software were developed by Björn Zeeb
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/filio.h>
45 #include <sys/pciio.h>
46 #include <sys/pctrie.h>
47 #include <sys/rwlock.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51
52 #include <machine/stdarg.h>
53
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pci_private.h>
56 #include <dev/pci/pci_iov.h>
57 #include <dev/backlight/backlight.h>
58
59 #include <linux/kernel.h>
60 #include <linux/kobject.h>
61 #include <linux/device.h>
62 #include <linux/slab.h>
63 #include <linux/module.h>
64 #include <linux/cdev.h>
65 #include <linux/file.h>
66 #include <linux/sysfs.h>
67 #include <linux/mm.h>
68 #include <linux/io.h>
69 #include <linux/vmalloc.h>
70 #include <linux/pci.h>
71 #include <linux/compat.h>
72
73 #include <linux/backlight.h>
74
75 #include "backlight_if.h"
76 #include "pcib_if.h"
77
78 /* Undef the linux function macro defined in linux/pci.h */
79 #undef pci_get_class
80
81 extern int linuxkpi_debug;
82
83 SYSCTL_DECL(_compat_linuxkpi);
84
85 static counter_u64_t lkpi_pci_nseg1_fail;
86 SYSCTL_COUNTER_U64(_compat_linuxkpi, OID_AUTO, lkpi_pci_nseg1_fail, CTLFLAG_RD,
87     &lkpi_pci_nseg1_fail, "Count of busdma mapping failures of single-segment");
88
89 static device_probe_t linux_pci_probe;
90 static device_attach_t linux_pci_attach;
91 static device_detach_t linux_pci_detach;
92 static device_suspend_t linux_pci_suspend;
93 static device_resume_t linux_pci_resume;
94 static device_shutdown_t linux_pci_shutdown;
95 static pci_iov_init_t linux_pci_iov_init;
96 static pci_iov_uninit_t linux_pci_iov_uninit;
97 static pci_iov_add_vf_t linux_pci_iov_add_vf;
98 static int linux_backlight_get_status(device_t dev, struct backlight_props *props);
99 static int linux_backlight_update_status(device_t dev, struct backlight_props *props);
100 static int linux_backlight_get_info(device_t dev, struct backlight_info *info);
101
102 static device_method_t pci_methods[] = {
103         DEVMETHOD(device_probe, linux_pci_probe),
104         DEVMETHOD(device_attach, linux_pci_attach),
105         DEVMETHOD(device_detach, linux_pci_detach),
106         DEVMETHOD(device_suspend, linux_pci_suspend),
107         DEVMETHOD(device_resume, linux_pci_resume),
108         DEVMETHOD(device_shutdown, linux_pci_shutdown),
109         DEVMETHOD(pci_iov_init, linux_pci_iov_init),
110         DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit),
111         DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf),
112
113         /* backlight interface */
114         DEVMETHOD(backlight_update_status, linux_backlight_update_status),
115         DEVMETHOD(backlight_get_status, linux_backlight_get_status),
116         DEVMETHOD(backlight_get_info, linux_backlight_get_info),
117         DEVMETHOD_END
118 };
119
120 struct linux_dma_priv {
121         uint64_t        dma_mask;
122         bus_dma_tag_t   dmat;
123         uint64_t        dma_coherent_mask;
124         bus_dma_tag_t   dmat_coherent;
125         struct mtx      lock;
126         struct pctrie   ptree;
127 };
128 #define DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
129 #define DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
130
131 static bool
132 linux_is_drm(struct pci_driver *pdrv)
133 {
134         return (pdrv->name != NULL && strcmp(pdrv->name, "drmn") == 0);
135 }
136
137 static int
138 linux_pdev_dma_uninit(struct pci_dev *pdev)
139 {
140         struct linux_dma_priv *priv;
141
142         priv = pdev->dev.dma_priv;
143         if (priv->dmat)
144                 bus_dma_tag_destroy(priv->dmat);
145         if (priv->dmat_coherent)
146                 bus_dma_tag_destroy(priv->dmat_coherent);
147         mtx_destroy(&priv->lock);
148         pdev->dev.dma_priv = NULL;
149         free(priv, M_DEVBUF);
150         return (0);
151 }
152
153 static int
154 linux_pdev_dma_init(struct pci_dev *pdev)
155 {
156         struct linux_dma_priv *priv;
157         int error;
158
159         priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
160
161         mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
162         pctrie_init(&priv->ptree);
163
164         pdev->dev.dma_priv = priv;
165
166         /* Create a default DMA tags. */
167         error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
168         if (error != 0)
169                 goto err;
170         /* Coherent is lower 32bit only by default in Linux. */
171         error = linux_dma_tag_init_coherent(&pdev->dev, DMA_BIT_MASK(32));
172         if (error != 0)
173                 goto err;
174
175         return (error);
176
177 err:
178         linux_pdev_dma_uninit(pdev);
179         return (error);
180 }
181
182 int
183 linux_dma_tag_init(struct device *dev, u64 dma_mask)
184 {
185         struct linux_dma_priv *priv;
186         int error;
187
188         priv = dev->dma_priv;
189
190         if (priv->dmat) {
191                 if (priv->dma_mask == dma_mask)
192                         return (0);
193
194                 bus_dma_tag_destroy(priv->dmat);
195         }
196
197         priv->dma_mask = dma_mask;
198
199         error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
200             1, 0,                       /* alignment, boundary */
201             dma_mask,                   /* lowaddr */
202             BUS_SPACE_MAXADDR,          /* highaddr */
203             NULL, NULL,                 /* filtfunc, filtfuncarg */
204             BUS_SPACE_MAXSIZE,          /* maxsize */
205             1,                          /* nsegments */
206             BUS_SPACE_MAXSIZE,          /* maxsegsz */
207             0,                          /* flags */
208             NULL, NULL,                 /* lockfunc, lockfuncarg */
209             &priv->dmat);
210         return (-error);
211 }
212
213 int
214 linux_dma_tag_init_coherent(struct device *dev, u64 dma_mask)
215 {
216         struct linux_dma_priv *priv;
217         int error;
218
219         priv = dev->dma_priv;
220
221         if (priv->dmat_coherent) {
222                 if (priv->dma_coherent_mask == dma_mask)
223                         return (0);
224
225                 bus_dma_tag_destroy(priv->dmat_coherent);
226         }
227
228         priv->dma_coherent_mask = dma_mask;
229
230         error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
231             1, 0,                       /* alignment, boundary */
232             dma_mask,                   /* lowaddr */
233             BUS_SPACE_MAXADDR,          /* highaddr */
234             NULL, NULL,                 /* filtfunc, filtfuncarg */
235             BUS_SPACE_MAXSIZE,          /* maxsize */
236             1,                          /* nsegments */
237             BUS_SPACE_MAXSIZE,          /* maxsegsz */
238             0,                          /* flags */
239             NULL, NULL,                 /* lockfunc, lockfuncarg */
240             &priv->dmat_coherent);
241         return (-error);
242 }
243
244 static struct pci_driver *
245 linux_pci_find(device_t dev, const struct pci_device_id **idp)
246 {
247         const struct pci_device_id *id;
248         struct pci_driver *pdrv;
249         uint16_t vendor;
250         uint16_t device;
251         uint16_t subvendor;
252         uint16_t subdevice;
253
254         vendor = pci_get_vendor(dev);
255         device = pci_get_device(dev);
256         subvendor = pci_get_subvendor(dev);
257         subdevice = pci_get_subdevice(dev);
258
259         spin_lock(&pci_lock);
260         list_for_each_entry(pdrv, &pci_drivers, node) {
261                 for (id = pdrv->id_table; id->vendor != 0; id++) {
262                         if (vendor == id->vendor &&
263                             (PCI_ANY_ID == id->device || device == id->device) &&
264                             (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
265                             (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
266                                 *idp = id;
267                                 spin_unlock(&pci_lock);
268                                 return (pdrv);
269                         }
270                 }
271         }
272         spin_unlock(&pci_lock);
273         return (NULL);
274 }
275
276 struct pci_dev *
277 lkpi_pci_get_device(uint16_t vendor, uint16_t device, struct pci_dev *odev)
278 {
279         struct pci_dev *pdev;
280
281         KASSERT(odev == NULL, ("%s: odev argument not yet supported\n", __func__));
282
283         spin_lock(&pci_lock);
284         list_for_each_entry(pdev, &pci_devices, links) {
285                 if (pdev->vendor == vendor && pdev->device == device)
286                         break;
287         }
288         spin_unlock(&pci_lock);
289
290         return (pdev);
291 }
292
293 static void
294 lkpi_pci_dev_release(struct device *dev)
295 {
296
297         lkpi_devres_release_free_list(dev);
298         spin_lock_destroy(&dev->devres_lock);
299 }
300
301 static void
302 lkpifill_pci_dev(device_t dev, struct pci_dev *pdev)
303 {
304
305         pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
306         pdev->vendor = pci_get_vendor(dev);
307         pdev->device = pci_get_device(dev);
308         pdev->subsystem_vendor = pci_get_subvendor(dev);
309         pdev->subsystem_device = pci_get_subdevice(dev);
310         pdev->class = pci_get_class(dev);
311         pdev->revision = pci_get_revid(dev);
312         pdev->bus = malloc(sizeof(*pdev->bus), M_DEVBUF, M_WAITOK | M_ZERO);
313         /*
314          * This should be the upstream bridge; pci_upstream_bridge()
315          * handles that case on demand as otherwise we'll shadow the
316          * entire PCI hierarchy.
317          */
318         pdev->bus->self = pdev;
319         pdev->bus->number = pci_get_bus(dev);
320         pdev->bus->domain = pci_get_domain(dev);
321         pdev->dev.bsddev = dev;
322         pdev->dev.parent = &linux_root_device;
323         pdev->dev.release = lkpi_pci_dev_release;
324         INIT_LIST_HEAD(&pdev->dev.irqents);
325         kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
326         kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
327         kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
328             kobject_name(&pdev->dev.kobj));
329         spin_lock_init(&pdev->dev.devres_lock);
330         INIT_LIST_HEAD(&pdev->dev.devres_head);
331 }
332
333 static void
334 lkpinew_pci_dev_release(struct device *dev)
335 {
336         struct pci_dev *pdev;
337
338         pdev = to_pci_dev(dev);
339         if (pdev->root != NULL)
340                 pci_dev_put(pdev->root);
341         if (pdev->bus->self != pdev)
342                 pci_dev_put(pdev->bus->self);
343         free(pdev->bus, M_DEVBUF);
344         free(pdev, M_DEVBUF);
345 }
346
347 struct pci_dev *
348 lkpinew_pci_dev(device_t dev)
349 {
350         struct pci_dev *pdev;
351
352         pdev = malloc(sizeof(*pdev), M_DEVBUF, M_WAITOK|M_ZERO);
353         lkpifill_pci_dev(dev, pdev);
354         pdev->dev.release = lkpinew_pci_dev_release;
355
356         return (pdev);
357 }
358
359 struct pci_dev *
360 lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
361 {
362         device_t dev;
363         device_t devfrom = NULL;
364         struct pci_dev *pdev;
365
366         if (from != NULL)
367                 devfrom = from->dev.bsddev;
368
369         dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
370         if (dev == NULL)
371                 return (NULL);
372
373         pdev = lkpinew_pci_dev(dev);
374         return (pdev);
375 }
376
377 struct pci_dev *
378 lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,
379     unsigned int devfn)
380 {
381         device_t dev;
382         struct pci_dev *pdev;
383
384         dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
385         if (dev == NULL)
386                 return (NULL);
387
388         pdev = lkpinew_pci_dev(dev);
389         return (pdev);
390 }
391
392 static int
393 linux_pci_probe(device_t dev)
394 {
395         const struct pci_device_id *id;
396         struct pci_driver *pdrv;
397
398         if ((pdrv = linux_pci_find(dev, &id)) == NULL)
399                 return (ENXIO);
400         if (device_get_driver(dev) != &pdrv->bsddriver)
401                 return (ENXIO);
402         device_set_desc(dev, pdrv->name);
403
404         /* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */
405         if (pdrv->bsd_probe_return == 0)
406                 return (BUS_PROBE_DEFAULT);
407         else
408                 return (pdrv->bsd_probe_return);
409 }
410
411 static int
412 linux_pci_attach(device_t dev)
413 {
414         const struct pci_device_id *id;
415         struct pci_driver *pdrv;
416         struct pci_dev *pdev;
417
418         pdrv = linux_pci_find(dev, &id);
419         pdev = device_get_softc(dev);
420
421         MPASS(pdrv != NULL);
422         MPASS(pdev != NULL);
423
424         return (linux_pci_attach_device(dev, pdrv, id, pdev));
425 }
426
427 int
428 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
429     const struct pci_device_id *id, struct pci_dev *pdev)
430 {
431         struct resource_list_entry *rle;
432         device_t parent;
433         uintptr_t rid;
434         int error;
435         bool isdrm;
436
437         linux_set_current(curthread);
438
439         parent = device_get_parent(dev);
440         isdrm = pdrv != NULL && linux_is_drm(pdrv);
441
442         if (isdrm) {
443                 struct pci_devinfo *dinfo;
444
445                 dinfo = device_get_ivars(parent);
446                 device_set_ivars(dev, dinfo);
447         }
448
449         lkpifill_pci_dev(dev, pdev);
450         if (isdrm)
451                 PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);
452         else
453                 PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);
454         pdev->devfn = rid;
455         pdev->pdrv = pdrv;
456         rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);
457         if (rle != NULL)
458                 pdev->dev.irq = rle->start;
459         else
460                 pdev->dev.irq = LINUX_IRQ_INVALID;
461         pdev->irq = pdev->dev.irq;
462         error = linux_pdev_dma_init(pdev);
463         if (error)
464                 goto out_dma_init;
465
466         TAILQ_INIT(&pdev->mmio);
467
468         spin_lock(&pci_lock);
469         list_add(&pdev->links, &pci_devices);
470         spin_unlock(&pci_lock);
471
472         if (pdrv != NULL) {
473                 error = pdrv->probe(pdev, id);
474                 if (error)
475                         goto out_probe;
476         }
477         return (0);
478
479 out_probe:
480         free(pdev->bus, M_DEVBUF);
481         linux_pdev_dma_uninit(pdev);
482 out_dma_init:
483         spin_lock(&pci_lock);
484         list_del(&pdev->links);
485         spin_unlock(&pci_lock);
486         put_device(&pdev->dev);
487         return (-error);
488 }
489
490 static int
491 linux_pci_detach(device_t dev)
492 {
493         struct pci_dev *pdev;
494
495         pdev = device_get_softc(dev);
496
497         MPASS(pdev != NULL);
498
499         device_set_desc(dev, NULL);
500
501         return (linux_pci_detach_device(pdev));
502 }
503
504 int
505 linux_pci_detach_device(struct pci_dev *pdev)
506 {
507
508         linux_set_current(curthread);
509
510         if (pdev->pdrv != NULL)
511                 pdev->pdrv->remove(pdev);
512
513         if (pdev->root != NULL)
514                 pci_dev_put(pdev->root);
515         free(pdev->bus, M_DEVBUF);
516         linux_pdev_dma_uninit(pdev);
517
518         spin_lock(&pci_lock);
519         list_del(&pdev->links);
520         spin_unlock(&pci_lock);
521         put_device(&pdev->dev);
522
523         return (0);
524 }
525
526 static int
527 lkpi_pci_disable_dev(struct device *dev)
528 {
529
530         (void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);
531         (void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);
532         return (0);
533 }
534
535 struct pci_devres *
536 lkpi_pci_devres_get_alloc(struct pci_dev *pdev)
537 {
538         struct pci_devres *dr;
539
540         dr = lkpi_devres_find(&pdev->dev, lkpi_pci_devres_release, NULL, NULL);
541         if (dr == NULL) {
542                 dr = lkpi_devres_alloc(lkpi_pci_devres_release, sizeof(*dr),
543                     GFP_KERNEL | __GFP_ZERO);
544                 if (dr != NULL)
545                         lkpi_devres_add(&pdev->dev, dr);
546         }
547
548         return (dr);
549 }
550
551 void
552 lkpi_pci_devres_release(struct device *dev, void *p)
553 {
554         struct pci_devres *dr;
555         struct pci_dev *pdev;
556         int bar;
557
558         pdev = to_pci_dev(dev);
559         dr = p;
560
561         if (pdev->msix_enabled)
562                 lkpi_pci_disable_msix(pdev);
563         if (pdev->msi_enabled)
564                 lkpi_pci_disable_msi(pdev);
565
566         if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)
567                 dr->enable_io = false;
568
569         if (dr->region_mask == 0)
570                 return;
571         for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
572
573                 if ((dr->region_mask & (1 << bar)) == 0)
574                         continue;
575                 pci_release_region(pdev, bar);
576         }
577 }
578
579 struct pcim_iomap_devres *
580 lkpi_pcim_iomap_devres_find(struct pci_dev *pdev)
581 {
582         struct pcim_iomap_devres *dr;
583
584         dr = lkpi_devres_find(&pdev->dev, lkpi_pcim_iomap_table_release,
585             NULL, NULL);
586         if (dr == NULL) {
587                 dr = lkpi_devres_alloc(lkpi_pcim_iomap_table_release,
588                     sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
589                 if (dr != NULL)
590                         lkpi_devres_add(&pdev->dev, dr);
591         }
592
593         if (dr == NULL)
594                 device_printf(pdev->dev.bsddev, "%s: NULL\n", __func__);
595
596         return (dr);
597 }
598
599 void
600 lkpi_pcim_iomap_table_release(struct device *dev, void *p)
601 {
602         struct pcim_iomap_devres *dr;
603         struct pci_dev *pdev;
604         int bar;
605
606         dr = p;
607         pdev = to_pci_dev(dev);
608         for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
609
610                 if (dr->mmio_table[bar] == NULL)
611                         continue;
612
613                 pci_iounmap(pdev, dr->mmio_table[bar]);
614         }
615 }
616
617 static int
618 linux_pci_suspend(device_t dev)
619 {
620         const struct dev_pm_ops *pmops;
621         struct pm_message pm = { };
622         struct pci_dev *pdev;
623         int error;
624
625         error = 0;
626         linux_set_current(curthread);
627         pdev = device_get_softc(dev);
628         pmops = pdev->pdrv->driver.pm;
629
630         if (pdev->pdrv->suspend != NULL)
631                 error = -pdev->pdrv->suspend(pdev, pm);
632         else if (pmops != NULL && pmops->suspend != NULL) {
633                 error = -pmops->suspend(&pdev->dev);
634                 if (error == 0 && pmops->suspend_late != NULL)
635                         error = -pmops->suspend_late(&pdev->dev);
636         }
637         return (error);
638 }
639
640 static int
641 linux_pci_resume(device_t dev)
642 {
643         const struct dev_pm_ops *pmops;
644         struct pci_dev *pdev;
645         int error;
646
647         error = 0;
648         linux_set_current(curthread);
649         pdev = device_get_softc(dev);
650         pmops = pdev->pdrv->driver.pm;
651
652         if (pdev->pdrv->resume != NULL)
653                 error = -pdev->pdrv->resume(pdev);
654         else if (pmops != NULL && pmops->resume != NULL) {
655                 if (pmops->resume_early != NULL)
656                         error = -pmops->resume_early(&pdev->dev);
657                 if (error == 0 && pmops->resume != NULL)
658                         error = -pmops->resume(&pdev->dev);
659         }
660         return (error);
661 }
662
663 static int
664 linux_pci_shutdown(device_t dev)
665 {
666         struct pci_dev *pdev;
667
668         linux_set_current(curthread);
669         pdev = device_get_softc(dev);
670         if (pdev->pdrv->shutdown != NULL)
671                 pdev->pdrv->shutdown(pdev);
672         return (0);
673 }
674
675 static int
676 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
677 {
678         struct pci_dev *pdev;
679         int error;
680
681         linux_set_current(curthread);
682         pdev = device_get_softc(dev);
683         if (pdev->pdrv->bsd_iov_init != NULL)
684                 error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);
685         else
686                 error = EINVAL;
687         return (error);
688 }
689
690 static void
691 linux_pci_iov_uninit(device_t dev)
692 {
693         struct pci_dev *pdev;
694
695         linux_set_current(curthread);
696         pdev = device_get_softc(dev);
697         if (pdev->pdrv->bsd_iov_uninit != NULL)
698                 pdev->pdrv->bsd_iov_uninit(dev);
699 }
700
701 static int
702 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
703 {
704         struct pci_dev *pdev;
705         int error;
706
707         linux_set_current(curthread);
708         pdev = device_get_softc(dev);
709         if (pdev->pdrv->bsd_iov_add_vf != NULL)
710                 error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);
711         else
712                 error = EINVAL;
713         return (error);
714 }
715
716 static int
717 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
718 {
719         int error;
720
721         linux_set_current(curthread);
722         spin_lock(&pci_lock);
723         list_add(&pdrv->node, &pci_drivers);
724         spin_unlock(&pci_lock);
725         if (pdrv->bsddriver.name == NULL)
726                 pdrv->bsddriver.name = pdrv->name;
727         pdrv->bsddriver.methods = pci_methods;
728         pdrv->bsddriver.size = sizeof(struct pci_dev);
729
730         bus_topo_lock();
731         error = devclass_add_driver(dc, &pdrv->bsddriver,
732             BUS_PASS_DEFAULT, &pdrv->bsdclass);
733         bus_topo_unlock();
734         return (-error);
735 }
736
737 int
738 linux_pci_register_driver(struct pci_driver *pdrv)
739 {
740         devclass_t dc;
741
742         dc = devclass_find("pci");
743         if (dc == NULL)
744                 return (-ENXIO);
745         return (_linux_pci_register_driver(pdrv, dc));
746 }
747
748 struct resource_list_entry *
749 linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,
750     int type, int rid)
751 {
752         device_t dev;
753         struct resource *res;
754
755         KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,
756             ("trying to reserve non-BAR type %d", type));
757
758         dev = pdev->pdrv != NULL && linux_is_drm(pdev->pdrv) ?
759             device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
760         res = pci_reserve_map(device_get_parent(dev), dev, type, &rid, 0, ~0,
761             1, 1, 0);
762         if (res == NULL)
763                 return (NULL);
764         return (resource_list_find(rl, type, rid));
765 }
766
767 unsigned long
768 pci_resource_start(struct pci_dev *pdev, int bar)
769 {
770         struct resource_list_entry *rle;
771         rman_res_t newstart;
772         device_t dev;
773
774         if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
775                 return (0);
776         dev = pdev->pdrv != NULL && linux_is_drm(pdev->pdrv) ?
777             device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
778         if (BUS_TRANSLATE_RESOURCE(dev, rle->type, rle->start, &newstart)) {
779                 device_printf(pdev->dev.bsddev, "translate of %#jx failed\n",
780                     (uintmax_t)rle->start);
781                 return (0);
782         }
783         return (newstart);
784 }
785
786 unsigned long
787 pci_resource_len(struct pci_dev *pdev, int bar)
788 {
789         struct resource_list_entry *rle;
790
791         if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
792                 return (0);
793         return (rle->count);
794 }
795
796 int
797 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
798 {
799         struct resource *res;
800         struct pci_devres *dr;
801         struct pci_mmio_region *mmio;
802         int rid;
803         int type;
804
805         type = pci_resource_type(pdev, bar);
806         if (type < 0)
807                 return (-ENODEV);
808         rid = PCIR_BAR(bar);
809         res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
810             RF_ACTIVE|RF_SHAREABLE);
811         if (res == NULL) {
812                 device_printf(pdev->dev.bsddev, "%s: failed to alloc "
813                     "bar %d type %d rid %d\n",
814                     __func__, bar, type, PCIR_BAR(bar));
815                 return (-ENODEV);
816         }
817
818         /*
819          * It seems there is an implicit devres tracking on these if the device
820          * is managed; otherwise the resources are not automatiaclly freed on
821          * FreeBSD/LinuxKPI tough they should be/are expected to be by Linux
822          * drivers.
823          */
824         dr = lkpi_pci_devres_find(pdev);
825         if (dr != NULL) {
826                 dr->region_mask |= (1 << bar);
827                 dr->region_table[bar] = res;
828         }
829
830         /* Even if the device is not managed we need to track it for iomap. */
831         mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
832         mmio->rid = PCIR_BAR(bar);
833         mmio->type = type;
834         mmio->res = res;
835         TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
836
837         return (0);
838 }
839
840 struct resource *
841 _lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size __unused)
842 {
843         struct pci_mmio_region *mmio, *p;
844         int type;
845
846         type = pci_resource_type(pdev, bar);
847         if (type < 0) {
848                 device_printf(pdev->dev.bsddev, "%s: bar %d type %d\n",
849                      __func__, bar, type);
850                 return (NULL);
851         }
852
853         /*
854          * Check for duplicate mappings.
855          * This can happen if a driver calls pci_request_region() first.
856          */
857         TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
858                 if (mmio->type == type && mmio->rid == PCIR_BAR(bar)) {
859                         return (mmio->res);
860                 }
861         }
862
863         mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
864         mmio->rid = PCIR_BAR(bar);
865         mmio->type = type;
866         mmio->res = bus_alloc_resource_any(pdev->dev.bsddev, mmio->type,
867             &mmio->rid, RF_ACTIVE|RF_SHAREABLE);
868         if (mmio->res == NULL) {
869                 device_printf(pdev->dev.bsddev, "%s: failed to alloc "
870                     "bar %d type %d rid %d\n",
871                     __func__, bar, type, PCIR_BAR(bar));
872                 free(mmio, M_DEVBUF);
873                 return (NULL);
874         }
875         TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
876
877         return (mmio->res);
878 }
879
880 int
881 linux_pci_register_drm_driver(struct pci_driver *pdrv)
882 {
883         devclass_t dc;
884
885         dc = devclass_create("vgapci");
886         if (dc == NULL)
887                 return (-ENXIO);
888         pdrv->name = "drmn";
889         return (_linux_pci_register_driver(pdrv, dc));
890 }
891
892 void
893 linux_pci_unregister_driver(struct pci_driver *pdrv)
894 {
895         devclass_t bus;
896
897         bus = devclass_find("pci");
898
899         spin_lock(&pci_lock);
900         list_del(&pdrv->node);
901         spin_unlock(&pci_lock);
902         bus_topo_lock();
903         if (bus != NULL)
904                 devclass_delete_driver(bus, &pdrv->bsddriver);
905         bus_topo_unlock();
906 }
907
908 void
909 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
910 {
911         devclass_t bus;
912
913         bus = devclass_find("vgapci");
914
915         spin_lock(&pci_lock);
916         list_del(&pdrv->node);
917         spin_unlock(&pci_lock);
918         bus_topo_lock();
919         if (bus != NULL)
920                 devclass_delete_driver(bus, &pdrv->bsddriver);
921         bus_topo_unlock();
922 }
923
924 int
925 pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,
926     unsigned int flags)
927 {
928         int error;
929
930         if (flags & PCI_IRQ_MSIX) {
931                 struct msix_entry *entries;
932                 int i;
933
934                 entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL);
935                 if (entries == NULL) {
936                         error = -ENOMEM;
937                         goto out;
938                 }
939                 for (i = 0; i < maxv; ++i)
940                         entries[i].entry = i;
941                 error = pci_enable_msix(pdev, entries, maxv);
942 out:
943                 kfree(entries);
944                 if (error == 0 && pdev->msix_enabled)
945                         return (pdev->dev.irq_end - pdev->dev.irq_start);
946         }
947         if (flags & PCI_IRQ_MSI) {
948                 if (pci_msi_count(pdev->dev.bsddev) < minv)
949                         return (-ENOSPC);
950                 /* We only support 1 vector in pci_enable_msi() */
951                 if (minv != 1)
952                         return (-ENOSPC);
953                 error = pci_enable_msi(pdev);
954                 if (error == 0 && pdev->msi_enabled)
955                         return (pdev->dev.irq_end - pdev->dev.irq_start);
956         }
957         if (flags & PCI_IRQ_LEGACY) {
958                 if (pdev->irq)
959                         return (1);
960         }
961
962         return (-EINVAL);
963 }
964
965 bool
966 pci_device_is_present(struct pci_dev *pdev)
967 {
968         device_t dev;
969
970         dev = pdev->dev.bsddev;
971
972         return (bus_child_present(dev));
973 }
974
975 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
976
977 struct linux_dma_obj {
978         void            *vaddr;
979         uint64_t        dma_addr;
980         bus_dmamap_t    dmamap;
981         bus_dma_tag_t   dmat;
982 };
983
984 static uma_zone_t linux_dma_trie_zone;
985 static uma_zone_t linux_dma_obj_zone;
986
987 static void
988 linux_dma_init(void *arg)
989 {
990
991         linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
992             pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
993             UMA_ALIGN_PTR, 0);
994         linux_dma_obj_zone = uma_zcreate("linux_dma_object",
995             sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
996             UMA_ALIGN_PTR, 0);
997         lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);
998 }
999 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
1000
1001 static void
1002 linux_dma_uninit(void *arg)
1003 {
1004
1005         counter_u64_free(lkpi_pci_nseg1_fail);
1006         uma_zdestroy(linux_dma_obj_zone);
1007         uma_zdestroy(linux_dma_trie_zone);
1008 }
1009 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
1010
1011 static void *
1012 linux_dma_trie_alloc(struct pctrie *ptree)
1013 {
1014
1015         return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
1016 }
1017
1018 static void
1019 linux_dma_trie_free(struct pctrie *ptree, void *node)
1020 {
1021
1022         uma_zfree(linux_dma_trie_zone, node);
1023 }
1024
1025 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
1026     linux_dma_trie_free);
1027
1028 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1029 static dma_addr_t
1030 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,
1031     bus_dma_tag_t dmat)
1032 {
1033         struct linux_dma_priv *priv;
1034         struct linux_dma_obj *obj;
1035         int error, nseg;
1036         bus_dma_segment_t seg;
1037
1038         priv = dev->dma_priv;
1039
1040         /*
1041          * If the resultant mapping will be entirely 1:1 with the
1042          * physical address, short-circuit the remainder of the
1043          * bus_dma API.  This avoids tracking collisions in the pctrie
1044          * with the additional benefit of reducing overhead.
1045          */
1046         if (bus_dma_id_mapped(dmat, phys, len))
1047                 return (phys);
1048
1049         obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
1050         if (obj == NULL) {
1051                 return (0);
1052         }
1053         obj->dmat = dmat;
1054
1055         DMA_PRIV_LOCK(priv);
1056         if (bus_dmamap_create(obj->dmat, 0, &obj->dmamap) != 0) {
1057                 DMA_PRIV_UNLOCK(priv);
1058                 uma_zfree(linux_dma_obj_zone, obj);
1059                 return (0);
1060         }
1061
1062         nseg = -1;
1063         if (_bus_dmamap_load_phys(obj->dmat, obj->dmamap, phys, len,
1064             BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
1065                 bus_dmamap_destroy(obj->dmat, obj->dmamap);
1066                 DMA_PRIV_UNLOCK(priv);
1067                 uma_zfree(linux_dma_obj_zone, obj);
1068                 counter_u64_add(lkpi_pci_nseg1_fail, 1);
1069                 if (linuxkpi_debug)
1070                         dump_stack();
1071                 return (0);
1072         }
1073
1074         KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1075         obj->dma_addr = seg.ds_addr;
1076
1077         error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
1078         if (error != 0) {
1079                 bus_dmamap_unload(obj->dmat, obj->dmamap);
1080                 bus_dmamap_destroy(obj->dmat, obj->dmamap);
1081                 DMA_PRIV_UNLOCK(priv);
1082                 uma_zfree(linux_dma_obj_zone, obj);
1083                 return (0);
1084         }
1085         DMA_PRIV_UNLOCK(priv);
1086         return (obj->dma_addr);
1087 }
1088 #else
1089 static dma_addr_t
1090 linux_dma_map_phys_common(struct device *dev __unused, vm_paddr_t phys,
1091     size_t len __unused, bus_dma_tag_t dmat __unused)
1092 {
1093         return (phys);
1094 }
1095 #endif
1096
1097 dma_addr_t
1098 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
1099 {
1100         struct linux_dma_priv *priv;
1101
1102         priv = dev->dma_priv;
1103         return (linux_dma_map_phys_common(dev, phys, len, priv->dmat));
1104 }
1105
1106 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1107 void
1108 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1109 {
1110         struct linux_dma_priv *priv;
1111         struct linux_dma_obj *obj;
1112
1113         priv = dev->dma_priv;
1114
1115         if (pctrie_is_empty(&priv->ptree))
1116                 return;
1117
1118         DMA_PRIV_LOCK(priv);
1119         obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1120         if (obj == NULL) {
1121                 DMA_PRIV_UNLOCK(priv);
1122                 return;
1123         }
1124         LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
1125         bus_dmamap_unload(obj->dmat, obj->dmamap);
1126         bus_dmamap_destroy(obj->dmat, obj->dmamap);
1127         DMA_PRIV_UNLOCK(priv);
1128
1129         uma_zfree(linux_dma_obj_zone, obj);
1130 }
1131 #else
1132 void
1133 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1134 {
1135 }
1136 #endif
1137
1138 void *
1139 linux_dma_alloc_coherent(struct device *dev, size_t size,
1140     dma_addr_t *dma_handle, gfp_t flag)
1141 {
1142         struct linux_dma_priv *priv;
1143         vm_paddr_t high;
1144         size_t align;
1145         void *mem;
1146
1147         if (dev == NULL || dev->dma_priv == NULL) {
1148                 *dma_handle = 0;
1149                 return (NULL);
1150         }
1151         priv = dev->dma_priv;
1152         if (priv->dma_coherent_mask)
1153                 high = priv->dma_coherent_mask;
1154         else
1155                 /* Coherent is lower 32bit only by default in Linux. */
1156                 high = BUS_SPACE_MAXADDR_32BIT;
1157         align = PAGE_SIZE << get_order(size);
1158         /* Always zero the allocation. */
1159         flag |= M_ZERO;
1160         mem = (void *)kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,
1161             align, 0, VM_MEMATTR_DEFAULT);
1162         if (mem != NULL) {
1163                 *dma_handle = linux_dma_map_phys_common(dev, vtophys(mem), size,
1164                     priv->dmat_coherent);
1165                 if (*dma_handle == 0) {
1166                         kmem_free((vm_offset_t)mem, size);
1167                         mem = NULL;
1168                 }
1169         } else {
1170                 *dma_handle = 0;
1171         }
1172         return (mem);
1173 }
1174
1175 struct lkpi_devres_dmam_coherent {
1176         size_t size;
1177         dma_addr_t *handle;
1178         void *mem;
1179 };
1180
1181 static void
1182 lkpi_dmam_free_coherent(struct device *dev, void *p)
1183 {
1184         struct lkpi_devres_dmam_coherent *dr;
1185
1186         dr = p;
1187         dma_free_coherent(dev, dr->size, dr->mem, *dr->handle);
1188 }
1189
1190 void *
1191 linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1192     gfp_t flag)
1193 {
1194         struct lkpi_devres_dmam_coherent *dr;
1195
1196         dr = lkpi_devres_alloc(lkpi_dmam_free_coherent,
1197            sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
1198
1199         if (dr == NULL)
1200                 return (NULL);
1201
1202         dr->size = size;
1203         dr->mem = linux_dma_alloc_coherent(dev, size, dma_handle, flag);
1204         dr->handle = dma_handle;
1205         if (dr->mem == NULL) {
1206                 lkpi_devres_free(dr);
1207                 return (NULL);
1208         }
1209
1210         lkpi_devres_add(dev, dr);
1211         return (dr->mem);
1212 }
1213
1214 void
1215 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,
1216     bus_dmasync_op_t op)
1217 {
1218         struct linux_dma_priv *priv;
1219         struct linux_dma_obj *obj;
1220
1221         priv = dev->dma_priv;
1222
1223         if (pctrie_is_empty(&priv->ptree))
1224                 return;
1225
1226         DMA_PRIV_LOCK(priv);
1227         obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1228         if (obj == NULL) {
1229                 DMA_PRIV_UNLOCK(priv);
1230                 return;
1231         }
1232
1233         bus_dmamap_sync(obj->dmat, obj->dmamap, op);
1234         DMA_PRIV_UNLOCK(priv);
1235 }
1236
1237 int
1238 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
1239     enum dma_data_direction direction, unsigned long attrs __unused)
1240 {
1241         struct linux_dma_priv *priv;
1242         struct scatterlist *sg;
1243         int i, nseg;
1244         bus_dma_segment_t seg;
1245
1246         priv = dev->dma_priv;
1247
1248         DMA_PRIV_LOCK(priv);
1249
1250         /* create common DMA map in the first S/G entry */
1251         if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
1252                 DMA_PRIV_UNLOCK(priv);
1253                 return (0);
1254         }
1255
1256         /* load all S/G list entries */
1257         for_each_sg(sgl, sg, nents, i) {
1258                 nseg = -1;
1259                 if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
1260                     sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
1261                     &seg, &nseg) != 0) {
1262                         bus_dmamap_unload(priv->dmat, sgl->dma_map);
1263                         bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1264                         DMA_PRIV_UNLOCK(priv);
1265                         return (0);
1266                 }
1267                 KASSERT(nseg == 0,
1268                     ("More than one segment (nseg=%d)", nseg + 1));
1269
1270                 sg_dma_address(sg) = seg.ds_addr;
1271         }
1272
1273         switch (direction) {
1274         case DMA_BIDIRECTIONAL:
1275                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1276                 break;
1277         case DMA_TO_DEVICE:
1278                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1279                 break;
1280         case DMA_FROM_DEVICE:
1281                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1282                 break;
1283         default:
1284                 break;
1285         }
1286
1287         DMA_PRIV_UNLOCK(priv);
1288
1289         return (nents);
1290 }
1291
1292 void
1293 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
1294     int nents __unused, enum dma_data_direction direction,
1295     unsigned long attrs __unused)
1296 {
1297         struct linux_dma_priv *priv;
1298
1299         priv = dev->dma_priv;
1300
1301         DMA_PRIV_LOCK(priv);
1302
1303         switch (direction) {
1304         case DMA_BIDIRECTIONAL:
1305                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1306                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1307                 break;
1308         case DMA_TO_DEVICE:
1309                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);
1310                 break;
1311         case DMA_FROM_DEVICE:
1312                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1313                 break;
1314         default:
1315                 break;
1316         }
1317
1318         bus_dmamap_unload(priv->dmat, sgl->dma_map);
1319         bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1320         DMA_PRIV_UNLOCK(priv);
1321 }
1322
1323 struct dma_pool {
1324         struct device  *pool_device;
1325         uma_zone_t      pool_zone;
1326         struct mtx      pool_lock;
1327         bus_dma_tag_t   pool_dmat;
1328         size_t          pool_entry_size;
1329         struct pctrie   pool_ptree;
1330 };
1331
1332 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
1333 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
1334
1335 static inline int
1336 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
1337 {
1338         struct linux_dma_obj *obj = mem;
1339         struct dma_pool *pool = arg;
1340         int error, nseg;
1341         bus_dma_segment_t seg;
1342
1343         nseg = -1;
1344         DMA_POOL_LOCK(pool);
1345         error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
1346             vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
1347             &seg, &nseg);
1348         DMA_POOL_UNLOCK(pool);
1349         if (error != 0) {
1350                 return (error);
1351         }
1352         KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1353         obj->dma_addr = seg.ds_addr;
1354
1355         return (0);
1356 }
1357
1358 static void
1359 dma_pool_obj_dtor(void *mem, int size, void *arg)
1360 {
1361         struct linux_dma_obj *obj = mem;
1362         struct dma_pool *pool = arg;
1363
1364         DMA_POOL_LOCK(pool);
1365         bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
1366         DMA_POOL_UNLOCK(pool);
1367 }
1368
1369 static int
1370 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
1371     int flags)
1372 {
1373         struct dma_pool *pool = arg;
1374         struct linux_dma_obj *obj;
1375         int error, i;
1376
1377         for (i = 0; i < count; i++) {
1378                 obj = uma_zalloc(linux_dma_obj_zone, flags);
1379                 if (obj == NULL)
1380                         break;
1381
1382                 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
1383                     BUS_DMA_NOWAIT, &obj->dmamap);
1384                 if (error!= 0) {
1385                         uma_zfree(linux_dma_obj_zone, obj);
1386                         break;
1387                 }
1388
1389                 store[i] = obj;
1390         }
1391
1392         return (i);
1393 }
1394
1395 static void
1396 dma_pool_obj_release(void *arg, void **store, int count)
1397 {
1398         struct dma_pool *pool = arg;
1399         struct linux_dma_obj *obj;
1400         int i;
1401
1402         for (i = 0; i < count; i++) {
1403                 obj = store[i];
1404                 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
1405                 uma_zfree(linux_dma_obj_zone, obj);
1406         }
1407 }
1408
1409 struct dma_pool *
1410 linux_dma_pool_create(char *name, struct device *dev, size_t size,
1411     size_t align, size_t boundary)
1412 {
1413         struct linux_dma_priv *priv;
1414         struct dma_pool *pool;
1415
1416         priv = dev->dma_priv;
1417
1418         pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1419         pool->pool_device = dev;
1420         pool->pool_entry_size = size;
1421
1422         if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
1423             align, boundary,            /* alignment, boundary */
1424             priv->dma_mask,             /* lowaddr */
1425             BUS_SPACE_MAXADDR,          /* highaddr */
1426             NULL, NULL,                 /* filtfunc, filtfuncarg */
1427             size,                       /* maxsize */
1428             1,                          /* nsegments */
1429             size,                       /* maxsegsz */
1430             0,                          /* flags */
1431             NULL, NULL,                 /* lockfunc, lockfuncarg */
1432             &pool->pool_dmat)) {
1433                 kfree(pool);
1434                 return (NULL);
1435         }
1436
1437         pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
1438             dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
1439             dma_pool_obj_release, pool, 0);
1440
1441         mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
1442         pctrie_init(&pool->pool_ptree);
1443
1444         return (pool);
1445 }
1446
1447 void
1448 linux_dma_pool_destroy(struct dma_pool *pool)
1449 {
1450
1451         uma_zdestroy(pool->pool_zone);
1452         bus_dma_tag_destroy(pool->pool_dmat);
1453         mtx_destroy(&pool->pool_lock);
1454         kfree(pool);
1455 }
1456
1457 void
1458 lkpi_dmam_pool_destroy(struct device *dev, void *p)
1459 {
1460         struct dma_pool *pool;
1461
1462         pool = *(struct dma_pool **)p;
1463         LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);
1464         linux_dma_pool_destroy(pool);
1465 }
1466
1467 void *
1468 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
1469     dma_addr_t *handle)
1470 {
1471         struct linux_dma_obj *obj;
1472
1473         obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);
1474         if (obj == NULL)
1475                 return (NULL);
1476
1477         DMA_POOL_LOCK(pool);
1478         if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
1479                 DMA_POOL_UNLOCK(pool);
1480                 uma_zfree_arg(pool->pool_zone, obj, pool);
1481                 return (NULL);
1482         }
1483         DMA_POOL_UNLOCK(pool);
1484
1485         *handle = obj->dma_addr;
1486         return (obj->vaddr);
1487 }
1488
1489 void
1490 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
1491 {
1492         struct linux_dma_obj *obj;
1493
1494         DMA_POOL_LOCK(pool);
1495         obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
1496         if (obj == NULL) {
1497                 DMA_POOL_UNLOCK(pool);
1498                 return;
1499         }
1500         LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
1501         DMA_POOL_UNLOCK(pool);
1502
1503         uma_zfree_arg(pool->pool_zone, obj, pool);
1504 }
1505
1506 static int
1507 linux_backlight_get_status(device_t dev, struct backlight_props *props)
1508 {
1509         struct pci_dev *pdev;
1510
1511         linux_set_current(curthread);
1512         pdev = device_get_softc(dev);
1513
1514         props->brightness = pdev->dev.bd->props.brightness;
1515         props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
1516         props->nlevels = 0;
1517
1518         return (0);
1519 }
1520
1521 static int
1522 linux_backlight_get_info(device_t dev, struct backlight_info *info)
1523 {
1524         struct pci_dev *pdev;
1525
1526         linux_set_current(curthread);
1527         pdev = device_get_softc(dev);
1528
1529         info->type = BACKLIGHT_TYPE_PANEL;
1530         strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
1531         return (0);
1532 }
1533
1534 static int
1535 linux_backlight_update_status(device_t dev, struct backlight_props *props)
1536 {
1537         struct pci_dev *pdev;
1538
1539         linux_set_current(curthread);
1540         pdev = device_get_softc(dev);
1541
1542         pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness *
1543                 props->brightness / 100;
1544         pdev->dev.bd->props.power = props->brightness == 0 ?
1545                 4/* FB_BLANK_POWERDOWN */ : 0/* FB_BLANK_UNBLANK */;
1546         return (pdev->dev.bd->ops->update_status(pdev->dev.bd));
1547 }
1548
1549 struct backlight_device *
1550 linux_backlight_device_register(const char *name, struct device *dev,
1551     void *data, const struct backlight_ops *ops, struct backlight_properties *props)
1552 {
1553
1554         dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
1555         dev->bd->ops = ops;
1556         dev->bd->props.type = props->type;
1557         dev->bd->props.max_brightness = props->max_brightness;
1558         dev->bd->props.brightness = props->brightness;
1559         dev->bd->props.power = props->power;
1560         dev->bd->data = data;
1561         dev->bd->dev = dev;
1562         dev->bd->name = strdup(name, M_DEVBUF);
1563
1564         dev->backlight_dev = backlight_register(name, dev->bsddev);
1565
1566         return (dev->bd);
1567 }
1568
1569 void
1570 linux_backlight_device_unregister(struct backlight_device *bd)
1571 {
1572
1573         backlight_destroy(bd->dev->backlight_dev);
1574         free(bd->name, M_DEVBUF);
1575         free(bd, M_DEVBUF);
1576 }