pci_link.c 25.2 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 41 42 43 44 45
/*
 *  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>

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

#define _COMPONENT		ACPI_PCI_COMPONENT
L
Len Brown 已提交
46
ACPI_MODULE_NAME("pci_link")
L
Linus Torvalds 已提交
47 48 49 50 51 52 53
#define ACPI_PCI_LINK_CLASS		"pci_irq_routing"
#define ACPI_PCI_LINK_HID		"PNP0C0F"
#define ACPI_PCI_LINK_DRIVER_NAME	"ACPI PCI Interrupt Link Driver"
#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 59 60 61 62 63 64
	.name = ACPI_PCI_LINK_DRIVER_NAME,
	.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 73 74 75 76 77 78 79
	u8 active;		/* Current IRQ */
	u8 edge_level;		/* All IRQs */
	u8 active_high_low;	/* All IRQs */
	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 85
	struct list_head node;
	struct acpi_device *device;
	acpi_handle handle;
L
Linus Torvalds 已提交
86
	struct acpi_pci_link_irq irq;
L
Len Brown 已提交
87
	int refcnt;
L
Linus Torvalds 已提交
88 89 90
};

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

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

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

	ACPI_FUNCTION_TRACE("acpi_pci_link_check_possible");

111
	switch (resource->type) {
L
Linus Torvalds 已提交
112 113 114
	case ACPI_RSTYPE_START_DPF:
		return_ACPI_STATUS(AE_OK);
	case ACPI_RSTYPE_IRQ:
L
Len Brown 已提交
115 116 117 118 119 120
		{
			struct acpi_resource_irq *p = &resource->data.irq;
			if (!p || !p->number_of_interrupts) {
				ACPI_DEBUG_PRINT((ACPI_DB_WARN,
						  "Blank IRQ resource\n"));
				return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
121
			}
L
Len Brown 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
			for (i = 0;
			     (i < p->number_of_interrupts
			      && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
				if (!p->interrupts[i]) {
					ACPI_DEBUG_PRINT((ACPI_DB_WARN,
							  "Invalid IRQ %d\n",
							  p->interrupts[i]));
					continue;
				}
				link->irq.possible[i] = p->interrupts[i];
				link->irq.possible_count++;
			}
			link->irq.edge_level = p->edge_level;
			link->irq.active_high_low = p->active_high_low;
			link->irq.resource_type = ACPI_RSTYPE_IRQ;
			break;
L
Linus Torvalds 已提交
138 139
		}
	case ACPI_RSTYPE_EXT_IRQ:
L
Len Brown 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		{
			struct acpi_resource_ext_irq *p =
			    &resource->data.extended_irq;
			if (!p || !p->number_of_interrupts) {
				ACPI_DEBUG_PRINT((ACPI_DB_WARN,
						  "Blank EXT IRQ resource\n"));
				return_ACPI_STATUS(AE_OK);
			}
			for (i = 0;
			     (i < p->number_of_interrupts
			      && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
				if (!p->interrupts[i]) {
					ACPI_DEBUG_PRINT((ACPI_DB_WARN,
							  "Invalid IRQ %d\n",
							  p->interrupts[i]));
					continue;
				}
				link->irq.possible[i] = p->interrupts[i];
				link->irq.possible_count++;
L
Linus Torvalds 已提交
159
			}
L
Len Brown 已提交
160 161 162 163
			link->irq.edge_level = p->edge_level;
			link->irq.active_high_low = p->active_high_low;
			link->irq.resource_type = ACPI_RSTYPE_EXT_IRQ;
			break;
L
Linus Torvalds 已提交
164 165
		}
	default:
L
Len Brown 已提交
166 167
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
				  "Resource is not an IRQ entry\n"));
L
Linus Torvalds 已提交
168 169 170 171 172 173
		return_ACPI_STATUS(AE_OK);
	}

	return_ACPI_STATUS(AE_CTRL_TERMINATE);
}

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

	ACPI_FUNCTION_TRACE("acpi_pci_link_get_possible");

	if (!link)
		return_VALUE(-EINVAL);

	status = acpi_walk_resources(link->handle, METHOD_NAME__PRS,
L
Len Brown 已提交
184
				     acpi_pci_link_check_possible, link);
