2 * Atomic operations that C can't guarantee us. Useful for
3 * resource counting etc.
5 * But use these as seldom as possible since they are slower than
8 * Copyright (C) 2004-2006 Atmel Corporation
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #ifndef __ASM_AVR32_CMPXCHG_H
15 #define __ASM_AVR32_CMPXCHG_H
18 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
20 extern void __xchg_called_with_bad_pointer(void);
22 static inline unsigned long xchg_u32(u32 val
, volatile u32
*m
)
26 asm volatile("xchg %[ret], %[m], %[val]"
27 : [ret
] "=&r"(ret
), "=m"(*m
)
28 : "m"(*m
), [m
] "r"(m
), [val
] "r"(val
)
33 static inline unsigned long __xchg(unsigned long x
,
39 return xchg_u32(x
, ptr
);
41 __xchg_called_with_bad_pointer();
46 static inline unsigned long __cmpxchg_u32(volatile int *m
, unsigned long old
,
53 " ld.w %[ret], %[m]\n"
54 " cp.w %[ret], %[old]\n"
56 " stcond %[m], %[new]\n"
59 : [ret
] "=&r"(ret
), [m
] "=m"(*m
)
60 : "m"(m
), [old
] "ir"(old
), [new] "r"(new)
65 extern unsigned long __cmpxchg_u64_unsupported_on_32bit_kernels(
66 volatile int * m
, unsigned long old
, unsigned long new);
67 #define __cmpxchg_u64 __cmpxchg_u64_unsupported_on_32bit_kernels
69 /* This function doesn't exist, so you'll get a linker error
70 if something tries to do an invalid cmpxchg(). */
71 extern void __cmpxchg_called_with_bad_pointer(void);
73 #define __HAVE_ARCH_CMPXCHG 1
75 static inline unsigned long __cmpxchg(volatile void *ptr
, unsigned long old
,
76 unsigned long new, int size
)
80 return __cmpxchg_u32(ptr
, old
, new);
82 return __cmpxchg_u64(ptr
, old
, new);
85 __cmpxchg_called_with_bad_pointer();
89 #define cmpxchg(ptr, old, new) \
90 ((typeof(*(ptr)))__cmpxchg((ptr), (unsigned long)(old), \
91 (unsigned long)(new), \
94 #include <asm-generic/cmpxchg-local.h>
96 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
98 unsigned long new, int size
)
102 return __cmpxchg_u32(ptr
, old
, new);
104 return __cmpxchg_local_generic(ptr
, old
, new, size
);
110 #define cmpxchg_local(ptr, old, new) \
111 ((typeof(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(old), \
112 (unsigned long)(new), \
115 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
117 #endif /* __ASM_AVR32_CMPXCHG_H */