[ALSA] hda-codec - Fix surrounds on 3stack mode of AD1988
[linux-2.6/linux-loongson.git] / drivers / acpi / pci_irq.c
blob09567c2edcfb5225a67deb44d71cba7249915735
1 /*
2 * pci_irq.c - ACPI PCI Interrupt Routing ($Revision: 11 $)
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/config.h>
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>
41 #define _COMPONENT ACPI_PCI_COMPONENT
42 ACPI_MODULE_NAME("pci_irq")
44 static struct acpi_prt_list acpi_prt;
45 static DEFINE_SPINLOCK(acpi_prt_lock);
47 /* --------------------------------------------------------------------------
48 PCI IRQ Routing Table (PRT) Support
49 -------------------------------------------------------------------------- */
51 static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
52 int bus,
53 int device, int pin)
55 struct list_head *node = NULL;
56 struct acpi_prt_entry *entry = NULL;
58 ACPI_FUNCTION_TRACE("acpi_pci_irq_find_prt_entry");
60 if (!acpi_prt.count)
61 return_PTR(NULL);
64 * Parse through all PRT entries looking for a match on the specified
65 * PCI device's segment, bus, device, and pin (don't care about func).
68 spin_lock(&acpi_prt_lock);
69 list_for_each(node, &acpi_prt.entries) {
70 entry = list_entry(node, struct acpi_prt_entry, node);
71 if ((segment == entry->id.segment)
72 && (bus == entry->id.bus)
73 && (device == entry->id.device)
74 && (pin == entry->pin)) {
75 spin_unlock(&acpi_prt_lock);
76 return_PTR(entry);
80 spin_unlock(&acpi_prt_lock);
81 return_PTR(NULL);
84 static int
85 acpi_pci_irq_add_entry(acpi_handle handle,
86 int segment, int bus, struct acpi_pci_routing_table *prt)
88 struct acpi_prt_entry *entry = NULL;
90 ACPI_FUNCTION_TRACE("acpi_pci_irq_add_entry");
92 if (!prt)
93 return_VALUE(-EINVAL);
95 entry = kmalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
96 if (!entry)
97 return_VALUE(-ENOMEM);
98 memset(entry, 0, sizeof(struct acpi_prt_entry));
100 entry->id.segment = segment;
101 entry->id.bus = bus;
102 entry->id.device = (prt->address >> 16) & 0xFFFF;
103 entry->id.function = prt->address & 0xFFFF;
104 entry->pin = prt->pin;
107 * Type 1: Dynamic
108 * ---------------
109 * The 'source' field specifies the PCI interrupt link device used to
110 * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
111 * indicates which resource descriptor in the resource template (of
112 * the link device) this interrupt is allocated from.
114 * NOTE: Don't query the Link Device for IRQ information at this time
115 * because Link Device enumeration may not have occurred yet
116 * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
117 * namespace).
119 if (prt->source[0]) {
120 acpi_get_handle(handle, prt->source, &entry->link.handle);
121 entry->link.index = prt->source_index;
124 * Type 2: Static
125 * --------------
126 * The 'source' field is NULL, and the 'source_index' field specifies
127 * the IRQ value, which is hardwired to specific interrupt inputs on
128 * the interrupt controller.
130 else
131 entry->link.index = prt->source_index;
133 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
134 " %02X:%02X:%02X[%c] -> %s[%d]\n",
135 entry->id.segment, entry->id.bus,
136 entry->id.device, ('A' + entry->pin), prt->source,
137 entry->link.index));
139 spin_lock(&acpi_prt_lock);
140 list_add_tail(&entry->node, &acpi_prt.entries);
141 acpi_prt.count++;
142 spin_unlock(&acpi_prt_lock);
144 return_VALUE(0);
147 static void
148 acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
150 if (segment == entry->id.segment && bus == entry->id.bus) {
151 acpi_prt.count--;
152 list_del(&entry->node);
153 kfree(entry);
157 int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
159 acpi_status status = AE_OK;
160 char *pathname = NULL;
161 struct acpi_buffer buffer = { 0, NULL };
162 struct acpi_pci_routing_table *prt = NULL;
163 struct acpi_pci_routing_table *entry = NULL;
164 static int first_time = 1;
166 ACPI_FUNCTION_TRACE("acpi_pci_irq_add_prt");
168 pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
169 if (!pathname)
170 return_VALUE(-ENOMEM);
171 memset(pathname, 0, ACPI_PATHNAME_MAX);
173 if (first_time) {
174 acpi_prt.count = 0;
175 INIT_LIST_HEAD(&acpi_prt.entries);
176 first_time = 0;
180 * NOTE: We're given a 'handle' to the _PRT object's parent device
181 * (either a PCI root bridge or PCI-PCI bridge).
184 buffer.length = ACPI_PATHNAME_MAX;
185 buffer.pointer = pathname;
186 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
188 printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
189 pathname);
192 * Evaluate this _PRT and add its entries to our global list (acpi_prt).
195 buffer.length = 0;
196 buffer.pointer = NULL;
197 kfree(pathname);
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);
205 prt = kmalloc(buffer.length, GFP_KERNEL);
206 if (!prt) {
207 return_VALUE(-ENOMEM);
209 memset(prt, 0, buffer.length);
210 buffer.pointer = prt;
212 status = acpi_get_irq_routing_table(handle, &buffer);
213 if (ACPI_FAILURE(status)) {
214 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRT [%s]\n",
215 acpi_format_exception(status)));
216 kfree(buffer.pointer);
217 return_VALUE(-ENODEV);
220 entry = prt;
222 while (entry && (entry->length > 0)) {
223 acpi_pci_irq_add_entry(handle, segment, bus, entry);
224 entry = (struct acpi_pci_routing_table *)
225 ((unsigned long)entry + entry->length);
228 kfree(prt);
230 return_VALUE(0);
233 void acpi_pci_irq_del_prt(int segment, int bus)
235 struct list_head *node = NULL, *n = NULL;
236 struct acpi_prt_entry *entry = NULL;
238 if (!acpi_prt.count) {
239 return;
242 printk(KERN_DEBUG
243 "ACPI: Delete PCI Interrupt Routing Table for %x:%x\n", segment,
244 bus);
245 spin_lock(&acpi_prt_lock);
246 list_for_each_safe(node, n, &acpi_prt.entries) {
247 entry = list_entry(node, struct acpi_prt_entry, node);
249 acpi_pci_irq_del_entry(segment, bus, entry);
251 spin_unlock(&acpi_prt_lock);
254 /* --------------------------------------------------------------------------
255 PCI Interrupt Routing Support
256 -------------------------------------------------------------------------- */
257 typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
259 static int
260 acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
261 int *edge_level, int *active_high_low, char **link)
263 int irq;
265 ACPI_FUNCTION_TRACE("acpi_pci_allocate_irq");
267 if (entry->link.handle) {
268 irq = acpi_pci_link_allocate_irq(entry->link.handle,
269 entry->link.index, edge_level,
270 active_high_low, link);
271 if (irq < 0) {
272 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
273 "Invalid IRQ link routing entry\n"));
274 return_VALUE(-1);
276 } else {
277 irq = entry->link.index;
278 *edge_level = ACPI_LEVEL_SENSITIVE;
279 *active_high_low = ACPI_ACTIVE_LOW;
282 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
283 return_VALUE(irq);
286 static int
287 acpi_pci_free_irq(struct acpi_prt_entry *entry,
288 int *edge_level, int *active_high_low, char **link)
290 int irq;
292 ACPI_FUNCTION_TRACE("acpi_pci_free_irq");
293 if (entry->link.handle) {
294 irq = acpi_pci_link_free_irq(entry->link.handle);
295 } else {
296 irq = entry->link.index;
298 return_VALUE(irq);
302 * acpi_pci_irq_lookup
303 * success: return IRQ >= 0
304 * failure: return -1
306 static int
307 acpi_pci_irq_lookup(struct pci_bus *bus,
308 int device,
309 int pin,
310 int *edge_level,
311 int *active_high_low, char **link, irq_lookup_func func)
313 struct acpi_prt_entry *entry = NULL;
314 int segment = pci_domain_nr(bus);
315 int bus_nr = bus->number;
316 int ret;
318 ACPI_FUNCTION_TRACE("acpi_pci_irq_lookup");
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
322 segment, bus_nr, device, ('A' + pin)));
324 entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
325 if (!entry) {
326 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
327 return_VALUE(-1);
330 ret = func(entry, edge_level, active_high_low, link);
331 return_VALUE(ret);
335 * acpi_pci_irq_derive
336 * success: return IRQ >= 0
337 * failure: return < 0
339 static int
340 acpi_pci_irq_derive(struct pci_dev *dev,
341 int pin,
342 int *edge_level,
343 int *active_high_low, char **link, irq_lookup_func func)
345 struct pci_dev *bridge = dev;
346 int irq = -1;
347 u8 bridge_pin = 0;
349 ACPI_FUNCTION_TRACE("acpi_pci_irq_derive");
351 if (!dev)
352 return_VALUE(-EINVAL);
355 * Attempt to derive an IRQ for this device from a parent bridge's
356 * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
358 while (irq < 0 && bridge->bus->self) {
359 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
360 bridge = bridge->bus->self;
362 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
363 /* PC card has the same IRQ as its cardbridge */
364 pci_read_config_byte(bridge, PCI_INTERRUPT_PIN,
365 &bridge_pin);
366 if (!bridge_pin) {
367 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
368 "No interrupt pin configured for device %s\n",
369 pci_name(bridge)));
370 return_VALUE(-1);
372 /* Pin is from 0 to 3 */
373 bridge_pin--;
374 pin = bridge_pin;
377 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
378 pin, edge_level, active_high_low,
379 link, func);
382 if (irq < 0) {
383 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
384 "Unable to derive IRQ for device %s\n",
385 pci_name(dev)));
386 return_VALUE(-1);
389 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
390 irq, pci_name(dev), pci_name(bridge)));
392 return_VALUE(irq);
396 * acpi_pci_irq_enable
397 * success: return 0
398 * failure: return < 0
401 int acpi_pci_irq_enable(struct pci_dev *dev)
403 int irq = 0;
404 u8 pin = 0;
405 int edge_level = ACPI_LEVEL_SENSITIVE;
406 int active_high_low = ACPI_ACTIVE_LOW;
407 char *link = NULL;
408 int rc;
410 ACPI_FUNCTION_TRACE("acpi_pci_irq_enable");
412 if (!dev)
413 return_VALUE(-EINVAL);
415 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
416 if (!pin) {
417 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
418 "No interrupt pin configured for device %s\n",
419 pci_name(dev)));
420 return_VALUE(0);
422 pin--;
424 if (!dev->bus) {
425 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
426 "Invalid (NULL) 'bus' field\n"));
427 return_VALUE(-ENODEV);
431 * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
432 * values override any BIOS-assigned IRQs set during boot.
434 irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
435 &edge_level, &active_high_low, &link,
436 acpi_pci_allocate_irq);
439 * If no PRT entry was found, we'll try to derive an IRQ from the
440 * device's parent bridge.
442 if (irq < 0)
443 irq = acpi_pci_irq_derive(dev, pin, &edge_level,
444 &active_high_low, &link,
445 acpi_pci_allocate_irq);
448 * No IRQ known to the ACPI subsystem - maybe the BIOS /
449 * driver reported one, then use it. Exit in any case.
451 if (irq < 0) {
452 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
453 pci_name(dev), ('A' + pin));
454 /* Interrupt Line values above 0xF are forbidden */
455 if (dev->irq > 0 && (dev->irq <= 0xF)) {
456 printk(" - using IRQ %d\n", dev->irq);
457 acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
458 ACPI_ACTIVE_LOW);
459 return_VALUE(0);
460 } else {
461 printk("\n");
462 return_VALUE(0);
466 rc = acpi_register_gsi(irq, edge_level, active_high_low);
467 if (rc < 0) {
468 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: failed "
469 "to register GSI\n", pci_name(dev), ('A' + pin));
470 return_VALUE(rc);
472 dev->irq = rc;
474 printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
475 pci_name(dev), 'A' + pin);
477 if (link)
478 printk("Link [%s] -> ", link);
480 printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
481 (edge_level == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
482 (active_high_low == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
484 return_VALUE(0);
487 EXPORT_SYMBOL(acpi_pci_irq_enable);
489 /* FIXME: implement x86/x86_64 version */
490 void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
494 void acpi_pci_irq_disable(struct pci_dev *dev)
496 int gsi = 0;
497 u8 pin = 0;
498 int edge_level = ACPI_LEVEL_SENSITIVE;
499 int active_high_low = ACPI_ACTIVE_LOW;
501 ACPI_FUNCTION_TRACE("acpi_pci_irq_disable");
503 if (!dev || !dev->bus)
504 return_VOID;
506 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
507 if (!pin)
508 return_VOID;
509 pin--;
512 * First we check the PCI IRQ routing table (PRT) for an IRQ.
514 gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
515 &edge_level, &active_high_low, NULL,
516 acpi_pci_free_irq);
518 * If no PRT entry was found, we'll try to derive an IRQ from the
519 * device's parent bridge.
521 if (gsi < 0)
522 gsi = acpi_pci_irq_derive(dev, pin,
523 &edge_level, &active_high_low, NULL,
524 acpi_pci_free_irq);
525 if (gsi < 0)
526 return_VOID;
529 * TBD: It might be worth clearing dev->irq by magic constant
530 * (e.g. PCI_UNDEFINED_IRQ).
533 printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
534 pci_name(dev));
536 acpi_unregister_gsi(gsi);
538 return_VOID;