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