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