L
Linus Torvalds 已提交
185 186 187 188 189
	if (ACPI_FAILURE(status)) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRS\n"));
		return_VALUE(-ENODEV);
	}

L
Len Brown 已提交
190 191 192
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
			  "Found %d possible IRQs\n",
			  link->irq.possible_count));
L
Linus Torvalds 已提交
193 194 195 196 197

	return_VALUE(0);
}

static acpi_status
L
Len Brown 已提交
198
acpi_pci_link_check_current(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
199
{
L
Len Brown 已提交
200
	int *irq = (int *)context;
L
Linus Torvalds 已提交
201 202 203

	ACPI_FUNCTION_TRACE("acpi_pci_link_check_current");

204
	switch (resource->type) {
L
Linus Torvalds 已提交
205
	case ACPI_RSTYPE_IRQ:
L
Len Brown 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218
		{
			struct acpi_resource_irq *p = &resource->data.irq;
			if (!p || !p->number_of_interrupts) {
				/*
				 * IRQ descriptors may have no IRQ# bits set,
				 * particularly those those w/ _STA disabled
				 */
				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
						  "Blank IRQ resource\n"));
				return_ACPI_STATUS(AE_OK);
			}
			*irq = p->interrupts[0];
			break;
L
Linus Torvalds 已提交
219 220
		}
	case ACPI_RSTYPE_EXT_IRQ:
L
Len Brown 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234
		{
			struct acpi_resource_ext_irq *p =
			    &resource->data.extended_irq;
			if (!p || !p->number_of_interrupts) {
				/*
				 * extended IRQ descriptors must
				 * return at least 1 IRQ
				 */
				ACPI_DEBUG_PRINT((ACPI_DB_WARN,
						  "Blank EXT IRQ resource\n"));
				return_ACPI_STATUS(AE_OK);
			}
			*irq = p->interrupts[0];
			break;
L
Linus Torvalds 已提交
235 236
		}
	default:
L
Len Brown 已提交
237
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Resource isn't an IRQ\n"));
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249
		return_ACPI_STATUS(AE_OK);
	}
	return_ACPI_STATUS(AE_CTRL_TERMINATE);
}

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

	ACPI_FUNCTION_TRACE("acpi_pci_link_get_current");

	if (!link || !link->handle)
		return_VALUE(-EINVAL);

	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) {
L
Len Brown 已提交
268 269
			ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
					  "Unable to read status\n"));
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283
			goto end;
		}

		if (!link->device->status.enabled) {
			ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
			return_VALUE(0);
		}
	}

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

	status = acpi_walk_resources(link->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
284
				     acpi_pci_link_check_current, &irq);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	if (ACPI_FAILURE(status)) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _CRS\n"));
		result = -ENODEV;
		goto end;
	}

	if (acpi_strict && !irq) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_CRS returned 0\n"));
		result = -ENODEV;
	}

	link->irq.active = irq;

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

L
Len Brown 已提交
300
      end:
L
Linus Torvalds 已提交
301 302 303
	return_VALUE(result);
}

