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