5 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
16 #define CARDBUS_LATENCY_TIMER 176
17 #define CARDBUS_RESERVE_BUSNR 3
30 static LIST_HEAD(pci_domain_busn_res_list);
38 static struct resource *get_pci_domain_busn_res(
int domain_nr)
76 no_devices = (dev ==
NULL);
85 static void release_pcibus_dev(
struct device *
dev)
96 static struct class pcibus_class = {
98 .dev_release = &release_pcibus_dev,
102 static int __init pcibus_class_init(
void)
116 size = (size & ~(size-1)) - 1;
120 if (base == maxbase && ((base | size) &
mask) != mask)
126 static inline unsigned long decode_bar(
struct pci_dev *dev,
u32 bar)
174 bool bar_too_big =
false, bar_disabled =
false;
185 res->
name = pci_name(dev);
187 pci_read_config_dword(dev, pos, &l);
188 pci_write_config_dword(dev, pos, l | mask);
189 pci_read_config_dword(dev, pos, &sz);
190 pci_write_config_dword(dev, pos, l);
198 if (!sz || sz == 0xffffffff)
209 res->
flags = decode_bar(dev, l);
227 u64 mask64 = mask | (
u64)~0 << 32;
229 pci_read_config_dword(dev, pos + 4, &l);
230 pci_write_config_dword(dev, pos + 4, ~0);
231 pci_read_config_dword(dev, pos + 4, &sz);
232 pci_write_config_dword(dev, pos + 4, l);
234 l64 |= ((
u64)l << 32);
235 sz64 |= ((
u64)sz << 32);
237 sz64 = pci_size(l64, sz64, mask64);
249 pci_write_config_dword(dev, pos, 0);
250 pci_write_config_dword(dev, pos + 4, 0);
257 region.
end = l64 + sz64;
261 sz = pci_size(l, sz, mask);
281 dev_err(&dev->
dev,
"reg %x: can't handle 64-bit BAR\n", pos);
282 if (res->
flags && !bar_disabled)
283 dev_printk(
KERN_DEBUG, &dev->
dev,
"reg %x: %pR\n", pos, res);
292 for (pos = 0; pos <
howmany; pos++) {
311 u8 io_base_lo, io_limit_lo;
312 unsigned long io_mask, io_granularity, base,
limit;
317 io_granularity = 0x1000;
321 io_granularity = 0x400;
325 pci_read_config_byte(dev,
PCI_IO_BASE, &io_base_lo);
327 base = (io_base_lo & io_mask) << 8;
328 limit = (io_limit_lo & io_mask) << 8;
331 u16 io_base_hi, io_limit_hi;
335 base |= ((
unsigned long) io_base_hi << 16);
336 limit |= ((
unsigned long) io_limit_hi << 16);
342 region.end = limit + io_granularity - 1;
344 dev_printk(
KERN_DEBUG, &dev->
dev,
" bridge window %pR\n", res);
348 static void __devinit pci_read_bridge_mmio(
struct pci_bus *child)
351 u16 mem_base_lo, mem_limit_lo;
352 unsigned long base,
limit;
360 limit = ((
unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
364 region.end = limit + 0xfffff;
366 dev_printk(
KERN_DEBUG, &dev->
dev,
" bridge window %pR\n", res);
370 static void __devinit pci_read_bridge_mmio_pref(
struct pci_bus *child)
373 u16 mem_base_lo, mem_limit_lo;
374 unsigned long base,
limit;
382 limit = ((
unsigned long) mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
385 u32 mem_base_hi, mem_limit_hi;
395 if (mem_base_hi <= mem_limit_hi) {
396 #if BITS_PER_LONG == 64
397 base |= ((
unsigned long) mem_base_hi) << 32;
398 limit |= ((
unsigned long) mem_limit_hi) << 32;
400 if (mem_base_hi || mem_limit_hi) {
402 "address space for bridge\n");
414 region.end = limit + 0xfffff;
416 dev_printk(
KERN_DEBUG, &dev->
dev,
" bridge window %pR\n", res);
426 if (pci_is_root_bus(child))
437 pci_read_bridge_io(child);
438 pci_read_bridge_mmio(child);
439 pci_read_bridge_mmio_pref(child);
442 pci_bus_for_each_resource(child->
parent, res, i) {
447 " bridge window %pR (subtractive decode)\n",
454 static struct pci_bus * pci_alloc_bus(
void)
460 INIT_LIST_HEAD(&b->
node);
463 INIT_LIST_HEAD(&b->
slots);
471 static struct pci_host_bridge *pci_alloc_host_bridge(
struct pci_bus *b)
475 bridge = kzalloc(
sizeof(*bridge),
GFP_KERNEL);
477 INIT_LIST_HEAD(&bridge->
windows);
484 static unsigned char pcix_bus_speed[] = {
503 static unsigned char pcie_link_speed[] = {
528 static unsigned char agp_speeds[] = {
542 else if (agpstat & 2)
544 else if (agpstat & 1)
556 return agp_speeds[
index];
560 static void pci_set_bus_speed(
struct pci_bus *
bus)
582 pci_read_config_word(bridge, pos + 2, &status);
584 if (status & 0x8000) {
586 }
else if (status & 0x4000) {
588 }
else if (status & 0x0002) {
589 if (((status >> 12) & 0x3) == 2) {
618 static struct pci_bus *pci_alloc_child_bus(
struct pci_bus *
parent,
619 struct pci_dev *bridge,
int busnr)
621 struct pci_bus *child;
627 child = pci_alloc_bus();
640 child->
dev.class = &pcibus_class;
657 pci_set_bus_speed(child);
671 struct pci_bus *child;
673 child = pci_alloc_child_bus(parent, dev, busnr);
682 static void pci_fixup_parent_subordinate_busnr(
struct pci_bus *child,
int max)
684 struct pci_bus *parent = child->
parent;
710 struct pci_bus *child;
718 primary = buses & 0xFF;
719 secondary = (buses >> 8) & 0xFF;
720 subordinate = (buses >> 16) & 0xFF;
722 dev_dbg(&dev->
dev,
"scanning [bus %02x-%02x] behind bridge, pass %d\n",
723 secondary, subordinate, pass);
725 if (!primary && (primary != bus->
number) && secondary && subordinate) {
726 dev_warn(&dev->
dev,
"Primary bus is hard wired to 0\n");
733 secondary > subordinate)) {
734 dev_info(&dev->
dev,
"bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
735 secondary, subordinate);
746 !is_cardbus && !broken) {
796 pci_write_config_word(dev,
PCI_STATUS, 0xffff);
808 buses = (buses & 0xff000000)
809 | ((
unsigned int)(child->
primary) << 0)
811 | ((
unsigned int)(child->
busn_res.end) << 16);
818 buses &= ~0xff000000;
835 pci_fixup_parent_subordinate_busnr(child, max);
842 pci_fixup_parent_subordinate_busnr(child, max);
850 struct pci_bus *parent =
bus;
873 pci_fixup_parent_subordinate_busnr(child, max);
883 (is_cardbus ?
"PCI CardBus %04x:%02x" :
"PCI Bus %04x:%02x"),
893 "hidden behind%s bridge %s %pR\n",
897 "wholly" :
"partially",
898 bus->
self->transparent ?
" transparent" :
"",
915 static void pci_read_irq(
struct pci_dev *dev)
951 #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
976 dev->
dev.parent = dev->
bus->bridge;
997 dev->
class =
class >> 8;
999 dev_printk(
KERN_DEBUG, &dev->
dev,
"[%04x:%04x] type %02x class %#08x\n",
1011 class = dev->class >> 8;
1031 if ((progif & 1) == 0) {
1032 region.
start = 0x1F0;
1037 region.
start = 0x3F6;
1043 if ((progif & 4) == 0) {
1044 region.
start = 0x170;
1049 region.
start = 0x376;
1079 pci_read_bases(dev, 1, 0);
1085 dev_err(&dev->
dev,
"unknown header type %02x, "
1086 "ignoring device\n", dev->
hdr_type);
1090 dev_err(&dev->
dev,
"ignoring class %#08x (doesn't match header "
1099 static void pci_release_capabilities(
struct pci_dev *dev)
1101 pci_vpd_release(dev);
1113 static void pci_release_dev(
struct device *dev)
1118 pci_release_capabilities(pci_dev);
1141 if (status == 0xffffffff)
1156 class = dev->class >> 8;
1160 if (!pci_is_pcie(dev)) {
1165 pci_read_config_dword(dev, pos +
PCI_X_STATUS, &status);
1176 static void pci_release_bus_bridge_dev(
struct device *dev)
1207 if (pci_bus_read_config_dword(bus, devfn,
PCI_VENDOR_ID, l))
1211 if (*l == 0xffffffff || *l == 0x00000000 ||
1212 *l == 0x0000ffff || *l == 0xffff0000)
1216 while (*l == 0xffff0001) {
1222 if (pci_bus_read_config_dword(bus, devfn,
PCI_VENDOR_ID, l))
1225 if (delay > crs_timeout) {
1242 static struct pci_dev *pci_scan_device(
struct pci_bus *bus,
int devfn)
1256 dev->
vendor = l & 0xffff;
1257 dev->
device = (l >> 16) & 0xffff;
1269 static void pci_init_capabilities(
struct pci_dev *dev)
1297 dev->
dev.release = pci_release_dev;
1302 dev->
dev.coherent_dma_mask = 0xffffffffull;
1317 pci_init_capabilities(dev);
1338 dev = pci_scan_device(bus, devfn);
1348 static unsigned next_ari_fn(
struct pci_dev *dev,
unsigned fn)
1351 unsigned pos, next_fn;
1359 pci_read_config_word(dev, pos + 4, &cap);
1366 static unsigned next_trad_fn(
struct pci_dev *dev,
unsigned fn)
1368 return (fn + 1) % 8;
1371 static unsigned no_next_fn(
struct pci_dev *dev,
unsigned fn)
1376 static int only_one_child(
struct pci_bus *bus)
1380 if (!parent || !pci_is_pcie(parent))
1385 !pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
1403 unsigned fn,
nr = 0;
1405 unsigned (*next_fn)(
struct pci_dev *, unsigned) = no_next_fn;
1407 if (only_one_child(bus) && (devfn > 0))
1416 if (pci_ari_enabled(bus))
1417 next_fn = next_ari_fn;
1419 next_fn = next_trad_fn;
1421 for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {
1431 if (bus->
self && nr)
1437 static int pcie_find_smpss(
struct pci_dev *dev,
void *
data)
1441 if (!pci_is_pcie(dev))
1470 static void pcie_write_mps(
struct pci_dev *dev,
int mps)
1496 dev_err(&dev->
dev,
"Failed attempting to set the MPS\n");
1499 static void pcie_write_mrrs(
struct pci_dev *dev)
1526 dev_warn(&dev->
dev,
"Failed attempting to set the MRRS\n");
1531 dev_err(&dev->
dev,
"MRRS was unable to be configured with a "
1532 "safe value. If problems are experienced, try running "
1533 "with pci=pcie_bus_safe.\n");
1536 static int pcie_bus_configure_set(
struct pci_dev *dev,
void *data)
1540 if (!pci_is_pcie(dev))
1543 mps = 128 << *(
u8 *)data;
1546 pcie_write_mps(dev, mps);
1547 pcie_write_mrrs(dev);
1549 dev_info(&dev->
dev,
"PCI-E Max Payload Size set to %4d/%4d (was %4d), "
1564 if (!pci_is_pcie(bus->
self))
1580 pcie_find_smpss(bus->
self, &smpss);
1584 pcie_bus_configure_set(bus->
self, &smpss);
1597 for (devfn = 0; devfn < 0x100; devfn += 8)
1610 if (pci_is_root_bus(bus))
1614 for (pass=0; pass < 2; pass++)
1628 dev_dbg(&bus->
dev,
"bus scan returning with max=%02x\n", max);
1637 struct pci_bus *
b, *b2;
1645 b = pci_alloc_bus();
1658 bridge = pci_alloc_host_bridge(b);
1663 bridge->
dev.release = pci_release_bus_bridge_dev;
1667 goto bridge_dev_reg_err;
1669 device_enable_async_suspend(b->
bridge);
1675 b->
dev.class = &pcibus_class;
1680 goto class_dev_reg_err;
1683 pci_create_legacy_files(b);
1688 dev_info(parent,
"PCI host bridge to bus %s\n", dev_name(&b->
dev));
1703 fmt =
" (bus address [%#06llx-%#06llx])";
1705 fmt =
" (bus address [%#010llx-%#010llx])";
1706 snprintf(bus_addr,
sizeof(bus_addr), fmt,
1707 (
unsigned long long) (res->
start - offset),
1708 (
unsigned long long) (res->
end - offset));
1711 dev_info(&b->
dev,
"root bus resource %pR%s\n", res, bus_addr);
1733 struct resource *parent_res, *conflict;
1739 if (!pci_is_root_bus(b))
1740 parent_res = &b->
parent->busn_res;
1750 "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
1751 res, pci_is_root_bus(b) ?
"domain " :
"",
1752 parent_res, conflict->
name, conflict);
1754 return conflict ==
NULL;
1764 if (res->
start > bus_max)
1767 size = bus_max - res->
start + 1;
1770 "busn_res: %pR end %s updated to %02x\n",
1771 &old_res, ret ?
"can not be" :
"is", bus_max);
1773 if (!ret && !res->
parent)
1789 "busn_res: %pR %s released\n",
1790 res, ret ?
"can not be" :
"is");
1813 "No busn resource found for root bus, will use [bus %02x-ff]\n",
1867 #ifdef CONFIG_HOTPLUG
1879 unsigned int __ref pci_rescan_bus_bridge_resize(
struct pci_dev *bridge)
1899 static int __init pci_sort_bf_cmp(
const struct device *d_a,
const struct device *d_b)
1907 if (a->
bus->number < b->
bus->number)
return -1;
1908 else if (a->
bus->number > b->
bus->number)
return 1;