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

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

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

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