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