Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "cpu_detect_test.h"
12 : #include "cpu_detect_print.h"
13 :
14 : #define GROUP_GAP_TIME 1000 // 1000us
15 :
16 : typedef CpudStatus (*GroupDetectFunc)(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu);
17 : typedef void (*TestcaseDetectFuncA)(uint32_t *regValues);
18 : typedef void (*TestcaseDetectFuncB)(uint32_t *regValues, uint32_t *loadStoreBuf);
19 :
20 : typedef struct {
21 : const char *name;
22 : GroupDetectFunc func;
23 : } CpuGroupDetectSets;
24 :
25 : typedef struct {
26 : const char *name;
27 : TestcaseDetectFuncA funcA;
28 : TestcaseDetectFuncB funcB;
29 : } CpuTestcaseDetectSets;
30 :
31 : STATIC CpuGroupDetectSets g_cpuDetectSets[] = {
32 : {"GroupTaiShan", CpuDetectGroupTaiShan},
33 : {"GroupA", CpuDetectGroupA},
34 : {"GroupB", CpuDetectGroupB},
35 : {"GroupC", CpuDetectGroupC},
36 : {"GroupD", CpuDetectGroupD},
37 : {"GroupE", CpuDetectGroupE},
38 : {"GroupF", CpuDetectGroupF},
39 : {"GroupG", CpuDetectGroupG},
40 : {"GroupH", CpuDetectGroupH},
41 : };
42 :
43 : #ifdef CPU_DETECT_LINX_CORE
44 : // Taishan
45 : STATIC CpuTestcaseDetectSets g_cpuGroupTaiShanDetectSets[] = {};
46 :
47 : // GroupA
48 : STATIC CpuTestcaseDetectSets g_cpuGroupADetectSets[] = {
49 : {"TSRTIntAlu", TSRTIntAlu, NULL},
50 : {"TSRTIntCompare", TSRTIntCompare, NULL},
51 : {"TSRTIntCRCAndShiftAndMov", TSRTIntCRCAndShiftAndMov, NULL},
52 : };
53 :
54 : // GroupB
55 : STATIC CpuTestcaseDetectSets g_cpuGroupBDetectSets[] = {};
56 :
57 : // GroupC
58 : STATIC CpuTestcaseDetectSets g_cpuGroupCDetectSets[] = {};
59 :
60 : // GroupD
61 : STATIC CpuTestcaseDetectSets g_cpuGroupDDetectSets[] = {};
62 :
63 : // GroupE
64 : STATIC CpuTestcaseDetectSets g_cpuGroupEDetectSets[] = {
65 : {"GeneralLoadStoreP01", NULL, GeneralLoadStoreP01},
66 : {"GeneralLoadStoreP02", NULL, GeneralLoadStoreP02},
67 : {"LCRTGeneralLoadStoreP02", NULL, LCRTGeneralLoadStoreP02},
68 : {"LCRTGeneralLoadStoreP03", NULL, LCRTGeneralLoadStoreP03},
69 : {"LCRTGeneralLoadStoreP04", NULL, LCRTGeneralLoadStoreP04},
70 : {"GeneralPrefetchP01", NULL, GeneralPrefetchP01},
71 : {"LCRTGeneralLoadStoreP05", NULL, LCRTGeneralLoadStoreP05},
72 : };
73 :
74 : // GroupF
75 : STATIC CpuTestcaseDetectSets g_cpuGroupFDetectSets[] = {
76 : {"BranchInGeneralP01", BranchInGeneralP01, NULL},
77 : {"BranchInGeneralP02", BranchInGeneralP02, NULL},
78 : };
79 :
80 : // GroupG
81 : STATIC CpuTestcaseDetectSets g_cpuGroupGDetectSets[] = {
82 : {"EnhanceBranchP01", EnhanceBranchP01, NULL},
83 : {"EnhanceLoadStoreP02", NULL, EnhanceLoadStoreP02},
84 : {"BrAdrLiteral", BrAdrLiteral, NULL},
85 : {"DataCacheInvalidP01", NULL, DataCacheInvalidP01},
86 : {"EnhanceLoadStoreP01", NULL, EnhanceLoadStoreP01},
87 : {"InstrCacheInvalid", InstrCacheInvalid, NULL},
88 : {"LoadALUDependencyP01", NULL, LoadALUDependencyP01},
89 : };
90 :
91 : // GroupH
92 : STATIC CpuTestcaseDetectSets g_cpuGroupHDetectSets[] = {};
93 :
94 : #else
95 : // Taishan
96 : STATIC CpuTestcaseDetectSets g_cpuGroupTaiShanDetectSets[] = {
97 : {"TaishanFpComparisonP01", TaishanFpComparisonP01, NULL},
98 : {"TaishanFpMiniMaxiP01", TaishanFpMiniMaxiP01, NULL},
99 : {"TaishanFpMovMaddP01", TaishanFpMovMaddP01, NULL},
100 : {"TaishanSimdAddP01", TaishanSimdAddP01, NULL},
101 : {"TaishanSimdAritheticP01", TaishanSimdAritheticP01, NULL},
102 : {"TaishanSimdCompareP01", TaishanSimdCompareP01, NULL},
103 : {"TaishanSimdComplexP01", TaishanSimdComplexP01, NULL},
104 : {"TaishanSimdCryptoP01", TaishanSimdCryptoP01, NULL},
105 : {"TaishanSimdElementArithemticP01", TaishanSimdElementArithemticP01, NULL},
106 : {"TaishanSimdFpIntConversionP01", TaishanSimdFpIntConversionP01, NULL},
107 : {"TaishanSimdImmP01", TaishanSimdImmP01, NULL},
108 : {"TaishanSimdMovP01", TaishanSimdMovP01, NULL},
109 : {"TaishanSimdPairArithmeticP01", TaishanSimdPairArithmeticP01, NULL},
110 : {"TaishanSimdPermuteP01", TaishanSimdPermuteP01, NULL},
111 : {"TaishanSimdReduceP01", TaishanSimdReduceP01, NULL},
112 : {"TaishanSimdUnaryArithmeticP01", TaishanSimdUnaryArithmeticP01, NULL},
113 : {"TaishanSimdWideNarryArthemticP01", TaishanSimdWideNarryArthemticP01, NULL},
114 : {"TaishanIntAluP01", TaishanIntAluP01, NULL},
115 : {"TaishanIntCcP01", TaishanIntCcP01, NULL},
116 : {"TaishanIntMduP01", TaishanIntMduP01, NULL},
117 : {"TaishanIntAluP02", TaishanIntAluP02, NULL},
118 : {"TaishanIntMduP02", TaishanIntMduP02, NULL},
119 : {"TaishanIntCacheWriteReadP01", NULL, TaishanIntCacheWriteReadP01},
120 : {"TaishanIntCacheWriteReadP02", NULL, TaishanIntCacheWriteReadP02},
121 : {"TaishanIntCacheWriteReadP03", NULL, TaishanIntCacheWriteReadP03},
122 : {"TaishanIntCacheWriteReadP04", NULL, TaishanIntCacheWriteReadP04},
123 : {"TaishanIntCacheWriteReadP05", NULL, TaishanIntCacheWriteReadP05},
124 : {"TaishanIntCacheWriteReadP06", NULL, TaishanIntCacheWriteReadP06},
125 : };
126 :
127 : // GroupA
128 : STATIC CpuTestcaseDetectSets g_cpuGroupADetectSets[] = {
129 : {"SinglePressAdvsimdScalar01", SinglePressAdvsimdScalar01, NULL},
130 : {"SinglePressAdvsimdScalar02", SinglePressAdvsimdScalar02, NULL},
131 : {"SinglePressAdvsimdScalar03", SinglePressAdvsimdScalar03, NULL},
132 : {"SinglePressAdvsimdVector01", SinglePressAdvsimdVector01, NULL},
133 : {"SinglePressAdvsimdVector02", SinglePressAdvsimdVector02, NULL},
134 : {"SinglePressAdvsimdVector03", SinglePressAdvsimdVector03, NULL},
135 : {"SinglePressAdvsimdVector04", SinglePressAdvsimdVector04, NULL},
136 : {"SinglePressAdvsimdVector05", SinglePressAdvsimdVector05, NULL},
137 : {"SinglePressAdvsimdVector06", SinglePressAdvsimdVector06, NULL},
138 : {"SinglePressAdvsimdVector07", SinglePressAdvsimdVector07, NULL},
139 : {"SinglePressAdvsimdVector08", SinglePressAdvsimdVector08, NULL},
140 : {"SinglePressFloat01", SinglePressFloat01, NULL},
141 : {"SinglePressFloat02", SinglePressFloat02, NULL},
142 : {"SinglePressFloat03", SinglePressFloat03, NULL},
143 : {"SinglePressInt01", SinglePressInt01, NULL},
144 : {"SinglePressInt02", SinglePressInt02, NULL},
145 : {"SinglePressInt03", SinglePressInt03, NULL},
146 : };
147 :
148 : // GroupB
149 : STATIC CpuTestcaseDetectSets g_cpuGroupBDetectSets[] = {
150 : {"TSRTIntAllOperation", TSRTIntAllOperation, NULL},
151 : {"TSRTIntCompare", TSRTIntCompare, NULL},
152 : {"TSRTIntAlu", TSRTIntAlu, NULL},
153 : {"TSRTIntCRCAndShiftAndMov", TSRTIntCRCAndShiftAndMov, NULL},
154 : };
155 :
156 : // GroupC
157 : STATIC CpuTestcaseDetectSets g_cpuGroupCDetectSets[] = {
158 : {"TSRTFloatAllOperation", TSRTFloatAllOperation, NULL},
159 : {"TSRTFloatCompare", TSRTFloatCompare, NULL},
160 : {"TSRTFloatFixedPointConvertToFloatPoint", TSRTFloatFixedPointConvertToFloatPoint, NULL},
161 : {"TSRTFloatFloatPointConvertPrecision", TSRTFloatFloatPointConvertPrecision, NULL},
162 : {"TSRTFloatFloatPointRoundToInteger", TSRTFloatFloatPointRoundToInteger, NULL},
163 : {"TSRTFloatMinMax", TSRTFloatMinMax, NULL},
164 : {"TSRTFloatMovMadd", TSRTFloatMovMadd, NULL},
165 : };
166 :
167 : // GroupD
168 : STATIC CpuTestcaseDetectSets g_cpuGroupDDetectSets[] = {
169 : {"TSRTAdvsimdScalarAllOperation", TSRTAdvsimdScalarAllOperation, NULL},
170 : {"TSRTAdvsimdScalarCompare", TSRTAdvsimdScalarCompare, NULL},
171 : {"TSRTAdvsimdScalarFixedPointConvertToFloatPoint", TSRTAdvsimdScalarFixedPointConvertToFloatPoint, NULL},
172 : {"TSRTAdvsimdScalarFloatPointConvertToInteger", TSRTAdvsimdScalarFloatPointConvertToInteger, NULL},
173 : {"TSRTAdvsimdScalarGeneralOperation", TSRTAdvsimdScalarGeneralOperation, NULL},
174 : };
175 :
176 : // GroupE
177 : STATIC CpuTestcaseDetectSets g_cpuGroupEDetectSets[] = {
178 : {"TSRTAdvsimdVectorAllOperation", TSRTAdvsimdVectorAllOperation, NULL},
179 : {"TSRTAdvsimdVectorCompare", TSRTAdvsimdVectorCompare, NULL},
180 : {"TSRTAdvsimdVectorFixedPointConvertToFloatingPoint", TSRTAdvsimdVectorFixedPointConvertToFloatingPoint, NULL},
181 : {"TSRTAdvsimdVectorFloatingPointConvertToInteger", TSRTAdvsimdVectorFloatingPointConvertToInteger, NULL},
182 : {"TSRTAdvsimdVectorFloatingPointOperation", TSRTAdvsimdVectorFloatingPointOperation, NULL},
183 : {"TSRTAdvsimdVectorGeneralOperation", TSRTAdvsimdVectorGeneralOperation, NULL},
184 : {"TSRTAdvsimdVectorMaxMin", TSRTAdvsimdVectorMaxMin, NULL},
185 : {"TSRTAdvsimdVectorSecureHashStandard", TSRTAdvsimdVectorSecureHashStandard, NULL},
186 : {"TSRTAdvsimdVectorSignedOperation", TSRTAdvsimdVectorSignedOperation, NULL},
187 : {"TSRTAdvsimdVectorUnsignedOperation", TSRTAdvsimdVectorUnsignedOperation, NULL},
188 : };
189 :
190 : // GroupF
191 : STATIC CpuTestcaseDetectSets g_cpuGroupFDetectSets[] = {
192 : {"AdvsimdLoadDeprecatedP01", NULL, AdvsimdLoadDeprecatedP01},
193 : {"AdvsimdLoadDeprecatedP02", NULL, AdvsimdLoadDeprecatedP02},
194 : {"AdvsimdLoadDeprecatedP03", NULL, AdvsimdLoadDeprecatedP03},
195 : {"AdvsimdLoadDeprecatedP04", NULL, AdvsimdLoadDeprecatedP04},
196 : {"AdvsimdLoadStoreP01", NULL, AdvsimdLoadStoreP01},
197 : {"AdvsimdLoadStoreP02", NULL, AdvsimdLoadStoreP02},
198 : {"AdvsimdLoadStoreP03", NULL, AdvsimdLoadStoreP03},
199 : {"AdvsimdLoadStoreP04", NULL, AdvsimdLoadStoreP04},
200 : {"AdvsimdLoadStoreP05", NULL, AdvsimdLoadStoreP05},
201 : {"AdvsimdLoadStoreP06", NULL, AdvsimdLoadStoreP06},
202 : {"AdvsimdLoadStoreP07", NULL, AdvsimdLoadStoreP07},
203 : {"FpsimdLoadStoreP01", NULL, FpsimdLoadStoreP01},
204 : {"FpsimdLoadStoreP02", NULL, FpsimdLoadStoreP02},
205 : {"GeneralLoadStoreP01", NULL, GeneralLoadStoreP01},
206 : {"GeneralLoadStoreP02", NULL, GeneralLoadStoreP02},
207 : {"GeneralLoadStoreP03", NULL, GeneralLoadStoreP03},
208 : {"GeneralLoadStoreP04", NULL, GeneralLoadStoreP04},
209 : };
210 :
211 : // GroupG
212 : STATIC CpuTestcaseDetectSets g_cpuGroupGDetectSets[] = {
213 : {"BranchInGeneralP01", BranchInGeneralP01, NULL},
214 : {"BranchInGeneralP02", BranchInGeneralP02, NULL},
215 :
216 : };
217 :
218 : // GroupH
219 : STATIC CpuTestcaseDetectSets g_cpuGroupHDetectSets[] = {
220 : {"DataReverseAdvsimdScalarP01", DataReverseAdvsimdScalarP01, NULL},
221 : {"DataReverseAdvsimdVectorP01", DataReverseAdvsimdVectorP01, NULL},
222 : {"DataReverseAdvsimdVectorP02", DataReverseAdvsimdVectorP02, NULL},
223 : {"DataReverseAdvsimdVectorP03", DataReverseAdvsimdVectorP03, NULL},
224 : {"DataReverseAdvsimdVectorP04", DataReverseAdvsimdVectorP04, NULL},
225 : {"DataReverseFloatP01", DataReverseFloatP01, NULL},
226 : {"DataReverseIntP01", DataReverseIntP01, NULL},
227 : {"EnhanceBranchP01", EnhanceBranchP01, NULL},
228 : {"EnhanceDependencyP01", EnhanceDependencyP01, NULL},
229 : {"EnhanceLoadStoreP01", NULL, EnhanceLoadStoreP01},
230 : {"EnhanceLoadStoreP02", NULL, EnhanceLoadStoreP02},
231 : };
232 :
233 : #endif
234 :
235 : //print the debug info of CPU
236 1 : STATIC void CpuDetectDumpBuf(uint32_t *regValues, int32_t len, int32_t cpu)
237 : {
238 1 : const int32_t regSize = 4;
239 1 : const int32_t regNum = len / regSize;
240 1 : uint32_t *p = regValues;
241 17 : for (int32_t idx = 0; idx < regNum; idx++) {
242 16 : int32_t v = idx * regSize;
243 16 : ADETECT_ERR("cpu(%d) detect failed, X%02d:0x%08x_%08x X%02d:0x%08x_%08x",
244 : cpu, (v / 2 + 1), p[v + 1], p[v], (v / 2 + 2), p[v + 3], p[v + 2]); // 一个寄存器有4个字节,分别打印0\1\2\3字节
245 : }
246 1 : }
247 :
248 80689 : STATIC bool CpuDetectCheckBuf(uint32_t *regValues, int32_t len)
249 : {
250 : // if test fail, buf[1][0] will not be zero
251 80689 : const int32_t retReg = 2;
252 80689 : if ((len < retReg) || (regValues[0] != 0) || (regValues[1] != 0)) {
253 0 : return false;
254 : }
255 80689 : return true;
256 : }
257 :
258 7217 : STATIC CpudStatus CpuDetectGroupComm(const CpuTestcaseDetectSets *detectSet, uint32_t num, uint32_t *regValues,
259 : uint32_t *loadStoreBuf, int cpu)
260 : {
261 87819 : for (uint32_t i = 0; i < num; i++) {
262 80664 : if ((detectSet[i].funcA == NULL) && (detectSet[i].funcB == NULL)) {
263 0 : continue;
264 : }
265 : // run testcase
266 80664 : if (detectSet[i].funcA != NULL) {
267 60697 : (void)memset_s(regValues, TEMP_BUF_SIZE, 0, TEMP_BUF_SIZE);
268 60896 : detectSet[i].funcA(regValues);
269 19967 : } else if (detectSet[i].funcB != NULL) {
270 19965 : (void)memset_s(regValues, TEMP_BUF_SIZE, 0, TEMP_BUF_SIZE);
271 20028 : (void)memset_s(loadStoreBuf, TEMP_BUF_SIZE, 0, TEMP_BUF_SIZE);
272 19991 : detectSet[i].funcB(regValues, loadStoreBuf);
273 : }
274 :
275 : // check
276 80818 : bool result = CpuDetectCheckBuf(regValues, TEMP_BUF_SIZE);
277 80633 : if (!result) {
278 31 : ADETECT_ERR("cpu(%d) detection run testcase(%s) failed, reg[0] = %d, reg[1] = %d.", cpu, detectSet[i].name,
279 : regValues[0], regValues[1]);
280 1 : CpuDetectDumpBuf(regValues, PRINT_TEMP_BUF_LEN, cpu);
281 1 : return CPUD_ERROR_TESTCASE;
282 : }
283 : }
284 7155 : usleep(GROUP_GAP_TIME);
285 7217 : return CPUD_SUCCESS;
286 : }
287 :
288 802 : CpudStatus CpuDetectGroupTaiShan(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
289 : {
290 802 : const uint32_t num = sizeof(g_cpuGroupTaiShanDetectSets) / sizeof(CpuTestcaseDetectSets);
291 802 : return CpuDetectGroupComm(g_cpuGroupTaiShanDetectSets, num, regValues, loadStoreBuf, cpu);
292 : }
293 :
294 802 : CpudStatus CpuDetectGroupA(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
295 : {
296 802 : const uint32_t num = sizeof(g_cpuGroupADetectSets) / sizeof(CpuTestcaseDetectSets);
297 802 : return CpuDetectGroupComm(g_cpuGroupADetectSets, num, regValues, loadStoreBuf, cpu);
298 : }
299 :
300 802 : CpudStatus CpuDetectGroupB(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
301 : {
302 802 : const uint32_t num = sizeof(g_cpuGroupBDetectSets) / sizeof(CpuTestcaseDetectSets);
303 802 : return CpuDetectGroupComm(g_cpuGroupBDetectSets, num, regValues, loadStoreBuf, cpu);
304 : }
305 :
306 802 : CpudStatus CpuDetectGroupC(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
307 : {
308 802 : const uint32_t num = sizeof(g_cpuGroupCDetectSets) / sizeof(CpuTestcaseDetectSets);
309 802 : return CpuDetectGroupComm(g_cpuGroupCDetectSets, num, regValues, loadStoreBuf, cpu);
310 : }
311 :
312 802 : CpudStatus CpuDetectGroupD(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
313 : {
314 802 : const uint32_t num = sizeof(g_cpuGroupDDetectSets) / sizeof(CpuTestcaseDetectSets);
315 802 : return CpuDetectGroupComm(g_cpuGroupDDetectSets, num, regValues, loadStoreBuf, cpu);
316 : }
317 :
318 802 : CpudStatus CpuDetectGroupE(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
319 : {
320 802 : const uint32_t num = sizeof(g_cpuGroupEDetectSets) / sizeof(CpuTestcaseDetectSets);
321 802 : return CpuDetectGroupComm(g_cpuGroupEDetectSets, num, regValues, loadStoreBuf, cpu);
322 : }
323 :
324 802 : CpudStatus CpuDetectGroupF(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
325 : {
326 802 : const uint32_t num = sizeof(g_cpuGroupFDetectSets) / sizeof(CpuTestcaseDetectSets);
327 802 : return CpuDetectGroupComm(g_cpuGroupFDetectSets, num, regValues, loadStoreBuf, cpu);
328 : }
329 :
330 802 : CpudStatus CpuDetectGroupG(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
331 : {
332 802 : const uint32_t num = sizeof(g_cpuGroupGDetectSets) / sizeof(CpuTestcaseDetectSets);
333 802 : return CpuDetectGroupComm(g_cpuGroupGDetectSets, num, regValues, loadStoreBuf, cpu);
334 : }
335 :
336 802 : CpudStatus CpuDetectGroupH(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
337 : {
338 802 : const uint32_t num = sizeof(g_cpuGroupHDetectSets) / sizeof(CpuTestcaseDetectSets);
339 802 : return CpuDetectGroupComm(g_cpuGroupHDetectSets, num, regValues, loadStoreBuf, cpu);
340 : }
341 :
342 802 : CpudStatus CpuDetectGroup(uint32_t *regValues, uint32_t *loadStoreBuf, int32_t cpu)
343 : {
344 802 : ADETECT_CHK_EXPR_ACTION((regValues == NULL) || (loadStoreBuf == NULL), return CPUD_ERROR_PARAM,
345 : "invalid input param.");
346 802 : int32_t num = sizeof(g_cpuDetectSets) / sizeof(CpuGroupDetectSets);
347 8019 : for (int32_t i = 0; i < num; i++) {
348 7218 : if (g_cpuDetectSets[i].func == NULL) {
349 0 : continue;
350 : }
351 7218 : CpudStatus ret = g_cpuDetectSets[i].func(regValues, loadStoreBuf, cpu);
352 7218 : if (ret != CPUD_SUCCESS) {
353 1 : ADETECT_ERR("cpu(%d) run testcase group(%s) failed.", cpu, g_cpuDetectSets[i].name);
354 1 : return ret;
355 : }
356 : }
357 801 : return CPUD_SUCCESS;
358 : }
|