Subversion Repositories HelenOS

Rev

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

Rev 2064 Rev 2071
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * Copyright (C) 2006 Jakub Jermar
3
 * Copyright (c) 2006 Jakub Jermar
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup genericipc
30
/** @addtogroup genericipc
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief IRQ notification framework.
35
 * @brief IRQ notification framework.
36
 *
36
 *
37
 * This framework allows applications to register to receive a notification
37
 * This framework allows applications to register to receive a notification
38
 * when interrupt is detected. The application may provide a simple 'top-half'
38
 * when interrupt is detected. The application may provide a simple 'top-half'
39
 * handler as part of its registration, which can perform simple operations
39
 * handler as part of its registration, which can perform simple operations
40
 * (read/write port/memory, add information to notification ipc message).
40
 * (read/write port/memory, add information to notification ipc message).
41
 *
41
 *
42
 * The structure of a notification message is as follows:
42
 * The structure of a notification message is as follows:
43
 * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall
43
 * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall
44
 * - ARG1: payload modified by a 'top-half' handler
44
 * - ARG1: payload modified by a 'top-half' handler
45
 * - ARG2: payload modified by a 'top-half' handler
45
 * - ARG2: payload modified by a 'top-half' handler
46
 * - ARG3: payload modified by a 'top-half' handler
46
 * - ARG3: payload modified by a 'top-half' handler
47
 * - in_phone_hash: interrupt counter (may be needed to assure correct order
47
 * - in_phone_hash: interrupt counter (may be needed to assure correct order
48
 *         in multithreaded drivers)
48
 *         in multithreaded drivers)
49
 */
49
 */
50
 
50
 
51
#include <arch.h>
51
#include <arch.h>
52
#include <mm/slab.h>
52
#include <mm/slab.h>
53
#include <errno.h>
53
#include <errno.h>
54
#include <ddi/irq.h>
54
#include <ddi/irq.h>
55
#include <ipc/ipc.h>
55
#include <ipc/ipc.h>
56
#include <ipc/irq.h>
56
#include <ipc/irq.h>
57
#include <syscall/copy.h>
57
#include <syscall/copy.h>
58
#include <console/console.h>
58
#include <console/console.h>
59
#include <print.h>
59
#include <print.h>
60
 
60
 
61
/** Execute code associated with IRQ notification.
61
/** Execute code associated with IRQ notification.
62
 *
62
 *
63
 * @param call Notification call.
63
 * @param call Notification call.
64
 * @param code Top-half pseudocode.
64
 * @param code Top-half pseudocode.
65
 */
65
 */
