[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 | |
---|
| 25 | #include <hal_types.h> |
---|
| 26 | #include <hal_special.h> |
---|
[101] | 27 | #include <core.h> |
---|
| 28 | #include <thread.h> |
---|
[1] | 29 | |
---|
| 30 | /**** Forward declarations ****/ |
---|
| 31 | |
---|
| 32 | struct thread_s; |
---|
| 33 | |
---|
[101] | 34 | |
---|
[121] | 35 | ////////////////////////// |
---|
| 36 | inline gid_t hal_get_gid() |
---|
[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 | |
---|
[121] | 45 | //////////////////////////////// |
---|
| 46 | inline cycle_t hal_time_stamp() |
---|
| 47 | { |
---|
| 48 | cycle_t count; |
---|
| 49 | |
---|
| 50 | asm volatile ("mfc0 %0, $9 " : "=&r" (count)); |
---|
| 51 | |
---|
| 52 | return count; |
---|
| 53 | } |
---|
| 54 | |
---|
[1] | 55 | ///////////////////////// |
---|
[95] | 56 | uint64_t hal_get_cycles() |
---|
[1] | 57 | { |
---|
[95] | 58 | uint64_t cycles; // absolute time to be returned |
---|
| 59 | uint32_t last_count; // last registered cycles count |
---|
| 60 | uint32_t current_count; // current cycles count |
---|
| 61 | uint32_t elapsed; |
---|
| 62 | |
---|
| 63 | core_t * core = CURRENT_THREAD->core; |
---|
| 64 | |
---|
| 65 | // get last registered time stamp |
---|
| 66 | last_count = core->time_stamp; |
---|
| 67 | |
---|
| 68 | // get current time stamp from hardware register |
---|
[121] | 69 | current_count = hal_time_stamp(); |
---|
[95] | 70 | |
---|
| 71 | // compute number of elapsed cycles, taking into account 32 bits register wrap |
---|
| 72 | if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count; |
---|
| 73 | else elapsed = current_count - last_count; |
---|
| 74 | |
---|
| 75 | // compute absolute time |
---|
| 76 | cycles = core->cycles + elapsed; |
---|
| 77 | |
---|
| 78 | // update core time |
---|
| 79 | core->time_stamp = current_count; |
---|
| 80 | core->cycles = cycles; |
---|
| 81 | |
---|
[124] | 82 | hal_fence(); |
---|
[95] | 83 | |
---|
[1] | 84 | return cycles; |
---|
| 85 | } |
---|
| 86 | |
---|
[121] | 87 | ///////////////////////////////////////////////// |
---|
| 88 | inline struct thread_s * hal_get_current_thread() |
---|
[1] | 89 | { |
---|
| 90 | void * thread_ptr; |
---|
| 91 | |
---|
| 92 | asm volatile |
---|
| 93 | ( "mfc0 %0, $4, 2 \n" |
---|
| 94 | : "=&r" (thread_ptr) ); |
---|
| 95 | |
---|
| 96 | return thread_ptr; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /////////////////////////////////////////////////////// |
---|
| 100 | void hal_set_current_thread( struct thread_s * thread ) |
---|
| 101 | { |
---|
| 102 | asm volatile |
---|
| 103 | ( "mtc0 %0, $4, 2 \n" |
---|
| 104 | : : "r" (thread) ); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | ///////////////////// |
---|
| 108 | void hal_fpu_enable() |
---|
| 109 | { |
---|
| 110 | asm volatile |
---|
| 111 | ( ".set noat \n" |
---|
| 112 | "lui $27, 0x2000 \n" |
---|
| 113 | "mfc0 $1, $12 \n" |
---|
| 114 | "or $27, $1, $27 \n" |
---|
| 115 | "mtc0 $27, $12 \n" |
---|
| 116 | ".set at \n" ); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | ////////////////////// |
---|
| 120 | void hal_fpu_disable() |
---|
| 121 | { |
---|
| 122 | asm volatile |
---|
| 123 | ( ".set noat \n" |
---|
| 124 | "lui $27, 0xDFFF \n" |
---|
| 125 | "ori $27, $27, 0xFFFF \n" |
---|
| 126 | "mfc0 $1, $12 \n" |
---|
| 127 | "and $27, $1, $27 \n" |
---|
| 128 | "mtc0 $27, $12 \n" |
---|
| 129 | ".set at \n"); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | //////////////////////// |
---|
| 133 | uint32_t hal_get_stack() |
---|
| 134 | { |
---|
| 135 | register uint32_t sp; |
---|
| 136 | |
---|
| 137 | asm volatile |
---|
| 138 | ( "or %0, $0, $29 \n" |
---|
| 139 | : "=&r" (sp) ); |
---|
| 140 | |
---|
| 141 | return sp; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | //////////////////////////////////////// |
---|
| 145 | uint32_t hal_set_stack( void * new_val ) |
---|
| 146 | { |
---|
| 147 | register uint32_t sp; |
---|
| 148 | |
---|
| 149 | asm volatile |
---|
| 150 | ( "or %0, $0, $29 \n" |
---|
| 151 | "or $29, $0, %1 \n" |
---|
| 152 | : "=&r" (sp) : "r" (new_val) ); |
---|
| 153 | |
---|
| 154 | return sp; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | //////////////////////////// |
---|
| 158 | uint32_t hal_get_bad_vaddr() |
---|
| 159 | { |
---|
| 160 | register uint32_t bad_va; |
---|
| 161 | |
---|
| 162 | asm volatile |
---|
| 163 | ( "mfc0 %0, $8 \n" |
---|
| 164 | : "=&r" (bad_va) ); |
---|
| 165 | |
---|
| 166 | return bad_va; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | //////////////////////////////////////////// |
---|
| 170 | uint32_t hal_uncached_read( uint32_t * ptr ) |
---|
| 171 | { |
---|
| 172 | register uint32_t val; |
---|
| 173 | |
---|
| 174 | asm volatile |
---|
| 175 | ( "ll %0, (%1) \n" |
---|
| 176 | : "=&r"(val) : "r" (ptr) ); |
---|
| 177 | |
---|
| 178 | return val; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | ////////////////////////////////////////// |
---|
| 182 | void hal_invalid_dcache_line( void * ptr ) |
---|
| 183 | { |
---|
| 184 | asm volatile |
---|
| 185 | ( "cache %0, (%1) \n" |
---|
| 186 | "sync \n" |
---|
| 187 | : : "i" (0x11) , "r" (ptr) ); |
---|
| 188 | } |
---|
| 189 | |
---|
[124] | 190 | //////////////// |
---|
| 191 | void hal_fence() |
---|
[1] | 192 | { |
---|
| 193 | asm volatile |
---|
| 194 | ( "sync \n":: ); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | //////////////// |
---|
| 198 | void hal_rdbar() |
---|
| 199 | { |
---|
| 200 | asm volatile( "" ::: "memory" ); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | ///////////////////// |
---|
| 204 | void hal_core_sleep() |
---|
| 205 | { |
---|
| 206 | asm volatile |
---|
| 207 | ("wait \n"::); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | ////////////////////////////////////// |
---|
| 211 | void hal_fixed_delay( uint32_t delay ) |
---|
| 212 | { |
---|
| 213 | asm volatile |
---|
| 214 | ( "1: \n" |
---|
| 215 | "or $27, %0, $0 \n" |
---|
| 216 | "addi $27, $27, -1 \n" |
---|
| 217 | "bne $27, $0, 1b \n" |
---|
| 218 | "nop \n" |
---|
| 219 | : : "r" (delay) : "$27" ); |
---|
| 220 | } |
---|
| 221 | |
---|
[16] | 222 | ////////////////////////////////////////////////// |
---|
| 223 | void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code, |
---|
| 224 | intptr_t * mmu_ins_bad_vaddr, |
---|
| 225 | intptr_t * mmu_dat_excp_code, |
---|
| 226 | intptr_t * mmu_dat_bad_vaddr ) |
---|
| 227 | { |
---|
| 228 | asm volatile |
---|
| 229 | ( "mfc2 %0, $11 \n" |
---|
| 230 | "mfc2 %1, $13 \n" |
---|
| 231 | "mfc2 %2, $12 \n" |
---|
| 232 | "mfc2 %3, $14 \n" |
---|
| 233 | : "=&r"(mmu_ins_excp_code), |
---|
| 234 | "=&r"(mmu_ins_bad_vaddr), |
---|
| 235 | "=&r"(mmu_dat_excp_code), |
---|
| 236 | "=&r"(mmu_dat_bad_vaddr) ); |
---|
| 237 | } |
---|