Subversion Repositories HelenOS

Rev

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

Rev 4274 Rev 4687
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
 * - ARG4: payload modified by a 'top-half' handler
47
 * - ARG4: payload modified by a 'top-half' handler
48
 * - ARG5: payload modified by a 'top-half' handler
48
 * - ARG5: payload modified by a 'top-half' handler
49
 * - in_phone_hash: interrupt counter (may be needed to assure correct order
49
 * - in_phone_hash: interrupt counter (may be needed to assure correct order
50
 *         in multithreaded drivers)
50
 *         in multithreaded drivers)
51
 *
51
 *
52
 * Note on synchronization for ipc_irq_register(), ipc_irq_unregister(),
52
 * Note on synchronization for ipc_irq_register(), ipc_irq_unregister(),
53
 * ipc_irq_cleanup() and IRQ handlers:
53
 * ipc_irq_cleanup() and IRQ handlers:
54
 *
54
 *
55
 *   By always taking all of the uspace IRQ hash table lock, IRQ structure lock
55
 *   By always taking all of the uspace IRQ hash table lock, IRQ structure lock
56
 *   and answerbox lock, we can rule out race conditions between the
56
 *   and answerbox lock, we can rule out race conditions between the
57
 *   registration functions and also the cleanup function. Thus the observer can
57
 *   registration functions and also the cleanup function. Thus the observer can
58
 *   either see the IRQ structure present in both the hash table and the
58
 *   either see the IRQ structure present in both the hash table and the
59
 *   answerbox list or absent in both. Views in which the IRQ structure would be
59
 *   answerbox list or absent in both. Views in which the IRQ structure would be
60
 *   linked in the hash table but not in the answerbox list, or vice versa, are
60
 *   linked in the hash table but not in the answerbox list, or vice versa, are
61
 *   not possible.
61
 *   not possible.
62
 *
62
 *
63
 *   By always taking the hash table lock and the IRQ structure lock, we can
63
 *   By always taking the hash table lock and the IRQ structure lock, we can
64
 *   rule out a scenario in which we would free up an IRQ structure, which is
64
 *   rule out a scenario in which we would free up an IRQ structure, which is
65
 *   still referenced by, for example, an IRQ handler. The locking scheme forces
65
 *   still referenced by, for example, an IRQ handler. The locking scheme forces
66
 *   us to lock the IRQ structure only after any progressing IRQs on that
66
 *   us to lock the IRQ structure only after any progressing IRQs on that
67
 *   structure are finished. Because we hold the hash table lock, we prevent new
67
 *   structure are finished. Because we hold the hash table lock, we prevent new
68
 *   IRQs from taking new references to the IRQ structure.
68
 *   IRQs from taking new references to the IRQ structure.
69
 */
69
 */
70
 
70
 
71
#include <arch.h>
71
#include <arch.h>
72
#include <mm/slab.h>
72
#include <mm/slab.h>
73
#include <errno.h>
73
#include <errno.h>
74
#include <ddi/irq.h>
74
#include <ddi/irq.h>
75
#include <ipc/ipc.h>
75
#include <ipc/ipc.h>
76
#include <ipc/irq.h>
76
#include <ipc/irq.h>
77
#include <syscall/copy.h>
77
#include <syscall/copy.h>
78
#include <console/console.h>
78
#include <console/console.h>
79
#include <print.h>
79
#include <print.h>
80
 
80
 
81
/** Free the top-half pseudocode.
81
/** Free the top-half pseudocode.
82
 *
82
 *
83
 * @param code      Pointer to the top-half pseudocode.
83
 * @param code      Pointer to the top-half pseudocode.
84
 */
84
 */
85
static void code_free(irq_code_t *code)
85
static void code_free(irq_code_t *code)
86
{
86
{
87
    if (code) {
87
    if (code) {
88
        free(code->cmds);
88
        free(code->cmds);
89
        free(code);
89
        free(code);
90
    }
90
    }
91
}
91
}
92
 
92
 
