| 1 | // SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 | |
| 5 | #ifndef _URCU_ARCH_GENERIC_H |
| 6 | #define _URCU_ARCH_GENERIC_H |
| 7 | |
| 8 | /* |
| 9 | * arch_generic.h: common definitions for multiple architectures. |
| 10 | */ |
| 11 | |
| 12 | #include <urcu/compiler.h> |
| 13 | #include <urcu/config.h> |
| 14 | #include <urcu/syscall-compat.h> |
| 15 | |
| 16 | #ifdef __cplusplus |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | #ifndef CAA_CACHE_LINE_SIZE |
| 21 | #define CAA_CACHE_LINE_SIZE 64 |
| 22 | #endif |
| 23 | |
| 24 | #if !defined(cmm_mc) && !defined(cmm_rmc) && !defined(cmm_wmc) |
| 25 | #define CONFIG_HAVE_MEM_COHERENCY |
| 26 | /* |
| 27 | * Architectures with cache coherency must _not_ define cmm_mc/cmm_rmc/cmm_wmc. |
| 28 | * |
| 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). |
| 32 | */ |
| 33 | |
| 34 | #ifndef cmm_mb |
| 35 | #define cmm_mb() __sync_synchronize() |
| 36 | #endif |
| 37 | |
| 38 | #ifndef cmm_rmb |
| 39 | #define cmm_rmb() cmm_mb() |
| 40 | #endif |
| 41 | |
| 42 | #ifndef cmm_wmb |
| 43 | #define cmm_wmb() cmm_mb() |
| 44 | #endif |
| 45 | |
| 46 | #define cmm_mc() cmm_barrier() |
| 47 | #define cmm_rmc() cmm_barrier() |
| 48 | #define cmm_wmc() cmm_barrier() |
| 49 | #else |
| 50 | /* |
| 51 | * Architectures without cache coherency need something like the following: |
| 52 | * |
| 53 | * #define cmm_mc() arch_cache_flush() |
| 54 | * #define cmm_rmc() arch_cache_flush_read() |
| 55 | * #define cmm_wmc() arch_cache_flush_write() |
| 56 | * |
| 57 | * Of these, only cmm_mc is mandatory. cmm_rmc and cmm_wmc default to |
| 58 | * cmm_mc. cmm_mb/cmm_rmb/cmm_wmb use these definitions by default: |
| 59 | * |
| 60 | * #define cmm_mb() cmm_mc() |
| 61 | * #define cmm_rmb() cmm_rmc() |
| 62 | * #define cmm_wmb() cmm_wmc() |
| 63 | */ |
| 64 | |
| 65 | #ifndef cmm_mb |
| 66 | #define cmm_mb() cmm_mc() |
| 67 | #endif |
| 68 | |
| 69 | #ifndef cmm_rmb |
| 70 | #define cmm_rmb() cmm_rmc() |
| 71 | #endif |
| 72 | |
| 73 | #ifndef cmm_wmb |
| 74 | #define cmm_wmb() cmm_wmc() |
| 75 | #endif |
| 76 | |
| 77 | #ifndef cmm_rmc |
| 78 | #define cmm_rmc() cmm_mc() |
| 79 | #endif |
| 80 | |
| 81 | #ifndef cmm_wmc |
| 82 | #define cmm_wmc() cmm_mc() |
| 83 | #endif |
| 84 | #endif |
| 85 | |
| 86 | /* Nop everywhere except on alpha. */ |
| 87 | #ifndef cmm_read_barrier_depends |
| 88 | #define cmm_read_barrier_depends() |
| 89 | #endif |
| 90 | |
| 91 | #ifdef CONFIG_RCU_SMP |
| 92 | #ifndef cmm_smp_mb |
| 93 | #define cmm_smp_mb() cmm_mb() |
| 94 | #endif |
| 95 | #ifndef cmm_smp_rmb |
| 96 | #define cmm_smp_rmb() cmm_rmb() |
| 97 | #endif |
| 98 | #ifndef cmm_smp_wmb |
| 99 | #define cmm_smp_wmb() cmm_wmb() |
| 100 | #endif |
| 101 | #ifndef cmm_smp_mc |
| 102 | #define cmm_smp_mc() cmm_mc() |
| 103 | #endif |
| 104 | #ifndef cmm_smp_rmc |
| 105 | #define cmm_smp_rmc() cmm_rmc() |
| 106 | #endif |
| 107 | #ifndef cmm_smp_wmc |
| 108 | #define cmm_smp_wmc() cmm_wmc() |
| 109 | #endif |
| 110 | #ifndef cmm_smp_read_barrier_depends |
| 111 | #define cmm_smp_read_barrier_depends() cmm_read_barrier_depends() |
| 112 | #endif |
| 113 | #else |
| 114 | #ifndef cmm_smp_mb |
| 115 | #define cmm_smp_mb() cmm_barrier() |
| 116 | #endif |
| 117 | #ifndef cmm_smp_rmb |
| 118 | #define cmm_smp_rmb() cmm_barrier() |
| 119 | #endif |
| 120 | #ifndef cmm_smp_wmb |
| 121 | #define cmm_smp_wmb() cmm_barrier() |
| 122 | #endif |
| 123 | #ifndef cmm_smp_mc |
| 124 | #define cmm_smp_mc() cmm_barrier() |
| 125 | #endif |
| 126 | #ifndef cmm_smp_rmc |
| 127 | #define cmm_smp_rmc() cmm_barrier() |
| 128 | #endif |
| 129 | #ifndef cmm_smp_wmc |
| 130 | #define cmm_smp_wmc() cmm_barrier() |
| 131 | #endif |
| 132 | #ifndef cmm_smp_read_barrier_depends |
| 133 | #define cmm_smp_read_barrier_depends() |
| 134 | #endif |
| 135 | #endif |
| 136 | |
| 137 | #ifndef caa_cpu_relax |
| 138 | #define caa_cpu_relax() cmm_barrier() |
| 139 | #endif |
| 140 | |
| 141 | #ifndef HAS_CAA_GET_CYCLES |
| 142 | #define HAS_CAA_GET_CYCLES |
| 143 | |
| 144 | #if defined(__APPLE__) |
| 145 | |
| 146 | #include <mach/mach.h> |
| 147 | #include <mach/clock.h> |
| 148 | #include <mach/mach_time.h> |
| 149 | #include <time.h> |
| 150 | #include <stdint.h> |
| 151 | |
| 152 | typedef uint64_t caa_cycles_t; |
| 153 | |
| 154 | static inline caa_cycles_t caa_get_cycles (void) |
| 155 | { |
| 156 | mach_timespec_t ts = { 0, 0 }; |
| 157 | static clock_serv_t clock_service; |
| 158 | |
| 159 | if (caa_unlikely(!clock_service)) { |
| 160 | if (host_get_clock_service(mach_host_self(), |
| 161 | SYSTEM_CLOCK, &clock_service)) |
| 162 | return -1ULL; |
| 163 | } |
| 164 | if (caa_unlikely(clock_get_time(clock_service, &ts))) |
| 165 | return -1ULL; |
| 166 | return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec; |
| 167 | } |
| 168 | |
| 169 | #elif defined(CONFIG_RCU_HAVE_CLOCK_GETTIME) |
| 170 | |
| 171 | #include <time.h> |
| 172 | #include <stdint.h> |
| 173 | |
| 174 | typedef uint64_t caa_cycles_t; |
| 175 | |
| 176 | static inline caa_cycles_t caa_get_cycles (void) |
| 177 | { |
| 178 | struct timespec ts; |
| 179 | |
| 180 | if (caa_unlikely(clock_gettime(CLOCK_MONOTONIC, &ts))) |
| 181 | return -1ULL; |
| 182 | return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec; |
| 183 | } |
| 184 | |
| 185 | #else |
| 186 | |
| 187 | #error caa_get_cycles() not implemented for this platform. |
| 188 | |
| 189 | #endif |
| 190 | |
| 191 | #endif /* HAS_CAA_GET_CYCLES */ |
| 192 | |
| 193 | #ifdef __cplusplus |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | #endif /* _URCU_ARCH_GENERIC_H */ |