pci_link.c 23.4 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

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

46 47
#define PREFIX "ACPI: "

48
#define _COMPONENT			ACPI_PCI_COMPONENT
49
ACPI_MODULE_NAME("pci_link");
L
Linus Torvalds 已提交
50 51 52 53
#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"
54 55
#define ACPI_PCI_LINK_MAX_POSSIBLE	16

L
Len Brown 已提交
56 57
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 已提交
58

59
static const struct acpi_device_id link_device_ids[] = {
60 61 62 63 64
	{"PNP0C0F", 0},
	{"", 0},
};
MODULE_DEVICE_TABLE(acpi, link_device_ids);

L
Linus Torvalds 已提交
65
static struct acpi_driver acpi_pci_link_driver = {
L
Len Brown 已提交
66
	.name = "pci_link",
L
Len Brown 已提交
67
	.class = ACPI_PCI_LINK_CLASS,
68
	.ids = link_device_ids,
L
Len Brown 已提交
69 70 71
	.ops = {
		.add = acpi_pci_link_add,
		.remove = acpi_pci_link_remove,
72
	},
L
Linus Torvalds 已提交
73 74
};

75 76 77 78
/*
 * 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 已提交
79
struct acpi_pci_link_irq {
L
Len Brown 已提交
80
	u8 active;		/* Current IRQ */
B
Bob Moore 已提交
81
	u8 triggering;		/* All IRQs */
82
	u8 polarity;		/* All IRQs */
L
Len Brown 已提交
83 84 85 86 87
	u8 resource_type;
	u8 possible_count;
	u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
	u8 initialized:1;
	u8 reserved:7;
L
Linus Torvalds 已提交
88 89 90
};

struct acpi_pci_link {
91
	struct list_head		list;
92 93 94
	struct acpi_device		*device;
	struct acpi_pci_link_irq	irq;
	int				refcnt;
L
Linus Torvalds 已提交
95 96
};

97
static LIST_HEAD(acpi_link_list);
A
Adrian Bunk 已提交
98
static DEFINE_MUTEX(acpi_link_lock);
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106

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

/*
 * set context (link) possible list from resource list
 */
107 108
static acpi_status acpi_pci_link_check_possible(struct acpi_resource *resource,
						void *context)
L
Linus Torvalds 已提交
109
{
110
	struct acpi_pci_link *link = context;
111
	u32 i;
L
Linus Torvalds 已提交
112

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

174
	return AE_CTRL_TERMINATE;
L
Linus Torvalds 已提交
175 176
}

L
Len Brown 已提交
177
static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
178
{
L
Len Brown 已提交
179
	acpi_status status;
L
Linus Torvalds 已提交
180

181
	status = acpi_walk_resources(link->device->handle, METHOD_NAME__PRS,
L
Len Brown 已提交
182
				     acpi_pci_link_check_possible, link);
L
Linus Torvalds 已提交
183
	if (ACPI_FAILURE(status)) {
184
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRS"));
185
		return -ENODEV;
L
Linus Torvalds 已提交
186 187
	}

L
Len Brown 已提交
188 189 190
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
			  "Found %d possible IRQs\n",
			  link->irq.possible_count));
L
Linus Torvalds 已提交
191

192
	return 0;
L
Linus Torvalds 已提交
193 194
}

195 196
static acpi_status acpi_pci_link_check_current(struct acpi_resource *resource,
					       void *context)
L
Linus Torvalds 已提交
197
{
198
	int *irq = context;
L
Linus Torvalds 已提交
199

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

242
	return AE_CTRL_TERMINATE;
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251
}

/*
 * Run _CRS and set link->irq.active
 *
 * return value:
 * 0 - success
 * !0 - failure
 */
L
Len Brown 已提交
252
static int acpi_pci_link_get_current(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
253
{
L
Len Brown 已提交
254
	int result = 0;
255
	acpi_status status;
L
Len Brown 已提交
256
	int irq = 0;
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264

	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) {
265
			printk(KERN_ERR PREFIX "Unable to read status\n");
L
Linus Torvalds 已提交
266 267 268 269 270
			goto end;
		}

		if (!link->device->status.enabled) {
			ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
271
			return 0;
L
Linus Torvalds 已提交
272 273 274 275 276 277 278
		}
	}

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

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

	if (acpi_strict && !irq) {
288
		printk(KERN_ERR PREFIX "_CRS returned 0\n");
L
Linus Torvalds 已提交
289 290 291 292 293 294 295
		result = -ENODEV;
	}

	link->irq.active = irq;

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

