blob: c4a5c49809db81f4458cf79722ae837a65f31ff8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Bjorn Helgaas391df5d2008-03-11 13:45:15 -070028#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050042ACPI_MODULE_NAME("pci_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Bjorn Helgaasf748baf2008-12-08 21:30:31 -070044struct acpi_prt_entry {
45 struct list_head node;
46 struct acpi_pci_id id;
47 u8 pin;
48 struct {
49 acpi_handle handle;
50 u32 index;
51 } link;
52 u32 irq;
53};
54
55struct acpi_prt_list {
56 int count;
57 struct list_head entries;
58};
59
Len Brown4be44fc2005-08-05 00:44:28 -040060static struct acpi_prt_list acpi_prt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static DEFINE_SPINLOCK(acpi_prt_lock);
62
Bjorn Helgaascf68b802008-12-08 21:30:36 -070063static inline char pin_name(int pin)
64{
Bjorn Helgaase64e9db2008-12-08 21:30:41 -070065 return 'A' + pin - 1;
Bjorn Helgaascf68b802008-12-08 21:30:36 -070066}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* --------------------------------------------------------------------------
69 PCI IRQ Routing Table (PRT) Support
70 -------------------------------------------------------------------------- */
71
Len Brown4be44fc2005-08-05 00:44:28 -040072static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
73 int bus,
74 int device, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Len Brown4be44fc2005-08-05 00:44:28 -040076 struct acpi_prt_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!acpi_prt.count)
Patrick Mocheld550d982006-06-27 00:41:40 -040079 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /*
82 * Parse through all PRT entries looking for a match on the specified
83 * PCI device's segment, bus, device, and pin (don't care about func).
84 *
85 */
86 spin_lock(&acpi_prt_lock);
Matthias Kaehlcke1a3b77a2008-02-04 23:31:20 -080087 list_for_each_entry(entry, &acpi_prt.entries, node) {
Len Brown4be44fc2005-08-05 00:44:28 -040088 if ((segment == entry->id.segment)
89 && (bus == entry->id.bus)
90 && (device == entry->id.device)
91 && (pin == entry->pin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 spin_unlock(&acpi_prt_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -040093 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 }
96
97 spin_unlock(&acpi_prt_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -040098 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700101/* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
102static struct dmi_system_id medion_md9580[] = {
103 {
104 .ident = "Medion MD9580-F laptop",
105 .matches = {
106 DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
107 DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
108 },
109 },
110 { }
111};
112
113/* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
114static struct dmi_system_id dell_optiplex[] = {
115 {
116 .ident = "Dell Optiplex GX1",
117 .matches = {
118 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
119 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex GX1 600S+"),
120 },
121 },
122 { }
123};
124
125/* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
126static struct dmi_system_id hp_t5710[] = {
127 {
128 .ident = "HP t5710",
129 .matches = {
130 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
131 DMI_MATCH(DMI_PRODUCT_NAME, "hp t5000 series"),
132 DMI_MATCH(DMI_BOARD_NAME, "098Ch"),
133 },
134 },
135 { }
136};
137
138struct prt_quirk {
139 struct dmi_system_id *system;
140 unsigned int segment;
141 unsigned int bus;
142 unsigned int device;
143 unsigned char pin;
144 char *source; /* according to BIOS */
145 char *actual_source;
146};
147
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700148#define PCI_INTX_PIN(c) (c - 'A' + 1)
149
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700150/*
151 * These systems have incorrect _PRT entries. The BIOS claims the PCI
152 * interrupt at the listed segment/bus/device/pin is connected to the first
153 * link device, but it is actually connected to the second.
154 */
155static struct prt_quirk prt_quirks[] = {
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700156 { medion_md9580, 0, 0, 9, PCI_INTX_PIN('A'),
Bjorn Helgaasb97d4802008-03-25 11:21:11 -0600157 "\\_SB_.PCI0.ISA_.LNKA",
158 "\\_SB_.PCI0.ISA_.LNKB"},
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700159 { dell_optiplex, 0, 0, 0xd, PCI_INTX_PIN('A'),
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700160 "\\_SB_.LNKB",
161 "\\_SB_.LNKA"},
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700162 { hp_t5710, 0, 0, 1, PCI_INTX_PIN('A'),
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700163 "\\_SB_.PCI0.LNK1",
164 "\\_SB_.PCI0.LNK3"},
165};
166
167static void
168do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
169{
170 int i;
171 struct prt_quirk *quirk;
172
173 for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
174 quirk = &prt_quirks[i];
175
176 /* All current quirks involve link devices, not GSIs */
177 if (!prt->source)
178 continue;
179
180 if (dmi_check_system(quirk->system) &&
181 entry->id.segment == quirk->segment &&
182 entry->id.bus == quirk->bus &&
183 entry->id.device == quirk->device &&
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700184 entry->pin == quirk->pin &&
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700185 !strcmp(prt->source, quirk->source) &&
186 strlen(prt->source) >= strlen(quirk->actual_source)) {
187 printk(KERN_WARNING PREFIX "firmware reports "
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600188 "%04x:%02x:%02x PCI INT %c connected to %s; "
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700189 "changing to %s\n",
190 entry->id.segment, entry->id.bus,
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700191 entry->id.device, pin_name(entry->pin),
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700192 prt->source, quirk->actual_source);
193 strcpy(prt->source, quirk->actual_source);
194 }
195 }
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int
Len Brown4be44fc2005-08-05 00:44:28 -0400199acpi_pci_irq_add_entry(acpi_handle handle,
200 int segment, int bus, struct acpi_pci_routing_table *prt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Len Brown4be44fc2005-08-05 00:44:28 -0400202 struct acpi_prt_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Burman Yan36bcbec2006-12-19 12:56:11 -0800204 entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400206 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Bjorn Helgaase64e9db2008-12-08 21:30:41 -0700208 /*
209 * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
210 * 1=INTA, 2=INTB. We use the PCI encoding throughout, so convert
211 * it here.
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 entry->id.segment = segment;
214 entry->id.bus = bus;
215 entry->id.device = (prt->address >> 16) & 0xFFFF;
Bjorn Helgaase64e9db2008-12-08 21:30:41 -0700216 entry->pin = prt->pin + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700218 do_prt_fixups(entry, prt);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
221 * Type 1: Dynamic
222 * ---------------
223 * The 'source' field specifies the PCI interrupt link device used to
224 * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
225 * indicates which resource descriptor in the resource template (of
226 * the link device) this interrupt is allocated from.
227 *
228 * NOTE: Don't query the Link Device for IRQ information at this time
229 * because Link Device enumeration may not have occurred yet
230 * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
231 * namespace).
232 */
233 if (prt->source[0]) {
234 acpi_get_handle(handle, prt->source, &entry->link.handle);
235 entry->link.index = prt->source_index;
236 }
237 /*
238 * Type 2: Static
239 * --------------
240 * The 'source' field is NULL, and the 'source_index' field specifies
241 * the IRQ value, which is hardwired to specific interrupt inputs on
242 * the interrupt controller.
243 */
244 else
245 entry->link.index = prt->source_index;
246
247 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700248 " %04x:%02x:%02x[%c] -> %s[%d]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400249 entry->id.segment, entry->id.bus,
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700250 entry->id.device, pin_name(entry->pin),
251 prt->source, entry->link.index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 spin_lock(&acpi_prt_lock);
254 list_add_tail(&entry->node, &acpi_prt.entries);
255 acpi_prt.count++;
256 spin_unlock(&acpi_prt_lock);
257
Patrick Mocheld550d982006-06-27 00:41:40 -0400258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static void
Len Brown4be44fc2005-08-05 00:44:28 -0400262acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Len Brown4be44fc2005-08-05 00:44:28 -0400264 if (segment == entry->id.segment && bus == entry->id.bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 acpi_prt.count--;
266 list_del(&entry->node);
267 kfree(entry);
268 }
269}
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700273 acpi_status status;
274 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
275 struct acpi_pci_routing_table *entry;
Len Brown4be44fc2005-08-05 00:44:28 -0400276 static int first_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (first_time) {
279 acpi_prt.count = 0;
280 INIT_LIST_HEAD(&acpi_prt.entries);
281 first_time = 0;
282 }
283
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700284 /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
285 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
286 if (ACPI_FAILURE(status))
287 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700290 (char *) buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700292 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700294 buffer.length = ACPI_ALLOCATE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 buffer.pointer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 status = acpi_get_irq_routing_table(handle, &buffer);
298 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400299 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
300 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400302 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700305 entry = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 while (entry && (entry->length > 0)) {
307 acpi_pci_irq_add_entry(handle, segment, bus, entry);
308 entry = (struct acpi_pci_routing_table *)
Len Brown4be44fc2005-08-05 00:44:28 -0400309 ((unsigned long)entry + entry->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700312 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Len Brown4be44fc2005-08-05 00:44:28 -0400316void acpi_pci_irq_del_prt(int segment, int bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Len Brown4be44fc2005-08-05 00:44:28 -0400318 struct list_head *node = NULL, *n = NULL;
319 struct acpi_prt_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Len Brown4be44fc2005-08-05 00:44:28 -0400321 if (!acpi_prt.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return;
323 }
324
Len Brown4be44fc2005-08-05 00:44:28 -0400325 printk(KERN_DEBUG
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700326 "ACPI: Delete PCI Interrupt Routing Table for %04x:%02x\n",
327 segment, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 spin_lock(&acpi_prt_lock);
329 list_for_each_safe(node, n, &acpi_prt.entries) {
330 entry = list_entry(node, struct acpi_prt_entry, node);
331
332 acpi_pci_irq_del_entry(segment, bus, entry);
333 }
334 spin_unlock(&acpi_prt_lock);
335}
Len Brown4be44fc2005-08-05 00:44:28 -0400336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/* --------------------------------------------------------------------------
338 PCI Interrupt Routing Support
339 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400340typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David Shaohua Li87bec662005-07-27 23:02:00 -0400342static int
343acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
Bob Moore50eca3e2005-09-30 19:03:00 -0400344 int *triggering, int *polarity, char **link)
David Shaohua Li87bec662005-07-27 23:02:00 -0400345{
Len Brown4be44fc2005-08-05 00:44:28 -0400346 int irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400347
David Shaohua Li87bec662005-07-27 23:02:00 -0400348
349 if (entry->link.handle) {
350 irq = acpi_pci_link_allocate_irq(entry->link.handle,
Bob Moore50eca3e2005-09-30 19:03:00 -0400351 entry->link.index, triggering,
352 polarity, link);
David Shaohua Li87bec662005-07-27 23:02:00 -0400353 if (irq < 0) {
Len Browncece9292006-06-26 23:04:31 -0400354 printk(KERN_WARNING PREFIX
355 "Invalid IRQ link routing entry\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400356 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400357 }
358 } else {
359 irq = entry->link.index;
Bob Moore50eca3e2005-09-30 19:03:00 -0400360 *triggering = ACPI_LEVEL_SENSITIVE;
361 *polarity = ACPI_ACTIVE_LOW;
David Shaohua Li87bec662005-07-27 23:02:00 -0400362 }
363
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700364 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found GSI %d\n", irq));
Patrick Mocheld550d982006-06-27 00:41:40 -0400365 return irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400366}
367
368static int
369acpi_pci_free_irq(struct acpi_prt_entry *entry,
Bob Moore50eca3e2005-09-30 19:03:00 -0400370 int *triggering, int *polarity, char **link)
David Shaohua Li87bec662005-07-27 23:02:00 -0400371{
Len Brown4be44fc2005-08-05 00:44:28 -0400372 int irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400373
David Shaohua Li87bec662005-07-27 23:02:00 -0400374 if (entry->link.handle) {
375 irq = acpi_pci_link_free_irq(entry->link.handle);
376 } else {
377 irq = entry->link.index;
378 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400379 return irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400380}
Len Brown4be44fc2005-08-05 00:44:28 -0400381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * acpi_pci_irq_lookup
384 * success: return IRQ >= 0
385 * failure: return -1
386 */
387static int
Len Brown4be44fc2005-08-05 00:44:28 -0400388acpi_pci_irq_lookup(struct pci_bus *bus,
389 int device,
390 int pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400391 int *triggering,
392 int *polarity, char **link, irq_lookup_func func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Len Brown4be44fc2005-08-05 00:44:28 -0400394 struct acpi_prt_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int segment = pci_domain_nr(bus);
396 int bus_nr = bus->number;
David Shaohua Li87bec662005-07-27 23:02:00 -0400397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700401 "Searching for _PRT entry for %04x:%02x:%02x[%c]\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700402 segment, bus_nr, device, pin_name(pin)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Len Brown4be44fc2005-08-05 00:44:28 -0400404 entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!entry) {
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700406 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_PRT entry not found\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400407 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Len Brown4be44fc2005-08-05 00:44:28 -0400409
Bob Moore50eca3e2005-09-30 19:03:00 -0400410 ret = func(entry, triggering, polarity, link);
Patrick Mocheld550d982006-06-27 00:41:40 -0400411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/*
415 * acpi_pci_irq_derive
416 * success: return IRQ >= 0
417 * failure: return < 0
418 */
419static int
Len Brown4be44fc2005-08-05 00:44:28 -0400420acpi_pci_irq_derive(struct pci_dev *dev,
421 int pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400422 int *triggering,
423 int *polarity, char **link, irq_lookup_func func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Len Brown4be44fc2005-08-05 00:44:28 -0400425 struct pci_dev *bridge = dev;
426 int irq = -1;
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600427 u8 bridge_pin = 0, orig_pin = pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
431 * Attempt to derive an IRQ for this device from a parent bridge's
432 * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
433 */
434 while (irq < 0 && bridge->bus->self) {
Bjorn Helgaase64e9db2008-12-08 21:30:41 -0700435 pin = (((pin - 1) + PCI_SLOT(bridge->devfn)) % 4) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 bridge = bridge->bus->self;
437
438 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
439 /* PC card has the same IRQ as its cardbridge */
Kristen Accardi8015a012005-11-02 16:24:35 -0800440 bridge_pin = bridge->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!bridge_pin) {
Len Brown4be44fc2005-08-05 00:44:28 -0400442 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
443 "No interrupt pin configured for device %s\n",
444 pci_name(bridge)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400445 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 pin = bridge_pin;
448 }
449
450 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
Bob Moore50eca3e2005-09-30 19:03:00 -0400451 pin, triggering, polarity,
Len Brown4be44fc2005-08-05 00:44:28 -0400452 link, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 if (irq < 0) {
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600456 dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700457 pin_name(orig_pin));
Patrick Mocheld550d982006-06-27 00:41:40 -0400458 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700461 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive GSI %d for device %s from %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400462 irq, pci_name(dev), pci_name(bridge)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Patrick Mocheld550d982006-06-27 00:41:40 -0400464 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/*
468 * acpi_pci_irq_enable
469 * success: return 0
470 * failure: return < 0
471 */
472
Len Brown4be44fc2005-08-05 00:44:28 -0400473int acpi_pci_irq_enable(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700475 int gsi = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400476 u8 pin = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400477 int triggering = ACPI_LEVEL_SENSITIVE;
478 int polarity = ACPI_ACTIVE_LOW;
Len Brown4be44fc2005-08-05 00:44:28 -0400479 char *link = NULL;
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600480 char link_desc[16];
Len Brown4be44fc2005-08-05 00:44:28 -0400481 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Kristen Accardi8015a012005-11-02 16:24:35 -0800484 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!pin) {
Len Brown4be44fc2005-08-05 00:44:28 -0400486 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
487 "No interrupt pin configured for device %s\n",
488 pci_name(dev)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
494 * values override any BIOS-assigned IRQs set during boot.
495 */
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700496 gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400497 &triggering, &polarity, &link,
Len Brown4be44fc2005-08-05 00:44:28 -0400498 acpi_pci_allocate_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /*
501 * If no PRT entry was found, we'll try to derive an IRQ from the
502 * device's parent bridge.
503 */
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700504 if (gsi < 0)
505 gsi = acpi_pci_irq_derive(dev, pin, &triggering,
Bob Moore50eca3e2005-09-30 19:03:00 -0400506 &polarity, &link,
Len Brown4be44fc2005-08-05 00:44:28 -0400507 acpi_pci_allocate_irq);
508
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700509 if (gsi < 0) {
Alan Cox96c2a872008-01-10 22:49:58 -0500510 /*
511 * IDE legacy mode controller IRQs are magic. Why do compat
512 * extensions always make such a nasty mess.
513 */
514 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
515 (dev->class & 0x05) == 0)
516 return 0;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * No IRQ known to the ACPI subsystem - maybe the BIOS /
520 * driver reported one, then use it. Exit in any case.
521 */
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700522 if (gsi < 0) {
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700523 dev_warn(&dev->dev, "PCI INT %c: no GSI", pin_name(pin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Interrupt Line values above 0xF are forbidden */
Linus Torvalds44f8e1a2005-07-02 10:35:33 -0700525 if (dev->irq > 0 && (dev->irq <= 0xF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 printk(" - using IRQ %d\n", dev->irq);
Len Brown4be44fc2005-08-05 00:44:28 -0400527 acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
528 ACPI_ACTIVE_LOW);
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 printk("\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Len Brown4be44fc2005-08-05 00:44:28 -0400534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700536 rc = acpi_register_gsi(gsi, triggering, polarity);
Kenji Kaneshige349f0d52005-07-28 14:42:00 -0400537 if (rc < 0) {
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600538 dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700539 pin_name(pin));
Patrick Mocheld550d982006-06-27 00:41:40 -0400540 return rc;
Kenji Kaneshige349f0d52005-07-28 14:42:00 -0400541 }
542 dev->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (link)
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600545 snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
546 else
547 link_desc[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600549 dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700550 pin_name(pin), link_desc, gsi,
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600551 (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
552 (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Len Brown4be44fc2005-08-05 00:44:28 -0400556
David Shaohua Li87bec662005-07-27 23:02:00 -0400557/* FIXME: implement x86/x86_64 version */
Len Brown4be44fc2005-08-05 00:44:28 -0400558void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown4be44fc2005-08-05 00:44:28 -0400560}
561
562void acpi_pci_irq_disable(struct pci_dev *dev)
563{
564 int gsi = 0;
565 u8 pin = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400566 int triggering = ACPI_LEVEL_SENSITIVE;
567 int polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Kristen Accardi8015a012005-11-02 16:24:35 -0800570 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!pin)
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /*
575 * First we check the PCI IRQ routing table (PRT) for an IRQ.
576 */
Len Brown4be44fc2005-08-05 00:44:28 -0400577 gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400578 &triggering, &polarity, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400579 acpi_pci_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
581 * If no PRT entry was found, we'll try to derive an IRQ from the
582 * device's parent bridge.
583 */
584 if (gsi < 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400585 gsi = acpi_pci_irq_derive(dev, pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400586 &triggering, &polarity, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400587 acpi_pci_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (gsi < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /*
592 * TBD: It might be worth clearing dev->irq by magic constant
593 * (e.g. PCI_UNDEFINED_IRQ).
594 */
595
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700596 dev_info(&dev->dev, "PCI INT %c disabled\n", pin_name(pin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 acpi_unregister_gsi(gsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}