Subversion Repositories Kolibri OS

Rev

Rev 1633 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1633 Rev 1867
1
#include 
1
#include 
2
#include 
2
#include 
3
#include 
3
#include 
4
#include 
4
#include 
5
#include 
5
#include 
6
#include 
6
#include 
7
 
7
 
8
#include "acpi.h"
8
#include "acpi.h"
9
#include "acpi_bus.h"
9
#include "acpi_bus.h"
10
 
10
 
11
#define PREFIX "ACPI: "
11
#define PREFIX "ACPI: "
12
 
12
 
13
struct acpi_prt_entry
13
struct acpi_prt_entry
14
{
14
{
15
    struct list_head    list;
15
    struct list_head    list;
16
    ACPI_PCI_ID         id;
16
    ACPI_PCI_ID         id;
17
    u8                  pin;
17
    u8                  pin;
18
    ACPI_HANDLE         link;
18
    ACPI_HANDLE         link;
19
    u32                 index;      /* GSI, or link _CRS index */
19
    u32                 index;      /* GSI, or link _CRS index */
20
};
20
};
21
 
21
 
22
static LIST_HEAD(acpi_prt_list);
22
static LIST_HEAD(acpi_prt_list);
23
static DEFINE_SPINLOCK(acpi_prt_lock);
23
static DEFINE_SPINLOCK(acpi_prt_lock);
24
 
24
 
25
static inline char pin_name(int pin)
25
static inline char pin_name(int pin)
26
{
26
{
27
    return 'A' + pin - 1;
27
    return 'A' + pin - 1;
28
}
28
}
29
 
29
 
30
 
30
 
31
/* --------------------------------------------------------------------------
31
/* --------------------------------------------------------------------------
32
                         PCI IRQ Routing Table (PRT) Support
32
                         PCI IRQ Routing Table (PRT) Support
33
   -------------------------------------------------------------------------- */
33
   -------------------------------------------------------------------------- */
34
 
34
 
35
static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(struct pci_dev *dev,
35
static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(struct pci_dev *dev,
36
                              int pin)
36
                              int pin)
37
{
37
{
38
    struct acpi_prt_entry *entry;
38
    struct acpi_prt_entry *entry;
39
    int segment = pci_domain_nr(dev->bus);
39
    int segment = pci_domain_nr(dev->bus);
40
    int bus = dev->bus->number;
40
    int bus = dev->bus->number;
41
    int device = PCI_SLOT(dev->devfn);
41
    int device = PCI_SLOT(dev->devfn);
42
 
42
 
43
    spin_lock(&acpi_prt_lock);
43
    spin_lock(&acpi_prt_lock);
44
    list_for_each_entry(entry, &acpi_prt_list, list) {
44
    list_for_each_entry(entry, &acpi_prt_list, list) {
45
        if ((segment == entry->id.Segment)
45
        if ((segment == entry->id.Segment)
46
            && (bus == entry->id.Bus)
46
            && (bus == entry->id.Bus)
47
            && (device == entry->id.Device)
47
            && (device == entry->id.Device)
48
            && (pin == entry->pin)) {
48
            && (pin == entry->pin)) {
49
            spin_unlock(&acpi_prt_lock);
49
            spin_unlock(&acpi_prt_lock);
50
            return entry;
50
            return entry;
51
        }
51
        }
52
    }
52
    }
53
    spin_unlock(&acpi_prt_lock);
53
    spin_unlock(&acpi_prt_lock);
54
    return NULL;
54
    return NULL;
55
}
55
}
56
 
56
 
57
static int acpi_pci_irq_add_entry(ACPI_HANDLE handle, struct pci_bus *bus,
57
static int acpi_pci_irq_add_entry(ACPI_HANDLE handle, struct pci_bus *bus,
58
                  struct acpi_pci_routing_table *prt)
58
                  struct acpi_pci_routing_table *prt)
