xref: /dpdk/app/test/test_cpuflags.c (revision dada9ef6)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2012 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 
35 #include <stdio.h>
36 
37 #include <cmdline_parse.h>
38 #include <errno.h>
39 #include <stdint.h>
40 #include <rte_cpuflags.h>
41 #include <rte_debug.h>
42 
43 #include "test.h"
44 
45 
46 /* convenience define */
47 #define CHECK_FOR_FLAG(x) \
48 			result = rte_cpu_get_flag_enabled(x);    \
49 			printf("%s\n", cpu_flag_result(result)); \
50 			if (result == -ENOENT)                   \
51 				return -1;
52 
53 /*
54  * Helper function to display result
55  */
56 static inline const char *
57 cpu_flag_result(int result)
58 {
59 	switch (result) {
60 	case 0:
61 		return "NOT PRESENT";
62 	case 1:
63 		return "OK";
64 	default:
65 		return "ERROR";
66 	}
67 }
68 
69 
70 
71 /*
72  * CPUID test
73  * ===========
74  *
75  * - Check flags from different registers with rte_cpu_get_flag_enabled()
76  * - Check if register and CPUID functions fail properly
77  */
78 
79 int
80 test_cpuflags(void)
81 {
82 	int result;
83 	printf("\nChecking for flags from different registers...\n");
84 
85 	printf("Check for SSE:\t\t");
86 	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE);
87 
88 	printf("Check for SSE2:\t\t");
89 	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE2);
90 
91 	printf("Check for SSE3:\t\t");
92 	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE3);
93 
94 	printf("Check for SSE4.1:\t");
95 	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE4_1);
96 
97 	printf("Check for SSE4.2:\t");
98 	CHECK_FOR_FLAG(RTE_CPUFLAG_SSE4_2);
99 
100 	printf("Check for AVX:\t\t");
101 	CHECK_FOR_FLAG(RTE_CPUFLAG_AVX);
102 
103 	printf("Check for AVX2:\t\t");
104 	CHECK_FOR_FLAG(RTE_CPUFLAG_AVX2);
105 
106 	printf("Check for TRBOBST:\t");
107 	CHECK_FOR_FLAG(RTE_CPUFLAG_TRBOBST);
108 
109 	printf("Check for ENERGY_EFF:\t");
110 	CHECK_FOR_FLAG(RTE_CPUFLAG_ENERGY_EFF);
111 
112 	printf("Check for LAHF_SAHF:\t");
113 	CHECK_FOR_FLAG(RTE_CPUFLAG_LAHF_SAHF);
114 
115 	printf("Check for 1GB_PG:\t");
116 	CHECK_FOR_FLAG(RTE_CPUFLAG_1GB_PG);
117 
118 	printf("Check for INVTSC:\t");
119 	CHECK_FOR_FLAG(RTE_CPUFLAG_INVTSC);
120 
121 
122 
123 	/*
124 	 * Check if invalid data is handled properly
125 	 */
126 	printf("\nCheck for invalid flag:\t");
127 	result = rte_cpu_get_flag_enabled(RTE_CPUFLAG_NUMFLAGS);
128 	printf("%s\n", cpu_flag_result(result));
129 	if (result != -ENOENT)
130 		return -1;
131 
132 	return 0;
133 }
134