Merge branch 'bkl/procfs' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.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>
5a0e3ad6 42#include <linux/slab.h>
1da177e4
LT
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
a192a958
LB
47#define PREFIX "ACPI: "
48
1c9ca3a7 49#define _COMPONENT ACPI_PCI_COMPONENT
f52fd66d 50ACPI_MODULE_NAME("pci_link");
1da177e4 51#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
1da177e4
LT
52#define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
53#define ACPI_PCI_LINK_FILE_INFO "info"
54#define ACPI_PCI_LINK_FILE_STATUS "state"
1c9ca3a7
BH
55#define ACPI_PCI_LINK_MAX_POSSIBLE 16
56
4be44fcd
LB
57static int acpi_pci_link_add(struct acpi_device *device);
58static int acpi_pci_link_remove(struct acpi_device *device, int type);
1da177e4 59
c97adf9e 60static const struct acpi_device_id link_device_ids[] = {
1ba90e3a
TR
61 {"PNP0C0F", 0},
62 {"", 0},
63};
64MODULE_DEVICE_TABLE(acpi, link_device_ids);
65
1da177e4 66static struct acpi_driver acpi_pci_link_driver = {
c2b6705b 67 .name = "pci_link",
4be44fcd 68 .class = ACPI_PCI_LINK_CLASS,
1ba90e3a 69 .ids = link_device_ids,
4be44fcd
LB
70 .ops = {
71 .add = acpi_pci_link_add,
72 .remove = acpi_pci_link_remove,
1c9ca3a7 73 },
1da177e4
LT
74};
75
87bec66b
DSL
76/*
77 * If a link is initialized, we never change its active and initialized
78 * later even the link is disable. Instead, we just repick the active irq
79 */
1da177e4 80struct acpi_pci_link_irq {
4be44fcd 81 u8 active; /* Current IRQ */
50eca3eb 82 u8 triggering; /* All IRQs */
1c9ca3a7 83 u8 polarity; /* All IRQs */
4be44fcd
LB
84 u8 resource_type;
85 u8 possible_count;
86 u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
87 u8 initialized:1;
88 u8 reserved:7;
1da177e4
LT
89};
90
91struct acpi_pci_link {
5f0dccaa 92 struct list_head list;
1c9ca3a7
BH
93 struct acpi_device *device;
94 struct acpi_pci_link_irq irq;
95 int refcnt;
1da177e4
LT
96};
97
5f0dccaa 98static LIST_HEAD(acpi_link_list);
e5685b9d 99static DEFINE_MUTEX(acpi_link_lock);
1da177e4 100
1da177e4
LT
101/* --------------------------------------------------------------------------
102 PCI Link Device Management
103 -------------------------------------------------------------------------- */
104
105/*
106 * set context (link) possible list from resource list
107 */
1c9ca3a7
BH
108static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
109 void *context)
1da177e4 110{
50dd0969 111 struct acpi_pci_link *link = context;
c9d62443 112 u32 i;
1da177e4 113
eca008c8 114 switch (resource->type) {
50eca3eb 115 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
4a5e3638 116 case ACPI_RESOURCE_TYPE_END_TAG:
d550d98d 117 return AE_OK;
50eca3eb 118 case ACPI_RESOURCE_TYPE_IRQ:
4be44fcd
LB
119 {
120 struct acpi_resource_irq *p = &resource->data.irq;
50eca3eb 121 if (!p || !p->interrupt_count) {
4a5e3638
BH
122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
123 "Blank _PRS IRQ resource\n"));
d550d98d 124 return AE_OK;
1da177e4 125 }
4be44fcd 126 for (i = 0;
50eca3eb 127 (i < p->interrupt_count
4be44fcd
LB
128 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
129 if (!p->interrupts[i]) {
4a5e3638
BH
130 printk(KERN_WARNING PREFIX
131 "Invalid _PRS IRQ %d\n",
132 p->interrupts[i]);
4be44fcd
LB
133 continue;
134 }
135 link->irq.possible[i] = p->interrupts[i];
136 link->irq.possible_count++;
137 }
50eca3eb
BM
138 link->irq.triggering = p->triggering;
139 link->irq.polarity = p->polarity;
140 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
4be44fcd 141 break;
1da177e4 142 }
50eca3eb 143 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
4be44fcd 144 {
50eca3eb 145 struct acpi_resource_extended_irq *p =
4be44fcd 146 &resource->data.extended_irq;
50eca3eb 147 if (!p || !p->interrupt_count) {
cece9296 148 printk(KERN_WARNING PREFIX
4a5e3638 149 "Blank _PRS EXT IRQ resource\n");
d550d98d 150 return AE_OK;
4be44fcd
LB
151 }
152 for (i = 0;
50eca3eb 153 (i < p->interrupt_count
4be44fcd
LB
154 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
155 if (!p->interrupts[i]) {
4a5e3638
BH
156 printk(KERN_WARNING PREFIX
157 "Invalid _PRS IRQ %d\n",
158 p->interrupts[i]);
4be44fcd
LB
159 continue;
160 }
161 link->irq.possible[i] = p->interrupts[i];
162 link->irq.possible_count++;
1da177e4 163 }
50eca3eb
BM
164 link->irq.triggering = p->triggering;
165 link->irq.polarity = p->polarity;
166 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
4be44fcd 167 break;
1da177e4 168 }
1da177e4 169 default:
4a5e3638
BH
170 printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
171 resource->type);
d550d98d 172 return AE_OK;
1da177e4
LT
173 }
174
d550d98d 175 return AE_CTRL_TERMINATE;
1da177e4
LT
176}
177
4be44fcd 178static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
1da177e4 179{
4be44fcd 180 acpi_status status;
1da177e4 181
67a71365 182 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
4be44fcd 183 acpi_pci_link_check_possible, link);
1da177e4 184 if (ACPI_FAILURE(status)) {
a6fc6720 185 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
d550d98d 186 return -ENODEV;
1da177e4
LT
187 }
188
4be44fcd
LB
189 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
190 "Found %d possible IRQs\n",
191 link->irq.possible_count));
1da177e4 192
d550d98d 193 return 0;
1da177e4
LT
194}
195
1c9ca3a7
BH
196static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
197 void *context)
1da177e4 198{
c9d62443 199 int *irq = context;
1da177e4 200
eca008c8 201 switch (resource->type) {
4a5e3638
BH
202 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
203 case ACPI_RESOURCE_TYPE_END_TAG:
204 return AE_OK;
50eca3eb 205 case ACPI_RESOURCE_TYPE_IRQ:
4be44fcd
LB
206 {
207 struct acpi_resource_irq *p = &resource->data.irq;
50eca3eb 208 if (!p || !p->interrupt_count) {
4be44fcd
LB
209 /*
210 * IRQ descriptors may have no IRQ# bits set,
211 * particularly those those w/ _STA disabled
212 */
213 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4a5e3638 214 "Blank _CRS IRQ resource\n"));
d550d98d 215 return AE_OK;
4be44fcd
LB
216 }
217 *irq = p->interrupts[0];
218 break;
1da177e4 219 }
50eca3eb 220 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
4be44fcd 221 {
50eca3eb 222 struct acpi_resource_extended_irq *p =
4be44fcd 223 &resource->data.extended_irq;
50eca3eb 224 if (!p || !p->interrupt_count) {
4be44fcd
LB
225 /*
226 * extended IRQ descriptors must
227 * return at least 1 IRQ
228 */
cece9296 229 printk(KERN_WARNING PREFIX
4a5e3638 230 "Blank _CRS EXT IRQ resource\n");
d550d98d 231 return AE_OK;
4be44fcd
LB
232 }
233 *irq = p->interrupts[0];
234 break;
1da177e4 235 }
d4ec6c7c 236 break;
1da177e4 237 default:
4a5e3638
BH
238 printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
239 resource->type);
d550d98d 240 return AE_OK;
1da177e4 241 }
4a5e3638 242
d550d98d 243 return AE_CTRL_TERMINATE;
1da177e4
LT
244}
245
246/*
247 * Run _CRS and set link->irq.active
248 *
249 * return value:
250 * 0 - success
251 * !0 - failure
252 */
4be44fcd 253static int acpi_pci_link_get_current(struct acpi_pci_link *link)
1da177e4 254{
4be44fcd 255 int result = 0;
c9d62443 256 acpi_status status;
4be44fcd 257 int irq = 0;
1da177e4 258
1da177e4
LT
259 link->irq.active = 0;
260
261 /* in practice, status disabled is meaningless, ignore it */
262 if (acpi_strict) {
263 /* Query _STA, set link->device->status */
264 result = acpi_bus_get_status(link->device);
265 if (result) {
6468463a 266 printk(KERN_ERR PREFIX "Unable to read status\n");
1da177e4
LT
267 goto end;
268 }
269
270 if (!link->device->status.enabled) {
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
d550d98d 272 return 0;
1da177e4
LT
273 }
274 }
275
276 /*
277 * Query and parse _CRS to get the current IRQ assignment.
278 */
279
67a71365 280 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
4be44fcd 281 acpi_pci_link_check_current, &irq);
1da177e4 282 if (ACPI_FAILURE(status)) {
a6fc6720 283 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
1da177e4
LT
284 result = -ENODEV;
285 goto end;
286 }
287
288 if (acpi_strict && !irq) {
6468463a 289 printk(KERN_ERR PREFIX "_CRS returned 0\n");
1da177e4
LT
290 result = -ENODEV;
291 }
292
293 link->irq.active = irq;
294
295 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
296
4be44fcd 297 end:
d550d98d 298 return result;
1da177e4
LT
299}
300
4be44fcd 301static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
1da177e4 302{
c9d62443
BH
303 int result;
304 acpi_status status;
1da177e4 305 struct {
4be44fcd
LB
306 struct acpi_resource res;
307 struct acpi_resource end;
308 } *resource;
309 struct acpi_buffer buffer = { 0, NULL };
1da177e4 310
6eca4b4c 311 if (!irq)
d550d98d 312 return -EINVAL;
1da177e4 313
36bcbec7 314 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
4be44fcd 315 if (!resource)
d550d98d 316 return -ENOMEM;
1da177e4 317
4be44fcd 318 buffer.length = sizeof(*resource) + 1;
1da177e4
LT
319 buffer.pointer = resource;
320
4be44fcd 321 switch (link->irq.resource_type) {
50eca3eb
BM
322 case ACPI_RESOURCE_TYPE_IRQ:
323 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
1da177e4 324 resource->res.length = sizeof(struct acpi_resource);
50eca3eb
BM
325 resource->res.data.irq.triggering = link->irq.triggering;
326 resource->res.data.irq.polarity =
327 link->irq.polarity;
328 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
329 resource->res.data.irq.sharable =
4be44fcd 330 ACPI_EXCLUSIVE;
1da177e4 331 else
50eca3eb
BM
332 resource->res.data.irq.sharable = ACPI_SHARED;
333 resource->res.data.irq.interrupt_count = 1;
1da177e4
LT
334 resource->res.data.irq.interrupts[0] = irq;
335 break;
4be44fcd 336
50eca3eb
BM
337 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
338 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
1da177e4 339 resource->res.length = sizeof(struct acpi_resource);
4be44fcd
LB
340 resource->res.data.extended_irq.producer_consumer =
341 ACPI_CONSUMER;
50eca3eb
BM
342 resource->res.data.extended_irq.triggering =
343 link->irq.triggering;
344 resource->res.data.extended_irq.polarity =
345 link->irq.polarity;
346 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
347 resource->res.data.irq.sharable =
4be44fcd 348 ACPI_EXCLUSIVE;
1da177e4 349 else
50eca3eb
BM
350 resource->res.data.irq.sharable = ACPI_SHARED;
351 resource->res.data.extended_irq.interrupt_count = 1;
1da177e4
LT
352 resource->res.data.extended_irq.interrupts[0] = irq;
353 /* ignore resource_source, it's optional */
354 break;
355 default:
6468463a 356 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
1da177e4
LT
357 result = -EINVAL;
358 goto end;
359
360 }
50eca3eb 361 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1da177e4
LT
362
363 /* Attempt to set the resource */
67a71365 364 status = acpi_set_current_resources(link->device->handle, &buffer);
1da177e4
LT
365
366 /* check for total failure */
367 if (ACPI_FAILURE(status)) {
a6fc6720 368 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
1da177e4
LT
369 result = -ENODEV;
370 goto end;
371 }
372
373 /* Query _STA, set device->status */
374 result = acpi_bus_get_status(link->device);
375 if (result) {
6468463a 376 printk(KERN_ERR PREFIX "Unable to read status\n");
1da177e4
LT
377 goto end;
378 }
379 if (!link->device->status.enabled) {
cece9296
LB
380 printk(KERN_WARNING PREFIX
381 "%s [%s] disabled and referenced, BIOS bug\n",
a6fc6720 382 acpi_device_name(link->device),
cece9296 383 acpi_device_bid(link->device));
1da177e4
LT
384 }
385
386 /* Query _CRS, set link->irq.active */
387 result = acpi_pci_link_get_current(link);
388 if (result) {
389 goto end;
390 }
391
392 /*
393 * Is current setting not what we set?
394 * set link->irq.active
395 */
396 if (link->irq.active != irq) {
397 /*
398 * policy: when _CRS doesn't return what we just _SRS
399 * assume _SRS worked and override _CRS value.
400 */
cece9296
LB
401 printk(KERN_WARNING PREFIX
402 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
a6fc6720 403 acpi_device_name(link->device),
cece9296 404 acpi_device_bid(link->device), link->irq.active, irq);
1da177e4
LT
405 link->irq.active = irq;
406 }
407
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
4be44fcd
LB
409
410 end:
1da177e4 411 kfree(resource);
d550d98d 412 return result;
1da177e4
LT
413}
414
1da177e4
LT
415/* --------------------------------------------------------------------------
416 PCI Link IRQ Management
417 -------------------------------------------------------------------------- */
418
419/*
420 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
421 * Link Devices to move the PIRQs around to minimize sharing.
422 *
423 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
424 * that the BIOS has already set to active. This is necessary because
425 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
426 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
427 * even if acpi_irq_nobalance is set.
428 *
429 * A tables of penalties avoids directing PCI interrupts to well known
430 * ISA IRQs. Boot params are available to over-ride the default table:
431 *
432 * List interrupts that are free for PCI use.
433 * acpi_irq_pci=n[,m]
434 *
435 * List interrupts that should not be used for PCI:
436 * acpi_irq_isa=n[,m]
437 *
438 * Note that PCI IRQ routers have a list of possible IRQs,
439 * which may not include the IRQs this table says are available.
440 *
441 * Since this heuristic can't tell the difference between a link
442 * that no device will attach to, vs. a link which may be shared
443 * by multiple active devices -- it is not optimal.
444 *
445 * If interrupt performance is that important, get an IO-APIC system
446 * with a pin dedicated to each device. Or for that matter, an MSI
447 * enabled system.
448 */
449
450#define ACPI_MAX_IRQS 256
451#define ACPI_MAX_ISA_IRQ 16
452
453#define PIRQ_PENALTY_PCI_AVAILABLE (0)
454#define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
455#define PIRQ_PENALTY_PCI_USING (16*16*16)
456#define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
457#define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
458#define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
459
460static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
461 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
462 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
463 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
4be44fcd
LB
464 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
465 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
1da177e4
LT
466 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
467 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
468 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
469 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
470 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9 PCI, often acpi */
471 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
472 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
1c9ca3a7
BH
473 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
474 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
475 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
476 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
4be44fcd 477 /* >IRQ15 */
1da177e4
LT
478};
479
4be44fcd 480int __init acpi_irq_penalty_init(void)
1da177e4 481{
c9d62443
BH
482 struct acpi_pci_link *link;
483 int i;
1da177e4 484
1da177e4
LT
485 /*
486 * Update penalties to facilitate IRQ balancing.
487 */
5f0dccaa 488 list_for_each_entry(link, &acpi_link_list, list) {
1da177e4
LT
489
490 /*
491 * reflect the possible and active irqs in the penalty table --
492 * useful for breaking ties.
493 */
494 if (link->irq.possible_count) {
4be44fcd
LB
495 int penalty =
496 PIRQ_PENALTY_PCI_POSSIBLE /
497 link->irq.possible_count;
1da177e4
LT
498
499 for (i = 0; i < link->irq.possible_count; i++) {
500 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
4be44fcd
LB
501 acpi_irq_penalty[link->irq.
502 possible[i]] +=
503 penalty;
1da177e4
LT
504 }
505
506 } else if (link->irq.active) {
4be44fcd
LB
507 acpi_irq_penalty[link->irq.active] +=
508 PIRQ_PENALTY_PCI_POSSIBLE;
1da177e4
LT
509 }
510 }
511 /* Add a penalty for the SCI */
cee324b1 512 acpi_irq_penalty[acpi_gbl_FADT.sci_interrupt] += PIRQ_PENALTY_PCI_USING;
d550d98d 513 return 0;
1da177e4
LT
514}
515
32836259 516static int acpi_irq_balance = -1; /* 0: static, 1: balance */
1da177e4 517
4be44fcd 518static int acpi_pci_link_allocate(struct acpi_pci_link *link)
1da177e4 519{
4be44fcd
LB
520 int irq;
521 int i;
1da177e4 522
87bec66b
DSL
523 if (link->irq.initialized) {
524 if (link->refcnt == 0)
525 /* This means the link is disabled but initialized */
526 acpi_pci_link_set(link, link->irq.active);
d550d98d 527 return 0;
87bec66b 528 }
1da177e4
LT
529
530 /*
531 * search for active IRQ in list of possible IRQs.
532 */
533 for (i = 0; i < link->irq.possible_count; ++i) {
534 if (link->irq.active == link->irq.possible[i])
535 break;
536 }
537 /*
538 * forget active IRQ that is not in possible list
539 */
540 if (i == link->irq.possible_count) {
541 if (acpi_strict)
cece9296
LB
542 printk(KERN_WARNING PREFIX "_CRS %d not found"
543 " in _PRS\n", link->irq.active);
1da177e4
LT
544 link->irq.active = 0;
545 }
546
547 /*
548 * if active found, use it; else pick entry from end of possible list.
549 */
1c9ca3a7 550 if (link->irq.active)
1da177e4 551 irq = link->irq.active;
1c9ca3a7 552 else
1da177e4 553 irq = link->irq.possible[link->irq.possible_count - 1];
1da177e4
LT
554
555 if (acpi_irq_balance || !link->irq.active) {
556 /*
557 * Select the best IRQ. This is done in reverse to promote
558 * the use of IRQs 9, 10, 11, and >15.
559 */
560 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
4be44fcd
LB
561 if (acpi_irq_penalty[irq] >
562 acpi_irq_penalty[link->irq.possible[i]])
1da177e4
LT
563 irq = link->irq.possible[i];
564 }
565 }
566
567 /* Attempt to enable the link device at this IRQ. */
568 if (acpi_pci_link_set(link, irq)) {
6468463a
LB
569 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
570 "Try pci=noacpi or acpi=off\n",
a6fc6720 571 acpi_device_name(link->device),
6468463a 572 acpi_device_bid(link->device));
d550d98d 573 return -ENODEV;
1da177e4
LT
574 } else {
575 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
4d939155 576 printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
4be44fcd
LB
577 acpi_device_name(link->device),
578 acpi_device_bid(link->device), link->irq.active);
1da177e4
LT
579 }
580
581 link->irq.initialized = 1;
d550d98d 582 return 0;
1da177e4
LT
583}
584
585/*
87bec66b 586 * acpi_pci_link_allocate_irq
1da177e4
LT
587 * success: return IRQ >= 0
588 * failure: return -1
589 */
1c9ca3a7
BH
590int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
591 int *polarity, char **name)
1da177e4 592{
c9d62443
BH
593 int result;
594 struct acpi_device *device;
595 struct acpi_pci_link *link;
1da177e4 596
1da177e4
LT
597 result = acpi_bus_get_device(handle, &device);
598 if (result) {
6468463a 599 printk(KERN_ERR PREFIX "Invalid link device\n");
d550d98d 600 return -1;
1da177e4
LT
601 }
602
50dd0969 603 link = acpi_driver_data(device);
1da177e4 604 if (!link) {
6468463a 605 printk(KERN_ERR PREFIX "Invalid link context\n");
d550d98d 606 return -1;
1da177e4
LT
607 }
608
609 /* TBD: Support multiple index (IRQ) entries per Link Device */
610 if (index) {
6468463a 611 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
d550d98d 612 return -1;
1da177e4
LT
613 }
614
36e43095 615 mutex_lock(&acpi_link_lock);
87bec66b 616 if (acpi_pci_link_allocate(link)) {
36e43095 617 mutex_unlock(&acpi_link_lock);
d550d98d 618 return -1;
87bec66b 619 }
4be44fcd 620
1da177e4 621 if (!link->irq.active) {
36e43095 622 mutex_unlock(&acpi_link_lock);
6468463a 623 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
d550d98d 624 return -1;
1da177e4 625 }
4be44fcd 626 link->refcnt++;
36e43095 627 mutex_unlock(&acpi_link_lock);
1da177e4 628
50eca3eb
BM
629 if (triggering)
630 *triggering = link->irq.triggering;
631 if (polarity)
632 *polarity = link->irq.polarity;
4be44fcd
LB
633 if (name)
634 *name = acpi_device_bid(link->device);
87bec66b 635 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
636 "Link %s is referenced\n",
637 acpi_device_bid(link->device)));
d550d98d 638 return (link->irq.active);
1da177e4
LT
639}
640
87bec66b
DSL
641/*
642 * We don't change link's irq information here. After it is reenabled, we
643 * continue use the info
644 */
4be44fcd 645int acpi_pci_link_free_irq(acpi_handle handle)
87bec66b 646{
c9d62443
BH
647 struct acpi_device *device;
648 struct acpi_pci_link *link;
4be44fcd 649 acpi_status result;
87bec66b 650
87bec66b
DSL
651 result = acpi_bus_get_device(handle, &device);
652 if (result) {
6468463a 653 printk(KERN_ERR PREFIX "Invalid link device\n");
d550d98d 654 return -1;
87bec66b 655 }
1da177e4 656
50dd0969 657 link = acpi_driver_data(device);
87bec66b 658 if (!link) {
6468463a 659 printk(KERN_ERR PREFIX "Invalid link context\n");
d550d98d 660 return -1;
87bec66b
DSL
661 }
662
36e43095 663 mutex_lock(&acpi_link_lock);
87bec66b 664 if (!link->irq.initialized) {
36e43095 665 mutex_unlock(&acpi_link_lock);
6468463a 666 printk(KERN_ERR PREFIX "Link isn't initialized\n");
d550d98d 667 return -1;
87bec66b 668 }
ecc21ebe
DSL
669#ifdef FUTURE_USE
670 /*
671 * The Link reference count allows us to _DISable an unused link
672 * and suspend time, and set it again on resume.
673 * However, 2.6.12 still has irq_router.resume
674 * which blindly restores the link state.
675 * So we disable the reference count method
676 * to prevent duplicate acpi_pci_link_set()
677 * which would harm some systems
678 */
4be44fcd 679 link->refcnt--;
ecc21ebe 680#endif
87bec66b 681 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
682 "Link %s is dereferenced\n",
683 acpi_device_bid(link->device)));
87bec66b 684
1c9ca3a7 685 if (link->refcnt == 0)
383d7a11 686 acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
1c9ca3a7 687
36e43095 688 mutex_unlock(&acpi_link_lock);
d550d98d 689 return (link->irq.active);
87bec66b 690}
4be44fcd 691
1da177e4
LT
692/* --------------------------------------------------------------------------
693 Driver Interface
694 -------------------------------------------------------------------------- */
695
4be44fcd 696static int acpi_pci_link_add(struct acpi_device *device)
1da177e4 697{
c9d62443
BH
698 int result;
699 struct acpi_pci_link *link;
700 int i;
4be44fcd 701 int found = 0;
1da177e4 702
36bcbec7 703 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
1da177e4 704 if (!link)
d550d98d 705 return -ENOMEM;
1da177e4
LT
706
707 link->device = device;
1da177e4
LT
708 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
709 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
db89b4f0 710 device->driver_data = link;
1da177e4 711
36e43095 712 mutex_lock(&acpi_link_lock);
1da177e4
LT
713 result = acpi_pci_link_get_possible(link);
714 if (result)
715 goto end;
716
717 /* query and set link->irq.active */
718 acpi_pci_link_get_current(link);
719
0dc070bb 720 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
4be44fcd 721 acpi_device_bid(device));
1da177e4
LT
722 for (i = 0; i < link->irq.possible_count; i++) {
723 if (link->irq.active == link->irq.possible[i]) {
724 printk(" *%d", link->irq.possible[i]);
725 found = 1;
4be44fcd 726 } else
1da177e4
LT
727 printk(" %d", link->irq.possible[i]);
728 }
729
730 printk(")");
731
732 if (!found)
733 printk(" *%d", link->irq.active);
734
4be44fcd 735 if (!link->device->status.enabled)
1da177e4
LT
736 printk(", disabled.");
737
738 printk("\n");
739
5f0dccaa 740 list_add_tail(&link->list, &acpi_link_list);
1da177e4 741
4be44fcd 742 end:
1da177e4 743 /* disable all links -- to be activated on use */
383d7a11 744 acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
36e43095 745 mutex_unlock(&acpi_link_lock);
1da177e4
LT
746
747 if (result)
748 kfree(link);
749
d550d98d 750 return result;
1da177e4
LT
751}
752
4be44fcd 753static int acpi_pci_link_resume(struct acpi_pci_link *link)
697a2d63 754{
697a2d63 755 if (link->refcnt && link->irq.active && link->irq.initialized)
d550d98d 756 return (acpi_pci_link_set(link, link->irq.active));
1c9ca3a7
BH
757
758 return 0;
697a2d63
LT
759}
760
4be44fcd 761static int irqrouter_resume(struct sys_device *dev)
1da177e4 762{
c9d62443 763 struct acpi_pci_link *link;
1da177e4 764
5f0dccaa 765 list_for_each_entry(link, &acpi_link_list, list) {
697a2d63 766 acpi_pci_link_resume(link);
1da177e4 767 }
d550d98d 768 return 0;
1da177e4
LT
769}
770
4be44fcd 771static int acpi_pci_link_remove(struct acpi_device *device, int type)
1da177e4 772{
c9d62443 773 struct acpi_pci_link *link;
1da177e4 774
50dd0969 775 link = acpi_driver_data(device);
1da177e4 776
36e43095 777 mutex_lock(&acpi_link_lock);
5f0dccaa 778 list_del(&link->list);
36e43095 779 mutex_unlock(&acpi_link_lock);
1da177e4
LT
780
781 kfree(link);
d550d98d 782 return 0;
1da177e4
LT
783}
784
785/*
786 * modify acpi_irq_penalty[] from cmdline
787 */
788static int __init acpi_irq_penalty_update(char *str, int used)
789{
790 int i;
791
792 for (i = 0; i < 16; i++) {
793 int retval;
794 int irq;
795
4be44fcd 796 retval = get_option(&str, &irq);
1da177e4
LT
797
798 if (!retval)
799 break; /* no number found */
800
801 if (irq < 0)
802 continue;
4be44fcd 803
fa46d352 804 if (irq >= ARRAY_SIZE(acpi_irq_penalty))
1da177e4
LT
805 continue;
806
807 if (used)
808 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
809 else
810 acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
811
812 if (retval != 2) /* no next number */
813 break;
814 }
815 return 1;
816}
817
818/*
819 * We'd like PNP to call this routine for the
820 * single ISA_USED value for each legacy device.
821 * But instead it calls us with each POSSIBLE setting.
822 * There is no ISA_POSSIBLE weight, so we simply use
823 * the (small) PCI_USING penalty.
824 */
c9c3e457 825void acpi_penalize_isa_irq(int irq, int active)
1da177e4 826{
fa46d352
BH
827 if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
828 if (active)
829 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
830 else
831 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
832 }
1da177e4
LT
833}
834
835/*
836 * Over-ride default table to reserve additional IRQs for use by ISA
837 * e.g. acpi_irq_isa=5
838 * Useful for telling ACPI how not to interfere with your ISA sound card.
839 */
840static int __init acpi_irq_isa(char *str)
841{
842 return acpi_irq_penalty_update(str, 1);
843}
4be44fcd 844
1da177e4
LT
845__setup("acpi_irq_isa=", acpi_irq_isa);
846
847/*
848 * Over-ride default table to free additional IRQs for use by PCI
849 * e.g. acpi_irq_pci=7,15
850 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
851 */
852static int __init acpi_irq_pci(char *str)
853{
854 return acpi_irq_penalty_update(str, 0);
855}
4be44fcd 856
1da177e4
LT
857__setup("acpi_irq_pci=", acpi_irq_pci);
858
859static int __init acpi_irq_nobalance_set(char *str)
860{
861 acpi_irq_balance = 0;
862 return 1;
863}
4be44fcd 864
1da177e4
LT
865__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
866
8a383ef0 867static int __init acpi_irq_balance_set(char *str)
1da177e4
LT
868{
869 acpi_irq_balance = 1;
870 return 1;
871}
1da177e4 872
4be44fcd 873__setup("acpi_irq_balance", acpi_irq_balance_set);
1da177e4 874
87bec66b 875/* FIXME: we will remove this interface after all drivers call pci_disable_device */
1da177e4 876static struct sysdev_class irqrouter_sysdev_class = {
af5ca3f4 877 .name = "irqrouter",
4be44fcd 878 .resume = irqrouter_resume,
1da177e4
LT
879};
880
1da177e4 881static struct sys_device device_irqrouter = {
4be44fcd
LB
882 .id = 0,
883 .cls = &irqrouter_sysdev_class,
1da177e4
LT
884};
885
1da177e4
LT
886static int __init irqrouter_init_sysfs(void)
887{
888 int error;
889
1da177e4 890 if (acpi_disabled || acpi_noirq)
d550d98d 891 return 0;
1da177e4
LT
892
893 error = sysdev_class_register(&irqrouter_sysdev_class);
894 if (!error)
895 error = sysdev_register(&device_irqrouter);
896
d550d98d 897 return error;
4be44fcd 898}
1da177e4
LT
899
900device_initcall(irqrouter_init_sysfs);
901
4be44fcd 902static int __init acpi_pci_link_init(void)
1da177e4 903{
1da177e4 904 if (acpi_noirq)
d550d98d 905 return 0;
1da177e4 906
32836259
BH
907 if (acpi_irq_balance == -1) {
908 /* no command line switch: enable balancing in IOAPIC mode */
909 if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
910 acpi_irq_balance = 1;
911 else
912 acpi_irq_balance = 0;
913 }
914
1da177e4 915 if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
d550d98d 916 return -ENODEV;
1da177e4 917
d550d98d 918 return 0;
1da177e4
LT
919}
920
921subsys_initcall(acpi_pci_link_init);