93
/** Copy the top-half pseudocode from userspace into the kernel.
93
/** Copy the top-half pseudocode from userspace into the kernel.
94
 *
94
 *
95
 * @param ucode     Userspace address of the top-half pseudocode.
95
 * @param ucode     Userspace address of the top-half pseudocode.
96
 *
96
 *
97
 * @return      Kernel address of the copied pseudocode.
97
 * @return      Kernel address of the copied pseudocode.
98
 */
98
 */
99
static irq_code_t *code_from_uspace(irq_code_t *ucode)
99
static irq_code_t *code_from_uspace(irq_code_t *ucode)
100
{
100
{
101
    irq_code_t *code;
101
    irq_code_t *code;
102
    irq_cmd_t *ucmds;
102
    irq_cmd_t *ucmds;
103
    int rc;
103
    int rc;
104
 
104
 
105
    code = malloc(sizeof(*code), 0);
105
    code = malloc(sizeof(*code), 0);
106
    rc = copy_from_uspace(code, ucode, sizeof(*code));
106
    rc = copy_from_uspace(code, ucode, sizeof(*code));
107
    if (rc != 0) {
107
    if (rc != 0) {
108
        free(code);
108
        free(code);
109
        return NULL;
109
        return NULL;
110
    }
110
    }
111
   
111
   
112
    if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
112
    if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
113
        free(code);
113
        free(code);
114
        return NULL;
114
        return NULL;
115
    }
115
    }
116
    ucmds = code->cmds;
116
    ucmds = code->cmds;
117
    code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
117
    code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
118
    rc = copy_from_uspace(code->cmds, ucmds,
118
    rc = copy_from_uspace(code->cmds, ucmds,
119
        sizeof(code->cmds[0]) * code->cmdcount);
119
        sizeof(code->cmds[0]) * code->cmdcount);
120
    if (rc != 0) {
120
    if (rc != 0) {
121
        free(code->cmds);
121
        free(code->cmds);
122
        free(code);
122
        free(code);
123
        return NULL;
123
        return NULL;
124
    }
124
    }
125
 
125
 
126
    return code;
126
    return code;
127
}
127
}
128
 
128
 
129
/** Register an answerbox as a receiving end for IRQ notifications.
129
/** Register an answerbox as a receiving end for IRQ notifications.
130
 *
130
 *
131
 * @param box    Receiving answerbox.
131
 * @param box    Receiving answerbox.
132
 * @param inr    IRQ number.
132
 * @param inr    IRQ number.
133
 * @param devno  Device number.
133
 * @param devno  Device number.
134
 * @param method Method to be associated with the notification.
134
 * @param method Method to be associated with the notification.
135
 * @param ucode  Uspace pointer to top-half pseudocode.
135
 * @param ucode  Uspace pointer to top-half pseudocode.
136
 *
136
 *
137
 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
137
 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
138
 *
138
 *
139
 */
139
 */
140
int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
140
int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
141
    unative_t method, irq_code_t *ucode)
141
    unative_t method, irq_code_t *ucode)
