pci_link.c 23.7 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 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 *  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.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * 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).
 */

#include <linux/sysdev.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
#include <linux/pci.h>
I
Ingo Molnar 已提交
41
#include <linux/mutex.h>
L
Linus Torvalds 已提交
42 43 44 45 46

#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

#define _COMPONENT		ACPI_PCI_COMPONENT
47
ACPI_MODULE_NAME("pci_link");
L
Linus Torvalds 已提交
48 49 50 51 52 53
#define ACPI_PCI_LINK_CLASS		"pci_irq_routing"
#define ACPI_PCI_LINK_HID		"PNP0C0F"
#define ACPI_PCI_LINK_DEVICE_NAME	"PCI Interrupt Link"
#define ACPI_PCI_LINK_FILE_INFO		"info"
#define ACPI_PCI_LINK_FILE_STATUS	"state"
#define ACPI_PCI_LINK_MAX_POSSIBLE 16
L
Len Brown 已提交
54 55
static int acpi_pci_link_add(struct acpi_device *device);
static int acpi_pci_link_remove(struct acpi_device *device, int type);
L
Linus Torvalds 已提交
56 57

static struct acpi_driver acpi_pci_link_driver = {
L
Len Brown 已提交
58
	.name = "pci_link",
L
Len Brown 已提交
59 60 61 62 63 64
	.class = ACPI_PCI_LINK_CLASS,
	.ids = ACPI_PCI_LINK_HID,
	.ops = {
		.add = acpi_pci_link_add,
		.remove = acpi_pci_link_remove,
		},
L
Linus Torvalds 已提交
65 66
};

67 68 69 70
/*
 * 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 已提交
71
struct acpi_pci_link_irq {
L
Len Brown 已提交
72
	u8 active;		/* Current IRQ */
B
Bob Moore 已提交
73 74
	u8 triggering;		/* All IRQs */
	u8 polarity;	/* All IRQs */
L
Len Brown 已提交
75 76 77 78 79
	u8 resource_type;
	u8 possible_count;
	u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
	u8 initialized:1;
	u8 reserved:7;
L
Linus Torvalds 已提交
80 81 82
};

struct acpi_pci_link {
L
Len Brown 已提交
83 84
	struct list_head node;
	struct acpi_device *device;
L
Linus Torvalds 已提交
85
	struct acpi_pci_link_irq irq;
L
Len Brown 已提交
86
	int refcnt;
L
Linus Torvalds 已提交
87 88 89
};

static struct {
L
Len Brown 已提交
90 91 92
	int count;
	struct list_head entries;
} acpi_link;
I
Ingo Molnar 已提交
93
DEFINE_MUTEX(acpi_link_lock);
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102

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

/*
 * set context (link) possible list from resource list
 */
static acpi_status
L
Len Brown 已提交
103
acpi_pci_link_check_possible(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
104
{
105
	struct acpi_pci_link *link = context;
L
Len Brown 已提交
106
	u32 i = 0;
L
Linus Torvalds 已提交
107 108


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

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

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


	if (!link)
174
		return -EINVAL;
L
Linus Torvalds 已提交
175

176
	status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
L
Len Brown 已提交
177
				     acpi_pci_link_check_possible, link);
L
Linus Torvalds 已提交
178
	if (ACPI_FAILURE(status)) {
179
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
180
		return -ENODEV;
L
Linus Torvalds 已提交
181 182
	}

L
Len Brown 已提交
183 184 185
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
			  "Found %d possible IRQs\n",
			  link->irq.possible_count));
L
Linus Torvalds 已提交
186

187
	return 0;
L
Linus Torvalds 已提交
188 189 190
}

static acpi_status
L
Len Brown 已提交
191
acpi_pci_link_check_current(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
192
{
L
Len Brown 已提交
193
	int *irq = (int *)context;
L
Linus Torvalds 已提交
194 195


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

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

250
	if (!link)
251
		return -EINVAL;
L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259

	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) {
260
			printk(KERN_ERR PREFIX "Unable to read status\n");
L
Linus Torvalds 已提交
261 262 263 264 265
			goto end;
		}

		if (!link->device->status.enabled) {
			ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
266
			return 0;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273
		}
	}

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

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

	if (acpi_strict && !irq) {
283
		printk(KERN_ERR PREFIX "_CRS returned 0\n");
L
Linus Torvalds 已提交
284 285 286 287 288 289 290
		result = -ENODEV;
	}

	link->irq.active = irq;

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

L
Len Brown 已提交
291
      end:
