2 * Copyright (c) 2015-2016 Mellanox Technologies, Ltd.
4 * Copyright (c) 2020-2022 The FreeBSD Foundation
6 * Portions of this software were developed by Björn Zeeb
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
41 #include <sys/mutex.h>
42 #include <sys/fcntl.h>
44 #include <sys/filio.h>
45 #include <sys/pciio.h>
46 #include <sys/pctrie.h>
47 #include <sys/rwlock.h>
52 #include <machine/stdarg.h>
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>
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>
69 #include <linux/vmalloc.h>
70 #include <linux/pci.h>
71 #include <linux/compat.h>
73 #include <linux/backlight.h>
75 #include "backlight_if.h"
78 /* Undef the linux function macro defined in linux/pci.h */
81 extern int linuxkpi_debug;
83 SYSCTL_DECL(_compat_linuxkpi);
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");
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);
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),
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),
120 struct linux_dma_priv {
123 uint64_t dma_coherent_mask;
124 bus_dma_tag_t dmat_coherent;
128 #define DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
129 #define DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
132 linux_is_drm(struct pci_driver *pdrv)
134 return (pdrv->name != NULL && strcmp(pdrv->name, "drmn") == 0);
138 linux_pdev_dma_uninit(struct pci_dev *pdev)
140 struct linux_dma_priv *priv;
142 priv = pdev->dev.dma_priv;
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);
154 linux_pdev_dma_init(struct pci_dev *pdev)
156 struct linux_dma_priv *priv;
159 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
161 mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
162 pctrie_init(&priv->ptree);
164 pdev->dev.dma_priv = priv;
166 /* Create a default DMA tags. */
167 error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
170 /* Coherent is lower 32bit only by default in Linux. */
171 error = linux_dma_tag_init_coherent(&pdev->dev, DMA_BIT_MASK(32));
178 linux_pdev_dma_uninit(pdev);
183 linux_dma_tag_init(struct device *dev, u64 dma_mask)
185 struct linux_dma_priv *priv;
188 priv = dev->dma_priv;
191 if (priv->dma_mask == dma_mask)
194 bus_dma_tag_destroy(priv->dmat);
197 priv->dma_mask = dma_mask;
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 */
206 BUS_SPACE_MAXSIZE, /* maxsegsz */
208 NULL, NULL, /* lockfunc, lockfuncarg */
214 linux_dma_tag_init_coherent(struct device *dev, u64 dma_mask)
216 struct linux_dma_priv *priv;
219 priv = dev->dma_priv;
221 if (priv->dmat_coherent) {
222 if (priv->dma_coherent_mask == dma_mask)
225 bus_dma_tag_destroy(priv->dmat_coherent);
228 priv->dma_coherent_mask = dma_mask;
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 */
237 BUS_SPACE_MAXSIZE, /* maxsegsz */
239 NULL, NULL, /* lockfunc, lockfuncarg */
240 &priv->dmat_coherent);
244 static struct pci_driver *
245 linux_pci_find(device_t dev, const struct pci_device_id **idp)
247 const struct pci_device_id *id;
248 struct pci_driver *pdrv;
254 vendor = pci_get_vendor(dev);
255 device = pci_get_device(dev);
256 subvendor = pci_get_subvendor(dev);
257 subdevice = pci_get_subdevice(dev);
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)) {
267 spin_unlock(&pci_lock);
272 spin_unlock(&pci_lock);
277 lkpi_pci_get_device(uint16_t vendor, uint16_t device, struct pci_dev *odev)
279 struct pci_dev *pdev;
281 KASSERT(odev == NULL, ("%s: odev argument not yet supported\n", __func__));
283 spin_lock(&pci_lock);
284 list_for_each_entry(pdev, &pci_devices, links) {
285 if (pdev->vendor == vendor && pdev->device == device)
288 spin_unlock(&pci_lock);
294 lkpi_pci_dev_release(struct device *dev)
297 lkpi_devres_release_free_list(dev);
298 spin_lock_destroy(&dev->devres_lock);
302 lkpifill_pci_dev(device_t dev, struct pci_dev *pdev)
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);
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.
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);
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);
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);
339 lkpinew_pci_dev_release(struct device *dev)
341 struct pci_dev *pdev;
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);
355 free(pdev, M_DEVBUF);
359 lkpinew_pci_dev(device_t dev)
361 struct pci_dev *pdev;
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;
371 lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
374 device_t devfrom = NULL;
375 struct pci_dev *pdev;
378 devfrom = from->dev.bsddev;
380 dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
384 pdev = lkpinew_pci_dev(dev);
389 lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,
393 struct pci_dev *pdev;
395 dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
399 pdev = lkpinew_pci_dev(dev);
404 linux_pci_probe(device_t dev)
406 const struct pci_device_id *id;
407 struct pci_driver *pdrv;
409 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
411 if (device_get_driver(dev) != &pdrv->bsddriver)
413 device_set_desc(dev, pdrv->name);
415 /* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */
416 if (pdrv->bsd_probe_return == 0)
417 return (BUS_PROBE_DEFAULT);
419 return (pdrv->bsd_probe_return);
423 linux_pci_attach(device_t dev)
425 const struct pci_device_id *id;
426 struct pci_driver *pdrv;
427 struct pci_dev *pdev;
429 pdrv = linux_pci_find(dev, &id);
430 pdev = device_get_softc(dev);
435 return (linux_pci_attach_device(dev, pdrv, id, pdev));
439 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
440 const struct pci_device_id *id, struct pci_dev *pdev)
442 struct resource_list_entry *rle;
448 linux_set_current(curthread);
450 parent = device_get_parent(dev);
451 isdrm = pdrv != NULL && linux_is_drm(pdrv);
454 struct pci_devinfo *dinfo;
456 dinfo = device_get_ivars(parent);
457 device_set_ivars(dev, dinfo);
460 lkpifill_pci_dev(dev, pdev);
462 PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);
464 PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);
467 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);
469 pdev->dev.irq = rle->start;
471 pdev->dev.irq = LINUX_IRQ_INVALID;
472 pdev->irq = pdev->dev.irq;
473 error = linux_pdev_dma_init(pdev);
477 TAILQ_INIT(&pdev->mmio);
479 spin_lock(&pci_lock);
480 list_add(&pdev->links, &pci_devices);
481 spin_unlock(&pci_lock);
484 error = pdrv->probe(pdev, id);
491 free(pdev->bus, M_DEVBUF);
492 linux_pdev_dma_uninit(pdev);
494 spin_lock(&pci_lock);
495 list_del(&pdev->links);
496 spin_unlock(&pci_lock);
497 put_device(&pdev->dev);
502 linux_pci_detach(device_t dev)
504 struct pci_dev *pdev;
506 pdev = device_get_softc(dev);
510 device_set_desc(dev, NULL);
512 return (linux_pci_detach_device(pdev));
516 linux_pci_detach_device(struct pci_dev *pdev)
519 linux_set_current(curthread);
521 if (pdev->pdrv != NULL)
522 pdev->pdrv->remove(pdev);
524 if (pdev->root != NULL)
525 pci_dev_put(pdev->root);
526 free(pdev->bus, M_DEVBUF);
527 linux_pdev_dma_uninit(pdev);
529 spin_lock(&pci_lock);
530 list_del(&pdev->links);
531 spin_unlock(&pci_lock);
532 put_device(&pdev->dev);
538 lkpi_pci_disable_dev(struct device *dev)
541 (void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);
542 (void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);
547 lkpi_pci_devres_get_alloc(struct pci_dev *pdev)
549 struct pci_devres *dr;
551 dr = lkpi_devres_find(&pdev->dev, lkpi_pci_devres_release, NULL, NULL);
553 dr = lkpi_devres_alloc(lkpi_pci_devres_release, sizeof(*dr),
554 GFP_KERNEL | __GFP_ZERO);
556 lkpi_devres_add(&pdev->dev, dr);
563 lkpi_pci_devres_release(struct device *dev, void *p)
565 struct pci_devres *dr;
566 struct pci_dev *pdev;
569 pdev = to_pci_dev(dev);
572 if (pdev->msix_enabled)
573 lkpi_pci_disable_msix(pdev);
574 if (pdev->msi_enabled)
575 lkpi_pci_disable_msi(pdev);
577 if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)
578 dr->enable_io = false;
580 if (dr->region_mask == 0)
582 for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
584 if ((dr->region_mask & (1 << bar)) == 0)
586 pci_release_region(pdev, bar);
590 struct pcim_iomap_devres *
591 lkpi_pcim_iomap_devres_find(struct pci_dev *pdev)
593 struct pcim_iomap_devres *dr;
595 dr = lkpi_devres_find(&pdev->dev, lkpi_pcim_iomap_table_release,
598 dr = lkpi_devres_alloc(lkpi_pcim_iomap_table_release,
599 sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
601 lkpi_devres_add(&pdev->dev, dr);
605 device_printf(pdev->dev.bsddev, "%s: NULL\n", __func__);
611 lkpi_pcim_iomap_table_release(struct device *dev, void *p)
613 struct pcim_iomap_devres *dr;
614 struct pci_dev *pdev;
618 pdev = to_pci_dev(dev);
619 for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
621 if (dr->mmio_table[bar] == NULL)
624 pci_iounmap(pdev, dr->mmio_table[bar]);
629 linux_pci_suspend(device_t dev)
631 const struct dev_pm_ops *pmops;
632 struct pm_message pm = { };
633 struct pci_dev *pdev;
637 linux_set_current(curthread);
638 pdev = device_get_softc(dev);
639 pmops = pdev->pdrv->driver.pm;
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);
652 linux_pci_resume(device_t dev)
654 const struct dev_pm_ops *pmops;
655 struct pci_dev *pdev;
659 linux_set_current(curthread);
660 pdev = device_get_softc(dev);
661 pmops = pdev->pdrv->driver.pm;
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);
675 linux_pci_shutdown(device_t dev)
677 struct pci_dev *pdev;
679 linux_set_current(curthread);
680 pdev = device_get_softc(dev);
681 if (pdev->pdrv->shutdown != NULL)
682 pdev->pdrv->shutdown(pdev);
687 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
689 struct pci_dev *pdev;
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);
702 linux_pci_iov_uninit(device_t dev)
704 struct pci_dev *pdev;
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);
713 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
715 struct pci_dev *pdev;
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);
728 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
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);
742 error = devclass_add_driver(dc, &pdrv->bsddriver,
743 BUS_PASS_DEFAULT, &pdrv->bsdclass);
749 linux_pci_register_driver(struct pci_driver *pdrv)
753 dc = devclass_find("pci");
756 return (_linux_pci_register_driver(pdrv, dc));
759 struct resource_list_entry *
760 linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,
764 struct resource *res;
766 KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,
767 ("trying to reserve non-BAR type %d", type));
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,
775 return (resource_list_find(rl, type, rid));
779 pci_resource_start(struct pci_dev *pdev, int bar)
781 struct resource_list_entry *rle;
785 if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
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);
798 pci_resource_len(struct pci_dev *pdev, int bar)
800 struct resource_list_entry *rle;
802 if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
808 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
810 struct resource *res;
811 struct pci_devres *dr;
812 struct pci_mmio_region *mmio;
816 type = pci_resource_type(pdev, bar);
820 res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
821 RF_ACTIVE|RF_SHAREABLE);
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));
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
835 dr = lkpi_pci_devres_find(pdev);
837 dr->region_mask |= (1 << bar);
838 dr->region_table[bar] = res;
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);
846 TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
852 _lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size __unused)
854 struct pci_mmio_region *mmio, *p;
857 type = pci_resource_type(pdev, bar);
859 device_printf(pdev->dev.bsddev, "%s: bar %d type %d\n",
860 __func__, bar, type);
865 * Check for duplicate mappings.
866 * This can happen if a driver calls pci_request_region() first.
868 TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
869 if (mmio->type == type && mmio->rid == PCIR_BAR(bar)) {
874 mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
875 mmio->rid = PCIR_BAR(bar);
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);
886 TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
892 linux_pci_register_drm_driver(struct pci_driver *pdrv)
896 dc = devclass_create("vgapci");
900 return (_linux_pci_register_driver(pdrv, dc));
904 linux_pci_unregister_driver(struct pci_driver *pdrv)
908 bus = devclass_find("pci");
910 spin_lock(&pci_lock);
911 list_del(&pdrv->node);
912 spin_unlock(&pci_lock);
915 devclass_delete_driver(bus, &pdrv->bsddriver);
920 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
924 bus = devclass_find("vgapci");
926 spin_lock(&pci_lock);
927 list_del(&pdrv->node);
928 spin_unlock(&pci_lock);
931 devclass_delete_driver(bus, &pdrv->bsddriver);
936 pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,
941 if (flags & PCI_IRQ_MSIX) {
942 struct msix_entry *entries;
945 entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL);
946 if (entries == NULL) {
950 for (i = 0; i < maxv; ++i)
951 entries[i].entry = i;
952 error = pci_enable_msix(pdev, entries, maxv);
955 if (error == 0 && pdev->msix_enabled)
956 return (pdev->dev.irq_end - pdev->dev.irq_start);
958 if (flags & PCI_IRQ_MSI) {
959 if (pci_msi_count(pdev->dev.bsddev) < minv)
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);
965 if (flags & PCI_IRQ_LEGACY) {
974 pci_device_is_present(struct pci_dev *pdev)
978 dev = pdev->dev.bsddev;
980 return (bus_child_present(dev));
984 lkpi_pci_msi_desc_alloc(int irq)
987 struct pci_dev *pdev;
988 struct msi_desc *desc;
989 struct pci_devinfo *dinfo;
990 struct pcicfg_msi *msi;
993 dev = linux_pci_find_irq_dev(irq);
997 pdev = to_pci_dev(dev);
999 if (pdev->msi_desc == NULL)
1002 if (irq < pdev->dev.irq_start || irq >= pdev->dev.irq_end)
1005 vec = pdev->dev.irq_start - irq;
1007 if (pdev->msi_desc[vec] != NULL)
1008 return (pdev->msi_desc[vec]);
1010 dinfo = device_get_ivars(dev->bsddev);
1011 msi = &dinfo->cfg.msi;
1013 desc = malloc(sizeof(*desc), M_DEVBUF, M_WAITOK | M_ZERO);
1015 desc->msi_attrib.is_64 =
1016 (msi->msi_ctrl & PCIM_MSICTRL_64BIT) ? true : false;
1017 desc->msg.data = msi->msi_data;
1019 pdev->msi_desc[vec] = desc;
1024 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
1026 struct linux_dma_obj {
1029 bus_dmamap_t dmamap;
1033 static uma_zone_t linux_dma_trie_zone;
1034 static uma_zone_t linux_dma_obj_zone;
1037 linux_dma_init(void *arg)
1040 linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
1041 pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
1043 linux_dma_obj_zone = uma_zcreate("linux_dma_object",
1044 sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
1046 lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);
1048 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
1051 linux_dma_uninit(void *arg)
1054 counter_u64_free(lkpi_pci_nseg1_fail);
1055 uma_zdestroy(linux_dma_obj_zone);
1056 uma_zdestroy(linux_dma_trie_zone);
1058 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
1061 linux_dma_trie_alloc(struct pctrie *ptree)
1064 return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
1068 linux_dma_trie_free(struct pctrie *ptree, void *node)
1071 uma_zfree(linux_dma_trie_zone, node);
1074 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
1075 linux_dma_trie_free);
1077 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1079 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,
1082 struct linux_dma_priv *priv;
1083 struct linux_dma_obj *obj;
1085 bus_dma_segment_t seg;
1087 priv = dev->dma_priv;
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.
1095 if (bus_dma_id_mapped(dmat, phys, len))
1098 obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
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);
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);
1123 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1124 obj->dma_addr = seg.ds_addr;
1126 error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
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);
1134 DMA_PRIV_UNLOCK(priv);
1135 return (obj->dma_addr);
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)
1147 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
1149 struct linux_dma_priv *priv;
1151 priv = dev->dma_priv;
1152 return (linux_dma_map_phys_common(dev, phys, len, priv->dmat));
1155 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1157 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1159 struct linux_dma_priv *priv;
1160 struct linux_dma_obj *obj;
1162 priv = dev->dma_priv;
1164 if (pctrie_is_empty(&priv->ptree))
1167 DMA_PRIV_LOCK(priv);
1168 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1170 DMA_PRIV_UNLOCK(priv);
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);
1178 uma_zfree(linux_dma_obj_zone, obj);
1182 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1188 linux_dma_alloc_coherent(struct device *dev, size_t size,
1189 dma_addr_t *dma_handle, gfp_t flag)
1191 struct linux_dma_priv *priv;
1196 if (dev == NULL || dev->dma_priv == NULL) {
1200 priv = dev->dma_priv;
1201 if (priv->dma_coherent_mask)
1202 high = priv->dma_coherent_mask;
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. */
1209 mem = (void *)kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,
1210 align, 0, VM_MEMATTR_DEFAULT);
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);
1224 struct lkpi_devres_dmam_coherent {
1231 lkpi_dmam_free_coherent(struct device *dev, void *p)
1233 struct lkpi_devres_dmam_coherent *dr;
1236 dma_free_coherent(dev, dr->size, dr->mem, *dr->handle);
1240 linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1243 struct lkpi_devres_dmam_coherent *dr;
1245 dr = lkpi_devres_alloc(lkpi_dmam_free_coherent,
1246 sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
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);
1259 lkpi_devres_add(dev, dr);
1264 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,
1265 bus_dmasync_op_t op)
1267 struct linux_dma_priv *priv;
1268 struct linux_dma_obj *obj;
1270 priv = dev->dma_priv;
1272 if (pctrie_is_empty(&priv->ptree))
1275 DMA_PRIV_LOCK(priv);
1276 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1278 DMA_PRIV_UNLOCK(priv);
1282 bus_dmamap_sync(obj->dmat, obj->dmamap, op);
1283 DMA_PRIV_UNLOCK(priv);
1287 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
1288 enum dma_data_direction direction, unsigned long attrs __unused)
1290 struct linux_dma_priv *priv;
1291 struct scatterlist *sg;
1293 bus_dma_segment_t seg;
1295 priv = dev->dma_priv;
1297 DMA_PRIV_LOCK(priv);
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);
1305 /* load all S/G list entries */
1306 for_each_sg(sgl, sg, nents, i) {
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);
1317 ("More than one segment (nseg=%d)", nseg + 1));
1319 sg_dma_address(sg) = seg.ds_addr;
1322 switch (direction) {
1323 case DMA_BIDIRECTIONAL:
1324 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1327 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1329 case DMA_FROM_DEVICE:
1330 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1336 DMA_PRIV_UNLOCK(priv);
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)
1346 struct linux_dma_priv *priv;
1348 priv = dev->dma_priv;
1350 DMA_PRIV_LOCK(priv);
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);
1358 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);
1360 case DMA_FROM_DEVICE:
1361 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1367 bus_dmamap_unload(priv->dmat, sgl->dma_map);
1368 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1369 DMA_PRIV_UNLOCK(priv);
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;
1381 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
1382 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
1385 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
1387 struct linux_dma_obj *obj = mem;
1388 struct dma_pool *pool = arg;
1390 bus_dma_segment_t seg;
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,
1397 DMA_POOL_UNLOCK(pool);
1401 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1402 obj->dma_addr = seg.ds_addr;
1408 dma_pool_obj_dtor(void *mem, int size, void *arg)
1410 struct linux_dma_obj *obj = mem;
1411 struct dma_pool *pool = arg;
1413 DMA_POOL_LOCK(pool);
1414 bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
1415 DMA_POOL_UNLOCK(pool);
1419 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
1422 struct dma_pool *pool = arg;
1423 struct linux_dma_obj *obj;
1426 for (i = 0; i < count; i++) {
1427 obj = uma_zalloc(linux_dma_obj_zone, flags);
1431 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
1432 BUS_DMA_NOWAIT, &obj->dmamap);
1434 uma_zfree(linux_dma_obj_zone, obj);
1445 dma_pool_obj_release(void *arg, void **store, int count)
1447 struct dma_pool *pool = arg;
1448 struct linux_dma_obj *obj;
1451 for (i = 0; i < count; i++) {
1453 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
1454 uma_zfree(linux_dma_obj_zone, obj);
1459 linux_dma_pool_create(char *name, struct device *dev, size_t size,
1460 size_t align, size_t boundary)
1462 struct linux_dma_priv *priv;
1463 struct dma_pool *pool;
1465 priv = dev->dma_priv;
1467 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1468 pool->pool_device = dev;
1469 pool->pool_entry_size = size;
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 */
1478 size, /* maxsegsz */
1480 NULL, NULL, /* lockfunc, lockfuncarg */
1481 &pool->pool_dmat)) {
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);
1490 mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
1491 pctrie_init(&pool->pool_ptree);
1497 linux_dma_pool_destroy(struct dma_pool *pool)
1500 uma_zdestroy(pool->pool_zone);
1501 bus_dma_tag_destroy(pool->pool_dmat);
1502 mtx_destroy(&pool->pool_lock);
1507 lkpi_dmam_pool_destroy(struct device *dev, void *p)
1509 struct dma_pool *pool;
1511 pool = *(struct dma_pool **)p;
1512 LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);
1513 linux_dma_pool_destroy(pool);
1517 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
1520 struct linux_dma_obj *obj;
1522 obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);
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);
1532 DMA_POOL_UNLOCK(pool);
1534 *handle = obj->dma_addr;
1535 return (obj->vaddr);
1539 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
1541 struct linux_dma_obj *obj;
1543 DMA_POOL_LOCK(pool);
1544 obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
1546 DMA_POOL_UNLOCK(pool);
1549 LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
1550 DMA_POOL_UNLOCK(pool);
1552 uma_zfree_arg(pool->pool_zone, obj, pool);
1556 linux_backlight_get_status(device_t dev, struct backlight_props *props)
1558 struct pci_dev *pdev;
1560 linux_set_current(curthread);
1561 pdev = device_get_softc(dev);
1563 props->brightness = pdev->dev.bd->props.brightness;
1564 props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
1571 linux_backlight_get_info(device_t dev, struct backlight_info *info)
1573 struct pci_dev *pdev;
1575 linux_set_current(curthread);
1576 pdev = device_get_softc(dev);
1578 info->type = BACKLIGHT_TYPE_PANEL;
1579 strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
1584 linux_backlight_update_status(device_t dev, struct backlight_props *props)
1586 struct pci_dev *pdev;
1588 linux_set_current(curthread);
1589 pdev = device_get_softc(dev);
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));
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)
1603 dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
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;
1611 dev->bd->name = strdup(name, M_DEVBUF);
1613 dev->backlight_dev = backlight_register(name, dev->bsddev);
1619 linux_backlight_device_unregister(struct backlight_device *bd)
1622 backlight_destroy(bd->dev->backlight_dev);
1623 free(bd->name, M_DEVBUF);