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