11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/pci/setup-bus.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Extruded from code written by 51da177e4SLinus Torvalds * Dave Rusling (david.rusling@reo.mts.dec.com) 61da177e4SLinus Torvalds * David Mosberger (davidm@cs.arizona.edu) 71da177e4SLinus Torvalds * David Miller (davem@redhat.com) 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Support routines for initializing a PCI subsystem. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 141da177e4SLinus Torvalds * PCI-PCI bridges cleanup, sorted resource allocation. 151da177e4SLinus Torvalds * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 161da177e4SLinus Torvalds * Converted to allocation in 3 passes, which gives 171da177e4SLinus Torvalds * tighter packing. Prefetchable range support. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/kernel.h> 221da177e4SLinus Torvalds #include <linux/module.h> 231da177e4SLinus Torvalds #include <linux/pci.h> 241da177e4SLinus Torvalds #include <linux/errno.h> 251da177e4SLinus Torvalds #include <linux/ioport.h> 261da177e4SLinus Torvalds #include <linux/cache.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 2847087700SBjorn Helgaas #include <asm-generic/pci-bridge.h> 296faf17f6SChris Wright #include "pci.h" 301da177e4SLinus Torvalds 31844393f4SBjorn Helgaas unsigned int pci_flags; 3247087700SBjorn Helgaas 33bdc4abecSYinghai Lu struct pci_dev_resource { 34bdc4abecSYinghai Lu struct list_head list; 352934a0deSYinghai Lu struct resource *res; 362934a0deSYinghai Lu struct pci_dev *dev; 37568ddef8SYinghai Lu resource_size_t start; 38568ddef8SYinghai Lu resource_size_t end; 39c8adf9a3SRam Pai resource_size_t add_size; 402bbc6942SRam Pai resource_size_t min_align; 41568ddef8SYinghai Lu unsigned long flags; 42568ddef8SYinghai Lu }; 43568ddef8SYinghai Lu 44bffc56d4SYinghai Lu static void free_list(struct list_head *head) 45bffc56d4SYinghai Lu { 46bffc56d4SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 47bffc56d4SYinghai Lu 48bffc56d4SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 49bffc56d4SYinghai Lu list_del(&dev_res->list); 50bffc56d4SYinghai Lu kfree(dev_res); 51bffc56d4SYinghai Lu } 52bffc56d4SYinghai Lu } 53094732a5SRam Pai 54c8adf9a3SRam Pai /** 55c8adf9a3SRam Pai * add_to_list() - add a new resource tracker to the list 56c8adf9a3SRam Pai * @head: Head of the list 57c8adf9a3SRam Pai * @dev: device corresponding to which the resource 58c8adf9a3SRam Pai * belongs 59c8adf9a3SRam Pai * @res: The resource to be tracked 60c8adf9a3SRam Pai * @add_size: additional size to be optionally added 61c8adf9a3SRam Pai * to the resource 62c8adf9a3SRam Pai */ 63bdc4abecSYinghai Lu static int add_to_list(struct list_head *head, 64c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res, 652bbc6942SRam Pai resource_size_t add_size, resource_size_t min_align) 66568ddef8SYinghai Lu { 67764242a0SYinghai Lu struct pci_dev_resource *tmp; 68568ddef8SYinghai Lu 69bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 70568ddef8SYinghai Lu if (!tmp) { 71c8adf9a3SRam Pai pr_warning("add_to_list: kmalloc() failed!\n"); 72ef62dfefSYinghai Lu return -ENOMEM; 73568ddef8SYinghai Lu } 74568ddef8SYinghai Lu 75568ddef8SYinghai Lu tmp->res = res; 76568ddef8SYinghai Lu tmp->dev = dev; 77568ddef8SYinghai Lu tmp->start = res->start; 78568ddef8SYinghai Lu tmp->end = res->end; 79568ddef8SYinghai Lu tmp->flags = res->flags; 80c8adf9a3SRam Pai tmp->add_size = add_size; 812bbc6942SRam Pai tmp->min_align = min_align; 82bdc4abecSYinghai Lu 83bdc4abecSYinghai Lu list_add(&tmp->list, head); 84ef62dfefSYinghai Lu 85ef62dfefSYinghai Lu return 0; 86568ddef8SYinghai Lu } 87568ddef8SYinghai Lu 88b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head, 893e6e0d80SYinghai Lu struct resource *res) 903e6e0d80SYinghai Lu { 91b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 923e6e0d80SYinghai Lu 93b9b0bba9SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 94b9b0bba9SYinghai Lu if (dev_res->res == res) { 95b9b0bba9SYinghai Lu list_del(&dev_res->list); 96b9b0bba9SYinghai Lu kfree(dev_res); 97bdc4abecSYinghai Lu break; 983e6e0d80SYinghai Lu } 993e6e0d80SYinghai Lu } 1003e6e0d80SYinghai Lu } 1013e6e0d80SYinghai Lu 102b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head, 1031c372353SYinghai Lu struct resource *res) 1041c372353SYinghai Lu { 105b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res; 1061c372353SYinghai Lu 107b9b0bba9SYinghai Lu list_for_each_entry(dev_res, head, list) { 108b9b0bba9SYinghai Lu if (dev_res->res == res) { 109b592443dSYinghai Lu int idx = res - &dev_res->dev->resource[0]; 110b592443dSYinghai Lu 111b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &dev_res->dev->dev, 112b592443dSYinghai Lu "res[%d]=%pR get_res_add_size add_size %llx\n", 113b592443dSYinghai Lu idx, dev_res->res, 114b9b0bba9SYinghai Lu (unsigned long long)dev_res->add_size); 115b592443dSYinghai Lu 116b9b0bba9SYinghai Lu return dev_res->add_size; 117bdc4abecSYinghai Lu } 1183e6e0d80SYinghai Lu } 1191c372353SYinghai Lu 1201c372353SYinghai Lu return 0; 1211c372353SYinghai Lu } 1221c372353SYinghai Lu 12378c3b329SYinghai Lu /* Sort resources by alignment */ 124bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 12578c3b329SYinghai Lu { 12678c3b329SYinghai Lu int i; 12778c3b329SYinghai Lu 12878c3b329SYinghai Lu for (i = 0; i < PCI_NUM_RESOURCES; i++) { 12978c3b329SYinghai Lu struct resource *r; 130bdc4abecSYinghai Lu struct pci_dev_resource *dev_res, *tmp; 13178c3b329SYinghai Lu resource_size_t r_align; 132bdc4abecSYinghai Lu struct list_head *n; 13378c3b329SYinghai Lu 13478c3b329SYinghai Lu r = &dev->resource[i]; 13578c3b329SYinghai Lu 13678c3b329SYinghai Lu if (r->flags & IORESOURCE_PCI_FIXED) 13778c3b329SYinghai Lu continue; 13878c3b329SYinghai Lu 13978c3b329SYinghai Lu if (!(r->flags) || r->parent) 14078c3b329SYinghai Lu continue; 14178c3b329SYinghai Lu 14278c3b329SYinghai Lu r_align = pci_resource_alignment(dev, r); 14378c3b329SYinghai Lu if (!r_align) { 14478c3b329SYinghai Lu dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n", 14578c3b329SYinghai Lu i, r); 14678c3b329SYinghai Lu continue; 14778c3b329SYinghai Lu } 14878c3b329SYinghai Lu 149bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 15078c3b329SYinghai Lu if (!tmp) 15178c3b329SYinghai Lu panic("pdev_sort_resources(): " 15278c3b329SYinghai Lu "kmalloc() failed!\n"); 15378c3b329SYinghai Lu tmp->res = r; 15478c3b329SYinghai Lu tmp->dev = dev; 155bdc4abecSYinghai Lu 156bdc4abecSYinghai Lu /* fallback is smallest one or list is empty*/ 157bdc4abecSYinghai Lu n = head; 158bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 159bdc4abecSYinghai Lu resource_size_t align; 160bdc4abecSYinghai Lu 161bdc4abecSYinghai Lu align = pci_resource_alignment(dev_res->dev, 162bdc4abecSYinghai Lu dev_res->res); 163bdc4abecSYinghai Lu 164bdc4abecSYinghai Lu if (r_align > align) { 165bdc4abecSYinghai Lu n = &dev_res->list; 16678c3b329SYinghai Lu break; 16778c3b329SYinghai Lu } 16878c3b329SYinghai Lu } 169bdc4abecSYinghai Lu /* Insert it just before n*/ 170bdc4abecSYinghai Lu list_add_tail(&tmp->list, n); 17178c3b329SYinghai Lu } 17278c3b329SYinghai Lu } 17378c3b329SYinghai Lu 1746841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev, 175bdc4abecSYinghai Lu struct list_head *head) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds u16 class = dev->class >> 8; 1781da177e4SLinus Torvalds 1799bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 1806841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1816841ec68SYinghai Lu return; 1821da177e4SLinus Torvalds 1839bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 18423186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 1859bded00bSKenji Kaneshige u16 command; 1869bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 1879bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 1886841ec68SYinghai Lu return; 18923186279SSatoru Takeuchi } 19023186279SSatoru Takeuchi 1916841ec68SYinghai Lu pdev_sort_resources(dev, head); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 195fc075e1dSRam Pai { 196fc075e1dSRam Pai res->start = 0; 197fc075e1dSRam Pai res->end = 0; 198fc075e1dSRam Pai res->flags = 0; 199fc075e1dSRam Pai } 200fc075e1dSRam Pai 201c8adf9a3SRam Pai /** 2029e8bf93aSRam Pai * reassign_resources_sorted() - satisfy any additional resource requests 203c8adf9a3SRam Pai * 2049e8bf93aSRam Pai * @realloc_head : head of the list tracking requests requiring additional 205c8adf9a3SRam Pai * resources 206c8adf9a3SRam Pai * @head : head of the list tracking requests with allocated 207c8adf9a3SRam Pai * resources 208c8adf9a3SRam Pai * 2099e8bf93aSRam Pai * Walk through each element of the realloc_head and try to procure 210c8adf9a3SRam Pai * additional resources for the element, provided the element 211c8adf9a3SRam Pai * is in the head list. 212c8adf9a3SRam Pai */ 213bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head, 214bdc4abecSYinghai Lu struct list_head *head) 215c8adf9a3SRam Pai { 216c8adf9a3SRam Pai struct resource *res; 217b9b0bba9SYinghai Lu struct pci_dev_resource *add_res, *tmp; 218bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 219c8adf9a3SRam Pai resource_size_t add_size; 220c8adf9a3SRam Pai int idx; 221c8adf9a3SRam Pai 222b9b0bba9SYinghai Lu list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 223bdc4abecSYinghai Lu bool found_match = false; 224bdc4abecSYinghai Lu 225b9b0bba9SYinghai Lu res = add_res->res; 226c8adf9a3SRam Pai /* skip resource that has been reset */ 227c8adf9a3SRam Pai if (!res->flags) 228c8adf9a3SRam Pai goto out; 229c8adf9a3SRam Pai 230c8adf9a3SRam Pai /* skip this resource if not found in head list */ 231bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 232bdc4abecSYinghai Lu if (dev_res->res == res) { 233bdc4abecSYinghai Lu found_match = true; 234bdc4abecSYinghai Lu break; 235c8adf9a3SRam Pai } 236bdc4abecSYinghai Lu } 237bdc4abecSYinghai Lu if (!found_match)/* just skip */ 238bdc4abecSYinghai Lu continue; 239c8adf9a3SRam Pai 240b9b0bba9SYinghai Lu idx = res - &add_res->dev->resource[0]; 241b9b0bba9SYinghai Lu add_size = add_res->add_size; 2422bbc6942SRam Pai if (!resource_size(res)) { 243b9b0bba9SYinghai Lu res->start = add_res->start; 244c8adf9a3SRam Pai res->end = res->start + add_size - 1; 245b9b0bba9SYinghai Lu if (pci_assign_resource(add_res->dev, idx)) 246c8adf9a3SRam Pai reset_resource(res); 2472bbc6942SRam Pai } else { 248b9b0bba9SYinghai Lu resource_size_t align = add_res->min_align; 249b9b0bba9SYinghai Lu res->flags |= add_res->flags & 250bdc4abecSYinghai Lu (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 251b9b0bba9SYinghai Lu if (pci_reassign_resource(add_res->dev, idx, 252bdc4abecSYinghai Lu add_size, align)) 253b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &add_res->dev->dev, 254b592443dSYinghai Lu "failed to add %llx res[%d]=%pR\n", 255b592443dSYinghai Lu (unsigned long long)add_size, 256b592443dSYinghai Lu idx, res); 257c8adf9a3SRam Pai } 258c8adf9a3SRam Pai out: 259b9b0bba9SYinghai Lu list_del(&add_res->list); 260b9b0bba9SYinghai Lu kfree(add_res); 261c8adf9a3SRam Pai } 262c8adf9a3SRam Pai } 263c8adf9a3SRam Pai 264c8adf9a3SRam Pai /** 265c8adf9a3SRam Pai * assign_requested_resources_sorted() - satisfy resource requests 266c8adf9a3SRam Pai * 267c8adf9a3SRam Pai * @head : head of the list tracking requests for resources 2688356aad4SWanpeng Li * @fail_head : head of the list tracking requests that could 269c8adf9a3SRam Pai * not be allocated 270c8adf9a3SRam Pai * 271c8adf9a3SRam Pai * Satisfy resource requests of each element in the list. Add 272c8adf9a3SRam Pai * requests that could not satisfied to the failed_list. 273c8adf9a3SRam Pai */ 274bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head, 275bdc4abecSYinghai Lu struct list_head *fail_head) 2766841ec68SYinghai Lu { 2776841ec68SYinghai Lu struct resource *res; 278bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 2796841ec68SYinghai Lu int idx; 2806841ec68SYinghai Lu 281bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 282bdc4abecSYinghai Lu res = dev_res->res; 283bdc4abecSYinghai Lu idx = res - &dev_res->dev->resource[0]; 284bdc4abecSYinghai Lu if (resource_size(res) && 285bdc4abecSYinghai Lu pci_assign_resource(dev_res->dev, idx)) { 286a3cb999dSYinghai Lu if (fail_head) { 2879a928660SYinghai Lu /* 2889a928660SYinghai Lu * if the failed res is for ROM BAR, and it will 2899a928660SYinghai Lu * be enabled later, don't add it to the list 2909a928660SYinghai Lu */ 2919a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 2929a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 29367cc7e26SYinghai Lu add_to_list(fail_head, 29467cc7e26SYinghai Lu dev_res->dev, res, 295f7625980SBjorn Helgaas 0 /* don't care */, 296f7625980SBjorn Helgaas 0 /* don't care */); 2979a928660SYinghai Lu } 298fc075e1dSRam Pai reset_resource(res); 299542df5deSRajesh Shah } 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 303aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head) 304aa914f5eSYinghai Lu { 305aa914f5eSYinghai Lu struct pci_dev_resource *fail_res; 306aa914f5eSYinghai Lu unsigned long mask = 0; 307aa914f5eSYinghai Lu 308aa914f5eSYinghai Lu /* check failed type */ 309aa914f5eSYinghai Lu list_for_each_entry(fail_res, fail_head, list) 310aa914f5eSYinghai Lu mask |= fail_res->flags; 311aa914f5eSYinghai Lu 312aa914f5eSYinghai Lu /* 313aa914f5eSYinghai Lu * one pref failed resource will set IORESOURCE_MEM, 314aa914f5eSYinghai Lu * as we can allocate pref in non-pref range. 315aa914f5eSYinghai Lu * Will release all assigned non-pref sibling resources 316aa914f5eSYinghai Lu * according to that bit. 317aa914f5eSYinghai Lu */ 318aa914f5eSYinghai Lu return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH); 319aa914f5eSYinghai Lu } 320aa914f5eSYinghai Lu 321aa914f5eSYinghai Lu static bool pci_need_to_release(unsigned long mask, struct resource *res) 322aa914f5eSYinghai Lu { 323aa914f5eSYinghai Lu if (res->flags & IORESOURCE_IO) 324aa914f5eSYinghai Lu return !!(mask & IORESOURCE_IO); 325aa914f5eSYinghai Lu 326aa914f5eSYinghai Lu /* check pref at first */ 327aa914f5eSYinghai Lu if (res->flags & IORESOURCE_PREFETCH) { 328aa914f5eSYinghai Lu if (mask & IORESOURCE_PREFETCH) 329aa914f5eSYinghai Lu return true; 330aa914f5eSYinghai Lu /* count pref if its parent is non-pref */ 331aa914f5eSYinghai Lu else if ((mask & IORESOURCE_MEM) && 332aa914f5eSYinghai Lu !(res->parent->flags & IORESOURCE_PREFETCH)) 333aa914f5eSYinghai Lu return true; 334aa914f5eSYinghai Lu else 335aa914f5eSYinghai Lu return false; 336aa914f5eSYinghai Lu } 337aa914f5eSYinghai Lu 338aa914f5eSYinghai Lu if (res->flags & IORESOURCE_MEM) 339aa914f5eSYinghai Lu return !!(mask & IORESOURCE_MEM); 340aa914f5eSYinghai Lu 341aa914f5eSYinghai Lu return false; /* should not get here */ 342aa914f5eSYinghai Lu } 343aa914f5eSYinghai Lu 344bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head, 345bdc4abecSYinghai Lu struct list_head *realloc_head, 346bdc4abecSYinghai Lu struct list_head *fail_head) 347c8adf9a3SRam Pai { 3483e6e0d80SYinghai Lu /* 3493e6e0d80SYinghai Lu * Should not assign requested resources at first. 3503e6e0d80SYinghai Lu * they could be adjacent, so later reassign can not reallocate 3513e6e0d80SYinghai Lu * them one by one in parent resource window. 352367fa982SMasanari Iida * Try to assign requested + add_size at beginning 3533e6e0d80SYinghai Lu * if could do that, could get out early. 3543e6e0d80SYinghai Lu * if could not do that, we still try to assign requested at first, 3553e6e0d80SYinghai Lu * then try to reassign add_size for some resources. 356aa914f5eSYinghai Lu * 357aa914f5eSYinghai Lu * Separate three resource type checking if we need to release 358aa914f5eSYinghai Lu * assigned resource after requested + add_size try. 359aa914f5eSYinghai Lu * 1. if there is io port assign fail, will release assigned 360aa914f5eSYinghai Lu * io port. 361aa914f5eSYinghai Lu * 2. if there is pref mmio assign fail, release assigned 362aa914f5eSYinghai Lu * pref mmio. 363aa914f5eSYinghai Lu * if assigned pref mmio's parent is non-pref mmio and there 364aa914f5eSYinghai Lu * is non-pref mmio assign fail, will release that assigned 365aa914f5eSYinghai Lu * pref mmio. 366aa914f5eSYinghai Lu * 3. if there is non-pref mmio assign fail or pref mmio 367aa914f5eSYinghai Lu * assigned fail, will release assigned non-pref mmio. 3683e6e0d80SYinghai Lu */ 369bdc4abecSYinghai Lu LIST_HEAD(save_head); 370bdc4abecSYinghai Lu LIST_HEAD(local_fail_head); 371b9b0bba9SYinghai Lu struct pci_dev_resource *save_res; 372aa914f5eSYinghai Lu struct pci_dev_resource *dev_res, *tmp_res; 373aa914f5eSYinghai Lu unsigned long fail_type; 3743e6e0d80SYinghai Lu 3753e6e0d80SYinghai Lu /* Check if optional add_size is there */ 376bdc4abecSYinghai Lu if (!realloc_head || list_empty(realloc_head)) 3773e6e0d80SYinghai Lu goto requested_and_reassign; 3783e6e0d80SYinghai Lu 3793e6e0d80SYinghai Lu /* Save original start, end, flags etc at first */ 380bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 381bdc4abecSYinghai Lu if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 382bffc56d4SYinghai Lu free_list(&save_head); 3833e6e0d80SYinghai Lu goto requested_and_reassign; 3843e6e0d80SYinghai Lu } 385bdc4abecSYinghai Lu } 3863e6e0d80SYinghai Lu 3873e6e0d80SYinghai Lu /* Update res in head list with add_size in realloc_head list */ 388bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 389bdc4abecSYinghai Lu dev_res->res->end += get_res_add_size(realloc_head, 390bdc4abecSYinghai Lu dev_res->res); 3913e6e0d80SYinghai Lu 3923e6e0d80SYinghai Lu /* Try updated head list with add_size added */ 3933e6e0d80SYinghai Lu assign_requested_resources_sorted(head, &local_fail_head); 3943e6e0d80SYinghai Lu 3953e6e0d80SYinghai Lu /* all assigned with add_size ? */ 396bdc4abecSYinghai Lu if (list_empty(&local_fail_head)) { 3973e6e0d80SYinghai Lu /* Remove head list from realloc_head list */ 398bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 399bdc4abecSYinghai Lu remove_from_list(realloc_head, dev_res->res); 400bffc56d4SYinghai Lu free_list(&save_head); 401bffc56d4SYinghai Lu free_list(head); 4023e6e0d80SYinghai Lu return; 4033e6e0d80SYinghai Lu } 4043e6e0d80SYinghai Lu 405aa914f5eSYinghai Lu /* check failed type */ 406aa914f5eSYinghai Lu fail_type = pci_fail_res_type_mask(&local_fail_head); 407aa914f5eSYinghai Lu /* remove not need to be released assigned res from head list etc */ 408aa914f5eSYinghai Lu list_for_each_entry_safe(dev_res, tmp_res, head, list) 409aa914f5eSYinghai Lu if (dev_res->res->parent && 410aa914f5eSYinghai Lu !pci_need_to_release(fail_type, dev_res->res)) { 411aa914f5eSYinghai Lu /* remove it from realloc_head list */ 412aa914f5eSYinghai Lu remove_from_list(realloc_head, dev_res->res); 413aa914f5eSYinghai Lu remove_from_list(&save_head, dev_res->res); 414aa914f5eSYinghai Lu list_del(&dev_res->list); 415aa914f5eSYinghai Lu kfree(dev_res); 416aa914f5eSYinghai Lu } 417aa914f5eSYinghai Lu 418bffc56d4SYinghai Lu free_list(&local_fail_head); 4193e6e0d80SYinghai Lu /* Release assigned resource */ 420bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 421bdc4abecSYinghai Lu if (dev_res->res->parent) 422bdc4abecSYinghai Lu release_resource(dev_res->res); 4233e6e0d80SYinghai Lu /* Restore start/end/flags from saved list */ 424b9b0bba9SYinghai Lu list_for_each_entry(save_res, &save_head, list) { 425b9b0bba9SYinghai Lu struct resource *res = save_res->res; 4263e6e0d80SYinghai Lu 427b9b0bba9SYinghai Lu res->start = save_res->start; 428b9b0bba9SYinghai Lu res->end = save_res->end; 429b9b0bba9SYinghai Lu res->flags = save_res->flags; 4303e6e0d80SYinghai Lu } 431bffc56d4SYinghai Lu free_list(&save_head); 4323e6e0d80SYinghai Lu 4333e6e0d80SYinghai Lu requested_and_reassign: 434c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 435c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 436c8adf9a3SRam Pai 4370a2daa1cSRam Pai /* Try to satisfy any additional optional resource 438c8adf9a3SRam Pai requests */ 4399e8bf93aSRam Pai if (realloc_head) 4409e8bf93aSRam Pai reassign_resources_sorted(realloc_head, head); 441bffc56d4SYinghai Lu free_list(head); 442c8adf9a3SRam Pai } 443c8adf9a3SRam Pai 4446841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 445bdc4abecSYinghai Lu struct list_head *add_head, 446bdc4abecSYinghai Lu struct list_head *fail_head) 4476841ec68SYinghai Lu { 448bdc4abecSYinghai Lu LIST_HEAD(head); 4496841ec68SYinghai Lu 4506841ec68SYinghai Lu __dev_sort_resources(dev, &head); 4518424d759SYinghai Lu __assign_resources_sorted(&head, add_head, fail_head); 4526841ec68SYinghai Lu 4536841ec68SYinghai Lu } 4546841ec68SYinghai Lu 4556841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 456bdc4abecSYinghai Lu struct list_head *realloc_head, 457bdc4abecSYinghai Lu struct list_head *fail_head) 4586841ec68SYinghai Lu { 4596841ec68SYinghai Lu struct pci_dev *dev; 460bdc4abecSYinghai Lu LIST_HEAD(head); 4616841ec68SYinghai Lu 4626841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 4636841ec68SYinghai Lu __dev_sort_resources(dev, &head); 4646841ec68SYinghai Lu 4659e8bf93aSRam Pai __assign_resources_sorted(&head, realloc_head, fail_head); 4666841ec68SYinghai Lu } 4676841ec68SYinghai Lu 468b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 471c7dabef8SBjorn Helgaas struct resource *res; 4721da177e4SLinus Torvalds struct pci_bus_region region; 4731da177e4SLinus Torvalds 474b918c62eSYinghai Lu dev_info(&bridge->dev, "CardBus bridge to %pR\n", 475b918c62eSYinghai Lu &bus->busn_res); 4761da177e4SLinus Torvalds 477c7dabef8SBjorn Helgaas res = bus->resource[0]; 478fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 479c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 4801da177e4SLinus Torvalds /* 4811da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 4821da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 4831da177e4SLinus Torvalds */ 484c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4851da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 4861da177e4SLinus Torvalds region.start); 4871da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 4881da177e4SLinus Torvalds region.end); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 491c7dabef8SBjorn Helgaas res = bus->resource[1]; 492fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 493c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 494c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4951da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 4961da177e4SLinus Torvalds region.start); 4971da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 4981da177e4SLinus Torvalds region.end); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 501c7dabef8SBjorn Helgaas res = bus->resource[2]; 502fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 503c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 504c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5051da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 5061da177e4SLinus Torvalds region.start); 5071da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 5081da177e4SLinus Torvalds region.end); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 511c7dabef8SBjorn Helgaas res = bus->resource[3]; 512fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 513c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 514c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5151da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 5161da177e4SLinus Torvalds region.start); 5171da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 5181da177e4SLinus Torvalds region.end); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds } 521b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 5241da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 5251da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 5261da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 5271da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 5301da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 5311da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 5321da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 5331da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 5347cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 537c7dabef8SBjorn Helgaas struct resource *res; 5381da177e4SLinus Torvalds struct pci_bus_region region; 5392b28ae19SBjorn Helgaas unsigned long io_mask; 5402b28ae19SBjorn Helgaas u8 io_base_lo, io_limit_lo; 5415b764b83SBjorn Helgaas u16 l; 5425b764b83SBjorn Helgaas u32 io_upper16; 5431da177e4SLinus Torvalds 5442b28ae19SBjorn Helgaas io_mask = PCI_IO_RANGE_MASK; 5452b28ae19SBjorn Helgaas if (bridge->io_window_1k) 5462b28ae19SBjorn Helgaas io_mask = PCI_IO_1K_RANGE_MASK; 5472b28ae19SBjorn Helgaas 5481da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 549c7dabef8SBjorn Helgaas res = bus->resource[0]; 550fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 551c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5525b764b83SBjorn Helgaas pci_read_config_word(bridge, PCI_IO_BASE, &l); 5532b28ae19SBjorn Helgaas io_base_lo = (region.start >> 8) & io_mask; 5542b28ae19SBjorn Helgaas io_limit_lo = (region.end >> 8) & io_mask; 5555b764b83SBjorn Helgaas l = ((u16) io_limit_lo << 8) | io_base_lo; 5561da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 5571da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 558c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5597cc5997dSYinghai Lu } else { 5601da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 5611da177e4SLinus Torvalds io_upper16 = 0; 5621da177e4SLinus Torvalds l = 0x00f0; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 5651da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 5661da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 5675b764b83SBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, l); 5681da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 5691da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 5707cc5997dSYinghai Lu } 5711da177e4SLinus Torvalds 5727cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus) 5737cc5997dSYinghai Lu { 5747cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5757cc5997dSYinghai Lu struct resource *res; 5767cc5997dSYinghai Lu struct pci_bus_region region; 5777cc5997dSYinghai Lu u32 l; 5787cc5997dSYinghai Lu 5797cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 580c7dabef8SBjorn Helgaas res = bus->resource[1]; 581fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 582c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 5831da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 5841da177e4SLinus Torvalds l |= region.end & 0xfff00000; 585c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5867cc5997dSYinghai Lu } else { 5871da177e4SLinus Torvalds l = 0x0000fff0; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 5907cc5997dSYinghai Lu } 5917cc5997dSYinghai Lu 5927cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) 5937cc5997dSYinghai Lu { 5947cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5957cc5997dSYinghai Lu struct resource *res; 5967cc5997dSYinghai Lu struct pci_bus_region region; 5977cc5997dSYinghai Lu u32 l, bu, lu; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 6001da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 6011da177e4SLinus Torvalds disables PREF range, which is ok. */ 6021da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds /* Set up PREF base/limit. */ 605c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 606c7dabef8SBjorn Helgaas res = bus->resource[2]; 607fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 608c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 6091da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6101da177e4SLinus Torvalds l |= region.end & 0xfff00000; 611c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 61213d36c24SAndrew Morton bu = upper_32_bits(region.start); 61313d36c24SAndrew Morton lu = upper_32_bits(region.end); 6141f82de10SYinghai Lu } 615c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 6167cc5997dSYinghai Lu } else { 6171da177e4SLinus Torvalds l = 0x0000fff0; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 6201da177e4SLinus Torvalds 621c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 622c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 623c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 6247cc5997dSYinghai Lu } 6257cc5997dSYinghai Lu 6267cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 6277cc5997dSYinghai Lu { 6287cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 6297cc5997dSYinghai Lu 630b918c62eSYinghai Lu dev_info(&bridge->dev, "PCI bridge to %pR\n", 631b918c62eSYinghai Lu &bus->busn_res); 6327cc5997dSYinghai Lu 6337cc5997dSYinghai Lu if (type & IORESOURCE_IO) 6347cc5997dSYinghai Lu pci_setup_bridge_io(bus); 6357cc5997dSYinghai Lu 6367cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 6377cc5997dSYinghai Lu pci_setup_bridge_mmio(bus); 6387cc5997dSYinghai Lu 6397cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 6407cc5997dSYinghai Lu pci_setup_bridge_mmio_pref(bus); 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 645e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 6467cc5997dSYinghai Lu { 6477cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 6487cc5997dSYinghai Lu IORESOURCE_PREFETCH; 6497cc5997dSYinghai Lu 6507cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 6517cc5997dSYinghai Lu } 6527cc5997dSYinghai Lu 6531da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 6541da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 6551da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 65696bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 6571da177e4SLinus Torvalds { 6581da177e4SLinus Torvalds u16 io; 6591da177e4SLinus Torvalds u32 pmem; 6601da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 6611da177e4SLinus Torvalds struct resource *b_res; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 6641da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6671da177e4SLinus Torvalds if (!io) { 668d2f54d9bSBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0); 6691da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6701da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds if (io) 6731da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 674d2f54d9bSBjorn Helgaas 6751da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 6761da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 6771da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 6781da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 6791da177e4SLinus Torvalds return; 680d2f54d9bSBjorn Helgaas 6811da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 6821da177e4SLinus Torvalds if (!pmem) { 6831da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 684d2f54d9bSBjorn Helgaas 0xffe0fff0); 6851da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 6861da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 6871da177e4SLinus Torvalds } 6881f82de10SYinghai Lu if (pmem) { 6891da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 69099586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 69199586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 6921f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 69399586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 69499586105SYinghai Lu } 6951f82de10SYinghai Lu } 6961f82de10SYinghai Lu 6971f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 6981f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 6991f82de10SYinghai Lu u32 mem_base_hi, tmp; 7001f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7011f82de10SYinghai Lu &mem_base_hi); 7021f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7031f82de10SYinghai Lu 0xffffffff); 7041f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 7051f82de10SYinghai Lu if (!tmp) 7061f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 7071f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 7081f82de10SYinghai Lu mem_base_hi); 7091f82de10SYinghai Lu } 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 7131da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 7141da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 7151da177e4SLinus Torvalds have non-NULL parent resource). */ 7165b285415SYinghai Lu static struct resource *find_free_bus_resource(struct pci_bus *bus, 7175b285415SYinghai Lu unsigned long type_mask, unsigned long type) 7181da177e4SLinus Torvalds { 7191da177e4SLinus Torvalds int i; 7201da177e4SLinus Torvalds struct resource *r; 7211da177e4SLinus Torvalds 72289a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 723299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 724299de034SIvan Kokshaysky continue; 72555a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 7261da177e4SLinus Torvalds return r; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds return NULL; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 73113583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 73213583b16SRam Pai resource_size_t min_size, 73313583b16SRam Pai resource_size_t size1, 73413583b16SRam Pai resource_size_t old_size, 73513583b16SRam Pai resource_size_t align) 73613583b16SRam Pai { 73713583b16SRam Pai if (size < min_size) 73813583b16SRam Pai size = min_size; 73913583b16SRam Pai if (old_size == 1 ) 74013583b16SRam Pai old_size = 0; 74113583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 74213583b16SRam Pai flag in the struct pci_bus. */ 74313583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 74413583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 74513583b16SRam Pai #endif 74613583b16SRam Pai size = ALIGN(size + size1, align); 74713583b16SRam Pai if (size < old_size) 74813583b16SRam Pai size = old_size; 74913583b16SRam Pai return size; 75013583b16SRam Pai } 75113583b16SRam Pai 75213583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 75313583b16SRam Pai resource_size_t min_size, 75413583b16SRam Pai resource_size_t size1, 75513583b16SRam Pai resource_size_t old_size, 75613583b16SRam Pai resource_size_t align) 75713583b16SRam Pai { 75813583b16SRam Pai if (size < min_size) 75913583b16SRam Pai size = min_size; 76013583b16SRam Pai if (old_size == 1 ) 76113583b16SRam Pai old_size = 0; 76213583b16SRam Pai if (size < old_size) 76313583b16SRam Pai size = old_size; 76413583b16SRam Pai size = ALIGN(size + size1, align); 76513583b16SRam Pai return size; 76613583b16SRam Pai } 76713583b16SRam Pai 768ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, 769ac5ad93eSGavin Shan unsigned long type) 770ac5ad93eSGavin Shan { 771ac5ad93eSGavin Shan return 1; 772ac5ad93eSGavin Shan } 773ac5ad93eSGavin Shan 774ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */ 775ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */ 776ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */ 777ac5ad93eSGavin Shan 778ac5ad93eSGavin Shan static resource_size_t window_alignment(struct pci_bus *bus, 779ac5ad93eSGavin Shan unsigned long type) 780ac5ad93eSGavin Shan { 781ac5ad93eSGavin Shan resource_size_t align = 1, arch_align; 782ac5ad93eSGavin Shan 783ac5ad93eSGavin Shan if (type & IORESOURCE_MEM) 784ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_MEM_ALIGN; 785ac5ad93eSGavin Shan else if (type & IORESOURCE_IO) { 786ac5ad93eSGavin Shan /* 787ac5ad93eSGavin Shan * Per spec, I/O windows are 4K-aligned, but some 788ac5ad93eSGavin Shan * bridges have an extension to support 1K alignment. 789ac5ad93eSGavin Shan */ 790ac5ad93eSGavin Shan if (bus->self->io_window_1k) 791ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN_1K; 792ac5ad93eSGavin Shan else 793ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN; 794ac5ad93eSGavin Shan } 795ac5ad93eSGavin Shan 796ac5ad93eSGavin Shan arch_align = pcibios_window_alignment(bus, type); 797ac5ad93eSGavin Shan return max(align, arch_align); 798ac5ad93eSGavin Shan } 799ac5ad93eSGavin Shan 800c8adf9a3SRam Pai /** 801c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 802c8adf9a3SRam Pai * 803c8adf9a3SRam Pai * @bus : the bus 804c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 805c8adf9a3SRam Pai * @add_size : additional optional io window 8069e8bf93aSRam Pai * @realloc_head : track the additional io window on this list 807c8adf9a3SRam Pai * 808c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 809fd591341SYinghai Lu * since these windows have 1K or 4K granularity and the IO ranges 810c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 811c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 812c8adf9a3SRam Pai */ 813c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 814bdc4abecSYinghai Lu resource_size_t add_size, struct list_head *realloc_head) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds struct pci_dev *dev; 8175b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO, 8185b285415SYinghai Lu IORESOURCE_IO); 81911251a86SWei Yang resource_size_t size = 0, size0 = 0, size1 = 0; 820be768912SYinghai Lu resource_size_t children_add_size = 0; 8212d1d6678SBjorn Helgaas resource_size_t min_align, align; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds if (!b_res) 8241da177e4SLinus Torvalds return; 8251da177e4SLinus Torvalds 8262d1d6678SBjorn Helgaas min_align = window_alignment(bus, IORESOURCE_IO); 8271da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8281da177e4SLinus Torvalds int i; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 8311da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 8321da177e4SLinus Torvalds unsigned long r_size; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 8351da177e4SLinus Torvalds continue; 836022edd86SZhao, Yu r_size = resource_size(r); 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds if (r_size < 0x400) 8391da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 8401da177e4SLinus Torvalds size += r_size; 8411da177e4SLinus Torvalds else 8421da177e4SLinus Torvalds size1 += r_size; 843be768912SYinghai Lu 844fd591341SYinghai Lu align = pci_resource_alignment(dev, r); 845fd591341SYinghai Lu if (align > min_align) 846fd591341SYinghai Lu min_align = align; 847fd591341SYinghai Lu 8489e8bf93aSRam Pai if (realloc_head) 8499e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds } 852fd591341SYinghai Lu 853c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 854fd591341SYinghai Lu resource_size(b_res), min_align); 855be768912SYinghai Lu if (children_add_size > add_size) 856be768912SYinghai Lu add_size = children_add_size; 8579e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 858a4ac9feaSYinghai Lu calculate_iosize(size, min_size, add_size + size1, 859fd591341SYinghai Lu resource_size(b_res), min_align); 860c8adf9a3SRam Pai if (!size0 && !size1) { 861865df576SBjorn Helgaas if (b_res->start || b_res->end) 862865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 863b918c62eSYinghai Lu "%pR to %pR (unused)\n", b_res, 864b918c62eSYinghai Lu &bus->busn_res); 8651da177e4SLinus Torvalds b_res->flags = 0; 8661da177e4SLinus Torvalds return; 8671da177e4SLinus Torvalds } 868fd591341SYinghai Lu 869fd591341SYinghai Lu b_res->start = min_align; 870c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 87188452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 872b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 873fd591341SYinghai Lu add_to_list(realloc_head, bus->self, b_res, size1-size0, 874fd591341SYinghai Lu min_align); 875b592443dSYinghai Lu dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 87611251a86SWei Yang "%pR to %pR add_size %llx\n", b_res, 87711251a86SWei Yang &bus->busn_res, 87811251a86SWei Yang (unsigned long long)size1-size0); 879b592443dSYinghai Lu } 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 882c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns, 883c121504eSGavin Shan int max_order) 884c121504eSGavin Shan { 885c121504eSGavin Shan resource_size_t align = 0; 886c121504eSGavin Shan resource_size_t min_align = 0; 887c121504eSGavin Shan int order; 888c121504eSGavin Shan 889c121504eSGavin Shan for (order = 0; order <= max_order; order++) { 890c121504eSGavin Shan resource_size_t align1 = 1; 891c121504eSGavin Shan 892c121504eSGavin Shan align1 <<= (order + 20); 893c121504eSGavin Shan 894c121504eSGavin Shan if (!align) 895c121504eSGavin Shan min_align = align1; 896c121504eSGavin Shan else if (ALIGN(align + min_align, min_align) < align1) 897c121504eSGavin Shan min_align = align1 >> 1; 898c121504eSGavin Shan align += aligns[order]; 899c121504eSGavin Shan } 900c121504eSGavin Shan 901c121504eSGavin Shan return min_align; 902c121504eSGavin Shan } 903c121504eSGavin Shan 904c8adf9a3SRam Pai /** 905c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 906c8adf9a3SRam Pai * 907c8adf9a3SRam Pai * @bus : the bus 908496f70cfSWei Yang * @mask: mask the resource flag, then compare it with type 909496f70cfSWei Yang * @type: the type of free resource from bridge 9105b285415SYinghai Lu * @type2: second match type 9115b285415SYinghai Lu * @type3: third match type 912c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 913c8adf9a3SRam Pai * @add_size : additional optional memory window 9149e8bf93aSRam Pai * @realloc_head : track the additional memory window on this list 915c8adf9a3SRam Pai * 916c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 917c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 91830afe8d0SBjorn Helgaas * 91930afe8d0SBjorn Helgaas * Returns -ENOSPC if there's no available bus resource of the desired type. 92030afe8d0SBjorn Helgaas * Otherwise, sets the bus resource start/end to indicate the required 92130afe8d0SBjorn Helgaas * size, adds things to realloc_head (if supplied), and returns 0. 922c8adf9a3SRam Pai */ 92328760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 9245b285415SYinghai Lu unsigned long type, unsigned long type2, 9255b285415SYinghai Lu unsigned long type3, 9265b285415SYinghai Lu resource_size_t min_size, resource_size_t add_size, 927bdc4abecSYinghai Lu struct list_head *realloc_head) 9281da177e4SLinus Torvalds { 9291da177e4SLinus Torvalds struct pci_dev *dev; 930c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 93114c8530dSAlan resource_size_t aligns[14]; /* Alignments from 1Mb to 8Gb */ 9321da177e4SLinus Torvalds int order, max_order; 9335b285415SYinghai Lu struct resource *b_res = find_free_bus_resource(bus, 9345b285415SYinghai Lu mask | IORESOURCE_PREFETCH, type); 935be768912SYinghai Lu resource_size_t children_add_size = 0; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (!b_res) 93830afe8d0SBjorn Helgaas return -ENOSPC; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 9411da177e4SLinus Torvalds max_order = 0; 9421da177e4SLinus Torvalds size = 0; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 9451da177e4SLinus Torvalds int i; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 9481da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 949c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 9501da177e4SLinus Torvalds 9515b285415SYinghai Lu if (r->parent || ((r->flags & mask) != type && 9525b285415SYinghai Lu (r->flags & mask) != type2 && 9535b285415SYinghai Lu (r->flags & mask) != type3)) 9541da177e4SLinus Torvalds continue; 955022edd86SZhao, Yu r_size = resource_size(r); 9562aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 9572aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 9589e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 9592aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 9602aceefcbSYinghai Lu r->end = r->start - 1; 961f7625980SBjorn Helgaas add_to_list(realloc_head, dev, r, r_size, 0/* don't care */); 9622aceefcbSYinghai Lu children_add_size += r_size; 9632aceefcbSYinghai Lu continue; 9642aceefcbSYinghai Lu } 9652aceefcbSYinghai Lu #endif 96614c8530dSAlan /* 96714c8530dSAlan * aligns[0] is for 1MB (since bridge memory 96814c8530dSAlan * windows are always at least 1MB aligned), so 96914c8530dSAlan * keep "order" from being negative for smaller 97014c8530dSAlan * resources. 97114c8530dSAlan */ 9726faf17f6SChris Wright align = pci_resource_alignment(dev, r); 9731da177e4SLinus Torvalds order = __ffs(align) - 20; 97414c8530dSAlan if (order < 0) 97514c8530dSAlan order = 0; 97614c8530dSAlan if (order >= ARRAY_SIZE(aligns)) { 977865df576SBjorn Helgaas dev_warn(&dev->dev, "disabling BAR %d: %pR " 978865df576SBjorn Helgaas "(bad alignment %#llx)\n", i, r, 979865df576SBjorn Helgaas (unsigned long long) align); 9801da177e4SLinus Torvalds r->flags = 0; 9811da177e4SLinus Torvalds continue; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds size += r_size; 9841da177e4SLinus Torvalds /* Exclude ranges with size > align from 9851da177e4SLinus Torvalds calculation of the alignment. */ 9861da177e4SLinus Torvalds if (r_size == align) 9871da177e4SLinus Torvalds aligns[order] += align; 9881da177e4SLinus Torvalds if (order > max_order) 9891da177e4SLinus Torvalds max_order = order; 990be768912SYinghai Lu 9919e8bf93aSRam Pai if (realloc_head) 9929e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds } 9958308c54dSJeremy Fitzhardinge 996c121504eSGavin Shan min_align = calculate_mem_align(aligns, max_order); 9973ad94b0dSWei Yang min_align = max(min_align, window_alignment(bus, b_res->flags)); 998b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 999be768912SYinghai Lu if (children_add_size > add_size) 1000be768912SYinghai Lu add_size = children_add_size; 10019e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 1002a4ac9feaSYinghai Lu calculate_memsize(size, min_size, add_size, 1003b42282e5SLinus Torvalds resource_size(b_res), min_align); 1004c8adf9a3SRam Pai if (!size0 && !size1) { 1005865df576SBjorn Helgaas if (b_res->start || b_res->end) 1006865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 1007b918c62eSYinghai Lu "%pR to %pR (unused)\n", b_res, 1008b918c62eSYinghai Lu &bus->busn_res); 10091da177e4SLinus Torvalds b_res->flags = 0; 101030afe8d0SBjorn Helgaas return 0; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds b_res->start = min_align; 1013c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 10145b285415SYinghai Lu b_res->flags |= IORESOURCE_STARTALIGN; 1015b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 10169e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 1017b592443dSYinghai Lu dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 1018b918c62eSYinghai Lu "%pR to %pR add_size %llx\n", b_res, 1019b918c62eSYinghai Lu &bus->busn_res, (unsigned long long)size1-size0); 1020b592443dSYinghai Lu } 102130afe8d0SBjorn Helgaas return 0; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 10240a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 10250a2daa1cSRam Pai { 10260a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 10270a2daa1cSRam Pai return pci_cardbus_io_size; 10280a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 10290a2daa1cSRam Pai return pci_cardbus_mem_size; 10300a2daa1cSRam Pai return 0; 10310a2daa1cSRam Pai } 10320a2daa1cSRam Pai 10330a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 1034bdc4abecSYinghai Lu struct list_head *realloc_head) 10351da177e4SLinus Torvalds { 10361da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 10371da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 103811848934SYinghai Lu resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 10391da177e4SLinus Torvalds u16 ctrl; 10401da177e4SLinus Torvalds 10413796f1e2SYinghai Lu if (b_res[0].parent) 10423796f1e2SYinghai Lu goto handle_b_res_1; 10431da177e4SLinus Torvalds /* 10441da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 10451da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 10461da177e4SLinus Torvalds */ 104711848934SYinghai Lu b_res[0].start = pci_cardbus_io_size; 104811848934SYinghai Lu b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; 104911848934SYinghai Lu b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 105011848934SYinghai Lu if (realloc_head) { 105111848934SYinghai Lu b_res[0].end -= pci_cardbus_io_size; 105211848934SYinghai Lu add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 105311848934SYinghai Lu pci_cardbus_io_size); 105411848934SYinghai Lu } 10551da177e4SLinus Torvalds 10563796f1e2SYinghai Lu handle_b_res_1: 10573796f1e2SYinghai Lu if (b_res[1].parent) 10583796f1e2SYinghai Lu goto handle_b_res_2; 105911848934SYinghai Lu b_res[1].start = pci_cardbus_io_size; 106011848934SYinghai Lu b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; 106111848934SYinghai Lu b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 106211848934SYinghai Lu if (realloc_head) { 106311848934SYinghai Lu b_res[1].end -= pci_cardbus_io_size; 106411848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 106511848934SYinghai Lu pci_cardbus_io_size); 106611848934SYinghai Lu } 10671da177e4SLinus Torvalds 10683796f1e2SYinghai Lu handle_b_res_2: 1069dcef0d06SYinghai Lu /* MEM1 must not be pref mmio */ 1070dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1071dcef0d06SYinghai Lu if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 1072dcef0d06SYinghai Lu ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 1073dcef0d06SYinghai Lu pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1074dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1075dcef0d06SYinghai Lu } 1076dcef0d06SYinghai Lu 10771da177e4SLinus Torvalds /* 10781da177e4SLinus Torvalds * Check whether prefetchable memory is supported 10791da177e4SLinus Torvalds * by this bridge. 10801da177e4SLinus Torvalds */ 10811da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 10821da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 10831da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 10841da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 10851da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 10883796f1e2SYinghai Lu if (b_res[2].parent) 10893796f1e2SYinghai Lu goto handle_b_res_3; 10901da177e4SLinus Torvalds /* 10911da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 10921da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 10931da177e4SLinus Torvalds * twice the size. 10941da177e4SLinus Torvalds */ 10951da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 109611848934SYinghai Lu b_res[2].start = pci_cardbus_mem_size; 109711848934SYinghai Lu b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; 109811848934SYinghai Lu b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 109911848934SYinghai Lu IORESOURCE_STARTALIGN; 110011848934SYinghai Lu if (realloc_head) { 110111848934SYinghai Lu b_res[2].end -= pci_cardbus_mem_size; 110211848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+2, 110311848934SYinghai Lu pci_cardbus_mem_size, pci_cardbus_mem_size); 11041da177e4SLinus Torvalds } 11050a2daa1cSRam Pai 110611848934SYinghai Lu /* reduce that to half */ 110711848934SYinghai Lu b_res_3_size = pci_cardbus_mem_size; 110811848934SYinghai Lu } 110911848934SYinghai Lu 11103796f1e2SYinghai Lu handle_b_res_3: 11113796f1e2SYinghai Lu if (b_res[3].parent) 11123796f1e2SYinghai Lu goto handle_done; 111311848934SYinghai Lu b_res[3].start = pci_cardbus_mem_size; 111411848934SYinghai Lu b_res[3].end = b_res[3].start + b_res_3_size - 1; 111511848934SYinghai Lu b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 111611848934SYinghai Lu if (realloc_head) { 111711848934SYinghai Lu b_res[3].end -= b_res_3_size; 111811848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+3, b_res_3_size, 111911848934SYinghai Lu pci_cardbus_mem_size); 112011848934SYinghai Lu } 11213796f1e2SYinghai Lu 11223796f1e2SYinghai Lu handle_done: 11233796f1e2SYinghai Lu ; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 1126d66ecb72SJiang Liu void __ref __pci_bus_size_bridges(struct pci_bus *bus, 1127bdc4abecSYinghai Lu struct list_head *realloc_head) 11281da177e4SLinus Torvalds { 11291da177e4SLinus Torvalds struct pci_dev *dev; 11305b285415SYinghai Lu unsigned long mask, prefmask, type2 = 0, type3 = 0; 1131c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 11325b285415SYinghai Lu struct resource *b_res; 113330afe8d0SBjorn Helgaas int ret; 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 11361da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 11371da177e4SLinus Torvalds if (!b) 11381da177e4SLinus Torvalds continue; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds switch (dev->class >> 8) { 11411da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 11429e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 11431da177e4SLinus Torvalds break; 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 11461da177e4SLinus Torvalds default: 11479e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 11481da177e4SLinus Torvalds break; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* The root bus? */ 11532ba29e27SWei Yang if (pci_is_root_bus(bus)) 11541da177e4SLinus Torvalds return; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds switch (bus->self->class >> 8) { 11571da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 11581da177e4SLinus Torvalds /* don't size cardbuses yet. */ 11591da177e4SLinus Torvalds break; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 11621da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 116328760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 1164c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 1165c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 116628760489SEric W. Biederman } 1167*67d29b5cSBjorn Helgaas /* Fall through */ 11681da177e4SLinus Torvalds default: 116919aa7ee4SYinghai Lu pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 117019aa7ee4SYinghai Lu additional_io_size, realloc_head); 1171*67d29b5cSBjorn Helgaas 1172*67d29b5cSBjorn Helgaas /* 1173*67d29b5cSBjorn Helgaas * If there's a 64-bit prefetchable MMIO window, compute 1174*67d29b5cSBjorn Helgaas * the size required to put all 64-bit prefetchable 1175*67d29b5cSBjorn Helgaas * resources in it. 1176*67d29b5cSBjorn Helgaas */ 11775b285415SYinghai Lu b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES]; 11781da177e4SLinus Torvalds mask = IORESOURCE_MEM; 11791da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 11805b285415SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 11815b285415SYinghai Lu prefmask |= IORESOURCE_MEM_64; 118230afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 11835b285415SYinghai Lu prefmask, prefmask, 118419aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 118530afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 1186*67d29b5cSBjorn Helgaas 11875b285415SYinghai Lu /* 1188*67d29b5cSBjorn Helgaas * If successful, all non-prefetchable resources 1189*67d29b5cSBjorn Helgaas * and any 32-bit prefetchable resources will go in 1190*67d29b5cSBjorn Helgaas * the non-prefetchable window. 1191*67d29b5cSBjorn Helgaas */ 1192*67d29b5cSBjorn Helgaas if (ret == 0) { 11935b285415SYinghai Lu mask = prefmask; 11945b285415SYinghai Lu type2 = prefmask & ~IORESOURCE_MEM_64; 11955b285415SYinghai Lu type3 = prefmask & ~IORESOURCE_PREFETCH; 11965b285415SYinghai Lu } 11975b285415SYinghai Lu } 1198*67d29b5cSBjorn Helgaas 1199*67d29b5cSBjorn Helgaas /* 1200*67d29b5cSBjorn Helgaas * If there is no 64-bit prefetchable window, compute the 1201*67d29b5cSBjorn Helgaas * size required to put all prefetchable resources in the 1202*67d29b5cSBjorn Helgaas * 32-bit prefetchable window (if there is one). 1203*67d29b5cSBjorn Helgaas */ 12045b285415SYinghai Lu if (!type2) { 12055b285415SYinghai Lu prefmask &= ~IORESOURCE_MEM_64; 120630afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12075b285415SYinghai Lu prefmask, prefmask, 12085b285415SYinghai Lu realloc_head ? 0 : additional_mem_size, 120930afe8d0SBjorn Helgaas additional_mem_size, realloc_head); 1210*67d29b5cSBjorn Helgaas 1211*67d29b5cSBjorn Helgaas /* 1212*67d29b5cSBjorn Helgaas * If successful, only non-prefetchable resources 1213*67d29b5cSBjorn Helgaas * will go in the non-prefetchable window. 1214*67d29b5cSBjorn Helgaas */ 1215*67d29b5cSBjorn Helgaas if (ret == 0) 12165b285415SYinghai Lu mask = prefmask; 1217*67d29b5cSBjorn Helgaas else 1218c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 1219*67d29b5cSBjorn Helgaas 12205b285415SYinghai Lu type2 = type3 = IORESOURCE_MEM; 12215b285415SYinghai Lu } 1222*67d29b5cSBjorn Helgaas 1223*67d29b5cSBjorn Helgaas /* 1224*67d29b5cSBjorn Helgaas * Compute the size required to put everything else in the 1225*67d29b5cSBjorn Helgaas * non-prefetchable window. This includes: 1226*67d29b5cSBjorn Helgaas * 1227*67d29b5cSBjorn Helgaas * - all non-prefetchable resources 1228*67d29b5cSBjorn Helgaas * - 32-bit prefetchable resources if there's a 64-bit 1229*67d29b5cSBjorn Helgaas * prefetchable window or no prefetchable window at all 1230*67d29b5cSBjorn Helgaas * - 64-bit prefetchable resources if there's no 1231*67d29b5cSBjorn Helgaas * prefetchable window at all 1232*67d29b5cSBjorn Helgaas * 1233*67d29b5cSBjorn Helgaas * Note that the strategy in __pci_assign_resource() must 1234*67d29b5cSBjorn Helgaas * match that used here. Specifically, we cannot put a 1235*67d29b5cSBjorn Helgaas * 32-bit prefetchable resource in a 64-bit prefetchable 1236*67d29b5cSBjorn Helgaas * window. 1237*67d29b5cSBjorn Helgaas */ 12385b285415SYinghai Lu pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, 123919aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 124019aa7ee4SYinghai Lu additional_mem_size, realloc_head); 12411da177e4SLinus Torvalds break; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds } 1244c8adf9a3SRam Pai 1245c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus) 1246c8adf9a3SRam Pai { 1247c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 1248c8adf9a3SRam Pai } 12491da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 12501da177e4SLinus Torvalds 1251d66ecb72SJiang Liu void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 1252bdc4abecSYinghai Lu struct list_head *realloc_head, 1253bdc4abecSYinghai Lu struct list_head *fail_head) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds struct pci_bus *b; 12561da177e4SLinus Torvalds struct pci_dev *dev; 12571da177e4SLinus Torvalds 12589e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 12611da177e4SLinus Torvalds b = dev->subordinate; 12621da177e4SLinus Torvalds if (!b) 12631da177e4SLinus Torvalds continue; 12641da177e4SLinus Torvalds 12659e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds switch (dev->class >> 8) { 12681da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 12696841ec68SYinghai Lu if (!pci_is_enabled(dev)) 12701da177e4SLinus Torvalds pci_setup_bridge(b); 12711da177e4SLinus Torvalds break; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 12741da177e4SLinus Torvalds pci_setup_cardbus(b); 12751da177e4SLinus Torvalds break; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds default: 127880ccba11SBjorn Helgaas dev_info(&dev->dev, "not setting up bridge for bus " 127980ccba11SBjorn Helgaas "%04x:%02x\n", pci_domain_nr(b), b->number); 12801da177e4SLinus Torvalds break; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds } 1284568ddef8SYinghai Lu 1285568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus) 1286568ddef8SYinghai Lu { 1287c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 1288568ddef8SYinghai Lu } 12891da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 12901da177e4SLinus Torvalds 12916841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 1292bdc4abecSYinghai Lu struct list_head *add_head, 1293bdc4abecSYinghai Lu struct list_head *fail_head) 12946841ec68SYinghai Lu { 12956841ec68SYinghai Lu struct pci_bus *b; 12966841ec68SYinghai Lu 12978424d759SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, 12988424d759SYinghai Lu add_head, fail_head); 12996841ec68SYinghai Lu 13006841ec68SYinghai Lu b = bridge->subordinate; 13016841ec68SYinghai Lu if (!b) 13026841ec68SYinghai Lu return; 13036841ec68SYinghai Lu 13048424d759SYinghai Lu __pci_bus_assign_resources(b, add_head, fail_head); 13056841ec68SYinghai Lu 13066841ec68SYinghai Lu switch (bridge->class >> 8) { 13076841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 13086841ec68SYinghai Lu pci_setup_bridge(b); 13096841ec68SYinghai Lu break; 13106841ec68SYinghai Lu 13116841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 13126841ec68SYinghai Lu pci_setup_cardbus(b); 13136841ec68SYinghai Lu break; 13146841ec68SYinghai Lu 13156841ec68SYinghai Lu default: 13166841ec68SYinghai Lu dev_info(&bridge->dev, "not setting up bridge for bus " 13176841ec68SYinghai Lu "%04x:%02x\n", pci_domain_nr(b), b->number); 13186841ec68SYinghai Lu break; 13196841ec68SYinghai Lu } 13206841ec68SYinghai Lu } 13215009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 13225009b460SYinghai Lu unsigned long type) 13235009b460SYinghai Lu { 13245b285415SYinghai Lu struct pci_dev *dev = bus->self; 13255009b460SYinghai Lu struct resource *r; 13265009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 13275b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 13285b285415SYinghai Lu unsigned old_flags = 0; 13295b285415SYinghai Lu struct resource *b_res; 13305b285415SYinghai Lu int idx = 1; 13315009b460SYinghai Lu 13325b285415SYinghai Lu b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; 13335b285415SYinghai Lu 13345b285415SYinghai Lu /* 13355b285415SYinghai Lu * 1. if there is io port assign fail, will release bridge 13365b285415SYinghai Lu * io port. 13375b285415SYinghai Lu * 2. if there is non pref mmio assign fail, release bridge 13385b285415SYinghai Lu * nonpref mmio. 13395b285415SYinghai Lu * 3. if there is 64bit pref mmio assign fail, and bridge pref 13405b285415SYinghai Lu * is 64bit, release bridge pref mmio. 13415b285415SYinghai Lu * 4. if there is pref mmio assign fail, and bridge pref is 13425b285415SYinghai Lu * 32bit mmio, release bridge pref mmio 13435b285415SYinghai Lu * 5. if there is pref mmio assign fail, and bridge pref is not 13445b285415SYinghai Lu * assigned, release bridge nonpref mmio. 13455b285415SYinghai Lu */ 13465b285415SYinghai Lu if (type & IORESOURCE_IO) 13475b285415SYinghai Lu idx = 0; 13485b285415SYinghai Lu else if (!(type & IORESOURCE_PREFETCH)) 13495b285415SYinghai Lu idx = 1; 13505b285415SYinghai Lu else if ((type & IORESOURCE_MEM_64) && 13515b285415SYinghai Lu (b_res[2].flags & IORESOURCE_MEM_64)) 13525b285415SYinghai Lu idx = 2; 13535b285415SYinghai Lu else if (!(b_res[2].flags & IORESOURCE_MEM_64) && 13545b285415SYinghai Lu (b_res[2].flags & IORESOURCE_PREFETCH)) 13555b285415SYinghai Lu idx = 2; 13565b285415SYinghai Lu else 13575b285415SYinghai Lu idx = 1; 13585b285415SYinghai Lu 13595b285415SYinghai Lu r = &b_res[idx]; 13605b285415SYinghai Lu 13615009b460SYinghai Lu if (!r->parent) 13625b285415SYinghai Lu return; 13635b285415SYinghai Lu 13645009b460SYinghai Lu /* 13655009b460SYinghai Lu * if there are children under that, we should release them 13665009b460SYinghai Lu * all 13675009b460SYinghai Lu */ 13685009b460SYinghai Lu release_child_resources(r); 13695009b460SYinghai Lu if (!release_resource(r)) { 13705b285415SYinghai Lu type = old_flags = r->flags & type_mask; 13715b285415SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n", 13725b285415SYinghai Lu PCI_BRIDGE_RESOURCES + idx, r); 13735009b460SYinghai Lu /* keep the old size */ 13745009b460SYinghai Lu r->end = resource_size(r) - 1; 13755009b460SYinghai Lu r->start = 0; 13765009b460SYinghai Lu r->flags = 0; 13775009b460SYinghai Lu 13785009b460SYinghai Lu /* avoiding touch the one without PREF */ 13795009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 13805009b460SYinghai Lu type = IORESOURCE_PREFETCH; 13815009b460SYinghai Lu __pci_setup_bridge(bus, type); 13825b285415SYinghai Lu /* for next child res under same bridge */ 13835b285415SYinghai Lu r->flags = old_flags; 13845009b460SYinghai Lu } 13855009b460SYinghai Lu } 13865009b460SYinghai Lu 13875009b460SYinghai Lu enum release_type { 13885009b460SYinghai Lu leaf_only, 13895009b460SYinghai Lu whole_subtree, 13905009b460SYinghai Lu }; 13915009b460SYinghai Lu /* 13925009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 13935009b460SYinghai Lu * so we can allocate big new one later 13945009b460SYinghai Lu */ 13955009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, 13965009b460SYinghai Lu unsigned long type, 13975009b460SYinghai Lu enum release_type rel_type) 13985009b460SYinghai Lu { 13995009b460SYinghai Lu struct pci_dev *dev; 14005009b460SYinghai Lu bool is_leaf_bridge = true; 14015009b460SYinghai Lu 14025009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 14035009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 14045009b460SYinghai Lu if (!b) 14055009b460SYinghai Lu continue; 14065009b460SYinghai Lu 14075009b460SYinghai Lu is_leaf_bridge = false; 14085009b460SYinghai Lu 14095009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 14105009b460SYinghai Lu continue; 14115009b460SYinghai Lu 14125009b460SYinghai Lu if (rel_type == whole_subtree) 14135009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 14145009b460SYinghai Lu whole_subtree); 14155009b460SYinghai Lu } 14165009b460SYinghai Lu 14175009b460SYinghai Lu if (pci_is_root_bus(bus)) 14185009b460SYinghai Lu return; 14195009b460SYinghai Lu 14205009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 14215009b460SYinghai Lu return; 14225009b460SYinghai Lu 14235009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 14245009b460SYinghai Lu pci_bridge_release_resources(bus, type); 14255009b460SYinghai Lu } 14265009b460SYinghai Lu 142776fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 142876fbc263SYinghai Lu { 142989a74eccSBjorn Helgaas struct resource *res; 143076fbc263SYinghai Lu int i; 143176fbc263SYinghai Lu 143289a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 14337c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 143476fbc263SYinghai Lu continue; 143576fbc263SYinghai Lu 1436c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 143776fbc263SYinghai Lu } 143876fbc263SYinghai Lu } 143976fbc263SYinghai Lu 144076fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 144176fbc263SYinghai Lu { 144276fbc263SYinghai Lu struct pci_bus *b; 144376fbc263SYinghai Lu struct pci_dev *dev; 144476fbc263SYinghai Lu 144576fbc263SYinghai Lu 144676fbc263SYinghai Lu pci_bus_dump_res(bus); 144776fbc263SYinghai Lu 144876fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 144976fbc263SYinghai Lu b = dev->subordinate; 145076fbc263SYinghai Lu if (!b) 145176fbc263SYinghai Lu continue; 145276fbc263SYinghai Lu 145376fbc263SYinghai Lu pci_bus_dump_resources(b); 145476fbc263SYinghai Lu } 145576fbc263SYinghai Lu } 145676fbc263SYinghai Lu 1457ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus) 1458da7822e5SYinghai Lu { 1459da7822e5SYinghai Lu int depth = 0; 1460f2a230bdSWei Yang struct pci_bus *child_bus; 1461da7822e5SYinghai Lu 1462f2a230bdSWei Yang list_for_each_entry(child_bus, &bus->children, node){ 1463da7822e5SYinghai Lu int ret; 1464da7822e5SYinghai Lu 1465f2a230bdSWei Yang ret = pci_bus_get_depth(child_bus); 1466da7822e5SYinghai Lu if (ret + 1 > depth) 1467da7822e5SYinghai Lu depth = ret + 1; 1468da7822e5SYinghai Lu } 1469da7822e5SYinghai Lu 1470da7822e5SYinghai Lu return depth; 1471da7822e5SYinghai Lu } 1472da7822e5SYinghai Lu 1473b55438fdSYinghai Lu /* 1474b55438fdSYinghai Lu * -1: undefined, will auto detect later 1475b55438fdSYinghai Lu * 0: disabled by user 1476b55438fdSYinghai Lu * 1: disabled by auto detect 1477b55438fdSYinghai Lu * 2: enabled by user 1478b55438fdSYinghai Lu * 3: enabled by auto detect 1479b55438fdSYinghai Lu */ 1480b55438fdSYinghai Lu enum enable_type { 1481b55438fdSYinghai Lu undefined = -1, 1482b55438fdSYinghai Lu user_disabled, 1483b55438fdSYinghai Lu auto_disabled, 1484b55438fdSYinghai Lu user_enabled, 1485b55438fdSYinghai Lu auto_enabled, 1486b55438fdSYinghai Lu }; 1487b55438fdSYinghai Lu 1488ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined; 1489b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str) 1490b55438fdSYinghai Lu { 1491b55438fdSYinghai Lu if (!strncmp(str, "off", 3)) 1492b55438fdSYinghai Lu pci_realloc_enable = user_disabled; 1493b55438fdSYinghai Lu else if (!strncmp(str, "on", 2)) 1494b55438fdSYinghai Lu pci_realloc_enable = user_enabled; 1495b55438fdSYinghai Lu } 1496ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable) 1497b55438fdSYinghai Lu { 1498967260cdSYinghai Lu return enable >= user_enabled; 1499b55438fdSYinghai Lu } 1500f483d392SRam Pai 1501b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) 1502ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data) 1503223d96fcSYinghai Lu { 1504b07f2ebcSYinghai Lu int i; 1505223d96fcSYinghai Lu bool *unassigned = data; 1506b07f2ebcSYinghai Lu 1507b07f2ebcSYinghai Lu for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) { 1508b07f2ebcSYinghai Lu struct resource *r = &dev->resource[i]; 1509fa216bf4SYinghai Lu struct pci_bus_region region; 1510b07f2ebcSYinghai Lu 1511223d96fcSYinghai Lu /* Not assigned or rejected by kernel? */ 1512fa216bf4SYinghai Lu if (!r->flags) 1513fa216bf4SYinghai Lu continue; 1514b07f2ebcSYinghai Lu 1515fc279850SYinghai Lu pcibios_resource_to_bus(dev->bus, ®ion, r); 1516fa216bf4SYinghai Lu if (!region.start) { 1517223d96fcSYinghai Lu *unassigned = true; 1518223d96fcSYinghai Lu return 1; /* return early from pci_walk_bus() */ 1519b07f2ebcSYinghai Lu } 1520b07f2ebcSYinghai Lu } 1521b07f2ebcSYinghai Lu 1522223d96fcSYinghai Lu return 0; 1523223d96fcSYinghai Lu } 1524223d96fcSYinghai Lu 1525ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1526967260cdSYinghai Lu enum enable_type enable_local) 1527223d96fcSYinghai Lu { 1528223d96fcSYinghai Lu bool unassigned = false; 1529223d96fcSYinghai Lu 1530967260cdSYinghai Lu if (enable_local != undefined) 1531967260cdSYinghai Lu return enable_local; 1532223d96fcSYinghai Lu 1533223d96fcSYinghai Lu pci_walk_bus(bus, iov_resources_unassigned, &unassigned); 1534967260cdSYinghai Lu if (unassigned) 1535967260cdSYinghai Lu return auto_enabled; 1536967260cdSYinghai Lu 1537967260cdSYinghai Lu return enable_local; 1538b07f2ebcSYinghai Lu } 1539223d96fcSYinghai Lu #else 1540ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1541967260cdSYinghai Lu enum enable_type enable_local) 1542967260cdSYinghai Lu { 1543967260cdSYinghai Lu return enable_local; 1544b07f2ebcSYinghai Lu } 1545b07f2ebcSYinghai Lu #endif 1546b07f2ebcSYinghai Lu 1547da7822e5SYinghai Lu /* 1548da7822e5SYinghai Lu * first try will not touch pci bridge res 1549da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1550f7625980SBjorn Helgaas * will stop till to the max depth if can not find good one 1551da7822e5SYinghai Lu */ 155239772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) 15531da177e4SLinus Torvalds { 1554bdc4abecSYinghai Lu LIST_HEAD(realloc_head); /* list of resources that 1555c8adf9a3SRam Pai want additional resources */ 1556bdc4abecSYinghai Lu struct list_head *add_list = NULL; 1557da7822e5SYinghai Lu int tried_times = 0; 1558da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1559bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1560b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 1561da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 15625b285415SYinghai Lu IORESOURCE_PREFETCH | IORESOURCE_MEM_64; 156319aa7ee4SYinghai Lu int pci_try_num = 1; 156455ed83a6SYinghai Lu enum enable_type enable_local; 1565da7822e5SYinghai Lu 156619aa7ee4SYinghai Lu /* don't realloc if asked to do so */ 156755ed83a6SYinghai Lu enable_local = pci_realloc_detect(bus, pci_realloc_enable); 1568967260cdSYinghai Lu if (pci_realloc_enabled(enable_local)) { 156955ed83a6SYinghai Lu int max_depth = pci_bus_get_depth(bus); 157019aa7ee4SYinghai Lu 1571da7822e5SYinghai Lu pci_try_num = max_depth + 1; 157255ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 157355ed83a6SYinghai Lu "max bus depth: %d pci_try_num: %d\n", 1574da7822e5SYinghai Lu max_depth, pci_try_num); 157519aa7ee4SYinghai Lu } 1576da7822e5SYinghai Lu 1577da7822e5SYinghai Lu again: 157819aa7ee4SYinghai Lu /* 157919aa7ee4SYinghai Lu * last try will use add_list, otherwise will try good to have as 158019aa7ee4SYinghai Lu * must have, so can realloc parent bridge resource 158119aa7ee4SYinghai Lu */ 158219aa7ee4SYinghai Lu if (tried_times + 1 == pci_try_num) 1583bdc4abecSYinghai Lu add_list = &realloc_head; 15841da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 15851da177e4SLinus Torvalds subordinate buses. */ 158619aa7ee4SYinghai Lu __pci_bus_size_bridges(bus, add_list); 1587c8adf9a3SRam Pai 15881da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1589bdc4abecSYinghai Lu __pci_bus_assign_resources(bus, add_list, &fail_head); 159019aa7ee4SYinghai Lu if (add_list) 1591bdc4abecSYinghai Lu BUG_ON(!list_empty(add_list)); 1592da7822e5SYinghai Lu tried_times++; 1593da7822e5SYinghai Lu 1594da7822e5SYinghai Lu /* any device complain? */ 1595bdc4abecSYinghai Lu if (list_empty(&fail_head)) 1596928bea96SYinghai Lu goto dump; 1597f483d392SRam Pai 15980c5be0cbSYinghai Lu if (tried_times >= pci_try_num) { 1599967260cdSYinghai Lu if (enable_local == undefined) 160055ed83a6SYinghai Lu dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n"); 1601967260cdSYinghai Lu else if (enable_local == auto_enabled) 160255ed83a6SYinghai Lu dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); 1603eb572e7cSYinghai Lu 1604bffc56d4SYinghai Lu free_list(&fail_head); 1605928bea96SYinghai Lu goto dump; 1606da7822e5SYinghai Lu } 1607da7822e5SYinghai Lu 160855ed83a6SYinghai Lu dev_printk(KERN_DEBUG, &bus->dev, 160955ed83a6SYinghai Lu "No. %d try to assign unassigned res\n", tried_times + 1); 1610da7822e5SYinghai Lu 1611da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1612da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1613da7822e5SYinghai Lu rel_type = whole_subtree; 1614da7822e5SYinghai Lu 1615da7822e5SYinghai Lu /* 1616da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1617da7822e5SYinghai Lu * child device under that bridge 1618da7822e5SYinghai Lu */ 161961e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 162061e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 1621b9b0bba9SYinghai Lu fail_res->flags & type_mask, 1622da7822e5SYinghai Lu rel_type); 162361e83cddSYinghai Lu 1624da7822e5SYinghai Lu /* restore size and flags */ 1625b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1626b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 1627da7822e5SYinghai Lu 1628b9b0bba9SYinghai Lu res->start = fail_res->start; 1629b9b0bba9SYinghai Lu res->end = fail_res->end; 1630b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1631b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 1632da7822e5SYinghai Lu res->flags = 0; 1633da7822e5SYinghai Lu } 1634bffc56d4SYinghai Lu free_list(&fail_head); 1635da7822e5SYinghai Lu 1636da7822e5SYinghai Lu goto again; 1637da7822e5SYinghai Lu 1638928bea96SYinghai Lu dump: 163976fbc263SYinghai Lu /* dump the resource on buses */ 164076fbc263SYinghai Lu pci_bus_dump_resources(bus); 164176fbc263SYinghai Lu } 16426841ec68SYinghai Lu 164355ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void) 164455ed83a6SYinghai Lu { 164555ed83a6SYinghai Lu struct pci_bus *root_bus; 164655ed83a6SYinghai Lu 164755ed83a6SYinghai Lu list_for_each_entry(root_bus, &pci_root_buses, node) 164855ed83a6SYinghai Lu pci_assign_unassigned_root_bus_resources(root_bus); 164955ed83a6SYinghai Lu } 165055ed83a6SYinghai Lu 16516841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 16526841ec68SYinghai Lu { 16536841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 1654bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 16558424d759SYinghai Lu want additional resources */ 165632180e40SYinghai Lu int tried_times = 0; 1657bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1658b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 16596841ec68SYinghai Lu int retval; 166032180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 166132180e40SYinghai Lu IORESOURCE_PREFETCH; 16626841ec68SYinghai Lu 166332180e40SYinghai Lu again: 16648424d759SYinghai Lu __pci_bus_size_bridges(parent, &add_list); 1665bdc4abecSYinghai Lu __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 1666bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 166732180e40SYinghai Lu tried_times++; 166832180e40SYinghai Lu 1669bdc4abecSYinghai Lu if (list_empty(&fail_head)) 16703f579c34SYinghai Lu goto enable_all; 167132180e40SYinghai Lu 167232180e40SYinghai Lu if (tried_times >= 2) { 167332180e40SYinghai Lu /* still fail, don't need to try more */ 1674bffc56d4SYinghai Lu free_list(&fail_head); 16753f579c34SYinghai Lu goto enable_all; 167632180e40SYinghai Lu } 167732180e40SYinghai Lu 167832180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 167932180e40SYinghai Lu tried_times + 1); 168032180e40SYinghai Lu 168132180e40SYinghai Lu /* 168232180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 168332180e40SYinghai Lu * child device under that bridge 168432180e40SYinghai Lu */ 168561e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 168661e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 168761e83cddSYinghai Lu fail_res->flags & type_mask, 168832180e40SYinghai Lu whole_subtree); 168961e83cddSYinghai Lu 169032180e40SYinghai Lu /* restore size and flags */ 1691b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1692b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 169332180e40SYinghai Lu 1694b9b0bba9SYinghai Lu res->start = fail_res->start; 1695b9b0bba9SYinghai Lu res->end = fail_res->end; 1696b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1697b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 169832180e40SYinghai Lu res->flags = 0; 169932180e40SYinghai Lu } 1700bffc56d4SYinghai Lu free_list(&fail_head); 170132180e40SYinghai Lu 170232180e40SYinghai Lu goto again; 17033f579c34SYinghai Lu 17043f579c34SYinghai Lu enable_all: 17053f579c34SYinghai Lu retval = pci_reenable_device(bridge); 17069fc9eea0SBjorn Helgaas if (retval) 17079fc9eea0SBjorn Helgaas dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval); 17083f579c34SYinghai Lu pci_set_master(bridge); 17096841ec68SYinghai Lu } 17106841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 17119b03088fSYinghai Lu 171217787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus) 17139b03088fSYinghai Lu { 17149b03088fSYinghai Lu struct pci_dev *dev; 1715bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 17169b03088fSYinghai Lu want additional resources */ 17179b03088fSYinghai Lu 17189b03088fSYinghai Lu down_read(&pci_bus_sem); 17199b03088fSYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 17209b03088fSYinghai Lu if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 17219b03088fSYinghai Lu dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 17229b03088fSYinghai Lu if (dev->subordinate) 17239b03088fSYinghai Lu __pci_bus_size_bridges(dev->subordinate, 17249b03088fSYinghai Lu &add_list); 17259b03088fSYinghai Lu up_read(&pci_bus_sem); 17269b03088fSYinghai Lu __pci_bus_assign_resources(bus, &add_list, NULL); 1727bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 172817787940SYinghai Lu } 1729