142
{
142
{
143
    ipl_t ipl;
143
    ipl_t ipl;
144
    irq_code_t *code;
144
    irq_code_t *code;
145
    irq_t *irq;
145
    irq_t *irq;
146
    link_t *hlp;
146
    link_t *hlp;
147
    unative_t key[] = {
147
    unative_t key[] = {
148
        (unative_t) inr,
148
        (unative_t) inr,
149
        (unative_t) devno
149
        (unative_t) devno
150
    };
150
    };
151
   
151
   
152
    if (ucode) {
152
    if (ucode) {
153
        code = code_from_uspace(ucode);
153
        code = code_from_uspace(ucode);
154
        if (!code)
154
        if (!code)
155
            return EBADMEM;
155
            return EBADMEM;
156
    } else {
156
    } else {
157
        code = NULL;
157
        code = NULL;
158
    }
158
    }
159
   
159
   
160
    /*
160
    /*
161
     * Allocate and populate the IRQ structure.
161
     * Allocate and populate the IRQ structure.
162
     */
162
     */
163
    irq = malloc(sizeof(irq_t), 0);
163
    irq = malloc(sizeof(irq_t), 0);
164
    irq_initialize(irq);
164
    irq_initialize(irq);
165
    irq->devno = devno;
165
    irq->devno = devno;
166
    irq->inr = inr;
166
    irq->inr = inr;
167
    irq->claim = ipc_irq_top_half_claim;
167
    irq->claim = ipc_irq_top_half_claim;
168
    irq->handler = ipc_irq_top_half_handler;
168
    irq->handler = ipc_irq_top_half_handler;
169
    irq->notif_cfg.notify = true;
169
    irq->notif_cfg.notify = true;
170
    irq->notif_cfg.answerbox = box;
170
    irq->notif_cfg.answerbox = box;
171
    irq->notif_cfg.method = method;
171
    irq->notif_cfg.method = method;
172
    irq->notif_cfg.code = code;
172
    irq->notif_cfg.code = code;
173
    irq->notif_cfg.counter = 0;
173
    irq->notif_cfg.counter = 0;
174
   
174
   
175
    /*
175
    /*
176
     * Enlist the IRQ structure in the uspace IRQ hash table and the
176
     * Enlist the IRQ structure in the uspace IRQ hash table and the
177
     * answerbox's list.
177
     * answerbox's list.
178
     */
178
     */
179
    ipl = interrupts_disable();
179
    ipl = interrupts_disable();
180
    spinlock_lock(&irq_uspace_hash_table_lock);
180
    spinlock_lock(&irq_uspace_hash_table_lock);
181
    hlp = hash_table_find(&irq_uspace_hash_table, key);
181
    hlp = hash_table_find(&irq_uspace_hash_table, key);
182
    if (hlp) {
182
    if (hlp) {
183
        irq_t *hirq __attribute__((unused))
183
        irq_t *hirq __attribute__((unused))
184
            = hash_table_get_instance(hlp, irq_t, link);
184
            = hash_table_get_instance(hlp, irq_t, link);
185
       
185
       
186
        /* hirq is locked */
186
        /* hirq is locked */
187
        spinlock_unlock(&hirq->lock);
187
        spinlock_unlock(&hirq->lock);
188
        code_free(code);
188
        code_free(code);
189
        spinlock_unlock(&irq_uspace_hash_table_lock);
189
        spinlock_unlock(&irq_uspace_hash_table_lock);
190
        free(irq);
190
        free(irq);
191
        interrupts_restore(ipl);
191
        interrupts_restore(ipl);
192
        return EEXISTS;
192
        return EEXISTS;
193
    }
193
    }
194
   
194
   
195
    spinlock_lock(&irq->lock);  /* Not really necessary, but paranoid */
195
    spinlock_lock(&irq->lock);  /* Not really necessary, but paranoid */
196
    spinlock_lock(&box->irq_lock);
196
    spinlock_lock(&box->irq_lock);
197
    hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
197
    hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
198
    list_append(&irq->notif_cfg.link, &box->irq_head);
198
    list_append(&irq->notif_cfg.link, &box->irq_head);
199
    spinlock_unlock(&box->irq_lock);
199
    spinlock_unlock(&box->irq_lock);
200
    spinlock_unlock(&irq->lock);
200
    spinlock_unlock(&irq->lock);
201
    spinlock_unlock(&irq_uspace_hash_table_lock);
201
    spinlock_unlock(&irq_uspace_hash_table_lock);
202
   
202
   
203
    interrupts_restore(ipl);
203
    interrupts_restore(ipl);
204
    return EOK;
204
    return EOK;
205
}
205
}
206
 
206
 
207
/** Unregister task from IRQ notification.
207
/** Unregister task from IRQ notification.
208
 *
208
 *
209
 * @param box       Answerbox associated with the notification.
209
 * @param box       Answerbox associated with the notification.
210
 * @param inr       IRQ number.
210
 * @param inr       IRQ number.
211
 * @param devno     Device number.
211
 * @param devno     Device number.
212
 */
212
 */
