blob: aabf8a965aff469caaa0063209239b416e775c74 [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
Bjorn Helgaas063563b2008-12-08 21:30:51 -070072static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(struct pci_dev *dev,
73 int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Len Brown4be44fc2005-08-05 00:44:28 -040075 struct acpi_prt_entry *entry = NULL;
Bjorn Helgaas063563b2008-12-08 21:30:51 -070076 int segment = pci_domain_nr(dev->bus);
77 int bus = dev->bus->number;
78 int device = PCI_SLOT(dev->devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!acpi_prt.count)
Patrick Mocheld550d982006-06-27 00:41:40 -040081 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /*
84 * Parse through all PRT entries looking for a match on the specified
85 * PCI device's segment, bus, device, and pin (don't care about func).
86 *
87 */
88 spin_lock(&acpi_prt_lock);
Matthias Kaehlcke1a3b77a2008-02-04 23:31:20 -080089 list_for_each_entry(entry, &acpi_prt.entries, node) {
Len Brown4be44fc2005-08-05 00:44:28 -040090 if ((segment == entry->id.segment)
91 && (bus == entry->id.bus)
92 && (device == entry->id.device)
93 && (pin == entry->pin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_unlock(&acpi_prt_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -040095 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97 }
98
99 spin_unlock(&acpi_prt_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400100 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700103/* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
104static struct dmi_system_id medion_md9580[] = {
105 {
106 .ident = "Medion MD9580-F laptop",
107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
110 },
111 },
112 { }
113};
114
115/* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
116static struct dmi_system_id dell_optiplex[] = {
117 {
118 .ident = "Dell Optiplex GX1",
119 .matches = {
120 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
121 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex GX1 600S+"),
122 },
123 },
124 { }
125};
126
127/* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
128static struct dmi_system_id hp_t5710[] = {
129 {
130 .ident = "HP t5710",
131 .matches = {
132 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
133 DMI_MATCH(DMI_PRODUCT_NAME, "hp t5000 series"),
134 DMI_MATCH(DMI_BOARD_NAME, "098Ch"),
135 },
136 },
137 { }
138};
139
140struct prt_quirk {
141 struct dmi_system_id *system;
142 unsigned int segment;
143 unsigned int bus;
144 unsigned int device;
145 unsigned char pin;
146 char *source; /* according to BIOS */
147 char *actual_source;
148};
149
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700150#define PCI_INTX_PIN(c) (c - 'A' + 1)
151
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700152/*
153 * These systems have incorrect _PRT entries. The BIOS claims the PCI
154 * interrupt at the listed segment/bus/device/pin is connected to the first
155 * link device, but it is actually connected to the second.
156 */
157static struct prt_quirk prt_quirks[] = {
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700158 { medion_md9580, 0, 0, 9, PCI_INTX_PIN('A'),
Bjorn Helgaasb97d4802008-03-25 11:21:11 -0600159 "\\_SB_.PCI0.ISA_.LNKA",
160 "\\_SB_.PCI0.ISA_.LNKB"},
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700161 { dell_optiplex, 0, 0, 0xd, PCI_INTX_PIN('A'),
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700162 "\\_SB_.LNKB",
163 "\\_SB_.LNKA"},
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700164 { hp_t5710, 0, 0, 1, PCI_INTX_PIN('A'),
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700165 "\\_SB_.PCI0.LNK1",
166 "\\_SB_.PCI0.LNK3"},
167};
168
169static void
170do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
171{
172 int i;
173 struct prt_quirk *quirk;
174
175 for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
176 quirk = &prt_quirks[i];
177
178 /* All current quirks involve link devices, not GSIs */
179 if (!prt->source)
180 continue;
181
182 if (dmi_check_system(quirk->system) &&
183 entry->id.segment == quirk->segment &&
184 entry->id.bus == quirk->bus &&
185 entry->id.device == quirk->device &&
Bjorn Helgaasc4580332008-12-08 21:30:46 -0700186 entry->pin == quirk->pin &&
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700187 !strcmp(prt->source, quirk->source) &&
188 strlen(prt->source) >= strlen(quirk->actual_source)) {
189 printk(KERN_WARNING PREFIX "firmware reports "
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600190 "%04x:%02x:%02x PCI INT %c connected to %s; "
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700191 "changing to %s\n",
192 entry->id.segment, entry->id.bus,
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700193 entry->id.device, pin_name(entry->pin),
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700194 prt->source, quirk->actual_source);
195 strcpy(prt->source, quirk->actual_source);
196 }
197 }
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int
Len Brown4be44fc2005-08-05 00:44:28 -0400201acpi_pci_irq_add_entry(acpi_handle handle,
202 int segment, int bus, struct acpi_pci_routing_table *prt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Len Brown4be44fc2005-08-05 00:44:28 -0400204 struct acpi_prt_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Burman Yan36bcbec2006-12-19 12:56:11 -0800206 entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400208 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Bjorn Helgaase64e9db2008-12-08 21:30:41 -0700210 /*
211 * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
212 * 1=INTA, 2=INTB. We use the PCI encoding throughout, so convert
213 * it here.
214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 entry->id.segment = segment;
216 entry->id.bus = bus;
217 entry->id.device = (prt->address >> 16) & 0xFFFF;
Bjorn Helgaase64e9db2008-12-08 21:30:41 -0700218 entry->pin = prt->pin + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Bjorn Helgaas391df5d2008-03-11 13:45:15 -0700220 do_prt_fixups(entry, prt);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * Type 1: Dynamic
224 * ---------------
225 * The 'source' field specifies the PCI interrupt link device used to
226 * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
227 * indicates which resource descriptor in the resource template (of
228 * the link device) this interrupt is allocated from.
229 *
230 * NOTE: Don't query the Link Device for IRQ information at this time
231 * because Link Device enumeration may not have occurred yet
232 * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
233 * namespace).
234 */
235 if (prt->source[0]) {
236 acpi_get_handle(handle, prt->source, &entry->link.handle);
237 entry->link.index = prt->source_index;
238 }
239 /*
240 * Type 2: Static
241 * --------------
242 * The 'source' field is NULL, and the 'source_index' field specifies
243 * the IRQ value, which is hardwired to specific interrupt inputs on
244 * the interrupt controller.
245 */
246 else
247 entry->link.index = prt->source_index;
248
249 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700250 " %04x:%02x:%02x[%c] -> %s[%d]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400251 entry->id.segment, entry->id.bus,
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700252 entry->id.device, pin_name(entry->pin),
253 prt->source, entry->link.index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 spin_lock(&acpi_prt_lock);
256 list_add_tail(&entry->node, &acpi_prt.entries);
257 acpi_prt.count++;
258 spin_unlock(&acpi_prt_lock);
259
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static void
Len Brown4be44fc2005-08-05 00:44:28 -0400264acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Len Brown4be44fc2005-08-05 00:44:28 -0400266 if (segment == entry->id.segment && bus == entry->id.bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 acpi_prt.count--;
268 list_del(&entry->node);
269 kfree(entry);
270 }
271}
272
Len Brown4be44fc2005-08-05 00:44:28 -0400273int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700275 acpi_status status;
276 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
277 struct acpi_pci_routing_table *entry;
Len Brown4be44fc2005-08-05 00:44:28 -0400278 static int first_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (first_time) {
281 acpi_prt.count = 0;
282 INIT_LIST_HEAD(&acpi_prt.entries);
283 first_time = 0;
284 }
285
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700286 /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
287 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
288 if (ACPI_FAILURE(status))
289 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700292 (char *) buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700294 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700296 buffer.length = ACPI_ALLOCATE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 buffer.pointer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 status = acpi_get_irq_routing_table(handle, &buffer);
300 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400301 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
302 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400304 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700307 entry = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 while (entry && (entry->length > 0)) {
309 acpi_pci_irq_add_entry(handle, segment, bus, entry);
310 entry = (struct acpi_pci_routing_table *)
Len Brown4be44fc2005-08-05 00:44:28 -0400311 ((unsigned long)entry + entry->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
Bjorn Helgaas2320ac62008-12-08 21:30:15 -0700314 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Len Brown4be44fc2005-08-05 00:44:28 -0400318void acpi_pci_irq_del_prt(int segment, int bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Len Brown4be44fc2005-08-05 00:44:28 -0400320 struct list_head *node = NULL, *n = NULL;
321 struct acpi_prt_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Len Brown4be44fc2005-08-05 00:44:28 -0400323 if (!acpi_prt.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return;
325 }
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327 printk(KERN_DEBUG
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700328 "ACPI: Delete PCI Interrupt Routing Table for %04x:%02x\n",
329 segment, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 spin_lock(&acpi_prt_lock);
331 list_for_each_safe(node, n, &acpi_prt.entries) {
332 entry = list_entry(node, struct acpi_prt_entry, node);
333
334 acpi_pci_irq_del_entry(segment, bus, entry);
335 }
336 spin_unlock(&acpi_prt_lock);
337}
Len Brown4be44fc2005-08-05 00:44:28 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* --------------------------------------------------------------------------
340 PCI Interrupt Routing Support
341 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400342typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David Shaohua Li87bec662005-07-27 23:02:00 -0400344static int
345acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
Bob Moore50eca3e2005-09-30 19:03:00 -0400346 int *triggering, int *polarity, char **link)
David Shaohua Li87bec662005-07-27 23:02:00 -0400347{
Len Brown4be44fc2005-08-05 00:44:28 -0400348 int irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400349
David Shaohua Li87bec662005-07-27 23:02:00 -0400350
351 if (entry->link.handle) {
352 irq = acpi_pci_link_allocate_irq(entry->link.handle,
Bob Moore50eca3e2005-09-30 19:03:00 -0400353 entry->link.index, triggering,
354 polarity, link);
David Shaohua Li87bec662005-07-27 23:02:00 -0400355 if (irq < 0) {
Len Browncece9292006-06-26 23:04:31 -0400356 printk(KERN_WARNING PREFIX
357 "Invalid IRQ link routing entry\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400358 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400359 }
360 } else {
361 irq = entry->link.index;
Bob Moore50eca3e2005-09-30 19:03:00 -0400362 *triggering = ACPI_LEVEL_SENSITIVE;
363 *polarity = ACPI_ACTIVE_LOW;
David Shaohua Li87bec662005-07-27 23:02:00 -0400364 }
365
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700366 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found GSI %d\n", irq));
Patrick Mocheld550d982006-06-27 00:41:40 -0400367 return irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400368}
369
370static int
371acpi_pci_free_irq(struct acpi_prt_entry *entry,
Bob Moore50eca3e2005-09-30 19:03:00 -0400372 int *triggering, int *polarity, char **link)
David Shaohua Li87bec662005-07-27 23:02:00 -0400373{
Len Brown4be44fc2005-08-05 00:44:28 -0400374 int irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400375
David Shaohua Li87bec662005-07-27 23:02:00 -0400376 if (entry->link.handle) {
377 irq = acpi_pci_link_free_irq(entry->link.handle);
378 } else {
379 irq = entry->link.index;
380 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400381 return irq;
David Shaohua Li87bec662005-07-27 23:02:00 -0400382}
Len Brown4be44fc2005-08-05 00:44:28 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
385 * acpi_pci_irq_lookup
386 * success: return IRQ >= 0
387 * failure: return -1
388 */
389static int
Bjorn Helgaas063563b2008-12-08 21:30:51 -0700390acpi_pci_irq_lookup(struct pci_dev *dev, 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;
David Shaohua Li87bec662005-07-27 23:02:00 -0400395 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Bjorn Helgaas063563b2008-12-08 21:30:51 -0700398 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Searching for _PRT entry for %s[%c]\n",
399 pci_name(dev), pin_name(pin)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Bjorn Helgaas063563b2008-12-08 21:30:51 -0700401 entry = acpi_pci_irq_find_prt_entry(dev, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!entry) {
Bjorn Helgaas21a53282008-12-08 21:30:05 -0700403 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_PRT entry not found\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400404 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Len Brown4be44fc2005-08-05 00:44:28 -0400406
Bob Moore50eca3e2005-09-30 19:03:00 -0400407 ret = func(entry, triggering, polarity, link);
Patrick Mocheld550d982006-06-27 00:41:40 -0400408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
412 * acpi_pci_irq_derive
413 * success: return IRQ >= 0
414 * failure: return < 0
415 */
416static int
Len Brown4be44fc2005-08-05 00:44:28 -0400417acpi_pci_irq_derive(struct pci_dev *dev,
418 int pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400419 int *triggering,
420 int *polarity, char **link, irq_lookup_func func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Len Brown4be44fc2005-08-05 00:44:28 -0400422 struct pci_dev *bridge = dev;
423 int irq = -1;
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600424 u8 bridge_pin = 0, orig_pin = pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /*
428 * Attempt to derive an IRQ for this device from a parent bridge's
429 * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
430 */
431 while (irq < 0 && bridge->bus->self) {
Bjorn Helgaase64e9db2008-12-08 21:30:41 -0700432 pin = (((pin - 1) + PCI_SLOT(bridge->devfn)) % 4) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 bridge = bridge->bus->self;
434
435 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
436 /* PC card has the same IRQ as its cardbridge */
Kristen Accardi8015a012005-11-02 16:24:35 -0800437 bridge_pin = bridge->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!bridge_pin) {
Len Brown4be44fc2005-08-05 00:44:28 -0400439 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
440 "No interrupt pin configured for device %s\n",
441 pci_name(bridge)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 pin = bridge_pin;
445 }
446
Bjorn Helgaas063563b2008-12-08 21:30:51 -0700447 irq = acpi_pci_irq_lookup(bridge,
Bob Moore50eca3e2005-09-30 19:03:00 -0400448 pin, triggering, polarity,
Len Brown4be44fc2005-08-05 00:44:28 -0400449 link, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 if (irq < 0) {
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600453 dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700454 pin_name(orig_pin));
Patrick Mocheld550d982006-06-27 00:41:40 -0400455 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700458 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive GSI %d for device %s from %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400459 irq, pci_name(dev), pci_name(bridge)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Patrick Mocheld550d982006-06-27 00:41:40 -0400461 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/*
465 * acpi_pci_irq_enable
466 * success: return 0
467 * failure: return < 0
468 */
469
Len Brown4be44fc2005-08-05 00:44:28 -0400470int acpi_pci_irq_enable(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700472 int gsi = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400473 u8 pin = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400474 int triggering = ACPI_LEVEL_SENSITIVE;
475 int polarity = ACPI_ACTIVE_LOW;
Len Brown4be44fc2005-08-05 00:44:28 -0400476 char *link = NULL;
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600477 char link_desc[16];
Len Brown4be44fc2005-08-05 00:44:28 -0400478 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Kristen Accardi8015a012005-11-02 16:24:35 -0800481 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!pin) {
Len Brown4be44fc2005-08-05 00:44:28 -0400483 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
484 "No interrupt pin configured for device %s\n",
485 pci_name(dev)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
491 * values override any BIOS-assigned IRQs set during boot.
492 */
Bjorn Helgaas063563b2008-12-08 21:30:51 -0700493 gsi = acpi_pci_irq_lookup(dev, pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400494 &triggering, &polarity, &link,
Len Brown4be44fc2005-08-05 00:44:28 -0400495 acpi_pci_allocate_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /*
498 * If no PRT entry was found, we'll try to derive an IRQ from the
499 * device's parent bridge.
500 */
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700501 if (gsi < 0)
502 gsi = acpi_pci_irq_derive(dev, pin, &triggering,
Bob Moore50eca3e2005-09-30 19:03:00 -0400503 &polarity, &link,
Len Brown4be44fc2005-08-05 00:44:28 -0400504 acpi_pci_allocate_irq);
505
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700506 if (gsi < 0) {
Alan Cox96c2a872008-01-10 22:49:58 -0500507 /*
508 * IDE legacy mode controller IRQs are magic. Why do compat
509 * extensions always make such a nasty mess.
510 */
511 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
512 (dev->class & 0x05) == 0)
513 return 0;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * No IRQ known to the ACPI subsystem - maybe the BIOS /
517 * driver reported one, then use it. Exit in any case.
518 */
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700519 if (gsi < 0) {
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700520 dev_warn(&dev->dev, "PCI INT %c: no GSI", pin_name(pin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Interrupt Line values above 0xF are forbidden */
Linus Torvalds44f8e1a2005-07-02 10:35:33 -0700522 if (dev->irq > 0 && (dev->irq <= 0xF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 printk(" - using IRQ %d\n", dev->irq);
Len Brown4be44fc2005-08-05 00:44:28 -0400524 acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
525 ACPI_ACTIVE_LOW);
Patrick Mocheld550d982006-06-27 00:41:40 -0400526 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 printk("\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Len Brown4be44fc2005-08-05 00:44:28 -0400531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Bjorn Helgaasc13f8892008-12-08 21:30:26 -0700533 rc = acpi_register_gsi(gsi, triggering, polarity);
Kenji Kaneshige349f0d52005-07-28 14:42:00 -0400534 if (rc < 0) {
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600535 dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700536 pin_name(pin));
Patrick Mocheld550d982006-06-27 00:41:40 -0400537 return rc;
Kenji Kaneshige349f0d52005-07-28 14:42:00 -0400538 }
539 dev->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (link)
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600542 snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
543 else
544 link_desc[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600546 dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700547 pin_name(pin), link_desc, gsi,
Bjorn Helgaasc83642d2008-06-27 08:45:39 -0600548 (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
549 (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Patrick Mocheld550d982006-06-27 00:41:40 -0400551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
Len Brown4be44fc2005-08-05 00:44:28 -0400553
David Shaohua Li87bec662005-07-27 23:02:00 -0400554/* FIXME: implement x86/x86_64 version */
Len Brown4be44fc2005-08-05 00:44:28 -0400555void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Len Brown4be44fc2005-08-05 00:44:28 -0400557}
558
559void acpi_pci_irq_disable(struct pci_dev *dev)
560{
561 int gsi = 0;
562 u8 pin = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400563 int triggering = ACPI_LEVEL_SENSITIVE;
564 int polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Kristen Accardi8015a012005-11-02 16:24:35 -0800567 pin = dev->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!pin)
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
572 * First we check the PCI IRQ routing table (PRT) for an IRQ.
573 */
Bjorn Helgaas063563b2008-12-08 21:30:51 -0700574 gsi = acpi_pci_irq_lookup(dev, pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400575 &triggering, &polarity, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400576 acpi_pci_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * If no PRT entry was found, we'll try to derive an IRQ from the
579 * device's parent bridge.
580 */
581 if (gsi < 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400582 gsi = acpi_pci_irq_derive(dev, pin,
Bob Moore50eca3e2005-09-30 19:03:00 -0400583 &triggering, &polarity, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400584 acpi_pci_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (gsi < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400586 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /*
589 * TBD: It might be worth clearing dev->irq by magic constant
590 * (e.g. PCI_UNDEFINED_IRQ).
591 */
592
Bjorn Helgaascf68b802008-12-08 21:30:36 -0700593 dev_info(&dev->dev, "PCI INT %c disabled\n", pin_name(pin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 acpi_unregister_gsi(gsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}