VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 static int
231 acpi_pci_irq_lookup (
232         struct pci_bus          *bus,
233         int                     device,
234         int                     pin,
235         int                     *edge_level,
236         int                     *active_high_low)
237 {
238         struct acpi_prt_entry   *entry = NULL;
239         int segment = pci_domain_nr(bus);
240         int bus_nr = bus->number;
241         int irq;
242
243         ACPI_FUNCTION_TRACE("acpi_pci_irq_lookup");
244
245         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
246                 "Searching for PRT entry for %02x:%02x:%02x[%c]\n", 
247                 segment, bus_nr, device, ('A' + pin)));
248
249         entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin); 
250         if (!entry) {
251                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
252                 return_VALUE(0);
253         }
254         
255         if (entry->link.handle) {
256                 irq = acpi_pci_link_get_irq(entry->link.handle, entry->link.index, edge_level, active_high_low);
257                 if (!irq) {
258                         ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ link routing entry\n"));
259                         return_VALUE(0);
260                 }
261         } else {
262                 irq = entry->link.index;
263                 *edge_level = ACPI_LEVEL_SENSITIVE;
264                 *active_high_low = ACPI_ACTIVE_LOW;
265         }
266
267         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
268
269         return_VALUE(irq);
270 }
271
272 static int
273 acpi_pci_irq_derive (
274         struct pci_dev          *dev,
275         int                     pin,
276         int                     *edge_level,
277         int                     *active_high_low)
278 {
279         struct pci_dev          *bridge = dev;
280         int                     irq = 0;
281         u8                      bridge_pin = 0;
282
283         ACPI_FUNCTION_TRACE("acpi_pci_irq_derive");
284
285         if (!dev)
286                 return_VALUE(-EINVAL);
287
288         /* 
289          * Attempt to derive an IRQ for this device from a parent bridge's
290          * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
291          */
292         while (!irq && bridge->bus->self) {
293                 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
294                 bridge = bridge->bus->self;
295
296                 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
297                         /* PC card has the same IRQ as its cardbridge */
298                         pci_read_config_byte(bridge, PCI_INTERRUPT_PIN, &bridge_pin);
299                         if (!bridge_pin) {
300                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
301                                         "No interrupt pin configured for device %s\n", pci_name(bridge)));
302                                 return_VALUE(0);
303                         }
304                         /* Pin is from 0 to 3 */
305                         bridge_pin --;
306                         pin = bridge_pin;
307                 }
308
309                 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
310                         pin, edge_level, active_high_low);
311         }
312
313         if (!irq) {
314                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Unable to derive IRQ for device %s\n", pci_name(dev)));
315                 return_VALUE(0);
316         }
317
318         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
319                 irq, pci_name(dev), pci_name(bridge)));
320
321         return_VALUE(irq);
322 }
323
324
325 int
326 acpi_pci_irq_enable (
327         struct pci_dev          *dev)
328 {
329         int                     irq = 0;
330         u8                      pin = 0;
331         int                     edge_level = ACPI_LEVEL_SENSITIVE;
332         int                     active_high_low = ACPI_ACTIVE_LOW;
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, &edge_level, &active_high_low);
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, &edge_level, &active_high_low);
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 "PCI interrupt %s[%c]: no GSI",
370                         pci_name(dev), ('A' + pin));
371                 /* Interrupt Line values above 0xF are forbidden */
372                 if (dev->irq && (dev->irq <= 0xF)) {
373                         printk(" - using IRQ %d\n", dev->irq);
374                         return_VALUE(dev->irq);
375                 }
376                 else {
377                         printk("\n");
378                         return_VALUE(0);
379                 }
380         }
381
382         dev->irq = acpi_register_gsi(irq, edge_level, active_high_low);
383
384         printk(KERN_INFO PREFIX "PCI interrupt %s[%c] -> GSI %u "
385                 "(%s, %s) -> IRQ %d\n",
386                 pci_name(dev), 'A' + pin, irq,
387                 (edge_level == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
388                 (active_high_low == ACPI_ACTIVE_LOW) ? "low" : "high",
389                 dev->irq);
390
391         return_VALUE(dev->irq);
392 }