blob: 8bf152910eb027c302d146f28335e3d6d0091a05 [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
121adjust_transparent_bridge_resources(struct pci_bus *bus)
122{
123 struct pci_dev *dev;
124
125 list_for_each_entry(dev, &bus->devices, bus_list) {
126 int i;
127 u16 class = dev->class >> 8;
128
129 if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) {
130 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
131 dev->subordinate->resource[i] =
132 dev->bus->resource[i - 3];
133 }
134 }
135}
136
137static void
138get_current_resources(struct acpi_device *device, int busnum,
Gary Hadecb3576f2008-02-08 14:00:52 -0800139 int domain, struct pci_bus *bus)
Gary Hade62f420f2007-10-03 15:56:51 -0700140{
141 struct pci_root_info info;
142 size_t size;
143
144 info.bus = bus;
145 info.res_num = 0;
146 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
147 &info);
148 if (!info.res_num)
149 return;
150
151 size = sizeof(*info.res) * info.res_num;
152 info.res = kmalloc(size, GFP_KERNEL);
153 if (!info.res)
154 goto res_alloc_fail;
155
Gary Hadecb3576f2008-02-08 14:00:52 -0800156 info.name = kmalloc(16, GFP_KERNEL);
Gary Hade62f420f2007-10-03 15:56:51 -0700157 if (!info.name)
158 goto name_alloc_fail;
Gary Hadecb3576f2008-02-08 14:00:52 -0800159 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
Gary Hade62f420f2007-10-03 15:56:51 -0700160
161 info.res_num = 0;
162 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
163 &info);
164 if (info.res_num)
165 adjust_transparent_bridge_resources(bus);
166
167 return;
168
169name_alloc_fail:
170 kfree(info.res);
171res_alloc_fail:
172 return;
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
176{
Andi Kleen69e1a332005-09-12 18:49:24 +0200177 struct pci_bus *bus;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300178 struct pci_sysdata *sd;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800179 int node;
180#ifdef CONFIG_ACPI_NUMA
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300181 int pxm;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800182#endif
Andi Kleen69e1a332005-09-12 18:49:24 +0200183
Jeff Garzika79e4192007-10-11 16:58:30 -0400184 if (domain && !pci_domains_supported) {
185 printk(KERN_WARNING "PCI: Multiple domains not supported "
186 "(dom %d, bus %d)\n", domain, busnum);
187 return NULL;
188 }
189
Yinghai Lu871d5f82008-02-19 03:20:09 -0800190 node = -1;
191#ifdef CONFIG_ACPI_NUMA
192 pxm = acpi_get_pxm(device->handle);
193 if (pxm >= 0)
194 node = pxm_to_node(pxm);
195 if (node != -1)
196 set_mp_bus_to_node(busnum, node);
197 else
Yinghai Lu871d5f82008-02-19 03:20:09 -0800198#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800199 node = get_mp_bus_to_node(busnum);
Yinghai Lub755de82008-02-20 12:41:52 -0800200
201 if (node != -1 && !node_online(node))
202 node = -1;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800203
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300204 /* Allocate per-root-bus (not per bus) arch-specific data.
205 * TODO: leak; this memory is never freed.
206 * It's arguable whether it's worth the trouble to care.
207 */
208 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
209 if (!sd) {
210 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return NULL;
212 }
213
Jeff Garzika79e4192007-10-11 16:58:30 -0400214 sd->domain = domain;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800215 sd->node = node;
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700216 /*
217 * Maybe the desired pci bus has been already scanned. In such case
218 * it is unnecessary to scan the pci bus with the given domain,busnum.
219 */
220 bus = pci_find_bus(domain, busnum);
221 if (bus) {
222 /*
223 * If the desired bus exits, the content of bus->sysdata will
224 * be replaced by sd.
225 */
226 memcpy(bus->sysdata, sd, sizeof(*sd));
227 kfree(sd);
228 } else
229 bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300230
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300231 if (!bus)
232 kfree(sd);
233
Yinghai Ludbb61522008-04-19 01:30:16 -0700234 if (bus && node != -1) {
Andi Kleen69e1a332005-09-12 18:49:24 +0200235#ifdef CONFIG_ACPI_NUMA
Yinghai Ludbb61522008-04-19 01:30:16 -0700236 if (pxm >= 0)
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700237 dev_printk(KERN_DEBUG, &bus->dev,
238 "on NUMA node %d (pxm %d)\n", node, pxm);
Yinghai Ludbb61522008-04-19 01:30:16 -0700239#else
Bjorn Helgaas2b8c2ef2008-12-18 16:34:51 -0700240 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
Andi Kleen69e1a332005-09-12 18:49:24 +0200241#endif
Yinghai Ludbb61522008-04-19 01:30:16 -0700242 }
Gary Hade62f420f2007-10-03 15:56:51 -0700243
Linus Torvalds236e9462009-06-24 16:23:03 -0700244 if (bus && (pci_probe & PCI_USE__CRS))
Gary Hadecb3576f2008-02-08 14:00:52 -0800245 get_current_resources(device, busnum, domain, bus);
Andi Kleen69e1a332005-09-12 18:49:24 +0200246 return bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Robert Richter8dd779b2008-07-02 22:50:29 +0200249int __init pci_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct pci_dev *dev = NULL;
252
253 if (pcibios_scanned)
254 return 0;
255
256 if (acpi_noirq)
257 return 0;
258
259 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
260 acpi_irq_penalty_init();
261 pcibios_scanned++;
262 pcibios_enable_irq = acpi_pci_irq_enable;
David Shaohua Li87bec662005-07-27 23:02:00 -0400263 pcibios_disable_irq = acpi_pci_irq_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (pci_routeirq) {
266 /*
267 * PCI IRQ routing is set up by pci_enable_device(), but we
268 * also do it here in case there are still broken drivers that
269 * don't use pci_enable_device().
270 */
271 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
Hanna Linderfb37fb92005-11-06 23:39:36 -0800272 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 acpi_pci_irq_enable(dev);
Bjorn Helgaas657472e92008-02-18 09:44:13 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}