Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/ipc/irq.c

    rc822026 r8add9ca5  
    3131 * @{
    3232 */
     33
    3334/**
    3435 * @file
     
    4142 *
    4243 * 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
    4446 * - ARG1: payload modified by a 'top-half' handler
    4547 * - ARG2: payload modified by a 'top-half' handler
     
    4850 * - ARG5: payload modified by a 'top-half' handler
    4951 * - in_phone_hash: interrupt counter (may be needed to assure correct order
    50  *         in multithreaded drivers)
     52 *                  in multithreaded drivers)
    5153 *
    5254 * Note on synchronization for ipc_irq_register(), ipc_irq_unregister(),
     
    6769 *   structure are finished. Because we hold the hash table lock, we prevent new
    6870 *   IRQs from taking new references to the IRQ structure.
     71 *
    6972 */
    7073
     
    8184/** Free the top-half pseudocode.
    8285 *
    83  * @param code          Pointer to the top-half pseudocode.
     86 * @param code Pointer to the top-half pseudocode.
     87 *
    8488 */
    8589static void code_free(irq_code_t *code)
     
    9397/** Copy the top-half pseudocode from userspace into the kernel.
    9498 *
    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 *
    98103 */
    99104static irq_code_t *code_from_uspace(irq_code_t *ucode)
    100105{
    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));
    107108        if (rc != 0) {
    108109                free(code);
     
    114115                return NULL;
    115116        }
    116         ucmds = code->cmds;
     117       
     118        irq_cmd_t *ucmds = code->cmds;
    117119        code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
    118120        rc = copy_from_uspace(code->cmds, ucmds,
     
    123125                return NULL;
    124126        }
    125 
     127       
    126128        return code;
    127129}
     
    129131/** Register an answerbox as a receiving end for IRQ notifications.
    130132 *
    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.
     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.
    138140 *
    139141 */
    140142int 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       
    144153        irq_code_t *code;
    145         irq_t *irq;
    146         link_t *hlp;
    147         unative_t key[] = {
    148                 (unative_t) inr,
    149                 (unative_t) devno
    150         };
    151        
    152154        if (ucode) {
    153155                code = code_from_uspace(ucode);
    154156                if (!code)
    155157                        return EBADMEM;
    156         } else {
     158        } else
    157159                code = NULL;
    158         }
    159160       
    160161        /*
    161162         * Allocate and populate the IRQ structure.
    162163         */
    163         irq = malloc(sizeof(irq_t), 0);
     164        irq_t *irq = malloc(sizeof(irq_t), 0);
     165       
    164166        irq_initialize(irq);
    165167        irq->devno = devno;
     
    169171        irq->notif_cfg.notify = true;
    170172        irq->notif_cfg.answerbox = box;
    171         irq->notif_cfg.method = method;
     173        irq->notif_cfg.imethod = imethod;
    172174        irq->notif_cfg.code = code;
    173175        irq->notif_cfg.counter = 0;
     
    177179         * answerbox's list.
    178180         */
    179         ipl = 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);
    182184        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);
    185186               
    186187                /* hirq is locked */
    187                 spinlock_unlock(&hirq->lock);
     188                irq_spinlock_unlock(&hirq->lock, false);
    188189                code_free(code);
    189                 spinlock_unlock(&irq_uspace_hash_table_lock);
     190                irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
     191               
    190192                free(irq);
    191                 interrupts_restore(ipl);
    192193                return EEXISTS;
    193194        }
    194195       
    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       
    197200        hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
    198201        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       
    204207        return EOK;
    205208}
     
    210213 * @param inr           IRQ number.
    211214 * @param devno         Device number.
     215 * @return              EOK on success or a negative error code.
    212216 */
    213217int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
    214218{
    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
    219222        };
    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);
     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);
    226229        if (!lnk) {
    227                 spinlock_unlock(&irq_uspace_hash_table_lock);
    228                 interrupts_restore(ipl);
     230                irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
    229231                return ENOENT;
    230232        }
    231         irq = hash_table_get_instance(lnk, irq_t, link);
     233       
     234        irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
     235       
    232236        /* irq is locked */
    233         spinlock_lock(&box->irq_lock);
     237        irq_spinlock_lock(&box->irq_lock, false);
    234238       
    235239        ASSERT(irq->notif_cfg.answerbox == box);
     
    237241        /* Free up the pseudo code and associated structures. */
    238242        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. */
    241245        list_remove(&irq->notif_cfg.link);
    242 
     246       
    243247        /*
    244248         * We need to drop the IRQ lock now because hash_table_remove() will try
     
    248252         * the meantime.
    249253         */
    250         spinlock_unlock(&irq->lock);
    251 
     254        irq_spinlock_unlock(&irq->lock, false);
     255       
    252256        /* Remove the IRQ from the uspace IRQ hash table. */
    253257        hash_table_remove(&irq_uspace_hash_table, key, 2);
    254258       
    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);
    257261       
    258262        /* Free up the IRQ structure. */
    259263        free(irq);
    260264       
    261         interrupts_restore(ipl);
    262265        return EOK;
    263266}
    264 
    265267
    266268/** Disconnect all IRQ notifications from an answerbox.
     
    270272 * send notifications to it.
    271273 *
    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 *
    273276 */
    274277void ipc_irq_cleanup(answerbox_t *box)
    275278{
    276         ipl_t ipl;
    277        
    278279loop:
    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);
    282282       
    283283        while (box->irq_head.next != &box->irq_head) {
    284                 link_t *cur = box->irq_head.next;
    285                 irq_t *irq;
    286284                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)) {
    291290                        /*
    292291                         * Avoid deadlock by trying again.
    293292                         */
    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);
    297295                        DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
    298296                        goto loop;
    299297                }
     298               
     299                sysarg_t key[2];
    300300                key[0] = irq->inr;
    301301                key[1] = irq->devno;
    302                
    303302               
    304303                ASSERT(irq->notif_cfg.answerbox == box);
     
    317316                 * didn't drop the hash table lock in the meantime.
    318317                 */
    319                 spinlock_unlock(&irq->lock);
     318                irq_spinlock_unlock(&irq->lock, false);
    320319               
    321320                /* Remove from the hash table. */
     
    325324        }
    326325       
    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);
    330328}
    331329
    332330/** Add a call to the proper answerbox queue.
    333331 *
    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 *
    338337 */
    339338static void send_call(irq_t *irq, call_t *call)
    340339{
    341         spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
     340        irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);
    342341        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       
    345344        waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
    346345}
     
    348347/** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
    349348 *
    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 *
    354354 */
    355355irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
    356356{
    357         unsigned int i;
    358         unative_t dstval;
    359357        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;
    362359       
    363360        if (!irq->notif_cfg.notify)
     
    367364                return IRQ_DECLINE;
    368365       
     366        size_t i;
    369367        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;
    372371               
    373372                if (srcarg >= IPC_CALL_LEN)
    374373                        break;
     374               
    375375                if (dstarg >= IPC_CALL_LEN)
    376376                        break;
     
    404404                            (uint32_t) code->cmds[i].value);
    405405                        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;
    406424                case CMD_BTEST:
    407                         if (srcarg && dstarg) {
     425                        if ((srcarg) && (dstarg)) {
    408426                                dstval = scratch[srcarg] & code->cmds[i].value;
    409427                                scratch[dstarg] = dstval;
     
    411429                        break;
    412430                case CMD_PREDICATE:
    413                         if (srcarg && !scratch[srcarg]) {
     431                        if ((srcarg) && (!scratch[srcarg])) {
    414432                                i += code->cmds[i].value;
    415433                                continue;
     
    418436                case CMD_ACCEPT:
    419437                        return IRQ_ACCEPT;
    420                         break;
    421438                case CMD_DECLINE:
    422439                default:
     
    428445}
    429446
    430 
    431447/* IRQ top-half handler.
    432448 *
    433449 * We expect interrupts to be disabled and the irq->lock already held.
    434450 *
    435  * @param irq           IRQ structure.
     451 * @param irq IRQ structure.
     452 *
    436453 */
    437454void ipc_irq_top_half_handler(irq_t *irq)
     
    439456        ASSERT(irq);
    440457
     458        ASSERT(interrupts_disabled());
     459        ASSERT(irq_spinlock_locked(&irq->lock));
     460       
    441461        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);
    445463                if (!call)
    446464                        return;
     
    449467                /* Put a counter to the message */
    450468                call->priv = ++irq->notif_cfg.counter;
    451 
     469               
    452470                /* Set up args */
    453                 IPC_SET_METHOD(call->data, irq->notif_cfg.method);
     471                IPC_SET_IMETHOD(call->data, irq->notif_cfg.imethod);
    454472                IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]);
    455473                IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]);
     
    457475                IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
    458476                IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
    459 
     477               
    460478                send_call(irq, call);
    461479        }
     
    464482/** Send notification message.
    465483 *
    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 */
     492void 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       
    480497        if (irq->notif_cfg.answerbox) {
    481                 call = ipc_call_alloc(FRAME_ATOMIC);
     498                call_t *call = ipc_call_alloc(FRAME_ATOMIC);
    482499                if (!call) {
    483                         spinlock_unlock(&irq->lock);
     500                        irq_spinlock_unlock(&irq->lock, true);
    484501                        return;
    485502                }
     503               
    486504                call->flags |= IPC_CALL_NOTIF;
    487505                /* Put a counter to the message */
    488506                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);
    491509                IPC_SET_ARG1(call->data, a1);
    492510                IPC_SET_ARG2(call->data, a2);
     
    497515                send_call(irq, call);
    498516        }
    499         spinlock_unlock(&irq->lock);
     517       
     518        irq_spinlock_unlock(&irq->lock, true);
    500519}
    501520
Note: See TracChangeset for help on using the changeset viewer.