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 30568ddef8SYinghai Lu struct resource_list_x { 31568ddef8SYinghai Lu struct resource_list_x *next; 32568ddef8SYinghai Lu struct resource *res; 33568ddef8SYinghai Lu struct pci_dev *dev; 34568ddef8SYinghai Lu resource_size_t start; 35568ddef8SYinghai Lu resource_size_t end; 36c8adf9a3SRam Pai resource_size_t add_size; 372bbc6942SRam Pai resource_size_t min_align; 38568ddef8SYinghai Lu unsigned long flags; 39568ddef8SYinghai Lu }; 40568ddef8SYinghai Lu 41094732a5SRam Pai #define free_list(type, head) do { \ 42094732a5SRam Pai struct type *list, *tmp; \ 43094732a5SRam Pai for (list = (head)->next; list;) { \ 44094732a5SRam Pai tmp = list; \ 45094732a5SRam Pai list = list->next; \ 46094732a5SRam Pai kfree(tmp); \ 47094732a5SRam Pai } \ 48094732a5SRam Pai (head)->next = NULL; \ 49094732a5SRam Pai } while (0) 50094732a5SRam Pai 51f483d392SRam Pai int pci_realloc_enable = 0; 52f483d392SRam Pai #define pci_realloc_enabled() pci_realloc_enable 53f483d392SRam Pai void pci_realloc(void) 54f483d392SRam Pai { 55f483d392SRam Pai pci_realloc_enable = 1; 56f483d392SRam Pai } 57f483d392SRam Pai 58c8adf9a3SRam Pai /** 59c8adf9a3SRam Pai * add_to_list() - add a new resource tracker to the list 60c8adf9a3SRam Pai * @head: Head of the list 61c8adf9a3SRam Pai * @dev: device corresponding to which the resource 62c8adf9a3SRam Pai * belongs 63c8adf9a3SRam Pai * @res: The resource to be tracked 64c8adf9a3SRam Pai * @add_size: additional size to be optionally added 65c8adf9a3SRam Pai * to the resource 66c8adf9a3SRam Pai */ 67c8adf9a3SRam Pai static void add_to_list(struct resource_list_x *head, 68c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res, 692bbc6942SRam Pai resource_size_t add_size, resource_size_t min_align) 70568ddef8SYinghai Lu { 71568ddef8SYinghai Lu struct resource_list_x *list = head; 72568ddef8SYinghai Lu struct resource_list_x *ln = list->next; 73568ddef8SYinghai Lu struct resource_list_x *tmp; 74568ddef8SYinghai Lu 75568ddef8SYinghai Lu tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); 76568ddef8SYinghai Lu if (!tmp) { 77c8adf9a3SRam Pai pr_warning("add_to_list: kmalloc() failed!\n"); 78568ddef8SYinghai Lu return; 79568ddef8SYinghai Lu } 80568ddef8SYinghai Lu 81568ddef8SYinghai Lu tmp->next = ln; 82568ddef8SYinghai Lu tmp->res = res; 83568ddef8SYinghai Lu tmp->dev = dev; 84568ddef8SYinghai Lu tmp->start = res->start; 85568ddef8SYinghai Lu tmp->end = res->end; 86568ddef8SYinghai Lu tmp->flags = res->flags; 87c8adf9a3SRam Pai tmp->add_size = add_size; 882bbc6942SRam Pai tmp->min_align = min_align; 89568ddef8SYinghai Lu list->next = tmp; 90568ddef8SYinghai Lu } 91568ddef8SYinghai Lu 92c8adf9a3SRam Pai static void add_to_failed_list(struct resource_list_x *head, 93c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res) 94c8adf9a3SRam Pai { 952bbc6942SRam Pai add_to_list(head, dev, res, 962bbc6942SRam Pai 0 /* dont care */, 972bbc6942SRam Pai 0 /* dont care */); 98c8adf9a3SRam Pai } 99c8adf9a3SRam Pai 1006841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev, 1016841ec68SYinghai Lu struct resource_list *head) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds u16 class = dev->class >> 8; 1041da177e4SLinus Torvalds 1059bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 1066841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1076841ec68SYinghai Lu return; 1081da177e4SLinus Torvalds 1099bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 11023186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 1119bded00bSKenji Kaneshige u16 command; 1129bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 1139bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 1146841ec68SYinghai Lu return; 11523186279SSatoru Takeuchi } 11623186279SSatoru Takeuchi 1176841ec68SYinghai Lu pdev_sort_resources(dev, head); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 120fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 121fc075e1dSRam Pai { 122fc075e1dSRam Pai res->start = 0; 123fc075e1dSRam Pai res->end = 0; 124fc075e1dSRam Pai res->flags = 0; 125fc075e1dSRam Pai } 126fc075e1dSRam Pai 127c8adf9a3SRam Pai /** 128c8adf9a3SRam Pai * adjust_resources_sorted() - satisfy any additional resource requests 129c8adf9a3SRam Pai * 130c8adf9a3SRam Pai * @add_head : head of the list tracking requests requiring additional 131c8adf9a3SRam Pai * resources 132c8adf9a3SRam Pai * @head : head of the list tracking requests with allocated 133c8adf9a3SRam Pai * resources 134c8adf9a3SRam Pai * 135c8adf9a3SRam Pai * Walk through each element of the add_head and try to procure 136c8adf9a3SRam Pai * additional resources for the element, provided the element 137c8adf9a3SRam Pai * is in the head list. 138c8adf9a3SRam Pai */ 139c8adf9a3SRam Pai static void adjust_resources_sorted(struct resource_list_x *add_head, 140c8adf9a3SRam Pai struct resource_list *head) 141c8adf9a3SRam Pai { 142c8adf9a3SRam Pai struct resource *res; 143c8adf9a3SRam Pai struct resource_list_x *list, *tmp, *prev; 144c8adf9a3SRam Pai struct resource_list *hlist; 145c8adf9a3SRam Pai resource_size_t add_size; 146c8adf9a3SRam Pai int idx; 147c8adf9a3SRam Pai 148c8adf9a3SRam Pai prev = add_head; 149c8adf9a3SRam Pai for (list = add_head->next; list;) { 150c8adf9a3SRam Pai res = list->res; 151c8adf9a3SRam Pai /* skip resource that has been reset */ 152c8adf9a3SRam Pai if (!res->flags) 153c8adf9a3SRam Pai goto out; 154c8adf9a3SRam Pai 155c8adf9a3SRam Pai /* skip this resource if not found in head list */ 156c8adf9a3SRam Pai for (hlist = head->next; hlist && hlist->res != res; 157c8adf9a3SRam Pai hlist = hlist->next); 158c8adf9a3SRam Pai if (!hlist) { /* just skip */ 159c8adf9a3SRam Pai prev = list; 160c8adf9a3SRam Pai list = list->next; 161c8adf9a3SRam Pai continue; 162c8adf9a3SRam Pai } 163c8adf9a3SRam Pai 164c8adf9a3SRam Pai idx = res - &list->dev->resource[0]; 165c8adf9a3SRam Pai add_size=list->add_size; 1662bbc6942SRam Pai if (!resource_size(res)) { 167*0a2daa1cSRam Pai res->start = list->start; 168c8adf9a3SRam Pai res->end = res->start + add_size - 1; 169c8adf9a3SRam Pai if(pci_assign_resource(list->dev, idx)) 170c8adf9a3SRam Pai reset_resource(res); 1712bbc6942SRam Pai } else { 1722bbc6942SRam Pai resource_size_t align = list->min_align; 1732bbc6942SRam Pai res->flags |= list->flags & (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 1742bbc6942SRam Pai if (pci_reassign_resource(list->dev, idx, add_size, align)) 1752bbc6942SRam Pai dev_printk(KERN_DEBUG, &list->dev->dev, "failed to add optional resources res=%pR\n", 1762bbc6942SRam Pai res); 177c8adf9a3SRam Pai } 178c8adf9a3SRam Pai out: 179c8adf9a3SRam Pai tmp = list; 180c8adf9a3SRam Pai prev->next = list = list->next; 181c8adf9a3SRam Pai kfree(tmp); 182c8adf9a3SRam Pai } 183c8adf9a3SRam Pai } 184c8adf9a3SRam Pai 185c8adf9a3SRam Pai /** 186c8adf9a3SRam Pai * assign_requested_resources_sorted() - satisfy resource requests 187c8adf9a3SRam Pai * 188c8adf9a3SRam Pai * @head : head of the list tracking requests for resources 189c8adf9a3SRam Pai * @failed_list : head of the list tracking requests that could 190c8adf9a3SRam Pai * not be allocated 191c8adf9a3SRam Pai * 192c8adf9a3SRam Pai * Satisfy resource requests of each element in the list. Add 193c8adf9a3SRam Pai * requests that could not satisfied to the failed_list. 194c8adf9a3SRam Pai */ 195c8adf9a3SRam Pai static void assign_requested_resources_sorted(struct resource_list *head, 1966841ec68SYinghai Lu struct resource_list_x *fail_head) 1976841ec68SYinghai Lu { 1986841ec68SYinghai Lu struct resource *res; 199c8adf9a3SRam Pai struct resource_list *list; 2006841ec68SYinghai Lu int idx; 2016841ec68SYinghai Lu 202c8adf9a3SRam Pai for (list = head->next; list; list = list->next) { 2031da177e4SLinus Torvalds res = list->res; 2041da177e4SLinus Torvalds idx = res - &list->dev->resource[0]; 205c8adf9a3SRam Pai if (resource_size(res) && pci_assign_resource(list->dev, idx)) { 2069a928660SYinghai Lu if (fail_head && !pci_is_root_bus(list->dev->bus)) { 2079a928660SYinghai Lu /* 2089a928660SYinghai Lu * if the failed res is for ROM BAR, and it will 2099a928660SYinghai Lu * be enabled later, don't add it to the list 2109a928660SYinghai Lu */ 2119a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 2129a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 213568ddef8SYinghai Lu add_to_failed_list(fail_head, list->dev, res); 2149a928660SYinghai Lu } 215fc075e1dSRam Pai reset_resource(res); 216542df5deSRajesh Shah } 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 220c8adf9a3SRam Pai static void __assign_resources_sorted(struct resource_list *head, 221c8adf9a3SRam Pai struct resource_list_x *add_head, 222c8adf9a3SRam Pai struct resource_list_x *fail_head) 223c8adf9a3SRam Pai { 224c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 225c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 226c8adf9a3SRam Pai 227*0a2daa1cSRam Pai /* Try to satisfy any additional optional resource 228c8adf9a3SRam Pai requests */ 229c8adf9a3SRam Pai if (add_head) 230c8adf9a3SRam Pai adjust_resources_sorted(add_head, head); 231c8adf9a3SRam Pai free_list(resource_list, head); 232c8adf9a3SRam Pai } 233c8adf9a3SRam Pai 2346841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 2356841ec68SYinghai Lu struct resource_list_x *fail_head) 2366841ec68SYinghai Lu { 2376841ec68SYinghai Lu struct resource_list head; 2386841ec68SYinghai Lu 2396841ec68SYinghai Lu head.next = NULL; 2406841ec68SYinghai Lu __dev_sort_resources(dev, &head); 241c8adf9a3SRam Pai __assign_resources_sorted(&head, NULL, fail_head); 2426841ec68SYinghai Lu 2436841ec68SYinghai Lu } 2446841ec68SYinghai Lu 2456841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 246c8adf9a3SRam Pai struct resource_list_x *add_head, 2476841ec68SYinghai Lu struct resource_list_x *fail_head) 2486841ec68SYinghai Lu { 2496841ec68SYinghai Lu struct pci_dev *dev; 2506841ec68SYinghai Lu struct resource_list head; 2516841ec68SYinghai Lu 2526841ec68SYinghai Lu head.next = NULL; 2536841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 2546841ec68SYinghai Lu __dev_sort_resources(dev, &head); 2556841ec68SYinghai Lu 256c8adf9a3SRam Pai __assign_resources_sorted(&head, add_head, fail_head); 2576841ec68SYinghai Lu } 2586841ec68SYinghai Lu 259b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 262c7dabef8SBjorn Helgaas struct resource *res; 2631da177e4SLinus Torvalds struct pci_bus_region region; 2641da177e4SLinus Torvalds 265865df576SBjorn Helgaas dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", 266865df576SBjorn Helgaas bus->secondary, bus->subordinate); 2671da177e4SLinus Torvalds 268c7dabef8SBjorn Helgaas res = bus->resource[0]; 269c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 270c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 2711da177e4SLinus Torvalds /* 2721da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 2731da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 2741da177e4SLinus Torvalds */ 275c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 2761da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 2771da177e4SLinus Torvalds region.start); 2781da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 2791da177e4SLinus Torvalds region.end); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 282c7dabef8SBjorn Helgaas res = bus->resource[1]; 283c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 284c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 285c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 2861da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 2871da177e4SLinus Torvalds region.start); 2881da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 2891da177e4SLinus Torvalds region.end); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 292c7dabef8SBjorn Helgaas res = bus->resource[2]; 293c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 294c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 295c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 2961da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 2971da177e4SLinus Torvalds region.start); 2981da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 2991da177e4SLinus Torvalds region.end); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 302c7dabef8SBjorn Helgaas res = bus->resource[3]; 303c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 304c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 305c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 3061da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 3071da177e4SLinus Torvalds region.start); 3081da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 3091da177e4SLinus Torvalds region.end); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds } 312b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 3151da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 3161da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 3171da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 3181da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 3211da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 3221da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 3231da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 3241da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 3257cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 328c7dabef8SBjorn Helgaas struct resource *res; 3291da177e4SLinus Torvalds struct pci_bus_region region; 3307cc5997dSYinghai Lu u32 l, io_upper16; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 333c7dabef8SBjorn Helgaas res = bus->resource[0]; 334c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 335c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 3361da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_IO_BASE, &l); 3371da177e4SLinus Torvalds l &= 0xffff0000; 3381da177e4SLinus Torvalds l |= (region.start >> 8) & 0x00f0; 3391da177e4SLinus Torvalds l |= region.end & 0xf000; 3401da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 3411da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 342c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 3437cc5997dSYinghai Lu } else { 3441da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 3451da177e4SLinus Torvalds io_upper16 = 0; 3461da177e4SLinus Torvalds l = 0x00f0; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 3491da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 3501da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 3511da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE, l); 3521da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 3531da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 3547cc5997dSYinghai Lu } 3551da177e4SLinus Torvalds 3567cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus) 3577cc5997dSYinghai Lu { 3587cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 3597cc5997dSYinghai Lu struct resource *res; 3607cc5997dSYinghai Lu struct pci_bus_region region; 3617cc5997dSYinghai Lu u32 l; 3627cc5997dSYinghai Lu 3637cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 364c7dabef8SBjorn Helgaas res = bus->resource[1]; 365c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 366c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 3671da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 3681da177e4SLinus Torvalds l |= region.end & 0xfff00000; 369c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 3707cc5997dSYinghai Lu } else { 3711da177e4SLinus Torvalds l = 0x0000fff0; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 3747cc5997dSYinghai Lu } 3757cc5997dSYinghai Lu 3767cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) 3777cc5997dSYinghai Lu { 3787cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 3797cc5997dSYinghai Lu struct resource *res; 3807cc5997dSYinghai Lu struct pci_bus_region region; 3817cc5997dSYinghai Lu u32 l, bu, lu; 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 3841da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 3851da177e4SLinus Torvalds disables PREF range, which is ok. */ 3861da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* Set up PREF base/limit. */ 389c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 390c7dabef8SBjorn Helgaas res = bus->resource[2]; 391c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 392c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 3931da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 3941da177e4SLinus Torvalds l |= region.end & 0xfff00000; 395c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 39613d36c24SAndrew Morton bu = upper_32_bits(region.start); 39713d36c24SAndrew Morton lu = upper_32_bits(region.end); 3981f82de10SYinghai Lu } 399c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4007cc5997dSYinghai Lu } else { 4011da177e4SLinus Torvalds l = 0x0000fff0; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 4041da177e4SLinus Torvalds 405c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 406c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 407c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 4087cc5997dSYinghai Lu } 4097cc5997dSYinghai Lu 4107cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 4117cc5997dSYinghai Lu { 4127cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 4137cc5997dSYinghai Lu 4147cc5997dSYinghai Lu dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", 4157cc5997dSYinghai Lu bus->secondary, bus->subordinate); 4167cc5997dSYinghai Lu 4177cc5997dSYinghai Lu if (type & IORESOURCE_IO) 4187cc5997dSYinghai Lu pci_setup_bridge_io(bus); 4197cc5997dSYinghai Lu 4207cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 4217cc5997dSYinghai Lu pci_setup_bridge_mmio(bus); 4227cc5997dSYinghai Lu 4237cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 4247cc5997dSYinghai Lu pci_setup_bridge_mmio_pref(bus); 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4297cc5997dSYinghai Lu static void pci_setup_bridge(struct pci_bus *bus) 4307cc5997dSYinghai Lu { 4317cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 4327cc5997dSYinghai Lu IORESOURCE_PREFETCH; 4337cc5997dSYinghai Lu 4347cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 4357cc5997dSYinghai Lu } 4367cc5997dSYinghai Lu 4371da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 4381da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 4391da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 44096bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds u16 io; 4431da177e4SLinus Torvalds u32 pmem; 4441da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 4451da177e4SLinus Torvalds struct resource *b_res; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 4481da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 4511da177e4SLinus Torvalds if (!io) { 4521da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 4531da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 4541da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds if (io) 4571da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 4581da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 4591da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 4601da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 4611da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 4621da177e4SLinus Torvalds return; 4631da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 4641da177e4SLinus Torvalds if (!pmem) { 4651da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 4661da177e4SLinus Torvalds 0xfff0fff0); 4671da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 4681da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 4691da177e4SLinus Torvalds } 4701f82de10SYinghai Lu if (pmem) { 4711da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 47299586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 47399586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 4741f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 47599586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 47699586105SYinghai Lu } 4771f82de10SYinghai Lu } 4781f82de10SYinghai Lu 4791f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 4801f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 4811f82de10SYinghai Lu u32 mem_base_hi, tmp; 4821f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 4831f82de10SYinghai Lu &mem_base_hi); 4841f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 4851f82de10SYinghai Lu 0xffffffff); 4861f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 4871f82de10SYinghai Lu if (!tmp) 4881f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 4891f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 4901f82de10SYinghai Lu mem_base_hi); 4911f82de10SYinghai Lu } 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 4951da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 4961da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 4971da177e4SLinus Torvalds have non-NULL parent resource). */ 49896bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds int i; 5011da177e4SLinus Torvalds struct resource *r; 5021da177e4SLinus Torvalds unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 5031da177e4SLinus Torvalds IORESOURCE_PREFETCH; 5041da177e4SLinus Torvalds 50589a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 506299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 507299de034SIvan Kokshaysky continue; 50855a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 5091da177e4SLinus Torvalds return r; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds return NULL; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 51413583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 51513583b16SRam Pai resource_size_t min_size, 51613583b16SRam Pai resource_size_t size1, 51713583b16SRam Pai resource_size_t old_size, 51813583b16SRam Pai resource_size_t align) 51913583b16SRam Pai { 52013583b16SRam Pai if (size < min_size) 52113583b16SRam Pai size = min_size; 52213583b16SRam Pai if (old_size == 1 ) 52313583b16SRam Pai old_size = 0; 52413583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 52513583b16SRam Pai flag in the struct pci_bus. */ 52613583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 52713583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 52813583b16SRam Pai #endif 52913583b16SRam Pai size = ALIGN(size + size1, align); 53013583b16SRam Pai if (size < old_size) 53113583b16SRam Pai size = old_size; 53213583b16SRam Pai return size; 53313583b16SRam Pai } 53413583b16SRam Pai 53513583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 53613583b16SRam Pai resource_size_t min_size, 53713583b16SRam Pai resource_size_t size1, 53813583b16SRam Pai resource_size_t old_size, 53913583b16SRam Pai resource_size_t align) 54013583b16SRam Pai { 54113583b16SRam Pai if (size < min_size) 54213583b16SRam Pai size = min_size; 54313583b16SRam Pai if (old_size == 1 ) 54413583b16SRam Pai old_size = 0; 54513583b16SRam Pai if (size < old_size) 54613583b16SRam Pai size = old_size; 54713583b16SRam Pai size = ALIGN(size + size1, align); 54813583b16SRam Pai return size; 54913583b16SRam Pai } 55013583b16SRam Pai 551be768912SYinghai Lu static resource_size_t get_res_add_size(struct resource_list_x *add_head, 552be768912SYinghai Lu struct resource *res) 553be768912SYinghai Lu { 554be768912SYinghai Lu struct resource_list_x *list; 555be768912SYinghai Lu 556be768912SYinghai Lu /* check if it is in add_head list */ 557be768912SYinghai Lu for (list = add_head->next; list && list->res != res; 558be768912SYinghai Lu list = list->next); 559be768912SYinghai Lu if (list) 560be768912SYinghai Lu return list->add_size; 561be768912SYinghai Lu 562be768912SYinghai Lu return 0; 563be768912SYinghai Lu } 564be768912SYinghai Lu 565c8adf9a3SRam Pai /** 566c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 567c8adf9a3SRam Pai * 568c8adf9a3SRam Pai * @bus : the bus 569c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 570c8adf9a3SRam Pai * @add_size : additional optional io window 571c8adf9a3SRam Pai * @add_head : track the additional io window on this list 572c8adf9a3SRam Pai * 573c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 574c8adf9a3SRam Pai * since these windows have 4K granularity and the IO ranges 575c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 576c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 577c8adf9a3SRam Pai */ 578c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 579c8adf9a3SRam Pai resource_size_t add_size, struct resource_list_x *add_head) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds struct pci_dev *dev; 5821da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 583c8adf9a3SRam Pai unsigned long size = 0, size0 = 0, size1 = 0; 584be768912SYinghai Lu resource_size_t children_add_size = 0; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (!b_res) 5871da177e4SLinus Torvalds return; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 5901da177e4SLinus Torvalds int i; 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 5931da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 5941da177e4SLinus Torvalds unsigned long r_size; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 5971da177e4SLinus Torvalds continue; 598022edd86SZhao, Yu r_size = resource_size(r); 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds if (r_size < 0x400) 6011da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 6021da177e4SLinus Torvalds size += r_size; 6031da177e4SLinus Torvalds else 6041da177e4SLinus Torvalds size1 += r_size; 605be768912SYinghai Lu 606be768912SYinghai Lu if (add_head) 607be768912SYinghai Lu children_add_size += get_res_add_size(add_head, r); 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds } 610c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 61113583b16SRam Pai resource_size(b_res), 4096); 612be768912SYinghai Lu if (children_add_size > add_size) 613be768912SYinghai Lu add_size = children_add_size; 61493d2175dSYinghai Lu size1 = (!add_head || (add_head && !add_size)) ? size0 : 615c8adf9a3SRam Pai calculate_iosize(size, min_size+add_size, size1, 616c8adf9a3SRam Pai resource_size(b_res), 4096); 617c8adf9a3SRam Pai if (!size0 && !size1) { 618865df576SBjorn Helgaas if (b_res->start || b_res->end) 619865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 620865df576SBjorn Helgaas "%pR to [bus %02x-%02x] (unused)\n", b_res, 621865df576SBjorn Helgaas bus->secondary, bus->subordinate); 6221da177e4SLinus Torvalds b_res->flags = 0; 6231da177e4SLinus Torvalds return; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds /* Alignment of the IO window is always 4K */ 6261da177e4SLinus Torvalds b_res->start = 4096; 627c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 62888452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 629c8adf9a3SRam Pai if (size1 > size0 && add_head) 6302bbc6942SRam Pai add_to_list(add_head, bus->self, b_res, size1-size0, 4096); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 633c8adf9a3SRam Pai /** 634c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 635c8adf9a3SRam Pai * 636c8adf9a3SRam Pai * @bus : the bus 637c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 638c8adf9a3SRam Pai * @add_size : additional optional memory window 639c8adf9a3SRam Pai * @add_head : track the additional memory window on this list 640c8adf9a3SRam Pai * 641c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 642c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 643c8adf9a3SRam Pai */ 64428760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 645c8adf9a3SRam Pai unsigned long type, resource_size_t min_size, 646c8adf9a3SRam Pai resource_size_t add_size, 647c8adf9a3SRam Pai struct resource_list_x *add_head) 6481da177e4SLinus Torvalds { 6491da177e4SLinus Torvalds struct pci_dev *dev; 650c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 651c40a22e0SBenjamin Herrenschmidt resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 6521da177e4SLinus Torvalds int order, max_order; 6531da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, type); 6541f82de10SYinghai Lu unsigned int mem64_mask = 0; 655be768912SYinghai Lu resource_size_t children_add_size = 0; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds if (!b_res) 6581da177e4SLinus Torvalds return 0; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 6611da177e4SLinus Torvalds max_order = 0; 6621da177e4SLinus Torvalds size = 0; 6631da177e4SLinus Torvalds 6641f82de10SYinghai Lu mem64_mask = b_res->flags & IORESOURCE_MEM_64; 6651f82de10SYinghai Lu b_res->flags &= ~IORESOURCE_MEM_64; 6661f82de10SYinghai Lu 6671da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 6681da177e4SLinus Torvalds int i; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 6711da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 672c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds if (r->parent || (r->flags & mask) != type) 6751da177e4SLinus Torvalds continue; 676022edd86SZhao, Yu r_size = resource_size(r); 6772aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 6782aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 6792aceefcbSYinghai Lu if (add_head && i >= PCI_IOV_RESOURCES && 6802aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 6812aceefcbSYinghai Lu r->end = r->start - 1; 682*0a2daa1cSRam Pai add_to_list(add_head, dev, r, r_size, 0/* dont' care */); 6832aceefcbSYinghai Lu children_add_size += r_size; 6842aceefcbSYinghai Lu continue; 6852aceefcbSYinghai Lu } 6862aceefcbSYinghai Lu #endif 6871da177e4SLinus Torvalds /* For bridges size != alignment */ 6886faf17f6SChris Wright align = pci_resource_alignment(dev, r); 6891da177e4SLinus Torvalds order = __ffs(align) - 20; 6901da177e4SLinus Torvalds if (order > 11) { 691865df576SBjorn Helgaas dev_warn(&dev->dev, "disabling BAR %d: %pR " 692865df576SBjorn Helgaas "(bad alignment %#llx)\n", i, r, 693865df576SBjorn Helgaas (unsigned long long) align); 6941da177e4SLinus Torvalds r->flags = 0; 6951da177e4SLinus Torvalds continue; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds size += r_size; 6981da177e4SLinus Torvalds if (order < 0) 6991da177e4SLinus Torvalds order = 0; 7001da177e4SLinus Torvalds /* Exclude ranges with size > align from 7011da177e4SLinus Torvalds calculation of the alignment. */ 7021da177e4SLinus Torvalds if (r_size == align) 7031da177e4SLinus Torvalds aligns[order] += align; 7041da177e4SLinus Torvalds if (order > max_order) 7051da177e4SLinus Torvalds max_order = order; 7061f82de10SYinghai Lu mem64_mask &= r->flags & IORESOURCE_MEM_64; 707be768912SYinghai Lu 708be768912SYinghai Lu if (add_head) 709be768912SYinghai Lu children_add_size += get_res_add_size(add_head, r); 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds align = 0; 7131da177e4SLinus Torvalds min_align = 0; 7141da177e4SLinus Torvalds for (order = 0; order <= max_order; order++) { 7158308c54dSJeremy Fitzhardinge resource_size_t align1 = 1; 7168308c54dSJeremy Fitzhardinge 7178308c54dSJeremy Fitzhardinge align1 <<= (order + 20); 7188308c54dSJeremy Fitzhardinge 7191da177e4SLinus Torvalds if (!align) 7201da177e4SLinus Torvalds min_align = align1; 7216f6f8c2fSMilind Arun Choudhary else if (ALIGN(align + min_align, min_align) < align1) 7221da177e4SLinus Torvalds min_align = align1 >> 1; 7231da177e4SLinus Torvalds align += aligns[order]; 7241da177e4SLinus Torvalds } 725b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 726be768912SYinghai Lu if (children_add_size > add_size) 727be768912SYinghai Lu add_size = children_add_size; 72893d2175dSYinghai Lu size1 = (!add_head || (add_head && !add_size)) ? size0 : 729c8adf9a3SRam Pai calculate_memsize(size, min_size+add_size, 0, 730b42282e5SLinus Torvalds resource_size(b_res), min_align); 731c8adf9a3SRam Pai if (!size0 && !size1) { 732865df576SBjorn Helgaas if (b_res->start || b_res->end) 733865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 734865df576SBjorn Helgaas "%pR to [bus %02x-%02x] (unused)\n", b_res, 735865df576SBjorn Helgaas bus->secondary, bus->subordinate); 7361da177e4SLinus Torvalds b_res->flags = 0; 7371da177e4SLinus Torvalds return 1; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds b_res->start = min_align; 740c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 741c8adf9a3SRam Pai b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; 742c8adf9a3SRam Pai if (size1 > size0 && add_head) 7432bbc6942SRam Pai add_to_list(add_head, bus->self, b_res, size1-size0, min_align); 7441da177e4SLinus Torvalds return 1; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 747*0a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 748*0a2daa1cSRam Pai { 749*0a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 750*0a2daa1cSRam Pai return pci_cardbus_io_size; 751*0a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 752*0a2daa1cSRam Pai return pci_cardbus_mem_size; 753*0a2daa1cSRam Pai return 0; 754*0a2daa1cSRam Pai } 755*0a2daa1cSRam Pai 756*0a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 757*0a2daa1cSRam Pai struct resource_list_x *add_head) 7581da177e4SLinus Torvalds { 7591da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 7601da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 7611da177e4SLinus Torvalds u16 ctrl; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 7641da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 7651da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 7661da177e4SLinus Torvalds */ 767934b7024SLinus Torvalds b_res[0].start = 0; 768934b7024SLinus Torvalds b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 769*0a2daa1cSRam Pai if (add_head) 770*0a2daa1cSRam Pai add_to_list(add_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */); 7711da177e4SLinus Torvalds 772934b7024SLinus Torvalds b_res[1].start = 0; 773934b7024SLinus Torvalds b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 774*0a2daa1cSRam Pai if (add_head) 775*0a2daa1cSRam Pai add_to_list(add_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */); 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* 7781da177e4SLinus Torvalds * Check whether prefetchable memory is supported 7791da177e4SLinus Torvalds * by this bridge. 7801da177e4SLinus Torvalds */ 7811da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 7821da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 7831da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 7841da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 7851da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 7901da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 7911da177e4SLinus Torvalds * twice the size. 7921da177e4SLinus Torvalds */ 7931da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 794934b7024SLinus Torvalds b_res[2].start = 0; 795934b7024SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 796*0a2daa1cSRam Pai if (add_head) 797*0a2daa1cSRam Pai add_to_list(add_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */); 7981da177e4SLinus Torvalds 799934b7024SLinus Torvalds b_res[3].start = 0; 800934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 801*0a2daa1cSRam Pai if (add_head) 802*0a2daa1cSRam Pai add_to_list(add_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */); 8031da177e4SLinus Torvalds } else { 804934b7024SLinus Torvalds b_res[3].start = 0; 805934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 806*0a2daa1cSRam Pai if (add_head) 807*0a2daa1cSRam Pai add_to_list(add_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */); 8081da177e4SLinus Torvalds } 809*0a2daa1cSRam Pai 810*0a2daa1cSRam Pai /* set the size of the resource to zero, so that the resource does not 811*0a2daa1cSRam Pai * get assigned during required-resource allocation cycle but gets assigned 812*0a2daa1cSRam Pai * during the optional-resource allocation cycle. 813*0a2daa1cSRam Pai */ 814*0a2daa1cSRam Pai b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1; 815*0a2daa1cSRam Pai b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 818c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus, 819c8adf9a3SRam Pai struct resource_list_x *add_head) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct pci_dev *dev; 8221da177e4SLinus Torvalds unsigned long mask, prefmask; 823c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8261da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 8271da177e4SLinus Torvalds if (!b) 8281da177e4SLinus Torvalds continue; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds switch (dev->class >> 8) { 8311da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 832*0a2daa1cSRam Pai pci_bus_size_cardbus(b, add_head); 8331da177e4SLinus Torvalds break; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 8361da177e4SLinus Torvalds default: 837c8adf9a3SRam Pai __pci_bus_size_bridges(b, add_head); 8381da177e4SLinus Torvalds break; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds /* The root bus? */ 8431da177e4SLinus Torvalds if (!bus->self) 8441da177e4SLinus Torvalds return; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds switch (bus->self->class >> 8) { 8471da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 8481da177e4SLinus Torvalds /* don't size cardbuses yet. */ 8491da177e4SLinus Torvalds break; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 8521da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 85328760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 854c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 855c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 85628760489SEric W. Biederman } 857c8adf9a3SRam Pai /* 858c8adf9a3SRam Pai * Follow thru 859c8adf9a3SRam Pai */ 8601da177e4SLinus Torvalds default: 861c8adf9a3SRam Pai pbus_size_io(bus, 0, additional_io_size, add_head); 8621da177e4SLinus Torvalds /* If the bridge supports prefetchable range, size it 8631da177e4SLinus Torvalds separately. If it doesn't, or its prefetchable window 8641da177e4SLinus Torvalds has already been allocated by arch code, try 8651da177e4SLinus Torvalds non-prefetchable range for both types of PCI memory 8661da177e4SLinus Torvalds resources. */ 8671da177e4SLinus Torvalds mask = IORESOURCE_MEM; 8681da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 869c8adf9a3SRam Pai if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, add_head)) 8701da177e4SLinus Torvalds mask = prefmask; /* Success, size non-prefetch only. */ 87128760489SEric W. Biederman else 872c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 873c8adf9a3SRam Pai pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, add_head); 8741da177e4SLinus Torvalds break; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds } 877c8adf9a3SRam Pai 878c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus) 879c8adf9a3SRam Pai { 880c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 881c8adf9a3SRam Pai } 8821da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 8831da177e4SLinus Torvalds 884568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 885c8adf9a3SRam Pai struct resource_list_x *add_head, 886568ddef8SYinghai Lu struct resource_list_x *fail_head) 8871da177e4SLinus Torvalds { 8881da177e4SLinus Torvalds struct pci_bus *b; 8891da177e4SLinus Torvalds struct pci_dev *dev; 8901da177e4SLinus Torvalds 891c8adf9a3SRam Pai pbus_assign_resources_sorted(bus, add_head, fail_head); 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8941da177e4SLinus Torvalds b = dev->subordinate; 8951da177e4SLinus Torvalds if (!b) 8961da177e4SLinus Torvalds continue; 8971da177e4SLinus Torvalds 898c8adf9a3SRam Pai __pci_bus_assign_resources(b, add_head, fail_head); 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds switch (dev->class >> 8) { 9011da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 9026841ec68SYinghai Lu if (!pci_is_enabled(dev)) 9031da177e4SLinus Torvalds pci_setup_bridge(b); 9041da177e4SLinus Torvalds break; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 9071da177e4SLinus Torvalds pci_setup_cardbus(b); 9081da177e4SLinus Torvalds break; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds default: 91180ccba11SBjorn Helgaas dev_info(&dev->dev, "not setting up bridge for bus " 91280ccba11SBjorn Helgaas "%04x:%02x\n", pci_domain_nr(b), b->number); 9131da177e4SLinus Torvalds break; 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds } 917568ddef8SYinghai Lu 918568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus) 919568ddef8SYinghai Lu { 920c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 921568ddef8SYinghai Lu } 9221da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 9231da177e4SLinus Torvalds 9246841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 9256841ec68SYinghai Lu struct resource_list_x *fail_head) 9266841ec68SYinghai Lu { 9276841ec68SYinghai Lu struct pci_bus *b; 9286841ec68SYinghai Lu 9296841ec68SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head); 9306841ec68SYinghai Lu 9316841ec68SYinghai Lu b = bridge->subordinate; 9326841ec68SYinghai Lu if (!b) 9336841ec68SYinghai Lu return; 9346841ec68SYinghai Lu 935c8adf9a3SRam Pai __pci_bus_assign_resources(b, NULL, fail_head); 9366841ec68SYinghai Lu 9376841ec68SYinghai Lu switch (bridge->class >> 8) { 9386841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 9396841ec68SYinghai Lu pci_setup_bridge(b); 9406841ec68SYinghai Lu break; 9416841ec68SYinghai Lu 9426841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 9436841ec68SYinghai Lu pci_setup_cardbus(b); 9446841ec68SYinghai Lu break; 9456841ec68SYinghai Lu 9466841ec68SYinghai Lu default: 9476841ec68SYinghai Lu dev_info(&bridge->dev, "not setting up bridge for bus " 9486841ec68SYinghai Lu "%04x:%02x\n", pci_domain_nr(b), b->number); 9496841ec68SYinghai Lu break; 9506841ec68SYinghai Lu } 9516841ec68SYinghai Lu } 9525009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 9535009b460SYinghai Lu unsigned long type) 9545009b460SYinghai Lu { 9555009b460SYinghai Lu int idx; 9565009b460SYinghai Lu bool changed = false; 9575009b460SYinghai Lu struct pci_dev *dev; 9585009b460SYinghai Lu struct resource *r; 9595009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 9605009b460SYinghai Lu IORESOURCE_PREFETCH; 9615009b460SYinghai Lu 9625009b460SYinghai Lu dev = bus->self; 9635009b460SYinghai Lu for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; 9645009b460SYinghai Lu idx++) { 9655009b460SYinghai Lu r = &dev->resource[idx]; 9665009b460SYinghai Lu if ((r->flags & type_mask) != type) 9675009b460SYinghai Lu continue; 9685009b460SYinghai Lu if (!r->parent) 9695009b460SYinghai Lu continue; 9705009b460SYinghai Lu /* 9715009b460SYinghai Lu * if there are children under that, we should release them 9725009b460SYinghai Lu * all 9735009b460SYinghai Lu */ 9745009b460SYinghai Lu release_child_resources(r); 9755009b460SYinghai Lu if (!release_resource(r)) { 9765009b460SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, 9775009b460SYinghai Lu "resource %d %pR released\n", idx, r); 9785009b460SYinghai Lu /* keep the old size */ 9795009b460SYinghai Lu r->end = resource_size(r) - 1; 9805009b460SYinghai Lu r->start = 0; 9815009b460SYinghai Lu r->flags = 0; 9825009b460SYinghai Lu changed = true; 9835009b460SYinghai Lu } 9845009b460SYinghai Lu } 9855009b460SYinghai Lu 9865009b460SYinghai Lu if (changed) { 9875009b460SYinghai Lu /* avoiding touch the one without PREF */ 9885009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 9895009b460SYinghai Lu type = IORESOURCE_PREFETCH; 9905009b460SYinghai Lu __pci_setup_bridge(bus, type); 9915009b460SYinghai Lu } 9925009b460SYinghai Lu } 9935009b460SYinghai Lu 9945009b460SYinghai Lu enum release_type { 9955009b460SYinghai Lu leaf_only, 9965009b460SYinghai Lu whole_subtree, 9975009b460SYinghai Lu }; 9985009b460SYinghai Lu /* 9995009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 10005009b460SYinghai Lu * so we can allocate big new one later 10015009b460SYinghai Lu */ 10025009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, 10035009b460SYinghai Lu unsigned long type, 10045009b460SYinghai Lu enum release_type rel_type) 10055009b460SYinghai Lu { 10065009b460SYinghai Lu struct pci_dev *dev; 10075009b460SYinghai Lu bool is_leaf_bridge = true; 10085009b460SYinghai Lu 10095009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 10105009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 10115009b460SYinghai Lu if (!b) 10125009b460SYinghai Lu continue; 10135009b460SYinghai Lu 10145009b460SYinghai Lu is_leaf_bridge = false; 10155009b460SYinghai Lu 10165009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 10175009b460SYinghai Lu continue; 10185009b460SYinghai Lu 10195009b460SYinghai Lu if (rel_type == whole_subtree) 10205009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 10215009b460SYinghai Lu whole_subtree); 10225009b460SYinghai Lu } 10235009b460SYinghai Lu 10245009b460SYinghai Lu if (pci_is_root_bus(bus)) 10255009b460SYinghai Lu return; 10265009b460SYinghai Lu 10275009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 10285009b460SYinghai Lu return; 10295009b460SYinghai Lu 10305009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 10315009b460SYinghai Lu pci_bridge_release_resources(bus, type); 10325009b460SYinghai Lu } 10335009b460SYinghai Lu 103476fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 103576fbc263SYinghai Lu { 103689a74eccSBjorn Helgaas struct resource *res; 103776fbc263SYinghai Lu int i; 103876fbc263SYinghai Lu 103989a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 10407c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 104176fbc263SYinghai Lu continue; 104276fbc263SYinghai Lu 1043c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 104476fbc263SYinghai Lu } 104576fbc263SYinghai Lu } 104676fbc263SYinghai Lu 104776fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 104876fbc263SYinghai Lu { 104976fbc263SYinghai Lu struct pci_bus *b; 105076fbc263SYinghai Lu struct pci_dev *dev; 105176fbc263SYinghai Lu 105276fbc263SYinghai Lu 105376fbc263SYinghai Lu pci_bus_dump_res(bus); 105476fbc263SYinghai Lu 105576fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 105676fbc263SYinghai Lu b = dev->subordinate; 105776fbc263SYinghai Lu if (!b) 105876fbc263SYinghai Lu continue; 105976fbc263SYinghai Lu 106076fbc263SYinghai Lu pci_bus_dump_resources(b); 106176fbc263SYinghai Lu } 106276fbc263SYinghai Lu } 106376fbc263SYinghai Lu 1064da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus) 1065da7822e5SYinghai Lu { 1066da7822e5SYinghai Lu int depth = 0; 1067da7822e5SYinghai Lu struct pci_dev *dev; 1068da7822e5SYinghai Lu 1069da7822e5SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 1070da7822e5SYinghai Lu int ret; 1071da7822e5SYinghai Lu struct pci_bus *b = dev->subordinate; 1072da7822e5SYinghai Lu if (!b) 1073da7822e5SYinghai Lu continue; 1074da7822e5SYinghai Lu 1075da7822e5SYinghai Lu ret = pci_bus_get_depth(b); 1076da7822e5SYinghai Lu if (ret + 1 > depth) 1077da7822e5SYinghai Lu depth = ret + 1; 1078da7822e5SYinghai Lu } 1079da7822e5SYinghai Lu 1080da7822e5SYinghai Lu return depth; 1081da7822e5SYinghai Lu } 1082da7822e5SYinghai Lu static int __init pci_get_max_depth(void) 1083da7822e5SYinghai Lu { 1084da7822e5SYinghai Lu int depth = 0; 1085da7822e5SYinghai Lu struct pci_bus *bus; 1086da7822e5SYinghai Lu 1087da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) { 1088da7822e5SYinghai Lu int ret; 1089da7822e5SYinghai Lu 1090da7822e5SYinghai Lu ret = pci_bus_get_depth(bus); 1091da7822e5SYinghai Lu if (ret > depth) 1092da7822e5SYinghai Lu depth = ret; 1093da7822e5SYinghai Lu } 1094da7822e5SYinghai Lu 1095da7822e5SYinghai Lu return depth; 1096da7822e5SYinghai Lu } 1097da7822e5SYinghai Lu 1098f483d392SRam Pai 1099da7822e5SYinghai Lu /* 1100da7822e5SYinghai Lu * first try will not touch pci bridge res 1101da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1102da7822e5SYinghai Lu * will stop till to the max deepth if can not find good one 1103da7822e5SYinghai Lu */ 11041da177e4SLinus Torvalds void __init 11051da177e4SLinus Torvalds pci_assign_unassigned_resources(void) 11061da177e4SLinus Torvalds { 11071da177e4SLinus Torvalds struct pci_bus *bus; 1108c8adf9a3SRam Pai struct resource_list_x add_list; /* list of resources that 1109c8adf9a3SRam Pai want additional resources */ 1110da7822e5SYinghai Lu int tried_times = 0; 1111da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1112da7822e5SYinghai Lu struct resource_list_x head, *list; 1113da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1114da7822e5SYinghai Lu IORESOURCE_PREFETCH; 1115da7822e5SYinghai Lu unsigned long failed_type; 1116da7822e5SYinghai Lu int max_depth = pci_get_max_depth(); 1117da7822e5SYinghai Lu int pci_try_num; 1118da7822e5SYinghai Lu 1119da7822e5SYinghai Lu 1120da7822e5SYinghai Lu head.next = NULL; 1121c8adf9a3SRam Pai add_list.next = NULL; 1122da7822e5SYinghai Lu 1123da7822e5SYinghai Lu pci_try_num = max_depth + 1; 1124da7822e5SYinghai Lu printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n", 1125da7822e5SYinghai Lu max_depth, pci_try_num); 1126da7822e5SYinghai Lu 1127da7822e5SYinghai Lu again: 11281da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 11291da177e4SLinus Torvalds subordinate buses. */ 1130da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 1131c8adf9a3SRam Pai __pci_bus_size_bridges(bus, &add_list); 1132c8adf9a3SRam Pai 11331da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1134da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 1135da7822e5SYinghai Lu __pci_bus_assign_resources(bus, &add_list, &head); 1136c8adf9a3SRam Pai BUG_ON(add_list.next); 1137da7822e5SYinghai Lu tried_times++; 1138da7822e5SYinghai Lu 1139da7822e5SYinghai Lu /* any device complain? */ 1140da7822e5SYinghai Lu if (!head.next) 1141da7822e5SYinghai Lu goto enable_and_dump; 1142f483d392SRam Pai 1143f483d392SRam Pai /* don't realloc if asked to do so */ 1144f483d392SRam Pai if (!pci_realloc_enabled()) { 1145f483d392SRam Pai free_list(resource_list_x, &head); 1146f483d392SRam Pai goto enable_and_dump; 1147f483d392SRam Pai } 1148f483d392SRam Pai 1149da7822e5SYinghai Lu failed_type = 0; 1150da7822e5SYinghai Lu for (list = head.next; list;) { 1151da7822e5SYinghai Lu failed_type |= list->flags; 1152da7822e5SYinghai Lu list = list->next; 1153da7822e5SYinghai Lu } 1154da7822e5SYinghai Lu /* 1155da7822e5SYinghai Lu * io port are tight, don't try extra 1156da7822e5SYinghai Lu * or if reach the limit, don't want to try more 1157da7822e5SYinghai Lu */ 1158da7822e5SYinghai Lu failed_type &= type_mask; 1159da7822e5SYinghai Lu if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) { 1160da7822e5SYinghai Lu free_list(resource_list_x, &head); 1161da7822e5SYinghai Lu goto enable_and_dump; 1162da7822e5SYinghai Lu } 1163da7822e5SYinghai Lu 1164da7822e5SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 1165da7822e5SYinghai Lu tried_times + 1); 1166da7822e5SYinghai Lu 1167da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1168da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1169da7822e5SYinghai Lu rel_type = whole_subtree; 1170da7822e5SYinghai Lu 1171da7822e5SYinghai Lu /* 1172da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1173da7822e5SYinghai Lu * child device under that bridge 1174da7822e5SYinghai Lu */ 1175da7822e5SYinghai Lu for (list = head.next; list;) { 1176da7822e5SYinghai Lu bus = list->dev->bus; 1177da7822e5SYinghai Lu pci_bus_release_bridge_resources(bus, list->flags & type_mask, 1178da7822e5SYinghai Lu rel_type); 1179da7822e5SYinghai Lu list = list->next; 1180da7822e5SYinghai Lu } 1181da7822e5SYinghai Lu /* restore size and flags */ 1182da7822e5SYinghai Lu for (list = head.next; list;) { 1183da7822e5SYinghai Lu struct resource *res = list->res; 1184da7822e5SYinghai Lu 1185da7822e5SYinghai Lu res->start = list->start; 1186da7822e5SYinghai Lu res->end = list->end; 1187da7822e5SYinghai Lu res->flags = list->flags; 1188da7822e5SYinghai Lu if (list->dev->subordinate) 1189da7822e5SYinghai Lu res->flags = 0; 1190da7822e5SYinghai Lu 1191da7822e5SYinghai Lu list = list->next; 1192da7822e5SYinghai Lu } 1193da7822e5SYinghai Lu free_list(resource_list_x, &head); 1194da7822e5SYinghai Lu 1195da7822e5SYinghai Lu goto again; 1196da7822e5SYinghai Lu 1197da7822e5SYinghai Lu enable_and_dump: 1198da7822e5SYinghai Lu /* Depth last, update the hardware. */ 1199da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 1200da7822e5SYinghai Lu pci_enable_bridges(bus); 120176fbc263SYinghai Lu 120276fbc263SYinghai Lu /* dump the resource on buses */ 1203da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 120476fbc263SYinghai Lu pci_bus_dump_resources(bus); 120576fbc263SYinghai Lu } 12066841ec68SYinghai Lu 12076841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 12086841ec68SYinghai Lu { 12096841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 121032180e40SYinghai Lu int tried_times = 0; 121132180e40SYinghai Lu struct resource_list_x head, *list; 12126841ec68SYinghai Lu int retval; 121332180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 121432180e40SYinghai Lu IORESOURCE_PREFETCH; 12156841ec68SYinghai Lu 121632180e40SYinghai Lu head.next = NULL; 121732180e40SYinghai Lu 121832180e40SYinghai Lu again: 12196841ec68SYinghai Lu pci_bus_size_bridges(parent); 122032180e40SYinghai Lu __pci_bridge_assign_resources(bridge, &head); 122132180e40SYinghai Lu 122232180e40SYinghai Lu tried_times++; 122332180e40SYinghai Lu 122432180e40SYinghai Lu if (!head.next) 12253f579c34SYinghai Lu goto enable_all; 122632180e40SYinghai Lu 122732180e40SYinghai Lu if (tried_times >= 2) { 122832180e40SYinghai Lu /* still fail, don't need to try more */ 1229094732a5SRam Pai free_list(resource_list_x, &head); 12303f579c34SYinghai Lu goto enable_all; 123132180e40SYinghai Lu } 123232180e40SYinghai Lu 123332180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 123432180e40SYinghai Lu tried_times + 1); 123532180e40SYinghai Lu 123632180e40SYinghai Lu /* 123732180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 123832180e40SYinghai Lu * child device under that bridge 123932180e40SYinghai Lu */ 124032180e40SYinghai Lu for (list = head.next; list;) { 124132180e40SYinghai Lu struct pci_bus *bus = list->dev->bus; 124232180e40SYinghai Lu unsigned long flags = list->flags; 124332180e40SYinghai Lu 124432180e40SYinghai Lu pci_bus_release_bridge_resources(bus, flags & type_mask, 124532180e40SYinghai Lu whole_subtree); 124632180e40SYinghai Lu list = list->next; 124732180e40SYinghai Lu } 124832180e40SYinghai Lu /* restore size and flags */ 124932180e40SYinghai Lu for (list = head.next; list;) { 125032180e40SYinghai Lu struct resource *res = list->res; 125132180e40SYinghai Lu 125232180e40SYinghai Lu res->start = list->start; 125332180e40SYinghai Lu res->end = list->end; 125432180e40SYinghai Lu res->flags = list->flags; 125532180e40SYinghai Lu if (list->dev->subordinate) 125632180e40SYinghai Lu res->flags = 0; 125732180e40SYinghai Lu 125832180e40SYinghai Lu list = list->next; 125932180e40SYinghai Lu } 1260094732a5SRam Pai free_list(resource_list_x, &head); 126132180e40SYinghai Lu 126232180e40SYinghai Lu goto again; 12633f579c34SYinghai Lu 12643f579c34SYinghai Lu enable_all: 12653f579c34SYinghai Lu retval = pci_reenable_device(bridge); 12663f579c34SYinghai Lu pci_set_master(bridge); 12673f579c34SYinghai Lu pci_enable_bridges(parent); 12686841ec68SYinghai Lu } 12696841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 1270