gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / acpi / pci_link.c
blob00a6da2121be30bd08de4bcbe2f9a6e9a12c4488
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2002 Dominik Brodowski <devel@brodo.de>
9 * TBD:
10 * 1. Support more than one IRQ resource entry per link device (index).
11 * 2. Implement start/stop mechanism and use ACPI Bus Driver facilities
12 * for IRQ management (e.g. start()->_SRS).
15 #include <linux/syscore_ops.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21 #include <linux/pm.h>
22 #include <linux/pci.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
26 #include <linux/irq.h>
28 #include "internal.h"
30 #define _COMPONENT ACPI_PCI_COMPONENT
31 ACPI_MODULE_NAME("pci_link");
32 #define ACPI_PCI_LINK_CLASS "pci_irq_routing"
33 #define ACPI_PCI_LINK_DEVICE_NAME "PCI Interrupt Link"
34 #define ACPI_PCI_LINK_FILE_INFO "info"
35 #define ACPI_PCI_LINK_FILE_STATUS "state"
36 #define ACPI_PCI_LINK_MAX_POSSIBLE 16
38 static int acpi_pci_link_add(struct acpi_device *device,
39 const struct acpi_device_id *not_used);
40 static void acpi_pci_link_remove(struct acpi_device *device);
42 static const struct acpi_device_id link_device_ids[] = {
43 {"PNP0C0F", 0},
44 {"", 0},
47 static struct acpi_scan_handler pci_link_handler = {
48 .ids = link_device_ids,
49 .attach = acpi_pci_link_add,
50 .detach = acpi_pci_link_remove,
54 * If a link is initialized, we never change its active and initialized
55 * later even the link is disable. Instead, we just repick the active irq
57 struct acpi_pci_link_irq {
58 u32 active; /* Current IRQ */
59 u8 triggering; /* All IRQs */
60 u8 polarity; /* All IRQs */
61 u8 resource_type;
62 u8 possible_count;
63 u32 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
64 u8 initialized:1;
65 u8 reserved:7;
68 struct acpi_pci_link {
69 struct list_head list;
70 struct acpi_device *device;
71 struct acpi_pci_link_irq irq;
72 int refcnt;
75 static LIST_HEAD(acpi_link_list);
76 static DEFINE_MUTEX(acpi_link_lock);
77 static int sci_irq = -1, sci_penalty;
79 /* --------------------------------------------------------------------------
80 PCI Link Device Management
81 -------------------------------------------------------------------------- */
84 * set context (link) possible list from resource list
86 static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
87 void *context)
89 struct acpi_pci_link *link = context;
90 u32 i;
92 switch (resource->type) {
93 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
94 case ACPI_RESOURCE_TYPE_END_TAG:
95 return AE_OK;
96 case ACPI_RESOURCE_TYPE_IRQ:
98 struct acpi_resource_irq *p = &resource->data.irq;
99 if (!p || !p->interrupt_count) {
100 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
101 "Blank _PRS IRQ resource\n"));
102 return AE_OK;
104 for (i = 0;
105 (i < p->interrupt_count
106 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
107 if (!p->interrupts[i]) {
108 printk(KERN_WARNING PREFIX
109 "Invalid _PRS IRQ %d\n",
110 p->interrupts[i]);
111 continue;
113 link->irq.possible[i] = p->interrupts[i];
114 link->irq.possible_count++;
116 link->irq.triggering = p->triggering;
117 link->irq.polarity = p->polarity;
118 link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
119 break;
121 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
123 struct acpi_resource_extended_irq *p =
124 &resource->data.extended_irq;
125 if (!p || !p->interrupt_count) {
126 printk(KERN_WARNING PREFIX
127 "Blank _PRS EXT IRQ resource\n");
128 return AE_OK;
130 for (i = 0;
131 (i < p->interrupt_count
132 && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
133 if (!p->interrupts[i]) {
134 printk(KERN_WARNING PREFIX
135 "Invalid _PRS IRQ %d\n",
136 p->interrupts[i]);
137 continue;
139 link->irq.possible[i] = p->interrupts[i];
140 link->irq.possible_count++;
142 link->irq.triggering = p->triggering;
143 link->irq.polarity = p->polarity;
144 link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
145 break;
147 default:
148 printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
149 resource->type);
150 return AE_OK;
153 return AE_CTRL_TERMINATE;
156 static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
158 acpi_status status;
160 status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
161 acpi_pci_link_check_possible, link);
162 if (ACPI_FAILURE(status)) {
163 acpi_handle_debug(link->device->handle, "_PRS not present or invalid");
164 return 0;
167 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
168 "Found %d possible IRQs\n",
169 link->irq.possible_count));
171 return 0;
174 static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
175 void *context)
177 int *irq = context;
179 switch (resource->type) {
180 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
181 case ACPI_RESOURCE_TYPE_END_TAG:
182 return AE_OK;
183 case ACPI_RESOURCE_TYPE_IRQ:
185 struct acpi_resource_irq *p = &resource->data.irq;
186 if (!p || !p->interrupt_count) {
188 * IRQ descriptors may have no IRQ# bits set,
189 * particularly those those w/ _STA disabled
191 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
192 "Blank _CRS IRQ resource\n"));
193 return AE_OK;
195 *irq = p->interrupts[0];
196 break;
198 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
200 struct acpi_resource_extended_irq *p =
201 &resource->data.extended_irq;
202 if (!p || !p->interrupt_count) {
204 * extended IRQ descriptors must
205 * return at least 1 IRQ
207 printk(KERN_WARNING PREFIX
208 "Blank _CRS EXT IRQ resource\n");
209 return AE_OK;
211 *irq = p->interrupts[0];
212 break;
214 break;
215 default:
216 printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
217 resource->type);
218 return AE_OK;
221 return AE_CTRL_TERMINATE;
225 * Run _CRS and set link->irq.active
227 * return value:
228 * 0 - success
229 * !0 - failure
231 static int acpi_pci_link_get_current(struct acpi_pci_link *link)
233 int result = 0;
234 acpi_status status;
235 int irq = 0;
237 link->irq.active = 0;
239 /* in practice, status disabled is meaningless, ignore it */
240 if (acpi_strict) {
241 /* Query _STA, set link->device->status */
242 result = acpi_bus_get_status(link->device);
243 if (result) {
244 printk(KERN_ERR PREFIX "Unable to read status\n");
245 goto end;
248 if (!link->device->status.enabled) {
249 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
250 return 0;
255 * Query and parse _CRS to get the current IRQ assignment.
258 status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
259 acpi_pci_link_check_current, &irq);
260 if (ACPI_FAILURE(status)) {
261 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
262 result = -ENODEV;
263 goto end;
266 if (acpi_strict && !irq) {
267 printk(KERN_ERR PREFIX "_CRS returned 0\n");
268 result = -ENODEV;
271 link->irq.active = irq;
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
275 end:
276 return result;
279 static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
281 int result;
282 acpi_status status;
283 struct {
284 struct acpi_resource res;
285 struct acpi_resource end;
286 } *resource;
287 struct acpi_buffer buffer = { 0, NULL };
289 if (!irq)
290 return -EINVAL;
292 resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
293 if (!resource)
294 return -ENOMEM;
296 buffer.length = sizeof(*resource) + 1;
297 buffer.pointer = resource;
299 switch (link->irq.resource_type) {
300 case ACPI_RESOURCE_TYPE_IRQ:
301 resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
302 resource->res.length = sizeof(struct acpi_resource);
303 resource->res.data.irq.triggering = link->irq.triggering;
304 resource->res.data.irq.polarity =
305 link->irq.polarity;
306 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
307 resource->res.data.irq.shareable =
308 ACPI_EXCLUSIVE;
309 else
310 resource->res.data.irq.shareable = ACPI_SHARED;
311 resource->res.data.irq.interrupt_count = 1;
312 resource->res.data.irq.interrupts[0] = irq;
313 break;
315 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
316 resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
317 resource->res.length = sizeof(struct acpi_resource);
318 resource->res.data.extended_irq.producer_consumer =
319 ACPI_CONSUMER;
320 resource->res.data.extended_irq.triggering =
321 link->irq.triggering;
322 resource->res.data.extended_irq.polarity =
323 link->irq.polarity;
324 if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
325 resource->res.data.irq.shareable =
326 ACPI_EXCLUSIVE;
327 else
328 resource->res.data.irq.shareable = ACPI_SHARED;
329 resource->res.data.extended_irq.interrupt_count = 1;
330 resource->res.data.extended_irq.interrupts[0] = irq;
331 /* ignore resource_source, it's optional */
332 break;
333 default:
334 printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
335 result = -EINVAL;
336 goto end;
339 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
340 resource->end.length = sizeof(struct acpi_resource);
342 /* Attempt to set the resource */
343 status = acpi_set_current_resources(link->device->handle, &buffer);
345 /* check for total failure */
346 if (ACPI_FAILURE(status)) {
347 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
348 result = -ENODEV;
349 goto end;
352 /* Query _STA, set device->status */
353 result = acpi_bus_get_status(link->device);
354 if (result) {
355 printk(KERN_ERR PREFIX "Unable to read status\n");
356 goto end;
358 if (!link->device->status.enabled) {
359 printk(KERN_WARNING PREFIX
360 "%s [%s] disabled and referenced, BIOS bug\n",
361 acpi_device_name(link->device),
362 acpi_device_bid(link->device));
365 /* Query _CRS, set link->irq.active */
366 result = acpi_pci_link_get_current(link);
367 if (result) {
368 goto end;
372 * Is current setting not what we set?
373 * set link->irq.active
375 if (link->irq.active != irq) {
377 * policy: when _CRS doesn't return what we just _SRS
378 * assume _SRS worked and override _CRS value.
380 printk(KERN_WARNING PREFIX
381 "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
382 acpi_device_name(link->device),
383 acpi_device_bid(link->device), link->irq.active, irq);
384 link->irq.active = irq;
387 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
389 end:
390 kfree(resource);
391 return result;
394 /* --------------------------------------------------------------------------
395 PCI Link IRQ Management
396 -------------------------------------------------------------------------- */
399 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
400 * Link Devices to move the PIRQs around to minimize sharing.
402 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
403 * that the BIOS has already set to active. This is necessary because
404 * ACPI has no automatic means of knowing what ISA IRQs are used. Note that
405 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
406 * even if acpi_irq_nobalance is set.
408 * A tables of penalties avoids directing PCI interrupts to well known
409 * ISA IRQs. Boot params are available to over-ride the default table:
411 * List interrupts that are free for PCI use.
412 * acpi_irq_pci=n[,m]
414 * List interrupts that should not be used for PCI:
415 * acpi_irq_isa=n[,m]
417 * Note that PCI IRQ routers have a list of possible IRQs,
418 * which may not include the IRQs this table says are available.
420 * Since this heuristic can't tell the difference between a link
421 * that no device will attach to, vs. a link which may be shared
422 * by multiple active devices -- it is not optimal.
424 * If interrupt performance is that important, get an IO-APIC system
425 * with a pin dedicated to each device. Or for that matter, an MSI
426 * enabled system.
429 #define ACPI_MAX_ISA_IRQS 16
431 #define PIRQ_PENALTY_PCI_POSSIBLE (16*16)
432 #define PIRQ_PENALTY_PCI_USING (16*16*16)
433 #define PIRQ_PENALTY_ISA_TYPICAL (16*16*16*16)
434 #define PIRQ_PENALTY_ISA_USED (16*16*16*16*16)
435 #define PIRQ_PENALTY_ISA_ALWAYS (16*16*16*16*16*16)
437 static int acpi_isa_irq_penalty[ACPI_MAX_ISA_IRQS] = {
438 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ0 timer */
439 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ1 keyboard */
440 PIRQ_PENALTY_ISA_ALWAYS, /* IRQ2 cascade */
441 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ3 serial */
442 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ4 serial */
443 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ5 sometimes SoundBlaster */
444 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ6 */
445 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ7 parallel, spurious */
446 PIRQ_PENALTY_ISA_TYPICAL, /* IRQ8 rtc, sometimes */
447 0, /* IRQ9 PCI, often acpi */
448 0, /* IRQ10 PCI */
449 0, /* IRQ11 PCI */
450 PIRQ_PENALTY_ISA_USED, /* IRQ12 mouse */
451 PIRQ_PENALTY_ISA_USED, /* IRQ13 fpe, sometimes */
452 PIRQ_PENALTY_ISA_USED, /* IRQ14 ide0 */
453 PIRQ_PENALTY_ISA_USED, /* IRQ15 ide1 */
454 /* >IRQ15 */
457 static int acpi_irq_pci_sharing_penalty(int irq)
459 struct acpi_pci_link *link;
460 int penalty = 0;
461 int i;
463 list_for_each_entry(link, &acpi_link_list, list) {
465 * If a link is active, penalize its IRQ heavily
466 * so we try to choose a different IRQ.
468 if (link->irq.active && link->irq.active == irq)
469 penalty += PIRQ_PENALTY_PCI_USING;
472 * penalize the IRQs PCI might use, but not as severely.
474 for (i = 0; i < link->irq.possible_count; i++)
475 if (link->irq.possible[i] == irq)
476 penalty += PIRQ_PENALTY_PCI_POSSIBLE /
477 link->irq.possible_count;
480 return penalty;
483 static int acpi_irq_get_penalty(int irq)
485 int penalty = 0;
487 if (irq == sci_irq)
488 penalty += sci_penalty;
490 if (irq < ACPI_MAX_ISA_IRQS)
491 return penalty + acpi_isa_irq_penalty[irq];
493 return penalty + acpi_irq_pci_sharing_penalty(irq);
496 int __init acpi_irq_penalty_init(void)
498 struct acpi_pci_link *link;
499 int i;
502 * Update penalties to facilitate IRQ balancing.
504 list_for_each_entry(link, &acpi_link_list, list) {
507 * reflect the possible and active irqs in the penalty table --
508 * useful for breaking ties.
510 if (link->irq.possible_count) {
511 int penalty =
512 PIRQ_PENALTY_PCI_POSSIBLE /
513 link->irq.possible_count;
515 for (i = 0; i < link->irq.possible_count; i++) {
516 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQS)
517 acpi_isa_irq_penalty[link->irq.
518 possible[i]] +=
519 penalty;
522 } else if (link->irq.active &&
523 (link->irq.active < ACPI_MAX_ISA_IRQS)) {
524 acpi_isa_irq_penalty[link->irq.active] +=
525 PIRQ_PENALTY_PCI_POSSIBLE;
529 return 0;
532 static int acpi_irq_balance = -1; /* 0: static, 1: balance */
534 static int acpi_pci_link_allocate(struct acpi_pci_link *link)
536 int irq;
537 int i;
539 if (link->irq.initialized) {
540 if (link->refcnt == 0)
541 /* This means the link is disabled but initialized */
542 acpi_pci_link_set(link, link->irq.active);
543 return 0;
547 * search for active IRQ in list of possible IRQs.
549 for (i = 0; i < link->irq.possible_count; ++i) {
550 if (link->irq.active == link->irq.possible[i])
551 break;
554 * forget active IRQ that is not in possible list
556 if (i == link->irq.possible_count) {
557 if (acpi_strict)
558 printk(KERN_WARNING PREFIX "_CRS %d not found"
559 " in _PRS\n", link->irq.active);
560 link->irq.active = 0;
564 * if active found, use it; else pick entry from end of possible list.
566 if (link->irq.active)
567 irq = link->irq.active;
568 else
569 irq = link->irq.possible[link->irq.possible_count - 1];
571 if (acpi_irq_balance || !link->irq.active) {
573 * Select the best IRQ. This is done in reverse to promote
574 * the use of IRQs 9, 10, 11, and >15.
576 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
577 if (acpi_irq_get_penalty(irq) >
578 acpi_irq_get_penalty(link->irq.possible[i]))
579 irq = link->irq.possible[i];
582 if (acpi_irq_get_penalty(irq) >= PIRQ_PENALTY_ISA_ALWAYS) {
583 printk(KERN_ERR PREFIX "No IRQ available for %s [%s]. "
584 "Try pci=noacpi or acpi=off\n",
585 acpi_device_name(link->device),
586 acpi_device_bid(link->device));
587 return -ENODEV;
590 /* Attempt to enable the link device at this IRQ. */
591 if (acpi_pci_link_set(link, irq)) {
592 printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
593 "Try pci=noacpi or acpi=off\n",
594 acpi_device_name(link->device),
595 acpi_device_bid(link->device));
596 return -ENODEV;
597 } else {
598 if (link->irq.active < ACPI_MAX_ISA_IRQS)
599 acpi_isa_irq_penalty[link->irq.active] +=
600 PIRQ_PENALTY_PCI_USING;
602 pr_info("%s [%s] enabled at IRQ %d\n",
603 acpi_device_name(link->device),
604 acpi_device_bid(link->device), link->irq.active);
607 link->irq.initialized = 1;
608 return 0;
612 * acpi_pci_link_allocate_irq
613 * success: return IRQ >= 0
614 * failure: return -1
616 int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
617 int *polarity, char **name)
619 int result;
620 struct acpi_device *device;
621 struct acpi_pci_link *link;
623 result = acpi_bus_get_device(handle, &device);
624 if (result) {
625 printk(KERN_ERR PREFIX "Invalid link device\n");
626 return -1;
629 link = acpi_driver_data(device);
630 if (!link) {
631 printk(KERN_ERR PREFIX "Invalid link context\n");
632 return -1;
635 /* TBD: Support multiple index (IRQ) entries per Link Device */
636 if (index) {
637 printk(KERN_ERR PREFIX "Invalid index %d\n", index);
638 return -1;
641 mutex_lock(&acpi_link_lock);
642 if (acpi_pci_link_allocate(link)) {
643 mutex_unlock(&acpi_link_lock);
644 return -1;
647 if (!link->irq.active) {
648 mutex_unlock(&acpi_link_lock);
649 printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
650 return -1;
652 link->refcnt++;
653 mutex_unlock(&acpi_link_lock);
655 if (triggering)
656 *triggering = link->irq.triggering;
657 if (polarity)
658 *polarity = link->irq.polarity;
659 if (name)
660 *name = acpi_device_bid(link->device);
661 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
662 "Link %s is referenced\n",
663 acpi_device_bid(link->device)));
664 return link->irq.active;
668 * We don't change link's irq information here. After it is reenabled, we
669 * continue use the info
671 int acpi_pci_link_free_irq(acpi_handle handle)
673 struct acpi_device *device;
674 struct acpi_pci_link *link;
675 acpi_status result;
677 result = acpi_bus_get_device(handle, &device);
678 if (result) {
679 printk(KERN_ERR PREFIX "Invalid link device\n");
680 return -1;
683 link = acpi_driver_data(device);
684 if (!link) {
685 printk(KERN_ERR PREFIX "Invalid link context\n");
686 return -1;
689 mutex_lock(&acpi_link_lock);
690 if (!link->irq.initialized) {
691 mutex_unlock(&acpi_link_lock);
692 printk(KERN_ERR PREFIX "Link isn't initialized\n");
693 return -1;
695 #ifdef FUTURE_USE
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
705 link->refcnt--;
706 #endif
707 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
708 "Link %s is dereferenced\n",
709 acpi_device_bid(link->device)));
711 if (link->refcnt == 0)
712 acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
714 mutex_unlock(&acpi_link_lock);
715 return link->irq.active;
718 /* --------------------------------------------------------------------------
719 Driver Interface
720 -------------------------------------------------------------------------- */
722 static int acpi_pci_link_add(struct acpi_device *device,
723 const struct acpi_device_id *not_used)
725 int result;
726 struct acpi_pci_link *link;
727 int i;
728 int found = 0;
730 link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
731 if (!link)
732 return -ENOMEM;
734 link->device = device;
735 strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
736 strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
737 device->driver_data = link;
739 mutex_lock(&acpi_link_lock);
740 result = acpi_pci_link_get_possible(link);
741 if (result)
742 goto end;
744 /* query and set link->irq.active */
745 acpi_pci_link_get_current(link);
747 printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
748 acpi_device_bid(device));
749 for (i = 0; i < link->irq.possible_count; i++) {
750 if (link->irq.active == link->irq.possible[i]) {
751 printk(KERN_CONT " *%d", link->irq.possible[i]);
752 found = 1;
753 } else
754 printk(KERN_CONT " %d", link->irq.possible[i]);
757 printk(KERN_CONT ")");
759 if (!found)
760 printk(KERN_CONT " *%d", link->irq.active);
762 if (!link->device->status.enabled)
763 printk(KERN_CONT ", disabled.");
765 printk(KERN_CONT "\n");
767 list_add_tail(&link->list, &acpi_link_list);
769 end:
770 /* disable all links -- to be activated on use */
771 acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
772 mutex_unlock(&acpi_link_lock);
774 if (result)
775 kfree(link);
777 return result < 0 ? result : 1;
780 static int acpi_pci_link_resume(struct acpi_pci_link *link)
782 if (link->refcnt && link->irq.active && link->irq.initialized)
783 return (acpi_pci_link_set(link, link->irq.active));
785 return 0;
788 static void irqrouter_resume(void)
790 struct acpi_pci_link *link;
792 list_for_each_entry(link, &acpi_link_list, list) {
793 acpi_pci_link_resume(link);
797 static void acpi_pci_link_remove(struct acpi_device *device)
799 struct acpi_pci_link *link;
801 link = acpi_driver_data(device);
803 mutex_lock(&acpi_link_lock);
804 list_del(&link->list);
805 mutex_unlock(&acpi_link_lock);
807 kfree(link);
811 * modify acpi_isa_irq_penalty[] from cmdline
813 static int __init acpi_irq_penalty_update(char *str, int used)
815 int i;
817 for (i = 0; i < 16; i++) {
818 int retval;
819 int irq;
820 int new_penalty;
822 retval = get_option(&str, &irq);
824 if (!retval)
825 break; /* no number found */
827 /* see if this is a ISA IRQ */
828 if ((irq < 0) || (irq >= ACPI_MAX_ISA_IRQS))
829 continue;
831 if (used)
832 new_penalty = acpi_isa_irq_penalty[irq] +
833 PIRQ_PENALTY_ISA_USED;
834 else
835 new_penalty = 0;
837 acpi_isa_irq_penalty[irq] = new_penalty;
838 if (retval != 2) /* no next number */
839 break;
841 return 1;
845 * We'd like PNP to call this routine for the
846 * single ISA_USED value for each legacy device.
847 * But instead it calls us with each POSSIBLE setting.
848 * There is no ISA_POSSIBLE weight, so we simply use
849 * the (small) PCI_USING penalty.
851 void acpi_penalize_isa_irq(int irq, int active)
853 if ((irq >= 0) && (irq < ARRAY_SIZE(acpi_isa_irq_penalty)))
854 acpi_isa_irq_penalty[irq] +=
855 (active ? PIRQ_PENALTY_ISA_USED : PIRQ_PENALTY_PCI_USING);
858 bool acpi_isa_irq_available(int irq)
860 return irq >= 0 && (irq >= ARRAY_SIZE(acpi_isa_irq_penalty) ||
861 acpi_irq_get_penalty(irq) < PIRQ_PENALTY_ISA_ALWAYS);
864 void acpi_penalize_sci_irq(int irq, int trigger, int polarity)
866 sci_irq = irq;
868 if (trigger == ACPI_MADT_TRIGGER_LEVEL &&
869 polarity == ACPI_MADT_POLARITY_ACTIVE_LOW)
870 sci_penalty = PIRQ_PENALTY_PCI_USING;
871 else
872 sci_penalty = PIRQ_PENALTY_ISA_ALWAYS;
876 * Over-ride default table to reserve additional IRQs for use by ISA
877 * e.g. acpi_irq_isa=5
878 * Useful for telling ACPI how not to interfere with your ISA sound card.
880 static int __init acpi_irq_isa(char *str)
882 return acpi_irq_penalty_update(str, 1);
885 __setup("acpi_irq_isa=", acpi_irq_isa);
888 * Over-ride default table to free additional IRQs for use by PCI
889 * e.g. acpi_irq_pci=7,15
890 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
892 static int __init acpi_irq_pci(char *str)
894 return acpi_irq_penalty_update(str, 0);
897 __setup("acpi_irq_pci=", acpi_irq_pci);
899 static int __init acpi_irq_nobalance_set(char *str)
901 acpi_irq_balance = 0;
902 return 1;
905 __setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
907 static int __init acpi_irq_balance_set(char *str)
909 acpi_irq_balance = 1;
910 return 1;
913 __setup("acpi_irq_balance", acpi_irq_balance_set);
915 static struct syscore_ops irqrouter_syscore_ops = {
916 .resume = irqrouter_resume,
919 void __init acpi_pci_link_init(void)
921 if (acpi_noirq)
922 return;
924 if (acpi_irq_balance == -1) {
925 /* no command line switch: enable balancing in IOAPIC mode */
926 if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
927 acpi_irq_balance = 1;
928 else
929 acpi_irq_balance = 0;
931 register_syscore_ops(&irqrouter_syscore_ops);
932 acpi_scan_add_handler(&pci_link_handler);