59
{
59
{
60
    struct acpi_prt_entry *entry;
60
    struct acpi_prt_entry *entry;
61
 
61
 
62
    entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
62
    entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
63
    if (!entry)
63
    if (!entry)
64
        return -ENOMEM;
64
        return -ENOMEM;
65
 
65
 
66
    /*
66
    /*
67
     * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
67
     * Note that the _PRT uses 0=INTA, 1=INTB, etc, while PCI uses
68
     * 1=INTA, 2=INTB.  We use the PCI encoding throughout, so convert
68
     * 1=INTA, 2=INTB.  We use the PCI encoding throughout, so convert
69
     * it here.
69
     * it here.
70
     */
70
     */
71
    entry->id.Segment = pci_domain_nr(bus);
71
    entry->id.Segment = pci_domain_nr(bus);
72
    entry->id.Bus = bus->number;
72
    entry->id.Bus = bus->number;
73
    entry->id.Device = (prt->Address >> 16) & 0xFFFF;
73
    entry->id.Device = (prt->Address >> 16) & 0xFFFF;
74
    entry->pin = prt->Pin + 1;
74
    entry->pin = prt->Pin + 1;
75
 
75
 
76
//    do_prt_fixups(entry, prt);
76
//    do_prt_fixups(entry, prt);
77
 
77
 
78
    entry->index = prt->SourceIndex;
78
    entry->index = prt->SourceIndex;
79
 
79
 
80
    /*
80
    /*
81
     * Type 1: Dynamic
81
     * Type 1: Dynamic
82
     * ---------------
82
     * ---------------
83
     * The 'source' field specifies the PCI interrupt link device used to
83
     * The 'source' field specifies the PCI interrupt link device used to
84
     * configure the IRQ assigned to this slot|dev|pin.  The 'source_index'
84
     * configure the IRQ assigned to this slot|dev|pin.  The 'source_index'
85
     * indicates which resource descriptor in the resource template (of
85
     * indicates which resource descriptor in the resource template (of
86
     * the link device) this interrupt is allocated from.
86
     * the link device) this interrupt is allocated from.
87
     *
87
     *
88
     * NOTE: Don't query the Link Device for IRQ information at this time
88
     * NOTE: Don't query the Link Device for IRQ information at this time
89
     *       because Link Device enumeration may not have occurred yet
89
     *       because Link Device enumeration may not have occurred yet
90
     *       (e.g. exists somewhere 'below' this _PRT entry in the ACPI
90
     *       (e.g. exists somewhere 'below' this _PRT entry in the ACPI
91
     *       namespace).
91
     *       namespace).
92
     */
92
     */
93
    if (prt->Source[0])
93
    if (prt->Source[0])
94
        AcpiGetHandle(handle, prt->Source, &entry->link);
94
        AcpiGetHandle(handle, prt->Source, &entry->link);
95
 
95
 
96
    /*
96
    /*
97
     * Type 2: Static
97
     * Type 2: Static
98
     * --------------
98
     * --------------
99
     * The 'source' field is NULL, and the 'source_index' field specifies
99
     * The 'source' field is NULL, and the 'source_index' field specifies
100
     * the IRQ value, which is hardwired to specific interrupt inputs on
100
     * the IRQ value, which is hardwired to specific interrupt inputs on
101
     * the interrupt controller.
101
     * the interrupt controller.
102
     */
102
     */
103
 
103
 
104
    dbgprintf(PREFIX "      %04x:%02x:%02x[%c] -> %s[%d]\n",
104
    dbgprintf(PREFIX "      %04x:%02x:%02x[%c] -> %s[%d]\n",
105
                  entry->id.Segment, entry->id.Bus,
105
                  entry->id.Segment, entry->id.Bus,
106
                  entry->id.Device, pin_name(entry->pin),
106
                  entry->id.Device, pin_name(entry->pin),
107
                  prt->Source, entry->index);
107
                  prt->Source, entry->index);
108
 
108
 
109
    spin_lock(&acpi_prt_lock);
109
    spin_lock(&acpi_prt_lock);
110
    list_add_tail(&entry->list, &acpi_prt_list);
110
    list_add_tail(&entry->list, &acpi_prt_list);
111
    spin_unlock(&acpi_prt_lock);
111
    spin_unlock(&acpi_prt_lock);
112
 
112
 
113
    return 0;
113
    return 0;
114
}
114
}
115
 
115
 
116
 
116
 
117
 
117
 
118
int acpi_pci_irq_add_prt(ACPI_HANDLE handle, struct pci_bus *bus)
118
int acpi_pci_irq_add_prt(ACPI_HANDLE handle, struct pci_bus *bus)
119
{
119
{
120
    ACPI_STATUS status;
120
    ACPI_STATUS status;
121
    struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
121
    struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
122
    struct acpi_pci_routing_table *entry;
122
    struct acpi_pci_routing_table *entry;
123
 
123
 
124
    /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
124
    /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
125
    status = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buffer);
125
    status = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buffer);
126
    if (ACPI_FAILURE(status))
126
    if (ACPI_FAILURE(status))
127
        return -ENODEV;
127
        return -ENODEV;
128
 
128
 
129
    printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
129
    printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
130
           (char *) buffer.Pointer);
130
           (char *) buffer.Pointer);
131
 
131
 
132
    kfree(buffer.Pointer);
132
    kfree(buffer.Pointer);
133
 
133
 
134
    buffer.Length = ACPI_ALLOCATE_BUFFER;
134
    buffer.Length = ACPI_ALLOCATE_BUFFER;
135
    buffer.Pointer = NULL;
135
    buffer.Pointer = NULL;
136
 
136
 
137
    status = AcpiGetIrqRoutingTable(handle, &buffer);
137
    status = AcpiGetIrqRoutingTable(handle, &buffer);
138
    if (ACPI_FAILURE(status))
138
    if (ACPI_FAILURE(status))
139
    {
139
    {
140
        dbgprintf("AcpiGetIrqRoutingTable failed "
140
        dbgprintf("AcpiGetIrqRoutingTable failed "
141
                  "evaluating _PRT [%s]\n",AcpiFormatException(status));
141
                  "evaluating _PRT [%s]\n",AcpiFormatException(status));
142
        kfree(buffer.Pointer);
142
        kfree(buffer.Pointer);
143
        return -ENODEV;
143
        return -ENODEV;
144
    }
144
    }
145
 
145
 
146
    entry = buffer.Pointer;
146
    entry = buffer.Pointer;
