1
2
3
4
5
6
7
8
9
10
11#ifndef _XTENSA_CMPXCHG_H
12#define _XTENSA_CMPXCHG_H
13
14#ifndef __ASSEMBLY__
15
16#include <linux/stringify.h>
17
18
19
20
21
22static inline unsigned long
23__cmpxchg_u32(volatile int *p, int old, int new)
24{
25#if XCHAL_HAVE_S32C1I
26 __asm__ __volatile__(
27 " wsr %2, scompare1\n"
28 " s32c1i %0, %1, 0\n"
29 : "+a" (new)
30 : "a" (p), "a" (old)
31 : "memory"
32 );
33
34 return new;
35#else
36 __asm__ __volatile__(
37 " rsil a15, "__stringify(TOPLEVEL)"\n"
38 " l32i %0, %1, 0\n"
39 " bne %0, %2, 1f\n"
40 " s32i %3, %1, 0\n"
41 "1:\n"
42 " wsr a15, ps\n"
43 " rsync\n"
44 : "=&a" (old)
45 : "a" (p), "a" (old), "r" (new)
46 : "a15", "memory");
47 return old;
48#endif
49}
50
51
52
53extern void __cmpxchg_called_with_bad_pointer(void);
54
55static __inline__ unsigned long
56__cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
57{
58 switch (size) {
59 case 4: return __cmpxchg_u32(ptr, old, new);
60 default: __cmpxchg_called_with_bad_pointer();
61 return old;
62 }
63}
64
65#define cmpxchg(ptr,o,n) \
66 ({ __typeof__(*(ptr)) _o_ = (o); \
67 __typeof__(*(ptr)) _n_ = (n); \
68 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
69 (unsigned long)_n_, sizeof (*(ptr))); \
70 })
71
72#include <asm-generic/cmpxchg-local.h>
73
74static inline unsigned long __cmpxchg_local(volatile void *ptr,
75 unsigned long old,
76 unsigned long new, int size)
77{
78 switch (size) {
79 case 4:
80 return __cmpxchg_u32(ptr, old, new);
81 default:
82 return __cmpxchg_local_generic(ptr, old, new, size);
83 }
84
85 return old;
86}
87
88
89
90
91
92#define cmpxchg_local(ptr, o, n) \
93 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
94 (unsigned long)(n), sizeof(*(ptr))))
95#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
96#define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
97
98
99
100
101
102
103
104
105
106
107
108static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
109{
110#if XCHAL_HAVE_S32C1I
111 unsigned long tmp, result;
112 __asm__ __volatile__(
113 "1: l32i %1, %2, 0\n"
114 " mov %0, %3\n"
115 " wsr %1, scompare1\n"
116 " s32c1i %0, %2, 0\n"
117 " bne %0, %1, 1b\n"
118 : "=&a" (result), "=&a" (tmp)
119 : "a" (m), "a" (val)
120 : "memory"
121 );
122 return result;
123#else
124 unsigned long tmp;
125 __asm__ __volatile__(
126 " rsil a15, "__stringify(TOPLEVEL)"\n"
127 " l32i %0, %1, 0\n"
128 " s32i %2, %1, 0\n"
129 " wsr a15, ps\n"
130 " rsync\n"
131 : "=&a" (tmp)
132 : "a" (m), "a" (val)
133 : "a15", "memory");
134 return tmp;
135#endif
136}
137
138#define xchg(ptr,x) \
139 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
140
141
142
143
144
145
146
147extern void __xchg_called_with_bad_pointer(void);
148
149static __inline__ unsigned long
150__xchg(unsigned long x, volatile void * ptr, int size)
151{
152 switch (size) {
153 case 4:
154 return xchg_u32(ptr, x);
155 }
156 __xchg_called_with_bad_pointer();
157 return x;
158}
159
160#endif
161
162#endif
163