pci_link.c 23.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
 *
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *  Copyright (C) 2002       Dominik Brodowski <devel@brodo.de>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * TBD: 
 *      1. Support more than one IRQ resource entry per link device (index).
 *	2. Implement start/stop mechanism and use ACPI Bus Driver facilities
 *	   for IRQ management (e.g. start()->_SRS).
 */

28
#include <linux/syscore_ops.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
#include <linux/pci.h>
I
Ingo Molnar 已提交
36
#include <linux/mutex.h>
37
#include <linux/slab.h>
38
#include <linux/acpi.h>
L
Linus Torvalds 已提交
39

40 41
#include "internal.h"

42
#define _COMPONENT			ACPI_PCI_COMPONENT
43
ACPI_MODULE_NAME("pci_link");
L
Linus Torvalds 已提交
44 45 46 47
#define ACPI_PCI_LINK_CLASS		"pci_irq_routing"
#define ACPI_PCI_LINK_DEVICE_NAME	"PCI Interrupt Link"
#define ACPI_PCI_LINK_FILE_INFO		"info"
#define ACPI_PCI_LINK_FILE_STATUS	"state"
48 49
#define ACPI_PCI_LINK_MAX_POSSIBLE	16

50 51 52
static int acpi_pci_link_add(struct acpi_device *device,
			     const struct acpi_device_id *not_used);
static void acpi_pci_link_remove(struct acpi_device *device);
L
Linus Torvalds 已提交
53

54
static const struct acpi_device_id link_device_ids[] = {
55 56 57 58
	{"PNP0C0F", 0},
	{"", 0},
};

59
static struct acpi_scan_handler pci_link_handler = {
60
	.ids = link_device_ids,
61 62
	.attach = acpi_pci_link_add,
	.detach = acpi_pci_link_remove,
L
Linus Torvalds 已提交
63 64
};

65 66 67 68
/*
 * If a link is initialized, we never change its active and initialized
 * later even the link is disable. Instead, we just repick the active irq
 */
L
Linus Torvalds 已提交
69
struct acpi_pci_link_irq {
70
	u32 active;		/* Current IRQ */
B
Bob Moore 已提交
71
	u8 triggering;		/* All IRQs */
72
	u8 polarity;		/* All IRQs */
L
Len Brown 已提交
73 74
	u8 resource_type;
	u8 possible_count;
75
	u32 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
L
Len Brown 已提交
76 77
	u8 initialized:1;
	u8 reserved:7;
L
Linus Torvalds 已提交
78 79 80
};

struct acpi_pci_link {
81
	struct list_head		list;
82 83 84
	struct acpi_device		*device;
	struct acpi_pci_link_irq	irq;
	int				refcnt;
L
Linus Torvalds 已提交
85 86
};

87
static LIST_HEAD(acpi_link_list);
A
Adrian Bunk 已提交
88
static DEFINE_MUTEX(acpi_link_lock);
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96

/* --------------------------------------------------------------------------
                            PCI Link Device Management
   -------------------------------------------------------------------------- */

/*
 * set context (link) possible list from resource list
 */
97 98
static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
						void *context)
