1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Support routines for initializing a PCI subsystem 4 * 5 * Extruded from code written by 6 * Dave Rusling (david.rusling@reo.mts.dec.com) 7 * David Mosberger (davidm@cs.arizona.edu) 8 * David Miller (davem@redhat.com) 9 * 10 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 11 * PCI-PCI bridges cleanup, sorted resource allocation. 12 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> 13 * Converted to allocation in 3 passes, which gives 14 * tighter packing. Prefetchable range support. 15 */ 16 17 #include <linux/bitops.h> 18 #include <linux/init.h> 19 #include <linux/kernel.h> 20 #include <linux/module.h> 21 #include <linux/pci.h> 22 #include <linux/errno.h> 23 #include <linux/ioport.h> 24 #include <linux/cache.h> 25 #include <linux/limits.h> 26 #include <linux/sizes.h> 27 #include <linux/slab.h> 28 #include <linux/acpi.h> 29 #include "pci.h" 30 31 unsigned int pci_flags; 32 EXPORT_SYMBOL_GPL(pci_flags); 33 34 struct pci_dev_resource { 35 struct list_head list; 36 struct resource *res; 37 struct pci_dev *dev; 38 resource_size_t start; 39 resource_size_t end; 40 resource_size_t add_size; 41 resource_size_t min_align; 42 unsigned long flags; 43 }; 44 45 static void free_list(struct list_head *head) 46 { 47 struct pci_dev_resource *dev_res, *tmp; 48 49 list_for_each_entry_safe(dev_res, tmp, head, list) { 50 list_del(&dev_res->list); 51 kfree(dev_res); 52 } 53 } 54 55 /** 56 * add_to_list() - Add a new resource tracker to the list 57 * @head: Head of the list 58 * @dev: Device to which the resource belongs 59 * @res: Resource to be tracked 60 * @add_size: Additional size to be optionally added to the resource 61 * @min_align: Minimum memory window alignment 62 */ 63 static int add_to_list(struct list_head *head, struct pci_dev *dev, 64 struct resource *res, resource_size_t add_size, 65 resource_size_t min_align) 66 { 67 struct pci_dev_resource *tmp; 68 69 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 70 if (!tmp) 71 return -ENOMEM; 72 73 tmp->res = res; 74 tmp->dev = dev; 75 tmp->start = res->start; 76 tmp->end = res->end; 77 tmp->flags = res->flags; 78 tmp->add_size = add_size; 79 tmp->min_align = min_align; 80 81 list_add(&tmp->list, head); 82 83 return 0; 84 } 85 86 static void remove_from_list(struct list_head *head, struct resource *res) 87 { 88 struct pci_dev_resource *dev_res, *tmp; 89 90 list_for_each_entry_safe(dev_res, tmp, head, list) { 91 if (dev_res->res == res) { 92 list_del(&dev_res->list); 93 kfree(dev_res); 94 break; 95 } 96 } 97 } 98 99 static struct pci_dev_resource *res_to_dev_res(struct list_head *head, 100 struct resource *res) 101 { 102 struct pci_dev_resource *dev_res; 103 104 list_for_each_entry(dev_res, head, list) { 105 if (dev_res->res == res) 106 return dev_res; 107 } 108 109 return NULL; 110 } 111 112 static resource_size_t get_res_add_size(struct list_head *head, 113 struct resource *res) 114 { 115 struct pci_dev_resource *dev_res; 116 117 dev_res = res_to_dev_res(head, res); 118 return dev_res ? dev_res->add_size : 0; 119 } 120 121 static resource_size_t get_res_add_align(struct list_head *head, 122 struct resource *res) 123 { 124 struct pci_dev_resource *dev_res; 125 126 dev_res = res_to_dev_res(head, res); 127 return dev_res ? dev_res->min_align : 0; 128 } 129 130 static void restore_dev_resource(struct pci_dev_resource *dev_res) 131 { 132 struct resource *res = dev_res->res; 133 134 res->start = dev_res->start; 135 res->end = dev_res->end; 136 res->flags = dev_res->flags; 137 } 138 139 static bool pdev_resources_assignable(struct pci_dev *dev) 140 { 141 u16 class = dev->class >> 8, command; 142 143 /* Don't touch classless devices or host bridges or IOAPICs */ 144 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) 145 return false; 146 147 /* Don't touch IOAPIC devices already enabled by firmware */ 148 if (class == PCI_CLASS_SYSTEM_PIC) { 149 pci_read_config_word(dev, PCI_COMMAND, &command); 150 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) 151 return false; 152 } 153 154 return true; 155 } 156 157 /* Sort resources by alignment */ 158 static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) 159 { 160 struct resource *r; 161 int i; 162 163 if (!pdev_resources_assignable(dev)) 164 return; 165 166 pci_dev_for_each_resource(dev, r, i) { 167 const char *r_name = pci_resource_name(dev, i); 168 struct pci_dev_resource *dev_res, *tmp; 169 resource_size_t r_align; 170 struct list_head *n; 171 172 if (r->flags & IORESOURCE_PCI_FIXED) 173 continue; 174 175 if (!(r->flags) || r->parent) 176 continue; 177 178 r_align = pci_resource_alignment(dev, r); 179 if (!r_align) { 180 pci_warn(dev, "%s %pR: alignment must not be zero\n", 181 r_name, r); 182 continue; 183 } 184 185 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 186 if (!tmp) 187 panic("%s: kzalloc() failed!\n", __func__); 188 tmp->res = r; 189 tmp->dev = dev; 190 191 /* Fallback is smallest one or list is empty */ 192 n = head; 193 list_for_each_entry(dev_res, head, list) { 194 resource_size_t align; 195 196 align = pci_resource_alignment(dev_res->dev, 197 dev_res->res); 198 199 if (r_align > align) { 200 n = &dev_res->list; 201 break; 202 } 203 } 204 /* Insert it just before n */ 205 list_add_tail(&tmp->list, n); 206 } 207 } 208 209 bool pci_resource_is_optional(const struct pci_dev *dev, int resno) 210 { 211 const struct resource *res = pci_resource_n(dev, resno); 212 213 if (pci_resource_is_iov(resno)) 214 return true; 215 if (resno == PCI_ROM_RESOURCE && !(res->flags & IORESOURCE_ROM_ENABLE)) 216 return true; 217 218 return false; 219 } 220 221 static inline void reset_resource(struct resource *res) 222 { 223 res->start = 0; 224 res->end = 0; 225 res->flags = 0; 226 } 227 228 /** 229 * reassign_resources_sorted() - Satisfy any additional resource requests 230 * 231 * @realloc_head: Head of the list tracking requests requiring 232 * additional resources 233 * @head: Head of the list tracking requests with allocated 234 * resources 235 * 236 * Walk through each element of the realloc_head and try to procure additional 237 * resources for the element, provided the element is in the head list. 238 */ 239 static void reassign_resources_sorted(struct list_head *realloc_head, 240 struct list_head *head) 241 { 242 struct pci_dev_resource *add_res, *tmp; 243 struct pci_dev_resource *dev_res; 244 struct pci_dev *dev; 245 struct resource *res; 246 const char *res_name; 247 resource_size_t add_size, align; 248 int idx; 249 250 list_for_each_entry_safe(add_res, tmp, realloc_head, list) { 251 bool found_match = false; 252 253 res = add_res->res; 254 dev = add_res->dev; 255 idx = pci_resource_num(dev, res); 256 257 /* 258 * Skip resource that failed the earlier assignment and is 259 * not optional as it would just fail again. 260 */ 261 if (!res->parent && resource_size(res) && 262 !pci_resource_is_optional(dev, idx)) 263 goto out; 264 265 /* Skip this resource if not found in head list */ 266 list_for_each_entry(dev_res, head, list) { 267 if (dev_res->res == res) { 268 found_match = true; 269 break; 270 } 271 } 272 if (!found_match) /* Just skip */ 273 continue; 274 275 res_name = pci_resource_name(dev, idx); 276 add_size = add_res->add_size; 277 align = add_res->min_align; 278 if (!res->parent) { 279 resource_set_range(res, align, 280 resource_size(res) + add_size); 281 if (pci_assign_resource(dev, idx)) { 282 pci_dbg(dev, 283 "%s %pR: ignoring failure in optional allocation\n", 284 res_name, res); 285 } 286 } else if (add_size > 0) { 287 res->flags |= add_res->flags & 288 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 289 if (pci_reassign_resource(dev, idx, add_size, align)) 290 pci_info(dev, "%s %pR: failed to add optional %llx\n", 291 res_name, res, 292 (unsigned long long) add_size); 293 } 294 out: 295 list_del(&add_res->list); 296 kfree(add_res); 297 } 298 } 299 300 /** 301 * assign_requested_resources_sorted() - Satisfy resource requests 302 * 303 * @head: Head of the list tracking requests for resources 304 * @fail_head: Head of the list tracking requests that could not be 305 * allocated 306 * @optional: Assign also optional resources 307 * 308 * Satisfy resource requests of each element in the list. Add requests that 309 * could not be satisfied to the failed_list. 310 */ 311 static void assign_requested_resources_sorted(struct list_head *head, 312 struct list_head *fail_head, 313 bool optional) 314 { 315 struct pci_dev_resource *dev_res; 316 struct resource *res; 317 struct pci_dev *dev; 318 bool optional_res; 319 int idx; 320 321 list_for_each_entry(dev_res, head, list) { 322 res = dev_res->res; 323 dev = dev_res->dev; 324 idx = pci_resource_num(dev, res); 325 optional_res = pci_resource_is_optional(dev, idx); 326 327 if (!resource_size(res)) 328 continue; 329 330 if (!optional && optional_res) 331 continue; 332 333 if (pci_assign_resource(dev, idx)) { 334 if (fail_head) { 335 add_to_list(fail_head, dev, res, 336 0 /* don't care */, 337 0 /* don't care */); 338 } 339 } 340 } 341 } 342 343 static unsigned long pci_fail_res_type_mask(struct list_head *fail_head) 344 { 345 struct pci_dev_resource *fail_res; 346 unsigned long mask = 0; 347 348 /* Check failed type */ 349 list_for_each_entry(fail_res, fail_head, list) 350 mask |= fail_res->flags; 351 352 /* 353 * One pref failed resource will set IORESOURCE_MEM, as we can 354 * allocate pref in non-pref range. Will release all assigned 355 * non-pref sibling resources according to that bit. 356 */ 357 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH); 358 } 359 360 static bool pci_need_to_release(unsigned long mask, struct resource *res) 361 { 362 if (res->flags & IORESOURCE_IO) 363 return !!(mask & IORESOURCE_IO); 364 365 /* Check pref at first */ 366 if (res->flags & IORESOURCE_PREFETCH) { 367 if (mask & IORESOURCE_PREFETCH) 368 return true; 369 /* Count pref if its parent is non-pref */ 370 else if ((mask & IORESOURCE_MEM) && 371 !(res->parent->flags & IORESOURCE_PREFETCH)) 372 return true; 373 else 374 return false; 375 } 376 377 if (res->flags & IORESOURCE_MEM) 378 return !!(mask & IORESOURCE_MEM); 379 380 return false; /* Should not get here */ 381 } 382 383 /* Return: @true if assignment of a required resource failed. */ 384 static bool pci_required_resource_failed(struct list_head *fail_head) 385 { 386 struct pci_dev_resource *fail_res; 387 388 list_for_each_entry(fail_res, fail_head, list) { 389 int idx = pci_resource_num(fail_res->dev, fail_res->res); 390 391 if (!pci_resource_is_optional(fail_res->dev, idx)) 392 return true; 393 } 394 return false; 395 } 396 397 static void __assign_resources_sorted(struct list_head *head, 398 struct list_head *realloc_head, 399 struct list_head *fail_head) 400 { 401 /* 402 * Should not assign requested resources at first. They could be 403 * adjacent, so later reassign can not reallocate them one by one in 404 * parent resource window. 405 * 406 * Try to assign required and any optional resources at beginning 407 * (add_size included). If all required resources were successfully 408 * assigned, get out early. If could not do that, we still try to 409 * assign required at first, then try to reassign some optional 410 * resources. 411 * 412 * Separate three resource type checking if we need to release 413 * assigned resource after requested + add_size try. 414 * 415 * 1. If IO port assignment fails, will release assigned IO 416 * port. 417 * 2. If pref MMIO assignment fails, release assigned pref 418 * MMIO. If assigned pref MMIO's parent is non-pref MMIO 419 * and non-pref MMIO assignment fails, will release that 420 * assigned pref MMIO. 421 * 3. If non-pref MMIO assignment fails or pref MMIO 422 * assignment fails, will release assigned non-pref MMIO. 423 */ 424 LIST_HEAD(save_head); 425 LIST_HEAD(local_fail_head); 426 LIST_HEAD(dummy_head); 427 struct pci_dev_resource *save_res; 428 struct pci_dev_resource *dev_res, *tmp_res, *dev_res2; 429 struct resource *res; 430 struct pci_dev *dev; 431 const char *res_name; 432 int idx; 433 unsigned long fail_type; 434 resource_size_t add_align, align; 435 436 if (!realloc_head) 437 realloc_head = &dummy_head; 438 439 /* Check if optional add_size is there */ 440 if (list_empty(realloc_head)) 441 goto assign; 442 443 /* Save original start, end, flags etc at first */ 444 list_for_each_entry(dev_res, head, list) { 445 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { 446 free_list(&save_head); 447 goto assign; 448 } 449 } 450 451 /* Update res in head list with add_size in realloc_head list */ 452 list_for_each_entry_safe(dev_res, tmp_res, head, list) { 453 res = dev_res->res; 454 455 res->end += get_res_add_size(realloc_head, res); 456 457 /* 458 * There are two kinds of additional resources in the list: 459 * 1. bridge resource -- IORESOURCE_STARTALIGN 460 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN 461 * Here just fix the additional alignment for bridge 462 */ 463 if (!(res->flags & IORESOURCE_STARTALIGN)) 464 continue; 465 466 add_align = get_res_add_align(realloc_head, res); 467 468 /* 469 * The "head" list is sorted by alignment so resources with 470 * bigger alignment will be assigned first. After we 471 * change the alignment of a dev_res in "head" list, we 472 * need to reorder the list by alignment to make it 473 * consistent. 474 */ 475 if (add_align > res->start) { 476 resource_set_range(res, add_align, resource_size(res)); 477 478 list_for_each_entry(dev_res2, head, list) { 479 align = pci_resource_alignment(dev_res2->dev, 480 dev_res2->res); 481 if (add_align > align) { 482 list_move_tail(&dev_res->list, 483 &dev_res2->list); 484 break; 485 } 486 } 487 } 488 489 } 490 491 assign: 492 assign_requested_resources_sorted(head, &local_fail_head, true); 493 494 /* All non-optional resources assigned? */ 495 if (list_empty(&local_fail_head)) { 496 /* Remove head list from realloc_head list */ 497 list_for_each_entry(dev_res, head, list) 498 remove_from_list(realloc_head, dev_res->res); 499 free_list(&save_head); 500 goto out; 501 } 502 503 /* Without realloc_head and only optional fails, nothing more to do. */ 504 if (!pci_required_resource_failed(&local_fail_head) && 505 list_empty(realloc_head)) { 506 list_for_each_entry(save_res, &save_head, list) { 507 struct resource *res = save_res->res; 508 509 if (res->parent) 510 continue; 511 512 restore_dev_resource(save_res); 513 } 514 free_list(&local_fail_head); 515 free_list(&save_head); 516 goto out; 517 } 518 519 /* Check failed type */ 520 fail_type = pci_fail_res_type_mask(&local_fail_head); 521 /* Remove not need to be released assigned res from head list etc */ 522 list_for_each_entry_safe(dev_res, tmp_res, head, list) { 523 res = dev_res->res; 524 525 if (res->parent && !pci_need_to_release(fail_type, res)) { 526 /* Remove it from realloc_head list */ 527 remove_from_list(realloc_head, res); 528 remove_from_list(&save_head, res); 529 list_del(&dev_res->list); 530 kfree(dev_res); 531 } 532 } 533 534 free_list(&local_fail_head); 535 /* Release assigned resource */ 536 list_for_each_entry(dev_res, head, list) { 537 res = dev_res->res; 538 dev = dev_res->dev; 539 540 if (!res->parent) 541 continue; 542 543 idx = pci_resource_num(dev, res); 544 res_name = pci_resource_name(dev, idx); 545 pci_dbg(dev, "%s %pR: releasing\n", res_name, res); 546 547 release_resource(res); 548 } 549 /* Restore start/end/flags from saved list */ 550 list_for_each_entry(save_res, &save_head, list) 551 restore_dev_resource(save_res); 552 free_list(&save_head); 553 554 /* Satisfy the must-have resource requests */ 555 assign_requested_resources_sorted(head, NULL, false); 556 557 /* Try to satisfy any additional optional resource requests */ 558 if (!list_empty(realloc_head)) 559 reassign_resources_sorted(realloc_head, head); 560 561 out: 562 /* Reset any failed resource, cannot use fail_head as it can be NULL. */ 563 list_for_each_entry(dev_res, head, list) { 564 res = dev_res->res; 565 dev = dev_res->dev; 566 567 if (res->parent) 568 continue; 569 570 if (fail_head) { 571 add_to_list(fail_head, dev, res, 572 0 /* don't care */, 573 0 /* don't care */); 574 } 575 576 reset_resource(res); 577 } 578 579 free_list(head); 580 } 581 582 static void pdev_assign_resources_sorted(struct pci_dev *dev, 583 struct list_head *add_head, 584 struct list_head *fail_head) 585 { 586 LIST_HEAD(head); 587 588 pdev_sort_resources(dev, &head); 589 __assign_resources_sorted(&head, add_head, fail_head); 590 591 } 592 593 static void pbus_assign_resources_sorted(const struct pci_bus *bus, 594 struct list_head *realloc_head, 595 struct list_head *fail_head) 596 { 597 struct pci_dev *dev; 598 LIST_HEAD(head); 599 600 list_for_each_entry(dev, &bus->devices, bus_list) 601 pdev_sort_resources(dev, &head); 602 603 __assign_resources_sorted(&head, realloc_head, fail_head); 604 } 605 606 void pci_setup_cardbus(struct pci_bus *bus) 607 { 608 struct pci_dev *bridge = bus->self; 609 struct resource *res; 610 struct pci_bus_region region; 611 612 pci_info(bridge, "CardBus bridge to %pR\n", 613 &bus->busn_res); 614 615 res = bus->resource[0]; 616 pcibios_resource_to_bus(bridge->bus, ®ion, res); 617 if (res->flags & IORESOURCE_IO) { 618 /* 619 * The IO resource is allocated a range twice as large as it 620 * would normally need. This allows us to set both IO regs. 621 */ 622 pci_info(bridge, " bridge window %pR\n", res); 623 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 624 region.start); 625 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 626 region.end); 627 } 628 629 res = bus->resource[1]; 630 pcibios_resource_to_bus(bridge->bus, ®ion, res); 631 if (res->flags & IORESOURCE_IO) { 632 pci_info(bridge, " bridge window %pR\n", res); 633 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 634 region.start); 635 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 636 region.end); 637 } 638 639 res = bus->resource[2]; 640 pcibios_resource_to_bus(bridge->bus, ®ion, res); 641 if (res->flags & IORESOURCE_MEM) { 642 pci_info(bridge, " bridge window %pR\n", res); 643 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 644 region.start); 645 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 646 region.end); 647 } 648 649 res = bus->resource[3]; 650 pcibios_resource_to_bus(bridge->bus, ®ion, res); 651 if (res->flags & IORESOURCE_MEM) { 652 pci_info(bridge, " bridge window %pR\n", res); 653 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 654 region.start); 655 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 656 region.end); 657 } 658 } 659 EXPORT_SYMBOL(pci_setup_cardbus); 660 661 /* 662 * Initialize bridges with base/limit values we have collected. PCI-to-PCI 663 * Bridge Architecture Specification rev. 1.1 (1998) requires that if there 664 * are no I/O ports or memory behind the bridge, the corresponding range 665 * must be turned off by writing base value greater than limit to the 666 * bridge's base/limit registers. 667 * 668 * Note: care must be taken when updating I/O base/limit registers of 669 * bridges which support 32-bit I/O. This update requires two config space 670 * writes, so it's quite possible that an I/O window of the bridge will 671 * have some undesirable address (e.g. 0) after the first write. Ditto 672 * 64-bit prefetchable MMIO. 673 */ 674 static void pci_setup_bridge_io(struct pci_dev *bridge) 675 { 676 struct resource *res; 677 const char *res_name; 678 struct pci_bus_region region; 679 unsigned long io_mask; 680 u8 io_base_lo, io_limit_lo; 681 u16 l; 682 u32 io_upper16; 683 684 io_mask = PCI_IO_RANGE_MASK; 685 if (bridge->io_window_1k) 686 io_mask = PCI_IO_1K_RANGE_MASK; 687 688 /* Set up the top and bottom of the PCI I/O segment for this bus */ 689 res = &bridge->resource[PCI_BRIDGE_IO_WINDOW]; 690 res_name = pci_resource_name(bridge, PCI_BRIDGE_IO_WINDOW); 691 pcibios_resource_to_bus(bridge->bus, ®ion, res); 692 if (res->flags & IORESOURCE_IO) { 693 pci_read_config_word(bridge, PCI_IO_BASE, &l); 694 io_base_lo = (region.start >> 8) & io_mask; 695 io_limit_lo = (region.end >> 8) & io_mask; 696 l = ((u16) io_limit_lo << 8) | io_base_lo; 697 /* Set up upper 16 bits of I/O base/limit */ 698 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 699 pci_info(bridge, " %s %pR\n", res_name, res); 700 } else { 701 /* Clear upper 16 bits of I/O base/limit */ 702 io_upper16 = 0; 703 l = 0x00f0; 704 } 705 /* Temporarily disable the I/O range before updating PCI_IO_BASE */ 706 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 707 /* Update lower 16 bits of I/O base/limit */ 708 pci_write_config_word(bridge, PCI_IO_BASE, l); 709 /* Update upper 16 bits of I/O base/limit */ 710 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); 711 } 712 713 static void pci_setup_bridge_mmio(struct pci_dev *bridge) 714 { 715 struct resource *res; 716 const char *res_name; 717 struct pci_bus_region region; 718 u32 l; 719 720 /* Set up the top and bottom of the PCI Memory segment for this bus */ 721 res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW]; 722 res_name = pci_resource_name(bridge, PCI_BRIDGE_MEM_WINDOW); 723 pcibios_resource_to_bus(bridge->bus, ®ion, res); 724 if (res->flags & IORESOURCE_MEM) { 725 l = (region.start >> 16) & 0xfff0; 726 l |= region.end & 0xfff00000; 727 pci_info(bridge, " %s %pR\n", res_name, res); 728 } else { 729 l = 0x0000fff0; 730 } 731 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 732 } 733 734 static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge) 735 { 736 struct resource *res; 737 const char *res_name; 738 struct pci_bus_region region; 739 u32 l, bu, lu; 740 741 /* 742 * Clear out the upper 32 bits of PREF limit. If 743 * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables 744 * PREF range, which is ok. 745 */ 746 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); 747 748 /* Set up PREF base/limit */ 749 bu = lu = 0; 750 res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 751 res_name = pci_resource_name(bridge, PCI_BRIDGE_PREF_MEM_WINDOW); 752 pcibios_resource_to_bus(bridge->bus, ®ion, res); 753 if (res->flags & IORESOURCE_PREFETCH) { 754 l = (region.start >> 16) & 0xfff0; 755 l |= region.end & 0xfff00000; 756 if (res->flags & IORESOURCE_MEM_64) { 757 bu = upper_32_bits(region.start); 758 lu = upper_32_bits(region.end); 759 } 760 pci_info(bridge, " %s %pR\n", res_name, res); 761 } else { 762 l = 0x0000fff0; 763 } 764 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 765 766 /* Set the upper 32 bits of PREF base & limit */ 767 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); 768 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); 769 } 770 771 static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) 772 { 773 struct pci_dev *bridge = bus->self; 774 775 pci_info(bridge, "PCI bridge to %pR\n", 776 &bus->busn_res); 777 778 if (type & IORESOURCE_IO) 779 pci_setup_bridge_io(bridge); 780 781 if (type & IORESOURCE_MEM) 782 pci_setup_bridge_mmio(bridge); 783 784 if (type & IORESOURCE_PREFETCH) 785 pci_setup_bridge_mmio_pref(bridge); 786 787 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); 788 } 789 790 void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type) 791 { 792 } 793 794 static void pci_setup_bridge(struct pci_bus *bus) 795 { 796 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | 797 IORESOURCE_PREFETCH; 798 799 pcibios_setup_bridge(bus, type); 800 __pci_setup_bridge(bus, type); 801 } 802 803 804 int pci_claim_bridge_resource(struct pci_dev *bridge, int i) 805 { 806 if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END) 807 return 0; 808 809 if (pci_claim_resource(bridge, i) == 0) 810 return 0; /* Claimed the window */ 811 812 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI) 813 return 0; 814 815 if (!pci_bus_clip_resource(bridge, i)) 816 return -EINVAL; /* Clipping didn't change anything */ 817 818 switch (i) { 819 case PCI_BRIDGE_IO_WINDOW: 820 pci_setup_bridge_io(bridge); 821 break; 822 case PCI_BRIDGE_MEM_WINDOW: 823 pci_setup_bridge_mmio(bridge); 824 break; 825 case PCI_BRIDGE_PREF_MEM_WINDOW: 826 pci_setup_bridge_mmio_pref(bridge); 827 break; 828 default: 829 return -EINVAL; 830 } 831 832 if (pci_claim_resource(bridge, i) == 0) 833 return 0; /* Claimed a smaller window */ 834 835 return -EINVAL; 836 } 837 838 /* 839 * Check whether the bridge supports optional I/O and prefetchable memory 840 * ranges. If not, the respective base/limit registers must be read-only 841 * and read as 0. 842 */ 843 static void pci_bridge_check_ranges(struct pci_bus *bus) 844 { 845 struct pci_dev *bridge = bus->self; 846 struct resource *b_res; 847 848 b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW]; 849 b_res->flags |= IORESOURCE_MEM; 850 851 if (bridge->io_window) { 852 b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW]; 853 b_res->flags |= IORESOURCE_IO; 854 } 855 856 if (bridge->pref_window) { 857 b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 858 b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; 859 if (bridge->pref_64_window) { 860 b_res->flags |= IORESOURCE_MEM_64 | 861 PCI_PREF_RANGE_TYPE_64; 862 } 863 } 864 } 865 866 /* 867 * Helper function for sizing routines. Assigned resources have non-NULL 868 * parent resource. 869 * 870 * Return first unassigned resource of the correct type. If there is none, 871 * return first assigned resource of the correct type. If none of the 872 * above, return NULL. 873 * 874 * Returning an assigned resource of the correct type allows the caller to 875 * distinguish between already assigned and no resource of the correct type. 876 */ 877 static struct resource *find_bus_resource_of_type(struct pci_bus *bus, 878 unsigned long type_mask, 879 unsigned long type) 880 { 881 struct resource *r, *r_assigned = NULL; 882 883 pci_bus_for_each_resource(bus, r) { 884 if (r == &ioport_resource || r == &iomem_resource) 885 continue; 886 if (r && (r->flags & type_mask) == type && !r->parent) 887 return r; 888 if (r && (r->flags & type_mask) == type && !r_assigned) 889 r_assigned = r; 890 } 891 return r_assigned; 892 } 893 894 static resource_size_t calculate_iosize(resource_size_t size, 895 resource_size_t min_size, 896 resource_size_t size1, 897 resource_size_t add_size, 898 resource_size_t children_add_size, 899 resource_size_t old_size, 900 resource_size_t align) 901 { 902 if (size < min_size) 903 size = min_size; 904 if (old_size == 1) 905 old_size = 0; 906 /* 907 * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the 908 * struct pci_bus. 909 */ 910 #if defined(CONFIG_ISA) || defined(CONFIG_EISA) 911 size = (size & 0xff) + ((size & ~0xffUL) << 2); 912 #endif 913 size = size + size1; 914 915 size = max(size, add_size) + children_add_size; 916 return ALIGN(max(size, old_size), align); 917 } 918 919 static resource_size_t calculate_memsize(resource_size_t size, 920 resource_size_t min_size, 921 resource_size_t add_size, 922 resource_size_t children_add_size, 923 resource_size_t old_size, 924 resource_size_t align) 925 { 926 if (size < min_size) 927 size = min_size; 928 if (old_size == 1) 929 old_size = 0; 930 931 size = max(size, add_size) + children_add_size; 932 return ALIGN(max(size, old_size), align); 933 } 934 935 resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, 936 unsigned long type) 937 { 938 return 1; 939 } 940 941 #define PCI_P2P_DEFAULT_MEM_ALIGN SZ_1M 942 #define PCI_P2P_DEFAULT_IO_ALIGN SZ_4K 943 #define PCI_P2P_DEFAULT_IO_ALIGN_1K SZ_1K 944 945 static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type) 946 { 947 resource_size_t align = 1, arch_align; 948 949 if (type & IORESOURCE_MEM) 950 align = PCI_P2P_DEFAULT_MEM_ALIGN; 951 else if (type & IORESOURCE_IO) { 952 /* 953 * Per spec, I/O windows are 4K-aligned, but some bridges have 954 * an extension to support 1K alignment. 955 */ 956 if (bus->self && bus->self->io_window_1k) 957 align = PCI_P2P_DEFAULT_IO_ALIGN_1K; 958 else 959 align = PCI_P2P_DEFAULT_IO_ALIGN; 960 } 961 962 arch_align = pcibios_window_alignment(bus, type); 963 return max(align, arch_align); 964 } 965 966 /** 967 * pbus_size_io() - Size the I/O window of a given bus 968 * 969 * @bus: The bus 970 * @min_size: The minimum I/O window that must be allocated 971 * @add_size: Additional optional I/O window 972 * @realloc_head: Track the additional I/O window on this list 973 * 974 * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these 975 * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI 976 * devices are limited to 256 bytes. We must be careful with the ISA 977 * aliasing though. 978 */ 979 static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 980 resource_size_t add_size, 981 struct list_head *realloc_head) 982 { 983 struct pci_dev *dev; 984 struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO, 985 IORESOURCE_IO); 986 resource_size_t size = 0, size0 = 0, size1 = 0; 987 resource_size_t children_add_size = 0; 988 resource_size_t min_align, align; 989 990 if (!b_res) 991 return; 992 993 /* If resource is already assigned, nothing more to do */ 994 if (b_res->parent) 995 return; 996 997 min_align = window_alignment(bus, IORESOURCE_IO); 998 list_for_each_entry(dev, &bus->devices, bus_list) { 999 struct resource *r; 1000 1001 pci_dev_for_each_resource(dev, r) { 1002 unsigned long r_size; 1003 1004 if (r->parent || !(r->flags & IORESOURCE_IO)) 1005 continue; 1006 r_size = resource_size(r); 1007 1008 if (r_size < SZ_1K) 1009 /* Might be re-aligned for ISA */ 1010 size += r_size; 1011 else 1012 size1 += r_size; 1013 1014 align = pci_resource_alignment(dev, r); 1015 if (align > min_align) 1016 min_align = align; 1017 1018 if (realloc_head) 1019 children_add_size += get_res_add_size(realloc_head, r); 1020 } 1021 } 1022 1023 size0 = calculate_iosize(size, min_size, size1, 0, 0, 1024 resource_size(b_res), min_align); 1025 1026 size1 = size0; 1027 if (realloc_head && (add_size > 0 || children_add_size > 0)) { 1028 size1 = calculate_iosize(size, min_size, size1, add_size, 1029 children_add_size, resource_size(b_res), 1030 min_align); 1031 } 1032 1033 if (!size0 && !size1) { 1034 if (bus->self && (b_res->start || b_res->end)) 1035 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n", 1036 b_res, &bus->busn_res); 1037 b_res->flags = 0; 1038 return; 1039 } 1040 1041 resource_set_range(b_res, min_align, size0); 1042 b_res->flags |= IORESOURCE_STARTALIGN; 1043 if (bus->self && size1 > size0 && realloc_head) { 1044 add_to_list(realloc_head, bus->self, b_res, size1-size0, 1045 min_align); 1046 pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n", 1047 b_res, &bus->busn_res, 1048 (unsigned long long) size1 - size0); 1049 } 1050 } 1051 1052 static inline resource_size_t calculate_mem_align(resource_size_t *aligns, 1053 int max_order) 1054 { 1055 resource_size_t align = 0; 1056 resource_size_t min_align = 0; 1057 int order; 1058 1059 for (order = 0; order <= max_order; order++) { 1060 resource_size_t align1 = 1; 1061 1062 align1 <<= order + __ffs(SZ_1M); 1063 1064 if (!align) 1065 min_align = align1; 1066 else if (ALIGN(align + min_align, min_align) < align1) 1067 min_align = align1 >> 1; 1068 align += aligns[order]; 1069 } 1070 1071 return min_align; 1072 } 1073 1074 /** 1075 * pbus_upstream_space_available - Check no upstream resource limits allocation 1076 * @bus: The bus 1077 * @mask: Mask the resource flag, then compare it with type 1078 * @type: The type of resource from bridge 1079 * @size: The size required from the bridge window 1080 * @align: Required alignment for the resource 1081 * 1082 * Checks that @size can fit inside the upstream bridge resources that are 1083 * already assigned. 1084 * 1085 * Return: %true if enough space is available on all assigned upstream 1086 * resources. 1087 */ 1088 static bool pbus_upstream_space_available(struct pci_bus *bus, unsigned long mask, 1089 unsigned long type, resource_size_t size, 1090 resource_size_t align) 1091 { 1092 struct resource_constraint constraint = { 1093 .max = RESOURCE_SIZE_MAX, 1094 .align = align, 1095 }; 1096 struct pci_bus *downstream = bus; 1097 struct resource *r; 1098 1099 while ((bus = bus->parent)) { 1100 if (pci_is_root_bus(bus)) 1101 break; 1102 1103 pci_bus_for_each_resource(bus, r) { 1104 if (!r || !r->parent || (r->flags & mask) != type) 1105 continue; 1106 1107 if (resource_size(r) >= size) { 1108 struct resource gap = {}; 1109 1110 if (find_resource_space(r, &gap, size, &constraint) == 0) { 1111 gap.flags = type; 1112 pci_dbg(bus->self, 1113 "Assigned bridge window %pR to %pR free space at %pR\n", 1114 r, &bus->busn_res, &gap); 1115 return true; 1116 } 1117 } 1118 1119 if (bus->self) { 1120 pci_info(bus->self, 1121 "Assigned bridge window %pR to %pR cannot fit 0x%llx required for %s bridging to %pR\n", 1122 r, &bus->busn_res, 1123 (unsigned long long)size, 1124 pci_name(downstream->self), 1125 &downstream->busn_res); 1126 } 1127 1128 return false; 1129 } 1130 } 1131 1132 return true; 1133 } 1134 1135 /** 1136 * pbus_size_mem() - Size the memory window of a given bus 1137 * 1138 * @bus: The bus 1139 * @mask: Mask the resource flag, then compare it with type 1140 * @type: The type of free resource from bridge 1141 * @type2: Second match type 1142 * @type3: Third match type 1143 * @min_size: The minimum memory window that must be allocated 1144 * @add_size: Additional optional memory window 1145 * @realloc_head: Track the additional memory window on this list 1146 * 1147 * Calculate the size of the bus and minimal alignment which guarantees 1148 * that all child resources fit in this size. 1149 * 1150 * Return -ENOSPC if there's no available bus resource of the desired 1151 * type. Otherwise, set the bus resource start/end to indicate the 1152 * required size, add things to realloc_head (if supplied), and return 0. 1153 */ 1154 static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 1155 unsigned long type, unsigned long type2, 1156 unsigned long type3, resource_size_t min_size, 1157 resource_size_t add_size, 1158 struct list_head *realloc_head) 1159 { 1160 struct pci_dev *dev; 1161 resource_size_t min_align, win_align, align, size, size0, size1 = 0; 1162 resource_size_t aligns[28]; /* Alignments from 1MB to 128TB */ 1163 int order, max_order; 1164 struct resource *b_res = find_bus_resource_of_type(bus, 1165 mask | IORESOURCE_PREFETCH, type); 1166 resource_size_t children_add_size = 0; 1167 resource_size_t children_add_align = 0; 1168 resource_size_t add_align = 0; 1169 1170 if (!b_res) 1171 return -ENOSPC; 1172 1173 /* If resource is already assigned, nothing more to do */ 1174 if (b_res->parent) 1175 return 0; 1176 1177 memset(aligns, 0, sizeof(aligns)); 1178 max_order = 0; 1179 size = 0; 1180 1181 list_for_each_entry(dev, &bus->devices, bus_list) { 1182 struct resource *r; 1183 int i; 1184 1185 pci_dev_for_each_resource(dev, r, i) { 1186 const char *r_name = pci_resource_name(dev, i); 1187 resource_size_t r_size; 1188 1189 if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) || 1190 ((r->flags & mask) != type && 1191 (r->flags & mask) != type2 && 1192 (r->flags & mask) != type3)) 1193 continue; 1194 r_size = resource_size(r); 1195 1196 /* Put SRIOV requested res to the optional list */ 1197 if (realloc_head && pci_resource_is_optional(dev, i)) { 1198 add_align = max(pci_resource_alignment(dev, r), add_align); 1199 add_to_list(realloc_head, dev, r, 0, 0 /* Don't care */); 1200 children_add_size += r_size; 1201 continue; 1202 } 1203 1204 /* 1205 * aligns[0] is for 1MB (since bridge memory 1206 * windows are always at least 1MB aligned), so 1207 * keep "order" from being negative for smaller 1208 * resources. 1209 */ 1210 align = pci_resource_alignment(dev, r); 1211 order = __ffs(align) - __ffs(SZ_1M); 1212 if (order < 0) 1213 order = 0; 1214 if (order >= ARRAY_SIZE(aligns)) { 1215 pci_warn(dev, "%s %pR: disabling; bad alignment %#llx\n", 1216 r_name, r, (unsigned long long) align); 1217 r->flags = 0; 1218 continue; 1219 } 1220 size += max(r_size, align); 1221 /* 1222 * Exclude ranges with size > align from calculation of 1223 * the alignment. 1224 */ 1225 if (r_size <= align) 1226 aligns[order] += align; 1227 if (order > max_order) 1228 max_order = order; 1229 1230 if (realloc_head) { 1231 children_add_size += get_res_add_size(realloc_head, r); 1232 children_add_align = get_res_add_align(realloc_head, r); 1233 add_align = max(add_align, children_add_align); 1234 } 1235 } 1236 } 1237 1238 win_align = window_alignment(bus, b_res->flags); 1239 min_align = calculate_mem_align(aligns, max_order); 1240 min_align = max(min_align, win_align); 1241 size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align); 1242 1243 if (bus->self && size0 && 1244 !pbus_upstream_space_available(bus, mask | IORESOURCE_PREFETCH, type, 1245 size0, min_align)) { 1246 min_align = 1ULL << (max_order + __ffs(SZ_1M)); 1247 min_align = max(min_align, win_align); 1248 size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), win_align); 1249 pci_info(bus->self, "bridge window %pR to %pR requires relaxed alignment rules\n", 1250 b_res, &bus->busn_res); 1251 } 1252 1253 if (realloc_head && (add_size > 0 || children_add_size > 0)) { 1254 add_align = max(min_align, add_align); 1255 size1 = calculate_memsize(size, min_size, add_size, children_add_size, 1256 resource_size(b_res), add_align); 1257 1258 if (bus->self && size1 && 1259 !pbus_upstream_space_available(bus, mask | IORESOURCE_PREFETCH, type, 1260 size1, add_align)) { 1261 min_align = 1ULL << (max_order + __ffs(SZ_1M)); 1262 min_align = max(min_align, win_align); 1263 size1 = calculate_memsize(size, min_size, add_size, children_add_size, 1264 resource_size(b_res), win_align); 1265 pci_info(bus->self, 1266 "bridge window %pR to %pR requires relaxed alignment rules\n", 1267 b_res, &bus->busn_res); 1268 } 1269 } 1270 1271 if (!size0 && !size1) { 1272 if (bus->self && (b_res->start || b_res->end)) 1273 pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n", 1274 b_res, &bus->busn_res); 1275 b_res->flags = 0; 1276 return 0; 1277 } 1278 1279 resource_set_range(b_res, min_align, size0); 1280 b_res->flags |= IORESOURCE_STARTALIGN; 1281 if (bus->self && size1 > size0 && realloc_head) { 1282 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align); 1283 pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n", 1284 b_res, &bus->busn_res, 1285 (unsigned long long) (size1 - size0), 1286 (unsigned long long) add_align); 1287 } 1288 return 0; 1289 } 1290 1291 unsigned long pci_cardbus_resource_alignment(struct resource *res) 1292 { 1293 if (res->flags & IORESOURCE_IO) 1294 return pci_cardbus_io_size; 1295 if (res->flags & IORESOURCE_MEM) 1296 return pci_cardbus_mem_size; 1297 return 0; 1298 } 1299 1300 static void pci_bus_size_cardbus(struct pci_bus *bus, 1301 struct list_head *realloc_head) 1302 { 1303 struct pci_dev *bridge = bus->self; 1304 struct resource *b_res; 1305 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 1306 u16 ctrl; 1307 1308 b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW]; 1309 if (b_res->parent) 1310 goto handle_b_res_1; 1311 /* 1312 * Reserve some resources for CardBus. We reserve a fixed amount 1313 * of bus space for CardBus bridges. 1314 */ 1315 resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size); 1316 b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 1317 if (realloc_head) { 1318 b_res->end -= pci_cardbus_io_size; 1319 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 1320 pci_cardbus_io_size); 1321 } 1322 1323 handle_b_res_1: 1324 b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW]; 1325 if (b_res->parent) 1326 goto handle_b_res_2; 1327 resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size); 1328 b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 1329 if (realloc_head) { 1330 b_res->end -= pci_cardbus_io_size; 1331 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 1332 pci_cardbus_io_size); 1333 } 1334 1335 handle_b_res_2: 1336 /* MEM1 must not be pref MMIO */ 1337 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1338 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 1339 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 1340 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1341 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1342 } 1343 1344 /* Check whether prefetchable memory is supported by this bridge. */ 1345 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1346 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 1347 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 1348 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 1349 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 1350 } 1351 1352 b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW]; 1353 if (b_res->parent) 1354 goto handle_b_res_3; 1355 /* 1356 * If we have prefetchable memory support, allocate two regions. 1357 * Otherwise, allocate one region of twice the size. 1358 */ 1359 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 1360 resource_set_range(b_res, pci_cardbus_mem_size, 1361 pci_cardbus_mem_size); 1362 b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 1363 IORESOURCE_STARTALIGN; 1364 if (realloc_head) { 1365 b_res->end -= pci_cardbus_mem_size; 1366 add_to_list(realloc_head, bridge, b_res, 1367 pci_cardbus_mem_size, pci_cardbus_mem_size); 1368 } 1369 1370 /* Reduce that to half */ 1371 b_res_3_size = pci_cardbus_mem_size; 1372 } 1373 1374 handle_b_res_3: 1375 b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW]; 1376 if (b_res->parent) 1377 goto handle_done; 1378 resource_set_range(b_res, pci_cardbus_mem_size, b_res_3_size); 1379 b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 1380 if (realloc_head) { 1381 b_res->end -= b_res_3_size; 1382 add_to_list(realloc_head, bridge, b_res, b_res_3_size, 1383 pci_cardbus_mem_size); 1384 } 1385 1386 handle_done: 1387 ; 1388 } 1389 1390 void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) 1391 { 1392 struct pci_dev *dev; 1393 unsigned long mask, prefmask, type2 = 0, type3 = 0; 1394 resource_size_t additional_io_size = 0, additional_mmio_size = 0, 1395 additional_mmio_pref_size = 0; 1396 struct resource *pref; 1397 struct pci_host_bridge *host; 1398 int hdr_type, ret; 1399 1400 list_for_each_entry(dev, &bus->devices, bus_list) { 1401 struct pci_bus *b = dev->subordinate; 1402 if (!b) 1403 continue; 1404 1405 switch (dev->hdr_type) { 1406 case PCI_HEADER_TYPE_CARDBUS: 1407 pci_bus_size_cardbus(b, realloc_head); 1408 break; 1409 1410 case PCI_HEADER_TYPE_BRIDGE: 1411 default: 1412 __pci_bus_size_bridges(b, realloc_head); 1413 break; 1414 } 1415 } 1416 1417 /* The root bus? */ 1418 if (pci_is_root_bus(bus)) { 1419 host = to_pci_host_bridge(bus->bridge); 1420 if (!host->size_windows) 1421 return; 1422 pci_bus_for_each_resource(bus, pref) 1423 if (pref && (pref->flags & IORESOURCE_PREFETCH)) 1424 break; 1425 hdr_type = -1; /* Intentionally invalid - not a PCI device. */ 1426 } else { 1427 pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 1428 hdr_type = bus->self->hdr_type; 1429 } 1430 1431 switch (hdr_type) { 1432 case PCI_HEADER_TYPE_CARDBUS: 1433 /* Don't size CardBuses yet */ 1434 break; 1435 1436 case PCI_HEADER_TYPE_BRIDGE: 1437 pci_bridge_check_ranges(bus); 1438 if (bus->self->is_hotplug_bridge) { 1439 additional_io_size = pci_hotplug_io_size; 1440 additional_mmio_size = pci_hotplug_mmio_size; 1441 additional_mmio_pref_size = pci_hotplug_mmio_pref_size; 1442 } 1443 fallthrough; 1444 default: 1445 pbus_size_io(bus, realloc_head ? 0 : additional_io_size, 1446 additional_io_size, realloc_head); 1447 1448 /* 1449 * If there's a 64-bit prefetchable MMIO window, compute 1450 * the size required to put all 64-bit prefetchable 1451 * resources in it. 1452 */ 1453 mask = IORESOURCE_MEM; 1454 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 1455 if (pref && (pref->flags & IORESOURCE_MEM_64)) { 1456 prefmask |= IORESOURCE_MEM_64; 1457 ret = pbus_size_mem(bus, prefmask, prefmask, 1458 prefmask, prefmask, 1459 realloc_head ? 0 : additional_mmio_pref_size, 1460 additional_mmio_pref_size, realloc_head); 1461 1462 /* 1463 * If successful, all non-prefetchable resources 1464 * and any 32-bit prefetchable resources will go in 1465 * the non-prefetchable window. 1466 */ 1467 if (ret == 0) { 1468 mask = prefmask; 1469 type2 = prefmask & ~IORESOURCE_MEM_64; 1470 type3 = prefmask & ~IORESOURCE_PREFETCH; 1471 } 1472 } 1473 1474 /* 1475 * If there is no 64-bit prefetchable window, compute the 1476 * size required to put all prefetchable resources in the 1477 * 32-bit prefetchable window (if there is one). 1478 */ 1479 if (!type2) { 1480 prefmask &= ~IORESOURCE_MEM_64; 1481 ret = pbus_size_mem(bus, prefmask, prefmask, 1482 prefmask, prefmask, 1483 realloc_head ? 0 : additional_mmio_pref_size, 1484 additional_mmio_pref_size, realloc_head); 1485 1486 /* 1487 * If successful, only non-prefetchable resources 1488 * will go in the non-prefetchable window. 1489 */ 1490 if (ret == 0) 1491 mask = prefmask; 1492 else 1493 additional_mmio_size += additional_mmio_pref_size; 1494 1495 type2 = type3 = IORESOURCE_MEM; 1496 } 1497 1498 /* 1499 * Compute the size required to put everything else in the 1500 * non-prefetchable window. This includes: 1501 * 1502 * - all non-prefetchable resources 1503 * - 32-bit prefetchable resources if there's a 64-bit 1504 * prefetchable window or no prefetchable window at all 1505 * - 64-bit prefetchable resources if there's no prefetchable 1506 * window at all 1507 * 1508 * Note that the strategy in __pci_assign_resource() must match 1509 * that used here. Specifically, we cannot put a 32-bit 1510 * prefetchable resource in a 64-bit prefetchable window. 1511 */ 1512 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, 1513 realloc_head ? 0 : additional_mmio_size, 1514 additional_mmio_size, realloc_head); 1515 break; 1516 } 1517 } 1518 1519 void pci_bus_size_bridges(struct pci_bus *bus) 1520 { 1521 __pci_bus_size_bridges(bus, NULL); 1522 } 1523 EXPORT_SYMBOL(pci_bus_size_bridges); 1524 1525 static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) 1526 { 1527 struct resource *parent_r; 1528 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM | 1529 IORESOURCE_PREFETCH; 1530 1531 pci_bus_for_each_resource(b, parent_r) { 1532 if (!parent_r) 1533 continue; 1534 1535 if ((r->flags & mask) == (parent_r->flags & mask) && 1536 resource_contains(parent_r, r)) 1537 request_resource(parent_r, r); 1538 } 1539 } 1540 1541 /* 1542 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are 1543 * skipped by pbus_assign_resources_sorted(). 1544 */ 1545 static void pdev_assign_fixed_resources(struct pci_dev *dev) 1546 { 1547 struct resource *r; 1548 1549 pci_dev_for_each_resource(dev, r) { 1550 struct pci_bus *b; 1551 1552 if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) || 1553 !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) 1554 continue; 1555 1556 b = dev->bus; 1557 while (b && !r->parent) { 1558 assign_fixed_resource_on_bus(b, r); 1559 b = b->parent; 1560 } 1561 } 1562 } 1563 1564 void __pci_bus_assign_resources(const struct pci_bus *bus, 1565 struct list_head *realloc_head, 1566 struct list_head *fail_head) 1567 { 1568 struct pci_bus *b; 1569 struct pci_dev *dev; 1570 1571 pbus_assign_resources_sorted(bus, realloc_head, fail_head); 1572 1573 list_for_each_entry(dev, &bus->devices, bus_list) { 1574 pdev_assign_fixed_resources(dev); 1575 1576 b = dev->subordinate; 1577 if (!b) 1578 continue; 1579 1580 __pci_bus_assign_resources(b, realloc_head, fail_head); 1581 1582 switch (dev->hdr_type) { 1583 case PCI_HEADER_TYPE_BRIDGE: 1584 if (!pci_is_enabled(dev)) 1585 pci_setup_bridge(b); 1586 break; 1587 1588 case PCI_HEADER_TYPE_CARDBUS: 1589 pci_setup_cardbus(b); 1590 break; 1591 1592 default: 1593 pci_info(dev, "not setting up bridge for bus %04x:%02x\n", 1594 pci_domain_nr(b), b->number); 1595 break; 1596 } 1597 } 1598 } 1599 1600 void pci_bus_assign_resources(const struct pci_bus *bus) 1601 { 1602 __pci_bus_assign_resources(bus, NULL, NULL); 1603 } 1604 EXPORT_SYMBOL(pci_bus_assign_resources); 1605 1606 static void pci_claim_device_resources(struct pci_dev *dev) 1607 { 1608 int i; 1609 1610 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) { 1611 struct resource *r = &dev->resource[i]; 1612 1613 if (!r->flags || r->parent) 1614 continue; 1615 1616 pci_claim_resource(dev, i); 1617 } 1618 } 1619 1620 static void pci_claim_bridge_resources(struct pci_dev *dev) 1621 { 1622 int i; 1623 1624 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) { 1625 struct resource *r = &dev->resource[i]; 1626 1627 if (!r->flags || r->parent) 1628 continue; 1629 1630 pci_claim_bridge_resource(dev, i); 1631 } 1632 } 1633 1634 static void pci_bus_allocate_dev_resources(struct pci_bus *b) 1635 { 1636 struct pci_dev *dev; 1637 struct pci_bus *child; 1638 1639 list_for_each_entry(dev, &b->devices, bus_list) { 1640 pci_claim_device_resources(dev); 1641 1642 child = dev->subordinate; 1643 if (child) 1644 pci_bus_allocate_dev_resources(child); 1645 } 1646 } 1647 1648 static void pci_bus_allocate_resources(struct pci_bus *b) 1649 { 1650 struct pci_bus *child; 1651 1652 /* 1653 * Carry out a depth-first search on the PCI bus tree to allocate 1654 * bridge apertures. Read the programmed bridge bases and 1655 * recursively claim the respective bridge resources. 1656 */ 1657 if (b->self) { 1658 pci_read_bridge_bases(b); 1659 pci_claim_bridge_resources(b->self); 1660 } 1661 1662 list_for_each_entry(child, &b->children, node) 1663 pci_bus_allocate_resources(child); 1664 } 1665 1666 void pci_bus_claim_resources(struct pci_bus *b) 1667 { 1668 pci_bus_allocate_resources(b); 1669 pci_bus_allocate_dev_resources(b); 1670 } 1671 EXPORT_SYMBOL(pci_bus_claim_resources); 1672 1673 static void __pci_bridge_assign_resources(const struct pci_dev *bridge, 1674 struct list_head *add_head, 1675 struct list_head *fail_head) 1676 { 1677 struct pci_bus *b; 1678 1679 pdev_assign_resources_sorted((struct pci_dev *)bridge, 1680 add_head, fail_head); 1681 1682 b = bridge->subordinate; 1683 if (!b) 1684 return; 1685 1686 __pci_bus_assign_resources(b, add_head, fail_head); 1687 1688 switch (bridge->class >> 8) { 1689 case PCI_CLASS_BRIDGE_PCI: 1690 pci_setup_bridge(b); 1691 break; 1692 1693 case PCI_CLASS_BRIDGE_CARDBUS: 1694 pci_setup_cardbus(b); 1695 break; 1696 1697 default: 1698 pci_info(bridge, "not setting up bridge for bus %04x:%02x\n", 1699 pci_domain_nr(b), b->number); 1700 break; 1701 } 1702 } 1703 1704 #define PCI_RES_TYPE_MASK \ 1705 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\ 1706 IORESOURCE_MEM_64) 1707 1708 static void pci_bridge_release_resources(struct pci_bus *bus, 1709 unsigned long type) 1710 { 1711 struct pci_dev *dev = bus->self; 1712 struct resource *r; 1713 unsigned int old_flags; 1714 struct resource *b_res; 1715 int idx = 1; 1716 1717 b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; 1718 1719 /* 1720 * 1. If IO port assignment fails, release bridge IO port. 1721 * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO. 1722 * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit, 1723 * release bridge pref MMIO. 1724 * 4. If pref MMIO assignment fails, and bridge pref is 32bit, 1725 * release bridge pref MMIO. 1726 * 5. If pref MMIO assignment fails, and bridge pref is not 1727 * assigned, release bridge nonpref MMIO. 1728 */ 1729 if (type & IORESOURCE_IO) 1730 idx = 0; 1731 else if (!(type & IORESOURCE_PREFETCH)) 1732 idx = 1; 1733 else if ((type & IORESOURCE_MEM_64) && 1734 (b_res[2].flags & IORESOURCE_MEM_64)) 1735 idx = 2; 1736 else if (!(b_res[2].flags & IORESOURCE_MEM_64) && 1737 (b_res[2].flags & IORESOURCE_PREFETCH)) 1738 idx = 2; 1739 else 1740 idx = 1; 1741 1742 r = &b_res[idx]; 1743 1744 if (!r->parent) 1745 return; 1746 1747 /* If there are children, release them all */ 1748 release_child_resources(r); 1749 if (!release_resource(r)) { 1750 type = old_flags = r->flags & PCI_RES_TYPE_MASK; 1751 pci_info(dev, "resource %d %pR released\n", 1752 PCI_BRIDGE_RESOURCES + idx, r); 1753 /* Keep the old size */ 1754 resource_set_range(r, 0, resource_size(r)); 1755 r->flags = 0; 1756 1757 /* Avoiding touch the one without PREF */ 1758 if (type & IORESOURCE_PREFETCH) 1759 type = IORESOURCE_PREFETCH; 1760 __pci_setup_bridge(bus, type); 1761 /* For next child res under same bridge */ 1762 r->flags = old_flags; 1763 } 1764 } 1765 1766 enum release_type { 1767 leaf_only, 1768 whole_subtree, 1769 }; 1770 1771 /* 1772 * Try to release PCI bridge resources from leaf bridge, so we can allocate 1773 * a larger window later. 1774 */ 1775 static void pci_bus_release_bridge_resources(struct pci_bus *bus, 1776 unsigned long type, 1777 enum release_type rel_type) 1778 { 1779 struct pci_dev *dev; 1780 bool is_leaf_bridge = true; 1781 1782 list_for_each_entry(dev, &bus->devices, bus_list) { 1783 struct pci_bus *b = dev->subordinate; 1784 if (!b) 1785 continue; 1786 1787 is_leaf_bridge = false; 1788 1789 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) 1790 continue; 1791 1792 if (rel_type == whole_subtree) 1793 pci_bus_release_bridge_resources(b, type, 1794 whole_subtree); 1795 } 1796 1797 if (pci_is_root_bus(bus)) 1798 return; 1799 1800 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) 1801 return; 1802 1803 if ((rel_type == whole_subtree) || is_leaf_bridge) 1804 pci_bridge_release_resources(bus, type); 1805 } 1806 1807 static void pci_bus_dump_res(struct pci_bus *bus) 1808 { 1809 struct resource *res; 1810 int i; 1811 1812 pci_bus_for_each_resource(bus, res, i) { 1813 if (!res || !res->end || !res->flags) 1814 continue; 1815 1816 dev_info(&bus->dev, "resource %d %pR\n", i, res); 1817 } 1818 } 1819 1820 static void pci_bus_dump_resources(struct pci_bus *bus) 1821 { 1822 struct pci_bus *b; 1823 struct pci_dev *dev; 1824 1825 1826 pci_bus_dump_res(bus); 1827 1828 list_for_each_entry(dev, &bus->devices, bus_list) { 1829 b = dev->subordinate; 1830 if (!b) 1831 continue; 1832 1833 pci_bus_dump_resources(b); 1834 } 1835 } 1836 1837 static int pci_bus_get_depth(struct pci_bus *bus) 1838 { 1839 int depth = 0; 1840 struct pci_bus *child_bus; 1841 1842 list_for_each_entry(child_bus, &bus->children, node) { 1843 int ret; 1844 1845 ret = pci_bus_get_depth(child_bus); 1846 if (ret + 1 > depth) 1847 depth = ret + 1; 1848 } 1849 1850 return depth; 1851 } 1852 1853 /* 1854 * -1: undefined, will auto detect later 1855 * 0: disabled by user 1856 * 1: disabled by auto detect 1857 * 2: enabled by user 1858 * 3: enabled by auto detect 1859 */ 1860 enum enable_type { 1861 undefined = -1, 1862 user_disabled, 1863 auto_disabled, 1864 user_enabled, 1865 auto_enabled, 1866 }; 1867 1868 static enum enable_type pci_realloc_enable = undefined; 1869 void __init pci_realloc_get_opt(char *str) 1870 { 1871 if (!strncmp(str, "off", 3)) 1872 pci_realloc_enable = user_disabled; 1873 else if (!strncmp(str, "on", 2)) 1874 pci_realloc_enable = user_enabled; 1875 } 1876 static bool pci_realloc_enabled(enum enable_type enable) 1877 { 1878 return enable >= user_enabled; 1879 } 1880 1881 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) 1882 static int iov_resources_unassigned(struct pci_dev *dev, void *data) 1883 { 1884 int i; 1885 bool *unassigned = data; 1886 1887 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 1888 struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES]; 1889 struct pci_bus_region region; 1890 1891 /* Not assigned or rejected by kernel? */ 1892 if (!r->flags) 1893 continue; 1894 1895 pcibios_resource_to_bus(dev->bus, ®ion, r); 1896 if (!region.start) { 1897 *unassigned = true; 1898 return 1; /* Return early from pci_walk_bus() */ 1899 } 1900 } 1901 1902 return 0; 1903 } 1904 1905 static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1906 enum enable_type enable_local) 1907 { 1908 bool unassigned = false; 1909 struct pci_host_bridge *host; 1910 1911 if (enable_local != undefined) 1912 return enable_local; 1913 1914 host = pci_find_host_bridge(bus); 1915 if (host->preserve_config) 1916 return auto_disabled; 1917 1918 pci_walk_bus(bus, iov_resources_unassigned, &unassigned); 1919 if (unassigned) 1920 return auto_enabled; 1921 1922 return enable_local; 1923 } 1924 #else 1925 static enum enable_type pci_realloc_detect(struct pci_bus *bus, 1926 enum enable_type enable_local) 1927 { 1928 return enable_local; 1929 } 1930 #endif 1931 1932 static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res, 1933 struct list_head *add_list, 1934 resource_size_t new_size) 1935 { 1936 resource_size_t add_size, size = resource_size(res); 1937 1938 if (res->parent) 1939 return; 1940 1941 if (!new_size) 1942 return; 1943 1944 if (new_size > size) { 1945 add_size = new_size - size; 1946 pci_dbg(bridge, "bridge window %pR extended by %pa\n", res, 1947 &add_size); 1948 } else if (new_size < size) { 1949 add_size = size - new_size; 1950 pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res, 1951 &add_size); 1952 } else { 1953 return; 1954 } 1955 1956 resource_set_size(res, new_size); 1957 1958 /* If the resource is part of the add_list, remove it now */ 1959 if (add_list) 1960 remove_from_list(add_list, res); 1961 } 1962 1963 static void remove_dev_resource(struct resource *avail, struct pci_dev *dev, 1964 struct resource *res) 1965 { 1966 resource_size_t size, align, tmp; 1967 1968 size = resource_size(res); 1969 if (!size) 1970 return; 1971 1972 align = pci_resource_alignment(dev, res); 1973 align = align ? ALIGN(avail->start, align) - avail->start : 0; 1974 tmp = align + size; 1975 avail->start = min(avail->start + tmp, avail->end + 1); 1976 } 1977 1978 static void remove_dev_resources(struct pci_dev *dev, struct resource *io, 1979 struct resource *mmio, 1980 struct resource *mmio_pref) 1981 { 1982 struct resource *res; 1983 1984 pci_dev_for_each_resource(dev, res) { 1985 if (resource_type(res) == IORESOURCE_IO) { 1986 remove_dev_resource(io, dev, res); 1987 } else if (resource_type(res) == IORESOURCE_MEM) { 1988 1989 /* 1990 * Make sure prefetchable memory is reduced from 1991 * the correct resource. Specifically we put 32-bit 1992 * prefetchable memory in non-prefetchable window 1993 * if there is a 64-bit prefetchable window. 1994 * 1995 * See comments in __pci_bus_size_bridges() for 1996 * more information. 1997 */ 1998 if ((res->flags & IORESOURCE_PREFETCH) && 1999 ((res->flags & IORESOURCE_MEM_64) == 2000 (mmio_pref->flags & IORESOURCE_MEM_64))) 2001 remove_dev_resource(mmio_pref, dev, res); 2002 else 2003 remove_dev_resource(mmio, dev, res); 2004 } 2005 } 2006 } 2007 2008 #define ALIGN_DOWN_IF_NONZERO(addr, align) \ 2009 ((align) ? ALIGN_DOWN((addr), (align)) : (addr)) 2010 2011 /* 2012 * io, mmio and mmio_pref contain the total amount of bridge window space 2013 * available. This includes the minimal space needed to cover all the 2014 * existing devices on the bus and the possible extra space that can be 2015 * shared with the bridges. 2016 */ 2017 static void pci_bus_distribute_available_resources(struct pci_bus *bus, 2018 struct list_head *add_list, 2019 struct resource io, 2020 struct resource mmio, 2021 struct resource mmio_pref) 2022 { 2023 unsigned int normal_bridges = 0, hotplug_bridges = 0; 2024 struct resource *io_res, *mmio_res, *mmio_pref_res; 2025 struct pci_dev *dev, *bridge = bus->self; 2026 resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align; 2027 2028 io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW]; 2029 mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW]; 2030 mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 2031 2032 /* 2033 * The alignment of this bridge is yet to be considered, hence it must 2034 * be done now before extending its bridge window. 2035 */ 2036 align = pci_resource_alignment(bridge, io_res); 2037 if (!io_res->parent && align) 2038 io.start = min(ALIGN(io.start, align), io.end + 1); 2039 2040 align = pci_resource_alignment(bridge, mmio_res); 2041 if (!mmio_res->parent && align) 2042 mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1); 2043 2044 align = pci_resource_alignment(bridge, mmio_pref_res); 2045 if (!mmio_pref_res->parent && align) 2046 mmio_pref.start = min(ALIGN(mmio_pref.start, align), 2047 mmio_pref.end + 1); 2048 2049 /* 2050 * Now that we have adjusted for alignment, update the bridge window 2051 * resources to fill as much remaining resource space as possible. 2052 */ 2053 adjust_bridge_window(bridge, io_res, add_list, resource_size(&io)); 2054 adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio)); 2055 adjust_bridge_window(bridge, mmio_pref_res, add_list, 2056 resource_size(&mmio_pref)); 2057 2058 /* 2059 * Calculate how many hotplug bridges and normal bridges there 2060 * are on this bus. We will distribute the additional available 2061 * resources between hotplug bridges. 2062 */ 2063 for_each_pci_bridge(dev, bus) { 2064 if (dev->is_hotplug_bridge) 2065 hotplug_bridges++; 2066 else 2067 normal_bridges++; 2068 } 2069 2070 if (!(hotplug_bridges + normal_bridges)) 2071 return; 2072 2073 /* 2074 * Calculate the amount of space we can forward from "bus" to any 2075 * downstream buses, i.e., the space left over after assigning the 2076 * BARs and windows on "bus". 2077 */ 2078 list_for_each_entry(dev, &bus->devices, bus_list) { 2079 if (!dev->is_virtfn) 2080 remove_dev_resources(dev, &io, &mmio, &mmio_pref); 2081 } 2082 2083 /* 2084 * If there is at least one hotplug bridge on this bus it gets all 2085 * the extra resource space that was left after the reductions 2086 * above. 2087 * 2088 * If there are no hotplug bridges the extra resource space is 2089 * split between non-hotplug bridges. This is to allow possible 2090 * hotplug bridges below them to get the extra space as well. 2091 */ 2092 if (hotplug_bridges) { 2093 io_per_b = div64_ul(resource_size(&io), hotplug_bridges); 2094 mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges); 2095 mmio_pref_per_b = div64_ul(resource_size(&mmio_pref), 2096 hotplug_bridges); 2097 } else { 2098 io_per_b = div64_ul(resource_size(&io), normal_bridges); 2099 mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges); 2100 mmio_pref_per_b = div64_ul(resource_size(&mmio_pref), 2101 normal_bridges); 2102 } 2103 2104 for_each_pci_bridge(dev, bus) { 2105 struct resource *res; 2106 struct pci_bus *b; 2107 2108 b = dev->subordinate; 2109 if (!b) 2110 continue; 2111 if (hotplug_bridges && !dev->is_hotplug_bridge) 2112 continue; 2113 2114 res = &dev->resource[PCI_BRIDGE_IO_WINDOW]; 2115 2116 /* 2117 * Make sure the split resource space is properly aligned 2118 * for bridge windows (align it down to avoid going above 2119 * what is available). 2120 */ 2121 align = pci_resource_alignment(dev, res); 2122 resource_set_size(&io, ALIGN_DOWN_IF_NONZERO(io_per_b, align)); 2123 2124 /* 2125 * The x_per_b holds the extra resource space that can be 2126 * added for each bridge but there is the minimal already 2127 * reserved as well so adjust x.start down accordingly to 2128 * cover the whole space. 2129 */ 2130 io.start -= resource_size(res); 2131 2132 res = &dev->resource[PCI_BRIDGE_MEM_WINDOW]; 2133 align = pci_resource_alignment(dev, res); 2134 resource_set_size(&mmio, 2135 ALIGN_DOWN_IF_NONZERO(mmio_per_b,align)); 2136 mmio.start -= resource_size(res); 2137 2138 res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 2139 align = pci_resource_alignment(dev, res); 2140 resource_set_size(&mmio_pref, 2141 ALIGN_DOWN_IF_NONZERO(mmio_pref_per_b, align)); 2142 mmio_pref.start -= resource_size(res); 2143 2144 pci_bus_distribute_available_resources(b, add_list, io, mmio, 2145 mmio_pref); 2146 2147 io.start += io.end + 1; 2148 mmio.start += mmio.end + 1; 2149 mmio_pref.start += mmio_pref.end + 1; 2150 } 2151 } 2152 2153 static void pci_bridge_distribute_available_resources(struct pci_dev *bridge, 2154 struct list_head *add_list) 2155 { 2156 struct resource available_io, available_mmio, available_mmio_pref; 2157 2158 if (!bridge->is_hotplug_bridge) 2159 return; 2160 2161 pci_dbg(bridge, "distributing available resources\n"); 2162 2163 /* Take the initial extra resources from the hotplug port */ 2164 available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW]; 2165 available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW]; 2166 available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 2167 2168 pci_bus_distribute_available_resources(bridge->subordinate, 2169 add_list, available_io, 2170 available_mmio, 2171 available_mmio_pref); 2172 } 2173 2174 static bool pci_bridge_resources_not_assigned(struct pci_dev *dev) 2175 { 2176 const struct resource *r; 2177 2178 /* 2179 * If the child device's resources are not yet assigned it means we 2180 * are configuring them (not the boot firmware), so we should be 2181 * able to extend the upstream bridge resources in the same way we 2182 * do with the normal hotplug case. 2183 */ 2184 r = &dev->resource[PCI_BRIDGE_IO_WINDOW]; 2185 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN)) 2186 return false; 2187 r = &dev->resource[PCI_BRIDGE_MEM_WINDOW]; 2188 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN)) 2189 return false; 2190 r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; 2191 if (r->flags && !(r->flags & IORESOURCE_STARTALIGN)) 2192 return false; 2193 2194 return true; 2195 } 2196 2197 static void 2198 pci_root_bus_distribute_available_resources(struct pci_bus *bus, 2199 struct list_head *add_list) 2200 { 2201 struct pci_dev *dev, *bridge = bus->self; 2202 2203 for_each_pci_bridge(dev, bus) { 2204 struct pci_bus *b; 2205 2206 b = dev->subordinate; 2207 if (!b) 2208 continue; 2209 2210 /* 2211 * Need to check "bridge" here too because it is NULL 2212 * in case of root bus. 2213 */ 2214 if (bridge && pci_bridge_resources_not_assigned(dev)) 2215 pci_bridge_distribute_available_resources(dev, add_list); 2216 else 2217 pci_root_bus_distribute_available_resources(b, add_list); 2218 } 2219 } 2220 2221 static void pci_prepare_next_assign_round(struct list_head *fail_head, 2222 int tried_times, 2223 enum release_type rel_type) 2224 { 2225 struct pci_dev_resource *fail_res; 2226 2227 pr_info("PCI: No. %d try to assign unassigned res\n", tried_times + 1); 2228 2229 /* 2230 * Try to release leaf bridge's resources that aren't big 2231 * enough to contain child device resources. 2232 */ 2233 list_for_each_entry(fail_res, fail_head, list) { 2234 pci_bus_release_bridge_resources(fail_res->dev->bus, 2235 fail_res->flags & PCI_RES_TYPE_MASK, 2236 rel_type); 2237 } 2238 2239 /* Restore size and flags */ 2240 list_for_each_entry(fail_res, fail_head, list) { 2241 struct resource *res = fail_res->res; 2242 struct pci_dev *dev = fail_res->dev; 2243 int idx = pci_resource_num(dev, res); 2244 2245 restore_dev_resource(fail_res); 2246 2247 if (!pci_is_bridge(dev)) 2248 continue; 2249 2250 if (idx >= PCI_BRIDGE_RESOURCES && 2251 idx <= PCI_BRIDGE_RESOURCE_END) 2252 res->flags = 0; 2253 } 2254 2255 free_list(fail_head); 2256 } 2257 2258 /* 2259 * First try will not touch PCI bridge res. 2260 * Second and later try will clear small leaf bridge res. 2261 * Will stop till to the max depth if can not find good one. 2262 */ 2263 void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) 2264 { 2265 LIST_HEAD(realloc_head); 2266 /* List of resources that want additional resources */ 2267 struct list_head *add_list = NULL; 2268 int tried_times = 0; 2269 enum release_type rel_type = leaf_only; 2270 LIST_HEAD(fail_head); 2271 int pci_try_num = 1; 2272 enum enable_type enable_local; 2273 2274 /* Don't realloc if asked to do so */ 2275 enable_local = pci_realloc_detect(bus, pci_realloc_enable); 2276 if (pci_realloc_enabled(enable_local)) { 2277 int max_depth = pci_bus_get_depth(bus); 2278 2279 pci_try_num = max_depth + 1; 2280 dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n", 2281 max_depth, pci_try_num); 2282 } 2283 2284 while (1) { 2285 /* 2286 * Last try will use add_list, otherwise will try good to 2287 * have as must have, so can realloc parent bridge resource 2288 */ 2289 if (tried_times + 1 == pci_try_num) 2290 add_list = &realloc_head; 2291 /* 2292 * Depth first, calculate sizes and alignments of all 2293 * subordinate buses. 2294 */ 2295 __pci_bus_size_bridges(bus, add_list); 2296 2297 pci_root_bus_distribute_available_resources(bus, add_list); 2298 2299 /* Depth last, allocate resources and update the hardware. */ 2300 __pci_bus_assign_resources(bus, add_list, &fail_head); 2301 if (add_list) 2302 BUG_ON(!list_empty(add_list)); 2303 tried_times++; 2304 2305 /* Any device complain? */ 2306 if (list_empty(&fail_head)) 2307 break; 2308 2309 if (tried_times >= pci_try_num) { 2310 if (enable_local == undefined) { 2311 dev_info(&bus->dev, 2312 "Some PCI device resources are unassigned, try booting with pci=realloc\n"); 2313 } else if (enable_local == auto_enabled) { 2314 dev_info(&bus->dev, 2315 "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); 2316 } 2317 free_list(&fail_head); 2318 break; 2319 } 2320 2321 /* Third times and later will not check if it is leaf */ 2322 if (tried_times + 1 > 2) 2323 rel_type = whole_subtree; 2324 2325 pci_prepare_next_assign_round(&fail_head, tried_times, rel_type); 2326 } 2327 2328 pci_bus_dump_resources(bus); 2329 } 2330 2331 void pci_assign_unassigned_resources(void) 2332 { 2333 struct pci_bus *root_bus; 2334 2335 list_for_each_entry(root_bus, &pci_root_buses, node) { 2336 pci_assign_unassigned_root_bus_resources(root_bus); 2337 2338 /* Make sure the root bridge has a companion ACPI device */ 2339 if (ACPI_HANDLE(root_bus->bridge)) 2340 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge)); 2341 } 2342 } 2343 2344 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 2345 { 2346 struct pci_bus *parent = bridge->subordinate; 2347 /* List of resources that want additional resources */ 2348 LIST_HEAD(add_list); 2349 int tried_times = 0; 2350 LIST_HEAD(fail_head); 2351 int ret; 2352 2353 while (1) { 2354 __pci_bus_size_bridges(parent, &add_list); 2355 2356 /* 2357 * Distribute remaining resources (if any) equally between 2358 * hotplug bridges below. This makes it possible to extend 2359 * the hierarchy later without running out of resources. 2360 */ 2361 pci_bridge_distribute_available_resources(bridge, &add_list); 2362 2363 __pci_bridge_assign_resources(bridge, &add_list, &fail_head); 2364 BUG_ON(!list_empty(&add_list)); 2365 tried_times++; 2366 2367 if (list_empty(&fail_head)) 2368 break; 2369 2370 if (tried_times >= 2) { 2371 /* Still fail, don't need to try more */ 2372 free_list(&fail_head); 2373 break; 2374 } 2375 2376 pci_prepare_next_assign_round(&fail_head, tried_times, 2377 whole_subtree); 2378 } 2379 2380 ret = pci_reenable_device(bridge); 2381 if (ret) 2382 pci_err(bridge, "Error reenabling bridge (%d)\n", ret); 2383 pci_set_master(bridge); 2384 } 2385 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 2386 2387 int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type) 2388 { 2389 struct pci_dev_resource *dev_res; 2390 struct pci_dev *next; 2391 LIST_HEAD(saved); 2392 LIST_HEAD(added); 2393 LIST_HEAD(failed); 2394 unsigned int i; 2395 int ret; 2396 2397 down_read(&pci_bus_sem); 2398 2399 /* Walk to the root hub, releasing bridge BARs when possible */ 2400 next = bridge; 2401 do { 2402 bridge = next; 2403 for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END; 2404 i++) { 2405 struct resource *res = &bridge->resource[i]; 2406 const char *res_name = pci_resource_name(bridge, i); 2407 2408 if ((res->flags ^ type) & PCI_RES_TYPE_MASK) 2409 continue; 2410 2411 /* Ignore BARs which are still in use */ 2412 if (res->child) 2413 continue; 2414 2415 ret = add_to_list(&saved, bridge, res, 0, 0); 2416 if (ret) 2417 goto cleanup; 2418 2419 pci_info(bridge, "%s %pR: releasing\n", res_name, res); 2420 2421 if (res->parent) 2422 release_resource(res); 2423 res->start = 0; 2424 res->end = 0; 2425 break; 2426 } 2427 if (i == PCI_BRIDGE_RESOURCE_END) 2428 break; 2429 2430 next = bridge->bus ? bridge->bus->self : NULL; 2431 } while (next); 2432 2433 if (list_empty(&saved)) { 2434 up_read(&pci_bus_sem); 2435 return -ENOENT; 2436 } 2437 2438 __pci_bus_size_bridges(bridge->subordinate, &added); 2439 __pci_bridge_assign_resources(bridge, &added, &failed); 2440 BUG_ON(!list_empty(&added)); 2441 2442 if (!list_empty(&failed)) { 2443 ret = -ENOSPC; 2444 goto cleanup; 2445 } 2446 2447 list_for_each_entry(dev_res, &saved, list) { 2448 /* Skip the bridge we just assigned resources for */ 2449 if (bridge == dev_res->dev) 2450 continue; 2451 2452 bridge = dev_res->dev; 2453 pci_setup_bridge(bridge->subordinate); 2454 } 2455 2456 free_list(&saved); 2457 up_read(&pci_bus_sem); 2458 return 0; 2459 2460 cleanup: 2461 /* Restore size and flags */ 2462 list_for_each_entry(dev_res, &failed, list) 2463 restore_dev_resource(dev_res); 2464 free_list(&failed); 2465 2466 /* Revert to the old configuration */ 2467 list_for_each_entry(dev_res, &saved, list) { 2468 struct resource *res = dev_res->res; 2469 2470 bridge = dev_res->dev; 2471 i = pci_resource_num(bridge, res); 2472 2473 restore_dev_resource(dev_res); 2474 2475 pci_claim_resource(bridge, i); 2476 pci_setup_bridge(bridge->subordinate); 2477 } 2478 free_list(&saved); 2479 up_read(&pci_bus_sem); 2480 2481 return ret; 2482 } 2483 2484 void pci_assign_unassigned_bus_resources(struct pci_bus *bus) 2485 { 2486 struct pci_dev *dev; 2487 /* List of resources that want additional resources */ 2488 LIST_HEAD(add_list); 2489 2490 down_read(&pci_bus_sem); 2491 for_each_pci_bridge(dev, bus) 2492 if (pci_has_subordinate(dev)) 2493 __pci_bus_size_bridges(dev->subordinate, &add_list); 2494 up_read(&pci_bus_sem); 2495 __pci_bus_assign_resources(bus, &add_list, NULL); 2496 BUG_ON(!list_empty(&add_list)); 2497 } 2498 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources); 2499