11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/pci/setup-bus.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Extruded from code written by 51da177e4SLinus Torvalds * Dave Rusling (david.rusling@reo.mts.dec.com) 61da177e4SLinus Torvalds * David Mosberger (davidm@cs.arizona.edu) 71da177e4SLinus Torvalds * David Miller (davem@redhat.com) 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Support routines for initializing a PCI subsystem. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 141da177e4SLinus Torvalds * PCI-PCI bridges cleanup, sorted resource allocation. 151da177e4SLinus Torvalds * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 161da177e4SLinus Torvalds * Converted to allocation in 3 passes, which gives 171da177e4SLinus Torvalds * tighter packing. Prefetchable range support. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/kernel.h> 221da177e4SLinus Torvalds #include <linux/module.h> 231da177e4SLinus Torvalds #include <linux/pci.h> 241da177e4SLinus Torvalds #include <linux/errno.h> 251da177e4SLinus Torvalds #include <linux/ioport.h> 261da177e4SLinus Torvalds #include <linux/cache.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 286faf17f6SChris Wright #include "pci.h" 291da177e4SLinus Torvalds 30844393f4SBjorn Helgaas unsigned int pci_flags; 3147087700SBjorn Helgaas 32bdc4abecSYinghai Lu struct pci_dev_resource { 33bdc4abecSYinghai Lu struct list_head list; 342934a0deSYinghai Lu struct resource *res; 352934a0deSYinghai Lu struct pci_dev *dev; 36568ddef8SYinghai Lu resource_size_t start; 37568ddef8SYinghai Lu resource_size_t end; 38c8adf9a3SRam Pai resource_size_t add_size; 392bbc6942SRam Pai resource_size_t min_align; 40568ddef8SYinghai Lu unsigned long flags; 41568ddef8SYinghai Lu }; 42568ddef8SYinghai Lu 43bffc56d4SYinghai Lu static void free_list(struct list_head *head) 44bffc56d4SYinghai Lu { 45bffc56d4SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 46bffc56d4SYinghai Lu 47bffc56d4SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 48bffc56d4SYinghai Lu list_del(&dev_res->list); 49bffc56d4SYinghai Lu kfree(dev_res); 50bffc56d4SYinghai Lu } 51bffc56d4SYinghai Lu } 52094732a5SRam Pai 53c8adf9a3SRam Pai /** 54c8adf9a3SRam Pai * add_to_list() - add a new resource tracker to the list 55c8adf9a3SRam Pai * @head: Head of the list 56c8adf9a3SRam Pai * @dev: device corresponding to which the resource 57c8adf9a3SRam Pai * belongs 58c8adf9a3SRam Pai * @res: The resource to be tracked 59c8adf9a3SRam Pai * @add_size: additional size to be optionally added 60c8adf9a3SRam Pai * to the resource 61c8adf9a3SRam Pai */ 62bdc4abecSYinghai Lu static int add_to_list(struct list_head *head, 63c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res, 642bbc6942SRam Pai resource_size_t add_size, resource_size_t min_align) 65568ddef8SYinghai Lu { 66764242a0SYinghai Lu struct pci_dev_resource *tmp; 67568ddef8SYinghai Lu 68bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 69568ddef8SYinghai Lu if (!tmp) { 703c78bc61SRyan Desfosses pr_warn("add_to_list: kmalloc() failed!\n"); 71ef62dfefSYinghai Lu return -ENOMEM; 72568ddef8SYinghai Lu } 73568ddef8SYinghai Lu 74568ddef8SYinghai Lu tmp->res = res; 75568ddef8SYinghai Lu tmp->dev = dev; 76568ddef8SYinghai Lu tmp->start = res->start; 77568ddef8SYinghai Lu tmp->end = res->end; 78568ddef8SYinghai Lu tmp->flags = res->flags; 79c8adf9a3SRam Pai tmp->add_size = add_size; 802bbc6942SRam Pai tmp->min_align = min_align; 81bdc4abecSYinghai Lu 82bdc4abecSYinghai Lu list_add(&tmp->list, head); 83ef62dfefSYinghai Lu 84ef62dfefSYinghai Lu return 0; 85568ddef8SYinghai Lu } 86568ddef8SYinghai Lu 87b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head, 883e6e0d80SYinghai Lu struct resource *res) 893e6e0d80SYinghai Lu { 90b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 913e6e0d80SYinghai Lu 92b9b0bba9SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 93b9b0bba9SYinghai Lu if (dev_res->res == res) { 94b9b0bba9SYinghai Lu list_del(&dev_res->list); 95b9b0bba9SYinghai Lu kfree(dev_res); 96bdc4abecSYinghai Lu break; 973e6e0d80SYinghai Lu } 983e6e0d80SYinghai Lu } 993e6e0d80SYinghai Lu } 1003e6e0d80SYinghai Lu 101d74b9027SWei Yang static struct pci_dev_resource *res_to_dev_res(struct list_head *head, 1021c372353SYinghai Lu struct resource *res) 1031c372353SYinghai Lu { 104b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res; 1051c372353SYinghai Lu 106b9b0bba9SYinghai Lu list_for_each_entry(dev_res, head, list) { 107b9b0bba9SYinghai Lu if (dev_res->res == res) { 108b592443dSYinghai Lu int idx = res - &dev_res->dev->resource[0]; 109b592443dSYinghai Lu 110b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &dev_res->dev->dev, 111d74b9027SWei Yang "res[%d]=%pR res_to_dev_res add_size %llx min_align %llx\n", 112b592443dSYinghai Lu idx, dev_res->res, 113d74b9027SWei Yang (unsigned long long)dev_res->add_size, 114d74b9027SWei Yang (unsigned long long)dev_res->min_align); 115b592443dSYinghai Lu 116d74b9027SWei Yang return dev_res; 117bdc4abecSYinghai Lu } 1183e6e0d80SYinghai Lu } 1191c372353SYinghai Lu 120d74b9027SWei Yang return NULL; 1211c372353SYinghai Lu } 1221c372353SYinghai Lu 123d74b9027SWei Yang static resource_size_t get_res_add_size(struct list_head *head, 124d74b9027SWei Yang struct resource *res) 125d74b9027SWei Yang { 126d74b9027SWei Yang struct pci_dev_resource *dev_res; 127d74b9027SWei Yang 128d74b9027SWei Yang dev_res = res_to_dev_res(head, res); 129d74b9027SWei Yang return dev_res ? dev_res->add_size : 0; 130d74b9027SWei Yang } 131d74b9027SWei Yang 132d74b9027SWei Yang static resource_size_t get_res_add_align(struct list_head *head, 133d74b9027SWei Yang struct resource *res) 134d74b9027SWei Yang { 135d74b9027SWei Yang struct pci_dev_resource *dev_res; 136d74b9027SWei Yang 137d74b9027SWei Yang dev_res = res_to_dev_res(head, res); 138d74b9027SWei Yang return dev_res ? dev_res->min_align : 0; 139d74b9027SWei Yang } 140d74b9027SWei Yang 141d74b9027SWei Yang 14278c3b329SYinghai Lu /* Sort resources by alignment */ 143bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 14478c3b329SYinghai Lu { 14578c3b329SYinghai Lu int i; 14678c3b329SYinghai Lu 14778c3b329SYinghai Lu for (i = 0; i < PCI_NUM_RESOURCES; i++) { 14878c3b329SYinghai Lu struct resource *r; 149bdc4abecSYinghai Lu struct pci_dev_resource *dev_res, *tmp; 15078c3b329SYinghai Lu resource_size_t r_align; 151bdc4abecSYinghai Lu struct list_head *n; 15278c3b329SYinghai Lu 15378c3b329SYinghai Lu r = &dev->resource[i]; 15478c3b329SYinghai Lu 15578c3b329SYinghai Lu if (r->flags & IORESOURCE_PCI_FIXED) 15678c3b329SYinghai Lu continue; 15778c3b329SYinghai Lu 15878c3b329SYinghai Lu if (!(r->flags) || r->parent) 15978c3b329SYinghai Lu continue; 16078c3b329SYinghai Lu 16178c3b329SYinghai Lu r_align = pci_resource_alignment(dev, r); 16278c3b329SYinghai Lu if (!r_align) { 16378c3b329SYinghai Lu dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n", 16478c3b329SYinghai Lu i, r); 16578c3b329SYinghai Lu continue; 16678c3b329SYinghai Lu } 16778c3b329SYinghai Lu 168bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 16978c3b329SYinghai Lu if (!tmp) 170227f0647SRyan Desfosses panic("pdev_sort_resources(): kmalloc() failed!\n"); 17178c3b329SYinghai Lu tmp->res = r; 17278c3b329SYinghai Lu tmp->dev = dev; 173bdc4abecSYinghai Lu 174bdc4abecSYinghai Lu /* fallback is smallest one or list is empty*/ 175bdc4abecSYinghai Lu n = head; 176bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 177bdc4abecSYinghai Lu resource_size_t align; 178bdc4abecSYinghai Lu 179bdc4abecSYinghai Lu align = pci_resource_alignment(dev_res->dev, 180bdc4abecSYinghai Lu dev_res->res); 181bdc4abecSYinghai Lu 182bdc4abecSYinghai Lu if (r_align > align) { 183bdc4abecSYinghai Lu n = &dev_res->list; 18478c3b329SYinghai Lu break; 18578c3b329SYinghai Lu } 18678c3b329SYinghai Lu } 187bdc4abecSYinghai Lu /* Insert it just before n*/ 188bdc4abecSYinghai Lu list_add_tail(&tmp->list, n); 18978c3b329SYinghai Lu } 19078c3b329SYinghai Lu } 19178c3b329SYinghai Lu 1926841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev, 193bdc4abecSYinghai Lu struct list_head *head) 1941da177e4SLinus Torvalds { 1951da177e4SLinus Torvalds u16 class = dev->class >> 8; 1961da177e4SLinus Torvalds 1979bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 1986841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1996841ec68SYinghai Lu return; 2001da177e4SLinus Torvalds 2019bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 20223186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 2039bded00bSKenji Kaneshige u16 command; 2049bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 2059bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 2066841ec68SYinghai Lu return; 20723186279SSatoru Takeuchi } 20823186279SSatoru Takeuchi 2096841ec68SYinghai Lu pdev_sort_resources(dev, head); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 212fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 213fc075e1dSRam Pai { 214fc075e1dSRam Pai res->start = 0; 215fc075e1dSRam Pai res->end = 0; 216fc075e1dSRam Pai res->flags = 0; 217fc075e1dSRam Pai } 218fc075e1dSRam Pai 219c8adf9a3SRam Pai /** 2209e8bf93aSRam Pai * reassign_resources_sorted() - satisfy any additional resource requests 221c8adf9a3SRam Pai * 2229e8bf93aSRam Pai * @realloc_head : head of the list tracking requests requiring additional 223c8adf9a3SRam Pai * resources 224c8adf9a3SRam Pai * @head : head of the list tracking requests with allocated 225c8adf9a3SRam Pai * resources 226c8adf9a3SRam Pai * 2279e8bf93aSRam Pai * Walk through each element of the realloc_head and try to procure 228c8adf9a3SRam Pai * additional resources for the element, provided the element 229c8adf9a3SRam Pai * is in the head list. 230c8adf9a3SRam Pai */ 231bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head, 232bdc4abecSYinghai Lu struct list_head *head) 233c8adf9a3SRam Pai { 234c8adf9a3SRam Pai struct resource *res; 235b9b0bba9SYinghai Lu struct pci_dev_resource *add_res, *tmp; 236bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 237d74b9027SWei Yang resource_size_t add_size, align; 238c8adf9a3SRam Pai int idx; 239c8adf9a3SRam Pai 240b9b0bba9SYinghai Lu list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 241bdc4abecSYinghai Lu bool found_match = false; 242bdc4abecSYinghai Lu 243b9b0bba9SYinghai Lu res = add_res->res; 244c8adf9a3SRam Pai /* skip resource that has been reset */ 245c8adf9a3SRam Pai if (!res->flags) 246c8adf9a3SRam Pai goto out; 247c8adf9a3SRam Pai 248c8adf9a3SRam Pai /* skip this resource if not found in head list */ 249bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 250bdc4abecSYinghai Lu if (dev_res->res == res) { 251bdc4abecSYinghai Lu found_match = true; 252bdc4abecSYinghai Lu break; 253c8adf9a3SRam Pai } 254bdc4abecSYinghai Lu } 255bdc4abecSYinghai Lu if (!found_match)/* just skip */ 256bdc4abecSYinghai Lu continue; 257c8adf9a3SRam Pai 258b9b0bba9SYinghai Lu idx = res - &add_res->dev->resource[0]; 259b9b0bba9SYinghai Lu add_size = add_res->add_size; 260d74b9027SWei Yang align = add_res->min_align; 2612bbc6942SRam Pai if (!resource_size(res)) { 262d74b9027SWei Yang res->start = align; 263c8adf9a3SRam Pai res->end = res->start + add_size - 1; 264b9b0bba9SYinghai Lu if (pci_assign_resource(add_res->dev, idx)) 265c8adf9a3SRam Pai reset_resource(res); 2662bbc6942SRam Pai } else { 267b9b0bba9SYinghai Lu res->flags |= add_res->flags & 268bdc4abecSYinghai Lu (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 269b9b0bba9SYinghai Lu if (pci_reassign_resource(add_res->dev, idx, 270bdc4abecSYinghai Lu add_size, align)) 271b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &add_res->dev->dev, 272b592443dSYinghai Lu "failed to add %llx res[%d]=%pR\n", 273b592443dSYinghai Lu (unsigned long long)add_size, 274b592443dSYinghai Lu idx, res); 275c8adf9a3SRam Pai } 276c8adf9a3SRam Pai out: 277b9b0bba9SYinghai Lu list_del(&add_res->list); 278b9b0bba9SYinghai Lu kfree(add_res); 279c8adf9a3SRam Pai } 280c8adf9a3SRam Pai } 281c8adf9a3SRam Pai 282c8adf9a3SRam Pai /** 283c8adf9a3SRam Pai * assign_requested_resources_sorted() - satisfy resource requests 284c8adf9a3SRam Pai * 285c8adf9a3SRam Pai * @head : head of the list tracking requests for resources 2868356aad4SWanpeng Li * @fail_head : head of the list tracking requests that could 287c8adf9a3SRam Pai * not be allocated 288c8adf9a3SRam Pai * 289c8adf9a3SRam Pai * Satisfy resource requests of each element in the list. Add 290c8adf9a3SRam Pai * requests that could not satisfied to the failed_list. 291c8adf9a3SRam Pai */ 292bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head, 293bdc4abecSYinghai Lu struct list_head *fail_head) 2946841ec68SYinghai Lu { 2956841ec68SYinghai Lu struct resource *res; 296bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 2976841ec68SYinghai Lu int idx; 2986841ec68SYinghai Lu 299bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 300bdc4abecSYinghai Lu res = dev_res->res; 301bdc4abecSYinghai Lu idx = res - &dev_res->dev->resource[0]; 302bdc4abecSYinghai Lu if (resource_size(res) && 303bdc4abecSYinghai Lu pci_assign_resource(dev_res->dev, idx)) { 304a3cb999dSYinghai Lu if (fail_head) { 3059a928660SYinghai Lu /* 3069a928660SYinghai Lu * if the failed res is for ROM BAR, and it will 3079a928660SYinghai Lu * be enabled later, don't add it to the list 3089a928660SYinghai Lu */ 3099a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 3109a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 31167cc7e26SYinghai Lu add_to_list(fail_head, 31267cc7e26SYinghai Lu dev_res->dev, res, 313f7625980SBjorn Helgaas 0 /* don't care */, 314f7625980SBjorn Helgaas 0 /* don't care */); 3159a928660SYinghai Lu } 316fc075e1dSRam Pai reset_resource(res); 317542df5deSRajesh Shah } 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 321aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head) 322aa914f5eSYinghai Lu { 323aa914f5eSYinghai Lu struct pci_dev_resource *fail_res; 324aa914f5eSYinghai Lu unsigned long mask = 0; 325aa914f5eSYinghai Lu 326aa914f5eSYinghai Lu /* check failed type */ 327aa914f5eSYinghai Lu list_for_each_entry(fail_res, fail_head, list) 328aa914f5eSYinghai Lu mask |= fail_res->flags; 329aa914f5eSYinghai Lu 330aa914f5eSYinghai Lu /* 331aa914f5eSYinghai Lu * one pref failed resource will set IORESOURCE_MEM, 332aa914f5eSYinghai Lu * as we can allocate pref in non-pref range. 333aa914f5eSYinghai Lu * Will release all assigned non-pref sibling resources 334aa914f5eSYinghai Lu * according to that bit. 335aa914f5eSYinghai Lu */ 336aa914f5eSYinghai Lu return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH); 337aa914f5eSYinghai Lu } 338aa914f5eSYinghai Lu 339aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res) 340aa914f5eSYinghai Lu { 341aa914f5eSYinghai Lu if (res->flags & IORESOURCE_IO) 342aa914f5eSYinghai Lu return !!(mask & IORESOURCE_IO); 343aa914f5eSYinghai Lu 344aa914f5eSYinghai Lu /* check pref at first */ 345aa914f5eSYinghai Lu if (res->flags & IORESOURCE_PREFETCH) { 346aa914f5eSYinghai Lu if (mask & IORESOURCE_PREFETCH) 347aa914f5eSYinghai Lu return true; 348aa914f5eSYinghai Lu /* count pref if its parent is non-pref */ 349aa914f5eSYinghai Lu else if ((mask & IORESOURCE_MEM) && 350aa914f5eSYinghai Lu !(res->parent->flags & IORESOURCE_PREFETCH)) 351aa914f5eSYinghai Lu return true; 352aa914f5eSYinghai Lu else 353aa914f5eSYinghai Lu return false; 354aa914f5eSYinghai Lu } 355aa914f5eSYinghai Lu 356aa914f5eSYinghai Lu if (res->flags & IORESOURCE_MEM) 357aa914f5eSYinghai Lu return !!(mask & IORESOURCE_MEM); 358aa914f5eSYinghai Lu 359aa914f5eSYinghai Lu return false; /* should not get here */ 360aa914f5eSYinghai Lu } 361aa914f5eSYinghai Lu 362bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head, 363bdc4abecSYinghai Lu struct list_head *realloc_head, 364bdc4abecSYinghai Lu struct list_head *fail_head) 365c8adf9a3SRam Pai { 3663e6e0d80SYinghai Lu /* 3673e6e0d80SYinghai Lu * Should not assign requested resources at first. 3683e6e0d80SYinghai Lu * they could be adjacent, so later reassign can not reallocate 3693e6e0d80SYinghai Lu * them one by one in parent resource window. 370367fa982SMasanari Iida * Try to assign requested + add_size at beginning 3713e6e0d80SYinghai Lu * if could do that, could get out early. 3723e6e0d80SYinghai Lu * if could not do that, we still try to assign requested at first, 3733e6e0d80SYinghai Lu * then try to reassign add_size for some resources. 374aa914f5eSYinghai Lu * 375aa914f5eSYinghai Lu * Separate three resource type checking if we need to release 376aa914f5eSYinghai Lu * assigned resource after requested + add_size try. 377aa914f5eSYinghai Lu * 1. if there is io port assign fail, will release assigned 378aa914f5eSYinghai Lu * io port. 379aa914f5eSYinghai Lu * 2. if there is pref mmio assign fail, release assigned 380aa914f5eSYinghai Lu * pref mmio. 381aa914f5eSYinghai Lu * if assigned pref mmio's parent is non-pref mmio and there 382aa914f5eSYinghai Lu * is non-pref mmio assign fail, will release that assigned 383aa914f5eSYinghai Lu * pref mmio. 384aa914f5eSYinghai Lu * 3. if there is non-pref mmio assign fail or pref mmio 385aa914f5eSYinghai Lu * assigned fail, will release assigned non-pref mmio. 3863e6e0d80SYinghai Lu */ 387bdc4abecSYinghai Lu LIST_HEAD(save_head); 388bdc4abecSYinghai Lu LIST_HEAD(local_fail_head); 389b9b0bba9SYinghai Lu struct pci_dev_resource *save_res; 390d74b9027SWei Yang struct pci_dev_resource *dev_res, *tmp_res, *dev_res2; 391aa914f5eSYinghai Lu unsigned long fail_type; 392d74b9027SWei Yang resource_size_t add_align, align; 3933e6e0d80SYinghai Lu 3943e6e0d80SYinghai Lu /* Check if optional add_size is there */ 395bdc4abecSYinghai Lu if (!realloc_head || list_empty(realloc_head)) 3963e6e0d80SYinghai Lu goto requested_and_reassign; 3973e6e0d80SYinghai Lu 3983e6e0d80SYinghai Lu /* Save original start, end, flags etc at first */ 399bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 400bdc4abecSYinghai Lu if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 401bffc56d4SYinghai Lu free_list(&save_head); 4023e6e0d80SYinghai Lu goto requested_and_reassign; 4033e6e0d80SYinghai Lu } 404bdc4abecSYinghai Lu } 4053e6e0d80SYinghai Lu 4063e6e0d80SYinghai Lu /* Update res in head list with add_size in realloc_head list */ 407d74b9027SWei Yang list_for_each_entry_safe(dev_res, tmp_res, head, list) { 408bdc4abecSYinghai Lu dev_res->res->end += get_res_add_size(realloc_head, 409bdc4abecSYinghai Lu dev_res->res); 4103e6e0d80SYinghai Lu 411d74b9027SWei Yang /* 412d74b9027SWei Yang * There are two kinds of additional resources in the list: 413d74b9027SWei Yang * 1. bridge resource -- IORESOURCE_STARTALIGN 414d74b9027SWei Yang * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN 415d74b9027SWei Yang * Here just fix the additional alignment for bridge 416d74b9027SWei Yang */ 417d74b9027SWei Yang if (!(dev_res->res->flags & IORESOURCE_STARTALIGN)) 418d74b9027SWei Yang continue; 419d74b9027SWei Yang 420d74b9027SWei Yang add_align = get_res_add_align(realloc_head, dev_res->res); 421d74b9027SWei Yang 422d74b9027SWei Yang /* 423d74b9027SWei Yang * The "head" list is sorted by the alignment to make sure 424d74b9027SWei Yang * resources with bigger alignment will be assigned first. 425d74b9027SWei Yang * After we change the alignment of a dev_res in "head" list, 426d74b9027SWei Yang * we need to reorder the list by alignment to make it 427d74b9027SWei Yang * consistent. 428d74b9027SWei Yang */ 429d74b9027SWei Yang if (add_align > dev_res->res->start) { 430552bc94eSYinghai Lu resource_size_t r_size = resource_size(dev_res->res); 431552bc94eSYinghai Lu 432d74b9027SWei Yang dev_res->res->start = add_align; 433552bc94eSYinghai Lu dev_res->res->end = add_align + r_size - 1; 434d74b9027SWei Yang 435d74b9027SWei Yang list_for_each_entry(dev_res2, head, list) { 436d74b9027SWei Yang align = pci_resource_alignment(dev_res2->dev, 437d74b9027SWei Yang dev_res2->res); 438a6b65983SWei Yang if (add_align > align) { 439d74b9027SWei Yang list_move_tail(&dev_res->list, 440d74b9027SWei Yang &dev_res2->list); 441a6b65983SWei Yang break; 442a6b65983SWei Yang } 443d74b9027SWei Yang } 444d74b9027SWei Yang } 445d74b9027SWei Yang 446d74b9027SWei Yang } 447d74b9027SWei Yang 4483e6e0d80SYinghai Lu /* Try updated head list with add_size added */ 4493e6e0d80SYinghai Lu assign_requested_resources_sorted(head, &local_fail_head); 4503e6e0d80SYinghai Lu 4513e6e0d80SYinghai Lu /* all assigned with add_size ? */ 452bdc4abecSYinghai Lu if (list_empty(&local_fail_head)) { 4533e6e0d80SYinghai Lu /* Remove head list from realloc_head list */ 454bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 455bdc4abecSYinghai Lu remove_from_list(realloc_head, dev_res->res); 456bffc56d4SYinghai Lu free_list(&save_head); 457bffc56d4SYinghai Lu free_list(head); 4583e6e0d80SYinghai Lu return; 4593e6e0d80SYinghai Lu } 4603e6e0d80SYinghai Lu 461aa914f5eSYinghai Lu /* check failed type */ 462aa914f5eSYinghai Lu fail_type = pci_fail_res_type_mask(&local_fail_head); 463aa914f5eSYinghai Lu /* remove not need to be released assigned res from head list etc */ 464aa914f5eSYinghai Lu list_for_each_entry_safe(dev_res, tmp_res, head, list) 465aa914f5eSYinghai Lu if (dev_res->res->parent && 466aa914f5eSYinghai Lu !pci_need_to_release(fail_type, dev_res->res)) { 467aa914f5eSYinghai Lu /* remove it from realloc_head list */ 468aa914f5eSYinghai Lu remove_from_list(realloc_head, dev_res->res); 469aa914f5eSYinghai Lu remove_from_list(&save_head, dev_res->res); 470aa914f5eSYinghai Lu list_del(&dev_res->list); 471aa914f5eSYinghai Lu kfree(dev_res); 472aa914f5eSYinghai Lu } 473aa914f5eSYinghai Lu 474bffc56d4SYinghai Lu free_list(&local_fail_head); 4753e6e0d80SYinghai Lu /* Release assigned resource */ 476bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 477bdc4abecSYinghai Lu if (dev_res->res->parent) 478bdc4abecSYinghai Lu release_resource(dev_res->res); 4793e6e0d80SYinghai Lu /* Restore start/end/flags from saved list */ 480b9b0bba9SYinghai Lu list_for_each_entry(save_res, &save_head, list) { 481b9b0bba9SYinghai Lu struct resource *res = save_res->res; 4823e6e0d80SYinghai Lu 483b9b0bba9SYinghai Lu res->start = save_res->start; 484b9b0bba9SYinghai Lu res->end = save_res->end; 485b9b0bba9SYinghai Lu res->flags = save_res->flags; 4863e6e0d80SYinghai Lu } 487bffc56d4SYinghai Lu free_list(&save_head); 4883e6e0d80SYinghai Lu 4893e6e0d80SYinghai Lu requested_and_reassign: 490c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 491c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 492c8adf9a3SRam Pai 4930a2daa1cSRam Pai /* Try to satisfy any additional optional resource 494c8adf9a3SRam Pai requests */ 4959e8bf93aSRam Pai if (realloc_head) 4969e8bf93aSRam Pai reassign_resources_sorted(realloc_head, head); 497bffc56d4SYinghai Lu free_list(head); 498c8adf9a3SRam Pai } 499c8adf9a3SRam Pai 5006841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 501bdc4abecSYinghai Lu struct list_head *add_head, 502bdc4abecSYinghai Lu struct list_head *fail_head) 5036841ec68SYinghai Lu { 504bdc4abecSYinghai Lu LIST_HEAD(head); 5056841ec68SYinghai Lu 5066841ec68SYinghai Lu __dev_sort_resources(dev, &head); 5078424d759SYinghai Lu __assign_resources_sorted(&head, add_head, fail_head); 5086841ec68SYinghai Lu 5096841ec68SYinghai Lu } 5106841ec68SYinghai Lu 5116841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 512bdc4abecSYinghai Lu struct list_head *realloc_head, 513bdc4abecSYinghai Lu struct list_head *fail_head) 5146841ec68SYinghai Lu { 5156841ec68SYinghai Lu struct pci_dev *dev; 516bdc4abecSYinghai Lu LIST_HEAD(head); 5176841ec68SYinghai Lu 5186841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 5196841ec68SYinghai Lu __dev_sort_resources(dev, &head); 5206841ec68SYinghai Lu 5219e8bf93aSRam Pai __assign_resources_sorted(&head, realloc_head, fail_head); 5226841ec68SYinghai Lu } 5236841ec68SYinghai Lu 524b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 527c7dabef8SBjorn Helgaas struct resource *res; 5281da177e4SLinus Torvalds struct pci_bus_region region; 5291da177e4SLinus Torvalds 530b918c62eSYinghai Lu dev_info(&bridge->dev, "CardBus bridge to %pR\n", 531b918c62eSYinghai Lu &bus->busn_res); 5321da177e4SLinus Torvalds 533c7dabef8SBjorn Helgaas res = bus->resource[0]; 534fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 535c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5361da177e4SLinus Torvalds /* 5371da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 5381da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 5391da177e4SLinus Torvalds */ 540c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5411da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 5421da177e4SLinus Torvalds region.start); 5431da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 5441da177e4SLinus Torvalds region.end); 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 547c7dabef8SBjorn Helgaas res = bus->resource[1]; 548fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 549c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 550c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5511da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 5521da177e4SLinus Torvalds region.start); 5531da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 5541da177e4SLinus Torvalds region.end); 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 557c7dabef8SBjorn Helgaas res = bus->resource[2]; 558fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 559c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 560c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5611da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 5621da177e4SLinus Torvalds region.start); 5631da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 5641da177e4SLinus Torvalds region.end); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567c7dabef8SBjorn Helgaas res = bus->resource[3]; 568fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 569c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 570c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5711da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 5721da177e4SLinus Torvalds region.start); 5731da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 5741da177e4SLinus Torvalds region.end); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds } 577b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 5801da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 5811da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 5821da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 5831da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 5861da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 5871da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 5881da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 5891da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 5903f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge) 5911da177e4SLinus Torvalds { 592c7dabef8SBjorn Helgaas struct resource *res; 5931da177e4SLinus Torvalds struct pci_bus_region region; 5942b28ae19SBjorn Helgaas unsigned long io_mask; 5952b28ae19SBjorn Helgaas u8 io_base_lo, io_limit_lo; 5965b764b83SBjorn Helgaas u16 l; 5975b764b83SBjorn Helgaas u32 io_upper16; 5981da177e4SLinus Torvalds 5992b28ae19SBjorn Helgaas io_mask = PCI_IO_RANGE_MASK; 6002b28ae19SBjorn Helgaas if (bridge->io_window_1k) 6012b28ae19SBjorn Helgaas io_mask = PCI_IO_1K_RANGE_MASK; 6022b28ae19SBjorn Helgaas 6031da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 6043f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0]; 605fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 606c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 6075b764b83SBjorn Helgaas pci_read_config_word(bridge, PCI_IO_BASE, &l); 6082b28ae19SBjorn Helgaas io_base_lo = (region.start >> 8) & io_mask; 6092b28ae19SBjorn Helgaas io_limit_lo = (region.end >> 8) & io_mask; 6105b764b83SBjorn Helgaas l = ((u16) io_limit_lo << 8) | io_base_lo; 6111da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 6121da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 613c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 6147cc5997dSYinghai Lu } else { 6151da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 6161da177e4SLinus Torvalds io_upper16 = 0; 6171da177e4SLinus Torvalds l = 0x00f0; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 6201da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 6211da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 6225b764b83SBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, l); 6231da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 6241da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 6257cc5997dSYinghai Lu } 6261da177e4SLinus Torvalds 6273f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge) 6287cc5997dSYinghai Lu { 6297cc5997dSYinghai Lu struct resource *res; 6307cc5997dSYinghai Lu struct pci_bus_region region; 6317cc5997dSYinghai Lu u32 l; 6327cc5997dSYinghai Lu 6337cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 6343f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1]; 635fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 636c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 6371da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6381da177e4SLinus Torvalds l |= region.end & 0xfff00000; 639c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 6407cc5997dSYinghai Lu } else { 6411da177e4SLinus Torvalds l = 0x0000fff0; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 6447cc5997dSYinghai Lu } 6457cc5997dSYinghai Lu 6463f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge) 6477cc5997dSYinghai Lu { 6487cc5997dSYinghai Lu struct resource *res; 6497cc5997dSYinghai Lu struct pci_bus_region region; 6507cc5997dSYinghai Lu u32 l, bu, lu; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 6531da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 6541da177e4SLinus Torvalds disables PREF range, which is ok. */ 6551da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* Set up PREF base/limit. */ 658c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 6593f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2]; 660fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 661c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 6621da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6631da177e4SLinus Torvalds l |= region.end & 0xfff00000; 664c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 66513d36c24SAndrew Morton bu = upper_32_bits(region.start); 66613d36c24SAndrew Morton lu = upper_32_bits(region.end); 6671f82de10SYinghai Lu } 668c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 6697cc5997dSYinghai Lu } else { 6701da177e4SLinus Torvalds l = 0x0000fff0; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 6731da177e4SLinus Torvalds 674c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 675c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 676c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 6777cc5997dSYinghai Lu } 6787cc5997dSYinghai Lu 6797cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 6807cc5997dSYinghai Lu { 6817cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 6827cc5997dSYinghai Lu 683b918c62eSYinghai Lu dev_info(&bridge->dev, "PCI bridge to %pR\n", 684b918c62eSYinghai Lu &bus->busn_res); 6857cc5997dSYinghai Lu 6867cc5997dSYinghai Lu if (type & IORESOURCE_IO) 6873f2f4dc4SYinghai Lu pci_setup_bridge_io(bridge); 6887cc5997dSYinghai Lu 6897cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 6903f2f4dc4SYinghai Lu pci_setup_bridge_mmio(bridge); 6917cc5997dSYinghai Lu 6927cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 6933f2f4dc4SYinghai Lu pci_setup_bridge_mmio_pref(bridge); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 698e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 6997cc5997dSYinghai Lu { 7007cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 7017cc5997dSYinghai Lu IORESOURCE_PREFETCH; 7027cc5997dSYinghai Lu 7037cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 7047cc5997dSYinghai Lu } 7057cc5997dSYinghai Lu 7068505e729SYinghai Lu 7078505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i) 7088505e729SYinghai Lu { 7098505e729SYinghai Lu if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END) 7108505e729SYinghai Lu return 0; 7118505e729SYinghai Lu 7128505e729SYinghai Lu if (pci_claim_resource(bridge, i) == 0) 7138505e729SYinghai Lu return 0; /* claimed the window */ 7148505e729SYinghai Lu 7158505e729SYinghai Lu if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI) 7168505e729SYinghai Lu return 0; 7178505e729SYinghai Lu 7188505e729SYinghai Lu if (!pci_bus_clip_resource(bridge, i)) 7198505e729SYinghai Lu return -EINVAL; /* clipping didn't change anything */ 7208505e729SYinghai Lu 7218505e729SYinghai Lu switch (i - PCI_BRIDGE_RESOURCES) { 7228505e729SYinghai Lu case 0: 7238505e729SYinghai Lu pci_setup_bridge_io(bridge); 7248505e729SYinghai Lu break; 7258505e729SYinghai Lu case 1: 7268505e729SYinghai Lu pci_setup_bridge_mmio(bridge); 7278505e729SYinghai Lu break; 7288505e729SYinghai Lu case 2: 7298505e729SYinghai Lu pci_setup_bridge_mmio_pref(bridge); 7308505e729SYinghai Lu break; 7318505e729SYinghai Lu default: 7328505e729SYinghai Lu return -EINVAL; 7338505e729SYinghai Lu } 7348505e729SYinghai Lu 7358505e729SYinghai Lu if (pci_claim_resource(bridge, i) == 0) 7368505e729SYinghai Lu return 0; /* claimed a smaller window */ 7378505e729SYinghai Lu 7388505e729SYinghai Lu return -EINVAL; 7398505e729SYinghai Lu } 7408505e729SYinghai Lu 7411da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 7421da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 7431da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 74496bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 7451da177e4SLinus Torvalds { 7461da177e4SLinus Torvalds u16 io; 7471da177e4SLinus Torvalds u32 pmem; 7481da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 7491da177e4SLinus Torvalds struct resource *b_res; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 7521da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 7551da177e4SLinus Torvalds if (!io) { 756d2f54d9bSBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0); 7571da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 7581da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds if (io) 7611da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 762d2f54d9bSBjorn Helgaas 7631da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 7641da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 7651da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 7661da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 7671da177e4SLinus Torvalds return; 768d2f54d9bSBjorn Helgaas 7691da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 7701da177e4SLinus Torvalds if (!pmem) { 7711da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 772d2f54d9bSBjorn Helgaas 0xffe0fff0); 7731da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 7741da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 7751da177e4SLinus Torvalds } 7761f82de10SYinghai Lu if (pmem) { 7771da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 77899586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 77999586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 7801f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 78199586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 78299586105SYinghai Lu } 7831f82de10SYinghai Lu } 7841f82de10SYinghai Lu 7851f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 7861f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 7871f82de10SYinghai Lu u32 mem_base_hi, tmp; 7881f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7891f82de10SYinghai Lu &mem_base_hi); 7901f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7911f82de10SYinghai Lu 0xffffffff); 7921f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 7931f82de10SYinghai Lu if (!tmp) 7941f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 7951f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7961f82de10SYinghai Lu mem_base_hi); 7971f82de10SYinghai Lu } 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 8011da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 8021da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 8031da177e4SLinus Torvalds have non-NULL parent resource). */ 8045b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus, 8055b285415SYinghai Lu unsigned long type_mask, unsigned long type) 8061da177e4SLinus Torvalds { 8071da177e4SLinus Torvalds int i; 8081da177e4SLinus Torvalds struct resource *r; 8091da177e4SLinus Torvalds 81089a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 811299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 812299de034SIvan Kokshaysky continue; 81355a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 8141da177e4SLinus Torvalds return r; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds return NULL; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 81913583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 82013583b16SRam Pai resource_size_t min_size, 82113583b16SRam Pai resource_size_t size1, 82213583b16SRam Pai resource_size_t old_size, 82313583b16SRam Pai resource_size_t align) 82413583b16SRam Pai { 82513583b16SRam Pai if (size < min_size) 82613583b16SRam Pai size = min_size; 82713583b16SRam Pai if (old_size == 1) 82813583b16SRam Pai old_size = 0; 82913583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 83013583b16SRam Pai flag in the struct pci_bus. */ 83113583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 83213583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 83313583b16SRam Pai #endif 83413583b16SRam Pai size = ALIGN(size + size1, align); 83513583b16SRam Pai if (size < old_size) 83613583b16SRam Pai size = old_size; 83713583b16SRam Pai return size; 83813583b16SRam Pai } 83913583b16SRam Pai 84013583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 84113583b16SRam Pai resource_size_t min_size, 84213583b16SRam Pai resource_size_t size1, 84313583b16SRam Pai resource_size_t old_size, 84413583b16SRam Pai resource_size_t align) 84513583b16SRam Pai { 84613583b16SRam Pai if (size < min_size) 84713583b16SRam Pai size = min_size; 84813583b16SRam Pai if (old_size == 1) 84913583b16SRam Pai old_size = 0; 85013583b16SRam Pai if (size < old_size) 85113583b16SRam Pai size = old_size; 85213583b16SRam Pai size = ALIGN(size + size1, align); 85313583b16SRam Pai return size; 85413583b16SRam Pai } 85513583b16SRam Pai 856ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, 857ac5ad93eSGavin Shan unsigned long type) 858ac5ad93eSGavin Shan { 859ac5ad93eSGavin Shan return 1; 860ac5ad93eSGavin Shan } 861ac5ad93eSGavin Shan 862ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */ 863ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */ 864ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */ 865ac5ad93eSGavin Shan 866ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus, 867ac5ad93eSGavin Shan unsigned long type) 868ac5ad93eSGavin Shan { 869ac5ad93eSGavin Shan resource_size_t align = 1, arch_align; 870ac5ad93eSGavin Shan 871ac5ad93eSGavin Shan if (type & IORESOURCE_MEM) 872ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_MEM_ALIGN; 873ac5ad93eSGavin Shan else if (type & IORESOURCE_IO) { 874ac5ad93eSGavin Shan /* 875ac5ad93eSGavin Shan * Per spec, I/O windows are 4K-aligned, but some 876ac5ad93eSGavin Shan * bridges have an extension to support 1K alignment. 877ac5ad93eSGavin Shan */ 878ac5ad93eSGavin Shan if (bus->self->io_window_1k) 879ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN_1K; 880ac5ad93eSGavin Shan else 881ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN; 882ac5ad93eSGavin Shan } 883ac5ad93eSGavin Shan 884ac5ad93eSGavin Shan arch_align = pcibios_window_alignment(bus, type); 885ac5ad93eSGavin Shan return max(align, arch_align); 886ac5ad93eSGavin Shan } 887ac5ad93eSGavin Shan 888c8adf9a3SRam Pai /** 889c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 890c8adf9a3SRam Pai * 891c8adf9a3SRam Pai * @bus : the bus 892c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 893c8adf9a3SRam Pai * @add_size : additional optional io window 8949e8bf93aSRam Pai * @realloc_head : track the additional io window on this list 895c8adf9a3SRam Pai * 896c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 897fd591341SYinghai Lu * since these windows have 1K or 4K granularity and the IO ranges 898c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 899c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 900c8adf9a3SRam Pai */ 901c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 902bdc4abecSYinghai Lu resource_size_t add_size, struct list_head *realloc_head) 9031da177e4SLinus Torvalds { 9041da177e4SLinus Torvalds struct pci_dev *dev; 9055b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO, 9065b285415SYinghai Lu IORESOURCE_IO); 90711251a86SWei Yang resource_size_t size = 0, size0 = 0, size1 = 0; 908be768912SYinghai Lu resource_size_t children_add_size = 0; 9092d1d6678SBjorn Helgaas resource_size_t min_align, align; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds if (!b_res) 9121da177e4SLinus Torvalds return; 9131da177e4SLinus Torvalds 9142d1d6678SBjorn Helgaas min_align = window_alignment(bus, IORESOURCE_IO); 9151da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 9161da177e4SLinus Torvalds int i; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 9191da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 9201da177e4SLinus Torvalds unsigned long r_size; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 9231da177e4SLinus Torvalds continue; 924022edd86SZhao, Yu r_size = resource_size(r); 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds if (r_size < 0x400) 9271da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 9281da177e4SLinus Torvalds size += r_size; 9291da177e4SLinus Torvalds else 9301da177e4SLinus Torvalds size1 += r_size; 931be768912SYinghai Lu 932fd591341SYinghai Lu align = pci_resource_alignment(dev, r); 933fd591341SYinghai Lu if (align > min_align) 934fd591341SYinghai Lu min_align = align; 935fd591341SYinghai Lu 9369e8bf93aSRam Pai if (realloc_head) 9379e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds } 940fd591341SYinghai Lu 941c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 942fd591341SYinghai Lu resource_size(b_res), min_align); 943be768912SYinghai Lu if (children_add_size > add_size) 944be768912SYinghai Lu add_size = children_add_size; 9459e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 946a4ac9feaSYinghai Lu calculate_iosize(size, min_size, add_size + size1, 947fd591341SYinghai Lu resource_size(b_res), min_align); 948c8adf9a3SRam Pai if (!size0 && !size1) { 949865df576SBjorn Helgaas if (b_res->start || b_res->end) 950227f0647SRyan Desfosses dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n", 951227f0647SRyan Desfosses b_res, &bus->busn_res); 9521da177e4SLinus Torvalds b_res->flags = 0; 9531da177e4SLinus Torvalds return; 9541da177e4SLinus Torvalds } 955fd591341SYinghai Lu 956fd591341SYinghai Lu b_res->start = min_align; 957c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 95888452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 959b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 960fd591341SYinghai Lu add_to_list(realloc_head, bus->self, b_res, size1-size0, 961fd591341SYinghai Lu min_align); 962227f0647SRyan Desfosses dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n", 963227f0647SRyan Desfosses b_res, &bus->busn_res, 96411251a86SWei Yang (unsigned long long)size1-size0); 965b592443dSYinghai Lu } 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds 968c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns, 969c121504eSGavin Shan int max_order) 970c121504eSGavin Shan { 971c121504eSGavin Shan resource_size_t align = 0; 972c121504eSGavin Shan resource_size_t min_align = 0; 973c121504eSGavin Shan int order; 974c121504eSGavin Shan 975c121504eSGavin Shan for (order = 0; order <= max_order; order++) { 976c121504eSGavin Shan resource_size_t align1 = 1; 977c121504eSGavin Shan 978c121504eSGavin Shan align1 <<= (order + 20); 979c121504eSGavin Shan 980c121504eSGavin Shan if (!align) 981c121504eSGavin Shan min_align = align1; 982c121504eSGavin Shan else if (ALIGN(align + min_align, min_align) < align1) 983c121504eSGavin Shan min_align = align1 >> 1; 984c121504eSGavin Shan align += aligns[order]; 985c121504eSGavin Shan } 986c121504eSGavin Shan 987c121504eSGavin Shan return min_align; 988c121504eSGavin Shan } 989c121504eSGavin Shan 990c8adf9a3SRam Pai /** 991c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 992c8adf9a3SRam Pai * 993c8adf9a3SRam Pai * @bus : the bus 994496f70cfSWei Yang * @mask: mask the resource flag, then compare it with type 995496f70cfSWei Yang * @type: the type of free resource from bridge 9965b285415SYinghai Lu * @type2: second match type 9975b285415SYinghai Lu * @type3: third match type 998c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 999c8adf9a3SRam Pai * @add_size : additional optional memory window 10009e8bf93aSRam Pai * @realloc_head : track the additional memory window on this list 1001c8adf9a3SRam Pai * 1002c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 1003c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 100430afe8d0SBjorn Helgaas * 100530afe8d0SBjorn Helgaas * Returns -ENOSPC if there's no available bus resource of the desired type. 100630afe8d0SBjorn Helgaas * Otherwise, sets the bus resource start/end to indicate the required 100730afe8d0SBjorn Helgaas * size, adds things to realloc_head (if supplied), and returns 0. 1008c8adf9a3SRam Pai */ 100928760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 10105b285415SYinghai Lu unsigned long type, unsigned long type2, 10115b285415SYinghai Lu unsigned long type3, 10125b285415SYinghai Lu resource_size_t min_size, resource_size_t add_size, 1013bdc4abecSYinghai Lu struct list_head *realloc_head) 10141da177e4SLinus Torvalds { 10151da177e4SLinus Torvalds struct pci_dev *dev; 1016c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 1017096d4221SYinghai Lu resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */ 10181da177e4SLinus Torvalds int order, max_order; 10195b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, 10205b285415SYinghai Lu mask | IORESOURCE_PREFETCH, type); 1021be768912SYinghai Lu resource_size_t children_add_size = 0; 1022d74b9027SWei Yang resource_size_t children_add_align = 0; 1023d74b9027SWei Yang resource_size_t add_align = 0; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (!b_res) 102630afe8d0SBjorn Helgaas return -ENOSPC; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 10291da177e4SLinus Torvalds max_order = 0; 10301da177e4SLinus Torvalds size = 0; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 10331da177e4SLinus Torvalds int i; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 10361da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 1037c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 10381da177e4SLinus Torvalds 1039a2220d80SDavid Daney if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) || 1040a2220d80SDavid Daney ((r->flags & mask) != type && 10415b285415SYinghai Lu (r->flags & mask) != type2 && 10425b285415SYinghai Lu (r->flags & mask) != type3)) 10431da177e4SLinus Torvalds continue; 1044022edd86SZhao, Yu r_size = resource_size(r); 10452aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 10462aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 10479e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 10482aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 1049d74b9027SWei Yang add_align = max(pci_resource_alignment(dev, r), add_align); 10502aceefcbSYinghai Lu r->end = r->start - 1; 1051f7625980SBjorn Helgaas add_to_list(realloc_head, dev, r, r_size, 0/* don't care */); 10522aceefcbSYinghai Lu children_add_size += r_size; 10532aceefcbSYinghai Lu continue; 10542aceefcbSYinghai Lu } 10552aceefcbSYinghai Lu #endif 105614c8530dSAlan /* 105714c8530dSAlan * aligns[0] is for 1MB (since bridge memory 105814c8530dSAlan * windows are always at least 1MB aligned), so 105914c8530dSAlan * keep "order" from being negative for smaller 106014c8530dSAlan * resources. 106114c8530dSAlan */ 10626faf17f6SChris Wright align = pci_resource_alignment(dev, r); 10631da177e4SLinus Torvalds order = __ffs(align) - 20; 106414c8530dSAlan if (order < 0) 106514c8530dSAlan order = 0; 106614c8530dSAlan if (order >= ARRAY_SIZE(aligns)) { 1067227f0647SRyan Desfosses dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n", 1068227f0647SRyan Desfosses i, r, (unsigned long long) align); 10691da177e4SLinus Torvalds r->flags = 0; 10701da177e4SLinus Torvalds continue; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds size += r_size; 10731da177e4SLinus Torvalds /* Exclude ranges with size > align from 10741da177e4SLinus Torvalds calculation of the alignment. */ 10751da177e4SLinus Torvalds if (r_size == align) 10761da177e4SLinus Torvalds aligns[order] += align; 10771da177e4SLinus Torvalds if (order > max_order) 10781da177e4SLinus Torvalds max_order = order; 1079be768912SYinghai Lu 1080d74b9027SWei Yang if (realloc_head) { 10819e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 1082d74b9027SWei Yang children_add_align = get_res_add_align(realloc_head, r); 1083d74b9027SWei Yang add_align = max(add_align, children_add_align); 1084d74b9027SWei Yang } 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds } 10878308c54dSJeremy Fitzhardinge 1088c121504eSGavin Shan min_align = calculate_mem_align(aligns, max_order); 10893ad94b0dSWei Yang min_align = max(min_align, window_alignment(bus, b_res->flags)); 1090b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 1091d74b9027SWei Yang add_align = max(min_align, add_align); 1092be768912SYinghai Lu if (children_add_size > add_size) 1093be768912SYinghai Lu add_size = children_add_size; 10949e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 1095a4ac9feaSYinghai Lu calculate_memsize(size, min_size, add_size, 1096d74b9027SWei Yang resource_size(b_res), add_align); 1097c8adf9a3SRam Pai if (!size0 && !size1) { 1098865df576SBjorn Helgaas if (b_res->start || b_res->end) 1099227f0647SRyan Desfosses dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n", 1100227f0647SRyan Desfosses b_res, &bus->busn_res); 11011da177e4SLinus Torvalds b_res->flags = 0; 110230afe8d0SBjorn Helgaas return 0; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds b_res->start = min_align; 1105c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 11065b285415SYinghai Lu b_res->flags |= IORESOURCE_STARTALIGN; 1107b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 1108d74b9027SWei Yang add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align); 1109d74b9027SWei Yang dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx add_align %llx\n", 1110227f0647SRyan Desfosses b_res, &bus->busn_res, 1111d74b9027SWei Yang (unsigned long long) (size1 - size0), 1112d74b9027SWei Yang (unsigned long long) add_align); 1113b592443dSYinghai Lu } 111430afe8d0SBjorn Helgaas return 0; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11170a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 11180a2daa1cSRam Pai { 11190a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 11200a2daa1cSRam Pai return pci_cardbus_io_size; 11210a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 11220a2daa1cSRam Pai return pci_cardbus_mem_size; 11230a2daa1cSRam Pai return 0; 11240a2daa1cSRam Pai } 11250a2daa1cSRam Pai 11260a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 1127bdc4abecSYinghai Lu struct list_head *realloc_head) 11281da177e4SLinus Torvalds { 11291da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 11301da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 113111848934SYinghai Lu resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 11321da177e4SLinus Torvalds u16 ctrl; 11331da177e4SLinus Torvalds 11343796f1e2SYinghai Lu if (b_res[0].parent) 11353796f1e2SYinghai Lu goto handle_b_res_1; 11361da177e4SLinus Torvalds /* 11371da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 11381da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 11391da177e4SLinus Torvalds */ 114011848934SYinghai Lu b_res[0].start = pci_cardbus_io_size; 114111848934SYinghai Lu b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; 114211848934SYinghai Lu b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 114311848934SYinghai Lu if (realloc_head) { 114411848934SYinghai Lu b_res[0].end -= pci_cardbus_io_size; 114511848934SYinghai Lu add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 114611848934SYinghai Lu pci_cardbus_io_size); 114711848934SYinghai Lu } 11481da177e4SLinus Torvalds 11493796f1e2SYinghai Lu handle_b_res_1: 11503796f1e2SYinghai Lu if (b_res[1].parent) 11513796f1e2SYinghai Lu goto handle_b_res_2; 115211848934SYinghai Lu b_res[1].start = pci_cardbus_io_size; 115311848934SYinghai Lu b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; 115411848934SYinghai Lu b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 115511848934SYinghai Lu if (realloc_head) { 115611848934SYinghai Lu b_res[1].end -= pci_cardbus_io_size; 115711848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 115811848934SYinghai Lu pci_cardbus_io_size); 115911848934SYinghai Lu } 11601da177e4SLinus Torvalds 11613796f1e2SYinghai Lu handle_b_res_2: 1162dcef0d06SYinghai Lu /* MEM1 must not be pref mmio */ 1163dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1164dcef0d06SYinghai Lu if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 1165dcef0d06SYinghai Lu ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 1166dcef0d06SYinghai Lu pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1167dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1168dcef0d06SYinghai Lu } 1169dcef0d06SYinghai Lu 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * Check whether prefetchable memory is supported 11721da177e4SLinus Torvalds * by this bridge. 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 11751da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 11761da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 11771da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 11781da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11813796f1e2SYinghai Lu if (b_res[2].parent) 11823796f1e2SYinghai Lu goto handle_b_res_3; 11831da177e4SLinus Torvalds /* 11841da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 11851da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 11861da177e4SLinus Torvalds * twice the size. 11871da177e4SLinus Torvalds */ 11881da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 118911848934SYinghai Lu b_res[2].start = pci_cardbus_mem_size; 119011848934SYinghai Lu b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; 119111848934SYinghai Lu b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 119211848934SYinghai Lu IORESOURCE_STARTALIGN; 119311848934SYinghai Lu if (realloc_head) { 119411848934SYinghai Lu b_res[2].end -= pci_cardbus_mem_size; 119511848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+2, 119611848934SYinghai Lu pci_cardbus_mem_size, pci_cardbus_mem_size); 11971da177e4SLinus Torvalds } 11980a2daa1cSRam Pai 119911848934SYinghai Lu /* reduce that to half */ 120011848934SYinghai Lu b_res_3_size = pci_cardbus_mem_size; 120111848934SYinghai Lu } 120211848934SYinghai Lu 12033796f1e2SYinghai Lu handle_b_res_3: 12043796f1e2SYinghai Lu if (b_res[3].parent) 12053796f1e2SYinghai Lu goto handle_done; 120611848934SYinghai Lu b_res[3].start = pci_cardbus_mem_size; 120711848934SYinghai Lu b_res[3].end = b_res[3].start + b_res_3_size - 1; 120811848934SYinghai Lu b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 120911848934SYinghai Lu if (realloc_head) { 121011848934SYinghai Lu b_res[3].end -= b_res_3_size; 121111848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+3, b_res_3_size, 121211848934SYinghai Lu pci_cardbus_mem_size); 121311848934SYinghai Lu } 12143796f1e2SYinghai Lu 12153796f1e2SYinghai Lu handle_done: 12163796f1e2SYinghai Lu ; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 121910874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) 12201da177e4SLinus Torvalds { 12211da177e4SLinus Torvalds struct pci_dev *dev; 12225b285415SYinghai Lu unsigned long mask, prefmask, type2 = 0, type3 = 0; 1223c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 12245b285415SYinghai Lu struct resource *b_res; 122530afe8d0SBjorn Helgaas int ret; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 12281da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 12291da177e4SLinus Torvalds if (!b) 12301da177e4SLinus Torvalds continue; 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds switch (dev->class >> 8) { 12331da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 12349e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 12351da177e4SLinus Torvalds break; 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 12381da177e4SLinus Torvalds default: 12399e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 12401da177e4SLinus Torvalds break; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds /* The root bus? */ 12452ba29e27SWei Yang if (pci_is_root_bus(bus)) 12461da177e4SLinus Torvalds return; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds switch (bus->self->class >> 8) { 12491da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 12501da177e4SLinus Torvalds /* don't size cardbuses yet. */ 12511da177e4SLinus Torvalds break; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 12541da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 125528760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 1256c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 1257c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 125828760489SEric W. Biederman } 125967d29b5cSBjorn Helgaas /* Fall through */ 12601da177e4SLinus Torvalds default: 126119aa7ee4SYinghai Lu pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 126219aa7ee4SYinghai Lu additional_io_size, realloc_head); 126367d29b5cSBjorn Helgaas 126467d29b5cSBjorn Helgaas /* 126567d29b5cSBjorn Helgaas * If there's a 64-bit prefetchable MMIO window, compute 126667d29b5cSBjorn Helgaas * the size required to put all 64-bit prefetchable 126767d29b5cSBjorn Helgaas * resources in it. 126867d29b5cSBjorn Helgaas */ 12695b285415SYinghai Lu b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES]; 12701da177e4SLinus Torvalds mask = IORESOURCE_MEM; 12711da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 12725b285415SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 12735b285415SYinghai Lu prefmask |= IORESOURCE_MEM_64; 127430afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12755b285415SYinghai Lu prefmask, prefmask, 127619aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 127730afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 127867d29b5cSBjorn Helgaas 12795b285415SYinghai Lu /* 128067d29b5cSBjorn Helgaas * If successful, all non-prefetchable resources 128167d29b5cSBjorn Helgaas * and any 32-bit prefetchable resources will go in 128267d29b5cSBjorn Helgaas * the non-prefetchable window. 128367d29b5cSBjorn Helgaas */ 128467d29b5cSBjorn Helgaas if (ret == 0) { 12855b285415SYinghai Lu mask = prefmask; 12865b285415SYinghai Lu type2 = prefmask & ~IORESOURCE_MEM_64; 12875b285415SYinghai Lu type3 = prefmask & ~IORESOURCE_PREFETCH; 12885b285415SYinghai Lu } 12895b285415SYinghai Lu } 129067d29b5cSBjorn Helgaas 129167d29b5cSBjorn Helgaas /* 129267d29b5cSBjorn Helgaas * If there is no 64-bit prefetchable window, compute the 129367d29b5cSBjorn Helgaas * size required to put all prefetchable resources in the 129467d29b5cSBjorn Helgaas * 32-bit prefetchable window (if there is one). 129567d29b5cSBjorn Helgaas */ 12965b285415SYinghai Lu if (!type2) { 12975b285415SYinghai Lu prefmask &= ~IORESOURCE_MEM_64; 129830afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12995b285415SYinghai Lu prefmask, prefmask, 13005b285415SYinghai Lu realloc_head ? 0 : additional_mem_size, 130130afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 130267d29b5cSBjorn Helgaas 130367d29b5cSBjorn Helgaas /* 130467d29b5cSBjorn Helgaas * If successful, only non-prefetchable resources 130567d29b5cSBjorn Helgaas * will go in the non-prefetchable window. 130667d29b5cSBjorn Helgaas */ 130767d29b5cSBjorn Helgaas if (ret == 0) 13085b285415SYinghai Lu mask = prefmask; 130928760489SEric W. Biederman else 1310c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 131167d29b5cSBjorn Helgaas 13125b285415SYinghai Lu type2 = type3 = IORESOURCE_MEM; 13135b285415SYinghai Lu } 131467d29b5cSBjorn Helgaas 131567d29b5cSBjorn Helgaas /* 131667d29b5cSBjorn Helgaas * Compute the size required to put everything else in the 131767d29b5cSBjorn Helgaas * non-prefetchable window. This includes: 131867d29b5cSBjorn Helgaas * 131967d29b5cSBjorn Helgaas * - all non-prefetchable resources 132067d29b5cSBjorn Helgaas * - 32-bit prefetchable resources if there's a 64-bit 132167d29b5cSBjorn Helgaas * prefetchable window or no prefetchable window at all 132267d29b5cSBjorn Helgaas * - 64-bit prefetchable resources if there's no 132367d29b5cSBjorn Helgaas * prefetchable window at all 132467d29b5cSBjorn Helgaas * 132567d29b5cSBjorn Helgaas * Note that the strategy in __pci_assign_resource() must 132667d29b5cSBjorn Helgaas * match that used here. Specifically, we cannot put a 132767d29b5cSBjorn Helgaas * 32-bit prefetchable resource in a 64-bit prefetchable 132867d29b5cSBjorn Helgaas * window. 132967d29b5cSBjorn Helgaas */ 13305b285415SYinghai Lu pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, 133119aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 133219aa7ee4SYinghai Lu additional_mem_size, realloc_head); 13331da177e4SLinus Torvalds break; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds } 1336c8adf9a3SRam Pai 133710874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus) 1338c8adf9a3SRam Pai { 1339c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 1340c8adf9a3SRam Pai } 13411da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 13421da177e4SLinus Torvalds 1343d04d0111SDavid Daney static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) 1344d04d0111SDavid Daney { 1345d04d0111SDavid Daney int i; 1346d04d0111SDavid Daney struct resource *parent_r; 1347d04d0111SDavid Daney unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM | 1348d04d0111SDavid Daney IORESOURCE_PREFETCH; 1349d04d0111SDavid Daney 1350d04d0111SDavid Daney pci_bus_for_each_resource(b, parent_r, i) { 1351d04d0111SDavid Daney if (!parent_r) 1352d04d0111SDavid Daney continue; 1353d04d0111SDavid Daney 1354d04d0111SDavid Daney if ((r->flags & mask) == (parent_r->flags & mask) && 1355d04d0111SDavid Daney resource_contains(parent_r, r)) 1356d04d0111SDavid Daney request_resource(parent_r, r); 1357d04d0111SDavid Daney } 1358d04d0111SDavid Daney } 1359d04d0111SDavid Daney 1360d04d0111SDavid Daney /* 1361d04d0111SDavid Daney * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they 1362d04d0111SDavid Daney * are skipped by pbus_assign_resources_sorted(). 1363d04d0111SDavid Daney */ 1364d04d0111SDavid Daney static void pdev_assign_fixed_resources(struct pci_dev *dev) 1365d04d0111SDavid Daney { 1366d04d0111SDavid Daney int i; 1367d04d0111SDavid Daney 1368d04d0111SDavid Daney for (i = 0; i < PCI_NUM_RESOURCES; i++) { 1369d04d0111SDavid Daney struct pci_bus *b; 1370d04d0111SDavid Daney struct resource *r = &dev->resource[i]; 1371d04d0111SDavid Daney 1372d04d0111SDavid Daney if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) || 1373d04d0111SDavid Daney !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) 1374d04d0111SDavid Daney continue; 1375d04d0111SDavid Daney 1376d04d0111SDavid Daney b = dev->bus; 1377d04d0111SDavid Daney while (b && !r->parent) { 1378d04d0111SDavid Daney assign_fixed_resource_on_bus(b, r); 1379d04d0111SDavid Daney b = b->parent; 1380d04d0111SDavid Daney } 1381d04d0111SDavid Daney } 1382d04d0111SDavid Daney } 1383d04d0111SDavid Daney 138410874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus, 1385bdc4abecSYinghai Lu struct list_head *realloc_head, 1386bdc4abecSYinghai Lu struct list_head *fail_head) 13871da177e4SLinus Torvalds { 13881da177e4SLinus Torvalds struct pci_bus *b; 13891da177e4SLinus Torvalds struct pci_dev *dev; 13901da177e4SLinus Torvalds 13919e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 1394d04d0111SDavid Daney pdev_assign_fixed_resources(dev); 1395d04d0111SDavid Daney 13961da177e4SLinus Torvalds b = dev->subordinate; 13971da177e4SLinus Torvalds if (!b) 13981da177e4SLinus Torvalds continue; 13991da177e4SLinus Torvalds 14009e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds switch (dev->class >> 8) { 14031da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 14046841ec68SYinghai Lu if (!pci_is_enabled(dev)) 14051da177e4SLinus Torvalds pci_setup_bridge(b); 14061da177e4SLinus Torvalds break; 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 14091da177e4SLinus Torvalds pci_setup_cardbus(b); 14101da177e4SLinus Torvalds break; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds default: 1413227f0647SRyan Desfosses dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n", 1414227f0647SRyan Desfosses pci_domain_nr(b), b->number); 14151da177e4SLinus Torvalds break; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds } 1419568ddef8SYinghai Lu 142010874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus) 1421568ddef8SYinghai Lu { 1422c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 1423568ddef8SYinghai Lu } 14241da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 14251da177e4SLinus Torvalds 1426*765bf9b7SLorenzo Pieralisi static void pci_claim_device_resources(struct pci_dev *dev) 1427*765bf9b7SLorenzo Pieralisi { 1428*765bf9b7SLorenzo Pieralisi int i; 1429*765bf9b7SLorenzo Pieralisi 1430*765bf9b7SLorenzo Pieralisi for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) { 1431*765bf9b7SLorenzo Pieralisi struct resource *r = &dev->resource[i]; 1432*765bf9b7SLorenzo Pieralisi 1433*765bf9b7SLorenzo Pieralisi if (!r->flags || r->parent) 1434*765bf9b7SLorenzo Pieralisi continue; 1435*765bf9b7SLorenzo Pieralisi 1436*765bf9b7SLorenzo Pieralisi pci_claim_resource(dev, i); 1437*765bf9b7SLorenzo Pieralisi } 1438*765bf9b7SLorenzo Pieralisi } 1439*765bf9b7SLorenzo Pieralisi 1440*765bf9b7SLorenzo Pieralisi static void pci_claim_bridge_resources(struct pci_dev *dev) 1441*765bf9b7SLorenzo Pieralisi { 1442*765bf9b7SLorenzo Pieralisi int i; 1443*765bf9b7SLorenzo Pieralisi 1444*765bf9b7SLorenzo Pieralisi for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) { 1445*765bf9b7SLorenzo Pieralisi struct resource *r = &dev->resource[i]; 1446*765bf9b7SLorenzo Pieralisi 1447*765bf9b7SLorenzo Pieralisi if (!r->flags || r->parent) 1448*765bf9b7SLorenzo Pieralisi continue; 1449*765bf9b7SLorenzo Pieralisi 1450*765bf9b7SLorenzo Pieralisi pci_claim_bridge_resource(dev, i); 1451*765bf9b7SLorenzo Pieralisi } 1452*765bf9b7SLorenzo Pieralisi } 1453*765bf9b7SLorenzo Pieralisi 1454*765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_dev_resources(struct pci_bus *b) 1455*765bf9b7SLorenzo Pieralisi { 1456*765bf9b7SLorenzo Pieralisi struct pci_dev *dev; 1457*765bf9b7SLorenzo Pieralisi struct pci_bus *child; 1458*765bf9b7SLorenzo Pieralisi 1459*765bf9b7SLorenzo Pieralisi list_for_each_entry(dev, &b->devices, bus_list) { 1460*765bf9b7SLorenzo Pieralisi pci_claim_device_resources(dev); 1461*765bf9b7SLorenzo Pieralisi 1462*765bf9b7SLorenzo Pieralisi child = dev->subordinate; 1463*765bf9b7SLorenzo Pieralisi if (child) 1464*765bf9b7SLorenzo Pieralisi pci_bus_allocate_dev_resources(child); 1465*765bf9b7SLorenzo Pieralisi } 1466*765bf9b7SLorenzo Pieralisi } 1467*765bf9b7SLorenzo Pieralisi 1468*765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_resources(struct pci_bus *b) 1469*765bf9b7SLorenzo Pieralisi { 1470*765bf9b7SLorenzo Pieralisi struct pci_bus *child; 1471*765bf9b7SLorenzo Pieralisi 1472*765bf9b7SLorenzo Pieralisi /* 1473*765bf9b7SLorenzo Pieralisi * Carry out a depth-first search on the PCI bus 1474*765bf9b7SLorenzo Pieralisi * tree to allocate bridge apertures. Read the 1475*765bf9b7SLorenzo Pieralisi * programmed bridge bases and recursively claim 1476*765bf9b7SLorenzo Pieralisi * the respective bridge resources. 1477*765bf9b7SLorenzo Pieralisi */ 1478*765bf9b7SLorenzo Pieralisi if (b->self) { 1479*765bf9b7SLorenzo Pieralisi pci_read_bridge_bases(b); 1480*765bf9b7SLorenzo Pieralisi pci_claim_bridge_resources(b->self); 1481*765bf9b7SLorenzo Pieralisi } 1482*765bf9b7SLorenzo Pieralisi 1483*765bf9b7SLorenzo Pieralisi list_for_each_entry(child, &b->children, node) 1484*765bf9b7SLorenzo Pieralisi pci_bus_allocate_resources(child); 1485*765bf9b7SLorenzo Pieralisi } 1486*765bf9b7SLorenzo Pieralisi 1487*765bf9b7SLorenzo Pieralisi void pci_bus_claim_resources(struct pci_bus *b) 1488*765bf9b7SLorenzo Pieralisi { 1489*765bf9b7SLorenzo Pieralisi pci_bus_allocate_resources(b); 1490*765bf9b7SLorenzo Pieralisi pci_bus_allocate_dev_resources(b); 1491*765bf9b7SLorenzo Pieralisi } 1492*765bf9b7SLorenzo Pieralisi EXPORT_SYMBOL(pci_bus_claim_resources); 1493*765bf9b7SLorenzo Pieralisi 149410874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge, 1495bdc4abecSYinghai Lu struct list_head *add_head, 1496bdc4abecSYinghai Lu struct list_head *fail_head) 14976841ec68SYinghai Lu { 14986841ec68SYinghai Lu struct pci_bus *b; 14996841ec68SYinghai Lu 15008424d759SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, 15018424d759SYinghai Lu add_head, fail_head); 15026841ec68SYinghai Lu 15036841ec68SYinghai Lu b = bridge->subordinate; 15046841ec68SYinghai Lu if (!b) 15056841ec68SYinghai Lu return; 15066841ec68SYinghai Lu 15078424d759SYinghai Lu __pci_bus_assign_resources(b, add_head, fail_head); 15086841ec68SYinghai Lu 15096841ec68SYinghai Lu switch (bridge->class >> 8) { 15106841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 15116841ec68SYinghai Lu pci_setup_bridge(b); 15126841ec68SYinghai Lu break; 15136841ec68SYinghai Lu 15146841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 15156841ec68SYinghai Lu pci_setup_cardbus(b); 15166841ec68SYinghai Lu break; 15176841ec68SYinghai Lu 15186841ec68SYinghai Lu default: 1519227f0647SRyan Desfosses dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n", 1520227f0647SRyan Desfosses pci_domain_nr(b), b->number); 15216841ec68SYinghai Lu break; 15226841ec68SYinghai Lu } 15236841ec68SYinghai Lu } 15245009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 15255009b460SYinghai Lu unsigned long type) 15265009b460SYinghai Lu { 15275b285415SYinghai Lu struct pci_dev *dev = bus->self; 15285009b460SYinghai Lu struct resource *r; 15295009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 15305b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 15315b285415SYinghai Lu unsigned old_flags = 0; 15325b285415SYinghai Lu struct resource *b_res; 15335b285415SYinghai Lu int idx = 1; 15345009b460SYinghai Lu 15355b285415SYinghai Lu b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; 15365b285415SYinghai Lu 15375b285415SYinghai Lu /* 15385b285415SYinghai Lu * 1. if there is io port assign fail, will release bridge 15395b285415SYinghai Lu * io port. 15405b285415SYinghai Lu * 2. if there is non pref mmio assign fail, release bridge 15415b285415SYinghai Lu * nonpref mmio. 15425b285415SYinghai Lu * 3. if there is 64bit pref mmio assign fail, and bridge pref 15435b285415SYinghai Lu * is 64bit, release bridge pref mmio. 15445b285415SYinghai Lu * 4. if there is pref mmio assign fail, and bridge pref is 15455b285415SYinghai Lu * 32bit mmio, release bridge pref mmio 15465b285415SYinghai Lu * 5. if there is pref mmio assign fail, and bridge pref is not 15475b285415SYinghai Lu * assigned, release bridge nonpref mmio. 15485b285415SYinghai Lu */ 15495b285415SYinghai Lu if (type & IORESOURCE_IO) 15505b285415SYinghai Lu idx = 0; 15515b285415SYinghai Lu else if (!(type & IORESOURCE_PREFETCH)) 15525b285415SYinghai Lu idx = 1; 15535b285415SYinghai Lu else if ((type & IORESOURCE_MEM_64) && 15545b285415SYinghai Lu (b_res[2].flags & IORESOURCE_MEM_64)) 15555b285415SYinghai Lu idx = 2; 15565b285415SYinghai Lu else if (!(b_res[2].flags & IORESOURCE_MEM_64) && 15575b285415SYinghai Lu (b_res[2].flags & IORESOURCE_PREFETCH)) 15585b285415SYinghai Lu idx = 2; 15595b285415SYinghai Lu else 15605b285415SYinghai Lu idx = 1; 15615b285415SYinghai Lu 15625b285415SYinghai Lu r = &b_res[idx]; 15635b285415SYinghai Lu 15645009b460SYinghai Lu if (!r->parent) 15655b285415SYinghai Lu return; 15665b285415SYinghai Lu 15675009b460SYinghai Lu /* 15685009b460SYinghai Lu * if there are children under that, we should release them 15695009b460SYinghai Lu * all 15705009b460SYinghai Lu */ 15715009b460SYinghai Lu release_child_resources(r); 15725009b460SYinghai Lu if (!release_resource(r)) { 15735b285415SYinghai Lu type = old_flags = r->flags & type_mask; 15745b285415SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n", 15755b285415SYinghai Lu PCI_BRIDGE_RESOURCES + idx, r); 15765009b460SYinghai Lu /* keep the old size */ 15775009b460SYinghai Lu r->end = resource_size(r) - 1; 15785009b460SYinghai Lu r->start = 0; 15795009b460SYinghai Lu r->flags = 0; 15805009b460SYinghai Lu 15815009b460SYinghai Lu /* avoiding touch the one without PREF */ 15825009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 15835009b460SYinghai Lu type = IORESOURCE_PREFETCH; 15845009b460SYinghai Lu __pci_setup_bridge(bus, type); 15855b285415SYinghai Lu /* for next child res under same bridge */ 15865b285415SYinghai Lu r->flags = old_flags; 15875009b460SYinghai Lu } 15885009b460SYinghai Lu } 15895009b460SYinghai Lu 15905009b460SYinghai Lu enum release_type { 15915009b460SYinghai Lu leaf_only, 15925009b460SYinghai Lu whole_subtree, 15935009b460SYinghai Lu }; 15945009b460SYinghai Lu /* 15955009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 15965009b460SYinghai Lu * so we can allocate big new one later 15975009b460SYinghai Lu */ 159810874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus, 15995009b460SYinghai Lu unsigned long type, 16005009b460SYinghai Lu enum release_type rel_type) 16015009b460SYinghai Lu { 16025009b460SYinghai Lu struct pci_dev *dev; 16035009b460SYinghai Lu bool is_leaf_bridge = true; 16045009b460SYinghai Lu 16055009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 16065009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 16075009b460SYinghai Lu if (!b) 16085009b460SYinghai Lu continue; 16095009b460SYinghai Lu 16105009b460SYinghai Lu is_leaf_bridge = false; 16115009b460SYinghai Lu 16125009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 16135009b460SYinghai Lu continue; 16145009b460SYinghai Lu 16155009b460SYinghai Lu if (rel_type == whole_subtree) 16165009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 16175009b460SYinghai Lu whole_subtree); 16185009b460SYinghai Lu } 16195009b460SYinghai Lu 16205009b460SYinghai Lu if (pci_is_root_bus(bus)) 16215009b460SYinghai Lu return; 16225009b460SYinghai Lu 16235009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 16245009b460SYinghai Lu return; 16255009b460SYinghai Lu 16265009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 16275009b460SYinghai Lu pci_bridge_release_resources(bus, type); 16285009b460SYinghai Lu } 16295009b460SYinghai Lu 163076fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 163176fbc263SYinghai Lu { 163289a74eccSBjorn Helgaas struct resource *res; 163376fbc263SYinghai Lu int i; 163476fbc263SYinghai Lu 163589a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 16367c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 163776fbc263SYinghai Lu continue; 163876fbc263SYinghai Lu 1639c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 164076fbc263SYinghai Lu } 164176fbc263SYinghai Lu } 164276fbc263SYinghai Lu 164376fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 164476fbc263SYinghai Lu { 164576fbc263SYinghai Lu struct pci_bus *b; 164676fbc263SYinghai Lu struct pci_dev *dev; 164776fbc263SYinghai Lu 164876fbc263SYinghai Lu 164976fbc263SYinghai Lu pci_bus_dump_res(bus); 165076fbc263SYinghai Lu 165176fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 165276fbc263SYinghai Lu b = dev->subordinate; 165376fbc263SYinghai Lu if (!b) 165476fbc263SYinghai Lu continue; 165576fbc263SYinghai Lu 165676fbc263SYinghai Lu pci_bus_dump_resources(b); 165776fbc263SYinghai Lu } 165876fbc263SYinghai Lu } 165976fbc263SYinghai Lu 1660ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus) 1661da7822e5SYinghai Lu { 1662da7822e5SYinghai Lu int depth = 0; 1663f2a230bdSWei Yang struct pci_bus *child_bus; 1664da7822e5SYinghai Lu 1665f2a230bdSWei Yang list_for_each_entry(child_bus, &bus->children, node) { 1666da7822e5SYinghai Lu int ret; 1667da7822e5SYinghai Lu 1668f2a230bdSWei Yang ret = pci_bus_get_depth(child_bus); 1669da7822e5SYinghai Lu if (ret + 1 > depth) 1670da7822e5SYinghai Lu depth = ret + 1; 1671da7822e5SYinghai Lu } 1672da7822e5SYinghai Lu 1673da7822e5SYinghai Lu return depth; 1674da7822e5SYinghai Lu } 1675da7822e5SYinghai Lu 1676b55438fdSYinghai Lu /* 1677b55438fdSYinghai Lu * -1: undefined, will auto detect later 1678b55438fdSYinghai Lu * 0: disabled by user 1679b55438fdSYinghai Lu * 1: disabled by auto detect 1680b55438fdSYinghai Lu * 2: enabled by user 1681b55438fdSYinghai Lu * 3: enabled by auto detect 1682b55438fdSYinghai Lu */ 1683b55438fdSYinghai Lu enum enable_type { 1684b55438fdSYinghai Lu undefined = -1, 1685b55438fdSYinghai Lu user_disabled, 1686b55438fdSYinghai Lu auto_disabled, 1687b55438fdSYinghai Lu user_enabled, 1688b55438fdSYinghai Lu auto_enabled, 1689b55438fdSYinghai Lu }; 1690b55438fdSYinghai Lu 1691ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined; 1692b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str) 1693b55438fdSYinghai Lu { 1694b55438fdSYinghai Lu if (!strncmp(str, "off", 3)) 1695b55438fdSYinghai Lu pci_realloc_enable = user_disabled; 1696b55438fdSYinghai Lu else if (!strncmp(str, "on", 2)) 1697b55438fdSYinghai Lu pci_realloc_enable = user_enabled; 1698b55438fdSYinghai Lu } 1699ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable) 1700b55438fdSYinghai Lu { 1701967260cdSYinghai Lu return enable >= user_enabled; 1702b55438fdSYinghai Lu } 1703f483d392SRam Pai 1704b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) 1705ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data) 1706223d96fcSYinghai Lu { 1707b07f2ebcSYinghai Lu int i; 1708223d96fcSYinghai Lu bool *unassigned = data; 1709b07f2ebcSYinghai Lu 1710b07f2ebcSYinghai Lu for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) { 1711b07f2ebcSYinghai Lu struct resource *r = &dev->resource[i]; 1712fa216bf4SYinghai Lu struct pci_bus_region region; 1713b07f2ebcSYinghai Lu 1714223d96fcSYinghai Lu /* Not assigned or rejected by kernel? */ 1715fa216bf4SYinghai Lu if (!r->flags) 1716fa216bf4SYinghai Lu continue; 1717b07f2ebcSYinghai Lu 1718fc279850SYinghai Lu pcibios_resource_to_bus(dev->bus, ®ion, r); 1719fa216bf4SYinghai Lu if (!region.start) { 1720223d96fcSYinghai Lu *unassigned = true; 1721223d96fcSYinghai Lu return 1; /* return early from pci_walk_bus() */ 1722b07f2ebcSYinghai Lu } 1723b07f2ebcSYinghai Lu } 1724b07f2ebcSYinghai Lu 1725223d96fcSYinghai Lu return 0; 1726223d96fcSYinghai Lu } 1727223d96fcSYinghai Lu 1728ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1729967260cdSYinghai Lu enum enable_type enable_local) 1730223d96fcSYinghai Lu { 1731223d96fcSYinghai Lu bool unassigned = false; 1732223d96fcSYinghai Lu 1733967260cdSYinghai Lu if (enable_local != undefined) 1734967260cdSYinghai Lu return enable_local; 1735223d96fcSYinghai Lu 1736223d96fcSYinghai Lu pci_walk_bus(bus, iov_resources_unassigned, &unassigned); 1737967260cdSYinghai Lu if (unassigned) 1738967260cdSYinghai Lu return auto_enabled; 1739967260cdSYinghai Lu 1740967260cdSYinghai Lu return enable_local; 1741b07f2ebcSYinghai Lu } 1742223d96fcSYinghai Lu #else 1743ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1744967260cdSYinghai Lu enum enable_type enable_local) 1745967260cdSYinghai Lu { 1746967260cdSYinghai Lu return enable_local; 1747b07f2ebcSYinghai Lu } 1748b07f2ebcSYinghai Lu #endif 1749b07f2ebcSYinghai Lu 1750da7822e5SYinghai Lu /* 1751da7822e5SYinghai Lu * first try will not touch pci bridge res 1752da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1753f7625980SBjorn Helgaas * will stop till to the max depth if can not find good one 1754da7822e5SYinghai Lu */ 175539772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) 17561da177e4SLinus Torvalds { 1757bdc4abecSYinghai Lu LIST_HEAD(realloc_head); /* list of resources that 1758c8adf9a3SRam Pai want additional resources */ 1759bdc4abecSYinghai Lu struct list_head *add_list = NULL; 1760da7822e5SYinghai Lu int tried_times = 0; 1761da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1762bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1763b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 1764da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 17655b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 176619aa7ee4SYinghai Lu int pci_try_num = 1; 176755ed83a6SYinghai Lu enum enable_type enable_local; 1768da7822e5SYinghai Lu 176919aa7ee4SYinghai Lu /* don't realloc if asked to do so */ 177055ed83a6SYinghai Lu enable_local = pci_realloc_detect(bus, pci_realloc_enable); 1771967260cdSYinghai Lu if (pci_realloc_enabled(enable_local)) { 177255ed83a6SYinghai Lu int max_depth = pci_bus_get_depth(bus); 177319aa7ee4SYinghai Lu 1774da7822e5SYinghai Lu pci_try_num = max_depth + 1; 177555ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 177655ed83a6SYinghai Lu "max bus depth: %d pci_try_num: %d\n", 1777da7822e5SYinghai Lu max_depth, pci_try_num); 177819aa7ee4SYinghai Lu } 1779da7822e5SYinghai Lu 1780da7822e5SYinghai Lu again: 178119aa7ee4SYinghai Lu /* 178219aa7ee4SYinghai Lu * last try will use add_list, otherwise will try good to have as 178319aa7ee4SYinghai Lu * must have, so can realloc parent bridge resource 178419aa7ee4SYinghai Lu */ 178519aa7ee4SYinghai Lu if (tried_times + 1 == pci_try_num) 1786bdc4abecSYinghai Lu add_list = &realloc_head; 17871da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 17881da177e4SLinus Torvalds subordinate buses. */ 178919aa7ee4SYinghai Lu __pci_bus_size_bridges(bus, add_list); 1790c8adf9a3SRam Pai 17911da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1792bdc4abecSYinghai Lu __pci_bus_assign_resources(bus, add_list, &fail_head); 179319aa7ee4SYinghai Lu if (add_list) 1794bdc4abecSYinghai Lu BUG_ON(!list_empty(add_list)); 1795da7822e5SYinghai Lu tried_times++; 1796da7822e5SYinghai Lu 1797da7822e5SYinghai Lu /* any device complain? */ 1798bdc4abecSYinghai Lu if (list_empty(&fail_head)) 1799928bea96SYinghai Lu goto dump; 1800f483d392SRam Pai 18010c5be0cbSYinghai Lu if (tried_times >= pci_try_num) { 1802967260cdSYinghai Lu if (enable_local == undefined) 180355ed83a6SYinghai Lu dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n"); 1804967260cdSYinghai Lu else if (enable_local == auto_enabled) 180555ed83a6SYinghai Lu dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); 1806eb572e7cSYinghai Lu 1807bffc56d4SYinghai Lu free_list(&fail_head); 1808928bea96SYinghai Lu goto dump; 1809da7822e5SYinghai Lu } 1810da7822e5SYinghai Lu 181155ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 181255ed83a6SYinghai Lu "No. %d try to assign unassigned res\n", tried_times + 1); 1813da7822e5SYinghai Lu 1814da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1815da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1816da7822e5SYinghai Lu rel_type = whole_subtree; 1817da7822e5SYinghai Lu 1818da7822e5SYinghai Lu /* 1819da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1820da7822e5SYinghai Lu * child device under that bridge 1821da7822e5SYinghai Lu */ 182261e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 182361e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 1824b9b0bba9SYinghai Lu fail_res->flags & type_mask, 1825da7822e5SYinghai Lu rel_type); 182661e83cddSYinghai Lu 1827da7822e5SYinghai Lu /* restore size and flags */ 1828b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1829b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 1830da7822e5SYinghai Lu 1831b9b0bba9SYinghai Lu res->start = fail_res->start; 1832b9b0bba9SYinghai Lu res->end = fail_res->end; 1833b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1834b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 1835da7822e5SYinghai Lu res->flags = 0; 1836da7822e5SYinghai Lu } 1837bffc56d4SYinghai Lu free_list(&fail_head); 1838da7822e5SYinghai Lu 1839da7822e5SYinghai Lu goto again; 1840da7822e5SYinghai Lu 1841928bea96SYinghai Lu dump: 184276fbc263SYinghai Lu /* dump the resource on buses */ 184376fbc263SYinghai Lu pci_bus_dump_resources(bus); 184476fbc263SYinghai Lu } 18456841ec68SYinghai Lu 184655ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void) 184755ed83a6SYinghai Lu { 184855ed83a6SYinghai Lu struct pci_bus *root_bus; 184955ed83a6SYinghai Lu 185055ed83a6SYinghai Lu list_for_each_entry(root_bus, &pci_root_buses, node) 185155ed83a6SYinghai Lu pci_assign_unassigned_root_bus_resources(root_bus); 185255ed83a6SYinghai Lu } 185355ed83a6SYinghai Lu 18546841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 18556841ec68SYinghai Lu { 18566841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 1857bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 18588424d759SYinghai Lu want additional resources */ 185932180e40SYinghai Lu int tried_times = 0; 1860bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1861b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 18626841ec68SYinghai Lu int retval; 186332180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1864d61b0e87SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 18656841ec68SYinghai Lu 186632180e40SYinghai Lu again: 18678424d759SYinghai Lu __pci_bus_size_bridges(parent, &add_list); 1868bdc4abecSYinghai Lu __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 1869bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 187032180e40SYinghai Lu tried_times++; 187132180e40SYinghai Lu 1872bdc4abecSYinghai Lu if (list_empty(&fail_head)) 18733f579c34SYinghai Lu goto enable_all; 187432180e40SYinghai Lu 187532180e40SYinghai Lu if (tried_times >= 2) { 187632180e40SYinghai Lu /* still fail, don't need to try more */ 1877bffc56d4SYinghai Lu free_list(&fail_head); 18783f579c34SYinghai Lu goto enable_all; 187932180e40SYinghai Lu } 188032180e40SYinghai Lu 188132180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 188232180e40SYinghai Lu tried_times + 1); 188332180e40SYinghai Lu 188432180e40SYinghai Lu /* 188532180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 188632180e40SYinghai Lu * child device under that bridge 188732180e40SYinghai Lu */ 188861e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 188961e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 189061e83cddSYinghai Lu fail_res->flags & type_mask, 189132180e40SYinghai Lu whole_subtree); 189261e83cddSYinghai Lu 189332180e40SYinghai Lu /* restore size and flags */ 1894b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1895b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 189632180e40SYinghai Lu 1897b9b0bba9SYinghai Lu res->start = fail_res->start; 1898b9b0bba9SYinghai Lu res->end = fail_res->end; 1899b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1900b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 190132180e40SYinghai Lu res->flags = 0; 190232180e40SYinghai Lu } 1903bffc56d4SYinghai Lu free_list(&fail_head); 190432180e40SYinghai Lu 190532180e40SYinghai Lu goto again; 19063f579c34SYinghai Lu 19073f579c34SYinghai Lu enable_all: 19083f579c34SYinghai Lu retval = pci_reenable_device(bridge); 19099fc9eea0SBjorn Helgaas if (retval) 19109fc9eea0SBjorn Helgaas dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval); 19113f579c34SYinghai Lu pci_set_master(bridge); 19126841ec68SYinghai Lu } 19136841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 19149b03088fSYinghai Lu 191517787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus) 19169b03088fSYinghai Lu { 19179b03088fSYinghai Lu struct pci_dev *dev; 1918bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 19199b03088fSYinghai Lu want additional resources */ 19209b03088fSYinghai Lu 19219b03088fSYinghai Lu down_read(&pci_bus_sem); 19229b03088fSYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 19236788a51fSYijing Wang if (pci_is_bridge(dev) && pci_has_subordinate(dev)) 19249b03088fSYinghai Lu __pci_bus_size_bridges(dev->subordinate, 19259b03088fSYinghai Lu &add_list); 19269b03088fSYinghai Lu up_read(&pci_bus_sem); 19279b03088fSYinghai Lu __pci_bus_assign_resources(bus, &add_list, NULL); 1928bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 192917787940SYinghai Lu } 1930e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources); 1931