Subversion Repositories HelenOS

Rev

Rev 4153 | Rev 4263 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Ondrej Palkovsky
  3.  * Copyright (c) 2006 Jakub Jermar
  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.  
  30. /** @addtogroup genericipc
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  * @brief IRQ notification framework.
  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:
  43.  * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall
  44.  * - ARG1: payload modified by a 'top-half' handler
  45.  * - ARG2: payload modified by a 'top-half' handler
  46.  * - ARG3: payload modified by a 'top-half' handler
  47.  * - ARG4: payload modified by a 'top-half' handler
  48.  * - ARG5: payload modified by a 'top-half' handler
  49.  * - in_phone_hash: interrupt counter (may be needed to assure correct order
  50.  *         in multithreaded drivers)
  51.  *
  52.  * Note on synchronization for ipc_irq_register(), ipc_irq_unregister(),
  53.  * ipc_irq_cleanup() and IRQ handlers:
  54.  *
  55.  *   By always taking all of the uspace IRQ hash table lock, IRQ structure lock
  56.  *   and answerbox lock, we can rule out race conditions between the
  57.  *   registration functions and also the cleanup function. Thus the observer can
  58.  *   either see the IRQ structure present in both the hash table and the
  59.  *   answerbox list or absent in both. Views in which the IRQ structure would be
  60.  *   linked in the hash table but not in the answerbox list, or vice versa, are
  61.  *   not possible.
  62.  *
  63.  *   By always taking the hash table lock and the IRQ structure lock, we can
  64.  *   rule out a scenario in which we would free up an IRQ structure, which is
  65.  *   still referenced by, for example, an IRQ handler. The locking scheme forces
  66.  *   us to lock the IRQ structure only after any progressing IRQs on that
  67.  *   structure are finished. Because we hold the hash table lock, we prevent new
  68.  *   IRQs from taking new references to the IRQ structure.
  69.  */
  70.  
  71. #include <arch.h>
  72. #include <mm/slab.h>
  73. #include <errno.h>
  74. #include <ddi/irq.h>
  75. #include <ipc/ipc.h>
  76. #include <ipc/irq.h>
  77. #include <syscall/copy.h>
  78. #include <console/console.h>
  79. #include <print.h>
  80. // explicitly enable irq
  81. #include <arch/interrupt.h>
  82.  
  83. /** Free the top-half pseudocode.
  84.  *
  85.  * @param code      Pointer to the top-half pseudocode.
  86.  */
  87. static void code_free(irq_code_t *code)
  88. {
  89.     if (code) {
  90.         free(code->cmds);
  91.         free(code);
  92.     }
  93. }
  94.  
  95. /** Copy the top-half pseudocode from userspace into the kernel.
  96.  *
  97.  * @param ucode     Userspace address of the top-half pseudocode.
  98.  *
  99.  * @return      Kernel address of the copied pseudocode.
  100.  */
  101. static irq_code_t *code_from_uspace(irq_code_t *ucode)
  102. {
  103.     irq_code_t *code;
  104.     irq_cmd_t *ucmds;
  105.     int rc;
  106.  
  107.     code = malloc(sizeof(*code), 0);
  108.     rc = copy_from_uspace(code, ucode, sizeof(*code));
  109.     if (rc != 0) {
  110.         free(code);
  111.         return NULL;
  112.     }
  113.    
  114.     if (code->cmdcount > IRQ_MAX_PROG_SIZE) {
  115.         free(code);
  116.         return NULL;
  117.     }
  118.     ucmds = code->cmds;
  119.     code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
  120.     rc = copy_from_uspace(code->cmds, ucmds,
  121.         sizeof(code->cmds[0]) * code->cmdcount);
  122.     if (rc != 0) {
  123.         free(code->cmds);
  124.         free(code);
  125.         return NULL;
  126.     }
  127.  
  128.     return code;
  129. }
  130.  
  131. /** Register an answerbox as a receiving end for IRQ notifications.
  132.  *
  133.  * @param box       Receiving answerbox.
  134.  * @param inr       IRQ number.
  135.  * @param devno     Device number.
  136.  * @param method    Method to be associated with the notification.
  137.  * @param ucode     Uspace pointer to top-half pseudocode.
  138.  *
  139.  * @return      EBADMEM, ENOENT or EEXISTS on failure or 0 on success.
  140.  */
  141. int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno,
  142.     unative_t method, irq_code_t *ucode)
  143. {
  144.     ipl_t ipl;
  145.     irq_code_t *code;
  146.     irq_t *irq;
  147.     unative_t key[] = {
  148.         (unative_t) inr,
  149.         (unative_t) devno
  150.     };
  151.  
  152.     if (ucode) {
  153.         code = code_from_uspace(ucode);
  154.         if (!code)
  155.             return EBADMEM;
  156.     } else {
  157.         code = NULL;
  158.     }
  159.  
  160.     /*
  161.      * Allocate and populate the IRQ structure.
  162.      */
  163.     irq = malloc(sizeof(irq_t), 0);
  164.     irq_initialize(irq);
  165.     irq->devno = devno;
  166.     irq->inr = inr;
  167.     irq->claim = ipc_irq_top_half_claim;
  168.     irq->handler = ipc_irq_top_half_handler;
  169.     irq->notif_cfg.notify = true;
  170.     irq->notif_cfg.answerbox = box;
  171.     irq->notif_cfg.method = method;
  172.     irq->notif_cfg.code = code;
  173.     irq->notif_cfg.counter = 0;
  174.  
  175.     /*
  176.      * Enlist the IRQ structure in the uspace IRQ hash table and the
  177.      * answerbox's list.
  178.      */
  179.     ipl = interrupts_disable();
  180.     spinlock_lock(&irq_uspace_hash_table_lock);
  181.     spinlock_lock(&irq->lock);
  182.     spinlock_lock(&box->irq_lock);
  183.     if (hash_table_find(&irq_uspace_hash_table, key)) {
  184.         code_free(code);
  185.         spinlock_unlock(&box->irq_lock);
  186.         spinlock_unlock(&irq->lock);
  187.         spinlock_unlock(&irq_uspace_hash_table_lock);
  188.         free(irq);
  189.         interrupts_restore(ipl);
  190.         return EEXISTS;
  191.     }
  192.     hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
  193.     list_append(&irq->notif_cfg.link, &box->irq_head);
  194.     spinlock_unlock(&box->irq_lock);
  195.     spinlock_unlock(&irq->lock);
  196.     spinlock_unlock(&irq_uspace_hash_table_lock);
  197.  
  198.     interrupts_restore(ipl);
  199. //  explicitly enable irq
  200. /*  different byteorder?
  201.  *  trap_virtual_enable_irqs( 1 << ( irq->inr - 1 ));
  202.  */
  203.     trap_virtual_enable_irqs( 1 << ( irq->inr + 7 ));
  204.     return EOK;
  205. }
  206.  
  207. /** Unregister task from IRQ notification.
  208.  *
  209.  * @param box       Answerbox associated with the notification.
  210.  * @param inr       IRQ number.
  211.  * @param devno     Device number.
  212.  */
  213. int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
  214. {
  215.     ipl_t ipl;
  216.     unative_t key[] = {
  217.         (unative_t) inr,
  218.         (unative_t) devno
  219.     };
  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);
  226.     if (!lnk) {
  227.         spinlock_unlock(&irq_uspace_hash_table_lock);
  228.         interrupts_restore(ipl);
  229.         return ENOENT;
  230.     }
  231.     irq = hash_table_get_instance(lnk, irq_t, link);
  232.     spinlock_lock(&irq->lock);
  233.     spinlock_lock(&box->irq_lock);
  234.    
  235.     ASSERT(irq->notif_cfg.answerbox == box);
  236.    
  237.     /* Free up the pseudo code and associated structures. */
  238.     code_free(irq->notif_cfg.code);
  239.  
  240.     /* Remove the IRQ from the answerbox's list. */
  241.     list_remove(&irq->notif_cfg.link);
  242.  
  243.     /* Remove the IRQ from the uspace IRQ hash table. */
  244.     hash_table_remove(&irq_uspace_hash_table, key, 2);
  245.    
  246.     spinlock_unlock(&irq_uspace_hash_table_lock);
  247.     spinlock_unlock(&irq->lock);
  248.     spinlock_unlock(&box->irq_lock);
  249.    
  250.     /* Free up the IRQ structure. */
  251.     free(irq);
  252.    
  253.     interrupts_restore(ipl);
  254.     return EOK;
  255. }
  256.  
  257.  
  258. /** Disconnect all IRQ notifications from an answerbox.
  259.  *
  260.  * This function is effective because the answerbox contains
  261.  * list of all irq_t structures that are registered to
  262.  * send notifications to it.
  263.  *
  264.  * @param box       Answerbox for which we want to carry out the cleanup.
  265.  */
  266. void ipc_irq_cleanup(answerbox_t *box)
  267. {
  268.     ipl_t ipl;
  269.    
  270. loop:
  271.     ipl = interrupts_disable();
  272.     spinlock_lock(&irq_uspace_hash_table_lock);
  273.     spinlock_lock(&box->irq_lock);
  274.    
  275.     while (box->irq_head.next != &box->irq_head) {
  276.         link_t *cur = box->irq_head.next;
  277.         irq_t *irq;
  278.         DEADLOCK_PROBE_INIT(p_irqlock);
  279.         unative_t key[2];
  280.        
  281.         irq = list_get_instance(cur, irq_t, notif_cfg.link);
  282.         if (!spinlock_trylock(&irq->lock)) {
  283.             /*
  284.              * Avoid deadlock by trying again.
  285.              */
  286.             spinlock_unlock(&box->irq_lock);
  287.             spinlock_unlock(&irq_uspace_hash_table_lock);
  288.             interrupts_restore(ipl);
  289.             DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
  290.             goto loop;
  291.         }
  292.         key[0] = irq->inr;
  293.         key[1] = irq->devno;
  294.        
  295.        
  296.         ASSERT(irq->notif_cfg.answerbox == box);
  297.        
  298.         /* Unlist from the answerbox. */
  299.         list_remove(&irq->notif_cfg.link);
  300.        
  301.         /* Remove from the hash table. */
  302.         hash_table_remove(&irq_uspace_hash_table, key, 2);
  303.        
  304.         /* Free up the pseudo code and associated structures. */
  305.         code_free(irq->notif_cfg.code);
  306.        
  307.         spinlock_unlock(&irq->lock);
  308.         free(irq);
  309.     }
  310.    
  311.     spinlock_unlock(&box->irq_lock);
  312.     spinlock_unlock(&irq_uspace_hash_table_lock);
  313.     interrupts_restore(ipl);
  314. }
  315.  
  316. /** Add a call to the proper answerbox queue.
  317.  *
  318.  * Assume irq->lock is locked.
  319.  *
  320.  * @param irq       IRQ structure referencing the target answerbox.
  321.  * @param call      IRQ notification call.
  322.  */
  323. static void send_call(irq_t *irq, call_t *call)
  324. {
  325.     spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
  326.     list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
  327.     spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
  328.        
  329.     waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
  330. }
  331.  
  332. /** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
  333.  *
  334.  * @param irq       IRQ structure.
  335.  *
  336.  * @return      IRQ_ACCEPT if the interrupt is accepted by the
  337.  *          pseudocode. IRQ_DECLINE otherwise.
  338.  */
  339. irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
  340. {
  341.     unsigned int i;
  342.     unative_t dstval;
  343.     irq_code_t *code = irq->notif_cfg.code;
  344.     unative_t *scratch = irq->notif_cfg.scratch;
  345.  
  346.    
  347.     if (!irq->notif_cfg.notify)
  348.         return IRQ_DECLINE;
  349.    
  350.     if (!code)
  351.         return IRQ_DECLINE;
  352.    
  353.     for (i = 0; i < code->cmdcount; i++) {
  354.         unsigned int srcarg = code->cmds[i].srcarg;
  355.         unsigned int dstarg = code->cmds[i].dstarg;
  356.        
  357.         if (srcarg >= IPC_CALL_LEN)
  358.             break;
  359.         if (dstarg >= IPC_CALL_LEN)
  360.             break;
  361.    
  362.         switch (code->cmds[i].cmd) {
  363.         case CMD_PIO_READ_8:
  364.             dstval = pio_read_8((ioport8_t *) code->cmds[i].addr);
  365.             if (dstarg)
  366.                 scratch[dstarg] = dstval;
  367.             break;
  368.         case CMD_PIO_READ_16:
  369.             dstval = pio_read_16((ioport16_t *) code->cmds[i].addr);
  370.             if (dstarg)
  371.                 scratch[dstarg] = dstval;
  372.             break;
  373.         case CMD_PIO_READ_32:
  374.             dstval = pio_read_32((ioport32_t *) code->cmds[i].addr);
  375.             if (dstarg)
  376.                 scratch[dstarg] = dstval;
  377.             break;
  378.         case CMD_PIO_WRITE_8:
  379.             pio_write_8((ioport8_t *) code->cmds[i].addr,
  380.                 (uint8_t) code->cmds[i].value);
  381.             break;
  382.         case CMD_PIO_WRITE_16:
  383.             pio_write_16((ioport16_t *) code->cmds[i].addr,
  384.                 (uint16_t) code->cmds[i].value);
  385.             break;
  386.         case CMD_PIO_WRITE_32:
  387.             pio_write_32((ioport32_t *) code->cmds[i].addr,
  388.                 (uint32_t) code->cmds[i].value);
  389.             break;
  390.         case CMD_BTEST:
  391.             if (srcarg && dstarg) {
  392.                 dstval = scratch[srcarg] & code->cmds[i].value;
  393.                 scratch[dstarg] = dstval;
  394.             }
  395.             break;
  396.         case CMD_PREDICATE:
  397.             if (srcarg && !scratch[srcarg]) {
  398.                 i += code->cmds[i].value;
  399.                 continue;
  400.             }
  401.             break;
  402.         case CMD_ACCEPT:
  403.             return IRQ_ACCEPT;
  404.             break;
  405.         case CMD_DECLINE:
  406.         default:
  407.             return IRQ_DECLINE;
  408.         }
  409.     }
  410.    
  411.     return IRQ_DECLINE;
  412. }
  413.  
  414.  
  415. /* IRQ top-half handler.
  416.  *
  417.  * We expect interrupts to be disabled and the irq->lock already held.
  418.  *
  419.  * @param irq       IRQ structure.
  420.  */
  421. void ipc_irq_top_half_handler(irq_t *irq)
  422. {
  423.     ASSERT(irq);
  424.  
  425.     if (irq->notif_cfg.answerbox) {
  426.         call_t *call;
  427.  
  428.         call = ipc_call_alloc(FRAME_ATOMIC);
  429.         if (!call)
  430.             return;
  431.        
  432.         call->flags |= IPC_CALL_NOTIF;
  433.         /* Put a counter to the message */
  434.         call->priv = ++irq->notif_cfg.counter;
  435.  
  436.         /* Set up args */
  437.         IPC_SET_METHOD(call->data, irq->notif_cfg.method);
  438.         IPC_SET_ARG1(call->data, irq->notif_cfg.scratch[1]);
  439.         IPC_SET_ARG2(call->data, irq->notif_cfg.scratch[2]);
  440.         IPC_SET_ARG3(call->data, irq->notif_cfg.scratch[3]);
  441.         IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
  442.         IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
  443.  
  444.         send_call(irq, call);
  445.     }
  446. }
  447.  
  448. /** Send notification message.
  449.  *
  450.  * @param irq       IRQ structure.
  451.  * @param a1        Driver-specific payload argument.
  452.  * @param a2        Driver-specific payload argument.
  453.  * @param a3        Driver-specific payload argument.
  454.  * @param a4        Driver-specific payload argument.
  455.  * @param a5        Driver-specific payload argument.
  456.  */
  457. void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3,
  458.     unative_t a4, unative_t a5)
  459. {
  460.     call_t *call;
  461.  
  462.     spinlock_lock(&irq->lock);
  463.  
  464.     if (irq->notif_cfg.answerbox) {
  465.         call = ipc_call_alloc(FRAME_ATOMIC);
  466.         if (!call) {
  467.             spinlock_unlock(&irq->lock);
  468.             return;
  469.         }
  470.         call->flags |= IPC_CALL_NOTIF;
  471.         /* Put a counter to the message */
  472.         call->priv = ++irq->notif_cfg.counter;
  473.  
  474.         IPC_SET_METHOD(call->data, irq->notif_cfg.method);
  475.         IPC_SET_ARG1(call->data, a1);
  476.         IPC_SET_ARG2(call->data, a2);
  477.         IPC_SET_ARG3(call->data, a3);
  478.         IPC_SET_ARG4(call->data, a4);
  479.         IPC_SET_ARG5(call->data, a5);
  480.        
  481.         send_call(irq, call);
  482.     }
  483.     spinlock_unlock(&irq->lock);
  484. }
  485.  
  486. /** @}
  487.  */
  488.