blob: 6a29610edc112f6a047b229a2fd4bbeb051b1309 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2002 Dominik Brodowski <devel@brodo.de>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 *
26 * TBD:
27 * 1. Support more than one IRQ resource entry per link device (index).
28 * 2. Implement start/stop mechanism and use ACPI Bus Driver facilities
29 * for IRQ management (e.g. start()->_SRS).
30 */
31
32#include <linux/sysdev.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/types.h>
37#include <linux/proc_fs.h>
38#include <linux/spinlock.h>
39#include <linux/pm.h>
40#include <linux/pci.h>
41
42#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44
45
46#define _COMPONENT ACPI_PCI_COMPONENT
47ACPI_MODULE_NAME ("pci_link")
48
49#define ACPI_PCI_LINK_CLASS "pci_irq_routing"
50#define ACPI_PCI_LINK_HID "PNP0C0F"
51#define ACPI_PCI_LINK_DRIVER_NAME "ACPI PCI Interrupt Link Driver"
52#define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
53#define ACPI_PCI_LINK_FILE_INFO "info"
54#define ACPI_PCI_LINK_FILE_STATUS "state"
55
56#define ACPI_PCI_LINK_MAX_POSSIBLE 16
57
58static int acpi_pci_link_add (struct acpi_device *device);
59static int acpi_pci_link_remove (struct acpi_device *device, int type);
60
61static struct acpi_driver acpi_pci_link_driver = {
62 .name = ACPI_PCI_LINK_DRIVER_NAME,
63 .class = ACPI_PCI_LINK_CLASS,
64 .ids = ACPI_PCI_LINK_HID,
65 .ops = {
66 .add = acpi_pci_link_add,
67 .remove = acpi_pci_link_remove,
68 },
69};
70
David Shaohua Li87bec662005-07-27 23:02:00 -040071/*
72 * If a link is initialized, we never change its active and initialized
73 * later even the link is disable. Instead, we just repick the active irq
74 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct acpi_pci_link_irq {
76 u8 active; /* Current IRQ */
77 u8 edge_level; /* All IRQs */
78 u8 active_high_low; /* All IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 u8 resource_type;
80 u8 possible_count;
81 u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
David Shaohua Li362b06b2005-03-18 16:30:29 -050082 u8 initialized:1;
David Shaohua Li87bec662005-07-27 23:02:00 -040083 u8 reserved:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86struct acpi_pci_link {
87 struct list_head node;
88 struct acpi_device *device;
89 acpi_handle handle;
90 struct acpi_pci_link_irq irq;
David Shaohua Li87bec662005-07-27 23:02:00 -040091 int refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94static struct {
95 int count;
96 struct list_head entries;
97} acpi_link;
David Shaohua Li87bec662005-07-27 23:02:00 -040098DECLARE_MUTEX(acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100
101/* --------------------------------------------------------------------------
102 PCI Link Device Management
103 -------------------------------------------------------------------------- */
104
105/*
106 * set context (link) possible list from resource list
107 */
108static acpi_status
109acpi_pci_link_check_possible (
110 struct acpi_resource *resource,
111 void *context)
112{
113 struct acpi_pci_link *link = (struct acpi_pci_link *) context;
114 u32 i = 0;
115
116 ACPI_FUNCTION_TRACE("acpi_pci_link_check_possible");
117
118 switch (resource->id) {
119 case ACPI_RSTYPE_START_DPF:
120 return_ACPI_STATUS(AE_OK);
121 case ACPI_RSTYPE_IRQ:
122 {
123 struct acpi_resource_irq *p = &resource->data.irq;
124 if (!p || !p->number_of_interrupts) {
125 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Blank IRQ resource\n"));
126 return_ACPI_STATUS(AE_OK);
127 }
128 for (i = 0; (i<p->number_of_interrupts && i<ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
129 if (!p->interrupts[i]) {
130 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ %d\n", p->interrupts[i]));
131 continue;
132 }
133 link->irq.possible[i] = p->interrupts[i];
134 link->irq.possible_count++;
135 }
136 link->irq.edge_level = p->edge_level;
137 link->irq.active_high_low = p->active_high_low;
138 link->irq.resource_type = ACPI_RSTYPE_IRQ;
139 break;
140 }
141 case ACPI_RSTYPE_EXT_IRQ:
142 {
143 struct acpi_resource_ext_irq *p = &resource->data.extended_irq;
144 if (!p || !p->number_of_interrupts) {
145 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
146 "Blank EXT IRQ resource\n"));
147 return_ACPI_STATUS(AE_OK);
148 }
149 for (i = 0; (i<p->number_of_interrupts && i<ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
150 if (!p->interrupts[i]) {
151 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ %d\n", p->interrupts[i]));
152 continue;
153 }
154 link->irq.possible[i] = p->interrupts[i];
155 link->irq.possible_count++;
156 }
157 link->irq.edge_level = p->edge_level;
158 link->irq.active_high_low = p->active_high_low;
159 link->irq.resource_type = ACPI_RSTYPE_EXT_IRQ;
160 break;
161 }
162 default:
163 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
164 "Resource is not an IRQ entry\n"));
165 return_ACPI_STATUS(AE_OK);
166 }
167
168 return_ACPI_STATUS(AE_CTRL_TERMINATE);
169}
170
171
172static int
173acpi_pci_link_get_possible (
174 struct acpi_pci_link *link)
175{
176 acpi_status status;
177
178 ACPI_FUNCTION_TRACE("acpi_pci_link_get_possible");
179
180 if (!link)
181 return_VALUE(-EINVAL);
182
183 status = acpi_walk_resources(link->handle, METHOD_NAME__PRS,
184 acpi_pci_link_check_possible, link);
185 if (ACPI_FAILURE(status)) {
186 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRS\n"));
187 return_VALUE(-ENODEV);
188 }
189
190 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
191 "Found %d possible IRQs\n", link->irq.possible_count));
192
193 return_VALUE(0);
194}
195
196
197static acpi_status
198acpi_pci_link_check_current (
199 struct acpi_resource *resource,
200 void *context)
201{
202 int *irq = (int *) context;
203
204 ACPI_FUNCTION_TRACE("acpi_pci_link_check_current");
205
206 switch (resource->id) {
207 case ACPI_RSTYPE_IRQ:
208 {
209 struct acpi_resource_irq *p = &resource->data.irq;
210 if (!p || !p->number_of_interrupts) {
211 /*
212 * IRQ descriptors may have no IRQ# bits set,
213 * particularly those those w/ _STA disabled
214 */
215 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
216 "Blank IRQ resource\n"));
217 return_ACPI_STATUS(AE_OK);
218 }
219 *irq = p->interrupts[0];
220 break;
221 }
222 case ACPI_RSTYPE_EXT_IRQ:
223 {
224 struct acpi_resource_ext_irq *p = &resource->data.extended_irq;
225 if (!p || !p->number_of_interrupts) {
226 /*
227 * extended IRQ descriptors must
228 * return at least 1 IRQ
229 */
230 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
231 "Blank EXT IRQ resource\n"));
232 return_ACPI_STATUS(AE_OK);
233 }
234 *irq = p->interrupts[0];
235 break;
236 }
237 default:
238 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
239 "Resource isn't an IRQ\n"));
240 return_ACPI_STATUS(AE_OK);
241 }
242 return_ACPI_STATUS(AE_CTRL_TERMINATE);
243}
244
245/*
246 * Run _CRS and set link->irq.active
247 *
248 * return value:
249 * 0 - success
250 * !0 - failure
251 */
252static int
253acpi_pci_link_get_current (
254 struct acpi_pci_link *link)
255{
256 int result = 0;
257 acpi_status status = AE_OK;
258 int irq = 0;
259
260 ACPI_FUNCTION_TRACE("acpi_pci_link_get_current");
261
262 if (!link || !link->handle)
263 return_VALUE(-EINVAL);
264
265 link->irq.active = 0;
266
267 /* in practice, status disabled is meaningless, ignore it */
268 if (acpi_strict) {
269 /* Query _STA, set link->device->status */
270 result = acpi_bus_get_status(link->device);
271 if (result) {
272 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to read status\n"));
273 goto end;
274 }
275
276 if (!link->device->status.enabled) {
277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
278 return_VALUE(0);
279 }
280 }
281
282 /*
283 * Query and parse _CRS to get the current IRQ assignment.
284 */
285
286 status = acpi_walk_resources(link->handle, METHOD_NAME__CRS,
287 acpi_pci_link_check_current, &irq);
288 if (ACPI_FAILURE(status)) {
289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _CRS\n"));
290 result = -ENODEV;
291 goto end;
292 }
293
294 if (acpi_strict && !irq) {
295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_CRS returned 0\n"));
296 result = -ENODEV;
297 }
298
299 link->irq.active = irq;
300
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
302
303end:
304 return_VALUE(result);
305}
306
307static int
308acpi_pci_link_set (
309 struct acpi_pci_link *link,
310 int irq)
311{
312 int result = 0;
313 acpi_status status = AE_OK;
314 struct {
315 struct acpi_resource res;
316 struct acpi_resource end;
317 } *resource;
318 struct acpi_buffer buffer = {0, NULL};
319
320 ACPI_FUNCTION_TRACE("acpi_pci_link_set");
321
322 if (!link || !irq)
323 return_VALUE(-EINVAL);
324
325 resource = kmalloc( sizeof(*resource)+1, GFP_KERNEL);
326 if(!resource)
327 return_VALUE(-ENOMEM);
328
329 memset(resource, 0, sizeof(*resource)+1);
330 buffer.length = sizeof(*resource) +1;
331 buffer.pointer = resource;
332
333 switch(link->irq.resource_type) {
334 case ACPI_RSTYPE_IRQ:
335 resource->res.id = ACPI_RSTYPE_IRQ;
336 resource->res.length = sizeof(struct acpi_resource);
337 resource->res.data.irq.edge_level = link->irq.edge_level;
338 resource->res.data.irq.active_high_low = link->irq.active_high_low;
339 if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
340 resource->res.data.irq.shared_exclusive = ACPI_EXCLUSIVE;
341 else
342 resource->res.data.irq.shared_exclusive = ACPI_SHARED;
343 resource->res.data.irq.number_of_interrupts = 1;
344 resource->res.data.irq.interrupts[0] = irq;
345 break;
346
347 case ACPI_RSTYPE_EXT_IRQ:
348 resource->res.id = ACPI_RSTYPE_EXT_IRQ;
349 resource->res.length = sizeof(struct acpi_resource);
350 resource->res.data.extended_irq.producer_consumer = ACPI_CONSUMER;
351 resource->res.data.extended_irq.edge_level = link->irq.edge_level;
352 resource->res.data.extended_irq.active_high_low = link->irq.active_high_low;
353 if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
354 resource->res.data.irq.shared_exclusive = ACPI_EXCLUSIVE;
355 else
356 resource->res.data.irq.shared_exclusive = ACPI_SHARED;
357 resource->res.data.extended_irq.number_of_interrupts = 1;
358 resource->res.data.extended_irq.interrupts[0] = irq;
359 /* ignore resource_source, it's optional */
360 break;
361 default:
362 printk("ACPI BUG: resource_type %d\n", link->irq.resource_type);
363 result = -EINVAL;
364 goto end;
365
366 }
367 resource->end.id = ACPI_RSTYPE_END_TAG;
368
369 /* Attempt to set the resource */
370 status = acpi_set_current_resources(link->handle, &buffer);
371
372 /* check for total failure */
373 if (ACPI_FAILURE(status)) {
374 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SRS\n"));
375 result = -ENODEV;
376 goto end;
377 }
378
379 /* Query _STA, set device->status */
380 result = acpi_bus_get_status(link->device);
381 if (result) {
382 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to read status\n"));
383 goto end;
384 }
385 if (!link->device->status.enabled) {
386 printk(KERN_WARNING PREFIX
387 "%s [%s] disabled and referenced, BIOS bug.\n",
388 acpi_device_name(link->device),
389 acpi_device_bid(link->device));
390 }
391
392 /* Query _CRS, set link->irq.active */
393 result = acpi_pci_link_get_current(link);
394 if (result) {
395 goto end;
396 }
397
398 /*
399 * Is current setting not what we set?
400 * set link->irq.active
401 */
402 if (link->irq.active != irq) {
403 /*
404 * policy: when _CRS doesn't return what we just _SRS
405 * assume _SRS worked and override _CRS value.
406 */
407 printk(KERN_WARNING PREFIX
408 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
409 acpi_device_name(link->device),
410 acpi_device_bid(link->device),
411 link->irq.active, irq);
412 link->irq.active = irq;
413 }
414
415 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
416
417end:
418 kfree(resource);
419 return_VALUE(result);
420}
421
422
423/* --------------------------------------------------------------------------
424 PCI Link IRQ Management
425 -------------------------------------------------------------------------- */
426
427/*
428 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
429 * Link Devices to move the PIRQs around to minimize sharing.
430 *
431 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
432 * that the BIOS has already set to active. This is necessary because
433 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
434 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
435 * even if acpi_irq_nobalance is set.
436 *
437 * A tables of penalties avoids directing PCI interrupts to well known
438 * ISA IRQs. Boot params are available to over-ride the default table:
439 *
440 * List interrupts that are free for PCI use.
441 * acpi_irq_pci=n[,m]
442 *
443 * List interrupts that should not be used for PCI:
444 * acpi_irq_isa=n[,m]
445 *
446 * Note that PCI IRQ routers have a list of possible IRQs,
447 * which may not include the IRQs this table says are available.
448 *
449 * Since this heuristic can't tell the difference between a link
450 * that no device will attach to, vs. a link which may be shared
451 * by multiple active devices -- it is not optimal.
452 *
453 * If interrupt performance is that important, get an IO-APIC system
454 * with a pin dedicated to each device. Or for that matter, an MSI
455 * enabled system.
456 */
457
458#define ACPI_MAX_IRQS 256
459#define ACPI_MAX_ISA_IRQ 16
460
461#define PIRQ_PENALTY_PCI_AVAILABLE (0)
462#define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
463#define PIRQ_PENALTY_PCI_USING (16*16*16)
464#define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
465#define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
466#define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
467
468static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
469 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
470 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
471 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
472 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
473 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
474 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
475 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
476 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
477 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
478 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9 PCI, often acpi */
479 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
480 PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
481 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
482 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
483 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
484 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
485 /* >IRQ15 */
486};
487
488int __init
489acpi_irq_penalty_init(void)
490{
491 struct list_head *node = NULL;
492 struct acpi_pci_link *link = NULL;
493 int i = 0;
494
495 ACPI_FUNCTION_TRACE("acpi_irq_penalty_init");
496
497 /*
498 * Update penalties to facilitate IRQ balancing.
499 */
500 list_for_each(node, &acpi_link.entries) {
501
502 link = list_entry(node, struct acpi_pci_link, node);
503 if (!link) {
504 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
505 continue;
506 }
507
508 /*
509 * reflect the possible and active irqs in the penalty table --
510 * useful for breaking ties.
511 */
512 if (link->irq.possible_count) {
513 int penalty = PIRQ_PENALTY_PCI_POSSIBLE / link->irq.possible_count;
514
515 for (i = 0; i < link->irq.possible_count; i++) {
516 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
517 acpi_irq_penalty[link->irq.possible[i]] += penalty;
518 }
519
520 } else if (link->irq.active) {
521 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_POSSIBLE;
522 }
523 }
524 /* Add a penalty for the SCI */
525 acpi_irq_penalty[acpi_fadt.sci_int] += PIRQ_PENALTY_PCI_USING;
526
527 return_VALUE(0);
528}
529
530static int acpi_irq_balance; /* 0: static, 1: balance */
531
532static int acpi_pci_link_allocate(
533 struct acpi_pci_link *link)
534{
535 int irq;
536 int i;
537
538 ACPI_FUNCTION_TRACE("acpi_pci_link_allocate");
539
David Shaohua Li87bec662005-07-27 23:02:00 -0400540 if (link->irq.initialized) {
541 if (link->refcnt == 0)
542 /* This means the link is disabled but initialized */
543 acpi_pci_link_set(link, link->irq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return_VALUE(0);
David Shaohua Li87bec662005-07-27 23:02:00 -0400545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /*
548 * search for active IRQ in list of possible IRQs.
549 */
550 for (i = 0; i < link->irq.possible_count; ++i) {
551 if (link->irq.active == link->irq.possible[i])
552 break;
553 }
554 /*
555 * forget active IRQ that is not in possible list
556 */
557 if (i == link->irq.possible_count) {
558 if (acpi_strict)
559 printk(KERN_WARNING PREFIX "_CRS %d not found"
560 " in _PRS\n", link->irq.active);
561 link->irq.active = 0;
562 }
563
564 /*
565 * if active found, use it; else pick entry from end of possible list.
566 */
567 if (link->irq.active) {
568 irq = link->irq.active;
569 } else {
570 irq = link->irq.possible[link->irq.possible_count - 1];
571 }
572
573 if (acpi_irq_balance || !link->irq.active) {
574 /*
575 * Select the best IRQ. This is done in reverse to promote
576 * the use of IRQs 9, 10, 11, and >15.
577 */
578 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
579 if (acpi_irq_penalty[irq] > acpi_irq_penalty[link->irq.possible[i]])
580 irq = link->irq.possible[i];
581 }
582 }
583
584 /* Attempt to enable the link device at this IRQ. */
585 if (acpi_pci_link_set(link, irq)) {
586 printk(PREFIX "Unable to set IRQ for %s [%s] (likely buggy ACPI BIOS).\n"
587 "Try pci=noacpi or acpi=off\n",
588 acpi_device_name(link->device),
589 acpi_device_bid(link->device));
590 return_VALUE(-ENODEV);
591 } else {
592 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
593 printk(PREFIX "%s [%s] enabled at IRQ %d\n",
594 acpi_device_name(link->device),
595 acpi_device_bid(link->device), link->irq.active);
596 }
597
598 link->irq.initialized = 1;
599
600 return_VALUE(0);
601}
602
603/*
David Shaohua Li87bec662005-07-27 23:02:00 -0400604 * acpi_pci_link_allocate_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * success: return IRQ >= 0
606 * failure: return -1
607 */
608
609int
David Shaohua Li87bec662005-07-27 23:02:00 -0400610acpi_pci_link_allocate_irq (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 acpi_handle handle,
612 int index,
613 int *edge_level,
614 int *active_high_low,
615 char **name)
616{
617 int result = 0;
618 struct acpi_device *device = NULL;
619 struct acpi_pci_link *link = NULL;
620
David Shaohua Li87bec662005-07-27 23:02:00 -0400621 ACPI_FUNCTION_TRACE("acpi_pci_link_allocate_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 result = acpi_bus_get_device(handle, &device);
624 if (result) {
625 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
626 return_VALUE(-1);
627 }
628
629 link = (struct acpi_pci_link *) acpi_driver_data(device);
630 if (!link) {
631 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
632 return_VALUE(-1);
633 }
634
635 /* TBD: Support multiple index (IRQ) entries per Link Device */
636 if (index) {
637 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid index %d\n", index));
638 return_VALUE(-1);
639 }
640
David Shaohua Li87bec662005-07-27 23:02:00 -0400641 down(&acpi_link_lock);
642 if (acpi_pci_link_allocate(link)) {
643 up(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return_VALUE(-1);
David Shaohua Li87bec662005-07-27 23:02:00 -0400645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (!link->irq.active) {
David Shaohua Li87bec662005-07-27 23:02:00 -0400648 up(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link active IRQ is 0!\n"));
650 return_VALUE(-1);
651 }
David Shaohua Li87bec662005-07-27 23:02:00 -0400652 link->refcnt ++;
653 up(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (edge_level) *edge_level = link->irq.edge_level;
656 if (active_high_low) *active_high_low = link->irq.active_high_low;
657 if (name) *name = acpi_device_bid(link->device);
David Shaohua Li87bec662005-07-27 23:02:00 -0400658 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
659 "Link %s is referenced\n", acpi_device_bid(link->device)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return_VALUE(link->irq.active);
661}
662
David Shaohua Li87bec662005-07-27 23:02:00 -0400663/*
664 * We don't change link's irq information here. After it is reenabled, we
665 * continue use the info
666 */
667int
668acpi_pci_link_free_irq(acpi_handle handle)
669{
670 struct acpi_device *device = NULL;
671 struct acpi_pci_link *link = NULL;
672 acpi_status result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David Shaohua Li87bec662005-07-27 23:02:00 -0400674 ACPI_FUNCTION_TRACE("acpi_pci_link_free_irq");
675
676 result = acpi_bus_get_device(handle, &device);
677 if (result) {
678 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
679 return_VALUE(-1);
680 }
681
682 link = (struct acpi_pci_link *) acpi_driver_data(device);
683 if (!link) {
684 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
685 return_VALUE(-1);
686 }
687
688 down(&acpi_link_lock);
689 if (!link->irq.initialized) {
690 up(&acpi_link_lock);
691 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link isn't initialized\n"));
692 return_VALUE(-1);
693 }
694
695 link->refcnt --;
696 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
697 "Link %s is dereferenced\n", acpi_device_bid(link->device)));
698
699 if (link->refcnt == 0) {
700 acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
701 }
702 up(&acpi_link_lock);
703 return_VALUE(link->irq.active);
704}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/* --------------------------------------------------------------------------
706 Driver Interface
707 -------------------------------------------------------------------------- */
708
709static int
710acpi_pci_link_add (
711 struct acpi_device *device)
712{
713 int result = 0;
714 struct acpi_pci_link *link = NULL;
715 int i = 0;
716 int found = 0;
717
718 ACPI_FUNCTION_TRACE("acpi_pci_link_add");
719
720 if (!device)
721 return_VALUE(-EINVAL);
722
723 link = kmalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
724 if (!link)
725 return_VALUE(-ENOMEM);
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
David Shaohua Li87bec662005-07-27 23:02:00 -0400734 down(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 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),
743 acpi_device_bid(device));
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;
748 }
749 else
750 printk(" %d", link->irq.possible[i]);
751 }
752
753 printk(")");
754
755 if (!found)
756 printk(" *%d", link->irq.active);
757
758 if(!link->device->status.enabled)
759 printk(", disabled.");
760
761 printk("\n");
762
763 /* TBD: Acquire/release lock */
764 list_add_tail(&link->node, &acpi_link.entries);
765 acpi_link.count++;
766
767end:
768 /* disable all links -- to be activated on use */
769 acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
David Shaohua Li87bec662005-07-27 23:02:00 -0400770 up(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (result)
773 kfree(link);
774
775 return_VALUE(result);
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int
David Shaohua Li362b06b2005-03-18 16:30:29 -0500779irqrouter_suspend(
780 struct sys_device *dev,
781 u32 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct list_head *node = NULL;
784 struct acpi_pci_link *link = NULL;
David Shaohua Li87bec662005-07-27 23:02:00 -0400785 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
David Shaohua Li362b06b2005-03-18 16:30:29 -0500787 ACPI_FUNCTION_TRACE("irqrouter_suspend");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 list_for_each(node, &acpi_link.entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 link = list_entry(node, struct acpi_pci_link, node);
791 if (!link) {
David Shaohua Li87bec662005-07-27 23:02:00 -0400792 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
793 "Invalid link context\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 continue;
795 }
David Shaohua Li87bec662005-07-27 23:02:00 -0400796 if (link->irq.initialized && link->refcnt != 0
797 /* We ignore legacy IDE device irq */
798 && link->irq.active != 14 && link->irq.active !=15) {
799 printk(KERN_WARNING PREFIX
800 "%d drivers with interrupt %d neglected to call"
801 " pci_disable_device at .suspend\n",
802 link->refcnt,
803 link->irq.active);
804 printk(KERN_WARNING PREFIX
805 "Fix the driver, or rmmod before suspend\n");
806 link->refcnt = 0;
807 ret = -EINVAL;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
David Shaohua Li87bec662005-07-27 23:02:00 -0400810 return_VALUE(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813
814static int
815acpi_pci_link_remove (
816 struct acpi_device *device,
817 int type)
818{
819 struct acpi_pci_link *link = NULL;
820
821 ACPI_FUNCTION_TRACE("acpi_pci_link_remove");
822
823 if (!device || !acpi_driver_data(device))
824 return_VALUE(-EINVAL);
825
826 link = (struct acpi_pci_link *) acpi_driver_data(device);
827
David Shaohua Li87bec662005-07-27 23:02:00 -0400828 down(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 list_del(&link->node);
David Shaohua Li87bec662005-07-27 23:02:00 -0400830 up(&acpi_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 kfree(link);
833
834 return_VALUE(0);
835}
836
837/*
838 * modify acpi_irq_penalty[] from cmdline
839 */
840static int __init acpi_irq_penalty_update(char *str, int used)
841{
842 int i;
843
844 for (i = 0; i < 16; i++) {
845 int retval;
846 int irq;
847
848 retval = get_option(&str,&irq);
849
850 if (!retval)
851 break; /* no number found */
852
853 if (irq < 0)
854 continue;
855
856 if (irq >= ACPI_MAX_IRQS)
857 continue;
858
859 if (used)
860 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
861 else
862 acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
863
864 if (retval != 2) /* no next number */
865 break;
866 }
867 return 1;
868}
869
870/*
871 * We'd like PNP to call this routine for the
872 * single ISA_USED value for each legacy device.
873 * But instead it calls us with each POSSIBLE setting.
874 * There is no ISA_POSSIBLE weight, so we simply use
875 * the (small) PCI_USING penalty.
876 */
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500877void acpi_penalize_isa_irq(int irq, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
David Shaohua Lic9c3e452005-04-01 00:07:31 -0500879 if (active)
880 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
881 else
882 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/*
886 * Over-ride default table to reserve additional IRQs for use by ISA
887 * e.g. acpi_irq_isa=5
888 * Useful for telling ACPI how not to interfere with your ISA sound card.
889 */
890static int __init acpi_irq_isa(char *str)
891{
892 return acpi_irq_penalty_update(str, 1);
893}
894__setup("acpi_irq_isa=", acpi_irq_isa);
895
896/*
897 * Over-ride default table to free additional IRQs for use by PCI
898 * e.g. acpi_irq_pci=7,15
899 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
900 */
901static int __init acpi_irq_pci(char *str)
902{
903 return acpi_irq_penalty_update(str, 0);
904}
905__setup("acpi_irq_pci=", acpi_irq_pci);
906
907static int __init acpi_irq_nobalance_set(char *str)
908{
909 acpi_irq_balance = 0;
910 return 1;
911}
912__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
913
914int __init acpi_irq_balance_set(char *str)
915{
916 acpi_irq_balance = 1;
917 return 1;
918}
919__setup("acpi_irq_balance", acpi_irq_balance_set);
920
921
David Shaohua Li87bec662005-07-27 23:02:00 -0400922/* FIXME: we will remove this interface after all drivers call pci_disable_device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static struct sysdev_class irqrouter_sysdev_class = {
924 set_kset_name("irqrouter"),
David Shaohua Li362b06b2005-03-18 16:30:29 -0500925 .suspend = irqrouter_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926};
927
928
929static struct sys_device device_irqrouter = {
930 .id = 0,
931 .cls = &irqrouter_sysdev_class,
932};
933
934
935static int __init irqrouter_init_sysfs(void)
936{
937 int error;
938
939 ACPI_FUNCTION_TRACE("irqrouter_init_sysfs");
940
941 if (acpi_disabled || acpi_noirq)
942 return_VALUE(0);
943
944 error = sysdev_class_register(&irqrouter_sysdev_class);
945 if (!error)
946 error = sysdev_register(&device_irqrouter);
947
948 return_VALUE(error);
949}
950
951device_initcall(irqrouter_init_sysfs);
952
953
954static int __init acpi_pci_link_init (void)
955{
956 ACPI_FUNCTION_TRACE("acpi_pci_link_init");
957
958 if (acpi_noirq)
959 return_VALUE(0);
960
961 acpi_link.count = 0;
962 INIT_LIST_HEAD(&acpi_link.entries);
963
964 if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
965 return_VALUE(-ENODEV);
966
967 return_VALUE(0);
968}
969
970subsys_initcall(acpi_pci_link_init);