[1] | 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 | //////////////////////////////// |
---|
| 28 | void hal_remote_sb( xptr_t xp, |
---|
| 29 | char data ) |
---|
| 30 | { |
---|
| 31 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 32 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 33 | |
---|
[8] | 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" ); |
---|
[1] | 43 | } |
---|
| 44 | |
---|
| 45 | ///////////////////////////////// |
---|
| 46 | void 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 */ |
---|
[8] | 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" ); |
---|
[1] | 60 | } |
---|
| 61 | |
---|
| 62 | ////////////////////////////////// |
---|
| 63 | void 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 | |
---|
[8] | 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" ); |
---|
[1] | 82 | } |
---|
| 83 | |
---|
| 84 | //////////////////////////////////// |
---|
| 85 | void hal_remote_spt( xptr_t xp, |
---|
| 86 | void * pt ) |
---|
| 87 | { |
---|
| 88 | hal_remote_sw ( xp , (uint32_t)pt ); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | //////////////////////////////// |
---|
| 92 | char 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 | |
---|
[8] | 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" ); |
---|
[1] | 106 | |
---|
| 107 | return ( data ); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | //////////////////////////////////// |
---|
| 111 | uint32_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 | |
---|
[8] | 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" ); |
---|
[1] | 125 | |
---|
| 126 | return ( data ); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | ///////////////////////////////////// |
---|
| 130 | uint64_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 | |
---|
[8] | 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" ); |
---|
[1] | 146 | |
---|
| 147 | return ( (((uint64_t)data_msb)<<32) + (((uint64_t)data_lsb)) ); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | //////////////////////////////////// |
---|
| 151 | void * hal_remote_lpt( xptr_t xp, |
---|
| 152 | void * pt ) |
---|
| 153 | { |
---|
| 154 | return (void *)hal_remote_lw ( xp ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | //////////////////////////////////////// |
---|
| 158 | uint32_t hal_remote_lw_unc( xptr_t xp ) |
---|
| 159 | { |
---|
| 160 | uint32_t data; |
---|
| 161 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 162 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 163 | |
---|
[8] | 164 | asm volatile( |
---|
| 165 | ".set noreorder \n" |
---|
| 166 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 167 | "mtc2 %2, $24 \n" /* PADDR_EXT <= cxy */ |
---|
| 168 | "ll %0, 0(%1) \n" /* data <= *paddr */ |
---|
| 169 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 170 | ".set reorder \n" |
---|
| 171 | : "=r" (data) : "r" (ptr), "r" (cxy) : "$15" ); |
---|
[1] | 172 | |
---|
| 173 | return ( data ); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /////////////////////////////////////////// |
---|
| 177 | bool_t hal_remote_atomic_cas( xptr_t xp, |
---|
| 178 | uint32_t old, |
---|
| 179 | uint32_t new ) |
---|
| 180 | { |
---|
| 181 | bool_t isAtomic; |
---|
| 182 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 183 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 184 | |
---|
[8] | 185 | asm volatile( |
---|
| 186 | ".set noreorder \n" |
---|
| 187 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 188 | "mtc2 %4, $24 \n" /* PADDR_EXT <= cxy */ |
---|
| 189 | "or $8, $0, %3 \n" /* $8 <= new */ |
---|
| 190 | "ll $3, 0(%1) \n" /* $3 <= *paddr */ |
---|
| 191 | "bne $3, %2, 1f \n" /* if ($3 != old) */ |
---|
| 192 | "li $7, 0 \n" /* $7 <= 0 */ |
---|
| 193 | "sc $8, (%1) \n" /* *paddr <= new */ |
---|
| 194 | "or $7, $8, $0 \n" /* $7 <= atomic */ |
---|
| 195 | "sync \n" |
---|
| 196 | "1: \n" |
---|
| 197 | "or %0, $7, $0 \n" /* isAtomic <= $7 */ |
---|
| 198 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 199 | ".set reorder \n" |
---|
| 200 | : "=&r" (isAtomic) : "r" (ptr), "r" (old) , "r" (new), "r" (cxy) |
---|
| 201 | : "$3", "$7", "$8", "$15" ); |
---|
[1] | 202 | |
---|
| 203 | return isAtomic; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | //////////////////////////////////////////// |
---|
| 207 | uint32_t hal_remote_atomic_add( xptr_t xp, |
---|
| 208 | uint32_t incr ) |
---|
| 209 | { |
---|
| 210 | uint32_t current; |
---|
| 211 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 212 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 213 | |
---|
[8] | 214 | asm volatile( |
---|
| 215 | ".set noreorder \n" |
---|
| 216 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 217 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
| 218 | "1: \n" |
---|
| 219 | "ll %0, (%1) \n" /* current <= *paddr */ |
---|
| 220 | "addu $3, %0, %2 \n" /* $3 <= current + incr */ |
---|
| 221 | "sc $3, (%1) \n" /* *paddr <= $3 */ |
---|
| 222 | "beq $3, $0, 1b \n" /* retry if failure */ |
---|
| 223 | "nop \n" |
---|
| 224 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 225 | ".set reorder \n" |
---|
| 226 | : "=&r" (current) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" ); |
---|
[1] | 227 | |
---|
| 228 | return current; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | //////////////////////////////////////////// |
---|
| 232 | uint32_t hal_remote_atomic_and( xptr_t xp, |
---|
| 233 | uint32_t mask ) |
---|
| 234 | { |
---|
| 235 | uint32_t current; |
---|
| 236 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 237 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 238 | |
---|
[8] | 239 | asm volatile( |
---|
| 240 | ".set noreorder \n" |
---|
| 241 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 242 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
| 243 | "1: \n" |
---|
| 244 | "ll %0, (%1) \n" /* current <= *paddr */ |
---|
| 245 | "and $3, %0, %2 \n" /* $3 <= current & mask */ |
---|
| 246 | "sc $3, (%1) \n" /* *paddr <= $3 */ |
---|
| 247 | "beq $3, $0, 1b \n" /* retry if failure */ |
---|
| 248 | "nop \n" |
---|
| 249 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 250 | ".set reorder \n" |
---|
| 251 | : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" ); |
---|
[1] | 252 | |
---|
| 253 | return current; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | //////////////////////////////////////////// |
---|
| 257 | uint32_t hal_remote_atomic_or( xptr_t xp, |
---|
| 258 | uint32_t mask ) |
---|
| 259 | { |
---|
| 260 | uint32_t current; |
---|
| 261 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 262 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 263 | |
---|
[8] | 264 | asm volatile( |
---|
| 265 | ".set noreorder \n" |
---|
| 266 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 267 | "mtc2 %3, $24 \n" /* PADDR_EXT <= cxy */ |
---|
| 268 | "1: \n" |
---|
| 269 | "ll %0, (%1) \n" /* current <= *paddr */ |
---|
| 270 | "or $3, %0, %2 \n" /* $3 <= current | mask */ |
---|
| 271 | "sc $3, (%1) \n" /* *paddr <= $3 */ |
---|
| 272 | "beq $3, $0, 1b \n" /* retry if failure */ |
---|
| 273 | "nop \n" |
---|
| 274 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 275 | ".set reorder \n" |
---|
| 276 | : "=&r" (current) : "r" (ptr), "r" (mask), "r" (cxy) : "$3", "$15" ); |
---|
[1] | 277 | |
---|
| 278 | return current; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | ///////////////////////////////////////////////// |
---|
| 282 | error_t hal_remote_atomic_try_add( xptr_t xp, |
---|
| 283 | uint32_t incr, |
---|
| 284 | uint32_t * old ) |
---|
| 285 | { |
---|
| 286 | uint32_t current; |
---|
| 287 | error_t error; |
---|
| 288 | uint32_t ptr = (uint32_t)GET_PTR( xp ); |
---|
| 289 | uint32_t cxy = (uint32_t)GET_CXY( xp ); |
---|
| 290 | |
---|
[8] | 291 | asm volatile( |
---|
| 292 | ".set noreorder \n" |
---|
| 293 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 294 | "mtc2 %4, $24 \n" /* PADDR_EXT <= cxy */ |
---|
| 295 | "ll %0, (%2) \n" /* current <= *paddr */ |
---|
| 296 | "addu $3, %0, %3 \n" /* $3 <= current + incr */ |
---|
| 297 | "sc $3, (%2) \n" /* *paddr <= $3 */ |
---|
| 298 | "beq $3, $0, 1f \n" /* exit if failure */ |
---|
| 299 | "ori %1, $0, 1 \n" /* fail: ret <= 1 */ |
---|
| 300 | "and %1, $0, $0 \n" /* success: ret <= 0 */ |
---|
| 301 | "1: \n" |
---|
| 302 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 303 | ".set reorder \n" |
---|
| 304 | : "=&r" (current), "=&r" (error) : "r" (ptr), "r" (incr), "r" (cxy) : "$3", "$15" ); |
---|
[1] | 305 | |
---|
| 306 | *old = current; |
---|
| 307 | |
---|
| 308 | return error; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | ///////////////////////////////////// |
---|
| 312 | void hal_remote_memcpy( xptr_t dst, |
---|
| 313 | xptr_t src, |
---|
| 314 | uint32_t size ) |
---|
| 315 | { |
---|
| 316 | uint32_t i; |
---|
| 317 | uint32_t wsize; |
---|
| 318 | uint32_t dptr = (uint32_t)GET_PTR( dst ); |
---|
| 319 | uint32_t dcxy = (uint32_t)GET_CXY( dst ); |
---|
| 320 | uint32_t sptr = (uint32_t)GET_PTR( src ); |
---|
| 321 | uint32_t scxy = (uint32_t)GET_CXY( src ); |
---|
| 322 | |
---|
| 323 | if( (dptr & 0x3) || (sptr & 0x3) ) wsize = 0; // do it all in bytes |
---|
| 324 | else wsize = size >> 2; |
---|
| 325 | |
---|
| 326 | for( i = 0 ; i < wsize ; i++ ) // transfer one word per iteration |
---|
| 327 | { |
---|
[8] | 328 | asm volatile( |
---|
| 329 | ".set noreorder \n" |
---|
| 330 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 331 | "mtc2 %0, $24 \n" /* PADDR_EXT <= scxy */ |
---|
| 332 | "lw $3, 0(%1) \n" /* $3 <= *src */ |
---|
| 333 | "mtc2 %2, $24 \n" /* PADDR_EXT <= dcxy */ |
---|
| 334 | "sw $3, 0(%3) \n" /* *dst <= $3 */ |
---|
| 335 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 336 | ".set reorder \n" |
---|
| 337 | : : "r"(scxy), "r" (sptr+(i<<2)), "r"(dcxy), "r" (dptr+(i<<2)) : "$3", "$15" ); |
---|
[1] | 338 | } |
---|
| 339 | |
---|
| 340 | for( i = wsize << 2 ; i < size ; i++ ) // transfer one byte per iteration |
---|
| 341 | { |
---|
[8] | 342 | asm volatile( |
---|
| 343 | ".set noreorder \n" |
---|
| 344 | "mfc2 $15, $24 \n" /* $15 <= PADDR_EXT */ |
---|
| 345 | "mtc2 %0, $24 \n" /* PADDR_EXT <= scxy */ |
---|
| 346 | "lb $3, 0(%1) \n" /* $3 <= *src */ |
---|
| 347 | "mtc2 %2, $24 \n" /* PADDR_EXT <= dcxy */ |
---|
| 348 | "sb $3, 0(%3) \n" /* *dst <= $3 */ |
---|
| 349 | "mtc2 $15, $24 \n" /* PADDR_EXT <= $15 */ |
---|
| 350 | ".set reorder \n" |
---|
| 351 | : : "r"(scxy), "r" (sptr+i), "r"(dcxy), "r" (dptr+i) : "$3", "$15" ); |
---|
[1] | 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | |
---|