blob: 7bcf06a7cd12069e9e4f5c2f13066e1551ba8a27 [file] [log] [blame]
Yinghai Lu67f241f2009-11-11 22:27:40 -08001#include <linux/init.h>
2#include <linux/pci.h>
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -08003#include <linux/range.h>
Yinghai Lu67f241f2009-11-11 22:27:40 -08004
5#include "bus_numa.h"
6
Yinghai Lud28e5ac2012-04-02 18:31:54 -07007LIST_HEAD(pci_root_infos);
8
9static struct pci_root_info *x86_find_pci_root_info(int bus)
10{
11 struct pci_root_info *info;
12
Yinghai Lud28e5ac2012-04-02 18:31:54 -070013 list_for_each_entry(info, &pci_root_infos, list)
Yinghai Lua10bb122012-05-17 18:51:12 -070014 if (info->busn.start == bus)
Yinghai Lud28e5ac2012-04-02 18:31:54 -070015 return info;
16
17 return NULL;
18}
Yinghai Lu67f241f2009-11-11 22:27:40 -080019
Bjorn Helgaasafcf21c22014-01-24 11:54:36 -070020int 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 Helgaas2cd69752011-10-28 16:28:14 -060030void x86_pci_root_bus_resources(int bus, struct list_head *resources)
Yinghai Lu67f241f2009-11-11 22:27:40 -080031{
Yinghai Lud28e5ac2012-04-02 18:31:54 -070032 struct pci_root_info *info = x86_find_pci_root_info(bus);
33 struct pci_root_res *root_res;
Jiang Liu14d76b62015-02-05 13:44:44 +080034 struct resource_entry *window;
Yinghai Lua10bb122012-05-17 18:51:12 -070035 bool found = false;
Yinghai Lu67f241f2009-11-11 22:27:40 -080036
Yinghai Lud28e5ac2012-04-02 18:31:54 -070037 if (!info)
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060038 goto default_resources;
Yinghai Lu67f241f2009-11-11 22:27:40 -080039
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060040 printk(KERN_DEBUG "PCI: root bus %02x: hardware-probed resources\n",
41 bus);
Yinghai Lu67f241f2009-11-11 22:27:40 -080042
Yinghai Lua10bb122012-05-17 18:51:12 -070043 /* already added by acpi ? */
Jiang Liu14d76b62015-02-05 13:44:44 +080044 resource_list_for_each_entry(window, resources)
Yinghai Lua10bb122012-05-17 18:51:12 -070045 if (window->res->flags & IORESOURCE_BUS) {
46 found = true;
47 break;
48 }
49
50 if (!found)
51 pci_add_resource(resources, &info->busn);
52
Yinghai Lud28e5ac2012-04-02 18:31:54 -070053 list_for_each_entry(root_res, &info->resources, list) {
Yinghai Lu67f241f2009-11-11 22:27:40 -080054 struct resource *res;
55 struct resource *root;
56
Yinghai Lud28e5ac2012-04-02 18:31:54 -070057 res = &root_res->res;
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060058 pci_add_resource(resources, res);
Yinghai Lu67f241f2009-11-11 22:27:40 -080059 if (res->flags & IORESOURCE_IO)
60 root = &ioport_resource;
61 else
62 root = &iomem_resource;
63 insert_resource(root, res);
64 }
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060065 return;
66
67default_resources:
68 /*
69 * We don't have any host bridge aperture information from the
70 * "native host bridge drivers," e.g., amd_bus or broadcom_bus,
71 * so fall back to the defaults historically used by pci_create_bus().
72 */
73 printk(KERN_DEBUG "PCI: root bus %02x: using default resources\n", bus);
74 pci_add_resource(resources, &ioport_resource);
75 pci_add_resource(resources, &iomem_resource);
Yinghai Lu67f241f2009-11-11 22:27:40 -080076}
77
Yinghai Lud28e5ac2012-04-02 18:31:54 -070078struct pci_root_info __init *alloc_pci_root_info(int bus_min, int bus_max,
79 int node, int link)
80{
81 struct pci_root_info *info;
82
83 info = kzalloc(sizeof(*info), GFP_KERNEL);
84
85 if (!info)
86 return info;
87
Yinghai Lua10bb122012-05-17 18:51:12 -070088 sprintf(info->name, "PCI Bus #%02x", bus_min);
89
Yinghai Lud28e5ac2012-04-02 18:31:54 -070090 INIT_LIST_HEAD(&info->resources);
Yinghai Lua10bb122012-05-17 18:51:12 -070091 info->busn.name = info->name;
92 info->busn.start = bus_min;
93 info->busn.end = bus_max;
94 info->busn.flags = IORESOURCE_BUS;
Yinghai Lud28e5ac2012-04-02 18:31:54 -070095 info->node = node;
96 info->link = link;
97
98 list_add_tail(&info->list, &pci_root_infos);
99
100 return info;
101}
102
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800103void update_res(struct pci_root_info *info, resource_size_t start,
104 resource_size_t end, unsigned long flags, int merge)
Yinghai Lu67f241f2009-11-11 22:27:40 -0800105{
Yinghai Lu67f241f2009-11-11 22:27:40 -0800106 struct resource *res;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700107 struct pci_root_res *root_res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800108
109 if (start > end)
110 return;
111
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800112 if (start == MAX_RESOURCE)
113 return;
114
Yinghai Lu67f241f2009-11-11 22:27:40 -0800115 if (!merge)
116 goto addit;
117
118 /* try to merge it with old one */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700119 list_for_each_entry(root_res, &info->resources, list) {
Yinghai Lub74fd232010-02-10 01:20:08 -0800120 resource_size_t final_start, final_end;
121 resource_size_t common_start, common_end;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800122
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700123 res = &root_res->res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800124 if (res->flags != flags)
125 continue;
126
Yinghai Lub74fd232010-02-10 01:20:08 -0800127 common_start = max(res->start, start);
128 common_end = min(res->end, end);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800129 if (common_start > common_end + 1)
130 continue;
131
Yinghai Lub74fd232010-02-10 01:20:08 -0800132 final_start = min(res->start, start);
133 final_end = max(res->end, end);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800134
135 res->start = final_start;
136 res->end = final_end;
137 return;
138 }
139
140addit:
141
142 /* need to add that */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700143 root_res = kzalloc(sizeof(*root_res), GFP_KERNEL);
144 if (!root_res)
Yinghai Lu67f241f2009-11-11 22:27:40 -0800145 return;
146
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700147 res = &root_res->res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800148 res->name = info->name;
149 res->flags = flags;
150 res->start = start;
151 res->end = end;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700152
153 list_add_tail(&root_res->list, &info->resources);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800154}