L
Linus Torvalds 已提交
99
{
100
	struct acpi_pci_link *link = context;
101
	u32 i;
L
Linus Torvalds 已提交
102

103
	switch (resource->type) {
B
Bob Moore 已提交
104
	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
105
	case ACPI_RESOURCE_TYPE_END_TAG:
106
		return AE_OK;
B
Bob Moore 已提交
107
	case ACPI_RESOURCE_TYPE_IRQ:
L
Len Brown 已提交
108 109
		{
			struct acpi_resource_irq *p = &resource->data.irq;
B
Bob Moore 已提交
110
			if (!p || !p->interrupt_count) {
111 112
				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
						  "Blank _PRS IRQ resource\n"));
113
				return AE_OK;
L
Linus Torvalds 已提交
114
			}
L
Len Brown 已提交
115
			for (i = 0;
B
Bob Moore 已提交
116
			     (i < p->interrupt_count
L
Len Brown 已提交
117 118
			      && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
				if (!p->interrupts[i]) {
119 120 121
					printk(KERN_WARNING PREFIX
					       "Invalid _PRS IRQ %d\n",
					       p->interrupts[i]);
L
Len Brown 已提交
122 123 124 125 126
					continue;
				}
				link->irq.possible[i] = p->interrupts[i];
				link->irq.possible_count++;
			}
B
Bob Moore 已提交
127 128 129
			link->irq.triggering = p->triggering;
			link->irq.polarity = p->polarity;
			link->irq.resource_type = ACPI_RESOURCE_TYPE_IRQ;
L
Len Brown 已提交
130
			break;
L
Linus Torvalds 已提交
131
		}
B
Bob Moore 已提交
132
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
L
Len Brown 已提交
133
		{
B
Bob Moore 已提交
134
			struct acpi_resource_extended_irq *p =
L
Len Brown 已提交
135
			    &resource->data.extended_irq;
B
Bob Moore 已提交
136
			if (!p || !p->interrupt_count) {
137
				printk(KERN_WARNING PREFIX
138
					      "Blank _PRS EXT IRQ resource\n");
139
				return AE_OK;
L
Len Brown 已提交
140 141
			}
			for (i = 0;
B
Bob Moore 已提交
142
			     (i < p->interrupt_count
L
Len Brown 已提交
143 144
			      && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
				if (!p->interrupts[i]) {
145 146 147
					printk(KERN_WARNING PREFIX
					       "Invalid _PRS IRQ %d\n",
					       p->interrupts[i]);
L
Len Brown 已提交
148 149 150 151
					continue;
				}
				link->irq.possible[i] = p->interrupts[i];
				link->irq.possible_count++;
L
Linus Torvalds 已提交
152
			}
B
Bob Moore 已提交
153 154 155
			link->irq.triggering = p->triggering;
			link->irq.polarity = p->polarity;
			link->irq.resource_type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
L
Len Brown 已提交
156
			break;
L
Linus Torvalds 已提交
157 158
		}
	default:
159 160
		printk(KERN_ERR PREFIX "_PRS resource type 0x%x isn't an IRQ\n",
		       resource->type);
161
		return AE_OK;
L
Linus Torvalds 已提交
162 163
	}

164
	return AE_CTRL_TERMINATE;
L
Linus Torvalds 已提交
165 166
}

L
Len Brown 已提交
167
static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
168
{
L
Len Brown 已提交
169
	acpi_status status;
L
Linus Torvalds 已提交
170

171
	status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
L
Len Brown 已提交
172
				     acpi_pci_link_check_possible, link);
L
Linus Torvalds 已提交
173
	if (ACPI_FAILURE(status)) {
174
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
175
		return -ENODEV;
L
Linus Torvalds 已提交
176 177
	}

L
Len Brown 已提交
178 179 180
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
			  "Found %d possible IRQs\n",
			  link->irq.possible_count));
L
Linus Torvalds 已提交
181

182
	return 0;
L
Linus Torvalds 已提交
183 184
}

185 186
static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
					       void *context)
L
Linus Torvalds 已提交
187
{
188
	int *irq = context;
L
Linus Torvalds 已提交
189

190
	switch (resource->type) {
191 192 193
	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
	case ACPI_RESOURCE_TYPE_END_TAG:
		return AE_OK;
B
Bob Moore 已提交
194
	case ACPI_RESOURCE_TYPE_IRQ:
L
Len Brown 已提交
195 196
		{
			struct acpi_resource_irq *p = &resource->data.irq;
B
Bob Moore 已提交
197
			if (!p || !p->interrupt_count) {
L
Len Brown 已提交
198 199 200 201 202
				/*
				 * IRQ descriptors may have no IRQ# bits set,
				 * particularly those those w/ _STA disabled
				 */
				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
203
						  "Blank _CRS IRQ resource\n"));
204
				return AE_OK;
L
Len Brown 已提交
205 206 207
			}
			*irq = p->interrupts[0];
			break;
L
Linus Torvalds 已提交
208
		}
B
Bob Moore 已提交
209
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
L
Len Brown 已提交
210
		{
B
Bob Moore 已提交
211
			struct acpi_resource_extended_irq *p =
L
Len Brown 已提交
212
			    &resource->data.extended_irq;
B
Bob Moore 已提交
213
			if (!p || !p->interrupt_count) {
L
Len Brown 已提交
214 215 216 217
				/*
				 * extended IRQ descriptors must
				 * return at least 1 IRQ
				 */
218
				printk(KERN_WARNING PREFIX
219
					      "Blank _CRS EXT IRQ resource\n");
220
				return AE_OK;
L
Len Brown 已提交
221 222 223
			}
			*irq = p->interrupts[0];
			break;
L
Linus Torvalds 已提交
224
		}
225
		break;
L
Linus Torvalds 已提交
226
	default:
227 228
		printk(KERN_ERR PREFIX "_CRS resource type 0x%x isn't an IRQ\n",
		       resource->type);
229
		return AE_OK;
L
Linus Torvalds 已提交
230
	}