292
	return result;
L
Linus Torvalds 已提交
293 294
}

L
Len Brown 已提交
295
static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
L
Linus Torvalds 已提交
296
{
L
Len Brown 已提交
297 298
	int result = 0;
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
299
	struct {
L
Len Brown 已提交
300 301 302 303
		struct acpi_resource res;
		struct acpi_resource end;
	} *resource;
	struct acpi_buffer buffer = { 0, NULL };
L
Linus Torvalds 已提交
304 305 306


	if (!link || !irq)
307
		return -EINVAL;
L
Linus Torvalds 已提交
308

309
	resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
L
Len Brown 已提交
310
	if (!resource)
311
		return -ENOMEM;
L
Linus Torvalds 已提交
312

L
Len Brown 已提交
313
	buffer.length = sizeof(*resource) + 1;
L
Linus Torvalds 已提交
314 315
	buffer.pointer = resource;

L
Len Brown 已提交
316
	switch (link->irq.resource_type) {
B
Bob Moore 已提交
317 318
	case ACPI_RESOURCE_TYPE_IRQ:
		resource->res.type = ACPI_RESOURCE_TYPE_IRQ;
L
Linus Torvalds 已提交
319
		resource->res.length = sizeof(struct acpi_resource);
B
Bob Moore 已提交
320 321 322 323 324
		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 已提交
325
			    ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
326
		else
B
Bob Moore 已提交
327 328
			resource->res.data.irq.sharable = ACPI_SHARED;
		resource->res.data.irq.interrupt_count = 1;
L
Linus Torvalds 已提交
329 330
		resource->res.data.irq.interrupts[0] = irq;
		break;
L
Len Brown 已提交
331

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

	}
B
Bob Moore 已提交
356
	resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
L
Linus Torvalds 已提交
357 358

	/* Attempt to set the resource */
359
	status = acpi_set_current_resources(link->device->handle, &buffer);
L
Linus Torvalds 已提交
360 361 362

	/* check for total failure */
	if (ACPI_FAILURE(status)) {
363
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SRS"));
L
Linus Torvalds 已提交
364 365 366 367 368 369 370
		result = -ENODEV;
		goto end;
	}

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

	/* 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.
		 */
396 397
		printk(KERN_WARNING PREFIX
			      "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
398
			      acpi_device_name(link->device),
399
			      acpi_device_bid(link->device), link->irq.active, irq);
L
Linus Torvalds 已提交
400 401 402 403
		link->irq.active = irq;
	}

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

      end:
L
Linus Torvalds 已提交
406
	kfree(resource);
407
	return result;
L
Linus Torvalds 已提交
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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
}

/* --------------------------------------------------------------------------
                            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.
 */

#define ACPI_MAX_IRQS		256
#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)

static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
	PIRQ_PENALTY_ISA_ALWAYS,	/* IRQ0 timer */
	PIRQ_PENALTY_ISA_ALWAYS,	/* IRQ1 keyboard */
	PIRQ_PENALTY_ISA_ALWAYS,	/* IRQ2 cascade */
L
Len Brown 已提交
459 460
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ3 serial */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ4 serial */
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469 470 471
	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 */
	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 */
L
Len Brown 已提交
472
	/* >IRQ15 */
L
Linus Torvalds 已提交
473 474
};

L
Len Brown 已提交
475
int __init acpi_irq_penalty_init(void)
L
Linus Torvalds 已提交
476
{
L
Len Brown 已提交
477 478 479
	struct list_head *node = NULL;
	struct acpi_pci_link *link = NULL;
	int i = 0;
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488


	/*
	 * Update penalties to facilitate IRQ balancing.
	 */
	list_for_each(node, &acpi_link.entries) {

		link = list_entry(node, struct acpi_pci_link, node);
		if (!link) {
489
			printk(KERN_ERR PREFIX "Invalid link context\n");
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497
			continue;
		}

		/*
		 * reflect the possible and active irqs in the penalty table --
		 * useful for breaking ties.
		 */
		if (link->irq.possible_count) {
L
Len Brown 已提交
498 499 500
			int penalty =
			    PIRQ_PENALTY_PCI_POSSIBLE /
			    link->irq.possible_count;
L
Linus Torvalds 已提交
501 502 503

			for (i = 0; i < link->irq.possible_count; i++) {
				if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
L
Len Brown 已提交
504 505 506
					acpi_irq_penalty[link->irq.
							 possible[i]] +=
					    penalty;
L
Linus Torvalds 已提交
507 508 509
			}

		} else if (link->irq.active) {
L
Len Brown 已提交
510 511
			acpi_irq_penalty[link->irq.active] +=
			    PIRQ_PENALTY_PCI_POSSIBLE;
L
Linus Torvalds 已提交
512 513 514
		}
	}
	/* Add a penalty for the SCI */
515
	acpi_irq_penalty[acpi_gbl_FADT.sci_interrupt] += PIRQ_PENALTY_PCI_USING;
L
Linus Torvalds 已提交
516

517
	return 0;
L
Linus Torvalds 已提交
518 519 520 521
}

