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
75 if (!access_ok(uaddr
, sizeof(u32
)))
80 __futex_atomic_op("mov %[newval], %[oparg]",
81 ret
, oldval
, uaddr
, oparg
);
84 __futex_atomic_op("add %[newval], %[oldval], %[oparg]",
85 ret
, oldval
, uaddr
, oparg
);
88 __futex_atomic_op("or %[newval], %[oldval], %[oparg]",
89 ret
, oldval
, uaddr
, oparg
);
92 __futex_atomic_op("and %[newval], %[oldval], %[oparg]",
93 ret
, oldval
, uaddr
, ~oparg
);
96 __futex_atomic_op("xor %[newval], %[oldval], %[oparg]",
97 ret
, oldval
, uaddr
, oparg
);
113 futex_atomic_cmpxchg_inatomic(u32
*uval
, u32 __user
*uaddr
,
114 u32 oldval
, u32 newval
)
116 #if XCHAL_HAVE_S32C1I || XCHAL_HAVE_EXCLUSIVE
120 if (!access_ok(uaddr
, sizeof(u32
)))
123 __asm__
__volatile__ (
124 " # futex_atomic_cmpxchg_inatomic\n"
125 #if XCHAL_HAVE_EXCLUSIVE
126 "1: l32ex %[tmp], %[addr]\n"
127 " s32i %[tmp], %[uval], 0\n"
128 " bne %[tmp], %[oldval], 2f\n"
129 " mov %[tmp], %[newval]\n"
130 "3: s32ex %[tmp], %[addr]\n"
133 #elif XCHAL_HAVE_S32C1I
134 " wsr %[oldval], scompare1\n"
135 "1: s32c1i %[newval], %[addr], 0\n"
136 " s32i %[newval], %[uval], 0\n"
139 " .section .fixup,\"ax\"\n"
141 " .literal_position\n"
142 "4: movi %[tmp], 2b\n"
143 " movi %[ret], %[fault]\n"
146 " .section __ex_table,\"a\"\n"
148 #if XCHAL_HAVE_EXCLUSIVE
152 : [ret
] "+r" (ret
), [newval
] "+r" (newval
), [tmp
] "=&r" (tmp
)
153 : [addr
] "r" (uaddr
), [oldval
] "r" (oldval
), [uval
] "r" (uval
),
154 [fault
] "I" (-EFAULT
)
163 #endif /* _ASM_XTENSA_FUTEX_H */