1 // SPDX-License-Identifier: GPL-2.0
3 * PCI detection and setup code
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/init.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>
23 #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
24 #define CARDBUS_RESERVE_BUSNR 3
26 static struct resource busn_resource
= {
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
;
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
)
53 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
57 r
->domain_nr
= domain_nr
;
60 r
->res
.flags
= IORESOURCE_BUS
| IORESOURCE_PCI_FIXED
;
62 list_add_tail(&r
->list
, &pci_domain_busn_res_list
);
67 static int find_anything(struct device
*dev
, void *data
)
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)
82 dev
= bus_find_device(&pci_bus_type
, NULL
, NULL
, find_anything
);
83 no_devices
= (dev
== NULL
);
87 EXPORT_SYMBOL(no_pci_devices
);
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
);
102 static struct class pcibus_class
= {
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 */
121 * Get the lowest of them to find the decode size, and from that
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
)
136 static inline unsigned long decode_bar(struct pci_dev
*dev
, u32 bar
)
141 if ((bar
& PCI_BASE_ADDRESS_SPACE
) == PCI_BASE_ADDRESS_SPACE_IO
) {
142 flags
= bar
& ~PCI_BASE_ADDRESS_IO_MASK
;
143 flags
|= IORESOURCE_IO
;
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
;
154 case PCI_BASE_ADDRESS_MEM_TYPE_32
:
156 case PCI_BASE_ADDRESS_MEM_TYPE_1M
:
157 /* 1M mem BAR treated as 32-bit BAR */
159 case PCI_BASE_ADDRESS_MEM_TYPE_64
:
160 flags
|= IORESOURCE_MEM_64
;
163 /* mem unknown type treated as 32-bit BAR */
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
;
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
212 if (sz
== 0xffffffff)
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.
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
;
230 l64
= l
& PCI_BASE_ADDRESS_MEM_MASK
;
231 sz64
= sz
& PCI_BASE_ADDRESS_MEM_MASK
;
232 mask64
= (u32
)PCI_BASE_ADDRESS_MEM_MASK
;
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
);
259 sz64
= pci_size(l64
, sz64
, mask64
);
261 pci_info(dev
, FW_BUG
"reg 0x%x: invalid BAR (can't size)\n",
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
;
272 pci_err(dev
, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
273 pos
, (unsigned long long)sz64
);
277 if ((sizeof(pci_bus_addr_t
) < 8) && l
) {
278 /* Above 32-bit boundary; try to reallocate */
279 res
->flags
|= IORESOURCE_UNSET
;
282 pci_info(dev
, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
283 pos
, (unsigned long long)l64
);
289 region
.end
= l64
+ sz64
;
291 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
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
;
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
);
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
)
332 /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */
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
);
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);
383 res
->flags
= (io_base_lo
& PCI_IO_RANGE_TYPE_MASK
) | IORESOURCE_IO
;
385 region
.end
= limit
+ io_granularity
- 1;
386 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
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;
405 res
->flags
= (mem_base_lo
& PCI_MEMORY_RANGE_TYPE_MASK
) | IORESOURCE_MEM
;
407 region
.end
= limit
+ 0xfffff;
408 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
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
;
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
);
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
;
460 region
.end
= limit
+ 0xfffff;
461 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
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
;
472 if (pci_is_root_bus(child
)) /* It's a host bus, nothing to read */
475 pci_info(dev
, "PCI bridge to %pR%s\n",
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",
500 static struct pci_bus
*pci_alloc_bus(struct pci_bus
*parent
)
504 b
= kzalloc(sizeof(*b
), GFP_KERNEL
);
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
517 b
->domain_nr
= parent
->domain_nr
;
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
);
563 pci_init_host_bridge(bridge
);
564 bridge
->dev
.release
= pci_release_host_bridge_dev
;
568 EXPORT_SYMBOL(pci_alloc_host_bridge
);
570 struct pci_host_bridge
*devm_pci_alloc_host_bridge(struct device
*dev
,
573 struct pci_host_bridge
*bridge
;
575 bridge
= devm_kzalloc(dev
, sizeof(*bridge
) + priv
, GFP_KERNEL
);
579 pci_init_host_bridge(bridge
);
580 bridge
->dev
.release
= devm_pci_release_host_bridge_dev
;
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
);
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
[] = {
646 static enum pci_bus_speed
agp_speed(int agp3
, int agpstat
)
652 else if (agpstat
& 2)
654 else if (agpstat
& 1)
666 return agp_speeds
[index
];
669 static void pci_set_bus_speed(struct pci_bus
*bus
)
671 struct pci_dev
*bridge
= bus
->self
;
674 pos
= pci_find_capability(bridge
, PCI_CAP_ID_AGP
);
676 pos
= pci_find_capability(bridge
, PCI_CAP_ID_AGP3
);
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
);
690 enum pci_bus_speed max
;
692 pci_read_config_word(bridge
, pos
+ PCI_X_BRIDGE_SSTATUS
,
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
;
703 max
= PCI_SPEED_133MHz_PCIX
;
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];
715 if (pci_is_pcie(bridge
)) {
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
);
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.
745 struct fwnode_handle
*fwnode
= pci_root_bus_fwnode(bus
);
748 d
= irq_find_matching_fwnode(fwnode
,
756 static void pci_set_bus_msi_domain(struct pci_bus
*bus
)
758 struct irq_domain
*d
;
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
) {
768 d
= dev_get_msi_domain(&b
->self
->dev
);
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
;
789 bus
= pci_alloc_bus(NULL
);
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
);
805 b
= pci_find_bus(pci_domain_nr(bus
), bridge
->busnr
);
807 /* Ignore it if we already got here via a different bridge */
808 dev_dbg(&b
->dev
, "bus already known\n");
813 dev_set_name(&bridge
->dev
, "pci%04x:%02x", pci_domain_nr(bus
),
816 err
= pcibios_root_bridge_prepare(bridge
);
820 err
= device_register(&bridge
->dev
);
822 put_device(&bridge
->dev
);
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
);
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
);
843 pcibios_add_bus(bus
);
845 /* Create legacy_io and legacy_mem files for this bus */
846 pci_create_legacy_files(bus
);
849 dev_info(parent
, "PCI host bridge to bus %s\n", name
);
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
;
859 if (res
->flags
& IORESOURCE_BUS
)
860 pci_bus_insert_busn_res(bus
, bus
->number
, res
->end
);
862 pci_bus_add_resource(bus
, res
, 0);
865 if (resource_type(res
) == IORESOURCE_IO
)
866 fmt
= " (bus address [%#06llx-%#06llx])";
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
));
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
);
886 put_device(&bridge
->dev
);
887 device_unregister(&bridge
->dev
);
894 static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev
*bridge
)
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
)
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
))
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
);
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
;
940 /* Allocate a new bus and inherit stuff from the parent */
941 child
= pci_alloc_bus(parent
);
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;
964 child
->dev
.parent
= parent
->bridge
;
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
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
;
992 pci_set_bus_msi_domain(child
);
993 ret
= device_register(&child
->dev
);
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
);
1010 struct pci_bus
*pci_add_new_bus(struct pci_bus
*parent
, struct pci_dev
*dev
,
1013 struct pci_bus
*child
;
1015 child
= pci_alloc_child_bus(parent
, dev
, busnr
);
1017 down_write(&pci_bus_sem
);
1018 list_add_tail(&child
->node
, &parent
->children
);
1019 up_write(&pci_bus_sem
);
1023 EXPORT_SYMBOL(pci_add_new_bus
);
1025 static void pci_enable_crs(struct pci_dev
*pdev
)
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
,
1066 struct pci_bus
*child
;
1067 int is_cardbus
= (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
);
1068 u32 buses
, i
, j
= 0;
1070 u8 primary
, secondary
, subordinate
;
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 */
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
);
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
) {
1116 * Bus already configured by firmware, process it in the
1117 * first pass and just note the configuration.
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
);
1130 child
= pci_add_new_bus(bus
, dev
, secondary
);
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",
1143 /* Subordinate should equal child->busn_res.end */
1144 if (subordinate
> max
)
1149 * We need to assign a number to this bus which we always
1150 * do in the second 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
1163 pci_write_config_dword(dev
, PCI_PRIMARY_BUS
,
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);
1178 child
= pci_add_new_bus(bus
, dev
, max
+1);
1181 pci_bus_insert_busn_res(child
, max
+1,
1185 if (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.
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
);
1206 child
->bridge_ctl
= bctl
;
1207 max
= pci_scan_child_bus_extend(child
, available_buses
);
1211 * For CardBus bridges, we leave 4 bus numbers as
1212 * cards with a PCI-to-PCI bridge can be inserted
1215 for (i
= 0; i
< CARDBUS_RESERVE_BUSNR
; i
++) {
1216 struct pci_bus
*parent
= bus
;
1217 if (pci_find_bus(pci_domain_nr(bus
),
1220 while (parent
->parent
) {
1221 if ((!pcibios_assign_all_busses()) &&
1222 (parent
->busn_res
.end
> max
) &&
1223 (parent
->busn_res
.end
<= max
+i
)) {
1226 parent
= parent
->parent
;
1231 * Often, there are two CardBus
1232 * bridges -- try to leave one
1233 * valid bus number for each one.
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",
1265 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, bctl
);
1267 pm_runtime_put(&dev
->dev
);
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
)
1305 /* VFs are not allowed to use INTx, so skip the config reads */
1306 if (dev
->is_virtfn
) {
1312 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &irq
);
1315 pci_read_config_byte(dev
, PCI_INTERRUPT_LINE
, &irq
);
1319 void set_pcie_port_type(struct pci_dev
*pdev
)
1324 struct pci_dev
*parent
;
1326 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
1330 pdev
->pcie_cap
= pos
;
1331 pci_read_config_word(pdev
, pos
+ PCI_EXP_FLAGS
, ®16
);
1332 pdev
->pcie_flags_reg
= reg16
;
1333 pci_read_config_word(pdev
, pos
+ PCI_EXP_DEVCAP
, ®16
);
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
)
1363 pcie_capability_read_dword(pdev
, PCI_EXP_SLTCAP
, ®32
);
1364 if (reg32
& PCI_EXP_SLTCAP_HPC
)
1365 pdev
->is_hotplug_bridge
= 1;
1368 static void set_pcie_thunderbolt(struct pci_dev
*dev
)
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;
1387 * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
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.
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
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
1424 * pci_cfg_space_size - Get the configuration space size of the 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
)
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
)
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
);
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
)
1478 #ifdef CONFIG_PCI_IOV
1480 return dev
->physfn
->sriov
->class;
1482 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &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
;
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
)
1503 #ifdef CONFIG_PCI_IOV
1505 return dev
->physfn
->sriov
->hdr_type
;
1507 pci_read_config_byte(dev
, PCI_HEADER_TYPE
, &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
);
1522 pci_msi_set_enable(dev
, 0);
1524 dev
->msix_cap
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
1526 pci_msix_clear_and_set_ctrl(dev
, PCI_MSIX_FLAGS_ENABLE
, 0);
1530 * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
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.
1557 static void early_dump_pci_device(struct pci_dev
*pdev
)
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,
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
)
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);
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
)
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
) {
1667 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &progif
);
1668 if ((progif
& 1) == 0) {
1669 region
.start
= 0x1F0;
1671 res
= &dev
->resource
[0];
1672 res
->flags
= LEGACY_IO_RESOURCE
;
1673 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
1674 pci_info(dev
, "legacy IDE quirk: reg 0x10: %pR\n",
1676 region
.start
= 0x3F6;
1678 res
= &dev
->resource
[1];
1679 res
->flags
= LEGACY_IO_RESOURCE
;
1680 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
1681 pci_info(dev
, "legacy IDE quirk: reg 0x14: %pR\n",
1684 if ((progif
& 4) == 0) {
1685 region
.start
= 0x170;
1687 res
= &dev
->resource
[2];
1688 res
->flags
= LEGACY_IO_RESOURCE
;
1689 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
1690 pci_info(dev
, "legacy IDE quirk: reg 0x18: %pR\n",
1692 region
.start
= 0x376;
1694 res
= &dev
->resource
[3];
1695 res
->flags
= LEGACY_IO_RESOURCE
;
1696 pcibios_bus_to_resource(dev
->bus
, res
, ®ion
);
1697 pci_info(dev
, "legacy IDE quirk: reg 0x1c: %pR\n",
1703 case PCI_HEADER_TYPE_BRIDGE
: /* bridge header */
1704 if (class != PCI_CLASS_BRIDGE_PCI
)
1708 * The PCI-to-PCI bridge spec requires that subtractive
1709 * decoding (i.e. transparent) bridge must have programming
1710 * interface code of 0x01.
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
);
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
);
1723 case PCI_HEADER_TYPE_CARDBUS
: /* CardBus bridge header */
1724 if (class != PCI_CLASS_BRIDGE_CARDBUS
)
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
);
1732 default: /* unknown header */
1733 pci_err(dev
, "unknown header type %02x, ignoring device\n",
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... */
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
))
1755 /* MPS and MRRS fields are of type 'RsvdP' for VFs, short-circuit out */
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
)
1767 mps
= 128 << dev
->pcie_mpss
;
1768 rc
= pcie_set_mps(dev
, mps
);
1770 pci_warn(dev
, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1776 if (!bridge
|| !pci_is_pcie(bridge
))
1779 mps
= pcie_get_mps(dev
);
1780 p_mps
= pcie_get_mps(bridge
);
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
);
1792 * Fancier MPS configuration is done later by
1793 * pcie_bus_configure_settings()
1795 if (pcie_bus_config
!= PCIE_BUS_DEFAULT
)
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
);
1808 pci_warn(dev
, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
1813 pci_info(dev
, "Max Payload Size set to %d (was %d, max %d)\n",
1817 static struct hpp_type0 pci_default_type0
= {
1819 .cache_line_size
= 8,
1820 .latency_timer
= 0x40,
1825 static void program_hpp_type0(struct pci_dev
*dev
, struct hpp_type0
*hpp
)
1827 u16 pci_cmd
, pci_bctl
;
1830 hpp
= &pci_default_type0
;
1832 if (hpp
->revision
> 1) {
1833 pci_warn(dev
, "PCI settings rev %d not supported; using defaults\n",
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
)
1867 pos
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
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
);
1882 pcie_capability_read_word(rp
, PCI_EXP_LNKCTL
, &lnkctl
);
1883 if (lnkctl
& PCI_EXP_LNKCTL_RCB
)
1889 static void program_hpp_type2(struct pci_dev
*dev
, struct hpp_type2
*hpp
)
1897 if (!pci_is_pcie(dev
))
1900 if (hpp
->revision
> 1) {
1901 pci_warn(dev
, "PCIe settings rev %d not supported\n",
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
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
);
1941 /* Initialize Uncorrectable Error Mask Register */
1942 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_MASK
, ®32
);
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
, ®32
);
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
, ®32
);
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
, ®32
);
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
;
1982 if (!pci_is_pcie(dev
))
1985 ret
= pcie_capability_read_dword(dev
, PCI_EXP_DEVCAP
, &cap
);
1989 if (!(cap
& PCI_EXP_DEVCAP_EXT_TAG
))
1992 ret
= pcie_capability_read_word(dev
, PCI_EXP_DEVCTL
, &ctl
);
1996 host
= pci_find_host_bridge(dev
->bus
);
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
);
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
);
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
)
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 */
2045 if (!pcie_relaxed_ordering_enabled(dev
))
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
);
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
;
2070 if (!pci_is_pcie(dev
))
2073 pcie_capability_read_dword(dev
, PCI_EXP_DEVCAP2
, &cap
);
2074 if (!(cap
& PCI_EXP_DEVCAP2_LTR
))
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
) {
2084 bridge
= pci_upstream_bridge(dev
);
2085 if (bridge
&& bridge
->ltr_path
)
2091 if (!host
->native_ltr
)
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
);
2109 static void pci_configure_eetlp_prefix(struct pci_dev
*dev
)
2111 #ifdef CONFIG_PCI_PASID
2112 struct pci_dev
*bridge
;
2116 if (!pci_is_pcie(dev
))
2119 pcie_capability_read_dword(dev
, PCI_EXP_DEVCAP2
, &cap
);
2120 if (!(cap
& PCI_EXP_DEVCAP2_EE_PREFIX
))
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;
2128 bridge
= pci_upstream_bridge(dev
);
2129 if (bridge
&& bridge
->eetlp_prefix_path
)
2130 dev
->eetlp_prefix_path
= 1;
2135 static void pci_configure_device(struct pci_dev
*dev
)
2137 struct hotplug_params hpp
;
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
);
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
)
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
2167 * @dev: device that's been disconnected
2169 * Will be called only by the device core when all users of this PCI device are
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
);
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
);
2194 INIT_LIST_HEAD(&dev
->bus_list
);
2195 dev
->dev
.type
= &pci_dev_type
;
2196 dev
->bus
= pci_bus_get(bus
);
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
,
2212 if (!pci_bus_crs_vendor_id(*l
))
2213 return true; /* not a CRS completion */
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);
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);
2239 if (pci_bus_read_config_dword(bus
, devfn
, PCI_VENDOR_ID
, l
))
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);
2251 bool pci_bus_generic_read_dev_vendor_id(struct pci_bus
*bus
, int devfn
, u32
*l
,
2254 if (pci_bus_read_config_dword(bus
, devfn
, PCI_VENDOR_ID
, l
))
2257 /* Some broken boards return 0 or ~0 if a slot is empty: */
2258 if (*l
== 0xffffffff || *l
== 0x00000000 ||
2259 *l
== 0x0000ffff || *l
== 0xffff0000)
2262 if (pci_bus_crs_vendor_id(*l
))
2263 return pci_bus_wait_crs(bus
, devfn
, l
, timeout
);
2268 bool pci_bus_read_dev_vendor_id(struct pci_bus
*bus
, int devfn
, u32
*l
,
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
);
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
;
2296 if (!pci_bus_read_dev_vendor_id(bus
, devfn
, &l
, 60*1000))
2299 dev
= pci_alloc_dev(bus
);
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
);
2318 static void pcie_report_downtraining(struct pci_dev
*dev
)
2320 if (!pci_is_pcie(dev
))
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
))
2329 /* Multi-function PCIe devices share the same link/status */
2330 if (PCI_FUNC(dev
->devfn
) != 0 || dev
->is_virtfn
)
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 */
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 */
2351 /* Vital Product Data */
2354 /* Alternative Routing-ID Forwarding */
2355 pci_configure_ari(dev
);
2357 /* Single Root I/O Virtualization */
2360 /* Address Translation Services */
2363 /* Enable ACS P2P upstream forwarding */
2364 pci_enable_acs(dev
);
2366 /* Precision Time Measurement */
2369 /* Advanced Error Reporting */
2372 pcie_report_downtraining(dev
);
2374 if (pci_probe_reset_function(dev
) == 0)
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
);
2396 * Let's see if we have a firmware interface able to provide
2399 d
= pci_msi_get_device_domain(dev
);
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
);
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
)
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
);
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
);
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
);
2481 dev
= pci_scan_device(bus
, devfn
);
2485 pci_device_add(dev
, bus
);
2489 EXPORT_SYMBOL(pci_scan_single_device
);
2491 static unsigned next_fn(struct pci_bus
*bus
, struct pci_dev
*dev
, unsigned fn
)
2497 if (pci_ari_enabled(bus
)) {
2500 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ARI
);
2504 pci_read_config_word(dev
, pos
+ PCI_ARI_CAP
, &cap
);
2505 next_fn
= PCI_ARI_CAP_NFN(cap
);
2507 return 0; /* protect against malformed list */
2512 /* dev may be NULL for non-contiguous multifunction devices */
2513 if (!dev
|| dev
->multifunction
)
2514 return (fn
+ 1) % 8;
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
))
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
)
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
);
2567 if (!pci_dev_is_added(dev
))
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
);
2573 if (!pci_dev_is_added(dev
))
2575 dev
->multifunction
= 1;
2579 /* Only one slot has PCIe device */
2580 if (bus
->self
&& nr
)
2581 pcie_aspm_init_link_state(bus
->self
);
2585 EXPORT_SYMBOL(pci_scan_slot
);
2587 static int pcie_find_smpss(struct pci_dev
*dev
, void *data
)
2591 if (!pci_is_pcie(dev
))
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
)
2613 if (*smpss
> dev
->pcie_mpss
)
2614 *smpss
= dev
->pcie_mpss
;
2619 static void pcie_write_mps(struct pci_dev
*dev
, int mps
)
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
&&
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
);
2647 pci_err(dev
, "Failed attempting to set the MPS\n");
2650 static void pcie_write_mrrs(struct pci_dev
*dev
)
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
)
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
);
2680 pci_warn(dev
, "Failed attempting to set the MRRS\n");
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
)
2692 if (!pci_is_pcie(dev
))
2695 if (pcie_bus_config
== PCIE_BUS_TUNE_OFF
||
2696 pcie_bus_config
== PCIE_BUS_DEFAULT
)
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
));
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
2717 void pcie_bus_configure_settings(struct pci_bus
*bus
)
2724 if (!pci_is_pcie(bus
->self
))
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
)
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
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
;
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
);
2792 dev
->multifunction
= 1;
2797 /* Reserve buses for SR-IOV capability */
2798 used_buses
= pci_iov_bus_range(bus
);
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
);
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
)
2824 * Scan bridges that are already configured. We don't touch them
2825 * unless they are misconfigured (which will be done in the second
2828 for_each_pci_bridge(dev
, bus
) {
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.
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
2853 buses
= available_buses
;
2854 } else if (dev
->is_hotplug_bridge
) {
2857 * Distribute the extra buses between hotplug
2860 buses
= available_buses
/ hotplug_bridges
;
2861 buses
= min(buses
, available_buses
- used_buses
+ 1);
2865 max
= pci_scan_bridge_extend(bus
, dev
, cmax
, buses
, 1);
2866 /* One bus is already accounted so don't add it again */
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
2896 * Return how far we've got finding sub-buses.
2898 dev_dbg(&bus
->dev
, "bus scan returning with max=%02x\n", 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
)
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
)
2939 struct pci_host_bridge
*bridge
;
2941 bridge
= pci_alloc_host_bridge(0);
2945 bridge
->dev
.parent
= parent
;
2947 list_splice_init(resources
, &bridge
->windows
);
2948 bridge
->sysdata
= sysdata
;
2949 bridge
->busnr
= bus
;
2952 error
= pci_register_host_bridge(bridge
);
2962 EXPORT_SYMBOL_GPL(pci_create_root_bus
);
2964 int pci_host_probe(struct pci_host_bridge
*bridge
)
2966 struct pci_bus
*bus
, *child
;
2969 ret
= pci_scan_root_bus_bridge(bridge
);
2971 dev_err(bridge
->dev
.parent
, "Scanning root bridge failed");
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
);
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
);
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
;
3004 res
->flags
= IORESOURCE_BUS
;
3006 if (!pci_is_root_bus(b
))
3007 parent_res
= &b
->parent
->busn_res
;
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
);
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
;
3031 if (res
->start
> bus_max
)
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
);
3046 void pci_bus_release_busn_res(struct pci_bus
*b
)
3048 struct resource
*res
= &b
->busn_res
;
3051 if (!res
->flags
|| !res
->parent
)
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
;
3070 resource_list_for_each_entry(window
, &bridge
->windows
)
3071 if (window
->res
->flags
& IORESOURCE_BUS
) {
3076 ret
= pci_register_host_bridge(bridge
);
3081 bus
= bridge
->busnr
;
3085 "No busn resource found for root bus, will use [bus %02x-ff]\n",
3087 pci_bus_insert_busn_res(b
, bus
, 255);
3090 max
= pci_scan_child_bus(b
);
3093 pci_bus_update_busn_res_end(b
, max
);
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
;
3107 resource_list_for_each_entry(window
, resources
)
3108 if (window
->res
->flags
& IORESOURCE_BUS
) {
3113 b
= pci_create_root_bus(parent
, bus
, ops
, sysdata
, resources
);
3119 "No busn resource found for root bus, will use [bus %02x-ff]\n",
3121 pci_bus_insert_busn_res(b
, bus
, 255);
3124 max
= pci_scan_child_bus(b
);
3127 pci_bus_update_busn_res_end(b
, max
);
3131 EXPORT_SYMBOL(pci_scan_root_bus
);
3133 struct pci_bus
*pci_scan_bus(int bus
, struct pci_ops
*ops
,
3136 LIST_HEAD(resources
);
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
);
3144 pci_scan_child_bus(b
);
3146 pci_free_resource_list(&resources
);
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
)
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
);
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,
3184 * Returns the max number of subordinate bus discovered.
3186 unsigned int pci_rescan_bus(struct pci_bus
*bus
)
3190 max
= pci_scan_child_bus(bus
);
3191 pci_assign_unassigned_bus_resources(bus
);
3192 pci_bus_add_devices(bus
);
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;
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
))
3250 if (busnr
-- > end
) {
3251 pci_err(dev
, "No bus number available for hot-added bridge\n");
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
)
3272 EXPORT_SYMBOL_GPL(pci_hp_add_bridge
);