231

232
	return AE_CTRL_TERMINATE;
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241
}

/*
 * Run _CRS and set link->irq.active
 *
 * return value:
 * 0 - success
 * !0 - failure
 */
L
Len Brown 已提交
242
static int acpi_pci_link_get_current(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
243
{
L
Len Brown 已提交
244
	int result = 0;
245
	acpi_status status;
L
Len Brown 已提交
246
	int irq = 0;
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254

	link->irq.active = 0;

	/* in practice, status disabled is meaningless, ignore it */
	if (acpi_strict) {
		/* Query _STA, set link->device->status */
		result = acpi_bus_get_status(link->device);
		if (result) {
255
			printk(KERN_ERR PREFIX "Unable to read status\n");
L
Linus Torvalds 已提交
256 257 258 259 260
			goto end;
		}

		if (!link->device->status.enabled) {
			ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
261
			return 0;
L
Linus Torvalds 已提交
262 263 264 265 266 267 268
		}
	}

	/* 
	 * Query and parse _CRS to get the current IRQ assignment. 
	 */

269
	status = acpi_walk_resources(link->device->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
270
				     acpi_pci_link_check_current, &irq);
L
Linus Torvalds 已提交
271
	if (ACPI_FAILURE(status)) {
272
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _CRS"));
L
Linus Torvalds 已提交
273 274 275 276 277
		result = -ENODEV;
		goto end;
	}

	if (acpi_strict && !irq) {
278
		printk(KERN_ERR PREFIX "_CRS returned 0\n");
L
Linus Torvalds 已提交
279 280 281 282 283 284 285
		result = -ENODEV;
	}

	link->irq.active = irq;

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));

L
Len Brown 已提交
286
      end:
287
	return result;
L
Linus Torvalds 已提交
288 289
}

L
Len Brown 已提交
290
static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
L
Linus Torvalds 已提交
291
{
292 293
	int result;
	acpi_status status;
L
Linus Torvalds 已提交
294
	struct {
L
Len Brown 已提交
295 296 297 298
		struct acpi_resource res;
		struct acpi_resource end;
	} *resource;
	struct acpi_buffer buffer = { 0, NULL };
L
Linus Torvalds 已提交
299

300
	if (!irq)
301
		return -EINVAL;
L
Linus Torvalds 已提交
302

303
	resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
L
Len Brown 已提交
304
	if (!resource)
305
		return -ENOMEM;
L
Linus Torvalds 已提交
306

L
Len Brown 已提交
307
	buffer.length = sizeof(*resource) + 1;
L
Linus Torvalds 已提交
308 309
	buffer.pointer = resource;

L
Len Brown 已提交
310
	switch (link->irq.resource_type) {
B
Bob Moore 已提交
311 312
	case ACPI_RESOURCE_TYPE_IRQ:
		resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
L
Linus Torvalds 已提交
313
		resource->res.length = sizeof(struct acpi_resource);
B
Bob Moore 已提交
314 315 316 317 318
		resource->res.data.irq.triggering = link->irq.triggering;
		resource->res.data.irq.polarity =
		    link->irq.polarity;
		if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
			resource->res.data.irq.sharable =
L
Len Brown 已提交
319
			    ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
320
		else
B
Bob Moore 已提交
321 322
			resource->res.data.irq.sharable = ACPI_SHARED;
		resource->res.data.irq.interrupt_count = 1;
L
Linus Torvalds 已提交
323 324
		resource->res.data.irq.interrupts[0] = irq;
		break;
L
Len Brown 已提交
325

B
Bob Moore 已提交
326 327
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
		resource->res.type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
L
Linus Torvalds 已提交
328
		resource->res.length = sizeof(struct acpi_resource);
L
Len Brown 已提交
329 330
		resource->res.data.extended_irq.producer_consumer =
		    ACPI_CONSUMER;
B
Bob Moore 已提交
331 332 333 334 335 336
		resource->res.data.extended_irq.triggering =
		    link->irq.triggering;
		resource->res.data.extended_irq.polarity =
		    link->irq.polarity;
		if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
			resource->res.data.irq.sharable =
L
Len Brown 已提交
337
			    ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
338
		else
B
Bob Moore 已提交
339 340
			resource->res.data.irq.sharable = ACPI_SHARED;
		resource->res.data.extended_irq.interrupt_count = 1;
L
Linus Torvalds 已提交
341 342 343 344
		resource->res.data.extended_irq.interrupts[0] = irq;
		/* ignore resource_source, it's optional */
		break;
	default:
345
		printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
L
Linus Torvalds 已提交
346 347 348 349
		result = -EINVAL;
		goto end;

	}
B
Bob Moore 已提交
350
	resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
351
	resource->end.length = sizeof(struct acpi_resource);
L
Linus Torvalds 已提交
352 353

	/* Attempt to set the resource */
354
	status = acpi_set_current_resources(link->device->handle, &buffer);
L
Linus Torvalds 已提交
355 356 357

	/* check for total failure */
	if (ACPI_FAILURE(status)) {
358
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
L
Linus Torvalds 已提交
359 360 361 362 363 364 365
		result = -ENODEV;
		goto end;
	}

	/* Query _STA, set device->status */
	result = acpi_bus_get_status(link->device);
	if (result) {
366
		printk(KERN_ERR PREFIX "Unable to read status\n");
L
Linus Torvalds 已提交
367 368 369
		goto end;
	}
	if (!link->device->status.enabled) {
370 371
		printk(KERN_WARNING PREFIX
			      "%s [%s] disabled and referenced, BIOS bug\n",
372
			      acpi_device_name(link->device),
373
			      acpi_device_bid(link->device));
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	}

	/* Query _CRS, set link->irq.active */
	result = acpi_pci_link_get_current(link);
	if (result) {
		goto end;
	}

	/*
	 * Is current setting not what we set?
	 * set link->irq.active
	 */
	if (link->irq.active != irq) {
		/*
		 * policy: when _CRS doesn't return what we just _SRS
		 * assume _SRS worked and override _CRS value.
		 */
391 392
		printk(KERN_WARNING PREFIX
			      "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
393
			      acpi_device_name(link->device),
394
			      acpi_device_bid(link->device), link->irq.active, irq);
L
Linus Torvalds 已提交
395 396 397 398
		link->irq.active = irq;
	}

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
L
Len Brown 已提交
399 400

      end:
L
Linus Torvalds 已提交
401
	kfree(resource);
402
	return result;
L
Linus Torvalds 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
}

