1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Atomic futex routines
5 * Based on the PowerPC implementataion
7 * Copyright (C) 2013 TangoTec Ltd.
9 * Baruch Siach <baruch@tkos.co.il>
12 #ifndef _ASM_XTENSA_FUTEX_H
13 #define _ASM_XTENSA_FUTEX_H
15 #include <linux/futex.h>
16 #include <linux/uaccess.h>
17 #include <linux/errno.h>
19 #if XCHAL_HAVE_EXCLUSIVE
20 #define __futex_atomic_op(insn, ret, old, uaddr, arg) \
22 "1: l32ex %[oldval], %[addr]\n" \
24 "2: s32ex %[newval], %[addr]\n" \
25 " getex %[newval]\n" \
26 " beqz %[newval], 1b\n" \
27 " movi %[newval], 0\n" \
29 " .section .fixup,\"ax\"\n" \
31 " .literal_position\n" \
32 "5: movi %[oldval], 3b\n" \
33 " movi %[newval], %[fault]\n" \
36 " .section __ex_table,\"a\"\n" \
37 " .long 1b, 5b, 2b, 5b\n" \
39 : [oldval] "=&r" (old), [newval] "=&r" (ret) \
40 : [addr] "r" (uaddr), [oparg] "r" (arg), \
41 [fault] "I" (-EFAULT) \
43 #elif XCHAL_HAVE_S32C1I
44 #define __futex_atomic_op(insn, ret, old, uaddr, arg) \
46 "1: l32i %[oldval], %[mem]\n" \
48 " wsr %[oldval], scompare1\n" \
49 "2: s32c1i %[newval], %[mem]\n" \
50 " bne %[newval], %[oldval], 1b\n" \
51 " movi %[newval], 0\n" \
53 " .section .fixup,\"ax\"\n" \
55 " .literal_position\n" \
56 "5: movi %[oldval], 3b\n" \
57 " movi %[newval], %[fault]\n" \
60 " .section __ex_table,\"a\"\n" \
61 " .long 1b, 5b, 2b, 5b\n" \
63 : [oldval] "=&r" (old), [newval] "=&r" (ret), \
64 [mem] "+m" (*(uaddr)) \
65 : [oparg] "r" (arg), [fault] "I" (-EFAULT) \
69 static inline int arch_futex_atomic_op_inuser(int op
, int oparg
, int *oval
,
72 #if XCHAL_HAVE_S32C1I || XCHAL_HAVE_EXCLUSIVE
79 __futex_atomic_op("mov %[newval], %[oparg]",
80 ret
, oldval
, uaddr
, oparg
);
83 __futex_atomic_op("add %[newval], %[oldval], %[oparg]",
84 ret
, oldval
, uaddr
, oparg
);
87 __futex_atomic_op("or %[newval], %[oldval], %[oparg]",
88 ret
, oldval
, uaddr
, oparg
);
91 __futex_atomic_op("and %[newval], %[oldval], %[oparg]",
92 ret
, oldval
, uaddr
, ~oparg
);
95 __futex_atomic_op("xor %[newval], %[oldval], %[oparg]",
96 ret
, oldval
, uaddr
, oparg
);
114 futex_atomic_cmpxchg_inatomic(u32
*uval
, u32 __user
*uaddr
,
115 u32 oldval
, u32 newval
)
117 #if XCHAL_HAVE_S32C1I || XCHAL_HAVE_EXCLUSIVE
121 if (!access_ok(uaddr
, sizeof(u32
)))
124 __asm__
__volatile__ (
125 " # futex_atomic_cmpxchg_inatomic\n"
126 #if XCHAL_HAVE_EXCLUSIVE
127 "1: l32ex %[tmp], %[addr]\n"
128 " s32i %[tmp], %[uval], 0\n"
129 " bne %[tmp], %[oldval], 2f\n"
130 " mov %[tmp], %[newval]\n"
131 "3: s32ex %[tmp], %[addr]\n"
134 #elif XCHAL_HAVE_S32C1I
135 " wsr %[oldval], scompare1\n"
136 "1: s32c1i %[newval], %[addr], 0\n"
137 " s32i %[newval], %[uval], 0\n"
140 " .section .fixup,\"ax\"\n"
142 " .literal_position\n"
143 "4: movi %[tmp], 2b\n"
144 " movi %[ret], %[fault]\n"
147 " .section __ex_table,\"a\"\n"
149 #if XCHAL_HAVE_EXCLUSIVE
153 : [ret
] "+r" (ret
), [newval
] "+r" (newval
), [tmp
] "=&r" (tmp
)
154 : [addr
] "r" (uaddr
), [oldval
] "r" (oldval
), [uval
] "r" (uval
),
155 [fault
] "I" (-EFAULT
)
164 #endif /* _ASM_XTENSA_FUTEX_H */