1 /* Low-level functions for atomic operations. Mips version.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _MIPS_BITS_ATOMIC_H
20 #define _MIPS_BITS_ATOMIC_H 1
26 typedef int32_t atomic32_t;
27 typedef uint32_t uatomic32_t;
28 typedef int_fast32_t atomic_fast32_t;
29 typedef uint_fast32_t uatomic_fast32_t;
31 typedef int64_t atomic64_t;
32 typedef uint64_t uatomic64_t;
33 typedef int_fast64_t atomic_fast64_t;
34 typedef uint_fast64_t uatomic_fast64_t;
36 typedef intptr_t atomicptr_t;
37 typedef uintptr_t uatomicptr_t;
38 typedef intmax_t atomic_max_t;
39 typedef uintmax_t uatomic_max_t;
41 #if _MIPS_SIM == _ABIO32
42 #define MIPS_PUSH_MIPS2 ".set mips2\n\t"
44 #define MIPS_PUSH_MIPS2
47 #if _MIPS_SIM == _ABIO32
48 #define __HAVE_64B_ATOMICS 0
50 #define __HAVE_64B_ATOMICS 1
53 /* See the comments in <sys/asm.h> about the use of the sync instruction. */
55 # define MIPS_SYNC sync
58 /* Certain revisions of the R10000 Processor need an LL/SC Workaround
59 enabled. Revisions before 3.0 misbehave on atomic operations, and
60 Revs 2.6 and lower deadlock after several seconds due to other errata.
62 To quote the R10K Errata:
63 Workaround: The basic idea is to inhibit the four instructions
64 from simultaneously becoming active in R10000. Padding all
65 ll/sc sequences with nops or changing the looping branch in the
66 routines to a branch likely (which is always predicted taken
67 by R10000) will work. The nops should go after the loop, and the
68 number of them should be 28. This number could be decremented for
69 each additional instruction in the ll/sc loop such as the lock
70 modifier(s) between the ll and sc, the looping branch and its
71 delay slot. For typical short routines with one ll/sc loop, any
72 instructions after the loop could also count as a decrement. The
73 nop workaround pollutes the cache more but would be a few cycles
74 faster if all the code is in the cache and the looping branch
75 is predicted not taken. */
78 #ifdef _MIPS_ARCH_R10000
79 #define R10K_BEQZ_INSN "beqzl"
81 #define R10K_BEQZ_INSN "beqz"
84 #define MIPS_SYNC_STR_2(X) #X
85 #define MIPS_SYNC_STR_1(X) MIPS_SYNC_STR_2(X)
86 #define MIPS_SYNC_STR MIPS_SYNC_STR_1(MIPS_SYNC)
88 #if __GNUC_PREREQ (4, 8) || (defined __mips16 && __GNUC_PREREQ (4, 7))
89 /* The __atomic_* builtins are available in GCC 4.7 and later, but MIPS
90 support for their efficient implementation was added only in GCC 4.8.
91 We still want to use them even with GCC 4.7 for MIPS16 code where we
92 have no assembly alternative available and want to avoid the __sync_*
93 builtins if at all possible. */
95 #define USE_ATOMIC_COMPILER_BUILTINS 1
97 /* Compare and exchange.
98 For all "bool" routines, we return FALSE if exchange succesful. */
100 # define __arch_compare_and_exchange_bool_8_int(mem, newval, oldval, model) \
103 # define __arch_compare_and_exchange_bool_16_int(mem, newval, oldval, model) \
106 # define __arch_compare_and_exchange_bool_32_int(mem, newval, oldval, model) \
108 typeof (*mem) __oldval = (oldval); \
109 !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
110 model, __ATOMIC_RELAXED); \
113 # define __arch_compare_and_exchange_val_8_int(mem, newval, oldval, model) \
114 (abort (), (typeof(*mem)) 0)
116 # define __arch_compare_and_exchange_val_16_int(mem, newval, oldval, model) \
117 (abort (), (typeof(*mem)) 0)
119 # define __arch_compare_and_exchange_val_32_int(mem, newval, oldval, model) \
121 typeof (*mem) __oldval = (oldval); \
122 __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
123 model, __ATOMIC_RELAXED); \
127 # if _MIPS_SIM == _ABIO32
128 /* We can't do an atomic 64-bit operation in O32. */
129 # define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
131 # define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
132 (abort (), (typeof(*mem)) 0)
134 # define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
135 __arch_compare_and_exchange_bool_32_int (mem, newval, oldval, model)
136 # define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
137 __arch_compare_and_exchange_val_32_int (mem, newval, oldval, model)
140 /* Compare and exchange with "acquire" semantics, ie barrier after. */
142 # define atomic_compare_and_exchange_bool_acq(mem, new, old) \
143 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
144 mem, new, old, __ATOMIC_ACQUIRE)
146 # define atomic_compare_and_exchange_val_acq(mem, new, old) \
147 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
148 mem, new, old, __ATOMIC_ACQUIRE)
150 /* Compare and exchange with "release" semantics, ie barrier before. */
152 # define atomic_compare_and_exchange_bool_rel(mem, new, old) \
153 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
154 mem, new, old, __ATOMIC_RELEASE)
156 # define atomic_compare_and_exchange_val_rel(mem, new, old) \
157 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
158 mem, new, old, __ATOMIC_RELEASE)
161 /* Atomic exchange (without compare). */
163 # define __arch_exchange_8_int(mem, newval, model) \
164 (abort (), (typeof(*mem)) 0)
166 # define __arch_exchange_16_int(mem, newval, model) \
167 (abort (), (typeof(*mem)) 0)
169 # define __arch_exchange_32_int(mem, newval, model) \
170 __atomic_exchange_n (mem, newval, model)
172 # if _MIPS_SIM == _ABIO32
173 /* We can't do an atomic 64-bit operation in O32. */
174 # define __arch_exchange_64_int(mem, newval, model) \
175 (abort (), (typeof(*mem)) 0)
177 # define __arch_exchange_64_int(mem, newval, model) \
178 __atomic_exchange_n (mem, newval, model)
181 # define atomic_exchange_acq(mem, value) \
182 __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_ACQUIRE)
184 # define atomic_exchange_rel(mem, value) \
185 __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_RELEASE)
188 /* Atomically add value and return the previous (unincremented) value. */
190 # define __arch_exchange_and_add_8_int(mem, value, model) \
191 (abort (), (typeof(*mem)) 0)
193 # define __arch_exchange_and_add_16_int(mem, value, model) \
194 (abort (), (typeof(*mem)) 0)
196 # define __arch_exchange_and_add_32_int(mem, value, model) \
197 __atomic_fetch_add (mem, value, model)
199 # if _MIPS_SIM == _ABIO32
200 /* We can't do an atomic 64-bit operation in O32. */
201 # define __arch_exchange_and_add_64_int(mem, value, model) \
202 (abort (), (typeof(*mem)) 0)
204 # define __arch_exchange_and_add_64_int(mem, value, model) \
205 __atomic_fetch_add (mem, value, model)
208 # define atomic_exchange_and_add_acq(mem, value) \
209 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
212 # define atomic_exchange_and_add_rel(mem, value) \
213 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
216 #elif defined __mips16 /* !__GNUC_PREREQ (4, 7) */
217 /* This implementation using __sync* builtins will be removed once glibc
218 requires GCC 4.7 or later to build. */
220 # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
221 __sync_val_compare_and_swap ((mem), (oldval), (newval))
222 # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
223 (!__sync_bool_compare_and_swap ((mem), (oldval), (newval)))
225 # define atomic_exchange_acq(mem, newval) \
226 __sync_lock_test_and_set ((mem), (newval))
228 # define atomic_exchange_and_add(mem, val) \
229 __sync_fetch_and_add ((mem), (val))
231 # define atomic_bit_test_set(mem, bit) \
232 ({ __typeof (bit) __bit = (bit); \
233 (__sync_fetch_and_or ((mem), 1 << (__bit)) & (1 << (__bit))); })
235 # define atomic_and(mem, mask) (void) __sync_fetch_and_and ((mem), (mask))
236 # define atomic_and_val(mem, mask) __sync_fetch_and_and ((mem), (mask))
238 # define atomic_or(mem, mask) (void) __sync_fetch_and_or ((mem), (mask))
239 # define atomic_or_val(mem, mask) __sync_fetch_and_or ((mem), (mask))
241 #else /* !__mips16 && !__GNUC_PREREQ (4, 8) */
242 /* This implementation using inline assembly will be removed once glibc
243 requires GCC 4.8 or later to build. */
245 #define USE_ATOMIC_COMPILER_BUILTINS 0
247 /* Compare and exchange. For all of the "xxx" routines, we expect a
248 "__prev" and a "__cmp" variable to be provided by the enclosing scope,
249 in which values are returned. */
251 # define __arch_compare_and_exchange_xxx_8_int(mem, newval, oldval, rel, acq) \
252 (abort (), __prev = 0, __cmp = 0, (void) __cmp)
254 # define __arch_compare_and_exchange_xxx_16_int(mem, newval, oldval, rel, acq) \
255 (abort (), __prev = 0, __cmp = 0, (void) __cmp)
257 # define __arch_compare_and_exchange_xxx_32_int(mem, newval, oldval, rel, acq) \
258 __asm__ __volatile__ ( \
268 R10K_BEQZ_INSN" %1,1b\n" \
272 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
273 : "r" (oldval), "r" (newval), "m" (*mem) \
276 # if _MIPS_SIM == _ABIO32
277 /* We can't do an atomic 64-bit operation in O32. */
278 # define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
279 (abort (), __prev = 0, __cmp = 0, (void) __cmp)
281 # define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
282 __asm__ __volatile__ ("\n" \
292 R10K_BEQZ_INSN" %1,1b\n" \
296 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
297 : "r" (oldval), "r" (newval), "m" (*mem) \
301 /* For all "bool" routines, we return FALSE if exchange succesful. */
303 # define __arch_compare_and_exchange_bool_8_int(mem, new, old, rel, acq) \
304 ({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
305 __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
308 # define __arch_compare_and_exchange_bool_16_int(mem, new, old, rel, acq) \
309 ({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
310 __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
313 # define __arch_compare_and_exchange_bool_32_int(mem, new, old, rel, acq) \
314 ({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
315 __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
318 # define __arch_compare_and_exchange_bool_64_int(mem, new, old, rel, acq) \
319 ({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
320 __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
323 /* For all "val" routines, return the old value whether exchange
324 successful or not. */
326 # define __arch_compare_and_exchange_val_8_int(mem, new, old, rel, acq) \
327 ({ typeof (*mem) __prev; int __cmp; \
328 __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
329 (typeof (*mem))__prev; })
331 # define __arch_compare_and_exchange_val_16_int(mem, new, old, rel, acq) \
332 ({ typeof (*mem) __prev; int __cmp; \
333 __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
334 (typeof (*mem))__prev; })
336 # define __arch_compare_and_exchange_val_32_int(mem, new, old, rel, acq) \
337 ({ typeof (*mem) __prev; int __cmp; \
338 __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
339 (typeof (*mem))__prev; })
341 # define __arch_compare_and_exchange_val_64_int(mem, new, old, rel, acq) \
342 ({ typeof (*mem) __prev; int __cmp; \
343 __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
344 (typeof (*mem))__prev; })
346 /* Compare and exchange with "acquire" semantics, ie barrier after. */
348 # define atomic_compare_and_exchange_bool_acq(mem, new, old) \
349 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
350 mem, new, old, "", MIPS_SYNC_STR)
352 # define atomic_compare_and_exchange_val_acq(mem, new, old) \
353 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
354 mem, new, old, "", MIPS_SYNC_STR)
356 /* Compare and exchange with "release" semantics, ie barrier before. */
358 # define atomic_compare_and_exchange_bool_rel(mem, new, old) \
359 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
360 mem, new, old, MIPS_SYNC_STR, "")
362 # define atomic_compare_and_exchange_val_rel(mem, new, old) \
363 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
364 mem, new, old, MIPS_SYNC_STR, "")
368 /* Atomic exchange (without compare). */
370 # define __arch_exchange_xxx_8_int(mem, newval, rel, acq) \
371 (abort (), (typeof(*mem)) 0)
373 # define __arch_exchange_xxx_16_int(mem, newval, rel, acq) \
374 (abort (), (typeof(*mem)) 0)
376 # define __arch_exchange_xxx_32_int(mem, newval, rel, acq) \
377 ({ typeof (*mem) __prev; int __cmp; \
378 __asm__ __volatile__ ("\n" \
386 R10K_BEQZ_INSN" %1,1b\n" \
390 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
391 : "r" (newval), "m" (*mem) \
395 # if _MIPS_SIM == _ABIO32
396 /* We can't do an atomic 64-bit operation in O32. */
397 # define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
398 (abort (), (typeof(*mem)) 0)
400 # define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
401 ({ typeof (*mem) __prev; int __cmp; \
402 __asm__ __volatile__ ("\n" \
410 R10K_BEQZ_INSN" %1,1b\n" \
414 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
415 : "r" (newval), "m" (*mem) \
420 # define atomic_exchange_acq(mem, value) \
421 __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, "", MIPS_SYNC_STR)
423 # define atomic_exchange_rel(mem, value) \
424 __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, MIPS_SYNC_STR, "")
427 /* Atomically add value and return the previous (unincremented) value. */
429 # define __arch_exchange_and_add_8_int(mem, newval, rel, acq) \
430 (abort (), (typeof(*mem)) 0)
432 # define __arch_exchange_and_add_16_int(mem, newval, rel, acq) \
433 (abort (), (typeof(*mem)) 0)
435 # define __arch_exchange_and_add_32_int(mem, value, rel, acq) \
436 ({ typeof (*mem) __prev; int __cmp; \
437 __asm__ __volatile__ ("\n" \
443 "addu %1,%0,%3\n\t" \
445 R10K_BEQZ_INSN" %1,1b\n" \
449 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
450 : "r" (value), "m" (*mem) \
454 # if _MIPS_SIM == _ABIO32
455 /* We can't do an atomic 64-bit operation in O32. */
456 # define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
457 (abort (), (typeof(*mem)) 0)
459 # define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
460 ({ typeof (*mem) __prev; int __cmp; \
461 __asm__ __volatile__ ( \
467 "daddu %1,%0,%3\n\t" \
469 R10K_BEQZ_INSN" %1,1b\n" \
473 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
474 : "r" (value), "m" (*mem) \
479 # define atomic_exchange_and_add_acq(mem, value) \
480 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
483 # define atomic_exchange_and_add_rel(mem, value) \
484 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
487 #endif /* !__mips16 && !__GNUC_PREREQ (4, 8) */
489 /* TODO: More atomic operations could be implemented efficiently; only the
490 basic requirements are done. */
493 # define atomic_full_barrier() __sync_synchronize ()
495 #else /* !__mips16 */
496 # define atomic_full_barrier() \
497 __asm__ __volatile__ (".set push\n\t" \
499 MIPS_SYNC_STR "\n\t" \
500 ".set pop" : : : "memory")
501 #endif /* !__mips16 */
503 #endif /* bits/atomic.h */