Subversion Repositories HelenOS

Rev

Rev 1920 | Rev 1922 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1920 Rev 1921
1
/*
1
/*
2
 * Copyright (C) 2006 Jakub Jermar
2
 * Copyright (C) 2006 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup genericddi
29
/** @addtogroup genericddi
30
 * @{
30
 * @{
31
 */
31
 */
32
/**
32
/**
33
 * @file
33
 * @file
34
 * @brief   IRQ redirector.
34
 * @brief   IRQ redirector.
35
 *
35
 *
36
 * This file provides means of connecting IRQs with particular
36
 * This file provides means of connecting IRQs with particular
37
 * devices and logic for dispatching interrupts to IRQ handlers
37
 * devices and logic for dispatching interrupts to IRQ handlers
38
 * defined by those devices.
38
 * defined by those devices.
39
 *
39
 *
40
 * This code is designed to support:
40
 * This code is designed to support:
41
 * - multiple devices sharing single IRQ
41
 * - multiple devices sharing single IRQ
42
 * - multiple IRQs per signle device
42
 * - multiple IRQs per signle device
43
 *
43
 *
44
 *
44
 *
45
 * Note about architectures.
45
 * Note about architectures.
46
 *
46
 *
47
 * Some architectures has the term IRQ well defined. Examples
47
 * Some architectures has the term IRQ well defined. Examples
48
 * of such architectures include amd64, ia32 and mips32. Some
48
 * of such architectures include amd64, ia32 and mips32. Some
49
 * other architectures, such as sparc64, don't use the term
49
 * other architectures, such as sparc64, don't use the term
50
 * at all. In those cases, we boldly step forward and define what
50
 * at all. In those cases, we boldly step forward and define what
51
 * an IRQ is.
51
 * an IRQ is.
52
 *
52
 *
53
 * The implementation is generic enough and still allows the
53
 * The implementation is generic enough and still allows the
54
 * architectures to use the hardware layout effectively.
54
 * architectures to use the hardware layout effectively.
55
 * For instance, on amd64 and ia32, where there is only 16
55
 * For instance, on amd64 and ia32, where there is only 16
56
 * IRQs, the irq_hash_table can be optimized to a one-dimensional
56
 * IRQs, the irq_hash_table can be optimized to a one-dimensional
57
 * array. Next, when it is known that the IRQ numbers (aka INR's)
57
 * array. Next, when it is known that the IRQ numbers (aka INR's)
58
 * are unique, the claim functions can always return IRQ_ACCEPT.
58
 * are unique, the claim functions can always return IRQ_ACCEPT.
59
 */
59
 */
60
 
60
 
61
#include <ddi/irq.h>
61
#include <ddi/irq.h>
62
#include <adt/hash_table.h>
62
#include <adt/hash_table.h>
63
#include <arch/types.h>
63
#include <arch/types.h>
64
#include <typedefs.h>
64
#include <typedefs.h>
65
#include <synch/spinlock.h>
65
#include <synch/spinlock.h>
-
 
66
#include <atomic.h>
66
#include <arch.h>
67
#include <arch.h>
67
 
68
 
68
/**
69
/**
69
 * Spinlock protecting the hash table.
70
 * Spinlock protecting the hash table.
70
 * This lock must be taken only when interrupts are disabled.
71
 * This lock must be taken only when interrupts are disabled.
71
 */
72
 */
72
SPINLOCK_INITIALIZE(irq_hash_table_lock);
73
SPINLOCK_INITIALIZE(irq_hash_table_lock);
73
static hash_table_t irq_hash_table;
74
static hash_table_t irq_hash_table;
74
 
75
 
75
/**
76
/**
76
 * Hash table operations for cases when we know that
77
 * Hash table operations for cases when we know that
77
 * there will be collisions between different keys.
78
 * there will be collisions between different keys.
78
 */
79
 */
79
static index_t irq_ht_hash(unative_t *key);
80
static index_t irq_ht_hash(unative_t *key);
80
static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
81
static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
81
 
82
 
82
static hash_table_operations_t irq_ht_ops = {
83
static hash_table_operations_t irq_ht_ops = {
83
    .hash = irq_ht_hash,
84
    .hash = irq_ht_hash,
84
    .compare = irq_ht_compare,
85
    .compare = irq_ht_compare,
85
    .remove_callback = NULL     /* not used */
86
    .remove_callback = NULL     /* not used */
86
};
87
};
87
 
