blob: a84d4c8acbbe65847621fb4f0ee6f40705fee177 [file] [log] [blame]
Catalin Marinas10b663a2012-03-05 11:49:34 +00001/*
2 * Based on arch/arm/include/asm/cmpxchg.h
3 *
4 * Copyright (C) 2012 ARM Ltd.
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 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#ifndef __ASM_CMPXCHG_H
19#define __ASM_CMPXCHG_H
20
21#include <linux/bug.h>
22
23#include <asm/barrier.h>
24
25static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
26{
27 unsigned long ret, tmp;
28
29 switch (size) {
30 case 1:
31 asm volatile("// __xchg1\n"
Will Deacond0d49072014-02-04 12:29:12 +000032 "1: ldxrb %w0, %2\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000033 " stlxrb %w1, %w3, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +000034 " cbnz %w1, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000035 : "=&r" (ret), "=&r" (tmp), "+Q" (*(u8 *)ptr)
36 : "r" (x)
Will Deacon8d0390d2014-02-04 12:29:13 +000037 : "memory");
Catalin Marinas10b663a2012-03-05 11:49:34 +000038 break;
39 case 2:
40 asm volatile("// __xchg2\n"
Will Deacond0d49072014-02-04 12:29:12 +000041 "1: ldxrh %w0, %2\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000042 " stlxrh %w1, %w3, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +000043 " cbnz %w1, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000044 : "=&r" (ret), "=&r" (tmp), "+Q" (*(u16 *)ptr)
45 : "r" (x)
Will Deacon8d0390d2014-02-04 12:29:13 +000046 : "memory");
Catalin Marinas10b663a2012-03-05 11:49:34 +000047 break;
48 case 4:
49 asm volatile("// __xchg4\n"
Will Deacond0d49072014-02-04 12:29:12 +000050 "1: ldxr %w0, %2\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000051 " stlxr %w1, %w3, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +000052 " cbnz %w1, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000053 : "=&r" (ret), "=&r" (tmp), "+Q" (*(u32 *)ptr)
54 : "r" (x)
Will Deacon8d0390d2014-02-04 12:29:13 +000055 : "memory");
Catalin Marinas10b663a2012-03-05 11:49:34 +000056 break;
57 case 8:
58 asm volatile("// __xchg8\n"
Will Deacond0d49072014-02-04 12:29:12 +000059 "1: ldxr %0, %2\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000060 " stlxr %w1, %3, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +000061 " cbnz %w1, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000062 : "=&r" (ret), "=&r" (tmp), "+Q" (*(u64 *)ptr)
63 : "r" (x)
Will Deacon8d0390d2014-02-04 12:29:13 +000064 : "memory");
Catalin Marinas10b663a2012-03-05 11:49:34 +000065 break;
66 default:
67 BUILD_BUG();
68 }
69
Will Deacond0d49072014-02-04 12:29:12 +000070 smp_mb();
Catalin Marinas10b663a2012-03-05 11:49:34 +000071 return ret;
72}
73
74#define xchg(ptr,x) \
Will Deaconf7060432014-04-30 16:23:06 +010075({ \
76 __typeof__(*(ptr)) __ret; \
77 __ret = (__typeof__(*(ptr))) \
78 __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \
79 __ret; \
80})
Catalin Marinas10b663a2012-03-05 11:49:34 +000081
82static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
83 unsigned long new, int size)
84{
85 unsigned long oldval = 0, res;
86
87 switch (size) {
88 case 1:
89 do {
90 asm volatile("// __cmpxchg1\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000091 " ldxrb %w1, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +000092 " mov %w0, #0\n"
93 " cmp %w1, %w3\n"
94 " b.ne 1f\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000095 " stxrb %w0, %w4, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +000096 "1:\n"
Will Deacon3a0310e2013-02-04 12:12:33 +000097 : "=&r" (res), "=&r" (oldval), "+Q" (*(u8 *)ptr)
98 : "Ir" (old), "r" (new)
Catalin Marinas10b663a2012-03-05 11:49:34 +000099 : "cc");
100 } while (res);
101 break;
102
103 case 2:
104 do {
105 asm volatile("// __cmpxchg2\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000106 " ldxrh %w1, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +0000107 " mov %w0, #0\n"
108 " cmp %w1, %w3\n"
109 " b.ne 1f\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000110 " stxrh %w0, %w4, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +0000111 "1:\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000112 : "=&r" (res), "=&r" (oldval), "+Q" (*(u16 *)ptr)
113 : "Ir" (old), "r" (new)
114 : "cc");
Catalin Marinas10b663a2012-03-05 11:49:34 +0000115 } while (res);
116 break;
117
118 case 4:
119 do {
120 asm volatile("// __cmpxchg4\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000121 " ldxr %w1, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +0000122 " mov %w0, #0\n"
123 " cmp %w1, %w3\n"
124 " b.ne 1f\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000125 " stxr %w0, %w4, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +0000126 "1:\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000127 : "=&r" (res), "=&r" (oldval), "+Q" (*(u32 *)ptr)
128 : "Ir" (old), "r" (new)
Catalin Marinas10b663a2012-03-05 11:49:34 +0000129 : "cc");
130 } while (res);
131 break;
132
133 case 8:
134 do {
135 asm volatile("// __cmpxchg8\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000136 " ldxr %1, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +0000137 " mov %w0, #0\n"
138 " cmp %1, %3\n"
139 " b.ne 1f\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000140 " stxr %w0, %4, %2\n"
Catalin Marinas10b663a2012-03-05 11:49:34 +0000141 "1:\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000142 : "=&r" (res), "=&r" (oldval), "+Q" (*(u64 *)ptr)
143 : "Ir" (old), "r" (new)
Catalin Marinas10b663a2012-03-05 11:49:34 +0000144 : "cc");
145 } while (res);
146 break;
147
148 default:
149 BUILD_BUG();
150 }
151
152 return oldval;
153}
154
155static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
156 unsigned long new, int size)
157{
158 unsigned long ret;
159
160 smp_mb();
161 ret = __cmpxchg(ptr, old, new, size);
162 smp_mb();
163
164 return ret;
165}
166
Mark Hambleton26b54532013-12-02 18:10:44 +0000167#define cmpxchg(ptr, o, n) \
168({ \
169 __typeof__(*(ptr)) __ret; \
170 __ret = (__typeof__(*(ptr))) \
171 __cmpxchg_mb((ptr), (unsigned long)(o), (unsigned long)(n), \
172 sizeof(*(ptr))); \
173 __ret; \
174})
Catalin Marinas10b663a2012-03-05 11:49:34 +0000175
Mark Hambleton26b54532013-12-02 18:10:44 +0000176#define cmpxchg_local(ptr, o, n) \
177({ \
178 __typeof__(*(ptr)) __ret; \
179 __ret = (__typeof__(*(ptr))) \
180 __cmpxchg((ptr), (unsigned long)(o), \
181 (unsigned long)(n), sizeof(*(ptr))); \
182 __ret; \
183})
Catalin Marinas10b663a2012-03-05 11:49:34 +0000184
Chen Ganga84b0862013-04-22 06:08:41 +0100185#define cmpxchg64(ptr,o,n) cmpxchg((ptr),(o),(n))
186#define cmpxchg64_local(ptr,o,n) cmpxchg_local((ptr),(o),(n))
187
Will Deacon9e7ced52013-10-09 15:54:28 +0100188#define cmpxchg64_relaxed(ptr,o,n) cmpxchg_local((ptr),(o),(n))
189
Catalin Marinas10b663a2012-03-05 11:49:34 +0000190#endif /* __ASM_CMPXCHG_H */