static int acpi_irq_balance;	/* 0: static, 1: balance */

L
Len Brown 已提交
522
static int acpi_pci_link_allocate(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
523
{
L
Len Brown 已提交
524 525
	int irq;
	int i;
L
Linus Torvalds 已提交
526 527


528 529 530 531
	if (link->irq.initialized) {
		if (link->refcnt == 0)
			/* This means the link is disabled but initialized */
			acpi_pci_link_set(link, link->irq.active);
532
		return 0;
533
	}
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546

	/*
	 * 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)
547 548
			printk(KERN_WARNING PREFIX "_CRS %d not found"
				      " in _PRS\n", link->irq.active);
L
Linus Torvalds 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		link->irq.active = 0;
	}

	/*
	 * if active found, use it; else pick entry from end of possible list.
	 */
	if (link->irq.active) {
		irq = link->irq.active;
	} else {
		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--) {
L
Len Brown 已提交
567 568
			if (acpi_irq_penalty[irq] >
			    acpi_irq_penalty[link->irq.possible[i]])
L
Linus Torvalds 已提交
569 570 571 572 573 574
				irq = link->irq.possible[i];
		}
	}

	/* Attempt to enable the link device at this IRQ. */
	if (acpi_pci_link_set(link, irq)) {
575 576
		printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
			    "Try pci=noacpi or acpi=off\n",
577
			    acpi_device_name(link->device),
578
			    acpi_device_bid(link->device));
579
		return -ENODEV;
L
Linus Torvalds 已提交
580 581
	} else {
		acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
L
Len Brown 已提交
582 583 584
		printk(PREFIX "%s [%s] enabled at IRQ %d\n",
		       acpi_device_name(link->device),
		       acpi_device_bid(link->device), link->irq.active);
L
Linus Torvalds 已提交
585 586 587 588
	}

	link->irq.initialized = 1;

589
	return 0;
L
Linus Torvalds 已提交
590 591 592
}

/*
593
 * acpi_pci_link_allocate_irq
L
Linus Torvalds 已提交
594 595 596 597 598
 * success: return IRQ >= 0
 * failure: return -1
 */

int
L
Len Brown 已提交
599 600
acpi_pci_link_allocate_irq(acpi_handle handle,
			   int index,
B
Bob Moore 已提交
601
			   int *triggering, int *polarity, char **name)
L
Linus Torvalds 已提交
602
{
L
Len Brown 已提交
603 604 605
	int result = 0;
	struct acpi_device *device = NULL;
	struct acpi_pci_link *link = NULL;
L
Linus Torvalds 已提交
606 607 608 609


	result = acpi_bus_get_device(handle, &device);
	if (result) {
610
		printk(KERN_ERR PREFIX "Invalid link device\n");
611
		return -1;
L
Linus Torvalds 已提交
612 613
	}

614
	link = acpi_driver_data(device);
L
Linus Torvalds 已提交
615
	if (!link) {
616
		printk(KERN_ERR PREFIX "Invalid link context\n");
617
		return -1;
L
Linus Torvalds 已提交
618 619 620 621
	}

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

I
Ingo Molnar 已提交
626
	mutex_lock(&acpi_link_lock);
627
	if (acpi_pci_link_allocate(link)) {
I
Ingo Molnar 已提交
628
		mutex_unlock(&acpi_link_lock);
629
		return -1;
630
	}
L
Len Brown 已提交
631

L
Linus Torvalds 已提交
632
	if (!link->irq.active) {
I
Ingo Molnar 已提交
633
		mutex_unlock(&acpi_link_lock);
634
		printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
635
		return -1;
L
Linus Torvalds 已提交
636
	}
L
Len Brown 已提交
637
	link->refcnt++;
I
Ingo Molnar 已提交
638
	mutex_unlock(&acpi_link_lock);
L
Linus Torvalds 已提交
639

B
Bob Moore 已提交
640 641 642 643
	if (triggering)
		*triggering = link->irq.triggering;
	if (polarity)
		*polarity = link->irq.polarity;
L
Len Brown 已提交
644 645
	if (name)
		*name = acpi_device_bid(link->device);
646
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
647 648
			  "Link %s is referenced\n",
			  acpi_device_bid(link->device)));
