11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/pci/setup-bus.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Extruded from code written by 51da177e4SLinus Torvalds * Dave Rusling (david.rusling@reo.mts.dec.com) 61da177e4SLinus Torvalds * David Mosberger (davidm@cs.arizona.edu) 71da177e4SLinus Torvalds * David Miller (davem@redhat.com) 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Support routines for initializing a PCI subsystem. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 141da177e4SLinus Torvalds * PCI-PCI bridges cleanup, sorted resource allocation. 151da177e4SLinus Torvalds * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 161da177e4SLinus Torvalds * Converted to allocation in 3 passes, which gives 171da177e4SLinus Torvalds * tighter packing. Prefetchable range support. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/kernel.h> 221da177e4SLinus Torvalds #include <linux/module.h> 231da177e4SLinus Torvalds #include <linux/pci.h> 241da177e4SLinus Torvalds #include <linux/errno.h> 251da177e4SLinus Torvalds #include <linux/ioport.h> 261da177e4SLinus Torvalds #include <linux/cache.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 286faf17f6SChris Wright #include "pci.h" 291da177e4SLinus Torvalds 30bdc4abecSYinghai Lu struct pci_dev_resource { 31bdc4abecSYinghai Lu struct list_head list; 322934a0deSYinghai Lu struct resource *res; 332934a0deSYinghai Lu struct pci_dev *dev; 34568ddef8SYinghai Lu resource_size_t start; 35568ddef8SYinghai Lu resource_size_t end; 36c8adf9a3SRam Pai resource_size_t add_size; 372bbc6942SRam Pai resource_size_t min_align; 38568ddef8SYinghai Lu unsigned long flags; 39568ddef8SYinghai Lu }; 40568ddef8SYinghai Lu 41bffc56d4SYinghai Lu static void free_list(struct list_head *head) 42bffc56d4SYinghai Lu { 43bffc56d4SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 44bffc56d4SYinghai Lu 45bffc56d4SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 46bffc56d4SYinghai Lu list_del(&dev_res->list); 47bffc56d4SYinghai Lu kfree(dev_res); 48bffc56d4SYinghai Lu } 49bffc56d4SYinghai Lu } 50094732a5SRam Pai 51f483d392SRam Pai int pci_realloc_enable = 0; 52f483d392SRam Pai #define pci_realloc_enabled() pci_realloc_enable 53f483d392SRam Pai void pci_realloc(void) 54f483d392SRam Pai { 55f483d392SRam Pai pci_realloc_enable = 1; 56f483d392SRam Pai } 57f483d392SRam Pai 58c8adf9a3SRam Pai /** 59c8adf9a3SRam Pai * add_to_list() - add a new resource tracker to the list 60c8adf9a3SRam Pai * @head: Head of the list 61c8adf9a3SRam Pai * @dev: device corresponding to which the resource 62c8adf9a3SRam Pai * belongs 63c8adf9a3SRam Pai * @res: The resource to be tracked 64c8adf9a3SRam Pai * @add_size: additional size to be optionally added 65c8adf9a3SRam Pai * to the resource 66c8adf9a3SRam Pai */ 67bdc4abecSYinghai Lu static int add_to_list(struct list_head *head, 68c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res, 692bbc6942SRam Pai resource_size_t add_size, resource_size_t min_align) 70568ddef8SYinghai Lu { 71764242a0SYinghai Lu struct pci_dev_resource *tmp; 72568ddef8SYinghai Lu 73bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 74568ddef8SYinghai Lu if (!tmp) { 75c8adf9a3SRam Pai pr_warning("add_to_list: kmalloc() failed!\n"); 76ef62dfefSYinghai Lu return -ENOMEM; 77568ddef8SYinghai Lu } 78568ddef8SYinghai Lu 79568ddef8SYinghai Lu tmp->res = res; 80568ddef8SYinghai Lu tmp->dev = dev; 81568ddef8SYinghai Lu tmp->start = res->start; 82568ddef8SYinghai Lu tmp->end = res->end; 83568ddef8SYinghai Lu tmp->flags = res->flags; 84c8adf9a3SRam Pai tmp->add_size = add_size; 852bbc6942SRam Pai tmp->min_align = min_align; 86bdc4abecSYinghai Lu 87bdc4abecSYinghai Lu list_add(&tmp->list, head); 88ef62dfefSYinghai Lu 89ef62dfefSYinghai Lu return 0; 90568ddef8SYinghai Lu } 91568ddef8SYinghai Lu 92bdc4abecSYinghai Lu static void add_to_failed_list(struct list_head *head, 93c8adf9a3SRam Pai struct pci_dev *dev, struct resource *res) 94c8adf9a3SRam Pai { 952bbc6942SRam Pai add_to_list(head, dev, res, 962bbc6942SRam Pai 0 /* dont care */, 972bbc6942SRam Pai 0 /* dont care */); 98c8adf9a3SRam Pai } 99c8adf9a3SRam Pai 100b9b0bba9SYinghai Lu static void remove_from_list(struct list_head *head, 1013e6e0d80SYinghai Lu struct resource *res) 1023e6e0d80SYinghai Lu { 103b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res, *tmp; 1043e6e0d80SYinghai Lu 105b9b0bba9SYinghai Lu list_for_each_entry_safe(dev_res, tmp, head, list) { 106b9b0bba9SYinghai Lu if (dev_res->res == res) { 107b9b0bba9SYinghai Lu list_del(&dev_res->list); 108b9b0bba9SYinghai Lu kfree(dev_res); 109bdc4abecSYinghai Lu break; 1103e6e0d80SYinghai Lu } 1113e6e0d80SYinghai Lu } 1123e6e0d80SYinghai Lu } 1133e6e0d80SYinghai Lu 114b9b0bba9SYinghai Lu static resource_size_t get_res_add_size(struct list_head *head, 1151c372353SYinghai Lu struct resource *res) 1161c372353SYinghai Lu { 117b9b0bba9SYinghai Lu struct pci_dev_resource *dev_res; 1181c372353SYinghai Lu 119b9b0bba9SYinghai Lu list_for_each_entry(dev_res, head, list) { 120b9b0bba9SYinghai Lu if (dev_res->res == res) { 121*b592443dSYinghai Lu int idx = res - &dev_res->dev->resource[0]; 122*b592443dSYinghai Lu 123b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &dev_res->dev->dev, 124*b592443dSYinghai Lu "res[%d]=%pR get_res_add_size add_size %llx\n", 125*b592443dSYinghai Lu idx, dev_res->res, 126b9b0bba9SYinghai Lu (unsigned long long)dev_res->add_size); 127*b592443dSYinghai Lu 128b9b0bba9SYinghai Lu return dev_res->add_size; 129bdc4abecSYinghai Lu } 1303e6e0d80SYinghai Lu } 1311c372353SYinghai Lu 1321c372353SYinghai Lu return 0; 1331c372353SYinghai Lu } 1341c372353SYinghai Lu 13578c3b329SYinghai Lu /* Sort resources by alignment */ 136bdc4abecSYinghai Lu static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 13778c3b329SYinghai Lu { 13878c3b329SYinghai Lu int i; 13978c3b329SYinghai Lu 14078c3b329SYinghai Lu for (i = 0; i < PCI_NUM_RESOURCES; i++) { 14178c3b329SYinghai Lu struct resource *r; 142bdc4abecSYinghai Lu struct pci_dev_resource *dev_res, *tmp; 14378c3b329SYinghai Lu resource_size_t r_align; 144bdc4abecSYinghai Lu struct list_head *n; 14578c3b329SYinghai Lu 14678c3b329SYinghai Lu r = &dev->resource[i]; 14778c3b329SYinghai Lu 14878c3b329SYinghai Lu if (r->flags & IORESOURCE_PCI_FIXED) 14978c3b329SYinghai Lu continue; 15078c3b329SYinghai Lu 15178c3b329SYinghai Lu if (!(r->flags) || r->parent) 15278c3b329SYinghai Lu continue; 15378c3b329SYinghai Lu 15478c3b329SYinghai Lu r_align = pci_resource_alignment(dev, r); 15578c3b329SYinghai Lu if (!r_align) { 15678c3b329SYinghai Lu dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n", 15778c3b329SYinghai Lu i, r); 15878c3b329SYinghai Lu continue; 15978c3b329SYinghai Lu } 16078c3b329SYinghai Lu 161bdc4abecSYinghai Lu tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 16278c3b329SYinghai Lu if (!tmp) 16378c3b329SYinghai Lu panic("pdev_sort_resources(): " 16478c3b329SYinghai Lu "kmalloc() failed!\n"); 16578c3b329SYinghai Lu tmp->res = r; 16678c3b329SYinghai Lu tmp->dev = dev; 167bdc4abecSYinghai Lu 168bdc4abecSYinghai Lu /* fallback is smallest one or list is empty*/ 169bdc4abecSYinghai Lu n = head; 170bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 171bdc4abecSYinghai Lu resource_size_t align; 172bdc4abecSYinghai Lu 173bdc4abecSYinghai Lu align = pci_resource_alignment(dev_res->dev, 174bdc4abecSYinghai Lu dev_res->res); 175bdc4abecSYinghai Lu 176bdc4abecSYinghai Lu if (r_align > align) { 177bdc4abecSYinghai Lu n = &dev_res->list; 17878c3b329SYinghai Lu break; 17978c3b329SYinghai Lu } 18078c3b329SYinghai Lu } 181bdc4abecSYinghai Lu /* Insert it just before n*/ 182bdc4abecSYinghai Lu list_add_tail(&tmp->list, n); 18378c3b329SYinghai Lu } 18478c3b329SYinghai Lu } 18578c3b329SYinghai Lu 1866841ec68SYinghai Lu static void __dev_sort_resources(struct pci_dev *dev, 187bdc4abecSYinghai Lu struct list_head *head) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds u16 class = dev->class >> 8; 1901da177e4SLinus Torvalds 1919bded00bSKenji Kaneshige /* Don't touch classless devices or host bridges or ioapics. */ 1926841ec68SYinghai Lu if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 1936841ec68SYinghai Lu return; 1941da177e4SLinus Torvalds 1959bded00bSKenji Kaneshige /* Don't touch ioapic devices already enabled by firmware */ 19623186279SSatoru Takeuchi if (class == PCI_CLASS_SYSTEM_PIC) { 1979bded00bSKenji Kaneshige u16 command; 1989bded00bSKenji Kaneshige pci_read_config_word(dev, PCI_COMMAND, &command); 1999bded00bSKenji Kaneshige if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 2006841ec68SYinghai Lu return; 20123186279SSatoru Takeuchi } 20223186279SSatoru Takeuchi 2036841ec68SYinghai Lu pdev_sort_resources(dev, head); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 206fc075e1dSRam Pai static inline void reset_resource(struct resource *res) 207fc075e1dSRam Pai { 208fc075e1dSRam Pai res->start = 0; 209fc075e1dSRam Pai res->end = 0; 210fc075e1dSRam Pai res->flags = 0; 211fc075e1dSRam Pai } 212fc075e1dSRam Pai 213c8adf9a3SRam Pai /** 2149e8bf93aSRam Pai * reassign_resources_sorted() - satisfy any additional resource requests 215c8adf9a3SRam Pai * 2169e8bf93aSRam Pai * @realloc_head : head of the list tracking requests requiring additional 217c8adf9a3SRam Pai * resources 218c8adf9a3SRam Pai * @head : head of the list tracking requests with allocated 219c8adf9a3SRam Pai * resources 220c8adf9a3SRam Pai * 2219e8bf93aSRam Pai * Walk through each element of the realloc_head and try to procure 222c8adf9a3SRam Pai * additional resources for the element, provided the element 223c8adf9a3SRam Pai * is in the head list. 224c8adf9a3SRam Pai */ 225bdc4abecSYinghai Lu static void reassign_resources_sorted(struct list_head *realloc_head, 226bdc4abecSYinghai Lu struct list_head *head) 227c8adf9a3SRam Pai { 228c8adf9a3SRam Pai struct resource *res; 229b9b0bba9SYinghai Lu struct pci_dev_resource *add_res, *tmp; 230bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 231c8adf9a3SRam Pai resource_size_t add_size; 232c8adf9a3SRam Pai int idx; 233c8adf9a3SRam Pai 234b9b0bba9SYinghai Lu list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 235bdc4abecSYinghai Lu bool found_match = false; 236bdc4abecSYinghai Lu 237b9b0bba9SYinghai Lu res = add_res->res; 238c8adf9a3SRam Pai /* skip resource that has been reset */ 239c8adf9a3SRam Pai if (!res->flags) 240c8adf9a3SRam Pai goto out; 241c8adf9a3SRam Pai 242c8adf9a3SRam Pai /* skip this resource if not found in head list */ 243bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 244bdc4abecSYinghai Lu if (dev_res->res == res) { 245bdc4abecSYinghai Lu found_match = true; 246bdc4abecSYinghai Lu break; 247c8adf9a3SRam Pai } 248bdc4abecSYinghai Lu } 249bdc4abecSYinghai Lu if (!found_match)/* just skip */ 250bdc4abecSYinghai Lu continue; 251c8adf9a3SRam Pai 252b9b0bba9SYinghai Lu idx = res - &add_res->dev->resource[0]; 253b9b0bba9SYinghai Lu add_size = add_res->add_size; 2542bbc6942SRam Pai if (!resource_size(res)) { 255b9b0bba9SYinghai Lu res->start = add_res->start; 256c8adf9a3SRam Pai res->end = res->start + add_size - 1; 257b9b0bba9SYinghai Lu if (pci_assign_resource(add_res->dev, idx)) 258c8adf9a3SRam Pai reset_resource(res); 2592bbc6942SRam Pai } else { 260b9b0bba9SYinghai Lu resource_size_t align = add_res->min_align; 261b9b0bba9SYinghai Lu res->flags |= add_res->flags & 262bdc4abecSYinghai Lu (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 263b9b0bba9SYinghai Lu if (pci_reassign_resource(add_res->dev, idx, 264bdc4abecSYinghai Lu add_size, align)) 265b9b0bba9SYinghai Lu dev_printk(KERN_DEBUG, &add_res->dev->dev, 266*b592443dSYinghai Lu "failed to add %llx res[%d]=%pR\n", 267*b592443dSYinghai Lu (unsigned long long)add_size, 268*b592443dSYinghai Lu idx, res); 269c8adf9a3SRam Pai } 270c8adf9a3SRam Pai out: 271b9b0bba9SYinghai Lu list_del(&add_res->list); 272b9b0bba9SYinghai Lu kfree(add_res); 273c8adf9a3SRam Pai } 274c8adf9a3SRam Pai } 275c8adf9a3SRam Pai 276c8adf9a3SRam Pai /** 277c8adf9a3SRam Pai * assign_requested_resources_sorted() - satisfy resource requests 278c8adf9a3SRam Pai * 279c8adf9a3SRam Pai * @head : head of the list tracking requests for resources 280c8adf9a3SRam Pai * @failed_list : head of the list tracking requests that could 281c8adf9a3SRam Pai * not be allocated 282c8adf9a3SRam Pai * 283c8adf9a3SRam Pai * Satisfy resource requests of each element in the list. Add 284c8adf9a3SRam Pai * requests that could not satisfied to the failed_list. 285c8adf9a3SRam Pai */ 286bdc4abecSYinghai Lu static void assign_requested_resources_sorted(struct list_head *head, 287bdc4abecSYinghai Lu struct list_head *fail_head) 2886841ec68SYinghai Lu { 2896841ec68SYinghai Lu struct resource *res; 290bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 2916841ec68SYinghai Lu int idx; 2926841ec68SYinghai Lu 293bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 294bdc4abecSYinghai Lu res = dev_res->res; 295bdc4abecSYinghai Lu idx = res - &dev_res->dev->resource[0]; 296bdc4abecSYinghai Lu if (resource_size(res) && 297bdc4abecSYinghai Lu pci_assign_resource(dev_res->dev, idx)) { 298bdc4abecSYinghai Lu if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) { 2999a928660SYinghai Lu /* 3009a928660SYinghai Lu * if the failed res is for ROM BAR, and it will 3019a928660SYinghai Lu * be enabled later, don't add it to the list 3029a928660SYinghai Lu */ 3039a928660SYinghai Lu if (!((idx == PCI_ROM_RESOURCE) && 3049a928660SYinghai Lu (!(res->flags & IORESOURCE_ROM_ENABLE)))) 305bdc4abecSYinghai Lu add_to_failed_list(fail_head, 306bdc4abecSYinghai Lu dev_res->dev, res); 3079a928660SYinghai Lu } 308fc075e1dSRam Pai reset_resource(res); 309542df5deSRajesh Shah } 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313bdc4abecSYinghai Lu static void __assign_resources_sorted(struct list_head *head, 314bdc4abecSYinghai Lu struct list_head *realloc_head, 315bdc4abecSYinghai Lu struct list_head *fail_head) 316c8adf9a3SRam Pai { 3173e6e0d80SYinghai Lu /* 3183e6e0d80SYinghai Lu * Should not assign requested resources at first. 3193e6e0d80SYinghai Lu * they could be adjacent, so later reassign can not reallocate 3203e6e0d80SYinghai Lu * them one by one in parent resource window. 3213e6e0d80SYinghai Lu * Try to assign requested + add_size at begining 3223e6e0d80SYinghai Lu * if could do that, could get out early. 3233e6e0d80SYinghai Lu * if could not do that, we still try to assign requested at first, 3243e6e0d80SYinghai Lu * then try to reassign add_size for some resources. 3253e6e0d80SYinghai Lu */ 326bdc4abecSYinghai Lu LIST_HEAD(save_head); 327bdc4abecSYinghai Lu LIST_HEAD(local_fail_head); 328b9b0bba9SYinghai Lu struct pci_dev_resource *save_res; 329bdc4abecSYinghai Lu struct pci_dev_resource *dev_res; 3303e6e0d80SYinghai Lu 3313e6e0d80SYinghai Lu /* Check if optional add_size is there */ 332bdc4abecSYinghai Lu if (!realloc_head || list_empty(realloc_head)) 3333e6e0d80SYinghai Lu goto requested_and_reassign; 3343e6e0d80SYinghai Lu 3353e6e0d80SYinghai Lu /* Save original start, end, flags etc at first */ 336bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) { 337bdc4abecSYinghai Lu if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 338bffc56d4SYinghai Lu free_list(&save_head); 3393e6e0d80SYinghai Lu goto requested_and_reassign; 3403e6e0d80SYinghai Lu } 341bdc4abecSYinghai Lu } 3423e6e0d80SYinghai Lu 3433e6e0d80SYinghai Lu /* Update res in head list with add_size in realloc_head list */ 344bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 345bdc4abecSYinghai Lu dev_res->res->end += get_res_add_size(realloc_head, 346bdc4abecSYinghai Lu dev_res->res); 3473e6e0d80SYinghai Lu 3483e6e0d80SYinghai Lu /* Try updated head list with add_size added */ 3493e6e0d80SYinghai Lu assign_requested_resources_sorted(head, &local_fail_head); 3503e6e0d80SYinghai Lu 3513e6e0d80SYinghai Lu /* all assigned with add_size ? */ 352bdc4abecSYinghai Lu if (list_empty(&local_fail_head)) { 3533e6e0d80SYinghai Lu /* Remove head list from realloc_head list */ 354bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 355bdc4abecSYinghai Lu remove_from_list(realloc_head, dev_res->res); 356bffc56d4SYinghai Lu free_list(&save_head); 357bffc56d4SYinghai Lu free_list(head); 3583e6e0d80SYinghai Lu return; 3593e6e0d80SYinghai Lu } 3603e6e0d80SYinghai Lu 361bffc56d4SYinghai Lu free_list(&local_fail_head); 3623e6e0d80SYinghai Lu /* Release assigned resource */ 363bdc4abecSYinghai Lu list_for_each_entry(dev_res, head, list) 364bdc4abecSYinghai Lu if (dev_res->res->parent) 365bdc4abecSYinghai Lu release_resource(dev_res->res); 3663e6e0d80SYinghai Lu /* Restore start/end/flags from saved list */ 367b9b0bba9SYinghai Lu list_for_each_entry(save_res, &save_head, list) { 368b9b0bba9SYinghai Lu struct resource *res = save_res->res; 3693e6e0d80SYinghai Lu 370b9b0bba9SYinghai Lu res->start = save_res->start; 371b9b0bba9SYinghai Lu res->end = save_res->end; 372b9b0bba9SYinghai Lu res->flags = save_res->flags; 3733e6e0d80SYinghai Lu } 374bffc56d4SYinghai Lu free_list(&save_head); 3753e6e0d80SYinghai Lu 3763e6e0d80SYinghai Lu requested_and_reassign: 377c8adf9a3SRam Pai /* Satisfy the must-have resource requests */ 378c8adf9a3SRam Pai assign_requested_resources_sorted(head, fail_head); 379c8adf9a3SRam Pai 3800a2daa1cSRam Pai /* Try to satisfy any additional optional resource 381c8adf9a3SRam Pai requests */ 3829e8bf93aSRam Pai if (realloc_head) 3839e8bf93aSRam Pai reassign_resources_sorted(realloc_head, head); 384bffc56d4SYinghai Lu free_list(head); 385c8adf9a3SRam Pai } 386c8adf9a3SRam Pai 3876841ec68SYinghai Lu static void pdev_assign_resources_sorted(struct pci_dev *dev, 388bdc4abecSYinghai Lu struct list_head *add_head, 389bdc4abecSYinghai Lu struct list_head *fail_head) 3906841ec68SYinghai Lu { 391bdc4abecSYinghai Lu LIST_HEAD(head); 3926841ec68SYinghai Lu 3936841ec68SYinghai Lu __dev_sort_resources(dev, &head); 3948424d759SYinghai Lu __assign_resources_sorted(&head, add_head, fail_head); 3956841ec68SYinghai Lu 3966841ec68SYinghai Lu } 3976841ec68SYinghai Lu 3986841ec68SYinghai Lu static void pbus_assign_resources_sorted(const struct pci_bus *bus, 399bdc4abecSYinghai Lu struct list_head *realloc_head, 400bdc4abecSYinghai Lu struct list_head *fail_head) 4016841ec68SYinghai Lu { 4026841ec68SYinghai Lu struct pci_dev *dev; 403bdc4abecSYinghai Lu LIST_HEAD(head); 4046841ec68SYinghai Lu 4056841ec68SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 4066841ec68SYinghai Lu __dev_sort_resources(dev, &head); 4076841ec68SYinghai Lu 4089e8bf93aSRam Pai __assign_resources_sorted(&head, realloc_head, fail_head); 4096841ec68SYinghai Lu } 4106841ec68SYinghai Lu 411b3743fa4SDominik Brodowski void pci_setup_cardbus(struct pci_bus *bus) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 414c7dabef8SBjorn Helgaas struct resource *res; 4151da177e4SLinus Torvalds struct pci_bus_region region; 4161da177e4SLinus Torvalds 417865df576SBjorn Helgaas dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", 418865df576SBjorn Helgaas bus->secondary, bus->subordinate); 4191da177e4SLinus Torvalds 420c7dabef8SBjorn Helgaas res = bus->resource[0]; 421c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 422c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 4231da177e4SLinus Torvalds /* 4241da177e4SLinus Torvalds * The IO resource is allocated a range twice as large as it 4251da177e4SLinus Torvalds * would normally need. This allows us to set both IO regs. 4261da177e4SLinus Torvalds */ 427c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4281da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 4291da177e4SLinus Torvalds region.start); 4301da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 4311da177e4SLinus Torvalds region.end); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 434c7dabef8SBjorn Helgaas res = bus->resource[1]; 435c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 436c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 437c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4381da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 4391da177e4SLinus Torvalds region.start); 4401da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 4411da177e4SLinus Torvalds region.end); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 444c7dabef8SBjorn Helgaas res = bus->resource[2]; 445c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 446c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 447c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4481da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 4491da177e4SLinus Torvalds region.start); 4501da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 4511da177e4SLinus Torvalds region.end); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 454c7dabef8SBjorn Helgaas res = bus->resource[3]; 455c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 456c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 457c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4581da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 4591da177e4SLinus Torvalds region.start); 4601da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 4611da177e4SLinus Torvalds region.end); 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds } 464b3743fa4SDominik Brodowski EXPORT_SYMBOL(pci_setup_cardbus); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /* Initialize bridges with base/limit values we have collected. 4671da177e4SLinus Torvalds PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 4681da177e4SLinus Torvalds requires that if there is no I/O ports or memory behind the 4691da177e4SLinus Torvalds bridge, corresponding range must be turned off by writing base 4701da177e4SLinus Torvalds value greater than limit to the bridge's base/limit registers. 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds Note: care must be taken when updating I/O base/limit registers 4731da177e4SLinus Torvalds of bridges which support 32-bit I/O. This update requires two 4741da177e4SLinus Torvalds config space writes, so it's quite possible that an I/O window of 4751da177e4SLinus Torvalds the bridge will have some undesirable address (e.g. 0) after the 4761da177e4SLinus Torvalds first write. Ditto 64-bit prefetchable MMIO. */ 4777cc5997dSYinghai Lu static void pci_setup_bridge_io(struct pci_bus *bus) 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 480c7dabef8SBjorn Helgaas struct resource *res; 4811da177e4SLinus Torvalds struct pci_bus_region region; 4827cc5997dSYinghai Lu u32 l, io_upper16; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* Set up the top and bottom of the PCI I/O segment for this bus. */ 485c7dabef8SBjorn Helgaas res = bus->resource[0]; 486c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 487c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 4881da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_IO_BASE, &l); 4891da177e4SLinus Torvalds l &= 0xffff0000; 4901da177e4SLinus Torvalds l |= (region.start >> 8) & 0x00f0; 4911da177e4SLinus Torvalds l |= region.end & 0xf000; 4921da177e4SLinus Torvalds /* Set up upper 16 bits of I/O base/limit. */ 4931da177e4SLinus Torvalds io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 494c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 4957cc5997dSYinghai Lu } else { 4961da177e4SLinus Torvalds /* Clear upper 16 bits of I/O base/limit. */ 4971da177e4SLinus Torvalds io_upper16 = 0; 4981da177e4SLinus Torvalds l = 0x00f0; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 5011da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 5021da177e4SLinus Torvalds /* Update lower 16 bits of I/O base/limit. */ 5031da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE, l); 5041da177e4SLinus Torvalds /* Update upper 16 bits of I/O base/limit. */ 5051da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 5067cc5997dSYinghai Lu } 5071da177e4SLinus Torvalds 5087cc5997dSYinghai Lu static void pci_setup_bridge_mmio(struct pci_bus *bus) 5097cc5997dSYinghai Lu { 5107cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5117cc5997dSYinghai Lu struct resource *res; 5127cc5997dSYinghai Lu struct pci_bus_region region; 5137cc5997dSYinghai Lu u32 l; 5147cc5997dSYinghai Lu 5157cc5997dSYinghai Lu /* Set up the top and bottom of the PCI Memory segment for this bus. */ 516c7dabef8SBjorn Helgaas res = bus->resource[1]; 517c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 518c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM) { 5191da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 5201da177e4SLinus Torvalds l |= region.end & 0xfff00000; 521c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5227cc5997dSYinghai Lu } else { 5231da177e4SLinus Torvalds l = 0x0000fff0; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 5267cc5997dSYinghai Lu } 5277cc5997dSYinghai Lu 5287cc5997dSYinghai Lu static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) 5297cc5997dSYinghai Lu { 5307cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5317cc5997dSYinghai Lu struct resource *res; 5327cc5997dSYinghai Lu struct pci_bus_region region; 5337cc5997dSYinghai Lu u32 l, bu, lu; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* Clear out the upper 32 bits of PREF limit. 5361da177e4SLinus Torvalds If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 5371da177e4SLinus Torvalds disables PREF range, which is ok. */ 5381da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* Set up PREF base/limit. */ 541c40a22e0SBenjamin Herrenschmidt bu = lu = 0; 542c7dabef8SBjorn Helgaas res = bus->resource[2]; 543c7dabef8SBjorn Helgaas pcibios_resource_to_bus(bridge, ®ion, res); 544c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) { 5451da177e4SLinus Torvalds l = (region.start >> 16) & 0xfff0; 5461da177e4SLinus Torvalds l |= region.end & 0xfff00000; 547c7dabef8SBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) { 54813d36c24SAndrew Morton bu = upper_32_bits(region.start); 54913d36c24SAndrew Morton lu = upper_32_bits(region.end); 5501f82de10SYinghai Lu } 551c7dabef8SBjorn Helgaas dev_info(&bridge->dev, " bridge window %pR\n", res); 5527cc5997dSYinghai Lu } else { 5531da177e4SLinus Torvalds l = 0x0000fff0; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 5561da177e4SLinus Torvalds 557c40a22e0SBenjamin Herrenschmidt /* Set the upper 32 bits of PREF base & limit. */ 558c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 559c40a22e0SBenjamin Herrenschmidt pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 5607cc5997dSYinghai Lu } 5617cc5997dSYinghai Lu 5627cc5997dSYinghai Lu static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 5637cc5997dSYinghai Lu { 5647cc5997dSYinghai Lu struct pci_dev *bridge = bus->self; 5657cc5997dSYinghai Lu 5667cc5997dSYinghai Lu dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", 5677cc5997dSYinghai Lu bus->secondary, bus->subordinate); 5687cc5997dSYinghai Lu 5697cc5997dSYinghai Lu if (type & IORESOURCE_IO) 5707cc5997dSYinghai Lu pci_setup_bridge_io(bus); 5717cc5997dSYinghai Lu 5727cc5997dSYinghai Lu if (type & IORESOURCE_MEM) 5737cc5997dSYinghai Lu pci_setup_bridge_mmio(bus); 5747cc5997dSYinghai Lu 5757cc5997dSYinghai Lu if (type & IORESOURCE_PREFETCH) 5767cc5997dSYinghai Lu pci_setup_bridge_mmio_pref(bus); 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 581e2444273SBenjamin Herrenschmidt void pci_setup_bridge(struct pci_bus *bus) 5827cc5997dSYinghai Lu { 5837cc5997dSYinghai Lu unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 5847cc5997dSYinghai Lu IORESOURCE_PREFETCH; 5857cc5997dSYinghai Lu 5867cc5997dSYinghai Lu __pci_setup_bridge(bus, type); 5877cc5997dSYinghai Lu } 5887cc5997dSYinghai Lu 5891da177e4SLinus Torvalds /* Check whether the bridge supports optional I/O and 5901da177e4SLinus Torvalds prefetchable memory ranges. If not, the respective 5911da177e4SLinus Torvalds base/limit registers must be read-only and read as 0. */ 59296bde06aSSam Ravnborg static void pci_bridge_check_ranges(struct pci_bus *bus) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds u16 io; 5951da177e4SLinus Torvalds u32 pmem; 5961da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 5971da177e4SLinus Torvalds struct resource *b_res; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 6001da177e4SLinus Torvalds b_res[1].flags |= IORESOURCE_MEM; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6031da177e4SLinus Torvalds if (!io) { 6041da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 6051da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_IO_BASE, &io); 6061da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds if (io) 6091da177e4SLinus Torvalds b_res[0].flags |= IORESOURCE_IO; 6101da177e4SLinus Torvalds /* DECchip 21050 pass 2 errata: the bridge may miss an address 6111da177e4SLinus Torvalds disconnect boundary by one PCI data phase. 6121da177e4SLinus Torvalds Workaround: do not use prefetching on this device. */ 6131da177e4SLinus Torvalds if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 6141da177e4SLinus Torvalds return; 6151da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 6161da177e4SLinus Torvalds if (!pmem) { 6171da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 6181da177e4SLinus Torvalds 0xfff0fff0); 6191da177e4SLinus Torvalds pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 6201da177e4SLinus Torvalds pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 6211da177e4SLinus Torvalds } 6221f82de10SYinghai Lu if (pmem) { 6231da177e4SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 62499586105SYinghai Lu if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 62599586105SYinghai Lu PCI_PREF_RANGE_TYPE_64) { 6261f82de10SYinghai Lu b_res[2].flags |= IORESOURCE_MEM_64; 62799586105SYinghai Lu b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 62899586105SYinghai Lu } 6291f82de10SYinghai Lu } 6301f82de10SYinghai Lu 6311f82de10SYinghai Lu /* double check if bridge does support 64 bit pref */ 6321f82de10SYinghai Lu if (b_res[2].flags & IORESOURCE_MEM_64) { 6331f82de10SYinghai Lu u32 mem_base_hi, tmp; 6341f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 6351f82de10SYinghai Lu &mem_base_hi); 6361f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 6371f82de10SYinghai Lu 0xffffffff); 6381f82de10SYinghai Lu pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 6391f82de10SYinghai Lu if (!tmp) 6401f82de10SYinghai Lu b_res[2].flags &= ~IORESOURCE_MEM_64; 6411f82de10SYinghai Lu pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 6421f82de10SYinghai Lu mem_base_hi); 6431f82de10SYinghai Lu } 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* Helper function for sizing routines: find first available 6471da177e4SLinus Torvalds bus resource of a given type. Note: we intentionally skip 6481da177e4SLinus Torvalds the bus resources which have already been assigned (that is, 6491da177e4SLinus Torvalds have non-NULL parent resource). */ 65096bde06aSSam Ravnborg static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds int i; 6531da177e4SLinus Torvalds struct resource *r; 6541da177e4SLinus Torvalds unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 6551da177e4SLinus Torvalds IORESOURCE_PREFETCH; 6561da177e4SLinus Torvalds 65789a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, r, i) { 658299de034SIvan Kokshaysky if (r == &ioport_resource || r == &iomem_resource) 659299de034SIvan Kokshaysky continue; 66055a10984SJesse Barnes if (r && (r->flags & type_mask) == type && !r->parent) 6611da177e4SLinus Torvalds return r; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds return NULL; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 66613583b16SRam Pai static resource_size_t calculate_iosize(resource_size_t size, 66713583b16SRam Pai resource_size_t min_size, 66813583b16SRam Pai resource_size_t size1, 66913583b16SRam Pai resource_size_t old_size, 67013583b16SRam Pai resource_size_t align) 67113583b16SRam Pai { 67213583b16SRam Pai if (size < min_size) 67313583b16SRam Pai size = min_size; 67413583b16SRam Pai if (old_size == 1 ) 67513583b16SRam Pai old_size = 0; 67613583b16SRam Pai /* To be fixed in 2.5: we should have sort of HAVE_ISA 67713583b16SRam Pai flag in the struct pci_bus. */ 67813583b16SRam Pai #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 67913583b16SRam Pai size = (size & 0xff) + ((size & ~0xffUL) << 2); 68013583b16SRam Pai #endif 68113583b16SRam Pai size = ALIGN(size + size1, align); 68213583b16SRam Pai if (size < old_size) 68313583b16SRam Pai size = old_size; 68413583b16SRam Pai return size; 68513583b16SRam Pai } 68613583b16SRam Pai 68713583b16SRam Pai static resource_size_t calculate_memsize(resource_size_t size, 68813583b16SRam Pai resource_size_t min_size, 68913583b16SRam Pai resource_size_t size1, 69013583b16SRam Pai resource_size_t old_size, 69113583b16SRam Pai resource_size_t align) 69213583b16SRam Pai { 69313583b16SRam Pai if (size < min_size) 69413583b16SRam Pai size = min_size; 69513583b16SRam Pai if (old_size == 1 ) 69613583b16SRam Pai old_size = 0; 69713583b16SRam Pai if (size < old_size) 69813583b16SRam Pai size = old_size; 69913583b16SRam Pai size = ALIGN(size + size1, align); 70013583b16SRam Pai return size; 70113583b16SRam Pai } 70213583b16SRam Pai 703c8adf9a3SRam Pai /** 704c8adf9a3SRam Pai * pbus_size_io() - size the io window of a given bus 705c8adf9a3SRam Pai * 706c8adf9a3SRam Pai * @bus : the bus 707c8adf9a3SRam Pai * @min_size : the minimum io window that must to be allocated 708c8adf9a3SRam Pai * @add_size : additional optional io window 7099e8bf93aSRam Pai * @realloc_head : track the additional io window on this list 710c8adf9a3SRam Pai * 711c8adf9a3SRam Pai * Sizing the IO windows of the PCI-PCI bridge is trivial, 712c8adf9a3SRam Pai * since these windows have 4K granularity and the IO ranges 713c8adf9a3SRam Pai * of non-bridge PCI devices are limited to 256 bytes. 714c8adf9a3SRam Pai * We must be careful with the ISA aliasing though. 715c8adf9a3SRam Pai */ 716c8adf9a3SRam Pai static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 717bdc4abecSYinghai Lu resource_size_t add_size, struct list_head *realloc_head) 7181da177e4SLinus Torvalds { 7191da177e4SLinus Torvalds struct pci_dev *dev; 7201da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 721c8adf9a3SRam Pai unsigned long size = 0, size0 = 0, size1 = 0; 722be768912SYinghai Lu resource_size_t children_add_size = 0; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds if (!b_res) 7251da177e4SLinus Torvalds return; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 7281da177e4SLinus Torvalds int i; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 7311da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 7321da177e4SLinus Torvalds unsigned long r_size; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds if (r->parent || !(r->flags & IORESOURCE_IO)) 7351da177e4SLinus Torvalds continue; 736022edd86SZhao, Yu r_size = resource_size(r); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds if (r_size < 0x400) 7391da177e4SLinus Torvalds /* Might be re-aligned for ISA */ 7401da177e4SLinus Torvalds size += r_size; 7411da177e4SLinus Torvalds else 7421da177e4SLinus Torvalds size1 += r_size; 743be768912SYinghai Lu 7449e8bf93aSRam Pai if (realloc_head) 7459e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds } 748c8adf9a3SRam Pai size0 = calculate_iosize(size, min_size, size1, 74913583b16SRam Pai resource_size(b_res), 4096); 750be768912SYinghai Lu if (children_add_size > add_size) 751be768912SYinghai Lu add_size = children_add_size; 7529e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 753a4ac9feaSYinghai Lu calculate_iosize(size, min_size, add_size + size1, 754c8adf9a3SRam Pai resource_size(b_res), 4096); 755c8adf9a3SRam Pai if (!size0 && !size1) { 756865df576SBjorn Helgaas if (b_res->start || b_res->end) 757865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 758865df576SBjorn Helgaas "%pR to [bus %02x-%02x] (unused)\n", b_res, 759865df576SBjorn Helgaas bus->secondary, bus->subordinate); 7601da177e4SLinus Torvalds b_res->flags = 0; 7611da177e4SLinus Torvalds return; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds /* Alignment of the IO window is always 4K */ 7641da177e4SLinus Torvalds b_res->start = 4096; 765c8adf9a3SRam Pai b_res->end = b_res->start + size0 - 1; 76688452565SIvan Kokshaysky b_res->flags |= IORESOURCE_STARTALIGN; 767*b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 7689e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096); 769*b592443dSYinghai Lu dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 770*b592443dSYinghai Lu "%pR to [bus %02x-%02x] add_size %lx\n", b_res, 771*b592443dSYinghai Lu bus->secondary, bus->subordinate, size1-size0); 772*b592443dSYinghai Lu } 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 775c8adf9a3SRam Pai /** 776c8adf9a3SRam Pai * pbus_size_mem() - size the memory window of a given bus 777c8adf9a3SRam Pai * 778c8adf9a3SRam Pai * @bus : the bus 779c8adf9a3SRam Pai * @min_size : the minimum memory window that must to be allocated 780c8adf9a3SRam Pai * @add_size : additional optional memory window 7819e8bf93aSRam Pai * @realloc_head : track the additional memory window on this list 782c8adf9a3SRam Pai * 783c8adf9a3SRam Pai * Calculate the size of the bus and minimal alignment which 784c8adf9a3SRam Pai * guarantees that all child resources fit in this size. 785c8adf9a3SRam Pai */ 78628760489SEric W. Biederman static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 787c8adf9a3SRam Pai unsigned long type, resource_size_t min_size, 788c8adf9a3SRam Pai resource_size_t add_size, 789bdc4abecSYinghai Lu struct list_head *realloc_head) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct pci_dev *dev; 792c8adf9a3SRam Pai resource_size_t min_align, align, size, size0, size1; 793c40a22e0SBenjamin Herrenschmidt resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 7941da177e4SLinus Torvalds int order, max_order; 7951da177e4SLinus Torvalds struct resource *b_res = find_free_bus_resource(bus, type); 7961f82de10SYinghai Lu unsigned int mem64_mask = 0; 797be768912SYinghai Lu resource_size_t children_add_size = 0; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds if (!b_res) 8001da177e4SLinus Torvalds return 0; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds memset(aligns, 0, sizeof(aligns)); 8031da177e4SLinus Torvalds max_order = 0; 8041da177e4SLinus Torvalds size = 0; 8051da177e4SLinus Torvalds 8061f82de10SYinghai Lu mem64_mask = b_res->flags & IORESOURCE_MEM_64; 8071f82de10SYinghai Lu b_res->flags &= ~IORESOURCE_MEM_64; 8081f82de10SYinghai Lu 8091da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 8101da177e4SLinus Torvalds int i; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds for (i = 0; i < PCI_NUM_RESOURCES; i++) { 8131da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 814c40a22e0SBenjamin Herrenschmidt resource_size_t r_size; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds if (r->parent || (r->flags & mask) != type) 8171da177e4SLinus Torvalds continue; 818022edd86SZhao, Yu r_size = resource_size(r); 8192aceefcbSYinghai Lu #ifdef CONFIG_PCI_IOV 8202aceefcbSYinghai Lu /* put SRIOV requested res to the optional list */ 8219e8bf93aSRam Pai if (realloc_head && i >= PCI_IOV_RESOURCES && 8222aceefcbSYinghai Lu i <= PCI_IOV_RESOURCE_END) { 8232aceefcbSYinghai Lu r->end = r->start - 1; 8249e8bf93aSRam Pai add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */); 8252aceefcbSYinghai Lu children_add_size += r_size; 8262aceefcbSYinghai Lu continue; 8272aceefcbSYinghai Lu } 8282aceefcbSYinghai Lu #endif 8291da177e4SLinus Torvalds /* For bridges size != alignment */ 8306faf17f6SChris Wright align = pci_resource_alignment(dev, r); 8311da177e4SLinus Torvalds order = __ffs(align) - 20; 8321da177e4SLinus Torvalds if (order > 11) { 833865df576SBjorn Helgaas dev_warn(&dev->dev, "disabling BAR %d: %pR " 834865df576SBjorn Helgaas "(bad alignment %#llx)\n", i, r, 835865df576SBjorn Helgaas (unsigned long long) align); 8361da177e4SLinus Torvalds r->flags = 0; 8371da177e4SLinus Torvalds continue; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds size += r_size; 8401da177e4SLinus Torvalds if (order < 0) 8411da177e4SLinus Torvalds order = 0; 8421da177e4SLinus Torvalds /* Exclude ranges with size > align from 8431da177e4SLinus Torvalds calculation of the alignment. */ 8441da177e4SLinus Torvalds if (r_size == align) 8451da177e4SLinus Torvalds aligns[order] += align; 8461da177e4SLinus Torvalds if (order > max_order) 8471da177e4SLinus Torvalds max_order = order; 8481f82de10SYinghai Lu mem64_mask &= r->flags & IORESOURCE_MEM_64; 849be768912SYinghai Lu 8509e8bf93aSRam Pai if (realloc_head) 8519e8bf93aSRam Pai children_add_size += get_res_add_size(realloc_head, r); 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds align = 0; 8551da177e4SLinus Torvalds min_align = 0; 8561da177e4SLinus Torvalds for (order = 0; order <= max_order; order++) { 8578308c54dSJeremy Fitzhardinge resource_size_t align1 = 1; 8588308c54dSJeremy Fitzhardinge 8598308c54dSJeremy Fitzhardinge align1 <<= (order + 20); 8608308c54dSJeremy Fitzhardinge 8611da177e4SLinus Torvalds if (!align) 8621da177e4SLinus Torvalds min_align = align1; 8636f6f8c2fSMilind Arun Choudhary else if (ALIGN(align + min_align, min_align) < align1) 8641da177e4SLinus Torvalds min_align = align1 >> 1; 8651da177e4SLinus Torvalds align += aligns[order]; 8661da177e4SLinus Torvalds } 867b42282e5SLinus Torvalds size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 868be768912SYinghai Lu if (children_add_size > add_size) 869be768912SYinghai Lu add_size = children_add_size; 8709e8bf93aSRam Pai size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 871a4ac9feaSYinghai Lu calculate_memsize(size, min_size, add_size, 872b42282e5SLinus Torvalds resource_size(b_res), min_align); 873c8adf9a3SRam Pai if (!size0 && !size1) { 874865df576SBjorn Helgaas if (b_res->start || b_res->end) 875865df576SBjorn Helgaas dev_info(&bus->self->dev, "disabling bridge window " 876865df576SBjorn Helgaas "%pR to [bus %02x-%02x] (unused)\n", b_res, 877865df576SBjorn Helgaas bus->secondary, bus->subordinate); 8781da177e4SLinus Torvalds b_res->flags = 0; 8791da177e4SLinus Torvalds return 1; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds b_res->start = min_align; 882c8adf9a3SRam Pai b_res->end = size0 + min_align - 1; 883c8adf9a3SRam Pai b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; 884*b592443dSYinghai Lu if (size1 > size0 && realloc_head) { 8859e8bf93aSRam Pai add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 886*b592443dSYinghai Lu dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 887*b592443dSYinghai Lu "%pR to [bus %02x-%02x] add_size %llx\n", b_res, 888*b592443dSYinghai Lu bus->secondary, bus->subordinate, (unsigned long long)size1-size0); 889*b592443dSYinghai Lu } 8901da177e4SLinus Torvalds return 1; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8930a2daa1cSRam Pai unsigned long pci_cardbus_resource_alignment(struct resource *res) 8940a2daa1cSRam Pai { 8950a2daa1cSRam Pai if (res->flags & IORESOURCE_IO) 8960a2daa1cSRam Pai return pci_cardbus_io_size; 8970a2daa1cSRam Pai if (res->flags & IORESOURCE_MEM) 8980a2daa1cSRam Pai return pci_cardbus_mem_size; 8990a2daa1cSRam Pai return 0; 9000a2daa1cSRam Pai } 9010a2daa1cSRam Pai 9020a2daa1cSRam Pai static void pci_bus_size_cardbus(struct pci_bus *bus, 903bdc4abecSYinghai Lu struct list_head *realloc_head) 9041da177e4SLinus Torvalds { 9051da177e4SLinus Torvalds struct pci_dev *bridge = bus->self; 9061da177e4SLinus Torvalds struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 9071da177e4SLinus Torvalds u16 ctrl; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /* 9101da177e4SLinus Torvalds * Reserve some resources for CardBus. We reserve 9111da177e4SLinus Torvalds * a fixed amount of bus space for CardBus bridges. 9121da177e4SLinus Torvalds */ 913934b7024SLinus Torvalds b_res[0].start = 0; 914934b7024SLinus Torvalds b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 9159e8bf93aSRam Pai if (realloc_head) 9169e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */); 9171da177e4SLinus Torvalds 918934b7024SLinus Torvalds b_res[1].start = 0; 919934b7024SLinus Torvalds b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 9209e8bf93aSRam Pai if (realloc_head) 9219e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /* 9241da177e4SLinus Torvalds * Check whether prefetchable memory is supported 9251da177e4SLinus Torvalds * by this bridge. 9261da177e4SLinus Torvalds */ 9271da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 9281da177e4SLinus Torvalds if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 9291da177e4SLinus Torvalds ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 9301da177e4SLinus Torvalds pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 9311da177e4SLinus Torvalds pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * If we have prefetchable memory support, allocate 9361da177e4SLinus Torvalds * two regions. Otherwise, allocate one region of 9371da177e4SLinus Torvalds * twice the size. 9381da177e4SLinus Torvalds */ 9391da177e4SLinus Torvalds if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 940934b7024SLinus Torvalds b_res[2].start = 0; 941934b7024SLinus Torvalds b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 9429e8bf93aSRam Pai if (realloc_head) 9439e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */); 9441da177e4SLinus Torvalds 945934b7024SLinus Torvalds b_res[3].start = 0; 946934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 9479e8bf93aSRam Pai if (realloc_head) 9489e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */); 9491da177e4SLinus Torvalds } else { 950934b7024SLinus Torvalds b_res[3].start = 0; 951934b7024SLinus Torvalds b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 9529e8bf93aSRam Pai if (realloc_head) 9539e8bf93aSRam Pai add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */); 9541da177e4SLinus Torvalds } 9550a2daa1cSRam Pai 9560a2daa1cSRam Pai /* set the size of the resource to zero, so that the resource does not 9570a2daa1cSRam Pai * get assigned during required-resource allocation cycle but gets assigned 9580a2daa1cSRam Pai * during the optional-resource allocation cycle. 9590a2daa1cSRam Pai */ 9600a2daa1cSRam Pai b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1; 9610a2daa1cSRam Pai b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 964c8adf9a3SRam Pai void __ref __pci_bus_size_bridges(struct pci_bus *bus, 965bdc4abecSYinghai Lu struct list_head *realloc_head) 9661da177e4SLinus Torvalds { 9671da177e4SLinus Torvalds struct pci_dev *dev; 9681da177e4SLinus Torvalds unsigned long mask, prefmask; 969c8adf9a3SRam Pai resource_size_t additional_mem_size = 0, additional_io_size = 0; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 9721da177e4SLinus Torvalds struct pci_bus *b = dev->subordinate; 9731da177e4SLinus Torvalds if (!b) 9741da177e4SLinus Torvalds continue; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds switch (dev->class >> 8) { 9771da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 9789e8bf93aSRam Pai pci_bus_size_cardbus(b, realloc_head); 9791da177e4SLinus Torvalds break; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 9821da177e4SLinus Torvalds default: 9839e8bf93aSRam Pai __pci_bus_size_bridges(b, realloc_head); 9841da177e4SLinus Torvalds break; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /* The root bus? */ 9891da177e4SLinus Torvalds if (!bus->self) 9901da177e4SLinus Torvalds return; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds switch (bus->self->class >> 8) { 9931da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 9941da177e4SLinus Torvalds /* don't size cardbuses yet. */ 9951da177e4SLinus Torvalds break; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 9981da177e4SLinus Torvalds pci_bridge_check_ranges(bus); 99928760489SEric W. Biederman if (bus->self->is_hotplug_bridge) { 1000c8adf9a3SRam Pai additional_io_size = pci_hotplug_io_size; 1001c8adf9a3SRam Pai additional_mem_size = pci_hotplug_mem_size; 100228760489SEric W. Biederman } 1003c8adf9a3SRam Pai /* 1004c8adf9a3SRam Pai * Follow thru 1005c8adf9a3SRam Pai */ 10061da177e4SLinus Torvalds default: 100719aa7ee4SYinghai Lu pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 100819aa7ee4SYinghai Lu additional_io_size, realloc_head); 10091da177e4SLinus Torvalds /* If the bridge supports prefetchable range, size it 10101da177e4SLinus Torvalds separately. If it doesn't, or its prefetchable window 10111da177e4SLinus Torvalds has already been allocated by arch code, try 10121da177e4SLinus Torvalds non-prefetchable range for both types of PCI memory 10131da177e4SLinus Torvalds resources. */ 10141da177e4SLinus Torvalds mask = IORESOURCE_MEM; 10151da177e4SLinus Torvalds prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 101619aa7ee4SYinghai Lu if (pbus_size_mem(bus, prefmask, prefmask, 101719aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 101819aa7ee4SYinghai Lu additional_mem_size, realloc_head)) 10191da177e4SLinus Torvalds mask = prefmask; /* Success, size non-prefetch only. */ 102028760489SEric W. Biederman else 1021c8adf9a3SRam Pai additional_mem_size += additional_mem_size; 102219aa7ee4SYinghai Lu pbus_size_mem(bus, mask, IORESOURCE_MEM, 102319aa7ee4SYinghai Lu realloc_head ? 0 : additional_mem_size, 102419aa7ee4SYinghai Lu additional_mem_size, realloc_head); 10251da177e4SLinus Torvalds break; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds } 1028c8adf9a3SRam Pai 1029c8adf9a3SRam Pai void __ref pci_bus_size_bridges(struct pci_bus *bus) 1030c8adf9a3SRam Pai { 1031c8adf9a3SRam Pai __pci_bus_size_bridges(bus, NULL); 1032c8adf9a3SRam Pai } 10331da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_size_bridges); 10341da177e4SLinus Torvalds 1035568ddef8SYinghai Lu static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 1036bdc4abecSYinghai Lu struct list_head *realloc_head, 1037bdc4abecSYinghai Lu struct list_head *fail_head) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds struct pci_bus *b; 10401da177e4SLinus Torvalds struct pci_dev *dev; 10411da177e4SLinus Torvalds 10429e8bf93aSRam Pai pbus_assign_resources_sorted(bus, realloc_head, fail_head); 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds list_for_each_entry(dev, &bus->devices, bus_list) { 10451da177e4SLinus Torvalds b = dev->subordinate; 10461da177e4SLinus Torvalds if (!b) 10471da177e4SLinus Torvalds continue; 10481da177e4SLinus Torvalds 10499e8bf93aSRam Pai __pci_bus_assign_resources(b, realloc_head, fail_head); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds switch (dev->class >> 8) { 10521da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_PCI: 10536841ec68SYinghai Lu if (!pci_is_enabled(dev)) 10541da177e4SLinus Torvalds pci_setup_bridge(b); 10551da177e4SLinus Torvalds break; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds case PCI_CLASS_BRIDGE_CARDBUS: 10581da177e4SLinus Torvalds pci_setup_cardbus(b); 10591da177e4SLinus Torvalds break; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds default: 106280ccba11SBjorn Helgaas dev_info(&dev->dev, "not setting up bridge for bus " 106380ccba11SBjorn Helgaas "%04x:%02x\n", pci_domain_nr(b), b->number); 10641da177e4SLinus Torvalds break; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds } 1068568ddef8SYinghai Lu 1069568ddef8SYinghai Lu void __ref pci_bus_assign_resources(const struct pci_bus *bus) 1070568ddef8SYinghai Lu { 1071c8adf9a3SRam Pai __pci_bus_assign_resources(bus, NULL, NULL); 1072568ddef8SYinghai Lu } 10731da177e4SLinus Torvalds EXPORT_SYMBOL(pci_bus_assign_resources); 10741da177e4SLinus Torvalds 10756841ec68SYinghai Lu static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 1076bdc4abecSYinghai Lu struct list_head *add_head, 1077bdc4abecSYinghai Lu struct list_head *fail_head) 10786841ec68SYinghai Lu { 10796841ec68SYinghai Lu struct pci_bus *b; 10806841ec68SYinghai Lu 10818424d759SYinghai Lu pdev_assign_resources_sorted((struct pci_dev *)bridge, 10828424d759SYinghai Lu add_head, fail_head); 10836841ec68SYinghai Lu 10846841ec68SYinghai Lu b = bridge->subordinate; 10856841ec68SYinghai Lu if (!b) 10866841ec68SYinghai Lu return; 10876841ec68SYinghai Lu 10888424d759SYinghai Lu __pci_bus_assign_resources(b, add_head, fail_head); 10896841ec68SYinghai Lu 10906841ec68SYinghai Lu switch (bridge->class >> 8) { 10916841ec68SYinghai Lu case PCI_CLASS_BRIDGE_PCI: 10926841ec68SYinghai Lu pci_setup_bridge(b); 10936841ec68SYinghai Lu break; 10946841ec68SYinghai Lu 10956841ec68SYinghai Lu case PCI_CLASS_BRIDGE_CARDBUS: 10966841ec68SYinghai Lu pci_setup_cardbus(b); 10976841ec68SYinghai Lu break; 10986841ec68SYinghai Lu 10996841ec68SYinghai Lu default: 11006841ec68SYinghai Lu dev_info(&bridge->dev, "not setting up bridge for bus " 11016841ec68SYinghai Lu "%04x:%02x\n", pci_domain_nr(b), b->number); 11026841ec68SYinghai Lu break; 11036841ec68SYinghai Lu } 11046841ec68SYinghai Lu } 11055009b460SYinghai Lu static void pci_bridge_release_resources(struct pci_bus *bus, 11065009b460SYinghai Lu unsigned long type) 11075009b460SYinghai Lu { 11085009b460SYinghai Lu int idx; 11095009b460SYinghai Lu bool changed = false; 11105009b460SYinghai Lu struct pci_dev *dev; 11115009b460SYinghai Lu struct resource *r; 11125009b460SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 11135009b460SYinghai Lu IORESOURCE_PREFETCH; 11145009b460SYinghai Lu 11155009b460SYinghai Lu dev = bus->self; 11165009b460SYinghai Lu for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; 11175009b460SYinghai Lu idx++) { 11185009b460SYinghai Lu r = &dev->resource[idx]; 11195009b460SYinghai Lu if ((r->flags & type_mask) != type) 11205009b460SYinghai Lu continue; 11215009b460SYinghai Lu if (!r->parent) 11225009b460SYinghai Lu continue; 11235009b460SYinghai Lu /* 11245009b460SYinghai Lu * if there are children under that, we should release them 11255009b460SYinghai Lu * all 11265009b460SYinghai Lu */ 11275009b460SYinghai Lu release_child_resources(r); 11285009b460SYinghai Lu if (!release_resource(r)) { 11295009b460SYinghai Lu dev_printk(KERN_DEBUG, &dev->dev, 11305009b460SYinghai Lu "resource %d %pR released\n", idx, r); 11315009b460SYinghai Lu /* keep the old size */ 11325009b460SYinghai Lu r->end = resource_size(r) - 1; 11335009b460SYinghai Lu r->start = 0; 11345009b460SYinghai Lu r->flags = 0; 11355009b460SYinghai Lu changed = true; 11365009b460SYinghai Lu } 11375009b460SYinghai Lu } 11385009b460SYinghai Lu 11395009b460SYinghai Lu if (changed) { 11405009b460SYinghai Lu /* avoiding touch the one without PREF */ 11415009b460SYinghai Lu if (type & IORESOURCE_PREFETCH) 11425009b460SYinghai Lu type = IORESOURCE_PREFETCH; 11435009b460SYinghai Lu __pci_setup_bridge(bus, type); 11445009b460SYinghai Lu } 11455009b460SYinghai Lu } 11465009b460SYinghai Lu 11475009b460SYinghai Lu enum release_type { 11485009b460SYinghai Lu leaf_only, 11495009b460SYinghai Lu whole_subtree, 11505009b460SYinghai Lu }; 11515009b460SYinghai Lu /* 11525009b460SYinghai Lu * try to release pci bridge resources that is from leaf bridge, 11535009b460SYinghai Lu * so we can allocate big new one later 11545009b460SYinghai Lu */ 11555009b460SYinghai Lu static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, 11565009b460SYinghai Lu unsigned long type, 11575009b460SYinghai Lu enum release_type rel_type) 11585009b460SYinghai Lu { 11595009b460SYinghai Lu struct pci_dev *dev; 11605009b460SYinghai Lu bool is_leaf_bridge = true; 11615009b460SYinghai Lu 11625009b460SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 11635009b460SYinghai Lu struct pci_bus *b = dev->subordinate; 11645009b460SYinghai Lu if (!b) 11655009b460SYinghai Lu continue; 11665009b460SYinghai Lu 11675009b460SYinghai Lu is_leaf_bridge = false; 11685009b460SYinghai Lu 11695009b460SYinghai Lu if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 11705009b460SYinghai Lu continue; 11715009b460SYinghai Lu 11725009b460SYinghai Lu if (rel_type == whole_subtree) 11735009b460SYinghai Lu pci_bus_release_bridge_resources(b, type, 11745009b460SYinghai Lu whole_subtree); 11755009b460SYinghai Lu } 11765009b460SYinghai Lu 11775009b460SYinghai Lu if (pci_is_root_bus(bus)) 11785009b460SYinghai Lu return; 11795009b460SYinghai Lu 11805009b460SYinghai Lu if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 11815009b460SYinghai Lu return; 11825009b460SYinghai Lu 11835009b460SYinghai Lu if ((rel_type == whole_subtree) || is_leaf_bridge) 11845009b460SYinghai Lu pci_bridge_release_resources(bus, type); 11855009b460SYinghai Lu } 11865009b460SYinghai Lu 118776fbc263SYinghai Lu static void pci_bus_dump_res(struct pci_bus *bus) 118876fbc263SYinghai Lu { 118989a74eccSBjorn Helgaas struct resource *res; 119076fbc263SYinghai Lu int i; 119176fbc263SYinghai Lu 119289a74eccSBjorn Helgaas pci_bus_for_each_resource(bus, res, i) { 11937c9342b8SYinghai Lu if (!res || !res->end || !res->flags) 119476fbc263SYinghai Lu continue; 119576fbc263SYinghai Lu 1196c7dabef8SBjorn Helgaas dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 119776fbc263SYinghai Lu } 119876fbc263SYinghai Lu } 119976fbc263SYinghai Lu 120076fbc263SYinghai Lu static void pci_bus_dump_resources(struct pci_bus *bus) 120176fbc263SYinghai Lu { 120276fbc263SYinghai Lu struct pci_bus *b; 120376fbc263SYinghai Lu struct pci_dev *dev; 120476fbc263SYinghai Lu 120576fbc263SYinghai Lu 120676fbc263SYinghai Lu pci_bus_dump_res(bus); 120776fbc263SYinghai Lu 120876fbc263SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 120976fbc263SYinghai Lu b = dev->subordinate; 121076fbc263SYinghai Lu if (!b) 121176fbc263SYinghai Lu continue; 121276fbc263SYinghai Lu 121376fbc263SYinghai Lu pci_bus_dump_resources(b); 121476fbc263SYinghai Lu } 121576fbc263SYinghai Lu } 121676fbc263SYinghai Lu 1217da7822e5SYinghai Lu static int __init pci_bus_get_depth(struct pci_bus *bus) 1218da7822e5SYinghai Lu { 1219da7822e5SYinghai Lu int depth = 0; 1220da7822e5SYinghai Lu struct pci_dev *dev; 1221da7822e5SYinghai Lu 1222da7822e5SYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) { 1223da7822e5SYinghai Lu int ret; 1224da7822e5SYinghai Lu struct pci_bus *b = dev->subordinate; 1225da7822e5SYinghai Lu if (!b) 1226da7822e5SYinghai Lu continue; 1227da7822e5SYinghai Lu 1228da7822e5SYinghai Lu ret = pci_bus_get_depth(b); 1229da7822e5SYinghai Lu if (ret + 1 > depth) 1230da7822e5SYinghai Lu depth = ret + 1; 1231da7822e5SYinghai Lu } 1232da7822e5SYinghai Lu 1233da7822e5SYinghai Lu return depth; 1234da7822e5SYinghai Lu } 1235da7822e5SYinghai Lu static int __init pci_get_max_depth(void) 1236da7822e5SYinghai Lu { 1237da7822e5SYinghai Lu int depth = 0; 1238da7822e5SYinghai Lu struct pci_bus *bus; 1239da7822e5SYinghai Lu 1240da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) { 1241da7822e5SYinghai Lu int ret; 1242da7822e5SYinghai Lu 1243da7822e5SYinghai Lu ret = pci_bus_get_depth(bus); 1244da7822e5SYinghai Lu if (ret > depth) 1245da7822e5SYinghai Lu depth = ret; 1246da7822e5SYinghai Lu } 1247da7822e5SYinghai Lu 1248da7822e5SYinghai Lu return depth; 1249da7822e5SYinghai Lu } 1250da7822e5SYinghai Lu 1251f483d392SRam Pai 1252da7822e5SYinghai Lu /* 1253da7822e5SYinghai Lu * first try will not touch pci bridge res 1254da7822e5SYinghai Lu * second and later try will clear small leaf bridge res 1255da7822e5SYinghai Lu * will stop till to the max deepth if can not find good one 1256da7822e5SYinghai Lu */ 12571da177e4SLinus Torvalds void __init 12581da177e4SLinus Torvalds pci_assign_unassigned_resources(void) 12591da177e4SLinus Torvalds { 12601da177e4SLinus Torvalds struct pci_bus *bus; 1261bdc4abecSYinghai Lu LIST_HEAD(realloc_head); /* list of resources that 1262c8adf9a3SRam Pai want additional resources */ 1263bdc4abecSYinghai Lu struct list_head *add_list = NULL; 1264da7822e5SYinghai Lu int tried_times = 0; 1265da7822e5SYinghai Lu enum release_type rel_type = leaf_only; 1266bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1267b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 1268da7822e5SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1269da7822e5SYinghai Lu IORESOURCE_PREFETCH; 1270da7822e5SYinghai Lu unsigned long failed_type; 127119aa7ee4SYinghai Lu int pci_try_num = 1; 1272da7822e5SYinghai Lu 127319aa7ee4SYinghai Lu /* don't realloc if asked to do so */ 127419aa7ee4SYinghai Lu if (pci_realloc_enabled()) { 127519aa7ee4SYinghai Lu int max_depth = pci_get_max_depth(); 127619aa7ee4SYinghai Lu 1277da7822e5SYinghai Lu pci_try_num = max_depth + 1; 1278da7822e5SYinghai Lu printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n", 1279da7822e5SYinghai Lu max_depth, pci_try_num); 128019aa7ee4SYinghai Lu } 1281da7822e5SYinghai Lu 1282da7822e5SYinghai Lu again: 128319aa7ee4SYinghai Lu /* 128419aa7ee4SYinghai Lu * last try will use add_list, otherwise will try good to have as 128519aa7ee4SYinghai Lu * must have, so can realloc parent bridge resource 128619aa7ee4SYinghai Lu */ 128719aa7ee4SYinghai Lu if (tried_times + 1 == pci_try_num) 1288bdc4abecSYinghai Lu add_list = &realloc_head; 12891da177e4SLinus Torvalds /* Depth first, calculate sizes and alignments of all 12901da177e4SLinus Torvalds subordinate buses. */ 1291da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 129219aa7ee4SYinghai Lu __pci_bus_size_bridges(bus, add_list); 1293c8adf9a3SRam Pai 12941da177e4SLinus Torvalds /* Depth last, allocate resources and update the hardware. */ 1295da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 1296bdc4abecSYinghai Lu __pci_bus_assign_resources(bus, add_list, &fail_head); 129719aa7ee4SYinghai Lu if (add_list) 1298bdc4abecSYinghai Lu BUG_ON(!list_empty(add_list)); 1299da7822e5SYinghai Lu tried_times++; 1300da7822e5SYinghai Lu 1301da7822e5SYinghai Lu /* any device complain? */ 1302bdc4abecSYinghai Lu if (list_empty(&fail_head)) 1303da7822e5SYinghai Lu goto enable_and_dump; 1304f483d392SRam Pai 1305da7822e5SYinghai Lu failed_type = 0; 1306b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) 1307b9b0bba9SYinghai Lu failed_type |= fail_res->flags; 1308bdc4abecSYinghai Lu 1309da7822e5SYinghai Lu /* 1310da7822e5SYinghai Lu * io port are tight, don't try extra 1311da7822e5SYinghai Lu * or if reach the limit, don't want to try more 1312da7822e5SYinghai Lu */ 1313da7822e5SYinghai Lu failed_type &= type_mask; 1314da7822e5SYinghai Lu if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) { 1315bffc56d4SYinghai Lu free_list(&fail_head); 1316da7822e5SYinghai Lu goto enable_and_dump; 1317da7822e5SYinghai Lu } 1318da7822e5SYinghai Lu 1319da7822e5SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 1320da7822e5SYinghai Lu tried_times + 1); 1321da7822e5SYinghai Lu 1322da7822e5SYinghai Lu /* third times and later will not check if it is leaf */ 1323da7822e5SYinghai Lu if ((tried_times + 1) > 2) 1324da7822e5SYinghai Lu rel_type = whole_subtree; 1325da7822e5SYinghai Lu 1326da7822e5SYinghai Lu /* 1327da7822e5SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 1328da7822e5SYinghai Lu * child device under that bridge 1329da7822e5SYinghai Lu */ 1330b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1331b9b0bba9SYinghai Lu bus = fail_res->dev->bus; 1332bdc4abecSYinghai Lu pci_bus_release_bridge_resources(bus, 1333b9b0bba9SYinghai Lu fail_res->flags & type_mask, 1334da7822e5SYinghai Lu rel_type); 1335da7822e5SYinghai Lu } 1336da7822e5SYinghai Lu /* restore size and flags */ 1337b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1338b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 1339da7822e5SYinghai Lu 1340b9b0bba9SYinghai Lu res->start = fail_res->start; 1341b9b0bba9SYinghai Lu res->end = fail_res->end; 1342b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1343b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 1344da7822e5SYinghai Lu res->flags = 0; 1345da7822e5SYinghai Lu } 1346bffc56d4SYinghai Lu free_list(&fail_head); 1347da7822e5SYinghai Lu 1348da7822e5SYinghai Lu goto again; 1349da7822e5SYinghai Lu 1350da7822e5SYinghai Lu enable_and_dump: 1351da7822e5SYinghai Lu /* Depth last, update the hardware. */ 1352da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 1353da7822e5SYinghai Lu pci_enable_bridges(bus); 135476fbc263SYinghai Lu 135576fbc263SYinghai Lu /* dump the resource on buses */ 1356da7822e5SYinghai Lu list_for_each_entry(bus, &pci_root_buses, node) 135776fbc263SYinghai Lu pci_bus_dump_resources(bus); 135876fbc263SYinghai Lu } 13596841ec68SYinghai Lu 13606841ec68SYinghai Lu void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 13616841ec68SYinghai Lu { 13626841ec68SYinghai Lu struct pci_bus *parent = bridge->subordinate; 1363bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 13648424d759SYinghai Lu want additional resources */ 136532180e40SYinghai Lu int tried_times = 0; 1366bdc4abecSYinghai Lu LIST_HEAD(fail_head); 1367b9b0bba9SYinghai Lu struct pci_dev_resource *fail_res; 13686841ec68SYinghai Lu int retval; 136932180e40SYinghai Lu unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 137032180e40SYinghai Lu IORESOURCE_PREFETCH; 13716841ec68SYinghai Lu 137232180e40SYinghai Lu again: 13738424d759SYinghai Lu __pci_bus_size_bridges(parent, &add_list); 1374bdc4abecSYinghai Lu __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 1375bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 137632180e40SYinghai Lu tried_times++; 137732180e40SYinghai Lu 1378bdc4abecSYinghai Lu if (list_empty(&fail_head)) 13793f579c34SYinghai Lu goto enable_all; 138032180e40SYinghai Lu 138132180e40SYinghai Lu if (tried_times >= 2) { 138232180e40SYinghai Lu /* still fail, don't need to try more */ 1383bffc56d4SYinghai Lu free_list(&fail_head); 13843f579c34SYinghai Lu goto enable_all; 138532180e40SYinghai Lu } 138632180e40SYinghai Lu 138732180e40SYinghai Lu printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 138832180e40SYinghai Lu tried_times + 1); 138932180e40SYinghai Lu 139032180e40SYinghai Lu /* 139132180e40SYinghai Lu * Try to release leaf bridge's resources that doesn't fit resource of 139232180e40SYinghai Lu * child device under that bridge 139332180e40SYinghai Lu */ 1394b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1395b9b0bba9SYinghai Lu struct pci_bus *bus = fail_res->dev->bus; 1396b9b0bba9SYinghai Lu unsigned long flags = fail_res->flags; 139732180e40SYinghai Lu 139832180e40SYinghai Lu pci_bus_release_bridge_resources(bus, flags & type_mask, 139932180e40SYinghai Lu whole_subtree); 140032180e40SYinghai Lu } 140132180e40SYinghai Lu /* restore size and flags */ 1402b9b0bba9SYinghai Lu list_for_each_entry(fail_res, &fail_head, list) { 1403b9b0bba9SYinghai Lu struct resource *res = fail_res->res; 140432180e40SYinghai Lu 1405b9b0bba9SYinghai Lu res->start = fail_res->start; 1406b9b0bba9SYinghai Lu res->end = fail_res->end; 1407b9b0bba9SYinghai Lu res->flags = fail_res->flags; 1408b9b0bba9SYinghai Lu if (fail_res->dev->subordinate) 140932180e40SYinghai Lu res->flags = 0; 141032180e40SYinghai Lu } 1411bffc56d4SYinghai Lu free_list(&fail_head); 141232180e40SYinghai Lu 141332180e40SYinghai Lu goto again; 14143f579c34SYinghai Lu 14153f579c34SYinghai Lu enable_all: 14163f579c34SYinghai Lu retval = pci_reenable_device(bridge); 14173f579c34SYinghai Lu pci_set_master(bridge); 14183f579c34SYinghai Lu pci_enable_bridges(parent); 14196841ec68SYinghai Lu } 14206841ec68SYinghai Lu EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 14219b03088fSYinghai Lu 14229b03088fSYinghai Lu #ifdef CONFIG_HOTPLUG 14239b03088fSYinghai Lu /** 14249b03088fSYinghai Lu * pci_rescan_bus - scan a PCI bus for devices. 14259b03088fSYinghai Lu * @bus: PCI bus to scan 14269b03088fSYinghai Lu * 14279b03088fSYinghai Lu * Scan a PCI bus and child buses for new devices, adds them, 14289b03088fSYinghai Lu * and enables them. 14299b03088fSYinghai Lu * 14309b03088fSYinghai Lu * Returns the max number of subordinate bus discovered. 14319b03088fSYinghai Lu */ 14329b03088fSYinghai Lu unsigned int __ref pci_rescan_bus(struct pci_bus *bus) 14339b03088fSYinghai Lu { 14349b03088fSYinghai Lu unsigned int max; 14359b03088fSYinghai Lu struct pci_dev *dev; 1436bdc4abecSYinghai Lu LIST_HEAD(add_list); /* list of resources that 14379b03088fSYinghai Lu want additional resources */ 14389b03088fSYinghai Lu 14399b03088fSYinghai Lu max = pci_scan_child_bus(bus); 14409b03088fSYinghai Lu 14419b03088fSYinghai Lu down_read(&pci_bus_sem); 14429b03088fSYinghai Lu list_for_each_entry(dev, &bus->devices, bus_list) 14439b03088fSYinghai Lu if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 14449b03088fSYinghai Lu dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 14459b03088fSYinghai Lu if (dev->subordinate) 14469b03088fSYinghai Lu __pci_bus_size_bridges(dev->subordinate, 14479b03088fSYinghai Lu &add_list); 14489b03088fSYinghai Lu up_read(&pci_bus_sem); 14499b03088fSYinghai Lu __pci_bus_assign_resources(bus, &add_list, NULL); 1450bdc4abecSYinghai Lu BUG_ON(!list_empty(&add_list)); 14519b03088fSYinghai Lu 14529b03088fSYinghai Lu pci_enable_bridges(bus); 14539b03088fSYinghai Lu pci_bus_add_devices(bus); 14549b03088fSYinghai Lu 14559b03088fSYinghai Lu return max; 14569b03088fSYinghai Lu } 14579b03088fSYinghai Lu EXPORT_SYMBOL_GPL(pci_rescan_bus); 14589b03088fSYinghai Lu #endif 1459