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