]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_pci.c
LinuxKPI: PCI: implement support for more than 1 MSI vector
[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
326         if (pci_msi_count(dev) > 0)
327                 pdev->msi_desc = malloc(pci_msi_count(dev) *
328                     sizeof(*pdev->msi_desc), M_DEVBUF, M_WAITOK | M_ZERO);
329
330         kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
331         kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
332         kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
333             kobject_name(&pdev->dev.kobj));
334         spin_lock_init(&pdev->dev.devres_lock);
335         INIT_LIST_HEAD(&pdev->dev.devres_head);
336 }
337
338 static void
339 lkpinew_pci_dev_release(struct device *dev)
340 {
341         struct pci_dev *pdev;
342         int i;
343
344         pdev = to_pci_dev(dev);
345         if (pdev->root != NULL)
346                 pci_dev_put(pdev->root);
347         if (pdev->bus->self != pdev)
348                 pci_dev_put(pdev->bus->self);
349         free(pdev->bus, M_DEVBUF);
350         if (pdev->msi_desc != NULL) {
351                 for (i = pci_msi_count(pdev->dev.bsddev) - 1; i >= 0; i--)
352                         free(pdev->msi_desc[i], M_DEVBUF);
353                 free(pdev->msi_desc, M_DEVBUF);
354         }
355         free(pdev, M_DEVBUF);
356 }
357
358 struct pci_dev *
359 lkpinew_pci_dev(device_t dev)
360 {
361         struct pci_dev *pdev;
362
363         pdev = malloc(sizeof(*pdev), M_DEVBUF, M_WAITOK|M_ZERO);
364         lkpifill_pci_dev(dev, pdev);
365         pdev->dev.release = lkpinew_pci_dev_release;
366
367         return (pdev);
368 }
369
370 struct pci_dev *
371 lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
372 {
373         device_t dev;
374         device_t devfrom = NULL;
375         struct pci_dev *pdev;
376
377         if (from != NULL)
378                 devfrom = from->dev.bsddev;
379
380         dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
381         if (dev == NULL)
382                 return (NULL);
383
384         pdev = lkpinew_pci_dev(dev);
385         return (pdev);
386 }
387
388 struct pci_dev *
389 lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,
390     unsigned int devfn)
391 {
392         device_t dev;
393         struct pci_dev *pdev;
394
395         dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
396         if (dev == NULL)
397                 return (NULL);
398
399         pdev = lkpinew_pci_dev(dev);
400         return (pdev);
401 }
402
403 static int
404 linux_pci_probe(device_t dev)
405 {
406         const struct pci_device_id *id;
407         struct pci_driver *pdrv;
408
409         if ((pdrv = linux_pci_find(dev, &id)) == NULL)
410                 return (ENXIO);
411         if (device_get_driver(dev) != &pdrv->bsddriver)
412                 return (ENXIO);
413         device_set_desc(dev, pdrv->name);
414
415         /* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */
416         if (pdrv->bsd_probe_return == 0)
417                 return (BUS_PROBE_DEFAULT);
418         else
419                 return (pdrv->bsd_probe_return);
420 }
421
422 static int
423 linux_pci_attach(device_t dev)
424 {
425         const struct pci_device_id *id;
426         struct pci_driver *pdrv;
427         struct pci_dev *pdev;
428
429         pdrv = linux_pci_find(dev, &id);
430         pdev = device_get_softc(dev);
431
432         MPASS(pdrv != NULL);
433         MPASS(pdev != NULL);
434
435         return (linux_pci_attach_device(dev, pdrv, id, pdev));
436 }
437
438 int
439 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
440     const struct pci_device_id *id, struct pci_dev *pdev)
441 {
442         struct resource_list_entry *rle;
443         device_t parent;
444         uintptr_t rid;
445         int error;
446         bool isdrm;
447
448         linux_set_current(curthread);
449
450         parent = device_get_parent(dev);
451         isdrm = pdrv != NULL && linux_is_drm(pdrv);
452
453         if (isdrm) {
454                 struct pci_devinfo *dinfo;
455
456                 dinfo = device_get_ivars(parent);
457                 device_set_ivars(dev, dinfo);
458         }
459
460         lkpifill_pci_dev(dev, pdev);
461         if (isdrm)
462                 PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);
463         else
464                 PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);
465         pdev->devfn = rid;
466         pdev->pdrv = pdrv;
467         rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);
468         if (rle != NULL)
469                 pdev->dev.irq = rle->start;
470         else
471                 pdev->dev.irq = LINUX_IRQ_INVALID;
472         pdev->irq = pdev->dev.irq;
473         error = linux_pdev_dma_init(pdev);
474         if (error)
475                 goto out_dma_init;
476
477         TAILQ_INIT(&pdev->mmio);
478
479         spin_lock(&pci_lock);
480         list_add(&pdev->links, &pci_devices);
481         spin_unlock(&pci_lock);
482
483         if (pdrv != NULL) {
484                 error = pdrv->probe(pdev, id);
485                 if (error)
486                         goto out_probe;
487         }
488         return (0);
489
490 out_probe:
491         free(pdev->bus, M_DEVBUF);
492         linux_pdev_dma_uninit(pdev);
493 out_dma_init:
494         spin_lock(&pci_lock);
495         list_del(&pdev->links);
496         spin_unlock(&pci_lock);
497         put_device(&pdev->dev);
498         return (-error);
499 }
500
501 static int
502 linux_pci_detach(device_t dev)
503 {
504         struct pci_dev *pdev;
505
506         pdev = device_get_softc(dev);
507
508         MPASS(pdev != NULL);
509
510         device_set_desc(dev, NULL);
511
512         return (linux_pci_detach_device(pdev));
513 }
514
515 int
516 linux_pci_detach_device(struct pci_dev *pdev)
517 {
518
519         linux_set_current(curthread);
520
521         if (pdev->pdrv != NULL)
522                 pdev->pdrv->remove(pdev);
523
524         if (pdev->root != NULL)
525                 pci_dev_put(pdev->root);
526         free(pdev->bus, M_DEVBUF);
527         linux_pdev_dma_uninit(pdev);
528
529         spin_lock(&pci_lock);
530         list_del(&pdev->links);
531         spin_unlock(&pci_lock);
532         put_device(&pdev->dev);
533
534         return (0);
535 }
536
537 static int
538 lkpi_pci_disable_dev(struct device *dev)
539 {
540
541         (void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);
542         (void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);
543         return (0);
544 }
545
546 struct pci_devres *
547 lkpi_pci_devres_get_alloc(struct pci_dev *pdev)
548 {
549         struct pci_devres *dr;
550
551         dr = lkpi_devres_find(&pdev->dev, lkpi_pci_devres_release, NULL, NULL);
552         if (dr == NULL) {
553                 dr = lkpi_devres_alloc(lkpi_pci_devres_release, sizeof(*dr),
554                     GFP_KERNEL | __GFP_ZERO);
555                 if (dr != NULL)
556                         lkpi_devres_add(&pdev->dev, dr);
557         }
558
559         return (dr);
560 }
561
562 void
563 lkpi_pci_devres_release(struct device *dev, void *p)
564 {
565         struct pci_devres *dr;
566         struct pci_dev *pdev;
567         int bar;
568
569         pdev = to_pci_dev(dev);
570         dr = p;
571
572         if (pdev->msix_enabled)
573                 lkpi_pci_disable_msix(pdev);
574         if (pdev->msi_enabled)
575                 lkpi_pci_disable_msi(pdev);
576
577         if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)
578                 dr->enable_io = false;
579
580         if (dr->region_mask == 0)
581                 return;
582         for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
583
584                 if ((dr->region_mask & (1 << bar)) == 0)
585                         continue;
586                 pci_release_region(pdev, bar);
587         }
588 }
589
590 struct pcim_iomap_devres *
591 lkpi_pcim_iomap_devres_find(struct pci_dev *pdev)
592 {
593         struct pcim_iomap_devres *dr;
594
595         dr = lkpi_devres_find(&pdev->dev, lkpi_pcim_iomap_table_release,
596             NULL, NULL);
597         if (dr == NULL) {
598                 dr = lkpi_devres_alloc(lkpi_pcim_iomap_table_release,
599                     sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
600                 if (dr != NULL)
601                         lkpi_devres_add(&pdev->dev, dr);
602         }
603
604         if (dr == NULL)
605                 device_printf(pdev->dev.bsddev, "%s: NULL\n", __func__);
606
607         return (dr);
608 }
609
610 void
611 lkpi_pcim_iomap_table_release(struct device *dev, void *p)
612 {
613         struct pcim_iomap_devres *dr;
614         struct pci_dev *pdev;
615         int bar;
616
617         dr = p;
618         pdev = to_pci_dev(dev);
619         for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
620
621                 if (dr->mmio_table[bar] == NULL)
622                         continue;
623
624                 pci_iounmap(pdev, dr->mmio_table[bar]);
625         }
626 }
627
628 static int
629 linux_pci_suspend(device_t dev)
630 {
631         const struct dev_pm_ops *pmops;
632         struct pm_message pm = { };
633         struct pci_dev *pdev;
634         int error;
635
636         error = 0;
637         linux_set_current(curthread);
638         pdev = device_get_softc(dev);
639         pmops = pdev->pdrv->driver.pm;
640
641         if (pdev->pdrv->suspend != NULL)
642                 error = -pdev->pdrv->suspend(pdev, pm);
643         else if (pmops != NULL && pmops->suspend != NULL) {
644                 error = -pmops->suspend(&pdev->dev);
645                 if (error == 0 && pmops->suspend_late != NULL)
646                         error = -pmops->suspend_late(&pdev->dev);
647         }
648         return (error);
649 }
650
651 static int
652 linux_pci_resume(device_t dev)
653 {
654         const struct dev_pm_ops *pmops;
655         struct pci_dev *pdev;
656         int error;
657
658         error = 0;
659         linux_set_current(curthread);
660         pdev = device_get_softc(dev);
661         pmops = pdev->pdrv->driver.pm;
662
663         if (pdev->pdrv->resume != NULL)
664                 error = -pdev->pdrv->resume(pdev);
665         else if (pmops != NULL && pmops->resume != NULL) {
666                 if (pmops->resume_early != NULL)
667                         error = -pmops->resume_early(&pdev->dev);
668                 if (error == 0 && pmops->resume != NULL)
669                         error = -pmops->resume(&pdev->dev);
670         }
671         return (error);
672 }
673
674 static int
675 linux_pci_shutdown(device_t dev)
676 {
677         struct pci_dev *pdev;
678
679         linux_set_current(curthread);
680         pdev = device_get_softc(dev);
681         if (pdev->pdrv->shutdown != NULL)
682                 pdev->pdrv->shutdown(pdev);
683         return (0);
684 }
685
686 static int
687 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
688 {
689         struct pci_dev *pdev;
690         int error;
691
692         linux_set_current(curthread);
693         pdev = device_get_softc(dev);
694         if (pdev->pdrv->bsd_iov_init != NULL)
695                 error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);
696         else
697                 error = EINVAL;
698         return (error);
699 }
700
701 static void
702 linux_pci_iov_uninit(device_t dev)
703 {
704         struct pci_dev *pdev;
705
706         linux_set_current(curthread);
707         pdev = device_get_softc(dev);
708         if (pdev->pdrv->bsd_iov_uninit != NULL)
709                 pdev->pdrv->bsd_iov_uninit(dev);
710 }
711
712 static int
713 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
714 {
715         struct pci_dev *pdev;
716         int error;
717
718         linux_set_current(curthread);
719         pdev = device_get_softc(dev);
720         if (pdev->pdrv->bsd_iov_add_vf != NULL)
721                 error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);
722         else
723                 error = EINVAL;
724         return (error);
725 }
726
727 static int
728 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
729 {
730         int error;
731
732         linux_set_current(curthread);
733         spin_lock(&pci_lock);
734         list_add(&pdrv->node, &pci_drivers);
735         spin_unlock(&pci_lock);
736         if (pdrv->bsddriver.name == NULL)
737                 pdrv->bsddriver.name = pdrv->name;
738         pdrv->bsddriver.methods = pci_methods;
739         pdrv->bsddriver.size = sizeof(struct pci_dev);
740
741         bus_topo_lock();
742         error = devclass_add_driver(dc, &pdrv->bsddriver,
743             BUS_PASS_DEFAULT, &pdrv->bsdclass);
744         bus_topo_unlock();
745         return (-error);
746 }
747
748 int
749 linux_pci_register_driver(struct pci_driver *pdrv)
750 {
751         devclass_t dc;
752
753         dc = devclass_find("pci");
754         if (dc == NULL)
755                 return (-ENXIO);
756         return (_linux_pci_register_driver(pdrv, dc));
757 }
758
759 struct resource_list_entry *
760 linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,
761     int type, int rid)
762 {
763         device_t dev;
764         struct resource *res;
765
766         KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,
767             ("trying to reserve non-BAR type %d", type));
768
769         dev = pdev->pdrv != NULL && linux_is_drm(pdev->pdrv) ?
770             device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
771         res = pci_reserve_map(device_get_parent(dev), dev, type, &rid, 0, ~0,
772             1, 1, 0);
773         if (res == NULL)
774                 return (NULL);
775         return (resource_list_find(rl, type, rid));
776 }
777
778 unsigned long
779 pci_resource_start(struct pci_dev *pdev, int bar)
780 {
781         struct resource_list_entry *rle;
782         rman_res_t newstart;
783         device_t dev;
784
785         if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
786                 return (0);
787         dev = pdev->pdrv != NULL && linux_is_drm(pdev->pdrv) ?
788             device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
789         if (BUS_TRANSLATE_RESOURCE(dev, rle->type, rle->start, &newstart)) {
790                 device_printf(pdev->dev.bsddev, "translate of %#jx failed\n",
791                     (uintmax_t)rle->start);
792                 return (0);
793         }
794         return (newstart);
795 }
796
797 unsigned long
798 pci_resource_len(struct pci_dev *pdev, int bar)
799 {
800         struct resource_list_entry *rle;
801
802         if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
803                 return (0);
804         return (rle->count);
805 }
806
807 int
808 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
809 {
810         struct resource *res;
811         struct pci_devres *dr;
812         struct pci_mmio_region *mmio;
813         int rid;
814         int type;
815
816         type = pci_resource_type(pdev, bar);
817         if (type < 0)
818                 return (-ENODEV);
819         rid = PCIR_BAR(bar);
820         res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
821             RF_ACTIVE|RF_SHAREABLE);
822         if (res == NULL) {
823                 device_printf(pdev->dev.bsddev, "%s: failed to alloc "
824                     "bar %d type %d rid %d\n",
825                     __func__, bar, type, PCIR_BAR(bar));
826                 return (-ENODEV);
827         }
828
829         /*
830          * It seems there is an implicit devres tracking on these if the device
831          * is managed; otherwise the resources are not automatiaclly freed on
832          * FreeBSD/LinuxKPI tough they should be/are expected to be by Linux
833          * drivers.
834          */
835         dr = lkpi_pci_devres_find(pdev);
836         if (dr != NULL) {
837                 dr->region_mask |= (1 << bar);
838                 dr->region_table[bar] = res;
839         }
840
841         /* Even if the device is not managed we need to track it for iomap. */
842         mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
843         mmio->rid = PCIR_BAR(bar);
844         mmio->type = type;
845         mmio->res = res;
846         TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
847
848         return (0);
849 }
850
851 struct resource *
852 _lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size __unused)
853 {
854         struct pci_mmio_region *mmio, *p;
855         int type;
856
857         type = pci_resource_type(pdev, bar);
858         if (type < 0) {
859                 device_printf(pdev->dev.bsddev, "%s: bar %d type %d\n",
860                      __func__, bar, type);
861                 return (NULL);
862         }
863
864         /*
865          * Check for duplicate mappings.
866          * This can happen if a driver calls pci_request_region() first.
867          */
868         TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
869                 if (mmio->type == type && mmio->rid == PCIR_BAR(bar)) {
870                         return (mmio->res);
871                 }
872         }
873
874         mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
875         mmio->rid = PCIR_BAR(bar);
876         mmio->type = type;
877         mmio->res = bus_alloc_resource_any(pdev->dev.bsddev, mmio->type,
878             &mmio->rid, RF_ACTIVE|RF_SHAREABLE);
879         if (mmio->res == NULL) {
880                 device_printf(pdev->dev.bsddev, "%s: failed to alloc "
881                     "bar %d type %d rid %d\n",
882                     __func__, bar, type, PCIR_BAR(bar));
883                 free(mmio, M_DEVBUF);
884                 return (NULL);
885         }
886         TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
887
888         return (mmio->res);
889 }
890
891 int
892 linux_pci_register_drm_driver(struct pci_driver *pdrv)
893 {
894         devclass_t dc;
895
896         dc = devclass_create("vgapci");
897         if (dc == NULL)
898                 return (-ENXIO);
899         pdrv->name = "drmn";
900         return (_linux_pci_register_driver(pdrv, dc));
901 }
902
903 void
904 linux_pci_unregister_driver(struct pci_driver *pdrv)
905 {
906         devclass_t bus;
907
908         bus = devclass_find("pci");
909
910         spin_lock(&pci_lock);
911         list_del(&pdrv->node);
912         spin_unlock(&pci_lock);
913         bus_topo_lock();
914         if (bus != NULL)
915                 devclass_delete_driver(bus, &pdrv->bsddriver);
916         bus_topo_unlock();
917 }
918
919 void
920 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
921 {
922         devclass_t bus;
923
924         bus = devclass_find("vgapci");
925
926         spin_lock(&pci_lock);
927         list_del(&pdrv->node);
928         spin_unlock(&pci_lock);
929         bus_topo_lock();
930         if (bus != NULL)
931                 devclass_delete_driver(bus, &pdrv->bsddriver);
932         bus_topo_unlock();
933 }
934
935 int
936 pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,
937     unsigned int flags)
938 {
939         int error;
940
941         if (flags & PCI_IRQ_MSIX) {
942                 struct msix_entry *entries;
943                 int i;
944
945                 entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL);
946                 if (entries == NULL) {
947                         error = -ENOMEM;
948                         goto out;
949                 }
950                 for (i = 0; i < maxv; ++i)
951                         entries[i].entry = i;
952                 error = pci_enable_msix(pdev, entries, maxv);
953 out:
954                 kfree(entries);
955                 if (error == 0 && pdev->msix_enabled)
956                         return (pdev->dev.irq_end - pdev->dev.irq_start);
957         }
958         if (flags & PCI_IRQ_MSI) {
959                 if (pci_msi_count(pdev->dev.bsddev) < minv)
960                         return (-ENOSPC);
961                 error = _lkpi_pci_enable_msi_range(pdev, minv, maxv);
962                 if (error == 0 && pdev->msi_enabled)
963                         return (pdev->dev.irq_end - pdev->dev.irq_start);
964         }
965         if (flags & PCI_IRQ_LEGACY) {
966                 if (pdev->irq)
967                         return (1);
968         }
969
970         return (-EINVAL);
971 }
972
973 bool
974 pci_device_is_present(struct pci_dev *pdev)
975 {
976         device_t dev;
977
978         dev = pdev->dev.bsddev;
979
980         return (bus_child_present(dev));
981 }
982
983 struct msi_desc *
984 lkpi_pci_msi_desc_alloc(int irq)
985 {
986         struct device *dev;
987         struct pci_dev *pdev;
988         struct msi_desc *desc;
989         struct pci_devinfo *dinfo;
990         struct pcicfg_msi *msi;
991         int vec;
992
993         dev = linux_pci_find_irq_dev(irq);
994         if (dev == NULL)
995                 return (NULL);
996
997         pdev = to_pci_dev(dev);
998
999         if (pdev->msi_desc == NULL)
1000                 return (NULL);
1001
1002         if (irq < pdev->dev.irq_start || irq >= pdev->dev.irq_end)
1003                 return (NULL);
1004
1005         vec = pdev->dev.irq_start - irq;
1006
1007         if (pdev->msi_desc[vec] != NULL)
1008                 return (pdev->msi_desc[vec]);
1009
1010         dinfo = device_get_ivars(dev->bsddev);
1011         msi = &dinfo->cfg.msi;
1012
1013         desc = malloc(sizeof(*desc), M_DEVBUF, M_WAITOK | M_ZERO);
1014
1015         desc->msi_attrib.is_64 =
1016            (msi->msi_ctrl & PCIM_MSICTRL_64BIT) ? true : false;
1017         desc->msg.data = msi->msi_data;
1018
1019         pdev->msi_desc[vec] = desc;
1020
1021         return (desc);
1022 }
1023
1024 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
1025
1026 struct linux_dma_obj {
1027         void            *vaddr;
1028         uint64_t        dma_addr;
1029         bus_dmamap_t    dmamap;
1030         bus_dma_tag_t   dmat;
1031 };
1032
1033 static uma_zone_t linux_dma_trie_zone;
1034 static uma_zone_t linux_dma_obj_zone;
1035
1036 static void
1037 linux_dma_init(void *arg)
1038 {
1039
1040         linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
1041             pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
1042             UMA_ALIGN_PTR, 0);
1043         linux_dma_obj_zone = uma_zcreate("linux_dma_object",
1044             sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
1045             UMA_ALIGN_PTR, 0);
1046         lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);
1047 }
1048 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
1049
1050 static void
1051 linux_dma_uninit(void *arg)
1052 {
1053
1054         counter_u64_free(lkpi_pci_nseg1_fail);
1055         uma_zdestroy(linux_dma_obj_zone);
1056         uma_zdestroy(linux_dma_trie_zone);
1057 }
1058 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
1059
1060 static void *
1061 linux_dma_trie_alloc(struct pctrie *ptree)
1062 {
1063
1064         return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
1065 }
1066
1067 static void
1068 linux_dma_trie_free(struct pctrie *ptree, void *node)
1069 {
1070
1071         uma_zfree(linux_dma_trie_zone, node);
1072 }
1073
1074 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
1075     linux_dma_trie_free);
1076
1077 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1078 static dma_addr_t
1079 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,
1080     bus_dma_tag_t dmat)
1081 {
1082         struct linux_dma_priv *priv;
1083         struct linux_dma_obj *obj;
1084         int error, nseg;
1085         bus_dma_segment_t seg;
1086
1087         priv = dev->dma_priv;
1088
1089         /*
1090          * If the resultant mapping will be entirely 1:1 with the
1091          * physical address, short-circuit the remainder of the
1092          * bus_dma API.  This avoids tracking collisions in the pctrie
1093          * with the additional benefit of reducing overhead.
1094          */
1095         if (bus_dma_id_mapped(dmat, phys, len))
1096                 return (phys);
1097
1098         obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
1099         if (obj == NULL) {
1100                 return (0);
1101         }
1102         obj->dmat = dmat;
1103
1104         DMA_PRIV_LOCK(priv);
1105         if (bus_dmamap_create(obj->dmat, 0, &obj->dmamap) != 0) {
1106                 DMA_PRIV_UNLOCK(priv);
1107                 uma_zfree(linux_dma_obj_zone, obj);
1108                 return (0);
1109         }
1110
1111         nseg = -1;
1112         if (_bus_dmamap_load_phys(obj->dmat, obj->dmamap, phys, len,
1113             BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
1114                 bus_dmamap_destroy(obj->dmat, obj->dmamap);
1115                 DMA_PRIV_UNLOCK(priv);
1116                 uma_zfree(linux_dma_obj_zone, obj);
1117                 counter_u64_add(lkpi_pci_nseg1_fail, 1);
1118                 if (linuxkpi_debug)
1119                         dump_stack();
1120                 return (0);
1121         }
1122
1123         KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1124         obj->dma_addr = seg.ds_addr;
1125
1126         error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
1127         if (error != 0) {
1128                 bus_dmamap_unload(obj->dmat, obj->dmamap);
1129                 bus_dmamap_destroy(obj->dmat, obj->dmamap);
1130                 DMA_PRIV_UNLOCK(priv);
1131                 uma_zfree(linux_dma_obj_zone, obj);
1132                 return (0);
1133         }
1134         DMA_PRIV_UNLOCK(priv);
1135         return (obj->dma_addr);
1136 }
1137 #else
1138 static dma_addr_t
1139 linux_dma_map_phys_common(struct device *dev __unused, vm_paddr_t phys,
1140     size_t len __unused, bus_dma_tag_t dmat __unused)
1141 {
1142         return (phys);
1143 }
1144 #endif
1145
1146 dma_addr_t
1147 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
1148 {
1149         struct linux_dma_priv *priv;
1150
1151         priv = dev->dma_priv;
1152         return (linux_dma_map_phys_common(dev, phys, len, priv->dmat));
1153 }
1154
1155 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1156 void
1157 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1158 {
1159         struct linux_dma_priv *priv;
1160         struct linux_dma_obj *obj;
1161
1162         priv = dev->dma_priv;
1163
1164         if (pctrie_is_empty(&priv->ptree))
1165                 return;
1166
1167         DMA_PRIV_LOCK(priv);
1168         obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1169         if (obj == NULL) {
1170                 DMA_PRIV_UNLOCK(priv);
1171                 return;
1172         }
1173         LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
1174         bus_dmamap_unload(obj->dmat, obj->dmamap);
1175         bus_dmamap_destroy(obj->dmat, obj->dmamap);
1176         DMA_PRIV_UNLOCK(priv);
1177
1178         uma_zfree(linux_dma_obj_zone, obj);
1179 }
1180 #else
1181 void
1182 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1183 {
1184 }
1185 #endif
1186
1187 void *
1188 linux_dma_alloc_coherent(struct device *dev, size_t size,
1189     dma_addr_t *dma_handle, gfp_t flag)
1190 {
1191         struct linux_dma_priv *priv;
1192         vm_paddr_t high;
1193         size_t align;
1194         void *mem;
1195
1196         if (dev == NULL || dev->dma_priv == NULL) {
1197                 *dma_handle = 0;
1198                 return (NULL);
1199         }
1200         priv = dev->dma_priv;
1201         if (priv->dma_coherent_mask)
1202                 high = priv->dma_coherent_mask;
1203         else
1204                 /* Coherent is lower 32bit only by default in Linux. */
1205                 high = BUS_SPACE_MAXADDR_32BIT;
1206         align = PAGE_SIZE << get_order(size);
1207         /* Always zero the allocation. */
1208         flag |= M_ZERO;
1209         mem = (void *)kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,
1210             align, 0, VM_MEMATTR_DEFAULT);
1211         if (mem != NULL) {
1212                 *dma_handle = linux_dma_map_phys_common(dev, vtophys(mem), size,
1213                     priv->dmat_coherent);
1214                 if (*dma_handle == 0) {
1215                         kmem_free((vm_offset_t)mem, size);
1216                         mem = NULL;
1217                 }
1218         } else {
1219                 *dma_handle = 0;
1220         }
1221         return (mem);
1222 }
1223
1224 struct lkpi_devres_dmam_coherent {
1225         size_t size;
1226         dma_addr_t *handle;
1227         void *mem;
1228 };
1229
1230 static void
1231 lkpi_dmam_free_coherent(struct device *dev, void *p)
1232 {
1233         struct lkpi_devres_dmam_coherent *dr;
1234
1235         dr = p;
1236         dma_free_coherent(dev, dr->size, dr->mem, *dr->handle);
1237 }
1238
1239 void *
1240 linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1241     gfp_t flag)
1242 {
1243         struct lkpi_devres_dmam_coherent *dr;
1244
1245         dr = lkpi_devres_alloc(lkpi_dmam_free_coherent,
1246             sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
1247
1248         if (dr == NULL)
1249                 return (NULL);
1250
1251         dr->size = size;
1252         dr->mem = linux_dma_alloc_coherent(dev, size, dma_handle, flag);
1253         dr->handle = dma_handle;
1254         if (dr->mem == NULL) {
1255                 lkpi_devres_free(dr);
1256                 return (NULL);
1257         }
1258
1259         lkpi_devres_add(dev, dr);
1260         return (dr->mem);
1261 }
1262
1263 void
1264 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,
1265     bus_dmasync_op_t op)
1266 {
1267         struct linux_dma_priv *priv;
1268         struct linux_dma_obj *obj;
1269
1270         priv = dev->dma_priv;
1271
1272         if (pctrie_is_empty(&priv->ptree))
1273                 return;
1274
1275         DMA_PRIV_LOCK(priv);
1276         obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1277         if (obj == NULL) {
1278                 DMA_PRIV_UNLOCK(priv);
1279                 return;
1280         }
1281
1282         bus_dmamap_sync(obj->dmat, obj->dmamap, op);
1283         DMA_PRIV_UNLOCK(priv);
1284 }
1285
1286 int
1287 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
1288     enum dma_data_direction direction, unsigned long attrs __unused)
1289 {
1290         struct linux_dma_priv *priv;
1291         struct scatterlist *sg;
1292         int i, nseg;
1293         bus_dma_segment_t seg;
1294
1295         priv = dev->dma_priv;
1296
1297         DMA_PRIV_LOCK(priv);
1298
1299         /* create common DMA map in the first S/G entry */
1300         if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
1301                 DMA_PRIV_UNLOCK(priv);
1302                 return (0);
1303         }
1304
1305         /* load all S/G list entries */
1306         for_each_sg(sgl, sg, nents, i) {
1307                 nseg = -1;
1308                 if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
1309                     sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
1310                     &seg, &nseg) != 0) {
1311                         bus_dmamap_unload(priv->dmat, sgl->dma_map);
1312                         bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1313                         DMA_PRIV_UNLOCK(priv);
1314                         return (0);
1315                 }
1316                 KASSERT(nseg == 0,
1317                     ("More than one segment (nseg=%d)", nseg + 1));
1318
1319                 sg_dma_address(sg) = seg.ds_addr;
1320         }
1321
1322         switch (direction) {
1323         case DMA_BIDIRECTIONAL:
1324                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1325                 break;
1326         case DMA_TO_DEVICE:
1327                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1328                 break;
1329         case DMA_FROM_DEVICE:
1330                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1331                 break;
1332         default:
1333                 break;
1334         }
1335
1336         DMA_PRIV_UNLOCK(priv);
1337
1338         return (nents);
1339 }
1340
1341 void
1342 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
1343     int nents __unused, enum dma_data_direction direction,
1344     unsigned long attrs __unused)
1345 {
1346         struct linux_dma_priv *priv;
1347
1348         priv = dev->dma_priv;
1349
1350         DMA_PRIV_LOCK(priv);
1351
1352         switch (direction) {
1353         case DMA_BIDIRECTIONAL:
1354                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1355                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1356                 break;
1357         case DMA_TO_DEVICE:
1358                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);
1359                 break;
1360         case DMA_FROM_DEVICE:
1361                 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1362                 break;
1363         default:
1364                 break;
1365         }
1366
1367         bus_dmamap_unload(priv->dmat, sgl->dma_map);
1368         bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1369         DMA_PRIV_UNLOCK(priv);
1370 }
1371
1372 struct dma_pool {
1373         struct device  *pool_device;
1374         uma_zone_t      pool_zone;
1375         struct mtx      pool_lock;
1376         bus_dma_tag_t   pool_dmat;
1377         size_t          pool_entry_size;
1378         struct pctrie   pool_ptree;
1379 };
1380
1381 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
1382 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
1383
1384 static inline int
1385 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
1386 {
1387         struct linux_dma_obj *obj = mem;
1388         struct dma_pool *pool = arg;
1389         int error, nseg;
1390         bus_dma_segment_t seg;
1391
1392         nseg = -1;
1393         DMA_POOL_LOCK(pool);
1394         error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
1395             vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
1396             &seg, &nseg);
1397         DMA_POOL_UNLOCK(pool);
1398         if (error != 0) {
1399                 return (error);
1400         }
1401         KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1402         obj->dma_addr = seg.ds_addr;
1403
1404         return (0);
1405 }
1406
1407 static void
1408 dma_pool_obj_dtor(void *mem, int size, void *arg)
1409 {
1410         struct linux_dma_obj *obj = mem;
1411         struct dma_pool *pool = arg;
1412
1413         DMA_POOL_LOCK(pool);
1414         bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
1415         DMA_POOL_UNLOCK(pool);
1416 }
1417
1418 static int
1419 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
1420     int flags)
1421 {
1422         struct dma_pool *pool = arg;
1423         struct linux_dma_obj *obj;
1424         int error, i;
1425
1426         for (i = 0; i < count; i++) {
1427                 obj = uma_zalloc(linux_dma_obj_zone, flags);
1428                 if (obj == NULL)
1429                         break;
1430
1431                 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
1432                     BUS_DMA_NOWAIT, &obj->dmamap);
1433                 if (error!= 0) {
1434                         uma_zfree(linux_dma_obj_zone, obj);
1435                         break;
1436                 }
1437
1438                 store[i] = obj;
1439         }
1440
1441         return (i);
1442 }
1443
1444 static void
1445 dma_pool_obj_release(void *arg, void **store, int count)
1446 {
1447         struct dma_pool *pool = arg;
1448         struct linux_dma_obj *obj;
1449         int i;
1450
1451         for (i = 0; i < count; i++) {
1452                 obj = store[i];
1453                 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
1454                 uma_zfree(linux_dma_obj_zone, obj);
1455         }
1456 }
1457
1458 struct dma_pool *
1459 linux_dma_pool_create(char *name, struct device *dev, size_t size,
1460     size_t align, size_t boundary)
1461 {
1462         struct linux_dma_priv *priv;
1463         struct dma_pool *pool;
1464
1465         priv = dev->dma_priv;
1466
1467         pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1468         pool->pool_device = dev;
1469         pool->pool_entry_size = size;
1470
1471         if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
1472             align, boundary,            /* alignment, boundary */
1473             priv->dma_mask,             /* lowaddr */
1474             BUS_SPACE_MAXADDR,          /* highaddr */
1475             NULL, NULL,                 /* filtfunc, filtfuncarg */
1476             size,                       /* maxsize */
1477             1,                          /* nsegments */
1478             size,                       /* maxsegsz */
1479             0,                          /* flags */
1480             NULL, NULL,                 /* lockfunc, lockfuncarg */
1481             &pool->pool_dmat)) {
1482                 kfree(pool);
1483                 return (NULL);
1484         }
1485
1486         pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
1487             dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
1488             dma_pool_obj_release, pool, 0);
1489
1490         mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
1491         pctrie_init(&pool->pool_ptree);
1492
1493         return (pool);
1494 }
1495
1496 void
1497 linux_dma_pool_destroy(struct dma_pool *pool)
1498 {
1499
1500         uma_zdestroy(pool->pool_zone);
1501         bus_dma_tag_destroy(pool->pool_dmat);
1502         mtx_destroy(&pool->pool_lock);
1503         kfree(pool);
1504 }
1505
1506 void
1507 lkpi_dmam_pool_destroy(struct device *dev, void *p)
1508 {
1509         struct dma_pool *pool;
1510
1511         pool = *(struct dma_pool **)p;
1512         LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);
1513         linux_dma_pool_destroy(pool);
1514 }
1515
1516 void *
1517 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
1518     dma_addr_t *handle)
1519 {
1520         struct linux_dma_obj *obj;
1521
1522         obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);
1523         if (obj == NULL)
1524                 return (NULL);
1525
1526         DMA_POOL_LOCK(pool);
1527         if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
1528                 DMA_POOL_UNLOCK(pool);
1529                 uma_zfree_arg(pool->pool_zone, obj, pool);
1530                 return (NULL);
1531         }
1532         DMA_POOL_UNLOCK(pool);
1533
1534         *handle = obj->dma_addr;
1535         return (obj->vaddr);
1536 }
1537
1538 void
1539 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
1540 {
1541         struct linux_dma_obj *obj;
1542
1543         DMA_POOL_LOCK(pool);
1544         obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
1545         if (obj == NULL) {
1546                 DMA_POOL_UNLOCK(pool);
1547                 return;
1548         }
1549         LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
1550         DMA_POOL_UNLOCK(pool);
1551
1552         uma_zfree_arg(pool->pool_zone, obj, pool);
1553 }
1554
1555 static int
1556 linux_backlight_get_status(device_t dev, struct backlight_props *props)
1557 {
1558         struct pci_dev *pdev;
1559
1560         linux_set_current(curthread);
1561         pdev = device_get_softc(dev);
1562
1563         props->brightness = pdev->dev.bd->props.brightness;
1564         props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
1565         props->nlevels = 0;
1566
1567         return (0);
1568 }
1569
1570 static int
1571 linux_backlight_get_info(device_t dev, struct backlight_info *info)
1572 {
1573         struct pci_dev *pdev;
1574
1575         linux_set_current(curthread);
1576         pdev = device_get_softc(dev);
1577
1578         info->type = BACKLIGHT_TYPE_PANEL;
1579         strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
1580         return (0);
1581 }
1582
1583 static int
1584 linux_backlight_update_status(device_t dev, struct backlight_props *props)
1585 {
1586         struct pci_dev *pdev;
1587
1588         linux_set_current(curthread);
1589         pdev = device_get_softc(dev);
1590
1591         pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness *
1592                 props->brightness / 100;
1593         pdev->dev.bd->props.power = props->brightness == 0 ?
1594                 4/* FB_BLANK_POWERDOWN */ : 0/* FB_BLANK_UNBLANK */;
1595         return (pdev->dev.bd->ops->update_status(pdev->dev.bd));
1596 }
1597
1598 struct backlight_device *
1599 linux_backlight_device_register(const char *name, struct device *dev,
1600     void *data, const struct backlight_ops *ops, struct backlight_properties *props)
1601 {
1602
1603         dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
1604         dev->bd->ops = ops;
1605         dev->bd->props.type = props->type;
1606         dev->bd->props.max_brightness = props->max_brightness;
1607         dev->bd->props.brightness = props->brightness;
1608         dev->bd->props.power = props->power;
1609         dev->bd->data = data;
1610         dev->bd->dev = dev;
1611         dev->bd->name = strdup(name, M_DEVBUF);
1612
1613         dev->backlight_dev = backlight_register(name, dev->bsddev);
1614
1615         return (dev->bd);
1616 }
1617
1618 void
1619 linux_backlight_device_unregister(struct backlight_device *bd)
1620 {
1621
1622         backlight_destroy(bd->dev->backlight_dev);
1623         free(bd->name, M_DEVBUF);
1624         free(bd, M_DEVBUF);
1625 }