L
Len Brown 已提交
296
      end:
297
	return result;
L
Linus Torvalds 已提交
298 299
}

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

310
	if (!irq)
311
		return -EINVAL;
L
Linus Torvalds 已提交
312

313
	resource = kzalloc(sizeof(*resource) + 1, irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
L
Len Brown 已提交
314
	if (!resource)
315
		return -ENOMEM;
L
Linus Torvalds 已提交
316

L
Len Brown 已提交
317
	buffer.length = sizeof(*resource) + 1;
L
Linus Torvalds 已提交
318 319
	buffer.pointer = resource;

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

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

	}
B
Bob Moore 已提交
360
	resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
L
Linus Torvalds 已提交
361 362

	/* Attempt to set the resource */
363
	status = acpi_set_current_resources(link->device->handle, &buffer);
L
Linus Torvalds 已提交
364 365 366

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

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

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

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

      end:
L
Linus Torvalds 已提交
410
	kfree(resource);
411
	return result;
L
Linus Torvalds 已提交
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 459 460 461 462
}

/* --------------------------------------------------------------------------
                            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 已提交
463 464
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ3 serial */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ4 serial */
L
Linus Torvalds 已提交
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 */
472 473 474 475
	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 已提交
476
	/* >IRQ15 */
L
Linus Torvalds 已提交
477 478
};

L
Len Brown 已提交
479
int __init acpi_irq_penalty_init(void)
L
Linus Torvalds 已提交
480
{
481 482
	struct acpi_pci_link *link;
	int i;
L
Linus Torvalds 已提交
483 484 485 486

	/*
	 * Update penalties to facilitate IRQ balancing.
	 */
487
	list_for_each_entry(link, &acpi_link_list, list) {
L
Linus Torvalds 已提交
488 489 490 491 492 493

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

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

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

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

L
Len Brown 已提交
517
static int acpi_pci_link_allocate(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
518
{
L
Len Brown 已提交
519 520
	int irq;
	int i;
L
Linus Torvalds 已提交
521

522 523 524 525
	if (link->irq.initialized) {
		if (link->refcnt == 0)
			/* This means the link is disabled but initialized */
			acpi_pci_link_set(link, link->irq.active);
526
		return 0;
527
	}
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540

	/*
	 * 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)
541 542
			printk(KERN_WARNING PREFIX "_CRS %d not found"
				      " in _PRS\n", link->irq.active);
L
Linus Torvalds 已提交
543 544 545 546 547 548
		link->irq.active = 0;
	}

	/*
	 * if active found, use it; else pick entry from end of possible list.
	 */
549
	if (link->irq.active)
L
Linus Torvalds 已提交
550
		irq = link->irq.active;
551
	else
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559
		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 已提交
560 561
			if (acpi_irq_penalty[irq] >
			    acpi_irq_penalty[link->irq.possible[i]])
L
Linus Torvalds 已提交
562 563 564 565 566 567
				irq = link->irq.possible[i];
		}
	}

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

	link->irq.initialized = 1;
581
	return 0;
L
Linus Torvalds 已提交
582 583 584
}

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

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

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

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

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

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

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

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

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

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

I
Ingo Molnar 已提交
662
	mutex_lock(&acpi_link_lock);
663
	if (!link->irq.initialized) {
I
Ingo Molnar 已提交
664
		mutex_unlock(&acpi_link_lock);
665
		printk(KERN_ERR PREFIX "Link isn't initialized\n");
666
		return -1;
667
	}
668 669 670 671 672 673 674 675 676 677
#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 已提交
678
	link->refcnt--;
679
#endif
680
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
681 682
			  "Link %s is dereferenced\n",
			  acpi_device_bid(link->device)));
683

684
	if (link->refcnt == 0)
685
		acpi_evaluate_object(link->device->handle, "_DIS", NULL, NULL);
686

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

