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