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_warn_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 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, 2245 PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED | 2246 PCI_EXP_DEVSTA_FED | PCI_EXP_DEVSTA_URD); 2247 } 2248 #endif 2249 2250 /** 2251 * pcie_clear_root_pme_status - Clear root port PME interrupt status. 2252 * @dev: PCIe root port or event collector. 2253 */ 2254 void pcie_clear_root_pme_status(struct pci_dev *dev) 2255 { 2256 pcie_capability_write_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME); 2257 } 2258 2259 /** 2260 * pci_check_pme_status - Check if given device has generated PME. 2261 * @dev: Device to check. 2262 * 2263 * Check the PME status of the device and if set, clear it and clear PME enable 2264 * (if set). Return 'true' if PME status and PME enable were both set or 2265 * 'false' otherwise. 2266 */ 2267 bool pci_check_pme_status(struct pci_dev *dev) 2268 { 2269 int pmcsr_pos; 2270 u16 pmcsr; 2271 bool ret = false; 2272 2273 if (!dev->pm_cap) 2274 return false; 2275 2276 pmcsr_pos = dev->pm_cap + PCI_PM_CTRL; 2277 pci_read_config_word(dev, pmcsr_pos, &pmcsr); 2278 if (!(pmcsr & PCI_PM_CTRL_PME_STATUS)) 2279 return false; 2280 2281 /* Clear PME status. */ 2282 pmcsr |= PCI_PM_CTRL_PME_STATUS; 2283 if (pmcsr & PCI_PM_CTRL_PME_ENABLE) { 2284 /* Disable PME to avoid interrupt flood. */ 2285 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; 2286 ret = true; 2287 } 2288 2289 pci_write_config_word(dev, pmcsr_pos, pmcsr); 2290 2291 return ret; 2292 } 2293 2294 /** 2295 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set. 2296 * @dev: Device to handle. 2297 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag. 2298 * 2299 * Check if @dev has generated PME and queue a resume request for it in that 2300 * case. 2301 */ 2302 static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset) 2303 { 2304 if (pme_poll_reset && dev->pme_poll) 2305 dev->pme_poll = false; 2306 2307 if (pci_check_pme_status(dev)) { 2308 pci_wakeup_event(dev); 2309 pm_request_resume(&dev->dev); 2310 } 2311 return 0; 2312 } 2313 2314 /** 2315 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary. 2316 * @bus: Top bus of the subtree to walk. 2317 */ 2318 void pci_pme_wakeup_bus(struct pci_bus *bus) 2319 { 2320 if (bus) 2321 pci_walk_bus(bus, pci_pme_wakeup, (void *)true); 2322 } 2323 2324 2325 /** 2326 * pci_pme_capable - check the capability of PCI device to generate PME# 2327 * @dev: PCI device to handle. 2328 * @state: PCI state from which device will issue PME#. 2329 */ 2330 bool pci_pme_capable(struct pci_dev *dev, pci_power_t state) 2331 { 2332 if (!dev->pm_cap) 2333 return false; 2334 2335 return !!(dev->pme_support & (1 << state)); 2336 } 2337 EXPORT_SYMBOL(pci_pme_capable); 2338 2339 static void pci_pme_list_scan(struct work_struct *work) 2340 { 2341 struct pci_pme_device *pme_dev, *n; 2342 2343 mutex_lock(&pci_pme_list_mutex); 2344 list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) { 2345 struct pci_dev *pdev = pme_dev->dev; 2346 2347 if (pdev->pme_poll) { 2348 struct pci_dev *bridge = pdev->bus->self; 2349 struct device *dev = &pdev->dev; 2350 struct device *bdev = bridge ? &bridge->dev : NULL; 2351 int bref = 0; 2352 2353 /* 2354 * If we have a bridge, it should be in an active/D0 2355 * state or the configuration space of subordinate 2356 * devices may not be accessible or stable over the 2357 * course of the call. 2358 */ 2359 if (bdev) { 2360 bref = pm_runtime_get_if_active(bdev); 2361 if (!bref) 2362 continue; 2363 2364 if (bridge->current_state != PCI_D0) 2365 goto put_bridge; 2366 } 2367 2368 /* 2369 * The device itself should be suspended but config 2370 * space must be accessible, therefore it cannot be in 2371 * D3cold. 2372 */ 2373 if (pm_runtime_suspended(dev) && 2374 pdev->current_state != PCI_D3cold) 2375 pci_pme_wakeup(pdev, NULL); 2376 2377 put_bridge: 2378 if (bref > 0) 2379 pm_runtime_put(bdev); 2380 } else { 2381 list_del(&pme_dev->list); 2382 kfree(pme_dev); 2383 } 2384 } 2385 if (!list_empty(&pci_pme_list)) 2386 queue_delayed_work(system_freezable_wq, &pci_pme_work, 2387 msecs_to_jiffies(PME_TIMEOUT)); 2388 mutex_unlock(&pci_pme_list_mutex); 2389 } 2390 2391 static void __pci_pme_active(struct pci_dev *dev, bool enable) 2392 { 2393 u16 pmcsr; 2394 2395 if (!dev->pme_support) 2396 return; 2397 2398 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 2399 /* Clear PME_Status by writing 1 to it and enable PME# */ 2400 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE; 2401 if (!enable) 2402 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; 2403 2404 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); 2405 } 2406 2407 /** 2408 * pci_pme_restore - Restore PME configuration after config space restore. 2409 * @dev: PCI device to update. 2410 */ 2411 void pci_pme_restore(struct pci_dev *dev) 2412 { 2413 u16 pmcsr; 2414 2415 if (!dev->pme_support) 2416 return; 2417 2418 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); 2419 if (dev->wakeup_prepared) { 2420 pmcsr |= PCI_PM_CTRL_PME_ENABLE; 2421 pmcsr &= ~PCI_PM_CTRL_PME_STATUS; 2422 } else { 2423 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; 2424 pmcsr |= PCI_PM_CTRL_PME_STATUS; 2425 } 2426 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); 2427 } 2428 2429 /** 2430 * pci_pme_active - enable or disable PCI device's PME# function 2431 * @dev: PCI device to handle. 2432 * @enable: 'true' to enable PME# generation; 'false' to disable it. 2433 * 2434 * The caller must verify that the device is capable of generating PME# before 2435 * calling this function with @enable equal to 'true'. 2436 */ 2437 void pci_pme_active(struct pci_dev *dev, bool enable) 2438 { 2439 __pci_pme_active(dev, enable); 2440 2441 /* 2442 * PCI (as opposed to PCIe) PME requires that the device have 2443 * its PME# line hooked up correctly. Not all hardware vendors 2444 * do this, so the PME never gets delivered and the device 2445 * remains asleep. The easiest way around this is to 2446 * periodically walk the list of suspended devices and check 2447 * whether any have their PME flag set. The assumption is that 2448 * we'll wake up often enough anyway that this won't be a huge 2449 * hit, and the power savings from the devices will still be a 2450 * win. 2451 * 2452 * Although PCIe uses in-band PME message instead of PME# line 2453 * to report PME, PME does not work for some PCIe devices in 2454 * reality. For example, there are devices that set their PME 2455 * status bits, but don't really bother to send a PME message; 2456 * there are PCI Express Root Ports that don't bother to 2457 * trigger interrupts when they receive PME messages from the 2458 * devices below. So PME poll is used for PCIe devices too. 2459 */ 2460 2461 if (dev->pme_poll) { 2462 struct pci_pme_device *pme_dev; 2463 if (enable) { 2464 pme_dev = kmalloc_obj(struct pci_pme_device); 2465 if (!pme_dev) { 2466 pci_warn(dev, "can't enable PME#\n"); 2467 return; 2468 } 2469 pme_dev->dev = dev; 2470 mutex_lock(&pci_pme_list_mutex); 2471 list_add(&pme_dev->list, &pci_pme_list); 2472 if (list_is_singular(&pci_pme_list)) 2473 queue_delayed_work(system_freezable_wq, 2474 &pci_pme_work, 2475 msecs_to_jiffies(PME_TIMEOUT)); 2476 mutex_unlock(&pci_pme_list_mutex); 2477 } else { 2478 mutex_lock(&pci_pme_list_mutex); 2479 list_for_each_entry(pme_dev, &pci_pme_list, list) { 2480 if (pme_dev->dev == dev) { 2481 list_del(&pme_dev->list); 2482 kfree(pme_dev); 2483 break; 2484 } 2485 } 2486 mutex_unlock(&pci_pme_list_mutex); 2487 } 2488 } 2489 2490 pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled"); 2491 } 2492 EXPORT_SYMBOL(pci_pme_active); 2493 2494 /** 2495 * __pci_enable_wake - enable PCI device as wakeup event source 2496 * @dev: PCI device affected 2497 * @state: PCI state from which device will issue wakeup events 2498 * @enable: True to enable event generation; false to disable 2499 * 2500 * This enables the device as a wakeup event source, or disables it. 2501 * When such events involves platform-specific hooks, those hooks are 2502 * called automatically by this routine. 2503 * 2504 * Devices with legacy power management (no standard PCI PM capabilities) 2505 * always require such platform hooks. 2506 * 2507 * RETURN VALUE: 2508 * 0 is returned on success 2509 * -EINVAL is returned if device is not supposed to wake up the system 2510 * Error code depending on the platform is returned if both the platform and 2511 * the native mechanism fail to enable the generation of wake-up events 2512 */ 2513 static int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable) 2514 { 2515 int ret = 0; 2516 2517 /* 2518 * Bridges that are not power-manageable directly only signal 2519 * wakeup on behalf of subordinate devices which is set up 2520 * elsewhere, so skip them. However, bridges that are 2521 * power-manageable may signal wakeup for themselves (for example, 2522 * on a hotplug event) and they need to be covered here. 2523 */ 2524 if (!pci_power_manageable(dev)) 2525 return 0; 2526 2527 /* Don't do the same thing twice in a row for one device. */ 2528 if (!!enable == !!dev->wakeup_prepared) 2529 return 0; 2530 2531 /* 2532 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don 2533 * Anderson we should be doing PME# wake enable followed by ACPI wake 2534 * enable. To disable wake-up we call the platform first, for symmetry. 2535 */ 2536 2537 if (enable) { 2538 int error; 2539 2540 /* 2541 * Enable PME signaling if the device can signal PME from 2542 * D3cold regardless of whether or not it can signal PME from 2543 * the current target state, because that will allow it to 2544 * signal PME when the hierarchy above it goes into D3cold and 2545 * the device itself ends up in D3cold as a result of that. 2546 */ 2547 if (pci_pme_capable(dev, state) || pci_pme_capable(dev, PCI_D3cold)) 2548 pci_pme_active(dev, true); 2549 else 2550 ret = 1; 2551 error = platform_pci_set_wakeup(dev, true); 2552 if (ret) 2553 ret = error; 2554 if (!ret) 2555 dev->wakeup_prepared = true; 2556 } else { 2557 platform_pci_set_wakeup(dev, false); 2558 pci_pme_active(dev, false); 2559 dev->wakeup_prepared = false; 2560 } 2561 2562 return ret; 2563 } 2564 2565 /** 2566 * pci_enable_wake - change wakeup settings for a PCI device 2567 * @pci_dev: Target device 2568 * @state: PCI state from which device will issue wakeup events 2569 * @enable: Whether or not to enable event generation 2570 * 2571 * If @enable is set, check device_may_wakeup() for the device before calling 2572 * __pci_enable_wake() for it. 2573 */ 2574 int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable) 2575 { 2576 if (enable && !device_may_wakeup(&pci_dev->dev)) 2577 return -EINVAL; 2578 2579 return __pci_enable_wake(pci_dev, state, enable); 2580 } 2581 EXPORT_SYMBOL(pci_enable_wake); 2582 2583 /** 2584 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold 2585 * @dev: PCI device to prepare 2586 * @enable: True to enable wake-up event generation; false to disable 2587 * 2588 * Many drivers want the device to wake up the system from D3_hot or D3_cold 2589 * and this function allows them to set that up cleanly - pci_enable_wake() 2590 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI 2591 * ordering constraints. 2592 * 2593 * This function only returns error code if the device is not allowed to wake 2594 * up the system from sleep or it is not capable of generating PME# from both 2595 * D3_hot and D3_cold and the platform is unable to enable wake-up power for it. 2596 */ 2597 int pci_wake_from_d3(struct pci_dev *dev, bool enable) 2598 { 2599 return pci_pme_capable(dev, PCI_D3cold) ? 2600 pci_enable_wake(dev, PCI_D3cold, enable) : 2601 pci_enable_wake(dev, PCI_D3hot, enable); 2602 } 2603 EXPORT_SYMBOL(pci_wake_from_d3); 2604 2605 /** 2606 * pci_target_state - find an appropriate low power state for a given PCI dev 2607 * @dev: PCI device 2608 * @wakeup: Whether or not wakeup functionality will be enabled for the device. 2609 * 2610 * Use underlying platform code to find a supported low power state for @dev. 2611 * If the platform can't manage @dev, return the deepest state from which it 2612 * can generate wake events, based on any available PME info. 2613 */ 2614 static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup) 2615 { 2616 if (platform_pci_power_manageable(dev)) { 2617 /* 2618 * Call the platform to find the target state for the device. 2619 */ 2620 pci_power_t state = platform_pci_choose_state(dev); 2621 2622 switch (state) { 2623 case PCI_POWER_ERROR: 2624 case PCI_UNKNOWN: 2625 return PCI_D3hot; 2626 2627 case PCI_D1: 2628 case PCI_D2: 2629 if (pci_no_d1d2(dev)) 2630 return PCI_D3hot; 2631 } 2632 2633 return state; 2634 } 2635 2636 /* 2637 * If the device is in D3cold even though it's not power-manageable by 2638 * the platform, it may have been powered down by non-standard means. 2639 * Best to let it slumber. 2640 */ 2641 if (dev->current_state == PCI_D3cold) 2642 return PCI_D3cold; 2643 else if (!dev->pm_cap) 2644 return PCI_D0; 2645 2646 if (wakeup && dev->pme_support) { 2647 pci_power_t state = PCI_D3hot; 2648 2649 /* 2650 * Find the deepest state from which the device can generate 2651 * PME#. 2652 */ 2653 while (state && !(dev->pme_support & (1 << state))) 2654 state--; 2655 2656 if (state) 2657 return state; 2658 else if (dev->pme_support & 1) 2659 return PCI_D0; 2660 } 2661 2662 return PCI_D3hot; 2663 } 2664 2665 /** 2666 * pci_prepare_to_sleep - prepare PCI device for system-wide transition 2667 * into a sleep state 2668 * @dev: Device to handle. 2669 * 2670 * Choose the power state appropriate for the device depending on whether 2671 * it can wake up the system and/or is power manageable by the platform 2672 * (PCI_D3hot is the default) and put the device into that state. 2673 */ 2674 int pci_prepare_to_sleep(struct pci_dev *dev) 2675 { 2676 bool wakeup = device_may_wakeup(&dev->dev); 2677 pci_power_t target_state = pci_target_state(dev, wakeup); 2678 int error; 2679 2680 if (target_state == PCI_POWER_ERROR) 2681 return -EIO; 2682 2683 pci_enable_wake(dev, target_state, wakeup); 2684 2685 error = pci_set_power_state(dev, target_state); 2686 2687 if (error) 2688 pci_enable_wake(dev, target_state, false); 2689 2690 return error; 2691 } 2692 EXPORT_SYMBOL(pci_prepare_to_sleep); 2693 2694 /** 2695 * pci_back_from_sleep - turn PCI device on during system-wide transition 2696 * into working state 2697 * @dev: Device to handle. 2698 * 2699 * Disable device's system wake-up capability and put it into D0. 2700 */ 2701 int pci_back_from_sleep(struct pci_dev *dev) 2702 { 2703 int ret = pci_set_power_state(dev, PCI_D0); 2704 2705 if (ret) 2706 return ret; 2707 2708 pci_enable_wake(dev, PCI_D0, false); 2709 return 0; 2710 } 2711 EXPORT_SYMBOL(pci_back_from_sleep); 2712 2713 /** 2714 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend. 2715 * @dev: PCI device being suspended. 2716 * 2717 * Prepare @dev to generate wake-up events at run time and put it into a low 2718 * power state. 2719 */ 2720 int pci_finish_runtime_suspend(struct pci_dev *dev) 2721 { 2722 pci_power_t target_state; 2723 int error; 2724 2725 target_state = pci_target_state(dev, device_can_wakeup(&dev->dev)); 2726 if (target_state == PCI_POWER_ERROR) 2727 return -EIO; 2728 2729 __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev)); 2730 2731 error = pci_set_power_state(dev, target_state); 2732 2733 if (error) 2734 pci_enable_wake(dev, target_state, false); 2735 2736 return error; 2737 } 2738 2739 /** 2740 * pci_dev_run_wake - Check if device can generate run-time wake-up events. 2741 * @dev: Device to check. 2742 * 2743 * Return true if the device itself is capable of generating wake-up events 2744 * (through the platform or using the native PCIe PME) or if the device supports 2745 * PME and one of its upstream bridges can generate wake-up events. 2746 */ 2747 bool pci_dev_run_wake(struct pci_dev *dev) 2748 { 2749 struct pci_bus *bus = dev->bus; 2750 2751 if (!dev->pme_support) 2752 return false; 2753 2754 /* PME-capable in principle, but not from the target power state */ 2755 if (!pci_pme_capable(dev, pci_target_state(dev, true))) 2756 return false; 2757 2758 if (device_can_wakeup(&dev->dev)) 2759 return true; 2760 2761 while (bus->parent) { 2762 struct pci_dev *bridge = bus->self; 2763 2764 if (device_can_wakeup(&bridge->dev)) 2765 return true; 2766 2767 bus = bus->parent; 2768 } 2769 2770 /* We have reached the root bus. */ 2771 if (bus->bridge) 2772 return device_can_wakeup(bus->bridge); 2773 2774 return false; 2775 } 2776 EXPORT_SYMBOL_GPL(pci_dev_run_wake); 2777 2778 /** 2779 * pci_dev_need_resume - Check if it is necessary to resume the device. 2780 * @pci_dev: Device to check. 2781 * 2782 * Return 'true' if the device is not runtime-suspended or it has to be 2783 * reconfigured due to wakeup settings difference between system and runtime 2784 * suspend, or the current power state of it is not suitable for the upcoming 2785 * (system-wide) transition. 2786 */ 2787 bool pci_dev_need_resume(struct pci_dev *pci_dev) 2788 { 2789 struct device *dev = &pci_dev->dev; 2790 pci_power_t target_state; 2791 2792 if (!pm_runtime_suspended(dev) || platform_pci_need_resume(pci_dev)) 2793 return true; 2794 2795 target_state = pci_target_state(pci_dev, device_may_wakeup(dev)); 2796 2797 /* 2798 * If the earlier platform check has not triggered, D3cold is just power 2799 * removal on top of D3hot, so no need to resume the device in that 2800 * case. 2801 */ 2802 return target_state != pci_dev->current_state && 2803 target_state != PCI_D3cold && 2804 pci_dev->current_state != PCI_D3hot; 2805 } 2806 2807 /** 2808 * pci_dev_adjust_pme - Adjust PME setting for a suspended device. 2809 * @pci_dev: Device to check. 2810 * 2811 * If the device is suspended and it is not configured for system wakeup, 2812 * disable PME for it to prevent it from waking up the system unnecessarily. 2813 * 2814 * Note that if the device's power state is D3cold and the platform check in 2815 * pci_dev_need_resume() has not triggered, the device's configuration need not 2816 * be changed. 2817 */ 2818 void pci_dev_adjust_pme(struct pci_dev *pci_dev) 2819 { 2820 struct device *dev = &pci_dev->dev; 2821 2822 spin_lock_irq(&dev->power.lock); 2823 2824 if (pm_runtime_suspended(dev) && !device_may_wakeup(dev) && 2825 pci_dev->current_state < PCI_D3cold) 2826 __pci_pme_active(pci_dev, false); 2827 2828 spin_unlock_irq(&dev->power.lock); 2829 } 2830 2831 /** 2832 * pci_dev_complete_resume - Finalize resume from system sleep for a device. 2833 * @pci_dev: Device to handle. 2834 * 2835 * If the device is runtime suspended and wakeup-capable, enable PME for it as 2836 * it might have been disabled during the prepare phase of system suspend if 2837 * the device was not configured for system wakeup. 2838 */ 2839 void pci_dev_complete_resume(struct pci_dev *pci_dev) 2840 { 2841 struct device *dev = &pci_dev->dev; 2842 2843 if (!pci_dev_run_wake(pci_dev)) 2844 return; 2845 2846 spin_lock_irq(&dev->power.lock); 2847 2848 if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold) 2849 __pci_pme_active(pci_dev, true); 2850 2851 spin_unlock_irq(&dev->power.lock); 2852 } 2853 2854 /** 2855 * pci_choose_state - Choose the power state of a PCI device. 2856 * @dev: Target PCI device. 2857 * @state: Target state for the whole system. 2858 * 2859 * Returns PCI power state suitable for @dev and @state. 2860 */ 2861 pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state) 2862 { 2863 if (state.event == PM_EVENT_ON) 2864 return PCI_D0; 2865 2866 return pci_target_state(dev, false); 2867 } 2868 EXPORT_SYMBOL(pci_choose_state); 2869 2870 void pci_config_pm_runtime_get(struct pci_dev *pdev) 2871 { 2872 struct device *dev = &pdev->dev; 2873 struct device *parent = dev->parent; 2874 2875 if (parent) 2876 pm_runtime_get_sync(parent); 2877 pm_runtime_get_noresume(dev); 2878 /* 2879 * pdev->current_state is set to PCI_D3cold during suspending, 2880 * so wait until suspending completes 2881 */ 2882 pm_runtime_barrier(dev); 2883 /* 2884 * Only need to resume devices in D3cold, because config 2885 * registers are still accessible for devices suspended but 2886 * not in D3cold. 2887 */ 2888 if (pdev->current_state == PCI_D3cold) 2889 pm_runtime_resume(dev); 2890 } 2891 2892 void pci_config_pm_runtime_put(struct pci_dev *pdev) 2893 { 2894 struct device *dev = &pdev->dev; 2895 struct device *parent = dev->parent; 2896 2897 pm_runtime_put(dev); 2898 if (parent) 2899 pm_runtime_put_sync(parent); 2900 } 2901 2902 static const struct dmi_system_id bridge_d3_blacklist[] = { 2903 #ifdef CONFIG_X86 2904 { 2905 /* 2906 * Gigabyte X299 root port is not marked as hotplug capable 2907 * which allows Linux to power manage it. However, this 2908 * confuses the BIOS SMI handler so don't power manage root 2909 * ports on that system. 2910 */ 2911 .ident = "X299 DESIGNARE EX-CF", 2912 .matches = { 2913 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."), 2914 DMI_MATCH(DMI_BOARD_NAME, "X299 DESIGNARE EX-CF"), 2915 }, 2916 }, 2917 { 2918 /* 2919 * Downstream device is not accessible after putting a root port 2920 * into D3cold and back into D0 on Elo Continental Z2 board 2921 */ 2922 .ident = "Elo Continental Z2", 2923 .matches = { 2924 DMI_MATCH(DMI_BOARD_VENDOR, "Elo Touch Solutions"), 2925 DMI_MATCH(DMI_BOARD_NAME, "Geminilake"), 2926 DMI_MATCH(DMI_BOARD_VERSION, "Continental Z2"), 2927 }, 2928 }, 2929 { 2930 /* 2931 * Changing power state of root port dGPU is connected fails 2932 * https://gitlab.freedesktop.org/drm/amd/-/issues/3229 2933 */ 2934 .ident = "Hewlett-Packard HP Pavilion 17 Notebook PC/1972", 2935 .matches = { 2936 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"), 2937 DMI_MATCH(DMI_BOARD_NAME, "1972"), 2938 DMI_MATCH(DMI_BOARD_VERSION, "95.33"), 2939 }, 2940 }, 2941 #endif 2942 { } 2943 }; 2944 2945 /** 2946 * pci_bridge_d3_possible - Is it possible to put the bridge into D3 2947 * @bridge: Bridge to check 2948 * 2949 * Currently we only allow D3 for some PCIe ports and for Thunderbolt. 2950 * 2951 * Return: Whether it is possible to move the bridge to D3. 2952 * 2953 * The return value is guaranteed to be constant across the entire lifetime 2954 * of the bridge, including its hot-removal. 2955 */ 2956 bool pci_bridge_d3_possible(struct pci_dev *bridge) 2957 { 2958 if (!pci_is_pcie(bridge)) 2959 return false; 2960 2961 switch (pci_pcie_type(bridge)) { 2962 case PCI_EXP_TYPE_ROOT_PORT: 2963 case PCI_EXP_TYPE_UPSTREAM: 2964 case PCI_EXP_TYPE_DOWNSTREAM: 2965 if (pci_bridge_d3_disable) 2966 return false; 2967 2968 /* 2969 * Hotplug ports handled by platform firmware may not be put 2970 * into D3 by the OS, e.g. ACPI slots ... 2971 */ 2972 if (bridge->is_hotplug_bridge && !bridge->is_pciehp) 2973 return false; 2974 2975 /* ... or PCIe hotplug ports not handled natively by the OS. */ 2976 if (bridge->is_pciehp && !pciehp_is_native(bridge)) 2977 return false; 2978 2979 if (pci_bridge_d3_force) 2980 return true; 2981 2982 /* Even the oldest 2010 Thunderbolt controller supports D3. */ 2983 if (bridge->is_thunderbolt) 2984 return true; 2985 2986 /* Platform might know better if the bridge supports D3 */ 2987 if (platform_pci_bridge_d3(bridge)) 2988 return true; 2989 2990 /* 2991 * Hotplug ports handled natively by the OS were not validated 2992 * by vendors for runtime D3 at least until 2018 because there 2993 * was no OS support. 2994 */ 2995 if (bridge->is_pciehp) 2996 return false; 2997 2998 if (dmi_check_system(bridge_d3_blacklist)) 2999 return false; 3000 3001 /* 3002 * Out of caution, we only allow PCIe ports from 2015 or newer 3003 * into D3 on x86. 3004 */ 3005 if (!IS_ENABLED(CONFIG_X86) || dmi_get_bios_year() >= 2015) 3006 return true; 3007 break; 3008 } 3009 3010 return false; 3011 } 3012 3013 static int pci_dev_check_d3cold(struct pci_dev *dev, void *data) 3014 { 3015 bool *d3cold_ok = data; 3016 3017 if (/* The device needs to be allowed to go D3cold ... */ 3018 dev->no_d3cold || !dev->d3cold_allowed || 3019 3020 /* ... and if it is wakeup capable to do so from D3cold. */ 3021 (device_may_wakeup(&dev->dev) && 3022 !pci_pme_capable(dev, PCI_D3cold)) || 3023 3024 /* If it is a bridge it must be allowed to go to D3. */ 3025 !pci_power_manageable(dev)) 3026 3027 *d3cold_ok = false; 3028 3029 return !*d3cold_ok; 3030 } 3031 3032 /* 3033 * pci_bridge_d3_update - Update bridge D3 capabilities 3034 * @dev: PCI device which is changed 3035 * 3036 * Update upstream bridge PM capabilities accordingly depending on if the 3037 * device PM configuration was changed or the device is being removed. The 3038 * change is also propagated upstream. 3039 */ 3040 void pci_bridge_d3_update(struct pci_dev *dev) 3041 { 3042 bool remove = !device_is_registered(&dev->dev); 3043 struct pci_dev *bridge; 3044 bool d3cold_ok = true; 3045 3046 bridge = pci_upstream_bridge(dev); 3047 if (!bridge || !pci_bridge_d3_possible(bridge)) 3048 return; 3049 3050 /* 3051 * If D3 is currently allowed for the bridge, removing one of its 3052 * children won't change that. 3053 */ 3054 if (remove && bridge->bridge_d3) 3055 return; 3056 3057 /* 3058 * If D3 is currently allowed for the bridge and a child is added or 3059 * changed, disallowance of D3 can only be caused by that child, so 3060 * we only need to check that single device, not any of its siblings. 3061 * 3062 * If D3 is currently not allowed for the bridge, checking the device 3063 * first may allow us to skip checking its siblings. 3064 */ 3065 if (!remove) 3066 pci_dev_check_d3cold(dev, &d3cold_ok); 3067 3068 /* 3069 * If D3 is currently not allowed for the bridge, this may be caused 3070 * either by the device being changed/removed or any of its siblings, 3071 * so we need to go through all children to find out if one of them 3072 * continues to block D3. 3073 */ 3074 if (d3cold_ok && !bridge->bridge_d3) 3075 pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold, 3076 &d3cold_ok); 3077 3078 if (bridge->bridge_d3 != d3cold_ok) { 3079 bridge->bridge_d3 = d3cold_ok; 3080 /* Propagate change to upstream bridges */ 3081 pci_bridge_d3_update(bridge); 3082 } 3083 } 3084 3085 /** 3086 * pci_d3cold_enable - Enable D3cold for device 3087 * @dev: PCI device to handle 3088 * 3089 * This function can be used in drivers to enable D3cold from the device 3090 * they handle. It also updates upstream PCI bridge PM capabilities 3091 * accordingly. 3092 */ 3093 void pci_d3cold_enable(struct pci_dev *dev) 3094 { 3095 if (dev->no_d3cold) { 3096 dev->no_d3cold = false; 3097 pci_bridge_d3_update(dev); 3098 } 3099 } 3100 EXPORT_SYMBOL_GPL(pci_d3cold_enable); 3101 3102 /** 3103 * pci_d3cold_disable - Disable D3cold for device 3104 * @dev: PCI device to handle 3105 * 3106 * This function can be used in drivers to disable D3cold from the device 3107 * they handle. It also updates upstream PCI bridge PM capabilities 3108 * accordingly. 3109 */ 3110 void pci_d3cold_disable(struct pci_dev *dev) 3111 { 3112 if (!dev->no_d3cold) { 3113 dev->no_d3cold = true; 3114 pci_bridge_d3_update(dev); 3115 } 3116 } 3117 EXPORT_SYMBOL_GPL(pci_d3cold_disable); 3118 3119 void pci_pm_power_up_and_verify_state(struct pci_dev *pci_dev) 3120 { 3121 pci_power_up(pci_dev); 3122 pci_update_current_state(pci_dev, PCI_D0); 3123 } 3124 3125 /** 3126 * pci_pm_init - Initialize PM functions of given PCI device 3127 * @dev: PCI device to handle. 3128 */ 3129 void pci_pm_init(struct pci_dev *dev) 3130 { 3131 int pm; 3132 u16 pmc; 3133 3134 device_enable_async_suspend(&dev->dev); 3135 dev->wakeup_prepared = false; 3136 3137 dev->pm_cap = 0; 3138 dev->pme_support = 0; 3139 3140 /* find PCI PM capability in list */ 3141 pm = pci_find_capability(dev, PCI_CAP_ID_PM); 3142 if (!pm) 3143 goto poweron; 3144 /* Check device's ability to generate PME# */ 3145 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc); 3146 3147 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) { 3148 pci_err(dev, "unsupported PM cap regs version (%u)\n", 3149 pmc & PCI_PM_CAP_VER_MASK); 3150 goto poweron; 3151 } 3152 3153 dev->pm_cap = pm; 3154 dev->d3hot_delay = PCI_PM_D3HOT_WAIT; 3155 dev->d3cold_delay = PCI_PM_D3COLD_WAIT; 3156 dev->bridge_d3 = pci_bridge_d3_possible(dev); 3157 dev->d3cold_allowed = true; 3158 3159 dev->d1_support = false; 3160 dev->d2_support = false; 3161 if (!pci_no_d1d2(dev)) { 3162 if (pmc & PCI_PM_CAP_D1) 3163 dev->d1_support = true; 3164 if (pmc & PCI_PM_CAP_D2) 3165 dev->d2_support = true; 3166 3167 if (dev->d1_support || dev->d2_support) 3168 pci_info(dev, "supports%s%s\n", 3169 dev->d1_support ? " D1" : "", 3170 dev->d2_support ? " D2" : ""); 3171 } 3172 3173 pmc &= PCI_PM_CAP_PME_MASK; 3174 if (pmc) { 3175 pci_info(dev, "PME# supported from%s%s%s%s%s\n", 3176 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "", 3177 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "", 3178 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "", 3179 (pmc & PCI_PM_CAP_PME_D3hot) ? " D3hot" : "", 3180 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : ""); 3181 dev->pme_support = FIELD_GET(PCI_PM_CAP_PME_MASK, pmc); 3182 dev->pme_poll = true; 3183 /* 3184 * Make device's PM flags reflect the wake-up capability, but 3185 * let the user space enable it to wake up the system as needed. 3186 */ 3187 device_set_wakeup_capable(&dev->dev, true); 3188 /* Disable the PME# generation functionality */ 3189 pci_pme_active(dev, false); 3190 } 3191 3192 poweron: 3193 pci_pm_power_up_and_verify_state(dev); 3194 pm_runtime_forbid(&dev->dev); 3195 3196 /* 3197 * Runtime PM will be enabled for the device when it has been fully 3198 * configured, but since its parent and suppliers may suspend in 3199 * the meantime, prevent them from doing so by changing the 3200 * device's runtime PM status to "active". 3201 */ 3202 pm_runtime_set_active(&dev->dev); 3203 } 3204 3205 static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop) 3206 { 3207 unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI; 3208 3209 switch (prop) { 3210 case PCI_EA_P_MEM: 3211 case PCI_EA_P_VF_MEM: 3212 flags |= IORESOURCE_MEM; 3213 break; 3214 case PCI_EA_P_MEM_PREFETCH: 3215 case PCI_EA_P_VF_MEM_PREFETCH: 3216 flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 3217 break; 3218 case PCI_EA_P_IO: 3219 flags |= IORESOURCE_IO; 3220 break; 3221 default: 3222 return 0; 3223 } 3224 3225 return flags; 3226 } 3227 3228 static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei, 3229 u8 prop) 3230 { 3231 if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO) 3232 return &dev->resource[bei]; 3233 #ifdef CONFIG_PCI_IOV 3234 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 && 3235 (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH)) 3236 return &dev->resource[PCI_IOV_RESOURCES + 3237 bei - PCI_EA_BEI_VF_BAR0]; 3238 #endif 3239 else if (bei == PCI_EA_BEI_ROM) 3240 return &dev->resource[PCI_ROM_RESOURCE]; 3241 else 3242 return NULL; 3243 } 3244 3245 /* Read an Enhanced Allocation (EA) entry */ 3246 static int pci_ea_read(struct pci_dev *dev, int offset) 3247 { 3248 struct resource *res; 3249 const char *res_name; 3250 int ent_size, ent_offset = offset; 3251 resource_size_t start, end; 3252 unsigned long flags; 3253 u32 dw0, bei, base, max_offset; 3254 u8 prop; 3255 bool support_64 = (sizeof(resource_size_t) >= 8); 3256 3257 pci_read_config_dword(dev, ent_offset, &dw0); 3258 ent_offset += 4; 3259 3260 /* Entry size field indicates DWORDs after 1st */ 3261 ent_size = (FIELD_GET(PCI_EA_ES, dw0) + 1) << 2; 3262 3263 if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */ 3264 goto out; 3265 3266 bei = FIELD_GET(PCI_EA_BEI, dw0); 3267 prop = FIELD_GET(PCI_EA_PP, dw0); 3268 3269 /* 3270 * If the Property is in the reserved range, try the Secondary 3271 * Property instead. 3272 */ 3273 if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED) 3274 prop = FIELD_GET(PCI_EA_SP, dw0); 3275 if (prop > PCI_EA_P_BRIDGE_IO) 3276 goto out; 3277 3278 res = pci_ea_get_resource(dev, bei, prop); 3279 res_name = pci_resource_name(dev, bei); 3280 if (!res) { 3281 pci_err(dev, "Unsupported EA entry BEI: %u\n", bei); 3282 goto out; 3283 } 3284 3285 flags = pci_ea_flags(dev, prop); 3286 if (!flags) { 3287 pci_err(dev, "Unsupported EA properties: %#x\n", prop); 3288 goto out; 3289 } 3290 3291 /* Read Base */ 3292 pci_read_config_dword(dev, ent_offset, &base); 3293 start = (base & PCI_EA_FIELD_MASK); 3294 ent_offset += 4; 3295 3296 /* Read MaxOffset */ 3297 pci_read_config_dword(dev, ent_offset, &max_offset); 3298 ent_offset += 4; 3299 3300 /* Read Base MSBs (if 64-bit entry) */ 3301 if (base & PCI_EA_IS_64) { 3302 u32 base_upper; 3303 3304 pci_read_config_dword(dev, ent_offset, &base_upper); 3305 ent_offset += 4; 3306 3307 flags |= IORESOURCE_MEM_64; 3308 3309 /* entry starts above 32-bit boundary, can't use */ 3310 if (!support_64 && base_upper) 3311 goto out; 3312 3313 if (support_64) 3314 start |= ((u64)base_upper << 32); 3315 } 3316 3317 end = start + (max_offset | 0x03); 3318 3319 /* Read MaxOffset MSBs (if 64-bit entry) */ 3320 if (max_offset & PCI_EA_IS_64) { 3321 u32 max_offset_upper; 3322 3323 pci_read_config_dword(dev, ent_offset, &max_offset_upper); 3324 ent_offset += 4; 3325 3326 flags |= IORESOURCE_MEM_64; 3327 3328 /* entry too big, can't use */ 3329 if (!support_64 && max_offset_upper) 3330 goto out; 3331 3332 if (support_64) 3333 end += ((u64)max_offset_upper << 32); 3334 } 3335 3336 if (end < start) { 3337 pci_err(dev, "EA Entry crosses address boundary\n"); 3338 goto out; 3339 } 3340 3341 if (ent_size != ent_offset - offset) { 3342 pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n", 3343 ent_size, ent_offset - offset); 3344 goto out; 3345 } 3346 3347 res->name = pci_name(dev); 3348 res->start = start; 3349 res->end = end; 3350 res->flags = flags; 3351 3352 if (bei <= PCI_EA_BEI_BAR5) 3353 pci_info(dev, "%s %pR: from Enhanced Allocation, properties %#02x\n", 3354 res_name, res, prop); 3355 else if (bei == PCI_EA_BEI_ROM) 3356 pci_info(dev, "%s %pR: from Enhanced Allocation, properties %#02x\n", 3357 res_name, res, prop); 3358 else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5) 3359 pci_info(dev, "%s %pR: from Enhanced Allocation, properties %#02x\n", 3360 res_name, res, prop); 3361 else 3362 pci_info(dev, "BEI %d %pR: from Enhanced Allocation, properties %#02x\n", 3363 bei, res, prop); 3364 3365 out: 3366 return offset + ent_size; 3367 } 3368 3369 /* Enhanced Allocation Initialization */ 3370 void pci_ea_init(struct pci_dev *dev) 3371 { 3372 int ea; 3373 u8 num_ent; 3374 int offset; 3375 int i; 3376 3377 /* find PCI EA capability in list */ 3378 ea = pci_find_capability(dev, PCI_CAP_ID_EA); 3379 if (!ea) 3380 return; 3381 3382 /* determine the number of entries */ 3383 pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT, 3384 &num_ent); 3385 num_ent &= PCI_EA_NUM_ENT_MASK; 3386 3387 offset = ea + PCI_EA_FIRST_ENT; 3388 3389 /* Skip DWORD 2 for type 1 functions */ 3390 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) 3391 offset += 4; 3392 3393 /* parse each EA entry */ 3394 for (i = 0; i < num_ent; ++i) 3395 offset = pci_ea_read(dev, offset); 3396 } 3397 3398 static void pci_add_saved_cap(struct pci_dev *pci_dev, 3399 struct pci_cap_saved_state *new_cap) 3400 { 3401 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space); 3402 } 3403 3404 /** 3405 * _pci_add_cap_save_buffer - allocate buffer for saving given 3406 * capability registers 3407 * @dev: the PCI device 3408 * @cap: the capability to allocate the buffer for 3409 * @extended: Standard or Extended capability ID 3410 * @size: requested size of the buffer 3411 */ 3412 static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap, 3413 bool extended, unsigned int size) 3414 { 3415 int pos; 3416 struct pci_cap_saved_state *save_state; 3417 3418 if (extended) 3419 pos = pci_find_ext_capability(dev, cap); 3420 else 3421 pos = pci_find_capability(dev, cap); 3422 3423 if (!pos) 3424 return 0; 3425 3426 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL); 3427 if (!save_state) 3428 return -ENOMEM; 3429 3430 save_state->cap.cap_nr = cap; 3431 save_state->cap.cap_extended = extended; 3432 save_state->cap.size = size; 3433 pci_add_saved_cap(dev, save_state); 3434 3435 return 0; 3436 } 3437 3438 int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size) 3439 { 3440 return _pci_add_cap_save_buffer(dev, cap, false, size); 3441 } 3442 3443 int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size) 3444 { 3445 return _pci_add_cap_save_buffer(dev, cap, true, size); 3446 } 3447 3448 /** 3449 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities 3450 * @dev: the PCI device 3451 */ 3452 void pci_allocate_cap_save_buffers(struct pci_dev *dev) 3453 { 3454 int error; 3455 3456 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 3457 PCI_EXP_SAVE_REGS * sizeof(u16)); 3458 if (error) 3459 pci_err(dev, "unable to preallocate PCI Express save buffer\n"); 3460 3461 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16)); 3462 if (error) 3463 pci_err(dev, "unable to preallocate PCI-X save buffer\n"); 3464 3465 error = pci_add_ext_cap_save_buffer(dev, PCI_EXT_CAP_ID_LTR, 3466 2 * sizeof(u16)); 3467 if (error) 3468 pci_err(dev, "unable to allocate suspend buffer for LTR\n"); 3469 3470 pci_allocate_vc_save_buffers(dev); 3471 } 3472 3473 void pci_free_cap_save_buffers(struct pci_dev *dev) 3474 { 3475 struct pci_cap_saved_state *tmp; 3476 struct hlist_node *n; 3477 3478 hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next) 3479 kfree(tmp); 3480 } 3481 3482 /** 3483 * pci_configure_ari - enable or disable ARI forwarding 3484 * @dev: the PCI device 3485 * 3486 * If @dev and its upstream bridge both support ARI, enable ARI in the 3487 * bridge. Otherwise, disable ARI in the bridge. 3488 */ 3489 void pci_configure_ari(struct pci_dev *dev) 3490 { 3491 u32 cap; 3492 struct pci_dev *bridge; 3493 3494 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn) 3495 return; 3496 3497 bridge = dev->bus->self; 3498 if (!bridge) 3499 return; 3500 3501 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap); 3502 if (!(cap & PCI_EXP_DEVCAP2_ARI)) 3503 return; 3504 3505 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) { 3506 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2, 3507 PCI_EXP_DEVCTL2_ARI); 3508 bridge->ari_enabled = 1; 3509 } else { 3510 pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2, 3511 PCI_EXP_DEVCTL2_ARI); 3512 bridge->ari_enabled = 0; 3513 } 3514 } 3515 3516 static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags) 3517 { 3518 int pos; 3519 u16 ctrl; 3520 3521 pos = pdev->acs_cap; 3522 if (!pos) 3523 return false; 3524 3525 /* 3526 * Except for egress control, capabilities are either required 3527 * or only required if controllable. Features missing from the 3528 * capability field can therefore be assumed as hard-wired enabled. 3529 */ 3530 acs_flags &= (pdev->acs_capabilities | PCI_ACS_EC); 3531 3532 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl); 3533 return (ctrl & acs_flags) == acs_flags; 3534 } 3535 3536 /** 3537 * pci_acs_enabled - test ACS against required flags for a given device 3538 * @pdev: device to test 3539 * @acs_flags: required PCI ACS flags 3540 * 3541 * Return true if the device supports the provided flags. Automatically 3542 * filters out flags that are not implemented on multifunction devices. 3543 * 3544 * Note that this interface checks the effective ACS capabilities of the 3545 * device rather than the actual capabilities. For instance, most single 3546 * function endpoints are not required to support ACS because they have no 3547 * opportunity for peer-to-peer access. We therefore return 'true' 3548 * regardless of whether the device exposes an ACS capability. This makes 3549 * it much easier for callers of this function to ignore the actual type 3550 * or topology of the device when testing ACS support. 3551 */ 3552 bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags) 3553 { 3554 int ret; 3555 3556 ret = pci_dev_specific_acs_enabled(pdev, acs_flags); 3557 if (ret >= 0) 3558 return ret > 0; 3559 3560 /* 3561 * Conventional PCI and PCI-X devices never support ACS, either 3562 * effectively or actually. The shared bus topology implies that 3563 * any device on the bus can receive or snoop DMA. 3564 */ 3565 if (!pci_is_pcie(pdev)) 3566 return false; 3567 3568 switch (pci_pcie_type(pdev)) { 3569 /* 3570 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec, 3571 * but since their primary interface is PCI/X, we conservatively 3572 * handle them as we would a non-PCIe device. 3573 */ 3574 case PCI_EXP_TYPE_PCIE_BRIDGE: 3575 /* 3576 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never 3577 * applicable... must never implement an ACS Extended Capability...". 3578 * This seems arbitrary, but we take a conservative interpretation 3579 * of this statement. 3580 */ 3581 case PCI_EXP_TYPE_PCI_BRIDGE: 3582 case PCI_EXP_TYPE_RC_EC: 3583 return false; 3584 /* 3585 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should 3586 * implement ACS in order to indicate their peer-to-peer capabilities, 3587 * regardless of whether they are single- or multi-function devices. 3588 */ 3589 case PCI_EXP_TYPE_DOWNSTREAM: 3590 case PCI_EXP_TYPE_ROOT_PORT: 3591 return pci_acs_flags_enabled(pdev, acs_flags); 3592 /* 3593 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be 3594 * implemented by the remaining PCIe types to indicate peer-to-peer 3595 * capabilities, but only when they are part of a multifunction 3596 * device. The footnote for section 6.12 indicates the specific 3597 * PCIe types included here. 3598 */ 3599 case PCI_EXP_TYPE_ENDPOINT: 3600 case PCI_EXP_TYPE_UPSTREAM: 3601 case PCI_EXP_TYPE_LEG_END: 3602 case PCI_EXP_TYPE_RC_END: 3603 if (!pdev->multifunction) 3604 break; 3605 3606 return pci_acs_flags_enabled(pdev, acs_flags); 3607 } 3608 3609 /* 3610 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable 3611 * to single function devices with the exception of downstream ports. 3612 */ 3613 return true; 3614 } 3615 3616 /** 3617 * pci_acs_path_enabled - test ACS flags from start to end in a hierarchy 3618 * @start: starting downstream device 3619 * @end: ending upstream device or NULL to search to the root bus 3620 * @acs_flags: required flags 3621 * 3622 * Walk up a device tree from start to end testing PCI ACS support. If 3623 * any step along the way does not support the required flags, return false. 3624 */ 3625 bool pci_acs_path_enabled(struct pci_dev *start, 3626 struct pci_dev *end, u16 acs_flags) 3627 { 3628 struct pci_dev *pdev, *parent = start; 3629 3630 do { 3631 pdev = parent; 3632 3633 if (!pci_acs_enabled(pdev, acs_flags)) 3634 return false; 3635 3636 if (pci_is_root_bus(pdev->bus)) 3637 return (end == NULL); 3638 3639 parent = pdev->bus->self; 3640 } while (pdev != end); 3641 3642 return true; 3643 } 3644 3645 /** 3646 * pci_acs_init - Initialize ACS if hardware supports it 3647 * @dev: the PCI device 3648 */ 3649 void pci_acs_init(struct pci_dev *dev) 3650 { 3651 int pos; 3652 3653 dev->acs_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS); 3654 pos = dev->acs_cap; 3655 if (!pos) 3656 return; 3657 3658 pci_read_config_word(dev, pos + PCI_ACS_CAP, &dev->acs_capabilities); 3659 pci_disable_broken_acs_cap(dev); 3660 } 3661 3662 /** 3663 * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port 3664 * @dev: the PCI device 3665 * @cap_mask: mask of desired AtomicOp sizes, including one or more of: 3666 * PCI_EXP_DEVCAP2_ATOMIC_COMP32 3667 * PCI_EXP_DEVCAP2_ATOMIC_COMP64 3668 * PCI_EXP_DEVCAP2_ATOMIC_COMP128 3669 * 3670 * Return 0 if all upstream bridges support AtomicOp routing, egress 3671 * blocking is disabled on all upstream ports, and the root port supports 3672 * the requested completion capabilities (32-bit, 64-bit and/or 128-bit 3673 * AtomicOp completion), or negative otherwise. 3674 */ 3675 int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask) 3676 { 3677 struct pci_dev *root, *bridge; 3678 u32 cap, ctl2; 3679 3680 /* 3681 * Per PCIe r7.0, sec 7.5.3.16, the AtomicOp Requester Enable bit 3682 * in Device Control 2 is reserved in VFs and the PF value applies 3683 * to all associated VFs. 3684 */ 3685 if (dev->is_virtfn) 3686 return -EINVAL; 3687 3688 if (!pci_is_pcie(dev)) 3689 return -EINVAL; 3690 3691 /* 3692 * Per PCIe r7.0, sec 6.15, endpoints and root ports may be 3693 * AtomicOp requesters. For now, we only support (legacy) endpoints 3694 * as requesters and root ports as completers. No endpoints as 3695 * completers, and no peer-to-peer. 3696 */ 3697 3698 switch (pci_pcie_type(dev)) { 3699 case PCI_EXP_TYPE_ENDPOINT: 3700 case PCI_EXP_TYPE_LEG_END: 3701 break; 3702 default: 3703 return -EINVAL; 3704 } 3705 3706 root = pcie_find_root_port(dev); 3707 if (!root) 3708 return -EINVAL; 3709 3710 pcie_capability_read_dword(root, PCI_EXP_DEVCAP2, &cap); 3711 if ((cap & cap_mask) != cap_mask) 3712 return -EINVAL; 3713 3714 bridge = pci_upstream_bridge(dev); 3715 while (bridge != root) { 3716 switch (pci_pcie_type(bridge)) { 3717 case PCI_EXP_TYPE_UPSTREAM: 3718 /* Upstream ports must not block AtomicOps on egress */ 3719 pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2, 3720 &ctl2); 3721 if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK) 3722 return -EINVAL; 3723 fallthrough; 3724 3725 /* All switch ports need to route AtomicOps */ 3726 case PCI_EXP_TYPE_DOWNSTREAM: 3727 pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, 3728 &cap); 3729 if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE)) 3730 return -EINVAL; 3731 break; 3732 } 3733 3734 bridge = pci_upstream_bridge(bridge); 3735 } 3736 3737 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2, 3738 PCI_EXP_DEVCTL2_ATOMIC_REQ); 3739 return 0; 3740 } 3741 EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); 3742 3743 /** 3744 * pci_release_region - Release a PCI bar 3745 * @pdev: PCI device whose resources were previously reserved by 3746 * pci_request_region() 3747 * @bar: BAR to release 3748 * 3749 * Releases the PCI I/O and memory resources previously reserved by a 3750 * successful call to pci_request_region(). Call this function only 3751 * after all use of the PCI regions has ceased. 3752 */ 3753 void pci_release_region(struct pci_dev *pdev, int bar) 3754 { 3755 if (!pci_bar_index_is_valid(bar)) 3756 return; 3757 3758 if (pci_resource_len(pdev, bar) == 0) 3759 return; 3760 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) 3761 release_region(pci_resource_start(pdev, bar), 3762 pci_resource_len(pdev, bar)); 3763 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) 3764 release_mem_region(pci_resource_start(pdev, bar), 3765 pci_resource_len(pdev, bar)); 3766 } 3767 EXPORT_SYMBOL(pci_release_region); 3768 3769 /** 3770 * __pci_request_region - Reserved PCI I/O and memory resource 3771 * @pdev: PCI device whose resources are to be reserved 3772 * @bar: BAR to be reserved 3773 * @name: name of the driver requesting the resource 3774 * @exclusive: whether the region access is exclusive or not 3775 * 3776 * Returns: 0 on success, negative error code on failure. 3777 * 3778 * Mark the PCI region associated with PCI device @pdev BAR @bar as being 3779 * reserved by owner @name. Do not access any address inside the PCI regions 3780 * unless this call returns successfully. 3781 * 3782 * If @exclusive is set, then the region is marked so that userspace 3783 * is explicitly not allowed to map the resource via /dev/mem or 3784 * sysfs MMIO access. 3785 * 3786 * Returns 0 on success, or %EBUSY on error. A warning 3787 * message is also printed on failure. 3788 */ 3789 static int __pci_request_region(struct pci_dev *pdev, int bar, 3790 const char *name, int exclusive) 3791 { 3792 if (!pci_bar_index_is_valid(bar)) 3793 return -EINVAL; 3794 3795 if (pci_resource_len(pdev, bar) == 0) 3796 return 0; 3797 3798 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) { 3799 if (!request_region(pci_resource_start(pdev, bar), 3800 pci_resource_len(pdev, bar), name)) 3801 goto err_out; 3802 } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) { 3803 if (!__request_mem_region(pci_resource_start(pdev, bar), 3804 pci_resource_len(pdev, bar), name, 3805 exclusive)) 3806 goto err_out; 3807 } 3808 3809 return 0; 3810 3811 err_out: 3812 pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar, 3813 &pdev->resource[bar]); 3814 return -EBUSY; 3815 } 3816 3817 /** 3818 * pci_request_region - Reserve PCI I/O and memory resource 3819 * @pdev: PCI device whose resources are to be reserved 3820 * @bar: BAR to be reserved 3821 * @name: name of the driver requesting the resource 3822 * 3823 * Returns: 0 on success, negative error code on failure. 3824 * 3825 * Mark the PCI region associated with PCI device @pdev BAR @bar as being 3826 * reserved by owner @name. Do not access any address inside the PCI regions 3827 * unless this call returns successfully. 3828 * 3829 * Returns 0 on success, or %EBUSY on error. A warning 3830 * message is also printed on failure. 3831 */ 3832 int pci_request_region(struct pci_dev *pdev, int bar, const char *name) 3833 { 3834 return __pci_request_region(pdev, bar, name, 0); 3835 } 3836 EXPORT_SYMBOL(pci_request_region); 3837 3838 /** 3839 * pci_release_selected_regions - Release selected PCI I/O and memory resources 3840 * @pdev: PCI device whose resources were previously reserved 3841 * @bars: Bitmask of BARs to be released 3842 * 3843 * Release selected PCI I/O and memory resources previously reserved. 3844 * Call this function only after all use of the PCI regions has ceased. 3845 */ 3846 void pci_release_selected_regions(struct pci_dev *pdev, int bars) 3847 { 3848 int i; 3849 3850 for (i = 0; i < PCI_STD_NUM_BARS; i++) 3851 if (bars & (1 << i)) 3852 pci_release_region(pdev, i); 3853 } 3854 EXPORT_SYMBOL(pci_release_selected_regions); 3855 3856 static int __pci_request_selected_regions(struct pci_dev *pdev, int bars, 3857 const char *name, int excl) 3858 { 3859 int i; 3860 3861 for (i = 0; i < PCI_STD_NUM_BARS; i++) 3862 if (bars & (1 << i)) 3863 if (__pci_request_region(pdev, i, name, excl)) 3864 goto err_out; 3865 return 0; 3866 3867 err_out: 3868 while (--i >= 0) 3869 if (bars & (1 << i)) 3870 pci_release_region(pdev, i); 3871 3872 return -EBUSY; 3873 } 3874 3875 3876 /** 3877 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources 3878 * @pdev: PCI device whose resources are to be reserved 3879 * @bars: Bitmask of BARs to be requested 3880 * @name: Name of the driver requesting the resources 3881 * 3882 * Returns: 0 on success, negative error code on failure. 3883 */ 3884 int pci_request_selected_regions(struct pci_dev *pdev, int bars, 3885 const char *name) 3886 { 3887 return __pci_request_selected_regions(pdev, bars, name, 0); 3888 } 3889 EXPORT_SYMBOL(pci_request_selected_regions); 3890 3891 /** 3892 * pci_request_selected_regions_exclusive - Request regions exclusively 3893 * @pdev: PCI device to request regions from 3894 * @bars: bit mask of BARs to request 3895 * @name: name of the driver requesting the resources 3896 * 3897 * Returns: 0 on success, negative error code on failure. 3898 */ 3899 int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars, 3900 const char *name) 3901 { 3902 return __pci_request_selected_regions(pdev, bars, name, 3903 IORESOURCE_EXCLUSIVE); 3904 } 3905 EXPORT_SYMBOL(pci_request_selected_regions_exclusive); 3906 3907 /** 3908 * pci_release_regions - Release reserved PCI I/O and memory resources 3909 * @pdev: PCI device whose resources were previously reserved by 3910 * pci_request_regions() 3911 * 3912 * Releases all PCI I/O and memory resources previously reserved by a 3913 * successful call to pci_request_regions(). Call this function only 3914 * after all use of the PCI regions has ceased. 3915 */ 3916 void pci_release_regions(struct pci_dev *pdev) 3917 { 3918 pci_release_selected_regions(pdev, (1 << PCI_STD_NUM_BARS) - 1); 3919 } 3920 EXPORT_SYMBOL(pci_release_regions); 3921 3922 /** 3923 * pci_request_regions - Reserve PCI I/O and memory resources 3924 * @pdev: PCI device whose resources are to be reserved 3925 * @name: name of the driver requesting the resources 3926 * 3927 * Mark all PCI regions associated with PCI device @pdev as being reserved by 3928 * owner @name. Do not access any address inside the PCI regions unless this 3929 * call returns successfully. 3930 * 3931 * Returns 0 on success, or %EBUSY on error. A warning 3932 * message is also printed on failure. 3933 */ 3934 int pci_request_regions(struct pci_dev *pdev, const char *name) 3935 { 3936 return pci_request_selected_regions(pdev, 3937 ((1 << PCI_STD_NUM_BARS) - 1), name); 3938 } 3939 EXPORT_SYMBOL(pci_request_regions); 3940 3941 /** 3942 * pci_request_regions_exclusive - Reserve PCI I/O and memory resources 3943 * @pdev: PCI device whose resources are to be reserved 3944 * @name: name of the driver requesting the resources 3945 * 3946 * Returns: 0 on success, negative error code on failure. 3947 * 3948 * Mark all PCI regions associated with PCI device @pdev as being reserved 3949 * by owner @name. Do not access any address inside the PCI regions 3950 * unless this call returns successfully. 3951 * 3952 * pci_request_regions_exclusive() will mark the region so that /dev/mem 3953 * and the sysfs MMIO access will not be allowed. 3954 * 3955 * Returns 0 on success, or %EBUSY on error. A warning message is also 3956 * printed on failure. 3957 */ 3958 int pci_request_regions_exclusive(struct pci_dev *pdev, const char *name) 3959 { 3960 return pci_request_selected_regions_exclusive(pdev, 3961 ((1 << PCI_STD_NUM_BARS) - 1), name); 3962 } 3963 EXPORT_SYMBOL(pci_request_regions_exclusive); 3964 3965 /* 3966 * Record the PCI IO range (expressed as CPU physical address + size). 3967 * Return a negative value if an error has occurred, zero otherwise 3968 */ 3969 int pci_register_io_range(const struct fwnode_handle *fwnode, phys_addr_t addr, 3970 resource_size_t size) 3971 { 3972 int ret = 0; 3973 #ifdef PCI_IOBASE 3974 struct logic_pio_hwaddr *range; 3975 3976 if (!size || addr + size < addr) 3977 return -EINVAL; 3978 3979 range = kzalloc_obj(*range, GFP_ATOMIC); 3980 if (!range) 3981 return -ENOMEM; 3982 3983 range->fwnode = fwnode; 3984 range->size = size; 3985 range->hw_start = addr; 3986 range->flags = LOGIC_PIO_CPU_MMIO; 3987 3988 ret = logic_pio_register_range(range); 3989 if (ret) 3990 kfree(range); 3991 3992 /* Ignore duplicates due to deferred probing */ 3993 if (ret == -EEXIST) 3994 ret = 0; 3995 #endif 3996 3997 return ret; 3998 } 3999 4000 phys_addr_t pci_pio_to_address(unsigned long pio) 4001 { 4002 #ifdef PCI_IOBASE 4003 if (pio < MMIO_UPPER_LIMIT) 4004 return logic_pio_to_hwaddr(pio); 4005 #endif 4006 4007 return (phys_addr_t) OF_BAD_ADDR; 4008 } 4009 EXPORT_SYMBOL_GPL(pci_pio_to_address); 4010 4011 unsigned long __weak pci_address_to_pio(phys_addr_t address) 4012 { 4013 #ifdef PCI_IOBASE 4014 return logic_pio_trans_cpuaddr(address); 4015 #else 4016 if (address > IO_SPACE_LIMIT) 4017 return (unsigned long)-1; 4018 4019 return (unsigned long) address; 4020 #endif 4021 } 4022 4023 /** 4024 * pci_remap_iospace - Remap the memory mapped I/O space 4025 * @res: Resource describing the I/O space 4026 * @phys_addr: physical address of range to be mapped 4027 * 4028 * Remap the memory mapped I/O space described by the @res and the CPU 4029 * physical address @phys_addr into virtual address space. Only 4030 * architectures that have memory mapped IO functions defined (and the 4031 * PCI_IOBASE value defined) should call this function. 4032 */ 4033 #ifndef pci_remap_iospace 4034 int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr) 4035 { 4036 #if defined(PCI_IOBASE) 4037 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start; 4038 4039 if (!(res->flags & IORESOURCE_IO)) 4040 return -EINVAL; 4041 4042 if (res->end > IO_SPACE_LIMIT) 4043 return -EINVAL; 4044 4045 return vmap_page_range(vaddr, vaddr + resource_size(res), phys_addr, 4046 pgprot_device(PAGE_KERNEL)); 4047 #else 4048 /* 4049 * This architecture does not have memory mapped I/O space, 4050 * so this function should never be called 4051 */ 4052 WARN_ONCE(1, "This architecture does not support memory mapped I/O\n"); 4053 return -ENODEV; 4054 #endif 4055 } 4056 EXPORT_SYMBOL(pci_remap_iospace); 4057 #endif 4058 4059 /** 4060 * pci_unmap_iospace - Unmap the memory mapped I/O space 4061 * @res: resource to be unmapped 4062 * 4063 * Unmap the CPU virtual address @res from virtual address space. Only 4064 * architectures that have memory mapped IO functions defined (and the 4065 * PCI_IOBASE value defined) should call this function. 4066 */ 4067 void pci_unmap_iospace(struct resource *res) 4068 { 4069 #if defined(PCI_IOBASE) 4070 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start; 4071 4072 vunmap_range(vaddr, vaddr + resource_size(res)); 4073 #endif 4074 } 4075 EXPORT_SYMBOL(pci_unmap_iospace); 4076 4077 static void __pci_set_master(struct pci_dev *dev, bool enable) 4078 { 4079 u16 old_cmd, cmd; 4080 4081 pci_read_config_word(dev, PCI_COMMAND, &old_cmd); 4082 if (enable) 4083 cmd = old_cmd | PCI_COMMAND_MASTER; 4084 else 4085 cmd = old_cmd & ~PCI_COMMAND_MASTER; 4086 if (cmd != old_cmd) { 4087 pci_dbg(dev, "%s bus mastering\n", 4088 enable ? "enabling" : "disabling"); 4089 pci_write_config_word(dev, PCI_COMMAND, cmd); 4090 } 4091 dev->is_busmaster = enable; 4092 } 4093 4094 /** 4095 * pcibios_setup - process "pci=" kernel boot arguments 4096 * @str: string used to pass in "pci=" kernel boot arguments 4097 * 4098 * Process kernel boot arguments. This is the default implementation. 4099 * Architecture specific implementations can override this as necessary. 4100 */ 4101 char * __weak __init pcibios_setup(char *str) 4102 { 4103 return str; 4104 } 4105 4106 /** 4107 * pcibios_set_master - enable PCI bus-mastering for device dev 4108 * @dev: the PCI device to enable 4109 * 4110 * Enables PCI bus-mastering for the device. This is the default 4111 * implementation. Architecture specific implementations can override 4112 * this if necessary. 4113 */ 4114 void __weak pcibios_set_master(struct pci_dev *dev) 4115 { 4116 u8 lat; 4117 4118 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */ 4119 if (pci_is_pcie(dev)) 4120 return; 4121 4122 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); 4123 if (lat < 16) 4124 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; 4125 else if (lat > pcibios_max_latency) 4126 lat = pcibios_max_latency; 4127 else 4128 return; 4129 4130 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); 4131 } 4132 4133 /** 4134 * pci_set_master - enables bus-mastering for device dev 4135 * @dev: the PCI device to enable 4136 * 4137 * Enables bus-mastering on the device and calls pcibios_set_master() 4138 * to do the needed arch specific settings. 4139 */ 4140 void pci_set_master(struct pci_dev *dev) 4141 { 4142 __pci_set_master(dev, true); 4143 pcibios_set_master(dev); 4144 } 4145 EXPORT_SYMBOL(pci_set_master); 4146 4147 /** 4148 * pci_clear_master - disables bus-mastering for device dev 4149 * @dev: the PCI device to disable 4150 */ 4151 void pci_clear_master(struct pci_dev *dev) 4152 { 4153 __pci_set_master(dev, false); 4154 } 4155 EXPORT_SYMBOL(pci_clear_master); 4156 4157 /** 4158 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed 4159 * @dev: the PCI device for which MWI is to be enabled 4160 * 4161 * Helper function for pci_set_mwi. 4162 * Originally copied from drivers/net/acenic.c. 4163 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. 4164 * 4165 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 4166 */ 4167 int pci_set_cacheline_size(struct pci_dev *dev) 4168 { 4169 u8 cacheline_size; 4170 4171 if (!pci_cache_line_size) 4172 return -EINVAL; 4173 4174 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be 4175 equal to or multiple of the right value. */ 4176 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size); 4177 if (cacheline_size >= pci_cache_line_size && 4178 (cacheline_size % pci_cache_line_size) == 0) 4179 return 0; 4180 4181 /* Write the correct value. */ 4182 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size); 4183 /* Read it back. */ 4184 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size); 4185 if (cacheline_size == pci_cache_line_size) 4186 return 0; 4187 4188 pci_dbg(dev, "cache line size of %d is not supported\n", 4189 pci_cache_line_size << 2); 4190 4191 return -EINVAL; 4192 } 4193 EXPORT_SYMBOL_GPL(pci_set_cacheline_size); 4194 4195 /** 4196 * pci_set_mwi - enables memory-write-invalidate PCI transaction 4197 * @dev: the PCI device for which MWI is enabled 4198 * 4199 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND. 4200 * 4201 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 4202 */ 4203 int pci_set_mwi(struct pci_dev *dev) 4204 { 4205 #ifdef PCI_DISABLE_MWI 4206 return 0; 4207 #else 4208 int rc; 4209 u16 cmd; 4210 4211 rc = pci_set_cacheline_size(dev); 4212 if (rc) 4213 return rc; 4214 4215 pci_read_config_word(dev, PCI_COMMAND, &cmd); 4216 if (!(cmd & PCI_COMMAND_INVALIDATE)) { 4217 pci_dbg(dev, "enabling Mem-Wr-Inval\n"); 4218 cmd |= PCI_COMMAND_INVALIDATE; 4219 pci_write_config_word(dev, PCI_COMMAND, cmd); 4220 } 4221 return 0; 4222 #endif 4223 } 4224 EXPORT_SYMBOL(pci_set_mwi); 4225 4226 /** 4227 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction 4228 * @dev: the PCI device for which MWI is enabled 4229 * 4230 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND. 4231 * Callers are not required to check the return value. 4232 * 4233 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 4234 */ 4235 int pci_try_set_mwi(struct pci_dev *dev) 4236 { 4237 #ifdef PCI_DISABLE_MWI 4238 return 0; 4239 #else 4240 return pci_set_mwi(dev); 4241 #endif 4242 } 4243 EXPORT_SYMBOL(pci_try_set_mwi); 4244 4245 /** 4246 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev 4247 * @dev: the PCI device to disable 4248 * 4249 * Disables PCI Memory-Write-Invalidate transaction on the device 4250 */ 4251 void pci_clear_mwi(struct pci_dev *dev) 4252 { 4253 #ifndef PCI_DISABLE_MWI 4254 u16 cmd; 4255 4256 pci_read_config_word(dev, PCI_COMMAND, &cmd); 4257 if (cmd & PCI_COMMAND_INVALIDATE) { 4258 cmd &= ~PCI_COMMAND_INVALIDATE; 4259 pci_write_config_word(dev, PCI_COMMAND, cmd); 4260 } 4261 #endif 4262 } 4263 EXPORT_SYMBOL(pci_clear_mwi); 4264 4265 /** 4266 * pci_disable_parity - disable parity checking for device 4267 * @dev: the PCI device to operate on 4268 * 4269 * Disable parity checking for device @dev 4270 */ 4271 void pci_disable_parity(struct pci_dev *dev) 4272 { 4273 u16 cmd; 4274 4275 pci_read_config_word(dev, PCI_COMMAND, &cmd); 4276 if (cmd & PCI_COMMAND_PARITY) { 4277 cmd &= ~PCI_COMMAND_PARITY; 4278 pci_write_config_word(dev, PCI_COMMAND, cmd); 4279 } 4280 } 4281 4282 /** 4283 * pci_intx - enables/disables PCI INTx for device dev 4284 * @pdev: the PCI device to operate on 4285 * @enable: boolean: whether to enable or disable PCI INTx 4286 * 4287 * Enables/disables PCI INTx for device @pdev 4288 */ 4289 void pci_intx(struct pci_dev *pdev, int enable) 4290 { 4291 u16 pci_command, new; 4292 4293 pci_read_config_word(pdev, PCI_COMMAND, &pci_command); 4294 4295 if (enable) 4296 new = pci_command & ~PCI_COMMAND_INTX_DISABLE; 4297 else 4298 new = pci_command | PCI_COMMAND_INTX_DISABLE; 4299 4300 if (new == pci_command) 4301 return; 4302 4303 pci_write_config_word(pdev, PCI_COMMAND, new); 4304 } 4305 EXPORT_SYMBOL_GPL(pci_intx); 4306 4307 /** 4308 * pci_wait_for_pending_transaction - wait for pending transaction 4309 * @dev: the PCI device to operate on 4310 * 4311 * Return 0 if transaction is pending 1 otherwise. 4312 */ 4313 int pci_wait_for_pending_transaction(struct pci_dev *dev) 4314 { 4315 if (!pci_is_pcie(dev)) 4316 return 1; 4317 4318 return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA, 4319 PCI_EXP_DEVSTA_TRPND); 4320 } 4321 EXPORT_SYMBOL(pci_wait_for_pending_transaction); 4322 4323 /** 4324 * pcie_flr - initiate a PCIe function level reset 4325 * @dev: device to reset 4326 * 4327 * Initiate a function level reset unconditionally on @dev without 4328 * checking any flags and DEVCAP 4329 */ 4330 int pcie_flr(struct pci_dev *dev) 4331 { 4332 int ret; 4333 4334 if (!pci_wait_for_pending_transaction(dev)) 4335 pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n"); 4336 4337 /* Have to call it after waiting for pending DMA transaction */ 4338 ret = pci_dev_reset_iommu_prepare(dev); 4339 if (ret) { 4340 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); 4341 return ret; 4342 } 4343 4344 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR); 4345 4346 if (dev->imm_ready) 4347 goto done; 4348 4349 /* 4350 * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within 4351 * 100ms, but may silently discard requests while the FLR is in 4352 * progress. Wait 100ms before trying to access the device. 4353 */ 4354 msleep(100); 4355 4356 ret = pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS); 4357 done: 4358 pci_dev_reset_iommu_done(dev); 4359 return ret; 4360 } 4361 EXPORT_SYMBOL_GPL(pcie_flr); 4362 4363 /** 4364 * pcie_reset_flr - initiate a PCIe function level reset 4365 * @dev: device to reset 4366 * @probe: if true, return 0 if device can be reset this way 4367 * 4368 * Initiate a function level reset on @dev. 4369 */ 4370 int pcie_reset_flr(struct pci_dev *dev, bool probe) 4371 { 4372 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET) 4373 return -ENOTTY; 4374 4375 if (!(dev->devcap & PCI_EXP_DEVCAP_FLR)) 4376 return -ENOTTY; 4377 4378 if (probe) 4379 return 0; 4380 4381 return pcie_flr(dev); 4382 } 4383 EXPORT_SYMBOL_GPL(pcie_reset_flr); 4384 4385 static int pci_af_flr(struct pci_dev *dev, bool probe) 4386 { 4387 int ret; 4388 int pos; 4389 u8 cap; 4390 4391 pos = pci_find_capability(dev, PCI_CAP_ID_AF); 4392 if (!pos) 4393 return -ENOTTY; 4394 4395 if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET) 4396 return -ENOTTY; 4397 4398 pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap); 4399 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR)) 4400 return -ENOTTY; 4401 4402 if (probe) 4403 return 0; 4404 4405 /* 4406 * Wait for Transaction Pending bit to clear. A word-aligned test 4407 * is used, so we use the control offset rather than status and shift 4408 * the test bit to match. 4409 */ 4410 if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL, 4411 PCI_AF_STATUS_TP << 8)) 4412 pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n"); 4413 4414 /* Have to call it after waiting for pending DMA transaction */ 4415 ret = pci_dev_reset_iommu_prepare(dev); 4416 if (ret) { 4417 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); 4418 return ret; 4419 } 4420 4421 pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR); 4422 4423 if (dev->imm_ready) 4424 goto done; 4425 4426 /* 4427 * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006, 4428 * updated 27 July 2006; a device must complete an FLR within 4429 * 100ms, but may silently discard requests while the FLR is in 4430 * progress. Wait 100ms before trying to access the device. 4431 */ 4432 msleep(100); 4433 4434 ret = pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS); 4435 done: 4436 pci_dev_reset_iommu_done(dev); 4437 return ret; 4438 } 4439 4440 /** 4441 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0. 4442 * @dev: Device to reset. 4443 * @probe: if true, return 0 if the device can be reset this way. 4444 * 4445 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is 4446 * unset, it will be reinitialized internally when going from PCI_D3hot to 4447 * PCI_D0. If that's the case and the device is not in a low-power state 4448 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset. 4449 * 4450 * NOTE: This causes the caller to sleep for twice the device power transition 4451 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms 4452 * by default (i.e. unless the @dev's d3hot_delay field has a different value). 4453 * Moreover, only devices in D0 can be reset by this function. 4454 */ 4455 static int pci_pm_reset(struct pci_dev *dev, bool probe) 4456 { 4457 u16 csr; 4458 int ret; 4459 4460 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET) 4461 return -ENOTTY; 4462 4463 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr); 4464 if (csr & PCI_PM_CTRL_NO_SOFT_RESET) 4465 return -ENOTTY; 4466 4467 if (probe) 4468 return 0; 4469 4470 if (dev->current_state != PCI_D0) 4471 return -EINVAL; 4472 4473 ret = pci_dev_reset_iommu_prepare(dev); 4474 if (ret) { 4475 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); 4476 return ret; 4477 } 4478 4479 csr &= ~PCI_PM_CTRL_STATE_MASK; 4480 csr |= PCI_D3hot; 4481 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); 4482 pci_dev_d3_sleep(dev); 4483 4484 csr &= ~PCI_PM_CTRL_STATE_MASK; 4485 csr |= PCI_D0; 4486 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); 4487 pci_dev_d3_sleep(dev); 4488 4489 ret = pci_dev_wait(dev, "PM D3hot->D0", PCIE_RESET_READY_POLL_MS); 4490 pci_dev_reset_iommu_done(dev); 4491 return ret; 4492 } 4493 4494 /** 4495 * pcie_wait_for_link_status - Wait for link status change 4496 * @pdev: Device whose link to wait for. 4497 * @use_lt: Use the LT bit if TRUE, or the DLLLA bit if FALSE. 4498 * @active: Waiting for active or inactive? 4499 * 4500 * Return 0 if successful, or -ETIMEDOUT if status has not changed within 4501 * PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds. 4502 */ 4503 static int pcie_wait_for_link_status(struct pci_dev *pdev, 4504 bool use_lt, bool active) 4505 { 4506 u16 lnksta_mask, lnksta_match; 4507 unsigned long end_jiffies; 4508 u16 lnksta; 4509 4510 lnksta_mask = use_lt ? PCI_EXP_LNKSTA_LT : PCI_EXP_LNKSTA_DLLLA; 4511 lnksta_match = active ? lnksta_mask : 0; 4512 4513 end_jiffies = jiffies + msecs_to_jiffies(PCIE_LINK_RETRAIN_TIMEOUT_MS); 4514 do { 4515 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta); 4516 if ((lnksta & lnksta_mask) == lnksta_match) 4517 return 0; 4518 msleep(1); 4519 } while (time_before(jiffies, end_jiffies)); 4520 4521 return -ETIMEDOUT; 4522 } 4523 4524 /** 4525 * pcie_retrain_link - Request a link retrain and wait for it to complete 4526 * @pdev: Device whose link to retrain. 4527 * @use_lt: Use the LT bit if TRUE, or the DLLLA bit if FALSE, for status. 4528 * 4529 * Trigger retraining of the PCIe Link and wait for the completion of the 4530 * retraining. As link retraining is known to asserts LBMS and may change 4531 * the Link Speed, LBMS is cleared after the retraining and the Link Speed 4532 * of the subordinate bus is updated. 4533 * 4534 * Retrain completion status is retrieved from the Link Status Register 4535 * according to @use_lt. It is not verified whether the use of the DLLLA 4536 * bit is valid. 4537 * 4538 * Return 0 if successful, or -ETIMEDOUT if training has not completed 4539 * within PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds. 4540 */ 4541 int pcie_retrain_link(struct pci_dev *pdev, bool use_lt) 4542 { 4543 int rc; 4544 4545 /* 4546 * Ensure the updated LNKCTL parameters are used during link 4547 * training by checking that there is no ongoing link training that 4548 * may have started before link parameters were changed, so as to 4549 * avoid LTSSM race as recommended in Implementation Note at the end 4550 * of PCIe r6.1 sec 7.5.3.7. 4551 */ 4552 rc = pcie_wait_for_link_status(pdev, true, false); 4553 if (rc) 4554 return rc; 4555 4556 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_RL); 4557 if (pdev->clear_retrain_link) { 4558 /* 4559 * Due to an erratum in some devices the Retrain Link bit 4560 * needs to be cleared again manually to allow the link 4561 * training to succeed. 4562 */ 4563 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_RL); 4564 } 4565 4566 rc = pcie_wait_for_link_status(pdev, use_lt, !use_lt); 4567 4568 /* 4569 * Clear LBMS after a manual retrain so that the bit can be used 4570 * to track link speed or width changes made by hardware itself 4571 * in attempt to correct unreliable link operation. 4572 */ 4573 pcie_reset_lbms(pdev); 4574 4575 /* 4576 * Ensure the Link Speed updates after retraining in case the Link 4577 * Speed was changed because of the retraining. While the bwctrl's 4578 * IRQ handler normally picks up the new Link Speed, clearing LBMS 4579 * races with the IRQ handler reading the Link Status register and 4580 * can result in the handler returning early without updating the 4581 * Link Speed. 4582 */ 4583 if (pdev->subordinate) 4584 pcie_update_link_speed(pdev->subordinate, PCIE_LINK_RETRAIN); 4585 4586 return rc; 4587 } 4588 4589 /** 4590 * pcie_wait_for_link_delay - Wait until link is active or inactive 4591 * @pdev: Bridge device 4592 * @active: waiting for active or inactive? 4593 * @delay: Delay to wait after link has become active (in ms) 4594 * 4595 * Use this to wait till link becomes active or inactive. 4596 */ 4597 static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active, 4598 int delay) 4599 { 4600 int rc; 4601 4602 /* 4603 * Some controllers might not implement link active reporting. In this 4604 * case, we wait for 1000 ms + any delay requested by the caller. 4605 */ 4606 if (!pdev->link_active_reporting) { 4607 msleep(PCIE_LINK_RETRAIN_TIMEOUT_MS + delay); 4608 return true; 4609 } 4610 4611 /* 4612 * PCIe r4.0 sec 6.6.1, a component must enter LTSSM Detect within 20ms, 4613 * after which we should expect the link to be active if the reset was 4614 * successful. If so, software must wait a minimum 100ms before sending 4615 * configuration requests to devices downstream this port. 4616 * 4617 * If the link fails to activate, either the device was physically 4618 * removed or the link is permanently failed. 4619 */ 4620 if (active) 4621 msleep(20); 4622 rc = pcie_wait_for_link_status(pdev, false, active); 4623 if (active) { 4624 if (rc) 4625 rc = pcie_failed_link_retrain(pdev); 4626 if (rc) 4627 return false; 4628 4629 msleep(delay); 4630 return true; 4631 } 4632 4633 if (rc) 4634 return false; 4635 4636 return true; 4637 } 4638 4639 /** 4640 * pcie_wait_for_link - Wait until link is active or inactive 4641 * @pdev: Bridge device 4642 * @active: waiting for active or inactive? 4643 * 4644 * Use this to wait till link becomes active or inactive. 4645 */ 4646 bool pcie_wait_for_link(struct pci_dev *pdev, bool active) 4647 { 4648 return pcie_wait_for_link_delay(pdev, active, 100); 4649 } 4650 4651 /* 4652 * Find maximum D3cold delay required by all the devices on the bus. The 4653 * spec says 100 ms, but firmware can lower it and we allow drivers to 4654 * increase it as well. 4655 * 4656 * Context: Called with @pci_bus_sem locked for reading. 4657 */ 4658 static int pci_bus_max_d3cold_delay(const struct pci_bus *bus) 4659 { 4660 const struct pci_dev *pdev; 4661 int min_delay = 100; 4662 int max_delay = 0; 4663 4664 lockdep_assert_held(&pci_bus_sem); 4665 4666 list_for_each_entry(pdev, &bus->devices, bus_list) { 4667 if (pdev->d3cold_delay < min_delay) 4668 min_delay = pdev->d3cold_delay; 4669 if (pdev->d3cold_delay > max_delay) 4670 max_delay = pdev->d3cold_delay; 4671 } 4672 4673 return max(min_delay, max_delay); 4674 } 4675 4676 /** 4677 * pci_bridge_wait_for_secondary_bus - Wait for secondary bus to be accessible 4678 * @dev: PCI bridge 4679 * @reset_type: reset type in human-readable form 4680 * 4681 * Handle necessary delays before access to the devices on the secondary 4682 * side of the bridge are permitted after D3cold to D0 transition 4683 * or Conventional Reset. 4684 * 4685 * For PCIe this means the delays in PCIe 5.0 section 6.6.1. For 4686 * conventional PCI it means Tpvrh + Trhfa specified in PCI 3.0 section 4687 * 4.3.2. 4688 * 4689 * Return 0 on success or -ENOTTY if the first device on the secondary bus 4690 * failed to become accessible. 4691 */ 4692 int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type) 4693 { 4694 struct pci_dev *child __free(pci_dev_put) = NULL; 4695 int delay; 4696 4697 if (pci_dev_is_disconnected(dev)) 4698 return 0; 4699 4700 if (!pci_is_bridge(dev)) 4701 return 0; 4702 4703 down_read(&pci_bus_sem); 4704 4705 /* 4706 * We only deal with devices that are present currently on the bus. 4707 * For any hot-added devices the access delay is handled in pciehp 4708 * board_added(). In case of ACPI hotplug the firmware is expected 4709 * to configure the devices before OS is notified. 4710 */ 4711 if (!dev->subordinate || list_empty(&dev->subordinate->devices)) { 4712 up_read(&pci_bus_sem); 4713 return 0; 4714 } 4715 4716 /* Take d3cold_delay requirements into account */ 4717 delay = pci_bus_max_d3cold_delay(dev->subordinate); 4718 if (!delay) { 4719 up_read(&pci_bus_sem); 4720 return 0; 4721 } 4722 4723 child = pci_dev_get(list_first_entry(&dev->subordinate->devices, 4724 struct pci_dev, bus_list)); 4725 up_read(&pci_bus_sem); 4726 4727 /* 4728 * Conventional PCI and PCI-X we need to wait Tpvrh + Trhfa before 4729 * accessing the device after reset (that is 1000 ms + 100 ms). 4730 */ 4731 if (!pci_is_pcie(dev)) { 4732 pci_dbg(dev, "waiting %d ms for secondary bus\n", 1000 + delay); 4733 msleep(1000 + delay); 4734 return 0; 4735 } 4736 4737 /* 4738 * For PCIe downstream and root ports that do not support speeds 4739 * greater than 5 GT/s need to wait minimum 100 ms. For higher 4740 * speeds (gen3) we need to wait first for the data link layer to 4741 * become active. 4742 * 4743 * However, 100 ms is the minimum and the PCIe spec says the 4744 * software must allow at least 1s before it can determine that the 4745 * device that did not respond is a broken device. Also device can 4746 * take longer than that to respond if it indicates so through Request 4747 * Retry Status completions. 4748 * 4749 * Therefore we wait for 100 ms and check for the device presence 4750 * until the timeout expires. 4751 */ 4752 if (!pcie_downstream_port(dev)) 4753 return 0; 4754 4755 if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) { 4756 u16 status; 4757 4758 pci_dbg(dev, "waiting %d ms for downstream link\n", delay); 4759 msleep(delay); 4760 4761 if (!pci_dev_wait(child, reset_type, PCI_RESET_WAIT - delay)) 4762 return 0; 4763 4764 /* 4765 * If the port supports active link reporting we now check 4766 * whether the link is active and if not bail out early with 4767 * the assumption that the device is not present anymore. 4768 */ 4769 if (!dev->link_active_reporting) 4770 return -ENOTTY; 4771 4772 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &status); 4773 if (!(status & PCI_EXP_LNKSTA_DLLLA)) 4774 return -ENOTTY; 4775 4776 return pci_dev_wait(child, reset_type, 4777 PCIE_RESET_READY_POLL_MS - PCI_RESET_WAIT); 4778 } 4779 4780 pci_dbg(dev, "waiting %d ms for downstream link, after activation\n", 4781 delay); 4782 if (!pcie_wait_for_link_delay(dev, true, delay)) { 4783 /* Did not train, no need to wait any further */ 4784 pci_info(dev, "Data Link Layer Link Active not set in %d msec\n", delay); 4785 return -ENOTTY; 4786 } 4787 4788 return pci_dev_wait(child, reset_type, 4789 PCIE_RESET_READY_POLL_MS - delay); 4790 } 4791 4792 void pci_reset_secondary_bus(struct pci_dev *dev) 4793 { 4794 u16 ctrl; 4795 4796 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl); 4797 ctrl |= PCI_BRIDGE_CTL_BUS_RESET; 4798 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl); 4799 4800 /* 4801 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double 4802 * this to 2ms to ensure that we meet the minimum requirement. 4803 */ 4804 msleep(2); 4805 4806 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET; 4807 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl); 4808 } 4809 4810 void __weak pcibios_reset_secondary_bus(struct pci_dev *dev) 4811 { 4812 pci_reset_secondary_bus(dev); 4813 } 4814 4815 /** 4816 * pci_bridge_secondary_bus_reset - Reset the secondary bus on a PCI bridge. 4817 * @dev: Bridge device 4818 * 4819 * Use the bridge control register to assert reset on the secondary bus. 4820 * Devices on the secondary bus are left in power-on state. 4821 */ 4822 int pci_bridge_secondary_bus_reset(struct pci_dev *dev) 4823 { 4824 if (!dev->block_cfg_access) 4825 pci_warn_once(dev, "unlocked secondary bus reset via: %pS\n", 4826 __builtin_return_address(0)); 4827 pcibios_reset_secondary_bus(dev); 4828 4829 return pci_bridge_wait_for_secondary_bus(dev, "bus reset"); 4830 } 4831 EXPORT_SYMBOL_GPL(pci_bridge_secondary_bus_reset); 4832 4833 static int pci_parent_bus_reset(struct pci_dev *dev, bool probe) 4834 { 4835 struct pci_dev *pdev; 4836 4837 if (pci_is_root_bus(dev->bus) || dev->subordinate || 4838 !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET) 4839 return -ENOTTY; 4840 4841 list_for_each_entry(pdev, &dev->bus->devices, bus_list) 4842 if (pdev != dev) 4843 return -ENOTTY; 4844 4845 if (probe) 4846 return 0; 4847 4848 return pci_bridge_secondary_bus_reset(dev->bus->self); 4849 } 4850 4851 static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, bool probe) 4852 { 4853 int rc = -ENOTTY; 4854 4855 if (!hotplug || !try_module_get(hotplug->owner)) 4856 return rc; 4857 4858 if (hotplug->ops->reset_slot) 4859 rc = hotplug->ops->reset_slot(hotplug, probe); 4860 4861 module_put(hotplug->owner); 4862 4863 return rc; 4864 } 4865 4866 static int pci_dev_reset_slot_function(struct pci_dev *dev, bool probe) 4867 { 4868 if (dev->multifunction || dev->subordinate || !dev->slot || 4869 dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET) 4870 return -ENOTTY; 4871 4872 return pci_reset_hotplug_slot(dev->slot->hotplug, probe); 4873 } 4874 4875 static u16 cxl_port_dvsec(struct pci_dev *dev) 4876 { 4877 return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_CXL, 4878 PCI_DVSEC_CXL_PORT); 4879 } 4880 4881 static bool cxl_sbr_masked(struct pci_dev *dev) 4882 { 4883 u16 dvsec, reg; 4884 int rc; 4885 4886 dvsec = cxl_port_dvsec(dev); 4887 if (!dvsec) 4888 return false; 4889 4890 rc = pci_read_config_word(dev, dvsec + PCI_DVSEC_CXL_PORT_CTL, ®); 4891 if (rc || PCI_POSSIBLE_ERROR(reg)) 4892 return false; 4893 4894 /* 4895 * Per CXL spec r3.1, sec 8.1.5.2, when "Unmask SBR" is 0, the SBR 4896 * bit in Bridge Control has no effect. When 1, the Port generates 4897 * hot reset when the SBR bit is set to 1. 4898 */ 4899 if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) 4900 return false; 4901 4902 return true; 4903 } 4904 4905 static int pci_reset_bus_function(struct pci_dev *dev, bool probe) 4906 { 4907 struct pci_dev *bridge = pci_upstream_bridge(dev); 4908 int rc; 4909 4910 /* 4911 * If "dev" is below a CXL port that has SBR control masked, SBR 4912 * won't do anything, so return error. 4913 */ 4914 if (bridge && pcie_is_cxl(bridge) && cxl_sbr_masked(bridge)) 4915 return -ENOTTY; 4916 4917 rc = pci_dev_reset_iommu_prepare(dev); 4918 if (rc) { 4919 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); 4920 return rc; 4921 } 4922 4923 rc = pci_dev_reset_slot_function(dev, probe); 4924 if (rc != -ENOTTY) 4925 goto done; 4926 4927 rc = pci_parent_bus_reset(dev, probe); 4928 done: 4929 pci_dev_reset_iommu_done(dev); 4930 return rc; 4931 } 4932 4933 static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) 4934 { 4935 struct pci_dev *bridge; 4936 u16 dvsec, reg, val; 4937 int rc; 4938 4939 bridge = pci_upstream_bridge(dev); 4940 if (!bridge) 4941 return -ENOTTY; 4942 4943 dvsec = cxl_port_dvsec(bridge); 4944 if (!dvsec) 4945 return -ENOTTY; 4946 4947 if (probe) 4948 return 0; 4949 4950 rc = pci_read_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, ®); 4951 if (rc) 4952 return -ENOTTY; 4953 4954 rc = pci_dev_reset_iommu_prepare(dev); 4955 if (rc) { 4956 pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); 4957 return rc; 4958 } 4959 4960 if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) { 4961 val = reg; 4962 } else { 4963 val = reg | PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR; 4964 pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, 4965 val); 4966 } 4967 4968 rc = pci_reset_bus_function(dev, probe); 4969 4970 if (reg != val) 4971 pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, 4972 reg); 4973 4974 pci_dev_reset_iommu_done(dev); 4975 return rc; 4976 } 4977 4978 void pci_dev_lock(struct pci_dev *dev) 4979 { 4980 /* block PM suspend, driver probe, etc. */ 4981 device_lock(&dev->dev); 4982 pci_cfg_access_lock(dev); 4983 } 4984 EXPORT_SYMBOL_GPL(pci_dev_lock); 4985 4986 /* Return 1 on successful lock, 0 on contention */ 4987 int pci_dev_trylock(struct pci_dev *dev) 4988 { 4989 if (device_trylock(&dev->dev)) { 4990 if (pci_cfg_access_trylock(dev)) 4991 return 1; 4992 device_unlock(&dev->dev); 4993 } 4994 4995 return 0; 4996 } 4997 EXPORT_SYMBOL_GPL(pci_dev_trylock); 4998 4999 void pci_dev_unlock(struct pci_dev *dev) 5000 { 5001 pci_cfg_access_unlock(dev); 5002 device_unlock(&dev->dev); 5003 } 5004 EXPORT_SYMBOL_GPL(pci_dev_unlock); 5005 5006 static void pci_dev_save_and_disable(struct pci_dev *dev) 5007 { 5008 const struct pci_error_handlers *err_handler = 5009 dev->driver ? dev->driver->err_handler : NULL; 5010 5011 /* 5012 * dev->driver->err_handler->reset_prepare() is protected against 5013 * races with ->remove() by the device lock, which must be held by 5014 * the caller. 5015 */ 5016 device_lock_assert(&dev->dev); 5017 if (err_handler && err_handler->reset_prepare) 5018 err_handler->reset_prepare(dev); 5019 else if (dev->driver) 5020 pci_warn(dev, "resetting"); 5021 5022 /* 5023 * Wake-up device prior to save. PM registers default to D0 after 5024 * reset and a simple register restore doesn't reliably return 5025 * to a non-D0 state anyway. 5026 */ 5027 pci_set_power_state(dev, PCI_D0); 5028 5029 pci_save_state(dev); 5030 /* 5031 * Disable the device by clearing the Command register, except for 5032 * INTx-disable which is set. This not only disables MMIO and I/O port 5033 * BARs, but also prevents the device from being Bus Master, preventing 5034 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3 5035 * compliant devices, INTx-disable prevents legacy interrupts. 5036 */ 5037 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); 5038 } 5039 5040 static void pci_dev_restore(struct pci_dev *dev) 5041 { 5042 const struct pci_error_handlers *err_handler = 5043 dev->driver ? dev->driver->err_handler : NULL; 5044 5045 pci_restore_state(dev); 5046 5047 /* 5048 * dev->driver->err_handler->reset_done() is protected against 5049 * races with ->remove() by the device lock, which must be held by 5050 * the caller. 5051 */ 5052 if (err_handler && err_handler->reset_done) 5053 err_handler->reset_done(dev); 5054 else if (dev->driver) 5055 pci_warn(dev, "reset done"); 5056 } 5057 5058 /* dev->reset_methods[] is a 0-terminated list of indices into this array */ 5059 const struct pci_reset_fn_method pci_reset_fn_methods[] = { 5060 { }, 5061 { pci_dev_specific_reset, .name = "device_specific" }, 5062 { pci_dev_acpi_reset, .name = "acpi" }, 5063 { pcie_reset_flr, .name = "flr" }, 5064 { pci_af_flr, .name = "af_flr" }, 5065 { pci_pm_reset, .name = "pm" }, 5066 { pci_reset_bus_function, .name = "bus" }, 5067 { cxl_reset_bus_function, .name = "cxl_bus" }, 5068 }; 5069 5070 /** 5071 * __pci_reset_function_locked - reset a PCI device function while holding 5072 * the @dev mutex lock. 5073 * @dev: PCI device to reset 5074 * 5075 * Some devices allow an individual function to be reset without affecting 5076 * other functions in the same device. The PCI device must be responsive 5077 * to PCI config space in order to use this function. 5078 * 5079 * The device function is presumed to be unused and the caller is holding 5080 * the device mutex lock when this function is called. 5081 * 5082 * Resetting the device will make the contents of PCI configuration space 5083 * random, so any caller of this must be prepared to reinitialise the 5084 * device including MSI, bus mastering, BARs, decoding IO and memory spaces, 5085 * etc. 5086 * 5087 * Context: The caller must hold the device lock. 5088 * 5089 * Return: 0 if the device function was successfully reset or negative if the 5090 * device doesn't support resetting a single function. 5091 */ 5092 int __pci_reset_function_locked(struct pci_dev *dev) 5093 { 5094 int i, m, rc; 5095 const struct pci_reset_fn_method *method; 5096 5097 might_sleep(); 5098 device_lock_assert(&dev->dev); 5099 5100 /* 5101 * A reset method returns -ENOTTY if it doesn't support this device and 5102 * we should try the next method. 5103 * 5104 * If it returns 0 (success), we're finished. If it returns any other 5105 * error, we're also finished: this indicates that further reset 5106 * mechanisms might be broken on the device. 5107 */ 5108 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) { 5109 m = dev->reset_methods[i]; 5110 if (!m) 5111 return -ENOTTY; 5112 5113 method = &pci_reset_fn_methods[m]; 5114 pci_dbg(dev, "reset via %s\n", method->name); 5115 rc = method->reset_fn(dev, PCI_RESET_DO_RESET); 5116 if (!rc) 5117 return 0; 5118 5119 pci_dbg(dev, "%s failed with %d\n", method->name, rc); 5120 if (rc != -ENOTTY) 5121 return rc; 5122 } 5123 5124 return -ENOTTY; 5125 } 5126 EXPORT_SYMBOL_GPL(__pci_reset_function_locked); 5127 5128 /** 5129 * pci_init_reset_methods - check whether device can be safely reset 5130 * and store supported reset mechanisms. 5131 * @dev: PCI device to check for reset mechanisms 5132 * 5133 * Some devices allow an individual function to be reset without affecting 5134 * other functions in the same device. The PCI device must be in D0-D3hot 5135 * state. 5136 * 5137 * Stores reset mechanisms supported by device in reset_methods byte array 5138 * which is a member of struct pci_dev. 5139 */ 5140 void pci_init_reset_methods(struct pci_dev *dev) 5141 { 5142 int m, i, rc; 5143 5144 BUILD_BUG_ON(ARRAY_SIZE(pci_reset_fn_methods) != PCI_NUM_RESET_METHODS); 5145 5146 might_sleep(); 5147 5148 i = 0; 5149 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) { 5150 rc = pci_reset_fn_methods[m].reset_fn(dev, PCI_RESET_PROBE); 5151 if (!rc) 5152 dev->reset_methods[i++] = m; 5153 else if (rc != -ENOTTY) 5154 break; 5155 } 5156 5157 dev->reset_methods[i] = 0; 5158 } 5159 5160 /** 5161 * pci_reset_function - quiesce and reset a PCI device function 5162 * @dev: PCI device to reset 5163 * 5164 * Some devices allow an individual function to be reset without affecting 5165 * other functions in the same device. The PCI device must be responsive 5166 * to PCI config space in order to use this function. 5167 * 5168 * This function does not just reset the PCI portion of a device, but 5169 * clears all the state associated with the device. This function differs 5170 * from __pci_reset_function_locked() in that it saves and restores device state 5171 * over the reset and takes the PCI device lock. 5172 * 5173 * Returns 0 if the device function was successfully reset or negative if the 5174 * device doesn't support resetting a single function. 5175 */ 5176 int pci_reset_function(struct pci_dev *dev) 5177 { 5178 struct pci_dev *bridge; 5179 int rc; 5180 5181 if (!pci_reset_supported(dev)) 5182 return -ENOTTY; 5183 5184 /* 5185 * If there's no upstream bridge, no locking is needed since there is 5186 * no upstream bridge configuration to hold consistent. 5187 */ 5188 bridge = pci_upstream_bridge(dev); 5189 if (bridge) 5190 pci_dev_lock(bridge); 5191 5192 pci_dev_lock(dev); 5193 pci_dev_save_and_disable(dev); 5194 5195 rc = __pci_reset_function_locked(dev); 5196 5197 pci_dev_restore(dev); 5198 pci_dev_unlock(dev); 5199 5200 if (bridge) 5201 pci_dev_unlock(bridge); 5202 5203 return rc; 5204 } 5205 EXPORT_SYMBOL_GPL(pci_reset_function); 5206 5207 /** 5208 * pci_reset_function_locked - quiesce and reset a PCI device function 5209 * @dev: PCI device to reset 5210 * 5211 * Some devices allow an individual function to be reset without affecting 5212 * other functions in the same device. The PCI device must be responsive 5213 * to PCI config space in order to use this function. 5214 * 5215 * This function does not just reset the PCI portion of a device, but 5216 * clears all the state associated with the device. This function differs 5217 * from __pci_reset_function_locked() in that it saves and restores device state 5218 * over the reset. It also differs from pci_reset_function() in that it 5219 * requires the PCI device lock to be held. 5220 * 5221 * Context: The caller must hold the device lock. 5222 * 5223 * Return: 0 if the device function was successfully reset or negative if the 5224 * device doesn't support resetting a single function. 5225 */ 5226 int pci_reset_function_locked(struct pci_dev *dev) 5227 { 5228 int rc; 5229 5230 device_lock_assert(&dev->dev); 5231 5232 if (!pci_reset_supported(dev)) 5233 return -ENOTTY; 5234 5235 pci_dev_save_and_disable(dev); 5236 5237 rc = __pci_reset_function_locked(dev); 5238 5239 pci_dev_restore(dev); 5240 5241 return rc; 5242 } 5243 EXPORT_SYMBOL_GPL(pci_reset_function_locked); 5244 5245 /** 5246 * pci_try_reset_function - quiesce and reset a PCI device function 5247 * @dev: PCI device to reset 5248 * 5249 * Same as above, except return -EAGAIN if unable to lock device. 5250 */ 5251 int pci_try_reset_function(struct pci_dev *dev) 5252 { 5253 int rc; 5254 5255 if (!pci_reset_supported(dev)) 5256 return -ENOTTY; 5257 5258 if (!pci_dev_trylock(dev)) 5259 return -EAGAIN; 5260 5261 pci_dev_save_and_disable(dev); 5262 rc = __pci_reset_function_locked(dev); 5263 pci_dev_restore(dev); 5264 pci_dev_unlock(dev); 5265 5266 return rc; 5267 } 5268 EXPORT_SYMBOL_GPL(pci_try_reset_function); 5269 5270 /* Do any devices on or below this bus prevent a bus reset? */ 5271 static bool pci_bus_resettable(struct pci_bus *bus) 5272 { 5273 struct pci_dev *dev; 5274 5275 5276 if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)) 5277 return false; 5278 5279 list_for_each_entry(dev, &bus->devices, bus_list) { 5280 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET || 5281 (dev->subordinate && !pci_bus_resettable(dev->subordinate))) 5282 return false; 5283 } 5284 5285 return true; 5286 } 5287 5288 static void pci_bus_lock(struct pci_bus *bus); 5289 static void pci_bus_unlock(struct pci_bus *bus); 5290 static int pci_bus_trylock(struct pci_bus *bus); 5291 5292 /* Lock devices from the top of the tree down */ 5293 static void __pci_bus_lock(struct pci_bus *bus, struct pci_slot *slot) 5294 { 5295 struct pci_dev *dev, *bridge = bus->self; 5296 5297 if (bridge) 5298 pci_dev_lock(bridge); 5299 5300 list_for_each_entry(dev, &bus->devices, bus_list) { 5301 if (slot && (!dev->slot || dev->slot != slot)) 5302 continue; 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, struct pci_slot *slot) 5312 { 5313 struct pci_dev *dev, *bridge = bus->self; 5314 5315 list_for_each_entry(dev, &bus->devices, bus_list) { 5316 if (slot && (!dev->slot || dev->slot != slot)) 5317 continue; 5318 if (dev->subordinate) 5319 pci_bus_unlock(dev->subordinate); 5320 else 5321 pci_dev_unlock(dev); 5322 } 5323 5324 if (bridge) 5325 pci_dev_unlock(bridge); 5326 } 5327 5328 /* Return 1 on successful lock, 0 on contention */ 5329 static int __pci_bus_trylock(struct pci_bus *bus, struct pci_slot *slot) 5330 { 5331 struct pci_dev *dev, *bridge = bus->self; 5332 5333 if (bridge && !pci_dev_trylock(bridge)) 5334 return 0; 5335 5336 list_for_each_entry(dev, &bus->devices, bus_list) { 5337 if (slot && (!dev->slot || dev->slot != slot)) 5338 continue; 5339 if (dev->subordinate) { 5340 if (!pci_bus_trylock(dev->subordinate)) 5341 goto unlock; 5342 } else if (!pci_dev_trylock(dev)) 5343 goto unlock; 5344 } 5345 return 1; 5346 5347 unlock: 5348 list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) { 5349 if (slot && (!dev->slot || dev->slot != slot)) 5350 continue; 5351 if (dev->subordinate) 5352 pci_bus_unlock(dev->subordinate); 5353 else 5354 pci_dev_unlock(dev); 5355 } 5356 5357 if (bridge) 5358 pci_dev_unlock(bridge); 5359 return 0; 5360 } 5361 5362 /* Lock devices from the top of the tree down */ 5363 static void pci_bus_lock(struct pci_bus *bus) 5364 { 5365 __pci_bus_lock(bus, NULL); 5366 } 5367 5368 /* Unlock devices from the bottom of the tree up */ 5369 static void pci_bus_unlock(struct pci_bus *bus) 5370 { 5371 __pci_bus_unlock(bus, NULL); 5372 } 5373 5374 /* Return 1 on successful lock, 0 on contention */ 5375 static int pci_bus_trylock(struct pci_bus *bus) 5376 { 5377 return __pci_bus_trylock(bus, NULL); 5378 } 5379 5380 /* Do any devices on or below this slot prevent a bus reset? */ 5381 static bool pci_slot_resettable(struct pci_slot *slot) 5382 { 5383 struct pci_dev *dev, *bridge = slot->bus->self; 5384 5385 if (bridge && (bridge->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)) 5386 return false; 5387 5388 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5389 if (!dev->slot || dev->slot != slot) 5390 continue; 5391 if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET || 5392 (dev->subordinate && !pci_bus_resettable(dev->subordinate))) 5393 return false; 5394 } 5395 5396 return true; 5397 } 5398 5399 /* Lock devices from the top of the tree down */ 5400 static void pci_slot_lock(struct pci_slot *slot) 5401 { 5402 __pci_bus_lock(slot->bus, slot); 5403 } 5404 5405 /* Unlock devices from the bottom of the tree up */ 5406 static void pci_slot_unlock(struct pci_slot *slot) 5407 { 5408 __pci_bus_unlock(slot->bus, slot); 5409 } 5410 5411 /* Return 1 on successful lock, 0 on contention */ 5412 static int pci_slot_trylock(struct pci_slot *slot) 5413 { 5414 return __pci_bus_trylock(slot->bus, slot); 5415 } 5416 5417 /* 5418 * Save and disable devices from the top of the tree down while holding 5419 * the @dev mutex lock for the entire tree. 5420 */ 5421 static void pci_bus_save_and_disable_locked(struct pci_bus *bus) 5422 { 5423 struct pci_dev *dev; 5424 5425 list_for_each_entry(dev, &bus->devices, bus_list) { 5426 pci_dev_save_and_disable(dev); 5427 if (dev->subordinate) 5428 pci_bus_save_and_disable_locked(dev->subordinate); 5429 } 5430 } 5431 5432 /* 5433 * Restore devices from top of the tree down while holding @dev mutex lock 5434 * for the entire tree. Parent bridges need to be restored before we can 5435 * get to subordinate devices. 5436 */ 5437 static void pci_bus_restore_locked(struct pci_bus *bus) 5438 { 5439 struct pci_dev *dev; 5440 5441 list_for_each_entry(dev, &bus->devices, bus_list) { 5442 pci_dev_restore(dev); 5443 if (dev->subordinate) { 5444 pci_bridge_wait_for_secondary_bus(dev, "bus reset"); 5445 pci_bus_restore_locked(dev->subordinate); 5446 } 5447 } 5448 } 5449 5450 /* 5451 * Save and disable devices from the top of the tree down while holding 5452 * the @dev mutex lock for the entire tree. 5453 */ 5454 static void pci_slot_save_and_disable_locked(struct pci_slot *slot) 5455 { 5456 struct pci_dev *dev; 5457 5458 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5459 if (!dev->slot || dev->slot != slot) 5460 continue; 5461 pci_dev_save_and_disable(dev); 5462 if (dev->subordinate) 5463 pci_bus_save_and_disable_locked(dev->subordinate); 5464 } 5465 } 5466 5467 /* 5468 * Restore devices from top of the tree down while holding @dev mutex lock 5469 * for the entire tree. Parent bridges need to be restored before we can 5470 * get to subordinate devices. 5471 */ 5472 static void pci_slot_restore_locked(struct pci_slot *slot) 5473 { 5474 struct pci_dev *dev; 5475 5476 list_for_each_entry(dev, &slot->bus->devices, bus_list) { 5477 if (!dev->slot || dev->slot != slot) 5478 continue; 5479 pci_dev_restore(dev); 5480 if (dev->subordinate) { 5481 pci_bridge_wait_for_secondary_bus(dev, "slot reset"); 5482 pci_bus_restore_locked(dev->subordinate); 5483 } 5484 } 5485 } 5486 5487 static int pci_slot_reset(struct pci_slot *slot, bool probe) 5488 { 5489 int rc; 5490 5491 if (!slot || !pci_slot_resettable(slot)) 5492 return -ENOTTY; 5493 5494 if (!probe) 5495 pci_slot_lock(slot); 5496 5497 might_sleep(); 5498 5499 rc = pci_reset_hotplug_slot(slot->hotplug, probe); 5500 5501 if (!probe) 5502 pci_slot_unlock(slot); 5503 5504 return rc; 5505 } 5506 5507 /** 5508 * pci_probe_reset_slot - probe whether a PCI slot can be reset 5509 * @slot: PCI slot to probe 5510 * 5511 * Return 0 if slot can be reset, negative if a slot reset is not supported. 5512 */ 5513 int pci_probe_reset_slot(struct pci_slot *slot) 5514 { 5515 return pci_slot_reset(slot, PCI_RESET_PROBE); 5516 } 5517 EXPORT_SYMBOL_GPL(pci_probe_reset_slot); 5518 5519 /** 5520 * pci_try_reset_slot - Try to reset a PCI slot 5521 * @slot: PCI slot to reset 5522 * 5523 * A PCI bus may host multiple slots, each slot may support a reset mechanism 5524 * independent of other slots. For instance, some slots may support slot power 5525 * control. In the case of a 1:1 bus to slot architecture, this function may 5526 * wrap the bus reset to avoid spurious slot related events such as hotplug. 5527 * Generally a slot reset should be attempted before a bus reset. All of the 5528 * function of the slot and any subordinate buses behind the slot are reset 5529 * through this function. PCI config space of all devices in the slot and 5530 * behind the slot is saved before and restored after reset. 5531 * 5532 * Same as above except return -EAGAIN if the slot cannot be locked 5533 */ 5534 static int pci_try_reset_slot(struct pci_slot *slot) 5535 { 5536 int rc; 5537 5538 rc = pci_slot_reset(slot, PCI_RESET_PROBE); 5539 if (rc) 5540 return rc; 5541 5542 if (pci_slot_trylock(slot)) { 5543 pci_slot_save_and_disable_locked(slot); 5544 might_sleep(); 5545 rc = pci_reset_hotplug_slot(slot->hotplug, PCI_RESET_DO_RESET); 5546 pci_slot_restore_locked(slot); 5547 pci_slot_unlock(slot); 5548 } else 5549 rc = -EAGAIN; 5550 5551 return rc; 5552 } 5553 5554 static int pci_bus_reset(struct pci_bus *bus, bool probe) 5555 { 5556 int ret; 5557 5558 if (!bus->self || !pci_bus_resettable(bus)) 5559 return -ENOTTY; 5560 5561 if (probe) 5562 return 0; 5563 5564 pci_bus_lock(bus); 5565 5566 might_sleep(); 5567 5568 ret = pci_bridge_secondary_bus_reset(bus->self); 5569 5570 pci_bus_unlock(bus); 5571 5572 return ret; 5573 } 5574 5575 /** 5576 * pci_try_reset_bus - Try to reset a PCI bus 5577 * @bus: top level PCI bus to reset 5578 * 5579 * Same as above except return -EAGAIN if the bus cannot be locked 5580 */ 5581 static int pci_try_reset_bus(struct pci_bus *bus) 5582 { 5583 int rc; 5584 5585 rc = pci_bus_reset(bus, PCI_RESET_PROBE); 5586 if (rc) 5587 return rc; 5588 5589 if (pci_bus_trylock(bus)) { 5590 pci_bus_save_and_disable_locked(bus); 5591 might_sleep(); 5592 rc = pci_bridge_secondary_bus_reset(bus->self); 5593 pci_bus_restore_locked(bus); 5594 pci_bus_unlock(bus); 5595 } else 5596 rc = -EAGAIN; 5597 5598 return rc; 5599 } 5600 5601 #define PCI_RESET_RESTORE true 5602 #define PCI_RESET_NO_RESTORE false 5603 /** 5604 * pci_reset_bridge - reset a bridge's subordinate bus 5605 * @bridge: bridge that connects to the bus to reset 5606 * @restore: when true use a reset method that invokes pci_dev_restore() post 5607 * reset for affected devices 5608 * 5609 * This function will first try to reset the slots on this bus if the method is 5610 * available. If slot reset fails or is not available, this will fall back to a 5611 * secondary bus reset. 5612 */ 5613 static int pci_reset_bridge(struct pci_dev *bridge, bool restore) 5614 { 5615 struct pci_bus *bus = bridge->subordinate; 5616 struct pci_slot *slot; 5617 5618 if (!bus) 5619 return -ENOTTY; 5620 5621 mutex_lock(&pci_slot_mutex); 5622 if (list_empty(&bus->slots)) 5623 goto bus_reset; 5624 5625 list_for_each_entry(slot, &bus->slots, list) 5626 if (pci_probe_reset_slot(slot)) 5627 goto bus_reset; 5628 5629 list_for_each_entry(slot, &bus->slots, list) { 5630 int ret; 5631 5632 if (restore) 5633 ret = pci_try_reset_slot(slot); 5634 else 5635 ret = pci_slot_reset(slot, PCI_RESET_DO_RESET); 5636 5637 if (ret) 5638 goto bus_reset; 5639 } 5640 5641 mutex_unlock(&pci_slot_mutex); 5642 return 0; 5643 bus_reset: 5644 mutex_unlock(&pci_slot_mutex); 5645 5646 if (restore) 5647 return pci_try_reset_bus(bus); 5648 return pci_bus_reset(bridge->subordinate, PCI_RESET_DO_RESET); 5649 } 5650 5651 /** 5652 * pci_bus_error_reset - reset the bridge's subordinate bus 5653 * @bridge: The parent device that connects to the bus to reset 5654 */ 5655 int pci_bus_error_reset(struct pci_dev *bridge) 5656 { 5657 return pci_reset_bridge(bridge, PCI_RESET_NO_RESTORE); 5658 } 5659 5660 int pci_try_reset_bridge(struct pci_dev *bridge) 5661 { 5662 return pci_reset_bridge(bridge, PCI_RESET_RESTORE); 5663 } 5664 5665 /** 5666 * pci_probe_reset_bus - probe whether a PCI bus can be reset 5667 * @bus: PCI bus to probe 5668 * 5669 * Return 0 if bus can be reset, negative if a bus reset is not supported. 5670 */ 5671 int pci_probe_reset_bus(struct pci_bus *bus) 5672 { 5673 return pci_bus_reset(bus, PCI_RESET_PROBE); 5674 } 5675 EXPORT_SYMBOL_GPL(pci_probe_reset_bus); 5676 5677 /** 5678 * pci_reset_bus - Try to reset a PCI bus 5679 * @pdev: top level PCI device to reset via slot/bus 5680 * 5681 * Same as above except return -EAGAIN if the bus cannot be locked 5682 */ 5683 int pci_reset_bus(struct pci_dev *pdev) 5684 { 5685 return (!pci_probe_reset_slot(pdev->slot)) ? 5686 pci_try_reset_slot(pdev->slot) : pci_try_reset_bus(pdev->bus); 5687 } 5688 EXPORT_SYMBOL_GPL(pci_reset_bus); 5689 5690 /** 5691 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count 5692 * @dev: PCI device to query 5693 * 5694 * Returns mmrbc: maximum designed memory read count in bytes or 5695 * appropriate error value. 5696 */ 5697 int pcix_get_max_mmrbc(struct pci_dev *dev) 5698 { 5699 int cap; 5700 u32 stat; 5701 5702 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); 5703 if (!cap) 5704 return -EINVAL; 5705 5706 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat)) 5707 return -EINVAL; 5708 5709 return 512 << FIELD_GET(PCI_X_STATUS_MAX_READ, stat); 5710 } 5711 EXPORT_SYMBOL(pcix_get_max_mmrbc); 5712 5713 /** 5714 * pcix_get_mmrbc - get PCI-X maximum memory read byte count 5715 * @dev: PCI device to query 5716 * 5717 * Returns mmrbc: maximum memory read count in bytes or appropriate error 5718 * value. 5719 */ 5720 int pcix_get_mmrbc(struct pci_dev *dev) 5721 { 5722 int cap; 5723 u16 cmd; 5724 5725 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); 5726 if (!cap) 5727 return -EINVAL; 5728 5729 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd)) 5730 return -EINVAL; 5731 5732 return 512 << FIELD_GET(PCI_X_CMD_MAX_READ, cmd); 5733 } 5734 EXPORT_SYMBOL(pcix_get_mmrbc); 5735 5736 /** 5737 * pcix_set_mmrbc - set PCI-X maximum memory read byte count 5738 * @dev: PCI device to query 5739 * @mmrbc: maximum memory read count in bytes 5740 * valid values are 512, 1024, 2048, 4096 5741 * 5742 * If possible sets maximum memory read byte count, some bridges have errata 5743 * that prevent this. 5744 */ 5745 int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc) 5746 { 5747 int cap; 5748 u32 stat, v, o; 5749 u16 cmd; 5750 5751 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc)) 5752 return -EINVAL; 5753 5754 v = ffs(mmrbc) - 10; 5755 5756 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); 5757 if (!cap) 5758 return -EINVAL; 5759 5760 if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat)) 5761 return -EINVAL; 5762 5763 if (v > FIELD_GET(PCI_X_STATUS_MAX_READ, stat)) 5764 return -E2BIG; 5765 5766 if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd)) 5767 return -EINVAL; 5768 5769 o = FIELD_GET(PCI_X_CMD_MAX_READ, cmd); 5770 if (o != v) { 5771 if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC)) 5772 return -EIO; 5773 5774 cmd &= ~PCI_X_CMD_MAX_READ; 5775 cmd |= FIELD_PREP(PCI_X_CMD_MAX_READ, v); 5776 if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd)) 5777 return -EIO; 5778 } 5779 return 0; 5780 } 5781 EXPORT_SYMBOL(pcix_set_mmrbc); 5782 5783 /** 5784 * pcie_get_readrq - get PCI Express read request size 5785 * @dev: PCI device to query 5786 * 5787 * Returns maximum memory read request in bytes or appropriate error value. 5788 */ 5789 int pcie_get_readrq(struct pci_dev *dev) 5790 { 5791 u16 ctl; 5792 5793 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl); 5794 5795 return 128 << FIELD_GET(PCI_EXP_DEVCTL_READRQ, ctl); 5796 } 5797 EXPORT_SYMBOL(pcie_get_readrq); 5798 5799 /** 5800 * pcie_set_readrq - set PCI Express maximum memory read request 5801 * @dev: PCI device to query 5802 * @rq: maximum memory read count in bytes 5803 * valid values are 128, 256, 512, 1024, 2048, 4096 5804 * 5805 * If possible sets maximum memory read request in bytes 5806 */ 5807 int pcie_set_readrq(struct pci_dev *dev, int rq) 5808 { 5809 u16 v; 5810 int ret; 5811 unsigned int firstbit; 5812 struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus); 5813 5814 if (rq < 128 || rq > 4096 || !is_power_of_2(rq)) 5815 return -EINVAL; 5816 5817 /* 5818 * If using the "performance" PCIe config, we clamp the read rq 5819 * size to the max packet size to keep the host bridge from 5820 * generating requests larger than we can cope with. 5821 */ 5822 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) { 5823 int mps = pcie_get_mps(dev); 5824 5825 if (mps < rq) 5826 rq = mps; 5827 } 5828 5829 firstbit = ffs(rq); 5830 if (firstbit < 8) 5831 return -EINVAL; 5832 v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, firstbit - 8); 5833 5834 if (bridge->no_inc_mrrs) { 5835 int max_mrrs = pcie_get_readrq(dev); 5836 5837 if (rq > max_mrrs) { 5838 pci_info(dev, "can't set Max_Read_Request_Size to %d; max is %d\n", rq, max_mrrs); 5839 return -EINVAL; 5840 } 5841 } 5842 5843 ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL, 5844 PCI_EXP_DEVCTL_READRQ, v); 5845 5846 return pcibios_err_to_errno(ret); 5847 } 5848 EXPORT_SYMBOL(pcie_set_readrq); 5849 5850 /** 5851 * pcie_get_mps - get PCI Express maximum payload size 5852 * @dev: PCI device to query 5853 * 5854 * Returns maximum payload size in bytes 5855 */ 5856 int pcie_get_mps(struct pci_dev *dev) 5857 { 5858 u16 ctl; 5859 5860 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl); 5861 5862 return 128 << FIELD_GET(PCI_EXP_DEVCTL_PAYLOAD, ctl); 5863 } 5864 EXPORT_SYMBOL(pcie_get_mps); 5865 5866 /** 5867 * pcie_set_mps - set PCI Express maximum payload size 5868 * @dev: PCI device to query 5869 * @mps: maximum payload size in bytes 5870 * valid values are 128, 256, 512, 1024, 2048, 4096 5871 * 5872 * If possible sets maximum payload size 5873 */ 5874 int pcie_set_mps(struct pci_dev *dev, int mps) 5875 { 5876 u16 v; 5877 int ret; 5878 5879 if (mps < 128 || mps > 4096 || !is_power_of_2(mps)) 5880 return -EINVAL; 5881 5882 v = ffs(mps) - 8; 5883 if (v > dev->pcie_mpss) 5884 return -EINVAL; 5885 v = FIELD_PREP(PCI_EXP_DEVCTL_PAYLOAD, v); 5886 5887 ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL, 5888 PCI_EXP_DEVCTL_PAYLOAD, v); 5889 5890 return pcibios_err_to_errno(ret); 5891 } 5892 EXPORT_SYMBOL(pcie_set_mps); 5893 5894 static enum pci_bus_speed to_pcie_link_speed(u16 lnksta) 5895 { 5896 return pcie_link_speed[FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta)]; 5897 } 5898 5899 int pcie_link_speed_mbps(struct pci_dev *pdev) 5900 { 5901 u16 lnksta; 5902 int err; 5903 5904 err = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta); 5905 if (err) 5906 return err; 5907 5908 return pcie_dev_speed_mbps(to_pcie_link_speed(lnksta)); 5909 } 5910 EXPORT_SYMBOL(pcie_link_speed_mbps); 5911 5912 /** 5913 * pcie_bandwidth_available - determine minimum link settings of a PCIe 5914 * device and its bandwidth limitation 5915 * @dev: PCI device to query 5916 * @limiting_dev: storage for device causing the bandwidth limitation 5917 * @speed: storage for speed of limiting device 5918 * @width: storage for width of limiting device 5919 * 5920 * Walk up the PCI device chain and find the point where the minimum 5921 * bandwidth is available. Return the bandwidth available there and (if 5922 * limiting_dev, speed, and width pointers are supplied) information about 5923 * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of 5924 * raw bandwidth. 5925 */ 5926 u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev, 5927 enum pci_bus_speed *speed, 5928 enum pcie_link_width *width) 5929 { 5930 u16 lnksta; 5931 enum pci_bus_speed next_speed; 5932 enum pcie_link_width next_width; 5933 u32 bw, next_bw; 5934 5935 if (speed) 5936 *speed = PCI_SPEED_UNKNOWN; 5937 if (width) 5938 *width = PCIE_LNK_WIDTH_UNKNOWN; 5939 5940 bw = 0; 5941 5942 while (dev) { 5943 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta); 5944 5945 next_speed = to_pcie_link_speed(lnksta); 5946 next_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta); 5947 5948 next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed); 5949 5950 /* Check if current device limits the total bandwidth */ 5951 if (!bw || next_bw <= bw) { 5952 bw = next_bw; 5953 5954 if (limiting_dev) 5955 *limiting_dev = dev; 5956 if (speed) 5957 *speed = next_speed; 5958 if (width) 5959 *width = next_width; 5960 } 5961 5962 dev = pci_upstream_bridge(dev); 5963 } 5964 5965 return bw; 5966 } 5967 EXPORT_SYMBOL(pcie_bandwidth_available); 5968 5969 /** 5970 * pcie_get_supported_speeds - query Supported Link Speed Vector 5971 * @dev: PCI device to query 5972 * 5973 * Query @dev supported link speeds. 5974 * 5975 * Implementation Note in PCIe r6.0 sec 7.5.3.18 recommends determining 5976 * supported link speeds using the Supported Link Speeds Vector in the Link 5977 * Capabilities 2 Register (when available). 5978 * 5979 * Link Capabilities 2 was added in PCIe r3.0, sec 7.8.18. 5980 * 5981 * Without Link Capabilities 2, i.e., prior to PCIe r3.0, Supported Link 5982 * Speeds field in Link Capabilities is used and only 2.5 GT/s and 5.0 GT/s 5983 * speeds were defined. 5984 * 5985 * For @dev without Supported Link Speed Vector, the field is synthesized 5986 * from the Max Link Speed field in the Link Capabilities Register. 5987 * 5988 * Return: Supported Link Speeds Vector (+ reserved 0 at LSB). 5989 */ 5990 u8 pcie_get_supported_speeds(struct pci_dev *dev) 5991 { 5992 u32 lnkcap2, lnkcap; 5993 u8 speeds; 5994 5995 /* 5996 * Speeds retain the reserved 0 at LSB before PCIe Supported Link 5997 * Speeds Vector to allow using SLS Vector bit defines directly. 5998 */ 5999 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2); 6000 speeds = lnkcap2 & PCI_EXP_LNKCAP2_SLS; 6001 6002 /* Ignore speeds higher than Max Link Speed */ 6003 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap); 6004 speeds &= GENMASK(lnkcap & PCI_EXP_LNKCAP_SLS, 0); 6005 6006 /* PCIe r3.0-compliant */ 6007 if (speeds) 6008 return speeds; 6009 6010 /* Synthesize from the Max Link Speed field */ 6011 if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB) 6012 speeds = PCI_EXP_LNKCAP2_SLS_5_0GB | PCI_EXP_LNKCAP2_SLS_2_5GB; 6013 else if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_2_5GB) 6014 speeds = PCI_EXP_LNKCAP2_SLS_2_5GB; 6015 6016 return speeds; 6017 } 6018 6019 /** 6020 * pcie_get_speed_cap - query for the PCI device's link speed capability 6021 * @dev: PCI device to query 6022 * 6023 * Query the PCI device speed capability. 6024 * 6025 * Return: the maximum link speed supported by the device. 6026 */ 6027 enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev) 6028 { 6029 return PCIE_LNKCAP2_SLS2SPEED(dev->supported_speeds); 6030 } 6031 EXPORT_SYMBOL(pcie_get_speed_cap); 6032 6033 /** 6034 * pcie_get_width_cap - query for the PCI device's link width capability 6035 * @dev: PCI device to query 6036 * 6037 * Query the PCI device width capability. Return the maximum link width 6038 * supported by the device. 6039 */ 6040 enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev) 6041 { 6042 u32 lnkcap; 6043 6044 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap); 6045 if (lnkcap) 6046 return FIELD_GET(PCI_EXP_LNKCAP_MLW, lnkcap); 6047 6048 return PCIE_LNK_WIDTH_UNKNOWN; 6049 } 6050 EXPORT_SYMBOL(pcie_get_width_cap); 6051 6052 /** 6053 * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability 6054 * @dev: PCI device 6055 * @speed: storage for link speed 6056 * @width: storage for link width 6057 * 6058 * Calculate a PCI device's link bandwidth by querying for its link speed 6059 * and width, multiplying them, and applying encoding overhead. The result 6060 * is in Mb/s, i.e., megabits/second of raw bandwidth. 6061 */ 6062 static u32 pcie_bandwidth_capable(struct pci_dev *dev, 6063 enum pci_bus_speed *speed, 6064 enum pcie_link_width *width) 6065 { 6066 *speed = pcie_get_speed_cap(dev); 6067 *width = pcie_get_width_cap(dev); 6068 6069 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN) 6070 return 0; 6071 6072 return *width * PCIE_SPEED2MBS_ENC(*speed); 6073 } 6074 6075 /** 6076 * __pcie_print_link_status - Report the PCI device's link speed and width 6077 * @dev: PCI device to query 6078 * @verbose: Print info even when enough bandwidth is available 6079 * 6080 * If the available bandwidth at the device is less than the device is 6081 * capable of, report the device's maximum possible bandwidth and the 6082 * upstream link that limits its performance. If @verbose, always print 6083 * the available bandwidth, even if the device isn't constrained. 6084 */ 6085 void __pcie_print_link_status(struct pci_dev *dev, bool verbose) 6086 { 6087 enum pcie_link_width width, width_cap; 6088 enum pci_bus_speed speed, speed_cap; 6089 struct pci_dev *limiting_dev = NULL; 6090 u32 bw_avail, bw_cap; 6091 char *flit_mode = ""; 6092 6093 bw_cap = pcie_bandwidth_capable(dev, &speed_cap, &width_cap); 6094 bw_avail = pcie_bandwidth_available(dev, &limiting_dev, &speed, &width); 6095 6096 if (dev->bus && dev->bus->flit_mode) 6097 flit_mode = ", in Flit mode"; 6098 6099 if (bw_avail >= bw_cap && verbose) 6100 pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth (%s x%d link)%s\n", 6101 bw_cap / 1000, bw_cap % 1000, 6102 pci_speed_string(speed_cap), width_cap, flit_mode); 6103 else if (bw_avail < bw_cap) 6104 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", 6105 bw_avail / 1000, bw_avail % 1000, 6106 pci_speed_string(speed), width, 6107 limiting_dev ? pci_name(limiting_dev) : "<unknown>", 6108 bw_cap / 1000, bw_cap % 1000, 6109 pci_speed_string(speed_cap), width_cap, flit_mode); 6110 } 6111 6112 /** 6113 * pcie_print_link_status - Report the PCI device's link speed and width 6114 * @dev: PCI device to query 6115 * 6116 * Report the available bandwidth at the device. 6117 */ 6118 void pcie_print_link_status(struct pci_dev *dev) 6119 { 6120 __pcie_print_link_status(dev, true); 6121 } 6122 EXPORT_SYMBOL(pcie_print_link_status); 6123 6124 /** 6125 * pci_select_bars - Make BAR mask from the type of resource 6126 * @dev: the PCI device for which BAR mask is made 6127 * @flags: resource type mask to be selected 6128 * 6129 * This helper routine makes bar mask from the type of resource. 6130 */ 6131 int pci_select_bars(struct pci_dev *dev, unsigned long flags) 6132 { 6133 int i, bars = 0; 6134 for (i = 0; i < PCI_NUM_RESOURCES; i++) 6135 if (pci_resource_flags(dev, i) & flags) 6136 bars |= (1 << i); 6137 return bars; 6138 } 6139 EXPORT_SYMBOL(pci_select_bars); 6140 6141 /* Some architectures require additional programming to enable VGA */ 6142 static arch_set_vga_state_t arch_set_vga_state; 6143 6144 void __init pci_register_set_vga_state(arch_set_vga_state_t func) 6145 { 6146 arch_set_vga_state = func; /* NULL disables */ 6147 } 6148 6149 static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode, 6150 unsigned int command_bits, u32 flags) 6151 { 6152 if (arch_set_vga_state) 6153 return arch_set_vga_state(dev, decode, command_bits, 6154 flags); 6155 return 0; 6156 } 6157 6158 /** 6159 * pci_set_vga_state - set VGA decode state on device and parents if requested 6160 * @dev: the PCI device 6161 * @decode: true = enable decoding, false = disable decoding 6162 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY 6163 * @flags: traverse ancestors and change bridges 6164 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE 6165 */ 6166 int pci_set_vga_state(struct pci_dev *dev, bool decode, 6167 unsigned int command_bits, u32 flags) 6168 { 6169 struct pci_bus *bus; 6170 struct pci_dev *bridge; 6171 u16 cmd; 6172 int rc; 6173 6174 WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY))); 6175 6176 /* ARCH specific VGA enables */ 6177 rc = pci_set_vga_state_arch(dev, decode, command_bits, flags); 6178 if (rc) 6179 return rc; 6180 6181 if (flags & PCI_VGA_STATE_CHANGE_DECODES) { 6182 pci_read_config_word(dev, PCI_COMMAND, &cmd); 6183 if (decode) 6184 cmd |= command_bits; 6185 else 6186 cmd &= ~command_bits; 6187 pci_write_config_word(dev, PCI_COMMAND, cmd); 6188 } 6189 6190 if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE)) 6191 return 0; 6192 6193 bus = dev->bus; 6194 while (bus) { 6195 bridge = bus->self; 6196 if (bridge) { 6197 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, 6198 &cmd); 6199 if (decode) 6200 cmd |= PCI_BRIDGE_CTL_VGA; 6201 else 6202 cmd &= ~PCI_BRIDGE_CTL_VGA; 6203 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, 6204 cmd); 6205 6206 6207 /* 6208 * VGA Enable may not be writable if bridge doesn't 6209 * support it. 6210 */ 6211 if (decode) { 6212 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, 6213 &cmd); 6214 if (!(cmd & PCI_BRIDGE_CTL_VGA)) 6215 return -EIO; 6216 } 6217 } 6218 bus = bus->parent; 6219 } 6220 return 0; 6221 } 6222 6223 #ifdef CONFIG_ACPI 6224 bool pci_pr3_present(struct pci_dev *pdev) 6225 { 6226 struct acpi_device *adev; 6227 6228 if (acpi_disabled) 6229 return false; 6230 6231 adev = ACPI_COMPANION(&pdev->dev); 6232 if (!adev) 6233 return false; 6234 6235 return adev->power.flags.power_resources && 6236 acpi_has_method(adev->handle, "_PR3"); 6237 } 6238 EXPORT_SYMBOL_GPL(pci_pr3_present); 6239 #endif 6240 6241 /** 6242 * pci_add_dma_alias - Add a DMA devfn alias for a device 6243 * @dev: the PCI device for which alias is added 6244 * @devfn_from: alias slot and function 6245 * @nr_devfns: number of subsequent devfns to alias 6246 * 6247 * This helper encodes an 8-bit devfn as a bit number in dma_alias_mask 6248 * which is used to program permissible bus-devfn source addresses for DMA 6249 * requests in an IOMMU. These aliases factor into IOMMU group creation 6250 * and are useful for devices generating DMA requests beyond or different 6251 * from their logical bus-devfn. Examples include device quirks where the 6252 * device simply uses the wrong devfn, as well as non-transparent bridges 6253 * where the alias may be a proxy for devices in another domain. 6254 * 6255 * IOMMU group creation is performed during device discovery or addition, 6256 * prior to any potential DMA mapping and therefore prior to driver probing 6257 * (especially for userspace assigned devices where IOMMU group definition 6258 * cannot be left as a userspace activity). DMA aliases should therefore 6259 * be configured via quirks, such as the PCI fixup header quirk. 6260 */ 6261 void pci_add_dma_alias(struct pci_dev *dev, u8 devfn_from, 6262 unsigned int nr_devfns) 6263 { 6264 int devfn_to; 6265 6266 nr_devfns = min(nr_devfns, (unsigned int)MAX_NR_DEVFNS - devfn_from); 6267 devfn_to = devfn_from + nr_devfns - 1; 6268 6269 if (!dev->dma_alias_mask) 6270 dev->dma_alias_mask = bitmap_zalloc(MAX_NR_DEVFNS, GFP_KERNEL); 6271 if (!dev->dma_alias_mask) { 6272 pci_warn(dev, "Unable to allocate DMA alias mask\n"); 6273 return; 6274 } 6275 6276 bitmap_set(dev->dma_alias_mask, devfn_from, nr_devfns); 6277 6278 if (nr_devfns == 1) 6279 pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n", 6280 PCI_SLOT(devfn_from), PCI_FUNC(devfn_from)); 6281 else if (nr_devfns > 1) 6282 pci_info(dev, "Enabling fixed DMA alias for devfn range from %02x.%d to %02x.%d\n", 6283 PCI_SLOT(devfn_from), PCI_FUNC(devfn_from), 6284 PCI_SLOT(devfn_to), PCI_FUNC(devfn_to)); 6285 } 6286 6287 bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2) 6288 { 6289 return (dev1->dma_alias_mask && 6290 test_bit(dev2->devfn, dev1->dma_alias_mask)) || 6291 (dev2->dma_alias_mask && 6292 test_bit(dev1->devfn, dev2->dma_alias_mask)) || 6293 pci_real_dma_dev(dev1) == dev2 || 6294 pci_real_dma_dev(dev2) == dev1; 6295 } 6296 6297 bool pci_device_is_present(struct pci_dev *pdev) 6298 { 6299 u32 v; 6300 6301 /* Check PF if pdev is a VF, since VF Vendor/Device IDs are 0xffff */ 6302 pdev = pci_physfn(pdev); 6303 if (pci_dev_is_disconnected(pdev)) 6304 return false; 6305 return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0); 6306 } 6307 EXPORT_SYMBOL_GPL(pci_device_is_present); 6308 6309 void pci_ignore_hotplug(struct pci_dev *dev) 6310 { 6311 struct pci_dev *bridge = dev->bus->self; 6312 6313 dev->ignore_hotplug = 1; 6314 /* Propagate the "ignore hotplug" setting to the parent bridge. */ 6315 if (bridge) 6316 bridge->ignore_hotplug = 1; 6317 } 6318 EXPORT_SYMBOL_GPL(pci_ignore_hotplug); 6319 6320 /** 6321 * pci_real_dma_dev - Get PCI DMA device for PCI device 6322 * @dev: the PCI device that may have a PCI DMA alias 6323 * 6324 * Permits the platform to provide architecture-specific functionality to 6325 * devices needing to alias DMA to another PCI device on another PCI bus. If 6326 * the PCI device is on the same bus, it is recommended to use 6327 * pci_add_dma_alias(). This is the default implementation. Architecture 6328 * implementations can override this. 6329 */ 6330 struct pci_dev __weak *pci_real_dma_dev(struct pci_dev *dev) 6331 { 6332 return dev; 6333 } 6334 6335 resource_size_t __weak pcibios_default_alignment(void) 6336 { 6337 return 0; 6338 } 6339 6340 /* 6341 * Arches that don't want to expose struct resource to userland as-is in 6342 * sysfs and /proc can implement their own pci_resource_to_user(). 6343 */ 6344 void __weak pci_resource_to_user(const struct pci_dev *dev, int bar, 6345 const struct resource *rsrc, 6346 resource_size_t *start, resource_size_t *end) 6347 { 6348 *start = rsrc->start; 6349 *end = rsrc->end; 6350 } 6351 6352 static char *resource_alignment_param; 6353 static DEFINE_SPINLOCK(resource_alignment_lock); 6354 6355 /** 6356 * pci_specified_resource_alignment - get resource alignment specified by user. 6357 * @dev: the PCI device to get 6358 * @resize: whether or not to change resources' size when reassigning alignment 6359 * 6360 * RETURNS: Resource alignment if it is specified. 6361 * Zero if it is not specified. 6362 */ 6363 static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev, 6364 bool *resize) 6365 { 6366 int align_order, count; 6367 resource_size_t align = pcibios_default_alignment(); 6368 const char *p; 6369 int ret; 6370 6371 spin_lock(&resource_alignment_lock); 6372 p = resource_alignment_param; 6373 if (!p || !*p) 6374 goto out; 6375 if (pci_has_flag(PCI_PROBE_ONLY)) { 6376 align = 0; 6377 pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n"); 6378 goto out; 6379 } 6380 6381 while (*p) { 6382 count = 0; 6383 if (sscanf(p, "%d%n", &align_order, &count) == 1 && 6384 p[count] == '@') { 6385 p += count + 1; 6386 if (align_order > 63) { 6387 pr_err("PCI: Invalid requested alignment (order %d)\n", 6388 align_order); 6389 align_order = PAGE_SHIFT; 6390 } 6391 } else { 6392 align_order = PAGE_SHIFT; 6393 } 6394 6395 ret = pci_dev_str_match(dev, p, &p); 6396 if (ret == 1) { 6397 *resize = true; 6398 align = 1ULL << align_order; 6399 break; 6400 } else if (ret < 0) { 6401 pr_err("PCI: Can't parse resource_alignment parameter: %s\n", 6402 p); 6403 break; 6404 } 6405 6406 if (*p != ';' && *p != ',') { 6407 /* End of param or invalid format */ 6408 break; 6409 } 6410 p++; 6411 } 6412 out: 6413 spin_unlock(&resource_alignment_lock); 6414 return align; 6415 } 6416 6417 static void pci_request_resource_alignment(struct pci_dev *dev, int bar, 6418 resource_size_t align, bool resize) 6419 { 6420 struct resource *r = &dev->resource[bar]; 6421 const char *r_name = pci_resource_name(dev, bar); 6422 resource_size_t size; 6423 6424 if (!(r->flags & IORESOURCE_MEM)) 6425 return; 6426 6427 if (r->flags & IORESOURCE_PCI_FIXED) { 6428 pci_info(dev, "%s %pR: ignoring requested alignment %#llx\n", 6429 r_name, r, (unsigned long long)align); 6430 return; 6431 } 6432 6433 size = resource_size(r); 6434 if (size >= align) 6435 return; 6436 6437 /* 6438 * Increase the alignment of the resource. There are two ways we 6439 * can do this: 6440 * 6441 * 1) Increase the size of the resource. BARs are aligned on their 6442 * size, so when we reallocate space for this resource, we'll 6443 * allocate it with the larger alignment. This also prevents 6444 * assignment of any other BARs inside the alignment region, so 6445 * if we're requesting page alignment, this means no other BARs 6446 * will share the page. 6447 * 6448 * The disadvantage is that this makes the resource larger than 6449 * the hardware BAR, which may break drivers that compute things 6450 * based on the resource size, e.g., to find registers at a 6451 * fixed offset before the end of the BAR. 6452 * 6453 * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and 6454 * set r->start to the desired alignment. By itself this 6455 * doesn't prevent other BARs being put inside the alignment 6456 * region, but if we realign *every* resource of every device in 6457 * the system, none of them will share an alignment region. 6458 * 6459 * When the user has requested alignment for only some devices via 6460 * the "pci=resource_alignment" argument, "resize" is true and we 6461 * use the first method. Otherwise we assume we're aligning all 6462 * devices and we use the second. 6463 */ 6464 6465 pci_info(dev, "%s %pR: requesting alignment to %#llx\n", 6466 r_name, r, (unsigned long long)align); 6467 6468 if (resize) { 6469 r->start = 0; 6470 r->end = align - 1; 6471 } else { 6472 r->flags &= ~IORESOURCE_SIZEALIGN; 6473 r->flags |= IORESOURCE_STARTALIGN; 6474 resource_set_range(r, align, size); 6475 } 6476 r->flags |= IORESOURCE_UNSET; 6477 } 6478 6479 /* 6480 * This function disables memory decoding and releases memory resources 6481 * of the device specified by kernel's boot parameter 'pci=resource_alignment='. 6482 * It also rounds up size to specified alignment. 6483 * Later on, the kernel will assign page-aligned memory resource back 6484 * to the device. 6485 */ 6486 void pci_reassigndev_resource_alignment(struct pci_dev *dev) 6487 { 6488 int i; 6489 struct resource *r; 6490 resource_size_t align; 6491 u16 command; 6492 bool resize = false; 6493 6494 /* 6495 * VF BARs are read-only zero according to SR-IOV spec r1.1, sec 6496 * 3.4.1.11. Their resources are allocated from the space 6497 * described by the VF BARx register in the PF's SR-IOV capability. 6498 * We can't influence their alignment here. 6499 */ 6500 if (dev->is_virtfn) 6501 return; 6502 6503 /* check if specified PCI is target device to reassign */ 6504 align = pci_specified_resource_alignment(dev, &resize); 6505 if (!align) 6506 return; 6507 6508 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL && 6509 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) { 6510 pci_warn(dev, "Can't reassign resources to host bridge\n"); 6511 return; 6512 } 6513 6514 pci_read_config_word(dev, PCI_COMMAND, &command); 6515 command &= ~PCI_COMMAND_MEMORY; 6516 pci_write_config_word(dev, PCI_COMMAND, command); 6517 6518 for (i = 0; i <= PCI_ROM_RESOURCE; i++) 6519 pci_request_resource_alignment(dev, i, align, resize); 6520 6521 /* 6522 * Need to disable bridge's resource window, 6523 * to enable the kernel to reassign new resource 6524 * window later on. 6525 */ 6526 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) { 6527 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) { 6528 r = &dev->resource[i]; 6529 if (!(r->flags & IORESOURCE_MEM)) 6530 continue; 6531 r->flags |= IORESOURCE_UNSET; 6532 r->end = resource_size(r) - 1; 6533 r->start = 0; 6534 } 6535 pci_disable_bridge_window(dev); 6536 } 6537 } 6538 6539 static ssize_t resource_alignment_show(const struct bus_type *bus, char *buf) 6540 { 6541 size_t count = 0; 6542 6543 spin_lock(&resource_alignment_lock); 6544 if (resource_alignment_param) 6545 count = sysfs_emit(buf, "%s\n", resource_alignment_param); 6546 spin_unlock(&resource_alignment_lock); 6547 6548 return count; 6549 } 6550 6551 static ssize_t resource_alignment_store(const struct bus_type *bus, 6552 const char *buf, size_t count) 6553 { 6554 char *param, *old, *end; 6555 6556 if (count >= (PAGE_SIZE - 1)) 6557 return -EINVAL; 6558 6559 param = kstrndup(buf, count, GFP_KERNEL); 6560 if (!param) 6561 return -ENOMEM; 6562 6563 end = strchr(param, '\n'); 6564 if (end) 6565 *end = '\0'; 6566 6567 spin_lock(&resource_alignment_lock); 6568 old = resource_alignment_param; 6569 if (strlen(param)) { 6570 resource_alignment_param = param; 6571 } else { 6572 kfree(param); 6573 resource_alignment_param = NULL; 6574 } 6575 spin_unlock(&resource_alignment_lock); 6576 6577 kfree(old); 6578 6579 return count; 6580 } 6581 6582 static BUS_ATTR_RW(resource_alignment); 6583 6584 static int __init pci_resource_alignment_sysfs_init(void) 6585 { 6586 return bus_create_file(&pci_bus_type, 6587 &bus_attr_resource_alignment); 6588 } 6589 late_initcall(pci_resource_alignment_sysfs_init); 6590 6591 static void pci_no_domains(void) 6592 { 6593 #ifdef CONFIG_PCI_DOMAINS 6594 pci_domains_supported = 0; 6595 #endif 6596 } 6597 6598 #ifdef CONFIG_PCI_DOMAINS 6599 static DEFINE_IDA(pci_domain_nr_dynamic_ida); 6600 6601 /** 6602 * pci_bus_find_emul_domain_nr() - allocate a PCI domain number per constraints 6603 * @hint: desired domain, 0 if any ID in the range of @min to @max is acceptable 6604 * @min: minimum allowable domain 6605 * @max: maximum allowable domain, no IDs higher than INT_MAX will be returned 6606 */ 6607 int pci_bus_find_emul_domain_nr(u32 hint, u32 min, u32 max) 6608 { 6609 return ida_alloc_range(&pci_domain_nr_dynamic_ida, max(hint, min), max, 6610 GFP_KERNEL); 6611 } 6612 EXPORT_SYMBOL_GPL(pci_bus_find_emul_domain_nr); 6613 6614 void pci_bus_release_emul_domain_nr(int domain_nr) 6615 { 6616 ida_free(&pci_domain_nr_dynamic_ida, domain_nr); 6617 } 6618 EXPORT_SYMBOL_GPL(pci_bus_release_emul_domain_nr); 6619 #endif 6620 6621 #ifdef CONFIG_PCI_DOMAINS_GENERIC 6622 static DEFINE_IDA(pci_domain_nr_static_ida); 6623 6624 static void of_pci_reserve_static_domain_nr(void) 6625 { 6626 struct device_node *np; 6627 int domain_nr; 6628 6629 for_each_node_by_type(np, "pci") { 6630 domain_nr = of_get_pci_domain_nr(np); 6631 if (domain_nr < 0) 6632 continue; 6633 /* 6634 * Permanently allocate domain_nr in dynamic_ida 6635 * to prevent it from dynamic allocation. 6636 */ 6637 ida_alloc_range(&pci_domain_nr_dynamic_ida, 6638 domain_nr, domain_nr, GFP_KERNEL); 6639 } 6640 } 6641 6642 static int of_pci_bus_find_domain_nr(struct device *parent) 6643 { 6644 static bool static_domains_reserved = false; 6645 int domain_nr; 6646 6647 /* On the first call scan device tree for static allocations. */ 6648 if (!static_domains_reserved) { 6649 of_pci_reserve_static_domain_nr(); 6650 static_domains_reserved = true; 6651 } 6652 6653 if (parent) { 6654 /* 6655 * If domain is in DT, allocate it in static IDA. This 6656 * prevents duplicate static allocations in case of errors 6657 * in DT. 6658 */ 6659 domain_nr = of_get_pci_domain_nr(parent->of_node); 6660 if (domain_nr >= 0) 6661 return ida_alloc_range(&pci_domain_nr_static_ida, 6662 domain_nr, domain_nr, 6663 GFP_KERNEL); 6664 } 6665 6666 /* 6667 * If domain was not specified in DT, choose a free ID from dynamic 6668 * allocations. All domain numbers from DT are permanently in 6669 * dynamic allocations to prevent assigning them to other DT nodes 6670 * without static domain. 6671 */ 6672 return ida_alloc(&pci_domain_nr_dynamic_ida, GFP_KERNEL); 6673 } 6674 6675 static void of_pci_bus_release_domain_nr(struct device *parent, int domain_nr) 6676 { 6677 if (domain_nr < 0) 6678 return; 6679 6680 /* Release domain from IDA where it was allocated. */ 6681 if (parent && of_get_pci_domain_nr(parent->of_node) == domain_nr) 6682 ida_free(&pci_domain_nr_static_ida, domain_nr); 6683 else 6684 ida_free(&pci_domain_nr_dynamic_ida, domain_nr); 6685 } 6686 6687 int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent) 6688 { 6689 return acpi_disabled ? of_pci_bus_find_domain_nr(parent) : 6690 acpi_pci_bus_find_domain_nr(bus); 6691 } 6692 6693 void pci_bus_release_domain_nr(struct device *parent, int domain_nr) 6694 { 6695 if (!acpi_disabled) 6696 return; 6697 of_pci_bus_release_domain_nr(parent, domain_nr); 6698 } 6699 #endif 6700 6701 /** 6702 * pci_ext_cfg_avail - can we access extended PCI config space? 6703 * 6704 * Returns 1 if we can access PCI extended config space (offsets 6705 * greater than 0xff). This is the default implementation. Architecture 6706 * implementations can override this. 6707 */ 6708 int __weak pci_ext_cfg_avail(void) 6709 { 6710 return 1; 6711 } 6712 6713 static int __init pci_setup(char *str) 6714 { 6715 while (str) { 6716 char *k = strchr(str, ','); 6717 if (k) 6718 *k++ = 0; 6719 if (*str && (str = pcibios_setup(str)) && *str) { 6720 if (!pci_setup_cardbus(str)) { 6721 /* Function handled the parameters */ 6722 } else if (!strcmp(str, "nomsi")) { 6723 pci_no_msi(); 6724 } else if (!strncmp(str, "noats", 5)) { 6725 pr_info("PCIe: ATS is disabled\n"); 6726 pcie_ats_disabled = true; 6727 } else if (!strcmp(str, "noaer")) { 6728 pci_no_aer(); 6729 } else if (!strcmp(str, "earlydump")) { 6730 pci_early_dump = true; 6731 } else if (!strncmp(str, "realloc=", 8)) { 6732 pci_realloc_get_opt(str + 8); 6733 } else if (!strncmp(str, "realloc", 7)) { 6734 pci_realloc_get_opt("on"); 6735 } else if (!strcmp(str, "nodomains")) { 6736 pci_no_domains(); 6737 } else if (!strncmp(str, "noari", 5)) { 6738 pcie_ari_disabled = true; 6739 } else if (!strncmp(str, "notph", 5)) { 6740 pci_no_tph(); 6741 } else if (!strncmp(str, "resource_alignment=", 19)) { 6742 resource_alignment_param = str + 19; 6743 } else if (!strncmp(str, "ecrc=", 5)) { 6744 pcie_ecrc_get_policy(str + 5); 6745 } else if (!strncmp(str, "hpiosize=", 9)) { 6746 pci_hotplug_io_size = memparse(str + 9, &str); 6747 } else if (!strncmp(str, "hpmmiosize=", 11)) { 6748 pci_hotplug_mmio_size = memparse(str + 11, &str); 6749 } else if (!strncmp(str, "hpmmioprefsize=", 15)) { 6750 pci_hotplug_mmio_pref_size = memparse(str + 15, &str); 6751 } else if (!strncmp(str, "hpmemsize=", 10)) { 6752 pci_hotplug_mmio_size = memparse(str + 10, &str); 6753 pci_hotplug_mmio_pref_size = pci_hotplug_mmio_size; 6754 } else if (!strncmp(str, "hpbussize=", 10)) { 6755 pci_hotplug_bus_size = 6756 simple_strtoul(str + 10, &str, 0); 6757 if (pci_hotplug_bus_size > 0xff) 6758 pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE; 6759 } else if (!strncmp(str, "pcie_bus_tune_off", 17)) { 6760 pcie_bus_config = PCIE_BUS_TUNE_OFF; 6761 } else if (!strncmp(str, "pcie_bus_safe", 13)) { 6762 pcie_bus_config = PCIE_BUS_SAFE; 6763 } else if (!strncmp(str, "pcie_bus_perf", 13)) { 6764 pcie_bus_config = PCIE_BUS_PERFORMANCE; 6765 } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) { 6766 pcie_bus_config = PCIE_BUS_PEER2PEER; 6767 } else if (!strncmp(str, "pcie_scan_all", 13)) { 6768 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS); 6769 } else if (!strncmp(str, "disable_acs_redir=", 18)) { 6770 disable_acs_redir_param = str + 18; 6771 } else if (!strncmp(str, "config_acs=", 11)) { 6772 config_acs_param = str + 11; 6773 } else { 6774 pr_err("PCI: Unknown option `%s'\n", str); 6775 } 6776 } 6777 str = k; 6778 } 6779 return 0; 6780 } 6781 early_param("pci", pci_setup); 6782 6783 /* 6784 * 'resource_alignment_param' and 'disable_acs_redir_param' are initialized 6785 * in pci_setup(), above, to point to data in the __initdata section which 6786 * will be freed after the init sequence is complete. We can't allocate memory 6787 * in pci_setup() because some architectures do not have any memory allocation 6788 * service available during an early_param() call. So we allocate memory and 6789 * copy the variable here before the init section is freed. 6790 * 6791 */ 6792 static int __init pci_realloc_setup_params(void) 6793 { 6794 resource_alignment_param = kstrdup(resource_alignment_param, 6795 GFP_KERNEL); 6796 disable_acs_redir_param = kstrdup(disable_acs_redir_param, GFP_KERNEL); 6797 config_acs_param = kstrdup(config_acs_param, GFP_KERNEL); 6798 6799 return 0; 6800 } 6801 pure_initcall(pci_realloc_setup_params); 6802