blob: 9b3daf976732a0fc535de5c81c11272b9d11544a [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
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
Gary Hadef9cde5f2009-05-27 12:41:44 -070048static int
49bus_has_transparent_bridge(struct pci_bus *bus)
50{
51 struct pci_dev *dev;
52
53 list_for_each_entry(dev, &bus->devices, bus_list) {
54 u16 class = dev->class >> 8;
55
56 if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent)
57 return true;
58 }
59 return false;
60}
61
Gary Hade62f420f2007-10-03 15:56:51 -070062static acpi_status
63setup_resource(struct acpi_resource *acpi_res, void *data)
64{
65 struct pci_root_info *info = data;
66 struct resource *res;
67 struct acpi_resource_address64 addr;
68 acpi_status status;
69 unsigned long flags;
70 struct resource *root;
Gary Hadef9cde5f2009-05-27 12:41:44 -070071 int max_root_bus_resources = PCI_BUS_NUM_RESOURCES;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070072 u64 start, end;
73
74 if (bus_has_transparent_bridge(info->bus))
75 max_root_bus_resources -= 3;
Yinghai Lu3d9befd2007-11-17 16:27:01 +010076
Gary Hade62f420f2007-10-03 15:56:51 -070077 status = resource_to_addr(acpi_res, &addr);
78 if (!ACPI_SUCCESS(status))
79 return AE_OK;
80
81 if (addr.resource_type == ACPI_MEMORY_RANGE) {
82 root = &iomem_resource;
83 flags = IORESOURCE_MEM;
84 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
85 flags |= IORESOURCE_PREFETCH;
86 } else if (addr.resource_type == ACPI_IO_RANGE) {
87 root = &ioport_resource;
88 flags = IORESOURCE_IO;
89 } else
90 return AE_OK;
91
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070092 start = addr.minimum + addr.translation_offset;
93 end = start + addr.address_length - 1;
Gary Hadef9cde5f2009-05-27 12:41:44 -070094 if (info->res_num >= max_root_bus_resources) {
95 printk(KERN_WARNING "PCI: Failed to allocate 0x%lx-0x%lx "
96 "from %s for %s due to _CRS returning more than "
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070097 "%d resource descriptors\n", (unsigned long) start,
98 (unsigned long) end, root->name, info->name,
Gary Hadef9cde5f2009-05-27 12:41:44 -070099 max_root_bus_resources);
Gary Hadef9cde5f2009-05-27 12:41:44 -0700100 return AE_OK;
101 }
102
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700103 res = &info->res[info->res_num];
104 res->name = info->name;
105 res->flags = flags;
106 res->start = start;
107 res->end = end;
108 res->child = NULL;
109
Gary Hade62f420f2007-10-03 15:56:51 -0700110 if (insert_resource(root, res)) {
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600111 dev_err(&info->bridge->dev, "can't allocate %pRt\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700112 } else {
113 info->bus->resource[info->res_num] = res;
114 info->res_num++;
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600115 if (addr.translation_offset)
116 dev_info(&info->bridge->dev, "host bridge window: %pRt "
117 "(PCI address [%#llx-%#llx])\n",
118 res, res->start - addr.translation_offset,
119 res->end - addr.translation_offset);
120 else
121 dev_info(&info->bridge->dev,
122 "host bridge window: %pRt\n", res);
Gary Hade62f420f2007-10-03 15:56:51 -0700123 }
124 return AE_OK;
125}
126
127static void
Gary Hade62f420f2007-10-03 15:56:51 -0700128get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800129 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700130{
131 struct pci_root_info info;
132 size_t size;
133
Bjorn Helgaas42887b22009-10-06 15:33:49 -0600134 info.bridge = device;
Gary Hade62f420f2007-10-03 15:56:51 -0700135 info.bus = bus;
136 info.res_num = 0;
137 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
138 &info);
139 if (!info.res_num)
140 return;
141
142 size = sizeof(*info.res) * info.res_num;
143 info.res = kmalloc(size, GFP_KERNEL);
144 if (!info.res)
145 goto res_alloc_fail;
146
Gary Hadecb3576f2008-02-08 14:00:52 -0800147 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700148 if (!info.name)
149 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800150 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700151
152 info.res_num = 0;
153 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
154 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700155
156 return;
157
158name_alloc_fail:
159 kfree(info.res);
160res_alloc_fail:
161 return;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
165{
Andi Kleen69e1a332005-09-12 18:49:24 +0200166 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300167 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800168 int node;
169#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300170 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800171#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200172
Jeff Garzika79e4192007-10-11 16:58:30 -0400173 if (domain && !pci_domains_supported) {
174 printk(KERN_WARNING "PCI: Multiple domains not supported "
175 "(dom %d, bus %d)\n", domain, busnum);
176 return NULL;
177 }
178
Yinghai Lu871d5f82008-02-19 03:20:09 -0800179 node = -1;
180#ifdef CONFIG_ACPI_NUMA
181 pxm = acpi_get_pxm(device->handle);
182 if (pxm >= 0)
183 node = pxm_to_node(pxm);
184 if (node != -1)
185 set_mp_bus_to_node(busnum, node);
186 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800187#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800188 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800189
190 if (node != -1 && !node_online(node))
191 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800192
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300193 /* Allocate per-root-bus (not per bus) arch-specific data.
194 * TODO: leak; this memory is never freed.
195 * It's arguable whether it's worth the trouble to care.
196 */
197 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
198 if (!sd) {
199 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return NULL;
201 }
202
Jeff Garzika79e4192007-10-11 16:58:30 -0400203 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800204 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700205 /*
206 * Maybe the desired pci bus has been already scanned. In such case
207 * it is unnecessary to scan the pci bus with the given domain,busnum.
208 */
209 bus = pci_find_bus(domain, busnum);
210 if (bus) {
211 /*
212 * If the desired bus exits, the content of bus->sysdata will
213 * be replaced by sd.
214 */
215 memcpy(bus->sysdata, sd, sizeof(*sd));
216 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700217 } else {
218 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
219 if (bus) {
220 if (pci_probe & PCI_USE__CRS)
221 get_current_resources(device, busnum, domain,
222 bus);
223 bus->subordinate = pci_scan_child_bus(bus);
224 }
225 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300226
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300227 if (!bus)
228 kfree(sd);
229
Yinghai Ludbb61522008-04-19 01:30:16 -0700230 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200231#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700232 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700233 dev_printk(KERN_DEBUG, &bus->dev,
234 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700235#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700236 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200237#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700238 }
Gary Hade62f420f2007-10-03 15:56:51 -0700239
Andi Kleen69e1a332005-09-12 18:49:24 +0200240 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Robert Richter8dd779b2008-07-02 22:50:29 +0200243int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct pci_dev *dev = NULL;
246
247 if (pcibios_scanned)
248 return 0;
249
250 if (acpi_noirq)
251 return 0;
252
253 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
254 acpi_irq_penalty_init();
255 pcibios_scanned++;
256 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400257 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (pci_routeirq) {
260 /*
261 * PCI IRQ routing is set up by pci_enable_device(), but we
262 * also do it here in case there are still broken drivers that
263 * don't use pci_enable_device().
264 */
265 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800266 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271}