2 * Copyright (c) 2015-2016 Mellanox Technologies, Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
37 #include <sys/mutex.h>
38 #include <sys/fcntl.h>
40 #include <sys/filio.h>
41 #include <sys/pciio.h>
42 #include <sys/pctrie.h>
43 #include <sys/rwlock.h>
48 #include <machine/stdarg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 #include <dev/pci/pci_iov.h>
54 #include <linux/kobject.h>
55 #include <linux/device.h>
56 #include <linux/slab.h>
57 #include <linux/module.h>
58 #include <linux/cdev.h>
59 #include <linux/file.h>
60 #include <linux/sysfs.h>
63 #include <linux/vmalloc.h>
64 #include <linux/pci.h>
65 #include <linux/compat.h>
67 static device_probe_t linux_pci_probe;
68 static device_attach_t linux_pci_attach;
69 static device_detach_t linux_pci_detach;
70 static device_suspend_t linux_pci_suspend;
71 static device_resume_t linux_pci_resume;
72 static device_shutdown_t linux_pci_shutdown;
73 static pci_iov_init_t linux_pci_iov_init;
74 static pci_iov_uninit_t linux_pci_iov_uninit;
75 static pci_iov_add_vf_t linux_pci_iov_add_vf;
77 static device_method_t pci_methods[] = {
78 DEVMETHOD(device_probe, linux_pci_probe),
79 DEVMETHOD(device_attach, linux_pci_attach),
80 DEVMETHOD(device_detach, linux_pci_detach),
81 DEVMETHOD(device_suspend, linux_pci_suspend),
82 DEVMETHOD(device_resume, linux_pci_resume),
83 DEVMETHOD(device_shutdown, linux_pci_shutdown),
84 DEVMETHOD(pci_iov_init, linux_pci_iov_init),
85 DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit),
86 DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf),
90 struct linux_dma_priv {
96 #define DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
97 #define DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
100 linux_pdev_dma_init(struct pci_dev *pdev)
102 struct linux_dma_priv *priv;
105 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
106 pdev->dev.dma_priv = priv;
108 mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
110 pctrie_init(&priv->ptree);
112 /* create a default DMA tag */
113 error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
115 mtx_destroy(&priv->lock);
116 free(priv, M_DEVBUF);
117 pdev->dev.dma_priv = NULL;
123 linux_pdev_dma_uninit(struct pci_dev *pdev)
125 struct linux_dma_priv *priv;
127 priv = pdev->dev.dma_priv;
129 bus_dma_tag_destroy(priv->dmat);
130 mtx_destroy(&priv->lock);
131 free(priv, M_DEVBUF);
132 pdev->dev.dma_priv = NULL;
137 linux_dma_tag_init(struct device *dev, u64 dma_mask)
139 struct linux_dma_priv *priv;
142 priv = dev->dma_priv;
145 if (priv->dma_mask == dma_mask)
148 bus_dma_tag_destroy(priv->dmat);
151 priv->dma_mask = dma_mask;
153 error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
154 1, 0, /* alignment, boundary */
155 dma_mask, /* lowaddr */
156 BUS_SPACE_MAXADDR, /* highaddr */
157 NULL, NULL, /* filtfunc, filtfuncarg */
158 BUS_SPACE_MAXSIZE, /* maxsize */
160 BUS_SPACE_MAXSIZE, /* maxsegsz */
162 NULL, NULL, /* lockfunc, lockfuncarg */
167 static struct pci_driver *
168 linux_pci_find(device_t dev, const struct pci_device_id **idp)
170 const struct pci_device_id *id;
171 struct pci_driver *pdrv;
177 vendor = pci_get_vendor(dev);
178 device = pci_get_device(dev);
179 subvendor = pci_get_subvendor(dev);
180 subdevice = pci_get_subdevice(dev);
182 spin_lock(&pci_lock);
183 list_for_each_entry(pdrv, &pci_drivers, links) {
184 for (id = pdrv->id_table; id->vendor != 0; id++) {
185 if (vendor == id->vendor &&
186 (PCI_ANY_ID == id->device || device == id->device) &&
187 (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
188 (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
190 spin_unlock(&pci_lock);
195 spin_unlock(&pci_lock);
200 linux_pci_probe(device_t dev)
202 const struct pci_device_id *id;
203 struct pci_driver *pdrv;
205 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
207 if (device_get_driver(dev) != &pdrv->bsddriver)
209 device_set_desc(dev, pdrv->name);
214 linux_pci_attach(device_t dev)
216 struct resource_list_entry *rle;
217 struct pci_bus *pbus;
218 struct pci_dev *pdev;
219 struct pci_devinfo *dinfo;
220 struct pci_driver *pdrv;
221 const struct pci_device_id *id;
225 linux_set_current(curthread);
227 pdrv = linux_pci_find(dev, &id);
228 pdev = device_get_softc(dev);
230 parent = device_get_parent(dev);
232 dinfo = device_get_ivars(parent);
233 device_set_ivars(dev, dinfo);
235 dinfo = device_get_ivars(dev);
238 pdev->dev.parent = &linux_root_device;
239 pdev->dev.bsddev = dev;
240 INIT_LIST_HEAD(&pdev->dev.irqents);
241 pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
242 pdev->device = dinfo->cfg.device;
243 pdev->vendor = dinfo->cfg.vendor;
244 pdev->subsystem_vendor = dinfo->cfg.subvendor;
245 pdev->subsystem_device = dinfo->cfg.subdevice;
246 pdev->class = pci_get_class(dev);
247 pdev->revision = pci_get_revid(dev);
249 kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
250 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
251 kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
252 kobject_name(&pdev->dev.kobj));
253 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0);
255 pdev->dev.irq = rle->start;
257 pdev->dev.irq = LINUX_IRQ_INVALID;
258 pdev->irq = pdev->dev.irq;
259 error = linux_pdev_dma_init(pdev);
263 pbus = malloc(sizeof(*pbus), M_DEVBUF, M_WAITOK | M_ZERO);
265 pbus->number = pci_get_bus(dev);
266 pbus->domain = pci_get_domain(dev);
269 spin_lock(&pci_lock);
270 list_add(&pdev->links, &pci_devices);
271 spin_unlock(&pci_lock);
273 error = pdrv->probe(pdev, id);
279 free(pdev->bus, M_DEVBUF);
280 linux_pdev_dma_uninit(pdev);
282 spin_lock(&pci_lock);
283 list_del(&pdev->links);
284 spin_unlock(&pci_lock);
285 put_device(&pdev->dev);
290 linux_pci_detach(device_t dev)
292 struct pci_dev *pdev;
294 linux_set_current(curthread);
295 pdev = device_get_softc(dev);
297 pdev->pdrv->remove(pdev);
299 free(pdev->bus, M_DEVBUF);
300 linux_pdev_dma_uninit(pdev);
302 spin_lock(&pci_lock);
303 list_del(&pdev->links);
304 spin_unlock(&pci_lock);
305 device_set_desc(dev, NULL);
306 put_device(&pdev->dev);
312 linux_pci_suspend(device_t dev)
314 const struct dev_pm_ops *pmops;
315 struct pm_message pm = { };
316 struct pci_dev *pdev;
320 linux_set_current(curthread);
321 pdev = device_get_softc(dev);
322 pmops = pdev->pdrv->driver.pm;
324 if (pdev->pdrv->suspend != NULL)
325 error = -pdev->pdrv->suspend(pdev, pm);
326 else if (pmops != NULL && pmops->suspend != NULL) {
327 error = -pmops->suspend(&pdev->dev);
328 if (error == 0 && pmops->suspend_late != NULL)
329 error = -pmops->suspend_late(&pdev->dev);
335 linux_pci_resume(device_t dev)
337 const struct dev_pm_ops *pmops;
338 struct pci_dev *pdev;
342 linux_set_current(curthread);
343 pdev = device_get_softc(dev);
344 pmops = pdev->pdrv->driver.pm;
346 if (pdev->pdrv->resume != NULL)
347 error = -pdev->pdrv->resume(pdev);
348 else if (pmops != NULL && pmops->resume != NULL) {
349 if (pmops->resume_early != NULL)
350 error = -pmops->resume_early(&pdev->dev);
351 if (error == 0 && pmops->resume != NULL)
352 error = -pmops->resume(&pdev->dev);
358 linux_pci_shutdown(device_t dev)
360 struct pci_dev *pdev;
362 linux_set_current(curthread);
363 pdev = device_get_softc(dev);
364 if (pdev->pdrv->shutdown != NULL)
365 pdev->pdrv->shutdown(pdev);
370 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
372 struct pci_dev *pdev;
375 linux_set_current(curthread);
376 pdev = device_get_softc(dev);
377 if (pdev->pdrv->bsd_iov_init != NULL)
378 error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);
385 linux_pci_iov_uninit(device_t dev)
387 struct pci_dev *pdev;
389 linux_set_current(curthread);
390 pdev = device_get_softc(dev);
391 if (pdev->pdrv->bsd_iov_uninit != NULL)
392 pdev->pdrv->bsd_iov_uninit(dev);
396 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
398 struct pci_dev *pdev;
401 linux_set_current(curthread);
402 pdev = device_get_softc(dev);
403 if (pdev->pdrv->bsd_iov_add_vf != NULL)
404 error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);
411 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
415 linux_set_current(curthread);
416 spin_lock(&pci_lock);
417 list_add(&pdrv->links, &pci_drivers);
418 spin_unlock(&pci_lock);
419 pdrv->bsddriver.name = pdrv->name;
420 pdrv->bsddriver.methods = pci_methods;
421 pdrv->bsddriver.size = sizeof(struct pci_dev);
424 error = devclass_add_driver(dc, &pdrv->bsddriver,
425 BUS_PASS_DEFAULT, &pdrv->bsdclass);
431 linux_pci_register_driver(struct pci_driver *pdrv)
435 dc = devclass_find("pci");
439 return (_linux_pci_register_driver(pdrv, dc));
443 pci_resource_start(struct pci_dev *pdev, int bar)
445 struct resource_list_entry *rle;
449 if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
451 dev = pci_find_dbsf(pdev->bus->domain, pdev->bus->number,
452 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
454 if (BUS_TRANSLATE_RESOURCE(dev, rle->type, rle->start, &newstart)) {
455 device_printf(pdev->dev.bsddev, "translate of %#jx failed\n",
456 (uintmax_t)rle->start);
463 pci_resource_len(struct pci_dev *pdev, int bar)
465 struct resource_list_entry *rle;
467 if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
473 linux_pci_register_drm_driver(struct pci_driver *pdrv)
477 dc = devclass_create("vgapci");
482 return (_linux_pci_register_driver(pdrv, dc));
486 linux_pci_unregister_driver(struct pci_driver *pdrv)
490 bus = devclass_find("pci");
492 spin_lock(&pci_lock);
493 list_del(&pdrv->links);
494 spin_unlock(&pci_lock);
497 devclass_delete_driver(bus, &pdrv->bsddriver);
502 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
506 bus = devclass_find("vgapci");
508 spin_lock(&pci_lock);
509 list_del(&pdrv->links);
510 spin_unlock(&pci_lock);
513 devclass_delete_driver(bus, &pdrv->bsddriver);
517 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
519 struct linux_dma_obj {
525 static uma_zone_t linux_dma_trie_zone;
526 static uma_zone_t linux_dma_obj_zone;
529 linux_dma_init(void *arg)
532 linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
533 pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
535 linux_dma_obj_zone = uma_zcreate("linux_dma_object",
536 sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
540 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
543 linux_dma_uninit(void *arg)
546 uma_zdestroy(linux_dma_obj_zone);
547 uma_zdestroy(linux_dma_trie_zone);
549 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
552 linux_dma_trie_alloc(struct pctrie *ptree)
555 return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
559 linux_dma_trie_free(struct pctrie *ptree, void *node)
562 uma_zfree(linux_dma_trie_zone, node);
566 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
567 linux_dma_trie_free);
570 linux_dma_alloc_coherent(struct device *dev, size_t size,
571 dma_addr_t *dma_handle, gfp_t flag)
573 struct linux_dma_priv *priv;
578 if (dev == NULL || dev->dma_priv == NULL) {
582 priv = dev->dma_priv;
584 high = priv->dma_mask;
585 else if (flag & GFP_DMA32)
586 high = BUS_SPACE_MAXADDR_32BIT;
588 high = BUS_SPACE_MAXADDR;
589 align = PAGE_SIZE << get_order(size);
590 mem = (void *)kmem_alloc_contig(size, flag, 0, high, align, 0,
593 *dma_handle = linux_dma_map_phys(dev, vtophys(mem), size);
594 if (*dma_handle == 0) {
595 kmem_free((vm_offset_t)mem, size);
604 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
606 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
608 struct linux_dma_priv *priv;
609 struct linux_dma_obj *obj;
611 bus_dma_segment_t seg;
613 priv = dev->dma_priv;
616 * If the resultant mapping will be entirely 1:1 with the
617 * physical address, short-circuit the remainder of the
618 * bus_dma API. This avoids tracking collisions in the pctrie
619 * with the additional benefit of reducing overhead.
621 if (bus_dma_id_mapped(priv->dmat, phys, len))
624 obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
630 if (bus_dmamap_create(priv->dmat, 0, &obj->dmamap) != 0) {
631 DMA_PRIV_UNLOCK(priv);
632 uma_zfree(linux_dma_obj_zone, obj);
637 if (_bus_dmamap_load_phys(priv->dmat, obj->dmamap, phys, len,
638 BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
639 bus_dmamap_destroy(priv->dmat, obj->dmamap);
640 DMA_PRIV_UNLOCK(priv);
641 uma_zfree(linux_dma_obj_zone, obj);
645 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
646 obj->dma_addr = seg.ds_addr;
648 error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
650 bus_dmamap_unload(priv->dmat, obj->dmamap);
651 bus_dmamap_destroy(priv->dmat, obj->dmamap);
652 DMA_PRIV_UNLOCK(priv);
653 uma_zfree(linux_dma_obj_zone, obj);
656 DMA_PRIV_UNLOCK(priv);
657 return (obj->dma_addr);
661 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
667 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
669 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
671 struct linux_dma_priv *priv;
672 struct linux_dma_obj *obj;
674 priv = dev->dma_priv;
676 if (pctrie_is_empty(&priv->ptree))
680 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
682 DMA_PRIV_UNLOCK(priv);
685 LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
686 bus_dmamap_unload(priv->dmat, obj->dmamap);
687 bus_dmamap_destroy(priv->dmat, obj->dmamap);
688 DMA_PRIV_UNLOCK(priv);
690 uma_zfree(linux_dma_obj_zone, obj);
694 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
700 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
701 enum dma_data_direction dir, struct dma_attrs *attrs)
703 struct linux_dma_priv *priv;
704 struct scatterlist *sg;
706 bus_dma_segment_t seg;
708 priv = dev->dma_priv;
712 /* create common DMA map in the first S/G entry */
713 if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
714 DMA_PRIV_UNLOCK(priv);
718 /* load all S/G list entries */
719 for_each_sg(sgl, sg, nents, i) {
721 if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
722 sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
724 bus_dmamap_unload(priv->dmat, sgl->dma_map);
725 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
726 DMA_PRIV_UNLOCK(priv);
730 ("More than one segment (nseg=%d)", nseg + 1));
732 sg_dma_address(sg) = seg.ds_addr;
734 DMA_PRIV_UNLOCK(priv);
740 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
741 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
743 struct linux_dma_priv *priv;
745 priv = dev->dma_priv;
748 bus_dmamap_unload(priv->dmat, sgl->dma_map);
749 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
750 DMA_PRIV_UNLOCK(priv);
754 struct device *pool_device;
755 uma_zone_t pool_zone;
756 struct mtx pool_lock;
757 bus_dma_tag_t pool_dmat;
758 size_t pool_entry_size;
759 struct pctrie pool_ptree;
762 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
763 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
766 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
768 struct linux_dma_obj *obj = mem;
769 struct dma_pool *pool = arg;
771 bus_dma_segment_t seg;
775 error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
776 vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
778 DMA_POOL_UNLOCK(pool);
782 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
783 obj->dma_addr = seg.ds_addr;
789 dma_pool_obj_dtor(void *mem, int size, void *arg)
791 struct linux_dma_obj *obj = mem;
792 struct dma_pool *pool = arg;
795 bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
796 DMA_POOL_UNLOCK(pool);
800 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
803 struct dma_pool *pool = arg;
804 struct linux_dma_priv *priv;
805 struct linux_dma_obj *obj;
808 priv = pool->pool_device->dma_priv;
809 for (i = 0; i < count; i++) {
810 obj = uma_zalloc(linux_dma_obj_zone, flags);
814 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
815 BUS_DMA_NOWAIT, &obj->dmamap);
817 uma_zfree(linux_dma_obj_zone, obj);
828 dma_pool_obj_release(void *arg, void **store, int count)
830 struct dma_pool *pool = arg;
831 struct linux_dma_priv *priv;
832 struct linux_dma_obj *obj;
835 priv = pool->pool_device->dma_priv;
836 for (i = 0; i < count; i++) {
838 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
839 uma_zfree(linux_dma_obj_zone, obj);
844 linux_dma_pool_create(char *name, struct device *dev, size_t size,
845 size_t align, size_t boundary)
847 struct linux_dma_priv *priv;
848 struct dma_pool *pool;
850 priv = dev->dma_priv;
852 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
853 pool->pool_device = dev;
854 pool->pool_entry_size = size;
856 if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
857 align, boundary, /* alignment, boundary */
858 priv->dma_mask, /* lowaddr */
859 BUS_SPACE_MAXADDR, /* highaddr */
860 NULL, NULL, /* filtfunc, filtfuncarg */
865 NULL, NULL, /* lockfunc, lockfuncarg */
871 pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
872 dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
873 dma_pool_obj_release, pool, 0);
875 mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
876 pctrie_init(&pool->pool_ptree);
882 linux_dma_pool_destroy(struct dma_pool *pool)
885 uma_zdestroy(pool->pool_zone);
886 bus_dma_tag_destroy(pool->pool_dmat);
887 mtx_destroy(&pool->pool_lock);
892 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
895 struct linux_dma_obj *obj;
897 obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags);
902 if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
903 DMA_POOL_UNLOCK(pool);
904 uma_zfree_arg(pool->pool_zone, obj, pool);
907 DMA_POOL_UNLOCK(pool);
909 *handle = obj->dma_addr;
914 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
916 struct linux_dma_obj *obj;
919 obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
921 DMA_POOL_UNLOCK(pool);
924 LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
925 DMA_POOL_UNLOCK(pool);
927 uma_zfree_arg(pool->pool_zone, obj, pool);