2 * 1,2 and 4 byte cmpxchg and xchg implementations for OpenRISC.
4 * Copyright (C) 2014 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
5 * Copyright (C) 2017 Stafford Horne <shorne@gmail.com>
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
12 * The portable implementations of 1 and 2 byte xchg and cmpxchg using a 4
13 * byte cmpxchg is sourced heavily from the sh and mips implementations.
16 #ifndef __ASM_OPENRISC_CMPXCHG_H
17 #define __ASM_OPENRISC_CMPXCHG_H
19 #include <linux/types.h>
20 #include <linux/bitops.h>
22 #define __HAVE_ARCH_CMPXCHG 1
24 static inline unsigned long cmpxchg_u32(volatile void *ptr,
25 unsigned long old, unsigned long new)
28 "1: l.lwa %0, 0(%1) \n"
37 : "r"(ptr), "r"(old), "r"(new)
43 static inline unsigned long xchg_u32(volatile void *ptr,
47 "1: l.lwa %0, 0(%1) \n"
58 static inline u32 cmpxchg_small(volatile void *ptr, u32 old, u32 new,
61 int off = (unsigned long)ptr % sizeof(u32);
62 volatile u32 *p = ptr - off;
64 int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
66 int bitoff = off * BITS_PER_BYTE;
68 u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
69 u32 load32, old32, new32;
72 load32 = READ_ONCE(*p);
75 ret = (load32 & bitmask) >> bitoff;
79 old32 = (load32 & ~bitmask) | (old << bitoff);
80 new32 = (load32 & ~bitmask) | (new << bitoff);
82 /* Do 32 bit cmpxchg */
83 load32 = cmpxchg_u32(p, old32, new32);
91 static inline u32 xchg_small(volatile void *ptr, u32 x, int size)
93 int off = (unsigned long)ptr % sizeof(u32);
94 volatile u32 *p = ptr - off;
96 int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
98 int bitoff = off * BITS_PER_BYTE;
100 u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
105 oldv = READ_ONCE(*p);
106 ret = (oldv & bitmask) >> bitoff;
107 newv = (oldv & ~bitmask) | (x << bitoff);
108 } while (cmpxchg_u32(p, oldv, newv) != oldv);
114 * This function doesn't exist, so you'll get a linker error
115 * if something tries to do an invalid cmpxchg().
117 extern unsigned long __cmpxchg_called_with_bad_pointer(void)
118 __compiletime_error("Bad argument size for cmpxchg");
120 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
121 unsigned long new, int size)
126 return cmpxchg_small(ptr, old, new, size);
128 return cmpxchg_u32(ptr, old, new);
130 return __cmpxchg_called_with_bad_pointer();
134 #define cmpxchg(ptr, o, n) \
136 (__typeof__(*(ptr))) __cmpxchg((ptr), \
137 (unsigned long)(o), \
138 (unsigned long)(n), \
143 * This function doesn't exist, so you'll get a linker error if
144 * something tries to do an invalidly-sized xchg().
146 extern unsigned long __xchg_called_with_bad_pointer(void)
147 __compiletime_error("Bad argument size for xchg");
149 static inline unsigned long __xchg(volatile void *ptr, unsigned long with,
155 return xchg_small(ptr, with, size);
157 return xchg_u32(ptr, with);
159 return __xchg_called_with_bad_pointer();
163 #define xchg(ptr, with) \
165 (__typeof__(*(ptr))) __xchg((ptr), \
166 (unsigned long)(with), \
170 #endif /* __ASM_OPENRISC_CMPXCHG_H */