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