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 3796bde06aSSam Ravnborg static void pbus_assign_resources_sorted(struct pci_bus *bus) 381da177e4SLinus Torvalds { 391da177e4SLinus Torvalds struct pci_dev *dev; 401da177e4SLinus Torvalds struct resource *res; 411da177e4SLinus Torvalds struct resource_list head, *list, *tmp; 421da177e4SLinus Torvalds int idx; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds head.next = NULL; 451da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 461da177e4SLinus Torvalds u16 class = dev->class >> 8; 471da177e4SLinus Torvalds 489bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 491da177e4SLinus Torvalds if (class == PCI_CLASS_NOT_DEFINED || 5023186279SSatoru Takeuchi class == PCI_CLASS_BRIDGE_HOST) 511da177e4SLinus Torvalds continue; 521da177e4SLinus Torvalds 539bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 5423186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 559bded00bSKenji Kaneshige u16 command; 569bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 579bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 5823186279SSatoru Takeuchi continue; 5923186279SSatoru Takeuchi } 6023186279SSatoru Takeuchi 611da177e4SLinus Torvalds pdev_sort_resources(dev, &head); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds for (list = head.next; list;) { 651da177e4SLinus Torvalds res = list->res; 661da177e4SLinus Torvalds idx = res - &list->dev->resource[0]; 67542df5deSRajesh Shah if (pci_assign_resource(list->dev, idx)) { 6888452565SIvan Kokshaysky /* FIXME: get rid of this */ 69542df5deSRajesh Shah res->start = 0; 70960b8466SIvan Kokshaysky res->end = 0; 71542df5deSRajesh Shah res->flags = 0; 72542df5deSRajesh Shah } 731da177e4SLinus Torvalds tmp = list; 741da177e4SLinus Torvalds list = list->next; 751da177e4SLinus Torvalds kfree(tmp); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 79b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 801da177e4SLinus Torvalds { 811da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 821da177e4SLinus Torvalds struct pci_bus_region region; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds printk("PCI: Bus %d, cardbus bridge: %s\n", 851da177e4SLinus Torvalds bus->number, pci_name(bridge)); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 881da177e4SLinus Torvalds if (bus->resource[0]->flags & IORESOURCE_IO) { 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 911da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 921da177e4SLinus Torvalds */ 93c40a22e0SBenjamin Herrenschmidt printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n", 94c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 95c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 961da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 971da177e4SLinus Torvalds region.start); 981da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 991da177e4SLinus Torvalds region.end); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 1031da177e4SLinus Torvalds if (bus->resource[1]->flags & IORESOURCE_IO) { 104c40a22e0SBenjamin Herrenschmidt printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n", 105c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 106c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1071da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 1081da177e4SLinus Torvalds region.start); 1091da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 1101da177e4SLinus Torvalds region.end); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 1141da177e4SLinus Torvalds if (bus->resource[2]->flags & IORESOURCE_MEM) { 115c40a22e0SBenjamin Herrenschmidt printk(KERN_INFO " PREFETCH window: 0x%08lx-0x%08lx\n", 116c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 117c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1181da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 1191da177e4SLinus Torvalds region.start); 1201da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 1211da177e4SLinus Torvalds region.end); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[3]); 1251da177e4SLinus Torvalds if (bus->resource[3]->flags & IORESOURCE_MEM) { 126c40a22e0SBenjamin Herrenschmidt printk(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n", 127c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 128c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1291da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 1301da177e4SLinus Torvalds region.start); 1311da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 1321da177e4SLinus Torvalds region.end); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds } 135b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 1381da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 1391da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 1401da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 1411da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 1441da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 1451da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 1461da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 1471da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 148a391f197SAdrian Bunk static void pci_setup_bridge(struct pci_bus *bus) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 1511da177e4SLinus Torvalds struct pci_bus_region region; 152c40a22e0SBenjamin Herrenschmidt u32 l, bu, lu, io_upper16; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge)); 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 1571da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 1581da177e4SLinus Torvalds if (bus->resource[0]->flags & IORESOURCE_IO) { 1591da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_IO_BASE, &l); 1601da177e4SLinus Torvalds l &= 0xffff0000; 1611da177e4SLinus Torvalds l |= (region.start >> 8) & 0x00f0; 1621da177e4SLinus Torvalds l |= region.end & 0xf000; 1631da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 1641da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 1651da177e4SLinus Torvalds DBG(KERN_INFO " IO window: %04lx-%04lx\n", 166c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 167c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds else { 1701da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 1711da177e4SLinus Torvalds io_upper16 = 0; 1721da177e4SLinus Torvalds l = 0x00f0; 1731da177e4SLinus Torvalds DBG(KERN_INFO " IO window: disabled.\n"); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 1761da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 1771da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 1781da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE, l); 1791da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 1801da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* Set up the top and bottom of the PCI Memory segment 1831da177e4SLinus Torvalds for this bus. */ 1841da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 1851da177e4SLinus Torvalds if (bus->resource[1]->flags & IORESOURCE_MEM) { 1861da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 1871da177e4SLinus Torvalds l |= region.end & 0xfff00000; 188c40a22e0SBenjamin Herrenschmidt DBG(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n", 189c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 190c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds else { 1931da177e4SLinus Torvalds l = 0x0000fff0; 1941da177e4SLinus Torvalds DBG(KERN_INFO " MEM window: disabled.\n"); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 1991da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 2001da177e4SLinus Torvalds disables PREF range, which is ok. */ 2011da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* Set up PREF base/limit. */ 204c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 2051da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 2061da177e4SLinus Torvalds if (bus->resource[2]->flags & IORESOURCE_PREFETCH) { 2071da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 2081da177e4SLinus Torvalds l |= region.end & 0xfff00000; 20913d36c24SAndrew Morton bu = upper_32_bits(region.start); 21013d36c24SAndrew Morton lu = upper_32_bits(region.end); 211c40a22e0SBenjamin Herrenschmidt DBG(KERN_INFO " PREFETCH window: 0x%016llx-0x%016llx\n", 212c40a22e0SBenjamin Herrenschmidt (unsigned long long)region.start, 213c40a22e0SBenjamin Herrenschmidt (unsigned long long)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 221c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 222c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 223c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 2291da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 2301da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 23196bde06aSSam Ravnborg static void 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). */ 26996bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds int i; 2721da177e4SLinus Torvalds struct resource *r; 2731da177e4SLinus Torvalds unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 2741da177e4SLinus Torvalds IORESOURCE_PREFETCH; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { 2771da177e4SLinus Torvalds r = bus->resource[i]; 278299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 279299de034SIvan Kokshaysky continue; 2801da177e4SLinus Torvalds if (r && (r->flags & type_mask) == type && !r->parent) 2811da177e4SLinus Torvalds return r; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds return NULL; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* Sizing the IO windows of the PCI-PCI bridge is trivial, 2871da177e4SLinus Torvalds since these windows have 4K granularity and the IO ranges 2881da177e4SLinus Torvalds of non-bridge PCI devices are limited to 256 bytes. 2891da177e4SLinus Torvalds We must be careful with the ISA aliasing though. */ 29096bde06aSSam Ravnborg static void pbus_size_io(struct pci_bus *bus) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct pci_dev *dev; 2931da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 2941da177e4SLinus Torvalds unsigned long size = 0, size1 = 0; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds if (!b_res) 2971da177e4SLinus Torvalds return; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 3001da177e4SLinus Torvalds int i; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 3031da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 3041da177e4SLinus Torvalds unsigned long r_size; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 3071da177e4SLinus Torvalds continue; 3081da177e4SLinus Torvalds r_size = r->end - r->start + 1; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds if (r_size < 0x400) 3111da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 3121da177e4SLinus Torvalds size += r_size; 3131da177e4SLinus Torvalds else 3141da177e4SLinus Torvalds size1 += r_size; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds /* To be fixed in 2.5: we should have sort of HAVE_ISA 3181da177e4SLinus Torvalds flag in the struct pci_bus. */ 3191da177e4SLinus Torvalds #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 3201da177e4SLinus Torvalds size = (size & 0xff) + ((size & ~0xffUL) << 2); 3211da177e4SLinus Torvalds #endif 3226f6f8c2fSMilind Arun Choudhary size = ALIGN(size + size1, 4096); 3231da177e4SLinus Torvalds if (!size) { 3241da177e4SLinus Torvalds b_res->flags = 0; 3251da177e4SLinus Torvalds return; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds /* Alignment of the IO window is always 4K */ 3281da177e4SLinus Torvalds b_res->start = 4096; 3291da177e4SLinus Torvalds b_res->end = b_res->start + size - 1; 33088452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* Calculate the size of the bus and minimal alignment which 3341da177e4SLinus Torvalds guarantees that all child resources fit in this size. */ 33596bde06aSSam Ravnborg static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds struct pci_dev *dev; 338c40a22e0SBenjamin Herrenschmidt resource_size_t min_align, align, size; 339c40a22e0SBenjamin Herrenschmidt resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 3401da177e4SLinus Torvalds int order, max_order; 3411da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, type); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds if (!b_res) 3441da177e4SLinus Torvalds return 0; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 3471da177e4SLinus Torvalds max_order = 0; 3481da177e4SLinus Torvalds size = 0; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 3511da177e4SLinus Torvalds int i; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 3541da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 355c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds if (r->parent || (r->flags & mask) != type) 3581da177e4SLinus Torvalds continue; 3591da177e4SLinus Torvalds r_size = r->end - r->start + 1; 3601da177e4SLinus Torvalds /* For bridges size != alignment */ 3611da177e4SLinus Torvalds align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start; 3621da177e4SLinus Torvalds order = __ffs(align) - 20; 3631da177e4SLinus Torvalds if (order > 11) { 3641da177e4SLinus Torvalds printk(KERN_WARNING "PCI: region %s/%d " 365c40a22e0SBenjamin Herrenschmidt "too large: 0x%016llx-0x%016llx\n", 3661396a8c3SGreg Kroah-Hartman pci_name(dev), i, 3671396a8c3SGreg Kroah-Hartman (unsigned long long)r->start, 3681396a8c3SGreg Kroah-Hartman (unsigned long long)r->end); 3691da177e4SLinus Torvalds r->flags = 0; 3701da177e4SLinus Torvalds continue; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds size += r_size; 3731da177e4SLinus Torvalds if (order < 0) 3741da177e4SLinus Torvalds order = 0; 3751da177e4SLinus Torvalds /* Exclude ranges with size > align from 3761da177e4SLinus Torvalds calculation of the alignment. */ 3771da177e4SLinus Torvalds if (r_size == align) 3781da177e4SLinus Torvalds aligns[order] += align; 3791da177e4SLinus Torvalds if (order > max_order) 3801da177e4SLinus Torvalds max_order = order; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds align = 0; 3851da177e4SLinus Torvalds min_align = 0; 3861da177e4SLinus Torvalds for (order = 0; order <= max_order; order++) { 387c40a22e0SBenjamin Herrenschmidt #ifdef CONFIG_RESOURCES_64BIT 388c40a22e0SBenjamin Herrenschmidt resource_size_t align1 = 1ULL << (order + 20); 389c40a22e0SBenjamin Herrenschmidt #else 390c40a22e0SBenjamin Herrenschmidt resource_size_t align1 = 1U << (order + 20); 391c40a22e0SBenjamin Herrenschmidt #endif 3921da177e4SLinus Torvalds if (!align) 3931da177e4SLinus Torvalds min_align = align1; 3946f6f8c2fSMilind Arun Choudhary else if (ALIGN(align + min_align, min_align) < align1) 3951da177e4SLinus Torvalds min_align = align1 >> 1; 3961da177e4SLinus Torvalds align += aligns[order]; 3971da177e4SLinus Torvalds } 3986f6f8c2fSMilind Arun Choudhary size = ALIGN(size, min_align); 3991da177e4SLinus Torvalds if (!size) { 4001da177e4SLinus Torvalds b_res->flags = 0; 4011da177e4SLinus Torvalds return 1; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds b_res->start = min_align; 4041da177e4SLinus Torvalds b_res->end = size + min_align - 1; 40588452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 4061da177e4SLinus Torvalds return 1; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4095468ae61SAdrian Bunk static void pci_bus_size_cardbus(struct pci_bus *bus) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 4121da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 4131da177e4SLinus Torvalds u16 ctrl; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 4171da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 4181da177e4SLinus Torvalds */ 419934b7024SLinus Torvalds b_res[0].start = 0; 420934b7024SLinus Torvalds b_res[0].end = pci_cardbus_io_size - 1; 421934b7024SLinus Torvalds b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 4221da177e4SLinus Torvalds 423934b7024SLinus Torvalds b_res[1].start = 0; 424934b7024SLinus Torvalds b_res[1].end = pci_cardbus_io_size - 1; 425934b7024SLinus Torvalds b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* 4281da177e4SLinus Torvalds * Check whether prefetchable memory is supported 4291da177e4SLinus Torvalds * by this bridge. 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 4321da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 4331da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 4341da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 4351da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* 4391da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 4401da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 4411da177e4SLinus Torvalds * twice the size. 4421da177e4SLinus Torvalds */ 4431da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 444934b7024SLinus Torvalds b_res[2].start = 0; 445934b7024SLinus Torvalds b_res[2].end = pci_cardbus_mem_size - 1; 446934b7024SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 4471da177e4SLinus Torvalds 448934b7024SLinus Torvalds b_res[3].start = 0; 449934b7024SLinus Torvalds b_res[3].end = pci_cardbus_mem_size - 1; 450934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 4511da177e4SLinus Torvalds } else { 452934b7024SLinus Torvalds b_res[3].start = 0; 453934b7024SLinus Torvalds b_res[3].end = pci_cardbus_mem_size * 2 - 1; 454934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458451124a7SSam Ravnborg void __ref 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 508451124a7SSam Ravnborg void __ref pci_bus_assign_resources(struct pci_bus *bus) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds struct pci_bus *b; 5111da177e4SLinus Torvalds struct pci_dev *dev; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds pbus_assign_resources_sorted(bus); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 5161da177e4SLinus Torvalds b = dev->subordinate; 5171da177e4SLinus Torvalds if (!b) 5181da177e4SLinus Torvalds continue; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds pci_bus_assign_resources(b); 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds switch (dev->class >> 8) { 5231da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 5241da177e4SLinus Torvalds pci_setup_bridge(b); 5251da177e4SLinus Torvalds break; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 5281da177e4SLinus Torvalds pci_setup_cardbus(b); 5291da177e4SLinus Torvalds break; 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds default: 5321da177e4SLinus Torvalds printk(KERN_INFO "PCI: not setting up bridge %s " 5331da177e4SLinus Torvalds "for bus %d\n", pci_name(dev), b->number); 5341da177e4SLinus Torvalds break; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 5391da177e4SLinus Torvalds 540*76fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 541*76fbc263SYinghai Lu { 542*76fbc263SYinghai Lu int i; 543*76fbc263SYinghai Lu 544*76fbc263SYinghai Lu for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { 545*76fbc263SYinghai Lu struct resource *res = bus->resource[i]; 546*76fbc263SYinghai Lu if (!res) 547*76fbc263SYinghai Lu continue; 548*76fbc263SYinghai Lu 549*76fbc263SYinghai Lu printk(KERN_INFO "bus: %02x index %x %s: [%llx, %llx]\n", bus->number, i, (res->flags & IORESOURCE_IO)? "io port":"mmio", res->start, res->end); 550*76fbc263SYinghai Lu } 551*76fbc263SYinghai Lu } 552*76fbc263SYinghai Lu 553*76fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 554*76fbc263SYinghai Lu { 555*76fbc263SYinghai Lu struct pci_bus *b; 556*76fbc263SYinghai Lu struct pci_dev *dev; 557*76fbc263SYinghai Lu 558*76fbc263SYinghai Lu 559*76fbc263SYinghai Lu pci_bus_dump_res(bus); 560*76fbc263SYinghai Lu 561*76fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 562*76fbc263SYinghai Lu b = dev->subordinate; 563*76fbc263SYinghai Lu if (!b) 564*76fbc263SYinghai Lu continue; 565*76fbc263SYinghai Lu 566*76fbc263SYinghai Lu pci_bus_dump_resources(b); 567*76fbc263SYinghai Lu } 568*76fbc263SYinghai Lu } 569*76fbc263SYinghai Lu 5701da177e4SLinus Torvalds void __init 5711da177e4SLinus Torvalds pci_assign_unassigned_resources(void) 5721da177e4SLinus Torvalds { 5731da177e4SLinus Torvalds struct pci_bus *bus; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 5761da177e4SLinus Torvalds subordinate buses. */ 5771da177e4SLinus Torvalds list_for_each_entry(bus, &pci_root_buses, node) { 5781da177e4SLinus Torvalds pci_bus_size_bridges(bus); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 5811da177e4SLinus Torvalds list_for_each_entry(bus, &pci_root_buses, node) { 5821da177e4SLinus Torvalds pci_bus_assign_resources(bus); 5831da177e4SLinus Torvalds pci_enable_bridges(bus); 5841da177e4SLinus Torvalds } 585*76fbc263SYinghai Lu 586*76fbc263SYinghai Lu /* dump the resource on buses */ 587*76fbc263SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) { 588*76fbc263SYinghai Lu pci_bus_dump_resources(bus); 589*76fbc263SYinghai Lu } 5901da177e4SLinus Torvalds } 591