1/*-
2 *   BSD LICENSE
3 *
4 *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 *   All rights reserved.
6 *
7 *   Redistribution and use in source and binary forms, with or without
8 *   modification, are permitted provided that the following conditions
9 *   are met:
10 *
11 *     * Redistributions of source code must retain the above copyright
12 *       notice, this list of conditions and the following disclaimer.
13 *     * Redistributions in binary form must reproduce the above copyright
14 *       notice, this list of conditions and the following disclaimer in
15 *       the documentation and/or other materials provided with the
16 *       distribution.
17 *     * Neither the name of Intel Corporation nor the names of its
18 *       contributors may be used to endorse or promote products derived
19 *       from this software without specific prior written permission.
20 *
21 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <stdio.h>
35
36#include <errno.h>
37#include <stdint.h>
38#include <rte_cpuflags.h>
39#include <rte_debug.h>
40
41#include "test.h"
42
43
44/* convenience define */
45#define CHECK_FOR_FLAG(x) \
46			result = rte_cpu_get_flag_enabled(x);    \
47			printf("%s\n", cpu_flag_result(result)); \
48			if (result == -ENOENT)                   \
49				return -1;
50
51/*
52 * Helper function to display result
53 */
54static inline const char *
55cpu_flag_result(int result)
56{
57	switch (result) {
58	case 0:
59		return "NOT PRESENT";
60	case 1:
61		return "OK";
62	default:
63		return "ERROR";
64	}
65}
66
67
68
69/*
70 * CPUID test
71 * ===========
72 *
73 * - Check flags from different registers with rte_cpu_get_flag_enabled()
74 * - Check if register and CPUID functions fail properly
75 */
76
77static int
78test_cpuflags(void)
79{
80	int result;
81	printf("\nChecking for flags from different registers...\n");
82
83#ifdef RTE_ARCH_PPC_64
84	printf("Check for PPC64:\t\t");
85	CHECK_FOR_FLAG(RTE_CPUFLAG_PPC64);
86
87	printf("Check for PPC32:\t\t");
88	CHECK_FOR_FLAG(RTE_CPUFLAG_PPC32);
89
90	printf("Check for VSX:\t\t");
91	CHECK_FOR_FLAG(RTE_CPUFLAG_VSX);
92
93	printf("Check for DFP:\t\t");
94	CHECK_FOR_FLAG(RTE_CPUFLAG_DFP);
95
96	printf("Check for FPU:\t\t");
97	CHECK_FOR_FLAG(RTE_CPUFLAG_FPU);
98
99	printf("Check for SMT:\t\t");
100	CHECK_FOR_FLAG(RTE_CPUFLAG_SMT);
101
102	printf("Check for MMU:\t\t");
103	CHECK_FOR_FLAG(RTE_CPUFLAG_MMU);
104
105	printf("Check for ALTIVEC:\t\t");
106	CHECK_FOR_FLAG(RTE_CPUFLAG_ALTIVEC);
107
108	printf("Check for ARCH_2_06:\t\t");
109	CHECK_FOR_FLAG(RTE_CPUFLAG_ARCH_2_06);
110
111	printf("Check for ARCH_2_07:\t\t");
112	CHECK_FOR_FLAG(RTE_CPUFLAG_ARCH_2_07);
113
114	printf("Check for ICACHE_SNOOP:\t\t");
115	CHECK_FOR_FLAG(RTE_CPUFLAG_ICACHE_SNOOP);
116#endif
117
118#if defined(RTE_ARCH_ARM)
119	printf("Check for NEON:\t\t");
120	CHECK_FOR_FLAG(RTE_CPUFLAG_NEON);
121#endif
122
123#if defined(RTE_ARCH_ARM64)
124	printf("Check for FP:\t\t");
125	CHECK_FOR_FLAG(RTE_CPUFLAG_FP);
126
127	printf("Check for ASIMD:\t");
128	CHECK_FOR_FLAG(RTE_CPUFLAG_NEON);
129
130	printf("Check for EVTSTRM:\t");
131	CHECK_FOR_FLAG(RTE_CPUFLAG_EVTSTRM);
132
133	printf("Check for AES:\t\t");
134	CHECK_FOR_FLAG(RTE_CPUFLAG_AES);
135
136	printf("Check for PMULL:\t");
137	CHECK_FOR_FLAG(RTE_CPUFLAG_PMULL);
138
139	printf("Check for SHA1:\t\t");
140	CHECK_FOR_FLAG(RTE_CPUFLAG_SHA1);
141
142	printf("Check for SHA2:\t\t");
143	CHECK_FOR_FLAG(RTE_CPUFLAG_SHA2);
144
145	printf("Check for CRC32:\t");
146	CHECK_FOR_FLAG(RTE_CPUFLAG_CRC32);
147
148	printf("Check for ATOMICS:\t");
149	CHECK_FOR_FLAG(RTE_CPUFLAG_ATOMICS);
150#endif
151
152#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
153	printf("Check for SSE:\t\t");
154	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE);
155
156	printf("Check for SSE2:\t\t");
157	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE2);
158
159	printf("Check for SSE3:\t\t");
160	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE3);
161
162	printf("Check for SSE4.1:\t");
163	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE4_1);
164
165	printf("Check for SSE4.2:\t");
166	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE4_2);
167
168	printf("Check for AVX:\t\t");
169	CHECK_FOR_FLAG(RTE_CPUFLAG_AVX);
170
171	printf("Check for AVX2:\t\t");
172	CHECK_FOR_FLAG(RTE_CPUFLAG_AVX2);
173
174	printf("Check for TRBOBST:\t");
175	CHECK_FOR_FLAG(RTE_CPUFLAG_TRBOBST);
176
177	printf("Check for ENERGY_EFF:\t");
178	CHECK_FOR_FLAG(RTE_CPUFLAG_ENERGY_EFF);
179
180	printf("Check for LAHF_SAHF:\t");
181	CHECK_FOR_FLAG(RTE_CPUFLAG_LAHF_SAHF);
182
183	printf("Check for 1GB_PG:\t");
184	CHECK_FOR_FLAG(RTE_CPUFLAG_1GB_PG);
185
186	printf("Check for INVTSC:\t");
187	CHECK_FOR_FLAG(RTE_CPUFLAG_INVTSC);
188#endif
189
190	/*
191	 * Check if invalid data is handled properly
192	 */
193	printf("\nCheck for invalid flag:\t");
194	result = rte_cpu_get_flag_enabled(RTE_CPUFLAG_NUMFLAGS);
195	printf("%s\n", cpu_flag_result(result));
196	if (result != -ENOENT)
197		return -1;
198
199	return 0;
200}
201
202REGISTER_TEST_COMMAND(cpuflags_autotest, test_cpuflags);
203