Skip to content

Commit a8ed2b7

Browse files
Leonardo Braspalmer-dabbelt
authored andcommitted
riscv/cmpxchg: Implement xchg for variables of size 1 and 2
xchg for variables of size 1-byte and 2-bytes is not yet available for riscv, even though its present in other architectures such as arm64 and x86. This could lead to not being able to implement some locking mechanisms or requiring some rework to make it work properly. Implement 1-byte and 2-bytes xchg in order to achieve parity with other architectures. Signed-off-by: Leonardo Bras <[email protected]> Tested-by: Guo Ren <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 54280ca commit a8ed2b7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

arch/riscv/include/asm/cmpxchg.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@
1111
#include <asm/barrier.h>
1212
#include <asm/fence.h>
1313

14+
#define __arch_xchg_masked(prepend, append, r, p, n) \
15+
({ \
16+
u32 *__ptr32b = (u32 *)((ulong)(p) & ~0x3); \
17+
ulong __s = ((ulong)(p) & (0x4 - sizeof(*p))) * BITS_PER_BYTE; \
18+
ulong __mask = GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) \
19+
<< __s; \
20+
ulong __newx = (ulong)(n) << __s; \
21+
ulong __retx; \
22+
ulong __rc; \
23+
\
24+
__asm__ __volatile__ ( \
25+
prepend \
26+
"0: lr.w %0, %2\n" \
27+
" and %1, %0, %z4\n" \
28+
" or %1, %1, %z3\n" \
29+
" sc.w %1, %1, %2\n" \
30+
" bnez %1, 0b\n" \
31+
append \
32+
: "=&r" (__retx), "=&r" (__rc), "+A" (*(__ptr32b)) \
33+
: "rJ" (__newx), "rJ" (~__mask) \
34+
: "memory"); \
35+
\
36+
r = (__typeof__(*(p)))((__retx & __mask) >> __s); \
37+
})
38+
1439
#define __arch_xchg(sfx, prepend, append, r, p, n) \
1540
({ \
1641
__asm__ __volatile__ ( \
@@ -27,7 +52,13 @@
2752
__typeof__(ptr) __ptr = (ptr); \
2853
__typeof__(*(__ptr)) __new = (new); \
2954
__typeof__(*(__ptr)) __ret; \
55+
\
3056
switch (sizeof(*__ptr)) { \
57+
case 1: \
58+
case 2: \
59+
__arch_xchg_masked(prepend, append, \
60+
__ret, __ptr, __new); \
61+
break; \
3162
case 4: \
3263
__arch_xchg(".w" sfx, prepend, append, \
3364
__ret, __ptr, __new); \

0 commit comments

Comments
 (0)