| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /* SPDX-License-Identifier: GPL-2.0-only */
- /*
- * Copyright (C) 2014 Regents of the University of California
- */
- #ifndef _ASM_RISCV_CMPXCHG_H
- #define _ASM_RISCV_CMPXCHG_H
- #include <linux/bug.h>
- #include <asm/alternative-macros.h>
- #include <asm/fence.h>
- #include <asm/hwcap.h>
- #include <asm/insn-def.h>
- #define __arch_xchg_masked(sc_sfx, prepend, append, r, p, n) \
- ({ \
- u32 *__ptr32b = (u32 *)((ulong)(p) & ~0x3); \
- ulong __s = ((ulong)(p) & (0x4 - sizeof(*p))) * BITS_PER_BYTE; \
- ulong __mask = GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) \
- << __s; \
- ulong __newx = (ulong)(n) << __s; \
- ulong __retx; \
- ulong __rc; \
- \
- __asm__ __volatile__ ( \
- prepend \
- "0: lr.w %0, %2\n" \
- " and %1, %0, %z4\n" \
- " or %1, %1, %z3\n" \
- " sc.w" sc_sfx " %1, %1, %2\n" \
- " bnez %1, 0b\n" \
- append \
- : "=&r" (__retx), "=&r" (__rc), "+A" (*(__ptr32b)) \
- : "rJ" (__newx), "rJ" (~__mask) \
- : "memory"); \
- \
- r = (__typeof__(*(p)))((__retx & __mask) >> __s); \
- })
- #define __arch_xchg(sfx, prepend, append, r, p, n) \
- ({ \
- __asm__ __volatile__ ( \
- prepend \
- " amoswap" sfx " %0, %2, %1\n" \
- append \
- : "=r" (r), "+A" (*(p)) \
- : "r" (n) \
- : "memory"); \
- })
- #define _arch_xchg(ptr, new, sc_sfx, swap_sfx, prepend, \
- sc_append, swap_append) \
- ({ \
- __typeof__(ptr) __ptr = (ptr); \
- __typeof__(*(__ptr)) __new = (new); \
- __typeof__(*(__ptr)) __ret; \
- \
- switch (sizeof(*__ptr)) { \
- case 1: \
- case 2: \
- __arch_xchg_masked(sc_sfx, prepend, sc_append, \
- __ret, __ptr, __new); \
- break; \
- case 4: \
- __arch_xchg(".w" swap_sfx, prepend, swap_append, \
- __ret, __ptr, __new); \
- break; \
- case 8: \
- __arch_xchg(".d" swap_sfx, prepend, swap_append, \
- __ret, __ptr, __new); \
- break; \
- default: \
- BUILD_BUG(); \
- } \
- (__typeof__(*(__ptr)))__ret; \
- })
- #define arch_xchg_relaxed(ptr, x) \
- _arch_xchg(ptr, x, "", "", "", "", "")
- #define arch_xchg_acquire(ptr, x) \
- _arch_xchg(ptr, x, "", "", "", \
- RISCV_ACQUIRE_BARRIER, RISCV_ACQUIRE_BARRIER)
- #define arch_xchg_release(ptr, x) \
- _arch_xchg(ptr, x, "", "", RISCV_RELEASE_BARRIER, "", "")
- #define arch_xchg(ptr, x) \
- _arch_xchg(ptr, x, ".rl", ".aqrl", "", RISCV_FULL_BARRIER, "")
- #define xchg32(ptr, x) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 4); \
- arch_xchg((ptr), (x)); \
- })
- #define xchg64(ptr, x) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
- arch_xchg((ptr), (x)); \
- })
- /*
- * Atomic compare and exchange. Compare OLD with MEM, if identical,
- * store NEW in MEM. Return the initial value in MEM. Success is
- * indicated by comparing RETURN with OLD.
- */
- #define __arch_cmpxchg_masked(sc_sfx, prepend, append, r, p, o, n) \
- ({ \
- u32 *__ptr32b = (u32 *)((ulong)(p) & ~0x3); \
- ulong __s = ((ulong)(p) & (0x4 - sizeof(*p))) * BITS_PER_BYTE; \
- ulong __mask = GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) \
- << __s; \
- ulong __newx = (ulong)(n) << __s; \
- ulong __oldx = (ulong)(o) << __s; \
- ulong __retx; \
- ulong __rc; \
- \
- __asm__ __volatile__ ( \
- prepend \
- "0: lr.w %0, %2\n" \
- " and %1, %0, %z5\n" \
- " bne %1, %z3, 1f\n" \
- " and %1, %0, %z6\n" \
- " or %1, %1, %z4\n" \
- " sc.w" sc_sfx " %1, %1, %2\n" \
- " bnez %1, 0b\n" \
- append \
- "1:\n" \
- : "=&r" (__retx), "=&r" (__rc), "+A" (*(__ptr32b)) \
- : "rJ" ((long)__oldx), "rJ" (__newx), \
- "rJ" (__mask), "rJ" (~__mask) \
- : "memory"); \
- \
- r = (__typeof__(*(p)))((__retx & __mask) >> __s); \
- })
- #define __arch_cmpxchg(lr_sfx, sc_sfx, prepend, append, r, p, co, o, n) \
- ({ \
- register unsigned int __rc; \
- \
- __asm__ __volatile__ ( \
- prepend \
- "0: lr" lr_sfx " %0, %2\n" \
- " bne %0, %z3, 1f\n" \
- " sc" sc_sfx " %1, %z4, %2\n" \
- " bnez %1, 0b\n" \
- append \
- "1:\n" \
- : "=&r" (r), "=&r" (__rc), "+A" (*(p)) \
- : "rJ" (co o), "rJ" (n) \
- : "memory"); \
- })
- #define _arch_cmpxchg(ptr, old, new, sc_sfx, prepend, append) \
- ({ \
- __typeof__(ptr) __ptr = (ptr); \
- __typeof__(*(__ptr)) __old = (old); \
- __typeof__(*(__ptr)) __new = (new); \
- __typeof__(*(__ptr)) __ret; \
- \
- switch (sizeof(*__ptr)) { \
- case 1: \
- case 2: \
- __arch_cmpxchg_masked(sc_sfx, prepend, append, \
- __ret, __ptr, __old, __new); \
- break; \
- case 4: \
- __arch_cmpxchg(".w", ".w" sc_sfx, prepend, append, \
- __ret, __ptr, (long)(int)(long), __old, __new); \
- break; \
- case 8: \
- __arch_cmpxchg(".d", ".d" sc_sfx, prepend, append, \
- __ret, __ptr, /**/, __old, __new); \
- break; \
- default: \
- BUILD_BUG(); \
- } \
- (__typeof__(*(__ptr)))__ret; \
- })
- #define arch_cmpxchg_relaxed(ptr, o, n) \
- _arch_cmpxchg((ptr), (o), (n), "", "", "")
- #define arch_cmpxchg_acquire(ptr, o, n) \
- _arch_cmpxchg((ptr), (o), (n), "", "", RISCV_ACQUIRE_BARRIER)
- #define arch_cmpxchg_release(ptr, o, n) \
- _arch_cmpxchg((ptr), (o), (n), "", RISCV_RELEASE_BARRIER, "")
- #define arch_cmpxchg(ptr, o, n) \
- _arch_cmpxchg((ptr), (o), (n), ".rl", "", " fence rw, rw\n")
- #define arch_cmpxchg_local(ptr, o, n) \
- arch_cmpxchg_relaxed((ptr), (o), (n))
- #define arch_cmpxchg64(ptr, o, n) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
- arch_cmpxchg((ptr), (o), (n)); \
- })
- #define arch_cmpxchg64_local(ptr, o, n) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
- arch_cmpxchg_relaxed((ptr), (o), (n)); \
- })
- #define arch_cmpxchg64_relaxed(ptr, o, n) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
- arch_cmpxchg_relaxed((ptr), (o), (n)); \
- })
- #define arch_cmpxchg64_acquire(ptr, o, n) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
- arch_cmpxchg_acquire((ptr), (o), (n)); \
- })
- #define arch_cmpxchg64_release(ptr, o, n) \
- ({ \
- BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
- arch_cmpxchg_release((ptr), (o), (n)); \
- })
- #ifdef CONFIG_RISCV_ISA_ZAWRS
- /*
- * Despite wrs.nto being "WRS-with-no-timeout", in the absence of changes to
- * @val we expect it to still terminate within a "reasonable" amount of time
- * for an implementation-specific other reason, a pending, locally-enabled
- * interrupt, or because it has been configured to raise an illegal
- * instruction exception.
- */
- static __always_inline void __cmpwait(volatile void *ptr,
- unsigned long val,
- int size)
- {
- unsigned long tmp;
- asm goto(ALTERNATIVE("j %l[no_zawrs]", "nop",
- 0, RISCV_ISA_EXT_ZAWRS, 1)
- : : : : no_zawrs);
- switch (size) {
- case 4:
- asm volatile(
- " lr.w %0, %1\n"
- " xor %0, %0, %2\n"
- " bnez %0, 1f\n"
- ZAWRS_WRS_NTO "\n"
- "1:"
- : "=&r" (tmp), "+A" (*(u32 *)ptr)
- : "r" (val));
- break;
- #if __riscv_xlen == 64
- case 8:
- asm volatile(
- " lr.d %0, %1\n"
- " xor %0, %0, %2\n"
- " bnez %0, 1f\n"
- ZAWRS_WRS_NTO "\n"
- "1:"
- : "=&r" (tmp), "+A" (*(u64 *)ptr)
- : "r" (val));
- break;
- #endif
- default:
- BUILD_BUG();
- }
- return;
- no_zawrs:
- asm volatile(RISCV_PAUSE : : : "memory");
- }
- #define __cmpwait_relaxed(ptr, val) \
- __cmpwait((ptr), (unsigned long)(val), sizeof(*(ptr)))
- #endif
- #endif /* _ASM_RISCV_CMPXCHG_H */
|