147
    while (entry && (entry->Length > 0)) {
147
    while (entry && (entry->Length > 0)) {
148
        acpi_pci_irq_add_entry(handle, bus, entry);
148
        acpi_pci_irq_add_entry(handle, bus, entry);
149
        entry = (struct acpi_pci_routing_table *)
149
        entry = (struct acpi_pci_routing_table *)
150
            ((unsigned long)entry + entry->Length);
150
            ((unsigned long)entry + entry->Length);
151
    }
151
    }
152
 
152
 
153
    kfree(buffer.Pointer);
153
    kfree(buffer.Pointer);
154
    return 0;
154
    return 0;
155
}
155
}
156
 
156
 
157
static struct acpi_prt_entry *acpi_pci_irq_lookup(struct pci_dev *dev, int pin)
157
static struct acpi_prt_entry *acpi_pci_irq_lookup(struct pci_dev *dev, int pin)
158
{
158
{
159
    struct acpi_prt_entry *entry;
159
    struct acpi_prt_entry *entry;
160
    struct pci_dev *bridge;
160
    struct pci_dev *bridge;
161
    u8 bridge_pin, orig_pin = pin;
161
    u8 bridge_pin, orig_pin = pin;
162
 
162
 
163
    entry = acpi_pci_irq_find_prt_entry(dev, pin);
163
    entry = acpi_pci_irq_find_prt_entry(dev, pin);
164
    if (entry) {
164
    if (entry) {
165
        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %s[%c] _PRT entry\n",
165
        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %s[%c] _PRT entry\n",
166
                  pci_name(dev), pin_name(pin)));
166
                  pci_name(dev), pin_name(pin)));
167
        return entry;
167
        return entry;
168
    }
168
    }
169
 
169
 
170
    /*
170
    /*
171
     * Attempt to derive an IRQ for this device from a parent bridge's
171
     * Attempt to derive an IRQ for this device from a parent bridge's
172
     * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
172
     * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
173
     */
173
     */
174
    bridge = dev->bus->self;
174
    bridge = dev->bus->self;
175
    while (bridge)
175
    while (bridge)
176
    {
176
    {
177
        pin = pci_swizzle_interrupt_pin(dev, pin);
177
        pin = pci_swizzle_interrupt_pin(dev, pin);
178
 
178
 
179
        if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
179
        if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
180
            /* PC card has the same IRQ as its cardbridge */
180
            /* PC card has the same IRQ as its cardbridge */
181
            bridge_pin = bridge->pin;
181
            bridge_pin = bridge->pin;
182
            if (!bridge_pin) {
182
            if (!bridge_pin) {
183
                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
183
                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
184
                          "No interrupt pin configured for device %s\n",
184
                          "No interrupt pin configured for device %s\n",
185
                          pci_name(bridge)));
185
                          pci_name(bridge)));
186
                return NULL;
186
                return NULL;
187
            }
187
            }
188
            pin = bridge_pin;
188
            pin = bridge_pin;
189
        }
189
        }
190
 
190
 
191
        entry = acpi_pci_irq_find_prt_entry(bridge, pin);
191
        entry = acpi_pci_irq_find_prt_entry(bridge, pin);
192
        if (entry) {
192
        if (entry) {
193
            ACPI_DEBUG_PRINT((ACPI_DB_INFO,
193
            ACPI_DEBUG_PRINT((ACPI_DB_INFO,
194
                     "Derived GSI for %s INT %c from %s\n",
194
                     "Derived GSI for %s INT %c from %s\n",
195
                     pci_name(dev), pin_name(orig_pin),
195
                     pci_name(dev), pin_name(orig_pin),
196
                     pci_name(bridge)));
196
                     pci_name(bridge)));
197
            return entry;
197
            return entry;
198
        }
198
        }
199
 
199
 
200
        dev = bridge;
200
        dev = bridge;
201
        bridge = dev->bus->self;
201
        bridge = dev->bus->self;
202
    }
202
    }
203
 
203
 
204
    dbgprintf("can't derive routing for PCI INT %c\n",
204
    dbgprintf("can't derive routing for PCI INT %c\n",
205
         pin_name(orig_pin));
205
         pin_name(orig_pin));
206
    return NULL;
206
    return NULL;
207
}
207
}
208
 
208
 
209
 
209
 
210
int acpi_get_irq(struct pci_dev *dev)
210
int acpi_get_irq(struct pci_dev *dev)
211
{
211
{
212
    struct acpi_prt_entry *entry;
212
    struct acpi_prt_entry *entry;
213
    int gsi = -1;
213
    int gsi = -1;
214
    u8 pin;
214
    u8 pin;
215
 
215
 
216
    int triggering = ACPI_LEVEL_SENSITIVE;
216
    int triggering = ACPI_LEVEL_SENSITIVE;
217
    int polarity = ACPI_ACTIVE_LOW;
217
    int polarity = ACPI_ACTIVE_LOW;
218
 
218
 
219
    char *link = NULL;
219
    char *link = NULL;
220
    char link_desc[16];
220
    char link_desc[16];
221
    int rc;
221
    int rc;
222
 
222
 
223
    pin = dev->pin;
223
    pin = dev->pin;
224
    if (!pin) {
224
 
-
 
225
    if ( !pin )
225
        dbgprintf(("No interrupt pin configured for device %s\n",
226
    {
-
 
227
        dbgprintf("No interrupt pin configured for device %s\n",
226
                  pci_name(dev)));
228
                  pci_name(dev));
227
        return 0;
229
        return 0;
228
    }
230
    }
229
 
231
 
230
    entry = acpi_pci_irq_lookup(dev, pin);
232
    entry = acpi_pci_irq_lookup(dev, pin);
231
    if (!entry) {
233
    if (!entry) {
232
        /*
234
        /*
233
         * IDE legacy mode controller IRQs are magic. Why do compat
235
         * IDE legacy mode controller IRQs are magic. Why do compat
234
         * extensions always make such a nasty mess.
236
         * extensions always make such a nasty mess.
235
         */
237
         */
236
        if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
238
        if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
237
                (dev->class & 0x05) == 0)
239
                (dev->class & 0x05) == 0)
238
            return 0;
240
            return 0;
239
    }
241
    }