213
int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
213
int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
214
{
214
{
215
    ipl_t ipl;
215
    ipl_t ipl;
216
    unative_t key[] = {
216
    unative_t key[] = {
217
        (unative_t) inr,
217
        (unative_t) inr,
218
        (unative_t) devno
218
        (unative_t) devno
219
    };
219
    };
220
    link_t *lnk;
220
    link_t *lnk;
221
    irq_t *irq;
221
    irq_t *irq;
222
 
222
 
223
    ipl = interrupts_disable();
223
    ipl = interrupts_disable();
224
    spinlock_lock(&irq_uspace_hash_table_lock);
224
    spinlock_lock(&irq_uspace_hash_table_lock);
225
    lnk = hash_table_find(&irq_uspace_hash_table, key);
225
    lnk = hash_table_find(&irq_uspace_hash_table, key);
226
    if (!lnk) {
226
    if (!lnk) {
227
        spinlock_unlock(&irq_uspace_hash_table_lock);
227
        spinlock_unlock(&irq_uspace_hash_table_lock);
228
        interrupts_restore(ipl);
228
        interrupts_restore(ipl);
229
        return ENOENT;
229
        return ENOENT;
230
    }
230
    }
231
    irq = hash_table_get_instance(lnk, irq_t, link);
231
    irq = hash_table_get_instance(lnk, irq_t, link);
232
    /* irq is locked */
232
    /* irq is locked */
233
    spinlock_lock(&box->irq_lock);
233
    spinlock_lock(&box->irq_lock);
234
   
234
   
235
    ASSERT(irq->notif_cfg.answerbox == box);
235
    ASSERT(irq->notif_cfg.answerbox == box);
236
   
236
   
237
    /* Free up the pseudo code and associated structures. */
237
    /* Free up the pseudo code and associated structures. */
238
    code_free(irq->notif_cfg.code);
238
    code_free(irq->notif_cfg.code);
239
 
239
 
240
    /* Remove the IRQ from the answerbox's list. */
240
    /* Remove the IRQ from the answerbox's list. */
241
    list_remove(&irq->notif_cfg.link);
241
    list_remove(&irq->notif_cfg.link);
242
 
242
 
243
    /*
243
    /*
244
     * We need to drop the IRQ lock now because hash_table_remove() will try
244
     * We need to drop the IRQ lock now because hash_table_remove() will try
245
     * to reacquire it. That basically violates the natural locking order,
245
     * to reacquire it. That basically violates the natural locking order,
246
     * but a deadlock in hash_table_remove() is prevented by the fact that
246
     * but a deadlock in hash_table_remove() is prevented by the fact that
247
     * we already held the IRQ lock and didn't drop the hash table lock in
247
     * we already held the IRQ lock and didn't drop the hash table lock in
248
     * the meantime.
248
     * the meantime.
249
     */
249
     */
250
    spinlock_unlock(&irq->lock);
250
    spinlock_unlock(&irq->lock);
251
 
251
 
252
    /* Remove the IRQ from the uspace IRQ hash table. */
252
    /* Remove the IRQ from the uspace IRQ hash table. */
253
    hash_table_remove(&irq_uspace_hash_table, key, 2);
253
    hash_table_remove(&irq_uspace_hash_table, key, 2);
254
   
254
   
255
    spinlock_unlock(&irq_uspace_hash_table_lock);
255
    spinlock_unlock(&irq_uspace_hash_table_lock);
256
    spinlock_unlock(&box->irq_lock);
256
    spinlock_unlock(&box->irq_lock);
257
   
257
   
258
    /* Free up the IRQ structure. */
258
    /* Free up the IRQ structure. */
259
    free(irq);
259
    free(irq);
260
   
260
   
261
    interrupts_restore(ipl);
261
    interrupts_restore(ipl);
262
    return EOK;
262
    return EOK;
263
}
263
}
264
 
264
 
265
 
265
 
