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

Last change on this file since 92 was 92, checked in by max@…, 7 years ago

fix the definition of hal_remote_spt, and add the *pt ops on
x86_64

File size: 13.3 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////////////////////////////////////////
157uint32_t hal_remote_lw_unc( xptr_t  xp )
158{
159        uint32_t data;
160    uint32_t ptr = (uint32_t)GET_PTR( xp );
161    uint32_t cxy = (uint32_t)GET_CXY( xp );
162
163    asm volatile( 
164        ".set noreorder              \n"
165        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT   */ 
166        "mtc2   %2,     $24          \n"  /* PADDR_EXT <= cxy   */   
167        "ll     %0,     0(%1)        \n"  /* data <= *paddr     */
168        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15   */   
169        ".set reorder                \n"
170        : "=r" (data) : "r" (ptr), "r" (cxy) : "$15" );
171
172        return ( data );
173}
174
175///////////////////////////////////////////
176bool_t hal_remote_atomic_cas( xptr_t    xp,
177                              uint32_t  old,
178                              uint32_t  new )
179{
180        bool_t   isAtomic;
181    uint32_t ptr = (uint32_t)GET_PTR( xp );
182    uint32_t cxy = (uint32_t)GET_CXY( xp );
183
184    asm volatile( 
185        ".set noreorder              \n"
186        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT   */ 
187        "mtc2   %4,     $24          \n"  /* PADDR_EXT <= cxy   */   
188        "or     $8,     $0,    %3    \n"  /* $8 <= new          */
189        "ll     $3,    0(%1)         \n"  /* $3 <= *paddr       */
190        "bne    $3,     %2,    1f    \n"  /* if ($3 != old)     */
191        "li     $7,     0            \n"  /* $7 <= 0            */
192        "sc     $8,     (%1)         \n"  /* *paddr <= new      */
193        "or     $7,     $8,    $0    \n"  /* $7 <= atomic       */
194        "sync                        \n"
195        "1:                          \n"
196        "or     %0,     $7,    $0    \n"  /* isAtomic <= $7     */
197        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15   */   
198        ".set reorder                \n"
199        : "=&r" (isAtomic) : "r" (ptr), "r" (old) , "r" (new), "r" (cxy) 
200                : "$3", "$7", "$8", "$15" );
201
202        return isAtomic;
203}
204
205////////////////////////////////////////////
206uint32_t hal_remote_atomic_add( xptr_t   xp, 
207                                uint32_t incr )
208{       
209        uint32_t current;
210    uint32_t ptr = (uint32_t)GET_PTR( xp );
211    uint32_t cxy = (uint32_t)GET_CXY( xp );
212
213    asm volatile( 
214        ".set noreorder              \n"
215        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
216        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy     */   
217        "1:                          \n"
218        "ll     %0,     (%1)         \n"  /* current <= *paddr    */
219        "addu   $3,     %0,     %2   \n"  /* $3 <= current + incr */
220        "sc     $3,     (%1)         \n"  /* *paddr <= $3         */
221        "beq    $3,     $0,     1b   \n"  /* retry if failure     */
222        "nop                         \n"
223        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
224        ".set reorder                \n"
225                : "=&r" (current) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" );
226
227        return current;
228}
229
230////////////////////////////////////////////
231uint32_t hal_remote_atomic_and( xptr_t   xp, 
232                                uint32_t mask )
233{       
234        uint32_t current;
235    uint32_t ptr = (uint32_t)GET_PTR( xp );
236    uint32_t cxy = (uint32_t)GET_CXY( xp );
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        "and    $3,     %0,     %2   \n"  /* $3 <= current & mask */
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" (mask), "r" (cxy) : "$3", "$15" );
251
252        return current;
253}
254
255////////////////////////////////////////////
256uint32_t hal_remote_atomic_or( xptr_t   xp, 
257                               uint32_t mask )
258{       
259        uint32_t current;
260    uint32_t ptr = (uint32_t)GET_PTR( xp );
261    uint32_t cxy = (uint32_t)GET_CXY( xp );
262
263    asm volatile( 
264        ".set noreorder              \n"
265        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
266        "mtc2   %3,     $24          \n"  /* PADDR_EXT <= cxy     */   
267        "1:                          \n"
268        "ll     %0,     (%1)         \n"  /* current <= *paddr    */
269        "or     $3,     %0,     %2   \n"  /* $3 <= current | mask */
270        "sc     $3,     (%1)         \n"  /* *paddr <= $3         */
271        "beq    $3,     $0,     1b   \n"  /* retry if failure     */
272        "nop                         \n"
273        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
274        ".set reorder                \n"
275            : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" );
276
277        return current;
278}
279
280/////////////////////////////////////////////////
281error_t hal_remote_atomic_try_add( xptr_t     xp,
282                                   uint32_t   incr,
283                                   uint32_t * old )
284{
285        uint32_t current;
286        error_t  error;
287    uint32_t ptr = (uint32_t)GET_PTR( xp );
288    uint32_t cxy = (uint32_t)GET_CXY( xp );
289
290    asm volatile( 
291        ".set noreorder              \n"
292        "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
293        "mtc2   %4,     $24          \n"  /* PADDR_EXT <= cxy     */   
294        "ll     %0,     (%2)         \n"  /* current <= *paddr    */
295        "addu   $3,     %0,     %3   \n"  /* $3 <= current + incr */
296        "sc     $3,     (%2)         \n"  /* *paddr <= $3         */
297        "beq    $3,     $0,     1f   \n"  /* exit if failure      */
298        "ori    %1,     $0,      1   \n"        /* fail: ret <= 1       */
299        "and    %1,     $0,     $0   \n"        /* success: ret <= 0    */
300        "1:                          \n"
301        "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
302        ".set reorder                \n"
303                : "=&r" (current), "=&r" (error) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" );
304               
305    *old = current;
306               
307        return error;
308}
309
310/////////////////////////////////////
311void hal_remote_memcpy( xptr_t   dst,
312                        xptr_t   src,
313                        uint32_t size )
314{
315        uint32_t i;
316        uint32_t wsize;
317    uint32_t dptr = (uint32_t)GET_PTR( dst );
318    uint32_t dcxy = (uint32_t)GET_CXY( dst );
319    uint32_t sptr = (uint32_t)GET_PTR( src );
320    uint32_t scxy = (uint32_t)GET_CXY( src );
321
322        if( (dptr & 0x3) || (sptr & 0x3) ) wsize = 0;  // do it all in bytes
323    else                               wsize = size >> 2;
324
325        for( i = 0 ; i < wsize ; i++ )          // transfer one word per iteration
326        {
327        asm volatile( 
328            ".set noreorder              \n"
329            "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
330            "mtc2   %0,     $24          \n"  /* PADDR_EXT <= scxy    */   
331                    "lw     $3,     0(%1)            \n"  /* $3 <= *src           */                           
332            "mtc2   %2,     $24          \n"  /* PADDR_EXT <= dcxy    */       
333            "sw     $3,     0(%3)        \n"  /* *dst <= $3           */
334            "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
335            ".set reorder                \n"
336                    : : "r"(scxy), "r" (sptr+(i<<2)), "r"(dcxy), "r" (dptr+(i<<2)) : "$3", "$15" );             
337        }
338
339        for( i = wsize << 2 ; i < size ; i++ )  // transfer one byte per iteration
340        {
341        asm volatile( 
342            ".set noreorder              \n"
343            "mfc2   $15,    $24          \n"  /* $15 <= PADDR_EXT     */ 
344            "mtc2   %0,     $24          \n"  /* PADDR_EXT <= scxy    */   
345                    "lb         $3,     0(%1)        \n"  /* $3 <= *src           */                           
346            "mtc2   %2,     $24          \n"  /* PADDR_EXT <= dcxy    */       
347            "sb     $3,     0(%3)        \n"  /* *dst <= $3           */
348            "mtc2   $15,    $24          \n"  /* PADDR_EXT <= $15     */   
349            ".set reorder                \n"
350                    : : "r"(scxy), "r" (sptr+i), "r"(dcxy), "r" (dptr+i) : "$3", "$15" );               
351        }
352}
353
354
Note: See TracBrowser for help on using the repository browser.