Linux 4.19.133
[linux/fpc-iii.git] / drivers / pci / probe.c
blobcbc0d8da7483cbdb06f693e851035d465489fa69
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI detection and setup code
4 */
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/pci.h>
10 #include <linux/of_device.h>
11 #include <linux/of_pci.h>
12 #include <linux/pci_hotplug.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/cpumask.h>
16 #include <linux/aer.h>
17 #include <linux/acpi.h>
18 #include <linux/hypervisor.h>
19 #include <linux/irqdomain.h>
20 #include <linux/pm_runtime.h>
21 #include "pci.h"
23 #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
24 #define CARDBUS_RESERVE_BUSNR 3
26 static struct resource busn_resource = {
27 .name = "PCI busn",
28 .start = 0,
29 .end = 255,
30 .flags = IORESOURCE_BUS,
33 /* Ugh. Need to stop exporting this to modules. */
34 LIST_HEAD(pci_root_buses);
35 EXPORT_SYMBOL(pci_root_buses);
37 static LIST_HEAD(pci_domain_busn_res_list);
39 struct pci_domain_busn_res {
40 struct list_head list;
41 struct resource res;
42 int domain_nr;
45 static struct resource *get_pci_domain_busn_res(int domain_nr)
47 struct pci_domain_busn_res *r;
49 list_for_each_entry(r, &pci_domain_busn_res_list, list)
50 if (r->domain_nr == domain_nr)
51 return &r->res;
53 r = kzalloc(sizeof(*r), GFP_KERNEL);
54 if (!r)
55 return NULL;
57 r->domain_nr = domain_nr;
58 r->res.start = 0;
59 r->res.end = 0xff;
60 r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
62 list_add_tail(&r->list, &pci_domain_busn_res_list);
64 return &r->res;
67 static int find_anything(struct device *dev, void *data)
69 return 1;
73 * Some device drivers need know if PCI is initiated.
74 * Basically, we think PCI is not initiated when there
75 * is no device to be found on the pci_bus_type.
77 int no_pci_devices(void)
79 struct device *dev;
80 int no_devices;
82 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
83 no_devices = (dev == NULL);
84 put_device(dev);
85 return no_devices;
87 EXPORT_SYMBOL(no_pci_devices);
90 * PCI Bus Class
92 static void release_pcibus_dev(struct device *dev)
94 struct pci_bus *pci_bus = to_pci_bus(dev);
96 put_device(pci_bus->bridge);
97 pci_bus_remove_resources(pci_bus);
98 pci_release_bus_of_node(pci_bus);
99 kfree(pci_bus);
102 static struct class pcibus_class = {
103 .name = "pci_bus",
104 .dev_release = &release_pcibus_dev,
105 .dev_groups = pcibus_groups,
108 static int __init pcibus_class_init(void)
110 return class_register(&pcibus_class);
112 postcore_initcall(pcibus_class_init);
114 static u64 pci_size(u64 base, u64 maxbase, u64 mask)
116 u64 size = mask & maxbase; /* Find the significant bits */
117 if (!size)
118 return 0;
121 * Get the lowest of them to find the decode size, and from that
122 * the extent.
124 size = (size & ~(size-1)) - 1;
127 * base == maxbase can be valid only if the BAR has already been
128 * programmed with all 1s.
130 if (base == maxbase && ((base | size) & mask) != mask)
131 return 0;
133 return size;
136 static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
138 u32 mem_type;
139 unsigned long flags;
141 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
142 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
143 flags |= IORESOURCE_IO;
144 return flags;
147 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
148 flags |= IORESOURCE_MEM;
149 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
150 flags |= IORESOURCE_PREFETCH;
152 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
153 switch (mem_type) {
154 case PCI_BASE_ADDRESS_MEM_TYPE_32:
155 break;
156 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
157 /* 1M mem BAR treated as 32-bit BAR */
158 break;
159 case PCI_BASE_ADDRESS_MEM_TYPE_64:
160 flags |= IORESOURCE_MEM_64;
161 break;
162 default:
163 /* mem unknown type treated as 32-bit BAR */
164 break;
166 return flags;
169 #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
172 * pci_read_base - Read a PCI BAR
173 * @dev: the PCI device
174 * @type: type of the BAR
175 * @res: resource buffer to be filled in
176 * @pos: BAR position in the config space
178 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
180 int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
181 struct resource *res, unsigned int pos)
183 u32 l = 0, sz = 0, mask;
184 u64 l64, sz64, mask64;
185 u16 orig_cmd;
186 struct pci_bus_region region, inverted_region;
188 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
190 /* No printks while decoding is disabled! */
191 if (!dev->mmio_always_on) {
192 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
193 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
194 pci_write_config_word(dev, PCI_COMMAND,
195 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
199 res->name = pci_name(dev);
201 pci_read_config_dword(dev, pos, &l);
202 pci_write_config_dword(dev, pos, l | mask);
203 pci_read_config_dword(dev, pos, &sz);
204 pci_write_config_dword(dev, pos, l);
207 * All bits set in sz means the device isn't working properly.
208 * If the BAR isn't implemented, all bits must be 0. If it's a
209 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
210 * 1 must be clear.
212 if (sz == 0xffffffff)
213 sz = 0;
216 * I don't know how l can have all bits set. Copied from old code.
217 * Maybe it fixes a bug on some ancient platform.
219 if (l == 0xffffffff)
220 l = 0;
222 if (type == pci_bar_unknown) {
223 res->flags = decode_bar(dev, l);
224 res->flags |= IORESOURCE_SIZEALIGN;
225 if (res->flags & IORESOURCE_IO) {
226 l64 = l & PCI_BASE_ADDRESS_IO_MASK;
227 sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
228 mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
229 } else {
230 l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
231 sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
232 mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
234 } else {
235 if (l & PCI_ROM_ADDRESS_ENABLE)
236 res->flags |= IORESOURCE_ROM_ENABLE;
237 l64 = l & PCI_ROM_ADDRESS_MASK;
238 sz64 = sz & PCI_ROM_ADDRESS_MASK;
239 mask64 = PCI_ROM_ADDRESS_MASK;
242 if (res->flags & IORESOURCE_MEM_64) {
243 pci_read_config_dword(dev, pos + 4, &l);
244 pci_write_config_dword(dev, pos + 4, ~0);
245 pci_read_config_dword(dev, pos + 4, &sz);
246 pci_write_config_dword(dev, pos + 4, l);
248 l64 |= ((u64)l << 32);
249 sz64 |= ((u64)sz << 32);
250 mask64 |= ((u64)~0 << 32);
253 if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
254 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
256 if (!sz64)
257 goto fail;
259 sz64 = pci_size(l64, sz64, mask64);
260 if (!sz64) {
261 pci_info(dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
262 pos);
263 goto fail;
266 if (res->flags & IORESOURCE_MEM_64) {
267 if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
268 && sz64 > 0x100000000ULL) {
269 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
270 res->start = 0;
271 res->end = 0;
272 pci_err(dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
273 pos, (unsigned long long)sz64);
274 goto out;
277 if ((sizeof(pci_bus_addr_t) < 8) && l) {
278 /* Above 32-bit boundary; try to reallocate */
279 res->flags |= IORESOURCE_UNSET;
280 res->start = 0;
281 res->end = sz64;
282 pci_info(dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
283 pos, (unsigned long long)l64);
284 goto out;
288 region.start = l64;
289 region.end = l64 + sz64;
291 pcibios_bus_to_resource(dev->bus, res, &region);
292 pcibios_resource_to_bus(dev->bus, &inverted_region, res);
295 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
296 * the corresponding resource address (the physical address used by
297 * the CPU. Converting that resource address back to a bus address
298 * should yield the original BAR value:
300 * resource_to_bus(bus_to_resource(A)) == A
302 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
303 * be claimed by the device.
305 if (inverted_region.start != region.start) {
306 res->flags |= IORESOURCE_UNSET;
307 res->start = 0;
308 res->end = region.end - region.start;
309 pci_info(dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
310 pos, (unsigned long long)region.start);
313 goto out;
316 fail:
317 res->flags = 0;
318 out:
319 if (res->flags)
320 pci_printk(KERN_DEBUG, dev, "reg 0x%x: %pR\n", pos, res);
322 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
325 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
327 unsigned int pos, reg;
329 if (dev->non_compliant_bars)
330 return;
332 /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */
333 if (dev->is_virtfn)
334 return;
336 for (pos = 0; pos < howmany; pos++) {
337 struct resource *res = &dev->resource[pos];
338 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
339 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
342 if (rom) {
343 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
344 dev->rom_base_reg = rom;
345 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
346 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
347 __pci_read_base(dev, pci_bar_mem32, res, rom);
351 static void pci_read_bridge_io(struct pci_bus *child)
353 struct pci_dev *dev = child->self;
354 u8 io_base_lo, io_limit_lo;
355 unsigned long io_mask, io_granularity, base, limit;
356 struct pci_bus_region region;
357 struct resource *res;
359 io_mask = PCI_IO_RANGE_MASK;
360 io_granularity = 0x1000;
361 if (dev->io_window_1k) {
362 /* Support 1K I/O space granularity */
363 io_mask = PCI_IO_1K_RANGE_MASK;
364 io_granularity = 0x400;
367 res = child->resource[0];
368 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
369 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
370 base = (io_base_lo & io_mask) << 8;
371 limit = (io_limit_lo & io_mask) << 8;
373 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
374 u16 io_base_hi, io_limit_hi;
376 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
377 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
378 base |= ((unsigned long) io_base_hi << 16);
379 limit |= ((unsigned long) io_limit_hi << 16);
382 if (base <= limit) {
383 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
384 region.start = base;
385 region.end = limit + io_granularity - 1;
386 pcibios_bus_to_resource(dev->bus, res, &region);
387 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
391 static void pci_read_bridge_mmio(struct pci_bus *child)
393 struct pci_dev *dev = child->self;
394 u16 mem_base_lo, mem_limit_lo;
395 unsigned long base, limit;
396 struct pci_bus_region region;
397 struct resource *res;
399 res = child->resource[1];
400 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
401 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
402 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
403 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
404 if (base <= limit) {
405 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
406 region.start = base;
407 region.end = limit + 0xfffff;
408 pcibios_bus_to_resource(dev->bus, res, &region);
409 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
413 static void pci_read_bridge_mmio_pref(struct pci_bus *child)
415 struct pci_dev *dev = child->self;
416 u16 mem_base_lo, mem_limit_lo;
417 u64 base64, limit64;
418 pci_bus_addr_t base, limit;
419 struct pci_bus_region region;
420 struct resource *res;
422 res = child->resource[2];
423 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
424 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
425 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
426 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
428 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
429 u32 mem_base_hi, mem_limit_hi;
431 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
432 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
435 * Some bridges set the base > limit by default, and some
436 * (broken) BIOSes do not initialize them. If we find
437 * this, just assume they are not being used.
439 if (mem_base_hi <= mem_limit_hi) {
440 base64 |= (u64) mem_base_hi << 32;
441 limit64 |= (u64) mem_limit_hi << 32;
445 base = (pci_bus_addr_t) base64;
446 limit = (pci_bus_addr_t) limit64;
448 if (base != base64) {
449 pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
450 (unsigned long long) base64);
451 return;
454 if (base <= limit) {
455 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
456 IORESOURCE_MEM | IORESOURCE_PREFETCH;
457 if (res->flags & PCI_PREF_RANGE_TYPE_64)
458 res->flags |= IORESOURCE_MEM_64;
459 region.start = base;
460 region.end = limit + 0xfffff;
461 pcibios_bus_to_resource(dev->bus, res, &region);
462 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
466 void pci_read_bridge_bases(struct pci_bus *child)
468 struct pci_dev *dev = child->self;
469 struct resource *res;
470 int i;
472 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
473 return;
475 pci_info(dev, "PCI bridge to %pR%s\n",
476 &child->busn_res,
477 dev->transparent ? " (subtractive decode)" : "");
479 pci_bus_remove_resources(child);
480 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
481 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
483 pci_read_bridge_io(child);
484 pci_read_bridge_mmio(child);
485 pci_read_bridge_mmio_pref(child);
487 if (dev->transparent) {
488 pci_bus_for_each_resource(child->parent, res, i) {
489 if (res && res->flags) {
490 pci_bus_add_resource(child, res,
491 PCI_SUBTRACTIVE_DECODE);
492 pci_printk(KERN_DEBUG, dev,
493 " bridge window %pR (subtractive decode)\n",
494 res);
500 static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
502 struct pci_bus *b;
504 b = kzalloc(sizeof(*b), GFP_KERNEL);
505 if (!b)
506 return NULL;
508 INIT_LIST_HEAD(&b->node);
509 INIT_LIST_HEAD(&b->children);
510 INIT_LIST_HEAD(&b->devices);
511 INIT_LIST_HEAD(&b->slots);
512 INIT_LIST_HEAD(&b->resources);
513 b->max_bus_speed = PCI_SPEED_UNKNOWN;
514 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
515 #ifdef CONFIG_PCI_DOMAINS_GENERIC
516 if (parent)
517 b->domain_nr = parent->domain_nr;
518 #endif
519 return b;
522 static void devm_pci_release_host_bridge_dev(struct device *dev)
524 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
526 if (bridge->release_fn)
527 bridge->release_fn(bridge);
529 pci_free_resource_list(&bridge->windows);
532 static void pci_release_host_bridge_dev(struct device *dev)
534 devm_pci_release_host_bridge_dev(dev);
535 kfree(to_pci_host_bridge(dev));
538 static void pci_init_host_bridge(struct pci_host_bridge *bridge)
540 INIT_LIST_HEAD(&bridge->windows);
543 * We assume we can manage these PCIe features. Some systems may
544 * reserve these for use by the platform itself, e.g., an ACPI BIOS
545 * may implement its own AER handling and use _OSC to prevent the
546 * OS from interfering.
548 bridge->native_aer = 1;
549 bridge->native_pcie_hotplug = 1;
550 bridge->native_shpc_hotplug = 1;
551 bridge->native_pme = 1;
552 bridge->native_ltr = 1;
555 struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
557 struct pci_host_bridge *bridge;
559 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
560 if (!bridge)
561 return NULL;
563 pci_init_host_bridge(bridge);
564 bridge->dev.release = pci_release_host_bridge_dev;
566 return bridge;
568 EXPORT_SYMBOL(pci_alloc_host_bridge);
570 struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
571 size_t priv)
573 struct pci_host_bridge *bridge;
575 bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
576 if (!bridge)
577 return NULL;
579 pci_init_host_bridge(bridge);
580 bridge->dev.release = devm_pci_release_host_bridge_dev;
582 return bridge;
584 EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
586 void pci_free_host_bridge(struct pci_host_bridge *bridge)
588 pci_free_resource_list(&bridge->windows);
590 kfree(bridge);
592 EXPORT_SYMBOL(pci_free_host_bridge);
594 static const unsigned char pcix_bus_speed[] = {
595 PCI_SPEED_UNKNOWN, /* 0 */
596 PCI_SPEED_66MHz_PCIX, /* 1 */
597 PCI_SPEED_100MHz_PCIX, /* 2 */
598 PCI_SPEED_133MHz_PCIX, /* 3 */
599 PCI_SPEED_UNKNOWN, /* 4 */
600 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
601 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
602 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
603 PCI_SPEED_UNKNOWN, /* 8 */
604 PCI_SPEED_66MHz_PCIX_266, /* 9 */
605 PCI_SPEED_100MHz_PCIX_266, /* A */
606 PCI_SPEED_133MHz_PCIX_266, /* B */
607 PCI_SPEED_UNKNOWN, /* C */
608 PCI_SPEED_66MHz_PCIX_533, /* D */
609 PCI_SPEED_100MHz_PCIX_533, /* E */
610 PCI_SPEED_133MHz_PCIX_533 /* F */
613 const unsigned char pcie_link_speed[] = {
614 PCI_SPEED_UNKNOWN, /* 0 */
615 PCIE_SPEED_2_5GT, /* 1 */
616 PCIE_SPEED_5_0GT, /* 2 */
617 PCIE_SPEED_8_0GT, /* 3 */
618 PCIE_SPEED_16_0GT, /* 4 */
619 PCI_SPEED_UNKNOWN, /* 5 */
620 PCI_SPEED_UNKNOWN, /* 6 */
621 PCI_SPEED_UNKNOWN, /* 7 */
622 PCI_SPEED_UNKNOWN, /* 8 */
623 PCI_SPEED_UNKNOWN, /* 9 */
624 PCI_SPEED_UNKNOWN, /* A */
625 PCI_SPEED_UNKNOWN, /* B */
626 PCI_SPEED_UNKNOWN, /* C */
627 PCI_SPEED_UNKNOWN, /* D */
628 PCI_SPEED_UNKNOWN, /* E */
629 PCI_SPEED_UNKNOWN /* F */
632 void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
634 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
636 EXPORT_SYMBOL_GPL(pcie_update_link_speed);
638 static unsigned char agp_speeds[] = {
639 AGP_UNKNOWN,
640 AGP_1X,
641 AGP_2X,
642 AGP_4X,
643 AGP_8X
646 static enum pci_bus_speed agp_speed(int agp3, int agpstat)
648 int index = 0;
650 if (agpstat & 4)
651 index = 3;
652 else if (agpstat & 2)
653 index = 2;
654 else if (agpstat & 1)
655 index = 1;
656 else
657 goto out;
659 if (agp3) {
660 index += 2;
661 if (index == 5)
662 index = 0;
665 out:
666 return agp_speeds[index];
669 static void pci_set_bus_speed(struct pci_bus *bus)
671 struct pci_dev *bridge = bus->self;
672 int pos;
674 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
675 if (!pos)
676 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
677 if (pos) {
678 u32 agpstat, agpcmd;
680 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
681 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
683 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
684 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
687 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
688 if (pos) {
689 u16 status;
690 enum pci_bus_speed max;
692 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
693 &status);
695 if (status & PCI_X_SSTATUS_533MHZ) {
696 max = PCI_SPEED_133MHz_PCIX_533;
697 } else if (status & PCI_X_SSTATUS_266MHZ) {
698 max = PCI_SPEED_133MHz_PCIX_266;
699 } else if (status & PCI_X_SSTATUS_133MHZ) {
700 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
701 max = PCI_SPEED_133MHz_PCIX_ECC;
702 else
703 max = PCI_SPEED_133MHz_PCIX;
704 } else {
705 max = PCI_SPEED_66MHz_PCIX;
708 bus->max_bus_speed = max;
709 bus->cur_bus_speed = pcix_bus_speed[
710 (status & PCI_X_SSTATUS_FREQ) >> 6];
712 return;
715 if (pci_is_pcie(bridge)) {
716 u32 linkcap;
717 u16 linksta;
719 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
720 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
722 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
723 pcie_update_link_speed(bus, linksta);
727 static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
729 struct irq_domain *d;
732 * Any firmware interface that can resolve the msi_domain
733 * should be called from here.
735 d = pci_host_bridge_of_msi_domain(bus);
736 if (!d)
737 d = pci_host_bridge_acpi_msi_domain(bus);
739 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
741 * If no IRQ domain was found via the OF tree, try looking it up
742 * directly through the fwnode_handle.
744 if (!d) {
745 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
747 if (fwnode)
748 d = irq_find_matching_fwnode(fwnode,
749 DOMAIN_BUS_PCI_MSI);
751 #endif
753 return d;
756 static void pci_set_bus_msi_domain(struct pci_bus *bus)
758 struct irq_domain *d;
759 struct pci_bus *b;
762 * The bus can be a root bus, a subordinate bus, or a virtual bus
763 * created by an SR-IOV device. Walk up to the first bridge device
764 * found or derive the domain from the host bridge.
766 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
767 if (b->self)
768 d = dev_get_msi_domain(&b->self->dev);
771 if (!d)
772 d = pci_host_bridge_msi_domain(b);
774 dev_set_msi_domain(&bus->dev, d);
777 static int pci_register_host_bridge(struct pci_host_bridge *bridge)
779 struct device *parent = bridge->dev.parent;
780 struct resource_entry *window, *n;
781 struct pci_bus *bus, *b;
782 resource_size_t offset;
783 LIST_HEAD(resources);
784 struct resource *res;
785 char addr[64], *fmt;
786 const char *name;
787 int err;
789 bus = pci_alloc_bus(NULL);
790 if (!bus)
791 return -ENOMEM;
793 bridge->bus = bus;
795 /* Temporarily move resources off the list */
796 list_splice_init(&bridge->windows, &resources);
797 bus->sysdata = bridge->sysdata;
798 bus->msi = bridge->msi;
799 bus->ops = bridge->ops;
800 bus->number = bus->busn_res.start = bridge->busnr;
801 #ifdef CONFIG_PCI_DOMAINS_GENERIC
802 bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
803 #endif
805 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
806 if (b) {
807 /* Ignore it if we already got here via a different bridge */
808 dev_dbg(&b->dev, "bus already known\n");
809 err = -EEXIST;
810 goto free;
813 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
814 bridge->busnr);
816 err = pcibios_root_bridge_prepare(bridge);
817 if (err)
818 goto free;
820 err = device_register(&bridge->dev);
821 if (err) {
822 put_device(&bridge->dev);
823 goto free;
825 bus->bridge = get_device(&bridge->dev);
826 device_enable_async_suspend(bus->bridge);
827 pci_set_bus_of_node(bus);
828 pci_set_bus_msi_domain(bus);
830 if (!parent)
831 set_dev_node(bus->bridge, pcibus_to_node(bus));
833 bus->dev.class = &pcibus_class;
834 bus->dev.parent = bus->bridge;
836 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
837 name = dev_name(&bus->dev);
839 err = device_register(&bus->dev);
840 if (err)
841 goto unregister;
843 pcibios_add_bus(bus);
845 /* Create legacy_io and legacy_mem files for this bus */
846 pci_create_legacy_files(bus);
848 if (parent)
849 dev_info(parent, "PCI host bridge to bus %s\n", name);
850 else
851 pr_info("PCI host bridge to bus %s\n", name);
853 /* Add initial resources to the bus */
854 resource_list_for_each_entry_safe(window, n, &resources) {
855 list_move_tail(&window->node, &bridge->windows);
856 offset = window->offset;
857 res = window->res;
859 if (res->flags & IORESOURCE_BUS)
860 pci_bus_insert_busn_res(bus, bus->number, res->end);
861 else
862 pci_bus_add_resource(bus, res, 0);
864 if (offset) {
865 if (resource_type(res) == IORESOURCE_IO)
866 fmt = " (bus address [%#06llx-%#06llx])";
867 else
868 fmt = " (bus address [%#010llx-%#010llx])";
870 snprintf(addr, sizeof(addr), fmt,
871 (unsigned long long)(res->start - offset),
872 (unsigned long long)(res->end - offset));
873 } else
874 addr[0] = '\0';
876 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
879 down_write(&pci_bus_sem);
880 list_add_tail(&bus->node, &pci_root_buses);
881 up_write(&pci_bus_sem);
883 return 0;
885 unregister:
886 put_device(&bridge->dev);
887 device_unregister(&bridge->dev);
889 free:
890 kfree(bus);
891 return err;
894 static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev *bridge)
896 int pos;
897 u32 status;
900 * If extended config space isn't accessible on a bridge's primary
901 * bus, we certainly can't access it on the secondary bus.
903 if (bridge->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
904 return false;
907 * PCIe Root Ports and switch ports are PCIe on both sides, so if
908 * extended config space is accessible on the primary, it's also
909 * accessible on the secondary.
911 if (pci_is_pcie(bridge) &&
912 (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT ||
913 pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM ||
914 pci_pcie_type(bridge) == PCI_EXP_TYPE_DOWNSTREAM))
915 return true;
918 * For the other bridge types:
919 * - PCI-to-PCI bridges
920 * - PCIe-to-PCI/PCI-X forward bridges
921 * - PCI/PCI-X-to-PCIe reverse bridges
922 * extended config space on the secondary side is only accessible
923 * if the bridge supports PCI-X Mode 2.
925 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
926 if (!pos)
927 return false;
929 pci_read_config_dword(bridge, pos + PCI_X_STATUS, &status);
930 return status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ);
933 static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
934 struct pci_dev *bridge, int busnr)
936 struct pci_bus *child;
937 int i;
938 int ret;
940 /* Allocate a new bus and inherit stuff from the parent */
941 child = pci_alloc_bus(parent);
942 if (!child)
943 return NULL;
945 child->parent = parent;
946 child->ops = parent->ops;
947 child->msi = parent->msi;
948 child->sysdata = parent->sysdata;
949 child->bus_flags = parent->bus_flags;
952 * Initialize some portions of the bus device, but don't register
953 * it now as the parent is not properly set up yet.
955 child->dev.class = &pcibus_class;
956 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
958 /* Set up the primary, secondary and subordinate bus numbers */
959 child->number = child->busn_res.start = busnr;
960 child->primary = parent->busn_res.start;
961 child->busn_res.end = 0xff;
963 if (!bridge) {
964 child->dev.parent = parent->bridge;
965 goto add_dev;
968 child->self = bridge;
969 child->bridge = get_device(&bridge->dev);
970 child->dev.parent = child->bridge;
971 pci_set_bus_of_node(child);
972 pci_set_bus_speed(child);
975 * Check whether extended config space is accessible on the child
976 * bus. Note that we currently assume it is always accessible on
977 * the root bus.
979 if (!pci_bridge_child_ext_cfg_accessible(bridge)) {
980 child->bus_flags |= PCI_BUS_FLAGS_NO_EXTCFG;
981 pci_info(child, "extended config space not accessible\n");
984 /* Set up default resource pointers and names */
985 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
986 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
987 child->resource[i]->name = child->name;
989 bridge->subordinate = child;
991 add_dev:
992 pci_set_bus_msi_domain(child);
993 ret = device_register(&child->dev);
994 WARN_ON(ret < 0);
996 pcibios_add_bus(child);
998 if (child->ops->add_bus) {
999 ret = child->ops->add_bus(child);
1000 if (WARN_ON(ret < 0))
1001 dev_err(&child->dev, "failed to add bus: %d\n", ret);
1004 /* Create legacy_io and legacy_mem files for this bus */
1005 pci_create_legacy_files(child);
1007 return child;
1010 struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
1011 int busnr)
1013 struct pci_bus *child;
1015 child = pci_alloc_child_bus(parent, dev, busnr);
1016 if (child) {
1017 down_write(&pci_bus_sem);
1018 list_add_tail(&child->node, &parent->children);
1019 up_write(&pci_bus_sem);
1021 return child;
1023 EXPORT_SYMBOL(pci_add_new_bus);
1025 static void pci_enable_crs(struct pci_dev *pdev)
1027 u16 root_cap = 0;
1029 /* Enable CRS Software Visibility if supported */
1030 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
1031 if (root_cap & PCI_EXP_RTCAP_CRSVIS)
1032 pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
1033 PCI_EXP_RTCTL_CRSSVE);
1036 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
1037 unsigned int available_buses);
1040 * pci_scan_bridge_extend() - Scan buses behind a bridge
1041 * @bus: Parent bus the bridge is on
1042 * @dev: Bridge itself
1043 * @max: Starting subordinate number of buses behind this bridge
1044 * @available_buses: Total number of buses available for this bridge and
1045 * the devices below. After the minimal bus space has
1046 * been allocated the remaining buses will be
1047 * distributed equally between hotplug-capable bridges.
1048 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1049 * that need to be reconfigured.
1051 * If it's a bridge, configure it and scan the bus behind it.
1052 * For CardBus bridges, we don't scan behind as the devices will
1053 * be handled by the bridge driver itself.
1055 * We need to process bridges in two passes -- first we scan those
1056 * already configured by the BIOS and after we are done with all of
1057 * them, we proceed to assigning numbers to the remaining buses in
1058 * order to avoid overlaps between old and new bus numbers.
1060 * Return: New subordinate number covering all buses behind this bridge.
1062 static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
1063 int max, unsigned int available_buses,
1064 int pass)
1066 struct pci_bus *child;
1067 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
1068 u32 buses, i, j = 0;
1069 u16 bctl;
1070 u8 primary, secondary, subordinate;
1071 int broken = 0;
1074 * Make sure the bridge is powered on to be able to access config
1075 * space of devices below it.
1077 pm_runtime_get_sync(&dev->dev);
1079 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
1080 primary = buses & 0xFF;
1081 secondary = (buses >> 8) & 0xFF;
1082 subordinate = (buses >> 16) & 0xFF;
1084 pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
1085 secondary, subordinate, pass);
1087 if (!primary && (primary != bus->number) && secondary && subordinate) {
1088 pci_warn(dev, "Primary bus is hard wired to 0\n");
1089 primary = bus->number;
1092 /* Check if setup is sensible at all */
1093 if (!pass &&
1094 (primary != bus->number || secondary <= bus->number ||
1095 secondary > subordinate)) {
1096 pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
1097 secondary, subordinate);
1098 broken = 1;
1102 * Disable Master-Abort Mode during probing to avoid reporting of
1103 * bus errors in some architectures.
1105 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1106 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1107 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1109 pci_enable_crs(dev);
1111 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1112 !is_cardbus && !broken) {
1113 unsigned int cmax;
1116 * Bus already configured by firmware, process it in the
1117 * first pass and just note the configuration.
1119 if (pass)
1120 goto out;
1123 * The bus might already exist for two reasons: Either we
1124 * are rescanning the bus or the bus is reachable through
1125 * more than one bridge. The second case can happen with
1126 * the i450NX chipset.
1128 child = pci_find_bus(pci_domain_nr(bus), secondary);
1129 if (!child) {
1130 child = pci_add_new_bus(bus, dev, secondary);
1131 if (!child)
1132 goto out;
1133 child->primary = primary;
1134 pci_bus_insert_busn_res(child, secondary, subordinate);
1135 child->bridge_ctl = bctl;
1138 cmax = pci_scan_child_bus(child);
1139 if (cmax > subordinate)
1140 pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
1141 subordinate, cmax);
1143 /* Subordinate should equal child->busn_res.end */
1144 if (subordinate > max)
1145 max = subordinate;
1146 } else {
1149 * We need to assign a number to this bus which we always
1150 * do in the second pass.
1152 if (!pass) {
1153 if (pcibios_assign_all_busses() || broken || is_cardbus)
1156 * Temporarily disable forwarding of the
1157 * configuration cycles on all bridges in
1158 * this bus segment to avoid possible
1159 * conflicts in the second pass between two
1160 * bridges programmed with overlapping bus
1161 * ranges.
1163 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1164 buses & ~0xffffff);
1165 goto out;
1168 /* Clear errors */
1169 pci_write_config_word(dev, PCI_STATUS, 0xffff);
1172 * Prevent assigning a bus number that already exists.
1173 * This can happen when a bridge is hot-plugged, so in this
1174 * case we only re-scan this bus.
1176 child = pci_find_bus(pci_domain_nr(bus), max+1);
1177 if (!child) {
1178 child = pci_add_new_bus(bus, dev, max+1);
1179 if (!child)
1180 goto out;
1181 pci_bus_insert_busn_res(child, max+1,
1182 bus->busn_res.end);
1184 max++;
1185 if (available_buses)
1186 available_buses--;
1188 buses = (buses & 0xff000000)
1189 | ((unsigned int)(child->primary) << 0)
1190 | ((unsigned int)(child->busn_res.start) << 8)
1191 | ((unsigned int)(child->busn_res.end) << 16);
1194 * yenta.c forces a secondary latency timer of 176.
1195 * Copy that behaviour here.
1197 if (is_cardbus) {
1198 buses &= ~0xff000000;
1199 buses |= CARDBUS_LATENCY_TIMER << 24;
1202 /* We need to blast all three values with a single write */
1203 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1205 if (!is_cardbus) {
1206 child->bridge_ctl = bctl;
1207 max = pci_scan_child_bus_extend(child, available_buses);
1208 } else {
1211 * For CardBus bridges, we leave 4 bus numbers as
1212 * cards with a PCI-to-PCI bridge can be inserted
1213 * later.
1215 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
1216 struct pci_bus *parent = bus;
1217 if (pci_find_bus(pci_domain_nr(bus),
1218 max+i+1))
1219 break;
1220 while (parent->parent) {
1221 if ((!pcibios_assign_all_busses()) &&
1222 (parent->busn_res.end > max) &&
1223 (parent->busn_res.end <= max+i)) {
1224 j = 1;
1226 parent = parent->parent;
1228 if (j) {
1231 * Often, there are two CardBus
1232 * bridges -- try to leave one
1233 * valid bus number for each one.
1235 i /= 2;
1236 break;
1239 max += i;
1242 /* Set subordinate bus number to its real value */
1243 pci_bus_update_busn_res_end(child, max);
1244 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1247 sprintf(child->name,
1248 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1249 pci_domain_nr(bus), child->number);
1251 /* Check that all devices are accessible */
1252 while (bus->parent) {
1253 if ((child->busn_res.end > bus->busn_res.end) ||
1254 (child->number > bus->busn_res.end) ||
1255 (child->number < bus->number) ||
1256 (child->busn_res.end < bus->number)) {
1257 dev_info(&dev->dev, "devices behind bridge are unusable because %pR cannot be assigned for them\n",
1258 &child->busn_res);
1259 break;
1261 bus = bus->parent;
1264 out:
1265 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1267 pm_runtime_put(&dev->dev);
1269 return max;
1273 * pci_scan_bridge() - Scan buses behind a bridge
1274 * @bus: Parent bus the bridge is on
1275 * @dev: Bridge itself
1276 * @max: Starting subordinate number of buses behind this bridge
1277 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1278 * that need to be reconfigured.
1280 * If it's a bridge, configure it and scan the bus behind it.
1281 * For CardBus bridges, we don't scan behind as the devices will
1282 * be handled by the bridge driver itself.
1284 * We need to process bridges in two passes -- first we scan those
1285 * already configured by the BIOS and after we are done with all of
1286 * them, we proceed to assigning numbers to the remaining buses in
1287 * order to avoid overlaps between old and new bus numbers.
1289 * Return: New subordinate number covering all buses behind this bridge.
1291 int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1293 return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1295 EXPORT_SYMBOL(pci_scan_bridge);
1298 * Read interrupt line and base address registers.
1299 * The architecture-dependent code can tweak these, of course.
1301 static void pci_read_irq(struct pci_dev *dev)
1303 unsigned char irq;
1305 /* VFs are not allowed to use INTx, so skip the config reads */
1306 if (dev->is_virtfn) {
1307 dev->pin = 0;
1308 dev->irq = 0;
1309 return;
1312 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
1313 dev->pin = irq;
1314 if (irq)
1315 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1316 dev->irq = irq;
1319 void set_pcie_port_type(struct pci_dev *pdev)
1321 int pos;
1322 u16 reg16;
1323 int type;
1324 struct pci_dev *parent;
1326 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1327 if (!pos)
1328 return;
1330 pdev->pcie_cap = pos;
1331 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
1332 pdev->pcie_flags_reg = reg16;
1333 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
1334 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
1337 * A Root Port or a PCI-to-PCIe bridge is always the upstream end
1338 * of a Link. No PCIe component has two Links. Two Links are
1339 * connected by a Switch that has a Port on each Link and internal
1340 * logic to connect the two Ports.
1342 type = pci_pcie_type(pdev);
1343 if (type == PCI_EXP_TYPE_ROOT_PORT ||
1344 type == PCI_EXP_TYPE_PCIE_BRIDGE)
1345 pdev->has_secondary_link = 1;
1346 else if (type == PCI_EXP_TYPE_UPSTREAM ||
1347 type == PCI_EXP_TYPE_DOWNSTREAM) {
1348 parent = pci_upstream_bridge(pdev);
1351 * Usually there's an upstream device (Root Port or Switch
1352 * Downstream Port), but we can't assume one exists.
1354 if (parent && !parent->has_secondary_link)
1355 pdev->has_secondary_link = 1;
1359 void set_pcie_hotplug_bridge(struct pci_dev *pdev)
1361 u32 reg32;
1363 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
1364 if (reg32 & PCI_EXP_SLTCAP_HPC)
1365 pdev->is_hotplug_bridge = 1;
1368 static void set_pcie_thunderbolt(struct pci_dev *dev)
1370 int vsec = 0;
1371 u32 header;
1373 while ((vsec = pci_find_next_ext_capability(dev, vsec,
1374 PCI_EXT_CAP_ID_VNDR))) {
1375 pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
1377 /* Is the device part of a Thunderbolt controller? */
1378 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
1379 PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
1380 dev->is_thunderbolt = 1;
1381 return;
1387 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
1388 * @dev: PCI device
1390 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1391 * when forwarding a type1 configuration request the bridge must check that
1392 * the extended register address field is zero. The bridge is not permitted
1393 * to forward the transactions and must handle it as an Unsupported Request.
1394 * Some bridges do not follow this rule and simply drop the extended register
1395 * bits, resulting in the standard config space being aliased, every 256
1396 * bytes across the entire configuration space. Test for this condition by
1397 * comparing the first dword of each potential alias to the vendor/device ID.
1398 * Known offenders:
1399 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1400 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1402 static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1404 #ifdef CONFIG_PCI_QUIRKS
1405 int pos;
1406 u32 header, tmp;
1408 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1410 for (pos = PCI_CFG_SPACE_SIZE;
1411 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1412 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1413 || header != tmp)
1414 return false;
1417 return true;
1418 #else
1419 return false;
1420 #endif
1424 * pci_cfg_space_size - Get the configuration space size of the PCI device
1425 * @dev: PCI device
1427 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1428 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1429 * access it. Maybe we don't have a way to generate extended config space
1430 * accesses, or the device is behind a reverse Express bridge. So we try
1431 * reading the dword at 0x100 which must either be 0 or a valid extended
1432 * capability header.
1434 static int pci_cfg_space_size_ext(struct pci_dev *dev)
1436 u32 status;
1437 int pos = PCI_CFG_SPACE_SIZE;
1439 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1440 return PCI_CFG_SPACE_SIZE;
1441 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
1442 return PCI_CFG_SPACE_SIZE;
1444 return PCI_CFG_SPACE_EXP_SIZE;
1447 int pci_cfg_space_size(struct pci_dev *dev)
1449 int pos;
1450 u32 status;
1451 u16 class;
1453 if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
1454 return PCI_CFG_SPACE_SIZE;
1456 class = dev->class >> 8;
1457 if (class == PCI_CLASS_BRIDGE_HOST)
1458 return pci_cfg_space_size_ext(dev);
1460 if (pci_is_pcie(dev))
1461 return pci_cfg_space_size_ext(dev);
1463 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1464 if (!pos)
1465 return PCI_CFG_SPACE_SIZE;
1467 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1468 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1469 return pci_cfg_space_size_ext(dev);
1471 return PCI_CFG_SPACE_SIZE;
1474 static u32 pci_class(struct pci_dev *dev)
1476 u32 class;
1478 #ifdef CONFIG_PCI_IOV
1479 if (dev->is_virtfn)
1480 return dev->physfn->sriov->class;
1481 #endif
1482 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
1483 return class;
1486 static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device)
1488 #ifdef CONFIG_PCI_IOV
1489 if (dev->is_virtfn) {
1490 *vendor = dev->physfn->sriov->subsystem_vendor;
1491 *device = dev->physfn->sriov->subsystem_device;
1492 return;
1494 #endif
1495 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor);
1496 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device);
1499 static u8 pci_hdr_type(struct pci_dev *dev)
1501 u8 hdr_type;
1503 #ifdef CONFIG_PCI_IOV
1504 if (dev->is_virtfn)
1505 return dev->physfn->sriov->hdr_type;
1506 #endif
1507 pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
1508 return hdr_type;
1511 #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
1513 static void pci_msi_setup_pci_dev(struct pci_dev *dev)
1516 * Disable the MSI hardware to avoid screaming interrupts
1517 * during boot. This is the power on reset default so
1518 * usually this should be a noop.
1520 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1521 if (dev->msi_cap)
1522 pci_msi_set_enable(dev, 0);
1524 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1525 if (dev->msix_cap)
1526 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1530 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
1531 * @dev: PCI device
1533 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
1534 * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1536 static int pci_intx_mask_broken(struct pci_dev *dev)
1538 u16 orig, toggle, new;
1540 pci_read_config_word(dev, PCI_COMMAND, &orig);
1541 toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1542 pci_write_config_word(dev, PCI_COMMAND, toggle);
1543 pci_read_config_word(dev, PCI_COMMAND, &new);
1545 pci_write_config_word(dev, PCI_COMMAND, orig);
1548 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1549 * r2.3, so strictly speaking, a device is not *broken* if it's not
1550 * writable. But we'll live with the misnomer for now.
1552 if (new != toggle)
1553 return 1;
1554 return 0;
1557 static void early_dump_pci_device(struct pci_dev *pdev)
1559 u32 value[256 / 4];
1560 int i;
1562 pci_info(pdev, "config space:\n");
1564 for (i = 0; i < 256; i += 4)
1565 pci_read_config_dword(pdev, i, &value[i / 4]);
1567 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1568 value, 256, false);
1572 * pci_setup_device - Fill in class and map information of a device
1573 * @dev: the device structure to fill
1575 * Initialize the device structure with information about the device's
1576 * vendor,class,memory and IO-space addresses, IRQ lines etc.
1577 * Called at initialisation of the PCI subsystem and by CardBus services.
1578 * Returns 0 on success and negative if unknown type of device (not normal,
1579 * bridge or CardBus).
1581 int pci_setup_device(struct pci_dev *dev)
1583 u32 class;
1584 u16 cmd;
1585 u8 hdr_type;
1586 int pos = 0;
1587 struct pci_bus_region region;
1588 struct resource *res;
1590 hdr_type = pci_hdr_type(dev);
1592 dev->sysdata = dev->bus->sysdata;
1593 dev->dev.parent = dev->bus->bridge;
1594 dev->dev.bus = &pci_bus_type;
1595 dev->hdr_type = hdr_type & 0x7f;
1596 dev->multifunction = !!(hdr_type & 0x80);
1597 dev->error_state = pci_channel_io_normal;
1598 set_pcie_port_type(dev);
1600 pci_dev_assign_slot(dev);
1603 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1604 * set this higher, assuming the system even supports it.
1606 dev->dma_mask = 0xffffffff;
1608 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1609 dev->bus->number, PCI_SLOT(dev->devfn),
1610 PCI_FUNC(dev->devfn));
1612 class = pci_class(dev);
1614 dev->revision = class & 0xff;
1615 dev->class = class >> 8; /* upper 3 bytes */
1617 pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n",
1618 dev->vendor, dev->device, dev->hdr_type, dev->class);
1620 if (pci_early_dump)
1621 early_dump_pci_device(dev);
1623 /* Need to have dev->class ready */
1624 dev->cfg_size = pci_cfg_space_size(dev);
1626 /* Need to have dev->cfg_size ready */
1627 set_pcie_thunderbolt(dev);
1629 /* "Unknown power state" */
1630 dev->current_state = PCI_UNKNOWN;
1632 /* Early fixups, before probing the BARs */
1633 pci_fixup_device(pci_fixup_early, dev);
1635 /* Device class may be changed after fixup */
1636 class = dev->class >> 8;
1638 if (dev->non_compliant_bars && !dev->mmio_always_on) {
1639 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1640 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
1641 pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
1642 cmd &= ~PCI_COMMAND_IO;
1643 cmd &= ~PCI_COMMAND_MEMORY;
1644 pci_write_config_word(dev, PCI_COMMAND, cmd);
1648 dev->broken_intx_masking = pci_intx_mask_broken(dev);
1650 switch (dev->hdr_type) { /* header type */
1651 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1652 if (class == PCI_CLASS_BRIDGE_PCI)
1653 goto bad;
1654 pci_read_irq(dev);
1655 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
1657 pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device);
1660 * Do the ugly legacy mode stuff here rather than broken chip
1661 * quirk code. Legacy mode ATA controllers have fixed
1662 * addresses. These are not always echoed in BAR0-3, and
1663 * BAR0-3 in a few cases contain junk!
1665 if (class == PCI_CLASS_STORAGE_IDE) {
1666 u8 progif;
1667 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1668 if ((progif & 1) == 0) {
1669 region.start = 0x1F0;
1670 region.end = 0x1F7;
1671 res = &dev->resource[0];
1672 res->flags = LEGACY_IO_RESOURCE;
1673 pcibios_bus_to_resource(dev->bus, res, &region);
1674 pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n",
1675 res);
1676 region.start = 0x3F6;
1677 region.end = 0x3F6;
1678 res = &dev->resource[1];
1679 res->flags = LEGACY_IO_RESOURCE;
1680 pcibios_bus_to_resource(dev->bus, res, &region);
1681 pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n",
1682 res);
1684 if ((progif & 4) == 0) {
1685 region.start = 0x170;
1686 region.end = 0x177;
1687 res = &dev->resource[2];
1688 res->flags = LEGACY_IO_RESOURCE;
1689 pcibios_bus_to_resource(dev->bus, res, &region);
1690 pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n",
1691 res);
1692 region.start = 0x376;
1693 region.end = 0x376;
1694 res = &dev->resource[3];
1695 res->flags = LEGACY_IO_RESOURCE;
1696 pcibios_bus_to_resource(dev->bus, res, &region);
1697 pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n",
1698 res);
1701 break;
1703 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1704 if (class != PCI_CLASS_BRIDGE_PCI)
1705 goto bad;
1708 * The PCI-to-PCI bridge spec requires that subtractive
1709 * decoding (i.e. transparent) bridge must have programming
1710 * interface code of 0x01.
1712 pci_read_irq(dev);
1713 dev->transparent = ((dev->class & 0xff) == 1);
1714 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
1715 set_pcie_hotplug_bridge(dev);
1716 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1717 if (pos) {
1718 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1719 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1721 break;
1723 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1724 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1725 goto bad;
1726 pci_read_irq(dev);
1727 pci_read_bases(dev, 1, 0);
1728 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1729 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1730 break;
1732 default: /* unknown header */
1733 pci_err(dev, "unknown header type %02x, ignoring device\n",
1734 dev->hdr_type);
1735 return -EIO;
1737 bad:
1738 pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
1739 dev->class, dev->hdr_type);
1740 dev->class = PCI_CLASS_NOT_DEFINED << 8;
1743 /* We found a fine healthy device, go go go... */
1744 return 0;
1747 static void pci_configure_mps(struct pci_dev *dev)
1749 struct pci_dev *bridge = pci_upstream_bridge(dev);
1750 int mps, mpss, p_mps, rc;
1752 if (!pci_is_pcie(dev))
1753 return;
1755 /* MPS and MRRS fields are of type 'RsvdP' for VFs, short-circuit out */
1756 if (dev->is_virtfn)
1757 return;
1760 * For Root Complex Integrated Endpoints, program the maximum
1761 * supported value unless limited by the PCIE_BUS_PEER2PEER case.
1763 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) {
1764 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
1765 mps = 128;
1766 else
1767 mps = 128 << dev->pcie_mpss;
1768 rc = pcie_set_mps(dev, mps);
1769 if (rc) {
1770 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1771 mps);
1773 return;
1776 if (!bridge || !pci_is_pcie(bridge))
1777 return;
1779 mps = pcie_get_mps(dev);
1780 p_mps = pcie_get_mps(bridge);
1782 if (mps == p_mps)
1783 return;
1785 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
1786 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",
1787 mps, pci_name(bridge), p_mps);
1788 return;
1792 * Fancier MPS configuration is done later by
1793 * pcie_bus_configure_settings()
1795 if (pcie_bus_config != PCIE_BUS_DEFAULT)
1796 return;
1798 mpss = 128 << dev->pcie_mpss;
1799 if (mpss < p_mps && pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT) {
1800 pcie_set_mps(bridge, mpss);
1801 pci_info(dev, "Upstream bridge's Max Payload Size set to %d (was %d, max %d)\n",
1802 mpss, p_mps, 128 << bridge->pcie_mpss);
1803 p_mps = pcie_get_mps(bridge);
1806 rc = pcie_set_mps(dev, p_mps);
1807 if (rc) {
1808 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1809 p_mps);
1810 return;
1813 pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
1814 p_mps, mps, mpss);
1817 static struct hpp_type0 pci_default_type0 = {
1818 .revision = 1,
1819 .cache_line_size = 8,
1820 .latency_timer = 0x40,
1821 .enable_serr = 0,
1822 .enable_perr = 0,
1825 static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1827 u16 pci_cmd, pci_bctl;
1829 if (!hpp)
1830 hpp = &pci_default_type0;
1832 if (hpp->revision > 1) {
1833 pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
1834 hpp->revision);
1835 hpp = &pci_default_type0;
1838 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1839 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1840 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1841 if (hpp->enable_serr)
1842 pci_cmd |= PCI_COMMAND_SERR;
1843 if (hpp->enable_perr)
1844 pci_cmd |= PCI_COMMAND_PARITY;
1845 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1847 /* Program bridge control value */
1848 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1849 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1850 hpp->latency_timer);
1851 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1852 if (hpp->enable_serr)
1853 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1854 if (hpp->enable_perr)
1855 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1856 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1860 static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1862 int pos;
1864 if (!hpp)
1865 return;
1867 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1868 if (!pos)
1869 return;
1871 pci_warn(dev, "PCI-X settings not supported\n");
1874 static bool pcie_root_rcb_set(struct pci_dev *dev)
1876 struct pci_dev *rp = pcie_find_root_port(dev);
1877 u16 lnkctl;
1879 if (!rp)
1880 return false;
1882 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
1883 if (lnkctl & PCI_EXP_LNKCTL_RCB)
1884 return true;
1886 return false;
1889 static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1891 int pos;
1892 u32 reg32;
1894 if (!hpp)
1895 return;
1897 if (!pci_is_pcie(dev))
1898 return;
1900 if (hpp->revision > 1) {
1901 pci_warn(dev, "PCIe settings rev %d not supported\n",
1902 hpp->revision);
1903 return;
1907 * Don't allow _HPX to change MPS or MRRS settings. We manage
1908 * those to make sure they're consistent with the rest of the
1909 * platform.
1911 hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
1912 PCI_EXP_DEVCTL_READRQ;
1913 hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
1914 PCI_EXP_DEVCTL_READRQ);
1916 /* Initialize Device Control Register */
1917 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1918 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1920 /* Initialize Link Control Register */
1921 if (pcie_cap_has_lnkctl(dev)) {
1924 * If the Root Port supports Read Completion Boundary of
1925 * 128, set RCB to 128. Otherwise, clear it.
1927 hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
1928 hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
1929 if (pcie_root_rcb_set(dev))
1930 hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
1932 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1933 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
1936 /* Find Advanced Error Reporting Enhanced Capability */
1937 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1938 if (!pos)
1939 return;
1941 /* Initialize Uncorrectable Error Mask Register */
1942 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1943 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1944 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1946 /* Initialize Uncorrectable Error Severity Register */
1947 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1948 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1949 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1951 /* Initialize Correctable Error Mask Register */
1952 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1953 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1954 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1956 /* Initialize Advanced Error Capabilities and Control Register */
1957 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1958 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
1960 /* Don't enable ECRC generation or checking if unsupported */
1961 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
1962 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
1963 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
1964 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
1965 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1968 * FIXME: The following two registers are not supported yet.
1970 * o Secondary Uncorrectable Error Severity Register
1971 * o Secondary Uncorrectable Error Mask Register
1975 int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
1977 struct pci_host_bridge *host;
1978 u32 cap;
1979 u16 ctl;
1980 int ret;
1982 if (!pci_is_pcie(dev))
1983 return 0;
1985 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
1986 if (ret)
1987 return 0;
1989 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
1990 return 0;
1992 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
1993 if (ret)
1994 return 0;
1996 host = pci_find_host_bridge(dev->bus);
1997 if (!host)
1998 return 0;
2001 * If some device in the hierarchy doesn't handle Extended Tags
2002 * correctly, make sure they're disabled.
2004 if (host->no_ext_tags) {
2005 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
2006 pci_info(dev, "disabling Extended Tags\n");
2007 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
2008 PCI_EXP_DEVCTL_EXT_TAG);
2010 return 0;
2013 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
2014 pci_info(dev, "enabling Extended Tags\n");
2015 pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
2016 PCI_EXP_DEVCTL_EXT_TAG);
2018 return 0;
2022 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
2023 * @dev: PCI device to query
2025 * Returns true if the device has enabled relaxed ordering attribute.
2027 bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
2029 u16 v;
2031 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
2033 return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
2035 EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
2037 static void pci_configure_relaxed_ordering(struct pci_dev *dev)
2039 struct pci_dev *root;
2041 /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
2042 if (dev->is_virtfn)
2043 return;
2045 if (!pcie_relaxed_ordering_enabled(dev))
2046 return;
2049 * For now, we only deal with Relaxed Ordering issues with Root
2050 * Ports. Peer-to-Peer DMA is another can of worms.
2052 root = pci_find_pcie_root_port(dev);
2053 if (!root)
2054 return;
2056 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
2057 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
2058 PCI_EXP_DEVCTL_RELAX_EN);
2059 pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
2063 static void pci_configure_ltr(struct pci_dev *dev)
2065 #ifdef CONFIG_PCIEASPM
2066 struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
2067 struct pci_dev *bridge;
2068 u32 cap, ctl;
2070 if (!pci_is_pcie(dev))
2071 return;
2073 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
2074 if (!(cap & PCI_EXP_DEVCAP2_LTR))
2075 return;
2077 pcie_capability_read_dword(dev, PCI_EXP_DEVCTL2, &ctl);
2078 if (ctl & PCI_EXP_DEVCTL2_LTR_EN) {
2079 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
2080 dev->ltr_path = 1;
2081 return;
2084 bridge = pci_upstream_bridge(dev);
2085 if (bridge && bridge->ltr_path)
2086 dev->ltr_path = 1;
2088 return;
2091 if (!host->native_ltr)
2092 return;
2095 * Software must not enable LTR in an Endpoint unless the Root
2096 * Complex and all intermediate Switches indicate support for LTR.
2097 * PCIe r4.0, sec 6.18.
2099 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
2100 ((bridge = pci_upstream_bridge(dev)) &&
2101 bridge->ltr_path)) {
2102 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
2103 PCI_EXP_DEVCTL2_LTR_EN);
2104 dev->ltr_path = 1;
2106 #endif
2109 static void pci_configure_eetlp_prefix(struct pci_dev *dev)
2111 #ifdef CONFIG_PCI_PASID
2112 struct pci_dev *bridge;
2113 int pcie_type;
2114 u32 cap;
2116 if (!pci_is_pcie(dev))
2117 return;
2119 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
2120 if (!(cap & PCI_EXP_DEVCAP2_EE_PREFIX))
2121 return;
2123 pcie_type = pci_pcie_type(dev);
2124 if (pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
2125 pcie_type == PCI_EXP_TYPE_RC_END)
2126 dev->eetlp_prefix_path = 1;
2127 else {
2128 bridge = pci_upstream_bridge(dev);
2129 if (bridge && bridge->eetlp_prefix_path)
2130 dev->eetlp_prefix_path = 1;
2132 #endif
2135 static void pci_configure_device(struct pci_dev *dev)
2137 struct hotplug_params hpp;
2138 int ret;
2140 pci_configure_mps(dev);
2141 pci_configure_extended_tags(dev, NULL);
2142 pci_configure_relaxed_ordering(dev);
2143 pci_configure_ltr(dev);
2144 pci_configure_eetlp_prefix(dev);
2146 memset(&hpp, 0, sizeof(hpp));
2147 ret = pci_get_hp_params(dev, &hpp);
2148 if (ret)
2149 return;
2151 program_hpp_type2(dev, hpp.t2);
2152 program_hpp_type1(dev, hpp.t1);
2153 program_hpp_type0(dev, hpp.t0);
2156 static void pci_release_capabilities(struct pci_dev *dev)
2158 pci_aer_exit(dev);
2159 pci_vpd_release(dev);
2160 pci_iov_release(dev);
2161 pci_free_cap_save_buffers(dev);
2165 * pci_release_dev - Free a PCI device structure when all users of it are
2166 * finished
2167 * @dev: device that's been disconnected
2169 * Will be called only by the device core when all users of this PCI device are
2170 * done.
2172 static void pci_release_dev(struct device *dev)
2174 struct pci_dev *pci_dev;
2176 pci_dev = to_pci_dev(dev);
2177 pci_release_capabilities(pci_dev);
2178 pci_release_of_node(pci_dev);
2179 pcibios_release_device(pci_dev);
2180 pci_bus_put(pci_dev->bus);
2181 kfree(pci_dev->driver_override);
2182 kfree(pci_dev->dma_alias_mask);
2183 kfree(pci_dev);
2186 struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
2188 struct pci_dev *dev;
2190 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
2191 if (!dev)
2192 return NULL;
2194 INIT_LIST_HEAD(&dev->bus_list);
2195 dev->dev.type = &pci_dev_type;
2196 dev->bus = pci_bus_get(bus);
2198 return dev;
2200 EXPORT_SYMBOL(pci_alloc_dev);
2202 static bool pci_bus_crs_vendor_id(u32 l)
2204 return (l & 0xffff) == 0x0001;
2207 static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
2208 int timeout)
2210 int delay = 1;
2212 if (!pci_bus_crs_vendor_id(*l))
2213 return true; /* not a CRS completion */
2215 if (!timeout)
2216 return false; /* CRS, but caller doesn't want to wait */
2219 * We got the reserved Vendor ID that indicates a completion with
2220 * Configuration Request Retry Status (CRS). Retry until we get a
2221 * valid Vendor ID or we time out.
2223 while (pci_bus_crs_vendor_id(*l)) {
2224 if (delay > timeout) {
2225 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
2226 pci_domain_nr(bus), bus->number,
2227 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2229 return false;
2231 if (delay >= 1000)
2232 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
2233 pci_domain_nr(bus), bus->number,
2234 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2236 msleep(delay);
2237 delay *= 2;
2239 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2240 return false;
2243 if (delay >= 1000)
2244 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
2245 pci_domain_nr(bus), bus->number,
2246 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2248 return true;
2251 bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2252 int timeout)
2254 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2255 return false;
2257 /* Some broken boards return 0 or ~0 if a slot is empty: */
2258 if (*l == 0xffffffff || *l == 0x00000000 ||
2259 *l == 0x0000ffff || *l == 0xffff0000)
2260 return false;
2262 if (pci_bus_crs_vendor_id(*l))
2263 return pci_bus_wait_crs(bus, devfn, l, timeout);
2265 return true;
2268 bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2269 int timeout)
2271 #ifdef CONFIG_PCI_QUIRKS
2272 struct pci_dev *bridge = bus->self;
2275 * Certain IDT switches have an issue where they improperly trigger
2276 * ACS Source Validation errors on completions for config reads.
2278 if (bridge && bridge->vendor == PCI_VENDOR_ID_IDT &&
2279 bridge->device == 0x80b5)
2280 return pci_idt_bus_quirk(bus, devfn, l, timeout);
2281 #endif
2283 return pci_bus_generic_read_dev_vendor_id(bus, devfn, l, timeout);
2285 EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2288 * Read the config data for a PCI device, sanity-check it,
2289 * and fill in the dev structure.
2291 static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
2293 struct pci_dev *dev;
2294 u32 l;
2296 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
2297 return NULL;
2299 dev = pci_alloc_dev(bus);
2300 if (!dev)
2301 return NULL;
2303 dev->devfn = devfn;
2304 dev->vendor = l & 0xffff;
2305 dev->device = (l >> 16) & 0xffff;
2307 pci_set_of_node(dev);
2309 if (pci_setup_device(dev)) {
2310 pci_bus_put(dev->bus);
2311 kfree(dev);
2312 return NULL;
2315 return dev;
2318 static void pcie_report_downtraining(struct pci_dev *dev)
2320 if (!pci_is_pcie(dev))
2321 return;
2323 /* Look from the device up to avoid downstream ports with no devices */
2324 if ((pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT) &&
2325 (pci_pcie_type(dev) != PCI_EXP_TYPE_LEG_END) &&
2326 (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM))
2327 return;
2329 /* Multi-function PCIe devices share the same link/status */
2330 if (PCI_FUNC(dev->devfn) != 0 || dev->is_virtfn)
2331 return;
2333 /* Print link status only if the device is constrained by the fabric */
2334 __pcie_print_link_status(dev, false);
2337 static void pci_init_capabilities(struct pci_dev *dev)
2339 /* Enhanced Allocation */
2340 pci_ea_init(dev);
2342 /* Setup MSI caps & disable MSI/MSI-X interrupts */
2343 pci_msi_setup_pci_dev(dev);
2345 /* Buffers for saving PCIe and PCI-X capabilities */
2346 pci_allocate_cap_save_buffers(dev);
2348 /* Power Management */
2349 pci_pm_init(dev);
2351 /* Vital Product Data */
2352 pci_vpd_init(dev);
2354 /* Alternative Routing-ID Forwarding */
2355 pci_configure_ari(dev);
2357 /* Single Root I/O Virtualization */
2358 pci_iov_init(dev);
2360 /* Address Translation Services */
2361 pci_ats_init(dev);
2363 /* Enable ACS P2P upstream forwarding */
2364 pci_enable_acs(dev);
2366 /* Precision Time Measurement */
2367 pci_ptm_init(dev);
2369 /* Advanced Error Reporting */
2370 pci_aer_init(dev);
2372 pcie_report_downtraining(dev);
2374 if (pci_probe_reset_function(dev) == 0)
2375 dev->reset_fn = 1;
2379 * This is the equivalent of pci_host_bridge_msi_domain() that acts on
2380 * devices. Firmware interfaces that can select the MSI domain on a
2381 * per-device basis should be called from here.
2383 static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2385 struct irq_domain *d;
2388 * If a domain has been set through the pcibios_add_device()
2389 * callback, then this is the one (platform code knows best).
2391 d = dev_get_msi_domain(&dev->dev);
2392 if (d)
2393 return d;
2396 * Let's see if we have a firmware interface able to provide
2397 * the domain.
2399 d = pci_msi_get_device_domain(dev);
2400 if (d)
2401 return d;
2403 return NULL;
2406 static void pci_set_msi_domain(struct pci_dev *dev)
2408 struct irq_domain *d;
2411 * If the platform or firmware interfaces cannot supply a
2412 * device-specific MSI domain, then inherit the default domain
2413 * from the host bridge itself.
2415 d = pci_dev_msi_domain(dev);
2416 if (!d)
2417 d = dev_get_msi_domain(&dev->bus->dev);
2419 dev_set_msi_domain(&dev->dev, d);
2422 void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
2424 int ret;
2426 pci_configure_device(dev);
2428 device_initialize(&dev->dev);
2429 dev->dev.release = pci_release_dev;
2431 set_dev_node(&dev->dev, pcibus_to_node(bus));
2432 dev->dev.dma_mask = &dev->dma_mask;
2433 dev->dev.dma_parms = &dev->dma_parms;
2434 dev->dev.coherent_dma_mask = 0xffffffffull;
2436 pci_set_dma_max_seg_size(dev, 65536);
2437 pci_set_dma_seg_boundary(dev, 0xffffffff);
2439 /* Fix up broken headers */
2440 pci_fixup_device(pci_fixup_header, dev);
2442 /* Moved out from quirk header fixup code */
2443 pci_reassigndev_resource_alignment(dev);
2445 /* Clear the state_saved flag */
2446 dev->state_saved = false;
2448 /* Initialize various capabilities */
2449 pci_init_capabilities(dev);
2452 * Add the device to our list of discovered devices
2453 * and the bus list for fixup functions, etc.
2455 down_write(&pci_bus_sem);
2456 list_add_tail(&dev->bus_list, &bus->devices);
2457 up_write(&pci_bus_sem);
2459 ret = pcibios_add_device(dev);
2460 WARN_ON(ret < 0);
2462 /* Set up MSI IRQ domain */
2463 pci_set_msi_domain(dev);
2465 /* Notifier could use PCI capabilities */
2466 dev->match_driver = false;
2467 ret = device_add(&dev->dev);
2468 WARN_ON(ret < 0);
2471 struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
2473 struct pci_dev *dev;
2475 dev = pci_get_slot(bus, devfn);
2476 if (dev) {
2477 pci_dev_put(dev);
2478 return dev;
2481 dev = pci_scan_device(bus, devfn);
2482 if (!dev)
2483 return NULL;
2485 pci_device_add(dev, bus);
2487 return dev;
2489 EXPORT_SYMBOL(pci_scan_single_device);
2491 static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
2493 int pos;
2494 u16 cap = 0;
2495 unsigned next_fn;
2497 if (pci_ari_enabled(bus)) {
2498 if (!dev)
2499 return 0;
2500 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2501 if (!pos)
2502 return 0;
2504 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2505 next_fn = PCI_ARI_CAP_NFN(cap);
2506 if (next_fn <= fn)
2507 return 0; /* protect against malformed list */
2509 return next_fn;
2512 /* dev may be NULL for non-contiguous multifunction devices */
2513 if (!dev || dev->multifunction)
2514 return (fn + 1) % 8;
2516 return 0;
2519 static int only_one_child(struct pci_bus *bus)
2521 struct pci_dev *bridge = bus->self;
2524 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
2525 * we scan for all possible devices, not just Device 0.
2527 if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
2528 return 0;
2531 * A PCIe Downstream Port normally leads to a Link with only Device
2532 * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan
2533 * only for Device 0 in that situation.
2535 * Checking has_secondary_link is a hack to identify Downstream
2536 * Ports because sometimes Switches are configured such that the
2537 * PCIe Port Type labels are backwards.
2539 if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link)
2540 return 1;
2542 return 0;
2546 * pci_scan_slot - Scan a PCI slot on a bus for devices
2547 * @bus: PCI bus to scan
2548 * @devfn: slot number to scan (must have zero function)
2550 * Scan a PCI slot on the specified PCI bus for devices, adding
2551 * discovered devices to the @bus->devices list. New devices
2552 * will not have is_added set.
2554 * Returns the number of new devices found.
2556 int pci_scan_slot(struct pci_bus *bus, int devfn)
2558 unsigned fn, nr = 0;
2559 struct pci_dev *dev;
2561 if (only_one_child(bus) && (devfn > 0))
2562 return 0; /* Already scanned the entire slot */
2564 dev = pci_scan_single_device(bus, devfn);
2565 if (!dev)
2566 return 0;
2567 if (!pci_dev_is_added(dev))
2568 nr++;
2570 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
2571 dev = pci_scan_single_device(bus, devfn + fn);
2572 if (dev) {
2573 if (!pci_dev_is_added(dev))
2574 nr++;
2575 dev->multifunction = 1;
2579 /* Only one slot has PCIe device */
2580 if (bus->self && nr)
2581 pcie_aspm_init_link_state(bus->self);
2583 return nr;
2585 EXPORT_SYMBOL(pci_scan_slot);
2587 static int pcie_find_smpss(struct pci_dev *dev, void *data)
2589 u8 *smpss = data;
2591 if (!pci_is_pcie(dev))
2592 return 0;
2595 * We don't have a way to change MPS settings on devices that have
2596 * drivers attached. A hot-added device might support only the minimum
2597 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
2598 * where devices may be hot-added, we limit the fabric MPS to 128 so
2599 * hot-added devices will work correctly.
2601 * However, if we hot-add a device to a slot directly below a Root
2602 * Port, it's impossible for there to be other existing devices below
2603 * the port. We don't limit the MPS in this case because we can
2604 * reconfigure MPS on both the Root Port and the hot-added device,
2605 * and there are no other devices involved.
2607 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
2609 if (dev->is_hotplug_bridge &&
2610 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
2611 *smpss = 0;
2613 if (*smpss > dev->pcie_mpss)
2614 *smpss = dev->pcie_mpss;
2616 return 0;
2619 static void pcie_write_mps(struct pci_dev *dev, int mps)
2621 int rc;
2623 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
2624 mps = 128 << dev->pcie_mpss;
2626 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2627 dev->bus->self)
2630 * For "Performance", the assumption is made that
2631 * downstream communication will never be larger than
2632 * the MRRS. So, the MPS only needs to be configured
2633 * for the upstream communication. This being the case,
2634 * walk from the top down and set the MPS of the child
2635 * to that of the parent bus.
2637 * Configure the device MPS with the smaller of the
2638 * device MPSS or the bridge MPS (which is assumed to be
2639 * properly configured at this point to the largest
2640 * allowable MPS based on its parent bus).
2642 mps = min(mps, pcie_get_mps(dev->bus->self));
2645 rc = pcie_set_mps(dev, mps);
2646 if (rc)
2647 pci_err(dev, "Failed attempting to set the MPS\n");
2650 static void pcie_write_mrrs(struct pci_dev *dev)
2652 int rc, mrrs;
2655 * In the "safe" case, do not configure the MRRS. There appear to be
2656 * issues with setting MRRS to 0 on a number of devices.
2658 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2659 return;
2662 * For max performance, the MRRS must be set to the largest supported
2663 * value. However, it cannot be configured larger than the MPS the
2664 * device or the bus can support. This should already be properly
2665 * configured by a prior call to pcie_write_mps().
2667 mrrs = pcie_get_mps(dev);
2670 * MRRS is a R/W register. Invalid values can be written, but a
2671 * subsequent read will verify if the value is acceptable or not.
2672 * If the MRRS value provided is not acceptable (e.g., too large),
2673 * shrink the value until it is acceptable to the HW.
2675 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2676 rc = pcie_set_readrq(dev, mrrs);
2677 if (!rc)
2678 break;
2680 pci_warn(dev, "Failed attempting to set the MRRS\n");
2681 mrrs /= 2;
2684 if (mrrs < 128)
2685 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");
2688 static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2690 int mps, orig_mps;
2692 if (!pci_is_pcie(dev))
2693 return 0;
2695 if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2696 pcie_bus_config == PCIE_BUS_DEFAULT)
2697 return 0;
2699 mps = 128 << *(u8 *)data;
2700 orig_mps = pcie_get_mps(dev);
2702 pcie_write_mps(dev, mps);
2703 pcie_write_mrrs(dev);
2705 pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
2706 pcie_get_mps(dev), 128 << dev->pcie_mpss,
2707 orig_mps, pcie_get_readrq(dev));
2709 return 0;
2713 * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
2714 * parents then children fashion. If this changes, then this code will not
2715 * work as designed.
2717 void pcie_bus_configure_settings(struct pci_bus *bus)
2719 u8 smpss = 0;
2721 if (!bus->self)
2722 return;
2724 if (!pci_is_pcie(bus->self))
2725 return;
2728 * FIXME - Peer to peer DMA is possible, though the endpoint would need
2729 * to be aware of the MPS of the destination. To work around this,
2730 * simply force the MPS of the entire system to the smallest possible.
2732 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2733 smpss = 0;
2735 if (pcie_bus_config == PCIE_BUS_SAFE) {
2736 smpss = bus->self->pcie_mpss;
2738 pcie_find_smpss(bus->self, &smpss);
2739 pci_walk_bus(bus, pcie_find_smpss, &smpss);
2742 pcie_bus_configure_set(bus->self, &smpss);
2743 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2745 EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
2748 * Called after each bus is probed, but before its children are examined. This
2749 * is marked as __weak because multiple architectures define it.
2751 void __weak pcibios_fixup_bus(struct pci_bus *bus)
2753 /* nothing to do, expected to be removed in the future */
2757 * pci_scan_child_bus_extend() - Scan devices below a bus
2758 * @bus: Bus to scan for devices
2759 * @available_buses: Total number of buses available (%0 does not try to
2760 * extend beyond the minimal)
2762 * Scans devices below @bus including subordinate buses. Returns new
2763 * subordinate number including all the found devices. Passing
2764 * @available_buses causes the remaining bus space to be distributed
2765 * equally between hotplug-capable bridges to allow future extension of the
2766 * hierarchy.
2768 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2769 unsigned int available_buses)
2771 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2772 unsigned int start = bus->busn_res.start;
2773 unsigned int devfn, fn, cmax, max = start;
2774 struct pci_dev *dev;
2775 int nr_devs;
2777 dev_dbg(&bus->dev, "scanning bus\n");
2779 /* Go find them, Rover! */
2780 for (devfn = 0; devfn < 256; devfn += 8) {
2781 nr_devs = pci_scan_slot(bus, devfn);
2784 * The Jailhouse hypervisor may pass individual functions of a
2785 * multi-function device to a guest without passing function 0.
2786 * Look for them as well.
2788 if (jailhouse_paravirt() && nr_devs == 0) {
2789 for (fn = 1; fn < 8; fn++) {
2790 dev = pci_scan_single_device(bus, devfn + fn);
2791 if (dev)
2792 dev->multifunction = 1;
2797 /* Reserve buses for SR-IOV capability */
2798 used_buses = pci_iov_bus_range(bus);
2799 max += used_buses;
2802 * After performing arch-dependent fixup of the bus, look behind
2803 * all PCI-to-PCI bridges on this bus.
2805 if (!bus->is_added) {
2806 dev_dbg(&bus->dev, "fixups for bus\n");
2807 pcibios_fixup_bus(bus);
2808 bus->is_added = 1;
2812 * Calculate how many hotplug bridges and normal bridges there
2813 * are on this bus. We will distribute the additional available
2814 * buses between hotplug bridges.
2816 for_each_pci_bridge(dev, bus) {
2817 if (dev->is_hotplug_bridge)
2818 hotplug_bridges++;
2819 else
2820 normal_bridges++;
2824 * Scan bridges that are already configured. We don't touch them
2825 * unless they are misconfigured (which will be done in the second
2826 * scan below).
2828 for_each_pci_bridge(dev, bus) {
2829 cmax = max;
2830 max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
2833 * Reserve one bus for each bridge now to avoid extending
2834 * hotplug bridges too much during the second scan below.
2836 used_buses++;
2837 if (cmax - max > 1)
2838 used_buses += cmax - max - 1;
2841 /* Scan bridges that need to be reconfigured */
2842 for_each_pci_bridge(dev, bus) {
2843 unsigned int buses = 0;
2845 if (!hotplug_bridges && normal_bridges == 1) {
2848 * There is only one bridge on the bus (upstream
2849 * port) so it gets all available buses which it
2850 * can then distribute to the possible hotplug
2851 * bridges below.
2853 buses = available_buses;
2854 } else if (dev->is_hotplug_bridge) {
2857 * Distribute the extra buses between hotplug
2858 * bridges if any.
2860 buses = available_buses / hotplug_bridges;
2861 buses = min(buses, available_buses - used_buses + 1);
2864 cmax = max;
2865 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
2866 /* One bus is already accounted so don't add it again */
2867 if (max - cmax > 1)
2868 used_buses += max - cmax - 1;
2872 * Make sure a hotplug bridge has at least the minimum requested
2873 * number of buses but allow it to grow up to the maximum available
2874 * bus number of there is room.
2876 if (bus->self && bus->self->is_hotplug_bridge) {
2877 used_buses = max_t(unsigned int, available_buses,
2878 pci_hotplug_bus_size - 1);
2879 if (max - start < used_buses) {
2880 max = start + used_buses;
2882 /* Do not allocate more buses than we have room left */
2883 if (max > bus->busn_res.end)
2884 max = bus->busn_res.end;
2886 dev_dbg(&bus->dev, "%pR extended by %#02x\n",
2887 &bus->busn_res, max - start);
2892 * We've scanned the bus and so we know all about what's on
2893 * the other side of any bridges that may be on this bus plus
2894 * any devices.
2896 * Return how far we've got finding sub-buses.
2898 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
2899 return max;
2903 * pci_scan_child_bus() - Scan devices below a bus
2904 * @bus: Bus to scan for devices
2906 * Scans devices below @bus including subordinate buses. Returns new
2907 * subordinate number including all the found devices.
2909 unsigned int pci_scan_child_bus(struct pci_bus *bus)
2911 return pci_scan_child_bus_extend(bus, 0);
2913 EXPORT_SYMBOL_GPL(pci_scan_child_bus);
2916 * pcibios_root_bridge_prepare - Platform-specific host bridge setup
2917 * @bridge: Host bridge to set up
2919 * Default empty implementation. Replace with an architecture-specific setup
2920 * routine, if necessary.
2922 int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
2924 return 0;
2927 void __weak pcibios_add_bus(struct pci_bus *bus)
2931 void __weak pcibios_remove_bus(struct pci_bus *bus)
2935 struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
2936 struct pci_ops *ops, void *sysdata, struct list_head *resources)
2938 int error;
2939 struct pci_host_bridge *bridge;
2941 bridge = pci_alloc_host_bridge(0);
2942 if (!bridge)
2943 return NULL;
2945 bridge->dev.parent = parent;
2947 list_splice_init(resources, &bridge->windows);
2948 bridge->sysdata = sysdata;
2949 bridge->busnr = bus;
2950 bridge->ops = ops;
2952 error = pci_register_host_bridge(bridge);
2953 if (error < 0)
2954 goto err_out;
2956 return bridge->bus;
2958 err_out:
2959 kfree(bridge);
2960 return NULL;
2962 EXPORT_SYMBOL_GPL(pci_create_root_bus);
2964 int pci_host_probe(struct pci_host_bridge *bridge)
2966 struct pci_bus *bus, *child;
2967 int ret;
2969 ret = pci_scan_root_bus_bridge(bridge);
2970 if (ret < 0) {
2971 dev_err(bridge->dev.parent, "Scanning root bridge failed");
2972 return ret;
2975 bus = bridge->bus;
2978 * We insert PCI resources into the iomem_resource and
2979 * ioport_resource trees in either pci_bus_claim_resources()
2980 * or pci_bus_assign_resources().
2982 if (pci_has_flag(PCI_PROBE_ONLY)) {
2983 pci_bus_claim_resources(bus);
2984 } else {
2985 pci_bus_size_bridges(bus);
2986 pci_bus_assign_resources(bus);
2988 list_for_each_entry(child, &bus->children, node)
2989 pcie_bus_configure_settings(child);
2992 pci_bus_add_devices(bus);
2993 return 0;
2995 EXPORT_SYMBOL_GPL(pci_host_probe);
2997 int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2999 struct resource *res = &b->busn_res;
3000 struct resource *parent_res, *conflict;
3002 res->start = bus;
3003 res->end = bus_max;
3004 res->flags = IORESOURCE_BUS;
3006 if (!pci_is_root_bus(b))
3007 parent_res = &b->parent->busn_res;
3008 else {
3009 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
3010 res->flags |= IORESOURCE_PCI_FIXED;
3013 conflict = request_resource_conflict(parent_res, res);
3015 if (conflict)
3016 dev_printk(KERN_DEBUG, &b->dev,
3017 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
3018 res, pci_is_root_bus(b) ? "domain " : "",
3019 parent_res, conflict->name, conflict);
3021 return conflict == NULL;
3024 int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
3026 struct resource *res = &b->busn_res;
3027 struct resource old_res = *res;
3028 resource_size_t size;
3029 int ret;
3031 if (res->start > bus_max)
3032 return -EINVAL;
3034 size = bus_max - res->start + 1;
3035 ret = adjust_resource(res, res->start, size);
3036 dev_printk(KERN_DEBUG, &b->dev,
3037 "busn_res: %pR end %s updated to %02x\n",
3038 &old_res, ret ? "can not be" : "is", bus_max);
3040 if (!ret && !res->parent)
3041 pci_bus_insert_busn_res(b, res->start, res->end);
3043 return ret;
3046 void pci_bus_release_busn_res(struct pci_bus *b)
3048 struct resource *res = &b->busn_res;
3049 int ret;
3051 if (!res->flags || !res->parent)
3052 return;
3054 ret = release_resource(res);
3055 dev_printk(KERN_DEBUG, &b->dev,
3056 "busn_res: %pR %s released\n",
3057 res, ret ? "can not be" : "is");
3060 int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
3062 struct resource_entry *window;
3063 bool found = false;
3064 struct pci_bus *b;
3065 int max, bus, ret;
3067 if (!bridge)
3068 return -EINVAL;
3070 resource_list_for_each_entry(window, &bridge->windows)
3071 if (window->res->flags & IORESOURCE_BUS) {
3072 found = true;
3073 break;
3076 ret = pci_register_host_bridge(bridge);
3077 if (ret < 0)
3078 return ret;
3080 b = bridge->bus;
3081 bus = bridge->busnr;
3083 if (!found) {
3084 dev_info(&b->dev,
3085 "No busn resource found for root bus, will use [bus %02x-ff]\n",
3086 bus);
3087 pci_bus_insert_busn_res(b, bus, 255);
3090 max = pci_scan_child_bus(b);
3092 if (!found)
3093 pci_bus_update_busn_res_end(b, max);
3095 return 0;
3097 EXPORT_SYMBOL(pci_scan_root_bus_bridge);
3099 struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
3100 struct pci_ops *ops, void *sysdata, struct list_head *resources)
3102 struct resource_entry *window;
3103 bool found = false;
3104 struct pci_bus *b;
3105 int max;
3107 resource_list_for_each_entry(window, resources)
3108 if (window->res->flags & IORESOURCE_BUS) {
3109 found = true;
3110 break;
3113 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
3114 if (!b)
3115 return NULL;
3117 if (!found) {
3118 dev_info(&b->dev,
3119 "No busn resource found for root bus, will use [bus %02x-ff]\n",
3120 bus);
3121 pci_bus_insert_busn_res(b, bus, 255);
3124 max = pci_scan_child_bus(b);
3126 if (!found)
3127 pci_bus_update_busn_res_end(b, max);
3129 return b;
3131 EXPORT_SYMBOL(pci_scan_root_bus);
3133 struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
3134 void *sysdata)
3136 LIST_HEAD(resources);
3137 struct pci_bus *b;
3139 pci_add_resource(&resources, &ioport_resource);
3140 pci_add_resource(&resources, &iomem_resource);
3141 pci_add_resource(&resources, &busn_resource);
3142 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
3143 if (b) {
3144 pci_scan_child_bus(b);
3145 } else {
3146 pci_free_resource_list(&resources);
3148 return b;
3150 EXPORT_SYMBOL(pci_scan_bus);
3153 * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
3154 * @bridge: PCI bridge for the bus to scan
3156 * Scan a PCI bus and child buses for new devices, add them,
3157 * and enable them, resizing bridge mmio/io resource if necessary
3158 * and possible. The caller must ensure the child devices are already
3159 * removed for resizing to occur.
3161 * Returns the max number of subordinate bus discovered.
3163 unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
3165 unsigned int max;
3166 struct pci_bus *bus = bridge->subordinate;
3168 max = pci_scan_child_bus(bus);
3170 pci_assign_unassigned_bridge_resources(bridge);
3172 pci_bus_add_devices(bus);
3174 return max;
3178 * pci_rescan_bus - Scan a PCI bus for devices
3179 * @bus: PCI bus to scan
3181 * Scan a PCI bus and child buses for new devices, add them,
3182 * and enable them.
3184 * Returns the max number of subordinate bus discovered.
3186 unsigned int pci_rescan_bus(struct pci_bus *bus)
3188 unsigned int max;
3190 max = pci_scan_child_bus(bus);
3191 pci_assign_unassigned_bus_resources(bus);
3192 pci_bus_add_devices(bus);
3194 return max;
3196 EXPORT_SYMBOL_GPL(pci_rescan_bus);
3199 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
3200 * routines should always be executed under this mutex.
3202 static DEFINE_MUTEX(pci_rescan_remove_lock);
3204 void pci_lock_rescan_remove(void)
3206 mutex_lock(&pci_rescan_remove_lock);
3208 EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
3210 void pci_unlock_rescan_remove(void)
3212 mutex_unlock(&pci_rescan_remove_lock);
3214 EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
3216 static int __init pci_sort_bf_cmp(const struct device *d_a,
3217 const struct device *d_b)
3219 const struct pci_dev *a = to_pci_dev(d_a);
3220 const struct pci_dev *b = to_pci_dev(d_b);
3222 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
3223 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
3225 if (a->bus->number < b->bus->number) return -1;
3226 else if (a->bus->number > b->bus->number) return 1;
3228 if (a->devfn < b->devfn) return -1;
3229 else if (a->devfn > b->devfn) return 1;
3231 return 0;
3234 void __init pci_sort_breadthfirst(void)
3236 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
3239 int pci_hp_add_bridge(struct pci_dev *dev)
3241 struct pci_bus *parent = dev->bus;
3242 int busnr, start = parent->busn_res.start;
3243 unsigned int available_buses = 0;
3244 int end = parent->busn_res.end;
3246 for (busnr = start; busnr <= end; busnr++) {
3247 if (!pci_find_bus(pci_domain_nr(parent), busnr))
3248 break;
3250 if (busnr-- > end) {
3251 pci_err(dev, "No bus number available for hot-added bridge\n");
3252 return -1;
3255 /* Scan bridges that are already configured */
3256 busnr = pci_scan_bridge(parent, dev, busnr, 0);
3259 * Distribute the available bus numbers between hotplug-capable
3260 * bridges to make extending the chain later possible.
3262 available_buses = end - busnr;
3264 /* Scan bridges that need to be reconfigured */
3265 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
3267 if (!dev->subordinate)
3268 return -1;
3270 return 0;
3272 EXPORT_SYMBOL_GPL(pci_hp_add_bridge);