Subversion Repositories HelenOS

Rev

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