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