source: mainline/kernel/generic/src/ipc/irq.c@ 773f188

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 773f188 was 8add9ca5, checked in by Martin Decky <martin@…>, 14 years ago

ABI change: syscalls for registering IRQ notifications are probably more DDI-related than IPC-related

  • Property mode set to 100644
File size: 14.4 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:
[8add9ca5]44 * - IMETHOD: interface and method as registered by the SYS_REGISTER_IRQ
[228e490]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 *
[78ffb70]133 * @param box Receiving answerbox.
134 * @param inr IRQ number.
135 * @param devno Device number.
136 * @param imethod Interface and method to be associated with the
137 * notification.
138 * @param ucode Uspace pointer to top-half pseudocode.
139 * @return EOK on success or a negative error code.
[2b017ba]140 *
141 */
[8b243f2]142int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
[228e490]143 sysarg_t imethod, irq_code_t *ucode)
[162f919]144{
[96b02eb9]145 sysarg_t key[] = {
146 (sysarg_t) inr,
147 (sysarg_t) devno
[cecb0789]148 };
[78ffb70]149
150 if ((inr < 0) || (inr > last_inr))
151 return ELIMIT;
[c822026]152
[da1bafb]153 irq_code_t *code;
[162f919]154 if (ucode) {
155 code = code_from_uspace(ucode);
156 if (!code)
157 return EBADMEM;
[da1bafb]158 } else
[162f919]159 code = NULL;
[c822026]160
[cecb0789]161 /*
162 * Allocate and populate the IRQ structure.
163 */
[da1bafb]164 irq_t *irq = malloc(sizeof(irq_t), 0);
165
[cecb0789]166 irq_initialize(irq);
167 irq->devno = devno;
168 irq->inr = inr;
169 irq->claim = ipc_irq_top_half_claim;
[691eb52]170 irq->handler = ipc_irq_top_half_handler;
[4874c2d]171 irq->notif_cfg.notify = true;
[2b017ba]172 irq->notif_cfg.answerbox = box;
[228e490]173 irq->notif_cfg.imethod = imethod;
[2b017ba]174 irq->notif_cfg.code = code;
175 irq->notif_cfg.counter = 0;
[c822026]176
[cecb0789]177 /*
178 * Enlist the IRQ structure in the uspace IRQ hash table and the
179 * answerbox's list.
180 */
[da1bafb]181 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
182
183 link_t *hlp = hash_table_find(&irq_uspace_hash_table, key);
[2845930]184 if (hlp) {
[da1bafb]185 irq_t *hirq = hash_table_get_instance(hlp, irq_t, link);
[c822026]186
[2845930]187 /* hirq is locked */
[da1bafb]188 irq_spinlock_unlock(&hirq->lock, false);
[cecb0789]189 code_free(code);
[da1bafb]190 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
191
[cecb0789]192 free(irq);
193 return EEXISTS;
194 }
[c822026]195
[da1bafb]196 /* Locking is not really necessary, but paranoid */
197 irq_spinlock_lock(&irq->lock, false);
198 irq_spinlock_lock(&box->irq_lock, false);
199
[cecb0789]200 hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
[b14e35f2]201 list_append(&irq->notif_cfg.link, &box->irq_head);
[c822026]202
[da1bafb]203 irq_spinlock_unlock(&box->irq_lock, false);
204 irq_spinlock_unlock(&irq->lock, false);
205 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
206
[cecb0789]207 return EOK;
208}
209
210/** Unregister task from IRQ notification.
211 *
[78ffb70]212 * @param box Answerbox associated with the notification.
213 * @param inr IRQ number.
214 * @param devno Device number.
215 * @return EOK on success or a negative error code.
[cecb0789]216 */
217int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
218{
[96b02eb9]219 sysarg_t key[] = {
220 (sysarg_t) inr,
221 (sysarg_t) devno
[cecb0789]222 };
[78ffb70]223
224 if ((inr < 0) || (inr > last_inr))
225 return ELIMIT;
[da1bafb]226
227 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
228 link_t *lnk = hash_table_find(&irq_uspace_hash_table, key);
[cecb0789]229 if (!lnk) {
[da1bafb]230 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[cecb0789]231 return ENOENT;
232 }
[da1bafb]233
234 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
235
[2845930]236 /* irq is locked */
[da1bafb]237 irq_spinlock_lock(&box->irq_lock, false);
[cecb0789]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);
[da1bafb]243
244 /* Remove the IRQ from the answerbox's list. */
[cecb0789]245 list_remove(&irq->notif_cfg.link);
[da1bafb]246
[2845930]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 */
[da1bafb]254 irq_spinlock_unlock(&irq->lock, false);
255
[cecb0789]256 /* Remove the IRQ from the uspace IRQ hash table. */
257 hash_table_remove(&irq_uspace_hash_table, key, 2);
258
[da1bafb]259 irq_spinlock_unlock(&box->irq_lock, false);
260 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[cecb0789]261
262 /* Free up the IRQ structure. */
263 free(irq);
264
265 return EOK;
266}
267
268/** Disconnect all IRQ notifications from an answerbox.
269 *
270 * This function is effective because the answerbox contains
271 * list of all irq_t structures that are registered to
272 * send notifications to it.
273 *
[da1bafb]274 * @param box Answerbox for which we want to carry out the cleanup.
275 *
[cecb0789]276 */
277void ipc_irq_cleanup(answerbox_t *box)
278{
279loop:
[da1bafb]280 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
281 irq_spinlock_lock(&box->irq_lock, false);
[cecb0789]282
283 while (box->irq_head.next != &box->irq_head) {
284 DEADLOCK_PROBE_INIT(p_irqlock);
285
[da1bafb]286 irq_t *irq = list_get_instance(box->irq_head.next, irq_t,
287 notif_cfg.link);
288
289 if (!irq_spinlock_trylock(&irq->lock)) {
[cecb0789]290 /*
291 * Avoid deadlock by trying again.
292 */
[da1bafb]293 irq_spinlock_unlock(&box->irq_lock, false);
294 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[cecb0789]295 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
296 goto loop;
297 }
[da1bafb]298
[96b02eb9]299 sysarg_t key[2];
[cecb0789]300 key[0] = irq->inr;
301 key[1] = irq->devno;
302
303 ASSERT(irq->notif_cfg.answerbox == box);
304
305 /* Unlist from the answerbox. */
306 list_remove(&irq->notif_cfg.link);
307
308 /* Free up the pseudo code and associated structures. */
309 code_free(irq->notif_cfg.code);
310
[2845930]311 /*
312 * We need to drop the IRQ lock now because hash_table_remove()
313 * will try to reacquire it. That basically violates the natural
314 * locking order, but a deadlock in hash_table_remove() is
315 * prevented by the fact that we already held the IRQ lock and
316 * didn't drop the hash table lock in the meantime.
317 */
[da1bafb]318 irq_spinlock_unlock(&irq->lock, false);
[37be841]319
320 /* Remove from the hash table. */
321 hash_table_remove(&irq_uspace_hash_table, key, 2);
322
[cecb0789]323 free(irq);
324 }
325
[da1bafb]326 irq_spinlock_unlock(&box->irq_lock, false);
327 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
[162f919]328}
329
[8b243f2]330/** Add a call to the proper answerbox queue.
[2b017ba]331 *
[da1bafb]332 * Assume irq->lock is locked and interrupts disabled.
333 *
334 * @param irq IRQ structure referencing the target answerbox.
335 * @param call IRQ notification call.
[874621f]336 *
[2b017ba]337 */
338static void send_call(irq_t *irq, call_t *call)
[874621f]339{
[da1bafb]340 irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);
[2b017ba]341 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
[da1bafb]342 irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false);
343
[2b017ba]344 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
[874621f]345}
346
[cecb0789]347/** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
[874621f]348 *
[da1bafb]349 * @param irq IRQ structure.
350 *
351 * @return IRQ_ACCEPT if the interrupt is accepted by the
352 * pseudocode, IRQ_DECLINE otherwise.
[cecb0789]353 *
[874621f]354 */
[cecb0789]355irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
[874621f]356{
[cecb0789]357 irq_code_t *code = irq->notif_cfg.code;
[da1bafb]358 uint32_t *scratch = irq->notif_cfg.scratch;
[cecb0789]359
360 if (!irq->notif_cfg.notify)
361 return IRQ_DECLINE;
362
363 if (!code)
364 return IRQ_DECLINE;
365
[da1bafb]366 size_t i;
[cecb0789]367 for (i = 0; i < code->cmdcount; i++) {
[da1bafb]368 uint32_t dstval;
369 uintptr_t srcarg = code->cmds[i].srcarg;
370 uintptr_t dstarg = code->cmds[i].dstarg;
[874621f]371
[cecb0789]372 if (srcarg >= IPC_CALL_LEN)
373 break;
[da1bafb]374
[cecb0789]375 if (dstarg >= IPC_CALL_LEN)
376 break;
377
378 switch (code->cmds[i].cmd) {
379 case CMD_PIO_READ_8:
380 dstval = pio_read_8((ioport8_t *) code->cmds[i].addr);
381 if (dstarg)
382 scratch[dstarg] = dstval;
383 break;
384 case CMD_PIO_READ_16:
385 dstval = pio_read_16((ioport16_t *) code->cmds[i].addr);
386 if (dstarg)
387 scratch[dstarg] = dstval;
388 break;
389 case CMD_PIO_READ_32:
390 dstval = pio_read_32((ioport32_t *) code->cmds[i].addr);
391 if (dstarg)
392 scratch[dstarg] = dstval;
393 break;
394 case CMD_PIO_WRITE_8:
395 pio_write_8((ioport8_t *) code->cmds[i].addr,
396 (uint8_t) code->cmds[i].value);
397 break;
398 case CMD_PIO_WRITE_16:
399 pio_write_16((ioport16_t *) code->cmds[i].addr,
400 (uint16_t) code->cmds[i].value);
401 break;
402 case CMD_PIO_WRITE_32:
403 pio_write_32((ioport32_t *) code->cmds[i].addr,
404 (uint32_t) code->cmds[i].value);
405 break;
[9cdac5a]406 case CMD_PIO_WRITE_A_8:
407 if (srcarg) {
408 pio_write_8((ioport8_t *) code->cmds[i].addr,
409 (uint8_t) scratch[srcarg]);
410 }
411 break;
412 case CMD_PIO_WRITE_A_16:
413 if (srcarg) {
414 pio_write_16((ioport16_t *) code->cmds[i].addr,
415 (uint16_t) scratch[srcarg]);
416 }
417 break;
418 case CMD_PIO_WRITE_A_32:
419 if (srcarg) {
420 pio_write_32((ioport32_t *) code->cmds[i].addr,
421 (uint32_t) scratch[srcarg]);
422 }
423 break;
[cecb0789]424 case CMD_BTEST:
[da1bafb]425 if ((srcarg) && (dstarg)) {
[cecb0789]426 dstval = scratch[srcarg] & code->cmds[i].value;
427 scratch[dstarg] = dstval;
428 }
429 break;
430 case CMD_PREDICATE:
[da1bafb]431 if ((srcarg) && (!scratch[srcarg])) {
[cecb0789]432 i += code->cmds[i].value;
433 continue;
434 }
435 break;
436 case CMD_ACCEPT:
437 return IRQ_ACCEPT;
438 case CMD_DECLINE:
439 default:
440 return IRQ_DECLINE;
441 }
[874621f]442 }
[cecb0789]443
444 return IRQ_DECLINE;
[874621f]445}
446
[cecb0789]447/* IRQ top-half handler.
[162f919]448 *
[2b017ba]449 * We expect interrupts to be disabled and the irq->lock already held.
[8b243f2]450 *
[da1bafb]451 * @param irq IRQ structure.
452 *
[162f919]453 */
[cecb0789]454void ipc_irq_top_half_handler(irq_t *irq)
[162f919]455{
[2b017ba]456 ASSERT(irq);
[1d432f9]457
458 ASSERT(interrupts_disabled());
459 ASSERT(irq_spinlock_locked(&irq->lock));
[da1bafb]460
[2b017ba]461 if (irq->notif_cfg.answerbox) {
[da1bafb]462 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
[cecb0789]463 if (!call)
[d8f7362]464 return;
[cecb0789]465
[162f919]466 call->flags |= IPC_CALL_NOTIF;
[43752b6]467 /* Put a counter to the message */
[0c1a5d8a]468 call->priv = ++irq->notif_cfg.counter;
[da1bafb]469
[43752b6]470 /* Set up args */
[228e490]471 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
[cecb0789]472 IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]);
473 IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]);
474 IPC_SET_ARG3(call->data, irq->notif_cfg.scratch[3]);
475 IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
476 IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
[da1bafb]477
[2b017ba]478 send_call(irq, call);
[162f919]479 }
480}
481
[cecb0789]482/** Send notification message.
[874621f]483 *
[da1bafb]484 * @param irq IRQ structure.
485 * @param a1 Driver-specific payload argument.
486 * @param a2 Driver-specific payload argument.
487 * @param a3 Driver-specific payload argument.
488 * @param a4 Driver-specific payload argument.
489 * @param a5 Driver-specific payload argument.
490 *
[162f919]491 */
[96b02eb9]492void ipc_irq_send_msg(irq_t *irq, sysarg_t a1, sysarg_t a2, sysarg_t a3,
493 sysarg_t a4, sysarg_t a5)
[162f919]494{
[da1bafb]495 irq_spinlock_lock(&irq->lock, true);
496
[cecb0789]497 if (irq->notif_cfg.answerbox) {
[da1bafb]498 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
[cecb0789]499 if (!call) {
[da1bafb]500 irq_spinlock_unlock(&irq->lock, true);
[cecb0789]501 return;
[b14e35f2]502 }
[da1bafb]503
[cecb0789]504 call->flags |= IPC_CALL_NOTIF;
505 /* Put a counter to the message */
506 call->priv = ++irq->notif_cfg.counter;
[da1bafb]507
[228e490]508 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
[cecb0789]509 IPC_SET_ARG1(call->data, a1);
510 IPC_SET_ARG2(call->data, a2);
511 IPC_SET_ARG3(call->data, a3);
512 IPC_SET_ARG4(call->data, a4);
513 IPC_SET_ARG5(call->data, a5);
514
515 send_call(irq, call);
[b14e35f2]516 }
[da1bafb]517
518 irq_spinlock_unlock(&irq->lock, true);
[162f919]519}
[b45c443]520
[cc73a8a1]521/** @}
[b45c443]522 */
Note: See TracBrowser for help on using the repository browser.