/* --------------------------------------------------------------------------
                            PCI Link IRQ Management
   -------------------------------------------------------------------------- */

/*
 * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
 * Link Devices to move the PIRQs around to minimize sharing.
 * 
 * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
 * that the BIOS has already set to active.  This is necessary because
 * ACPI has no automatic means of knowing what ISA IRQs are used.  Note that
 * if the BIOS doesn't set a Link Device active, ACPI needs to program it
 * even if acpi_irq_nobalance is set.
 *
 * A tables of penalties avoids directing PCI interrupts to well known
 * ISA IRQs. Boot params are available to over-ride the default table:
 *
 * List interrupts that are free for PCI use.
 * acpi_irq_pci=n[,m]
 *
 * List interrupts that should not be used for PCI:
 * acpi_irq_isa=n[,m]
 *
 * Note that PCI IRQ routers have a list of possible IRQs,
 * which may not include the IRQs this table says are available.
 * 
 * Since this heuristic can't tell the difference between a link
 * that no device will attach to, vs. a link which may be shared
 * by multiple active devices -- it is not optimal.
 *
 * If interrupt performance is that important, get an IO-APIC system
 * with a pin dedicated to each device.  Or for that matter, an MSI
 * enabled system.
 */

440
#define ACPI_MAX_IRQS		256
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449
#define ACPI_MAX_ISA_IRQ	16

#define PIRQ_PENALTY_PCI_AVAILABLE	(0)
#define PIRQ_PENALTY_PCI_POSSIBLE	(16*16)
#define PIRQ_PENALTY_PCI_USING		(16*16*16)
#define PIRQ_PENALTY_ISA_TYPICAL	(16*16*16*16)
#define PIRQ_PENALTY_ISA_USED		(16*16*16*16*16)
#define PIRQ_PENALTY_ISA_ALWAYS		(16*16*16*16*16*16)

450
static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
L
Linus Torvalds 已提交
451 452 453
	PIRQ_PENALTY_ISA_ALWAYS,	/* IRQ0 timer */
	PIRQ_PENALTY_ISA_ALWAYS,	/* IRQ1 keyboard */
	PIRQ_PENALTY_ISA_ALWAYS,	/* IRQ2 cascade */
L
Len Brown 已提交
454 455
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ3 serial */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ4 serial */
L
Linus Torvalds 已提交
456 457 458 459 460 461 462
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ5 sometimes SoundBlaster */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ6 */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ7 parallel, spurious */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ8 rtc, sometimes */
	PIRQ_PENALTY_PCI_AVAILABLE,	/* IRQ9  PCI, often acpi */
	PIRQ_PENALTY_PCI_AVAILABLE,	/* IRQ10 PCI */
	PIRQ_PENALTY_PCI_AVAILABLE,	/* IRQ11 PCI */
