11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/pci/setup-bus.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Extruded from code written by 51da177e4SLinus Torvalds * Dave Rusling (david.rusling@reo.mts.dec.com) 61da177e4SLinus Torvalds * David Mosberger (davidm@cs.arizona.edu) 71da177e4SLinus Torvalds * David Miller (davem@redhat.com) 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Support routines for initializing a PCI subsystem. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 141da177e4SLinus Torvalds * PCI-PCI bridges cleanup, sorted resource allocation. 151da177e4SLinus Torvalds * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 161da177e4SLinus Torvalds * Converted to allocation in 3 passes, which gives 171da177e4SLinus Torvalds * tighter packing. Prefetchable range support. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/kernel.h> 221da177e4SLinus Torvalds #include <linux/module.h> 231da177e4SLinus Torvalds #include <linux/pci.h> 241da177e4SLinus Torvalds #include <linux/errno.h> 251da177e4SLinus Torvalds #include <linux/ioport.h> 261da177e4SLinus Torvalds #include <linux/cache.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #define DEBUG_CONFIG 1 311da177e4SLinus Torvalds #if DEBUG_CONFIG 321da177e4SLinus Torvalds #define DBG(x...) printk(x) 331da177e4SLinus Torvalds #else 341da177e4SLinus Torvalds #define DBG(x...) 351da177e4SLinus Torvalds #endif 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /* 401da177e4SLinus Torvalds * FIXME: IO should be max 256 bytes. However, since we may 411da177e4SLinus Torvalds * have a P2P bridge below a cardbus bridge, we need 4K. 421da177e4SLinus Torvalds */ 434196c3afSLinus Torvalds #define CARDBUS_IO_SIZE (256) 441da177e4SLinus Torvalds #define CARDBUS_MEM_SIZE (32*1024*1024) 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static void __devinit 471da177e4SLinus Torvalds pbus_assign_resources_sorted(struct pci_bus *bus) 481da177e4SLinus Torvalds { 491da177e4SLinus Torvalds struct pci_dev *dev; 501da177e4SLinus Torvalds struct resource *res; 511da177e4SLinus Torvalds struct resource_list head, *list, *tmp; 521da177e4SLinus Torvalds int idx; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds head.next = NULL; 551da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 561da177e4SLinus Torvalds u16 class = dev->class >> 8; 571da177e4SLinus Torvalds 58*9bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 591da177e4SLinus Torvalds if (class == PCI_CLASS_NOT_DEFINED || 6023186279SSatoru Takeuchi class == PCI_CLASS_BRIDGE_HOST) 611da177e4SLinus Torvalds continue; 621da177e4SLinus Torvalds 63*9bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 6423186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 65*9bded00bSKenji Kaneshige u16 command; 66*9bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 67*9bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 6823186279SSatoru Takeuchi continue; 6923186279SSatoru Takeuchi } 7023186279SSatoru Takeuchi 711da177e4SLinus Torvalds pdev_sort_resources(dev, &head); 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds for (list = head.next; list;) { 751da177e4SLinus Torvalds res = list->res; 761da177e4SLinus Torvalds idx = res - &list->dev->resource[0]; 77542df5deSRajesh Shah if (pci_assign_resource(list->dev, idx)) { 78542df5deSRajesh Shah res->start = 0; 79960b8466SIvan Kokshaysky res->end = 0; 80542df5deSRajesh Shah res->flags = 0; 81542df5deSRajesh Shah } 821da177e4SLinus Torvalds tmp = list; 831da177e4SLinus Torvalds list = list->next; 841da177e4SLinus Torvalds kfree(tmp); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 88b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 911da177e4SLinus Torvalds struct pci_bus_region region; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds printk("PCI: Bus %d, cardbus bridge: %s\n", 941da177e4SLinus Torvalds bus->number, pci_name(bridge)); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 971da177e4SLinus Torvalds if (bus->resource[0]->flags & IORESOURCE_IO) { 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 1001da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds printk(" IO window: %08lx-%08lx\n", 1031da177e4SLinus Torvalds region.start, region.end); 1041da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 1051da177e4SLinus Torvalds region.start); 1061da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 1071da177e4SLinus Torvalds region.end); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 1111da177e4SLinus Torvalds if (bus->resource[1]->flags & IORESOURCE_IO) { 1121da177e4SLinus Torvalds printk(" IO window: %08lx-%08lx\n", 1131da177e4SLinus Torvalds region.start, region.end); 1141da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 1151da177e4SLinus Torvalds region.start); 1161da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 1171da177e4SLinus Torvalds region.end); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 1211da177e4SLinus Torvalds if (bus->resource[2]->flags & IORESOURCE_MEM) { 1221da177e4SLinus Torvalds printk(" PREFETCH window: %08lx-%08lx\n", 1231da177e4SLinus Torvalds region.start, region.end); 1241da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 1251da177e4SLinus Torvalds region.start); 1261da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 1271da177e4SLinus Torvalds region.end); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[3]); 1311da177e4SLinus Torvalds if (bus->resource[3]->flags & IORESOURCE_MEM) { 1321da177e4SLinus Torvalds printk(" MEM window: %08lx-%08lx\n", 1331da177e4SLinus Torvalds region.start, region.end); 1341da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 1351da177e4SLinus Torvalds region.start); 1361da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 1371da177e4SLinus Torvalds region.end); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds } 140b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 1431da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 1441da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 1451da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 1461da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 1491da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 1501da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 1511da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 1521da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 1531da177e4SLinus Torvalds static void __devinit 1541da177e4SLinus Torvalds pci_setup_bridge(struct pci_bus *bus) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 1571da177e4SLinus Torvalds struct pci_bus_region region; 1581da177e4SLinus Torvalds u32 l, io_upper16; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge)); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 1631da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 1641da177e4SLinus Torvalds if (bus->resource[0]->flags & IORESOURCE_IO) { 1651da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_IO_BASE, &l); 1661da177e4SLinus Torvalds l &= 0xffff0000; 1671da177e4SLinus Torvalds l |= (region.start >> 8) & 0x00f0; 1681da177e4SLinus Torvalds l |= region.end & 0xf000; 1691da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 1701da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 1711da177e4SLinus Torvalds DBG(KERN_INFO " IO window: %04lx-%04lx\n", 1721da177e4SLinus Torvalds region.start, region.end); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds else { 1751da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 1761da177e4SLinus Torvalds io_upper16 = 0; 1771da177e4SLinus Torvalds l = 0x00f0; 1781da177e4SLinus Torvalds DBG(KERN_INFO " IO window: disabled.\n"); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 1811da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 1821da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 1831da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE, l); 1841da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 1851da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* Set up the top and bottom of the PCI Memory segment 1881da177e4SLinus Torvalds for this bus. */ 1891da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 1901da177e4SLinus Torvalds if (bus->resource[1]->flags & IORESOURCE_MEM) { 1911da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 1921da177e4SLinus Torvalds l |= region.end & 0xfff00000; 1931da177e4SLinus Torvalds DBG(KERN_INFO " MEM window: %08lx-%08lx\n", 1941da177e4SLinus Torvalds region.start, region.end); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds else { 1971da177e4SLinus Torvalds l = 0x0000fff0; 1981da177e4SLinus Torvalds DBG(KERN_INFO " MEM window: disabled.\n"); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 2031da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 2041da177e4SLinus Torvalds disables PREF range, which is ok. */ 2051da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /* Set up PREF base/limit. */ 2081da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 2091da177e4SLinus Torvalds if (bus->resource[2]->flags & IORESOURCE_PREFETCH) { 2101da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 2111da177e4SLinus Torvalds l |= region.end & 0xfff00000; 2121da177e4SLinus Torvalds DBG(KERN_INFO " PREFETCH window: %08lx-%08lx\n", 2131da177e4SLinus Torvalds region.start, region.end); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds else { 2161da177e4SLinus Torvalds l = 0x0000fff0; 2171da177e4SLinus Torvalds DBG(KERN_INFO " PREFETCH window: disabled.\n"); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF base. */ 2221da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0); 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 2281da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 2291da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 2301da177e4SLinus Torvalds static void __devinit 2311da177e4SLinus Torvalds pci_bridge_check_ranges(struct pci_bus *bus) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds u16 io; 2341da177e4SLinus Torvalds u32 pmem; 2351da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 2361da177e4SLinus Torvalds struct resource *b_res; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 2391da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 2421da177e4SLinus Torvalds if (!io) { 2431da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 2441da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 2451da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds if (io) 2481da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 2491da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 2501da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 2511da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 2521da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 2531da177e4SLinus Torvalds return; 2541da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 2551da177e4SLinus Torvalds if (!pmem) { 2561da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 2571da177e4SLinus Torvalds 0xfff0fff0); 2581da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 2591da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds if (pmem) 2621da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 2661da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 2671da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 2681da177e4SLinus Torvalds have non-NULL parent resource). */ 2691da177e4SLinus Torvalds static struct resource * __devinit 2701da177e4SLinus Torvalds find_free_bus_resource(struct pci_bus *bus, unsigned long type) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds int i; 2731da177e4SLinus Torvalds struct resource *r; 2741da177e4SLinus Torvalds unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 2751da177e4SLinus Torvalds IORESOURCE_PREFETCH; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { 2781da177e4SLinus Torvalds r = bus->resource[i]; 279299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 280299de034SIvan Kokshaysky continue; 2811da177e4SLinus Torvalds if (r && (r->flags & type_mask) == type && !r->parent) 2821da177e4SLinus Torvalds return r; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds return NULL; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* Sizing the IO windows of the PCI-PCI bridge is trivial, 2881da177e4SLinus Torvalds since these windows have 4K granularity and the IO ranges 2891da177e4SLinus Torvalds of non-bridge PCI devices are limited to 256 bytes. 2901da177e4SLinus Torvalds We must be careful with the ISA aliasing though. */ 2911da177e4SLinus Torvalds static void __devinit 2921da177e4SLinus Torvalds pbus_size_io(struct pci_bus *bus) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds struct pci_dev *dev; 2951da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 2961da177e4SLinus Torvalds unsigned long size = 0, size1 = 0; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds if (!b_res) 2991da177e4SLinus Torvalds return; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 3021da177e4SLinus Torvalds int i; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 3051da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 3061da177e4SLinus Torvalds unsigned long r_size; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 3091da177e4SLinus Torvalds continue; 3101da177e4SLinus Torvalds r_size = r->end - r->start + 1; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds if (r_size < 0x400) 3131da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 3141da177e4SLinus Torvalds size += r_size; 3151da177e4SLinus Torvalds else 3161da177e4SLinus Torvalds size1 += r_size; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds /* To be fixed in 2.5: we should have sort of HAVE_ISA 3201da177e4SLinus Torvalds flag in the struct pci_bus. */ 3211da177e4SLinus Torvalds #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 3221da177e4SLinus Torvalds size = (size & 0xff) + ((size & ~0xffUL) << 2); 3231da177e4SLinus Torvalds #endif 3241da177e4SLinus Torvalds size = ROUND_UP(size + size1, 4096); 3251da177e4SLinus Torvalds if (!size) { 3261da177e4SLinus Torvalds b_res->flags = 0; 3271da177e4SLinus Torvalds return; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds /* Alignment of the IO window is always 4K */ 3301da177e4SLinus Torvalds b_res->start = 4096; 3311da177e4SLinus Torvalds b_res->end = b_res->start + size - 1; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* Calculate the size of the bus and minimal alignment which 3351da177e4SLinus Torvalds guarantees that all child resources fit in this size. */ 3361da177e4SLinus Torvalds static int __devinit 3371da177e4SLinus Torvalds pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct pci_dev *dev; 3401da177e4SLinus Torvalds unsigned long min_align, align, size; 3411da177e4SLinus Torvalds unsigned long aligns[12]; /* Alignments from 1Mb to 2Gb */ 3421da177e4SLinus Torvalds int order, max_order; 3431da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, type); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (!b_res) 3461da177e4SLinus Torvalds return 0; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 3491da177e4SLinus Torvalds max_order = 0; 3501da177e4SLinus Torvalds size = 0; 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 3531da177e4SLinus Torvalds int i; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 3561da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 3571da177e4SLinus Torvalds unsigned long r_size; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds if (r->parent || (r->flags & mask) != type) 3601da177e4SLinus Torvalds continue; 3611da177e4SLinus Torvalds r_size = r->end - r->start + 1; 3621da177e4SLinus Torvalds /* For bridges size != alignment */ 3631da177e4SLinus Torvalds align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start; 3641da177e4SLinus Torvalds order = __ffs(align) - 20; 3651da177e4SLinus Torvalds if (order > 11) { 3661da177e4SLinus Torvalds printk(KERN_WARNING "PCI: region %s/%d " 3671396a8c3SGreg Kroah-Hartman "too large: %llx-%llx\n", 3681396a8c3SGreg Kroah-Hartman pci_name(dev), i, 3691396a8c3SGreg Kroah-Hartman (unsigned long long)r->start, 3701396a8c3SGreg Kroah-Hartman (unsigned long long)r->end); 3711da177e4SLinus Torvalds r->flags = 0; 3721da177e4SLinus Torvalds continue; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds size += r_size; 3751da177e4SLinus Torvalds if (order < 0) 3761da177e4SLinus Torvalds order = 0; 3771da177e4SLinus Torvalds /* Exclude ranges with size > align from 3781da177e4SLinus Torvalds calculation of the alignment. */ 3791da177e4SLinus Torvalds if (r_size == align) 3801da177e4SLinus Torvalds aligns[order] += align; 3811da177e4SLinus Torvalds if (order > max_order) 3821da177e4SLinus Torvalds max_order = order; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds align = 0; 3871da177e4SLinus Torvalds min_align = 0; 3881da177e4SLinus Torvalds for (order = 0; order <= max_order; order++) { 3891da177e4SLinus Torvalds unsigned long align1 = 1UL << (order + 20); 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if (!align) 3921da177e4SLinus Torvalds min_align = align1; 3931da177e4SLinus Torvalds else if (ROUND_UP(align + min_align, min_align) < align1) 3941da177e4SLinus Torvalds min_align = align1 >> 1; 3951da177e4SLinus Torvalds align += aligns[order]; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds size = ROUND_UP(size, min_align); 3981da177e4SLinus Torvalds if (!size) { 3991da177e4SLinus Torvalds b_res->flags = 0; 4001da177e4SLinus Torvalds return 1; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds b_res->start = min_align; 4031da177e4SLinus Torvalds b_res->end = size + min_align - 1; 4041da177e4SLinus Torvalds return 1; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds static void __devinit 4081da177e4SLinus Torvalds pci_bus_size_cardbus(struct pci_bus *bus) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 4111da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 4121da177e4SLinus Torvalds u16 ctrl; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* 4151da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 4161da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds b_res[0].start = CARDBUS_IO_SIZE; 4191da177e4SLinus Torvalds b_res[0].end = b_res[0].start + CARDBUS_IO_SIZE - 1; 4201da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds b_res[1].start = CARDBUS_IO_SIZE; 4231da177e4SLinus Torvalds b_res[1].end = b_res[1].start + CARDBUS_IO_SIZE - 1; 4241da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_IO; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* 4271da177e4SLinus Torvalds * Check whether prefetchable memory is supported 4281da177e4SLinus Torvalds * by this bridge. 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 4311da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 4321da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 4331da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 4341da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* 4381da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 4391da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 4401da177e4SLinus Torvalds * twice the size. 4411da177e4SLinus Torvalds */ 4421da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 4431da177e4SLinus Torvalds b_res[2].start = CARDBUS_MEM_SIZE; 4441da177e4SLinus Torvalds b_res[2].end = b_res[2].start + CARDBUS_MEM_SIZE - 1; 4451da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds b_res[3].start = CARDBUS_MEM_SIZE; 4481da177e4SLinus Torvalds b_res[3].end = b_res[3].start + CARDBUS_MEM_SIZE - 1; 4491da177e4SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM; 4501da177e4SLinus Torvalds } else { 4511da177e4SLinus Torvalds b_res[3].start = CARDBUS_MEM_SIZE * 2; 4521da177e4SLinus Torvalds b_res[3].end = b_res[3].start + CARDBUS_MEM_SIZE * 2 - 1; 4531da177e4SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds void __devinit 4581da177e4SLinus Torvalds pci_bus_size_bridges(struct pci_bus *bus) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct pci_dev *dev; 4611da177e4SLinus Torvalds unsigned long mask, prefmask; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 4641da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 4651da177e4SLinus Torvalds if (!b) 4661da177e4SLinus Torvalds continue; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds switch (dev->class >> 8) { 4691da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 4701da177e4SLinus Torvalds pci_bus_size_cardbus(b); 4711da177e4SLinus Torvalds break; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 4741da177e4SLinus Torvalds default: 4751da177e4SLinus Torvalds pci_bus_size_bridges(b); 4761da177e4SLinus Torvalds break; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* The root bus? */ 4811da177e4SLinus Torvalds if (!bus->self) 4821da177e4SLinus Torvalds return; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds switch (bus->self->class >> 8) { 4851da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 4861da177e4SLinus Torvalds /* don't size cardbuses yet. */ 4871da177e4SLinus Torvalds break; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 4901da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 4911da177e4SLinus Torvalds default: 4921da177e4SLinus Torvalds pbus_size_io(bus); 4931da177e4SLinus Torvalds /* If the bridge supports prefetchable range, size it 4941da177e4SLinus Torvalds separately. If it doesn't, or its prefetchable window 4951da177e4SLinus Torvalds has already been allocated by arch code, try 4961da177e4SLinus Torvalds non-prefetchable range for both types of PCI memory 4971da177e4SLinus Torvalds resources. */ 4981da177e4SLinus Torvalds mask = IORESOURCE_MEM; 4991da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 5001da177e4SLinus Torvalds if (pbus_size_mem(bus, prefmask, prefmask)) 5011da177e4SLinus Torvalds mask = prefmask; /* Success, size non-prefetch only. */ 5021da177e4SLinus Torvalds pbus_size_mem(bus, mask, IORESOURCE_MEM); 5031da177e4SLinus Torvalds break; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds void __devinit 5091da177e4SLinus Torvalds pci_bus_assign_resources(struct pci_bus *bus) 5101da177e4SLinus Torvalds { 5111da177e4SLinus Torvalds struct pci_bus *b; 5121da177e4SLinus Torvalds struct pci_dev *dev; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds pbus_assign_resources_sorted(bus); 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 5171da177e4SLinus Torvalds b = dev->subordinate; 5181da177e4SLinus Torvalds if (!b) 5191da177e4SLinus Torvalds continue; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds pci_bus_assign_resources(b); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds switch (dev->class >> 8) { 5241da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 5251da177e4SLinus Torvalds pci_setup_bridge(b); 5261da177e4SLinus Torvalds break; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 5291da177e4SLinus Torvalds pci_setup_cardbus(b); 5301da177e4SLinus Torvalds break; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds default: 5331da177e4SLinus Torvalds printk(KERN_INFO "PCI: not setting up bridge %s " 5341da177e4SLinus Torvalds "for bus %d\n", pci_name(dev), b->number); 5351da177e4SLinus Torvalds break; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds void __init 5421da177e4SLinus Torvalds pci_assign_unassigned_resources(void) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds struct pci_bus *bus; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 5471da177e4SLinus Torvalds subordinate buses. */ 5481da177e4SLinus Torvalds list_for_each_entry(bus, &pci_root_buses, node) { 5491da177e4SLinus Torvalds pci_bus_size_bridges(bus); 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 5521da177e4SLinus Torvalds list_for_each_entry(bus, &pci_root_buses, node) { 5531da177e4SLinus Torvalds pci_bus_assign_resources(bus); 5541da177e4SLinus Torvalds pci_enable_bridges(bus); 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds } 557