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