66
static void code_execute(call_t *call, irq_code_t *code)
66
static void code_execute(call_t *call, irq_code_t *code)
67
{
67
{
68
    int i;
68
    int i;
69
    unative_t dstval = 0;
69
    unative_t dstval = 0;
70
   
70
   
71
    if (!code)
71
    if (!code)
72
        return;
72
        return;
73
   
73
   
74
    for (i=0; i < code->cmdcount;i++) {
74
    for (i=0; i < code->cmdcount;i++) {
75
        switch (code->cmds[i].cmd) {
75
        switch (code->cmds[i].cmd) {
76
        case CMD_MEM_READ_1:
76
        case CMD_MEM_READ_1:
77
            dstval = *((uint8_t *)code->cmds[i].addr);
77
            dstval = *((uint8_t *)code->cmds[i].addr);
78
            break;
78
            break;
79
        case CMD_MEM_READ_2:
79
        case CMD_MEM_READ_2:
80
            dstval = *((uint16_t *)code->cmds[i].addr);
80
            dstval = *((uint16_t *)code->cmds[i].addr);
81
            break;
81
            break;
82
        case CMD_MEM_READ_4:
82
        case CMD_MEM_READ_4:
83
            dstval = *((uint32_t *)code->cmds[i].addr);
83
            dstval = *((uint32_t *)code->cmds[i].addr);
84
            break;
84
            break;
85
        case CMD_MEM_READ_8:
85
        case CMD_MEM_READ_8:
86
            dstval = *((uint64_t *)code->cmds[i].addr);
86
            dstval = *((uint64_t *)code->cmds[i].addr);
87
            break;
87
            break;
88
        case CMD_MEM_WRITE_1:
88
        case CMD_MEM_WRITE_1:
89
            *((uint8_t *)code->cmds[i].addr) = code->cmds[i].value;
89
            *((uint8_t *)code->cmds[i].addr) = code->cmds[i].value;
90
            break;
90
            break;
91
        case CMD_MEM_WRITE_2:
91
        case CMD_MEM_WRITE_2:
92
            *((uint16_t *)code->cmds[i].addr) = code->cmds[i].value;
92
            *((uint16_t *)code->cmds[i].addr) = code->cmds[i].value;
93
            break;
93
            break;
94
        case CMD_MEM_WRITE_4:
94
        case CMD_MEM_WRITE_4:
95
            *((uint32_t *)code->cmds[i].addr) = code->cmds[i].value;
95
            *((uint32_t *)code->cmds[i].addr) = code->cmds[i].value;
96
            break;
96
            break;
97
        case CMD_MEM_WRITE_8:
97
        case CMD_MEM_WRITE_8:
98
            *((uint64_t *)code->cmds[i].addr) = code->cmds[i].value;
98
            *((uint64_t *)code->cmds[i].addr) = code->cmds[i].value;
99
            break;
99
            break;
100
#if defined(ia32) || defined(amd64)
100
#if defined(ia32) || defined(amd64)
101
        case CMD_PORT_READ_1:
101
        case CMD_PORT_READ_1:
102
            dstval = inb((long)code->cmds[i].addr);
102
            dstval = inb((long)code->cmds[i].addr);
103
            break;
103
            break;
104
        case CMD_PORT_WRITE_1:
104
        case CMD_PORT_WRITE_1:
105
            outb((long)code->cmds[i].addr, code->cmds[i].value);
105
            outb((long)code->cmds[i].addr, code->cmds[i].value);
106
            break;
106
            break;
107
#endif
107
#endif
108
#if defined(ia64) && defined(SKI)
108
#if defined(ia64) && defined(SKI)
109
        case CMD_IA64_GETCHAR:
109
        case CMD_IA64_GETCHAR:
110
            dstval = _getc(&ski_uconsole);
110
            dstval = _getc(&ski_uconsole);
111
            break;
111
            break;
112
#endif
112
#endif
113
#if defined(ppc32)
113
#if defined(ppc32)
114
        case CMD_PPC32_GETCHAR:
114
        case CMD_PPC32_GETCHAR:
115
            dstval = cuda_get_scancode();
115
            dstval = cuda_get_scancode();
116
            break;
116
            break;
117
#endif
117
#endif
118
        default:
118
        default:
119
            break;
119
            break;
120
        }
120
        }
121
        if (code->cmds[i].dstarg && code->cmds[i].dstarg < 4) {
121
        if (code->cmds[i].dstarg && code->cmds[i].dstarg < 4) {
122
            call->data.args[code->cmds[i].dstarg] = dstval;
122
            call->data.args[code->cmds[i].dstarg] = dstval;
123
        }
123
        }
124
    }
124
    }
125
}
125
}
126
 
126
 
127
static void code_free(irq_code_t *code)
127
static void code_free(irq_code_t *code)
128
{
128
{
129
    if (code) {
129
    if (code) {
130
        free(code->cmds);
130
        free(code->cmds);
131
        free(code);
131
        free(code);
132
    }
132
    }
133
}
133
}
134
 
134
 
135
static irq_code_t * code_from_uspace(irq_code_t *ucode)
135
static irq_code_t * code_from_uspace(irq_code_t *ucode)
136
{
136
{
137
    irq_code_t *code;
137
    irq_code_t *code;
138
    irq_cmd_t *ucmds;
138
    irq_cmd_t *ucmds;
139
    int rc;
139
    int rc;
140
 
140
 
141
    code = malloc(sizeof(*code), 0);
141
    code = malloc(sizeof(*code), 0);
142
    rc = copy_from_uspace(code, ucode, sizeof(*code));
142
    rc = copy_from_uspace(code, ucode, sizeof(*code));
143
    if (rc != 0) {
143
    if (rc != 0) {
144
        free(code);
144
        free(code);
145
        return NULL;
145
        return NULL;
146
    }
146
    }
147
   
147
   
148
    if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
148
    if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
149
        free(code);
149
        free(code);
150
        return NULL;
150
        return NULL;
151
    }
151
    }
152
    ucmds = code->cmds;
152
    ucmds = code->cmds;
153
    code->cmds = malloc(sizeof(code->cmds[0]) * (code->cmdcount), 0);
153
    code->cmds = malloc(sizeof(code->cmds[0]) * (code->cmdcount), 0);
154
    rc = copy_from_uspace(code->cmds, ucmds, sizeof(code->cmds[0]) * (code->cmdcount));
154
    rc = copy_from_uspace(code->cmds, ucmds, sizeof(code->cmds[0]) * (code->cmdcount));
