Subversion Repositories HelenOS

Rev

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