]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/pci/acpi.c
PCI: augment bus resource table with a list
[mirror_ubuntu-focal-kernel.git] / arch / x86 / pci / acpi.c
CommitLineData
1da177e4
LT
1#include <linux/pci.h>
2#include <linux/acpi.h>
3#include <linux/init.h>
b33fa1f3 4#include <linux/irq.h>
036fff4c 5#include <linux/dmi.h>
69e1a33f 6#include <asm/numa.h>
82487711 7#include <asm/pci_x86.h>
1da177e4 8
62f420f8 9struct pci_root_info {
42887b29 10 struct acpi_device *bridge;
62f420f8
GH
11 char *name;
12 unsigned int res_num;
13 struct resource *res;
14 struct pci_bus *bus;
15 int busnum;
16};
17
18static acpi_status
19resource_to_addr(struct acpi_resource *resource,
20 struct acpi_resource_address64 *addr)
21{
22 acpi_status status;
23
24 status = acpi_resource_to_address64(resource, addr);
25 if (ACPI_SUCCESS(status) &&
26 (addr->resource_type == ACPI_MEMORY_RANGE ||
27 addr->resource_type == ACPI_IO_RANGE) &&
28 addr->address_length > 0 &&
29 addr->producer_consumer == ACPI_PRODUCER) {
30 return AE_OK;
31 }
32 return AE_ERROR;
33}
34
35static acpi_status
36count_resource(struct acpi_resource *acpi_res, void *data)
37{
38 struct pci_root_info *info = data;
39 struct acpi_resource_address64 addr;
40 acpi_status status;
41
42 status = resource_to_addr(acpi_res, &addr);
43 if (ACPI_SUCCESS(status))
44 info->res_num++;
45 return AE_OK;
46}
47
03db42ad
BH
48static void
49align_resource(struct acpi_device *bridge, struct resource *res)
50{
51 int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
52
53 /*
54 * Host bridge windows are not BARs, but the decoders on the PCI side
55 * that claim this address space have starting alignment and length
56 * constraints, so fix any obvious BIOS goofs.
57 */
ea7f1b6e 58 if (!IS_ALIGNED(res->start, align)) {
03db42ad
BH
59 dev_printk(KERN_DEBUG, &bridge->dev,
60 "host bridge window %pR invalid; "
61 "aligning start to %d-byte boundary\n", res, align);
62 res->start &= ~(align - 1);
63 }
ea7f1b6e 64 if (!IS_ALIGNED(res->end + 1, align)) {
03db42ad
BH
65 dev_printk(KERN_DEBUG, &bridge->dev,
66 "host bridge window %pR invalid; "
67 "aligning end to %d-byte boundary\n", res, align);
ea7f1b6e 68 res->end = ALIGN(res->end, align) - 1;
03db42ad
BH
69 }
70}
71
62f420f8
GH
72static acpi_status
73setup_resource(struct acpi_resource *acpi_res, void *data)
74{
75 struct pci_root_info *info = data;
76 struct resource *res;
77 struct acpi_resource_address64 addr;
78 acpi_status status;
79 unsigned long flags;
80 struct resource *root;
2cdb3f1d
YL
81 u64 start, end;
82
62f420f8
GH
83 status = resource_to_addr(acpi_res, &addr);
84 if (!ACPI_SUCCESS(status))
85 return AE_OK;
86
87 if (addr.resource_type == ACPI_MEMORY_RANGE) {
88 root = &iomem_resource;
89 flags = IORESOURCE_MEM;
90 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
91 flags |= IORESOURCE_PREFETCH;
92 } else if (addr.resource_type == ACPI_IO_RANGE) {
93 root = &ioport_resource;
94 flags = IORESOURCE_IO;
95 } else
96 return AE_OK;
97
2cdb3f1d
YL
98 start = addr.minimum + addr.translation_offset;
99 end = start + addr.address_length - 1;
f9cde5ff 100
2cdb3f1d
YL
101 res = &info->res[info->res_num];
102 res->name = info->name;
103 res->flags = flags;
104 res->start = start;
105 res->end = end;
106 res->child = NULL;
03db42ad 107 align_resource(info->bridge, res);
2cdb3f1d 108
f1db6fde
BH
109 if (!(pci_probe & PCI_USE__CRS)) {
110 dev_printk(KERN_DEBUG, &info->bridge->dev,
111 "host bridge window %pR (ignored)\n", res);
112 return AE_OK;
113 }
114
62f420f8 115 if (insert_resource(root, res)) {
c7dabef8
BH
116 dev_err(&info->bridge->dev,
117 "can't allocate host bridge window %pR\n", res);
62f420f8 118 } else {
2fe2abf8 119 pci_bus_add_resource(info->bus, res, 0);
62f420f8 120 info->res_num++;
42887b29 121 if (addr.translation_offset)
c7dabef8 122 dev_info(&info->bridge->dev, "host bridge window %pR "
42887b29
BH
123 "(PCI address [%#llx-%#llx])\n",
124 res, res->start - addr.translation_offset,
125 res->end - addr.translation_offset);
126 else
127 dev_info(&info->bridge->dev,
c7dabef8 128 "host bridge window %pR\n", res);
62f420f8
GH
129 }
130 return AE_OK;
131}
132
62f420f8
GH
133static void
134get_current_resources(struct acpi_device *device, int busnum,
cb3576fa 135 int domain, struct pci_bus *bus)
62f420f8
GH
136{
137 struct pci_root_info info;
138 size_t size;
139
2fe2abf8
BH
140 if (pci_probe & PCI_USE__CRS)
141 pci_bus_remove_resources(bus);
142 else
f1db6fde
BH
143 dev_info(&device->dev,
144 "ignoring host bridge windows from ACPI; "
145 "boot with \"pci=use_crs\" to use them\n");
146
42887b29 147 info.bridge = device;
62f420f8
GH
148 info.bus = bus;
149 info.res_num = 0;
150 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
151 &info);
152 if (!info.res_num)
153 return;
154
155 size = sizeof(*info.res) * info.res_num;
156 info.res = kmalloc(size, GFP_KERNEL);
157 if (!info.res)
158 goto res_alloc_fail;
159
cb3576fa 160 info.name = kmalloc(16, GFP_KERNEL);
62f420f8
GH
161 if (!info.name)
162 goto name_alloc_fail;
cb3576fa 163 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
62f420f8
GH
164
165 info.res_num = 0;
166 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
167 &info);
62f420f8
GH
168
169 return;
170
171name_alloc_fail:
172 kfree(info.res);
173res_alloc_fail:
174 return;
175}
176
1da177e4
LT
177struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
178{
69e1a33f 179 struct pci_bus *bus;
08f1c192 180 struct pci_sysdata *sd;
871d5f8d
YL
181 int node;
182#ifdef CONFIG_ACPI_NUMA
08f1c192 183 int pxm;
871d5f8d 184#endif
08f1c192 185
a79e4198 186 if (domain && !pci_domains_supported) {
2a6bed83
BH
187 printk(KERN_WARNING "pci_bus %04x:%02x: "
188 "ignored (multiple domains not supported)\n",
189 domain, busnum);
a79e4198
JG
190 return NULL;
191 }
192
871d5f8d
YL
193 node = -1;
194#ifdef CONFIG_ACPI_NUMA
195 pxm = acpi_get_pxm(device->handle);
196 if (pxm >= 0)
197 node = pxm_to_node(pxm);
198 if (node != -1)
199 set_mp_bus_to_node(busnum, node);
200 else
871d5f8d 201#endif
871d5f8d 202 node = get_mp_bus_to_node(busnum);
b755de8d
YL
203
204 if (node != -1 && !node_online(node))
205 node = -1;
871d5f8d 206
08f1c192
MBY
207 /* Allocate per-root-bus (not per bus) arch-specific data.
208 * TODO: leak; this memory is never freed.
209 * It's arguable whether it's worth the trouble to care.
210 */
211 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
212 if (!sd) {
2a6bed83
BH
213 printk(KERN_WARNING "pci_bus %04x:%02x: "
214 "ignored (out of memory)\n", domain, busnum);
08f1c192
MBY
215 return NULL;
216 }
69e1a33f 217
a79e4198 218 sd->domain = domain;
871d5f8d 219 sd->node = node;
b87e81e5 220 /*
221 * Maybe the desired pci bus has been already scanned. In such case
222 * it is unnecessary to scan the pci bus with the given domain,busnum.
223 */
224 bus = pci_find_bus(domain, busnum);
225 if (bus) {
226 /*
227 * If the desired bus exits, the content of bus->sysdata will
228 * be replaced by sd.
229 */
230 memcpy(bus->sysdata, sd, sizeof(*sd));
231 kfree(sd);
626fdfec
YL
232 } else {
233 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
234 if (bus) {
f1db6fde 235 get_current_resources(device, busnum, domain, bus);
626fdfec
YL
236 bus->subordinate = pci_scan_child_bus(bus);
237 }
238 }
08f1c192 239
08f1c192
MBY
240 if (!bus)
241 kfree(sd);
242
dbb6152e 243 if (bus && node != -1) {
69e1a33f 244#ifdef CONFIG_ACPI_NUMA
dbb6152e 245 if (pxm >= 0)
2b8c2efe
BH
246 dev_printk(KERN_DEBUG, &bus->dev,
247 "on NUMA node %d (pxm %d)\n", node, pxm);
dbb6152e 248#else
2b8c2efe 249 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
69e1a33f 250#endif
dbb6152e 251 }
62f420f8 252
69e1a33f 253 return bus;
1da177e4
LT
254}
255
8dd779b1 256int __init pci_acpi_init(void)
1da177e4
LT
257{
258 struct pci_dev *dev = NULL;
259
260 if (pcibios_scanned)
261 return 0;
262
263 if (acpi_noirq)
264 return 0;
265
266 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
267 acpi_irq_penalty_init();
268 pcibios_scanned++;
269 pcibios_enable_irq = acpi_pci_irq_enable;
87bec66b 270 pcibios_disable_irq = acpi_pci_irq_disable;
1da177e4
LT
271
272 if (pci_routeirq) {
273 /*
274 * PCI IRQ routing is set up by pci_enable_device(), but we
275 * also do it here in case there are still broken drivers that
276 * don't use pci_enable_device().
277 */
278 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
fb37fb96 279 for_each_pci_dev(dev)
1da177e4 280 acpi_pci_irq_enable(dev);
657472e9 281 }
1da177e4 282
1da177e4
LT
283 return 0;
284}