266
/** Disconnect all IRQ notifications from an answerbox.
266
/** Disconnect all IRQ notifications from an answerbox.
267
 *
267
 *
268
 * This function is effective because the answerbox contains
268
 * This function is effective because the answerbox contains
269
 * list of all irq_t structures that are registered to
269
 * list of all irq_t structures that are registered to
270
 * send notifications to it.
270
 * send notifications to it.
271
 *
271
 *
272
 * @param box       Answerbox for which we want to carry out the cleanup.
272
 * @param box       Answerbox for which we want to carry out the cleanup.
273
 */
273
 */
274
void ipc_irq_cleanup(answerbox_t *box)
274
void ipc_irq_cleanup(answerbox_t *box)
275
{
275
{
276
    ipl_t ipl;
276
    ipl_t ipl;
277
   
277
   
278
loop:
278
loop:
279
    ipl = interrupts_disable();
279
    ipl = interrupts_disable();
280
    spinlock_lock(&irq_uspace_hash_table_lock);
280
    spinlock_lock(&irq_uspace_hash_table_lock);
281
    spinlock_lock(&box->irq_lock);
281
    spinlock_lock(&box->irq_lock);
282
   
282
   
283
    while (box->irq_head.next != &box->irq_head) {
283
    while (box->irq_head.next != &box->irq_head) {
284
        link_t *cur = box->irq_head.next;
284
        link_t *cur = box->irq_head.next;
285
        irq_t *irq;
285
        irq_t *irq;
286
        DEADLOCK_PROBE_INIT(p_irqlock);
286
        DEADLOCK_PROBE_INIT(p_irqlock);
287
        unative_t key[2];
287
        unative_t key[2];
288
       
288
       
289
        irq = list_get_instance(cur, irq_t, notif_cfg.link);
289
        irq = list_get_instance(cur, irq_t, notif_cfg.link);
290
        if (!spinlock_trylock(&irq->lock)) {
290
        if (!spinlock_trylock(&irq->lock)) {
291
            /*
291
            /*
292
             * Avoid deadlock by trying again.
292
             * Avoid deadlock by trying again.
293
             */
293
             */
294
            spinlock_unlock(&box->irq_lock);
294
            spinlock_unlock(&box->irq_lock);
295
            spinlock_unlock(&irq_uspace_hash_table_lock);
295
            spinlock_unlock(&irq_uspace_hash_table_lock);
296
            interrupts_restore(ipl);
296
            interrupts_restore(ipl);
297
            DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
297
            DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
298
            goto loop;
298
            goto loop;
299
        }
299
        }
300
        key[0] = irq->inr;
300
        key[0] = irq->inr;
301
        key[1] = irq->devno;
301
        key[1] = irq->devno;
302
       
302
       
303
       
303
       
304
        ASSERT(irq->notif_cfg.answerbox == box);
304
        ASSERT(irq->notif_cfg.answerbox == box);
305
       
305
       
306
        /* Unlist from the answerbox. */
306
        /* Unlist from the answerbox. */
307
        list_remove(&irq->notif_cfg.link);
307
        list_remove(&irq->notif_cfg.link);
308
       
308
       
309
        /* Free up the pseudo code and associated structures. */
309
        /* Free up the pseudo code and associated structures. */
310
        code_free(irq->notif_cfg.code);
310
        code_free(irq->notif_cfg.code);
311
       
311
       
312
        /*
312
        /*
313
         * We need to drop the IRQ lock now because hash_table_remove()
313
         * We need to drop the IRQ lock now because hash_table_remove()
314
         * will try to reacquire it. That basically violates the natural
314
         * will try to reacquire it. That basically violates the natural
315
         * locking order, but a deadlock in hash_table_remove() is
315
         * locking order, but a deadlock in hash_table_remove() is
316
         * prevented by the fact that we already held the IRQ lock and
316
         * prevented by the fact that we already held the IRQ lock and
317
         * didn't drop the hash table lock in the meantime.
317
         * didn't drop the hash table lock in the meantime.
318
         */
318
         */
319
        spinlock_unlock(&irq->lock);
319
        spinlock_unlock(&irq->lock);
320
       
320
       
321
        /* Remove from the hash table. */
321
        /* Remove from the hash table. */
322
        hash_table_remove(&irq_uspace_hash_table, key, 2);
322
        hash_table_remove(&irq_uspace_hash_table, key, 2);
323
       
323
       
324
        free(irq);
324
        free(irq);
325
    }
325
    }
326
   
326
   
327
    spinlock_unlock(&box->irq_lock);
327
    spinlock_unlock(&box->irq_lock);
328
    spinlock_unlock(&irq_uspace_hash_table_lock);
328
    spinlock_unlock(&irq_uspace_hash_table_lock);
329
    interrupts_restore(ipl);
329
    interrupts_restore(ipl);
330
}
330
}
331
 