649
	return (link->irq.active);
L
Linus Torvalds 已提交
650 651
}

652 653 654 655
/*
 * We don't change link's irq information here.  After it is reenabled, we
 * continue use the info
 */
L
Len Brown 已提交
656
int acpi_pci_link_free_irq(acpi_handle handle)
657
{
L
Len Brown 已提交
658 659 660
	struct acpi_device *device = NULL;
	struct acpi_pci_link *link = NULL;
	acpi_status result;
661 662 663 664


	result = acpi_bus_get_device(handle, &device);
	if (result) {
665
		printk(KERN_ERR PREFIX "Invalid link device\n");
666
		return -1;
667
	}
L
Linus Torvalds 已提交
668

669
	link = acpi_driver_data(device);
670
	if (!link) {
671
		printk(KERN_ERR PREFIX "Invalid link context\n");
672
		return -1;
673 674
	}

I
Ingo Molnar 已提交
675
	mutex_lock(&acpi_link_lock);
676
	if (!link->irq.initialized) {
I
Ingo Molnar 已提交
677
		mutex_unlock(&acpi_link_lock);
678
		printk(KERN_ERR PREFIX "Link isn't initialized\n");
679
		return -1;
680
	}
681 682 683 684 685 686 687 688 689 690
#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 已提交
691
	link->refcnt--;
692
#endif
693
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
694 695
			  "Link %s is dereferenced\n",
			  acpi_device_bid(link->device)));
696 697

	if (link->refcnt == 0) {
698
		acpi_ut_evaluate_object(link->device->handle, "_DIS", 0, NULL);
699
	}
I
Ingo Molnar 已提交
700
	mutex_unlock(&acpi_link_lock);
701
	return (link->irq.active);
702
}
L
Len Brown 已提交
703

L
Linus Torvalds 已提交
704 705 706 707
/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
708
static int acpi_pci_link_add(struct acpi_device *device)
L
Linus Torvalds 已提交
709
{
L
Len Brown 已提交
710 711 712 713
	int result = 0;
	struct acpi_pci_link *link = NULL;
	int i = 0;
	int found = 0;
L
Linus Torvalds 已提交
714 715 716


	if (!device)
717
		return -EINVAL;
L
Linus Torvalds 已提交
718

719
	link = kzalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
L
Linus Torvalds 已提交
720
	if (!link)
721
		return -ENOMEM;
L
Linus Torvalds 已提交
722 723 724 725 726 727

	link->device = device;
	strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
	acpi_driver_data(device) = link;

I
Ingo Molnar 已提交
728
	mutex_lock(&acpi_link_lock);
L
Linus Torvalds 已提交
729 730 731 732 733 734 735 736
	result = acpi_pci_link_get_possible(link);
	if (result)
		goto end;

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

	printk(PREFIX "%s [%s] (IRQs", acpi_device_name(device),
L
Len Brown 已提交
737
	       acpi_device_bid(device));
L
Linus Torvalds 已提交
738 739 740 741
	for (i = 0; i < link->irq.possible_count; i++) {
		if (link->irq.active == link->irq.possible[i]) {
			printk(" *%d", link->irq.possible[i]);
			found = 1;
L
Len Brown 已提交
742
		} else
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750
			printk(" %d", link->irq.possible[i]);
	}

	printk(")");

	if (!found)
		printk(" *%d", link->irq.active);

L
Len Brown 已提交
751
	if (!link->device->status.enabled)
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759
		printk(", disabled.");

	printk("\n");

	/* TBD: Acquire/release lock */
	list_add_tail(&link->node, &acpi_link.entries);
	acpi_link.count++;

L
Len Brown 已提交
760
      end:
L
Linus Torvalds 已提交
761
	/* disable all links -- to be activated on use */
762
	acpi_ut_evaluate_object(device->handle, "_DIS", 0, NULL);
I
Ingo Molnar 已提交
763
	mutex_unlock(&acpi_link_lock);
L
Linus Torvalds 已提交
764 765 766 767

	if (result)
		kfree(link);

768
	return result;
L
Linus Torvalds 已提交
769 770
}