240
 
242
 
241
    if (entry)
243
    if (entry)
242
    {
244
    {
243
        if (entry->link)
245
        if (entry->link)
244
        {
246
        {
245
            gsi = acpi_pci_link_allocate_irq(entry->link,
247
            gsi = acpi_pci_link_allocate_irq(entry->link,
246
                             entry->index,
248
                             entry->index,
247
                             &triggering, &polarity,
249
                             &triggering, &polarity,
248
                             &link);
250
                             &link);
249
//            dbgprintf("link not implemen\n");
251
//            dbgprintf("link not implemen\n");
250
        }
252
        }
251
        else
253
        else
252
            gsi = entry->index;
254
            gsi = entry->index;
253
    } else
255
    } else
254
        gsi = -1;
256
        gsi = -1;
255
 
257
 
256
#if 0
258
#if 0
257
 
259
 
258
    /*
260
    /*
259
     * No IRQ known to the ACPI subsystem - maybe the BIOS /
261
     * No IRQ known to the ACPI subsystem - maybe the BIOS /
260
     * driver reported one, then use it. Exit in any case.
262
     * driver reported one, then use it. Exit in any case.
261
     */
263
     */
262
    if (gsi < 0) {
264
    if (gsi < 0) {
263
        u32 dev_gsi;
265
        u32 dev_gsi;
264
        dev_warn(&dev->dev, "PCI INT %c: no GSI", pin_name(pin));
266
        dev_warn(&dev->dev, "PCI INT %c: no GSI", pin_name(pin));
265
        /* Interrupt Line values above 0xF are forbidden */
267
        /* Interrupt Line values above 0xF are forbidden */
266
        if (dev->irq > 0 && (dev->irq <= 0xF) &&
268
        if (dev->irq > 0 && (dev->irq <= 0xF) &&
267
            (acpi_isa_irq_to_gsi(dev->irq, &dev_gsi) == 0)) {
269
            (acpi_isa_irq_to_gsi(dev->irq, &dev_gsi) == 0)) {
268
            printk(" - using ISA IRQ %d\n", dev->irq);
270
            printk(" - using ISA IRQ %d\n", dev->irq);
269
            acpi_register_gsi(&dev->dev, dev_gsi,
271
            acpi_register_gsi(&dev->dev, dev_gsi,
270
                      ACPI_LEVEL_SENSITIVE,
272
                      ACPI_LEVEL_SENSITIVE,
271
                      ACPI_ACTIVE_LOW);
273
                      ACPI_ACTIVE_LOW);
272
            return 0;
274
            return 0;
273
        } else {
275
        } else {
274
            printk("\n");
276
            printk("\n");
275
            return 0;
277
            return 0;
276
        }
278
        }
277
    }
279
    }
278
 
280
 
279
    rc = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
281
    rc = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
280
    if (rc < 0) {
282
    if (rc < 0) {
281
        dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
283
        dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
282
             pin_name(pin));
284
             pin_name(pin));
283
        return rc;
285
        return rc;
284
    }
286
    }
285
    dev->irq = rc;
287
    dev->irq = rc;
286
 
288
 
287
    if (link)
289
    if (link)
288
        snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
290
        snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
289
    else
291
    else
290
        link_desc[0] = '\0';
292
        link_desc[0] = '\0';
291
 
293
 
292
    dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
294
    dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
293
         pin_name(pin), link_desc, gsi,
295
         pin_name(pin), link_desc, gsi,
