Merge branch 'x86/microcode' into x86/urgent, to pick up cleanup
[linux/fpc-iii.git] / arch / avr32 / include / asm / cmpxchg.h
blob572739b4c4b4ebd01857c6d889fcbdf2688c0710
1 /*
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
6 * regular operations.
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
17 #define xchg(ptr,x) \
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)
24 u32 ret;
26 asm volatile("xchg %[ret], %[m], %[val]"
27 : [ret] "=&r"(ret), "=m"(*m)
28 : "m"(*m), [m] "r"(m), [val] "r"(val)
29 : "memory");
30 return ret;
33 static inline unsigned long __xchg(unsigned long x,
34 volatile void *ptr,
35 int size)
37 switch(size) {
38 case 4:
39 return xchg_u32(x, ptr);
40 default:
41 __xchg_called_with_bad_pointer();
42 return x;
46 static inline unsigned long __cmpxchg_u32(volatile int *m, unsigned long old,
47 unsigned long new)
49 __u32 ret;
51 asm volatile(
52 "1: ssrf 5\n"
53 " ld.w %[ret], %[m]\n"
54 " cp.w %[ret], %[old]\n"
55 " brne 2f\n"
56 " stcond %[m], %[new]\n"
57 " brne 1b\n"
58 "2:\n"
59 : [ret] "=&r"(ret), [m] "=m"(*m)
60 : "m"(m), [old] "Ks21r"(old), [new] "r"(new)
61 : "memory", "cc");
62 return ret;
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 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
74 unsigned long new, int size)
76 switch (size) {
77 case 4:
78 return __cmpxchg_u32(ptr, old, new);
79 case 8:
80 return __cmpxchg_u64(ptr, old, new);
83 __cmpxchg_called_with_bad_pointer();
84 return old;
87 #define cmpxchg(ptr, old, new) \
88 ((typeof(*(ptr)))__cmpxchg((ptr), (unsigned long)(old), \
89 (unsigned long)(new), \
90 sizeof(*(ptr))))
92 #include <asm-generic/cmpxchg-local.h>
94 static inline unsigned long __cmpxchg_local(volatile void *ptr,
95 unsigned long old,
96 unsigned long new, int size)
98 switch (size) {
99 case 4:
100 return __cmpxchg_u32(ptr, old, new);
101 default:
102 return __cmpxchg_local_generic(ptr, old, new, size);
105 return old;
108 #define cmpxchg_local(ptr, old, new) \
109 ((typeof(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(old), \
110 (unsigned long)(new), \
111 sizeof(*(ptr))))
113 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
115 #endif /* __ASM_AVR32_CMPXCHG_H */