331
 
332
/** Add a call to the proper answerbox queue.
332
/** Add a call to the proper answerbox queue.
333
 *
333
 *
334
 * Assume irq->lock is locked.
334
 * Assume irq->lock is locked.
335
 *
335
 *
336
 * @param irq       IRQ structure referencing the target answerbox.
336
 * @param irq       IRQ structure referencing the target answerbox.
337
 * @param call      IRQ notification call.
337
 * @param call      IRQ notification call.
338
 */
338
 */
339
static void send_call(irq_t *irq, call_t *call)
339
static void send_call(irq_t *irq, call_t *call)
340
{
340
{
341
    spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
341
    spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
342
    list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
342
    list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
343
    spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
343
    spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
344
       
344
       
345
    waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
345
    waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
346
}
346
}
347
 
347
 
348
/** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
348
/** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
349
 *
349
 *
350
 * @param irq       IRQ structure.
350
 * @param irq       IRQ structure.
351
 *
351
 *
352
 * @return      IRQ_ACCEPT if the interrupt is accepted by the
352
 * @return      IRQ_ACCEPT if the interrupt is accepted by the
353
 *          pseudocode. IRQ_DECLINE otherwise.
353
 *          pseudocode. IRQ_DECLINE otherwise.
354
 */
354
 */
