source: trunk/kernel/devices/dev_dma.c @ 565

Last change on this file since 565 was 565, checked in by alain, 3 years ago

Complete restructuration of kernel locks.

File size: 4.1 KB
Line 
1/*
2 * dev_dma.c - DMA (Interrupt Controler Unit) generic device API implementation.
3 *
4 * Authors   Alain Greiner  (2016,2017,2018)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTDMALAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <hal_kernel_types.h>
25#include <hal_special.h>
26#include <chdev.h>
27#include <thread.h>
28#include <cluster.h>
29#include <printk.h>
30#include <memcpy.h>
31#include <dev_dma.h>
32#include <hal_drivers.h>
33
34/////////////////////////////////////////////////////////////////////////////////////////
35// Extern global variables
36/////////////////////////////////////////////////////////////////////////////////////////
37
38extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
39
40//////////////////////////////////
41void dev_dma_init( chdev_t * dma )
42{
43    // get implementation & channel from DMA dma descriptor
44    uint32_t impl    = dma->impl;
45    uint32_t channel = dma->channel;
46
47    // set dma name
48    snprintf( dma->name , 16 , "dma%d_%x" , channel , local_cxy );
49
50    // call driver init function
51    hal_drivers_dma_init( dma, impl );
52
53    // bind IRQ to the core defined by the DMA channel
54    dev_pic_bind_irq( channel , dma );
55
56    // enable IRQ
57    dev_pic_enable_irq( channel, XPTR( local_cxy , dma ) );
58
59    // create server thread
60    thread_t * new_thread;
61    error_t    error;
62
63    error = thread_kernel_create( &new_thread,
64                                  THREAD_DEV,
65                                  &chdev_sequencial_server,
66                                  dma,
67                                  cluster_select_local_core() );
68    if( error )
69    {
70        assert( false , "cannot create server thread" );
71    }
72
73    // initialises server field in chdev descriptor
74    dma->server = new_thread;
75
76    // initializes chdev field in thread descriptor
77    new_thread->chdev = dma;
78   
79    // unblock server thread
80    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
81
82} // dev_dma_init()
83
84////////////////////////////////////////////////
85error_t dev_dma_remote_memcpy( xptr_t    dst_xp,
86                               xptr_t    src_xp,
87                               uint32_t  size )
88{
89    thread_t * this = CURRENT_THREAD;
90
91#if CONGIG_DEBUG_DEV_DMA
92uint32_t cycle = (uint32_t)hal_get_cycles();
93if( CONGIG_DEBUG_DEV_DMA < cycle )
94printk("\n[DBG] %s : thread %x enters / dst %l / src %l / size = %x\n",
95__FUNCTION__ , this, dst_xp, src_xp, size );
96#endif
97
98    // select DMA channel corresponding to core lid
99    uint32_t channel = this->core->lid;
100
101    // get extended pointer on selected DMA chdev descriptor
102    xptr_t  dev_xp = chdev_dir.dma[channel];
103
104    assert( (dev_xp != XPTR_NULL) , "undefined DMA chdev descriptor" );
105
106    // register command in calling thread descriptor
107    this->dma_cmd.dev_xp  = dev_xp;
108    this->dma_cmd.dst_xp  = dst_xp;
109    this->dma_cmd.src_xp  = src_xp;
110    this->dma_cmd.size    = size;
111
112    // register client thread in waiting queue, activate server thread
113    // block client thread on THREAD_BLOCKED_IO and deschedule.
114    // it is re-activated by the ISR signaling IO operation completion.
115    chdev_register_command( dev_xp );
116
117#if CONGIG_DEBUG_DEV_DMA
118cycle = (uint32_t)hal_get_cycles();
119if( CONGIG_DEBUG_DEV_DMA < cycle )
120printk("\n[DBG] %s : thread %x exit / dst %l / src %l / size = %x\n",
121__FUNCTION__ , this, dst_xp, src_xp, size );
122#endif
123
124    // return I/O operation status from calling thread descriptor
125    return this->dma_cmd.error; 
126
127}  // dev_dma_remote_memcpy()
128
Note: See TracBrowser for help on using the repository browser.