21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/errno.h>
27 #include <linux/slab.h>
71 pr_warning(
"add_to_list: kmalloc() failed!\n");
83 list_add(&tmp->
list, head);
88 static void remove_from_list(
struct list_head *head,
94 if (dev_res->
res == res) {
108 if (dev_res->
res == res) {
109 int idx = res - &dev_res->
dev->resource[0];
112 "res[%d]=%pR get_res_add_size add_size %llx\n",
114 (
unsigned long long)dev_res->
add_size);
124 static void pdev_sort_resources(
struct pci_dev *dev,
struct list_head *head)
142 r_align = pci_resource_alignment(dev, r);
144 dev_warn(&dev->
dev,
"BAR %d: %pR has bogus alignment\n",
151 panic(
"pdev_sort_resources(): "
152 "kmalloc() failed!\n");
161 align = pci_resource_alignment(dev_res->
dev,
164 if (r_align > align) {
174 static void __dev_sort_resources(
struct pci_dev *dev,
191 pdev_sort_resources(dev, head);
194 static inline void reset_resource(
struct resource *res)
213 static void reassign_resources_sorted(
struct list_head *realloc_head,
223 bool found_match =
false;
232 if (dev_res->
res == res) {
240 idx = res - &add_res->
dev->resource[0];
242 if (!resource_size(res)) {
244 res->
end = res->
start + add_size - 1;
254 "failed to add %llx res[%d]=%pR\n",
274 static void assign_requested_resources_sorted(
struct list_head *head,
283 idx = res - &dev_res->
dev->resource[0];
284 if (resource_size(res) &&
286 if (fail_head && !pci_is_root_bus(dev_res->
dev->bus)) {
293 add_to_list(fail_head,
303 static void __assign_resources_sorted(
struct list_head *head,
322 if (!realloc_head || list_empty(realloc_head))
323 goto requested_and_reassign;
327 if (add_to_list(&save_head, dev_res->
dev, dev_res->
res, 0, 0)) {
328 free_list(&save_head);
329 goto requested_and_reassign;
335 dev_res->res->
end += get_res_add_size(realloc_head,
339 assign_requested_resources_sorted(head, &local_fail_head);
342 if (list_empty(&local_fail_head)) {
345 remove_from_list(realloc_head, dev_res->res);
346 free_list(&save_head);
351 free_list(&local_fail_head);
354 if (dev_res->res->parent)
358 struct resource *res = save_res->res;
360 res->
start = save_res->start;
361 res->
end = save_res->end;
362 res->
flags = save_res->flags;
364 free_list(&save_head);
366 requested_and_reassign:
368 assign_requested_resources_sorted(head, fail_head);
373 reassign_resources_sorted(realloc_head, head);
377 static void pdev_assign_resources_sorted(
struct pci_dev *dev,
383 __dev_sort_resources(dev, &head);
384 __assign_resources_sorted(&head, add_head, fail_head);
388 static void pbus_assign_resources_sorted(
const struct pci_bus *
bus,
396 __dev_sort_resources(dev, &head);
398 __assign_resources_sorted(&head, realloc_head, fail_head);
410 res = bus->resource[0];
417 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
424 res = bus->resource[1];
427 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
434 res = bus->resource[2];
437 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
444 res = bus->resource[3];
447 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
467 static void pci_setup_bridge_io(
struct pci_bus *bus)
472 unsigned long io_mask;
473 u8 io_base_lo, io_limit_lo;
486 io_base_lo = (
region.start >> 8) & io_mask;
487 io_limit_lo = (
region.end >> 8) & io_mask;
488 l |= ((
u32) io_limit_lo << 8) | io_base_lo;
490 io_upper16 = (
region.end & 0xffff0000) | (
region.start >> 16);
491 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
505 static void pci_setup_bridge_mmio(
struct pci_bus *bus)
516 l = (
region.start >> 16) & 0xfff0;
517 l |=
region.end & 0xfff00000;
518 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
525 static void pci_setup_bridge_mmio_pref(
struct pci_bus *bus)
542 l = (
region.start >> 16) & 0xfff0;
543 l |=
region.end & 0xfff00000;
548 dev_info(&bridge->
dev,
" bridge window %pR\n", res);
559 static void __pci_setup_bridge(
struct pci_bus *bus,
unsigned long type)
567 pci_setup_bridge_io(bus);
570 pci_setup_bridge_mmio(bus);
573 pci_setup_bridge_mmio_pref(bus);
580 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
583 __pci_setup_bridge(bus, type);
589 static void pci_bridge_check_ranges(
struct pci_bus *bus)
601 pci_write_config_word(bridge,
PCI_IO_BASE, 0xf0f0);
637 b_res[2].
flags &= ~IORESOURCE_MEM_64;
647 static struct resource *find_free_bus_resource(
struct pci_bus *bus,
unsigned long type)
651 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
654 pci_bus_for_each_resource(bus, r, i) {
657 if (r && (r->
flags & type_mask) == type && !r->
parent)
675 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
676 size = (size & 0xff) + ((size & ~0xffUL) << 2);
678 size =
ALIGN(size + size1, align);
696 size =
ALIGN(size + size1, align);
706 #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000
707 #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000
708 #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400
715 if (type & IORESOURCE_MEM)
717 else if (type & IORESOURCE_IO) {
722 if (bus->
self->io_window_1k)
729 return max(align, arch_align);
749 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
750 unsigned long size = 0, size0 = 0, size1 = 0;
757 io_align = min_align = window_alignment(bus, IORESOURCE_IO);
763 unsigned long r_size;
767 r_size = resource_size(r);
775 align = pci_resource_alignment(dev, r);
776 if (align > min_align)
780 children_add_size += get_res_add_size(realloc_head, r);
784 if (min_align > io_align)
787 size0 = calculate_iosize(size, min_size, size1,
788 resource_size(b_res), min_align);
789 if (children_add_size > add_size)
790 add_size = children_add_size;
791 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
792 calculate_iosize(size, min_size, add_size + size1,
793 resource_size(b_res), min_align);
794 if (!size0 && !size1) {
797 "%pR to %pR (unused)\n", b_res,
803 b_res->
start = min_align;
804 b_res->
end = b_res->
start + size0 - 1;
806 if (size1 > size0 && realloc_head) {
807 add_to_list(realloc_head, bus->
self, b_res, size1-size0,
810 "%pR to %pR add_size %lx\n", b_res,
822 for (order = 0; order <= max_order; order++) {
825 align1 <<= (order + 20);
829 else if (
ALIGN(align + min_align, min_align) < align1)
830 min_align = align1 >> 1;
831 align += aligns[
order];
848 static int pbus_size_mem(
struct pci_bus *bus,
unsigned long mask,
856 int order, max_order;
857 struct resource *b_res = find_free_bus_resource(bus, type);
858 unsigned int mem64_mask = 0;
864 memset(aligns, 0,
sizeof(aligns));
869 b_res->
flags &= ~IORESOURCE_MEM_64;
880 r_size = resource_size(r);
881 #ifdef CONFIG_PCI_IOV
883 if (realloc_head && i >= PCI_IOV_RESOURCES &&
884 i <= PCI_IOV_RESOURCE_END) {
886 add_to_list(realloc_head, dev, r, r_size, 0);
887 children_add_size += r_size;
892 align = pci_resource_alignment(dev, r);
893 order =
__ffs(align) - 20;
896 "(bad alignment %#llx)\n", i, r,
897 (
unsigned long long) align);
908 if (order > max_order)
913 children_add_size += get_res_add_size(realloc_head, r);
917 min_align = calculate_mem_align(aligns, max_order);
918 min_align =
max(min_align, window_alignment(bus, b_res->
flags & mask));
919 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
920 if (children_add_size > add_size)
921 add_size = children_add_size;
922 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
923 calculate_memsize(size, min_size, add_size,
924 resource_size(b_res), min_align);
925 if (!size0 && !size1) {
928 "%pR to %pR (unused)\n", b_res,
933 b_res->
start = min_align;
934 b_res->
end = size0 + min_align - 1;
936 if (size1 > size0 && realloc_head) {
937 add_to_list(realloc_head, bus->
self, b_res, size1-size0, min_align);
939 "%pR to %pR add_size %llx\n", b_res,
940 &bus->
busn_res, (
unsigned long long)size1-size0);
947 if (res->
flags & IORESOURCE_IO)
949 if (res->
flags & IORESOURCE_MEM)
954 static void pci_bus_size_cardbus(
struct pci_bus *bus,
993 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1009 if (b_res[2].parent)
1010 goto handle_b_res_3;
1016 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
1023 add_to_list(realloc_head, bridge, b_res+2,
1032 if (b_res[3].parent)
1035 b_res[3].
end = b_res[3].
start + b_res_3_size - 1;
1038 b_res[3].
end -= b_res_3_size;
1039 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
1051 unsigned long mask, prefmask;
1059 switch (dev->
class >> 8) {
1061 pci_bus_size_cardbus(b, realloc_head);
1075 switch (bus->
self->class >> 8) {
1081 pci_bridge_check_ranges(bus);
1082 if (bus->
self->is_hotplug_bridge) {
1090 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1091 additional_io_size, realloc_head);
1099 if (pbus_size_mem(bus, prefmask, prefmask,
1100 realloc_head ? 0 : additional_mem_size,
1101 additional_mem_size, realloc_head))
1104 additional_mem_size += additional_mem_size;
1105 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1106 realloc_head ? 0 : additional_mem_size,
1107 additional_mem_size, realloc_head);
1118 static void __ref __pci_bus_assign_resources(
const struct pci_bus *bus,
1125 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
1132 __pci_bus_assign_resources(b, realloc_head, fail_head);
1134 switch (dev->
class >> 8) {
1136 if (!pci_is_enabled(dev))
1145 dev_info(&dev->
dev,
"not setting up bridge for bus "
1154 __pci_bus_assign_resources(bus,
NULL,
NULL);
1158 static void __ref __pci_bridge_assign_resources(
const struct pci_dev *bridge,
1164 pdev_assign_resources_sorted((
struct pci_dev *)bridge,
1165 add_head, fail_head);
1171 __pci_bus_assign_resources(b, add_head, fail_head);
1173 switch (bridge->
class >> 8) {
1183 dev_info(&bridge->
dev,
"not setting up bridge for bus "
1188 static void pci_bridge_release_resources(
struct pci_bus *bus,
1195 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1202 if ((r->
flags & type_mask) != type)
1213 "resource %d %pR released\n", idx, r);
1215 r->
end = resource_size(r) - 1;
1226 __pci_setup_bridge(bus, type);
1238 static void __ref pci_bus_release_bridge_resources(
struct pci_bus *bus,
1243 bool is_leaf_bridge =
true;
1250 is_leaf_bridge =
false;
1256 pci_bus_release_bridge_resources(b, type,
1260 if (pci_is_root_bus(bus))
1267 pci_bridge_release_resources(bus, type);
1270 static void pci_bus_dump_res(
struct pci_bus *bus)
1275 pci_bus_for_each_resource(bus, res, i) {
1276 if (!res || !res->
end || !res->
flags)
1279 dev_printk(
KERN_DEBUG, &bus->
dev,
"resource %d %pR\n", i, res);
1283 static void pci_bus_dump_resources(
struct pci_bus *bus)
1289 pci_bus_dump_res(bus);
1296 pci_bus_dump_resources(b);
1311 ret = pci_bus_get_depth(b);
1312 if (ret + 1 > depth)
1318 static int __init pci_get_max_depth(
void)
1326 ret = pci_bus_get_depth(bus);
1354 else if (!
strncmp(str,
"on", 2))
1357 static bool __init pci_realloc_enabled(
void)
1362 static void __init pci_realloc_detect(
void)
1364 #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1373 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1399 int tried_times = 0;
1403 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1405 int pci_try_num = 1;
1408 pci_realloc_detect();
1409 if (pci_realloc_enabled()) {
1410 int max_depth = pci_get_max_depth();
1412 pci_try_num = max_depth + 1;
1414 max_depth, pci_try_num);
1422 if (tried_times + 1 == pci_try_num)
1423 add_list = &realloc_head;
1431 __pci_bus_assign_resources(bus, add_list, &fail_head);
1433 BUG_ON(!list_empty(add_list));
1437 if (list_empty(&fail_head))
1438 goto enable_and_dump;
1440 if (tried_times >= pci_try_num) {
1442 printk(
KERN_INFO "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1444 printk(
KERN_INFO "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1446 free_list(&fail_head);
1447 goto enable_and_dump;
1454 if ((tried_times + 1) > 2)
1462 bus = fail_res->
dev->bus;
1463 pci_bus_release_bridge_resources(bus,
1464 fail_res->
flags & type_mask,
1472 res->
end = fail_res->
end;
1474 if (fail_res->
dev->subordinate)
1477 free_list(&fail_head);
1488 pci_bus_dump_resources(bus);
1496 int tried_times = 0;
1500 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1505 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1506 BUG_ON(!list_empty(&add_list));
1509 if (list_empty(&fail_head))
1512 if (tried_times >= 2) {
1514 free_list(&fail_head);
1529 pci_bus_release_bridge_resources(bus, flags & type_mask,
1537 res->
end = fail_res->
end;
1539 if (fail_res->
dev->subordinate)
1542 free_list(&fail_head);
1553 #ifdef CONFIG_HOTPLUG
1580 __pci_bus_assign_resources(bus, &add_list,
NULL);
1581 BUG_ON(!list_empty(&add_list));