Path: blob/main/sys/compat/linuxkpi/common/src/linux_pci.c
39586 views
/*-1* Copyright (c) 2015-2016 Mellanox Technologies, Ltd.2* All rights reserved.3* Copyright (c) 2020-2025 The FreeBSD Foundation4*5* Portions of this software were developed by Björn Zeeb6* under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice unmodified, this list of conditions, and the following13* disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR19* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES20* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.21* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,22* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT23* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF27* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28*/2930#include <sys/param.h>31#include <sys/systm.h>32#include <sys/bus.h>33#include <sys/malloc.h>34#include <sys/kernel.h>35#include <sys/sysctl.h>36#include <sys/lock.h>37#include <sys/mutex.h>38#include <sys/fcntl.h>39#include <sys/file.h>40#include <sys/filio.h>41#include <sys/pciio.h>42#include <sys/pctrie.h>43#include <sys/rman.h>44#include <sys/rwlock.h>45#include <sys/stdarg.h>4647#include <vm/vm.h>48#include <vm/pmap.h>4950#include <machine/bus.h>51#include <machine/resource.h>5253#include <dev/pci/pcivar.h>54#include <dev/pci/pci_private.h>55#include <dev/pci/pci_iov.h>56#include <dev/backlight/backlight.h>5758#include <linux/kernel.h>59#include <linux/kobject.h>60#include <linux/device.h>61#include <linux/slab.h>62#include <linux/module.h>63#include <linux/cdev.h>64#include <linux/file.h>65#include <linux/sysfs.h>66#include <linux/mm.h>67#include <linux/io.h>68#include <linux/vmalloc.h>69#define WANT_NATIVE_PCI_GET_SLOT70#include <linux/pci.h>71#include <linux/compat.h>7273#include <linux/backlight.h>7475#include "backlight_if.h"76#include "pcib_if.h"7778/* Undef the linux function macro defined in linux/pci.h */79#undef pci_get_class8081extern int linuxkpi_debug;8283SYSCTL_DECL(_compat_linuxkpi);8485static counter_u64_t lkpi_pci_nseg1_fail;86SYSCTL_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");8889static device_probe_t linux_pci_probe;90static device_attach_t linux_pci_attach;91static device_detach_t linux_pci_detach;92static device_suspend_t linux_pci_suspend;93static device_resume_t linux_pci_resume;94static device_shutdown_t linux_pci_shutdown;95static pci_iov_init_t linux_pci_iov_init;96static pci_iov_uninit_t linux_pci_iov_uninit;97static pci_iov_add_vf_t linux_pci_iov_add_vf;98static int linux_backlight_get_status(device_t dev, struct backlight_props *props);99static int linux_backlight_update_status(device_t dev, struct backlight_props *props);100static int linux_backlight_get_info(device_t dev, struct backlight_info *info);101static void lkpi_pcim_iomap_table_release(struct device *, void *);102103static device_method_t pci_methods[] = {104DEVMETHOD(device_probe, linux_pci_probe),105DEVMETHOD(device_attach, linux_pci_attach),106DEVMETHOD(device_detach, linux_pci_detach),107DEVMETHOD(device_suspend, linux_pci_suspend),108DEVMETHOD(device_resume, linux_pci_resume),109DEVMETHOD(device_shutdown, linux_pci_shutdown),110DEVMETHOD(pci_iov_init, linux_pci_iov_init),111DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit),112DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf),113114/* Bus interface. */115DEVMETHOD(bus_add_child, bus_generic_add_child),116117/* backlight interface */118DEVMETHOD(backlight_update_status, linux_backlight_update_status),119DEVMETHOD(backlight_get_status, linux_backlight_get_status),120DEVMETHOD(backlight_get_info, linux_backlight_get_info),121DEVMETHOD_END122};123124const char *pci_power_names[] = {125"UNKNOWN", "D0", "D1", "D2", "D3hot", "D3cold"126};127128/* We need some meta-struct to keep track of these for devres. */129struct pci_devres {130bool enable_io;131/* PCIR_MAX_BAR_0 + 1 = 6 => BIT(0..5). */132uint8_t region_mask;133struct resource *region_table[PCIR_MAX_BAR_0 + 1]; /* Not needed. */134};135struct pcim_iomap_devres {136void *mmio_table[PCIR_MAX_BAR_0 + 1];137struct resource *res_table[PCIR_MAX_BAR_0 + 1];138};139140struct linux_dma_priv {141uint64_t dma_mask;142bus_dma_tag_t dmat;143uint64_t dma_coherent_mask;144bus_dma_tag_t dmat_coherent;145struct mtx lock;146struct pctrie ptree;147};148#define DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)149#define DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)150151static void152lkpi_set_pcim_iomap_devres(struct pcim_iomap_devres *dr, int bar,153void *res)154{155dr->mmio_table[bar] = (void *)rman_get_bushandle(res);156dr->res_table[bar] = res;157}158159static bool160lkpi_pci_bar_id_valid(int bar)161{162if (bar < 0 || bar > PCIR_MAX_BAR_0)163return (false);164165return (true);166}167168static int169linux_pdev_dma_uninit(struct pci_dev *pdev)170{171struct linux_dma_priv *priv;172173priv = pdev->dev.dma_priv;174if (priv->dmat)175bus_dma_tag_destroy(priv->dmat);176if (priv->dmat_coherent)177bus_dma_tag_destroy(priv->dmat_coherent);178mtx_destroy(&priv->lock);179pdev->dev.dma_priv = NULL;180free(priv, M_DEVBUF);181return (0);182}183184static int185linux_pdev_dma_init(struct pci_dev *pdev)186{187struct linux_dma_priv *priv;188int error;189190priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);191192mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);193pctrie_init(&priv->ptree);194195pdev->dev.dma_priv = priv;196197/* Create a default DMA tags. */198error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));199if (error != 0)200goto err;201/* Coherent is lower 32bit only by default in Linux. */202error = linux_dma_tag_init_coherent(&pdev->dev, DMA_BIT_MASK(32));203if (error != 0)204goto err;205206return (error);207208err:209linux_pdev_dma_uninit(pdev);210return (error);211}212213int214linux_dma_tag_init(struct device *dev, u64 dma_mask)215{216struct linux_dma_priv *priv;217int error;218219priv = dev->dma_priv;220221if (priv->dmat) {222if (priv->dma_mask == dma_mask)223return (0);224225bus_dma_tag_destroy(priv->dmat);226}227228priv->dma_mask = dma_mask;229230error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),2311, 0, /* alignment, boundary */232dma_mask, /* lowaddr */233BUS_SPACE_MAXADDR, /* highaddr */234NULL, NULL, /* filtfunc, filtfuncarg */235BUS_SPACE_MAXSIZE, /* maxsize */2361, /* nsegments */237BUS_SPACE_MAXSIZE, /* maxsegsz */2380, /* flags */239NULL, NULL, /* lockfunc, lockfuncarg */240&priv->dmat);241return (-error);242}243244int245linux_dma_tag_init_coherent(struct device *dev, u64 dma_mask)246{247struct linux_dma_priv *priv;248int error;249250priv = dev->dma_priv;251252if (priv->dmat_coherent) {253if (priv->dma_coherent_mask == dma_mask)254return (0);255256bus_dma_tag_destroy(priv->dmat_coherent);257}258259priv->dma_coherent_mask = dma_mask;260261error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),2621, 0, /* alignment, boundary */263dma_mask, /* lowaddr */264BUS_SPACE_MAXADDR, /* highaddr */265NULL, NULL, /* filtfunc, filtfuncarg */266BUS_SPACE_MAXSIZE, /* maxsize */2671, /* nsegments */268BUS_SPACE_MAXSIZE, /* maxsegsz */2690, /* flags */270NULL, NULL, /* lockfunc, lockfuncarg */271&priv->dmat_coherent);272return (-error);273}274275static struct pci_driver *276linux_pci_find(device_t dev, const struct pci_device_id **idp)277{278const struct pci_device_id *id;279struct pci_driver *pdrv;280uint16_t vendor;281uint16_t device;282uint16_t subvendor;283uint16_t subdevice;284285vendor = pci_get_vendor(dev);286device = pci_get_device(dev);287subvendor = pci_get_subvendor(dev);288subdevice = pci_get_subdevice(dev);289290spin_lock(&pci_lock);291list_for_each_entry(pdrv, &pci_drivers, node) {292for (id = pdrv->id_table; id->vendor != 0; id++) {293if (vendor == id->vendor &&294(PCI_ANY_ID == id->device || device == id->device) &&295(PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&296(PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {297*idp = id;298spin_unlock(&pci_lock);299return (pdrv);300}301}302}303spin_unlock(&pci_lock);304return (NULL);305}306307struct pci_dev *308lkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev)309{310struct pci_dev *pdev, *found;311312found = NULL;313spin_lock(&pci_lock);314list_for_each_entry(pdev, &pci_devices, links) {315/* Walk until we find odev. */316if (odev != NULL) {317if (pdev == odev)318odev = NULL;319continue;320}321322if ((pdev->vendor == vendor || vendor == PCI_ANY_ID) &&323(pdev->device == device || device == PCI_ANY_ID)) {324found = pdev;325break;326}327}328pci_dev_get(found);329spin_unlock(&pci_lock);330331return (found);332}333334static void335lkpi_pci_dev_release(struct device *dev)336{337338lkpi_devres_release_free_list(dev);339spin_lock_destroy(&dev->devres_lock);340}341342static int343lkpifill_pci_dev(device_t dev, struct pci_dev *pdev)344{345struct pci_devinfo *dinfo;346int error;347348error = kobject_init_and_add(&pdev->dev.kobj, &linux_dev_ktype,349&linux_root_device.kobj, device_get_nameunit(dev));350if (error != 0) {351printf("%s:%d: kobject_init_and_add returned %d\n",352__func__, __LINE__, error);353return (error);354}355356pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));357pdev->vendor = pci_get_vendor(dev);358pdev->device = pci_get_device(dev);359pdev->subsystem_vendor = pci_get_subvendor(dev);360pdev->subsystem_device = pci_get_subdevice(dev);361pdev->class = pci_get_class(dev);362pdev->revision = pci_get_revid(dev);363pdev->path_name = kasprintf(GFP_KERNEL, "%04d:%02d:%02d.%d",364pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),365pci_get_function(dev));366367pdev->bus = malloc(sizeof(*pdev->bus), M_DEVBUF, M_WAITOK | M_ZERO);368pdev->bus->number = pci_get_bus(dev);369pdev->bus->domain = pci_get_domain(dev);370371/* Check if we have reached the root to satisfy pci_is_root_bus() */372dinfo = device_get_ivars(dev);373if (dinfo->cfg.pcie.pcie_location != 0 &&374dinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT) {375pdev->bus->self = NULL;376} else {377/*378* This should be the upstream bridge; pci_upstream_bridge()379* handles that case on demand as otherwise we'll shadow the380* entire PCI hierarchy.381*/382pdev->bus->self = pdev;383}384pdev->dev.bsddev = dev;385pdev->dev.parent = &linux_root_device;386pdev->dev.release = lkpi_pci_dev_release;387INIT_LIST_HEAD(&pdev->dev.irqents);388389if (pci_msi_count(dev) > 0)390pdev->msi_desc = malloc(pci_msi_count(dev) *391sizeof(*pdev->msi_desc), M_DEVBUF, M_WAITOK | M_ZERO);392393spin_lock_init(&pdev->dev.devres_lock);394INIT_LIST_HEAD(&pdev->dev.devres_head);395396return (0);397}398399static void400lkpinew_pci_dev_release(struct device *dev)401{402struct pci_dev *pdev;403int i;404405pdev = to_pci_dev(dev);406if (pdev->root != NULL)407pci_dev_put(pdev->root);408if (pdev->bus->self != pdev && pdev->bus->self != NULL)409pci_dev_put(pdev->bus->self);410free(pdev->bus, M_DEVBUF);411if (pdev->msi_desc != NULL) {412for (i = pci_msi_count(pdev->dev.bsddev) - 1; i >= 0; i--)413free(pdev->msi_desc[i], M_DEVBUF);414free(pdev->msi_desc, M_DEVBUF);415}416kfree(pdev->path_name);417free(pdev, M_DEVBUF);418}419420struct pci_dev *421lkpinew_pci_dev(device_t dev)422{423struct pci_dev *pdev;424int error;425426pdev = malloc(sizeof(*pdev), M_DEVBUF, M_WAITOK|M_ZERO);427error = lkpifill_pci_dev(dev, pdev);428if (error != 0) {429free(pdev, M_DEVBUF);430return (NULL);431}432pdev->dev.release = lkpinew_pci_dev_release;433434return (pdev);435}436437struct pci_dev *438lkpi_pci_get_class(unsigned int class, struct pci_dev *from)439{440device_t dev;441device_t devfrom = NULL;442struct pci_dev *pdev;443444if (from != NULL)445devfrom = from->dev.bsddev;446447dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);448if (dev == NULL)449return (NULL);450451pdev = lkpinew_pci_dev(dev);452return (pdev);453}454455struct pci_dev *456lkpi_pci_get_base_class(unsigned int baseclass, struct pci_dev *from)457{458device_t dev;459device_t devfrom = NULL;460struct pci_dev *pdev;461462if (from != NULL)463devfrom = from->dev.bsddev;464465dev = pci_find_base_class_from(baseclass, devfrom);466if (dev == NULL)467return (NULL);468469pdev = lkpinew_pci_dev(dev);470return (pdev);471}472473struct pci_dev *474lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,475unsigned int devfn)476{477device_t dev;478struct pci_dev *pdev;479480dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));481if (dev == NULL)482return (NULL);483484pdev = lkpinew_pci_dev(dev);485return (pdev);486}487488struct pci_dev *489lkpi_pci_get_slot(struct pci_bus *pbus, unsigned int devfn)490{491device_t dev;492struct pci_dev *pdev;493494dev = pci_find_bsf(pbus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));495if (dev == NULL)496return (NULL);497498pdev = lkpinew_pci_dev(dev);499return (pdev);500}501502static int503linux_pci_probe(device_t dev)504{505const struct pci_device_id *id;506struct pci_driver *pdrv;507508if ((pdrv = linux_pci_find(dev, &id)) == NULL)509return (ENXIO);510if (device_get_driver(dev) != &pdrv->bsddriver)511return (ENXIO);512device_set_desc(dev, pdrv->name);513514/* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */515if (pdrv->bsd_probe_return == 0)516return (BUS_PROBE_DEFAULT);517else518return (pdrv->bsd_probe_return);519}520521static int522linux_pci_attach(device_t dev)523{524const struct pci_device_id *id;525struct pci_driver *pdrv;526struct pci_dev *pdev;527528pdrv = linux_pci_find(dev, &id);529pdev = device_get_softc(dev);530531MPASS(pdrv != NULL);532MPASS(pdev != NULL);533534return (linux_pci_attach_device(dev, pdrv, id, pdev));535}536537static struct resource_list_entry *538linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,539int type, int rid)540{541device_t dev;542struct resource *res;543544KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,545("trying to reserve non-BAR type %d", type));546547dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ?548device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;549res = pci_reserve_map(device_get_parent(dev), dev, type, &rid, 0, ~0,5501, 1, 0);551if (res == NULL)552return (NULL);553return (resource_list_find(rl, type, rid));554}555556static struct resource_list_entry *557linux_pci_get_rle(struct pci_dev *pdev, int type, int rid, bool reserve_bar)558{559struct pci_devinfo *dinfo;560struct resource_list *rl;561struct resource_list_entry *rle;562563dinfo = device_get_ivars(pdev->dev.bsddev);564rl = &dinfo->resources;565rle = resource_list_find(rl, type, rid);566/* Reserve resources for this BAR if needed. */567if (rle == NULL && reserve_bar)568rle = linux_pci_reserve_bar(pdev, rl, type, rid);569return (rle);570}571572int573linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,574const struct pci_device_id *id, struct pci_dev *pdev)575{576struct resource_list_entry *rle;577device_t parent;578struct pci_dev *pbus, *ppbus;579uintptr_t rid;580int error;581bool isdrm;582583linux_set_current(curthread);584585parent = device_get_parent(dev);586isdrm = pdrv != NULL && pdrv->isdrm;587588if (isdrm) {589struct pci_devinfo *dinfo;590591dinfo = device_get_ivars(parent);592device_set_ivars(dev, dinfo);593}594595error = lkpifill_pci_dev(dev, pdev);596if (error != 0)597return (error);598599if (isdrm)600PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);601else602PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);603pdev->devfn = rid;604pdev->pdrv = pdrv;605rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);606if (rle != NULL)607pdev->dev.irq = rle->start;608else609pdev->dev.irq = LINUX_IRQ_INVALID;610pdev->irq = pdev->dev.irq;611error = linux_pdev_dma_init(pdev);612if (error)613goto out_dma_init;614615TAILQ_INIT(&pdev->mmio);616spin_lock_init(&pdev->pcie_cap_lock);617618spin_lock(&pci_lock);619list_add(&pdev->links, &pci_devices);620spin_unlock(&pci_lock);621622/*623* Create the hierarchy now as we cannot on demand later.624* Take special care of DRM as there is a non-PCI device in the chain.625*/626pbus = pdev;627if (isdrm) {628pbus = lkpinew_pci_dev(parent);629if (pbus == NULL) {630error = ENXIO;631goto out_dma_init;632}633}634pcie_find_root_port(pbus);635if (isdrm)636pdev->root = pbus->root;637ppbus = pci_upstream_bridge(pbus);638while (ppbus != NULL && ppbus != pbus) {639pbus = ppbus;640ppbus = pci_upstream_bridge(pbus);641}642643if (pdrv != NULL) {644error = pdrv->probe(pdev, id);645if (error)646goto out_probe;647}648return (0);649650/* XXX the cleanup does not match the allocation up there. */651out_probe:652free(pdev->bus, M_DEVBUF);653spin_lock_destroy(&pdev->pcie_cap_lock);654linux_pdev_dma_uninit(pdev);655out_dma_init:656spin_lock(&pci_lock);657list_del(&pdev->links);658spin_unlock(&pci_lock);659put_device(&pdev->dev);660return (-error);661}662663static int664linux_pci_detach(device_t dev)665{666struct pci_dev *pdev;667668pdev = device_get_softc(dev);669670MPASS(pdev != NULL);671672device_set_desc(dev, NULL);673674return (linux_pci_detach_device(pdev));675}676677int678linux_pci_detach_device(struct pci_dev *pdev)679{680681linux_set_current(curthread);682683if (pdev->pdrv != NULL)684pdev->pdrv->remove(pdev);685686if (pdev->root != NULL)687pci_dev_put(pdev->root);688free(pdev->bus, M_DEVBUF);689linux_pdev_dma_uninit(pdev);690691spin_lock(&pci_lock);692list_del(&pdev->links);693spin_unlock(&pci_lock);694spin_lock_destroy(&pdev->pcie_cap_lock);695put_device(&pdev->dev);696697return (0);698}699700static int701lkpi_pci_disable_dev(struct device *dev)702{703704(void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);705(void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);706return (0);707}708709static struct pci_devres *710lkpi_pci_devres_get_alloc(struct pci_dev *pdev)711{712struct pci_devres *dr;713714dr = lkpi_devres_find(&pdev->dev, lkpi_pci_devres_release, NULL, NULL);715if (dr == NULL) {716dr = lkpi_devres_alloc(lkpi_pci_devres_release, sizeof(*dr),717GFP_KERNEL | __GFP_ZERO);718if (dr != NULL)719lkpi_devres_add(&pdev->dev, dr);720}721722return (dr);723}724725static struct pci_devres *726lkpi_pci_devres_find(struct pci_dev *pdev)727{728if (!pdev->managed)729return (NULL);730731return (lkpi_pci_devres_get_alloc(pdev));732}733734void735lkpi_pci_devres_release(struct device *dev, void *p)736{737struct pci_devres *dr;738struct pci_dev *pdev;739int bar;740741pdev = to_pci_dev(dev);742dr = p;743744if (pdev->msix_enabled)745lkpi_pci_disable_msix(pdev);746if (pdev->msi_enabled)747lkpi_pci_disable_msi(pdev);748749if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)750dr->enable_io = false;751752if (dr->region_mask == 0)753return;754for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {755756if ((dr->region_mask & (1 << bar)) == 0)757continue;758pci_release_region(pdev, bar);759}760}761762int763linuxkpi_pcim_enable_device(struct pci_dev *pdev)764{765struct pci_devres *dr;766int error;767768/* Here we cannot run through the pdev->managed check. */769dr = lkpi_pci_devres_get_alloc(pdev);770if (dr == NULL)771return (-ENOMEM);772773/* If resources were enabled before do not do it again. */774if (dr->enable_io)775return (0);776777error = pci_enable_device(pdev);778if (error == 0)779dr->enable_io = true;780781/* This device is not managed. */782pdev->managed = true;783784return (error);785}786787static struct pcim_iomap_devres *788lkpi_pcim_iomap_devres_find(struct pci_dev *pdev)789{790struct pcim_iomap_devres *dr;791792dr = lkpi_devres_find(&pdev->dev, lkpi_pcim_iomap_table_release,793NULL, NULL);794if (dr == NULL) {795dr = lkpi_devres_alloc(lkpi_pcim_iomap_table_release,796sizeof(*dr), GFP_KERNEL | __GFP_ZERO);797if (dr != NULL)798lkpi_devres_add(&pdev->dev, dr);799}800801if (dr == NULL)802device_printf(pdev->dev.bsddev, "%s: NULL\n", __func__);803804return (dr);805}806807void __iomem **808linuxkpi_pcim_iomap_table(struct pci_dev *pdev)809{810struct pcim_iomap_devres *dr;811812dr = lkpi_pcim_iomap_devres_find(pdev);813if (dr == NULL)814return (NULL);815816/*817* If the driver has manually set a flag to be able to request the818* resource to use bus_read/write_<n>, return the shadow table.819*/820if (pdev->want_iomap_res)821return ((void **)dr->res_table);822823/* This is the Linux default. */824return (dr->mmio_table);825}826827static struct resource *828_lkpi_pci_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen __unused)829{830struct pci_mmio_region *mmio, *p;831int type;832833if (!lkpi_pci_bar_id_valid(bar))834return (NULL);835836type = pci_resource_type(pdev, bar);837if (type < 0) {838device_printf(pdev->dev.bsddev, "%s: bar %d type %d\n",839__func__, bar, type);840return (NULL);841}842843/*844* Check for duplicate mappings.845* This can happen if a driver calls pci_request_region() first.846*/847TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {848if (mmio->type == type && mmio->rid == PCIR_BAR(bar)) {849return (mmio->res);850}851}852853mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);854mmio->rid = PCIR_BAR(bar);855mmio->type = type;856mmio->res = bus_alloc_resource_any(pdev->dev.bsddev, mmio->type,857&mmio->rid, RF_ACTIVE|RF_SHAREABLE);858if (mmio->res == NULL) {859device_printf(pdev->dev.bsddev, "%s: failed to alloc "860"bar %d type %d rid %d\n",861__func__, bar, type, PCIR_BAR(bar));862free(mmio, M_DEVBUF);863return (NULL);864}865TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);866867return (mmio->res);868}869870void *871linuxkpi_pci_iomap_range(struct pci_dev *pdev, int bar,872unsigned long off, unsigned long maxlen)873{874struct resource *res;875876if (!lkpi_pci_bar_id_valid(bar))877return (NULL);878879res = _lkpi_pci_iomap(pdev, bar, maxlen);880if (res == NULL)881return (NULL);882/* This is a FreeBSD extension so we can use bus_*(). */883if (pdev->want_iomap_res)884return (res);885MPASS(off < rman_get_size(res));886return ((void *)(rman_get_bushandle(res) + off));887}888889void *890linuxkpi_pci_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)891{892if (!lkpi_pci_bar_id_valid(bar))893return (NULL);894895return (linuxkpi_pci_iomap_range(pdev, bar, 0, maxlen));896}897898void *899linuxkpi_pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)900{901struct pcim_iomap_devres *dr;902void *res;903904if (!lkpi_pci_bar_id_valid(bar))905return (NULL);906907dr = lkpi_pcim_iomap_devres_find(pdev);908if (dr == NULL)909return (NULL);910911if (dr->res_table[bar] != NULL)912return (dr->res_table[bar]);913914res = linuxkpi_pci_iomap(pdev, bar, maxlen);915if (res == NULL) {916/*917* Do not free the devres in case there were918* other valid mappings before already.919*/920return (NULL);921}922lkpi_set_pcim_iomap_devres(dr, bar, res);923924return (res);925}926927void928linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res)929{930struct pci_mmio_region *mmio, *p;931bus_space_handle_t bh = (bus_space_handle_t)res;932933TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {934if (pdev->want_iomap_res) {935if (res != mmio->res)936continue;937} else {938if (bh < rman_get_bushandle(mmio->res) ||939bh >= rman_get_bushandle(mmio->res) +940rman_get_size(mmio->res))941continue;942}943bus_release_resource(pdev->dev.bsddev,944mmio->type, mmio->rid, mmio->res);945TAILQ_REMOVE(&pdev->mmio, mmio, next);946free(mmio, M_DEVBUF);947return;948}949}950951int952linuxkpi_pcim_iomap_regions(struct pci_dev *pdev, uint32_t mask, const char *name)953{954struct pcim_iomap_devres *dr;955void *res;956uint32_t mappings;957int bar;958959dr = lkpi_pcim_iomap_devres_find(pdev);960if (dr == NULL)961return (-ENOMEM);962963/* Now iomap all the requested (by "mask") ones. */964for (bar = mappings = 0; mappings != mask; bar++) {965if ((mask & (1 << bar)) == 0)966continue;967968/* Request double is not allowed. */969if (dr->mmio_table[bar] != NULL) {970device_printf(pdev->dev.bsddev, "%s: bar %d %p\n",971__func__, bar, dr->mmio_table[bar]);972goto err;973}974975res = _lkpi_pci_iomap(pdev, bar, 0);976if (res == NULL)977goto err;978lkpi_set_pcim_iomap_devres(dr, bar, res);979980mappings |= (1 << bar);981}982983return (0);984err:985for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {986if ((mappings & (1 << bar)) != 0) {987res = dr->mmio_table[bar];988if (res == NULL)989continue;990pci_iounmap(pdev, res);991}992}993994return (-EINVAL);995}996997static void998lkpi_pcim_iomap_table_release(struct device *dev, void *p)999{1000struct pcim_iomap_devres *dr;1001struct pci_dev *pdev;1002int bar;10031004dr = p;1005pdev = to_pci_dev(dev);1006for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {10071008if (dr->mmio_table[bar] == NULL)1009continue;10101011pci_iounmap(pdev, dr->mmio_table[bar]);1012}1013}10141015static int1016linux_pci_suspend(device_t dev)1017{1018const struct dev_pm_ops *pmops;1019struct pm_message pm = { };1020struct pci_dev *pdev;1021int error;10221023error = 0;1024linux_set_current(curthread);1025pdev = device_get_softc(dev);1026pmops = pdev->pdrv->driver.pm;10271028if (pdev->pdrv->suspend != NULL)1029error = -pdev->pdrv->suspend(pdev, pm);1030else if (pmops != NULL && pmops->suspend != NULL) {1031error = -pmops->suspend(&pdev->dev);1032if (error == 0 && pmops->suspend_late != NULL)1033error = -pmops->suspend_late(&pdev->dev);1034if (error == 0 && pmops->suspend_noirq != NULL)1035error = -pmops->suspend_noirq(&pdev->dev);1036}1037return (error);1038}10391040static int1041linux_pci_resume(device_t dev)1042{1043const struct dev_pm_ops *pmops;1044struct pci_dev *pdev;1045int error;10461047error = 0;1048linux_set_current(curthread);1049pdev = device_get_softc(dev);1050pmops = pdev->pdrv->driver.pm;10511052if (pdev->pdrv->resume != NULL)1053error = -pdev->pdrv->resume(pdev);1054else if (pmops != NULL && pmops->resume != NULL) {1055if (pmops->resume_early != NULL)1056error = -pmops->resume_early(&pdev->dev);1057if (error == 0 && pmops->resume != NULL)1058error = -pmops->resume(&pdev->dev);1059}1060return (error);1061}10621063static int1064linux_pci_shutdown(device_t dev)1065{1066struct pci_dev *pdev;10671068linux_set_current(curthread);1069pdev = device_get_softc(dev);1070if (pdev->pdrv->shutdown != NULL)1071pdev->pdrv->shutdown(pdev);1072return (0);1073}10741075static int1076linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)1077{1078struct pci_dev *pdev;1079int error;10801081linux_set_current(curthread);1082pdev = device_get_softc(dev);1083if (pdev->pdrv->bsd_iov_init != NULL)1084error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);1085else1086error = EINVAL;1087return (error);1088}10891090static void1091linux_pci_iov_uninit(device_t dev)1092{1093struct pci_dev *pdev;10941095linux_set_current(curthread);1096pdev = device_get_softc(dev);1097if (pdev->pdrv->bsd_iov_uninit != NULL)1098pdev->pdrv->bsd_iov_uninit(dev);1099}11001101static int1102linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)1103{1104struct pci_dev *pdev;1105int error;11061107linux_set_current(curthread);1108pdev = device_get_softc(dev);1109if (pdev->pdrv->bsd_iov_add_vf != NULL)1110error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);1111else1112error = EINVAL;1113return (error);1114}11151116static int1117_linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)1118{1119int error;11201121linux_set_current(curthread);1122spin_lock(&pci_lock);1123list_add(&pdrv->node, &pci_drivers);1124spin_unlock(&pci_lock);1125if (pdrv->bsddriver.name == NULL)1126pdrv->bsddriver.name = pdrv->name;1127pdrv->bsddriver.methods = pci_methods;1128pdrv->bsddriver.size = sizeof(struct pci_dev);11291130bus_topo_lock();1131error = devclass_add_driver(dc, &pdrv->bsddriver,1132BUS_PASS_DEFAULT, &pdrv->bsdclass);1133bus_topo_unlock();1134return (-error);1135}11361137int1138linux_pci_register_driver(struct pci_driver *pdrv)1139{1140devclass_t dc;11411142pdrv->isdrm = strcmp(pdrv->name, "drmn") == 0;1143dc = pdrv->isdrm ? devclass_create("vgapci") : devclass_find("pci");1144if (dc == NULL)1145return (-ENXIO);1146return (_linux_pci_register_driver(pdrv, dc));1147}11481149static struct resource_list_entry *1150lkpi_pci_get_bar(struct pci_dev *pdev, int bar, bool reserve)1151{1152int type;11531154type = pci_resource_type(pdev, bar);1155if (type < 0)1156return (NULL);1157bar = PCIR_BAR(bar);1158return (linux_pci_get_rle(pdev, type, bar, reserve));1159}11601161struct device *1162lkpi_pci_find_irq_dev(unsigned int irq)1163{1164struct pci_dev *pdev;1165struct device *found;11661167found = NULL;1168spin_lock(&pci_lock);1169list_for_each_entry(pdev, &pci_devices, links) {1170if (irq == pdev->dev.irq ||1171(irq >= pdev->dev.irq_start && irq < pdev->dev.irq_end)) {1172found = &pdev->dev;1173break;1174}1175}1176spin_unlock(&pci_lock);1177return (found);1178}11791180unsigned long1181pci_resource_start(struct pci_dev *pdev, int bar)1182{1183struct resource_list_entry *rle;1184rman_res_t newstart;1185device_t dev;1186int error;11871188if ((rle = lkpi_pci_get_bar(pdev, bar, true)) == NULL)1189return (0);1190dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ?1191device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;1192error = bus_translate_resource(dev, rle->type, rle->start, &newstart);1193if (error != 0) {1194device_printf(pdev->dev.bsddev,1195"translate of %#jx failed: %d\n",1196(uintmax_t)rle->start, error);1197return (0);1198}1199return (newstart);1200}12011202unsigned long1203pci_resource_len(struct pci_dev *pdev, int bar)1204{1205struct resource_list_entry *rle;12061207if ((rle = lkpi_pci_get_bar(pdev, bar, true)) == NULL)1208return (0);1209return (rle->count);1210}12111212static int1213lkpi_pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,1214bool managed)1215{1216struct resource *res;1217struct pci_devres *dr;1218struct pci_mmio_region *mmio;1219int rid;1220int type;12211222if (!lkpi_pci_bar_id_valid(bar))1223return (-EINVAL);12241225/*1226* If the bar is not valid, return success without adding the BAR;1227* otherwise linuxkpi_pcim_request_all_regions() will error.1228*/1229if (pci_resource_len(pdev, bar) == 0)1230return (0);1231/* Likewise if it is neither IO nor MEM, nothing to do for us. */1232type = pci_resource_type(pdev, bar);1233if (type < 0)1234return (0);12351236rid = PCIR_BAR(bar);1237res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,1238RF_ACTIVE|RF_SHAREABLE);1239if (res == NULL) {1240device_printf(pdev->dev.bsddev, "%s: failed to alloc "1241"bar %d type %d rid %d\n",1242__func__, bar, type, PCIR_BAR(bar));1243return (-ENODEV);1244}12451246/*1247* It seems there is an implicit devres tracking on these if the device1248* is managed (lkpi_pci_devres_find() case); otherwise the resources are1249* not automatically freed on FreeBSD/LinuxKPI though they should be/are1250* expected to be by Linux drivers.1251* Otherwise if we are called from a pcim-function with the managed1252* argument set, we need to track devres independent of pdev->managed.1253*/1254if (managed)1255dr = lkpi_pci_devres_get_alloc(pdev);1256else1257dr = lkpi_pci_devres_find(pdev);1258if (dr != NULL) {1259dr->region_mask |= (1 << bar);1260dr->region_table[bar] = res;1261}12621263/* Even if the device is not managed we need to track it for iomap. */1264mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);1265mmio->rid = PCIR_BAR(bar);1266mmio->type = type;1267mmio->res = res;1268TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);12691270return (0);1271}12721273int1274linuxkpi_pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)1275{1276return (lkpi_pci_request_region(pdev, bar, res_name, false));1277}12781279int1280linuxkpi_pci_request_regions(struct pci_dev *pdev, const char *res_name)1281{1282int error;1283int i;12841285for (i = 0; i <= PCIR_MAX_BAR_0; i++) {1286error = pci_request_region(pdev, i, res_name);1287if (error && error != -ENODEV) {1288pci_release_regions(pdev);1289return (error);1290}1291}1292return (0);1293}12941295int1296linuxkpi_pcim_request_all_regions(struct pci_dev *pdev, const char *res_name)1297{1298int bar, error;12991300for (bar = 0; bar <= PCIR_MAX_BAR_0; bar++) {1301error = lkpi_pci_request_region(pdev, bar, res_name, true);1302if (error != 0) {1303device_printf(pdev->dev.bsddev, "%s: bar %d res_name '%s': "1304"lkpi_pci_request_region returned %d\n", __func__,1305bar, res_name, error);1306pci_release_regions(pdev);1307return (error);1308}1309}1310return (0);1311}13121313void1314linuxkpi_pci_release_region(struct pci_dev *pdev, int bar)1315{1316struct resource_list_entry *rle;1317struct pci_devres *dr;1318struct pci_mmio_region *mmio, *p;13191320if ((rle = lkpi_pci_get_bar(pdev, bar, false)) == NULL)1321return;13221323/*1324* As we implicitly track the requests we also need to clear them on1325* release. Do clear before resource release.1326*/1327dr = lkpi_pci_devres_find(pdev);1328if (dr != NULL) {1329KASSERT(dr->region_table[bar] == rle->res, ("%s: pdev %p bar %d"1330" region_table res %p != rel->res %p\n", __func__, pdev,1331bar, dr->region_table[bar], rle->res));1332dr->region_table[bar] = NULL;1333dr->region_mask &= ~(1 << bar);1334}13351336TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {1337if (rle->res != (void *)rman_get_bushandle(mmio->res))1338continue;1339TAILQ_REMOVE(&pdev->mmio, mmio, next);1340free(mmio, M_DEVBUF);1341}13421343bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);1344}13451346void1347linuxkpi_pci_release_regions(struct pci_dev *pdev)1348{1349int i;13501351for (i = 0; i <= PCIR_MAX_BAR_0; i++)1352pci_release_region(pdev, i);1353}13541355int1356linux_pci_register_drm_driver(struct pci_driver *pdrv)1357{1358devclass_t dc;13591360dc = devclass_create("vgapci");1361if (dc == NULL)1362return (-ENXIO);1363pdrv->isdrm = true;1364pdrv->name = "drmn";1365return (_linux_pci_register_driver(pdrv, dc));1366}13671368void1369linux_pci_unregister_driver(struct pci_driver *pdrv)1370{1371devclass_t bus;13721373bus = devclass_find(pdrv->isdrm ? "vgapci" : "pci");13741375spin_lock(&pci_lock);1376list_del(&pdrv->node);1377spin_unlock(&pci_lock);1378bus_topo_lock();1379if (bus != NULL)1380devclass_delete_driver(bus, &pdrv->bsddriver);1381bus_topo_unlock();1382}13831384void1385linux_pci_unregister_drm_driver(struct pci_driver *pdrv)1386{1387devclass_t bus;13881389bus = devclass_find("vgapci");13901391spin_lock(&pci_lock);1392list_del(&pdrv->node);1393spin_unlock(&pci_lock);1394bus_topo_lock();1395if (bus != NULL)1396devclass_delete_driver(bus, &pdrv->bsddriver);1397bus_topo_unlock();1398}13991400int1401linuxkpi_pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries,1402int nreq)1403{1404struct resource_list_entry *rle;1405int error;1406int avail;1407int i;14081409avail = pci_msix_count(pdev->dev.bsddev);1410if (avail < nreq) {1411if (avail == 0)1412return -EINVAL;1413return avail;1414}1415avail = nreq;1416if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)1417return error;1418/*1419* Handle case where "pci_alloc_msix()" may allocate less1420* interrupts than available and return with no error:1421*/1422if (avail < nreq) {1423pci_release_msi(pdev->dev.bsddev);1424return avail;1425}1426rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1, false);1427pdev->dev.irq_start = rle->start;1428pdev->dev.irq_end = rle->start + avail;1429for (i = 0; i < nreq; i++)1430entries[i].vector = pdev->dev.irq_start + i;1431pdev->msix_enabled = true;1432return (0);1433}14341435int1436_lkpi_pci_enable_msi_range(struct pci_dev *pdev, int minvec, int maxvec)1437{1438struct resource_list_entry *rle;1439int error;1440int nvec;14411442if (maxvec < minvec)1443return (-EINVAL);14441445nvec = pci_msi_count(pdev->dev.bsddev);1446if (nvec < 1 || nvec < minvec)1447return (-ENOSPC);14481449nvec = min(nvec, maxvec);1450if ((error = -pci_alloc_msi(pdev->dev.bsddev, &nvec)) != 0)1451return error;14521453/* Native PCI might only ever ask for 32 vectors. */1454if (nvec < minvec) {1455pci_release_msi(pdev->dev.bsddev);1456return (-ENOSPC);1457}14581459rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1, false);1460pdev->dev.irq_start = rle->start;1461pdev->dev.irq_end = rle->start + nvec;1462pdev->irq = rle->start;1463pdev->msi_enabled = true;1464return (0);1465}14661467int1468pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,1469unsigned int flags)1470{1471int error;14721473if (flags & PCI_IRQ_MSIX) {1474struct msix_entry *entries;1475int i;14761477entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL);1478if (entries == NULL) {1479error = -ENOMEM;1480goto out;1481}1482for (i = 0; i < maxv; ++i)1483entries[i].entry = i;1484error = pci_enable_msix(pdev, entries, maxv);1485out:1486kfree(entries);1487if (error == 0 && pdev->msix_enabled)1488return (pdev->dev.irq_end - pdev->dev.irq_start);1489}1490if (flags & PCI_IRQ_MSI) {1491if (pci_msi_count(pdev->dev.bsddev) < minv)1492return (-ENOSPC);1493error = _lkpi_pci_enable_msi_range(pdev, minv, maxv);1494if (error == 0 && pdev->msi_enabled)1495return (pdev->dev.irq_end - pdev->dev.irq_start);1496}1497if (flags & PCI_IRQ_INTX) {1498if (pdev->irq)1499return (1);1500}15011502return (-EINVAL);1503}15041505struct msi_desc *1506lkpi_pci_msi_desc_alloc(int irq)1507{1508struct device *dev;1509struct pci_dev *pdev;1510struct msi_desc *desc;1511struct pci_devinfo *dinfo;1512struct pcicfg_msi *msi;1513int vec;15141515dev = lkpi_pci_find_irq_dev(irq);1516if (dev == NULL)1517return (NULL);15181519pdev = to_pci_dev(dev);15201521if (pdev->msi_desc == NULL)1522return (NULL);15231524if (irq < pdev->dev.irq_start || irq >= pdev->dev.irq_end)1525return (NULL);15261527vec = pdev->dev.irq_start - irq;15281529if (pdev->msi_desc[vec] != NULL)1530return (pdev->msi_desc[vec]);15311532dinfo = device_get_ivars(dev->bsddev);1533msi = &dinfo->cfg.msi;15341535desc = malloc(sizeof(*desc), M_DEVBUF, M_WAITOK | M_ZERO);15361537desc->pci.msi_attrib.is_64 =1538(msi->msi_ctrl & PCIM_MSICTRL_64BIT) ? true : false;1539desc->msg.data = msi->msi_data;15401541pdev->msi_desc[vec] = desc;15421543return (desc);1544}15451546bool1547pci_device_is_present(struct pci_dev *pdev)1548{1549device_t dev;15501551dev = pdev->dev.bsddev;15521553return (bus_child_present(dev));1554}15551556CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));15571558struct linux_dma_obj {1559void *vaddr;1560uint64_t dma_addr;1561bus_dmamap_t dmamap;1562bus_dma_tag_t dmat;1563};15641565static uma_zone_t linux_dma_trie_zone;1566static uma_zone_t linux_dma_obj_zone;15671568static void1569linux_dma_init(void *arg)1570{15711572linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",1573pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,1574UMA_ALIGN_PTR, 0);1575linux_dma_obj_zone = uma_zcreate("linux_dma_object",1576sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,1577UMA_ALIGN_PTR, 0);1578lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);1579}1580SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);15811582static void1583linux_dma_uninit(void *arg)1584{15851586counter_u64_free(lkpi_pci_nseg1_fail);1587uma_zdestroy(linux_dma_obj_zone);1588uma_zdestroy(linux_dma_trie_zone);1589}1590SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);15911592static void *1593linux_dma_trie_alloc(struct pctrie *ptree)1594{15951596return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));1597}15981599static void1600linux_dma_trie_free(struct pctrie *ptree, void *node)1601{16021603uma_zfree(linux_dma_trie_zone, node);1604}16051606PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,1607linux_dma_trie_free);16081609#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)1610static dma_addr_t1611linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,1612bus_dma_tag_t dmat)1613{1614struct linux_dma_priv *priv;1615struct linux_dma_obj *obj;1616int error, nseg;1617bus_dma_segment_t seg;16181619priv = dev->dma_priv;16201621/*1622* If the resultant mapping will be entirely 1:1 with the1623* physical address, short-circuit the remainder of the1624* bus_dma API. This avoids tracking collisions in the pctrie1625* with the additional benefit of reducing overhead.1626*/1627if (bus_dma_id_mapped(dmat, phys, len))1628return (phys);16291630obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);1631if (obj == NULL) {1632return (0);1633}1634obj->dmat = dmat;16351636DMA_PRIV_LOCK(priv);1637if (bus_dmamap_create(obj->dmat, 0, &obj->dmamap) != 0) {1638DMA_PRIV_UNLOCK(priv);1639uma_zfree(linux_dma_obj_zone, obj);1640return (0);1641}16421643nseg = -1;1644error = _bus_dmamap_load_phys(obj->dmat, obj->dmamap, phys, len,1645BUS_DMA_NOWAIT, &seg, &nseg);1646if (error != 0) {1647bus_dmamap_destroy(obj->dmat, obj->dmamap);1648DMA_PRIV_UNLOCK(priv);1649uma_zfree(linux_dma_obj_zone, obj);1650counter_u64_add(lkpi_pci_nseg1_fail, 1);1651if (linuxkpi_debug) {1652device_printf(dev->bsddev, "%s: _bus_dmamap_load_phys "1653"error %d, phys %#018jx len %zu\n", __func__,1654error, (uintmax_t)phys, len);1655dump_stack();1656}1657return (0);1658}16591660KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));1661obj->dma_addr = seg.ds_addr;16621663error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);1664if (error != 0) {1665bus_dmamap_unload(obj->dmat, obj->dmamap);1666bus_dmamap_destroy(obj->dmat, obj->dmamap);1667DMA_PRIV_UNLOCK(priv);1668uma_zfree(linux_dma_obj_zone, obj);1669return (0);1670}1671DMA_PRIV_UNLOCK(priv);1672return (obj->dma_addr);1673}1674#else1675static dma_addr_t1676linux_dma_map_phys_common(struct device *dev __unused, vm_paddr_t phys,1677size_t len __unused, bus_dma_tag_t dmat __unused)1678{1679return (phys);1680}1681#endif16821683dma_addr_t1684lkpi_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len,1685enum dma_data_direction direction, unsigned long attrs)1686{1687struct linux_dma_priv *priv;1688dma_addr_t dma;16891690priv = dev->dma_priv;1691dma = linux_dma_map_phys_common(dev, phys, len, priv->dmat);1692if (dma_mapping_error(dev, dma))1693return (dma);16941695if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)1696dma_sync_single_for_device(dev, dma, len, direction);16971698return (dma);1699}17001701/* For backward compat only so we can MFC this. Remove before 15. */1702dma_addr_t1703linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)1704{1705return (lkpi_dma_map_phys(dev, phys, len, DMA_NONE, 0));1706}17071708#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)1709void1710lkpi_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len,1711enum dma_data_direction direction, unsigned long attrs)1712{1713struct linux_dma_priv *priv;1714struct linux_dma_obj *obj;17151716priv = dev->dma_priv;17171718if (pctrie_is_empty(&priv->ptree))1719return;17201721DMA_PRIV_LOCK(priv);1722obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);1723if (obj == NULL) {1724DMA_PRIV_UNLOCK(priv);1725return;1726}1727LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);17281729if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)1730dma_sync_single_for_cpu(dev, dma_addr, len, direction);17311732bus_dmamap_unload(obj->dmat, obj->dmamap);1733bus_dmamap_destroy(obj->dmat, obj->dmamap);1734DMA_PRIV_UNLOCK(priv);17351736uma_zfree(linux_dma_obj_zone, obj);1737}1738#else1739void1740lkpi_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len,1741enum dma_data_direction direction, unsigned long attrs)1742{1743}1744#endif17451746/* For backward compat only so we can MFC this. Remove before 15. */1747void1748linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)1749{1750lkpi_dma_unmap(dev, dma_addr, len, DMA_NONE, 0);1751}17521753void *1754linux_dma_alloc_coherent(struct device *dev, size_t size,1755dma_addr_t *dma_handle, gfp_t flag)1756{1757struct linux_dma_priv *priv;1758vm_paddr_t high;1759size_t align;1760void *mem;17611762if (dev == NULL || dev->dma_priv == NULL) {1763*dma_handle = 0;1764return (NULL);1765}1766priv = dev->dma_priv;1767if (priv->dma_coherent_mask)1768high = priv->dma_coherent_mask;1769else1770/* Coherent is lower 32bit only by default in Linux. */1771high = BUS_SPACE_MAXADDR_32BIT;1772align = PAGE_SIZE << get_order(size);1773/* Always zero the allocation. */1774flag |= M_ZERO;1775mem = kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,1776align, 0, VM_MEMATTR_DEFAULT);1777if (mem != NULL) {1778*dma_handle = linux_dma_map_phys_common(dev, vtophys(mem), size,1779priv->dmat_coherent);1780if (*dma_handle == 0) {1781kmem_free(mem, size);1782mem = NULL;1783}1784} else {1785*dma_handle = 0;1786}1787return (mem);1788}17891790struct lkpi_devres_dmam_coherent {1791size_t size;1792dma_addr_t *handle;1793void *mem;1794};17951796static void1797lkpi_dmam_free_coherent(struct device *dev, void *p)1798{1799struct lkpi_devres_dmam_coherent *dr;18001801dr = p;1802dma_free_coherent(dev, dr->size, dr->mem, *dr->handle);1803}18041805void *1806linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,1807gfp_t flag)1808{1809struct lkpi_devres_dmam_coherent *dr;18101811dr = lkpi_devres_alloc(lkpi_dmam_free_coherent,1812sizeof(*dr), GFP_KERNEL | __GFP_ZERO);18131814if (dr == NULL)1815return (NULL);18161817dr->size = size;1818dr->mem = linux_dma_alloc_coherent(dev, size, dma_handle, flag);1819dr->handle = dma_handle;1820if (dr->mem == NULL) {1821lkpi_devres_free(dr);1822return (NULL);1823}18241825lkpi_devres_add(dev, dr);1826return (dr->mem);1827}18281829void1830linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,1831bus_dmasync_op_t op)1832{1833struct linux_dma_priv *priv;1834struct linux_dma_obj *obj;18351836priv = dev->dma_priv;18371838if (pctrie_is_empty(&priv->ptree))1839return;18401841DMA_PRIV_LOCK(priv);1842obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);1843if (obj == NULL) {1844DMA_PRIV_UNLOCK(priv);1845return;1846}18471848bus_dmamap_sync(obj->dmat, obj->dmamap, op);1849DMA_PRIV_UNLOCK(priv);1850}18511852int1853linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,1854enum dma_data_direction direction, unsigned long attrs)1855{1856struct linux_dma_priv *priv;1857struct scatterlist *sg;1858int i, nseg;1859bus_dma_segment_t seg;18601861priv = dev->dma_priv;18621863DMA_PRIV_LOCK(priv);18641865/* create common DMA map in the first S/G entry */1866if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {1867DMA_PRIV_UNLOCK(priv);1868return (0);1869}18701871/* load all S/G list entries */1872for_each_sg(sgl, sg, nents, i) {1873nseg = -1;1874if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,1875sg_phys(sg), sg->length, BUS_DMA_NOWAIT,1876&seg, &nseg) != 0) {1877bus_dmamap_unload(priv->dmat, sgl->dma_map);1878bus_dmamap_destroy(priv->dmat, sgl->dma_map);1879DMA_PRIV_UNLOCK(priv);1880return (0);1881}1882KASSERT(nseg == 0,1883("More than one segment (nseg=%d)", nseg + 1));18841885sg_dma_address(sg) = seg.ds_addr;1886}18871888if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) != 0)1889goto skip_sync;18901891switch (direction) {1892case DMA_BIDIRECTIONAL:1893bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);1894break;1895case DMA_TO_DEVICE:1896bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);1897break;1898case DMA_FROM_DEVICE:1899bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);1900break;1901default:1902break;1903}1904skip_sync:19051906DMA_PRIV_UNLOCK(priv);19071908return (nents);1909}19101911void1912linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,1913int nents __unused, enum dma_data_direction direction,1914unsigned long attrs)1915{1916struct linux_dma_priv *priv;19171918priv = dev->dma_priv;19191920DMA_PRIV_LOCK(priv);19211922if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) != 0)1923goto skip_sync;19241925switch (direction) {1926case DMA_BIDIRECTIONAL:1927bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);1928bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);1929break;1930case DMA_TO_DEVICE:1931bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);1932break;1933case DMA_FROM_DEVICE:1934bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);1935break;1936default:1937break;1938}1939skip_sync:19401941bus_dmamap_unload(priv->dmat, sgl->dma_map);1942bus_dmamap_destroy(priv->dmat, sgl->dma_map);1943DMA_PRIV_UNLOCK(priv);1944}19451946struct dma_pool {1947struct device *pool_device;1948uma_zone_t pool_zone;1949struct mtx pool_lock;1950bus_dma_tag_t pool_dmat;1951size_t pool_entry_size;1952struct pctrie pool_ptree;1953};19541955#define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)1956#define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)19571958static inline int1959dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)1960{1961struct linux_dma_obj *obj = mem;1962struct dma_pool *pool = arg;1963int error, nseg;1964bus_dma_segment_t seg;19651966nseg = -1;1967DMA_POOL_LOCK(pool);1968error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,1969vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,1970&seg, &nseg);1971DMA_POOL_UNLOCK(pool);1972if (error != 0) {1973return (error);1974}1975KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));1976obj->dma_addr = seg.ds_addr;19771978return (0);1979}19801981static void1982dma_pool_obj_dtor(void *mem, int size, void *arg)1983{1984struct linux_dma_obj *obj = mem;1985struct dma_pool *pool = arg;19861987DMA_POOL_LOCK(pool);1988bus_dmamap_unload(pool->pool_dmat, obj->dmamap);1989DMA_POOL_UNLOCK(pool);1990}19911992static int1993dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,1994int flags)1995{1996struct dma_pool *pool = arg;1997struct linux_dma_obj *obj;1998int error, i;19992000for (i = 0; i < count; i++) {2001obj = uma_zalloc(linux_dma_obj_zone, flags);2002if (obj == NULL)2003break;20042005error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,2006BUS_DMA_NOWAIT, &obj->dmamap);2007if (error!= 0) {2008uma_zfree(linux_dma_obj_zone, obj);2009break;2010}20112012store[i] = obj;2013}20142015return (i);2016}20172018static void2019dma_pool_obj_release(void *arg, void **store, int count)2020{2021struct dma_pool *pool = arg;2022struct linux_dma_obj *obj;2023int i;20242025for (i = 0; i < count; i++) {2026obj = store[i];2027bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);2028uma_zfree(linux_dma_obj_zone, obj);2029}2030}20312032struct dma_pool *2033linux_dma_pool_create(char *name, struct device *dev, size_t size,2034size_t align, size_t boundary)2035{2036struct linux_dma_priv *priv;2037struct dma_pool *pool;20382039priv = dev->dma_priv;20402041pool = kzalloc(sizeof(*pool), GFP_KERNEL);2042pool->pool_device = dev;2043pool->pool_entry_size = size;20442045if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),2046align, boundary, /* alignment, boundary */2047priv->dma_mask, /* lowaddr */2048BUS_SPACE_MAXADDR, /* highaddr */2049NULL, NULL, /* filtfunc, filtfuncarg */2050size, /* maxsize */20511, /* nsegments */2052size, /* maxsegsz */20530, /* flags */2054NULL, NULL, /* lockfunc, lockfuncarg */2055&pool->pool_dmat)) {2056kfree(pool);2057return (NULL);2058}20592060pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,2061dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,2062dma_pool_obj_release, pool, 0);20632064mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);2065pctrie_init(&pool->pool_ptree);20662067return (pool);2068}20692070void2071linux_dma_pool_destroy(struct dma_pool *pool)2072{20732074uma_zdestroy(pool->pool_zone);2075bus_dma_tag_destroy(pool->pool_dmat);2076mtx_destroy(&pool->pool_lock);2077kfree(pool);2078}20792080void2081lkpi_dmam_pool_destroy(struct device *dev, void *p)2082{2083struct dma_pool *pool;20842085pool = *(struct dma_pool **)p;2086LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);2087linux_dma_pool_destroy(pool);2088}20892090void *2091linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,2092dma_addr_t *handle)2093{2094struct linux_dma_obj *obj;20952096obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);2097if (obj == NULL)2098return (NULL);20992100DMA_POOL_LOCK(pool);2101if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {2102DMA_POOL_UNLOCK(pool);2103uma_zfree_arg(pool->pool_zone, obj, pool);2104return (NULL);2105}2106DMA_POOL_UNLOCK(pool);21072108*handle = obj->dma_addr;2109return (obj->vaddr);2110}21112112void2113linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)2114{2115struct linux_dma_obj *obj;21162117DMA_POOL_LOCK(pool);2118obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);2119if (obj == NULL) {2120DMA_POOL_UNLOCK(pool);2121return;2122}2123LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);2124DMA_POOL_UNLOCK(pool);21252126uma_zfree_arg(pool->pool_zone, obj, pool);2127}21282129static int2130linux_backlight_get_status(device_t dev, struct backlight_props *props)2131{2132struct pci_dev *pdev;21332134linux_set_current(curthread);2135pdev = device_get_softc(dev);21362137props->brightness = pdev->dev.bd->props.brightness;2138props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;2139props->nlevels = 0;21402141return (0);2142}21432144static int2145linux_backlight_get_info(device_t dev, struct backlight_info *info)2146{2147struct pci_dev *pdev;21482149linux_set_current(curthread);2150pdev = device_get_softc(dev);21512152info->type = BACKLIGHT_TYPE_PANEL;2153strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);2154return (0);2155}21562157static int2158linux_backlight_update_status(device_t dev, struct backlight_props *props)2159{2160struct pci_dev *pdev;21612162linux_set_current(curthread);2163pdev = device_get_softc(dev);21642165pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness *2166props->brightness / 100;2167pdev->dev.bd->props.power = props->brightness == 0 ?21684/* FB_BLANK_POWERDOWN */ : 0/* FB_BLANK_UNBLANK */;2169return (pdev->dev.bd->ops->update_status(pdev->dev.bd));2170}21712172struct backlight_device *2173linux_backlight_device_register(const char *name, struct device *dev,2174void *data, const struct backlight_ops *ops, struct backlight_properties *props)2175{21762177dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);2178dev->bd->ops = ops;2179dev->bd->props.type = props->type;2180dev->bd->props.max_brightness = props->max_brightness;2181dev->bd->props.brightness = props->brightness;2182dev->bd->props.power = props->power;2183dev->bd->data = data;2184dev->bd->dev = dev;2185dev->bd->name = strdup(name, M_DEVBUF);21862187dev->backlight_dev = backlight_register(name, dev->bsddev);21882189return (dev->bd);2190}21912192void2193linux_backlight_device_unregister(struct backlight_device *bd)2194{21952196backlight_destroy(bd->dev->backlight_dev);2197free(bd->name, M_DEVBUF);2198free(bd, M_DEVBUF);2199}220022012202