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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0cb9fa0 was 5cb223f, checked in by Jakub Jermar <jakub@…>, 17 years ago

Adapt most of the kernel to ioport8_t, ioport16_t and ioport32_t types.

  • Property mode set to 100644
File size: 10.0 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 case CMD_PORT_READ_1:
[5cb223f]104 dstval = pio_read_8((ioport8_t *) code->cmds[i].addr);
[bdc5c516]105 break;
106 case CMD_PORT_WRITE_1:
[5cb223f]107 pio_write_8((ioport8_t *) code->cmds[i].addr, code->cmds[i].value);
[bdc5c516]108 break;
[162f919]109 default:
110 break;
111 }
[d2e0a8cb]112 if (code->cmds[i].dstarg && code->cmds[i].dstarg <
113 IPC_CALL_LEN) {
[43752b6]114 call->data.args[code->cmds[i].dstarg] = dstval;
115 }
[162f919]116 }
117}
118
[8b243f2]119/** Free top-half pseudocode.
120 *
121 * @param code Pointer to the top-half pseudocode.
122 */
[162f919]123static void code_free(irq_code_t *code)
124{
125 if (code) {
126 free(code->cmds);
127 free(code);
128 }
129}
130
[8b243f2]131/** Copy top-half pseudocode from userspace into the kernel.
132 *
133 * @param ucode Userspace address of the top-half pseudocode.
134 *
135 * @return Kernel address of the copied pseudocode.
136 */
137static irq_code_t *code_from_uspace(irq_code_t *ucode)
[162f919]138{
139 irq_code_t *code;
140 irq_cmd_t *ucmds;
[e3c762cd]141 int rc;
[162f919]142
143 code = malloc(sizeof(*code), 0);
[e3c762cd]144 rc = copy_from_uspace(code, ucode, sizeof(*code));
145 if (rc != 0) {
146 free(code);
147 return NULL;
148 }
[162f919]149
150 if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
151 free(code);
152 return NULL;
153 }
154 ucmds = code->cmds;
[8b243f2]155 code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
156 rc = copy_from_uspace(code->cmds, ucmds,
157 sizeof(code->cmds[0]) * code->cmdcount);
[e3c762cd]158 if (rc != 0) {
159 free(code->cmds);
160 free(code);
161 return NULL;
162 }
[162f919]163
164 return code;
165}
166
[2b017ba]167/** Unregister task from IRQ notification.
168 *
[8b243f2]169 * @param box Answerbox associated with the notification.
170 * @param inr IRQ number.
171 * @param devno Device number.
[2b017ba]172 */
173void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
[162f919]174{
175 ipl_t ipl;
[2b017ba]176 irq_t *irq;
[162f919]177
178 ipl = interrupts_disable();
[2b017ba]179 irq = irq_find_and_lock(inr, devno);
180 if (irq) {
181 if (irq->notif_cfg.answerbox == box) {
[b14e35f2]182 code_free(irq->notif_cfg.code);
[4874c2d]183 irq->notif_cfg.notify = false;
[2b017ba]184 irq->notif_cfg.answerbox = NULL;
[4874c2d]185 irq->notif_cfg.code = NULL;
[2b017ba]186 irq->notif_cfg.method = 0;
187 irq->notif_cfg.counter = 0;
[b14e35f2]188
189 spinlock_lock(&box->irq_lock);
190 list_remove(&irq->notif_cfg.link);
191 spinlock_unlock(&box->irq_lock);
192
[2b017ba]193 spinlock_unlock(&irq->lock);
194 }
[162f919]195 }
196 interrupts_restore(ipl);
197}
198
[2b017ba]199/** Register an answerbox as a receiving end for IRQ notifications.
200 *
[8b243f2]201 * @param box Receiving answerbox.
202 * @param inr IRQ number.
203 * @param devno Device number.
204 * @param method Method to be associated with the notification.
205 * @param ucode Uspace pointer to top-half pseudocode.
[2b017ba]206 *
[8b243f2]207 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
[2b017ba]208 */
[8b243f2]209int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
210 unative_t method, irq_code_t *ucode)
[162f919]211{
212 ipl_t ipl;
213 irq_code_t *code;
[2b017ba]214 irq_t *irq;
[162f919]215
216 if (ucode) {
217 code = code_from_uspace(ucode);
218 if (!code)
219 return EBADMEM;
[8b243f2]220 } else {
[162f919]221 code = NULL;
[8b243f2]222 }
[162f919]223
224 ipl = interrupts_disable();
[2b017ba]225 irq = irq_find_and_lock(inr, devno);
226 if (!irq) {
227 interrupts_restore(ipl);
228 code_free(code);
229 return ENOENT;
230 }
231
232 if (irq->notif_cfg.answerbox) {
233 spinlock_unlock(&irq->lock);
[162f919]234 interrupts_restore(ipl);
235 code_free(code);
236 return EEXISTS;
237 }
[2b017ba]238
[4874c2d]239 irq->notif_cfg.notify = true;
[2b017ba]240 irq->notif_cfg.answerbox = box;
241 irq->notif_cfg.method = method;
242 irq->notif_cfg.code = code;
243 irq->notif_cfg.counter = 0;
[b14e35f2]244
245 spinlock_lock(&box->irq_lock);
246 list_append(&irq->notif_cfg.link, &box->irq_head);
247 spinlock_unlock(&box->irq_lock);
248
[2b017ba]249 spinlock_unlock(&irq->lock);
[162f919]250 interrupts_restore(ipl);
251
252 return 0;
253}
254
[8b243f2]255/** Add a call to the proper answerbox queue.
[2b017ba]256 *
257 * Assume irq->lock is locked.
[874621f]258 *
[8b243f2]259 * @param irq IRQ structure referencing the target answerbox.
260 * @param call IRQ notification call.
[2b017ba]261 */
262static void send_call(irq_t *irq, call_t *call)
[874621f]263{
[2b017ba]264 spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
265 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
266 spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
[874621f]267
[2b017ba]268 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
[874621f]269}
270
[8b243f2]271/** Send notification message.
[874621f]272 *
[8b243f2]273 * @param irq IRQ structure.
274 * @param a1 Driver-specific payload argument.
275 * @param a2 Driver-specific payload argument.
276 * @param a3 Driver-specific payload argument.
[d2e0a8cb]277 * @param a4 Driver-specific payload argument.
278 * @param a5 Driver-specific payload argument.
[874621f]279 */
[d2e0a8cb]280void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3,
281 unative_t a4, unative_t a5)
[874621f]282{
283 call_t *call;
284
[2b017ba]285 spinlock_lock(&irq->lock);
[874621f]286
[2b017ba]287 if (irq->notif_cfg.answerbox) {
[874621f]288 call = ipc_call_alloc(FRAME_ATOMIC);
289 if (!call) {
[2b017ba]290 spinlock_unlock(&irq->lock);
[874621f]291 return;
292 }
293 call->flags |= IPC_CALL_NOTIF;
[2b017ba]294 IPC_SET_METHOD(call->data, irq->notif_cfg.method);
[43752b6]295 IPC_SET_ARG1(call->data, a1);
[874621f]296 IPC_SET_ARG2(call->data, a2);
297 IPC_SET_ARG3(call->data, a3);
[d2e0a8cb]298 IPC_SET_ARG4(call->data, a4);
299 IPC_SET_ARG5(call->data, a5);
[43752b6]300 /* Put a counter to the message */
[0c1a5d8a]301 call->priv = ++irq->notif_cfg.counter;
[874621f]302
[2b017ba]303 send_call(irq, call);
[874621f]304 }
[2b017ba]305 spinlock_unlock(&irq->lock);
[874621f]306}
307
[8b243f2]308/** Notify a task that an IRQ had occurred.
[162f919]309 *
[2b017ba]310 * We expect interrupts to be disabled and the irq->lock already held.
[8b243f2]311 *
312 * @param irq IRQ structure.
[162f919]313 */
[2b017ba]314void ipc_irq_send_notif(irq_t *irq)
[162f919]315{
316 call_t *call;
317
[2b017ba]318 ASSERT(irq);
[162f919]319
[2b017ba]320 if (irq->notif_cfg.answerbox) {
[162f919]321 call = ipc_call_alloc(FRAME_ATOMIC);
[d8f7362]322 if (!call) {
323 return;
324 }
[162f919]325 call->flags |= IPC_CALL_NOTIF;
[43752b6]326 /* Put a counter to the message */
[0c1a5d8a]327 call->priv = ++irq->notif_cfg.counter;
[43752b6]328 /* Set up args */
[2b017ba]329 IPC_SET_METHOD(call->data, irq->notif_cfg.method);
[162f919]330
331 /* Execute code to handle irq */
[2b017ba]332 code_execute(call, irq->notif_cfg.code);
[874621f]333
[2b017ba]334 send_call(irq, call);
[162f919]335 }
336}
337
[2b017ba]338/** Disconnect all IRQ notifications from an answerbox.
[b14e35f2]339 *
340 * This function is effective because the answerbox contains
341 * list of all irq_t structures that are registered to
342 * send notifications to it.
[874621f]343 *
[8b243f2]344 * @param box Answerbox for which we want to carry out the cleanup.
[162f919]345 */
346void ipc_irq_cleanup(answerbox_t *box)
347{
[b14e35f2]348 ipl_t ipl;
349
350loop:
351 ipl = interrupts_disable();
352 spinlock_lock(&box->irq_lock);
353
354 while (box->irq_head.next != &box->irq_head) {
355 link_t *cur = box->irq_head.next;
356 irq_t *irq;
[31d8e10]357 DEADLOCK_PROBE_INIT(p_irqlock);
[b14e35f2]358
359 irq = list_get_instance(cur, irq_t, notif_cfg.link);
360 if (!spinlock_trylock(&irq->lock)) {
361 /*
362 * Avoid deadlock by trying again.
363 */
364 spinlock_unlock(&box->irq_lock);
365 interrupts_restore(ipl);
[31d8e10]366 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
[b14e35f2]367 goto loop;
368 }
369
370 ASSERT(irq->notif_cfg.answerbox == box);
371
372 list_remove(&irq->notif_cfg.link);
373
374 /*
375 * Don't forget to free any top-half pseudocode.
376 */
377 code_free(irq->notif_cfg.code);
378
379 irq->notif_cfg.notify = false;
380 irq->notif_cfg.answerbox = NULL;
381 irq->notif_cfg.code = NULL;
382 irq->notif_cfg.method = 0;
383 irq->notif_cfg.counter = 0;
384
385 spinlock_unlock(&irq->lock);
386 }
387
388 spinlock_unlock(&box->irq_lock);
389 interrupts_restore(ipl);
[162f919]390}
[b45c443]391
[cc73a8a1]392/** @}
[b45c443]393 */
Note: See TracBrowser for help on using the repository browser.