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_range(struct pci_dev *pdev, int mmio_bar, 760 unsigned long mmio_off, unsigned long mmio_size) 761 { 762 struct resource *res; 763 764 res = _lkpi_pci_iomap(pdev, mmio_bar, mmio_size); 765 if (res == NULL) 766 return (NULL); 767 /* This is a FreeBSD extension so we can use bus_*(). */ 768 if (pdev->want_iomap_res) 769 return (res); 770 MPASS(mmio_off < rman_get_size(res)); 771 return ((void *)(rman_get_bushandle(res) + mmio_off)); 772 } 773 774 void * 775 linuxkpi_pci_iomap(struct pci_dev *pdev, int mmio_bar, int mmio_size) 776 { 777 return (linuxkpi_pci_iomap_range(pdev, mmio_bar, 0, mmio_size)); 778 } 779 780 void 781 linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res) 782 { 783 struct pci_mmio_region *mmio, *p; 784 bus_space_handle_t bh = (bus_space_handle_t)res; 785 786 TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) { 787 if (pdev->want_iomap_res) { 788 if (res != mmio->res) 789 continue; 790 } else { 791 if (bh < rman_get_bushandle(mmio->res) || 792 bh >= rman_get_bushandle(mmio->res) + 793 rman_get_size(mmio->res)) 794 continue; 795 } 796 bus_release_resource(pdev->dev.bsddev, 797 mmio->type, mmio->rid, mmio->res); 798 TAILQ_REMOVE(&pdev->mmio, mmio, next); 799 free(mmio, M_DEVBUF); 800 return; 801 } 802 } 803 804 int 805 linuxkpi_pcim_iomap_regions(struct pci_dev *pdev, uint32_t mask, const char *name) 806 { 807 struct pcim_iomap_devres *dr; 808 void *res; 809 uint32_t mappings; 810 int bar; 811 812 dr = lkpi_pcim_iomap_devres_find(pdev); 813 if (dr == NULL) 814 return (-ENOMEM); 815 816 /* Now iomap all the requested (by "mask") ones. */ 817 for (bar = mappings = 0; mappings != mask; bar++) { 818 if ((mask & (1 << bar)) == 0) 819 continue; 820 821 /* Request double is not allowed. */ 822 if (dr->mmio_table[bar] != NULL) { 823 device_printf(pdev->dev.bsddev, "%s: bar %d %p\n", 824 __func__, bar, dr->mmio_table[bar]); 825 goto err; 826 } 827 828 res = _lkpi_pci_iomap(pdev, bar, 0); 829 if (res == NULL) 830 goto err; 831 dr->mmio_table[bar] = (void *)rman_get_bushandle(res); 832 dr->res_table[bar] = res; 833 834 mappings |= (1 << bar); 835 } 836 837 return (0); 838 err: 839 for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) { 840 if ((mappings & (1 << bar)) != 0) { 841 res = dr->mmio_table[bar]; 842 if (res == NULL) 843 continue; 844 pci_iounmap(pdev, res); 845 } 846 } 847 848 return (-EINVAL); 849 } 850 851 static void 852 lkpi_pcim_iomap_table_release(struct device *dev, void *p) 853 { 854 struct pcim_iomap_devres *dr; 855 struct pci_dev *pdev; 856 int bar; 857 858 dr = p; 859 pdev = to_pci_dev(dev); 860 for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) { 861 862 if (dr->mmio_table[bar] == NULL) 863 continue; 864 865 pci_iounmap(pdev, dr->mmio_table[bar]); 866 } 867 } 868 869 static int 870 linux_pci_suspend(device_t dev) 871 { 872 const struct dev_pm_ops *pmops; 873 struct pm_message pm = { }; 874 struct pci_dev *pdev; 875 int error; 876 877 error = 0; 878 linux_set_current(curthread); 879 pdev = device_get_softc(dev); 880 pmops = pdev->pdrv->driver.pm; 881 882 if (pdev->pdrv->suspend != NULL) 883 error = -pdev->pdrv->suspend(pdev, pm); 884 else if (pmops != NULL && pmops->suspend != NULL) { 885 error = -pmops->suspend(&pdev->dev); 886 if (error == 0 && pmops->suspend_late != NULL) 887 error = -pmops->suspend_late(&pdev->dev); 888 } 889 return (error); 890 } 891 892 static int 893 linux_pci_resume(device_t dev) 894 { 895 const struct dev_pm_ops *pmops; 896 struct pci_dev *pdev; 897 int error; 898 899 error = 0; 900 linux_set_current(curthread); 901 pdev = device_get_softc(dev); 902 pmops = pdev->pdrv->driver.pm; 903 904 if (pdev->pdrv->resume != NULL) 905 error = -pdev->pdrv->resume(pdev); 906 else if (pmops != NULL && pmops->resume != NULL) { 907 if (pmops->resume_early != NULL) 908 error = -pmops->resume_early(&pdev->dev); 909 if (error == 0 && pmops->resume != NULL) 910 error = -pmops->resume(&pdev->dev); 911 } 912 return (error); 913 } 914 915 static int 916 linux_pci_shutdown(device_t dev) 917 { 918 struct pci_dev *pdev; 919 920 linux_set_current(curthread); 921 pdev = device_get_softc(dev); 922 if (pdev->pdrv->shutdown != NULL) 923 pdev->pdrv->shutdown(pdev); 924 return (0); 925 } 926 927 static int 928 linux_pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *pf_config) 929 { 930 struct pci_dev *pdev; 931 int error; 932 933 linux_set_current(curthread); 934 pdev = device_get_softc(dev); 935 if (pdev->pdrv->bsd_iov_init != NULL) 936 error = pdev->pdrv->bsd_iov_init(dev, num_vfs, pf_config); 937 else 938 error = EINVAL; 939 return (error); 940 } 941 942 static void 943 linux_pci_iov_uninit(device_t dev) 944 { 945 struct pci_dev *pdev; 946 947 linux_set_current(curthread); 948 pdev = device_get_softc(dev); 949 if (pdev->pdrv->bsd_iov_uninit != NULL) 950 pdev->pdrv->bsd_iov_uninit(dev); 951 } 952 953 static int 954 linux_pci_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *vf_config) 955 { 956 struct pci_dev *pdev; 957 int error; 958 959 linux_set_current(curthread); 960 pdev = device_get_softc(dev); 961 if (pdev->pdrv->bsd_iov_add_vf != NULL) 962 error = pdev->pdrv->bsd_iov_add_vf(dev, vfnum, vf_config); 963 else 964 error = EINVAL; 965 return (error); 966 } 967 968 static int 969 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc) 970 { 971 int error; 972 973 linux_set_current(curthread); 974 spin_lock(&pci_lock); 975 list_add(&pdrv->node, &pci_drivers); 976 spin_unlock(&pci_lock); 977 if (pdrv->bsddriver.name == NULL) 978 pdrv->bsddriver.name = pdrv->name; 979 pdrv->bsddriver.methods = pci_methods; 980 pdrv->bsddriver.size = sizeof(struct pci_dev); 981 982 bus_topo_lock(); 983 error = devclass_add_driver(dc, &pdrv->bsddriver, 984 BUS_PASS_DEFAULT, &pdrv->bsdclass); 985 bus_topo_unlock(); 986 return (-error); 987 } 988 989 int 990 linux_pci_register_driver(struct pci_driver *pdrv) 991 { 992 devclass_t dc; 993 994 pdrv->isdrm = strcmp(pdrv->name, "drmn") == 0; 995 dc = pdrv->isdrm ? devclass_create("vgapci") : devclass_find("pci"); 996 if (dc == NULL) 997 return (-ENXIO); 998 return (_linux_pci_register_driver(pdrv, dc)); 999 } 1000 1001 static struct resource_list_entry * 1002 lkpi_pci_get_bar(struct pci_dev *pdev, int bar, bool reserve) 1003 { 1004 int type; 1005 1006 type = pci_resource_type(pdev, bar); 1007 if (type < 0) 1008 return (NULL); 1009 bar = PCIR_BAR(bar); 1010 return (linux_pci_get_rle(pdev, type, bar, reserve)); 1011 } 1012 1013 struct device * 1014 lkpi_pci_find_irq_dev(unsigned int irq) 1015 { 1016 struct pci_dev *pdev; 1017 struct device *found; 1018 1019 found = NULL; 1020 spin_lock(&pci_lock); 1021 list_for_each_entry(pdev, &pci_devices, links) { 1022 if (irq == pdev->dev.irq || 1023 (irq >= pdev->dev.irq_start && irq < pdev->dev.irq_end)) { 1024 found = &pdev->dev; 1025 break; 1026 } 1027 } 1028 spin_unlock(&pci_lock); 1029 return (found); 1030 } 1031 1032 unsigned long 1033 pci_resource_start(struct pci_dev *pdev, int bar) 1034 { 1035 struct resource_list_entry *rle; 1036 rman_res_t newstart; 1037 device_t dev; 1038 int error; 1039 1040 if ((rle = lkpi_pci_get_bar(pdev, bar, true)) == NULL) 1041 return (0); 1042 dev = pdev->pdrv != NULL && pdev->pdrv->isdrm ? 1043 device_get_parent(pdev->dev.bsddev) : pdev->dev.bsddev; 1044 error = bus_translate_resource(dev, rle->type, rle->start, &newstart); 1045 if (error != 0) { 1046 device_printf(pdev->dev.bsddev, 1047 "translate of %#jx failed: %d\n", 1048 (uintmax_t)rle->start, error); 1049 return (0); 1050 } 1051 return (newstart); 1052 } 1053 1054 unsigned long 1055 pci_resource_len(struct pci_dev *pdev, int bar) 1056 { 1057 struct resource_list_entry *rle; 1058 1059 if ((rle = lkpi_pci_get_bar(pdev, bar, true)) == NULL) 1060 return (0); 1061 return (rle->count); 1062 } 1063 1064 int 1065 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name) 1066 { 1067 struct resource *res; 1068 struct pci_devres *dr; 1069 struct pci_mmio_region *mmio; 1070 int rid; 1071 int type; 1072 1073 type = pci_resource_type(pdev, bar); 1074 if (type < 0) 1075 return (-ENODEV); 1076 rid = PCIR_BAR(bar); 1077 res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid, 1078 RF_ACTIVE|RF_SHAREABLE); 1079 if (res == NULL) { 1080 device_printf(pdev->dev.bsddev, "%s: failed to alloc " 1081 "bar %d type %d rid %d\n", 1082 __func__, bar, type, PCIR_BAR(bar)); 1083 return (-ENODEV); 1084 } 1085 1086 /* 1087 * It seems there is an implicit devres tracking on these if the device 1088 * is managed; otherwise the resources are not automatiaclly freed on 1089 * FreeBSD/LinuxKPI tough they should be/are expected to be by Linux 1090 * drivers. 1091 */ 1092 dr = lkpi_pci_devres_find(pdev); 1093 if (dr != NULL) { 1094 dr->region_mask |= (1 << bar); 1095 dr->region_table[bar] = res; 1096 } 1097 1098 /* Even if the device is not managed we need to track it for iomap. */ 1099 mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO); 1100 mmio->rid = PCIR_BAR(bar); 1101 mmio->type = type; 1102 mmio->res = res; 1103 TAILQ_INSERT_TAIL(&pdev->mmio, mmio, next); 1104 1105 return (0); 1106 } 1107 1108 int 1109 linuxkpi_pci_request_regions(struct pci_dev *pdev, const char *res_name) 1110 { 1111 int error; 1112 int i; 1113 1114 for (i = 0; i <= PCIR_MAX_BAR_0; i++) { 1115 error = pci_request_region(pdev, i, res_name); 1116 if (error && error != -ENODEV) { 1117 pci_release_regions(pdev); 1118 return (error); 1119 } 1120 } 1121 return (0); 1122 } 1123 1124 void 1125 linuxkpi_pci_release_region(struct pci_dev *pdev, int bar) 1126 { 1127 struct resource_list_entry *rle; 1128 struct pci_devres *dr; 1129 struct pci_mmio_region *mmio, *p; 1130 1131 if ((rle = lkpi_pci_get_bar(pdev, bar, false)) == NULL) 1132 return; 1133 1134 /* 1135 * As we implicitly track the requests we also need to clear them on 1136 * release. Do clear before resource release. 1137 */ 1138 dr = lkpi_pci_devres_find(pdev); 1139 if (dr != NULL) { 1140 KASSERT(dr->region_table[bar] == rle->res, ("%s: pdev %p bar %d" 1141 " region_table res %p != rel->res %p\n", __func__, pdev, 1142 bar, dr->region_table[bar], rle->res)); 1143 dr->region_table[bar] = NULL; 1144 dr->region_mask &= ~(1 << bar); 1145 } 1146 1147 TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) { 1148 if (rle->res != (void *)rman_get_bushandle(mmio->res)) 1149 continue; 1150 TAILQ_REMOVE(&pdev->mmio, mmio, next); 1151 free(mmio, M_DEVBUF); 1152 } 1153 1154 bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res); 1155 } 1156 1157 void 1158 linuxkpi_pci_release_regions(struct pci_dev *pdev) 1159 { 1160 int i; 1161 1162 for (i = 0; i <= PCIR_MAX_BAR_0; i++) 1163 pci_release_region(pdev, i); 1164 } 1165 1166 int 1167 linux_pci_register_drm_driver(struct pci_driver *pdrv) 1168 { 1169 devclass_t dc; 1170 1171 dc = devclass_create("vgapci"); 1172 if (dc == NULL) 1173 return (-ENXIO); 1174 pdrv->isdrm = true; 1175 pdrv->name = "drmn"; 1176 return (_linux_pci_register_driver(pdrv, dc)); 1177 } 1178 1179 void 1180 linux_pci_unregister_driver(struct pci_driver *pdrv) 1181 { 1182 devclass_t bus; 1183 1184 bus = devclass_find(pdrv->isdrm ? "vgapci" : "pci"); 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 void 1196 linux_pci_unregister_drm_driver(struct pci_driver *pdrv) 1197 { 1198 devclass_t bus; 1199 1200 bus = devclass_find("vgapci"); 1201 1202 spin_lock(&pci_lock); 1203 list_del(&pdrv->node); 1204 spin_unlock(&pci_lock); 1205 bus_topo_lock(); 1206 if (bus != NULL) 1207 devclass_delete_driver(bus, &pdrv->bsddriver); 1208 bus_topo_unlock(); 1209 } 1210 1211 int 1212 linuxkpi_pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, 1213 int nreq) 1214 { 1215 struct resource_list_entry *rle; 1216 int error; 1217 int avail; 1218 int i; 1219 1220 avail = pci_msix_count(pdev->dev.bsddev); 1221 if (avail < nreq) { 1222 if (avail == 0) 1223 return -EINVAL; 1224 return avail; 1225 } 1226 avail = nreq; 1227 if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0) 1228 return error; 1229 /* 1230 * Handle case where "pci_alloc_msix()" may allocate less 1231 * interrupts than available and return with no error: 1232 */ 1233 if (avail < nreq) { 1234 pci_release_msi(pdev->dev.bsddev); 1235 return avail; 1236 } 1237 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1, false); 1238 pdev->dev.irq_start = rle->start; 1239 pdev->dev.irq_end = rle->start + avail; 1240 for (i = 0; i < nreq; i++) 1241 entries[i].vector = pdev->dev.irq_start + i; 1242 pdev->msix_enabled = true; 1243 return (0); 1244 } 1245 1246 int 1247 _lkpi_pci_enable_msi_range(struct pci_dev *pdev, int minvec, int maxvec) 1248 { 1249 struct resource_list_entry *rle; 1250 int error; 1251 int nvec; 1252 1253 if (maxvec < minvec) 1254 return (-EINVAL); 1255 1256 nvec = pci_msi_count(pdev->dev.bsddev); 1257 if (nvec < 1 || nvec < minvec) 1258 return (-ENOSPC); 1259 1260 nvec = min(nvec, maxvec); 1261 if ((error = -pci_alloc_msi(pdev->dev.bsddev, &nvec)) != 0) 1262 return error; 1263 1264 /* Native PCI might only ever ask for 32 vectors. */ 1265 if (nvec < minvec) { 1266 pci_release_msi(pdev->dev.bsddev); 1267 return (-ENOSPC); 1268 } 1269 1270 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1, false); 1271 pdev->dev.irq_start = rle->start; 1272 pdev->dev.irq_end = rle->start + nvec; 1273 pdev->irq = rle->start; 1274 pdev->msi_enabled = true; 1275 return (0); 1276 } 1277 1278 int 1279 pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv, 1280 unsigned int flags) 1281 { 1282 int error; 1283 1284 if (flags & PCI_IRQ_MSIX) { 1285 struct msix_entry *entries; 1286 int i; 1287 1288 entries = kcalloc(maxv, sizeof(*entries), GFP_KERNEL); 1289 if (entries == NULL) { 1290 error = -ENOMEM; 1291 goto out; 1292 } 1293 for (i = 0; i < maxv; ++i) 1294 entries[i].entry = i; 1295 error = pci_enable_msix(pdev, entries, maxv); 1296 out: 1297 kfree(entries); 1298 if (error == 0 && pdev->msix_enabled) 1299 return (pdev->dev.irq_end - pdev->dev.irq_start); 1300 } 1301 if (flags & PCI_IRQ_MSI) { 1302 if (pci_msi_count(pdev->dev.bsddev) < minv) 1303 return (-ENOSPC); 1304 error = _lkpi_pci_enable_msi_range(pdev, minv, maxv); 1305 if (error == 0 && pdev->msi_enabled) 1306 return (pdev->dev.irq_end - pdev->dev.irq_start); 1307 } 1308 if (flags & PCI_IRQ_LEGACY) { 1309 if (pdev->irq) 1310 return (1); 1311 } 1312 1313 return (-EINVAL); 1314 } 1315 1316 struct msi_desc * 1317 lkpi_pci_msi_desc_alloc(int irq) 1318 { 1319 struct device *dev; 1320 struct pci_dev *pdev; 1321 struct msi_desc *desc; 1322 struct pci_devinfo *dinfo; 1323 struct pcicfg_msi *msi; 1324 int vec; 1325 1326 dev = lkpi_pci_find_irq_dev(irq); 1327 if (dev == NULL) 1328 return (NULL); 1329 1330 pdev = to_pci_dev(dev); 1331 1332 if (pdev->msi_desc == NULL) 1333 return (NULL); 1334 1335 if (irq < pdev->dev.irq_start || irq >= pdev->dev.irq_end) 1336 return (NULL); 1337 1338 vec = pdev->dev.irq_start - irq; 1339 1340 if (pdev->msi_desc[vec] != NULL) 1341 return (pdev->msi_desc[vec]); 1342 1343 dinfo = device_get_ivars(dev->bsddev); 1344 msi = &dinfo->cfg.msi; 1345 1346 desc = malloc(sizeof(*desc), M_DEVBUF, M_WAITOK | M_ZERO); 1347 1348 desc->pci.msi_attrib.is_64 = 1349 (msi->msi_ctrl & PCIM_MSICTRL_64BIT) ? true : false; 1350 desc->msg.data = msi->msi_data; 1351 1352 pdev->msi_desc[vec] = desc; 1353 1354 return (desc); 1355 } 1356 1357 bool 1358 pci_device_is_present(struct pci_dev *pdev) 1359 { 1360 device_t dev; 1361 1362 dev = pdev->dev.bsddev; 1363 1364 return (bus_child_present(dev)); 1365 } 1366 1367 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t)); 1368 1369 struct linux_dma_obj { 1370 void *vaddr; 1371 uint64_t dma_addr; 1372 bus_dmamap_t dmamap; 1373 bus_dma_tag_t dmat; 1374 }; 1375 1376 static uma_zone_t linux_dma_trie_zone; 1377 static uma_zone_t linux_dma_obj_zone; 1378 1379 static void 1380 linux_dma_init(void *arg) 1381 { 1382 1383 linux_dma_trie_zone = uma_zcreate("linux_dma_pctrie", 1384 pctrie_node_size(), NULL, NULL, pctrie_zone_init, NULL, 1385 UMA_ALIGN_PTR, 0); 1386 linux_dma_obj_zone = uma_zcreate("linux_dma_object", 1387 sizeof(struct linux_dma_obj), NULL, NULL, NULL, NULL, 1388 UMA_ALIGN_PTR, 0); 1389 lkpi_pci_nseg1_fail = counter_u64_alloc(M_WAITOK); 1390 } 1391 SYSINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_init, NULL); 1392 1393 static void 1394 linux_dma_uninit(void *arg) 1395 { 1396 1397 counter_u64_free(lkpi_pci_nseg1_fail); 1398 uma_zdestroy(linux_dma_obj_zone); 1399 uma_zdestroy(linux_dma_trie_zone); 1400 } 1401 SYSUNINIT(linux_dma, SI_SUB_DRIVERS, SI_ORDER_THIRD, linux_dma_uninit, NULL); 1402 1403 static void * 1404 linux_dma_trie_alloc(struct pctrie *ptree) 1405 { 1406 1407 return (uma_zalloc(linux_dma_trie_zone, M_NOWAIT)); 1408 } 1409 1410 static void 1411 linux_dma_trie_free(struct pctrie *ptree, void *node) 1412 { 1413 1414 uma_zfree(linux_dma_trie_zone, node); 1415 } 1416 1417 PCTRIE_DEFINE(LINUX_DMA, linux_dma_obj, dma_addr, linux_dma_trie_alloc, 1418 linux_dma_trie_free); 1419 1420 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__) 1421 static dma_addr_t 1422 linux_dma_map_phys_common(struct device *dev, vm_paddr_t phys, size_t len, 1423 bus_dma_tag_t dmat) 1424 { 1425 struct linux_dma_priv *priv; 1426 struct linux_dma_obj *obj; 1427 int error, nseg; 1428 bus_dma_segment_t seg; 1429 1430 priv = dev->dma_priv; 1431 1432 /* 1433 * If the resultant mapping will be entirely 1:1 with the 1434 * physical address, short-circuit the remainder of the 1435 * bus_dma API. This avoids tracking collisions in the pctrie 1436 * with the additional benefit of reducing overhead. 1437 */ 1438 if (bus_dma_id_mapped(dmat, phys, len)) 1439 return (phys); 1440 1441 obj = uma_zalloc(linux_dma_obj_zone, M_NOWAIT); 1442 if (obj == NULL) { 1443 return (0); 1444 } 1445 obj->dmat = dmat; 1446 1447 DMA_PRIV_LOCK(priv); 1448 if (bus_dmamap_create(obj->dmat, 0, &obj->dmamap) != 0) { 1449 DMA_PRIV_UNLOCK(priv); 1450 uma_zfree(linux_dma_obj_zone, obj); 1451 return (0); 1452 } 1453 1454 nseg = -1; 1455 if (_bus_dmamap_load_phys(obj->dmat, obj->dmamap, phys, len, 1456 BUS_DMA_NOWAIT, &seg, &nseg) != 0) { 1457 bus_dmamap_destroy(obj->dmat, obj->dmamap); 1458 DMA_PRIV_UNLOCK(priv); 1459 uma_zfree(linux_dma_obj_zone, obj); 1460 counter_u64_add(lkpi_pci_nseg1_fail, 1); 1461 if (linuxkpi_debug) 1462 dump_stack(); 1463 return (0); 1464 } 1465 1466 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg)); 1467 obj->dma_addr = seg.ds_addr; 1468 1469 error = LINUX_DMA_PCTRIE_INSERT(&priv->ptree, obj); 1470 if (error != 0) { 1471 bus_dmamap_unload(obj->dmat, obj->dmamap); 1472 bus_dmamap_destroy(obj->dmat, obj->dmamap); 1473 DMA_PRIV_UNLOCK(priv); 1474 uma_zfree(linux_dma_obj_zone, obj); 1475 return (0); 1476 } 1477 DMA_PRIV_UNLOCK(priv); 1478 return (obj->dma_addr); 1479 } 1480 #else 1481 static dma_addr_t 1482 linux_dma_map_phys_common(struct device *dev __unused, vm_paddr_t phys, 1483 size_t len __unused, bus_dma_tag_t dmat __unused) 1484 { 1485 return (phys); 1486 } 1487 #endif 1488 1489 dma_addr_t 1490 linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len) 1491 { 1492 struct linux_dma_priv *priv; 1493 1494 priv = dev->dma_priv; 1495 return (linux_dma_map_phys_common(dev, phys, len, priv->dmat)); 1496 } 1497 1498 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__) 1499 void 1500 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len) 1501 { 1502 struct linux_dma_priv *priv; 1503 struct linux_dma_obj *obj; 1504 1505 priv = dev->dma_priv; 1506 1507 if (pctrie_is_empty(&priv->ptree)) 1508 return; 1509 1510 DMA_PRIV_LOCK(priv); 1511 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr); 1512 if (obj == NULL) { 1513 DMA_PRIV_UNLOCK(priv); 1514 return; 1515 } 1516 LINUX_DMA_PCTRIE_REMOVE(&priv->ptree, dma_addr); 1517 bus_dmamap_unload(obj->dmat, obj->dmamap); 1518 bus_dmamap_destroy(obj->dmat, obj->dmamap); 1519 DMA_PRIV_UNLOCK(priv); 1520 1521 uma_zfree(linux_dma_obj_zone, obj); 1522 } 1523 #else 1524 void 1525 linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t len) 1526 { 1527 } 1528 #endif 1529 1530 void * 1531 linux_dma_alloc_coherent(struct device *dev, size_t size, 1532 dma_addr_t *dma_handle, gfp_t flag) 1533 { 1534 struct linux_dma_priv *priv; 1535 vm_paddr_t high; 1536 size_t align; 1537 void *mem; 1538 1539 if (dev == NULL || dev->dma_priv == NULL) { 1540 *dma_handle = 0; 1541 return (NULL); 1542 } 1543 priv = dev->dma_priv; 1544 if (priv->dma_coherent_mask) 1545 high = priv->dma_coherent_mask; 1546 else 1547 /* Coherent is lower 32bit only by default in Linux. */ 1548 high = BUS_SPACE_MAXADDR_32BIT; 1549 align = PAGE_SIZE << get_order(size); 1550 /* Always zero the allocation. */ 1551 flag |= M_ZERO; 1552 mem = kmem_alloc_contig(size, flag & GFP_NATIVE_MASK, 0, high, 1553 align, 0, VM_MEMATTR_DEFAULT); 1554 if (mem != NULL) { 1555 *dma_handle = linux_dma_map_phys_common(dev, vtophys(mem), size, 1556 priv->dmat_coherent); 1557 if (*dma_handle == 0) { 1558 kmem_free(mem, size); 1559 mem = NULL; 1560 } 1561 } else { 1562 *dma_handle = 0; 1563 } 1564 return (mem); 1565 } 1566 1567 struct lkpi_devres_dmam_coherent { 1568 size_t size; 1569 dma_addr_t *handle; 1570 void *mem; 1571 }; 1572 1573 static void 1574 lkpi_dmam_free_coherent(struct device *dev, void *p) 1575 { 1576 struct lkpi_devres_dmam_coherent *dr; 1577 1578 dr = p; 1579 dma_free_coherent(dev, dr->size, dr->mem, *dr->handle); 1580 } 1581 1582 void * 1583 linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 1584 gfp_t flag) 1585 { 1586 struct lkpi_devres_dmam_coherent *dr; 1587 1588 dr = lkpi_devres_alloc(lkpi_dmam_free_coherent, 1589 sizeof(*dr), GFP_KERNEL | __GFP_ZERO); 1590 1591 if (dr == NULL) 1592 return (NULL); 1593 1594 dr->size = size; 1595 dr->mem = linux_dma_alloc_coherent(dev, size, dma_handle, flag); 1596 dr->handle = dma_handle; 1597 if (dr->mem == NULL) { 1598 lkpi_devres_free(dr); 1599 return (NULL); 1600 } 1601 1602 lkpi_devres_add(dev, dr); 1603 return (dr->mem); 1604 } 1605 1606 void 1607 linuxkpi_dma_sync(struct device *dev, dma_addr_t dma_addr, size_t size, 1608 bus_dmasync_op_t op) 1609 { 1610 struct linux_dma_priv *priv; 1611 struct linux_dma_obj *obj; 1612 1613 priv = dev->dma_priv; 1614 1615 if (pctrie_is_empty(&priv->ptree)) 1616 return; 1617 1618 DMA_PRIV_LOCK(priv); 1619 obj = LINUX_DMA_PCTRIE_LOOKUP(&priv->ptree, dma_addr); 1620 if (obj == NULL) { 1621 DMA_PRIV_UNLOCK(priv); 1622 return; 1623 } 1624 1625 bus_dmamap_sync(obj->dmat, obj->dmamap, op); 1626 DMA_PRIV_UNLOCK(priv); 1627 } 1628 1629 int 1630 linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, 1631 enum dma_data_direction direction, unsigned long attrs __unused) 1632 { 1633 struct linux_dma_priv *priv; 1634 struct scatterlist *sg; 1635 int i, nseg; 1636 bus_dma_segment_t seg; 1637 1638 priv = dev->dma_priv; 1639 1640 DMA_PRIV_LOCK(priv); 1641 1642 /* create common DMA map in the first S/G entry */ 1643 if (bus_dmamap_create(priv->dmat, 0, &sgl->dma_map) != 0) { 1644 DMA_PRIV_UNLOCK(priv); 1645 return (0); 1646 } 1647 1648 /* load all S/G list entries */ 1649 for_each_sg(sgl, sg, nents, i) { 1650 nseg = -1; 1651 if (_bus_dmamap_load_phys(priv->dmat, sgl->dma_map, 1652 sg_phys(sg), sg->length, BUS_DMA_NOWAIT, 1653 &seg, &nseg) != 0) { 1654 bus_dmamap_unload(priv->dmat, sgl->dma_map); 1655 bus_dmamap_destroy(priv->dmat, sgl->dma_map); 1656 DMA_PRIV_UNLOCK(priv); 1657 return (0); 1658 } 1659 KASSERT(nseg == 0, 1660 ("More than one segment (nseg=%d)", nseg + 1)); 1661 1662 sg_dma_address(sg) = seg.ds_addr; 1663 } 1664 1665 switch (direction) { 1666 case DMA_BIDIRECTIONAL: 1667 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE); 1668 break; 1669 case DMA_TO_DEVICE: 1670 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD); 1671 break; 1672 case DMA_FROM_DEVICE: 1673 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREWRITE); 1674 break; 1675 default: 1676 break; 1677 } 1678 1679 DMA_PRIV_UNLOCK(priv); 1680 1681 return (nents); 1682 } 1683 1684 void 1685 linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl, 1686 int nents __unused, enum dma_data_direction direction, 1687 unsigned long attrs __unused) 1688 { 1689 struct linux_dma_priv *priv; 1690 1691 priv = dev->dma_priv; 1692 1693 DMA_PRIV_LOCK(priv); 1694 1695 switch (direction) { 1696 case DMA_BIDIRECTIONAL: 1697 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD); 1698 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_PREREAD); 1699 break; 1700 case DMA_TO_DEVICE: 1701 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTWRITE); 1702 break; 1703 case DMA_FROM_DEVICE: 1704 bus_dmamap_sync(priv->dmat, sgl->dma_map, BUS_DMASYNC_POSTREAD); 1705 break; 1706 default: 1707 break; 1708 } 1709 1710 bus_dmamap_unload(priv->dmat, sgl->dma_map); 1711 bus_dmamap_destroy(priv->dmat, sgl->dma_map); 1712 DMA_PRIV_UNLOCK(priv); 1713 } 1714 1715 struct dma_pool { 1716 struct device *pool_device; 1717 uma_zone_t pool_zone; 1718 struct mtx pool_lock; 1719 bus_dma_tag_t pool_dmat; 1720 size_t pool_entry_size; 1721 struct pctrie pool_ptree; 1722 }; 1723 1724 #define DMA_POOL_LOCK(pool) mtx_lock(&(pool)->pool_lock) 1725 #define DMA_POOL_UNLOCK(pool) mtx_unlock(&(pool)->pool_lock) 1726 1727 static inline int 1728 dma_pool_obj_ctor(void *mem, int size, void *arg, int flags) 1729 { 1730 struct linux_dma_obj *obj = mem; 1731 struct dma_pool *pool = arg; 1732 int error, nseg; 1733 bus_dma_segment_t seg; 1734 1735 nseg = -1; 1736 DMA_POOL_LOCK(pool); 1737 error = _bus_dmamap_load_phys(pool->pool_dmat, obj->dmamap, 1738 vtophys(obj->vaddr), pool->pool_entry_size, BUS_DMA_NOWAIT, 1739 &seg, &nseg); 1740 DMA_POOL_UNLOCK(pool); 1741 if (error != 0) { 1742 return (error); 1743 } 1744 KASSERT(++nseg == 1, ("More than one segment (nseg=%d)", nseg)); 1745 obj->dma_addr = seg.ds_addr; 1746 1747 return (0); 1748 } 1749 1750 static void 1751 dma_pool_obj_dtor(void *mem, int size, void *arg) 1752 { 1753 struct linux_dma_obj *obj = mem; 1754 struct dma_pool *pool = arg; 1755 1756 DMA_POOL_LOCK(pool); 1757 bus_dmamap_unload(pool->pool_dmat, obj->dmamap); 1758 DMA_POOL_UNLOCK(pool); 1759 } 1760 1761 static int 1762 dma_pool_obj_import(void *arg, void **store, int count, int domain __unused, 1763 int flags) 1764 { 1765 struct dma_pool *pool = arg; 1766 struct linux_dma_obj *obj; 1767 int error, i; 1768 1769 for (i = 0; i < count; i++) { 1770 obj = uma_zalloc(linux_dma_obj_zone, flags); 1771 if (obj == NULL) 1772 break; 1773 1774 error = bus_dmamem_alloc(pool->pool_dmat, &obj->vaddr, 1775 BUS_DMA_NOWAIT, &obj->dmamap); 1776 if (error!= 0) { 1777 uma_zfree(linux_dma_obj_zone, obj); 1778 break; 1779 } 1780 1781 store[i] = obj; 1782 } 1783 1784 return (i); 1785 } 1786 1787 static void 1788 dma_pool_obj_release(void *arg, void **store, int count) 1789 { 1790 struct dma_pool *pool = arg; 1791 struct linux_dma_obj *obj; 1792 int i; 1793 1794 for (i = 0; i < count; i++) { 1795 obj = store[i]; 1796 bus_dmamem_free(pool->pool_dmat, obj->vaddr, obj->dmamap); 1797 uma_zfree(linux_dma_obj_zone, obj); 1798 } 1799 } 1800 1801 struct dma_pool * 1802 linux_dma_pool_create(char *name, struct device *dev, size_t size, 1803 size_t align, size_t boundary) 1804 { 1805 struct linux_dma_priv *priv; 1806 struct dma_pool *pool; 1807 1808 priv = dev->dma_priv; 1809 1810 pool = kzalloc(sizeof(*pool), GFP_KERNEL); 1811 pool->pool_device = dev; 1812 pool->pool_entry_size = size; 1813 1814 if (bus_dma_tag_create(bus_get_dma_tag(dev->bsddev), 1815 align, boundary, /* alignment, boundary */ 1816 priv->dma_mask, /* lowaddr */ 1817 BUS_SPACE_MAXADDR, /* highaddr */ 1818 NULL, NULL, /* filtfunc, filtfuncarg */ 1819 size, /* maxsize */ 1820 1, /* nsegments */ 1821 size, /* maxsegsz */ 1822 0, /* flags */ 1823 NULL, NULL, /* lockfunc, lockfuncarg */ 1824 &pool->pool_dmat)) { 1825 kfree(pool); 1826 return (NULL); 1827 } 1828 1829 pool->pool_zone = uma_zcache_create(name, -1, dma_pool_obj_ctor, 1830 dma_pool_obj_dtor, NULL, NULL, dma_pool_obj_import, 1831 dma_pool_obj_release, pool, 0); 1832 1833 mtx_init(&pool->pool_lock, "lkpi-dma-pool", NULL, MTX_DEF); 1834 pctrie_init(&pool->pool_ptree); 1835 1836 return (pool); 1837 } 1838 1839 void 1840 linux_dma_pool_destroy(struct dma_pool *pool) 1841 { 1842 1843 uma_zdestroy(pool->pool_zone); 1844 bus_dma_tag_destroy(pool->pool_dmat); 1845 mtx_destroy(&pool->pool_lock); 1846 kfree(pool); 1847 } 1848 1849 void 1850 lkpi_dmam_pool_destroy(struct device *dev, void *p) 1851 { 1852 struct dma_pool *pool; 1853 1854 pool = *(struct dma_pool **)p; 1855 LINUX_DMA_PCTRIE_RECLAIM(&pool->pool_ptree); 1856 linux_dma_pool_destroy(pool); 1857 } 1858 1859 void * 1860 linux_dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags, 1861 dma_addr_t *handle) 1862 { 1863 struct linux_dma_obj *obj; 1864 1865 obj = uma_zalloc_arg(pool->pool_zone, pool, mem_flags & GFP_NATIVE_MASK); 1866 if (obj == NULL) 1867 return (NULL); 1868 1869 DMA_POOL_LOCK(pool); 1870 if (LINUX_DMA_PCTRIE_INSERT(&pool->pool_ptree, obj) != 0) { 1871 DMA_POOL_UNLOCK(pool); 1872 uma_zfree_arg(pool->pool_zone, obj, pool); 1873 return (NULL); 1874 } 1875 DMA_POOL_UNLOCK(pool); 1876 1877 *handle = obj->dma_addr; 1878 return (obj->vaddr); 1879 } 1880 1881 void 1882 linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr) 1883 { 1884 struct linux_dma_obj *obj; 1885 1886 DMA_POOL_LOCK(pool); 1887 obj = LINUX_DMA_PCTRIE_LOOKUP(&pool->pool_ptree, dma_addr); 1888 if (obj == NULL) { 1889 DMA_POOL_UNLOCK(pool); 1890 return; 1891 } 1892 LINUX_DMA_PCTRIE_REMOVE(&pool->pool_ptree, dma_addr); 1893 DMA_POOL_UNLOCK(pool); 1894 1895 uma_zfree_arg(pool->pool_zone, obj, pool); 1896 } 1897 1898 static int 1899 linux_backlight_get_status(device_t dev, struct backlight_props *props) 1900 { 1901 struct pci_dev *pdev; 1902 1903 linux_set_current(curthread); 1904 pdev = device_get_softc(dev); 1905 1906 props->brightness = pdev->dev.bd->props.brightness; 1907 props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness; 1908 props->nlevels = 0; 1909 1910 return (0); 1911 } 1912 1913 static int 1914 linux_backlight_get_info(device_t dev, struct backlight_info *info) 1915 { 1916 struct pci_dev *pdev; 1917 1918 linux_set_current(curthread); 1919 pdev = device_get_softc(dev); 1920 1921 info->type = BACKLIGHT_TYPE_PANEL; 1922 strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH); 1923 return (0); 1924 } 1925 1926 static int 1927 linux_backlight_update_status(device_t dev, struct backlight_props *props) 1928 { 1929 struct pci_dev *pdev; 1930 1931 linux_set_current(curthread); 1932 pdev = device_get_softc(dev); 1933 1934 pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness * 1935 props->brightness / 100; 1936 pdev->dev.bd->props.power = props->brightness == 0 ? 1937 4/* FB_BLANK_POWERDOWN */ : 0/* FB_BLANK_UNBLANK */; 1938 return (pdev->dev.bd->ops->update_status(pdev->dev.bd)); 1939 } 1940 1941 struct backlight_device * 1942 linux_backlight_device_register(const char *name, struct device *dev, 1943 void *data, const struct backlight_ops *ops, struct backlight_properties *props) 1944 { 1945 1946 dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO); 1947 dev->bd->ops = ops; 1948 dev->bd->props.type = props->type; 1949 dev->bd->props.max_brightness = props->max_brightness; 1950 dev->bd->props.brightness = props->brightness; 1951 dev->bd->props.power = props->power; 1952 dev->bd->data = data; 1953 dev->bd->dev = dev; 1954 dev->bd->name = strdup(name, M_DEVBUF); 1955 1956 dev->backlight_dev = backlight_register(name, dev->bsddev); 1957 1958 return (dev->bd); 1959 } 1960 1961 void 1962 linux_backlight_device_unregister(struct backlight_device *bd) 1963 { 1964 1965 backlight_destroy(bd->dev->backlight_dev); 1966 free(bd->name, M_DEVBUF); 1967 free(bd, M_DEVBUF); 1968 } 1969