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