[162f919] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Ondrej Palkovsky
|
---|
[2b017ba] | 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 | *
|
---|
| 63 | * @param call Notification call.
|
---|
| 64 | * @param code Top-half pseudocode.
|
---|
| 65 | */
|
---|
[162f919] | 66 | static void code_execute(call_t *call, irq_code_t *code)
|
---|
| 67 | {
|
---|
| 68 | int i;
|
---|
[7f1c620] | 69 | unative_t dstval = 0;
|
---|
[ae971b3e] | 70 |
|
---|
[162f919] | 71 | if (!code)
|
---|
| 72 | return;
|
---|
| 73 |
|
---|
| 74 | for (i=0; i < code->cmdcount;i++) {
|
---|
| 75 | switch (code->cmds[i].cmd) {
|
---|
| 76 | case CMD_MEM_READ_1:
|
---|
[7f1c620] | 77 | dstval = *((uint8_t *)code->cmds[i].addr);
|
---|
[162f919] | 78 | break;
|
---|
| 79 | case CMD_MEM_READ_2:
|
---|
[7f1c620] | 80 | dstval = *((uint16_t *)code->cmds[i].addr);
|
---|
[162f919] | 81 | break;
|
---|
| 82 | case CMD_MEM_READ_4:
|
---|
[7f1c620] | 83 | dstval = *((uint32_t *)code->cmds[i].addr);
|
---|
[162f919] | 84 | break;
|
---|
| 85 | case CMD_MEM_READ_8:
|
---|
[7f1c620] | 86 | dstval = *((uint64_t *)code->cmds[i].addr);
|
---|
[162f919] | 87 | break;
|
---|
| 88 | case CMD_MEM_WRITE_1:
|
---|
[7f1c620] | 89 | *((uint8_t *)code->cmds[i].addr) = code->cmds[i].value;
|
---|
[162f919] | 90 | break;
|
---|
| 91 | case CMD_MEM_WRITE_2:
|
---|
[7f1c620] | 92 | *((uint16_t *)code->cmds[i].addr) = code->cmds[i].value;
|
---|
[162f919] | 93 | break;
|
---|
| 94 | case CMD_MEM_WRITE_4:
|
---|
[7f1c620] | 95 | *((uint32_t *)code->cmds[i].addr) = code->cmds[i].value;
|
---|
[162f919] | 96 | break;
|
---|
| 97 | case CMD_MEM_WRITE_8:
|
---|
[7f1c620] | 98 | *((uint64_t *)code->cmds[i].addr) = code->cmds[i].value;
|
---|
[162f919] | 99 | break;
|
---|
[bdc5c516] | 100 | #if defined(ia32) || defined(amd64)
|
---|
| 101 | case CMD_PORT_READ_1:
|
---|
[43752b6] | 102 | dstval = inb((long)code->cmds[i].addr);
|
---|
[bdc5c516] | 103 | break;
|
---|
| 104 | case CMD_PORT_WRITE_1:
|
---|
| 105 | outb((long)code->cmds[i].addr, code->cmds[i].value);
|
---|
| 106 | break;
|
---|
[d0c5901] | 107 | #endif
|
---|
| 108 | #if defined(ia64)
|
---|
| 109 | case CMD_IA64_GETCHAR:
|
---|
[43752b6] | 110 | dstval = _getc(&ski_uconsole);
|
---|
[d0c5901] | 111 | break;
|
---|
[732fd3c] | 112 | #endif
|
---|
[ae971b3e] | 113 | #if defined(ppc32)
|
---|
[732fd3c] | 114 | case CMD_PPC32_GETCHAR:
|
---|
[43752b6] | 115 | dstval = cuda_get_scancode();
|
---|
[732fd3c] | 116 | break;
|
---|
[bdc5c516] | 117 | #endif
|
---|
[162f919] | 118 | default:
|
---|
| 119 | break;
|
---|
| 120 | }
|
---|
[43752b6] | 121 | if (code->cmds[i].dstarg && code->cmds[i].dstarg < 4) {
|
---|
| 122 | call->data.args[code->cmds[i].dstarg] = dstval;
|
---|
| 123 | }
|
---|
[162f919] | 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | static void code_free(irq_code_t *code)
|
---|
| 128 | {
|
---|
| 129 | if (code) {
|
---|
| 130 | free(code->cmds);
|
---|
| 131 | free(code);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | static irq_code_t * code_from_uspace(irq_code_t *ucode)
|
---|
| 136 | {
|
---|
| 137 | irq_code_t *code;
|
---|
| 138 | irq_cmd_t *ucmds;
|
---|
[e3c762cd] | 139 | int rc;
|
---|
[162f919] | 140 |
|
---|
| 141 | code = malloc(sizeof(*code), 0);
|
---|
[e3c762cd] | 142 | rc = copy_from_uspace(code, ucode, sizeof(*code));
|
---|
| 143 | if (rc != 0) {
|
---|
| 144 | free(code);
|
---|
| 145 | return NULL;
|
---|
| 146 | }
|
---|
[162f919] | 147 |
|
---|
| 148 | if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
|
---|
| 149 | free(code);
|
---|
| 150 | return NULL;
|
---|
| 151 | }
|
---|
| 152 | ucmds = code->cmds;
|
---|
| 153 | code->cmds = malloc(sizeof(code->cmds[0]) * (code->cmdcount), 0);
|
---|
[e3c762cd] | 154 | rc = copy_from_uspace(code->cmds, ucmds, sizeof(code->cmds[0]) * (code->cmdcount));
|
---|
| 155 | if (rc != 0) {
|
---|
| 156 | free(code->cmds);
|
---|
| 157 | free(code);
|
---|
| 158 | return NULL;
|
---|
| 159 | }
|
---|
[162f919] | 160 |
|
---|
| 161 | return code;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[2b017ba] | 164 | /** Unregister task from IRQ notification.
|
---|
| 165 | *
|
---|
| 166 | * @param box Answerbox associated with the notification.
|
---|
| 167 | * @param inr IRQ numbe.
|
---|
| 168 | * @param devno Device number.
|
---|
| 169 | */
|
---|
| 170 | void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
|
---|
[162f919] | 171 | {
|
---|
| 172 | ipl_t ipl;
|
---|
[2b017ba] | 173 | irq_t *irq;
|
---|
[162f919] | 174 |
|
---|
| 175 | ipl = interrupts_disable();
|
---|
[2b017ba] | 176 | irq = irq_find_and_lock(inr, devno);
|
---|
| 177 | if (irq) {
|
---|
| 178 | if (irq->notif_cfg.answerbox == box) {
|
---|
[4874c2d] | 179 | irq->notif_cfg.notify = false;
|
---|
[2b017ba] | 180 | irq->notif_cfg.answerbox = NULL;
|
---|
[4874c2d] | 181 | irq->notif_cfg.code = NULL;
|
---|
[2b017ba] | 182 | irq->notif_cfg.method = 0;
|
---|
| 183 | irq->notif_cfg.counter = 0;
|
---|
[4874c2d] | 184 | code_free(irq->notif_cfg.code);
|
---|
[2b017ba] | 185 | spinlock_unlock(&irq->lock);
|
---|
| 186 | }
|
---|
[162f919] | 187 | }
|
---|
| 188 | interrupts_restore(ipl);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[2b017ba] | 191 | /** Register an answerbox as a receiving end for IRQ notifications.
|
---|
| 192 | *
|
---|
| 193 | * @param box Receiving answerbox.
|
---|
| 194 | * @param inr IRQ number.
|
---|
| 195 | * @param devno Device number.
|
---|
| 196 | * @param method Method to be associated with the notification.
|
---|
| 197 | * @param ucode Uspace pointer to top-half pseudocode.
|
---|
| 198 | *
|
---|
| 199 | * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
|
---|
| 200 | */
|
---|
| 201 | int
|
---|
| 202 | ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, unative_t method, irq_code_t *ucode)
|
---|
[162f919] | 203 | {
|
---|
| 204 | ipl_t ipl;
|
---|
| 205 | irq_code_t *code;
|
---|
[2b017ba] | 206 | irq_t *irq;
|
---|
[162f919] | 207 |
|
---|
| 208 | if (ucode) {
|
---|
| 209 | code = code_from_uspace(ucode);
|
---|
| 210 | if (!code)
|
---|
| 211 | return EBADMEM;
|
---|
| 212 | } else
|
---|
| 213 | code = NULL;
|
---|
| 214 |
|
---|
| 215 | ipl = interrupts_disable();
|
---|
[2b017ba] | 216 | irq = irq_find_and_lock(inr, devno);
|
---|
| 217 | if (!irq) {
|
---|
| 218 | interrupts_restore(ipl);
|
---|
| 219 | code_free(code);
|
---|
| 220 | return ENOENT;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if (irq->notif_cfg.answerbox) {
|
---|
| 224 | spinlock_unlock(&irq->lock);
|
---|
[162f919] | 225 | interrupts_restore(ipl);
|
---|
| 226 | code_free(code);
|
---|
| 227 | return EEXISTS;
|
---|
| 228 | }
|
---|
[2b017ba] | 229 |
|
---|
[4874c2d] | 230 | irq->notif_cfg.notify = true;
|
---|
[2b017ba] | 231 | irq->notif_cfg.answerbox = box;
|
---|
| 232 | irq->notif_cfg.method = method;
|
---|
| 233 | irq->notif_cfg.code = code;
|
---|
| 234 | irq->notif_cfg.counter = 0;
|
---|
| 235 | spinlock_unlock(&irq->lock);
|
---|
[162f919] | 236 | interrupts_restore(ipl);
|
---|
| 237 |
|
---|
| 238 | return 0;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[2b017ba] | 241 | /** Add call to proper answerbox queue.
|
---|
| 242 | *
|
---|
| 243 | * Assume irq->lock is locked.
|
---|
[874621f] | 244 | *
|
---|
[2b017ba] | 245 | */
|
---|
| 246 | static void send_call(irq_t *irq, call_t *call)
|
---|
[874621f] | 247 | {
|
---|
[2b017ba] | 248 | spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
|
---|
| 249 | list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
|
---|
| 250 | spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
|
---|
[874621f] | 251 |
|
---|
[2b017ba] | 252 | waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
|
---|
[874621f] | 253 | }
|
---|
| 254 |
|
---|
| 255 | /** Send notification message
|
---|
| 256 | *
|
---|
| 257 | */
|
---|
[2b017ba] | 258 | void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3)
|
---|
[874621f] | 259 | {
|
---|
| 260 | call_t *call;
|
---|
| 261 |
|
---|
[2b017ba] | 262 | spinlock_lock(&irq->lock);
|
---|
[874621f] | 263 |
|
---|
[2b017ba] | 264 | if (irq->notif_cfg.answerbox) {
|
---|
[874621f] | 265 | call = ipc_call_alloc(FRAME_ATOMIC);
|
---|
| 266 | if (!call) {
|
---|
[2b017ba] | 267 | spinlock_unlock(&irq->lock);
|
---|
[874621f] | 268 | return;
|
---|
| 269 | }
|
---|
| 270 | call->flags |= IPC_CALL_NOTIF;
|
---|
[2b017ba] | 271 | IPC_SET_METHOD(call->data, irq->notif_cfg.method);
|
---|
[43752b6] | 272 | IPC_SET_ARG1(call->data, a1);
|
---|
[874621f] | 273 | IPC_SET_ARG2(call->data, a2);
|
---|
| 274 | IPC_SET_ARG3(call->data, a3);
|
---|
[43752b6] | 275 | /* Put a counter to the message */
|
---|
[2b017ba] | 276 | call->private = ++irq->notif_cfg.counter;
|
---|
[874621f] | 277 |
|
---|
[2b017ba] | 278 | send_call(irq, call);
|
---|
[874621f] | 279 | }
|
---|
[2b017ba] | 280 | spinlock_unlock(&irq->lock);
|
---|
[874621f] | 281 | }
|
---|
| 282 |
|
---|
[f3a3f0d7] | 283 | /** Notify task that an irq had occurred.
|
---|
[162f919] | 284 | *
|
---|
[2b017ba] | 285 | * We expect interrupts to be disabled and the irq->lock already held.
|
---|
[162f919] | 286 | */
|
---|
[2b017ba] | 287 | void ipc_irq_send_notif(irq_t *irq)
|
---|
[162f919] | 288 | {
|
---|
| 289 | call_t *call;
|
---|
| 290 |
|
---|
[2b017ba] | 291 | ASSERT(irq);
|
---|
[162f919] | 292 |
|
---|
[2b017ba] | 293 | if (irq->notif_cfg.answerbox) {
|
---|
[162f919] | 294 | call = ipc_call_alloc(FRAME_ATOMIC);
|
---|
[d8f7362] | 295 | if (!call) {
|
---|
| 296 | return;
|
---|
| 297 | }
|
---|
[162f919] | 298 | call->flags |= IPC_CALL_NOTIF;
|
---|
[43752b6] | 299 | /* Put a counter to the message */
|
---|
[2b017ba] | 300 | call->private = ++irq->notif_cfg.counter;
|
---|
[43752b6] | 301 | /* Set up args */
|
---|
[2b017ba] | 302 | IPC_SET_METHOD(call->data, irq->notif_cfg.method);
|
---|
[162f919] | 303 |
|
---|
| 304 | /* Execute code to handle irq */
|
---|
[2b017ba] | 305 | code_execute(call, irq->notif_cfg.code);
|
---|
[874621f] | 306 |
|
---|
[2b017ba] | 307 | send_call(irq, call);
|
---|
[162f919] | 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[2b017ba] | 311 | /** Disconnect all IRQ notifications from an answerbox.
|
---|
[874621f] | 312 | *
|
---|
[2b017ba] | 313 | * @param box Answerbox for which we want to carry out the cleanup.
|
---|
[162f919] | 314 | */
|
---|
| 315 | void ipc_irq_cleanup(answerbox_t *box)
|
---|
| 316 | {
|
---|
[2b017ba] | 317 | /* TODO */
|
---|
[162f919] | 318 | }
|
---|
[b45c443] | 319 |
|
---|
[cc73a8a1] | 320 | /** @}
|
---|
[b45c443] | 321 | */
|
---|