Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/pci.h> |
Yinghai Lu | 9ad3f2c | 2010-02-10 01:20:11 -0800 | [diff] [blame] | 3 | #include <linux/range.h> |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 4 | |
| 5 | #include "bus_numa.h" |
| 6 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 7 | LIST_HEAD(pci_root_infos); |
| 8 | |
| 9 | static struct pci_root_info *x86_find_pci_root_info(int bus) |
| 10 | { |
| 11 | struct pci_root_info *info; |
| 12 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 13 | list_for_each_entry(info, &pci_root_infos, list) |
Yinghai Lu | a10bb12 | 2012-05-17 18:51:12 -0700 | [diff] [blame] | 14 | if (info->busn.start == bus) |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 15 | return info; |
| 16 | |
| 17 | return NULL; |
| 18 | } |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 19 | |
Bjorn Helgaas | afcf21c2 | 2014-01-24 11:54:36 -0700 | [diff] [blame] | 20 | int x86_pci_root_bus_node(int bus) |
| 21 | { |
| 22 | struct pci_root_info *info = x86_find_pci_root_info(bus); |
| 23 | |
| 24 | if (!info) |
| 25 | return NUMA_NO_NODE; |
| 26 | |
| 27 | return info->node; |
| 28 | } |
| 29 | |
Bjorn Helgaas | 2cd6975 | 2011-10-28 16:28:14 -0600 | [diff] [blame] | 30 | void x86_pci_root_bus_resources(int bus, struct list_head *resources) |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 31 | { |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 32 | struct pci_root_info *info = x86_find_pci_root_info(bus); |
| 33 | struct pci_root_res *root_res; |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 34 | struct resource_entry *window; |
Yinghai Lu | a10bb12 | 2012-05-17 18:51:12 -0700 | [diff] [blame] | 35 | bool found = false; |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 36 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 37 | if (!info) |
Bjorn Helgaas | 2cd6975 | 2011-10-28 16:28:14 -0600 | [diff] [blame] | 38 | goto default_resources; |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 39 | |
Bjorn Helgaas | 2cd6975 | 2011-10-28 16:28:14 -0600 | [diff] [blame] | 40 | printk(KERN_DEBUG "PCI: root bus %02x: hardware-probed resources\n", |
| 41 | bus); |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 42 | |
Yinghai Lu | a10bb12 | 2012-05-17 18:51:12 -0700 | [diff] [blame] | 43 | /* already added by acpi ? */ |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 44 | resource_list_for_each_entry(window, resources) |
Yinghai Lu | a10bb12 | 2012-05-17 18:51:12 -0700 | [diff] [blame] | 45 | if (window->res->flags & IORESOURCE_BUS) { |
| 46 | found = true; |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | if (!found) |
| 51 | pci_add_resource(resources, &info->busn); |
| 52 | |
Liu Jiang | 727ae8b | 2015-11-27 11:12:33 +0800 | [diff] [blame] | 53 | list_for_each_entry(root_res, &info->resources, list) |
| 54 | pci_add_resource(resources, &root_res->res); |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 55 | |
Bjorn Helgaas | 2cd6975 | 2011-10-28 16:28:14 -0600 | [diff] [blame] | 56 | return; |
| 57 | |
| 58 | default_resources: |
| 59 | /* |
| 60 | * We don't have any host bridge aperture information from the |
| 61 | * "native host bridge drivers," e.g., amd_bus or broadcom_bus, |
| 62 | * so fall back to the defaults historically used by pci_create_bus(). |
| 63 | */ |
| 64 | printk(KERN_DEBUG "PCI: root bus %02x: using default resources\n", bus); |
| 65 | pci_add_resource(resources, &ioport_resource); |
| 66 | pci_add_resource(resources, &iomem_resource); |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 69 | struct pci_root_info __init *alloc_pci_root_info(int bus_min, int bus_max, |
| 70 | int node, int link) |
| 71 | { |
| 72 | struct pci_root_info *info; |
| 73 | |
| 74 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 75 | |
| 76 | if (!info) |
| 77 | return info; |
| 78 | |
Yinghai Lu | a10bb12 | 2012-05-17 18:51:12 -0700 | [diff] [blame] | 79 | sprintf(info->name, "PCI Bus #%02x", bus_min); |
| 80 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 81 | INIT_LIST_HEAD(&info->resources); |
Yinghai Lu | a10bb12 | 2012-05-17 18:51:12 -0700 | [diff] [blame] | 82 | info->busn.name = info->name; |
| 83 | info->busn.start = bus_min; |
| 84 | info->busn.end = bus_max; |
| 85 | info->busn.flags = IORESOURCE_BUS; |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 86 | info->node = node; |
| 87 | info->link = link; |
| 88 | |
| 89 | list_add_tail(&info->list, &pci_root_infos); |
| 90 | |
| 91 | return info; |
| 92 | } |
| 93 | |
Greg Kroah-Hartman | a18e369 | 2012-12-21 14:02:53 -0800 | [diff] [blame] | 94 | void update_res(struct pci_root_info *info, resource_size_t start, |
| 95 | resource_size_t end, unsigned long flags, int merge) |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 96 | { |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 97 | struct resource *res; |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 98 | struct pci_root_res *root_res; |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 99 | |
| 100 | if (start > end) |
| 101 | return; |
| 102 | |
Yinghai Lu | 9ad3f2c | 2010-02-10 01:20:11 -0800 | [diff] [blame] | 103 | if (start == MAX_RESOURCE) |
| 104 | return; |
| 105 | |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 106 | if (!merge) |
| 107 | goto addit; |
| 108 | |
| 109 | /* try to merge it with old one */ |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 110 | list_for_each_entry(root_res, &info->resources, list) { |
Yinghai Lu | b74fd23 | 2010-02-10 01:20:08 -0800 | [diff] [blame] | 111 | resource_size_t final_start, final_end; |
| 112 | resource_size_t common_start, common_end; |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 113 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 114 | res = &root_res->res; |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 115 | if (res->flags != flags) |
| 116 | continue; |
| 117 | |
Yinghai Lu | b74fd23 | 2010-02-10 01:20:08 -0800 | [diff] [blame] | 118 | common_start = max(res->start, start); |
| 119 | common_end = min(res->end, end); |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 120 | if (common_start > common_end + 1) |
| 121 | continue; |
| 122 | |
Yinghai Lu | b74fd23 | 2010-02-10 01:20:08 -0800 | [diff] [blame] | 123 | final_start = min(res->start, start); |
| 124 | final_end = max(res->end, end); |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 125 | |
| 126 | res->start = final_start; |
| 127 | res->end = final_end; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | addit: |
| 132 | |
| 133 | /* need to add that */ |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 134 | root_res = kzalloc(sizeof(*root_res), GFP_KERNEL); |
| 135 | if (!root_res) |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 136 | return; |
| 137 | |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 138 | res = &root_res->res; |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 139 | res->name = info->name; |
| 140 | res->flags = flags; |
| 141 | res->start = start; |
| 142 | res->end = end; |
Yinghai Lu | d28e5ac | 2012-04-02 18:31:54 -0700 | [diff] [blame] | 143 | |
| 144 | list_add_tail(&root_res->list, &info->resources); |
Yinghai Lu | 67f241f | 2009-11-11 22:27:40 -0800 | [diff] [blame] | 145 | } |