]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/acpi/pci_link.c
ACPI: autoload modules - Create ACPI alias interface
[net-next-2.6.git] / drivers / acpi / pci_link.c
CommitLineData
1da177e4
LT
1/*
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>
36e43095 41#include <linux/mutex.h>
1da177e4
LT
42
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45
1da177e4 46#define _COMPONENT ACPI_PCI_COMPONENT
f52fd66d 47ACPI_MODULE_NAME("pci_link");
1da177e4
LT
48#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
49#define ACPI_PCI_LINK_HID "PNP0C0F"
1da177e4
LT
50#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"
1da177e4 53#define ACPI_PCI_LINK_MAX_POSSIBLE 16
4be44fcd
LB
54static int acpi_pci_link_add(struct acpi_device *device);
55static int acpi_pci_link_remove(struct acpi_device *device, int type);
1da177e4
LT
56
57static struct acpi_driver acpi_pci_link_driver = {
c2b6705b 58 .name = "pci_link",
4be44fcd
LB
59 .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 },
1da177e4
LT
65};
66
87bec66b
DSL
67/*
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 */
1da177e4 71struct acpi_pci_link_irq {
4be44fcd 72 u8 active; /* Current IRQ */
50eca3eb
BM
73 u8 triggering; /* All IRQs */
74 u8 polarity; /* All IRQs */
4be44fcd
LB
75 u8 resource_type;
76 u8 possible_count;
77 u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
78 u8 initialized:1;
79 u8 reserved:7;
1da177e4
LT
80};
81
82struct acpi_pci_link {
4be44fcd
LB
83 struct list_head node;
84 struct acpi_device *device;
1da177e4 85 struct acpi_pci_link_irq irq;
4be44fcd 86 int refcnt;
1da177e4
LT
87};
88
89static struct {
4be44fcd
LB
90 int count;
91 struct list_head entries;
92} acpi_link;
36e43095 93DEFINE_MUTEX(acpi_link_lock);
1da177e4 94
1da177e4
LT
95/* --------------------------------------------------------------------------
96 PCI Link Device Management
97 -------------------------------------------------------------------------- */
98
99/*
100 * set context (link) possible list from resource list
101 */
102static acpi_status
4be44fcd 103acpi_pci_link_check_possible(struct acpi_resource *resource, void *context)
1da177e4 104{
50dd0969 105 struct acpi_pci_link *link = context;
4be44fcd 106 u32 i = 0;
1da177e4 107
1da177e4 108
eca008c8 109 switch (resource->type) {
50eca3eb 110 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
d550d98d 111 return AE_OK;
50eca3eb 112 case ACPI_RESOURCE_TYPE_IRQ:
4be44fcd
LB
113 {
114 struct acpi_resource_irq *p = &resource->data.irq;
50eca3eb 115 if (!p || !p->interrupt_count) {
cece9296 116 printk(KERN_WARNING PREFIX "Blank IRQ resource\n");
d550d98d 117 return AE_OK;
1da177e4 118 }
4be44fcd 119 for (i = 0;
50eca3eb 120 (i < p->interrupt_count
4be44fcd
LB
121 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
122 if (!p->interrupts[i]) {
cece9296
LB
123 printk(KERN_WARNING PREFIX "Invalid IRQ %d\n",
124 p->interrupts[i]);
4be44fcd
LB
125 continue;
126 }
127 link->irq.possible[i] = p->interrupts[i];
128 link->irq.possible_count++;
129 }
50eca3eb
BM
130 link->irq.triggering = p->triggering;
131 link->irq.polarity = p->polarity;
132 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
4be44fcd 133 break;
1da177e4 134 }
50eca3eb 135 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
4be44fcd 136 {
50eca3eb 137 struct acpi_resource_extended_irq *p =
4be44fcd 138 &resource->data.extended_irq;
50eca3eb 139 if (!p || !p->interrupt_count) {
cece9296
LB
140 printk(KERN_WARNING PREFIX
141 "Blank EXT IRQ resource\n");
d550d98d 142 return AE_OK;
4be44fcd
LB
143 }
144 for (i = 0;
50eca3eb 145 (i < p->interrupt_count
4be44fcd
LB
146 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
147 if (!p->interrupts[i]) {
cece9296
LB
148 printk(KERN_WARNING PREFIX "Invalid IRQ %d\n",
149 p->interrupts[i]);
4be44fcd
LB
150 continue;
151 }
152 link->irq.possible[i] = p->interrupts[i];
153 link->irq.possible_count++;
1da177e4 154 }
50eca3eb
BM
155 link->irq.triggering = p->triggering;
156 link->irq.polarity = p->polarity;
157 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
4be44fcd 158 break;
1da177e4 159 }
1da177e4 160 default:
6468463a 161 printk(KERN_ERR PREFIX "Resource is not an IRQ entry\n");
d550d98d 162 return AE_OK;
1da177e4
LT
163 }
164
d550d98d 165 return AE_CTRL_TERMINATE;
1da177e4
LT
166}
167
4be44fcd 168static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
1da177e4 169{
4be44fcd 170 acpi_status status;
1da177e4 171
1da177e4
LT
172
173 if (!link)
d550d98d 174 return -EINVAL;
1da177e4 175
67a71365 176 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
4be44fcd 177 acpi_pci_link_check_possible, link);
1da177e4 178 if (ACPI_FAILURE(status)) {
a6fc6720 179 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
d550d98d 180 return -ENODEV;
1da177e4
LT
181 }
182
4be44fcd
LB
183 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
184 "Found %d possible IRQs\n",
185 link->irq.possible_count));
1da177e4 186
d550d98d 187 return 0;
1da177e4
LT
188}
189
1da177e4 190static acpi_status
4be44fcd 191acpi_pci_link_check_current(struct acpi_resource *resource, void *context)
1da177e4 192{
4be44fcd 193 int *irq = (int *)context;
1da177e4 194
1da177e4 195
eca008c8 196 switch (resource->type) {
50eca3eb 197 case ACPI_RESOURCE_TYPE_IRQ:
4be44fcd
LB
198 {
199 struct acpi_resource_irq *p = &resource->data.irq;
50eca3eb 200 if (!p || !p->interrupt_count) {
4be44fcd
LB
201 /*
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"));
d550d98d 207 return AE_OK;
4be44fcd
LB
208 }
209 *irq = p->interrupts[0];
210 break;
1da177e4 211 }
50eca3eb 212 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
4be44fcd 213 {
50eca3eb 214 struct acpi_resource_extended_irq *p =
4be44fcd 215 &resource->data.extended_irq;
50eca3eb 216 if (!p || !p->interrupt_count) {
4be44fcd
LB
217 /*
218 * extended IRQ descriptors must
219 * return at least 1 IRQ
220 */
cece9296
LB
221 printk(KERN_WARNING PREFIX
222 "Blank EXT IRQ resource\n");
d550d98d 223 return AE_OK;
4be44fcd
LB
224 }
225 *irq = p->interrupts[0];
226 break;
1da177e4 227 }
d4ec6c7c 228 break;
1da177e4 229 default:
6468463a 230 printk(KERN_ERR PREFIX "Resource %d isn't an IRQ\n", resource->type);
d4ec6c7c 231 case ACPI_RESOURCE_TYPE_END_TAG:
d550d98d 232 return AE_OK;
1da177e4 233 }
d550d98d 234 return AE_CTRL_TERMINATE;
1da177e4
LT
235}
236
237/*
238 * Run _CRS and set link->irq.active
239 *
240 * return value:
241 * 0 - success
242 * !0 - failure
243 */
4be44fcd 244static int acpi_pci_link_get_current(struct acpi_pci_link *link)
1da177e4 245{
4be44fcd
LB
246 int result = 0;
247 acpi_status status = AE_OK;
248 int irq = 0;
1da177e4 249
e0e4e117 250 if (!link)
d550d98d 251 return -EINVAL;
1da177e4
LT
252
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) {
6468463a 260 printk(KERN_ERR PREFIX "Unable to read status\n");
1da177e4
LT
261 goto end;
262 }
263
264 if (!link->device->status.enabled) {
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
d550d98d 266 return 0;
1da177e4
LT
267 }
268 }
269
270 /*
271 * Query and parse _CRS to get the current IRQ assignment.
272 */
273
67a71365 274 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
4be44fcd 275 acpi_pci_link_check_current, &irq);
1da177e4 276 if (ACPI_FAILURE(status)) {
a6fc6720 277 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
1da177e4
LT
278 result = -ENODEV;
279 goto end;
280 }
281
282 if (acpi_strict && !irq) {
6468463a 283 printk(KERN_ERR PREFIX "_CRS returned 0\n");
1da177e4
LT
284 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
4be44fcd 291 end:
d550d98d 292 return result;
1da177e4
LT
293}
294
4be44fcd 295static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
1da177e4 296{
4be44fcd
LB
297 int result = 0;
298 acpi_status status = AE_OK;
1da177e4 299 struct {
4be44fcd
LB
300 struct acpi_resource res;
301 struct acpi_resource end;
302 } *resource;
303 struct acpi_buffer buffer = { 0, NULL };
1da177e4 304
1da177e4
LT
305
306 if (!link || !irq)
d550d98d 307 return -EINVAL;
1da177e4 308
36bcbec7 309 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
4be44fcd 310 if (!resource)
d550d98d 311 return -ENOMEM;
1da177e4 312
4be44fcd 313 buffer.length = sizeof(*resource) + 1;
1da177e4
LT
314 buffer.pointer = resource;
315
4be44fcd 316 switch (link->irq.resource_type) {
50eca3eb
BM
317 case ACPI_RESOURCE_TYPE_IRQ:
318 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
1da177e4 319 resource->res.length = sizeof(struct acpi_resource);
50eca3eb
BM
320 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 =
4be44fcd 325 ACPI_EXCLUSIVE;
1da177e4 326 else
50eca3eb
BM
327 resource->res.data.irq.sharable = ACPI_SHARED;
328 resource->res.data.irq.interrupt_count = 1;
1da177e4
LT
329 resource->res.data.irq.interrupts[0] = irq;
330 break;
4be44fcd 331
50eca3eb
BM
332 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
333 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
1da177e4 334 resource->res.length = sizeof(struct acpi_resource);
4be44fcd
LB
335 resource->res.data.extended_irq.producer_consumer =
336 ACPI_CONSUMER;
50eca3eb
BM
337 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 =
4be44fcd 343 ACPI_EXCLUSIVE;
1da177e4 344 else
50eca3eb
BM
345 resource->res.data.irq.sharable = ACPI_SHARED;
346 resource->res.data.extended_irq.interrupt_count = 1;
1da177e4
LT
347 resource->res.data.extended_irq.interrupts[0] = irq;
348 /* ignore resource_source, it's optional */
349 break;
350 default:
6468463a 351 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
1da177e4
LT
352 result = -EINVAL;
353 goto end;
354
355 }
50eca3eb 356 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1da177e4
LT
357
358 /* Attempt to set the resource */
67a71365 359 status = acpi_set_current_resources(link->device->handle, &buffer);
1da177e4
LT
360
361 /* check for total failure */
362 if (ACPI_FAILURE(status)) {
a6fc6720 363 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
1da177e4
LT
364 result = -ENODEV;
365 goto end;
366 }
367
368 /* Query _STA, set device->status */
369 result = acpi_bus_get_status(link->device);
370 if (result) {
6468463a 371 printk(KERN_ERR PREFIX "Unable to read status\n");
1da177e4
LT
372 goto end;
373 }
374 if (!link->device->status.enabled) {
cece9296
LB
375 printk(KERN_WARNING PREFIX
376 "%s [%s] disabled and referenced, BIOS bug\n",
a6fc6720 377 acpi_device_name(link->device),
cece9296 378 acpi_device_bid(link->device));
1da177e4
LT
379 }
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 */
cece9296
LB
396 printk(KERN_WARNING PREFIX
397 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
a6fc6720 398 acpi_device_name(link->device),
cece9296 399 acpi_device_bid(link->device), link->irq.active, irq);
1da177e4
LT
400 link->irq.active = irq;
401 }
402
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
4be44fcd
LB
404
405 end:
1da177e4 406 kfree(resource);
d550d98d 407 return result;
1da177e4
LT
408}
409
1da177e4
LT
410/* --------------------------------------------------------------------------
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 */
4be44fcd
LB
459 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
460 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
1da177e4
LT
461 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 */
4be44fcd 472 /* >IRQ15 */
1da177e4
LT
473};
474
4be44fcd 475int __init acpi_irq_penalty_init(void)
1da177e4 476{
4be44fcd
LB
477 struct list_head *node = NULL;
478 struct acpi_pci_link *link = NULL;
479 int i = 0;
1da177e4 480
1da177e4
LT
481
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) {
6468463a 489 printk(KERN_ERR PREFIX "Invalid link context\n");
1da177e4
LT
490 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) {
4be44fcd
LB
498 int penalty =
499 PIRQ_PENALTY_PCI_POSSIBLE /
500 link->irq.possible_count;
1da177e4
LT
501
502 for (i = 0; i < link->irq.possible_count; i++) {
503 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
4be44fcd
LB
504 acpi_irq_penalty[link->irq.
505 possible[i]] +=
506 penalty;
1da177e4
LT
507 }
508
509 } else if (link->irq.active) {
4be44fcd
LB
510 acpi_irq_penalty[link->irq.active] +=
511 PIRQ_PENALTY_PCI_POSSIBLE;
1da177e4
LT
512 }
513 }
514 /* Add a penalty for the SCI */
cee324b1 515 acpi_irq_penalty[acpi_gbl_FADT.sci_interrupt] += PIRQ_PENALTY_PCI_USING;
1da177e4 516
d550d98d 517 return 0;
1da177e4
LT
518}
519
520static int acpi_irq_balance; /* 0: static, 1: balance */
521
4be44fcd 522static int acpi_pci_link_allocate(struct acpi_pci_link *link)
1da177e4 523{
4be44fcd
LB
524 int irq;
525 int i;
1da177e4 526
1da177e4 527
87bec66b
DSL
528 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);
d550d98d 532 return 0;
87bec66b 533 }
1da177e4
LT
534
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)
cece9296
LB
547 printk(KERN_WARNING PREFIX "_CRS %d not found"
548 " in _PRS\n", link->irq.active);
1da177e4
LT
549 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--) {
4be44fcd
LB
567 if (acpi_irq_penalty[irq] >
568 acpi_irq_penalty[link->irq.possible[i]])
1da177e4
LT
569 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)) {
6468463a
LB
575 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
576 "Try pci=noacpi or acpi=off\n",
a6fc6720 577 acpi_device_name(link->device),
6468463a 578 acpi_device_bid(link->device));
d550d98d 579 return -ENODEV;
1da177e4
LT
580 } else {
581 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
4be44fcd
LB
582 printk(PREFIX "%s [%s] enabled at IRQ %d\n",
583 acpi_device_name(link->device),
584 acpi_device_bid(link->device), link->irq.active);
1da177e4
LT
585 }
586
587 link->irq.initialized = 1;
588
d550d98d 589 return 0;
1da177e4
LT
590}
591
592/*
87bec66b 593 * acpi_pci_link_allocate_irq
1da177e4
LT
594 * success: return IRQ >= 0
595 * failure: return -1
596 */
597
598int
4be44fcd
LB
599acpi_pci_link_allocate_irq(acpi_handle handle,
600 int index,
50eca3eb 601 int *triggering, int *polarity, char **name)
1da177e4 602{
4be44fcd
LB
603 int result = 0;
604 struct acpi_device *device = NULL;
605 struct acpi_pci_link *link = NULL;
1da177e4 606
1da177e4
LT
607
608 result = acpi_bus_get_device(handle, &device);
609 if (result) {
6468463a 610 printk(KERN_ERR PREFIX "Invalid link device\n");
d550d98d 611 return -1;
1da177e4
LT
612 }
613
50dd0969 614 link = acpi_driver_data(device);
1da177e4 615 if (!link) {
6468463a 616 printk(KERN_ERR PREFIX "Invalid link context\n");
d550d98d 617 return -1;
1da177e4
LT
618 }
619
620 /* TBD: Support multiple index (IRQ) entries per Link Device */
621 if (index) {
6468463a 622 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
d550d98d 623 return -1;
1da177e4
LT
624 }
625
36e43095 626 mutex_lock(&acpi_link_lock);
87bec66b 627 if (acpi_pci_link_allocate(link)) {
36e43095 628 mutex_unlock(&acpi_link_lock);
d550d98d 629 return -1;
87bec66b 630 }
4be44fcd 631
1da177e4 632 if (!link->irq.active) {
36e43095 633 mutex_unlock(&acpi_link_lock);
6468463a 634 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
d550d98d 635 return -1;
1da177e4 636 }
4be44fcd 637 link->refcnt++;
36e43095 638 mutex_unlock(&acpi_link_lock);
1da177e4 639
50eca3eb
BM
640 if (triggering)
641 *triggering = link->irq.triggering;
642 if (polarity)
643 *polarity = link->irq.polarity;
4be44fcd
LB
644 if (name)
645 *name = acpi_device_bid(link->device);
87bec66b 646 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
647 "Link %s is referenced\n",
648 acpi_device_bid(link->device)));
d550d98d 649 return (link->irq.active);
1da177e4
LT
650}
651
87bec66b
DSL
652/*
653 * We don't change link's irq information here. After it is reenabled, we
654 * continue use the info
655 */
4be44fcd 656int acpi_pci_link_free_irq(acpi_handle handle)
87bec66b 657{
4be44fcd
LB
658 struct acpi_device *device = NULL;
659 struct acpi_pci_link *link = NULL;
660 acpi_status result;
87bec66b 661
87bec66b
DSL
662
663 result = acpi_bus_get_device(handle, &device);
664 if (result) {
6468463a 665 printk(KERN_ERR PREFIX "Invalid link device\n");
d550d98d 666 return -1;
87bec66b 667 }
1da177e4 668
50dd0969 669 link = acpi_driver_data(device);
87bec66b 670 if (!link) {
6468463a 671 printk(KERN_ERR PREFIX "Invalid link context\n");
d550d98d 672 return -1;
87bec66b
DSL
673 }
674
36e43095 675 mutex_lock(&acpi_link_lock);
87bec66b 676 if (!link->irq.initialized) {
36e43095 677 mutex_unlock(&acpi_link_lock);
6468463a 678 printk(KERN_ERR PREFIX "Link isn't initialized\n");
d550d98d 679 return -1;
87bec66b 680 }
ecc21ebe
DSL
681#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 */
4be44fcd 691 link->refcnt--;
ecc21ebe 692#endif
87bec66b 693 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
694 "Link %s is dereferenced\n",
695 acpi_device_bid(link->device)));
87bec66b
DSL
696
697 if (link->refcnt == 0) {
67a71365 698 acpi_ut_evaluate_object(link->device->handle, "_DIS", 0, NULL);
87bec66b 699 }
36e43095 700 mutex_unlock(&acpi_link_lock);
d550d98d 701 return (link->irq.active);
87bec66b 702}
4be44fcd 703
1da177e4
LT
704/* --------------------------------------------------------------------------
705 Driver Interface
706 -------------------------------------------------------------------------- */
707
4be44fcd 708static int acpi_pci_link_add(struct acpi_device *device)
1da177e4 709{
4be44fcd
LB
710 int result = 0;
711 struct acpi_pci_link *link = NULL;
712 int i = 0;
713 int found = 0;
1da177e4 714
1da177e4
LT
715
716 if (!device)
d550d98d 717 return -EINVAL;
1da177e4 718
36bcbec7 719 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
1da177e4 720 if (!link)
d550d98d 721 return -ENOMEM;
1da177e4
LT
722
723 link->device = device;
1da177e4
LT
724 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
36e43095 728 mutex_lock(&acpi_link_lock);
1da177e4
LT
729 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
0dc070bb 736 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
4be44fcd 737 acpi_device_bid(device));
1da177e4
LT
738 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;
4be44fcd 742 } else
1da177e4
LT
743 printk(" %d", link->irq.possible[i]);
744 }
745
746 printk(")");
747
748 if (!found)
749 printk(" *%d", link->irq.active);
750
4be44fcd 751 if (!link->device->status.enabled)
1da177e4
LT
752 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
4be44fcd 760 end:
1da177e4 761 /* disable all links -- to be activated on use */
67a71365 762 acpi_ut_evaluate_object(device->handle, "_DIS", 0, NULL);
36e43095 763 mutex_unlock(&acpi_link_lock);
1da177e4
LT
764
765 if (result)
766 kfree(link);
767
d550d98d 768 return result;
1da177e4
LT
769}
770
4be44fcd 771static int acpi_pci_link_resume(struct acpi_pci_link *link)
697a2d63 772{
697a2d63
LT
773
774 if (link->refcnt && link->irq.active && link->irq.initialized)
d550d98d 775 return (acpi_pci_link_set(link, link->irq.active));
697a2d63 776 else
d550d98d 777 return 0;
697a2d63
LT
778}
779
4be44fcd 780static int irqrouter_resume(struct sys_device *dev)
1da177e4 781{
4be44fcd
LB
782 struct list_head *node = NULL;
783 struct acpi_pci_link *link = NULL;
1da177e4 784
1da177e4 785
56035091 786 /* Make sure SCI is enabled again (Apple firmware bug?) */
d8c71b6d 787 acpi_set_register(ACPI_BITREG_SCI_ENABLE, 1);
56035091 788
1da177e4 789 list_for_each(node, &acpi_link.entries) {
1da177e4
LT
790 link = list_entry(node, struct acpi_pci_link, node);
791 if (!link) {
6468463a 792 printk(KERN_ERR PREFIX "Invalid link context\n");
1da177e4
LT
793 continue;
794 }
697a2d63 795 acpi_pci_link_resume(link);
1da177e4 796 }
d550d98d 797 return 0;
1da177e4
LT
798}
799
4be44fcd 800static int acpi_pci_link_remove(struct acpi_device *device, int type)
1da177e4
LT
801{
802 struct acpi_pci_link *link = NULL;
803
1da177e4
LT
804
805 if (!device || !acpi_driver_data(device))
d550d98d 806 return -EINVAL;
1da177e4 807
50dd0969 808 link = acpi_driver_data(device);
1da177e4 809
36e43095 810 mutex_lock(&acpi_link_lock);
1da177e4 811 list_del(&link->node);
36e43095 812 mutex_unlock(&acpi_link_lock);
1da177e4
LT
813
814 kfree(link);
815
d550d98d 816 return 0;
1da177e4
LT
817}
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
4be44fcd 830 retval = get_option(&str, &irq);
1da177e4
LT
831
832 if (!retval)
833 break; /* no number found */
834
835 if (irq < 0)
836 continue;
4be44fcd 837
1da177e4
LT
838 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 */
c9c3e457 859void acpi_penalize_isa_irq(int irq, int active)
1da177e4 860{
c9c3e457
DSL
861 if (active)
862 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
863 else
864 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
1da177e4
LT
865}
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}
4be44fcd 876
1da177e4
LT
877__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}
4be44fcd 888
1da177e4
LT
889__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}
4be44fcd 896
1da177e4
LT
897__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}
1da177e4 904
4be44fcd 905__setup("acpi_irq_balance", acpi_irq_balance_set);
1da177e4 906
87bec66b 907/* FIXME: we will remove this interface after all drivers call pci_disable_device */
1da177e4 908static struct sysdev_class irqrouter_sysdev_class = {
4be44fcd
LB
909 set_kset_name("irqrouter"),
910 .resume = irqrouter_resume,
1da177e4
LT
911};
912
1da177e4 913static struct sys_device device_irqrouter = {
4be44fcd
LB
914 .id = 0,
915 .cls = &irqrouter_sysdev_class,
1da177e4
LT
916};
917
1da177e4
LT
918static int __init irqrouter_init_sysfs(void)
919{
920 int error;
921
1da177e4
LT
922
923 if (acpi_disabled || acpi_noirq)
d550d98d 924 return 0;
1da177e4
LT
925
926 error = sysdev_class_register(&irqrouter_sysdev_class);
927 if (!error)
928 error = sysdev_register(&device_irqrouter);
929
d550d98d 930 return error;
4be44fcd 931}
1da177e4
LT
932
933device_initcall(irqrouter_init_sysfs);
934
4be44fcd 935static int __init acpi_pci_link_init(void)
1da177e4 936{
1da177e4
LT
937
938 if (acpi_noirq)
d550d98d 939 return 0;
1da177e4
LT
940
941 acpi_link.count = 0;
942 INIT_LIST_HEAD(&acpi_link.entries);
943
944 if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
d550d98d 945 return -ENODEV;
1da177e4 946
d550d98d 947 return 0;
1da177e4
LT
948}
949
950subsys_initcall(acpi_pci_link_init);