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
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2006 Jakub Jermar
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
30/** @addtogroup genericipc
31 * @{
32 */
33
34/**
35 * @file
36 * @brief IRQ notification framework.
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:
44 * - IMETHOD: interface and method as registered by the SYS_IPC_REGISTER_IRQ
45 * syscall
46 * - ARG1: payload modified by a 'top-half' handler
47 * - ARG2: payload modified by a 'top-half' handler
48 * - ARG3: payload modified by a 'top-half' handler
49 * - ARG4: payload modified by a 'top-half' handler
50 * - ARG5: payload modified by a 'top-half' handler
51 * - in_phone_hash: interrupt counter (may be needed to assure correct order
52 * in multithreaded drivers)
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.
71 *
72 */
73
74#include <arch.h>
75#include <mm/slab.h>
76#include <errno.h>
77#include <ddi/irq.h>
78#include <ipc/ipc.h>
79#include <ipc/irq.h>
80#include <syscall/copy.h>
81#include <console/console.h>
82#include <print.h>
83
84/** Free the top-half pseudocode.
85 *
86 * @param code Pointer to the top-half pseudocode.
87 *
88 */
89static void code_free(irq_code_t *code)
90{
91 if (code) {
92 free(code->cmds);
93 free(code);
94 }
95}
96
97/** Copy the top-half pseudocode from userspace into the kernel.
98 *
99 * @param ucode Userspace address of the top-half pseudocode.
100 *
101 * @return Kernel address of the copied pseudocode.
102 *
103 */
104static irq_code_t *code_from_uspace(irq_code_t *ucode)
105{
106 irq_code_t *code = malloc(sizeof(*code), 0);
107 int rc = copy_from_uspace(code, ucode, sizeof(*code));
108 if (rc != 0) {
109 free(code);
110 return NULL;
111 }
112
113 if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
114 free(code);
115 return NULL;
116 }
117
118 irq_cmd_t *ucmds = code->cmds;
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);
122 if (rc != 0) {
123 free(code->cmds);
124 free(code);
125 return NULL;
126 }
127
128 return code;
129}
130
131/** Register an answerbox as a receiving end for IRQ notifications.
132 *
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.
139 *
140 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
141 *
142 */
143int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
144 sysarg_t imethod, irq_code_t *ucode)
145{
146 sysarg_t key[] = {
147 (sysarg_t) inr,
148 (sysarg_t) devno
149 };
150
151 irq_code_t *code;
152 if (ucode) {
153 code = code_from_uspace(ucode);
154 if (!code)
155 return EBADMEM;
156 } else
157 code = NULL;
158
159 /*
160 * Allocate and populate the IRQ structure.
161 */
162 irq_t *irq = malloc(sizeof(irq_t), 0);
163
164 irq_initialize(irq);
165 irq->devno = devno;
166 irq->inr = inr;
167 irq->claim = ipc_irq_top_half_claim;
168 irq->handler = ipc_irq_top_half_handler;
169 irq->notif_cfg.notify = true;
170 irq->notif_cfg.answerbox = box;
171 irq->notif_cfg.imethod = imethod;
172 irq->notif_cfg.code = code;
173 irq->notif_cfg.counter = 0;
174
175 /*
176 * Enlist the IRQ structure in the uspace IRQ hash table and the
177 * answerbox's list.
178 */
179 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
180
181 link_t *hlp = hash_table_find(&irq_uspace_hash_table, key);
182 if (hlp) {
183 irq_t *hirq = hash_table_get_instance(hlp, irq_t, link);
184
185 /* hirq is locked */
186 irq_spinlock_unlock(&hirq->lock, false);
187 code_free(code);
188 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
189
190 free(irq);
191 return EEXISTS;
192 }
193
194 /* Locking is not really necessary, but paranoid */
195 irq_spinlock_lock(&irq->lock, false);
196 irq_spinlock_lock(&box->irq_lock, false);
197
198 hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
199 list_append(&irq->notif_cfg.link, &box->irq_head);
200
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
205 return EOK;
206}
207
208/** Unregister task from IRQ notification.
209 *
210 * @param box Answerbox associated with the notification.
211 * @param inr IRQ number.
212 * @param devno Device number.
213 *
214 */
215int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
216{
217 sysarg_t key[] = {
218 (sysarg_t) inr,
219 (sysarg_t) devno
220 };
221
222 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
223 link_t *lnk = hash_table_find(&irq_uspace_hash_table, key);
224 if (!lnk) {
225 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
226 return ENOENT;
227 }
228
229 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
230
231 /* irq is locked */
232 irq_spinlock_lock(&box->irq_lock, false);
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);
238
239 /* Remove the IRQ from the answerbox's list. */
240 list_remove(&irq->notif_cfg.link);
241
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 */
249 irq_spinlock_unlock(&irq->lock, false);
250
251 /* Remove the IRQ from the uspace IRQ hash table. */
252 hash_table_remove(&irq_uspace_hash_table, key, 2);
253
254 irq_spinlock_unlock(&box->irq_lock, false);
255 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
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 *
269 * @param box Answerbox for which we want to carry out the cleanup.
270 *
271 */
272void ipc_irq_cleanup(answerbox_t *box)
273{
274loop:
275 irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
276 irq_spinlock_lock(&box->irq_lock, false);
277
278 while (box->irq_head.next != &box->irq_head) {
279 DEADLOCK_PROBE_INIT(p_irqlock);
280
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)) {
285 /*
286 * Avoid deadlock by trying again.
287 */
288 irq_spinlock_unlock(&box->irq_lock, false);
289 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
290 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
291 goto loop;
292 }
293
294 sysarg_t key[2];
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
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 */
313 irq_spinlock_unlock(&irq->lock, false);
314
315 /* Remove from the hash table. */
316 hash_table_remove(&irq_uspace_hash_table, key, 2);
317
318 free(irq);
319 }
320
321 irq_spinlock_unlock(&box->irq_lock, false);
322 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
323}
324
325/** Add a call to the proper answerbox queue.
326 *
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.
331 *
332 */
333static void send_call(irq_t *irq, call_t *call)
334{
335 irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);
336 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
337 irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false);
338
339 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
340}
341
342/** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
343 *
344 * @param irq IRQ structure.
345 *
346 * @return IRQ_ACCEPT if the interrupt is accepted by the
347 * pseudocode, IRQ_DECLINE otherwise.
348 *
349 */
350irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
351{
352 irq_code_t *code = irq->notif_cfg.code;
353 uint32_t *scratch = irq->notif_cfg.scratch;
354
355 if (!irq->notif_cfg.notify)
356 return IRQ_DECLINE;
357
358 if (!code)
359 return IRQ_DECLINE;
360
361 size_t i;
362 for (i = 0; i < code->cmdcount; i++) {
363 uint32_t dstval;
364 uintptr_t srcarg = code->cmds[i].srcarg;
365 uintptr_t dstarg = code->cmds[i].dstarg;
366
367 if (srcarg >= IPC_CALL_LEN)
368 break;
369
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:
402 if ((srcarg) && (dstarg)) {
403 dstval = scratch[srcarg] & code->cmds[i].value;
404 scratch[dstarg] = dstval;
405 }
406 break;
407 case CMD_PREDICATE:
408 if ((srcarg) && (!scratch[srcarg])) {
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 }
419 }
420
421 return IRQ_DECLINE;
422}
423
424/* IRQ top-half handler.
425 *
426 * We expect interrupts to be disabled and the irq->lock already held.
427 *
428 * @param irq IRQ structure.
429 *
430 */
431void ipc_irq_top_half_handler(irq_t *irq)
432{
433 ASSERT(irq);
434
435 ASSERT(interrupts_disabled());
436 ASSERT(irq_spinlock_locked(&irq->lock));
437
438 if (irq->notif_cfg.answerbox) {
439 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
440 if (!call)
441 return;
442
443 call->flags |= IPC_CALL_NOTIF;
444 /* Put a counter to the message */
445 call->priv = ++irq->notif_cfg.counter;
446
447 /* Set up args */
448 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
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]);
454
455 send_call(irq, call);
456 }
457}
458
459/** Send notification message.
460 *
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 *
468 */
469void ipc_irq_send_msg(irq_t *irq, sysarg_t a1, sysarg_t a2, sysarg_t a3,
470 sysarg_t a4, sysarg_t a5)
471{
472 irq_spinlock_lock(&irq->lock, true);
473
474 if (irq->notif_cfg.answerbox) {
475 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
476 if (!call) {
477 irq_spinlock_unlock(&irq->lock, true);
478 return;
479 }
480
481 call->flags |= IPC_CALL_NOTIF;
482 /* Put a counter to the message */
483 call->priv = ++irq->notif_cfg.counter;
484
485 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
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);
493 }
494
495 irq_spinlock_unlock(&irq->lock, true);
496}
497
498/** @}
499 */
Note: See TracBrowser for help on using the repository browser.