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