xref: /openbmc/linux/arch/xtensa/include/asm/futex.h (revision 151f4e2b)
1 /*
2  * Atomic futex routines
3  *
4  * Based on the PowerPC implementataion
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Copyright (C) 2013 TangoTec Ltd.
11  *
12  * Baruch Siach <baruch@tkos.co.il>
13  */
14 
15 #ifndef _ASM_XTENSA_FUTEX_H
16 #define _ASM_XTENSA_FUTEX_H
17 
18 #include <linux/futex.h>
19 #include <linux/uaccess.h>
20 #include <linux/errno.h>
21 
22 #if XCHAL_HAVE_EXCLUSIVE
23 #define __futex_atomic_op(insn, ret, old, uaddr, arg)	\
24 	__asm__ __volatile(				\
25 	"1:	l32ex	%[oldval], %[addr]\n"		\
26 		insn "\n"				\
27 	"2:	s32ex	%[newval], %[addr]\n"		\
28 	"	getex	%[newval]\n"			\
29 	"	beqz	%[newval], 1b\n"		\
30 	"	movi	%[newval], 0\n"			\
31 	"3:\n"						\
32 	"	.section .fixup,\"ax\"\n"		\
33 	"	.align 4\n"				\
34 	"	.literal_position\n"			\
35 	"5:	movi	%[oldval], 3b\n"		\
36 	"	movi	%[newval], %[fault]\n"		\
37 	"	jx	%[oldval]\n"			\
38 	"	.previous\n"				\
39 	"	.section __ex_table,\"a\"\n"		\
40 	"	.long 1b, 5b, 2b, 5b\n"			\
41 	"	.previous\n"				\
42 	: [oldval] "=&r" (old), [newval] "=&r" (ret)	\
43 	: [addr] "r" (uaddr), [oparg] "r" (arg),	\
44 	  [fault] "I" (-EFAULT)				\
45 	: "memory")
46 #elif XCHAL_HAVE_S32C1I
47 #define __futex_atomic_op(insn, ret, old, uaddr, arg)	\
48 	__asm__ __volatile(				\
49 	"1:	l32i	%[oldval], %[addr], 0\n"	\
50 		insn "\n"				\
51 	"	wsr	%[oldval], scompare1\n"		\
52 	"2:	s32c1i	%[newval], %[addr], 0\n"	\
53 	"	bne	%[newval], %[oldval], 1b\n"	\
54 	"	movi	%[newval], 0\n"			\
55 	"3:\n"						\
56 	"	.section .fixup,\"ax\"\n"		\
57 	"	.align 4\n"				\
58 	"	.literal_position\n"			\
59 	"5:	movi	%[oldval], 3b\n"		\
60 	"	movi	%[newval], %[fault]\n"		\
61 	"	jx	%[oldval]\n"			\
62 	"	.previous\n"				\
63 	"	.section __ex_table,\"a\"\n"		\
64 	"	.long 1b, 5b, 2b, 5b\n"			\
65 	"	.previous\n"				\
66 	: [oldval] "=&r" (old), [newval] "=&r" (ret)	\
67 	: [addr] "r" (uaddr), [oparg] "r" (arg),	\
68 	  [fault] "I" (-EFAULT)				\
69 	: "memory")
70 #endif
71 
72 static inline int arch_futex_atomic_op_inuser(int op, int oparg, int *oval,
73 		u32 __user *uaddr)
74 {
75 #if XCHAL_HAVE_S32C1I || XCHAL_HAVE_EXCLUSIVE
76 	int oldval = 0, ret;
77 
78 	pagefault_disable();
79 
80 	switch (op) {
81 	case FUTEX_OP_SET:
82 		__futex_atomic_op("mov %[newval], %[oparg]",
83 				  ret, oldval, uaddr, oparg);
84 		break;
85 	case FUTEX_OP_ADD:
86 		__futex_atomic_op("add %[newval], %[oldval], %[oparg]",
87 				  ret, oldval, uaddr, oparg);
88 		break;
89 	case FUTEX_OP_OR:
90 		__futex_atomic_op("or %[newval], %[oldval], %[oparg]",
91 				  ret, oldval, uaddr, oparg);
92 		break;
93 	case FUTEX_OP_ANDN:
94 		__futex_atomic_op("and %[newval], %[oldval], %[oparg]",
95 				  ret, oldval, uaddr, ~oparg);
96 		break;
97 	case FUTEX_OP_XOR:
98 		__futex_atomic_op("xor %[newval], %[oldval], %[oparg]",
99 				  ret, oldval, uaddr, oparg);
100 		break;
101 	default:
102 		ret = -ENOSYS;
103 	}
104 
105 	pagefault_enable();
106 
107 	if (!ret)
108 		*oval = oldval;
109 
110 	return ret;
111 #else
112 	return -ENOSYS;
113 #endif
114 }
115 
116 static inline int
117 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
118 			      u32 oldval, u32 newval)
119 {
120 #if XCHAL_HAVE_S32C1I || XCHAL_HAVE_EXCLUSIVE
121 	unsigned long tmp;
122 	int ret = 0;
123 
124 	if (!access_ok(uaddr, sizeof(u32)))
125 		return -EFAULT;
126 
127 	__asm__ __volatile__ (
128 	"	# futex_atomic_cmpxchg_inatomic\n"
129 #if XCHAL_HAVE_EXCLUSIVE
130 	"1:	l32ex	%[tmp], %[addr]\n"
131 	"	s32i	%[tmp], %[uval], 0\n"
132 	"	bne	%[tmp], %[oldval], 2f\n"
133 	"	mov	%[tmp], %[newval]\n"
134 	"3:	s32ex	%[tmp], %[addr]\n"
135 	"	getex	%[tmp]\n"
136 	"	beqz	%[tmp], 1b\n"
137 #elif XCHAL_HAVE_S32C1I
138 	"	wsr	%[oldval], scompare1\n"
139 	"1:	s32c1i	%[newval], %[addr], 0\n"
140 	"	s32i	%[newval], %[uval], 0\n"
141 #endif
142 	"2:\n"
143 	"	.section .fixup,\"ax\"\n"
144 	"	.align 4\n"
145 	"	.literal_position\n"
146 	"4:	movi	%[tmp], 2b\n"
147 	"	movi	%[ret], %[fault]\n"
148 	"	jx	%[tmp]\n"
149 	"	.previous\n"
150 	"	.section __ex_table,\"a\"\n"
151 	"	.long 1b, 4b\n"
152 #if XCHAL_HAVE_EXCLUSIVE
153 	"	.long 3b, 4b\n"
154 #endif
155 	"	.previous\n"
156 	: [ret] "+r" (ret), [newval] "+r" (newval), [tmp] "=&r" (tmp)
157 	: [addr] "r" (uaddr), [oldval] "r" (oldval), [uval] "r" (uval),
158 	  [fault] "I" (-EFAULT)
159 	: "memory");
160 
161 	return ret;
162 #else
163 	return -ENOSYS;
164 #endif
165 }
166 
167 #endif /* _ASM_XTENSA_FUTEX_H */
168