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