]> err.no Git - linux-2.6/blob - drivers/acpi/pci_irq.c
Remove obsolete #include <linux/config.h>
[linux-2.6] / 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
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/proc_fs.h>
33 #include <linux/spinlock.h>
34 #include <linux/pm.h>
35 #include <linux/pci.h>
36 #include <linux/acpi.h>
37 #include <acpi/acpi_bus.h>
38 #include <acpi/acpi_drivers.h>
39
40 #define _COMPONENT              ACPI_PCI_COMPONENT
41 ACPI_MODULE_NAME("pci_irq")
42
43 static struct acpi_prt_list acpi_prt;
44 static DEFINE_SPINLOCK(acpi_prt_lock);
45
46 /* --------------------------------------------------------------------------
47                          PCI IRQ Routing Table (PRT) Support
48    -------------------------------------------------------------------------- */
49
50 static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
51                                                           int bus,
52                                                           int device, int pin)
53 {
54         struct list_head *node = NULL;
55         struct acpi_prt_entry *entry = NULL;
56
57         ACPI_FUNCTION_TRACE("acpi_pci_irq_find_prt_entry");
58
59         if (!acpi_prt.count)
60                 return_PTR(NULL);
61
62         /*
63          * Parse through all PRT entries looking for a match on the specified
64          * PCI device's segment, bus, device, and pin (don't care about func).
65          *
66          */
67         spin_lock(&acpi_prt_lock);
68         list_for_each(node, &acpi_prt.entries) {
69                 entry = list_entry(node, struct acpi_prt_entry, node);
70                 if ((segment == entry->id.segment)
71                     && (bus == entry->id.bus)
72                     && (device == entry->id.device)
73                     && (pin == entry->pin)) {
74                         spin_unlock(&acpi_prt_lock);
75                         return_PTR(entry);
76                 }
77         }
78
79         spin_unlock(&acpi_prt_lock);
80         return_PTR(NULL);
81 }
82
83 static int
84 acpi_pci_irq_add_entry(acpi_handle handle,
85                        int segment, int bus, struct acpi_pci_routing_table *prt)
86 {
87         struct acpi_prt_entry *entry = NULL;
88
89         ACPI_FUNCTION_TRACE("acpi_pci_irq_add_entry");
90
91         if (!prt)
92                 return_VALUE(-EINVAL);
93
94         entry = kmalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
95         if (!entry)
96                 return_VALUE(-ENOMEM);
97         memset(entry, 0, sizeof(struct acpi_prt_entry));
98
99         entry->id.segment = segment;
100         entry->id.bus = bus;
101         entry->id.device = (prt->address >> 16) & 0xFFFF;
102         entry->id.function = prt->address & 0xFFFF;
103         entry->pin = prt->pin;
104
105         /*
106          * Type 1: Dynamic
107          * ---------------
108          * The 'source' field specifies the PCI interrupt link device used to
109          * configure the IRQ assigned to this slot|dev|pin.  The 'source_index'
110          * indicates which resource descriptor in the resource template (of
111          * the link device) this interrupt is allocated from.
112          * 
113          * NOTE: Don't query the Link Device for IRQ information at this time
114          *       because Link Device enumeration may not have occurred yet
115          *       (e.g. exists somewhere 'below' this _PRT entry in the ACPI
116          *       namespace).
117          */
118         if (prt->source[0]) {
119                 acpi_get_handle(handle, prt->source, &entry->link.handle);
120                 entry->link.index = prt->source_index;
121         }
122         /*
123          * Type 2: Static
124          * --------------
125          * The 'source' field is NULL, and the 'source_index' field specifies
126          * the IRQ value, which is hardwired to specific interrupt inputs on
127          * the interrupt controller.
128          */
129         else
130                 entry->link.index = prt->source_index;
131
132         ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
133                               "      %02X:%02X:%02X[%c] -> %s[%d]\n",
134                               entry->id.segment, entry->id.bus,
135                               entry->id.device, ('A' + entry->pin), prt->source,
136                               entry->link.index));
137
138         spin_lock(&acpi_prt_lock);
139         list_add_tail(&entry->node, &acpi_prt.entries);
140         acpi_prt.count++;
141         spin_unlock(&acpi_prt_lock);
142
143         return_VALUE(0);
144 }
145
146 static void
147 acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
148 {
149         if (segment == entry->id.segment && bus == entry->id.bus) {
150                 acpi_prt.count--;
151                 list_del(&entry->node);
152                 kfree(entry);
153         }
154 }
155
156 int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
157 {
158         acpi_status status = AE_OK;
159         char *pathname = NULL;
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         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
168         if (!pathname)
169                 return_VALUE(-ENOMEM);
170         memset(pathname, 0, ACPI_PATHNAME_MAX);
171
172         if (first_time) {
173                 acpi_prt.count = 0;
174                 INIT_LIST_HEAD(&acpi_prt.entries);
175                 first_time = 0;
176         }
177
178         /* 
179          * NOTE: We're given a 'handle' to the _PRT object's parent device
180          *       (either a PCI root bridge or PCI-PCI bridge).
181          */
182
183         buffer.length = ACPI_PATHNAME_MAX;
184         buffer.pointer = pathname;
185         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
186
187         printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
188                pathname);
189
190         /* 
191          * Evaluate this _PRT and add its entries to our global list (acpi_prt).
192          */
193
194         buffer.length = 0;
195         buffer.pointer = NULL;
196         kfree(pathname);
197         status = acpi_get_irq_routing_table(handle, &buffer);
198         if (status != AE_BUFFER_OVERFLOW) {
199                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRT [%s]\n",
200                                   acpi_format_exception(status)));
201                 return_VALUE(-ENODEV);
202         }
203
204         prt = kmalloc(buffer.length, GFP_KERNEL);
205         if (!prt) {
206                 return_VALUE(-ENOMEM);
207         }
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 void acpi_pci_irq_del_prt(int segment, int bus)
233 {
234         struct list_head *node = NULL, *n = NULL;
235         struct acpi_prt_entry *entry = NULL;
236
237         if (!acpi_prt.count) {
238                 return;
239         }
240
241         printk(KERN_DEBUG
242                "ACPI: Delete PCI Interrupt Routing Table for %x:%x\n", segment,
243                bus);
244         spin_lock(&acpi_prt_lock);
245         list_for_each_safe(node, n, &acpi_prt.entries) {
246                 entry = list_entry(node, struct acpi_prt_entry, node);
247
248                 acpi_pci_irq_del_entry(segment, bus, entry);
249         }
250         spin_unlock(&acpi_prt_lock);
251 }
252
253 /* --------------------------------------------------------------------------
254                           PCI Interrupt Routing Support
255    -------------------------------------------------------------------------- */
256 typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
257
258 static int
259 acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
260                       int *triggering, int *polarity, char **link)
261 {
262         int irq;
263
264         ACPI_FUNCTION_TRACE("acpi_pci_allocate_irq");
265
266         if (entry->link.handle) {
267                 irq = acpi_pci_link_allocate_irq(entry->link.handle,
268                                                  entry->link.index, triggering,
269                                                  polarity, link);
270                 if (irq < 0) {
271                         ACPI_DEBUG_PRINT((ACPI_DB_WARN,
272                                           "Invalid IRQ link routing entry\n"));
273                         return_VALUE(-1);
274                 }
275         } else {
276                 irq = entry->link.index;
277                 *triggering = ACPI_LEVEL_SENSITIVE;
278                 *polarity = ACPI_ACTIVE_LOW;
279         }
280
281         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
282         return_VALUE(irq);
283 }
284
285 static int
286 acpi_pci_free_irq(struct acpi_prt_entry *entry,
287                   int *triggering, int *polarity, char **link)
288 {
289         int irq;
290
291         ACPI_FUNCTION_TRACE("acpi_pci_free_irq");
292         if (entry->link.handle) {
293                 irq = acpi_pci_link_free_irq(entry->link.handle);
294         } else {
295                 irq = entry->link.index;
296         }
297         return_VALUE(irq);
298 }
299
300 /*
301  * acpi_pci_irq_lookup
302  * success: return IRQ >= 0
303  * failure: return -1
304  */
305 static int
306 acpi_pci_irq_lookup(struct pci_bus *bus,
307                     int device,
308                     int pin,
309                     int *triggering,
310                     int *polarity, char **link, irq_lookup_func func)
311 {
312         struct acpi_prt_entry *entry = NULL;
313         int segment = pci_domain_nr(bus);
314         int bus_nr = bus->number;
315         int ret;
316
317         ACPI_FUNCTION_TRACE("acpi_pci_irq_lookup");
318
319         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
320                           "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
321                           segment, bus_nr, device, ('A' + pin)));
322
323         entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
324         if (!entry) {
325                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
326                 return_VALUE(-1);
327         }
328
329         ret = func(entry, triggering, polarity, link);
330         return_VALUE(ret);
331 }
332
333 /*
334  * acpi_pci_irq_derive
335  * success: return IRQ >= 0
336  * failure: return < 0
337  */
338 static int
339 acpi_pci_irq_derive(struct pci_dev *dev,
340                     int pin,
341                     int *triggering,
342                     int *polarity, char **link, irq_lookup_func func)
343 {
344         struct pci_dev *bridge = dev;
345         int irq = -1;
346         u8 bridge_pin = 0;
347
348         ACPI_FUNCTION_TRACE("acpi_pci_irq_derive");
349
350         if (!dev)
351                 return_VALUE(-EINVAL);
352
353         /* 
354          * Attempt to derive an IRQ for this device from a parent bridge's
355          * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
356          */
357         while (irq < 0 && bridge->bus->self) {
358                 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
359                 bridge = bridge->bus->self;
360
361                 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
362                         /* PC card has the same IRQ as its cardbridge */
363                         bridge_pin = bridge->pin;
364                         if (!bridge_pin) {
365                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
366                                                   "No interrupt pin configured for device %s\n",
367                                                   pci_name(bridge)));
368                                 return_VALUE(-1);
369                         }
370                         /* Pin is from 0 to 3 */
371                         bridge_pin--;
372                         pin = bridge_pin;
373                 }
374
375                 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
376                                           pin, triggering, polarity,
377                                           link, func);
378         }
379
380         if (irq < 0) {
381                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
382                                   "Unable to derive IRQ for device %s\n",
383                                   pci_name(dev)));
384                 return_VALUE(-1);
385         }
386
387         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
388                           irq, pci_name(dev), pci_name(bridge)));
389
390         return_VALUE(irq);
391 }
392
393 /*
394  * acpi_pci_irq_enable
395  * success: return 0
396  * failure: return < 0
397  */
398
399 int acpi_pci_irq_enable(struct pci_dev *dev)
400 {
401         int irq = 0;
402         u8 pin = 0;
403         int triggering = ACPI_LEVEL_SENSITIVE;
404         int polarity = ACPI_ACTIVE_LOW;
405         char *link = NULL;
406         int rc;
407
408         ACPI_FUNCTION_TRACE("acpi_pci_irq_enable");
409
410         if (!dev)
411                 return_VALUE(-EINVAL);
412
413         pin = dev->pin;
414         if (!pin) {
415                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
416                                   "No interrupt pin configured for device %s\n",
417                                   pci_name(dev)));
418                 return_VALUE(0);
419         }
420         pin--;
421
422         if (!dev->bus) {
423                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
424                                   "Invalid (NULL) 'bus' field\n"));
425                 return_VALUE(-ENODEV);
426         }
427
428         /* 
429          * First we check the PCI IRQ routing table (PRT) for an IRQ.  PRT
430          * values override any BIOS-assigned IRQs set during boot.
431          */
432         irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
433                                   &triggering, &polarity, &link,
434                                   acpi_pci_allocate_irq);
435
436         /*
437          * If no PRT entry was found, we'll try to derive an IRQ from the
438          * device's parent bridge.
439          */
440         if (irq < 0)
441                 irq = acpi_pci_irq_derive(dev, pin, &triggering,
442                                           &polarity, &link,
443                                           acpi_pci_allocate_irq);
444
445         /*
446          * No IRQ known to the ACPI subsystem - maybe the BIOS / 
447          * driver reported one, then use it. Exit in any case.
448          */
449         if (irq < 0) {
450                 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
451                        pci_name(dev), ('A' + pin));
452                 /* Interrupt Line values above 0xF are forbidden */
453                 if (dev->irq > 0 && (dev->irq <= 0xF)) {
454                         printk(" - using IRQ %d\n", dev->irq);
455                         acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
456                                           ACPI_ACTIVE_LOW);
457                         return_VALUE(0);
458                 } else {
459                         printk("\n");
460                         return_VALUE(0);
461                 }
462         }
463
464         rc = acpi_register_gsi(irq, triggering, polarity);
465         if (rc < 0) {
466                 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: failed "
467                        "to register GSI\n", pci_name(dev), ('A' + pin));
468                 return_VALUE(rc);
469         }
470         dev->irq = rc;
471
472         printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
473                pci_name(dev), 'A' + pin);
474
475         if (link)
476                 printk("Link [%s] -> ", link);
477
478         printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
479                (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
480                (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
481
482         return_VALUE(0);
483 }
484
485 EXPORT_SYMBOL(acpi_pci_irq_enable);
486
487 /* FIXME: implement x86/x86_64 version */
488 void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
489 {
490 }
491
492 void acpi_pci_irq_disable(struct pci_dev *dev)
493 {
494         int gsi = 0;
495         u8 pin = 0;
496         int triggering = ACPI_LEVEL_SENSITIVE;
497         int polarity = ACPI_ACTIVE_LOW;
498
499         ACPI_FUNCTION_TRACE("acpi_pci_irq_disable");
500
501         if (!dev || !dev->bus)
502                 return_VOID;
503
504         pin = dev->pin;
505         if (!pin)
506                 return_VOID;
507         pin--;
508
509         /*
510          * First we check the PCI IRQ routing table (PRT) for an IRQ.
511          */
512         gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
513                                   &triggering, &polarity, NULL,
514                                   acpi_pci_free_irq);
515         /*
516          * If no PRT entry was found, we'll try to derive an IRQ from the
517          * device's parent bridge.
518          */
519         if (gsi < 0)
520                 gsi = acpi_pci_irq_derive(dev, pin,
521                                           &triggering, &polarity, NULL,
522                                           acpi_pci_free_irq);
523         if (gsi < 0)
524                 return_VOID;
525
526         /*
527          * TBD: It might be worth clearing dev->irq by magic constant
528          * (e.g. PCI_UNDEFINED_IRQ).
529          */
530
531         printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
532                pci_name(dev));
533
534         acpi_unregister_gsi(gsi);
535
536         return_VOID;
537 }