source: mainline/kernel/generic/src/ipc/irq.c@ 40dc422

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 40dc422 was 228e490, checked in by Martin Decky <martin@…>, 15 years ago

initial modifications for supporting declarative IPC interfaces

  • Property mode set to 100644
File size: 13.9 KB
RevLine 
[162f919]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2006 Jakub Jermar
[162f919]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
[cc73a8a1]30/** @addtogroup genericipc
[b45c443]31 * @{
32 */
[da1bafb]33
[cc73a8a1]34/**
35 * @file
36 * @brief IRQ notification framework.
[bdc5c516]37 *
38 * This framework allows applications to register to receive a notification
39 * when interrupt is detected. The application may provide a simple 'top-half'
40 * handler as part of its registration, which can perform simple operations
41 * (read/write port/memory, add information to notification ipc message).
42 *
43 * The structure of a notification message is as follows:
[228e490]44 * - IMETHOD: interface and method as registered by the SYS_IPC_REGISTER_IRQ
45 * syscall
[43752b6]46 * - ARG1: payload modified by a 'top-half' handler
[2b017ba]47 * - ARG2: payload modified by a 'top-half' handler
48 * - ARG3: payload modified by a 'top-half' handler
[cecb0789]49 * - ARG4: payload modified by a 'top-half' handler
50 * - ARG5: payload modified by a 'top-half' handler
[43752b6]51 * - in_phone_hash: interrupt counter (may be needed to assure correct order
[228e490]52 * in multithreaded drivers)
[cecb0789]53 *
54 * Note on synchronization for ipc_irq_register(), ipc_irq_unregister(),
55 * ipc_irq_cleanup() and IRQ handlers:
56 *
57 * By always taking all of the uspace IRQ hash table lock, IRQ structure lock
58 * and answerbox lock, we can rule out race conditions between the
59 * registration functions and also the cleanup function. Thus the observer can
60 * either see the IRQ structure present in both the hash table and the
61 * answerbox list or absent in both. Views in which the IRQ structure would be
62 * linked in the hash table but not in the answerbox list, or vice versa, are
63 * not possible.
64 *
65 * By always taking the hash table lock and the IRQ structure lock, we can
66 * rule out a scenario in which we would free up an IRQ structure, which is
67 * still referenced by, for example, an IRQ handler. The locking scheme forces
68 * us to lock the IRQ structure only after any progressing IRQs on that
69 * structure are finished. Because we hold the hash table lock, we prevent new
70 * IRQs from taking new references to the IRQ structure.
[da1bafb]71 *
[bdc5c516]72 */
73
[162f919]74#include <arch.h>
75#include <mm/slab.h>
76#include <errno.h>
[2b017ba]77#include <ddi/irq.h>
[162f919]78#include <ipc/ipc.h>
79#include <ipc/irq.h>
[e3c762cd]80#include <syscall/copy.h>
[d0c5901]81#include <console/console.h>
[253f35a1]82#include <print.h>
[162f919]83
[cecb0789]84/** Free the top-half pseudocode.
[8b243f2]85 *
[da1bafb]86 * @param code Pointer to the top-half pseudocode.
87 *
[8b243f2]88 */
[162f919]89static void code_free(irq_code_t *code)
90{
91 if (code) {
92 free(code->cmds);
93 free(code);
94 }
95}
96
[cecb0789]97/** Copy the top-half pseudocode from userspace into the kernel.
[8b243f2]98 *
[da1bafb]99 * @param ucode Userspace address of the top-half pseudocode.
100 *
101 * @return Kernel address of the copied pseudocode.
[8b243f2]102 *
103 */
104static irq_code_t *code_from_uspace(irq_code_t *ucode)
[162f919]105{
[da1bafb]106 irq_code_t *code = malloc(sizeof(*code), 0);
107 int rc = copy_from_uspace(code, ucode, sizeof(*code));
[e3c762cd]108 if (rc != 0) {
109 free(code);
110 return NULL;
111 }
[162f919]112
113 if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
114 free(code);
115 return NULL;
116 }
[da1bafb]117
118 irq_cmd_t *ucmds = code->cmds;
[8b243f2]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);
[e3c762cd]122 if (rc != 0) {
123 free(code->cmds);
124 free(code);
125 return NULL;
126 }
[da1bafb]127
[162f919]128 return code;
129}
130
[2b017ba]131/** Register an answerbox as a receiving end for IRQ notifications.
132 *
[228e490]133 * @param box Receiving answerbox.
134 * @param inr IRQ number.
135 * @param devno Device number.
136 * @param imethod Interface and method to be associated
137 * with the notification.
138 * @param ucode Uspace pointer to top-half pseudocode.
[c822026]139 *
140 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
[2b017ba]141 *
142 */
[8b243f2]143int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
[228e490]144 sysarg_t imethod, irq_code_t *ucode)
[162f919]145{
[96b02eb9]146 sysarg_t key[] = {
147 (sysarg_t) inr,
148 (sysarg_t) devno
[cecb0789]149 };
[c822026]150
[da1bafb]151 irq_code_t *code;
[162f919]152 if (ucode) {
153 code = code_from_uspace(ucode);
154 if (!code)
155 return EBADMEM;
[da1bafb]156 } else
[162f919]157 code = NULL;
[c822026]158
[cecb0789]159 /*
160 * Allocate and populate the IRQ structure.
161 */
[da1bafb]162 irq_t *irq = malloc(sizeof(irq_t), 0);
163
[cecb0789]164 irq_initialize(irq);
165 irq->devno = devno;
166 irq->inr = inr;
167 irq->claim = ipc_irq_top_half_claim;
[691eb52]168 irq->handler = ipc_irq_top_half_handler;
[4874c2d]169 irq->notif_cfg.notify = true;
[2b017ba]170 irq->notif_cfg.answerbox = box;
[228e490]171 irq->notif_cfg.imethod = imethod;
[2b017ba]172 irq->notif_cfg.code = code;
173 irq->notif_cfg.counter = 0;
[c822026]174
[cecb0789]175 /*
176 * Enlist the IRQ structure in the uspace IRQ hash table and the
177 * answerbox's list.
178 */
[da1bafb]179 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
180
181 link_t *hlp = hash_table_find(&irq_uspace_hash_table, key);
[2845930]182 if (hlp) {
[da1bafb]183 irq_t *hirq = hash_table_get_instance(hlp, irq_t, link);
[c822026]184
[2845930]185 /* hirq is locked */
[da1bafb]186 irq_spinlock_unlock(&hirq->lock, false);
[cecb0789]187 code_free(code);
[da1bafb]188 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
189
[cecb0789]190 free(irq);
191 return EEXISTS;
192 }
[c822026]193
[da1bafb]194 /* Locking is not really necessary, but paranoid */
195 irq_spinlock_lock(&irq->lock, false);
196 irq_spinlock_lock(&box->irq_lock, false);
197
[cecb0789]198 hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
[b14e35f2]199 list_append(&irq->notif_cfg.link, &box->irq_head);
[c822026]200
[da1bafb]201 irq_spinlock_unlock(&box->irq_lock, false);
202 irq_spinlock_unlock(&irq->lock, false);
203 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
204
[cecb0789]205 return EOK;
206}
207
208/** Unregister task from IRQ notification.
209 *
[da1bafb]210 * @param box Answerbox associated with the notification.
211 * @param inr IRQ number.
212 * @param devno Device number.
213 *
[cecb0789]214 */
215int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
216{
[96b02eb9]217 sysarg_t key[] = {
218 (sysarg_t) inr,
219 (sysarg_t) devno
[cecb0789]220 };
[da1bafb]221
222 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
223 link_t *lnk = hash_table_find(&irq_uspace_hash_table, key);
[cecb0789]224 if (!lnk) {
[da1bafb]225 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[cecb0789]226 return ENOENT;
227 }
[da1bafb]228
229 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
230
[2845930]231 /* irq is locked */
[da1bafb]232 irq_spinlock_lock(&box->irq_lock, false);
[cecb0789]233
234 ASSERT(irq->notif_cfg.answerbox == box);
235
236 /* Free up the pseudo code and associated structures. */
237 code_free(irq->notif_cfg.code);
[da1bafb]238
239 /* Remove the IRQ from the answerbox's list. */
[cecb0789]240 list_remove(&irq->notif_cfg.link);
[da1bafb]241
[2845930]242 /*
243 * We need to drop the IRQ lock now because hash_table_remove() will try
244 * to reacquire it. That basically violates the natural locking order,
245 * but a deadlock in hash_table_remove() is prevented by the fact that
246 * we already held the IRQ lock and didn't drop the hash table lock in
247 * the meantime.
248 */
[da1bafb]249 irq_spinlock_unlock(&irq->lock, false);
250
[cecb0789]251 /* Remove the IRQ from the uspace IRQ hash table. */
252 hash_table_remove(&irq_uspace_hash_table, key, 2);
253
[da1bafb]254 irq_spinlock_unlock(&box->irq_lock, false);
255 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[cecb0789]256
257 /* Free up the IRQ structure. */
258 free(irq);
259
260 return EOK;
261}
262
263/** Disconnect all IRQ notifications from an answerbox.
264 *
265 * This function is effective because the answerbox contains
266 * list of all irq_t structures that are registered to
267 * send notifications to it.
268 *
[da1bafb]269 * @param box Answerbox for which we want to carry out the cleanup.
270 *
[cecb0789]271 */
272void ipc_irq_cleanup(answerbox_t *box)
273{
274loop:
[da1bafb]275 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
276 irq_spinlock_lock(&box->irq_lock, false);
[cecb0789]277
278 while (box->irq_head.next != &box->irq_head) {
279 DEADLOCK_PROBE_INIT(p_irqlock);
280
[da1bafb]281 irq_t *irq = list_get_instance(box->irq_head.next, irq_t,
282 notif_cfg.link);
283
284 if (!irq_spinlock_trylock(&irq->lock)) {
[cecb0789]285 /*
286 * Avoid deadlock by trying again.
287 */
[da1bafb]288 irq_spinlock_unlock(&box->irq_lock, false);
289 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[cecb0789]290 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
291 goto loop;
292 }
[da1bafb]293
[96b02eb9]294 sysarg_t key[2];
[cecb0789]295 key[0] = irq->inr;
296 key[1] = irq->devno;
297
298 ASSERT(irq->notif_cfg.answerbox == box);
299
300 /* Unlist from the answerbox. */
301 list_remove(&irq->notif_cfg.link);
302
303 /* Free up the pseudo code and associated structures. */
304 code_free(irq->notif_cfg.code);
305
[2845930]306 /*
307 * We need to drop the IRQ lock now because hash_table_remove()
308 * will try to reacquire it. That basically violates the natural
309 * locking order, but a deadlock in hash_table_remove() is
310 * prevented by the fact that we already held the IRQ lock and
311 * didn't drop the hash table lock in the meantime.
312 */
[da1bafb]313 irq_spinlock_unlock(&irq->lock, false);
[37be841]314
315 /* Remove from the hash table. */
316 hash_table_remove(&irq_uspace_hash_table, key, 2);
317
[cecb0789]318 free(irq);
319 }
320
[da1bafb]321 irq_spinlock_unlock(&box->irq_lock, false);
322 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[162f919]323}
324
[8b243f2]325/** Add a call to the proper answerbox queue.
[2b017ba]326 *
[da1bafb]327 * Assume irq->lock is locked and interrupts disabled.
328 *
329 * @param irq IRQ structure referencing the target answerbox.
330 * @param call IRQ notification call.
[874621f]331 *
[2b017ba]332 */
333static void send_call(irq_t *irq, call_t *call)
[874621f]334{
[da1bafb]335 irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);
[2b017ba]336 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
[da1bafb]337 irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false);
338
[2b017ba]339 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
[874621f]340}
341
[cecb0789]342/** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
[874621f]343 *
[da1bafb]344 * @param irq IRQ structure.
345 *
346 * @return IRQ_ACCEPT if the interrupt is accepted by the
347 * pseudocode, IRQ_DECLINE otherwise.
[cecb0789]348 *
[874621f]349 */
[cecb0789]350irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
[874621f]351{
[cecb0789]352 irq_code_t *code = irq->notif_cfg.code;
[da1bafb]353 uint32_t *scratch = irq->notif_cfg.scratch;
[cecb0789]354
355 if (!irq->notif_cfg.notify)
356 return IRQ_DECLINE;
357
358 if (!code)
359 return IRQ_DECLINE;
360
[da1bafb]361 size_t i;
[cecb0789]362 for (i = 0; i < code->cmdcount; i++) {
[da1bafb]363 uint32_t dstval;
364 uintptr_t srcarg = code->cmds[i].srcarg;
365 uintptr_t dstarg = code->cmds[i].dstarg;
[874621f]366
[cecb0789]367 if (srcarg >= IPC_CALL_LEN)
368 break;
[da1bafb]369
[cecb0789]370 if (dstarg >= IPC_CALL_LEN)
371 break;
372
373 switch (code->cmds[i].cmd) {
374 case CMD_PIO_READ_8:
375 dstval = pio_read_8((ioport8_t *) code->cmds[i].addr);
376 if (dstarg)
377 scratch[dstarg] = dstval;
378 break;
379 case CMD_PIO_READ_16:
380 dstval = pio_read_16((ioport16_t *) code->cmds[i].addr);
381 if (dstarg)
382 scratch[dstarg] = dstval;
383 break;
384 case CMD_PIO_READ_32:
385 dstval = pio_read_32((ioport32_t *) code->cmds[i].addr);
386 if (dstarg)
387 scratch[dstarg] = dstval;
388 break;
389 case CMD_PIO_WRITE_8:
390 pio_write_8((ioport8_t *) code->cmds[i].addr,
391 (uint8_t) code->cmds[i].value);
392 break;
393 case CMD_PIO_WRITE_16:
394 pio_write_16((ioport16_t *) code->cmds[i].addr,
395 (uint16_t) code->cmds[i].value);
396 break;
397 case CMD_PIO_WRITE_32:
398 pio_write_32((ioport32_t *) code->cmds[i].addr,
399 (uint32_t) code->cmds[i].value);
400 break;
401 case CMD_BTEST:
[da1bafb]402 if ((srcarg) && (dstarg)) {
[cecb0789]403 dstval = scratch[srcarg] & code->cmds[i].value;
404 scratch[dstarg] = dstval;
405 }
406 break;
407 case CMD_PREDICATE:
[da1bafb]408 if ((srcarg) && (!scratch[srcarg])) {
[cecb0789]409 i += code->cmds[i].value;
410 continue;
411 }
412 break;
413 case CMD_ACCEPT:
414 return IRQ_ACCEPT;
415 case CMD_DECLINE:
416 default:
417 return IRQ_DECLINE;
418 }
[874621f]419 }
[cecb0789]420
421 return IRQ_DECLINE;
[874621f]422}
423
[cecb0789]424/* IRQ top-half handler.
[162f919]425 *
[2b017ba]426 * We expect interrupts to be disabled and the irq->lock already held.
[8b243f2]427 *
[da1bafb]428 * @param irq IRQ structure.
429 *
[162f919]430 */
[cecb0789]431void ipc_irq_top_half_handler(irq_t *irq)
[162f919]432{
[2b017ba]433 ASSERT(irq);
[1d432f9]434
435 ASSERT(interrupts_disabled());
436 ASSERT(irq_spinlock_locked(&irq->lock));
[da1bafb]437
[2b017ba]438 if (irq->notif_cfg.answerbox) {
[da1bafb]439 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
[cecb0789]440 if (!call)
[d8f7362]441 return;
[cecb0789]442
[162f919]443 call->flags |= IPC_CALL_NOTIF;
[43752b6]444 /* Put a counter to the message */
[0c1a5d8a]445 call->priv = ++irq->notif_cfg.counter;
[da1bafb]446
[43752b6]447 /* Set up args */
[228e490]448 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
[cecb0789]449 IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]);
450 IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]);
451 IPC_SET_ARG3(call->data, irq->notif_cfg.scratch[3]);
452 IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
453 IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
[da1bafb]454
[2b017ba]455 send_call(irq, call);
[162f919]456 }
457}
458
[cecb0789]459/** Send notification message.
[874621f]460 *
[da1bafb]461 * @param irq IRQ structure.
462 * @param a1 Driver-specific payload argument.
463 * @param a2 Driver-specific payload argument.
464 * @param a3 Driver-specific payload argument.
465 * @param a4 Driver-specific payload argument.
466 * @param a5 Driver-specific payload argument.
467 *
[162f919]468 */
[96b02eb9]469void ipc_irq_send_msg(irq_t *irq, sysarg_t a1, sysarg_t a2, sysarg_t a3,
470 sysarg_t a4, sysarg_t a5)
[162f919]471{
[da1bafb]472 irq_spinlock_lock(&irq->lock, true);
473
[cecb0789]474 if (irq->notif_cfg.answerbox) {
[da1bafb]475 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
[cecb0789]476 if (!call) {
[da1bafb]477 irq_spinlock_unlock(&irq->lock, true);
[cecb0789]478 return;
[b14e35f2]479 }
[da1bafb]480
[cecb0789]481 call->flags |= IPC_CALL_NOTIF;
482 /* Put a counter to the message */
483 call->priv = ++irq->notif_cfg.counter;
[da1bafb]484
[228e490]485 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
[cecb0789]486 IPC_SET_ARG1(call->data, a1);
487 IPC_SET_ARG2(call->data, a2);
488 IPC_SET_ARG3(call->data, a3);
489 IPC_SET_ARG4(call->data, a4);
490 IPC_SET_ARG5(call->data, a5);
491
492 send_call(irq, call);
[b14e35f2]493 }
[da1bafb]494
495 irq_spinlock_unlock(&irq->lock, true);
[162f919]496}
[b45c443]497
[cc73a8a1]498/** @}
[b45c443]499 */
Note: See TracBrowser for help on using the repository browser.