1*eca53ba6SRoland Levillain // Copyright 2022 Google LLC
2*eca53ba6SRoland Levillain //
3*eca53ba6SRoland Levillain // Licensed under the Apache License, Version 2.0 (the "License");
4*eca53ba6SRoland Levillain // you may not use this file except in compliance with the License.
5*eca53ba6SRoland Levillain // You may obtain a copy of the License at
6*eca53ba6SRoland Levillain //
7*eca53ba6SRoland Levillain // http://www.apache.org/licenses/LICENSE-2.0
8*eca53ba6SRoland Levillain //
9*eca53ba6SRoland Levillain // Unless required by applicable law or agreed to in writing, software
10*eca53ba6SRoland Levillain // distributed under the License is distributed on an "AS IS" BASIS,
11*eca53ba6SRoland Levillain // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eca53ba6SRoland Levillain // See the License for the specific language governing permissions and
13*eca53ba6SRoland Levillain // limitations under the License.
14*eca53ba6SRoland Levillain
15*eca53ba6SRoland Levillain #include "cpuinfo_riscv.h"
16*eca53ba6SRoland Levillain
17*eca53ba6SRoland Levillain #include "filesystem_for_testing.h"
18*eca53ba6SRoland Levillain #include "gtest/gtest.h"
19*eca53ba6SRoland Levillain #include "hwcaps_for_testing.h"
20*eca53ba6SRoland Levillain
21*eca53ba6SRoland Levillain namespace cpu_features {
22*eca53ba6SRoland Levillain namespace {
23*eca53ba6SRoland Levillain
TEST(CpuinfoRiscvTest,Sipeed_Lichee_RV_FromCpuInfo)24*eca53ba6SRoland Levillain TEST(CpuinfoRiscvTest, Sipeed_Lichee_RV_FromCpuInfo) {
25*eca53ba6SRoland Levillain ResetHwcaps();
26*eca53ba6SRoland Levillain auto& fs = GetEmptyFilesystem();
27*eca53ba6SRoland Levillain fs.CreateFile("/proc/cpuinfo", R"(processor : 0
28*eca53ba6SRoland Levillain hart : 0
29*eca53ba6SRoland Levillain isa : rv64imafdc
30*eca53ba6SRoland Levillain mmu : sv39
31*eca53ba6SRoland Levillain uarch : thead,c906)");
32*eca53ba6SRoland Levillain const auto info = GetRiscvInfo();
33*eca53ba6SRoland Levillain EXPECT_STREQ(info.uarch, "c906");
34*eca53ba6SRoland Levillain EXPECT_STREQ(info.vendor, "thead");
35*eca53ba6SRoland Levillain
36*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.RV32I);
37*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.RV64I);
38*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.M);
39*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.A);
40*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.F);
41*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.D);
42*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.Q);
43*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.C);
44*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.V);
45*eca53ba6SRoland Levillain }
46*eca53ba6SRoland Levillain
47*eca53ba6SRoland Levillain // https://github.com/ThomasKaiser/sbc-bench/blob/284e82b016ec1beeac42a5fcbe556b670f68441a/results/Kendryte-K510-4.17.0.cpuinfo
TEST(CpuinfoRiscvTest,Kendryte_K510_FromCpuInfo)48*eca53ba6SRoland Levillain TEST(CpuinfoRiscvTest, Kendryte_K510_FromCpuInfo) {
49*eca53ba6SRoland Levillain ResetHwcaps();
50*eca53ba6SRoland Levillain auto& fs = GetEmptyFilesystem();
51*eca53ba6SRoland Levillain fs.CreateFile("/proc/cpuinfo", R"(
52*eca53ba6SRoland Levillain hart : 0
53*eca53ba6SRoland Levillain isa : rv64i2p0m2p0a2p0f2p0d2p0c2p0xv5-0p0
54*eca53ba6SRoland Levillain mmu : sv39
55*eca53ba6SRoland Levillain
56*eca53ba6SRoland Levillain hart : 1
57*eca53ba6SRoland Levillain isa : rv64i2p0m2p0a2p0f2p0d2p0c2p0xv5-0p0
58*eca53ba6SRoland Levillain mmu : sv39");
59*eca53ba6SRoland Levillain const auto info = GetRiscvInfo();
60*eca53ba6SRoland Levillain EXPECT_STREQ(info.uarch, "");
61*eca53ba6SRoland Levillain EXPECT_STREQ(info.vendor, "");
62*eca53ba6SRoland Levillain
63*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.RV32I);
64*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.RV64I);
65*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.M);
66*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.A);
67*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.F);
68*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.D);
69*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.Q);
70*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.C);
71*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.V);
72*eca53ba6SRoland Levillain }
73*eca53ba6SRoland Levillain
74*eca53ba6SRoland Levillain // https://github.com/ThomasKaiser/sbc-bench/blob/284e82b016ec1beeac42a5fcbe556b670f68441a/results/T-Head-C910-5.10.4.cpuinfo
75*eca53ba6SRoland Levillain TEST(CpuinfoRiscvTest, T_Head_C910_FromCpuInfo) {
76*eca53ba6SRoland Levillain ResetHwcaps();
77*eca53ba6SRoland Levillain auto& fs = GetEmptyFilesystem();
78*eca53ba6SRoland Levillain fs.CreateFile("/proc/cpuinfo", R"(
79*eca53ba6SRoland Levillain processor : 0
80*eca53ba6SRoland Levillain hart : 0
81*eca53ba6SRoland Levillain isa : rv64imafdcsu
82*eca53ba6SRoland Levillain mmu : sv39
83*eca53ba6SRoland Levillain cpu-freq : 1.2Ghz
84*eca53ba6SRoland Levillain cpu-icache : 64KB
85*eca53ba6SRoland Levillain cpu-dcache : 64KB
86*eca53ba6SRoland Levillain cpu-l2cache : 2MB
87*eca53ba6SRoland Levillain cpu-tlb : 1024 4-ways
88*eca53ba6SRoland Levillain cpu-cacheline : 64Bytes
89*eca53ba6SRoland Levillain cpu-vector : 0.7.1
90*eca53ba6SRoland Levillain
91*eca53ba6SRoland Levillain processor : 1
92*eca53ba6SRoland Levillain hart : 1
93*eca53ba6SRoland Levillain isa : rv64imafdcsu
94*eca53ba6SRoland Levillain mmu : sv39
95*eca53ba6SRoland Levillain cpu-freq : 1.2Ghz
96*eca53ba6SRoland Levillain cpu-icache : 64KB
97*eca53ba6SRoland Levillain cpu-dcache : 64KB
98*eca53ba6SRoland Levillain cpu-l2cache : 2MB
99*eca53ba6SRoland Levillain cpu-tlb : 1024 4-ways
100*eca53ba6SRoland Levillain cpu-cacheline : 64Bytes
101*eca53ba6SRoland Levillain cpu-vector : 0.7.1");
102*eca53ba6SRoland Levillain const auto info = GetRiscvInfo();
103*eca53ba6SRoland Levillain EXPECT_STREQ(info.uarch, "");
104*eca53ba6SRoland Levillain EXPECT_STREQ(info.vendor, "");
105*eca53ba6SRoland Levillain
106*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.RV32I);
107*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.RV64I);
108*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.M);
109*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.A);
110*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.F);
111*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.D);
112*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.Q);
113*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.C);
114*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.V);
115*eca53ba6SRoland Levillain }
116*eca53ba6SRoland Levillain
117*eca53ba6SRoland Levillain TEST(CpuinfoRiscvTest, UnknownFromCpuInfo) {
118*eca53ba6SRoland Levillain ResetHwcaps();
119*eca53ba6SRoland Levillain auto& fs = GetEmptyFilesystem();
120*eca53ba6SRoland Levillain fs.CreateFile("/proc/cpuinfo", R"(
121*eca53ba6SRoland Levillain processor : 0
122*eca53ba6SRoland Levillain hart : 2
123*eca53ba6SRoland Levillain isa : rv64imafdc
124*eca53ba6SRoland Levillain mmu : sv39
125*eca53ba6SRoland Levillain uarch : sifive,bullet0
126*eca53ba6SRoland Levillain
127*eca53ba6SRoland Levillain processor : 1
128*eca53ba6SRoland Levillain hart : 1
129*eca53ba6SRoland Levillain isa : rv64imafdc
130*eca53ba6SRoland Levillain mmu : sv39
131*eca53ba6SRoland Levillain uarch : sifive,bullet0
132*eca53ba6SRoland Levillain
133*eca53ba6SRoland Levillain processor : 2
134*eca53ba6SRoland Levillain hart : 3
135*eca53ba6SRoland Levillain isa : rv64imafdc
136*eca53ba6SRoland Levillain mmu : sv39
137*eca53ba6SRoland Levillain uarch : sifive,bullet0
138*eca53ba6SRoland Levillain
139*eca53ba6SRoland Levillain processor : 3
140*eca53ba6SRoland Levillain hart : 4
141*eca53ba6SRoland Levillain isa : rv64imafdc
142*eca53ba6SRoland Levillain mmu : sv39
143*eca53ba6SRoland Levillain uarch : sifive,bullet0)");
144*eca53ba6SRoland Levillain const auto info = GetRiscvInfo();
145*eca53ba6SRoland Levillain EXPECT_STREQ(info.uarch, "bullet0");
146*eca53ba6SRoland Levillain EXPECT_STREQ(info.vendor, "sifive");
147*eca53ba6SRoland Levillain
148*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.RV32I);
149*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.RV64I);
150*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.M);
151*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.A);
152*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.F);
153*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.D);
154*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.Q);
155*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.C);
156*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.V);
157*eca53ba6SRoland Levillain }
158*eca53ba6SRoland Levillain
TEST(CpuinfoRiscvTest,QemuCpuInfo)159*eca53ba6SRoland Levillain TEST(CpuinfoRiscvTest, QemuCpuInfo) {
160*eca53ba6SRoland Levillain ResetHwcaps();
161*eca53ba6SRoland Levillain auto& fs = GetEmptyFilesystem();
162*eca53ba6SRoland Levillain fs.CreateFile("/proc/cpuinfo", R"(
163*eca53ba6SRoland Levillain processor : 0
164*eca53ba6SRoland Levillain hart : 0
165*eca53ba6SRoland Levillain isa : rv64imafdcvh_zba_zbb_zbc_zbs
166*eca53ba6SRoland Levillain mmu : sv48)");
167*eca53ba6SRoland Levillain const auto info = GetRiscvInfo();
168*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.RV32I);
169*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.RV64I);
170*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.M);
171*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.A);
172*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.F);
173*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.D);
174*eca53ba6SRoland Levillain EXPECT_FALSE(info.features.Q);
175*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.C);
176*eca53ba6SRoland Levillain EXPECT_TRUE(info.features.V);
177*eca53ba6SRoland Levillain }
178*eca53ba6SRoland Levillain
179*eca53ba6SRoland Levillain } // namespace
180*eca53ba6SRoland Levillain } // namespace cpu_features
181