294
         (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
296
         (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
295
         (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
297
         (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
296
#endif
298
#endif
297
 
299
    return gsi;
298
    return gsi;
-
 
299
}
300
}
300
 
301
 
301
 
302
 
302
#define ACPI_PCI_LINK_MAX_POSSIBLE  16
303
#define ACPI_PCI_LINK_MAX_POSSIBLE  16
303
 
304
 
304
/*
305
/*
305
 * If a link is initialized, we never change its active and initialized
306
 * If a link is initialized, we never change its active and initialized
306
 * later even the link is disable. Instead, we just repick the active irq
307
 * later even the link is disable. Instead, we just repick the active irq
307
 */
308
 */
308
struct acpi_pci_link_irq {
309
struct acpi_pci_link_irq {
309
    u8 active;      /* Current IRQ */
310
    u8 active;      /* Current IRQ */
310
    u8 triggering;      /* All IRQs */
311
    u8 triggering;      /* All IRQs */
311
    u8 polarity;        /* All IRQs */
312
    u8 polarity;        /* All IRQs */
312
    u8 resource_type;
313
    u8 resource_type;
313
    u8 possible_count;
314
    u8 possible_count;
314
    u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
315
    u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
315
    u8 initialized:1;
316
    u8 initialized:1;
316
    u8 reserved:7;
317
    u8 reserved:7;
317
};
318
};
318
 
319
 
319
struct acpi_pci_link {
320
struct acpi_pci_link {
320
    struct list_head        list;
321
    struct list_head        list;
321
    struct acpi_device      *device;
322
    struct acpi_device      *device;
322
    struct acpi_pci_link_irq    irq;
323
    struct acpi_pci_link_irq    irq;
323
    int             refcnt;
324
    int             refcnt;
324
};
325
};
325
 
326
 
326
static LIST_HEAD(acpi_link_list);
327
static LIST_HEAD(acpi_link_list);
327
static DEFINE_MUTEX(acpi_link_lock);
328
static DEFINE_MUTEX(acpi_link_lock);
328
 
329
 
329
 
330
 
330
static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
331
static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
331
{
332
{
332
    int result;
333
    int result;
333
    ACPI_STATUS status;
334
    ACPI_STATUS status;
334
    struct {
335
    struct {
335
        struct acpi_resource res;
336
        struct acpi_resource res;
336
        struct acpi_resource end;
337
        struct acpi_resource end;
337
    } *resource;
338
    } *resource;
338
 
339
 
339
    ACPI_BUFFER buffer = { 0, NULL };
340
    ACPI_BUFFER buffer = { 0, NULL };
340
 
341
 
341
    if (!irq)
342
    if (!irq)
342
        return -EINVAL;
343
        return -EINVAL;
343
 
344
 
344
    resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
345
    resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
345
    if (!resource)
346
    if (!resource)
346
        return -ENOMEM;
347
        return -ENOMEM;
347
 
348
 
348
    buffer.Length = sizeof(*resource) + 1;
349
    buffer.Length = sizeof(*resource) + 1;
349
    buffer.Pointer = resource;
350
    buffer.Pointer = resource;
350
 
351
 
351
    switch (link->irq.resource_type) {
352
    switch (link->irq.resource_type) {
352
    case ACPI_RESOURCE_TYPE_IRQ:
353
    case ACPI_RESOURCE_TYPE_IRQ:
353
        resource->res.Type = ACPI_RESOURCE_TYPE_IRQ;
354
        resource->res.Type = ACPI_RESOURCE_TYPE_IRQ;
354
        resource->res.Length = sizeof(struct acpi_resource);
355
        resource->res.Length = sizeof(struct acpi_resource);
355
        resource->res.Data.Irq.Triggering = link->irq.triggering;
356
        resource->res.Data.Irq.Triggering = link->irq.triggering;
356
        resource->res.Data.Irq.Polarity =
357
        resource->res.Data.Irq.Polarity =
357
            link->irq.polarity;
358
            link->irq.polarity;
358
        if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
359
        if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
359
            resource->res.Data.Irq.Sharable =
360
            resource->res.Data.Irq.Sharable =
360
                ACPI_EXCLUSIVE;
361
                ACPI_EXCLUSIVE;
361
        else
362
        else
362
            resource->res.Data.Irq.Sharable = ACPI_SHARED;
363
            resource->res.Data.Irq.Sharable = ACPI_SHARED;
363
        resource->res.Data.Irq.InterruptCount = 1;
364
        resource->res.Data.Irq.InterruptCount = 1;
364
        resource->res.Data.Irq.Interrupts[0] = irq;
365
        resource->res.Data.Irq.Interrupts[0] = irq;
365
        break;
366
        break;
366
 
367
 
367
    case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
368
    case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
368
        resource->res.Type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
369
        resource->res.Type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
369
        resource->res.Length = sizeof(struct acpi_resource);
370
        resource->res.Length = sizeof(struct acpi_resource);
370
        resource->res.Data.ExtendedIrq.ProducerConsumer =
371
        resource->res.Data.ExtendedIrq.ProducerConsumer =
371
            ACPI_CONSUMER;
372
            ACPI_CONSUMER;
372
        resource->res.Data.ExtendedIrq.Triggering =
373
        resource->res.Data.ExtendedIrq.Triggering =
373
            link->irq.triggering;
374
            link->irq.triggering;
374
        resource->res.Data.ExtendedIrq.Polarity =
375
        resource->res.Data.ExtendedIrq.Polarity =
375
            link->irq.polarity;
376
            link->irq.polarity;
376
        if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
377
        if (link->irq.triggering == ACPI_EDGE_SENSITIVE)
377
            resource->res.Data.Irq.Sharable =
378
            resource->res.Data.Irq.Sharable =
378
                ACPI_EXCLUSIVE;
379
                ACPI_EXCLUSIVE;
379
        else
380
        else
380
            resource->res.Data.Irq.Sharable = ACPI_SHARED;
381
            resource->res.Data.Irq.Sharable = ACPI_SHARED;
381
        resource->res.Data.ExtendedIrq.InterruptCount = 1;
382
        resource->res.Data.ExtendedIrq.InterruptCount = 1;
382
        resource->res.Data.ExtendedIrq.Interrupts[0] = irq;
383
        resource->res.Data.ExtendedIrq.Interrupts[0] = irq;
383
        /* ignore resource_source, it's optional */
384
        /* ignore resource_source, it's optional */
384
        break;
385
        break;
385
    default:
386
    default:
386
        printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
387
        printk(KERN_ERR PREFIX "Invalid Resource_type %d\n", link->irq.resource_type);
387
        result = -EINVAL;
388
        result = -EINVAL;
388
        goto end;
389
        goto end;
389
 
390
 
390
    }
391
    }
391
    resource->end.Type = ACPI_RESOURCE_TYPE_END_TAG;
392
    resource->end.Type = ACPI_RESOURCE_TYPE_END_TAG;
392
 
393
 
393
#if 0
394
#if 0
394
    /* Attempt to set the resource */
395
    /* Attempt to set the resource */
395
    status = acpi_set_current_resources(link->device->handle, &buffer);
396
    status = acpi_set_current_resources(link->device->handle, &buffer);
396
 
397
 
397
    /* check for total failure */
398
    /* check for total failure */
398
    if (ACPI_FAILURE(status)) {
399
    if (ACPI_FAILURE(status)) {
399
        dbgprintf("%s failure Evaluating _SRS", __FUNCTION__);
400
        dbgprintf("%s failure Evaluating _SRS", __FUNCTION__);
400
        result = -ENODEV;
401
        result = -ENODEV;
401
        goto end;
402
        goto end;
402
    }
403
    }
403
 
404
 
404
    /* Query _STA, set device->status */
405
    /* Query _STA, set device->status */
405
    result = acpi_bus_get_status(link->device);
406
    result = acpi_bus_get_status(link->device);
406
    if (result) {
407
    if (result) {
407
        printk(KERN_ERR PREFIX "Unable to read status\n");
408
        printk(KERN_ERR PREFIX "Unable to read status\n");
408
        goto end;
409
        goto end;
409
    }
410
    }
410
    if (!link->device->status.enabled) {
411
    if (!link->device->status.enabled) {
411
        printk(KERN_WARNING PREFIX
412
        printk(KERN_WARNING PREFIX
412
                  "%s [%s] disabled and referenced, BIOS bug\n",
413
                  "%s [%s] disabled and referenced, BIOS bug\n",
413
                  acpi_device_name(link->device),
414
                  acpi_device_name(link->device),
414
                  acpi_device_bid(link->device));
415
                  acpi_device_bid(link->device));
415
    }
416
    }
416
 
417
 
417
    /* Query _CRS, set link->irq.active */
418
    /* Query _CRS, set link->irq.active */
418
    result = acpi_pci_link_get_current(link);
419
    result = acpi_pci_link_get_current(link);
419
    if (result) {
420
    if (result) {
420
        goto end;
421
        goto end;
421
    }
422
    }
422
 
423
 
423
    /*
424
    /*
424
     * Is current setting not what we set?
425
     * Is current setting not what we set?
425
     * set link->irq.active
426
     * set link->irq.active
426
     */
427
     */
427
    if (link->irq.active != irq) {
428
    if (link->irq.active != irq) {
428
        /*
429
        /*
429
         * policy: when _CRS doesn't return what we just _SRS
430
         * policy: when _CRS doesn't return what we just _SRS
430
         * assume _SRS worked and override _CRS value.
431
         * assume _SRS worked and override _CRS value.
431
         */
432
         */
432
        printk(KERN_WARNING PREFIX
433
        printk(KERN_WARNING PREFIX
433
                  "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
434
                  "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
434
                  acpi_device_name(link->device),
435
                  acpi_device_name(link->device),
435
                  acpi_device_bid(link->device), link->irq.active, irq);
436
                  acpi_device_bid(link->device), link->irq.active, irq);
436
        link->irq.active = irq;
437
        link->irq.active = irq;
437
    }
438
    }
438
#endif
439
#endif
439
 
440
 
440
    ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
441
    ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
441
 
442
 
442
end:
443
end:
443
    kfree(resource);
444
    kfree(resource);
444
    return result;
445
    return result;
445
}
446
}
446
 
447
 
447
 
448
 
448
 
449
 
449
#define ACPI_MAX_IRQS       256
450
#define ACPI_MAX_IRQS       256
450
#define ACPI_MAX_ISA_IRQ    16
451
#define ACPI_MAX_ISA_IRQ    16
451
 
452
 
452
#define PIRQ_PENALTY_PCI_AVAILABLE  (0)
453
#define PIRQ_PENALTY_PCI_AVAILABLE  (0)
453
#define PIRQ_PENALTY_PCI_POSSIBLE   (16*16)
454
#define PIRQ_PENALTY_PCI_POSSIBLE   (16*16)
454
#define PIRQ_PENALTY_PCI_USING      (16*16*16)
455
#define PIRQ_PENALTY_PCI_USING      (16*16*16)
455
#define PIRQ_PENALTY_ISA_TYPICAL    (16*16*16*16)
456
#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_USED       (16*16*16*16*16)
457
#define PIRQ_PENALTY_ISA_ALWAYS     (16*16*16*16*16*16)
458
#define PIRQ_PENALTY_ISA_ALWAYS     (16*16*16*16*16*16)
458
 
459
 
459
static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
460
static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
460
    PIRQ_PENALTY_ISA_ALWAYS,    /* IRQ0 timer */
461
    PIRQ_PENALTY_ISA_ALWAYS,    /* IRQ0 timer */
461
    PIRQ_PENALTY_ISA_ALWAYS,    /* IRQ1 keyboard */
462
    PIRQ_PENALTY_ISA_ALWAYS,    /* IRQ1 keyboard */
462
    PIRQ_PENALTY_ISA_ALWAYS,    /* IRQ2 cascade */
463
    PIRQ_PENALTY_ISA_ALWAYS,    /* IRQ2 cascade */
463
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ3 serial */
464
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ3 serial */
464
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ4 serial */
465
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ4 serial */
465
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ5 sometimes SoundBlaster */
466
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ5 sometimes SoundBlaster */
466
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ6 */
467
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ6 */
467
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ7 parallel, spurious */
468
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ7 parallel, spurious */
468
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ8 rtc, sometimes */
469
    PIRQ_PENALTY_ISA_TYPICAL,   /* IRQ8 rtc, sometimes */
469
    PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9  PCI, often acpi */
470
    PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ9  PCI, often acpi */
470
    PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
471
    PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ10 PCI */
471
    PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
472
    PIRQ_PENALTY_PCI_AVAILABLE, /* IRQ11 PCI */
472
    PIRQ_PENALTY_ISA_USED,      /* IRQ12 mouse */
473
    PIRQ_PENALTY_ISA_USED,      /* IRQ12 mouse */
473
    PIRQ_PENALTY_ISA_USED,      /* IRQ13 fpe, sometimes */
474
    PIRQ_PENALTY_ISA_USED,      /* IRQ13 fpe, sometimes */
474
    PIRQ_PENALTY_ISA_USED,      /* IRQ14 ide0 */
475
    PIRQ_PENALTY_ISA_USED,      /* IRQ14 ide0 */
475
    PIRQ_PENALTY_ISA_USED,      /* IRQ15 ide1 */
476
    PIRQ_PENALTY_ISA_USED,      /* IRQ15 ide1 */
476
    /* >IRQ15 */
477
    /* >IRQ15 */
477
};
478
};
478
 
