xref: /aosp_15_r20/external/cpu_features/src/impl_aarch64_windows.c (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cpu_features_macros.h"
16 
17 #ifdef CPU_FEATURES_ARCH_AARCH64
18 #ifdef CPU_FEATURES_OS_WINDOWS
19 
20 #include "cpuinfo_aarch64.h"
21 
22 ////////////////////////////////////////////////////////////////////////////////
23 // Definitions for introspection.
24 ////////////////////////////////////////////////////////////////////////////////
25 #define INTROSPECTION_TABLE                  \
26   LINE(AARCH64_FP, fp, , , )                 \
27   LINE(AARCH64_ASIMD, asimd, , , )           \
28   LINE(AARCH64_EVTSTRM, evtstrm, , , )       \
29   LINE(AARCH64_AES, aes, , , )               \
30   LINE(AARCH64_PMULL, pmull, , , )           \
31   LINE(AARCH64_SHA1, sha1, , , )             \
32   LINE(AARCH64_SHA2, sha2, , , )             \
33   LINE(AARCH64_CRC32, crc32, , , )           \
34   LINE(AARCH64_ATOMICS, atomics, , , )       \
35   LINE(AARCH64_FPHP, fphp, , , )             \
36   LINE(AARCH64_ASIMDHP, asimdhp, , , )       \
37   LINE(AARCH64_CPUID, cpuid, , , )           \
38   LINE(AARCH64_ASIMDRDM, asimdrdm, , , )     \
39   LINE(AARCH64_JSCVT, jscvt, , , )           \
40   LINE(AARCH64_FCMA, fcma, , , )             \
41   LINE(AARCH64_LRCPC, lrcpc, , , )           \
42   LINE(AARCH64_DCPOP, dcpop, , , )           \
43   LINE(AARCH64_SHA3, sha3, , , )             \
44   LINE(AARCH64_SM3, sm3, , , )               \
45   LINE(AARCH64_SM4, sm4, , , )               \
46   LINE(AARCH64_ASIMDDP, asimddp, , , )       \
47   LINE(AARCH64_SHA512, sha512, , , )         \
48   LINE(AARCH64_SVE, sve, , , )               \
49   LINE(AARCH64_ASIMDFHM, asimdfhm, , , )     \
50   LINE(AARCH64_DIT, dit, , , )               \
51   LINE(AARCH64_USCAT, uscat, , , )           \
52   LINE(AARCH64_ILRCPC, ilrcpc, , , )         \
53   LINE(AARCH64_FLAGM, flagm, , , )           \
54   LINE(AARCH64_SSBS, ssbs, , , )             \
55   LINE(AARCH64_SB, sb, , , )                 \
56   LINE(AARCH64_PACA, paca, , , )             \
57   LINE(AARCH64_PACG, pacg, , , )             \
58   LINE(AARCH64_DCPODP, dcpodp, , , )         \
59   LINE(AARCH64_SVE2, sve2, , , )             \
60   LINE(AARCH64_SVEAES, sveaes, , , )         \
61   LINE(AARCH64_SVEPMULL, svepmull, , , )     \
62   LINE(AARCH64_SVEBITPERM, svebitperm, , , ) \
63   LINE(AARCH64_SVESHA3, svesha3, , , )       \
64   LINE(AARCH64_SVESM4, svesm4, , , )         \
65   LINE(AARCH64_FLAGM2, flagm2, , , )         \
66   LINE(AARCH64_FRINT, frint, , , )           \
67   LINE(AARCH64_SVEI8MM, svei8mm, , , )       \
68   LINE(AARCH64_SVEF32MM, svef32mm, , , )     \
69   LINE(AARCH64_SVEF64MM, svef64mm, , , )     \
70   LINE(AARCH64_SVEBF16, svebf16, , , )       \
71   LINE(AARCH64_I8MM, i8mm, , , )             \
72   LINE(AARCH64_BF16, bf16, , , )             \
73   LINE(AARCH64_DGH, dgh, , , )               \
74   LINE(AARCH64_RNG, rng, , , )               \
75   LINE(AARCH64_BTI, bti, , , )               \
76   LINE(AARCH64_MTE, mte, , , )               \
77   LINE(AARCH64_ECV, ecv, , , )               \
78   LINE(AARCH64_AFP, afp, , , )               \
79   LINE(AARCH64_RPRES, rpres, , , )
80 #define INTROSPECTION_PREFIX Aarch64
81 #define INTROSPECTION_ENUM_PREFIX AARCH64
82 #include "define_introspection.inl"
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 // Implementation.
86 ////////////////////////////////////////////////////////////////////////////////
87 
88 #include <stdbool.h>
89 
90 #include "internal/windows_utils.h"
91 
92 #ifdef CPU_FEATURES_MOCK_CPUID_AARCH64
93 extern bool GetWindowsIsProcessorFeaturePresent(DWORD);
94 extern WORD GetWindowsNativeSystemInfoProcessorRevision();
95 #else  // CPU_FEATURES_MOCK_CPUID_AARCH64
GetWindowsIsProcessorFeaturePresent(DWORD dwProcessorFeature)96 static bool GetWindowsIsProcessorFeaturePresent(DWORD dwProcessorFeature) {
97   return IsProcessorFeaturePresent(dwProcessorFeature);
98 }
99 
GetWindowsNativeSystemInfoProcessorRevision()100 static WORD GetWindowsNativeSystemInfoProcessorRevision() {
101   SYSTEM_INFO system_info;
102   GetNativeSystemInfo(&system_info);
103   return system_info.wProcessorRevision;
104 }
105 #endif
106 
107 static const Aarch64Info kEmptyAarch64Info;
108 
GetAarch64Info(void)109 Aarch64Info GetAarch64Info(void) {
110   Aarch64Info info = kEmptyAarch64Info;
111   info.revision = GetWindowsNativeSystemInfoProcessorRevision();
112   info.features.fp =
113       GetWindowsIsProcessorFeaturePresent(PF_ARM_VFP_32_REGISTERS_AVAILABLE);
114   info.features.asimd =
115       GetWindowsIsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE);
116   info.features.crc32 = GetWindowsIsProcessorFeaturePresent(
117       PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE);
118   info.features.asimddp =
119       GetWindowsIsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE);
120   info.features.jscvt = GetWindowsIsProcessorFeaturePresent(
121       PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE);
122   info.features.lrcpc = GetWindowsIsProcessorFeaturePresent(
123       PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE);
124   info.features.atomics = GetWindowsIsProcessorFeaturePresent(
125       PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE);
126 
127 
128   bool is_crypto_available = GetWindowsIsProcessorFeaturePresent(
129       PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
130   info.features.aes = is_crypto_available;
131   info.features.sha1 = is_crypto_available;
132   info.features.sha2 = is_crypto_available;
133   info.features.pmull = is_crypto_available;
134   return info;
135 }
136 
137 #endif  // CPU_FEATURES_OS_WINDOWS
138 #endif  // CPU_FEATURES_ARCH_AARCH64
139