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