Index: trunk/kernel/devices/dev_mmc.c
===================================================================
--- trunk/kernel/devices/dev_mmc.c	(revision 690)
+++ trunk/kernel/devices/dev_mmc.c	(revision 1)
@@ -2,5 +2,5 @@
  * dev_mmc.c - MMC (Memory Cache Controler) generic device API implementation.
  * 
- * Author  Alain Greiner    (2016,2017,2018,2019,2020)
+ * Author  Alain Greiner    (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -22,11 +22,9 @@
  */
 
-#include <hal_kernel_types.h>
+#include <hal_types.h>
 #include <hal_special.h>
-#include <hal_drivers.h>
+#include <soclib_mmc.h>
 #include <printk.h>
-#include <chdev.h>
 #include <thread.h>
-#include <remote_busylock.h>
 #include <dev_mmc.h>
 
@@ -35,146 +33,144 @@
 /////////////////////////////////////////////////////////////////////////////////////////
 
-extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
+extern devices_directory_t  devices_dir;         // allocated in kernel_init.c
+
+extern devices_input_irq_t  devices_input_irq;   // allocated in kernel_init.c
 
 //////////////////////////////////
-void dev_mmc_init( chdev_t * mmc )
-{
-    // set mmc name
-    snprintk( mmc->name , 16 , "mmc_%x" , local_cxy );
-
-    // call driver init function
-    hal_drivers_mmc_init( mmc );
-
-    // bind IRQ to CP0
-    dev_pic_bind_irq( 0 , mmc );
-   
-    // enable IRQ
-    dev_pic_enable_irq( 0 , XPTR( local_cxy , mmc ) );
-
+void dev_mmc_init( xptr_t dev_xp )
+{
+    // get MMC device descriptor cluster and local pointer
+    cxy_t      dev_cxy = GET_CXY( dev_xp );
+    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
+
+    // get implementation from device descriptor
+    uint32_t  impl = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->impl ) );
+
+    // set driver specific fields in device descriptor
+    // and call driver init function
+    if( impl == IMPL_MMC_TSR )
+    {
+        hal_remote_spt( XPTR( dev_cxy , &dev_ptr->cmd ) , &soclib_mmc_command );
+        hal_remote_spt( XPTR( dev_cxy , &dev_ptr->isr ) , &soclib_mmc_isr );
+        hal_remote_memcpy( XPTR( dev_cxy , &dev_ptr->name ),
+                           XPTR( local_cxy , "MMC_TSR" ) , 16 );
+        soclib_mmc_init( dev_xp );
+    }
+    else
+    {
+        printk("\n[PANIC] in %s: undefined MMC device implementation\n", __FUNCTION__ );
+        hal_core_sleep();
+    }
 }  // end dev_mmc_init()
 
 /////////////////////////////////////////////////////////////////////////////
-// This static function is called by all MMC device access functions.
-// It makes some checking, takes the lock granting exclusive
-// access to MMC peripheral, call the driver to execute the command
+// This static function makes some checking, takes the lock granting
+// exclusive access to MMC peripheral, call the driver to execute the command
 // registered in the calling thread descriptor, and releases the lock.
 /////////////////////////////////////////////////////////////////////////////
-static error_t dev_mmc_access( thread_t * this )
-{
-    // get extended pointer on MMC device descriptor
-    xptr_t  dev_xp = this->mmc_cmd.dev_xp;
-
-    assert( __FUNCTION__, (dev_xp != XPTR_NULL) , "target MMC device undefined" );
+static void dev_mmc_access( thread_t * this )
+{
+    mmc_dmsg("\n[INFO] %s enters for thread %x in process %x : command = %d\n", 
+                 __FUNCTION__ , this->trdid , this->process->pid , this->dev.mmc.type );
+
+    // get extended pointer on MMC device
+    xptr_t  dev_xp  = this->dev.mmc.dev_xp;
+
+    if ( dev_xp == XPTR_NULL )
+    {
+        printk("\n[PANIC] in %s : undefined MMC device descriptor "
+               "in cluster %x for thread %x in process %x\n",
+               __FUNCTION__ , local_cxy , this->trdid , this->process->pid );
+        hal_core_sleep();
+    }
 
     // get MMC device cluster identifier & local pointer
-    cxy_t     dev_cxy = GET_CXY( dev_xp );
-    chdev_t * dev_ptr = GET_PTR( dev_xp );
+    cxy_t      dev_cxy = GET_CXY( dev_xp );
+    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
 
     // get driver command function pointer from MMC device descriptor
     dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
 
-    // get the MMC device remote busylock
-    remote_busylock_acquire( XPTR( dev_cxy , &dev_ptr->wait_lock ) );  
+    // get the MMC device remote spinlock
+    remote_spinlock_lock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );  
 
     // call driver command
     cmd( XPTR( local_cxy , this ) );
     
-    // release the MMC device remote busylock
-    remote_busylock_release( XPTR( dev_cxy , &dev_ptr->wait_lock ) );  
-
-    // return operation status
-    return this->mmc_cmd.error;  
+    // release the MMC device remote spinlock
+    remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );  
+
+    mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", 
+                 __FUNCTION__ , this->trdid , this->process->pid , this->dev.ioc.error );
 
 }  // end dev_mmc_access()
 
