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 */ 67*ef62dfefSYinghai Lu static int 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"); 78*ef62dfefSYinghai Lu return -ENOMEM; 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; 90*ef62dfefSYinghai Lu 91*ef62dfefSYinghai Lu return 0; 92568ddef8SYinghai Lu } 93568ddef8SYinghai Lu 94c8adf9a3SRam Pai static void add_to_failed_list(struct resource_list_x *head, 95c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res) 96c8adf9a3SRam Pai { 972bbc6942SRam Pai add_to_list(head, dev, res, 982bbc6942SRam Pai 0 /* dont care */, 992bbc6942SRam Pai 0 /* dont care */); 100c8adf9a3SRam Pai } 101c8adf9a3SRam Pai 1026841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev, 1036841ec68SYinghai Lu struct resource_list *head) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds u16 class = dev->class >> 8; 1061da177e4SLinus Torvalds 1079bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 1086841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1096841ec68SYinghai Lu return; 1101da177e4SLinus Torvalds 1119bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 11223186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 1139bded00bSKenji Kaneshige u16 command; 1149bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 1159bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 1166841ec68SYinghai Lu return; 11723186279SSatoru Takeuchi } 11823186279SSatoru Takeuchi 1196841ec68SYinghai Lu pdev_sort_resources(dev, head); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 122fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 123fc075e1dSRam Pai { 124fc075e1dSRam Pai res->start = 0; 125fc075e1dSRam Pai res->end = 0; 126fc075e1dSRam Pai res->flags = 0; 127fc075e1dSRam Pai } 128fc075e1dSRam Pai 129c8adf9a3SRam Pai /** 1309e8bf93aSRam Pai * reassign_resources_sorted() - satisfy any additional resource requests 131c8adf9a3SRam Pai * 1329e8bf93aSRam Pai * @realloc_head : head of the list tracking requests requiring additional 133c8adf9a3SRam Pai * resources 134c8adf9a3SRam Pai * @head : head of the list tracking requests with allocated 135c8adf9a3SRam Pai * resources 136c8adf9a3SRam Pai * 1379e8bf93aSRam Pai * Walk through each element of the realloc_head and try to procure 138c8adf9a3SRam Pai * additional resources for the element, provided the element 139c8adf9a3SRam Pai * is in the head list. 140c8adf9a3SRam Pai */ 1419e8bf93aSRam Pai static void reassign_resources_sorted(struct resource_list_x *realloc_head, 142c8adf9a3SRam Pai struct resource_list *head) 143c8adf9a3SRam Pai { 144c8adf9a3SRam Pai struct resource *res; 145c8adf9a3SRam Pai struct resource_list_x *list, *tmp, *prev; 146c8adf9a3SRam Pai struct resource_list *hlist; 147c8adf9a3SRam Pai resource_size_t add_size; 148c8adf9a3SRam Pai int idx; 149c8adf9a3SRam Pai 1509e8bf93aSRam Pai prev = realloc_head; 1519e8bf93aSRam Pai for (list = realloc_head->next; list;) { 152c8adf9a3SRam Pai res = list->res; 153c8adf9a3SRam Pai /* skip resource that has been reset */ 154c8adf9a3SRam Pai if (!res->flags) 155c8adf9a3SRam Pai goto out; 156c8adf9a3SRam Pai 157c8adf9a3SRam Pai /* skip this resource if not found in head list */ 158c8adf9a3SRam Pai for (hlist = head->next; hlist && hlist->res != res; 159c8adf9a3SRam Pai hlist = hlist->next); 160c8adf9a3SRam Pai if (!hlist) { /* just skip */ 161c8adf9a3SRam Pai prev = list; 162c8adf9a3SRam Pai list = list->next; 163c8adf9a3SRam Pai continue; 164c8adf9a3SRam Pai } 165c8adf9a3SRam Pai 166c8adf9a3SRam Pai idx = res - &list->dev->resource[0]; 167c8adf9a3SRam Pai add_size=list->add_size; 1682bbc6942SRam Pai if (!resource_size(res)) { 1690a2daa1cSRam Pai res->start = list->start; 170c8adf9a3SRam Pai res->end = res->start + add_size - 1; 171c8adf9a3SRam Pai if(pci_assign_resource(list->dev, idx)) 172c8adf9a3SRam Pai reset_resource(res); 1732bbc6942SRam Pai } else { 1742bbc6942SRam Pai resource_size_t align = list->min_align; 1752bbc6942SRam Pai res->flags |= list->flags & (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 1762bbc6942SRam Pai if (pci_reassign_resource(list->dev, idx, add_size, align)) 1772bbc6942SRam Pai dev_printk(KERN_DEBUG, &list->dev->dev, "failed to add optional resources res=%pR\n", 1782bbc6942SRam Pai res); 179c8adf9a3SRam Pai } 180c8adf9a3SRam Pai out: 181c8adf9a3SRam Pai tmp = list; 182c8adf9a3SRam Pai prev->next = list = list->next; 183c8adf9a3SRam Pai kfree(tmp); 184c8adf9a3SRam Pai } 185c8adf9a3SRam Pai } 186c8adf9a3SRam Pai 187c8adf9a3SRam Pai /** 188c8adf9a3SRam Pai * assign_requested_resources_sorted() - satisfy resource requests 189c8adf9a3SRam Pai * 190c8adf9a3SRam Pai * @head : head of the list tracking requests for resources 191c8adf9a3SRam Pai * @failed_list : head of the list tracking requests that could 192c8adf9a3SRam Pai * not be allocated 193c8adf9a3SRam Pai * 194c8adf9a3SRam Pai * Satisfy resource requests of each element in the list. Add 195c8adf9a3SRam Pai * requests that could not satisfied to the failed_list. 196c8adf9a3SRam Pai */ 197c8adf9a3SRam Pai static void assign_requested_resources_sorted(struct resource_list *head, 1986841ec68SYinghai Lu struct resource_list_x *fail_head) 1996841ec68SYinghai Lu { 2006841ec68SYinghai Lu struct resource *res; 201c8adf9a3SRam Pai struct resource_list *list; 2026841ec68SYinghai Lu int idx; 2036841ec68SYinghai Lu 204c8adf9a3SRam Pai for (list = head->next; list; list = list->next) { 2051da177e4SLinus Torvalds res = list->res; 2061da177e4SLinus Torvalds idx = res - &list->dev->resource[0]; 207c8adf9a3SRam Pai if (resource_size(res) && pci_assign_resource(list->dev, idx)) { 2089a928660SYinghai Lu if (fail_head && !pci_is_root_bus(list->dev->bus)) { 2099a928660SYinghai Lu /* 2109a928660SYinghai Lu * if the failed res is for ROM BAR, and it will 2119a928660SYinghai Lu * be enabled later, don't add it to the list 2129a928660SYinghai Lu */ 2139a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 2149a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 215568ddef8SYinghai Lu add_to_failed_list(fail_head, list->dev, res); 2169a928660SYinghai Lu } 217fc075e1dSRam Pai reset_resource(res); 218542df5deSRajesh Shah } 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 222c8adf9a3SRam Pai static void __assign_resources_sorted(struct resource_list *head, 2239e8bf93aSRam Pai struct resource_list_x *realloc_head, 224c8adf9a3SRam Pai struct resource_list_x *fail_head) 225c8adf9a3SRam Pai { 226c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 227c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 228c8adf9a3SRam Pai 2290a2daa1cSRam Pai /* Try to satisfy any additional optional resource 230c8adf9a3SRam Pai requests */ 2319e8bf93aSRam Pai if (realloc_head) 2329e8bf93aSRam Pai reassign_resources_sorted(realloc_head, head); 233c8adf9a3SRam Pai free_list(resource_list, head); 234c8adf9a3SRam Pai } 235c8adf9a3SRam Pai 2366841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 2376841ec68SYinghai Lu struct resource_list_x *fail_head) 2386841ec68SYinghai Lu { 2396841ec68SYinghai Lu struct resource_list head; 2406841ec68SYinghai Lu 2416841ec68SYinghai Lu head.next = NULL; 2426841ec68SYinghai Lu __dev_sort_resources(dev, &head); 243c8adf9a3SRam Pai __assign_resources_sorted(&head, NULL, fail_head); 2446841ec68SYinghai Lu 2456841ec68SYinghai Lu } 2466841ec68SYinghai Lu 2476841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 2489e8bf93aSRam Pai struct resource_list_x *realloc_head, 2496841ec68SYinghai Lu struct resource_list_x *fail_head) 2506841ec68SYinghai Lu { 2516841ec68SYinghai Lu struct pci_dev *dev; 2526841ec68SYinghai Lu struct resource_list head; 2536841ec68SYinghai Lu 2546841ec68SYinghai Lu head.next = NULL; 2556841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 2566841ec68SYinghai Lu __dev_sort_resources(dev, &head); 2576841ec68SYinghai Lu 2589e8bf93aSRam Pai __assign_resources_sorted(&head, realloc_head, fail_head); 2596841ec68SYinghai Lu } 2606841ec68SYinghai Lu 261b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 264c7dabef8SBjorn Helgaas struct resource *res; 2651da177e4SLinus Torvalds struct pci_bus_region region; 2661da177e4SLinus Torvalds 267865df576SBjorn Helgaas dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", 268865df576SBjorn Helgaas bus->secondary, bus->subordinate); 2691da177e4SLinus Torvalds 270c7dabef8SBjorn Helgaas res = bus->resource[0]; 271c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 272c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 2731da177e4SLinus Torvalds /* 2741da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 2751da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 2761da177e4SLinus Torvalds */ 277c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 2781da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 2791da177e4SLinus Torvalds region.start); 2801da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 2811da177e4SLinus Torvalds region.end); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 284c7dabef8SBjorn Helgaas res = bus->resource[1]; 285c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 286c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 287c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 2881da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 2891da177e4SLinus Torvalds region.start); 2901da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 2911da177e4SLinus Torvalds region.end); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 294c7dabef8SBjorn Helgaas res = bus->resource[2]; 295c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 296c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 297c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 2981da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 2991da177e4SLinus Torvalds region.start); 3001da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 3011da177e4SLinus Torvalds region.end); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 304c7dabef8SBjorn Helgaas res = bus->resource[3]; 305c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 306c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 307c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 3081da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 3091da177e4SLinus Torvalds region.start); 3101da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 3111da177e4SLinus Torvalds region.end); 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds } 314b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 3171da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 3181da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 3191da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 3201da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 3231da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 3241da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 3251da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 3261da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 3277cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus) 3281da177e4SLinus Torvalds { 3291da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 330c7dabef8SBjorn Helgaas struct resource *res; 3311da177e4SLinus Torvalds struct pci_bus_region region; 3327cc5997dSYinghai Lu u32 l, io_upper16; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 335c7dabef8SBjorn Helgaas res = bus->resource[0]; 336c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 337c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 3381da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_IO_BASE, &l); 3391da177e4SLinus Torvalds l &= 0xffff0000; 3401da177e4SLinus Torvalds l |= (region.start >> 8) & 0x00f0; 3411da177e4SLinus Torvalds l |= region.end & 0xf000; 3421da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 3431da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 344c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 3457cc5997dSYinghai Lu } else { 3461da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 3471da177e4SLinus Torvalds io_upper16 = 0; 3481da177e4SLinus Torvalds l = 0x00f0; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 3511da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 3521da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 3531da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE, l); 3541da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 3551da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 3567cc5997dSYinghai Lu } 3571da177e4SLinus Torvalds 3587cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus) 3597cc5997dSYinghai Lu { 3607cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 3617cc5997dSYinghai Lu struct resource *res; 3627cc5997dSYinghai Lu struct pci_bus_region region; 3637cc5997dSYinghai Lu u32 l; 3647cc5997dSYinghai Lu 3657cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 366c7dabef8SBjorn Helgaas res = bus->resource[1]; 367c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 368c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 3691da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 3701da177e4SLinus Torvalds l |= region.end & 0xfff00000; 371c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 3727cc5997dSYinghai Lu } else { 3731da177e4SLinus Torvalds l = 0x0000fff0; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 3767cc5997dSYinghai Lu } 3777cc5997dSYinghai Lu 3787cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) 3797cc5997dSYinghai Lu { 3807cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 3817cc5997dSYinghai Lu struct resource *res; 3827cc5997dSYinghai Lu struct pci_bus_region region; 3837cc5997dSYinghai Lu u32 l, bu, lu; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 3861da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 3871da177e4SLinus Torvalds disables PREF range, which is ok. */ 3881da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds /* Set up PREF base/limit. */ 391c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 392c7dabef8SBjorn Helgaas res = bus->resource[2]; 393c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 394c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 3951da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 3961da177e4SLinus Torvalds l |= region.end & 0xfff00000; 397c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 39813d36c24SAndrew Morton bu = upper_32_bits(region.start); 39913d36c24SAndrew Morton lu = upper_32_bits(region.end); 4001f82de10SYinghai Lu } 401c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4027cc5997dSYinghai Lu } else { 4031da177e4SLinus Torvalds l = 0x0000fff0; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 4061da177e4SLinus Torvalds 407c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 408c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 409c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 4107cc5997dSYinghai Lu } 4117cc5997dSYinghai Lu 4127cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 4137cc5997dSYinghai Lu { 4147cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 4157cc5997dSYinghai Lu 4167cc5997dSYinghai Lu dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", 4177cc5997dSYinghai Lu bus->secondary, bus->subordinate); 4187cc5997dSYinghai Lu 4197cc5997dSYinghai Lu if (type & IORESOURCE_IO) 4207cc5997dSYinghai Lu pci_setup_bridge_io(bus); 4217cc5997dSYinghai Lu 4227cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 4237cc5997dSYinghai Lu pci_setup_bridge_mmio(bus); 4247cc5997dSYinghai Lu 4257cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 4267cc5997dSYinghai Lu pci_setup_bridge_mmio_pref(bus); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 4327cc5997dSYinghai Lu { 4337cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 4347cc5997dSYinghai Lu IORESOURCE_PREFETCH; 4357cc5997dSYinghai Lu 4367cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 4377cc5997dSYinghai Lu } 4387cc5997dSYinghai Lu 4391da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 4401da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 4411da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 44296bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds u16 io; 4451da177e4SLinus Torvalds u32 pmem; 4461da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 4471da177e4SLinus Torvalds struct resource *b_res; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 4501da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 4531da177e4SLinus Torvalds if (!io) { 4541da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 4551da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 4561da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds if (io) 4591da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 4601da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 4611da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 4621da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 4631da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 4641da177e4SLinus Torvalds return; 4651da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 4661da177e4SLinus Torvalds if (!pmem) { 4671da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 4681da177e4SLinus Torvalds 0xfff0fff0); 4691da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 4701da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 4711da177e4SLinus Torvalds } 4721f82de10SYinghai Lu if (pmem) { 4731da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 47499586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 47599586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 4761f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 47799586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 47899586105SYinghai Lu } 4791f82de10SYinghai Lu } 4801f82de10SYinghai Lu 4811f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 4821f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 4831f82de10SYinghai Lu u32 mem_base_hi, tmp; 4841f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 4851f82de10SYinghai Lu &mem_base_hi); 4861f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 4871f82de10SYinghai Lu 0xffffffff); 4881f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 4891f82de10SYinghai Lu if (!tmp) 4901f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 4911f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 4921f82de10SYinghai Lu mem_base_hi); 4931f82de10SYinghai Lu } 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 4971da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 4981da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 4991da177e4SLinus Torvalds have non-NULL parent resource). */ 50096bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds int i; 5031da177e4SLinus Torvalds struct resource *r; 5041da177e4SLinus Torvalds unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 5051da177e4SLinus Torvalds IORESOURCE_PREFETCH; 5061da177e4SLinus Torvalds 50789a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 508299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 509299de034SIvan Kokshaysky continue; 51055a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 5111da177e4SLinus Torvalds return r; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds return NULL; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 51613583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 51713583b16SRam Pai resource_size_t min_size, 51813583b16SRam Pai resource_size_t size1, 51913583b16SRam Pai resource_size_t old_size, 52013583b16SRam Pai resource_size_t align) 52113583b16SRam Pai { 52213583b16SRam Pai if (size < min_size) 52313583b16SRam Pai size = min_size; 52413583b16SRam Pai if (old_size == 1 ) 52513583b16SRam Pai old_size = 0; 52613583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 52713583b16SRam Pai flag in the struct pci_bus. */ 52813583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 52913583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 53013583b16SRam Pai #endif 53113583b16SRam Pai size = ALIGN(size + size1, align); 53213583b16SRam Pai if (size < old_size) 53313583b16SRam Pai size = old_size; 53413583b16SRam Pai return size; 53513583b16SRam Pai } 53613583b16SRam Pai 53713583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 53813583b16SRam Pai resource_size_t min_size, 53913583b16SRam Pai resource_size_t size1, 54013583b16SRam Pai resource_size_t old_size, 54113583b16SRam Pai resource_size_t align) 54213583b16SRam Pai { 54313583b16SRam Pai if (size < min_size) 54413583b16SRam Pai size = min_size; 54513583b16SRam Pai if (old_size == 1 ) 54613583b16SRam Pai old_size = 0; 54713583b16SRam Pai if (size < old_size) 54813583b16SRam Pai size = old_size; 54913583b16SRam Pai size = ALIGN(size + size1, align); 55013583b16SRam Pai return size; 55113583b16SRam Pai } 55213583b16SRam Pai 5539e8bf93aSRam Pai static resource_size_t get_res_add_size(struct resource_list_x *realloc_head, 554be768912SYinghai Lu struct resource *res) 555be768912SYinghai Lu { 556be768912SYinghai Lu struct resource_list_x *list; 557be768912SYinghai Lu 5589e8bf93aSRam Pai /* check if it is in realloc_head list */ 5599e8bf93aSRam Pai for (list = realloc_head->next; list && list->res != res; 560be768912SYinghai Lu list = list->next); 561be768912SYinghai Lu if (list) 562be768912SYinghai Lu return list->add_size; 563be768912SYinghai Lu 564be768912SYinghai Lu return 0; 565be768912SYinghai Lu } 566be768912SYinghai Lu 567c8adf9a3SRam Pai /** 568c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 569c8adf9a3SRam Pai * 570c8adf9a3SRam Pai * @bus : the bus 571c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 572c8adf9a3SRam Pai * @add_size : additional optional io window 5739e8bf93aSRam Pai * @realloc_head : track the additional io window on this list 574c8adf9a3SRam Pai * 575c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 576c8adf9a3SRam Pai * since these windows have 4K granularity and the IO ranges 577c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 578c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 579c8adf9a3SRam Pai */ 580c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 5819e8bf93aSRam Pai resource_size_t add_size, struct resource_list_x *realloc_head) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds struct pci_dev *dev; 5841da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 585c8adf9a3SRam Pai unsigned long size = 0, size0 = 0, size1 = 0; 586be768912SYinghai Lu resource_size_t children_add_size = 0; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds if (!b_res) 5891da177e4SLinus Torvalds return; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 5921da177e4SLinus Torvalds int i; 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 5951da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 5961da177e4SLinus Torvalds unsigned long r_size; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 5991da177e4SLinus Torvalds continue; 600022edd86SZhao, Yu r_size = resource_size(r); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds if (r_size < 0x400) 6031da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 6041da177e4SLinus Torvalds size += r_size; 6051da177e4SLinus Torvalds else 6061da177e4SLinus Torvalds size1 += r_size; 607be768912SYinghai Lu 6089e8bf93aSRam Pai if (realloc_head) 6099e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds } 612c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 61313583b16SRam Pai resource_size(b_res), 4096); 614be768912SYinghai Lu if (children_add_size > add_size) 615be768912SYinghai Lu add_size = children_add_size; 6169e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 617a4ac9feaSYinghai Lu calculate_iosize(size, min_size, add_size + size1, 618c8adf9a3SRam Pai resource_size(b_res), 4096); 619c8adf9a3SRam Pai if (!size0 && !size1) { 620865df576SBjorn Helgaas if (b_res->start || b_res->end) 621865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 622865df576SBjorn Helgaas "%pR to [bus %02x-%02x] (unused)\n", b_res, 623865df576SBjorn Helgaas bus->secondary, bus->subordinate); 6241da177e4SLinus Torvalds b_res->flags = 0; 6251da177e4SLinus Torvalds return; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds /* Alignment of the IO window is always 4K */ 6281da177e4SLinus Torvalds b_res->start = 4096; 629c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 63088452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 6319e8bf93aSRam Pai if (size1 > size0 && realloc_head) 6329e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 635c8adf9a3SRam Pai /** 636c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 637c8adf9a3SRam Pai * 638c8adf9a3SRam Pai * @bus : the bus 639c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 640c8adf9a3SRam Pai * @add_size : additional optional memory window 6419e8bf93aSRam Pai * @realloc_head : track the additional memory window on this list 642c8adf9a3SRam Pai * 643c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 644c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 645c8adf9a3SRam Pai */ 64628760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 647c8adf9a3SRam Pai unsigned long type, resource_size_t min_size, 648c8adf9a3SRam Pai resource_size_t add_size, 6499e8bf93aSRam Pai struct resource_list_x *realloc_head) 6501da177e4SLinus Torvalds { 6511da177e4SLinus Torvalds struct pci_dev *dev; 652c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 653c40a22e0SBenjamin Herrenschmidt resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 6541da177e4SLinus Torvalds int order, max_order; 6551da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, type); 6561f82de10SYinghai Lu unsigned int mem64_mask = 0; 657be768912SYinghai Lu resource_size_t children_add_size = 0; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (!b_res) 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 6631da177e4SLinus Torvalds max_order = 0; 6641da177e4SLinus Torvalds size = 0; 6651da177e4SLinus Torvalds 6661f82de10SYinghai Lu mem64_mask = b_res->flags & IORESOURCE_MEM_64; 6671f82de10SYinghai Lu b_res->flags &= ~IORESOURCE_MEM_64; 6681f82de10SYinghai Lu 6691da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 6701da177e4SLinus Torvalds int i; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 6731da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 674c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds if (r->parent || (r->flags & mask) != type) 6771da177e4SLinus Torvalds continue; 678022edd86SZhao, Yu r_size = resource_size(r); 6792aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 6802aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 6819e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 6822aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 6832aceefcbSYinghai Lu r->end = r->start - 1; 6849e8bf93aSRam Pai add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */); 6852aceefcbSYinghai Lu children_add_size += r_size; 6862aceefcbSYinghai Lu continue; 6872aceefcbSYinghai Lu } 6882aceefcbSYinghai Lu #endif 6891da177e4SLinus Torvalds /* For bridges size != alignment */ 6906faf17f6SChris Wright align = pci_resource_alignment(dev, r); 6911da177e4SLinus Torvalds order = __ffs(align) - 20; 6921da177e4SLinus Torvalds if (order > 11) { 693865df576SBjorn Helgaas dev_warn(&dev->dev, "disabling BAR %d: %pR " 694865df576SBjorn Helgaas "(bad alignment %#llx)\n", i, r, 695865df576SBjorn Helgaas (unsigned long long) align); 6961da177e4SLinus Torvalds r->flags = 0; 6971da177e4SLinus Torvalds continue; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds size += r_size; 7001da177e4SLinus Torvalds if (order < 0) 7011da177e4SLinus Torvalds order = 0; 7021da177e4SLinus Torvalds /* Exclude ranges with size > align from 7031da177e4SLinus Torvalds calculation of the alignment. */ 7041da177e4SLinus Torvalds if (r_size == align) 7051da177e4SLinus Torvalds aligns[order] += align; 7061da177e4SLinus Torvalds if (order > max_order) 7071da177e4SLinus Torvalds max_order = order; 7081f82de10SYinghai Lu mem64_mask &= r->flags & IORESOURCE_MEM_64; 709be768912SYinghai Lu 7109e8bf93aSRam Pai if (realloc_head) 7119e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds align = 0; 7151da177e4SLinus Torvalds min_align = 0; 7161da177e4SLinus Torvalds for (order = 0; order <= max_order; order++) { 7178308c54dSJeremy Fitzhardinge resource_size_t align1 = 1; 7188308c54dSJeremy Fitzhardinge 7198308c54dSJeremy Fitzhardinge align1 <<= (order + 20); 7208308c54dSJeremy Fitzhardinge 7211da177e4SLinus Torvalds if (!align) 7221da177e4SLinus Torvalds min_align = align1; 7236f6f8c2fSMilind Arun Choudhary else if (ALIGN(align + min_align, min_align) < align1) 7241da177e4SLinus Torvalds min_align = align1 >> 1; 7251da177e4SLinus Torvalds align += aligns[order]; 7261da177e4SLinus Torvalds } 727b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 728be768912SYinghai Lu if (children_add_size > add_size) 729be768912SYinghai Lu add_size = children_add_size; 7309e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 731a4ac9feaSYinghai Lu calculate_memsize(size, min_size, add_size, 732b42282e5SLinus Torvalds resource_size(b_res), min_align); 733c8adf9a3SRam Pai if (!size0 && !size1) { 734865df576SBjorn Helgaas if (b_res->start || b_res->end) 735865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 736865df576SBjorn Helgaas "%pR to [bus %02x-%02x] (unused)\n", b_res, 737865df576SBjorn Helgaas bus->secondary, bus->subordinate); 7381da177e4SLinus Torvalds b_res->flags = 0; 7391da177e4SLinus Torvalds return 1; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds b_res->start = min_align; 742c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 743c8adf9a3SRam Pai b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; 7449e8bf93aSRam Pai if (size1 > size0 && realloc_head) 7459e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 7461da177e4SLinus Torvalds return 1; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7490a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 7500a2daa1cSRam Pai { 7510a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 7520a2daa1cSRam Pai return pci_cardbus_io_size; 7530a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 7540a2daa1cSRam Pai return pci_cardbus_mem_size; 7550a2daa1cSRam Pai return 0; 7560a2daa1cSRam Pai } 7570a2daa1cSRam Pai 7580a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 7599e8bf93aSRam Pai struct resource_list_x *realloc_head) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 7621da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 7631da177e4SLinus Torvalds u16 ctrl; 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* 7661da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 7671da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 7681da177e4SLinus Torvalds */ 769934b7024SLinus Torvalds b_res[0].start = 0; 770934b7024SLinus Torvalds b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 7719e8bf93aSRam Pai if (realloc_head) 7729e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */); 7731da177e4SLinus Torvalds 774934b7024SLinus Torvalds b_res[1].start = 0; 775934b7024SLinus Torvalds b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 7769e8bf93aSRam Pai if (realloc_head) 7779e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */); 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* 7801da177e4SLinus Torvalds * Check whether prefetchable memory is supported 7811da177e4SLinus Torvalds * by this bridge. 7821da177e4SLinus Torvalds */ 7831da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 7841da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 7851da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 7861da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 7871da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds /* 7911da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 7921da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 7931da177e4SLinus Torvalds * twice the size. 7941da177e4SLinus Torvalds */ 7951da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 796934b7024SLinus Torvalds b_res[2].start = 0; 797934b7024SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 7989e8bf93aSRam Pai if (realloc_head) 7999e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */); 8001da177e4SLinus Torvalds 801934b7024SLinus Torvalds b_res[3].start = 0; 802934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 8039e8bf93aSRam Pai if (realloc_head) 8049e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */); 8051da177e4SLinus Torvalds } else { 806934b7024SLinus Torvalds b_res[3].start = 0; 807934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 8089e8bf93aSRam Pai if (realloc_head) 8099e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */); 8101da177e4SLinus Torvalds } 8110a2daa1cSRam Pai 8120a2daa1cSRam Pai /* set the size of the resource to zero, so that the resource does not 8130a2daa1cSRam Pai * get assigned during required-resource allocation cycle but gets assigned 8140a2daa1cSRam Pai * during the optional-resource allocation cycle. 8150a2daa1cSRam Pai */ 8160a2daa1cSRam Pai b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1; 8170a2daa1cSRam Pai b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 820c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus, 8219e8bf93aSRam Pai struct resource_list_x *realloc_head) 8221da177e4SLinus Torvalds { 8231da177e4SLinus Torvalds struct pci_dev *dev; 8241da177e4SLinus Torvalds unsigned long mask, prefmask; 825c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8281da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 8291da177e4SLinus Torvalds if (!b) 8301da177e4SLinus Torvalds continue; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds switch (dev->class >> 8) { 8331da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 8349e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 8351da177e4SLinus Torvalds break; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 8381da177e4SLinus Torvalds default: 8399e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 8401da177e4SLinus Torvalds break; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* The root bus? */ 8451da177e4SLinus Torvalds if (!bus->self) 8461da177e4SLinus Torvalds return; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds switch (bus->self->class >> 8) { 8491da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 8501da177e4SLinus Torvalds /* don't size cardbuses yet. */ 8511da177e4SLinus Torvalds break; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 8541da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 85528760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 856c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 857c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 85828760489SEric W. Biederman } 859c8adf9a3SRam Pai /* 860c8adf9a3SRam Pai * Follow thru 861c8adf9a3SRam Pai */ 8621da177e4SLinus Torvalds default: 8639e8bf93aSRam Pai pbus_size_io(bus, 0, additional_io_size, realloc_head); 8641da177e4SLinus Torvalds /* If the bridge supports prefetchable range, size it 8651da177e4SLinus Torvalds separately. If it doesn't, or its prefetchable window 8661da177e4SLinus Torvalds has already been allocated by arch code, try 8671da177e4SLinus Torvalds non-prefetchable range for both types of PCI memory 8681da177e4SLinus Torvalds resources. */ 8691da177e4SLinus Torvalds mask = IORESOURCE_MEM; 8701da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 8719e8bf93aSRam Pai if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, realloc_head)) 8721da177e4SLinus Torvalds mask = prefmask; /* Success, size non-prefetch only. */ 87328760489SEric W. Biederman else 874c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 8759e8bf93aSRam Pai pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, realloc_head); 8761da177e4SLinus Torvalds break; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds } 879c8adf9a3SRam Pai 880c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus) 881c8adf9a3SRam Pai { 882c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 883c8adf9a3SRam Pai } 8841da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 8851da177e4SLinus Torvalds 886568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 8879e8bf93aSRam Pai struct resource_list_x *realloc_head, 888568ddef8SYinghai Lu struct resource_list_x *fail_head) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds struct pci_bus *b; 8911da177e4SLinus Torvalds struct pci_dev *dev; 8921da177e4SLinus Torvalds 8939e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8961da177e4SLinus Torvalds b = dev->subordinate; 8971da177e4SLinus Torvalds if (!b) 8981da177e4SLinus Torvalds continue; 8991da177e4SLinus Torvalds 9009e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds switch (dev->class >> 8) { 9031da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 9046841ec68SYinghai Lu if (!pci_is_enabled(dev)) 9051da177e4SLinus Torvalds pci_setup_bridge(b); 9061da177e4SLinus Torvalds break; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 9091da177e4SLinus Torvalds pci_setup_cardbus(b); 9101da177e4SLinus Torvalds break; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds default: 91380ccba11SBjorn Helgaas dev_info(&dev->dev, "not setting up bridge for bus " 91480ccba11SBjorn Helgaas "%04x:%02x\n", pci_domain_nr(b), b->number); 9151da177e4SLinus Torvalds break; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds } 919568ddef8SYinghai Lu 920568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus) 921568ddef8SYinghai Lu { 922c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 923568ddef8SYinghai Lu } 9241da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 9251da177e4SLinus Torvalds 9266841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 9276841ec68SYinghai Lu struct resource_list_x *fail_head) 9286841ec68SYinghai Lu { 9296841ec68SYinghai Lu struct pci_bus *b; 9306841ec68SYinghai Lu 9316841ec68SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head); 9326841ec68SYinghai Lu 9336841ec68SYinghai Lu b = bridge->subordinate; 9346841ec68SYinghai Lu if (!b) 9356841ec68SYinghai Lu return; 9366841ec68SYinghai Lu 937c8adf9a3SRam Pai __pci_bus_assign_resources(b, NULL, fail_head); 9386841ec68SYinghai Lu 9396841ec68SYinghai Lu switch (bridge->class >> 8) { 9406841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 9416841ec68SYinghai Lu pci_setup_bridge(b); 9426841ec68SYinghai Lu break; 9436841ec68SYinghai Lu 9446841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 9456841ec68SYinghai Lu pci_setup_cardbus(b); 9466841ec68SYinghai Lu break; 9476841ec68SYinghai Lu 9486841ec68SYinghai Lu default: 9496841ec68SYinghai Lu dev_info(&bridge->dev, "not setting up bridge for bus " 9506841ec68SYinghai Lu "%04x:%02x\n", pci_domain_nr(b), b->number); 9516841ec68SYinghai Lu break; 9526841ec68SYinghai Lu } 9536841ec68SYinghai Lu } 9545009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 9555009b460SYinghai Lu unsigned long type) 9565009b460SYinghai Lu { 9575009b460SYinghai Lu int idx; 9585009b460SYinghai Lu bool changed = false; 9595009b460SYinghai Lu struct pci_dev *dev; 9605009b460SYinghai Lu struct resource *r; 9615009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 9625009b460SYinghai Lu IORESOURCE_PREFETCH; 9635009b460SYinghai Lu 9645009b460SYinghai Lu dev = bus->self; 9655009b460SYinghai Lu for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; 9665009b460SYinghai Lu idx++) { 9675009b460SYinghai Lu r = &dev->resource[idx]; 9685009b460SYinghai Lu if ((r->flags & type_mask) != type) 9695009b460SYinghai Lu continue; 9705009b460SYinghai Lu if (!r->parent) 9715009b460SYinghai Lu continue; 9725009b460SYinghai Lu /* 9735009b460SYinghai Lu * if there are children under that, we should release them 9745009b460SYinghai Lu * all 9755009b460SYinghai Lu */ 9765009b460SYinghai Lu release_child_resources(r); 9775009b460SYinghai Lu if (!release_resource(r)) { 9785009b460SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, 9795009b460SYinghai Lu "resource %d %pR released\n", idx, r); 9805009b460SYinghai Lu /* keep the old size */ 9815009b460SYinghai Lu r->end = resource_size(r) - 1; 9825009b460SYinghai Lu r->start = 0; 9835009b460SYinghai Lu r->flags = 0; 9845009b460SYinghai Lu changed = true; 9855009b460SYinghai Lu } 9865009b460SYinghai Lu } 9875009b460SYinghai Lu 9885009b460SYinghai Lu if (changed) { 9895009b460SYinghai Lu /* avoiding touch the one without PREF */ 9905009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 9915009b460SYinghai Lu type = IORESOURCE_PREFETCH; 9925009b460SYinghai Lu __pci_setup_bridge(bus, type); 9935009b460SYinghai Lu } 9945009b460SYinghai Lu } 9955009b460SYinghai Lu 9965009b460SYinghai Lu enum release_type { 9975009b460SYinghai Lu leaf_only, 9985009b460SYinghai Lu whole_subtree, 9995009b460SYinghai Lu }; 10005009b460SYinghai Lu /* 10015009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 10025009b460SYinghai Lu * so we can allocate big new one later 10035009b460SYinghai Lu */ 10045009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, 10055009b460SYinghai Lu unsigned long type, 10065009b460SYinghai Lu enum release_type rel_type) 10075009b460SYinghai Lu { 10085009b460SYinghai Lu struct pci_dev *dev; 10095009b460SYinghai Lu bool is_leaf_bridge = true; 10105009b460SYinghai Lu 10115009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 10125009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 10135009b460SYinghai Lu if (!b) 10145009b460SYinghai Lu continue; 10155009b460SYinghai Lu 10165009b460SYinghai Lu is_leaf_bridge = false; 10175009b460SYinghai Lu 10185009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 10195009b460SYinghai Lu continue; 10205009b460SYinghai Lu 10215009b460SYinghai Lu if (rel_type == whole_subtree) 10225009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 10235009b460SYinghai Lu whole_subtree); 10245009b460SYinghai Lu } 10255009b460SYinghai Lu 10265009b460SYinghai Lu if (pci_is_root_bus(bus)) 10275009b460SYinghai Lu return; 10285009b460SYinghai Lu 10295009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 10305009b460SYinghai Lu return; 10315009b460SYinghai Lu 10325009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 10335009b460SYinghai Lu pci_bridge_release_resources(bus, type); 10345009b460SYinghai Lu } 10355009b460SYinghai Lu 103676fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 103776fbc263SYinghai Lu { 103889a74eccSBjorn Helgaas struct resource *res; 103976fbc263SYinghai Lu int i; 104076fbc263SYinghai Lu 104189a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 10427c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 104376fbc263SYinghai Lu continue; 104476fbc263SYinghai Lu 1045c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 104676fbc263SYinghai Lu } 104776fbc263SYinghai Lu } 104876fbc263SYinghai Lu 104976fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 105076fbc263SYinghai Lu { 105176fbc263SYinghai Lu struct pci_bus *b; 105276fbc263SYinghai Lu struct pci_dev *dev; 105376fbc263SYinghai Lu 105476fbc263SYinghai Lu 105576fbc263SYinghai Lu pci_bus_dump_res(bus); 105676fbc263SYinghai Lu 105776fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 105876fbc263SYinghai Lu b = dev->subordinate; 105976fbc263SYinghai Lu if (!b) 106076fbc263SYinghai Lu continue; 106176fbc263SYinghai Lu 106276fbc263SYinghai Lu pci_bus_dump_resources(b); 106376fbc263SYinghai Lu } 106476fbc263SYinghai Lu } 106576fbc263SYinghai Lu 1066da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus) 1067da7822e5SYinghai Lu { 1068da7822e5SYinghai Lu int depth = 0; 1069da7822e5SYinghai Lu struct pci_dev *dev; 1070da7822e5SYinghai Lu 1071da7822e5SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 1072da7822e5SYinghai Lu int ret; 1073da7822e5SYinghai Lu struct pci_bus *b = dev->subordinate; 1074da7822e5SYinghai Lu if (!b) 1075da7822e5SYinghai Lu continue; 1076da7822e5SYinghai Lu 1077da7822e5SYinghai Lu ret = pci_bus_get_depth(b); 1078da7822e5SYinghai Lu if (ret + 1 > depth) 1079da7822e5SYinghai Lu depth = ret + 1; 1080da7822e5SYinghai Lu } 1081da7822e5SYinghai Lu 1082da7822e5SYinghai Lu return depth; 1083da7822e5SYinghai Lu } 1084da7822e5SYinghai Lu static int __init pci_get_max_depth(void) 1085da7822e5SYinghai Lu { 1086da7822e5SYinghai Lu int depth = 0; 1087da7822e5SYinghai Lu struct pci_bus *bus; 1088da7822e5SYinghai Lu 1089da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) { 1090da7822e5SYinghai Lu int ret; 1091da7822e5SYinghai Lu 1092da7822e5SYinghai Lu ret = pci_bus_get_depth(bus); 1093da7822e5SYinghai Lu if (ret > depth) 1094da7822e5SYinghai Lu depth = ret; 1095da7822e5SYinghai Lu } 1096da7822e5SYinghai Lu 1097da7822e5SYinghai Lu return depth; 1098da7822e5SYinghai Lu } 1099da7822e5SYinghai Lu 1100f483d392SRam Pai 1101da7822e5SYinghai Lu /* 1102da7822e5SYinghai Lu * first try will not touch pci bridge res 1103da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1104da7822e5SYinghai Lu * will stop till to the max deepth if can not find good one 1105da7822e5SYinghai Lu */ 11061da177e4SLinus Torvalds void __init 11071da177e4SLinus Torvalds pci_assign_unassigned_resources(void) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds struct pci_bus *bus; 11109e8bf93aSRam Pai struct resource_list_x realloc_list; /* list of resources that 1111c8adf9a3SRam Pai want additional resources */ 1112da7822e5SYinghai Lu int tried_times = 0; 1113da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1114da7822e5SYinghai Lu struct resource_list_x head, *list; 1115da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1116da7822e5SYinghai Lu IORESOURCE_PREFETCH; 1117da7822e5SYinghai Lu unsigned long failed_type; 1118da7822e5SYinghai Lu int max_depth = pci_get_max_depth(); 1119da7822e5SYinghai Lu int pci_try_num; 1120da7822e5SYinghai Lu 1121da7822e5SYinghai Lu 1122da7822e5SYinghai Lu head.next = NULL; 11239e8bf93aSRam Pai realloc_list.next = NULL; 1124da7822e5SYinghai Lu 1125da7822e5SYinghai Lu pci_try_num = max_depth + 1; 1126da7822e5SYinghai Lu printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n", 1127da7822e5SYinghai Lu max_depth, pci_try_num); 1128da7822e5SYinghai Lu 1129da7822e5SYinghai Lu again: 11301da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 11311da177e4SLinus Torvalds subordinate buses. */ 1132da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 11339e8bf93aSRam Pai __pci_bus_size_bridges(bus, &realloc_list); 1134c8adf9a3SRam Pai 11351da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1136da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 11379e8bf93aSRam Pai __pci_bus_assign_resources(bus, &realloc_list, &head); 11389e8bf93aSRam Pai BUG_ON(realloc_list.next); 1139da7822e5SYinghai Lu tried_times++; 1140da7822e5SYinghai Lu 1141da7822e5SYinghai Lu /* any device complain? */ 1142da7822e5SYinghai Lu if (!head.next) 1143da7822e5SYinghai Lu goto enable_and_dump; 1144f483d392SRam Pai 1145f483d392SRam Pai /* don't realloc if asked to do so */ 1146f483d392SRam Pai if (!pci_realloc_enabled()) { 1147f483d392SRam Pai free_list(resource_list_x, &head); 1148f483d392SRam Pai goto enable_and_dump; 1149f483d392SRam Pai } 1150f483d392SRam Pai 1151da7822e5SYinghai Lu failed_type = 0; 1152da7822e5SYinghai Lu for (list = head.next; list;) { 1153da7822e5SYinghai Lu failed_type |= list->flags; 1154da7822e5SYinghai Lu list = list->next; 1155da7822e5SYinghai Lu } 1156da7822e5SYinghai Lu /* 1157da7822e5SYinghai Lu * io port are tight, don't try extra 1158da7822e5SYinghai Lu * or if reach the limit, don't want to try more 1159da7822e5SYinghai Lu */ 1160da7822e5SYinghai Lu failed_type &= type_mask; 1161da7822e5SYinghai Lu if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) { 1162da7822e5SYinghai Lu free_list(resource_list_x, &head); 1163da7822e5SYinghai Lu goto enable_and_dump; 1164da7822e5SYinghai Lu } 1165da7822e5SYinghai Lu 1166da7822e5SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 1167da7822e5SYinghai Lu tried_times + 1); 1168da7822e5SYinghai Lu 1169da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1170da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1171da7822e5SYinghai Lu rel_type = whole_subtree; 1172da7822e5SYinghai Lu 1173da7822e5SYinghai Lu /* 1174da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1175da7822e5SYinghai Lu * child device under that bridge 1176da7822e5SYinghai Lu */ 1177da7822e5SYinghai Lu for (list = head.next; list;) { 1178da7822e5SYinghai Lu bus = list->dev->bus; 1179da7822e5SYinghai Lu pci_bus_release_bridge_resources(bus, list->flags & type_mask, 1180da7822e5SYinghai Lu rel_type); 1181da7822e5SYinghai Lu list = list->next; 1182da7822e5SYinghai Lu } 1183da7822e5SYinghai Lu /* restore size and flags */ 1184da7822e5SYinghai Lu for (list = head.next; list;) { 1185da7822e5SYinghai Lu struct resource *res = list->res; 1186da7822e5SYinghai Lu 1187da7822e5SYinghai Lu res->start = list->start; 1188da7822e5SYinghai Lu res->end = list->end; 1189da7822e5SYinghai Lu res->flags = list->flags; 1190da7822e5SYinghai Lu if (list->dev->subordinate) 1191da7822e5SYinghai Lu res->flags = 0; 1192da7822e5SYinghai Lu 1193da7822e5SYinghai Lu list = list->next; 1194da7822e5SYinghai Lu } 1195da7822e5SYinghai Lu free_list(resource_list_x, &head); 1196da7822e5SYinghai Lu 1197da7822e5SYinghai Lu goto again; 1198da7822e5SYinghai Lu 1199da7822e5SYinghai Lu enable_and_dump: 1200da7822e5SYinghai Lu /* Depth last, update the hardware. */ 1201da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 1202da7822e5SYinghai Lu pci_enable_bridges(bus); 120376fbc263SYinghai Lu 120476fbc263SYinghai Lu /* dump the resource on buses */ 1205da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 120676fbc263SYinghai Lu pci_bus_dump_resources(bus); 120776fbc263SYinghai Lu } 12086841ec68SYinghai Lu 12096841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 12106841ec68SYinghai Lu { 12116841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 121232180e40SYinghai Lu int tried_times = 0; 121332180e40SYinghai Lu struct resource_list_x head, *list; 12146841ec68SYinghai Lu int retval; 121532180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 121632180e40SYinghai Lu IORESOURCE_PREFETCH; 12176841ec68SYinghai Lu 121832180e40SYinghai Lu head.next = NULL; 121932180e40SYinghai Lu 122032180e40SYinghai Lu again: 12216841ec68SYinghai Lu pci_bus_size_bridges(parent); 122232180e40SYinghai Lu __pci_bridge_assign_resources(bridge, &head); 122332180e40SYinghai Lu 122432180e40SYinghai Lu tried_times++; 122532180e40SYinghai Lu 122632180e40SYinghai Lu if (!head.next) 12273f579c34SYinghai Lu goto enable_all; 122832180e40SYinghai Lu 122932180e40SYinghai Lu if (tried_times >= 2) { 123032180e40SYinghai Lu /* still fail, don't need to try more */ 1231094732a5SRam Pai free_list(resource_list_x, &head); 12323f579c34SYinghai Lu goto enable_all; 123332180e40SYinghai Lu } 123432180e40SYinghai Lu 123532180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 123632180e40SYinghai Lu tried_times + 1); 123732180e40SYinghai Lu 123832180e40SYinghai Lu /* 123932180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 124032180e40SYinghai Lu * child device under that bridge 124132180e40SYinghai Lu */ 124232180e40SYinghai Lu for (list = head.next; list;) { 124332180e40SYinghai Lu struct pci_bus *bus = list->dev->bus; 124432180e40SYinghai Lu unsigned long flags = list->flags; 124532180e40SYinghai Lu 124632180e40SYinghai Lu pci_bus_release_bridge_resources(bus, flags & type_mask, 124732180e40SYinghai Lu whole_subtree); 124832180e40SYinghai Lu list = list->next; 124932180e40SYinghai Lu } 125032180e40SYinghai Lu /* restore size and flags */ 125132180e40SYinghai Lu for (list = head.next; list;) { 125232180e40SYinghai Lu struct resource *res = list->res; 125332180e40SYinghai Lu 125432180e40SYinghai Lu res->start = list->start; 125532180e40SYinghai Lu res->end = list->end; 125632180e40SYinghai Lu res->flags = list->flags; 125732180e40SYinghai Lu if (list->dev->subordinate) 125832180e40SYinghai Lu res->flags = 0; 125932180e40SYinghai Lu 126032180e40SYinghai Lu list = list->next; 126132180e40SYinghai Lu } 1262094732a5SRam Pai free_list(resource_list_x, &head); 126332180e40SYinghai Lu 126432180e40SYinghai Lu goto again; 12653f579c34SYinghai Lu 12663f579c34SYinghai Lu enable_all: 12673f579c34SYinghai Lu retval = pci_reenable_device(bridge); 12683f579c34SYinghai Lu pci_set_master(bridge); 12693f579c34SYinghai Lu pci_enable_bridges(parent); 12706841ec68SYinghai Lu } 12716841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 1272