blob: 1014eb4bfc37ac15cec9e3b24b3f980a1a52e7d9 [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 {
10 char *name;
11 unsigned int res_num;
12 struct resource *res;
13 struct pci_bus *bus;
14 int busnum;
15};
16
17static acpi_status
18resource_to_addr(struct acpi_resource *resource,
19 struct acpi_resource_address64 *addr)
20{
21 acpi_status status;
22
23 status = acpi_resource_to_address64(resource, addr);
24 if (ACPI_SUCCESS(status) &&
25 (addr->resource_type == ACPI_MEMORY_RANGE ||
26 addr->resource_type == ACPI_IO_RANGE) &&
27 addr->address_length > 0 &&
28 addr->producer_consumer == ACPI_PRODUCER) {
29 return AE_OK;
30 }
31 return AE_ERROR;
32}
33
34static acpi_status
35count_resource(struct acpi_resource *acpi_res, void *data)
36{
37 struct pci_root_info *info = data;
38 struct acpi_resource_address64 addr;
39 acpi_status status;
40
41 status = resource_to_addr(acpi_res, &addr);
42 if (ACPI_SUCCESS(status))
43 info->res_num++;
44 return AE_OK;
45}
46
Gary Hadef9cde5f2009-05-27 12:41:44 -070047static int
48bus_has_transparent_bridge(struct pci_bus *bus)
49{
50 struct pci_dev *dev;
51
52 list_for_each_entry(dev, &bus->devices, bus_list) {
53 u16 class = dev->class >> 8;
54
55 if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent)
56 return true;
57 }
58 return false;
59}
60
Gary Hade62f420f2007-10-03 15:56:51 -070061static acpi_status
62setup_resource(struct acpi_resource *acpi_res, void *data)
63{
64 struct pci_root_info *info = data;
65 struct resource *res;
66 struct acpi_resource_address64 addr;
67 acpi_status status;
68 unsigned long flags;
69 struct resource *root;
Gary Hadef9cde5f2009-05-27 12:41:44 -070070 int max_root_bus_resources = PCI_BUS_NUM_RESOURCES;
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070071 u64 start, end;
72
73 if (bus_has_transparent_bridge(info->bus))
74 max_root_bus_resources -= 3;
Yinghai Lu3d9befd2007-11-17 16:27:01 +010075
Gary Hade62f420f2007-10-03 15:56:51 -070076 status = resource_to_addr(acpi_res, &addr);
77 if (!ACPI_SUCCESS(status))
78 return AE_OK;
79
80 if (addr.resource_type == ACPI_MEMORY_RANGE) {
81 root = &iomem_resource;
82 flags = IORESOURCE_MEM;
83 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
84 flags |= IORESOURCE_PREFETCH;
85 } else if (addr.resource_type == ACPI_IO_RANGE) {
86 root = &ioport_resource;
87 flags = IORESOURCE_IO;
88 } else
89 return AE_OK;
90
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070091 start = addr.minimum + addr.translation_offset;
92 end = start + addr.address_length - 1;
Gary Hadef9cde5f2009-05-27 12:41:44 -070093 if (info->res_num >= max_root_bus_resources) {
94 printk(KERN_WARNING "PCI: Failed to allocate 0x%lx-0x%lx "
95 "from %s for %s due to _CRS returning more than "
Yinghai Lu2cdb3f12009-06-24 19:01:19 -070096 "%d resource descriptors\n", (unsigned long) start,
97 (unsigned long) end, root->name, info->name,
Gary Hadef9cde5f2009-05-27 12:41:44 -070098 max_root_bus_resources);
Gary Hadef9cde5f2009-05-27 12:41:44 -070099 return AE_OK;
100 }
101
Yinghai Lu2cdb3f12009-06-24 19:01:19 -0700102 res = &info->res[info->res_num];
103 res->name = info->name;
104 res->flags = flags;
105 res->start = start;
106 res->end = end;
107 res->child = NULL;
108
Gary Hade62f420f2007-10-03 15:56:51 -0700109 if (insert_resource(root, res)) {
110 printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx "
111 "from %s for %s\n", (unsigned long) res->start,
112 (unsigned long) res->end, root->name, info->name);
113 } else {
114 info->bus->resource[info->res_num] = res;
115 info->res_num++;
116 }
117 return AE_OK;
118}
119
120static void
Gary Hade62f420f2007-10-03 15:56:51 -0700121get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800122 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700123{
124 struct pci_root_info info;
125 size_t size;
126
127 info.bus = bus;
128 info.res_num = 0;
129 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
130 &info);
131 if (!info.res_num)
132 return;
133
134 size = sizeof(*info.res) * info.res_num;
135 info.res = kmalloc(size, GFP_KERNEL);
136 if (!info.res)
137 goto res_alloc_fail;
138
Gary Hadecb3576f2008-02-08 14:00:52 -0800139 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700140 if (!info.name)
141 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800142 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700143
144 info.res_num = 0;
145 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
146 &info);
Gary Hade62f420f2007-10-03 15:56:51 -0700147
148 return;
149
150name_alloc_fail:
151 kfree(info.res);
152res_alloc_fail:
153 return;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
157{
Andi Kleen69e1a332005-09-12 18:49:24 +0200158 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300159 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800160 int node;
161#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300162 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800163#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200164
Jeff Garzika79e4192007-10-11 16:58:30 -0400165 if (domain && !pci_domains_supported) {
166 printk(KERN_WARNING "PCI: Multiple domains not supported "
167 "(dom %d, bus %d)\n", domain, busnum);
168 return NULL;
169 }
170
Yinghai Lu871d5f82008-02-19 03:20:09 -0800171 node = -1;
172#ifdef CONFIG_ACPI_NUMA
173 pxm = acpi_get_pxm(device->handle);
174 if (pxm >= 0)
175 node = pxm_to_node(pxm);
176 if (node != -1)
177 set_mp_bus_to_node(busnum, node);
178 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800179#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800180 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800181
182 if (node != -1 && !node_online(node))
183 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800184
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300185 /* Allocate per-root-bus (not per bus) arch-specific data.
186 * TODO: leak; this memory is never freed.
187 * It's arguable whether it's worth the trouble to care.
188 */
189 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
190 if (!sd) {
191 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return NULL;
193 }
194
Jeff Garzika79e4192007-10-11 16:58:30 -0400195 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800196 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700197 /*
198 * Maybe the desired pci bus has been already scanned. In such case
199 * it is unnecessary to scan the pci bus with the given domain,busnum.
200 */
201 bus = pci_find_bus(domain, busnum);
202 if (bus) {
203 /*
204 * If the desired bus exits, the content of bus->sysdata will
205 * be replaced by sd.
206 */
207 memcpy(bus->sysdata, sd, sizeof(*sd));
208 kfree(sd);
Yinghai Lu626fdfe2009-06-24 20:00:12 -0700209 } else {
210 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
211 if (bus) {
212 if (pci_probe & PCI_USE__CRS)
213 get_current_resources(device, busnum, domain,
214 bus);
215 bus->subordinate = pci_scan_child_bus(bus);
216 }
217 }
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300218
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300219 if (!bus)
220 kfree(sd);
221
Yinghai Ludbb61522008-04-19 01:30:16 -0700222 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200223#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700224 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700225 dev_printk(KERN_DEBUG, &bus->dev,
226 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700227#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700228 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200229#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700230 }
Gary Hade62f420f2007-10-03 15:56:51 -0700231
Andi Kleen69e1a332005-09-12 18:49:24 +0200232 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Robert Richter8dd779b2008-07-02 22:50:29 +0200235int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct pci_dev *dev = NULL;
238
239 if (pcibios_scanned)
240 return 0;
241
242 if (acpi_noirq)
243 return 0;
244
245 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
246 acpi_irq_penalty_init();
247 pcibios_scanned++;
248 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400249 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (pci_routeirq) {
252 /*
253 * PCI IRQ routing is set up by pci_enable_device(), but we
254 * also do it here in case there are still broken drivers that
255 * don't use pci_enable_device().
256 */
257 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800258 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}