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>
53 #include <dev/backlight/backlight.h>
55 #include <linux/kobject.h>
56 #include <linux/device.h>
57 #include <linux/slab.h>
58 #include <linux/module.h>
59 #include <linux/cdev.h>
60 #include <linux/file.h>
61 #include <linux/sysfs.h>
64 #include <linux/vmalloc.h>
65 #include <linux/pci.h>
66 #include <linux/compat.h>
68 #include <linux/backlight.h>
70 #include "backlight_if.h"
72 static device_probe_t linux_pci_probe;
73 static device_attach_t linux_pci_attach;
74 static device_detach_t linux_pci_detach;
75 static device_suspend_t linux_pci_suspend;
76 static device_resume_t linux_pci_resume;
77 static device_shutdown_t linux_pci_shutdown;
78 static pci_iov_init_t linux_pci_iov_init;
79 static pci_iov_uninit_t linux_pci_iov_uninit;
80 static pci_iov_add_vf_t linux_pci_iov_add_vf;
81 static int linux_backlight_get_status(device_t dev, struct backlight_props *props);
82 static int linux_backlight_update_status(device_t dev, struct backlight_props *props);
83 static int linux_backlight_get_info(device_t dev, struct backlight_info *info);
85 static device_method_t pci_methods[] = {
86 DEVMETHOD(device_probe, linux_pci_probe),
87 DEVMETHOD(device_attach, linux_pci_attach),
88 DEVMETHOD(device_detach, linux_pci_detach),
89 DEVMETHOD(device_suspend, linux_pci_suspend),
90 DEVMETHOD(device_resume, linux_pci_resume),
91 DEVMETHOD(device_shutdown, linux_pci_shutdown),
92 DEVMETHOD(pci_iov_init, linux_pci_iov_init),
93 DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit),
94 DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf),
96 /* backlight interface */
97 DEVMETHOD(backlight_update_status, linux_backlight_update_status),
98 DEVMETHOD(backlight_get_status, linux_backlight_get_status),
99 DEVMETHOD(backlight_get_info, linux_backlight_get_info),
103 struct linux_dma_priv {
109 #define DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
110 #define DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
113 linux_pdev_dma_init(struct pci_dev *pdev)
115 struct linux_dma_priv *priv;
118 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
119 pdev->dev.dma_priv = priv;
121 mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
123 pctrie_init(&priv->ptree);
125 /* create a default DMA tag */
126 error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
128 mtx_destroy(&priv->lock);
129 free(priv, M_DEVBUF);
130 pdev->dev.dma_priv = NULL;
136 linux_pdev_dma_uninit(struct pci_dev *pdev)
138 struct linux_dma_priv *priv;
140 priv = pdev->dev.dma_priv;
142 bus_dma_tag_destroy(priv->dmat);
143 mtx_destroy(&priv->lock);
144 free(priv, M_DEVBUF);
145 pdev->dev.dma_priv = NULL;
150 linux_dma_tag_init(struct device *dev, u64 dma_mask)
152 struct linux_dma_priv *priv;
155 priv = dev->dma_priv;
158 if (priv->dma_mask == dma_mask)
161 bus_dma_tag_destroy(priv->dmat);
164 priv->dma_mask = dma_mask;
166 error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
167 1, 0, /* alignment, boundary */
168 dma_mask, /* lowaddr */
169 BUS_SPACE_MAXADDR, /* highaddr */
170 NULL, NULL, /* filtfunc, filtfuncarg */
171 BUS_SPACE_MAXSIZE, /* maxsize */
173 BUS_SPACE_MAXSIZE, /* maxsegsz */
175 NULL, NULL, /* lockfunc, lockfuncarg */
180 static struct pci_driver *
181 linux_pci_find(device_t dev, const struct pci_device_id **idp)
183 const struct pci_device_id *id;
184 struct pci_driver *pdrv;
190 vendor = pci_get_vendor(dev);
191 device = pci_get_device(dev);
192 subvendor = pci_get_subvendor(dev);
193 subdevice = pci_get_subdevice(dev);
195 spin_lock(&pci_lock);
196 list_for_each_entry(pdrv, &pci_drivers, links) {
197 for (id = pdrv->id_table; id->vendor != 0; id++) {
198 if (vendor == id->vendor &&
199 (PCI_ANY_ID == id->device || device == id->device) &&
200 (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
201 (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
203 spin_unlock(&pci_lock);
208 spin_unlock(&pci_lock);
213 linux_pci_probe(device_t dev)
215 const struct pci_device_id *id;
216 struct pci_driver *pdrv;
218 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
220 if (device_get_driver(dev) != &pdrv->bsddriver)
222 device_set_desc(dev, pdrv->name);
227 linux_pci_attach(device_t dev)
229 const struct pci_device_id *id;
230 struct pci_driver *pdrv;
231 struct pci_dev *pdev;
233 pdrv = linux_pci_find(dev, &id);
234 pdev = device_get_softc(dev);
239 return (linux_pci_attach_device(dev, pdrv, id, pdev));
243 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
244 const struct pci_device_id *id, struct pci_dev *pdev)
246 struct resource_list_entry *rle;
247 struct pci_bus *pbus;
248 struct pci_devinfo *dinfo;
252 linux_set_current(curthread);
254 if (pdrv != NULL && pdrv->isdrm) {
255 parent = device_get_parent(dev);
256 dinfo = device_get_ivars(parent);
257 device_set_ivars(dev, dinfo);
259 dinfo = device_get_ivars(dev);
262 pdev->dev.parent = &linux_root_device;
263 pdev->dev.bsddev = dev;
264 INIT_LIST_HEAD(&pdev->dev.irqents);
265 pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
266 pdev->device = dinfo->cfg.device;
267 pdev->vendor = dinfo->cfg.vendor;
268 pdev->subsystem_vendor = dinfo->cfg.subvendor;
269 pdev->subsystem_device = dinfo->cfg.subdevice;
270 pdev->class = pci_get_class(dev);
271 pdev->revision = pci_get_revid(dev);
273 kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
274 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
275 kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
276 kobject_name(&pdev->dev.kobj));
277 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0);
279 pdev->dev.irq = rle->start;
281 pdev->dev.irq = LINUX_IRQ_INVALID;
282 pdev->irq = pdev->dev.irq;
283 error = linux_pdev_dma_init(pdev);
287 TAILQ_INIT(&pdev->mmio);
288 pbus = malloc(sizeof(*pbus), M_DEVBUF, M_WAITOK | M_ZERO);
290 pbus->number = pci_get_bus(dev);
291 pbus->domain = pci_get_domain(dev);
294 spin_lock(&pci_lock);
295 list_add(&pdev->links, &pci_devices);
296 spin_unlock(&pci_lock);
299 error = pdrv->probe(pdev, id);
306 free(pdev->bus, M_DEVBUF);
307 linux_pdev_dma_uninit(pdev);
309 spin_lock(&pci_lock);
310 list_del(&pdev->links);
311 spin_unlock(&pci_lock);
312 put_device(&pdev->dev);
317 linux_pci_detach(device_t dev)
319 struct pci_dev *pdev;
321 pdev = device_get_softc(dev);
325 device_set_desc(dev, NULL);
327 return (linux_pci_detach_device(pdev));
331 linux_pci_detach_device(struct pci_dev *pdev)
334 linux_set_current(curthread);
336 if (pdev->pdrv != NULL)
337 pdev->pdrv->remove(pdev);
339 free(pdev->bus, M_DEVBUF);
340 linux_pdev_dma_uninit(pdev);
342 spin_lock(&pci_lock);
343 list_del(&pdev->links);
344 spin_unlock(&pci_lock);
345 put_device(&pdev->dev);
351 linux_pci_suspend(device_t dev)
353 const struct dev_pm_ops *pmops;
354 struct pm_message pm = { };
355 struct pci_dev *pdev;
359 linux_set_current(curthread);
360 pdev = device_get_softc(dev);
361 pmops = pdev->pdrv->driver.pm;
363 if (pdev->pdrv->suspend != NULL)
364 error = -pdev->pdrv->suspend(pdev, pm);
365 else if (pmops != NULL && pmops->suspend != NULL) {
366 error = -pmops->suspend(&pdev->dev);
367 if (error == 0 && pmops->suspend_late != NULL)
368 error = -pmops->suspend_late(&pdev->dev);
374 linux_pci_resume(device_t dev)
376 const struct dev_pm_ops *pmops;
377 struct pci_dev *pdev;
381 linux_set_current(curthread);
382 pdev = device_get_softc(dev);
383 pmops = pdev->pdrv->driver.pm;
385 if (pdev->pdrv->resume != NULL)
386 error = -pdev->pdrv->resume(pdev);
387 else if (pmops != NULL && pmops->resume != NULL) {
388 if (pmops->resume_early != NULL)
389 error = -pmops->resume_early(&pdev->dev);
390 if (error == 0 && pmops->resume != NULL)
391 error = -pmops->resume(&pdev->dev);
397 linux_pci_shutdown(device_t dev)
399 struct pci_dev *pdev;
401 linux_set_current(curthread);
402 pdev = device_get_softc(dev);
403 if (pdev->pdrv->shutdown != NULL)
404 pdev->pdrv->shutdown(pdev);
409 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
411 struct pci_dev *pdev;
414 linux_set_current(curthread);
415 pdev = device_get_softc(dev);
416 if (pdev->pdrv->bsd_iov_init != NULL)
417 error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);
424 linux_pci_iov_uninit(device_t dev)
426 struct pci_dev *pdev;
428 linux_set_current(curthread);
429 pdev = device_get_softc(dev);
430 if (pdev->pdrv->bsd_iov_uninit != NULL)
431 pdev->pdrv->bsd_iov_uninit(dev);
435 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
437 struct pci_dev *pdev;
440 linux_set_current(curthread);
441 pdev = device_get_softc(dev);
442 if (pdev->pdrv->bsd_iov_add_vf != NULL)
443 error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);
450 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
454 linux_set_current(curthread);
455 spin_lock(&pci_lock);
456 list_add(&pdrv->links, &pci_drivers);
457 spin_unlock(&pci_lock);
458 pdrv->bsddriver.name = pdrv->name;
459 pdrv->bsddriver.methods = pci_methods;
460 pdrv->bsddriver.size = sizeof(struct pci_dev);
463 error = devclass_add_driver(dc, &pdrv->bsddriver,
464 BUS_PASS_DEFAULT, &pdrv->bsdclass);
470 linux_pci_register_driver(struct pci_driver *pdrv)
474 dc = devclass_find("pci");
478 return (_linux_pci_register_driver(pdrv, dc));
482 pci_resource_start(struct pci_dev *pdev, int bar)
484 struct resource_list_entry *rle;
488 if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
490 dev = pci_find_dbsf(pdev->bus->domain, pdev->bus->number,
491 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
493 if (BUS_TRANSLATE_RESOURCE(dev, rle->type, rle->start, &newstart)) {
494 device_printf(pdev->dev.bsddev, "translate of %#jx failed\n",
495 (uintmax_t)rle->start);
502 pci_resource_len(struct pci_dev *pdev, int bar)
504 struct resource_list_entry *rle;
506 if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
512 linux_pci_register_drm_driver(struct pci_driver *pdrv)
516 dc = devclass_create("vgapci");
521 return (_linux_pci_register_driver(pdrv, dc));
525 linux_pci_unregister_driver(struct pci_driver *pdrv)
529 bus = devclass_find("pci");
531 spin_lock(&pci_lock);
532 list_del(&pdrv->links);
533 spin_unlock(&pci_lock);
536 devclass_delete_driver(bus, &pdrv->bsddriver);
541 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
545 bus = devclass_find("vgapci");
547 spin_lock(&pci_lock);
548 list_del(&pdrv->links);
549 spin_unlock(&pci_lock);
552 devclass_delete_driver(bus, &pdrv->bsddriver);
556 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
558 struct linux_dma_obj {
564 static uma_zone_t linux_dma_trie_zone;
565 static uma_zone_t linux_dma_obj_zone;
568 linux_dma_init(void *arg)
571 linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
572 pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
574 linux_dma_obj_zone = uma_zcreate("linux_dma_object",
575 sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
579 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
582 linux_dma_uninit(void *arg)
585 uma_zdestroy(linux_dma_obj_zone);
586 uma_zdestroy(linux_dma_trie_zone);
588 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
591 linux_dma_trie_alloc(struct pctrie *ptree)
594 return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
598 linux_dma_trie_free(struct pctrie *ptree, void *node)
601 uma_zfree(linux_dma_trie_zone, node);
604 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
605 linux_dma_trie_free);
608 linux_dma_alloc_coherent(struct device *dev, size_t size,
609 dma_addr_t *dma_handle, gfp_t flag)
611 struct linux_dma_priv *priv;
616 if (dev == NULL || dev->dma_priv == NULL) {
620 priv = dev->dma_priv;
622 high = priv->dma_mask;
623 else if (flag & GFP_DMA32)
624 high = BUS_SPACE_MAXADDR_32BIT;
626 high = BUS_SPACE_MAXADDR;
627 align = PAGE_SIZE << get_order(size);
628 mem = (void *)kmem_alloc_contig(size, flag, 0, high, align, 0,
631 *dma_handle = linux_dma_map_phys(dev, vtophys(mem), size);
632 if (*dma_handle == 0) {
633 kmem_free((vm_offset_t)mem, size);
642 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
644 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
646 struct linux_dma_priv *priv;
647 struct linux_dma_obj *obj;
649 bus_dma_segment_t seg;
651 priv = dev->dma_priv;
654 * If the resultant mapping will be entirely 1:1 with the
655 * physical address, short-circuit the remainder of the
656 * bus_dma API. This avoids tracking collisions in the pctrie
657 * with the additional benefit of reducing overhead.
659 if (bus_dma_id_mapped(priv->dmat, phys, len))
662 obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
668 if (bus_dmamap_create(priv->dmat, 0, &obj->dmamap) != 0) {
669 DMA_PRIV_UNLOCK(priv);
670 uma_zfree(linux_dma_obj_zone, obj);
675 if (_bus_dmamap_load_phys(priv->dmat, obj->dmamap, phys, len,
676 BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
677 bus_dmamap_destroy(priv->dmat, obj->dmamap);
678 DMA_PRIV_UNLOCK(priv);
679 uma_zfree(linux_dma_obj_zone, obj);
683 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
684 obj->dma_addr = seg.ds_addr;
686 error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
688 bus_dmamap_unload(priv->dmat, obj->dmamap);
689 bus_dmamap_destroy(priv->dmat, obj->dmamap);
690 DMA_PRIV_UNLOCK(priv);
691 uma_zfree(linux_dma_obj_zone, obj);
694 DMA_PRIV_UNLOCK(priv);
695 return (obj->dma_addr);
699 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
705 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
707 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
709 struct linux_dma_priv *priv;
710 struct linux_dma_obj *obj;
712 priv = dev->dma_priv;
714 if (pctrie_is_empty(&priv->ptree))
718 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
720 DMA_PRIV_UNLOCK(priv);
723 LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
724 bus_dmamap_unload(priv->dmat, obj->dmamap);
725 bus_dmamap_destroy(priv->dmat, obj->dmamap);
726 DMA_PRIV_UNLOCK(priv);
728 uma_zfree(linux_dma_obj_zone, obj);
732 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
738 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
739 enum dma_data_direction dir, struct dma_attrs *attrs)
741 struct linux_dma_priv *priv;
742 struct scatterlist *sg;
744 bus_dma_segment_t seg;
746 priv = dev->dma_priv;
750 /* create common DMA map in the first S/G entry */
751 if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
752 DMA_PRIV_UNLOCK(priv);
756 /* load all S/G list entries */
757 for_each_sg(sgl, sg, nents, i) {
759 if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
760 sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
762 bus_dmamap_unload(priv->dmat, sgl->dma_map);
763 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
764 DMA_PRIV_UNLOCK(priv);
768 ("More than one segment (nseg=%d)", nseg + 1));
770 sg_dma_address(sg) = seg.ds_addr;
772 DMA_PRIV_UNLOCK(priv);
778 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
779 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
781 struct linux_dma_priv *priv;
783 priv = dev->dma_priv;
786 bus_dmamap_unload(priv->dmat, sgl->dma_map);
787 bus_dmamap_destroy(priv->dmat, sgl->dma_map);
788 DMA_PRIV_UNLOCK(priv);
792 struct device *pool_device;
793 uma_zone_t pool_zone;
794 struct mtx pool_lock;
795 bus_dma_tag_t pool_dmat;
796 size_t pool_entry_size;
797 struct pctrie pool_ptree;
800 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
801 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
804 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
806 struct linux_dma_obj *obj = mem;
807 struct dma_pool *pool = arg;
809 bus_dma_segment_t seg;
813 error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
814 vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
816 DMA_POOL_UNLOCK(pool);
820 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
821 obj->dma_addr = seg.ds_addr;
827 dma_pool_obj_dtor(void *mem, int size, void *arg)
829 struct linux_dma_obj *obj = mem;
830 struct dma_pool *pool = arg;
833 bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
834 DMA_POOL_UNLOCK(pool);
838 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
841 struct dma_pool *pool = arg;
842 struct linux_dma_priv *priv;
843 struct linux_dma_obj *obj;
846 priv = pool->pool_device->dma_priv;
847 for (i = 0; i < count; i++) {
848 obj = uma_zalloc(linux_dma_obj_zone, flags);
852 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
853 BUS_DMA_NOWAIT, &obj->dmamap);
855 uma_zfree(linux_dma_obj_zone, obj);
866 dma_pool_obj_release(void *arg, void **store, int count)
868 struct dma_pool *pool = arg;
869 struct linux_dma_priv *priv;
870 struct linux_dma_obj *obj;
873 priv = pool->pool_device->dma_priv;
874 for (i = 0; i < count; i++) {
876 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
877 uma_zfree(linux_dma_obj_zone, obj);
882 linux_dma_pool_create(char *name, struct device *dev, size_t size,
883 size_t align, size_t boundary)
885 struct linux_dma_priv *priv;
886 struct dma_pool *pool;
888 priv = dev->dma_priv;
890 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
891 pool->pool_device = dev;
892 pool->pool_entry_size = size;
894 if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
895 align, boundary, /* alignment, boundary */
896 priv->dma_mask, /* lowaddr */
897 BUS_SPACE_MAXADDR, /* highaddr */
898 NULL, NULL, /* filtfunc, filtfuncarg */
903 NULL, NULL, /* lockfunc, lockfuncarg */
909 pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
910 dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
911 dma_pool_obj_release, pool, 0);
913 mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
914 pctrie_init(&pool->pool_ptree);
920 linux_dma_pool_destroy(struct dma_pool *pool)
923 uma_zdestroy(pool->pool_zone);
924 bus_dma_tag_destroy(pool->pool_dmat);
925 mtx_destroy(&pool->pool_lock);
930 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
933 struct linux_dma_obj *obj;
935 obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags);
940 if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
941 DMA_POOL_UNLOCK(pool);
942 uma_zfree_arg(pool->pool_zone, obj, pool);
945 DMA_POOL_UNLOCK(pool);
947 *handle = obj->dma_addr;
952 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
954 struct linux_dma_obj *obj;
957 obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
959 DMA_POOL_UNLOCK(pool);
962 LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
963 DMA_POOL_UNLOCK(pool);
965 uma_zfree_arg(pool->pool_zone, obj, pool);
969 linux_backlight_get_status(device_t dev, struct backlight_props *props)
971 struct pci_dev *pdev;
973 linux_set_current(curthread);
974 pdev = device_get_softc(dev);
976 props->brightness = pdev->dev.bd->props.brightness;
977 props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
984 linux_backlight_get_info(device_t dev, struct backlight_info *info)
986 struct pci_dev *pdev;
988 linux_set_current(curthread);
989 pdev = device_get_softc(dev);
991 info->type = BACKLIGHT_TYPE_PANEL;
992 strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
997 linux_backlight_update_status(device_t dev, struct backlight_props *props)
999 struct pci_dev *pdev;
1001 linux_set_current(curthread);
1002 pdev = device_get_softc(dev);
1004 pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness *
1005 props->brightness / 100;
1006 return (pdev->dev.bd->ops->update_status(pdev->dev.bd));
1009 struct backlight_device *
1010 linux_backlight_device_register(const char *name, struct device *dev,
1011 void *data, const struct backlight_ops *ops, struct backlight_properties *props)
1014 dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
1016 dev->bd->props.type = props->type;
1017 dev->bd->props.max_brightness = props->max_brightness;
1018 dev->bd->props.brightness = props->brightness;
1019 dev->bd->props.power = props->power;
1020 dev->bd->data = data;
1022 dev->bd->name = strdup(name, M_DEVBUF);
1024 dev->backlight_dev = backlight_register(name, dev->bsddev);
1030 linux_backlight_device_unregister(struct backlight_device *bd)
1033 backlight_destroy(bd->dev->backlight_dev);
1034 free(bd->name, M_DEVBUF);