Merge branch 'fix/misc' into for-linus
[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 <linux/slab.h>
7 #include <asm/numa.h>
8 #include <asm/pci_x86.h>
9
10 struct pci_root_info {
11         struct acpi_device *bridge;
12         char *name;
13         unsigned int res_num;
14         struct resource *res;
15         struct pci_bus *bus;
16         int busnum;
17 };
18
19 static bool pci_use_crs = true;
20
21 static int __init set_use_crs(const struct dmi_system_id *id)
22 {
23         pci_use_crs = true;
24         return 0;
25 }
26
27 static const struct dmi_system_id pci_use_crs_table[] __initconst = {
28         /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
29         {
30                 .callback = set_use_crs,
31                 .ident = "IBM System x3800",
32                 .matches = {
33                         DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
34                         DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
35                 },
36         },
37         {}
38 };
39
40 void __init pci_acpi_crs_quirks(void)
41 {
42         int year;
43
44         if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
45                 pci_use_crs = false;
46
47         dmi_check_system(pci_use_crs_table);
48
49         /*
50          * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
51          * takes precedence over anything we figured out above.
52          */
53         if (pci_probe & PCI_ROOT_NO_CRS)
54                 pci_use_crs = false;
55         else if (pci_probe & PCI_USE__CRS)
56                 pci_use_crs = true;
57
58         printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
59                "if necessary, use \"pci=%s\" and report a bug\n",
60                pci_use_crs ? "Using" : "Ignoring",
61                pci_use_crs ? "nocrs" : "use_crs");
62 }
63
64 static acpi_status
65 resource_to_addr(struct acpi_resource *resource,
66                         struct acpi_resource_address64 *addr)
67 {
68         acpi_status status;
69
70         status = acpi_resource_to_address64(resource, addr);
71         if (ACPI_SUCCESS(status) &&
72             (addr->resource_type == ACPI_MEMORY_RANGE ||
73             addr->resource_type == ACPI_IO_RANGE) &&
74             addr->address_length > 0 &&
75             addr->producer_consumer == ACPI_PRODUCER) {
76                 return AE_OK;
77         }
78         return AE_ERROR;
79 }
80
81 static acpi_status
82 count_resource(struct acpi_resource *acpi_res, void *data)
83 {
84         struct pci_root_info *info = data;
85         struct acpi_resource_address64 addr;
86         acpi_status status;
87
88         status = resource_to_addr(acpi_res, &addr);
89         if (ACPI_SUCCESS(status))
90                 info->res_num++;
91         return AE_OK;
92 }
93
94 static void
95 align_resource(struct acpi_device *bridge, struct resource *res)
96 {
97         int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
98
99         /*
100          * Host bridge windows are not BARs, but the decoders on the PCI side
101          * that claim this address space have starting alignment and length
102          * constraints, so fix any obvious BIOS goofs.
103          */
104         if (!IS_ALIGNED(res->start, align)) {
105                 dev_printk(KERN_DEBUG, &bridge->dev,
106                            "host bridge window %pR invalid; "
107                            "aligning start to %d-byte boundary\n", res, align);
108                 res->start &= ~(align - 1);
109         }
110         if (!IS_ALIGNED(res->end + 1, align)) {
111                 dev_printk(KERN_DEBUG, &bridge->dev,
112                            "host bridge window %pR invalid; "
113                            "aligning end to %d-byte boundary\n", res, align);
114                 res->end = ALIGN(res->end, align) - 1;
115         }
116 }
117
118 static acpi_status
119 setup_resource(struct acpi_resource *acpi_res, void *data)
120 {
121         struct pci_root_info *info = data;
122         struct resource *res;
123         struct acpi_resource_address64 addr;
124         acpi_status status;
125         unsigned long flags;
126         struct resource *root, *conflict;
127         u64 start, end, max_len;
128
129         status = resource_to_addr(acpi_res, &addr);
130         if (!ACPI_SUCCESS(status))
131                 return AE_OK;
132
133         if (addr.resource_type == ACPI_MEMORY_RANGE) {
134                 root = &iomem_resource;
135                 flags = IORESOURCE_MEM;
136                 if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
137                         flags |= IORESOURCE_PREFETCH;
138         } else if (addr.resource_type == ACPI_IO_RANGE) {
139                 root = &ioport_resource;
140                 flags = IORESOURCE_IO;
141         } else
142                 return AE_OK;
143
144         max_len = addr.maximum - addr.minimum + 1;
145         if (addr.address_length > max_len) {
146                 dev_printk(KERN_DEBUG, &info->bridge->dev,
147                            "host bridge window length %#llx doesn't fit in "
148                            "%#llx-%#llx, trimming\n",
149                            (unsigned long long) addr.address_length,
150                            (unsigned long long) addr.minimum,
151                            (unsigned long long) addr.maximum);
152                 addr.address_length = max_len;
153         }
154
155         start = addr.minimum + addr.translation_offset;
156         end = start + addr.address_length - 1;
157
158         res = &info->res[info->res_num];
159         res->name = info->name;
160         res->flags = flags;
161         res->start = start;
162         res->end = end;
163         res->child = NULL;
164         align_resource(info->bridge, res);
165
166         if (!pci_use_crs) {
167                 dev_printk(KERN_DEBUG, &info->bridge->dev,
168                            "host bridge window %pR (ignored)\n", res);
169                 return AE_OK;
170         }
171
172         conflict = insert_resource_conflict(root, res);
173         if (conflict) {
174                 dev_err(&info->bridge->dev,
175                         "address space collision: host bridge window %pR "
176                         "conflicts with %s %pR\n",
177                         res, conflict->name, conflict);
178         } else {
179                 pci_bus_add_resource(info->bus, res, 0);
180                 info->res_num++;
181                 if (addr.translation_offset)
182                         dev_info(&info->bridge->dev, "host bridge window %pR "
183                                  "(PCI address [%#llx-%#llx])\n",
184                                  res, res->start - addr.translation_offset,
185                                  res->end - addr.translation_offset);
186                 else
187                         dev_info(&info->bridge->dev,
188                                  "host bridge window %pR\n", res);
189         }
190         return AE_OK;
191 }
192
193 static void
194 get_current_resources(struct acpi_device *device, int busnum,
195                         int domain, struct pci_bus *bus)
196 {
197         struct pci_root_info info;
198         size_t size;
199
200         if (pci_use_crs)
201                 pci_bus_remove_resources(bus);
202
203         info.bridge = device;
204         info.bus = bus;
205         info.res_num = 0;
206         acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
207                                 &info);
208         if (!info.res_num)
209                 return;
210
211         size = sizeof(*info.res) * info.res_num;
212         info.res = kmalloc(size, GFP_KERNEL);
213         if (!info.res)
214                 goto res_alloc_fail;
215
216         info.name = kmalloc(16, GFP_KERNEL);
217         if (!info.name)
218                 goto name_alloc_fail;
219         sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
220
221         info.res_num = 0;
222         acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
223                                 &info);
224
225         return;
226
227 name_alloc_fail:
228         kfree(info.res);
229 res_alloc_fail:
230         return;
231 }
232
233 struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
234 {
235         struct pci_bus *bus;
236         struct pci_sysdata *sd;
237         int node;
238 #ifdef CONFIG_ACPI_NUMA
239         int pxm;
240 #endif
241
242         if (domain && !pci_domains_supported) {
243                 printk(KERN_WARNING "pci_bus %04x:%02x: "
244                        "ignored (multiple domains not supported)\n",
245                        domain, busnum);
246                 return NULL;
247         }
248
249         node = -1;
250 #ifdef CONFIG_ACPI_NUMA
251         pxm = acpi_get_pxm(device->handle);
252         if (pxm >= 0)
253                 node = pxm_to_node(pxm);
254         if (node != -1)
255                 set_mp_bus_to_node(busnum, node);
256         else
257 #endif
258                 node = get_mp_bus_to_node(busnum);
259
260         if (node != -1 && !node_online(node))
261                 node = -1;
262
263         /* Allocate per-root-bus (not per bus) arch-specific data.
264          * TODO: leak; this memory is never freed.
265          * It's arguable whether it's worth the trouble to care.
266          */
267         sd = kzalloc(sizeof(*sd), GFP_KERNEL);
268         if (!sd) {
269                 printk(KERN_WARNING "pci_bus %04x:%02x: "
270                        "ignored (out of memory)\n", domain, busnum);
271                 return NULL;
272         }
273
274         sd->domain = domain;
275         sd->node = node;
276         /*
277          * Maybe the desired pci bus has been already scanned. In such case
278          * it is unnecessary to scan the pci bus with the given domain,busnum.
279          */
280         bus = pci_find_bus(domain, busnum);
281         if (bus) {
282                 /*
283                  * If the desired bus exits, the content of bus->sysdata will
284                  * be replaced by sd.
285                  */
286                 memcpy(bus->sysdata, sd, sizeof(*sd));
287                 kfree(sd);
288         } else {
289                 bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
290                 if (bus) {
291                         get_current_resources(device, busnum, domain, bus);
292                         bus->subordinate = pci_scan_child_bus(bus);
293                 }
294         }
295
296         if (!bus)
297                 kfree(sd);
298
299         if (bus && node != -1) {
300 #ifdef CONFIG_ACPI_NUMA
301                 if (pxm >= 0)
302                         dev_printk(KERN_DEBUG, &bus->dev,
303                                    "on NUMA node %d (pxm %d)\n", node, pxm);
304 #else
305                 dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
306 #endif
307         }
308
309         return bus;
310 }
311
312 int __init pci_acpi_init(void)
313 {
314         struct pci_dev *dev = NULL;
315
316         if (acpi_noirq)
317                 return -ENODEV;
318
319         printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
320         acpi_irq_penalty_init();
321         pcibios_enable_irq = acpi_pci_irq_enable;
322         pcibios_disable_irq = acpi_pci_irq_disable;
323         x86_init.pci.init_irq = x86_init_noop;
324
325         if (pci_routeirq) {
326                 /*
327                  * PCI IRQ routing is set up by pci_enable_device(), but we
328                  * also do it here in case there are still broken drivers that
329                  * don't use pci_enable_device().
330                  */
331                 printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
332                 for_each_pci_dev(dev)
333                         acpi_pci_irq_enable(dev);
334         }
335
336         return 0;
337 }