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