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