source: trunk/hal/tsar_mips32/core/hal_remote.c @ 110

Last change on this file since 110 was 95, checked in by alain, 7 years ago

hal_special: replace hal_time_stamp() by hal_get_cycles()
hal_remote : remove hal_remove_unc()

File size: 12.7 KB
Line 
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
27////////////////////////////////
28void hal_remote_sb( xptr_t   xp,
29                    uint8_t  data )
30{
31    uint32_t ptr = (uint32_t)GET_PTR( xp );
32    uint32_t cxy = (uint32_t)GET_CXY( xp );
33
34    asm volatile( 
35        ".set noreorder              \n"
36        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT */   
37        "mtc2   %2,     $24          \n"  /* PADDR_EXT <= cxy */   
38        "sb     %0,     0(%1)        \n"  /* *paddr <= value  */
39        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15 */   
40        "sync                        \n"
41        ".set reorder                \n"
42        : : "r" (data), "r" (ptr), "r" (cxy) : "$15" );
43}
44
45/////////////////////////////////
46void hal_remote_sw( xptr_t    xp,
47                    uint32_t  data )
48{
49    uint32_t ptr = (uint32_t)GET_PTR( xp );
50    uint32_t cxy = (uint32_t)GET_CXY( xp );
51
52    asm volatile( "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT */   
53        ".set noreorder              \n"
54        "mtc2   %2,     $24          \n"  /* PADDR_EXT <= cxy */   
55        "sw     %0,     0(%1)        \n"  /* *paddr <= value  */
56        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15 */   
57        "sync                        \n"
58        ".set reorder                \n"
59        : : "r" (data), "r" (ptr), "r" (cxy) : "$15" );
60}
61
62//////////////////////////////////
63void hal_remote_swd( xptr_t    xp,
64                     uint64_t  data )
65{
66    uint32_t ptr = (uint32_t)GET_PTR( xp );
67    uint32_t cxy = (uint32_t)GET_CXY( xp );
68
69    uint32_t data_lsb = (uint32_t)data;
70    uint32_t data_msb = (uint32_t)(data>>32);
71
72    asm volatile( 
73        ".set noreorder              \n"
74        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT  */   
75        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy  */   
76        "sw     %0,     0(%2)        \n"  /* *paddr <= lsb     */
77        "sw     %1,     4(%2)        \n"  /* *(paddr+4) <= msb */
78        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15  */   
79        "sync                        \n"
80        ".set reorder                \n"
81        : : "r" (data_lsb), "r" (data_msb), "r" (ptr), "r" (cxy) : "$15" );
82}
83
84////////////////////////////////////
85void hal_remote_spt( xptr_t      xp,
86                     void *      pt )
87{
88    hal_remote_sw ( xp , (uint32_t)pt );
89}
90
91////////////////////////////////
92uint8_t hal_remote_lb( xptr_t  xp )
93{
94        char     data;
95    uint32_t ptr = (uint32_t)GET_PTR( xp );
96    uint32_t cxy = (uint32_t)GET_CXY( xp );
97
98    asm volatile( 
99        ".set noreorder              \n"
100        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT   */ 
101        "mtc2   %2,     $24          \n"  /* PADDR_EXT <= cxy   */   
102        "lb     %0,     0(%1)        \n"  /* data <= *paddr     */
103        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15   */   
104        ".set reorder                \n"
105        : "=r" (data) : "r" (ptr), "r" (cxy) : "$15" );
106
107        return ( data );
108}
109
110////////////////////////////////////
111uint32_t hal_remote_lw( xptr_t  xp )
112{
113        uint32_t data;
114    uint32_t ptr = (uint32_t)GET_PTR( xp );
115    uint32_t cxy = (uint32_t)GET_CXY( xp );
116
117    asm volatile( 
118        ".set noreorder              \n"
119        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT   */ 
120        "mtc2   %2,     $24          \n"  /* PADDR_EXT <= cxy   */   
121        "lw     %0,     0(%1)        \n"  /* data <= *paddr     */
122        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15   */   
123        ".set reorder                \n"
124        : "=r" (data) : "r" (ptr), "r" (cxy) : "$15" );
125
126    return ( data );
127}
128
129/////////////////////////////////////
130uint64_t hal_remote_lwd( xptr_t  xp )
131{
132    uint32_t data_lsb;
133    uint32_t data_msb;
134    uint32_t ptr = (uint32_t)GET_PTR( xp );
135    uint32_t cxy = (uint32_t)GET_CXY( xp );
136
137    asm volatile( 
138        ".set noreorder              \n"
139        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
140        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy     */   
141        "lw     %0,     0(%2)        \n"  /* data_lsb <= *paddr   */
142        "lw     %1,     4(%2)        \n"  /* data_msb <= *paddr+4 */
143        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
144        ".set reorder                \n"
145        : "=r" (data_lsb), "=r"(data_msb) : "r" (ptr), "r" (cxy) : "$15" );
146
147    return ( (((uint64_t)data_msb)<<32) + (((uint64_t)data_lsb)) );
148}
149
150////////////////////////////////////
151void * hal_remote_lpt( xptr_t    xp )
152{
153    return (void *)hal_remote_lw ( xp );
154}
155
156///////////////////////////////////////////
157bool_t hal_remote_atomic_cas( xptr_t    xp,
158                              uint32_t  old,
159                              uint32_t  new )
160{
161        bool_t   isAtomic;
162    uint32_t ptr = (uint32_t)GET_PTR( xp );
163    uint32_t cxy = (uint32_t)GET_CXY( xp );
164
165    asm volatile( 
166        ".set noreorder              \n"
167        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT   */ 
168        "mtc2   %4,     $24          \n"  /* PADDR_EXT <= cxy   */   
169        "or     $8,     $0,    %3    \n"  /* $8 <= new          */
170        "ll     $3,    0(%1)         \n"  /* $3 <= *paddr       */
171        "bne    $3,     %2,    1f    \n"  /* if ($3 != old)     */
172        "li     $7,     0            \n"  /* $7 <= 0            */
173        "sc     $8,     (%1)         \n"  /* *paddr <= new      */
174        "or     $7,     $8,    $0    \n"  /* $7 <= atomic       */
175        "sync                        \n"
176        "1:                          \n"
177        "or     %0,     $7,    $0    \n"  /* isAtomic <= $7     */
178        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15   */   
179        ".set reorder                \n"
180        : "=&r" (isAtomic) : "r" (ptr), "r" (old) , "r" (new), "r" (cxy) 
181                : "$3", "$7", "$8", "$15" );
182
183        return isAtomic;
184}
185
186////////////////////////////////////////////
187uint32_t hal_remote_atomic_add( xptr_t   xp, 
188                                uint32_t incr )
189{       
190        uint32_t current;
191    uint32_t ptr = (uint32_t)GET_PTR( xp );
192    uint32_t cxy = (uint32_t)GET_CXY( xp );
193
194    asm volatile( 
195        ".set noreorder              \n"
196        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
197        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy     */   
198        "1:                          \n"
199        "ll     %0,     (%1)         \n"  /* current <= *paddr    */
200        "addu   $3,     %0,     %2   \n"  /* $3 <= current + incr */
201        "sc     $3,     (%1)         \n"  /* *paddr <= $3         */
202        "beq    $3,     $0,     1b   \n"  /* retry if failure     */
203        "nop                         \n"
204        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
205        ".set reorder                \n"
206                : "=&r" (current) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" );
207
208        return current;
209}
210
211////////////////////////////////////////////
212uint32_t hal_remote_atomic_and( xptr_t   xp, 
213                                uint32_t mask )
214{       
215        uint32_t current;
216    uint32_t ptr = (uint32_t)GET_PTR( xp );
217    uint32_t cxy = (uint32_t)GET_CXY( xp );
218
219    asm volatile( 
220        ".set noreorder              \n"
221        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
222        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy     */   
223        "1:                          \n"
224        "ll     %0,     (%1)         \n"  /* current <= *paddr    */
225        "and    $3,     %0,     %2   \n"  /* $3 <= current & mask */
226        "sc     $3,     (%1)         \n"  /* *paddr <= $3         */
227        "beq    $3,     $0,     1b   \n"  /* retry if failure     */
228        "nop                         \n"
229        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
230        ".set reorder                \n"
231            : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" );
232
233        return current;
234}
235
236////////////////////////////////////////////
237uint32_t hal_remote_atomic_or( xptr_t   xp, 
238                               uint32_t mask )
239{       
240        uint32_t current;
241    uint32_t ptr = (uint32_t)GET_PTR( xp );
242    uint32_t cxy = (uint32_t)GET_CXY( xp );
243
244    asm volatile( 
245        ".set noreorder              \n"
246        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
247        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy     */   
248        "1:                          \n"
249        "ll     %0,     (%1)         \n"  /* current <= *paddr    */
250        "or     $3,     %0,     %2   \n"  /* $3 <= current | mask */
251        "sc     $3,     (%1)         \n"  /* *paddr <= $3         */
252        "beq    $3,     $0,     1b   \n"  /* retry if failure     */
253        "nop                         \n"
254        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
255        ".set reorder                \n"
256            : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" );
257
258        return current;
259}
260
261/////////////////////////////////////////////////
262error_t hal_remote_atomic_try_add( xptr_t     xp,
263                                   uint32_t   incr,
264                                   uint32_t * old )
265{
266        uint32_t current;
267        error_t  error;
268    uint32_t ptr = (uint32_t)GET_PTR( xp );
269    uint32_t cxy = (uint32_t)GET_CXY( xp );
270
271    asm volatile( 
272        ".set noreorder              \n"
273        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
274        "mtc2   %4,     $24          \n"  /* PADDR_EXT <= cxy     */   
275        "ll     %0,     (%2)         \n"  /* current <= *paddr    */
276        "addu   $3,     %0,     %3   \n"  /* $3 <= current + incr */
277        "sc     $3,     (%2)         \n"  /* *paddr <= $3         */
278        "beq    $3,     $0,     1f   \n"  /* exit if failure      */
279        "ori    %1,     $0,      1   \n"        /* fail: ret <= 1       */
280        "and    %1,     $0,     $0   \n"        /* success: ret <= 0    */
281        "1:                          \n"
282        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
283        ".set reorder                \n"
284                : "=&r" (current), "=&r" (error) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" );
285               
286    *old = current;
287               
288        return error;
289}
290
291/////////////////////////////////////
292void hal_remote_memcpy( xptr_t   dst,
293                        xptr_t   src,
294                        uint32_t size )
295{
296        uint32_t i;
297        uint32_t wsize;
298    uint32_t dptr = (uint32_t)GET_PTR( dst );
299    uint32_t dcxy = (uint32_t)GET_CXY( dst );
300    uint32_t sptr = (uint32_t)GET_PTR( src );
301    uint32_t scxy = (uint32_t)GET_CXY( src );
302
303        if( (dptr & 0x3) || (sptr & 0x3) ) wsize = 0;  // do it all in bytes
304    else                               wsize = size >> 2;
305
306        for( i = 0 ; i < wsize ; i++ )          // transfer one word per iteration
307        {
308        asm volatile( 
309            ".set noreorder              \n"
310            "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
311            "mtc2   %0,     $24          \n"  /* PADDR_EXT <= scxy    */   
312                    "lw     $3,     0(%1)            \n"  /* $3 <= *src           */                           
313            "mtc2   %2,     $24          \n"  /* PADDR_EXT <= dcxy    */       
314            "sw     $3,     0(%3)        \n"  /* *dst <= $3           */
315            "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
316            ".set reorder                \n"
317                    : : "r"(scxy), "r" (sptr+(i<<2)), "r"(dcxy), "r" (dptr+(i<<2)) : "$3", "$15" );             
318        }
319
320        for( i = wsize << 2 ; i < size ; i++ )  // transfer one byte per iteration
321        {
322        asm volatile( 
323            ".set noreorder              \n"
324            "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
325            "mtc2   %0,     $24          \n"  /* PADDR_EXT <= scxy    */   
326                    "lb         $3,     0(%1)        \n"  /* $3 <= *src           */                           
327            "mtc2   %2,     $24          \n"  /* PADDR_EXT <= dcxy    */       
328            "sb     $3,     0(%3)        \n"  /* *dst <= $3           */
329            "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
330            ".set reorder                \n"
331                    : : "r"(scxy), "r" (sptr+i), "r"(dcxy), "r" (dptr+i) : "$3", "$15" );               
332        }
333}
334
335
Note: See TracBrowser for help on using the repository browser.