+////////////////////////////////////////////
+error_t dev_mmc_inval( paddr_t    buf_paddr,
+                       uint32_t   buf_size )
+{
+    // get calling thread local pointer
+    thread_t * this = CURRENT_THREAD;
+
+    // get target cluster from paddr
+    cxy_t cxy = CXY_FROM_PADDR( buf_paddr );
+
+    if ( buf_paddr & (CONFIG_CACHE_LINE_SIZE -1) )
+    {
+        printk("\n[PANIC] in %s : buffer not aligned on cache line "
+               "for thread %x / target cluster %x\n",
+               __FUNCTION__ , this->trdid , cxy );
+        hal_core_sleep();
+    }
+
+    // get extended pointer on MMC device descriptor
+    xptr_t  dev_xp = devices_dir.mmc[cxy];
+
+    // store command arguments in thread descriptor
+    this->dev.mmc.dev_xp    = dev_xp;
+    this->dev.mmc.type      = MMC_CC_INVAL;
+    this->dev.mmc.buf_paddr = buf_paddr;
+    this->dev.mmc.buf_size  = buf_size;
+
+    // execute operation
+    dev_mmc_access( this ); 
+
+    // return operation status
+    return this->dev.mmc.error;  
+}  
+
+/////////////////////////////////////
+error_t dev_mmc_sync( paddr_t    buf_paddr,
+                      uint32_t   buf_size )
+{
+    // get calling thread local pointer
+    thread_t * this = CURRENT_THREAD;
+
+    // get target cluster from paddr
+    cxy_t cxy = CXY_FROM_PADDR( buf_paddr );
+
+    if ( buf_paddr & (CONFIG_CACHE_LINE_SIZE -1) )
+    {
+        printk("\n[PANIC] in %s : buffer not aligned on cache line "
+               "for thread %x / target cluster %x\n",
+               __FUNCTION__ , this->trdid , cxy );
+        hal_core_sleep();
+    }
+
+    // store command arguments in thread descriptor
+    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
+    this->dev.mmc.type      = MMC_CC_SYNC;
+    this->dev.mmc.buf_paddr = buf_paddr;
+    this->dev.mmc.buf_size  = buf_size;
+
+    // execute operation
+    dev_mmc_access( this ); 
+
+    // return operation status
+    return this->dev.mmc.error;  
+}  
+
 /////////////////////////////////////////
