1 // SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com>
3 // SPDX-License-Identifier: LGPL-2.1-or-later
5 #ifndef _URCU_ARCH_GENERIC_H
6 #define _URCU_ARCH_GENERIC_H
9 * arch_generic.h: common definitions for multiple architectures.
12 #include <urcu/compiler.h>
13 #include <urcu/config.h>
14 #include <urcu/syscall-compat.h>
20 #ifndef CAA_CACHE_LINE_SIZE
21 #define CAA_CACHE_LINE_SIZE 64
24 #if !defined(cmm_mc) && !defined(cmm_rmc) && !defined(cmm_wmc)
25 #define CONFIG_HAVE_MEM_COHERENCY
27 * Architectures with cache coherency must _not_ define cmm_mc/cmm_rmc/cmm_wmc.
29 * For them, cmm_mc/cmm_rmc/cmm_wmc are implemented with a simple
30 * compiler barrier; in addition, we provide defaults for cmm_mb (using
31 * GCC builtins) as well as cmm_rmb and cmm_wmb (defaulting to cmm_mb).
34 #ifdef CONFIG_RCU_USE_ATOMIC_BUILTINS
36 # ifdef CMM_SANITIZE_THREAD
38 * This makes TSAN quiet about unsupported thread fence.
40 static inline void _cmm_thread_fence_wrapper(void)
42 # if defined(__clang__)
43 # pragma clang diagnostic push
44 # pragma clang diagnostic ignored "-Wpragmas"
45 # pragma clang diagnostic ignored "-Wunknown-warning-option"
46 # pragma clang diagnostic ignored "-Wtsan"
47 # elif defined(__GNUC__)
48 # pragma GCC diagnostic push
49 # pragma GCC diagnostic ignored "-Wpragmas"
50 # pragma GCC diagnostic ignored "-Wtsan"
52 __atomic_thread_fence(__ATOMIC_SEQ_CST
);
53 # if defined(__clang__)
54 # pragma clang diagnostic pop
55 # elif defined(__GNUC__)
56 # pragma GCC diagnostic pop
59 # endif /* CMM_SANITIZE_THREAD */
62 # ifdef CMM_SANITIZE_THREAD
63 # define cmm_smp_mb() _cmm_thread_fence_wrapper()
65 # define cmm_smp_mb() __atomic_thread_fence(__ATOMIC_SEQ_CST)
66 # endif /* CMM_SANITIZE_THREAD */
67 # endif /* !cmm_smp_mb */
69 #endif /* CONFIG_RCU_USE_ATOMIC_BUILTINS */
73 * cmm_mb() expands to __sync_synchronize() instead of __atomic_thread_fence
74 * with SEQ_CST because the former "issues a full memory barrier" while the
75 * latter "acts as a synchronization fence between threads" which is too weak
76 * for what we want, for example with I/O devices.
78 * Even though sync_synchronize seems to be an alias for a sequential consistent
79 * atomic thread fence on every architecture on GCC and Clang, this assumption
80 * might be untrue in future. Therefore, the definitions above are used to
81 * ensure correct behavior in the future.
83 * The above defintions are quoted from the GCC manual.
86 #define cmm_mb() __sync_synchronize()
90 #define cmm_rmb() cmm_mb()
94 #define cmm_wmb() cmm_mb()
97 #define cmm_mc() cmm_barrier()
98 #define cmm_rmc() cmm_barrier()
99 #define cmm_wmc() cmm_barrier()
102 * Architectures without cache coherency need something like the following:
104 * #define cmm_mc() arch_cache_flush()
105 * #define cmm_rmc() arch_cache_flush_read()
106 * #define cmm_wmc() arch_cache_flush_write()
108 * Of these, only cmm_mc is mandatory. cmm_rmc and cmm_wmc default to
109 * cmm_mc. cmm_mb/cmm_rmb/cmm_wmb use these definitions by default:
111 * #define cmm_mb() cmm_mc()
112 * #define cmm_rmb() cmm_rmc()
113 * #define cmm_wmb() cmm_wmc()
117 #define cmm_mb() cmm_mc()
121 #define cmm_rmb() cmm_rmc()
125 #define cmm_wmb() cmm_wmc()
129 #define cmm_rmc() cmm_mc()
133 #define cmm_wmc() cmm_mc()
137 /* Nop everywhere except on alpha. */
138 #ifndef cmm_read_barrier_depends
139 #define cmm_read_barrier_depends()
142 #ifdef CONFIG_RCU_SMP
144 #define cmm_smp_mb() cmm_mb()
147 #define cmm_smp_rmb() cmm_rmb()
150 #define cmm_smp_wmb() cmm_wmb()
153 #define cmm_smp_mc() cmm_mc()
156 #define cmm_smp_rmc() cmm_rmc()
159 #define cmm_smp_wmc() cmm_wmc()
161 #ifndef cmm_smp_read_barrier_depends
162 #define cmm_smp_read_barrier_depends() cmm_read_barrier_depends()
166 #define cmm_smp_mb() cmm_barrier()
169 #define cmm_smp_rmb() cmm_barrier()
172 #define cmm_smp_wmb() cmm_barrier()
175 #define cmm_smp_mc() cmm_barrier()
178 #define cmm_smp_rmc() cmm_barrier()
181 #define cmm_smp_wmc() cmm_barrier()
183 #ifndef cmm_smp_read_barrier_depends
184 #define cmm_smp_read_barrier_depends()
188 #ifndef caa_cpu_relax
189 #define caa_cpu_relax() cmm_barrier()
192 #ifndef HAS_CAA_GET_CYCLES
193 #define HAS_CAA_GET_CYCLES
195 #if defined(__APPLE__)
197 #include <mach/mach.h>
198 #include <mach/clock.h>
199 #include <mach/mach_time.h>
203 typedef uint64_t caa_cycles_t
;
205 static inline caa_cycles_t
caa_get_cycles (void)
207 mach_timespec_t ts
= { 0, 0 };
208 static clock_serv_t clock_service
;
210 if (caa_unlikely(!clock_service
)) {
211 if (host_get_clock_service(mach_host_self(),
212 SYSTEM_CLOCK
, &clock_service
))
215 if (caa_unlikely(clock_get_time(clock_service
, &ts
)))
217 return ((uint64_t) ts
.tv_sec
* 1000000000ULL) + ts
.tv_nsec
;
220 #elif defined(CONFIG_RCU_HAVE_CLOCK_GETTIME)
225 typedef uint64_t caa_cycles_t
;
227 static inline caa_cycles_t
caa_get_cycles (void)
231 if (caa_unlikely(clock_gettime(CLOCK_MONOTONIC
, &ts
)))
233 return ((uint64_t) ts
.tv_sec
* 1000000000ULL) + ts
.tv_nsec
;
238 #error caa_get_cycles() not implemented for this platform.
242 #endif /* HAS_CAA_GET_CYCLES */
248 #endif /* _URCU_ARCH_GENERIC_H */