L
Len Brown 已提交
304
static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
L
Linus Torvalds 已提交
305
{
L
Len Brown 已提交
306 307
	int result = 0;
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
308
	struct {
L
Len Brown 已提交
309 310 311 312
		struct acpi_resource res;
		struct acpi_resource end;
	} *resource;
	struct acpi_buffer buffer = { 0, NULL };
L
Linus Torvalds 已提交
313 314 315 316 317 318

	ACPI_FUNCTION_TRACE("acpi_pci_link_set");

	if (!link || !irq)
		return_VALUE(-EINVAL);

L
Len Brown 已提交
319 320
	resource = kmalloc(sizeof(*resource) + 1, GFP_KERNEL);
	if (!resource)
L
Linus Torvalds 已提交
321 322
		return_VALUE(-ENOMEM);

L
Len Brown 已提交
323 324
	memset(resource, 0, sizeof(*resource) + 1);
	buffer.length = sizeof(*resource) + 1;
L
Linus Torvalds 已提交
325 326
	buffer.pointer = resource;

L
Len Brown 已提交
327
	switch (link->irq.resource_type) {
L
Linus Torvalds 已提交
328
	case ACPI_RSTYPE_IRQ:
329
		resource->res.type = ACPI_RSTYPE_IRQ;
L
Linus Torvalds 已提交
330 331
		resource->res.length = sizeof(struct acpi_resource);
		resource->res.data.irq.edge_level = link->irq.edge_level;
L
Len Brown 已提交
332 333
		resource->res.data.irq.active_high_low =
		    link->irq.active_high_low;
L
Linus Torvalds 已提交
334
		if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
L
Len Brown 已提交
335 336
			resource->res.data.irq.shared_exclusive =
			    ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
337 338 339 340 341
		else
			resource->res.data.irq.shared_exclusive = ACPI_SHARED;
		resource->res.data.irq.number_of_interrupts = 1;
		resource->res.data.irq.interrupts[0] = irq;
		break;
L
Len Brown 已提交
342

L
Linus Torvalds 已提交
343
	case ACPI_RSTYPE_EXT_IRQ:
344
		resource->res.type = ACPI_RSTYPE_EXT_IRQ;
L
Linus Torvalds 已提交
345
		resource->res.length = sizeof(struct acpi_resource);
L
Len Brown 已提交
346 347 348 349 350 351
		resource->res.data.extended_irq.producer_consumer =
		    ACPI_CONSUMER;
		resource->res.data.extended_irq.edge_level =
		    link->irq.edge_level;
		resource->res.data.extended_irq.active_high_low =
		    link->irq.active_high_low;
L
Linus Torvalds 已提交
352
		if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
L
Len Brown 已提交
353 354
			resource->res.data.irq.shared_exclusive =
			    ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
355 356 357 358 359 360 361 362 363 364 365 366
		else
			resource->res.data.irq.shared_exclusive = ACPI_SHARED;
		resource->res.data.extended_irq.number_of_interrupts = 1;
		resource->res.data.extended_irq.interrupts[0] = irq;
		/* ignore resource_source, it's optional */
		break;
	default:
		printk("ACPI BUG: resource_type %d\n", link->irq.resource_type);
		result = -EINVAL;
		goto end;

	}
367
	resource->end.type = ACPI_RSTYPE_END_TAG;
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

	/* Attempt to set the resource */
	status = acpi_set_current_resources(link->handle, &buffer);

	/* check for total failure */
	if (ACPI_FAILURE(status)) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SRS\n"));
		result = -ENODEV;
		goto end;
	}

	/* Query _STA, set device->status */
	result = acpi_bus_get_status(link->device);
	if (result) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to read status\n"));
		goto end;
	}
	if (!link->device->status.enabled) {
		printk(KERN_WARNING PREFIX
L
Len Brown 已提交
387 388 389
		       "%s [%s] disabled and referenced, BIOS bug.\n",
		       acpi_device_name(link->device),
		       acpi_device_bid(link->device));
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	}

	/* 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.
		 */
L
Len Brown 已提交
407 408 409 410
		printk(KERN_WARNING PREFIX
		       "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
		       acpi_device_name(link->device),
		       acpi_device_bid(link->device), link->irq.active, irq);
L
Linus Torvalds 已提交
411 412 413 414
		link->irq.active = irq;
	}

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

      end:
L
Linus Torvalds 已提交
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 463 464 465 466 467 468 469
	kfree(resource);
	return_VALUE(result);
}

/* --------------------------------------------------------------------------
                            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 已提交
470 471
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ3 serial */
	PIRQ_PENALTY_ISA_TYPICAL,	/* IRQ4 serial */
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479 480 481 482
	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 已提交
483
	/* >IRQ15 */
L
Linus Torvalds 已提交
484 485
};