88
 
88
/**
89
/**
89
 * Hash table operations for cases when we know that
90
 * Hash table operations for cases when we know that
90
 * there will be no collisions between different keys.
91
 * there will be no collisions between different keys.
91
 * However, there might be still collisions among
92
 * However, there might be still collisions among
92
 * elements with single key (sharing of one IRQ).
93
 * elements with single key (sharing of one IRQ).
93
 */
94
 */
94
static index_t irq_lin_hash(unative_t *key);
95
static index_t irq_lin_hash(unative_t *key);
95
static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
96
static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
96
 
97
 
97
static hash_table_operations_t irq_lin_ops = {
98
static hash_table_operations_t irq_lin_ops = {
98
    .hash = irq_lin_hash,
99
    .hash = irq_lin_hash,
99
    .compare = irq_lin_compare,
100
    .compare = irq_lin_compare,
100
    .remove_callback = NULL     /* not used */
101
    .remove_callback = NULL     /* not used */
101
};
102
};
102
 
103
 
103
/** Initialize IRQ subsystem.
104
/** Initialize IRQ subsystem.
104
 *
105
 *
105
 * @param inrs Numbers of unique IRQ numbers or INRs.
106
 * @param inrs Numbers of unique IRQ numbers or INRs.
106
 * @param chains Number of chains in the hash table.
107
 * @param chains Number of chains in the hash table.
107
 */
108
 */
108
void irq_init(count_t inrs, count_t chains)
109
void irq_init(count_t inrs, count_t chains)
109
{
110
{
110
    /*
111
    /*
111
     * Be smart about the choice of the hash table operations.
112
     * Be smart about the choice of the hash table operations.
112
     * In cases in which inrs equals the requested number of
113
     * In cases in which inrs equals the requested number of
113
     * chains (i.e. where there is no collision between
114
     * chains (i.e. where there is no collision between
114
     * different keys), we can use optimized set of operations.
115
     * different keys), we can use optimized set of operations.
115
     */
116
     */
116
    if (inrs == chains)
117
    if (inrs == chains)
117
        hash_table_create(&irq_hash_table, chains, 1, &irq_lin_ops);
118
        hash_table_create(&irq_hash_table, chains, 1, &irq_lin_ops);
118
    else
119
    else
119
        hash_table_create(&irq_hash_table, chains, 1, &irq_ht_ops);
120
        hash_table_create(&irq_hash_table, chains, 1, &irq_ht_ops);
120
}
121
}
121
 
122
 
122
/** Initialize one IRQ structure.
123
/** Initialize one IRQ structure.
123
 *
124
 *
124
 * @param irq Pointer to the IRQ structure to be initialized.
125
 * @param irq Pointer to the IRQ structure to be initialized.
125
 *
126
 *
126
 */
127
 */
127
void irq_initialize(irq_t *irq)
128
void irq_initialize(irq_t *irq)
128
{
129
{
129
    link_initialize(&irq->link);
130
    link_initialize(&irq->link);
-
 
131
    spinlock_initialize(&irq->lock, "irq.lock");
130
    irq->inr = -1;
132
    irq->inr = -1;
131
    irq->devno = -1;
133
    irq->devno = -1;
132
    irq->notif = 0;
-
 
133
    irq->trigger = 0;
134
    irq->trigger = 0;
134
    irq->claim = NULL;
135
    irq->claim = NULL;
135
    irq->handler = NULL;
136
    irq->handler = NULL;
136
    irq->arg = NULL;
137
    irq->arg = NULL;
-
 
138
    irq->notif_answerbox = NULL;
-
 
139
    irq->code = NULL;
-
 
140
    atomic_set(&irq->counter, 0);
137
}
141
}
138
 
142
 
139
/** Register IRQ for device.
143
/** Register IRQ for device.
140
 *
144
 *
141
 * The irq structure must be filled with information
145
 * The irq structure must be filled with information
142
 * about the interrupt source and with the claim()
146
 * about the interrupt source and with the claim()
143
 * function pointer and irq_handler() function pointer.
147
 * function pointer and irq_handler() function pointer.
144
 *
148
 *
145
 * @param irq IRQ structure belonging to a device.
149
 * @param irq IRQ structure belonging to a device.
146
 */
