blob: d37e2fec97e58fd3b4c3d35a5903fd6cf78060d1 [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
13 if (list_empty(&pci_root_infos))
14 return NULL;
15
16 list_for_each_entry(info, &pci_root_infos, list)
Yinghai Lua10bb122012-05-17 18:51:12 -070017 if (info->busn.start == bus)
Yinghai Lud28e5ac2012-04-02 18:31:54 -070018 return info;
19
20 return NULL;
21}
Yinghai Lu67f241f2009-11-11 22:27:40 -080022
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060023void x86_pci_root_bus_resources(int bus, struct list_head *resources)
Yinghai Lu67f241f2009-11-11 22:27:40 -080024{
Yinghai Lud28e5ac2012-04-02 18:31:54 -070025 struct pci_root_info *info = x86_find_pci_root_info(bus);
26 struct pci_root_res *root_res;
Yinghai Lua10bb122012-05-17 18:51:12 -070027 struct pci_host_bridge_window *window;
28 bool found = false;
Yinghai Lu67f241f2009-11-11 22:27:40 -080029
Yinghai Lud28e5ac2012-04-02 18:31:54 -070030 if (!info)
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060031 goto default_resources;
Yinghai Lu67f241f2009-11-11 22:27:40 -080032
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060033 printk(KERN_DEBUG "PCI: root bus %02x: hardware-probed resources\n",
34 bus);
Yinghai Lu67f241f2009-11-11 22:27:40 -080035
Yinghai Lua10bb122012-05-17 18:51:12 -070036 /* already added by acpi ? */
37 list_for_each_entry(window, resources, list)
38 if (window->res->flags & IORESOURCE_BUS) {
39 found = true;
40 break;
41 }
42
43 if (!found)
44 pci_add_resource(resources, &info->busn);
45
Yinghai Lud28e5ac2012-04-02 18:31:54 -070046 list_for_each_entry(root_res, &info->resources, list) {
Yinghai Lu67f241f2009-11-11 22:27:40 -080047 struct resource *res;
48 struct resource *root;
49
Yinghai Lud28e5ac2012-04-02 18:31:54 -070050 res = &root_res->res;
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060051 pci_add_resource(resources, res);
Yinghai Lu67f241f2009-11-11 22:27:40 -080052 if (res->flags & IORESOURCE_IO)
53 root = &ioport_resource;
54 else
55 root = &iomem_resource;
56 insert_resource(root, res);
57 }
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060058 return;
59
60default_resources:
61 /*
62 * We don't have any host bridge aperture information from the
63 * "native host bridge drivers," e.g., amd_bus or broadcom_bus,
64 * so fall back to the defaults historically used by pci_create_bus().
65 */
66 printk(KERN_DEBUG "PCI: root bus %02x: using default resources\n", bus);
67 pci_add_resource(resources, &ioport_resource);
68 pci_add_resource(resources, &iomem_resource);
Yinghai Lu67f241f2009-11-11 22:27:40 -080069}
70
Yinghai Lud28e5ac2012-04-02 18:31:54 -070071struct pci_root_info __init *alloc_pci_root_info(int bus_min, int bus_max,
72 int node, int link)
73{
74 struct pci_root_info *info;
75
76 info = kzalloc(sizeof(*info), GFP_KERNEL);
77
78 if (!info)
79 return info;
80
Yinghai Lua10bb122012-05-17 18:51:12 -070081 sprintf(info->name, "PCI Bus #%02x", bus_min);
82
Yinghai Lud28e5ac2012-04-02 18:31:54 -070083 INIT_LIST_HEAD(&info->resources);
Yinghai Lua10bb122012-05-17 18:51:12 -070084 info->busn.name = info->name;
85 info->busn.start = bus_min;
86 info->busn.end = bus_max;
87 info->busn.flags = IORESOURCE_BUS;
Yinghai Lud28e5ac2012-04-02 18:31:54 -070088 info->node = node;
89 info->link = link;
90
91 list_add_tail(&info->list, &pci_root_infos);
92
93 return info;
94}
95
Yinghai Lub74fd232010-02-10 01:20:08 -080096void __devinit update_res(struct pci_root_info *info, resource_size_t start,
97 resource_size_t end, unsigned long flags, int merge)
Yinghai Lu67f241f2009-11-11 22:27:40 -080098{
Yinghai Lu67f241f2009-11-11 22:27:40 -080099 struct resource *res;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700100 struct pci_root_res *root_res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800101
102 if (start > end)
103 return;
104
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800105 if (start == MAX_RESOURCE)
106 return;
107
Yinghai Lu67f241f2009-11-11 22:27:40 -0800108 if (!merge)
109 goto addit;
110
111 /* try to merge it with old one */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700112 list_for_each_entry(root_res, &info->resources, list) {
Yinghai Lub74fd232010-02-10 01:20:08 -0800113 resource_size_t final_start, final_end;
114 resource_size_t common_start, common_end;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800115
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700116 res = &root_res->res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800117 if (res->flags != flags)
118 continue;
119
Yinghai Lub74fd232010-02-10 01:20:08 -0800120 common_start = max(res->start, start);
121 common_end = min(res->end, end);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800122 if (common_start > common_end + 1)
123 continue;
124
Yinghai Lub74fd232010-02-10 01:20:08 -0800125 final_start = min(res->start, start);
126 final_end = max(res->end, end);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800127
128 res->start = final_start;
129 res->end = final_end;
130 return;
131 }
132
133addit:
134
135 /* need to add that */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700136 root_res = kzalloc(sizeof(*root_res), GFP_KERNEL);
137 if (!root_res)
Yinghai Lu67f241f2009-11-11 22:27:40 -0800138 return;
139
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700140 res = &root_res->res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800141 res->name = info->name;
142 res->flags = flags;
143 res->start = start;
144 res->end = end;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700145
146 list_add_tail(&root_res->list, &info->resources);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800147}