Subversion Repositories HelenOS

Rev

Rev 3665 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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