[158] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : irq_handler.c |
---|
| 3 | // Date : 01/04/2012 |
---|
[189] | 4 | // Author : alain greiner |
---|
[158] | 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 7 | // The irq_handler.c and irq_handler.h files are part of the GIET-VM nano-kernel. |
---|
| 8 | // They contain the code of the _irq_demux() function that access the XICU or |
---|
| 9 | // ICU component (Interupt Controler Unit), and the various ISRs (Interrupt |
---|
| 10 | // Service Routine) associated to the peripherals. |
---|
[158] | 11 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 12 | |
---|
| 13 | #include <giet_config.h> |
---|
| 14 | #include <irq_handler.h> |
---|
| 15 | #include <sys_handler.h> |
---|
| 16 | #include <drivers.h> |
---|
| 17 | #include <common.h> |
---|
| 18 | #include <ctx_handler.h> |
---|
| 19 | #include <hwr_mapping.h> |
---|
| 20 | |
---|
| 21 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 22 | // _irq_demux() |
---|
| 23 | // This function uses the ICU or XICU component (Interrupt Controler Unit) |
---|
| 24 | // to get the interrupt vector entry. There is one ICU or XICU component per |
---|
| 25 | // cluster, and this component can support up to NB_PROCS_MAX output IRQs. |
---|
| 26 | // It returns the highest priority active interrupt index (smaller |
---|
| 27 | // indexes have the highest priority). |
---|
| 28 | // Any value larger than 31 means "no active interrupt", and no ISR is executed. |
---|
[158] | 29 | // |
---|
[189] | 30 | // There is one interrupt vector per processor (stored in the scheduler associated |
---|
| 31 | // to the processor. Each interrupt vector entry contains two 16 bits fields: |
---|
| 32 | // - isr_id : defines the type of ISR to be executed. |
---|
| 33 | // - channel_id : defines the specific channel for multi-channels peripherals. |
---|
[158] | 34 | // |
---|
[189] | 35 | // If the peripheral is replicated in clusters (TIMER or DMA), the channel_id is |
---|
| 36 | // a global index : channel_id = cluster_id * NB_CHANNELS_MAX + loc_id |
---|
[158] | 37 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 38 | void _irq_demux() |
---|
[158] | 39 | { |
---|
[189] | 40 | unsigned int pid = _procid(); |
---|
| 41 | unsigned int irq_id; |
---|
[158] | 42 | |
---|
[189] | 43 | // get the highest priority active IRQ index |
---|
[203] | 44 | if ( _icu_get_index( pid / NB_PROCS_MAX, |
---|
| 45 | pid % NB_PROCS_MAX, |
---|
| 46 | &irq_id ) ) |
---|
[158] | 47 | { |
---|
[203] | 48 | _get_lock(&_tty_put_lock); |
---|
| 49 | _puts("\n[GIET ERROR] Strange... Wrong _icu_read in _irq_demux()\n"); |
---|
| 50 | _release_lock(&_tty_put_lock); |
---|
[189] | 51 | } |
---|
[158] | 52 | |
---|
[189] | 53 | if ( irq_id < 32 ) // do nothing if no interrupt active |
---|
[165] | 54 | { |
---|
[189] | 55 | unsigned int entry = _get_interrupt_vector_entry(irq_id); |
---|
| 56 | unsigned int isr_id = entry & 0x000000FF; |
---|
| 57 | unsigned int channel_id = (entry>>16) & 0x0000FFFF; |
---|
| 58 | if ( isr_id == ISR_SWITCH ) _isr_switch(); |
---|
| 59 | else if ( isr_id == ISR_IOC ) _isr_ioc(); |
---|
| 60 | else if ( isr_id == ISR_DMA ) _isr_dma( channel_id ); |
---|
| 61 | else if ( isr_id == ISR_TTY ) _isr_tty( channel_id ); |
---|
| 62 | else if ( isr_id == ISR_TIMER ) _isr_timer( channel_id ); |
---|
| 63 | else _isr_default(); |
---|
[165] | 64 | } |
---|
[158] | 65 | } |
---|
| 66 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 67 | // _isr_default() |
---|
| 68 | // The default ISR is called when no specific ISR has been installed in the |
---|
[203] | 69 | // interrupt vector. It simply displays an error message on kernel TTY[0]. |
---|
[158] | 70 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 71 | void _isr_default() |
---|
| 72 | { |
---|
[203] | 73 | _get_lock(&_tty_put_lock); |
---|
| 74 | _puts("\n[GIET ERROR] Strange... Default ISR activated for processor "); |
---|
| 75 | _putd( _procid() ); |
---|
| 76 | _puts("\n"); |
---|
| 77 | _release_lock(&_tty_put_lock); |
---|
[158] | 78 | } |
---|
[165] | 79 | |
---|
[158] | 80 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 81 | // _isr_dma() |
---|
[189] | 82 | // This ISR handles all IRQs generated by the multi-channels DMA controlers. |
---|
| 83 | // The multi_dma components can be distributed in the clusters. |
---|
[207] | 84 | // The channel_id argument is the local DMA channel index. |
---|
| 85 | // dma_global_id = cluster_id*NB_DMAS_MAX + channel_id |
---|
| 86 | // - The ISR saves the transfert status in _dma_status[dma_global_id]. |
---|
[189] | 87 | // - It acknowledges the interrupt to reinitialize the DMA controler. |
---|
[207] | 88 | // - it resets the synchronisation variable _dma_busy[dma_global_id]. |
---|
[158] | 89 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 90 | void _isr_dma( unsigned int channel_id ) |
---|
[158] | 91 | { |
---|
[213] | 92 | #if NB_DMAS_MAX > 0 |
---|
[207] | 93 | // compute cluster_id |
---|
| 94 | unsigned int cluster_id = _procid()/NB_PROCS_MAX; |
---|
[158] | 95 | |
---|
[207] | 96 | // compute dma_global_id |
---|
| 97 | unsigned int dma_global_id = cluster_id*NB_DMAS_MAX + channel_id; |
---|
| 98 | |
---|
[169] | 99 | // save DMA channel status |
---|
[207] | 100 | if ( _dma_get_status(cluster_id, |
---|
| 101 | channel_id, |
---|
| 102 | (unsigned int*)&_dma_status[dma_global_id] ) ) |
---|
[204] | 103 | { |
---|
| 104 | _get_lock(&_tty_put_lock); |
---|
| 105 | _puts("[GIET ERROR] illegal DMA channel detected by _isr_dma\n"); |
---|
| 106 | _release_lock(&_tty_put_lock); |
---|
| 107 | return; |
---|
| 108 | } |
---|
[169] | 109 | |
---|
[204] | 110 | // reset DMA channel irq |
---|
[207] | 111 | if ( _dma_reset_irq( cluster_id, |
---|
| 112 | channel_id) ) |
---|
[204] | 113 | { |
---|
| 114 | _get_lock(&_tty_put_lock); |
---|
| 115 | _puts("[GIET ERROR] illegal DMA channel detected by _isr_dma\n"); |
---|
| 116 | _release_lock(&_tty_put_lock); |
---|
| 117 | return; |
---|
| 118 | } |
---|
[165] | 119 | |
---|
[169] | 120 | // release DMA channel |
---|
[207] | 121 | _dma_done[dma_global_id] = 1; |
---|
[213] | 122 | #else |
---|
| 123 | _puts("[GIET ERROR] NB_DMAS_MAX is set to zero\n"); |
---|
| 124 | |
---|
| 125 | #endif |
---|
[158] | 126 | } |
---|
[165] | 127 | |
---|
[158] | 128 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 129 | // _isr_ioc() |
---|
[189] | 130 | // There is only one IOC controler shared by all tasks. |
---|
| 131 | // - The ISR save the status and acknowledge the IRQ. |
---|
| 132 | // - It sets the _ioc_done variable to signal completion. |
---|
[158] | 133 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 134 | void _isr_ioc() |
---|
| 135 | { |
---|
[204] | 136 | // save status & reset IRQ |
---|
[207] | 137 | if ( _ioc_get_status( (unsigned int*)&_ioc_status ) ) |
---|
[204] | 138 | { |
---|
| 139 | _get_lock(&_tty_put_lock); |
---|
| 140 | _puts("[GIET ERROR] bad access to IOC status detected by _isr_ioc\n"); |
---|
| 141 | _release_lock(&_tty_put_lock); |
---|
| 142 | return; |
---|
| 143 | } |
---|
[158] | 144 | |
---|
[204] | 145 | // signals completion |
---|
| 146 | _ioc_done = 1; |
---|
[158] | 147 | } |
---|
[165] | 148 | |
---|
[158] | 149 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 150 | // _isr_timer() |
---|
[203] | 151 | // This ISR handles the IRQs generated by the "user" timers (the IRQs generated |
---|
| 152 | // by the "system" timers should be handled by the _isr_switch(). |
---|
[189] | 153 | // These timers are distributed in all clusters, and can be implemented |
---|
| 154 | // in a vci_multi_timer component, or in a vci_xicu component. |
---|
[207] | 155 | // The timer_id argument is the user timer local index. |
---|
| 156 | // timer_globa_id = cluster_id*(NB_TIMERS_MAX) + timer_id |
---|
[189] | 157 | // The ISR acknowledges the IRQ and registers the event in the proper entry |
---|
[203] | 158 | // of the _timer_event[] array, and a log message is displayed on kernel terminal. |
---|
[158] | 159 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[203] | 160 | void _isr_timer(unsigned int timer_id) |
---|
[158] | 161 | { |
---|
[207] | 162 | // compute cluster_id |
---|
| 163 | unsigned int cluster_id = _procid()/NB_PROCS_MAX; |
---|
[158] | 164 | |
---|
[203] | 165 | // aknowledge IRQ |
---|
[207] | 166 | if ( _timer_reset_irq( cluster_id, |
---|
| 167 | NB_PROCS_MAX + timer_id ) ) |
---|
[204] | 168 | { |
---|
| 169 | _get_lock(&_tty_put_lock); |
---|
| 170 | _puts("[GIET ERROR] illegal timer index detected by _isr_timer\n"); |
---|
| 171 | _release_lock(&_tty_put_lock); |
---|
| 172 | return; |
---|
| 173 | } |
---|
[189] | 174 | |
---|
| 175 | #if NB_TIMERS_MAX |
---|
| 176 | // register the event |
---|
[207] | 177 | unsigned int timer_global_id = cluster_id*NB_TIMERS_MAX + timer_id; |
---|
| 178 | _user_timer_event[timer_global_id] = 1; |
---|
[189] | 179 | #endif |
---|
| 180 | |
---|
| 181 | // display a message on TTY 0 |
---|
[203] | 182 | _get_lock(&_tty_put_lock); |
---|
[207] | 183 | _puts("\n[GIET] User Timer IRQ at cycle "); |
---|
[203] | 184 | _putd( _proctime() ); |
---|
[207] | 185 | _puts("\n - cluster_id = "); |
---|
| 186 | _putd( cluster_id ); |
---|
| 187 | _puts("\n - timer_id = "); |
---|
| 188 | _putd( timer_id ); |
---|
[189] | 189 | _puts("\n"); |
---|
[203] | 190 | _release_lock(&_tty_put_lock); |
---|
[158] | 191 | } |
---|
| 192 | |
---|
| 193 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 194 | // _isr_tty() |
---|
| 195 | // This ISR handles the IRQs generated by the multi_tty controler, |
---|
| 196 | // signaling that a character is available. |
---|
[204] | 197 | // There is one single multi_tty component controling all TTYs, |
---|
[207] | 198 | // and the tty_id argument is the global TTY index. |
---|
[189] | 199 | // There is one communication buffer _tty_buf[tty_id] per terminal. |
---|
| 200 | // The sychronisation variable _tty_full[tty_id], is set by the ISR, |
---|
[158] | 201 | // and reset by the OS. |
---|
| 202 | // A character is lost if the buffer is full when the ISR is executed. |
---|
| 203 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 204 | void _isr_tty(unsigned int tty_id) |
---|
[158] | 205 | { |
---|
[189] | 206 | // save character and reset IRQ |
---|
[207] | 207 | if ( _tty_get_char( tty_id, |
---|
| 208 | (unsigned char*)&_tty_get_buf[tty_id] ) ) |
---|
[204] | 209 | { |
---|
| 210 | _get_lock(&_tty_put_lock); |
---|
| 211 | _puts("[GIET ERROR] illegal tty index detected by _isr_tty\n"); |
---|
| 212 | _release_lock(&_tty_put_lock); |
---|
| 213 | return; |
---|
| 214 | } |
---|
[158] | 215 | |
---|
[189] | 216 | // signals character available |
---|
[158] | 217 | _tty_get_full[tty_id] = 1; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 221 | // _isr_switch |
---|
[189] | 222 | // This ISR is in charge of context switch, and handle the IRQs generated by |
---|
| 223 | // the "system" timers. |
---|
| 224 | // The IRQs can be generated by the MULTI_TIMER component or by the XICU component, |
---|
| 225 | // that are distributed in all clusters. |
---|
| 226 | // The ISR acknowledges the IRQ and calls the _ctx_switch() function. |
---|
[158] | 227 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 228 | void _isr_switch() |
---|
| 229 | { |
---|
[189] | 230 | // get cluster index and proc local index |
---|
| 231 | unsigned int pid = _procid(); |
---|
[203] | 232 | unsigned int local_id = pid % NB_PROCS_MAX; |
---|
[189] | 233 | unsigned int cluster_id = pid / NB_PROCS_MAX; |
---|
[158] | 234 | |
---|
[203] | 235 | // acknowledge IRQ |
---|
[204] | 236 | if ( _timer_reset_irq( cluster_id, local_id ) ) |
---|
| 237 | { |
---|
| 238 | _get_lock(&_tty_put_lock); |
---|
| 239 | _puts("[GIET ERROR] illegal proc index detected by _isr_switch\n"); |
---|
| 240 | _release_lock(&_tty_put_lock); |
---|
| 241 | return; |
---|
| 242 | } |
---|
[158] | 243 | |
---|
[189] | 244 | // performs the context switch |
---|
[158] | 245 | _ctx_switch(); |
---|
| 246 | } |
---|
| 247 | |
---|