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 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/fcntl.h>
41 #include <sys/file.h>
42 #include <sys/filio.h>
43 #include <sys/pciio.h>
44 #include <sys/pctrie.h>
45 #include <sys/rman.h>
46 #include <sys/rwlock.h>
47 
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <machine/stdarg.h>
54 
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pci_private.h>
57 #include <dev/pci/pci_iov.h>
58 #include <dev/backlight/backlight.h>
59 
60 #include <linux/kernel.h>
61 #include <linux/kobject.h>
62 #include <linux/device.h>
63 #include <linux/slab.h>
64 #include <linux/module.h>
65 #include <linux/cdev.h>
66 #include <linux/file.h>
67 #include <linux/sysfs.h>
68 #include <linux/mm.h>
69 #include <linux/io.h>
70 #include <linux/vmalloc.h>
71 #include <linux/pci.h>
72 #include <linux/compat.h>
73 
74 #include <linux/backlight.h>
75 
76 #include "backlight_if.h"
77 #include "pcib_if.h"
78 
79 /* Undef the linux function macro defined in linux/pci.h */
80 #undef pci_get_class
81 
82 extern int linuxkpi_debug;
83 
84 SYSCTL_DECL(_compat_linuxkpi);
85 
86 static counter_u64_t lkpi_pci_nseg1_fail;
87 SYSCTL_COUNTER_U64(_compat_linuxkpi, OID_AUTO, lkpi_pci_nseg1_fail, CTLFLAG_RD,
88     &lkpi_pci_nseg1_fail, "Count of busdma mapping failures of single-segment");
89 
90 static device_probe_t linux_pci_probe;
91 static device_attach_t linux_pci_attach;
92 static device_detach_t linux_pci_detach;
93 static device_suspend_t linux_pci_suspend;
94 static device_resume_t linux_pci_resume;
95 static device_shutdown_t linux_pci_shutdown;
96 static pci_iov_init_t linux_pci_iov_init;
97 static pci_iov_uninit_t linux_pci_iov_uninit;
98 static pci_iov_add_vf_t linux_pci_iov_add_vf;
99 static int linux_backlight_get_status(device_t dev, struct backlight_props *props);
100 static int linux_backlight_update_status(device_t dev, struct backlight_props *props);
101 static int linux_backlight_get_info(device_t dev, struct backlight_info *info);
102 static void lkpi_pcim_iomap_table_release(struct device *, void *);
103 
104 static device_method_t pci_methods[] = {
105 	DEVMETHOD(device_probe, linux_pci_probe),
106 	DEVMETHOD(device_attach, linux_pci_attach),
107 	DEVMETHOD(device_detach, linux_pci_detach),
108 	DEVMETHOD(device_suspend, linux_pci_suspend),
109 	DEVMETHOD(device_resume, linux_pci_resume),
110 	DEVMETHOD(device_shutdown, linux_pci_shutdown),
111 	DEVMETHOD(pci_iov_init, linux_pci_iov_init),
112 	DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit),
113 	DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf),
114 
115 	/* backlight interface */
116 	DEVMETHOD(backlight_update_status, linux_backlight_update_status),
117 	DEVMETHOD(backlight_get_status, linux_backlight_get_status),
118 	DEVMETHOD(backlight_get_info, linux_backlight_get_info),
119 	DEVMETHOD_END
120 };
121 
122 const char *pci_power_names[] = {
123 	"UNKNOWN", "D0", "D1", "D2", "D3hot", "D3cold"
124 };
125 
126 /* We need some meta-struct to keep track of these for devres. */
127 struct pci_devres {
128 	bool		enable_io;
129 	/* PCIR_MAX_BAR_0 + 1 = 6 => BIT(0..5). */
130 	uint8_t		region_mask;
131 	struct resource	*region_table[PCIR_MAX_BAR_0 + 1]; /* Not needed. */
132 };
133 struct pcim_iomap_devres {
134 	void		*mmio_table[PCIR_MAX_BAR_0 + 1];
135 	struct resource	*res_table[PCIR_MAX_BAR_0 + 1];
136 };
137 
138 struct linux_dma_priv {
139 	uint64_t	dma_mask;
140 	bus_dma_tag_t	dmat;
141 	uint64_t	dma_coherent_mask;
142 	bus_dma_tag_t	dmat_coherent;
143 	struct mtx	lock;
144 	struct pctrie	ptree;
145 };
146 #define	DMA_PRIV_LOCK(priv) mtx_lock(&(priv)->lock)
147 #define	DMA_PRIV_UNLOCK(priv) mtx_unlock(&(priv)->lock)
148 
149 static int
150 linux_pdev_dma_uninit(struct pci_dev *pdev)
151 {
152 	struct linux_dma_priv *priv;
153 
154 	priv = pdev->dev.dma_priv;
155 	if (priv->dmat)
156 		bus_dma_tag_destroy(priv->dmat);
157 	if (priv->dmat_coherent)
158 		bus_dma_tag_destroy(priv->dmat_coherent);
159 	mtx_destroy(&priv->lock);
160 	pdev->dev.dma_priv = NULL;
161 	free(priv, M_DEVBUF);
162 	return (0);
163 }
164 
165 static int
166 linux_pdev_dma_init(struct pci_dev *pdev)
167 {
168 	struct linux_dma_priv *priv;
169 	int error;
170 
171 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
172 
173 	mtx_init(&priv->lock, "lkpi-priv-dma", NULL, MTX_DEF);
174 	pctrie_init(&priv->ptree);
175 
176 	pdev->dev.dma_priv = priv;
177 
178 	/* Create a default DMA tags. */
179 	error = linux_dma_tag_init(&pdev->dev, DMA_BIT_MASK(64));
180 	if (error != 0)
181 		goto err;
182 	/* Coherent is lower 32bit only by default in Linux. */
183 	error = linux_dma_tag_init_coherent(&pdev->dev, DMA_BIT_MASK(32));
184 	if (error != 0)
185 		goto err;
186 
187 	return (error);
188 
189 err:
190 	linux_pdev_dma_uninit(pdev);
191 	return (error);
192 }
193 
194 int
195 linux_dma_tag_init(struct device *dev, u64 dma_mask)
196 {
197 	struct linux_dma_priv *priv;
198 	int error;
199 
200 	priv = dev->dma_priv;
201 
202 	if (priv->dmat) {
203 		if (priv->dma_mask == dma_mask)
204 			return (0);
205 
206 		bus_dma_tag_destroy(priv->dmat);
207 	}
208 
209 	priv->dma_mask = dma_mask;
210 
211 	error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
212 	    1, 0,			/* alignment, boundary */
213 	    dma_mask,			/* lowaddr */
214 	    BUS_SPACE_MAXADDR,		/* highaddr */
215 	    NULL, NULL,			/* filtfunc, filtfuncarg */
216 	    BUS_SPACE_MAXSIZE,		/* maxsize */
217 	    1,				/* nsegments */
218 	    BUS_SPACE_MAXSIZE,		/* maxsegsz */
219 	    0,				/* flags */
220 	    NULL, NULL,			/* lockfunc, lockfuncarg */
221 	    &priv->dmat);
222 	return (-error);
223 }
224 
225 int
226 linux_dma_tag_init_coherent(struct device *dev, u64 dma_mask)
227 {
228 	struct linux_dma_priv *priv;
229 	int error;
230 
231 	priv = dev->dma_priv;
232 
233 	if (priv->dmat_coherent) {
234 		if (priv->dma_coherent_mask == dma_mask)
235 			return (0);
236 
237 		bus_dma_tag_destroy(priv->dmat_coherent);
238 	}
239 
240 	priv->dma_coherent_mask = dma_mask;
241 
242 	error = bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
243 	    1, 0,			/* alignment, boundary */
244 	    dma_mask,			/* lowaddr */
245 	    BUS_SPACE_MAXADDR,		/* highaddr */
246 	    NULL, NULL,			/* filtfunc, filtfuncarg */
247 	    BUS_SPACE_MAXSIZE,		/* maxsize */
248 	    1,				/* nsegments */
249 	    BUS_SPACE_MAXSIZE,		/* maxsegsz */
250 	    0,				/* flags */
251 	    NULL, NULL,			/* lockfunc, lockfuncarg */
252 	    &priv->dmat_coherent);
253 	return (-error);
254 }
255 
256 static struct pci_driver *
257 linux_pci_find(device_t dev, const struct pci_device_id **idp)
258 {
259 	const struct pci_device_id *id;
260 	struct pci_driver *pdrv;
261 	uint16_t vendor;
262 	uint16_t device;
263 	uint16_t subvendor;
264 	uint16_t subdevice;
265 
266 	vendor = pci_get_vendor(dev);
267 	device = pci_get_device(dev);
268 	subvendor = pci_get_subvendor(dev);
269 	subdevice = pci_get_subdevice(dev);
270 
271 	spin_lock(&pci_lock);
272 	list_for_each_entry(pdrv, &pci_drivers, node) {
273 		for (id = pdrv->id_table; id->vendor != 0; id++) {
274 			if (vendor == id->vendor &&
275 			    (PCI_ANY_ID == id->device || device == id->device) &&
276 			    (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
277 			    (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
278 				*idp = id;
279 				spin_unlock(&pci_lock);
280 				return (pdrv);
281 			}
282 		}
283 	}
284 	spin_unlock(&pci_lock);
285 	return (NULL);
286 }
287 
288 struct pci_dev *
289 lkpi_pci_get_device(uint16_t vendor, uint16_t device, struct pci_dev *odev)
290 {
291 	struct pci_dev *pdev;
292 
293 	KASSERT(odev == NULL, ("%s: odev argument not yet supported\n", __func__));
294 
295 	spin_lock(&pci_lock);
296 	list_for_each_entry(pdev, &pci_devices, links) {
297 		if (pdev->vendor == vendor && pdev->device == device)
298 			break;
299 	}
300 	spin_unlock(&pci_lock);
301 
302 	return (pdev);
303 }
304 
305 static void
306 lkpi_pci_dev_release(struct device *dev)
307 {
308 
309 	lkpi_devres_release_free_list(dev);
310 	spin_lock_destroy(&dev->devres_lock);
311 }
312 
313 static void
314 lkpifill_pci_dev(device_t dev, struct pci_dev *pdev)
315 {
316 
317 	pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
318 	pdev->vendor = pci_get_vendor(dev);
319 	pdev->device = pci_get_device(dev);
320 	pdev->subsystem_vendor = pci_get_subvendor(dev);
321 	pdev->subsystem_device = pci_get_subdevice(dev);
322 	pdev->class = pci_get_class(dev);
323 	pdev->revision = pci_get_revid(dev);
324 	pdev->path_name = kasprintf(GFP_KERNEL, "%04d:%02d:%02d.%d",
325 	    pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
326 	    pci_get_function(dev));
327 	pdev->bus = malloc(sizeof(*pdev->bus), M_DEVBUF, M_WAITOK | M_ZERO);
328 	/*
329 	 * This should be the upstream bridge; pci_upstream_bridge()
330 	 * handles that case on demand as otherwise we'll shadow the
331 	 * entire PCI hierarchy.
332 	 */
333 	pdev->bus->self = pdev;
334 	pdev->bus->number = pci_get_bus(dev);
335 	pdev->bus->domain = pci_get_domain(dev);
336 	pdev->dev.bsddev = dev;
337 	pdev->dev.parent = &linux_root_device;
338 	pdev->dev.release = lkpi_pci_dev_release;
339 	INIT_LIST_HEAD(&pdev->dev.irqents);
340 
341 	if (pci_msi_count(dev) > 0)
342 		pdev->msi_desc = malloc(pci_msi_count(dev) *
343 		    sizeof(*pdev->msi_desc), M_DEVBUF, M_WAITOK | M_ZERO);
344 
345 	kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
346 	kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
347 	kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
348 	    kobject_name(&pdev->dev.kobj));
349 	spin_lock_init(&pdev->dev.devres_lock);
350 	INIT_LIST_HEAD(&pdev->dev.devres_head);
351 }
352 
353 static void
354 lkpinew_pci_dev_release(struct device *dev)
355 {
356 	struct pci_dev *pdev;
357 	int i;
358 
359 	pdev = to_pci_dev(dev);
360 	if (pdev->root != NULL)
361 		pci_dev_put(pdev->root);
362 	if (pdev->bus->self != pdev)
363 		pci_dev_put(pdev->bus->self);
364 	free(pdev->bus, M_DEVBUF);
365 	if (pdev->msi_desc != NULL) {
366 		for (i = pci_msi_count(pdev->dev.bsddev) - 1; i >= 0; i--)
367 			free(pdev->msi_desc[i], M_DEVBUF);
368 		free(pdev->msi_desc, M_DEVBUF);
369 	}
370 	kfree(pdev->path_name);
371 	free(pdev, M_DEVBUF);
372 }
373 
374 struct pci_dev *
375 lkpinew_pci_dev(device_t dev)
376 {
377 	struct pci_dev *pdev;
378 
379 	pdev = malloc(sizeof(*pdev), M_DEVBUF, M_WAITOK|M_ZERO);
380 	lkpifill_pci_dev(dev, pdev);
381 	pdev->dev.release = lkpinew_pci_dev_release;
382 
383 	return (pdev);
384 }
385 
386 struct pci_dev *
387 lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
388 {
389 	device_t dev;
390 	device_t devfrom = NULL;
391 	struct pci_dev *pdev;
392 
393 	if (from != NULL)
394 		devfrom = from->dev.bsddev;
395 
396 	dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
397 	if (dev == NULL)
398 		return (NULL);
399 
400 	pdev = lkpinew_pci_dev(dev);
401 	return (pdev);
402 }
403 
404 struct pci_dev *
405 lkpi_pci_get_domain_bus_and_slot(int domain, unsigned int bus,
406     unsigned int devfn)
407 {
408 	device_t dev;
409 	struct pci_dev *pdev;
410 
411 	dev = pci_find_dbsf(domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
412 	if (dev == NULL)
413 		return (NULL);
414 
415 	pdev = lkpinew_pci_dev(dev);
416 	return (pdev);
417 }
418 
419 static int
420 linux_pci_probe(device_t dev)
421 {
422 	const struct pci_device_id *id;
423 	struct pci_driver *pdrv;
424 
425 	if ((pdrv = linux_pci_find(dev, &id)) == NULL)
426 		return (ENXIO);
427 	if (device_get_driver(dev) != &pdrv->bsddriver)
428 		return (ENXIO);
429 	device_set_desc(dev, pdrv->name);
430 
431 	/* Assume BSS initialized (should never return BUS_PROBE_SPECIFIC). */
432 	if (pdrv->bsd_probe_return == 0)
433 		return (BUS_PROBE_DEFAULT);
434 	else
435 		return (pdrv->bsd_probe_return);
436 }
437 
438 static int
439 linux_pci_attach(device_t dev)
440 {
441 	const struct pci_device_id *id;
442 	struct pci_driver *pdrv;
443 	struct pci_dev *pdev;
444 
445 	pdrv = linux_pci_find(dev, &id);
446 	pdev = device_get_softc(dev);
447 
448 	MPASS(pdrv != NULL);
449 	MPASS(pdev != NULL);
450 
451 	return (linux_pci_attach_device(dev, pdrv, id, pdev));
452 }
453 
454 static struct resource_list_entry *
455 linux_pci_reserve_bar(struct pci_dev *pdev, struct resource_list *rl,
456     int type, int rid)
457 {
458 	device_t dev;
459 	struct resource *res;
460 
461 	KASSERT(type == SYS_RES_IOPORT || type == SYS_RES_MEMORY,
462 	    ("trying to reserve non-BAR type %d", type));
463 
464 	dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ?
465 	    device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
466 	res = pci_reserve_map(device_get_parent(dev), dev, type, &rid, 0, ~0,
467 	    1, 1, 0);
468 	if (res == NULL)
469 		return (NULL);
470 	return (resource_list_find(rl, type, rid));
471 }
472 
473 static struct resource_list_entry *
474 linux_pci_get_rle(struct pci_dev *pdev, int type, int rid, bool reserve_bar)
475 {
476 	struct pci_devinfo *dinfo;
477 	struct resource_list *rl;
478 	struct resource_list_entry *rle;
479 
480 	dinfo = device_get_ivars(pdev->dev.bsddev);
481 	rl = &dinfo->resources;
482 	rle = resource_list_find(rl, type, rid);
483 	/* Reserve resources for this BAR if needed. */
484 	if (rle == NULL && reserve_bar)
485 		rle = linux_pci_reserve_bar(pdev, rl, type, rid);
486 	return (rle);
487 }
488 
489 int
490 linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
491     const struct pci_device_id *id, struct pci_dev *pdev)
492 {
493 	struct resource_list_entry *rle;
494 	device_t parent;
495 	uintptr_t rid;
496 	int error;
497 	bool isdrm;
498 
499 	linux_set_current(curthread);
500 
501 	parent = device_get_parent(dev);
502 	isdrm = pdrv != NULL && pdrv->isdrm;
503 
504 	if (isdrm) {
505 		struct pci_devinfo *dinfo;
506 
507 		dinfo = device_get_ivars(parent);
508 		device_set_ivars(dev, dinfo);
509 	}
510 
511 	lkpifill_pci_dev(dev, pdev);
512 	if (isdrm)
513 		PCI_GET_ID(device_get_parent(parent), parent, PCI_ID_RID, &rid);
514 	else
515 		PCI_GET_ID(parent, dev, PCI_ID_RID, &rid);
516 	pdev->devfn = rid;
517 	pdev->pdrv = pdrv;
518 	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0, false);
519 	if (rle != NULL)
520 		pdev->dev.irq = rle->start;
521 	else
522 		pdev->dev.irq = LINUX_IRQ_INVALID;
523 	pdev->irq = pdev->dev.irq;
524 	error = linux_pdev_dma_init(pdev);
525 	if (error)
526 		goto out_dma_init;
527 
528 	TAILQ_INIT(&pdev->mmio);
529 	spin_lock_init(&pdev->pcie_cap_lock);
530 
531 	spin_lock(&pci_lock);
532 	list_add(&pdev->links, &pci_devices);
533 	spin_unlock(&pci_lock);
534 
535 	if (pdrv != NULL) {
536 		error = pdrv->probe(pdev, id);
537 		if (error)
538 			goto out_probe;
539 	}
540 	return (0);
541 
542 out_probe:
543 	free(pdev->bus, M_DEVBUF);
544 	spin_lock_destroy(&pdev->pcie_cap_lock);
545 	linux_pdev_dma_uninit(pdev);
546 out_dma_init:
547 	spin_lock(&pci_lock);
548 	list_del(&pdev->links);
549 	spin_unlock(&pci_lock);
550 	put_device(&pdev->dev);
551 	return (-error);
552 }
553 
554 static int
555 linux_pci_detach(device_t dev)
556 {
557 	struct pci_dev *pdev;
558 
559 	pdev = device_get_softc(dev);
560 
561 	MPASS(pdev != NULL);
562 
563 	device_set_desc(dev, NULL);
564 
565 	return (linux_pci_detach_device(pdev));
566 }
567 
568 int
569 linux_pci_detach_device(struct pci_dev *pdev)
570 {
571 
572 	linux_set_current(curthread);
573 
574 	if (pdev->pdrv != NULL)
575 		pdev->pdrv->remove(pdev);
576 
577 	if (pdev->root != NULL)
578 		pci_dev_put(pdev->root);
579 	free(pdev->bus, M_DEVBUF);
580 	linux_pdev_dma_uninit(pdev);
581 
582 	spin_lock(&pci_lock);
583 	list_del(&pdev->links);
584 	spin_unlock(&pci_lock);
585 	spin_lock_destroy(&pdev->pcie_cap_lock);
586 	put_device(&pdev->dev);
587 
588 	return (0);
589 }
590 
591 static int
592 lkpi_pci_disable_dev(struct device *dev)
593 {
594 
595 	(void) pci_disable_io(dev->bsddev, SYS_RES_MEMORY);
596 	(void) pci_disable_io(dev->bsddev, SYS_RES_IOPORT);
597 	return (0);
598 }
599 
600 static struct pci_devres *
601 lkpi_pci_devres_get_alloc(struct pci_dev *pdev)
602 {
603 	struct pci_devres *dr;
604 
605 	dr = lkpi_devres_find(&pdev->dev, lkpi_pci_devres_release, NULL, NULL);
606 	if (dr == NULL) {
607 		dr = lkpi_devres_alloc(lkpi_pci_devres_release, sizeof(*dr),
608 		    GFP_KERNEL | __GFP_ZERO);
609 		if (dr != NULL)
610 			lkpi_devres_add(&pdev->dev, dr);
611 	}
612 
613 	return (dr);
614 }
615 
616 static struct pci_devres *
617 lkpi_pci_devres_find(struct pci_dev *pdev)
618 {
619 	if (!pdev->managed)
620 		return (NULL);
621 
622 	return (lkpi_pci_devres_get_alloc(pdev));
623 }
624 
625 void
626 lkpi_pci_devres_release(struct device *dev, void *p)
627 {
628 	struct pci_devres *dr;
629 	struct pci_dev *pdev;
630 	int bar;
631 
632 	pdev = to_pci_dev(dev);
633 	dr = p;
634 
635 	if (pdev->msix_enabled)
636 		lkpi_pci_disable_msix(pdev);
637         if (pdev->msi_enabled)
638 		lkpi_pci_disable_msi(pdev);
639 
640 	if (dr->enable_io && lkpi_pci_disable_dev(dev) == 0)
641 		dr->enable_io = false;
642 
643 	if (dr->region_mask == 0)
644 		return;
645 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
646 
647 		if ((dr->region_mask & (1 << bar)) == 0)
648 			continue;
649 		pci_release_region(pdev, bar);
650 	}
651 }
652 
653 int
654 linuxkpi_pcim_enable_device(struct pci_dev *pdev)
655 {
656 	struct pci_devres *dr;
657 	int error;
658 
659 	/* Here we cannot run through the pdev->managed check. */
660 	dr = lkpi_pci_devres_get_alloc(pdev);
661 	if (dr == NULL)
662 		return (-ENOMEM);
663 
664 	/* If resources were enabled before do not do it again. */
665 	if (dr->enable_io)
666 		return (0);
667 
668 	error = pci_enable_device(pdev);
669 	if (error == 0)
670 		dr->enable_io = true;
671 
672 	/* This device is not managed. */
673 	pdev->managed = true;
674 
675 	return (error);
676 }
677 
678 static struct pcim_iomap_devres *
679 lkpi_pcim_iomap_devres_find(struct pci_dev *pdev)
680 {
681 	struct pcim_iomap_devres *dr;
682 
683 	dr = lkpi_devres_find(&pdev->dev, lkpi_pcim_iomap_table_release,
684 	    NULL, NULL);
685 	if (dr == NULL) {
686 		dr = lkpi_devres_alloc(lkpi_pcim_iomap_table_release,
687 		    sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
688 		if (dr != NULL)
689 			lkpi_devres_add(&pdev->dev, dr);
690 	}
691 
692 	if (dr == NULL)
693 		device_printf(pdev->dev.bsddev, "%s: NULL\n", __func__);
694 
695 	return (dr);
696 }
697 
698 void __iomem **
699 linuxkpi_pcim_iomap_table(struct pci_dev *pdev)
700 {
701 	struct pcim_iomap_devres *dr;
702 
703 	dr = lkpi_pcim_iomap_devres_find(pdev);
704 	if (dr == NULL)
705 		return (NULL);
706 
707 	/*
708 	 * If the driver has manually set a flag to be able to request the
709 	 * resource to use bus_read/write_<n>, return the shadow table.
710 	 */
711 	if (pdev->want_iomap_res)
712 		return ((void **)dr->res_table);
713 
714 	/* This is the Linux default. */
715 	return (dr->mmio_table);
716 }
717 
718 static struct resource *
719 _lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size __unused)
720 {
721 	struct pci_mmio_region *mmio, *p;
722 	int type;
723 
724 	type = pci_resource_type(pdev, bar);
725 	if (type < 0) {
726 		device_printf(pdev->dev.bsddev, "%s: bar %d type %d\n",
727 		     __func__, bar, type);
728 		return (NULL);
729 	}
730 
731 	/*
732 	 * Check for duplicate mappings.
733 	 * This can happen if a driver calls pci_request_region() first.
734 	 */
735 	TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
736 		if (mmio->type == type && mmio->rid == PCIR_BAR(bar)) {
737 			return (mmio->res);
738 		}
739 	}
740 
741 	mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
742 	mmio->rid = PCIR_BAR(bar);
743 	mmio->type = type;
744 	mmio->res = bus_alloc_resource_any(pdev->dev.bsddev, mmio->type,
745 	    &mmio->rid, RF_ACTIVE|RF_SHAREABLE);
746 	if (mmio->res == NULL) {
747 		device_printf(pdev->dev.bsddev, "%s: failed to alloc "
748 		    "bar %d type %d rid %d\n",
749 		    __func__, bar, type, PCIR_BAR(bar));
750 		free(mmio, M_DEVBUF);
751 		return (NULL);
752 	}
753 	TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
754 
755 	return (mmio->res);
756 }
757 
758 void *
759 linuxkpi_pci_iomap(struct pci_dev *pdev, int mmio_bar, int mmio_size)
760 {
761 	struct resource *res;
762 
763 	res = _lkpi_pci_iomap(pdev, mmio_bar, mmio_size);
764 	if (res == NULL)
765 		return (NULL);
766 	/* This is a FreeBSD extension so we can use bus_*(). */
767 	if (pdev->want_iomap_res)
768 		return (res);
769 	return ((void *)rman_get_bushandle(res));
770 }
771 
772 void
773 linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res)
774 {
775 	struct pci_mmio_region *mmio, *p;
776 
777 	TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
778 		if (res != (void *)rman_get_bushandle(mmio->res))
779 			continue;
780 		bus_release_resource(pdev->dev.bsddev,
781 		    mmio->type, mmio->rid, mmio->res);
782 		TAILQ_REMOVE(&pdev->mmio, mmio, next);
783 		free(mmio, M_DEVBUF);
784 		return;
785 	}
786 }
787 
788 int
789 linuxkpi_pcim_iomap_regions(struct pci_dev *pdev, uint32_t mask, const char *name)
790 {
791 	struct pcim_iomap_devres *dr;
792 	void *res;
793 	uint32_t mappings;
794 	int bar;
795 
796 	dr = lkpi_pcim_iomap_devres_find(pdev);
797 	if (dr == NULL)
798 		return (-ENOMEM);
799 
800 	/* Now iomap all the requested (by "mask") ones. */
801 	for (bar = mappings = 0; mappings != mask; bar++) {
802 		if ((mask & (1 << bar)) == 0)
803 			continue;
804 
805 		/* Request double is not allowed. */
806 		if (dr->mmio_table[bar] != NULL) {
807 			device_printf(pdev->dev.bsddev, "%s: bar %d %p\n",
808 			    __func__, bar, dr->mmio_table[bar]);
809 			goto err;
810 		}
811 
812 		res = _lkpi_pci_iomap(pdev, bar, 0);
813 		if (res == NULL)
814 			goto err;
815 		dr->mmio_table[bar] = (void *)rman_get_bushandle(res);
816 		dr->res_table[bar] = res;
817 
818 		mappings |= (1 << bar);
819 	}
820 
821 	return (0);
822 err:
823 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
824 		if ((mappings & (1 << bar)) != 0) {
825 			res = dr->mmio_table[bar];
826 			if (res == NULL)
827 				continue;
828 			pci_iounmap(pdev, res);
829 		}
830 	}
831 
832 	return (-EINVAL);
833 }
834 
835 static void
836 lkpi_pcim_iomap_table_release(struct device *dev, void *p)
837 {
838 	struct pcim_iomap_devres *dr;
839 	struct pci_dev *pdev;
840 	int bar;
841 
842 	dr = p;
843 	pdev = to_pci_dev(dev);
844 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
845 
846 		if (dr->mmio_table[bar] == NULL)
847 			continue;
848 
849 		pci_iounmap(pdev, dr->mmio_table[bar]);
850 	}
851 }
852 
853 static int
854 linux_pci_suspend(device_t dev)
855 {
856 	const struct dev_pm_ops *pmops;
857 	struct pm_message pm = { };
858 	struct pci_dev *pdev;
859 	int error;
860 
861 	error = 0;
862 	linux_set_current(curthread);
863 	pdev = device_get_softc(dev);
864 	pmops = pdev->pdrv->driver.pm;
865 
866 	if (pdev->pdrv->suspend != NULL)
867 		error = -pdev->pdrv->suspend(pdev, pm);
868 	else if (pmops != NULL && pmops->suspend != NULL) {
869 		error = -pmops->suspend(&pdev->dev);
870 		if (error == 0 && pmops->suspend_late != NULL)
871 			error = -pmops->suspend_late(&pdev->dev);
872 	}
873 	return (error);
874 }
875 
876 static int
877 linux_pci_resume(device_t dev)
878 {
879 	const struct dev_pm_ops *pmops;
880 	struct pci_dev *pdev;
881 	int error;
882 
883 	error = 0;
884 	linux_set_current(curthread);
885 	pdev = device_get_softc(dev);
886 	pmops = pdev->pdrv->driver.pm;
887 
888 	if (pdev->pdrv->resume != NULL)
889 		error = -pdev->pdrv->resume(pdev);
890 	else if (pmops != NULL && pmops->resume != NULL) {
891 		if (pmops->resume_early != NULL)
892 			error = -pmops->resume_early(&pdev->dev);
893 		if (error == 0 && pmops->resume != NULL)
894 			error = -pmops->resume(&pdev->dev);
895 	}
896 	return (error);
897 }
898 
899 static int
900 linux_pci_shutdown(device_t dev)
901 {
902 	struct pci_dev *pdev;
903 
904 	linux_set_current(curthread);
905 	pdev = device_get_softc(dev);
906 	if (pdev->pdrv->shutdown != NULL)
907 		pdev->pdrv->shutdown(pdev);
908 	return (0);
909 }
910 
911 static int
912 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config)
913 {
914 	struct pci_dev *pdev;
915 	int error;
916 
917 	linux_set_current(curthread);
918 	pdev = device_get_softc(dev);
919 	if (pdev->pdrv->bsd_iov_init != NULL)
920 		error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config);
921 	else
922 		error = EINVAL;
923 	return (error);
924 }
925 
926 static void
927 linux_pci_iov_uninit(device_t dev)
928 {
929 	struct pci_dev *pdev;
930 
931 	linux_set_current(curthread);
932 	pdev = device_get_softc(dev);
933 	if (pdev->pdrv->bsd_iov_uninit != NULL)
934 		pdev->pdrv->bsd_iov_uninit(dev);
935 }
936 
937 static int
938 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config)
939 {
940 	struct pci_dev *pdev;
941 	int error;
942 
943 	linux_set_current(curthread);
944 	pdev = device_get_softc(dev);
945 	if (pdev->pdrv->bsd_iov_add_vf != NULL)
946 		error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config);
947 	else
948 		error = EINVAL;
949 	return (error);
950 }
951 
952 static int
953 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
954 {
955 	int error;
956 
957 	linux_set_current(curthread);
958 	spin_lock(&pci_lock);
959 	list_add(&pdrv->node, &pci_drivers);
960 	spin_unlock(&pci_lock);
961 	if (pdrv->bsddriver.name == NULL)
962 		pdrv->bsddriver.name = pdrv->name;
963 	pdrv->bsddriver.methods = pci_methods;
964 	pdrv->bsddriver.size = sizeof(struct pci_dev);
965 
966 	bus_topo_lock();
967 	error = devclass_add_driver(dc, &pdrv->bsddriver,
968 	    BUS_PASS_DEFAULT, &pdrv->bsdclass);
969 	bus_topo_unlock();
970 	return (-error);
971 }
972 
973 int
974 linux_pci_register_driver(struct pci_driver *pdrv)
975 {
976 	devclass_t dc;
977 
978 	dc = devclass_find("pci");
979 	if (dc == NULL)
980 		return (-ENXIO);
981 	pdrv->isdrm = false;
982 	return (_linux_pci_register_driver(pdrv, dc));
983 }
984 
985 static struct resource_list_entry *
986 lkpi_pci_get_bar(struct pci_dev *pdev, int bar, bool reserve)
987 {
988 	int type;
989 
990 	type = pci_resource_type(pdev, bar);
991 	if (type < 0)
992 		return (NULL);
993 	bar = PCIR_BAR(bar);
994 	return (linux_pci_get_rle(pdev, type, bar, reserve));
995 }
996 
997 struct device *
998 lkpi_pci_find_irq_dev(unsigned int irq)
999 {
1000 	struct pci_dev *pdev;
1001 	struct device *found;
1002 
1003 	found = NULL;
1004 	spin_lock(&pci_lock);
1005 	list_for_each_entry(pdev, &pci_devices, links) {
1006 		if (irq == pdev->dev.irq ||
1007 		    (irq >= pdev->dev.irq_start && irq < pdev->dev.irq_end)) {
1008 			found = &pdev->dev;
1009 			break;
1010 		}
1011 	}
1012 	spin_unlock(&pci_lock);
1013 	return (found);
1014 }
1015 
1016 unsigned long
1017 pci_resource_start(struct pci_dev *pdev, int bar)
1018 {
1019 	struct resource_list_entry *rle;
1020 	rman_res_t newstart;
1021 	device_t dev;
1022 	int error;
1023 
1024 	if ((rle = lkpi_pci_get_bar(pdev, bar, true)) == NULL)
1025 		return (0);
1026 	dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ?
1027 	    device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev;
1028 	error = bus_translate_resource(dev, rle->type, rle->start, &newstart);
1029 	if (error != 0) {
1030 		device_printf(pdev->dev.bsddev,
1031 		    "translate of %#jx failed: %d\n",
1032 		    (uintmax_t)rle->start, error);
1033 		return (0);
1034 	}
1035 	return (newstart);
1036 }
1037 
1038 unsigned long
1039 pci_resource_len(struct pci_dev *pdev, int bar)
1040 {
1041 	struct resource_list_entry *rle;
1042 
1043 	if ((rle = lkpi_pci_get_bar(pdev, bar, true)) == NULL)
1044 		return (0);
1045 	return (rle->count);
1046 }
1047 
1048 int
1049 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
1050 {
1051 	struct resource *res;
1052 	struct pci_devres *dr;
1053 	struct pci_mmio_region *mmio;
1054 	int rid;
1055 	int type;
1056 
1057 	type = pci_resource_type(pdev, bar);
1058 	if (type < 0)
1059 		return (-ENODEV);
1060 	rid = PCIR_BAR(bar);
1061 	res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
1062 	    RF_ACTIVE|RF_SHAREABLE);
1063 	if (res == NULL) {
1064 		device_printf(pdev->dev.bsddev, "%s: failed to alloc "
1065 		    "bar %d type %d rid %d\n",
1066 		    __func__, bar, type, PCIR_BAR(bar));
1067 		return (-ENODEV);
1068 	}
1069 
1070 	/*
1071 	 * It seems there is an implicit devres tracking on these if the device
1072 	 * is managed; otherwise the resources are not automatiaclly freed on
1073 	 * FreeBSD/LinuxKPI tough they should be/are expected to be by Linux
1074 	 * drivers.
1075 	 */
1076 	dr = lkpi_pci_devres_find(pdev);
1077 	if (dr != NULL) {
1078 		dr->region_mask |= (1 << bar);
1079 		dr->region_table[bar] = res;
1080 	}
1081 
1082 	/* Even if the device is not managed we need to track it for iomap. */
1083 	mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
1084 	mmio->rid = PCIR_BAR(bar);
1085 	mmio->type = type;
1086 	mmio->res = res;
1087 	TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next);
1088 
1089 	return (0);
1090 }
1091 
1092 int
1093 linuxkpi_pci_request_regions(struct pci_dev *pdev, const char *res_name)
1094 {
1095 	int error;
1096 	int i;
1097 
1098 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
1099 		error = pci_request_region(pdev, i, res_name);
1100 		if (error && error != -ENODEV) {
1101 			pci_release_regions(pdev);
1102 			return (error);
1103 		}
1104 	}
1105 	return (0);
1106 }
1107 
1108 void
1109 linuxkpi_pci_release_region(struct pci_dev *pdev, int bar)
1110 {
1111 	struct resource_list_entry *rle;
1112 	struct pci_devres *dr;
1113 	struct pci_mmio_region *mmio, *p;
1114 
1115 	if ((rle = lkpi_pci_get_bar(pdev, bar, false)) == NULL)
1116 		return;
1117 
1118 	/*
1119 	 * As we implicitly track the requests we also need to clear them on
1120 	 * release.  Do clear before resource release.
1121 	 */
1122 	dr = lkpi_pci_devres_find(pdev);
1123 	if (dr != NULL) {
1124 		KASSERT(dr->region_table[bar] == rle->res, ("%s: pdev %p bar %d"
1125 		    " region_table res %p != rel->res %p\n", __func__, pdev,
1126 		    bar, dr->region_table[bar], rle->res));
1127 		dr->region_table[bar] = NULL;
1128 		dr->region_mask &= ~(1 << bar);
1129 	}
1130 
1131 	TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
1132 		if (rle->res != (void *)rman_get_bushandle(mmio->res))
1133 			continue;
1134 		TAILQ_REMOVE(&pdev->mmio, mmio, next);
1135 		free(mmio, M_DEVBUF);
1136 	}
1137 
1138 	bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
1139 }
1140 
1141 void
1142 linuxkpi_pci_release_regions(struct pci_dev *pdev)
1143 {
1144 	int i;
1145 
1146 	for (i = 0; i <= PCIR_MAX_BAR_0; i++)
1147 		pci_release_region(pdev, i);
1148 }
1149 
1150 int
1151 linux_pci_register_drm_driver(struct pci_driver *pdrv)
1152 {
1153 	devclass_t dc;
1154 
1155 	dc = devclass_create("vgapci");
1156 	if (dc == NULL)
1157 		return (-ENXIO);
1158 	pdrv->isdrm = true;
1159 	pdrv->name = "drmn";
1160 	return (_linux_pci_register_driver(pdrv, dc));
1161 }
1162 
1163 void
1164 linux_pci_unregister_driver(struct pci_driver *pdrv)
1165 {
1166 	devclass_t bus;
1167 
1168 	bus = devclass_find("pci");
1169 
1170 	spin_lock(&pci_lock);
1171 	list_del(&pdrv->node);
1172 	spin_unlock(&pci_lock);
1173 	bus_topo_lock();
1174 	if (bus != NULL)
1175 		devclass_delete_driver(bus, &pdrv->bsddriver);
1176 	bus_topo_unlock();
1177 }
1178 
1179 void
1180 linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
1181 {
1182 	devclass_t bus;
1183 
1184 	bus = devclass_find("vgapci");
1185 
1186 	spin_lock(&pci_lock);
1187 	list_del(&pdrv->node);
1188 	spin_unlock(&pci_lock);
1189 	bus_topo_lock();
1190 	if (bus != NULL)
1191 		devclass_delete_driver(bus, &pdrv->bsddriver);
1192 	bus_topo_unlock();
1193 }
1194 
1195 int
1196 linuxkpi_pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries,
1197     int nreq)
1198 {
1199 	struct resource_list_entry *rle;
1200 	int error;
1201 	int avail;
1202 	int i;
1203 
1204 	avail = pci_msix_count(pdev->dev.bsddev);
1205 	if (avail < nreq) {
1206 		if (avail == 0)
1207 			return -EINVAL;
1208 		return avail;
1209 	}
1210 	avail = nreq;
1211 	if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
1212 		return error;
1213 	/*
1214 	* Handle case where "pci_alloc_msix()" may allocate less
1215 	* interrupts than available and return with no error:
1216 	*/
1217 	if (avail < nreq) {
1218 		pci_release_msi(pdev->dev.bsddev);
1219 		return avail;
1220 	}
1221 	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1, false);
1222 	pdev->dev.irq_start = rle->start;
1223 	pdev->dev.irq_end = rle->start + avail;
1224 	for (i = 0; i < nreq; i++)
1225 		entries[i].vector = pdev->dev.irq_start + i;
1226 	pdev->msix_enabled = true;
1227 	return (0);
1228 }
1229 
1230 int
1231 _lkpi_pci_enable_msi_range(struct pci_dev *pdev, int minvec, int maxvec)
1232 {
1233 	struct resource_list_entry *rle;
1234 	int error;
1235 	int nvec;
1236 
1237 	if (maxvec < minvec)
1238 		return (-EINVAL);
1239 
1240 	nvec = pci_msi_count(pdev->dev.bsddev);
1241 	if (nvec < 1 || nvec < minvec)
1242 		return (-ENOSPC);
1243 
1244 	nvec = min(nvec, maxvec);
1245 	if ((error = -pci_alloc_msi(pdev->dev.bsddev, &nvec)) != 0)
1246 		return error;
1247 
1248 	/* Native PCI might only ever ask for 32 vectors. */
1249 	if (nvec < minvec) {
1250 		pci_release_msi(pdev->dev.bsddev);
1251 		return (-ENOSPC);
1252 	}
1253 
1254 	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1, false);
1255 	pdev->dev.irq_start = rle->start;
1256 	pdev->dev.irq_end = rle->start + nvec;
1257 	pdev->irq = rle->start;
1258 	pdev->msi_enabled = true;
1259 	return (0);
1260 }
1261 
1262 int
1263 pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,
1264     unsigned int flags)
1265 {
1266 	int error;
1267 
1268 	if (flags & PCI_IRQ_MSIX) {
1269 		struct msix_entry *entries;
1270 		int i;
1271 
1272 		entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL);
1273 		if (entries == NULL) {
1274 			error = -ENOMEM;
1275 			goto out;
1276 		}
1277 		for (i = 0; i < maxv; ++i)
1278 			entries[i].entry = i;
1279 		error = pci_enable_msix(pdev, entries, maxv);
1280 out:
1281 		kfree(entries);
1282 		if (error == 0 && pdev->msix_enabled)
1283 			return (pdev->dev.irq_end - pdev->dev.irq_start);
1284 	}
1285 	if (flags & PCI_IRQ_MSI) {
1286 		if (pci_msi_count(pdev->dev.bsddev) < minv)
1287 			return (-ENOSPC);
1288 		error = _lkpi_pci_enable_msi_range(pdev, minv, maxv);
1289 		if (error == 0 && pdev->msi_enabled)
1290 			return (pdev->dev.irq_end - pdev->dev.irq_start);
1291 	}
1292 	if (flags & PCI_IRQ_LEGACY) {
1293 		if (pdev->irq)
1294 			return (1);
1295 	}
1296 
1297 	return (-EINVAL);
1298 }
1299 
1300 struct msi_desc *
1301 lkpi_pci_msi_desc_alloc(int irq)
1302 {
1303 	struct device *dev;
1304 	struct pci_dev *pdev;
1305 	struct msi_desc *desc;
1306 	struct pci_devinfo *dinfo;
1307 	struct pcicfg_msi *msi;
1308 	int vec;
1309 
1310 	dev = lkpi_pci_find_irq_dev(irq);
1311 	if (dev == NULL)
1312 		return (NULL);
1313 
1314 	pdev = to_pci_dev(dev);
1315 
1316 	if (pdev->msi_desc == NULL)
1317 		return (NULL);
1318 
1319 	if (irq < pdev->dev.irq_start || irq >= pdev->dev.irq_end)
1320 		return (NULL);
1321 
1322 	vec = pdev->dev.irq_start - irq;
1323 
1324 	if (pdev->msi_desc[vec] != NULL)
1325 		return (pdev->msi_desc[vec]);
1326 
1327 	dinfo = device_get_ivars(dev->bsddev);
1328 	msi = &dinfo->cfg.msi;
1329 
1330 	desc = malloc(sizeof(*desc), M_DEVBUF, M_WAITOK | M_ZERO);
1331 
1332 	desc->pci.msi_attrib.is_64 =
1333 	   (msi->msi_ctrl & PCIM_MSICTRL_64BIT) ? true : false;
1334 	desc->msg.data = msi->msi_data;
1335 
1336 	pdev->msi_desc[vec] = desc;
1337 
1338 	return (desc);
1339 }
1340 
1341 bool
1342 pci_device_is_present(struct pci_dev *pdev)
1343 {
1344 	device_t dev;
1345 
1346 	dev = pdev->dev.bsddev;
1347 
1348 	return (bus_child_present(dev));
1349 }
1350 
1351 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
1352 
1353 struct linux_dma_obj {
1354 	void		*vaddr;
1355 	uint64_t	dma_addr;
1356 	bus_dmamap_t	dmamap;
1357 	bus_dma_tag_t	dmat;
1358 };
1359 
1360 static uma_zone_t linux_dma_trie_zone;
1361 static uma_zone_t linux_dma_obj_zone;
1362 
1363 static void
1364 linux_dma_init(void *arg)
1365 {
1366 
1367 	linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie",
1368 	    pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL,
1369 	    UMA_ALIGN_PTR, 0);
1370 	linux_dma_obj_zone = uma_zcreate("linux_dma_object",
1371 	    sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL,
1372 	    UMA_ALIGN_PTR, 0);
1373 	lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK);
1374 }
1375 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL);
1376 
1377 static void
1378 linux_dma_uninit(void *arg)
1379 {
1380 
1381 	counter_u64_free(lkpi_pci_nseg1_fail);
1382 	uma_zdestroy(linux_dma_obj_zone);
1383 	uma_zdestroy(linux_dma_trie_zone);
1384 }
1385 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL);
1386 
1387 static void *
1388 linux_dma_trie_alloc(struct pctrie *ptree)
1389 {
1390 
1391 	return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT));
1392 }
1393 
1394 static void
1395 linux_dma_trie_free(struct pctrie *ptree, void *node)
1396 {
1397 
1398 	uma_zfree(linux_dma_trie_zone, node);
1399 }
1400 
1401 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc,
1402     linux_dma_trie_free);
1403 
1404 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1405 static dma_addr_t
1406 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len,
1407     bus_dma_tag_t dmat)
1408 {
1409 	struct linux_dma_priv *priv;
1410 	struct linux_dma_obj *obj;
1411 	int error, nseg;
1412 	bus_dma_segment_t seg;
1413 
1414 	priv = dev->dma_priv;
1415 
1416 	/*
1417 	 * If the resultant mapping will be entirely 1:1 with the
1418 	 * physical address, short-circuit the remainder of the
1419 	 * bus_dma API.  This avoids tracking collisions in the pctrie
1420 	 * with the additional benefit of reducing overhead.
1421 	 */
1422 	if (bus_dma_id_mapped(dmat, phys, len))
1423 		return (phys);
1424 
1425 	obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT);
1426 	if (obj == NULL) {
1427 		return (0);
1428 	}
1429 	obj->dmat = dmat;
1430 
1431 	DMA_PRIV_LOCK(priv);
1432 	if (bus_dmamap_create(obj->dmat, 0, &obj->dmamap) != 0) {
1433 		DMA_PRIV_UNLOCK(priv);
1434 		uma_zfree(linux_dma_obj_zone, obj);
1435 		return (0);
1436 	}
1437 
1438 	nseg = -1;
1439 	if (_bus_dmamap_load_phys(obj->dmat, obj->dmamap, phys, len,
1440 	    BUS_DMA_NOWAIT, &seg, &nseg) != 0) {
1441 		bus_dmamap_destroy(obj->dmat, obj->dmamap);
1442 		DMA_PRIV_UNLOCK(priv);
1443 		uma_zfree(linux_dma_obj_zone, obj);
1444 		counter_u64_add(lkpi_pci_nseg1_fail, 1);
1445 		if (linuxkpi_debug)
1446 			dump_stack();
1447 		return (0);
1448 	}
1449 
1450 	KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1451 	obj->dma_addr = seg.ds_addr;
1452 
1453 	error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj);
1454 	if (error != 0) {
1455 		bus_dmamap_unload(obj->dmat, obj->dmamap);
1456 		bus_dmamap_destroy(obj->dmat, obj->dmamap);
1457 		DMA_PRIV_UNLOCK(priv);
1458 		uma_zfree(linux_dma_obj_zone, obj);
1459 		return (0);
1460 	}
1461 	DMA_PRIV_UNLOCK(priv);
1462 	return (obj->dma_addr);
1463 }
1464 #else
1465 static dma_addr_t
1466 linux_dma_map_phys_common(struct device *dev __unused, vm_paddr_t phys,
1467     size_t len __unused, bus_dma_tag_t dmat __unused)
1468 {
1469 	return (phys);
1470 }
1471 #endif
1472 
1473 dma_addr_t
1474 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len)
1475 {
1476 	struct linux_dma_priv *priv;
1477 
1478 	priv = dev->dma_priv;
1479 	return (linux_dma_map_phys_common(dev, phys, len, priv->dmat));
1480 }
1481 
1482 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1483 void
1484 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1485 {
1486 	struct linux_dma_priv *priv;
1487 	struct linux_dma_obj *obj;
1488 
1489 	priv = dev->dma_priv;
1490 
1491 	if (pctrie_is_empty(&priv->ptree))
1492 		return;
1493 
1494 	DMA_PRIV_LOCK(priv);
1495 	obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1496 	if (obj == NULL) {
1497 		DMA_PRIV_UNLOCK(priv);
1498 		return;
1499 	}
1500 	LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr);
1501 	bus_dmamap_unload(obj->dmat, obj->dmamap);
1502 	bus_dmamap_destroy(obj->dmat, obj->dmamap);
1503 	DMA_PRIV_UNLOCK(priv);
1504 
1505 	uma_zfree(linux_dma_obj_zone, obj);
1506 }
1507 #else
1508 void
1509 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len)
1510 {
1511 }
1512 #endif
1513 
1514 void *
1515 linux_dma_alloc_coherent(struct device *dev, size_t size,
1516     dma_addr_t *dma_handle, gfp_t flag)
1517 {
1518 	struct linux_dma_priv *priv;
1519 	vm_paddr_t high;
1520 	size_t align;
1521 	void *mem;
1522 
1523 	if (dev == NULL || dev->dma_priv == NULL) {
1524 		*dma_handle = 0;
1525 		return (NULL);
1526 	}
1527 	priv = dev->dma_priv;
1528 	if (priv->dma_coherent_mask)
1529 		high = priv->dma_coherent_mask;
1530 	else
1531 		/* Coherent is lower 32bit only by default in Linux. */
1532 		high = BUS_SPACE_MAXADDR_32BIT;
1533 	align = PAGE_SIZE << get_order(size);
1534 	/* Always zero the allocation. */
1535 	flag |= M_ZERO;
1536 	mem = kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high,
1537 	    align, 0, VM_MEMATTR_DEFAULT);
1538 	if (mem != NULL) {
1539 		*dma_handle = linux_dma_map_phys_common(dev, vtophys(mem), size,
1540 		    priv->dmat_coherent);
1541 		if (*dma_handle == 0) {
1542 			kmem_free(mem, size);
1543 			mem = NULL;
1544 		}
1545 	} else {
1546 		*dma_handle = 0;
1547 	}
1548 	return (mem);
1549 }
1550 
1551 struct lkpi_devres_dmam_coherent {
1552 	size_t size;
1553 	dma_addr_t *handle;
1554 	void *mem;
1555 };
1556 
1557 static void
1558 lkpi_dmam_free_coherent(struct device *dev, void *p)
1559 {
1560 	struct lkpi_devres_dmam_coherent *dr;
1561 
1562 	dr = p;
1563 	dma_free_coherent(dev, dr->size, dr->mem, *dr->handle);
1564 }
1565 
1566 void *
1567 linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1568     gfp_t flag)
1569 {
1570 	struct lkpi_devres_dmam_coherent *dr;
1571 
1572 	dr = lkpi_devres_alloc(lkpi_dmam_free_coherent,
1573 	    sizeof(*dr), GFP_KERNEL | __GFP_ZERO);
1574 
1575 	if (dr == NULL)
1576 		return (NULL);
1577 
1578 	dr->size = size;
1579 	dr->mem = linux_dma_alloc_coherent(dev, size, dma_handle, flag);
1580 	dr->handle = dma_handle;
1581 	if (dr->mem == NULL) {
1582 		lkpi_devres_free(dr);
1583 		return (NULL);
1584 	}
1585 
1586 	lkpi_devres_add(dev, dr);
1587 	return (dr->mem);
1588 }
1589 
1590 void
1591 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size,
1592     bus_dmasync_op_t op)
1593 {
1594 	struct linux_dma_priv *priv;
1595 	struct linux_dma_obj *obj;
1596 
1597 	priv = dev->dma_priv;
1598 
1599 	if (pctrie_is_empty(&priv->ptree))
1600 		return;
1601 
1602 	DMA_PRIV_LOCK(priv);
1603 	obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr);
1604 	if (obj == NULL) {
1605 		DMA_PRIV_UNLOCK(priv);
1606 		return;
1607 	}
1608 
1609 	bus_dmamap_sync(obj->dmat, obj->dmamap, op);
1610 	DMA_PRIV_UNLOCK(priv);
1611 }
1612 
1613 int
1614 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
1615     enum dma_data_direction direction, unsigned long attrs __unused)
1616 {
1617 	struct linux_dma_priv *priv;
1618 	struct scatterlist *sg;
1619 	int i, nseg;
1620 	bus_dma_segment_t seg;
1621 
1622 	priv = dev->dma_priv;
1623 
1624 	DMA_PRIV_LOCK(priv);
1625 
1626 	/* create common DMA map in the first S/G entry */
1627 	if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) {
1628 		DMA_PRIV_UNLOCK(priv);
1629 		return (0);
1630 	}
1631 
1632 	/* load all S/G list entries */
1633 	for_each_sg(sgl, sg, nents, i) {
1634 		nseg = -1;
1635 		if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map,
1636 		    sg_phys(sg), sg->length, BUS_DMA_NOWAIT,
1637 		    &seg, &nseg) != 0) {
1638 			bus_dmamap_unload(priv->dmat, sgl->dma_map);
1639 			bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1640 			DMA_PRIV_UNLOCK(priv);
1641 			return (0);
1642 		}
1643 		KASSERT(nseg == 0,
1644 		    ("More than one segment (nseg=%d)", nseg + 1));
1645 
1646 		sg_dma_address(sg) = seg.ds_addr;
1647 	}
1648 
1649 	switch (direction) {
1650 	case DMA_BIDIRECTIONAL:
1651 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1652 		break;
1653 	case DMA_TO_DEVICE:
1654 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1655 		break;
1656 	case DMA_FROM_DEVICE:
1657 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE);
1658 		break;
1659 	default:
1660 		break;
1661 	}
1662 
1663 	DMA_PRIV_UNLOCK(priv);
1664 
1665 	return (nents);
1666 }
1667 
1668 void
1669 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
1670     int nents __unused, enum dma_data_direction direction,
1671     unsigned long attrs __unused)
1672 {
1673 	struct linux_dma_priv *priv;
1674 
1675 	priv = dev->dma_priv;
1676 
1677 	DMA_PRIV_LOCK(priv);
1678 
1679 	switch (direction) {
1680 	case DMA_BIDIRECTIONAL:
1681 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1682 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD);
1683 		break;
1684 	case DMA_TO_DEVICE:
1685 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE);
1686 		break;
1687 	case DMA_FROM_DEVICE:
1688 		bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD);
1689 		break;
1690 	default:
1691 		break;
1692 	}
1693 
1694 	bus_dmamap_unload(priv->dmat, sgl->dma_map);
1695 	bus_dmamap_destroy(priv->dmat, sgl->dma_map);
1696 	DMA_PRIV_UNLOCK(priv);
1697 }
1698 
1699 struct dma_pool {
1700 	struct device  *pool_device;
1701 	uma_zone_t	pool_zone;
1702 	struct mtx	pool_lock;
1703 	bus_dma_tag_t	pool_dmat;
1704 	size_t		pool_entry_size;
1705 	struct pctrie	pool_ptree;
1706 };
1707 
1708 #define	DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock)
1709 #define	DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock)
1710 
1711 static inline int
1712 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags)
1713 {
1714 	struct linux_dma_obj *obj = mem;
1715 	struct dma_pool *pool = arg;
1716 	int error, nseg;
1717 	bus_dma_segment_t seg;
1718 
1719 	nseg = -1;
1720 	DMA_POOL_LOCK(pool);
1721 	error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap,
1722 	    vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT,
1723 	    &seg, &nseg);
1724 	DMA_POOL_UNLOCK(pool);
1725 	if (error != 0) {
1726 		return (error);
1727 	}
1728 	KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg));
1729 	obj->dma_addr = seg.ds_addr;
1730 
1731 	return (0);
1732 }
1733 
1734 static void
1735 dma_pool_obj_dtor(void *mem, int size, void *arg)
1736 {
1737 	struct linux_dma_obj *obj = mem;
1738 	struct dma_pool *pool = arg;
1739 
1740 	DMA_POOL_LOCK(pool);
1741 	bus_dmamap_unload(pool->pool_dmat, obj->dmamap);
1742 	DMA_POOL_UNLOCK(pool);
1743 }
1744 
1745 static int
1746 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused,
1747     int flags)
1748 {
1749 	struct dma_pool *pool = arg;
1750 	struct linux_dma_obj *obj;
1751 	int error, i;
1752 
1753 	for (i = 0; i < count; i++) {
1754 		obj = uma_zalloc(linux_dma_obj_zone, flags);
1755 		if (obj == NULL)
1756 			break;
1757 
1758 		error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr,
1759 		    BUS_DMA_NOWAIT, &obj->dmamap);
1760 		if (error!= 0) {
1761 			uma_zfree(linux_dma_obj_zone, obj);
1762 			break;
1763 		}
1764 
1765 		store[i] = obj;
1766 	}
1767 
1768 	return (i);
1769 }
1770 
1771 static void
1772 dma_pool_obj_release(void *arg, void **store, int count)
1773 {
1774 	struct dma_pool *pool = arg;
1775 	struct linux_dma_obj *obj;
1776 	int i;
1777 
1778 	for (i = 0; i < count; i++) {
1779 		obj = store[i];
1780 		bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap);
1781 		uma_zfree(linux_dma_obj_zone, obj);
1782 	}
1783 }
1784 
1785 struct dma_pool *
1786 linux_dma_pool_create(char *name, struct device *dev, size_t size,
1787     size_t align, size_t boundary)
1788 {
1789 	struct linux_dma_priv *priv;
1790 	struct dma_pool *pool;
1791 
1792 	priv = dev->dma_priv;
1793 
1794 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1795 	pool->pool_device = dev;
1796 	pool->pool_entry_size = size;
1797 
1798 	if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev),
1799 	    align, boundary,		/* alignment, boundary */
1800 	    priv->dma_mask,		/* lowaddr */
1801 	    BUS_SPACE_MAXADDR,		/* highaddr */
1802 	    NULL, NULL,			/* filtfunc, filtfuncarg */
1803 	    size,			/* maxsize */
1804 	    1,				/* nsegments */
1805 	    size,			/* maxsegsz */
1806 	    0,				/* flags */
1807 	    NULL, NULL,			/* lockfunc, lockfuncarg */
1808 	    &pool->pool_dmat)) {
1809 		kfree(pool);
1810 		return (NULL);
1811 	}
1812 
1813 	pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor,
1814 	    dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import,
1815 	    dma_pool_obj_release, pool, 0);
1816 
1817 	mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF);
1818 	pctrie_init(&pool->pool_ptree);
1819 
1820 	return (pool);
1821 }
1822 
1823 void
1824 linux_dma_pool_destroy(struct dma_pool *pool)
1825 {
1826 
1827 	uma_zdestroy(pool->pool_zone);
1828 	bus_dma_tag_destroy(pool->pool_dmat);
1829 	mtx_destroy(&pool->pool_lock);
1830 	kfree(pool);
1831 }
1832 
1833 void
1834 lkpi_dmam_pool_destroy(struct device *dev, void *p)
1835 {
1836 	struct dma_pool *pool;
1837 
1838 	pool = *(struct dma_pool **)p;
1839 	LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree);
1840 	linux_dma_pool_destroy(pool);
1841 }
1842 
1843 void *
1844 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
1845     dma_addr_t *handle)
1846 {
1847 	struct linux_dma_obj *obj;
1848 
1849 	obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK);
1850 	if (obj == NULL)
1851 		return (NULL);
1852 
1853 	DMA_POOL_LOCK(pool);
1854 	if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) {
1855 		DMA_POOL_UNLOCK(pool);
1856 		uma_zfree_arg(pool->pool_zone, obj, pool);
1857 		return (NULL);
1858 	}
1859 	DMA_POOL_UNLOCK(pool);
1860 
1861 	*handle = obj->dma_addr;
1862 	return (obj->vaddr);
1863 }
1864 
1865 void
1866 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
1867 {
1868 	struct linux_dma_obj *obj;
1869 
1870 	DMA_POOL_LOCK(pool);
1871 	obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr);
1872 	if (obj == NULL) {
1873 		DMA_POOL_UNLOCK(pool);
1874 		return;
1875 	}
1876 	LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr);
1877 	DMA_POOL_UNLOCK(pool);
1878 
1879 	uma_zfree_arg(pool->pool_zone, obj, pool);
1880 }
1881 
1882 static int
1883 linux_backlight_get_status(device_t dev, struct backlight_props *props)
1884 {
1885 	struct pci_dev *pdev;
1886 
1887 	linux_set_current(curthread);
1888 	pdev = device_get_softc(dev);
1889 
1890 	props->brightness = pdev->dev.bd->props.brightness;
1891 	props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness;
1892 	props->nlevels = 0;
1893 
1894 	return (0);
1895 }
1896 
1897 static int
1898 linux_backlight_get_info(device_t dev, struct backlight_info *info)
1899 {
1900 	struct pci_dev *pdev;
1901 
1902 	linux_set_current(curthread);
1903 	pdev = device_get_softc(dev);
1904 
1905 	info->type = BACKLIGHT_TYPE_PANEL;
1906 	strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH);
1907 	return (0);
1908 }
1909 
1910 static int
1911 linux_backlight_update_status(device_t dev, struct backlight_props *props)
1912 {
1913 	struct pci_dev *pdev;
1914 
1915 	linux_set_current(curthread);
1916 	pdev = device_get_softc(dev);
1917 
1918 	pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness *
1919 		props->brightness / 100;
1920 	pdev->dev.bd->props.power = props->brightness == 0 ?
1921 		4/* FB_BLANK_POWERDOWN */ : 0/* FB_BLANK_UNBLANK */;
1922 	return (pdev->dev.bd->ops->update_status(pdev->dev.bd));
1923 }
1924 
1925 struct backlight_device *
1926 linux_backlight_device_register(const char *name, struct device *dev,
1927     void *data, const struct backlight_ops *ops, struct backlight_properties *props)
1928 {
1929 
1930 	dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO);
1931 	dev->bd->ops = ops;
1932 	dev->bd->props.type = props->type;
1933 	dev->bd->props.max_brightness = props->max_brightness;
1934 	dev->bd->props.brightness = props->brightness;
1935 	dev->bd->props.power = props->power;
1936 	dev->bd->data = data;
1937 	dev->bd->dev = dev;
1938 	dev->bd->name = strdup(name, M_DEVBUF);
1939 
1940 	dev->backlight_dev = backlight_register(name, dev->bsddev);
1941 
1942 	return (dev->bd);
1943 }
1944 
1945 void
1946 linux_backlight_device_unregister(struct backlight_device *bd)
1947 {
1948 
1949 	backlight_destroy(bd->dev->backlight_dev);
1950 	free(bd->name, M_DEVBUF);
1951 	free(bd, M_DEVBUF);
1952 }
1953