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