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