| 1 |
/* Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Richard Henderson <[email protected]>. |
| 3 |
|
| 4 |
This file is part of the GNU Atomic Library (libatomic). |
| 5 |
|
| 6 |
Libatomic is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 |
more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
#include <config/arm/arm-config.h> |
| 26 |
|
| 27 |
|
| 28 |
/* Kernel helper for 32-bit compare-and-exchange. */ |
| 29 |
typedef int (__kernel_cmpxchg_t) (UWORD oldval, UWORD newval, UWORD *ptr); |
| 30 |
#define __kernel_cmpxchg (*(__kernel_cmpxchg_t *) 0xffff0fc0) |
| 31 |
|
| 32 |
/* Kernel helper for 64-bit compare-and-exchange. */ |
| 33 |
typedef int (__kernel_cmpxchg64_t) (const U_8 * oldval, const U_8 * newval, |
| 34 |
U_8 *ptr); |
| 35 |
#define __kernel_cmpxchg64 (*(__kernel_cmpxchg64_t *) 0xffff0f60) |
| 36 |
|
| 37 |
/* Kernel helper for memory barrier. */ |
| 38 |
typedef void (__kernel_dmb_t) (void); |
| 39 |
#define __kernel_dmb (*(__kernel_dmb_t *) 0xffff0fa0) |
| 40 |
|
| 41 |
/* Kernel helper page version number. */ |
| 42 |
static inline unsigned* |
| 43 |
__kernel_helper_version () |
| 44 |
{ |
| 45 |
unsigned *volatile addr = (unsigned int *)0xffff0ffc; |
| 46 |
return addr; |
| 47 |
} |
| 48 |
|
| 49 |
#define __kernel_helper_version (*__kernel_helper_version()) |
| 50 |
|
| 51 |
#ifndef HAVE_STREX |
| 52 |
static inline bool |
| 53 |
atomic_compare_exchange_w (UWORD *mptr, UWORD *eptr, UWORD newval, |
| 54 |
bool weak_p UNUSED, int sm UNUSED, int fm UNUSED) |
| 55 |
{ |
| 56 |
bool ret = true; |
| 57 |
UWORD oldval; |
| 58 |
|
| 59 |
oldval = *eptr; |
| 60 |
if (__builtin_expect (__kernel_cmpxchg (oldval, newval, mptr) != 0, 0)) |
| 61 |
{ |
| 62 |
oldval = *mptr; |
| 63 |
ret = false; |
| 64 |
} |
| 65 |
*eptr = oldval; |
| 66 |
|
| 67 |
return ret; |
| 68 |
} |
| 69 |
# define atomic_compare_exchange_w atomic_compare_exchange_w |
| 70 |
# if N == WORDSIZE |
| 71 |
# define atomic_compare_exchange_n atomic_compare_exchange_w |
| 72 |
# endif |
| 73 |
#endif /* HAVE_STREX */ |
| 74 |
|
| 75 |
#if !defined(HAVE_STREXBHD) && defined(HAVE_KERNEL64) && N == 8 |
| 76 |
static inline bool |
| 77 |
atomic_compare_exchange_n (UTYPE *mptr, UTYPE *eptr, UTYPE newval, |
| 78 |
bool weak_p UNUSED, int sm UNUSED, int fm UNUSED) |
| 79 |
{ |
| 80 |
if (__kernel_cmpxchg64 (eptr, &newval, mptr) == 0) |
| 81 |
return true; |
| 82 |
else |
| 83 |
{ |
| 84 |
*eptr = *mptr; |
| 85 |
return false; |
| 86 |
} |
| 87 |
} |
| 88 |
#define atomic_compare_exchange_n atomic_compare_exchange_n |
| 89 |
#endif |
| 90 |
|
| 91 |
#if !defined(HAVE_DMB) && !defined(HAVE_DMB_MCR) |
| 92 |
static inline void |
| 93 |
pre_barrier(int model UNUSED) |
| 94 |
{ |
| 95 |
__kernel_dmb (); |
| 96 |
} |
| 97 |
|
| 98 |
static inline void |
| 99 |
post_barrier(int model UNUSED) |
| 100 |
{ |
| 101 |
__kernel_dmb (); |
| 102 |
} |
| 103 |
# define pre_post_barrier 1 |
| 104 |
#endif /* !HAVE_DMB */ |
| 105 |
|
| 106 |
#if HAVE_IFUNC |
| 107 |
extern bool libat_have_strexbhd HIDDEN; |
| 108 |
|
| 109 |
# define IFUNC_COND_1 libat_have_strexbhd |
| 110 |
# define IFUNC_COND_2 (__kernel_helper_version >= 5) |
| 111 |
|
| 112 |
/* Alternative 1 is -march=armv7-a -- we have everything native. */ |
| 113 |
# if IFUNC_ALT == 1 |
| 114 |
# undef HAVE_ATOMIC_CAS_1 |
| 115 |
# undef HAVE_ATOMIC_CAS_2 |
| 116 |
# undef HAVE_ATOMIC_CAS_4 |
| 117 |
# undef HAVE_ATOMIC_CAS_8 |
| 118 |
# undef HAVE_ATOMIC_EXCHANGE_1 |
| 119 |
# undef HAVE_ATOMIC_EXCHANGE_2 |
| 120 |
# undef HAVE_ATOMIC_EXCHANGE_4 |
| 121 |
# undef HAVE_ATOMIC_EXCHANGE_8 |
| 122 |
# undef HAVE_ATOMIC_LDST_1 |
| 123 |
# undef HAVE_ATOMIC_LDST_2 |
| 124 |
# undef HAVE_ATOMIC_LDST_4 |
| 125 |
# undef HAVE_ATOMIC_LDST_8 |
| 126 |
# undef HAVE_ATOMIC_FETCH_OP_1 |
| 127 |
# undef HAVE_ATOMIC_FETCH_OP_2 |
| 128 |
# undef HAVE_ATOMIC_FETCH_OP_4 |
| 129 |
# undef HAVE_ATOMIC_FETCH_OP_8 |
| 130 |
# undef HAVE_ATOMIC_TAS_1 |
| 131 |
# undef HAVE_ATOMIC_TAS_2 |
| 132 |
# undef HAVE_ATOMIC_TAS_4 |
| 133 |
# undef HAVE_ATOMIC_TAS_8 |
| 134 |
# define HAVE_ATOMIC_CAS_1 1 |
| 135 |
# define HAVE_ATOMIC_CAS_2 1 |
| 136 |
# define HAVE_ATOMIC_CAS_4 1 |
| 137 |
# define HAVE_ATOMIC_CAS_8 1 |
| 138 |
# define HAVE_ATOMIC_EXCHANGE_1 1 |
| 139 |
# define HAVE_ATOMIC_EXCHANGE_2 1 |
| 140 |
# define HAVE_ATOMIC_EXCHANGE_4 1 |
| 141 |
# define HAVE_ATOMIC_EXCHANGE_8 1 |
| 142 |
# define HAVE_ATOMIC_LDST_1 1 |
| 143 |
# define HAVE_ATOMIC_LDST_2 1 |
| 144 |
# define HAVE_ATOMIC_LDST_4 1 |
| 145 |
# define HAVE_ATOMIC_LDST_8 1 |
| 146 |
# define HAVE_ATOMIC_FETCH_OP_1 1 |
| 147 |
# define HAVE_ATOMIC_FETCH_OP_2 1 |
| 148 |
# define HAVE_ATOMIC_FETCH_OP_4 1 |
| 149 |
# define HAVE_ATOMIC_FETCH_OP_8 1 |
| 150 |
# define HAVE_ATOMIC_TAS_1 1 |
| 151 |
# define HAVE_ATOMIC_TAS_2 1 |
| 152 |
# define HAVE_ATOMIC_TAS_4 1 |
| 153 |
# define HAVE_ATOMIC_TAS_8 1 |
| 154 |
# endif /* IFUNC_ALT == 1 */ |
| 155 |
|
| 156 |
# undef MAYBE_HAVE_ATOMIC_CAS_1 |
| 157 |
# define MAYBE_HAVE_ATOMIC_CAS_1 IFUNC_COND_1 |
| 158 |
# undef MAYBE_HAVE_ATOMIC_EXCHANGE_1 |
| 159 |
# define MAYBE_HAVE_ATOMIC_EXCHANGE_1 MAYBE_HAVE_ATOMIC_CAS_1 |
| 160 |
# undef MAYBE_HAVE_ATOMIC_LDST_1 |
| 161 |
# define MAYBE_HAVE_ATOMIC_LDST_1 MAYBE_HAVE_ATOMIC_CAS_1 |
| 162 |
# undef MAYBE_HAVE_ATOMIC_CAS_2 |
| 163 |
# define MAYBE_HAVE_ATOMIC_CAS_2 IFUNC_COND_1 |
| 164 |
# undef MAYBE_HAVE_ATOMIC_EXCHANGE_2 |
| 165 |
# define MAYBE_HAVE_ATOMIC_EXCHANGE_2 MAYBE_HAVE_ATOMIC_CAS_2 |
| 166 |
# undef MAYBE_HAVE_ATOMIC_LDST_2 |
| 167 |
# define MAYBE_HAVE_ATOMIC_LDST_2 MAYBE_HAVE_ATOMIC_CAS_2 |
| 168 |
# undef MAYBE_HAVE_ATOMIC_CAS_4 |
| 169 |
# define MAYBE_HAVE_ATOMIC_CAS_4 IFUNC_COND_1 |
| 170 |
# undef MAYBE_HAVE_ATOMIC_EXCHANGE_4 |
| 171 |
# define MAYBE_HAVE_ATOMIC_EXCHANGE_4 MAYBE_HAVE_ATOMIC_CAS_4 |
| 172 |
# undef MAYBE_HAVE_ATOMIC_LDST_4 |
| 173 |
# define MAYBE_HAVE_ATOMIC_LDST_4 MAYBE_HAVE_ATOMIC_CAS_4 |
| 174 |
# undef MAYBE_HAVE_ATOMIC_CAS_8 |
| 175 |
# define MAYBE_HAVE_ATOMIC_CAS_8 (IFUNC_COND_1 | IFUNC_COND_2) |
| 176 |
# undef MAYBE_HAVE_ATOMIC_EXCHANGE_8 |
| 177 |
# define MAYBE_HAVE_ATOMIC_EXCHANGE_8 MAYBE_HAVE_ATOMIC_CAS_8 |
| 178 |
# undef MAYBE_HAVE_ATOMIC_LDST_8 |
| 179 |
# define MAYBE_HAVE_ATOMIC_LDST_8 MAYBE_HAVE_ATOMIC_CAS_8 |
| 180 |
|
| 181 |
# define IFUNC_NCOND(N) (N == 8 ? 2 : 1) |
| 182 |
|
| 183 |
#endif /* HAVE_IFUNC */ |
| 184 |
|
| 185 |
#include_next <host-config.h> |