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 class Arm64InstructionSetFeatures;
25 using Arm64FeaturesUniquePtr = std::unique_ptr<const Arm64InstructionSetFeatures>;
26 
27 // Instruction set features relevant to the ARM64 architecture.
28 class Arm64InstructionSetFeatures final : public InstructionSetFeatures {
29  public:
30   // Process a CPU variant string like "krait" or "cortex-a15" and create InstructionSetFeatures.
31   static Arm64FeaturesUniquePtr FromVariant(const std::string& variant, std::string* error_msg);
32 
33   // Parse a bitmap and create an InstructionSetFeatures.
34   static Arm64FeaturesUniquePtr FromBitmap(uint32_t bitmap);
35 
36   // Turn C pre-processor #defines into the equivalent instruction set features.
37   static Arm64FeaturesUniquePtr FromCppDefines();
38 
39   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
40   static Arm64FeaturesUniquePtr FromCpuInfo();
41 
42   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
43   // InstructionSetFeatures.
44   static Arm64FeaturesUniquePtr FromHwcap();
45 
46   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
47   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
48   static Arm64FeaturesUniquePtr FromAssembly();
49 
50   bool Equals(const InstructionSetFeatures* other) const override;
51 
52   // Note that newer CPUs do not have a53 erratum 835769 and 843419,
53   // so the two a53 fix features (fix_cortex_a53_835769 and fix_cortex_a53_843419)
54   // are not tested for HasAtLeast.
55   bool HasAtLeast(const InstructionSetFeatures* other) const override;
56 
GetInstructionSet()57   InstructionSet GetInstructionSet() const override {
58     return InstructionSet::kArm64;
59   }
60 
61   uint32_t AsBitmap() const override;
62 
63   // Return a string of the form "a53" or "none".
64   std::string GetFeatureString() const override;
65 
66   // Generate code addressing Cortex-A53 erratum 835769?
NeedFixCortexA53_835769()67   bool NeedFixCortexA53_835769() const {
68       return fix_cortex_a53_835769_;
69   }
70 
71   // Generate code addressing Cortex-A53 erratum 843419?
NeedFixCortexA53_843419()72   bool NeedFixCortexA53_843419() const {
73       return fix_cortex_a53_843419_;
74   }
75 
HasCRC()76   bool HasCRC() const {
77     return has_crc_;
78   }
79 
HasLSE()80   bool HasLSE() const {
81     return has_lse_;
82   }
83 
HasFP16()84   bool HasFP16() const {
85     return has_fp16_;
86   }
87 
88   // Are Dot Product instructions (UDOT/SDOT) available?
HasDotProd()89   bool HasDotProd() const {
90     return has_dotprod_;
91   }
92 
HasSVE()93   bool HasSVE() const {
94     return has_sve_;
95   }
96 
~Arm64InstructionSetFeatures()97   virtual ~Arm64InstructionSetFeatures() {}
98 
99  protected:
100   // Parse a vector of the form "a53" adding these to a new ArmInstructionSetFeatures.
101   std::unique_ptr<const InstructionSetFeatures>
102       AddFeaturesFromSplitString(const std::vector<std::string>& features,
103                                  std::string* error_msg) const override;
104 
105   std::unique_ptr<const InstructionSetFeatures>
106       AddRuntimeDetectedFeatures(const InstructionSetFeatures *features) const override;
107 
108  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)109   Arm64InstructionSetFeatures(bool needs_a53_835769_fix,
110                               bool needs_a53_843419_fix,
111                               bool has_crc,
112                               bool has_lse,
113                               bool has_fp16,
114                               bool has_dotprod,
115                               bool has_sve)
116       : InstructionSetFeatures(),
117         fix_cortex_a53_835769_(needs_a53_835769_fix),
118         fix_cortex_a53_843419_(needs_a53_843419_fix),
119         has_crc_(has_crc),
120         has_lse_(has_lse),
121         has_fp16_(has_fp16),
122         has_dotprod_(has_dotprod),
123         has_sve_(has_sve) {
124   }
125 
126   // Bitmap positions for encoding features as a bitmap.
127   enum {
128     kA53Bitfield = 1 << 0,
129     kCRCBitField = 1 << 1,
130     kLSEBitField = 1 << 2,
131     kFP16BitField = 1 << 3,
132     kDotProdBitField = 1 << 4,
133     kSVEBitField = 1 << 5,
134   };
135 
136   const bool fix_cortex_a53_835769_;
137   const bool fix_cortex_a53_843419_;
138   const bool has_crc_;      // optional in ARMv8.0, mandatory in ARMv8.1.
139   const bool has_lse_;      // ARMv8.1 Large System Extensions.
140   const bool has_fp16_;     // ARMv8.2 FP16 extensions.
141   const bool has_dotprod_;  // optional in ARMv8.2, mandatory in ARMv8.4.
142   const bool has_sve_;      // optional in ARMv8.2.
143 
144   DISALLOW_COPY_AND_ASSIGN(Arm64InstructionSetFeatures);
145 };
146 
147 }  // namespace art
148 
149 #endif  // ART_RUNTIME_ARCH_ARM64_INSTRUCTION_SET_FEATURES_ARM64_H_
150