Changes in kernel/generic/src/ipc/irq.c [1d432f9:a70bda4] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/ipc/irq.c
r1d432f9 ra70bda4 31 31 * @{ 32 32 */ 33 34 33 /** 35 34 * @file … … 68 67 * structure are finished. Because we hold the hash table lock, we prevent new 69 68 * IRQs from taking new references to the IRQ structure. 70 *71 69 */ 72 70 … … 83 81 /** Free the top-half pseudocode. 84 82 * 85 * @param code Pointer to the top-half pseudocode. 86 * 83 * @param code Pointer to the top-half pseudocode. 87 84 */ 88 85 static void code_free(irq_code_t *code) … … 96 93 /** Copy the top-half pseudocode from userspace into the kernel. 97 94 * 98 * @param ucode Userspace address of the top-half pseudocode. 99 * 100 * @return Kernel address of the copied pseudocode. 101 * 95 * @param ucode Userspace address of the top-half pseudocode. 96 * 97 * @return Kernel address of the copied pseudocode. 102 98 */ 103 99 static irq_code_t *code_from_uspace(irq_code_t *ucode) 104 100 { 105 irq_code_t *code = malloc(sizeof(*code), 0); 106 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)); 107 107 if (rc != 0) { 108 108 free(code); … … 114 114 return NULL; 115 115 } 116 117 irq_cmd_t *ucmds = code->cmds; 116 ucmds = code->cmds; 118 117 code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0); 119 118 rc = copy_from_uspace(code->cmds, ucmds, … … 124 123 return NULL; 125 124 } 126 125 127 126 return code; 128 127 } … … 142 141 unative_t method, irq_code_t *ucode) 143 142 { 143 ipl_t ipl; 144 irq_code_t *code; 145 irq_t *irq; 146 link_t *hlp; 144 147 unative_t key[] = { 145 148 (unative_t) inr, … … 147 150 }; 148 151 149 irq_code_t *code;150 152 if (ucode) { 151 153 code = code_from_uspace(ucode); 152 154 if (!code) 153 155 return EBADMEM; 154 } else 156 } else { 155 157 code = NULL; 158 } 156 159 157 160 /* 158 161 * Allocate and populate the IRQ structure. 159 162 */ 160 irq_t *irq = malloc(sizeof(irq_t), 0); 161 163 irq = malloc(sizeof(irq_t), 0); 162 164 irq_initialize(irq); 163 165 irq->devno = devno; … … 175 177 * answerbox's list. 176 178 */ 177 i rq_spinlock_lock(&irq_uspace_hash_table_lock, true);178 179 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); 180 182 if (hlp) { 181 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); 182 185 183 186 /* hirq is locked */ 184 irq_spinlock_unlock(&hirq->lock, false);187 spinlock_unlock(&hirq->lock); 185 188 code_free(code); 186 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 187 189 spinlock_unlock(&irq_uspace_hash_table_lock); 188 190 free(irq); 191 interrupts_restore(ipl); 189 192 return EEXISTS; 190 193 } 191 194 192 /* Locking is not really necessary, but paranoid */ 193 irq_spinlock_lock(&irq->lock, false); 194 irq_spinlock_lock(&box->irq_lock, false); 195 195 spinlock_lock(&irq->lock); /* Not really necessary, but paranoid */ 196 spinlock_lock(&box->irq_lock); 196 197 hash_table_insert(&irq_uspace_hash_table, key, &irq->link); 197 198 list_append(&irq->notif_cfg.link, &box->irq_head); 198 199 irq_spinlock_unlock(&box->irq_lock, false);200 irq_spinlock_unlock(&irq->lock, false);201 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);202 199 spinlock_unlock(&box->irq_lock); 200 spinlock_unlock(&irq->lock); 201 spinlock_unlock(&irq_uspace_hash_table_lock); 202 203 interrupts_restore(ipl); 203 204 return EOK; 204 205 } … … 206 207 /** Unregister task from IRQ notification. 207 208 * 208 * @param box Answerbox associated with the notification. 209 * @param inr IRQ number. 210 * @param devno Device number. 211 * 209 * @param box Answerbox associated with the notification. 210 * @param inr IRQ number. 211 * @param devno Device number. 212 212 */ 213 213 int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno) 214 214 { 215 ipl_t ipl; 215 216 unative_t key[] = { 216 217 (unative_t) inr, 217 218 (unative_t) devno 218 219 }; 219 220 irq_spinlock_lock(&irq_uspace_hash_table_lock, true); 221 link_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); 222 226 if (!lnk) { 223 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true); 227 spinlock_unlock(&irq_uspace_hash_table_lock); 228 interrupts_restore(ipl); 224 229 return ENOENT; 225 230 } 226 227 irq_t *irq = hash_table_get_instance(lnk, irq_t, link); 228 231 irq = hash_table_get_instance(lnk, irq_t, link); 229 232 /* irq is locked */ 230 irq_spinlock_lock(&box->irq_lock, false);233 spinlock_lock(&box->irq_lock); 231 234 232 235 ASSERT(irq->notif_cfg.answerbox == box); … … 234 237 /* Free up the pseudo code and associated structures. */ 235 238 code_free(irq->notif_cfg.code); 236 237 /* Remove the IRQ from the answerbox's list. */ 239 240 /* Remove the IRQ from the answerbox's list. */ 238 241 list_remove(&irq->notif_cfg.link); 239 242 240 243 /* 241 244 * We need to drop the IRQ lock now because hash_table_remove() will try … … 245 248 * the meantime. 246 249 */ 247 irq_spinlock_unlock(&irq->lock, false);248 250 spinlock_unlock(&irq->lock); 251 249 252 /* Remove the IRQ from the uspace IRQ hash table. */ 250 253 hash_table_remove(&irq_uspace_hash_table, key, 2); 251 254 252 irq_spinlock_unlock(&box->irq_lock, false);253 irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);255 spinlock_unlock(&irq_uspace_hash_table_lock); 256 spinlock_unlock(&box->irq_lock); 254 257 255 258 /* Free up the IRQ structure. */ 256 259 free(irq); 257 260 261 interrupts_restore(ipl); 258 262 return EOK; 259 263 } 264 260 265 261 266 /** Disconnect all IRQ notifications from an answerbox. … … 265 270 * send notifications to it. 266 271 * 267 * @param box Answerbox for which we want to carry out the cleanup. 268 * 272 * @param box Answerbox for which we want to carry out the cleanup. 269 273 */ 270 274 void ipc_irq_cleanup(answerbox_t *box) 271 275 { 276 ipl_t ipl; 277 272 278 loop: 273 irq_spinlock_lock(&irq_uspace_hash_table_lock, true); 274 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); 275 282 276 283 while (box->irq_head.next != &box->irq_head) { 284 link_t *cur = box->irq_head.next; 285 irq_t *irq; 277 286 DEADLOCK_PROBE_INIT(p_irqlock); 278 279 irq_t *irq = list_get_instance(box->irq_head.next, irq_t, 280 notif_cfg.link); 281 282 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)) { 283 291 /* 284 292 * Avoid deadlock by trying again. 285 293 */ 286 irq_spinlock_unlock(&box->irq_lock, false); 287 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); 288 297 DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD); 289 298 goto loop; 290 299 } 291 292 unative_t key[2];293 300 key[0] = irq->inr; 294 301 key[1] = irq->devno; 302 295 303 296 304 ASSERT(irq->notif_cfg.answerbox == box); … … 309 317 * didn't drop the hash table lock in the meantime. 310 318 */ 311 irq_spinlock_unlock(&irq->lock, false);319 spinlock_unlock(&irq->lock); 312 320 313 321 /* Remove from the hash table. */ … … 317 325 } 318 326 319 irq_spinlock_unlock(&box->irq_lock, false); 320 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); 321 330 } 322 331 323 332 /** Add a call to the proper answerbox queue. 324 333 * 325 * Assume irq->lock is locked and interrupts disabled. 326 * 327 * @param irq IRQ structure referencing the target answerbox. 328 * @param call IRQ notification call. 329 * 334 * Assume irq->lock is locked. 335 * 336 * @param irq IRQ structure referencing the target answerbox. 337 * @param call IRQ notification call. 330 338 */ 331 339 static void send_call(irq_t *irq, call_t *call) 332 340 { 333 irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);341 spinlock_lock(&irq->notif_cfg.answerbox->irq_lock); 334 342 list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs); 335 irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false);336 343 spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock); 344 337 345 waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST); 338 346 } … … 340 348 /** Apply the top-half pseudo code to find out whether to accept the IRQ or not. 341 349 * 342 * @param irq IRQ structure. 343 * 344 * @return IRQ_ACCEPT if the interrupt is accepted by the 345 * pseudocode, IRQ_DECLINE otherwise. 346 * 350 * @param irq IRQ structure. 351 * 352 * @return IRQ_ACCEPT if the interrupt is accepted by the 353 * pseudocode. IRQ_DECLINE otherwise. 347 354 */ 348 355 irq_ownership_t ipc_irq_top_half_claim(irq_t *irq) 349 356 { 357 unsigned int i; 358 unative_t dstval; 350 359 irq_code_t *code = irq->notif_cfg.code; 351 uint32_t *scratch = irq->notif_cfg.scratch; 360 unative_t *scratch = irq->notif_cfg.scratch; 361 352 362 353 363 if (!irq->notif_cfg.notify) … … 357 367 return IRQ_DECLINE; 358 368 359 size_t i;360 369 for (i = 0; i < code->cmdcount; i++) { 361 uint32_t dstval; 362 uintptr_t srcarg = code->cmds[i].srcarg; 363 uintptr_t dstarg = code->cmds[i].dstarg; 370 unsigned int srcarg = code->cmds[i].srcarg; 371 unsigned int dstarg = code->cmds[i].dstarg; 364 372 365 373 if (srcarg >= IPC_CALL_LEN) 366 374 break; 367 368 375 if (dstarg >= IPC_CALL_LEN) 369 376 break; … … 398 405 break; 399 406 case CMD_BTEST: 400 if ( (srcarg) && (dstarg)) {407 if (srcarg && dstarg) { 401 408 dstval = scratch[srcarg] & code->cmds[i].value; 402 409 scratch[dstarg] = dstval; … … 404 411 break; 405 412 case CMD_PREDICATE: 406 if ( (srcarg) && (!scratch[srcarg])) {413 if (srcarg && !scratch[srcarg]) { 407 414 i += code->cmds[i].value; 408 415 continue; … … 420 427 } 421 428 429 422 430 /* IRQ top-half handler. 423 431 * 424 432 * We expect interrupts to be disabled and the irq->lock already held. 425 433 * 426 * @param irq IRQ structure. 427 * 434 * @param irq IRQ structure. 428 435 */ 429 436 void ipc_irq_top_half_handler(irq_t *irq) … … 431 438 ASSERT(irq); 432 439 433 ASSERT(interrupts_disabled());434 ASSERT(irq_spinlock_locked(&irq->lock));435 436 440 if (irq->notif_cfg.answerbox) { 437 call_t *call = ipc_call_alloc(FRAME_ATOMIC); 441 call_t *call; 442 443 call = ipc_call_alloc(FRAME_ATOMIC); 438 444 if (!call) 439 445 return; … … 442 448 /* Put a counter to the message */ 443 449 call->priv = ++irq->notif_cfg.counter; 444 450 445 451 /* Set up args */ 446 452 IPC_SET_METHOD(call->data, irq->notif_cfg.method); … … 450 456 IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]); 451 457 IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]); 452 458 453 459 send_call(irq, call); 454 460 } … … 457 463 /** Send notification message. 458 464 * 459 * @param irq IRQ structure. 460 * @param a1 Driver-specific payload argument. 461 * @param a2 Driver-specific payload argument. 462 * @param a3 Driver-specific payload argument. 463 * @param a4 Driver-specific payload argument. 464 * @param a5 Driver-specific payload argument. 465 * 465 * @param irq IRQ structure. 466 * @param a1 Driver-specific payload argument. 467 * @param a2 Driver-specific payload argument. 468 * @param a3 Driver-specific payload argument. 469 * @param a4 Driver-specific payload argument. 470 * @param a5 Driver-specific payload argument. 466 471 */ 467 472 void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3, 468 473 unative_t a4, unative_t a5) 469 474 { 470 irq_spinlock_lock(&irq->lock, true); 471 475 call_t *call; 476 477 spinlock_lock(&irq->lock); 478 472 479 if (irq->notif_cfg.answerbox) { 473 call _t *call= ipc_call_alloc(FRAME_ATOMIC);480 call = ipc_call_alloc(FRAME_ATOMIC); 474 481 if (!call) { 475 irq_spinlock_unlock(&irq->lock, true);482 spinlock_unlock(&irq->lock); 476 483 return; 477 484 } 478 479 485 call->flags |= IPC_CALL_NOTIF; 480 486 /* Put a counter to the message */ 481 487 call->priv = ++irq->notif_cfg.counter; 482 488 483 489 IPC_SET_METHOD(call->data, irq->notif_cfg.method); 484 490 IPC_SET_ARG1(call->data, a1); … … 490 496 send_call(irq, call); 491 497 } 492 493 irq_spinlock_unlock(&irq->lock, true); 498 spinlock_unlock(&irq->lock); 494 499 } 495 500
Note:
See TracChangeset
for help on using the changeset viewer.