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 */
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 #endif /* CONFIG_ARC_HAS_LLSC */
69 #define cmpxchg(ptr, o, n) ((typeof(*(ptr)))__cmpxchg((ptr), \
70 (unsigned long)(o), (unsigned long)(n)))
73 * Since not supported natively, ARC cmpxchg() uses atomic_ops_lock (UP/SMP)
74 * just to gaurantee semantics.
75 * atomic_cmpxchg() needs to use the same locks as it's other atomic siblings
76 * which also happens to be atomic_ops_lock.
78 * Thus despite semantically being different, implementation of atomic_cmpxchg()
79 * is same as cmpxchg().
81 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
85 * xchg (reg with memory) based on "Native atomic" EX insn
87 static inline unsigned long __xchg(unsigned long val
, volatile void *ptr
,
90 extern unsigned long __xchg_bad_pointer(void);
106 return __xchg_bad_pointer();
109 #define _xchg(ptr, with) ((typeof(*(ptr)))__xchg((unsigned long)(with), (ptr), \
113 * xchg() maps directly to ARC EX instruction which guarantees atomicity.
114 * However in !LLSC config, it also needs to be use @atomic_ops_lock spinlock
115 * due to a subtle reason:
116 * - For !LLSC, cmpxchg() needs to use that lock (see above) and there is lot
117 * of kernel code which calls xchg()/cmpxchg() on same data (see llist.h)
118 * Hence xchg() needs to follow same locking rules.
120 * Technically the lock is also needed for UP (boils down to irq save/restore)
121 * but we can cheat a bit since cmpxchg() atomic_ops_lock() would cause irqs to
122 * be disabled thus can't possibly be interrpted/preempted/clobbered by xchg()
123 * Other way around, xchg is one instruction anyways, so can't be interrupted
127 #if !defined(CONFIG_ARC_HAS_LLSC) && defined(CONFIG_SMP)
129 #define xchg(ptr, with) \
131 unsigned long flags; \
132 typeof(*(ptr)) old_val; \
134 atomic_ops_lock(flags); \
135 old_val = _xchg(ptr, with); \
136 atomic_ops_unlock(flags); \
142 #define xchg(ptr, with) _xchg(ptr, with)
147 * "atomic" variant of xchg()
148 * REQ: It needs to follow the same serialization rules as other atomic_xxx()
149 * Since xchg() doesn't always do that, it would seem that following defintion
150 * is incorrect. But here's the rationale:
151 * SMP : Even xchg() takes the atomic_ops_lock, so OK.
152 * LLSC: atomic_ops_lock are not relevent at all (even if SMP, since LLSC
153 * is natively "SMP safe", no serialization required).
154 * UP : other atomics disable IRQ, so no way a difft ctxt atomic_xchg()
155 * could clobber them. atomic_xchg() itself would be 1 insn, so it
156 * can't be clobbered by others. Thus no serialization required when
157 * atomic_xchg is involved.
159 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))