x86/PCI: ACPI based PCI gap calculation
[linux-2.6/mini2440.git] / arch / x86 / pci / acpi.c
blobd1ffb570917430da779cbef859eab4a7159b3a3d
1 #include <linux/pci.h>
2 #include <linux/acpi.h>
3 #include <linux/init.h>
4 #include <linux/irq.h>
5 #include <linux/dmi.h>
6 #include <asm/numa.h>
7 #include <asm/e820.h>
8 #include "pci.h"
10 struct pci_root_info {
11 char *name;
12 unsigned int res_num;
13 struct resource *res;
14 struct pci_bus *bus;
15 int busnum;
18 struct gap_info {
19 unsigned long gapstart;
20 unsigned long gapsize;
23 static acpi_status
24 resource_to_addr(struct acpi_resource *resource,
25 struct acpi_resource_address64 *addr)
27 acpi_status status;
29 status = acpi_resource_to_address64(resource, addr);
30 if (ACPI_SUCCESS(status) &&
31 (addr->resource_type == ACPI_MEMORY_RANGE ||
32 addr->resource_type == ACPI_IO_RANGE) &&
33 addr->address_length > 0 &&
34 addr->producer_consumer == ACPI_PRODUCER) {
35 return AE_OK;
37 return AE_ERROR;
40 static acpi_status
41 count_resource(struct acpi_resource *acpi_res, void *data)
43 struct pci_root_info *info = data;
44 struct acpi_resource_address64 addr;
45 acpi_status status;
47 if (info->res_num >= PCI_BUS_NUM_RESOURCES)
48 return AE_OK;
50 status = resource_to_addr(acpi_res, &addr);
51 if (ACPI_SUCCESS(status))
52 info->res_num++;
53 return AE_OK;
56 static acpi_status
57 setup_resource(struct acpi_resource *acpi_res, void *data)
59 struct pci_root_info *info = data;
60 struct resource *res;
61 struct acpi_resource_address64 addr;
62 acpi_status status;
63 unsigned long flags;
64 struct resource *root;
66 if (info->res_num >= PCI_BUS_NUM_RESOURCES)
67 return AE_OK;
69 status = resource_to_addr(acpi_res, &addr);
70 if (!ACPI_SUCCESS(status))
71 return AE_OK;
73 if (addr.resource_type == ACPI_MEMORY_RANGE) {
74 root = &iomem_resource;
75 flags = IORESOURCE_MEM;
76 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
77 flags |= IORESOURCE_PREFETCH;
78 } else if (addr.resource_type == ACPI_IO_RANGE) {
79 root = &ioport_resource;
80 flags = IORESOURCE_IO;
81 } else
82 return AE_OK;
84 res = &info->res[info->res_num];
85 res->name = info->name;
86 res->flags = flags;
87 res->start = addr.minimum + addr.translation_offset;
88 res->end = res->start + addr.address_length - 1;
89 res->child = NULL;
91 if (insert_resource(root, res)) {
92 printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx "
93 "from %s for %s\n", (unsigned long) res->start,
94 (unsigned long) res->end, root->name, info->name);
95 } else {
96 info->bus->resource[info->res_num] = res;
97 info->res_num++;
99 return AE_OK;
102 static void
103 adjust_transparent_bridge_resources(struct pci_bus *bus)
105 struct pci_dev *dev;
107 list_for_each_entry(dev, &bus->devices, bus_list) {
108 int i;
109 u16 class = dev->class >> 8;
111 if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) {
112 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
113 dev->subordinate->resource[i] =
114 dev->bus->resource[i - 3];
119 static acpi_status search_gap(struct acpi_resource *resource, void *data)
121 struct acpi_resource_address64 addr;
122 acpi_status status;
123 struct gap_info *gap = data;
124 unsigned long long start_addr, end_addr;
126 status = resource_to_addr(resource, &addr);
127 if (ACPI_SUCCESS(status) &&
128 addr.resource_type == ACPI_MEMORY_RANGE &&
129 addr.address_length > gap->gapsize) {
130 start_addr = addr.minimum + addr.translation_offset;
132 * We want space only in the 32bit address range
134 if (start_addr < UINT_MAX) {
135 end_addr = start_addr + addr.address_length;
136 e820_search_gap(&gap->gapstart, &gap->gapsize,
137 start_addr, end_addr);
141 return AE_OK;
145 * Search for a hole in the 32 bit address space for PCI to assign MMIO
146 * resources, for hotplug or unconfigured resources.
147 * We query the CRS object of the PCI root device to look for possible producer
148 * resources in the tree and consider these while calulating the start address
149 * for this hole.
151 static void pci_setup_gap(acpi_handle *handle)
153 struct gap_info gap;
154 acpi_status status;
156 gap.gapstart = 0;
157 gap.gapsize = 0x400000;
159 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
160 search_gap, &gap);
162 if (ACPI_SUCCESS(status)) {
163 unsigned long round;
165 if (!gap.gapstart) {
166 printk(KERN_ERR "ACPI: Warning: Cannot find a gap "
167 "in the 32bit address range for PCI\n"
168 "ACPI: PCI devices may collide with "
169 "hotpluggable memory address range\n");
172 * Round the gapstart, uses the same logic as in
173 * e820_gap_setup
175 round = 0x100000;
176 while ((gap.gapsize >> 4) > round)
177 round += round;
178 /* Fun with two's complement */
179 pci_mem_start = (gap.gapstart + round) & -round;
181 printk(KERN_INFO "ACPI: PCI resources should "
182 "start at %lx (gap: %lx:%lx)\n",
183 pci_mem_start, gap.gapstart, gap.gapsize);
184 } else {
185 printk(KERN_ERR "ACPI: Error while searching for gap in "
186 "the 32bit address range for PCI\n");
191 static void
192 get_current_resources(struct acpi_device *device, int busnum,
193 int domain, struct pci_bus *bus)
195 struct pci_root_info info;
196 size_t size;
198 info.bus = bus;
199 info.res_num = 0;
200 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
201 &info);
202 if (!info.res_num)
203 return;
205 size = sizeof(*info.res) * info.res_num;
206 info.res = kmalloc(size, GFP_KERNEL);
207 if (!info.res)
208 goto res_alloc_fail;
210 info.name = kmalloc(16, GFP_KERNEL);
211 if (!info.name)
212 goto name_alloc_fail;
213 sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
215 info.res_num = 0;
216 acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
217 &info);
218 if (info.res_num)
219 adjust_transparent_bridge_resources(bus);
221 return;
223 name_alloc_fail:
224 kfree(info.res);
225 res_alloc_fail:
226 return;
229 struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
231 struct pci_bus *bus;
232 struct pci_sysdata *sd;
233 int node;
234 #ifdef CONFIG_ACPI_NUMA
235 int pxm;
236 #endif
238 if (domain && !pci_domains_supported) {
239 printk(KERN_WARNING "PCI: Multiple domains not supported "
240 "(dom %d, bus %d)\n", domain, busnum);
241 return NULL;
244 node = -1;
245 #ifdef CONFIG_ACPI_NUMA
246 pxm = acpi_get_pxm(device->handle);
247 if (pxm >= 0)
248 node = pxm_to_node(pxm);
249 if (node != -1)
250 set_mp_bus_to_node(busnum, node);
251 else
252 node = get_mp_bus_to_node(busnum);
253 #endif
255 /* Allocate per-root-bus (not per bus) arch-specific data.
256 * TODO: leak; this memory is never freed.
257 * It's arguable whether it's worth the trouble to care.
259 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
260 if (!sd) {
261 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
262 return NULL;
265 sd->domain = domain;
266 sd->node = node;
268 * Maybe the desired pci bus has been already scanned. In such case
269 * it is unnecessary to scan the pci bus with the given domain,busnum.
271 bus = pci_find_bus(domain, busnum);
272 if (bus) {
274 * If the desired bus exits, the content of bus->sysdata will
275 * be replaced by sd.
277 memcpy(bus->sysdata, sd, sizeof(*sd));
278 kfree(sd);
279 } else
280 bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
282 if (!bus)
283 kfree(sd);
285 #ifdef CONFIG_ACPI_NUMA
286 if (bus) {
287 if (pxm >= 0) {
288 printk(KERN_DEBUG "bus %02x -> pxm %d -> node %d\n",
289 busnum, pxm, pxm_to_node(pxm));
292 #endif
294 if (bus && (pci_probe & PCI_USE__CRS))
295 get_current_resources(device, busnum, domain, bus);
297 pci_setup_gap(device->handle);
298 return bus;
301 extern int pci_routeirq;
302 static int __init pci_acpi_init(void)
304 struct pci_dev *dev = NULL;
306 if (pcibios_scanned)
307 return 0;
309 if (acpi_noirq)
310 return 0;
312 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
313 acpi_irq_penalty_init();
314 pcibios_scanned++;
315 pcibios_enable_irq = acpi_pci_irq_enable;
316 pcibios_disable_irq = acpi_pci_irq_disable;
318 if (pci_routeirq) {
320 * PCI IRQ routing is set up by pci_enable_device(), but we
321 * also do it here in case there are still broken drivers that
322 * don't use pci_enable_device().
324 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
325 for_each_pci_dev(dev)
326 acpi_pci_irq_enable(dev);
329 #ifdef CONFIG_X86_IO_APIC
330 if (acpi_ioapic)
331 print_IO_APIC();
332 #endif
334 return 0;
336 subsys_initcall(pci_acpi_init);