Skip to content

Commit 8263db4

Browse files
paulburtonralfbaechle
authored andcommitted
MIPS: cmpxchg: Implement __cmpxchg() as a function
Replace the macro definition of __cmpxchg() with an inline function, which is easier to read & modify. The cmpxchg() & cmpxchg_local() macros are adjusted to call the new __cmpxchg() function. Signed-off-by: Paul Burton <paul.burton@imgtec.com> Cc: linux-mips@linux-mips.org Patchwork: https://patchwork.linux-mips.org/patch/16353/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 62c6081 commit 8263db4

1 file changed

Lines changed: 32 additions & 27 deletions

File tree

‎arch/mips/include/asm/cmpxchg.h‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* - Get an error at link-time due to the call to the missing function.
3636
*/
37-
extern void __cmpxchg_called_with_bad_pointer(void)
37+
extern unsigned long __cmpxchg_called_with_bad_pointer(void)
3838
__compiletime_error("Bad argument size for cmpxchg");
3939
extern unsigned long __xchg_called_with_bad_pointer(void)
4040
__compiletime_error("Bad argument size for xchg");
@@ -137,38 +137,43 @@ static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int siz
137137
__ret; \
138138
})
139139

140-
#define __cmpxchg(ptr, old, new, pre_barrier, post_barrier) \
140+
static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
141+
unsigned long new, unsigned int size)
142+
{
143+
switch (size) {
144+
case 4:
145+
return __cmpxchg_asm("ll", "sc", (volatile u32 *)ptr, old, new);
146+
147+
case 8:
148+
/* lld/scd are only available for MIPS64 */
149+
if (!IS_ENABLED(CONFIG_64BIT))
150+
return __cmpxchg_called_with_bad_pointer();
151+
152+
return __cmpxchg_asm("lld", "scd", (volatile u64 *)ptr, old, new);
153+
154+
default:
155+
return __cmpxchg_called_with_bad_pointer();
156+
}
157+
}
158+
159+
#define cmpxchg_local(ptr, old, new) \
160+
((__typeof__(*(ptr))) \
161+
__cmpxchg((ptr), \
162+
(unsigned long)(__typeof__(*(ptr)))(old), \
163+
(unsigned long)(__typeof__(*(ptr)))(new), \
164+
sizeof(*(ptr))))
165+
166+
#define cmpxchg(ptr, old, new) \
141167
({ \
142-
__typeof__(ptr) __ptr = (ptr); \
143-
__typeof__(*(ptr)) __old = (old); \
144-
__typeof__(*(ptr)) __new = (new); \
145-
__typeof__(*(ptr)) __res = 0; \
146-
\
147-
pre_barrier; \
148-
\
149-
switch (sizeof(*(__ptr))) { \
150-
case 4: \
151-
__res = __cmpxchg_asm("ll", "sc", __ptr, __old, __new); \
152-
break; \
153-
case 8: \
154-
if (sizeof(long) == 8) { \
155-
__res = __cmpxchg_asm("lld", "scd", __ptr, \
156-
__old, __new); \
157-
break; \
158-
} \
159-
default: \
160-
__cmpxchg_called_with_bad_pointer(); \
161-
break; \
162-
} \
168+
__typeof__(*(ptr)) __res; \
163169
\
164-
post_barrier; \
170+
smp_mb__before_llsc(); \
171+
__res = cmpxchg_local((ptr), (old), (new)); \
172+
smp_llsc_mb(); \
165173
\
166174
__res; \
167175
})
168176

169-
#define cmpxchg(ptr, old, new) __cmpxchg(ptr, old, new, smp_mb__before_llsc(), smp_llsc_mb())
170-
#define cmpxchg_local(ptr, old, new) __cmpxchg(ptr, old, new, , )
171-
172177
#ifdef CONFIG_64BIT
173178
#define cmpxchg64_local(ptr, o, n) \
174179
({ \

0 commit comments

Comments
 (0)