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