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#include "instruction_set_features_mips64.h"
18
19#include <fstream>
20#include <sstream>
21
22#include "android-base/stringprintf.h"
23#include "android-base/strings.h"
24
25#include "base/logging.h"
26
27namespace art {
28
29using android::base::StringPrintf;
30
31Mips64FeaturesUniquePtr Mips64InstructionSetFeatures::FromVariant(
32    const std::string& variant, std::string* error_msg ATTRIBUTE_UNUSED) {
33  bool msa = true;
34  if (variant != "default" && variant != "mips64r6") {
35    LOG(WARNING) << "Unexpected CPU variant for Mips64 using defaults: " << variant;
36  }
37  return Mips64FeaturesUniquePtr(new Mips64InstructionSetFeatures(msa));
38}
39
40Mips64FeaturesUniquePtr Mips64InstructionSetFeatures::FromBitmap(uint32_t bitmap) {
41  bool msa = (bitmap & kMsaBitfield) != 0;
42  return Mips64FeaturesUniquePtr(new Mips64InstructionSetFeatures(msa));
43}
44
45Mips64FeaturesUniquePtr Mips64InstructionSetFeatures::FromCppDefines() {
46#if defined(_MIPS_ARCH_MIPS64R6)
47  const bool msa = true;
48#else
49  const bool msa = false;
50#endif
51  return Mips64FeaturesUniquePtr(new Mips64InstructionSetFeatures(msa));
52}
53
54Mips64FeaturesUniquePtr Mips64InstructionSetFeatures::FromCpuInfo() {
55  // Look in /proc/cpuinfo for features we need.  Only use this when we can guarantee that
56  // the kernel puts the appropriate feature flags in here.  Sometimes it doesn't.
57  bool msa = false;
58
59  std::ifstream in("/proc/cpuinfo");
60  if (!in.fail()) {
61    while (!in.eof()) {
62      std::string line;
63      std::getline(in, line);
64      if (!in.eof()) {
65        LOG(INFO) << "cpuinfo line: " << line;
66        if (line.find("ASEs") != std::string::npos) {
67          LOG(INFO) << "found Application Specific Extensions";
68          if (line.find("msa") != std::string::npos) {
69            msa = true;
70          }
71        }
72      }
73    }
74    in.close();
75  } else {
76    LOG(ERROR) << "Failed to open /proc/cpuinfo";
77  }
78  return Mips64FeaturesUniquePtr(new Mips64InstructionSetFeatures(msa));
79}
80
81Mips64FeaturesUniquePtr Mips64InstructionSetFeatures::FromHwcap() {
82  UNIMPLEMENTED(WARNING);
83  return FromCppDefines();
84}
85
86Mips64FeaturesUniquePtr Mips64InstructionSetFeatures::FromAssembly() {
87  UNIMPLEMENTED(WARNING);
88  return FromCppDefines();
89}
90
91bool Mips64InstructionSetFeatures::Equals(const InstructionSetFeatures* other) const {
92  if (kMips64 != other->GetInstructionSet()) {
93    return false;
94  }
95  const Mips64InstructionSetFeatures* other_as_mips64 = other->AsMips64InstructionSetFeatures();
96  return msa_ == other_as_mips64->msa_;
97}
98
99uint32_t Mips64InstructionSetFeatures::AsBitmap() const {
100  return (msa_ ? kMsaBitfield : 0);
101}
102
103std::string Mips64InstructionSetFeatures::GetFeatureString() const {
104  std::string result;
105  if (msa_) {
106    result += "msa";
107  } else {
108    result += "-msa";
109  }
110  return result;
111}
112
113std::unique_ptr<const InstructionSetFeatures>
114Mips64InstructionSetFeatures::AddFeaturesFromSplitString(
115    const std::vector<std::string>& features, std::string* error_msg) const {
116  bool msa = msa_;
117  for (auto i = features.begin(); i != features.end(); i++) {
118    std::string feature = android::base::Trim(*i);
119    if (feature == "msa") {
120      msa = true;
121    } else if (feature == "-msa") {
122      msa = false;
123    } else {
124      *error_msg = StringPrintf("Unknown instruction set feature: '%s'", feature.c_str());
125      return nullptr;
126    }
127  }
128  return std::unique_ptr<const InstructionSetFeatures>(new Mips64InstructionSetFeatures(msa));
129}
130
131}  // namespace art
132