| 1 |
/* |
| 2 |
* Copyright 2009-2015 Samy Al Bahra. |
| 3 |
* Copyright 2012 João Fernandes. |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 |
* SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef CK_PR_PPC_H |
| 29 |
#define CK_PR_PPC_H |
| 30 |
|
| 31 |
#ifndef CK_PR_H |
| 32 |
#error Do not include this file directly, use ck_pr.h |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <ck_cc.h> |
| 36 |
#include <ck_md.h> |
| 37 |
|
| 38 |
/* |
| 39 |
* The following represent supported atomic operations. |
| 40 |
* These operations may be emulated. |
| 41 |
*/ |
| 42 |
#include "ck_f_pr.h" |
| 43 |
|
| 44 |
/* |
| 45 |
* Minimum interface requirement met. |
| 46 |
*/ |
| 47 |
#define CK_F_PR |
| 48 |
|
| 49 |
/* |
| 50 |
* This bounces the hardware thread from low to medium |
| 51 |
* priority. I am unsure of the benefits of this approach |
| 52 |
* but it is used by the Linux kernel. |
| 53 |
*/ |
| 54 |
CK_CC_INLINE static void |
| 55 |
ck_pr_stall(void) |
| 56 |
{ |
| 57 |
|
| 58 |
__asm__ __volatile__("or 1, 1, 1;" |
| 59 |
"or 2, 2, 2;" ::: "memory"); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
#define CK_PR_FENCE(T, I) \ |
| 64 |
CK_CC_INLINE static void \ |
| 65 |
ck_pr_fence_strict_##T(void) \ |
| 66 |
{ \ |
| 67 |
__asm__ __volatile__(I ::: "memory"); \ |
| 68 |
} |
| 69 |
|
| 70 |
#ifdef CK_MD_PPC32_LWSYNC |
| 71 |
#define CK_PR_LWSYNCOP "lwsync" |
| 72 |
#else /* CK_MD_PPC32_LWSYNC_DISABLE */ |
| 73 |
#define CK_PR_LWSYNCOP "sync" |
| 74 |
#endif |
| 75 |
|
| 76 |
CK_PR_FENCE(atomic, CK_PR_LWSYNCOP) |
| 77 |
CK_PR_FENCE(atomic_store, CK_PR_LWSYNCOP) |
| 78 |
CK_PR_FENCE(atomic_load, "sync") |
| 79 |
CK_PR_FENCE(store_atomic, CK_PR_LWSYNCOP) |
| 80 |
CK_PR_FENCE(load_atomic, CK_PR_LWSYNCOP) |
| 81 |
CK_PR_FENCE(store, CK_PR_LWSYNCOP) |
| 82 |
CK_PR_FENCE(store_load, "sync") |
| 83 |
CK_PR_FENCE(load, CK_PR_LWSYNCOP) |
| 84 |
CK_PR_FENCE(load_store, CK_PR_LWSYNCOP) |
| 85 |
CK_PR_FENCE(memory, "sync") |
| 86 |
CK_PR_FENCE(acquire, CK_PR_LWSYNCOP) |
| 87 |
CK_PR_FENCE(release, CK_PR_LWSYNCOP) |
| 88 |
CK_PR_FENCE(acqrel, CK_PR_LWSYNCOP) |
| 89 |
CK_PR_FENCE(lock, CK_PR_LWSYNCOP) |
| 90 |
CK_PR_FENCE(unlock, CK_PR_LWSYNCOP) |
| 91 |
|
| 92 |
#undef CK_PR_LWSYNCOP |
| 93 |
|
| 94 |
#undef CK_PR_FENCE |
| 95 |
|
| 96 |
#define CK_PR_LOAD(S, M, T, C, I) \ |
| 97 |
CK_CC_INLINE static T \ |
| 98 |
ck_pr_md_load_##S(const M *target) \ |
| 99 |
{ \ |
| 100 |
T r; \ |
| 101 |
__asm__ __volatile__(I "%U1%X1 %0, %1" \ |
| 102 |
: "=r" (r) \ |
| 103 |
: "m" (*(const C *)target) \ |
| 104 |
: "memory"); \ |
| 105 |
return (r); \ |
| 106 |
} |
| 107 |
|
| 108 |
CK_PR_LOAD(ptr, void, void *, uint32_t, "lwz") |
| 109 |
|
| 110 |
#define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I) |
| 111 |
|
| 112 |
CK_PR_LOAD_S(32, uint32_t, "lwz") |
| 113 |
CK_PR_LOAD_S(16, uint16_t, "lhz") |
| 114 |
CK_PR_LOAD_S(8, uint8_t, "lbz") |
| 115 |
CK_PR_LOAD_S(uint, unsigned int, "lwz") |
| 116 |
CK_PR_LOAD_S(int, int, "lwz") |
| 117 |
CK_PR_LOAD_S(short, short, "lhz") |
| 118 |
CK_PR_LOAD_S(char, char, "lbz") |
| 119 |
|
| 120 |
#undef CK_PR_LOAD_S |
| 121 |
#undef CK_PR_LOAD |
| 122 |
|
| 123 |
#define CK_PR_STORE(S, M, T, C, I) \ |
| 124 |
CK_CC_INLINE static void \ |
| 125 |
ck_pr_md_store_##S(M *target, T v) \ |
| 126 |
{ \ |
| 127 |
__asm__ __volatile__(I "%U0%X0 %1, %0" \ |
| 128 |
: "=m" (*(C *)target) \ |
| 129 |
: "r" (v) \ |
| 130 |
: "memory"); \ |
| 131 |
return; \ |
| 132 |
} |
| 133 |
|
| 134 |
CK_PR_STORE(ptr, void, const void *, uint32_t, "stw") |
| 135 |
|
| 136 |
#define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I) |
| 137 |
|
| 138 |
CK_PR_STORE_S(32, uint32_t, "stw") |
| 139 |
CK_PR_STORE_S(16, uint16_t, "sth") |
| 140 |
CK_PR_STORE_S(8, uint8_t, "stb") |
| 141 |
CK_PR_STORE_S(uint, unsigned int, "stw") |
| 142 |
CK_PR_STORE_S(int, int, "stw") |
| 143 |
CK_PR_STORE_S(short, short, "sth") |
| 144 |
CK_PR_STORE_S(char, char, "stb") |
| 145 |
|
| 146 |
#undef CK_PR_STORE_S |
| 147 |
#undef CK_PR_STORE |
| 148 |
|
| 149 |
#define CK_PR_CAS(N, T, M) \ |
| 150 |
CK_CC_INLINE static bool \ |
| 151 |
ck_pr_cas_##N##_value(M *target, T compare, T set, M *value) \ |
| 152 |
{ \ |
| 153 |
T previous; \ |
| 154 |
__asm__ __volatile__("1:" \ |
| 155 |
"lwarx %0, 0, %1;" \ |
| 156 |
"cmpw 0, %0, %3;" \ |
| 157 |
"bne- 2f;" \ |
| 158 |
"stwcx. %2, 0, %1;" \ |
| 159 |
"bne- 1b;" \ |
| 160 |
"2:" \ |
| 161 |
: "=&r" (previous) \ |
| 162 |
: "r" (target), \ |
| 163 |
"r" (set), \ |
| 164 |
"r" (compare) \ |
| 165 |
: "memory", "cc"); \ |
| 166 |
*(T *)value = previous; \ |
| 167 |
return (previous == compare); \ |
| 168 |
} \ |
| 169 |
CK_CC_INLINE static bool \ |
| 170 |
ck_pr_cas_##N(M *target, T compare, T set) \ |
| 171 |
{ \ |
| 172 |
T previous; \ |
| 173 |
__asm__ __volatile__("1:" \ |
| 174 |
"lwarx %0, 0, %1;" \ |
| 175 |
"cmpw 0, %0, %3;" \ |
| 176 |
"bne- 2f;" \ |
| 177 |
"stwcx. %2, 0, %1;" \ |
| 178 |
"bne- 1b;" \ |
| 179 |
"2:" \ |
| 180 |
: "=&r" (previous) \ |
| 181 |
: "r" (target), \ |
| 182 |
"r" (set), \ |
| 183 |
"r" (compare) \ |
| 184 |
: "memory", "cc"); \ |
| 185 |
return (previous == compare); \ |
| 186 |
} |
| 187 |
|
| 188 |
CK_PR_CAS(ptr, void *, void) |
| 189 |
#define CK_PR_CAS_S(a, b) CK_PR_CAS(a, b, b) |
| 190 |
CK_PR_CAS_S(32, uint32_t) |
| 191 |
CK_PR_CAS_S(uint, unsigned int) |
| 192 |
CK_PR_CAS_S(int, int) |
| 193 |
|
| 194 |
#undef CK_PR_CAS_S |
| 195 |
#undef CK_PR_CAS |
| 196 |
|
| 197 |
#define CK_PR_FAS(N, M, T, W) \ |
| 198 |
CK_CC_INLINE static T \ |
| 199 |
ck_pr_fas_##N(M *target, T v) \ |
| 200 |
{ \ |
| 201 |
T previous; \ |
| 202 |
__asm__ __volatile__("1:" \ |
| 203 |
"l" W "arx %0, 0, %1;" \ |
| 204 |
"st" W "cx. %2, 0, %1;" \ |
| 205 |
"bne- 1b;" \ |
| 206 |
: "=&r" (previous) \ |
| 207 |
: "r" (target), \ |
| 208 |
"r" (v) \ |
| 209 |
: "memory", "cc"); \ |
| 210 |
return (previous); \ |
| 211 |
} |
| 212 |
|
| 213 |
CK_PR_FAS(32, uint32_t, uint32_t, "w") |
| 214 |
CK_PR_FAS(ptr, void, void *, "w") |
| 215 |
CK_PR_FAS(int, int, int, "w") |
| 216 |
CK_PR_FAS(uint, unsigned int, unsigned int, "w") |
| 217 |
|
| 218 |
#undef CK_PR_FAS |
| 219 |
|
| 220 |
#define CK_PR_UNARY(O, N, M, T, I, W) \ |
| 221 |
CK_CC_INLINE static void \ |
| 222 |
ck_pr_##O##_##N(M *target) \ |
| 223 |
{ \ |
| 224 |
T previous; \ |
| 225 |
__asm__ __volatile__("1:" \ |
| 226 |
"l" W "arx %0, 0, %1;" \ |
| 227 |
I ";" \ |
| 228 |
"st" W "cx. %0, 0, %1;" \ |
| 229 |
"bne- 1b;" \ |
| 230 |
: "=&r" (previous) \ |
| 231 |
: "r" (target) \ |
| 232 |
: "memory", "cc"); \ |
| 233 |
return; \ |
| 234 |
} |
| 235 |
|
| 236 |
CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "w") |
| 237 |
CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "w") |
| 238 |
CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "w") |
| 239 |
CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "w") |
| 240 |
|
| 241 |
#define CK_PR_UNARY_S(S, T, W) \ |
| 242 |
CK_PR_UNARY(inc, S, T, T, "addic %0, %0, 1", W) \ |
| 243 |
CK_PR_UNARY(dec, S, T, T, "addic %0, %0, -1", W) \ |
| 244 |
CK_PR_UNARY(not, S, T, T, "not %0, %0", W) \ |
| 245 |
CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W) |
| 246 |
|
| 247 |
CK_PR_UNARY_S(32, uint32_t, "w") |
| 248 |
CK_PR_UNARY_S(uint, unsigned int, "w") |
| 249 |
CK_PR_UNARY_S(int, int, "w") |
| 250 |
|
| 251 |
#undef CK_PR_UNARY_S |
| 252 |
#undef CK_PR_UNARY |
| 253 |
|
| 254 |
#define CK_PR_BINARY(O, N, M, T, I, W) \ |
| 255 |
CK_CC_INLINE static void \ |
| 256 |
ck_pr_##O##_##N(M *target, T delta) \ |
| 257 |
{ \ |
| 258 |
T previous; \ |
| 259 |
__asm__ __volatile__("1:" \ |
| 260 |
"l" W "arx %0, 0, %1;" \ |
| 261 |
I " %0, %2, %0;" \ |
| 262 |
"st" W "cx. %0, 0, %1;" \ |
| 263 |
"bne- 1b;" \ |
| 264 |
: "=&r" (previous) \ |
| 265 |
: "r" (target), \ |
| 266 |
"r" (delta) \ |
| 267 |
: "memory", "cc"); \ |
| 268 |
return; \ |
| 269 |
} |
| 270 |
|
| 271 |
CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "w") |
| 272 |
CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "w") |
| 273 |
CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "w") |
| 274 |
CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "w") |
| 275 |
CK_PR_BINARY(xor, ptr, void, uintptr_t, "xor", "w") |
| 276 |
|
| 277 |
#define CK_PR_BINARY_S(S, T, W) \ |
| 278 |
CK_PR_BINARY(and, S, T, T, "and", W) \ |
| 279 |
CK_PR_BINARY(add, S, T, T, "add", W) \ |
| 280 |
CK_PR_BINARY(or, S, T, T, "or", W) \ |
| 281 |
CK_PR_BINARY(sub, S, T, T, "subf", W) \ |
| 282 |
CK_PR_BINARY(xor, S, T, T, "xor", W) |
| 283 |
|
| 284 |
CK_PR_BINARY_S(32, uint32_t, "w") |
| 285 |
CK_PR_BINARY_S(uint, unsigned int, "w") |
| 286 |
CK_PR_BINARY_S(int, int, "w") |
| 287 |
|
| 288 |
#undef CK_PR_BINARY_S |
| 289 |
#undef CK_PR_BINARY |
| 290 |
|
| 291 |
CK_CC_INLINE static void * |
| 292 |
ck_pr_faa_ptr(void *target, uintptr_t delta) |
| 293 |
{ |
| 294 |
uintptr_t previous, r; |
| 295 |
|
| 296 |
__asm__ __volatile__("1:" |
| 297 |
"lwarx %0, 0, %2;" |
| 298 |
"add %1, %3, %0;" |
| 299 |
"stwcx. %1, 0, %2;" |
| 300 |
"bne- 1b;" |
| 301 |
: "=&r" (previous), |
| 302 |
"=&r" (r) |
| 303 |
: "r" (target), |
| 304 |
"r" (delta) |
| 305 |
: "memory", "cc"); |
| 306 |
|
| 307 |
return (void *)(previous); |
| 308 |
} |
| 309 |
|
| 310 |
#define CK_PR_FAA(S, T, W) \ |
| 311 |
CK_CC_INLINE static T \ |
| 312 |
ck_pr_faa_##S(T *target, T delta) \ |
| 313 |
{ \ |
| 314 |
T previous, r; \ |
| 315 |
__asm__ __volatile__("1:" \ |
| 316 |
"l" W "arx %0, 0, %2;" \ |
| 317 |
"add %1, %3, %0;" \ |
| 318 |
"st" W "cx. %1, 0, %2;" \ |
| 319 |
"bne- 1b;" \ |
| 320 |
: "=&r" (previous), \ |
| 321 |
"=&r" (r) \ |
| 322 |
: "r" (target), \ |
| 323 |
"r" (delta) \ |
| 324 |
: "memory", "cc"); \ |
| 325 |
return (previous); \ |
| 326 |
} |
| 327 |
|
| 328 |
CK_PR_FAA(32, uint32_t, "w") |
| 329 |
CK_PR_FAA(uint, unsigned int, "w") |
| 330 |
CK_PR_FAA(int, int, "w") |
| 331 |
|
| 332 |
#undef CK_PR_FAA |
| 333 |
|
| 334 |
#endif /* CK_PR_PPC_H */ |
| 335 |
|