1 /* Atomic operations. PowerPC Common version.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <bits/wordsize.h>
24 /* Atomic operations. PowerPC64 version.
25 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
26 This file is part of the GNU C Library.
29 The GNU C Library is free software; you can redistribute it and/or
30 modify it under the terms of the GNU Lesser General Public
31 License as published by the Free Software Foundation; either
32 version 2.1 of the License, or (at your option) any later version.
34 The GNU C Library is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 Lesser General Public License for more details.
39 You should have received a copy of the GNU Lesser General Public
40 License along with the GNU C Library; if not, write to the Free
41 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
44 /* The 32-bit exchange_bool is different on powerpc64 because the subf
45 does signed 64-bit arthmatic while the lwarx is 32-bit unsigned
46 (a load word and zero (high 32) form) load.
47 In powerpc64 register values are 64-bit by default, including oldval.
48 The value in old val unknown sign extension, lwarx loads the 32-bit
49 value as unsigned. So we explicitly clear the high 32 bits in oldval. */
50 # define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
52 unsigned int __tmp, __tmp2; \
53 __asm__ __volatile__ (" clrldi %1,%1,32\n" \
54 "1: lwarx %0,0,%2\n" \
59 "2: " __ARCH_ACQ_INSTR \
60 : "=&r" (__tmp), "=r" (__tmp2) \
61 : "b" (mem), "1" (oldval), "r" (newval) \
66 # define __arch_compare_and_exchange_bool_32_rel(mem, newval, oldval) \
68 unsigned int __tmp, __tmp2; \
69 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
70 " clrldi %1,%1,32\n" \
71 "1: lwarx %0,0,%2\n" \
77 : "=&r" (__tmp), "=r" (__tmp2) \
78 : "b" (mem), "1" (oldval), "r" (newval) \
84 * Only powerpc64 processors support Load doubleword and reserve index (ldarx)
85 * and Store doubleword conditional indexed (stdcx) instructions. So here
86 * we define the 64-bit forms.
88 # define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
90 unsigned long __tmp; \
91 __asm__ __volatile__ ( \
92 "1: ldarx %0,0,%1\n" \
97 "2: " __ARCH_ACQ_INSTR \
99 : "b" (mem), "r" (oldval), "r" (newval) \
100 : "cr0", "memory"); \
104 # define __arch_compare_and_exchange_bool_64_rel(mem, newval, oldval) \
106 unsigned long __tmp; \
107 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
108 "1: ldarx %0,0,%1\n" \
109 " subf. %0,%2,%0\n" \
111 " stdcx. %3,0,%1\n" \
115 : "b" (mem), "r" (oldval), "r" (newval) \
116 : "cr0", "memory"); \
120 #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
122 __typeof (*(mem)) __tmp; \
123 __typeof (mem) __memp = (mem); \
124 __asm__ __volatile__ ( \
125 "1: ldarx %0,0,%1\n" \
128 " stdcx. %3,0,%1\n" \
130 "2: " __ARCH_ACQ_INSTR \
132 : "b" (__memp), "r" (oldval), "r" (newval) \
133 : "cr0", "memory"); \
137 #define __arch_compare_and_exchange_val_64_rel(mem, newval, oldval) \
139 __typeof (*(mem)) __tmp; \
140 __typeof (mem) __memp = (mem); \
141 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
142 "1: ldarx %0,0,%1\n" \
145 " stdcx. %3,0,%1\n" \
149 : "b" (__memp), "r" (oldval), "r" (newval) \
150 : "cr0", "memory"); \
154 # define __arch_atomic_exchange_64_acq(mem, value) \
156 __typeof (*mem) __val; \
157 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
158 "1: ldarx %0,0,%2\n" \
159 " stdcx. %3,0,%2\n" \
161 " " __ARCH_ACQ_INSTR \
162 : "=&r" (__val), "=m" (*mem) \
163 : "b" (mem), "r" (value), "m" (*mem) \
164 : "cr0", "memory"); \
168 # define __arch_atomic_exchange_64_rel(mem, value) \
170 __typeof (*mem) __val; \
171 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
172 "1: ldarx %0,0,%2\n" \
173 " stdcx. %3,0,%2\n" \
175 : "=&r" (__val), "=m" (*mem) \
176 : "b" (mem), "r" (value), "m" (*mem) \
177 : "cr0", "memory"); \
181 # define __arch_atomic_exchange_and_add_64(mem, value) \
183 __typeof (*mem) __val, __tmp; \
184 __asm__ __volatile__ ("1: ldarx %0,0,%3\n" \
186 " stdcx. %1,0,%3\n" \
188 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
189 : "b" (mem), "r" (value), "m" (*mem) \
190 : "cr0", "memory"); \
194 # define __arch_atomic_increment_val_64(mem) \
196 __typeof (*(mem)) __val; \
197 __asm__ __volatile__ ("1: ldarx %0,0,%2\n" \
199 " stdcx. %0,0,%2\n" \
201 : "=&b" (__val), "=m" (*mem) \
202 : "b" (mem), "m" (*mem) \
203 : "cr0", "memory"); \
207 # define __arch_atomic_decrement_val_64(mem) \
209 __typeof (*(mem)) __val; \
210 __asm__ __volatile__ ("1: ldarx %0,0,%2\n" \
212 " stdcx. %0,0,%2\n" \
214 : "=&b" (__val), "=m" (*mem) \
215 : "b" (mem), "m" (*mem) \
216 : "cr0", "memory"); \
220 # define __arch_atomic_decrement_if_positive_64(mem) \
221 ({ int __val, __tmp; \
222 __asm__ __volatile__ ("1: ldarx %0,0,%3\n" \
226 " stdcx. %1,0,%3\n" \
228 "2: " __ARCH_ACQ_INSTR \
229 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
230 : "b" (mem), "m" (*mem) \
231 : "cr0", "memory"); \
236 * All powerpc64 processors support the new "light weight" sync (lwsync).
238 # define atomic_read_barrier() __asm__ ("lwsync" ::: "memory")
240 * "light weight" sync can also be used for the release barrier.
243 # define __ARCH_REL_INSTR "lwsync"
247 /* Atomic operations. PowerPC32 version.
248 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
249 This file is part of the GNU C Library.
252 The GNU C Library is free software; you can redistribute it and/or
253 modify it under the terms of the GNU Lesser General Public
254 License as published by the Free Software Foundation; either
255 version 2.1 of the License, or (at your option) any later version.
257 The GNU C Library is distributed in the hope that it will be useful,
258 but WITHOUT ANY WARRANTY; without even the implied warranty of
259 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
260 Lesser General Public License for more details.
262 You should have received a copy of the GNU Lesser General Public
263 License along with the GNU C Library; if not, write to the Free
264 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
268 * The 32-bit exchange_bool is different on powerpc64 because the subf
269 * does signed 64-bit arthmatic while the lwarx is 32-bit unsigned
270 * (a load word and zero (high 32) form). So powerpc64 has a slightly
271 * different version in sysdeps/powerpc/powerpc64/bits/atomic.h.
273 # define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
275 unsigned int __tmp; \
276 __asm__ __volatile__ ( \
277 "1: lwarx %0,0,%1\n" \
278 " subf. %0,%2,%0\n" \
280 " stwcx. %3,0,%1\n" \
282 "2: " __ARCH_ACQ_INSTR \
284 : "b" (mem), "r" (oldval), "r" (newval) \
285 : "cr0", "memory"); \
289 # define __arch_compare_and_exchange_bool_32_rel(mem, newval, oldval) \
291 unsigned int __tmp; \
292 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
293 "1: lwarx %0,0,%1\n" \
294 " subf. %0,%2,%0\n" \
296 " stwcx. %3,0,%1\n" \
300 : "b" (mem), "r" (oldval), "r" (newval) \
301 : "cr0", "memory"); \
305 /* Powerpc32 processors don't implement the 64-bit (doubleword) forms of
306 load and reserve (ldarx) and store conditional (stdcx.) instructions.
307 So for powerpc32 we stub out the 64-bit forms. */
308 # define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
311 # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
312 (abort (), (__typeof (*mem)) 0)
314 # define __arch_compare_and_exchange_bool_64_rel(mem, newval, oldval) \
317 # define __arch_compare_and_exchange_val_64_rel(mem, newval, oldval) \
318 (abort (), (__typeof (*mem)) 0)
320 # define __arch_atomic_exchange_64_acq(mem, value) \
321 ({ abort (); (*mem) = (value); })
323 # define __arch_atomic_exchange_64_rel(mem, value) \
324 ({ abort (); (*mem) = (value); })
326 # define __arch_atomic_exchange_and_add_64(mem, value) \
327 ({ abort (); (*mem) = (value); })
329 # define __arch_atomic_increment_val_64(mem) \
330 ({ abort (); (*mem)++; })
332 # define __arch_atomic_decrement_val_64(mem) \
333 ({ abort (); (*mem)--; })
335 # define __arch_atomic_decrement_if_positive_64(mem) \
336 ({ abort (); (*mem)--; })
340 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
341 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
344 # define atomic_read_barrier() __asm__ ("lwsync" ::: "memory")
346 * "light weight" sync can also be used for the release barrier.
349 # define __ARCH_REL_INSTR "lwsync"
354 * Older powerpc32 processors don't support the new "light weight"
355 * sync (lwsync). So the only safe option is to use normal sync
356 * for all powerpc32 applications.
358 # define atomic_read_barrier() __asm__ ("sync" ::: "memory")
365 typedef int32_t atomic32_t;
366 typedef uint32_t uatomic32_t;
367 typedef int_fast32_t atomic_fast32_t;
368 typedef uint_fast32_t uatomic_fast32_t;
370 typedef int64_t atomic64_t;
371 typedef uint64_t uatomic64_t;
372 typedef int_fast64_t atomic_fast64_t;
373 typedef uint_fast64_t uatomic_fast64_t;
375 typedef intptr_t atomicptr_t;
376 typedef uintptr_t uatomicptr_t;
377 typedef intmax_t atomic_max_t;
378 typedef uintmax_t uatomic_max_t;
381 * Powerpc does not have byte and halfword forms of load and reserve and
382 * store conditional. So for powerpc we stub out the 8- and 16-bit forms.
384 #define __arch_compare_and_exchange_bool_8_acq(mem, newval, oldval) \
387 #define __arch_compare_and_exchange_bool_16_acq(mem, newval, oldval) \
390 #define __arch_compare_and_exchange_bool_8_rel(mem, newval, oldval) \
393 #define __arch_compare_and_exchange_bool_16_rel(mem, newval, oldval) \
397 # define __ARCH_ACQ_INSTR ""
398 # define __ARCH_REL_INSTR ""
400 # define __ARCH_ACQ_INSTR "isync"
401 # ifndef __ARCH_REL_INSTR
402 # define __ARCH_REL_INSTR "sync"
406 #ifndef MUTEX_HINT_ACQ
407 # define MUTEX_HINT_ACQ
409 #ifndef MUTEX_HINT_REL
410 # define MUTEX_HINT_REL
413 #define atomic_full_barrier() __asm__ ("sync" ::: "memory")
414 #define atomic_write_barrier() __asm__ ("eieio" ::: "memory")
416 #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
418 __typeof (*(mem)) __tmp; \
419 __typeof (mem) __memp = (mem); \
420 __asm__ __volatile__ ( \
421 "1: lwarx %0,0,%1\n" \
424 " stwcx. %3,0,%1\n" \
426 "2: " __ARCH_ACQ_INSTR \
428 : "b" (__memp), "r" (oldval), "r" (newval) \
429 : "cr0", "memory"); \
433 #define __arch_compare_and_exchange_val_32_rel(mem, newval, oldval) \
435 __typeof (*(mem)) __tmp; \
436 __typeof (mem) __memp = (mem); \
437 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
438 "1: lwarx %0,0,%1\n" \
441 " stwcx. %3,0,%1\n" \
445 : "b" (__memp), "r" (oldval), "r" (newval) \
446 : "cr0", "memory"); \
450 #define __arch_atomic_exchange_32_acq(mem, value) \
452 __typeof (*mem) __val; \
453 __asm__ __volatile__ ( \
454 "1: lwarx %0,0,%2\n" \
455 " stwcx. %3,0,%2\n" \
457 " " __ARCH_ACQ_INSTR \
458 : "=&r" (__val), "=m" (*mem) \
459 : "b" (mem), "r" (value), "m" (*mem) \
460 : "cr0", "memory"); \
464 #define __arch_atomic_exchange_32_rel(mem, value) \
466 __typeof (*mem) __val; \
467 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
468 "1: lwarx %0,0,%2\n" \
469 " stwcx. %3,0,%2\n" \
471 : "=&r" (__val), "=m" (*mem) \
472 : "b" (mem), "r" (value), "m" (*mem) \
473 : "cr0", "memory"); \
477 #define __arch_atomic_exchange_and_add_32(mem, value) \
479 __typeof (*mem) __val, __tmp; \
480 __asm__ __volatile__ ("1: lwarx %0,0,%3\n" \
482 " stwcx. %1,0,%3\n" \
484 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
485 : "b" (mem), "r" (value), "m" (*mem) \
486 : "cr0", "memory"); \
490 #define __arch_atomic_increment_val_32(mem) \
492 __typeof (*(mem)) __val; \
493 __asm__ __volatile__ ("1: lwarx %0,0,%2\n" \
495 " stwcx. %0,0,%2\n" \
497 : "=&b" (__val), "=m" (*mem) \
498 : "b" (mem), "m" (*mem) \
499 : "cr0", "memory"); \
503 #define __arch_atomic_decrement_val_32(mem) \
505 __typeof (*(mem)) __val; \
506 __asm__ __volatile__ ("1: lwarx %0,0,%2\n" \
508 " stwcx. %0,0,%2\n" \
510 : "=&b" (__val), "=m" (*mem) \
511 : "b" (mem), "m" (*mem) \
512 : "cr0", "memory"); \
516 #define __arch_atomic_decrement_if_positive_32(mem) \
517 ({ int __val, __tmp; \
518 __asm__ __volatile__ ("1: lwarx %0,0,%3\n" \
522 " stwcx. %1,0,%3\n" \
524 "2: " __ARCH_ACQ_INSTR \
525 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
526 : "b" (mem), "m" (*mem) \
527 : "cr0", "memory"); \
531 #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
533 __typeof (*(mem)) __result; \
534 if (sizeof (*mem) == 4) \
535 __result = __arch_compare_and_exchange_val_32_acq(mem, newval, oldval); \
536 else if (sizeof (*mem) == 8) \
537 __result = __arch_compare_and_exchange_val_64_acq(mem, newval, oldval); \
543 #define atomic_compare_and_exchange_val_rel(mem, newval, oldval) \
545 __typeof (*(mem)) __result; \
546 if (sizeof (*mem) == 4) \
547 __result = __arch_compare_and_exchange_val_32_rel(mem, newval, oldval); \
548 else if (sizeof (*mem) == 8) \
549 __result = __arch_compare_and_exchange_val_64_rel(mem, newval, oldval); \
555 #define atomic_exchange_acq(mem, value) \
557 __typeof (*(mem)) __result; \
558 if (sizeof (*mem) == 4) \
559 __result = __arch_atomic_exchange_32_acq (mem, value); \
560 else if (sizeof (*mem) == 8) \
561 __result = __arch_atomic_exchange_64_acq (mem, value); \
567 #define atomic_exchange_rel(mem, value) \
569 __typeof (*(mem)) __result; \
570 if (sizeof (*mem) == 4) \
571 __result = __arch_atomic_exchange_32_rel (mem, value); \
572 else if (sizeof (*mem) == 8) \
573 __result = __arch_atomic_exchange_64_rel (mem, value); \
579 #define atomic_exchange_and_add(mem, value) \
581 __typeof (*(mem)) __result; \
582 if (sizeof (*mem) == 4) \
583 __result = __arch_atomic_exchange_and_add_32 (mem, value); \
584 else if (sizeof (*mem) == 8) \
585 __result = __arch_atomic_exchange_and_add_64 (mem, value); \
591 #define atomic_increment_val(mem) \
593 __typeof (*(mem)) __result; \
594 if (sizeof (*(mem)) == 4) \
595 __result = __arch_atomic_increment_val_32 (mem); \
596 else if (sizeof (*(mem)) == 8) \
597 __result = __arch_atomic_increment_val_64 (mem); \
603 #define atomic_increment(mem) ({ atomic_increment_val (mem); (void) 0; })
605 #define atomic_decrement_val(mem) \
607 __typeof (*(mem)) __result; \
608 if (sizeof (*(mem)) == 4) \
609 __result = __arch_atomic_decrement_val_32 (mem); \
610 else if (sizeof (*(mem)) == 8) \
611 __result = __arch_atomic_decrement_val_64 (mem); \
617 #define atomic_decrement(mem) ({ atomic_decrement_val (mem); (void) 0; })
620 /* Decrement *MEM if it is > 0, and return the old value. */
621 #define atomic_decrement_if_positive(mem) \
622 ({ __typeof (*(mem)) __result; \
623 if (sizeof (*mem) == 4) \
624 __result = __arch_atomic_decrement_if_positive_32 (mem); \
625 else if (sizeof (*mem) == 8) \
626 __result = __arch_atomic_decrement_if_positive_64 (mem); \