/*
 * dev_dma.c - DMA (Interrupt Controler Unit) generic device API implementation.
 *
 * Authors   Alain Greiner  (2016,2017,2018,2019,2020)
 *
 * Copyright (c) UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.0 of the License.
 *
 * ALMOS-MKH is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTDMALAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <hal_kernel_types.h>
#include <hal_special.h>
#include <chdev.h>
#include <thread.h>
#include <cluster.h>
#include <printk.h>
#include <memcpy.h>
#include <dev_dma.h>
#include <hal_drivers.h>

/////////////////////////////////////////////////////////////////////////////////////////
// Extern global variables
/////////////////////////////////////////////////////////////////////////////////////////

extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c

//////////////////////////////////
void dev_dma_init( chdev_t * dma )
{
    // get channel from chdev descriptor
    uint32_t channel = dma->channel;

    // set dma name
    snprintf( dma->name , 16 , "dma%d_%x" , channel , local_cxy );

    // call driver init function
    hal_drivers_dma_init( dma );

    // bind IRQ to the core defined by the DMA channel 
    dev_pic_bind_irq( channel , dma );

    // enable IRQ
    dev_pic_enable_irq( channel, XPTR( local_cxy , dma ) );

    // create server thread
    thread_t * new_thread;
    error_t    error;

    lid_t lid = cluster_select_local_core( local_cxy );

    error = thread_kernel_create( &new_thread,
                                  THREAD_DEV,
                                  &chdev_server_func,
                                  dma,
                                  lid );
    if( error )
    {
        assert( false , "cannot create server thread" );
    }

    // initialises server field in chdev descriptor
    dma->server = new_thread;

    // initializes chdev field in thread descriptor
    new_thread->chdev = dma;
    
    // unblock server thread
    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );

} // dev_dma_init()

////////////////////////////////////////////////
error_t dev_dma_async_memcpy( xptr_t    dst_xp,
                              xptr_t    src_xp,
                              uint32_t  size )
{
    thread_t * this = CURRENT_THREAD;

#if DEBUG_DEV_DMA
uint32_t cycle = (uint32_t)hal_get_cycles();
if( CONGIG_DEBUG_DEV_DMA < cycle )
printk("\n[DBG] %s : thread %x enters / dst %l / src %l / size = %x\n",
__FUNCTION__ , this, dst_xp, src_xp, size );
#endif

    // select DMA channel corresponding to core lid
    uint32_t channel = this->core->lid;

    // get extended pointer on selected DMA chdev descriptor
    xptr_t  dev_xp = chdev_dir.dma[channel];

// check DMA chdev definition
assert( (dev_xp != XPTR_NULL) , "undefined DMA chdev descriptor" );

    // register command in calling thread descriptor
    this->dma_cmd.sync    = false;
    this->dma_cmd.dev_xp  = dev_xp;
    this->dma_cmd.dst_xp  = dst_xp;
    this->dma_cmd.src_xp  = src_xp;
    this->dma_cmd.size    = size;

    // register the client thread in waiting queue, activate the server thread,
    // block the client thread on THREAD_BLOCKED_IO, and deschedule.
    // it is re-activated by the server thread  when the transfer is completed.
    chdev_register_command( dev_xp );

#if DEBUG_DEV_DMA
cycle = (uint32_t)hal_get_cycles();
if( CONGIG_DEBUG_DEV_DMA < cycle )
printk("\n[DBG] %s : thread %x exit / dst %l / src %l / size = %x\n",
__FUNCTION__ , this, dst_xp, src_xp, size );
#endif

    // return I/O operation status from calling thread descriptor
    return this->dma_cmd.error;  

}  // dev_dma_async_memcpy()

//////////////////////////////////////////////
error_t dev_dma_sync_memcpy( xptr_t    dst_xp,
                             xptr_t    src_xp,
                             uint32_t  size )
{
    thread_t * this = CURRENT_THREAD;

#if DEBUG_DEV_DMA
uint32_t cycle = (uint32_t)hal_get_cycles();
if( CONGIG_DEBUG_DEV_DMA < cycle )
printk("\n[DBG] %s : thread %x enters / dst %l / src %l / size = %x\n",
__FUNCTION__ , this, dst_xp, src_xp, size );
#endif

    // select DMA channel corresponding to core lid
    uint32_t channel = this->core->lid;

    // get extended pointer on selected DMA chdev descriptor
    xptr_t  dev_xp = chdev_dir.dma[channel];

// check DMA chdev definition
assert( (dev_xp != XPTR_NULL) , "undefined DMA chdev descriptor" );

    // register command in calling thread descriptor
    this->dma_cmd.sync    = true;
    this->dma_cmd.dev_xp  = dev_xp;
    this->dma_cmd.dst_xp  = dst_xp;
    this->dma_cmd.src_xp  = src_xp;
    this->dma_cmd.size    = size;

    // get driver command function
    cxy_t       dev_cxy = GET_CXY( dev_xp );
    chdev_t   * dev_ptr = GET_PTR( dev_xp );
    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );

    // call directly the blocking driver function
    cmd( XPTR( local_cxy , this ) );

#if DEBUG_DEV_DMA
cycle = (uint32_t)hal_get_cycles();
if( CONGIG_DEBUG_DEV_DMA < cycle )
printk("\n[DBG] %s : thread %x exit / dst %l / src %l / size = %x\n",
__FUNCTION__ , this, dst_xp, src_xp, size );
#endif

    // return I/O operation status from calling thread descriptor
    return this->dma_cmd.error;  

}  // dev_dma_sync_memcpy()