479
 
479
 
480
 
480
 
481
 
481
 
482
 
482
static int acpi_irq_balance = 0;
483
static int acpi_irq_balance = 0;
483
 
484
 
484
static int acpi_pci_link_allocate(struct acpi_pci_link *link)
485
static int acpi_pci_link_allocate(struct acpi_pci_link *link)
485
{
486
{
486
    int irq;
487
    int irq;
487
    int i;
488
    int i;
488
 
489
 
489
    if (link->irq.initialized) {
490
    if (link->irq.initialized) {
490
        if (link->refcnt == 0)
491
        if (link->refcnt == 0)
491
            /* This means the link is disabled but initialized */
492
            /* This means the link is disabled but initialized */
492
            acpi_pci_link_set(link, link->irq.active);
493
            acpi_pci_link_set(link, link->irq.active);
493
        return 0;
494
        return 0;
494
    }
495
    }
495
 
496
 
496
    /*
497
    /*
497
     * search for active IRQ in list of possible IRQs.
498
     * search for active IRQ in list of possible IRQs.
498
     */
499
     */
499
    for (i = 0; i < link->irq.possible_count; ++i) {
500
    for (i = 0; i < link->irq.possible_count; ++i) {
500
        if (link->irq.active == link->irq.possible[i])
501
        if (link->irq.active == link->irq.possible[i])
501
            break;
502
            break;
502
    }
503
    }
503
    /*
504
    /*
504
     * forget active IRQ that is not in possible list
505
     * forget active IRQ that is not in possible list
505
     */
506
     */
506
    if (i == link->irq.possible_count) {
507
    if (i == link->irq.possible_count) {
507
        printk(KERN_WARNING PREFIX "_CRS %d not found"
508
        dbgprintf(KERN_WARNING PREFIX "_CRS %d not found"
508
                      " in _PRS\n", link->irq.active);
509
                      " in _PRS\n", link->irq.active);
509
        link->irq.active = 0;
510
        link->irq.active = 0;
510
    }
511
    }
511
 
512
 
512
    /*
513
    /*
513
     * if active found, use it; else pick entry from end of possible list.
514
     * if active found, use it; else pick entry from end of possible list.
514
     */
515
     */
515
    if (link->irq.active)
516
    if (link->irq.active)
516
        irq = link->irq.active;
517
        irq = link->irq.active;
517
    else
518
    else
518
        irq = link->irq.possible[link->irq.possible_count - 1];
519
        irq = link->irq.possible[link->irq.possible_count - 1];
519
 
520
 
520
    if (acpi_irq_balance || !link->irq.active) {
521
    if (acpi_irq_balance || !link->irq.active) {
521
        /*
522
        /*
522
         * Select the best IRQ.  This is done in reverse to promote
523
         * Select the best IRQ.  This is done in reverse to promote
523
         * the use of IRQs 9, 10, 11, and >15.
524
         * the use of IRQs 9, 10, 11, and >15.
524
         */
525
         */
525
        for (i = (link->irq.possible_count - 1); i >= 0; i--) {
526
        for (i = (link->irq.possible_count - 1); i >= 0; i--) {
526
            if (acpi_irq_penalty[irq] >
527
            if (acpi_irq_penalty[irq] >
527
                acpi_irq_penalty[link->irq.possible[i]])
528
                acpi_irq_penalty[link->irq.possible[i]])
528
                irq = link->irq.possible[i];
529
                irq = link->irq.possible[i];
529
        }
530
        }
530
    }
531
    }
531
 
532
 
532
    /* Attempt to enable the link device at this IRQ. */
533
    /* Attempt to enable the link device at this IRQ. */
533
    if (acpi_pci_link_set(link, irq)) {
534
    if (acpi_pci_link_set(link, irq)) {
534
        printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
535
        printk(KERN_ERR PREFIX "Unable to set IRQ for %s [%s]. "
535
                "Try pci=noacpi or acpi=off\n",
536
                "Try pci=noacpi or acpi=off\n",
536
                acpi_device_name(link->device),
537
                acpi_device_name(link->device),
537
                acpi_device_bid(link->device));
538
                acpi_device_bid(link->device));
538
        return -ENODEV;
539
        return -ENODEV;
539
    } else {
540
    } else {
540
        acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
541
        acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
541
        printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
542
        printk(KERN_WARNING PREFIX "%s [%s] enabled at IRQ %d\n",
542
               acpi_device_name(link->device),
543
               acpi_device_name(link->device),
543
               acpi_device_bid(link->device), link->irq.active);
544
               acpi_device_bid(link->device), link->irq.active);
544
    }
545
    }
