Subversion Repositories HelenOS

Rev

Rev 2307 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup genericddi
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * @brief   IRQ dispatcher.
  35.  *
  36.  * This file provides means of connecting IRQs with particular
  37.  * devices and logic for dispatching interrupts to IRQ handlers
  38.  * defined by those devices.
  39.  *
  40.  * This code is designed to support:
  41.  * - multiple devices sharing single IRQ
  42.  * - multiple IRQs per signle device
  43.  *
  44.  *
  45.  * Note about architectures.
  46.  *
  47.  * Some architectures has the term IRQ well defined. Examples
  48.  * of such architectures include amd64, ia32 and mips32. Some
  49.  * other architectures, such as sparc64, don't use the term
  50.  * at all. In those cases, we boldly step forward and define what
  51.  * an IRQ is.
  52.  *
  53.  * The implementation is generic enough and still allows the
  54.  * architectures to use the hardware layout effectively.
  55.  * For instance, on amd64 and ia32, where there is only 16
  56.  * IRQs, the irq_hash_table can be optimized to a one-dimensional
  57.  * array. Next, when it is known that the IRQ numbers (aka INR's)
  58.  * are unique, the claim functions can always return IRQ_ACCEPT.
  59.  *
  60.  *
  61.  * Note about the irq_hash_table.
  62.  *
  63.  * The hash table is configured to use two keys: inr and devno.
  64.  * However, the hash index is computed only from inr. Moreover,
  65.  * if devno is -1, the match is based on the return value of
  66.  * the claim() function instead of on devno.
  67.  */
  68.  
  69. #include <ddi/irq.h>
  70. #include <adt/hash_table.h>
  71. #include <arch/types.h>
  72. #include <synch/spinlock.h>
  73. #include <arch.h>
  74. #include <synch/rcu.h>
  75.  
  76.  
  77. #define KEY_INR     0
  78. #define KEY_DEVNO   1
  79.  
  80. /**
  81.  * Spinlock protecting the hash table.
  82.  * This lock must be taken only when interrupts are disabled.
  83.  */
  84. SPINLOCK_INITIALIZE(irq_hash_table_lock);
  85. static hash_table_t irq_hash_table;
  86.  
  87. /**
  88.  * Hash table operations for cases when we know that
  89.  * there will be collisions between different keys.
  90.  */
  91. static index_t irq_ht_hash(unative_t *key);
  92. static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
  93.  
  94. static hash_table_operations_t irq_ht_ops = {
  95.     .hash = irq_ht_hash,
  96.     .compare = irq_ht_compare,
  97.     .remove_callback = NULL     /* not used */
  98. };
  99.  
  100. /**
  101.  * Hash table operations for cases when we know that
  102.  * there will be no collisions between different keys.
  103.  * However, there might be still collisions among
  104.  * elements with single key (sharing of one IRQ).
  105.  */
  106. static index_t irq_lin_hash(unative_t *key);
  107. static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
  108.  
  109. static hash_table_operations_t irq_lin_ops = {
  110.     .hash = irq_lin_hash,
  111.     .compare = irq_lin_compare,
  112.     .remove_callback = NULL     /* not used */
  113. };
  114.  
  115. /** Initialize IRQ subsystem.
  116.  *
  117.  * @param inrs Numbers of unique IRQ numbers or INRs.
  118.  * @param chains Number of chains in the hash table.
  119.  */
  120. void irq_init(count_t inrs, count_t chains)
  121. {
  122.     /*
  123.      * Be smart about the choice of the hash table operations.
  124.      * In cases in which inrs equals the requested number of
  125.      * chains (i.e. where there is no collision between
  126.      * different keys), we can use optimized set of operations.
  127.      */
  128.     if (inrs == chains)
  129.         hash_table_create(&irq_hash_table, chains, 2, &irq_lin_ops);
  130.     else
  131.         hash_table_create(&irq_hash_table, chains, 2, &irq_ht_ops);
  132. }
  133.  
  134. /** Initialize one IRQ structure.
  135.  *
  136.  * @param irq Pointer to the IRQ structure to be initialized.
  137.  *
  138.  */
  139. void irq_initialize(irq_t *irq)
  140. {
  141.     link_initialize(&irq->link);
  142.     spinlock_initialize(&irq->lock, "irq.lock");
  143.     irq->preack = false;
  144.     irq->inr = -1;
  145.     irq->devno = -1;
  146.     irq->trigger = (irq_trigger_t) 0;
  147.     irq->claim = NULL;
  148.     irq->handler = NULL;
  149.     irq->arg = NULL;
  150.     irq->notif_cfg = malloc(sizeof(ipc_notif_cfg_t), 0);
  151.     irq->notif_cfg->notify = false;
  152.     irq->notif_cfg->answerbox = NULL;
  153.     irq->notif_cfg->code = NULL;
  154.     irq->notif_cfg->method = 0;
  155.     irq->notif_cfg->counter = 0;
  156.     link_initialize(&irq->notif_cfg->link);
  157. }
  158.  
  159. /** Register IRQ for device.
  160.  *
  161.  * The irq structure must be filled with information
  162.  * about the interrupt source and with the claim()
  163.  * function pointer and irq_handler() function pointer.
  164.  *
  165.  * @param irq IRQ structure belonging to a device.
  166.  */
  167. void irq_register(irq_t *irq)
  168. {
  169.     ipl_t ipl;
  170.     unative_t key[] = {
  171.         (unative_t) irq->inr,
  172.         (unative_t) irq->devno
  173.     };
  174.    
  175.     ipl = interrupts_disable();
  176.     spinlock_lock(&irq_hash_table_lock);
  177.     hash_table_insert(&irq_hash_table, key, &irq->link);
  178.     spinlock_unlock(&irq_hash_table_lock);
  179.     interrupts_restore(ipl);
  180. }
  181.  
  182. /** Dispatch the IRQ.
  183.  *
  184.  * We assume this function is only called from interrupt
  185.  * context (i.e. that interrupts are disabled prior to
  186.  * this call).
  187.  *
  188.  * This function attempts to lookup a fitting IRQ
  189.  * structure. In case of success, return with interrupts
  190.  * disabled and holding the respective structure.
  191.  *
  192.  * @param inr Interrupt number (aka inr or irq).
  193.  *
  194.  * @return IRQ structure of the respective device or NULL.
  195.  */
  196. irq_t *irq_dispatch_and_lock(inr_t inr)
  197. {
  198.     link_t *lnk;
  199.     unative_t key[] = {
  200.         (unative_t) inr,
  201.         (unative_t) -1      /* search will use claim() instead of devno */
  202.     };
  203.    
  204.     spinlock_lock(&irq_hash_table_lock);
  205.  
  206.     lnk = hash_table_find(&irq_hash_table, key);
  207.     if (lnk) {
  208.         irq_t *irq;
  209.        
  210.         irq = hash_table_get_instance(lnk, irq_t, link);
  211.  
  212.         spinlock_unlock(&irq_hash_table_lock);
  213.         return irq;
  214.     }
  215.    
  216.     spinlock_unlock(&irq_hash_table_lock);
  217.  
  218.     return NULL;   
  219. }
  220.  
  221. /** Find the IRQ structure corresponding to inr and devno.
  222.  *
  223.  * This functions attempts to lookup the IRQ structure
  224.  * corresponding to its arguments. On success, this
  225.  * function returns with interrups disabled, holding
  226.  * the lock of the respective IRQ structure.
  227.  *
  228.  * This function assumes interrupts are already disabled.
  229.  *
  230.  * @param inr INR being looked up.
  231.  * @param devno Devno being looked up.
  232.  *
  233.  * @return Locked IRQ structure on success or NULL on failure.
  234.  */
  235. irq_t *irq_find_and_lock(inr_t inr, devno_t devno)
  236. {
  237.     link_t *lnk;
  238.     unative_t keys[] = {
  239.         (unative_t) inr,
  240.         (unative_t) devno
  241.     };
  242.    
  243.     spinlock_lock(&irq_hash_table_lock);
  244.  
  245.     lnk = hash_table_find(&irq_hash_table, keys);
  246.     if (lnk) {
  247.         irq_t *irq;
  248.        
  249.         irq = hash_table_get_instance(lnk, irq_t, link);
  250.  
  251.         spinlock_unlock(&irq_hash_table_lock);
  252.         return irq;
  253.     }
  254.    
  255.     spinlock_unlock(&irq_hash_table_lock);
  256.  
  257.     return NULL;   
  258. }
  259.  
  260. /** Compute hash index for the key.
  261.  *
  262.  * This function computes hash index into
  263.  * the IRQ hash table for which there
  264.  * can be collisions between different
  265.  * INRs.
  266.  *
  267.  * The devno is not used to compute the hash.
  268.  *
  269.  * @param key The first of the keys is inr and the second is devno or -1.
  270.  *
  271.  * @return Index into the hash table.
  272.  */
  273. index_t irq_ht_hash(unative_t key[])
  274. {
  275.     inr_t inr = (inr_t) key[KEY_INR];
  276.     return inr % irq_hash_table.entries;
  277. }
  278.  
  279. /** Compare hash table element with a key.
  280.  *
  281.  * There are two things to note about this function.
  282.  * First, it is used for the more complex architecture setup
  283.  * in which there are way too many interrupt numbers (i.e. inr's)
  284.  * to arrange the hash table so that collisions occur only
  285.  * among same inrs of different devnos. So the explicit check
  286.  * for inr match must be done.
  287.  * Second, if devno is -1, the second key (i.e. devno) is not
  288.  * used for the match and the result of the claim() function
  289.  * is used instead.
  290.  *
  291.  * This function assumes interrupts are already disabled.
  292.  *
  293.  * @param key Keys (i.e. inr and devno).
  294.  * @param keys This is 2.
  295.  * @param item The item to compare the key with.
  296.  *
  297.  * @return True on match or false otherwise.
  298.  */
  299. bool irq_ht_compare(unative_t key[], count_t keys, link_t *item)
  300. {
  301.     irq_t *irq = hash_table_get_instance(item, irq_t, link);
  302.     inr_t inr = (inr_t) key[KEY_INR];
  303.     devno_t devno = (devno_t) key[KEY_DEVNO];
  304.  
  305.     bool rv;
  306.    
  307.     spinlock_lock(&irq->lock);
  308.     if (devno == -1) {
  309.         /* Invoked by irq_dispatch_and_lock(). */
  310.         rv = ((irq->inr == inr) && (irq->claim() == IRQ_ACCEPT));
  311.     } else {
  312.         /* Invoked by irq_find_and_lock(). */
  313.         rv = ((irq->inr == inr) && (irq->devno == devno));
  314.     }
  315.    
  316.     /* unlock only on non-match */
  317.     if (!rv)
  318.         spinlock_unlock(&irq->lock);
  319.  
  320.     return rv;
  321. }
  322.  
  323. /** Compute hash index for the key.
  324.  *
  325.  * This function computes hash index into
  326.  * the IRQ hash table for which there
  327.  * are no collisions between different
  328.  * INRs.
  329.  *
  330.  * @param key The first of the keys is inr and the second is devno or -1.
  331.  *
  332.  * @return Index into the hash table.
  333.  */
  334. index_t irq_lin_hash(unative_t key[])
  335. {
  336.     inr_t inr = (inr_t) key[KEY_INR];
  337.     return inr;
  338. }
  339.  
  340. /** Compare hash table element with a key.
  341.  *
  342.  * There are two things to note about this function.
  343.  * First, it is used for the less complex architecture setup
  344.  * in which there are not too many interrupt numbers (i.e. inr's)
  345.  * to arrange the hash table so that collisions occur only
  346.  * among same inrs of different devnos. So the explicit check
  347.  * for inr match is not done.
  348.  * Second, if devno is -1, the second key (i.e. devno) is not
  349.  * used for the match and the result of the claim() function
  350.  * is used instead.
  351.  *
  352.  * This function assumes interrupts are already disabled.
  353.  *
  354.  * @param key Keys (i.e. inr and devno).
  355.  * @param keys This is 2.
  356.  * @param item The item to compare the key with.
  357.  *
  358.  * @return True on match or false otherwise.
  359.  */
  360. bool irq_lin_compare(unative_t key[], count_t keys, link_t *item)
  361. {
  362.     irq_t *irq = list_get_instance(item, irq_t, link);
  363.     devno_t devno = (devno_t) key[KEY_DEVNO];
  364.     bool rv;
  365.    
  366.     spinlock_lock(&irq->lock);
  367.     if (devno == -1) {
  368.         /* Invoked by irq_dispatch_and_lock() */
  369.         rv = (irq->claim() == IRQ_ACCEPT);
  370.     } else {
  371.         /* Invoked by irq_find_and_lock() */
  372.         rv = (irq->devno == devno);
  373.     }
  374.    
  375.     /* unlock only on non-match */
  376.     if (!rv)
  377.         spinlock_unlock(&irq->lock);
  378.    
  379.     return rv;
  380. }
  381.  
  382. /** @}
  383.  */
  384.