17328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 3df62ab5eSBjorn Helgaas * Support routines for initializing a PCI subsystem 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Extruded from code written by 61da177e4SLinus Torvalds * Dave Rusling (david.rusling@reo.mts.dec.com) 71da177e4SLinus Torvalds * David Mosberger (davidm@cs.arizona.edu) 81da177e4SLinus Torvalds * David Miller (davem@redhat.com) 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 111da177e4SLinus Torvalds * PCI-PCI bridges cleanup, sorted resource allocation. 121da177e4SLinus Torvalds * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 131da177e4SLinus Torvalds * Converted to allocation in 3 passes, which gives 141da177e4SLinus Torvalds * tighter packing. Prefetchable range support. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/kernel.h> 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/pci.h> 211da177e4SLinus Torvalds #include <linux/errno.h> 221da177e4SLinus Torvalds #include <linux/ioport.h> 231da177e4SLinus Torvalds #include <linux/cache.h> 241da177e4SLinus Torvalds #include <linux/slab.h> 25584c5c42SRui Wang #include <linux/acpi.h> 266faf17f6SChris Wright #include "pci.h" 271da177e4SLinus Torvalds 28844393f4SBjorn Helgaas unsigned int pci_flags; 29*0c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_flags); 3047087700SBjorn Helgaas 31bdc4abecSYinghai Lu struct pci_dev_resource { 32bdc4abecSYinghai Lu struct list_head list; 332934a0deSYinghai Lu struct resource *res; 342934a0deSYinghai Lu struct pci_dev *dev; 35568ddef8SYinghai Lu resource_size_t start; 36568ddef8SYinghai Lu resource_size_t end; 37c8adf9a3SRam Pai resource_size_t add_size; 382bbc6942SRam Pai resource_size_t min_align; 39568ddef8SYinghai Lu unsigned long flags; 40568ddef8SYinghai Lu }; 41568ddef8SYinghai Lu 42bffc56d4SYinghai Lu static void free_list(struct list_head *head) 43bffc56d4SYinghai Lu { 44bffc56d4SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 45bffc56d4SYinghai Lu 46bffc56d4SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 47bffc56d4SYinghai Lu list_del(&dev_res->list); 48bffc56d4SYinghai Lu kfree(dev_res); 49bffc56d4SYinghai Lu } 50bffc56d4SYinghai Lu } 51094732a5SRam Pai 52c8adf9a3SRam Pai /** 530d607618SNicholas Johnson * add_to_list() - Add a new resource tracker to the list 54c8adf9a3SRam Pai * @head: Head of the list 550d607618SNicholas Johnson * @dev: Device to which the resource belongs 560d607618SNicholas Johnson * @res: Resource to be tracked 570d607618SNicholas Johnson * @add_size: Additional size to be optionally added to the resource 58c8adf9a3SRam Pai */ 590d607618SNicholas Johnson static int add_to_list(struct list_head *head, struct pci_dev *dev, 600d607618SNicholas Johnson struct resource *res, resource_size_t add_size, 610d607618SNicholas Johnson resource_size_t min_align) 62568ddef8SYinghai Lu { 63764242a0SYinghai Lu struct pci_dev_resource *tmp; 64568ddef8SYinghai Lu 65bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 66c7abb235SMarkus Elfring if (!tmp) 67ef62dfefSYinghai Lu return -ENOMEM; 68568ddef8SYinghai Lu 69568ddef8SYinghai Lu tmp->res = res; 70568ddef8SYinghai Lu tmp->dev = dev; 71568ddef8SYinghai Lu tmp->start = res->start; 72568ddef8SYinghai Lu tmp->end = res->end; 73568ddef8SYinghai Lu tmp->flags = res->flags; 74c8adf9a3SRam Pai tmp->add_size = add_size; 752bbc6942SRam Pai tmp->min_align = min_align; 76bdc4abecSYinghai Lu 77bdc4abecSYinghai Lu list_add(&tmp->list, head); 78ef62dfefSYinghai Lu 79ef62dfefSYinghai Lu return 0; 80568ddef8SYinghai Lu } 81568ddef8SYinghai Lu 820d607618SNicholas Johnson static void remove_from_list(struct list_head *head, struct resource *res) 833e6e0d80SYinghai Lu { 84b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 853e6e0d80SYinghai Lu 86b9b0bba9SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 87b9b0bba9SYinghai Lu if (dev_res->res == res) { 88b9b0bba9SYinghai Lu list_del(&dev_res->list); 89b9b0bba9SYinghai Lu kfree(dev_res); 90bdc4abecSYinghai Lu break; 913e6e0d80SYinghai Lu } 923e6e0d80SYinghai Lu } 933e6e0d80SYinghai Lu } 943e6e0d80SYinghai Lu 95d74b9027SWei Yang static struct pci_dev_resource *res_to_dev_res(struct list_head *head, 961c372353SYinghai Lu struct resource *res) 971c372353SYinghai Lu { 98b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res; 991c372353SYinghai Lu 100b9b0bba9SYinghai Lu list_for_each_entry(dev_res, head, list) { 10125e77388SBjorn Helgaas if (dev_res->res == res) 102d74b9027SWei Yang return dev_res; 103bdc4abecSYinghai Lu } 1041c372353SYinghai Lu 105d74b9027SWei Yang return NULL; 1061c372353SYinghai Lu } 1071c372353SYinghai Lu 108d74b9027SWei Yang static resource_size_t get_res_add_size(struct list_head *head, 109d74b9027SWei Yang struct resource *res) 110d74b9027SWei Yang { 111d74b9027SWei Yang struct pci_dev_resource *dev_res; 112d74b9027SWei Yang 113d74b9027SWei Yang dev_res = res_to_dev_res(head, res); 114d74b9027SWei Yang return dev_res ? dev_res->add_size : 0; 115d74b9027SWei Yang } 116d74b9027SWei Yang 117d74b9027SWei Yang static resource_size_t get_res_add_align(struct list_head *head, 118d74b9027SWei Yang struct resource *res) 119d74b9027SWei Yang { 120d74b9027SWei Yang struct pci_dev_resource *dev_res; 121d74b9027SWei Yang 122d74b9027SWei Yang dev_res = res_to_dev_res(head, res); 123d74b9027SWei Yang return dev_res ? dev_res->min_align : 0; 124d74b9027SWei Yang } 125d74b9027SWei Yang 126d74b9027SWei Yang 12778c3b329SYinghai Lu /* Sort resources by alignment */ 128bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 12978c3b329SYinghai Lu { 13078c3b329SYinghai Lu int i; 13178c3b329SYinghai Lu 13278c3b329SYinghai Lu for (i = 0; i < PCI_NUM_RESOURCES; i++) { 13378c3b329SYinghai Lu struct resource *r; 134bdc4abecSYinghai Lu struct pci_dev_resource *dev_res, *tmp; 13578c3b329SYinghai Lu resource_size_t r_align; 136bdc4abecSYinghai Lu struct list_head *n; 13778c3b329SYinghai Lu 13878c3b329SYinghai Lu r = &dev->resource[i]; 13978c3b329SYinghai Lu 14078c3b329SYinghai Lu if (r->flags & IORESOURCE_PCI_FIXED) 14178c3b329SYinghai Lu continue; 14278c3b329SYinghai Lu 14378c3b329SYinghai Lu if (!(r->flags) || r->parent) 14478c3b329SYinghai Lu continue; 14578c3b329SYinghai Lu 14678c3b329SYinghai Lu r_align = pci_resource_alignment(dev, r); 14778c3b329SYinghai Lu if (!r_align) { 1487506dc79SFrederick Lawler pci_warn(dev, "BAR %d: %pR has bogus alignment\n", 14978c3b329SYinghai Lu i, r); 15078c3b329SYinghai Lu continue; 15178c3b329SYinghai Lu } 15278c3b329SYinghai Lu 153bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 15478c3b329SYinghai Lu if (!tmp) 155227f0647SRyan Desfosses panic("pdev_sort_resources(): kmalloc() failed!\n"); 15678c3b329SYinghai Lu tmp->res = r; 15778c3b329SYinghai Lu tmp->dev = dev; 158bdc4abecSYinghai Lu 1590d607618SNicholas Johnson /* Fallback is smallest one or list is empty */ 160bdc4abecSYinghai Lu n = head; 161bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 162bdc4abecSYinghai Lu resource_size_t align; 163bdc4abecSYinghai Lu 164bdc4abecSYinghai Lu align = pci_resource_alignment(dev_res->dev, 165bdc4abecSYinghai Lu dev_res->res); 166bdc4abecSYinghai Lu 167bdc4abecSYinghai Lu if (r_align > align) { 168bdc4abecSYinghai Lu n = &dev_res->list; 16978c3b329SYinghai Lu break; 17078c3b329SYinghai Lu } 17178c3b329SYinghai Lu } 172bdc4abecSYinghai Lu /* Insert it just before n */ 173bdc4abecSYinghai Lu list_add_tail(&tmp->list, n); 17478c3b329SYinghai Lu } 17578c3b329SYinghai Lu } 17678c3b329SYinghai Lu 1770d607618SNicholas Johnson static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds u16 class = dev->class >> 8; 1801da177e4SLinus Torvalds 1810d607618SNicholas Johnson /* Don't touch classless devices or host bridges or IOAPICs */ 1826841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1836841ec68SYinghai Lu return; 1841da177e4SLinus Torvalds 1850d607618SNicholas Johnson /* Don't touch IOAPIC devices already enabled by firmware */ 18623186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 1879bded00bSKenji Kaneshige u16 command; 1889bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 1899bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 1906841ec68SYinghai Lu return; 19123186279SSatoru Takeuchi } 19223186279SSatoru Takeuchi 1936841ec68SYinghai Lu pdev_sort_resources(dev, head); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 196fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 197fc075e1dSRam Pai { 198fc075e1dSRam Pai res->start = 0; 199fc075e1dSRam Pai res->end = 0; 200fc075e1dSRam Pai res->flags = 0; 201fc075e1dSRam Pai } 202fc075e1dSRam Pai 203c8adf9a3SRam Pai /** 2040d607618SNicholas Johnson * reassign_resources_sorted() - Satisfy any additional resource requests 205c8adf9a3SRam Pai * 2060d607618SNicholas Johnson * @realloc_head: Head of the list tracking requests requiring 2070d607618SNicholas Johnson * additional resources 2080d607618SNicholas Johnson * @head: Head of the list tracking requests with allocated 209c8adf9a3SRam Pai * resources 210c8adf9a3SRam Pai * 2110d607618SNicholas Johnson * Walk through each element of the realloc_head and try to procure additional 2120d607618SNicholas Johnson * resources for the element, provided the element is in the head list. 213c8adf9a3SRam Pai */ 214bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head, 215bdc4abecSYinghai Lu struct list_head *head) 216c8adf9a3SRam Pai { 217c8adf9a3SRam Pai struct resource *res; 218b9b0bba9SYinghai Lu struct pci_dev_resource *add_res, *tmp; 219bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 220d74b9027SWei Yang resource_size_t add_size, align; 221c8adf9a3SRam Pai int idx; 222c8adf9a3SRam Pai 223b9b0bba9SYinghai Lu list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 224bdc4abecSYinghai Lu bool found_match = false; 225bdc4abecSYinghai Lu 226b9b0bba9SYinghai Lu res = add_res->res; 2270d607618SNicholas Johnson /* Skip resource that has been reset */ 228c8adf9a3SRam Pai if (!res->flags) 229c8adf9a3SRam Pai goto out; 230c8adf9a3SRam Pai 2310d607618SNicholas Johnson /* Skip this resource if not found in head list */ 232bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 233bdc4abecSYinghai Lu if (dev_res->res == res) { 234bdc4abecSYinghai Lu found_match = true; 235bdc4abecSYinghai Lu break; 236c8adf9a3SRam Pai } 237bdc4abecSYinghai Lu } 2380d607618SNicholas Johnson if (!found_match) /* Just skip */ 239bdc4abecSYinghai Lu continue; 240c8adf9a3SRam Pai 241b9b0bba9SYinghai Lu idx = res - &add_res->dev->resource[0]; 242b9b0bba9SYinghai Lu add_size = add_res->add_size; 243d74b9027SWei Yang align = add_res->min_align; 2442bbc6942SRam Pai if (!resource_size(res)) { 245d74b9027SWei Yang res->start = align; 246c8adf9a3SRam Pai res->end = res->start + add_size - 1; 247b9b0bba9SYinghai Lu if (pci_assign_resource(add_res->dev, idx)) 248c8adf9a3SRam Pai reset_resource(res); 2492bbc6942SRam Pai } else { 250b9b0bba9SYinghai Lu res->flags |= add_res->flags & 251bdc4abecSYinghai Lu (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 252b9b0bba9SYinghai Lu if (pci_reassign_resource(add_res->dev, idx, 253bdc4abecSYinghai Lu add_size, align)) 25434c6b710SMohan Kumar pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n", 25534c6b710SMohan Kumar (unsigned long long) add_size, idx, 25634c6b710SMohan Kumar 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 /** 2650d607618SNicholas Johnson * assign_requested_resources_sorted() - Satisfy resource requests 266c8adf9a3SRam Pai * 2670d607618SNicholas Johnson * @head: Head of the list tracking requests for resources 2680d607618SNicholas Johnson * @fail_head: Head of the list tracking requests that could not be 2690d607618SNicholas Johnson * allocated 270c8adf9a3SRam Pai * 2710d607618SNicholas Johnson * Satisfy resource requests of each element in the list. Add requests that 2720d607618SNicholas Johnson * could not be 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 /* 2880d607618SNicholas Johnson * If the failed resource is a ROM BAR and 2890d607618SNicholas Johnson * it will be enabled later, don't add it 2900d607618SNicholas Johnson * to the list. 2919a928660SYinghai Lu */ 2929a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 2939a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 29467cc7e26SYinghai Lu add_to_list(fail_head, 29567cc7e26SYinghai Lu dev_res->dev, res, 296f7625980SBjorn Helgaas 0 /* don't care */, 297f7625980SBjorn Helgaas 0 /* don't care */); 2989a928660SYinghai Lu } 299fc075e1dSRam Pai reset_resource(res); 300542df5deSRajesh Shah } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 304aa914f5eSYinghai Lu static unsigned long pci_fail_res_type_mask(struct list_head *fail_head) 305aa914f5eSYinghai Lu { 306aa914f5eSYinghai Lu struct pci_dev_resource *fail_res; 307aa914f5eSYinghai Lu unsigned long mask = 0; 308aa914f5eSYinghai Lu 3090d607618SNicholas Johnson /* Check failed type */ 310aa914f5eSYinghai Lu list_for_each_entry(fail_res, fail_head, list) 311aa914f5eSYinghai Lu mask |= fail_res->flags; 312aa914f5eSYinghai Lu 313aa914f5eSYinghai Lu /* 3140d607618SNicholas Johnson * One pref failed resource will set IORESOURCE_MEM, as we can 3150d607618SNicholas Johnson * allocate pref in non-pref range. Will release all assigned 3160d607618SNicholas Johnson * non-pref sibling resources 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 3260d607618SNicholas Johnson /* Check pref at first */ 327aa914f5eSYinghai Lu if (res->flags & IORESOURCE_PREFETCH) { 328aa914f5eSYinghai Lu if (mask & IORESOURCE_PREFETCH) 329aa914f5eSYinghai Lu return true; 3300d607618SNicholas Johnson /* 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 3410d607618SNicholas Johnson 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 /* 3490d607618SNicholas Johnson * Should not assign requested resources at first. They could be 3500d607618SNicholas Johnson * adjacent, so later reassign can not reallocate them one by one in 3510d607618SNicholas Johnson * parent resource window. 3520d607618SNicholas Johnson * 3530d607618SNicholas Johnson * Try to assign requested + add_size at beginning. If could do that, 3540d607618SNicholas Johnson * could get out early. If could not do that, we still try to assign 3550d607618SNicholas Johnson * requested at first, 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. 3590d607618SNicholas Johnson * 3600d607618SNicholas Johnson * 1. If IO port assignment fails, will release assigned IO 3610d607618SNicholas Johnson * port. 3620d607618SNicholas Johnson * 2. If pref MMIO assignment fails, release assigned pref 3630d607618SNicholas Johnson * MMIO. If assigned pref MMIO's parent is non-pref MMIO 3640d607618SNicholas Johnson * and non-pref MMIO assignment fails, will release that 3650d607618SNicholas Johnson * assigned pref MMIO. 3660d607618SNicholas Johnson * 3. If non-pref MMIO assignment fails or pref MMIO 3670d607618SNicholas Johnson * assignment fails, 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; 372d74b9027SWei Yang struct pci_dev_resource *dev_res, *tmp_res, *dev_res2; 373aa914f5eSYinghai Lu unsigned long fail_type; 374d74b9027SWei Yang resource_size_t add_align, align; 3753e6e0d80SYinghai Lu 3763e6e0d80SYinghai Lu /* Check if optional add_size is there */ 377bdc4abecSYinghai Lu if (!realloc_head || list_empty(realloc_head)) 3783e6e0d80SYinghai Lu goto requested_and_reassign; 3793e6e0d80SYinghai Lu 3803e6e0d80SYinghai Lu /* Save original start, end, flags etc at first */ 381bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 382bdc4abecSYinghai Lu if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 383bffc56d4SYinghai Lu free_list(&save_head); 3843e6e0d80SYinghai Lu goto requested_and_reassign; 3853e6e0d80SYinghai Lu } 386bdc4abecSYinghai Lu } 3873e6e0d80SYinghai Lu 3883e6e0d80SYinghai Lu /* Update res in head list with add_size in realloc_head list */ 389d74b9027SWei Yang list_for_each_entry_safe(dev_res, tmp_res, head, list) { 390bdc4abecSYinghai Lu dev_res->res->end += get_res_add_size(realloc_head, 391bdc4abecSYinghai Lu dev_res->res); 3923e6e0d80SYinghai Lu 393d74b9027SWei Yang /* 394d74b9027SWei Yang * There are two kinds of additional resources in the list: 395d74b9027SWei Yang * 1. bridge resource -- IORESOURCE_STARTALIGN 396d74b9027SWei Yang * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN 397d74b9027SWei Yang * Here just fix the additional alignment for bridge 398d74b9027SWei Yang */ 399d74b9027SWei Yang if (!(dev_res->res->flags & IORESOURCE_STARTALIGN)) 400d74b9027SWei Yang continue; 401d74b9027SWei Yang 402d74b9027SWei Yang add_align = get_res_add_align(realloc_head, dev_res->res); 403d74b9027SWei Yang 404d74b9027SWei Yang /* 4050d607618SNicholas Johnson * The "head" list is sorted by alignment so resources with 4060d607618SNicholas Johnson * bigger alignment will be assigned first. After we 4070d607618SNicholas Johnson * change the alignment of a dev_res in "head" list, we 4080d607618SNicholas Johnson * need to reorder the list by alignment to make it 409d74b9027SWei Yang * consistent. 410d74b9027SWei Yang */ 411d74b9027SWei Yang if (add_align > dev_res->res->start) { 412552bc94eSYinghai Lu resource_size_t r_size = resource_size(dev_res->res); 413552bc94eSYinghai Lu 414d74b9027SWei Yang dev_res->res->start = add_align; 415552bc94eSYinghai Lu dev_res->res->end = add_align + r_size - 1; 416d74b9027SWei Yang 417d74b9027SWei Yang list_for_each_entry(dev_res2, head, list) { 418d74b9027SWei Yang align = pci_resource_alignment(dev_res2->dev, 419d74b9027SWei Yang dev_res2->res); 420a6b65983SWei Yang if (add_align > align) { 421d74b9027SWei Yang list_move_tail(&dev_res->list, 422d74b9027SWei Yang &dev_res2->list); 423a6b65983SWei Yang break; 424a6b65983SWei Yang } 425d74b9027SWei Yang } 426d74b9027SWei Yang } 427d74b9027SWei Yang 428d74b9027SWei Yang } 429d74b9027SWei Yang 4303e6e0d80SYinghai Lu /* Try updated head list with add_size added */ 4313e6e0d80SYinghai Lu assign_requested_resources_sorted(head, &local_fail_head); 4323e6e0d80SYinghai Lu 4330d607618SNicholas Johnson /* All assigned with add_size? */ 434bdc4abecSYinghai Lu if (list_empty(&local_fail_head)) { 4353e6e0d80SYinghai Lu /* Remove head list from realloc_head list */ 436bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 437bdc4abecSYinghai Lu remove_from_list(realloc_head, dev_res->res); 438bffc56d4SYinghai Lu free_list(&save_head); 439bffc56d4SYinghai Lu free_list(head); 4403e6e0d80SYinghai Lu return; 4413e6e0d80SYinghai Lu } 4423e6e0d80SYinghai Lu 4430d607618SNicholas Johnson /* Check failed type */ 444aa914f5eSYinghai Lu fail_type = pci_fail_res_type_mask(&local_fail_head); 4450d607618SNicholas Johnson /* Remove not need to be released assigned res from head list etc */ 446aa914f5eSYinghai Lu list_for_each_entry_safe(dev_res, tmp_res, head, list) 447aa914f5eSYinghai Lu if (dev_res->res->parent && 448aa914f5eSYinghai Lu !pci_need_to_release(fail_type, dev_res->res)) { 4490d607618SNicholas Johnson /* Remove it from realloc_head list */ 450aa914f5eSYinghai Lu remove_from_list(realloc_head, dev_res->res); 451aa914f5eSYinghai Lu remove_from_list(&save_head, dev_res->res); 452aa914f5eSYinghai Lu list_del(&dev_res->list); 453aa914f5eSYinghai Lu kfree(dev_res); 454aa914f5eSYinghai Lu } 455aa914f5eSYinghai Lu 456bffc56d4SYinghai Lu free_list(&local_fail_head); 4573e6e0d80SYinghai Lu /* Release assigned resource */ 458bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 459bdc4abecSYinghai Lu if (dev_res->res->parent) 460bdc4abecSYinghai Lu release_resource(dev_res->res); 4613e6e0d80SYinghai Lu /* Restore start/end/flags from saved list */ 462b9b0bba9SYinghai Lu list_for_each_entry(save_res, &save_head, list) { 463b9b0bba9SYinghai Lu struct resource *res = save_res->res; 4643e6e0d80SYinghai Lu 465b9b0bba9SYinghai Lu res->start = save_res->start; 466b9b0bba9SYinghai Lu res->end = save_res->end; 467b9b0bba9SYinghai Lu res->flags = save_res->flags; 4683e6e0d80SYinghai Lu } 469bffc56d4SYinghai Lu free_list(&save_head); 4703e6e0d80SYinghai Lu 4713e6e0d80SYinghai Lu requested_and_reassign: 472c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 473c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 474c8adf9a3SRam Pai 4750d607618SNicholas Johnson /* Try to satisfy any additional optional resource requests */ 4769e8bf93aSRam Pai if (realloc_head) 4779e8bf93aSRam Pai reassign_resources_sorted(realloc_head, head); 478bffc56d4SYinghai Lu free_list(head); 479c8adf9a3SRam Pai } 480c8adf9a3SRam Pai 4816841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 482bdc4abecSYinghai Lu struct list_head *add_head, 483bdc4abecSYinghai Lu struct list_head *fail_head) 4846841ec68SYinghai Lu { 485bdc4abecSYinghai Lu LIST_HEAD(head); 4866841ec68SYinghai Lu 4876841ec68SYinghai Lu __dev_sort_resources(dev, &head); 4888424d759SYinghai Lu __assign_resources_sorted(&head, add_head, fail_head); 4896841ec68SYinghai Lu 4906841ec68SYinghai Lu } 4916841ec68SYinghai Lu 4926841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 493bdc4abecSYinghai Lu struct list_head *realloc_head, 494bdc4abecSYinghai Lu struct list_head *fail_head) 4956841ec68SYinghai Lu { 4966841ec68SYinghai Lu struct pci_dev *dev; 497bdc4abecSYinghai Lu LIST_HEAD(head); 4986841ec68SYinghai Lu 4996841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 5006841ec68SYinghai Lu __dev_sort_resources(dev, &head); 5016841ec68SYinghai Lu 5029e8bf93aSRam Pai __assign_resources_sorted(&head, realloc_head, fail_head); 5036841ec68SYinghai Lu } 5046841ec68SYinghai Lu 505b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 5061da177e4SLinus Torvalds { 5071da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 508c7dabef8SBjorn Helgaas struct resource *res; 5091da177e4SLinus Torvalds struct pci_bus_region region; 5101da177e4SLinus Torvalds 5117506dc79SFrederick Lawler pci_info(bridge, "CardBus bridge to %pR\n", 512b918c62eSYinghai Lu &bus->busn_res); 5131da177e4SLinus Torvalds 514c7dabef8SBjorn Helgaas res = bus->resource[0]; 515fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 516c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5171da177e4SLinus Torvalds /* 5181da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 5191da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 5201da177e4SLinus Torvalds */ 5217506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 5221da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 5231da177e4SLinus Torvalds region.start); 5241da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 5251da177e4SLinus Torvalds region.end); 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 528c7dabef8SBjorn Helgaas res = bus->resource[1]; 529fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 530c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5317506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 5321da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 5331da177e4SLinus Torvalds region.start); 5341da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 5351da177e4SLinus Torvalds region.end); 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 538c7dabef8SBjorn Helgaas res = bus->resource[2]; 539fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 540c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 5417506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 5421da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 5431da177e4SLinus Torvalds region.start); 5441da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 5451da177e4SLinus Torvalds region.end); 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 548c7dabef8SBjorn Helgaas res = bus->resource[3]; 549fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 550c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 5517506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 5521da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 5531da177e4SLinus Torvalds region.start); 5541da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 5551da177e4SLinus Torvalds region.end); 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds } 558b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 5591da177e4SLinus Torvalds 5600d607618SNicholas Johnson /* 5610d607618SNicholas Johnson * Initialize bridges with base/limit values we have collected. PCI-to-PCI 5620d607618SNicholas Johnson * Bridge Architecture Specification rev. 1.1 (1998) requires that if there 5630d607618SNicholas Johnson * are no I/O ports or memory behind the bridge, the corresponding range 5640d607618SNicholas Johnson * must be turned off by writing base value greater than limit to the 5650d607618SNicholas Johnson * bridge's base/limit registers. 5660d607618SNicholas Johnson * 5670d607618SNicholas Johnson * Note: care must be taken when updating I/O base/limit registers of 5680d607618SNicholas Johnson * bridges which support 32-bit I/O. This update requires two config space 5690d607618SNicholas Johnson * writes, so it's quite possible that an I/O window of the bridge will 5700d607618SNicholas Johnson * have some undesirable address (e.g. 0) after the first write. Ditto 5710d607618SNicholas Johnson * 64-bit prefetchable MMIO. 5720d607618SNicholas Johnson */ 5733f2f4dc4SYinghai Lu static void pci_setup_bridge_io(struct pci_dev *bridge) 5741da177e4SLinus Torvalds { 575c7dabef8SBjorn Helgaas struct resource *res; 5761da177e4SLinus Torvalds struct pci_bus_region region; 5772b28ae19SBjorn Helgaas unsigned long io_mask; 5782b28ae19SBjorn Helgaas u8 io_base_lo, io_limit_lo; 5795b764b83SBjorn Helgaas u16 l; 5805b764b83SBjorn Helgaas u32 io_upper16; 5811da177e4SLinus Torvalds 5822b28ae19SBjorn Helgaas io_mask = PCI_IO_RANGE_MASK; 5832b28ae19SBjorn Helgaas if (bridge->io_window_1k) 5842b28ae19SBjorn Helgaas io_mask = PCI_IO_1K_RANGE_MASK; 5852b28ae19SBjorn Helgaas 5860d607618SNicholas Johnson /* Set up the top and bottom of the PCI I/O segment for this bus */ 5873f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0]; 588fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 589c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 5905b764b83SBjorn Helgaas pci_read_config_word(bridge, PCI_IO_BASE, &l); 5912b28ae19SBjorn Helgaas io_base_lo = (region.start >> 8) & io_mask; 5922b28ae19SBjorn Helgaas io_limit_lo = (region.end >> 8) & io_mask; 5935b764b83SBjorn Helgaas l = ((u16) io_limit_lo << 8) | io_base_lo; 5940d607618SNicholas Johnson /* Set up upper 16 bits of I/O base/limit */ 5951da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 5967506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 5977cc5997dSYinghai Lu } else { 5980d607618SNicholas Johnson /* Clear upper 16 bits of I/O base/limit */ 5991da177e4SLinus Torvalds io_upper16 = 0; 6001da177e4SLinus Torvalds l = 0x00f0; 6011da177e4SLinus Torvalds } 6020d607618SNicholas Johnson /* Temporarily disable the I/O range before updating PCI_IO_BASE */ 6031da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 6040d607618SNicholas Johnson /* Update lower 16 bits of I/O base/limit */ 6055b764b83SBjorn Helgaas pci_write_config_word(bridge, PCI_IO_BASE, l); 6060d607618SNicholas Johnson /* Update upper 16 bits of I/O base/limit */ 6071da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 6087cc5997dSYinghai Lu } 6091da177e4SLinus Torvalds 6103f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio(struct pci_dev *bridge) 6117cc5997dSYinghai Lu { 6127cc5997dSYinghai Lu struct resource *res; 6137cc5997dSYinghai Lu struct pci_bus_region region; 6147cc5997dSYinghai Lu u32 l; 6157cc5997dSYinghai Lu 6160d607618SNicholas Johnson /* Set up the top and bottom of the PCI Memory segment for this bus */ 6173f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1]; 618fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 619c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 6201da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6211da177e4SLinus Torvalds l |= region.end & 0xfff00000; 6227506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 6237cc5997dSYinghai Lu } else { 6241da177e4SLinus Torvalds l = 0x0000fff0; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 6277cc5997dSYinghai Lu } 6287cc5997dSYinghai Lu 6293f2f4dc4SYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge) 6307cc5997dSYinghai Lu { 6317cc5997dSYinghai Lu struct resource *res; 6327cc5997dSYinghai Lu struct pci_bus_region region; 6337cc5997dSYinghai Lu u32 l, bu, lu; 6341da177e4SLinus Torvalds 6350d607618SNicholas Johnson /* 6360d607618SNicholas Johnson * Clear out the upper 32 bits of PREF limit. If 6370d607618SNicholas Johnson * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables 6380d607618SNicholas Johnson * PREF range, which is ok. 6390d607618SNicholas Johnson */ 6401da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 6411da177e4SLinus Torvalds 6420d607618SNicholas Johnson /* Set up PREF base/limit */ 643c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 6443f2f4dc4SYinghai Lu res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2]; 645fc279850SYinghai Lu pcibios_resource_to_bus(bridge->bus, ®ion, res); 646c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 6471da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 6481da177e4SLinus Torvalds l |= region.end & 0xfff00000; 649c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 65013d36c24SAndrew Morton bu = upper_32_bits(region.start); 65113d36c24SAndrew Morton lu = upper_32_bits(region.end); 6521f82de10SYinghai Lu } 6537506dc79SFrederick Lawler pci_info(bridge, " bridge window %pR\n", res); 6547cc5997dSYinghai Lu } else { 6551da177e4SLinus Torvalds l = 0x0000fff0; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 6581da177e4SLinus Torvalds 6590d607618SNicholas Johnson /* Set the upper 32 bits of PREF base & limit */ 660c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 661c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 6627cc5997dSYinghai Lu } 6637cc5997dSYinghai Lu 6647cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 6657cc5997dSYinghai Lu { 6667cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 6677cc5997dSYinghai Lu 6687506dc79SFrederick Lawler pci_info(bridge, "PCI bridge to %pR\n", 669b918c62eSYinghai Lu &bus->busn_res); 6707cc5997dSYinghai Lu 6717cc5997dSYinghai Lu if (type & IORESOURCE_IO) 6723f2f4dc4SYinghai Lu pci_setup_bridge_io(bridge); 6737cc5997dSYinghai Lu 6747cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 6753f2f4dc4SYinghai Lu pci_setup_bridge_mmio(bridge); 6767cc5997dSYinghai Lu 6777cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 6783f2f4dc4SYinghai Lu pci_setup_bridge_mmio_pref(bridge); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 683d366d28cSGavin Shan void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type) 684d366d28cSGavin Shan { 685d366d28cSGavin Shan } 686d366d28cSGavin Shan 687e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 6887cc5997dSYinghai Lu { 6897cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 6907cc5997dSYinghai Lu IORESOURCE_PREFETCH; 6917cc5997dSYinghai Lu 692d366d28cSGavin Shan pcibios_setup_bridge(bus, type); 6937cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 6947cc5997dSYinghai Lu } 6957cc5997dSYinghai Lu 6968505e729SYinghai Lu 6978505e729SYinghai Lu int pci_claim_bridge_resource(struct pci_dev *bridge, int i) 6988505e729SYinghai Lu { 6998505e729SYinghai Lu if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END) 7008505e729SYinghai Lu return 0; 7018505e729SYinghai Lu 7028505e729SYinghai Lu if (pci_claim_resource(bridge, i) == 0) 7030d607618SNicholas Johnson return 0; /* Claimed the window */ 7048505e729SYinghai Lu 7058505e729SYinghai Lu if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI) 7068505e729SYinghai Lu return 0; 7078505e729SYinghai Lu 7088505e729SYinghai Lu if (!pci_bus_clip_resource(bridge, i)) 7090d607618SNicholas Johnson return -EINVAL; /* Clipping didn't change anything */ 7108505e729SYinghai Lu 7118505e729SYinghai Lu switch (i - PCI_BRIDGE_RESOURCES) { 7128505e729SYinghai Lu case 0: 7138505e729SYinghai Lu pci_setup_bridge_io(bridge); 7148505e729SYinghai Lu break; 7158505e729SYinghai Lu case 1: 7168505e729SYinghai Lu pci_setup_bridge_mmio(bridge); 7178505e729SYinghai Lu break; 7188505e729SYinghai Lu case 2: 7198505e729SYinghai Lu pci_setup_bridge_mmio_pref(bridge); 7208505e729SYinghai Lu break; 7218505e729SYinghai Lu default: 7228505e729SYinghai Lu return -EINVAL; 7238505e729SYinghai Lu } 7248505e729SYinghai Lu 7258505e729SYinghai Lu if (pci_claim_resource(bridge, i) == 0) 7260d607618SNicholas Johnson return 0; /* Claimed a smaller window */ 7278505e729SYinghai Lu 7288505e729SYinghai Lu return -EINVAL; 7298505e729SYinghai Lu } 7308505e729SYinghai Lu 7310d607618SNicholas Johnson /* 7320d607618SNicholas Johnson * Check whether the bridge supports optional I/O and prefetchable memory 7330d607618SNicholas Johnson * ranges. If not, the respective base/limit registers must be read-only 7340d607618SNicholas Johnson * and read as 0. 7350d607618SNicholas Johnson */ 73696bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 73951c48b31SBjorn Helgaas struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 7421da177e4SLinus Torvalds 74351c48b31SBjorn Helgaas if (bridge->io_window) 7441da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 745d2f54d9bSBjorn Helgaas 74651c48b31SBjorn Helgaas if (bridge->pref_window) { 7471da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 74851c48b31SBjorn Helgaas if (bridge->pref_64_window) { 7491f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 75099586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 75199586105SYinghai Lu } 7521f82de10SYinghai Lu } 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7550d607618SNicholas Johnson /* 756c13704f5SNicholas Johnson * Helper function for sizing routines. Assigned resources have non-NULL 757c13704f5SNicholas Johnson * parent resource. 758c13704f5SNicholas Johnson * 759c13704f5SNicholas Johnson * Return first unassigned resource of the correct type. If there is none, 760c13704f5SNicholas Johnson * return first assigned resource of the correct type. If none of the 761c13704f5SNicholas Johnson * above, return NULL. 762c13704f5SNicholas Johnson * 763c13704f5SNicholas Johnson * Returning an assigned resource of the correct type allows the caller to 764c13704f5SNicholas Johnson * distinguish between already assigned and no resource of the correct type. 7650d607618SNicholas Johnson */ 766c13704f5SNicholas Johnson static struct resource *find_bus_resource_of_type(struct pci_bus *bus, 7670d607618SNicholas Johnson unsigned long type_mask, 7680d607618SNicholas Johnson unsigned long type) 7691da177e4SLinus Torvalds { 770c13704f5SNicholas Johnson struct resource *r, *r_assigned = NULL; 7711da177e4SLinus Torvalds int i; 7721da177e4SLinus Torvalds 77389a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 774299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 775299de034SIvan Kokshaysky continue; 77655a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 7771da177e4SLinus Torvalds return r; 778c13704f5SNicholas Johnson if (r && (r->flags & type_mask) == type && !r_assigned) 779c13704f5SNicholas Johnson r_assigned = r; 7801da177e4SLinus Torvalds } 781c13704f5SNicholas Johnson return r_assigned; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 78413583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 78513583b16SRam Pai resource_size_t min_size, 78613583b16SRam Pai resource_size_t size1, 787de3ffa30SJon Derrick resource_size_t add_size, 788de3ffa30SJon Derrick resource_size_t children_add_size, 78913583b16SRam Pai resource_size_t old_size, 79013583b16SRam Pai resource_size_t align) 79113583b16SRam Pai { 79213583b16SRam Pai if (size < min_size) 79313583b16SRam Pai size = min_size; 79413583b16SRam Pai if (old_size == 1) 79513583b16SRam Pai old_size = 0; 7960d607618SNicholas Johnson /* 7970d607618SNicholas Johnson * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the 7980d607618SNicholas Johnson * struct pci_bus. 7990d607618SNicholas Johnson */ 80013583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 80113583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 80213583b16SRam Pai #endif 803de3ffa30SJon Derrick size = size + size1; 80413583b16SRam Pai if (size < old_size) 80513583b16SRam Pai size = old_size; 806de3ffa30SJon Derrick 807de3ffa30SJon Derrick size = ALIGN(max(size, add_size) + children_add_size, align); 80813583b16SRam Pai return size; 80913583b16SRam Pai } 81013583b16SRam Pai 81113583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 81213583b16SRam Pai resource_size_t min_size, 813de3ffa30SJon Derrick resource_size_t add_size, 814de3ffa30SJon Derrick resource_size_t children_add_size, 81513583b16SRam Pai resource_size_t old_size, 81613583b16SRam Pai resource_size_t align) 81713583b16SRam Pai { 81813583b16SRam Pai if (size < min_size) 81913583b16SRam Pai size = min_size; 82013583b16SRam Pai if (old_size == 1) 82113583b16SRam Pai old_size = 0; 82213583b16SRam Pai if (size < old_size) 82313583b16SRam Pai size = old_size; 824de3ffa30SJon Derrick 825de3ffa30SJon Derrick size = ALIGN(max(size, add_size) + children_add_size, align); 82613583b16SRam Pai return size; 82713583b16SRam Pai } 82813583b16SRam Pai 829ac5ad93eSGavin Shan resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, 830ac5ad93eSGavin Shan unsigned long type) 831ac5ad93eSGavin Shan { 832ac5ad93eSGavin Shan return 1; 833ac5ad93eSGavin Shan } 834ac5ad93eSGavin Shan 835ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */ 836ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */ 837ac5ad93eSGavin Shan #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */ 838ac5ad93eSGavin Shan 8390d607618SNicholas Johnson static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type) 840ac5ad93eSGavin Shan { 841ac5ad93eSGavin Shan resource_size_t align = 1, arch_align; 842ac5ad93eSGavin Shan 843ac5ad93eSGavin Shan if (type & IORESOURCE_MEM) 844ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_MEM_ALIGN; 845ac5ad93eSGavin Shan else if (type & IORESOURCE_IO) { 846ac5ad93eSGavin Shan /* 8470d607618SNicholas Johnson * Per spec, I/O windows are 4K-aligned, but some bridges have 8480d607618SNicholas Johnson * an extension to support 1K alignment. 849ac5ad93eSGavin Shan */ 8502c8d5a2dSIvan Kokshaysky if (bus->self && bus->self->io_window_1k) 851ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN_1K; 852ac5ad93eSGavin Shan else 853ac5ad93eSGavin Shan align = PCI_P2P_DEFAULT_IO_ALIGN; 854ac5ad93eSGavin Shan } 855ac5ad93eSGavin Shan 856ac5ad93eSGavin Shan arch_align = pcibios_window_alignment(bus, type); 857ac5ad93eSGavin Shan return max(align, arch_align); 858ac5ad93eSGavin Shan } 859ac5ad93eSGavin Shan 860c8adf9a3SRam Pai /** 8610d607618SNicholas Johnson * pbus_size_io() - Size the I/O window of a given bus 862c8adf9a3SRam Pai * 8630d607618SNicholas Johnson * @bus: The bus 8640d607618SNicholas Johnson * @min_size: The minimum I/O window that must be allocated 8650d607618SNicholas Johnson * @add_size: Additional optional I/O window 8660d607618SNicholas Johnson * @realloc_head: Track the additional I/O window on this list 867c8adf9a3SRam Pai * 8680d607618SNicholas Johnson * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these 8690d607618SNicholas Johnson * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI 8700d607618SNicholas Johnson * devices are limited to 256 bytes. We must be careful with the ISA 8710d607618SNicholas Johnson * aliasing though. 872c8adf9a3SRam Pai */ 873c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 8740d607618SNicholas Johnson resource_size_t add_size, 8750d607618SNicholas Johnson struct list_head *realloc_head) 8761da177e4SLinus Torvalds { 8771da177e4SLinus Torvalds struct pci_dev *dev; 878c13704f5SNicholas Johnson struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO, 8795b285415SYinghai Lu IORESOURCE_IO); 88011251a86SWei Yang resource_size_t size = 0, size0 = 0, size1 = 0; 881be768912SYinghai Lu resource_size_t children_add_size = 0; 8822d1d6678SBjorn Helgaas resource_size_t min_align, align; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds if (!b_res) 8851da177e4SLinus Torvalds return; 8861da177e4SLinus Torvalds 887c13704f5SNicholas Johnson /* If resource is already assigned, nothing more to do */ 888c13704f5SNicholas Johnson if (b_res->parent) 889c13704f5SNicholas Johnson return; 890c13704f5SNicholas Johnson 8912d1d6678SBjorn Helgaas min_align = window_alignment(bus, IORESOURCE_IO); 8921da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8931da177e4SLinus Torvalds int i; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 8961da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 8971da177e4SLinus Torvalds unsigned long r_size; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 9001da177e4SLinus Torvalds continue; 901022edd86SZhao, Yu r_size = resource_size(r); 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds if (r_size < 0x400) 9041da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 9051da177e4SLinus Torvalds size += r_size; 9061da177e4SLinus Torvalds else 9071da177e4SLinus Torvalds size1 += r_size; 908be768912SYinghai Lu 909fd591341SYinghai Lu align = pci_resource_alignment(dev, r); 910fd591341SYinghai Lu if (align > min_align) 911fd591341SYinghai Lu min_align = align; 912fd591341SYinghai Lu 9139e8bf93aSRam Pai if (realloc_head) 9149e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds } 917fd591341SYinghai Lu 918de3ffa30SJon Derrick size0 = calculate_iosize(size, min_size, size1, 0, 0, 919fd591341SYinghai Lu resource_size(b_res), min_align); 920de3ffa30SJon Derrick size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 : 921de3ffa30SJon Derrick calculate_iosize(size, min_size, size1, add_size, children_add_size, 922fd591341SYinghai Lu resource_size(b_res), min_align); 923c8adf9a3SRam Pai if (!size0 && !size1) { 9242c8d5a2dSIvan Kokshaysky if (bus->self && (b_res->start || b_res->end)) 9257506dc79SFrederick Lawler pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n", 926227f0647SRyan Desfosses b_res, &bus->busn_res); 9271da177e4SLinus Torvalds b_res->flags = 0; 9281da177e4SLinus Torvalds return; 9291da177e4SLinus Torvalds } 930fd591341SYinghai Lu 931fd591341SYinghai Lu b_res->start = min_align; 932c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 93388452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 9342c8d5a2dSIvan Kokshaysky if (bus->self && size1 > size0 && realloc_head) { 935fd591341SYinghai Lu add_to_list(realloc_head, bus->self, b_res, size1-size0, 936fd591341SYinghai Lu min_align); 93734c6b710SMohan Kumar pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n", 938227f0647SRyan Desfosses b_res, &bus->busn_res, 93911251a86SWei Yang (unsigned long long) size1 - size0); 940b592443dSYinghai Lu } 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 943c121504eSGavin Shan static inline resource_size_t calculate_mem_align(resource_size_t *aligns, 944c121504eSGavin Shan int max_order) 945c121504eSGavin Shan { 946c121504eSGavin Shan resource_size_t align = 0; 947c121504eSGavin Shan resource_size_t min_align = 0; 948c121504eSGavin Shan int order; 949c121504eSGavin Shan 950c121504eSGavin Shan for (order = 0; order <= max_order; order++) { 951c121504eSGavin Shan resource_size_t align1 = 1; 952c121504eSGavin Shan 953c121504eSGavin Shan align1 <<= (order + 20); 954c121504eSGavin Shan 955c121504eSGavin Shan if (!align) 956c121504eSGavin Shan min_align = align1; 957c121504eSGavin Shan else if (ALIGN(align + min_align, min_align) < align1) 958c121504eSGavin Shan min_align = align1 >> 1; 959c121504eSGavin Shan align += aligns[order]; 960c121504eSGavin Shan } 961c121504eSGavin Shan 962c121504eSGavin Shan return min_align; 963c121504eSGavin Shan } 964c121504eSGavin Shan 965c8adf9a3SRam Pai /** 9660d607618SNicholas Johnson * pbus_size_mem() - Size the memory window of a given bus 967c8adf9a3SRam Pai * 9680d607618SNicholas Johnson * @bus: The bus 9690d607618SNicholas Johnson * @mask: Mask the resource flag, then compare it with type 9700d607618SNicholas Johnson * @type: The type of free resource from bridge 9710d607618SNicholas Johnson * @type2: Second match type 9720d607618SNicholas Johnson * @type3: Third match type 9730d607618SNicholas Johnson * @min_size: The minimum memory window that must be allocated 9740d607618SNicholas Johnson * @add_size: Additional optional memory window 9750d607618SNicholas Johnson * @realloc_head: Track the additional memory window on this list 976c8adf9a3SRam Pai * 9770d607618SNicholas Johnson * Calculate the size of the bus and minimal alignment which guarantees 9780d607618SNicholas Johnson * that all child resources fit in this size. 97930afe8d0SBjorn Helgaas * 9800d607618SNicholas Johnson * Return -ENOSPC if there's no available bus resource of the desired 9810d607618SNicholas Johnson * type. Otherwise, set the bus resource start/end to indicate the 9820d607618SNicholas Johnson * required size, add things to realloc_head (if supplied), and return 0. 983c8adf9a3SRam Pai */ 98428760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 9855b285415SYinghai Lu unsigned long type, unsigned long type2, 9860d607618SNicholas Johnson unsigned long type3, resource_size_t min_size, 9870d607618SNicholas Johnson resource_size_t add_size, 988bdc4abecSYinghai Lu struct list_head *realloc_head) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds struct pci_dev *dev; 991c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 9920d607618SNicholas Johnson resource_size_t aligns[18]; /* Alignments from 1MB to 128GB */ 9931da177e4SLinus Torvalds int order, max_order; 994c13704f5SNicholas Johnson struct resource *b_res = find_bus_resource_of_type(bus, 9955b285415SYinghai Lu mask | IORESOURCE_PREFETCH, type); 996be768912SYinghai Lu resource_size_t children_add_size = 0; 997d74b9027SWei Yang resource_size_t children_add_align = 0; 998d74b9027SWei Yang resource_size_t add_align = 0; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds if (!b_res) 100130afe8d0SBjorn Helgaas return -ENOSPC; 10021da177e4SLinus Torvalds 1003c13704f5SNicholas Johnson /* If resource is already assigned, nothing more to do */ 1004c13704f5SNicholas Johnson if (b_res->parent) 1005c13704f5SNicholas Johnson return 0; 1006c13704f5SNicholas Johnson 10071da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 10081da177e4SLinus Torvalds max_order = 0; 10091da177e4SLinus Torvalds size = 0; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 10121da177e4SLinus Torvalds int i; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 10151da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 1016c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 10171da177e4SLinus Torvalds 1018a2220d80SDavid Daney if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) || 1019a2220d80SDavid Daney ((r->flags & mask) != type && 10205b285415SYinghai Lu (r->flags & mask) != type2 && 10215b285415SYinghai Lu (r->flags & mask) != type3)) 10221da177e4SLinus Torvalds continue; 1023022edd86SZhao, Yu r_size = resource_size(r); 10242aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 10250d607618SNicholas Johnson /* Put SRIOV requested res to the optional list */ 10269e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 10272aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 1028d74b9027SWei Yang add_align = max(pci_resource_alignment(dev, r), add_align); 10292aceefcbSYinghai Lu r->end = r->start - 1; 10300d607618SNicholas Johnson add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */); 10312aceefcbSYinghai Lu children_add_size += r_size; 10322aceefcbSYinghai Lu continue; 10332aceefcbSYinghai Lu } 10342aceefcbSYinghai Lu #endif 103514c8530dSAlan /* 103614c8530dSAlan * aligns[0] is for 1MB (since bridge memory 103714c8530dSAlan * windows are always at least 1MB aligned), so 103814c8530dSAlan * keep "order" from being negative for smaller 103914c8530dSAlan * resources. 104014c8530dSAlan */ 10416faf17f6SChris Wright align = pci_resource_alignment(dev, r); 10421da177e4SLinus Torvalds order = __ffs(align) - 20; 104314c8530dSAlan if (order < 0) 104414c8530dSAlan order = 0; 104514c8530dSAlan if (order >= ARRAY_SIZE(aligns)) { 10467506dc79SFrederick Lawler pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n", 1047227f0647SRyan Desfosses i, r, (unsigned long long) align); 10481da177e4SLinus Torvalds r->flags = 0; 10491da177e4SLinus Torvalds continue; 10501da177e4SLinus Torvalds } 1051c9c75143SYongji Xie size += max(r_size, align); 10520d607618SNicholas Johnson /* 10530d607618SNicholas Johnson * Exclude ranges with size > align from calculation of 10540d607618SNicholas Johnson * the alignment. 10550d607618SNicholas Johnson */ 1056c9c75143SYongji Xie if (r_size <= align) 10571da177e4SLinus Torvalds aligns[order] += align; 10581da177e4SLinus Torvalds if (order > max_order) 10591da177e4SLinus Torvalds max_order = order; 1060be768912SYinghai Lu 1061d74b9027SWei Yang if (realloc_head) { 10629e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 1063d74b9027SWei Yang children_add_align = get_res_add_align(realloc_head, r); 1064d74b9027SWei Yang add_align = max(add_align, children_add_align); 1065d74b9027SWei Yang } 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds } 10688308c54dSJeremy Fitzhardinge 1069c121504eSGavin Shan min_align = calculate_mem_align(aligns, max_order); 10703ad94b0dSWei Yang min_align = max(min_align, window_alignment(bus, b_res->flags)); 1071de3ffa30SJon Derrick size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align); 1072d74b9027SWei Yang add_align = max(min_align, add_align); 1073de3ffa30SJon Derrick size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 : 1074de3ffa30SJon Derrick calculate_memsize(size, min_size, add_size, children_add_size, 1075d74b9027SWei Yang resource_size(b_res), add_align); 1076c8adf9a3SRam Pai if (!size0 && !size1) { 10772c8d5a2dSIvan Kokshaysky if (bus->self && (b_res->start || b_res->end)) 10787506dc79SFrederick Lawler pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n", 1079227f0647SRyan Desfosses b_res, &bus->busn_res); 10801da177e4SLinus Torvalds b_res->flags = 0; 108130afe8d0SBjorn Helgaas return 0; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds b_res->start = min_align; 1084c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 10855b285415SYinghai Lu b_res->flags |= IORESOURCE_STARTALIGN; 10862c8d5a2dSIvan Kokshaysky if (bus->self && size1 > size0 && realloc_head) { 1087d74b9027SWei Yang add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align); 108834c6b710SMohan Kumar pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n", 1089227f0647SRyan Desfosses b_res, &bus->busn_res, 1090d74b9027SWei Yang (unsigned long long) (size1 - size0), 1091d74b9027SWei Yang (unsigned long long) add_align); 1092b592443dSYinghai Lu } 109330afe8d0SBjorn Helgaas return 0; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10960a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 10970a2daa1cSRam Pai { 10980a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 10990a2daa1cSRam Pai return pci_cardbus_io_size; 11000a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 11010a2daa1cSRam Pai return pci_cardbus_mem_size; 11020a2daa1cSRam Pai return 0; 11030a2daa1cSRam Pai } 11040a2daa1cSRam Pai 11050a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 1106bdc4abecSYinghai Lu struct list_head *realloc_head) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 11091da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 111011848934SYinghai Lu resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 11111da177e4SLinus Torvalds u16 ctrl; 11121da177e4SLinus Torvalds 11133796f1e2SYinghai Lu if (b_res[0].parent) 11143796f1e2SYinghai Lu goto handle_b_res_1; 11151da177e4SLinus Torvalds /* 11160d607618SNicholas Johnson * Reserve some resources for CardBus. We reserve a fixed amount 11170d607618SNicholas Johnson * of bus space for CardBus bridges. 11181da177e4SLinus Torvalds */ 111911848934SYinghai Lu b_res[0].start = pci_cardbus_io_size; 112011848934SYinghai Lu b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; 112111848934SYinghai Lu b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 112211848934SYinghai Lu if (realloc_head) { 112311848934SYinghai Lu b_res[0].end -= pci_cardbus_io_size; 112411848934SYinghai Lu add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 112511848934SYinghai Lu pci_cardbus_io_size); 112611848934SYinghai Lu } 11271da177e4SLinus Torvalds 11283796f1e2SYinghai Lu handle_b_res_1: 11293796f1e2SYinghai Lu if (b_res[1].parent) 11303796f1e2SYinghai Lu goto handle_b_res_2; 113111848934SYinghai Lu b_res[1].start = pci_cardbus_io_size; 113211848934SYinghai Lu b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; 113311848934SYinghai Lu b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 113411848934SYinghai Lu if (realloc_head) { 113511848934SYinghai Lu b_res[1].end -= pci_cardbus_io_size; 113611848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 113711848934SYinghai Lu pci_cardbus_io_size); 113811848934SYinghai Lu } 11391da177e4SLinus Torvalds 11403796f1e2SYinghai Lu handle_b_res_2: 11410d607618SNicholas Johnson /* MEM1 must not be pref MMIO */ 1142dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1143dcef0d06SYinghai Lu if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 1144dcef0d06SYinghai Lu ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 1145dcef0d06SYinghai Lu pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1146dcef0d06SYinghai Lu pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1147dcef0d06SYinghai Lu } 1148dcef0d06SYinghai Lu 11490d607618SNicholas Johnson /* Check whether prefetchable memory is supported by this bridge. */ 11501da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 11511da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 11521da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 11531da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 11541da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 11573796f1e2SYinghai Lu if (b_res[2].parent) 11583796f1e2SYinghai Lu goto handle_b_res_3; 11591da177e4SLinus Torvalds /* 11600d607618SNicholas Johnson * If we have prefetchable memory support, allocate two regions. 11610d607618SNicholas Johnson * Otherwise, allocate one region of twice the size. 11621da177e4SLinus Torvalds */ 11631da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 116411848934SYinghai Lu b_res[2].start = pci_cardbus_mem_size; 116511848934SYinghai Lu b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; 116611848934SYinghai Lu b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 116711848934SYinghai Lu IORESOURCE_STARTALIGN; 116811848934SYinghai Lu if (realloc_head) { 116911848934SYinghai Lu b_res[2].end -= pci_cardbus_mem_size; 117011848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+2, 117111848934SYinghai Lu pci_cardbus_mem_size, pci_cardbus_mem_size); 11721da177e4SLinus Torvalds } 11730a2daa1cSRam Pai 11740d607618SNicholas Johnson /* Reduce that to half */ 117511848934SYinghai Lu b_res_3_size = pci_cardbus_mem_size; 117611848934SYinghai Lu } 117711848934SYinghai Lu 11783796f1e2SYinghai Lu handle_b_res_3: 11793796f1e2SYinghai Lu if (b_res[3].parent) 11803796f1e2SYinghai Lu goto handle_done; 118111848934SYinghai Lu b_res[3].start = pci_cardbus_mem_size; 118211848934SYinghai Lu b_res[3].end = b_res[3].start + b_res_3_size - 1; 118311848934SYinghai Lu b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 118411848934SYinghai Lu if (realloc_head) { 118511848934SYinghai Lu b_res[3].end -= b_res_3_size; 118611848934SYinghai Lu add_to_list(realloc_head, bridge, b_res+3, b_res_3_size, 118711848934SYinghai Lu pci_cardbus_mem_size); 118811848934SYinghai Lu } 11893796f1e2SYinghai Lu 11903796f1e2SYinghai Lu handle_done: 11913796f1e2SYinghai Lu ; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 119410874f5aSBjorn Helgaas void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) 11951da177e4SLinus Torvalds { 11961da177e4SLinus Torvalds struct pci_dev *dev; 11975b285415SYinghai Lu unsigned long mask, prefmask, type2 = 0, type3 = 0; 1198d7b8a217SNicholas Johnson resource_size_t additional_io_size = 0, additional_mmio_size = 0, 1199d7b8a217SNicholas Johnson additional_mmio_pref_size = 0; 12002c8d5a2dSIvan Kokshaysky struct resource *pref; 12012c8d5a2dSIvan Kokshaysky struct pci_host_bridge *host; 12022c8d5a2dSIvan Kokshaysky int hdr_type, i, ret; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 12051da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 12061da177e4SLinus Torvalds if (!b) 12071da177e4SLinus Torvalds continue; 12081da177e4SLinus Torvalds 1209b2fb5cc5SHonghui Zhang switch (dev->hdr_type) { 1210b2fb5cc5SHonghui Zhang case PCI_HEADER_TYPE_CARDBUS: 12119e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 12121da177e4SLinus Torvalds break; 12131da177e4SLinus Torvalds 1214b2fb5cc5SHonghui Zhang case PCI_HEADER_TYPE_BRIDGE: 12151da177e4SLinus Torvalds default: 12169e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 12171da177e4SLinus Torvalds break; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* The root bus? */ 12222c8d5a2dSIvan Kokshaysky if (pci_is_root_bus(bus)) { 12232c8d5a2dSIvan Kokshaysky host = to_pci_host_bridge(bus->bridge); 12242c8d5a2dSIvan Kokshaysky if (!host->size_windows) 12251da177e4SLinus Torvalds return; 12262c8d5a2dSIvan Kokshaysky pci_bus_for_each_resource(bus, pref, i) 12272c8d5a2dSIvan Kokshaysky if (pref && (pref->flags & IORESOURCE_PREFETCH)) 12282c8d5a2dSIvan Kokshaysky break; 12292c8d5a2dSIvan Kokshaysky hdr_type = -1; /* Intentionally invalid - not a PCI device. */ 12302c8d5a2dSIvan Kokshaysky } else { 12312c8d5a2dSIvan Kokshaysky pref = &bus->self->resource[PCI_BRIDGE_RESOURCES + 2]; 12322c8d5a2dSIvan Kokshaysky hdr_type = bus->self->hdr_type; 12332c8d5a2dSIvan Kokshaysky } 12341da177e4SLinus Torvalds 12352c8d5a2dSIvan Kokshaysky switch (hdr_type) { 1236b2fb5cc5SHonghui Zhang case PCI_HEADER_TYPE_CARDBUS: 12370d607618SNicholas Johnson /* Don't size CardBuses yet */ 12381da177e4SLinus Torvalds break; 12391da177e4SLinus Torvalds 1240b2fb5cc5SHonghui Zhang case PCI_HEADER_TYPE_BRIDGE: 12411da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 124228760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 1243c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 1244d7b8a217SNicholas Johnson additional_mmio_size = pci_hotplug_mmio_size; 1245d7b8a217SNicholas Johnson additional_mmio_pref_size = pci_hotplug_mmio_pref_size; 124628760489SEric W. Biederman } 124767d29b5cSBjorn Helgaas /* Fall through */ 12481da177e4SLinus Torvalds default: 124919aa7ee4SYinghai Lu pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 125019aa7ee4SYinghai Lu additional_io_size, realloc_head); 125167d29b5cSBjorn Helgaas 125267d29b5cSBjorn Helgaas /* 125367d29b5cSBjorn Helgaas * If there's a 64-bit prefetchable MMIO window, compute 125467d29b5cSBjorn Helgaas * the size required to put all 64-bit prefetchable 125567d29b5cSBjorn Helgaas * resources in it. 125667d29b5cSBjorn Helgaas */ 12571da177e4SLinus Torvalds mask = IORESOURCE_MEM; 12581da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 12592c8d5a2dSIvan Kokshaysky if (pref && (pref->flags & IORESOURCE_MEM_64)) { 12605b285415SYinghai Lu prefmask |= IORESOURCE_MEM_64; 126130afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12625b285415SYinghai Lu prefmask, prefmask, 1263d7b8a217SNicholas Johnson realloc_head ? 0 : additional_mmio_pref_size, 1264d7b8a217SNicholas Johnson additional_mmio_pref_size, realloc_head); 126567d29b5cSBjorn Helgaas 12665b285415SYinghai Lu /* 126767d29b5cSBjorn Helgaas * If successful, all non-prefetchable resources 126867d29b5cSBjorn Helgaas * and any 32-bit prefetchable resources will go in 126967d29b5cSBjorn Helgaas * the non-prefetchable window. 127067d29b5cSBjorn Helgaas */ 127167d29b5cSBjorn Helgaas if (ret == 0) { 12725b285415SYinghai Lu mask = prefmask; 12735b285415SYinghai Lu type2 = prefmask & ~IORESOURCE_MEM_64; 12745b285415SYinghai Lu type3 = prefmask & ~IORESOURCE_PREFETCH; 12755b285415SYinghai Lu } 12765b285415SYinghai Lu } 127767d29b5cSBjorn Helgaas 127867d29b5cSBjorn Helgaas /* 127967d29b5cSBjorn Helgaas * If there is no 64-bit prefetchable window, compute the 128067d29b5cSBjorn Helgaas * size required to put all prefetchable resources in the 128167d29b5cSBjorn Helgaas * 32-bit prefetchable window (if there is one). 128267d29b5cSBjorn Helgaas */ 12835b285415SYinghai Lu if (!type2) { 12845b285415SYinghai Lu prefmask &= ~IORESOURCE_MEM_64; 128530afe8d0SBjorn Helgaas ret = pbus_size_mem(bus, prefmask, prefmask, 12865b285415SYinghai Lu prefmask, prefmask, 1287d7b8a217SNicholas Johnson realloc_head ? 0 : additional_mmio_pref_size, 1288d7b8a217SNicholas Johnson additional_mmio_pref_size, realloc_head); 128967d29b5cSBjorn Helgaas 129067d29b5cSBjorn Helgaas /* 129167d29b5cSBjorn Helgaas * If successful, only non-prefetchable resources 129267d29b5cSBjorn Helgaas * will go in the non-prefetchable window. 129367d29b5cSBjorn Helgaas */ 129467d29b5cSBjorn Helgaas if (ret == 0) 12955b285415SYinghai Lu mask = prefmask; 129628760489SEric W. Biederman else 1297d7b8a217SNicholas Johnson additional_mmio_size += additional_mmio_pref_size; 129867d29b5cSBjorn Helgaas 12995b285415SYinghai Lu type2 = type3 = IORESOURCE_MEM; 13005b285415SYinghai Lu } 130167d29b5cSBjorn Helgaas 130267d29b5cSBjorn Helgaas /* 130367d29b5cSBjorn Helgaas * Compute the size required to put everything else in the 130467d29b5cSBjorn Helgaas * non-prefetchable window. This includes: 130567d29b5cSBjorn Helgaas * 130667d29b5cSBjorn Helgaas * - all non-prefetchable resources 130767d29b5cSBjorn Helgaas * - 32-bit prefetchable resources if there's a 64-bit 130867d29b5cSBjorn Helgaas * prefetchable window or no prefetchable window at all 13090d607618SNicholas Johnson * - 64-bit prefetchable resources if there's no prefetchable 13100d607618SNicholas Johnson * window at all 131167d29b5cSBjorn Helgaas * 13120d607618SNicholas Johnson * Note that the strategy in __pci_assign_resource() must match 13130d607618SNicholas Johnson * that used here. Specifically, we cannot put a 32-bit 13140d607618SNicholas Johnson * prefetchable resource in a 64-bit prefetchable window. 131567d29b5cSBjorn Helgaas */ 13165b285415SYinghai Lu pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, 1317d7b8a217SNicholas Johnson realloc_head ? 0 : additional_mmio_size, 1318d7b8a217SNicholas Johnson additional_mmio_size, realloc_head); 13191da177e4SLinus Torvalds break; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds } 1322c8adf9a3SRam Pai 132310874f5aSBjorn Helgaas void pci_bus_size_bridges(struct pci_bus *bus) 1324c8adf9a3SRam Pai { 1325c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 1326c8adf9a3SRam Pai } 13271da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 13281da177e4SLinus Torvalds 1329d04d0111SDavid Daney static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) 1330d04d0111SDavid Daney { 1331d04d0111SDavid Daney int i; 1332d04d0111SDavid Daney struct resource *parent_r; 1333d04d0111SDavid Daney unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM | 1334d04d0111SDavid Daney IORESOURCE_PREFETCH; 1335d04d0111SDavid Daney 1336d04d0111SDavid Daney pci_bus_for_each_resource(b, parent_r, i) { 1337d04d0111SDavid Daney if (!parent_r) 1338d04d0111SDavid Daney continue; 1339d04d0111SDavid Daney 1340d04d0111SDavid Daney if ((r->flags & mask) == (parent_r->flags & mask) && 1341d04d0111SDavid Daney resource_contains(parent_r, r)) 1342d04d0111SDavid Daney request_resource(parent_r, r); 1343d04d0111SDavid Daney } 1344d04d0111SDavid Daney } 1345d04d0111SDavid Daney 1346d04d0111SDavid Daney /* 13470d607618SNicholas Johnson * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are 13480d607618SNicholas Johnson * skipped by pbus_assign_resources_sorted(). 1349d04d0111SDavid Daney */ 1350d04d0111SDavid Daney static void pdev_assign_fixed_resources(struct pci_dev *dev) 1351d04d0111SDavid Daney { 1352d04d0111SDavid Daney int i; 1353d04d0111SDavid Daney 1354d04d0111SDavid Daney for (i = 0; i < PCI_NUM_RESOURCES; i++) { 1355d04d0111SDavid Daney struct pci_bus *b; 1356d04d0111SDavid Daney struct resource *r = &dev->resource[i]; 1357d04d0111SDavid Daney 1358d04d0111SDavid Daney if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) || 1359d04d0111SDavid Daney !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) 1360d04d0111SDavid Daney continue; 1361d04d0111SDavid Daney 1362d04d0111SDavid Daney b = dev->bus; 1363d04d0111SDavid Daney while (b && !r->parent) { 1364d04d0111SDavid Daney assign_fixed_resource_on_bus(b, r); 1365d04d0111SDavid Daney b = b->parent; 1366d04d0111SDavid Daney } 1367d04d0111SDavid Daney } 1368d04d0111SDavid Daney } 1369d04d0111SDavid Daney 137010874f5aSBjorn Helgaas void __pci_bus_assign_resources(const struct pci_bus *bus, 1371bdc4abecSYinghai Lu struct list_head *realloc_head, 1372bdc4abecSYinghai Lu struct list_head *fail_head) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct pci_bus *b; 13751da177e4SLinus Torvalds struct pci_dev *dev; 13761da177e4SLinus Torvalds 13779e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 1380d04d0111SDavid Daney pdev_assign_fixed_resources(dev); 1381d04d0111SDavid Daney 13821da177e4SLinus Torvalds b = dev->subordinate; 13831da177e4SLinus Torvalds if (!b) 13841da177e4SLinus Torvalds continue; 13851da177e4SLinus Torvalds 13869e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 13871da177e4SLinus Torvalds 1388b2fb5cc5SHonghui Zhang switch (dev->hdr_type) { 1389b2fb5cc5SHonghui Zhang case PCI_HEADER_TYPE_BRIDGE: 13906841ec68SYinghai Lu if (!pci_is_enabled(dev)) 13911da177e4SLinus Torvalds pci_setup_bridge(b); 13921da177e4SLinus Torvalds break; 13931da177e4SLinus Torvalds 1394b2fb5cc5SHonghui Zhang case PCI_HEADER_TYPE_CARDBUS: 13951da177e4SLinus Torvalds pci_setup_cardbus(b); 13961da177e4SLinus Torvalds break; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds default: 13997506dc79SFrederick Lawler pci_info(dev, "not setting up bridge for bus %04x:%02x\n", 1400227f0647SRyan Desfosses pci_domain_nr(b), b->number); 14011da177e4SLinus Torvalds break; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds } 1405568ddef8SYinghai Lu 140610874f5aSBjorn Helgaas void pci_bus_assign_resources(const struct pci_bus *bus) 1407568ddef8SYinghai Lu { 1408c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 1409568ddef8SYinghai Lu } 14101da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 14111da177e4SLinus Torvalds 1412765bf9b7SLorenzo Pieralisi static void pci_claim_device_resources(struct pci_dev *dev) 1413765bf9b7SLorenzo Pieralisi { 1414765bf9b7SLorenzo Pieralisi int i; 1415765bf9b7SLorenzo Pieralisi 1416765bf9b7SLorenzo Pieralisi for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) { 1417765bf9b7SLorenzo Pieralisi struct resource *r = &dev->resource[i]; 1418765bf9b7SLorenzo Pieralisi 1419765bf9b7SLorenzo Pieralisi if (!r->flags || r->parent) 1420765bf9b7SLorenzo Pieralisi continue; 1421765bf9b7SLorenzo Pieralisi 1422765bf9b7SLorenzo Pieralisi pci_claim_resource(dev, i); 1423765bf9b7SLorenzo Pieralisi } 1424765bf9b7SLorenzo Pieralisi } 1425765bf9b7SLorenzo Pieralisi 1426765bf9b7SLorenzo Pieralisi static void pci_claim_bridge_resources(struct pci_dev *dev) 1427765bf9b7SLorenzo Pieralisi { 1428765bf9b7SLorenzo Pieralisi int i; 1429765bf9b7SLorenzo Pieralisi 1430765bf9b7SLorenzo Pieralisi for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) { 1431765bf9b7SLorenzo Pieralisi struct resource *r = &dev->resource[i]; 1432765bf9b7SLorenzo Pieralisi 1433765bf9b7SLorenzo Pieralisi if (!r->flags || r->parent) 1434765bf9b7SLorenzo Pieralisi continue; 1435765bf9b7SLorenzo Pieralisi 1436765bf9b7SLorenzo Pieralisi pci_claim_bridge_resource(dev, i); 1437765bf9b7SLorenzo Pieralisi } 1438765bf9b7SLorenzo Pieralisi } 1439765bf9b7SLorenzo Pieralisi 1440765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_dev_resources(struct pci_bus *b) 1441765bf9b7SLorenzo Pieralisi { 1442765bf9b7SLorenzo Pieralisi struct pci_dev *dev; 1443765bf9b7SLorenzo Pieralisi struct pci_bus *child; 1444765bf9b7SLorenzo Pieralisi 1445765bf9b7SLorenzo Pieralisi list_for_each_entry(dev, &b->devices, bus_list) { 1446765bf9b7SLorenzo Pieralisi pci_claim_device_resources(dev); 1447765bf9b7SLorenzo Pieralisi 1448765bf9b7SLorenzo Pieralisi child = dev->subordinate; 1449765bf9b7SLorenzo Pieralisi if (child) 1450765bf9b7SLorenzo Pieralisi pci_bus_allocate_dev_resources(child); 1451765bf9b7SLorenzo Pieralisi } 1452765bf9b7SLorenzo Pieralisi } 1453765bf9b7SLorenzo Pieralisi 1454765bf9b7SLorenzo Pieralisi static void pci_bus_allocate_resources(struct pci_bus *b) 1455765bf9b7SLorenzo Pieralisi { 1456765bf9b7SLorenzo Pieralisi struct pci_bus *child; 1457765bf9b7SLorenzo Pieralisi 1458765bf9b7SLorenzo Pieralisi /* 14590d607618SNicholas Johnson * Carry out a depth-first search on the PCI bus tree to allocate 14600d607618SNicholas Johnson * bridge apertures. Read the programmed bridge bases and 14610d607618SNicholas Johnson * recursively claim the respective bridge resources. 1462765bf9b7SLorenzo Pieralisi */ 1463765bf9b7SLorenzo Pieralisi if (b->self) { 1464765bf9b7SLorenzo Pieralisi pci_read_bridge_bases(b); 1465765bf9b7SLorenzo Pieralisi pci_claim_bridge_resources(b->self); 1466765bf9b7SLorenzo Pieralisi } 1467765bf9b7SLorenzo Pieralisi 1468765bf9b7SLorenzo Pieralisi list_for_each_entry(child, &b->children, node) 1469765bf9b7SLorenzo Pieralisi pci_bus_allocate_resources(child); 1470765bf9b7SLorenzo Pieralisi } 1471765bf9b7SLorenzo Pieralisi 1472765bf9b7SLorenzo Pieralisi void pci_bus_claim_resources(struct pci_bus *b) 1473765bf9b7SLorenzo Pieralisi { 1474765bf9b7SLorenzo Pieralisi pci_bus_allocate_resources(b); 1475765bf9b7SLorenzo Pieralisi pci_bus_allocate_dev_resources(b); 1476765bf9b7SLorenzo Pieralisi } 1477765bf9b7SLorenzo Pieralisi EXPORT_SYMBOL(pci_bus_claim_resources); 1478765bf9b7SLorenzo Pieralisi 147910874f5aSBjorn Helgaas static void __pci_bridge_assign_resources(const struct pci_dev *bridge, 1480bdc4abecSYinghai Lu struct list_head *add_head, 1481bdc4abecSYinghai Lu struct list_head *fail_head) 14826841ec68SYinghai Lu { 14836841ec68SYinghai Lu struct pci_bus *b; 14846841ec68SYinghai Lu 14858424d759SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, 14868424d759SYinghai Lu add_head, fail_head); 14876841ec68SYinghai Lu 14886841ec68SYinghai Lu b = bridge->subordinate; 14896841ec68SYinghai Lu if (!b) 14906841ec68SYinghai Lu return; 14916841ec68SYinghai Lu 14928424d759SYinghai Lu __pci_bus_assign_resources(b, add_head, fail_head); 14936841ec68SYinghai Lu 14946841ec68SYinghai Lu switch (bridge->class >> 8) { 14956841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 14966841ec68SYinghai Lu pci_setup_bridge(b); 14976841ec68SYinghai Lu break; 14986841ec68SYinghai Lu 14996841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 15006841ec68SYinghai Lu pci_setup_cardbus(b); 15016841ec68SYinghai Lu break; 15026841ec68SYinghai Lu 15036841ec68SYinghai Lu default: 15047506dc79SFrederick Lawler pci_info(bridge, "not setting up bridge for bus %04x:%02x\n", 1505227f0647SRyan Desfosses pci_domain_nr(b), b->number); 15066841ec68SYinghai Lu break; 15076841ec68SYinghai Lu } 15086841ec68SYinghai Lu } 1509cb21bc94SChristian König 1510cb21bc94SChristian König #define PCI_RES_TYPE_MASK \ 1511cb21bc94SChristian König (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\ 1512cb21bc94SChristian König IORESOURCE_MEM_64) 1513cb21bc94SChristian König 15145009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 15155009b460SYinghai Lu unsigned long type) 15165009b460SYinghai Lu { 15175b285415SYinghai Lu struct pci_dev *dev = bus->self; 15185009b460SYinghai Lu struct resource *r; 15195b285415SYinghai Lu unsigned old_flags = 0; 15205b285415SYinghai Lu struct resource *b_res; 15215b285415SYinghai Lu int idx = 1; 15225009b460SYinghai Lu 15235b285415SYinghai Lu b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; 15245b285415SYinghai Lu 15255b285415SYinghai Lu /* 15260d607618SNicholas Johnson * 1. If IO port assignment fails, release bridge IO port. 15270d607618SNicholas Johnson * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO. 15280d607618SNicholas Johnson * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit, 15290d607618SNicholas Johnson * release bridge pref MMIO. 15300d607618SNicholas Johnson * 4. If pref MMIO assignment fails, and bridge pref is 32bit, 15310d607618SNicholas Johnson * release bridge pref MMIO. 15320d607618SNicholas Johnson * 5. If pref MMIO assignment fails, and bridge pref is not 15330d607618SNicholas Johnson * assigned, release bridge nonpref MMIO. 15345b285415SYinghai Lu */ 15355b285415SYinghai Lu if (type & IORESOURCE_IO) 15365b285415SYinghai Lu idx = 0; 15375b285415SYinghai Lu else if (!(type & IORESOURCE_PREFETCH)) 15385b285415SYinghai Lu idx = 1; 15395b285415SYinghai Lu else if ((type & IORESOURCE_MEM_64) && 15405b285415SYinghai Lu (b_res[2].flags & IORESOURCE_MEM_64)) 15415b285415SYinghai Lu idx = 2; 15425b285415SYinghai Lu else if (!(b_res[2].flags & IORESOURCE_MEM_64) && 15435b285415SYinghai Lu (b_res[2].flags & IORESOURCE_PREFETCH)) 15445b285415SYinghai Lu idx = 2; 15455b285415SYinghai Lu else 15465b285415SYinghai Lu idx = 1; 15475b285415SYinghai Lu 15485b285415SYinghai Lu r = &b_res[idx]; 15495b285415SYinghai Lu 15505009b460SYinghai Lu if (!r->parent) 15515b285415SYinghai Lu return; 15525b285415SYinghai Lu 15530d607618SNicholas Johnson /* If there are children, release them all */ 15545009b460SYinghai Lu release_child_resources(r); 15555009b460SYinghai Lu if (!release_resource(r)) { 1556cb21bc94SChristian König type = old_flags = r->flags & PCI_RES_TYPE_MASK; 155734c6b710SMohan Kumar pci_info(dev, "resource %d %pR released\n", 15585b285415SYinghai Lu PCI_BRIDGE_RESOURCES + idx, r); 15590d607618SNicholas Johnson /* Keep the old size */ 15605009b460SYinghai Lu r->end = resource_size(r) - 1; 15615009b460SYinghai Lu r->start = 0; 15625009b460SYinghai Lu r->flags = 0; 15635009b460SYinghai Lu 15640d607618SNicholas Johnson /* Avoiding touch the one without PREF */ 15655009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 15665009b460SYinghai Lu type = IORESOURCE_PREFETCH; 15675009b460SYinghai Lu __pci_setup_bridge(bus, type); 15680d607618SNicholas Johnson /* For next child res under same bridge */ 15695b285415SYinghai Lu r->flags = old_flags; 15705009b460SYinghai Lu } 15715009b460SYinghai Lu } 15725009b460SYinghai Lu 15735009b460SYinghai Lu enum release_type { 15745009b460SYinghai Lu leaf_only, 15755009b460SYinghai Lu whole_subtree, 15765009b460SYinghai Lu }; 15770d607618SNicholas Johnson 15785009b460SYinghai Lu /* 15790d607618SNicholas Johnson * Try to release PCI bridge resources from leaf bridge, so we can allocate 15800d607618SNicholas Johnson * a larger window later. 15815009b460SYinghai Lu */ 158210874f5aSBjorn Helgaas static void pci_bus_release_bridge_resources(struct pci_bus *bus, 15835009b460SYinghai Lu unsigned long type, 15845009b460SYinghai Lu enum release_type rel_type) 15855009b460SYinghai Lu { 15865009b460SYinghai Lu struct pci_dev *dev; 15875009b460SYinghai Lu bool is_leaf_bridge = true; 15885009b460SYinghai Lu 15895009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 15905009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 15915009b460SYinghai Lu if (!b) 15925009b460SYinghai Lu continue; 15935009b460SYinghai Lu 15945009b460SYinghai Lu is_leaf_bridge = false; 15955009b460SYinghai Lu 15965009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 15975009b460SYinghai Lu continue; 15985009b460SYinghai Lu 15995009b460SYinghai Lu if (rel_type == whole_subtree) 16005009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 16015009b460SYinghai Lu whole_subtree); 16025009b460SYinghai Lu } 16035009b460SYinghai Lu 16045009b460SYinghai Lu if (pci_is_root_bus(bus)) 16055009b460SYinghai Lu return; 16065009b460SYinghai Lu 16075009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 16085009b460SYinghai Lu return; 16095009b460SYinghai Lu 16105009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 16115009b460SYinghai Lu pci_bridge_release_resources(bus, type); 16125009b460SYinghai Lu } 16135009b460SYinghai Lu 161476fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 161576fbc263SYinghai Lu { 161689a74eccSBjorn Helgaas struct resource *res; 161776fbc263SYinghai Lu int i; 161876fbc263SYinghai Lu 161989a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 16207c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 162176fbc263SYinghai Lu continue; 162276fbc263SYinghai Lu 162334c6b710SMohan Kumar dev_info(&bus->dev, "resource %d %pR\n", i, res); 162476fbc263SYinghai Lu } 162576fbc263SYinghai Lu } 162676fbc263SYinghai Lu 162776fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 162876fbc263SYinghai Lu { 162976fbc263SYinghai Lu struct pci_bus *b; 163076fbc263SYinghai Lu struct pci_dev *dev; 163176fbc263SYinghai Lu 163276fbc263SYinghai Lu 163376fbc263SYinghai Lu pci_bus_dump_res(bus); 163476fbc263SYinghai Lu 163576fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 163676fbc263SYinghai Lu b = dev->subordinate; 163776fbc263SYinghai Lu if (!b) 163876fbc263SYinghai Lu continue; 163976fbc263SYinghai Lu 164076fbc263SYinghai Lu pci_bus_dump_resources(b); 164176fbc263SYinghai Lu } 164276fbc263SYinghai Lu } 164376fbc263SYinghai Lu 1644ff35147cSYinghai Lu static int pci_bus_get_depth(struct pci_bus *bus) 1645da7822e5SYinghai Lu { 1646da7822e5SYinghai Lu int depth = 0; 1647f2a230bdSWei Yang struct pci_bus *child_bus; 1648da7822e5SYinghai Lu 1649f2a230bdSWei Yang list_for_each_entry(child_bus, &bus->children, node) { 1650da7822e5SYinghai Lu int ret; 1651da7822e5SYinghai Lu 1652f2a230bdSWei Yang ret = pci_bus_get_depth(child_bus); 1653da7822e5SYinghai Lu if (ret + 1 > depth) 1654da7822e5SYinghai Lu depth = ret + 1; 1655da7822e5SYinghai Lu } 1656da7822e5SYinghai Lu 1657da7822e5SYinghai Lu return depth; 1658da7822e5SYinghai Lu } 1659da7822e5SYinghai Lu 1660b55438fdSYinghai Lu /* 1661b55438fdSYinghai Lu * -1: undefined, will auto detect later 1662b55438fdSYinghai Lu * 0: disabled by user 1663b55438fdSYinghai Lu * 1: disabled by auto detect 1664b55438fdSYinghai Lu * 2: enabled by user 1665b55438fdSYinghai Lu * 3: enabled by auto detect 1666b55438fdSYinghai Lu */ 1667b55438fdSYinghai Lu enum enable_type { 1668b55438fdSYinghai Lu undefined = -1, 1669b55438fdSYinghai Lu user_disabled, 1670b55438fdSYinghai Lu auto_disabled, 1671b55438fdSYinghai Lu user_enabled, 1672b55438fdSYinghai Lu auto_enabled, 1673b55438fdSYinghai Lu }; 1674b55438fdSYinghai Lu 1675ff35147cSYinghai Lu static enum enable_type pci_realloc_enable = undefined; 1676b55438fdSYinghai Lu void __init pci_realloc_get_opt(char *str) 1677b55438fdSYinghai Lu { 1678b55438fdSYinghai Lu if (!strncmp(str, "off", 3)) 1679b55438fdSYinghai Lu pci_realloc_enable = user_disabled; 1680b55438fdSYinghai Lu else if (!strncmp(str, "on", 2)) 1681b55438fdSYinghai Lu pci_realloc_enable = user_enabled; 1682b55438fdSYinghai Lu } 1683ff35147cSYinghai Lu static bool pci_realloc_enabled(enum enable_type enable) 1684b55438fdSYinghai Lu { 1685967260cdSYinghai Lu return enable >= user_enabled; 1686b55438fdSYinghai Lu } 1687f483d392SRam Pai 1688b07f2ebcSYinghai Lu #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) 1689ff35147cSYinghai Lu static int iov_resources_unassigned(struct pci_dev *dev, void *data) 1690223d96fcSYinghai Lu { 1691b07f2ebcSYinghai Lu int i; 1692223d96fcSYinghai Lu bool *unassigned = data; 1693b07f2ebcSYinghai Lu 169439098edbSDenis Efremov for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 169539098edbSDenis Efremov struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES]; 1696fa216bf4SYinghai Lu struct pci_bus_region region; 1697b07f2ebcSYinghai Lu 1698223d96fcSYinghai Lu /* Not assigned or rejected by kernel? */ 1699fa216bf4SYinghai Lu if (!r->flags) 1700fa216bf4SYinghai Lu continue; 1701b07f2ebcSYinghai Lu 1702fc279850SYinghai Lu pcibios_resource_to_bus(dev->bus, ®ion, r); 1703fa216bf4SYinghai Lu if (!region.start) { 1704223d96fcSYinghai Lu *unassigned = true; 17050d607618SNicholas Johnson return 1; /* Return early from pci_walk_bus() */ 1706b07f2ebcSYinghai Lu } 1707b07f2ebcSYinghai Lu } 1708b07f2ebcSYinghai Lu 1709223d96fcSYinghai Lu return 0; 1710223d96fcSYinghai Lu } 1711223d96fcSYinghai Lu 1712ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1713967260cdSYinghai Lu enum enable_type enable_local) 1714223d96fcSYinghai Lu { 1715223d96fcSYinghai Lu bool unassigned = false; 17167ac0d094SBenjamin Herrenschmidt struct pci_host_bridge *host; 1717223d96fcSYinghai Lu 1718967260cdSYinghai Lu if (enable_local != undefined) 1719967260cdSYinghai Lu return enable_local; 1720223d96fcSYinghai Lu 17217ac0d094SBenjamin Herrenschmidt host = pci_find_host_bridge(bus); 17227ac0d094SBenjamin Herrenschmidt if (host->preserve_config) 17237ac0d094SBenjamin Herrenschmidt return auto_disabled; 17247ac0d094SBenjamin Herrenschmidt 1725223d96fcSYinghai Lu pci_walk_bus(bus, iov_resources_unassigned, &unassigned); 1726967260cdSYinghai Lu if (unassigned) 1727967260cdSYinghai Lu return auto_enabled; 1728967260cdSYinghai Lu 1729967260cdSYinghai Lu return enable_local; 1730b07f2ebcSYinghai Lu } 1731223d96fcSYinghai Lu #else 1732ff35147cSYinghai Lu static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1733967260cdSYinghai Lu enum enable_type enable_local) 1734967260cdSYinghai Lu { 1735967260cdSYinghai Lu return enable_local; 1736b07f2ebcSYinghai Lu } 1737b07f2ebcSYinghai Lu #endif 1738b07f2ebcSYinghai Lu 1739da7822e5SYinghai Lu /* 17400d607618SNicholas Johnson * First try will not touch PCI bridge res. 17410d607618SNicholas Johnson * Second and later try will clear small leaf bridge res. 17420d607618SNicholas Johnson * Will stop till to the max depth if can not find good one. 1743da7822e5SYinghai Lu */ 174439772038SYinghai Lu void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) 17451da177e4SLinus Torvalds { 17460d607618SNicholas Johnson LIST_HEAD(realloc_head); 17470d607618SNicholas Johnson /* List of resources that want additional resources */ 1748bdc4abecSYinghai Lu struct list_head *add_list = NULL; 1749da7822e5SYinghai Lu int tried_times = 0; 1750da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1751bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1752b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 175319aa7ee4SYinghai Lu int pci_try_num = 1; 175455ed83a6SYinghai Lu enum enable_type enable_local; 1755da7822e5SYinghai Lu 17560d607618SNicholas Johnson /* Don't realloc if asked to do so */ 175755ed83a6SYinghai Lu enable_local = pci_realloc_detect(bus, pci_realloc_enable); 1758967260cdSYinghai Lu if (pci_realloc_enabled(enable_local)) { 175955ed83a6SYinghai Lu int max_depth = pci_bus_get_depth(bus); 176019aa7ee4SYinghai Lu 1761da7822e5SYinghai Lu pci_try_num = max_depth + 1; 176234c6b710SMohan Kumar dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n", 1763da7822e5SYinghai Lu max_depth, pci_try_num); 176419aa7ee4SYinghai Lu } 1765da7822e5SYinghai Lu 1766da7822e5SYinghai Lu again: 176719aa7ee4SYinghai Lu /* 17680d607618SNicholas Johnson * Last try will use add_list, otherwise will try good to have as must 17690d607618SNicholas Johnson * have, so can realloc parent bridge resource 177019aa7ee4SYinghai Lu */ 177119aa7ee4SYinghai Lu if (tried_times + 1 == pci_try_num) 1772bdc4abecSYinghai Lu add_list = &realloc_head; 17730d607618SNicholas Johnson /* 17740d607618SNicholas Johnson * Depth first, calculate sizes and alignments of all subordinate buses. 17750d607618SNicholas Johnson */ 177619aa7ee4SYinghai Lu __pci_bus_size_bridges(bus, add_list); 1777c8adf9a3SRam Pai 17781da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1779bdc4abecSYinghai Lu __pci_bus_assign_resources(bus, add_list, &fail_head); 178019aa7ee4SYinghai Lu if (add_list) 1781bdc4abecSYinghai Lu BUG_ON(!list_empty(add_list)); 1782da7822e5SYinghai Lu tried_times++; 1783da7822e5SYinghai Lu 17840d607618SNicholas Johnson /* Any device complain? */ 1785bdc4abecSYinghai Lu if (list_empty(&fail_head)) 1786928bea96SYinghai Lu goto dump; 1787f483d392SRam Pai 17880c5be0cbSYinghai Lu if (tried_times >= pci_try_num) { 1789967260cdSYinghai Lu if (enable_local == undefined) 179055ed83a6SYinghai Lu dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n"); 1791967260cdSYinghai Lu else if (enable_local == auto_enabled) 179255ed83a6SYinghai Lu dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); 1793eb572e7cSYinghai Lu 1794bffc56d4SYinghai Lu free_list(&fail_head); 1795928bea96SYinghai Lu goto dump; 1796da7822e5SYinghai Lu } 1797da7822e5SYinghai Lu 179834c6b710SMohan Kumar dev_info(&bus->dev, "No. %d try to assign unassigned res\n", 179934c6b710SMohan Kumar tried_times + 1); 1800da7822e5SYinghai Lu 18010d607618SNicholas Johnson /* Third times and later will not check if it is leaf */ 1802da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1803da7822e5SYinghai Lu rel_type = whole_subtree; 1804da7822e5SYinghai Lu 1805da7822e5SYinghai Lu /* 1806da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 18070d607618SNicholas Johnson * child device under that bridge. 1808da7822e5SYinghai Lu */ 180961e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 181061e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 1811cb21bc94SChristian König fail_res->flags & PCI_RES_TYPE_MASK, 1812da7822e5SYinghai Lu rel_type); 181361e83cddSYinghai Lu 18140d607618SNicholas Johnson /* Restore size and flags */ 1815b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1816b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 18179db8dc6dSLogan Gunthorpe int idx; 1818da7822e5SYinghai Lu 1819b9b0bba9SYinghai Lu res->start = fail_res->start; 1820b9b0bba9SYinghai Lu res->end = fail_res->end; 1821b9b0bba9SYinghai Lu res->flags = fail_res->flags; 18229db8dc6dSLogan Gunthorpe 18239db8dc6dSLogan Gunthorpe if (pci_is_bridge(fail_res->dev)) { 18249db8dc6dSLogan Gunthorpe idx = res - &fail_res->dev->resource[0]; 18259db8dc6dSLogan Gunthorpe if (idx >= PCI_BRIDGE_RESOURCES && 18269db8dc6dSLogan Gunthorpe idx <= PCI_BRIDGE_RESOURCE_END) 1827da7822e5SYinghai Lu res->flags = 0; 1828da7822e5SYinghai Lu } 18299db8dc6dSLogan Gunthorpe } 1830bffc56d4SYinghai Lu free_list(&fail_head); 1831da7822e5SYinghai Lu 1832da7822e5SYinghai Lu goto again; 1833da7822e5SYinghai Lu 1834928bea96SYinghai Lu dump: 18350d607618SNicholas Johnson /* Dump the resource on buses */ 183676fbc263SYinghai Lu pci_bus_dump_resources(bus); 183776fbc263SYinghai Lu } 18386841ec68SYinghai Lu 183955ed83a6SYinghai Lu void __init pci_assign_unassigned_resources(void) 184055ed83a6SYinghai Lu { 184155ed83a6SYinghai Lu struct pci_bus *root_bus; 184255ed83a6SYinghai Lu 1843584c5c42SRui Wang list_for_each_entry(root_bus, &pci_root_buses, node) { 184455ed83a6SYinghai Lu pci_assign_unassigned_root_bus_resources(root_bus); 1845d9c149d6SRui Wang 18460d607618SNicholas Johnson /* Make sure the root bridge has a companion ACPI device */ 1847d9c149d6SRui Wang if (ACPI_HANDLE(root_bus->bridge)) 1848584c5c42SRui Wang acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge)); 1849584c5c42SRui Wang } 185055ed83a6SYinghai Lu } 185155ed83a6SYinghai Lu 18521e58f4e1SNicholas Johnson static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res, 18530d607618SNicholas Johnson struct list_head *add_list, 18543d264da9SNicholas Johnson resource_size_t new_size) 18551a576772SMika Westerberg { 185694867573SNicholas Johnson resource_size_t add_size, size = resource_size(res); 18571a576772SMika Westerberg 18581a576772SMika Westerberg if (res->parent) 18591a576772SMika Westerberg return; 18601a576772SMika Westerberg 186194867573SNicholas Johnson if (!new_size) 18621a576772SMika Westerberg return; 18631a576772SMika Westerberg 186494867573SNicholas Johnson if (new_size > size) { 186594867573SNicholas Johnson add_size = new_size - size; 186694867573SNicholas Johnson pci_dbg(bridge, "bridge window %pR extended by %pa\n", res, 186794867573SNicholas Johnson &add_size); 186894867573SNicholas Johnson } else if (new_size < size) { 186994867573SNicholas Johnson add_size = size - new_size; 187094867573SNicholas Johnson pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res, 187194867573SNicholas Johnson &add_size); 187294867573SNicholas Johnson } 187394867573SNicholas Johnson 1874ae4611f1SNicholas Johnson res->end = res->start + new_size - 1; 1875ae4611f1SNicholas Johnson remove_from_list(add_list, res); 18761a576772SMika Westerberg } 18771a576772SMika Westerberg 18781a576772SMika Westerberg static void pci_bus_distribute_available_resources(struct pci_bus *bus, 18790d607618SNicholas Johnson struct list_head *add_list, 1880d555a50fSNicholas Johnson struct resource io, 1881d555a50fSNicholas Johnson struct resource mmio, 1882d555a50fSNicholas Johnson struct resource mmio_pref) 18831a576772SMika Westerberg { 18841a576772SMika Westerberg unsigned int normal_bridges = 0, hotplug_bridges = 0; 18851a576772SMika Westerberg struct resource *io_res, *mmio_res, *mmio_pref_res; 18861a576772SMika Westerberg struct pci_dev *dev, *bridge = bus->self; 1887f924c26eSNicholas Johnson resource_size_t io_per_hp, mmio_per_hp, mmio_pref_per_hp, align; 18881a576772SMika Westerberg 18891a576772SMika Westerberg io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0]; 18901a576772SMika Westerberg mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1]; 18911a576772SMika Westerberg mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2]; 18921a576772SMika Westerberg 18931a576772SMika Westerberg /* 1894f924c26eSNicholas Johnson * The alignment of this bridge is yet to be considered, hence it must 1895f924c26eSNicholas Johnson * be done now before extending its bridge window. 1896f924c26eSNicholas Johnson */ 1897f924c26eSNicholas Johnson align = pci_resource_alignment(bridge, io_res); 1898f924c26eSNicholas Johnson if (!io_res->parent && align) 1899f924c26eSNicholas Johnson io.start = min(ALIGN(io.start, align), io.end + 1); 1900f924c26eSNicholas Johnson 1901f924c26eSNicholas Johnson align = pci_resource_alignment(bridge, mmio_res); 1902f924c26eSNicholas Johnson if (!mmio_res->parent && align) 1903f924c26eSNicholas Johnson mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1); 1904f924c26eSNicholas Johnson 1905f924c26eSNicholas Johnson align = pci_resource_alignment(bridge, mmio_pref_res); 1906f924c26eSNicholas Johnson if (!mmio_pref_res->parent && align) 1907f924c26eSNicholas Johnson mmio_pref.start = min(ALIGN(mmio_pref.start, align), 1908f924c26eSNicholas Johnson mmio_pref.end + 1); 1909f924c26eSNicholas Johnson 1910f924c26eSNicholas Johnson /* 1911ae4611f1SNicholas Johnson * Now that we have adjusted for alignment, update the bridge window 1912ae4611f1SNicholas Johnson * resources to fill as much remaining resource space as possible. 19131a576772SMika Westerberg */ 19141e58f4e1SNicholas Johnson adjust_bridge_window(bridge, io_res, add_list, resource_size(&io)); 19151e58f4e1SNicholas Johnson adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio)); 19161e58f4e1SNicholas Johnson adjust_bridge_window(bridge, mmio_pref_res, add_list, 191777793854SNicholas Johnson resource_size(&mmio_pref)); 19181a576772SMika Westerberg 19191a576772SMika Westerberg /* 19201a576772SMika Westerberg * Calculate how many hotplug bridges and normal bridges there 19211a576772SMika Westerberg * are on this bus. We will distribute the additional available 19221a576772SMika Westerberg * resources between hotplug bridges. 19231a576772SMika Westerberg */ 19241a576772SMika Westerberg for_each_pci_bridge(dev, bus) { 19251a576772SMika Westerberg if (dev->is_hotplug_bridge) 19261a576772SMika Westerberg hotplug_bridges++; 19271a576772SMika Westerberg else 19281a576772SMika Westerberg normal_bridges++; 19291a576772SMika Westerberg } 19301a576772SMika Westerberg 19315c6bcc34SNicholas Johnson /* 19325c6bcc34SNicholas Johnson * There is only one bridge on the bus so it gets all available 19335c6bcc34SNicholas Johnson * resources which it can then distribute to the possible hotplug 19345c6bcc34SNicholas Johnson * bridges below. 19355c6bcc34SNicholas Johnson */ 19365c6bcc34SNicholas Johnson if (hotplug_bridges + normal_bridges == 1) { 19375c6bcc34SNicholas Johnson dev = list_first_entry(&bus->devices, struct pci_dev, bus_list); 19383d67a2dbSNicholas Johnson if (dev->subordinate) 19395c6bcc34SNicholas Johnson pci_bus_distribute_available_resources(dev->subordinate, 1940d555a50fSNicholas Johnson add_list, io, mmio, mmio_pref); 19415c6bcc34SNicholas Johnson return; 19425c6bcc34SNicholas Johnson } 19435c6bcc34SNicholas Johnson 19446a381ea6SNicholas Johnson if (hotplug_bridges == 0) 19456a381ea6SNicholas Johnson return; 19466a381ea6SNicholas Johnson 19475c6bcc34SNicholas Johnson /* 19485c6bcc34SNicholas Johnson * Calculate the total amount of extra resource space we can 19495c6bcc34SNicholas Johnson * pass to bridges below this one. This is basically the 19505c6bcc34SNicholas Johnson * extra space reduced by the minimal required space for the 19515c6bcc34SNicholas Johnson * non-hotplug bridges. 19525c6bcc34SNicholas Johnson */ 19531a576772SMika Westerberg for_each_pci_bridge(dev, bus) { 1954f924c26eSNicholas Johnson resource_size_t used_size; 1955f924c26eSNicholas Johnson struct resource *res; 19561a576772SMika Westerberg 19571a576772SMika Westerberg if (dev->is_hotplug_bridge) 19581a576772SMika Westerberg continue; 19591a576772SMika Westerberg 19601a576772SMika Westerberg /* 19611a576772SMika Westerberg * Reduce the available resource space by what the 19621a576772SMika Westerberg * bridge and devices below it occupy. 19631a576772SMika Westerberg */ 19641a576772SMika Westerberg res = &dev->resource[PCI_BRIDGE_RESOURCES + 0]; 1965f924c26eSNicholas Johnson align = pci_resource_alignment(dev, res); 1966f924c26eSNicholas Johnson align = align ? ALIGN(io.start, align) - io.start : 0; 1967f924c26eSNicholas Johnson used_size = align + resource_size(res); 1968f924c26eSNicholas Johnson if (!res->parent) 1969f924c26eSNicholas Johnson io.start = min(io.start + used_size, io.end + 1); 19701a576772SMika Westerberg 19711a576772SMika Westerberg res = &dev->resource[PCI_BRIDGE_RESOURCES + 1]; 1972f924c26eSNicholas Johnson align = pci_resource_alignment(dev, res); 1973f924c26eSNicholas Johnson align = align ? ALIGN(mmio.start, align) - mmio.start : 0; 1974f924c26eSNicholas Johnson used_size = align + resource_size(res); 1975f924c26eSNicholas Johnson if (!res->parent) 1976f924c26eSNicholas Johnson mmio.start = min(mmio.start + used_size, mmio.end + 1); 19771a576772SMika Westerberg 19781a576772SMika Westerberg res = &dev->resource[PCI_BRIDGE_RESOURCES + 2]; 1979f924c26eSNicholas Johnson align = pci_resource_alignment(dev, res); 1980f924c26eSNicholas Johnson align = align ? ALIGN(mmio_pref.start, align) - 1981f924c26eSNicholas Johnson mmio_pref.start : 0; 1982f924c26eSNicholas Johnson used_size = align + resource_size(res); 1983f924c26eSNicholas Johnson if (!res->parent) 1984f924c26eSNicholas Johnson mmio_pref.start = min(mmio_pref.start + used_size, 1985f924c26eSNicholas Johnson mmio_pref.end + 1); 19861a576772SMika Westerberg } 19871a576772SMika Westerberg 1988f924c26eSNicholas Johnson io_per_hp = div64_ul(resource_size(&io), hotplug_bridges); 1989f924c26eSNicholas Johnson mmio_per_hp = div64_ul(resource_size(&mmio), hotplug_bridges); 1990f924c26eSNicholas Johnson mmio_pref_per_hp = div64_ul(resource_size(&mmio_pref), 1991f924c26eSNicholas Johnson hotplug_bridges); 1992f924c26eSNicholas Johnson 19931a576772SMika Westerberg /* 19941a576772SMika Westerberg * Go over devices on this bus and distribute the remaining 19951a576772SMika Westerberg * resource space between hotplug bridges. 19961a576772SMika Westerberg */ 19971a576772SMika Westerberg for_each_pci_bridge(dev, bus) { 19981a576772SMika Westerberg struct pci_bus *b; 19991a576772SMika Westerberg 20001a576772SMika Westerberg b = dev->subordinate; 200114fe5951SMika Westerberg if (!b || !dev->is_hotplug_bridge) 20021a576772SMika Westerberg continue; 20031a576772SMika Westerberg 20041a576772SMika Westerberg /* 200514fe5951SMika Westerberg * Distribute available extra resources equally between 200614fe5951SMika Westerberg * hotplug-capable downstream ports taking alignment into 200714fe5951SMika Westerberg * account. 20081a576772SMika Westerberg */ 2009d555a50fSNicholas Johnson io.end = io.start + io_per_hp - 1; 2010d555a50fSNicholas Johnson mmio.end = mmio.start + mmio_per_hp - 1; 2011d555a50fSNicholas Johnson mmio_pref.end = mmio_pref.start + mmio_pref_per_hp - 1; 2012d555a50fSNicholas Johnson 2013d555a50fSNicholas Johnson pci_bus_distribute_available_resources(b, add_list, io, mmio, 2014d555a50fSNicholas Johnson mmio_pref); 2015f924c26eSNicholas Johnson 2016f924c26eSNicholas Johnson io.start += io_per_hp; 2017f924c26eSNicholas Johnson mmio.start += mmio_per_hp; 2018f924c26eSNicholas Johnson mmio_pref.start += mmio_pref_per_hp; 20191a576772SMika Westerberg } 20201a576772SMika Westerberg } 20211a576772SMika Westerberg 20220d607618SNicholas Johnson static void pci_bridge_distribute_available_resources(struct pci_dev *bridge, 20231a576772SMika Westerberg struct list_head *add_list) 20241a576772SMika Westerberg { 2025d555a50fSNicholas Johnson struct resource available_io, available_mmio, available_mmio_pref; 20261a576772SMika Westerberg 20271a576772SMika Westerberg if (!bridge->is_hotplug_bridge) 20281a576772SMika Westerberg return; 20291a576772SMika Westerberg 20301a576772SMika Westerberg /* Take the initial extra resources from the hotplug port */ 2031d555a50fSNicholas Johnson available_io = bridge->resource[PCI_BRIDGE_RESOURCES + 0]; 2032d555a50fSNicholas Johnson available_mmio = bridge->resource[PCI_BRIDGE_RESOURCES + 1]; 2033d555a50fSNicholas Johnson available_mmio_pref = bridge->resource[PCI_BRIDGE_RESOURCES + 2]; 20341a576772SMika Westerberg 20351a576772SMika Westerberg pci_bus_distribute_available_resources(bridge->subordinate, 20360d607618SNicholas Johnson add_list, available_io, 20370d607618SNicholas Johnson available_mmio, 20380d607618SNicholas Johnson available_mmio_pref); 20391a576772SMika Westerberg } 20401a576772SMika Westerberg 20416841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 20426841ec68SYinghai Lu { 20436841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 20440d607618SNicholas Johnson /* List of resources that want additional resources */ 20450d607618SNicholas Johnson LIST_HEAD(add_list); 20460d607618SNicholas Johnson 204732180e40SYinghai Lu int tried_times = 0; 2048bdc4abecSYinghai Lu LIST_HEAD(fail_head); 2049b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 20506841ec68SYinghai Lu int retval; 20516841ec68SYinghai Lu 205232180e40SYinghai Lu again: 20538424d759SYinghai Lu __pci_bus_size_bridges(parent, &add_list); 20541a576772SMika Westerberg 20551a576772SMika Westerberg /* 20560d607618SNicholas Johnson * Distribute remaining resources (if any) equally between hotplug 20570d607618SNicholas Johnson * bridges below. This makes it possible to extend the hierarchy 20580d607618SNicholas Johnson * later without running out of resources. 20591a576772SMika Westerberg */ 20601a576772SMika Westerberg pci_bridge_distribute_available_resources(bridge, &add_list); 20611a576772SMika Westerberg 2062bdc4abecSYinghai Lu __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 2063bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 206432180e40SYinghai Lu tried_times++; 206532180e40SYinghai Lu 2066bdc4abecSYinghai Lu if (list_empty(&fail_head)) 20673f579c34SYinghai Lu goto enable_all; 206832180e40SYinghai Lu 206932180e40SYinghai Lu if (tried_times >= 2) { 20700d607618SNicholas Johnson /* Still fail, don't need to try more */ 2071bffc56d4SYinghai Lu free_list(&fail_head); 20723f579c34SYinghai Lu goto enable_all; 207332180e40SYinghai Lu } 207432180e40SYinghai Lu 207532180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 207632180e40SYinghai Lu tried_times + 1); 207732180e40SYinghai Lu 207832180e40SYinghai Lu /* 20790d607618SNicholas Johnson * Try to release leaf bridge's resources that aren't big enough 20800d607618SNicholas Johnson * to contain child device resources. 208132180e40SYinghai Lu */ 208261e83cddSYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 208361e83cddSYinghai Lu pci_bus_release_bridge_resources(fail_res->dev->bus, 2084cb21bc94SChristian König fail_res->flags & PCI_RES_TYPE_MASK, 208532180e40SYinghai Lu whole_subtree); 208661e83cddSYinghai Lu 20870d607618SNicholas Johnson /* Restore size and flags */ 2088b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 2089b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 20909db8dc6dSLogan Gunthorpe int idx; 209132180e40SYinghai Lu 2092b9b0bba9SYinghai Lu res->start = fail_res->start; 2093b9b0bba9SYinghai Lu res->end = fail_res->end; 2094b9b0bba9SYinghai Lu res->flags = fail_res->flags; 20959db8dc6dSLogan Gunthorpe 20969db8dc6dSLogan Gunthorpe if (pci_is_bridge(fail_res->dev)) { 20979db8dc6dSLogan Gunthorpe idx = res - &fail_res->dev->resource[0]; 20989db8dc6dSLogan Gunthorpe if (idx >= PCI_BRIDGE_RESOURCES && 20999db8dc6dSLogan Gunthorpe idx <= PCI_BRIDGE_RESOURCE_END) 210032180e40SYinghai Lu res->flags = 0; 210132180e40SYinghai Lu } 21029db8dc6dSLogan Gunthorpe } 2103bffc56d4SYinghai Lu free_list(&fail_head); 210432180e40SYinghai Lu 210532180e40SYinghai Lu goto again; 21063f579c34SYinghai Lu 21073f579c34SYinghai Lu enable_all: 21083f579c34SYinghai Lu retval = pci_reenable_device(bridge); 21099fc9eea0SBjorn Helgaas if (retval) 21107506dc79SFrederick Lawler pci_err(bridge, "Error reenabling bridge (%d)\n", retval); 21113f579c34SYinghai Lu pci_set_master(bridge); 21126841ec68SYinghai Lu } 21136841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 21149b03088fSYinghai Lu 21158bb705e3SChristian König int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type) 21168bb705e3SChristian König { 21178bb705e3SChristian König struct pci_dev_resource *dev_res; 21188bb705e3SChristian König struct pci_dev *next; 21198bb705e3SChristian König LIST_HEAD(saved); 21208bb705e3SChristian König LIST_HEAD(added); 21218bb705e3SChristian König LIST_HEAD(failed); 21228bb705e3SChristian König unsigned int i; 21238bb705e3SChristian König int ret; 21248bb705e3SChristian König 2125fb794a70SBenjamin Herrenschmidt down_read(&pci_bus_sem); 2126fb794a70SBenjamin Herrenschmidt 21278bb705e3SChristian König /* Walk to the root hub, releasing bridge BARs when possible */ 21288bb705e3SChristian König next = bridge; 21298bb705e3SChristian König do { 21308bb705e3SChristian König bridge = next; 21318bb705e3SChristian König for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END; 21328bb705e3SChristian König i++) { 21338bb705e3SChristian König struct resource *res = &bridge->resource[i]; 21348bb705e3SChristian König 21358bb705e3SChristian König if ((res->flags ^ type) & PCI_RES_TYPE_MASK) 21368bb705e3SChristian König continue; 21378bb705e3SChristian König 21388bb705e3SChristian König /* Ignore BARs which are still in use */ 21398bb705e3SChristian König if (res->child) 21408bb705e3SChristian König continue; 21418bb705e3SChristian König 21428bb705e3SChristian König ret = add_to_list(&saved, bridge, res, 0, 0); 21438bb705e3SChristian König if (ret) 21448bb705e3SChristian König goto cleanup; 21458bb705e3SChristian König 21467506dc79SFrederick Lawler pci_info(bridge, "BAR %d: releasing %pR\n", 21478bb705e3SChristian König i, res); 21488bb705e3SChristian König 21498bb705e3SChristian König if (res->parent) 21508bb705e3SChristian König release_resource(res); 21518bb705e3SChristian König res->start = 0; 21528bb705e3SChristian König res->end = 0; 21538bb705e3SChristian König break; 21548bb705e3SChristian König } 21558bb705e3SChristian König if (i == PCI_BRIDGE_RESOURCE_END) 21568bb705e3SChristian König break; 21578bb705e3SChristian König 21588bb705e3SChristian König next = bridge->bus ? bridge->bus->self : NULL; 21598bb705e3SChristian König } while (next); 21608bb705e3SChristian König 2161fb794a70SBenjamin Herrenschmidt if (list_empty(&saved)) { 2162fb794a70SBenjamin Herrenschmidt up_read(&pci_bus_sem); 21638bb705e3SChristian König return -ENOENT; 2164fb794a70SBenjamin Herrenschmidt } 21658bb705e3SChristian König 21668bb705e3SChristian König __pci_bus_size_bridges(bridge->subordinate, &added); 21678bb705e3SChristian König __pci_bridge_assign_resources(bridge, &added, &failed); 21688bb705e3SChristian König BUG_ON(!list_empty(&added)); 21698bb705e3SChristian König 21708bb705e3SChristian König if (!list_empty(&failed)) { 21718bb705e3SChristian König ret = -ENOSPC; 21728bb705e3SChristian König goto cleanup; 21738bb705e3SChristian König } 21748bb705e3SChristian König 21758bb705e3SChristian König list_for_each_entry(dev_res, &saved, list) { 21760d607618SNicholas Johnson /* Skip the bridge we just assigned resources for */ 21778bb705e3SChristian König if (bridge == dev_res->dev) 21788bb705e3SChristian König continue; 21798bb705e3SChristian König 21808bb705e3SChristian König bridge = dev_res->dev; 21818bb705e3SChristian König pci_setup_bridge(bridge->subordinate); 21828bb705e3SChristian König } 21838bb705e3SChristian König 21848bb705e3SChristian König free_list(&saved); 2185fb794a70SBenjamin Herrenschmidt up_read(&pci_bus_sem); 21868bb705e3SChristian König return 0; 21878bb705e3SChristian König 21888bb705e3SChristian König cleanup: 21890d607618SNicholas Johnson /* Restore size and flags */ 21908bb705e3SChristian König list_for_each_entry(dev_res, &failed, list) { 21918bb705e3SChristian König struct resource *res = dev_res->res; 21928bb705e3SChristian König 21938bb705e3SChristian König res->start = dev_res->start; 21948bb705e3SChristian König res->end = dev_res->end; 21958bb705e3SChristian König res->flags = dev_res->flags; 21968bb705e3SChristian König } 21978bb705e3SChristian König free_list(&failed); 21988bb705e3SChristian König 21998bb705e3SChristian König /* Revert to the old configuration */ 22008bb705e3SChristian König list_for_each_entry(dev_res, &saved, list) { 22018bb705e3SChristian König struct resource *res = dev_res->res; 22028bb705e3SChristian König 22038bb705e3SChristian König bridge = dev_res->dev; 22048bb705e3SChristian König i = res - bridge->resource; 22058bb705e3SChristian König 22068bb705e3SChristian König res->start = dev_res->start; 22078bb705e3SChristian König res->end = dev_res->end; 22088bb705e3SChristian König res->flags = dev_res->flags; 22098bb705e3SChristian König 22108bb705e3SChristian König pci_claim_resource(bridge, i); 22118bb705e3SChristian König pci_setup_bridge(bridge->subordinate); 22128bb705e3SChristian König } 22138bb705e3SChristian König free_list(&saved); 2214fb794a70SBenjamin Herrenschmidt up_read(&pci_bus_sem); 22158bb705e3SChristian König 22168bb705e3SChristian König return ret; 22178bb705e3SChristian König } 22188bb705e3SChristian König 221917787940SYinghai Lu void pci_assign_unassigned_bus_resources(struct pci_bus *bus) 22209b03088fSYinghai Lu { 22219b03088fSYinghai Lu struct pci_dev *dev; 22220d607618SNicholas Johnson /* List of resources that want additional resources */ 22230d607618SNicholas Johnson LIST_HEAD(add_list); 22249b03088fSYinghai Lu 22259b03088fSYinghai Lu down_read(&pci_bus_sem); 222624a0c654SAndy Shevchenko for_each_pci_bridge(dev, bus) 222724a0c654SAndy Shevchenko if (pci_has_subordinate(dev)) 222824a0c654SAndy Shevchenko __pci_bus_size_bridges(dev->subordinate, &add_list); 22299b03088fSYinghai Lu up_read(&pci_bus_sem); 22309b03088fSYinghai Lu __pci_bus_assign_resources(bus, &add_list, NULL); 2231bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 223217787940SYinghai Lu } 2233e6b29deaSRay Jui EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources); 2234