2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #ifndef __ASM_ARC_CMPXCHG_H
10 #define __ASM_ARC_CMPXCHG_H
12 #include <linux/types.h>
14 #include <asm/barrier.h>
17 #ifdef CONFIG_ARC_HAS_LLSC
19 static inline unsigned long
20 __cmpxchg(volatile void *ptr
, unsigned long expected
, unsigned long new)
25 * Explicit full memory barrier needed before/after as
26 * LLOCK/SCOND thmeselves don't provide any such semantics
31 "1: llock %0, [%1] \n"
36 : "=&r"(prev
) /* Early clobber, to prevent reg reuse */
37 : "r"(ptr
), /* Not "m": llock only supports reg direct addr mode */
39 "r"(new) /* can't be "ir". scond can't take LIMM for "b" */
40 : "cc", "memory"); /* so that gcc knows memory is being written here */
47 #elif !defined(CONFIG_ARC_PLAT_EZNPS)
49 static inline unsigned long
50 __cmpxchg(volatile void *ptr
, unsigned long expected
, unsigned long new)
54 volatile unsigned long *p
= ptr
;
57 * spin lock/unlock provide the needed smp_mb() before/after
59 atomic_ops_lock(flags
);
63 atomic_ops_unlock(flags
);
67 #else /* CONFIG_ARC_PLAT_EZNPS */
69 static inline unsigned long
70 __cmpxchg(volatile void *ptr
, unsigned long expected
, unsigned long new)
73 * Explicit full memory barrier needed before/after
77 write_aux_reg(CTOP_AUX_GPA1
, expected
);
85 : "r"(ptr
), "i"(CTOP_INST_EXC_DI_R2_R2_R3
)
86 : "r2", "r3", "memory");
93 #endif /* CONFIG_ARC_HAS_LLSC */
95 #define cmpxchg(ptr, o, n) ((typeof(*(ptr)))__cmpxchg((ptr), \
96 (unsigned long)(o), (unsigned long)(n)))
99 * atomic_cmpxchg is same as cmpxchg
100 * LLSC: only different in data-type, semantics are exactly same
101 * !LLSC: cmpxchg() has to use an external lock atomic_ops_lock to guarantee
102 * semantics, and this lock also happens to be used by atomic_*()
104 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
107 #ifndef CONFIG_ARC_PLAT_EZNPS
110 * xchg (reg with memory) based on "Native atomic" EX insn
112 static inline unsigned long __xchg(unsigned long val
, volatile void *ptr
,
115 extern unsigned long __xchg_bad_pointer(void);
121 __asm__
__volatile__(
131 return __xchg_bad_pointer();
134 #define _xchg(ptr, with) ((typeof(*(ptr)))__xchg((unsigned long)(with), (ptr), \
138 * xchg() maps directly to ARC EX instruction which guarantees atomicity.
139 * However in !LLSC config, it also needs to be use @atomic_ops_lock spinlock
140 * due to a subtle reason:
141 * - For !LLSC, cmpxchg() needs to use that lock (see above) and there is lot
142 * of kernel code which calls xchg()/cmpxchg() on same data (see llist.h)
143 * Hence xchg() needs to follow same locking rules.
145 * Technically the lock is also needed for UP (boils down to irq save/restore)
146 * but we can cheat a bit since cmpxchg() atomic_ops_lock() would cause irqs to
147 * be disabled thus can't possibly be interrpted/preempted/clobbered by xchg()
148 * Other way around, xchg is one instruction anyways, so can't be interrupted
152 #if !defined(CONFIG_ARC_HAS_LLSC) && defined(CONFIG_SMP)
154 #define xchg(ptr, with) \
156 unsigned long flags; \
157 typeof(*(ptr)) old_val; \
159 atomic_ops_lock(flags); \
160 old_val = _xchg(ptr, with); \
161 atomic_ops_unlock(flags); \
167 #define xchg(ptr, with) _xchg(ptr, with)
171 #else /* CONFIG_ARC_PLAT_EZNPS */
173 static inline unsigned long __xchg(unsigned long val
, volatile void *ptr
,
176 extern unsigned long __xchg_bad_pointer(void);
181 * Explicit full memory barrier needed before/after
185 __asm__
__volatile__(
191 : "r"(ptr
), "i"(CTOP_INST_XEX_DI_R2_R2_R3
)
192 : "r2", "r3", "memory");
198 return __xchg_bad_pointer();
201 #define xchg(ptr, with) ((typeof(*(ptr)))__xchg((unsigned long)(with), (ptr), \
204 #endif /* CONFIG_ARC_PLAT_EZNPS */
207 * "atomic" variant of xchg()
208 * REQ: It needs to follow the same serialization rules as other atomic_xxx()
209 * Since xchg() doesn't always do that, it would seem that following defintion
210 * is incorrect. But here's the rationale:
211 * SMP : Even xchg() takes the atomic_ops_lock, so OK.
212 * LLSC: atomic_ops_lock are not relevant at all (even if SMP, since LLSC
213 * is natively "SMP safe", no serialization required).
214 * UP : other atomics disable IRQ, so no way a difft ctxt atomic_xchg()
215 * could clobber them. atomic_xchg() itself would be 1 insn, so it
216 * can't be clobbered by others. Thus no serialization required when
217 * atomic_xchg is involved.
219 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))