L
Len Brown 已提交
486
int __init acpi_irq_penalty_init(void)
L
Linus Torvalds 已提交
487
{
L
Len Brown 已提交
488 489 490
	struct list_head *node = NULL;
	struct acpi_pci_link *link = NULL;
	int i = 0;
L
Linus Torvalds 已提交
491 492 493 494 495 496 497 498 499 500

	ACPI_FUNCTION_TRACE("acpi_irq_penalty_init");

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

		link = list_entry(node, struct acpi_pci_link, node);
		if (!link) {
L
Len Brown 已提交
501 502
			ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
					  "Invalid link context\n"));
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510
			continue;
		}

		/*
		 * reflect the possible and active irqs in the penalty table --
		 * useful for breaking ties.
		 */
		if (link->irq.possible_count) {
L
Len Brown 已提交
511 512 513
			int penalty =
			    PIRQ_PENALTY_PCI_POSSIBLE /
			    link->irq.possible_count;
L
Linus Torvalds 已提交
514 515 516

			for (i = 0; i < link->irq.possible_count; i++) {
				if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
L
Len Brown 已提交
517 518 519
					acpi_irq_penalty[link->irq.
							 possible[i]] +=
					    penalty;
L
Linus Torvalds 已提交
520 521 522
			}

		} else if (link->irq.active) {
L
Len Brown 已提交
523 524
			acpi_irq_penalty[link->irq.active] +=
			    PIRQ_PENALTY_PCI_POSSIBLE;
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532 533 534
		}
	}
	/* Add a penalty for the SCI */
	acpi_irq_penalty[acpi_fadt.sci_int] += PIRQ_PENALTY_PCI_USING;

	return_VALUE(0);
}

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

L
Len Brown 已提交
535
static int acpi_pci_link_allocate(struct acpi_pci_link *link)
L
Linus Torvalds 已提交
536
{
L
Len Brown 已提交
537 538
	int irq;
	int i;
L
Linus Torvalds 已提交
539 540 541

	ACPI_FUNCTION_TRACE("acpi_pci_link_allocate");

542 543 544 545
	if (link->irq.initialized) {
		if (link->refcnt == 0)
			/* This means the link is disabled but initialized */
			acpi_pci_link_set(link, link->irq.active);
L
Linus Torvalds 已提交
546
		return_VALUE(0);
547
	}
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561

	/*
	 * 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)
			printk(KERN_WARNING PREFIX "_CRS %d not found"
L
Len Brown 已提交
562
			       " in _PRS\n", link->irq.active);
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
		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 已提交
581 582
			if (acpi_irq_penalty[irq] >
			    acpi_irq_penalty[link->irq.possible[i]])
L
Linus Torvalds 已提交
583 584 585 586 587 588
				irq = link->irq.possible[i];
		}
	}

	/* Attempt to enable the link device at this IRQ. */
	if (acpi_pci_link_set(link, irq)) {
L
Len Brown 已提交
589 590 591 592 593
		printk(PREFIX
		       "Unable to set IRQ for %s [%s] (likely buggy ACPI BIOS).\n"
		       "Try pci=noacpi or acpi=off\n",
		       acpi_device_name(link->device),
		       acpi_device_bid(link->device));
L
Linus Torvalds 已提交
594 595 596
		return_VALUE(-ENODEV);
	} else {
		acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
L
Len Brown 已提交
597 598 599
		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 已提交
600 601 602 603 604 605 606 607
	}

	link->irq.initialized = 1;

	return_VALUE(0);
}

/*
608
 * acpi_pci_link_allocate_irq
L
Linus Torvalds 已提交
609 610 611 612 613
 * success: return IRQ >= 0
 * failure: return -1
 */

int
L
Len Brown 已提交
614 615 616
acpi_pci_link_allocate_irq(acpi_handle handle,
			   int index,
			   int *edge_level, int *active_high_low, char **name)
L
Linus Torvalds 已提交
617
{
L
Len Brown 已提交
618 619 620
	int result = 0;
	struct acpi_device *device = NULL;
	struct acpi_pci_link *link = NULL;
L
Linus Torvalds 已提交
621

622
	ACPI_FUNCTION_TRACE("acpi_pci_link_allocate_irq");
L
Linus Torvalds 已提交
623 624 625 626 627 628 629

	result = acpi_bus_get_device(handle, &device);
	if (result) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
		return_VALUE(-1);
	}

L
Len Brown 已提交
630
	link = (struct acpi_pci_link *)acpi_driver_data(device);
L
Linus Torvalds 已提交
631 632 633 634 635 636 637 638 639 640 641
	if (!link) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
		return_VALUE(-1);
	}

	/* TBD: Support multiple index (IRQ) entries per Link Device */
	if (index) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid index %d\n", index));
		return_VALUE(-1);
	}

642 643 644
	down(&acpi_link_lock);
	if (acpi_pci_link_allocate(link)) {
		up(&acpi_link_lock);
L
Linus Torvalds 已提交
645
		return_VALUE(-1);
646
	}
