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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
35 #include <linux/pci.h>
36 #include <linux/acpi.h>
37 #include <acpi/acpi_bus.h>
38 #include <acpi/acpi_drivers.h>
40 #define _COMPONENT ACPI_PCI_COMPONENT
41 ACPI_MODULE_NAME("pci_irq")
43 static struct acpi_prt_list acpi_prt
;
44 static DEFINE_SPINLOCK(acpi_prt_lock
);
46 /* --------------------------------------------------------------------------
47 PCI IRQ Routing Table (PRT) Support
48 -------------------------------------------------------------------------- */
50 static struct acpi_prt_entry
*acpi_pci_irq_find_prt_entry(int segment
,
54 struct list_head
*node
= NULL
;
55 struct acpi_prt_entry
*entry
= NULL
;
62 * Parse through all PRT entries looking for a match on the specified
63 * PCI device's segment, bus, device, and pin (don't care about func).
66 spin_lock(&acpi_prt_lock
);
67 list_for_each(node
, &acpi_prt
.entries
) {
68 entry
= list_entry(node
, struct acpi_prt_entry
, node
);
69 if ((segment
== entry
->id
.segment
)
70 && (bus
== entry
->id
.bus
)
71 && (device
== entry
->id
.device
)
72 && (pin
== entry
->pin
)) {
73 spin_unlock(&acpi_prt_lock
);
78 spin_unlock(&acpi_prt_lock
);
83 acpi_pci_irq_add_entry(acpi_handle handle
,
84 int segment
, int bus
, struct acpi_pci_routing_table
*prt
)
86 struct acpi_prt_entry
*entry
= NULL
;
92 entry
= kmalloc(sizeof(struct acpi_prt_entry
), GFP_KERNEL
);
95 memset(entry
, 0, sizeof(struct acpi_prt_entry
));
97 entry
->id
.segment
= segment
;
99 entry
->id
.device
= (prt
->address
>> 16) & 0xFFFF;
100 entry
->id
.function
= prt
->address
& 0xFFFF;
101 entry
->pin
= prt
->pin
;
106 * The 'source' field specifies the PCI interrupt link device used to
107 * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
108 * indicates which resource descriptor in the resource template (of
109 * the link device) this interrupt is allocated from.
111 * NOTE: Don't query the Link Device for IRQ information at this time
112 * because Link Device enumeration may not have occurred yet
113 * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
116 if (prt
->source
[0]) {
117 acpi_get_handle(handle
, prt
->source
, &entry
->link
.handle
);
118 entry
->link
.index
= prt
->source_index
;
123 * The 'source' field is NULL, and the 'source_index' field specifies
124 * the IRQ value, which is hardwired to specific interrupt inputs on
125 * the interrupt controller.
128 entry
->link
.index
= prt
->source_index
;
130 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO
,
131 " %02X:%02X:%02X[%c] -> %s[%d]\n",
132 entry
->id
.segment
, entry
->id
.bus
,
133 entry
->id
.device
, ('A' + entry
->pin
), prt
->source
,
136 spin_lock(&acpi_prt_lock
);
137 list_add_tail(&entry
->node
, &acpi_prt
.entries
);
139 spin_unlock(&acpi_prt_lock
);
145 acpi_pci_irq_del_entry(int segment
, int bus
, struct acpi_prt_entry
*entry
)
147 if (segment
== entry
->id
.segment
&& bus
== entry
->id
.bus
) {
149 list_del(&entry
->node
);
154 int acpi_pci_irq_add_prt(acpi_handle handle
, int segment
, int bus
)
156 acpi_status status
= AE_OK
;
157 char *pathname
= NULL
;
158 struct acpi_buffer buffer
= { 0, NULL
};
159 struct acpi_pci_routing_table
*prt
= NULL
;
160 struct acpi_pci_routing_table
*entry
= NULL
;
161 static int first_time
= 1;
164 pathname
= (char *)kmalloc(ACPI_PATHNAME_MAX
, GFP_KERNEL
);
167 memset(pathname
, 0, ACPI_PATHNAME_MAX
);
171 INIT_LIST_HEAD(&acpi_prt
.entries
);
176 * NOTE: We're given a 'handle' to the _PRT object's parent device
177 * (either a PCI root bridge or PCI-PCI bridge).
180 buffer
.length
= ACPI_PATHNAME_MAX
;
181 buffer
.pointer
= pathname
;
182 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
184 printk(KERN_DEBUG
"ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
188 * Evaluate this _PRT and add its entries to our global list (acpi_prt).
192 buffer
.pointer
= NULL
;
194 status
= acpi_get_irq_routing_table(handle
, &buffer
);
195 if (status
!= AE_BUFFER_OVERFLOW
) {
196 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRT [%s]",
197 acpi_format_exception(status
)));
201 prt
= kmalloc(buffer
.length
, GFP_KERNEL
);
205 memset(prt
, 0, buffer
.length
);
206 buffer
.pointer
= prt
;
208 status
= acpi_get_irq_routing_table(handle
, &buffer
);
209 if (ACPI_FAILURE(status
)) {
210 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRT [%s]",
211 acpi_format_exception(status
)));
212 kfree(buffer
.pointer
);
218 while (entry
&& (entry
->length
> 0)) {
219 acpi_pci_irq_add_entry(handle
, segment
, bus
, entry
);
220 entry
= (struct acpi_pci_routing_table
*)
221 ((unsigned long)entry
+ entry
->length
);
229 void acpi_pci_irq_del_prt(int segment
, int bus
)
231 struct list_head
*node
= NULL
, *n
= NULL
;
232 struct acpi_prt_entry
*entry
= NULL
;
234 if (!acpi_prt
.count
) {
239 "ACPI: Delete PCI Interrupt Routing Table for %x:%x\n", segment
,
241 spin_lock(&acpi_prt_lock
);
242 list_for_each_safe(node
, n
, &acpi_prt
.entries
) {
243 entry
= list_entry(node
, struct acpi_prt_entry
, node
);
245 acpi_pci_irq_del_entry(segment
, bus
, entry
);
247 spin_unlock(&acpi_prt_lock
);
250 /* --------------------------------------------------------------------------
251 PCI Interrupt Routing Support
252 -------------------------------------------------------------------------- */
253 typedef int (*irq_lookup_func
) (struct acpi_prt_entry
*, int *, int *, char **);
256 acpi_pci_allocate_irq(struct acpi_prt_entry
*entry
,
257 int *triggering
, int *polarity
, char **link
)
262 if (entry
->link
.handle
) {
263 irq
= acpi_pci_link_allocate_irq(entry
->link
.handle
,
264 entry
->link
.index
, triggering
,
267 printk(KERN_WARNING PREFIX
268 "Invalid IRQ link routing entry\n");
272 irq
= entry
->link
.index
;
273 *triggering
= ACPI_LEVEL_SENSITIVE
;
274 *polarity
= ACPI_ACTIVE_LOW
;
277 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found IRQ %d\n", irq
));
282 acpi_pci_free_irq(struct acpi_prt_entry
*entry
,
283 int *triggering
, int *polarity
, char **link
)
287 if (entry
->link
.handle
) {
288 irq
= acpi_pci_link_free_irq(entry
->link
.handle
);
290 irq
= entry
->link
.index
;
296 * acpi_pci_irq_lookup
297 * success: return IRQ >= 0
301 acpi_pci_irq_lookup(struct pci_bus
*bus
,
305 int *polarity
, char **link
, irq_lookup_func func
)
307 struct acpi_prt_entry
*entry
= NULL
;
308 int segment
= pci_domain_nr(bus
);
309 int bus_nr
= bus
->number
;
313 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
314 "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
315 segment
, bus_nr
, device
, ('A' + pin
)));
317 entry
= acpi_pci_irq_find_prt_entry(segment
, bus_nr
, device
, pin
);
319 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PRT entry not found\n"));
323 ret
= func(entry
, triggering
, polarity
, link
);
328 * acpi_pci_irq_derive
329 * success: return IRQ >= 0
330 * failure: return < 0
333 acpi_pci_irq_derive(struct pci_dev
*dev
,
336 int *polarity
, char **link
, irq_lookup_func func
)
338 struct pci_dev
*bridge
= dev
;
347 * Attempt to derive an IRQ for this device from a parent bridge's
348 * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
350 while (irq
< 0 && bridge
->bus
->self
) {
351 pin
= (pin
+ PCI_SLOT(bridge
->devfn
)) % 4;
352 bridge
= bridge
->bus
->self
;
354 if ((bridge
->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS
) {
355 /* PC card has the same IRQ as its cardbridge */
356 bridge_pin
= bridge
->pin
;
358 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
359 "No interrupt pin configured for device %s\n",
363 /* Pin is from 0 to 3 */
368 irq
= acpi_pci_irq_lookup(bridge
->bus
, PCI_SLOT(bridge
->devfn
),
369 pin
, triggering
, polarity
,
374 printk(KERN_WARNING PREFIX
"Unable to derive IRQ for device %s\n",
379 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Derive IRQ %d for device %s from %s\n",
380 irq
, pci_name(dev
), pci_name(bridge
)));
386 * acpi_pci_irq_enable
388 * failure: return < 0
391 int acpi_pci_irq_enable(struct pci_dev
*dev
)
395 int triggering
= ACPI_LEVEL_SENSITIVE
;
396 int polarity
= ACPI_ACTIVE_LOW
;
406 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
407 "No interrupt pin configured for device %s\n",
414 printk(KERN_ERR PREFIX
"Invalid (NULL) 'bus' field\n");
419 * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
420 * values override any BIOS-assigned IRQs set during boot.
422 irq
= acpi_pci_irq_lookup(dev
->bus
, PCI_SLOT(dev
->devfn
), pin
,
423 &triggering
, &polarity
, &link
,
424 acpi_pci_allocate_irq
);
427 * If no PRT entry was found, we'll try to derive an IRQ from the
428 * device's parent bridge.
431 irq
= acpi_pci_irq_derive(dev
, pin
, &triggering
,
433 acpi_pci_allocate_irq
);
436 * No IRQ known to the ACPI subsystem - maybe the BIOS /
437 * driver reported one, then use it. Exit in any case.
440 printk(KERN_WARNING PREFIX
"PCI Interrupt %s[%c]: no GSI",
441 pci_name(dev
), ('A' + pin
));
442 /* Interrupt Line values above 0xF are forbidden */
443 if (dev
->irq
> 0 && (dev
->irq
<= 0xF)) {
444 printk(" - using IRQ %d\n", dev
->irq
);
445 acpi_register_gsi(dev
->irq
, ACPI_LEVEL_SENSITIVE
,
454 rc
= acpi_register_gsi(irq
, triggering
, polarity
);
456 printk(KERN_WARNING PREFIX
"PCI Interrupt %s[%c]: failed "
457 "to register GSI\n", pci_name(dev
), ('A' + pin
));
462 printk(KERN_INFO PREFIX
"PCI Interrupt %s[%c] -> ",
463 pci_name(dev
), 'A' + pin
);
466 printk("Link [%s] -> ", link
);
468 printk("GSI %u (%s, %s) -> IRQ %d\n", irq
,
469 (triggering
== ACPI_LEVEL_SENSITIVE
) ? "level" : "edge",
470 (polarity
== ACPI_ACTIVE_LOW
) ? "low" : "high", dev
->irq
);
475 EXPORT_SYMBOL(acpi_pci_irq_enable
);
477 /* FIXME: implement x86/x86_64 version */
478 void __attribute__ ((weak
)) acpi_unregister_gsi(u32 i
)
482 void acpi_pci_irq_disable(struct pci_dev
*dev
)
486 int triggering
= ACPI_LEVEL_SENSITIVE
;
487 int polarity
= ACPI_ACTIVE_LOW
;
490 if (!dev
|| !dev
->bus
)
499 * First we check the PCI IRQ routing table (PRT) for an IRQ.
501 gsi
= acpi_pci_irq_lookup(dev
->bus
, PCI_SLOT(dev
->devfn
), pin
,
502 &triggering
, &polarity
, NULL
,
505 * If no PRT entry was found, we'll try to derive an IRQ from the
506 * device's parent bridge.
509 gsi
= acpi_pci_irq_derive(dev
, pin
,
510 &triggering
, &polarity
, NULL
,
516 * TBD: It might be worth clearing dev->irq by magic constant
517 * (e.g. PCI_UNDEFINED_IRQ).
520 printk(KERN_INFO PREFIX
"PCI interrupt for device %s disabled\n",
523 acpi_unregister_gsi(gsi
);