155
    if (rc != 0) {
155
    if (rc != 0) {
156
        free(code->cmds);
156
        free(code->cmds);
157
        free(code);
157
        free(code);
158
        return NULL;
158
        return NULL;
159
    }
159
    }
160
 
160
 
161
    return code;
161
    return code;
162
}
162
}
163
 
163
 
164
/** Unregister task from IRQ notification.
164
/** Unregister task from IRQ notification.
165
 *
165
 *
166
 * @param box Answerbox associated with the notification.
166
 * @param box Answerbox associated with the notification.
167
 * @param inr IRQ numbe.
167
 * @param inr IRQ numbe.
168
 * @param devno Device number.
168
 * @param devno Device number.
169
 */
169
 */
170
void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
170
void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
171
{
171
{
172
    ipl_t ipl;
172
    ipl_t ipl;
173
    irq_t *irq;
173
    irq_t *irq;
174
 
174
 
175
    ipl = interrupts_disable();
175
    ipl = interrupts_disable();
176
    irq = irq_find_and_lock(inr, devno);
176
    irq = irq_find_and_lock(inr, devno);
177
    if (irq) {
177
    if (irq) {
178
        if (irq->notif_cfg.answerbox == box) {
178
        if (irq->notif_cfg.answerbox == box) {
179
            code_free(irq->notif_cfg.code);
179
            code_free(irq->notif_cfg.code);
180
            irq->notif_cfg.notify = false;
180
            irq->notif_cfg.notify = false;
181
            irq->notif_cfg.answerbox = NULL;
181
            irq->notif_cfg.answerbox = NULL;
182
            irq->notif_cfg.code = NULL;
182
            irq->notif_cfg.code = NULL;
183
            irq->notif_cfg.method = 0;
183
            irq->notif_cfg.method = 0;
184
            irq->notif_cfg.counter = 0;
184
            irq->notif_cfg.counter = 0;
185
 
185
 
186
            spinlock_lock(&box->irq_lock);
186
            spinlock_lock(&box->irq_lock);
187
            list_remove(&irq->notif_cfg.link);
187
            list_remove(&irq->notif_cfg.link);
188
            spinlock_unlock(&box->irq_lock);
188
            spinlock_unlock(&box->irq_lock);
189
           
189
           
190
            spinlock_unlock(&irq->lock);
190
            spinlock_unlock(&irq->lock);
191
        }
191
        }
192
    }
192
    }
193
    interrupts_restore(ipl);
193
    interrupts_restore(ipl);
194
}
194
}
195
 
195
 
196
/** Register an answerbox as a receiving end for IRQ notifications.
196
/** Register an answerbox as a receiving end for IRQ notifications.
197
 *
197
 *
198
 * @param box Receiving answerbox.
198
 * @param box Receiving answerbox.
199
 * @param inr IRQ number.
199
 * @param inr IRQ number.
200
 * @param devno Device number.
200
 * @param devno Device number.
201
 * @param method Method to be associated with the notification.
201
 * @param method Method to be associated with the notification.
202
 * @param ucode Uspace pointer to top-half pseudocode.
202
 * @param ucode Uspace pointer to top-half pseudocode.
203
 *
203
 *
204
 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
204
 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
205
 */
205
 */