L
Len Brown 已提交
771
static int acpi_pci_link_resume(struct acpi_pci_link *link)
772 773 774
{

	if (link->refcnt && link->irq.active && link->irq.initialized)
775
		return (acpi_pci_link_set(link, link->irq.active));
776
	else
777
		return 0;
778 779
}

L
Len Brown 已提交
780
static int irqrouter_resume(struct sys_device *dev)
L
Linus Torvalds 已提交
781
{
L
Len Brown 已提交
782 783
	struct list_head *node = NULL;
	struct acpi_pci_link *link = NULL;
L
Linus Torvalds 已提交
784 785


786
	/* Make sure SCI is enabled again (Apple firmware bug?) */
787
	acpi_set_register(ACPI_BITREG_SCI_ENABLE, 1);
788

L
Linus Torvalds 已提交
789 790 791
	list_for_each(node, &acpi_link.entries) {
		link = list_entry(node, struct acpi_pci_link, node);
		if (!link) {
792
			printk(KERN_ERR PREFIX "Invalid link context\n");
L
Linus Torvalds 已提交
793 794
			continue;
		}
795
		acpi_pci_link_resume(link);
L
Linus Torvalds 已提交
796
	}
797
	return 0;
L
Linus Torvalds 已提交
798 799
}

L
Len Brown 已提交
800
static int acpi_pci_link_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
801 802 803 804 805
{
	struct acpi_pci_link *link = NULL;


	if (!device || !acpi_driver_data(device))
806
		return -EINVAL;
L
Linus Torvalds 已提交
807

808
	link = acpi_driver_data(device);
L
Linus Torvalds 已提交
809

I
Ingo Molnar 已提交
810
	mutex_lock(&acpi_link_lock);
L
Linus Torvalds 已提交
811
	list_del(&link->node);
I
Ingo Molnar 已提交
812
	mutex_unlock(&acpi_link_lock);
L
Linus Torvalds 已提交
813 814 815

	kfree(link);

816
	return 0;
L
Linus Torvalds 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829
}

/*
 * modify acpi_irq_penalty[] from cmdline
 */
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 已提交
830
		retval = get_option(&str, &irq);
L
Linus Torvalds 已提交
831 832 833 834 835 836

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

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

L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
		if (irq >= ACPI_MAX_IRQS)
			continue;

		if (used)
			acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
		else
			acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;

		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 已提交
859
void acpi_penalize_isa_irq(int irq, int active)
L
Linus Torvalds 已提交
860
{
D
David Shaohua Li 已提交
861 862 863 864
	if (active)
		acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
	else
		acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875
}

/*
 * 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 已提交
876

L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884 885 886 887
__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 已提交
888

L
Linus Torvalds 已提交
889 890 891 892 893 894 895
__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 已提交
896

L
Linus Torvalds 已提交
897 898 899 900 901 902 903 904
__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);

int __init acpi_irq_balance_set(char *str)
{
	acpi_irq_balance = 1;
	return 1;
}

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

907
/* FIXME: we will remove this interface after all drivers call pci_disable_device */
L
Linus Torvalds 已提交
908
static struct sysdev_class irqrouter_sysdev_class = {
L
Len Brown 已提交
909 910
	set_kset_name("irqrouter"),
	.resume = irqrouter_resume,
L
Linus Torvalds 已提交
911 912 913
};

static struct sys_device device_irqrouter = {
L
Len Brown 已提交
914 915
	.id = 0,
	.cls = &irqrouter_sysdev_class,
L
Linus Torvalds 已提交
916 917 918 919 920 921 922 923
};

static int __init irqrouter_init_sysfs(void)
{
	int error;


	if (acpi_disabled || acpi_noirq)
924
		return 0;
L
Linus Torvalds 已提交
925 926 927 928 929

	error = sysdev_class_register(&irqrouter_sysdev_class);
	if (!error)
		error = sysdev_register(&device_irqrouter);

930
	return error;
L
Len Brown 已提交
931
}
L
Linus Torvalds 已提交
932 933 934

device_initcall(irqrouter_init_sysfs);

L
Len Brown 已提交
935
static int __init acpi_pci_link_init(void)
L
Linus Torvalds 已提交
936 937 938
{

	if (acpi_noirq)
939
		return 0;
L
Linus Torvalds 已提交
940 941 942 943 944

	acpi_link.count = 0;
	INIT_LIST_HEAD(&acpi_link.entries);

	if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
945
		return -ENODEV;
L
Linus Torvalds 已提交
946

947
	return 0;
L
Linus Torvalds 已提交
948 949 950
}

subsys_initcall(acpi_pci_link_init);