-error_t dev_mmc_inval( xptr_t     buf_xp,
-                       uint32_t   buf_size )
-{
-    error_t error;
-
-    // get calling thread local pointer
-    thread_t  * this    = CURRENT_THREAD;
-
-#if DEBUG_DEV_MMC
-uint32_t cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_DEV_MMC < cycle )
-printk("\n[%s] thread[%x,%x] enters / cluster %x / buffer %x\n", 
-__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
-#endif
-
-    // get buffer cluster and local pointer
-    cxy_t     buf_cxy = GET_CXY( buf_xp );
-    uint8_t * buf_ptr = GET_PTR( buf_xp );
-    
-    // force buffer align
-    uint32_t  delta = (uint32_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE - 1); 
-    uint8_t * base  = buf_ptr - delta;
-    uint32_t  size  = buf_size + delta;
-
-    // store command arguments in thread descriptor
-    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
-    this->mmc_cmd.type      = MMC_CC_INVAL;
-    this->mmc_cmd.buf_ptr   = base;
-    this->mmc_cmd.buf_size  = size;
-
-    // call MMC driver
-    error = dev_mmc_access( this );
-
-#if DEBUG_DEV_MMC
-cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_DEV_MMC < cycle )
-printk("\n[%s] thread[%x,%x] exit / cluster %x / buffer %x\n", 
-__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
-#endif
-
-    return error;
-}
-
-////////////////////////////////////////
-error_t dev_mmc_sync( xptr_t     buf_xp,
-                      uint32_t   buf_size )
-{
-    error_t error;
-
-    thread_t  * this    = CURRENT_THREAD;
-
-#if DEBUG_DEV_MMC
-uint32_t cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_DEV_MMC < cycle )
-printk("\n[%s] thread[%x,%x] enters / cluster %x / buffer %x\n", 
-__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
-#endif
-
-    // get buffer cluster and local pointer
-    cxy_t  buf_cxy = GET_CXY( buf_xp );
-    void * buf_ptr = GET_PTR( buf_xp );
-    
-    // force buffer align
-    uint32_t  delta = (uint32_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE - 1); 
-    uint8_t * base  = buf_ptr - delta;
-    uint32_t  size  = buf_size + delta;
-
-    // store command arguments in thread descriptor
-    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
-    this->mmc_cmd.type      = MMC_CC_SYNC;
-    this->mmc_cmd.buf_ptr   = base;
-    this->mmc_cmd.buf_size  = size;
-
-    // call MMC driver
-    error = dev_mmc_access( this );
-
-#if DEBUG_DEV_MMC
-cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_DEV_MMC < cycle )
-printk("\n[%s] thread[%x,%x] exit / cluster %x / buffer %x\n", 
-__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
-#endif
-
-    return error;
-}
-
-/////////////////////////////////////////
-error_t dev_mmc_error_set( cxy_t     cxy,
+error_t dev_mmc_set_error( cxy_t     cxy,
                            uint32_t  index,
                            uint32_t  wdata )
@@ -184,15 +180,18 @@
 
     // store command arguments in thread descriptor
-    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
-    this->mmc_cmd.type      = MMC_ERROR_SET;
-    this->mmc_cmd.reg_index = index;
-    this->mmc_cmd.reg_ptr   = &wdata;
-
-    // execute operation
-    return dev_mmc_access( this ); 
+    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
+    this->dev.mmc.type      = MMC_SET_ERROR;
+    this->dev.mmc.reg_index = index;
+    this->dev.mmc.reg_ptr   = &wdata;
+
+    // execute operation
+    dev_mmc_access( this ); 
+
+    // return operation status
+    return this->dev.mmc.error;  
 }
                         
 //////////////////////////////////////////
-error_t dev_mmc_error_get( cxy_t      cxy,
+error_t dev_mmc_get_error( cxy_t      cxy,
                            uint32_t   index,
                            uint32_t * rdata )
@@ -202,28 +201,35 @@
 
     // store command arguments in thread descriptor
-    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
-    this->mmc_cmd.type      = MMC_ERROR_GET;
-    this->mmc_cmd.reg_index = index;
-    this->mmc_cmd.reg_ptr   = rdata;
-
-    // execute operation
-    return dev_mmc_access( this ); 
+    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
+    this->dev.mmc.type      = MMC_GET_ERROR;
+    this->dev.mmc.reg_index = index;
+    this->dev.mmc.reg_ptr   = rdata;
+
+    // execute operation
+    dev_mmc_access( this ); 
+
+    // return operation status
+    return this->dev.mmc.error;  
 }
-
-//////////////////////////////////////////
-error_t dev_mmc_instr_get( cxy_t      cxy,
-                           uint32_t   index,
-                           uint32_t * rdata )
-{
-    // get calling thread local pointer
-    thread_t * this = CURRENT_THREAD;
-
-    // store command arguments in thread descriptor
-    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
-    this->mmc_cmd.type      = MMC_INSTR_GET;
-    this->mmc_cmd.reg_index = index;
-    this->mmc_cmd.reg_ptr   = rdata;
-
-    // execute operation
-    return dev_mmc_access( this ); 
+                        
+////////////////////////////////////////////////////
+error_t dev_mmc_get_instrumentation( cxy_t      cxy,
+                                     uint32_t   index,
+                                     uint32_t * rdata )
+{
+    // get calling thread local pointer
+    thread_t * this = CURRENT_THREAD;
+
+    // store command arguments in thread descriptor
+    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
+    this->dev.mmc.type      = MMC_GET_INSTRU;
+    this->dev.mmc.reg_index = index;
+    this->dev.mmc.reg_ptr   = rdata;
+
+    // execute operation
+    dev_mmc_access( this ); 
+
+    // return operation status
+    return this->dev.mmc.error;  
 }
+