463 464 465 466
	PIRQ_PENALTY_ISA_USED,		/* IRQ12 mouse */
	PIRQ_PENALTY_ISA_USED,		/* IRQ13 fpe, sometimes */
	PIRQ_PENALTY_ISA_USED,		/* IRQ14 ide0 */
	PIRQ_PENALTY_ISA_USED,		/* IRQ15 ide1 */
467
	/* >IRQ15 */
L
Linus Torvalds 已提交
468 469
};

L
Len Brown 已提交
470
int __init acpi_irq_penalty_init(void)
L
Linus Torvalds 已提交
471
{
472 473
	struct acpi_pci_link *link;
	int i;
L
Linus Torvalds 已提交
474 475 476 477

	/*
	 * Update penalties to facilitate IRQ balancing.
	 */
478
	list_for_each_entry(link, &acpi_link_list, list) {
L
Linus Torvalds 已提交
479 480 481 482 483 484

		/*
		 * reflect the possible and active irqs in the penalty table --
		 * useful for breaking ties.
		 */
		if (link->irq.possible_count) {
L
Len Brown 已提交
485 486 487
			int penalty =
			    PIRQ_PENALTY_PCI_POSSIBLE /
			    link->irq.possible_count;
L
Linus Torvalds 已提交
488 489

			for (i = 0; i < link->irq.possible_count; i++) {
490 491 492 493
				if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
					acpi_irq_penalty[link->irq.
							 possible[i]] +=
					    penalty;
L
Linus Torvalds 已提交
494 495 496
			}

		} else if (link->irq.active) {
497 498
			acpi_irq_penalty[link->irq.active] +=
			    PIRQ_PENALTY_PCI_POSSIBLE;
L
Linus Torvalds 已提交
499 500
		}
	}
501

502
	return 0;
L
Linus Torvalds 已提交
503 504
}

505
static int acpi_irq_balance = -1;	/* 0: static, 1: balance */
L
Linus Torvalds 已提交
506

L
Len Brown 已提交
507
static int acpi_pci_link_allocate(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
508
{
L
Len Brown 已提交
509 510
	int irq;
	int i;
L
Linus Torvalds 已提交
511

512 513 514 515
	if (link->irq.initialized) {
		if (link->refcnt == 0)
			/* This means the link is disabled but initialized */
			acpi_pci_link_set(link, link->irq.active);
516
		return 0;
517
	}
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530

	/*
	 * search for active IRQ in list of possible IRQs.
	 */
	for (i = 0; i < link->irq.possible_count; ++i) {
		if (link->irq.active == link->irq.possible[i])
			break;
	}
	/*
	 * forget active IRQ that is not in possible list
	 */
	if (i == link->irq.possible_count) {
		if (acpi_strict)
531 532
			printk(KERN_WARNING PREFIX "_CRS %d not found"
				      " in _PRS\n", link->irq.active);
L
Linus Torvalds 已提交
533 534 535 536 537 538
		link->irq.active = 0;
	}

	/*
	 * if active found, use it; else pick entry from end of possible list.
	 */
539
	if (link->irq.active)
L
Linus Torvalds 已提交
540
		irq = link->irq.active;
541
	else
L
Linus Torvalds 已提交
542 543 544 545 546 547 548 549
		irq = link->irq.possible[link->irq.possible_count - 1];

	if (acpi_irq_balance || !link->irq.active) {
		/*
		 * Select the best IRQ.  This is done in reverse to promote
		 * the use of IRQs 9, 10, 11, and >15.
		 */
		for (i = (link->irq.possible_count - 1); i >= 0; i--) {
550 551
			if (acpi_irq_penalty[irq] >
			    acpi_irq_penalty[link->irq.possible[i]])
L
Linus Torvalds 已提交
552 553 554
				irq = link->irq.possible[i];
		}
	}
555
	if (acpi_irq_penalty[irq] >= PIRQ_PENALTY_ISA_ALWAYS) {
556 557 558 559 560 561
		printk(KERN_ERR PREFIX "No IRQ available for %s [%s]. "
			    "Try pci=noacpi or acpi=off\n",
			    acpi_device_name(link->device),
			    acpi_device_bid(link->device));
		return -ENODEV;
	}
L
Linus Torvalds 已提交
562 563 564

	/* Attempt to enable the link device at this IRQ. */
	if (acpi_pci_link_set(link, irq)) {
565 566
		printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
			    "Try pci=noacpi or acpi=off\n",
567
			    acpi_device_name(link->device),
568
			    acpi_device_bid(link->device));
569
		return -ENODEV;
L
Linus Torvalds 已提交
570
	} else {
571
		acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
572
		printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
L
Len Brown 已提交
573 574
		       acpi_device_name(link->device),
		       acpi_device_bid(link->device), link->irq.active);
L
Linus Torvalds 已提交
575 576 577
	}

	link->irq.initialized = 1;
