1 | /* |
---|
2 | * hal_remote.c - implementation of Generic Remote Access API for TSAR-MIPS32 |
---|
3 | * |
---|
4 | * Authors : Mohammed Karaoui (2015) |
---|
5 | * Alain Greiner (2016) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH.. |
---|
10 | * |
---|
11 | * ALMOS-MKH. is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH. is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include <hal_types.h> |
---|
26 | #include <hal_irqmask.h> |
---|
27 | |
---|
28 | //////////////////////////////// |
---|
29 | void hal_remote_sb( xptr_t xp, |
---|
30 | uint8_t data ) |
---|
31 | { |
---|
32 | uint32_t save_sr; |
---|
33 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
34 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
35 | |
---|
36 | hal_disable_irq( &save_sr ); |
---|
37 | |
---|
38 | asm volatile( |
---|
39 | ".set noreorder \n" |
---|
40 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
41 | "mtc2 %2, $24 \n" /* PADDR_EXT <= cxy */ |
---|
42 | "sb %0, 0(%1) \n" /* *paddr <= value */ |
---|
43 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
44 | "sync \n" |
---|
45 | ".set reorder \n" |
---|
46 | : : "r" (data), "r" (ptr), "r" (cxy) : "$15" ); |
---|
47 | |
---|
48 | hal_restore_irq( save_sr ); |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | ///////////////////////////////// |
---|
53 | void hal_remote_sw( xptr_t xp, |
---|
54 | uint32_t data ) |
---|
55 | { |
---|
56 | uint32_t save_sr; |
---|
57 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
58 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
59 | |
---|
60 | hal_disable_irq( &save_sr ); |
---|
61 | |
---|
62 | asm volatile( "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
63 | ".set noreorder \n" |
---|
64 | "mtc2 %2, $24 \n" /* PADDR_EXT <= cxy */ |
---|
65 | "sw %0, 0(%1) \n" /* *paddr <= value */ |
---|
66 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
67 | "sync \n" |
---|
68 | ".set reorder \n" |
---|
69 | : : "r" (data), "r" (ptr), "r" (cxy) : "$15" ); |
---|
70 | |
---|
71 | hal_restore_irq( save_sr ); |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | ////////////////////////////////// |
---|
76 | void hal_remote_swd( xptr_t xp, |
---|
77 | uint64_t data ) |
---|
78 | { |
---|
79 | uint32_t save_sr; |
---|
80 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
81 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
82 | |
---|
83 | uint32_t data_lsb = (uint32_t)data; |
---|
84 | uint32_t data_msb = (uint32_t)(data>>32); |
---|
85 | |
---|
86 | hal_disable_irq( &save_sr ); |
---|
87 | |
---|
88 | asm volatile( |
---|
89 | ".set noreorder \n" |
---|
90 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
91 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
92 | "sw %0, 0(%2) \n" /* *paddr <= lsb */ |
---|
93 | "sw %1, 4(%2) \n" /* *(paddr+4) <= msb */ |
---|
94 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
95 | "sync \n" |
---|
96 | ".set reorder \n" |
---|
97 | : : "r" (data_lsb), "r" (data_msb), "r" (ptr), "r" (cxy) : "$15" ); |
---|
98 | |
---|
99 | hal_restore_irq( save_sr ); |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | //////////////////////////////////// |
---|
104 | void hal_remote_spt( xptr_t xp, |
---|
105 | void * pt ) |
---|
106 | { |
---|
107 | hal_remote_sw ( xp , (uint32_t)pt ); |
---|
108 | } |
---|
109 | |
---|
110 | //////////////////////////////// |
---|
111 | uint8_t hal_remote_lb( xptr_t xp ) |
---|
112 | { |
---|
113 | uint32_t save_sr; |
---|
114 | char data; |
---|
115 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
116 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
117 | |
---|
118 | hal_disable_irq( &save_sr ); |
---|
119 | |
---|
120 | asm volatile( |
---|
121 | ".set noreorder \n" |
---|
122 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
123 | "mtc2 %2, $24 \n" /* PADDR_EXT <= cxy */ |
---|
124 | "lb %0, 0(%1) \n" /* data <= *paddr */ |
---|
125 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
126 | ".set reorder \n" |
---|
127 | : "=r" (data) : "r" (ptr), "r" (cxy) : "$15" ); |
---|
128 | |
---|
129 | hal_restore_irq( save_sr ); |
---|
130 | |
---|
131 | return ( data ); |
---|
132 | } |
---|
133 | |
---|
134 | //////////////////////////////////// |
---|
135 | uint32_t hal_remote_lw( xptr_t xp ) |
---|
136 | { |
---|
137 | uint32_t save_sr; |
---|
138 | uint32_t data; |
---|
139 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
140 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
141 | |
---|
142 | hal_disable_irq( &save_sr ); |
---|
143 | |
---|
144 | asm volatile( |
---|
145 | ".set noreorder \n" |
---|
146 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
147 | "mtc2 %2, $24 \n" /* PADDR_EXT <= cxy */ |
---|
148 | "lw %0, 0(%1) \n" /* data <= *paddr */ |
---|
149 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
150 | ".set reorder \n" |
---|
151 | : "=r" (data) : "r" (ptr), "r" (cxy) : "$15" ); |
---|
152 | |
---|
153 | hal_restore_irq( save_sr ); |
---|
154 | |
---|
155 | return ( data ); |
---|
156 | } |
---|
157 | |
---|
158 | ///////////////////////////////////// |
---|
159 | uint64_t hal_remote_lwd( xptr_t xp ) |
---|
160 | { |
---|
161 | uint32_t save_sr; |
---|
162 | uint32_t data_lsb; |
---|
163 | uint32_t data_msb; |
---|
164 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
165 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
166 | |
---|
167 | hal_disable_irq( &save_sr ); |
---|
168 | |
---|
169 | asm volatile( |
---|
170 | ".set noreorder \n" |
---|
171 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
172 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
173 | "lw %0, 0(%2) \n" /* data_lsb <= *paddr */ |
---|
174 | "lw %1, 4(%2) \n" /* data_msb <= *paddr+4 */ |
---|
175 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
176 | ".set reorder \n" |
---|
177 | : "=r" (data_lsb), "=r"(data_msb) : "r" (ptr), "r" (cxy) : "$15" ); |
---|
178 | |
---|
179 | hal_restore_irq( save_sr ); |
---|
180 | |
---|
181 | return ( (((uint64_t)data_msb)<<32) + (((uint64_t)data_lsb)) ); |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | //////////////////////////////////// |
---|
186 | void * hal_remote_lpt( xptr_t xp ) |
---|
187 | { |
---|
188 | return (void *)hal_remote_lw ( xp ); |
---|
189 | } |
---|
190 | |
---|
191 | /////////////////////////////////////////// |
---|
192 | bool_t hal_remote_atomic_cas( xptr_t xp, |
---|
193 | uint32_t old, |
---|
194 | uint32_t new ) |
---|
195 | { |
---|
196 | uint32_t save_sr; |
---|
197 | bool_t isAtomic; |
---|
198 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
199 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
200 | |
---|
201 | hal_disable_irq( &save_sr ); |
---|
202 | |
---|
203 | asm volatile( |
---|
204 | ".set noreorder \n" |
---|
205 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
206 | "mtc2 %4, $24 \n" /* PADDR_EXT <= cxy */ |
---|
207 | "or $8, $0, %3 \n" /* $8 <= new */ |
---|
208 | "ll $3, 0(%1) \n" /* $3 <= *paddr */ |
---|
209 | "bne $3, %2, 1f \n" /* if ($3 != old) */ |
---|
210 | "li $7, 0 \n" /* $7 <= 0 */ |
---|
211 | "sc $8, (%1) \n" /* *paddr <= new */ |
---|
212 | "or $7, $8, $0 \n" /* $7 <= atomic */ |
---|
213 | "sync \n" |
---|
214 | "1: \n" |
---|
215 | "or %0, $7, $0 \n" /* isAtomic <= $7 */ |
---|
216 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
217 | ".set reorder \n" |
---|
218 | : "=&r" (isAtomic) : "r" (ptr), "r" (old) , "r" (new), "r" (cxy) |
---|
219 | : "$3", "$7", "$8", "$15" ); |
---|
220 | |
---|
221 | hal_restore_irq( save_sr ); |
---|
222 | |
---|
223 | return isAtomic; |
---|
224 | |
---|
225 | } // end hal_remote_atomic_cas() |
---|
226 | |
---|
227 | //////////////////////////////////////////// |
---|
228 | uint32_t hal_remote_atomic_add( xptr_t xp, |
---|
229 | uint32_t incr ) |
---|
230 | { |
---|
231 | uint32_t save_sr; |
---|
232 | uint32_t current; |
---|
233 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
234 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
235 | |
---|
236 | hal_disable_irq( &save_sr ); |
---|
237 | |
---|
238 | asm volatile( |
---|
239 | ".set noreorder \n" |
---|
240 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
241 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
242 | "1: \n" |
---|
243 | "ll %0, (%1) \n" /* current <= *paddr */ |
---|
244 | "addu $3, %0, %2 \n" /* $3 <= current + incr */ |
---|
245 | "sc $3, (%1) \n" /* *paddr <= $3 */ |
---|
246 | "beq $3, $0, 1b \n" /* retry if failure */ |
---|
247 | "nop \n" |
---|
248 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
249 | ".set reorder \n" |
---|
250 | : "=&r" (current) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" ); |
---|
251 | |
---|
252 | hal_restore_irq( save_sr ); |
---|
253 | |
---|
254 | return current; |
---|
255 | |
---|
256 | } // end hal_remote_atomic_add() |
---|
257 | |
---|
258 | //////////////////////////////////////////// |
---|
259 | uint32_t hal_remote_atomic_and( xptr_t xp, |
---|
260 | uint32_t mask ) |
---|
261 | { |
---|
262 | uint32_t save_sr; |
---|
263 | uint32_t current; |
---|
264 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
265 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
266 | |
---|
267 | hal_disable_irq( &save_sr ); |
---|
268 | |
---|
269 | asm volatile( |
---|
270 | ".set noreorder \n" |
---|
271 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
272 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
273 | "1: \n" |
---|
274 | "ll %0, (%1) \n" /* current <= *paddr */ |
---|
275 | "and $3, %0, %2 \n" /* $3 <= current & mask */ |
---|
276 | "sc $3, (%1) \n" /* *paddr <= $3 */ |
---|
277 | "beq $3, $0, 1b \n" /* retry if failure */ |
---|
278 | "nop \n" |
---|
279 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
280 | ".set reorder \n" |
---|
281 | : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" ); |
---|
282 | |
---|
283 | hal_restore_irq( save_sr ); |
---|
284 | |
---|
285 | return current; |
---|
286 | |
---|
287 | } // end hal_remote_atomic_and() |
---|
288 | |
---|
289 | //////////////////////////////////////////// |
---|
290 | uint32_t hal_remote_atomic_or( xptr_t xp, |
---|
291 | uint32_t mask ) |
---|
292 | { |
---|
293 | uint32_t save_sr; |
---|
294 | uint32_t current; |
---|
295 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
296 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
297 | |
---|
298 | hal_disable_irq( &save_sr ); |
---|
299 | |
---|
300 | asm volatile( |
---|
301 | ".set noreorder \n" |
---|
302 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
303 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
304 | "1: \n" |
---|
305 | "ll %0, (%1) \n" /* current <= *paddr */ |
---|
306 | "or $3, %0, %2 \n" /* $3 <= current | mask */ |
---|
307 | "sc $3, (%1) \n" /* *paddr <= $3 */ |
---|
308 | "beq $3, $0, 1b \n" /* retry if failure */ |
---|
309 | "nop \n" |
---|
310 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
311 | ".set reorder \n" |
---|
312 | : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" ); |
---|
313 | |
---|
314 | hal_restore_irq( save_sr ); |
---|
315 | |
---|
316 | return current; |
---|
317 | |
---|
318 | } // end hal_remote_atomic_or() |
---|
319 | |
---|
320 | ///////////////////////////////////////////////// |
---|
321 | error_t hal_remote_atomic_try_add( xptr_t xp, |
---|
322 | uint32_t incr, |
---|
323 | uint32_t * old ) |
---|
324 | { |
---|
325 | uint32_t save_sr; |
---|
326 | uint32_t current; |
---|
327 | error_t error; |
---|
328 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
329 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
330 | |
---|
331 | hal_disable_irq( &save_sr ); |
---|
332 | |
---|
333 | asm volatile( |
---|
334 | ".set noreorder \n" |
---|
335 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
336 | "mtc2 %4, $24 \n" /* PADDR_EXT <= cxy */ |
---|
337 | "ll %0, (%2) \n" /* current <= *paddr */ |
---|
338 | "addu $3, %0, %3 \n" /* $3 <= current + incr */ |
---|
339 | "sc $3, (%2) \n" /* *paddr <= $3 */ |
---|
340 | "beq $3, $0, 1f \n" /* exit if failure */ |
---|
341 | "ori %1, $0, 1 \n" /* fail: ret <= 1 */ |
---|
342 | "and %1, $0, $0 \n" /* success: ret <= 0 */ |
---|
343 | "1: \n" |
---|
344 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
345 | ".set reorder \n" |
---|
346 | : "=&r" (current), "=&r" (error) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" ); |
---|
347 | |
---|
348 | hal_restore_irq( save_sr ); |
---|
349 | |
---|
350 | *old = current; |
---|
351 | |
---|
352 | return error; |
---|
353 | |
---|
354 | } // end hal_remote_atomic_try_add() |
---|
355 | |
---|
356 | ///////////////////////////////////// |
---|
357 | void hal_remote_memcpy( xptr_t dst, |
---|
358 | xptr_t src, |
---|
359 | uint32_t size ) |
---|
360 | { |
---|
361 | uint32_t save_sr; |
---|
362 | uint32_t i; |
---|
363 | uint32_t wsize; |
---|
364 | uint32_t dptr = (uint32_t)GET_PTR( dst ); |
---|
365 | uint32_t dcxy = (uint32_t)GET_CXY( dst ); |
---|
366 | uint32_t sptr = (uint32_t)GET_PTR( src ); |
---|
367 | uint32_t scxy = (uint32_t)GET_CXY( src ); |
---|
368 | |
---|
369 | hal_disable_irq( &save_sr ); |
---|
370 | |
---|
371 | if( (dptr & 0x3) || (sptr & 0x3) ) wsize = 0; // do it all in bytes |
---|
372 | else wsize = size >> 2; |
---|
373 | |
---|
374 | for( i = 0 ; i < wsize ; i++ ) // transfer one word per iteration |
---|
375 | { |
---|
376 | asm volatile( |
---|
377 | ".set noreorder \n" |
---|
378 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
379 | "mtc2 %0, $24 \n" /* PADDR_EXT <= scxy */ |
---|
380 | "lw $3, 0(%1) \n" /* $3 <= *src */ |
---|
381 | "mtc2 %2, $24 \n" /* PADDR_EXT <= dcxy */ |
---|
382 | "sw $3, 0(%3) \n" /* *dst <= $3 */ |
---|
383 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
384 | ".set reorder \n" |
---|
385 | : : "r"(scxy), "r" (sptr+(i<<2)), "r"(dcxy), "r" (dptr+(i<<2)) : "$3", "$15" ); |
---|
386 | } |
---|
387 | |
---|
388 | for( i = wsize << 2 ; i < size ; i++ ) // transfer one byte per iteration |
---|
389 | { |
---|
390 | asm volatile( |
---|
391 | ".set noreorder \n" |
---|
392 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
393 | "mtc2 %0, $24 \n" /* PADDR_EXT <= scxy */ |
---|
394 | "lb $3, 0(%1) \n" /* $3 <= *src */ |
---|
395 | "mtc2 %2, $24 \n" /* PADDR_EXT <= dcxy */ |
---|
396 | "sb $3, 0(%3) \n" /* *dst <= $3 */ |
---|
397 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
398 | ".set reorder \n" |
---|
399 | : : "r"(scxy), "r" (sptr+i), "r"(dcxy), "r" (dptr+i) : "$3", "$15" ); |
---|
400 | } |
---|
401 | |
---|
402 | hal_restore_irq( save_sr ); |
---|
403 | |
---|
404 | } // end hal_remote_memcpy() |
---|
405 | |
---|
406 | /////////////////////////////////// |
---|
407 | void hal_remote_strcpy( xptr_t dst, |
---|
408 | xptr_t src ) |
---|
409 | { |
---|
410 | uint32_t save_sr; |
---|
411 | uint32_t dptr = (uint32_t)GET_PTR( dst ); |
---|
412 | uint32_t dcxy = (uint32_t)GET_CXY( dst ); |
---|
413 | uint32_t sptr = (uint32_t)GET_PTR( src ); |
---|
414 | uint32_t scxy = (uint32_t)GET_CXY( src ); |
---|
415 | |
---|
416 | hal_disable_irq( &save_sr ); |
---|
417 | |
---|
418 | // loop on characters while non NUL |
---|
419 | asm volatile( |
---|
420 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
421 | "1: \n" /* loop entry */ |
---|
422 | "mtc2 %0, $24 \n" /* PADDR_EXT <= scxy */ |
---|
423 | "lb $13, 0(%1) \n" /* read char from src string */ |
---|
424 | "mtc2 %2, $24 \n" /* PADDR_EXT <= dcxy */ |
---|
425 | "sb $13, 0(%3) \n" /* store char to dst string */ |
---|
426 | "addi %1, %1, 1 \n" /* increment sptr pointer */ |
---|
427 | "addi %3, %3, 1 \n" /* increment dptr pointer */ |
---|
428 | "bne $13, $0, 1b \n" /* test NUL */ |
---|
429 | "nop \n" |
---|
430 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
431 | : : "r"(scxy), "r"(sptr), "r"(dcxy), "r"(dptr) : "$13","$15", "memory" ); |
---|
432 | |
---|
433 | hal_restore_irq( save_sr ); |
---|
434 | |
---|
435 | } // end hal_remote_strcpy() |
---|