206
int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, unative_t method, irq_code_t *ucode)
206
int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, unative_t method, irq_code_t *ucode)
207
{
207
{
208
    ipl_t ipl;
208
    ipl_t ipl;
209
    irq_code_t *code;
209
    irq_code_t *code;
210
    irq_t *irq;
210
    irq_t *irq;
211
 
211
 
212
    if (ucode) {
212
    if (ucode) {
213
        code = code_from_uspace(ucode);
213
        code = code_from_uspace(ucode);
214
        if (!code)
214
        if (!code)
215
            return EBADMEM;
215
            return EBADMEM;
216
    } else
216
    } else
217
        code = NULL;
217
        code = NULL;
218
 
218
 
219
    ipl = interrupts_disable();
219
    ipl = interrupts_disable();
220
    irq = irq_find_and_lock(inr, devno);
220
    irq = irq_find_and_lock(inr, devno);
221
    if (!irq) {
221
    if (!irq) {
222
        interrupts_restore(ipl);
222
        interrupts_restore(ipl);
223
        code_free(code);
223
        code_free(code);
224
        return ENOENT;
224
        return ENOENT;
225
    }
225
    }
226
   
226
   
227
    if (irq->notif_cfg.answerbox) {
227
    if (irq->notif_cfg.answerbox) {
228
        spinlock_unlock(&irq->lock);
228
        spinlock_unlock(&irq->lock);
229
        interrupts_restore(ipl);
229
        interrupts_restore(ipl);
230
        code_free(code);
230
        code_free(code);
231
        return EEXISTS;
231
        return EEXISTS;
232
    }
232
    }
233
   
233
   
234
    irq->notif_cfg.notify = true;
234
    irq->notif_cfg.notify = true;
235
    irq->notif_cfg.answerbox = box;
235
    irq->notif_cfg.answerbox = box;
236
    irq->notif_cfg.method = method;
236
    irq->notif_cfg.method = method;
237
    irq->notif_cfg.code = code;
237
    irq->notif_cfg.code = code;
238
    irq->notif_cfg.counter = 0;
238
    irq->notif_cfg.counter = 0;
239
 
239
 
240
    spinlock_lock(&box->irq_lock);
240
    spinlock_lock(&box->irq_lock);
241
    list_append(&irq->notif_cfg.link, &box->irq_head);
241
    list_append(&irq->notif_cfg.link, &box->irq_head);
242
    spinlock_unlock(&box->irq_lock);
242
    spinlock_unlock(&box->irq_lock);
243
 
243
 
244
    spinlock_unlock(&irq->lock);
244
    spinlock_unlock(&irq->lock);
245
    interrupts_restore(ipl);
245
    interrupts_restore(ipl);
246
 
246
 
247
    return 0;
247
    return 0;
248
}
248
}
249
 
249
 
250
/** Add call to proper answerbox queue.
250
/** Add call to proper answerbox queue.
251
 *
251
 *
252
 * Assume irq->lock is locked.
252
 * Assume irq->lock is locked.
253
 *
253
 *
254
 */
254
 */
255
static void send_call(irq_t *irq, call_t *call)
255
static void send_call(irq_t *irq, call_t *call)
256
{
256
{
257
    spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
257
    spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
258
    list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
258
    list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
259
    spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
259
    spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
260
       
260
       
261
    waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
261
    waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
262
}
262
}
263
 
263
 
264
/** Send notification message
264
/** Send notification message
265
 *
265
 *
266
 */
266
 */
267
void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3)
267
void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3)
268
{
268
{
269
    call_t *call;
269
    call_t *call;
270
 
270
 
271
    spinlock_lock(&irq->lock);
271
    spinlock_lock(&irq->lock);
272
 
272
 
273
    if (irq->notif_cfg.answerbox) {
273
    if (irq->notif_cfg.answerbox) {
274
        call = ipc_call_alloc(FRAME_ATOMIC);
274
        call = ipc_call_alloc(FRAME_ATOMIC);
275
        if (!call) {
275
        if (!call) {
276
            spinlock_unlock(&irq->lock);
276
            spinlock_unlock(&irq->lock);
277
            return;
277
            return;
278
        }
278
        }
279
        call->flags |= IPC_CALL_NOTIF;
279
        call->flags |= IPC_CALL_NOTIF;
280
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
280
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
281
        IPC_SET_ARG1(call->data, a1);
281
        IPC_SET_ARG1(call->data, a1);
282
        IPC_SET_ARG2(call->data, a2);
282
        IPC_SET_ARG2(call->data, a2);
283
        IPC_SET_ARG3(call->data, a3);
283
        IPC_SET_ARG3(call->data, a3);
284
        /* Put a counter to the message */
284
        /* Put a counter to the message */
285
        call->private = ++irq->notif_cfg.counter;
285
        call->private = ++irq->notif_cfg.counter;
286
       
286
       
287
        send_call(irq, call);
287
        send_call(irq, call);
288
    }
288
    }
289
    spinlock_unlock(&irq->lock);
289
    spinlock_unlock(&irq->lock);
290
}
290
}
291
 
291
 
292
/** Notify task that an irq had occurred.
292
/** Notify task that an irq had occurred.
293
 *
293
 *
294
 * We expect interrupts to be disabled and the irq->lock already held.
294
 * We expect interrupts to be disabled and the irq->lock already held.
295
 */
295
 */
