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