commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / acpi / pci_irq.c
1 /*
2  *  pci_irq.c - ACPI PCI Interrupt Routing ($Revision: 11 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2002       Dominik Brodowski <devel@brodo.de>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/proc_fs.h>
32 #include <linux/spinlock.h>
33 #include <linux/pm.h>
34 #include <linux/pci.h>
35 #include <linux/acpi.h>
36 #ifdef CONFIG_X86_IO_APIC
37 #include <asm/mpspec.h>
38 #endif
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41
42
43 #define _COMPONENT              ACPI_PCI_COMPONENT
44 ACPI_MODULE_NAME                ("pci_irq")
45
46 struct acpi_prt_list            acpi_prt;
47
48 #ifdef CONFIG_X86
49 extern void eisa_set_level_irq(unsigned int irq);
50 #endif
51
52
53 /* --------------------------------------------------------------------------
54                          PCI IRQ Routing Table (PRT) Support
55    -------------------------------------------------------------------------- */
56
57 static struct acpi_prt_entry *
58 acpi_pci_irq_find_prt_entry (
59         int                     segment,
60         int                     bus,
61         int                     device,
62         int                     pin)
63 {
64         struct list_head        *node = NULL;
65         struct acpi_prt_entry   *entry = NULL;
66
67         ACPI_FUNCTION_TRACE("acpi_pci_irq_find_prt_entry");
68
69         /*
70          * Parse through all PRT entries looking for a match on the specified
71          * PCI device's segment, bus, device, and pin (don't care about func).
72          *
73          * TBD: Acquire/release lock
74          */
75         list_for_each(node, &acpi_prt.entries) {
76                 entry = list_entry(node, struct acpi_prt_entry, node);
77                 if ((segment == entry->id.segment) 
78                         && (bus == entry->id.bus) 
79                         && (device == entry->id.device)
80                         && (pin == entry->pin)) {
81                         return_PTR(entry);
82                 }
83         }
84
85         return_PTR(NULL);
86 }
87
88
89 static int
90 acpi_pci_irq_add_entry (
91         acpi_handle                     handle,
92         int                             segment,
93         int                             bus,
94         struct acpi_pci_routing_table   *prt)
95 {
96         struct acpi_prt_entry   *entry = NULL;
97
98         ACPI_FUNCTION_TRACE("acpi_pci_irq_add_entry");
99
100         if (!prt)
101                 return_VALUE(-EINVAL);
102
103         entry = kmalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
104         if (!entry)
105                 return_VALUE(-ENOMEM);
106         memset(entry, 0, sizeof(struct acpi_prt_entry));
107
108         entry->id.segment = segment;
109         entry->id.bus = bus;
110         entry->id.device = (prt->address >> 16) & 0xFFFF;
111         entry->id.function = prt->address & 0xFFFF;
112         entry->pin = prt->pin;
113
114         /*
115          * Type 1: Dynamic
116          * ---------------
117          * The 'source' field specifies the PCI interrupt link device used to
118          * configure the IRQ assigned to this slot|dev|pin.  The 'source_index'
119          * indicates which resource descriptor in the resource template (of
120          * the link device) this interrupt is allocated from.
121          * 
122          * NOTE: Don't query the Link Device for IRQ information at this time
123          *       because Link Device enumeration may not have occurred yet
124          *       (e.g. exists somewhere 'below' this _PRT entry in the ACPI
125          *       namespace).
126          */
127         if (prt->source[0]) {
128                 acpi_get_handle(handle, prt->source, &entry->link.handle);
129                 entry->link.index = prt->source_index;
130         }
131         /*
132          * Type 2: Static
133          * --------------
134          * The 'source' field is NULL, and the 'source_index' field specifies
135          * the IRQ value, which is hardwired to specific interrupt inputs on
136          * the interrupt controller.
137          */
138         else
139                 entry->link.index = prt->source_index;
140
141         ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
142                 "      %02X:%02X:%02X[%c] -> %s[%d]\n", 
143                 entry->id.segment, entry->id.bus, entry->id.device, 
144                 ('A' + entry->pin), prt->source, entry->link.index));
145
146         /* TBD: Acquire/release lock */
147         list_add_tail(&entry->node, &acpi_prt.entries);
148         acpi_prt.count++;
149
150         return_VALUE(0);
151 }
152
153
154 int
155 acpi_pci_irq_add_prt (
156         acpi_handle             handle,
157         int                     segment,
158         int                     bus)
159 {
160         acpi_status                     status = AE_OK;
161         char                            pathname[ACPI_PATHNAME_MAX] = {0};
162         struct acpi_buffer              buffer = {0, NULL};
163         struct acpi_pci_routing_table   *prt = NULL;
164         struct acpi_pci_routing_table   *entry = NULL;
165         static int                      first_time = 1;
166
167         ACPI_FUNCTION_TRACE("acpi_pci_irq_add_prt");
168
169         if (first_time) {
170                 acpi_prt.count = 0;
171                 INIT_LIST_HEAD(&acpi_prt.entries);
172                 first_time = 0;
173         }
174
175         /* 
176          * NOTE: We're given a 'handle' to the _PRT object's parent device
177          *       (either a PCI root bridge or PCI-PCI bridge).
178          */
179
180         buffer.length = sizeof(pathname);
181         buffer.pointer = pathname;
182         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
183
184         printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
185                 pathname);
186
187         /* 
188          * Evaluate this _PRT and add its entries to our global list (acpi_prt).
189          */
190
191         buffer.length = 0;
192         buffer.pointer = NULL;
193         status = acpi_get_irq_routing_table(handle, &buffer);
194         if (status != AE_BUFFER_OVERFLOW) {
195                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRT [%s]\n",
196                         acpi_format_exception(status)));
197                 return_VALUE(-ENODEV);
198         }
199
200         prt = kmalloc(buffer.length, GFP_KERNEL);
201         if (!prt)
202                 return_VALUE(-ENOMEM);
203         memset(prt, 0, buffer.length);
204         buffer.pointer = prt;
205
206         status = acpi_get_irq_routing_table(handle, &buffer);
207         if (ACPI_FAILURE(status)) {
208                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRT [%s]\n",
209                         acpi_format_exception(status)));
210                 kfree(buffer.pointer);
211                 return_VALUE(-ENODEV);
212         }
213
214         entry = prt;
215
216         while (entry && (entry->length > 0)) {
217                 acpi_pci_irq_add_entry(handle, segment, bus, entry);
218                 entry = (struct acpi_pci_routing_table *)
219                         ((unsigned long) entry + entry->length);
220         }
221
222         kfree(prt);
223
224         return_VALUE(0);
225 }
226
227
228 /* --------------------------------------------------------------------------
229                           PCI Interrupt Routing Support
230    -------------------------------------------------------------------------- */
231
232 static int
233 acpi_pci_irq_lookup (struct pci_bus *bus, int device, int pin)
234 {
235         struct acpi_prt_entry   *entry = NULL;
236         int segment = pci_domain_nr(bus);
237         int bus_nr = bus->number;
238
239         ACPI_FUNCTION_TRACE("acpi_pci_irq_lookup");
240
241         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
242                 "Searching for PRT entry for %02x:%02x:%02x[%c]\n", 
243                 segment, bus_nr, device, ('A' + pin)));
244
245         entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin); 
246         if (!entry) {
247                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
248                 return_VALUE(0);
249         }
250
251         if (!entry->irq && entry->link.handle) {
252                 entry->irq = acpi_pci_link_get_irq(entry->link.handle, entry->link.index);
253                 if (!entry->irq) {
254                         ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ link routing entry\n"));
255                         return_VALUE(0);
256                 }
257         }
258         else if (!entry->irq) {
259                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid static routing entry (IRQ 0)\n"));
260                 return_VALUE(0);
261         }
262
263         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", entry->irq));
264
265         return_VALUE(entry->irq);
266 }
267
268
269 static int
270 acpi_pci_irq_derive (
271         struct pci_dev          *dev,
272         int                     pin)
273 {
274         struct pci_dev          *bridge = dev;
275         int                     irq = 0;
276
277         ACPI_FUNCTION_TRACE("acpi_pci_irq_derive");
278
279         if (!dev)
280                 return_VALUE(-EINVAL);
281
282         /* 
283          * Attempt to derive an IRQ for this device from a parent bridge's
284          * PCI interrupt routing entry (a.k.a. the "bridge swizzle").
285          */
286         while (!irq && bridge->bus->self) {
287                 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
288                 bridge = bridge->bus->self;
289                 irq = acpi_pci_irq_lookup(bridge->bus,
290                                 PCI_SLOT(bridge->devfn), pin);
291         }
292
293         if (!irq) {
294                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Unable to derive IRQ for device %s\n", pci_name(dev)));
295                 return_VALUE(0);
296         }
297
298         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derived IRQ %d\n", irq));
299
300         return_VALUE(irq);
301 }
302
303
304 int
305 acpi_pci_irq_enable (
306         struct pci_dev          *dev)
307 {
308         int                     irq = 0;
309         u8                      pin = 0;
310         static u16              irq_mask = 0;
311
312         ACPI_FUNCTION_TRACE("acpi_pci_irq_enable");
313
314         if (!dev)
315                 return_VALUE(-EINVAL);
316         
317         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
318         if (!pin) {
319                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No interrupt pin configured for device %s\n", pci_name(dev)));
320                 return_VALUE(0);
321         }
322         pin--;
323
324         if (!dev->bus) {
325                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) 'bus' field\n"));
326                 return_VALUE(-ENODEV);
327         }
328
329         /* 
330          * First we check the PCI IRQ routing table (PRT) for an IRQ.  PRT
331          * values override any BIOS-assigned IRQs set during boot.
332          */
333         irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin);
334  
335         /*
336          * If no PRT entry was found, we'll try to derive an IRQ from the
337          * device's parent bridge.
338          */
339         if (!irq)
340                 irq = acpi_pci_irq_derive(dev, pin);
341  
342         /*
343          * No IRQ known to the ACPI subsystem - maybe the BIOS / 
344          * driver reported one, then use it. Exit in any case.
345          */
346         if (!irq) {
347                 printk(KERN_WARNING PREFIX "No IRQ known for interrupt pin %c of device %s", ('A' + pin), pci_name(dev));
348                 /* Interrupt Line values above 0xF are forbidden */
349                 if (dev->irq && dev->irq >= 0xF) {
350                         printk(" - using IRQ %d\n", dev->irq);
351                         return_VALUE(dev->irq);
352                 }
353                 else {
354                         printk("\n");
355                         return_VALUE(0);
356                 }
357         }
358
359         dev->irq = irq;
360
361         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device %s using IRQ %d\n", pci_name(dev), dev->irq));
362
363         /* 
364          * Make sure all (legacy) PCI IRQs are set as level-triggered.
365          */
366 #ifdef CONFIG_X86
367         if ((dev->irq < 16) &&  !((1 << dev->irq) & irq_mask)) {
368                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Setting IRQ %d as level-triggered\n", dev->irq));
369                 irq_mask |= (1 << dev->irq);
370                 eisa_set_level_irq(dev->irq);
371         }
372 #endif
373 #ifdef CONFIG_IOSAPIC
374         if (acpi_irq_model == ACPI_IRQ_MODEL_IOSAPIC)
375                 iosapic_enable_intr(dev->irq);
376 #endif
377
378         return_VALUE(dev->irq);
379 }
380
381
382 int __init
383 acpi_pci_irq_init (void)
384 {
385         struct pci_dev          *dev = NULL;
386
387         ACPI_FUNCTION_TRACE("acpi_pci_irq_init");
388
389         if (!acpi_prt.count) {
390                 printk(KERN_WARNING PREFIX "ACPI tables contain no PCI IRQ "
391                         "routing entries\n");
392                 return_VALUE(-ENODEV);
393         }
394
395         /* Make sure all link devices have a valid IRQ. */
396         acpi_pci_link_check();
397
398 #ifdef CONFIG_X86_IO_APIC
399         /* Program IOAPICs using data from PRT entries. */
400         if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
401                 mp_parse_prt();
402 #endif
403 #ifdef CONFIG_IOSAPIC
404         if (acpi_irq_model == ACPI_IRQ_MODEL_IOSAPIC)
405                 iosapic_parse_prt();
406 #endif
407
408         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
409                 acpi_pci_irq_enable(dev);
410
411         return_VALUE(0);
412 }