Subversion Repositories HelenOS

Rev

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