Index: soft/giet_vm/giet_kernel/irq_handler.c
===================================================================
--- soft/giet_vm/giet_kernel/irq_handler.c	(revision 702)
+++ soft/giet_vm/giet_kernel/irq_handler.c	(revision 709)
@@ -26,4 +26,15 @@
 #include <tty0.h>
 
+////////////////////////////////////////////////////////////////////////////
+//        Extern variables
+////////////////////////////////////////////////////////////////////////////
+
+// allocated in sys_handler.c file
+extern unsigned int _tty_channel_wti[NB_TTY_CHANNELS];
+extern unsigned int _tim_channel_wti[NB_TIM_CHANNELS];
+extern unsigned int _cma_channel_wti[NB_CMA_CHANNELS];
+extern unsigned int _nic_rx_channel_wti[NB_NIC_CHANNELS];
+extern unsigned int _nic_tx_channel_wti[NB_NIC_CHANNELS];
+
 /////////////////////////////////////////////////////////////////////////
 //       Global variables 
@@ -41,4 +52,6 @@
 __attribute__((section(".kdata")))
 unsigned char _wti_alloc_ter[X_SIZE][Y_SIZE][NB_PROCS_MAX];
+
+#define MAX_RETRY   10000
 
 /////////////////////////////////////////////////////////////////////////
@@ -134,5 +147,5 @@
 }  // end _ext_irq_init()
 
