| 1 |
/* Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
This file is part of the GNU Atomic Library (libatomic). |
| 4 |
|
| 5 |
Libatomic is free software; you can redistribute it and/or modify it |
| 6 |
under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 3 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 |
more details. |
| 14 |
|
| 15 |
Under Section 7 of GPL version 3, you are granted additional |
| 16 |
permissions described in the GCC Runtime Library Exception, version |
| 17 |
3.1, as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License and |
| 20 |
a copy of the GCC Runtime Library Exception along with this program; |
| 21 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
#if HAVE_IFUNC |
| 25 |
#include <sys/auxv.h> |
| 26 |
|
| 27 |
#ifndef HWCAP_ATOMICS |
| 28 |
# define HWCAP_ATOMICS (1 << 8) |
| 29 |
#endif |
| 30 |
#ifndef HWCAP_CPUID |
| 31 |
# define HWCAP_CPUID (1 << 11) |
| 32 |
#endif |
| 33 |
#ifndef HWCAP_USCAT |
| 34 |
# define HWCAP_USCAT (1 << 25) |
| 35 |
#endif |
| 36 |
#ifndef HWCAP2_LRCPC3 |
| 37 |
# define HWCAP2_LRCPC3 (1UL << 46) |
| 38 |
#endif |
| 39 |
#ifndef HWCAP2_LSE128 |
| 40 |
# define HWCAP2_LSE128 (1UL << 47) |
| 41 |
#endif |
| 42 |
|
| 43 |
#if __has_include(<sys/ifunc.h>) |
| 44 |
# include <sys/ifunc.h> |
| 45 |
#else |
| 46 |
typedef struct __ifunc_arg_t { |
| 47 |
unsigned long _size; |
| 48 |
unsigned long _hwcap; |
| 49 |
unsigned long _hwcap2; |
| 50 |
} __ifunc_arg_t; |
| 51 |
# define _IFUNC_ARG_HWCAP (1ULL << 62) |
| 52 |
#endif |
| 53 |
|
| 54 |
/* From the file which imported `host-config.h' we can ascertain which |
| 55 |
architectural extension provides relevant atomic support. From this, |
| 56 |
we can proceed to tweak the ifunc selector behavior. */ |
| 57 |
#if defined (LAT_CAS_N) |
| 58 |
# define LSE_ATOP |
| 59 |
#elif defined (LAT_LOAD_N) || defined (LAT_STORE_N) |
| 60 |
# define LSE2_LRCPC3_ATOP |
| 61 |
#elif defined (LAT_EXCH_N) || defined (LAT_FIOR_N) || defined (LAT_FAND_N) |
| 62 |
# define LSE128_ATOP |
| 63 |
#endif |
| 64 |
|
| 65 |
# if N == 16 |
| 66 |
# if defined (LSE_ATOP) |
| 67 |
# define IFUNC_NCOND(N) 1 |
| 68 |
# define IFUNC_COND_1 (hwcap & HWCAP_ATOMICS) |
| 69 |
# elif defined (LSE2_LRCPC3_ATOP) |
| 70 |
# define IFUNC_NCOND(N) 2 |
| 71 |
# define IFUNC_COND_1 (has_rcpc3 (hwcap, features)) |
| 72 |
# define IFUNC_COND_2 (has_lse2 (hwcap, features)) |
| 73 |
# elif defined (LSE128_ATOP) |
| 74 |
# define IFUNC_NCOND(N) 1 |
| 75 |
# define IFUNC_COND_1 (has_lse128 (hwcap, features)) |
| 76 |
# else |
| 77 |
# define IFUNC_NCOND(N) 0 |
| 78 |
# define IFUNC_ALT 1 |
| 79 |
# endif |
| 80 |
# else |
| 81 |
# define IFUNC_COND_1 (hwcap & HWCAP_ATOMICS) |
| 82 |
# define IFUNC_NCOND(N) 1 |
| 83 |
# endif |
| 84 |
|
| 85 |
#define MIDR_IMPLEMENTOR(midr) (((midr) >> 24) & 255) |
| 86 |
#define MIDR_PARTNUM(midr) (((midr) >> 4) & 0xfff) |
| 87 |
|
| 88 |
static inline bool |
| 89 |
has_lse2 (unsigned long hwcap, const __ifunc_arg_t *features) |
| 90 |
{ |
| 91 |
/* Check for LSE2. */ |
| 92 |
if (hwcap & HWCAP_USCAT) |
| 93 |
return true; |
| 94 |
|
| 95 |
/* If LSE and CPUID are supported, check MIDR. */ |
| 96 |
if (hwcap & HWCAP_CPUID && hwcap & HWCAP_ATOMICS) |
| 97 |
{ |
| 98 |
unsigned long midr; |
| 99 |
asm volatile ("mrs %0, midr_el1" : "=r" (midr)); |
| 100 |
|
| 101 |
/* Neoverse N1 supports atomic 128-bit load/store. */ |
| 102 |
return MIDR_IMPLEMENTOR (midr) == 'A' && MIDR_PARTNUM (midr) == 0xd0c; |
| 103 |
} |
| 104 |
|
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
/* LSE128 atomic support encoded in ID_AA64ISAR0_EL1.Atomic, bits[23:20]. |
| 109 |
The minimum value for LSE128 is 0b0011. */ |
| 110 |
|
| 111 |
#define AT_FEAT_FIELD(isar0) (((isar0) >> 20) & 15) |
| 112 |
|
| 113 |
static inline bool |
| 114 |
has_lse128 (unsigned long hwcap, const __ifunc_arg_t *features) |
| 115 |
{ |
| 116 |
if (hwcap & _IFUNC_ARG_HWCAP && features->_hwcap2 & HWCAP2_LSE128) |
| 117 |
return true; |
| 118 |
|
| 119 |
/* If LSE2 and CPUID are supported, check for LSE128. */ |
| 120 |
if (hwcap & HWCAP_CPUID && hwcap & HWCAP_USCAT) |
| 121 |
{ |
| 122 |
unsigned long isar0; |
| 123 |
asm volatile ("mrs %0, ID_AA64ISAR0_EL1" : "=r" (isar0)); |
| 124 |
return AT_FEAT_FIELD (isar0) >= 3; |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
/* LRCPC atomic support encoded in ID_AA64ISAR1_EL1.Atomic, bits[23:20]. |
| 131 |
The minimum value for LRCPC3 is 0b0011. */ |
| 132 |
|
| 133 |
static inline bool |
| 134 |
has_rcpc3 (unsigned long hwcap, const __ifunc_arg_t *features) |
| 135 |
{ |
| 136 |
/* LSE2 is a prerequisite for atomic LDIAPP/STILP - check HWCAP_USCAT since |
| 137 |
has_lse2 is more expensive and Neoverse N1 does not have LRCPC3. */ |
| 138 |
if (!(hwcap & HWCAP_USCAT)) |
| 139 |
return false; |
| 140 |
|
| 141 |
if (hwcap & _IFUNC_ARG_HWCAP && features->_hwcap2 & HWCAP2_LRCPC3) |
| 142 |
return true; |
| 143 |
|
| 144 |
if (hwcap & HWCAP_CPUID) |
| 145 |
{ |
| 146 |
unsigned long isar1; |
| 147 |
asm volatile ("mrs %0, ID_AA64ISAR1_EL1" : "=r" (isar1)); |
| 148 |
return AT_FEAT_FIELD (isar1) >= 3; |
| 149 |
} |
| 150 |
|
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
#endif /* HAVE_IFUNC */ |
| 155 |
|
| 156 |
/* All 128-bit atomic functions are defined in aarch64/atomic_16.S. */ |
| 157 |
#if N == 16 |
| 158 |
# define DONE 1 |
| 159 |
# if !HAVE_IFUNC |
| 160 |
# define IFUNC_ALT 1 |
| 161 |
# endif |
| 162 |
#endif |
| 163 |
|
| 164 |
#include_next <host-config.h> |