355
irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
355
irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
356
{
356
{
357
    unsigned int i;
357
    unsigned int i;
358
    unative_t dstval;
358
    unative_t dstval;
359
    irq_code_t *code = irq->notif_cfg.code;
359
    irq_code_t *code = irq->notif_cfg.code;
360
    unative_t *scratch = irq->notif_cfg.scratch;
360
    unative_t *scratch = irq->notif_cfg.scratch;
361
 
361
 
362
   
362
   
363
    if (!irq->notif_cfg.notify)
363
    if (!irq->notif_cfg.notify)
364
        return IRQ_DECLINE;
364
        return IRQ_DECLINE;
365
   
365
   
366
    if (!code)
366
    if (!code)
367
        return IRQ_DECLINE;
367
        return IRQ_DECLINE;
368
   
368
   
369
    for (i = 0; i < code->cmdcount; i++) {
369
    for (i = 0; i < code->cmdcount; i++) {
370
        unsigned int srcarg = code->cmds[i].srcarg;
370
        unsigned int srcarg = code->cmds[i].srcarg;
371
        unsigned int dstarg = code->cmds[i].dstarg;
371
        unsigned int dstarg = code->cmds[i].dstarg;
372
       
372
       
373
        if (srcarg >= IPC_CALL_LEN)
373
        if (srcarg >= IPC_CALL_LEN)
374
            break;
374
            break;
375
        if (dstarg >= IPC_CALL_LEN)
375
        if (dstarg >= IPC_CALL_LEN)
376
            break;
376
            break;
377
   
377
   
378
        switch (code->cmds[i].cmd) {
378
        switch (code->cmds[i].cmd) {
379
        case CMD_PIO_READ_8:
379
        case CMD_PIO_READ_8:
380
            dstval = pio_read_8((ioport8_t *) code->cmds[i].addr);
380
            dstval = pio_read_8((ioport8_t *) code->cmds[i].addr);
381
            if (dstarg)
381
            if (dstarg)
382
                scratch[dstarg] = dstval;
382
                scratch[dstarg] = dstval;
383
            break;
383
            break;
384
        case CMD_PIO_READ_16:
384
        case CMD_PIO_READ_16:
385
            dstval = pio_read_16((ioport16_t *) code->cmds[i].addr);
385
            dstval = pio_read_16((ioport16_t *) code->cmds[i].addr);
386
            if (dstarg)
386
            if (dstarg)
387
                scratch[dstarg] = dstval;
387
                scratch[dstarg] = dstval;
388
            break;
388
            break;
389
        case CMD_PIO_READ_32:
389
        case CMD_PIO_READ_32:
390
            dstval = pio_read_32((ioport32_t *) code->cmds[i].addr);
390
            dstval = pio_read_32((ioport32_t *) code->cmds[i].addr);
391
            if (dstarg)
391
            if (dstarg)
392
                scratch[dstarg] = dstval;
392
                scratch[dstarg] = dstval;
393
            break;
393
            break;
394
        case CMD_PIO_WRITE_8:
394
        case CMD_PIO_WRITE_8:
395
            pio_write_8((ioport8_t *) code->cmds[i].addr,
395
            pio_write_8((ioport8_t *) code->cmds[i].addr,
396
                (uint8_t) code->cmds[i].value);
396
                (uint8_t) code->cmds[i].value);
397
            break;
397
            break;
398
        case CMD_PIO_WRITE_16:
398
        case CMD_PIO_WRITE_16:
399
            pio_write_16((ioport16_t *) code->cmds[i].addr,
399
            pio_write_16((ioport16_t *) code->cmds[i].addr,
400
                (uint16_t) code->cmds[i].value);
400
                (uint16_t) code->cmds[i].value);
401
            break;
401
            break;
402
        case CMD_PIO_WRITE_32:
402
        case CMD_PIO_WRITE_32:
403
            pio_write_32((ioport32_t *) code->cmds[i].addr,
403
            pio_write_32((ioport32_t *) code->cmds[i].addr,
404
                (uint32_t) code->cmds[i].value);
404
                (uint32_t) code->cmds[i].value);
405
            break;
405
            break;
406
        case CMD_BTEST:
406
        case CMD_BTEST:
407
            if (srcarg && dstarg) {
407
            if (srcarg && dstarg) {
408
                dstval = scratch[srcarg] & code->cmds[i].value;
408
                dstval = scratch[srcarg] & code->cmds[i].value;
409
                scratch[dstarg] = dstval;
409
                scratch[dstarg] = dstval;
410
            }
410
            }
411
            break;
411
            break;
412
        case CMD_PREDICATE:
412
        case CMD_PREDICATE:
413
            if (srcarg && !scratch[srcarg]) {
413
            if (srcarg && !scratch[srcarg]) {
414
                i += code->cmds[i].value;
414
                i += code->cmds[i].value;
415
                continue;
415
                continue;
416
            }
416
            }
417
            break;
417
            break;
418
        case CMD_ACCEPT:
418
        case CMD_ACCEPT:
419
            return IRQ_ACCEPT;
419
            return IRQ_ACCEPT;
420
            break;
420
            break;
421
        case CMD_DECLINE:
421
        case CMD_DECLINE:
422
        default:
422
        default:
423
            return IRQ_DECLINE;
423
            return IRQ_DECLINE;
424
        }
424
        }
425
    }
425
    }
426
   
426
   
427
    return IRQ_DECLINE;
427
    return IRQ_DECLINE;
428
}
428
}
429
 
429
 
430
 
430
 
431
/* IRQ top-half handler.
431
/* IRQ top-half handler.
432
 *
432
 *
433
 * We expect interrupts to be disabled and the irq->lock already held.
433
 * We expect interrupts to be disabled and the irq->lock already held.
434
 *
434
 *
435
 * @param irq       IRQ structure.
435
 * @param irq       IRQ structure.
436
 */
436
 */
