Subversion Repositories HelenOS

Rev

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