578
	return 0;
L
Linus Torvalds 已提交
579 580 581
}

/*
582
 * acpi_pci_link_allocate_irq
L
Linus Torvalds 已提交
583 584 585
 * success: return IRQ >= 0
 * failure: return -1
 */
586 587
int acpi_pci_link_allocate_irq(acpi_handle handle, int index, int *triggering,
			       int *polarity, char **name)
L
Linus Torvalds 已提交
588
{
589 590 591
	int result;
	struct acpi_device *device;
	struct acpi_pci_link *link;
L
Linus Torvalds 已提交
592 593 594

	result = acpi_bus_get_device(handle, &device);
	if (result) {
595
		printk(KERN_ERR PREFIX "Invalid link device\n");
596
		return -1;
L
Linus Torvalds 已提交
597 598
	}

599
	link = acpi_driver_data(device);
L
Linus Torvalds 已提交
600
	if (!link) {
601
		printk(KERN_ERR PREFIX "Invalid link context\n");
602
		return -1;
L
Linus Torvalds 已提交
603 604 605 606
	}

	/* TBD: Support multiple index (IRQ) entries per Link Device */
	if (index) {
607
		printk(KERN_ERR PREFIX "Invalid index %d\n", index);
608
		return -1;
L
Linus Torvalds 已提交
609 610
	}

I
Ingo Molnar 已提交
611
	mutex_lock(&acpi_link_lock);
612
	if (acpi_pci_link_allocate(link)) {
I
Ingo Molnar 已提交
613
		mutex_unlock(&acpi_link_lock);
614
		return -1;
615
	}
L
Len Brown 已提交
616

L
Linus Torvalds 已提交
617
	if (!link->irq.active) {
I
Ingo Molnar 已提交
618
		mutex_unlock(&acpi_link_lock);
619
		printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
620
		return -1;
L
Linus Torvalds 已提交
621
	}
L
Len Brown 已提交
622
	link->refcnt++;
I
Ingo Molnar 已提交
623
	mutex_unlock(&acpi_link_lock);
L
Linus Torvalds 已提交
624

B
Bob Moore 已提交
625 626 627 628
	if (triggering)
		*triggering = link->irq.triggering;
	if (polarity)
		*polarity = link->irq.polarity;
L
Len Brown 已提交
629 630
	if (name)
		*name = acpi_device_bid(link->device);
631
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
632 633
			  "Link %s is referenced\n",
			  acpi_device_bid(link->device)));
634
	return (link->irq.active);
L
Linus Torvalds 已提交
635 636
}

637 638 639 640
/*
 * We don't change link's irq information here.  After it is reenabled, we
 * continue use the info
 */
L
Len Brown 已提交
641
int acpi_pci_link_free_irq(acpi_handle handle)
642
{
643 644
	struct acpi_device *device;
	struct acpi_pci_link *link;
L
Len Brown 已提交
645
	acpi_status result;
646 647 648

	result = acpi_bus_get_device(handle, &device);
	if (result) {
649
		printk(KERN_ERR PREFIX "Invalid link device\n");
650
		return -1;
651
	}
L
Linus Torvalds 已提交
652

653
	link = acpi_driver_data(device);
654
	if (!link) {
655
		printk(KERN_ERR PREFIX "Invalid link context\n");
656
		return -1;
657 658
	}

I
Ingo Molnar 已提交
659
	mutex_lock(&acpi_link_lock);
660
	if (!link->irq.initialized) {
I
Ingo Molnar 已提交
661
		mutex_unlock(&acpi_link_lock);
662
		printk(KERN_ERR PREFIX "Link isn't initialized\n");
663
		return -1;
664
	}
665 666 667 668 669 670 671 672 673 674
#ifdef	FUTURE_USE
	/*
	 * The Link reference count allows us to _DISable an unused link
	 * and suspend time, and set it again  on resume.
	 * However, 2.6.12 still has irq_router.resume
	 * which blindly restores the link state.
	 * So we disable the reference count method
	 * to prevent duplicate acpi_pci_link_set()
	 * which would harm some systems
	 */
L
Len Brown 已提交
675
	link->refcnt--;
676
#endif
677
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
678 679
			  "Link %s is dereferenced\n",
			  acpi_device_bid(link->device)));
