2 * Copyright (c) 1998 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifndef _MACHINE_ATOMIC_H_
30 #define _MACHINE_ATOMIC_H_
32 #include <machine/alpha_cpu.h>
35 * Various simple arithmetic on memory which is atomic in the presence
36 * of interrupts and SMP safe.
39 void atomic_set_8(volatile u_int8_t *, u_int8_t);
40 void atomic_clear_8(volatile u_int8_t *, u_int8_t);
41 void atomic_add_8(volatile u_int8_t *, u_int8_t);
42 void atomic_subtract_8(volatile u_int8_t *, u_int8_t);
44 void atomic_set_16(volatile u_int16_t *, u_int16_t);
45 void atomic_clear_16(volatile u_int16_t *, u_int16_t);
46 void atomic_add_16(volatile u_int16_t *, u_int16_t);
47 void atomic_subtract_16(volatile u_int16_t *, u_int16_t);
49 static __inline void atomic_set_32(volatile u_int32_t *p, u_int32_t v)
55 "1:\tldl_l %0, %2\n\t" /* load old value */
56 "bis %0, %3, %0\n\t" /* calculate new value */
57 "stl_c %0, %1\n\t" /* attempt to store */
58 "beq %0, 2f\n\t" /* spin if failed */
59 "mb\n\t" /* drain to memory */
60 ".section .text3,\"ax\"\n" /* improve branch prediction */
61 "2:\tbr 1b\n" /* try again */
63 : "=&r" (temp), "=m" (*p)
69 static __inline void atomic_clear_32(volatile u_int32_t *p, u_int32_t v)
75 "1:\tldl_l %0, %1\n\t" /* load old value */
76 "bic %0, %2, %0\n\t" /* calculate new value */
77 "stl_c %0, %1\n\t" /* attempt to store */
78 "beq %0, 2f\n\t" /* spin if failed */
79 "mb\n\t" /* drain to memory */
80 ".section .text3,\"ax\"\n" /* improve branch prediction */
81 "2:\tbr 1b\n" /* try again */
83 : "=&r" (temp), "+m" (*p)
89 static __inline void atomic_add_32(volatile u_int32_t *p, u_int32_t v)
95 "1:\tldl_l %0, %1\n\t" /* load old value */
96 "addl %0, %2, %0\n\t" /* calculate new value */
97 "stl_c %0, %1\n\t" /* attempt to store */
98 "beq %0, 2f\n\t" /* spin if failed */
99 "mb\n\t" /* drain to memory */
100 ".section .text3,\"ax\"\n" /* improve branch prediction */
101 "2:\tbr 1b\n" /* try again */
103 : "=&r" (temp), "+m" (*p)
109 static __inline void atomic_subtract_32(volatile u_int32_t *p, u_int32_t v)
115 "1:\tldl_l %0, %1\n\t" /* load old value */
116 "subl %0, %2, %0\n\t" /* calculate new value */
117 "stl_c %0, %1\n\t" /* attempt to store */
118 "beq %0, 2f\n\t" /* spin if failed */
119 "mb\n\t" /* drain to memory */
120 ".section .text3,\"ax\"\n" /* improve branch prediction */
121 "2:\tbr 1b\n" /* try again */
123 : "=&r" (temp), "+m" (*p)
129 static __inline u_int32_t atomic_readandclear_32(volatile u_int32_t *addr)
131 u_int32_t result,temp;
135 "wmb\n" /* ensure pending writes have drained */
136 "1:\tldl_l %0,%2\n\t" /* load current value, asserting lock */
137 "ldiq %1,0\n\t" /* value to store */
138 "stl_c %1,%2\n\t" /* attempt to store */
139 "beq %1,2f\n\t" /* if the store failed, spin */
140 "br 3f\n" /* it worked, exit */
141 "2:\tbr 1b\n" /* *addr not updated, loop */
142 "3:\tmb\n" /* it worked */
143 : "=&r"(result), "=&r"(temp), "+m" (*addr)
151 static __inline void atomic_set_64(volatile u_int64_t *p, u_int64_t v)
157 "1:\tldq_l %0, %1\n\t" /* load old value */
158 "bis %0, %2, %0\n\t" /* calculate new value */
159 "stq_c %0, %1\n\t" /* attempt to store */
160 "beq %0, 2f\n\t" /* spin if failed */
161 "mb\n\t" /* drain to memory */
162 ".section .text3,\"ax\"\n" /* improve branch prediction */
163 "2:\tbr 1b\n" /* try again */
165 : "=&r" (temp), "+m" (*p)
171 static __inline void atomic_clear_64(volatile u_int64_t *p, u_int64_t v)
177 "1:\tldq_l %0, %1\n\t" /* load old value */
178 "bic %0, %2, %0\n\t" /* calculate new value */
179 "stq_c %0, %1\n\t" /* attempt to store */
180 "beq %0, 2f\n\t" /* spin if failed */
181 "mb\n\t" /* drain to memory */
182 ".section .text3,\"ax\"\n" /* improve branch prediction */
183 "2:\tbr 1b\n" /* try again */
185 : "=&r" (temp), "+m" (*p)
191 static __inline void atomic_add_64(volatile u_int64_t *p, u_int64_t v)
197 "1:\tldq_l %0, %1\n\t" /* load old value */
198 "addq %0, %2, %0\n\t" /* calculate new value */
199 "stq_c %0, %1\n\t" /* attempt to store */
200 "beq %0, 2f\n\t" /* spin if failed */
201 "mb\n\t" /* drain to memory */
202 ".section .text3,\"ax\"\n" /* improve branch prediction */
203 "2:\tbr 1b\n" /* try again */
205 : "=&r" (temp), "+m" (*p)
211 static __inline void atomic_subtract_64(volatile u_int64_t *p, u_int64_t v)
217 "1:\tldq_l %0, %1\n\t" /* load old value */
218 "subq %0, %2, %0\n\t" /* calculate new value */
219 "stq_c %0, %1\n\t" /* attempt to store */
220 "beq %0, 2f\n\t" /* spin if failed */
221 "mb\n\t" /* drain to memory */
222 ".section .text3,\"ax\"\n" /* improve branch prediction */
223 "2:\tbr 1b\n" /* try again */
225 : "=&r" (temp), "+m" (*p)
231 static __inline u_int64_t atomic_readandclear_64(volatile u_int64_t *addr)
233 u_int64_t result,temp;
237 "wmb\n" /* ensure pending writes have drained */
238 "1:\tldq_l %0,%2\n\t" /* load current value, asserting lock */
239 "ldiq %1,0\n\t" /* value to store */
240 "stq_c %1,%2\n\t" /* attempt to store */
241 "beq %1,2f\n\t" /* if the store failed, spin */
242 "br 3f\n" /* it worked, exit */
243 "2:\tbr 1b\n" /* *addr not updated, loop */
244 "3:\tmb\n" /* it worked */
245 : "=&r"(result), "=&r"(temp), "+m" (*addr)
253 #define atomic_set_char atomic_set_8
254 #define atomic_clear_char atomic_clear_8
255 #define atomic_add_char atomic_add_8
256 #define atomic_subtract_char atomic_subtract_8
258 #define atomic_set_short atomic_set_16
259 #define atomic_clear_short atomic_clear_16
260 #define atomic_add_short atomic_add_16
261 #define atomic_subtract_short atomic_subtract_16
263 #define atomic_set_int atomic_set_32
264 #define atomic_clear_int atomic_clear_32
265 #define atomic_add_int atomic_add_32
266 #define atomic_subtract_int atomic_subtract_32
267 #define atomic_readandclear_int atomic_readandclear_32
269 #define atomic_set_long atomic_set_64
270 #define atomic_clear_long atomic_clear_64
271 #define atomic_add_long atomic_add_64
272 #define atomic_subtract_long atomic_subtract_64
273 #define atomic_readandclear_long atomic_readandclear_64
275 #define ATOMIC_ACQ_REL(NAME, WIDTH, TYPE) \
276 static __inline void \
277 atomic_##NAME##_acq_##WIDTH(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v)\
279 atomic_##NAME##_##WIDTH(p, v); \
283 static __inline void \
284 atomic_##NAME##_rel_##WIDTH(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v)\
287 atomic_##NAME##_##WIDTH(p, v); \
290 static __inline void \
291 atomic_##NAME##_acq_##TYPE(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v)\
293 atomic_##NAME##_##WIDTH(p, v); \
297 static __inline void \
298 atomic_##NAME##_rel_##TYPE(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v)\
301 atomic_##NAME##_##WIDTH(p, v); \
304 ATOMIC_ACQ_REL(set, 8, char)
305 ATOMIC_ACQ_REL(clear, 8, char)
306 ATOMIC_ACQ_REL(add, 8, char)
307 ATOMIC_ACQ_REL(subtract, 8, char)
308 ATOMIC_ACQ_REL(set, 16, short)
309 ATOMIC_ACQ_REL(clear, 16, short)
310 ATOMIC_ACQ_REL(add, 16, short)
311 ATOMIC_ACQ_REL(subtract, 16, short)
312 ATOMIC_ACQ_REL(set, 32, int)
313 ATOMIC_ACQ_REL(clear, 32, int)
314 ATOMIC_ACQ_REL(add, 32, int)
315 ATOMIC_ACQ_REL(subtract, 32, int)
316 ATOMIC_ACQ_REL(set, 64, long)
317 ATOMIC_ACQ_REL(clear, 64, long)
318 ATOMIC_ACQ_REL(add, 64, long)
319 ATOMIC_ACQ_REL(subtract, 64, long)
321 #undef ATOMIC_ACQ_REL
324 * We assume that a = b will do atomic loads and stores.
326 #define ATOMIC_STORE_LOAD(TYPE, WIDTH) \
327 static __inline u_##TYPE \
328 atomic_load_acq_##WIDTH(volatile u_##TYPE *p) \
337 static __inline void \
338 atomic_store_rel_##WIDTH(volatile u_##TYPE *p, u_##TYPE v)\
343 static __inline u_##TYPE \
344 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
353 static __inline void \
354 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
360 ATOMIC_STORE_LOAD(char, 8)
361 ATOMIC_STORE_LOAD(short, 16)
362 ATOMIC_STORE_LOAD(int, 32)
363 ATOMIC_STORE_LOAD(long, 64)
365 #undef ATOMIC_STORE_LOAD
368 * Atomically compare the value stored at *p with cmpval and if the
369 * two values are equal, update the value of *p with newval. Returns
370 * zero if the compare failed, nonzero otherwise.
372 static __inline u_int32_t
373 atomic_cmpset_32(volatile u_int32_t* p, u_int32_t cmpval, u_int32_t newval)
379 "1:\tldl_l %0, %1\n\t" /* load old value */
380 "cmpeq %0, %2, %0\n\t" /* compare */
381 "beq %0, 2f\n\t" /* exit if not equal */
382 "mov %3, %0\n\t" /* value to store */
383 "stl_c %0, %1\n\t" /* attempt to store */
384 "beq %0, 3f\n\t" /* if it failed, spin */
385 "mb\n\t" /* drain to memory */
387 ".section .text3,\"ax\"\n" /* improve branch prediction */
388 "3:\tbr 1b\n" /* try again */
390 : "=&r" (ret), "+m" (*p)
391 : "r" ((long)(int)cmpval), "r" (newval)
399 * Atomically compare the value stored at *p with cmpval and if the
400 * two values are equal, update the value of *p with newval. Returns
401 * zero if the compare failed, nonzero otherwise.
403 static __inline u_int64_t
404 atomic_cmpset_64(volatile u_int64_t* p, u_int64_t cmpval, u_int64_t newval)
410 "1:\tldq_l %0, %1\n\t" /* load old value */
411 "cmpeq %0, %2, %0\n\t" /* compare */
412 "beq %0, 2f\n\t" /* exit if not equal */
413 "mov %3, %0\n\t" /* value to store */
414 "stq_c %0, %1\n\t" /* attempt to store */
415 "beq %0, 3f\n\t" /* if it failed, spin */
416 "mb\n\t" /* drain to memory */
418 ".section .text3,\"ax\"\n" /* improve branch prediction */
419 "3:\tbr 1b\n" /* try again */
421 : "=&r" (ret), "+m" (*p)
422 : "r" (cmpval), "r" (newval)
429 #define atomic_cmpset_int atomic_cmpset_32
430 #define atomic_cmpset_long atomic_cmpset_64
433 atomic_cmpset_ptr(volatile void *dst, void *exp, void *src)
436 return (atomic_cmpset_long((volatile u_long *)dst, (u_long)exp,
440 static __inline u_int32_t
441 atomic_cmpset_acq_32(volatile u_int32_t *p, u_int32_t cmpval, u_int32_t newval)
445 retval = atomic_cmpset_32(p, cmpval, newval);
450 static __inline u_int32_t
451 atomic_cmpset_rel_32(volatile u_int32_t *p, u_int32_t cmpval, u_int32_t newval)
454 return (atomic_cmpset_32(p, cmpval, newval));
457 static __inline u_int64_t
458 atomic_cmpset_acq_64(volatile u_int64_t *p, u_int64_t cmpval, u_int64_t newval)
462 retval = atomic_cmpset_64(p, cmpval, newval);
467 static __inline u_int64_t
468 atomic_cmpset_rel_64(volatile u_int64_t *p, u_int64_t cmpval, u_int64_t newval)
471 return (atomic_cmpset_64(p, cmpval, newval));
474 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
475 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
476 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
477 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
480 atomic_cmpset_acq_ptr(volatile void *dst, void *exp, void *src)
483 return (atomic_cmpset_acq_long((volatile u_long *)dst, (u_long)exp,
488 atomic_cmpset_rel_ptr(volatile void *dst, void *exp, void *src)
491 return (atomic_cmpset_rel_long((volatile u_long *)dst, (u_long)exp,
495 static __inline void *
496 atomic_load_acq_ptr(volatile void *p)
498 return (void *)atomic_load_acq_long((volatile u_long *)p);
502 atomic_store_rel_ptr(volatile void *p, void *v)
504 atomic_store_rel_long((volatile u_long *)p, (u_long)v);
507 #define ATOMIC_PTR(NAME) \
508 static __inline void \
509 atomic_##NAME##_ptr(volatile void *p, uintptr_t v) \
511 atomic_##NAME##_long((volatile u_long *)p, v); \
514 static __inline void \
515 atomic_##NAME##_acq_ptr(volatile void *p, uintptr_t v) \
517 atomic_##NAME##_acq_long((volatile u_long *)p, v);\
520 static __inline void \
521 atomic_##NAME##_rel_ptr(volatile void *p, uintptr_t v) \
523 atomic_##NAME##_rel_long((volatile u_long *)p, v);\
533 #endif /* ! _MACHINE_ATOMIC_H_ */