1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com> 4 * (C) Copyright 2007 Novell Inc. 5 */ 6 7 #include <linux/pci.h> 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/device.h> 11 #include <linux/mempolicy.h> 12 #include <linux/string.h> 13 #include <linux/slab.h> 14 #include <linux/sched.h> 15 #include <linux/sched/isolation.h> 16 #include <linux/cpu.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/suspend.h> 19 #include <linux/kexec.h> 20 #include <linux/of_device.h> 21 #include <linux/acpi.h> 22 #include <linux/dma-map-ops.h> 23 #include <linux/iommu.h> 24 #include "pci.h" 25 #include "pcie/portdrv.h" 26 27 struct pci_dynid { 28 struct list_head node; 29 struct pci_device_id id; 30 }; 31 32 /** 33 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices 34 * @drv: target pci driver 35 * @vendor: PCI vendor ID 36 * @device: PCI device ID 37 * @subvendor: PCI subvendor ID 38 * @subdevice: PCI subdevice ID 39 * @class: PCI class 40 * @class_mask: PCI class mask 41 * @driver_data: private driver data 42 * 43 * Adds a new dynamic pci device ID to this driver and causes the 44 * driver to probe for all devices again. @drv must have been 45 * registered prior to calling this function. 46 * 47 * CONTEXT: 48 * Does GFP_KERNEL allocation. 49 * 50 * RETURNS: 51 * 0 on success, -errno on failure. 52 */ 53 int pci_add_dynid(struct pci_driver *drv, 54 unsigned int vendor, unsigned int device, 55 unsigned int subvendor, unsigned int subdevice, 56 unsigned int class, unsigned int class_mask, 57 unsigned long driver_data) 58 { 59 struct pci_dynid *dynid; 60 61 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); 62 if (!dynid) 63 return -ENOMEM; 64 65 dynid->id.vendor = vendor; 66 dynid->id.device = device; 67 dynid->id.subvendor = subvendor; 68 dynid->id.subdevice = subdevice; 69 dynid->id.class = class; 70 dynid->id.class_mask = class_mask; 71 dynid->id.driver_data = driver_data; 72 73 spin_lock(&drv->dynids.lock); 74 list_add_tail(&dynid->node, &drv->dynids.list); 75 spin_unlock(&drv->dynids.lock); 76 77 return driver_attach(&drv->driver); 78 } 79 EXPORT_SYMBOL_GPL(pci_add_dynid); 80 81 static void pci_free_dynids(struct pci_driver *drv) 82 { 83 struct pci_dynid *dynid, *n; 84 85 spin_lock(&drv->dynids.lock); 86 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { 87 list_del(&dynid->node); 88 kfree(dynid); 89 } 90 spin_unlock(&drv->dynids.lock); 91 } 92 93 /** 94 * pci_match_id - See if a PCI device matches a given pci_id table 95 * @ids: array of PCI device ID structures to search in 96 * @dev: the PCI device structure to match against. 97 * 98 * Used by a driver to check whether a PCI device is in its list of 99 * supported devices. Returns the matching pci_device_id structure or 100 * %NULL if there is no match. 101 * 102 * Deprecated; don't use this as it will not catch any dynamic IDs 103 * that a driver might want to check for. 104 */ 105 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, 106 struct pci_dev *dev) 107 { 108 if (ids) { 109 while (ids->vendor || ids->subvendor || ids->class_mask) { 110 if (pci_match_one_device(ids, dev)) 111 return ids; 112 ids++; 113 } 114 } 115 return NULL; 116 } 117 EXPORT_SYMBOL(pci_match_id); 118 119 static const struct pci_device_id pci_device_id_any = { 120 .vendor = PCI_ANY_ID, 121 .device = PCI_ANY_ID, 122 .subvendor = PCI_ANY_ID, 123 .subdevice = PCI_ANY_ID, 124 }; 125 126 /** 127 * pci_match_device - See if a device matches a driver's list of IDs 128 * @drv: the PCI driver to match against 129 * @dev: the PCI device structure to match against 130 * 131 * Used by a driver to check whether a PCI device is in its list of 132 * supported devices or in the dynids list, which may have been augmented 133 * via the sysfs "new_id" file. Returns the matching pci_device_id 134 * structure or %NULL if there is no match. 135 */ 136 static const struct pci_device_id *pci_match_device(struct pci_driver *drv, 137 struct pci_dev *dev) 138 { 139 struct pci_dynid *dynid; 140 const struct pci_device_id *found_id = NULL, *ids; 141 142 /* When driver_override is set, only bind to the matching driver */ 143 if (dev->driver_override && strcmp(dev->driver_override, drv->name)) 144 return NULL; 145 146 /* Look at the dynamic ids first, before the static ones */ 147 spin_lock(&drv->dynids.lock); 148 list_for_each_entry(dynid, &drv->dynids.list, node) { 149 if (pci_match_one_device(&dynid->id, dev)) { 150 found_id = &dynid->id; 151 break; 152 } 153 } 154 spin_unlock(&drv->dynids.lock); 155 156 if (found_id) 157 return found_id; 158 159 for (ids = drv->id_table; (found_id = pci_match_id(ids, dev)); 160 ids = found_id + 1) { 161 /* 162 * The match table is split based on driver_override. 163 * In case override_only was set, enforce driver_override 164 * matching. 165 */ 166 if (found_id->override_only) { 167 if (dev->driver_override) 168 return found_id; 169 } else { 170 return found_id; 171 } 172 } 173 174 /* driver_override will always match, send a dummy id */ 175 if (dev->driver_override) 176 return &pci_device_id_any; 177 return NULL; 178 } 179 180 /** 181 * new_id_store - sysfs frontend to pci_add_dynid() 182 * @driver: target device driver 183 * @buf: buffer for scanning device ID data 184 * @count: input size 185 * 186 * Allow PCI IDs to be added to an existing driver via sysfs. 187 */ 188 static ssize_t new_id_store(struct device_driver *driver, const char *buf, 189 size_t count) 190 { 191 struct pci_driver *pdrv = to_pci_driver(driver); 192 const struct pci_device_id *ids = pdrv->id_table; 193 u32 vendor, device, subvendor = PCI_ANY_ID, 194 subdevice = PCI_ANY_ID, class = 0, class_mask = 0; 195 unsigned long driver_data = 0; 196 int fields; 197 int retval = 0; 198 199 fields = sscanf(buf, "%x %x %x %x %x %x %lx", 200 &vendor, &device, &subvendor, &subdevice, 201 &class, &class_mask, &driver_data); 202 if (fields < 2) 203 return -EINVAL; 204 205 if (fields != 7) { 206 struct pci_dev *pdev = kzalloc(sizeof(*pdev), GFP_KERNEL); 207 if (!pdev) 208 return -ENOMEM; 209 210 pdev->vendor = vendor; 211 pdev->device = device; 212 pdev->subsystem_vendor = subvendor; 213 pdev->subsystem_device = subdevice; 214 pdev->class = class; 215 216 if (pci_match_device(pdrv, pdev)) 217 retval = -EEXIST; 218 219 kfree(pdev); 220 221 if (retval) 222 return retval; 223 } 224 225 /* Only accept driver_data values that match an existing id_table 226 entry */ 227 if (ids) { 228 retval = -EINVAL; 229 while (ids->vendor || ids->subvendor || ids->class_mask) { 230 if (driver_data == ids->driver_data) { 231 retval = 0; 232 break; 233 } 234 ids++; 235 } 236 if (retval) /* No match */ 237 return retval; 238 } 239 240 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice, 241 class, class_mask, driver_data); 242 if (retval) 243 return retval; 244 return count; 245 } 246 static DRIVER_ATTR_WO(new_id); 247 248 /** 249 * remove_id_store - remove a PCI device ID from this driver 250 * @driver: target device driver 251 * @buf: buffer for scanning device ID data 252 * @count: input size 253 * 254 * Removes a dynamic pci device ID to this driver. 255 */ 256 static ssize_t remove_id_store(struct device_driver *driver, const char *buf, 257 size_t count) 258 { 259 struct pci_dynid *dynid, *n; 260 struct pci_driver *pdrv = to_pci_driver(driver); 261 u32 vendor, device, subvendor = PCI_ANY_ID, 262 subdevice = PCI_ANY_ID, class = 0, class_mask = 0; 263 int fields; 264 size_t retval = -ENODEV; 265 266 fields = sscanf(buf, "%x %x %x %x %x %x", 267 &vendor, &device, &subvendor, &subdevice, 268 &class, &class_mask); 269 if (fields < 2) 270 return -EINVAL; 271 272 spin_lock(&pdrv->dynids.lock); 273 list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) { 274 struct pci_device_id *id = &dynid->id; 275 if ((id->vendor == vendor) && 276 (id->device == device) && 277 (subvendor == PCI_ANY_ID || id->subvendor == subvendor) && 278 (subdevice == PCI_ANY_ID || id->subdevice == subdevice) && 279 !((id->class ^ class) & class_mask)) { 280 list_del(&dynid->node); 281 kfree(dynid); 282 retval = count; 283 break; 284 } 285 } 286 spin_unlock(&pdrv->dynids.lock); 287 288 return retval; 289 } 290 static DRIVER_ATTR_WO(remove_id); 291 292 static struct attribute *pci_drv_attrs[] = { 293 &driver_attr_new_id.attr, 294 &driver_attr_remove_id.attr, 295 NULL, 296 }; 297 ATTRIBUTE_GROUPS(pci_drv); 298 299 struct drv_dev_and_id { 300 struct pci_driver *drv; 301 struct pci_dev *dev; 302 const struct pci_device_id *id; 303 }; 304 305 static long local_pci_probe(void *_ddi) 306 { 307 struct drv_dev_and_id *ddi = _ddi; 308 struct pci_dev *pci_dev = ddi->dev; 309 struct pci_driver *pci_drv = ddi->drv; 310 struct device *dev = &pci_dev->dev; 311 int rc; 312 313 /* 314 * Unbound PCI devices are always put in D0, regardless of 315 * runtime PM status. During probe, the device is set to 316 * active and the usage count is incremented. If the driver 317 * supports runtime PM, it should call pm_runtime_put_noidle(), 318 * or any other runtime PM helper function decrementing the usage 319 * count, in its probe routine and pm_runtime_get_noresume() in 320 * its remove routine. 321 */ 322 pm_runtime_get_sync(dev); 323 pci_dev->driver = pci_drv; 324 rc = pci_drv->probe(pci_dev, ddi->id); 325 if (!rc) 326 return rc; 327 if (rc < 0) { 328 pci_dev->driver = NULL; 329 pm_runtime_put_sync(dev); 330 return rc; 331 } 332 /* 333 * Probe function should return < 0 for failure, 0 for success 334 * Treat values > 0 as success, but warn. 335 */ 336 pci_warn(pci_dev, "Driver probe function unexpectedly returned %d\n", 337 rc); 338 return 0; 339 } 340 341 static bool pci_physfn_is_probed(struct pci_dev *dev) 342 { 343 #ifdef CONFIG_PCI_IOV 344 return dev->is_virtfn && dev->physfn->is_probed; 345 #else 346 return false; 347 #endif 348 } 349 350 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, 351 const struct pci_device_id *id) 352 { 353 int error, node, cpu; 354 struct drv_dev_and_id ddi = { drv, dev, id }; 355 356 /* 357 * Execute driver initialization on node where the device is 358 * attached. This way the driver likely allocates its local memory 359 * on the right node. 360 */ 361 node = dev_to_node(&dev->dev); 362 dev->is_probed = 1; 363 364 cpu_hotplug_disable(); 365 366 /* 367 * Prevent nesting work_on_cpu() for the case where a Virtual Function 368 * device is probed from work_on_cpu() of the Physical device. 369 */ 370 if (node < 0 || node >= MAX_NUMNODES || !node_online(node) || 371 pci_physfn_is_probed(dev)) { 372 cpu = nr_cpu_ids; 373 } else { 374 cpumask_var_t wq_domain_mask; 375 376 if (!zalloc_cpumask_var(&wq_domain_mask, GFP_KERNEL)) { 377 error = -ENOMEM; 378 goto out; 379 } 380 cpumask_and(wq_domain_mask, 381 housekeeping_cpumask(HK_TYPE_WQ), 382 housekeeping_cpumask(HK_TYPE_DOMAIN)); 383 384 cpu = cpumask_any_and(cpumask_of_node(node), 385 wq_domain_mask); 386 free_cpumask_var(wq_domain_mask); 387 } 388 389 if (cpu < nr_cpu_ids) 390 error = work_on_cpu(cpu, local_pci_probe, &ddi); 391 else 392 error = local_pci_probe(&ddi); 393 out: 394 dev->is_probed = 0; 395 cpu_hotplug_enable(); 396 return error; 397 } 398 399 /** 400 * __pci_device_probe - check if a driver wants to claim a specific PCI device 401 * @drv: driver to call to check if it wants the PCI device 402 * @pci_dev: PCI device being probed 403 * 404 * returns 0 on success, else error. 405 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. 406 */ 407 static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev) 408 { 409 const struct pci_device_id *id; 410 int error = 0; 411 412 if (drv->probe) { 413 error = -ENODEV; 414 415 id = pci_match_device(drv, pci_dev); 416 if (id) 417 error = pci_call_probe(drv, pci_dev, id); 418 } 419 return error; 420 } 421 422 #ifdef CONFIG_PCI_IOV 423 static inline bool pci_device_can_probe(struct pci_dev *pdev) 424 { 425 return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe || 426 pdev->driver_override); 427 } 428 #else 429 static inline bool pci_device_can_probe(struct pci_dev *pdev) 430 { 431 return true; 432 } 433 #endif 434 435 static int pci_device_probe(struct device *dev) 436 { 437 int error; 438 struct pci_dev *pci_dev = to_pci_dev(dev); 439 struct pci_driver *drv = to_pci_driver(dev->driver); 440 441 if (!pci_device_can_probe(pci_dev)) 442 return -ENODEV; 443 444 pci_assign_irq(pci_dev); 445 446 error = pcibios_alloc_irq(pci_dev); 447 if (error < 0) 448 return error; 449 450 pci_dev_get(pci_dev); 451 error = __pci_device_probe(drv, pci_dev); 452 if (error) { 453 pcibios_free_irq(pci_dev); 454 pci_dev_put(pci_dev); 455 } 456 457 return error; 458 } 459 460 static void pci_device_remove(struct device *dev) 461 { 462 struct pci_dev *pci_dev = to_pci_dev(dev); 463 struct pci_driver *drv = pci_dev->driver; 464 465 if (drv->remove) { 466 pm_runtime_get_sync(dev); 467 /* 468 * If the driver provides a .runtime_idle() callback and it has 469 * started to run already, it may continue to run in parallel 470 * with the code below, so wait until all of the runtime PM 471 * activity has completed. 472 */ 473 pm_runtime_barrier(dev); 474 drv->remove(pci_dev); 475 pm_runtime_put_noidle(dev); 476 } 477 pcibios_free_irq(pci_dev); 478 pci_dev->driver = NULL; 479 pci_iov_remove(pci_dev); 480 481 /* Undo the runtime PM settings in local_pci_probe() */ 482 pm_runtime_put_sync(dev); 483 484 /* 485 * If the device is still on, set the power state as "unknown", 486 * since it might change by the next time we load the driver. 487 */ 488 if (pci_dev->current_state == PCI_D0) 489 pci_dev->current_state = PCI_UNKNOWN; 490 491 /* 492 * We would love to complain here if pci_dev->is_enabled is set, that 493 * the driver should have called pci_disable_device(), but the 494 * unfortunate fact is there are too many odd BIOS and bridge setups 495 * that don't like drivers doing that all of the time. 496 * Oh well, we can dream of sane hardware when we sleep, no matter how 497 * horrible the crap we have to deal with is when we are awake... 498 */ 499 500 pci_dev_put(pci_dev); 501 } 502 503 static void pci_device_shutdown(struct device *dev) 504 { 505 struct pci_dev *pci_dev = to_pci_dev(dev); 506 struct pci_driver *drv = pci_dev->driver; 507 508 pm_runtime_resume(dev); 509 510 if (drv && drv->shutdown) 511 drv->shutdown(pci_dev); 512 513 /* 514 * If this is a kexec reboot, turn off Bus Master bit on the 515 * device to tell it to not continue to do DMA. Don't touch 516 * devices in D3cold or unknown states. 517 * If it is not a kexec reboot, firmware will hit the PCI 518 * devices with big hammer and stop their DMA any way. 519 */ 520 if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot)) 521 pci_clear_master(pci_dev); 522 } 523 524 #ifdef CONFIG_PM_SLEEP 525 526 /* Auxiliary functions used for system resume */ 527 528 /** 529 * pci_restore_standard_config - restore standard config registers of PCI device 530 * @pci_dev: PCI device to handle 531 */ 532 static int pci_restore_standard_config(struct pci_dev *pci_dev) 533 { 534 pci_update_current_state(pci_dev, PCI_UNKNOWN); 535 536 if (pci_dev->current_state != PCI_D0) { 537 int error = pci_set_power_state(pci_dev, PCI_D0); 538 if (error) 539 return error; 540 } 541 542 pci_restore_state(pci_dev); 543 pci_pme_restore(pci_dev); 544 return 0; 545 } 546 #endif /* CONFIG_PM_SLEEP */ 547 548 #ifdef CONFIG_PM 549 550 /* Auxiliary functions used for system resume and run-time resume */ 551 552 static void pci_pm_default_resume(struct pci_dev *pci_dev) 553 { 554 pci_fixup_device(pci_fixup_resume, pci_dev); 555 pci_enable_wake(pci_dev, PCI_D0, false); 556 } 557 558 static void pci_pm_default_resume_early(struct pci_dev *pci_dev) 559 { 560 pci_pm_power_up_and_verify_state(pci_dev); 561 pci_restore_state(pci_dev); 562 pci_pme_restore(pci_dev); 563 } 564 565 static void pci_pm_bridge_power_up_actions(struct pci_dev *pci_dev) 566 { 567 int ret; 568 569 ret = pci_bridge_wait_for_secondary_bus(pci_dev, "resume"); 570 if (ret) { 571 /* 572 * The downstream link failed to come up, so mark the 573 * devices below as disconnected to make sure we don't 574 * attempt to resume them. 575 */ 576 pci_walk_bus(pci_dev->subordinate, pci_dev_set_disconnected, 577 NULL); 578 return; 579 } 580 581 /* 582 * When powering on a bridge from D3cold, the whole hierarchy may be 583 * powered on into D0uninitialized state, resume them to give them a 584 * chance to suspend again 585 */ 586 pci_resume_bus(pci_dev->subordinate); 587 } 588 589 #endif /* CONFIG_PM */ 590 591 #ifdef CONFIG_PM_SLEEP 592 593 /* 594 * Default "suspend" method for devices that have no driver provided suspend, 595 * or not even a driver at all (second part). 596 */ 597 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev) 598 { 599 /* 600 * mark its power state as "unknown", since we don't know if 601 * e.g. the BIOS will change its device state when we suspend. 602 */ 603 if (pci_dev->current_state == PCI_D0) 604 pci_dev->current_state = PCI_UNKNOWN; 605 } 606 607 /* 608 * Default "resume" method for devices that have no driver provided resume, 609 * or not even a driver at all (second part). 610 */ 611 static int pci_pm_reenable_device(struct pci_dev *pci_dev) 612 { 613 int retval; 614 615 /* if the device was enabled before suspend, re-enable */ 616 retval = pci_reenable_device(pci_dev); 617 /* 618 * if the device was busmaster before the suspend, make it busmaster 619 * again 620 */ 621 if (pci_dev->is_busmaster) 622 pci_set_master(pci_dev); 623 624 return retval; 625 } 626 627 static int pci_legacy_suspend(struct device *dev, pm_message_t state) 628 { 629 struct pci_dev *pci_dev = to_pci_dev(dev); 630 struct pci_driver *drv = pci_dev->driver; 631 632 pci_dev->state_saved = false; 633 634 if (drv && drv->suspend) { 635 pci_power_t prev = pci_dev->current_state; 636 int error; 637 638 error = drv->suspend(pci_dev, state); 639 suspend_report_result(dev, drv->suspend, error); 640 if (error) 641 return error; 642 643 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 644 && pci_dev->current_state != PCI_UNKNOWN) { 645 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev, 646 "PCI PM: Device state not saved by %pS\n", 647 drv->suspend); 648 } 649 } 650 651 pci_fixup_device(pci_fixup_suspend, pci_dev); 652 653 return 0; 654 } 655 656 static int pci_legacy_suspend_late(struct device *dev) 657 { 658 struct pci_dev *pci_dev = to_pci_dev(dev); 659 660 if (!pci_dev->state_saved) 661 pci_save_state(pci_dev); 662 663 pci_pm_set_unknown_state(pci_dev); 664 665 pci_fixup_device(pci_fixup_suspend_late, pci_dev); 666 667 return 0; 668 } 669 670 static int pci_legacy_resume(struct device *dev) 671 { 672 struct pci_dev *pci_dev = to_pci_dev(dev); 673 struct pci_driver *drv = pci_dev->driver; 674 675 pci_fixup_device(pci_fixup_resume, pci_dev); 676 677 return drv && drv->resume ? 678 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev); 679 } 680 681 /* Auxiliary functions used by the new power management framework */ 682 683 static void pci_pm_default_suspend(struct pci_dev *pci_dev) 684 { 685 /* Disable non-bridge devices without PM support */ 686 if (!pci_has_subordinate(pci_dev)) 687 pci_disable_enabled_device(pci_dev); 688 } 689 690 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev) 691 { 692 struct pci_driver *drv = pci_dev->driver; 693 bool ret = drv && (drv->suspend || drv->resume); 694 695 /* 696 * Legacy PM support is used by default, so warn if the new framework is 697 * supported as well. Drivers are supposed to support either the 698 * former, or the latter, but not both at the same time. 699 */ 700 pci_WARN(pci_dev, ret && drv->driver.pm, "device %04x:%04x\n", 701 pci_dev->vendor, pci_dev->device); 702 703 return ret; 704 } 705 706 /* New power management framework */ 707 708 static int pci_pm_prepare(struct device *dev) 709 { 710 struct pci_dev *pci_dev = to_pci_dev(dev); 711 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 712 713 dev_pm_set_strict_midlayer(dev, true); 714 715 if (pm && pm->prepare) { 716 int error = pm->prepare(dev); 717 if (error < 0) 718 return error; 719 720 if (!error && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE)) 721 return 0; 722 } 723 if (pci_dev_need_resume(pci_dev)) 724 return 0; 725 726 /* 727 * The PME setting needs to be adjusted here in case the direct-complete 728 * optimization is used with respect to this device. 729 */ 730 pci_dev_adjust_pme(pci_dev); 731 return 1; 732 } 733 734 static void pci_pm_complete(struct device *dev) 735 { 736 struct pci_dev *pci_dev = to_pci_dev(dev); 737 738 pci_dev_complete_resume(pci_dev); 739 pm_generic_complete(dev); 740 741 /* Resume device if platform firmware has put it in reset-power-on */ 742 if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) { 743 pci_power_t pre_sleep_state = pci_dev->current_state; 744 745 pci_refresh_power_state(pci_dev); 746 /* 747 * On platforms with ACPI this check may also trigger for 748 * devices sharing power resources if one of those power 749 * resources has been activated as a result of a change of the 750 * power state of another device sharing it. However, in that 751 * case it is also better to resume the device, in general. 752 */ 753 if (pci_dev->current_state < pre_sleep_state) 754 pm_request_resume(dev); 755 } 756 757 dev_pm_set_strict_midlayer(dev, false); 758 } 759 760 #else /* !CONFIG_PM_SLEEP */ 761 762 #define pci_pm_prepare NULL 763 #define pci_pm_complete NULL 764 765 #endif /* !CONFIG_PM_SLEEP */ 766 767 #ifdef CONFIG_SUSPEND 768 static void pcie_pme_root_status_cleanup(struct pci_dev *pci_dev) 769 { 770 /* 771 * Some BIOSes forget to clear Root PME Status bits after system 772 * wakeup, which breaks ACPI-based runtime wakeup on PCI Express. 773 * Clear those bits now just in case (shouldn't hurt). 774 */ 775 if (pci_is_pcie(pci_dev) && 776 (pci_pcie_type(pci_dev) == PCI_EXP_TYPE_ROOT_PORT || 777 pci_pcie_type(pci_dev) == PCI_EXP_TYPE_RC_EC)) 778 pcie_clear_root_pme_status(pci_dev); 779 } 780 781 static int pci_pm_suspend(struct device *dev) 782 { 783 struct pci_dev *pci_dev = to_pci_dev(dev); 784 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 785 786 pci_dev->skip_bus_pm = false; 787 788 /* 789 * Disabling PTM allows some systems, e.g., Intel mobile chips 790 * since Coffee Lake, to enter a lower-power PM state. 791 */ 792 pci_suspend_ptm(pci_dev); 793 794 if (pci_has_legacy_pm_support(pci_dev)) 795 return pci_legacy_suspend(dev, PMSG_SUSPEND); 796 797 if (!pm) { 798 pci_pm_default_suspend(pci_dev); 799 return 0; 800 } 801 802 /* 803 * PCI devices suspended at run time may need to be resumed at this 804 * point, because in general it may be necessary to reconfigure them for 805 * system suspend. Namely, if the device is expected to wake up the 806 * system from the sleep state, it may have to be reconfigured for this 807 * purpose, or if the device is not expected to wake up the system from 808 * the sleep state, it should be prevented from signaling wakeup events 809 * going forward. 810 * 811 * Also if the driver of the device does not indicate that its system 812 * suspend callbacks can cope with runtime-suspended devices, it is 813 * better to resume the device from runtime suspend here. 814 */ 815 if (!dev_pm_smart_suspend(dev) || pci_dev_need_resume(pci_dev)) { 816 pm_runtime_resume(dev); 817 pci_dev->state_saved = false; 818 } else { 819 pci_dev_adjust_pme(pci_dev); 820 } 821 822 if (pm->suspend) { 823 pci_power_t prev = pci_dev->current_state; 824 int error; 825 826 error = pm->suspend(dev); 827 suspend_report_result(dev, pm->suspend, error); 828 if (error) 829 return error; 830 831 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 832 && pci_dev->current_state != PCI_UNKNOWN) { 833 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev, 834 "PCI PM: State of device not saved by %pS\n", 835 pm->suspend); 836 } 837 } 838 839 return 0; 840 } 841 842 static int pci_pm_suspend_late(struct device *dev) 843 { 844 if (dev_pm_skip_suspend(dev)) 845 return 0; 846 847 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev)); 848 849 return pm_generic_suspend_late(dev); 850 } 851 852 static int pci_pm_suspend_noirq(struct device *dev) 853 { 854 struct pci_dev *pci_dev = to_pci_dev(dev); 855 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 856 857 if (dev_pm_skip_suspend(dev)) 858 return 0; 859 860 if (pci_has_legacy_pm_support(pci_dev)) 861 return pci_legacy_suspend_late(dev); 862 863 if (!pm) { 864 pci_save_state(pci_dev); 865 goto Fixup; 866 } 867 868 if (pm->suspend_noirq) { 869 pci_power_t prev = pci_dev->current_state; 870 int error; 871 872 error = pm->suspend_noirq(dev); 873 suspend_report_result(dev, pm->suspend_noirq, error); 874 if (error) 875 return error; 876 877 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0 878 && pci_dev->current_state != PCI_UNKNOWN) { 879 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev, 880 "PCI PM: State of device not saved by %pS\n", 881 pm->suspend_noirq); 882 goto Fixup; 883 } 884 } 885 886 if (!pci_dev->state_saved) { 887 pci_save_state(pci_dev); 888 889 /* 890 * If the device is a bridge with a child in D0 below it, 891 * it needs to stay in D0, so check skip_bus_pm to avoid 892 * putting it into a low-power state in that case. 893 */ 894 if (!pci_dev->skip_bus_pm && pci_power_manageable(pci_dev)) 895 pci_prepare_to_sleep(pci_dev); 896 } 897 898 pci_dbg(pci_dev, "PCI PM: Suspend power state: %s\n", 899 pci_power_name(pci_dev->current_state)); 900 901 if (pci_dev->current_state == PCI_D0) { 902 pci_dev->skip_bus_pm = true; 903 /* 904 * Per PCI PM r1.2, table 6-1, a bridge must be in D0 if any 905 * downstream device is in D0, so avoid changing the power state 906 * of the parent bridge by setting the skip_bus_pm flag for it. 907 */ 908 if (pci_dev->bus->self) 909 pci_dev->bus->self->skip_bus_pm = true; 910 } 911 912 if (pci_dev->skip_bus_pm && pm_suspend_no_platform()) { 913 pci_dbg(pci_dev, "PCI PM: Skipped\n"); 914 goto Fixup; 915 } 916 917 pci_pm_set_unknown_state(pci_dev); 918 919 /* 920 * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's 921 * PCI COMMAND register isn't 0, the BIOS assumes that the controller 922 * hasn't been quiesced and tries to turn it off. If the controller 923 * is already in D3, this can hang or cause memory corruption. 924 * 925 * Since the value of the COMMAND register doesn't matter once the 926 * device has been suspended, we can safely set it to 0 here. 927 */ 928 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI) 929 pci_write_config_word(pci_dev, PCI_COMMAND, 0); 930 931 Fixup: 932 pci_fixup_device(pci_fixup_suspend_late, pci_dev); 933 934 /* 935 * If the target system sleep state is suspend-to-idle, it is sufficient 936 * to check whether or not the device's wakeup settings are good for 937 * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause 938 * pci_pm_complete() to take care of fixing up the device's state 939 * anyway, if need be. 940 */ 941 if (device_can_wakeup(dev) && !device_may_wakeup(dev)) 942 dev->power.may_skip_resume = false; 943 944 return 0; 945 } 946 947 static int pci_pm_resume_noirq(struct device *dev) 948 { 949 struct pci_dev *pci_dev = to_pci_dev(dev); 950 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 951 pci_power_t prev_state = pci_dev->current_state; 952 bool skip_bus_pm = pci_dev->skip_bus_pm; 953 954 if (dev_pm_skip_resume(dev)) 955 return 0; 956 957 /* 958 * In the suspend-to-idle case, devices left in D0 during suspend will 959 * stay in D0, so it is not necessary to restore or update their 960 * configuration here and attempting to put them into D0 again is 961 * pointless, so avoid doing that. 962 */ 963 if (!(skip_bus_pm && pm_suspend_no_platform())) 964 pci_pm_default_resume_early(pci_dev); 965 966 pci_fixup_device(pci_fixup_resume_early, pci_dev); 967 pcie_pme_root_status_cleanup(pci_dev); 968 969 if (!skip_bus_pm && prev_state == PCI_D3cold) 970 pci_pm_bridge_power_up_actions(pci_dev); 971 972 if (pci_has_legacy_pm_support(pci_dev)) 973 return 0; 974 975 if (pm && pm->resume_noirq) 976 return pm->resume_noirq(dev); 977 978 return 0; 979 } 980 981 static int pci_pm_resume_early(struct device *dev) 982 { 983 if (dev_pm_skip_resume(dev)) 984 return 0; 985 986 return pm_generic_resume_early(dev); 987 } 988 989 static int pci_pm_resume(struct device *dev) 990 { 991 struct pci_dev *pci_dev = to_pci_dev(dev); 992 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 993 994 /* 995 * This is necessary for the suspend error path in which resume is 996 * called without restoring the standard config registers of the device. 997 */ 998 if (pci_dev->state_saved) 999 pci_restore_standard_config(pci_dev); 1000 1001 pci_resume_ptm(pci_dev); 1002 1003 if (pci_has_legacy_pm_support(pci_dev)) 1004 return pci_legacy_resume(dev); 1005 1006 pci_pm_default_resume(pci_dev); 1007 1008 if (pm) { 1009 if (pm->resume) 1010 return pm->resume(dev); 1011 } else { 1012 pci_pm_reenable_device(pci_dev); 1013 } 1014 1015 return 0; 1016 } 1017 1018 #else /* !CONFIG_SUSPEND */ 1019 1020 #define pci_pm_suspend NULL 1021 #define pci_pm_suspend_late NULL 1022 #define pci_pm_suspend_noirq NULL 1023 #define pci_pm_resume NULL 1024 #define pci_pm_resume_early NULL 1025 #define pci_pm_resume_noirq NULL 1026 1027 #endif /* !CONFIG_SUSPEND */ 1028 1029 #ifdef CONFIG_HIBERNATE_CALLBACKS 1030 1031 static int pci_pm_freeze(struct device *dev) 1032 { 1033 struct pci_dev *pci_dev = to_pci_dev(dev); 1034 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1035 1036 if (pci_has_legacy_pm_support(pci_dev)) 1037 return pci_legacy_suspend(dev, PMSG_FREEZE); 1038 1039 if (!pm) { 1040 pci_pm_default_suspend(pci_dev); 1041 if (!pm_runtime_suspended(dev)) 1042 pci_dev->state_saved = false; 1043 return 0; 1044 } 1045 1046 /* 1047 * Resume all runtime-suspended devices before creating a snapshot 1048 * image of system memory, because the restore kernel generally cannot 1049 * be expected to always handle them consistently and they need to be 1050 * put into the runtime-active metastate during system resume anyway, 1051 * so it is better to ensure that the state saved in the image will be 1052 * always consistent with that. 1053 */ 1054 pm_runtime_resume(dev); 1055 pci_dev->state_saved = false; 1056 1057 if (pm->freeze) { 1058 int error; 1059 1060 error = pm->freeze(dev); 1061 suspend_report_result(dev, pm->freeze, error); 1062 if (error) 1063 return error; 1064 } 1065 1066 return 0; 1067 } 1068 1069 static int pci_pm_freeze_noirq(struct device *dev) 1070 { 1071 struct pci_dev *pci_dev = to_pci_dev(dev); 1072 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1073 1074 if (pci_has_legacy_pm_support(pci_dev)) 1075 return pci_legacy_suspend_late(dev); 1076 1077 if (pm && pm->freeze_noirq) { 1078 int error; 1079 1080 error = pm->freeze_noirq(dev); 1081 suspend_report_result(dev, pm->freeze_noirq, error); 1082 if (error) 1083 return error; 1084 } 1085 1086 if (!pci_dev->state_saved) 1087 pci_save_state(pci_dev); 1088 1089 pci_pm_set_unknown_state(pci_dev); 1090 1091 return 0; 1092 } 1093 1094 static int pci_pm_thaw_noirq(struct device *dev) 1095 { 1096 struct pci_dev *pci_dev = to_pci_dev(dev); 1097 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1098 1099 /* 1100 * The pm->thaw_noirq() callback assumes the device has been 1101 * returned to D0 and its config state has been restored. 1102 * 1103 * In addition, pci_restore_state() restores MSI-X state in MMIO 1104 * space, which requires the device to be in D0, so return it to D0 1105 * in case the driver's "freeze" callbacks put it into a low-power 1106 * state. 1107 */ 1108 pci_pm_power_up_and_verify_state(pci_dev); 1109 pci_restore_state(pci_dev); 1110 1111 if (pci_has_legacy_pm_support(pci_dev)) 1112 return 0; 1113 1114 if (pm && pm->thaw_noirq) 1115 return pm->thaw_noirq(dev); 1116 1117 return 0; 1118 } 1119 1120 static int pci_pm_thaw(struct device *dev) 1121 { 1122 struct pci_dev *pci_dev = to_pci_dev(dev); 1123 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1124 int error = 0; 1125 1126 if (pci_has_legacy_pm_support(pci_dev)) 1127 return pci_legacy_resume(dev); 1128 1129 if (pm) { 1130 if (pm->thaw) 1131 error = pm->thaw(dev); 1132 } else { 1133 pci_pm_reenable_device(pci_dev); 1134 } 1135 1136 return error; 1137 } 1138 1139 static int pci_pm_poweroff(struct device *dev) 1140 { 1141 struct pci_dev *pci_dev = to_pci_dev(dev); 1142 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1143 1144 if (pci_has_legacy_pm_support(pci_dev)) 1145 return pci_legacy_suspend(dev, PMSG_HIBERNATE); 1146 1147 if (!pm) { 1148 pci_pm_default_suspend(pci_dev); 1149 return 0; 1150 } 1151 1152 /* The reason to do that is the same as in pci_pm_suspend(). */ 1153 if (!dev_pm_smart_suspend(dev) || pci_dev_need_resume(pci_dev)) { 1154 pm_runtime_resume(dev); 1155 pci_dev->state_saved = false; 1156 } else { 1157 pci_dev_adjust_pme(pci_dev); 1158 } 1159 1160 if (pm->poweroff) { 1161 int error; 1162 1163 error = pm->poweroff(dev); 1164 suspend_report_result(dev, pm->poweroff, error); 1165 if (error) 1166 return error; 1167 } 1168 1169 return 0; 1170 } 1171 1172 static int pci_pm_poweroff_late(struct device *dev) 1173 { 1174 if (dev_pm_skip_suspend(dev)) 1175 return 0; 1176 1177 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev)); 1178 1179 return pm_generic_poweroff_late(dev); 1180 } 1181 1182 static int pci_pm_poweroff_noirq(struct device *dev) 1183 { 1184 struct pci_dev *pci_dev = to_pci_dev(dev); 1185 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1186 1187 if (dev_pm_skip_suspend(dev)) 1188 return 0; 1189 1190 if (pci_has_legacy_pm_support(pci_dev)) 1191 return pci_legacy_suspend_late(dev); 1192 1193 if (!pm) { 1194 pci_fixup_device(pci_fixup_suspend_late, pci_dev); 1195 return 0; 1196 } 1197 1198 if (pm->poweroff_noirq) { 1199 int error; 1200 1201 error = pm->poweroff_noirq(dev); 1202 suspend_report_result(dev, pm->poweroff_noirq, error); 1203 if (error) 1204 return error; 1205 } 1206 1207 if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev)) 1208 pci_prepare_to_sleep(pci_dev); 1209 1210 /* 1211 * The reason for doing this here is the same as for the analogous code 1212 * in pci_pm_suspend_noirq(). 1213 */ 1214 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI) 1215 pci_write_config_word(pci_dev, PCI_COMMAND, 0); 1216 1217 pci_fixup_device(pci_fixup_suspend_late, pci_dev); 1218 1219 return 0; 1220 } 1221 1222 static int pci_pm_restore_noirq(struct device *dev) 1223 { 1224 struct pci_dev *pci_dev = to_pci_dev(dev); 1225 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1226 1227 pci_pm_default_resume_early(pci_dev); 1228 pci_fixup_device(pci_fixup_resume_early, pci_dev); 1229 1230 if (pci_has_legacy_pm_support(pci_dev)) 1231 return 0; 1232 1233 if (pm && pm->restore_noirq) 1234 return pm->restore_noirq(dev); 1235 1236 return 0; 1237 } 1238 1239 static int pci_pm_restore(struct device *dev) 1240 { 1241 struct pci_dev *pci_dev = to_pci_dev(dev); 1242 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1243 1244 /* 1245 * This is necessary for the hibernation error path in which restore is 1246 * called without restoring the standard config registers of the device. 1247 */ 1248 if (pci_dev->state_saved) 1249 pci_restore_standard_config(pci_dev); 1250 1251 if (pci_has_legacy_pm_support(pci_dev)) 1252 return pci_legacy_resume(dev); 1253 1254 pci_pm_default_resume(pci_dev); 1255 1256 if (pm) { 1257 if (pm->restore) 1258 return pm->restore(dev); 1259 } else { 1260 pci_pm_reenable_device(pci_dev); 1261 } 1262 1263 return 0; 1264 } 1265 1266 #else /* !CONFIG_HIBERNATE_CALLBACKS */ 1267 1268 #define pci_pm_freeze NULL 1269 #define pci_pm_freeze_noirq NULL 1270 #define pci_pm_thaw NULL 1271 #define pci_pm_thaw_noirq NULL 1272 #define pci_pm_poweroff NULL 1273 #define pci_pm_poweroff_late NULL 1274 #define pci_pm_poweroff_noirq NULL 1275 #define pci_pm_restore NULL 1276 #define pci_pm_restore_noirq NULL 1277 1278 #endif /* !CONFIG_HIBERNATE_CALLBACKS */ 1279 1280 #ifdef CONFIG_PM 1281 1282 static int pci_pm_runtime_suspend(struct device *dev) 1283 { 1284 struct pci_dev *pci_dev = to_pci_dev(dev); 1285 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1286 pci_power_t prev = pci_dev->current_state; 1287 int error; 1288 1289 pci_suspend_ptm(pci_dev); 1290 1291 /* 1292 * If pci_dev->driver is not set (unbound), we leave the device in D0, 1293 * but it may go to D3cold when the bridge above it runtime suspends. 1294 * Save its config space in case that happens. 1295 */ 1296 if (!pci_dev->driver) { 1297 pci_save_state(pci_dev); 1298 return 0; 1299 } 1300 1301 pci_dev->state_saved = false; 1302 if (pm && pm->runtime_suspend) { 1303 error = pm->runtime_suspend(dev); 1304 /* 1305 * -EBUSY and -EAGAIN is used to request the runtime PM core 1306 * to schedule a new suspend, so log the event only with debug 1307 * log level. 1308 */ 1309 if (error == -EBUSY || error == -EAGAIN) { 1310 pci_dbg(pci_dev, "can't suspend now (%ps returned %d)\n", 1311 pm->runtime_suspend, error); 1312 return error; 1313 } else if (error) { 1314 pci_err(pci_dev, "can't suspend (%ps returned %d)\n", 1315 pm->runtime_suspend, error); 1316 return error; 1317 } 1318 } 1319 1320 pci_fixup_device(pci_fixup_suspend, pci_dev); 1321 1322 if (pm && pm->runtime_suspend 1323 && !pci_dev->state_saved && pci_dev->current_state != PCI_D0 1324 && pci_dev->current_state != PCI_UNKNOWN) { 1325 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev, 1326 "PCI PM: State of device not saved by %pS\n", 1327 pm->runtime_suspend); 1328 return 0; 1329 } 1330 1331 if (!pci_dev->state_saved) { 1332 pci_save_state(pci_dev); 1333 pci_finish_runtime_suspend(pci_dev); 1334 } 1335 1336 return 0; 1337 } 1338 1339 static int pci_pm_runtime_resume(struct device *dev) 1340 { 1341 struct pci_dev *pci_dev = to_pci_dev(dev); 1342 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1343 pci_power_t prev_state = pci_dev->current_state; 1344 int error = 0; 1345 1346 /* 1347 * Restoring config space is necessary even if the device is not bound 1348 * to a driver because although we left it in D0, it may have gone to 1349 * D3cold when the bridge above it runtime suspended. 1350 */ 1351 pci_pm_default_resume_early(pci_dev); 1352 pci_resume_ptm(pci_dev); 1353 1354 if (!pci_dev->driver) 1355 return 0; 1356 1357 pci_fixup_device(pci_fixup_resume_early, pci_dev); 1358 pci_pm_default_resume(pci_dev); 1359 1360 if (prev_state == PCI_D3cold) 1361 pci_pm_bridge_power_up_actions(pci_dev); 1362 1363 if (pm && pm->runtime_resume) 1364 error = pm->runtime_resume(dev); 1365 1366 return error; 1367 } 1368 1369 static int pci_pm_runtime_idle(struct device *dev) 1370 { 1371 struct pci_dev *pci_dev = to_pci_dev(dev); 1372 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1373 1374 /* 1375 * If pci_dev->driver is not set (unbound), the device should 1376 * always remain in D0 regardless of the runtime PM status 1377 */ 1378 if (!pci_dev->driver) 1379 return 0; 1380 1381 if (pm && pm->runtime_idle) 1382 return pm->runtime_idle(dev); 1383 1384 return 0; 1385 } 1386 1387 static const struct dev_pm_ops pci_dev_pm_ops = { 1388 .prepare = pci_pm_prepare, 1389 .complete = pci_pm_complete, 1390 .suspend = pci_pm_suspend, 1391 .suspend_late = pci_pm_suspend_late, 1392 .resume = pci_pm_resume, 1393 .resume_early = pci_pm_resume_early, 1394 .freeze = pci_pm_freeze, 1395 .thaw = pci_pm_thaw, 1396 .poweroff = pci_pm_poweroff, 1397 .poweroff_late = pci_pm_poweroff_late, 1398 .restore = pci_pm_restore, 1399 .suspend_noirq = pci_pm_suspend_noirq, 1400 .resume_noirq = pci_pm_resume_noirq, 1401 .freeze_noirq = pci_pm_freeze_noirq, 1402 .thaw_noirq = pci_pm_thaw_noirq, 1403 .poweroff_noirq = pci_pm_poweroff_noirq, 1404 .restore_noirq = pci_pm_restore_noirq, 1405 .runtime_suspend = pci_pm_runtime_suspend, 1406 .runtime_resume = pci_pm_runtime_resume, 1407 .runtime_idle = pci_pm_runtime_idle, 1408 }; 1409 1410 #define PCI_PM_OPS_PTR (&pci_dev_pm_ops) 1411 1412 #else /* !CONFIG_PM */ 1413 1414 #define pci_pm_runtime_suspend NULL 1415 #define pci_pm_runtime_resume NULL 1416 #define pci_pm_runtime_idle NULL 1417 1418 #define PCI_PM_OPS_PTR NULL 1419 1420 #endif /* !CONFIG_PM */ 1421 1422 /** 1423 * __pci_register_driver - register a new pci driver 1424 * @drv: the driver structure to register 1425 * @owner: owner module of drv 1426 * @mod_name: module name string 1427 * 1428 * Adds the driver structure to the list of registered drivers. 1429 * Returns a negative value on error, otherwise 0. 1430 * If no error occurred, the driver remains registered even if 1431 * no device was claimed during registration. 1432 */ 1433 int __pci_register_driver(struct pci_driver *drv, struct module *owner, 1434 const char *mod_name) 1435 { 1436 /* initialize common driver fields */ 1437 drv->driver.name = drv->name; 1438 drv->driver.bus = &pci_bus_type; 1439 drv->driver.owner = owner; 1440 drv->driver.mod_name = mod_name; 1441 drv->driver.groups = drv->groups; 1442 drv->driver.dev_groups = drv->dev_groups; 1443 1444 spin_lock_init(&drv->dynids.lock); 1445 INIT_LIST_HEAD(&drv->dynids.list); 1446 1447 /* register with core */ 1448 return driver_register(&drv->driver); 1449 } 1450 EXPORT_SYMBOL(__pci_register_driver); 1451 1452 /** 1453 * pci_unregister_driver - unregister a pci driver 1454 * @drv: the driver structure to unregister 1455 * 1456 * Deletes the driver structure from the list of registered PCI drivers, 1457 * gives it a chance to clean up by calling its remove() function for 1458 * each device it was responsible for, and marks those devices as 1459 * driverless. 1460 */ 1461 1462 void pci_unregister_driver(struct pci_driver *drv) 1463 { 1464 driver_unregister(&drv->driver); 1465 pci_free_dynids(drv); 1466 } 1467 EXPORT_SYMBOL(pci_unregister_driver); 1468 1469 static struct pci_driver pci_compat_driver = { 1470 .name = "compat" 1471 }; 1472 1473 /** 1474 * pci_dev_driver - get the pci_driver of a device 1475 * @dev: the device to query 1476 * 1477 * Returns the appropriate pci_driver structure or %NULL if there is no 1478 * registered driver for the device. 1479 */ 1480 struct pci_driver *pci_dev_driver(const struct pci_dev *dev) 1481 { 1482 int i; 1483 1484 if (dev->driver) 1485 return dev->driver; 1486 1487 for (i = 0; i <= PCI_ROM_RESOURCE; i++) 1488 if (dev->resource[i].flags & IORESOURCE_BUSY) 1489 return &pci_compat_driver; 1490 1491 return NULL; 1492 } 1493 EXPORT_SYMBOL(pci_dev_driver); 1494 1495 /** 1496 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure 1497 * @dev: the PCI device structure to match against 1498 * @drv: the device driver to search for matching PCI device id structures 1499 * 1500 * Used by a driver to check whether a PCI device present in the 1501 * system is in its list of supported devices. Returns the matching 1502 * pci_device_id structure or %NULL if there is no match. 1503 */ 1504 static int pci_bus_match(struct device *dev, const struct device_driver *drv) 1505 { 1506 struct pci_dev *pci_dev = to_pci_dev(dev); 1507 struct pci_driver *pci_drv; 1508 const struct pci_device_id *found_id; 1509 1510 if (pci_dev_binding_disallowed(pci_dev)) 1511 return 0; 1512 1513 pci_drv = (struct pci_driver *)to_pci_driver(drv); 1514 found_id = pci_match_device(pci_drv, pci_dev); 1515 if (found_id) 1516 return 1; 1517 1518 return 0; 1519 } 1520 1521 /** 1522 * pci_dev_get - increments the reference count of the pci device structure 1523 * @dev: the device being referenced 1524 * 1525 * Each live reference to a device should be refcounted. 1526 * 1527 * Drivers for PCI devices should normally record such references in 1528 * their probe() methods, when they bind to a device, and release 1529 * them by calling pci_dev_put(), in their disconnect() methods. 1530 * 1531 * A pointer to the device with the incremented reference counter is returned. 1532 */ 1533 struct pci_dev *pci_dev_get(struct pci_dev *dev) 1534 { 1535 if (dev) 1536 get_device(&dev->dev); 1537 return dev; 1538 } 1539 EXPORT_SYMBOL(pci_dev_get); 1540 1541 /** 1542 * pci_dev_put - release a use of the pci device structure 1543 * @dev: device that's been disconnected 1544 * 1545 * Must be called when a user of a device is finished with it. When the last 1546 * user of the device calls this function, the memory of the device is freed. 1547 */ 1548 void pci_dev_put(struct pci_dev *dev) 1549 { 1550 if (dev) 1551 put_device(&dev->dev); 1552 } 1553 EXPORT_SYMBOL(pci_dev_put); 1554 1555 static int pci_uevent(const struct device *dev, struct kobj_uevent_env *env) 1556 { 1557 const struct pci_dev *pdev; 1558 1559 if (!dev) 1560 return -ENODEV; 1561 1562 pdev = to_pci_dev(dev); 1563 1564 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class)) 1565 return -ENOMEM; 1566 1567 if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device)) 1568 return -ENOMEM; 1569 1570 if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor, 1571 pdev->subsystem_device)) 1572 return -ENOMEM; 1573 1574 if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev))) 1575 return -ENOMEM; 1576 1577 if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X", 1578 pdev->vendor, pdev->device, 1579 pdev->subsystem_vendor, pdev->subsystem_device, 1580 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8), 1581 (u8)(pdev->class))) 1582 return -ENOMEM; 1583 1584 return 0; 1585 } 1586 1587 #if defined(CONFIG_PCIEAER) || defined(CONFIG_EEH) || defined(CONFIG_S390) 1588 /** 1589 * pci_uevent_ers - emit a uevent during recovery path of PCI device 1590 * @pdev: PCI device undergoing error recovery 1591 * @err_type: type of error event 1592 */ 1593 void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type) 1594 { 1595 int idx = 0; 1596 char *envp[3]; 1597 1598 switch (err_type) { 1599 case PCI_ERS_RESULT_NONE: 1600 case PCI_ERS_RESULT_CAN_RECOVER: 1601 case PCI_ERS_RESULT_NEED_RESET: 1602 envp[idx++] = "ERROR_EVENT=BEGIN_RECOVERY"; 1603 envp[idx++] = "DEVICE_ONLINE=0"; 1604 break; 1605 case PCI_ERS_RESULT_RECOVERED: 1606 envp[idx++] = "ERROR_EVENT=SUCCESSFUL_RECOVERY"; 1607 envp[idx++] = "DEVICE_ONLINE=1"; 1608 break; 1609 case PCI_ERS_RESULT_DISCONNECT: 1610 envp[idx++] = "ERROR_EVENT=FAILED_RECOVERY"; 1611 envp[idx++] = "DEVICE_ONLINE=0"; 1612 break; 1613 default: 1614 break; 1615 } 1616 1617 if (idx > 0) { 1618 envp[idx++] = NULL; 1619 kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, envp); 1620 } 1621 } 1622 #endif 1623 1624 static int pci_bus_num_vf(struct device *dev) 1625 { 1626 return pci_num_vf(to_pci_dev(dev)); 1627 } 1628 1629 /** 1630 * pci_dma_configure - Setup DMA configuration 1631 * @dev: ptr to dev structure 1632 * 1633 * Function to update PCI devices's DMA configuration using the same 1634 * info from the OF node or ACPI node of host bridge's parent (if any). 1635 */ 1636 static int pci_dma_configure(struct device *dev) 1637 { 1638 const struct device_driver *drv = READ_ONCE(dev->driver); 1639 struct device *bridge; 1640 int ret = 0; 1641 1642 bridge = pci_get_host_bridge_device(to_pci_dev(dev)); 1643 1644 if (IS_ENABLED(CONFIG_OF) && bridge->parent && 1645 bridge->parent->of_node) { 1646 ret = of_dma_configure(dev, bridge->parent->of_node, true); 1647 } else if (has_acpi_companion(bridge)) { 1648 struct acpi_device *adev = to_acpi_device_node(bridge->fwnode); 1649 1650 ret = acpi_dma_configure(dev, acpi_get_dma_attr(adev)); 1651 } 1652 1653 pci_put_host_bridge_device(bridge); 1654 1655 /* @drv may not be valid when we're called from the IOMMU layer */ 1656 if (!ret && drv && !to_pci_driver(drv)->driver_managed_dma) { 1657 ret = iommu_device_use_default_domain(dev); 1658 if (ret) 1659 arch_teardown_dma_ops(dev); 1660 } 1661 1662 return ret; 1663 } 1664 1665 static void pci_dma_cleanup(struct device *dev) 1666 { 1667 struct pci_driver *driver = to_pci_driver(dev->driver); 1668 1669 if (!driver->driver_managed_dma) 1670 iommu_device_unuse_default_domain(dev); 1671 } 1672 1673 /* 1674 * pci_device_irq_get_affinity - get IRQ affinity mask for device 1675 * @dev: ptr to dev structure 1676 * @irq_vec: interrupt vector number 1677 * 1678 * Return the CPU affinity mask for @dev and @irq_vec. 1679 */ 1680 static const struct cpumask *pci_device_irq_get_affinity(struct device *dev, 1681 unsigned int irq_vec) 1682 { 1683 return pci_irq_get_affinity(to_pci_dev(dev), irq_vec); 1684 } 1685 1686 const struct bus_type pci_bus_type = { 1687 .name = "pci", 1688 .match = pci_bus_match, 1689 .uevent = pci_uevent, 1690 .probe = pci_device_probe, 1691 .remove = pci_device_remove, 1692 .shutdown = pci_device_shutdown, 1693 .irq_get_affinity = pci_device_irq_get_affinity, 1694 .dev_groups = pci_dev_groups, 1695 .bus_groups = pci_bus_groups, 1696 .drv_groups = pci_drv_groups, 1697 .pm = PCI_PM_OPS_PTR, 1698 .num_vf = pci_bus_num_vf, 1699 .dma_configure = pci_dma_configure, 1700 .dma_cleanup = pci_dma_cleanup, 1701 }; 1702 EXPORT_SYMBOL(pci_bus_type); 1703 1704 #ifdef CONFIG_PCIEPORTBUS 1705 static int pcie_port_bus_match(struct device *dev, const struct device_driver *drv) 1706 { 1707 struct pcie_device *pciedev; 1708 const struct pcie_port_service_driver *driver; 1709 1710 if (drv->bus != &pcie_port_bus_type || dev->bus != &pcie_port_bus_type) 1711 return 0; 1712 1713 pciedev = to_pcie_device(dev); 1714 driver = to_service_driver(drv); 1715 1716 if (driver->service != pciedev->service) 1717 return 0; 1718 1719 if (driver->port_type != PCIE_ANY_PORT && 1720 driver->port_type != pci_pcie_type(pciedev->port)) 1721 return 0; 1722 1723 return 1; 1724 } 1725 1726 const struct bus_type pcie_port_bus_type = { 1727 .name = "pci_express", 1728 .match = pcie_port_bus_match, 1729 }; 1730 #endif 1731 1732 static int __init pci_driver_init(void) 1733 { 1734 int ret; 1735 1736 ret = bus_register(&pci_bus_type); 1737 if (ret) 1738 return ret; 1739 1740 #ifdef CONFIG_PCIEPORTBUS 1741 ret = bus_register(&pcie_port_bus_type); 1742 if (ret) 1743 return ret; 1744 #endif 1745 dma_debug_add_bus(&pci_bus_type); 1746 return 0; 1747 } 1748 postcore_initcall(pci_driver_init); 1749