[3] | 1 | /* |
---|
| 2 | * dev_dma.c - DMA (Interrupt Controler Unit) generic device API implementation. |
---|
| 3 | * |
---|
| 4 | * Authors Alain Greiner (2017) |
---|
| 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_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 <spinlock.h> |
---|
| 32 | #include <dev_dma.h> |
---|
[261] | 33 | #include <hal_drivers.h> |
---|
[3] | 34 | |
---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | // Extern global variables |
---|
| 37 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 38 | |
---|
| 39 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
| 40 | |
---|
[238] | 41 | ////////////////////////////////// |
---|
| 42 | void dev_dma_init( chdev_t * dma ) |
---|
[3] | 43 | { |
---|
[238] | 44 | // get implementation & channel from DMA dma descriptor |
---|
| 45 | uint32_t impl = dma->impl; |
---|
| 46 | uint32_t channel = dma->channel; |
---|
[3] | 47 | |
---|
[238] | 48 | // set dma name |
---|
[408] | 49 | snprintf( dma->name , 16 , "dma%d_%x" , channel , local_cxy ); |
---|
[23] | 50 | |
---|
[261] | 51 | // call driver init function |
---|
| 52 | hal_drivers_dma_init( dma, impl ); |
---|
[3] | 53 | |
---|
[188] | 54 | // bind IRQ to the core defined by the DMA channel |
---|
[238] | 55 | dev_pic_bind_irq( channel , dma ); |
---|
[3] | 56 | |
---|
[188] | 57 | // enable IRQ |
---|
[238] | 58 | dev_pic_enable_irq( channel, XPTR( local_cxy , dma ) ); |
---|
[3] | 59 | |
---|
| 60 | // create server thread |
---|
| 61 | thread_t * new_thread; |
---|
| 62 | error_t error; |
---|
| 63 | |
---|
| 64 | error = thread_kernel_create( &new_thread, |
---|
| 65 | THREAD_DEV, |
---|
| 66 | &chdev_sequencial_server, |
---|
[238] | 67 | dma, |
---|
[3] | 68 | cluster_select_local_core() ); |
---|
| 69 | if( error ) |
---|
| 70 | { |
---|
| 71 | assert( false , __FUNCTION__ , "cannot create server thread" ); |
---|
| 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 | //////////////////////////////////////////////// |
---|
| 86 | error_t dev_dma_remote_memcpy( xptr_t dst_xp, |
---|
| 87 | xptr_t src_xp, |
---|
| 88 | uint32_t size ) |
---|
| 89 | { |
---|
| 90 | thread_t * this = CURRENT_THREAD; |
---|
| 91 | |
---|
[407] | 92 | dma_dmsg("\n[DBG] %s : enters for thread %x / dst = %l /src = %l / size = %x\n", |
---|
[3] | 93 | __FUNCTION__ , this->trdid , dst_xp , src_xp , size ); |
---|
| 94 | |
---|
| 95 | // select DMA channel corresponding to core lid |
---|
| 96 | uint32_t channel = this->core->lid; |
---|
| 97 | |
---|
| 98 | // get extended pointer on selected DMA chdev descriptor |
---|
| 99 | xptr_t dev_xp = chdev_dir.dma[channel]; |
---|
| 100 | |
---|
| 101 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined DMA chdev descriptor" ); |
---|
| 102 | |
---|
| 103 | // register command in calling thread descriptor |
---|
[279] | 104 | this->dma_cmd.dev_xp = dev_xp; |
---|
| 105 | this->dma_cmd.dst_xp = dst_xp; |
---|
| 106 | this->dma_cmd.src_xp = src_xp; |
---|
| 107 | this->dma_cmd.size = size; |
---|
[3] | 108 | |
---|
| 109 | // register client thread in waiting queue, activate server thread |
---|
| 110 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
| 111 | // it is re-activated by the ISR signaling IO operation completion. |
---|
[407] | 112 | chdev_register_command( dev_xp ); |
---|
[3] | 113 | |
---|
[407] | 114 | dma_dmsg("\n[DBG] %s : completes for thread %x / error = %d\n", |
---|
[279] | 115 | __FUNCTION__ , this->trdid , this->dma_cmd.error ); |
---|
[3] | 116 | |
---|
| 117 | // return I/O operation status from calling thread descriptor |
---|
[279] | 118 | return this->dma_cmd.error; |
---|
[3] | 119 | |
---|
| 120 | } // dev_dma_remote_memcpy() |
---|
| 121 | |
---|