[642] | 1 | /* |
---|
| 2 | * sys_fbf.c - Acces the frame buffer peripheral. |
---|
| 3 | * |
---|
[657] | 4 | * Authors Alain Greiner (2016,2017,2018,2019,2020) |
---|
[642] | 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 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-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_uspace.h> |
---|
| 26 | #include <hal_vmm.h> |
---|
| 27 | #include <shared_fbf.h> |
---|
| 28 | #include <dev_fbf.h> |
---|
| 29 | #include <errno.h> |
---|
| 30 | #include <thread.h> |
---|
| 31 | #include <printk.h> |
---|
| 32 | #include <vmm.h> |
---|
| 33 | #include <syscalls.h> |
---|
| 34 | |
---|
[657] | 35 | //////////////////////// |
---|
| 36 | int sys_fbf( reg_t arg0, |
---|
| 37 | reg_t arg1, |
---|
| 38 | reg_t arg2, |
---|
| 39 | reg_t arg3 ) |
---|
[642] | 40 | { |
---|
| 41 | vseg_t * vseg; // for vaddr check |
---|
| 42 | error_t error; |
---|
| 43 | |
---|
| 44 | #if (DEBUG_SYS_FBF || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 45 | uint64_t tm_start = hal_get_cycles(); |
---|
| 46 | #endif |
---|
| 47 | |
---|
[657] | 48 | thread_t * this = CURRENT_THREAD; |
---|
| 49 | process_t * process = this->process; |
---|
| 50 | |
---|
| 51 | // for some operations, the MSB of arg0 can contain the wid |
---|
| 52 | uint32_t operation = arg0 & 0xFFFF; |
---|
| 53 | |
---|
[642] | 54 | #if DEBUG_SYS_FBF |
---|
| 55 | if( DEBUG_SYS_FBF < tm_start ) |
---|
[657] | 56 | printk("\n[%s] thread[%x,%x] enter for %s / arg1 %x / arg2 %x / arg3 %x / cycle %d\n", |
---|
| 57 | __FUNCTION__, process->pid, this->trdid, dev_fbf_cmd_str( operation ), |
---|
| 58 | arg1, arg2, arg3, (uint32_t)tm_start ); |
---|
[642] | 59 | #endif |
---|
| 60 | |
---|
| 61 | // execute requested operation |
---|
| 62 | switch( operation ) |
---|
| 63 | { |
---|
| 64 | //////////////////// |
---|
| 65 | case FBF_GET_CONFIG: |
---|
| 66 | { |
---|
| 67 | uint32_t width; |
---|
| 68 | uint32_t height; |
---|
| 69 | uint32_t type; |
---|
| 70 | |
---|
[657] | 71 | // check "width" in user vspace |
---|
| 72 | error = vmm_get_vseg( process , (intptr_t)arg1 , &vseg ); |
---|
[642] | 73 | if( error ) |
---|
| 74 | { |
---|
| 75 | |
---|
| 76 | #if DEBUG_SYSCALLS_ERROR |
---|
[657] | 77 | printk("\n[ERROR] in %s : unmapped arg1 %x for %s / thread[%x,%x]\n", |
---|
| 78 | __FUNCTION__ , arg1, dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
[642] | 79 | #endif |
---|
| 80 | this->errno = EINVAL; |
---|
| 81 | } |
---|
| 82 | |
---|
[657] | 83 | // check "height" in user vspace |
---|
| 84 | error |= vmm_get_vseg( process , (intptr_t)arg2 , &vseg ); |
---|
[642] | 85 | if( error ) |
---|
| 86 | { |
---|
| 87 | |
---|
| 88 | #if DEBUG_SYSCALLS_ERROR |
---|
[657] | 89 | printk("\n[ERROR] in %s : unmapped arg2 %x for %s / thread[%x,%x]\n", |
---|
| 90 | __FUNCTION__ , arg2, dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
[642] | 91 | #endif |
---|
| 92 | this->errno = EINVAL; |
---|
| 93 | } |
---|
| 94 | |
---|
[657] | 95 | // check "type" in user vspace |
---|
| 96 | error |= vmm_get_vseg( process , (intptr_t)arg3 , &vseg ); |
---|
[642] | 97 | if( error ) |
---|
| 98 | { |
---|
| 99 | |
---|
| 100 | #if DEBUG_SYSCALLS_ERROR |
---|
[657] | 101 | printk("\n[ERROR] in %s : unmapped arg3 %x for %s / thread[%x,%x]\n", |
---|
| 102 | __FUNCTION__ , arg3, dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
[642] | 103 | #endif |
---|
| 104 | this->errno = EINVAL; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // call relevant kernel function to get config |
---|
| 108 | dev_fbf_get_config( &width , &height , &type ); |
---|
| 109 | |
---|
[657] | 110 | // transfer to user space if no error |
---|
| 111 | if( error == 0 ) |
---|
| 112 | { |
---|
| 113 | hal_copy_to_uspace( (void*)arg1, XPTR(local_cxy , &width ), sizeof(uint32_t) ); |
---|
| 114 | hal_copy_to_uspace( (void*)arg2, XPTR(local_cxy , &height), sizeof(uint32_t) ); |
---|
| 115 | hal_copy_to_uspace( (void*)arg3, XPTR(local_cxy , &type ), sizeof(uint32_t) ); |
---|
| 116 | } |
---|
[642] | 117 | break; |
---|
| 118 | } |
---|
[657] | 119 | ///////////////////// |
---|
| 120 | case FBF_DIRECT_READ: |
---|
| 121 | case FBF_DIRECT_WRITE: |
---|
[642] | 122 | { |
---|
[657] | 123 | void * buffer = (void *)arg1; |
---|
| 124 | uint32_t npixels = arg2; |
---|
| 125 | uint32_t offset = arg3; |
---|
| 126 | bool_t is_write = (operation == FBF_DIRECT_WRITE); |
---|
| 127 | |
---|
| 128 | // check buffer in user space |
---|
| 129 | error = vmm_get_vseg( process , (intptr_t)buffer , &vseg ); |
---|
[642] | 130 | if( error ) |
---|
| 131 | { |
---|
| 132 | |
---|
| 133 | #if DEBUG_SYSCALLS_ERROR |
---|
[657] | 134 | printk("\n[ERROR] in %s : unmapped buffer %x for %s / thread[%x,%x]\n", |
---|
| 135 | __FUNCTION__ , buffer, dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
[642] | 136 | #endif |
---|
| 137 | this->errno = EINVAL; |
---|
| 138 | } |
---|
[657] | 139 | else |
---|
| 140 | { |
---|
| 141 | // call relevant kernel function |
---|
| 142 | error |= dev_fbf_move_data( is_write, buffer, npixels, offset ); |
---|
[642] | 143 | |
---|
[657] | 144 | if( error ) |
---|
| 145 | { |
---|
[642] | 146 | |
---|
[657] | 147 | #if DEBUG_SYSCALLS_ERROR |
---|
| 148 | printk("\n[ERROR] in %s : cannot move data for %s / buffer %x / thread[%x,%x]\n", |
---|
| 149 | __FUNCTION__ , dev_fbf_cmd_str(operation), buffer, process->pid, this->trdid ); |
---|
| 150 | #endif |
---|
| 151 | this->errno = EINVAL; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | break; |
---|
| 155 | } |
---|
| 156 | /////////////////////// |
---|
| 157 | case FBF_CREATE_WINDOW: |
---|
| 158 | { |
---|
| 159 | uint32_t l_zero = arg1 >> 16; |
---|
| 160 | uint32_t p_zero = arg1 & 0xFFFF; |
---|
| 161 | uint32_t nlines = arg2 >> 16; |
---|
| 162 | uint32_t npixels = arg2 & 0xFFFF; |
---|
[642] | 163 | |
---|
[657] | 164 | // check buffer in user space |
---|
| 165 | error = vmm_get_vseg( process , (intptr_t)arg3 , &vseg ); |
---|
| 166 | if( error ) |
---|
| 167 | { |
---|
| 168 | |
---|
| 169 | #if DEBUG_SYSCALLS_ERROR |
---|
| 170 | printk("\n[ERROR] in %s : unmapped user buffer %x for %s / thread[%x,%x]\n", |
---|
| 171 | __FUNCTION__ , (intptr_t)arg3, dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
| 172 | #endif |
---|
| 173 | this->errno = EINVAL; |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | // allocated buffer base address |
---|
| 178 | intptr_t user_buffer; |
---|
| 179 | |
---|
| 180 | // call relevant kernel function |
---|
| 181 | error = dev_fbf_create_window( nlines, |
---|
| 182 | npixels, |
---|
| 183 | l_zero, |
---|
| 184 | p_zero, |
---|
| 185 | &user_buffer ); |
---|
| 186 | if( error == -1 ) |
---|
| 187 | { |
---|
| 188 | |
---|
| 189 | #if DEBUG_SYSCALLS_ERROR |
---|
| 190 | printk("\n[ERROR] in %s : cannot create window for %s / thread[%x,%x]\n", |
---|
| 191 | __FUNCTION__ , dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
| 192 | #endif |
---|
| 193 | this->errno = EINVAL; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | // copy vseg base address to user space buffer |
---|
| 197 | hal_copy_to_uspace( (void *)arg3, |
---|
| 198 | XPTR( local_cxy , &user_buffer ), |
---|
| 199 | sizeof(intptr_t) ); |
---|
| 200 | hal_fence(); |
---|
| 201 | } |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | /////////////////////// |
---|
| 205 | case FBF_DELETE_WINDOW: |
---|
| 206 | { |
---|
| 207 | uint32_t wid = arg1; |
---|
| 208 | |
---|
| 209 | // call relevant kernel function |
---|
| 210 | error = dev_fbf_delete_window( wid ); |
---|
| 211 | |
---|
| 212 | if( error ) |
---|
| 213 | { |
---|
| 214 | |
---|
| 215 | #if DEBUG_SYSCALLS_ERROR |
---|
| 216 | printk("\n[ERROR] in %s : cannot delete window for %s / thread[%x,%x]\n", |
---|
| 217 | __FUNCTION__ , dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
| 218 | #endif |
---|
| 219 | this->errno = EINVAL; |
---|
| 220 | } |
---|
| 221 | break; |
---|
| 222 | } |
---|
| 223 | //////////////////////// |
---|
| 224 | case FBF_REFRESH_WINDOW: |
---|
| 225 | { |
---|
| 226 | uint32_t wid = arg1; |
---|
| 227 | uint32_t line_first = arg2; |
---|
| 228 | uint32_t line_last = arg3; |
---|
| 229 | |
---|
| 230 | // call relevant kernel function |
---|
| 231 | error = dev_fbf_refresh_window( wid, |
---|
| 232 | line_first, |
---|
| 233 | line_last ); |
---|
| 234 | |
---|
| 235 | if( error ) |
---|
| 236 | { |
---|
| 237 | |
---|
| 238 | #if DEBUG_SYSCALLS_ERROR |
---|
| 239 | printk("\n[ERROR] in %s : cannot refresh window for %s / thread[%x,%x]\n", |
---|
| 240 | __FUNCTION__ , dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
| 241 | #endif |
---|
| 242 | this->errno = EINVAL; |
---|
| 243 | } |
---|
| 244 | break; |
---|
| 245 | } |
---|
| 246 | ///////////////////// |
---|
| 247 | case FBF_MOVE_WINDOW: |
---|
| 248 | { |
---|
| 249 | uint32_t wid = arg1; |
---|
| 250 | uint32_t l_zero = arg2; |
---|
| 251 | uint32_t p_zero = arg3; |
---|
| 252 | |
---|
[642] | 253 | // call relevant kernel function to move data |
---|
[657] | 254 | error = dev_fbf_move_window( wid, l_zero, p_zero ); |
---|
[642] | 255 | |
---|
| 256 | if( error ) |
---|
| 257 | { |
---|
| 258 | |
---|
| 259 | #if DEBUG_SYSCALLS_ERROR |
---|
[657] | 260 | printk("\n[ERROR] in %s : cannot move window / thread[%x,%x]\n", |
---|
| 261 | __FUNCTION__ , dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
[642] | 262 | #endif |
---|
| 263 | this->errno = EINVAL; |
---|
| 264 | } |
---|
| 265 | break; |
---|
| 266 | } |
---|
[657] | 267 | /////////////////////// |
---|
| 268 | case FBF_RESIZE_WINDOW: |
---|
| 269 | { |
---|
| 270 | uint32_t wid = arg1; |
---|
| 271 | uint32_t width = arg2; |
---|
| 272 | uint32_t height = arg3; |
---|
| 273 | |
---|
| 274 | // call relevant kernel function to move data |
---|
| 275 | error = dev_fbf_resize_window( wid , width , height ); |
---|
| 276 | |
---|
| 277 | if( error ) |
---|
| 278 | { |
---|
| 279 | |
---|
| 280 | #if DEBUG_SYSCALLS_ERROR |
---|
| 281 | printk("\n[ERROR] in %s : cannot move window / thread[%x,%x]\n", |
---|
| 282 | __FUNCTION__ , dev_fbf_cmd_str(operation), process->pid, this->trdid ); |
---|
| 283 | #endif |
---|
| 284 | this->errno = EINVAL; |
---|
| 285 | } |
---|
| 286 | break; |
---|
| 287 | } |
---|
[642] | 288 | /////// |
---|
| 289 | default: // undefined operation |
---|
| 290 | { |
---|
| 291 | |
---|
| 292 | #if DEBUG_SYSCALLS_ERROR |
---|
| 293 | printk("\n[ERROR] in %s : undefined operation type %d / thread %x in process %x / cycle %d\n", |
---|
| 294 | __FUNCTION__ , operation, this->trdid, process->pid, (uint32_t)hal_get_cycles() ); |
---|
| 295 | #endif |
---|
| 296 | this->errno = EINVAL; |
---|
[657] | 297 | error = -1; |
---|
[642] | 298 | } |
---|
[657] | 299 | break; |
---|
[642] | 300 | } // end switch on operation |
---|
| 301 | |
---|
| 302 | hal_fence(); |
---|
| 303 | |
---|
| 304 | #if (DEBUG_SYS_FBF || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 305 | uint64_t tm_end = hal_get_cycles(); |
---|
| 306 | #endif |
---|
| 307 | |
---|
| 308 | #if DEBUG_SYS_FBF |
---|
| 309 | if( DEBUG_SYS_FBF < tm_end ) |
---|
[657] | 310 | printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n", |
---|
| 311 | __FUNCTION__, process->pid, this->trdid, dev_fbf_cmd_str( operation ), (uint32_t)tm_end ); |
---|
[642] | 312 | #endif |
---|
| 313 | |
---|
| 314 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 315 | hal_atomic_add( &syscalls_cumul_cost[SYS_FBF] , tm_end - tm_start ); |
---|
| 316 | hal_atomic_add( &syscalls_occurences[SYS_FBF] , 1 ); |
---|
| 317 | #endif |
---|
| 318 | |
---|
[657] | 319 | return error; |
---|
[642] | 320 | |
---|
| 321 | } // end sys_fbf() |
---|