[1] | 1 | /* |
---|
| 2 | * hal_special.c - implementation of Generic Special Register Access API for TSAR-MIPS32 |
---|
| 3 | * |
---|
[101] | 4 | * Author Alain Greiner (2016,2017) |
---|
[1] | 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 | |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_special.h> |
---|
[619] | 27 | #include <hal_exception.h> |
---|
[101] | 28 | #include <core.h> |
---|
| 29 | #include <thread.h> |
---|
[1] | 30 | |
---|
| 31 | /**** Forward declarations ****/ |
---|
| 32 | |
---|
| 33 | struct thread_s; |
---|
| 34 | |
---|
[570] | 35 | //////////////////////////////// |
---|
[481] | 36 | inline gid_t hal_get_gid( void ) |
---|
[1] | 37 | { |
---|
| 38 | uint32_t proc_id; |
---|
| 39 | |
---|
| 40 | asm volatile ("mfc0 %0, $15, 1" : "=&r" (proc_id)); |
---|
| 41 | |
---|
[16] | 42 | return (proc_id & 0x3FF); // 4/4/2 format for TSAR |
---|
[1] | 43 | } |
---|
| 44 | |
---|
[570] | 45 | /////////////////////////////////// |
---|
[481] | 46 | inline reg_t hal_time_stamp( void ) |
---|
[121] | 47 | { |
---|
[296] | 48 | reg_t count; |
---|
[121] | 49 | |
---|
[279] | 50 | asm volatile ("mfc0 %0, $9" : "=&r" (count)); |
---|
[121] | 51 | |
---|
| 52 | return count; |
---|
| 53 | } |
---|
| 54 | |
---|
[570] | 55 | /////////////////////////////// |
---|
[481] | 56 | inline reg_t hal_get_sr( void ) |
---|
[279] | 57 | { |
---|
[296] | 58 | reg_t sr; |
---|
[279] | 59 | |
---|
| 60 | asm volatile ("mfc0 %0, $12" : "=&r" (sr)); |
---|
| 61 | |
---|
| 62 | return sr; |
---|
| 63 | } |
---|
| 64 | |
---|
[570] | 65 | /////////////////////////////// |
---|
[481] | 66 | uint64_t hal_get_cycles( void ) |
---|
[1] | 67 | { |
---|
[95] | 68 | uint64_t cycles; // absolute time to be returned |
---|
| 69 | uint32_t last_count; // last registered cycles count |
---|
| 70 | uint32_t current_count; // current cycles count |
---|
| 71 | uint32_t elapsed; |
---|
| 72 | |
---|
| 73 | core_t * core = CURRENT_THREAD->core; |
---|
| 74 | |
---|
| 75 | // get last registered time stamp |
---|
| 76 | last_count = core->time_stamp; |
---|
| 77 | |
---|
| 78 | // get current time stamp from hardware register |
---|
[121] | 79 | current_count = hal_time_stamp(); |
---|
[95] | 80 | |
---|
| 81 | // compute number of elapsed cycles, taking into account 32 bits register wrap |
---|
| 82 | if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count; |
---|
| 83 | else elapsed = current_count - last_count; |
---|
| 84 | |
---|
| 85 | // compute absolute time |
---|
| 86 | cycles = core->cycles + elapsed; |
---|
| 87 | |
---|
| 88 | // update core time |
---|
| 89 | core->time_stamp = current_count; |
---|
| 90 | core->cycles = cycles; |
---|
| 91 | |
---|
[124] | 92 | hal_fence(); |
---|
[95] | 93 | |
---|
[1] | 94 | return cycles; |
---|
| 95 | } |
---|
| 96 | |
---|
[570] | 97 | /////////////////////////////////////////////////////// |
---|
[481] | 98 | inline struct thread_s * hal_get_current_thread( void ) |
---|
[1] | 99 | { |
---|
| 100 | void * thread_ptr; |
---|
| 101 | |
---|
[279] | 102 | asm volatile ("mfc0 %0, $4, 2" : "=&r" (thread_ptr)); |
---|
[1] | 103 | |
---|
| 104 | return thread_ptr; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /////////////////////////////////////////////////////// |
---|
| 108 | void hal_set_current_thread( struct thread_s * thread ) |
---|
| 109 | { |
---|
[279] | 110 | asm volatile ("mtc0 %0, $4, 2" : : "r" (thread)); |
---|
[1] | 111 | } |
---|
| 112 | |
---|
[570] | 113 | /////////////////////////// |
---|
[481] | 114 | void hal_fpu_enable( void ) |
---|
[1] | 115 | { |
---|
[459] | 116 | // set CU1 bit (FPU enable) in c0_sr |
---|
[1] | 117 | asm volatile |
---|
| 118 | ( ".set noat \n" |
---|
| 119 | "lui $27, 0x2000 \n" |
---|
| 120 | "mfc0 $1, $12 \n" |
---|
| 121 | "or $27, $1, $27 \n" |
---|
| 122 | "mtc0 $27, $12 \n" |
---|
| 123 | ".set at \n" ); |
---|
[459] | 124 | |
---|
| 125 | // set CU1 bit in calling thread UZONE |
---|
| 126 | uint32_t * uzone = CURRENT_THREAD->uzone_current; |
---|
| 127 | uzone[34] |= 0x20000000; |
---|
[1] | 128 | } |
---|
| 129 | |
---|
[570] | 130 | //////////////////////////// |
---|
[481] | 131 | void hal_fpu_disable( void ) |
---|
[1] | 132 | { |
---|
[459] | 133 | // reset CU1 bit (FPU enable) in c0_sr |
---|
[1] | 134 | asm volatile |
---|
| 135 | ( ".set noat \n" |
---|
| 136 | "lui $27, 0xDFFF \n" |
---|
| 137 | "ori $27, $27, 0xFFFF \n" |
---|
| 138 | "mfc0 $1, $12 \n" |
---|
| 139 | "and $27, $1, $27 \n" |
---|
| 140 | "mtc0 $27, $12 \n" |
---|
| 141 | ".set at \n"); |
---|
[459] | 142 | |
---|
| 143 | // reset CU1 bit in calling thread UZONE |
---|
| 144 | uint32_t * uzone = CURRENT_THREAD->uzone_current; |
---|
| 145 | uzone[34] &= 0xDFFFFFFF; |
---|
[1] | 146 | } |
---|
| 147 | |
---|
[619] | 148 | /////////////////////////// |
---|
| 149 | uint32_t hal_get_sp( void ) |
---|
[1] | 150 | { |
---|
| 151 | register uint32_t sp; |
---|
| 152 | |
---|
[279] | 153 | asm volatile ("or %0, $0, $29" : "=&r" (sp)); |
---|
[1] | 154 | |
---|
| 155 | return sp; |
---|
| 156 | } |
---|
| 157 | |
---|
[619] | 158 | ///////////////////////////////////// |
---|
| 159 | uint32_t hal_set_sp( void * new_val ) |
---|
[1] | 160 | { |
---|
| 161 | register uint32_t sp; |
---|
| 162 | |
---|
| 163 | asm volatile |
---|
| 164 | ( "or %0, $0, $29 \n" |
---|
| 165 | "or $29, $0, %1 \n" |
---|
| 166 | : "=&r" (sp) : "r" (new_val) ); |
---|
| 167 | |
---|
| 168 | return sp; |
---|
| 169 | } |
---|
| 170 | |
---|
[619] | 171 | /////////////////////////// |
---|
| 172 | uint32_t hal_get_ra( void ) |
---|
| 173 | { |
---|
| 174 | register uint32_t ra; |
---|
| 175 | |
---|
| 176 | asm volatile ("or %0, $0, $31" : "=&r" (ra)); |
---|
| 177 | |
---|
| 178 | return ra; |
---|
| 179 | } |
---|
| 180 | |
---|
[570] | 181 | ////////////////////////////////// |
---|
[481] | 182 | uint32_t hal_get_bad_vaddr( void ) |
---|
[1] | 183 | { |
---|
| 184 | register uint32_t bad_va; |
---|
| 185 | |
---|
| 186 | asm volatile |
---|
| 187 | ( "mfc0 %0, $8 \n" |
---|
| 188 | : "=&r" (bad_va) ); |
---|
| 189 | |
---|
| 190 | return bad_va; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | //////////////////////////////////////////// |
---|
| 194 | uint32_t hal_uncached_read( uint32_t * ptr ) |
---|
| 195 | { |
---|
| 196 | register uint32_t val; |
---|
| 197 | |
---|
| 198 | asm volatile |
---|
| 199 | ( "ll %0, (%1) \n" |
---|
| 200 | : "=&r"(val) : "r" (ptr) ); |
---|
| 201 | |
---|
| 202 | return val; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | ////////////////////////////////////////// |
---|
| 206 | void hal_invalid_dcache_line( void * ptr ) |
---|
| 207 | { |
---|
| 208 | asm volatile |
---|
| 209 | ( "cache %0, (%1) \n" |
---|
| 210 | "sync \n" |
---|
| 211 | : : "i" (0x11) , "r" (ptr) ); |
---|
| 212 | } |
---|
| 213 | |
---|
[570] | 214 | ///////////////////////////// |
---|
| 215 | inline void hal_fence( void ) |
---|
[1] | 216 | { |
---|
[407] | 217 | asm volatile ("sync"); |
---|
[1] | 218 | } |
---|
| 219 | |
---|
[570] | 220 | ///////////////////////////// |
---|
| 221 | inline void hal_rdbar( void ) |
---|
[1] | 222 | { |
---|
| 223 | asm volatile( "" ::: "memory" ); |
---|
| 224 | } |
---|
| 225 | |
---|
[570] | 226 | /////////////////////////// |
---|
[481] | 227 | void hal_core_sleep( void ) |
---|
[1] | 228 | { |
---|
[619] | 229 | thread_t * this = CURRENT_THREAD; |
---|
| 230 | |
---|
| 231 | printk("\n*** thread[%x,%x] on core[%x,%d]/n" |
---|
| 232 | " sr = %X / sp = %X / ra = %X\n", |
---|
| 233 | this->process->pid, this->trdid, local_cxy, this->core->lid, |
---|
| 234 | hal_get_sr(), hal_get_sp(), hal_get_ra() ); |
---|
| 235 | |
---|
[425] | 236 | while( 1 ) asm volatile ("nop"); |
---|
[1] | 237 | } |
---|
| 238 | |
---|
| 239 | ////////////////////////////////////// |
---|
| 240 | void hal_fixed_delay( uint32_t delay ) |
---|
| 241 | { |
---|
| 242 | asm volatile |
---|
[407] | 243 | ( ".set noreorder \n" |
---|
[1] | 244 | "or $27, %0, $0 \n" |
---|
[407] | 245 | "1: \n" |
---|
[1] | 246 | "addi $27, $27, -1 \n" |
---|
[407] | 247 | "nop \n" |
---|
[1] | 248 | "bne $27, $0, 1b \n" |
---|
| 249 | "nop \n" |
---|
[407] | 250 | ".set reorder \n" |
---|
| 251 | : : "r" (delay>>2) : "$27" ); |
---|
[1] | 252 | } |
---|
| 253 | |
---|
[16] | 254 | ////////////////////////////////////////////////// |
---|
| 255 | void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code, |
---|
| 256 | intptr_t * mmu_ins_bad_vaddr, |
---|
| 257 | intptr_t * mmu_dat_excp_code, |
---|
| 258 | intptr_t * mmu_dat_bad_vaddr ) |
---|
| 259 | { |
---|
| 260 | asm volatile |
---|
| 261 | ( "mfc2 %0, $11 \n" |
---|
| 262 | "mfc2 %1, $13 \n" |
---|
| 263 | "mfc2 %2, $12 \n" |
---|
| 264 | "mfc2 %3, $14 \n" |
---|
[406] | 265 | : "=&r"(*mmu_ins_excp_code), |
---|
| 266 | "=&r"(*mmu_ins_bad_vaddr), |
---|
| 267 | "=&r"(*mmu_dat_excp_code), |
---|
| 268 | "=&r"(*mmu_dat_bad_vaddr) ); |
---|
[16] | 269 | } |
---|
[406] | 270 | |
---|