GNU Linux-libre 4.4.295-gnu1
[releases.git] / arch / sparc / include / asm / cmpxchg_64.h
1 /* 64-bit atomic xchg() and cmpxchg() definitions.
2  *
3  * Copyright (C) 1996, 1997, 2000 David S. Miller (davem@redhat.com)
4  */
5
6 #ifndef __ARCH_SPARC64_CMPXCHG__
7 #define __ARCH_SPARC64_CMPXCHG__
8
9 static inline unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val)
10 {
11         unsigned long tmp1, tmp2;
12
13         __asm__ __volatile__(
14 "       mov             %0, %1\n"
15 "1:     lduw            [%4], %2\n"
16 "       cas             [%4], %2, %0\n"
17 "       cmp             %2, %0\n"
18 "       bne,a,pn        %%icc, 1b\n"
19 "        mov            %1, %0\n"
20         : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
21         : "0" (val), "r" (m)
22         : "cc", "memory");
23         return val;
24 }
25
26 static inline unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val)
27 {
28         unsigned long tmp1, tmp2;
29
30         __asm__ __volatile__(
31 "       mov             %0, %1\n"
32 "1:     ldx             [%4], %2\n"
33 "       casx            [%4], %2, %0\n"
34 "       cmp             %2, %0\n"
35 "       bne,a,pn        %%xcc, 1b\n"
36 "        mov            %1, %0\n"
37         : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
38         : "0" (val), "r" (m)
39         : "cc", "memory");
40         return val;
41 }
42
43 #define xchg(ptr,x)                                                     \
44 ({      __typeof__(*(ptr)) __ret;                                       \
45         __ret = (__typeof__(*(ptr)))                                    \
46                 __xchg((unsigned long)(x), (ptr), sizeof(*(ptr)));      \
47         __ret;                                                          \
48 })
49
50 void __xchg_called_with_bad_pointer(void);
51
52 static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
53                                        int size)
54 {
55         switch (size) {
56         case 4:
57                 return xchg32(ptr, x);
58         case 8:
59                 return xchg64(ptr, x);
60         }
61         __xchg_called_with_bad_pointer();
62         return x;
63 }
64
65 /*
66  * Atomic compare and exchange.  Compare OLD with MEM, if identical,
67  * store NEW in MEM.  Return the initial value in MEM.  Success is
68  * indicated by comparing RETURN with OLD.
69  */
70
71 #include <asm-generic/cmpxchg-local.h>
72
73 static inline unsigned long
74 __cmpxchg_u32(volatile int *m, int old, int new)
75 {
76         __asm__ __volatile__("cas [%2], %3, %0"
77                              : "=&r" (new)
78                              : "0" (new), "r" (m), "r" (old)
79                              : "memory");
80
81         return new;
82 }
83
84 static inline unsigned long
85 __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
86 {
87         __asm__ __volatile__("casx [%2], %3, %0"
88                              : "=&r" (new)
89                              : "0" (new), "r" (m), "r" (old)
90                              : "memory");
91
92         return new;
93 }
94
95 /* This function doesn't exist, so you'll get a linker error
96    if something tries to do an invalid cmpxchg().  */
97 void __cmpxchg_called_with_bad_pointer(void);
98
99 static inline unsigned long
100 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
101 {
102         switch (size) {
103                 case 4:
104                         return __cmpxchg_u32(ptr, old, new);
105                 case 8:
106                         return __cmpxchg_u64(ptr, old, new);
107         }
108         __cmpxchg_called_with_bad_pointer();
109         return old;
110 }
111
112 #define cmpxchg(ptr,o,n)                                                 \
113   ({                                                                     \
114      __typeof__(*(ptr)) _o_ = (o);                                       \
115      __typeof__(*(ptr)) _n_ = (n);                                       \
116      (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_,           \
117                                     (unsigned long)_n_, sizeof(*(ptr))); \
118   })
119
120 /*
121  * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
122  * them available.
123  */
124
125 static inline unsigned long __cmpxchg_local(volatile void *ptr,
126                                       unsigned long old,
127                                       unsigned long new, int size)
128 {
129         switch (size) {
130         case 4:
131         case 8: return __cmpxchg(ptr, old, new, size);
132         default:
133                 return __cmpxchg_local_generic(ptr, old, new, size);
134         }
135
136         return old;
137 }
138
139 #define cmpxchg_local(ptr, o, n)                                        \
140         ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
141                         (unsigned long)(n), sizeof(*(ptr))))
142 #define cmpxchg64_local(ptr, o, n)                                      \
143   ({                                                                    \
144         BUILD_BUG_ON(sizeof(*(ptr)) != 8);                              \
145         cmpxchg_local((ptr), (o), (n));                                 \
146   })
147 #define cmpxchg64(ptr, o, n)    cmpxchg64_local((ptr), (o), (n))
148
149 #endif /* __ARCH_SPARC64_CMPXCHG__ */