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.
12 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
13 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
22 #include "../read_ordered.h"
23 #include "../test_and_set_t_is_ao_t.h" /* Probably suboptimal */
25 #if __TARGET_ARCH_ARM < 6
26 Dont use with ARM instruction sets lower than v6
29 /* NEC LE-IT: ARMv6 is the first architecture providing support for simple LL/SC
30 * A data memory barrier must be raised via CP15 command (see documentation).
32 * ARMv7 is compatible to ARMv6 but has a simpler command for issuing a
33 * memory barrier (DMB). Raising it via CP15 should still work as told me by the
34 * support engineers. If it turns out to be much quicker than we should implement
35 * custom code for ARMv7 using the asm { dmb } command.
37 * If only a single processor is used, we can define AO_UNIPROCESSOR
38 * and do not need to access CP15 for ensuring a DMB at all.
44 # ifndef AO_UNIPROCESSOR
46 /* issue an data memory barrier (keeps ordering of memory transactions */
47 /* before and after this operation) */
48 __asm { mcr p15,0,dest,c7,c10,5 } ;
52 #define AO_HAVE_nop_full
55 AO_load(volatile AO_t *addr)
57 /* Cast away the volatile in case it adds fence semantics. */
58 return (*(AO_t *)addr);
62 /* NEC LE-IT: atomic "store" - according to ARM documentation this is
63 * the only safe way to set variables also used in LL/SC environment.
64 * A direct write won't be recognized by the LL/SC construct in other CPUs.
66 * HB: Based on subsequent discussion, I think it would be OK to use an
67 * ordinary store here if we knew that interrupt handlers always cleared
68 * the reservation. They should, but there is some doubt that this is
69 * currently always the case for e.g. Linux.
71 AO_INLINE void AO_store(volatile AO_t *addr, AO_t value)
78 strex tmp, value, [addr]
85 /* NEC LE-IT: replace the SWAP as recommended by ARM:
87 "Applies to: ARM11 Cores
88 Though the SWP instruction will still work with ARM V6 cores, it is recommended
89 to use the new V6 synchronization instructions. The SWP instruction produces
90 locked read and write accesses which are atomic, i.e. another operation cannot
91 be done between these locked accesses which ties up external bus (AHB,AXI)
92 bandwidth and can increase worst case interrupt latencies. LDREX,STREX are
93 more flexible, other instructions can be done between the LDREX and STREX accesses.
97 AO_test_and_set(volatile AO_TS_t *addr) {
101 unsigned long one = 1;
105 strex tmp, one, [addr]
113 #define AO_HAVE_test_and_set
115 /* NEC LE-IT: fetch and add for ARMv6 */
117 AO_fetch_and_add(volatile AO_t *p, AO_t incr)
119 unsigned long tmp,tmp2;
125 add tmp, incr, result
133 #define AO_HAVE_fetch_and_add
135 /* NEC LE-IT: fetch and add1 for ARMv6 */
137 AO_fetch_and_add1(volatile AO_t *p)
139 unsigned long tmp,tmp2;
154 #define AO_HAVE_fetch_and_add1
156 /* NEC LE-IT: fetch and sub for ARMv6 */
158 AO_fetch_and_sub1(volatile AO_t *p)
160 unsigned long tmp,tmp2;
175 #define AO_HAVE_fetch_and_sub1
177 /* NEC LE-IT: compare and swap */
178 /* Returns nonzero if the comparison succeeded. */
180 AO_compare_and_swap(volatile AO_t *addr,
181 AO_t old_val, AO_t new_val)
190 strexeq result, new_val, [addr]
195 return (result^2)>>1;
197 #define AO_HAVE_compare_and_swap
199 #endif // __TARGET_ARCH_ARM