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