-////////////////////////////////////////////
+/////////////////////////////////////////////
 void _ext_irq_alloc( unsigned int   isr_type,
                      unsigned int   isr_channel,
@@ -143,5 +156,7 @@
     unsigned int wti_addr;      // WTI mailbox physical address (32 lsb bits)
 
-    // check arguments
+    unsigned int count = MAX_RETRY;
+
+    // check input arguments
     if ( isr_type >= GIET_ISR_TYPE_MAX )
     {
@@ -163,6 +178,17 @@
 
     // allocate a WTI mailbox to proc[x,y,p] (blocking until success)
+    
     while ( 1 )
     {
+        if ( count == 0 )
+        {
+            unsigned int trdid = _get_context_slot( CTX_TRDID_ID );
+            unsigned int vsid  = _get_context_slot( CTX_VSID_ID );
+            _printf("\n[GIET WARNING] thread %x in vspace %d "
+                    "running on P[%d,%d,%d] still waiting in _ext_irq_alloc()\n",
+                    trdid, vsid, x, y, p );
+            count = MAX_RETRY;
+        }
+
         if ( _wti_alloc_one[x][y][p] == 0 )
         {
@@ -183,5 +209,7 @@
             break;
         }
+        count--;
     }    
+
     *wti_index = wti_id;
 
@@ -196,4 +224,5 @@
 
 #if GIET_DEBUG_IRQS
+if ( _get_proctime() > GIET_DEBUG_IRQS )
 _printf("\n[DEBUG IRQS] _ext_irq_alloc() for P[%d,%d,%d] at cycle %d\n"
         "  wti_id = %d / isr_type = %s / channel = %d / pic_input = %d\n",
@@ -204,54 +233,90 @@
 }  // end ext_irq_alloc()
 
-////////////////////////////////////////////
+////////////////////////////////////////////////
 void _ext_irq_release( unsigned int isr_type,
-                       unsigned int isr_channel )
-{
-    unsigned int wti_id;        // allocated WTI mailbox index in XCU
-    unsigned int irq_id;        // external IRQ index in PIC (input)
-
-    // get processor coordinates [x,y,p]
-    unsigned int gpid           = _get_procid();
-    unsigned int cluster_xy     = gpid >> P_WIDTH;
-    unsigned int x              = cluster_xy >> Y_WIDTH;
-    unsigned int y              = cluster_xy & ((1<<Y_WIDTH)-1);
-    unsigned int p              = gpid & ((1<<P_WIDTH)-1);
-
-    // check arguments
+                       unsigned int channel )
+{
+    // check input arguments
     if ( isr_type >= GIET_ISR_TYPE_MAX )
     {
-        _printf("\n[GIET ERROR] in _ext_irq_release() illegal ISR type\n");
-        _exit();
-    }
-    if ( isr_channel >= GIET_ISR_CHANNEL_MAX )
-    {
-        _printf("\n[GIET ERROR] in _ext_irq_release() : illegal ISR channel\n");
-        _exit();
-    }
-
-    // find WTI index
-    static_scheduler_t*  psched = (static_scheduler_t*)_get_sched();
-    for ( wti_id = 0 ; wti_id < 32 ; wti_id++ )
-    {
-        if ( psched->wti_vector[wti_id] == (isr_channel<<16 | isr_type) )
-            break;
-    }
-    if ( wti_id == 32 )
-    {
-        _printf("\n[GIET ERROR] in _ext_irq_release() : isr not found\n");
-        return;
-    }
-
+        _printf("\n[GIET ERROR] in _ext_irq_release() : "
+                "illegal ISR type %d\n", isr_type );
+        _exit();
+    }
+    if ( channel >= GIET_ISR_CHANNEL_MAX )
+    {
+        _printf("\n[GIET ERROR] in _ext_irq_release() : "
+                "illegal ISR channel %d\n", channel );
+        _exit();
+    }
+
+    // analyse ISR type to get WTI index (wti), and coordinates 
+    // of processor (x,y,p) that has been allocated the external IRQ
+    unsigned int wti    = 0;
+    unsigned int x      = 0;
+    unsigned int y      = 0;
+    unsigned int p      = 0;
+
+    if      ( (isr_type == ISR_TTY_RX) || (isr_type == ISR_TTY_TX) )
+    {
+        x       = (_tty_channel_wti[channel]>>24) & 0xFF;
+        y       = (_tty_channel_wti[channel]>>16) & 0xFF;
+        p       = (_tty_channel_wti[channel]>> 8) & 0xFF;
+        wti     = (_tty_channel_wti[channel]    ) & 0xFF;
+    }
+#if NB_TIM_CHANNELS
+    else if ( isr_type == ISR_TIMER )
+    {
+        x       = (_tim_channel_wti[channel]>>24) & 0xFF;
+        y       = (_tim_channel_wti[channel]>>16) & 0xFF;
+        p       = (_tim_channel_wti[channel]>> 8) & 0xFF;
+        wti     = (_tim_channel_wti[channel]    ) & 0xFF;
+    }
+#endif
+#if NB_CMA_CHANNELS
+    else if ( isr_type == ISR_CMA )
+    {
+        x       = (_cma_channel_wti[channel]>>24) & 0xFF;
+        y       = (_cma_channel_wti[channel]>>16) & 0xFF;
+        p       = (_cma_channel_wti[channel]>> 8) & 0xFF;
+        wti     = (_cma_channel_wti[channel]    ) & 0xFF;
+    }
+#endif
+#if NB_NIC_CHANNELS
+    else if ( isr_type == ISR_NIC_RX )
+    {
+        x       = (_nic_rx_channel_wti[channel]>>24) & 0xFF;
+        y       = (_nic_rx_channel_wti[channel]>>16) & 0xFF;
+        p       = (_nic_rx_channel_wti[channel]>> 8) & 0xFF;
+        wti     = (_nic_rx_channel_wti[channel]    ) & 0xFF;
+    }
+    else if ( isr_type == ISR_NIC_TX )
+    {
+        x       = (_nic_tx_channel_wti[channel]>>24) & 0xFF;
+        y       = (_nic_tx_channel_wti[channel]>>16) & 0xFF;
+        p       = (_nic_tx_channel_wti[channel]>> 8) & 0xFF;
+        wti     = (_nic_tx_channel_wti[channel]    ) & 0xFF;
+    }
+#endif
+    else  
+    {
+        _printf("\n[GIET ERROR] in _ext_irq_release() : "
+                "ISR type %s not supported / thread = %x\n", 
+                _isr_type_str[isr_type] , _get_thread_trdid() );
+        _exit();
+    }
+    
     // desactivates dynamically allocated PIC entry
-    irq_id = _ext_irq_index[isr_type][isr_channel];
+    unsigned int irq_id = _ext_irq_index[isr_type][channel];
     _pic_set_register( irq_id , IOPIC_MASK , 0 );
 
     // releases dynamically allocated WTI mailbox
-    if      ( wti_id == p +   NB_PROCS_MAX ) _wti_alloc_one[x][y][p] = 0;
-    else if ( wti_id == p + 2*NB_PROCS_MAX ) _wti_alloc_two[x][y][p] = 0;
-    else if ( wti_id == p + 3*NB_PROCS_MAX ) _wti_alloc_ter[x][y][p] = 0;
+    if      ( wti == p +   NB_PROCS_MAX ) _wti_alloc_one[x][y][p] = 0;
+    else if ( wti == p + 2*NB_PROCS_MAX ) _wti_alloc_two[x][y][p] = 0;
+    else if ( wti == p + 3*NB_PROCS_MAX ) _wti_alloc_ter[x][y][p] = 0;
     else
     {
-        _printf("\n[GIET ERROR] in _ext_irq_release() : illegal WTI index\n");
+        _printf("\n[GIET ERROR] in _ext_irq_release() : "
+                "WTI = %d / X = %d / Y = %d / P = %d\n", wti , x, y , p );
         _exit();
     }
@@ -294,4 +359,5 @@
 
 #if GIET_DEBUG_IRQS    // we don't take the TTY lock to avoid deadlocks
+if ( _get_proctime() > GIET_DEBUG_IRQS )
 _nolock_printf("\n[DEBUG IRQS] _irq_demux() Processor[%d,%d,%d] enters at cycle %d\n"
                " irq_type = %s / irq_id = %d / isr_type = %s / channel = %d\n",
@@ -360,7 +426,7 @@
 
     unsigned int value;     // WTI mailbox value
-    unsigned int save_sr;   // save SR value in pre-empted task stack
-
-    unsigned int ltid       = _get_current_task_id();
+    unsigned int save_sr;   // save SR value in pre-empted thread stack
+
+    unsigned int ltid       = _get_thread_ltid();
 
     if ( irq_type != IRQ_TYPE_WTI )
@@ -375,6 +441,7 @@
     _xcu_get_wti_value( cluster_xy, irq_id, &value );
 
-#if GIET_DEBUG_SWITCH
-_printf("\n[DEBUG SWITCH] P[%d,%d,%d] enters _isr_wakup() at cycle %d\n"
+#if GIET_DEBUG_IRQS
+if ( _get_proctime() > GIET_DEBUG_IRQS )
+_printf("\n[DEBUG IRQS] P[%d,%d,%d] enters _isr_wakup() at cycle %d\n"
         "  WTI index = %d / current ltid = %d / mailbox value = %x\n",
         x , y , p , _get_proctime() , irq_id , ltid , value );
@@ -382,5 +449,5 @@
 
     // enter critical section and swich context (if required)
-    if ( (ltid == IDLE_TASK_INDEX) || (value != 0) )
+    if ( (ltid == IDLE_THREAD_INDEX) || (value != 0) )
     {
         _it_disable( &save_sr );
@@ -402,5 +469,5 @@
     unsigned int p          = gpid & ((1<<P_WIDTH)-1);
 
-    unsigned int save_sr;   // save SR value in pre-empted task stack
+    unsigned int save_sr;   // save SR value in pre-empted thread stack
 
     if ( irq_type != IRQ_TYPE_PTI )
@@ -415,8 +482,8 @@
     _xcu_timer_reset_irq( cluster_xy, irq_id );
 
-#if (GIET_DEBUG_SWITCH & 0x1)
-unsigned int ltid  = _get_current_task_id();
-if ( _get_proctime() > GIET_DEBUG_SWITCH )
-_printf("\n[DEBUG SWITCH] P[%d,%d,%d] enters _isr_tick() at cycle %d\n"
+#if GIET_DEBUG_IRQS
+unsigned int ltid  = _get_thread_ltid();
+if ( _get_proctime() > GIET_DEBUG_IRQS )
+_printf("\n[DEBUG IRQS] P[%d,%d,%d] enters _isr_tick() at cycle %d\n"
         "  WTI index = %d / current ltid = %d\n",
         x , y , p , _get_proctime() , irq_id , ltid );
