]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/pci_link.c
ACPI / PCI: Make PCI root driver use struct acpi_scan_handler
[mirror_ubuntu-zesty-kernel.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>
1da177e4
LT
42
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45
a192a958
LB
46#define PREFIX "ACPI: "
47
1c9ca3a7 48#define _COMPONENT ACPI_PCI_COMPONENT
f52fd66d 49ACPI_MODULE_NAME("pci_link");
1da177e4 50#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
1da177e4
LT
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"
1c9ca3a7
BH
54#define ACPI_PCI_LINK_MAX_POSSIBLE 16
55
4be44fcd 56static int acpi_pci_link_add(struct acpi_device *device);
51fac838 57static int acpi_pci_link_remove(struct acpi_device *device);
1da177e4 58
c97adf9e 59static const struct acpi_device_id link_device_ids[] = {
1ba90e3a
TR
60 {"PNP0C0F", 0},
61 {"", 0},
62};
63MODULE_DEVICE_TABLE(acpi, link_device_ids);
64
1da177e4 65static struct acpi_driver acpi_pci_link_driver = {
c2b6705b 66 .name = "pci_link",
4be44fcd 67 .class = ACPI_PCI_LINK_CLASS,
1ba90e3a 68 .ids = link_device_ids,
4be44fcd
LB
69 .ops = {
70 .add = acpi_pci_link_add,
71 .remove = acpi_pci_link_remove,
1c9ca3a7 72 },
1da177e4
LT
73};
74
87bec66b
DSL
75/*
76 * If a link is initialized, we never change its active and initialized
77 * later even the link is disable. Instead, we just repick the active irq
78 */
1da177e4 79struct acpi_pci_link_irq {
4be44fcd 80 u8 active; /* Current IRQ */
50eca3eb 81 u8 triggering; /* All IRQs */
1c9ca3a7 82 u8 polarity; /* All IRQs */
4be44fcd
LB
83 u8 resource_type;
84 u8 possible_count;
85 u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
86 u8 initialized:1;
87 u8 reserved:7;
1da177e4
LT
88};
89
90struct acpi_pci_link {
5f0dccaa 91 struct list_head list;
1c9ca3a7
BH
92 struct acpi_device *device;
93 struct acpi_pci_link_irq irq;
94 int refcnt;
1da177e4
LT
95};
96
5f0dccaa 97static LIST_HEAD(acpi_link_list);
e5685b9d 98static DEFINE_MUTEX(acpi_link_lock);
1da177e4 99
1da177e4
LT
100/* --------------------------------------------------------------------------
101 PCI Link Device Management
102 -------------------------------------------------------------------------- */
103
104/*
105 * set context (link) possible list from resource list
106 */
1c9ca3a7
BH
107static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
108 void *context)
1da177e4 109{
50dd0969 110 struct acpi_pci_link *link = context;
c9d62443 111 u32 i;
1da177e4 112
eca008c8 113 switch (resource->type) {
50eca3eb 114 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
4a5e3638 115 case ACPI_RESOURCE_TYPE_END_TAG:
d550d98d 116 return AE_OK;
50eca3eb 117 case ACPI_RESOURCE_TYPE_IRQ:
4be44fcd
LB
118 {
119 struct acpi_resource_irq *p = &resource->data.irq;
50eca3eb 120 if (!p || !p->interrupt_count) {
4a5e3638
BH
121 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
122 "Blank _PRS IRQ resource\n"));
d550d98d 123 return AE_OK;
1da177e4 124 }
4be44fcd 125 for (i = 0;
50eca3eb 126 (i < p->interrupt_count
4be44fcd
LB
127 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
128 if (!p->interrupts[i]) {
4a5e3638
BH
129 printk(KERN_WARNING PREFIX
130 "Invalid _PRS IRQ %d\n",
131 p->interrupts[i]);
4be44fcd
LB
132 continue;
133 }
134 link->irq.possible[i] = p->interrupts[i];
135 link->irq.possible_count++;
136 }
50eca3eb
BM
137 link->irq.triggering = p->triggering;
138 link->irq.polarity = p->polarity;
139 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
4be44fcd 140 break;
1da177e4 141 }
50eca3eb 142 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
4be44fcd 143 {
50eca3eb 144 struct acpi_resource_extended_irq *p =
4be44fcd 145 &resource->data.extended_irq;
50eca3eb 146 if (!p || !p->interrupt_count) {
cece9296 147 printk(KERN_WARNING PREFIX
4a5e3638 148 "Blank _PRS EXT IRQ resource\n");
d550d98d 149 return AE_OK;
4be44fcd
LB
150 }
151 for (i = 0;
50eca3eb 152 (i < p->interrupt_count
4be44fcd
LB
153 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
154 if (!p->interrupts[i]) {
4a5e3638
BH
155 printk(KERN_WARNING PREFIX
156 "Invalid _PRS IRQ %d\n",
157 p->interrupts[i]);
4be44fcd
LB
158 continue;
159 }
160 link->irq.possible[i] = p->interrupts[i];
161 link->irq.possible_count++;
1da177e4 162 }
50eca3eb
BM
163 link->irq.triggering = p->triggering;
164 link->irq.polarity = p->polarity;
165 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
4be44fcd 166 break;
1da177e4 167 }
1da177e4 168 default:
4a5e3638
BH
169 printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
170 resource->type);
d550d98d 171 return AE_OK;
1da177e4
LT
172 }
173
d550d98d 174 return AE_CTRL_TERMINATE;
1da177e4
LT
175}
176
4be44fcd 177static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
1da177e4 178{
4be44fcd 179 acpi_status status;
1da177e4 180
67a71365 181 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
4be44fcd 182 acpi_pci_link_check_possible, link);
1da177e4 183 if (ACPI_FAILURE(status)) {
a6fc6720 184 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
d550d98d 185 return -ENODEV;
1da177e4
LT
186 }
187
4be44fcd
LB
188 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
189 "Found %d possible IRQs\n",
190 link->irq.possible_count));
1da177e4 191
d550d98d 192 return 0;
1da177e4
LT
193}
194
1c9ca3a7
BH
195static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
196 void *context)
1da177e4 197{
c9d62443 198 int *irq = context;
1da177e4 199
eca008c8 200 switch (resource->type) {
4a5e3638
BH
201 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
202 case ACPI_RESOURCE_TYPE_END_TAG:
203 return AE_OK;
50eca3eb 204 case ACPI_RESOURCE_TYPE_IRQ:
4be44fcd
LB
205 {
206 struct acpi_resource_irq *p = &resource->data.irq;
50eca3eb 207 if (!p || !p->interrupt_count) {
4be44fcd
LB
208 /*
209 * IRQ descriptors may have no IRQ# bits set,
210 * particularly those those w/ _STA disabled
211 */
212 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4a5e3638 213 "Blank _CRS IRQ resource\n"));
d550d98d 214 return AE_OK;
4be44fcd
LB
215 }
216 *irq = p->interrupts[0];
217 break;
1da177e4 218 }
50eca3eb 219 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
4be44fcd 220 {
50eca3eb 221 struct acpi_resource_extended_irq *p =
4be44fcd 222 &resource->data.extended_irq;
50eca3eb 223 if (!p || !p->interrupt_count) {
4be44fcd
LB
224 /*
225 * extended IRQ descriptors must
226 * return at least 1 IRQ
227 */
cece9296 228 printk(KERN_WARNING PREFIX
4a5e3638 229 "Blank _CRS EXT IRQ resource\n");
d550d98d 230 return AE_OK;
4be44fcd
LB
231 }
232 *irq = p->interrupts[0];
233 break;
1da177e4 234 }
d4ec6c7c 235 break;
1da177e4 236 default:
4a5e3638
BH
237 printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
238 resource->type);
d550d98d 239 return AE_OK;
1da177e4 240 }
4a5e3638 241
d550d98d 242 return AE_CTRL_TERMINATE;
1da177e4
LT
243}
244
245/*
246 * Run _CRS and set link->irq.active
247 *
248 * return value:
249 * 0 - success
250 * !0 - failure
251 */
4be44fcd 252static int acpi_pci_link_get_current(struct acpi_pci_link *link)
1da177e4 253{
4be44fcd 254 int result = 0;
c9d62443 255 acpi_status status;
4be44fcd 256 int irq = 0;
1da177e4 257
1da177e4
LT
258 link->irq.active = 0;
259
260 /* in practice, status disabled is meaningless, ignore it */
261 if (acpi_strict) {
262 /* Query _STA, set link->device->status */
263 result = acpi_bus_get_status(link->device);
264 if (result) {
6468463a 265 printk(KERN_ERR PREFIX "Unable to read status\n");
1da177e4
LT
266 goto end;
267 }
268
269 if (!link->device->status.enabled) {
270 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
d550d98d 271 return 0;
1da177e4
LT
272 }
273 }
274
275 /*
276 * Query and parse _CRS to get the current IRQ assignment.
277 */
278
67a71365 279 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
4be44fcd 280 acpi_pci_link_check_current, &irq);
1da177e4 281 if (ACPI_FAILURE(status)) {
a6fc6720 282 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
1da177e4
LT
283 result = -ENODEV;
284 goto end;
285 }
286
287 if (acpi_strict && !irq) {
6468463a 288 printk(KERN_ERR PREFIX "_CRS returned 0\n");
1da177e4
LT
289 result = -ENODEV;
290 }
291
292 link->irq.active = irq;
293
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
295
4be44fcd 296 end:
d550d98d 297 return result;
1da177e4
LT
298}
299
4be44fcd 300static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
1da177e4 301{
c9d62443
BH
302 int result;
303 acpi_status status;
1da177e4 304 struct {
4be44fcd
LB
305 struct acpi_resource res;
306 struct acpi_resource end;
307 } *resource;
308 struct acpi_buffer buffer = { 0, NULL };
1da177e4 309
6eca4b4c 310 if (!irq)
d550d98d 311 return -EINVAL;
1da177e4 312
36bcbec7 313 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
4be44fcd 314 if (!resource)
d550d98d 315 return -ENOMEM;
1da177e4 316
4be44fcd 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 */
67a71365 363 status = acpi_set_current_resources(link->device->handle, &buffer);
1da177e4
LT
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 */
1c9ca3a7
BH
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{
c9d62443
BH
481 struct acpi_pci_link *link;
482 int i;
1da177e4 483
1da177e4
LT
484 /*
485 * Update penalties to facilitate IRQ balancing.
486 */
5f0dccaa 487 list_for_each_entry(link, &acpi_link_list, list) {
1da177e4
LT
488
489 /*
490 * reflect the possible and active irqs in the penalty table --
491 * useful for breaking ties.
492 */
493 if (link->irq.possible_count) {
4be44fcd
LB
494 int penalty =
495 PIRQ_PENALTY_PCI_POSSIBLE /
496 link->irq.possible_count;
1da177e4
LT
497
498 for (i = 0; i < link->irq.possible_count; i++) {
499 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
4be44fcd
LB
500 acpi_irq_penalty[link->irq.
501 possible[i]] +=
502 penalty;
1da177e4
LT
503 }
504
505 } else if (link->irq.active) {
4be44fcd
LB
506 acpi_irq_penalty[link->irq.active] +=
507 PIRQ_PENALTY_PCI_POSSIBLE;
1da177e4
LT
508 }
509 }
510 /* Add a penalty for the SCI */
cee324b1 511 acpi_irq_penalty[acpi_gbl_FADT.sci_interrupt] += PIRQ_PENALTY_PCI_USING;
d550d98d 512 return 0;
1da177e4
LT
513}
514
32836259 515static int acpi_irq_balance = -1; /* 0: static, 1: balance */
1da177e4 516
4be44fcd 517static int acpi_pci_link_allocate(struct acpi_pci_link *link)
1da177e4 518{
4be44fcd
LB
519 int irq;
520 int i;
1da177e4 521
87bec66b
DSL
522 if (link->irq.initialized) {
523 if (link->refcnt == 0)
524 /* This means the link is disabled but initialized */
525 acpi_pci_link_set(link, link->irq.active);
d550d98d 526 return 0;
87bec66b 527 }
1da177e4
LT
528
529 /*
530 * search for active IRQ in list of possible IRQs.
531 */
532 for (i = 0; i < link->irq.possible_count; ++i) {
533 if (link->irq.active == link->irq.possible[i])
534 break;
535 }
536 /*
537 * forget active IRQ that is not in possible list
538 */
539 if (i == link->irq.possible_count) {
540 if (acpi_strict)
cece9296
LB
541 printk(KERN_WARNING PREFIX "_CRS %d not found"
542 " in _PRS\n", link->irq.active);
1da177e4
LT
543 link->irq.active = 0;
544 }
545
546 /*
547 * if active found, use it; else pick entry from end of possible list.
548 */
1c9ca3a7 549 if (link->irq.active)
1da177e4 550 irq = link->irq.active;
1c9ca3a7 551 else
1da177e4 552 irq = link->irq.possible[link->irq.possible_count - 1];
1da177e4
LT
553
554 if (acpi_irq_balance || !link->irq.active) {
555 /*
556 * Select the best IRQ. This is done in reverse to promote
557 * the use of IRQs 9, 10, 11, and >15.
558 */
559 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
4be44fcd
LB
560 if (acpi_irq_penalty[irq] >
561 acpi_irq_penalty[link->irq.possible[i]])
1da177e4
LT
562 irq = link->irq.possible[i];
563 }
564 }
565
566 /* Attempt to enable the link device at this IRQ. */
567 if (acpi_pci_link_set(link, irq)) {
6468463a
LB
568 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
569 "Try pci=noacpi or acpi=off\n",
a6fc6720 570 acpi_device_name(link->device),
6468463a 571 acpi_device_bid(link->device));
d550d98d 572 return -ENODEV;
1da177e4
LT
573 } else {
574 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
4d939155 575 printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
4be44fcd
LB
576 acpi_device_name(link->device),
577 acpi_device_bid(link->device), link->irq.active);
1da177e4
LT
578 }
579
580 link->irq.initialized = 1;
d550d98d 581 return 0;
1da177e4
LT
582}
583
584/*
87bec66b 585 * acpi_pci_link_allocate_irq
1da177e4
LT
586 * success: return IRQ >= 0
587 * failure: return -1
588 */
1c9ca3a7
BH
589int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
590 int *polarity, char **name)
1da177e4 591{
c9d62443
BH
592 int result;
593 struct acpi_device *device;
594 struct acpi_pci_link *link;
1da177e4 595
1da177e4
LT
596 result = acpi_bus_get_device(handle, &device);
597 if (result) {
6468463a 598 printk(KERN_ERR PREFIX "Invalid link device\n");
d550d98d 599 return -1;
1da177e4
LT
600 }
601
50dd0969 602 link = acpi_driver_data(device);
1da177e4 603 if (!link) {
6468463a 604 printk(KERN_ERR PREFIX "Invalid link context\n");
d550d98d 605 return -1;
1da177e4
LT
606 }
607
608 /* TBD: Support multiple index (IRQ) entries per Link Device */
609 if (index) {
6468463a 610 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
d550d98d 611 return -1;
1da177e4
LT
612 }
613
36e43095 614 mutex_lock(&acpi_link_lock);
87bec66b 615 if (acpi_pci_link_allocate(link)) {
36e43095 616 mutex_unlock(&acpi_link_lock);
d550d98d 617 return -1;
87bec66b 618 }
4be44fcd 619
1da177e4 620 if (!link->irq.active) {
36e43095 621 mutex_unlock(&acpi_link_lock);
6468463a 622 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
d550d98d 623 return -1;
1da177e4 624 }
4be44fcd 625 link->refcnt++;
36e43095 626 mutex_unlock(&acpi_link_lock);
1da177e4 627
50eca3eb
BM
628 if (triggering)
629 *triggering = link->irq.triggering;
630 if (polarity)
631 *polarity = link->irq.polarity;
4be44fcd
LB
632 if (name)
633 *name = acpi_device_bid(link->device);
87bec66b 634 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
635 "Link %s is referenced\n",
636 acpi_device_bid(link->device)));
d550d98d 637 return (link->irq.active);
1da177e4
LT
638}
639
87bec66b
DSL
640/*
641 * We don't change link's irq information here. After it is reenabled, we
642 * continue use the info
643 */
4be44fcd 644int acpi_pci_link_free_irq(acpi_handle handle)
87bec66b 645{
c9d62443
BH
646 struct acpi_device *device;
647 struct acpi_pci_link *link;
4be44fcd 648 acpi_status result;
87bec66b 649
87bec66b
DSL
650 result = acpi_bus_get_device(handle, &device);
651 if (result) {
6468463a 652 printk(KERN_ERR PREFIX "Invalid link device\n");
d550d98d 653 return -1;
87bec66b 654 }
1da177e4 655
50dd0969 656 link = acpi_driver_data(device);
87bec66b 657 if (!link) {
6468463a 658 printk(KERN_ERR PREFIX "Invalid link context\n");
d550d98d 659 return -1;
87bec66b
DSL
660 }
661
36e43095 662 mutex_lock(&acpi_link_lock);
87bec66b 663 if (!link->irq.initialized) {
36e43095 664 mutex_unlock(&acpi_link_lock);
6468463a 665 printk(KERN_ERR PREFIX "Link isn't initialized\n");
d550d98d 666 return -1;
87bec66b 667 }
ecc21ebe
DSL
668#ifdef FUTURE_USE
669 /*
670 * The Link reference count allows us to _DISable an unused link
671 * and suspend time, and set it again on resume.
672 * However, 2.6.12 still has irq_router.resume
673 * which blindly restores the link state.
674 * So we disable the reference count method
675 * to prevent duplicate acpi_pci_link_set()
676 * which would harm some systems
677 */
4be44fcd 678 link->refcnt--;
ecc21ebe 679#endif
87bec66b 680 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
681 "Link %s is dereferenced\n",
682 acpi_device_bid(link->device)));
87bec66b 683
1c9ca3a7 684 if (link->refcnt == 0)
383d7a11 685 acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
1c9ca3a7 686
36e43095 687 mutex_unlock(&acpi_link_lock);
d550d98d 688 return (link->irq.active);
87bec66b 689}
4be44fcd 690
1da177e4
LT
691/* --------------------------------------------------------------------------
692 Driver Interface
693 -------------------------------------------------------------------------- */
694
4be44fcd 695static int acpi_pci_link_add(struct acpi_device *device)
1da177e4 696{
c9d62443
BH
697 int result;
698 struct acpi_pci_link *link;
699 int i;
4be44fcd 700 int found = 0;
1da177e4 701
36bcbec7 702 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
1da177e4 703 if (!link)
d550d98d 704 return -ENOMEM;
1da177e4
LT
705
706 link->device = device;
1da177e4
LT
707 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
708 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
db89b4f0 709 device->driver_data = link;
1da177e4 710
36e43095 711 mutex_lock(&acpi_link_lock);
1da177e4
LT
712 result = acpi_pci_link_get_possible(link);
713 if (result)
714 goto end;
715
716 /* query and set link->irq.active */
717 acpi_pci_link_get_current(link);
718
0dc070bb 719 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
4be44fcd 720 acpi_device_bid(device));
1da177e4
LT
721 for (i = 0; i < link->irq.possible_count; i++) {
722 if (link->irq.active == link->irq.possible[i]) {
be96447e 723 printk(KERN_CONT " *%d", link->irq.possible[i]);
1da177e4 724 found = 1;
4be44fcd 725 } else
be96447e 726 printk(KERN_CONT " %d", link->irq.possible[i]);
1da177e4
LT
727 }
728
be96447e 729 printk(KERN_CONT ")");
1da177e4
LT
730
731 if (!found)
be96447e 732 printk(KERN_CONT " *%d", link->irq.active);
1da177e4 733
4be44fcd 734 if (!link->device->status.enabled)
be96447e 735 printk(KERN_CONT ", disabled.");
1da177e4 736
be96447e 737 printk(KERN_CONT "\n");
1da177e4 738
5f0dccaa 739 list_add_tail(&link->list, &acpi_link_list);
1da177e4 740
4be44fcd 741 end:
1da177e4 742 /* disable all links -- to be activated on use */
383d7a11 743 acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
36e43095 744 mutex_unlock(&acpi_link_lock);
1da177e4
LT
745
746 if (result)
747 kfree(link);
748
d550d98d 749 return result;
1da177e4
LT
750}
751
4be44fcd 752static int acpi_pci_link_resume(struct acpi_pci_link *link)
697a2d63 753{
697a2d63 754 if (link->refcnt && link->irq.active && link->irq.initialized)
d550d98d 755 return (acpi_pci_link_set(link, link->irq.active));
1c9ca3a7
BH
756
757 return 0;
697a2d63
LT
758}
759
c3146df2 760static void irqrouter_resume(void)
1da177e4 761{
c9d62443 762 struct acpi_pci_link *link;
1da177e4 763
5f0dccaa 764 list_for_each_entry(link, &acpi_link_list, list) {
697a2d63 765 acpi_pci_link_resume(link);
1da177e4 766 }
1da177e4
LT
767}
768
51fac838 769static int acpi_pci_link_remove(struct acpi_device *device)
1da177e4 770{
c9d62443 771 struct acpi_pci_link *link;
1da177e4 772
50dd0969 773 link = acpi_driver_data(device);
1da177e4 774
36e43095 775 mutex_lock(&acpi_link_lock);
5f0dccaa 776 list_del(&link->list);
36e43095 777 mutex_unlock(&acpi_link_lock);
1da177e4
LT
778
779 kfree(link);
d550d98d 780 return 0;
1da177e4
LT
781}
782
783/*
784 * modify acpi_irq_penalty[] from cmdline
785 */
786static int __init acpi_irq_penalty_update(char *str, int used)
787{
788 int i;
789
790 for (i = 0; i < 16; i++) {
791 int retval;
792 int irq;
793
4be44fcd 794 retval = get_option(&str, &irq);
1da177e4
LT
795
796 if (!retval)
797 break; /* no number found */
798
799 if (irq < 0)
800 continue;
4be44fcd 801
fa46d352 802 if (irq >= ARRAY_SIZE(acpi_irq_penalty))
1da177e4
LT
803 continue;
804
805 if (used)
806 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
807 else
808 acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
809
810 if (retval != 2) /* no next number */
811 break;
812 }
813 return 1;
814}
815
816/*
817 * We'd like PNP to call this routine for the
818 * single ISA_USED value for each legacy device.
819 * But instead it calls us with each POSSIBLE setting.
820 * There is no ISA_POSSIBLE weight, so we simply use
821 * the (small) PCI_USING penalty.
822 */
c9c3e457 823void acpi_penalize_isa_irq(int irq, int active)
1da177e4 824{
fa46d352
BH
825 if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
826 if (active)
827 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
828 else
829 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
830 }
1da177e4
LT
831}
832
833/*
834 * Over-ride default table to reserve additional IRQs for use by ISA
835 * e.g. acpi_irq_isa=5
836 * Useful for telling ACPI how not to interfere with your ISA sound card.
837 */
838static int __init acpi_irq_isa(char *str)
839{
840 return acpi_irq_penalty_update(str, 1);
841}
4be44fcd 842
1da177e4
LT
843__setup("acpi_irq_isa=", acpi_irq_isa);
844
845/*
846 * Over-ride default table to free additional IRQs for use by PCI
847 * e.g. acpi_irq_pci=7,15
848 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
849 */
850static int __init acpi_irq_pci(char *str)
851{
852 return acpi_irq_penalty_update(str, 0);
853}
4be44fcd 854
1da177e4
LT
855__setup("acpi_irq_pci=", acpi_irq_pci);
856
857static int __init acpi_irq_nobalance_set(char *str)
858{
859 acpi_irq_balance = 0;
860 return 1;
861}
4be44fcd 862
1da177e4
LT
863__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
864
8a383ef0 865static int __init acpi_irq_balance_set(char *str)
1da177e4
LT
866{
867 acpi_irq_balance = 1;
868 return 1;
869}
1da177e4 870
4be44fcd 871__setup("acpi_irq_balance", acpi_irq_balance_set);
1da177e4 872
c3146df2 873static struct syscore_ops irqrouter_syscore_ops = {
4be44fcd 874 .resume = irqrouter_resume,
1da177e4
LT
875};
876
c3146df2 877static int __init irqrouter_init_ops(void)
1da177e4 878{
c3146df2
RW
879 if (!acpi_disabled && !acpi_noirq)
880 register_syscore_ops(&irqrouter_syscore_ops);
1da177e4 881
c3146df2 882 return 0;
4be44fcd 883}
1da177e4 884
c3146df2 885device_initcall(irqrouter_init_ops);
1da177e4 886
4be44fcd 887static int __init acpi_pci_link_init(void)
1da177e4 888{
1da177e4 889 if (acpi_noirq)
d550d98d 890 return 0;
1da177e4 891
32836259
BH
892 if (acpi_irq_balance == -1) {
893 /* no command line switch: enable balancing in IOAPIC mode */
894 if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
895 acpi_irq_balance = 1;
896 else
897 acpi_irq_balance = 0;
898 }
899
1da177e4 900 if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
d550d98d 901 return -ENODEV;
1da177e4 902
d550d98d 903 return 0;
1da177e4
LT
904}
905
906subsys_initcall(acpi_pci_link_init);