Changes in kernel/generic/src/ipc/irq.c [c822026:8add9ca5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/ipc/irq.c
rc822026 r8add9ca5 31 31 * @{ 32 32 */ 33 33 34 /** 34 35 * @file … … 41 42 * 42 43 * The structure of a notification message is as follows: 43 * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall 44 * - IMETHOD: interface and method as registered by the SYS_REGISTER_IRQ 45 * syscall 44 46 * - ARG1: payload modified by a 'top-half' handler 45 47 * - ARG2: payload modified by a 'top-half' handler … … 48 50 * - ARG5: payload modified by a 'top-half' handler 49 51 * - in_phone_hash: interrupt counter (may be needed to assure correct order 50 * in multithreaded drivers)52 * in multithreaded drivers) 51 53 * 52 54 * Note on synchronization for ipc_irq_register(), ipc_irq_unregister(), … … 67 69 * structure are finished. Because we hold the hash table lock, we prevent new 68 70 * IRQs from taking new references to the IRQ structure. 71 * 69 72 */ 70 73 … … 81 84 /** Free the top-half pseudocode. 82 85 * 83 * @param code Pointer to the top-half pseudocode. 86 * @param code Pointer to the top-half pseudocode. 87 * 84 88 */ 85 89 static void code_free(irq_code_t *code) … … 93 97 /** Copy the top-half pseudocode from userspace into the kernel. 94 98 * 95 * @param ucode Userspace address of the top-half pseudocode. 96 * 97 * @return Kernel address of the copied pseudocode. 99 * @param ucode Userspace address of the top-half pseudocode. 100 * 101 * @return Kernel address of the copied pseudocode. 102 * 98 103 */ 99 104 static irq_code_t *code_from_uspace(irq_code_t *ucode) 100 105 { 101 irq_code_t *code; 102 irq_cmd_t *ucmds; 103 int rc; 104 105 code = malloc(sizeof(*code), 0); 106 rc = copy_from_uspace(code, ucode, sizeof(*code)); 106 irq_code_t *code = malloc(sizeof(*code), 0); 107 int rc = copy_from_uspace(code, ucode, sizeof(*code)); 107 108 if (rc != 0) { 108 109 free(code); … … 114 115 return NULL; 115 116 } 116 ucmds = code->cmds; 117 118 irq_cmd_t *ucmds = code->cmds; 117 119 code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0); 118 120 rc = copy_from_uspace(code->cmds, ucmds, … … 123 125 return NULL; 124 126 } 125 127 126 128 return code; 127 129 } … … 129 131 /** Register an answerbox as a receiving end for IRQ notifications. 130 132 * 131 * @param box 132 * @param inr 133 * @param devno 134 * @param method Method to be associated with the notification.135 * @param ucode Uspace pointer to top-half pseudocode.136 * 137 * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success.133 * @param box Receiving answerbox. 134 * @param inr IRQ number. 135 * @param devno Device number. 136 * @param imethod Interface and method to be associated with the 137 * notification. 138 * @param ucode Uspace pointer to top-half pseudocode. 139 * @return EOK on success or a negative error code. 138 140 * 139 141 */ 140 142 int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, 141 unative_t method, irq_code_t *ucode) 142 { 143 ipl_t ipl; 143 sysarg_t imethod, irq_code_t *ucode) 144 { 145 sysarg_t key[] = { 146 (sysarg_t) inr, 147 (sysarg_t) devno 148 }; 149 150 if ((inr < 0) || (inr > last_inr)) 151 return ELIMIT; 152 144 153 irq_code_t *code; 145 irq_t *irq;146 link_t *hlp;147 unative_t key[] = {148 (unative_t) inr,149 (unative_t) devno150 };151 152 154 if (ucode) { 153 155 code = code_from_uspace(ucode); 154 156 if (!code) 155 157 return EBADMEM; 156 } else {158 } else 157 159 code = NULL; 158 }159 160 160 161 /* 161 162 * Allocate and populate the IRQ structure. 162 163 */ 163 irq = malloc(sizeof(irq_t), 0); 164 irq_t *irq = malloc(sizeof(irq_t), 0); 165 164 166 irq_initialize(irq); 165 167 irq->devno = devno; … … 169 171 irq->notif_cfg.notify = true; 170 172 irq->notif_cfg.answerbox = box; 171 irq->notif_cfg. method =method;173 irq->notif_cfg.imethod = imethod; 172 174 irq->notif_cfg.code = code; 173 175 irq->notif_cfg.counter = 0; … … 177 179 * answerbox's list. 178 180 */ 179 i pl = interrupts_disable();180 spinlock_lock(&irq_uspace_hash_table_lock);181 hlp = hash_table_find(&irq_uspace_hash_table, key);181 irq_spinlock_lock(&irq_uspace_hash_table_lock, true); 182 183 link_t *hlp = hash_table_find(&irq_uspace_hash_table, key); 182 184 if (hlp) { 183 irq_t *hirq __attribute__((unused)) 184 = hash_table_get_instance(hlp, irq_t, link); 185 irq_t *hirq = hash_table_get_instance(hlp, irq_t, link); 185 186 186 187 /* hirq is locked */ 187 spinlock_unlock(&hirq->lock);188 irq_spinlock_unlock(&hirq->lock, false); 188 189 code_free(code); 189 spinlock_unlock(&irq_uspace_hash_table_lock); 190 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 191 190 192 free(irq); 191 interrupts_restore(ipl);192 193 return EEXISTS; 193 194 } 194 195 195 spinlock_lock(&irq->lock); /* Not really necessary, but paranoid */ 196 spinlock_lock(&box->irq_lock); 196 /* Locking is not really necessary, but paranoid */ 197 irq_spinlock_lock(&irq->lock, false); 198 irq_spinlock_lock(&box->irq_lock, false); 199 197 200 hash_table_insert(&irq_uspace_hash_table, key, &irq->link); 198 201 list_append(&irq->notif_cfg.link, &box->irq_head); 199 spinlock_unlock(&box->irq_lock);200 spinlock_unlock(&irq->lock);201 spinlock_unlock(&irq_uspace_hash_table_lock);202 203 interrupts_restore(ipl);202 203 irq_spinlock_unlock(&box->irq_lock, false); 204 irq_spinlock_unlock(&irq->lock, false); 205 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 206 204 207 return EOK; 205 208 } … … 210 213 * @param inr IRQ number. 211 214 * @param devno Device number. 215 * @return EOK on success or a negative error code. 212 216 */ 213 217 int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno) 214 218 { 215 ipl_t ipl; 216 unative_t key[] = { 217 (unative_t) inr, 218 (unative_t) devno 219 sysarg_t key[] = { 220 (sysarg_t) inr, 221 (sysarg_t) devno 219 222 }; 220 link_t *lnk; 221 i rq_t *irq;222 223 ipl = interrupts_disable();224 spinlock_lock(&irq_uspace_hash_table_lock);225 l nk = hash_table_find(&irq_uspace_hash_table, key);223 224 if ((inr < 0) || (inr > last_inr)) 225 return ELIMIT; 226 227 irq_spinlock_lock(&irq_uspace_hash_table_lock, true); 228 link_t *lnk = hash_table_find(&irq_uspace_hash_table, key); 226 229 if (!lnk) { 227 spinlock_unlock(&irq_uspace_hash_table_lock); 228 interrupts_restore(ipl); 230 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 229 231 return ENOENT; 230 232 } 231 irq = hash_table_get_instance(lnk, irq_t, link); 233 234 irq_t *irq = hash_table_get_instance(lnk, irq_t, link); 235 232 236 /* irq is locked */ 233 spinlock_lock(&box->irq_lock);237 irq_spinlock_lock(&box->irq_lock, false); 234 238 235 239 ASSERT(irq->notif_cfg.answerbox == box); … … 237 241 /* Free up the pseudo code and associated structures. */ 238 242 code_free(irq->notif_cfg.code); 239 240 /* Remove the IRQ from the answerbox's list. */ 243 244 /* Remove the IRQ from the answerbox's list. */ 241 245 list_remove(&irq->notif_cfg.link); 242 246 243 247 /* 244 248 * We need to drop the IRQ lock now because hash_table_remove() will try … … 248 252 * the meantime. 249 253 */ 250 spinlock_unlock(&irq->lock);251 254 irq_spinlock_unlock(&irq->lock, false); 255 252 256 /* Remove the IRQ from the uspace IRQ hash table. */ 253 257 hash_table_remove(&irq_uspace_hash_table, key, 2); 254 258 255 spinlock_unlock(&irq_uspace_hash_table_lock);256 spinlock_unlock(&box->irq_lock);259 irq_spinlock_unlock(&box->irq_lock, false); 260 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 257 261 258 262 /* Free up the IRQ structure. */ 259 263 free(irq); 260 264 261 interrupts_restore(ipl);262 265 return EOK; 263 266 } 264 265 267 266 268 /** Disconnect all IRQ notifications from an answerbox. … … 270 272 * send notifications to it. 271 273 * 272 * @param box Answerbox for which we want to carry out the cleanup. 274 * @param box Answerbox for which we want to carry out the cleanup. 275 * 273 276 */ 274 277 void ipc_irq_cleanup(answerbox_t *box) 275 278 { 276 ipl_t ipl;277 278 279 loop: 279 ipl = interrupts_disable(); 280 spinlock_lock(&irq_uspace_hash_table_lock); 281 spinlock_lock(&box->irq_lock); 280 irq_spinlock_lock(&irq_uspace_hash_table_lock, true); 281 irq_spinlock_lock(&box->irq_lock, false); 282 282 283 283 while (box->irq_head.next != &box->irq_head) { 284 link_t *cur = box->irq_head.next;285 irq_t *irq;286 284 DEADLOCK_PROBE_INIT(p_irqlock); 287 unative_t key[2]; 288 289 irq = list_get_instance(cur, irq_t, notif_cfg.link); 290 if (!spinlock_trylock(&irq->lock)) { 285 286 irq_t *irq = list_get_instance(box->irq_head.next, irq_t, 287 notif_cfg.link); 288 289 if (!irq_spinlock_trylock(&irq->lock)) { 291 290 /* 292 291 * Avoid deadlock by trying again. 293 292 */ 294 spinlock_unlock(&box->irq_lock); 295 spinlock_unlock(&irq_uspace_hash_table_lock); 296 interrupts_restore(ipl); 293 irq_spinlock_unlock(&box->irq_lock, false); 294 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 297 295 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD); 298 296 goto loop; 299 297 } 298 299 sysarg_t key[2]; 300 300 key[0] = irq->inr; 301 301 key[1] = irq->devno; 302 303 302 304 303 ASSERT(irq->notif_cfg.answerbox == box); … … 317 316 * didn't drop the hash table lock in the meantime. 318 317 */ 319 spinlock_unlock(&irq->lock);318 irq_spinlock_unlock(&irq->lock, false); 320 319 321 320 /* Remove from the hash table. */ … … 325 324 } 326 325 327 spinlock_unlock(&box->irq_lock); 328 spinlock_unlock(&irq_uspace_hash_table_lock); 329 interrupts_restore(ipl); 326 irq_spinlock_unlock(&box->irq_lock, false); 327 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 330 328 } 331 329 332 330 /** Add a call to the proper answerbox queue. 333 331 * 334 * Assume irq->lock is locked. 335 * 336 * @param irq IRQ structure referencing the target answerbox. 337 * @param call IRQ notification call. 332 * Assume irq->lock is locked and interrupts disabled. 333 * 334 * @param irq IRQ structure referencing the target answerbox. 335 * @param call IRQ notification call. 336 * 338 337 */ 339 338 static void send_call(irq_t *irq, call_t *call) 340 339 { 341 spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);340 irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false); 342 341 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs); 343 spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);344 342 irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false); 343 345 344 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST); 346 345 } … … 348 347 /** Apply the top-half pseudo code to find out whether to accept the IRQ or not. 349 348 * 350 * @param irq IRQ structure. 351 * 352 * @return IRQ_ACCEPT if the interrupt is accepted by the 353 * pseudocode. IRQ_DECLINE otherwise. 349 * @param irq IRQ structure. 350 * 351 * @return IRQ_ACCEPT if the interrupt is accepted by the 352 * pseudocode, IRQ_DECLINE otherwise. 353 * 354 354 */ 355 355 irq_ownership_t ipc_irq_top_half_claim(irq_t *irq) 356 356 { 357 unsigned int i;358 unative_t dstval;359 357 irq_code_t *code = irq->notif_cfg.code; 360 unative_t *scratch = irq->notif_cfg.scratch; 361 358 uint32_t *scratch = irq->notif_cfg.scratch; 362 359 363 360 if (!irq->notif_cfg.notify) … … 367 364 return IRQ_DECLINE; 368 365 366 size_t i; 369 367 for (i = 0; i < code->cmdcount; i++) { 370 unsigned int srcarg = code->cmds[i].srcarg; 371 unsigned int dstarg = code->cmds[i].dstarg; 368 uint32_t dstval; 369 uintptr_t srcarg = code->cmds[i].srcarg; 370 uintptr_t dstarg = code->cmds[i].dstarg; 372 371 373 372 if (srcarg >= IPC_CALL_LEN) 374 373 break; 374 375 375 if (dstarg >= IPC_CALL_LEN) 376 376 break; … … 404 404 (uint32_t) code->cmds[i].value); 405 405 break; 406 case CMD_PIO_WRITE_A_8: 407 if (srcarg) { 408 pio_write_8((ioport8_t *) code->cmds[i].addr, 409 (uint8_t) scratch[srcarg]); 410 } 411 break; 412 case CMD_PIO_WRITE_A_16: 413 if (srcarg) { 414 pio_write_16((ioport16_t *) code->cmds[i].addr, 415 (uint16_t) scratch[srcarg]); 416 } 417 break; 418 case CMD_PIO_WRITE_A_32: 419 if (srcarg) { 420 pio_write_32((ioport32_t *) code->cmds[i].addr, 421 (uint32_t) scratch[srcarg]); 422 } 423 break; 406 424 case CMD_BTEST: 407 if ( srcarg && dstarg) {425 if ((srcarg) && (dstarg)) { 408 426 dstval = scratch[srcarg] & code->cmds[i].value; 409 427 scratch[dstarg] = dstval; … … 411 429 break; 412 430 case CMD_PREDICATE: 413 if ( srcarg && !scratch[srcarg]) {431 if ((srcarg) && (!scratch[srcarg])) { 414 432 i += code->cmds[i].value; 415 433 continue; … … 418 436 case CMD_ACCEPT: 419 437 return IRQ_ACCEPT; 420 break;421 438 case CMD_DECLINE: 422 439 default: … … 428 445 } 429 446 430 431 447 /* IRQ top-half handler. 432 448 * 433 449 * We expect interrupts to be disabled and the irq->lock already held. 434 450 * 435 * @param irq IRQ structure. 451 * @param irq IRQ structure. 452 * 436 453 */ 437 454 void ipc_irq_top_half_handler(irq_t *irq) … … 439 456 ASSERT(irq); 440 457 458 ASSERT(interrupts_disabled()); 459 ASSERT(irq_spinlock_locked(&irq->lock)); 460 441 461 if (irq->notif_cfg.answerbox) { 442 call_t *call; 443 444 call = ipc_call_alloc(FRAME_ATOMIC); 462 call_t *call = ipc_call_alloc(FRAME_ATOMIC); 445 463 if (!call) 446 464 return; … … 449 467 /* Put a counter to the message */ 450 468 call->priv = ++irq->notif_cfg.counter; 451 469 452 470 /* Set up args */ 453 IPC_SET_ METHOD(call->data, irq->notif_cfg.method);471 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod); 454 472 IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]); 455 473 IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]); … … 457 475 IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]); 458 476 IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]); 459 477 460 478 send_call(irq, call); 461 479 } … … 464 482 /** Send notification message. 465 483 * 466 * @param irq IRQ structure. 467 * @param a1 Driver-specific payload argument. 468 * @param a2 Driver-specific payload argument. 469 * @param a3 Driver-specific payload argument. 470 * @param a4 Driver-specific payload argument. 471 * @param a5 Driver-specific payload argument. 472 */ 473 void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3, 474 unative_t a4, unative_t a5) 475 { 476 call_t *call; 477 478 spinlock_lock(&irq->lock); 479 484 * @param irq IRQ structure. 485 * @param a1 Driver-specific payload argument. 486 * @param a2 Driver-specific payload argument. 487 * @param a3 Driver-specific payload argument. 488 * @param a4 Driver-specific payload argument. 489 * @param a5 Driver-specific payload argument. 490 * 491 */ 492 void ipc_irq_send_msg(irq_t *irq, sysarg_t a1, sysarg_t a2, sysarg_t a3, 493 sysarg_t a4, sysarg_t a5) 494 { 495 irq_spinlock_lock(&irq->lock, true); 496 480 497 if (irq->notif_cfg.answerbox) { 481 call = ipc_call_alloc(FRAME_ATOMIC);498 call_t *call = ipc_call_alloc(FRAME_ATOMIC); 482 499 if (!call) { 483 spinlock_unlock(&irq->lock);500 irq_spinlock_unlock(&irq->lock, true); 484 501 return; 485 502 } 503 486 504 call->flags |= IPC_CALL_NOTIF; 487 505 /* Put a counter to the message */ 488 506 call->priv = ++irq->notif_cfg.counter; 489 490 IPC_SET_ METHOD(call->data, irq->notif_cfg.method);507 508 IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod); 491 509 IPC_SET_ARG1(call->data, a1); 492 510 IPC_SET_ARG2(call->data, a2); … … 497 515 send_call(irq, call); 498 516 } 499 spinlock_unlock(&irq->lock); 517 518 irq_spinlock_unlock(&irq->lock, true); 500 519 } 501 520
Note:
See TracChangeset
for help on using the changeset viewer.