094505ad7f694c825219e7395fde0da27f8bf648
[platform/upstream/libatomic_ops.git] / src / atomic_ops / sysdeps / gcc / arm.h
1 /*
2  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
3  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
4  * Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved.
5  *
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
10  * Permission is hereby granted to use or copy this program
11  * for any purpose,  provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  *
16  */
17
18 #include "../read_ordered.h"
19
20 #include "../test_and_set_t_is_ao_t.h" /* Probably suboptimal */
21
22 /* NEC LE-IT: ARMv6 is the first architecture providing support for simple LL/SC
23  * A data memory barrier must be raised via CP15 command (see documentation).
24  *
25  * ARMv7 is compatible to ARMv6 but has a simpler command for issuing a
26  * memory barrier (DMB). Raising it via CP15 should still work as told me by the
27  * support engineers. If it turns out to be much quicker than we should implement
28  * custom code for ARMv7 using the asm { dmb } command.
29  *
30  * If only a single processor is used, we can define AO_UNIPROCESSOR
31  * and do not need to access CP15 for ensuring a DMB
32 */
33
34 /* NEC LE-IT: gcc has no way to easily check the arm architecture       */
35 /* but it defines only one of __ARM_ARCH_x__ to be true.                */
36 #if !defined(__ARM_ARCH_2__) && !defined(__ARM_ARCH_3__) \
37     && !defined(__ARM_ARCH_3M__) && !defined(__ARM_ARCH_4__) \
38     && !defined(__ARM_ARCH_4T__) && !defined(__ARM_ARCH_5__) \
39     && !defined(__ARM_ARCH_5E__) && !defined(__ARM_ARCH_5T__) \
40     && !defined(__ARM_ARCH_5TE__) && !defined(__ARM_ARCH_5TEJ__)
41
42 #include "../standard_ao_double_t.h"
43
44 AO_INLINE void
45 AO_nop_full(void)
46 {
47 #ifndef AO_UNIPROCESSOR
48         /* Issue a data memory barrier (keeps ordering of memory        */
49         /* transactions before and after this operation).               */
50         unsigned int dest=0;
51         __asm__ __volatile__("mcr p15,0,%0,c7,c10,5"
52                               : "=&r"(dest) : : "memory");
53 #endif
54 }
55
56 #define AO_HAVE_nop_full
57
58 /* NEC LE-IT: AO_t load is simple reading */
59 AO_INLINE AO_t
60 AO_load(const volatile AO_t *addr)
61 {
62   /* Cast away the volatile for architectures like IA64 where   */
63   /* volatile adds barrier semantics.                           */
64   return (*(const AO_t *)addr);
65 }
66 #define AO_HAVE_load
67
68 /* NEC LE-IT: atomic "store" - according to ARM documentation this is
69  * the only safe way to set variables also used in LL/SC environment.
70  * A direct write won't be recognized by the LL/SC construct on the _same_ CPU.
71  * Support engineers response for behaviour of ARMv6:
72  *
73    Core1        Core2          SUCCESS
74    ===================================
75    LDREX(x)
76    STREX(x)                    Yes
77    -----------------------------------
78    LDREX(x)
79                 STR(x)
80    STREX(x)                    No
81    -----------------------------------
82    LDREX(x)
83    STR(x)
84    STREX(x)                    Yes
85    -----------------------------------
86
87  * ARMv7 behaves similar, see documentation CortexA8 TRM, point 8.5
88  *
89  * HB: I think this is only a problem if interrupt handlers do not clear
90  * the reservation, as they almost certainly should.  Probably change this back
91  * in a while?
92 */
93 AO_INLINE void AO_store(volatile AO_t *addr, AO_t value)
94 {
95         AO_t    flag;
96
97         __asm__ __volatile__("@AO_store\n"
98 "1:     ldrex   %0, [%2]\n"
99 "       strex   %0, %3, [%2]\n"
100 "       teq     %0, #0\n"
101 "       bne     1b"
102         : "=&r"(flag), "+m"(*addr)
103         : "r" (addr), "r"(value)
104         : "cc");
105 }
106 #define AO_HAVE_store
107
108 /* NEC LE-IT: replace the SWAP as recommended by ARM:
109
110    "Applies to: ARM11 Cores
111         Though the SWP instruction will still work with ARM V6 cores, it is
112         recommended     to use the new V6 synchronization instructions. The SWP
113         instruction produces 'locked' read and write accesses which are atomic,
114         i.e. another operation cannot be done between these locked accesses which
115         ties up external bus (AHB,AXI) bandwidth and can increase worst case
116         interrupt latencies. LDREX,STREX are more flexible, other instructions can
117         be done between the LDREX and STREX accesses.
118    "
119 */
120 AO_INLINE AO_TS_t
121 AO_test_and_set(volatile AO_TS_t *addr)
122 {
123
124         AO_TS_t oldval;
125         unsigned long flag;
126
127         __asm__ __volatile__("@AO_test_and_set\n"
128 "1:     ldrex   %0, [%3]\n"
129 "       strex   %1, %4, [%3]\n"
130 "       teq             %1, #0\n"
131 "       bne             1b\n"
132         : "=&r"(oldval),"=&r"(flag), "+m"(*addr)
133         : "r"(addr), "r"(1)
134         : "cc");
135
136         return oldval;
137 }
138
139 #define AO_HAVE_test_and_set
140
141 /* NEC LE-IT: fetch and add for ARMv6 */
142 AO_INLINE AO_t
143 AO_fetch_and_add(volatile AO_t *p, AO_t incr)
144 {
145         unsigned long flag,tmp;
146         AO_t result;
147
148         __asm__ __volatile__("@AO_fetch_and_add\n"
149 "1:     ldrex   %0, [%5]\n"             /* get original         */
150 "       add     %2, %0, %4\n"           /* sum up in incr       */
151 "       strex   %1, %2, [%5]\n"         /* store them           */
152 "       teq             %1, #0\n"
153 "       bne             1b\n"
154         : "=&r"(result),"=&r"(flag),"=&r"(tmp),"+m"(*p) /* 0..3 */
155         : "r"(incr), "r"(p)                                                             /* 4..5 */
156         : "cc");
157
158         return result;
159 }
160
161 #define AO_HAVE_fetch_and_add
162
163 /* NEC LE-IT: fetch and add1 for ARMv6 */
164 AO_INLINE AO_t
165 AO_fetch_and_add1(volatile AO_t *p)
166 {
167         unsigned long flag,tmp;
168         AO_t result;
169
170         __asm__ __volatile__("@AO_fetch_and_add1\n"
171 "1:     ldrex   %0, [%4]\n"             /* get original */
172 "       add     %1, %0, #1\n"           /* increment */
173 "       strex   %2, %1, [%4]\n"         /* store them */
174 "       teq             %2, #0\n"
175 "       bne             1b\n"
176         : "=&r"(result), "=&r"(tmp), "=&r"(flag), "+m"(*p)
177         : "r"(p)
178         : "cc");
179
180         return result;
181 }
182
183 #define AO_HAVE_fetch_and_add1
184
185 /* NEC LE-IT: fetch and sub for ARMv6 */
186 AO_INLINE AO_t
187 AO_fetch_and_sub1(volatile AO_t *p)
188 {
189         unsigned long flag,tmp;
190         AO_t result;
191
192         __asm__ __volatile__("@AO_fetch_and_sub1\n"
193 "1:     ldrex   %0, [%4]\n"             /* get original */
194 "       sub     %1, %0, #1\n"           /* decrement */
195 "       strex   %2, %1, [%4]\n"         /* store them */
196 "       teq             %2, #0\n"
197 "       bne             1b\n"
198         : "=&r"(result), "=&r"(tmp), "=&r"(flag), "+m"(*p)
199         : "r"(p)
200         : "cc");
201
202         return result;
203 }
204
205 #define AO_HAVE_fetch_and_sub1
206
207 /* NEC LE-IT: compare and swap */
208 /* Returns nonzero if the comparison succeeded. */
209 AO_INLINE int
210 AO_compare_and_swap(volatile AO_t *addr,
211                                 AO_t old_val, AO_t new_val)
212 {
213          AO_t result,tmp;
214
215         __asm__ __volatile__("@ AO_compare_and_swap\n"
216 "1:     mov             %0, #2\n"       /* store a flag */
217 "       ldrex   %1, [%3]\n"             /* get original */
218 "       teq             %1, %4\n"       /* see if match */
219 #       ifdef __thumb__
220   "       it            eq\n"
221 #       endif
222 "       strexeq %0, %5, [%3]\n"         /* store new one if matched */
223 "       teq             %0, #1\n"
224 "       beq             1b\n"           /* if update failed, repeat */
225         : "=&r"(result), "=&r"(tmp), "+m"(*addr)
226         : "r"(addr), "r"(old_val), "r"(new_val)
227         : "cc");
228
229         return !(result&2);             /* if succeded, return 1, else 0 */
230 }
231 #define AO_HAVE_compare_and_swap
232
233 AO_INLINE int
234 AO_compare_double_and_swap_double(volatile AO_double_t *addr,
235                                   AO_t old_val1, AO_t old_val2,
236                                   AO_t new_val1, AO_t new_val2)
237 {
238         double_ptr_storage old_val =
239                         ((double_ptr_storage)old_val2 << 32) | old_val1;
240         double_ptr_storage new_val =
241                         ((double_ptr_storage)new_val2 << 32) | new_val1;
242         double_ptr_storage tmp;
243         int result;
244
245         while(1) {
246                 __asm__ __volatile__("@ AO_compare_and_swap_double\n"
247                 "       ldrexd  %0, [%1]\n" /* get original to r1 & r2 */
248                         : "=&r"(tmp)
249                         : "r"(addr)
250                         : "cc");
251                 if(tmp != old_val)      return 0;
252                 __asm__ __volatile__(
253                 "       strexd  %0, %2, [%3]\n" /* store new one if matched */
254                         : "=&r"(result),"+m"(*addr)
255                         : "r"(new_val), "r"(addr)
256                         : "cc");
257                 if(!result)     return 1;
258         }
259 }
260
261 #define AO_HAVE_compare_double_and_swap_double
262
263 #else
264 /* pre ARMv6 architectures ... */
265
266 /* I found a slide set that, if I read it correctly, claims that        */
267 /* Loads followed by either a Load or Store are ordered, but nothing    */
268 /* else is.                                                             */
269 /* It appears that SWP is the only simple memory barrier.               */
270 #include "../all_atomic_load_store.h"
271
272 AO_INLINE AO_TS_VAL_t
273 AO_test_and_set_full(volatile AO_TS_t *addr)
274 {
275   AO_TS_VAL_t oldval;
276   /* SWP on ARM is very similar to XCHG on x86.                 */
277   /* The first operand is the result, the second the value      */
278   /* to be stored.  Both registers must be different from addr. */
279   /* Make the address operand an early clobber output so it     */
280   /* doesn't overlap with the other operands.  The early clobber*/
281   /* on oldval is necessary to prevent the compiler allocating  */
282   /* them to the same register if they are both unused.         */
283   __asm__ __volatile__("swp %0, %2, [%3]"
284                         : "=&r"(oldval), "=&r"(addr)
285                         : "r"(1), "1"(addr)
286                         : "memory");
287   return oldval;
288 }
289
290 #define AO_HAVE_test_and_set_full
291
292 #endif /* __ARM_ARCH_x */