blob: acc594771379af521ef06a5f7a549ca781583d8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
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 * TBD:
27 * 1. Support more than one IRQ resource entry per link device (index).
28 * 2. Implement start/stop mechanism and use ACPI Bus Driver facilities
29 * for IRQ management (e.g. start()->_SRS).
30 */
31
32#include <linux/sysdev.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/types.h>
37#include <linux/proc_fs.h>
38#include <linux/spinlock.h>
39#include <linux/pm.h>
40#include <linux/pci.h>
Ingo Molnar36e43092006-04-27 05:25:00 -040041#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050047ACPI_MODULE_NAME("pci_link");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
49#define ACPI_PCI_LINK_HID "PNP0C0F"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
51#define ACPI_PCI_LINK_FILE_INFO "info"
52#define ACPI_PCI_LINK_FILE_STATUS "state"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_PCI_LINK_MAX_POSSIBLE 16
Len Brown4be44fc2005-08-05 00:44:28 -040054static int acpi_pci_link_add(struct acpi_device *device);
55static int acpi_pci_link_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct acpi_driver acpi_pci_link_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050058 .name = "pci_link",
Len Brown4be44fc2005-08-05 00:44:28 -040059 .class = ACPI_PCI_LINK_CLASS,
60 .ids = ACPI_PCI_LINK_HID,
61 .ops = {
62 .add = acpi_pci_link_add,
63 .remove = acpi_pci_link_remove,
64 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
David Shaohua Li87bec662005-07-27 23:02:00 -040067/*
68 * If a link is initialized, we never change its active and initialized
69 * later even the link is disable. Instead, we just repick the active irq
70 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct acpi_pci_link_irq {
Len Brown4be44fc2005-08-05 00:44:28 -040072 u8 active; /* Current IRQ */
Bob Moore50eca3e2005-09-30 19:03:00 -040073 u8 triggering; /* All IRQs */
74 u8 polarity; /* All IRQs */
Len Brown4be44fc2005-08-05 00:44:28 -040075 u8 resource_type;
76 u8 possible_count;
77 u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
78 u8 initialized:1;
79 u8 reserved:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
82struct acpi_pci_link {
Len Brown4be44fc2005-08-05 00:44:28 -040083 struct list_head node;
84 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct acpi_pci_link_irq irq;
Len Brown4be44fc2005-08-05 00:44:28 -040086 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
89static struct {
Len Brown4be44fc2005-08-05 00:44:28 -040090 int count;
91 struct list_head entries;
92} acpi_link;
Ingo Molnar36e43092006-04-27 05:25:00 -040093DEFINE_MUTEX(acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* --------------------------------------------------------------------------
96 PCI Link Device Management
97 -------------------------------------------------------------------------- */
98
99/*
100 * set context (link) possible list from resource list
101 */
102static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400103acpi_pci_link_check_possible(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200105 struct acpi_pci_link *link = context;
Len Brown4be44fc2005-08-05 00:44:28 -0400106 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Len Browneca008c2005-09-22 00:25:18 -0400109 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400110 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Patrick Mocheld550d982006-06-27 00:41:40 -0400111 return AE_OK;
Bob Moore50eca3e2005-09-30 19:03:00 -0400112 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400113 {
114 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400115 if (!p || !p->interrupt_count) {
Len Browncece9292006-06-26 23:04:31 -0400116 printk(KERN_WARNING PREFIX "Blank IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400117 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Len Brown4be44fc2005-08-05 00:44:28 -0400119 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400120 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400121 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
122 if (!p->interrupts[i]) {
Len Browncece9292006-06-26 23:04:31 -0400123 printk(KERN_WARNING PREFIX "Invalid IRQ %d\n",
124 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400125 continue;
126 }
127 link->irq.possible[i] = p->interrupts[i];
128 link->irq.possible_count++;
129 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400130 link->irq.triggering = p->triggering;
131 link->irq.polarity = p->polarity;
132 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400135 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400136 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400137 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400138 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400139 if (!p || !p->interrupt_count) {
Len Browncece9292006-06-26 23:04:31 -0400140 printk(KERN_WARNING PREFIX
141 "Blank EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400142 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Len Brown4be44fc2005-08-05 00:44:28 -0400144 for (i = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400145 (i < p->interrupt_count
Len Brown4be44fc2005-08-05 00:44:28 -0400146 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
147 if (!p->interrupts[i]) {
Len Browncece9292006-06-26 23:04:31 -0400148 printk(KERN_WARNING PREFIX "Invalid IRQ %d\n",
149 p->interrupts[i]);
Len Brown4be44fc2005-08-05 00:44:28 -0400150 continue;
151 }
152 link->irq.possible[i] = p->interrupts[i];
153 link->irq.possible_count++;
154 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400155 link->irq.triggering = p->triggering;
156 link->irq.polarity = p->polarity;
157 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 default:
Len Brown64684632006-06-26 23:41:38 -0400161 printk(KERN_ERR PREFIX "Resource is not an IRQ entry\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400162 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Len Brown4be44fc2005-08-05 00:44:28 -0400168static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Len Brown4be44fc2005-08-05 00:44:28 -0400170 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Patrick Mochel67a71362006-05-19 16:54:42 -0400176 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400177 acpi_pci_link_check_possible, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400179 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400180 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
184 "Found %d possible IRQs\n",
185 link->irq.possible_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Patrick Mocheld550d982006-06-27 00:41:40 -0400187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400191acpi_pci_link_check_current(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Len Brown4be44fc2005-08-05 00:44:28 -0400193 int *irq = (int *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Len Browneca008c2005-09-22 00:25:18 -0400196 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400197 case ACPI_RESOURCE_TYPE_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400198 {
199 struct acpi_resource_irq *p = &resource->data.irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400200 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 /*
202 * IRQ descriptors may have no IRQ# bits set,
203 * particularly those those w/ _STA disabled
204 */
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
206 "Blank IRQ resource\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400207 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400208 }
209 *irq = p->interrupts[0];
210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400212 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Brown4be44fc2005-08-05 00:44:28 -0400213 {
Bob Moore50eca3e2005-09-30 19:03:00 -0400214 struct acpi_resource_extended_irq *p =
Len Brown4be44fc2005-08-05 00:44:28 -0400215 &resource->data.extended_irq;
Bob Moore50eca3e2005-09-30 19:03:00 -0400216 if (!p || !p->interrupt_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400217 /*
218 * extended IRQ descriptors must
219 * return at least 1 IRQ
220 */
Len Browncece9292006-06-26 23:04:31 -0400221 printk(KERN_WARNING PREFIX
222 "Blank EXT IRQ resource\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400223 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400224 }
225 *irq = p->interrupts[0];
226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Len Brownd4ec6c72006-01-26 17:23:38 -0500228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 default:
Len Brown64684632006-06-26 23:41:38 -0400230 printk(KERN_ERR PREFIX "Resource %d isn't an IRQ\n", resource->type);
Len Brownd4ec6c72006-01-26 17:23:38 -0500231 case ACPI_RESOURCE_TYPE_END_TAG:
Patrick Mocheld550d982006-06-27 00:41:40 -0400232 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/*
238 * Run _CRS and set link->irq.active
239 *
240 * return value:
241 * 0 - success
242 * !0 - failure
243 */
Len Brown4be44fc2005-08-05 00:44:28 -0400244static int acpi_pci_link_get_current(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Len Brown4be44fc2005-08-05 00:44:28 -0400246 int result = 0;
247 acpi_status status = AE_OK;
248 int irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Patrick Mochele0e4e112006-05-19 16:54:50 -0400250 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 link->irq.active = 0;
254
255 /* in practice, status disabled is meaningless, ignore it */
256 if (acpi_strict) {
257 /* Query _STA, set link->device->status */
258 result = acpi_bus_get_status(link->device);
259 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400260 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto end;
262 }
263
264 if (!link->device->status.enabled) {
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 }
269
270 /*
271 * Query and parse _CRS to get the current IRQ assignment.
272 */
273
Patrick Mochel67a71362006-05-19 16:54:42 -0400274 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
Len Brown4be44fc2005-08-05 00:44:28 -0400275 acpi_pci_link_check_current, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400277 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 result = -ENODEV;
279 goto end;
280 }
281
282 if (acpi_strict && !irq) {
Len Brown64684632006-06-26 23:41:38 -0400283 printk(KERN_ERR PREFIX "_CRS returned 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 result = -ENODEV;
285 }
286
287 link->irq.active = irq;
288
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Len Brown4be44fc2005-08-05 00:44:28 -0400295static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Len Brown4be44fc2005-08-05 00:44:28 -0400297 int result = 0;
298 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct {
Len Brown4be44fc2005-08-05 00:44:28 -0400300 struct acpi_resource res;
301 struct acpi_resource end;
302 } *resource;
303 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!link || !irq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Burman Yan36bcbec2006-12-19 12:56:11 -0800309 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400310 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Len Brown4be44fc2005-08-05 00:44:28 -0400313 buffer.length = sizeof(*resource) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 buffer.pointer = resource;
315
Len Brown4be44fc2005-08-05 00:44:28 -0400316 switch (link->irq.resource_type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400317 case ACPI_RESOURCE_TYPE_IRQ:
318 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 resource->res.length = sizeof(struct acpi_resource);
Bob Moore50eca3e2005-09-30 19:03:00 -0400320 resource->res.data.irq.triggering = link->irq.triggering;
321 resource->res.data.irq.polarity =
322 link->irq.polarity;
323 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
324 resource->res.data.irq.sharable =
Len Brown4be44fc2005-08-05 00:44:28 -0400325 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400327 resource->res.data.irq.sharable = ACPI_SHARED;
328 resource->res.data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 resource->res.data.irq.interrupts[0] = irq;
330 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400331
Bob Moore50eca3e2005-09-30 19:03:00 -0400332 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
333 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 resource->res.length = sizeof(struct acpi_resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400335 resource->res.data.extended_irq.producer_consumer =
336 ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400337 resource->res.data.extended_irq.triggering =
338 link->irq.triggering;
339 resource->res.data.extended_irq.polarity =
340 link->irq.polarity;
341 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
342 resource->res.data.irq.sharable =
Len Brown4be44fc2005-08-05 00:44:28 -0400343 ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400345 resource->res.data.irq.sharable = ACPI_SHARED;
346 resource->res.data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 resource->res.data.extended_irq.interrupts[0] = irq;
348 /* ignore resource_source, it's optional */
349 break;
350 default:
Len Brown64684632006-06-26 23:41:38 -0400351 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 result = -EINVAL;
353 goto end;
354
355 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400356 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /* Attempt to set the resource */
Patrick Mochel67a71362006-05-19 16:54:42 -0400359 status = acpi_set_current_resources(link->device->handle, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* check for total failure */
362 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400363 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 result = -ENODEV;
365 goto end;
366 }
367
368 /* Query _STA, set device->status */
369 result = acpi_bus_get_status(link->device);
370 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400371 printk(KERN_ERR PREFIX "Unable to read status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 goto end;
373 }
374 if (!link->device->status.enabled) {
Len Browncece9292006-06-26 23:04:31 -0400375 printk(KERN_WARNING PREFIX
376 "%s [%s] disabled and referenced, BIOS bug\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400377 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400378 acpi_device_bid(link->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 /* Query _CRS, set link->irq.active */
382 result = acpi_pci_link_get_current(link);
383 if (result) {
384 goto end;
385 }
386
387 /*
388 * Is current setting not what we set?
389 * set link->irq.active
390 */
391 if (link->irq.active != irq) {
392 /*
393 * policy: when _CRS doesn't return what we just _SRS
394 * assume _SRS worked and override _CRS value.
395 */
Len Browncece9292006-06-26 23:04:31 -0400396 printk(KERN_WARNING PREFIX
397 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400398 acpi_device_name(link->device),
Len Browncece9292006-06-26 23:04:31 -0400399 acpi_device_bid(link->device), link->irq.active, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 link->irq.active = irq;
401 }
402
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
Len Brown4be44fc2005-08-05 00:44:28 -0400404
405 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 kfree(resource);
Patrick Mocheld550d982006-06-27 00:41:40 -0400407 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* --------------------------------------------------------------------------
411 PCI Link IRQ Management
412 -------------------------------------------------------------------------- */
413
414/*
415 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
416 * Link Devices to move the PIRQs around to minimize sharing.
417 *
418 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
419 * that the BIOS has already set to active. This is necessary because
420 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
421 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
422 * even if acpi_irq_nobalance is set.
423 *
424 * A tables of penalties avoids directing PCI interrupts to well known
425 * ISA IRQs. Boot params are available to over-ride the default table:
426 *
427 * List interrupts that are free for PCI use.
428 * acpi_irq_pci=n[,m]
429 *
430 * List interrupts that should not be used for PCI:
431 * acpi_irq_isa=n[,m]
432 *
433 * Note that PCI IRQ routers have a list of possible IRQs,
434 * which may not include the IRQs this table says are available.
435 *
436 * Since this heuristic can't tell the difference between a link
437 * that no device will attach to, vs. a link which may be shared
438 * by multiple active devices -- it is not optimal.
439 *
440 * If interrupt performance is that important, get an IO-APIC system
441 * with a pin dedicated to each device. Or for that matter, an MSI
442 * enabled system.
443 */
444
445#define ACPI_MAX_IRQS 256
446#define ACPI_MAX_ISA_IRQ 16
447
448#define PIRQ_PENALTY_PCI_AVAILABLE (0)
449#define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
450#define PIRQ_PENALTY_PCI_USING (16*16*16)
451#define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
452#define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
453#define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
454
455static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
456 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
457 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
458 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
Len Brown4be44fc2005-08-05 00:44:28 -0400459 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
460 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
462 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
463 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
464 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
465 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9 PCI, often acpi */
466 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
467 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
468 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
469 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
470 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
471 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
Len Brown4be44fc2005-08-05 00:44:28 -0400472 /* >IRQ15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
Len Brown4be44fc2005-08-05 00:44:28 -0400475int __init acpi_irq_penalty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Len Brown4be44fc2005-08-05 00:44:28 -0400477 struct list_head *node = NULL;
478 struct acpi_pci_link *link = NULL;
479 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /*
483 * Update penalties to facilitate IRQ balancing.
484 */
485 list_for_each(node, &acpi_link.entries) {
486
487 link = list_entry(node, struct acpi_pci_link, node);
488 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400489 printk(KERN_ERR PREFIX "Invalid link context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 continue;
491 }
492
493 /*
494 * reflect the possible and active irqs in the penalty table --
495 * useful for breaking ties.
496 */
497 if (link->irq.possible_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400498 int penalty =
499 PIRQ_PENALTY_PCI_POSSIBLE /
500 link->irq.possible_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 for (i = 0; i < link->irq.possible_count; i++) {
503 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
Len Brown4be44fc2005-08-05 00:44:28 -0400504 acpi_irq_penalty[link->irq.
505 possible[i]] +=
506 penalty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 } else if (link->irq.active) {
Len Brown4be44fc2005-08-05 00:44:28 -0400510 acpi_irq_penalty[link->irq.active] +=
511 PIRQ_PENALTY_PCI_POSSIBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
514 /* Add a penalty for the SCI */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300515 acpi_irq_penalty[acpi_gbl_FADT.sci_interrupt] += PIRQ_PENALTY_PCI_USING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Patrick Mocheld550d982006-06-27 00:41:40 -0400517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static int acpi_irq_balance; /* 0: static, 1: balance */
521
Len Brown4be44fc2005-08-05 00:44:28 -0400522static int acpi_pci_link_allocate(struct acpi_pci_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Len Brown4be44fc2005-08-05 00:44:28 -0400524 int irq;
525 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
David Shaohua Li87bec662005-07-27 23:02:00 -0400528 if (link->irq.initialized) {
529 if (link->refcnt == 0)
530 /* This means the link is disabled but initialized */
531 acpi_pci_link_set(link, link->irq.active);
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return 0;
David Shaohua Li87bec662005-07-27 23:02:00 -0400533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /*
536 * search for active IRQ in list of possible IRQs.
537 */
538 for (i = 0; i < link->irq.possible_count; ++i) {
539 if (link->irq.active == link->irq.possible[i])
540 break;
541 }
542 /*
543 * forget active IRQ that is not in possible list
544 */
545 if (i == link->irq.possible_count) {
546 if (acpi_strict)
Len Browncece9292006-06-26 23:04:31 -0400547 printk(KERN_WARNING PREFIX "_CRS %d not found"
548 " in _PRS\n", link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 link->irq.active = 0;
550 }
551
552 /*
553 * if active found, use it; else pick entry from end of possible list.
554 */
555 if (link->irq.active) {
556 irq = link->irq.active;
557 } else {
558 irq = link->irq.possible[link->irq.possible_count - 1];
559 }
560
561 if (acpi_irq_balance || !link->irq.active) {
562 /*
563 * Select the best IRQ. This is done in reverse to promote
564 * the use of IRQs 9, 10, 11, and >15.
565 */
566 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
Len Brown4be44fc2005-08-05 00:44:28 -0400567 if (acpi_irq_penalty[irq] >
568 acpi_irq_penalty[link->irq.possible[i]])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 irq = link->irq.possible[i];
570 }
571 }
572
573 /* Attempt to enable the link device at this IRQ. */
574 if (acpi_pci_link_set(link, irq)) {
Len Brown64684632006-06-26 23:41:38 -0400575 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
576 "Try pci=noacpi or acpi=off\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400577 acpi_device_name(link->device),
Len Brown64684632006-06-26 23:41:38 -0400578 acpi_device_bid(link->device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 } else {
581 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
Len Brown4be44fc2005-08-05 00:44:28 -0400582 printk(PREFIX "%s [%s] enabled at IRQ %d\n",
583 acpi_device_name(link->device),
584 acpi_device_bid(link->device), link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
587 link->irq.initialized = 1;
588
Patrick Mocheld550d982006-06-27 00:41:40 -0400589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/*
David Shaohua Li87bec662005-07-27 23:02:00 -0400593 * acpi_pci_link_allocate_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * success: return IRQ >= 0
595 * failure: return -1
596 */
597
598int
Len Brown4be44fc2005-08-05 00:44:28 -0400599acpi_pci_link_allocate_irq(acpi_handle handle,
600 int index,
Bob Moore50eca3e2005-09-30 19:03:00 -0400601 int *triggering, int *polarity, char **name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Len Brown4be44fc2005-08-05 00:44:28 -0400603 int result = 0;
604 struct acpi_device *device = NULL;
605 struct acpi_pci_link *link = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 result = acpi_bus_get_device(handle, &device);
609 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400610 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200614 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400616 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 /* TBD: Support multiple index (IRQ) entries per Link Device */
621 if (index) {
Len Brown64684632006-06-26 23:41:38 -0400622 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Ingo Molnar36e43092006-04-27 05:25:00 -0400626 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400627 if (acpi_pci_link_allocate(link)) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400628 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400630 }
Len Brown4be44fc2005-08-05 00:44:28 -0400631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!link->irq.active) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400633 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400634 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400635 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Len Brown4be44fc2005-08-05 00:44:28 -0400637 link->refcnt++;
Ingo Molnar36e43092006-04-27 05:25:00 -0400638 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Bob Moore50eca3e2005-09-30 19:03:00 -0400640 if (triggering)
641 *triggering = link->irq.triggering;
642 if (polarity)
643 *polarity = link->irq.polarity;
Len Brown4be44fc2005-08-05 00:44:28 -0400644 if (name)
645 *name = acpi_device_bid(link->device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400646 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400647 "Link %s is referenced\n",
648 acpi_device_bid(link->device)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400649 return (link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
David Shaohua Li87bec662005-07-27 23:02:00 -0400652/*
653 * We don't change link's irq information here. After it is reenabled, we
654 * continue use the info
655 */
Len Brown4be44fc2005-08-05 00:44:28 -0400656int acpi_pci_link_free_irq(acpi_handle handle)
David Shaohua Li87bec662005-07-27 23:02:00 -0400657{
Len Brown4be44fc2005-08-05 00:44:28 -0400658 struct acpi_device *device = NULL;
659 struct acpi_pci_link *link = NULL;
660 acpi_status result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
David Shaohua Li87bec662005-07-27 23:02:00 -0400662
663 result = acpi_bus_get_device(handle, &device);
664 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400665 printk(KERN_ERR PREFIX "Invalid link device\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400667 }
668
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200669 link = acpi_driver_data(device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400670 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400671 printk(KERN_ERR PREFIX "Invalid link context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400672 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400673 }
674
Ingo Molnar36e43092006-04-27 05:25:00 -0400675 mutex_lock(&acpi_link_lock);
David Shaohua Li87bec662005-07-27 23:02:00 -0400676 if (!link->irq.initialized) {
Ingo Molnar36e43092006-04-27 05:25:00 -0400677 mutex_unlock(&acpi_link_lock);
Len Brown64684632006-06-26 23:41:38 -0400678 printk(KERN_ERR PREFIX "Link isn't initialized\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400679 return -1;
David Shaohua Li87bec662005-07-27 23:02:00 -0400680 }
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400681#ifdef FUTURE_USE
682 /*
683 * The Link reference count allows us to _DISable an unused link
684 * and suspend time, and set it again on resume.
685 * However, 2.6.12 still has irq_router.resume
686 * which blindly restores the link state.
687 * So we disable the reference count method
688 * to prevent duplicate acpi_pci_link_set()
689 * which would harm some systems
690 */
Len Brown4be44fc2005-08-05 00:44:28 -0400691 link->refcnt--;
David Shaohua Liecc21eb2005-08-03 11:00:11 -0400692#endif
David Shaohua Li87bec662005-07-27 23:02:00 -0400693 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400694 "Link %s is dereferenced\n",
695 acpi_device_bid(link->device)));
David Shaohua Li87bec662005-07-27 23:02:00 -0400696
697 if (link->refcnt == 0) {
Patrick Mochel67a71362006-05-19 16:54:42 -0400698 acpi_ut_evaluate_object(link->device->handle, "_DIS", 0, NULL);
David Shaohua Li87bec662005-07-27 23:02:00 -0400699 }
Ingo Molnar36e43092006-04-27 05:25:00 -0400700 mutex_unlock(&acpi_link_lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400701 return (link->irq.active);
David Shaohua Li87bec662005-07-27 23:02:00 -0400702}
Len Brown4be44fc2005-08-05 00:44:28 -0400703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/* --------------------------------------------------------------------------
705 Driver Interface
706 -------------------------------------------------------------------------- */
707
Len Brown4be44fc2005-08-05 00:44:28 -0400708static int acpi_pci_link_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Len Brown4be44fc2005-08-05 00:44:28 -0400710 int result = 0;
711 struct acpi_pci_link *link = NULL;
712 int i = 0;
713 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400717 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Burman Yan36bcbec2006-12-19 12:56:11 -0800719 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!link)
Patrick Mocheld550d982006-06-27 00:41:40 -0400721 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 link->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
725 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
726 acpi_driver_data(device) = link;
727
Ingo Molnar36e43092006-04-27 05:25:00 -0400728 mutex_lock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 result = acpi_pci_link_get_possible(link);
730 if (result)
731 goto end;
732
733 /* query and set link->irq.active */
734 acpi_pci_link_get_current(link);
735
736 printk(PREFIX "%s [%s] (IRQs", acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400737 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 for (i = 0; i < link->irq.possible_count; i++) {
739 if (link->irq.active == link->irq.possible[i]) {
740 printk(" *%d", link->irq.possible[i]);
741 found = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400742 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 printk(" %d", link->irq.possible[i]);
744 }
745
746 printk(")");
747
748 if (!found)
749 printk(" *%d", link->irq.active);
750
Len Brown4be44fc2005-08-05 00:44:28 -0400751 if (!link->device->status.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 printk(", disabled.");
753
754 printk("\n");
755
756 /* TBD: Acquire/release lock */
757 list_add_tail(&link->node, &acpi_link.entries);
758 acpi_link.count++;
759
Len Brown4be44fc2005-08-05 00:44:28 -0400760 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* disable all links -- to be activated on use */
Patrick Mochel67a71362006-05-19 16:54:42 -0400762 acpi_ut_evaluate_object(device->handle, "_DIS", 0, NULL);
Ingo Molnar36e43092006-04-27 05:25:00 -0400763 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (result)
766 kfree(link);
767
Patrick Mocheld550d982006-06-27 00:41:40 -0400768 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Len Brown4be44fc2005-08-05 00:44:28 -0400771static int acpi_pci_link_resume(struct acpi_pci_link *link)
Linus Torvalds697a2d62005-08-01 12:37:54 -0700772{
Linus Torvalds697a2d62005-08-01 12:37:54 -0700773
774 if (link->refcnt && link->irq.active && link->irq.initialized)
Patrick Mocheld550d982006-06-27 00:41:40 -0400775 return (acpi_pci_link_set(link, link->irq.active));
Linus Torvalds697a2d62005-08-01 12:37:54 -0700776 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400777 return 0;
Linus Torvalds697a2d62005-08-01 12:37:54 -0700778}
779
Len Brown4be44fc2005-08-05 00:44:28 -0400780static int irqrouter_resume(struct sys_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Len Brown4be44fc2005-08-05 00:44:28 -0400782 struct list_head *node = NULL;
783 struct acpi_pci_link *link = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds56035092006-06-19 18:05:09 -0700786 /* Make sure SCI is enabled again (Apple firmware bug?) */
Bob Moored8c71b62007-02-02 19:48:21 +0300787 acpi_set_register(ACPI_BITREG_SCI_ENABLE, 1);
Linus Torvalds56035092006-06-19 18:05:09 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 list_for_each(node, &acpi_link.entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 link = list_entry(node, struct acpi_pci_link, node);
791 if (!link) {
Len Brown64684632006-06-26 23:41:38 -0400792 printk(KERN_ERR PREFIX "Invalid link context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 continue;
794 }
Linus Torvalds697a2d62005-08-01 12:37:54 -0700795 acpi_pci_link_resume(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Len Brown4be44fc2005-08-05 00:44:28 -0400800static int acpi_pci_link_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct acpi_pci_link *link = NULL;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200808 link = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Ingo Molnar36e43092006-04-27 05:25:00 -0400810 mutex_lock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 list_del(&link->node);
Ingo Molnar36e43092006-04-27 05:25:00 -0400812 mutex_unlock(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 kfree(link);
815
Patrick Mocheld550d982006-06-27 00:41:40 -0400816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819/*
820 * modify acpi_irq_penalty[] from cmdline
821 */
822static int __init acpi_irq_penalty_update(char *str, int used)
823{
824 int i;
825
826 for (i = 0; i < 16; i++) {
827 int retval;
828 int irq;
829
Len Brown4be44fc2005-08-05 00:44:28 -0400830 retval = get_option(&str, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 if (!retval)
833 break; /* no number found */
834
835 if (irq < 0)
836 continue;
Len Brown4be44fc2005-08-05 00:44:28 -0400837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (irq >= ACPI_MAX_IRQS)
839 continue;
840
841 if (used)
842 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
843 else
844 acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
845
846 if (retval != 2) /* no next number */
847 break;
848 }
849 return 1;
850}
851
852/*
853 * We'd like PNP to call this routine for the
854 * single ISA_USED value for each legacy device.
855 * But instead it calls us with each POSSIBLE setting.
856 * There is no ISA_POSSIBLE weight, so we simply use
857 * the (small) PCI_USING penalty.
858 */
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500859void acpi_penalize_isa_irq(int irq, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500861 if (active)
862 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
863 else
864 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867/*
868 * Over-ride default table to reserve additional IRQs for use by ISA
869 * e.g. acpi_irq_isa=5
870 * Useful for telling ACPI how not to interfere with your ISA sound card.
871 */
872static int __init acpi_irq_isa(char *str)
873{
874 return acpi_irq_penalty_update(str, 1);
875}
Len Brown4be44fc2005-08-05 00:44:28 -0400876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877__setup("acpi_irq_isa=", acpi_irq_isa);
878
879/*
880 * Over-ride default table to free additional IRQs for use by PCI
881 * e.g. acpi_irq_pci=7,15
882 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
883 */
884static int __init acpi_irq_pci(char *str)
885{
886 return acpi_irq_penalty_update(str, 0);
887}
Len Brown4be44fc2005-08-05 00:44:28 -0400888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889__setup("acpi_irq_pci=", acpi_irq_pci);
890
891static int __init acpi_irq_nobalance_set(char *str)
892{
893 acpi_irq_balance = 0;
894 return 1;
895}
Len Brown4be44fc2005-08-05 00:44:28 -0400896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
898
899int __init acpi_irq_balance_set(char *str)
900{
901 acpi_irq_balance = 1;
902 return 1;
903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Len Brown4be44fc2005-08-05 00:44:28 -0400905__setup("acpi_irq_balance", acpi_irq_balance_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
David Shaohua Li87bec662005-07-27 23:02:00 -0400907/* FIXME: we will remove this interface after all drivers call pci_disable_device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908static struct sysdev_class irqrouter_sysdev_class = {
Len Brown4be44fc2005-08-05 00:44:28 -0400909 set_kset_name("irqrouter"),
910 .resume = irqrouter_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913static struct sys_device device_irqrouter = {
Len Brown4be44fc2005-08-05 00:44:28 -0400914 .id = 0,
915 .cls = &irqrouter_sysdev_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916};
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static int __init irqrouter_init_sysfs(void)
919{
920 int error;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (acpi_disabled || acpi_noirq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 error = sysdev_class_register(&irqrouter_sysdev_class);
927 if (!error)
928 error = sysdev_register(&device_irqrouter);
929
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return error;
Len Brown4be44fc2005-08-05 00:44:28 -0400931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933device_initcall(irqrouter_init_sysfs);
934
Len Brown4be44fc2005-08-05 00:44:28 -0400935static int __init acpi_pci_link_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (acpi_noirq)
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 acpi_link.count = 0;
942 INIT_LIST_HEAD(&acpi_link.entries);
943
944 if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400945 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Patrick Mocheld550d982006-06-27 00:41:40 -0400947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950subsys_initcall(acpi_pci_link_init);