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> 2847087700SBjorn Helgaas #include <asm-generic/pci-bridge.h> 296faf17f6SChris Wright #include "pci.h" 301da177e4SLinus Torvalds 31844393f4SBjorn Helgaas unsigned int pci_flags; 3247087700SBjorn Helgaas 33bdc4abecSYinghai Lu struct pci_dev_resource { 34bdc4abecSYinghai Lu struct list_head list; 352934a0deSYinghai Lu struct resource *res; 362934a0deSYinghai Lu struct pci_dev *dev; 37568ddef8SYinghai Lu resource_size_t start; 38568ddef8SYinghai Lu resource_size_t end; 39c8adf9a3SRam Pai resource_size_t add_size; 402bbc6942SRam Pai resource_size_t min_align; 41568ddef8SYinghai Lu unsigned long flags; 42568ddef8SYinghai Lu }; 43568ddef8SYinghai Lu 44bffc56d4SYinghai Lu static void free_list(struct list_head *head) 45bffc56d4SYinghai Lu { 46bffc56d4SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 47bffc56d4SYinghai Lu 48bffc56d4SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 49bffc56d4SYinghai Lu list_del(&dev_res->list); 50bffc56d4SYinghai Lu kfree(dev_res); 51bffc56d4SYinghai Lu } 52bffc56d4SYinghai Lu } 53094732a5SRam Pai 54c8adf9a3SRam Pai /** 55c8adf9a3SRam Pai * add_to_list() - add a new resource tracker to the list 56c8adf9a3SRam Pai * @head: Head of the list 57c8adf9a3SRam Pai * @dev: device corresponding to which the resource 58c8adf9a3SRam Pai * belongs 59c8adf9a3SRam Pai * @res: The resource to be tracked 60c8adf9a3SRam Pai * @add_size: additional size to be optionally added 61c8adf9a3SRam Pai * to the resource 62c8adf9a3SRam Pai */ 63bdc4abecSYinghai Lu static int add_to_list(struct list_head *head, 64c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res, 652bbc6942SRam Pai resource_size_t add_size, resource_size_t min_align) 66568ddef8SYinghai Lu { 67764242a0SYinghai Lu struct pci_dev_resource *tmp; 68568ddef8SYinghai Lu 69bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 70568ddef8SYinghai Lu if (!tmp) { 713c78bc61SRyan Desfosses pr_warn("add_to_list: kmalloc() failed!\n"); 72ef62dfefSYinghai Lu return -ENOMEM; 73568ddef8SYinghai Lu } 74568ddef8SYinghai Lu 75568ddef8SYinghai Lu tmp->res = res; 76568ddef8SYinghai Lu tmp->dev = dev; 77568ddef8SYinghai Lu tmp->start = res->start; 78568ddef8SYinghai Lu tmp->end = res->end; 79568ddef8SYinghai Lu tmp->flags = res->flags; 80c8adf9a3SRam Pai tmp->add_size = add_size; 812bbc6942SRam Pai tmp->min_align = min_align; 82bdc4abecSYinghai Lu 83bdc4abecSYinghai Lu list_add(&tmp->list, head); 84ef62dfefSYinghai Lu 85ef62dfefSYinghai Lu return 0; 86568ddef8SYinghai Lu } 87568ddef8SYinghai Lu 88b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head, 893e6e0d80SYinghai Lu struct resource *res) 903e6e0d80SYinghai Lu { 91b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 923e6e0d80SYinghai Lu 93b9b0bba9SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 94b9b0bba9SYinghai Lu if (dev_res->res == res) { 95b9b0bba9SYinghai Lu list_del(&dev_res->list); 96b9b0bba9SYinghai Lu kfree(dev_res); 97bdc4abecSYinghai Lu break; 983e6e0d80SYinghai Lu } 993e6e0d80SYinghai Lu } 1003e6e0d80SYinghai Lu } 1013e6e0d80SYinghai Lu 102b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head, 1031c372353SYinghai Lu struct resource *res) 1041c372353SYinghai Lu { 105b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res; 1061c372353SYinghai Lu 107b9b0bba9SYinghai Lu list_for_each_entry(dev_res, head, list) { 108b9b0bba9SYinghai Lu if (dev_res->res == res) { 109b592443dSYinghai Lu int idx = res - &dev_res->dev->resource[0]; 110b592443dSYinghai Lu 111b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &dev_res->dev->dev, 112b592443dSYinghai Lu "res[%d]=%pR get_res_add_size add_size %llx\n", 113b592443dSYinghai Lu idx, dev_res->res, 114b9b0bba9SYinghai Lu (unsigned long long)dev_res->add_size); 115b592443dSYinghai Lu 116b9b0bba9SYinghai Lu return dev_res->add_size; 117bdc4abecSYinghai Lu } 1183e6e0d80SYinghai Lu } 1191c372353SYinghai Lu 1201c372353SYinghai Lu return 0; 1211c372353SYinghai Lu } 1221c372353SYinghai Lu 12378c3b329SYinghai Lu /* Sort resources by alignment */ 124bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 12578c3b329SYinghai Lu { 12678c3b329SYinghai Lu int i; 12778c3b329SYinghai Lu 12878c3b329SYinghai Lu for (i = 0; i < PCI_NUM_RESOURCES; i++) { 12978c3b329SYinghai Lu struct resource *r; 130bdc4abecSYinghai Lu struct pci_dev_resource *dev_res, *tmp; 13178c3b329SYinghai Lu resource_size_t r_align; 132bdc4abecSYinghai Lu struct list_head *n; 13378c3b329SYinghai Lu 13478c3b329SYinghai Lu r = &dev->resource[i]; 13578c3b329SYinghai Lu 13678c3b329SYinghai Lu if (r->flags & IORESOURCE_PCI_FIXED) 13778c3b329SYinghai Lu continue; 13878c3b329SYinghai Lu 13978c3b329SYinghai Lu if (!(r->flags) || r->parent) 14078c3b329SYinghai Lu continue; 14178c3b329SYinghai Lu 14278c3b329SYinghai Lu r_align = pci_resource_alignment(dev, r); 14378c3b329SYinghai Lu if (!r_align) { 14478c3b329SYinghai Lu dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n", 14578c3b329SYinghai Lu i, r); 14678c3b329SYinghai Lu continue; 14778c3b329SYinghai Lu } 14878c3b329SYinghai Lu 149bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 15078c3b329SYinghai Lu if (!tmp) 151227f0647SRyan Desfosses panic("pdev_sort_resources(): kmalloc() failed!\n"); 15278c3b329SYinghai Lu tmp->res = r; 15378c3b329SYinghai Lu tmp->dev = dev; 154bdc4abecSYinghai Lu 155bdc4abecSYinghai Lu /* fallback is smallest one or list is empty*/ 156bdc4abecSYinghai Lu n = head; 157bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 158bdc4abecSYinghai Lu resource_size_t align; 159bdc4abecSYinghai Lu 160bdc4abecSYinghai Lu align = pci_resource_alignment(dev_res->dev, 161bdc4abecSYinghai Lu dev_res->res); 162bdc4abecSYinghai Lu 163bdc4abecSYinghai Lu if (r_align > align) { 164bdc4abecSYinghai Lu n = &dev_res->list; 16578c3b329SYinghai Lu break; 16678c3b329SYinghai Lu } 16778c3b329SYinghai Lu } 168bdc4abecSYinghai Lu /* Insert it just before n*/ 169bdc4abecSYinghai Lu list_add_tail(&tmp->list, n); 17078c3b329SYinghai Lu } 17178c3b329SYinghai Lu } 17278c3b329SYinghai Lu 1736841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev, 174bdc4abecSYinghai Lu struct list_head *head) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds u16 class = dev->class >> 8; 1771da177e4SLinus Torvalds 1789bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 1796841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1806841ec68SYinghai Lu return; 1811da177e4SLinus Torvalds 1829bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 18323186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 1849bded00bSKenji Kaneshige u16 command; 1859bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 1869bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 1876841ec68SYinghai Lu return; 18823186279SSatoru Takeuchi } 18923186279SSatoru Takeuchi 1906841ec68SYinghai Lu pdev_sort_resources(dev, head); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 193fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 194fc075e1dSRam Pai { 195fc075e1dSRam Pai res->start = 0; 196fc075e1dSRam Pai res->end = 0; 197fc075e1dSRam Pai res->flags = 0; 198fc075e1dSRam Pai } 199fc075e1dSRam Pai 200c8adf9a3SRam Pai /** 2019e8bf93aSRam Pai * reassign_resources_sorted() - satisfy any additional resource requests 202c8adf9a3SRam Pai * 2039e8bf93aSRam Pai * @realloc_head : head of the list tracking requests requiring additional 204c8adf9a3SRam Pai * resources 205c8adf9a3SRam Pai * @head : head of the list tracking requests with allocated 206c8adf9a3SRam Pai * resources 207c8adf9a3SRam Pai * 2089e8bf93aSRam Pai * Walk through each element of the realloc_head and try to procure 209c8adf9a3SRam Pai * additional resources for the element, provided the element 210c8adf9a3SRam Pai * is in the head list. 211c8adf9a3SRam Pai */ 212bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head, 213bdc4abecSYinghai Lu struct list_head *head) 214c8adf9a3SRam Pai { 215c8adf9a3SRam Pai struct resource *res; 216b9b0bba9SYinghai Lu struct pci_dev_resource *add_res, *tmp; 217bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 218c8adf9a3SRam Pai resource_size_t add_size; 219c8adf9a3SRam Pai int idx; 220c8adf9a3SRam Pai 221b9b0bba9SYinghai Lu list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 222bdc4abecSYinghai Lu bool found_match = false; 223bdc4abecSYinghai Lu 224b9b0bba9SYinghai Lu res = add_res->res; 225c8adf9a3SRam Pai /* skip resource that has been reset */ 226c8adf9a3SRam Pai if (!res->flags) 227c8adf9a3SRam Pai goto out; 228c8adf9a3SRam Pai 229c8adf9a3SRam Pai /* skip this resource if not found in head list */ 230bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 231bdc4abecSYinghai Lu if (dev_res->res == res) { 232bdc4abecSYinghai Lu found_match = true; 233bdc4abecSYinghai Lu break; 234c8adf9a3SRam Pai } 235bdc4abecSYinghai Lu } 236bdc4abecSYinghai Lu if (!found_match)/* just skip */ 237bdc4abecSYinghai Lu continue; 238c8adf9a3SRam Pai 239b9b0bba9SYinghai Lu idx = res - &add_res->dev->resource[0]; 240b9b0bba9SYinghai Lu add_size = add_res->add_size; 2412bbc6942SRam Pai if (!resource_size(res)) { 242b9b0bba9SYinghai Lu res->start = add_res->start; 243c8adf9a3SRam Pai res->end = res->start + add_size - 1; 244b9b0bba9SYinghai Lu if (pci_assign_resource(add_res->dev, idx)) 245c8adf9a3SRam Pai reset_resource(res); 2462bbc6942SRam Pai } else { 247b9b0bba9SYinghai Lu resource_size_t align = add_res->min_align; 248b9b0bba9SYinghai Lu res->flags |= add_res->flags & 249bdc4abecSYinghai Lu (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 250b9b0bba9SYinghai Lu if (pci_reassign_resource(add_res->dev, idx, 251bdc4abecSYinghai Lu add_size, align)) 252b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &add_res->dev->dev, 253b592443dSYinghai Lu "failed to add %llx res[%d]=%pR\n", 254b592443dSYinghai Lu (unsigned long long)add_size, 255b592443dSYinghai Lu idx, res); 256c8adf9a3SRam Pai } 257c8adf9a3SRam Pai out: 258b9b0bba9SYinghai Lu list_del(&add_res->list); 259b9b0bba9SYinghai Lu kfree(add_res); 260c8adf9a3SRam Pai } 261c8adf9a3SRam Pai } 262c8adf9a3SRam Pai 263c8adf9a3SRam Pai /** 264c8adf9a3SRam Pai * assign_requested_resources_sorted() - satisfy resource requests 265c8adf9a3SRam Pai * 266c8adf9a3SRam Pai * @head : head of the list tracking requests for resources 2678356aad4SWanpeng Li * @fail_head : head of the list tracking requests that could 268c8adf9a3SRam Pai * not be allocated 269c8adf9a3SRam Pai * 270c8adf9a3SRam Pai * Satisfy resource requests of each element in the list. Add 271c8adf9a3SRam Pai * requests that could not satisfied to the failed_list. 272c8adf9a3SRam Pai */ 273bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head, 274bdc4abecSYinghai Lu struct list_head *fail_head) 2756841ec68SYinghai Lu { 2766841ec68SYinghai Lu struct resource *res; 277bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 2786841ec68SYinghai Lu int idx; 2796841ec68SYinghai Lu 280bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 281bdc4abecSYinghai Lu res = dev_res->res; 282bdc4abecSYinghai Lu idx = res - &dev_res->dev->resource[0]; 283bdc4abecSYinghai Lu if (resource_size(res) && 284bdc4abecSYinghai Lu pci_assign_resource(dev_res->dev, idx)) { 285a3cb999dSYinghai Lu if (fail_head) { 2869a928660SYinghai Lu /* 2879a928660SYinghai Lu * if the failed res is for ROM BAR, and it will 2889a928660SYinghai Lu * be enabled later, don't add it to the list 2899a928660SYinghai Lu */ 2909a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 2919a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 29267cc7e26SYinghai Lu add_to_list(fail_head, 29367cc7e26SYinghai Lu dev_res->dev, res, 294f7625980SBjorn Helgaas 0 /* don't care */, 295f7625980SBjorn Helgaas 0 /* don't care */); 2969a928660SYinghai Lu } 297fc075e1dSRam Pai reset_resource(res); 298542df5deSRajesh Shah } 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 302aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head) 303aa914f5eSYinghai Lu { 304aa914f5eSYinghai Lu struct pci_dev_resource *fail_res; 305aa914f5eSYinghai Lu unsigned long mask = 0; 306aa914f5eSYinghai Lu 307aa914f5eSYinghai Lu /* check failed type */ 308aa914f5eSYinghai Lu list_for_each_entry(fail_res, fail_head, list) 309aa914f5eSYinghai Lu mask |= fail_res->flags; 310aa914f5eSYinghai Lu 311aa914f5eSYinghai Lu /* 312aa914f5eSYinghai Lu * one pref failed resource will set IORESOURCE_MEM, 313aa914f5eSYinghai Lu * as we can allocate pref in non-pref range. 314aa914f5eSYinghai Lu * Will release all assigned non-pref sibling resources 315aa914f5eSYinghai Lu * according to that bit. 316aa914f5eSYinghai Lu */ 317aa914f5eSYinghai Lu return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH); 318aa914f5eSYinghai Lu } 319aa914f5eSYinghai Lu 320aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res) 321aa914f5eSYinghai Lu { 322aa914f5eSYinghai Lu if (res->flags & IORESOURCE_IO) 323aa914f5eSYinghai Lu return !!(mask & IORESOURCE_IO); 324aa914f5eSYinghai Lu 325aa914f5eSYinghai Lu /* check pref at first */ 326aa914f5eSYinghai Lu if (res->flags & IORESOURCE_PREFETCH) { 327aa914f5eSYinghai Lu if (mask & IORESOURCE_PREFETCH) 328aa914f5eSYinghai Lu return true; 329aa914f5eSYinghai Lu /* count pref if its parent is non-pref */ 330aa914f5eSYinghai Lu else if ((mask & IORESOURCE_MEM) && 331aa914f5eSYinghai Lu !(res->parent->flags & IORESOURCE_PREFETCH)) 332aa914f5eSYinghai Lu return true; 333aa914f5eSYinghai Lu else 334aa914f5eSYinghai Lu return false; 335aa914f5eSYinghai Lu } 336aa914f5eSYinghai Lu 337aa914f5eSYinghai Lu if (res->flags & IORESOURCE_MEM) 338aa914f5eSYinghai Lu return !!(mask & IORESOURCE_MEM); 339aa914f5eSYinghai Lu 340aa914f5eSYinghai Lu return false; /* should not get here */ 341aa914f5eSYinghai Lu } 342aa914f5eSYinghai Lu 343bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head, 344bdc4abecSYinghai Lu struct list_head *realloc_head, 345bdc4abecSYinghai Lu struct list_head *fail_head) 346c8adf9a3SRam Pai { 3473e6e0d80SYinghai Lu /* 3483e6e0d80SYinghai Lu * Should not assign requested resources at first. 3493e6e0d80SYinghai Lu * they could be adjacent, so later reassign can not reallocate 3503e6e0d80SYinghai Lu * them one by one in parent resource window. 351367fa982SMasanari Iida * Try to assign requested + add_size at beginning 3523e6e0d80SYinghai Lu * if could do that, could get out early. 3533e6e0d80SYinghai Lu * if could not do that, we still try to assign requested at first, 3543e6e0d80SYinghai Lu * then try to reassign add_size for some resources. 355aa914f5eSYinghai Lu * 356aa914f5eSYinghai Lu * Separate three resource type checking if we need to release 357aa914f5eSYinghai Lu * assigned resource after requested + add_size try. 358aa914f5eSYinghai Lu * 1. if there is io port assign fail, will release assigned 359aa914f5eSYinghai Lu * io port. 360aa914f5eSYinghai Lu * 2. if there is pref mmio assign fail, release assigned 361aa914f5eSYinghai Lu * pref mmio. 362aa914f5eSYinghai Lu * if assigned pref mmio's parent is non-pref mmio and there 363aa914f5eSYinghai Lu * is non-pref mmio assign fail, will release that assigned 364aa914f5eSYinghai Lu * pref mmio. 365aa914f5eSYinghai Lu * 3. if there is non-pref mmio assign fail or pref mmio 366aa914f5eSYinghai Lu * assigned fail, will release assigned non-pref mmio. 3673e6e0d80SYinghai Lu */ 368bdc4abecSYinghai Lu LIST_HEAD(save_head); 369bdc4abecSYinghai Lu LIST_HEAD(local_fail_head); 370b9b0bba9SYinghai Lu struct pci_dev_resource *save_res; 371aa914f5eSYinghai Lu struct pci_dev_resource *dev_res, *tmp_res; 372aa914f5eSYinghai Lu unsigned long fail_type; 3733e6e0d80SYinghai Lu 3743e6e0d80SYinghai Lu /* Check if optional add_size is there */ 375bdc4abecSYinghai Lu if (!realloc_head || list_empty(realloc_head)) 3763e6e0d80SYinghai Lu goto requested_and_reassign; 3773e6e0d80SYinghai Lu 3783e6e0d80SYinghai Lu /* Save original start, end, flags etc at first */ 379bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 380bdc4abecSYinghai Lu if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 381bffc56d4SYinghai Lu free_list(&save_head); 3823e6e0d80SYinghai Lu goto requested_and_reassign; 3833e6e0d80SYinghai Lu } 384bdc4abecSYinghai Lu } 3853e6e0d80SYinghai Lu 3863e6e0d80SYinghai Lu /* Update res in head list with add_size in realloc_head list */ 387bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 388bdc4abecSYinghai Lu dev_res->res->end += get_res_add_size(realloc_head, 389bdc4abecSYinghai Lu dev_res->res); 3903e6e0d80SYinghai Lu 3913e6e0d80SYinghai Lu /* Try updated head list with add_size added */ 3923e6e0d80SYinghai Lu assign_requested_resources_sorted(head, &local_fail_head); 3933e6e0d80SYinghai Lu 3943e6e0d80SYinghai Lu /* all assigned with add_size ? */ 395bdc4abecSYinghai Lu if (list_empty(&local_fail_head)) { 3963e6e0d80SYinghai Lu /* Remove head list from realloc_head list */ 397bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 398bdc4abecSYinghai Lu remove_from_list(realloc_head, dev_res->res); 399bffc56d4SYinghai Lu free_list(&save_head); 400bffc56d4SYinghai Lu free_list(head); 4013e6e0d80SYinghai Lu return; 4023e6e0d80SYinghai Lu } 4033e6e0d80SYinghai Lu 404aa914f5eSYinghai Lu /* check failed type */ 405aa914f5eSYinghai Lu fail_type = pci_fail_res_type_mask(&local_fail_head); 406aa914f5eSYinghai Lu /* remove not need to be released assigned res from head list etc */ 407aa914f5eSYinghai Lu list_for_each_entry_safe(dev_res, tmp_res, head, list) 408aa914f5eSYinghai Lu if (dev_res->res->parent && 409aa914f5eSYinghai Lu !pci_need_to_release(fail_type, dev_res->res)) { 410aa914f5eSYinghai Lu /* remove it from realloc_head list */ 411aa914f5eSYinghai Lu remove_from_list(realloc_head, dev_res->res); 412aa914f5eSYinghai Lu remove_from_list(&save_head, dev_res->res); 413aa914f5eSYinghai Lu list_del(&dev_res->list); 414aa914f5eSYinghai Lu kfree(dev_res); 415aa914f5eSYinghai Lu } 416aa914f5eSYinghai Lu 417bffc56d4SYinghai Lu free_list(&local_fail_head); 4183e6e0d80SYinghai Lu /* Release assigned resource */ 419bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 420bdc4abecSYinghai Lu if (dev_res->res->parent) 421bdc4abecSYinghai Lu release_resource(dev_res->res); 4223e6e0d80SYinghai Lu /* Restore start/end/flags from saved list */ 423b9b0bba9SYinghai Lu list_for_each_entry(save_res, &save_head, list) { 424b9b0bba9SYinghai Lu struct resource *res = save_res->res; 4253e6e0d80SYinghai Lu 426b9b0bba9SYinghai Lu res->start = save_res->start; 427b9b0bba9SYinghai Lu res->end = save_res->end; 428b9b0bba9SYinghai Lu res->flags = save_res->flags; 4293e6e0d80SYinghai Lu } 430bffc56d4SYinghai Lu free_list(&save_head); 4313e6e0d80SYinghai Lu 4323e6e0d80SYinghai Lu requested_and_reassign: 433c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 434c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 435c8adf9a3SRam Pai 4360a2daa1cSRam Pai /* Try to satisfy any additional optional resource 437c8adf9a3SRam Pai requests */ 4389e8bf93aSRam Pai if (realloc_head) 4399e8bf93aSRam Pai reassign_resources_sorted(realloc_head, head); 440bffc56d4SYinghai Lu free_list(head); 441c8adf9a3SRam Pai } 442c8adf9a3SRam Pai 4436841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 444bdc4abecSYinghai Lu struct list_head *add_head, 445bdc4abecSYinghai Lu struct list_head *fail_head) 4466841ec68SYinghai Lu { 447bdc4abecSYinghai Lu LIST_HEAD(head); 4486841ec68SYinghai Lu 4496841ec68SYinghai Lu __dev_sort_resources(dev, &head); 4508424d759SYinghai Lu __assign_resources_sorted(&head, add_head, fail_head); 4516841ec68SYinghai Lu 4526841ec68SYinghai Lu } 4536841ec68SYinghai Lu 4546841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 455bdc4abecSYinghai Lu struct list_head *realloc_head, 456bdc4abecSYinghai Lu struct list_head *fail_head) 4576841ec68SYinghai Lu { 4586841ec68SYinghai Lu struct pci_dev *dev; 459bdc4abecSYinghai Lu LIST_HEAD(head); 4606841ec68SYinghai Lu 4616841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 4626841ec68SYinghai Lu __dev_sort_resources(dev, &head); 4636841ec68SYinghai Lu 4649e8bf93aSRam Pai __assign_resources_sorted(&head, realloc_head, fail_head); 4656841ec68SYinghai Lu } 4666841ec68SYinghai Lu 467b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 470c7dabef8SBjorn Helgaas struct resource *res; 4711da177e4SLinus Torvalds struct pci_bus_region region; 4721da177e4SLinus Torvalds 473b918c62eSYinghai Lu dev_info(&bridge->dev, "CardBus bridge to %pR\n", 474b918c62eSYinghai Lu &bus->busn_res); 4751da177e4SLinus Torvalds 476c7dabef8SBjorn Helgaas res = bus->resource[0]; 477fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 478c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 4811da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 4821da177e4SLinus Torvalds */ 483c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4841da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 4851da177e4SLinus Torvalds region.start); 4861da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 4871da177e4SLinus Torvalds region.end); 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 490c7dabef8SBjorn Helgaas res = bus->resource[1]; 491fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 492c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 493c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4941da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 4951da177e4SLinus Torvalds region.start); 4961da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 4971da177e4SLinus Torvalds region.end); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 500c7dabef8SBjorn Helgaas res = bus->resource[2]; 501fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 502c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 503c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5041da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 5051da177e4SLinus Torvalds region.start); 5061da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 5071da177e4SLinus Torvalds region.end); 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 510c7dabef8SBjorn Helgaas res = bus->resource[3]; 511fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 512c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 513c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5141da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 5151da177e4SLinus Torvalds region.start); 5161da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 5171da177e4SLinus Torvalds region.end); 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds } 520b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 5231da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 5241da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 5251da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 5261da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 5291da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 5301da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 5311da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 5321da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 5337cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 536c7dabef8SBjorn Helgaas struct resource *res; 5371da177e4SLinus Torvalds struct pci_bus_region region; 5382b28ae19SBjorn Helgaas unsigned long io_mask; 5392b28ae19SBjorn Helgaas u8 io_base_lo, io_limit_lo; 5405b764b83SBjorn Helgaas u16 l; 5415b764b83SBjorn Helgaas u32 io_upper16; 5421da177e4SLinus Torvalds 5432b28ae19SBjorn Helgaas io_mask = PCI_IO_RANGE_MASK; 5442b28ae19SBjorn Helgaas if (bridge->io_window_1k) 5452b28ae19SBjorn Helgaas io_mask = PCI_IO_1K_RANGE_MASK; 5462b28ae19SBjorn Helgaas 5471da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 548c7dabef8SBjorn Helgaas res = bus->resource[0]; 549fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 550c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5515b764b83SBjorn Helgaas pci_read_config_word(bridge, PCI_IO_BASE, &l); 5522b28ae19SBjorn Helgaas io_base_lo = (region.start >> 8) & io_mask; 5532b28ae19SBjorn Helgaas io_limit_lo = (region.end >> 8) & io_mask; 5545b764b83SBjorn Helgaas l = ((u16) io_limit_lo << 8) | io_base_lo; 5551da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 5561da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 557c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5587cc5997dSYinghai Lu } else { 5591da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 5601da177e4SLinus Torvalds io_upper16 = 0; 5611da177e4SLinus Torvalds l = 0x00f0; 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 5641da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 5651da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 5665b764b83SBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, l); 5671da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 5681da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 5697cc5997dSYinghai Lu } 5701da177e4SLinus Torvalds 5717cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus) 5727cc5997dSYinghai Lu { 5737cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5747cc5997dSYinghai Lu struct resource *res; 5757cc5997dSYinghai Lu struct pci_bus_region region; 5767cc5997dSYinghai Lu u32 l; 5777cc5997dSYinghai Lu 5787cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 579c7dabef8SBjorn Helgaas res = bus->resource[1]; 580fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 581c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 5821da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 5831da177e4SLinus Torvalds l |= region.end & 0xfff00000; 584c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5857cc5997dSYinghai Lu } else { 5861da177e4SLinus Torvalds l = 0x0000fff0; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 5897cc5997dSYinghai Lu } 5907cc5997dSYinghai Lu 5917cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) 5927cc5997dSYinghai Lu { 5937cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5947cc5997dSYinghai Lu struct resource *res; 5957cc5997dSYinghai Lu struct pci_bus_region region; 5967cc5997dSYinghai Lu u32 l, bu, lu; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 5991da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 6001da177e4SLinus Torvalds disables PREF range, which is ok. */ 6011da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* Set up PREF base/limit. */ 604c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 605c7dabef8SBjorn Helgaas res = bus->resource[2]; 606fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 607c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 6081da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6091da177e4SLinus Torvalds l |= region.end & 0xfff00000; 610c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 61113d36c24SAndrew Morton bu = upper_32_bits(region.start); 61213d36c24SAndrew Morton lu = upper_32_bits(region.end); 6131f82de10SYinghai Lu } 614c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 6157cc5997dSYinghai Lu } else { 6161da177e4SLinus Torvalds l = 0x0000fff0; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 6191da177e4SLinus Torvalds 620c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 621c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 622c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 6237cc5997dSYinghai Lu } 6247cc5997dSYinghai Lu 6257cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 6267cc5997dSYinghai Lu { 6277cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 6287cc5997dSYinghai Lu 629b918c62eSYinghai Lu dev_info(&bridge->dev, "PCI bridge to %pR\n", 630b918c62eSYinghai Lu &bus->busn_res); 6317cc5997dSYinghai Lu 6327cc5997dSYinghai Lu if (type & IORESOURCE_IO) 6337cc5997dSYinghai Lu pci_setup_bridge_io(bus); 6347cc5997dSYinghai Lu 6357cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 6367cc5997dSYinghai Lu pci_setup_bridge_mmio(bus); 6377cc5997dSYinghai Lu 6387cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 6397cc5997dSYinghai Lu pci_setup_bridge_mmio_pref(bus); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 644e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 6457cc5997dSYinghai Lu { 6467cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 6477cc5997dSYinghai Lu IORESOURCE_PREFETCH; 6487cc5997dSYinghai Lu 6497cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 6507cc5997dSYinghai Lu } 6517cc5997dSYinghai Lu 6521da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 6531da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 6541da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 65596bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds u16 io; 6581da177e4SLinus Torvalds u32 pmem; 6591da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 6601da177e4SLinus Torvalds struct resource *b_res; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 6631da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6661da177e4SLinus Torvalds if (!io) { 667d2f54d9bSBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0); 6681da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6691da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds if (io) 6721da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 673d2f54d9bSBjorn Helgaas 6741da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 6751da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 6761da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 6771da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 6781da177e4SLinus Torvalds return; 679d2f54d9bSBjorn Helgaas 6801da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 6811da177e4SLinus Torvalds if (!pmem) { 6821da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 683d2f54d9bSBjorn Helgaas 0xffe0fff0); 6841da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 6851da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 6861da177e4SLinus Torvalds } 6871f82de10SYinghai Lu if (pmem) { 6881da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 68999586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 69099586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 6911f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 69299586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 69399586105SYinghai Lu } 6941f82de10SYinghai Lu } 6951f82de10SYinghai Lu 6961f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 6971f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 6981f82de10SYinghai Lu u32 mem_base_hi, tmp; 6991f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7001f82de10SYinghai Lu &mem_base_hi); 7011f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7021f82de10SYinghai Lu 0xffffffff); 7031f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 7041f82de10SYinghai Lu if (!tmp) 7051f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 7061f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7071f82de10SYinghai Lu mem_base_hi); 7081f82de10SYinghai Lu } 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 7121da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 7131da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 7141da177e4SLinus Torvalds have non-NULL parent resource). */ 7155b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus, 7165b285415SYinghai Lu unsigned long type_mask, unsigned long type) 7171da177e4SLinus Torvalds { 7181da177e4SLinus Torvalds int i; 7191da177e4SLinus Torvalds struct resource *r; 7201da177e4SLinus Torvalds 72189a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 722299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 723299de034SIvan Kokshaysky continue; 72455a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 7251da177e4SLinus Torvalds return r; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds return NULL; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 73013583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 73113583b16SRam Pai resource_size_t min_size, 73213583b16SRam Pai resource_size_t size1, 73313583b16SRam Pai resource_size_t old_size, 73413583b16SRam Pai resource_size_t align) 73513583b16SRam Pai { 73613583b16SRam Pai if (size < min_size) 73713583b16SRam Pai size = min_size; 73813583b16SRam Pai if (old_size == 1) 73913583b16SRam Pai old_size = 0; 74013583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 74113583b16SRam Pai flag in the struct pci_bus. */ 74213583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 74313583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 74413583b16SRam Pai #endif 74513583b16SRam Pai size = ALIGN(size + size1, align); 74613583b16SRam Pai if (size < old_size) 74713583b16SRam Pai size = old_size; 74813583b16SRam Pai return size; 74913583b16SRam Pai } 75013583b16SRam Pai 75113583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 75213583b16SRam Pai resource_size_t min_size, 75313583b16SRam Pai resource_size_t size1, 75413583b16SRam Pai resource_size_t old_size, 75513583b16SRam Pai resource_size_t align) 75613583b16SRam Pai { 75713583b16SRam Pai if (size < min_size) 75813583b16SRam Pai size = min_size; 75913583b16SRam Pai if (old_size == 1) 76013583b16SRam Pai old_size = 0; 76113583b16SRam Pai if (size < old_size) 76213583b16SRam Pai size = old_size; 76313583b16SRam Pai size = ALIGN(size + size1, align); 76413583b16SRam Pai return size; 76513583b16SRam Pai } 76613583b16SRam Pai 767ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, 768ac5ad93eSGavin Shan unsigned long type) 769ac5ad93eSGavin Shan { 770ac5ad93eSGavin Shan return 1; 771ac5ad93eSGavin Shan } 772ac5ad93eSGavin Shan 773ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */ 774ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */ 775ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */ 776ac5ad93eSGavin Shan 777ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus, 778ac5ad93eSGavin Shan unsigned long type) 779ac5ad93eSGavin Shan { 780ac5ad93eSGavin Shan resource_size_t align = 1, arch_align; 781ac5ad93eSGavin Shan 782ac5ad93eSGavin Shan if (type & IORESOURCE_MEM) 783ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_MEM_ALIGN; 784ac5ad93eSGavin Shan else if (type & IORESOURCE_IO) { 785ac5ad93eSGavin Shan /* 786ac5ad93eSGavin Shan * Per spec, I/O windows are 4K-aligned, but some 787ac5ad93eSGavin Shan * bridges have an extension to support 1K alignment. 788ac5ad93eSGavin Shan */ 789ac5ad93eSGavin Shan if (bus->self->io_window_1k) 790ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN_1K; 791ac5ad93eSGavin Shan else 792ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN; 793ac5ad93eSGavin Shan } 794ac5ad93eSGavin Shan 795ac5ad93eSGavin Shan arch_align = pcibios_window_alignment(bus, type); 796ac5ad93eSGavin Shan return max(align, arch_align); 797ac5ad93eSGavin Shan } 798ac5ad93eSGavin Shan 799c8adf9a3SRam Pai /** 800c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 801c8adf9a3SRam Pai * 802c8adf9a3SRam Pai * @bus : the bus 803c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 804c8adf9a3SRam Pai * @add_size : additional optional io window 8059e8bf93aSRam Pai * @realloc_head : track the additional io window on this list 806c8adf9a3SRam Pai * 807c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 808fd591341SYinghai Lu * since these windows have 1K or 4K granularity and the IO ranges 809c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 810c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 811c8adf9a3SRam Pai */ 812c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 813bdc4abecSYinghai Lu resource_size_t add_size, struct list_head *realloc_head) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds struct pci_dev *dev; 8165b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO, 8175b285415SYinghai Lu IORESOURCE_IO); 81811251a86SWei Yang resource_size_t size = 0, size0 = 0, size1 = 0; 819be768912SYinghai Lu resource_size_t children_add_size = 0; 8202d1d6678SBjorn Helgaas resource_size_t min_align, align; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (!b_res) 8231da177e4SLinus Torvalds return; 8241da177e4SLinus Torvalds 8252d1d6678SBjorn Helgaas min_align = window_alignment(bus, IORESOURCE_IO); 8261da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8271da177e4SLinus Torvalds int i; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 8301da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 8311da177e4SLinus Torvalds unsigned long r_size; 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 8341da177e4SLinus Torvalds continue; 835022edd86SZhao, Yu r_size = resource_size(r); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds if (r_size < 0x400) 8381da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 8391da177e4SLinus Torvalds size += r_size; 8401da177e4SLinus Torvalds else 8411da177e4SLinus Torvalds size1 += r_size; 842be768912SYinghai Lu 843fd591341SYinghai Lu align = pci_resource_alignment(dev, r); 844fd591341SYinghai Lu if (align > min_align) 845fd591341SYinghai Lu min_align = align; 846fd591341SYinghai Lu 8479e8bf93aSRam Pai if (realloc_head) 8489e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds } 851fd591341SYinghai Lu 852c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 853fd591341SYinghai Lu resource_size(b_res), min_align); 854be768912SYinghai Lu if (children_add_size > add_size) 855be768912SYinghai Lu add_size = children_add_size; 8569e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 857a4ac9feaSYinghai Lu calculate_iosize(size, min_size, add_size + size1, 858fd591341SYinghai Lu resource_size(b_res), min_align); 859c8adf9a3SRam Pai if (!size0 && !size1) { 860865df576SBjorn Helgaas if (b_res->start || b_res->end) 861227f0647SRyan Desfosses dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n", 862227f0647SRyan Desfosses b_res, &bus->busn_res); 8631da177e4SLinus Torvalds b_res->flags = 0; 8641da177e4SLinus Torvalds return; 8651da177e4SLinus Torvalds } 866fd591341SYinghai Lu 867fd591341SYinghai Lu b_res->start = min_align; 868c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 86988452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 870b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 871fd591341SYinghai Lu add_to_list(realloc_head, bus->self, b_res, size1-size0, 872fd591341SYinghai Lu min_align); 873227f0647SRyan Desfosses dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n", 874227f0647SRyan Desfosses b_res, &bus->busn_res, 87511251a86SWei Yang (unsigned long long)size1-size0); 876b592443dSYinghai Lu } 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns, 880c121504eSGavin Shan int max_order) 881c121504eSGavin Shan { 882c121504eSGavin Shan resource_size_t align = 0; 883c121504eSGavin Shan resource_size_t min_align = 0; 884c121504eSGavin Shan int order; 885c121504eSGavin Shan 886c121504eSGavin Shan for (order = 0; order <= max_order; order++) { 887c121504eSGavin Shan resource_size_t align1 = 1; 888c121504eSGavin Shan 889c121504eSGavin Shan align1 <<= (order + 20); 890c121504eSGavin Shan 891c121504eSGavin Shan if (!align) 892c121504eSGavin Shan min_align = align1; 893c121504eSGavin Shan else if (ALIGN(align + min_align, min_align) < align1) 894c121504eSGavin Shan min_align = align1 >> 1; 895c121504eSGavin Shan align += aligns[order]; 896c121504eSGavin Shan } 897c121504eSGavin Shan 898c121504eSGavin Shan return min_align; 899c121504eSGavin Shan } 900c121504eSGavin Shan 901c8adf9a3SRam Pai /** 902c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 903c8adf9a3SRam Pai * 904c8adf9a3SRam Pai * @bus : the bus 905496f70cfSWei Yang * @mask: mask the resource flag, then compare it with type 906496f70cfSWei Yang * @type: the type of free resource from bridge 9075b285415SYinghai Lu * @type2: second match type 9085b285415SYinghai Lu * @type3: third match type 909c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 910c8adf9a3SRam Pai * @add_size : additional optional memory window 9119e8bf93aSRam Pai * @realloc_head : track the additional memory window on this list 912c8adf9a3SRam Pai * 913c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 914c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 91530afe8d0SBjorn Helgaas * 91630afe8d0SBjorn Helgaas * Returns -ENOSPC if there's no available bus resource of the desired type. 91730afe8d0SBjorn Helgaas * Otherwise, sets the bus resource start/end to indicate the required 91830afe8d0SBjorn Helgaas * size, adds things to realloc_head (if supplied), and returns 0. 919c8adf9a3SRam Pai */ 92028760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 9215b285415SYinghai Lu unsigned long type, unsigned long type2, 9225b285415SYinghai Lu unsigned long type3, 9235b285415SYinghai Lu resource_size_t min_size, resource_size_t add_size, 924bdc4abecSYinghai Lu struct list_head *realloc_head) 9251da177e4SLinus Torvalds { 9261da177e4SLinus Torvalds struct pci_dev *dev; 927c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 928*096d4221SYinghai Lu resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */ 9291da177e4SLinus Torvalds int order, max_order; 9305b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, 9315b285415SYinghai Lu mask | IORESOURCE_PREFETCH, type); 932be768912SYinghai Lu resource_size_t children_add_size = 0; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds if (!b_res) 93530afe8d0SBjorn Helgaas return -ENOSPC; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 9381da177e4SLinus Torvalds max_order = 0; 9391da177e4SLinus Torvalds size = 0; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 9421da177e4SLinus Torvalds int i; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 9451da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 946c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 9471da177e4SLinus Torvalds 9485b285415SYinghai Lu if (r->parent || ((r->flags & mask) != type && 9495b285415SYinghai Lu (r->flags & mask) != type2 && 9505b285415SYinghai Lu (r->flags & mask) != type3)) 9511da177e4SLinus Torvalds continue; 952022edd86SZhao, Yu r_size = resource_size(r); 9532aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 9542aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 9559e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 9562aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 9572aceefcbSYinghai Lu r->end = r->start - 1; 958f7625980SBjorn Helgaas add_to_list(realloc_head, dev, r, r_size, 0/* don't care */); 9592aceefcbSYinghai Lu children_add_size += r_size; 9602aceefcbSYinghai Lu continue; 9612aceefcbSYinghai Lu } 9622aceefcbSYinghai Lu #endif 96314c8530dSAlan /* 96414c8530dSAlan * aligns[0] is for 1MB (since bridge memory 96514c8530dSAlan * windows are always at least 1MB aligned), so 96614c8530dSAlan * keep "order" from being negative for smaller 96714c8530dSAlan * resources. 96814c8530dSAlan */ 9696faf17f6SChris Wright align = pci_resource_alignment(dev, r); 9701da177e4SLinus Torvalds order = __ffs(align) - 20; 97114c8530dSAlan if (order < 0) 97214c8530dSAlan order = 0; 97314c8530dSAlan if (order >= ARRAY_SIZE(aligns)) { 974227f0647SRyan Desfosses dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n", 975227f0647SRyan Desfosses i, r, (unsigned long long) align); 9761da177e4SLinus Torvalds r->flags = 0; 9771da177e4SLinus Torvalds continue; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds size += r_size; 9801da177e4SLinus Torvalds /* Exclude ranges with size > align from 9811da177e4SLinus Torvalds calculation of the alignment. */ 9821da177e4SLinus Torvalds if (r_size == align) 9831da177e4SLinus Torvalds aligns[order] += align; 9841da177e4SLinus Torvalds if (order > max_order) 9851da177e4SLinus Torvalds max_order = order; 986be768912SYinghai Lu 9879e8bf93aSRam Pai if (realloc_head) 9889e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds } 9918308c54dSJeremy Fitzhardinge 992c121504eSGavin Shan min_align = calculate_mem_align(aligns, max_order); 9933ad94b0dSWei Yang min_align = max(min_align, window_alignment(bus, b_res->flags)); 994b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 995be768912SYinghai Lu if (children_add_size > add_size) 996be768912SYinghai Lu add_size = children_add_size; 9979e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 998a4ac9feaSYinghai Lu calculate_memsize(size, min_size, add_size, 999b42282e5SLinus Torvalds resource_size(b_res), min_align); 1000c8adf9a3SRam Pai if (!size0 && !size1) { 1001865df576SBjorn Helgaas if (b_res->start || b_res->end) 1002227f0647SRyan Desfosses dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n", 1003227f0647SRyan Desfosses b_res, &bus->busn_res); 10041da177e4SLinus Torvalds b_res->flags = 0; 100530afe8d0SBjorn Helgaas return 0; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds b_res->start = min_align; 1008c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 10095b285415SYinghai Lu b_res->flags |= IORESOURCE_STARTALIGN; 1010b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 10119e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 1012227f0647SRyan Desfosses dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n", 1013227f0647SRyan Desfosses b_res, &bus->busn_res, 1014227f0647SRyan Desfosses (unsigned long long)size1-size0); 1015b592443dSYinghai Lu } 101630afe8d0SBjorn Helgaas return 0; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10190a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 10200a2daa1cSRam Pai { 10210a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 10220a2daa1cSRam Pai return pci_cardbus_io_size; 10230a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 10240a2daa1cSRam Pai return pci_cardbus_mem_size; 10250a2daa1cSRam Pai return 0; 10260a2daa1cSRam Pai } 10270a2daa1cSRam Pai 10280a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 1029bdc4abecSYinghai Lu struct list_head *realloc_head) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 10321da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 103311848934SYinghai Lu resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 10341da177e4SLinus Torvalds u16 ctrl; 10351da177e4SLinus Torvalds 10363796f1e2SYinghai Lu if (b_res[0].parent) 10373796f1e2SYinghai Lu goto handle_b_res_1; 10381da177e4SLinus Torvalds /* 10391da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 10401da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 10411da177e4SLinus Torvalds */ 104211848934SYinghai Lu b_res[0].start = pci_cardbus_io_size; 104311848934SYinghai Lu b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; 104411848934SYinghai Lu b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 104511848934SYinghai Lu if (realloc_head) { 104611848934SYinghai Lu b_res[0].end -= pci_cardbus_io_size; 104711848934SYinghai Lu add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 104811848934SYinghai Lu pci_cardbus_io_size); 104911848934SYinghai Lu } 10501da177e4SLinus Torvalds 10513796f1e2SYinghai Lu handle_b_res_1: 10523796f1e2SYinghai Lu if (b_res[1].parent) 10533796f1e2SYinghai Lu goto handle_b_res_2; 105411848934SYinghai Lu b_res[1].start = pci_cardbus_io_size; 105511848934SYinghai Lu b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; 105611848934SYinghai Lu b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 105711848934SYinghai Lu if (realloc_head) { 105811848934SYinghai Lu b_res[1].end -= pci_cardbus_io_size; 105911848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 106011848934SYinghai Lu pci_cardbus_io_size); 106111848934SYinghai Lu } 10621da177e4SLinus Torvalds 10633796f1e2SYinghai Lu handle_b_res_2: 1064dcef0d06SYinghai Lu /* MEM1 must not be pref mmio */ 1065dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1066dcef0d06SYinghai Lu if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 1067dcef0d06SYinghai Lu ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 1068dcef0d06SYinghai Lu pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1069dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1070dcef0d06SYinghai Lu } 1071dcef0d06SYinghai Lu 10721da177e4SLinus Torvalds /* 10731da177e4SLinus Torvalds * Check whether prefetchable memory is supported 10741da177e4SLinus Torvalds * by this bridge. 10751da177e4SLinus Torvalds */ 10761da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 10771da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 10781da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 10791da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 10801da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10833796f1e2SYinghai Lu if (b_res[2].parent) 10843796f1e2SYinghai Lu goto handle_b_res_3; 10851da177e4SLinus Torvalds /* 10861da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 10871da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 10881da177e4SLinus Torvalds * twice the size. 10891da177e4SLinus Torvalds */ 10901da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 109111848934SYinghai Lu b_res[2].start = pci_cardbus_mem_size; 109211848934SYinghai Lu b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; 109311848934SYinghai Lu b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 109411848934SYinghai Lu IORESOURCE_STARTALIGN; 109511848934SYinghai Lu if (realloc_head) { 109611848934SYinghai Lu b_res[2].end -= pci_cardbus_mem_size; 109711848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+2, 109811848934SYinghai Lu pci_cardbus_mem_size, pci_cardbus_mem_size); 10991da177e4SLinus Torvalds } 11000a2daa1cSRam Pai 110111848934SYinghai Lu /* reduce that to half */ 110211848934SYinghai Lu b_res_3_size = pci_cardbus_mem_size; 110311848934SYinghai Lu } 110411848934SYinghai Lu 11053796f1e2SYinghai Lu handle_b_res_3: 11063796f1e2SYinghai Lu if (b_res[3].parent) 11073796f1e2SYinghai Lu goto handle_done; 110811848934SYinghai Lu b_res[3].start = pci_cardbus_mem_size; 110911848934SYinghai Lu b_res[3].end = b_res[3].start + b_res_3_size - 1; 111011848934SYinghai Lu b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 111111848934SYinghai Lu if (realloc_head) { 111211848934SYinghai Lu b_res[3].end -= b_res_3_size; 111311848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+3, b_res_3_size, 111411848934SYinghai Lu pci_cardbus_mem_size); 111511848934SYinghai Lu } 11163796f1e2SYinghai Lu 11173796f1e2SYinghai Lu handle_done: 11183796f1e2SYinghai Lu ; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 112110874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) 11221da177e4SLinus Torvalds { 11231da177e4SLinus Torvalds struct pci_dev *dev; 11245b285415SYinghai Lu unsigned long mask, prefmask, type2 = 0, type3 = 0; 1125c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 11265b285415SYinghai Lu struct resource *b_res; 112730afe8d0SBjorn Helgaas int ret; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 11301da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 11311da177e4SLinus Torvalds if (!b) 11321da177e4SLinus Torvalds continue; 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds switch (dev->class >> 8) { 11351da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 11369e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 11371da177e4SLinus Torvalds break; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 11401da177e4SLinus Torvalds default: 11419e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 11421da177e4SLinus Torvalds break; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds /* The root bus? */ 11472ba29e27SWei Yang if (pci_is_root_bus(bus)) 11481da177e4SLinus Torvalds return; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds switch (bus->self->class >> 8) { 11511da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 11521da177e4SLinus Torvalds /* don't size cardbuses yet. */ 11531da177e4SLinus Torvalds break; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 11561da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 115728760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 1158c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 1159c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 116028760489SEric W. Biederman } 116167d29b5cSBjorn Helgaas /* Fall through */ 11621da177e4SLinus Torvalds default: 116319aa7ee4SYinghai Lu pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 116419aa7ee4SYinghai Lu additional_io_size, realloc_head); 116567d29b5cSBjorn Helgaas 116667d29b5cSBjorn Helgaas /* 116767d29b5cSBjorn Helgaas * If there's a 64-bit prefetchable MMIO window, compute 116867d29b5cSBjorn Helgaas * the size required to put all 64-bit prefetchable 116967d29b5cSBjorn Helgaas * resources in it. 117067d29b5cSBjorn Helgaas */ 11715b285415SYinghai Lu b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES]; 11721da177e4SLinus Torvalds mask = IORESOURCE_MEM; 11731da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 11745b285415SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 11755b285415SYinghai Lu prefmask |= IORESOURCE_MEM_64; 117630afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 11775b285415SYinghai Lu prefmask, prefmask, 117819aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 117930afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 118067d29b5cSBjorn Helgaas 11815b285415SYinghai Lu /* 118267d29b5cSBjorn Helgaas * If successful, all non-prefetchable resources 118367d29b5cSBjorn Helgaas * and any 32-bit prefetchable resources will go in 118467d29b5cSBjorn Helgaas * the non-prefetchable window. 118567d29b5cSBjorn Helgaas */ 118667d29b5cSBjorn Helgaas if (ret == 0) { 11875b285415SYinghai Lu mask = prefmask; 11885b285415SYinghai Lu type2 = prefmask & ~IORESOURCE_MEM_64; 11895b285415SYinghai Lu type3 = prefmask & ~IORESOURCE_PREFETCH; 11905b285415SYinghai Lu } 11915b285415SYinghai Lu } 119267d29b5cSBjorn Helgaas 119367d29b5cSBjorn Helgaas /* 119467d29b5cSBjorn Helgaas * If there is no 64-bit prefetchable window, compute the 119567d29b5cSBjorn Helgaas * size required to put all prefetchable resources in the 119667d29b5cSBjorn Helgaas * 32-bit prefetchable window (if there is one). 119767d29b5cSBjorn Helgaas */ 11985b285415SYinghai Lu if (!type2) { 11995b285415SYinghai Lu prefmask &= ~IORESOURCE_MEM_64; 120030afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12015b285415SYinghai Lu prefmask, prefmask, 12025b285415SYinghai Lu realloc_head ? 0 : additional_mem_size, 120330afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 120467d29b5cSBjorn Helgaas 120567d29b5cSBjorn Helgaas /* 120667d29b5cSBjorn Helgaas * If successful, only non-prefetchable resources 120767d29b5cSBjorn Helgaas * will go in the non-prefetchable window. 120867d29b5cSBjorn Helgaas */ 120967d29b5cSBjorn Helgaas if (ret == 0) 12105b285415SYinghai Lu mask = prefmask; 121128760489SEric W. Biederman else 1212c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 121367d29b5cSBjorn Helgaas 12145b285415SYinghai Lu type2 = type3 = IORESOURCE_MEM; 12155b285415SYinghai Lu } 121667d29b5cSBjorn Helgaas 121767d29b5cSBjorn Helgaas /* 121867d29b5cSBjorn Helgaas * Compute the size required to put everything else in the 121967d29b5cSBjorn Helgaas * non-prefetchable window. This includes: 122067d29b5cSBjorn Helgaas * 122167d29b5cSBjorn Helgaas * - all non-prefetchable resources 122267d29b5cSBjorn Helgaas * - 32-bit prefetchable resources if there's a 64-bit 122367d29b5cSBjorn Helgaas * prefetchable window or no prefetchable window at all 122467d29b5cSBjorn Helgaas * - 64-bit prefetchable resources if there's no 122567d29b5cSBjorn Helgaas * prefetchable window at all 122667d29b5cSBjorn Helgaas * 122767d29b5cSBjorn Helgaas * Note that the strategy in __pci_assign_resource() must 122867d29b5cSBjorn Helgaas * match that used here. Specifically, we cannot put a 122967d29b5cSBjorn Helgaas * 32-bit prefetchable resource in a 64-bit prefetchable 123067d29b5cSBjorn Helgaas * window. 123167d29b5cSBjorn Helgaas */ 12325b285415SYinghai Lu pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, 123319aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 123419aa7ee4SYinghai Lu additional_mem_size, realloc_head); 12351da177e4SLinus Torvalds break; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds } 1238c8adf9a3SRam Pai 123910874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus) 1240c8adf9a3SRam Pai { 1241c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 1242c8adf9a3SRam Pai } 12431da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 12441da177e4SLinus Torvalds 124510874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus, 1246bdc4abecSYinghai Lu struct list_head *realloc_head, 1247bdc4abecSYinghai Lu struct list_head *fail_head) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds struct pci_bus *b; 12501da177e4SLinus Torvalds struct pci_dev *dev; 12511da177e4SLinus Torvalds 12529e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 12551da177e4SLinus Torvalds b = dev->subordinate; 12561da177e4SLinus Torvalds if (!b) 12571da177e4SLinus Torvalds continue; 12581da177e4SLinus Torvalds 12599e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds switch (dev->class >> 8) { 12621da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 12636841ec68SYinghai Lu if (!pci_is_enabled(dev)) 12641da177e4SLinus Torvalds pci_setup_bridge(b); 12651da177e4SLinus Torvalds break; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 12681da177e4SLinus Torvalds pci_setup_cardbus(b); 12691da177e4SLinus Torvalds break; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds default: 1272227f0647SRyan Desfosses dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n", 1273227f0647SRyan Desfosses pci_domain_nr(b), b->number); 12741da177e4SLinus Torvalds break; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds } 1278568ddef8SYinghai Lu 127910874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus) 1280568ddef8SYinghai Lu { 1281c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 1282568ddef8SYinghai Lu } 12831da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 12841da177e4SLinus Torvalds 128510874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge, 1286bdc4abecSYinghai Lu struct list_head *add_head, 1287bdc4abecSYinghai Lu struct list_head *fail_head) 12886841ec68SYinghai Lu { 12896841ec68SYinghai Lu struct pci_bus *b; 12906841ec68SYinghai Lu 12918424d759SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, 12928424d759SYinghai Lu add_head, fail_head); 12936841ec68SYinghai Lu 12946841ec68SYinghai Lu b = bridge->subordinate; 12956841ec68SYinghai Lu if (!b) 12966841ec68SYinghai Lu return; 12976841ec68SYinghai Lu 12988424d759SYinghai Lu __pci_bus_assign_resources(b, add_head, fail_head); 12996841ec68SYinghai Lu 13006841ec68SYinghai Lu switch (bridge->class >> 8) { 13016841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 13026841ec68SYinghai Lu pci_setup_bridge(b); 13036841ec68SYinghai Lu break; 13046841ec68SYinghai Lu 13056841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 13066841ec68SYinghai Lu pci_setup_cardbus(b); 13076841ec68SYinghai Lu break; 13086841ec68SYinghai Lu 13096841ec68SYinghai Lu default: 1310227f0647SRyan Desfosses dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n", 1311227f0647SRyan Desfosses pci_domain_nr(b), b->number); 13126841ec68SYinghai Lu break; 13136841ec68SYinghai Lu } 13146841ec68SYinghai Lu } 13155009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 13165009b460SYinghai Lu unsigned long type) 13175009b460SYinghai Lu { 13185b285415SYinghai Lu struct pci_dev *dev = bus->self; 13195009b460SYinghai Lu struct resource *r; 13205009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 13215b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 13225b285415SYinghai Lu unsigned old_flags = 0; 13235b285415SYinghai Lu struct resource *b_res; 13245b285415SYinghai Lu int idx = 1; 13255009b460SYinghai Lu 13265b285415SYinghai Lu b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; 13275b285415SYinghai Lu 13285b285415SYinghai Lu /* 13295b285415SYinghai Lu * 1. if there is io port assign fail, will release bridge 13305b285415SYinghai Lu * io port. 13315b285415SYinghai Lu * 2. if there is non pref mmio assign fail, release bridge 13325b285415SYinghai Lu * nonpref mmio. 13335b285415SYinghai Lu * 3. if there is 64bit pref mmio assign fail, and bridge pref 13345b285415SYinghai Lu * is 64bit, release bridge pref mmio. 13355b285415SYinghai Lu * 4. if there is pref mmio assign fail, and bridge pref is 13365b285415SYinghai Lu * 32bit mmio, release bridge pref mmio 13375b285415SYinghai Lu * 5. if there is pref mmio assign fail, and bridge pref is not 13385b285415SYinghai Lu * assigned, release bridge nonpref mmio. 13395b285415SYinghai Lu */ 13405b285415SYinghai Lu if (type & IORESOURCE_IO) 13415b285415SYinghai Lu idx = 0; 13425b285415SYinghai Lu else if (!(type & IORESOURCE_PREFETCH)) 13435b285415SYinghai Lu idx = 1; 13445b285415SYinghai Lu else if ((type & IORESOURCE_MEM_64) && 13455b285415SYinghai Lu (b_res[2].flags & IORESOURCE_MEM_64)) 13465b285415SYinghai Lu idx = 2; 13475b285415SYinghai Lu else if (!(b_res[2].flags & IORESOURCE_MEM_64) && 13485b285415SYinghai Lu (b_res[2].flags & IORESOURCE_PREFETCH)) 13495b285415SYinghai Lu idx = 2; 13505b285415SYinghai Lu else 13515b285415SYinghai Lu idx = 1; 13525b285415SYinghai Lu 13535b285415SYinghai Lu r = &b_res[idx]; 13545b285415SYinghai Lu 13555009b460SYinghai Lu if (!r->parent) 13565b285415SYinghai Lu return; 13575b285415SYinghai Lu 13585009b460SYinghai Lu /* 13595009b460SYinghai Lu * if there are children under that, we should release them 13605009b460SYinghai Lu * all 13615009b460SYinghai Lu */ 13625009b460SYinghai Lu release_child_resources(r); 13635009b460SYinghai Lu if (!release_resource(r)) { 13645b285415SYinghai Lu type = old_flags = r->flags & type_mask; 13655b285415SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n", 13665b285415SYinghai Lu PCI_BRIDGE_RESOURCES + idx, r); 13675009b460SYinghai Lu /* keep the old size */ 13685009b460SYinghai Lu r->end = resource_size(r) - 1; 13695009b460SYinghai Lu r->start = 0; 13705009b460SYinghai Lu r->flags = 0; 13715009b460SYinghai Lu 13725009b460SYinghai Lu /* avoiding touch the one without PREF */ 13735009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 13745009b460SYinghai Lu type = IORESOURCE_PREFETCH; 13755009b460SYinghai Lu __pci_setup_bridge(bus, type); 13765b285415SYinghai Lu /* for next child res under same bridge */ 13775b285415SYinghai Lu r->flags = old_flags; 13785009b460SYinghai Lu } 13795009b460SYinghai Lu } 13805009b460SYinghai Lu 13815009b460SYinghai Lu enum release_type { 13825009b460SYinghai Lu leaf_only, 13835009b460SYinghai Lu whole_subtree, 13845009b460SYinghai Lu }; 13855009b460SYinghai Lu /* 13865009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 13875009b460SYinghai Lu * so we can allocate big new one later 13885009b460SYinghai Lu */ 138910874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus, 13905009b460SYinghai Lu unsigned long type, 13915009b460SYinghai Lu enum release_type rel_type) 13925009b460SYinghai Lu { 13935009b460SYinghai Lu struct pci_dev *dev; 13945009b460SYinghai Lu bool is_leaf_bridge = true; 13955009b460SYinghai Lu 13965009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 13975009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 13985009b460SYinghai Lu if (!b) 13995009b460SYinghai Lu continue; 14005009b460SYinghai Lu 14015009b460SYinghai Lu is_leaf_bridge = false; 14025009b460SYinghai Lu 14035009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 14045009b460SYinghai Lu continue; 14055009b460SYinghai Lu 14065009b460SYinghai Lu if (rel_type == whole_subtree) 14075009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 14085009b460SYinghai Lu whole_subtree); 14095009b460SYinghai Lu } 14105009b460SYinghai Lu 14115009b460SYinghai Lu if (pci_is_root_bus(bus)) 14125009b460SYinghai Lu return; 14135009b460SYinghai Lu 14145009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 14155009b460SYinghai Lu return; 14165009b460SYinghai Lu 14175009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 14185009b460SYinghai Lu pci_bridge_release_resources(bus, type); 14195009b460SYinghai Lu } 14205009b460SYinghai Lu 142176fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 142276fbc263SYinghai Lu { 142389a74eccSBjorn Helgaas struct resource *res; 142476fbc263SYinghai Lu int i; 142576fbc263SYinghai Lu 142689a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 14277c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 142876fbc263SYinghai Lu continue; 142976fbc263SYinghai Lu 1430c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 143176fbc263SYinghai Lu } 143276fbc263SYinghai Lu } 143376fbc263SYinghai Lu 143476fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 143576fbc263SYinghai Lu { 143676fbc263SYinghai Lu struct pci_bus *b; 143776fbc263SYinghai Lu struct pci_dev *dev; 143876fbc263SYinghai Lu 143976fbc263SYinghai Lu 144076fbc263SYinghai Lu pci_bus_dump_res(bus); 144176fbc263SYinghai Lu 144276fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 144376fbc263SYinghai Lu b = dev->subordinate; 144476fbc263SYinghai Lu if (!b) 144576fbc263SYinghai Lu continue; 144676fbc263SYinghai Lu 144776fbc263SYinghai Lu pci_bus_dump_resources(b); 144876fbc263SYinghai Lu } 144976fbc263SYinghai Lu } 145076fbc263SYinghai Lu 1451ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus) 1452da7822e5SYinghai Lu { 1453da7822e5SYinghai Lu int depth = 0; 1454f2a230bdSWei Yang struct pci_bus *child_bus; 1455da7822e5SYinghai Lu 1456f2a230bdSWei Yang list_for_each_entry(child_bus, &bus->children, node) { 1457da7822e5SYinghai Lu int ret; 1458da7822e5SYinghai Lu 1459f2a230bdSWei Yang ret = pci_bus_get_depth(child_bus); 1460da7822e5SYinghai Lu if (ret + 1 > depth) 1461da7822e5SYinghai Lu depth = ret + 1; 1462da7822e5SYinghai Lu } 1463da7822e5SYinghai Lu 1464da7822e5SYinghai Lu return depth; 1465da7822e5SYinghai Lu } 1466da7822e5SYinghai Lu 1467b55438fdSYinghai Lu /* 1468b55438fdSYinghai Lu * -1: undefined, will auto detect later 1469b55438fdSYinghai Lu * 0: disabled by user 1470b55438fdSYinghai Lu * 1: disabled by auto detect 1471b55438fdSYinghai Lu * 2: enabled by user 1472b55438fdSYinghai Lu * 3: enabled by auto detect 1473b55438fdSYinghai Lu */ 1474b55438fdSYinghai Lu enum enable_type { 1475b55438fdSYinghai Lu undefined = -1, 1476b55438fdSYinghai Lu user_disabled, 1477b55438fdSYinghai Lu auto_disabled, 1478b55438fdSYinghai Lu user_enabled, 1479b55438fdSYinghai Lu auto_enabled, 1480b55438fdSYinghai Lu }; 1481b55438fdSYinghai Lu 1482ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined; 1483b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str) 1484b55438fdSYinghai Lu { 1485b55438fdSYinghai Lu if (!strncmp(str, "off", 3)) 1486b55438fdSYinghai Lu pci_realloc_enable = user_disabled; 1487b55438fdSYinghai Lu else if (!strncmp(str, "on", 2)) 1488b55438fdSYinghai Lu pci_realloc_enable = user_enabled; 1489b55438fdSYinghai Lu } 1490ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable) 1491b55438fdSYinghai Lu { 1492967260cdSYinghai Lu return enable >= user_enabled; 1493b55438fdSYinghai Lu } 1494f483d392SRam Pai 1495b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) 1496ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data) 1497223d96fcSYinghai Lu { 1498b07f2ebcSYinghai Lu int i; 1499223d96fcSYinghai Lu bool *unassigned = data; 1500b07f2ebcSYinghai Lu 1501b07f2ebcSYinghai Lu for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) { 1502b07f2ebcSYinghai Lu struct resource *r = &dev->resource[i]; 1503fa216bf4SYinghai Lu struct pci_bus_region region; 1504b07f2ebcSYinghai Lu 1505223d96fcSYinghai Lu /* Not assigned or rejected by kernel? */ 1506fa216bf4SYinghai Lu if (!r->flags) 1507fa216bf4SYinghai Lu continue; 1508b07f2ebcSYinghai Lu 1509fc279850SYinghai Lu pcibios_resource_to_bus(dev->bus, ®ion, r); 1510fa216bf4SYinghai Lu if (!region.start) { 1511223d96fcSYinghai Lu *unassigned = true; 1512223d96fcSYinghai Lu return 1; /* return early from pci_walk_bus() */ 1513b07f2ebcSYinghai Lu } 1514b07f2ebcSYinghai Lu } 1515b07f2ebcSYinghai Lu 1516223d96fcSYinghai Lu return 0; 1517223d96fcSYinghai Lu } 1518223d96fcSYinghai Lu 1519ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1520967260cdSYinghai Lu enum enable_type enable_local) 1521223d96fcSYinghai Lu { 1522223d96fcSYinghai Lu bool unassigned = false; 1523223d96fcSYinghai Lu 1524967260cdSYinghai Lu if (enable_local != undefined) 1525967260cdSYinghai Lu return enable_local; 1526223d96fcSYinghai Lu 1527223d96fcSYinghai Lu pci_walk_bus(bus, iov_resources_unassigned, &unassigned); 1528967260cdSYinghai Lu if (unassigned) 1529967260cdSYinghai Lu return auto_enabled; 1530967260cdSYinghai Lu 1531967260cdSYinghai Lu return enable_local; 1532b07f2ebcSYinghai Lu } 1533223d96fcSYinghai Lu #else 1534ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1535967260cdSYinghai Lu enum enable_type enable_local) 1536967260cdSYinghai Lu { 1537967260cdSYinghai Lu return enable_local; 1538b07f2ebcSYinghai Lu } 1539b07f2ebcSYinghai Lu #endif 1540b07f2ebcSYinghai Lu 1541da7822e5SYinghai Lu /* 1542da7822e5SYinghai Lu * first try will not touch pci bridge res 1543da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1544f7625980SBjorn Helgaas * will stop till to the max depth if can not find good one 1545da7822e5SYinghai Lu */ 154639772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) 15471da177e4SLinus Torvalds { 1548bdc4abecSYinghai Lu LIST_HEAD(realloc_head); /* list of resources that 1549c8adf9a3SRam Pai want additional resources */ 1550bdc4abecSYinghai Lu struct list_head *add_list = NULL; 1551da7822e5SYinghai Lu int tried_times = 0; 1552da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1553bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1554b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 1555da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 15565b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 155719aa7ee4SYinghai Lu int pci_try_num = 1; 155855ed83a6SYinghai Lu enum enable_type enable_local; 1559da7822e5SYinghai Lu 156019aa7ee4SYinghai Lu /* don't realloc if asked to do so */ 156155ed83a6SYinghai Lu enable_local = pci_realloc_detect(bus, pci_realloc_enable); 1562967260cdSYinghai Lu if (pci_realloc_enabled(enable_local)) { 156355ed83a6SYinghai Lu int max_depth = pci_bus_get_depth(bus); 156419aa7ee4SYinghai Lu 1565da7822e5SYinghai Lu pci_try_num = max_depth + 1; 156655ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 156755ed83a6SYinghai Lu "max bus depth: %d pci_try_num: %d\n", 1568da7822e5SYinghai Lu max_depth, pci_try_num); 156919aa7ee4SYinghai Lu } 1570da7822e5SYinghai Lu 1571da7822e5SYinghai Lu again: 157219aa7ee4SYinghai Lu /* 157319aa7ee4SYinghai Lu * last try will use add_list, otherwise will try good to have as 157419aa7ee4SYinghai Lu * must have, so can realloc parent bridge resource 157519aa7ee4SYinghai Lu */ 157619aa7ee4SYinghai Lu if (tried_times + 1 == pci_try_num) 1577bdc4abecSYinghai Lu add_list = &realloc_head; 15781da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 15791da177e4SLinus Torvalds subordinate buses. */ 158019aa7ee4SYinghai Lu __pci_bus_size_bridges(bus, add_list); 1581c8adf9a3SRam Pai 15821da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1583bdc4abecSYinghai Lu __pci_bus_assign_resources(bus, add_list, &fail_head); 158419aa7ee4SYinghai Lu if (add_list) 1585bdc4abecSYinghai Lu BUG_ON(!list_empty(add_list)); 1586da7822e5SYinghai Lu tried_times++; 1587da7822e5SYinghai Lu 1588da7822e5SYinghai Lu /* any device complain? */ 1589bdc4abecSYinghai Lu if (list_empty(&fail_head)) 1590928bea96SYinghai Lu goto dump; 1591f483d392SRam Pai 15920c5be0cbSYinghai Lu if (tried_times >= pci_try_num) { 1593967260cdSYinghai Lu if (enable_local == undefined) 159455ed83a6SYinghai Lu dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n"); 1595967260cdSYinghai Lu else if (enable_local == auto_enabled) 159655ed83a6SYinghai Lu dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); 1597eb572e7cSYinghai Lu 1598bffc56d4SYinghai Lu free_list(&fail_head); 1599928bea96SYinghai Lu goto dump; 1600da7822e5SYinghai Lu } 1601da7822e5SYinghai Lu 160255ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 160355ed83a6SYinghai Lu "No. %d try to assign unassigned res\n", tried_times + 1); 1604da7822e5SYinghai Lu 1605da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1606da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1607da7822e5SYinghai Lu rel_type = whole_subtree; 1608da7822e5SYinghai Lu 1609da7822e5SYinghai Lu /* 1610da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1611da7822e5SYinghai Lu * child device under that bridge 1612da7822e5SYinghai Lu */ 161361e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 161461e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 1615b9b0bba9SYinghai Lu fail_res->flags & type_mask, 1616da7822e5SYinghai Lu rel_type); 161761e83cddSYinghai Lu 1618da7822e5SYinghai Lu /* restore size and flags */ 1619b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1620b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 1621da7822e5SYinghai Lu 1622b9b0bba9SYinghai Lu res->start = fail_res->start; 1623b9b0bba9SYinghai Lu res->end = fail_res->end; 1624b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1625b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 1626da7822e5SYinghai Lu res->flags = 0; 1627da7822e5SYinghai Lu } 1628bffc56d4SYinghai Lu free_list(&fail_head); 1629da7822e5SYinghai Lu 1630da7822e5SYinghai Lu goto again; 1631da7822e5SYinghai Lu 1632928bea96SYinghai Lu dump: 163376fbc263SYinghai Lu /* dump the resource on buses */ 163476fbc263SYinghai Lu pci_bus_dump_resources(bus); 163576fbc263SYinghai Lu } 16366841ec68SYinghai Lu 163755ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void) 163855ed83a6SYinghai Lu { 163955ed83a6SYinghai Lu struct pci_bus *root_bus; 164055ed83a6SYinghai Lu 164155ed83a6SYinghai Lu list_for_each_entry(root_bus, &pci_root_buses, node) 164255ed83a6SYinghai Lu pci_assign_unassigned_root_bus_resources(root_bus); 164355ed83a6SYinghai Lu } 164455ed83a6SYinghai Lu 16456841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 16466841ec68SYinghai Lu { 16476841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 1648bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 16498424d759SYinghai Lu want additional resources */ 165032180e40SYinghai Lu int tried_times = 0; 1651bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1652b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 16536841ec68SYinghai Lu int retval; 165432180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 165532180e40SYinghai Lu IORESOURCE_PREFETCH; 16566841ec68SYinghai Lu 165732180e40SYinghai Lu again: 16588424d759SYinghai Lu __pci_bus_size_bridges(parent, &add_list); 1659bdc4abecSYinghai Lu __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 1660bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 166132180e40SYinghai Lu tried_times++; 166232180e40SYinghai Lu 1663bdc4abecSYinghai Lu if (list_empty(&fail_head)) 16643f579c34SYinghai Lu goto enable_all; 166532180e40SYinghai Lu 166632180e40SYinghai Lu if (tried_times >= 2) { 166732180e40SYinghai Lu /* still fail, don't need to try more */ 1668bffc56d4SYinghai Lu free_list(&fail_head); 16693f579c34SYinghai Lu goto enable_all; 167032180e40SYinghai Lu } 167132180e40SYinghai Lu 167232180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 167332180e40SYinghai Lu tried_times + 1); 167432180e40SYinghai Lu 167532180e40SYinghai Lu /* 167632180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 167732180e40SYinghai Lu * child device under that bridge 167832180e40SYinghai Lu */ 167961e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 168061e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 168161e83cddSYinghai Lu fail_res->flags & type_mask, 168232180e40SYinghai Lu whole_subtree); 168361e83cddSYinghai Lu 168432180e40SYinghai Lu /* restore size and flags */ 1685b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1686b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 168732180e40SYinghai Lu 1688b9b0bba9SYinghai Lu res->start = fail_res->start; 1689b9b0bba9SYinghai Lu res->end = fail_res->end; 1690b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1691b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 169232180e40SYinghai Lu res->flags = 0; 169332180e40SYinghai Lu } 1694bffc56d4SYinghai Lu free_list(&fail_head); 169532180e40SYinghai Lu 169632180e40SYinghai Lu goto again; 16973f579c34SYinghai Lu 16983f579c34SYinghai Lu enable_all: 16993f579c34SYinghai Lu retval = pci_reenable_device(bridge); 17009fc9eea0SBjorn Helgaas if (retval) 17019fc9eea0SBjorn Helgaas dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval); 17023f579c34SYinghai Lu pci_set_master(bridge); 17036841ec68SYinghai Lu } 17046841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 17059b03088fSYinghai Lu 170617787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus) 17079b03088fSYinghai Lu { 17089b03088fSYinghai Lu struct pci_dev *dev; 1709bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 17109b03088fSYinghai Lu want additional resources */ 17119b03088fSYinghai Lu 17129b03088fSYinghai Lu down_read(&pci_bus_sem); 17139b03088fSYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 17146788a51fSYijing Wang if (pci_is_bridge(dev) && pci_has_subordinate(dev)) 17159b03088fSYinghai Lu __pci_bus_size_bridges(dev->subordinate, 17169b03088fSYinghai Lu &add_list); 17179b03088fSYinghai Lu up_read(&pci_bus_sem); 17189b03088fSYinghai Lu __pci_bus_assign_resources(bus, &add_list, NULL); 1719bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 172017787940SYinghai Lu } 1721