8a361834dc25887e658107a3bf5dbf5f8142752d
[~shefty/rdma-dev.git] / include / asm-generic / cmpxchg.h
1 /*
2  * Generic UP xchg and cmpxchg using interrupt disablement.  Does not
3  * support SMP.
4  */
5
6 #ifndef __ASM_GENERIC_CMPXCHG_H
7 #define __ASM_GENERIC_CMPXCHG_H
8
9 #ifdef CONFIG_SMP
10 #error "Cannot use generic cmpxchg on SMP"
11 #endif
12
13 #include <linux/irqflags.h>
14
15 #ifndef xchg
16
17 /*
18  * This function doesn't exist, so you'll get a linker error if
19  * something tries to do an invalidly-sized xchg().
20  */
21 extern void __xchg_called_with_bad_pointer(void);
22
23 static inline
24 unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
25 {
26         unsigned long ret, flags;
27
28         switch (size) {
29         case 1:
30 #ifdef __xchg_u8
31                 return __xchg_u8(x, ptr);
32 #else
33                 local_irq_save(flags);
34                 ret = *(volatile u8 *)ptr;
35                 *(volatile u8 *)ptr = x;
36                 local_irq_restore(flags);
37                 return ret;
38 #endif /* __xchg_u8 */
39
40         case 2:
41 #ifdef __xchg_u16
42                 return __xchg_u16(x, ptr);
43 #else
44                 local_irq_save(flags);
45                 ret = *(volatile u16 *)ptr;
46                 *(volatile u16 *)ptr = x;
47                 local_irq_restore(flags);
48                 return ret;
49 #endif /* __xchg_u16 */
50
51         case 4:
52 #ifdef __xchg_u32
53                 return __xchg_u32(x, ptr);
54 #else
55                 local_irq_save(flags);
56                 ret = *(volatile u32 *)ptr;
57                 *(volatile u32 *)ptr = x;
58                 local_irq_restore(flags);
59                 return ret;
60 #endif /* __xchg_u32 */
61
62 #ifdef CONFIG_64BIT
63         case 8:
64 #ifdef __xchg_u64
65                 return __xchg_u64(x, ptr);
66 #else
67                 local_irq_save(flags);
68                 ret = *(volatile u64 *)ptr;
69                 *(volatile u64 *)ptr = x;
70                 local_irq_restore(flags);
71                 return ret;
72 #endif /* __xchg_u64 */
73 #endif /* CONFIG_64BIT */
74
75         default:
76                 __xchg_called_with_bad_pointer();
77                 return x;
78         }
79 }
80
81 #define xchg(ptr, x) \
82         ((__typeof__(*(ptr))) __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))))
83
84 #endif /* xchg */
85
86 /*
87  * Atomic compare and exchange.
88  *
89  * Do not define __HAVE_ARCH_CMPXCHG because we want to use it to check whether
90  * a cmpxchg primitive faster than repeated local irq save/restore exists.
91  */
92 #include <asm-generic/cmpxchg-local.h>
93
94 #define cmpxchg(ptr, o, n)      cmpxchg_local((ptr), (o), (n))
95 #define cmpxchg64(ptr, o, n)    cmpxchg64_local((ptr), (o), (n))
96
97 #endif /* __ASM_GENERIC_CMPXCHG_H */