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