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/pctrie.h>
42 #include <sys/rwlock.h>
47 #include <machine/stdarg.h>
49 #include <linux/kobject.h>
50 #include <linux/device.h>
51 #include <linux/slab.h>
52 #include <linux/module.h>
53 #include <linux/cdev.h>
54 #include <linux/file.h>
55 #include <linux/sysfs.h>
58 #include <linux/vmalloc.h>
59 #include <linux/pci.h>
60 #include <linux/compat.h>
62 static device_probe_t linux_pci_probe;
63 static device_attach_t linux_pci_attach;
64 static device_detach_t linux_pci_detach;
65 static device_suspend_t linux_pci_suspend;
66 static device_resume_t linux_pci_resume;
67 static device_shutdown_t linux_pci_shutdown;
69 static device_method_t pci_methods[] = {
70 DEVMETHOD(device_probe, linux_pci_probe),
71 DEVMETHOD(device_attach, linux_pci_attach),
72 DEVMETHOD(device_detach, linux_pci_detach),
73 DEVMETHOD(device_suspend, linux_pci_suspend),
74 DEVMETHOD(device_resume, linux_pci_resume),
75 DEVMETHOD(device_shutdown, linux_pci_shutdown),
79 struct linux_dma_priv {
85 #define DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
86 #define DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
89 linux_pdev_dma_init(struct pci_dev *pdev)
91 struct linux_dma_priv *priv;
94 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
95 pdev->dev.dma_priv = priv;
97 mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
99 pctrie_init(&priv->ptree);
101 /* create a default DMA tag */
102 error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
104 mtx_destroy(&priv->lock);
105 free(priv, M_DEVBUF);
106 pdev->dev.dma_priv = NULL;
112 linux_pdev_dma_uninit(struct pci_dev *pdev)
114 struct linux_dma_priv *priv;
116 priv = pdev->dev.dma_priv;
118 bus_dma_tag_destroy(priv->dmat);
119 mtx_destroy(&priv->lock);
120 free(priv, M_DEVBUF);
121 pdev->dev.dma_priv = NULL;
126 linux_dma_tag_init(struct device *dev, u64 dma_mask)
128 struct linux_dma_priv *priv;
131 priv = dev->dma_priv;
134 if (priv->dma_mask == dma_mask)
137 bus_dma_tag_destroy(priv->dmat);
140 priv->dma_mask = dma_mask;
142 error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
143 1, 0, /* alignment, boundary */
144 dma_mask, /* lowaddr */
145 BUS_SPACE_MAXADDR, /* highaddr */
146 NULL, NULL, /* filtfunc, filtfuncarg */
147 BUS_SPACE_MAXSIZE, /* maxsize */
149 BUS_SPACE_MAXSIZE, /* maxsegsz */
151 NULL, NULL, /* lockfunc, lockfuncarg */
156 static struct pci_driver *
157 linux_pci_find(device_t dev, const struct pci_device_id **idp)
159 const struct pci_device_id *id;
160 struct pci_driver *pdrv;
166 vendor = pci_get_vendor(dev);
167 device = pci_get_device(dev);
168 subvendor = pci_get_subvendor(dev);
169 subdevice = pci_get_subdevice(dev);
171 spin_lock(&pci_lock);
172 list_for_each_entry(pdrv, &pci_drivers, links) {
173 for (id = pdrv->id_table; id->vendor != 0; id++) {
174 if (vendor == id->vendor &&
175 (PCI_ANY_ID == id->device || device == id->device) &&
176 (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
177 (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
179 spin_unlock(&pci_lock);
184 spin_unlock(&pci_lock);
189 linux_pci_probe(device_t dev)
191 const struct pci_device_id *id;
192 struct pci_driver *pdrv;
194 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
196 if (device_get_driver(dev) != &pdrv->bsddriver)
198 device_set_desc(dev, pdrv->name);
203 linux_pci_attach(device_t dev)
205 struct resource_list_entry *rle;
206 struct pci_bus *pbus;
207 struct pci_dev *pdev;
208 struct pci_devinfo *dinfo;
209 struct pci_driver *pdrv;
210 const struct pci_device_id *id;
214 linux_set_current(curthread);
216 pdrv = linux_pci_find(dev, &id);
217 pdev = device_get_softc(dev);
219 parent = device_get_parent(dev);
221 dinfo = device_get_ivars(parent);
222 device_set_ivars(dev, dinfo);
224 dinfo = device_get_ivars(dev);
227 pdev->dev.parent = &linux_root_device;
228 pdev->dev.bsddev = dev;
229 INIT_LIST_HEAD(&pdev->dev.irqents);
230 pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
231 pdev->device = dinfo->cfg.device;
232 pdev->vendor = dinfo->cfg.vendor;
233 pdev->subsystem_vendor = dinfo->cfg.subvendor;
234 pdev->subsystem_device = dinfo->cfg.subdevice;
235 pdev->class = pci_get_class(dev);
236 pdev->revision = pci_get_revid(dev);
238 kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
239 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
240 kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
241 kobject_name(&pdev->dev.kobj));
242 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0);
244 pdev->dev.irq = rle->start;
246 pdev->dev.irq = LINUX_IRQ_INVALID;
247 pdev->irq = pdev->dev.irq;
248 error = linux_pdev_dma_init(pdev);
252 pbus = malloc(sizeof(*pbus), M_DEVBUF, M_WAITOK | M_ZERO);
254 pbus->number = pci_get_bus(dev);
255 pbus->domain = pci_get_domain(dev);
258 spin_lock(&pci_lock);
259 list_add(&pdev->links, &pci_devices);
260 spin_unlock(&pci_lock);
262 error = pdrv->probe(pdev, id);
268 free(pdev->bus, M_DEVBUF);
269 linux_pdev_dma_uninit(pdev);
271 spin_lock(&pci_lock);
272 list_del(&pdev->links);
273 spin_unlock(&pci_lock);
274 put_device(&pdev->dev);
279 linux_pci_detach(device_t dev)
281 struct pci_dev *pdev;
283 linux_set_current(curthread);
284 pdev = device_get_softc(dev);
286 pdev->pdrv->remove(pdev);
288 free(pdev->bus, M_DEVBUF);
289 linux_pdev_dma_uninit(pdev);
291 spin_lock(&pci_lock);
292 list_del(&pdev->links);
293 spin_unlock(&pci_lock);
294 device_set_desc(dev, NULL);
295 put_device(&pdev->dev);
301 linux_pci_suspend(device_t dev)
303 const struct dev_pm_ops *pmops;
304 struct pm_message pm = { };
305 struct pci_dev *pdev;
309 linux_set_current(curthread);
310 pdev = device_get_softc(dev);
311 pmops = pdev->pdrv->driver.pm;
313 if (pdev->pdrv->suspend != NULL)
314 error = -pdev->pdrv->suspend(pdev, pm);
315 else if (pmops != NULL && pmops->suspend != NULL) {
316 error = -pmops->suspend(&pdev->dev);
317 if (error == 0 && pmops->suspend_late != NULL)
318 error = -pmops->suspend_late(&pdev->dev);
324 linux_pci_resume(device_t dev)
326 const struct dev_pm_ops *pmops;
327 struct pci_dev *pdev;
331 linux_set_current(curthread);
332 pdev = device_get_softc(dev);
333 pmops = pdev->pdrv->driver.pm;
335 if (pdev->pdrv->resume != NULL)
336 error = -pdev->pdrv->resume(pdev);
337 else if (pmops != NULL && pmops->resume != NULL) {
338 if (pmops->resume_early != NULL)
339 error = -pmops->resume_early(&pdev->dev);
340 if (error == 0 && pmops->resume != NULL)
341 error = -pmops->resume(&pdev->dev);
347 linux_pci_shutdown(device_t dev)
349 struct pci_dev *pdev;
351 linux_set_current(curthread);
352 pdev = device_get_softc(dev);
353 if (pdev->pdrv->shutdown != NULL)
354 pdev->pdrv->shutdown(pdev);
359 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
363 linux_set_current(curthread);
364 spin_lock(&pci_lock);
365 list_add(&pdrv->links, &pci_drivers);
366 spin_unlock(&pci_lock);
367 pdrv->bsddriver.name = pdrv->name;
368 pdrv->bsddriver.methods = pci_methods;
369 pdrv->bsddriver.size = sizeof(struct pci_dev);
372 error = devclass_add_driver(dc, &pdrv->bsddriver,
373 BUS_PASS_DEFAULT, &pdrv->bsdclass);
379 linux_pci_register_driver(struct pci_driver *pdrv)
383 dc = devclass_find("pci");
387 return (_linux_pci_register_driver(pdrv, dc));
391 pci_resource_start(struct pci_dev *pdev, int bar)
393 struct resource_list_entry *rle;
397 if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
399 dev = pci_find_dbsf(pdev->bus->domain, pdev->bus->number,
400 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
402 if (BUS_TRANSLATE_RESOURCE(dev, rle->type, rle->start, &newstart)) {
403 device_printf(pdev->dev.bsddev, "translate of %#jx failed\n",
404 (uintmax_t)rle->start);
411 pci_resource_len(struct pci_dev *pdev, int bar)
413 struct resource_list_entry *rle;
415 if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
421 linux_pci_register_drm_driver(struct pci_driver *pdrv)
425 dc = devclass_create("vgapci");
430 return (_linux_pci_register_driver(pdrv, dc));
434 linux_pci_unregister_driver(struct pci_driver *pdrv)
438 bus = devclass_find("pci");
440 spin_lock(&pci_lock);
441 list_del(&pdrv->links);
442 spin_unlock(&pci_lock);
445 devclass_delete_driver(bus, &pdrv->bsddriver);
450 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
454 bus = devclass_find("vgapci");
456 spin_lock(&pci_lock);
457 list_del(&pdrv->links);
458 spin_unlock(&pci_lock);
461 devclass_delete_driver(bus, &pdrv->bsddriver);
465 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
467 struct linux_dma_obj {
473 static uma_zone_t linux_dma_trie_zone;
474 static uma_zone_t linux_dma_obj_zone;
477 linux_dma_init(void *arg)
480 linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
481 pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
483 linux_dma_obj_zone = uma_zcreate("linux_dma_object",
484 sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
488 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
491 linux_dma_uninit(void *arg)
494 uma_zdestroy(linux_dma_obj_zone);
495 uma_zdestroy(linux_dma_trie_zone);
497 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
500 linux_dma_trie_alloc(struct pctrie *ptree)
503 return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
507 linux_dma_trie_free(struct pctrie *ptree, void *node)
510 uma_zfree(linux_dma_trie_zone, node);
514 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
515 linux_dma_trie_free);
518 linux_dma_alloc_coherent(struct device *dev, size_t size,
519 dma_addr_t *dma_handle, gfp_t flag)
521 struct linux_dma_priv *priv;
526 if (dev == NULL || dev->dma_priv == NULL) {
530 priv = dev->dma_priv;
532 high = priv->dma_mask;
533 else if (flag & GFP_DMA32)
534 high = BUS_SPACE_MAXADDR_32BIT;
536 high = BUS_SPACE_MAXADDR;
537 align = PAGE_SIZE << get_order(size);
538 mem = (void *)kmem_alloc_contig(size, flag, 0, high, align, 0,
541 *dma_handle = linux_dma_map_phys(dev, vtophys(mem), size);
542 if (*dma_handle == 0) {
543 kmem_free((vm_offset_t)mem, size);
552 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
554 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
556 struct linux_dma_priv *priv;
557 struct linux_dma_obj *obj;
559 bus_dma_segment_t seg;
561 priv = dev->dma_priv;
564 * If the resultant mapping will be entirely 1:1 with the
565 * physical address, short-circuit the remainder of the
566 * bus_dma API. This avoids tracking collisions in the pctrie
567 * with the additional benefit of reducing overhead.
569 if (bus_dma_id_mapped(priv->dmat, phys, len))
572 obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
578 if (bus_dmamap_create(priv->dmat, 0, &obj->dmamap) != 0) {
579 DMA_PRIV_UNLOCK(priv);
580 uma_zfree(linux_dma_obj_zone, obj);
585 if (_bus_dmamap_load_phys(priv->dmat, obj->dmamap, phys, len,
586 BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
587 bus_dmamap_destroy(priv->dmat, obj->dmamap);
588 DMA_PRIV_UNLOCK(priv);
589 uma_zfree(linux_dma_obj_zone, obj);
593 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
594 obj->dma_addr = seg.ds_addr;
596 error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
598 bus_dmamap_unload(priv->dmat, obj->dmamap);
599 bus_dmamap_destroy(priv->dmat, obj->dmamap);
600 DMA_PRIV_UNLOCK(priv);
601 uma_zfree(linux_dma_obj_zone, obj);
604 DMA_PRIV_UNLOCK(priv);
605 return (obj->dma_addr);
609 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
615 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
617 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
619 struct linux_dma_priv *priv;
620 struct linux_dma_obj *obj;
622 priv = dev->dma_priv;
624 if (pctrie_is_empty(&priv->ptree))
628 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
630 DMA_PRIV_UNLOCK(priv);
633 LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
634 bus_dmamap_unload(priv->dmat, obj->dmamap);
635 bus_dmamap_destroy(priv->dmat, obj->dmamap);
636 DMA_PRIV_UNLOCK(priv);
638 uma_zfree(linux_dma_obj_zone, obj);
642 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
648 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
649 enum dma_data_direction dir, struct dma_attrs *attrs)
651 struct linux_dma_priv *priv;
652 struct scatterlist *sg;
654 bus_dma_segment_t seg;
656 priv = dev->dma_priv;
660 /* create common DMA map in the first S/G entry */
661 if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
662 DMA_PRIV_UNLOCK(priv);
666 /* load all S/G list entries */
667 for_each_sg(sgl, sg, nents, i) {
669 if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
670 sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
672 bus_dmamap_unload(priv->dmat, sgl->dma_map);
673 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
674 DMA_PRIV_UNLOCK(priv);
678 ("More than one segment (nseg=%d)", nseg + 1));
680 sg_dma_address(sg) = seg.ds_addr;
682 DMA_PRIV_UNLOCK(priv);
688 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
689 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
691 struct linux_dma_priv *priv;
693 priv = dev->dma_priv;
696 bus_dmamap_unload(priv->dmat, sgl->dma_map);
697 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
698 DMA_PRIV_UNLOCK(priv);
702 struct device *pool_device;
703 uma_zone_t pool_zone;
704 struct mtx pool_lock;
705 bus_dma_tag_t pool_dmat;
706 size_t pool_entry_size;
707 struct pctrie pool_ptree;
710 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
711 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
714 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
716 struct linux_dma_obj *obj = mem;
717 struct dma_pool *pool = arg;
719 bus_dma_segment_t seg;
723 error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
724 vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
726 DMA_POOL_UNLOCK(pool);
730 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
731 obj->dma_addr = seg.ds_addr;
737 dma_pool_obj_dtor(void *mem, int size, void *arg)
739 struct linux_dma_obj *obj = mem;
740 struct dma_pool *pool = arg;
743 bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
744 DMA_POOL_UNLOCK(pool);
748 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
751 struct dma_pool *pool = arg;
752 struct linux_dma_priv *priv;
753 struct linux_dma_obj *obj;
756 priv = pool->pool_device->dma_priv;
757 for (i = 0; i < count; i++) {
758 obj = uma_zalloc(linux_dma_obj_zone, flags);
762 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
763 BUS_DMA_NOWAIT, &obj->dmamap);
765 uma_zfree(linux_dma_obj_zone, obj);
776 dma_pool_obj_release(void *arg, void **store, int count)
778 struct dma_pool *pool = arg;
779 struct linux_dma_priv *priv;
780 struct linux_dma_obj *obj;
783 priv = pool->pool_device->dma_priv;
784 for (i = 0; i < count; i++) {
786 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
787 uma_zfree(linux_dma_obj_zone, obj);
792 linux_dma_pool_create(char *name, struct device *dev, size_t size,
793 size_t align, size_t boundary)
795 struct linux_dma_priv *priv;
796 struct dma_pool *pool;
798 priv = dev->dma_priv;
800 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
801 pool->pool_device = dev;
802 pool->pool_entry_size = size;
804 if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
805 align, boundary, /* alignment, boundary */
806 priv->dma_mask, /* lowaddr */
807 BUS_SPACE_MAXADDR, /* highaddr */
808 NULL, NULL, /* filtfunc, filtfuncarg */
813 NULL, NULL, /* lockfunc, lockfuncarg */
819 pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
820 dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
821 dma_pool_obj_release, pool, 0);
823 mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
824 pctrie_init(&pool->pool_ptree);
830 linux_dma_pool_destroy(struct dma_pool *pool)
833 uma_zdestroy(pool->pool_zone);
834 bus_dma_tag_destroy(pool->pool_dmat);
835 mtx_destroy(&pool->pool_lock);
840 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
843 struct linux_dma_obj *obj;
845 obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags);
850 if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
851 DMA_POOL_UNLOCK(pool);
852 uma_zfree_arg(pool->pool_zone, obj, pool);
855 DMA_POOL_UNLOCK(pool);
857 *handle = obj->dma_addr;
862 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
864 struct linux_dma_obj *obj;
867 obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
869 DMA_POOL_UNLOCK(pool);
872 LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
873 DMA_POOL_UNLOCK(pool);
875 uma_zfree_arg(pool->pool_zone, obj, pool);