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. */ 5333f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge) 5341da177e4SLinus Torvalds { 535c7dabef8SBjorn Helgaas struct resource *res; 5361da177e4SLinus Torvalds struct pci_bus_region region; 5372b28ae19SBjorn Helgaas unsigned long io_mask; 5382b28ae19SBjorn Helgaas u8 io_base_lo, io_limit_lo; 5395b764b83SBjorn Helgaas u16 l; 5405b764b83SBjorn Helgaas u32 io_upper16; 5411da177e4SLinus Torvalds 5422b28ae19SBjorn Helgaas io_mask = PCI_IO_RANGE_MASK; 5432b28ae19SBjorn Helgaas if (bridge->io_window_1k) 5442b28ae19SBjorn Helgaas io_mask = PCI_IO_1K_RANGE_MASK; 5452b28ae19SBjorn Helgaas 5461da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 5473f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0]; 548fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 549c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5505b764b83SBjorn Helgaas pci_read_config_word(bridge, PCI_IO_BASE, &l); 5512b28ae19SBjorn Helgaas io_base_lo = (region.start >> 8) & io_mask; 5522b28ae19SBjorn Helgaas io_limit_lo = (region.end >> 8) & io_mask; 5535b764b83SBjorn Helgaas l = ((u16) io_limit_lo << 8) | io_base_lo; 5541da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 5551da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 556c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5577cc5997dSYinghai Lu } else { 5581da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 5591da177e4SLinus Torvalds io_upper16 = 0; 5601da177e4SLinus Torvalds l = 0x00f0; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 5631da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 5641da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 5655b764b83SBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, l); 5661da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 5671da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 5687cc5997dSYinghai Lu } 5691da177e4SLinus Torvalds 5703f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge) 5717cc5997dSYinghai Lu { 5727cc5997dSYinghai Lu struct resource *res; 5737cc5997dSYinghai Lu struct pci_bus_region region; 5747cc5997dSYinghai Lu u32 l; 5757cc5997dSYinghai Lu 5767cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 5773f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1]; 578fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 579c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 5801da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 5811da177e4SLinus Torvalds l |= region.end & 0xfff00000; 582c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5837cc5997dSYinghai Lu } else { 5841da177e4SLinus Torvalds l = 0x0000fff0; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 5877cc5997dSYinghai Lu } 5887cc5997dSYinghai Lu 5893f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge) 5907cc5997dSYinghai Lu { 5917cc5997dSYinghai Lu struct resource *res; 5927cc5997dSYinghai Lu struct pci_bus_region region; 5937cc5997dSYinghai Lu u32 l, bu, lu; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 5961da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 5971da177e4SLinus Torvalds disables PREF range, which is ok. */ 5981da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* Set up PREF base/limit. */ 601c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 6023f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2]; 603fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 604c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 6051da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6061da177e4SLinus Torvalds l |= region.end & 0xfff00000; 607c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 60813d36c24SAndrew Morton bu = upper_32_bits(region.start); 60913d36c24SAndrew Morton lu = upper_32_bits(region.end); 6101f82de10SYinghai Lu } 611c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 6127cc5997dSYinghai Lu } else { 6131da177e4SLinus Torvalds l = 0x0000fff0; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 6161da177e4SLinus Torvalds 617c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 618c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 619c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 6207cc5997dSYinghai Lu } 6217cc5997dSYinghai Lu 6227cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 6237cc5997dSYinghai Lu { 6247cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 6257cc5997dSYinghai Lu 626b918c62eSYinghai Lu dev_info(&bridge->dev, "PCI bridge to %pR\n", 627b918c62eSYinghai Lu &bus->busn_res); 6287cc5997dSYinghai Lu 6297cc5997dSYinghai Lu if (type & IORESOURCE_IO) 6303f2f4dc4SYinghai Lu pci_setup_bridge_io(bridge); 6317cc5997dSYinghai Lu 6327cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 6333f2f4dc4SYinghai Lu pci_setup_bridge_mmio(bridge); 6347cc5997dSYinghai Lu 6357cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 6363f2f4dc4SYinghai Lu pci_setup_bridge_mmio_pref(bridge); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 641e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 6427cc5997dSYinghai Lu { 6437cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 6447cc5997dSYinghai Lu IORESOURCE_PREFETCH; 6457cc5997dSYinghai Lu 6467cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 6477cc5997dSYinghai Lu } 6487cc5997dSYinghai Lu 649*8505e729SYinghai Lu 650*8505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i) 651*8505e729SYinghai Lu { 652*8505e729SYinghai Lu if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END) 653*8505e729SYinghai Lu return 0; 654*8505e729SYinghai Lu 655*8505e729SYinghai Lu if (pci_claim_resource(bridge, i) == 0) 656*8505e729SYinghai Lu return 0; /* claimed the window */ 657*8505e729SYinghai Lu 658*8505e729SYinghai Lu if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI) 659*8505e729SYinghai Lu return 0; 660*8505e729SYinghai Lu 661*8505e729SYinghai Lu if (!pci_bus_clip_resource(bridge, i)) 662*8505e729SYinghai Lu return -EINVAL; /* clipping didn't change anything */ 663*8505e729SYinghai Lu 664*8505e729SYinghai Lu switch (i - PCI_BRIDGE_RESOURCES) { 665*8505e729SYinghai Lu case 0: 666*8505e729SYinghai Lu pci_setup_bridge_io(bridge); 667*8505e729SYinghai Lu break; 668*8505e729SYinghai Lu case 1: 669*8505e729SYinghai Lu pci_setup_bridge_mmio(bridge); 670*8505e729SYinghai Lu break; 671*8505e729SYinghai Lu case 2: 672*8505e729SYinghai Lu pci_setup_bridge_mmio_pref(bridge); 673*8505e729SYinghai Lu break; 674*8505e729SYinghai Lu default: 675*8505e729SYinghai Lu return -EINVAL; 676*8505e729SYinghai Lu } 677*8505e729SYinghai Lu 678*8505e729SYinghai Lu if (pci_claim_resource(bridge, i) == 0) 679*8505e729SYinghai Lu return 0; /* claimed a smaller window */ 680*8505e729SYinghai Lu 681*8505e729SYinghai Lu return -EINVAL; 682*8505e729SYinghai Lu } 683*8505e729SYinghai Lu 6841da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 6851da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 6861da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 68796bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 6881da177e4SLinus Torvalds { 6891da177e4SLinus Torvalds u16 io; 6901da177e4SLinus Torvalds u32 pmem; 6911da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 6921da177e4SLinus Torvalds struct resource *b_res; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 6951da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6981da177e4SLinus Torvalds if (!io) { 699d2f54d9bSBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0); 7001da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 7011da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds if (io) 7041da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 705d2f54d9bSBjorn Helgaas 7061da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 7071da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 7081da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 7091da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 7101da177e4SLinus Torvalds return; 711d2f54d9bSBjorn Helgaas 7121da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 7131da177e4SLinus Torvalds if (!pmem) { 7141da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 715d2f54d9bSBjorn Helgaas 0xffe0fff0); 7161da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 7171da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 7181da177e4SLinus Torvalds } 7191f82de10SYinghai Lu if (pmem) { 7201da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 72199586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 72299586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 7231f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 72499586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 72599586105SYinghai Lu } 7261f82de10SYinghai Lu } 7271f82de10SYinghai Lu 7281f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 7291f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 7301f82de10SYinghai Lu u32 mem_base_hi, tmp; 7311f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7321f82de10SYinghai Lu &mem_base_hi); 7331f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7341f82de10SYinghai Lu 0xffffffff); 7351f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 7361f82de10SYinghai Lu if (!tmp) 7371f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 7381f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7391f82de10SYinghai Lu mem_base_hi); 7401f82de10SYinghai Lu } 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 7441da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 7451da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 7461da177e4SLinus Torvalds have non-NULL parent resource). */ 7475b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus, 7485b285415SYinghai Lu unsigned long type_mask, unsigned long type) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds int i; 7511da177e4SLinus Torvalds struct resource *r; 7521da177e4SLinus Torvalds 75389a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 754299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 755299de034SIvan Kokshaysky continue; 75655a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 7571da177e4SLinus Torvalds return r; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds return NULL; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 76213583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 76313583b16SRam Pai resource_size_t min_size, 76413583b16SRam Pai resource_size_t size1, 76513583b16SRam Pai resource_size_t old_size, 76613583b16SRam Pai resource_size_t align) 76713583b16SRam Pai { 76813583b16SRam Pai if (size < min_size) 76913583b16SRam Pai size = min_size; 77013583b16SRam Pai if (old_size == 1) 77113583b16SRam Pai old_size = 0; 77213583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 77313583b16SRam Pai flag in the struct pci_bus. */ 77413583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 77513583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 77613583b16SRam Pai #endif 77713583b16SRam Pai size = ALIGN(size + size1, align); 77813583b16SRam Pai if (size < old_size) 77913583b16SRam Pai size = old_size; 78013583b16SRam Pai return size; 78113583b16SRam Pai } 78213583b16SRam Pai 78313583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 78413583b16SRam Pai resource_size_t min_size, 78513583b16SRam Pai resource_size_t size1, 78613583b16SRam Pai resource_size_t old_size, 78713583b16SRam Pai resource_size_t align) 78813583b16SRam Pai { 78913583b16SRam Pai if (size < min_size) 79013583b16SRam Pai size = min_size; 79113583b16SRam Pai if (old_size == 1) 79213583b16SRam Pai old_size = 0; 79313583b16SRam Pai if (size < old_size) 79413583b16SRam Pai size = old_size; 79513583b16SRam Pai size = ALIGN(size + size1, align); 79613583b16SRam Pai return size; 79713583b16SRam Pai } 79813583b16SRam Pai 799ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, 800ac5ad93eSGavin Shan unsigned long type) 801ac5ad93eSGavin Shan { 802ac5ad93eSGavin Shan return 1; 803ac5ad93eSGavin Shan } 804ac5ad93eSGavin Shan 805ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */ 806ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */ 807ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */ 808ac5ad93eSGavin Shan 809ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus, 810ac5ad93eSGavin Shan unsigned long type) 811ac5ad93eSGavin Shan { 812ac5ad93eSGavin Shan resource_size_t align = 1, arch_align; 813ac5ad93eSGavin Shan 814ac5ad93eSGavin Shan if (type & IORESOURCE_MEM) 815ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_MEM_ALIGN; 816ac5ad93eSGavin Shan else if (type & IORESOURCE_IO) { 817ac5ad93eSGavin Shan /* 818ac5ad93eSGavin Shan * Per spec, I/O windows are 4K-aligned, but some 819ac5ad93eSGavin Shan * bridges have an extension to support 1K alignment. 820ac5ad93eSGavin Shan */ 821ac5ad93eSGavin Shan if (bus->self->io_window_1k) 822ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN_1K; 823ac5ad93eSGavin Shan else 824ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN; 825ac5ad93eSGavin Shan } 826ac5ad93eSGavin Shan 827ac5ad93eSGavin Shan arch_align = pcibios_window_alignment(bus, type); 828ac5ad93eSGavin Shan return max(align, arch_align); 829ac5ad93eSGavin Shan } 830ac5ad93eSGavin Shan 831c8adf9a3SRam Pai /** 832c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 833c8adf9a3SRam Pai * 834c8adf9a3SRam Pai * @bus : the bus 835c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 836c8adf9a3SRam Pai * @add_size : additional optional io window 8379e8bf93aSRam Pai * @realloc_head : track the additional io window on this list 838c8adf9a3SRam Pai * 839c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 840fd591341SYinghai Lu * since these windows have 1K or 4K granularity and the IO ranges 841c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 842c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 843c8adf9a3SRam Pai */ 844c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 845bdc4abecSYinghai Lu resource_size_t add_size, struct list_head *realloc_head) 8461da177e4SLinus Torvalds { 8471da177e4SLinus Torvalds struct pci_dev *dev; 8485b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO, 8495b285415SYinghai Lu IORESOURCE_IO); 85011251a86SWei Yang resource_size_t size = 0, size0 = 0, size1 = 0; 851be768912SYinghai Lu resource_size_t children_add_size = 0; 8522d1d6678SBjorn Helgaas resource_size_t min_align, align; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds if (!b_res) 8551da177e4SLinus Torvalds return; 8561da177e4SLinus Torvalds 8572d1d6678SBjorn Helgaas min_align = window_alignment(bus, IORESOURCE_IO); 8581da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8591da177e4SLinus Torvalds int i; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 8621da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 8631da177e4SLinus Torvalds unsigned long r_size; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 8661da177e4SLinus Torvalds continue; 867022edd86SZhao, Yu r_size = resource_size(r); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds if (r_size < 0x400) 8701da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 8711da177e4SLinus Torvalds size += r_size; 8721da177e4SLinus Torvalds else 8731da177e4SLinus Torvalds size1 += r_size; 874be768912SYinghai Lu 875fd591341SYinghai Lu align = pci_resource_alignment(dev, r); 876fd591341SYinghai Lu if (align > min_align) 877fd591341SYinghai Lu min_align = align; 878fd591341SYinghai Lu 8799e8bf93aSRam Pai if (realloc_head) 8809e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds } 883fd591341SYinghai Lu 884c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 885fd591341SYinghai Lu resource_size(b_res), min_align); 886be768912SYinghai Lu if (children_add_size > add_size) 887be768912SYinghai Lu add_size = children_add_size; 8889e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 889a4ac9feaSYinghai Lu calculate_iosize(size, min_size, add_size + size1, 890fd591341SYinghai Lu resource_size(b_res), min_align); 891c8adf9a3SRam Pai if (!size0 && !size1) { 892865df576SBjorn Helgaas if (b_res->start || b_res->end) 893227f0647SRyan Desfosses dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n", 894227f0647SRyan Desfosses b_res, &bus->busn_res); 8951da177e4SLinus Torvalds b_res->flags = 0; 8961da177e4SLinus Torvalds return; 8971da177e4SLinus Torvalds } 898fd591341SYinghai Lu 899fd591341SYinghai Lu b_res->start = min_align; 900c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 90188452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 902b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 903fd591341SYinghai Lu add_to_list(realloc_head, bus->self, b_res, size1-size0, 904fd591341SYinghai Lu min_align); 905227f0647SRyan Desfosses dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n", 906227f0647SRyan Desfosses b_res, &bus->busn_res, 90711251a86SWei Yang (unsigned long long)size1-size0); 908b592443dSYinghai Lu } 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 911c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns, 912c121504eSGavin Shan int max_order) 913c121504eSGavin Shan { 914c121504eSGavin Shan resource_size_t align = 0; 915c121504eSGavin Shan resource_size_t min_align = 0; 916c121504eSGavin Shan int order; 917c121504eSGavin Shan 918c121504eSGavin Shan for (order = 0; order <= max_order; order++) { 919c121504eSGavin Shan resource_size_t align1 = 1; 920c121504eSGavin Shan 921c121504eSGavin Shan align1 <<= (order + 20); 922c121504eSGavin Shan 923c121504eSGavin Shan if (!align) 924c121504eSGavin Shan min_align = align1; 925c121504eSGavin Shan else if (ALIGN(align + min_align, min_align) < align1) 926c121504eSGavin Shan min_align = align1 >> 1; 927c121504eSGavin Shan align += aligns[order]; 928c121504eSGavin Shan } 929c121504eSGavin Shan 930c121504eSGavin Shan return min_align; 931c121504eSGavin Shan } 932c121504eSGavin Shan 933c8adf9a3SRam Pai /** 934c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 935c8adf9a3SRam Pai * 936c8adf9a3SRam Pai * @bus : the bus 937496f70cfSWei Yang * @mask: mask the resource flag, then compare it with type 938496f70cfSWei Yang * @type: the type of free resource from bridge 9395b285415SYinghai Lu * @type2: second match type 9405b285415SYinghai Lu * @type3: third match type 941c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 942c8adf9a3SRam Pai * @add_size : additional optional memory window 9439e8bf93aSRam Pai * @realloc_head : track the additional memory window on this list 944c8adf9a3SRam Pai * 945c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 946c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 94730afe8d0SBjorn Helgaas * 94830afe8d0SBjorn Helgaas * Returns -ENOSPC if there's no available bus resource of the desired type. 94930afe8d0SBjorn Helgaas * Otherwise, sets the bus resource start/end to indicate the required 95030afe8d0SBjorn Helgaas * size, adds things to realloc_head (if supplied), and returns 0. 951c8adf9a3SRam Pai */ 95228760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 9535b285415SYinghai Lu unsigned long type, unsigned long type2, 9545b285415SYinghai Lu unsigned long type3, 9555b285415SYinghai Lu resource_size_t min_size, resource_size_t add_size, 956bdc4abecSYinghai Lu struct list_head *realloc_head) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds struct pci_dev *dev; 959c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 960096d4221SYinghai Lu resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */ 9611da177e4SLinus Torvalds int order, max_order; 9625b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, 9635b285415SYinghai Lu mask | IORESOURCE_PREFETCH, type); 964be768912SYinghai Lu resource_size_t children_add_size = 0; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds if (!b_res) 96730afe8d0SBjorn Helgaas return -ENOSPC; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 9701da177e4SLinus Torvalds max_order = 0; 9711da177e4SLinus Torvalds size = 0; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 9741da177e4SLinus Torvalds int i; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 9771da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 978c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 9791da177e4SLinus Torvalds 9805b285415SYinghai Lu if (r->parent || ((r->flags & mask) != type && 9815b285415SYinghai Lu (r->flags & mask) != type2 && 9825b285415SYinghai Lu (r->flags & mask) != type3)) 9831da177e4SLinus Torvalds continue; 984022edd86SZhao, Yu r_size = resource_size(r); 9852aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 9862aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 9879e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 9882aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 9892aceefcbSYinghai Lu r->end = r->start - 1; 990f7625980SBjorn Helgaas add_to_list(realloc_head, dev, r, r_size, 0/* don't care */); 9912aceefcbSYinghai Lu children_add_size += r_size; 9922aceefcbSYinghai Lu continue; 9932aceefcbSYinghai Lu } 9942aceefcbSYinghai Lu #endif 99514c8530dSAlan /* 99614c8530dSAlan * aligns[0] is for 1MB (since bridge memory 99714c8530dSAlan * windows are always at least 1MB aligned), so 99814c8530dSAlan * keep "order" from being negative for smaller 99914c8530dSAlan * resources. 100014c8530dSAlan */ 10016faf17f6SChris Wright align = pci_resource_alignment(dev, r); 10021da177e4SLinus Torvalds order = __ffs(align) - 20; 100314c8530dSAlan if (order < 0) 100414c8530dSAlan order = 0; 100514c8530dSAlan if (order >= ARRAY_SIZE(aligns)) { 1006227f0647SRyan Desfosses dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n", 1007227f0647SRyan Desfosses i, r, (unsigned long long) align); 10081da177e4SLinus Torvalds r->flags = 0; 10091da177e4SLinus Torvalds continue; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds size += r_size; 10121da177e4SLinus Torvalds /* Exclude ranges with size > align from 10131da177e4SLinus Torvalds calculation of the alignment. */ 10141da177e4SLinus Torvalds if (r_size == align) 10151da177e4SLinus Torvalds aligns[order] += align; 10161da177e4SLinus Torvalds if (order > max_order) 10171da177e4SLinus Torvalds max_order = order; 1018be768912SYinghai Lu 10199e8bf93aSRam Pai if (realloc_head) 10209e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds } 10238308c54dSJeremy Fitzhardinge 1024c121504eSGavin Shan min_align = calculate_mem_align(aligns, max_order); 10253ad94b0dSWei Yang min_align = max(min_align, window_alignment(bus, b_res->flags)); 1026b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 1027be768912SYinghai Lu if (children_add_size > add_size) 1028be768912SYinghai Lu add_size = children_add_size; 10299e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 1030a4ac9feaSYinghai Lu calculate_memsize(size, min_size, add_size, 1031b42282e5SLinus Torvalds resource_size(b_res), min_align); 1032c8adf9a3SRam Pai if (!size0 && !size1) { 1033865df576SBjorn Helgaas if (b_res->start || b_res->end) 1034227f0647SRyan Desfosses dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n", 1035227f0647SRyan Desfosses b_res, &bus->busn_res); 10361da177e4SLinus Torvalds b_res->flags = 0; 103730afe8d0SBjorn Helgaas return 0; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds b_res->start = min_align; 1040c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 10415b285415SYinghai Lu b_res->flags |= IORESOURCE_STARTALIGN; 1042b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 10439e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 1044227f0647SRyan Desfosses dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n", 1045227f0647SRyan Desfosses b_res, &bus->busn_res, 1046227f0647SRyan Desfosses (unsigned long long)size1-size0); 1047b592443dSYinghai Lu } 104830afe8d0SBjorn Helgaas return 0; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10510a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 10520a2daa1cSRam Pai { 10530a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 10540a2daa1cSRam Pai return pci_cardbus_io_size; 10550a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 10560a2daa1cSRam Pai return pci_cardbus_mem_size; 10570a2daa1cSRam Pai return 0; 10580a2daa1cSRam Pai } 10590a2daa1cSRam Pai 10600a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 1061bdc4abecSYinghai Lu struct list_head *realloc_head) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 10641da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 106511848934SYinghai Lu resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 10661da177e4SLinus Torvalds u16 ctrl; 10671da177e4SLinus Torvalds 10683796f1e2SYinghai Lu if (b_res[0].parent) 10693796f1e2SYinghai Lu goto handle_b_res_1; 10701da177e4SLinus Torvalds /* 10711da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 10721da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 10731da177e4SLinus Torvalds */ 107411848934SYinghai Lu b_res[0].start = pci_cardbus_io_size; 107511848934SYinghai Lu b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; 107611848934SYinghai Lu b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 107711848934SYinghai Lu if (realloc_head) { 107811848934SYinghai Lu b_res[0].end -= pci_cardbus_io_size; 107911848934SYinghai Lu add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 108011848934SYinghai Lu pci_cardbus_io_size); 108111848934SYinghai Lu } 10821da177e4SLinus Torvalds 10833796f1e2SYinghai Lu handle_b_res_1: 10843796f1e2SYinghai Lu if (b_res[1].parent) 10853796f1e2SYinghai Lu goto handle_b_res_2; 108611848934SYinghai Lu b_res[1].start = pci_cardbus_io_size; 108711848934SYinghai Lu b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; 108811848934SYinghai Lu b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 108911848934SYinghai Lu if (realloc_head) { 109011848934SYinghai Lu b_res[1].end -= pci_cardbus_io_size; 109111848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 109211848934SYinghai Lu pci_cardbus_io_size); 109311848934SYinghai Lu } 10941da177e4SLinus Torvalds 10953796f1e2SYinghai Lu handle_b_res_2: 1096dcef0d06SYinghai Lu /* MEM1 must not be pref mmio */ 1097dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1098dcef0d06SYinghai Lu if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 1099dcef0d06SYinghai Lu ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 1100dcef0d06SYinghai Lu pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1101dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1102dcef0d06SYinghai Lu } 1103dcef0d06SYinghai Lu 11041da177e4SLinus Torvalds /* 11051da177e4SLinus Torvalds * Check whether prefetchable memory is supported 11061da177e4SLinus Torvalds * by this bridge. 11071da177e4SLinus Torvalds */ 11081da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 11091da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 11101da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 11111da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 11121da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11153796f1e2SYinghai Lu if (b_res[2].parent) 11163796f1e2SYinghai Lu goto handle_b_res_3; 11171da177e4SLinus Torvalds /* 11181da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 11191da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 11201da177e4SLinus Torvalds * twice the size. 11211da177e4SLinus Torvalds */ 11221da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 112311848934SYinghai Lu b_res[2].start = pci_cardbus_mem_size; 112411848934SYinghai Lu b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; 112511848934SYinghai Lu b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 112611848934SYinghai Lu IORESOURCE_STARTALIGN; 112711848934SYinghai Lu if (realloc_head) { 112811848934SYinghai Lu b_res[2].end -= pci_cardbus_mem_size; 112911848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+2, 113011848934SYinghai Lu pci_cardbus_mem_size, pci_cardbus_mem_size); 11311da177e4SLinus Torvalds } 11320a2daa1cSRam Pai 113311848934SYinghai Lu /* reduce that to half */ 113411848934SYinghai Lu b_res_3_size = pci_cardbus_mem_size; 113511848934SYinghai Lu } 113611848934SYinghai Lu 11373796f1e2SYinghai Lu handle_b_res_3: 11383796f1e2SYinghai Lu if (b_res[3].parent) 11393796f1e2SYinghai Lu goto handle_done; 114011848934SYinghai Lu b_res[3].start = pci_cardbus_mem_size; 114111848934SYinghai Lu b_res[3].end = b_res[3].start + b_res_3_size - 1; 114211848934SYinghai Lu b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 114311848934SYinghai Lu if (realloc_head) { 114411848934SYinghai Lu b_res[3].end -= b_res_3_size; 114511848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+3, b_res_3_size, 114611848934SYinghai Lu pci_cardbus_mem_size); 114711848934SYinghai Lu } 11483796f1e2SYinghai Lu 11493796f1e2SYinghai Lu handle_done: 11503796f1e2SYinghai Lu ; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 115310874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct pci_dev *dev; 11565b285415SYinghai Lu unsigned long mask, prefmask, type2 = 0, type3 = 0; 1157c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 11585b285415SYinghai Lu struct resource *b_res; 115930afe8d0SBjorn Helgaas int ret; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 11621da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 11631da177e4SLinus Torvalds if (!b) 11641da177e4SLinus Torvalds continue; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds switch (dev->class >> 8) { 11671da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 11689e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 11691da177e4SLinus Torvalds break; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 11721da177e4SLinus Torvalds default: 11739e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 11741da177e4SLinus Torvalds break; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds /* The root bus? */ 11792ba29e27SWei Yang if (pci_is_root_bus(bus)) 11801da177e4SLinus Torvalds return; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds switch (bus->self->class >> 8) { 11831da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 11841da177e4SLinus Torvalds /* don't size cardbuses yet. */ 11851da177e4SLinus Torvalds break; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 11881da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 118928760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 1190c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 1191c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 119228760489SEric W. Biederman } 119367d29b5cSBjorn Helgaas /* Fall through */ 11941da177e4SLinus Torvalds default: 119519aa7ee4SYinghai Lu pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 119619aa7ee4SYinghai Lu additional_io_size, realloc_head); 119767d29b5cSBjorn Helgaas 119867d29b5cSBjorn Helgaas /* 119967d29b5cSBjorn Helgaas * If there's a 64-bit prefetchable MMIO window, compute 120067d29b5cSBjorn Helgaas * the size required to put all 64-bit prefetchable 120167d29b5cSBjorn Helgaas * resources in it. 120267d29b5cSBjorn Helgaas */ 12035b285415SYinghai Lu b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES]; 12041da177e4SLinus Torvalds mask = IORESOURCE_MEM; 12051da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 12065b285415SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 12075b285415SYinghai Lu prefmask |= IORESOURCE_MEM_64; 120830afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12095b285415SYinghai Lu prefmask, prefmask, 121019aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 121130afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 121267d29b5cSBjorn Helgaas 12135b285415SYinghai Lu /* 121467d29b5cSBjorn Helgaas * If successful, all non-prefetchable resources 121567d29b5cSBjorn Helgaas * and any 32-bit prefetchable resources will go in 121667d29b5cSBjorn Helgaas * the non-prefetchable window. 121767d29b5cSBjorn Helgaas */ 121867d29b5cSBjorn Helgaas if (ret == 0) { 12195b285415SYinghai Lu mask = prefmask; 12205b285415SYinghai Lu type2 = prefmask & ~IORESOURCE_MEM_64; 12215b285415SYinghai Lu type3 = prefmask & ~IORESOURCE_PREFETCH; 12225b285415SYinghai Lu } 12235b285415SYinghai Lu } 122467d29b5cSBjorn Helgaas 122567d29b5cSBjorn Helgaas /* 122667d29b5cSBjorn Helgaas * If there is no 64-bit prefetchable window, compute the 122767d29b5cSBjorn Helgaas * size required to put all prefetchable resources in the 122867d29b5cSBjorn Helgaas * 32-bit prefetchable window (if there is one). 122967d29b5cSBjorn Helgaas */ 12305b285415SYinghai Lu if (!type2) { 12315b285415SYinghai Lu prefmask &= ~IORESOURCE_MEM_64; 123230afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12335b285415SYinghai Lu prefmask, prefmask, 12345b285415SYinghai Lu realloc_head ? 0 : additional_mem_size, 123530afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 123667d29b5cSBjorn Helgaas 123767d29b5cSBjorn Helgaas /* 123867d29b5cSBjorn Helgaas * If successful, only non-prefetchable resources 123967d29b5cSBjorn Helgaas * will go in the non-prefetchable window. 124067d29b5cSBjorn Helgaas */ 124167d29b5cSBjorn Helgaas if (ret == 0) 12425b285415SYinghai Lu mask = prefmask; 124328760489SEric W. Biederman else 1244c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 124567d29b5cSBjorn Helgaas 12465b285415SYinghai Lu type2 = type3 = IORESOURCE_MEM; 12475b285415SYinghai Lu } 124867d29b5cSBjorn Helgaas 124967d29b5cSBjorn Helgaas /* 125067d29b5cSBjorn Helgaas * Compute the size required to put everything else in the 125167d29b5cSBjorn Helgaas * non-prefetchable window. This includes: 125267d29b5cSBjorn Helgaas * 125367d29b5cSBjorn Helgaas * - all non-prefetchable resources 125467d29b5cSBjorn Helgaas * - 32-bit prefetchable resources if there's a 64-bit 125567d29b5cSBjorn Helgaas * prefetchable window or no prefetchable window at all 125667d29b5cSBjorn Helgaas * - 64-bit prefetchable resources if there's no 125767d29b5cSBjorn Helgaas * prefetchable window at all 125867d29b5cSBjorn Helgaas * 125967d29b5cSBjorn Helgaas * Note that the strategy in __pci_assign_resource() must 126067d29b5cSBjorn Helgaas * match that used here. Specifically, we cannot put a 126167d29b5cSBjorn Helgaas * 32-bit prefetchable resource in a 64-bit prefetchable 126267d29b5cSBjorn Helgaas * window. 126367d29b5cSBjorn Helgaas */ 12645b285415SYinghai Lu pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, 126519aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 126619aa7ee4SYinghai Lu additional_mem_size, realloc_head); 12671da177e4SLinus Torvalds break; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds } 1270c8adf9a3SRam Pai 127110874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus) 1272c8adf9a3SRam Pai { 1273c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 1274c8adf9a3SRam Pai } 12751da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 12761da177e4SLinus Torvalds 127710874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus, 1278bdc4abecSYinghai Lu struct list_head *realloc_head, 1279bdc4abecSYinghai Lu struct list_head *fail_head) 12801da177e4SLinus Torvalds { 12811da177e4SLinus Torvalds struct pci_bus *b; 12821da177e4SLinus Torvalds struct pci_dev *dev; 12831da177e4SLinus Torvalds 12849e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 12871da177e4SLinus Torvalds b = dev->subordinate; 12881da177e4SLinus Torvalds if (!b) 12891da177e4SLinus Torvalds continue; 12901da177e4SLinus Torvalds 12919e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds switch (dev->class >> 8) { 12941da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 12956841ec68SYinghai Lu if (!pci_is_enabled(dev)) 12961da177e4SLinus Torvalds pci_setup_bridge(b); 12971da177e4SLinus Torvalds break; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 13001da177e4SLinus Torvalds pci_setup_cardbus(b); 13011da177e4SLinus Torvalds break; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds default: 1304227f0647SRyan Desfosses dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n", 1305227f0647SRyan Desfosses pci_domain_nr(b), b->number); 13061da177e4SLinus Torvalds break; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds } 1310568ddef8SYinghai Lu 131110874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus) 1312568ddef8SYinghai Lu { 1313c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 1314568ddef8SYinghai Lu } 13151da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 13161da177e4SLinus Torvalds 131710874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge, 1318bdc4abecSYinghai Lu struct list_head *add_head, 1319bdc4abecSYinghai Lu struct list_head *fail_head) 13206841ec68SYinghai Lu { 13216841ec68SYinghai Lu struct pci_bus *b; 13226841ec68SYinghai Lu 13238424d759SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, 13248424d759SYinghai Lu add_head, fail_head); 13256841ec68SYinghai Lu 13266841ec68SYinghai Lu b = bridge->subordinate; 13276841ec68SYinghai Lu if (!b) 13286841ec68SYinghai Lu return; 13296841ec68SYinghai Lu 13308424d759SYinghai Lu __pci_bus_assign_resources(b, add_head, fail_head); 13316841ec68SYinghai Lu 13326841ec68SYinghai Lu switch (bridge->class >> 8) { 13336841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 13346841ec68SYinghai Lu pci_setup_bridge(b); 13356841ec68SYinghai Lu break; 13366841ec68SYinghai Lu 13376841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 13386841ec68SYinghai Lu pci_setup_cardbus(b); 13396841ec68SYinghai Lu break; 13406841ec68SYinghai Lu 13416841ec68SYinghai Lu default: 1342227f0647SRyan Desfosses dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n", 1343227f0647SRyan Desfosses pci_domain_nr(b), b->number); 13446841ec68SYinghai Lu break; 13456841ec68SYinghai Lu } 13466841ec68SYinghai Lu } 13475009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 13485009b460SYinghai Lu unsigned long type) 13495009b460SYinghai Lu { 13505b285415SYinghai Lu struct pci_dev *dev = bus->self; 13515009b460SYinghai Lu struct resource *r; 13525009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 13535b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 13545b285415SYinghai Lu unsigned old_flags = 0; 13555b285415SYinghai Lu struct resource *b_res; 13565b285415SYinghai Lu int idx = 1; 13575009b460SYinghai Lu 13585b285415SYinghai Lu b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; 13595b285415SYinghai Lu 13605b285415SYinghai Lu /* 13615b285415SYinghai Lu * 1. if there is io port assign fail, will release bridge 13625b285415SYinghai Lu * io port. 13635b285415SYinghai Lu * 2. if there is non pref mmio assign fail, release bridge 13645b285415SYinghai Lu * nonpref mmio. 13655b285415SYinghai Lu * 3. if there is 64bit pref mmio assign fail, and bridge pref 13665b285415SYinghai Lu * is 64bit, release bridge pref mmio. 13675b285415SYinghai Lu * 4. if there is pref mmio assign fail, and bridge pref is 13685b285415SYinghai Lu * 32bit mmio, release bridge pref mmio 13695b285415SYinghai Lu * 5. if there is pref mmio assign fail, and bridge pref is not 13705b285415SYinghai Lu * assigned, release bridge nonpref mmio. 13715b285415SYinghai Lu */ 13725b285415SYinghai Lu if (type & IORESOURCE_IO) 13735b285415SYinghai Lu idx = 0; 13745b285415SYinghai Lu else if (!(type & IORESOURCE_PREFETCH)) 13755b285415SYinghai Lu idx = 1; 13765b285415SYinghai Lu else if ((type & IORESOURCE_MEM_64) && 13775b285415SYinghai Lu (b_res[2].flags & IORESOURCE_MEM_64)) 13785b285415SYinghai Lu idx = 2; 13795b285415SYinghai Lu else if (!(b_res[2].flags & IORESOURCE_MEM_64) && 13805b285415SYinghai Lu (b_res[2].flags & IORESOURCE_PREFETCH)) 13815b285415SYinghai Lu idx = 2; 13825b285415SYinghai Lu else 13835b285415SYinghai Lu idx = 1; 13845b285415SYinghai Lu 13855b285415SYinghai Lu r = &b_res[idx]; 13865b285415SYinghai Lu 13875009b460SYinghai Lu if (!r->parent) 13885b285415SYinghai Lu return; 13895b285415SYinghai Lu 13905009b460SYinghai Lu /* 13915009b460SYinghai Lu * if there are children under that, we should release them 13925009b460SYinghai Lu * all 13935009b460SYinghai Lu */ 13945009b460SYinghai Lu release_child_resources(r); 13955009b460SYinghai Lu if (!release_resource(r)) { 13965b285415SYinghai Lu type = old_flags = r->flags & type_mask; 13975b285415SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n", 13985b285415SYinghai Lu PCI_BRIDGE_RESOURCES + idx, r); 13995009b460SYinghai Lu /* keep the old size */ 14005009b460SYinghai Lu r->end = resource_size(r) - 1; 14015009b460SYinghai Lu r->start = 0; 14025009b460SYinghai Lu r->flags = 0; 14035009b460SYinghai Lu 14045009b460SYinghai Lu /* avoiding touch the one without PREF */ 14055009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 14065009b460SYinghai Lu type = IORESOURCE_PREFETCH; 14075009b460SYinghai Lu __pci_setup_bridge(bus, type); 14085b285415SYinghai Lu /* for next child res under same bridge */ 14095b285415SYinghai Lu r->flags = old_flags; 14105009b460SYinghai Lu } 14115009b460SYinghai Lu } 14125009b460SYinghai Lu 14135009b460SYinghai Lu enum release_type { 14145009b460SYinghai Lu leaf_only, 14155009b460SYinghai Lu whole_subtree, 14165009b460SYinghai Lu }; 14175009b460SYinghai Lu /* 14185009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 14195009b460SYinghai Lu * so we can allocate big new one later 14205009b460SYinghai Lu */ 142110874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus, 14225009b460SYinghai Lu unsigned long type, 14235009b460SYinghai Lu enum release_type rel_type) 14245009b460SYinghai Lu { 14255009b460SYinghai Lu struct pci_dev *dev; 14265009b460SYinghai Lu bool is_leaf_bridge = true; 14275009b460SYinghai Lu 14285009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 14295009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 14305009b460SYinghai Lu if (!b) 14315009b460SYinghai Lu continue; 14325009b460SYinghai Lu 14335009b460SYinghai Lu is_leaf_bridge = false; 14345009b460SYinghai Lu 14355009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 14365009b460SYinghai Lu continue; 14375009b460SYinghai Lu 14385009b460SYinghai Lu if (rel_type == whole_subtree) 14395009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 14405009b460SYinghai Lu whole_subtree); 14415009b460SYinghai Lu } 14425009b460SYinghai Lu 14435009b460SYinghai Lu if (pci_is_root_bus(bus)) 14445009b460SYinghai Lu return; 14455009b460SYinghai Lu 14465009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 14475009b460SYinghai Lu return; 14485009b460SYinghai Lu 14495009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 14505009b460SYinghai Lu pci_bridge_release_resources(bus, type); 14515009b460SYinghai Lu } 14525009b460SYinghai Lu 145376fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 145476fbc263SYinghai Lu { 145589a74eccSBjorn Helgaas struct resource *res; 145676fbc263SYinghai Lu int i; 145776fbc263SYinghai Lu 145889a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 14597c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 146076fbc263SYinghai Lu continue; 146176fbc263SYinghai Lu 1462c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 146376fbc263SYinghai Lu } 146476fbc263SYinghai Lu } 146576fbc263SYinghai Lu 146676fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 146776fbc263SYinghai Lu { 146876fbc263SYinghai Lu struct pci_bus *b; 146976fbc263SYinghai Lu struct pci_dev *dev; 147076fbc263SYinghai Lu 147176fbc263SYinghai Lu 147276fbc263SYinghai Lu pci_bus_dump_res(bus); 147376fbc263SYinghai Lu 147476fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 147576fbc263SYinghai Lu b = dev->subordinate; 147676fbc263SYinghai Lu if (!b) 147776fbc263SYinghai Lu continue; 147876fbc263SYinghai Lu 147976fbc263SYinghai Lu pci_bus_dump_resources(b); 148076fbc263SYinghai Lu } 148176fbc263SYinghai Lu } 148276fbc263SYinghai Lu 1483ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus) 1484da7822e5SYinghai Lu { 1485da7822e5SYinghai Lu int depth = 0; 1486f2a230bdSWei Yang struct pci_bus *child_bus; 1487da7822e5SYinghai Lu 1488f2a230bdSWei Yang list_for_each_entry(child_bus, &bus->children, node) { 1489da7822e5SYinghai Lu int ret; 1490da7822e5SYinghai Lu 1491f2a230bdSWei Yang ret = pci_bus_get_depth(child_bus); 1492da7822e5SYinghai Lu if (ret + 1 > depth) 1493da7822e5SYinghai Lu depth = ret + 1; 1494da7822e5SYinghai Lu } 1495da7822e5SYinghai Lu 1496da7822e5SYinghai Lu return depth; 1497da7822e5SYinghai Lu } 1498da7822e5SYinghai Lu 1499b55438fdSYinghai Lu /* 1500b55438fdSYinghai Lu * -1: undefined, will auto detect later 1501b55438fdSYinghai Lu * 0: disabled by user 1502b55438fdSYinghai Lu * 1: disabled by auto detect 1503b55438fdSYinghai Lu * 2: enabled by user 1504b55438fdSYinghai Lu * 3: enabled by auto detect 1505b55438fdSYinghai Lu */ 1506b55438fdSYinghai Lu enum enable_type { 1507b55438fdSYinghai Lu undefined = -1, 1508b55438fdSYinghai Lu user_disabled, 1509b55438fdSYinghai Lu auto_disabled, 1510b55438fdSYinghai Lu user_enabled, 1511b55438fdSYinghai Lu auto_enabled, 1512b55438fdSYinghai Lu }; 1513b55438fdSYinghai Lu 1514ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined; 1515b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str) 1516b55438fdSYinghai Lu { 1517b55438fdSYinghai Lu if (!strncmp(str, "off", 3)) 1518b55438fdSYinghai Lu pci_realloc_enable = user_disabled; 1519b55438fdSYinghai Lu else if (!strncmp(str, "on", 2)) 1520b55438fdSYinghai Lu pci_realloc_enable = user_enabled; 1521b55438fdSYinghai Lu } 1522ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable) 1523b55438fdSYinghai Lu { 1524967260cdSYinghai Lu return enable >= user_enabled; 1525b55438fdSYinghai Lu } 1526f483d392SRam Pai 1527b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) 1528ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data) 1529223d96fcSYinghai Lu { 1530b07f2ebcSYinghai Lu int i; 1531223d96fcSYinghai Lu bool *unassigned = data; 1532b07f2ebcSYinghai Lu 1533b07f2ebcSYinghai Lu for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) { 1534b07f2ebcSYinghai Lu struct resource *r = &dev->resource[i]; 1535fa216bf4SYinghai Lu struct pci_bus_region region; 1536b07f2ebcSYinghai Lu 1537223d96fcSYinghai Lu /* Not assigned or rejected by kernel? */ 1538fa216bf4SYinghai Lu if (!r->flags) 1539fa216bf4SYinghai Lu continue; 1540b07f2ebcSYinghai Lu 1541fc279850SYinghai Lu pcibios_resource_to_bus(dev->bus, ®ion, r); 1542fa216bf4SYinghai Lu if (!region.start) { 1543223d96fcSYinghai Lu *unassigned = true; 1544223d96fcSYinghai Lu return 1; /* return early from pci_walk_bus() */ 1545b07f2ebcSYinghai Lu } 1546b07f2ebcSYinghai Lu } 1547b07f2ebcSYinghai Lu 1548223d96fcSYinghai Lu return 0; 1549223d96fcSYinghai Lu } 1550223d96fcSYinghai Lu 1551ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1552967260cdSYinghai Lu enum enable_type enable_local) 1553223d96fcSYinghai Lu { 1554223d96fcSYinghai Lu bool unassigned = false; 1555223d96fcSYinghai Lu 1556967260cdSYinghai Lu if (enable_local != undefined) 1557967260cdSYinghai Lu return enable_local; 1558223d96fcSYinghai Lu 1559223d96fcSYinghai Lu pci_walk_bus(bus, iov_resources_unassigned, &unassigned); 1560967260cdSYinghai Lu if (unassigned) 1561967260cdSYinghai Lu return auto_enabled; 1562967260cdSYinghai Lu 1563967260cdSYinghai Lu return enable_local; 1564b07f2ebcSYinghai Lu } 1565223d96fcSYinghai Lu #else 1566ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1567967260cdSYinghai Lu enum enable_type enable_local) 1568967260cdSYinghai Lu { 1569967260cdSYinghai Lu return enable_local; 1570b07f2ebcSYinghai Lu } 1571b07f2ebcSYinghai Lu #endif 1572b07f2ebcSYinghai Lu 1573da7822e5SYinghai Lu /* 1574da7822e5SYinghai Lu * first try will not touch pci bridge res 1575da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1576f7625980SBjorn Helgaas * will stop till to the max depth if can not find good one 1577da7822e5SYinghai Lu */ 157839772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) 15791da177e4SLinus Torvalds { 1580bdc4abecSYinghai Lu LIST_HEAD(realloc_head); /* list of resources that 1581c8adf9a3SRam Pai want additional resources */ 1582bdc4abecSYinghai Lu struct list_head *add_list = NULL; 1583da7822e5SYinghai Lu int tried_times = 0; 1584da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1585bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1586b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 1587da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 15885b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 158919aa7ee4SYinghai Lu int pci_try_num = 1; 159055ed83a6SYinghai Lu enum enable_type enable_local; 1591da7822e5SYinghai Lu 159219aa7ee4SYinghai Lu /* don't realloc if asked to do so */ 159355ed83a6SYinghai Lu enable_local = pci_realloc_detect(bus, pci_realloc_enable); 1594967260cdSYinghai Lu if (pci_realloc_enabled(enable_local)) { 159555ed83a6SYinghai Lu int max_depth = pci_bus_get_depth(bus); 159619aa7ee4SYinghai Lu 1597da7822e5SYinghai Lu pci_try_num = max_depth + 1; 159855ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 159955ed83a6SYinghai Lu "max bus depth: %d pci_try_num: %d\n", 1600da7822e5SYinghai Lu max_depth, pci_try_num); 160119aa7ee4SYinghai Lu } 1602da7822e5SYinghai Lu 1603da7822e5SYinghai Lu again: 160419aa7ee4SYinghai Lu /* 160519aa7ee4SYinghai Lu * last try will use add_list, otherwise will try good to have as 160619aa7ee4SYinghai Lu * must have, so can realloc parent bridge resource 160719aa7ee4SYinghai Lu */ 160819aa7ee4SYinghai Lu if (tried_times + 1 == pci_try_num) 1609bdc4abecSYinghai Lu add_list = &realloc_head; 16101da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 16111da177e4SLinus Torvalds subordinate buses. */ 161219aa7ee4SYinghai Lu __pci_bus_size_bridges(bus, add_list); 1613c8adf9a3SRam Pai 16141da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1615bdc4abecSYinghai Lu __pci_bus_assign_resources(bus, add_list, &fail_head); 161619aa7ee4SYinghai Lu if (add_list) 1617bdc4abecSYinghai Lu BUG_ON(!list_empty(add_list)); 1618da7822e5SYinghai Lu tried_times++; 1619da7822e5SYinghai Lu 1620da7822e5SYinghai Lu /* any device complain? */ 1621bdc4abecSYinghai Lu if (list_empty(&fail_head)) 1622928bea96SYinghai Lu goto dump; 1623f483d392SRam Pai 16240c5be0cbSYinghai Lu if (tried_times >= pci_try_num) { 1625967260cdSYinghai Lu if (enable_local == undefined) 162655ed83a6SYinghai Lu dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n"); 1627967260cdSYinghai Lu else if (enable_local == auto_enabled) 162855ed83a6SYinghai Lu dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); 1629eb572e7cSYinghai Lu 1630bffc56d4SYinghai Lu free_list(&fail_head); 1631928bea96SYinghai Lu goto dump; 1632da7822e5SYinghai Lu } 1633da7822e5SYinghai Lu 163455ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 163555ed83a6SYinghai Lu "No. %d try to assign unassigned res\n", tried_times + 1); 1636da7822e5SYinghai Lu 1637da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1638da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1639da7822e5SYinghai Lu rel_type = whole_subtree; 1640da7822e5SYinghai Lu 1641da7822e5SYinghai Lu /* 1642da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1643da7822e5SYinghai Lu * child device under that bridge 1644da7822e5SYinghai Lu */ 164561e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 164661e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 1647b9b0bba9SYinghai Lu fail_res->flags & type_mask, 1648da7822e5SYinghai Lu rel_type); 164961e83cddSYinghai Lu 1650da7822e5SYinghai Lu /* restore size and flags */ 1651b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1652b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 1653da7822e5SYinghai Lu 1654b9b0bba9SYinghai Lu res->start = fail_res->start; 1655b9b0bba9SYinghai Lu res->end = fail_res->end; 1656b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1657b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 1658da7822e5SYinghai Lu res->flags = 0; 1659da7822e5SYinghai Lu } 1660bffc56d4SYinghai Lu free_list(&fail_head); 1661da7822e5SYinghai Lu 1662da7822e5SYinghai Lu goto again; 1663da7822e5SYinghai Lu 1664928bea96SYinghai Lu dump: 166576fbc263SYinghai Lu /* dump the resource on buses */ 166676fbc263SYinghai Lu pci_bus_dump_resources(bus); 166776fbc263SYinghai Lu } 16686841ec68SYinghai Lu 166955ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void) 167055ed83a6SYinghai Lu { 167155ed83a6SYinghai Lu struct pci_bus *root_bus; 167255ed83a6SYinghai Lu 167355ed83a6SYinghai Lu list_for_each_entry(root_bus, &pci_root_buses, node) 167455ed83a6SYinghai Lu pci_assign_unassigned_root_bus_resources(root_bus); 167555ed83a6SYinghai Lu } 167655ed83a6SYinghai Lu 16776841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 16786841ec68SYinghai Lu { 16796841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 1680bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 16818424d759SYinghai Lu want additional resources */ 168232180e40SYinghai Lu int tried_times = 0; 1683bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1684b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 16856841ec68SYinghai Lu int retval; 168632180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1687d61b0e87SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 16886841ec68SYinghai Lu 168932180e40SYinghai Lu again: 16908424d759SYinghai Lu __pci_bus_size_bridges(parent, &add_list); 1691bdc4abecSYinghai Lu __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 1692bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 169332180e40SYinghai Lu tried_times++; 169432180e40SYinghai Lu 1695bdc4abecSYinghai Lu if (list_empty(&fail_head)) 16963f579c34SYinghai Lu goto enable_all; 169732180e40SYinghai Lu 169832180e40SYinghai Lu if (tried_times >= 2) { 169932180e40SYinghai Lu /* still fail, don't need to try more */ 1700bffc56d4SYinghai Lu free_list(&fail_head); 17013f579c34SYinghai Lu goto enable_all; 170232180e40SYinghai Lu } 170332180e40SYinghai Lu 170432180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 170532180e40SYinghai Lu tried_times + 1); 170632180e40SYinghai Lu 170732180e40SYinghai Lu /* 170832180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 170932180e40SYinghai Lu * child device under that bridge 171032180e40SYinghai Lu */ 171161e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 171261e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 171361e83cddSYinghai Lu fail_res->flags & type_mask, 171432180e40SYinghai Lu whole_subtree); 171561e83cddSYinghai Lu 171632180e40SYinghai Lu /* restore size and flags */ 1717b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1718b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 171932180e40SYinghai Lu 1720b9b0bba9SYinghai Lu res->start = fail_res->start; 1721b9b0bba9SYinghai Lu res->end = fail_res->end; 1722b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1723b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 172432180e40SYinghai Lu res->flags = 0; 172532180e40SYinghai Lu } 1726bffc56d4SYinghai Lu free_list(&fail_head); 172732180e40SYinghai Lu 172832180e40SYinghai Lu goto again; 17293f579c34SYinghai Lu 17303f579c34SYinghai Lu enable_all: 17313f579c34SYinghai Lu retval = pci_reenable_device(bridge); 17329fc9eea0SBjorn Helgaas if (retval) 17339fc9eea0SBjorn Helgaas dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval); 17343f579c34SYinghai Lu pci_set_master(bridge); 17356841ec68SYinghai Lu } 17366841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 17379b03088fSYinghai Lu 173817787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus) 17399b03088fSYinghai Lu { 17409b03088fSYinghai Lu struct pci_dev *dev; 1741bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 17429b03088fSYinghai Lu want additional resources */ 17439b03088fSYinghai Lu 17449b03088fSYinghai Lu down_read(&pci_bus_sem); 17459b03088fSYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 17466788a51fSYijing Wang if (pci_is_bridge(dev) && pci_has_subordinate(dev)) 17479b03088fSYinghai Lu __pci_bus_size_bridges(dev->subordinate, 17489b03088fSYinghai Lu &add_list); 17499b03088fSYinghai Lu up_read(&pci_bus_sem); 17509b03088fSYinghai Lu __pci_bus_assign_resources(bus, &add_list, NULL); 1751bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 175217787940SYinghai Lu } 1753