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
|
/* $OpenBSD: atomic.h,v 1.11 2018/05/14 09:33:20 kettenis Exp $ */
/* Public Domain */
#ifndef _MACHINE_ATOMIC_H_
#define _MACHINE_ATOMIC_H_
#if defined(_KERNEL)
typedef volatile u_int __cpu_simple_lock_t __attribute__((__aligned__(16)));
#define __SIMPLELOCK_LOCKED 0
#define __SIMPLELOCK_UNLOCKED 1
static inline void
__cpu_simple_lock_init(__cpu_simple_lock_t *l)
{
*l = __SIMPLELOCK_UNLOCKED;
}
static inline unsigned int
__cpu_simple_lock_ldcws(__cpu_simple_lock_t *l)
{
unsigned int o;
asm volatile("ldcws 0(%2), %0" : "=&r" (o), "+m" (l) : "r" (l));
return (o);
}
static inline int
__cpu_simple_lock_try(__cpu_simple_lock_t *l)
{
return (__cpu_simple_lock_ldcws(l) == __SIMPLELOCK_UNLOCKED);
}
static inline void
__cpu_simple_lock(__cpu_simple_lock_t *l)
{
while (!__cpu_simple_lock_ldcws(l))
;
}
static inline void
__cpu_simple_unlock(__cpu_simple_lock_t *l)
{
*l = __SIMPLELOCK_UNLOCKED;
}
#ifdef MULTIPROCESSOR
extern __cpu_simple_lock_t atomic_lock;
#define ATOMIC_LOCK __cpu_simple_lock(&atomic_lock);
#define ATOMIC_UNLOCK __cpu_simple_unlock(&atomic_lock);
#else
#define ATOMIC_LOCK
#define ATOMIC_UNLOCK
#endif
static inline register_t
atomic_enter(void)
{
register_t eiem;
__asm volatile("mfctl %%cr15, %0": "=r" (eiem));
__asm volatile("mtctl %r0, %cr15");
ATOMIC_LOCK;
return (eiem);
}
static inline void
atomic_leave(register_t eiem)
{
ATOMIC_UNLOCK;
__asm volatile("mtctl %0, %%cr15":: "r" (eiem));
}
static inline unsigned int
_atomic_cas_uint(volatile unsigned int *uip, unsigned int o, unsigned int n)
{
register_t eiem;
unsigned int rv;
eiem = atomic_enter();
rv = *uip;
if (rv == o)
*uip = n;
atomic_leave(eiem);
return (rv);
}
#define atomic_cas_uint(_p, _o, _n) _atomic_cas_uint((_p), (_o), (_n))
static inline unsigned long
_atomic_cas_ulong(volatile unsigned long *uip, unsigned long o, unsigned long n)
{
register_t eiem;
unsigned long rv;
eiem = atomic_enter();
rv = *uip;
if (rv == o)
*uip = n;
atomic_leave(eiem);
return (rv);
}
#define atomic_cas_ulong(_p, _o, _n) _atomic_cas_ulong((_p), (_o), (_n))
static inline void *
_atomic_cas_ptr(volatile void *uip, void *o, void *n)
{
register_t eiem;
void * volatile *uipp = (void * volatile *)uip;
void *rv;
eiem = atomic_enter();
rv = *uipp;
if (rv == o)
*uipp = n;
atomic_leave(eiem);
return (rv);
}
#define atomic_cas_ptr(_p, _o, _n) _atomic_cas_ptr((_p), (_o), (_n))
static inline unsigned int
_atomic_swap_uint(volatile unsigned int *uip, unsigned int n)
{
register_t eiem;
unsigned int rv;
eiem = atomic_enter();
rv = *uip;
*uip = n;
atomic_leave(eiem);
return (rv);
}
#define atomic_swap_uint(_p, _n) _atomic_swap_uint((_p), (_n))
static inline unsigned long
_atomic_swap_ulong(volatile unsigned long *uip, unsigned long n)
{
register_t eiem;
unsigned long rv;
eiem = atomic_enter();
rv = *uip;
*uip = n;
atomic_leave(eiem);
return (rv);
}
#define atomic_swap_ulong(_p, _n) _atomic_swap_ulong((_p), (_n))
static inline void *
_atomic_swap_ptr(volatile void *uip, void *n)
{
register_t eiem;
void * volatile *uipp = (void * volatile *)uip;
void *rv;
eiem = atomic_enter();
rv = *uipp;
*uipp = n;
atomic_leave(eiem);
return (rv);
}
#define atomic_swap_ptr(_p, _n) _atomic_swap_ptr((_p), (_n))
static __inline unsigned int
_atomic_add_int_nv(volatile unsigned int *uip, unsigned int v)
{
register_t eiem;
unsigned int rv;
eiem = atomic_enter();
rv = *uip + v;
*uip = rv;
atomic_leave(eiem);
return (rv);
}
#define atomic_add_int_nv(_uip, _v) _atomic_add_int_nv((_uip), (_v))
#define atomic_sub_int_nv(_uip, _v) _atomic_add_int_nv((_uip), 0 - (_v))
static __inline unsigned long
_atomic_add_long_nv(volatile unsigned long *uip, unsigned long v)
{
register_t eiem;
unsigned long rv;
eiem = atomic_enter();
rv = *uip + v;
*uip = rv;
atomic_leave(eiem);
return (rv);
}
#define atomic_add_long_nv(_uip, _v) _atomic_add_long_nv((_uip), (_v))
#define atomic_sub_long_nv(_uip, _v) _atomic_add_long_nv((_uip), 0 - (_v))
static __inline void
atomic_setbits_int(volatile unsigned int *uip, unsigned int v)
{
register_t eiem;
eiem = atomic_enter();
*uip |= v;
atomic_leave(eiem);
}
static __inline void
atomic_clearbits_int(volatile unsigned int *uip, unsigned int v)
{
register_t eiem;
eiem = atomic_enter();
*uip &= ~v;
atomic_leave(eiem);
}
static __inline void
atomic_setbits_long(volatile unsigned long *uip, unsigned long v)
{
register_t eiem;
eiem = atomic_enter();
*uip |= v;
atomic_leave(eiem);
}
static __inline void
atomic_clearbits_long(volatile unsigned long *uip, unsigned long v)
{
register_t eiem;
eiem = atomic_enter();
*uip &= ~v;
atomic_leave(eiem);
}
#endif /* defined(_KERNEL) */
/*
* Although the PA-RISC 2.0 architecture allows an implementation to
* be weakly ordered, all PA-RISC processers to date implement a
* strong memory ordering model. So all we need is a compiler
* barrier.
*/
static inline void
__insn_barrier(void)
{
__asm volatile("" : : : "memory");
}
#define membar_enter() __insn_barrier()
#define membar_exit() __insn_barrier()
#define membar_producer() __insn_barrier()
#define membar_consumer() __insn_barrier()
#define membar_sync() __insn_barrier()
#endif /* _MACHINE_ATOMIC_H_ */
|