1 // Copyright 2018 IBM.
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 "cpuinfo_ppc.h"
16
17 #include "filesystem_for_testing.h"
18 #include "gtest/gtest.h"
19 #include "hwcaps_for_testing.h"
20 #include "internal/string_view.h"
21
22 namespace cpu_features {
23 namespace {
24
TEST(CpustringsPPCTest,PPCFeaturesEnum)25 TEST(CpustringsPPCTest, PPCFeaturesEnum) {
26 const char *last_name = GetPPCFeaturesEnumName(PPC_LAST_);
27 EXPECT_STREQ(last_name, "unknown_feature");
28 for (int i = static_cast<int>(PPC_32); i != static_cast<int>(PPC_LAST_); ++i) {
29 const auto feature = static_cast<PPCFeaturesEnum>(i);
30 const char *name = GetPPCFeaturesEnumName(feature);
31 ASSERT_FALSE(name == nullptr);
32 EXPECT_STRNE(name, "");
33 EXPECT_STRNE(name, last_name);
34 }
35 }
36
TEST(CpustringsPPCTest,FromHardwareCap)37 TEST(CpustringsPPCTest, FromHardwareCap) {
38 ResetHwcaps();
39 SetHardwareCapabilities(PPC_FEATURE_HAS_FPU | PPC_FEATURE_HAS_VSX,
40 PPC_FEATURE2_ARCH_3_00);
41 GetEmptyFilesystem(); // disabling /proc/cpuinfo
42 const auto info = GetPPCInfo();
43 EXPECT_TRUE(info.features.fpu);
44 EXPECT_FALSE(info.features.mmu);
45 EXPECT_TRUE(info.features.vsx);
46 EXPECT_TRUE(info.features.arch300);
47 EXPECT_FALSE(info.features.power4);
48 EXPECT_FALSE(info.features.altivec);
49 EXPECT_FALSE(info.features.vcrypto);
50 EXPECT_FALSE(info.features.htm);
51 }
52
TEST(CpustringsPPCTest,Blade)53 TEST(CpustringsPPCTest, Blade) {
54 ResetHwcaps();
55 auto& fs = GetEmptyFilesystem();
56 fs.CreateFile("/proc/cpuinfo",
57 R"(processor : 14
58 cpu : POWER7 (architected), altivec supported
59 clock : 3000.000000MHz
60 revision : 2.1 (pvr 003f 0201)
61
62 processor : 15
63 cpu : POWER7 (architected), altivec supported
64 clock : 3000.000000MHz
65 revision : 2.1 (pvr 003f 0201)
66
67 timebase : 512000000
68 platform : pSeries
69 model : IBM,8406-70Y
70 machine : CHRP IBM,8406-70Y)");
71 SetPlatformPointer("power7");
72 SetBasePlatformPointer("power8");
73 const auto strings = GetPPCPlatformStrings();
74 ASSERT_STREQ(strings.platform, "pSeries");
75 ASSERT_STREQ(strings.model, "IBM,8406-70Y");
76 ASSERT_STREQ(strings.machine, "CHRP IBM,8406-70Y");
77 ASSERT_STREQ(strings.cpu, "POWER7 (architected), altivec supported");
78 ASSERT_STREQ(strings.type.platform, "power7");
79 ASSERT_STREQ(strings.type.base_platform, "power8");
80 }
81
TEST(CpustringsPPCTest,Firestone)82 TEST(CpustringsPPCTest, Firestone) {
83 ResetHwcaps();
84 auto& fs = GetEmptyFilesystem();
85 fs.CreateFile("/proc/cpuinfo",
86 R"(processor : 126
87 cpu : POWER8 (raw), altivec supported
88 clock : 2061.000000MHz
89 revision : 2.0 (pvr 004d 0200)
90
91 processor : 127
92 cpu : POWER8 (raw), altivec supported
93 clock : 2061.000000MHz
94 revision : 2.0 (pvr 004d 0200)
95
96 timebase : 512000000
97 platform : PowerNV
98 model : 8335-GTA
99 machine : PowerNV 8335-GTA
100 firmware : OPAL v3)");
101 const auto strings = GetPPCPlatformStrings();
102 ASSERT_STREQ(strings.platform, "PowerNV");
103 ASSERT_STREQ(strings.model, "8335-GTA");
104 ASSERT_STREQ(strings.machine, "PowerNV 8335-GTA");
105 ASSERT_STREQ(strings.cpu, "POWER8 (raw), altivec supported");
106 }
107
TEST(CpustringsPPCTest,w8)108 TEST(CpustringsPPCTest, w8) {
109 ResetHwcaps();
110 auto& fs = GetEmptyFilesystem();
111 fs.CreateFile("/proc/cpuinfo",
112 R"(processor : 143
113 cpu : POWER9, altivec supported
114 clock : 2300.000000MHz
115 revision : 2.2 (pvr 004e 1202)
116
117 timebase : 512000000
118 platform : PowerNV
119 model : 0000000000000000
120 machine : PowerNV 0000000000000000
121 firmware : OPAL
122 MMU : Radix)");
123 const auto strings = GetPPCPlatformStrings();
124 ASSERT_STREQ(strings.platform, "PowerNV");
125 ASSERT_STREQ(strings.model, "0000000000000000");
126 ASSERT_STREQ(strings.machine, "PowerNV 0000000000000000");
127 ASSERT_STREQ(strings.cpu, "POWER9, altivec supported");
128 }
129
130 } // namespace
131 } // namespace cpu_features
132