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

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

code cleanup (mostly signed/unsigned)
allow extra compiler warnings

  • Property mode set to 100644
File size: 10.2 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 */
[cc73a8a1]33/**
34 * @file
35 * @brief IRQ notification framework.
[bdc5c516]36 *
37 * This framework allows applications to register to receive a notification
38 * when interrupt is detected. The application may provide a simple 'top-half'
39 * handler as part of its registration, which can perform simple operations
40 * (read/write port/memory, add information to notification ipc message).
41 *
42 * The structure of a notification message is as follows:
[2b017ba]43 * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall
[43752b6]44 * - ARG1: payload modified by a 'top-half' handler
[2b017ba]45 * - ARG2: payload modified by a 'top-half' handler
46 * - ARG3: payload modified by a 'top-half' handler
[43752b6]47 * - in_phone_hash: interrupt counter (may be needed to assure correct order
[bdc5c516]48 * in multithreaded drivers)
49 */
50
[162f919]51#include <arch.h>
52#include <mm/slab.h>
53#include <errno.h>
[2b017ba]54#include <ddi/irq.h>
[162f919]55#include <ipc/ipc.h>
56#include <ipc/irq.h>
[e3c762cd]57#include <syscall/copy.h>
[d0c5901]58#include <console/console.h>
[253f35a1]59#include <print.h>
[162f919]60
[2b017ba]61/** Execute code associated with IRQ notification.
62 *
[8b243f2]63 * @param call Notification call.
64 * @param code Top-half pseudocode.
[2b017ba]65 */
[162f919]66static void code_execute(call_t *call, irq_code_t *code)
67{
[6c441cf8]68 unsigned int i;
[7f1c620]69 unative_t dstval = 0;
[ae971b3e]70
[162f919]71 if (!code)
72 return;
73
[8b243f2]74 for (i = 0; i < code->cmdcount; i++) {
[162f919]75 switch (code->cmds[i].cmd) {
76 case CMD_MEM_READ_1:
[8b243f2]77 dstval = *((uint8_t *) code->cmds[i].addr);
[162f919]78 break;
79 case CMD_MEM_READ_2:
[8b243f2]80 dstval = *((uint16_t *) code->cmds[i].addr);
[162f919]81 break;
82 case CMD_MEM_READ_4:
[8b243f2]83 dstval = *((uint32_t *) code->cmds[i].addr);
[162f919]84 break;
85 case CMD_MEM_READ_8:
[8b243f2]86 dstval = *((uint64_t *) code->cmds[i].addr);
[162f919]87 break;
88 case CMD_MEM_WRITE_1:
[8b243f2]89 *((uint8_t *) code->cmds[i].addr) = code->cmds[i].value;
[162f919]90 break;
91 case CMD_MEM_WRITE_2:
[d2e0a8cb]92 *((uint16_t *) code->cmds[i].addr) =
93 code->cmds[i].value;
[162f919]94 break;
95 case CMD_MEM_WRITE_4:
[d2e0a8cb]96 *((uint32_t *) code->cmds[i].addr) =
97 code->cmds[i].value;
[162f919]98 break;
99 case CMD_MEM_WRITE_8:
[d2e0a8cb]100 *((uint64_t *) code->cmds[i].addr) =
101 code->cmds[i].value;
[162f919]102 break;
[bdc5c516]103#if defined(ia32) || defined(amd64)
104 case CMD_PORT_READ_1:
[8b243f2]105 dstval = inb((long) code->cmds[i].addr);
[bdc5c516]106 break;
107 case CMD_PORT_WRITE_1:
[8b243f2]108 outb((long) code->cmds[i].addr, code->cmds[i].value);
[bdc5c516]109 break;
[d0c5901]110#endif
[49319ac]111#if defined(ia64) && defined(SKI)
[d0c5901]112 case CMD_IA64_GETCHAR:
[43752b6]113 dstval = _getc(&ski_uconsole);
[d0c5901]114 break;
[732fd3c]115#endif
[ae971b3e]116#if defined(ppc32)
[732fd3c]117 case CMD_PPC32_GETCHAR:
[43752b6]118 dstval = cuda_get_scancode();
[732fd3c]119 break;
[bdc5c516]120#endif
[162f919]121 default:
122 break;
123 }
[d2e0a8cb]124 if (code->cmds[i].dstarg && code->cmds[i].dstarg <
125 IPC_CALL_LEN) {
[43752b6]126 call->data.args[code->cmds[i].dstarg] = dstval;
127 }
[162f919]128 }
129}
130
[8b243f2]131/** Free top-half pseudocode.
132 *
133 * @param code Pointer to the top-half pseudocode.
134 */
[162f919]135static void code_free(irq_code_t *code)
136{
137 if (code) {
138 free(code->cmds);
139 free(code);
140 }
141}
142
[8b243f2]143/** Copy top-half pseudocode from userspace into the kernel.
144 *
145 * @param ucode Userspace address of the top-half pseudocode.
146 *
147 * @return Kernel address of the copied pseudocode.
148 */
149static irq_code_t *code_from_uspace(irq_code_t *ucode)
[162f919]150{
151 irq_code_t *code;
152 irq_cmd_t *ucmds;
[e3c762cd]153 int rc;
[162f919]154
155 code = malloc(sizeof(*code), 0);
[e3c762cd]156 rc = copy_from_uspace(code, ucode, sizeof(*code));
157 if (rc != 0) {
158 free(code);
159 return NULL;
160 }
[162f919]161
162 if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
163 free(code);
164 return NULL;
165 }
166 ucmds = code->cmds;
[8b243f2]167 code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
168 rc = copy_from_uspace(code->cmds, ucmds,
169 sizeof(code->cmds[0]) * code->cmdcount);
[e3c762cd]170 if (rc != 0) {
171 free(code->cmds);
172 free(code);
173 return NULL;
174 }
[162f919]175
176 return code;
177}
178
[2b017ba]179/** Unregister task from IRQ notification.
180 *
[8b243f2]181 * @param box Answerbox associated with the notification.
182 * @param inr IRQ number.
183 * @param devno Device number.
[2b017ba]184 */
185void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
[162f919]186{
187 ipl_t ipl;
[2b017ba]188 irq_t *irq;
[162f919]189
190 ipl = interrupts_disable();
[2b017ba]191 irq = irq_find_and_lock(inr, devno);
192 if (irq) {
193 if (irq->notif_cfg.answerbox == box) {
[b14e35f2]194 code_free(irq->notif_cfg.code);
[4874c2d]195 irq->notif_cfg.notify = false;
[2b017ba]196 irq->notif_cfg.answerbox = NULL;
[4874c2d]197 irq->notif_cfg.code = NULL;
[2b017ba]198 irq->notif_cfg.method = 0;
199 irq->notif_cfg.counter = 0;
[b14e35f2]200
201 spinlock_lock(&box->irq_lock);
202 list_remove(&irq->notif_cfg.link);
203 spinlock_unlock(&box->irq_lock);
204
[2b017ba]205 spinlock_unlock(&irq->lock);
206 }
[162f919]207 }
208 interrupts_restore(ipl);
209}
210
[2b017ba]211/** Register an answerbox as a receiving end for IRQ notifications.
212 *
[8b243f2]213 * @param box Receiving answerbox.
214 * @param inr IRQ number.
215 * @param devno Device number.
216 * @param method Method to be associated with the notification.
217 * @param ucode Uspace pointer to top-half pseudocode.
[2b017ba]218 *
[8b243f2]219 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
[2b017ba]220 */
[8b243f2]221int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
222 unative_t method, irq_code_t *ucode)
[162f919]223{
224 ipl_t ipl;
225 irq_code_t *code;
[2b017ba]226 irq_t *irq;
[162f919]227
228 if (ucode) {
229 code = code_from_uspace(ucode);
230 if (!code)
231 return EBADMEM;
[8b243f2]232 } else {
[162f919]233 code = NULL;
[8b243f2]234 }
[162f919]235
236 ipl = interrupts_disable();
[2b017ba]237 irq = irq_find_and_lock(inr, devno);
238 if (!irq) {
239 interrupts_restore(ipl);
240 code_free(code);
241 return ENOENT;
242 }
243
244 if (irq->notif_cfg.answerbox) {
245 spinlock_unlock(&irq->lock);
[162f919]246 interrupts_restore(ipl);
247 code_free(code);
248 return EEXISTS;
249 }
[2b017ba]250
[4874c2d]251 irq->notif_cfg.notify = true;
[2b017ba]252 irq->notif_cfg.answerbox = box;
253 irq->notif_cfg.method = method;
254 irq->notif_cfg.code = code;
255 irq->notif_cfg.counter = 0;
[b14e35f2]256
257 spinlock_lock(&box->irq_lock);
258 list_append(&irq->notif_cfg.link, &box->irq_head);
259 spinlock_unlock(&box->irq_lock);
260
[2b017ba]261 spinlock_unlock(&irq->lock);
[162f919]262 interrupts_restore(ipl);
263
264 return 0;
265}
266
[8b243f2]267/** Add a call to the proper answerbox queue.
[2b017ba]268 *
269 * Assume irq->lock is locked.
[874621f]270 *
[8b243f2]271 * @param irq IRQ structure referencing the target answerbox.
272 * @param call IRQ notification call.
[2b017ba]273 */
274static void send_call(irq_t *irq, call_t *call)
[874621f]275{
[2b017ba]276 spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
277 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
278 spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
[874621f]279
[2b017ba]280 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
[874621f]281}
282
[8b243f2]283/** Send notification message.
[874621f]284 *
[8b243f2]285 * @param irq IRQ structure.
286 * @param a1 Driver-specific payload argument.
287 * @param a2 Driver-specific payload argument.
288 * @param a3 Driver-specific payload argument.
[d2e0a8cb]289 * @param a4 Driver-specific payload argument.
290 * @param a5 Driver-specific payload argument.
[874621f]291 */
[d2e0a8cb]292void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3,
293 unative_t a4, unative_t a5)
[874621f]294{
295 call_t *call;
296
[2b017ba]297 spinlock_lock(&irq->lock);
[874621f]298
[2b017ba]299 if (irq->notif_cfg.answerbox) {
[874621f]300 call = ipc_call_alloc(FRAME_ATOMIC);
301 if (!call) {
[2b017ba]302 spinlock_unlock(&irq->lock);
[874621f]303 return;
304 }
305 call->flags |= IPC_CALL_NOTIF;
[2b017ba]306 IPC_SET_METHOD(call->data, irq->notif_cfg.method);
[43752b6]307 IPC_SET_ARG1(call->data, a1);
[874621f]308 IPC_SET_ARG2(call->data, a2);
309 IPC_SET_ARG3(call->data, a3);
[d2e0a8cb]310 IPC_SET_ARG4(call->data, a4);
311 IPC_SET_ARG5(call->data, a5);
[43752b6]312 /* Put a counter to the message */
[0c1a5d8a]313 call->priv = ++irq->notif_cfg.counter;
[874621f]314
[2b017ba]315 send_call(irq, call);
[874621f]316 }
[2b017ba]317 spinlock_unlock(&irq->lock);
[874621f]318}
319
[8b243f2]320/** Notify a task that an IRQ had occurred.
[162f919]321 *
[2b017ba]322 * We expect interrupts to be disabled and the irq->lock already held.
[8b243f2]323 *
324 * @param irq IRQ structure.
[162f919]325 */
[2b017ba]326void ipc_irq_send_notif(irq_t *irq)
[162f919]327{
328 call_t *call;
329
[2b017ba]330 ASSERT(irq);
[162f919]331
[2b017ba]332 if (irq->notif_cfg.answerbox) {
[162f919]333 call = ipc_call_alloc(FRAME_ATOMIC);
[d8f7362]334 if (!call) {
335 return;
336 }
[162f919]337 call->flags |= IPC_CALL_NOTIF;
[43752b6]338 /* Put a counter to the message */
[0c1a5d8a]339 call->priv = ++irq->notif_cfg.counter;
[43752b6]340 /* Set up args */
[2b017ba]341 IPC_SET_METHOD(call->data, irq->notif_cfg.method);
[162f919]342
343 /* Execute code to handle irq */
[2b017ba]344 code_execute(call, irq->notif_cfg.code);
[874621f]345
[2b017ba]346 send_call(irq, call);
[162f919]347 }
348}
349
[2b017ba]350/** Disconnect all IRQ notifications from an answerbox.
[b14e35f2]351 *
352 * This function is effective because the answerbox contains
353 * list of all irq_t structures that are registered to
354 * send notifications to it.
[874621f]355 *
[8b243f2]356 * @param box Answerbox for which we want to carry out the cleanup.
[162f919]357 */
358void ipc_irq_cleanup(answerbox_t *box)
359{
[b14e35f2]360 ipl_t ipl;
361
362loop:
363 ipl = interrupts_disable();
364 spinlock_lock(&box->irq_lock);
365
366 while (box->irq_head.next != &box->irq_head) {
367 link_t *cur = box->irq_head.next;
368 irq_t *irq;
[31d8e10]369 DEADLOCK_PROBE_INIT(p_irqlock);
[b14e35f2]370
371 irq = list_get_instance(cur, irq_t, notif_cfg.link);
372 if (!spinlock_trylock(&irq->lock)) {
373 /*
374 * Avoid deadlock by trying again.
375 */
376 spinlock_unlock(&box->irq_lock);
377 interrupts_restore(ipl);
[31d8e10]378 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
[b14e35f2]379 goto loop;
380 }
381
382 ASSERT(irq->notif_cfg.answerbox == box);
383
384 list_remove(&irq->notif_cfg.link);
385
386 /*
387 * Don't forget to free any top-half pseudocode.
388 */
389 code_free(irq->notif_cfg.code);
390
391 irq->notif_cfg.notify = false;
392 irq->notif_cfg.answerbox = NULL;
393 irq->notif_cfg.code = NULL;
394 irq->notif_cfg.method = 0;
395 irq->notif_cfg.counter = 0;
396
397 spinlock_unlock(&irq->lock);
398 }
399
400 spinlock_unlock(&box->irq_lock);
401 interrupts_restore(ipl);
[162f919]402}
[b45c443]403
[cc73a8a1]404/** @}
[b45c443]405 */
Note: See TracBrowser for help on using the repository browser.