[3] | 1 | /* |
---|
| 2 | * dev_dma.c - DMA (Interrupt Controler Unit) generic device API implementation. |
---|
| 3 | * |
---|
[657] | 4 | * Authors Alain Greiner (2016,2017,2018,2019,2020) |
---|
[3] | 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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[3] | 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> |
---|
[261] | 32 | #include <hal_drivers.h> |
---|
[3] | 33 | |
---|
| 34 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 35 | // Extern global variables |
---|
| 36 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 37 | |
---|
| 38 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
| 39 | |
---|
[238] | 40 | ////////////////////////////////// |
---|
| 41 | void dev_dma_init( chdev_t * dma ) |
---|
[3] | 42 | { |
---|
[647] | 43 | // get channel from chdev descriptor |
---|
[238] | 44 | uint32_t channel = dma->channel; |
---|
[3] | 45 | |
---|
[238] | 46 | // set dma name |
---|
[674] | 47 | snprintk( dma->name , 16 , "dma%d_%x" , channel , local_cxy ); |
---|
[23] | 48 | |
---|
[261] | 49 | // call driver init function |
---|
[647] | 50 | hal_drivers_dma_init( dma ); |
---|
[3] | 51 | |
---|
[188] | 52 | // bind IRQ to the core defined by the DMA channel |
---|
[238] | 53 | dev_pic_bind_irq( channel , dma ); |
---|
[3] | 54 | |
---|
[188] | 55 | // enable IRQ |
---|
[238] | 56 | dev_pic_enable_irq( channel, XPTR( local_cxy , dma ) ); |
---|
[3] | 57 | |
---|
| 58 | // create server thread |
---|
| 59 | thread_t * new_thread; |
---|
| 60 | error_t error; |
---|
| 61 | |
---|
[637] | 62 | lid_t lid = cluster_select_local_core( local_cxy ); |
---|
| 63 | |
---|
[3] | 64 | error = thread_kernel_create( &new_thread, |
---|
| 65 | THREAD_DEV, |
---|
[619] | 66 | &chdev_server_func, |
---|
[238] | 67 | dma, |
---|
[637] | 68 | lid ); |
---|
[3] | 69 | if( error ) |
---|
| 70 | { |
---|
[674] | 71 | assert( __FUNCTION__, false , "cannot create server thread" ); |
---|
[3] | 72 | } |
---|
| 73 | |
---|
[408] | 74 | // initialises server field in chdev descriptor |
---|
[238] | 75 | dma->server = new_thread; |
---|
[408] | 76 | |
---|
| 77 | // initializes chdev field in thread descriptor |
---|
| 78 | new_thread->chdev = dma; |
---|
[3] | 79 | |
---|
[408] | 80 | // unblock server thread |
---|
[3] | 81 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
[188] | 82 | |
---|
[3] | 83 | } // dev_dma_init() |
---|
| 84 | |
---|
| 85 | //////////////////////////////////////////////// |
---|
[657] | 86 | error_t dev_dma_async_memcpy( xptr_t dst_xp, |
---|
| 87 | xptr_t src_xp, |
---|
| 88 | uint32_t size ) |
---|
[3] | 89 | { |
---|
| 90 | thread_t * this = CURRENT_THREAD; |
---|
| 91 | |
---|
[657] | 92 | #if DEBUG_DEV_DMA |
---|
[437] | 93 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 94 | if( CONGIG_DEBUG_DEV_DMA < cycle ) |
---|
| 95 | printk("\n[DBG] %s : thread %x enters / dst %l / src %l / size = %x\n", |
---|
| 96 | __FUNCTION__ , this, dst_xp, src_xp, size ); |
---|
| 97 | #endif |
---|
[3] | 98 | |
---|
| 99 | // select DMA channel corresponding to core lid |
---|
| 100 | uint32_t channel = this->core->lid; |
---|
| 101 | |
---|
| 102 | // get extended pointer on selected DMA chdev descriptor |
---|
| 103 | xptr_t dev_xp = chdev_dir.dma[channel]; |
---|
| 104 | |
---|
[647] | 105 | // check DMA chdev definition |
---|
[674] | 106 | assert( __FUNCTION__, (dev_xp != XPTR_NULL) , "undefined DMA chdev descriptor" ); |
---|
[3] | 107 | |
---|
| 108 | // register command in calling thread descriptor |
---|
[657] | 109 | this->dma_cmd.sync = false; |
---|
[279] | 110 | this->dma_cmd.dev_xp = dev_xp; |
---|
| 111 | this->dma_cmd.dst_xp = dst_xp; |
---|
| 112 | this->dma_cmd.src_xp = src_xp; |
---|
| 113 | this->dma_cmd.size = size; |
---|
[3] | 114 | |
---|
[657] | 115 | // register the client thread in waiting queue, activate the server thread, |
---|
| 116 | // block the client thread on THREAD_BLOCKED_IO, and deschedule. |
---|
| 117 | // it is re-activated by the server thread when the transfer is completed. |
---|
[407] | 118 | chdev_register_command( dev_xp ); |
---|
[3] | 119 | |
---|
[657] | 120 | #if DEBUG_DEV_DMA |
---|
[437] | 121 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 122 | if( CONGIG_DEBUG_DEV_DMA < cycle ) |
---|
| 123 | printk("\n[DBG] %s : thread %x exit / dst %l / src %l / size = %x\n", |
---|
| 124 | __FUNCTION__ , this, dst_xp, src_xp, size ); |
---|
| 125 | #endif |
---|
[3] | 126 | |
---|
| 127 | // return I/O operation status from calling thread descriptor |
---|
[279] | 128 | return this->dma_cmd.error; |
---|
[3] | 129 | |
---|
[657] | 130 | } // dev_dma_async_memcpy() |
---|
[3] | 131 | |
---|
[657] | 132 | ////////////////////////////////////////////// |
---|
| 133 | error_t dev_dma_sync_memcpy( xptr_t dst_xp, |
---|
| 134 | xptr_t src_xp, |
---|
| 135 | uint32_t size ) |
---|
| 136 | { |
---|
| 137 | thread_t * this = CURRENT_THREAD; |
---|
| 138 | |
---|
| 139 | #if DEBUG_DEV_DMA |
---|
| 140 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 141 | if( CONGIG_DEBUG_DEV_DMA < cycle ) |
---|
| 142 | printk("\n[DBG] %s : thread %x enters / dst %l / src %l / size = %x\n", |
---|
| 143 | __FUNCTION__ , this, dst_xp, src_xp, size ); |
---|
| 144 | #endif |
---|
| 145 | |
---|
| 146 | // select DMA channel corresponding to core lid |
---|
| 147 | uint32_t channel = this->core->lid; |
---|
| 148 | |
---|
| 149 | // get extended pointer on selected DMA chdev descriptor |
---|
| 150 | xptr_t dev_xp = chdev_dir.dma[channel]; |
---|
| 151 | |
---|
| 152 | // check DMA chdev definition |
---|
[674] | 153 | assert( __FUNCTION__, (dev_xp != XPTR_NULL) , "undefined DMA chdev descriptor" ); |
---|
[657] | 154 | |
---|
| 155 | // register command in calling thread descriptor |
---|
| 156 | this->dma_cmd.sync = true; |
---|
| 157 | this->dma_cmd.dev_xp = dev_xp; |
---|
| 158 | this->dma_cmd.dst_xp = dst_xp; |
---|
| 159 | this->dma_cmd.src_xp = src_xp; |
---|
| 160 | this->dma_cmd.size = size; |
---|
| 161 | |
---|
| 162 | // get driver command function |
---|
| 163 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 164 | chdev_t * dev_ptr = GET_PTR( dev_xp ); |
---|
| 165 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
---|
| 166 | |
---|
| 167 | // call directly the blocking driver function |
---|
| 168 | cmd( XPTR( local_cxy , this ) ); |
---|
| 169 | |
---|
| 170 | #if DEBUG_DEV_DMA |
---|
| 171 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 172 | if( CONGIG_DEBUG_DEV_DMA < cycle ) |
---|
| 173 | printk("\n[DBG] %s : thread %x exit / dst %l / src %l / size = %x\n", |
---|
| 174 | __FUNCTION__ , this, dst_xp, src_xp, size ); |
---|
| 175 | #endif |
---|
| 176 | |
---|
| 177 | // return I/O operation status from calling thread descriptor |
---|
| 178 | return this->dma_cmd.error; |
---|
| 179 | |
---|
| 180 | } // dev_dma_sync_memcpy() |
---|
| 181 | |
---|