1 /* 2 * drivers/pci/setup-bus.c 3 * 4 * Extruded from code written by 5 * Dave Rusling (david.rusling@reo.mts.dec.com) 6 * David Mosberger (davidm@cs.arizona.edu) 7 * David Miller (davem@redhat.com) 8 * 9 * Support routines for initializing a PCI subsystem. 10 */ 11 12 /* 13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 14 * PCI-PCI bridges cleanup, sorted resource allocation. 15 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 16 * Converted to allocation in 3 passes, which gives 17 * tighter packing. Prefetchable range support. 18 */ 19 20 #include <linux/init.h> 21 #include <linux/kernel.h> 22 #include <linux/module.h> 23 #include <linux/pci.h> 24 #include <linux/errno.h> 25 #include <linux/ioport.h> 26 #include <linux/cache.h> 27 #include <linux/slab.h> 28 #include "pci.h" 29 30 struct pci_dev_resource { 31 struct list_head list; 32 struct resource *res; 33 struct pci_dev *dev; 34 resource_size_t start; 35 resource_size_t end; 36 resource_size_t add_size; 37 resource_size_t min_align; 38 unsigned long flags; 39 }; 40 41 static void free_list(struct list_head *head) 42 { 43 struct pci_dev_resource *dev_res, *tmp; 44 45 list_for_each_entry_safe(dev_res, tmp, head, list) { 46 list_del(&dev_res->list); 47 kfree(dev_res); 48 } 49 } 50 51 int pci_realloc_enable = 0; 52 #define pci_realloc_enabled() pci_realloc_enable 53 void pci_realloc(void) 54 { 55 pci_realloc_enable = 1; 56 } 57 58 /** 59 * add_to_list() - add a new resource tracker to the list 60 * @head: Head of the list 61 * @dev: device corresponding to which the resource 62 * belongs 63 * @res: The resource to be tracked 64 * @add_size: additional size to be optionally added 65 * to the resource 66 */ 67 static int add_to_list(struct list_head *head, 68 struct pci_dev *dev, struct resource *res, 69 resource_size_t add_size, resource_size_t min_align) 70 { 71 struct pci_dev_resource *tmp; 72 73 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 74 if (!tmp) { 75 pr_warning("add_to_list: kmalloc() failed!\n"); 76 return -ENOMEM; 77 } 78 79 tmp->res = res; 80 tmp->dev = dev; 81 tmp->start = res->start; 82 tmp->end = res->end; 83 tmp->flags = res->flags; 84 tmp->add_size = add_size; 85 tmp->min_align = min_align; 86 87 list_add(&tmp->list, head); 88 89 return 0; 90 } 91 92 static void add_to_failed_list(struct list_head *head, 93 struct pci_dev *dev, struct resource *res) 94 { 95 add_to_list(head, dev, res, 96 0 /* dont care */, 97 0 /* dont care */); 98 } 99 100 static void remove_from_list(struct list_head *head, 101 struct resource *res) 102 { 103 struct pci_dev_resource *dev_res, *tmp; 104 105 list_for_each_entry_safe(dev_res, tmp, head, list) { 106 if (dev_res->res == res) { 107 list_del(&dev_res->list); 108 kfree(dev_res); 109 break; 110 } 111 } 112 } 113 114 static resource_size_t get_res_add_size(struct list_head *head, 115 struct resource *res) 116 { 117 struct pci_dev_resource *dev_res; 118 119 list_for_each_entry(dev_res, head, list) { 120 if (dev_res->res == res) { 121 int idx = res - &dev_res->dev->resource[0]; 122 123 dev_printk(KERN_DEBUG, &dev_res->dev->dev, 124 "res[%d]=%pR get_res_add_size add_size %llx\n", 125 idx, dev_res->res, 126 (unsigned long long)dev_res->add_size); 127 128 return dev_res->add_size; 129 } 130 } 131 132 return 0; 133 } 134 135 /* Sort resources by alignment */ 136 static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 137 { 138 int i; 139 140 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 141 struct resource *r; 142 struct pci_dev_resource *dev_res, *tmp; 143 resource_size_t r_align; 144 struct list_head *n; 145 146 r = &dev->resource[i]; 147 148 if (r->flags & IORESOURCE_PCI_FIXED) 149 continue; 150 151 if (!(r->flags) || r->parent) 152 continue; 153 154 r_align = pci_resource_alignment(dev, r); 155 if (!r_align) { 156 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n", 157 i, r); 158 continue; 159 } 160 161 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 162 if (!tmp) 163 panic("pdev_sort_resources(): " 164 "kmalloc() failed!\n"); 165 tmp->res = r; 166 tmp->dev = dev; 167 168 /* fallback is smallest one or list is empty*/ 169 n = head; 170 list_for_each_entry(dev_res, head, list) { 171 resource_size_t align; 172 173 align = pci_resource_alignment(dev_res->dev, 174 dev_res->res); 175 176 if (r_align > align) { 177 n = &dev_res->list; 178 break; 179 } 180 } 181 /* Insert it just before n*/ 182 list_add_tail(&tmp->list, n); 183 } 184 } 185 186 static void __dev_sort_resources(struct pci_dev *dev, 187 struct list_head *head) 188 { 189 u16 class = dev->class >> 8; 190 191 /* Don't touch classless devices or host bridges or ioapics. */ 192 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 193 return; 194 195 /* Don't touch ioapic devices already enabled by firmware */ 196 if (class == PCI_CLASS_SYSTEM_PIC) { 197 u16 command; 198 pci_read_config_word(dev, PCI_COMMAND, &command); 199 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 200 return; 201 } 202 203 pdev_sort_resources(dev, head); 204 } 205 206 static inline void reset_resource(struct resource *res) 207 { 208 res->start = 0; 209 res->end = 0; 210 res->flags = 0; 211 } 212 213 /** 214 * reassign_resources_sorted() - satisfy any additional resource requests 215 * 216 * @realloc_head : head of the list tracking requests requiring additional 217 * resources 218 * @head : head of the list tracking requests with allocated 219 * resources 220 * 221 * Walk through each element of the realloc_head and try to procure 222 * additional resources for the element, provided the element 223 * is in the head list. 224 */ 225 static void reassign_resources_sorted(struct list_head *realloc_head, 226 struct list_head *head) 227 { 228 struct resource *res; 229 struct pci_dev_resource *add_res, *tmp; 230 struct pci_dev_resource *dev_res; 231 resource_size_t add_size; 232 int idx; 233 234 list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 235 bool found_match = false; 236 237 res = add_res->res; 238 /* skip resource that has been reset */ 239 if (!res->flags) 240 goto out; 241 242 /* skip this resource if not found in head list */ 243 list_for_each_entry(dev_res, head, list) { 244 if (dev_res->res == res) { 245 found_match = true; 246 break; 247 } 248 } 249 if (!found_match)/* just skip */ 250 continue; 251 252 idx = res - &add_res->dev->resource[0]; 253 add_size = add_res->add_size; 254 if (!resource_size(res)) { 255 res->start = add_res->start; 256 res->end = res->start + add_size - 1; 257 if (pci_assign_resource(add_res->dev, idx)) 258 reset_resource(res); 259 } else { 260 resource_size_t align = add_res->min_align; 261 res->flags |= add_res->flags & 262 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 263 if (pci_reassign_resource(add_res->dev, idx, 264 add_size, align)) 265 dev_printk(KERN_DEBUG, &add_res->dev->dev, 266 "failed to add %llx res[%d]=%pR\n", 267 (unsigned long long)add_size, 268 idx, res); 269 } 270 out: 271 list_del(&add_res->list); 272 kfree(add_res); 273 } 274 } 275 276 /** 277 * assign_requested_resources_sorted() - satisfy resource requests 278 * 279 * @head : head of the list tracking requests for resources 280 * @failed_list : head of the list tracking requests that could 281 * not be allocated 282 * 283 * Satisfy resource requests of each element in the list. Add 284 * requests that could not satisfied to the failed_list. 285 */ 286 static void assign_requested_resources_sorted(struct list_head *head, 287 struct list_head *fail_head) 288 { 289 struct resource *res; 290 struct pci_dev_resource *dev_res; 291 int idx; 292 293 list_for_each_entry(dev_res, head, list) { 294 res = dev_res->res; 295 idx = res - &dev_res->dev->resource[0]; 296 if (resource_size(res) && 297 pci_assign_resource(dev_res->dev, idx)) { 298 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) { 299 /* 300 * if the failed res is for ROM BAR, and it will 301 * be enabled later, don't add it to the list 302 */ 303 if (!((idx == PCI_ROM_RESOURCE) && 304 (!(res->flags & IORESOURCE_ROM_ENABLE)))) 305 add_to_failed_list(fail_head, 306 dev_res->dev, res); 307 } 308 reset_resource(res); 309 } 310 } 311 } 312 313 static void __assign_resources_sorted(struct list_head *head, 314 struct list_head *realloc_head, 315 struct list_head *fail_head) 316 { 317 /* 318 * Should not assign requested resources at first. 319 * they could be adjacent, so later reassign can not reallocate 320 * them one by one in parent resource window. 321 * Try to assign requested + add_size at begining 322 * if could do that, could get out early. 323 * if could not do that, we still try to assign requested at first, 324 * then try to reassign add_size for some resources. 325 */ 326 LIST_HEAD(save_head); 327 LIST_HEAD(local_fail_head); 328 struct pci_dev_resource *save_res; 329 struct pci_dev_resource *dev_res; 330 331 /* Check if optional add_size is there */ 332 if (!realloc_head || list_empty(realloc_head)) 333 goto requested_and_reassign; 334 335 /* Save original start, end, flags etc at first */ 336 list_for_each_entry(dev_res, head, list) { 337 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 338 free_list(&save_head); 339 goto requested_and_reassign; 340 } 341 } 342 343 /* Update res in head list with add_size in realloc_head list */ 344 list_for_each_entry(dev_res, head, list) 345 dev_res->res->end += get_res_add_size(realloc_head, 346 dev_res->res); 347 348 /* Try updated head list with add_size added */ 349 assign_requested_resources_sorted(head, &local_fail_head); 350 351 /* all assigned with add_size ? */ 352 if (list_empty(&local_fail_head)) { 353 /* Remove head list from realloc_head list */ 354 list_for_each_entry(dev_res, head, list) 355 remove_from_list(realloc_head, dev_res->res); 356 free_list(&save_head); 357 free_list(head); 358 return; 359 } 360 361 free_list(&local_fail_head); 362 /* Release assigned resource */ 363 list_for_each_entry(dev_res, head, list) 364 if (dev_res->res->parent) 365 release_resource(dev_res->res); 366 /* Restore start/end/flags from saved list */ 367 list_for_each_entry(save_res, &save_head, list) { 368 struct resource *res = save_res->res; 369 370 res->start = save_res->start; 371 res->end = save_res->end; 372 res->flags = save_res->flags; 373 } 374 free_list(&save_head); 375 376 requested_and_reassign: 377 /* Satisfy the must-have resource requests */ 378 assign_requested_resources_sorted(head, fail_head); 379 380 /* Try to satisfy any additional optional resource 381 requests */ 382 if (realloc_head) 383 reassign_resources_sorted(realloc_head, head); 384 free_list(head); 385 } 386 387 static void pdev_assign_resources_sorted(struct pci_dev *dev, 388 struct list_head *add_head, 389 struct list_head *fail_head) 390 { 391 LIST_HEAD(head); 392 393 __dev_sort_resources(dev, &head); 394 __assign_resources_sorted(&head, add_head, fail_head); 395 396 } 397 398 static void pbus_assign_resources_sorted(const struct pci_bus *bus, 399 struct list_head *realloc_head, 400 struct list_head *fail_head) 401 { 402 struct pci_dev *dev; 403 LIST_HEAD(head); 404 405 list_for_each_entry(dev, &bus->devices, bus_list) 406 __dev_sort_resources(dev, &head); 407 408 __assign_resources_sorted(&head, realloc_head, fail_head); 409 } 410 411 void pci_setup_cardbus(struct pci_bus *bus) 412 { 413 struct pci_dev *bridge = bus->self; 414 struct resource *res; 415 struct pci_bus_region region; 416 417 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", 418 bus->secondary, bus->subordinate); 419 420 res = bus->resource[0]; 421 pcibios_resource_to_bus(bridge, ®ion, res); 422 if (res->flags & IORESOURCE_IO) { 423 /* 424 * The IO resource is allocated a range twice as large as it 425 * would normally need. This allows us to set both IO regs. 426 */ 427 dev_info(&bridge->dev, " bridge window %pR\n", res); 428 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 429 region.start); 430 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 431 region.end); 432 } 433 434 res = bus->resource[1]; 435 pcibios_resource_to_bus(bridge, ®ion, res); 436 if (res->flags & IORESOURCE_IO) { 437 dev_info(&bridge->dev, " bridge window %pR\n", res); 438 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 439 region.start); 440 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 441 region.end); 442 } 443 444 res = bus->resource[2]; 445 pcibios_resource_to_bus(bridge, ®ion, res); 446 if (res->flags & IORESOURCE_MEM) { 447 dev_info(&bridge->dev, " bridge window %pR\n", res); 448 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 449 region.start); 450 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 451 region.end); 452 } 453 454 res = bus->resource[3]; 455 pcibios_resource_to_bus(bridge, ®ion, res); 456 if (res->flags & IORESOURCE_MEM) { 457 dev_info(&bridge->dev, " bridge window %pR\n", res); 458 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 459 region.start); 460 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 461 region.end); 462 } 463 } 464 EXPORT_SYMBOL(pci_setup_cardbus); 465 466 /* Initialize bridges with base/limit values we have collected. 467 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) 468 requires that if there is no I/O ports or memory behind the 469 bridge, corresponding range must be turned off by writing base 470 value greater than limit to the bridge's base/limit registers. 471 472 Note: care must be taken when updating I/O base/limit registers 473 of bridges which support 32-bit I/O. This update requires two 474 config space writes, so it's quite possible that an I/O window of 475 the bridge will have some undesirable address (e.g. 0) after the 476 first write. Ditto 64-bit prefetchable MMIO. */ 477 static void pci_setup_bridge_io(struct pci_bus *bus) 478 { 479 struct pci_dev *bridge = bus->self; 480 struct resource *res; 481 struct pci_bus_region region; 482 u32 l, io_upper16; 483 484 /* Set up the top and bottom of the PCI I/O segment for this bus. */ 485 res = bus->resource[0]; 486 pcibios_resource_to_bus(bridge, ®ion, res); 487 if (res->flags & IORESOURCE_IO) { 488 pci_read_config_dword(bridge, PCI_IO_BASE, &l); 489 l &= 0xffff0000; 490 l |= (region.start >> 8) & 0x00f0; 491 l |= region.end & 0xf000; 492 /* Set up upper 16 bits of I/O base/limit. */ 493 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 494 dev_info(&bridge->dev, " bridge window %pR\n", res); 495 } else { 496 /* Clear upper 16 bits of I/O base/limit. */ 497 io_upper16 = 0; 498 l = 0x00f0; 499 } 500 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 501 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 502 /* Update lower 16 bits of I/O base/limit. */ 503 pci_write_config_dword(bridge, PCI_IO_BASE, l); 504 /* Update upper 16 bits of I/O base/limit. */ 505 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 506 } 507 508 static void pci_setup_bridge_mmio(struct pci_bus *bus) 509 { 510 struct pci_dev *bridge = bus->self; 511 struct resource *res; 512 struct pci_bus_region region; 513 u32 l; 514 515 /* Set up the top and bottom of the PCI Memory segment for this bus. */ 516 res = bus->resource[1]; 517 pcibios_resource_to_bus(bridge, ®ion, res); 518 if (res->flags & IORESOURCE_MEM) { 519 l = (region.start >> 16) & 0xfff0; 520 l |= region.end & 0xfff00000; 521 dev_info(&bridge->dev, " bridge window %pR\n", res); 522 } else { 523 l = 0x0000fff0; 524 } 525 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 526 } 527 528 static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) 529 { 530 struct pci_dev *bridge = bus->self; 531 struct resource *res; 532 struct pci_bus_region region; 533 u32 l, bu, lu; 534 535 /* Clear out the upper 32 bits of PREF limit. 536 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily 537 disables PREF range, which is ok. */ 538 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 539 540 /* Set up PREF base/limit. */ 541 bu = lu = 0; 542 res = bus->resource[2]; 543 pcibios_resource_to_bus(bridge, ®ion, res); 544 if (res->flags & IORESOURCE_PREFETCH) { 545 l = (region.start >> 16) & 0xfff0; 546 l |= region.end & 0xfff00000; 547 if (res->flags & IORESOURCE_MEM_64) { 548 bu = upper_32_bits(region.start); 549 lu = upper_32_bits(region.end); 550 } 551 dev_info(&bridge->dev, " bridge window %pR\n", res); 552 } else { 553 l = 0x0000fff0; 554 } 555 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 556 557 /* Set the upper 32 bits of PREF base & limit. */ 558 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 559 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 560 } 561 562 static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 563 { 564 struct pci_dev *bridge = bus->self; 565 566 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", 567 bus->secondary, bus->subordinate); 568 569 if (type & IORESOURCE_IO) 570 pci_setup_bridge_io(bus); 571 572 if (type & IORESOURCE_MEM) 573 pci_setup_bridge_mmio(bus); 574 575 if (type & IORESOURCE_PREFETCH) 576 pci_setup_bridge_mmio_pref(bus); 577 578 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 579 } 580 581 void pci_setup_bridge(struct pci_bus *bus) 582 { 583 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 584 IORESOURCE_PREFETCH; 585 586 __pci_setup_bridge(bus, type); 587 } 588 589 /* Check whether the bridge supports optional I/O and 590 prefetchable memory ranges. If not, the respective 591 base/limit registers must be read-only and read as 0. */ 592 static void pci_bridge_check_ranges(struct pci_bus *bus) 593 { 594 u16 io; 595 u32 pmem; 596 struct pci_dev *bridge = bus->self; 597 struct resource *b_res; 598 599 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 600 b_res[1].flags |= IORESOURCE_MEM; 601 602 pci_read_config_word(bridge, PCI_IO_BASE, &io); 603 if (!io) { 604 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); 605 pci_read_config_word(bridge, PCI_IO_BASE, &io); 606 pci_write_config_word(bridge, PCI_IO_BASE, 0x0); 607 } 608 if (io) 609 b_res[0].flags |= IORESOURCE_IO; 610 /* DECchip 21050 pass 2 errata: the bridge may miss an address 611 disconnect boundary by one PCI data phase. 612 Workaround: do not use prefetching on this device. */ 613 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) 614 return; 615 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 616 if (!pmem) { 617 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 618 0xfff0fff0); 619 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); 620 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); 621 } 622 if (pmem) { 623 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 624 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == 625 PCI_PREF_RANGE_TYPE_64) { 626 b_res[2].flags |= IORESOURCE_MEM_64; 627 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; 628 } 629 } 630 631 /* double check if bridge does support 64 bit pref */ 632 if (b_res[2].flags & IORESOURCE_MEM_64) { 633 u32 mem_base_hi, tmp; 634 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, 635 &mem_base_hi); 636 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 637 0xffffffff); 638 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); 639 if (!tmp) 640 b_res[2].flags &= ~IORESOURCE_MEM_64; 641 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 642 mem_base_hi); 643 } 644 } 645 646 /* Helper function for sizing routines: find first available 647 bus resource of a given type. Note: we intentionally skip 648 the bus resources which have already been assigned (that is, 649 have non-NULL parent resource). */ 650 static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) 651 { 652 int i; 653 struct resource *r; 654 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 655 IORESOURCE_PREFETCH; 656 657 pci_bus_for_each_resource(bus, r, i) { 658 if (r == &ioport_resource || r == &iomem_resource) 659 continue; 660 if (r && (r->flags & type_mask) == type && !r->parent) 661 return r; 662 } 663 return NULL; 664 } 665 666 static resource_size_t calculate_iosize(resource_size_t size, 667 resource_size_t min_size, 668 resource_size_t size1, 669 resource_size_t old_size, 670 resource_size_t align) 671 { 672 if (size < min_size) 673 size = min_size; 674 if (old_size == 1 ) 675 old_size = 0; 676 /* To be fixed in 2.5: we should have sort of HAVE_ISA 677 flag in the struct pci_bus. */ 678 #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 679 size = (size & 0xff) + ((size & ~0xffUL) << 2); 680 #endif 681 size = ALIGN(size + size1, align); 682 if (size < old_size) 683 size = old_size; 684 return size; 685 } 686 687 static resource_size_t calculate_memsize(resource_size_t size, 688 resource_size_t min_size, 689 resource_size_t size1, 690 resource_size_t old_size, 691 resource_size_t align) 692 { 693 if (size < min_size) 694 size = min_size; 695 if (old_size == 1 ) 696 old_size = 0; 697 if (size < old_size) 698 size = old_size; 699 size = ALIGN(size + size1, align); 700 return size; 701 } 702 703 /** 704 * pbus_size_io() - size the io window of a given bus 705 * 706 * @bus : the bus 707 * @min_size : the minimum io window that must to be allocated 708 * @add_size : additional optional io window 709 * @realloc_head : track the additional io window on this list 710 * 711 * Sizing the IO windows of the PCI-PCI bridge is trivial, 712 * since these windows have 4K granularity and the IO ranges 713 * of non-bridge PCI devices are limited to 256 bytes. 714 * We must be careful with the ISA aliasing though. 715 */ 716 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 717 resource_size_t add_size, struct list_head *realloc_head) 718 { 719 struct pci_dev *dev; 720 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 721 unsigned long size = 0, size0 = 0, size1 = 0; 722 resource_size_t children_add_size = 0; 723 724 if (!b_res) 725 return; 726 727 list_for_each_entry(dev, &bus->devices, bus_list) { 728 int i; 729 730 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 731 struct resource *r = &dev->resource[i]; 732 unsigned long r_size; 733 734 if (r->parent || !(r->flags & IORESOURCE_IO)) 735 continue; 736 r_size = resource_size(r); 737 738 if (r_size < 0x400) 739 /* Might be re-aligned for ISA */ 740 size += r_size; 741 else 742 size1 += r_size; 743 744 if (realloc_head) 745 children_add_size += get_res_add_size(realloc_head, r); 746 } 747 } 748 size0 = calculate_iosize(size, min_size, size1, 749 resource_size(b_res), 4096); 750 if (children_add_size > add_size) 751 add_size = children_add_size; 752 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 753 calculate_iosize(size, min_size, add_size + size1, 754 resource_size(b_res), 4096); 755 if (!size0 && !size1) { 756 if (b_res->start || b_res->end) 757 dev_info(&bus->self->dev, "disabling bridge window " 758 "%pR to [bus %02x-%02x] (unused)\n", b_res, 759 bus->secondary, bus->subordinate); 760 b_res->flags = 0; 761 return; 762 } 763 /* Alignment of the IO window is always 4K */ 764 b_res->start = 4096; 765 b_res->end = b_res->start + size0 - 1; 766 b_res->flags |= IORESOURCE_STARTALIGN; 767 if (size1 > size0 && realloc_head) { 768 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096); 769 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 770 "%pR to [bus %02x-%02x] add_size %lx\n", b_res, 771 bus->secondary, bus->subordinate, size1-size0); 772 } 773 } 774 775 /** 776 * pbus_size_mem() - size the memory window of a given bus 777 * 778 * @bus : the bus 779 * @min_size : the minimum memory window that must to be allocated 780 * @add_size : additional optional memory window 781 * @realloc_head : track the additional memory window on this list 782 * 783 * Calculate the size of the bus and minimal alignment which 784 * guarantees that all child resources fit in this size. 785 */ 786 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 787 unsigned long type, resource_size_t min_size, 788 resource_size_t add_size, 789 struct list_head *realloc_head) 790 { 791 struct pci_dev *dev; 792 resource_size_t min_align, align, size, size0, size1; 793 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ 794 int order, max_order; 795 struct resource *b_res = find_free_bus_resource(bus, type); 796 unsigned int mem64_mask = 0; 797 resource_size_t children_add_size = 0; 798 799 if (!b_res) 800 return 0; 801 802 memset(aligns, 0, sizeof(aligns)); 803 max_order = 0; 804 size = 0; 805 806 mem64_mask = b_res->flags & IORESOURCE_MEM_64; 807 b_res->flags &= ~IORESOURCE_MEM_64; 808 809 list_for_each_entry(dev, &bus->devices, bus_list) { 810 int i; 811 812 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 813 struct resource *r = &dev->resource[i]; 814 resource_size_t r_size; 815 816 if (r->parent || (r->flags & mask) != type) 817 continue; 818 r_size = resource_size(r); 819 #ifdef CONFIG_PCI_IOV 820 /* put SRIOV requested res to the optional list */ 821 if (realloc_head && i >= PCI_IOV_RESOURCES && 822 i <= PCI_IOV_RESOURCE_END) { 823 r->end = r->start - 1; 824 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */); 825 children_add_size += r_size; 826 continue; 827 } 828 #endif 829 /* For bridges size != alignment */ 830 align = pci_resource_alignment(dev, r); 831 order = __ffs(align) - 20; 832 if (order > 11) { 833 dev_warn(&dev->dev, "disabling BAR %d: %pR " 834 "(bad alignment %#llx)\n", i, r, 835 (unsigned long long) align); 836 r->flags = 0; 837 continue; 838 } 839 size += r_size; 840 if (order < 0) 841 order = 0; 842 /* Exclude ranges with size > align from 843 calculation of the alignment. */ 844 if (r_size == align) 845 aligns[order] += align; 846 if (order > max_order) 847 max_order = order; 848 mem64_mask &= r->flags & IORESOURCE_MEM_64; 849 850 if (realloc_head) 851 children_add_size += get_res_add_size(realloc_head, r); 852 } 853 } 854 align = 0; 855 min_align = 0; 856 for (order = 0; order <= max_order; order++) { 857 resource_size_t align1 = 1; 858 859 align1 <<= (order + 20); 860 861 if (!align) 862 min_align = align1; 863 else if (ALIGN(align + min_align, min_align) < align1) 864 min_align = align1 >> 1; 865 align += aligns[order]; 866 } 867 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); 868 if (children_add_size > add_size) 869 add_size = children_add_size; 870 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 871 calculate_memsize(size, min_size, add_size, 872 resource_size(b_res), min_align); 873 if (!size0 && !size1) { 874 if (b_res->start || b_res->end) 875 dev_info(&bus->self->dev, "disabling bridge window " 876 "%pR to [bus %02x-%02x] (unused)\n", b_res, 877 bus->secondary, bus->subordinate); 878 b_res->flags = 0; 879 return 1; 880 } 881 b_res->start = min_align; 882 b_res->end = size0 + min_align - 1; 883 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; 884 if (size1 > size0 && realloc_head) { 885 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 886 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " 887 "%pR to [bus %02x-%02x] add_size %llx\n", b_res, 888 bus->secondary, bus->subordinate, (unsigned long long)size1-size0); 889 } 890 return 1; 891 } 892 893 unsigned long pci_cardbus_resource_alignment(struct resource *res) 894 { 895 if (res->flags & IORESOURCE_IO) 896 return pci_cardbus_io_size; 897 if (res->flags & IORESOURCE_MEM) 898 return pci_cardbus_mem_size; 899 return 0; 900 } 901 902 static void pci_bus_size_cardbus(struct pci_bus *bus, 903 struct list_head *realloc_head) 904 { 905 struct pci_dev *bridge = bus->self; 906 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 907 u16 ctrl; 908 909 /* 910 * Reserve some resources for CardBus. We reserve 911 * a fixed amount of bus space for CardBus bridges. 912 */ 913 b_res[0].start = 0; 914 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 915 if (realloc_head) 916 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */); 917 918 b_res[1].start = 0; 919 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 920 if (realloc_head) 921 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */); 922 923 /* 924 * Check whether prefetchable memory is supported 925 * by this bridge. 926 */ 927 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 928 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 929 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 930 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 931 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 932 } 933 934 /* 935 * If we have prefetchable memory support, allocate 936 * two regions. Otherwise, allocate one region of 937 * twice the size. 938 */ 939 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 940 b_res[2].start = 0; 941 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 942 if (realloc_head) 943 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */); 944 945 b_res[3].start = 0; 946 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 947 if (realloc_head) 948 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */); 949 } else { 950 b_res[3].start = 0; 951 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 952 if (realloc_head) 953 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */); 954 } 955 956 /* set the size of the resource to zero, so that the resource does not 957 * get assigned during required-resource allocation cycle but gets assigned 958 * during the optional-resource allocation cycle. 959 */ 960 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1; 961 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0; 962 } 963 964 void __ref __pci_bus_size_bridges(struct pci_bus *bus, 965 struct list_head *realloc_head) 966 { 967 struct pci_dev *dev; 968 unsigned long mask, prefmask; 969 resource_size_t additional_mem_size = 0, additional_io_size = 0; 970 971 list_for_each_entry(dev, &bus->devices, bus_list) { 972 struct pci_bus *b = dev->subordinate; 973 if (!b) 974 continue; 975 976 switch (dev->class >> 8) { 977 case PCI_CLASS_BRIDGE_CARDBUS: 978 pci_bus_size_cardbus(b, realloc_head); 979 break; 980 981 case PCI_CLASS_BRIDGE_PCI: 982 default: 983 __pci_bus_size_bridges(b, realloc_head); 984 break; 985 } 986 } 987 988 /* The root bus? */ 989 if (!bus->self) 990 return; 991 992 switch (bus->self->class >> 8) { 993 case PCI_CLASS_BRIDGE_CARDBUS: 994 /* don't size cardbuses yet. */ 995 break; 996 997 case PCI_CLASS_BRIDGE_PCI: 998 pci_bridge_check_ranges(bus); 999 if (bus->self->is_hotplug_bridge) { 1000 additional_io_size = pci_hotplug_io_size; 1001 additional_mem_size = pci_hotplug_mem_size; 1002 } 1003 /* 1004 * Follow thru 1005 */ 1006 default: 1007 pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 1008 additional_io_size, realloc_head); 1009 /* If the bridge supports prefetchable range, size it 1010 separately. If it doesn't, or its prefetchable window 1011 has already been allocated by arch code, try 1012 non-prefetchable range for both types of PCI memory 1013 resources. */ 1014 mask = IORESOURCE_MEM; 1015 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 1016 if (pbus_size_mem(bus, prefmask, prefmask, 1017 realloc_head ? 0 : additional_mem_size, 1018 additional_mem_size, realloc_head)) 1019 mask = prefmask; /* Success, size non-prefetch only. */ 1020 else 1021 additional_mem_size += additional_mem_size; 1022 pbus_size_mem(bus, mask, IORESOURCE_MEM, 1023 realloc_head ? 0 : additional_mem_size, 1024 additional_mem_size, realloc_head); 1025 break; 1026 } 1027 } 1028 1029 void __ref pci_bus_size_bridges(struct pci_bus *bus) 1030 { 1031 __pci_bus_size_bridges(bus, NULL); 1032 } 1033 EXPORT_SYMBOL(pci_bus_size_bridges); 1034 1035 static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 1036 struct list_head *realloc_head, 1037 struct list_head *fail_head) 1038 { 1039 struct pci_bus *b; 1040 struct pci_dev *dev; 1041 1042 pbus_assign_resources_sorted(bus, realloc_head, fail_head); 1043 1044 list_for_each_entry(dev, &bus->devices, bus_list) { 1045 b = dev->subordinate; 1046 if (!b) 1047 continue; 1048 1049 __pci_bus_assign_resources(b, realloc_head, fail_head); 1050 1051 switch (dev->class >> 8) { 1052 case PCI_CLASS_BRIDGE_PCI: 1053 if (!pci_is_enabled(dev)) 1054 pci_setup_bridge(b); 1055 break; 1056 1057 case PCI_CLASS_BRIDGE_CARDBUS: 1058 pci_setup_cardbus(b); 1059 break; 1060 1061 default: 1062 dev_info(&dev->dev, "not setting up bridge for bus " 1063 "%04x:%02x\n", pci_domain_nr(b), b->number); 1064 break; 1065 } 1066 } 1067 } 1068 1069 void __ref pci_bus_assign_resources(const struct pci_bus *bus) 1070 { 1071 __pci_bus_assign_resources(bus, NULL, NULL); 1072 } 1073 EXPORT_SYMBOL(pci_bus_assign_resources); 1074 1075 static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 1076 struct list_head *add_head, 1077 struct list_head *fail_head) 1078 { 1079 struct pci_bus *b; 1080 1081 pdev_assign_resources_sorted((struct pci_dev *)bridge, 1082 add_head, fail_head); 1083 1084 b = bridge->subordinate; 1085 if (!b) 1086 return; 1087 1088 __pci_bus_assign_resources(b, add_head, fail_head); 1089 1090 switch (bridge->class >> 8) { 1091 case PCI_CLASS_BRIDGE_PCI: 1092 pci_setup_bridge(b); 1093 break; 1094 1095 case PCI_CLASS_BRIDGE_CARDBUS: 1096 pci_setup_cardbus(b); 1097 break; 1098 1099 default: 1100 dev_info(&bridge->dev, "not setting up bridge for bus " 1101 "%04x:%02x\n", pci_domain_nr(b), b->number); 1102 break; 1103 } 1104 } 1105 static void pci_bridge_release_resources(struct pci_bus *bus, 1106 unsigned long type) 1107 { 1108 int idx; 1109 bool changed = false; 1110 struct pci_dev *dev; 1111 struct resource *r; 1112 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1113 IORESOURCE_PREFETCH; 1114 1115 dev = bus->self; 1116 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; 1117 idx++) { 1118 r = &dev->resource[idx]; 1119 if ((r->flags & type_mask) != type) 1120 continue; 1121 if (!r->parent) 1122 continue; 1123 /* 1124 * if there are children under that, we should release them 1125 * all 1126 */ 1127 release_child_resources(r); 1128 if (!release_resource(r)) { 1129 dev_printk(KERN_DEBUG, &dev->dev, 1130 "resource %d %pR released\n", idx, r); 1131 /* keep the old size */ 1132 r->end = resource_size(r) - 1; 1133 r->start = 0; 1134 r->flags = 0; 1135 changed = true; 1136 } 1137 } 1138 1139 if (changed) { 1140 /* avoiding touch the one without PREF */ 1141 if (type & IORESOURCE_PREFETCH) 1142 type = IORESOURCE_PREFETCH; 1143 __pci_setup_bridge(bus, type); 1144 } 1145 } 1146 1147 enum release_type { 1148 leaf_only, 1149 whole_subtree, 1150 }; 1151 /* 1152 * try to release pci bridge resources that is from leaf bridge, 1153 * so we can allocate big new one later 1154 */ 1155 static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, 1156 unsigned long type, 1157 enum release_type rel_type) 1158 { 1159 struct pci_dev *dev; 1160 bool is_leaf_bridge = true; 1161 1162 list_for_each_entry(dev, &bus->devices, bus_list) { 1163 struct pci_bus *b = dev->subordinate; 1164 if (!b) 1165 continue; 1166 1167 is_leaf_bridge = false; 1168 1169 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 1170 continue; 1171 1172 if (rel_type == whole_subtree) 1173 pci_bus_release_bridge_resources(b, type, 1174 whole_subtree); 1175 } 1176 1177 if (pci_is_root_bus(bus)) 1178 return; 1179 1180 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 1181 return; 1182 1183 if ((rel_type == whole_subtree) || is_leaf_bridge) 1184 pci_bridge_release_resources(bus, type); 1185 } 1186 1187 static void pci_bus_dump_res(struct pci_bus *bus) 1188 { 1189 struct resource *res; 1190 int i; 1191 1192 pci_bus_for_each_resource(bus, res, i) { 1193 if (!res || !res->end || !res->flags) 1194 continue; 1195 1196 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); 1197 } 1198 } 1199 1200 static void pci_bus_dump_resources(struct pci_bus *bus) 1201 { 1202 struct pci_bus *b; 1203 struct pci_dev *dev; 1204 1205 1206 pci_bus_dump_res(bus); 1207 1208 list_for_each_entry(dev, &bus->devices, bus_list) { 1209 b = dev->subordinate; 1210 if (!b) 1211 continue; 1212 1213 pci_bus_dump_resources(b); 1214 } 1215 } 1216 1217 static int __init pci_bus_get_depth(struct pci_bus *bus) 1218 { 1219 int depth = 0; 1220 struct pci_dev *dev; 1221 1222 list_for_each_entry(dev, &bus->devices, bus_list) { 1223 int ret; 1224 struct pci_bus *b = dev->subordinate; 1225 if (!b) 1226 continue; 1227 1228 ret = pci_bus_get_depth(b); 1229 if (ret + 1 > depth) 1230 depth = ret + 1; 1231 } 1232 1233 return depth; 1234 } 1235 static int __init pci_get_max_depth(void) 1236 { 1237 int depth = 0; 1238 struct pci_bus *bus; 1239 1240 list_for_each_entry(bus, &pci_root_buses, node) { 1241 int ret; 1242 1243 ret = pci_bus_get_depth(bus); 1244 if (ret > depth) 1245 depth = ret; 1246 } 1247 1248 return depth; 1249 } 1250 1251 1252 /* 1253 * first try will not touch pci bridge res 1254 * second and later try will clear small leaf bridge res 1255 * will stop till to the max deepth if can not find good one 1256 */ 1257 void __init 1258 pci_assign_unassigned_resources(void) 1259 { 1260 struct pci_bus *bus; 1261 LIST_HEAD(realloc_head); /* list of resources that 1262 want additional resources */ 1263 struct list_head *add_list = NULL; 1264 int tried_times = 0; 1265 enum release_type rel_type = leaf_only; 1266 LIST_HEAD(fail_head); 1267 struct pci_dev_resource *fail_res; 1268 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1269 IORESOURCE_PREFETCH; 1270 unsigned long failed_type; 1271 int pci_try_num = 1; 1272 1273 /* don't realloc if asked to do so */ 1274 if (pci_realloc_enabled()) { 1275 int max_depth = pci_get_max_depth(); 1276 1277 pci_try_num = max_depth + 1; 1278 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n", 1279 max_depth, pci_try_num); 1280 } 1281 1282 again: 1283 /* 1284 * last try will use add_list, otherwise will try good to have as 1285 * must have, so can realloc parent bridge resource 1286 */ 1287 if (tried_times + 1 == pci_try_num) 1288 add_list = &realloc_head; 1289 /* Depth first, calculate sizes and alignments of all 1290 subordinate buses. */ 1291 list_for_each_entry(bus, &pci_root_buses, node) 1292 __pci_bus_size_bridges(bus, add_list); 1293 1294 /* Depth last, allocate resources and update the hardware. */ 1295 list_for_each_entry(bus, &pci_root_buses, node) 1296 __pci_bus_assign_resources(bus, add_list, &fail_head); 1297 if (add_list) 1298 BUG_ON(!list_empty(add_list)); 1299 tried_times++; 1300 1301 /* any device complain? */ 1302 if (list_empty(&fail_head)) 1303 goto enable_and_dump; 1304 1305 failed_type = 0; 1306 list_for_each_entry(fail_res, &fail_head, list) 1307 failed_type |= fail_res->flags; 1308 1309 /* 1310 * io port are tight, don't try extra 1311 * or if reach the limit, don't want to try more 1312 */ 1313 failed_type &= type_mask; 1314 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) { 1315 free_list(&fail_head); 1316 goto enable_and_dump; 1317 } 1318 1319 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 1320 tried_times + 1); 1321 1322 /* third times and later will not check if it is leaf */ 1323 if ((tried_times + 1) > 2) 1324 rel_type = whole_subtree; 1325 1326 /* 1327 * Try to release leaf bridge's resources that doesn't fit resource of 1328 * child device under that bridge 1329 */ 1330 list_for_each_entry(fail_res, &fail_head, list) { 1331 bus = fail_res->dev->bus; 1332 pci_bus_release_bridge_resources(bus, 1333 fail_res->flags & type_mask, 1334 rel_type); 1335 } 1336 /* restore size and flags */ 1337 list_for_each_entry(fail_res, &fail_head, list) { 1338 struct resource *res = fail_res->res; 1339 1340 res->start = fail_res->start; 1341 res->end = fail_res->end; 1342 res->flags = fail_res->flags; 1343 if (fail_res->dev->subordinate) 1344 res->flags = 0; 1345 } 1346 free_list(&fail_head); 1347 1348 goto again; 1349 1350 enable_and_dump: 1351 /* Depth last, update the hardware. */ 1352 list_for_each_entry(bus, &pci_root_buses, node) 1353 pci_enable_bridges(bus); 1354 1355 /* dump the resource on buses */ 1356 list_for_each_entry(bus, &pci_root_buses, node) 1357 pci_bus_dump_resources(bus); 1358 } 1359 1360 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 1361 { 1362 struct pci_bus *parent = bridge->subordinate; 1363 LIST_HEAD(add_list); /* list of resources that 1364 want additional resources */ 1365 int tried_times = 0; 1366 LIST_HEAD(fail_head); 1367 struct pci_dev_resource *fail_res; 1368 int retval; 1369 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1370 IORESOURCE_PREFETCH; 1371 1372 again: 1373 __pci_bus_size_bridges(parent, &add_list); 1374 __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 1375 BUG_ON(!list_empty(&add_list)); 1376 tried_times++; 1377 1378 if (list_empty(&fail_head)) 1379 goto enable_all; 1380 1381 if (tried_times >= 2) { 1382 /* still fail, don't need to try more */ 1383 free_list(&fail_head); 1384 goto enable_all; 1385 } 1386 1387 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", 1388 tried_times + 1); 1389 1390 /* 1391 * Try to release leaf bridge's resources that doesn't fit resource of 1392 * child device under that bridge 1393 */ 1394 list_for_each_entry(fail_res, &fail_head, list) { 1395 struct pci_bus *bus = fail_res->dev->bus; 1396 unsigned long flags = fail_res->flags; 1397 1398 pci_bus_release_bridge_resources(bus, flags & type_mask, 1399 whole_subtree); 1400 } 1401 /* restore size and flags */ 1402 list_for_each_entry(fail_res, &fail_head, list) { 1403 struct resource *res = fail_res->res; 1404 1405 res->start = fail_res->start; 1406 res->end = fail_res->end; 1407 res->flags = fail_res->flags; 1408 if (fail_res->dev->subordinate) 1409 res->flags = 0; 1410 } 1411 free_list(&fail_head); 1412 1413 goto again; 1414 1415 enable_all: 1416 retval = pci_reenable_device(bridge); 1417 pci_set_master(bridge); 1418 pci_enable_bridges(parent); 1419 } 1420 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 1421 1422 #ifdef CONFIG_HOTPLUG 1423 /** 1424 * pci_rescan_bus - scan a PCI bus for devices. 1425 * @bus: PCI bus to scan 1426 * 1427 * Scan a PCI bus and child buses for new devices, adds them, 1428 * and enables them. 1429 * 1430 * Returns the max number of subordinate bus discovered. 1431 */ 1432 unsigned int __ref pci_rescan_bus(struct pci_bus *bus) 1433 { 1434 unsigned int max; 1435 struct pci_dev *dev; 1436 LIST_HEAD(add_list); /* list of resources that 1437 want additional resources */ 1438 1439 max = pci_scan_child_bus(bus); 1440 1441 down_read(&pci_bus_sem); 1442 list_for_each_entry(dev, &bus->devices, bus_list) 1443 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 1444 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 1445 if (dev->subordinate) 1446 __pci_bus_size_bridges(dev->subordinate, 1447 &add_list); 1448 up_read(&pci_bus_sem); 1449 __pci_bus_assign_resources(bus, &add_list, NULL); 1450 BUG_ON(!list_empty(&add_list)); 1451 1452 pci_enable_bridges(bus); 1453 pci_bus_add_devices(bus); 1454 1455 return max; 1456 } 1457 EXPORT_SYMBOL_GPL(pci_rescan_bus); 1458 #endif 1459