xref: /freebsd/sys/compat/linuxkpi/common/src/linux_pci.c (revision 7a1bef40f1e9bb4d3d56331e743dec79c49a8fc4)
1 /*-
2  * Copyright (c) 2015-2016 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  * Copyright (c) 2020-2022 The FreeBSD Foundation
5  *
6  * Portions of this software were developed by Björn Zeeb
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/filio.h>
45 #include <sys/pciio.h>
46 #include <sys/pctrie.h>
47 #include <sys/rwlock.h>
48 
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 
52 #include <machine/stdarg.h>
53 
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pci_private.h>
56 #include <dev/pci/pci_iov.h>
57 #include <dev/backlight/backlight.h>
58 
59 #include <linux/kernel.h>
60 #include <linux/kobject.h>
61 #include <linux/device.h>
62 #include <linux/slab.h>
63 #include <linux/module.h>
64 #include <linux/cdev.h>
65 #include <linux/file.h>
66 #include <linux/sysfs.h>
67 #include <linux/mm.h>
68 #include <linux/io.h>
69 #include <linux/vmalloc.h>
70 #include <linux/pci.h>
71 #include <linux/compat.h>
72 
73 #include <linux/backlight.h>
74 
75 #include "backlight_if.h"
76 #include "pcib_if.h"
77 
78 /* Undef the linux function macro defined in linux/pci.h */
79 #undef pci_get_class
80 
81 extern int linuxkpi_debug;
82 
83 SYSCTL_DECL(_compat_linuxkpi);
84 
85 static counter_u64_t lkpi_pci_nseg1_fail;
86 SYSCTL_COUNTER_U64(_compat_linuxkpi, OID_AUTO, lkpi_pci_nseg1_fail, CTLFLAG_RD,
87     &lkpi_pci_nseg1_fail, "Count of busdma mapping failures of single-segment");
88 
89 static device_probe_t linux_pci_probe;
90 static device_attach_t linux_pci_attach;
91 static device_detach_t linux_pci_detach;
92 static device_suspend_t linux_pci_suspend;
93 static device_resume_t linux_pci_resume;
94 static device_shutdown_t linux_pci_shutdown;
95 static pci_iov_init_t linux_pci_iov_init;
96 static pci_iov_uninit_t linux_pci_iov_uninit;
97 static pci_iov_add_vf_t linux_pci_iov_add_vf;
98 static int linux_backlight_get_status(device_t dev, struct backlight_props *props);
99 static int linux_backlight_update_status(device_t dev, struct backlight_props *props);
100 static int linux_backlight_get_info(device_t dev, struct backlight_info *info);
101 
102 static device_method_t pci_methods[] = {
103 	DEVMETHOD(device_probe, linux_pci_probe),
104 	DEVMETHOD(device_attach, linux_pci_attach),
105 	DEVMETHOD(device_detach, linux_pci_detach),
106 	DEVMETHOD(device_suspend, linux_pci_suspend),
107 	DEVMETHOD(device_resume, linux_pci_resume),
108 	DEVMETHOD(device_shutdown, linux_pci_shutdown),
109 	DEVMETHOD(pci_iov_init, linux_pci_iov_init),
110 	DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit),
111 	DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf),
112 
113 	/* backlight interface */
114 	DEVMETHOD(backlight_update_status, linux_backlight_update_status),
115 	DEVMETHOD(backlight_get_status, linux_backlight_get_status),
116 	DEVMETHOD(backlight_get_info, linux_backlight_get_info),
117 	DEVMETHOD_END
118 };
119 
120 struct linux_dma_priv {
121 	uint64_t	dma_mask;
122 	bus_dma_tag_t	dmat;
123 	uint64_t	dma_coherent_mask;
124 	bus_dma_tag_t	dmat_coherent;
125 	struct mtx	lock;
126 	struct pctrie	ptree;
127 };
128 #define	DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
129 #define	DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
130 
131 static int
132 linux_pdev_dma_uninit(struct pci_dev *pdev)
133 {
134 	struct linux_dma_priv *priv;
135 
136 	priv = pdev->dev.dma_priv;
137 	if (priv->dmat)
138 		bus_dma_tag_destroy(priv->dmat);
139 	if (priv->dmat_coherent)
140 		bus_dma_tag_destroy(priv->dmat_coherent);
141 	mtx_destroy(&priv->lock);
142 	pdev->dev.dma_priv = NULL;
143 	free(priv, M_DEVBUF);
144 	return (0);
145 }
146 
147 static int
148 linux_pdev_dma_init(struct pci_dev *pdev)
149 {
150 	struct linux_dma_priv *priv;
151 	int error;
152 
153 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
154 
155 	mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
156 	pctrie_init(&priv->ptree);
157 
158 	pdev->dev.dma_priv = priv;
159 
160 	/* Create a default DMA tags. */
161 	error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
162 	if (error != 0)
163 		goto err;
164 	/* Coherent is lower 32bit only by default in Linux. */
165 	error = linux_dma_tag_init_coherent(&pdev->dev, DMA_BIT_MASK(32));
166 	if (error != 0)
167 		goto err;
168 
169 	return (error);
170 
171 err:
172 	linux_pdev_dma_uninit(pdev);
173 	return (error);
174 }
175 
176 int
177 linux_dma_tag_init(struct device *dev, u64 dma_mask)
178 {
179 	struct linux_dma_priv *priv;
180 	int error;
181 
182 	priv = dev->dma_priv;
183 
184 	if (priv->dmat) {
185 		if (priv->dma_mask == dma_mask)
186 			return (0);
187 
188 		bus_dma_tag_destroy(priv->dmat);
189 	}
190 
191 	priv->dma_mask = dma_mask;
192 
193 	error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
194 	    1, 0,			/* alignment, boundary */
195 	    dma_mask,			/* lowaddr */
196 	    BUS_SPACE_MAXADDR,		/* highaddr */
197 	    NULL, NULL,			/* filtfunc, filtfuncarg */
198 	    BUS_SPACE_MAXSIZE,		/* maxsize */
199 	    1,				/* nsegments */
200 	    BUS_SPACE_MAXSIZE,		/* maxsegsz */
201 	    0,				/* flags */
202 	    NULL, NULL,			/* lockfunc, lockfuncarg */
203 	    &priv->dmat);
204 	return (-error);
205 }
206 
207 int
208 linux_dma_tag_init_coherent(struct device *dev, u64 dma_mask)
209 {
210 	struct linux_dma_priv *priv;
211 	int error;
212 
213 	priv = dev->dma_priv;
214 
215 	if (priv->dmat_coherent) {
216 		if (priv->dma_coherent_mask == dma_mask)
217 			return (0);
218 
219 		bus_dma_tag_destroy(priv->dmat_coherent);
220 	}
221 
222 	priv->dma_coherent_mask = dma_mask;
223 
224 	error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
225 	    1, 0,			/* alignment, boundary */
226 	    dma_mask,			/* lowaddr */
227 	    BUS_SPACE_MAXADDR,		/* highaddr */
228 	    NULL, NULL,			/* filtfunc, filtfuncarg */
229 	    BUS_SPACE_MAXSIZE,		/* maxsize */
230 	    1,				/* nsegments */
231 	    BUS_SPACE_MAXSIZE,		/* maxsegsz */
232 	    0,				/* flags */
233 	    NULL, NULL,			/* lockfunc, lockfuncarg */
234 	    &priv->dmat_coherent);
235 	return (-error);
236 }
237 
238 static struct pci_driver *
239 linux_pci_find(device_t dev, const struct pci_device_id **idp)
240 {
241 	const struct pci_device_id *id;
242 	struct pci_driver *pdrv;
243 	uint16_t vendor;
244 	uint16_t device;
245 	uint16_t subvendor;
246 	uint16_t subdevice;
247 
248 	vendor = pci_get_vendor(dev);
249 	device = pci_get_device(dev);
250 	subvendor = pci_get_subvendor(dev);
251 	subdevice = pci_get_subdevice(dev);
252 
253 	spin_lock(&pci_lock);
254 	list_for_each_entry(pdrv, &pci_drivers, node) {
255 		for (id = pdrv->id_table; id->vendor != 0; id++) {
256 			if (vendor == id->vendor &&
257 			    (PCI_ANY_ID == id->device || device == id->device) &&
258 			    (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
259 			    (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
260 				*idp = id;
261 				spin_unlock(&pci_lock);
262 				return (pdrv);
263 			}
264 		}
265 	}
266 	spin_unlock(&pci_lock);
267 	return (NULL);
268 }
269 
270 static void
271 lkpi_pci_dev_release(struct device *dev)
272 {
273 
274 	lkpi_devres_release_free_list(dev);
275 	spin_lock_destroy(&dev->devres_lock);
276 }
277 
278 static void
279 lkpifill_pci_dev(device_t dev, struct pci_dev *pdev)
280 {
281 
282 	pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
283 	pdev->vendor = pci_get_vendor(dev);
284 	pdev->device = pci_get_device(dev);
285 	pdev->subsystem_vendor = pci_get_subvendor(dev);
286 	pdev->subsystem_device = pci_get_subdevice(dev);
287 	pdev->class = pci_get_class(dev);
288 	pdev->revision = pci_get_revid(dev);
289 	pdev->bus = malloc(sizeof(*pdev->bus), M_DEVBUF, M_WAITOK | M_ZERO);
290 	/*
291 	 * This should be the upstream bridge; pci_upstream_bridge()
292 	 * handles that case on demand as otherwise we'll shadow the
293 	 * entire PCI hierarchy.
294 	 */
295 	pdev->bus->self = pdev;
296 	pdev->bus->number = pci_get_bus(dev);
297 	pdev->bus->domain = pci_get_domain(dev);
298 	pdev->dev.bsddev = dev;
299 	pdev->dev.parent = &linux_root_device;
300 	pdev->dev.release = lkpi_pci_dev_release;
301 	INIT_LIST_HEAD(&pdev->dev.irqents);
302 	kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
303 	kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
304 	kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
305 	    kobject_name(&pdev->dev.kobj));
306 	spin_lock_init(&pdev->dev.devres_lock);
307 	INIT_LIST_HEAD(&pdev->dev.devres_head);
308 }
309 
310 static void
311 lkpinew_pci_dev_release(struct device *dev)
312 {
313 	struct pci_dev *pdev;
314 
315 	pdev = to_pci_dev(dev);
316 	if (pdev->root != NULL)
317 		pci_dev_put(pdev->root);
318 	if (pdev->bus->self != pdev)
319 		pci_dev_put(pdev->bus->self);
320 	free(pdev->bus, M_DEVBUF);
321 	free(pdev, M_DEVBUF);
322 }
323 
324 struct pci_dev *
325 lkpinew_pci_dev(device_t dev)
326 {
327 	struct pci_dev *pdev;
328 
329 	pdev = malloc(sizeof(*pdev), M_DEVBUF, M_WAITOK|M_ZERO);
330 	lkpifill_pci_dev(dev, pdev);
331 	pdev->dev.release = lkpinew_pci_dev_release;
332 
333 	return (pdev);
334 }
335 
336 struct pci_dev *
337 lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
338 {
339 	device_t dev;
340 	device_t devfrom = NULL;
341 	struct pci_dev *pdev;
342 
343 	if (from != NULL)
344 		devfrom = from->dev.bsddev;
345 
346 	dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
347 	if (dev == NULL)
348 		return (NULL);
349 
350 	pdev = lkpinew_pci_dev(dev);
351 	return (pdev);
352 }
353 
354 struct pci_dev *
355 lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,
356     unsigned int devfn)
357 {
358 	device_t dev;
359 	struct pci_dev *pdev;
360 
361 	dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
362 	if (dev == NULL)
363 		return (NULL);
364 
365 	pdev = lkpinew_pci_dev(dev);
366 	return (pdev);
367 }
368 
369 static int
370 linux_pci_probe(device_t dev)
371 {
372 	const struct pci_device_id *id;
373 	struct pci_driver *pdrv;
374 
375 	if ((pdrv = linux_pci_find(dev, &id)) == NULL)
376 		return (ENXIO);
377 	if (device_get_driver(dev) != &pdrv->bsddriver)
378 		return (ENXIO);
379 	device_set_desc(dev, pdrv->name);
380 
381 	/* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */
382 	if (pdrv->bsd_probe_return == 0)
383 		return (BUS_PROBE_DEFAULT);
384 	else
385 		return (pdrv->bsd_probe_return);
386 }
387 
388 static int
389 linux_pci_attach(device_t dev)
390 {
391 	const struct pci_device_id *id;
392 	struct pci_driver *pdrv;
393 	struct pci_dev *pdev;
394 
395 	pdrv = linux_pci_find(dev, &id);
396 	pdev = device_get_softc(dev);
397 
398 	MPASS(pdrv != NULL);
399 	MPASS(pdev != NULL);
400 
401 	return (linux_pci_attach_device(dev, pdrv, id, pdev));
402 }
403 
404 int
405 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
406     const struct pci_device_id *id, struct pci_dev *pdev)
407 {
408 	struct resource_list_entry *rle;
409 	device_t parent;
410 	uintptr_t rid;
411 	int error;
412 	bool isdrm;
413 
414 	linux_set_current(curthread);
415 
416 	parent = device_get_parent(dev);
417 	isdrm = pdrv != NULL && pdrv->isdrm;
418 
419 	if (isdrm) {
420 		struct pci_devinfo *dinfo;
421 
422 		dinfo = device_get_ivars(parent);
423 		device_set_ivars(dev, dinfo);
424 	}
425 
426 	lkpifill_pci_dev(dev, pdev);
427 	if (isdrm)
428 		PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);
429 	else
430 		PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);
431 	pdev->devfn = rid;
432 	pdev->pdrv = pdrv;
433 	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);
434 	if (rle != NULL)
435 		pdev->dev.irq = rle->start;
436 	else
437 		pdev->dev.irq = LINUX_IRQ_INVALID;
438 	pdev->irq = pdev->dev.irq;
439 	error = linux_pdev_dma_init(pdev);
440 	if (error)
441 		goto out_dma_init;
442 
443 	TAILQ_INIT(&pdev->mmio);
444 
445 	spin_lock(&pci_lock);
446 	list_add(&pdev->links, &pci_devices);
447 	spin_unlock(&pci_lock);
448 
449 	if (pdrv != NULL) {
450 		error = pdrv->probe(pdev, id);
451 		if (error)
452 			goto out_probe;
453 	}
454 	return (0);
455 
456 out_probe:
457 	free(pdev->bus, M_DEVBUF);
458 	linux_pdev_dma_uninit(pdev);
459 out_dma_init:
460 	spin_lock(&pci_lock);
461 	list_del(&pdev->links);
462 	spin_unlock(&pci_lock);
463 	put_device(&pdev->dev);
464 	return (-error);
465 }
466 
467 static int
468 linux_pci_detach(device_t dev)
469 {
470 	struct pci_dev *pdev;
471 
472 	pdev = device_get_softc(dev);
473 
474 	MPASS(pdev != NULL);
475 
476 	device_set_desc(dev, NULL);
477 
478 	return (linux_pci_detach_device(pdev));
479 }
480 
481 int
482 linux_pci_detach_device(struct pci_dev *pdev)
483 {
484 
485 	linux_set_current(curthread);
486 
487 	if (pdev->pdrv != NULL)
488 		pdev->pdrv->remove(pdev);
489 
490 	if (pdev->root != NULL)
491 		pci_dev_put(pdev->root);
492 	free(pdev->bus, M_DEVBUF);
493 	linux_pdev_dma_uninit(pdev);
494 
495 	spin_lock(&pci_lock);
496 	list_del(&pdev->links);
497 	spin_unlock(&pci_lock);
498 	put_device(&pdev->dev);
499 
500 	return (0);
501 }
502 
503 static int
504 lkpi_pci_disable_dev(struct device *dev)
505 {
506 
507 	(void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);
508 	(void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);
509 	return (0);
510 }
511 
512 void
513 lkpi_pci_devres_release(struct device *dev, void *p)
514 {
515 	struct pci_devres *dr;
516 	struct pci_dev *pdev;
517 	int bar;
518 
519 	pdev = to_pci_dev(dev);
520 	dr = p;
521 
522 	if (pdev->msix_enabled)
523 		lkpi_pci_disable_msix(pdev);
524         if (pdev->msi_enabled)
525 		lkpi_pci_disable_msi(pdev);
526 
527 	if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)
528 		dr->enable_io = false;
529 
530 	if (dr->region_mask == 0)
531 		return;
532 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
533 
534 		if ((dr->region_mask & (1 << bar)) == 0)
535 			continue;
536 		pci_release_region(pdev, bar);
537 	}
538 }
539 
540 void
541 lkpi_pcim_iomap_table_release(struct device *dev, void *p)
542 {
543 	struct pcim_iomap_devres *dr;
544 	struct pci_dev *pdev;
545 	int bar;
546 
547 	dr = p;
548 	pdev = to_pci_dev(dev);
549 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
550 
551 		if (dr->mmio_table[bar] == NULL)
552 			continue;
553 
554 		pci_iounmap(pdev, dr->mmio_table[bar]);
555 	}
556 }
557 
558 static int
559 linux_pci_suspend(device_t dev)
560 {
561 	const struct dev_pm_ops *pmops;
562 	struct pm_message pm = { };
563 	struct pci_dev *pdev;
564 	int error;
565 
566 	error = 0;
567 	linux_set_current(curthread);
568 	pdev = device_get_softc(dev);
569 	pmops = pdev->pdrv->driver.pm;
570 
571 	if (pdev->pdrv->suspend != NULL)
572 		error = -pdev->pdrv->suspend(pdev, pm);
573 	else if (pmops != NULL && pmops->suspend != NULL) {
574 		error = -pmops->suspend(&pdev->dev);
575 		if (error == 0 && pmops->suspend_late != NULL)
576 			error = -pmops->suspend_late(&pdev->dev);
577 	}
578 	return (error);
579 }
580 
581 static int
582 linux_pci_resume(device_t dev)
583 {
584 	const struct dev_pm_ops *pmops;
585 	struct pci_dev *pdev;
586 	int error;
587 
588 	error = 0;
589 	linux_set_current(curthread);
590 	pdev = device_get_softc(dev);
591 	pmops = pdev->pdrv->driver.pm;
592 
593 	if (pdev->pdrv->resume != NULL)
594 		error = -pdev->pdrv->resume(pdev);
595 	else if (pmops != NULL && pmops->resume != NULL) {
596 		if (pmops->resume_early != NULL)
597 			error = -pmops->resume_early(&pdev->dev);
598 		if (error == 0 && pmops->resume != NULL)
599 			error = -pmops->resume(&pdev->dev);
600 	}
601 	return (error);
602 }
603 
604 static int
605 linux_pci_shutdown(device_t dev)
606 {
607 	struct pci_dev *pdev;
608 
609 	linux_set_current(curthread);
610 	pdev = device_get_softc(dev);
611 	if (pdev->pdrv->shutdown != NULL)
612 		pdev->pdrv->shutdown(pdev);
613 	return (0);
614 }
615 
616 static int
617 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
618 {
619 	struct pci_dev *pdev;
620 	int error;
621 
622 	linux_set_current(curthread);
623 	pdev = device_get_softc(dev);
624 	if (pdev->pdrv->bsd_iov_init != NULL)
625 		error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);
626 	else
627 		error = EINVAL;
628 	return (error);
629 }
630 
631 static void
632 linux_pci_iov_uninit(device_t dev)
633 {
634 	struct pci_dev *pdev;
635 
636 	linux_set_current(curthread);
637 	pdev = device_get_softc(dev);
638 	if (pdev->pdrv->bsd_iov_uninit != NULL)
639 		pdev->pdrv->bsd_iov_uninit(dev);
640 }
641 
642 static int
643 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
644 {
645 	struct pci_dev *pdev;
646 	int error;
647 
648 	linux_set_current(curthread);
649 	pdev = device_get_softc(dev);
650 	if (pdev->pdrv->bsd_iov_add_vf != NULL)
651 		error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);
652 	else
653 		error = EINVAL;
654 	return (error);
655 }
656 
657 static int
658 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
659 {
660 	int error;
661 
662 	linux_set_current(curthread);
663 	spin_lock(&pci_lock);
664 	list_add(&pdrv->node, &pci_drivers);
665 	spin_unlock(&pci_lock);
666 	if (pdrv->bsddriver.name == NULL)
667 		pdrv->bsddriver.name = pdrv->name;
668 	pdrv->bsddriver.methods = pci_methods;
669 	pdrv->bsddriver.size = sizeof(struct pci_dev);
670 
671 	bus_topo_lock();
672 	error = devclass_add_driver(dc, &pdrv->bsddriver,
673 	    BUS_PASS_DEFAULT, &pdrv->bsdclass);
674 	bus_topo_unlock();
675 	return (-error);
676 }
677 
678 int
679 linux_pci_register_driver(struct pci_driver *pdrv)
680 {
681 	devclass_t dc;
682 
683 	dc = devclass_find("pci");
684 	if (dc == NULL)
685 		return (-ENXIO);
686 	pdrv->isdrm = false;
687 	return (_linux_pci_register_driver(pdrv, dc));
688 }
689 
690 struct resource_list_entry *
691 linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,
692     int type, int rid)
693 {
694 	device_t dev;
695 	struct resource *res;
696 
697 	KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,
698 	    ("trying to reserve non-BAR type %d", type));
699 
700 	dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ?
701 	    device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
702 	res = pci_reserve_map(device_get_parent(dev), dev, type, &rid, 0, ~0,
703 	    1, 1, 0);
704 	if (res == NULL)
705 		return (NULL);
706 	return (resource_list_find(rl, type, rid));
707 }
708 
709 unsigned long
710 pci_resource_start(struct pci_dev *pdev, int bar)
711 {
712 	struct resource_list_entry *rle;
713 	rman_res_t newstart;
714 	device_t dev;
715 	int error;
716 
717 	if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
718 		return (0);
719 	dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ?
720 	    device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
721 	error = bus_translate_resource(dev, rle->type, rle->start, &newstart);
722 	if (error != 0) {
723 		device_printf(pdev->dev.bsddev,
724 		    "translate of %#jx failed: %d\n",
725 		    (uintmax_t)rle->start, error);
726 		return (0);
727 	}
728 	return (newstart);
729 }
730 
731 unsigned long
732 pci_resource_len(struct pci_dev *pdev, int bar)
733 {
734 	struct resource_list_entry *rle;
735 
736 	if ((rle = linux_pci_get_bar(pdev, bar, true)) == NULL)
737 		return (0);
738 	return (rle->count);
739 }
740 
741 int
742 linux_pci_register_drm_driver(struct pci_driver *pdrv)
743 {
744 	devclass_t dc;
745 
746 	dc = devclass_create("vgapci");
747 	if (dc == NULL)
748 		return (-ENXIO);
749 	pdrv->isdrm = true;
750 	pdrv->name = "drmn";
751 	return (_linux_pci_register_driver(pdrv, dc));
752 }
753 
754 void
755 linux_pci_unregister_driver(struct pci_driver *pdrv)
756 {
757 	devclass_t bus;
758 
759 	bus = devclass_find("pci");
760 
761 	spin_lock(&pci_lock);
762 	list_del(&pdrv->node);
763 	spin_unlock(&pci_lock);
764 	bus_topo_lock();
765 	if (bus != NULL)
766 		devclass_delete_driver(bus, &pdrv->bsddriver);
767 	bus_topo_unlock();
768 }
769 
770 void
771 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
772 {
773 	devclass_t bus;
774 
775 	bus = devclass_find("vgapci");
776 
777 	spin_lock(&pci_lock);
778 	list_del(&pdrv->node);
779 	spin_unlock(&pci_lock);
780 	bus_topo_lock();
781 	if (bus != NULL)
782 		devclass_delete_driver(bus, &pdrv->bsddriver);
783 	bus_topo_unlock();
784 }
785 
786 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
787 
788 struct linux_dma_obj {
789 	void		*vaddr;
790 	uint64_t	dma_addr;
791 	bus_dmamap_t	dmamap;
792 	bus_dma_tag_t	dmat;
793 };
794 
795 static uma_zone_t linux_dma_trie_zone;
796 static uma_zone_t linux_dma_obj_zone;
797 
798 static void
799 linux_dma_init(void *arg)
800 {
801 
802 	linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
803 	    pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
804 	    UMA_ALIGN_PTR, 0);
805 	linux_dma_obj_zone = uma_zcreate("linux_dma_object",
806 	    sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
807 	    UMA_ALIGN_PTR, 0);
808 	lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);
809 }
810 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
811 
812 static void
813 linux_dma_uninit(void *arg)
814 {
815 
816 	counter_u64_free(lkpi_pci_nseg1_fail);
817 	uma_zdestroy(linux_dma_obj_zone);
818 	uma_zdestroy(linux_dma_trie_zone);
819 }
820 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
821 
822 static void *
823 linux_dma_trie_alloc(struct pctrie *ptree)
824 {
825 
826 	return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
827 }
828 
829 static void
830 linux_dma_trie_free(struct pctrie *ptree, void *node)
831 {
832 
833 	uma_zfree(linux_dma_trie_zone, node);
834 }
835 
836 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
837     linux_dma_trie_free);
838 
839 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
840 static dma_addr_t
841 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,
842     bus_dma_tag_t dmat)
843 {
844 	struct linux_dma_priv *priv;
845 	struct linux_dma_obj *obj;
846 	int error, nseg;
847 	bus_dma_segment_t seg;
848 
849 	priv = dev->dma_priv;
850 
851 	/*
852 	 * If the resultant mapping will be entirely 1:1 with the
853 	 * physical address, short-circuit the remainder of the
854 	 * bus_dma API.  This avoids tracking collisions in the pctrie
855 	 * with the additional benefit of reducing overhead.
856 	 */
857 	if (bus_dma_id_mapped(dmat, phys, len))
858 		return (phys);
859 
860 	obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
861 	if (obj == NULL) {
862 		return (0);
863 	}
864 	obj->dmat = dmat;
865 
866 	DMA_PRIV_LOCK(priv);
867 	if (bus_dmamap_create(obj->dmat, 0, &obj->dmamap) != 0) {
868 		DMA_PRIV_UNLOCK(priv);
869 		uma_zfree(linux_dma_obj_zone, obj);
870 		return (0);
871 	}
872 
873 	nseg = -1;
874 	if (_bus_dmamap_load_phys(obj->dmat, obj->dmamap, phys, len,
875 	    BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
876 		bus_dmamap_destroy(obj->dmat, obj->dmamap);
877 		DMA_PRIV_UNLOCK(priv);
878 		uma_zfree(linux_dma_obj_zone, obj);
879 		counter_u64_add(lkpi_pci_nseg1_fail, 1);
880 		if (linuxkpi_debug)
881 			dump_stack();
882 		return (0);
883 	}
884 
885 	KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
886 	obj->dma_addr = seg.ds_addr;
887 
888 	error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
889 	if (error != 0) {
890 		bus_dmamap_unload(obj->dmat, obj->dmamap);
891 		bus_dmamap_destroy(obj->dmat, obj->dmamap);
892 		DMA_PRIV_UNLOCK(priv);
893 		uma_zfree(linux_dma_obj_zone, obj);
894 		return (0);
895 	}
896 	DMA_PRIV_UNLOCK(priv);
897 	return (obj->dma_addr);
898 }
899 #else
900 static dma_addr_t
901 linux_dma_map_phys_common(struct device *dev __unused, vm_paddr_t phys,
902     size_t len __unused, bus_dma_tag_t dmat __unused)
903 {
904 	return (phys);
905 }
906 #endif
907 
908 dma_addr_t
909 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
910 {
911 	struct linux_dma_priv *priv;
912 
913 	priv = dev->dma_priv;
914 	return (linux_dma_map_phys_common(dev, phys, len, priv->dmat));
915 }
916 
917 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
918 void
919 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
920 {
921 	struct linux_dma_priv *priv;
922 	struct linux_dma_obj *obj;
923 
924 	priv = dev->dma_priv;
925 
926 	if (pctrie_is_empty(&priv->ptree))
927 		return;
928 
929 	DMA_PRIV_LOCK(priv);
930 	obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
931 	if (obj == NULL) {
932 		DMA_PRIV_UNLOCK(priv);
933 		return;
934 	}
935 	LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
936 	bus_dmamap_unload(obj->dmat, obj->dmamap);
937 	bus_dmamap_destroy(obj->dmat, obj->dmamap);
938 	DMA_PRIV_UNLOCK(priv);
939 
940 	uma_zfree(linux_dma_obj_zone, obj);
941 }
942 #else
943 void
944 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
945 {
946 }
947 #endif
948 
949 void *
950 linux_dma_alloc_coherent(struct device *dev, size_t size,
951     dma_addr_t *dma_handle, gfp_t flag)
952 {
953 	struct linux_dma_priv *priv;
954 	vm_paddr_t high;
955 	size_t align;
956 	void *mem;
957 
958 	if (dev == NULL || dev->dma_priv == NULL) {
959 		*dma_handle = 0;
960 		return (NULL);
961 	}
962 	priv = dev->dma_priv;
963 	if (priv->dma_coherent_mask)
964 		high = priv->dma_coherent_mask;
965 	else
966 		/* Coherent is lower 32bit only by default in Linux. */
967 		high = BUS_SPACE_MAXADDR_32BIT;
968 	align = PAGE_SIZE << get_order(size);
969 	/* Always zero the allocation. */
970 	flag |= M_ZERO;
971 	mem = (void *)kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,
972 	    align, 0, VM_MEMATTR_DEFAULT);
973 	if (mem != NULL) {
974 		*dma_handle = linux_dma_map_phys_common(dev, vtophys(mem), size,
975 		    priv->dmat_coherent);
976 		if (*dma_handle == 0) {
977 			kmem_free((vm_offset_t)mem, size);
978 			mem = NULL;
979 		}
980 	} else {
981 		*dma_handle = 0;
982 	}
983 	return (mem);
984 }
985 
986 void
987 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,
988     bus_dmasync_op_t op)
989 {
990 	struct linux_dma_priv *priv;
991 	struct linux_dma_obj *obj;
992 
993 	priv = dev->dma_priv;
994 
995 	if (pctrie_is_empty(&priv->ptree))
996 		return;
997 
998 	DMA_PRIV_LOCK(priv);
999 	obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1000 	if (obj == NULL) {
1001 		DMA_PRIV_UNLOCK(priv);
1002 		return;
1003 	}
1004 
1005 	bus_dmamap_sync(obj->dmat, obj->dmamap, op);
1006 	DMA_PRIV_UNLOCK(priv);
1007 }
1008 
1009 int
1010 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
1011     enum dma_data_direction direction, unsigned long attrs __unused)
1012 {
1013 	struct linux_dma_priv *priv;
1014 	struct scatterlist *sg;
1015 	int i, nseg;
1016 	bus_dma_segment_t seg;
1017 
1018 	priv = dev->dma_priv;
1019 
1020 	DMA_PRIV_LOCK(priv);
1021 
1022 	/* create common DMA map in the first S/G entry */
1023 	if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
1024 		DMA_PRIV_UNLOCK(priv);
1025 		return (0);
1026 	}
1027 
1028 	/* load all S/G list entries */
1029 	for_each_sg(sgl, sg, nents, i) {
1030 		nseg = -1;
1031 		if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
1032 		    sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
1033 		    &seg, &nseg) != 0) {
1034 			bus_dmamap_unload(priv->dmat, sgl->dma_map);
1035 			bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1036 			DMA_PRIV_UNLOCK(priv);
1037 			return (0);
1038 		}
1039 		KASSERT(nseg == 0,
1040 		    ("More than one segment (nseg=%d)", nseg + 1));
1041 
1042 		sg_dma_address(sg) = seg.ds_addr;
1043 	}
1044 
1045 	switch (direction) {
1046 	case DMA_BIDIRECTIONAL:
1047 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1048 		break;
1049 	case DMA_TO_DEVICE:
1050 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1051 		break;
1052 	case DMA_FROM_DEVICE:
1053 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1054 		break;
1055 	default:
1056 		break;
1057 	}
1058 
1059 	DMA_PRIV_UNLOCK(priv);
1060 
1061 	return (nents);
1062 }
1063 
1064 void
1065 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
1066     int nents __unused, enum dma_data_direction direction,
1067     unsigned long attrs __unused)
1068 {
1069 	struct linux_dma_priv *priv;
1070 
1071 	priv = dev->dma_priv;
1072 
1073 	DMA_PRIV_LOCK(priv);
1074 
1075 	switch (direction) {
1076 	case DMA_BIDIRECTIONAL:
1077 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1078 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1079 		break;
1080 	case DMA_TO_DEVICE:
1081 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);
1082 		break;
1083 	case DMA_FROM_DEVICE:
1084 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1085 		break;
1086 	default:
1087 		break;
1088 	}
1089 
1090 	bus_dmamap_unload(priv->dmat, sgl->dma_map);
1091 	bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1092 	DMA_PRIV_UNLOCK(priv);
1093 }
1094 
1095 struct dma_pool {
1096 	struct device  *pool_device;
1097 	uma_zone_t	pool_zone;
1098 	struct mtx	pool_lock;
1099 	bus_dma_tag_t	pool_dmat;
1100 	size_t		pool_entry_size;
1101 	struct pctrie	pool_ptree;
1102 };
1103 
1104 #define	DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
1105 #define	DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
1106 
1107 static inline int
1108 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
1109 {
1110 	struct linux_dma_obj *obj = mem;
1111 	struct dma_pool *pool = arg;
1112 	int error, nseg;
1113 	bus_dma_segment_t seg;
1114 
1115 	nseg = -1;
1116 	DMA_POOL_LOCK(pool);
1117 	error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
1118 	    vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
1119 	    &seg, &nseg);
1120 	DMA_POOL_UNLOCK(pool);
1121 	if (error != 0) {
1122 		return (error);
1123 	}
1124 	KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1125 	obj->dma_addr = seg.ds_addr;
1126 
1127 	return (0);
1128 }
1129 
1130 static void
1131 dma_pool_obj_dtor(void *mem, int size, void *arg)
1132 {
1133 	struct linux_dma_obj *obj = mem;
1134 	struct dma_pool *pool = arg;
1135 
1136 	DMA_POOL_LOCK(pool);
1137 	bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
1138 	DMA_POOL_UNLOCK(pool);
1139 }
1140 
1141 static int
1142 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
1143     int flags)
1144 {
1145 	struct dma_pool *pool = arg;
1146 	struct linux_dma_obj *obj;
1147 	int error, i;
1148 
1149 	for (i = 0; i < count; i++) {
1150 		obj = uma_zalloc(linux_dma_obj_zone, flags);
1151 		if (obj == NULL)
1152 			break;
1153 
1154 		error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
1155 		    BUS_DMA_NOWAIT, &obj->dmamap);
1156 		if (error!= 0) {
1157 			uma_zfree(linux_dma_obj_zone, obj);
1158 			break;
1159 		}
1160 
1161 		store[i] = obj;
1162 	}
1163 
1164 	return (i);
1165 }
1166 
1167 static void
1168 dma_pool_obj_release(void *arg, void **store, int count)
1169 {
1170 	struct dma_pool *pool = arg;
1171 	struct linux_dma_obj *obj;
1172 	int i;
1173 
1174 	for (i = 0; i < count; i++) {
1175 		obj = store[i];
1176 		bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
1177 		uma_zfree(linux_dma_obj_zone, obj);
1178 	}
1179 }
1180 
1181 struct dma_pool *
1182 linux_dma_pool_create(char *name, struct device *dev, size_t size,
1183     size_t align, size_t boundary)
1184 {
1185 	struct linux_dma_priv *priv;
1186 	struct dma_pool *pool;
1187 
1188 	priv = dev->dma_priv;
1189 
1190 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1191 	pool->pool_device = dev;
1192 	pool->pool_entry_size = size;
1193 
1194 	if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
1195 	    align, boundary,		/* alignment, boundary */
1196 	    priv->dma_mask,		/* lowaddr */
1197 	    BUS_SPACE_MAXADDR,		/* highaddr */
1198 	    NULL, NULL,			/* filtfunc, filtfuncarg */
1199 	    size,			/* maxsize */
1200 	    1,				/* nsegments */
1201 	    size,			/* maxsegsz */
1202 	    0,				/* flags */
1203 	    NULL, NULL,			/* lockfunc, lockfuncarg */
1204 	    &pool->pool_dmat)) {
1205 		kfree(pool);
1206 		return (NULL);
1207 	}
1208 
1209 	pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
1210 	    dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
1211 	    dma_pool_obj_release, pool, 0);
1212 
1213 	mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
1214 	pctrie_init(&pool->pool_ptree);
1215 
1216 	return (pool);
1217 }
1218 
1219 void
1220 linux_dma_pool_destroy(struct dma_pool *pool)
1221 {
1222 
1223 	uma_zdestroy(pool->pool_zone);
1224 	bus_dma_tag_destroy(pool->pool_dmat);
1225 	mtx_destroy(&pool->pool_lock);
1226 	kfree(pool);
1227 }
1228 
1229 void
1230 lkpi_dmam_pool_destroy(struct device *dev, void *p)
1231 {
1232 	struct dma_pool *pool;
1233 
1234 	pool = *(struct dma_pool **)p;
1235 	LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);
1236 	linux_dma_pool_destroy(pool);
1237 }
1238 
1239 void *
1240 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
1241     dma_addr_t *handle)
1242 {
1243 	struct linux_dma_obj *obj;
1244 
1245 	obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);
1246 	if (obj == NULL)
1247 		return (NULL);
1248 
1249 	DMA_POOL_LOCK(pool);
1250 	if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
1251 		DMA_POOL_UNLOCK(pool);
1252 		uma_zfree_arg(pool->pool_zone, obj, pool);
1253 		return (NULL);
1254 	}
1255 	DMA_POOL_UNLOCK(pool);
1256 
1257 	*handle = obj->dma_addr;
1258 	return (obj->vaddr);
1259 }
1260 
1261 void
1262 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
1263 {
1264 	struct linux_dma_obj *obj;
1265 
1266 	DMA_POOL_LOCK(pool);
1267 	obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
1268 	if (obj == NULL) {
1269 		DMA_POOL_UNLOCK(pool);
1270 		return;
1271 	}
1272 	LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
1273 	DMA_POOL_UNLOCK(pool);
1274 
1275 	uma_zfree_arg(pool->pool_zone, obj, pool);
1276 }
1277 
1278 static int
1279 linux_backlight_get_status(device_t dev, struct backlight_props *props)
1280 {
1281 	struct pci_dev *pdev;
1282 
1283 	linux_set_current(curthread);
1284 	pdev = device_get_softc(dev);
1285 
1286 	props->brightness = pdev->dev.bd->props.brightness;
1287 	props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
1288 	props->nlevels = 0;
1289 
1290 	return (0);
1291 }
1292 
1293 static int
1294 linux_backlight_get_info(device_t dev, struct backlight_info *info)
1295 {
1296 	struct pci_dev *pdev;
1297 
1298 	linux_set_current(curthread);
1299 	pdev = device_get_softc(dev);
1300 
1301 	info->type = BACKLIGHT_TYPE_PANEL;
1302 	strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
1303 	return (0);
1304 }
1305 
1306 static int
1307 linux_backlight_update_status(device_t dev, struct backlight_props *props)
1308 {
1309 	struct pci_dev *pdev;
1310 
1311 	linux_set_current(curthread);
1312 	pdev = device_get_softc(dev);
1313 
1314 	pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness *
1315 		props->brightness / 100;
1316 	pdev->dev.bd->props.power = props->brightness == 0 ?
1317 		4/* FB_BLANK_POWERDOWN */ : 0/* FB_BLANK_UNBLANK */;
1318 	return (pdev->dev.bd->ops->update_status(pdev->dev.bd));
1319 }
1320 
1321 struct backlight_device *
1322 linux_backlight_device_register(const char *name, struct device *dev,
1323     void *data, const struct backlight_ops *ops, struct backlight_properties *props)
1324 {
1325 
1326 	dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
1327 	dev->bd->ops = ops;
1328 	dev->bd->props.type = props->type;
1329 	dev->bd->props.max_brightness = props->max_brightness;
1330 	dev->bd->props.brightness = props->brightness;
1331 	dev->bd->props.power = props->power;
1332 	dev->bd->data = data;
1333 	dev->bd->dev = dev;
1334 	dev->bd->name = strdup(name, M_DEVBUF);
1335 
1336 	dev->backlight_dev = backlight_register(name, dev->bsddev);
1337 
1338 	return (dev->bd);
1339 }
1340 
1341 void
1342 linux_backlight_device_unregister(struct backlight_device *bd)
1343 {
1344 
1345 	backlight_destroy(bd->dev->backlight_dev);
1346 	free(bd->name, M_DEVBUF);
1347 	free(bd, M_DEVBUF);
1348 }
1349