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