296
void ipc_irq_send_notif(irq_t *irq)
296
void ipc_irq_send_notif(irq_t *irq)
297
{
297
{
298
    call_t *call;
298
    call_t *call;
299
 
299
 
300
    ASSERT(irq);
300
    ASSERT(irq);
301
 
301
 
302
    if (irq->notif_cfg.answerbox) {
302
    if (irq->notif_cfg.answerbox) {
303
        call = ipc_call_alloc(FRAME_ATOMIC);
303
        call = ipc_call_alloc(FRAME_ATOMIC);
304
        if (!call) {
304
        if (!call) {
305
            return;
305
            return;
306
        }
306
        }
307
        call->flags |= IPC_CALL_NOTIF;
307
        call->flags |= IPC_CALL_NOTIF;
308
        /* Put a counter to the message */
308
        /* Put a counter to the message */
309
        call->private = ++irq->notif_cfg.counter;
309
        call->private = ++irq->notif_cfg.counter;
310
        /* Set up args */
310
        /* Set up args */
311
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
311
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
312
 
312
 
313
        /* Execute code to handle irq */
313
        /* Execute code to handle irq */
314
        code_execute(call, irq->notif_cfg.code);
314
        code_execute(call, irq->notif_cfg.code);
315
       
315
       
316
        send_call(irq, call);
316
        send_call(irq, call);
317
    }
317
    }
318
}
318
}
319
 
319
 
320
/** Disconnect all IRQ notifications from an answerbox.
320
/** Disconnect all IRQ notifications from an answerbox.
321
 *
321
 *
322
 * This function is effective because the answerbox contains
322
 * This function is effective because the answerbox contains
323
 * list of all irq_t structures that are registered to
323
 * list of all irq_t structures that are registered to
324
 * send notifications to it.
324
 * send notifications to it.
325
 *
325
 *
326
 * @param box Answerbox for which we want to carry out the cleanup.
326
 * @param box Answerbox for which we want to carry out the cleanup.
327
 */
327
 */
328
void ipc_irq_cleanup(answerbox_t *box)
328
void ipc_irq_cleanup(answerbox_t *box)
329
{
329
{
330
    ipl_t ipl;
330
    ipl_t ipl;
331
   
331
   
332
loop:
332
loop:
333
    ipl = interrupts_disable();
333
    ipl = interrupts_disable();
334
    spinlock_lock(&box->irq_lock);
334
    spinlock_lock(&box->irq_lock);
335
   
335
   
336
    while (box->irq_head.next != &box->irq_head) {
336
    while (box->irq_head.next != &box->irq_head) {
337
        link_t *cur = box->irq_head.next;
337
        link_t *cur = box->irq_head.next;
338
        irq_t *irq;
338
        irq_t *irq;
339
       
339
       
340
        irq = list_get_instance(cur, irq_t, notif_cfg.link);
340
        irq = list_get_instance(cur, irq_t, notif_cfg.link);
341
        if (!spinlock_trylock(&irq->lock)) {
341
        if (!spinlock_trylock(&irq->lock)) {
342
            /*
342
            /*
343
             * Avoid deadlock by trying again.
343
             * Avoid deadlock by trying again.
344
             */
344
             */
345
            spinlock_unlock(&box->irq_lock);
345
            spinlock_unlock(&box->irq_lock);
346
            interrupts_restore(ipl);
346
            interrupts_restore(ipl);
347
            goto loop;
347
            goto loop;
348
        }
348
        }
349
       
349
       
350
        ASSERT(irq->notif_cfg.answerbox == box);
350
        ASSERT(irq->notif_cfg.answerbox == box);
351
       
351
       
352
        list_remove(&irq->notif_cfg.link);
352
        list_remove(&irq->notif_cfg.link);
353
       
353
       
354
        /*
354
        /*
355
         * Don't forget to free any top-half pseudocode.
355
         * Don't forget to free any top-half pseudocode.
356
         */
356
         */
357
        code_free(irq->notif_cfg.code);
357
        code_free(irq->notif_cfg.code);
358
       
358
       
359
        irq->notif_cfg.notify = false;
359
        irq->notif_cfg.notify = false;
360
        irq->notif_cfg.answerbox = NULL;
360
        irq->notif_cfg.answerbox = NULL;
361
        irq->notif_cfg.code = NULL;
361
        irq->notif_cfg.code = NULL;
362
        irq->notif_cfg.method = 0;
362
        irq->notif_cfg.method = 0;
363
        irq->notif_cfg.counter = 0;
363
        irq->notif_cfg.counter = 0;
364
 
364
 
365
        spinlock_unlock(&irq->lock);
365
        spinlock_unlock(&irq->lock);
366
    }
366
    }
367
   
367
   
368
    spinlock_unlock(&box->irq_lock);
368
    spinlock_unlock(&box->irq_lock);
369
    interrupts_restore(ipl);
369
    interrupts_restore(ipl);
370
}
370
}
371
 
371
 
372
/** @}
372
/** @}
373
 */
373
 */
374
 
374