437
void ipc_irq_top_half_handler(irq_t *irq)
437
void ipc_irq_top_half_handler(irq_t *irq)
438
{
438
{
439
    ASSERT(irq);
439
    ASSERT(irq);
440
 
440
 
441
    if (irq->notif_cfg.answerbox) {
441
    if (irq->notif_cfg.answerbox) {
442
        call_t *call;
442
        call_t *call;
443
 
443
 
444
        call = ipc_call_alloc(FRAME_ATOMIC);
444
        call = ipc_call_alloc(FRAME_ATOMIC);
445
        if (!call)
445
        if (!call)
446
            return;
446
            return;
447
       
447
       
448
        call->flags |= IPC_CALL_NOTIF;
448
        call->flags |= IPC_CALL_NOTIF;
449
        /* Put a counter to the message */
449
        /* Put a counter to the message */
450
        call->priv = ++irq->notif_cfg.counter;
450
        call->priv = ++irq->notif_cfg.counter;
451
 
451
 
452
        /* Set up args */
452
        /* Set up args */
453
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
453
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
454
        IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]);
454
        IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]);
455
        IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]);
455
        IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]);
456
        IPC_SET_ARG3(call->data, irq->notif_cfg.scratch[3]);
456
        IPC_SET_ARG3(call->data, irq->notif_cfg.scratch[3]);
457
        IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
457
        IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
458
        IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
458
        IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
459
 
459
 
460
        send_call(irq, call);
460
        send_call(irq, call);
461
    }
461
    }
462
}
462
}
463
 
463
 
464
/** Send notification message.
464
/** Send notification message.
465
 *
465
 *
466
 * @param irq       IRQ structure.
466
 * @param irq       IRQ structure.
467
 * @param a1        Driver-specific payload argument.
467
 * @param a1        Driver-specific payload argument.
468
 * @param a2        Driver-specific payload argument.
468
 * @param a2        Driver-specific payload argument.
469
 * @param a3        Driver-specific payload argument.
469
 * @param a3        Driver-specific payload argument.
470
 * @param a4        Driver-specific payload argument.
470
 * @param a4        Driver-specific payload argument.
471
 * @param a5        Driver-specific payload argument.
471
 * @param a5        Driver-specific payload argument.
472
 */
472
 */
473
void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3,
473
void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3,
474
    unative_t a4, unative_t a5)
474
    unative_t a4, unative_t a5)
475
{
475
{
476
    call_t *call;
476
    call_t *call;
477
 
477
 
478
    spinlock_lock(&irq->lock);
478
    spinlock_lock(&irq->lock);
479
 
479
 
480
    if (irq->notif_cfg.answerbox) {
480
    if (irq->notif_cfg.answerbox) {
481
        call = ipc_call_alloc(FRAME_ATOMIC);
481
        call = ipc_call_alloc(FRAME_ATOMIC);
482
        if (!call) {
482
        if (!call) {
483
            spinlock_unlock(&irq->lock);
483
            spinlock_unlock(&irq->lock);
484
            return;
484
            return;
485
        }
485
        }
486
        call->flags |= IPC_CALL_NOTIF;
486
        call->flags |= IPC_CALL_NOTIF;
487
        /* Put a counter to the message */
487
        /* Put a counter to the message */
488
        call->priv = ++irq->notif_cfg.counter;
488
        call->priv = ++irq->notif_cfg.counter;
489
 
489
 
490
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
490
        IPC_SET_METHOD(call->data, irq->notif_cfg.method);
491
        IPC_SET_ARG1(call->data, a1);
491
        IPC_SET_ARG1(call->data, a1);
492
        IPC_SET_ARG2(call->data, a2);
492
        IPC_SET_ARG2(call->data, a2);
493
        IPC_SET_ARG3(call->data, a3);
493
        IPC_SET_ARG3(call->data, a3);
494
        IPC_SET_ARG4(call->data, a4);
494
        IPC_SET_ARG4(call->data, a4);
495
        IPC_SET_ARG5(call->data, a5);
495
        IPC_SET_ARG5(call->data, a5);
496
       
496
       
497
        send_call(irq, call);
497
        send_call(irq, call);
498
    }
498
    }
499
    spinlock_unlock(&irq->lock);
499
    spinlock_unlock(&irq->lock);
500
}
500
}
501
 
501
 
502
/** @}
502
/** @}
503
 */
503
 */
504
 
504