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