1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_ARCH_ARM64_INSTRUCTION_SET_FEATURES_ARM64_H_
18 #define ART_RUNTIME_ARCH_ARM64_INSTRUCTION_SET_FEATURES_ARM64_H_
19 
20 #include "arch/instruction_set_features.h"
21 
22 namespace art {
23 
24 // SVE is currently not enabled.
25 static constexpr bool kArm64AllowSVE = false;
26 
27 class Arm64InstructionSetFeatures;
28 using Arm64FeaturesUniquePtr = std::unique_ptr<const Arm64InstructionSetFeatures>;
29 
30 // Instruction set features relevant to the ARM64 architecture.
31 class Arm64InstructionSetFeatures final : public InstructionSetFeatures {
32  public:
33   // Process a CPU variant string like "krait" or "cortex-a15" and create InstructionSetFeatures.
34   static Arm64FeaturesUniquePtr FromVariant(const std::string& variant, std::string* error_msg);
35 
36   // Parse a bitmap and create an InstructionSetFeatures.
37   static Arm64FeaturesUniquePtr FromBitmap(uint32_t bitmap);
38 
39   // Turn C pre-processor #defines into the equivalent instruction set features.
40   static Arm64FeaturesUniquePtr FromCppDefines();
41 
42   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
43   static Arm64FeaturesUniquePtr FromCpuInfo();
44 
45   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
46   // InstructionSetFeatures.
47   static Arm64FeaturesUniquePtr FromHwcap();
48 
49   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
50   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
51   static Arm64FeaturesUniquePtr FromAssembly();
52 
53   // Use external cpu_features library.
54   static Arm64FeaturesUniquePtr FromCpuFeatures();
55 
56   bool Equals(const InstructionSetFeatures* other) const override;
57 
58   // Note that newer CPUs do not have a53 erratum 835769 and 843419,
59   // so the two a53 fix features (fix_cortex_a53_835769 and fix_cortex_a53_843419)
60   // are not tested for HasAtLeast.
61   bool HasAtLeast(const InstructionSetFeatures* other) const override;
62 
GetInstructionSet()63   InstructionSet GetInstructionSet() const override {
64     return InstructionSet::kArm64;
65   }
66 
67   uint32_t AsBitmap() const override;
68 
69   // Return a string of the form "a53" or "none".
70   std::string GetFeatureString() const override;
71 
72   // Generate code addressing Cortex-A53 erratum 835769?
NeedFixCortexA53_835769()73   bool NeedFixCortexA53_835769() const {
74       return fix_cortex_a53_835769_;
75   }
76 
77   // Generate code addressing Cortex-A53 erratum 843419?
NeedFixCortexA53_843419()78   bool NeedFixCortexA53_843419() const {
79       return fix_cortex_a53_843419_;
80   }
81 
HasCRC()82   bool HasCRC() const {
83     return has_crc_;
84   }
85 
HasLSE()86   bool HasLSE() const {
87     return has_lse_;
88   }
89 
HasFP16()90   bool HasFP16() const {
91     return has_fp16_;
92   }
93 
94   // Are Dot Product instructions (UDOT/SDOT) available?
HasDotProd()95   bool HasDotProd() const {
96     return has_dotprod_;
97   }
98 
HasSVE()99   bool HasSVE() const {
100     return kArm64AllowSVE && has_sve_;
101   }
102 
GetSVEVectorLength()103   size_t GetSVEVectorLength() const {
104     // TODO: support SVE vector length detection.
105     return kArm64DefaultSVEVectorLength;
106   }
107 
~Arm64InstructionSetFeatures()108   virtual ~Arm64InstructionSetFeatures() {}
109 
110  protected:
111   // Parse a vector of the form "a53" adding these to a new ArmInstructionSetFeatures.
112   std::unique_ptr<const InstructionSetFeatures>
113       AddFeaturesFromSplitString(const std::vector<std::string>& features,
114                                  std::string* error_msg) const override;
115 
116   std::unique_ptr<const InstructionSetFeatures>
117       AddRuntimeDetectedFeatures(const InstructionSetFeatures *features) const override;
118 
119  private:
Arm64InstructionSetFeatures(bool needs_a53_835769_fix,bool needs_a53_843419_fix,bool has_crc,bool has_lse,bool has_fp16,bool has_dotprod,bool has_sve)120   Arm64InstructionSetFeatures(bool needs_a53_835769_fix,
121                               bool needs_a53_843419_fix,
122                               bool has_crc,
123                               bool has_lse,
124                               bool has_fp16,
125                               bool has_dotprod,
126                               bool has_sve)
127       : InstructionSetFeatures(),
128         fix_cortex_a53_835769_(needs_a53_835769_fix),
129         fix_cortex_a53_843419_(needs_a53_843419_fix),
130         has_crc_(has_crc),
131         has_lse_(has_lse),
132         has_fp16_(has_fp16),
133         has_dotprod_(has_dotprod),
134         has_sve_(has_sve) {
135   }
136 
137   // Bitmap positions for encoding features as a bitmap.
138   enum {
139     kA53Bitfield = 1 << 0,
140     kCRCBitField = 1 << 1,
141     kLSEBitField = 1 << 2,
142     kFP16BitField = 1 << 3,
143     kDotProdBitField = 1 << 4,
144     kSVEBitField = 1 << 5,
145   };
146 
147   const bool fix_cortex_a53_835769_;
148   const bool fix_cortex_a53_843419_;
149   const bool has_crc_;      // optional in ARMv8.0, mandatory in ARMv8.1.
150   const bool has_lse_;      // ARMv8.1 Large System Extensions.
151   const bool has_fp16_;     // ARMv8.2 FP16 extensions.
152   const bool has_dotprod_;  // optional in ARMv8.2, mandatory in ARMv8.4.
153   const bool has_sve_;      // optional in ARMv8.2.
154 
155   DISALLOW_COPY_AND_ASSIGN(Arm64InstructionSetFeatures);
156 };
157 
158 }  // namespace art
159 
160 #endif  // ART_RUNTIME_ARCH_ARM64_INSTRUCTION_SET_FEATURES_ARM64_H_
161