1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_M32R_CMPXCHG_H
3 #define _ASM_M32R_CMPXCHG_H
7 * Copyright (C) 2001, 2002 Hitoshi Yamamoto
8 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
11 #include <linux/irqflags.h>
12 #include <asm/assembler.h>
13 #include <asm/dcache_clear.h>
15 extern void __xchg_called_with_bad_pointer(void);
17 static __always_inline
unsigned long
18 __xchg(unsigned long x
, volatile void *ptr
, int size
)
21 unsigned long tmp
= 0;
23 local_irq_save(flags
);
28 __asm__
__volatile__ (
31 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
34 __asm__
__volatile__ (
37 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
40 __asm__
__volatile__ (
43 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
45 #else /* CONFIG_SMP */
47 __asm__
__volatile__ (
48 DCACHE_CLEAR("%0", "r4", "%2")
50 "unlock %1, @%2; \n\t"
51 : "=&r" (tmp
) : "r" (x
), "r" (ptr
)
53 #ifdef CONFIG_CHIP_M32700_TS1
55 #endif /* CONFIG_CHIP_M32700_TS1 */
58 #endif /* CONFIG_SMP */
60 __xchg_called_with_bad_pointer();
63 local_irq_restore(flags
);
68 #define xchg(ptr, x) ({ \
69 ((__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), \
73 static __always_inline
unsigned long
74 __xchg_local(unsigned long x
, volatile void *ptr
, int size
)
77 unsigned long tmp
= 0;
79 local_irq_save(flags
);
83 __asm__
__volatile__ (
86 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
89 __asm__
__volatile__ (
92 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
95 __asm__
__volatile__ (
98 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
101 __xchg_called_with_bad_pointer();
104 local_irq_restore(flags
);
109 #define xchg_local(ptr, x) \
110 ((__typeof__(*(ptr)))__xchg_local((unsigned long)(x), (ptr), \
113 static inline unsigned long
114 __cmpxchg_u32(volatile unsigned int *p
, unsigned int old
, unsigned int new)
119 local_irq_save(flags
);
120 __asm__
__volatile__ (
121 DCACHE_CLEAR("%0", "r4", "%1")
122 M32R_LOCK
" %0, @%1; \n"
123 " bne %0, %2, 1f; \n"
124 M32R_UNLOCK
" %3, @%1; \n"
128 M32R_UNLOCK
" %0, @%1; \n"
132 : "r" (p
), "r" (old
), "r" (new)
134 #ifdef CONFIG_CHIP_M32700_TS1
136 #endif /* CONFIG_CHIP_M32700_TS1 */
138 local_irq_restore(flags
);
143 static inline unsigned long
144 __cmpxchg_local_u32(volatile unsigned int *p
, unsigned int old
,
150 local_irq_save(flags
);
151 __asm__
__volatile__ (
152 DCACHE_CLEAR("%0", "r4", "%1")
154 " bne %0, %2, 1f; \n"
163 : "r" (p
), "r" (old
), "r" (new)
165 #ifdef CONFIG_CHIP_M32700_TS1
167 #endif /* CONFIG_CHIP_M32700_TS1 */
169 local_irq_restore(flags
);
174 /* This function doesn't exist, so you'll get a linker error
175 if something tries to do an invalid cmpxchg(). */
176 extern void __cmpxchg_called_with_bad_pointer(void);
178 static inline unsigned long
179 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new, int size
)
183 return __cmpxchg_u32(ptr
, old
, new);
184 #if 0 /* we don't have __cmpxchg_u64 */
186 return __cmpxchg_u64(ptr
, old
, new);
189 __cmpxchg_called_with_bad_pointer();
193 #define cmpxchg(ptr, o, n) ({ \
194 ((__typeof__(*(ptr))) \
195 __cmpxchg((ptr), (unsigned long)(o), \
196 (unsigned long)(n), \
200 #include <asm-generic/cmpxchg-local.h>
202 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
204 unsigned long new, int size
)
208 return __cmpxchg_local_u32(ptr
, old
, new);
210 return __cmpxchg_local_generic(ptr
, old
, new, size
);
217 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
220 #define cmpxchg_local(ptr, o, n) \
221 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
222 (unsigned long)(n), sizeof(*(ptr))))
223 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
225 #endif /* _ASM_M32R_CMPXCHG_H */