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