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
26 * from: src/sys/alpha/include/atomic.h,v 1.21.2.3 2005/10/06 18:12:05 jhb
30 #ifndef _MACHINE_ATOMIC_H_
31 #define _MACHINE_ATOMIC_H_
34 #error this file needs sys/cdefs.h as a prerequisite
40 __asm __volatile (".set noreorder\n\t"
54 #define mb() mips_sync()
55 #define wmb() mips_sync()
56 #define rmb() mips_sync()
59 * Various simple arithmetic on memory which is atomic in the presence
60 * of interrupts and SMP safe.
63 void atomic_set_8(__volatile uint8_t *, uint8_t);
64 void atomic_clear_8(__volatile uint8_t *, uint8_t);
65 void atomic_add_8(__volatile uint8_t *, uint8_t);
66 void atomic_subtract_8(__volatile uint8_t *, uint8_t);
68 void atomic_set_16(__volatile uint16_t *, uint16_t);
69 void atomic_clear_16(__volatile uint16_t *, uint16_t);
70 void atomic_add_16(__volatile uint16_t *, uint16_t);
71 void atomic_subtract_16(__volatile uint16_t *, uint16_t);
74 atomic_set_32(__volatile uint32_t *p, uint32_t v)
79 "1:\tll %0, %3\n\t" /* load old value */
80 "or %0, %2, %0\n\t" /* calculate new value */
81 "sc %0, %1\n\t" /* attempt to store */
82 "beqz %0, 1b\n\t" /* spin if failed */
83 : "=&r" (temp), "=m" (*p)
90 atomic_clear_32(__volatile uint32_t *p, uint32_t v)
96 "1:\tll %0, %3\n\t" /* load old value */
97 "and %0, %2, %0\n\t" /* calculate new value */
98 "sc %0, %1\n\t" /* attempt to store */
99 "beqz %0, 1b\n\t" /* spin if failed */
100 : "=&r" (temp), "=m" (*p)
106 atomic_add_32(__volatile uint32_t *p, uint32_t v)
111 "1:\tll %0, %3\n\t" /* load old value */
112 "addu %0, %2, %0\n\t" /* calculate new value */
113 "sc %0, %1\n\t" /* attempt to store */
114 "beqz %0, 1b\n\t" /* spin if failed */
115 : "=&r" (temp), "=m" (*p)
121 atomic_subtract_32(__volatile uint32_t *p, uint32_t v)
126 "1:\tll %0, %3\n\t" /* load old value */
127 "subu %0, %2\n\t" /* calculate new value */
128 "sc %0, %1\n\t" /* attempt to store */
129 "beqz %0, 1b\n\t" /* spin if failed */
130 : "=&r" (temp), "=m" (*p)
135 static __inline uint32_t
136 atomic_readandclear_32(__volatile uint32_t *addr)
138 uint32_t result,temp;
141 "1:\tll %0,%3\n\t" /* load current value, asserting lock */
142 "li %1,0\n\t" /* value to store */
143 "sc %1,%2\n\t" /* attempt to store */
144 "beqz %1, 1b\n\t" /* if the store failed, spin */
145 : "=&r"(result), "=&r"(temp), "=m" (*addr)
152 static __inline uint32_t
153 atomic_readandset_32(__volatile uint32_t *addr, uint32_t value)
155 uint32_t result,temp;
158 "1:\tll %0,%3\n\t" /* load current value, asserting lock */
160 "sc %1,%2\n\t" /* attempt to store */
161 "beqz %1, 1b\n\t" /* if the store failed, spin */
162 : "=&r"(result), "=&r"(temp), "=m" (*addr)
163 : "m" (*addr), "r" (value)
169 #define ATOMIC_ACQ_REL(NAME, WIDTH) \
170 static __inline void \
171 atomic_##NAME##_acq_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
173 atomic_##NAME##_##WIDTH(p, v); \
177 static __inline void \
178 atomic_##NAME##_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
181 atomic_##NAME##_##WIDTH(p, v); \
184 /* Variants of simple arithmetic with memory barriers. */
185 ATOMIC_ACQ_REL(set, 8)
186 ATOMIC_ACQ_REL(clear, 8)
187 ATOMIC_ACQ_REL(add, 8)
188 ATOMIC_ACQ_REL(subtract, 8)
189 ATOMIC_ACQ_REL(set, 16)
190 ATOMIC_ACQ_REL(clear, 16)
191 ATOMIC_ACQ_REL(add, 16)
192 ATOMIC_ACQ_REL(subtract, 16)
193 ATOMIC_ACQ_REL(set, 32)
194 ATOMIC_ACQ_REL(clear, 32)
195 ATOMIC_ACQ_REL(add, 32)
196 ATOMIC_ACQ_REL(subtract, 32)
198 ATOMIC_ACQ_REL(set, 64)
199 ATOMIC_ACQ_REL(clear, 64)
200 ATOMIC_ACQ_REL(add, 64)
201 ATOMIC_ACQ_REL(subtract, 64)
204 #undef ATOMIC_ACQ_REL
207 * We assume that a = b will do atomic loads and stores.
209 #define ATOMIC_STORE_LOAD(WIDTH) \
210 static __inline uint##WIDTH##_t \
211 atomic_load_acq_##WIDTH(__volatile uint##WIDTH##_t *p) \
220 static __inline void \
221 atomic_store_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
227 ATOMIC_STORE_LOAD(32)
228 ATOMIC_STORE_LOAD(64)
229 void atomic_store_64 (__volatile uint64_t *, uint64_t *);
230 void atomic_load_64 (__volatile uint64_t *, uint64_t *);
232 #undef ATOMIC_STORE_LOAD
235 * Atomically compare the value stored at *p with cmpval and if the
236 * two values are equal, update the value of *p with newval. Returns
237 * zero if the compare failed, nonzero otherwise.
239 static __inline uint32_t
240 atomic_cmpset_32(__volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
245 "1:\tll %0, %4\n\t" /* load old value */
246 "bne %0, %2, 2f\n\t" /* compare */
247 "move %0, %3\n\t" /* value to store */
248 "sc %0, %1\n\t" /* attempt to store */
249 "beqz %0, 1b\n\t" /* if it failed, spin */
254 : "=&r" (ret), "=m" (*p)
255 : "r" (cmpval), "r" (newval), "m" (*p)
262 * Atomically compare the value stored at *p with cmpval and if the
263 * two values are equal, update the value of *p with newval. Returns
264 * zero if the compare failed, nonzero otherwise.
266 static __inline uint32_t
267 atomic_cmpset_acq_32(__volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
271 retval = atomic_cmpset_32(p, cmpval, newval);
276 static __inline uint32_t
277 atomic_cmpset_rel_32(__volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
280 return (atomic_cmpset_32(p, cmpval, newval));
284 * Atomically add the value of v to the integer pointed to by p and return
285 * the previous value of *p.
287 static __inline uint32_t
288 atomic_fetchadd_32(__volatile uint32_t *p, uint32_t v)
290 uint32_t value, temp;
293 "1:\tll %0, %1\n\t" /* load old value */
294 "addu %2, %3, %0\n\t" /* calculate new value */
295 "sc %2, %1\n\t" /* attempt to store */
296 "beqz %2, 1b\n\t" /* spin if failed */
297 : "=&r" (value), "=m" (*p), "=r" (temp)
298 : "r" (v), "m" (*p));
302 /* Operations on chars. */
303 #define atomic_set_char atomic_set_8
304 #define atomic_set_acq_char atomic_set_acq_8
305 #define atomic_set_rel_char atomic_set_rel_8
306 #define atomic_clear_char atomic_clear_8
307 #define atomic_clear_acq_char atomic_clear_acq_8
308 #define atomic_clear_rel_char atomic_clear_rel_8
309 #define atomic_add_char atomic_add_8
310 #define atomic_add_acq_char atomic_add_acq_8
311 #define atomic_add_rel_char atomic_add_rel_8
312 #define atomic_subtract_char atomic_subtract_8
313 #define atomic_subtract_acq_char atomic_subtract_acq_8
314 #define atomic_subtract_rel_char atomic_subtract_rel_8
316 /* Operations on shorts. */
317 #define atomic_set_short atomic_set_16
318 #define atomic_set_acq_short atomic_set_acq_16
319 #define atomic_set_rel_short atomic_set_rel_16
320 #define atomic_clear_short atomic_clear_16
321 #define atomic_clear_acq_short atomic_clear_acq_16
322 #define atomic_clear_rel_short atomic_clear_rel_16
323 #define atomic_add_short atomic_add_16
324 #define atomic_add_acq_short atomic_add_acq_16
325 #define atomic_add_rel_short atomic_add_rel_16
326 #define atomic_subtract_short atomic_subtract_16
327 #define atomic_subtract_acq_short atomic_subtract_acq_16
328 #define atomic_subtract_rel_short atomic_subtract_rel_16
330 /* Operations on ints. */
331 #define atomic_set_int atomic_set_32
332 #define atomic_set_acq_int atomic_set_acq_32
333 #define atomic_set_rel_int atomic_set_rel_32
334 #define atomic_clear_int atomic_clear_32
335 #define atomic_clear_acq_int atomic_clear_acq_32
336 #define atomic_clear_rel_int atomic_clear_rel_32
337 #define atomic_add_int atomic_add_32
338 #define atomic_add_acq_int atomic_add_acq_32
339 #define atomic_add_rel_int atomic_add_rel_32
340 #define atomic_subtract_int atomic_subtract_32
341 #define atomic_subtract_acq_int atomic_subtract_acq_32
342 #define atomic_subtract_rel_int atomic_subtract_rel_32
343 #define atomic_cmpset_int atomic_cmpset_32
344 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
345 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
346 #define atomic_load_acq_int atomic_load_acq_32
347 #define atomic_store_rel_int atomic_store_rel_32
348 #define atomic_readandclear_int atomic_readandclear_32
349 #define atomic_readandset_int atomic_readandset_32
350 #define atomic_fetchadd_int atomic_fetchadd_32
353 /* Operations on longs. */
354 #define atomic_set_long atomic_set_64
355 #define atomic_set_acq_long atomic_set_acq_64
356 #define atomic_set_rel_long atomic_set_rel_64
357 #define atomic_clear_long atomic_clear_64
358 #define atomic_clear_acq_long atomic_clear_acq_64
359 #define atomic_clear_rel_long atomic_clear_rel_64
360 #define atomic_add_long atomic_add_64
361 #define atomic_add_acq_long atomic_add_acq_64
362 #define atomic_add_rel_long atomic_add_rel_64
363 #define atomic_subtract_long atomic_subtract_64
364 #define atomic_subtract_acq_long atomic_subtract_acq_64
365 #define atomic_subtract_rel_long atomic_subtract_rel_64
366 #define atomic_cmpset_long atomic_cmpset_64
367 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
368 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
369 #define atomic_load_acq_long atomic_load_acq_64
370 #define atomic_store_rel_long atomic_store_rel_64
371 #define atomic_fetchadd_long atomic_fetchadd_64
372 #define atomic_readandclear_long atomic_readandclear_64
374 /* Operations on pointers. */
375 #define atomic_set_ptr atomic_set_64
376 #define atomic_set_acq_ptr atomic_set_acq_64
377 #define atomic_set_rel_ptr atomic_set_rel_64
378 #define atomic_clear_ptr atomic_clear_64
379 #define atomic_clear_acq_ptr atomic_clear_acq_64
380 #define atomic_clear_rel_ptr atomic_clear_rel_64
381 #define atomic_add_ptr atomic_add_64
382 #define atomic_add_acq_ptr atomic_add_acq_64
383 #define atomic_add_rel_ptr atomic_add_rel_64
384 #define atomic_subtract_ptr atomic_subtract_64
385 #define atomic_subtract_acq_ptr atomic_subtract_acq_64
386 #define atomic_subtract_rel_ptr atomic_subtract_rel_64
387 #define atomic_cmpset_ptr atomic_cmpset_64
388 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_64
389 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_64
390 #define atomic_load_acq_ptr atomic_load_acq_64
391 #define atomic_store_rel_ptr atomic_store_rel_64
392 #define atomic_readandclear_ptr atomic_readandclear_64
396 /* Operations on longs. */
397 #define atomic_set_long atomic_set_32
398 #define atomic_set_acq_long atomic_set_acq_32
399 #define atomic_set_rel_long atomic_set_rel_32
400 #define atomic_clear_long atomic_clear_32
401 #define atomic_clear_acq_long atomic_clear_acq_32
402 #define atomic_clear_rel_long atomic_clear_rel_32
403 #define atomic_add_long(p, v) \
404 atomic_add_32((volatile u_int *)(p), (u_int)(v))
405 #define atomic_add_acq_long atomic_add_acq_32
406 #define atomic_add_rel_long atomic_add_rel_32
407 #define atomic_subtract_long(p, v) \
408 atomic_subtract_32((volatile u_int *)(p), (u_int)(v))
409 #define atomic_subtract_acq_long atomic_subtract_acq_32
410 #define atomic_subtract_rel_long atomic_subtract_rel_32
411 #define atomic_cmpset_long atomic_cmpset_32
412 #define atomic_cmpset_acq_long(p, cmpval, newval) \
413 atomic_cmpset_acq_32((volatile u_int *)(p), \
414 (u_int)(cmpval), (u_int)(newval))
415 #define atomic_cmpset_rel_long(p, cmpval, newval) \
416 atomic_cmpset_rel_32((volatile u_int *)(p), \
417 (u_int)(cmpval), (u_int)(newval))
418 #define atomic_load_acq_long atomic_load_acq_32
419 #define atomic_store_rel_long atomic_store_rel_32
420 #define atomic_fetchadd_long(p, v) \
421 atomic_fetchadd_32((volatile u_int *)(p), (u_int)(v))
422 #define atomic_readandclear_long atomic_readandclear_32
424 /* Operations on pointers. */
425 #define atomic_set_ptr atomic_set_32
426 #define atomic_set_acq_ptr atomic_set_acq_32
427 #define atomic_set_rel_ptr atomic_set_rel_32
428 #define atomic_clear_ptr atomic_clear_32
429 #define atomic_clear_acq_ptr atomic_clear_acq_32
430 #define atomic_clear_rel_ptr atomic_clear_rel_32
431 #define atomic_add_ptr atomic_add_32
432 #define atomic_add_acq_ptr atomic_add_acq_32
433 #define atomic_add_rel_ptr atomic_add_rel_32
434 #define atomic_subtract_ptr atomic_subtract_32
435 #define atomic_subtract_acq_ptr atomic_subtract_acq_32
436 #define atomic_subtract_rel_ptr atomic_subtract_rel_32
437 #define atomic_cmpset_ptr atomic_cmpset_32
438 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_32
439 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_32
440 #define atomic_load_acq_ptr atomic_load_acq_32
441 #define atomic_store_rel_ptr atomic_store_rel_32
442 #define atomic_readandclear_ptr atomic_readandclear_32
443 #endif /* __mips64 */
445 #endif /* ! _MACHINE_ATOMIC_H_ */