GNU Linux-libre 4.9.318-gnu1
[releases.git] / arch / parisc / include / asm / cmpxchg.h
1 /*
2  * forked from parisc asm/atomic.h which was:
3  *      Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
4  *      Copyright (C) 2006 Kyle McMartin <kyle@parisc-linux.org>
5  */
6
7 #ifndef _ASM_PARISC_CMPXCHG_H_
8 #define _ASM_PARISC_CMPXCHG_H_
9
10 /* This should get optimized out since it's never called.
11 ** Or get a link error if xchg is used "wrong".
12 */
13 extern void __xchg_called_with_bad_pointer(void);
14
15 /* __xchg32/64 defined in arch/parisc/lib/bitops.c */
16 extern unsigned long __xchg8(char, char *);
17 extern unsigned long __xchg32(int, int *);
18 #ifdef CONFIG_64BIT
19 extern unsigned long __xchg64(unsigned long, unsigned long *);
20 #endif
21
22 /* optimizer better get rid of switch since size is a constant */
23 static inline unsigned long
24 __xchg(unsigned long x, __volatile__ void *ptr, int size)
25 {
26         switch (size) {
27 #ifdef CONFIG_64BIT
28         case 8: return __xchg64(x, (unsigned long *) ptr);
29 #endif
30         case 4: return __xchg32((int) x, (int *) ptr);
31         case 1: return __xchg8((char) x, (char *) ptr);
32         }
33         __xchg_called_with_bad_pointer();
34         return x;
35 }
36
37 /*
38 ** REVISIT - Abandoned use of LDCW in xchg() for now:
39 ** o need to test sizeof(*ptr) to avoid clearing adjacent bytes
40 ** o and while we are at it, could CONFIG_64BIT code use LDCD too?
41 **
42 **      if (__builtin_constant_p(x) && (x == NULL))
43 **              if (((unsigned long)p & 0xf) == 0)
44 **                      return __ldcw(p);
45 */
46 #define xchg(ptr, x)                                                    \
47 ({                                                                      \
48         __typeof__(*(ptr)) __ret;                                       \
49         __typeof__(*(ptr)) _x_ = (x);                                   \
50         __ret = (__typeof__(*(ptr)))                                    \
51                 __xchg((unsigned long)_x_, (ptr), sizeof(*(ptr)));      \
52         __ret;                                                          \
53 })
54
55 /* bug catcher for when unsupported size is used - won't link */
56 extern void __cmpxchg_called_with_bad_pointer(void);
57
58 /* __cmpxchg_u32/u64 defined in arch/parisc/lib/bitops.c */
59 extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old,
60                                    unsigned int new_);
61 extern u64 __cmpxchg_u64(volatile u64 *ptr, u64 old, u64 new_);
62 extern u8 __cmpxchg_u8(volatile u8 *ptr, u8 old, u8 new_);
63
64 /* don't worry...optimizer will get rid of most of this */
65 static inline unsigned long
66 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
67 {
68         switch (size) {
69 #ifdef CONFIG_64BIT
70         case 8: return __cmpxchg_u64((u64 *)ptr, old, new_);
71 #endif
72         case 4: return __cmpxchg_u32((unsigned int *)ptr,
73                                      (unsigned int)old, (unsigned int)new_);
74         case 1: return __cmpxchg_u8((u8 *)ptr, old & 0xff, new_ & 0xff);
75         }
76         __cmpxchg_called_with_bad_pointer();
77         return old;
78 }
79
80 #define cmpxchg(ptr, o, n)                                               \
81 ({                                                                       \
82         __typeof__(*(ptr)) _o_ = (o);                                    \
83         __typeof__(*(ptr)) _n_ = (n);                                    \
84         (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_,        \
85                                     (unsigned long)_n_, sizeof(*(ptr))); \
86 })
87
88 #include <asm-generic/cmpxchg-local.h>
89
90 static inline unsigned long __cmpxchg_local(volatile void *ptr,
91                                       unsigned long old,
92                                       unsigned long new_, int size)
93 {
94         switch (size) {
95 #ifdef CONFIG_64BIT
96         case 8: return __cmpxchg_u64((u64 *)ptr, old, new_);
97 #endif
98         case 4: return __cmpxchg_u32(ptr, old, new_);
99         default:
100                 return __cmpxchg_local_generic(ptr, old, new_, size);
101         }
102 }
103
104 /*
105  * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
106  * them available.
107  */
108 #define cmpxchg_local(ptr, o, n)                                        \
109         ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
110                         (unsigned long)(n), sizeof(*(ptr))))
111 #ifdef CONFIG_64BIT
112 #define cmpxchg64_local(ptr, o, n)                                      \
113 ({                                                                      \
114         BUILD_BUG_ON(sizeof(*(ptr)) != 8);                              \
115         cmpxchg_local((ptr), (o), (n));                                 \
116 })
117 #else
118 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
119 #endif
120
121 #define cmpxchg64(ptr, o, n) __cmpxchg_u64(ptr, o, n)
122
123 #endif /* _ASM_PARISC_CMPXCHG_H_ */