L
Len Brown 已提交
647

L
Linus Torvalds 已提交
648
	if (!link->irq.active) {
649
		up(&acpi_link_lock);
L
Linus Torvalds 已提交
650 651 652
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link active IRQ is 0!\n"));
		return_VALUE(-1);
	}
L
Len Brown 已提交
653
	link->refcnt++;
654
	up(&acpi_link_lock);
L
Linus Torvalds 已提交
655

L
Len Brown 已提交
656 657 658 659 660 661
	if (edge_level)
		*edge_level = link->irq.edge_level;
	if (active_high_low)
		*active_high_low = link->irq.active_high_low;
	if (name)
		*name = acpi_device_bid(link->device);
662
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
663 664
			  "Link %s is referenced\n",
			  acpi_device_bid(link->device)));
L
Linus Torvalds 已提交
665 666 667
	return_VALUE(link->irq.active);
}

668 669 670 671
/*
 * We don't change link's irq information here.  After it is reenabled, we
 * continue use the info
 */
L
Len Brown 已提交
672
int acpi_pci_link_free_irq(acpi_handle handle)
673
{
L
Len Brown 已提交
674 675 676
	struct acpi_device *device = NULL;
	struct acpi_pci_link *link = NULL;
	acpi_status result;
677 678 679 680 681 682 683 684

	ACPI_FUNCTION_TRACE("acpi_pci_link_free_irq");

	result = acpi_bus_get_device(handle, &device);
	if (result) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
		return_VALUE(-1);
	}
L
Linus Torvalds 已提交
685

L
Len Brown 已提交
686
	link = (struct acpi_pci_link *)acpi_driver_data(device);
687 688 689 690 691 692 693 694 695 696 697
	if (!link) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
		return_VALUE(-1);
	}

	down(&acpi_link_lock);
	if (!link->irq.initialized) {
		up(&acpi_link_lock);
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link isn't initialized\n"));
		return_VALUE(-1);
	}
698 699 700 701 702 703 704 705 706 707
#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 已提交
708
	link->refcnt--;
709
#endif
710
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
711 712
			  "Link %s is dereferenced\n",
			  acpi_device_bid(link->device)));
713 714 715 716 717 718 719

	if (link->refcnt == 0) {
		acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
	}
	up(&acpi_link_lock);
	return_VALUE(link->irq.active);
}
L
Len Brown 已提交
720

L
Linus Torvalds 已提交
721 722 723 724
/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
725
static int acpi_pci_link_add(struct acpi_device *device)
L
Linus Torvalds 已提交
726
{
L
Len Brown 已提交
727 728 729 730
	int result = 0;
	struct acpi_pci_link *link = NULL;
	int i = 0;
	int found = 0;
L
Linus Torvalds 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

	ACPI_FUNCTION_TRACE("acpi_pci_link_add");

	if (!device)
		return_VALUE(-EINVAL);

	link = kmalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
	if (!link)
		return_VALUE(-ENOMEM);
	memset(link, 0, sizeof(struct acpi_pci_link));

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

748
	down(&acpi_link_lock);
L
Linus Torvalds 已提交
749 750 751 752 753 754 755 756
	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 已提交
757
	       acpi_device_bid(device));
L
Linus Torvalds 已提交
758 759 760 761
	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 已提交
762
		} else
L
Linus Torvalds 已提交
763 764 765 766 767 768 769 770
			printk(" %d", link->irq.possible[i]);
	}

	printk(")");

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

L
Len Brown 已提交
771
	if (!link->device->status.enabled)
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779
		printk(", disabled.");

	printk("\n");

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

L
Len Brown 已提交
780
      end:
L
Linus Torvalds 已提交
781 782
	/* disable all links -- to be activated on use */
	acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
783
	up(&acpi_link_lock);
L
Linus Torvalds 已提交
784 785 786 787 788 789 790

	if (result)
		kfree(link);

	return_VALUE(result);
}

L
Len Brown 已提交
791
static int acpi_pci_link_resume(struct acpi_pci_link *link)
792 793 794 795 796 797 798 799 800
{
	ACPI_FUNCTION_TRACE("acpi_pci_link_resume");

	if (link->refcnt && link->irq.active && link->irq.initialized)
		return_VALUE(acpi_pci_link_set(link, link->irq.active));
	else
		return_VALUE(0);
}

