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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0f13d2 was 1d432f9, checked in by Jakub Jermar <jakub@…>, 15 years ago

Reflect assumptions about lock and interrupt state in functions themselves.

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