680

681
	if (link->refcnt == 0)
682
		acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
683

I
Ingo Molnar 已提交
684
	mutex_unlock(&acpi_link_lock);
685
	return (link->irq.active);
686
}
L
Len Brown 已提交
687

L
Linus Torvalds 已提交
688 689 690 691
/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

692 693
static int acpi_pci_link_add(struct acpi_device *device,
			     const struct acpi_device_id *not_used)
L
Linus Torvalds 已提交
694
{
695 696 697
	int result;
	struct acpi_pci_link *link;
	int i;
L
Len Brown 已提交
698
	int found = 0;
L
Linus Torvalds 已提交
699

700
	link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
L
Linus Torvalds 已提交
701
	if (!link)
702
		return -ENOMEM;
L
Linus Torvalds 已提交
703 704 705 706

	link->device = device;
	strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
707
	device->driver_data = link;
L
Linus Torvalds 已提交
708

I
Ingo Molnar 已提交
709
	mutex_lock(&acpi_link_lock);
L
Linus Torvalds 已提交
710 711 712 713 714 715 716
	result = acpi_pci_link_get_possible(link);
	if (result)
		goto end;

	/* query and set link->irq.active */
	acpi_pci_link_get_current(link);

717
	printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
L
Len Brown 已提交
718
	       acpi_device_bid(device));
L
Linus Torvalds 已提交
719 720
	for (i = 0; i < link->irq.possible_count; i++) {
		if (link->irq.active == link->irq.possible[i]) {
721
			printk(KERN_CONT " *%d", link->irq.possible[i]);
L
Linus Torvalds 已提交
722
			found = 1;
L
Len Brown 已提交
723
		} else
724
			printk(KERN_CONT " %d", link->irq.possible[i]);
L
Linus Torvalds 已提交
725 726
	}

727
	printk(KERN_CONT ")");
L
Linus Torvalds 已提交
728 729

	if (!found)
730
		printk(KERN_CONT " *%d", link->irq.active);
L
Linus Torvalds 已提交
731

L
Len Brown 已提交
732
	if (!link->device->status.enabled)
733
		printk(KERN_CONT ", disabled.");
L
Linus Torvalds 已提交
734

735
	printk(KERN_CONT "\n");
L
Linus Torvalds 已提交
736

737
	list_add_tail(&link->list, &acpi_link_list);
L
Linus Torvalds 已提交
738

L
Len Brown 已提交
739
      end:
L
Linus Torvalds 已提交
740
	/* disable all links -- to be activated on use */
741
	acpi_evaluate_object(device->handle, "_DIS", NULL, NULL);
I
Ingo Molnar 已提交
742
	mutex_unlock(&acpi_link_lock);
L
Linus Torvalds 已提交
743 744 745 746

	if (result)
		kfree(link);

747
	return result < 0 ? result : 1;
L
Linus Torvalds 已提交
748 749
}

L
Len Brown 已提交
750
static int acpi_pci_link_resume(struct acpi_pci_link *link)
751 752
{
	if (link->refcnt && link->irq.active && link->irq.initialized)
753
		return (acpi_pci_link_set(link, link->irq.active));
754 755

	return 0;
756 757
}

758
static void irqrouter_resume(void)
L
Linus Torvalds 已提交
759
{
760
	struct acpi_pci_link *link;
L
Linus Torvalds 已提交
761

762
	list_for_each_entry(link, &acpi_link_list, list) {
763
		acpi_pci_link_resume(link);
L
Linus Torvalds 已提交
764 765 766
	}
}

767
static void acpi_pci_link_remove(struct acpi_device *device)
L
Linus Torvalds 已提交
768
{
769
	struct acpi_pci_link *link;
L
Linus Torvalds 已提交
770

771
	link = acpi_driver_data(device);
L
Linus Torvalds 已提交
772

I
Ingo Molnar 已提交
773
	mutex_lock(&acpi_link_lock);
774
	list_del(&link->list);
I
Ingo Molnar 已提交
775
	mutex_unlock(&acpi_link_lock);
L
Linus Torvalds 已提交
776 777 778 779 780

	kfree(link);
}

/*
781
 * modify acpi_irq_penalty[] from cmdline
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789 790
 */
