1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/* $OpenBSD: atomic.h,v 1.17 2017/05/12 08:53:46 mpi Exp $ */
/* Public Domain */
#ifndef _ARM_ATOMIC_H_
#define _ARM_ATOMIC_H_
/*
* Compare and set:
* ret = *ptr
* if (ret == expect)
* *ptr = new
* return (ret)
*/
#define _def_atomic_cas(_f, _t) \
static inline _t \
_f(volatile _t *p, _t e, _t n) \
{ \
_t ret, modified; \
\
__asm volatile ( \
"1: ldrex %0, [%4] \n\t" \
" cmp %0, %3 \n\t" \
" bne 2f \n\t" \
" strex %1, %2, [%4] \n\t" \
" cmp %1, #0 \n\t" \
" bne 1b \n\t" \
" b 3f \n\t" \
"2: clrex \n\t" \
"3: \n\t" \
: "=&r" (ret), "=&r" (modified) \
: "r" (n), "r" (e), "r" (p) \
: "memory", "cc" \
); \
return (ret); \
}
_def_atomic_cas(_atomic_cas_uint, unsigned int)
_def_atomic_cas(_atomic_cas_ulong, unsigned long)
#undef _def_atomic_cas
#define atomic_cas_uint(_p, _e, _n) _atomic_cas_uint((_p), (_e), (_n))
#define atomic_cas_ulong(_p, _e, _n) _atomic_cas_ulong((_p), (_e), (_n))
static inline void *
_atomic_cas_ptr(volatile void *p, void *e, void *n)
{
void *ret;
uint32_t modified;
__asm volatile (
"1: ldrex %0, [%4] \n\t"
" cmp %0, %3 \n\t"
" bne 2f \n\t"
" strex %1, %2, [%4] \n\t"
" cmp %1, #0 \n\t"
" bne 1b \n\t"
" b 3f \n\t"
"2: clrex \n\t"
"3: \n\t"
: "=&r" (ret), "=&r" (modified)
: "r" (n), "r" (e), "r" (p)
: "memory", "cc"
);
return (ret);
}
#define atomic_cas_ptr(_p, _e, _n) _atomic_cas_ptr((_p), (_e), (_n))
/*
* Swap:
* ret = *p
* *p = val
* return (ret)
*/
#define _def_atomic_swap(_f, _t) \
static inline _t \
_f(volatile _t *p, _t v) \
{ \
_t ret, modified; \
\
__asm volatile ( \
"1: ldrex %0, [%3] \n\t" \
" strex %1, %2, [%3] \n\t" \
" cmp %1, #0 \n\t" \
" bne 1b \n\t" \
: "=&r" (ret), "=&r" (modified) \
: "r" (v), "r" (p) \
: "memory", "cc" \
); \
return (ret); \
}
_def_atomic_swap(_atomic_swap_uint, unsigned int)
_def_atomic_swap(_atomic_swap_ulong, unsigned long)
#undef _def_atomic_swap
#define atomic_swap_uint(_p, _v) _atomic_swap_uint((_p), (_v))
#define atomic_swap_ulong(_p, _v) _atomic_swap_ulong((_p), (_v))
static inline void *
_atomic_swap_ptr(volatile void *p, void *v)
{
void *ret;
uint32_t modified;
__asm volatile (
"1: ldrex %0, [%3] \n\t"
" strex %1, %2, [%3] \n\t"
" cmp %1, #0 \n\t"
" bne 1b \n\t"
: "=&r" (ret), "=&r" (modified)
: "r" (v), "r" (p)
: "memory", "cc"
);
return (ret);
}
#define atomic_swap_ptr(_p, _v) _atomic_swap_ptr((_p), (_v))
/*
* Increment returning the new value
* *p += 1
* return (*p)
*/
#define _def_atomic_inc_nv(_f, _t) \
static inline _t \
_f(volatile _t *p) \
{ \
_t ret, modified; \
\
__asm volatile ( \
"1: ldrex %0, [%2] \n\t" \
" add %0, %0, #1 \n\t" \
" strex %1, %0, [%2] \n\t" \
" cmp %1, #0 \n\t" \
" bne 1b \n\t" \
: "=&r" (ret), "=&r" (modified) \
: "r" (p) \
: "memory", "cc" \
); \
return (ret); \
}
_def_atomic_inc_nv(_atomic_inc_int_nv, unsigned int)
_def_atomic_inc_nv(_atomic_inc_long_nv, unsigned long)
#undef _def_atomic_inc_nv
#define atomic_inc_int_nv(_p) _atomic_inc_int_nv((_p))
#define atomic_inc_long_nv(_p) _atomic_inc_long_nv((_p))
/*
* Decrement returning the new value
* *p -= 1
* return (*p)
*/
#define _def_atomic_dec_nv(_f, _t) \
static inline _t \
_f(volatile _t *p) \
{ \
_t ret, modified; \
\
__asm volatile ( \
"1: ldrex %0, [%2] \n\t" \
" sub %0, %0, #1 \n\t" \
" strex %1, %0, [%2] \n\t" \
" cmp %1, #0 \n\t" \
" bne 1b \n\t" \
: "=&r" (ret), "=&r" (modified) \
: "r" (p) \
: "memory", "cc" \
); \
return (ret); \
}
_def_atomic_dec_nv(_atomic_dec_int_nv, unsigned int)
_def_atomic_dec_nv(_atomic_dec_long_nv, unsigned long)
#undef _def_atomic_dec_nv
#define atomic_dec_int_nv(_p) _atomic_dec_int_nv((_p))
#define atomic_dec_long_nv(_p) _atomic_dec_long_nv((_p))
/*
* Addition returning the new value
* *p += v
* return (*p)
*/
#define _def_atomic_add_nv(_f, _t) \
static inline _t \
_f(volatile _t *p, _t v) \
{ \
_t ret, modified; \
\
__asm volatile ( \
"1: ldrex %0, [%2] \n\t" \
" add %0, %0, %3 \n\t" \
" strex %1, %0, [%2] \n\t" \
" cmp %1, #0 \n\t" \
" bne 1b \n\t" \
: "=&r" (ret), "=&r" (modified) \
: "r" (p), "r" (v) \
: "memory", "cc" \
); \
return (ret); \
}
_def_atomic_add_nv(_atomic_add_int_nv, unsigned int)
_def_atomic_add_nv(_atomic_add_long_nv, unsigned long)
#undef _def_atomic_add_nv
#define atomic_add_int_nv(_p, _v) _atomic_add_int_nv((_p), (_v))
#define atomic_add_long_nv(_p, _v) _atomic_add_long_nv((_p), (_v))
/*
* Subtraction returning the new value
* *p -= v
* return (*p)
*/
#define _def_atomic_sub_nv(_f, _t) \
static inline _t \
_f(volatile _t *p, _t v) \
{ \
_t ret, modified; \
\
__asm volatile ( \
"1: ldrex %0, [%2] \n\t" \
" sub %0, %0, %3 \n\t" \
" strex %1, %0, [%2] \n\t" \
" cmp %1, #0 \n\t" \
" bne 1b \n\t" \
: "=&r" (ret), "=&r" (modified) \
: "r" (p), "r" (v) \
: "memory", "cc" \
); \
return (ret); \
}
_def_atomic_sub_nv(_atomic_sub_int_nv, unsigned int)
_def_atomic_sub_nv(_atomic_sub_long_nv, unsigned long)
#undef _def_atomic_sub_nv
#define atomic_sub_int_nv(_p, _v) _atomic_sub_int_nv((_p), (_v))
#define atomic_sub_long_nv(_p, _v) _atomic_sub_long_nv((_p), (_v))
#define __membar(_f) do { __asm __volatile(_f ::: "memory"); } while (0)
#define membar_enter() __membar("dmb sy")
#define membar_exit() __membar("dmb sy")
#define membar_producer() __membar("dmb st")
#define membar_consumer() __membar("dmb sy")
#define membar_sync() __membar("dmb sy")
#if defined(_KERNEL)
/* virtio needs MP membars even on SP kernels */
#define virtio_membar_producer() __membar("dmb st")
#define virtio_membar_consumer() __membar("dmb sy")
#define virtio_membar_sync() __membar("dmb sy")
/*
* Set bits
* *p = *p | v
*/
static inline void
atomic_setbits_int(volatile unsigned int *p, unsigned int v)
{
unsigned int modified, tmp;
__asm volatile (
"1: ldrex %0, [%3] \n\t"
" orr %0, %0, %2 \n\t"
" strex %1, %0, [%3] \n\t"
" cmp %1, #0 \n\t"
" bne 1b \n\t"
: "=&r" (tmp), "=&r" (modified)
: "r" (v), "r" (p)
: "memory", "cc"
);
}
/*
* Clear bits
* *p = *p & (~v)
*/
static inline void
atomic_clearbits_int(volatile unsigned int *p, unsigned int v)
{
unsigned int modified, tmp;
__asm volatile (
"1: ldrex %0, [%3] \n\t"
" bic %0, %0, %2 \n\t"
" strex %1, %0, [%3] \n\t"
" cmp %1, #0 \n\t"
" bne 1b \n\t"
: "=&r" (tmp), "=&r" (modified)
: "r" (v), "r" (p)
: "memory", "cc"
);
}
#endif /* defined(_KERNEL) */
#endif /* _ARM_ATOMIC_H_ */
|