1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 * This file defines a generic API for atomic operations.
16 #include <rte_common.h>
20 /** @name Memory Barrier
24 * General memory barrier.
26 * Guarantees that the LOAD and STORE operations generated before the
27 * barrier occur before the LOAD and STORE operations generated after.
28 * This function is architecture dependent.
30 static inline void rte_mb(void);
33 * Write memory barrier.
35 * Guarantees that the STORE operations generated before the barrier
36 * occur before the STORE operations generated after.
37 * This function is architecture dependent.
39 static inline void rte_wmb(void);
42 * Read memory barrier.
44 * Guarantees that the LOAD operations generated before the barrier
45 * occur before the LOAD operations generated after.
46 * This function is architecture dependent.
48 static inline void rte_rmb(void);
51 /** @name SMP Memory Barrier
55 * General memory barrier between lcores
57 * Guarantees that the LOAD and STORE operations that precede the
58 * rte_smp_mb() call are globally visible across the lcores
59 * before the LOAD and STORE operations that follows it.
61 static inline void rte_smp_mb(void);
64 * Write memory barrier between lcores
66 * Guarantees that the STORE operations that precede the
67 * rte_smp_wmb() call are globally visible across the lcores
68 * before the STORE operations that follows it.
70 static inline void rte_smp_wmb(void);
73 * Read memory barrier between lcores
75 * Guarantees that the LOAD operations that precede the
76 * rte_smp_rmb() call are globally visible across the lcores
77 * before the LOAD operations that follows it.
79 static inline void rte_smp_rmb(void);
82 /** @name I/O Memory Barrier
86 * General memory barrier for I/O device
88 * Guarantees that the LOAD and STORE operations that precede the
89 * rte_io_mb() call are visible to I/O device or CPU before the
90 * LOAD and STORE operations that follow it.
92 static inline void rte_io_mb(void);
95 * Write memory barrier for I/O device
97 * Guarantees that the STORE operations that precede the
98 * rte_io_wmb() call are visible to I/O device before the STORE
99 * operations that follow it.
101 static inline void rte_io_wmb(void);
104 * Read memory barrier for IO device
106 * Guarantees that the LOAD operations on I/O device that precede the
107 * rte_io_rmb() call are visible to CPU before the LOAD
108 * operations that follow it.
110 static inline void rte_io_rmb(void);
113 /** @name Coherent I/O Memory Barrier
115 * Coherent I/O memory barrier is a lightweight version of I/O memory
116 * barriers which are system-wide data synchronization barriers. This
117 * is for only coherent memory domain between lcore and I/O device but
118 * it is same as the I/O memory barriers in most of architectures.
119 * However, some architecture provides even lighter barriers which are
120 * somewhere in between I/O memory barriers and SMP memory barriers.
121 * For example, in case of ARMv8, DMB(data memory barrier) instruction
122 * can have different shareability domains - inner-shareable and
123 * outer-shareable. And inner-shareable DMB fits for SMP memory
124 * barriers and outer-shareable DMB for coherent I/O memory barriers,
125 * which acts on coherent memory.
127 * In most cases, I/O memory barriers are safer but if operations are
128 * on coherent memory instead of incoherent MMIO region of a device,
129 * then coherent I/O memory barriers can be used and this could bring
130 * performance gain depending on architectures.
134 * Write memory barrier for coherent memory between lcore and I/O device
136 * Guarantees that the STORE operations on coherent memory that
137 * precede the rte_cio_wmb() call are visible to I/O device before the
138 * STORE operations that follow it.
140 static inline void rte_cio_wmb(void);
143 * Read memory barrier for coherent memory between lcore and I/O device
145 * Guarantees that the LOAD operations on coherent memory updated by
146 * I/O device that precede the rte_cio_rmb() call are visible to CPU
147 * before the LOAD operations that follow it.
149 static inline void rte_cio_rmb(void);
152 #endif /* __DOXYGEN__ */
157 * Guarantees that operation reordering does not occur at compile time
158 * for operations directly before and after the barrier.
160 #define rte_compiler_barrier() do { \
161 asm volatile ("" : : : "memory"); \
164 /*------------------------- 16 bit atomic operations -------------------------*/
167 * Atomic compare and set.
169 * (atomic) equivalent to:
171 * *dst = src (all 16-bit words)
174 * The destination location into which the value will be written.
176 * The expected value.
180 * Non-zero on success; 0 on failure.
183 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src);
185 #ifdef RTE_FORCE_INTRINSICS
187 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
189 return __sync_bool_compare_and_swap(dst, exp, src);
196 * (atomic) equivalent to:
202 * The destination location into which the value will be written.
206 * The original value at that location
208 static inline uint16_t
209 rte_atomic16_exchange(volatile uint16_t *dst, uint16_t val);
211 #ifdef RTE_FORCE_INTRINSICS
212 static inline uint16_t
213 rte_atomic16_exchange(volatile uint16_t *dst, uint16_t val)
215 #if defined(RTE_ARCH_ARM64) && defined(RTE_TOOLCHAIN_CLANG)
216 return __atomic_exchange_n(dst, val, __ATOMIC_SEQ_CST);
218 return __atomic_exchange_2(dst, val, __ATOMIC_SEQ_CST);
224 * The atomic counter structure.
227 volatile int16_t cnt; /**< An internal counter value. */
231 * Static initializer for an atomic counter.
233 #define RTE_ATOMIC16_INIT(val) { (val) }
236 * Initialize an atomic counter.
239 * A pointer to the atomic counter.
242 rte_atomic16_init(rte_atomic16_t *v)
248 * Atomically read a 16-bit value from a counter.
251 * A pointer to the atomic counter.
253 * The value of the counter.
255 static inline int16_t
256 rte_atomic16_read(const rte_atomic16_t *v)
262 * Atomically set a counter to a 16-bit value.
265 * A pointer to the atomic counter.
267 * The new value for the counter.
270 rte_atomic16_set(rte_atomic16_t *v, int16_t new_value)
276 * Atomically add a 16-bit value to an atomic counter.
279 * A pointer to the atomic counter.
281 * The value to be added to the counter.
284 rte_atomic16_add(rte_atomic16_t *v, int16_t inc)
286 __sync_fetch_and_add(&v->cnt, inc);
290 * Atomically subtract a 16-bit value from an atomic counter.
293 * A pointer to the atomic counter.
295 * The value to be subtracted from the counter.
298 rte_atomic16_sub(rte_atomic16_t *v, int16_t dec)
300 __sync_fetch_and_sub(&v->cnt, dec);
304 * Atomically increment a counter by one.
307 * A pointer to the atomic counter.
310 rte_atomic16_inc(rte_atomic16_t *v);
312 #ifdef RTE_FORCE_INTRINSICS
314 rte_atomic16_inc(rte_atomic16_t *v)
316 rte_atomic16_add(v, 1);
321 * Atomically decrement a counter by one.
324 * A pointer to the atomic counter.
327 rte_atomic16_dec(rte_atomic16_t *v);
329 #ifdef RTE_FORCE_INTRINSICS
331 rte_atomic16_dec(rte_atomic16_t *v)
333 rte_atomic16_sub(v, 1);
338 * Atomically add a 16-bit value to a counter and return the result.
340 * Atomically adds the 16-bits value (inc) to the atomic counter (v) and
341 * returns the value of v after addition.
344 * A pointer to the atomic counter.
346 * The value to be added to the counter.
348 * The value of v after the addition.
350 static inline int16_t
351 rte_atomic16_add_return(rte_atomic16_t *v, int16_t inc)
353 return __sync_add_and_fetch(&v->cnt, inc);
357 * Atomically subtract a 16-bit value from a counter and return
360 * Atomically subtracts the 16-bit value (inc) from the atomic counter
361 * (v) and returns the value of v after the subtraction.
364 * A pointer to the atomic counter.
366 * The value to be subtracted from the counter.
368 * The value of v after the subtraction.
370 static inline int16_t
371 rte_atomic16_sub_return(rte_atomic16_t *v, int16_t dec)
373 return __sync_sub_and_fetch(&v->cnt, dec);
377 * Atomically increment a 16-bit counter by one and test.
379 * Atomically increments the atomic counter (v) by one and returns true if
380 * the result is 0, or false in all other cases.
383 * A pointer to the atomic counter.
385 * True if the result after the increment operation is 0; false otherwise.
387 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v);
389 #ifdef RTE_FORCE_INTRINSICS
390 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
392 return __sync_add_and_fetch(&v->cnt, 1) == 0;
397 * Atomically decrement a 16-bit counter by one and test.
399 * Atomically decrements the atomic counter (v) by one and returns true if
400 * the result is 0, or false in all other cases.
403 * A pointer to the atomic counter.
405 * True if the result after the decrement operation is 0; false otherwise.
407 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v);
409 #ifdef RTE_FORCE_INTRINSICS
410 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
412 return __sync_sub_and_fetch(&v->cnt, 1) == 0;
417 * Atomically test and set a 16-bit atomic counter.
419 * If the counter value is already set, return 0 (failed). Otherwise, set
420 * the counter value to 1 and return 1 (success).
423 * A pointer to the atomic counter.
425 * 0 if failed; else 1, success.
427 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v);
429 #ifdef RTE_FORCE_INTRINSICS
430 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
432 return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
437 * Atomically set a 16-bit counter to 0.
440 * A pointer to the atomic counter.
442 static inline void rte_atomic16_clear(rte_atomic16_t *v)
447 /*------------------------- 32 bit atomic operations -------------------------*/
450 * Atomic compare and set.
452 * (atomic) equivalent to:
454 * *dst = src (all 32-bit words)
457 * The destination location into which the value will be written.
459 * The expected value.
463 * Non-zero on success; 0 on failure.
466 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src);
468 #ifdef RTE_FORCE_INTRINSICS
470 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
472 return __sync_bool_compare_and_swap(dst, exp, src);
479 * (atomic) equivalent to:
485 * The destination location into which the value will be written.
489 * The original value at that location
491 static inline uint32_t
492 rte_atomic32_exchange(volatile uint32_t *dst, uint32_t val);
494 #ifdef RTE_FORCE_INTRINSICS
495 static inline uint32_t
496 rte_atomic32_exchange(volatile uint32_t *dst, uint32_t val)
498 #if defined(RTE_ARCH_ARM64) && defined(RTE_TOOLCHAIN_CLANG)
499 return __atomic_exchange_n(dst, val, __ATOMIC_SEQ_CST);
501 return __atomic_exchange_4(dst, val, __ATOMIC_SEQ_CST);
507 * The atomic counter structure.
510 volatile int32_t cnt; /**< An internal counter value. */
514 * Static initializer for an atomic counter.
516 #define RTE_ATOMIC32_INIT(val) { (val) }
519 * Initialize an atomic counter.
522 * A pointer to the atomic counter.
525 rte_atomic32_init(rte_atomic32_t *v)
531 * Atomically read a 32-bit value from a counter.
534 * A pointer to the atomic counter.
536 * The value of the counter.
538 static inline int32_t
539 rte_atomic32_read(const rte_atomic32_t *v)
545 * Atomically set a counter to a 32-bit value.
548 * A pointer to the atomic counter.
550 * The new value for the counter.
553 rte_atomic32_set(rte_atomic32_t *v, int32_t new_value)
559 * Atomically add a 32-bit value to an atomic counter.
562 * A pointer to the atomic counter.
564 * The value to be added to the counter.
567 rte_atomic32_add(rte_atomic32_t *v, int32_t inc)
569 __sync_fetch_and_add(&v->cnt, inc);
573 * Atomically subtract a 32-bit value from an atomic counter.
576 * A pointer to the atomic counter.
578 * The value to be subtracted from the counter.
581 rte_atomic32_sub(rte_atomic32_t *v, int32_t dec)
583 __sync_fetch_and_sub(&v->cnt, dec);
587 * Atomically increment a counter by one.
590 * A pointer to the atomic counter.
593 rte_atomic32_inc(rte_atomic32_t *v);
595 #ifdef RTE_FORCE_INTRINSICS
597 rte_atomic32_inc(rte_atomic32_t *v)
599 rte_atomic32_add(v, 1);
604 * Atomically decrement a counter by one.
607 * A pointer to the atomic counter.
610 rte_atomic32_dec(rte_atomic32_t *v);
612 #ifdef RTE_FORCE_INTRINSICS
614 rte_atomic32_dec(rte_atomic32_t *v)
616 rte_atomic32_sub(v,1);
621 * Atomically add a 32-bit value to a counter and return the result.
623 * Atomically adds the 32-bits value (inc) to the atomic counter (v) and
624 * returns the value of v after addition.
627 * A pointer to the atomic counter.
629 * The value to be added to the counter.
631 * The value of v after the addition.
633 static inline int32_t
634 rte_atomic32_add_return(rte_atomic32_t *v, int32_t inc)
636 return __sync_add_and_fetch(&v->cnt, inc);
640 * Atomically subtract a 32-bit value from a counter and return
643 * Atomically subtracts the 32-bit value (inc) from the atomic counter
644 * (v) and returns the value of v after the subtraction.
647 * A pointer to the atomic counter.
649 * The value to be subtracted from the counter.
651 * The value of v after the subtraction.
653 static inline int32_t
654 rte_atomic32_sub_return(rte_atomic32_t *v, int32_t dec)
656 return __sync_sub_and_fetch(&v->cnt, dec);
660 * Atomically increment a 32-bit counter by one and test.
662 * Atomically increments the atomic counter (v) by one and returns true if
663 * the result is 0, or false in all other cases.
666 * A pointer to the atomic counter.
668 * True if the result after the increment operation is 0; false otherwise.
670 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v);
672 #ifdef RTE_FORCE_INTRINSICS
673 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
675 return __sync_add_and_fetch(&v->cnt, 1) == 0;
680 * Atomically decrement a 32-bit counter by one and test.
682 * Atomically decrements the atomic counter (v) by one and returns true if
683 * the result is 0, or false in all other cases.
686 * A pointer to the atomic counter.
688 * True if the result after the decrement operation is 0; false otherwise.
690 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v);
692 #ifdef RTE_FORCE_INTRINSICS
693 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
695 return __sync_sub_and_fetch(&v->cnt, 1) == 0;
700 * Atomically test and set a 32-bit atomic counter.
702 * If the counter value is already set, return 0 (failed). Otherwise, set
703 * the counter value to 1 and return 1 (success).
706 * A pointer to the atomic counter.
708 * 0 if failed; else 1, success.
710 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v);
712 #ifdef RTE_FORCE_INTRINSICS
713 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
715 return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
720 * Atomically set a 32-bit counter to 0.
723 * A pointer to the atomic counter.
725 static inline void rte_atomic32_clear(rte_atomic32_t *v)
730 /*------------------------- 64 bit atomic operations -------------------------*/
733 * An atomic compare and set function used by the mutex functions.
734 * (atomic) equivalent to:
736 * *dst = src (all 64-bit words)
739 * The destination into which the value will be written.
741 * The expected value.
745 * Non-zero on success; 0 on failure.
748 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src);
750 #ifdef RTE_FORCE_INTRINSICS
752 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src)
754 return __sync_bool_compare_and_swap(dst, exp, src);
761 * (atomic) equivalent to:
767 * The destination location into which the value will be written.
771 * The original value at that location
773 static inline uint64_t
774 rte_atomic64_exchange(volatile uint64_t *dst, uint64_t val);
776 #ifdef RTE_FORCE_INTRINSICS
777 static inline uint64_t
778 rte_atomic64_exchange(volatile uint64_t *dst, uint64_t val)
780 #if defined(RTE_ARCH_ARM64) && defined(RTE_TOOLCHAIN_CLANG)
781 return __atomic_exchange_n(dst, val, __ATOMIC_SEQ_CST);
783 return __atomic_exchange_8(dst, val, __ATOMIC_SEQ_CST);
789 * The atomic counter structure.
792 volatile int64_t cnt; /**< Internal counter value. */
796 * Static initializer for an atomic counter.
798 #define RTE_ATOMIC64_INIT(val) { (val) }
801 * Initialize the atomic counter.
804 * A pointer to the atomic counter.
807 rte_atomic64_init(rte_atomic64_t *v);
809 #ifdef RTE_FORCE_INTRINSICS
811 rte_atomic64_init(rte_atomic64_t *v)
819 while (success == 0) {
821 success = rte_atomic64_cmpset((volatile uint64_t *)&v->cnt,
829 * Atomically read a 64-bit counter.
832 * A pointer to the atomic counter.
834 * The value of the counter.
836 static inline int64_t
837 rte_atomic64_read(rte_atomic64_t *v);
839 #ifdef RTE_FORCE_INTRINSICS
840 static inline int64_t
841 rte_atomic64_read(rte_atomic64_t *v)
849 while (success == 0) {
851 /* replace the value by itself */
852 success = rte_atomic64_cmpset((volatile uint64_t *)&v->cnt,
861 * Atomically set a 64-bit counter.
864 * A pointer to the atomic counter.
866 * The new value of the counter.
869 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value);
871 #ifdef RTE_FORCE_INTRINSICS
873 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value)
881 while (success == 0) {
883 success = rte_atomic64_cmpset((volatile uint64_t *)&v->cnt,
891 * Atomically add a 64-bit value to a counter.
894 * A pointer to the atomic counter.
896 * The value to be added to the counter.
899 rte_atomic64_add(rte_atomic64_t *v, int64_t inc);
901 #ifdef RTE_FORCE_INTRINSICS
903 rte_atomic64_add(rte_atomic64_t *v, int64_t inc)
905 __sync_fetch_and_add(&v->cnt, inc);
910 * Atomically subtract a 64-bit value from a counter.
913 * A pointer to the atomic counter.
915 * The value to be subtracted from the counter.
918 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec);
920 #ifdef RTE_FORCE_INTRINSICS
922 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec)
924 __sync_fetch_and_sub(&v->cnt, dec);
929 * Atomically increment a 64-bit counter by one and test.
932 * A pointer to the atomic counter.
935 rte_atomic64_inc(rte_atomic64_t *v);
937 #ifdef RTE_FORCE_INTRINSICS
939 rte_atomic64_inc(rte_atomic64_t *v)
941 rte_atomic64_add(v, 1);
946 * Atomically decrement a 64-bit counter by one and test.
949 * A pointer to the atomic counter.
952 rte_atomic64_dec(rte_atomic64_t *v);
954 #ifdef RTE_FORCE_INTRINSICS
956 rte_atomic64_dec(rte_atomic64_t *v)
958 rte_atomic64_sub(v, 1);
963 * Add a 64-bit value to an atomic counter and return the result.
965 * Atomically adds the 64-bit value (inc) to the atomic counter (v) and
966 * returns the value of v after the addition.
969 * A pointer to the atomic counter.
971 * The value to be added to the counter.
973 * The value of v after the addition.
975 static inline int64_t
976 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc);
978 #ifdef RTE_FORCE_INTRINSICS
979 static inline int64_t
980 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc)
982 return __sync_add_and_fetch(&v->cnt, inc);
987 * Subtract a 64-bit value from an atomic counter and return the result.
989 * Atomically subtracts the 64-bit value (dec) from the atomic counter (v)
990 * and returns the value of v after the subtraction.
993 * A pointer to the atomic counter.
995 * The value to be subtracted from the counter.
997 * The value of v after the subtraction.
999 static inline int64_t
1000 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec);
1002 #ifdef RTE_FORCE_INTRINSICS
1003 static inline int64_t
1004 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec)
1006 return __sync_sub_and_fetch(&v->cnt, dec);
1011 * Atomically increment a 64-bit counter by one and test.
1013 * Atomically increments the atomic counter (v) by one and returns
1014 * true if the result is 0, or false in all other cases.
1017 * A pointer to the atomic counter.
1019 * True if the result after the addition is 0; false otherwise.
1021 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v);
1023 #ifdef RTE_FORCE_INTRINSICS
1024 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v)
1026 return rte_atomic64_add_return(v, 1) == 0;
1031 * Atomically decrement a 64-bit counter by one and test.
1033 * Atomically decrements the atomic counter (v) by one and returns true if
1034 * the result is 0, or false in all other cases.
1037 * A pointer to the atomic counter.
1039 * True if the result after subtraction is 0; false otherwise.
1041 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v);
1043 #ifdef RTE_FORCE_INTRINSICS
1044 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v)
1046 return rte_atomic64_sub_return(v, 1) == 0;
1051 * Atomically test and set a 64-bit atomic counter.
1053 * If the counter value is already set, return 0 (failed). Otherwise, set
1054 * the counter value to 1 and return 1 (success).
1057 * A pointer to the atomic counter.
1059 * 0 if failed; else 1, success.
1061 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v);
1063 #ifdef RTE_FORCE_INTRINSICS
1064 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v)
1066 return rte_atomic64_cmpset((volatile uint64_t *)&v->cnt, 0, 1);
1071 * Atomically set a 64-bit counter to 0.
1074 * A pointer to the atomic counter.
1076 static inline void rte_atomic64_clear(rte_atomic64_t *v);
1078 #ifdef RTE_FORCE_INTRINSICS
1079 static inline void rte_atomic64_clear(rte_atomic64_t *v)
1081 rte_atomic64_set(v, 0);
1085 #endif /* _RTE_ATOMIC_H_ */