implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / libatomic_ops / src / atomic_ops / sysdeps / armcc / arm_v6.h
1 /*
2  * Copyright (c) 2007 by NEC LE-IT:               All rights reserved.
3  * A transcription of ARMv6 atomic operations for the ARM Realview Toolchain.
4  * This code works with armcc from RVDS 3.1
5  * This is based on work in gcc/arm.h by
6  *   Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
7  *   Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
8  *   Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved.
9  *
10  *
11  *
12  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
13  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
14  *
15  * Permission is hereby granted to use or copy this program
16  * for any purpose,  provided the above notices are retained on all copies.
17  * Permission to modify the code and to distribute modified code is granted,
18  * provided the above notices are retained, and a notice that the code was
19  * modified is included with the above copyright notice.
20  *
21  */
22 #include "../read_ordered.h"
23 #include "../test_and_set_t_is_ao_t.h" /* Probably suboptimal */
24
25 #if __TARGET_ARCH_ARM < 6
26 Dont use with ARM instruction sets lower than v6
27 #else
28
29 #include "../standard_ao_double_t.h"
30
31 /* NEC LE-IT: ARMv6 is the first architecture providing support for simple LL/SC
32  * A data memory barrier must be raised via CP15 command (see documentation).
33  *
34  * ARMv7 is compatible to ARMv6 but has a simpler command for issuing a
35  * memory barrier (DMB). Raising it via CP15 should still work as told me by the
36  * support engineers. If it turns out to be much quicker than we should implement
37  * custom code for ARMv7 using the asm { dmb } command.
38  *
39  * If only a single processor is used, we can define AO_UNIPROCESSOR
40  * and do not need to access CP15 for ensuring a DMB at all.
41 */
42
43 AO_INLINE void
44 AO_nop_full(void)
45 {
46 # ifndef AO_UNIPROCESSOR
47     unsigned int dest=0;
48     /* issue an data memory barrier (keeps ordering of memory transactions */
49     /* before and after this operation)                                    */
50     __asm {
51             mcr p15,0,dest,c7,c10,5
52             };
53 # endif
54 }
55 #define AO_HAVE_nop_full
56
57 AO_INLINE AO_t
58 AO_load(const volatile AO_t *addr)
59 {
60         /* Cast away the volatile in case it adds fence semantics */
61         return (*(const AO_t *)addr);
62 }
63 #define AO_HAVE_load
64
65 /* NEC LE-IT: atomic "store" - according to ARM documentation this is
66  * the only safe way to set variables also used in LL/SC environment.
67  * A direct write won't be recognized by the LL/SC construct in other CPUs.
68  *
69  * HB: Based on subsequent discussion, I think it would be OK to use an
70  * ordinary store here if we knew that interrupt handlers always cleared
71  * the reservation.  They should, but there is some doubt that this is
72  * currently always the case for e.g. Linux.
73 */
74 AO_INLINE void AO_store(volatile AO_t *addr, AO_t value)
75 {
76         unsigned long tmp;
77
78 retry:
79 __asm {
80         ldrex   tmp, [addr]
81         strex   tmp, value, [addr]
82         teq     tmp, #0
83         bne     retry
84         };
85 }
86 #define AO_HAVE_store
87
88 /* NEC LE-IT: replace the SWAP as recommended by ARM:
89
90    "Applies to: ARM11 Cores
91         Though the SWP instruction will still work with ARM V6 cores, it is recommended
92         to use the new V6 synchronization instructions. The SWP instruction produces
93         locked read and write accesses which are atomic, i.e. another operation cannot
94         be done between these locked accesses which ties up external bus (AHB,AXI)
95         bandwidth and can increase worst case interrupt latencies. LDREX,STREX are
96         more flexible, other instructions can be done between the LDREX and STREX accesses.
97    "
98 */
99 AO_INLINE AO_TS_VAL_t
100 AO_test_and_set(volatile AO_TS_t *addr) {
101
102         AO_TS_VAL_t oldval;
103         unsigned long tmp;
104         unsigned long one = 1;
105 retry:
106 __asm {
107         ldrex   oldval, [addr]
108         strex   tmp, one, [addr]
109         teq     tmp, #0
110         bne     retry
111         }
112
113         return oldval;
114 }
115 #define AO_HAVE_test_and_set
116
117 /* NEC LE-IT: fetch and add for ARMv6 */
118 AO_INLINE AO_t
119 AO_fetch_and_add(volatile AO_t *p, AO_t incr)
120 {
121         unsigned long tmp,tmp2;
122         AO_t result;
123
124 retry:
125 __asm {
126         ldrex   result, [p]
127         add     tmp, incr, result
128         strex   tmp2, tmp, [p]
129         teq     tmp2, #0
130         bne     retry
131         }
132
133         return result;
134 }
135 #define AO_HAVE_fetch_and_add
136
137 /* NEC LE-IT: fetch and add1 for ARMv6 */
138 AO_INLINE AO_t
139 AO_fetch_and_add1(volatile AO_t *p)
140 {
141         unsigned long tmp,tmp2;
142         AO_t result;
143
144 retry:
145 __asm {
146         ldrex   result, [p]
147         add     tmp, result, #1
148         strex   tmp2, tmp, [p]
149         teq     tmp2, #0
150         bne     retry
151         }
152
153         return result;
154 }
155 #define AO_HAVE_fetch_and_add1
156
157 /* NEC LE-IT: fetch and sub for ARMv6 */
158 AO_INLINE AO_t
159 AO_fetch_and_sub1(volatile AO_t *p)
160 {
161         unsigned long tmp,tmp2;
162         AO_t result;
163
164 retry:
165 __asm {
166         ldrex   result, [p]
167         sub     tmp, result, #1
168         strex   tmp2, tmp, [p]
169         teq     tmp2, #0
170         bne     retry
171         }
172
173         return result;
174 }
175 #define AO_HAVE_fetch_and_sub1
176
177 /* NEC LE-IT: compare and swap */
178 /* Returns nonzero if the comparison succeeded. */
179 AO_INLINE int
180 AO_compare_and_swap(volatile AO_t *addr, AO_t old_val, AO_t new_val)
181 {
182          AO_t result,tmp;
183
184 retry:
185 __asm__ {
186         mov     result, #2
187         ldrex   tmp, [addr]
188         teq     tmp, old_val
189 #     ifdef __thumb__
190         it      eq
191 #     endif
192         strexeq result, new_val, [addr]
193         teq     result, #1
194         beq     retry
195         }
196
197         return !(result&2);
198 }
199 #define AO_HAVE_compare_and_swap
200
201 /* helper functions for the Realview compiler: LDREXD is not usable
202  * with inline assembler, so use the "embedded" assembler as
203  * suggested by ARM Dev. support (June 2008). */
204 __asm inline double_ptr_storage load_ex(volatile AO_double_t *addr) {
205         LDREXD r0,r1,[r0]
206 }
207
208 __asm inline int store_ex(AO_t val1, AO_t val2, volatile AO_double_t *addr) {
209         STREXD r3,r0,r1,[r2]
210         MOV    r0,r3
211 }
212
213 AO_INLINE int
214 AO_compare_double_and_swap_double(volatile AO_double_t *addr,
215                                   AO_t old_val1, AO_t old_val2,
216                                   AO_t new_val1, AO_t new_val2)
217 {
218         double_ptr_storage old_val =
219                         ((double_ptr_storage)old_val2 << 32) | old_val1;
220         double_ptr_storage tmp;
221         int result;
222
223         while(1) {
224                 tmp = load_ex(addr);
225                 if(tmp != old_val)      return 0;
226                 result = store_ex(new_val1, new_val2, addr);
227                 if(!result)     return 1;
228         }
229 }
230 #define AO_HAVE_compare_double_and_swap_double
231
232 #endif // __TARGET_ARCH_ARM