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> 286faf17f6SChris Wright #include "pci.h" 291da177e4SLinus Torvalds 30ea741551SAndrew Morton static void pbus_assign_resources_sorted(const struct pci_bus *bus) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds struct pci_dev *dev; 331da177e4SLinus Torvalds struct resource *res; 341da177e4SLinus Torvalds struct resource_list head, *list, *tmp; 351da177e4SLinus Torvalds int idx; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds head.next = NULL; 381da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 391da177e4SLinus Torvalds u16 class = dev->class >> 8; 401da177e4SLinus Torvalds 419bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 421da177e4SLinus Torvalds if (class == PCI_CLASS_NOT_DEFINED || 4323186279SSatoru Takeuchi class == PCI_CLASS_BRIDGE_HOST) 441da177e4SLinus Torvalds continue; 451da177e4SLinus Torvalds 469bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 4723186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 489bded00bSKenji Kaneshige u16 command; 499bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 509bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 5123186279SSatoru Takeuchi continue; 5223186279SSatoru Takeuchi } 5323186279SSatoru Takeuchi 541da177e4SLinus Torvalds pdev_sort_resources(dev, &head); 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds for (list = head.next; list;) { 581da177e4SLinus Torvalds res = list->res; 591da177e4SLinus Torvalds idx = res - &list->dev->resource[0]; 60542df5deSRajesh Shah if (pci_assign_resource(list->dev, idx)) { 61542df5deSRajesh Shah res->start = 0; 62960b8466SIvan Kokshaysky res->end = 0; 63542df5deSRajesh Shah res->flags = 0; 64542df5deSRajesh Shah } 651da177e4SLinus Torvalds tmp = list; 661da177e4SLinus Torvalds list = list->next; 671da177e4SLinus Torvalds kfree(tmp); 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 71b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 741da177e4SLinus Torvalds struct pci_bus_region region; 751da177e4SLinus Torvalds 7680ccba11SBjorn Helgaas dev_info(&bridge->dev, "CardBus bridge, secondary bus %04x:%02x\n", 7780ccba11SBjorn Helgaas pci_domain_nr(bus), bus->number); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 801da177e4SLinus Torvalds if (bus->resource[0]->flags & IORESOURCE_IO) { 811da177e4SLinus Torvalds /* 821da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 831da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 841da177e4SLinus Torvalds */ 8580ccba11SBjorn Helgaas dev_info(&bridge->dev, " IO window: %#08lx-%#08lx\n", 86c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 87c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 881da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 891da177e4SLinus Torvalds region.start); 901da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 911da177e4SLinus Torvalds region.end); 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 951da177e4SLinus Torvalds if (bus->resource[1]->flags & IORESOURCE_IO) { 9680ccba11SBjorn Helgaas dev_info(&bridge->dev, " IO window: %#08lx-%#08lx\n", 97c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 98c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 991da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 1001da177e4SLinus Torvalds region.start); 1011da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 1021da177e4SLinus Torvalds region.end); 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 1061da177e4SLinus Torvalds if (bus->resource[2]->flags & IORESOURCE_MEM) { 10780ccba11SBjorn Helgaas dev_info(&bridge->dev, " PREFETCH window: %#08lx-%#08lx\n", 108c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 109c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1101da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 1111da177e4SLinus Torvalds region.start); 1121da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 1131da177e4SLinus Torvalds region.end); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[3]); 1171da177e4SLinus Torvalds if (bus->resource[3]->flags & IORESOURCE_MEM) { 11880ccba11SBjorn Helgaas dev_info(&bridge->dev, " MEM window: %#08lx-%#08lx\n", 119c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 120c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1211da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 1221da177e4SLinus Torvalds region.start); 1231da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 1241da177e4SLinus Torvalds region.end); 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds } 127b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 1301da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 1311da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 1321da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 1331da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 1361da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 1371da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 1381da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 1391da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 140a391f197SAdrian Bunk static void pci_setup_bridge(struct pci_bus *bus) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 1431da177e4SLinus Torvalds struct pci_bus_region region; 144c40a22e0SBenjamin Herrenschmidt u32 l, bu, lu, io_upper16; 1451f82de10SYinghai Lu int pref_mem64; 1461da177e4SLinus Torvalds 147296ccb08SYuji Shimada if (pci_is_enabled(bridge)) 148b73e97d9SAlex Chiang return; 149b73e97d9SAlex Chiang 15080ccba11SBjorn Helgaas dev_info(&bridge->dev, "PCI bridge, secondary bus %04x:%02x\n", 15180ccba11SBjorn Helgaas pci_domain_nr(bus), bus->number); 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 1541da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[0]); 1551da177e4SLinus Torvalds if (bus->resource[0]->flags & IORESOURCE_IO) { 1561da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_IO_BASE, &l); 1571da177e4SLinus Torvalds l &= 0xffff0000; 1581da177e4SLinus Torvalds l |= (region.start >> 8) & 0x00f0; 1591da177e4SLinus Torvalds l |= region.end & 0xf000; 1601da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 1611da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 16280ccba11SBjorn Helgaas dev_info(&bridge->dev, " IO window: %#04lx-%#04lx\n", 163c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 164c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds else { 1671da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 1681da177e4SLinus Torvalds io_upper16 = 0; 1691da177e4SLinus Torvalds l = 0x00f0; 17080ccba11SBjorn Helgaas dev_info(&bridge->dev, " IO window: disabled\n"); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 1731da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 1741da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 1751da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE, l); 1761da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 1771da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /* Set up the top and bottom of the PCI Memory segment 1801da177e4SLinus Torvalds for this bus. */ 1811da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[1]); 1821da177e4SLinus Torvalds if (bus->resource[1]->flags & IORESOURCE_MEM) { 1831da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 1841da177e4SLinus Torvalds l |= region.end & 0xfff00000; 18580ccba11SBjorn Helgaas dev_info(&bridge->dev, " MEM window: %#08lx-%#08lx\n", 186c40a22e0SBenjamin Herrenschmidt (unsigned long)region.start, 187c40a22e0SBenjamin Herrenschmidt (unsigned long)region.end); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds else { 1901da177e4SLinus Torvalds l = 0x0000fff0; 19180ccba11SBjorn Helgaas dev_info(&bridge->dev, " MEM window: disabled\n"); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 1961da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 1971da177e4SLinus Torvalds disables PREF range, which is ok. */ 1981da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* Set up PREF base/limit. */ 2011f82de10SYinghai Lu pref_mem64 = 0; 202c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 2031da177e4SLinus Torvalds pcibios_resource_to_bus(bridge, ®ion, bus->resource[2]); 2041da177e4SLinus Torvalds if (bus->resource[2]->flags & IORESOURCE_PREFETCH) { 2051f82de10SYinghai Lu int width = 8; 2061da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 2071da177e4SLinus Torvalds l |= region.end & 0xfff00000; 2081f82de10SYinghai Lu if (bus->resource[2]->flags & IORESOURCE_MEM_64) { 2091f82de10SYinghai Lu pref_mem64 = 1; 21013d36c24SAndrew Morton bu = upper_32_bits(region.start); 21113d36c24SAndrew Morton lu = upper_32_bits(region.end); 2121f82de10SYinghai Lu width = 16; 2131f82de10SYinghai Lu } 2141f82de10SYinghai Lu dev_info(&bridge->dev, " PREFETCH window: %#0*llx-%#0*llx\n", 2151f82de10SYinghai Lu width, (unsigned long long)region.start, 2161f82de10SYinghai Lu width, (unsigned long long)region.end); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds else { 2191da177e4SLinus Torvalds l = 0x0000fff0; 22080ccba11SBjorn Helgaas dev_info(&bridge->dev, " PREFETCH window: disabled\n"); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 2231da177e4SLinus Torvalds 2241f82de10SYinghai Lu if (pref_mem64) { 225c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 226c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 227c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 2281f82de10SYinghai Lu } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 2341da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 2351da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 23696bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds u16 io; 2391da177e4SLinus Torvalds u32 pmem; 2401da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 2411da177e4SLinus Torvalds struct resource *b_res; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 2441da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 2471da177e4SLinus Torvalds if (!io) { 2481da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 2491da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 2501da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds if (io) 2531da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 2541da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 2551da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 2561da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 2571da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 2581da177e4SLinus Torvalds return; 2591da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 2601da177e4SLinus Torvalds if (!pmem) { 2611da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 2621da177e4SLinus Torvalds 0xfff0fff0); 2631da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 2641da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 2651da177e4SLinus Torvalds } 2661f82de10SYinghai Lu if (pmem) { 2671da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 2681f82de10SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) 2691f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 2701f82de10SYinghai Lu } 2711f82de10SYinghai Lu 2721f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 2731f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 2741f82de10SYinghai Lu u32 mem_base_hi, tmp; 2751f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 2761f82de10SYinghai Lu &mem_base_hi); 2771f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 2781f82de10SYinghai Lu 0xffffffff); 2791f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 2801f82de10SYinghai Lu if (!tmp) 2811f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 2821f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 2831f82de10SYinghai Lu mem_base_hi); 2841f82de10SYinghai Lu } 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 2881da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 2891da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 2901da177e4SLinus Torvalds have non-NULL parent resource). */ 29196bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds int i; 2941da177e4SLinus Torvalds struct resource *r; 2951da177e4SLinus Torvalds unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 2961da177e4SLinus Torvalds IORESOURCE_PREFETCH; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { 2991da177e4SLinus Torvalds r = bus->resource[i]; 300299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 301299de034SIvan Kokshaysky continue; 302*55a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 3031da177e4SLinus Torvalds return r; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds return NULL; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* Sizing the IO windows of the PCI-PCI bridge is trivial, 3091da177e4SLinus Torvalds since these windows have 4K granularity and the IO ranges 3101da177e4SLinus Torvalds of non-bridge PCI devices are limited to 256 bytes. 3111da177e4SLinus Torvalds We must be careful with the ISA aliasing though. */ 31228760489SEric W. Biederman static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds struct pci_dev *dev; 3151da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 3161da177e4SLinus Torvalds unsigned long size = 0, size1 = 0; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (!b_res) 3191da177e4SLinus Torvalds return; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 3221da177e4SLinus Torvalds int i; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 3251da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 3261da177e4SLinus Torvalds unsigned long r_size; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 3291da177e4SLinus Torvalds continue; 330022edd86SZhao, Yu r_size = resource_size(r); 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds if (r_size < 0x400) 3331da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 3341da177e4SLinus Torvalds size += r_size; 3351da177e4SLinus Torvalds else 3361da177e4SLinus Torvalds size1 += r_size; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds } 33928760489SEric W. Biederman if (size < min_size) 34028760489SEric W. Biederman size = min_size; 3411da177e4SLinus Torvalds /* To be fixed in 2.5: we should have sort of HAVE_ISA 3421da177e4SLinus Torvalds flag in the struct pci_bus. */ 3431da177e4SLinus Torvalds #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 3441da177e4SLinus Torvalds size = (size & 0xff) + ((size & ~0xffUL) << 2); 3451da177e4SLinus Torvalds #endif 3466f6f8c2fSMilind Arun Choudhary size = ALIGN(size + size1, 4096); 3471da177e4SLinus Torvalds if (!size) { 3481da177e4SLinus Torvalds b_res->flags = 0; 3491da177e4SLinus Torvalds return; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds /* Alignment of the IO window is always 4K */ 3521da177e4SLinus Torvalds b_res->start = 4096; 3531da177e4SLinus Torvalds b_res->end = b_res->start + size - 1; 35488452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* Calculate the size of the bus and minimal alignment which 3581da177e4SLinus Torvalds guarantees that all child resources fit in this size. */ 35928760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 36028760489SEric W. Biederman unsigned long type, resource_size_t min_size) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds struct pci_dev *dev; 363c40a22e0SBenjamin Herrenschmidt resource_size_t min_align, align, size; 364c40a22e0SBenjamin Herrenschmidt resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 3651da177e4SLinus Torvalds int order, max_order; 3661da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, type); 3671f82de10SYinghai Lu unsigned int mem64_mask = 0; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (!b_res) 3701da177e4SLinus Torvalds return 0; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 3731da177e4SLinus Torvalds max_order = 0; 3741da177e4SLinus Torvalds size = 0; 3751da177e4SLinus Torvalds 3761f82de10SYinghai Lu mem64_mask = b_res->flags & IORESOURCE_MEM_64; 3771f82de10SYinghai Lu b_res->flags &= ~IORESOURCE_MEM_64; 3781f82de10SYinghai Lu 3791da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 3801da177e4SLinus Torvalds int i; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 3831da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 384c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds if (r->parent || (r->flags & mask) != type) 3871da177e4SLinus Torvalds continue; 388022edd86SZhao, Yu r_size = resource_size(r); 3891da177e4SLinus Torvalds /* For bridges size != alignment */ 3906faf17f6SChris Wright align = pci_resource_alignment(dev, r); 3911da177e4SLinus Torvalds order = __ffs(align) - 20; 3921da177e4SLinus Torvalds if (order > 11) { 3935f17cfceSLinus Torvalds dev_warn(&dev->dev, "BAR %d bad alignment %llx: " 394096e6f67SBenjamin Herrenschmidt "%pR\n", i, (unsigned long long)align, r); 3951da177e4SLinus Torvalds r->flags = 0; 3961da177e4SLinus Torvalds continue; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds size += r_size; 3991da177e4SLinus Torvalds if (order < 0) 4001da177e4SLinus Torvalds order = 0; 4011da177e4SLinus Torvalds /* Exclude ranges with size > align from 4021da177e4SLinus Torvalds calculation of the alignment. */ 4031da177e4SLinus Torvalds if (r_size == align) 4041da177e4SLinus Torvalds aligns[order] += align; 4051da177e4SLinus Torvalds if (order > max_order) 4061da177e4SLinus Torvalds max_order = order; 4071f82de10SYinghai Lu mem64_mask &= r->flags & IORESOURCE_MEM_64; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds } 41028760489SEric W. Biederman if (size < min_size) 41128760489SEric W. Biederman size = min_size; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds align = 0; 4141da177e4SLinus Torvalds min_align = 0; 4151da177e4SLinus Torvalds for (order = 0; order <= max_order; order++) { 4168308c54dSJeremy Fitzhardinge resource_size_t align1 = 1; 4178308c54dSJeremy Fitzhardinge 4188308c54dSJeremy Fitzhardinge align1 <<= (order + 20); 4198308c54dSJeremy Fitzhardinge 4201da177e4SLinus Torvalds if (!align) 4211da177e4SLinus Torvalds min_align = align1; 4226f6f8c2fSMilind Arun Choudhary else if (ALIGN(align + min_align, min_align) < align1) 4231da177e4SLinus Torvalds min_align = align1 >> 1; 4241da177e4SLinus Torvalds align += aligns[order]; 4251da177e4SLinus Torvalds } 4266f6f8c2fSMilind Arun Choudhary size = ALIGN(size, min_align); 4271da177e4SLinus Torvalds if (!size) { 4281da177e4SLinus Torvalds b_res->flags = 0; 4291da177e4SLinus Torvalds return 1; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds b_res->start = min_align; 4321da177e4SLinus Torvalds b_res->end = size + min_align - 1; 43388452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 4341f82de10SYinghai Lu b_res->flags |= mem64_mask; 4351da177e4SLinus Torvalds return 1; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4385468ae61SAdrian Bunk static void pci_bus_size_cardbus(struct pci_bus *bus) 4391da177e4SLinus Torvalds { 4401da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 4411da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 4421da177e4SLinus Torvalds u16 ctrl; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* 4451da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 4461da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 4471da177e4SLinus Torvalds */ 448934b7024SLinus Torvalds b_res[0].start = 0; 449934b7024SLinus Torvalds b_res[0].end = pci_cardbus_io_size - 1; 450934b7024SLinus Torvalds b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 4511da177e4SLinus Torvalds 452934b7024SLinus Torvalds b_res[1].start = 0; 453934b7024SLinus Torvalds b_res[1].end = pci_cardbus_io_size - 1; 454934b7024SLinus Torvalds b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 4571da177e4SLinus Torvalds * Check whether prefetchable memory is supported 4581da177e4SLinus Torvalds * by this bridge. 4591da177e4SLinus Torvalds */ 4601da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 4611da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 4621da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 4631da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 4641da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /* 4681da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 4691da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 4701da177e4SLinus Torvalds * twice the size. 4711da177e4SLinus Torvalds */ 4721da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 473934b7024SLinus Torvalds b_res[2].start = 0; 474934b7024SLinus Torvalds b_res[2].end = pci_cardbus_mem_size - 1; 475934b7024SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 4761da177e4SLinus Torvalds 477934b7024SLinus Torvalds b_res[3].start = 0; 478934b7024SLinus Torvalds b_res[3].end = pci_cardbus_mem_size - 1; 479934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 4801da177e4SLinus Torvalds } else { 481934b7024SLinus Torvalds b_res[3].start = 0; 482934b7024SLinus Torvalds b_res[3].end = pci_cardbus_mem_size * 2 - 1; 483934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 487451124a7SSam Ravnborg void __ref pci_bus_size_bridges(struct pci_bus *bus) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds struct pci_dev *dev; 4901da177e4SLinus Torvalds unsigned long mask, prefmask; 49128760489SEric W. Biederman resource_size_t min_mem_size = 0, min_io_size = 0; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 4941da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 4951da177e4SLinus Torvalds if (!b) 4961da177e4SLinus Torvalds continue; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds switch (dev->class >> 8) { 4991da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 5001da177e4SLinus Torvalds pci_bus_size_cardbus(b); 5011da177e4SLinus Torvalds break; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 5041da177e4SLinus Torvalds default: 5051da177e4SLinus Torvalds pci_bus_size_bridges(b); 5061da177e4SLinus Torvalds break; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* The root bus? */ 5111da177e4SLinus Torvalds if (!bus->self) 5121da177e4SLinus Torvalds return; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds switch (bus->self->class >> 8) { 5151da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 5161da177e4SLinus Torvalds /* don't size cardbuses yet. */ 5171da177e4SLinus Torvalds break; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 5201da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 52128760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 52228760489SEric W. Biederman min_io_size = pci_hotplug_io_size; 52328760489SEric W. Biederman min_mem_size = pci_hotplug_mem_size; 52428760489SEric W. Biederman } 5251da177e4SLinus Torvalds default: 52628760489SEric W. Biederman pbus_size_io(bus, min_io_size); 5271da177e4SLinus Torvalds /* If the bridge supports prefetchable range, size it 5281da177e4SLinus Torvalds separately. If it doesn't, or its prefetchable window 5291da177e4SLinus Torvalds has already been allocated by arch code, try 5301da177e4SLinus Torvalds non-prefetchable range for both types of PCI memory 5311da177e4SLinus Torvalds resources. */ 5321da177e4SLinus Torvalds mask = IORESOURCE_MEM; 5331da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 53428760489SEric W. Biederman if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size)) 5351da177e4SLinus Torvalds mask = prefmask; /* Success, size non-prefetch only. */ 53628760489SEric W. Biederman else 53728760489SEric W. Biederman min_mem_size += min_mem_size; 53828760489SEric W. Biederman pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size); 5391da177e4SLinus Torvalds break; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 5431da177e4SLinus Torvalds 544ea741551SAndrew Morton void __ref pci_bus_assign_resources(const struct pci_bus *bus) 5451da177e4SLinus Torvalds { 5461da177e4SLinus Torvalds struct pci_bus *b; 5471da177e4SLinus Torvalds struct pci_dev *dev; 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds pbus_assign_resources_sorted(bus); 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 5521da177e4SLinus Torvalds b = dev->subordinate; 5531da177e4SLinus Torvalds if (!b) 5541da177e4SLinus Torvalds continue; 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds pci_bus_assign_resources(b); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds switch (dev->class >> 8) { 5591da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 5601da177e4SLinus Torvalds pci_setup_bridge(b); 5611da177e4SLinus Torvalds break; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 5641da177e4SLinus Torvalds pci_setup_cardbus(b); 5651da177e4SLinus Torvalds break; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds default: 56880ccba11SBjorn Helgaas dev_info(&dev->dev, "not setting up bridge for bus " 56980ccba11SBjorn Helgaas "%04x:%02x\n", pci_domain_nr(b), b->number); 5701da177e4SLinus Torvalds break; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 5751da177e4SLinus Torvalds 57676fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 57776fbc263SYinghai Lu { 57876fbc263SYinghai Lu int i; 57976fbc263SYinghai Lu 58076fbc263SYinghai Lu for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { 58176fbc263SYinghai Lu struct resource *res = bus->resource[i]; 582681bf597SYinghai Lu if (!res || !res->end) 58376fbc263SYinghai Lu continue; 58476fbc263SYinghai Lu 585a19f5df7SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %s %pR\n", i, 586681bf597SYinghai Lu (res->flags & IORESOURCE_IO) ? "io: " : 587681bf597SYinghai Lu ((res->flags & IORESOURCE_PREFETCH)? "pref mem":"mem:"), 588681bf597SYinghai Lu res); 58976fbc263SYinghai Lu } 59076fbc263SYinghai Lu } 59176fbc263SYinghai Lu 59276fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 59376fbc263SYinghai Lu { 59476fbc263SYinghai Lu struct pci_bus *b; 59576fbc263SYinghai Lu struct pci_dev *dev; 59676fbc263SYinghai Lu 59776fbc263SYinghai Lu 59876fbc263SYinghai Lu pci_bus_dump_res(bus); 59976fbc263SYinghai Lu 60076fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 60176fbc263SYinghai Lu b = dev->subordinate; 60276fbc263SYinghai Lu if (!b) 60376fbc263SYinghai Lu continue; 60476fbc263SYinghai Lu 60576fbc263SYinghai Lu pci_bus_dump_resources(b); 60676fbc263SYinghai Lu } 60776fbc263SYinghai Lu } 60876fbc263SYinghai Lu 6091da177e4SLinus Torvalds void __init 6101da177e4SLinus Torvalds pci_assign_unassigned_resources(void) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds struct pci_bus *bus; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 6151da177e4SLinus Torvalds subordinate buses. */ 6161da177e4SLinus Torvalds list_for_each_entry(bus, &pci_root_buses, node) { 6171da177e4SLinus Torvalds pci_bus_size_bridges(bus); 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 6201da177e4SLinus Torvalds list_for_each_entry(bus, &pci_root_buses, node) { 6211da177e4SLinus Torvalds pci_bus_assign_resources(bus); 6221da177e4SLinus Torvalds pci_enable_bridges(bus); 6231da177e4SLinus Torvalds } 62476fbc263SYinghai Lu 62576fbc263SYinghai Lu /* dump the resource on buses */ 62676fbc263SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) { 62776fbc263SYinghai Lu pci_bus_dump_resources(bus); 62876fbc263SYinghai Lu } 6291da177e4SLinus Torvalds } 630