L
Linus Torvalds 已提交
691 692 693 694
/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
695
static int acpi_pci_link_add(struct acpi_device *device)
L
Linus Torvalds 已提交
696
{
697 698 699
	int result;
	struct acpi_pci_link *link;
	int i;
L
Len Brown 已提交
700
	int found = 0;
L
Linus Torvalds 已提交
701

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

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

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

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

719
	printk(KERN_INFO PREFIX "%s [%s] (IRQs", acpi_device_name(device),
L
Len Brown 已提交
720
	       acpi_device_bid(device));
L
Linus Torvalds 已提交
721 722 723 724
	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 已提交
725
		} else
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733
			printk(" %d", link->irq.possible[i]);
	}

	printk(")");

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

L
Len Brown 已提交
734
	if (!link->device->status.enabled)
L
Linus Torvalds 已提交
735 736 737 738
		printk(", disabled.");

	printk("\n");

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

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

	if (result)
		kfree(link);

749
	return result;
L
Linus Torvalds 已提交
750 751
}

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

	return 0;
758 759
}

L
Len Brown 已提交
760
static int irqrouter_resume(struct sys_device *dev)
L
Linus Torvalds 已提交
761
{
762
	struct acpi_pci_link *link;
L
Linus Torvalds 已提交
763

764
	list_for_each_entry(link, &acpi_link_list, list) {
765
		acpi_pci_link_resume(link);
L
Linus Torvalds 已提交
766
	}
767
	return 0;
L
Linus Torvalds 已提交
768 769
}

L
Len Brown 已提交
770
static int acpi_pci_link_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
771
{
772
	struct acpi_pci_link *link;
L
Linus Torvalds 已提交
773

774
	link = acpi_driver_data(device);
L
Linus Torvalds 已提交
775

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

	kfree(link);
781
	return 0;
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794
}

/*
 * 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 已提交
795
		retval = get_option(&str, &irq);
L
Linus Torvalds 已提交
796 797 798 799 800 801

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

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

803
		if (irq >= ARRAY_SIZE(acpi_irq_penalty))
L
Linus Torvalds 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
			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 已提交
824
void acpi_penalize_isa_irq(int irq, int active)
L
Linus Torvalds 已提交
825
{
826 827 828 829 830 831
	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 已提交
832 833 834 835 836 837 838 839 840 841 842
}

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

L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854
__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 已提交
855

L
Linus Torvalds 已提交
856 857 858 859 860 861 862
__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 已提交
863

L
Linus Torvalds 已提交
864 865
__setup("acpi_irq_nobalance", acpi_irq_nobalance_set);

866
static int __init acpi_irq_balance_set(char *str)
L
Linus Torvalds 已提交
867 868 869 870 871
{
	acpi_irq_balance = 1;
	return 1;
}

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

874
/* FIXME: we will remove this interface after all drivers call pci_disable_device */
L
Linus Torvalds 已提交
875
static struct sysdev_class irqrouter_sysdev_class = {
876
	.name = "irqrouter",
L
Len Brown 已提交
877
	.resume = irqrouter_resume,
L
Linus Torvalds 已提交
878 879 880
};

static struct sys_device device_irqrouter = {
L
Len Brown 已提交
881 882
	.id = 0,
	.cls = &irqrouter_sysdev_class,
L
Linus Torvalds 已提交
883 884 885 886 887 888 889
};

static int __init irqrouter_init_sysfs(void)
{
	int error;

	if (acpi_disabled || acpi_noirq)
890
		return 0;
L
Linus Torvalds 已提交
891 892 893 894 895

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

896
	return error;
L
Len Brown 已提交
897
}
L
Linus Torvalds 已提交
898 899 900

device_initcall(irqrouter_init_sysfs);

L
Len Brown 已提交
901
static int __init acpi_pci_link_init(void)
L
Linus Torvalds 已提交
902 903
{
	if (acpi_noirq)
904
		return 0;
L
Linus Torvalds 已提交
905

906 907 908 909 910 911 912 913
	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;
	}

L
Linus Torvalds 已提交
914
	if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
915
		return -ENODEV;
L
Linus Torvalds 已提交
916

917
	return 0;
L
Linus Torvalds 已提交
918 919 920
}

subsys_initcall(acpi_pci_link_init);