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