545
 
546
 
546
    link->irq.initialized = 1;
547
    link->irq.initialized = 1;
547
    return 0;
548
    return 0;
548
}
549
}
549
 
550
 
550
 
551
 
551
/*
552
/*
552
 * acpi_pci_link_allocate_irq
553
 * acpi_pci_link_allocate_irq
553
 * success: return IRQ >= 0
554
 * success: return IRQ >= 0
554
 * failure: return -1
555
 * failure: return -1
555
 */
556
 */
556
int acpi_pci_link_allocate_irq(ACPI_HANDLE handle, int index,
557
int acpi_pci_link_allocate_irq(ACPI_HANDLE handle, int index,
557
                               int *triggering, int *polarity, char **name)
558
                               int *triggering, int *polarity, char **name)
558
{
559
{
559
    int result;
560
    int result;
560
    struct acpi_device *device;
561
    struct acpi_device *device;
561
    struct acpi_pci_link *link;
562
    struct acpi_pci_link *link;
562
 
563
 
563
    result = acpi_bus_get_device(handle, &device);
564
    result = acpi_bus_get_device(handle, &device);
564
    if (result) {
565
    if (result) {
565
        printk(KERN_ERR PREFIX "Invalid link device\n");
566
        printk(KERN_ERR PREFIX "Invalid link device\n");
566
        return -1;
567
        return -1;
567
    }
568
    }
568
 
569
 
569
    link = acpi_driver_data(device);
570
    link = acpi_driver_data(device);
570
    if (!link) {
571
    if (!link) {
571
        printk(KERN_ERR PREFIX "Invalid link context\n");
572
        printk(KERN_ERR PREFIX "Invalid link context\n");
572
        return -1;
573
        return -1;
573
    }
574
    }
574
 
575
 
575
    /* TBD: Support multiple index (IRQ) entries per Link Device */
576
    /* TBD: Support multiple index (IRQ) entries per Link Device */
576
    if (index) {
577
    if (index) {
577
        printk(KERN_ERR PREFIX "Invalid index %d\n", index);
578
        printk(KERN_ERR PREFIX "Invalid index %d\n", index);
578
        return -1;
579
        return -1;
579
    }
580
    }
580
 
581
 
581
    mutex_lock(&acpi_link_lock);
582
    mutex_lock(&acpi_link_lock);
582
    if (acpi_pci_link_allocate(link)) {
583
    if (acpi_pci_link_allocate(link)) {
583
        mutex_unlock(&acpi_link_lock);
584
        mutex_unlock(&acpi_link_lock);
584
        return -1;
585
        return -1;
585
    }
586
    }
586
 
587
 
587
    if (!link->irq.active) {
588
    if (!link->irq.active) {
588
        mutex_unlock(&acpi_link_lock);
589
        mutex_unlock(&acpi_link_lock);
589
        printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
590
        printk(KERN_ERR PREFIX "Link active IRQ is 0!\n");
590
        return -1;
591
        return -1;
591
    }
592
    }
592
    link->refcnt++;
593
    link->refcnt++;
593
    mutex_unlock(&acpi_link_lock);
594
    mutex_unlock(&acpi_link_lock);
594
 
595
 
595
    if (triggering)
596
    if (triggering)
596
        *triggering = link->irq.triggering;
597
        *triggering = link->irq.triggering;
597
    if (polarity)
598
    if (polarity)
598
        *polarity = link->irq.polarity;
599
        *polarity = link->irq.polarity;
599
    if (name)
600
    if (name)
600
        *name = acpi_device_bid(link->device);
601
        *name = acpi_device_bid(link->device);
601
    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
602
    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
602
              "Link %s is referenced\n",
603
              "Link %s is referenced\n",
603
              acpi_device_bid(link->device)));
604
              acpi_device_bid(link->device)));
604
    return (link->irq.active);
605
    return (link->irq.active);
605
}
606
}