1 #ifndef _ASM_M32R_CMPXCHG_H
2 #define _ASM_M32R_CMPXCHG_H
6 * Copyright (C) 2001, 2002 Hitoshi Yamamoto
7 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
10 #include <linux/irqflags.h>
11 #include <asm/assembler.h>
12 #include <asm/dcache_clear.h>
14 extern void __xchg_called_with_bad_pointer(void);
16 static __always_inline
unsigned long
17 __xchg(unsigned long x
, volatile void *ptr
, int size
)
20 unsigned long tmp
= 0;
22 local_irq_save(flags
);
27 __asm__
__volatile__ (
30 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
33 __asm__
__volatile__ (
36 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
39 __asm__
__volatile__ (
42 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
44 #else /* CONFIG_SMP */
46 __asm__
__volatile__ (
47 DCACHE_CLEAR("%0", "r4", "%2")
49 "unlock %1, @%2; \n\t"
50 : "=&r" (tmp
) : "r" (x
), "r" (ptr
)
52 #ifdef CONFIG_CHIP_M32700_TS1
54 #endif /* CONFIG_CHIP_M32700_TS1 */
57 #endif /* CONFIG_SMP */
59 __xchg_called_with_bad_pointer();
62 local_irq_restore(flags
);
67 #define xchg(ptr, x) \
68 ((__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), sizeof(*(ptr))))
70 static __always_inline
unsigned long
71 __xchg_local(unsigned long x
, volatile void *ptr
, int size
)
74 unsigned long tmp
= 0;
76 local_irq_save(flags
);
80 __asm__
__volatile__ (
83 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
86 __asm__
__volatile__ (
89 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
92 __asm__
__volatile__ (
95 : "=&r" (tmp
) : "r" (x
), "r" (ptr
) : "memory");
98 __xchg_called_with_bad_pointer();
101 local_irq_restore(flags
);
106 #define xchg_local(ptr, x) \
107 ((__typeof__(*(ptr)))__xchg_local((unsigned long)(x), (ptr), \
110 static inline unsigned long
111 __cmpxchg_u32(volatile unsigned int *p
, unsigned int old
, unsigned int new)
116 local_irq_save(flags
);
117 __asm__
__volatile__ (
118 DCACHE_CLEAR("%0", "r4", "%1")
119 M32R_LOCK
" %0, @%1; \n"
120 " bne %0, %2, 1f; \n"
121 M32R_UNLOCK
" %3, @%1; \n"
125 M32R_UNLOCK
" %0, @%1; \n"
129 : "r" (p
), "r" (old
), "r" (new)
131 #ifdef CONFIG_CHIP_M32700_TS1
133 #endif /* CONFIG_CHIP_M32700_TS1 */
135 local_irq_restore(flags
);
140 static inline unsigned long
141 __cmpxchg_local_u32(volatile unsigned int *p
, unsigned int old
,
147 local_irq_save(flags
);
148 __asm__
__volatile__ (
149 DCACHE_CLEAR("%0", "r4", "%1")
151 " bne %0, %2, 1f; \n"
160 : "r" (p
), "r" (old
), "r" (new)
162 #ifdef CONFIG_CHIP_M32700_TS1
164 #endif /* CONFIG_CHIP_M32700_TS1 */
166 local_irq_restore(flags
);
171 /* This function doesn't exist, so you'll get a linker error
172 if something tries to do an invalid cmpxchg(). */
173 extern void __cmpxchg_called_with_bad_pointer(void);
175 static inline unsigned long
176 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new, int size
)
180 return __cmpxchg_u32(ptr
, old
, new);
181 #if 0 /* we don't have __cmpxchg_u64 */
183 return __cmpxchg_u64(ptr
, old
, new);
186 __cmpxchg_called_with_bad_pointer();
190 #define cmpxchg(ptr, o, n) \
191 ((__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)(o), \
192 (unsigned long)(n), sizeof(*(ptr))))
194 #include <asm-generic/cmpxchg-local.h>
196 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
198 unsigned long new, int size
)
202 return __cmpxchg_local_u32(ptr
, old
, new);
204 return __cmpxchg_local_generic(ptr
, old
, new, size
);
211 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
214 #define cmpxchg_local(ptr, o, n) \
215 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
216 (unsigned long)(n), sizeof(*(ptr))))
217 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
219 #endif /* _ASM_M32R_CMPXCHG_H */