1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * PCI Bus Services, see include/linux/pci.h for further explanation. 4 * 5 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter, 6 * David Mosberger-Tang 7 * 8 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz> 9 */ 10 11 #include <linux/acpi.h> 12 #include <linux/kernel.h> 13 #include <linux/delay.h> 14 #include <linux/dmi.h> 15 #include <linux/init.h> 16 #include <linux/iommu.h> 17 #include <linux/lockdep.h> 18 #include <linux/msi.h> 19 #include <linux/of.h> 20 #include <linux/pci.h> 21 #include <linux/pm.h> 22 #include <linux/slab.h> 23 #include <linux/module.h> 24 #include <linux/spinlock.h> 25 #include <linux/string.h> 26 #include <linux/log2.h> 27 #include <linux/logic_pio.h> 28 #include <linux/device.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/pci-ats.h> 31 #include <linux/pci_hotplug.h> 32 #include <linux/vmalloc.h> 33 #include <asm/dma.h> 34 #include <linux/aer.h> 35 #include <linux/bitfield.h> 36 #include "pci.h" 37 38 DEFINE_MUTEX(pci_slot_mutex); 39 40 const char *pci_power_names[] = { 41 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown", 42 }; 43 EXPORT_SYMBOL_GPL(pci_power_names); 44 45 #ifdef CONFIG_X86_32 46 int isa_dma_bridge_buggy; 47 EXPORT_SYMBOL(isa_dma_bridge_buggy); 48 #endif 49 50 int pci_pci_problems; 51 EXPORT_SYMBOL(pci_pci_problems); 52 53 unsigned int pci_pm_d3hot_delay; 54 55 static void pci_pme_list_scan(struct work_struct *work); 56 57 static LIST_HEAD(pci_pme_list); 58 static DEFINE_MUTEX(pci_pme_list_mutex); 59 static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan); 60 61 struct pci_pme_device { 62 struct list_head list; 63 struct pci_dev *dev; 64 }; 65 66 #define PME_TIMEOUT 1000 /* How long between PME checks */ 67 68 /* 69 * Following exit from Conventional Reset, devices must be ready within 1 sec 70 * (PCIe r6.0 sec 6.6.1). A D3cold to D0 transition implies a Conventional 71 * Reset (PCIe r6.0 sec 5.8). 72 */ 73 #define PCI_RESET_WAIT 1000 /* msec */ 74 75 /* 76 * Devices may extend the 1 sec period through Request Retry Status 77 * completions (PCIe r6.0 sec 2.3.1). The spec does not provide an upper 78 * limit, but 60 sec ought to be enough for any device to become 79 * responsive. 80 */ 81 #define PCIE_RESET_READY_POLL_MS 60000 /* msec */ 82 83 static void pci_dev_d3_sleep(struct pci_dev *dev) 84 { 85 unsigned int delay_ms = max(dev->d3hot_delay, pci_pm_d3hot_delay); 86 unsigned int upper; 87 88 if (delay_ms) { 89 /* Use a 20% upper bound, 1ms minimum */ 90 upper = max(DIV_ROUND_CLOSEST(delay_ms, 5), 1U); 91 usleep_range(delay_ms * USEC_PER_MSEC, 92 (delay_ms + upper) * USEC_PER_MSEC); 93 } 94 } 95 96 bool pci_reset_supported(struct pci_dev *dev) 97 { 98 return dev->reset_methods[0] != 0; 99 } 100 101 #ifdef CONFIG_PCI_DOMAINS 102 int pci_domains_supported = 1; 103 #endif 104 105 #define DEFAULT_HOTPLUG_IO_SIZE (256) 106 #define DEFAULT_HOTPLUG_MMIO_SIZE (2*1024*1024) 107 #define DEFAULT_HOTPLUG_MMIO_PREF_SIZE (2*1024*1024) 108 /* hpiosize=nn can override this */ 109 unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE; 110 /* 111 * pci=hpmmiosize=nnM overrides non-prefetchable MMIO size, 112 * pci=hpmmioprefsize=nnM overrides prefetchable MMIO size; 113 * pci=hpmemsize=nnM overrides both 114 */ 115 unsigned long pci_hotplug_mmio_size = DEFAULT_HOTPLUG_MMIO_SIZE; 116 unsigned long pci_hotplug_mmio_pref_size = DEFAULT_HOTPLUG_MMIO_PREF_SIZE; 117 118 #define DEFAULT_HOTPLUG_BUS_SIZE 1 119 unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE; 120 121 122 /* PCIe MPS/MRRS strategy; can be overridden by kernel command-line param */ 123 #ifdef CONFIG_PCIE_BUS_TUNE_OFF 124 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF; 125 #elif defined CONFIG_PCIE_BUS_SAFE 126 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_SAFE; 127 #elif defined CONFIG_PCIE_BUS_PERFORMANCE 128 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_PERFORMANCE; 129 #elif defined CONFIG_PCIE_BUS_PEER2PEER 130 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_PEER2PEER; 131 #else 132 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT; 133 #endif 134 135 /* 136 * The default CLS is used if arch didn't set CLS explicitly and not 137 * all pci devices agree on the same value. Arch can override either 138 * the dfl or actual value as it sees fit. Don't forget this is 139 * measured in 32-bit words, not bytes. 140 */ 141 u8 pci_dfl_cache_line_size __ro_after_init = L1_CACHE_BYTES >> 2; 142 u8 pci_cache_line_size __ro_after_init ; 143 144 /* 145 * If we set up a device for bus mastering, we need to check the latency 146 * timer as certain BIOSes forget to set it properly. 147 */ 148 unsigned int pcibios_max_latency = 255; 149 150 /* If set, the PCIe ARI capability will not be used. */ 151 static bool pcie_ari_disabled; 152 153 /* If set, the PCIe ATS capability will not be used. */ 154 static bool pcie_ats_disabled; 155 156 /* If set, the PCI config space of each device is printed during boot. */ 157 bool pci_early_dump; 158 159 bool pci_ats_disabled(void) 160 { 161 return pcie_ats_disabled; 162 } 163 EXPORT_SYMBOL_GPL(pci_ats_disabled); 164 165 /* Disable bridge_d3 for all PCIe ports */ 166 static bool pci_bridge_d3_disable; 167 /* Force bridge_d3 for all PCIe ports */ 168 static bool pci_bridge_d3_force; 169 170 static int __init pcie_port_pm_setup(char *str) 171 { 172 if (!strcmp(str, "off")) 173 pci_bridge_d3_disable = true; 174 else if (!strcmp(str, "force")) 175 pci_bridge_d3_force = true; 176 return 1; 177 } 178 __setup("pcie_port_pm=", pcie_port_pm_setup); 179 180 /** 181 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children 182 * @bus: pointer to PCI bus structure to search 183 * 184 * Given a PCI bus, returns the highest PCI bus number present in the set 185 * including the given PCI bus and its list of child PCI buses. 186 */ 187 unsigned char pci_bus_max_busnr(struct pci_bus *bus) 188 { 189 struct pci_bus *tmp; 190 unsigned char max, n; 191 192 max = bus->busn_res.end; 193 list_for_each_entry(tmp, &bus->children, node) { 194 n = pci_bus_max_busnr(tmp); 195 if (n > max) 196 max = n; 197 } 198 return max; 199 } 200 EXPORT_SYMBOL_GPL(pci_bus_max_busnr); 201 202 /** 203 * pci_status_get_and_clear_errors - return and clear error bits in PCI_STATUS 204 * @pdev: the PCI device 205 * 206 * Returns error bits set in PCI_STATUS and clears them. 207 */ 208 int pci_status_get_and_clear_errors(struct pci_dev *pdev) 209 { 210 u16 status; 211 int ret; 212 213 ret = pci_read_config_word(pdev, PCI_STATUS, &status); 214 if (ret != PCIBIOS_SUCCESSFUL) 215 return -EIO; 216 217 status &= PCI_STATUS_ERROR_BITS; 218 if (status) 219 pci_write_config_word(pdev, PCI_STATUS, status); 220 221 return status; 222 } 223 EXPORT_SYMBOL_GPL(pci_status_get_and_clear_errors); 224 225 #ifdef CONFIG_HAS_IOMEM 226 static void __iomem *__pci_ioremap_resource(struct pci_dev *pdev, int bar, 227 bool write_combine) 228 { 229 struct resource *res = &pdev->resource[bar]; 230 resource_size_t start = res->start; 231 resource_size_t size = resource_size(res); 232 233 /* 234 * Make sure the BAR is actually a memory resource, not an IO resource 235 */ 236 if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) { 237 pci_err(pdev, "can't ioremap BAR %d: %pR\n", bar, res); 238 return NULL; 239 } 240 241 if (write_combine) 242 return ioremap_wc(start, size); 243 244 return ioremap(start, size); 245 } 246 247 void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar) 248 { 249 return __pci_ioremap_resource(pdev, bar, false); 250 } 251 EXPORT_SYMBOL_GPL(pci_ioremap_bar); 252 253 void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar) 254 { 255 return __pci_ioremap_resource(pdev, bar, true); 256 } 257 EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar); 258 #endif 259 260 /** 261 * pci_dev_str_match_path - test if a path string matches a device 262 * @dev: the PCI device to test 263 * @path: string to match the device against 264 * @endptr: pointer to the string after the match 265 * 266 * Test if a string (typically from a kernel parameter) formatted as a 267 * path of device/function addresses matches a PCI device. The string must 268 * be of the form: 269 * 270 * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]* 271 * 272 * A path for a device can be obtained using 'lspci -t'. Using a path 273 * is more robust against bus renumbering than using only a single bus, 274 * device and function address. 275 * 276 * Returns 1 if the string matches the device, 0 if it does not and 277 * a negative error code if it fails to parse the string. 278 */ 279 static int pci_dev_str_match_path(struct pci_dev *dev, const char *path, 280 const char **endptr) 281 { 282 int ret; 283 unsigned int seg, bus, slot, func; 284 char *wpath, *p; 285 char end; 286 287 *endptr = strchrnul(path, ';'); 288 289 wpath = kmemdup_nul(path, *endptr - path, GFP_ATOMIC); 290 if (!wpath) 291 return -ENOMEM; 292 293 while (1) { 294 p = strrchr(wpath, '/'); 295 if (!p) 296 break; 297 ret = sscanf(p, "/%x.%x%c", &slot, &func, &end); 298 if (ret != 2) { 299 ret = -EINVAL; 300 goto free_and_exit; 301 } 302 303 if (dev->devfn != PCI_DEVFN(slot, func)) { 304 ret = 0; 305 goto free_and_exit; 306 } 307 308 /* 309 * Note: we don't need to get a reference to the upstream 310 * bridge because we hold a reference to the top level 311 * device which should hold a reference to the bridge, 312 * and so on. 313 */ 314 dev = pci_upstream_bridge(dev); 315 if (!dev) { 316 ret = 0; 317 goto free_and_exit; 318 } 319 320 *p = 0; 321 } 322 323 ret = sscanf(wpath, "%x:%x:%x.%x%c", &seg, &bus, &slot, 324 &func, &end); 325 if (ret != 4) { 326 seg = 0; 327 ret = sscanf(wpath, "%x:%x.%x%c", &bus, &slot, &func, &end); 328 if (ret != 3) { 329 ret = -EINVAL; 330 goto free_and_exit; 331 } 332 } 333 334 ret = (seg == pci_domain_nr(dev->bus) && 335 bus == dev->bus->number && 336 dev->devfn == PCI_DEVFN(slot, func)); 337 338 free_and_exit: 339 kfree(wpath); 340 return ret; 341 } 342 343 /** 344 * pci_dev_str_match - test if a string matches a device 345 * @dev: the PCI device to test 346 * @p: string to match the device against 347 * @endptr: pointer to the string after the match 348 * 349 * Test if a string (typically from a kernel parameter) matches a specified 350 * PCI device. The string may be of one of the following formats: 351 * 352 * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]* 353 * pci:<vendor>:<device>[:<subvendor>:<subdevice>] 354 * 355 * The first format specifies a PCI bus/device/function address which 356 * may change if new hardware is inserted, if motherboard firmware changes, 357 * or due to changes caused in kernel parameters. If the domain is 358 * left unspecified, it is taken to be 0. In order to be robust against 359 * bus renumbering issues, a path of PCI device/function numbers may be used 360 * to address the specific device. The path for a device can be determined 361 * through the use of 'lspci -t'. 362 * 363 * The second format matches devices using IDs in the configuration 364 * space which may match multiple devices in the system. A value of 0 365 * for any field will match all devices. (Note: this differs from 366 * in-kernel code that uses PCI_ANY_ID which is ~0; this is for 367 * legacy reasons and convenience so users don't have to specify 368 * FFFFFFFFs on the command line.) 369 * 370 * Returns 1 if the string matches the device, 0 if it does not and 371 * a negative error code if the string cannot be parsed. 372 */ 373 static int pci_dev_str_match(struct pci_dev *dev, const char *p, 374 const char **endptr) 375 { 376 int ret; 377 int count; 378 unsigned short vendor, device, subsystem_vendor, subsystem_device; 379 380 if (strncmp(p, "pci:", 4) == 0) { 381 /* PCI vendor/device (subvendor/subdevice) IDs are specified */ 382 p += 4; 383 ret = sscanf(p, "%hx:%hx:%hx:%hx%n", &vendor, &device, 384 &subsystem_vendor, &subsystem_device, &count); 385 if (ret != 4) { 386 ret = sscanf(p, "%hx:%hx%n", &vendor, &device, &count); 387 if (ret != 2) 388 return -EINVAL; 389 390 subsystem_vendor = 0; 391 subsystem_device = 0; 392 } 393 394 p += count; 395 396 if ((!vendor || vendor == dev->vendor) && 397 (!device || device == dev->device) && 398 (!subsystem_vendor || 399 subsystem_vendor == dev->subsystem_vendor) && 400 (!subsystem_device || 401 subsystem_device == dev->subsystem_device)) 402 goto found; 403 } else { 404 /* 405 * PCI Bus, Device, Function IDs are specified 406 * (optionally, may include a path of devfns following it) 407 */ 408 ret = pci_dev_str_match_path(dev, p, &p); 409 if (ret < 0) 410 return ret; 411 else if (ret) 412 goto found; 413 } 414 415 *endptr = p; 416 return 0; 417 418 found: 419 *endptr = p; 420 return 1; 421 } 422 423 static u8 __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn, 424 u8 pos, int cap) 425 { 426 return PCI_FIND_NEXT_CAP(pci_bus_read_config, pos, cap, NULL, bus, devfn); 427 } 428 429 u8 pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap) 430 { 431 return __pci_find_next_cap(dev->bus, dev->devfn, 432 pos + PCI_CAP_LIST_NEXT, cap); 433 } 434 EXPORT_SYMBOL_GPL(pci_find_next_capability); 435 436 static u8 __pci_bus_find_cap_start(struct pci_bus *bus, 437 unsigned int devfn, u8 hdr_type) 438 { 439 u16 status; 440 441 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status); 442 if (!(status & PCI_STATUS_CAP_LIST)) 443 return 0; 444 445 switch (hdr_type) { 446 case PCI_HEADER_TYPE_NORMAL: 447 case PCI_HEADER_TYPE_BRIDGE: 448 return PCI_CAPABILITY_LIST; 449 case PCI_HEADER_TYPE_CARDBUS: 450 return PCI_CB_CAPABILITY_LIST; 451 } 452 453 return 0; 454 } 455 456 /** 457 * pci_find_capability - query for devices' capabilities 458 * @dev: PCI device to query 459 * @cap: capability code 460 * 461 * Tell if a device supports a given PCI capability. 462 * Returns the address of the requested capability structure within the 463 * device's PCI configuration space or 0 in case the device does not 464 * support it. Possible values for @cap include: 465 * 466 * %PCI_CAP_ID_PM Power Management 467 * %PCI_CAP_ID_AGP Accelerated Graphics Port 468 * %PCI_CAP_ID_VPD Vital Product Data 469 * %PCI_CAP_ID_SLOTID Slot Identification 470 * %PCI_CAP_ID_MSI Message Signalled Interrupts 471 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap 472 * %PCI_CAP_ID_PCIX PCI-X 473 * %PCI_CAP_ID_EXP PCI Express 474 */ 475 u8 pci_find_capability(struct pci_dev *dev, int cap) 476 { 477 u8 pos; 478 479 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type); 480 if (pos) 481 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap); 482 483 return pos; 484 } 485 EXPORT_SYMBOL(pci_find_capability); 486 487 /** 488 * pci_bus_find_capability - query for devices' capabilities 489 * @bus: the PCI bus to query 490 * @devfn: PCI device to query 491 * @cap: capability code 492 * 493 * Like pci_find_capability() but works for PCI devices that do not have a 494 * pci_dev structure set up yet. 495 * 496 * Returns the address of the requested capability structure within the 497 * device's PCI configuration space or 0 in case the device does not 498 * support it. 499 */ 500 u8 pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap) 501 { 502 u8 hdr_type, pos; 503 504 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type); 505 506 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & PCI_HEADER_TYPE_MASK); 507 if (pos) 508 pos = __pci_find_next_cap(bus, devfn, pos, cap); 509 510 return pos; 511 } 512 EXPORT_SYMBOL(pci_bus_find_capability); 513 514 /** 515 * pci_find_next_ext_capability - Find an extended capability 516 * @dev: PCI device to query 517 * @start: address at which to start looking (0 to start at beginning of list) 518 * @cap: capability code 519 * 520 * Returns the address of the next matching extended capability structure 521 * within the device's PCI configuration space or 0 if the device does 522 * not support it. Some capabilities can occur several times, e.g., the 523 * vendor-specific capability, and this provides a way to find them all. 524 */ 525 u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 start, int cap) 526 { 527 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE) 528 return 0; 529 530 return PCI_FIND_NEXT_EXT_CAP(pci_bus_read_config, start, cap, 531 NULL, dev->bus, dev->devfn); 532 } 533 EXPORT_SYMBOL_GPL(pci_find_next_ext_capability); 534 535 /** 536 * pci_find_ext_capability - Find an extended capability 537 * @dev: PCI device to query 538 * @cap: capability code 539 * 540 * Returns the address of the requested extended capability structure 541 * within the device's PCI configuration space or 0 if the device does 542 * not support it. Possible values for @cap include: 543 * 544 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting 545 * %PCI_EXT_CAP_ID_VC Virtual Channel 546 * %PCI_EXT_CAP_ID_DSN Device Serial Number 547 * %PCI_EXT_CAP_ID_PWR Power Budgeting 548 */ 549 u16 pci_find_ext_capability(struct pci_dev *dev, int cap) 550 { 551 return pci_find_next_ext_capability(dev, 0, cap); 552 } 553 EXPORT_SYMBOL_GPL(pci_find_ext_capability); 554 555 /** 556 * pci_get_dsn - Read and return the 8-byte Device Serial Number 557 * @dev: PCI device to query 558 * 559 * Looks up the PCI_EXT_CAP_ID_DSN and reads the 8 bytes of the Device Serial 560 * Number. 561 * 562 * Returns the DSN, or zero if the capability does not exist. 563 */ 564 u64 pci_get_dsn(struct pci_dev *dev) 565 { 566 u32 dword; 567 u64 dsn; 568 int pos; 569 570 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN); 571 if (!pos) 572 return 0; 573 574 /* 575 * The Device Serial Number is two dwords offset 4 bytes from the 576 * capability position. The specification says that the first dword is 577 * the lower half, and the second dword is the upper half. 578 */ 579 pos += 4; 580 pci_read_config_dword(dev, pos, &dword); 581 dsn = (u64)dword; 582 pci_read_config_dword(dev, pos + 4, &dword); 583 dsn |= ((u64)dword) << 32; 584 585 return dsn; 586 } 587 EXPORT_SYMBOL_GPL(pci_get_dsn); 588 589 static u8 __pci_find_next_ht_cap(struct pci_dev *dev, u8 pos, int ht_cap) 590 { 591 int rc; 592 u8 cap, mask; 593 594 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST) 595 mask = HT_3BIT_CAP_MASK; 596 else 597 mask = HT_5BIT_CAP_MASK; 598 599 pos = PCI_FIND_NEXT_CAP(pci_bus_read_config, pos, 600 PCI_CAP_ID_HT, NULL, dev->bus, dev->devfn); 601 while (pos) { 602 rc = pci_read_config_byte(dev, pos + 3, &cap); 603 if (rc != PCIBIOS_SUCCESSFUL) 604 return 0; 605 606 if ((cap & mask) == ht_cap) 607 return pos; 608 609 pos = PCI_FIND_NEXT_CAP(pci_bus_read_config, 610 pos + PCI_CAP_LIST_NEXT, 611 PCI_CAP_ID_HT, NULL, dev->bus, 612 dev->devfn); 613 } 614 615 return 0; 616 } 617 618 /** 619 * pci_find_next_ht_capability - query a device's HyperTransport capabilities 620 * @dev: PCI device to query 621 * @pos: Position from which to continue searching 622 * @ht_cap: HyperTransport capability code 623 * 624 * To be used in conjunction with pci_find_ht_capability() to search for 625 * all capabilities matching @ht_cap. @pos should always be a value returned 626 * from pci_find_ht_capability(). 627 * 628 * NB. To be 100% safe against broken PCI devices, the caller should take 629 * steps to avoid an infinite loop. 630 */ 631 u8 pci_find_next_ht_capability(struct pci_dev *dev, u8 pos, int ht_cap) 632 { 633 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap); 634 } 635 EXPORT_SYMBOL_GPL(pci_find_next_ht_capability); 636 637 /** 638 * pci_find_ht_capability - query a device's HyperTransport capabilities 639 * @dev: PCI device to query 640 * @ht_cap: HyperTransport capability code 641 * 642 * Tell if a device supports a given HyperTransport capability. 643 * Returns an address within the device's PCI configuration space 644 * or 0 in case the device does not support the request capability. 645 * The address points to the PCI capability, of type PCI_CAP_ID_HT, 646 * which has a HyperTransport capability matching @ht_cap. 647 */ 648 u8 pci_find_ht_capability(struct pci_dev *dev, int ht_cap) 649 { 650 u8 pos; 651 652 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type); 653 if (pos) 654 pos = __pci_find_next_ht_cap(dev, pos, ht_cap); 655 656 return pos; 657 } 658 EXPORT_SYMBOL_GPL(pci_find_ht_capability); 659 660 /** 661 * pci_find_vsec_capability - Find a vendor-specific extended capability 662 * @dev: PCI device to query 663 * @vendor: Vendor ID for which capability is defined 664 * @cap: Vendor-specific capability ID 665 * 666 * If @dev has Vendor ID @vendor, search for a VSEC capability with 667 * VSEC ID @cap. If found, return the capability offset in 668 * config space; otherwise return 0. 669 */ 670 u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap) 671 { 672 u16 vsec = 0; 673 u32 header; 674 int ret; 675 676 if (vendor != dev->vendor) 677 return 0; 678 679 while ((vsec = pci_find_next_ext_capability(dev, vsec, 680 PCI_EXT_CAP_ID_VNDR))) { 681 ret = pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header); 682 if (ret != PCIBIOS_SUCCESSFUL) 683 continue; 684 685 if (PCI_VNDR_HEADER_ID(header) == cap) 686 return vsec; 687 } 688 689 return 0; 690 } 691 EXPORT_SYMBOL_GPL(pci_find_vsec_capability); 692 693 /** 694 * pci_find_dvsec_capability - Find DVSEC for vendor 695 * @dev: PCI device to query 696 * @vendor: Vendor ID to match for the DVSEC 697 * @dvsec: Designated Vendor-specific capability ID 698 * 699 * If DVSEC has Vendor ID @vendor and DVSEC ID @dvsec return the capability 700 * offset in config space; otherwise return 0. 701 */ 702 u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec) 703 { 704 int pos; 705 706 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DVSEC); 707 if (!pos) 708 return 0; 709 710 while (pos) { 711 u16 v, id; 712 713 pci_read_config_word(dev, pos + PCI_DVSEC_HEADER1, &v); 714 pci_read_config_word(dev, pos + PCI_DVSEC_HEADER2, &id); 715 if (vendor == v && dvsec == id) 716 return pos; 717 718 pos = pci_find_next_ext_capability(dev, pos, PCI_EXT_CAP_ID_DVSEC); 719 } 720 721 return 0; 722 } 723 EXPORT_SYMBOL_GPL(pci_find_dvsec_capability); 724 725 /** 726 * pci_find_parent_resource - return resource region of parent bus of given 727 * region 728 * @dev: PCI device structure contains resources to be searched 729 * @res: child resource record for which parent is sought 730 * 731 * For given resource region of given device, return the resource region of 732 * parent bus the given region is contained in. 733 */ 734 struct resource *pci_find_parent_resource(const struct pci_dev *dev, 735 struct resource *res) 736 { 737 const struct pci_bus *bus = dev->bus; 738 struct resource *r; 739 740 pci_bus_for_each_resource(bus, r) { 741 if (!r) 742 continue; 743 if (resource_contains(r, res)) { 744 745 /* 746 * If the window is prefetchable but the BAR is 747 * not, the allocator made a mistake. 748 */ 749 if (r->flags & IORESOURCE_PREFETCH && 750 !(res->flags & IORESOURCE_PREFETCH)) 751 return NULL; 752 753 /* 754 * If we're below a transparent bridge, there may 755 * be both a positively-decoded aperture and a 756 * subtractively-decoded region that contain the BAR. 757 * We want the positively-decoded one, so this depends 758 * on pci_bus_for_each_resource() giving us those 759 * first. 760 */ 761 return r; 762 } 763 } 764 return NULL; 765 } 766 EXPORT_SYMBOL(pci_find_parent_resource); 767 768 /** 769 * pci_find_resource - Return matching PCI device resource 770 * @dev: PCI device to query 771 * @res: Resource to look for 772 * 773 * Goes over standard PCI resources (BARs) and checks if the given resource 774 * is partially or fully contained in any of them. In that case the 775 * matching resource is returned, %NULL otherwise. 776 */ 777 struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res) 778 { 779 int i; 780 781 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 782 struct resource *r = &dev->resource[i]; 783 784 if (r->start && resource_contains(r, res)) 785 return r; 786 } 787 788 return NULL; 789 } 790 EXPORT_SYMBOL(pci_find_resource); 791 792 /** 793 * pci_resource_name - Return the name of the PCI resource 794 * @dev: PCI device to query 795 * @i: index of the resource 796 * 797 * Return the standard PCI resource (BAR) name according to their index. 798 */ 799 const char *pci_resource_name(struct pci_dev *dev, unsigned int i) 800 { 801 static const char * const bar_name[] = { 802 "BAR 0", 803 "BAR 1", 804 "BAR 2", 805 "BAR 3", 806 "BAR 4", 807 "BAR 5", 808 "ROM", 809 #ifdef CONFIG_PCI_IOV 810 "VF BAR 0", 811 "VF BAR 1", 812 "VF BAR 2", 813 "VF BAR 3", 814 "VF BAR 4", 815 "VF BAR 5", 816 #endif 817 "bridge window", /* "io" included in %pR */ 818 "bridge window", /* "mem" included in %pR */ 819 "bridge window", /* "mem pref" included in %pR */ 820 }; 821 static const char * const cardbus_name[] = { 822 "BAR 1", 823 "unknown", 824 "unknown", 825 "unknown", 826 "unknown", 827 "unknown", 828 #ifdef CONFIG_PCI_IOV 829 "unknown", 830 "unknown", 831 "unknown", 832 "unknown", 833 "unknown", 834 "unknown", 835 #endif 836 "CardBus bridge window 0", /* I/O */ 837 "CardBus bridge window 1", /* I/O */ 838 "CardBus bridge window 0", /* mem */ 839 "CardBus bridge window 1", /* mem */ 840 }; 841 842 if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS && 843 i < ARRAY_SIZE(cardbus_name)) 844 return cardbus_name[i]; 845 846 if (i < ARRAY_SIZE(bar_name)) 847 return bar_name[i]; 848 849 return "unknown"; 850 } 851 852 /** 853 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos 854 * @dev: the PCI device to operate on 855 * @pos: config space offset of status word 856 * @mask: mask of bit(s) to care about in status word 857 * 858 * Return 1 when mask bit(s) in status word clear, 0 otherwise. 859 */ 860 int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask) 861 { 862 int i; 863 864 /* Wait for Transaction Pending bit clean */ 865 for (i = 0; i < 4; i++) { 866 u16 status; 867 if (i) 868 msleep((1 << (i - 1)) * 100); 869 870 pci_read_config_word(dev, pos, &status); 871 if (!(status & mask)) 872 return 1; 873 } 874 875 return 0; 876 } 877 878 static int pci_acs_enable; 879 880 /** 881 * pci_request_acs - ask for ACS to be enabled if supported 882 */ 883 void pci_request_acs(void) 884 { 885 pci_acs_enable = 1; 886 } 887 888 static const char *disable_acs_redir_param; 889 static const char *config_acs_param; 890 891 struct pci_acs { 892 u16 ctrl; 893 u16 fw_ctrl; 894 }; 895 896 static void __pci_config_acs(struct pci_dev *dev, struct pci_acs *caps, 897 const char *p, const u16 acs_mask, const u16 acs_flags) 898 { 899 u16 flags = acs_flags; 900 u16 mask = acs_mask; 901 char *delimit; 902 int ret = 0; 903 904 if (!p) 905 return; 906 907 while (*p) { 908 if (!acs_mask) { 909 /* Check for ACS flags */ 910 delimit = strstr(p, "@"); 911 if (delimit) { 912 int end; 913 u32 shift = 0; 914 915 end = delimit - p - 1; 916 mask = 0; 917 flags = 0; 918 919 while (end > -1) { 920 if (*(p + end) == '0') { 921 mask |= 1 << shift; 922 shift++; 923 end--; 924 } else if (*(p + end) == '1') { 925 mask |= 1 << shift; 926 flags |= 1 << shift; 927 shift++; 928 end--; 929 } else if ((*(p + end) == 'x') || (*(p + end) == 'X')) { 930 shift++; 931 end--; 932 } else { 933 pci_err(dev, "Invalid ACS flags... Ignoring\n"); 934 return; 935 } 936 } 937 p = delimit + 1; 938 } else { 939 pci_err(dev, "ACS Flags missing\n"); 940 return; 941 } 942 } 943 944 if (mask & ~(PCI_ACS_SV | PCI_ACS_TB | PCI_ACS_RR | PCI_ACS_CR | 945 PCI_ACS_UF | PCI_ACS_EC | PCI_ACS_DT)) { 946 pci_err(dev, "Invalid ACS flags specified\n"); 947 return; 948 } 949 950 ret = pci_dev_str_match(dev, p, &p); 951 if (ret < 0) { 952 pr_info_once("PCI: Can't parse ACS command line parameter\n"); 953 break; 954 } else if (ret == 1) { 955 /* Found a match */ 956 break; 957 } 958 959 if (*p != ';' && *p != ',') { 960 /* End of param or invalid format */ 961 break; 962 } 963 p++; 964 } 965 966 if (ret != 1) 967 return; 968 969 if (!pci_dev_specific_disable_acs_redir(dev)) 970 return; 971 972 pci_dbg(dev, "ACS mask = %#06x\n", mask); 973 pci_dbg(dev, "ACS flags = %#06x\n", flags); 974 pci_dbg(dev, "ACS control = %#06x\n", caps->ctrl); 975 pci_dbg(dev, "ACS fw_ctrl = %#06x\n", caps->fw_ctrl); 976 977 /* 978 * For mask bits that are 0, copy them from the firmware setting 979 * and apply flags for all the mask bits that are 1. 980 */ 981 caps->ctrl = (caps->fw_ctrl & ~mask) | (flags & mask); 982 983 pci_info(dev, "Configured ACS to %#06x\n", caps->ctrl); 984 } 985 986 /** 987 * pci_std_enable_acs - enable ACS on devices using standard ACS capabilities 988 * @dev: the PCI device 989 * @caps: default ACS controls 990 */ 991 static void pci_std_enable_acs(struct pci_dev *dev, struct pci_acs *caps) 992 { 993 /* Source Validation */ 994 caps->ctrl |= (dev->acs_capabilities & PCI_ACS_SV); 995 996 /* P2P Request Redirect */ 997 caps->ctrl |= (dev->acs_capabilities & PCI_ACS_RR); 998 999 /* P2P Completion Redirect */ 1000 caps->ctrl |= (dev->acs_capabilities & PCI_ACS_CR); 1001 1002 /* Upstream Forwarding */ 1003 caps->ctrl |= (dev->acs_capabilities & PCI_ACS_UF); 1004 1005 /* Enable Translation Blocking for external devices and noats */ 1006 if (pci_ats_disabled() || dev->external_facing || dev->untrusted) 1007 caps->ctrl |= (dev->acs_capabilities & PCI_ACS_TB); 1008 } 1009 1010 /** 1011 * pci_enable_acs - enable ACS if hardware support it 1012 * @dev: the PCI device 1013 */ 1014 void pci_enable_acs(struct pci_dev *dev) 1015 { 1016 struct pci_acs caps; 1017 bool enable_acs = false; 1018 int pos; 1019 1020 /* If an iommu is present we start with kernel default caps */ 1021 if (pci_acs_enable) { 1022 if (pci_dev_specific_enable_acs(dev)) 1023 enable_acs = true; 1024 } 1025 1026 pos = dev->acs_cap; 1027 if (!pos) 1028 return; 1029 1030 pci_read_config_word(dev, pos + PCI_ACS_CTRL, &caps.ctrl); 1031 caps.fw_ctrl = caps.ctrl; 1032 1033 if (enable_acs) 1034 pci_std_enable_acs(dev, &caps); 1035 1036 /* 1037 * Always apply caps from the command line, even if there is no iommu. 1038 * Trust that the admin has a reason to change the ACS settings. 1039 */ 1040 __pci_config_acs(dev, &caps, disable_acs_redir_param, 1041 PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC, 1042 ~(PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC)); 1043 __pci_config_acs(dev, &caps, config_acs_param, 0, 0); 1044 1045 pci_write_config_word(dev, pos + PCI_ACS_CTRL, caps.ctrl); 1046 } 1047 1048 /** 1049 * pci_restore_bars - restore a device's BAR values (e.g. after wake-up) 1050 * @dev: PCI device to have its BARs restored 1051 * 1052 * Restore the BAR values for a given device, so as to make it 1053 * accessible by its driver. 1054 */ 1055 static void pci_restore_bars(struct pci_dev *dev) 1056 { 1057 int i; 1058 1059 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) 1060 pci_update_resource(dev, i); 1061 } 1062 1063 static inline bool platform_pci_power_manageable(struct pci_dev *dev) 1064 { 1065 if (pci_use_mid_pm()) 1066 return true; 1067 1068 return acpi_pci_power_manageable(dev); 1069 } 1070 1071 static inline int platform_pci_set_power_state(struct pci_dev *dev, 1072 pci_power_t t) 1073 { 1074 if (pci_use_mid_pm()) 1075 return mid_pci_set_power_state(dev, t); 1076 1077 return acpi_pci_set_power_state(dev, t); 1078 } 1079 1080 static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev) 1081 { 1082 if (pci_use_mid_pm()) 1083 return mid_pci_get_power_state(dev); 1084 1085 return acpi_pci_get_power_state(dev); 1086 } 1087 1088 static inline void platform_pci_refresh_power_state(struct pci_dev *dev) 1089 { 1090 if (!pci_use_mid_pm()) 1091 acpi_pci_refresh_power_state(dev); 1092 } 1093 1094 static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev) 1095 { 1096 if (pci_use_mid_pm()) 1097 return PCI_POWER_ERROR; 1098 1099 return acpi_pci_choose_state(dev); 1100 } 1101 1102 static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable) 1103 { 1104 if (pci_use_mid_pm()) 1105 return PCI_POWER_ERROR; 1106 1107 return acpi_pci_wakeup(dev, enable); 1108 } 1109 1110 static inline bool platform_pci_need_resume(struct pci_dev *dev) 1111 { 1112 if (pci_use_mid_pm()) 1113 return false; 1114 1115 return acpi_pci_need_resume(dev); 1116 } 1117 1118 static inline bool platform_pci_bridge_d3(struct pci_dev *dev) 1119 { 1120 if (pci_use_mid_pm()) 1121 return false; 1122 1123 return acpi_pci_bridge_d3(dev); 1124 } 1125 1126 /** 1127 * pci_update_current_state - Read power state of given device and cache it 1128 * @dev: PCI device to handle. 1129 * @state: State to cache in case the device doesn't have the PM capability 1130 * 1131 * The power state is read from the PMCSR register, which however is 1132 * inaccessible in D3cold. The platform firmware is therefore queried first 1133 * to detect accessibility of the register. In case the platform firmware 1134 * reports an incorrect state or the device isn't power manageable by the 1135 * platform at all, we try to detect D3cold by testing accessibility of the 1136 * vendor ID in config space. 1137 */ 1138 void pci_update_current_state(struct pci_dev *dev, pci_power_t state) 1139 { 1140 if (platform_pci_get_power_state(dev) == PCI_D3cold) { 1141 dev->current_state = PCI_D3cold; 1142 } else if (dev->pm_cap) { 1143 u16 pmcsr; 1144 1145 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 1146 if (PCI_POSSIBLE_ERROR(pmcsr)) { 1147 dev->current_state = PCI_D3cold; 1148 return; 1149 } 1150 dev->current_state = pmcsr & PCI_PM_CTRL_STATE_MASK; 1151 } else { 1152 dev->current_state = state; 1153 } 1154 } 1155 1156 /** 1157 * pci_refresh_power_state - Refresh the given device's power state data 1158 * @dev: Target PCI device. 1159 * 1160 * Ask the platform to refresh the devices power state information and invoke 1161 * pci_update_current_state() to update its current PCI power state. 1162 */ 1163 void pci_refresh_power_state(struct pci_dev *dev) 1164 { 1165 platform_pci_refresh_power_state(dev); 1166 pci_update_current_state(dev, dev->current_state); 1167 } 1168 1169 /** 1170 * pci_platform_power_transition - Use platform to change device power state 1171 * @dev: PCI device to handle. 1172 * @state: State to put the device into. 1173 */ 1174 int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state) 1175 { 1176 int error; 1177 1178 error = platform_pci_set_power_state(dev, state); 1179 if (!error) 1180 pci_update_current_state(dev, state); 1181 else if (!dev->pm_cap) /* Fall back to PCI_D0 */ 1182 dev->current_state = PCI_D0; 1183 1184 return error; 1185 } 1186 EXPORT_SYMBOL_GPL(pci_platform_power_transition); 1187 1188 static int pci_resume_one(struct pci_dev *pci_dev, void *ign) 1189 { 1190 pm_request_resume(&pci_dev->dev); 1191 return 0; 1192 } 1193 1194 /** 1195 * pci_resume_bus - Walk given bus and runtime resume devices on it 1196 * @bus: Top bus of the subtree to walk. 1197 */ 1198 void pci_resume_bus(struct pci_bus *bus) 1199 { 1200 if (bus) 1201 pci_walk_bus(bus, pci_resume_one, NULL); 1202 } 1203 1204 static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout) 1205 { 1206 int delay = 1; 1207 bool retrain = false; 1208 struct pci_dev *root, *bridge; 1209 1210 root = pcie_find_root_port(dev); 1211 1212 if (pci_is_pcie(dev)) { 1213 bridge = pci_upstream_bridge(dev); 1214 if (bridge) 1215 retrain = true; 1216 } 1217 1218 /* 1219 * The caller has already waited long enough after a reset that the 1220 * device should respond to config requests, but it may respond 1221 * with Request Retry Status (RRS) if it needs more time to 1222 * initialize. 1223 * 1224 * If the device is below a Root Port with Configuration RRS 1225 * Software Visibility enabled, reading the Vendor ID returns a 1226 * special data value if the device responded with RRS. Read the 1227 * Vendor ID until we get non-RRS status. 1228 * 1229 * If there's no Root Port or Configuration RRS Software Visibility 1230 * is not enabled, the device may still respond with RRS, but 1231 * hardware may retry the config request. If no retries receive 1232 * Successful Completion, hardware generally synthesizes ~0 1233 * (PCI_ERROR_RESPONSE) data to complete the read. Reading Vendor 1234 * ID for VFs and non-existent devices also returns ~0, so read the 1235 * Command register until it returns something other than ~0. 1236 */ 1237 for (;;) { 1238 u32 id; 1239 1240 if (pci_dev_is_disconnected(dev)) { 1241 pci_dbg(dev, "disconnected; not waiting\n"); 1242 return -ENOTTY; 1243 } 1244 1245 if (root && root->config_rrs_sv) { 1246 pci_read_config_dword(dev, PCI_VENDOR_ID, &id); 1247 if (!pci_bus_rrs_vendor_id(id)) 1248 break; 1249 } else { 1250 pci_read_config_dword(dev, PCI_COMMAND, &id); 1251 if (!PCI_POSSIBLE_ERROR(id)) 1252 break; 1253 } 1254 1255 if (delay > timeout) { 1256 pci_warn(dev, "not ready %dms after %s; giving up\n", 1257 delay - 1, reset_type); 1258 return -ENOTTY; 1259 } 1260 1261 if (delay > PCI_RESET_WAIT) { 1262 if (retrain) { 1263 retrain = false; 1264 if (pcie_failed_link_retrain(bridge) == 0) { 1265 delay = 1; 1266 continue; 1267 } 1268 } 1269 pci_info(dev, "not ready %dms after %s; waiting\n", 1270 delay - 1, reset_type); 1271 } 1272 1273 msleep(delay); 1274 delay *= 2; 1275 } 1276 1277 if (delay > PCI_RESET_WAIT) 1278 pci_info(dev, "ready %dms after %s\n", delay - 1, 1279 reset_type); 1280 else 1281 pci_dbg(dev, "ready %dms after %s\n", delay - 1, 1282 reset_type); 1283 1284 return 0; 1285 } 1286 1287 /** 1288 * pci_power_up - Put the given device into D0 1289 * @dev: PCI device to power up 1290 * 1291 * On success, return 0 or 1, depending on whether or not it is necessary to 1292 * restore the device's BARs subsequently (1 is returned in that case). 1293 * 1294 * On failure, return a negative error code. Always return failure if @dev 1295 * lacks a Power Management Capability, even if the platform was able to 1296 * put the device in D0 via non-PCI means. 1297 */ 1298 int pci_power_up(struct pci_dev *dev) 1299 { 1300 bool need_restore; 1301 pci_power_t state; 1302 u16 pmcsr; 1303 1304 platform_pci_set_power_state(dev, PCI_D0); 1305 1306 if (!dev->pm_cap) { 1307 state = platform_pci_get_power_state(dev); 1308 if (state == PCI_UNKNOWN) 1309 dev->current_state = PCI_D0; 1310 else 1311 dev->current_state = state; 1312 1313 return -EIO; 1314 } 1315 1316 if (pci_dev_is_disconnected(dev)) { 1317 dev->current_state = PCI_D3cold; 1318 return -EIO; 1319 } 1320 1321 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 1322 if (PCI_POSSIBLE_ERROR(pmcsr)) { 1323 pci_err(dev, "Unable to change power state from %s to D0, device inaccessible\n", 1324 pci_power_name(dev->current_state)); 1325 dev->current_state = PCI_D3cold; 1326 return -EIO; 1327 } 1328 1329 state = pmcsr & PCI_PM_CTRL_STATE_MASK; 1330 1331 need_restore = (state == PCI_D3hot || dev->current_state >= PCI_D3hot) && 1332 !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET); 1333 1334 if (state == PCI_D0) 1335 goto end; 1336 1337 /* 1338 * Force the entire word to 0. This doesn't affect PME_Status, disables 1339 * PME_En, and sets PowerState to 0. 1340 */ 1341 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, 0); 1342 1343 /* Mandatory transition delays; see PCI PM 1.2. */ 1344 if (state == PCI_D3hot) 1345 pci_dev_d3_sleep(dev); 1346 else if (state == PCI_D2) 1347 udelay(PCI_PM_D2_DELAY); 1348 1349 end: 1350 dev->current_state = PCI_D0; 1351 if (need_restore) 1352 return 1; 1353 1354 return 0; 1355 } 1356 1357 /** 1358 * pci_set_full_power_state - Put a PCI device into D0 and update its state 1359 * @dev: PCI device to power up 1360 * @locked: whether pci_bus_sem is held 1361 * 1362 * Call pci_power_up() to put @dev into D0, read from its PCI_PM_CTRL register 1363 * to confirm the state change, restore its BARs if they might be lost and 1364 * reconfigure ASPM in accordance with the new power state. 1365 * 1366 * If pci_restore_state() is going to be called right after a power state change 1367 * to D0, it is more efficient to use pci_power_up() directly instead of this 1368 * function. 1369 */ 1370 static int pci_set_full_power_state(struct pci_dev *dev, bool locked) 1371 { 1372 u16 pmcsr; 1373 int ret; 1374 1375 ret = pci_power_up(dev); 1376 if (ret < 0) { 1377 if (dev->current_state == PCI_D0) 1378 return 0; 1379 1380 return ret; 1381 } 1382 1383 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 1384 dev->current_state = pmcsr & PCI_PM_CTRL_STATE_MASK; 1385 if (dev->current_state != PCI_D0) { 1386 pci_info_ratelimited(dev, "Refused to change power state from %s to D0\n", 1387 pci_power_name(dev->current_state)); 1388 } else if (ret > 0) { 1389 /* 1390 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT 1391 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning 1392 * from D3hot to D0 _may_ perform an internal reset, thereby 1393 * going to "D0 Uninitialized" rather than "D0 Initialized". 1394 * For example, at least some versions of the 3c905B and the 1395 * 3c556B exhibit this behaviour. 1396 * 1397 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave 1398 * devices in a D3hot state at boot. Consequently, we need to 1399 * restore at least the BARs so that the device will be 1400 * accessible to its driver. 1401 */ 1402 pci_restore_bars(dev); 1403 } 1404 1405 if (dev->bus->self) 1406 pcie_aspm_pm_state_change(dev->bus->self, locked); 1407 1408 return 0; 1409 } 1410 1411 /** 1412 * __pci_dev_set_current_state - Set current state of a PCI device 1413 * @dev: Device to handle 1414 * @data: pointer to state to be set 1415 */ 1416 static int __pci_dev_set_current_state(struct pci_dev *dev, void *data) 1417 { 1418 pci_power_t state = *(pci_power_t *)data; 1419 1420 dev->current_state = state; 1421 return 0; 1422 } 1423 1424 /** 1425 * pci_bus_set_current_state - Walk given bus and set current state of devices 1426 * @bus: Top bus of the subtree to walk. 1427 * @state: state to be set 1428 */ 1429 void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state) 1430 { 1431 if (bus) 1432 pci_walk_bus(bus, __pci_dev_set_current_state, &state); 1433 } 1434 1435 static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state, bool locked) 1436 { 1437 if (!bus) 1438 return; 1439 1440 if (locked) 1441 pci_walk_bus_locked(bus, __pci_dev_set_current_state, &state); 1442 else 1443 pci_walk_bus(bus, __pci_dev_set_current_state, &state); 1444 } 1445 1446 /** 1447 * pci_set_low_power_state - Put a PCI device into a low-power state. 1448 * @dev: PCI device to handle. 1449 * @state: PCI power state (D1, D2, D3hot) to put the device into. 1450 * @locked: whether pci_bus_sem is held 1451 * 1452 * Use the device's PCI_PM_CTRL register to put it into a low-power state. 1453 * 1454 * RETURN VALUE: 1455 * -EINVAL if the requested state is invalid. 1456 * -EIO if device does not support PCI PM or its PM capabilities register has a 1457 * wrong version, or device doesn't support the requested state. 1458 * 0 if device already is in the requested state. 1459 * 0 if device's power state has been successfully changed. 1460 */ 1461 static int pci_set_low_power_state(struct pci_dev *dev, pci_power_t state, bool locked) 1462 { 1463 u16 pmcsr; 1464 1465 if (!dev->pm_cap) 1466 return -EIO; 1467 1468 /* 1469 * Validate transition: We can enter D0 from any state, but if 1470 * we're already in a low-power state, we can only go deeper. E.g., 1471 * we can go from D1 to D3, but we can't go directly from D3 to D1; 1472 * we'd have to go from D3 to D0, then to D1. 1473 */ 1474 if (dev->current_state <= PCI_D3cold && dev->current_state > state) { 1475 pci_dbg(dev, "Invalid power transition (from %s to %s)\n", 1476 pci_power_name(dev->current_state), 1477 pci_power_name(state)); 1478 return -EINVAL; 1479 } 1480 1481 /* Check if this device supports the desired state */ 1482 if ((state == PCI_D1 && !dev->d1_support) 1483 || (state == PCI_D2 && !dev->d2_support)) 1484 return -EIO; 1485 1486 if (dev->current_state == state) 1487 return 0; 1488 1489 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 1490 if (PCI_POSSIBLE_ERROR(pmcsr)) { 1491 pci_err(dev, "Unable to change power state from %s to %s, device inaccessible\n", 1492 pci_power_name(dev->current_state), 1493 pci_power_name(state)); 1494 dev->current_state = PCI_D3cold; 1495 return -EIO; 1496 } 1497 1498 pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 1499 pmcsr |= state; 1500 1501 /* Enter specified state */ 1502 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); 1503 1504 /* Mandatory power management transition delays; see PCI PM 1.2. */ 1505 if (state == PCI_D3hot) 1506 pci_dev_d3_sleep(dev); 1507 else if (state == PCI_D2) 1508 udelay(PCI_PM_D2_DELAY); 1509 1510 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 1511 dev->current_state = pmcsr & PCI_PM_CTRL_STATE_MASK; 1512 if (dev->current_state != state) 1513 pci_info_ratelimited(dev, "Refused to change power state from %s to %s\n", 1514 pci_power_name(dev->current_state), 1515 pci_power_name(state)); 1516 1517 if (dev->bus->self) 1518 pcie_aspm_pm_state_change(dev->bus->self, locked); 1519 1520 return 0; 1521 } 1522 1523 static int __pci_set_power_state(struct pci_dev *dev, pci_power_t state, bool locked) 1524 { 1525 int error; 1526 1527 /* Bound the state we're entering */ 1528 if (state > PCI_D3cold) 1529 state = PCI_D3cold; 1530 else if (state < PCI_D0) 1531 state = PCI_D0; 1532 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev)) 1533 1534 /* 1535 * If the device or the parent bridge do not support PCI 1536 * PM, ignore the request if we're doing anything other 1537 * than putting it into D0 (which would only happen on 1538 * boot). 1539 */ 1540 return 0; 1541 1542 /* Check if we're already there */ 1543 if (dev->current_state == state) 1544 return 0; 1545 1546 if (state == PCI_D0) 1547 return pci_set_full_power_state(dev, locked); 1548 1549 /* 1550 * This device is quirked not to be put into D3, so don't put it in 1551 * D3 1552 */ 1553 if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3)) 1554 return 0; 1555 1556 if (state == PCI_D3cold) { 1557 /* 1558 * To put the device in D3cold, put it into D3hot in the native 1559 * way, then put it into D3cold using platform ops. 1560 */ 1561 error = pci_set_low_power_state(dev, PCI_D3hot, locked); 1562 1563 if (pci_platform_power_transition(dev, PCI_D3cold)) 1564 return error; 1565 1566 /* Powering off a bridge may power off the whole hierarchy */ 1567 if (dev->current_state == PCI_D3cold) 1568 __pci_bus_set_current_state(dev->subordinate, PCI_D3cold, locked); 1569 } else { 1570 error = pci_set_low_power_state(dev, state, locked); 1571 1572 if (pci_platform_power_transition(dev, state)) 1573 return error; 1574 } 1575 1576 return 0; 1577 } 1578 1579 /** 1580 * pci_set_power_state - Set the power state of a PCI device 1581 * @dev: PCI device to handle. 1582 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into. 1583 * 1584 * Transition a device to a new power state, using the platform firmware and/or 1585 * the device's PCI PM registers. 1586 * 1587 * RETURN VALUE: 1588 * -EINVAL if the requested state is invalid. 1589 * -EIO if device does not support PCI PM or its PM capabilities register has a 1590 * wrong version, or device doesn't support the requested state. 1591 * 0 if the transition is to D1 or D2 but D1 and D2 are not supported. 1592 * 0 if device already is in the requested state. 1593 * 0 if the transition is to D3 but D3 is not supported. 1594 * 0 if device's power state has been successfully changed. 1595 */ 1596 int pci_set_power_state(struct pci_dev *dev, pci_power_t state) 1597 { 1598 return __pci_set_power_state(dev, state, false); 1599 } 1600 EXPORT_SYMBOL(pci_set_power_state); 1601 1602 int pci_set_power_state_locked(struct pci_dev *dev, pci_power_t state) 1603 { 1604 lockdep_assert_held(&pci_bus_sem); 1605 1606 return __pci_set_power_state(dev, state, true); 1607 } 1608 EXPORT_SYMBOL(pci_set_power_state_locked); 1609 1610 #define PCI_EXP_SAVE_REGS 7 1611 1612 static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev, 1613 u16 cap, bool extended) 1614 { 1615 struct pci_cap_saved_state *tmp; 1616 1617 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) { 1618 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap) 1619 return tmp; 1620 } 1621 return NULL; 1622 } 1623 1624 struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap) 1625 { 1626 return _pci_find_saved_cap(dev, cap, false); 1627 } 1628 1629 struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap) 1630 { 1631 return _pci_find_saved_cap(dev, cap, true); 1632 } 1633 1634 static int pci_save_pcie_state(struct pci_dev *dev) 1635 { 1636 int i = 0; 1637 struct pci_cap_saved_state *save_state; 1638 u16 *cap; 1639 1640 if (!pci_is_pcie(dev)) 1641 return 0; 1642 1643 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); 1644 if (!save_state) { 1645 pci_err(dev, "buffer not found in %s\n", __func__); 1646 return -ENOMEM; 1647 } 1648 1649 cap = (u16 *)&save_state->cap.data[0]; 1650 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]); 1651 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]); 1652 pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]); 1653 pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]); 1654 pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]); 1655 pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]); 1656 pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]); 1657 1658 pci_save_aspm_l1ss_state(dev); 1659 pci_save_ltr_state(dev); 1660 1661 return 0; 1662 } 1663 1664 static void pci_restore_pcie_state(struct pci_dev *dev) 1665 { 1666 int i = 0; 1667 struct pci_cap_saved_state *save_state; 1668 u16 *cap; 1669 1670 /* 1671 * Restore max latencies (in the LTR capability) before enabling 1672 * LTR itself in PCI_EXP_DEVCTL2. 1673 */ 1674 pci_restore_ltr_state(dev); 1675 pci_restore_aspm_l1ss_state(dev); 1676 1677 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); 1678 if (!save_state) 1679 return; 1680 1681 /* 1682 * Downstream ports reset the LTR enable bit when link goes down. 1683 * Check and re-configure the bit here before restoring device. 1684 * PCIe r5.0, sec 7.5.3.16. 1685 */ 1686 pci_bridge_reconfigure_ltr(dev); 1687 1688 cap = (u16 *)&save_state->cap.data[0]; 1689 pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]); 1690 pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]); 1691 pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]); 1692 pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]); 1693 pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]); 1694 pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]); 1695 pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]); 1696 } 1697 1698 static int pci_save_pcix_state(struct pci_dev *dev) 1699 { 1700 int pos; 1701 struct pci_cap_saved_state *save_state; 1702 1703 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); 1704 if (!pos) 1705 return 0; 1706 1707 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX); 1708 if (!save_state) { 1709 pci_err(dev, "buffer not found in %s\n", __func__); 1710 return -ENOMEM; 1711 } 1712 1713 pci_read_config_word(dev, pos + PCI_X_CMD, 1714 (u16 *)save_state->cap.data); 1715 1716 return 0; 1717 } 1718 1719 static void pci_restore_pcix_state(struct pci_dev *dev) 1720 { 1721 int i = 0, pos; 1722 struct pci_cap_saved_state *save_state; 1723 u16 *cap; 1724 1725 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX); 1726 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); 1727 if (!save_state || !pos) 1728 return; 1729 cap = (u16 *)&save_state->cap.data[0]; 1730 1731 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]); 1732 } 1733 1734 /** 1735 * pci_save_state - save the PCI configuration space of a device before 1736 * suspending 1737 * @dev: PCI device that we're dealing with 1738 */ 1739 int pci_save_state(struct pci_dev *dev) 1740 { 1741 int i; 1742 /* XXX: 100% dword access ok here? */ 1743 for (i = 0; i < 16; i++) { 1744 pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]); 1745 pci_dbg(dev, "save config %#04x: %#010x\n", 1746 i * 4, dev->saved_config_space[i]); 1747 } 1748 dev->state_saved = true; 1749 1750 i = pci_save_pcie_state(dev); 1751 if (i != 0) 1752 return i; 1753 1754 i = pci_save_pcix_state(dev); 1755 if (i != 0) 1756 return i; 1757 1758 pci_save_dpc_state(dev); 1759 pci_save_aer_state(dev); 1760 pci_save_ptm_state(dev); 1761 pci_save_tph_state(dev); 1762 return pci_save_vc_state(dev); 1763 } 1764 EXPORT_SYMBOL(pci_save_state); 1765 1766 static void pci_restore_config_dword(struct pci_dev *pdev, int offset, 1767 u32 saved_val, int retry, bool force) 1768 { 1769 u32 val; 1770 1771 pci_read_config_dword(pdev, offset, &val); 1772 if (!force && val == saved_val) 1773 return; 1774 1775 for (;;) { 1776 pci_dbg(pdev, "restore config %#04x: %#010x -> %#010x\n", 1777 offset, val, saved_val); 1778 pci_write_config_dword(pdev, offset, saved_val); 1779 if (retry-- <= 0) 1780 return; 1781 1782 pci_read_config_dword(pdev, offset, &val); 1783 if (val == saved_val) 1784 return; 1785 1786 mdelay(1); 1787 } 1788 } 1789 1790 static void pci_restore_config_space_range(struct pci_dev *pdev, 1791 int start, int end, int retry, 1792 bool force) 1793 { 1794 int index; 1795 1796 for (index = end; index >= start; index--) 1797 pci_restore_config_dword(pdev, 4 * index, 1798 pdev->saved_config_space[index], 1799 retry, force); 1800 } 1801 1802 static void pci_restore_config_space(struct pci_dev *pdev) 1803 { 1804 if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) { 1805 pci_restore_config_space_range(pdev, 10, 15, 0, false); 1806 /* Restore BARs before the command register. */ 1807 pci_restore_config_space_range(pdev, 4, 9, 10, false); 1808 pci_restore_config_space_range(pdev, 0, 3, 0, false); 1809 } else if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE) { 1810 pci_restore_config_space_range(pdev, 12, 15, 0, false); 1811 1812 /* 1813 * Force rewriting of prefetch registers to avoid S3 resume 1814 * issues on Intel PCI bridges that occur when these 1815 * registers are not explicitly written. 1816 */ 1817 pci_restore_config_space_range(pdev, 9, 11, 0, true); 1818 pci_restore_config_space_range(pdev, 0, 8, 0, false); 1819 } else { 1820 pci_restore_config_space_range(pdev, 0, 15, 0, false); 1821 } 1822 } 1823 1824 /** 1825 * pci_restore_state - Restore the saved state of a PCI device 1826 * @dev: PCI device that we're dealing with 1827 */ 1828 void pci_restore_state(struct pci_dev *dev) 1829 { 1830 pci_restore_pcie_state(dev); 1831 pci_restore_pasid_state(dev); 1832 pci_restore_pri_state(dev); 1833 pci_restore_ats_state(dev); 1834 pci_restore_vc_state(dev); 1835 pci_restore_rebar_state(dev); 1836 pci_restore_dpc_state(dev); 1837 pci_restore_ptm_state(dev); 1838 pci_restore_tph_state(dev); 1839 1840 pci_aer_clear_status(dev); 1841 pci_restore_aer_state(dev); 1842 1843 pci_restore_config_space(dev); 1844 1845 pci_restore_pcix_state(dev); 1846 pci_restore_msi_state(dev); 1847 1848 /* Restore ACS and IOV configuration state */ 1849 pci_enable_acs(dev); 1850 pci_restore_iov_state(dev); 1851 1852 dev->state_saved = false; 1853 } 1854 EXPORT_SYMBOL(pci_restore_state); 1855 1856 struct pci_saved_state { 1857 u32 config_space[16]; 1858 struct pci_cap_saved_data cap[]; 1859 }; 1860 1861 /** 1862 * pci_store_saved_state - Allocate and return an opaque struct containing 1863 * the device saved state. 1864 * @dev: PCI device that we're dealing with 1865 * 1866 * Return NULL if no state or error. 1867 */ 1868 struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev) 1869 { 1870 struct pci_saved_state *state; 1871 struct pci_cap_saved_state *tmp; 1872 struct pci_cap_saved_data *cap; 1873 size_t size; 1874 1875 if (!dev->state_saved) 1876 return NULL; 1877 1878 size = sizeof(*state) + sizeof(struct pci_cap_saved_data); 1879 1880 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) 1881 size += sizeof(struct pci_cap_saved_data) + tmp->cap.size; 1882 1883 state = kzalloc(size, GFP_KERNEL); 1884 if (!state) 1885 return NULL; 1886 1887 memcpy(state->config_space, dev->saved_config_space, 1888 sizeof(state->config_space)); 1889 1890 cap = state->cap; 1891 hlist_for_each_entry(tmp, &dev->saved_cap_space, next) { 1892 size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size; 1893 memcpy(cap, &tmp->cap, len); 1894 cap = (struct pci_cap_saved_data *)((u8 *)cap + len); 1895 } 1896 /* Empty cap_save terminates list */ 1897 1898 return state; 1899 } 1900 EXPORT_SYMBOL_GPL(pci_store_saved_state); 1901 1902 /** 1903 * pci_load_saved_state - Reload the provided save state into struct pci_dev. 1904 * @dev: PCI device that we're dealing with 1905 * @state: Saved state returned from pci_store_saved_state() 1906 */ 1907 int pci_load_saved_state(struct pci_dev *dev, 1908 struct pci_saved_state *state) 1909 { 1910 struct pci_cap_saved_data *cap; 1911 1912 dev->state_saved = false; 1913 1914 if (!state) 1915 return 0; 1916 1917 memcpy(dev->saved_config_space, state->config_space, 1918 sizeof(state->config_space)); 1919 1920 cap = state->cap; 1921 while (cap->size) { 1922 struct pci_cap_saved_state *tmp; 1923 1924 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended); 1925 if (!tmp || tmp->cap.size != cap->size) 1926 return -EINVAL; 1927 1928 memcpy(tmp->cap.data, cap->data, tmp->cap.size); 1929 cap = (struct pci_cap_saved_data *)((u8 *)cap + 1930 sizeof(struct pci_cap_saved_data) + cap->size); 1931 } 1932 1933 dev->state_saved = true; 1934 return 0; 1935 } 1936 EXPORT_SYMBOL_GPL(pci_load_saved_state); 1937 1938 /** 1939 * pci_load_and_free_saved_state - Reload the save state pointed to by state, 1940 * and free the memory allocated for it. 1941 * @dev: PCI device that we're dealing with 1942 * @state: Pointer to saved state returned from pci_store_saved_state() 1943 */ 1944 int pci_load_and_free_saved_state(struct pci_dev *dev, 1945 struct pci_saved_state **state) 1946 { 1947 int ret = pci_load_saved_state(dev, *state); 1948 kfree(*state); 1949 *state = NULL; 1950 return ret; 1951 } 1952 EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state); 1953 1954 int __weak pcibios_enable_device(struct pci_dev *dev, int bars) 1955 { 1956 return pci_enable_resources(dev, bars); 1957 } 1958 1959 static int pci_host_bridge_enable_device(struct pci_dev *dev) 1960 { 1961 struct pci_host_bridge *host_bridge = pci_find_host_bridge(dev->bus); 1962 int err; 1963 1964 if (host_bridge && host_bridge->enable_device) { 1965 err = host_bridge->enable_device(host_bridge, dev); 1966 if (err) 1967 return err; 1968 } 1969 1970 return 0; 1971 } 1972 1973 static void pci_host_bridge_disable_device(struct pci_dev *dev) 1974 { 1975 struct pci_host_bridge *host_bridge = pci_find_host_bridge(dev->bus); 1976 1977 if (host_bridge && host_bridge->disable_device) 1978 host_bridge->disable_device(host_bridge, dev); 1979 } 1980 1981 static int do_pci_enable_device(struct pci_dev *dev, int bars) 1982 { 1983 int err; 1984 struct pci_dev *bridge; 1985 u16 cmd; 1986 u8 pin; 1987 1988 err = pci_set_power_state(dev, PCI_D0); 1989 if (err < 0 && err != -EIO) 1990 return err; 1991 1992 bridge = pci_upstream_bridge(dev); 1993 if (bridge) 1994 pcie_aspm_powersave_config_link(bridge); 1995 1996 err = pci_host_bridge_enable_device(dev); 1997 if (err) 1998 return err; 1999 2000 err = pcibios_enable_device(dev, bars); 2001 if (err < 0) 2002 goto err_enable; 2003 pci_fixup_device(pci_fixup_enable, dev); 2004 2005 if (dev->msi_enabled || dev->msix_enabled) 2006 return 0; 2007 2008 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin); 2009 if (pin) { 2010 pci_read_config_word(dev, PCI_COMMAND, &cmd); 2011 if (cmd & PCI_COMMAND_INTX_DISABLE) 2012 pci_write_config_word(dev, PCI_COMMAND, 2013 cmd & ~PCI_COMMAND_INTX_DISABLE); 2014 } 2015 2016 return 0; 2017 2018 err_enable: 2019 pci_host_bridge_disable_device(dev); 2020 2021 return err; 2022 2023 } 2024 2025 /** 2026 * pci_reenable_device - Resume abandoned device 2027 * @dev: PCI device to be resumed 2028 * 2029 * NOTE: This function is a backend of pci_default_resume() and is not supposed 2030 * to be called by normal code, write proper resume handler and use it instead. 2031 */ 2032 int pci_reenable_device(struct pci_dev *dev) 2033 { 2034 if (pci_is_enabled(dev)) 2035 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1); 2036 return 0; 2037 } 2038 EXPORT_SYMBOL(pci_reenable_device); 2039 2040 static void pci_enable_bridge(struct pci_dev *dev) 2041 { 2042 struct pci_dev *bridge; 2043 int retval; 2044 2045 bridge = pci_upstream_bridge(dev); 2046 if (bridge) 2047 pci_enable_bridge(bridge); 2048 2049 if (pci_is_enabled(dev)) { 2050 if (!dev->is_busmaster) 2051 pci_set_master(dev); 2052 return; 2053 } 2054 2055 retval = pci_enable_device(dev); 2056 if (retval) 2057 pci_err(dev, "Error enabling bridge (%d), continuing\n", 2058 retval); 2059 pci_set_master(dev); 2060 } 2061 2062 static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags) 2063 { 2064 struct pci_dev *bridge; 2065 int err; 2066 int i, bars = 0; 2067 2068 /* 2069 * Power state could be unknown at this point, either due to a fresh 2070 * boot or a device removal call. So get the current power state 2071 * so that things like MSI message writing will behave as expected 2072 * (e.g. if the device really is in D0 at enable time). 2073 */ 2074 pci_update_current_state(dev, dev->current_state); 2075 2076 if (atomic_inc_return(&dev->enable_cnt) > 1) 2077 return 0; /* already enabled */ 2078 2079 bridge = pci_upstream_bridge(dev); 2080 if (bridge) 2081 pci_enable_bridge(bridge); 2082 2083 /* only skip sriov related */ 2084 for (i = 0; i <= PCI_ROM_RESOURCE; i++) 2085 if (dev->resource[i].flags & flags) 2086 bars |= (1 << i); 2087 for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++) 2088 if (dev->resource[i].flags & flags) 2089 bars |= (1 << i); 2090 2091 err = do_pci_enable_device(dev, bars); 2092 if (err < 0) 2093 atomic_dec(&dev->enable_cnt); 2094 return err; 2095 } 2096 2097 /** 2098 * pci_enable_device_mem - Initialize a device for use with Memory space 2099 * @dev: PCI device to be initialized 2100 * 2101 * Initialize device before it's used by a driver. Ask low-level code 2102 * to enable Memory resources. Wake up the device if it was suspended. 2103 * Beware, this function can fail. 2104 */ 2105 int pci_enable_device_mem(struct pci_dev *dev) 2106 { 2107 return pci_enable_device_flags(dev, IORESOURCE_MEM); 2108 } 2109 EXPORT_SYMBOL(pci_enable_device_mem); 2110 2111 /** 2112 * pci_enable_device - Initialize device before it's used by a driver. 2113 * @dev: PCI device to be initialized 2114 * 2115 * Initialize device before it's used by a driver. Ask low-level code 2116 * to enable I/O and memory. Wake up the device if it was suspended. 2117 * Beware, this function can fail. 2118 * 2119 * Note we don't actually enable the device many times if we call 2120 * this function repeatedly (we just increment the count). 2121 */ 2122 int pci_enable_device(struct pci_dev *dev) 2123 { 2124 return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO); 2125 } 2126 EXPORT_SYMBOL(pci_enable_device); 2127 2128 /* 2129 * pcibios_device_add - provide arch specific hooks when adding device dev 2130 * @dev: the PCI device being added 2131 * 2132 * Permits the platform to provide architecture specific functionality when 2133 * devices are added. This is the default implementation. Architecture 2134 * implementations can override this. 2135 */ 2136 int __weak pcibios_device_add(struct pci_dev *dev) 2137 { 2138 return 0; 2139 } 2140 2141 /** 2142 * pcibios_release_device - provide arch specific hooks when releasing 2143 * device dev 2144 * @dev: the PCI device being released 2145 * 2146 * Permits the platform to provide architecture specific functionality when 2147 * devices are released. This is the default implementation. Architecture 2148 * implementations can override this. 2149 */ 2150 void __weak pcibios_release_device(struct pci_dev *dev) {} 2151 2152 /** 2153 * pcibios_disable_device - disable arch specific PCI resources for device dev 2154 * @dev: the PCI device to disable 2155 * 2156 * Disables architecture specific PCI resources for the device. This 2157 * is the default implementation. Architecture implementations can 2158 * override this. 2159 */ 2160 void __weak pcibios_disable_device(struct pci_dev *dev) {} 2161 2162 static void do_pci_disable_device(struct pci_dev *dev) 2163 { 2164 u16 pci_command; 2165 2166 pci_read_config_word(dev, PCI_COMMAND, &pci_command); 2167 if (pci_command & PCI_COMMAND_MASTER) { 2168 pci_command &= ~PCI_COMMAND_MASTER; 2169 pci_write_config_word(dev, PCI_COMMAND, pci_command); 2170 } 2171 2172 pcibios_disable_device(dev); 2173 } 2174 2175 /** 2176 * pci_disable_enabled_device - Disable device without updating enable_cnt 2177 * @dev: PCI device to disable 2178 * 2179 * NOTE: This function is a backend of PCI power management routines and is 2180 * not supposed to be called drivers. 2181 */ 2182 void pci_disable_enabled_device(struct pci_dev *dev) 2183 { 2184 if (pci_is_enabled(dev)) 2185 do_pci_disable_device(dev); 2186 } 2187 2188 /** 2189 * pci_disable_device - Disable PCI device after use 2190 * @dev: PCI device to be disabled 2191 * 2192 * Signal to the system that the PCI device is not in use by the system 2193 * anymore. This only involves disabling PCI bus-mastering, if active. 2194 * 2195 * Note we don't actually disable the device until all callers of 2196 * pci_enable_device() have called pci_disable_device(). 2197 */ 2198 void pci_disable_device(struct pci_dev *dev) 2199 { 2200 dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0, 2201 "disabling already-disabled device"); 2202 2203 if (atomic_dec_return(&dev->enable_cnt) != 0) 2204 return; 2205 2206 pci_host_bridge_disable_device(dev); 2207 2208 do_pci_disable_device(dev); 2209 2210 dev->is_busmaster = 0; 2211 } 2212 EXPORT_SYMBOL(pci_disable_device); 2213 2214 /** 2215 * pcibios_set_pcie_reset_state - set reset state for device dev 2216 * @dev: the PCIe device reset 2217 * @state: Reset state to enter into 2218 * 2219 * Set the PCIe reset state for the device. This is the default 2220 * implementation. Architecture implementations can override this. 2221 */ 2222 int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev, 2223 enum pcie_reset_state state) 2224 { 2225 return -EINVAL; 2226 } 2227 2228 /** 2229 * pci_set_pcie_reset_state - set reset state for device dev 2230 * @dev: the PCIe device reset 2231 * @state: Reset state to enter into 2232 * 2233 * Sets the PCI reset state for the device. 2234 */ 2235 int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state) 2236 { 2237 return pcibios_set_pcie_reset_state(dev, state); 2238 } 2239 EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state); 2240 2241 #ifdef CONFIG_PCIEAER 2242 void pcie_clear_device_status(struct pci_dev *dev) 2243 { 2244 u16 sta; 2245 2246 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &sta); 2247 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, sta); 2248 } 2249 #endif 2250 2251 /** 2252 * pcie_clear_root_pme_status - Clear root port PME interrupt status. 2253 * @dev: PCIe root port or event collector. 2254 */ 2255 void pcie_clear_root_pme_status(struct pci_dev *dev) 2256 { 2257 pcie_capability_write_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME); 2258 } 2259 2260 /** 2261 * pci_check_pme_status - Check if given device has generated PME. 2262 * @dev: Device to check. 2263 * 2264 * Check the PME status of the device and if set, clear it and clear PME enable 2265 * (if set). Return 'true' if PME status and PME enable were both set or 2266 * 'false' otherwise. 2267 */ 2268 bool pci_check_pme_status(struct pci_dev *dev) 2269 { 2270 int pmcsr_pos; 2271 u16 pmcsr; 2272 bool ret = false; 2273 2274 if (!dev->pm_cap) 2275 return false; 2276 2277 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL; 2278 pci_read_config_word(dev, pmcsr_pos, &pmcsr); 2279 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS)) 2280 return false; 2281 2282 /* Clear PME status. */ 2283 pmcsr |= PCI_PM_CTRL_PME_STATUS; 2284 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) { 2285 /* Disable PME to avoid interrupt flood. */ 2286 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; 2287 ret = true; 2288 } 2289 2290 pci_write_config_word(dev, pmcsr_pos, pmcsr); 2291 2292 return ret; 2293 } 2294 2295 /** 2296 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set. 2297 * @dev: Device to handle. 2298 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag. 2299 * 2300 * Check if @dev has generated PME and queue a resume request for it in that 2301 * case. 2302 */ 2303 static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset) 2304 { 2305 if (pme_poll_reset && dev->pme_poll) 2306 dev->pme_poll = false; 2307 2308 if (pci_check_pme_status(dev)) { 2309 pci_wakeup_event(dev); 2310 pm_request_resume(&dev->dev); 2311 } 2312 return 0; 2313 } 2314 2315 /** 2316 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary. 2317 * @bus: Top bus of the subtree to walk. 2318 */ 2319 void pci_pme_wakeup_bus(struct pci_bus *bus) 2320 { 2321 if (bus) 2322 pci_walk_bus(bus, pci_pme_wakeup, (void *)true); 2323 } 2324 2325 2326 /** 2327 * pci_pme_capable - check the capability of PCI device to generate PME# 2328 * @dev: PCI device to handle. 2329 * @state: PCI state from which device will issue PME#. 2330 */ 2331 bool pci_pme_capable(struct pci_dev *dev, pci_power_t state) 2332 { 2333 if (!dev->pm_cap) 2334 return false; 2335 2336 return !!(dev->pme_support & (1 << state)); 2337 } 2338 EXPORT_SYMBOL(pci_pme_capable); 2339 2340 static void pci_pme_list_scan(struct work_struct *work) 2341 { 2342 struct pci_pme_device *pme_dev, *n; 2343 2344 mutex_lock(&pci_pme_list_mutex); 2345 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) { 2346 struct pci_dev *pdev = pme_dev->dev; 2347 2348 if (pdev->pme_poll) { 2349 struct pci_dev *bridge = pdev->bus->self; 2350 struct device *dev = &pdev->dev; 2351 struct device *bdev = bridge ? &bridge->dev : NULL; 2352 int bref = 0; 2353 2354 /* 2355 * If we have a bridge, it should be in an active/D0 2356 * state or the configuration space of subordinate 2357 * devices may not be accessible or stable over the 2358 * course of the call. 2359 */ 2360 if (bdev) { 2361 bref = pm_runtime_get_if_active(bdev); 2362 if (!bref) 2363 continue; 2364 2365 if (bridge->current_state != PCI_D0) 2366 goto put_bridge; 2367 } 2368 2369 /* 2370 * The device itself should be suspended but config 2371 * space must be accessible, therefore it cannot be in 2372 * D3cold. 2373 */ 2374 if (pm_runtime_suspended(dev) && 2375 pdev->current_state != PCI_D3cold) 2376 pci_pme_wakeup(pdev, NULL); 2377 2378 put_bridge: 2379 if (bref > 0) 2380 pm_runtime_put(bdev); 2381 } else { 2382 list_del(&pme_dev->list); 2383 kfree(pme_dev); 2384 } 2385 } 2386 if (!list_empty(&pci_pme_list)) 2387 queue_delayed_work(system_freezable_wq, &pci_pme_work, 2388 msecs_to_jiffies(PME_TIMEOUT)); 2389 mutex_unlock(&pci_pme_list_mutex); 2390 } 2391 2392 static void __pci_pme_active(struct pci_dev *dev, bool enable) 2393 { 2394 u16 pmcsr; 2395 2396 if (!dev->pme_support) 2397 return; 2398 2399 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 2400 /* Clear PME_Status by writing 1 to it and enable PME# */ 2401 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE; 2402 if (!enable) 2403 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; 2404 2405 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); 2406 } 2407 2408 /** 2409 * pci_pme_restore - Restore PME configuration after config space restore. 2410 * @dev: PCI device to update. 2411 */ 2412 void pci_pme_restore(struct pci_dev *dev) 2413 { 2414 u16 pmcsr; 2415 2416 if (!dev->pme_support) 2417 return; 2418 2419 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 2420 if (dev->wakeup_prepared) { 2421 pmcsr |= PCI_PM_CTRL_PME_ENABLE; 2422 pmcsr &= ~PCI_PM_CTRL_PME_STATUS; 2423 } else { 2424 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; 2425 pmcsr |= PCI_PM_CTRL_PME_STATUS; 2426 } 2427 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); 2428 } 2429 2430 /** 2431 * pci_pme_active - enable or disable PCI device's PME# function 2432 * @dev: PCI device to handle. 2433 * @enable: 'true' to enable PME# generation; 'false' to disable it. 2434 * 2435 * The caller must verify that the device is capable of generating PME# before 2436 * calling this function with @enable equal to 'true'. 2437 */ 2438 void pci_pme_active(struct pci_dev *dev, bool enable) 2439 { 2440 __pci_pme_active(dev, enable); 2441 2442 /* 2443 * PCI (as opposed to PCIe) PME requires that the device have 2444 * its PME# line hooked up correctly. Not all hardware vendors 2445 * do this, so the PME never gets delivered and the device 2446 * remains asleep. The easiest way around this is to 2447 * periodically walk the list of suspended devices and check 2448 * whether any have their PME flag set. The assumption is that 2449 * we'll wake up often enough anyway that this won't be a huge 2450 * hit, and the power savings from the devices will still be a 2451 * win. 2452 * 2453 * Although PCIe uses in-band PME message instead of PME# line 2454 * to report PME, PME does not work for some PCIe devices in 2455 * reality. For example, there are devices that set their PME 2456 * status bits, but don't really bother to send a PME message; 2457 * there are PCI Express Root Ports that don't bother to 2458 * trigger interrupts when they receive PME messages from the 2459 * devices below. So PME poll is used for PCIe devices too. 2460 */ 2461 2462 if (dev->pme_poll) { 2463 struct pci_pme_device *pme_dev; 2464 if (enable) { 2465 pme_dev = kmalloc(sizeof(struct pci_pme_device), 2466 GFP_KERNEL); 2467 if (!pme_dev) { 2468 pci_warn(dev, "can't enable PME#\n"); 2469 return; 2470 } 2471 pme_dev->dev = dev; 2472 mutex_lock(&pci_pme_list_mutex); 2473 list_add(&pme_dev->list, &pci_pme_list); 2474 if (list_is_singular(&pci_pme_list)) 2475 queue_delayed_work(system_freezable_wq, 2476 &pci_pme_work, 2477 msecs_to_jiffies(PME_TIMEOUT)); 2478 mutex_unlock(&pci_pme_list_mutex); 2479 } else { 2480 mutex_lock(&pci_pme_list_mutex); 2481 list_for_each_entry(pme_dev, &pci_pme_list, list) { 2482 if (pme_dev->dev == dev) { 2483 list_del(&pme_dev->list); 2484 kfree(pme_dev); 2485 break; 2486 } 2487 } 2488 mutex_unlock(&pci_pme_list_mutex); 2489 } 2490 } 2491 2492 pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled"); 2493 } 2494 EXPORT_SYMBOL(pci_pme_active); 2495 2496 /** 2497 * __pci_enable_wake - enable PCI device as wakeup event source 2498 * @dev: PCI device affected 2499 * @state: PCI state from which device will issue wakeup events 2500 * @enable: True to enable event generation; false to disable 2501 * 2502 * This enables the device as a wakeup event source, or disables it. 2503 * When such events involves platform-specific hooks, those hooks are 2504 * called automatically by this routine. 2505 * 2506 * Devices with legacy power management (no standard PCI PM capabilities) 2507 * always require such platform hooks. 2508 * 2509 * RETURN VALUE: 2510 * 0 is returned on success 2511 * -EINVAL is returned if device is not supposed to wake up the system 2512 * Error code depending on the platform is returned if both the platform and 2513 * the native mechanism fail to enable the generation of wake-up events 2514 */ 2515 static int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable) 2516 { 2517 int ret = 0; 2518 2519 /* 2520 * Bridges that are not power-manageable directly only signal 2521 * wakeup on behalf of subordinate devices which is set up 2522 * elsewhere, so skip them. However, bridges that are 2523 * power-manageable may signal wakeup for themselves (for example, 2524 * on a hotplug event) and they need to be covered here. 2525 */ 2526 if (!pci_power_manageable(dev)) 2527 return 0; 2528 2529 /* Don't do the same thing twice in a row for one device. */ 2530 if (!!enable == !!dev->wakeup_prepared) 2531 return 0; 2532 2533 /* 2534 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don 2535 * Anderson we should be doing PME# wake enable followed by ACPI wake 2536 * enable. To disable wake-up we call the platform first, for symmetry. 2537 */ 2538 2539 if (enable) { 2540 int error; 2541 2542 /* 2543 * Enable PME signaling if the device can signal PME from 2544 * D3cold regardless of whether or not it can signal PME from 2545 * the current target state, because that will allow it to 2546 * signal PME when the hierarchy above it goes into D3cold and 2547 * the device itself ends up in D3cold as a result of that. 2548 */ 2549 if (pci_pme_capable(dev, state) || pci_pme_capable(dev, PCI_D3cold)) 2550 pci_pme_active(dev, true); 2551 else 2552 ret = 1; 2553 error = platform_pci_set_wakeup(dev, true); 2554 if (ret) 2555 ret = error; 2556 if (!ret) 2557 dev->wakeup_prepared = true; 2558 } else { 2559 platform_pci_set_wakeup(dev, false); 2560 pci_pme_active(dev, false); 2561 dev->wakeup_prepared = false; 2562 } 2563 2564 return ret; 2565 } 2566 2567 /** 2568 * pci_enable_wake - change wakeup settings for a PCI device 2569 * @pci_dev: Target device 2570 * @state: PCI state from which device will issue wakeup events 2571 * @enable: Whether or not to enable event generation 2572 * 2573 * If @enable is set, check device_may_wakeup() for the device before calling 2574 * __pci_enable_wake() for it. 2575 */ 2576 int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable) 2577 { 2578 if (enable && !device_may_wakeup(&pci_dev->dev)) 2579 return -EINVAL; 2580 2581 return __pci_enable_wake(pci_dev, state, enable); 2582 } 2583 EXPORT_SYMBOL(pci_enable_wake); 2584 2585 /** 2586 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold 2587 * @dev: PCI device to prepare 2588 * @enable: True to enable wake-up event generation; false to disable 2589 * 2590 * Many drivers want the device to wake up the system from D3_hot or D3_cold 2591 * and this function allows them to set that up cleanly - pci_enable_wake() 2592 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI 2593 * ordering constraints. 2594 * 2595 * This function only returns error code if the device is not allowed to wake 2596 * up the system from sleep or it is not capable of generating PME# from both 2597 * D3_hot and D3_cold and the platform is unable to enable wake-up power for it. 2598 */ 2599 int pci_wake_from_d3(struct pci_dev *dev, bool enable) 2600 { 2601 return pci_pme_capable(dev, PCI_D3cold) ? 2602 pci_enable_wake(dev, PCI_D3cold, enable) : 2603 pci_enable_wake(dev, PCI_D3hot, enable); 2604 } 2605 EXPORT_SYMBOL(pci_wake_from_d3); 2606 2607 /** 2608 * pci_target_state - find an appropriate low power state for a given PCI dev 2609 * @dev: PCI device 2610 * @wakeup: Whether or not wakeup functionality will be enabled for the device. 2611 * 2612 * Use underlying platform code to find a supported low power state for @dev. 2613 * If the platform can't manage @dev, return the deepest state from which it 2614 * can generate wake events, based on any available PME info. 2615 */ 2616 static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup) 2617 { 2618 if (platform_pci_power_manageable(dev)) { 2619 /* 2620 * Call the platform to find the target state for the device. 2621 */ 2622 pci_power_t state = platform_pci_choose_state(dev); 2623 2624 switch (state) { 2625 case PCI_POWER_ERROR: 2626 case PCI_UNKNOWN: 2627 return PCI_D3hot; 2628 2629 case PCI_D1: 2630 case PCI_D2: 2631 if (pci_no_d1d2(dev)) 2632 return PCI_D3hot; 2633 } 2634 2635 return state; 2636 } 2637 2638 /* 2639 * If the device is in D3cold even though it's not power-manageable by 2640 * the platform, it may have been powered down by non-standard means. 2641 * Best to let it slumber. 2642 */ 2643 if (dev->current_state == PCI_D3cold) 2644 return PCI_D3cold; 2645 else if (!dev->pm_cap) 2646 return PCI_D0; 2647 2648 if (wakeup && dev->pme_support) { 2649 pci_power_t state = PCI_D3hot; 2650 2651 /* 2652 * Find the deepest state from which the device can generate 2653 * PME#. 2654 */ 2655 while (state && !(dev->pme_support & (1 << state))) 2656 state--; 2657 2658 if (state) 2659 return state; 2660 else if (dev->pme_support & 1) 2661 return PCI_D0; 2662 } 2663 2664 return PCI_D3hot; 2665 } 2666 2667 /** 2668 * pci_prepare_to_sleep - prepare PCI device for system-wide transition 2669 * into a sleep state 2670 * @dev: Device to handle. 2671 * 2672 * Choose the power state appropriate for the device depending on whether 2673 * it can wake up the system and/or is power manageable by the platform 2674 * (PCI_D3hot is the default) and put the device into that state. 2675 */ 2676 int pci_prepare_to_sleep(struct pci_dev *dev) 2677 { 2678 bool wakeup = device_may_wakeup(&dev->dev); 2679 pci_power_t target_state = pci_target_state(dev, wakeup); 2680 int error; 2681 2682 if (target_state == PCI_POWER_ERROR) 2683 return -EIO; 2684 2685 pci_enable_wake(dev, target_state, wakeup); 2686 2687 error = pci_set_power_state(dev, target_state); 2688 2689 if (error) 2690 pci_enable_wake(dev, target_state, false); 2691 2692 return error; 2693 } 2694 EXPORT_SYMBOL(pci_prepare_to_sleep); 2695 2696 /** 2697 * pci_back_from_sleep - turn PCI device on during system-wide transition 2698 * into working state 2699 * @dev: Device to handle. 2700 * 2701 * Disable device's system wake-up capability and put it into D0. 2702 */ 2703 int pci_back_from_sleep(struct pci_dev *dev) 2704 { 2705 int ret = pci_set_power_state(dev, PCI_D0); 2706 2707 if (ret) 2708 return ret; 2709 2710 pci_enable_wake(dev, PCI_D0, false); 2711 return 0; 2712 } 2713 EXPORT_SYMBOL(pci_back_from_sleep); 2714 2715 /** 2716 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend. 2717 * @dev: PCI device being suspended. 2718 * 2719 * Prepare @dev to generate wake-up events at run time and put it into a low 2720 * power state. 2721 */ 2722 int pci_finish_runtime_suspend(struct pci_dev *dev) 2723 { 2724 pci_power_t target_state; 2725 int error; 2726 2727 target_state = pci_target_state(dev, device_can_wakeup(&dev->dev)); 2728 if (target_state == PCI_POWER_ERROR) 2729 return -EIO; 2730 2731 __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev)); 2732 2733 error = pci_set_power_state(dev, target_state); 2734 2735 if (error) 2736 pci_enable_wake(dev, target_state, false); 2737 2738 return error; 2739 } 2740 2741 /** 2742 * pci_dev_run_wake - Check if device can generate run-time wake-up events. 2743 * @dev: Device to check. 2744 * 2745 * Return true if the device itself is capable of generating wake-up events 2746 * (through the platform or using the native PCIe PME) or if the device supports 2747 * PME and one of its upstream bridges can generate wake-up events. 2748 */ 2749 bool pci_dev_run_wake(struct pci_dev *dev) 2750 { 2751 struct pci_bus *bus = dev->bus; 2752 2753 if (!dev->pme_support) 2754 return false; 2755 2756 /* PME-capable in principle, but not from the target power state */ 2757 if (!pci_pme_capable(dev, pci_target_state(dev, true))) 2758 return false; 2759 2760 if (device_can_wakeup(&dev->dev)) 2761 return true; 2762 2763 while (bus->parent) { 2764 struct pci_dev *bridge = bus->self; 2765 2766 if (device_can_wakeup(&bridge->dev)) 2767 return true; 2768 2769 bus = bus->parent; 2770 } 2771 2772 /* We have reached the root bus. */ 2773 if (bus->bridge) 2774 return device_can_wakeup(bus->bridge); 2775 2776 return false; 2777 } 2778 EXPORT_SYMBOL_GPL(pci_dev_run_wake); 2779 2780 /** 2781 * pci_dev_need_resume - Check if it is necessary to resume the device. 2782 * @pci_dev: Device to check. 2783 * 2784 * Return 'true' if the device is not runtime-suspended or it has to be 2785 * reconfigured due to wakeup settings difference between system and runtime 2786 * suspend, or the current power state of it is not suitable for the upcoming 2787 * (system-wide) transition. 2788 */ 2789 bool pci_dev_need_resume(struct pci_dev *pci_dev) 2790 { 2791 struct device *dev = &pci_dev->dev; 2792 pci_power_t target_state; 2793 2794 if (!pm_runtime_suspended(dev) || platform_pci_need_resume(pci_dev)) 2795 return true; 2796 2797 target_state = pci_target_state(pci_dev, device_may_wakeup(dev)); 2798 2799 /* 2800 * If the earlier platform check has not triggered, D3cold is just power 2801 * removal on top of D3hot, so no need to resume the device in that 2802 * case. 2803 */ 2804 return target_state != pci_dev->current_state && 2805 target_state != PCI_D3cold && 2806 pci_dev->current_state != PCI_D3hot; 2807 } 2808 2809 /** 2810 * pci_dev_adjust_pme - Adjust PME setting for a suspended device. 2811 * @pci_dev: Device to check. 2812 * 2813 * If the device is suspended and it is not configured for system wakeup, 2814 * disable PME for it to prevent it from waking up the system unnecessarily. 2815 * 2816 * Note that if the device's power state is D3cold and the platform check in 2817 * pci_dev_need_resume() has not triggered, the device's configuration need not 2818 * be changed. 2819 */ 2820 void pci_dev_adjust_pme(struct pci_dev *pci_dev) 2821 { 2822 struct device *dev = &pci_dev->dev; 2823 2824 spin_lock_irq(&dev->power.lock); 2825 2826 if (pm_runtime_suspended(dev) && !device_may_wakeup(dev) && 2827 pci_dev->current_state < PCI_D3cold) 2828 __pci_pme_active(pci_dev, false); 2829 2830 spin_unlock_irq(&dev->power.lock); 2831 } 2832 2833 /** 2834 * pci_dev_complete_resume - Finalize resume from system sleep for a device. 2835 * @pci_dev: Device to handle. 2836 * 2837 * If the device is runtime suspended and wakeup-capable, enable PME for it as 2838 * it might have been disabled during the prepare phase of system suspend if 2839 * the device was not configured for system wakeup. 2840 */ 2841 void pci_dev_complete_resume(struct pci_dev *pci_dev) 2842 { 2843 struct device *dev = &pci_dev->dev; 2844 2845 if (!pci_dev_run_wake(pci_dev)) 2846 return; 2847 2848 spin_lock_irq(&dev->power.lock); 2849 2850 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold) 2851 __pci_pme_active(pci_dev, true); 2852 2853 spin_unlock_irq(&dev->power.lock); 2854 } 2855 2856 /** 2857 * pci_choose_state - Choose the power state of a PCI device. 2858 * @dev: Target PCI device. 2859 * @state: Target state for the whole system. 2860 * 2861 * Returns PCI power state suitable for @dev and @state. 2862 */ 2863 pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state) 2864 { 2865 if (state.event == PM_EVENT_ON) 2866 return PCI_D0; 2867 2868 return pci_target_state(dev, false); 2869 } 2870 EXPORT_SYMBOL(pci_choose_state); 2871 2872 void pci_config_pm_runtime_get(struct pci_dev *pdev) 2873 { 2874 struct device *dev = &pdev->dev; 2875 struct device *parent = dev->parent; 2876 2877 if (parent) 2878 pm_runtime_get_sync(parent); 2879 pm_runtime_get_noresume(dev); 2880 /* 2881 * pdev->current_state is set to PCI_D3cold during suspending, 2882 * so wait until suspending completes 2883 */ 2884 pm_runtime_barrier(dev); 2885 /* 2886 * Only need to resume devices in D3cold, because config 2887 * registers are still accessible for devices suspended but 2888 * not in D3cold. 2889 */ 2890 if (pdev->current_state == PCI_D3cold) 2891 pm_runtime_resume(dev); 2892 } 2893 2894 void pci_config_pm_runtime_put(struct pci_dev *pdev) 2895 { 2896 struct device *dev = &pdev->dev; 2897 struct device *parent = dev->parent; 2898 2899 pm_runtime_put(dev); 2900 if (parent) 2901 pm_runtime_put_sync(parent); 2902 } 2903 2904 static const struct dmi_system_id bridge_d3_blacklist[] = { 2905 #ifdef CONFIG_X86 2906 { 2907 /* 2908 * Gigabyte X299 root port is not marked as hotplug capable 2909 * which allows Linux to power manage it. However, this 2910 * confuses the BIOS SMI handler so don't power manage root 2911 * ports on that system. 2912 */ 2913 .ident = "X299 DESIGNARE EX-CF", 2914 .matches = { 2915 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."), 2916 DMI_MATCH(DMI_BOARD_NAME, "X299 DESIGNARE EX-CF"), 2917 }, 2918 }, 2919 { 2920 /* 2921 * Downstream device is not accessible after putting a root port 2922 * into D3cold and back into D0 on Elo Continental Z2 board 2923 */ 2924 .ident = "Elo Continental Z2", 2925 .matches = { 2926 DMI_MATCH(DMI_BOARD_VENDOR, "Elo Touch Solutions"), 2927 DMI_MATCH(DMI_BOARD_NAME, "Geminilake"), 2928 DMI_MATCH(DMI_BOARD_VERSION, "Continental Z2"), 2929 }, 2930 }, 2931 { 2932 /* 2933 * Changing power state of root port dGPU is connected fails 2934 * https://gitlab.freedesktop.org/drm/amd/-/issues/3229 2935 */ 2936 .ident = "Hewlett-Packard HP Pavilion 17 Notebook PC/1972", 2937 .matches = { 2938 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"), 2939 DMI_MATCH(DMI_BOARD_NAME, "1972"), 2940 DMI_MATCH(DMI_BOARD_VERSION, "95.33"), 2941 }, 2942 }, 2943 #endif 2944 { } 2945 }; 2946 2947 /** 2948 * pci_bridge_d3_possible - Is it possible to put the bridge into D3 2949 * @bridge: Bridge to check 2950 * 2951 * Currently we only allow D3 for some PCIe ports and for Thunderbolt. 2952 * 2953 * Return: Whether it is possible to move the bridge to D3. 2954 * 2955 * The return value is guaranteed to be constant across the entire lifetime 2956 * of the bridge, including its hot-removal. 2957 */ 2958 bool pci_bridge_d3_possible(struct pci_dev *bridge) 2959 { 2960 if (!pci_is_pcie(bridge)) 2961 return false; 2962 2963 switch (pci_pcie_type(bridge)) { 2964 case PCI_EXP_TYPE_ROOT_PORT: 2965 case PCI_EXP_TYPE_UPSTREAM: 2966 case PCI_EXP_TYPE_DOWNSTREAM: 2967 if (pci_bridge_d3_disable) 2968 return false; 2969 2970 /* 2971 * Hotplug ports handled by platform firmware may not be put 2972 * into D3 by the OS, e.g. ACPI slots ... 2973 */ 2974 if (bridge->is_hotplug_bridge && !bridge->is_pciehp) 2975 return false; 2976 2977 /* ... or PCIe hotplug ports not handled natively by the OS. */ 2978 if (bridge->is_pciehp && !pciehp_is_native(bridge)) 2979 return false; 2980 2981 if (pci_bridge_d3_force) 2982 return true; 2983 2984 /* Even the oldest 2010 Thunderbolt controller supports D3. */ 2985 if (bridge->is_thunderbolt) 2986 return true; 2987 2988 /* Platform might know better if the bridge supports D3 */ 2989 if (platform_pci_bridge_d3(bridge)) 2990 return true; 2991 2992 /* 2993 * Hotplug ports handled natively by the OS were not validated 2994 * by vendors for runtime D3 at least until 2018 because there 2995 * was no OS support. 2996 */ 2997 if (bridge->is_pciehp) 2998 return false; 2999 3000 if (dmi_check_system(bridge_d3_blacklist)) 3001 return false; 3002 3003 /* 3004 * Out of caution, we only allow PCIe ports from 2015 or newer 3005 * into D3 on x86. 3006 */ 3007 if (!IS_ENABLED(CONFIG_X86) || dmi_get_bios_year() >= 2015) 3008 return true; 3009 break; 3010 } 3011 3012 return false; 3013 } 3014 3015 static int pci_dev_check_d3cold(struct pci_dev *dev, void *data) 3016 { 3017 bool *d3cold_ok = data; 3018 3019 if (/* The device needs to be allowed to go D3cold ... */ 3020 dev->no_d3cold || !dev->d3cold_allowed || 3021 3022 /* ... and if it is wakeup capable to do so from D3cold. */ 3023 (device_may_wakeup(&dev->dev) && 3024 !pci_pme_capable(dev, PCI_D3cold)) || 3025 3026 /* If it is a bridge it must be allowed to go to D3. */ 3027 !pci_power_manageable(dev)) 3028 3029 *d3cold_ok = false; 3030 3031 return !*d3cold_ok; 3032 } 3033 3034 /* 3035 * pci_bridge_d3_update - Update bridge D3 capabilities 3036 * @dev: PCI device which is changed 3037 * 3038 * Update upstream bridge PM capabilities accordingly depending on if the 3039 * device PM configuration was changed or the device is being removed. The 3040 * change is also propagated upstream. 3041 */ 3042 void pci_bridge_d3_update(struct pci_dev *dev) 3043 { 3044 bool remove = !device_is_registered(&dev->dev); 3045 struct pci_dev *bridge; 3046 bool d3cold_ok = true; 3047 3048 bridge = pci_upstream_bridge(dev); 3049 if (!bridge || !pci_bridge_d3_possible(bridge)) 3050 return; 3051 3052 /* 3053 * If D3 is currently allowed for the bridge, removing one of its 3054 * children won't change that. 3055 */ 3056 if (remove && bridge->bridge_d3) 3057 return; 3058 3059 /* 3060 * If D3 is currently allowed for the bridge and a child is added or 3061 * changed, disallowance of D3 can only be caused by that child, so 3062 * we only need to check that single device, not any of its siblings. 3063 * 3064 * If D3 is currently not allowed for the bridge, checking the device 3065 * first may allow us to skip checking its siblings. 3066 */ 3067 if (!remove) 3068 pci_dev_check_d3cold(dev, &d3cold_ok); 3069 3070 /* 3071 * If D3 is currently not allowed for the bridge, this may be caused 3072 * either by the device being changed/removed or any of its siblings, 3073 * so we need to go through all children to find out if one of them 3074 * continues to block D3. 3075 */ 3076 if (d3cold_ok && !bridge->bridge_d3) 3077 pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold, 3078 &d3cold_ok); 3079 3080 if (bridge->bridge_d3 != d3cold_ok) { 3081 bridge->bridge_d3 = d3cold_ok; 3082 /* Propagate change to upstream bridges */ 3083 pci_bridge_d3_update(bridge); 3084 } 3085 } 3086 3087 /** 3088 * pci_d3cold_enable - Enable D3cold for device 3089 * @dev: PCI device to handle 3090 * 3091 * This function can be used in drivers to enable D3cold from the device 3092 * they handle. It also updates upstream PCI bridge PM capabilities 3093 * accordingly. 3094 */ 3095 void pci_d3cold_enable(struct pci_dev *dev) 3096 { 3097 if (dev->no_d3cold) { 3098 dev->no_d3cold = false; 3099 pci_bridge_d3_update(dev); 3100 } 3101 } 3102 EXPORT_SYMBOL_GPL(pci_d3cold_enable); 3103 3104 /** 3105 * pci_d3cold_disable - Disable D3cold for device 3106 * @dev: PCI device to handle 3107 * 3108 * This function can be used in drivers to disable D3cold from the device 3109 * they handle. It also updates upstream PCI bridge PM capabilities 3110 * accordingly. 3111 */ 3112 void pci_d3cold_disable(struct pci_dev *dev) 3113 { 3114 if (!dev->no_d3cold) { 3115 dev->no_d3cold = true; 3116 pci_bridge_d3_update(dev); 3117 } 3118 } 3119 EXPORT_SYMBOL_GPL(pci_d3cold_disable); 3120 3121 void pci_pm_power_up_and_verify_state(struct pci_dev *pci_dev) 3122 { 3123 pci_power_up(pci_dev); 3124 pci_update_current_state(pci_dev, PCI_D0); 3125 } 3126 3127 /** 3128 * pci_pm_init - Initialize PM functions of given PCI device 3129 * @dev: PCI device to handle. 3130 */ 3131 void pci_pm_init(struct pci_dev *dev) 3132 { 3133 int pm; 3134 u16 pmc; 3135 3136 device_enable_async_suspend(&dev->dev); 3137 dev->wakeup_prepared = false; 3138 3139 dev->pm_cap = 0; 3140 dev->pme_support = 0; 3141 3142 /* find PCI PM capability in list */ 3143 pm = pci_find_capability(dev, PCI_CAP_ID_PM); 3144 if (!pm) 3145 goto poweron; 3146 /* Check device's ability to generate PME# */ 3147 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc); 3148 3149 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) { 3150 pci_err(dev, "unsupported PM cap regs version (%u)\n", 3151 pmc & PCI_PM_CAP_VER_MASK); 3152 goto poweron; 3153 } 3154 3155 dev->pm_cap = pm; 3156 dev->d3hot_delay = PCI_PM_D3HOT_WAIT; 3157 dev->d3cold_delay = PCI_PM_D3COLD_WAIT; 3158 dev->bridge_d3 = pci_bridge_d3_possible(dev); 3159 dev->d3cold_allowed = true; 3160 3161 dev->d1_support = false; 3162 dev->d2_support = false; 3163 if (!pci_no_d1d2(dev)) { 3164 if (pmc & PCI_PM_CAP_D1) 3165 dev->d1_support = true; 3166 if (pmc & PCI_PM_CAP_D2) 3167 dev->d2_support = true; 3168 3169 if (dev->d1_support || dev->d2_support) 3170 pci_info(dev, "supports%s%s\n", 3171 dev->d1_support ? " D1" : "", 3172 dev->d2_support ? " D2" : ""); 3173 } 3174 3175 pmc &= PCI_PM_CAP_PME_MASK; 3176 if (pmc) { 3177 pci_info(dev, "PME# supported from%s%s%s%s%s\n", 3178 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "", 3179 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "", 3180 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "", 3181 (pmc & PCI_PM_CAP_PME_D3hot) ? " D3hot" : "", 3182 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : ""); 3183 dev->pme_support = FIELD_GET(PCI_PM_CAP_PME_MASK, pmc); 3184 dev->pme_poll = true; 3185 /* 3186 * Make device's PM flags reflect the wake-up capability, but 3187 * let the user space enable it to wake up the system as needed. 3188 */ 3189 device_set_wakeup_capable(&dev->dev, true); 3190 /* Disable the PME# generation functionality */ 3191 pci_pme_active(dev, false); 3192 } 3193 3194 poweron: 3195 pci_pm_power_up_and_verify_state(dev); 3196 pm_runtime_forbid(&dev->dev); 3197 3198 /* 3199 * Runtime PM will be enabled for the device when it has been fully 3200 * configured, but since its parent and suppliers may suspend in 3201 * the meantime, prevent them from doing so by changing the 3202 * device's runtime PM status to "active". 3203 */ 3204 pm_runtime_set_active(&dev->dev); 3205 } 3206 3207 static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop) 3208 { 3209 unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI; 3210 3211 switch (prop) { 3212 case PCI_EA_P_MEM: 3213 case PCI_EA_P_VF_MEM: 3214 flags |= IORESOURCE_MEM; 3215 break; 3216 case PCI_EA_P_MEM_PREFETCH: 3217 case PCI_EA_P_VF_MEM_PREFETCH: 3218 flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 3219 break; 3220 case PCI_EA_P_IO: 3221 flags |= IORESOURCE_IO; 3222 break; 3223 default: 3224 return 0; 3225 } 3226 3227 return flags; 3228 } 3229 3230 static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei, 3231 u8 prop) 3232 { 3233 if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO) 3234 return &dev->resource[bei]; 3235 #ifdef CONFIG_PCI_IOV 3236 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 && 3237 (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH)) 3238 return &dev->resource[PCI_IOV_RESOURCES + 3239 bei - PCI_EA_BEI_VF_BAR0]; 3240 #endif 3241 else if (bei == PCI_EA_BEI_ROM) 3242 return &dev->resource[PCI_ROM_RESOURCE]; 3243 else 3244 return NULL; 3245 } 3246 3247 /* Read an Enhanced Allocation (EA) entry */ 3248 static int pci_ea_read(struct pci_dev *dev, int offset) 3249 { 3250 struct resource *res; 3251 const char *res_name; 3252 int ent_size, ent_offset = offset; 3253 resource_size_t start, end; 3254 unsigned long flags; 3255 u32 dw0, bei, base, max_offset; 3256 u8 prop; 3257 bool support_64 = (sizeof(resource_size_t) >= 8); 3258 3259 pci_read_config_dword(dev, ent_offset, &dw0); 3260 ent_offset += 4; 3261 3262 /* Entry size field indicates DWORDs after 1st */ 3263 ent_size = (FIELD_GET(PCI_EA_ES, dw0) + 1) << 2; 3264 3265 if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */ 3266 goto out; 3267 3268 bei = FIELD_GET(PCI_EA_BEI, dw0); 3269 prop = FIELD_GET(PCI_EA_PP, dw0); 3270 3271 /* 3272 * If the Property is in the reserved range, try the Secondary 3273 * Property instead. 3274 */ 3275 if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED) 3276 prop = FIELD_GET(PCI_EA_SP, dw0); 3277 if (prop > PCI_EA_P_BRIDGE_IO) 3278 goto out; 3279 3280 res = pci_ea_get_resource(dev, bei, prop); 3281 res_name = pci_resource_name(dev, bei); 3282 if (!res) { 3283 pci_err(dev, "Unsupported EA entry BEI: %u\n", bei); 3284 goto out; 3285 } 3286 3287 flags = pci_ea_flags(dev, prop); 3288 if (!flags) { 3289 pci_err(dev, "Unsupported EA properties: %#x\n", prop); 3290 goto out; 3291 } 3292 3293 /* Read Base */ 3294 pci_read_config_dword(dev, ent_offset, &base); 3295 start = (base & PCI_EA_FIELD_MASK); 3296 ent_offset += 4; 3297 3298 /* Read MaxOffset */ 3299 pci_read_config_dword(dev, ent_offset, &max_offset); 3300 ent_offset += 4; 3301 3302 /* Read Base MSBs (if 64-bit entry) */ 3303 if (base & PCI_EA_IS_64) { 3304 u32 base_upper; 3305 3306 pci_read_config_dword(dev, ent_offset, &base_upper); 3307 ent_offset += 4; 3308 3309 flags |= IORESOURCE_MEM_64; 3310 3311 /* entry starts above 32-bit boundary, can't use */ 3312 if (!support_64 && base_upper) 3313 goto out; 3314 3315 if (support_64) 3316 start |= ((u64)base_upper << 32); 3317 } 3318 3319 end = start + (max_offset | 0x03); 3320 3321 /* Read MaxOffset MSBs (if 64-bit entry) */ 3322 if (max_offset & PCI_EA_IS_64) { 3323 u32 max_offset_upper; 3324 3325 pci_read_config_dword(dev, ent_offset, &max_offset_upper); 3326 ent_offset += 4; 3327 3328 flags |= IORESOURCE_MEM_64; 3329 3330 /* entry too big, can't use */ 3331 if (!support_64 && max_offset_upper) 3332 goto out; 3333 3334 if (support_64) 3335 end += ((u64)max_offset_upper << 32); 3336 } 3337 3338 if (end < start) { 3339 pci_err(dev, "EA Entry crosses address boundary\n"); 3340 goto out; 3341 } 3342 3343 if (ent_size != ent_offset - offset) { 3344 pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n", 3345 ent_size, ent_offset - offset); 3346 goto out; 3347 } 3348 3349 res->name = pci_name(dev); 3350 res->start = start; 3351 res->end = end; 3352 res->flags = flags; 3353 3354 if (bei <= PCI_EA_BEI_BAR5) 3355 pci_info(dev, "%s %pR: from Enhanced Allocation, properties %#02x\n", 3356 res_name, res, prop); 3357 else if (bei == PCI_EA_BEI_ROM) 3358 pci_info(dev, "%s %pR: from Enhanced Allocation, properties %#02x\n", 3359 res_name, res, prop); 3360 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5) 3361 pci_info(dev, "%s %pR: from Enhanced Allocation, properties %#02x\n", 3362 res_name, res, prop); 3363 else 3364 pci_info(dev, "BEI %d %pR: from Enhanced Allocation, properties %#02x\n", 3365 bei, res, prop); 3366 3367 out: 3368 return offset + ent_size; 3369 } 3370 3371 /* Enhanced Allocation Initialization */ 3372 void pci_ea_init(struct pci_dev *dev) 3373 { 3374 int ea; 3375 u8 num_ent; 3376 int offset; 3377 int i; 3378 3379 /* find PCI EA capability in list */ 3380 ea = pci_find_capability(dev, PCI_CAP_ID_EA); 3381 if (!ea) 3382 return; 3383 3384 /* determine the number of entries */ 3385 pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT, 3386 &num_ent); 3387 num_ent &= PCI_EA_NUM_ENT_MASK; 3388 3389 offset = ea + PCI_EA_FIRST_ENT; 3390 3391 /* Skip DWORD 2 for type 1 functions */ 3392 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) 3393 offset += 4; 3394 3395 /* parse each EA entry */ 3396 for (i = 0; i < num_ent; ++i) 3397 offset = pci_ea_read(dev, offset); 3398 } 3399 3400 static void pci_add_saved_cap(struct pci_dev *pci_dev, 3401 struct pci_cap_saved_state *new_cap) 3402 { 3403 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space); 3404 } 3405 3406 /** 3407 * _pci_add_cap_save_buffer - allocate buffer for saving given 3408 * capability registers 3409 * @dev: the PCI device 3410 * @cap: the capability to allocate the buffer for 3411 * @extended: Standard or Extended capability ID 3412 * @size: requested size of the buffer 3413 */ 3414 static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap, 3415 bool extended, unsigned int size) 3416 { 3417 int pos; 3418 struct pci_cap_saved_state *save_state; 3419 3420 if (extended) 3421 pos = pci_find_ext_capability(dev, cap); 3422 else 3423 pos = pci_find_capability(dev, cap); 3424 3425 if (!pos) 3426 return 0; 3427 3428 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL); 3429 if (!save_state) 3430 return -ENOMEM; 3431 3432 save_state->cap.cap_nr = cap; 3433 save_state->cap.cap_extended = extended; 3434 save_state->cap.size = size; 3435 pci_add_saved_cap(dev, save_state); 3436 3437 return 0; 3438 } 3439 3440 int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size) 3441 { 3442 return _pci_add_cap_save_buffer(dev, cap, false, size); 3443 } 3444 3445 int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size) 3446 { 3447 return _pci_add_cap_save_buffer(dev, cap, true, size); 3448 } 3449 3450 /** 3451 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities 3452 * @dev: the PCI device 3453 */ 3454 void pci_allocate_cap_save_buffers(struct pci_dev *dev) 3455 { 3456 int error; 3457 3458 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 3459 PCI_EXP_SAVE_REGS * sizeof(u16)); 3460 if (error) 3461 pci_err(dev, "unable to preallocate PCI Express save buffer\n"); 3462 3463 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16)); 3464 if (error) 3465 pci_err(dev, "unable to preallocate PCI-X save buffer\n"); 3466 3467 error = pci_add_ext_cap_save_buffer(dev, PCI_EXT_CAP_ID_LTR, 3468 2 * sizeof(u16)); 3469 if (error) 3470 pci_err(dev, "unable to allocate suspend buffer for LTR\n"); 3471 3472 pci_allocate_vc_save_buffers(dev); 3473 } 3474 3475 void pci_free_cap_save_buffers(struct pci_dev *dev) 3476 { 3477 struct pci_cap_saved_state *tmp; 3478 struct hlist_node *n; 3479 3480 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next) 3481 kfree(tmp); 3482 } 3483 3484 /** 3485 * pci_configure_ari - enable or disable ARI forwarding 3486 * @dev: the PCI device 3487 * 3488 * If @dev and its upstream bridge both support ARI, enable ARI in the 3489 * bridge. Otherwise, disable ARI in the bridge. 3490 */ 3491 void pci_configure_ari(struct pci_dev *dev) 3492 { 3493 u32 cap; 3494 struct pci_dev *bridge; 3495 3496 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn) 3497 return; 3498 3499 bridge = dev->bus->self; 3500 if (!bridge) 3501 return; 3502 3503 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap); 3504 if (!(cap & PCI_EXP_DEVCAP2_ARI)) 3505 return; 3506 3507 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) { 3508 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2, 3509 PCI_EXP_DEVCTL2_ARI); 3510 bridge->ari_enabled = 1; 3511 } else { 3512 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2, 3513 PCI_EXP_DEVCTL2_ARI); 3514 bridge->ari_enabled = 0; 3515 } 3516 } 3517 3518 static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags) 3519 { 3520 int pos; 3521 u16 ctrl; 3522 3523 pos = pdev->acs_cap; 3524 if (!pos) 3525 return false; 3526 3527 /* 3528 * Except for egress control, capabilities are either required 3529 * or only required if controllable. Features missing from the 3530 * capability field can therefore be assumed as hard-wired enabled. 3531 */ 3532 acs_flags &= (pdev->acs_capabilities | PCI_ACS_EC); 3533 3534 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl); 3535 return (ctrl & acs_flags) == acs_flags; 3536 } 3537 3538 /** 3539 * pci_acs_enabled - test ACS against required flags for a given device 3540 * @pdev: device to test 3541 * @acs_flags: required PCI ACS flags 3542 * 3543 * Return true if the device supports the provided flags. Automatically 3544 * filters out flags that are not implemented on multifunction devices. 3545 * 3546 * Note that this interface checks the effective ACS capabilities of the 3547 * device rather than the actual capabilities. For instance, most single 3548 * function endpoints are not required to support ACS because they have no 3549 * opportunity for peer-to-peer access. We therefore return 'true' 3550 * regardless of whether the device exposes an ACS capability. This makes 3551 * it much easier for callers of this function to ignore the actual type 3552 * or topology of the device when testing ACS support. 3553 */ 3554 bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags) 3555 { 3556 int ret; 3557 3558 ret = pci_dev_specific_acs_enabled(pdev, acs_flags); 3559 if (ret >= 0) 3560 return ret > 0; 3561 3562 /* 3563 * Conventional PCI and PCI-X devices never support ACS, either 3564 * effectively or actually. The shared bus topology implies that 3565 * any device on the bus can receive or snoop DMA. 3566 */ 3567 if (!pci_is_pcie(pdev)) 3568 return false; 3569 3570 switch (pci_pcie_type(pdev)) { 3571 /* 3572 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec, 3573 * but since their primary interface is PCI/X, we conservatively 3574 * handle them as we would a non-PCIe device. 3575 */ 3576 case PCI_EXP_TYPE_PCIE_BRIDGE: 3577 /* 3578 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never 3579 * applicable... must never implement an ACS Extended Capability...". 3580 * This seems arbitrary, but we take a conservative interpretation 3581 * of this statement. 3582 */ 3583 case PCI_EXP_TYPE_PCI_BRIDGE: 3584 case PCI_EXP_TYPE_RC_EC: 3585 return false; 3586 /* 3587 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should 3588 * implement ACS in order to indicate their peer-to-peer capabilities, 3589 * regardless of whether they are single- or multi-function devices. 3590 */ 3591 case PCI_EXP_TYPE_DOWNSTREAM: 3592 case PCI_EXP_TYPE_ROOT_PORT: 3593 return pci_acs_flags_enabled(pdev, acs_flags); 3594 /* 3595 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be 3596 * implemented by the remaining PCIe types to indicate peer-to-peer 3597 * capabilities, but only when they are part of a multifunction 3598 * device. The footnote for section 6.12 indicates the specific 3599 * PCIe types included here. 3600 */ 3601 case PCI_EXP_TYPE_ENDPOINT: 3602 case PCI_EXP_TYPE_UPSTREAM: 3603 case PCI_EXP_TYPE_LEG_END: 3604 case PCI_EXP_TYPE_RC_END: 3605 if (!pdev->multifunction) 3606 break; 3607 3608 return pci_acs_flags_enabled(pdev, acs_flags); 3609 } 3610 3611 /* 3612 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable 3613 * to single function devices with the exception of downstream ports. 3614 */ 3615 return true; 3616 } 3617 3618 /** 3619 * pci_acs_path_enabled - test ACS flags from start to end in a hierarchy 3620 * @start: starting downstream device 3621 * @end: ending upstream device or NULL to search to the root bus 3622 * @acs_flags: required flags 3623 * 3624 * Walk up a device tree from start to end testing PCI ACS support. If 3625 * any step along the way does not support the required flags, return false. 3626 */ 3627 bool pci_acs_path_enabled(struct pci_dev *start, 3628 struct pci_dev *end, u16 acs_flags) 3629 { 3630 struct pci_dev *pdev, *parent = start; 3631 3632 do { 3633 pdev = parent; 3634 3635 if (!pci_acs_enabled(pdev, acs_flags)) 3636 return false; 3637 3638 if (pci_is_root_bus(pdev->bus)) 3639 return (end == NULL); 3640 3641 parent = pdev->bus->self; 3642 } while (pdev != end); 3643 3644 return true; 3645 } 3646 3647 /** 3648 * pci_acs_init - Initialize ACS if hardware supports it 3649 * @dev: the PCI device 3650 */ 3651 void pci_acs_init(struct pci_dev *dev) 3652 { 3653 int pos; 3654 3655 dev->acs_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS); 3656 pos = dev->acs_cap; 3657 if (!pos) 3658 return; 3659 3660 pci_read_config_word(dev, pos + PCI_ACS_CAP, &dev->acs_capabilities); 3661 pci_disable_broken_acs_cap(dev); 3662 } 3663 3664 /** 3665 * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port 3666 * @dev: the PCI device 3667 * @cap_mask: mask of desired AtomicOp sizes, including one or more of: 3668 * PCI_EXP_DEVCAP2_ATOMIC_COMP32 3669 * PCI_EXP_DEVCAP2_ATOMIC_COMP64 3670 * PCI_EXP_DEVCAP2_ATOMIC_COMP128 3671 * 3672 * Return 0 if all upstream bridges support AtomicOp routing, egress 3673 * blocking is disabled on all upstream ports, and the root port supports 3674 * the requested completion capabilities (32-bit, 64-bit and/or 128-bit 3675 * AtomicOp completion), or negative otherwise. 3676 */ 3677 int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask) 3678 { 3679 struct pci_bus *bus = dev->bus; 3680 struct pci_dev *bridge; 3681 u32 cap, ctl2; 3682 3683 /* 3684 * Per PCIe r5.0, sec 9.3.5.10, the AtomicOp Requester Enable bit 3685 * in Device Control 2 is reserved in VFs and the PF value applies 3686 * to all associated VFs. 3687 */ 3688 if (dev->is_virtfn) 3689 return -EINVAL; 3690 3691 if (!pci_is_pcie(dev)) 3692 return -EINVAL; 3693 3694 /* 3695 * Per PCIe r4.0, sec 6.15, endpoints and root ports may be 3696 * AtomicOp requesters. For now, we only support endpoints as 3697 * requesters and root ports as completers. No endpoints as 3698 * completers, and no peer-to-peer. 3699 */ 3700 3701 switch (pci_pcie_type(dev)) { 3702 case PCI_EXP_TYPE_ENDPOINT: 3703 case PCI_EXP_TYPE_LEG_END: 3704 case PCI_EXP_TYPE_RC_END: 3705 break; 3706 default: 3707 return -EINVAL; 3708 } 3709 3710 while (bus->parent) { 3711 bridge = bus->self; 3712 3713 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap); 3714 3715 switch (pci_pcie_type(bridge)) { 3716 /* Ensure switch ports support AtomicOp routing */ 3717 case PCI_EXP_TYPE_UPSTREAM: 3718 case PCI_EXP_TYPE_DOWNSTREAM: 3719 if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE)) 3720 return -EINVAL; 3721 break; 3722 3723 /* Ensure root port supports all the sizes we care about */ 3724 case PCI_EXP_TYPE_ROOT_PORT: 3725 if ((cap & cap_mask) != cap_mask) 3726 return -EINVAL; 3727 break; 3728 } 3729 3730 /* Ensure upstream ports don't block AtomicOps on egress */ 3731 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM) { 3732 pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2, 3733 &ctl2); 3734 if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK) 3735 return -EINVAL; 3736 } 3737 3738 bus = bus->parent; 3739 } 3740 3741 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2, 3742 PCI_EXP_DEVCTL2_ATOMIC_REQ); 3743 return 0; 3744 } 3745 EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); 3746 3747 /** 3748 * pci_release_region - Release a PCI bar 3749 * @pdev: PCI device whose resources were previously reserved by 3750 * pci_request_region() 3751 * @bar: BAR to release 3752 * 3753 * Releases the PCI I/O and memory resources previously reserved by a 3754 * successful call to pci_request_region(). Call this function only 3755 * after all use of the PCI regions has ceased. 3756 */ 3757 void pci_release_region(struct pci_dev *pdev, int bar) 3758 { 3759 if (!pci_bar_index_is_valid(bar)) 3760 return; 3761 3762 if (pci_resource_len(pdev, bar) == 0) 3763 return; 3764 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) 3765 release_region(pci_resource_start(pdev, bar), 3766 pci_resource_len(pdev, bar)); 3767 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) 3768 release_mem_region(pci_resource_start(pdev, bar), 3769 pci_resource_len(pdev, bar)); 3770 } 3771 EXPORT_SYMBOL(pci_release_region); 3772 3773 /** 3774 * __pci_request_region - Reserved PCI I/O and memory resource 3775 * @pdev: PCI device whose resources are to be reserved 3776 * @bar: BAR to be reserved 3777 * @name: name of the driver requesting the resource 3778 * @exclusive: whether the region access is exclusive or not 3779 * 3780 * Returns: 0 on success, negative error code on failure. 3781 * 3782 * Mark the PCI region associated with PCI device @pdev BAR @bar as being 3783 * reserved by owner @name. Do not access any address inside the PCI regions 3784 * unless this call returns successfully. 3785 * 3786 * If @exclusive is set, then the region is marked so that userspace 3787 * is explicitly not allowed to map the resource via /dev/mem or 3788 * sysfs MMIO access. 3789 * 3790 * Returns 0 on success, or %EBUSY on error. A warning 3791 * message is also printed on failure. 3792 */ 3793 static int __pci_request_region(struct pci_dev *pdev, int bar, 3794 const char *name, int exclusive) 3795 { 3796 if (!pci_bar_index_is_valid(bar)) 3797 return -EINVAL; 3798 3799 if (pci_resource_len(pdev, bar) == 0) 3800 return 0; 3801 3802 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) { 3803 if (!request_region(pci_resource_start(pdev, bar), 3804 pci_resource_len(pdev, bar), name)) 3805 goto err_out; 3806 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) { 3807 if (!__request_mem_region(pci_resource_start(pdev, bar), 3808 pci_resource_len(pdev, bar), name, 3809 exclusive)) 3810 goto err_out; 3811 } 3812 3813 return 0; 3814 3815 err_out: 3816 pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar, 3817 &pdev->resource[bar]); 3818 return -EBUSY; 3819 } 3820 3821 /** 3822 * pci_request_region - Reserve PCI I/O and memory resource 3823 * @pdev: PCI device whose resources are to be reserved 3824 * @bar: BAR to be reserved 3825 * @name: name of the driver requesting the resource 3826 * 3827 * Returns: 0 on success, negative error code on failure. 3828 * 3829 * Mark the PCI region associated with PCI device @pdev BAR @bar as being 3830 * reserved by owner @name. Do not access any address inside the PCI regions 3831 * unless this call returns successfully. 3832 * 3833 * Returns 0 on success, or %EBUSY on error. A warning 3834 * message is also printed on failure. 3835 */ 3836 int pci_request_region(struct pci_dev *pdev, int bar, const char *name) 3837 { 3838 return __pci_request_region(pdev, bar, name, 0); 3839 } 3840 EXPORT_SYMBOL(pci_request_region); 3841 3842 /** 3843 * pci_release_selected_regions - Release selected PCI I/O and memory resources 3844 * @pdev: PCI device whose resources were previously reserved 3845 * @bars: Bitmask of BARs to be released 3846 * 3847 * Release selected PCI I/O and memory resources previously reserved. 3848 * Call this function only after all use of the PCI regions has ceased. 3849 */ 3850 void pci_release_selected_regions(struct pci_dev *pdev, int bars) 3851 { 3852 int i; 3853 3854 for (i = 0; i < PCI_STD_NUM_BARS; i++) 3855 if (bars & (1 << i)) 3856 pci_release_region(pdev, i); 3857 } 3858 EXPORT_SYMBOL(pci_release_selected_regions); 3859 3860 static int __pci_request_selected_regions(struct pci_dev *pdev, int bars, 3861 const char *name, int excl) 3862 { 3863 int i; 3864 3865 for (i = 0; i < PCI_STD_NUM_BARS; i++) 3866 if (bars & (1 << i)) 3867 if (__pci_request_region(pdev, i, name, excl)) 3868 goto err_out; 3869 return 0; 3870 3871 err_out: 3872 while (--i >= 0) 3873 if (bars & (1 << i)) 3874 pci_release_region(pdev, i); 3875 3876 return -EBUSY; 3877 } 3878 3879 3880 /** 3881 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources 3882 * @pdev: PCI device whose resources are to be reserved 3883 * @bars: Bitmask of BARs to be requested 3884 * @name: Name of the driver requesting the resources 3885 * 3886 * Returns: 0 on success, negative error code on failure. 3887 */ 3888 int pci_request_selected_regions(struct pci_dev *pdev, int bars, 3889 const char *name) 3890 { 3891 return __pci_request_selected_regions(pdev, bars, name, 0); 3892 } 3893 EXPORT_SYMBOL(pci_request_selected_regions); 3894 3895 /** 3896 * pci_request_selected_regions_exclusive - Request regions exclusively 3897 * @pdev: PCI device to request regions from 3898 * @bars: bit mask of BARs to request 3899 * @name: name of the driver requesting the resources 3900 * 3901 * Returns: 0 on success, negative error code on failure. 3902 */ 3903 int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars, 3904 const char *name) 3905 { 3906 return __pci_request_selected_regions(pdev, bars, name, 3907 IORESOURCE_EXCLUSIVE); 3908 } 3909 EXPORT_SYMBOL(pci_request_selected_regions_exclusive); 3910 3911 /** 3912 * pci_release_regions - Release reserved PCI I/O and memory resources 3913 * @pdev: PCI device whose resources were previously reserved by 3914 * pci_request_regions() 3915 * 3916 * Releases all PCI I/O and memory resources previously reserved by a 3917 * successful call to pci_request_regions(). Call this function only 3918 * after all use of the PCI regions has ceased. 3919 */ 3920 void pci_release_regions(struct pci_dev *pdev) 3921 { 3922 pci_release_selected_regions(pdev, (1 << PCI_STD_NUM_BARS) - 1); 3923 } 3924 EXPORT_SYMBOL(pci_release_regions); 3925 3926 /** 3927 * pci_request_regions - Reserve PCI I/O and memory resources 3928 * @pdev: PCI device whose resources are to be reserved 3929 * @name: name of the driver requesting the resources 3930 * 3931 * Mark all PCI regions associated with PCI device @pdev as being reserved by 3932 * owner @name. Do not access any address inside the PCI regions unless this 3933 * call returns successfully. 3934 * 3935 * Returns 0 on success, or %EBUSY on error. A warning 3936 * message is also printed on failure. 3937 */ 3938 int pci_request_regions(struct pci_dev *pdev, const char *name) 3939 { 3940 return pci_request_selected_regions(pdev, 3941 ((1 << PCI_STD_NUM_BARS) - 1), name); 3942 } 3943 EXPORT_SYMBOL(pci_request_regions); 3944 3945 /** 3946 * pci_request_regions_exclusive - Reserve PCI I/O and memory resources 3947 * @pdev: PCI device whose resources are to be reserved 3948 * @name: name of the driver requesting the resources 3949 * 3950 * Returns: 0 on success, negative error code on failure. 3951 * 3952 * Mark all PCI regions associated with PCI device @pdev as being reserved 3953 * by owner @name. Do not access any address inside the PCI regions 3954 * unless this call returns successfully. 3955 * 3956 * pci_request_regions_exclusive() will mark the region so that /dev/mem 3957 * and the sysfs MMIO access will not be allowed. 3958 * 3959 * Returns 0 on success, or %EBUSY on error. A warning message is also 3960 * printed on failure. 3961 */ 3962 int pci_request_regions_exclusive(struct pci_dev *pdev, const char *name) 3963 { 3964 return pci_request_selected_regions_exclusive(pdev, 3965 ((1 << PCI_STD_NUM_BARS) - 1), name); 3966 } 3967 EXPORT_SYMBOL(pci_request_regions_exclusive); 3968 3969 /* 3970 * Record the PCI IO range (expressed as CPU physical address + size). 3971 * Return a negative value if an error has occurred, zero otherwise 3972 */ 3973 int pci_register_io_range(const struct fwnode_handle *fwnode, phys_addr_t addr, 3974 resource_size_t size) 3975 { 3976 int ret = 0; 3977 #ifdef PCI_IOBASE 3978 struct logic_pio_hwaddr *range; 3979 3980 if (!size || addr + size < addr) 3981 return -EINVAL; 3982 3983 range = kzalloc(sizeof(*range), GFP_ATOMIC); 3984 if (!range) 3985 return -ENOMEM; 3986 3987 range->fwnode = fwnode; 3988 range->size = size; 3989 range->hw_start = addr; 3990 range->flags = LOGIC_PIO_CPU_MMIO; 3991 3992 ret = logic_pio_register_range(range); 3993 if (ret) 3994 kfree(range); 3995 3996 /* Ignore duplicates due to deferred probing */ 3997 if (ret == -EEXIST) 3998 ret = 0; 3999 #endif 4000 4001 return ret; 4002 } 4003 4004 phys_addr_t pci_pio_to_address(unsigned long pio) 4005 { 4006 #ifdef PCI_IOBASE 4007 if (pio < MMIO_UPPER_LIMIT) 4008 return logic_pio_to_hwaddr(pio); 4009 #endif 4010 4011 return (phys_addr_t) OF_BAD_ADDR; 4012 } 4013 EXPORT_SYMBOL_GPL(pci_pio_to_address); 4014 4015 unsigned long __weak pci_address_to_pio(phys_addr_t address) 4016 { 4017 #ifdef PCI_IOBASE 4018 return logic_pio_trans_cpuaddr(address); 4019 #else 4020 if (address > IO_SPACE_LIMIT) 4021 return (unsigned long)-1; 4022 4023 return (unsigned long) address; 4024 #endif 4025 } 4026 4027 /** 4028 * pci_remap_iospace - Remap the memory mapped I/O space 4029 * @res: Resource describing the I/O space 4030 * @phys_addr: physical address of range to be mapped 4031 * 4032 * Remap the memory mapped I/O space described by the @res and the CPU 4033 * physical address @phys_addr into virtual address space. Only 4034 * architectures that have memory mapped IO functions defined (and the 4035 * PCI_IOBASE value defined) should call this function. 4036 */ 4037 #ifndef pci_remap_iospace 4038 int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr) 4039 { 4040 #if defined(PCI_IOBASE) 4041 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start; 4042 4043 if (!(res->flags & IORESOURCE_IO)) 4044 return -EINVAL; 4045 4046 if (res->end > IO_SPACE_LIMIT) 4047 return -EINVAL; 4048 4049 return vmap_page_range(vaddr, vaddr + resource_size(res), phys_addr, 4050 pgprot_device(PAGE_KERNEL)); 4051 #else 4052 /* 4053 * This architecture does not have memory mapped I/O space, 4054 * so this function should never be called 4055 */ 4056 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n"); 4057 return -ENODEV; 4058 #endif 4059 } 4060 EXPORT_SYMBOL(pci_remap_iospace); 4061 #endif 4062 4063 /** 4064 * pci_unmap_iospace - Unmap the memory mapped I/O space 4065 * @res: resource to be unmapped 4066 * 4067 * Unmap the CPU virtual address @res from virtual address space. Only 4068 * architectures that have memory mapped IO functions defined (and the 4069 * PCI_IOBASE value defined) should call this function. 4070 */ 4071 void pci_unmap_iospace(struct resource *res) 4072 { 4073 #if defined(PCI_IOBASE) 4074 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start; 4075 4076 vunmap_range(vaddr, vaddr + resource_size(res)); 4077 #endif 4078 } 4079 EXPORT_SYMBOL(pci_unmap_iospace); 4080 4081 static void __pci_set_master(struct pci_dev *dev, bool enable) 4082 { 4083 u16 old_cmd, cmd; 4084 4085 pci_read_config_word(dev, PCI_COMMAND, &old_cmd); 4086 if (enable) 4087 cmd = old_cmd | PCI_COMMAND_MASTER; 4088 else 4089 cmd = old_cmd & ~PCI_COMMAND_MASTER; 4090 if (cmd != old_cmd) { 4091 pci_dbg(dev, "%s bus mastering\n", 4092 enable ? "enabling" : "disabling"); 4093 pci_write_config_word(dev, PCI_COMMAND, cmd); 4094 } 4095 dev->is_busmaster = enable; 4096 } 4097 4098 /** 4099 * pcibios_setup - process "pci=" kernel boot arguments 4100 * @str: string used to pass in "pci=" kernel boot arguments 4101 * 4102 * Process kernel boot arguments. This is the default implementation. 4103 * Architecture specific implementations can override this as necessary. 4104 */ 4105 char * __weak __init pcibios_setup(char *str) 4106 { 4107 return str; 4108 } 4109 4110 /** 4111 * pcibios_set_master - enable PCI bus-mastering for device dev 4112 * @dev: the PCI device to enable 4113 * 4114 * Enables PCI bus-mastering for the device. This is the default 4115 * implementation. Architecture specific implementations can override 4116 * this if necessary. 4117 */ 4118 void __weak pcibios_set_master(struct pci_dev *dev) 4119 { 4120 u8 lat; 4121 4122 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */ 4123 if (pci_is_pcie(dev)) 4124 return; 4125 4126 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); 4127 if (lat < 16) 4128 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; 4129 else if (lat > pcibios_max_latency) 4130 lat = pcibios_max_latency; 4131 else 4132 return; 4133 4134 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); 4135 } 4136 4137 /** 4138 * pci_set_master - enables bus-mastering for device dev 4139 * @dev: the PCI device to enable 4140 * 4141 * Enables bus-mastering on the device and calls pcibios_set_master() 4142 * to do the needed arch specific settings. 4143 */ 4144 void pci_set_master(struct pci_dev *dev) 4145 { 4146 __pci_set_master(dev, true); 4147 pcibios_set_master(dev); 4148 } 4149 EXPORT_SYMBOL(pci_set_master); 4150 4151 /** 4152 * pci_clear_master - disables bus-mastering for device dev 4153 * @dev: the PCI device to disable 4154 */ 4155 void pci_clear_master(struct pci_dev *dev) 4156 { 4157 __pci_set_master(dev, false); 4158 } 4159 EXPORT_SYMBOL(pci_clear_master); 4160 4161 /** 4162 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed 4163 * @dev: the PCI device for which MWI is to be enabled 4164 * 4165 * Helper function for pci_set_mwi. 4166 * Originally copied from drivers/net/acenic.c. 4167 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. 4168 * 4169 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 4170 */ 4171 int pci_set_cacheline_size(struct pci_dev *dev) 4172 { 4173 u8 cacheline_size; 4174 4175 if (!pci_cache_line_size) 4176 return -EINVAL; 4177 4178 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be 4179 equal to or multiple of the right value. */ 4180 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size); 4181 if (cacheline_size >= pci_cache_line_size && 4182 (cacheline_size % pci_cache_line_size) == 0) 4183 return 0; 4184 4185 /* Write the correct value. */ 4186 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size); 4187 /* Read it back. */ 4188 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size); 4189 if (cacheline_size == pci_cache_line_size) 4190 return 0; 4191 4192 pci_dbg(dev, "cache line size of %d is not supported\n", 4193 pci_cache_line_size << 2); 4194 4195 return -EINVAL; 4196 } 4197 EXPORT_SYMBOL_GPL(pci_set_cacheline_size); 4198 4199 /** 4200 * pci_set_mwi - enables memory-write-invalidate PCI transaction 4201 * @dev: the PCI device for which MWI is enabled 4202 * 4203 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND. 4204 * 4205 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 4206 */ 4207 int pci_set_mwi(struct pci_dev *dev) 4208 { 4209 #ifdef PCI_DISABLE_MWI 4210 return 0; 4211 #else 4212 int rc; 4213 u16 cmd; 4214 4215 rc = pci_set_cacheline_size(dev); 4216 if (rc) 4217 return rc; 4218 4219 pci_read_config_word(dev, PCI_COMMAND, &cmd); 4220 if (!(cmd & PCI_COMMAND_INVALIDATE)) { 4221 pci_dbg(dev, "enabling Mem-Wr-Inval\n"); 4222 cmd |= PCI_COMMAND_INVALIDATE; 4223 pci_write_config_word(dev, PCI_COMMAND, cmd); 4224 } 4225 return 0; 4226 #endif 4227 } 4228 EXPORT_SYMBOL(pci_set_mwi); 4229 4230 /** 4231 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction 4232 * @dev: the PCI device for which MWI is enabled 4233 * 4234 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND. 4235 * Callers are not required to check the return value. 4236 * 4237 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 4238 */ 4239 int pci_try_set_mwi(struct pci_dev *dev) 4240 { 4241 #ifdef PCI_DISABLE_MWI 4242 return 0; 4243 #else 4244 return pci_set_mwi(dev); 4245 #endif 4246 } 4247 EXPORT_SYMBOL(pci_try_set_mwi); 4248 4249 /** 4250 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev 4251 * @dev: the PCI device to disable 4252 * 4253 * Disables PCI Memory-Write-Invalidate transaction on the device 4254 */ 4255 void pci_clear_mwi(struct pci_dev *dev) 4256 { 4257 #ifndef PCI_DISABLE_MWI 4258 u16 cmd; 4259 4260 pci_read_config_word(dev, PCI_COMMAND, &cmd); 4261 if (cmd & PCI_COMMAND_INVALIDATE) { 4262 cmd &= ~PCI_COMMAND_INVALIDATE; 4263 pci_write_config_word(dev, PCI_COMMAND, cmd); 4264 } 4265 #endif 4266 } 4267 EXPORT_SYMBOL(pci_clear_mwi); 4268 4269 /** 4270 * pci_disable_parity - disable parity checking for device 4271 * @dev: the PCI device to operate on 4272 * 4273 * Disable parity checking for device @dev 4274 */ 4275 void pci_disable_parity(struct pci_dev *dev) 4276 { 4277 u16 cmd; 4278 4279 pci_read_config_word(dev, PCI_COMMAND, &cmd); 4280 if (cmd & PCI_COMMAND_PARITY) { 4281 cmd &= ~PCI_COMMAND_PARITY; 4282 pci_write_config_word(dev, PCI_COMMAND, cmd); 4283 } 4284 } 4285 4286 /** 4287 * pci_intx - enables/disables PCI INTx for device dev 4288 * @pdev: the PCI device to operate on 4289 * @enable: boolean: whether to enable or disable PCI INTx 4290 * 4291 * Enables/disables PCI INTx for device @pdev 4292 */ 4293 void pci_intx(struct pci_dev *pdev, int enable) 4294 { 4295 u16 pci_command, new; 4296 4297 pci_read_config_word(pdev, PCI_COMMAND, &pci_command); 4298 4299 if (enable) 4300 new = pci_command & ~PCI_COMMAND_INTX_DISABLE; 4301 else 4302 new = pci_command | PCI_COMMAND_INTX_DISABLE; 4303 4304 if (new == pci_command) 4305 return; 4306 4307 pci_write_config_word(pdev, PCI_COMMAND, new); 4308 } 4309 EXPORT_SYMBOL_GPL(pci_intx); 4310 4311 /** 4312 * pci_wait_for_pending_transaction - wait for pending transaction 4313 * @dev: the PCI device to operate on 4314 * 4315 * Return 0 if transaction is pending 1 otherwise. 4316 */ 4317 int pci_wait_for_pending_transaction(struct pci_dev *dev) 4318 { 4319 if (!pci_is_pcie(dev)) 4320 return 1; 4321 4322 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA, 4323 PCI_EXP_DEVSTA_TRPND); 4324 } 4325 EXPORT_SYMBOL(pci_wait_for_pending_transaction); 4326 4327 /** 4328 * pcie_flr - initiate a PCIe function level reset 4329 * @dev: device to reset 4330 * 4331 * Initiate a function level reset unconditionally on @dev without 4332 * checking any flags and DEVCAP 4333 */ 4334 int pcie_flr(struct pci_dev *dev) 4335 { 4336 int ret; 4337 4338 if (!pci_wait_for_pending_transaction(dev)) 4339 pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n"); 4340 4341 /* Have to call it after waiting for pending DMA transaction */ 4342 ret = pci_dev_reset_iommu_prepare(dev); 4343 if (ret) { 4344 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); 4345 return ret; 4346 } 4347 4348 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR); 4349 4350 if (dev->imm_ready) 4351 goto done; 4352 4353 /* 4354 * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within 4355 * 100ms, but may silently discard requests while the FLR is in 4356 * progress. Wait 100ms before trying to access the device. 4357 */ 4358 msleep(100); 4359 4360 ret = pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS); 4361 done: 4362 pci_dev_reset_iommu_done(dev); 4363 return ret; 4364 } 4365 EXPORT_SYMBOL_GPL(pcie_flr); 4366 4367 /** 4368 * pcie_reset_flr - initiate a PCIe function level reset 4369 * @dev: device to reset 4370 * @probe: if true, return 0 if device can be reset this way 4371 * 4372 * Initiate a function level reset on @dev. 4373 */ 4374 int pcie_reset_flr(struct pci_dev *dev, bool probe) 4375 { 4376 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET) 4377 return -ENOTTY; 4378 4379 if (!(dev->devcap & PCI_EXP_DEVCAP_FLR)) 4380 return -ENOTTY; 4381 4382 if (probe) 4383 return 0; 4384 4385 return pcie_flr(dev); 4386 } 4387 EXPORT_SYMBOL_GPL(pcie_reset_flr); 4388 4389 static int pci_af_flr(struct pci_dev *dev, bool probe) 4390 { 4391 int ret; 4392 int pos; 4393 u8 cap; 4394 4395 pos = pci_find_capability(dev, PCI_CAP_ID_AF); 4396 if (!pos) 4397 return -ENOTTY; 4398 4399 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET) 4400 return -ENOTTY; 4401 4402 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap); 4403 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR)) 4404 return -ENOTTY; 4405 4406 if (probe) 4407 return 0; 4408 4409 /* 4410 * Wait for Transaction Pending bit to clear. A word-aligned test 4411 * is used, so we use the control offset rather than status and shift 4412 * the test bit to match. 4413 */ 4414 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL, 4415 PCI_AF_STATUS_TP << 8)) 4416 pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n"); 4417 4418 /* Have to call it after waiting for pending DMA transaction */ 4419 ret = pci_dev_reset_iommu_prepare(dev); 4420 if (ret) { 4421 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); 4422 return ret; 4423 } 4424 4425 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR); 4426 4427 if (dev->imm_ready) 4428 goto done; 4429 4430 /* 4431 * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006, 4432 * updated 27 July 2006; a device must complete an FLR within 4433 * 100ms, but may silently discard requests while the FLR is in 4434 * progress. Wait 100ms before trying to access the device. 4435 */ 4436 msleep(100); 4437 4438 ret = pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS); 4439 done: 4440 pci_dev_reset_iommu_done(dev); 4441 return ret; 4442 } 4443 4444 /** 4445 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0. 4446 * @dev: Device to reset. 4447 * @probe: if true, return 0 if the device can be reset this way. 4448 * 4449 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is 4450 * unset, it will be reinitialized internally when going from PCI_D3hot to 4451 * PCI_D0. If that's the case and the device is not in a low-power state 4452 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset. 4453 * 4454 * NOTE: This causes the caller to sleep for twice the device power transition 4455 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms 4456 * by default (i.e. unless the @dev's d3hot_delay field has a different value). 4457 * Moreover, only devices in D0 can be reset by this function. 4458 */ 4459 static int pci_pm_reset(struct pci_dev *dev, bool probe) 4460 { 4461 u16 csr; 4462 int ret; 4463 4464 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET) 4465 return -ENOTTY; 4466 4467 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr); 4468 if (csr & PCI_PM_CTRL_NO_SOFT_RESET) 4469 return -ENOTTY; 4470 4471 if (probe) 4472 return 0; 4473 4474 if (dev->current_state != PCI_D0) 4475 return -EINVAL; 4476 4477 ret = pci_dev_reset_iommu_prepare(dev); 4478 if (ret) { 4479 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); 4480 return ret; 4481 } 4482 4483 csr &= ~PCI_PM_CTRL_STATE_MASK; 4484 csr |= PCI_D3hot; 4485 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); 4486 pci_dev_d3_sleep(dev); 4487 4488 csr &= ~PCI_PM_CTRL_STATE_MASK; 4489 csr |= PCI_D0; 4490 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); 4491 pci_dev_d3_sleep(dev); 4492 4493 ret = pci_dev_wait(dev, "PM D3hot->D0", PCIE_RESET_READY_POLL_MS); 4494 pci_dev_reset_iommu_done(dev); 4495 return ret; 4496 } 4497 4498 /** 4499 * pcie_wait_for_link_status - Wait for link status change 4500 * @pdev: Device whose link to wait for. 4501 * @use_lt: Use the LT bit if TRUE, or the DLLLA bit if FALSE. 4502 * @active: Waiting for active or inactive? 4503 * 4504 * Return 0 if successful, or -ETIMEDOUT if status has not changed within 4505 * PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds. 4506 */ 4507 static int pcie_wait_for_link_status(struct pci_dev *pdev, 4508 bool use_lt, bool active) 4509 { 4510 u16 lnksta_mask, lnksta_match; 4511 unsigned long end_jiffies; 4512 u16 lnksta; 4513 4514 lnksta_mask = use_lt ? PCI_EXP_LNKSTA_LT : PCI_EXP_LNKSTA_DLLLA; 4515 lnksta_match = active ? lnksta_mask : 0; 4516 4517 end_jiffies = jiffies + msecs_to_jiffies(PCIE_LINK_RETRAIN_TIMEOUT_MS); 4518 do { 4519 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta); 4520 if ((lnksta & lnksta_mask) == lnksta_match) 4521 return 0; 4522 msleep(1); 4523 } while (time_before(jiffies, end_jiffies)); 4524 4525 return -ETIMEDOUT; 4526 } 4527 4528 /** 4529 * pcie_retrain_link - Request a link retrain and wait for it to complete 4530 * @pdev: Device whose link to retrain. 4531 * @use_lt: Use the LT bit if TRUE, or the DLLLA bit if FALSE, for status. 4532 * 4533 * Trigger retraining of the PCIe Link and wait for the completion of the 4534 * retraining. As link retraining is known to asserts LBMS and may change 4535 * the Link Speed, LBMS is cleared after the retraining and the Link Speed 4536 * of the subordinate bus is updated. 4537 * 4538 * Retrain completion status is retrieved from the Link Status Register 4539 * according to @use_lt. It is not verified whether the use of the DLLLA 4540 * bit is valid. 4541 * 4542 * Return 0 if successful, or -ETIMEDOUT if training has not completed 4543 * within PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds. 4544 */ 4545 int pcie_retrain_link(struct pci_dev *pdev, bool use_lt) 4546 { 4547 int rc; 4548 4549 /* 4550 * Ensure the updated LNKCTL parameters are used during link 4551 * training by checking that there is no ongoing link training that 4552 * may have started before link parameters were changed, so as to 4553 * avoid LTSSM race as recommended in Implementation Note at the end 4554 * of PCIe r6.1 sec 7.5.3.7. 4555 */ 4556 rc = pcie_wait_for_link_status(pdev, true, false); 4557 if (rc) 4558 return rc; 4559 4560 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_RL); 4561 if (pdev->clear_retrain_link) { 4562 /* 4563 * Due to an erratum in some devices the Retrain Link bit 4564 * needs to be cleared again manually to allow the link 4565 * training to succeed. 4566 */ 4567 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_RL); 4568 } 4569 4570 rc = pcie_wait_for_link_status(pdev, use_lt, !use_lt); 4571 4572 /* 4573 * Clear LBMS after a manual retrain so that the bit can be used 4574 * to track link speed or width changes made by hardware itself 4575 * in attempt to correct unreliable link operation. 4576 */ 4577 pcie_reset_lbms(pdev); 4578 4579 /* 4580 * Ensure the Link Speed updates after retraining in case the Link 4581 * Speed was changed because of the retraining. While the bwctrl's 4582 * IRQ handler normally picks up the new Link Speed, clearing LBMS 4583 * races with the IRQ handler reading the Link Status register and 4584 * can result in the handler returning early without updating the 4585 * Link Speed. 4586 */ 4587 if (pdev->subordinate) 4588 pcie_update_link_speed(pdev->subordinate, PCIE_LINK_RETRAIN); 4589 4590 return rc; 4591 } 4592 4593 /** 4594 * pcie_wait_for_link_delay - Wait until link is active or inactive 4595 * @pdev: Bridge device 4596 * @active: waiting for active or inactive? 4597 * @delay: Delay to wait after link has become active (in ms) 4598 * 4599 * Use this to wait till link becomes active or inactive. 4600 */ 4601 static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active, 4602 int delay) 4603 { 4604 int rc; 4605 4606 /* 4607 * Some controllers might not implement link active reporting. In this 4608 * case, we wait for 1000 ms + any delay requested by the caller. 4609 */ 4610 if (!pdev->link_active_reporting) { 4611 msleep(PCIE_LINK_RETRAIN_TIMEOUT_MS + delay); 4612 return true; 4613 } 4614 4615 /* 4616 * PCIe r4.0 sec 6.6.1, a component must enter LTSSM Detect within 20ms, 4617 * after which we should expect the link to be active if the reset was 4618 * successful. If so, software must wait a minimum 100ms before sending 4619 * configuration requests to devices downstream this port. 4620 * 4621 * If the link fails to activate, either the device was physically 4622 * removed or the link is permanently failed. 4623 */ 4624 if (active) 4625 msleep(20); 4626 rc = pcie_wait_for_link_status(pdev, false, active); 4627 if (active) { 4628 if (rc) 4629 rc = pcie_failed_link_retrain(pdev); 4630 if (rc) 4631 return false; 4632 4633 msleep(delay); 4634 return true; 4635 } 4636 4637 if (rc) 4638 return false; 4639 4640 return true; 4641 } 4642 4643 /** 4644 * pcie_wait_for_link - Wait until link is active or inactive 4645 * @pdev: Bridge device 4646 * @active: waiting for active or inactive? 4647 * 4648 * Use this to wait till link becomes active or inactive. 4649 */ 4650 bool pcie_wait_for_link(struct pci_dev *pdev, bool active) 4651 { 4652 return pcie_wait_for_link_delay(pdev, active, 100); 4653 } 4654 4655 /* 4656 * Find maximum D3cold delay required by all the devices on the bus. The 4657 * spec says 100 ms, but firmware can lower it and we allow drivers to 4658 * increase it as well. 4659 * 4660 * Context: Called with @pci_bus_sem locked for reading. 4661 */ 4662 static int pci_bus_max_d3cold_delay(const struct pci_bus *bus) 4663 { 4664 const struct pci_dev *pdev; 4665 int min_delay = 100; 4666 int max_delay = 0; 4667 4668 lockdep_assert_held(&pci_bus_sem); 4669 4670 list_for_each_entry(pdev, &bus->devices, bus_list) { 4671 if (pdev->d3cold_delay < min_delay) 4672 min_delay = pdev->d3cold_delay; 4673 if (pdev->d3cold_delay > max_delay) 4674 max_delay = pdev->d3cold_delay; 4675 } 4676 4677 return max(min_delay, max_delay); 4678 } 4679 4680 /** 4681 * pci_bridge_wait_for_secondary_bus - Wait for secondary bus to be accessible 4682 * @dev: PCI bridge 4683 * @reset_type: reset type in human-readable form 4684 * 4685 * Handle necessary delays before access to the devices on the secondary 4686 * side of the bridge are permitted after D3cold to D0 transition 4687 * or Conventional Reset. 4688 * 4689 * For PCIe this means the delays in PCIe 5.0 section 6.6.1. For 4690 * conventional PCI it means Tpvrh + Trhfa specified in PCI 3.0 section 4691 * 4.3.2. 4692 * 4693 * Return 0 on success or -ENOTTY if the first device on the secondary bus 4694 * failed to become accessible. 4695 */ 4696 int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type) 4697 { 4698 struct pci_dev *child __free(pci_dev_put) = NULL; 4699 int delay; 4700 4701 if (pci_dev_is_disconnected(dev)) 4702 return 0; 4703 4704 if (!pci_is_bridge(dev)) 4705 return 0; 4706 4707 down_read(&pci_bus_sem); 4708 4709 /* 4710 * We only deal with devices that are present currently on the bus. 4711 * For any hot-added devices the access delay is handled in pciehp 4712 * board_added(). In case of ACPI hotplug the firmware is expected 4713 * to configure the devices before OS is notified. 4714 */ 4715 if (!dev->subordinate || list_empty(&dev->subordinate->devices)) { 4716 up_read(&pci_bus_sem); 4717 return 0; 4718 } 4719 4720 /* Take d3cold_delay requirements into account */ 4721 delay = pci_bus_max_d3cold_delay(dev->subordinate); 4722 if (!delay) { 4723 up_read(&pci_bus_sem); 4724 return 0; 4725 } 4726 4727 child = pci_dev_get(list_first_entry(&dev->subordinate->devices, 4728 struct pci_dev, bus_list)); 4729 up_read(&pci_bus_sem); 4730 4731 /* 4732 * Conventional PCI and PCI-X we need to wait Tpvrh + Trhfa before 4733 * accessing the device after reset (that is 1000 ms + 100 ms). 4734 */ 4735 if (!pci_is_pcie(dev)) { 4736 pci_dbg(dev, "waiting %d ms for secondary bus\n", 1000 + delay); 4737 msleep(1000 + delay); 4738 return 0; 4739 } 4740 4741 /* 4742 * For PCIe downstream and root ports that do not support speeds 4743 * greater than 5 GT/s need to wait minimum 100 ms. For higher 4744 * speeds (gen3) we need to wait first for the data link layer to 4745 * become active. 4746 * 4747 * However, 100 ms is the minimum and the PCIe spec says the 4748 * software must allow at least 1s before it can determine that the 4749 * device that did not respond is a broken device. Also device can 4750 * take longer than that to respond if it indicates so through Request 4751 * Retry Status completions. 4752 * 4753 * Therefore we wait for 100 ms and check for the device presence 4754 * until the timeout expires. 4755 */ 4756 if (!pcie_downstream_port(dev)) 4757 return 0; 4758 4759 if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) { 4760 u16 status; 4761 4762 pci_dbg(dev, "waiting %d ms for downstream link\n", delay); 4763 msleep(delay); 4764 4765 if (!pci_dev_wait(child, reset_type, PCI_RESET_WAIT - delay)) 4766 return 0; 4767 4768 /* 4769 * If the port supports active link reporting we now check 4770 * whether the link is active and if not bail out early with 4771 * the assumption that the device is not present anymore. 4772 */ 4773 if (!dev->link_active_reporting) 4774 return -ENOTTY; 4775 4776 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &status); 4777 if (!(status & PCI_EXP_LNKSTA_DLLLA)) 4778 return -ENOTTY; 4779 4780 return pci_dev_wait(child, reset_type, 4781 PCIE_RESET_READY_POLL_MS - PCI_RESET_WAIT); 4782 } 4783 4784 pci_dbg(dev, "waiting %d ms for downstream link, after activation\n", 4785 delay); 4786 if (!pcie_wait_for_link_delay(dev, true, delay)) { 4787 /* Did not train, no need to wait any further */ 4788 pci_info(dev, "Data Link Layer Link Active not set in %d msec\n", delay); 4789 return -ENOTTY; 4790 } 4791 4792 return pci_dev_wait(child, reset_type, 4793 PCIE_RESET_READY_POLL_MS - delay); 4794 } 4795 4796 void pci_reset_secondary_bus(struct pci_dev *dev) 4797 { 4798 u16 ctrl; 4799 4800 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl); 4801 ctrl |= PCI_BRIDGE_CTL_BUS_RESET; 4802 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl); 4803 4804 /* 4805 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double 4806 * this to 2ms to ensure that we meet the minimum requirement. 4807 */ 4808 msleep(2); 4809 4810 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET; 4811 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl); 4812 } 4813 4814 void __weak pcibios_reset_secondary_bus(struct pci_dev *dev) 4815 { 4816 pci_reset_secondary_bus(dev); 4817 } 4818 4819 /** 4820 * pci_bridge_secondary_bus_reset - Reset the secondary bus on a PCI bridge. 4821 * @dev: Bridge device 4822 * 4823 * Use the bridge control register to assert reset on the secondary bus. 4824 * Devices on the secondary bus are left in power-on state. 4825 */ 4826 int pci_bridge_secondary_bus_reset(struct pci_dev *dev) 4827 { 4828 if (!dev->block_cfg_access) 4829 pci_warn_once(dev, "unlocked secondary bus reset via: %pS\n", 4830 __builtin_return_address(0)); 4831 pcibios_reset_secondary_bus(dev); 4832 4833 return pci_bridge_wait_for_secondary_bus(dev, "bus reset"); 4834 } 4835 EXPORT_SYMBOL_GPL(pci_bridge_secondary_bus_reset); 4836 4837 static int pci_parent_bus_reset(struct pci_dev *dev, bool probe) 4838 { 4839 struct pci_dev *pdev; 4840 4841 if (pci_is_root_bus(dev->bus) || dev->subordinate || 4842 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET) 4843 return -ENOTTY; 4844 4845 list_for_each_entry(pdev, &dev->bus->devices, bus_list) 4846 if (pdev != dev) 4847 return -ENOTTY; 4848 4849 if (probe) 4850 return 0; 4851 4852 return pci_bridge_secondary_bus_reset(dev->bus->self); 4853 } 4854 4855 static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, bool probe) 4856 { 4857 int rc = -ENOTTY; 4858 4859 if (!hotplug || !try_module_get(hotplug->owner)) 4860 return rc; 4861 4862 if (hotplug->ops->reset_slot) 4863 rc = hotplug->ops->reset_slot(hotplug, probe); 4864 4865 module_put(hotplug->owner); 4866 4867 return rc; 4868 } 4869 4870 static int pci_dev_reset_slot_function(struct pci_dev *dev, bool probe) 4871 { 4872 if (dev->multifunction || dev->subordinate || !dev->slot || 4873 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET) 4874 return -ENOTTY; 4875 4876 return pci_reset_hotplug_slot(dev->slot->hotplug, probe); 4877 } 4878 4879 static u16 cxl_port_dvsec(struct pci_dev *dev) 4880 { 4881 return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_CXL, 4882 PCI_DVSEC_CXL_PORT); 4883 } 4884 4885 static bool cxl_sbr_masked(struct pci_dev *dev) 4886 { 4887 u16 dvsec, reg; 4888 int rc; 4889 4890 dvsec = cxl_port_dvsec(dev); 4891 if (!dvsec) 4892 return false; 4893 4894 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_PORT_CTL, ®); 4895 if (rc || PCI_POSSIBLE_ERROR(reg)) 4896 return false; 4897 4898 /* 4899 * Per CXL spec r3.1, sec 8.1.5.2, when "Unmask SBR" is 0, the SBR 4900 * bit in Bridge Control has no effect. When 1, the Port generates 4901 * hot reset when the SBR bit is set to 1. 4902 */ 4903 if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) 4904 return false; 4905 4906 return true; 4907 } 4908 4909 static int pci_reset_bus_function(struct pci_dev *dev, bool probe) 4910 { 4911 struct pci_dev *bridge = pci_upstream_bridge(dev); 4912 int rc; 4913 4914 /* 4915 * If "dev" is below a CXL port that has SBR control masked, SBR 4916 * won't do anything, so return error. 4917 */ 4918 if (bridge && cxl_sbr_masked(bridge)) { 4919 if (probe) 4920 return 0; 4921 4922 return -ENOTTY; 4923 } 4924 4925 rc = pci_dev_reset_iommu_prepare(dev); 4926 if (rc) { 4927 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); 4928 return rc; 4929 } 4930 4931 rc = pci_dev_reset_slot_function(dev, probe); 4932 if (rc != -ENOTTY) 4933 goto done; 4934 4935 rc = pci_parent_bus_reset(dev, probe); 4936 done: 4937 pci_dev_reset_iommu_done(dev); 4938 return rc; 4939 } 4940 4941 static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) 4942 { 4943 struct pci_dev *bridge; 4944 u16 dvsec, reg, val; 4945 int rc; 4946 4947 bridge = pci_upstream_bridge(dev); 4948 if (!bridge) 4949 return -ENOTTY; 4950 4951 dvsec = cxl_port_dvsec(bridge); 4952 if (!dvsec) 4953 return -ENOTTY; 4954 4955 if (probe) 4956 return 0; 4957 4958 rc = pci_read_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, ®); 4959 if (rc) 4960 return -ENOTTY; 4961 4962 rc = pci_dev_reset_iommu_prepare(dev); 4963 if (rc) { 4964 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); 4965 return rc; 4966 } 4967 4968 if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) { 4969 val = reg; 4970 } else { 4971 val = reg | PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR; 4972 pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, 4973 val); 4974 } 4975 4976 rc = pci_reset_bus_function(dev, probe); 4977 4978 if (reg != val) 4979 pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, 4980 reg); 4981 4982 pci_dev_reset_iommu_done(dev); 4983 return rc; 4984 } 4985 4986 void pci_dev_lock(struct pci_dev *dev) 4987 { 4988 /* block PM suspend, driver probe, etc. */ 4989 device_lock(&dev->dev); 4990 pci_cfg_access_lock(dev); 4991 } 4992 EXPORT_SYMBOL_GPL(pci_dev_lock); 4993 4994 /* Return 1 on successful lock, 0 on contention */ 4995 int pci_dev_trylock(struct pci_dev *dev) 4996 { 4997 if (device_trylock(&dev->dev)) { 4998 if (pci_cfg_access_trylock(dev)) 4999 return 1; 5000 device_unlock(&dev->dev); 5001 } 5002 5003 return 0; 5004 } 5005 EXPORT_SYMBOL_GPL(pci_dev_trylock); 5006 5007 void pci_dev_unlock(struct pci_dev *dev) 5008 { 5009 pci_cfg_access_unlock(dev); 5010 device_unlock(&dev->dev); 5011 } 5012 EXPORT_SYMBOL_GPL(pci_dev_unlock); 5013 5014 static void pci_dev_save_and_disable(struct pci_dev *dev) 5015 { 5016 const struct pci_error_handlers *err_handler = 5017 dev->driver ? dev->driver->err_handler : NULL; 5018 5019 /* 5020 * dev->driver->err_handler->reset_prepare() is protected against 5021 * races with ->remove() by the device lock, which must be held by 5022 * the caller. 5023 */ 5024 device_lock_assert(&dev->dev); 5025 if (err_handler && err_handler->reset_prepare) 5026 err_handler->reset_prepare(dev); 5027 else if (dev->driver) 5028 pci_warn(dev, "resetting"); 5029 5030 /* 5031 * Wake-up device prior to save. PM registers default to D0 after 5032 * reset and a simple register restore doesn't reliably return 5033 * to a non-D0 state anyway. 5034 */ 5035 pci_set_power_state(dev, PCI_D0); 5036 5037 pci_save_state(dev); 5038 /* 5039 * Disable the device by clearing the Command register, except for 5040 * INTx-disable which is set. This not only disables MMIO and I/O port 5041 * BARs, but also prevents the device from being Bus Master, preventing 5042 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3 5043 * compliant devices, INTx-disable prevents legacy interrupts. 5044 */ 5045 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); 5046 } 5047 5048 static void pci_dev_restore(struct pci_dev *dev) 5049 { 5050 const struct pci_error_handlers *err_handler = 5051 dev->driver ? dev->driver->err_handler : NULL; 5052 5053 pci_restore_state(dev); 5054 5055 /* 5056 * dev->driver->err_handler->reset_done() is protected against 5057 * races with ->remove() by the device lock, which must be held by 5058 * the caller. 5059 */ 5060 if (err_handler && err_handler->reset_done) 5061 err_handler->reset_done(dev); 5062 else if (dev->driver) 5063 pci_warn(dev, "reset done"); 5064 } 5065 5066 /* dev->reset_methods[] is a 0-terminated list of indices into this array */ 5067 const struct pci_reset_fn_method pci_reset_fn_methods[] = { 5068 { }, 5069 { pci_dev_specific_reset, .name = "device_specific" }, 5070 { pci_dev_acpi_reset, .name = "acpi" }, 5071 { pcie_reset_flr, .name = "flr" }, 5072 { pci_af_flr, .name = "af_flr" }, 5073 { pci_pm_reset, .name = "pm" }, 5074 { pci_reset_bus_function, .name = "bus" }, 5075 { cxl_reset_bus_function, .name = "cxl_bus" }, 5076 }; 5077 5078 /** 5079 * __pci_reset_function_locked - reset a PCI device function while holding 5080 * the @dev mutex lock. 5081 * @dev: PCI device to reset 5082 * 5083 * Some devices allow an individual function to be reset without affecting 5084 * other functions in the same device. The PCI device must be responsive 5085 * to PCI config space in order to use this function. 5086 * 5087 * The device function is presumed to be unused and the caller is holding 5088 * the device mutex lock when this function is called. 5089 * 5090 * Resetting the device will make the contents of PCI configuration space 5091 * random, so any caller of this must be prepared to reinitialise the 5092 * device including MSI, bus mastering, BARs, decoding IO and memory spaces, 5093 * etc. 5094 * 5095 * Context: The caller must hold the device lock. 5096 * 5097 * Return: 0 if the device function was successfully reset or negative if the 5098 * device doesn't support resetting a single function. 5099 */ 5100 int __pci_reset_function_locked(struct pci_dev *dev) 5101 { 5102 int i, m, rc; 5103 const struct pci_reset_fn_method *method; 5104 5105 might_sleep(); 5106 device_lock_assert(&dev->dev); 5107 5108 /* 5109 * A reset method returns -ENOTTY if it doesn't support this device and 5110 * we should try the next method. 5111 * 5112 * If it returns 0 (success), we're finished. If it returns any other 5113 * error, we're also finished: this indicates that further reset 5114 * mechanisms might be broken on the device. 5115 */ 5116 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) { 5117 m = dev->reset_methods[i]; 5118 if (!m) 5119 return -ENOTTY; 5120 5121 method = &pci_reset_fn_methods[m]; 5122 pci_dbg(dev, "reset via %s\n", method->name); 5123 rc = method->reset_fn(dev, PCI_RESET_DO_RESET); 5124 if (!rc) 5125 return 0; 5126 5127 pci_dbg(dev, "%s failed with %d\n", method->name, rc); 5128 if (rc != -ENOTTY) 5129 return rc; 5130 } 5131 5132 return -ENOTTY; 5133 } 5134 EXPORT_SYMBOL_GPL(__pci_reset_function_locked); 5135 5136 /** 5137 * pci_init_reset_methods - check whether device can be safely reset 5138 * and store supported reset mechanisms. 5139 * @dev: PCI device to check for reset mechanisms 5140 * 5141 * Some devices allow an individual function to be reset without affecting 5142 * other functions in the same device. The PCI device must be in D0-D3hot 5143 * state. 5144 * 5145 * Stores reset mechanisms supported by device in reset_methods byte array 5146 * which is a member of struct pci_dev. 5147 */ 5148 void pci_init_reset_methods(struct pci_dev *dev) 5149 { 5150 int m, i, rc; 5151 5152 BUILD_BUG_ON(ARRAY_SIZE(pci_reset_fn_methods) != PCI_NUM_RESET_METHODS); 5153 5154 might_sleep(); 5155 5156 i = 0; 5157 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) { 5158 rc = pci_reset_fn_methods[m].reset_fn(dev, PCI_RESET_PROBE); 5159 if (!rc) 5160 dev->reset_methods[i++] = m; 5161 else if (rc != -ENOTTY) 5162 break; 5163 } 5164 5165 dev->reset_methods[i] = 0; 5166 } 5167 5168 /** 5169 * pci_reset_function - quiesce and reset a PCI device function 5170 * @dev: PCI device to reset 5171 * 5172 * Some devices allow an individual function to be reset without affecting 5173 * other functions in the same device. The PCI device must be responsive 5174 * to PCI config space in order to use this function. 5175 * 5176 * This function does not just reset the PCI portion of a device, but 5177 * clears all the state associated with the device. This function differs 5178 * from __pci_reset_function_locked() in that it saves and restores device state 5179 * over the reset and takes the PCI device lock. 5180 * 5181 * Returns 0 if the device function was successfully reset or negative if the 5182 * device doesn't support resetting a single function. 5183 */ 5184 int pci_reset_function(struct pci_dev *dev) 5185 { 5186 struct pci_dev *bridge; 5187 int rc; 5188 5189 if (!pci_reset_supported(dev)) 5190 return -ENOTTY; 5191 5192 /* 5193 * If there's no upstream bridge, no locking is needed since there is 5194 * no upstream bridge configuration to hold consistent. 5195 */ 5196 bridge = pci_upstream_bridge(dev); 5197 if (bridge) 5198 pci_dev_lock(bridge); 5199 5200 pci_dev_lock(dev); 5201 pci_dev_save_and_disable(dev); 5202 5203 rc = __pci_reset_function_locked(dev); 5204 5205 pci_dev_restore(dev); 5206 pci_dev_unlock(dev); 5207 5208 if (bridge) 5209 pci_dev_unlock(bridge); 5210 5211 return rc; 5212 } 5213 EXPORT_SYMBOL_GPL(pci_reset_function); 5214 5215 /** 5216 * pci_reset_function_locked - quiesce and reset a PCI device function 5217 * @dev: PCI device to reset 5218 * 5219 * Some devices allow an individual function to be reset without affecting 5220 * other functions in the same device. The PCI device must be responsive 5221 * to PCI config space in order to use this function. 5222 * 5223 * This function does not just reset the PCI portion of a device, but 5224 * clears all the state associated with the device. This function differs 5225 * from __pci_reset_function_locked() in that it saves and restores device state 5226 * over the reset. It also differs from pci_reset_function() in that it 5227 * requires the PCI device lock to be held. 5228 * 5229 * Context: The caller must hold the device lock. 5230 * 5231 * Return: 0 if the device function was successfully reset or negative if the 5232 * device doesn't support resetting a single function. 5233 */ 5234 int pci_reset_function_locked(struct pci_dev *dev) 5235 { 5236 int rc; 5237 5238 device_lock_assert(&dev->dev); 5239 5240 if (!pci_reset_supported(dev)) 5241 return -ENOTTY; 5242 5243 pci_dev_save_and_disable(dev); 5244 5245 rc = __pci_reset_function_locked(dev); 5246 5247 pci_dev_restore(dev); 5248 5249 return rc; 5250 } 5251 EXPORT_SYMBOL_GPL(pci_reset_function_locked); 5252 5253 /** 5254 * pci_try_reset_function - quiesce and reset a PCI device function 5255 * @dev: PCI device to reset 5256 * 5257 * Same as above, except return -EAGAIN if unable to lock device. 5258 */ 5259 int pci_try_reset_function(struct pci_dev *dev) 5260 { 5261 int rc; 5262 5263 if (!pci_reset_supported(dev)) 5264 return -ENOTTY; 5265 5266 if (!pci_dev_trylock(dev)) 5267 return -EAGAIN; 5268 5269 pci_dev_save_and_disable(dev); 5270 rc = __pci_reset_function_locked(dev); 5271 pci_dev_restore(dev); 5272 pci_dev_unlock(dev); 5273 5274 return rc; 5275 } 5276 EXPORT_SYMBOL_GPL(pci_try_reset_function); 5277 5278 /* Do any devices on or below this bus prevent a bus reset? */ 5279 static bool pci_bus_resettable(struct pci_bus *bus) 5280 { 5281 struct pci_dev *dev; 5282 5283 5284 if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)) 5285 return false; 5286 5287 list_for_each_entry(dev, &bus->devices, bus_list) { 5288 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET || 5289 (dev->subordinate && !pci_bus_resettable(dev->subordinate))) 5290 return false; 5291 } 5292 5293 return true; 5294 } 5295 5296 /* Lock devices from the top of the tree down */ 5297 static void pci_bus_lock(struct pci_bus *bus) 5298 { 5299 struct pci_dev *dev; 5300 5301 pci_dev_lock(bus->self); 5302 list_for_each_entry(dev, &bus->devices, bus_list) { 5303 if (dev->subordinate) 5304 pci_bus_lock(dev->subordinate); 5305 else 5306 pci_dev_lock(dev); 5307 } 5308 } 5309 5310 /* Unlock devices from the bottom of the tree up */ 5311 static void pci_bus_unlock(struct pci_bus *bus) 5312 { 5313 struct pci_dev *dev; 5314 5315 list_for_each_entry(dev, &bus->devices, bus_list) { 5316 if (dev->subordinate) 5317 pci_bus_unlock(dev->subordinate); 5318 else 5319 pci_dev_unlock(dev); 5320 } 5321 pci_dev_unlock(bus->self); 5322 } 5323 5324 /* Return 1 on successful lock, 0 on contention */ 5325 static int pci_bus_trylock(struct pci_bus *bus) 5326 { 5327 struct pci_dev *dev; 5328 5329 if (!pci_dev_trylock(bus->self)) 5330 return 0; 5331 5332 list_for_each_entry(dev, &bus->devices, bus_list) { 5333 if (dev->subordinate) { 5334 if (!pci_bus_trylock(dev->subordinate)) 5335 goto unlock; 5336 } else if (!pci_dev_trylock(dev)) 5337 goto unlock; 5338 } 5339 return 1; 5340 5341 unlock: 5342 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) { 5343 if (dev->subordinate) 5344 pci_bus_unlock(dev->subordinate); 5345 else 5346 pci_dev_unlock(dev); 5347 } 5348 pci_dev_unlock(bus->self); 5349 return 0; 5350 } 5351 5352 /* Do any devices on or below this slot prevent a bus reset? */ 5353 static bool pci_slot_resettable(struct pci_slot *slot) 5354 { 5355 struct pci_dev *dev, *bridge = slot->bus->self; 5356 5357 if (bridge && (bridge->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)) 5358 return false; 5359 5360 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5361 if (!dev->slot || dev->slot != slot) 5362 continue; 5363 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET || 5364 (dev->subordinate && !pci_bus_resettable(dev->subordinate))) 5365 return false; 5366 } 5367 5368 return true; 5369 } 5370 5371 /* Lock devices from the top of the tree down */ 5372 static void pci_slot_lock(struct pci_slot *slot) 5373 { 5374 struct pci_dev *dev, *bridge = slot->bus->self; 5375 5376 if (bridge) 5377 pci_dev_lock(bridge); 5378 5379 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5380 if (!dev->slot || dev->slot != slot) 5381 continue; 5382 if (dev->subordinate) 5383 pci_bus_lock(dev->subordinate); 5384 else 5385 pci_dev_lock(dev); 5386 } 5387 } 5388 5389 /* Unlock devices from the bottom of the tree up */ 5390 static void pci_slot_unlock(struct pci_slot *slot) 5391 { 5392 struct pci_dev *dev, *bridge = slot->bus->self; 5393 5394 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5395 if (!dev->slot || dev->slot != slot) 5396 continue; 5397 if (dev->subordinate) 5398 pci_bus_unlock(dev->subordinate); 5399 else 5400 pci_dev_unlock(dev); 5401 } 5402 5403 if (bridge) 5404 pci_dev_unlock(bridge); 5405 } 5406 5407 /* Return 1 on successful lock, 0 on contention */ 5408 static int pci_slot_trylock(struct pci_slot *slot) 5409 { 5410 struct pci_dev *dev, *bridge = slot->bus->self; 5411 5412 if (bridge && !pci_dev_trylock(bridge)) 5413 return 0; 5414 5415 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5416 if (!dev->slot || dev->slot != slot) 5417 continue; 5418 if (dev->subordinate) { 5419 if (!pci_bus_trylock(dev->subordinate)) 5420 goto unlock; 5421 } else if (!pci_dev_trylock(dev)) 5422 goto unlock; 5423 } 5424 return 1; 5425 5426 unlock: 5427 list_for_each_entry_continue_reverse(dev, 5428 &slot->bus->devices, bus_list) { 5429 if (!dev->slot || dev->slot != slot) 5430 continue; 5431 if (dev->subordinate) 5432 pci_bus_unlock(dev->subordinate); 5433 else 5434 pci_dev_unlock(dev); 5435 } 5436 5437 if (bridge) 5438 pci_dev_unlock(bridge); 5439 return 0; 5440 } 5441 5442 /* 5443 * Save and disable devices from the top of the tree down while holding 5444 * the @dev mutex lock for the entire tree. 5445 */ 5446 static void pci_bus_save_and_disable_locked(struct pci_bus *bus) 5447 { 5448 struct pci_dev *dev; 5449 5450 list_for_each_entry(dev, &bus->devices, bus_list) { 5451 pci_dev_save_and_disable(dev); 5452 if (dev->subordinate) 5453 pci_bus_save_and_disable_locked(dev->subordinate); 5454 } 5455 } 5456 5457 /* 5458 * Restore devices from top of the tree down while holding @dev mutex lock 5459 * for the entire tree. Parent bridges need to be restored before we can 5460 * get to subordinate devices. 5461 */ 5462 static void pci_bus_restore_locked(struct pci_bus *bus) 5463 { 5464 struct pci_dev *dev; 5465 5466 list_for_each_entry(dev, &bus->devices, bus_list) { 5467 pci_dev_restore(dev); 5468 if (dev->subordinate) { 5469 pci_bridge_wait_for_secondary_bus(dev, "bus reset"); 5470 pci_bus_restore_locked(dev->subordinate); 5471 } 5472 } 5473 } 5474 5475 /* 5476 * Save and disable devices from the top of the tree down while holding 5477 * the @dev mutex lock for the entire tree. 5478 */ 5479 static void pci_slot_save_and_disable_locked(struct pci_slot *slot) 5480 { 5481 struct pci_dev *dev; 5482 5483 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5484 if (!dev->slot || dev->slot != slot) 5485 continue; 5486 pci_dev_save_and_disable(dev); 5487 if (dev->subordinate) 5488 pci_bus_save_and_disable_locked(dev->subordinate); 5489 } 5490 } 5491 5492 /* 5493 * Restore devices from top of the tree down while holding @dev mutex lock 5494 * for the entire tree. Parent bridges need to be restored before we can 5495 * get to subordinate devices. 5496 */ 5497 static void pci_slot_restore_locked(struct pci_slot *slot) 5498 { 5499 struct pci_dev *dev; 5500 5501 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5502 if (!dev->slot || dev->slot != slot) 5503 continue; 5504 pci_dev_restore(dev); 5505 if (dev->subordinate) { 5506 pci_bridge_wait_for_secondary_bus(dev, "slot reset"); 5507 pci_bus_restore_locked(dev->subordinate); 5508 } 5509 } 5510 } 5511 5512 static int pci_slot_reset(struct pci_slot *slot, bool probe) 5513 { 5514 int rc; 5515 5516 if (!slot || !pci_slot_resettable(slot)) 5517 return -ENOTTY; 5518 5519 if (!probe) 5520 pci_slot_lock(slot); 5521 5522 might_sleep(); 5523 5524 rc = pci_reset_hotplug_slot(slot->hotplug, probe); 5525 5526 if (!probe) 5527 pci_slot_unlock(slot); 5528 5529 return rc; 5530 } 5531 5532 /** 5533 * pci_probe_reset_slot - probe whether a PCI slot can be reset 5534 * @slot: PCI slot to probe 5535 * 5536 * Return 0 if slot can be reset, negative if a slot reset is not supported. 5537 */ 5538 int pci_probe_reset_slot(struct pci_slot *slot) 5539 { 5540 return pci_slot_reset(slot, PCI_RESET_PROBE); 5541 } 5542 EXPORT_SYMBOL_GPL(pci_probe_reset_slot); 5543 5544 /** 5545 * __pci_reset_slot - Try to reset a PCI slot 5546 * @slot: PCI slot to reset 5547 * 5548 * A PCI bus may host multiple slots, each slot may support a reset mechanism 5549 * independent of other slots. For instance, some slots may support slot power 5550 * control. In the case of a 1:1 bus to slot architecture, this function may 5551 * wrap the bus reset to avoid spurious slot related events such as hotplug. 5552 * Generally a slot reset should be attempted before a bus reset. All of the 5553 * function of the slot and any subordinate buses behind the slot are reset 5554 * through this function. PCI config space of all devices in the slot and 5555 * behind the slot is saved before and restored after reset. 5556 * 5557 * Same as above except return -EAGAIN if the slot cannot be locked 5558 */ 5559 static int __pci_reset_slot(struct pci_slot *slot) 5560 { 5561 int rc; 5562 5563 rc = pci_slot_reset(slot, PCI_RESET_PROBE); 5564 if (rc) 5565 return rc; 5566 5567 if (pci_slot_trylock(slot)) { 5568 pci_slot_save_and_disable_locked(slot); 5569 might_sleep(); 5570 rc = pci_reset_hotplug_slot(slot->hotplug, PCI_RESET_DO_RESET); 5571 pci_slot_restore_locked(slot); 5572 pci_slot_unlock(slot); 5573 } else 5574 rc = -EAGAIN; 5575 5576 return rc; 5577 } 5578 5579 static int pci_bus_reset(struct pci_bus *bus, bool probe) 5580 { 5581 int ret; 5582 5583 if (!bus->self || !pci_bus_resettable(bus)) 5584 return -ENOTTY; 5585 5586 if (probe) 5587 return 0; 5588 5589 pci_bus_lock(bus); 5590 5591 might_sleep(); 5592 5593 ret = pci_bridge_secondary_bus_reset(bus->self); 5594 5595 pci_bus_unlock(bus); 5596 5597 return ret; 5598 } 5599 5600 /** 5601 * pci_bus_error_reset - reset the bridge's subordinate bus 5602 * @bridge: The parent device that connects to the bus to reset 5603 * 5604 * This function will first try to reset the slots on this bus if the method is 5605 * available. If slot reset fails or is not available, this will fall back to a 5606 * secondary bus reset. 5607 */ 5608 int pci_bus_error_reset(struct pci_dev *bridge) 5609 { 5610 struct pci_bus *bus = bridge->subordinate; 5611 struct pci_slot *slot; 5612 5613 if (!bus) 5614 return -ENOTTY; 5615 5616 mutex_lock(&pci_slot_mutex); 5617 if (list_empty(&bus->slots)) 5618 goto bus_reset; 5619 5620 list_for_each_entry(slot, &bus->slots, list) 5621 if (pci_probe_reset_slot(slot)) 5622 goto bus_reset; 5623 5624 list_for_each_entry(slot, &bus->slots, list) 5625 if (pci_slot_reset(slot, PCI_RESET_DO_RESET)) 5626 goto bus_reset; 5627 5628 mutex_unlock(&pci_slot_mutex); 5629 return 0; 5630 bus_reset: 5631 mutex_unlock(&pci_slot_mutex); 5632 return pci_bus_reset(bridge->subordinate, PCI_RESET_DO_RESET); 5633 } 5634 5635 /** 5636 * pci_probe_reset_bus - probe whether a PCI bus can be reset 5637 * @bus: PCI bus to probe 5638 * 5639 * Return 0 if bus can be reset, negative if a bus reset is not supported. 5640 */ 5641 int pci_probe_reset_bus(struct pci_bus *bus) 5642 { 5643 return pci_bus_reset(bus, PCI_RESET_PROBE); 5644 } 5645 EXPORT_SYMBOL_GPL(pci_probe_reset_bus); 5646 5647 /** 5648 * __pci_reset_bus - Try to reset a PCI bus 5649 * @bus: top level PCI bus to reset 5650 * 5651 * Same as above except return -EAGAIN if the bus cannot be locked 5652 */ 5653 int __pci_reset_bus(struct pci_bus *bus) 5654 { 5655 int rc; 5656 5657 rc = pci_bus_reset(bus, PCI_RESET_PROBE); 5658 if (rc) 5659 return rc; 5660 5661 if (pci_bus_trylock(bus)) { 5662 pci_bus_save_and_disable_locked(bus); 5663 might_sleep(); 5664 rc = pci_bridge_secondary_bus_reset(bus->self); 5665 pci_bus_restore_locked(bus); 5666 pci_bus_unlock(bus); 5667 } else 5668 rc = -EAGAIN; 5669 5670 return rc; 5671 } 5672 5673 /** 5674 * pci_reset_bus - Try to reset a PCI bus 5675 * @pdev: top level PCI device to reset via slot/bus 5676 * 5677 * Same as above except return -EAGAIN if the bus cannot be locked 5678 */ 5679 int pci_reset_bus(struct pci_dev *pdev) 5680 { 5681 return (!pci_probe_reset_slot(pdev->slot)) ? 5682 __pci_reset_slot(pdev->slot) : __pci_reset_bus(pdev->bus); 5683 } 5684 EXPORT_SYMBOL_GPL(pci_reset_bus); 5685 5686 /** 5687 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count 5688 * @dev: PCI device to query 5689 * 5690 * Returns mmrbc: maximum designed memory read count in bytes or 5691 * appropriate error value. 5692 */ 5693 int pcix_get_max_mmrbc(struct pci_dev *dev) 5694 { 5695 int cap; 5696 u32 stat; 5697 5698 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); 5699 if (!cap) 5700 return -EINVAL; 5701 5702 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat)) 5703 return -EINVAL; 5704 5705 return 512 << FIELD_GET(PCI_X_STATUS_MAX_READ, stat); 5706 } 5707 EXPORT_SYMBOL(pcix_get_max_mmrbc); 5708 5709 /** 5710 * pcix_get_mmrbc - get PCI-X maximum memory read byte count 5711 * @dev: PCI device to query 5712 * 5713 * Returns mmrbc: maximum memory read count in bytes or appropriate error 5714 * value. 5715 */ 5716 int pcix_get_mmrbc(struct pci_dev *dev) 5717 { 5718 int cap; 5719 u16 cmd; 5720 5721 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); 5722 if (!cap) 5723 return -EINVAL; 5724 5725 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd)) 5726 return -EINVAL; 5727 5728 return 512 << FIELD_GET(PCI_X_CMD_MAX_READ, cmd); 5729 } 5730 EXPORT_SYMBOL(pcix_get_mmrbc); 5731 5732 /** 5733 * pcix_set_mmrbc - set PCI-X maximum memory read byte count 5734 * @dev: PCI device to query 5735 * @mmrbc: maximum memory read count in bytes 5736 * valid values are 512, 1024, 2048, 4096 5737 * 5738 * If possible sets maximum memory read byte count, some bridges have errata 5739 * that prevent this. 5740 */ 5741 int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc) 5742 { 5743 int cap; 5744 u32 stat, v, o; 5745 u16 cmd; 5746 5747 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc)) 5748 return -EINVAL; 5749 5750 v = ffs(mmrbc) - 10; 5751 5752 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); 5753 if (!cap) 5754 return -EINVAL; 5755 5756 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat)) 5757 return -EINVAL; 5758 5759 if (v > FIELD_GET(PCI_X_STATUS_MAX_READ, stat)) 5760 return -E2BIG; 5761 5762 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd)) 5763 return -EINVAL; 5764 5765 o = FIELD_GET(PCI_X_CMD_MAX_READ, cmd); 5766 if (o != v) { 5767 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC)) 5768 return -EIO; 5769 5770 cmd &= ~PCI_X_CMD_MAX_READ; 5771 cmd |= FIELD_PREP(PCI_X_CMD_MAX_READ, v); 5772 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd)) 5773 return -EIO; 5774 } 5775 return 0; 5776 } 5777 EXPORT_SYMBOL(pcix_set_mmrbc); 5778 5779 /** 5780 * pcie_get_readrq - get PCI Express read request size 5781 * @dev: PCI device to query 5782 * 5783 * Returns maximum memory read request in bytes or appropriate error value. 5784 */ 5785 int pcie_get_readrq(struct pci_dev *dev) 5786 { 5787 u16 ctl; 5788 5789 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl); 5790 5791 return 128 << FIELD_GET(PCI_EXP_DEVCTL_READRQ, ctl); 5792 } 5793 EXPORT_SYMBOL(pcie_get_readrq); 5794 5795 /** 5796 * pcie_set_readrq - set PCI Express maximum memory read request 5797 * @dev: PCI device to query 5798 * @rq: maximum memory read count in bytes 5799 * valid values are 128, 256, 512, 1024, 2048, 4096 5800 * 5801 * If possible sets maximum memory read request in bytes 5802 */ 5803 int pcie_set_readrq(struct pci_dev *dev, int rq) 5804 { 5805 u16 v; 5806 int ret; 5807 unsigned int firstbit; 5808 struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus); 5809 5810 if (rq < 128 || rq > 4096 || !is_power_of_2(rq)) 5811 return -EINVAL; 5812 5813 /* 5814 * If using the "performance" PCIe config, we clamp the read rq 5815 * size to the max packet size to keep the host bridge from 5816 * generating requests larger than we can cope with. 5817 */ 5818 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) { 5819 int mps = pcie_get_mps(dev); 5820 5821 if (mps < rq) 5822 rq = mps; 5823 } 5824 5825 firstbit = ffs(rq); 5826 if (firstbit < 8) 5827 return -EINVAL; 5828 v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, firstbit - 8); 5829 5830 if (bridge->no_inc_mrrs) { 5831 int max_mrrs = pcie_get_readrq(dev); 5832 5833 if (rq > max_mrrs) { 5834 pci_info(dev, "can't set Max_Read_Request_Size to %d; max is %d\n", rq, max_mrrs); 5835 return -EINVAL; 5836 } 5837 } 5838 5839 ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL, 5840 PCI_EXP_DEVCTL_READRQ, v); 5841 5842 return pcibios_err_to_errno(ret); 5843 } 5844 EXPORT_SYMBOL(pcie_set_readrq); 5845 5846 /** 5847 * pcie_get_mps - get PCI Express maximum payload size 5848 * @dev: PCI device to query 5849 * 5850 * Returns maximum payload size in bytes 5851 */ 5852 int pcie_get_mps(struct pci_dev *dev) 5853 { 5854 u16 ctl; 5855 5856 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl); 5857 5858 return 128 << FIELD_GET(PCI_EXP_DEVCTL_PAYLOAD, ctl); 5859 } 5860 EXPORT_SYMBOL(pcie_get_mps); 5861 5862 /** 5863 * pcie_set_mps - set PCI Express maximum payload size 5864 * @dev: PCI device to query 5865 * @mps: maximum payload size in bytes 5866 * valid values are 128, 256, 512, 1024, 2048, 4096 5867 * 5868 * If possible sets maximum payload size 5869 */ 5870 int pcie_set_mps(struct pci_dev *dev, int mps) 5871 { 5872 u16 v; 5873 int ret; 5874 5875 if (mps < 128 || mps > 4096 || !is_power_of_2(mps)) 5876 return -EINVAL; 5877 5878 v = ffs(mps) - 8; 5879 if (v > dev->pcie_mpss) 5880 return -EINVAL; 5881 v = FIELD_PREP(PCI_EXP_DEVCTL_PAYLOAD, v); 5882 5883 ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL, 5884 PCI_EXP_DEVCTL_PAYLOAD, v); 5885 5886 return pcibios_err_to_errno(ret); 5887 } 5888 EXPORT_SYMBOL(pcie_set_mps); 5889 5890 static enum pci_bus_speed to_pcie_link_speed(u16 lnksta) 5891 { 5892 return pcie_link_speed[FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta)]; 5893 } 5894 5895 int pcie_link_speed_mbps(struct pci_dev *pdev) 5896 { 5897 u16 lnksta; 5898 int err; 5899 5900 err = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta); 5901 if (err) 5902 return err; 5903 5904 return pcie_dev_speed_mbps(to_pcie_link_speed(lnksta)); 5905 } 5906 EXPORT_SYMBOL(pcie_link_speed_mbps); 5907 5908 /** 5909 * pcie_bandwidth_available - determine minimum link settings of a PCIe 5910 * device and its bandwidth limitation 5911 * @dev: PCI device to query 5912 * @limiting_dev: storage for device causing the bandwidth limitation 5913 * @speed: storage for speed of limiting device 5914 * @width: storage for width of limiting device 5915 * 5916 * Walk up the PCI device chain and find the point where the minimum 5917 * bandwidth is available. Return the bandwidth available there and (if 5918 * limiting_dev, speed, and width pointers are supplied) information about 5919 * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of 5920 * raw bandwidth. 5921 */ 5922 u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev, 5923 enum pci_bus_speed *speed, 5924 enum pcie_link_width *width) 5925 { 5926 u16 lnksta; 5927 enum pci_bus_speed next_speed; 5928 enum pcie_link_width next_width; 5929 u32 bw, next_bw; 5930 5931 if (speed) 5932 *speed = PCI_SPEED_UNKNOWN; 5933 if (width) 5934 *width = PCIE_LNK_WIDTH_UNKNOWN; 5935 5936 bw = 0; 5937 5938 while (dev) { 5939 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta); 5940 5941 next_speed = to_pcie_link_speed(lnksta); 5942 next_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta); 5943 5944 next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed); 5945 5946 /* Check if current device limits the total bandwidth */ 5947 if (!bw || next_bw <= bw) { 5948 bw = next_bw; 5949 5950 if (limiting_dev) 5951 *limiting_dev = dev; 5952 if (speed) 5953 *speed = next_speed; 5954 if (width) 5955 *width = next_width; 5956 } 5957 5958 dev = pci_upstream_bridge(dev); 5959 } 5960 5961 return bw; 5962 } 5963 EXPORT_SYMBOL(pcie_bandwidth_available); 5964 5965 /** 5966 * pcie_get_supported_speeds - query Supported Link Speed Vector 5967 * @dev: PCI device to query 5968 * 5969 * Query @dev supported link speeds. 5970 * 5971 * Implementation Note in PCIe r6.0 sec 7.5.3.18 recommends determining 5972 * supported link speeds using the Supported Link Speeds Vector in the Link 5973 * Capabilities 2 Register (when available). 5974 * 5975 * Link Capabilities 2 was added in PCIe r3.0, sec 7.8.18. 5976 * 5977 * Without Link Capabilities 2, i.e., prior to PCIe r3.0, Supported Link 5978 * Speeds field in Link Capabilities is used and only 2.5 GT/s and 5.0 GT/s 5979 * speeds were defined. 5980 * 5981 * For @dev without Supported Link Speed Vector, the field is synthesized 5982 * from the Max Link Speed field in the Link Capabilities Register. 5983 * 5984 * Return: Supported Link Speeds Vector (+ reserved 0 at LSB). 5985 */ 5986 u8 pcie_get_supported_speeds(struct pci_dev *dev) 5987 { 5988 u32 lnkcap2, lnkcap; 5989 u8 speeds; 5990 5991 /* 5992 * Speeds retain the reserved 0 at LSB before PCIe Supported Link 5993 * Speeds Vector to allow using SLS Vector bit defines directly. 5994 */ 5995 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2); 5996 speeds = lnkcap2 & PCI_EXP_LNKCAP2_SLS; 5997 5998 /* Ignore speeds higher than Max Link Speed */ 5999 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap); 6000 speeds &= GENMASK(lnkcap & PCI_EXP_LNKCAP_SLS, 0); 6001 6002 /* PCIe r3.0-compliant */ 6003 if (speeds) 6004 return speeds; 6005 6006 /* Synthesize from the Max Link Speed field */ 6007 if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB) 6008 speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB; 6009 else if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_2_5GB) 6010 speeds = PCI_EXP_LNKCAP2_SLS_2_5GB; 6011 6012 return speeds; 6013 } 6014 6015 /** 6016 * pcie_get_speed_cap - query for the PCI device's link speed capability 6017 * @dev: PCI device to query 6018 * 6019 * Query the PCI device speed capability. 6020 * 6021 * Return: the maximum link speed supported by the device. 6022 */ 6023 enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev) 6024 { 6025 return PCIE_LNKCAP2_SLS2SPEED(dev->supported_speeds); 6026 } 6027 EXPORT_SYMBOL(pcie_get_speed_cap); 6028 6029 /** 6030 * pcie_get_width_cap - query for the PCI device's link width capability 6031 * @dev: PCI device to query 6032 * 6033 * Query the PCI device width capability. Return the maximum link width 6034 * supported by the device. 6035 */ 6036 enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev) 6037 { 6038 u32 lnkcap; 6039 6040 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap); 6041 if (lnkcap) 6042 return FIELD_GET(PCI_EXP_LNKCAP_MLW, lnkcap); 6043 6044 return PCIE_LNK_WIDTH_UNKNOWN; 6045 } 6046 EXPORT_SYMBOL(pcie_get_width_cap); 6047 6048 /** 6049 * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability 6050 * @dev: PCI device 6051 * @speed: storage for link speed 6052 * @width: storage for link width 6053 * 6054 * Calculate a PCI device's link bandwidth by querying for its link speed 6055 * and width, multiplying them, and applying encoding overhead. The result 6056 * is in Mb/s, i.e., megabits/second of raw bandwidth. 6057 */ 6058 static u32 pcie_bandwidth_capable(struct pci_dev *dev, 6059 enum pci_bus_speed *speed, 6060 enum pcie_link_width *width) 6061 { 6062 *speed = pcie_get_speed_cap(dev); 6063 *width = pcie_get_width_cap(dev); 6064 6065 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN) 6066 return 0; 6067 6068 return *width * PCIE_SPEED2MBS_ENC(*speed); 6069 } 6070 6071 /** 6072 * __pcie_print_link_status - Report the PCI device's link speed and width 6073 * @dev: PCI device to query 6074 * @verbose: Print info even when enough bandwidth is available 6075 * 6076 * If the available bandwidth at the device is less than the device is 6077 * capable of, report the device's maximum possible bandwidth and the 6078 * upstream link that limits its performance. If @verbose, always print 6079 * the available bandwidth, even if the device isn't constrained. 6080 */ 6081 void __pcie_print_link_status(struct pci_dev *dev, bool verbose) 6082 { 6083 enum pcie_link_width width, width_cap; 6084 enum pci_bus_speed speed, speed_cap; 6085 struct pci_dev *limiting_dev = NULL; 6086 u32 bw_avail, bw_cap; 6087 char *flit_mode = ""; 6088 6089 bw_cap = pcie_bandwidth_capable(dev, &speed_cap, &width_cap); 6090 bw_avail = pcie_bandwidth_available(dev, &limiting_dev, &speed, &width); 6091 6092 if (dev->bus && dev->bus->flit_mode) 6093 flit_mode = ", in Flit mode"; 6094 6095 if (bw_avail >= bw_cap && verbose) 6096 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth (%s x%d link)%s\n", 6097 bw_cap / 1000, bw_cap % 1000, 6098 pci_speed_string(speed_cap), width_cap, flit_mode); 6099 else if (bw_avail < bw_cap) 6100 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth, limited by %s x%d link at %s (capable of %u.%03u Gb/s with %s x%d link)%s\n", 6101 bw_avail / 1000, bw_avail % 1000, 6102 pci_speed_string(speed), width, 6103 limiting_dev ? pci_name(limiting_dev) : "<unknown>", 6104 bw_cap / 1000, bw_cap % 1000, 6105 pci_speed_string(speed_cap), width_cap, flit_mode); 6106 } 6107 6108 /** 6109 * pcie_print_link_status - Report the PCI device's link speed and width 6110 * @dev: PCI device to query 6111 * 6112 * Report the available bandwidth at the device. 6113 */ 6114 void pcie_print_link_status(struct pci_dev *dev) 6115 { 6116 __pcie_print_link_status(dev, true); 6117 } 6118 EXPORT_SYMBOL(pcie_print_link_status); 6119 6120 /** 6121 * pci_select_bars - Make BAR mask from the type of resource 6122 * @dev: the PCI device for which BAR mask is made 6123 * @flags: resource type mask to be selected 6124 * 6125 * This helper routine makes bar mask from the type of resource. 6126 */ 6127 int pci_select_bars(struct pci_dev *dev, unsigned long flags) 6128 { 6129 int i, bars = 0; 6130 for (i = 0; i < PCI_NUM_RESOURCES; i++) 6131 if (pci_resource_flags(dev, i) & flags) 6132 bars |= (1 << i); 6133 return bars; 6134 } 6135 EXPORT_SYMBOL(pci_select_bars); 6136 6137 /* Some architectures require additional programming to enable VGA */ 6138 static arch_set_vga_state_t arch_set_vga_state; 6139 6140 void __init pci_register_set_vga_state(arch_set_vga_state_t func) 6141 { 6142 arch_set_vga_state = func; /* NULL disables */ 6143 } 6144 6145 static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode, 6146 unsigned int command_bits, u32 flags) 6147 { 6148 if (arch_set_vga_state) 6149 return arch_set_vga_state(dev, decode, command_bits, 6150 flags); 6151 return 0; 6152 } 6153 6154 /** 6155 * pci_set_vga_state - set VGA decode state on device and parents if requested 6156 * @dev: the PCI device 6157 * @decode: true = enable decoding, false = disable decoding 6158 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY 6159 * @flags: traverse ancestors and change bridges 6160 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE 6161 */ 6162 int pci_set_vga_state(struct pci_dev *dev, bool decode, 6163 unsigned int command_bits, u32 flags) 6164 { 6165 struct pci_bus *bus; 6166 struct pci_dev *bridge; 6167 u16 cmd; 6168 int rc; 6169 6170 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY))); 6171 6172 /* ARCH specific VGA enables */ 6173 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags); 6174 if (rc) 6175 return rc; 6176 6177 if (flags & PCI_VGA_STATE_CHANGE_DECODES) { 6178 pci_read_config_word(dev, PCI_COMMAND, &cmd); 6179 if (decode) 6180 cmd |= command_bits; 6181 else 6182 cmd &= ~command_bits; 6183 pci_write_config_word(dev, PCI_COMMAND, cmd); 6184 } 6185 6186 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE)) 6187 return 0; 6188 6189 bus = dev->bus; 6190 while (bus) { 6191 bridge = bus->self; 6192 if (bridge) { 6193 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, 6194 &cmd); 6195 if (decode) 6196 cmd |= PCI_BRIDGE_CTL_VGA; 6197 else 6198 cmd &= ~PCI_BRIDGE_CTL_VGA; 6199 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, 6200 cmd); 6201 } 6202 bus = bus->parent; 6203 } 6204 return 0; 6205 } 6206 6207 #ifdef CONFIG_ACPI 6208 bool pci_pr3_present(struct pci_dev *pdev) 6209 { 6210 struct acpi_device *adev; 6211 6212 if (acpi_disabled) 6213 return false; 6214 6215 adev = ACPI_COMPANION(&pdev->dev); 6216 if (!adev) 6217 return false; 6218 6219 return adev->power.flags.power_resources && 6220 acpi_has_method(adev->handle, "_PR3"); 6221 } 6222 EXPORT_SYMBOL_GPL(pci_pr3_present); 6223 #endif 6224 6225 /** 6226 * pci_add_dma_alias - Add a DMA devfn alias for a device 6227 * @dev: the PCI device for which alias is added 6228 * @devfn_from: alias slot and function 6229 * @nr_devfns: number of subsequent devfns to alias 6230 * 6231 * This helper encodes an 8-bit devfn as a bit number in dma_alias_mask 6232 * which is used to program permissible bus-devfn source addresses for DMA 6233 * requests in an IOMMU. These aliases factor into IOMMU group creation 6234 * and are useful for devices generating DMA requests beyond or different 6235 * from their logical bus-devfn. Examples include device quirks where the 6236 * device simply uses the wrong devfn, as well as non-transparent bridges 6237 * where the alias may be a proxy for devices in another domain. 6238 * 6239 * IOMMU group creation is performed during device discovery or addition, 6240 * prior to any potential DMA mapping and therefore prior to driver probing 6241 * (especially for userspace assigned devices where IOMMU group definition 6242 * cannot be left as a userspace activity). DMA aliases should therefore 6243 * be configured via quirks, such as the PCI fixup header quirk. 6244 */ 6245 void pci_add_dma_alias(struct pci_dev *dev, u8 devfn_from, 6246 unsigned int nr_devfns) 6247 { 6248 int devfn_to; 6249 6250 nr_devfns = min(nr_devfns, (unsigned int)MAX_NR_DEVFNS - devfn_from); 6251 devfn_to = devfn_from + nr_devfns - 1; 6252 6253 if (!dev->dma_alias_mask) 6254 dev->dma_alias_mask = bitmap_zalloc(MAX_NR_DEVFNS, GFP_KERNEL); 6255 if (!dev->dma_alias_mask) { 6256 pci_warn(dev, "Unable to allocate DMA alias mask\n"); 6257 return; 6258 } 6259 6260 bitmap_set(dev->dma_alias_mask, devfn_from, nr_devfns); 6261 6262 if (nr_devfns == 1) 6263 pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n", 6264 PCI_SLOT(devfn_from), PCI_FUNC(devfn_from)); 6265 else if (nr_devfns > 1) 6266 pci_info(dev, "Enabling fixed DMA alias for devfn range from %02x.%d to %02x.%d\n", 6267 PCI_SLOT(devfn_from), PCI_FUNC(devfn_from), 6268 PCI_SLOT(devfn_to), PCI_FUNC(devfn_to)); 6269 } 6270 6271 bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2) 6272 { 6273 return (dev1->dma_alias_mask && 6274 test_bit(dev2->devfn, dev1->dma_alias_mask)) || 6275 (dev2->dma_alias_mask && 6276 test_bit(dev1->devfn, dev2->dma_alias_mask)) || 6277 pci_real_dma_dev(dev1) == dev2 || 6278 pci_real_dma_dev(dev2) == dev1; 6279 } 6280 6281 bool pci_device_is_present(struct pci_dev *pdev) 6282 { 6283 u32 v; 6284 6285 /* Check PF if pdev is a VF, since VF Vendor/Device IDs are 0xffff */ 6286 pdev = pci_physfn(pdev); 6287 if (pci_dev_is_disconnected(pdev)) 6288 return false; 6289 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0); 6290 } 6291 EXPORT_SYMBOL_GPL(pci_device_is_present); 6292 6293 void pci_ignore_hotplug(struct pci_dev *dev) 6294 { 6295 struct pci_dev *bridge = dev->bus->self; 6296 6297 dev->ignore_hotplug = 1; 6298 /* Propagate the "ignore hotplug" setting to the parent bridge. */ 6299 if (bridge) 6300 bridge->ignore_hotplug = 1; 6301 } 6302 EXPORT_SYMBOL_GPL(pci_ignore_hotplug); 6303 6304 /** 6305 * pci_real_dma_dev - Get PCI DMA device for PCI device 6306 * @dev: the PCI device that may have a PCI DMA alias 6307 * 6308 * Permits the platform to provide architecture-specific functionality to 6309 * devices needing to alias DMA to another PCI device on another PCI bus. If 6310 * the PCI device is on the same bus, it is recommended to use 6311 * pci_add_dma_alias(). This is the default implementation. Architecture 6312 * implementations can override this. 6313 */ 6314 struct pci_dev __weak *pci_real_dma_dev(struct pci_dev *dev) 6315 { 6316 return dev; 6317 } 6318 6319 resource_size_t __weak pcibios_default_alignment(void) 6320 { 6321 return 0; 6322 } 6323 6324 /* 6325 * Arches that don't want to expose struct resource to userland as-is in 6326 * sysfs and /proc can implement their own pci_resource_to_user(). 6327 */ 6328 void __weak pci_resource_to_user(const struct pci_dev *dev, int bar, 6329 const struct resource *rsrc, 6330 resource_size_t *start, resource_size_t *end) 6331 { 6332 *start = rsrc->start; 6333 *end = rsrc->end; 6334 } 6335 6336 static char *resource_alignment_param; 6337 static DEFINE_SPINLOCK(resource_alignment_lock); 6338 6339 /** 6340 * pci_specified_resource_alignment - get resource alignment specified by user. 6341 * @dev: the PCI device to get 6342 * @resize: whether or not to change resources' size when reassigning alignment 6343 * 6344 * RETURNS: Resource alignment if it is specified. 6345 * Zero if it is not specified. 6346 */ 6347 static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev, 6348 bool *resize) 6349 { 6350 int align_order, count; 6351 resource_size_t align = pcibios_default_alignment(); 6352 const char *p; 6353 int ret; 6354 6355 spin_lock(&resource_alignment_lock); 6356 p = resource_alignment_param; 6357 if (!p || !*p) 6358 goto out; 6359 if (pci_has_flag(PCI_PROBE_ONLY)) { 6360 align = 0; 6361 pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n"); 6362 goto out; 6363 } 6364 6365 while (*p) { 6366 count = 0; 6367 if (sscanf(p, "%d%n", &align_order, &count) == 1 && 6368 p[count] == '@') { 6369 p += count + 1; 6370 if (align_order > 63) { 6371 pr_err("PCI: Invalid requested alignment (order %d)\n", 6372 align_order); 6373 align_order = PAGE_SHIFT; 6374 } 6375 } else { 6376 align_order = PAGE_SHIFT; 6377 } 6378 6379 ret = pci_dev_str_match(dev, p, &p); 6380 if (ret == 1) { 6381 *resize = true; 6382 align = 1ULL << align_order; 6383 break; 6384 } else if (ret < 0) { 6385 pr_err("PCI: Can't parse resource_alignment parameter: %s\n", 6386 p); 6387 break; 6388 } 6389 6390 if (*p != ';' && *p != ',') { 6391 /* End of param or invalid format */ 6392 break; 6393 } 6394 p++; 6395 } 6396 out: 6397 spin_unlock(&resource_alignment_lock); 6398 return align; 6399 } 6400 6401 static void pci_request_resource_alignment(struct pci_dev *dev, int bar, 6402 resource_size_t align, bool resize) 6403 { 6404 struct resource *r = &dev->resource[bar]; 6405 const char *r_name = pci_resource_name(dev, bar); 6406 resource_size_t size; 6407 6408 if (!(r->flags & IORESOURCE_MEM)) 6409 return; 6410 6411 if (r->flags & IORESOURCE_PCI_FIXED) { 6412 pci_info(dev, "%s %pR: ignoring requested alignment %#llx\n", 6413 r_name, r, (unsigned long long)align); 6414 return; 6415 } 6416 6417 size = resource_size(r); 6418 if (size >= align) 6419 return; 6420 6421 /* 6422 * Increase the alignment of the resource. There are two ways we 6423 * can do this: 6424 * 6425 * 1) Increase the size of the resource. BARs are aligned on their 6426 * size, so when we reallocate space for this resource, we'll 6427 * allocate it with the larger alignment. This also prevents 6428 * assignment of any other BARs inside the alignment region, so 6429 * if we're requesting page alignment, this means no other BARs 6430 * will share the page. 6431 * 6432 * The disadvantage is that this makes the resource larger than 6433 * the hardware BAR, which may break drivers that compute things 6434 * based on the resource size, e.g., to find registers at a 6435 * fixed offset before the end of the BAR. 6436 * 6437 * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and 6438 * set r->start to the desired alignment. By itself this 6439 * doesn't prevent other BARs being put inside the alignment 6440 * region, but if we realign *every* resource of every device in 6441 * the system, none of them will share an alignment region. 6442 * 6443 * When the user has requested alignment for only some devices via 6444 * the "pci=resource_alignment" argument, "resize" is true and we 6445 * use the first method. Otherwise we assume we're aligning all 6446 * devices and we use the second. 6447 */ 6448 6449 pci_info(dev, "%s %pR: requesting alignment to %#llx\n", 6450 r_name, r, (unsigned long long)align); 6451 6452 if (resize) { 6453 r->start = 0; 6454 r->end = align - 1; 6455 } else { 6456 r->flags &= ~IORESOURCE_SIZEALIGN; 6457 r->flags |= IORESOURCE_STARTALIGN; 6458 resource_set_range(r, align, size); 6459 } 6460 r->flags |= IORESOURCE_UNSET; 6461 } 6462 6463 /* 6464 * This function disables memory decoding and releases memory resources 6465 * of the device specified by kernel's boot parameter 'pci=resource_alignment='. 6466 * It also rounds up size to specified alignment. 6467 * Later on, the kernel will assign page-aligned memory resource back 6468 * to the device. 6469 */ 6470 void pci_reassigndev_resource_alignment(struct pci_dev *dev) 6471 { 6472 int i; 6473 struct resource *r; 6474 resource_size_t align; 6475 u16 command; 6476 bool resize = false; 6477 6478 /* 6479 * VF BARs are read-only zero according to SR-IOV spec r1.1, sec 6480 * 3.4.1.11. Their resources are allocated from the space 6481 * described by the VF BARx register in the PF's SR-IOV capability. 6482 * We can't influence their alignment here. 6483 */ 6484 if (dev->is_virtfn) 6485 return; 6486 6487 /* check if specified PCI is target device to reassign */ 6488 align = pci_specified_resource_alignment(dev, &resize); 6489 if (!align) 6490 return; 6491 6492 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL && 6493 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) { 6494 pci_warn(dev, "Can't reassign resources to host bridge\n"); 6495 return; 6496 } 6497 6498 pci_read_config_word(dev, PCI_COMMAND, &command); 6499 command &= ~PCI_COMMAND_MEMORY; 6500 pci_write_config_word(dev, PCI_COMMAND, command); 6501 6502 for (i = 0; i <= PCI_ROM_RESOURCE; i++) 6503 pci_request_resource_alignment(dev, i, align, resize); 6504 6505 /* 6506 * Need to disable bridge's resource window, 6507 * to enable the kernel to reassign new resource 6508 * window later on. 6509 */ 6510 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) { 6511 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) { 6512 r = &dev->resource[i]; 6513 if (!(r->flags & IORESOURCE_MEM)) 6514 continue; 6515 r->flags |= IORESOURCE_UNSET; 6516 r->end = resource_size(r) - 1; 6517 r->start = 0; 6518 } 6519 pci_disable_bridge_window(dev); 6520 } 6521 } 6522 6523 static ssize_t resource_alignment_show(const struct bus_type *bus, char *buf) 6524 { 6525 size_t count = 0; 6526 6527 spin_lock(&resource_alignment_lock); 6528 if (resource_alignment_param) 6529 count = sysfs_emit(buf, "%s\n", resource_alignment_param); 6530 spin_unlock(&resource_alignment_lock); 6531 6532 return count; 6533 } 6534 6535 static ssize_t resource_alignment_store(const struct bus_type *bus, 6536 const char *buf, size_t count) 6537 { 6538 char *param, *old, *end; 6539 6540 if (count >= (PAGE_SIZE - 1)) 6541 return -EINVAL; 6542 6543 param = kstrndup(buf, count, GFP_KERNEL); 6544 if (!param) 6545 return -ENOMEM; 6546 6547 end = strchr(param, '\n'); 6548 if (end) 6549 *end = '\0'; 6550 6551 spin_lock(&resource_alignment_lock); 6552 old = resource_alignment_param; 6553 if (strlen(param)) { 6554 resource_alignment_param = param; 6555 } else { 6556 kfree(param); 6557 resource_alignment_param = NULL; 6558 } 6559 spin_unlock(&resource_alignment_lock); 6560 6561 kfree(old); 6562 6563 return count; 6564 } 6565 6566 static BUS_ATTR_RW(resource_alignment); 6567 6568 static int __init pci_resource_alignment_sysfs_init(void) 6569 { 6570 return bus_create_file(&pci_bus_type, 6571 &bus_attr_resource_alignment); 6572 } 6573 late_initcall(pci_resource_alignment_sysfs_init); 6574 6575 static void pci_no_domains(void) 6576 { 6577 #ifdef CONFIG_PCI_DOMAINS 6578 pci_domains_supported = 0; 6579 #endif 6580 } 6581 6582 #ifdef CONFIG_PCI_DOMAINS 6583 static DEFINE_IDA(pci_domain_nr_dynamic_ida); 6584 6585 /** 6586 * pci_bus_find_emul_domain_nr() - allocate a PCI domain number per constraints 6587 * @hint: desired domain, 0 if any ID in the range of @min to @max is acceptable 6588 * @min: minimum allowable domain 6589 * @max: maximum allowable domain, no IDs higher than INT_MAX will be returned 6590 */ 6591 int pci_bus_find_emul_domain_nr(u32 hint, u32 min, u32 max) 6592 { 6593 return ida_alloc_range(&pci_domain_nr_dynamic_ida, max(hint, min), max, 6594 GFP_KERNEL); 6595 } 6596 EXPORT_SYMBOL_GPL(pci_bus_find_emul_domain_nr); 6597 6598 void pci_bus_release_emul_domain_nr(int domain_nr) 6599 { 6600 ida_free(&pci_domain_nr_dynamic_ida, domain_nr); 6601 } 6602 EXPORT_SYMBOL_GPL(pci_bus_release_emul_domain_nr); 6603 #endif 6604 6605 #ifdef CONFIG_PCI_DOMAINS_GENERIC 6606 static DEFINE_IDA(pci_domain_nr_static_ida); 6607 6608 static void of_pci_reserve_static_domain_nr(void) 6609 { 6610 struct device_node *np; 6611 int domain_nr; 6612 6613 for_each_node_by_type(np, "pci") { 6614 domain_nr = of_get_pci_domain_nr(np); 6615 if (domain_nr < 0) 6616 continue; 6617 /* 6618 * Permanently allocate domain_nr in dynamic_ida 6619 * to prevent it from dynamic allocation. 6620 */ 6621 ida_alloc_range(&pci_domain_nr_dynamic_ida, 6622 domain_nr, domain_nr, GFP_KERNEL); 6623 } 6624 } 6625 6626 static int of_pci_bus_find_domain_nr(struct device *parent) 6627 { 6628 static bool static_domains_reserved = false; 6629 int domain_nr; 6630 6631 /* On the first call scan device tree for static allocations. */ 6632 if (!static_domains_reserved) { 6633 of_pci_reserve_static_domain_nr(); 6634 static_domains_reserved = true; 6635 } 6636 6637 if (parent) { 6638 /* 6639 * If domain is in DT, allocate it in static IDA. This 6640 * prevents duplicate static allocations in case of errors 6641 * in DT. 6642 */ 6643 domain_nr = of_get_pci_domain_nr(parent->of_node); 6644 if (domain_nr >= 0) 6645 return ida_alloc_range(&pci_domain_nr_static_ida, 6646 domain_nr, domain_nr, 6647 GFP_KERNEL); 6648 } 6649 6650 /* 6651 * If domain was not specified in DT, choose a free ID from dynamic 6652 * allocations. All domain numbers from DT are permanently in 6653 * dynamic allocations to prevent assigning them to other DT nodes 6654 * without static domain. 6655 */ 6656 return ida_alloc(&pci_domain_nr_dynamic_ida, GFP_KERNEL); 6657 } 6658 6659 static void of_pci_bus_release_domain_nr(struct device *parent, int domain_nr) 6660 { 6661 if (domain_nr < 0) 6662 return; 6663 6664 /* Release domain from IDA where it was allocated. */ 6665 if (parent && of_get_pci_domain_nr(parent->of_node) == domain_nr) 6666 ida_free(&pci_domain_nr_static_ida, domain_nr); 6667 else 6668 ida_free(&pci_domain_nr_dynamic_ida, domain_nr); 6669 } 6670 6671 int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent) 6672 { 6673 return acpi_disabled ? of_pci_bus_find_domain_nr(parent) : 6674 acpi_pci_bus_find_domain_nr(bus); 6675 } 6676 6677 void pci_bus_release_domain_nr(struct device *parent, int domain_nr) 6678 { 6679 if (!acpi_disabled) 6680 return; 6681 of_pci_bus_release_domain_nr(parent, domain_nr); 6682 } 6683 #endif 6684 6685 /** 6686 * pci_ext_cfg_avail - can we access extended PCI config space? 6687 * 6688 * Returns 1 if we can access PCI extended config space (offsets 6689 * greater than 0xff). This is the default implementation. Architecture 6690 * implementations can override this. 6691 */ 6692 int __weak pci_ext_cfg_avail(void) 6693 { 6694 return 1; 6695 } 6696 6697 static int __init pci_setup(char *str) 6698 { 6699 while (str) { 6700 char *k = strchr(str, ','); 6701 if (k) 6702 *k++ = 0; 6703 if (*str && (str = pcibios_setup(str)) && *str) { 6704 if (!pci_setup_cardbus(str)) { 6705 /* Function handled the parameters */ 6706 } else if (!strcmp(str, "nomsi")) { 6707 pci_no_msi(); 6708 } else if (!strncmp(str, "noats", 5)) { 6709 pr_info("PCIe: ATS is disabled\n"); 6710 pcie_ats_disabled = true; 6711 } else if (!strcmp(str, "noaer")) { 6712 pci_no_aer(); 6713 } else if (!strcmp(str, "earlydump")) { 6714 pci_early_dump = true; 6715 } else if (!strncmp(str, "realloc=", 8)) { 6716 pci_realloc_get_opt(str + 8); 6717 } else if (!strncmp(str, "realloc", 7)) { 6718 pci_realloc_get_opt("on"); 6719 } else if (!strcmp(str, "nodomains")) { 6720 pci_no_domains(); 6721 } else if (!strncmp(str, "noari", 5)) { 6722 pcie_ari_disabled = true; 6723 } else if (!strncmp(str, "notph", 5)) { 6724 pci_no_tph(); 6725 } else if (!strncmp(str, "resource_alignment=", 19)) { 6726 resource_alignment_param = str + 19; 6727 } else if (!strncmp(str, "ecrc=", 5)) { 6728 pcie_ecrc_get_policy(str + 5); 6729 } else if (!strncmp(str, "hpiosize=", 9)) { 6730 pci_hotplug_io_size = memparse(str + 9, &str); 6731 } else if (!strncmp(str, "hpmmiosize=", 11)) { 6732 pci_hotplug_mmio_size = memparse(str + 11, &str); 6733 } else if (!strncmp(str, "hpmmioprefsize=", 15)) { 6734 pci_hotplug_mmio_pref_size = memparse(str + 15, &str); 6735 } else if (!strncmp(str, "hpmemsize=", 10)) { 6736 pci_hotplug_mmio_size = memparse(str + 10, &str); 6737 pci_hotplug_mmio_pref_size = pci_hotplug_mmio_size; 6738 } else if (!strncmp(str, "hpbussize=", 10)) { 6739 pci_hotplug_bus_size = 6740 simple_strtoul(str + 10, &str, 0); 6741 if (pci_hotplug_bus_size > 0xff) 6742 pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE; 6743 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) { 6744 pcie_bus_config = PCIE_BUS_TUNE_OFF; 6745 } else if (!strncmp(str, "pcie_bus_safe", 13)) { 6746 pcie_bus_config = PCIE_BUS_SAFE; 6747 } else if (!strncmp(str, "pcie_bus_perf", 13)) { 6748 pcie_bus_config = PCIE_BUS_PERFORMANCE; 6749 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) { 6750 pcie_bus_config = PCIE_BUS_PEER2PEER; 6751 } else if (!strncmp(str, "pcie_scan_all", 13)) { 6752 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS); 6753 } else if (!strncmp(str, "disable_acs_redir=", 18)) { 6754 disable_acs_redir_param = str + 18; 6755 } else if (!strncmp(str, "config_acs=", 11)) { 6756 config_acs_param = str + 11; 6757 } else { 6758 pr_err("PCI: Unknown option `%s'\n", str); 6759 } 6760 } 6761 str = k; 6762 } 6763 return 0; 6764 } 6765 early_param("pci", pci_setup); 6766 6767 /* 6768 * 'resource_alignment_param' and 'disable_acs_redir_param' are initialized 6769 * in pci_setup(), above, to point to data in the __initdata section which 6770 * will be freed after the init sequence is complete. We can't allocate memory 6771 * in pci_setup() because some architectures do not have any memory allocation 6772 * service available during an early_param() call. So we allocate memory and 6773 * copy the variable here before the init section is freed. 6774 * 6775 */ 6776 static int __init pci_realloc_setup_params(void) 6777 { 6778 resource_alignment_param = kstrdup(resource_alignment_param, 6779 GFP_KERNEL); 6780 disable_acs_redir_param = kstrdup(disable_acs_redir_param, GFP_KERNEL); 6781 config_acs_param = kstrdup(config_acs_param, GFP_KERNEL); 6782 6783 return 0; 6784 } 6785 pure_initcall(pci_realloc_setup_params); 6786