Input: hilkbd - Add casts to HP9000/300 I/O accessors
[linux/fpc-iii.git] / drivers / pci / probe.c
blobac876e32de4b0fe4aaebf98209590720375daa50
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/pci-aspm.h>
17 #include <linux/aer.h>
18 #include <linux/acpi.h>
19 #include <linux/hypervisor.h>
20 #include <linux/irqdomain.h>
21 #include <linux/pm_runtime.h>
22 #include "pci.h"
24 #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
25 #define CARDBUS_RESERVE_BUSNR 3
27 static struct resource busn_resource = {
28 .name = "PCI busn",
29 .start = 0,
30 .end = 255,
31 .flags = IORESOURCE_BUS,
34 /* Ugh. Need to stop exporting this to modules. */
35 LIST_HEAD(pci_root_buses);
36 EXPORT_SYMBOL(pci_root_buses);
38 static LIST_HEAD(pci_domain_busn_res_list);
40 struct pci_domain_busn_res {
41 struct list_head list;
42 struct resource res;
43 int domain_nr;
46 static struct resource *get_pci_domain_busn_res(int domain_nr)
48 struct pci_domain_busn_res *r;
50 list_for_each_entry(r, &pci_domain_busn_res_list, list)
51 if (r->domain_nr == domain_nr)
52 return &r->res;
54 r = kzalloc(sizeof(*r), GFP_KERNEL);
55 if (!r)
56 return NULL;
58 r->domain_nr = domain_nr;
59 r->res.start = 0;
60 r->res.end = 0xff;
61 r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
63 list_add_tail(&r->list, &pci_domain_busn_res_list);
65 return &r->res;
68 static int find_anything(struct device *dev, void *data)
70 return 1;
74 * Some device drivers need know if PCI is initiated.
75 * Basically, we think PCI is not initiated when there
76 * is no device to be found on the pci_bus_type.
78 int no_pci_devices(void)
80 struct device *dev;
81 int no_devices;
83 dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
84 no_devices = (dev == NULL);
85 put_device(dev);
86 return no_devices;
88 EXPORT_SYMBOL(no_pci_devices);
91 * PCI Bus Class
93 static void release_pcibus_dev(struct device *dev)
95 struct pci_bus *pci_bus = to_pci_bus(dev);
97 put_device(pci_bus->bridge);
98 pci_bus_remove_resources(pci_bus);
99 pci_release_bus_of_node(pci_bus);
100 kfree(pci_bus);
103 static struct class pcibus_class = {
104 .name = "pci_bus",
105 .dev_release = &release_pcibus_dev,
106 .dev_groups = pcibus_groups,
109 static int __init pcibus_class_init(void)
111 return class_register(&pcibus_class);
113 postcore_initcall(pcibus_class_init);
115 static u64 pci_size(u64 base, u64 maxbase, u64 mask)
117 u64 size = mask & maxbase; /* Find the significant bits */
118 if (!size)
119 return 0;
122 * Get the lowest of them to find the decode size, and from that
123 * the extent.
125 size = (size & ~(size-1)) - 1;
128 * base == maxbase can be valid only if the BAR has already been
129 * programmed with all 1s.
131 if (base == maxbase && ((base | size) & mask) != mask)
132 return 0;
134 return size;
137 static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
139 u32 mem_type;
140 unsigned long flags;
142 if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
143 flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
144 flags |= IORESOURCE_IO;
145 return flags;
148 flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
149 flags |= IORESOURCE_MEM;
150 if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
151 flags |= IORESOURCE_PREFETCH;
153 mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
154 switch (mem_type) {
155 case PCI_BASE_ADDRESS_MEM_TYPE_32:
156 break;
157 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
158 /* 1M mem BAR treated as 32-bit BAR */
159 break;
160 case PCI_BASE_ADDRESS_MEM_TYPE_64:
161 flags |= IORESOURCE_MEM_64;
162 break;
163 default:
164 /* mem unknown type treated as 32-bit BAR */
165 break;
167 return flags;
170 #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
173 * pci_read_base - Read a PCI BAR
174 * @dev: the PCI device
175 * @type: type of the BAR
176 * @res: resource buffer to be filled in
177 * @pos: BAR position in the config space
179 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
181 int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
182 struct resource *res, unsigned int pos)
184 u32 l = 0, sz = 0, mask;
185 u64 l64, sz64, mask64;
186 u16 orig_cmd;
187 struct pci_bus_region region, inverted_region;
189 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
191 /* No printks while decoding is disabled! */
192 if (!dev->mmio_always_on) {
193 pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
194 if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
195 pci_write_config_word(dev, PCI_COMMAND,
196 orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
200 res->name = pci_name(dev);
202 pci_read_config_dword(dev, pos, &l);
203 pci_write_config_dword(dev, pos, l | mask);
204 pci_read_config_dword(dev, pos, &sz);
205 pci_write_config_dword(dev, pos, l);
208 * All bits set in sz means the device isn't working properly.
209 * If the BAR isn't implemented, all bits must be 0. If it's a
210 * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
211 * 1 must be clear.
213 if (sz == 0xffffffff)
214 sz = 0;
217 * I don't know how l can have all bits set. Copied from old code.
218 * Maybe it fixes a bug on some ancient platform.
220 if (l == 0xffffffff)
221 l = 0;
223 if (type == pci_bar_unknown) {
224 res->flags = decode_bar(dev, l);
225 res->flags |= IORESOURCE_SIZEALIGN;
226 if (res->flags & IORESOURCE_IO) {
227 l64 = l & PCI_BASE_ADDRESS_IO_MASK;
228 sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
229 mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
230 } else {
231 l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
232 sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
233 mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
235 } else {
236 if (l & PCI_ROM_ADDRESS_ENABLE)
237 res->flags |= IORESOURCE_ROM_ENABLE;
238 l64 = l & PCI_ROM_ADDRESS_MASK;
239 sz64 = sz & PCI_ROM_ADDRESS_MASK;
240 mask64 = PCI_ROM_ADDRESS_MASK;
243 if (res->flags & IORESOURCE_MEM_64) {
244 pci_read_config_dword(dev, pos + 4, &l);
245 pci_write_config_dword(dev, pos + 4, ~0);
246 pci_read_config_dword(dev, pos + 4, &sz);
247 pci_write_config_dword(dev, pos + 4, l);
249 l64 |= ((u64)l << 32);
250 sz64 |= ((u64)sz << 32);
251 mask64 |= ((u64)~0 << 32);
254 if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
255 pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
257 if (!sz64)
258 goto fail;
260 sz64 = pci_size(l64, sz64, mask64);
261 if (!sz64) {
262 pci_info(dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
263 pos);
264 goto fail;
267 if (res->flags & IORESOURCE_MEM_64) {
268 if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
269 && sz64 > 0x100000000ULL) {
270 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
271 res->start = 0;
272 res->end = 0;
273 pci_err(dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
274 pos, (unsigned long long)sz64);
275 goto out;
278 if ((sizeof(pci_bus_addr_t) < 8) && l) {
279 /* Above 32-bit boundary; try to reallocate */
280 res->flags |= IORESOURCE_UNSET;
281 res->start = 0;
282 res->end = sz64;
283 pci_info(dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
284 pos, (unsigned long long)l64);
285 goto out;
289 region.start = l64;
290 region.end = l64 + sz64;
292 pcibios_bus_to_resource(dev->bus, res, &region);
293 pcibios_resource_to_bus(dev->bus, &inverted_region, res);
296 * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
297 * the corresponding resource address (the physical address used by
298 * the CPU. Converting that resource address back to a bus address
299 * should yield the original BAR value:
301 * resource_to_bus(bus_to_resource(A)) == A
303 * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
304 * be claimed by the device.
306 if (inverted_region.start != region.start) {
307 res->flags |= IORESOURCE_UNSET;
308 res->start = 0;
309 res->end = region.end - region.start;
310 pci_info(dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
311 pos, (unsigned long long)region.start);
314 goto out;
317 fail:
318 res->flags = 0;
319 out:
320 if (res->flags)
321 pci_printk(KERN_DEBUG, dev, "reg 0x%x: %pR\n", pos, res);
323 return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
326 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
328 unsigned int pos, reg;
330 if (dev->non_compliant_bars)
331 return;
333 /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */
334 if (dev->is_virtfn)
335 return;
337 for (pos = 0; pos < howmany; pos++) {
338 struct resource *res = &dev->resource[pos];
339 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
340 pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
343 if (rom) {
344 struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
345 dev->rom_base_reg = rom;
346 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
347 IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
348 __pci_read_base(dev, pci_bar_mem32, res, rom);
352 static void pci_read_bridge_io(struct pci_bus *child)
354 struct pci_dev *dev = child->self;
355 u8 io_base_lo, io_limit_lo;
356 unsigned long io_mask, io_granularity, base, limit;
357 struct pci_bus_region region;
358 struct resource *res;
360 io_mask = PCI_IO_RANGE_MASK;
361 io_granularity = 0x1000;
362 if (dev->io_window_1k) {
363 /* Support 1K I/O space granularity */
364 io_mask = PCI_IO_1K_RANGE_MASK;
365 io_granularity = 0x400;
368 res = child->resource[0];
369 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
370 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
371 base = (io_base_lo & io_mask) << 8;
372 limit = (io_limit_lo & io_mask) << 8;
374 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
375 u16 io_base_hi, io_limit_hi;
377 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
378 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
379 base |= ((unsigned long) io_base_hi << 16);
380 limit |= ((unsigned long) io_limit_hi << 16);
383 if (base <= limit) {
384 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
385 region.start = base;
386 region.end = limit + io_granularity - 1;
387 pcibios_bus_to_resource(dev->bus, res, &region);
388 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
392 static void pci_read_bridge_mmio(struct pci_bus *child)
394 struct pci_dev *dev = child->self;
395 u16 mem_base_lo, mem_limit_lo;
396 unsigned long base, limit;
397 struct pci_bus_region region;
398 struct resource *res;
400 res = child->resource[1];
401 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
402 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
403 base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
404 limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
405 if (base <= limit) {
406 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
407 region.start = base;
408 region.end = limit + 0xfffff;
409 pcibios_bus_to_resource(dev->bus, res, &region);
410 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
414 static void pci_read_bridge_mmio_pref(struct pci_bus *child)
416 struct pci_dev *dev = child->self;
417 u16 mem_base_lo, mem_limit_lo;
418 u64 base64, limit64;
419 pci_bus_addr_t base, limit;
420 struct pci_bus_region region;
421 struct resource *res;
423 res = child->resource[2];
424 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
425 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
426 base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
427 limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
429 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
430 u32 mem_base_hi, mem_limit_hi;
432 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
433 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
436 * Some bridges set the base > limit by default, and some
437 * (broken) BIOSes do not initialize them. If we find
438 * this, just assume they are not being used.
440 if (mem_base_hi <= mem_limit_hi) {
441 base64 |= (u64) mem_base_hi << 32;
442 limit64 |= (u64) mem_limit_hi << 32;
446 base = (pci_bus_addr_t) base64;
447 limit = (pci_bus_addr_t) limit64;
449 if (base != base64) {
450 pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
451 (unsigned long long) base64);
452 return;
455 if (base <= limit) {
456 res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
457 IORESOURCE_MEM | IORESOURCE_PREFETCH;
458 if (res->flags & PCI_PREF_RANGE_TYPE_64)
459 res->flags |= IORESOURCE_MEM_64;
460 region.start = base;
461 region.end = limit + 0xfffff;
462 pcibios_bus_to_resource(dev->bus, res, &region);
463 pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
467 void pci_read_bridge_bases(struct pci_bus *child)
469 struct pci_dev *dev = child->self;
470 struct resource *res;
471 int i;
473 if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
474 return;
476 pci_info(dev, "PCI bridge to %pR%s\n",
477 &child->busn_res,
478 dev->transparent ? " (subtractive decode)" : "");
480 pci_bus_remove_resources(child);
481 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
482 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
484 pci_read_bridge_io(child);
485 pci_read_bridge_mmio(child);
486 pci_read_bridge_mmio_pref(child);
488 if (dev->transparent) {
489 pci_bus_for_each_resource(child->parent, res, i) {
490 if (res && res->flags) {
491 pci_bus_add_resource(child, res,
492 PCI_SUBTRACTIVE_DECODE);
493 pci_printk(KERN_DEBUG, dev,
494 " bridge window %pR (subtractive decode)\n",
495 res);
501 static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
503 struct pci_bus *b;
505 b = kzalloc(sizeof(*b), GFP_KERNEL);
506 if (!b)
507 return NULL;
509 INIT_LIST_HEAD(&b->node);
510 INIT_LIST_HEAD(&b->children);
511 INIT_LIST_HEAD(&b->devices);
512 INIT_LIST_HEAD(&b->slots);
513 INIT_LIST_HEAD(&b->resources);
514 b->max_bus_speed = PCI_SPEED_UNKNOWN;
515 b->cur_bus_speed = PCI_SPEED_UNKNOWN;
516 #ifdef CONFIG_PCI_DOMAINS_GENERIC
517 if (parent)
518 b->domain_nr = parent->domain_nr;
519 #endif
520 return b;
523 static void devm_pci_release_host_bridge_dev(struct device *dev)
525 struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
527 if (bridge->release_fn)
528 bridge->release_fn(bridge);
530 pci_free_resource_list(&bridge->windows);
533 static void pci_release_host_bridge_dev(struct device *dev)
535 devm_pci_release_host_bridge_dev(dev);
536 kfree(to_pci_host_bridge(dev));
539 struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
541 struct pci_host_bridge *bridge;
543 bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
544 if (!bridge)
545 return NULL;
547 INIT_LIST_HEAD(&bridge->windows);
548 bridge->dev.release = pci_release_host_bridge_dev;
551 * We assume we can manage these PCIe features. Some systems may
552 * reserve these for use by the platform itself, e.g., an ACPI BIOS
553 * may implement its own AER handling and use _OSC to prevent the
554 * OS from interfering.
556 bridge->native_aer = 1;
557 bridge->native_pcie_hotplug = 1;
558 bridge->native_shpc_hotplug = 1;
559 bridge->native_pme = 1;
560 bridge->native_ltr = 1;
562 return bridge;
564 EXPORT_SYMBOL(pci_alloc_host_bridge);
566 struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
567 size_t priv)
569 struct pci_host_bridge *bridge;
571 bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
572 if (!bridge)
573 return NULL;
575 INIT_LIST_HEAD(&bridge->windows);
576 bridge->dev.release = devm_pci_release_host_bridge_dev;
578 return bridge;
580 EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
582 void pci_free_host_bridge(struct pci_host_bridge *bridge)
584 pci_free_resource_list(&bridge->windows);
586 kfree(bridge);
588 EXPORT_SYMBOL(pci_free_host_bridge);
590 static const unsigned char pcix_bus_speed[] = {
591 PCI_SPEED_UNKNOWN, /* 0 */
592 PCI_SPEED_66MHz_PCIX, /* 1 */
593 PCI_SPEED_100MHz_PCIX, /* 2 */
594 PCI_SPEED_133MHz_PCIX, /* 3 */
595 PCI_SPEED_UNKNOWN, /* 4 */
596 PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
597 PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
598 PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
599 PCI_SPEED_UNKNOWN, /* 8 */
600 PCI_SPEED_66MHz_PCIX_266, /* 9 */
601 PCI_SPEED_100MHz_PCIX_266, /* A */
602 PCI_SPEED_133MHz_PCIX_266, /* B */
603 PCI_SPEED_UNKNOWN, /* C */
604 PCI_SPEED_66MHz_PCIX_533, /* D */
605 PCI_SPEED_100MHz_PCIX_533, /* E */
606 PCI_SPEED_133MHz_PCIX_533 /* F */
609 const unsigned char pcie_link_speed[] = {
610 PCI_SPEED_UNKNOWN, /* 0 */
611 PCIE_SPEED_2_5GT, /* 1 */
612 PCIE_SPEED_5_0GT, /* 2 */
613 PCIE_SPEED_8_0GT, /* 3 */
614 PCIE_SPEED_16_0GT, /* 4 */
615 PCI_SPEED_UNKNOWN, /* 5 */
616 PCI_SPEED_UNKNOWN, /* 6 */
617 PCI_SPEED_UNKNOWN, /* 7 */
618 PCI_SPEED_UNKNOWN, /* 8 */
619 PCI_SPEED_UNKNOWN, /* 9 */
620 PCI_SPEED_UNKNOWN, /* A */
621 PCI_SPEED_UNKNOWN, /* B */
622 PCI_SPEED_UNKNOWN, /* C */
623 PCI_SPEED_UNKNOWN, /* D */
624 PCI_SPEED_UNKNOWN, /* E */
625 PCI_SPEED_UNKNOWN /* F */
628 void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
630 bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
632 EXPORT_SYMBOL_GPL(pcie_update_link_speed);
634 static unsigned char agp_speeds[] = {
635 AGP_UNKNOWN,
636 AGP_1X,
637 AGP_2X,
638 AGP_4X,
639 AGP_8X
642 static enum pci_bus_speed agp_speed(int agp3, int agpstat)
644 int index = 0;
646 if (agpstat & 4)
647 index = 3;
648 else if (agpstat & 2)
649 index = 2;
650 else if (agpstat & 1)
651 index = 1;
652 else
653 goto out;
655 if (agp3) {
656 index += 2;
657 if (index == 5)
658 index = 0;
661 out:
662 return agp_speeds[index];
665 static void pci_set_bus_speed(struct pci_bus *bus)
667 struct pci_dev *bridge = bus->self;
668 int pos;
670 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
671 if (!pos)
672 pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
673 if (pos) {
674 u32 agpstat, agpcmd;
676 pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
677 bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
679 pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
680 bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
683 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
684 if (pos) {
685 u16 status;
686 enum pci_bus_speed max;
688 pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
689 &status);
691 if (status & PCI_X_SSTATUS_533MHZ) {
692 max = PCI_SPEED_133MHz_PCIX_533;
693 } else if (status & PCI_X_SSTATUS_266MHZ) {
694 max = PCI_SPEED_133MHz_PCIX_266;
695 } else if (status & PCI_X_SSTATUS_133MHZ) {
696 if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
697 max = PCI_SPEED_133MHz_PCIX_ECC;
698 else
699 max = PCI_SPEED_133MHz_PCIX;
700 } else {
701 max = PCI_SPEED_66MHz_PCIX;
704 bus->max_bus_speed = max;
705 bus->cur_bus_speed = pcix_bus_speed[
706 (status & PCI_X_SSTATUS_FREQ) >> 6];
708 return;
711 if (pci_is_pcie(bridge)) {
712 u32 linkcap;
713 u16 linksta;
715 pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
716 bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
718 pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
719 pcie_update_link_speed(bus, linksta);
723 static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
725 struct irq_domain *d;
728 * Any firmware interface that can resolve the msi_domain
729 * should be called from here.
731 d = pci_host_bridge_of_msi_domain(bus);
732 if (!d)
733 d = pci_host_bridge_acpi_msi_domain(bus);
735 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
737 * If no IRQ domain was found via the OF tree, try looking it up
738 * directly through the fwnode_handle.
740 if (!d) {
741 struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
743 if (fwnode)
744 d = irq_find_matching_fwnode(fwnode,
745 DOMAIN_BUS_PCI_MSI);
747 #endif
749 return d;
752 static void pci_set_bus_msi_domain(struct pci_bus *bus)
754 struct irq_domain *d;
755 struct pci_bus *b;
758 * The bus can be a root bus, a subordinate bus, or a virtual bus
759 * created by an SR-IOV device. Walk up to the first bridge device
760 * found or derive the domain from the host bridge.
762 for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
763 if (b->self)
764 d = dev_get_msi_domain(&b->self->dev);
767 if (!d)
768 d = pci_host_bridge_msi_domain(b);
770 dev_set_msi_domain(&bus->dev, d);
773 static int pci_register_host_bridge(struct pci_host_bridge *bridge)
775 struct device *parent = bridge->dev.parent;
776 struct resource_entry *window, *n;
777 struct pci_bus *bus, *b;
778 resource_size_t offset;
779 LIST_HEAD(resources);
780 struct resource *res;
781 char addr[64], *fmt;
782 const char *name;
783 int err;
785 bus = pci_alloc_bus(NULL);
786 if (!bus)
787 return -ENOMEM;
789 bridge->bus = bus;
791 /* Temporarily move resources off the list */
792 list_splice_init(&bridge->windows, &resources);
793 bus->sysdata = bridge->sysdata;
794 bus->msi = bridge->msi;
795 bus->ops = bridge->ops;
796 bus->number = bus->busn_res.start = bridge->busnr;
797 #ifdef CONFIG_PCI_DOMAINS_GENERIC
798 bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
799 #endif
801 b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
802 if (b) {
803 /* Ignore it if we already got here via a different bridge */
804 dev_dbg(&b->dev, "bus already known\n");
805 err = -EEXIST;
806 goto free;
809 dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
810 bridge->busnr);
812 err = pcibios_root_bridge_prepare(bridge);
813 if (err)
814 goto free;
816 err = device_register(&bridge->dev);
817 if (err)
818 put_device(&bridge->dev);
820 bus->bridge = get_device(&bridge->dev);
821 device_enable_async_suspend(bus->bridge);
822 pci_set_bus_of_node(bus);
823 pci_set_bus_msi_domain(bus);
825 if (!parent)
826 set_dev_node(bus->bridge, pcibus_to_node(bus));
828 bus->dev.class = &pcibus_class;
829 bus->dev.parent = bus->bridge;
831 dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
832 name = dev_name(&bus->dev);
834 err = device_register(&bus->dev);
835 if (err)
836 goto unregister;
838 pcibios_add_bus(bus);
840 /* Create legacy_io and legacy_mem files for this bus */
841 pci_create_legacy_files(bus);
843 if (parent)
844 dev_info(parent, "PCI host bridge to bus %s\n", name);
845 else
846 pr_info("PCI host bridge to bus %s\n", name);
848 /* Add initial resources to the bus */
849 resource_list_for_each_entry_safe(window, n, &resources) {
850 list_move_tail(&window->node, &bridge->windows);
851 offset = window->offset;
852 res = window->res;
854 if (res->flags & IORESOURCE_BUS)
855 pci_bus_insert_busn_res(bus, bus->number, res->end);
856 else
857 pci_bus_add_resource(bus, res, 0);
859 if (offset) {
860 if (resource_type(res) == IORESOURCE_IO)
861 fmt = " (bus address [%#06llx-%#06llx])";
862 else
863 fmt = " (bus address [%#010llx-%#010llx])";
865 snprintf(addr, sizeof(addr), fmt,
866 (unsigned long long)(res->start - offset),
867 (unsigned long long)(res->end - offset));
868 } else
869 addr[0] = '\0';
871 dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
874 down_write(&pci_bus_sem);
875 list_add_tail(&bus->node, &pci_root_buses);
876 up_write(&pci_bus_sem);
878 return 0;
880 unregister:
881 put_device(&bridge->dev);
882 device_unregister(&bridge->dev);
884 free:
885 kfree(bus);
886 return err;
889 static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev *bridge)
891 int pos;
892 u32 status;
895 * If extended config space isn't accessible on a bridge's primary
896 * bus, we certainly can't access it on the secondary bus.
898 if (bridge->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
899 return false;
902 * PCIe Root Ports and switch ports are PCIe on both sides, so if
903 * extended config space is accessible on the primary, it's also
904 * accessible on the secondary.
906 if (pci_is_pcie(bridge) &&
907 (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT ||
908 pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM ||
909 pci_pcie_type(bridge) == PCI_EXP_TYPE_DOWNSTREAM))
910 return true;
913 * For the other bridge types:
914 * - PCI-to-PCI bridges
915 * - PCIe-to-PCI/PCI-X forward bridges
916 * - PCI/PCI-X-to-PCIe reverse bridges
917 * extended config space on the secondary side is only accessible
918 * if the bridge supports PCI-X Mode 2.
920 pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
921 if (!pos)
922 return false;
924 pci_read_config_dword(bridge, pos + PCI_X_STATUS, &status);
925 return status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ);
928 static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
929 struct pci_dev *bridge, int busnr)
931 struct pci_bus *child;
932 int i;
933 int ret;
935 /* Allocate a new bus and inherit stuff from the parent */
936 child = pci_alloc_bus(parent);
937 if (!child)
938 return NULL;
940 child->parent = parent;
941 child->ops = parent->ops;
942 child->msi = parent->msi;
943 child->sysdata = parent->sysdata;
944 child->bus_flags = parent->bus_flags;
947 * Initialize some portions of the bus device, but don't register
948 * it now as the parent is not properly set up yet.
950 child->dev.class = &pcibus_class;
951 dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
953 /* Set up the primary, secondary and subordinate bus numbers */
954 child->number = child->busn_res.start = busnr;
955 child->primary = parent->busn_res.start;
956 child->busn_res.end = 0xff;
958 if (!bridge) {
959 child->dev.parent = parent->bridge;
960 goto add_dev;
963 child->self = bridge;
964 child->bridge = get_device(&bridge->dev);
965 child->dev.parent = child->bridge;
966 pci_set_bus_of_node(child);
967 pci_set_bus_speed(child);
970 * Check whether extended config space is accessible on the child
971 * bus. Note that we currently assume it is always accessible on
972 * the root bus.
974 if (!pci_bridge_child_ext_cfg_accessible(bridge)) {
975 child->bus_flags |= PCI_BUS_FLAGS_NO_EXTCFG;
976 pci_info(child, "extended config space not accessible\n");
979 /* Set up default resource pointers and names */
980 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
981 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
982 child->resource[i]->name = child->name;
984 bridge->subordinate = child;
986 add_dev:
987 pci_set_bus_msi_domain(child);
988 ret = device_register(&child->dev);
989 WARN_ON(ret < 0);
991 pcibios_add_bus(child);
993 if (child->ops->add_bus) {
994 ret = child->ops->add_bus(child);
995 if (WARN_ON(ret < 0))
996 dev_err(&child->dev, "failed to add bus: %d\n", ret);
999 /* Create legacy_io and legacy_mem files for this bus */
1000 pci_create_legacy_files(child);
1002 return child;
1005 struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
1006 int busnr)
1008 struct pci_bus *child;
1010 child = pci_alloc_child_bus(parent, dev, busnr);
1011 if (child) {
1012 down_write(&pci_bus_sem);
1013 list_add_tail(&child->node, &parent->children);
1014 up_write(&pci_bus_sem);
1016 return child;
1018 EXPORT_SYMBOL(pci_add_new_bus);
1020 static void pci_enable_crs(struct pci_dev *pdev)
1022 u16 root_cap = 0;
1024 /* Enable CRS Software Visibility if supported */
1025 pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
1026 if (root_cap & PCI_EXP_RTCAP_CRSVIS)
1027 pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
1028 PCI_EXP_RTCTL_CRSSVE);
1031 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
1032 unsigned int available_buses);
1035 * pci_scan_bridge_extend() - Scan buses behind a bridge
1036 * @bus: Parent bus the bridge is on
1037 * @dev: Bridge itself
1038 * @max: Starting subordinate number of buses behind this bridge
1039 * @available_buses: Total number of buses available for this bridge and
1040 * the devices below. After the minimal bus space has
1041 * been allocated the remaining buses will be
1042 * distributed equally between hotplug-capable bridges.
1043 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1044 * that need to be reconfigured.
1046 * If it's a bridge, configure it and scan the bus behind it.
1047 * For CardBus bridges, we don't scan behind as the devices will
1048 * be handled by the bridge driver itself.
1050 * We need to process bridges in two passes -- first we scan those
1051 * already configured by the BIOS and after we are done with all of
1052 * them, we proceed to assigning numbers to the remaining buses in
1053 * order to avoid overlaps between old and new bus numbers.
1055 * Return: New subordinate number covering all buses behind this bridge.
1057 static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
1058 int max, unsigned int available_buses,
1059 int pass)
1061 struct pci_bus *child;
1062 int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
1063 u32 buses, i, j = 0;
1064 u16 bctl;
1065 u8 primary, secondary, subordinate;
1066 int broken = 0;
1069 * Make sure the bridge is powered on to be able to access config
1070 * space of devices below it.
1072 pm_runtime_get_sync(&dev->dev);
1074 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
1075 primary = buses & 0xFF;
1076 secondary = (buses >> 8) & 0xFF;
1077 subordinate = (buses >> 16) & 0xFF;
1079 pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
1080 secondary, subordinate, pass);
1082 if (!primary && (primary != bus->number) && secondary && subordinate) {
1083 pci_warn(dev, "Primary bus is hard wired to 0\n");
1084 primary = bus->number;
1087 /* Check if setup is sensible at all */
1088 if (!pass &&
1089 (primary != bus->number || secondary <= bus->number ||
1090 secondary > subordinate)) {
1091 pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
1092 secondary, subordinate);
1093 broken = 1;
1097 * Disable Master-Abort Mode during probing to avoid reporting of
1098 * bus errors in some architectures.
1100 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
1101 pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
1102 bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
1104 pci_enable_crs(dev);
1106 if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
1107 !is_cardbus && !broken) {
1108 unsigned int cmax;
1111 * Bus already configured by firmware, process it in the
1112 * first pass and just note the configuration.
1114 if (pass)
1115 goto out;
1118 * The bus might already exist for two reasons: Either we
1119 * are rescanning the bus or the bus is reachable through
1120 * more than one bridge. The second case can happen with
1121 * the i450NX chipset.
1123 child = pci_find_bus(pci_domain_nr(bus), secondary);
1124 if (!child) {
1125 child = pci_add_new_bus(bus, dev, secondary);
1126 if (!child)
1127 goto out;
1128 child->primary = primary;
1129 pci_bus_insert_busn_res(child, secondary, subordinate);
1130 child->bridge_ctl = bctl;
1133 cmax = pci_scan_child_bus(child);
1134 if (cmax > subordinate)
1135 pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
1136 subordinate, cmax);
1138 /* Subordinate should equal child->busn_res.end */
1139 if (subordinate > max)
1140 max = subordinate;
1141 } else {
1144 * We need to assign a number to this bus which we always
1145 * do in the second pass.
1147 if (!pass) {
1148 if (pcibios_assign_all_busses() || broken || is_cardbus)
1151 * Temporarily disable forwarding of the
1152 * configuration cycles on all bridges in
1153 * this bus segment to avoid possible
1154 * conflicts in the second pass between two
1155 * bridges programmed with overlapping bus
1156 * ranges.
1158 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
1159 buses & ~0xffffff);
1160 goto out;
1163 /* Clear errors */
1164 pci_write_config_word(dev, PCI_STATUS, 0xffff);
1167 * Prevent assigning a bus number that already exists.
1168 * This can happen when a bridge is hot-plugged, so in this
1169 * case we only re-scan this bus.
1171 child = pci_find_bus(pci_domain_nr(bus), max+1);
1172 if (!child) {
1173 child = pci_add_new_bus(bus, dev, max+1);
1174 if (!child)
1175 goto out;
1176 pci_bus_insert_busn_res(child, max+1,
1177 bus->busn_res.end);
1179 max++;
1180 if (available_buses)
1181 available_buses--;
1183 buses = (buses & 0xff000000)
1184 | ((unsigned int)(child->primary) << 0)
1185 | ((unsigned int)(child->busn_res.start) << 8)
1186 | ((unsigned int)(child->busn_res.end) << 16);
1189 * yenta.c forces a secondary latency timer of 176.
1190 * Copy that behaviour here.
1192 if (is_cardbus) {
1193 buses &= ~0xff000000;
1194 buses |= CARDBUS_LATENCY_TIMER << 24;
1197 /* We need to blast all three values with a single write */
1198 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
1200 if (!is_cardbus) {
1201 child->bridge_ctl = bctl;
1202 max = pci_scan_child_bus_extend(child, available_buses);
1203 } else {
1206 * For CardBus bridges, we leave 4 bus numbers as
1207 * cards with a PCI-to-PCI bridge can be inserted
1208 * later.
1210 for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
1211 struct pci_bus *parent = bus;
1212 if (pci_find_bus(pci_domain_nr(bus),
1213 max+i+1))
1214 break;
1215 while (parent->parent) {
1216 if ((!pcibios_assign_all_busses()) &&
1217 (parent->busn_res.end > max) &&
1218 (parent->busn_res.end <= max+i)) {
1219 j = 1;
1221 parent = parent->parent;
1223 if (j) {
1226 * Often, there are two CardBus
1227 * bridges -- try to leave one
1228 * valid bus number for each one.
1230 i /= 2;
1231 break;
1234 max += i;
1237 /* Set subordinate bus number to its real value */
1238 pci_bus_update_busn_res_end(child, max);
1239 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
1242 sprintf(child->name,
1243 (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
1244 pci_domain_nr(bus), child->number);
1246 /* Check that all devices are accessible */
1247 while (bus->parent) {
1248 if ((child->busn_res.end > bus->busn_res.end) ||
1249 (child->number > bus->busn_res.end) ||
1250 (child->number < bus->number) ||
1251 (child->busn_res.end < bus->number)) {
1252 dev_info(&dev->dev, "devices behind bridge are unusable because %pR cannot be assigned for them\n",
1253 &child->busn_res);
1254 break;
1256 bus = bus->parent;
1259 out:
1260 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
1262 pm_runtime_put(&dev->dev);
1264 return max;
1268 * pci_scan_bridge() - Scan buses behind a bridge
1269 * @bus: Parent bus the bridge is on
1270 * @dev: Bridge itself
1271 * @max: Starting subordinate number of buses behind this bridge
1272 * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
1273 * that need to be reconfigured.
1275 * If it's a bridge, configure it and scan the bus behind it.
1276 * For CardBus bridges, we don't scan behind as the devices will
1277 * be handled by the bridge driver itself.
1279 * We need to process bridges in two passes -- first we scan those
1280 * already configured by the BIOS and after we are done with all of
1281 * them, we proceed to assigning numbers to the remaining buses in
1282 * order to avoid overlaps between old and new bus numbers.
1284 * Return: New subordinate number covering all buses behind this bridge.
1286 int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
1288 return pci_scan_bridge_extend(bus, dev, max, 0, pass);
1290 EXPORT_SYMBOL(pci_scan_bridge);
1293 * Read interrupt line and base address registers.
1294 * The architecture-dependent code can tweak these, of course.
1296 static void pci_read_irq(struct pci_dev *dev)
1298 unsigned char irq;
1300 /* VFs are not allowed to use INTx, so skip the config reads */
1301 if (dev->is_virtfn) {
1302 dev->pin = 0;
1303 dev->irq = 0;
1304 return;
1307 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
1308 dev->pin = irq;
1309 if (irq)
1310 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
1311 dev->irq = irq;
1314 void set_pcie_port_type(struct pci_dev *pdev)
1316 int pos;
1317 u16 reg16;
1318 int type;
1319 struct pci_dev *parent;
1321 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1322 if (!pos)
1323 return;
1325 pdev->pcie_cap = pos;
1326 pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
1327 pdev->pcie_flags_reg = reg16;
1328 pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
1329 pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
1332 * A Root Port or a PCI-to-PCIe bridge is always the upstream end
1333 * of a Link. No PCIe component has two Links. Two Links are
1334 * connected by a Switch that has a Port on each Link and internal
1335 * logic to connect the two Ports.
1337 type = pci_pcie_type(pdev);
1338 if (type == PCI_EXP_TYPE_ROOT_PORT ||
1339 type == PCI_EXP_TYPE_PCIE_BRIDGE)
1340 pdev->has_secondary_link = 1;
1341 else if (type == PCI_EXP_TYPE_UPSTREAM ||
1342 type == PCI_EXP_TYPE_DOWNSTREAM) {
1343 parent = pci_upstream_bridge(pdev);
1346 * Usually there's an upstream device (Root Port or Switch
1347 * Downstream Port), but we can't assume one exists.
1349 if (parent && !parent->has_secondary_link)
1350 pdev->has_secondary_link = 1;
1354 void set_pcie_hotplug_bridge(struct pci_dev *pdev)
1356 u32 reg32;
1358 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
1359 if (reg32 & PCI_EXP_SLTCAP_HPC)
1360 pdev->is_hotplug_bridge = 1;
1363 static void set_pcie_thunderbolt(struct pci_dev *dev)
1365 int vsec = 0;
1366 u32 header;
1368 while ((vsec = pci_find_next_ext_capability(dev, vsec,
1369 PCI_EXT_CAP_ID_VNDR))) {
1370 pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
1372 /* Is the device part of a Thunderbolt controller? */
1373 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
1374 PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
1375 dev->is_thunderbolt = 1;
1376 return;
1382 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
1383 * @dev: PCI device
1385 * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
1386 * when forwarding a type1 configuration request the bridge must check that
1387 * the extended register address field is zero. The bridge is not permitted
1388 * to forward the transactions and must handle it as an Unsupported Request.
1389 * Some bridges do not follow this rule and simply drop the extended register
1390 * bits, resulting in the standard config space being aliased, every 256
1391 * bytes across the entire configuration space. Test for this condition by
1392 * comparing the first dword of each potential alias to the vendor/device ID.
1393 * Known offenders:
1394 * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
1395 * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
1397 static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
1399 #ifdef CONFIG_PCI_QUIRKS
1400 int pos;
1401 u32 header, tmp;
1403 pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
1405 for (pos = PCI_CFG_SPACE_SIZE;
1406 pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
1407 if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
1408 || header != tmp)
1409 return false;
1412 return true;
1413 #else
1414 return false;
1415 #endif
1419 * pci_cfg_space_size - Get the configuration space size of the PCI device
1420 * @dev: PCI device
1422 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1423 * have 4096 bytes. Even if the device is capable, that doesn't mean we can
1424 * access it. Maybe we don't have a way to generate extended config space
1425 * accesses, or the device is behind a reverse Express bridge. So we try
1426 * reading the dword at 0x100 which must either be 0 or a valid extended
1427 * capability header.
1429 static int pci_cfg_space_size_ext(struct pci_dev *dev)
1431 u32 status;
1432 int pos = PCI_CFG_SPACE_SIZE;
1434 if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1435 return PCI_CFG_SPACE_SIZE;
1436 if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
1437 return PCI_CFG_SPACE_SIZE;
1439 return PCI_CFG_SPACE_EXP_SIZE;
1442 int pci_cfg_space_size(struct pci_dev *dev)
1444 int pos;
1445 u32 status;
1446 u16 class;
1448 if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
1449 return PCI_CFG_SPACE_SIZE;
1451 class = dev->class >> 8;
1452 if (class == PCI_CLASS_BRIDGE_HOST)
1453 return pci_cfg_space_size_ext(dev);
1455 if (pci_is_pcie(dev))
1456 return pci_cfg_space_size_ext(dev);
1458 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1459 if (!pos)
1460 return PCI_CFG_SPACE_SIZE;
1462 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1463 if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
1464 return pci_cfg_space_size_ext(dev);
1466 return PCI_CFG_SPACE_SIZE;
1469 static u32 pci_class(struct pci_dev *dev)
1471 u32 class;
1473 #ifdef CONFIG_PCI_IOV
1474 if (dev->is_virtfn)
1475 return dev->physfn->sriov->class;
1476 #endif
1477 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
1478 return class;
1481 static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device)
1483 #ifdef CONFIG_PCI_IOV
1484 if (dev->is_virtfn) {
1485 *vendor = dev->physfn->sriov->subsystem_vendor;
1486 *device = dev->physfn->sriov->subsystem_device;
1487 return;
1489 #endif
1490 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor);
1491 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device);
1494 static u8 pci_hdr_type(struct pci_dev *dev)
1496 u8 hdr_type;
1498 #ifdef CONFIG_PCI_IOV
1499 if (dev->is_virtfn)
1500 return dev->physfn->sriov->hdr_type;
1501 #endif
1502 pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
1503 return hdr_type;
1506 #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
1508 static void pci_msi_setup_pci_dev(struct pci_dev *dev)
1511 * Disable the MSI hardware to avoid screaming interrupts
1512 * during boot. This is the power on reset default so
1513 * usually this should be a noop.
1515 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1516 if (dev->msi_cap)
1517 pci_msi_set_enable(dev, 0);
1519 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1520 if (dev->msix_cap)
1521 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1525 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
1526 * @dev: PCI device
1528 * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
1529 * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
1531 static int pci_intx_mask_broken(struct pci_dev *dev)
1533 u16 orig, toggle, new;
1535 pci_read_config_word(dev, PCI_COMMAND, &orig);
1536 toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
1537 pci_write_config_word(dev, PCI_COMMAND, toggle);
1538 pci_read_config_word(dev, PCI_COMMAND, &new);
1540 pci_write_config_word(dev, PCI_COMMAND, orig);
1543 * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
1544 * r2.3, so strictly speaking, a device is not *broken* if it's not
1545 * writable. But we'll live with the misnomer for now.
1547 if (new != toggle)
1548 return 1;
1549 return 0;
1553 * pci_setup_device - Fill in class and map information of a device
1554 * @dev: the device structure to fill
1556 * Initialize the device structure with information about the device's
1557 * vendor,class,memory and IO-space addresses, IRQ lines etc.
1558 * Called at initialisation of the PCI subsystem and by CardBus services.
1559 * Returns 0 on success and negative if unknown type of device (not normal,
1560 * bridge or CardBus).
1562 int pci_setup_device(struct pci_dev *dev)
1564 u32 class;
1565 u16 cmd;
1566 u8 hdr_type;
1567 int pos = 0;
1568 struct pci_bus_region region;
1569 struct resource *res;
1571 hdr_type = pci_hdr_type(dev);
1573 dev->sysdata = dev->bus->sysdata;
1574 dev->dev.parent = dev->bus->bridge;
1575 dev->dev.bus = &pci_bus_type;
1576 dev->hdr_type = hdr_type & 0x7f;
1577 dev->multifunction = !!(hdr_type & 0x80);
1578 dev->error_state = pci_channel_io_normal;
1579 set_pcie_port_type(dev);
1581 pci_dev_assign_slot(dev);
1584 * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
1585 * set this higher, assuming the system even supports it.
1587 dev->dma_mask = 0xffffffff;
1589 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
1590 dev->bus->number, PCI_SLOT(dev->devfn),
1591 PCI_FUNC(dev->devfn));
1593 class = pci_class(dev);
1595 dev->revision = class & 0xff;
1596 dev->class = class >> 8; /* upper 3 bytes */
1598 pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n",
1599 dev->vendor, dev->device, dev->hdr_type, dev->class);
1601 /* Need to have dev->class ready */
1602 dev->cfg_size = pci_cfg_space_size(dev);
1604 /* Need to have dev->cfg_size ready */
1605 set_pcie_thunderbolt(dev);
1607 /* "Unknown power state" */
1608 dev->current_state = PCI_UNKNOWN;
1610 /* Early fixups, before probing the BARs */
1611 pci_fixup_device(pci_fixup_early, dev);
1613 /* Device class may be changed after fixup */
1614 class = dev->class >> 8;
1616 if (dev->non_compliant_bars) {
1617 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1618 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
1619 pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
1620 cmd &= ~PCI_COMMAND_IO;
1621 cmd &= ~PCI_COMMAND_MEMORY;
1622 pci_write_config_word(dev, PCI_COMMAND, cmd);
1626 dev->broken_intx_masking = pci_intx_mask_broken(dev);
1628 switch (dev->hdr_type) { /* header type */
1629 case PCI_HEADER_TYPE_NORMAL: /* standard header */
1630 if (class == PCI_CLASS_BRIDGE_PCI)
1631 goto bad;
1632 pci_read_irq(dev);
1633 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
1635 pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device);
1638 * Do the ugly legacy mode stuff here rather than broken chip
1639 * quirk code. Legacy mode ATA controllers have fixed
1640 * addresses. These are not always echoed in BAR0-3, and
1641 * BAR0-3 in a few cases contain junk!
1643 if (class == PCI_CLASS_STORAGE_IDE) {
1644 u8 progif;
1645 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1646 if ((progif & 1) == 0) {
1647 region.start = 0x1F0;
1648 region.end = 0x1F7;
1649 res = &dev->resource[0];
1650 res->flags = LEGACY_IO_RESOURCE;
1651 pcibios_bus_to_resource(dev->bus, res, &region);
1652 pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n",
1653 res);
1654 region.start = 0x3F6;
1655 region.end = 0x3F6;
1656 res = &dev->resource[1];
1657 res->flags = LEGACY_IO_RESOURCE;
1658 pcibios_bus_to_resource(dev->bus, res, &region);
1659 pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n",
1660 res);
1662 if ((progif & 4) == 0) {
1663 region.start = 0x170;
1664 region.end = 0x177;
1665 res = &dev->resource[2];
1666 res->flags = LEGACY_IO_RESOURCE;
1667 pcibios_bus_to_resource(dev->bus, res, &region);
1668 pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n",
1669 res);
1670 region.start = 0x376;
1671 region.end = 0x376;
1672 res = &dev->resource[3];
1673 res->flags = LEGACY_IO_RESOURCE;
1674 pcibios_bus_to_resource(dev->bus, res, &region);
1675 pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n",
1676 res);
1679 break;
1681 case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
1682 if (class != PCI_CLASS_BRIDGE_PCI)
1683 goto bad;
1686 * The PCI-to-PCI bridge spec requires that subtractive
1687 * decoding (i.e. transparent) bridge must have programming
1688 * interface code of 0x01.
1690 pci_read_irq(dev);
1691 dev->transparent = ((dev->class & 0xff) == 1);
1692 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
1693 set_pcie_hotplug_bridge(dev);
1694 pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
1695 if (pos) {
1696 pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
1697 pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
1699 break;
1701 case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
1702 if (class != PCI_CLASS_BRIDGE_CARDBUS)
1703 goto bad;
1704 pci_read_irq(dev);
1705 pci_read_bases(dev, 1, 0);
1706 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
1707 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
1708 break;
1710 default: /* unknown header */
1711 pci_err(dev, "unknown header type %02x, ignoring device\n",
1712 dev->hdr_type);
1713 return -EIO;
1715 bad:
1716 pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
1717 dev->class, dev->hdr_type);
1718 dev->class = PCI_CLASS_NOT_DEFINED << 8;
1721 /* We found a fine healthy device, go go go... */
1722 return 0;
1725 static void pci_configure_mps(struct pci_dev *dev)
1727 struct pci_dev *bridge = pci_upstream_bridge(dev);
1728 int mps, p_mps, rc;
1730 if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
1731 return;
1733 mps = pcie_get_mps(dev);
1734 p_mps = pcie_get_mps(bridge);
1736 if (mps == p_mps)
1737 return;
1739 if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
1740 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",
1741 mps, pci_name(bridge), p_mps);
1742 return;
1746 * Fancier MPS configuration is done later by
1747 * pcie_bus_configure_settings()
1749 if (pcie_bus_config != PCIE_BUS_DEFAULT)
1750 return;
1752 rc = pcie_set_mps(dev, p_mps);
1753 if (rc) {
1754 pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1755 p_mps);
1756 return;
1759 pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
1760 p_mps, mps, 128 << dev->pcie_mpss);
1763 static struct hpp_type0 pci_default_type0 = {
1764 .revision = 1,
1765 .cache_line_size = 8,
1766 .latency_timer = 0x40,
1767 .enable_serr = 0,
1768 .enable_perr = 0,
1771 static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
1773 u16 pci_cmd, pci_bctl;
1775 if (!hpp)
1776 hpp = &pci_default_type0;
1778 if (hpp->revision > 1) {
1779 pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
1780 hpp->revision);
1781 hpp = &pci_default_type0;
1784 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
1785 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
1786 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1787 if (hpp->enable_serr)
1788 pci_cmd |= PCI_COMMAND_SERR;
1789 if (hpp->enable_perr)
1790 pci_cmd |= PCI_COMMAND_PARITY;
1791 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1793 /* Program bridge control value */
1794 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1795 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1796 hpp->latency_timer);
1797 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1798 if (hpp->enable_serr)
1799 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1800 if (hpp->enable_perr)
1801 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1802 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1806 static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
1808 int pos;
1810 if (!hpp)
1811 return;
1813 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1814 if (!pos)
1815 return;
1817 pci_warn(dev, "PCI-X settings not supported\n");
1820 static bool pcie_root_rcb_set(struct pci_dev *dev)
1822 struct pci_dev *rp = pcie_find_root_port(dev);
1823 u16 lnkctl;
1825 if (!rp)
1826 return false;
1828 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
1829 if (lnkctl & PCI_EXP_LNKCTL_RCB)
1830 return true;
1832 return false;
1835 static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
1837 int pos;
1838 u32 reg32;
1840 if (!hpp)
1841 return;
1843 if (!pci_is_pcie(dev))
1844 return;
1846 if (hpp->revision > 1) {
1847 pci_warn(dev, "PCIe settings rev %d not supported\n",
1848 hpp->revision);
1849 return;
1853 * Don't allow _HPX to change MPS or MRRS settings. We manage
1854 * those to make sure they're consistent with the rest of the
1855 * platform.
1857 hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
1858 PCI_EXP_DEVCTL_READRQ;
1859 hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
1860 PCI_EXP_DEVCTL_READRQ);
1862 /* Initialize Device Control Register */
1863 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
1864 ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
1866 /* Initialize Link Control Register */
1867 if (pcie_cap_has_lnkctl(dev)) {
1870 * If the Root Port supports Read Completion Boundary of
1871 * 128, set RCB to 128. Otherwise, clear it.
1873 hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
1874 hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
1875 if (pcie_root_rcb_set(dev))
1876 hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
1878 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
1879 ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
1882 /* Find Advanced Error Reporting Enhanced Capability */
1883 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
1884 if (!pos)
1885 return;
1887 /* Initialize Uncorrectable Error Mask Register */
1888 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
1889 reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
1890 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
1892 /* Initialize Uncorrectable Error Severity Register */
1893 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
1894 reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
1895 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
1897 /* Initialize Correctable Error Mask Register */
1898 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
1899 reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
1900 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
1902 /* Initialize Advanced Error Capabilities and Control Register */
1903 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
1904 reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
1906 /* Don't enable ECRC generation or checking if unsupported */
1907 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
1908 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
1909 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
1910 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
1911 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
1914 * FIXME: The following two registers are not supported yet.
1916 * o Secondary Uncorrectable Error Severity Register
1917 * o Secondary Uncorrectable Error Mask Register
1921 int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
1923 struct pci_host_bridge *host;
1924 u32 cap;
1925 u16 ctl;
1926 int ret;
1928 if (!pci_is_pcie(dev))
1929 return 0;
1931 ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
1932 if (ret)
1933 return 0;
1935 if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
1936 return 0;
1938 ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
1939 if (ret)
1940 return 0;
1942 host = pci_find_host_bridge(dev->bus);
1943 if (!host)
1944 return 0;
1947 * If some device in the hierarchy doesn't handle Extended Tags
1948 * correctly, make sure they're disabled.
1950 if (host->no_ext_tags) {
1951 if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
1952 pci_info(dev, "disabling Extended Tags\n");
1953 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
1954 PCI_EXP_DEVCTL_EXT_TAG);
1956 return 0;
1959 if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
1960 pci_info(dev, "enabling Extended Tags\n");
1961 pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
1962 PCI_EXP_DEVCTL_EXT_TAG);
1964 return 0;
1968 * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
1969 * @dev: PCI device to query
1971 * Returns true if the device has enabled relaxed ordering attribute.
1973 bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
1975 u16 v;
1977 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
1979 return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
1981 EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
1983 static void pci_configure_relaxed_ordering(struct pci_dev *dev)
1985 struct pci_dev *root;
1987 /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
1988 if (dev->is_virtfn)
1989 return;
1991 if (!pcie_relaxed_ordering_enabled(dev))
1992 return;
1995 * For now, we only deal with Relaxed Ordering issues with Root
1996 * Ports. Peer-to-Peer DMA is another can of worms.
1998 root = pci_find_pcie_root_port(dev);
1999 if (!root)
2000 return;
2002 if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
2003 pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
2004 PCI_EXP_DEVCTL_RELAX_EN);
2005 pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
2009 static void pci_configure_ltr(struct pci_dev *dev)
2011 #ifdef CONFIG_PCIEASPM
2012 struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
2013 u32 cap;
2014 struct pci_dev *bridge;
2016 if (!host->native_ltr)
2017 return;
2019 if (!pci_is_pcie(dev))
2020 return;
2022 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
2023 if (!(cap & PCI_EXP_DEVCAP2_LTR))
2024 return;
2027 * Software must not enable LTR in an Endpoint unless the Root
2028 * Complex and all intermediate Switches indicate support for LTR.
2029 * PCIe r3.1, sec 6.18.
2031 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
2032 dev->ltr_path = 1;
2033 else {
2034 bridge = pci_upstream_bridge(dev);
2035 if (bridge && bridge->ltr_path)
2036 dev->ltr_path = 1;
2039 if (dev->ltr_path)
2040 pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
2041 PCI_EXP_DEVCTL2_LTR_EN);
2042 #endif
2045 static void pci_configure_device(struct pci_dev *dev)
2047 struct hotplug_params hpp;
2048 int ret;
2050 pci_configure_mps(dev);
2051 pci_configure_extended_tags(dev, NULL);
2052 pci_configure_relaxed_ordering(dev);
2053 pci_configure_ltr(dev);
2055 memset(&hpp, 0, sizeof(hpp));
2056 ret = pci_get_hp_params(dev, &hpp);
2057 if (ret)
2058 return;
2060 program_hpp_type2(dev, hpp.t2);
2061 program_hpp_type1(dev, hpp.t1);
2062 program_hpp_type0(dev, hpp.t0);
2065 static void pci_release_capabilities(struct pci_dev *dev)
2067 pci_vpd_release(dev);
2068 pci_iov_release(dev);
2069 pci_free_cap_save_buffers(dev);
2073 * pci_release_dev - Free a PCI device structure when all users of it are
2074 * finished
2075 * @dev: device that's been disconnected
2077 * Will be called only by the device core when all users of this PCI device are
2078 * done.
2080 static void pci_release_dev(struct device *dev)
2082 struct pci_dev *pci_dev;
2084 pci_dev = to_pci_dev(dev);
2085 pci_release_capabilities(pci_dev);
2086 pci_release_of_node(pci_dev);
2087 pcibios_release_device(pci_dev);
2088 pci_bus_put(pci_dev->bus);
2089 kfree(pci_dev->driver_override);
2090 kfree(pci_dev->dma_alias_mask);
2091 kfree(pci_dev);
2094 struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
2096 struct pci_dev *dev;
2098 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
2099 if (!dev)
2100 return NULL;
2102 INIT_LIST_HEAD(&dev->bus_list);
2103 dev->dev.type = &pci_dev_type;
2104 dev->bus = pci_bus_get(bus);
2106 return dev;
2108 EXPORT_SYMBOL(pci_alloc_dev);
2110 static bool pci_bus_crs_vendor_id(u32 l)
2112 return (l & 0xffff) == 0x0001;
2115 static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
2116 int timeout)
2118 int delay = 1;
2120 if (!pci_bus_crs_vendor_id(*l))
2121 return true; /* not a CRS completion */
2123 if (!timeout)
2124 return false; /* CRS, but caller doesn't want to wait */
2127 * We got the reserved Vendor ID that indicates a completion with
2128 * Configuration Request Retry Status (CRS). Retry until we get a
2129 * valid Vendor ID or we time out.
2131 while (pci_bus_crs_vendor_id(*l)) {
2132 if (delay > timeout) {
2133 pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
2134 pci_domain_nr(bus), bus->number,
2135 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2137 return false;
2139 if (delay >= 1000)
2140 pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
2141 pci_domain_nr(bus), bus->number,
2142 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2144 msleep(delay);
2145 delay *= 2;
2147 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2148 return false;
2151 if (delay >= 1000)
2152 pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
2153 pci_domain_nr(bus), bus->number,
2154 PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
2156 return true;
2159 bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
2160 int timeout)
2162 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
2163 return false;
2165 /* Some broken boards return 0 or ~0 if a slot is empty: */
2166 if (*l == 0xffffffff || *l == 0x00000000 ||
2167 *l == 0x0000ffff || *l == 0xffff0000)
2168 return false;
2170 if (pci_bus_crs_vendor_id(*l))
2171 return pci_bus_wait_crs(bus, devfn, l, timeout);
2173 return true;
2175 EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
2178 * Read the config data for a PCI device, sanity-check it,
2179 * and fill in the dev structure.
2181 static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
2183 struct pci_dev *dev;
2184 u32 l;
2186 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
2187 return NULL;
2189 dev = pci_alloc_dev(bus);
2190 if (!dev)
2191 return NULL;
2193 dev->devfn = devfn;
2194 dev->vendor = l & 0xffff;
2195 dev->device = (l >> 16) & 0xffff;
2197 pci_set_of_node(dev);
2199 if (pci_setup_device(dev)) {
2200 pci_bus_put(dev->bus);
2201 kfree(dev);
2202 return NULL;
2205 return dev;
2208 static void pci_init_capabilities(struct pci_dev *dev)
2210 /* Enhanced Allocation */
2211 pci_ea_init(dev);
2213 /* Setup MSI caps & disable MSI/MSI-X interrupts */
2214 pci_msi_setup_pci_dev(dev);
2216 /* Buffers for saving PCIe and PCI-X capabilities */
2217 pci_allocate_cap_save_buffers(dev);
2219 /* Power Management */
2220 pci_pm_init(dev);
2222 /* Vital Product Data */
2223 pci_vpd_init(dev);
2225 /* Alternative Routing-ID Forwarding */
2226 pci_configure_ari(dev);
2228 /* Single Root I/O Virtualization */
2229 pci_iov_init(dev);
2231 /* Address Translation Services */
2232 pci_ats_init(dev);
2234 /* Enable ACS P2P upstream forwarding */
2235 pci_enable_acs(dev);
2237 /* Precision Time Measurement */
2238 pci_ptm_init(dev);
2240 /* Advanced Error Reporting */
2241 pci_aer_init(dev);
2243 if (pci_probe_reset_function(dev) == 0)
2244 dev->reset_fn = 1;
2248 * This is the equivalent of pci_host_bridge_msi_domain() that acts on
2249 * devices. Firmware interfaces that can select the MSI domain on a
2250 * per-device basis should be called from here.
2252 static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
2254 struct irq_domain *d;
2257 * If a domain has been set through the pcibios_add_device()
2258 * callback, then this is the one (platform code knows best).
2260 d = dev_get_msi_domain(&dev->dev);
2261 if (d)
2262 return d;
2265 * Let's see if we have a firmware interface able to provide
2266 * the domain.
2268 d = pci_msi_get_device_domain(dev);
2269 if (d)
2270 return d;
2272 return NULL;
2275 static void pci_set_msi_domain(struct pci_dev *dev)
2277 struct irq_domain *d;
2280 * If the platform or firmware interfaces cannot supply a
2281 * device-specific MSI domain, then inherit the default domain
2282 * from the host bridge itself.
2284 d = pci_dev_msi_domain(dev);
2285 if (!d)
2286 d = dev_get_msi_domain(&dev->bus->dev);
2288 dev_set_msi_domain(&dev->dev, d);
2291 void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
2293 int ret;
2295 pci_configure_device(dev);
2297 device_initialize(&dev->dev);
2298 dev->dev.release = pci_release_dev;
2300 set_dev_node(&dev->dev, pcibus_to_node(bus));
2301 dev->dev.dma_mask = &dev->dma_mask;
2302 dev->dev.dma_parms = &dev->dma_parms;
2303 dev->dev.coherent_dma_mask = 0xffffffffull;
2305 pci_set_dma_max_seg_size(dev, 65536);
2306 pci_set_dma_seg_boundary(dev, 0xffffffff);
2308 /* Fix up broken headers */
2309 pci_fixup_device(pci_fixup_header, dev);
2311 /* Moved out from quirk header fixup code */
2312 pci_reassigndev_resource_alignment(dev);
2314 /* Clear the state_saved flag */
2315 dev->state_saved = false;
2317 /* Initialize various capabilities */
2318 pci_init_capabilities(dev);
2321 * Add the device to our list of discovered devices
2322 * and the bus list for fixup functions, etc.
2324 down_write(&pci_bus_sem);
2325 list_add_tail(&dev->bus_list, &bus->devices);
2326 up_write(&pci_bus_sem);
2328 ret = pcibios_add_device(dev);
2329 WARN_ON(ret < 0);
2331 /* Set up MSI IRQ domain */
2332 pci_set_msi_domain(dev);
2334 /* Notifier could use PCI capabilities */
2335 dev->match_driver = false;
2336 ret = device_add(&dev->dev);
2337 WARN_ON(ret < 0);
2340 struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
2342 struct pci_dev *dev;
2344 dev = pci_get_slot(bus, devfn);
2345 if (dev) {
2346 pci_dev_put(dev);
2347 return dev;
2350 dev = pci_scan_device(bus, devfn);
2351 if (!dev)
2352 return NULL;
2354 pci_device_add(dev, bus);
2356 return dev;
2358 EXPORT_SYMBOL(pci_scan_single_device);
2360 static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
2362 int pos;
2363 u16 cap = 0;
2364 unsigned next_fn;
2366 if (pci_ari_enabled(bus)) {
2367 if (!dev)
2368 return 0;
2369 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
2370 if (!pos)
2371 return 0;
2373 pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
2374 next_fn = PCI_ARI_CAP_NFN(cap);
2375 if (next_fn <= fn)
2376 return 0; /* protect against malformed list */
2378 return next_fn;
2381 /* dev may be NULL for non-contiguous multifunction devices */
2382 if (!dev || dev->multifunction)
2383 return (fn + 1) % 8;
2385 return 0;
2388 static int only_one_child(struct pci_bus *bus)
2390 struct pci_dev *bridge = bus->self;
2393 * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
2394 * we scan for all possible devices, not just Device 0.
2396 if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
2397 return 0;
2400 * A PCIe Downstream Port normally leads to a Link with only Device
2401 * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan
2402 * only for Device 0 in that situation.
2404 * Checking has_secondary_link is a hack to identify Downstream
2405 * Ports because sometimes Switches are configured such that the
2406 * PCIe Port Type labels are backwards.
2408 if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link)
2409 return 1;
2411 return 0;
2415 * pci_scan_slot - Scan a PCI slot on a bus for devices
2416 * @bus: PCI bus to scan
2417 * @devfn: slot number to scan (must have zero function)
2419 * Scan a PCI slot on the specified PCI bus for devices, adding
2420 * discovered devices to the @bus->devices list. New devices
2421 * will not have is_added set.
2423 * Returns the number of new devices found.
2425 int pci_scan_slot(struct pci_bus *bus, int devfn)
2427 unsigned fn, nr = 0;
2428 struct pci_dev *dev;
2430 if (only_one_child(bus) && (devfn > 0))
2431 return 0; /* Already scanned the entire slot */
2433 dev = pci_scan_single_device(bus, devfn);
2434 if (!dev)
2435 return 0;
2436 if (!dev->is_added)
2437 nr++;
2439 for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
2440 dev = pci_scan_single_device(bus, devfn + fn);
2441 if (dev) {
2442 if (!dev->is_added)
2443 nr++;
2444 dev->multifunction = 1;
2448 /* Only one slot has PCIe device */
2449 if (bus->self && nr)
2450 pcie_aspm_init_link_state(bus->self);
2452 return nr;
2454 EXPORT_SYMBOL(pci_scan_slot);
2456 static int pcie_find_smpss(struct pci_dev *dev, void *data)
2458 u8 *smpss = data;
2460 if (!pci_is_pcie(dev))
2461 return 0;
2464 * We don't have a way to change MPS settings on devices that have
2465 * drivers attached. A hot-added device might support only the minimum
2466 * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
2467 * where devices may be hot-added, we limit the fabric MPS to 128 so
2468 * hot-added devices will work correctly.
2470 * However, if we hot-add a device to a slot directly below a Root
2471 * Port, it's impossible for there to be other existing devices below
2472 * the port. We don't limit the MPS in this case because we can
2473 * reconfigure MPS on both the Root Port and the hot-added device,
2474 * and there are no other devices involved.
2476 * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
2478 if (dev->is_hotplug_bridge &&
2479 pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
2480 *smpss = 0;
2482 if (*smpss > dev->pcie_mpss)
2483 *smpss = dev->pcie_mpss;
2485 return 0;
2488 static void pcie_write_mps(struct pci_dev *dev, int mps)
2490 int rc;
2492 if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
2493 mps = 128 << dev->pcie_mpss;
2495 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
2496 dev->bus->self)
2499 * For "Performance", the assumption is made that
2500 * downstream communication will never be larger than
2501 * the MRRS. So, the MPS only needs to be configured
2502 * for the upstream communication. This being the case,
2503 * walk from the top down and set the MPS of the child
2504 * to that of the parent bus.
2506 * Configure the device MPS with the smaller of the
2507 * device MPSS or the bridge MPS (which is assumed to be
2508 * properly configured at this point to the largest
2509 * allowable MPS based on its parent bus).
2511 mps = min(mps, pcie_get_mps(dev->bus->self));
2514 rc = pcie_set_mps(dev, mps);
2515 if (rc)
2516 pci_err(dev, "Failed attempting to set the MPS\n");
2519 static void pcie_write_mrrs(struct pci_dev *dev)
2521 int rc, mrrs;
2524 * In the "safe" case, do not configure the MRRS. There appear to be
2525 * issues with setting MRRS to 0 on a number of devices.
2527 if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
2528 return;
2531 * For max performance, the MRRS must be set to the largest supported
2532 * value. However, it cannot be configured larger than the MPS the
2533 * device or the bus can support. This should already be properly
2534 * configured by a prior call to pcie_write_mps().
2536 mrrs = pcie_get_mps(dev);
2539 * MRRS is a R/W register. Invalid values can be written, but a
2540 * subsequent read will verify if the value is acceptable or not.
2541 * If the MRRS value provided is not acceptable (e.g., too large),
2542 * shrink the value until it is acceptable to the HW.
2544 while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
2545 rc = pcie_set_readrq(dev, mrrs);
2546 if (!rc)
2547 break;
2549 pci_warn(dev, "Failed attempting to set the MRRS\n");
2550 mrrs /= 2;
2553 if (mrrs < 128)
2554 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");
2557 static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
2559 int mps, orig_mps;
2561 if (!pci_is_pcie(dev))
2562 return 0;
2564 if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
2565 pcie_bus_config == PCIE_BUS_DEFAULT)
2566 return 0;
2568 mps = 128 << *(u8 *)data;
2569 orig_mps = pcie_get_mps(dev);
2571 pcie_write_mps(dev, mps);
2572 pcie_write_mrrs(dev);
2574 pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
2575 pcie_get_mps(dev), 128 << dev->pcie_mpss,
2576 orig_mps, pcie_get_readrq(dev));
2578 return 0;
2582 * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
2583 * parents then children fashion. If this changes, then this code will not
2584 * work as designed.
2586 void pcie_bus_configure_settings(struct pci_bus *bus)
2588 u8 smpss = 0;
2590 if (!bus->self)
2591 return;
2593 if (!pci_is_pcie(bus->self))
2594 return;
2597 * FIXME - Peer to peer DMA is possible, though the endpoint would need
2598 * to be aware of the MPS of the destination. To work around this,
2599 * simply force the MPS of the entire system to the smallest possible.
2601 if (pcie_bus_config == PCIE_BUS_PEER2PEER)
2602 smpss = 0;
2604 if (pcie_bus_config == PCIE_BUS_SAFE) {
2605 smpss = bus->self->pcie_mpss;
2607 pcie_find_smpss(bus->self, &smpss);
2608 pci_walk_bus(bus, pcie_find_smpss, &smpss);
2611 pcie_bus_configure_set(bus->self, &smpss);
2612 pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
2614 EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
2617 * Called after each bus is probed, but before its children are examined. This
2618 * is marked as __weak because multiple architectures define it.
2620 void __weak pcibios_fixup_bus(struct pci_bus *bus)
2622 /* nothing to do, expected to be removed in the future */
2626 * pci_scan_child_bus_extend() - Scan devices below a bus
2627 * @bus: Bus to scan for devices
2628 * @available_buses: Total number of buses available (%0 does not try to
2629 * extend beyond the minimal)
2631 * Scans devices below @bus including subordinate buses. Returns new
2632 * subordinate number including all the found devices. Passing
2633 * @available_buses causes the remaining bus space to be distributed
2634 * equally between hotplug-capable bridges to allow future extension of the
2635 * hierarchy.
2637 static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
2638 unsigned int available_buses)
2640 unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
2641 unsigned int start = bus->busn_res.start;
2642 unsigned int devfn, fn, cmax, max = start;
2643 struct pci_dev *dev;
2644 int nr_devs;
2646 dev_dbg(&bus->dev, "scanning bus\n");
2648 /* Go find them, Rover! */
2649 for (devfn = 0; devfn < 256; devfn += 8) {
2650 nr_devs = pci_scan_slot(bus, devfn);
2653 * The Jailhouse hypervisor may pass individual functions of a
2654 * multi-function device to a guest without passing function 0.
2655 * Look for them as well.
2657 if (jailhouse_paravirt() && nr_devs == 0) {
2658 for (fn = 1; fn < 8; fn++) {
2659 dev = pci_scan_single_device(bus, devfn + fn);
2660 if (dev)
2661 dev->multifunction = 1;
2666 /* Reserve buses for SR-IOV capability */
2667 used_buses = pci_iov_bus_range(bus);
2668 max += used_buses;
2671 * After performing arch-dependent fixup of the bus, look behind
2672 * all PCI-to-PCI bridges on this bus.
2674 if (!bus->is_added) {
2675 dev_dbg(&bus->dev, "fixups for bus\n");
2676 pcibios_fixup_bus(bus);
2677 bus->is_added = 1;
2681 * Calculate how many hotplug bridges and normal bridges there
2682 * are on this bus. We will distribute the additional available
2683 * buses between hotplug bridges.
2685 for_each_pci_bridge(dev, bus) {
2686 if (dev->is_hotplug_bridge)
2687 hotplug_bridges++;
2688 else
2689 normal_bridges++;
2693 * Scan bridges that are already configured. We don't touch them
2694 * unless they are misconfigured (which will be done in the second
2695 * scan below).
2697 for_each_pci_bridge(dev, bus) {
2698 cmax = max;
2699 max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
2702 * Reserve one bus for each bridge now to avoid extending
2703 * hotplug bridges too much during the second scan below.
2705 used_buses++;
2706 if (cmax - max > 1)
2707 used_buses += cmax - max - 1;
2710 /* Scan bridges that need to be reconfigured */
2711 for_each_pci_bridge(dev, bus) {
2712 unsigned int buses = 0;
2714 if (!hotplug_bridges && normal_bridges == 1) {
2717 * There is only one bridge on the bus (upstream
2718 * port) so it gets all available buses which it
2719 * can then distribute to the possible hotplug
2720 * bridges below.
2722 buses = available_buses;
2723 } else if (dev->is_hotplug_bridge) {
2726 * Distribute the extra buses between hotplug
2727 * bridges if any.
2729 buses = available_buses / hotplug_bridges;
2730 buses = min(buses, available_buses - used_buses + 1);
2733 cmax = max;
2734 max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
2735 /* One bus is already accounted so don't add it again */
2736 if (max - cmax > 1)
2737 used_buses += max - cmax - 1;
2741 * Make sure a hotplug bridge has at least the minimum requested
2742 * number of buses but allow it to grow up to the maximum available
2743 * bus number of there is room.
2745 if (bus->self && bus->self->is_hotplug_bridge) {
2746 used_buses = max_t(unsigned int, available_buses,
2747 pci_hotplug_bus_size - 1);
2748 if (max - start < used_buses) {
2749 max = start + used_buses;
2751 /* Do not allocate more buses than we have room left */
2752 if (max > bus->busn_res.end)
2753 max = bus->busn_res.end;
2755 dev_dbg(&bus->dev, "%pR extended by %#02x\n",
2756 &bus->busn_res, max - start);
2761 * We've scanned the bus and so we know all about what's on
2762 * the other side of any bridges that may be on this bus plus
2763 * any devices.
2765 * Return how far we've got finding sub-buses.
2767 dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
2768 return max;
2772 * pci_scan_child_bus() - Scan devices below a bus
2773 * @bus: Bus to scan for devices
2775 * Scans devices below @bus including subordinate buses. Returns new
2776 * subordinate number including all the found devices.
2778 unsigned int pci_scan_child_bus(struct pci_bus *bus)
2780 return pci_scan_child_bus_extend(bus, 0);
2782 EXPORT_SYMBOL_GPL(pci_scan_child_bus);
2785 * pcibios_root_bridge_prepare - Platform-specific host bridge setup
2786 * @bridge: Host bridge to set up
2788 * Default empty implementation. Replace with an architecture-specific setup
2789 * routine, if necessary.
2791 int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
2793 return 0;
2796 void __weak pcibios_add_bus(struct pci_bus *bus)
2800 void __weak pcibios_remove_bus(struct pci_bus *bus)
2804 struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
2805 struct pci_ops *ops, void *sysdata, struct list_head *resources)
2807 int error;
2808 struct pci_host_bridge *bridge;
2810 bridge = pci_alloc_host_bridge(0);
2811 if (!bridge)
2812 return NULL;
2814 bridge->dev.parent = parent;
2816 list_splice_init(resources, &bridge->windows);
2817 bridge->sysdata = sysdata;
2818 bridge->busnr = bus;
2819 bridge->ops = ops;
2821 error = pci_register_host_bridge(bridge);
2822 if (error < 0)
2823 goto err_out;
2825 return bridge->bus;
2827 err_out:
2828 kfree(bridge);
2829 return NULL;
2831 EXPORT_SYMBOL_GPL(pci_create_root_bus);
2833 int pci_host_probe(struct pci_host_bridge *bridge)
2835 struct pci_bus *bus, *child;
2836 int ret;
2838 ret = pci_scan_root_bus_bridge(bridge);
2839 if (ret < 0) {
2840 dev_err(bridge->dev.parent, "Scanning root bridge failed");
2841 return ret;
2844 bus = bridge->bus;
2847 * We insert PCI resources into the iomem_resource and
2848 * ioport_resource trees in either pci_bus_claim_resources()
2849 * or pci_bus_assign_resources().
2851 if (pci_has_flag(PCI_PROBE_ONLY)) {
2852 pci_bus_claim_resources(bus);
2853 } else {
2854 pci_bus_size_bridges(bus);
2855 pci_bus_assign_resources(bus);
2857 list_for_each_entry(child, &bus->children, node)
2858 pcie_bus_configure_settings(child);
2861 pci_bus_add_devices(bus);
2862 return 0;
2864 EXPORT_SYMBOL_GPL(pci_host_probe);
2866 int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
2868 struct resource *res = &b->busn_res;
2869 struct resource *parent_res, *conflict;
2871 res->start = bus;
2872 res->end = bus_max;
2873 res->flags = IORESOURCE_BUS;
2875 if (!pci_is_root_bus(b))
2876 parent_res = &b->parent->busn_res;
2877 else {
2878 parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
2879 res->flags |= IORESOURCE_PCI_FIXED;
2882 conflict = request_resource_conflict(parent_res, res);
2884 if (conflict)
2885 dev_printk(KERN_DEBUG, &b->dev,
2886 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
2887 res, pci_is_root_bus(b) ? "domain " : "",
2888 parent_res, conflict->name, conflict);
2890 return conflict == NULL;
2893 int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
2895 struct resource *res = &b->busn_res;
2896 struct resource old_res = *res;
2897 resource_size_t size;
2898 int ret;
2900 if (res->start > bus_max)
2901 return -EINVAL;
2903 size = bus_max - res->start + 1;
2904 ret = adjust_resource(res, res->start, size);
2905 dev_printk(KERN_DEBUG, &b->dev,
2906 "busn_res: %pR end %s updated to %02x\n",
2907 &old_res, ret ? "can not be" : "is", bus_max);
2909 if (!ret && !res->parent)
2910 pci_bus_insert_busn_res(b, res->start, res->end);
2912 return ret;
2915 void pci_bus_release_busn_res(struct pci_bus *b)
2917 struct resource *res = &b->busn_res;
2918 int ret;
2920 if (!res->flags || !res->parent)
2921 return;
2923 ret = release_resource(res);
2924 dev_printk(KERN_DEBUG, &b->dev,
2925 "busn_res: %pR %s released\n",
2926 res, ret ? "can not be" : "is");
2929 int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
2931 struct resource_entry *window;
2932 bool found = false;
2933 struct pci_bus *b;
2934 int max, bus, ret;
2936 if (!bridge)
2937 return -EINVAL;
2939 resource_list_for_each_entry(window, &bridge->windows)
2940 if (window->res->flags & IORESOURCE_BUS) {
2941 found = true;
2942 break;
2945 ret = pci_register_host_bridge(bridge);
2946 if (ret < 0)
2947 return ret;
2949 b = bridge->bus;
2950 bus = bridge->busnr;
2952 if (!found) {
2953 dev_info(&b->dev,
2954 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2955 bus);
2956 pci_bus_insert_busn_res(b, bus, 255);
2959 max = pci_scan_child_bus(b);
2961 if (!found)
2962 pci_bus_update_busn_res_end(b, max);
2964 return 0;
2966 EXPORT_SYMBOL(pci_scan_root_bus_bridge);
2968 struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
2969 struct pci_ops *ops, void *sysdata, struct list_head *resources)
2971 struct resource_entry *window;
2972 bool found = false;
2973 struct pci_bus *b;
2974 int max;
2976 resource_list_for_each_entry(window, resources)
2977 if (window->res->flags & IORESOURCE_BUS) {
2978 found = true;
2979 break;
2982 b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
2983 if (!b)
2984 return NULL;
2986 if (!found) {
2987 dev_info(&b->dev,
2988 "No busn resource found for root bus, will use [bus %02x-ff]\n",
2989 bus);
2990 pci_bus_insert_busn_res(b, bus, 255);
2993 max = pci_scan_child_bus(b);
2995 if (!found)
2996 pci_bus_update_busn_res_end(b, max);
2998 return b;
3000 EXPORT_SYMBOL(pci_scan_root_bus);
3002 struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
3003 void *sysdata)
3005 LIST_HEAD(resources);
3006 struct pci_bus *b;
3008 pci_add_resource(&resources, &ioport_resource);
3009 pci_add_resource(&resources, &iomem_resource);
3010 pci_add_resource(&resources, &busn_resource);
3011 b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
3012 if (b) {
3013 pci_scan_child_bus(b);
3014 } else {
3015 pci_free_resource_list(&resources);
3017 return b;
3019 EXPORT_SYMBOL(pci_scan_bus);
3022 * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
3023 * @bridge: PCI bridge for the bus to scan
3025 * Scan a PCI bus and child buses for new devices, add them,
3026 * and enable them, resizing bridge mmio/io resource if necessary
3027 * and possible. The caller must ensure the child devices are already
3028 * removed for resizing to occur.
3030 * Returns the max number of subordinate bus discovered.
3032 unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
3034 unsigned int max;
3035 struct pci_bus *bus = bridge->subordinate;
3037 max = pci_scan_child_bus(bus);
3039 pci_assign_unassigned_bridge_resources(bridge);
3041 pci_bus_add_devices(bus);
3043 return max;
3047 * pci_rescan_bus - Scan a PCI bus for devices
3048 * @bus: PCI bus to scan
3050 * Scan a PCI bus and child buses for new devices, add them,
3051 * and enable them.
3053 * Returns the max number of subordinate bus discovered.
3055 unsigned int pci_rescan_bus(struct pci_bus *bus)
3057 unsigned int max;
3059 max = pci_scan_child_bus(bus);
3060 pci_assign_unassigned_bus_resources(bus);
3061 pci_bus_add_devices(bus);
3063 return max;
3065 EXPORT_SYMBOL_GPL(pci_rescan_bus);
3068 * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
3069 * routines should always be executed under this mutex.
3071 static DEFINE_MUTEX(pci_rescan_remove_lock);
3073 void pci_lock_rescan_remove(void)
3075 mutex_lock(&pci_rescan_remove_lock);
3077 EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
3079 void pci_unlock_rescan_remove(void)
3081 mutex_unlock(&pci_rescan_remove_lock);
3083 EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
3085 static int __init pci_sort_bf_cmp(const struct device *d_a,
3086 const struct device *d_b)
3088 const struct pci_dev *a = to_pci_dev(d_a);
3089 const struct pci_dev *b = to_pci_dev(d_b);
3091 if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
3092 else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
3094 if (a->bus->number < b->bus->number) return -1;
3095 else if (a->bus->number > b->bus->number) return 1;
3097 if (a->devfn < b->devfn) return -1;
3098 else if (a->devfn > b->devfn) return 1;
3100 return 0;
3103 void __init pci_sort_breadthfirst(void)
3105 bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
3108 int pci_hp_add_bridge(struct pci_dev *dev)
3110 struct pci_bus *parent = dev->bus;
3111 int busnr, start = parent->busn_res.start;
3112 unsigned int available_buses = 0;
3113 int end = parent->busn_res.end;
3115 for (busnr = start; busnr <= end; busnr++) {
3116 if (!pci_find_bus(pci_domain_nr(parent), busnr))
3117 break;
3119 if (busnr-- > end) {
3120 pci_err(dev, "No bus number available for hot-added bridge\n");
3121 return -1;
3124 /* Scan bridges that are already configured */
3125 busnr = pci_scan_bridge(parent, dev, busnr, 0);
3128 * Distribute the available bus numbers between hotplug-capable
3129 * bridges to make extending the chain later possible.
3131 available_buses = end - busnr;
3133 /* Scan bridges that need to be reconfigured */
3134 pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
3136 if (!dev->subordinate)
3137 return -1;
3139 return 0;
3141 EXPORT_SYMBOL_GPL(pci_hp_add_bridge);