1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * PCI detection and setup code 4 */ 5 6 #include <linux/array_size.h> 7 #include <linux/kernel.h> 8 #include <linux/delay.h> 9 #include <linux/init.h> 10 #include <linux/pci.h> 11 #include <linux/msi.h> 12 #include <linux/of_pci.h> 13 #include <linux/of_platform.h> 14 #include <linux/platform_device.h> 15 #include <linux/pci_hotplug.h> 16 #include <linux/slab.h> 17 #include <linux/sprintf.h> 18 #include <linux/module.h> 19 #include <linux/cpumask.h> 20 #include <linux/aer.h> 21 #include <linux/acpi.h> 22 #include <linux/hypervisor.h> 23 #include <linux/irqdomain.h> 24 #include <linux/pm_runtime.h> 25 #include <linux/bitfield.h> 26 #include <trace/events/pci.h> 27 #include "pci.h" 28 29 static struct resource busn_resource = { 30 .name = "PCI busn", 31 .start = 0, 32 .end = 255, 33 .flags = IORESOURCE_BUS, 34 }; 35 36 /* Ugh. Need to stop exporting this to modules. */ 37 LIST_HEAD(pci_root_buses); 38 EXPORT_SYMBOL(pci_root_buses); 39 40 static LIST_HEAD(pci_domain_busn_res_list); 41 42 struct pci_domain_busn_res { 43 struct list_head list; 44 struct resource res; 45 int domain_nr; 46 }; 47 48 static struct resource *get_pci_domain_busn_res(int domain_nr) 49 { 50 struct pci_domain_busn_res *r; 51 52 list_for_each_entry(r, &pci_domain_busn_res_list, list) 53 if (r->domain_nr == domain_nr) 54 return &r->res; 55 56 r = kzalloc(sizeof(*r), GFP_KERNEL); 57 if (!r) 58 return NULL; 59 60 r->domain_nr = domain_nr; 61 r->res.start = 0; 62 r->res.end = 0xff; 63 r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED; 64 65 list_add_tail(&r->list, &pci_domain_busn_res_list); 66 67 return &r->res; 68 } 69 70 /* 71 * Some device drivers need know if PCI is initiated. 72 * Basically, we think PCI is not initiated when there 73 * is no device to be found on the pci_bus_type. 74 */ 75 int no_pci_devices(void) 76 { 77 struct device *dev; 78 int no_devices; 79 80 dev = bus_find_next_device(&pci_bus_type, NULL); 81 no_devices = (dev == NULL); 82 put_device(dev); 83 return no_devices; 84 } 85 EXPORT_SYMBOL(no_pci_devices); 86 87 /* 88 * PCI Bus Class 89 */ 90 static void release_pcibus_dev(struct device *dev) 91 { 92 struct pci_bus *pci_bus = to_pci_bus(dev); 93 94 put_device(pci_bus->bridge); 95 pci_bus_remove_resources(pci_bus); 96 pci_release_bus_of_node(pci_bus); 97 kfree(pci_bus); 98 } 99 100 static const struct class pcibus_class = { 101 .name = "pci_bus", 102 .dev_release = &release_pcibus_dev, 103 .dev_groups = pcibus_groups, 104 }; 105 106 static int __init pcibus_class_init(void) 107 { 108 return class_register(&pcibus_class); 109 } 110 postcore_initcall(pcibus_class_init); 111 112 static u64 pci_size(u64 base, u64 maxbase, u64 mask) 113 { 114 u64 size = mask & maxbase; /* Find the significant bits */ 115 if (!size) 116 return 0; 117 118 /* 119 * Get the lowest of them to find the decode size, and from that 120 * the extent. 121 */ 122 size = size & ~(size-1); 123 124 /* 125 * base == maxbase can be valid only if the BAR has already been 126 * programmed with all 1s. 127 */ 128 if (base == maxbase && ((base | (size - 1)) & mask) != mask) 129 return 0; 130 131 return size; 132 } 133 134 static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar) 135 { 136 u32 mem_type; 137 unsigned long flags; 138 139 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) { 140 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK; 141 flags |= IORESOURCE_IO; 142 return flags; 143 } 144 145 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK; 146 flags |= IORESOURCE_MEM; 147 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH) 148 flags |= IORESOURCE_PREFETCH; 149 150 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK; 151 switch (mem_type) { 152 case PCI_BASE_ADDRESS_MEM_TYPE_32: 153 break; 154 case PCI_BASE_ADDRESS_MEM_TYPE_1M: 155 /* 1M mem BAR treated as 32-bit BAR */ 156 break; 157 case PCI_BASE_ADDRESS_MEM_TYPE_64: 158 flags |= IORESOURCE_MEM_64; 159 break; 160 default: 161 /* mem unknown type treated as 32-bit BAR */ 162 break; 163 } 164 return flags; 165 } 166 167 #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO) 168 169 /** 170 * __pci_size_bars - Read the raw BAR mask for a range of PCI BARs 171 * @dev: the PCI device 172 * @count: number of BARs to size 173 * @pos: starting config space position 174 * @sizes: array to store mask values 175 * @rom: indicate whether to use ROM mask, which avoids enabling ROM BARs 176 * 177 * Provided @sizes array must be sufficiently sized to store results for 178 * @count u32 BARs. Caller is responsible for disabling decode to specified 179 * BAR range around calling this function. This function is intended to avoid 180 * disabling decode around sizing each BAR individually, which can result in 181 * non-trivial overhead in virtualized environments with very large PCI BARs. 182 */ 183 static void __pci_size_bars(struct pci_dev *dev, int count, 184 unsigned int pos, u32 *sizes, bool rom) 185 { 186 u32 orig, mask = rom ? PCI_ROM_ADDRESS_MASK : ~0; 187 int i; 188 189 for (i = 0; i < count; i++, pos += 4, sizes++) { 190 pci_read_config_dword(dev, pos, &orig); 191 pci_write_config_dword(dev, pos, mask); 192 pci_read_config_dword(dev, pos, sizes); 193 pci_write_config_dword(dev, pos, orig); 194 } 195 } 196 197 void __pci_size_stdbars(struct pci_dev *dev, int count, 198 unsigned int pos, u32 *sizes) 199 { 200 __pci_size_bars(dev, count, pos, sizes, false); 201 } 202 203 static void __pci_size_rom(struct pci_dev *dev, unsigned int pos, u32 *sizes) 204 { 205 __pci_size_bars(dev, 1, pos, sizes, true); 206 } 207 208 /** 209 * __pci_read_base - Read a PCI BAR 210 * @dev: the PCI device 211 * @type: type of the BAR 212 * @res: resource buffer to be filled in 213 * @pos: BAR position in the config space 214 * @sizes: array of one or more pre-read BAR masks 215 * 216 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit. 217 */ 218 int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, 219 struct resource *res, unsigned int pos, u32 *sizes) 220 { 221 u32 l = 0, sz; 222 u64 l64, sz64, mask64; 223 struct pci_bus_region region, inverted_region; 224 const char *res_name = pci_resource_name(dev, res - dev->resource); 225 226 res->name = pci_name(dev); 227 228 pci_read_config_dword(dev, pos, &l); 229 sz = sizes[0]; 230 231 /* 232 * All bits set in sz means the device isn't working properly. 233 * If the BAR isn't implemented, all bits must be 0. If it's a 234 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit 235 * 1 must be clear. 236 */ 237 if (PCI_POSSIBLE_ERROR(sz)) 238 sz = 0; 239 240 /* 241 * I don't know how l can have all bits set. Copied from old code. 242 * Maybe it fixes a bug on some ancient platform. 243 */ 244 if (PCI_POSSIBLE_ERROR(l)) 245 l = 0; 246 247 if (type == pci_bar_unknown) { 248 res->flags = decode_bar(dev, l); 249 res->flags |= IORESOURCE_SIZEALIGN; 250 if (res->flags & IORESOURCE_IO) { 251 l64 = l & PCI_BASE_ADDRESS_IO_MASK; 252 sz64 = sz & PCI_BASE_ADDRESS_IO_MASK; 253 mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT; 254 } else { 255 l64 = l & PCI_BASE_ADDRESS_MEM_MASK; 256 sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK; 257 mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK; 258 } 259 } else { 260 if (l & PCI_ROM_ADDRESS_ENABLE) 261 res->flags |= IORESOURCE_ROM_ENABLE; 262 l64 = l & PCI_ROM_ADDRESS_MASK; 263 sz64 = sz & PCI_ROM_ADDRESS_MASK; 264 mask64 = PCI_ROM_ADDRESS_MASK; 265 } 266 267 if (res->flags & IORESOURCE_MEM_64) { 268 pci_read_config_dword(dev, pos + 4, &l); 269 sz = sizes[1]; 270 271 l64 |= ((u64)l << 32); 272 sz64 |= ((u64)sz << 32); 273 mask64 |= ((u64)~0 << 32); 274 } 275 276 if (!sz64) 277 goto fail; 278 279 sz64 = pci_size(l64, sz64, mask64); 280 if (!sz64) { 281 pci_info(dev, FW_BUG "%s: invalid; can't size\n", res_name); 282 goto fail; 283 } 284 285 if (res->flags & IORESOURCE_MEM_64) { 286 if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8) 287 && sz64 > 0x100000000ULL) { 288 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; 289 resource_set_range(res, 0, 0); 290 pci_err(dev, "%s: can't handle BAR larger than 4GB (size %#010llx)\n", 291 res_name, (unsigned long long)sz64); 292 goto out; 293 } 294 295 if ((sizeof(pci_bus_addr_t) < 8) && l) { 296 /* Above 32-bit boundary; try to reallocate */ 297 res->flags |= IORESOURCE_UNSET; 298 resource_set_range(res, 0, sz64); 299 pci_info(dev, "%s: can't handle BAR above 4GB (bus address %#010llx)\n", 300 res_name, (unsigned long long)l64); 301 goto out; 302 } 303 } 304 305 region.start = l64; 306 region.end = l64 + sz64 - 1; 307 308 pcibios_bus_to_resource(dev->bus, res, ®ion); 309 pcibios_resource_to_bus(dev->bus, &inverted_region, res); 310 311 /* 312 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is 313 * the corresponding resource address (the physical address used by 314 * the CPU. Converting that resource address back to a bus address 315 * should yield the original BAR value: 316 * 317 * resource_to_bus(bus_to_resource(A)) == A 318 * 319 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not 320 * be claimed by the device. 321 */ 322 if (inverted_region.start != region.start) { 323 res->flags |= IORESOURCE_UNSET; 324 res->start = 0; 325 res->end = region.end - region.start; 326 pci_info(dev, "%s: initial BAR value %#010llx invalid\n", 327 res_name, (unsigned long long)region.start); 328 } 329 330 goto out; 331 332 333 fail: 334 res->flags = 0; 335 out: 336 if (res->flags) 337 pci_info(dev, "%s %pR\n", res_name, res); 338 339 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0; 340 } 341 342 static __always_inline void pci_read_bases(struct pci_dev *dev, 343 unsigned int howmany, int rom) 344 { 345 u32 rombar, stdbars[PCI_STD_NUM_BARS]; 346 unsigned int pos, reg; 347 u16 orig_cmd; 348 349 BUILD_BUG_ON(statically_true(howmany > PCI_STD_NUM_BARS)); 350 351 if (dev->non_compliant_bars) 352 return; 353 354 /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */ 355 if (dev->is_virtfn) 356 return; 357 358 /* No printks while decoding is disabled! */ 359 if (!dev->mmio_always_on) { 360 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd); 361 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) { 362 pci_write_config_word(dev, PCI_COMMAND, 363 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE); 364 } 365 } 366 367 __pci_size_stdbars(dev, howmany, PCI_BASE_ADDRESS_0, stdbars); 368 if (rom) 369 __pci_size_rom(dev, rom, &rombar); 370 371 if (!dev->mmio_always_on && 372 (orig_cmd & PCI_COMMAND_DECODE_ENABLE)) 373 pci_write_config_word(dev, PCI_COMMAND, orig_cmd); 374 375 for (pos = 0; pos < howmany; pos++) { 376 struct resource *res = &dev->resource[pos]; 377 reg = PCI_BASE_ADDRESS_0 + (pos << 2); 378 pos += __pci_read_base(dev, pci_bar_unknown, 379 res, reg, &stdbars[pos]); 380 } 381 382 if (rom) { 383 struct resource *res = &dev->resource[PCI_ROM_RESOURCE]; 384 dev->rom_base_reg = rom; 385 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | 386 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN; 387 __pci_read_base(dev, pci_bar_mem32, res, rom, &rombar); 388 } 389 } 390 391 static void pci_read_bridge_io(struct pci_dev *dev, struct resource *res, 392 bool log) 393 { 394 u8 io_base_lo, io_limit_lo; 395 unsigned long io_mask, io_granularity, base, limit; 396 struct pci_bus_region region; 397 398 io_mask = PCI_IO_RANGE_MASK; 399 io_granularity = 0x1000; 400 if (dev->io_window_1k) { 401 /* Support 1K I/O space granularity */ 402 io_mask = PCI_IO_1K_RANGE_MASK; 403 io_granularity = 0x400; 404 } 405 406 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); 407 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo); 408 base = (io_base_lo & io_mask) << 8; 409 limit = (io_limit_lo & io_mask) << 8; 410 411 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { 412 u16 io_base_hi, io_limit_hi; 413 414 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi); 415 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi); 416 base |= ((unsigned long) io_base_hi << 16); 417 limit |= ((unsigned long) io_limit_hi << 16); 418 } 419 420 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; 421 422 if (base <= limit) { 423 region.start = base; 424 region.end = limit + io_granularity - 1; 425 pcibios_bus_to_resource(dev->bus, res, ®ion); 426 if (log) 427 pci_info(dev, " bridge window %pR\n", res); 428 } else { 429 resource_set_range(res, 0, 0); 430 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; 431 } 432 } 433 434 static void pci_read_bridge_mmio(struct pci_dev *dev, struct resource *res, 435 bool log) 436 { 437 u16 mem_base_lo, mem_limit_lo; 438 unsigned long base, limit; 439 struct pci_bus_region region; 440 441 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo); 442 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo); 443 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16; 444 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; 445 446 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM; 447 448 if (base <= limit) { 449 region.start = base; 450 region.end = limit + 0xfffff; 451 pcibios_bus_to_resource(dev->bus, res, ®ion); 452 if (log) 453 pci_info(dev, " bridge window %pR\n", res); 454 } else { 455 resource_set_range(res, 0, 0); 456 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; 457 } 458 } 459 460 static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res, 461 bool log) 462 { 463 u16 mem_base_lo, mem_limit_lo; 464 u64 base64, limit64; 465 pci_bus_addr_t base, limit; 466 struct pci_bus_region region; 467 468 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo); 469 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo); 470 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16; 471 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16; 472 473 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { 474 u32 mem_base_hi, mem_limit_hi; 475 476 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi); 477 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi); 478 479 /* 480 * Some bridges set the base > limit by default, and some 481 * (broken) BIOSes do not initialize them. If we find 482 * this, just assume they are not being used. 483 */ 484 if (mem_base_hi <= mem_limit_hi) { 485 base64 |= (u64) mem_base_hi << 32; 486 limit64 |= (u64) mem_limit_hi << 32; 487 } 488 } 489 490 base = (pci_bus_addr_t) base64; 491 limit = (pci_bus_addr_t) limit64; 492 493 if (base != base64) { 494 pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n", 495 (unsigned long long) base64); 496 return; 497 } 498 499 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) | IORESOURCE_MEM | 500 IORESOURCE_PREFETCH; 501 if (res->flags & PCI_PREF_RANGE_TYPE_64) 502 res->flags |= IORESOURCE_MEM_64; 503 504 if (base <= limit) { 505 region.start = base; 506 region.end = limit + 0xfffff; 507 pcibios_bus_to_resource(dev->bus, res, ®ion); 508 if (log) 509 pci_info(dev, " bridge window %pR\n", res); 510 } else { 511 resource_set_range(res, 0, 0); 512 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; 513 } 514 } 515 516 static void pci_read_bridge_windows(struct pci_dev *bridge) 517 { 518 u32 buses; 519 u16 io; 520 u32 pmem, tmp; 521 struct resource res; 522 523 pci_read_config_dword(bridge, PCI_PRIMARY_BUS, &buses); 524 res.flags = IORESOURCE_BUS; 525 res.start = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses); 526 res.end = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses); 527 pci_info(bridge, "PCI bridge to %pR%s\n", &res, 528 bridge->transparent ? " (subtractive decode)" : ""); 529 530 pci_read_config_word(bridge, PCI_IO_BASE, &io); 531 if (!io) { 532 pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0); 533 pci_read_config_word(bridge, PCI_IO_BASE, &io); 534 pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 535 } 536 if (io) { 537 bridge->io_window = 1; 538 pci_read_bridge_io(bridge, &res, true); 539 } 540 541 pci_read_bridge_mmio(bridge, &res, true); 542 543 /* 544 * DECchip 21050 pass 2 errata: the bridge may miss an address 545 * disconnect boundary by one PCI data phase. Workaround: do not 546 * use prefetching on this device. 547 */ 548 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 549 return; 550 551 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 552 if (!pmem) { 553 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 554 0xffe0fff0); 555 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 556 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 557 } 558 if (!pmem) 559 return; 560 561 bridge->pref_window = 1; 562 563 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { 564 565 /* 566 * Bridge claims to have a 64-bit prefetchable memory 567 * window; verify that the upper bits are actually 568 * writable. 569 */ 570 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &pmem); 571 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 572 0xffffffff); 573 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 574 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, pmem); 575 if (tmp) 576 bridge->pref_64_window = 1; 577 } 578 579 pci_read_bridge_mmio_pref(bridge, &res, true); 580 } 581 582 void pci_read_bridge_bases(struct pci_bus *child) 583 { 584 struct pci_dev *dev = child->self; 585 struct resource *res; 586 int i; 587 588 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */ 589 return; 590 591 pci_info(dev, "PCI bridge to %pR%s\n", 592 &child->busn_res, 593 dev->transparent ? " (subtractive decode)" : ""); 594 595 pci_bus_remove_resources(child); 596 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) 597 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i]; 598 599 pci_read_bridge_io(child->self, 600 child->resource[PCI_BUS_BRIDGE_IO_WINDOW], false); 601 pci_read_bridge_mmio(child->self, 602 child->resource[PCI_BUS_BRIDGE_MEM_WINDOW], false); 603 pci_read_bridge_mmio_pref(child->self, 604 child->resource[PCI_BUS_BRIDGE_PREF_MEM_WINDOW], 605 false); 606 607 if (!dev->transparent) 608 return; 609 610 pci_bus_for_each_resource(child->parent, res) { 611 if (!res || !res->flags) 612 continue; 613 614 pci_bus_add_resource(child, res); 615 pci_info(dev, " bridge window %pR (subtractive decode)\n", res); 616 } 617 } 618 619 static struct pci_bus *pci_alloc_bus(struct pci_bus *parent) 620 { 621 struct pci_bus *b; 622 623 b = kzalloc(sizeof(*b), GFP_KERNEL); 624 if (!b) 625 return NULL; 626 627 INIT_LIST_HEAD(&b->node); 628 INIT_LIST_HEAD(&b->children); 629 INIT_LIST_HEAD(&b->devices); 630 INIT_LIST_HEAD(&b->slots); 631 INIT_LIST_HEAD(&b->resources); 632 b->max_bus_speed = PCI_SPEED_UNKNOWN; 633 b->cur_bus_speed = PCI_SPEED_UNKNOWN; 634 #ifdef CONFIG_PCI_DOMAINS_GENERIC 635 if (parent) 636 b->domain_nr = parent->domain_nr; 637 #endif 638 return b; 639 } 640 641 static void pci_release_host_bridge_dev(struct device *dev) 642 { 643 struct pci_host_bridge *bridge = to_pci_host_bridge(dev); 644 645 if (bridge->release_fn) 646 bridge->release_fn(bridge); 647 648 pci_free_resource_list(&bridge->windows); 649 pci_free_resource_list(&bridge->dma_ranges); 650 651 /* Host bridges only have domain_nr set in the emulation case */ 652 if (bridge->domain_nr != PCI_DOMAIN_NR_NOT_SET) 653 pci_bus_release_emul_domain_nr(bridge->domain_nr); 654 655 kfree(bridge); 656 } 657 658 static const struct attribute_group *pci_host_bridge_groups[] = { 659 #ifdef CONFIG_PCI_IDE 660 &pci_ide_attr_group, 661 #endif 662 NULL 663 }; 664 665 static const struct device_type pci_host_bridge_type = { 666 .groups = pci_host_bridge_groups, 667 .release = pci_release_host_bridge_dev, 668 }; 669 670 static void pci_init_host_bridge(struct pci_host_bridge *bridge) 671 { 672 INIT_LIST_HEAD(&bridge->windows); 673 INIT_LIST_HEAD(&bridge->dma_ranges); 674 675 /* 676 * We assume we can manage these PCIe features. Some systems may 677 * reserve these for use by the platform itself, e.g., an ACPI BIOS 678 * may implement its own AER handling and use _OSC to prevent the 679 * OS from interfering. 680 */ 681 bridge->native_aer = 1; 682 bridge->native_pcie_hotplug = 1; 683 bridge->native_shpc_hotplug = 1; 684 bridge->native_pme = 1; 685 bridge->native_ltr = 1; 686 bridge->native_dpc = 1; 687 bridge->domain_nr = PCI_DOMAIN_NR_NOT_SET; 688 bridge->native_cxl_error = 1; 689 bridge->dev.type = &pci_host_bridge_type; 690 pci_ide_init_host_bridge(bridge); 691 692 device_initialize(&bridge->dev); 693 } 694 695 struct pci_host_bridge *pci_alloc_host_bridge(size_t priv) 696 { 697 struct pci_host_bridge *bridge; 698 699 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL); 700 if (!bridge) 701 return NULL; 702 703 pci_init_host_bridge(bridge); 704 705 return bridge; 706 } 707 EXPORT_SYMBOL(pci_alloc_host_bridge); 708 709 static void devm_pci_alloc_host_bridge_release(void *data) 710 { 711 pci_free_host_bridge(data); 712 } 713 714 struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev, 715 size_t priv) 716 { 717 int ret; 718 struct pci_host_bridge *bridge; 719 720 bridge = pci_alloc_host_bridge(priv); 721 if (!bridge) 722 return NULL; 723 724 bridge->dev.parent = dev; 725 726 ret = devm_add_action_or_reset(dev, devm_pci_alloc_host_bridge_release, 727 bridge); 728 if (ret) 729 return NULL; 730 731 ret = devm_of_pci_bridge_init(dev, bridge); 732 if (ret) 733 return NULL; 734 735 return bridge; 736 } 737 EXPORT_SYMBOL(devm_pci_alloc_host_bridge); 738 739 void pci_free_host_bridge(struct pci_host_bridge *bridge) 740 { 741 put_device(&bridge->dev); 742 } 743 EXPORT_SYMBOL(pci_free_host_bridge); 744 745 /* Indexed by PCI_X_SSTATUS_FREQ (secondary bus mode and frequency) */ 746 static const unsigned char pcix_bus_speed[] = { 747 PCI_SPEED_UNKNOWN, /* 0 */ 748 PCI_SPEED_66MHz_PCIX, /* 1 */ 749 PCI_SPEED_100MHz_PCIX, /* 2 */ 750 PCI_SPEED_133MHz_PCIX, /* 3 */ 751 PCI_SPEED_UNKNOWN, /* 4 */ 752 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */ 753 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */ 754 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */ 755 PCI_SPEED_UNKNOWN, /* 8 */ 756 PCI_SPEED_66MHz_PCIX_266, /* 9 */ 757 PCI_SPEED_100MHz_PCIX_266, /* A */ 758 PCI_SPEED_133MHz_PCIX_266, /* B */ 759 PCI_SPEED_UNKNOWN, /* C */ 760 PCI_SPEED_66MHz_PCIX_533, /* D */ 761 PCI_SPEED_100MHz_PCIX_533, /* E */ 762 PCI_SPEED_133MHz_PCIX_533 /* F */ 763 }; 764 765 /* Indexed by PCI_EXP_LNKCAP_SLS, PCI_EXP_LNKSTA_CLS */ 766 const unsigned char pcie_link_speed[] = { 767 PCI_SPEED_UNKNOWN, /* 0 */ 768 PCIE_SPEED_2_5GT, /* 1 */ 769 PCIE_SPEED_5_0GT, /* 2 */ 770 PCIE_SPEED_8_0GT, /* 3 */ 771 PCIE_SPEED_16_0GT, /* 4 */ 772 PCIE_SPEED_32_0GT, /* 5 */ 773 PCIE_SPEED_64_0GT, /* 6 */ 774 PCI_SPEED_UNKNOWN, /* 7 */ 775 PCI_SPEED_UNKNOWN, /* 8 */ 776 PCI_SPEED_UNKNOWN, /* 9 */ 777 PCI_SPEED_UNKNOWN, /* A */ 778 PCI_SPEED_UNKNOWN, /* B */ 779 PCI_SPEED_UNKNOWN, /* C */ 780 PCI_SPEED_UNKNOWN, /* D */ 781 PCI_SPEED_UNKNOWN, /* E */ 782 PCI_SPEED_UNKNOWN /* F */ 783 }; 784 EXPORT_SYMBOL_GPL(pcie_link_speed); 785 786 const char *pci_speed_string(enum pci_bus_speed speed) 787 { 788 /* Indexed by the pci_bus_speed enum */ 789 static const char *speed_strings[] = { 790 "33 MHz PCI", /* 0x00 */ 791 "66 MHz PCI", /* 0x01 */ 792 "66 MHz PCI-X", /* 0x02 */ 793 "100 MHz PCI-X", /* 0x03 */ 794 "133 MHz PCI-X", /* 0x04 */ 795 NULL, /* 0x05 */ 796 NULL, /* 0x06 */ 797 NULL, /* 0x07 */ 798 NULL, /* 0x08 */ 799 "66 MHz PCI-X 266", /* 0x09 */ 800 "100 MHz PCI-X 266", /* 0x0a */ 801 "133 MHz PCI-X 266", /* 0x0b */ 802 "Unknown AGP", /* 0x0c */ 803 "1x AGP", /* 0x0d */ 804 "2x AGP", /* 0x0e */ 805 "4x AGP", /* 0x0f */ 806 "8x AGP", /* 0x10 */ 807 "66 MHz PCI-X 533", /* 0x11 */ 808 "100 MHz PCI-X 533", /* 0x12 */ 809 "133 MHz PCI-X 533", /* 0x13 */ 810 "2.5 GT/s PCIe", /* 0x14 */ 811 "5.0 GT/s PCIe", /* 0x15 */ 812 "8.0 GT/s PCIe", /* 0x16 */ 813 "16.0 GT/s PCIe", /* 0x17 */ 814 "32.0 GT/s PCIe", /* 0x18 */ 815 "64.0 GT/s PCIe", /* 0x19 */ 816 }; 817 818 if (speed < ARRAY_SIZE(speed_strings)) 819 return speed_strings[speed]; 820 return "Unknown"; 821 } 822 EXPORT_SYMBOL_GPL(pci_speed_string); 823 824 void pcie_update_link_speed(struct pci_bus *bus, 825 enum pcie_link_change_reason reason) 826 { 827 struct pci_dev *bridge = bus->self; 828 u16 linksta, linksta2; 829 830 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta); 831 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA2, &linksta2); 832 833 __pcie_update_link_speed(bus, reason, linksta, linksta2); 834 } 835 EXPORT_SYMBOL_GPL(pcie_update_link_speed); 836 837 static unsigned char agp_speeds[] = { 838 AGP_UNKNOWN, 839 AGP_1X, 840 AGP_2X, 841 AGP_4X, 842 AGP_8X 843 }; 844 845 static enum pci_bus_speed agp_speed(int agp3, int agpstat) 846 { 847 int index = 0; 848 849 if (agpstat & 4) 850 index = 3; 851 else if (agpstat & 2) 852 index = 2; 853 else if (agpstat & 1) 854 index = 1; 855 else 856 goto out; 857 858 if (agp3) { 859 index += 2; 860 if (index == 5) 861 index = 0; 862 } 863 864 out: 865 return agp_speeds[index]; 866 } 867 868 static void pci_set_bus_speed(struct pci_bus *bus) 869 { 870 struct pci_dev *bridge = bus->self; 871 int pos; 872 873 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP); 874 if (!pos) 875 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3); 876 if (pos) { 877 u32 agpstat, agpcmd; 878 879 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat); 880 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7); 881 882 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd); 883 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7); 884 } 885 886 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX); 887 if (pos) { 888 u16 status; 889 enum pci_bus_speed max; 890 891 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS, 892 &status); 893 894 if (status & PCI_X_SSTATUS_533MHZ) { 895 max = PCI_SPEED_133MHz_PCIX_533; 896 } else if (status & PCI_X_SSTATUS_266MHZ) { 897 max = PCI_SPEED_133MHz_PCIX_266; 898 } else if (status & PCI_X_SSTATUS_133MHZ) { 899 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2) 900 max = PCI_SPEED_133MHz_PCIX_ECC; 901 else 902 max = PCI_SPEED_133MHz_PCIX; 903 } else { 904 max = PCI_SPEED_66MHz_PCIX; 905 } 906 907 bus->max_bus_speed = max; 908 bus->cur_bus_speed = 909 pcix_bus_speed[FIELD_GET(PCI_X_SSTATUS_FREQ, status)]; 910 911 return; 912 } 913 914 if (pci_is_pcie(bridge)) { 915 u32 linkcap; 916 917 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap); 918 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS]; 919 920 pcie_update_link_speed(bus, PCIE_ADD_BUS); 921 } 922 } 923 924 static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus) 925 { 926 struct irq_domain *d; 927 928 /* If the host bridge driver sets a MSI domain of the bridge, use it */ 929 d = dev_get_msi_domain(bus->bridge); 930 931 /* 932 * Any firmware interface that can resolve the msi_domain 933 * should be called from here. 934 */ 935 if (!d) 936 d = pci_host_bridge_of_msi_domain(bus); 937 if (!d) 938 d = pci_host_bridge_acpi_msi_domain(bus); 939 940 /* 941 * If no IRQ domain was found via the OF tree, try looking it up 942 * directly through the fwnode_handle. 943 */ 944 if (!d) { 945 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus); 946 947 if (fwnode) 948 d = irq_find_matching_fwnode(fwnode, 949 DOMAIN_BUS_PCI_MSI); 950 } 951 952 return d; 953 } 954 955 static void pci_set_bus_msi_domain(struct pci_bus *bus) 956 { 957 struct irq_domain *d; 958 struct pci_bus *b; 959 960 /* 961 * The bus can be a root bus, a subordinate bus, or a virtual bus 962 * created by an SR-IOV device. Walk up to the first bridge device 963 * found or derive the domain from the host bridge. 964 */ 965 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) { 966 if (b->self) 967 d = dev_get_msi_domain(&b->self->dev); 968 } 969 970 if (!d) 971 d = pci_host_bridge_msi_domain(b); 972 973 dev_set_msi_domain(&bus->dev, d); 974 } 975 976 static bool pci_preserve_config(struct pci_host_bridge *host_bridge) 977 { 978 if (pci_acpi_preserve_config(host_bridge)) 979 return true; 980 981 if (host_bridge->dev.parent && host_bridge->dev.parent->of_node) 982 return of_pci_preserve_config(host_bridge->dev.parent->of_node); 983 984 return false; 985 } 986 987 static int pci_register_host_bridge(struct pci_host_bridge *bridge) 988 { 989 struct device *parent = bridge->dev.parent; 990 struct resource_entry *window, *next, *n; 991 struct pci_bus *bus, *b; 992 resource_size_t offset, next_offset; 993 LIST_HEAD(resources); 994 struct resource *res, *next_res; 995 bool bus_registered = false; 996 char addr[64], *fmt; 997 const char *name; 998 int err; 999 1000 bus = pci_alloc_bus(NULL); 1001 if (!bus) 1002 return -ENOMEM; 1003 1004 bridge->bus = bus; 1005 1006 bus->sysdata = bridge->sysdata; 1007 bus->ops = bridge->ops; 1008 bus->number = bus->busn_res.start = bridge->busnr; 1009 #ifdef CONFIG_PCI_DOMAINS_GENERIC 1010 if (bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET) 1011 bus->domain_nr = pci_bus_find_domain_nr(bus, parent); 1012 else 1013 bus->domain_nr = bridge->domain_nr; 1014 if (bus->domain_nr < 0) { 1015 err = bus->domain_nr; 1016 goto free; 1017 } 1018 #endif 1019 1020 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr); 1021 if (b) { 1022 /* Ignore it if we already got here via a different bridge */ 1023 dev_dbg(&b->dev, "bus already known\n"); 1024 err = -EEXIST; 1025 goto free; 1026 } 1027 1028 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus), 1029 bridge->busnr); 1030 1031 err = pcibios_root_bridge_prepare(bridge); 1032 if (err) 1033 goto free; 1034 1035 /* Temporarily move resources off the list */ 1036 list_splice_init(&bridge->windows, &resources); 1037 err = device_add(&bridge->dev); 1038 if (err) 1039 goto free; 1040 1041 bus->bridge = get_device(&bridge->dev); 1042 device_enable_async_suspend(bus->bridge); 1043 pci_set_bus_of_node(bus); 1044 pci_set_bus_msi_domain(bus); 1045 if (bridge->msi_domain && !dev_get_msi_domain(&bus->dev) && 1046 !pci_host_of_has_msi_map(parent)) 1047 bus->bus_flags |= PCI_BUS_FLAGS_NO_MSI; 1048 1049 if (!parent) 1050 set_dev_node(bus->bridge, pcibus_to_node(bus)); 1051 1052 bus->dev.class = &pcibus_class; 1053 bus->dev.parent = bus->bridge; 1054 1055 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number); 1056 name = dev_name(&bus->dev); 1057 1058 err = device_register(&bus->dev); 1059 bus_registered = true; 1060 if (err) 1061 goto unregister; 1062 1063 pcibios_add_bus(bus); 1064 1065 if (bus->ops->add_bus) { 1066 err = bus->ops->add_bus(bus); 1067 if (WARN_ON(err < 0)) 1068 dev_err(&bus->dev, "failed to add bus: %d\n", err); 1069 } 1070 1071 /* Create legacy_io and legacy_mem files for this bus */ 1072 pci_create_legacy_files(bus); 1073 1074 if (parent) 1075 dev_info(parent, "PCI host bridge to bus %s\n", name); 1076 else 1077 pr_info("PCI host bridge to bus %s\n", name); 1078 1079 if (nr_node_ids > 1 && pcibus_to_node(bus) == NUMA_NO_NODE) 1080 dev_warn(&bus->dev, "Unknown NUMA node; performance will be reduced\n"); 1081 1082 /* Check if the boot configuration by FW needs to be preserved */ 1083 bridge->preserve_config = pci_preserve_config(bridge); 1084 1085 /* Coalesce contiguous windows */ 1086 resource_list_for_each_entry_safe(window, n, &resources) { 1087 if (list_is_last(&window->node, &resources)) 1088 break; 1089 1090 next = list_next_entry(window, node); 1091 offset = window->offset; 1092 res = window->res; 1093 next_offset = next->offset; 1094 next_res = next->res; 1095 1096 if (res->flags != next_res->flags || offset != next_offset) 1097 continue; 1098 1099 if (res->end + 1 == next_res->start) { 1100 next_res->start = res->start; 1101 res->flags = res->start = res->end = 0; 1102 } 1103 } 1104 1105 /* Add initial resources to the bus */ 1106 resource_list_for_each_entry_safe(window, n, &resources) { 1107 offset = window->offset; 1108 res = window->res; 1109 if (!res->flags && !res->start && !res->end) { 1110 release_resource(res); 1111 resource_list_destroy_entry(window); 1112 continue; 1113 } 1114 1115 list_move_tail(&window->node, &bridge->windows); 1116 1117 if (res->flags & IORESOURCE_BUS) 1118 pci_bus_insert_busn_res(bus, bus->number, res->end); 1119 else 1120 pci_bus_add_resource(bus, res); 1121 1122 if (offset) { 1123 if (resource_type(res) == IORESOURCE_IO) 1124 fmt = " (bus address [%#06llx-%#06llx])"; 1125 else 1126 fmt = " (bus address [%#010llx-%#010llx])"; 1127 1128 snprintf(addr, sizeof(addr), fmt, 1129 (unsigned long long)(res->start - offset), 1130 (unsigned long long)(res->end - offset)); 1131 } else 1132 addr[0] = '\0'; 1133 1134 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr); 1135 } 1136 1137 of_pci_make_host_bridge_node(bridge); 1138 1139 down_write(&pci_bus_sem); 1140 list_add_tail(&bus->node, &pci_root_buses); 1141 up_write(&pci_bus_sem); 1142 1143 return 0; 1144 1145 unregister: 1146 put_device(&bridge->dev); 1147 device_del(&bridge->dev); 1148 free: 1149 #ifdef CONFIG_PCI_DOMAINS_GENERIC 1150 if (bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET) 1151 pci_bus_release_domain_nr(parent, bus->domain_nr); 1152 #endif 1153 if (bus_registered) 1154 put_device(&bus->dev); 1155 else 1156 kfree(bus); 1157 1158 return err; 1159 } 1160 1161 static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev *bridge) 1162 { 1163 int pos; 1164 u32 status; 1165 1166 /* 1167 * If extended config space isn't accessible on a bridge's primary 1168 * bus, we certainly can't access it on the secondary bus. 1169 */ 1170 if (bridge->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG) 1171 return false; 1172 1173 /* 1174 * PCIe Root Ports and switch ports are PCIe on both sides, so if 1175 * extended config space is accessible on the primary, it's also 1176 * accessible on the secondary. 1177 */ 1178 if (pci_is_pcie(bridge) && 1179 (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT || 1180 pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM || 1181 pci_pcie_type(bridge) == PCI_EXP_TYPE_DOWNSTREAM)) 1182 return true; 1183 1184 /* 1185 * For the other bridge types: 1186 * - PCI-to-PCI bridges 1187 * - PCIe-to-PCI/PCI-X forward bridges 1188 * - PCI/PCI-X-to-PCIe reverse bridges 1189 * extended config space on the secondary side is only accessible 1190 * if the bridge supports PCI-X Mode 2. 1191 */ 1192 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX); 1193 if (!pos) 1194 return false; 1195 1196 pci_read_config_dword(bridge, pos + PCI_X_STATUS, &status); 1197 return status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ); 1198 } 1199 1200 static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, 1201 struct pci_dev *bridge, int busnr) 1202 { 1203 struct pci_bus *child; 1204 struct pci_host_bridge *host; 1205 int i; 1206 int ret; 1207 1208 /* Allocate a new bus and inherit stuff from the parent */ 1209 child = pci_alloc_bus(parent); 1210 if (!child) 1211 return NULL; 1212 1213 child->parent = parent; 1214 child->sysdata = parent->sysdata; 1215 child->bus_flags = parent->bus_flags; 1216 1217 host = pci_find_host_bridge(parent); 1218 if (host->child_ops) 1219 child->ops = host->child_ops; 1220 else 1221 child->ops = parent->ops; 1222 1223 /* 1224 * Initialize some portions of the bus device, but don't register 1225 * it now as the parent is not properly set up yet. 1226 */ 1227 child->dev.class = &pcibus_class; 1228 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr); 1229 1230 /* Set up the primary, secondary and subordinate bus numbers */ 1231 child->number = child->busn_res.start = busnr; 1232 child->primary = parent->busn_res.start; 1233 child->busn_res.end = 0xff; 1234 1235 if (!bridge) { 1236 child->dev.parent = parent->bridge; 1237 goto add_dev; 1238 } 1239 1240 child->self = bridge; 1241 child->bridge = get_device(&bridge->dev); 1242 child->dev.parent = child->bridge; 1243 pci_set_bus_of_node(child); 1244 pci_set_bus_speed(child); 1245 1246 /* 1247 * Check whether extended config space is accessible on the child 1248 * bus. Note that we currently assume it is always accessible on 1249 * the root bus. 1250 */ 1251 if (!pci_bridge_child_ext_cfg_accessible(bridge)) { 1252 child->bus_flags |= PCI_BUS_FLAGS_NO_EXTCFG; 1253 pci_info(child, "extended config space not accessible\n"); 1254 } 1255 1256 /* Set up default resource pointers and names */ 1257 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { 1258 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i]; 1259 child->resource[i]->name = child->name; 1260 } 1261 bridge->subordinate = child; 1262 1263 add_dev: 1264 pci_set_bus_msi_domain(child); 1265 ret = device_register(&child->dev); 1266 if (WARN_ON(ret < 0)) { 1267 put_device(&child->dev); 1268 return NULL; 1269 } 1270 1271 pcibios_add_bus(child); 1272 1273 if (child->ops->add_bus) { 1274 ret = child->ops->add_bus(child); 1275 if (WARN_ON(ret < 0)) 1276 dev_err(&child->dev, "failed to add bus: %d\n", ret); 1277 } 1278 1279 /* Create legacy_io and legacy_mem files for this bus */ 1280 pci_create_legacy_files(child); 1281 1282 return child; 1283 } 1284 1285 struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, 1286 int busnr) 1287 { 1288 struct pci_bus *child; 1289 1290 child = pci_alloc_child_bus(parent, dev, busnr); 1291 if (child) { 1292 down_write(&pci_bus_sem); 1293 list_add_tail(&child->node, &parent->children); 1294 up_write(&pci_bus_sem); 1295 } 1296 return child; 1297 } 1298 EXPORT_SYMBOL(pci_add_new_bus); 1299 1300 static void pci_enable_rrs_sv(struct pci_dev *pdev) 1301 { 1302 u16 root_cap = 0; 1303 1304 /* Enable Configuration RRS Software Visibility if supported */ 1305 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap); 1306 if (root_cap & PCI_EXP_RTCAP_RRS_SV) { 1307 pcie_capability_set_word(pdev, PCI_EXP_RTCTL, 1308 PCI_EXP_RTCTL_RRS_SVE); 1309 pdev->config_rrs_sv = 1; 1310 } 1311 } 1312 1313 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus, 1314 unsigned int available_buses); 1315 1316 void pbus_validate_busn(struct pci_bus *bus) 1317 { 1318 struct pci_bus *upstream = bus->parent; 1319 struct pci_dev *bridge = bus->self; 1320 1321 /* Check that all devices are accessible */ 1322 while (upstream->parent) { 1323 if ((bus->busn_res.end > upstream->busn_res.end) || 1324 (bus->number > upstream->busn_res.end) || 1325 (bus->number < upstream->number) || 1326 (bus->busn_res.end < upstream->number)) { 1327 pci_info(bridge, "devices behind bridge are unusable because %pR cannot be assigned for them\n", 1328 &bus->busn_res); 1329 break; 1330 } 1331 upstream = upstream->parent; 1332 } 1333 } 1334 1335 /** 1336 * pci_ea_fixed_busnrs() - Read fixed Secondary and Subordinate bus 1337 * numbers from EA capability. 1338 * @dev: Bridge 1339 * @sec: updated with secondary bus number from EA 1340 * @sub: updated with subordinate bus number from EA 1341 * 1342 * If @dev is a bridge with EA capability that specifies valid secondary 1343 * and subordinate bus numbers, return true with the bus numbers in @sec 1344 * and @sub. Otherwise return false. 1345 */ 1346 bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub) 1347 { 1348 int ea, offset; 1349 u32 dw; 1350 u8 ea_sec, ea_sub; 1351 1352 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) 1353 return false; 1354 1355 /* find PCI EA capability in list */ 1356 ea = pci_find_capability(dev, PCI_CAP_ID_EA); 1357 if (!ea) 1358 return false; 1359 1360 offset = ea + PCI_EA_FIRST_ENT; 1361 pci_read_config_dword(dev, offset, &dw); 1362 ea_sec = FIELD_GET(PCI_EA_SEC_BUS_MASK, dw); 1363 ea_sub = FIELD_GET(PCI_EA_SUB_BUS_MASK, dw); 1364 if (ea_sec == 0 || ea_sub < ea_sec) 1365 return false; 1366 1367 *sec = ea_sec; 1368 *sub = ea_sub; 1369 return true; 1370 } 1371 1372 /* 1373 * pci_scan_bridge_extend() - Scan buses behind a bridge 1374 * @bus: Parent bus the bridge is on 1375 * @dev: Bridge itself 1376 * @max: Starting subordinate number of buses behind this bridge 1377 * @available_buses: Total number of buses available for this bridge and 1378 * the devices below. After the minimal bus space has 1379 * been allocated the remaining buses will be 1380 * distributed equally between hotplug-capable bridges. 1381 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges 1382 * that need to be reconfigured. 1383 * 1384 * If it's a bridge, configure it and scan the bus behind it. 1385 * For CardBus bridges, we don't scan behind as the devices will 1386 * be handled by the bridge driver itself. 1387 * 1388 * We need to process bridges in two passes -- first we scan those 1389 * already configured by the BIOS and after we are done with all of 1390 * them, we proceed to assigning numbers to the remaining buses in 1391 * order to avoid overlaps between old and new bus numbers. 1392 * 1393 * Return: New subordinate number covering all buses behind this bridge. 1394 */ 1395 static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev, 1396 int max, unsigned int available_buses, 1397 int pass) 1398 { 1399 struct pci_bus *child; 1400 u32 buses; 1401 u16 bctl; 1402 u8 primary, secondary, subordinate; 1403 int broken = 0; 1404 bool fixed_buses; 1405 u8 fixed_sec, fixed_sub; 1406 int next_busnr; 1407 1408 /* 1409 * Make sure the bridge is powered on to be able to access config 1410 * space of devices below it. 1411 */ 1412 pm_runtime_get_sync(&dev->dev); 1413 1414 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses); 1415 primary = FIELD_GET(PCI_PRIMARY_BUS_MASK, buses); 1416 secondary = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses); 1417 subordinate = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses); 1418 1419 pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n", 1420 secondary, subordinate, pass); 1421 1422 if (!primary && (primary != bus->number) && secondary && subordinate) { 1423 pci_warn(dev, "Primary bus is hard wired to 0\n"); 1424 primary = bus->number; 1425 } 1426 1427 /* Check if setup is sensible at all */ 1428 if (!pass && 1429 (primary != bus->number || secondary <= bus->number || 1430 secondary > subordinate)) { 1431 pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n", 1432 secondary, subordinate); 1433 broken = 1; 1434 } 1435 1436 /* 1437 * Disable Master-Abort Mode during probing to avoid reporting of 1438 * bus errors in some architectures. 1439 */ 1440 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl); 1441 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, 1442 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT); 1443 1444 if (pci_is_cardbus_bridge(dev)) { 1445 max = pci_cardbus_scan_bridge_extend(bus, dev, buses, max, 1446 available_buses, 1447 pass); 1448 goto out; 1449 } 1450 1451 if ((secondary || subordinate) && 1452 !pcibios_assign_all_busses() && !broken) { 1453 unsigned int cmax, buses; 1454 1455 /* 1456 * Bus already configured by firmware, process it in the 1457 * first pass and just note the configuration. 1458 */ 1459 if (pass) 1460 goto out; 1461 1462 /* 1463 * The bus might already exist for two reasons: Either we 1464 * are rescanning the bus or the bus is reachable through 1465 * more than one bridge. The second case can happen with 1466 * the i450NX chipset. 1467 */ 1468 child = pci_find_bus(pci_domain_nr(bus), secondary); 1469 if (!child) { 1470 child = pci_add_new_bus(bus, dev, secondary); 1471 if (!child) 1472 goto out; 1473 child->primary = primary; 1474 pci_bus_insert_busn_res(child, secondary, subordinate); 1475 child->bridge_ctl = bctl; 1476 } 1477 1478 buses = subordinate - secondary; 1479 cmax = pci_scan_child_bus_extend(child, buses); 1480 if (cmax > subordinate) 1481 pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n", 1482 subordinate, cmax); 1483 1484 /* Subordinate should equal child->busn_res.end */ 1485 if (subordinate > max) 1486 max = subordinate; 1487 } else { 1488 1489 /* 1490 * We need to assign a number to this bus which we always 1491 * do in the second pass. 1492 */ 1493 if (!pass) { 1494 if (pcibios_assign_all_busses() || broken) 1495 1496 /* 1497 * Temporarily disable forwarding of the 1498 * configuration cycles on all bridges in 1499 * this bus segment to avoid possible 1500 * conflicts in the second pass between two 1501 * bridges programmed with overlapping bus 1502 * ranges. 1503 */ 1504 pci_write_config_dword(dev, PCI_PRIMARY_BUS, 1505 buses & PCI_SEC_LATENCY_TIMER_MASK); 1506 goto out; 1507 } 1508 1509 /* Clear errors */ 1510 pci_write_config_word(dev, PCI_STATUS, 0xffff); 1511 1512 /* Read bus numbers from EA Capability (if present) */ 1513 fixed_buses = pci_ea_fixed_busnrs(dev, &fixed_sec, &fixed_sub); 1514 if (fixed_buses) 1515 next_busnr = fixed_sec; 1516 else 1517 next_busnr = max + 1; 1518 1519 /* 1520 * Prevent assigning a bus number that already exists. 1521 * This can happen when a bridge is hot-plugged, so in this 1522 * case we only re-scan this bus. 1523 */ 1524 child = pci_find_bus(pci_domain_nr(bus), next_busnr); 1525 if (!child) { 1526 child = pci_add_new_bus(bus, dev, next_busnr); 1527 if (!child) 1528 goto out; 1529 pci_bus_insert_busn_res(child, next_busnr, 1530 bus->busn_res.end); 1531 } 1532 max++; 1533 if (available_buses) 1534 available_buses--; 1535 1536 buses = (buses & PCI_SEC_LATENCY_TIMER_MASK) | 1537 FIELD_PREP(PCI_PRIMARY_BUS_MASK, child->primary) | 1538 FIELD_PREP(PCI_SECONDARY_BUS_MASK, child->busn_res.start) | 1539 FIELD_PREP(PCI_SUBORDINATE_BUS_MASK, child->busn_res.end); 1540 1541 /* We need to blast all three values with a single write */ 1542 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses); 1543 1544 child->bridge_ctl = bctl; 1545 max = pci_scan_child_bus_extend(child, available_buses); 1546 1547 /* 1548 * Set subordinate bus number to its real value. 1549 * If fixed subordinate bus number exists from EA 1550 * capability then use it. 1551 */ 1552 if (fixed_buses) 1553 max = fixed_sub; 1554 pci_bus_update_busn_res_end(child, max); 1555 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max); 1556 } 1557 scnprintf(child->name, sizeof(child->name), "PCI Bus %04x:%02x", 1558 pci_domain_nr(bus), child->number); 1559 1560 pbus_validate_busn(child); 1561 1562 out: 1563 /* Clear errors in the Secondary Status Register */ 1564 pci_write_config_word(dev, PCI_SEC_STATUS, 0xffff); 1565 1566 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl); 1567 1568 pm_runtime_put(&dev->dev); 1569 1570 return max; 1571 } 1572 1573 /* 1574 * pci_scan_bridge() - Scan buses behind a bridge 1575 * @bus: Parent bus the bridge is on 1576 * @dev: Bridge itself 1577 * @max: Starting subordinate number of buses behind this bridge 1578 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges 1579 * that need to be reconfigured. 1580 * 1581 * If it's a bridge, configure it and scan the bus behind it. 1582 * For CardBus bridges, we don't scan behind as the devices will 1583 * be handled by the bridge driver itself. 1584 * 1585 * We need to process bridges in two passes -- first we scan those 1586 * already configured by the BIOS and after we are done with all of 1587 * them, we proceed to assigning numbers to the remaining buses in 1588 * order to avoid overlaps between old and new bus numbers. 1589 * 1590 * Return: New subordinate number covering all buses behind this bridge. 1591 */ 1592 int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass) 1593 { 1594 return pci_scan_bridge_extend(bus, dev, max, 0, pass); 1595 } 1596 EXPORT_SYMBOL(pci_scan_bridge); 1597 1598 /* 1599 * Read interrupt line and base address registers. 1600 * The architecture-dependent code can tweak these, of course. 1601 */ 1602 static void pci_read_irq(struct pci_dev *dev) 1603 { 1604 unsigned char irq; 1605 1606 /* VFs are not allowed to use INTx, so skip the config reads */ 1607 if (dev->is_virtfn) { 1608 dev->pin = 0; 1609 dev->irq = 0; 1610 return; 1611 } 1612 1613 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq); 1614 dev->pin = irq; 1615 if (irq) 1616 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq); 1617 dev->irq = irq; 1618 } 1619 1620 void set_pcie_port_type(struct pci_dev *pdev) 1621 { 1622 int pos; 1623 u16 reg16; 1624 u32 reg32; 1625 int type; 1626 struct pci_dev *parent; 1627 1628 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); 1629 if (!pos) 1630 return; 1631 1632 pdev->pcie_cap = pos; 1633 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16); 1634 pdev->pcie_flags_reg = reg16; 1635 1636 type = pci_pcie_type(pdev); 1637 if (type == PCI_EXP_TYPE_ROOT_PORT) 1638 pci_enable_rrs_sv(pdev); 1639 1640 pci_read_config_dword(pdev, pos + PCI_EXP_DEVCAP, &pdev->devcap); 1641 pdev->pcie_mpss = FIELD_GET(PCI_EXP_DEVCAP_PAYLOAD, pdev->devcap); 1642 1643 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, ®32); 1644 if (reg32 & PCI_EXP_LNKCAP_DLLLARC) 1645 pdev->link_active_reporting = 1; 1646 1647 #ifdef CONFIG_PCIEASPM 1648 if (reg32 & PCI_EXP_LNKCAP_ASPM_L0S) 1649 pdev->aspm_l0s_support = 1; 1650 if (reg32 & PCI_EXP_LNKCAP_ASPM_L1) 1651 pdev->aspm_l1_support = 1; 1652 #endif 1653 1654 parent = pci_upstream_bridge(pdev); 1655 if (!parent) 1656 return; 1657 1658 /* 1659 * Some systems do not identify their upstream/downstream ports 1660 * correctly so detect impossible configurations here and correct 1661 * the port type accordingly. 1662 */ 1663 if (type == PCI_EXP_TYPE_DOWNSTREAM) { 1664 /* 1665 * If pdev claims to be downstream port but the parent 1666 * device is also downstream port assume pdev is actually 1667 * upstream port. 1668 */ 1669 if (pcie_downstream_port(parent)) { 1670 pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n"); 1671 pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE; 1672 pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM; 1673 } 1674 } else if (type == PCI_EXP_TYPE_UPSTREAM) { 1675 /* 1676 * If pdev claims to be upstream port but the parent 1677 * device is also upstream port assume pdev is actually 1678 * downstream port. 1679 */ 1680 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) { 1681 pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n"); 1682 pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE; 1683 pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM; 1684 } 1685 } 1686 } 1687 1688 void set_pcie_hotplug_bridge(struct pci_dev *pdev) 1689 { 1690 u32 reg32; 1691 1692 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32); 1693 if (reg32 & PCI_EXP_SLTCAP_HPC) 1694 pdev->is_hotplug_bridge = pdev->is_pciehp = 1; 1695 } 1696 1697 static void set_pcie_thunderbolt(struct pci_dev *dev) 1698 { 1699 u16 vsec; 1700 1701 /* Is the device part of a Thunderbolt controller? */ 1702 vsec = pci_find_vsec_capability(dev, PCI_VENDOR_ID_INTEL, PCI_VSEC_ID_INTEL_TBT); 1703 if (vsec) 1704 dev->is_thunderbolt = 1; 1705 } 1706 1707 static void set_pcie_untrusted(struct pci_dev *dev) 1708 { 1709 struct pci_dev *parent = pci_upstream_bridge(dev); 1710 1711 if (!parent) 1712 return; 1713 /* 1714 * If the upstream bridge is untrusted we treat this device as 1715 * untrusted as well. 1716 */ 1717 if (parent->untrusted) { 1718 dev->untrusted = true; 1719 return; 1720 } 1721 1722 if (arch_pci_dev_is_removable(dev)) { 1723 pci_dbg(dev, "marking as untrusted\n"); 1724 dev->untrusted = true; 1725 } 1726 } 1727 1728 static void pci_set_removable(struct pci_dev *dev) 1729 { 1730 struct pci_dev *parent = pci_upstream_bridge(dev); 1731 1732 if (!parent) 1733 return; 1734 /* 1735 * We (only) consider everything tunneled below an external_facing 1736 * device to be removable by the user. We're mainly concerned with 1737 * consumer platforms with user accessible thunderbolt ports that are 1738 * vulnerable to DMA attacks, and we expect those ports to be marked by 1739 * the firmware as external_facing. Devices in traditional hotplug 1740 * slots can technically be removed, but the expectation is that unless 1741 * the port is marked with external_facing, such devices are less 1742 * accessible to user / may not be removed by end user, and thus not 1743 * exposed as "removable" to userspace. 1744 */ 1745 if (dev_is_removable(&parent->dev)) { 1746 dev_set_removable(&dev->dev, DEVICE_REMOVABLE); 1747 return; 1748 } 1749 1750 if (arch_pci_dev_is_removable(dev)) { 1751 pci_dbg(dev, "marking as removable\n"); 1752 dev_set_removable(&dev->dev, DEVICE_REMOVABLE); 1753 } 1754 } 1755 1756 /** 1757 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config? 1758 * @dev: PCI device 1759 * 1760 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that 1761 * when forwarding a type1 configuration request the bridge must check that 1762 * the extended register address field is zero. The bridge is not permitted 1763 * to forward the transactions and must handle it as an Unsupported Request. 1764 * Some bridges do not follow this rule and simply drop the extended register 1765 * bits, resulting in the standard config space being aliased, every 256 1766 * bytes across the entire configuration space. Test for this condition by 1767 * comparing the first dword of each potential alias to the vendor/device ID. 1768 * Known offenders: 1769 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03) 1770 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40) 1771 */ 1772 static bool pci_ext_cfg_is_aliased(struct pci_dev *dev) 1773 { 1774 #ifdef CONFIG_PCI_QUIRKS 1775 int pos, ret; 1776 u32 header, tmp; 1777 1778 pci_read_config_dword(dev, PCI_VENDOR_ID, &header); 1779 1780 for (pos = PCI_CFG_SPACE_SIZE; 1781 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) { 1782 ret = pci_read_config_dword(dev, pos, &tmp); 1783 if ((ret != PCIBIOS_SUCCESSFUL) || (header != tmp)) 1784 return false; 1785 } 1786 1787 return true; 1788 #else 1789 return false; 1790 #endif 1791 } 1792 1793 /** 1794 * pci_cfg_space_size_ext - Get the configuration space size of the PCI device 1795 * @dev: PCI device 1796 * 1797 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices 1798 * have 4096 bytes. Even if the device is capable, that doesn't mean we can 1799 * access it. Maybe we don't have a way to generate extended config space 1800 * accesses, or the device is behind a reverse Express bridge. So we try 1801 * reading the dword at 0x100 which must either be 0 or a valid extended 1802 * capability header. 1803 */ 1804 static int pci_cfg_space_size_ext(struct pci_dev *dev) 1805 { 1806 u32 status; 1807 int pos = PCI_CFG_SPACE_SIZE; 1808 1809 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL) 1810 return PCI_CFG_SPACE_SIZE; 1811 if (PCI_POSSIBLE_ERROR(status) || pci_ext_cfg_is_aliased(dev)) 1812 return PCI_CFG_SPACE_SIZE; 1813 1814 return PCI_CFG_SPACE_EXP_SIZE; 1815 } 1816 1817 int pci_cfg_space_size(struct pci_dev *dev) 1818 { 1819 int pos; 1820 u32 status; 1821 u16 class; 1822 1823 #ifdef CONFIG_PCI_IOV 1824 /* 1825 * Per the SR-IOV specification (rev 1.1, sec 3.5), VFs are required to 1826 * implement a PCIe capability and therefore must implement extended 1827 * config space. We can skip the NO_EXTCFG test below and the 1828 * reachability/aliasing test in pci_cfg_space_size_ext() by virtue of 1829 * the fact that the SR-IOV capability on the PF resides in extended 1830 * config space and must be accessible and non-aliased to have enabled 1831 * support for this VF. This is a micro performance optimization for 1832 * systems supporting many VFs. 1833 */ 1834 if (dev->is_virtfn) 1835 return PCI_CFG_SPACE_EXP_SIZE; 1836 #endif 1837 1838 if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG) 1839 return PCI_CFG_SPACE_SIZE; 1840 1841 class = dev->class >> 8; 1842 if (class == PCI_CLASS_BRIDGE_HOST) 1843 return pci_cfg_space_size_ext(dev); 1844 1845 if (pci_is_pcie(dev)) 1846 return pci_cfg_space_size_ext(dev); 1847 1848 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); 1849 if (!pos) 1850 return PCI_CFG_SPACE_SIZE; 1851 1852 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status); 1853 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)) 1854 return pci_cfg_space_size_ext(dev); 1855 1856 return PCI_CFG_SPACE_SIZE; 1857 } 1858 1859 static u32 pci_class(struct pci_dev *dev) 1860 { 1861 u32 class; 1862 1863 #ifdef CONFIG_PCI_IOV 1864 if (dev->is_virtfn) 1865 return dev->physfn->sriov->class; 1866 #endif 1867 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class); 1868 return class; 1869 } 1870 1871 static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device) 1872 { 1873 #ifdef CONFIG_PCI_IOV 1874 if (dev->is_virtfn) { 1875 *vendor = dev->physfn->sriov->subsystem_vendor; 1876 *device = dev->physfn->sriov->subsystem_device; 1877 return; 1878 } 1879 #endif 1880 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor); 1881 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device); 1882 } 1883 1884 static u8 pci_hdr_type(struct pci_dev *dev) 1885 { 1886 u8 hdr_type; 1887 1888 #ifdef CONFIG_PCI_IOV 1889 if (dev->is_virtfn) 1890 return dev->physfn->sriov->hdr_type; 1891 #endif 1892 pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type); 1893 return hdr_type; 1894 } 1895 1896 #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED) 1897 1898 /** 1899 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability 1900 * @dev: PCI device 1901 * 1902 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this 1903 * at enumeration-time to avoid modifying PCI_COMMAND at run-time. 1904 */ 1905 static int pci_intx_mask_broken(struct pci_dev *dev) 1906 { 1907 u16 orig, toggle, new; 1908 1909 pci_read_config_word(dev, PCI_COMMAND, &orig); 1910 toggle = orig ^ PCI_COMMAND_INTX_DISABLE; 1911 pci_write_config_word(dev, PCI_COMMAND, toggle); 1912 pci_read_config_word(dev, PCI_COMMAND, &new); 1913 1914 pci_write_config_word(dev, PCI_COMMAND, orig); 1915 1916 /* 1917 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI 1918 * r2.3, so strictly speaking, a device is not *broken* if it's not 1919 * writable. But we'll live with the misnomer for now. 1920 */ 1921 if (new != toggle) 1922 return 1; 1923 return 0; 1924 } 1925 1926 static void early_dump_pci_device(struct pci_dev *pdev) 1927 { 1928 u32 value[PCI_CFG_SPACE_SIZE / sizeof(u32)]; 1929 int i; 1930 1931 pci_info(pdev, "config space:\n"); 1932 1933 for (i = 0; i < ARRAY_SIZE(value); i++) 1934 pci_read_config_dword(pdev, i * sizeof(u32), &value[i]); 1935 1936 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, 1937 value, ARRAY_SIZE(value) * sizeof(u32), false); 1938 } 1939 1940 static const char *pci_type_str(struct pci_dev *dev) 1941 { 1942 static const char * const str[] = { 1943 "PCIe Endpoint", 1944 "PCIe Legacy Endpoint", 1945 "PCIe unknown", 1946 "PCIe unknown", 1947 "PCIe Root Port", 1948 "PCIe Switch Upstream Port", 1949 "PCIe Switch Downstream Port", 1950 "PCIe to PCI/PCI-X bridge", 1951 "PCI/PCI-X to PCIe bridge", 1952 "PCIe Root Complex Integrated Endpoint", 1953 "PCIe Root Complex Event Collector", 1954 }; 1955 int type; 1956 1957 if (pci_is_pcie(dev)) { 1958 type = pci_pcie_type(dev); 1959 if (type < ARRAY_SIZE(str)) 1960 return str[type]; 1961 1962 return "PCIe unknown"; 1963 } 1964 1965 switch (dev->hdr_type) { 1966 case PCI_HEADER_TYPE_NORMAL: 1967 return "conventional PCI endpoint"; 1968 case PCI_HEADER_TYPE_BRIDGE: 1969 return "conventional PCI bridge"; 1970 case PCI_HEADER_TYPE_CARDBUS: 1971 return "CardBus bridge"; 1972 default: 1973 return "conventional PCI"; 1974 } 1975 } 1976 1977 /** 1978 * pci_setup_device - Fill in class and map information of a device 1979 * @dev: the device structure to fill 1980 * 1981 * Initialize the device structure with information about the device's 1982 * vendor,class,memory and IO-space addresses, IRQ lines etc. 1983 * Called at initialisation of the PCI subsystem and by CardBus services. 1984 * Returns 0 on success and negative if unknown type of device (not normal, 1985 * bridge or CardBus). 1986 */ 1987 int pci_setup_device(struct pci_dev *dev) 1988 { 1989 u32 class; 1990 u16 cmd; 1991 u8 hdr_type; 1992 int err, pos = 0; 1993 struct pci_bus_region region; 1994 struct resource *res; 1995 1996 hdr_type = pci_hdr_type(dev); 1997 1998 dev->sysdata = dev->bus->sysdata; 1999 dev->dev.parent = dev->bus->bridge; 2000 dev->dev.bus = &pci_bus_type; 2001 dev->hdr_type = FIELD_GET(PCI_HEADER_TYPE_MASK, hdr_type); 2002 dev->multifunction = FIELD_GET(PCI_HEADER_TYPE_MFD, hdr_type); 2003 dev->error_state = pci_channel_io_normal; 2004 set_pcie_port_type(dev); 2005 2006 err = pci_set_of_node(dev); 2007 if (err) 2008 return err; 2009 pci_set_acpi_fwnode(dev); 2010 2011 pci_dev_assign_slot(dev); 2012 2013 /* 2014 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer) 2015 * set this higher, assuming the system even supports it. 2016 */ 2017 dev->dma_mask = 0xffffffff; 2018 2019 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus), 2020 dev->bus->number, PCI_SLOT(dev->devfn), 2021 PCI_FUNC(dev->devfn)); 2022 2023 class = pci_class(dev); 2024 2025 dev->revision = class & 0xff; 2026 dev->class = class >> 8; /* upper 3 bytes */ 2027 2028 if (pci_early_dump) 2029 early_dump_pci_device(dev); 2030 2031 /* Need to have dev->class ready */ 2032 dev->cfg_size = pci_cfg_space_size(dev); 2033 2034 /* Need to have dev->cfg_size ready */ 2035 set_pcie_thunderbolt(dev); 2036 2037 set_pcie_untrusted(dev); 2038 2039 if (pci_is_pcie(dev)) 2040 dev->supported_speeds = pcie_get_supported_speeds(dev); 2041 2042 /* "Unknown power state" */ 2043 dev->current_state = PCI_UNKNOWN; 2044 2045 /* Early fixups, before probing the BARs */ 2046 pci_fixup_device(pci_fixup_early, dev); 2047 2048 pci_set_removable(dev); 2049 2050 pci_info(dev, "[%04x:%04x] type %02x class %#08x %s\n", 2051 dev->vendor, dev->device, dev->hdr_type, dev->class, 2052 pci_type_str(dev)); 2053 2054 /* Device class may be changed after fixup */ 2055 class = dev->class >> 8; 2056 2057 if (dev->non_compliant_bars && !dev->mmio_always_on) { 2058 pci_read_config_word(dev, PCI_COMMAND, &cmd); 2059 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) { 2060 pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n"); 2061 cmd &= ~PCI_COMMAND_IO; 2062 cmd &= ~PCI_COMMAND_MEMORY; 2063 pci_write_config_word(dev, PCI_COMMAND, cmd); 2064 } 2065 } 2066 2067 dev->broken_intx_masking = pci_intx_mask_broken(dev); 2068 2069 switch (dev->hdr_type) { /* header type */ 2070 case PCI_HEADER_TYPE_NORMAL: /* standard header */ 2071 if (class == PCI_CLASS_BRIDGE_PCI) 2072 goto bad; 2073 pci_read_irq(dev); 2074 pci_read_bases(dev, PCI_STD_NUM_BARS, PCI_ROM_ADDRESS); 2075 2076 pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device); 2077 2078 /* 2079 * Do the ugly legacy mode stuff here rather than broken chip 2080 * quirk code. Legacy mode ATA controllers have fixed 2081 * addresses. These are not always echoed in BAR0-3, and 2082 * BAR0-3 in a few cases contain junk! 2083 */ 2084 if (class == PCI_CLASS_STORAGE_IDE) { 2085 u8 progif; 2086 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif); 2087 if ((progif & 1) == 0) { 2088 region.start = 0x1F0; 2089 region.end = 0x1F7; 2090 res = &dev->resource[0]; 2091 res->flags = LEGACY_IO_RESOURCE; 2092 pcibios_bus_to_resource(dev->bus, res, ®ion); 2093 pci_info(dev, "BAR 0 %pR: legacy IDE quirk\n", 2094 res); 2095 region.start = 0x3F6; 2096 region.end = 0x3F6; 2097 res = &dev->resource[1]; 2098 res->flags = LEGACY_IO_RESOURCE; 2099 pcibios_bus_to_resource(dev->bus, res, ®ion); 2100 pci_info(dev, "BAR 1 %pR: legacy IDE quirk\n", 2101 res); 2102 } 2103 if ((progif & 4) == 0) { 2104 region.start = 0x170; 2105 region.end = 0x177; 2106 res = &dev->resource[2]; 2107 res->flags = LEGACY_IO_RESOURCE; 2108 pcibios_bus_to_resource(dev->bus, res, ®ion); 2109 pci_info(dev, "BAR 2 %pR: legacy IDE quirk\n", 2110 res); 2111 region.start = 0x376; 2112 region.end = 0x376; 2113 res = &dev->resource[3]; 2114 res->flags = LEGACY_IO_RESOURCE; 2115 pcibios_bus_to_resource(dev->bus, res, ®ion); 2116 pci_info(dev, "BAR 3 %pR: legacy IDE quirk\n", 2117 res); 2118 } 2119 } 2120 break; 2121 2122 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */ 2123 /* 2124 * The PCI-to-PCI bridge spec requires that subtractive 2125 * decoding (i.e. transparent) bridge must have programming 2126 * interface code of 0x01. 2127 */ 2128 pci_read_irq(dev); 2129 dev->transparent = ((dev->class & 0xff) == 1); 2130 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1); 2131 pci_read_bridge_windows(dev); 2132 set_pcie_hotplug_bridge(dev); 2133 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID); 2134 if (pos) { 2135 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor); 2136 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device); 2137 } 2138 break; 2139 2140 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */ 2141 if (class != PCI_CLASS_BRIDGE_CARDBUS) 2142 goto bad; 2143 pci_read_irq(dev); 2144 pci_read_bases(dev, 1, 0); 2145 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor); 2146 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device); 2147 break; 2148 2149 default: /* unknown header */ 2150 pci_err(dev, "unknown header type %02x, ignoring device\n", 2151 dev->hdr_type); 2152 pci_release_of_node(dev); 2153 return -EIO; 2154 2155 bad: 2156 pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n", 2157 dev->class, dev->hdr_type); 2158 dev->class = PCI_CLASS_NOT_DEFINED << 8; 2159 } 2160 2161 /* We found a fine healthy device, go go go... */ 2162 return 0; 2163 } 2164 2165 static void pci_configure_mps(struct pci_dev *dev) 2166 { 2167 struct pci_dev *bridge = pci_upstream_bridge(dev); 2168 int mps, mpss, p_mps, rc; 2169 2170 if (!pci_is_pcie(dev)) 2171 return; 2172 2173 /* MPS and MRRS fields are of type 'RsvdP' for VFs, short-circuit out */ 2174 if (dev->is_virtfn) 2175 return; 2176 2177 /* 2178 * For Root Complex Integrated Endpoints, program the maximum 2179 * supported value unless limited by the PCIE_BUS_PEER2PEER case. 2180 */ 2181 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) { 2182 if (pcie_bus_config == PCIE_BUS_PEER2PEER) 2183 mps = 128; 2184 else 2185 mps = 128 << dev->pcie_mpss; 2186 rc = pcie_set_mps(dev, mps); 2187 if (rc) { 2188 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n", 2189 mps); 2190 } 2191 return; 2192 } 2193 2194 if (!bridge || !pci_is_pcie(bridge)) 2195 return; 2196 2197 mps = pcie_get_mps(dev); 2198 p_mps = pcie_get_mps(bridge); 2199 2200 if (mps == p_mps) 2201 return; 2202 2203 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) { 2204 pci_warn(dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n", 2205 mps, pci_name(bridge), p_mps); 2206 return; 2207 } 2208 2209 /* 2210 * Fancier MPS configuration is done later by 2211 * pcie_bus_configure_settings() 2212 */ 2213 if (pcie_bus_config != PCIE_BUS_DEFAULT) 2214 return; 2215 2216 mpss = 128 << dev->pcie_mpss; 2217 if (mpss < p_mps && pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT) { 2218 pcie_set_mps(bridge, mpss); 2219 pci_info(dev, "Upstream bridge's Max Payload Size set to %d (was %d, max %d)\n", 2220 mpss, p_mps, 128 << bridge->pcie_mpss); 2221 p_mps = pcie_get_mps(bridge); 2222 } 2223 2224 rc = pcie_set_mps(dev, p_mps); 2225 if (rc) { 2226 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n", 2227 p_mps); 2228 return; 2229 } 2230 2231 pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n", 2232 p_mps, mps, mpss); 2233 } 2234 2235 int pci_configure_extended_tags(struct pci_dev *dev, void *ign) 2236 { 2237 struct pci_host_bridge *host; 2238 u32 cap; 2239 u16 ctl; 2240 int ret; 2241 2242 /* PCI_EXP_DEVCTL_EXT_TAG is RsvdP in VFs */ 2243 if (!pci_is_pcie(dev) || dev->is_virtfn) 2244 return 0; 2245 2246 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap); 2247 if (ret) 2248 return 0; 2249 2250 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG)) 2251 return 0; 2252 2253 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl); 2254 if (ret) 2255 return 0; 2256 2257 host = pci_find_host_bridge(dev->bus); 2258 if (!host) 2259 return 0; 2260 2261 /* 2262 * If some device in the hierarchy doesn't handle Extended Tags 2263 * correctly, make sure they're disabled. 2264 */ 2265 if (host->no_ext_tags) { 2266 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) { 2267 pci_info(dev, "disabling Extended Tags\n"); 2268 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL, 2269 PCI_EXP_DEVCTL_EXT_TAG); 2270 } 2271 return 0; 2272 } 2273 2274 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) { 2275 pci_info(dev, "enabling Extended Tags\n"); 2276 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, 2277 PCI_EXP_DEVCTL_EXT_TAG); 2278 } 2279 return 0; 2280 } 2281 2282 static void pci_dev3_init(struct pci_dev *pdev) 2283 { 2284 u16 cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DEV3); 2285 u32 val = 0; 2286 2287 if (!cap) 2288 return; 2289 pci_read_config_dword(pdev, cap + PCI_DEV3_STA, &val); 2290 pdev->fm_enabled = !!(val & PCI_DEV3_STA_SEGMENT); 2291 } 2292 2293 /** 2294 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable 2295 * @dev: PCI device to query 2296 * 2297 * Returns true if the device has enabled relaxed ordering attribute. 2298 */ 2299 bool pcie_relaxed_ordering_enabled(struct pci_dev *dev) 2300 { 2301 u16 v; 2302 2303 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v); 2304 2305 return !!(v & PCI_EXP_DEVCTL_RELAX_EN); 2306 } 2307 EXPORT_SYMBOL(pcie_relaxed_ordering_enabled); 2308 2309 static void pci_configure_relaxed_ordering(struct pci_dev *dev) 2310 { 2311 struct pci_dev *root; 2312 2313 /* PCI_EXP_DEVCTL_RELAX_EN is RsvdP in VFs */ 2314 if (dev->is_virtfn) 2315 return; 2316 2317 if (!pcie_relaxed_ordering_enabled(dev)) 2318 return; 2319 2320 /* 2321 * For now, we only deal with Relaxed Ordering issues with Root 2322 * Ports. Peer-to-Peer DMA is another can of worms. 2323 */ 2324 root = pcie_find_root_port(dev); 2325 if (!root) 2326 return; 2327 2328 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) { 2329 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL, 2330 PCI_EXP_DEVCTL_RELAX_EN); 2331 pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n"); 2332 } 2333 } 2334 2335 static void pci_configure_eetlp_prefix(struct pci_dev *dev) 2336 { 2337 struct pci_dev *bridge; 2338 unsigned int eetlp_max; 2339 int pcie_type; 2340 u32 cap; 2341 2342 if (!pci_is_pcie(dev)) 2343 return; 2344 2345 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap); 2346 if (!(cap & PCI_EXP_DEVCAP2_EE_PREFIX)) 2347 return; 2348 2349 pcie_type = pci_pcie_type(dev); 2350 2351 eetlp_max = FIELD_GET(PCI_EXP_DEVCAP2_EE_PREFIX_MAX, cap); 2352 /* 00b means 4 */ 2353 eetlp_max = eetlp_max ?: 4; 2354 2355 if (pcie_type == PCI_EXP_TYPE_ROOT_PORT || 2356 pcie_type == PCI_EXP_TYPE_RC_END) 2357 dev->eetlp_prefix_max = eetlp_max; 2358 else { 2359 bridge = pci_upstream_bridge(dev); 2360 if (bridge && bridge->eetlp_prefix_max) 2361 dev->eetlp_prefix_max = eetlp_max; 2362 } 2363 } 2364 2365 static void pci_configure_serr(struct pci_dev *dev) 2366 { 2367 u16 control; 2368 2369 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) { 2370 2371 /* 2372 * A bridge will not forward ERR_ messages coming from an 2373 * endpoint unless SERR# forwarding is enabled. 2374 */ 2375 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &control); 2376 if (!(control & PCI_BRIDGE_CTL_SERR)) { 2377 control |= PCI_BRIDGE_CTL_SERR; 2378 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, control); 2379 } 2380 } 2381 } 2382 2383 static void pci_configure_rcb(struct pci_dev *dev) 2384 { 2385 struct pci_dev *rp; 2386 u16 rp_lnkctl; 2387 2388 /* 2389 * Per PCIe r7.0, sec 7.5.3.7, RCB is only meaningful in Root Ports 2390 * (where it is read-only), Endpoints, and Bridges. It may only be 2391 * set for Endpoints and Bridges if it is set in the Root Port. For 2392 * Endpoints, it is 'RsvdP' for Virtual Functions. 2393 */ 2394 if (!pci_is_pcie(dev) || 2395 pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT || 2396 pci_pcie_type(dev) == PCI_EXP_TYPE_UPSTREAM || 2397 pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM || 2398 pci_pcie_type(dev) == PCI_EXP_TYPE_RC_EC || 2399 dev->is_virtfn) 2400 return; 2401 2402 /* Root Port often not visible to virtualized guests */ 2403 rp = pcie_find_root_port(dev); 2404 if (!rp) 2405 return; 2406 2407 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &rp_lnkctl); 2408 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL, 2409 PCI_EXP_LNKCTL_RCB, 2410 (rp_lnkctl & PCI_EXP_LNKCTL_RCB) ? 2411 PCI_EXP_LNKCTL_RCB : 0); 2412 } 2413 2414 static void pci_configure_device(struct pci_dev *dev) 2415 { 2416 pci_configure_mps(dev); 2417 pci_configure_extended_tags(dev, NULL); 2418 pci_configure_relaxed_ordering(dev); 2419 pci_configure_ltr(dev); 2420 pci_configure_aspm_l1ss(dev); 2421 pci_configure_eetlp_prefix(dev); 2422 pci_configure_serr(dev); 2423 pci_configure_rcb(dev); 2424 2425 pci_acpi_program_hp_params(dev); 2426 } 2427 2428 static void pci_release_capabilities(struct pci_dev *dev) 2429 { 2430 pci_aer_exit(dev); 2431 pci_rcec_exit(dev); 2432 pci_iov_release(dev); 2433 pci_free_cap_save_buffers(dev); 2434 } 2435 2436 /** 2437 * pci_release_dev - Free a PCI device structure when all users of it are 2438 * finished 2439 * @dev: device that's been disconnected 2440 * 2441 * Will be called only by the device core when all users of this PCI device are 2442 * done. 2443 */ 2444 static void pci_release_dev(struct device *dev) 2445 { 2446 struct pci_dev *pci_dev; 2447 2448 pci_dev = to_pci_dev(dev); 2449 pci_release_capabilities(pci_dev); 2450 pci_release_of_node(pci_dev); 2451 pcibios_release_device(pci_dev); 2452 pci_bus_put(pci_dev->bus); 2453 kfree(pci_dev->driver_override); 2454 bitmap_free(pci_dev->dma_alias_mask); 2455 dev_dbg(dev, "device released\n"); 2456 kfree(pci_dev); 2457 } 2458 2459 static const struct device_type pci_dev_type = { 2460 .groups = pci_dev_attr_groups, 2461 }; 2462 2463 struct pci_dev *pci_alloc_dev(struct pci_bus *bus) 2464 { 2465 struct pci_dev *dev; 2466 2467 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL); 2468 if (!dev) 2469 return NULL; 2470 2471 INIT_LIST_HEAD(&dev->bus_list); 2472 dev->dev.type = &pci_dev_type; 2473 dev->bus = pci_bus_get(bus); 2474 dev->driver_exclusive_resource = (struct resource) { 2475 .name = "PCI Exclusive", 2476 .start = 0, 2477 .end = -1, 2478 }; 2479 2480 spin_lock_init(&dev->pcie_cap_lock); 2481 #ifdef CONFIG_PCI_MSI 2482 raw_spin_lock_init(&dev->msi_lock); 2483 #endif 2484 return dev; 2485 } 2486 EXPORT_SYMBOL(pci_alloc_dev); 2487 2488 static bool pci_bus_wait_rrs(struct pci_bus *bus, int devfn, u32 *l, 2489 int timeout) 2490 { 2491 int delay = 1; 2492 2493 if (!pci_bus_rrs_vendor_id(*l)) 2494 return true; /* not a Configuration RRS completion */ 2495 2496 if (!timeout) 2497 return false; /* RRS, but caller doesn't want to wait */ 2498 2499 /* 2500 * We got the reserved Vendor ID that indicates a completion with 2501 * Configuration Request Retry Status (RRS). Retry until we get a 2502 * valid Vendor ID or we time out. 2503 */ 2504 while (pci_bus_rrs_vendor_id(*l)) { 2505 if (delay > timeout) { 2506 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n", 2507 pci_domain_nr(bus), bus->number, 2508 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1); 2509 2510 return false; 2511 } 2512 if (delay >= 1000) 2513 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n", 2514 pci_domain_nr(bus), bus->number, 2515 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1); 2516 2517 msleep(delay); 2518 delay *= 2; 2519 2520 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l)) 2521 return false; 2522 } 2523 2524 if (delay >= 1000) 2525 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n", 2526 pci_domain_nr(bus), bus->number, 2527 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1); 2528 2529 return true; 2530 } 2531 2532 bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l, 2533 int timeout) 2534 { 2535 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l)) 2536 return false; 2537 2538 /* Some broken boards return 0 or ~0 (PCI_ERROR_RESPONSE) if a slot is empty: */ 2539 if (PCI_POSSIBLE_ERROR(*l) || *l == 0x00000000 || 2540 *l == 0x0000ffff || *l == 0xffff0000) 2541 return false; 2542 2543 if (pci_bus_rrs_vendor_id(*l)) 2544 return pci_bus_wait_rrs(bus, devfn, l, timeout); 2545 2546 return true; 2547 } 2548 2549 bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l, 2550 int timeout) 2551 { 2552 return pci_bus_generic_read_dev_vendor_id(bus, devfn, l, timeout); 2553 } 2554 EXPORT_SYMBOL(pci_bus_read_dev_vendor_id); 2555 2556 /* 2557 * Read the config data for a PCI device, sanity-check it, 2558 * and fill in the dev structure. 2559 */ 2560 static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn) 2561 { 2562 struct pci_dev *dev; 2563 u32 l; 2564 2565 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000)) 2566 return NULL; 2567 2568 dev = pci_alloc_dev(bus); 2569 if (!dev) 2570 return NULL; 2571 2572 dev->devfn = devfn; 2573 dev->vendor = l & 0xffff; 2574 dev->device = (l >> 16) & 0xffff; 2575 2576 if (pci_setup_device(dev)) { 2577 pci_bus_put(dev->bus); 2578 kfree(dev); 2579 return NULL; 2580 } 2581 2582 return dev; 2583 } 2584 2585 void pcie_report_downtraining(struct pci_dev *dev) 2586 { 2587 if (!pci_is_pcie(dev)) 2588 return; 2589 2590 /* Look from the device up to avoid downstream ports with no devices */ 2591 if ((pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT) && 2592 (pci_pcie_type(dev) != PCI_EXP_TYPE_LEG_END) && 2593 (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM)) 2594 return; 2595 2596 /* Multi-function PCIe devices share the same link/status */ 2597 if (PCI_FUNC(dev->devfn) != 0 || dev->is_virtfn) 2598 return; 2599 2600 /* Print link status only if the device is constrained by the fabric */ 2601 __pcie_print_link_status(dev, false); 2602 } 2603 2604 static void pci_imm_ready_init(struct pci_dev *dev) 2605 { 2606 u16 status; 2607 2608 pci_read_config_word(dev, PCI_STATUS, &status); 2609 if (status & PCI_STATUS_IMM_READY) 2610 dev->imm_ready = 1; 2611 } 2612 2613 static void pci_init_capabilities(struct pci_dev *dev) 2614 { 2615 pci_ea_init(dev); /* Enhanced Allocation */ 2616 pci_msi_init(dev); /* Disable MSI */ 2617 pci_msix_init(dev); /* Disable MSI-X */ 2618 2619 /* Buffers for saving PCIe and PCI-X capabilities */ 2620 pci_allocate_cap_save_buffers(dev); 2621 2622 pci_imm_ready_init(dev); /* Immediate Readiness */ 2623 pci_pm_init(dev); /* Power Management */ 2624 pci_vpd_init(dev); /* Vital Product Data */ 2625 pci_configure_ari(dev); /* Alternative Routing-ID Forwarding */ 2626 pci_iov_init(dev); /* Single Root I/O Virtualization */ 2627 pci_ats_init(dev); /* Address Translation Services */ 2628 pci_pri_init(dev); /* Page Request Interface */ 2629 pci_pasid_init(dev); /* Process Address Space ID */ 2630 pci_acs_init(dev); /* Access Control Services */ 2631 pci_ptm_init(dev); /* Precision Time Measurement */ 2632 pci_aer_init(dev); /* Advanced Error Reporting */ 2633 pci_dpc_init(dev); /* Downstream Port Containment */ 2634 pci_rcec_init(dev); /* Root Complex Event Collector */ 2635 pci_doe_init(dev); /* Data Object Exchange */ 2636 pci_tph_init(dev); /* TLP Processing Hints */ 2637 pci_rebar_init(dev); /* Resizable BAR */ 2638 pci_dev3_init(dev); /* Device 3 capabilities */ 2639 pci_ide_init(dev); /* Link Integrity and Data Encryption */ 2640 2641 pcie_report_downtraining(dev); 2642 pci_init_reset_methods(dev); 2643 } 2644 2645 /* 2646 * This is the equivalent of pci_host_bridge_msi_domain() that acts on 2647 * devices. Firmware interfaces that can select the MSI domain on a 2648 * per-device basis should be called from here. 2649 */ 2650 static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev) 2651 { 2652 struct irq_domain *d; 2653 2654 /* 2655 * If a domain has been set through the pcibios_device_add() 2656 * callback, then this is the one (platform code knows best). 2657 */ 2658 d = dev_get_msi_domain(&dev->dev); 2659 if (d) 2660 return d; 2661 2662 /* 2663 * Let's see if we have a firmware interface able to provide 2664 * the domain. 2665 */ 2666 d = pci_msi_get_device_domain(dev); 2667 if (d) 2668 return d; 2669 2670 return NULL; 2671 } 2672 2673 static void pci_set_msi_domain(struct pci_dev *dev) 2674 { 2675 struct irq_domain *d; 2676 2677 /* 2678 * If the platform or firmware interfaces cannot supply a 2679 * device-specific MSI domain, then inherit the default domain 2680 * from the host bridge itself. 2681 */ 2682 d = pci_dev_msi_domain(dev); 2683 if (!d) 2684 d = dev_get_msi_domain(&dev->bus->dev); 2685 2686 dev_set_msi_domain(&dev->dev, d); 2687 } 2688 2689 void pci_device_add(struct pci_dev *dev, struct pci_bus *bus) 2690 { 2691 int ret; 2692 2693 pci_configure_device(dev); 2694 2695 device_initialize(&dev->dev); 2696 dev->dev.release = pci_release_dev; 2697 2698 set_dev_node(&dev->dev, pcibus_to_node(bus)); 2699 dev->dev.dma_mask = &dev->dma_mask; 2700 dev->dev.dma_parms = &dev->dma_parms; 2701 dev->dev.coherent_dma_mask = 0xffffffffull; 2702 2703 dma_set_max_seg_size(&dev->dev, 65536); 2704 dma_set_seg_boundary(&dev->dev, 0xffffffff); 2705 2706 pcie_failed_link_retrain(dev); 2707 2708 /* Fix up broken headers */ 2709 pci_fixup_device(pci_fixup_header, dev); 2710 2711 pci_reassigndev_resource_alignment(dev); 2712 2713 pci_init_capabilities(dev); 2714 2715 /* 2716 * Add the device to our list of discovered devices 2717 * and the bus list for fixup functions, etc. 2718 */ 2719 down_write(&pci_bus_sem); 2720 list_add_tail(&dev->bus_list, &bus->devices); 2721 up_write(&pci_bus_sem); 2722 2723 ret = pcibios_device_add(dev); 2724 WARN_ON(ret < 0); 2725 2726 /* Set up MSI IRQ domain */ 2727 pci_set_msi_domain(dev); 2728 2729 /* Notifier could use PCI capabilities */ 2730 ret = device_add(&dev->dev); 2731 WARN_ON(ret < 0); 2732 2733 /* Establish pdev->tsm for newly added (e.g. new SR-IOV VFs) */ 2734 pci_tsm_init(dev); 2735 2736 pci_npem_create(dev); 2737 2738 pci_doe_sysfs_init(dev); 2739 } 2740 2741 struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn) 2742 { 2743 struct pci_dev *dev; 2744 2745 dev = pci_get_slot(bus, devfn); 2746 if (dev) { 2747 pci_dev_put(dev); 2748 return dev; 2749 } 2750 2751 dev = pci_scan_device(bus, devfn); 2752 if (!dev) 2753 return NULL; 2754 2755 pci_device_add(dev, bus); 2756 2757 return dev; 2758 } 2759 EXPORT_SYMBOL(pci_scan_single_device); 2760 2761 static int next_ari_fn(struct pci_bus *bus, struct pci_dev *dev, int fn) 2762 { 2763 int pos; 2764 u16 cap = 0; 2765 unsigned int next_fn; 2766 2767 if (!dev) 2768 return -ENODEV; 2769 2770 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI); 2771 if (!pos) 2772 return -ENODEV; 2773 2774 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap); 2775 next_fn = PCI_ARI_CAP_NFN(cap); 2776 if (next_fn <= fn) 2777 return -ENODEV; /* protect against malformed list */ 2778 2779 return next_fn; 2780 } 2781 2782 static int next_fn(struct pci_bus *bus, struct pci_dev *dev, int fn) 2783 { 2784 if (pci_ari_enabled(bus)) 2785 return next_ari_fn(bus, dev, fn); 2786 2787 if (fn >= 7) 2788 return -ENODEV; 2789 /* only multifunction devices may have more functions */ 2790 if (dev && !dev->multifunction) 2791 return -ENODEV; 2792 2793 return fn + 1; 2794 } 2795 2796 static int only_one_child(struct pci_bus *bus) 2797 { 2798 struct pci_dev *bridge = bus->self; 2799 2800 /* 2801 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so 2802 * we scan for all possible devices, not just Device 0. 2803 */ 2804 if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS)) 2805 return 0; 2806 2807 /* 2808 * A PCIe Downstream Port normally leads to a Link with only Device 2809 * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan 2810 * only for Device 0 in that situation. 2811 */ 2812 if (bridge && pci_is_pcie(bridge) && pcie_downstream_port(bridge)) 2813 return 1; 2814 2815 return 0; 2816 } 2817 2818 /** 2819 * pci_scan_slot - Scan a PCI slot on a bus for devices 2820 * @bus: PCI bus to scan 2821 * @devfn: slot number to scan (must have zero function) 2822 * 2823 * Scan a PCI slot on the specified PCI bus for devices, adding 2824 * discovered devices to the @bus->devices list. New devices 2825 * will not have is_added set. 2826 * 2827 * Returns the number of new devices found. 2828 */ 2829 int pci_scan_slot(struct pci_bus *bus, int devfn) 2830 { 2831 struct pci_dev *dev; 2832 int fn = 0, nr = 0; 2833 2834 if (only_one_child(bus) && (devfn > 0)) 2835 return 0; /* Already scanned the entire slot */ 2836 2837 do { 2838 dev = pci_scan_single_device(bus, devfn + fn); 2839 if (dev) { 2840 if (!pci_dev_is_added(dev)) 2841 nr++; 2842 if (fn > 0) 2843 dev->multifunction = 1; 2844 } else if (fn == 0) { 2845 /* 2846 * Function 0 is required unless we are running on 2847 * a hypervisor that passes through individual PCI 2848 * functions. 2849 */ 2850 if (!hypervisor_isolated_pci_functions()) 2851 break; 2852 } 2853 fn = next_fn(bus, dev, fn); 2854 } while (fn >= 0); 2855 2856 /* Only one slot has PCIe device */ 2857 if (bus->self && nr) 2858 pcie_aspm_init_link_state(bus->self); 2859 2860 return nr; 2861 } 2862 EXPORT_SYMBOL(pci_scan_slot); 2863 2864 static int pcie_find_smpss(struct pci_dev *dev, void *data) 2865 { 2866 u8 *smpss = data; 2867 2868 if (!pci_is_pcie(dev)) 2869 return 0; 2870 2871 /* 2872 * We don't have a way to change MPS settings on devices that have 2873 * drivers attached. A hot-added device might support only the minimum 2874 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge 2875 * where devices may be hot-added, we limit the fabric MPS to 128 so 2876 * hot-added devices will work correctly. 2877 * 2878 * However, if we hot-add a device to a slot directly below a Root 2879 * Port, it's impossible for there to be other existing devices below 2880 * the port. We don't limit the MPS in this case because we can 2881 * reconfigure MPS on both the Root Port and the hot-added device, 2882 * and there are no other devices involved. 2883 * 2884 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA. 2885 */ 2886 if (dev->is_hotplug_bridge && 2887 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT) 2888 *smpss = 0; 2889 2890 if (*smpss > dev->pcie_mpss) 2891 *smpss = dev->pcie_mpss; 2892 2893 return 0; 2894 } 2895 2896 static void pcie_write_mps(struct pci_dev *dev, int mps) 2897 { 2898 int rc; 2899 2900 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) { 2901 mps = 128 << dev->pcie_mpss; 2902 2903 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT && 2904 dev->bus->self) 2905 2906 /* 2907 * For "Performance", the assumption is made that 2908 * downstream communication will never be larger than 2909 * the MRRS. So, the MPS only needs to be configured 2910 * for the upstream communication. This being the case, 2911 * walk from the top down and set the MPS of the child 2912 * to that of the parent bus. 2913 * 2914 * Configure the device MPS with the smaller of the 2915 * device MPSS or the bridge MPS (which is assumed to be 2916 * properly configured at this point to the largest 2917 * allowable MPS based on its parent bus). 2918 */ 2919 mps = min(mps, pcie_get_mps(dev->bus->self)); 2920 } 2921 2922 rc = pcie_set_mps(dev, mps); 2923 if (rc) 2924 pci_err(dev, "Failed attempting to set the MPS\n"); 2925 } 2926 2927 static void pcie_write_mrrs(struct pci_dev *dev) 2928 { 2929 int rc, mrrs; 2930 2931 /* 2932 * In the "safe" case, do not configure the MRRS. There appear to be 2933 * issues with setting MRRS to 0 on a number of devices. 2934 */ 2935 if (pcie_bus_config != PCIE_BUS_PERFORMANCE) 2936 return; 2937 2938 /* 2939 * For max performance, the MRRS must be set to the largest supported 2940 * value. However, it cannot be configured larger than the MPS the 2941 * device or the bus can support. This should already be properly 2942 * configured by a prior call to pcie_write_mps(). 2943 */ 2944 mrrs = pcie_get_mps(dev); 2945 2946 /* 2947 * MRRS is a R/W register. Invalid values can be written, but a 2948 * subsequent read will verify if the value is acceptable or not. 2949 * If the MRRS value provided is not acceptable (e.g., too large), 2950 * shrink the value until it is acceptable to the HW. 2951 */ 2952 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) { 2953 rc = pcie_set_readrq(dev, mrrs); 2954 if (!rc) 2955 break; 2956 2957 pci_warn(dev, "Failed attempting to set the MRRS\n"); 2958 mrrs /= 2; 2959 } 2960 2961 if (mrrs < 128) 2962 pci_err(dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n"); 2963 } 2964 2965 static int pcie_bus_configure_set(struct pci_dev *dev, void *data) 2966 { 2967 int mps, orig_mps; 2968 2969 if (!pci_is_pcie(dev)) 2970 return 0; 2971 2972 if (pcie_bus_config == PCIE_BUS_TUNE_OFF || 2973 pcie_bus_config == PCIE_BUS_DEFAULT) 2974 return 0; 2975 2976 mps = 128 << *(u8 *)data; 2977 orig_mps = pcie_get_mps(dev); 2978 2979 pcie_write_mps(dev, mps); 2980 pcie_write_mrrs(dev); 2981 2982 pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n", 2983 pcie_get_mps(dev), 128 << dev->pcie_mpss, 2984 orig_mps, pcie_get_readrq(dev)); 2985 2986 return 0; 2987 } 2988 2989 /* 2990 * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down, 2991 * parents then children fashion. If this changes, then this code will not 2992 * work as designed. 2993 */ 2994 void pcie_bus_configure_settings(struct pci_bus *bus) 2995 { 2996 u8 smpss = 0; 2997 2998 if (!bus->self) 2999 return; 3000 3001 if (!pci_is_pcie(bus->self)) 3002 return; 3003 3004 /* 3005 * FIXME - Peer to peer DMA is possible, though the endpoint would need 3006 * to be aware of the MPS of the destination. To work around this, 3007 * simply force the MPS of the entire system to the smallest possible. 3008 */ 3009 if (pcie_bus_config == PCIE_BUS_PEER2PEER) 3010 smpss = 0; 3011 3012 if (pcie_bus_config == PCIE_BUS_SAFE) { 3013 smpss = bus->self->pcie_mpss; 3014 3015 pcie_find_smpss(bus->self, &smpss); 3016 pci_walk_bus(bus, pcie_find_smpss, &smpss); 3017 } 3018 3019 pcie_bus_configure_set(bus->self, &smpss); 3020 pci_walk_bus(bus, pcie_bus_configure_set, &smpss); 3021 } 3022 EXPORT_SYMBOL_GPL(pcie_bus_configure_settings); 3023 3024 /* 3025 * Called after each bus is probed, but before its children are examined. This 3026 * is marked as __weak because multiple architectures define it. 3027 */ 3028 void __weak pcibios_fixup_bus(struct pci_bus *bus) 3029 { 3030 /* nothing to do, expected to be removed in the future */ 3031 } 3032 3033 /** 3034 * pci_scan_child_bus_extend() - Scan devices below a bus 3035 * @bus: Bus to scan for devices 3036 * @available_buses: Total number of buses available (%0 does not try to 3037 * extend beyond the minimal) 3038 * 3039 * Scans devices below @bus including subordinate buses. Returns new 3040 * subordinate number including all the found devices. Passing 3041 * @available_buses causes the remaining bus space to be distributed 3042 * equally between hotplug-capable bridges to allow future extension of the 3043 * hierarchy. 3044 */ 3045 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus, 3046 unsigned int available_buses) 3047 { 3048 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0; 3049 unsigned int start = bus->busn_res.start; 3050 unsigned int devnr, cmax, max = start; 3051 struct pci_dev *dev; 3052 3053 dev_dbg(&bus->dev, "scanning bus\n"); 3054 3055 /* Go find them, Rover! */ 3056 for (devnr = 0; devnr < PCI_MAX_NR_DEVS; devnr++) 3057 pci_scan_slot(bus, PCI_DEVFN(devnr, 0)); 3058 3059 /* Reserve buses for SR-IOV capability */ 3060 used_buses = pci_iov_bus_range(bus); 3061 max += used_buses; 3062 3063 /* 3064 * After performing arch-dependent fixup of the bus, look behind 3065 * all PCI-to-PCI bridges on this bus. 3066 */ 3067 if (!bus->is_added) { 3068 dev_dbg(&bus->dev, "fixups for bus\n"); 3069 pcibios_fixup_bus(bus); 3070 bus->is_added = 1; 3071 } 3072 3073 /* 3074 * Calculate how many hotplug bridges and normal bridges there 3075 * are on this bus. We will distribute the additional available 3076 * buses between hotplug bridges. 3077 */ 3078 for_each_pci_bridge(dev, bus) { 3079 if (dev->is_hotplug_bridge) 3080 hotplug_bridges++; 3081 else 3082 normal_bridges++; 3083 } 3084 3085 /* 3086 * Scan bridges that are already configured. We don't touch them 3087 * unless they are misconfigured (which will be done in the second 3088 * scan below). 3089 */ 3090 for_each_pci_bridge(dev, bus) { 3091 cmax = max; 3092 max = pci_scan_bridge_extend(bus, dev, max, 0, 0); 3093 3094 /* 3095 * Reserve one bus for each bridge now to avoid extending 3096 * hotplug bridges too much during the second scan below. 3097 */ 3098 used_buses++; 3099 if (max - cmax > 1) 3100 used_buses += max - cmax - 1; 3101 } 3102 3103 /* Scan bridges that need to be reconfigured */ 3104 for_each_pci_bridge(dev, bus) { 3105 unsigned int buses = 0; 3106 3107 if (!hotplug_bridges && normal_bridges == 1) { 3108 /* 3109 * There is only one bridge on the bus (upstream 3110 * port) so it gets all available buses which it 3111 * can then distribute to the possible hotplug 3112 * bridges below. 3113 */ 3114 buses = available_buses; 3115 } else if (dev->is_hotplug_bridge) { 3116 /* 3117 * Distribute the extra buses between hotplug 3118 * bridges if any. 3119 */ 3120 buses = available_buses / hotplug_bridges; 3121 buses = min(buses, available_buses - used_buses + 1); 3122 } 3123 3124 cmax = max; 3125 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1); 3126 /* One bus is already accounted so don't add it again */ 3127 if (max - cmax > 1) 3128 used_buses += max - cmax - 1; 3129 } 3130 3131 /* 3132 * Make sure a hotplug bridge has at least the minimum requested 3133 * number of buses but allow it to grow up to the maximum available 3134 * bus number if there is room. 3135 */ 3136 if (bus->self && bus->self->is_hotplug_bridge) { 3137 used_buses = max(available_buses, pci_hotplug_bus_size - 1); 3138 if (max - start < used_buses) { 3139 max = start + used_buses; 3140 3141 /* Do not allocate more buses than we have room left */ 3142 if (max > bus->busn_res.end) 3143 max = bus->busn_res.end; 3144 3145 dev_dbg(&bus->dev, "%pR extended by %#02x\n", 3146 &bus->busn_res, max - start); 3147 } 3148 } 3149 3150 /* 3151 * We've scanned the bus and so we know all about what's on 3152 * the other side of any bridges that may be on this bus plus 3153 * any devices. 3154 * 3155 * Return how far we've got finding sub-buses. 3156 */ 3157 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max); 3158 return max; 3159 } 3160 3161 /** 3162 * pci_scan_child_bus() - Scan devices below a bus 3163 * @bus: Bus to scan for devices 3164 * 3165 * Scans devices below @bus including subordinate buses. Returns new 3166 * subordinate number including all the found devices. 3167 */ 3168 unsigned int pci_scan_child_bus(struct pci_bus *bus) 3169 { 3170 return pci_scan_child_bus_extend(bus, 0); 3171 } 3172 EXPORT_SYMBOL_GPL(pci_scan_child_bus); 3173 3174 /** 3175 * pcibios_root_bridge_prepare - Platform-specific host bridge setup 3176 * @bridge: Host bridge to set up 3177 * 3178 * Default empty implementation. Replace with an architecture-specific setup 3179 * routine, if necessary. 3180 */ 3181 int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge) 3182 { 3183 return 0; 3184 } 3185 3186 void __weak pcibios_add_bus(struct pci_bus *bus) 3187 { 3188 } 3189 3190 void __weak pcibios_remove_bus(struct pci_bus *bus) 3191 { 3192 } 3193 3194 struct pci_bus *pci_create_root_bus(struct device *parent, int bus, 3195 struct pci_ops *ops, void *sysdata, struct list_head *resources) 3196 { 3197 int error; 3198 struct pci_host_bridge *bridge; 3199 3200 bridge = pci_alloc_host_bridge(0); 3201 if (!bridge) 3202 return NULL; 3203 3204 bridge->dev.parent = parent; 3205 3206 list_splice_init(resources, &bridge->windows); 3207 bridge->sysdata = sysdata; 3208 bridge->busnr = bus; 3209 bridge->ops = ops; 3210 3211 error = pci_register_host_bridge(bridge); 3212 if (error < 0) 3213 goto err_out; 3214 3215 return bridge->bus; 3216 3217 err_out: 3218 put_device(&bridge->dev); 3219 return NULL; 3220 } 3221 EXPORT_SYMBOL_GPL(pci_create_root_bus); 3222 3223 int pci_host_probe(struct pci_host_bridge *bridge) 3224 { 3225 struct pci_bus *bus, *child; 3226 int ret; 3227 3228 pci_lock_rescan_remove(); 3229 ret = pci_scan_root_bus_bridge(bridge); 3230 pci_unlock_rescan_remove(); 3231 if (ret < 0) { 3232 dev_err(bridge->dev.parent, "Scanning root bridge failed"); 3233 return ret; 3234 } 3235 3236 bus = bridge->bus; 3237 3238 /* If we must preserve the resource configuration, claim now */ 3239 if (bridge->preserve_config) 3240 pci_bus_claim_resources(bus); 3241 3242 /* 3243 * Assign whatever was left unassigned. If we didn't claim above, 3244 * this will reassign everything. 3245 */ 3246 pci_assign_unassigned_root_bus_resources(bus); 3247 3248 list_for_each_entry(child, &bus->children, node) 3249 pcie_bus_configure_settings(child); 3250 3251 pci_lock_rescan_remove(); 3252 pci_bus_add_devices(bus); 3253 pci_unlock_rescan_remove(); 3254 3255 /* 3256 * Ensure pm_runtime_enable() is called for the controller drivers 3257 * before calling pci_host_probe(). The PM framework expects that 3258 * if the parent device supports runtime PM, it will be enabled 3259 * before child runtime PM is enabled. 3260 */ 3261 pm_runtime_set_active(&bridge->dev); 3262 pm_runtime_no_callbacks(&bridge->dev); 3263 devm_pm_runtime_enable(&bridge->dev); 3264 3265 return 0; 3266 } 3267 EXPORT_SYMBOL_GPL(pci_host_probe); 3268 3269 int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max) 3270 { 3271 struct resource *res = &b->busn_res; 3272 struct resource *parent_res, *conflict; 3273 3274 res->start = bus; 3275 res->end = bus_max; 3276 res->flags = IORESOURCE_BUS; 3277 3278 if (!pci_is_root_bus(b)) 3279 parent_res = &b->parent->busn_res; 3280 else { 3281 parent_res = get_pci_domain_busn_res(pci_domain_nr(b)); 3282 res->flags |= IORESOURCE_PCI_FIXED; 3283 } 3284 3285 conflict = request_resource_conflict(parent_res, res); 3286 3287 if (conflict) 3288 dev_info(&b->dev, 3289 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n", 3290 res, pci_is_root_bus(b) ? "domain " : "", 3291 parent_res, conflict->name, conflict); 3292 3293 return conflict == NULL; 3294 } 3295 3296 int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max) 3297 { 3298 struct resource *res = &b->busn_res; 3299 struct resource old_res = *res; 3300 resource_size_t size; 3301 int ret; 3302 3303 if (res->start > bus_max) 3304 return -EINVAL; 3305 3306 size = bus_max - res->start + 1; 3307 ret = adjust_resource(res, res->start, size); 3308 dev_info(&b->dev, "busn_res: %pR end %s updated to %02x\n", 3309 &old_res, ret ? "can not be" : "is", bus_max); 3310 3311 if (!ret && !res->parent) 3312 pci_bus_insert_busn_res(b, res->start, res->end); 3313 3314 return ret; 3315 } 3316 3317 void pci_bus_release_busn_res(struct pci_bus *b) 3318 { 3319 struct resource *res = &b->busn_res; 3320 int ret; 3321 3322 if (!res->flags || !res->parent) 3323 return; 3324 3325 ret = release_resource(res); 3326 dev_info(&b->dev, "busn_res: %pR %s released\n", 3327 res, ret ? "can not be" : "is"); 3328 } 3329 3330 int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge) 3331 { 3332 struct resource_entry *window; 3333 bool found = false; 3334 struct pci_bus *b; 3335 int max, bus, ret; 3336 3337 if (!bridge) 3338 return -EINVAL; 3339 3340 resource_list_for_each_entry(window, &bridge->windows) 3341 if (window->res->flags & IORESOURCE_BUS) { 3342 bridge->busnr = window->res->start; 3343 found = true; 3344 break; 3345 } 3346 3347 ret = pci_register_host_bridge(bridge); 3348 if (ret < 0) 3349 return ret; 3350 3351 b = bridge->bus; 3352 bus = bridge->busnr; 3353 3354 if (!found) { 3355 dev_info(&b->dev, 3356 "No busn resource found for root bus, will use [bus %02x-ff]\n", 3357 bus); 3358 pci_bus_insert_busn_res(b, bus, 255); 3359 } 3360 3361 max = pci_scan_child_bus(b); 3362 3363 if (!found) 3364 pci_bus_update_busn_res_end(b, max); 3365 3366 return 0; 3367 } 3368 EXPORT_SYMBOL(pci_scan_root_bus_bridge); 3369 3370 struct pci_bus *pci_scan_root_bus(struct device *parent, int bus, 3371 struct pci_ops *ops, void *sysdata, struct list_head *resources) 3372 { 3373 struct resource_entry *window; 3374 bool found = false; 3375 struct pci_bus *b; 3376 int max; 3377 3378 resource_list_for_each_entry(window, resources) 3379 if (window->res->flags & IORESOURCE_BUS) { 3380 found = true; 3381 break; 3382 } 3383 3384 b = pci_create_root_bus(parent, bus, ops, sysdata, resources); 3385 if (!b) 3386 return NULL; 3387 3388 if (!found) { 3389 dev_info(&b->dev, 3390 "No busn resource found for root bus, will use [bus %02x-ff]\n", 3391 bus); 3392 pci_bus_insert_busn_res(b, bus, 255); 3393 } 3394 3395 max = pci_scan_child_bus(b); 3396 3397 if (!found) 3398 pci_bus_update_busn_res_end(b, max); 3399 3400 return b; 3401 } 3402 EXPORT_SYMBOL(pci_scan_root_bus); 3403 3404 struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops, 3405 void *sysdata) 3406 { 3407 LIST_HEAD(resources); 3408 struct pci_bus *b; 3409 3410 pci_add_resource(&resources, &ioport_resource); 3411 pci_add_resource(&resources, &iomem_resource); 3412 pci_add_resource(&resources, &busn_resource); 3413 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources); 3414 if (b) { 3415 pci_scan_child_bus(b); 3416 } else { 3417 pci_free_resource_list(&resources); 3418 } 3419 return b; 3420 } 3421 EXPORT_SYMBOL(pci_scan_bus); 3422 3423 /** 3424 * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices 3425 * @bridge: PCI bridge for the bus to scan 3426 * 3427 * Scan a PCI bus and child buses for new devices, add them, 3428 * and enable them, resizing bridge mmio/io resource if necessary 3429 * and possible. The caller must ensure the child devices are already 3430 * removed for resizing to occur. 3431 * 3432 * Returns the max number of subordinate bus discovered. 3433 */ 3434 unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge) 3435 { 3436 unsigned int max; 3437 struct pci_bus *bus = bridge->subordinate; 3438 3439 max = pci_scan_child_bus(bus); 3440 3441 pci_assign_unassigned_bridge_resources(bridge); 3442 3443 pci_bus_add_devices(bus); 3444 3445 return max; 3446 } 3447 3448 /** 3449 * pci_rescan_bus - Scan a PCI bus for devices 3450 * @bus: PCI bus to scan 3451 * 3452 * Scan a PCI bus and child buses for new devices, add them, 3453 * and enable them. 3454 * 3455 * Returns the max number of subordinate bus discovered. 3456 */ 3457 unsigned int pci_rescan_bus(struct pci_bus *bus) 3458 { 3459 unsigned int max; 3460 3461 max = pci_scan_child_bus(bus); 3462 pci_assign_unassigned_bus_resources(bus); 3463 pci_bus_add_devices(bus); 3464 3465 return max; 3466 } 3467 EXPORT_SYMBOL_GPL(pci_rescan_bus); 3468 3469 /* 3470 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal 3471 * routines should always be executed under this mutex. 3472 */ 3473 DEFINE_MUTEX(pci_rescan_remove_lock); 3474 3475 void pci_lock_rescan_remove(void) 3476 { 3477 mutex_lock(&pci_rescan_remove_lock); 3478 } 3479 EXPORT_SYMBOL_GPL(pci_lock_rescan_remove); 3480 3481 void pci_unlock_rescan_remove(void) 3482 { 3483 mutex_unlock(&pci_rescan_remove_lock); 3484 } 3485 EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove); 3486 3487 static int __init pci_sort_bf_cmp(const struct device *d_a, 3488 const struct device *d_b) 3489 { 3490 const struct pci_dev *a = to_pci_dev(d_a); 3491 const struct pci_dev *b = to_pci_dev(d_b); 3492 3493 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1; 3494 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1; 3495 3496 if (a->bus->number < b->bus->number) return -1; 3497 else if (a->bus->number > b->bus->number) return 1; 3498 3499 if (a->devfn < b->devfn) return -1; 3500 else if (a->devfn > b->devfn) return 1; 3501 3502 return 0; 3503 } 3504 3505 void __init pci_sort_breadthfirst(void) 3506 { 3507 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp); 3508 } 3509 3510 int pci_hp_add_bridge(struct pci_dev *dev) 3511 { 3512 struct pci_bus *parent = dev->bus; 3513 int busnr, start = parent->busn_res.start; 3514 unsigned int available_buses = 0; 3515 int end = parent->busn_res.end; 3516 3517 for (busnr = start; busnr <= end; busnr++) { 3518 if (!pci_find_bus(pci_domain_nr(parent), busnr)) 3519 break; 3520 } 3521 if (busnr-- > end) { 3522 pci_err(dev, "No bus number available for hot-added bridge\n"); 3523 return -1; 3524 } 3525 3526 /* Scan bridges that are already configured */ 3527 busnr = pci_scan_bridge(parent, dev, busnr, 0); 3528 3529 /* 3530 * Distribute the available bus numbers between hotplug-capable 3531 * bridges to make extending the chain later possible. 3532 */ 3533 available_buses = end - busnr; 3534 3535 /* Scan bridges that need to be reconfigured */ 3536 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1); 3537 3538 if (!dev->subordinate) 3539 return -1; 3540 3541 return 0; 3542 } 3543 EXPORT_SYMBOL_GPL(pci_hp_add_bridge); 3544