static int __init acpi_irq_penalty_update(char *str, int used)
{
	int i;

	for (i = 0; i < 16; i++) {
		int retval;
		int irq;

L
Len Brown 已提交
791
		retval = get_option(&str, &irq);
L
Linus Torvalds 已提交
792 793 794 795 796 797

		if (!retval)
			break;	/* no number found */

		if (irq < 0)
			continue;
L
Len Brown 已提交
798

799 800 801
		if (irq >= ARRAY_SIZE(acpi_irq_penalty))
			continue;

L
Linus Torvalds 已提交
802
		if (used)
803
			acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
L
Linus Torvalds 已提交
804
		else
805
			acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
L
Linus Torvalds 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819

		if (retval != 2)	/* no next number */
			break;
	}
	return 1;
}

/*
 * We'd like PNP to call this routine for the
 * single ISA_USED value for each legacy device.
 * But instead it calls us with each POSSIBLE setting.
 * There is no ISA_POSSIBLE weight, so we simply use
 * the (small) PCI_USING penalty.
 */
D
David Shaohua Li 已提交
820
void acpi_penalize_isa_irq(int irq, int active)
L
Linus Torvalds 已提交
821
{
822 823 824 825 826 827
	if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
		if (active)
			acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
		else
			acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
	}
L
Linus Torvalds 已提交
828 829
}

830 831
bool acpi_isa_irq_available(int irq)
{
832 833
	return irq >= 0 && (irq >= ARRAY_SIZE(acpi_irq_penalty) ||
			    acpi_irq_penalty[irq] < PIRQ_PENALTY_ISA_ALWAYS);
834 835
}

836 837 838 839 840 841 842
/*
 * Penalize IRQ used by ACPI SCI. If ACPI SCI pin attributes conflict with
 * PCI IRQ attributes, mark ACPI SCI as ISA_ALWAYS so it won't be use for
 * PCI IRQs.
 */
void acpi_penalize_sci_irq(int irq, int trigger, int polarity)
{
843 844 845 846 847 848 849
	if (irq >= 0 && irq < ARRAY_SIZE(acpi_irq_penalty)) {
		if (trigger != ACPI_MADT_TRIGGER_LEVEL ||
		    polarity != ACPI_MADT_POLARITY_ACTIVE_LOW)
			acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_ALWAYS;
		else
			acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
	}
850 851
}

L
Linus Torvalds 已提交
852 853 854 855 856 857 858 859 860
/*
 * Over-ride default table to reserve additional IRQs for use by ISA
 * e.g. acpi_irq_isa=5
 * Useful for telling ACPI how not to interfere with your ISA sound card.
 */
static int __init acpi_irq_isa(char *str)
{
	return acpi_irq_penalty_update(str, 1);
}
L
Len Brown 已提交
861

L
Linus Torvalds 已提交
862 863 864 865 866 867 868 869 870 871 872
__setup("acpi_irq_isa=", acpi_irq_isa);

/*
 * Over-ride default table to free additional IRQs for use by PCI
 * e.g. acpi_irq_pci=7,15
 * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
 */
static int __init acpi_irq_pci(char *str)
{
	return acpi_irq_penalty_update(str, 0);
}
L
Len Brown 已提交
873

L
Linus Torvalds 已提交
874 875 876 877 878 879 880
__setup("acpi_irq_pci=", acpi_irq_pci);

static int __init acpi_irq_nobalance_set(char *str)
{
	acpi_irq_balance = 0;
	return 1;
}
L
Len Brown 已提交
881

L
Linus Torvalds 已提交
882 883
__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);

884
static int __init acpi_irq_balance_set(char *str)
L
Linus Torvalds 已提交
885 886 887 888 889
{
	acpi_irq_balance = 1;
	return 1;
}

L
Len Brown 已提交
890
__setup("acpi_irq_balance", acpi_irq_balance_set);
L
Linus Torvalds 已提交
891

892
static struct syscore_ops irqrouter_syscore_ops = {
L
Len Brown 已提交
893
	.resume = irqrouter_resume,
L
Linus Torvalds 已提交
894 895
};

896
void __init acpi_pci_link_init(void)
L
Linus Torvalds 已提交
897 898
{
	if (acpi_noirq)
899
		return;
L
Linus Torvalds 已提交
900

901 902 903 904 905 906 907
	if (acpi_irq_balance == -1) {
		/* no command line switch: enable balancing in IOAPIC mode */
		if (acpi_irq_model == ACPI_IRQ_MODEL_IOAPIC)
			acpi_irq_balance = 1;
		else
			acpi_irq_balance = 0;
	}
908 909
	register_syscore_ops(&irqrouter_syscore_ops);
	acpi_scan_add_handler(&pci_link_handler);
L
Linus Torvalds 已提交
910
}