blob: 5f11ff6f5389bd45c6279b98c347c7754b164a8a [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>
Andi Kleen69e1a332005-09-12 18:49:24 +02006#include <asm/numa.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +05307#include <asm/pci_x86.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Gary Hade62f420f2007-10-03 15:56:51 -07009struct pci_root_info {
Bjorn Helgaas42887b22009-10-06 15:33:49 -060010 struct acpi_device *bridge;
Gary Hade62f420f2007-10-03 15:56:51 -070011 char *name;
12 unsigned int res_num;
13 struct resource *res;
14 struct pci_bus *bus;
15 int busnum;
16};
17
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -070018static bool pci_use_crs = true;
19
20static int __init set_use_crs(const struct dmi_system_id *id)
21{
22 pci_use_crs = true;
23 return 0;
24}
25
26static const struct dmi_system_id pci_use_crs_table[] __initconst = {
27 /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
28 {
29 .callback = set_use_crs,
30 .ident = "IBM System x3800",
31 .matches = {
32 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
33 DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
34 },
35 },
36 {}
37};
38
39void __init pci_acpi_crs_quirks(void)
40{
41 int year;
42
43 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
44 pci_use_crs = false;
45
46 dmi_check_system(pci_use_crs_table);
47
48 /*
49 * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
50 * takes precedence over anything we figured out above.
51 */
52 if (pci_probe & PCI_ROOT_NO_CRS)
53 pci_use_crs = false;
54 else if (pci_probe & PCI_USE__CRS)
55 pci_use_crs = true;
56
57 printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
58 "if necessary, use \"pci=%s\" and report a bug\n",
59 pci_use_crs ? "Using" : "Ignoring",
60 pci_use_crs ? "nocrs" : "use_crs");
61}
62
Gary Hade62f420f2007-10-03 15:56:51 -070063static acpi_status
64resource_to_addr(struct acpi_resource *resource,
65 struct acpi_resource_address64 *addr)
66{
67 acpi_status status;
68
69 status = acpi_resource_to_address64(resource, addr);
70 if (ACPI_SUCCESS(status) &&
71 (addr->resource_type == ACPI_MEMORY_RANGE ||
72 addr->resource_type == ACPI_IO_RANGE) &&
73 addr->address_length > 0 &&
74 addr->producer_consumer == ACPI_PRODUCER) {
75 return AE_OK;
76 }
77 return AE_ERROR;
78}
79
80static acpi_status
81count_resource(struct acpi_resource *acpi_res, void *data)
82{
83 struct pci_root_info *info = data;
84 struct acpi_resource_address64 addr;
85 acpi_status status;
86
87 status = resource_to_addr(acpi_res, &addr);
88 if (ACPI_SUCCESS(status))
89 info->res_num++;
90 return AE_OK;
91}
92
Bjorn Helgaas03db42a2009-11-04 10:39:18 -070093static void
94align_resource(struct acpi_device *bridge, struct resource *res)
95{
96 int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
97
98 /*
99 * Host bridge windows are not BARs, but the decoders on the PCI side
100 * that claim this address space have starting alignment and length
101 * constraints, so fix any obvious BIOS goofs.
102 */
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600103 if (!IS_ALIGNED(res->start, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700104 dev_printk(KERN_DEBUG, &bridge->dev,
105 "host bridge window %pR invalid; "
106 "aligning start to %d-byte boundary\n", res, align);
107 res->start &= ~(align - 1);
108 }
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600109 if (!IS_ALIGNED(res->end + 1, align)) {
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700110 dev_printk(KERN_DEBUG, &bridge->dev,
111 "host bridge window %pR invalid; "
112 "aligning end to %d-byte boundary\n", res, align);
Bjorn Helgaasea7f1b62009-11-05 11:17:11 -0600113 res->end = ALIGN(res->end, align) - 1;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700114 }
115}
116
Gary Hade62f420f2007-10-03 15:56:51 -0700117static acpi_status
118setup_resource(struct acpi_resource *acpi_res, void *data)
119{
120 struct pci_root_info *info = data;
121 struct resource *res;
122 struct acpi_resource_address64 addr;
123 acpi_status status;
124 unsigned long flags;
125 struct resource *root;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700126 u64 start, end;
127
Gary Hade62f420f2007-10-03 15:56:51 -0700128 status = resource_to_addr(acpi_res, &addr);
129 if (!ACPI_SUCCESS(status))
130 return AE_OK;
131
132 if (addr.resource_type == ACPI_MEMORY_RANGE) {
133 root = &iomem_resource;
134 flags = IORESOURCE_MEM;
135 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
136 flags |= IORESOURCE_PREFETCH;
137 } else if (addr.resource_type == ACPI_IO_RANGE) {
138 root = &ioport_resource;
139 flags = IORESOURCE_IO;
140 } else
141 return AE_OK;
142
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700143 start = addr.minimum + addr.translation_offset;
144 end = start + addr.address_length - 1;
Gary Hadef9cde5f2009-05-27 12:41:44 -0700145
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700146 res = &info->res[info->res_num];
147 res->name = info->name;
148 res->flags = flags;
149 res->start = start;
150 res->end = end;
151 res->child = NULL;
Bjorn Helgaas03db42a2009-11-04 10:39:18 -0700152 align_resource(info->bridge, res);
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700153
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700154 if (!pci_use_crs) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700155 dev_printk(KERN_DEBUG, &info->bridge->dev,
156 "host bridge window %pR (ignored)\n", res);
157 return AE_OK;
158 }
159
Gary Hade62f420f2007-10-03 15:56:51 -0700160 if (insert_resource(root, res)) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600161 dev_err(&info->bridge->dev,
162 "can't allocate host bridge window %pR\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700163 } else {
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700164 pci_bus_add_resource(info->bus, res, 0);
Gary Hade62f420f2007-10-03 15:56:51 -0700165 info->res_num++;
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600166 if (addr.translation_offset)
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600167 dev_info(&info->bridge->dev, "host bridge window %pR "
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600168 "(PCI address [%#llx-%#llx])\n",
169 res, res->start - addr.translation_offset,
170 res->end - addr.translation_offset);
171 else
172 dev_info(&info->bridge->dev,
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600173 "host bridge window %pR\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700174 }
175 return AE_OK;
176}
177
178static void
Gary Hade62f420f2007-10-03 15:56:51 -0700179get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800180 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700181{
182 struct pci_root_info info;
183 size_t size;
184
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700185 if (pci_use_crs)
Bjorn Helgaas2fe2abf2010-02-23 10:24:36 -0700186 pci_bus_remove_resources(bus);
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700187
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600188 info.bridge = device;
Gary Hade62f420f2007-10-03 15:56:51 -0700189 info.bus = bus;
190 info.res_num = 0;
191 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
192 &info);
193 if (!info.res_num)
194 return;
195
196 size = sizeof(*info.res) * info.res_num;
197 info.res = kmalloc(size, GFP_KERNEL);
198 if (!info.res)
199 goto res_alloc_fail;
200
Gary Hadecb3576f2008-02-08 14:00:52 -0800201 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700202 if (!info.name)
203 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800204 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700205
206 info.res_num = 0;
207 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
208 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700209
210 return;
211
212name_alloc_fail:
213 kfree(info.res);
214res_alloc_fail:
215 return;
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
219{
Andi Kleen69e1a332005-09-12 18:49:24 +0200220 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300221 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800222 int node;
223#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300224 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800225#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200226
Jeff Garzika79e4192007-10-11 16:58:30 -0400227 if (domain && !pci_domains_supported) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700228 printk(KERN_WARNING "pci_bus %04x:%02x: "
229 "ignored (multiple domains not supported)\n",
230 domain, busnum);
Jeff Garzika79e4192007-10-11 16:58:30 -0400231 return NULL;
232 }
233
Yinghai Lu871d5f82008-02-19 03:20:09 -0800234 node = -1;
235#ifdef CONFIG_ACPI_NUMA
236 pxm = acpi_get_pxm(device->handle);
237 if (pxm >= 0)
238 node = pxm_to_node(pxm);
239 if (node != -1)
240 set_mp_bus_to_node(busnum, node);
241 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800242#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800243 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800244
245 if (node != -1 && !node_online(node))
246 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800247
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300248 /* Allocate per-root-bus (not per bus) arch-specific data.
249 * TODO: leak; this memory is never freed.
250 * It's arguable whether it's worth the trouble to care.
251 */
252 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
253 if (!sd) {
Bjorn Helgaas2a6bed82009-11-04 10:32:47 -0700254 printk(KERN_WARNING "pci_bus %04x:%02x: "
255 "ignored (out of memory)\n", domain, busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return NULL;
257 }
258
Jeff Garzika79e4192007-10-11 16:58:30 -0400259 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800260 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700261 /*
262 * Maybe the desired pci bus has been already scanned. In such case
263 * it is unnecessary to scan the pci bus with the given domain,busnum.
264 */
265 bus = pci_find_bus(domain, busnum);
266 if (bus) {
267 /*
268 * If the desired bus exits, the content of bus->sysdata will
269 * be replaced by sd.
270 */
271 memcpy(bus->sysdata, sd, sizeof(*sd));
272 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700273 } else {
274 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
275 if (bus) {
Bjorn Helgaasf1db6fd2009-11-04 10:39:13 -0700276 get_current_resources(device, busnum, domain, bus);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700277 bus->subordinate = pci_scan_child_bus(bus);
278 }
279 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300280
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300281 if (!bus)
282 kfree(sd);
283
Yinghai Ludbb61522008-04-19 01:30:16 -0700284 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200285#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700286 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700287 dev_printk(KERN_DEBUG, &bus->dev,
288 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700289#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700290 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200291#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700292 }
Gary Hade62f420f2007-10-03 15:56:51 -0700293
Andi Kleen69e1a332005-09-12 18:49:24 +0200294 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Robert Richter8dd779b2008-07-02 22:50:29 +0200297int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct pci_dev *dev = NULL;
300
301 if (pcibios_scanned)
302 return 0;
303
304 if (acpi_noirq)
305 return 0;
306
307 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
308 acpi_irq_penalty_init();
309 pcibios_scanned++;
310 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400311 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (pci_routeirq) {
314 /*
315 * PCI IRQ routing is set up by pci_enable_device(), but we
316 * also do it here in case there are still broken drivers that
317 * don't use pci_enable_device().
318 */
319 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800320 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e2008-02-18 09:44:13 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325}