Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / arch / x86 / pci / acpi.c
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/pci_x86.h>
8
9 struct 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
17 static acpi_status
18 resource_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
34 static acpi_status
35 count_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
47 static int
48 bus_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
61 static acpi_status
62 setup_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;
70         int max_root_bus_resources = PCI_BUS_NUM_RESOURCES;
71
72         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
94         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
106         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
117 static void
118 adjust_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
134 static void
135 get_current_resources(struct acpi_device *device, int busnum,
136                         int domain, struct pci_bus *bus)
137 {
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
153         info.name = kmalloc(16, GFP_KERNEL);
154         if (!info.name)
155                 goto name_alloc_fail;
156         sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
157
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
166 name_alloc_fail:
167         kfree(info.res);
168 res_alloc_fail:
169         return;
170 }
171
172 struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
173 {
174         struct pci_bus *bus;
175         struct pci_sysdata *sd;
176         int node;
177 #ifdef CONFIG_ACPI_NUMA
178         int pxm;
179 #endif
180
181         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
187         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
195 #endif
196                 node = get_mp_bus_to_node(busnum);
197
198         if (node != -1 && !node_online(node))
199                 node = -1;
200
201         /* 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);
208                 return NULL;
209         }
210
211         sd->domain = domain;
212         sd->node = node;
213         /*
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);
227
228         if (!bus)
229                 kfree(sd);
230
231         if (bus && node != -1) {
232 #ifdef CONFIG_ACPI_NUMA
233                 if (pxm >= 0)
234                         dev_printk(KERN_DEBUG, &bus->dev,
235                                    "on NUMA node %d (pxm %d)\n", node, pxm);
236 #else
237                 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
238 #endif
239         }
240
241         if (bus && !(pci_probe & PCI_NO_ROOT_CRS))
242                 get_current_resources(device, busnum, domain, bus);
243         return bus;
244 }
245
246 int __init pci_acpi_init(void)
247 {
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;
260         pcibios_disable_irq = acpi_pci_irq_disable;
261
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");
269                 for_each_pci_dev(dev)
270                         acpi_pci_irq_enable(dev);
271         }
272
273         return 0;
274 }