150
 */
147
void irq_register(irq_t *irq)
151
void irq_register(irq_t *irq)
148
{
152
{
149
    ipl_t ipl;
153
    ipl_t ipl;
150
   
154
   
151
    ipl = interrupts_disable();
155
    ipl = interrupts_disable();
152
    spinlock_lock(&irq_hash_table_lock);
156
    spinlock_lock(&irq_hash_table_lock);
153
    hash_table_insert(&irq_hash_table, (void *) &irq->inr, &irq->link);
157
    hash_table_insert(&irq_hash_table, (void *) &irq->inr, &irq->link);
154
    spinlock_unlock(&irq_hash_table_lock);
158
    spinlock_unlock(&irq_hash_table_lock);
155
    interrupts_restore(ipl);
159
    interrupts_restore(ipl);
156
}
160
}
157
 
161
 
158
/** Dispatch the IRQ.
162
/** Dispatch the IRQ.
159
 *
163
 *
160
 * @param inr Interrupt number (aka inr or irq).
164
 * @param inr Interrupt number (aka inr or irq).
161
 *
165
 *
162
 * @return IRQ structure of the respective device or NULL.
166
 * @return IRQ structure of the respective device or NULL.
163
 */
167
 */
164
irq_t *irq_dispatch(inr_t inr)
168
irq_t *irq_dispatch(inr_t inr)
165
{
169
{
166
    ipl_t ipl;
170
    ipl_t ipl;
167
    link_t *lnk;
171
    link_t *lnk;
168
   
172
   
169
    ipl = interrupts_disable();
173
    ipl = interrupts_disable();
170
    spinlock_lock(&irq_hash_table_lock);
174
    spinlock_lock(&irq_hash_table_lock);
171
 
175
 
172
    lnk = hash_table_find(&irq_hash_table, (void *) &inr);
176
    lnk = hash_table_find(&irq_hash_table, (void *) &inr);
173
    if (lnk) {
177
    if (lnk) {
174
        irq_t *irq;
178
        irq_t *irq;
175
       
179
       
176
        irq = hash_table_get_instance(lnk, irq_t, link);
180
        irq = hash_table_get_instance(lnk, irq_t, link);
177
 
181
 
178
        spinlock_unlock(&irq_hash_table_lock);
182
        spinlock_unlock(&irq_hash_table_lock);
179
        interrupts_restore(ipl);
183
        interrupts_restore(ipl);
180
        return irq;
184
        return irq;
181
    }
185
    }
182
   
186
   
183
    spinlock_unlock(&irq_hash_table_lock);
187
    spinlock_unlock(&irq_hash_table_lock);
184
    interrupts_restore(ipl);
188
    interrupts_restore(ipl);
185
 
189
 
186
    return NULL;   
190
    return NULL;   
187
}
191
}
188
 
192
 
189
/** Compute hash index for the key.
193
/** Compute hash index for the key.
190
 *
194
 *
191
 * This function computes hash index into
195
 * This function computes hash index into
192
 * the IRQ hash table for which there
196
 * the IRQ hash table for which there
193
 * can be collisions between different
197
 * can be collisions between different
194
 * INRs.
198
 * INRs.
195
 *
199
 *
196
 * @param key Pointer to INR.
200
 * @param key Pointer to INR.
197
 *
201
 *
198
 * @return Index into the hash table.
202
 * @return Index into the hash table.
199
 */
203
 */
200
index_t irq_ht_hash(unative_t *key)
204
index_t irq_ht_hash(unative_t *key)
201
{
205
{
202
    inr_t *inr = (inr_t *) key;
206
    inr_t *inr = (inr_t *) key;
203
    return *inr % irq_hash_table.entries;
207
    return *inr % irq_hash_table.entries;
204
}
208
}
205
 
209
 
