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