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