blob: 1a9c0c6a1a1847f88c0024b06d18dfbd91d8cc22 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "pci.h"
8
Jeff Garzik752097c2007-10-12 22:34:40 -04009static int __devinit can_skip_ioresource_align(const struct dmi_system_id *d)
Gary Hade036fff42007-10-03 15:56:14 -070010{
11 pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
12 printk(KERN_INFO "PCI: %s detected, can skip ISA alignment\n", d->ident);
13 return 0;
14}
15
Adrian Bunk55b8d502007-11-09 07:02:11 +010016static struct dmi_system_id acpi_pciprobe_dmi_table[] __devinitdata = {
Gary Hade036fff42007-10-03 15:56:14 -070017/*
18 * Systems where PCI IO resource ISA alignment can be skipped
19 * when the ISA enable bit in the bridge control is not set
20 */
21 {
22 .callback = can_skip_ioresource_align,
23 .ident = "IBM System x3800",
24 .matches = {
25 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
26 DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
27 },
28 },
29 {
30 .callback = can_skip_ioresource_align,
31 .ident = "IBM System x3850",
32 .matches = {
33 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
34 DMI_MATCH(DMI_PRODUCT_NAME, "x3850"),
35 },
36 },
37 {
38 .callback = can_skip_ioresource_align,
39 .ident = "IBM System x3950",
40 .matches = {
41 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
42 DMI_MATCH(DMI_PRODUCT_NAME, "x3950"),
43 },
44 },
45 {}
46};
47
Gary Hade62f420f2007-10-03 15:56:51 -070048struct pci_root_info {
49 char *name;
50 unsigned int res_num;
51 struct resource *res;
52 struct pci_bus *bus;
53 int busnum;
54};
55
56static acpi_status
57resource_to_addr(struct acpi_resource *resource,
58 struct acpi_resource_address64 *addr)
59{
60 acpi_status status;
61
62 status = acpi_resource_to_address64(resource, addr);
63 if (ACPI_SUCCESS(status) &&
64 (addr->resource_type == ACPI_MEMORY_RANGE ||
65 addr->resource_type == ACPI_IO_RANGE) &&
66 addr->address_length > 0 &&
67 addr->producer_consumer == ACPI_PRODUCER) {
68 return AE_OK;
69 }
70 return AE_ERROR;
71}
72
73static acpi_status
74count_resource(struct acpi_resource *acpi_res, void *data)
75{
76 struct pci_root_info *info = data;
77 struct acpi_resource_address64 addr;
78 acpi_status status;
79
Yinghai Lu3d9befd2007-11-17 16:27:01 +010080 if (info->res_num >= PCI_BUS_NUM_RESOURCES)
81 return AE_OK;
82
Gary Hade62f420f2007-10-03 15:56:51 -070083 status = resource_to_addr(acpi_res, &addr);
84 if (ACPI_SUCCESS(status))
85 info->res_num++;
86 return AE_OK;
87}
88
89static acpi_status
90setup_resource(struct acpi_resource *acpi_res, void *data)
91{
92 struct pci_root_info *info = data;
93 struct resource *res;
94 struct acpi_resource_address64 addr;
95 acpi_status status;
96 unsigned long flags;
97 struct resource *root;
98
Yinghai Lu3d9befd2007-11-17 16:27:01 +010099 if (info->res_num >= PCI_BUS_NUM_RESOURCES)
100 return AE_OK;
101
Gary Hade62f420f2007-10-03 15:56:51 -0700102 status = resource_to_addr(acpi_res, &addr);
103 if (!ACPI_SUCCESS(status))
104 return AE_OK;
105
106 if (addr.resource_type == ACPI_MEMORY_RANGE) {
107 root = &iomem_resource;
108 flags = IORESOURCE_MEM;
109 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
110 flags |= IORESOURCE_PREFETCH;
111 } else if (addr.resource_type == ACPI_IO_RANGE) {
112 root = &ioport_resource;
113 flags = IORESOURCE_IO;
114 } else
115 return AE_OK;
116
117 res = &info->res[info->res_num];
118 res->name = info->name;
119 res->flags = flags;
120 res->start = addr.minimum + addr.translation_offset;
121 res->end = res->start + addr.address_length - 1;
122 res->child = NULL;
123
124 if (insert_resource(root, res)) {
125 printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx "
126 "from %s for %s\n", (unsigned long) res->start,
127 (unsigned long) res->end, root->name, info->name);
128 } else {
129 info->bus->resource[info->res_num] = res;
130 info->res_num++;
131 }
132 return AE_OK;
133}
134
135static void
136adjust_transparent_bridge_resources(struct pci_bus *bus)
137{
138 struct pci_dev *dev;
139
140 list_for_each_entry(dev, &bus->devices, bus_list) {
141 int i;
142 u16 class = dev->class >> 8;
143
144 if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) {
145 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
146 dev->subordinate->resource[i] =
147 dev->bus->resource[i - 3];
148 }
149 }
150}
151
152static void
153get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800154 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700155{
156 struct pci_root_info info;
157 size_t size;
158
159 info.bus = bus;
160 info.res_num = 0;
161 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
162 &info);
163 if (!info.res_num)
164 return;
165
166 size = sizeof(*info.res) * info.res_num;
167 info.res = kmalloc(size, GFP_KERNEL);
168 if (!info.res)
169 goto res_alloc_fail;
170
Gary Hadecb3576f2008-02-08 14:00:52 -0800171 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700172 if (!info.name)
173 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800174 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700175
176 info.res_num = 0;
177 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
178 &info);
179 if (info.res_num)
180 adjust_transparent_bridge_resources(bus);
181
182 return;
183
184name_alloc_fail:
185 kfree(info.res);
186res_alloc_fail:
187 return;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
191{
Andi Kleen69e1a332005-09-12 18:49:24 +0200192 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300193 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800194 int node;
195#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300196 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800197#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200198
Gary Hade036fff42007-10-03 15:56:14 -0700199 dmi_check_system(acpi_pciprobe_dmi_table);
200
Jeff Garzika79e4192007-10-11 16:58:30 -0400201 if (domain && !pci_domains_supported) {
202 printk(KERN_WARNING "PCI: Multiple domains not supported "
203 "(dom %d, bus %d)\n", domain, busnum);
204 return NULL;
205 }
206
Yinghai Lu871d5f82008-02-19 03:20:09 -0800207 node = -1;
208#ifdef CONFIG_ACPI_NUMA
209 pxm = acpi_get_pxm(device->handle);
210 if (pxm >= 0)
211 node = pxm_to_node(pxm);
212 if (node != -1)
213 set_mp_bus_to_node(busnum, node);
214 else
215 node = get_mp_bus_to_node(busnum);
216#endif
217
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300218 /* Allocate per-root-bus (not per bus) arch-specific data.
219 * TODO: leak; this memory is never freed.
220 * It's arguable whether it's worth the trouble to care.
221 */
222 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
223 if (!sd) {
224 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return NULL;
226 }
227
Jeff Garzika79e4192007-10-11 16:58:30 -0400228 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800229 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700230 /*
231 * Maybe the desired pci bus has been already scanned. In such case
232 * it is unnecessary to scan the pci bus with the given domain,busnum.
233 */
234 bus = pci_find_bus(domain, busnum);
235 if (bus) {
236 /*
237 * If the desired bus exits, the content of bus->sysdata will
238 * be replaced by sd.
239 */
240 memcpy(bus->sysdata, sd, sizeof(*sd));
241 kfree(sd);
242 } else
243 bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300244
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300245 if (!bus)
246 kfree(sd);
247
Andi Kleen69e1a332005-09-12 18:49:24 +0200248#ifdef CONFIG_ACPI_NUMA
Yinghai Lu871d5f82008-02-19 03:20:09 -0800249 if (bus) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200250 if (pxm >= 0) {
Yinghai Lu871d5f82008-02-19 03:20:09 -0800251 printk(KERN_DEBUG "bus %02x -> pxm %d -> node %d\n",
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700252 busnum, pxm, pxm_to_node(pxm));
Andi Kleen69e1a332005-09-12 18:49:24 +0200253 }
254 }
255#endif
Gary Hade62f420f2007-10-03 15:56:51 -0700256
257 if (bus && (pci_probe & PCI_USE__CRS))
Gary Hadecb3576f2008-02-08 14:00:52 -0800258 get_current_resources(device, busnum, domain, bus);
Andi Kleen69e1a332005-09-12 18:49:24 +0200259 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262extern int pci_routeirq;
263static int __init pci_acpi_init(void)
264{
265 struct pci_dev *dev = NULL;
266
267 if (pcibios_scanned)
268 return 0;
269
270 if (acpi_noirq)
271 return 0;
272
273 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
274 acpi_irq_penalty_init();
275 pcibios_scanned++;
276 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400277 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (pci_routeirq) {
280 /*
281 * PCI IRQ routing is set up by pci_enable_device(), but we
282 * also do it here in case there are still broken drivers that
283 * don't use pci_enable_device().
284 */
285 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800286 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290#ifdef CONFIG_X86_IO_APIC
291 if (acpi_ioapic)
292 print_IO_APIC();
293#endif
294
295 return 0;
296}
297subsys_initcall(pci_acpi_init);