[1] | 1 | /* |
---|
| 2 | * hal_special.c - implementation of Generic Special Register Access API for TSAR-MIPS32 |
---|
| 3 | * |
---|
[686] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
[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 | |
---|
[623] | 35 | |
---|
| 36 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 37 | // Extern global variables |
---|
| 38 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 39 | |
---|
| 40 | extern cxy_t local_cxy; |
---|
| 41 | extern void hal_kentry_enter( void ); |
---|
| 42 | |
---|
[625] | 43 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 44 | // For the TSAR architecture, this function registers the address of the |
---|
| 45 | // hal_kentry_enter() function in the MIPS32 cp0_ebase register. |
---|
| 46 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 47 | void hal_set_kentry( void ) |
---|
| 48 | { |
---|
| 49 | uint32_t kentry = (uint32_t)(&hal_kentry_enter); |
---|
| 50 | |
---|
| 51 | asm volatile("mtc0 %0, $15, 1" : : "r" (kentry) ); |
---|
| 52 | } |
---|
| 53 | |
---|
[623] | 54 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 55 | // For the TSAR architecture, this function register the physical address of |
---|
| 56 | // the first level page table (PT1) in the PTPR register. |
---|
[686] | 57 | // It activates the intructions MMU, and de-activates the data MMU, that is NOT |
---|
| 58 | // used by the kernel for 32 bits architectures. |
---|
[623] | 59 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 60 | void hal_mmu_init( gpt_t * gpt ) |
---|
| 61 | { |
---|
[625] | 62 | // set PT1 base address in cp2_ptpr register |
---|
[623] | 63 | uint32_t ptpr = (((uint32_t)gpt->ptr) >> 13) | (local_cxy << 19); |
---|
| 64 | asm volatile ( "mtc2 %0, $0 \n" : : "r" (ptpr) ); |
---|
| 65 | |
---|
[625] | 66 | // set ITLB | ICACHE | DCACHE bits in cp2_mode register |
---|
[623] | 67 | asm volatile ( "ori $26, $0, 0xB \n" |
---|
| 68 | "mtc2 $26, $1 \n" ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | //////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 72 | // For the TSAR architecture, this function returns the current value |
---|
| 73 | // of the 32 bits c0_sr register |
---|
[623] | 74 | //////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 75 | inline reg_t hal_get_sr( void ) |
---|
[623] | 76 | { |
---|
[625] | 77 | reg_t sr; |
---|
[623] | 78 | |
---|
[625] | 79 | asm volatile ("mfc0 %0, $12" : "=&r" (sr)); |
---|
| 80 | |
---|
| 81 | return sr; |
---|
[623] | 82 | } |
---|
| 83 | |
---|
[625] | 84 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 85 | // For the TSAR architecture, this function returns the 10 LSB bits |
---|
| 86 | // of the 32 bits c0_ebase register : Y (4 bits) | Y (4 bits) | LID (2 bits) |
---|
| 87 | //////////////////////////////////////////////////////////////////////////////// |
---|
[481] | 88 | inline gid_t hal_get_gid( void ) |
---|
[1] | 89 | { |
---|
| 90 | uint32_t proc_id; |
---|
| 91 | |
---|
| 92 | asm volatile ("mfc0 %0, $15, 1" : "=&r" (proc_id)); |
---|
| 93 | |
---|
[16] | 94 | return (proc_id & 0x3FF); // 4/4/2 format for TSAR |
---|
[1] | 95 | } |
---|
| 96 | |
---|
[625] | 97 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 98 | // For the TSAR architecture, this function returns the current value |
---|
| 99 | // of the 32 bits c0_count cycle counter. |
---|
| 100 | //////////////////////////////////////////////////////////////////////////////// |
---|
[481] | 101 | inline reg_t hal_time_stamp( void ) |
---|
[121] | 102 | { |
---|
[296] | 103 | reg_t count; |
---|
[121] | 104 | |
---|
[279] | 105 | asm volatile ("mfc0 %0, $9" : "=&r" (count)); |
---|
[121] | 106 | |
---|
| 107 | return count; |
---|
| 108 | } |
---|
| 109 | |
---|
[570] | 110 | /////////////////////////////// |
---|
[481] | 111 | uint64_t hal_get_cycles( void ) |
---|
[1] | 112 | { |
---|
[95] | 113 | uint64_t cycles; // absolute time to be returned |
---|
| 114 | uint32_t last_count; // last registered cycles count |
---|
| 115 | uint32_t current_count; // current cycles count |
---|
| 116 | uint32_t elapsed; |
---|
| 117 | |
---|
| 118 | core_t * core = CURRENT_THREAD->core; |
---|
| 119 | |
---|
| 120 | // get last registered time stamp |
---|
| 121 | last_count = core->time_stamp; |
---|
| 122 | |
---|
| 123 | // get current time stamp from hardware register |
---|
[121] | 124 | current_count = hal_time_stamp(); |
---|
[95] | 125 | |
---|
| 126 | // compute number of elapsed cycles, taking into account 32 bits register wrap |
---|
| 127 | if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count; |
---|
| 128 | else elapsed = current_count - last_count; |
---|
| 129 | |
---|
| 130 | // compute absolute time |
---|
| 131 | cycles = core->cycles + elapsed; |
---|
| 132 | |
---|
| 133 | // update core time |
---|
| 134 | core->time_stamp = current_count; |
---|
| 135 | core->cycles = cycles; |
---|
| 136 | |
---|
[124] | 137 | hal_fence(); |
---|
[95] | 138 | |
---|
[1] | 139 | return cycles; |
---|
| 140 | } |
---|
| 141 | |
---|
[625] | 142 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 143 | // For the TSAR architecture, this function returns the current value |
---|
| 144 | // of the 32 bits c0_th register. |
---|
| 145 | //////////////////////////////////////////////////////////////////////////////// |
---|
[481] | 146 | inline struct thread_s * hal_get_current_thread( void ) |
---|
[1] | 147 | { |
---|
| 148 | void * thread_ptr; |
---|
| 149 | |
---|
[279] | 150 | asm volatile ("mfc0 %0, $4, 2" : "=&r" (thread_ptr)); |
---|
[1] | 151 | |
---|
| 152 | return thread_ptr; |
---|
| 153 | } |
---|
| 154 | |
---|
[625] | 155 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 156 | // For the TSAR architecture, this function set a new value |
---|
| 157 | // to the 32 bits c0_th register. |
---|
| 158 | //////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 159 | void hal_set_current_thread( struct thread_s * thread ) |
---|
| 160 | { |
---|
[279] | 161 | asm volatile ("mtc0 %0, $4, 2" : : "r" (thread)); |
---|
[1] | 162 | } |
---|
| 163 | |
---|
[570] | 164 | /////////////////////////// |
---|
[481] | 165 | void hal_fpu_enable( void ) |
---|
[1] | 166 | { |
---|
[459] | 167 | // set CU1 bit (FPU enable) in c0_sr |
---|
[1] | 168 | asm volatile |
---|
| 169 | ( ".set noat \n" |
---|
| 170 | "lui $27, 0x2000 \n" |
---|
| 171 | "mfc0 $1, $12 \n" |
---|
| 172 | "or $27, $1, $27 \n" |
---|
| 173 | "mtc0 $27, $12 \n" |
---|
| 174 | ".set at \n" ); |
---|
[459] | 175 | |
---|
| 176 | // set CU1 bit in calling thread UZONE |
---|
| 177 | uint32_t * uzone = CURRENT_THREAD->uzone_current; |
---|
| 178 | uzone[34] |= 0x20000000; |
---|
[1] | 179 | } |
---|
| 180 | |
---|
[570] | 181 | //////////////////////////// |
---|
[481] | 182 | void hal_fpu_disable( void ) |
---|
[1] | 183 | { |
---|
[459] | 184 | // reset CU1 bit (FPU enable) in c0_sr |
---|
[1] | 185 | asm volatile |
---|
| 186 | ( ".set noat \n" |
---|
| 187 | "lui $27, 0xDFFF \n" |
---|
| 188 | "ori $27, $27, 0xFFFF \n" |
---|
| 189 | "mfc0 $1, $12 \n" |
---|
| 190 | "and $27, $1, $27 \n" |
---|
| 191 | "mtc0 $27, $12 \n" |
---|
| 192 | ".set at \n"); |
---|
[459] | 193 | |
---|
| 194 | // reset CU1 bit in calling thread UZONE |
---|
| 195 | uint32_t * uzone = CURRENT_THREAD->uzone_current; |
---|
| 196 | uzone[34] &= 0xDFFFFFFF; |
---|
[1] | 197 | } |
---|
| 198 | |
---|
[625] | 199 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 200 | // For the TSAR architecture, this function returns the current value |
---|
| 201 | // of the 32 bits sp_29 register. |
---|
| 202 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 203 | reg_t hal_get_sp( void ) |
---|
[1] | 204 | { |
---|
| 205 | register uint32_t sp; |
---|
| 206 | |
---|
[279] | 207 | asm volatile ("or %0, $0, $29" : "=&r" (sp)); |
---|
[1] | 208 | |
---|
| 209 | return sp; |
---|
| 210 | } |
---|
| 211 | |
---|
[570] | 212 | ////////////////////////////////// |
---|
[481] | 213 | uint32_t hal_get_bad_vaddr( void ) |
---|
[1] | 214 | { |
---|
| 215 | register uint32_t bad_va; |
---|
| 216 | |
---|
| 217 | asm volatile |
---|
| 218 | ( "mfc0 %0, $8 \n" |
---|
| 219 | : "=&r" (bad_va) ); |
---|
| 220 | |
---|
| 221 | return bad_va; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | //////////////////////////////////////////// |
---|
| 225 | uint32_t hal_uncached_read( uint32_t * ptr ) |
---|
| 226 | { |
---|
| 227 | register uint32_t val; |
---|
| 228 | |
---|
| 229 | asm volatile |
---|
| 230 | ( "ll %0, (%1) \n" |
---|
| 231 | : "=&r"(val) : "r" (ptr) ); |
---|
| 232 | |
---|
| 233 | return val; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | ////////////////////////////////////////// |
---|
| 237 | void hal_invalid_dcache_line( void * ptr ) |
---|
| 238 | { |
---|
| 239 | asm volatile |
---|
| 240 | ( "cache %0, (%1) \n" |
---|
| 241 | "sync \n" |
---|
| 242 | : : "i" (0x11) , "r" (ptr) ); |
---|
| 243 | } |
---|
| 244 | |
---|
[570] | 245 | ///////////////////////////// |
---|
| 246 | inline void hal_fence( void ) |
---|
[1] | 247 | { |
---|
[407] | 248 | asm volatile ("sync"); |
---|
[1] | 249 | } |
---|
| 250 | |
---|
[570] | 251 | ///////////////////////////// |
---|
| 252 | inline void hal_rdbar( void ) |
---|
[1] | 253 | { |
---|
| 254 | asm volatile( "" ::: "memory" ); |
---|
| 255 | } |
---|
| 256 | |
---|
[570] | 257 | /////////////////////////// |
---|
[481] | 258 | void hal_core_sleep( void ) |
---|
[1] | 259 | { |
---|
[624] | 260 | while( 1 ) asm volatile ("wait"); |
---|
[1] | 261 | } |
---|
| 262 | |
---|
| 263 | ////////////////////////////////////// |
---|
| 264 | void hal_fixed_delay( uint32_t delay ) |
---|
| 265 | { |
---|
| 266 | asm volatile |
---|
[407] | 267 | ( ".set noreorder \n" |
---|
[1] | 268 | "or $27, %0, $0 \n" |
---|
[407] | 269 | "1: \n" |
---|
[1] | 270 | "addi $27, $27, -1 \n" |
---|
[407] | 271 | "nop \n" |
---|
[1] | 272 | "bne $27, $0, 1b \n" |
---|
| 273 | "nop \n" |
---|
[407] | 274 | ".set reorder \n" |
---|
| 275 | : : "r" (delay>>2) : "$27" ); |
---|
[1] | 276 | } |
---|
| 277 | |
---|
[16] | 278 | ////////////////////////////////////////////////// |
---|
| 279 | void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code, |
---|
| 280 | intptr_t * mmu_ins_bad_vaddr, |
---|
| 281 | intptr_t * mmu_dat_excp_code, |
---|
| 282 | intptr_t * mmu_dat_bad_vaddr ) |
---|
| 283 | { |
---|
| 284 | asm volatile |
---|
| 285 | ( "mfc2 %0, $11 \n" |
---|
| 286 | "mfc2 %1, $13 \n" |
---|
| 287 | "mfc2 %2, $12 \n" |
---|
| 288 | "mfc2 %3, $14 \n" |
---|
[406] | 289 | : "=&r"(*mmu_ins_excp_code), |
---|
| 290 | "=&r"(*mmu_ins_bad_vaddr), |
---|
| 291 | "=&r"(*mmu_dat_excp_code), |
---|
| 292 | "=&r"(*mmu_dat_bad_vaddr) ); |
---|
[16] | 293 | } |
---|
[406] | 294 | |
---|
[658] | 295 | |
---|