801 802 803 804 805
/*
 * FIXME: this is a workaround to avoid nasty warning.  It will be removed
 * after every device calls pci_disable_device in .resume.
 */
int acpi_in_resume;
L
Len Brown 已提交
806
static int irqrouter_resume(struct sys_device *dev)
L
Linus Torvalds 已提交
807
{
L
Len Brown 已提交
808 809
	struct list_head *node = NULL;
	struct acpi_pci_link *link = NULL;
L
Linus Torvalds 已提交
810

811
	ACPI_FUNCTION_TRACE("irqrouter_resume");
L
Linus Torvalds 已提交
812

813
	acpi_in_resume = 1;
L
Linus Torvalds 已提交
814 815 816
	list_for_each(node, &acpi_link.entries) {
		link = list_entry(node, struct acpi_pci_link, node);
		if (!link) {
817
			ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
L
Len Brown 已提交
818
					  "Invalid link context\n"));
L
Linus Torvalds 已提交
819 820
			continue;
		}
821
		acpi_pci_link_resume(link);
L
Linus Torvalds 已提交
822
	}
823
	acpi_in_resume = 0;
824
	return_VALUE(0);
L
Linus Torvalds 已提交
825 826
}

L
Len Brown 已提交
827
static int acpi_pci_link_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835
{
	struct acpi_pci_link *link = NULL;

	ACPI_FUNCTION_TRACE("acpi_pci_link_remove");

	if (!device || !acpi_driver_data(device))
		return_VALUE(-EINVAL);

L
Len Brown 已提交
836
	link = (struct acpi_pci_link *)acpi_driver_data(device);
L
Linus Torvalds 已提交
837

838
	down(&acpi_link_lock);
L
Linus Torvalds 已提交
839
	list_del(&link->node);
840
	up(&acpi_link_lock);
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

	kfree(link);

	return_VALUE(0);
}

/*
 * 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 已提交
858
		retval = get_option(&str, &irq);
L
Linus Torvalds 已提交
859 860 861 862 863 864

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

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

L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
		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 已提交
887
void acpi_penalize_isa_irq(int irq, int active)
L
Linus Torvalds 已提交
888
{
D
David Shaohua Li 已提交
889 890 891 892
	if (active)
		acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
	else
		acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903
}

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

L
Linus Torvalds 已提交
905 906 907 908 909 910 911 912 913 914 915
__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 已提交
916

L
Linus Torvalds 已提交
917 918 919 920 921 922 923
__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 已提交
924

L
Linus Torvalds 已提交
925 926 927 928 929 930 931 932
__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 已提交
933
__setup("acpi_irq_balance", acpi_irq_balance_set);
L
Linus Torvalds 已提交
934

935
/* FIXME: we will remove this interface after all drivers call pci_disable_device */
L
Linus Torvalds 已提交
936
static struct sysdev_class irqrouter_sysdev_class = {
L
Len Brown 已提交
937 938
	set_kset_name("irqrouter"),
	.resume = irqrouter_resume,
L
Linus Torvalds 已提交
939 940 941
};

static struct sys_device device_irqrouter = {
L
Len Brown 已提交
942 943
	.id = 0,
	.cls = &irqrouter_sysdev_class,
L
Linus Torvalds 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
};

static int __init irqrouter_init_sysfs(void)
{
	int error;

	ACPI_FUNCTION_TRACE("irqrouter_init_sysfs");

	if (acpi_disabled || acpi_noirq)
		return_VALUE(0);

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

	return_VALUE(error);
L
Len Brown 已提交
960
}
L
Linus Torvalds 已提交
961 962 963

device_initcall(irqrouter_init_sysfs);

L
Len Brown 已提交
964
static int __init acpi_pci_link_init(void)
L
Linus Torvalds 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
{
	ACPI_FUNCTION_TRACE("acpi_pci_link_init");

	if (acpi_noirq)
		return_VALUE(0);

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

	if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
		return_VALUE(-ENODEV);

	return_VALUE(0);
}

subsys_initcall(acpi_pci_link_init);