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