blob: 16c3fda85bbad502317bafedc37159bf9847b278 [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 Lu3d9befd2007-11-17 16:27:01 +010071
Gary Hade62f420f2007-10-03 15:56:51 -070072 status = resource_to_addr(acpi_res, &addr);
73 if (!ACPI_SUCCESS(status))
74 return AE_OK;
75
76 if (addr.resource_type == ACPI_MEMORY_RANGE) {
77 root = &iomem_resource;
78 flags = IORESOURCE_MEM;
79 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
80 flags |= IORESOURCE_PREFETCH;
81 } else if (addr.resource_type == ACPI_IO_RANGE) {
82 root = &ioport_resource;
83 flags = IORESOURCE_IO;
84 } else
85 return AE_OK;
86
87 res = &info->res[info->res_num];
88 res->name = info->name;
89 res->flags = flags;
90 res->start = addr.minimum + addr.translation_offset;
91 res->end = res->start + addr.address_length - 1;
92 res->child = NULL;
93
Gary Hadef9cde5f2009-05-27 12:41:44 -070094 if (bus_has_transparent_bridge(info->bus))
95 max_root_bus_resources -= 3;
96 if (info->res_num >= max_root_bus_resources) {
97 printk(KERN_WARNING "PCI: Failed to allocate 0x%lx-0x%lx "
98 "from %s for %s due to _CRS returning more than "
99 "%d resource descriptors\n", (unsigned long) res->start,
100 (unsigned long) res->end, root->name, info->name,
101 max_root_bus_resources);
102 info->res_num++;
103 return AE_OK;
104 }
105
Gary Hade62f420f2007-10-03 15:56:51 -0700106 if (insert_resource(root, res)) {
107 printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx "
108 "from %s for %s\n", (unsigned long) res->start,
109 (unsigned long) res->end, root->name, info->name);
110 } else {
111 info->bus->resource[info->res_num] = res;
112 info->res_num++;
113 }
114 return AE_OK;
115}
116
117static void
118adjust_transparent_bridge_resources(struct pci_bus *bus)
119{
120 struct pci_dev *dev;
121
122 list_for_each_entry(dev, &bus->devices, bus_list) {
123 int i;
124 u16 class = dev->class >> 8;
125
126 if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) {
127 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
128 dev->subordinate->resource[i] =
129 dev->bus->resource[i - 3];
130 }
131 }
132}
133
134static void
135get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800136 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700137{
138 struct pci_root_info info;
139 size_t size;
140
141 info.bus = bus;
142 info.res_num = 0;
143 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
144 &info);
145 if (!info.res_num)
146 return;
147
148 size = sizeof(*info.res) * info.res_num;
149 info.res = kmalloc(size, GFP_KERNEL);
150 if (!info.res)
151 goto res_alloc_fail;
152
Gary Hadecb3576f2008-02-08 14:00:52 -0800153 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700154 if (!info.name)
155 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800156 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700157
158 info.res_num = 0;
159 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
160 &info);
161 if (info.res_num)
162 adjust_transparent_bridge_resources(bus);
163
164 return;
165
166name_alloc_fail:
167 kfree(info.res);
168res_alloc_fail:
169 return;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
173{
Andi Kleen69e1a332005-09-12 18:49:24 +0200174 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300175 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800176 int node;
177#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300178 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800179#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200180
Jeff Garzika79e4192007-10-11 16:58:30 -0400181 if (domain && !pci_domains_supported) {
182 printk(KERN_WARNING "PCI: Multiple domains not supported "
183 "(dom %d, bus %d)\n", domain, busnum);
184 return NULL;
185 }
186
Yinghai Lu871d5f82008-02-19 03:20:09 -0800187 node = -1;
188#ifdef CONFIG_ACPI_NUMA
189 pxm = acpi_get_pxm(device->handle);
190 if (pxm >= 0)
191 node = pxm_to_node(pxm);
192 if (node != -1)
193 set_mp_bus_to_node(busnum, node);
194 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800195#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800196 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800197
198 if (node != -1 && !node_online(node))
199 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800200
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300201 /* Allocate per-root-bus (not per bus) arch-specific data.
202 * TODO: leak; this memory is never freed.
203 * It's arguable whether it's worth the trouble to care.
204 */
205 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
206 if (!sd) {
207 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return NULL;
209 }
210
Jeff Garzika79e4192007-10-11 16:58:30 -0400211 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800212 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700213 /*
214 * Maybe the desired pci bus has been already scanned. In such case
215 * it is unnecessary to scan the pci bus with the given domain,busnum.
216 */
217 bus = pci_find_bus(domain, busnum);
218 if (bus) {
219 /*
220 * If the desired bus exits, the content of bus->sysdata will
221 * be replaced by sd.
222 */
223 memcpy(bus->sysdata, sd, sizeof(*sd));
224 kfree(sd);
225 } else
226 bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300227
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300228 if (!bus)
229 kfree(sd);
230
Yinghai Ludbb61522008-04-19 01:30:16 -0700231 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200232#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700233 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700234 dev_printk(KERN_DEBUG, &bus->dev,
235 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700236#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700237 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200238#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700239 }
Gary Hade62f420f2007-10-03 15:56:51 -0700240
Jesse Barnes9e9f46c2009-06-11 10:58:28 -0700241 if (bus && !(pci_probe & PCI_NO_ROOT_CRS))
Gary Hadecb3576f2008-02-08 14:00:52 -0800242 get_current_resources(device, busnum, domain, bus);
Andi Kleen69e1a332005-09-12 18:49:24 +0200243 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Robert Richter8dd779b2008-07-02 22:50:29 +0200246int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct pci_dev *dev = NULL;
249
250 if (pcibios_scanned)
251 return 0;
252
253 if (acpi_noirq)
254 return 0;
255
256 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
257 acpi_irq_penalty_init();
258 pcibios_scanned++;
259 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400260 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (pci_routeirq) {
263 /*
264 * PCI IRQ routing is set up by pci_enable_device(), but we
265 * also do it here in case there are still broken drivers that
266 * don't use pci_enable_device().
267 */
268 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800269 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274}