| 1 | /* | 
|---|
| 2 |  * dev_fbf.c - FBF (Block Device Controler) generic device API implementation. | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Author  Alain Greiner    (2016,2017,2018) | 
|---|
| 5 |  * | 
|---|
| 6 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 |  * | 
|---|
| 8 |  * This file is part of ALMOS-MK | 
|---|
| 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 PARTICULAR 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-kernel; if not, write to the Free Software Foundation, | 
|---|
| 21 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 |  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <kernel_config.h> | 
|---|
| 25 | #include <hal_kernel_types.h> | 
|---|
| 26 | #include <hal_gpt.h> | 
|---|
| 27 | #include <thread.h> | 
|---|
| 28 | #include <printk.h> | 
|---|
| 29 | #include <string.h> | 
|---|
| 30 | #include <chdev.h> | 
|---|
| 31 | #include <dev_fbf.h> | 
|---|
| 32 |  | 
|---|
| 33 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 34 | // Extern global variables | 
|---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 36 |  | 
|---|
| 37 | extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c | 
|---|
| 38 |  | 
|---|
| 39 | //////////////////////////////////// | 
|---|
| 40 | void dev_fbf_init( chdev_t  * chdev ) | 
|---|
| 41 | { | 
|---|
| 42 |     // set FBF chdev extension fields | 
|---|
| 43 |     // TODO this should be done in the implementation  | 
|---|
| 44 |     // TODO specific part, as these parameters must be obtained from the hardware. | 
|---|
| 45 |     chdev->ext.fbf.width  = CONFIG_FBF_WIDTH; | 
|---|
| 46 |     chdev->ext.fbf.height = CONFIG_FBF_HEIGHT; | 
|---|
| 47 |  | 
|---|
| 48 |     // get implementation  | 
|---|
| 49 |     uint32_t  impl = chdev->impl; | 
|---|
| 50 |  | 
|---|
| 51 |     // set chdev name | 
|---|
| 52 |     strcpy( chdev->name, "fbf" ); | 
|---|
| 53 |  | 
|---|
| 54 |     // call driver init function | 
|---|
| 55 |     if( impl == IMPL_FBF_SCL ) | 
|---|
| 56 |     { | 
|---|
| 57 |         // TODO | 
|---|
| 58 |     } | 
|---|
| 59 |     else | 
|---|
| 60 |     { | 
|---|
| 61 |         assert( false , __FUNCTION__ , "undefined FBF device implementation" ); | 
|---|
| 62 |     } | 
|---|
| 63 |  | 
|---|
| 64 | }  // end dev_fbf_init() | 
|---|
| 65 |  | 
|---|
| 66 | ///////////////////////////////////////// | 
|---|
| 67 | void dev_fbf_get_size( uint32_t  * width, | 
|---|
| 68 |                        uint32_t  * height ) | 
|---|
| 69 | { | 
|---|
| 70 |     // get extended pointer on FBF chdev descriptor | 
|---|
| 71 |     xptr_t  dev_xp = chdev_dir.fbf[0]; | 
|---|
| 72 |  | 
|---|
| 73 |     assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" ); | 
|---|
| 74 |  | 
|---|
| 75 |     // get FBF chdev cluster and local pointer | 
|---|
| 76 |     cxy_t     dev_cxy = GET_CXY( dev_xp ); | 
|---|
| 77 |     chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| 78 |  | 
|---|
| 79 |     // return values | 
|---|
| 80 |     *width  = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.width ) ); | 
|---|
| 81 |     *height = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.height ) ); | 
|---|
| 82 |  | 
|---|
| 83 | }  // end dev_fbf_get_size() | 
|---|
| 84 |  | 
|---|
| 85 | /////////////////////// | 
|---|
| 86 | error_t dev_fbf_alloc() | 
|---|
| 87 | { | 
|---|
| 88 |     // get extended pointer on FBF chdev descriptor | 
|---|
| 89 |     xptr_t  dev_xp = chdev_dir.fbf[0]; | 
|---|
| 90 |  | 
|---|
| 91 |     assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" ); | 
|---|
| 92 |  | 
|---|
| 93 |     // get FBF chdev cluster and local pointer | 
|---|
| 94 |     cxy_t     dev_cxy = GET_CXY( dev_xp ); | 
|---|
| 95 |     chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| 96 |  | 
|---|
| 97 |     // try to get FBF ownership | 
|---|
| 98 |     return remote_spinlock_trylock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); | 
|---|
| 99 |  | 
|---|
| 100 | }  // end dev_fbf_alloc() | 
|---|
| 101 |  | 
|---|
| 102 | /////////////////// | 
|---|
| 103 | void dev_fbf_free() | 
|---|
| 104 | { | 
|---|
| 105 |     // get extended pointer on FBF chdev descriptor | 
|---|
| 106 |     xptr_t  dev_xp = chdev_dir.fbf[0]; | 
|---|
| 107 |  | 
|---|
| 108 |     assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" ); | 
|---|
| 109 |  | 
|---|
| 110 |     // get FBF chdev cluster and local pointer | 
|---|
| 111 |     cxy_t     dev_cxy = GET_CXY( dev_xp ); | 
|---|
| 112 |     chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| 113 |  | 
|---|
| 114 |     // release FBF ownership | 
|---|
| 115 |     remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); | 
|---|
| 116 |  | 
|---|
| 117 | }  // end dev_fbf_free() | 
|---|
| 118 |  | 
|---|
| 119 | ////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 120 | // This static function is called by dev_fbf_read() & dev_fbf_write() functions. | 
|---|
| 121 | // It builds and registers the command in the calling thread descriptor.  | 
|---|
| 122 | // Then, it registers the calling thread in the relevant DMA chdev waiting queue. | 
|---|
| 123 | // Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule. | 
|---|
| 124 | ////////////////////////////////////i///////////////////////////////////////////// | 
|---|
| 125 | static error_t dev_fbf_access( bool_t    to_fbf, | 
|---|
| 126 |                                char    * buffer, | 
|---|
| 127 |                                uint32_t  length, | 
|---|
| 128 |                                uint32_t  offset ) | 
|---|
| 129 | { | 
|---|
| 130 |  | 
|---|
| 131 |     // get extended pointer on FBF chdev descriptor | 
|---|
| 132 |     xptr_t  fbf_xp = chdev_dir.fbf[0]; | 
|---|
| 133 |  | 
|---|
| 134 |     assert( (fbf_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" ); | 
|---|
| 135 |  | 
|---|
| 136 |     // get FBF chdev cluster and local pointer | 
|---|
| 137 |     cxy_t     fbf_cxy = GET_CXY( fbf_xp ); | 
|---|
| 138 |     chdev_t * fbf_ptr = (chdev_t *)GET_PTR( fbf_xp ); | 
|---|
| 139 |  | 
|---|
| 140 |     // get frame buffer base address, width and height | 
|---|
| 141 |     xptr_t   base   = hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) ); | 
|---|
| 142 |     uint32_t width  = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.width ) ); | 
|---|
| 143 |     uint32_t height = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.height ) ); | 
|---|
| 144 |  | 
|---|
| 145 |     // check offset and length versus FBF size | 
|---|
| 146 |     assert( ((offset + length) <= (width * height)) , __FUNCTION__ , | 
|---|
| 147 |     "offset %d / length %d / width %d / height %d\n", offset, length, width, height );  | 
|---|
| 148 |  | 
|---|
| 149 |     // compute extended pointers on frame buffer and memory buffer | 
|---|
| 150 |     xptr_t  mem_buf_xp = XPTR( local_cxy , buffer ); | 
|---|
| 151 |     xptr_t  fbf_buf_xp = base + offset; | 
|---|
| 152 |  | 
|---|
| 153 |     // register command in DMA chdev | 
|---|
| 154 |     if( to_fbf )  dev_dma_remote_memcpy( fbf_buf_xp , mem_buf_xp , length ); | 
|---|
| 155 |     else          dev_dma_remote_memcpy( mem_buf_xp , fbf_buf_xp , length ); | 
|---|
| 156 |  | 
|---|
| 157 |     return 0; | 
|---|
| 158 |  | 
|---|
| 159 | }  // end dev_fbf_access() | 
|---|
| 160 |  | 
|---|
| 161 | //////////////////////////////////////////// | 
|---|
| 162 | error_t dev_fbf_read( char         * buffer, | 
|---|
| 163 |                       uint32_t       length, | 
|---|
| 164 |                       uint32_t       offset ) | 
|---|
| 165 | { | 
|---|
| 166 |  | 
|---|
| 167 | #if DEBUG_DEV_FBF_RX | 
|---|
| 168 | uint32_t cycle = (uint32_t)hal_get_cycle(); | 
|---|
| 169 | if( DEBUG_DEV_FBF_RX < cycle ) | 
|---|
| 170 | printk("\n[DBG] %s : thread %x enter / process %x / vaddr %x / size %x\n",  | 
|---|
| 171 | __FUNCTION__ , this, this->process->pid , buffer , buf_paddr ); | 
|---|
| 172 | #endif | 
|---|
| 173 |  | 
|---|
| 174 |     return dev_fbf_access( false , buffer , length , offset );  | 
|---|
| 175 |  | 
|---|
| 176 | #if DEBUG_DEV_FBF_RX | 
|---|
| 177 | cycle = (uint32_t)hal_get_cycle(); | 
|---|
| 178 | if( DEBUG_DEV_FBF_RX < cycle ) | 
|---|
| 179 | printk("\n[DBG] %s : thread %x exit / process %x / vaddr %x / size %x\n",  | 
|---|
| 180 | __FUNCTION__ , this, this->process->pid , buffer , buf_paddr ); | 
|---|
| 181 | #endif | 
|---|
| 182 |  | 
|---|
| 183 | }   | 
|---|
| 184 |  | 
|---|
| 185 | //////////////////////////////////////////// | 
|---|
| 186 | error_t dev_fbf_write( char         * buffer, | 
|---|
| 187 |                        uint32_t       length, | 
|---|
| 188 |                        uint32_t       offset ) | 
|---|
| 189 | { | 
|---|
| 190 |  | 
|---|
| 191 | #if DEBUG_DEV_FBF_TX | 
|---|
| 192 | uint32_t cycle = (uint32_t)hal_get_cycle(); | 
|---|
| 193 | if( DEBUG_DEV_FBF_TX < cycle ) | 
|---|
| 194 | printk("\n[DBG] %s : thread %x enter / process %x / vaddr %x / size %x\n",  | 
|---|
| 195 | __FUNCTION__ , this, this->process->pid , buffer , buf_paddr ); | 
|---|
| 196 | #endif | 
|---|
| 197 |  | 
|---|
| 198 |     return dev_fbf_access( true , buffer , length , offset );  | 
|---|
| 199 |  | 
|---|
| 200 | #if DEBUG_DEV_FBF_RX | 
|---|
| 201 | cycle = (uint32_t)hal_get_cycle(); | 
|---|
| 202 | if( DEBUG_DEV_FBF_RX < cycle ) | 
|---|
| 203 | printk("\n[DBG] %s : thread %x exit / process %x / vaddr %x / size %x\n",  | 
|---|
| 204 | __FUNCTION__ , this, this->process->pid , buffer , buf_paddr ); | 
|---|
| 205 | #endif | 
|---|
| 206 |  | 
|---|
| 207 | } | 
|---|