blob: 44f83ce0247019d78d02f6b879c13da4c9866f0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/pci.h>
2#include <linux/acpi.h>
3#include <linux/init.h>
Nick Pigginb33fa1f2005-10-01 02:34:42 +10004#include <linux/irq.h>
Gary Hade036fff42007-10-03 15:56:14 -07005#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Andi Kleen69e1a332005-09-12 18:49:24 +02007#include <asm/numa.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +05308#include <asm/pci_x86.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Gary Hade62f420f2007-10-03 15:56:51 -070010struct pci_root_info {
Bjorn Helgaas42887b22009-10-06 15:33:49 -060011 struct acpi_device *bridge;
Gary Hade62f420f2007-10-03 15:56:51 -070012 char *name;
13 unsigned int res_num;
14 struct resource *res;
15 struct pci_bus *bus;
16 int busnum;
17};
18
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -070019static bool pci_use_crs = true;
20
21static int __init set_use_crs(const struct dmi_system_id *id)
22{
23 pci_use_crs = true;
24 return 0;
25}
26
27static const struct dmi_system_id pci_use_crs_table[] __initconst = {
28 /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
29 {
30 .callback = set_use_crs,
31 .ident = "IBM System x3800",
32 .matches = {
33 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
34 DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
35 },
36 },
37 {}
38};
39
40void __init pci_acpi_crs_quirks(void)
41{
42 int year;
43
44 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
45 pci_use_crs = false;
46
47 dmi_check_system(pci_use_crs_table);
48
49 /*
50 * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
51 * takes precedence over anything we figured out above.
52 */
53 if (pci_probe & PCI_ROOT_NO_CRS)
54 pci_use_crs = false;
55 else if (pci_probe & PCI_USE__CRS)
56 pci_use_crs = true;
57
58 printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
59 "if necessary, use \"pci=%s\" and report a bug\n",
60 pci_use_crs ? "Using" : "Ignoring",
61 pci_use_crs ? "nocrs" : "use_crs");
62}
63
Gary Hade62f420f2007-10-03 15:56:51 -070064static acpi_status
65resource_to_addr(struct acpi_resource *resource,
66 struct acpi_resource_address64 *addr)
67{
68 acpi_status status;
Bjorn Helgaas66528fd2010-04-20 13:52:41 -060069 struct acpi_resource_memory24 *memory24;
70 struct acpi_resource_memory32 *memory32;
71 struct acpi_resource_fixed_memory32 *fixed_memory32;
Gary Hade62f420f2007-10-03 15:56:51 -070072
Bjorn Helgaas66528fd2010-04-20 13:52:41 -060073 memset(addr, 0, sizeof(*addr));
74 switch (resource->type) {
75 case ACPI_RESOURCE_TYPE_MEMORY24:
76 memory24 = &resource->data.memory24;
77 addr->resource_type = ACPI_MEMORY_RANGE;
78 addr->minimum = memory24->minimum;
79 addr->address_length = memory24->address_length;
80 addr->maximum = addr->minimum + addr->address_length - 1;
Gary Hade62f420f2007-10-03 15:56:51 -070081 return AE_OK;
Bjorn Helgaas66528fd2010-04-20 13:52:41 -060082 case ACPI_RESOURCE_TYPE_MEMORY32:
83 memory32 = &resource->data.memory32;
84 addr->resource_type = ACPI_MEMORY_RANGE;
85 addr->minimum = memory32->minimum;
86 addr->address_length = memory32->address_length;
87 addr->maximum = addr->minimum + addr->address_length - 1;
88 return AE_OK;
89 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
90 fixed_memory32 = &resource->data.fixed_memory32;
91 addr->resource_type = ACPI_MEMORY_RANGE;
92 addr->minimum = fixed_memory32->address;
93 addr->address_length = fixed_memory32->address_length;
94 addr->maximum = addr->minimum + addr->address_length - 1;
95 return AE_OK;
96 case ACPI_RESOURCE_TYPE_ADDRESS16:
97 case ACPI_RESOURCE_TYPE_ADDRESS32:
98 case ACPI_RESOURCE_TYPE_ADDRESS64:
99 status = acpi_resource_to_address64(resource, addr);
100 if (ACPI_SUCCESS(status) &&
101 (addr->resource_type == ACPI_MEMORY_RANGE ||
102 addr->resource_type == ACPI_IO_RANGE) &&
103 addr->address_length > 0) {
104 return AE_OK;
105 }
106 break;
Gary Hade62f420f2007-10-03 15:56:51 -0700107 }
108 return AE_ERROR;
109}
110
111static acpi_status
112count_resource(struct acpi_resource *acpi_res, void *data)
113{
114 struct pci_root_info *info = data;
115 struct acpi_resource_address64 addr;
116 acpi_status status;
117
118 status = resource_to_addr(acpi_res, &addr);
119 if (ACPI_SUCCESS(status))
120 info->res_num++;
121 return AE_OK;
122}
123
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700124static void
125align_resource(struct acpi_device *bridge, struct resource *res)
126{
127 int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
128
129 /*
130 * Host bridge windows are not BARs, but the decoders on the PCI side
131 * that claim this address space have starting alignment and length
132 * constraints, so fix any obvious BIOS goofs.
133 */
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600134 if (!IS_ALIGNED(res->start, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700135 dev_printk(KERN_DEBUG, &bridge->dev,
136 "host bridge window %pR invalid; "
137 "aligning start to %d-byte boundary\n", res, align);
138 res->start &= ~(align - 1);
139 }
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600140 if (!IS_ALIGNED(res->end + 1, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700141 dev_printk(KERN_DEBUG, &bridge->dev,
142 "host bridge window %pR invalid; "
143 "aligning end to %d-byte boundary\n", res, align);
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600144 res->end = ALIGN(res->end, align) - 1;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700145 }
146}
147
Gary Hade62f420f2007-10-03 15:56:51 -0700148static acpi_status
149setup_resource(struct acpi_resource *acpi_res, void *data)
150{
151 struct pci_root_info *info = data;
152 struct resource *res;
153 struct acpi_resource_address64 addr;
154 acpi_status status;
155 unsigned long flags;
Bjorn Helgaaseb9fc8e2010-03-25 09:28:24 -0600156 struct resource *root, *conflict;
Bjorn Helgaasd558b482010-03-25 09:28:30 -0600157 u64 start, end, max_len;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700158
Gary Hade62f420f2007-10-03 15:56:51 -0700159 status = resource_to_addr(acpi_res, &addr);
160 if (!ACPI_SUCCESS(status))
161 return AE_OK;
162
163 if (addr.resource_type == ACPI_MEMORY_RANGE) {
164 root = &iomem_resource;
165 flags = IORESOURCE_MEM;
166 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
167 flags |= IORESOURCE_PREFETCH;
168 } else if (addr.resource_type == ACPI_IO_RANGE) {
169 root = &ioport_resource;
170 flags = IORESOURCE_IO;
171 } else
172 return AE_OK;
173
Bjorn Helgaasd558b482010-03-25 09:28:30 -0600174 max_len = addr.maximum - addr.minimum + 1;
175 if (addr.address_length > max_len) {
176 dev_printk(KERN_DEBUG, &info->bridge->dev,
177 "host bridge window length %#llx doesn't fit in "
178 "%#llx-%#llx, trimming\n",
179 (unsigned long long) addr.address_length,
180 (unsigned long long) addr.minimum,
181 (unsigned long long) addr.maximum);
182 addr.address_length = max_len;
183 }
184
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700185 start = addr.minimum + addr.translation_offset;
186 end = start + addr.address_length - 1;
Gary Hadef9cde5f2009-05-27 12:41:44 -0700187
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700188 res = &info->res[info->res_num];
189 res->name = info->name;
190 res->flags = flags;
191 res->start = start;
192 res->end = end;
193 res->child = NULL;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700194 align_resource(info->bridge, res);
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700195
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700196 if (!pci_use_crs) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700197 dev_printk(KERN_DEBUG, &info->bridge->dev,
198 "host bridge window %pR (ignored)\n", res);
199 return AE_OK;
200 }
201
Bjorn Helgaaseb9fc8e2010-03-25 09:28:24 -0600202 conflict = insert_resource_conflict(root, res);
203 if (conflict) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600204 dev_err(&info->bridge->dev,
Bjorn Helgaaseb9fc8e2010-03-25 09:28:24 -0600205 "address space collision: host bridge window %pR "
206 "conflicts with %s %pR\n",
207 res, conflict->name, conflict);
Gary Hade62f420f2007-10-03 15:56:51 -0700208 } else {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700209 pci_bus_add_resource(info->bus, res, 0);
Gary Hade62f420f2007-10-03 15:56:51 -0700210 info->res_num++;
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600211 if (addr.translation_offset)
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600212 dev_info(&info->bridge->dev, "host bridge window %pR "
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600213 "(PCI address [%#llx-%#llx])\n",
214 res, res->start - addr.translation_offset,
215 res->end - addr.translation_offset);
216 else
217 dev_info(&info->bridge->dev,
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600218 "host bridge window %pR\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700219 }
220 return AE_OK;
221}
222
223static void
Gary Hade62f420f2007-10-03 15:56:51 -0700224get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800225 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700226{
227 struct pci_root_info info;
228 size_t size;
229
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700230 if (pci_use_crs)
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700231 pci_bus_remove_resources(bus);
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700232
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600233 info.bridge = device;
Gary Hade62f420f2007-10-03 15:56:51 -0700234 info.bus = bus;
235 info.res_num = 0;
236 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
237 &info);
238 if (!info.res_num)
239 return;
240
241 size = sizeof(*info.res) * info.res_num;
242 info.res = kmalloc(size, GFP_KERNEL);
243 if (!info.res)
244 goto res_alloc_fail;
245
Gary Hadecb3576f2008-02-08 14:00:52 -0800246 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700247 if (!info.name)
248 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800249 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700250
251 info.res_num = 0;
252 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
253 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700254
255 return;
256
257name_alloc_fail:
258 kfree(info.res);
259res_alloc_fail:
260 return;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
264{
Andi Kleen69e1a332005-09-12 18:49:24 +0200265 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300266 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800267 int node;
268#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300269 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800270#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200271
Jeff Garzika79e4192007-10-11 16:58:30 -0400272 if (domain && !pci_domains_supported) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700273 printk(KERN_WARNING "pci_bus %04x:%02x: "
274 "ignored (multiple domains not supported)\n",
275 domain, busnum);
Jeff Garzika79e4192007-10-11 16:58:30 -0400276 return NULL;
277 }
278
Yinghai Lu871d5f82008-02-19 03:20:09 -0800279 node = -1;
280#ifdef CONFIG_ACPI_NUMA
281 pxm = acpi_get_pxm(device->handle);
282 if (pxm >= 0)
283 node = pxm_to_node(pxm);
284 if (node != -1)
285 set_mp_bus_to_node(busnum, node);
286 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800287#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800288 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800289
290 if (node != -1 && !node_online(node))
291 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800292
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300293 /* Allocate per-root-bus (not per bus) arch-specific data.
294 * TODO: leak; this memory is never freed.
295 * It's arguable whether it's worth the trouble to care.
296 */
297 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
298 if (!sd) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700299 printk(KERN_WARNING "pci_bus %04x:%02x: "
300 "ignored (out of memory)\n", domain, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return NULL;
302 }
303
Jeff Garzika79e4192007-10-11 16:58:30 -0400304 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800305 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700306 /*
307 * Maybe the desired pci bus has been already scanned. In such case
308 * it is unnecessary to scan the pci bus with the given domain,busnum.
309 */
310 bus = pci_find_bus(domain, busnum);
311 if (bus) {
312 /*
313 * If the desired bus exits, the content of bus->sysdata will
314 * be replaced by sd.
315 */
316 memcpy(bus->sysdata, sd, sizeof(*sd));
317 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700318 } else {
319 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
320 if (bus) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700321 get_current_resources(device, busnum, domain, bus);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700322 bus->subordinate = pci_scan_child_bus(bus);
323 }
324 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300325
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300326 if (!bus)
327 kfree(sd);
328
Yinghai Ludbb61522008-04-19 01:30:16 -0700329 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200330#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700331 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700332 dev_printk(KERN_DEBUG, &bus->dev,
333 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700334#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700335 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200336#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700337 }
Gary Hade62f420f2007-10-03 15:56:51 -0700338
Andi Kleen69e1a332005-09-12 18:49:24 +0200339 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Robert Richter8dd779b2008-07-02 22:50:29 +0200342int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct pci_dev *dev = NULL;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (acpi_noirq)
Thomas Gleixnerb72d0db2009-08-29 16:24:51 +0200347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
350 acpi_irq_penalty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400352 pcibios_disable_irq = acpi_pci_irq_disable;
Thomas Gleixnerab3b3792009-08-29 17:47:33 +0200353 x86_init.pci.init_irq = x86_init_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (pci_routeirq) {
356 /*
357 * PCI IRQ routing is set up by pci_enable_device(), but we
358 * also do it here in case there are still broken drivers that
359 * don't use pci_enable_device().
360 */
361 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800362 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367}