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