206
/** Compare hash table element with a key.
210
/** Compare hash table element with a key.
207
 *
211
 *
208
 * As usually, we do sort of a hack here.
212
 * As usually, we do sort of a hack here.
209
 * Even when the key matches the inr member,
213
 * Even when the key matches the inr member,
210
 * we ask the device to either accept
214
 * we ask the device to either accept
211
 * or decline to service the interrupt.
215
 * or decline to service the interrupt.
212
 *
216
 *
213
 * @param key Pointer to key (i.e. inr).
217
 * @param key Pointer to key (i.e. inr).
214
 * @param keys This is 1.
218
 * @param keys This is 1.
215
 * @param item The item to compare the key with.
219
 * @param item The item to compare the key with.
216
 *
220
 *
217
 * @return True on match or false otherwise.
221
 * @return True on match or false otherwise.
218
 */
222
 */
219
bool irq_ht_compare(unative_t *key, count_t keys, link_t *item)
223
bool irq_ht_compare(unative_t *key, count_t keys, link_t *item)
220
{
224
{
221
    irq_t *irq = hash_table_get_instance(item, irq_t, link);
225
    irq_t *irq = hash_table_get_instance(item, irq_t, link);
222
    inr_t *inr = (inr_t *) key;
226
    inr_t *inr = (inr_t *) key;
-
 
227
    bool rv;
223
   
228
   
-
 
229
    spinlock_lock(&irq->lock);
224
    return ((irq->inr == *inr) && (irq->claim() == IRQ_ACCEPT));
230
    rv = ((irq->inr == *inr) && (irq->claim() == IRQ_ACCEPT));
-
 
231
    spinlock_unlock(&irq->lock);
-
 
232
 
-
 
233
    return rv;
225
}
234
}
226
 
235
 
227
/** Compute hash index for the key.
236
/** Compute hash index for the key.
228
 *
237
 *
229
 * This function computes hash index into
238
 * This function computes hash index into
230
 * the IRQ hash table for which there
239
 * the IRQ hash table for which there
231
 * are no collisions between different
240
 * are no collisions between different
232
 * INRs.
241
 * INRs.
233
 *
242
 *
234
 * @param key INR.
243
 * @param key INR.
235
 *
244
 *
236
 * @return Index into the hash table.
245
 * @return Index into the hash table.
237
 */
246
 */
238
index_t irq_lin_hash(unative_t *key)
247
index_t irq_lin_hash(unative_t *key)
239
{
248
{
240
    inr_t *inr = (inr_t *) key;
249
    inr_t *inr = (inr_t *) key;
241
    return *inr;
250
    return *inr;
242
}
251
}
243
 
252
 
244
/** Compare hash table element with a key.
253
/** Compare hash table element with a key.
245
 *
254
 *
246
 * As usually, we do sort of a hack here.
255
 * As usually, we do sort of a hack here.
247
 * We don't compare the inr member with
256
 * We don't compare the inr member with
248
 * the key because we know that there are
257
 * the key because we know that there are
249
 * no collision between different keys.
258
 * no collision between different keys.
250
 * We only ask the device to either accept
259
 * We only ask the device to either accept
251
 * or decline to service the interrupt.
260
 * or decline to service the interrupt.
252
 *
261
 *
253
 * @param key Pointer to key (i.e. inr).
262
 * @param key Pointer to key (i.e. inr).
254
 * @param keys This is 1.
263
 * @param keys This is 1.
255
 * @param item The item to compare the key with.
264
 * @param item The item to compare the key with.
256
 *
265
 *
257
 * @return True on match or false otherwise.
266
 * @return True on match or false otherwise.
258
 */
267
 */
259
bool irq_lin_compare(unative_t *key, count_t keys, link_t *item)
268
bool irq_lin_compare(unative_t *key, count_t keys, link_t *item)
260
{
269
{
261
    irq_t *irq = list_get_instance(item, irq_t, link);
270
    irq_t *irq = list_get_instance(item, irq_t, link);
-
 
271
    bool rv;
-
 
272
   
-
 
273
    spinlock_lock(&irq->lock);
-
 
274
    rv = (irq->claim() == IRQ_ACCEPT);
-
 
275
    spinlock_unlock(&irq->lock);
262
   
276
   
263
    return (irq->claim() == IRQ_ACCEPT);
277
    return rv;
264
}
278
}
265
 
279
 
266
/** @}
280
/** @}
267
 */
281
 */
268
 
282