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);
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);
334 lkpinew_pci_dev_release(struct device *dev)
336 struct pci_dev *pdev;
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);
348 lkpinew_pci_dev(device_t dev)
350 struct pci_dev *pdev;
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;
360 lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
363 device_t devfrom = NULL;
364 struct pci_dev *pdev;
367 devfrom = from->dev.bsddev;
369 dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
373 pdev = lkpinew_pci_dev(dev);
378 lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,
382 struct pci_dev *pdev;
384 dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
388 pdev = lkpinew_pci_dev(dev);
393 linux_pci_probe(device_t dev)
395 const struct pci_device_id *id;
396 struct pci_driver *pdrv;
398 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
400 if (device_get_driver(dev) != &pdrv->bsddriver)
402 device_set_desc(dev, pdrv->name);
404 /* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */
405 if (pdrv->bsd_probe_return == 0)
406 return (BUS_PROBE_DEFAULT);
408 return (pdrv->bsd_probe_return);
412 linux_pci_attach(device_t dev)
414 const struct pci_device_id *id;
415 struct pci_driver *pdrv;
416 struct pci_dev *pdev;
418 pdrv = linux_pci_find(dev, &id);
419 pdev = device_get_softc(dev);
424 return (linux_pci_attach_device(dev, pdrv, id, pdev));
428 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
429 const struct pci_device_id *id, struct pci_dev *pdev)
431 struct resource_list_entry *rle;
437 linux_set_current(curthread);
439 parent = device_get_parent(dev);
440 isdrm = pdrv != NULL && linux_is_drm(pdrv);
443 struct pci_devinfo *dinfo;
445 dinfo = device_get_ivars(parent);
446 device_set_ivars(dev, dinfo);
449 lkpifill_pci_dev(dev, pdev);
451 PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);
453 PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);
456 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);
458 pdev->dev.irq = rle->start;
460 pdev->dev.irq = LINUX_IRQ_INVALID;
461 pdev->irq = pdev->dev.irq;
462 error = linux_pdev_dma_init(pdev);
466 TAILQ_INIT(&pdev->mmio);
468 spin_lock(&pci_lock);
469 list_add(&pdev->links, &pci_devices);
470 spin_unlock(&pci_lock);
473 error = pdrv->probe(pdev, id);
480 free(pdev->bus, M_DEVBUF);
481 linux_pdev_dma_uninit(pdev);
483 spin_lock(&pci_lock);
484 list_del(&pdev->links);
485 spin_unlock(&pci_lock);
486 put_device(&pdev->dev);
491 linux_pci_detach(device_t dev)
493 struct pci_dev *pdev;
495 pdev = device_get_softc(dev);
499 device_set_desc(dev, NULL);
501 return (linux_pci_detach_device(pdev));
505 linux_pci_detach_device(struct pci_dev *pdev)
508 linux_set_current(curthread);
510 if (pdev->pdrv != NULL)
511 pdev->pdrv->remove(pdev);
513 if (pdev->root != NULL)
514 pci_dev_put(pdev->root);
515 free(pdev->bus, M_DEVBUF);
516 linux_pdev_dma_uninit(pdev);
518 spin_lock(&pci_lock);
519 list_del(&pdev->links);
520 spin_unlock(&pci_lock);
521 put_device(&pdev->dev);
527 lkpi_pci_disable_dev(struct device *dev)
530 (void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);
531 (void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);
536 lkpi_pci_devres_get_alloc(struct pci_dev *pdev)
538 struct pci_devres *dr;
540 dr = lkpi_devres_find(&pdev->dev, lkpi_pci_devres_release, NULL, NULL);
542 dr = lkpi_devres_alloc(lkpi_pci_devres_release, sizeof(*dr),
543 GFP_KERNEL | __GFP_ZERO);
545 lkpi_devres_add(&pdev->dev, dr);
552 lkpi_pci_devres_release(struct device *dev, void *p)
554 struct pci_devres *dr;
555 struct pci_dev *pdev;
558 pdev = to_pci_dev(dev);
561 if (pdev->msix_enabled)
562 lkpi_pci_disable_msix(pdev);
563 if (pdev->msi_enabled)
564 lkpi_pci_disable_msi(pdev);
566 if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)
567 dr->enable_io = false;
569 if (dr->region_mask == 0)
571 for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
573 if ((dr->region_mask & (1 << bar)) == 0)
575 pci_release_region(pdev, bar);
579 struct pcim_iomap_devres *
580 lkpi_pcim_iomap_devres_find(struct pci_dev *pdev)
582 struct pcim_iomap_devres *dr;
584 dr = lkpi_devres_find(&pdev->dev, lkpi_pcim_iomap_table_release,
587 dr = lkpi_devres_alloc(lkpi_pcim_iomap_table_release,
588 sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
590 lkpi_devres_add(&pdev->dev, dr);
594 device_printf(pdev->dev.bsddev, "%s: NULL\n", __func__);
600 lkpi_pcim_iomap_table_release(struct device *dev, void *p)
602 struct pcim_iomap_devres *dr;
603 struct pci_dev *pdev;
607 pdev = to_pci_dev(dev);
608 for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
610 if (dr->mmio_table[bar] == NULL)
613 pci_iounmap(pdev, dr->mmio_table[bar]);
618 linux_pci_suspend(device_t dev)
620 const struct dev_pm_ops *pmops;
621 struct pm_message pm = { };
622 struct pci_dev *pdev;
626 linux_set_current(curthread);
627 pdev = device_get_softc(dev);
628 pmops = pdev->pdrv->driver.pm;
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);
641 linux_pci_resume(device_t dev)
643 const struct dev_pm_ops *pmops;
644 struct pci_dev *pdev;
648 linux_set_current(curthread);
649 pdev = device_get_softc(dev);
650 pmops = pdev->pdrv->driver.pm;
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);
664 linux_pci_shutdown(device_t dev)
666 struct pci_dev *pdev;
668 linux_set_current(curthread);
669 pdev = device_get_softc(dev);
670 if (pdev->pdrv->shutdown != NULL)
671 pdev->pdrv->shutdown(pdev);
676 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
678 struct pci_dev *pdev;
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);
691 linux_pci_iov_uninit(device_t dev)
693 struct pci_dev *pdev;
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);
702 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
704 struct pci_dev *pdev;
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);
717 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
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);
731 error = devclass_add_driver(dc, &pdrv->bsddriver,
732 BUS_PASS_DEFAULT, &pdrv->bsdclass);
738 linux_pci_register_driver(struct pci_driver *pdrv)
742 dc = devclass_find("pci");
745 return (_linux_pci_register_driver(pdrv, dc));
748 struct resource_list_entry *
749 linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,
753 struct resource *res;
755 KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,
756 ("trying to reserve non-BAR type %d", type));
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,
764 return (resource_list_find(rl, type, rid));
768 pci_resource_start(struct pci_dev *pdev, int bar)
770 struct resource_list_entry *rle;
774 if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
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);
787 pci_resource_len(struct pci_dev *pdev, int bar)
789 struct resource_list_entry *rle;
791 if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
797 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
799 struct resource *res;
800 struct pci_devres *dr;
801 struct pci_mmio_region *mmio;
805 type = pci_resource_type(pdev, bar);
809 res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
810 RF_ACTIVE|RF_SHAREABLE);
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));
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
824 dr = lkpi_pci_devres_find(pdev);
826 dr->region_mask |= (1 << bar);
827 dr->region_table[bar] = res;
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);
835 TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
841 _lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size __unused)
843 struct pci_mmio_region *mmio, *p;
846 type = pci_resource_type(pdev, bar);
848 device_printf(pdev->dev.bsddev, "%s: bar %d type %d\n",
849 __func__, bar, type);
854 * Check for duplicate mappings.
855 * This can happen if a driver calls pci_request_region() first.
857 TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
858 if (mmio->type == type && mmio->rid == PCIR_BAR(bar)) {
863 mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
864 mmio->rid = PCIR_BAR(bar);
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);
875 TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
881 linux_pci_register_drm_driver(struct pci_driver *pdrv)
885 dc = devclass_create("vgapci");
889 return (_linux_pci_register_driver(pdrv, dc));
893 linux_pci_unregister_driver(struct pci_driver *pdrv)
897 bus = devclass_find("pci");
899 spin_lock(&pci_lock);
900 list_del(&pdrv->node);
901 spin_unlock(&pci_lock);
904 devclass_delete_driver(bus, &pdrv->bsddriver);
909 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
913 bus = devclass_find("vgapci");
915 spin_lock(&pci_lock);
916 list_del(&pdrv->node);
917 spin_unlock(&pci_lock);
920 devclass_delete_driver(bus, &pdrv->bsddriver);
925 pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,
930 if (flags & PCI_IRQ_MSIX) {
931 struct msix_entry *entries;
934 entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL);
935 if (entries == NULL) {
939 for (i = 0; i < maxv; ++i)
940 entries[i].entry = i;
941 error = pci_enable_msix(pdev, entries, maxv);
944 if (error == 0 && pdev->msix_enabled)
945 return (pdev->dev.irq_end - pdev->dev.irq_start);
947 if (flags & PCI_IRQ_MSI) {
948 if (pci_msi_count(pdev->dev.bsddev) < minv)
950 /* We only support 1 vector in pci_enable_msi() */
953 error = pci_enable_msi(pdev);
954 if (error == 0 && pdev->msi_enabled)
955 return (pdev->dev.irq_end - pdev->dev.irq_start);
957 if (flags & PCI_IRQ_LEGACY) {
966 pci_device_is_present(struct pci_dev *pdev)
970 dev = pdev->dev.bsddev;
972 return (bus_child_present(dev));
975 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
977 struct linux_dma_obj {
984 static uma_zone_t linux_dma_trie_zone;
985 static uma_zone_t linux_dma_obj_zone;
988 linux_dma_init(void *arg)
991 linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
992 pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
994 linux_dma_obj_zone = uma_zcreate("linux_dma_object",
995 sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
997 lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);
999 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
1002 linux_dma_uninit(void *arg)
1005 counter_u64_free(lkpi_pci_nseg1_fail);
1006 uma_zdestroy(linux_dma_obj_zone);
1007 uma_zdestroy(linux_dma_trie_zone);
1009 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
1012 linux_dma_trie_alloc(struct pctrie *ptree)
1015 return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
1019 linux_dma_trie_free(struct pctrie *ptree, void *node)
1022 uma_zfree(linux_dma_trie_zone, node);
1025 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
1026 linux_dma_trie_free);
1028 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1030 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,
1033 struct linux_dma_priv *priv;
1034 struct linux_dma_obj *obj;
1036 bus_dma_segment_t seg;
1038 priv = dev->dma_priv;
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.
1046 if (bus_dma_id_mapped(dmat, phys, len))
1049 obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
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);
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);
1074 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1075 obj->dma_addr = seg.ds_addr;
1077 error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
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);
1085 DMA_PRIV_UNLOCK(priv);
1086 return (obj->dma_addr);
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)
1098 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
1100 struct linux_dma_priv *priv;
1102 priv = dev->dma_priv;
1103 return (linux_dma_map_phys_common(dev, phys, len, priv->dmat));
1106 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1108 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1110 struct linux_dma_priv *priv;
1111 struct linux_dma_obj *obj;
1113 priv = dev->dma_priv;
1115 if (pctrie_is_empty(&priv->ptree))
1118 DMA_PRIV_LOCK(priv);
1119 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1121 DMA_PRIV_UNLOCK(priv);
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);
1129 uma_zfree(linux_dma_obj_zone, obj);
1133 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1139 linux_dma_alloc_coherent(struct device *dev, size_t size,
1140 dma_addr_t *dma_handle, gfp_t flag)
1142 struct linux_dma_priv *priv;
1147 if (dev == NULL || dev->dma_priv == NULL) {
1151 priv = dev->dma_priv;
1152 if (priv->dma_coherent_mask)
1153 high = priv->dma_coherent_mask;
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. */
1160 mem = (void *)kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,
1161 align, 0, VM_MEMATTR_DEFAULT);
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);
1175 struct lkpi_devres_dmam_coherent {
1182 lkpi_dmam_free_coherent(struct device *dev, void *p)
1184 struct lkpi_devres_dmam_coherent *dr;
1187 dma_free_coherent(dev, dr->size, dr->mem, *dr->handle);
1191 linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1194 struct lkpi_devres_dmam_coherent *dr;
1196 dr = lkpi_devres_alloc(lkpi_dmam_free_coherent,
1197 sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
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);
1210 lkpi_devres_add(dev, dr);
1215 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,
1216 bus_dmasync_op_t op)
1218 struct linux_dma_priv *priv;
1219 struct linux_dma_obj *obj;
1221 priv = dev->dma_priv;
1223 if (pctrie_is_empty(&priv->ptree))
1226 DMA_PRIV_LOCK(priv);
1227 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1229 DMA_PRIV_UNLOCK(priv);
1233 bus_dmamap_sync(obj->dmat, obj->dmamap, op);
1234 DMA_PRIV_UNLOCK(priv);
1238 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
1239 enum dma_data_direction direction, unsigned long attrs __unused)
1241 struct linux_dma_priv *priv;
1242 struct scatterlist *sg;
1244 bus_dma_segment_t seg;
1246 priv = dev->dma_priv;
1248 DMA_PRIV_LOCK(priv);
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);
1256 /* load all S/G list entries */
1257 for_each_sg(sgl, sg, nents, i) {
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);
1268 ("More than one segment (nseg=%d)", nseg + 1));
1270 sg_dma_address(sg) = seg.ds_addr;
1273 switch (direction) {
1274 case DMA_BIDIRECTIONAL:
1275 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1278 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1280 case DMA_FROM_DEVICE:
1281 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1287 DMA_PRIV_UNLOCK(priv);
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)
1297 struct linux_dma_priv *priv;
1299 priv = dev->dma_priv;
1301 DMA_PRIV_LOCK(priv);
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);
1309 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);
1311 case DMA_FROM_DEVICE:
1312 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1318 bus_dmamap_unload(priv->dmat, sgl->dma_map);
1319 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1320 DMA_PRIV_UNLOCK(priv);
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;
1332 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
1333 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
1336 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
1338 struct linux_dma_obj *obj = mem;
1339 struct dma_pool *pool = arg;
1341 bus_dma_segment_t seg;
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,
1348 DMA_POOL_UNLOCK(pool);
1352 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1353 obj->dma_addr = seg.ds_addr;
1359 dma_pool_obj_dtor(void *mem, int size, void *arg)
1361 struct linux_dma_obj *obj = mem;
1362 struct dma_pool *pool = arg;
1364 DMA_POOL_LOCK(pool);
1365 bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
1366 DMA_POOL_UNLOCK(pool);
1370 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
1373 struct dma_pool *pool = arg;
1374 struct linux_dma_obj *obj;
1377 for (i = 0; i < count; i++) {
1378 obj = uma_zalloc(linux_dma_obj_zone, flags);
1382 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
1383 BUS_DMA_NOWAIT, &obj->dmamap);
1385 uma_zfree(linux_dma_obj_zone, obj);
1396 dma_pool_obj_release(void *arg, void **store, int count)
1398 struct dma_pool *pool = arg;
1399 struct linux_dma_obj *obj;
1402 for (i = 0; i < count; i++) {
1404 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
1405 uma_zfree(linux_dma_obj_zone, obj);
1410 linux_dma_pool_create(char *name, struct device *dev, size_t size,
1411 size_t align, size_t boundary)
1413 struct linux_dma_priv *priv;
1414 struct dma_pool *pool;
1416 priv = dev->dma_priv;
1418 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1419 pool->pool_device = dev;
1420 pool->pool_entry_size = size;
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 */
1429 size, /* maxsegsz */
1431 NULL, NULL, /* lockfunc, lockfuncarg */
1432 &pool->pool_dmat)) {
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);
1441 mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
1442 pctrie_init(&pool->pool_ptree);
1448 linux_dma_pool_destroy(struct dma_pool *pool)
1451 uma_zdestroy(pool->pool_zone);
1452 bus_dma_tag_destroy(pool->pool_dmat);
1453 mtx_destroy(&pool->pool_lock);
1458 lkpi_dmam_pool_destroy(struct device *dev, void *p)
1460 struct dma_pool *pool;
1462 pool = *(struct dma_pool **)p;
1463 LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);
1464 linux_dma_pool_destroy(pool);
1468 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
1471 struct linux_dma_obj *obj;
1473 obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);
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);
1483 DMA_POOL_UNLOCK(pool);
1485 *handle = obj->dma_addr;
1486 return (obj->vaddr);
1490 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
1492 struct linux_dma_obj *obj;
1494 DMA_POOL_LOCK(pool);
1495 obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
1497 DMA_POOL_UNLOCK(pool);
1500 LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
1501 DMA_POOL_UNLOCK(pool);
1503 uma_zfree_arg(pool->pool_zone, obj, pool);
1507 linux_backlight_get_status(device_t dev, struct backlight_props *props)
1509 struct pci_dev *pdev;
1511 linux_set_current(curthread);
1512 pdev = device_get_softc(dev);
1514 props->brightness = pdev->dev.bd->props.brightness;
1515 props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
1522 linux_backlight_get_info(device_t dev, struct backlight_info *info)
1524 struct pci_dev *pdev;
1526 linux_set_current(curthread);
1527 pdev = device_get_softc(dev);
1529 info->type = BACKLIGHT_TYPE_PANEL;
1530 strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
1535 linux_backlight_update_status(device_t dev, struct backlight_props *props)
1537 struct pci_dev *pdev;
1539 linux_set_current(curthread);
1540 pdev = device_get_softc(dev);
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));
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)
1554 dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
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;
1562 dev->bd->name = strdup(name, M_DEVBUF);
1564 dev->backlight_dev = backlight_register(name, dev->bsddev);
1570 linux_backlight_device_unregister(struct backlight_device *bd)
1573 backlight_destroy(bd->dev->backlight_dev);
1574 free(bd->name, M_DEVBUF);