Subversion Repositories HelenOS

Rev

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