xref: /openbmc/qemu/tests/unit/test-x86-topo.c (revision 321d2599)
1 /*
2  *  Test code for x86 APIC ID and Topology functions
3  *
4  *  Copyright (c) 2012 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 
27 #include "hw/i386/topology.h"
28 
test_topo_bits(void)29 static void test_topo_bits(void)
30 {
31     X86CPUTopoInfo topo_info = {0};
32 
33     /*
34      * simple tests for 1 thread per core, 1 core per module,
35      *                  1 module per die, 1 die per package
36      */
37     topo_info = (X86CPUTopoInfo) {1, 1, 1, 1};
38     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 0);
39     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 0);
40     g_assert_cmpuint(apicid_module_width(&topo_info), ==, 0);
41     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 0);
42 
43     topo_info = (X86CPUTopoInfo) {1, 1, 1, 1};
44     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0);
45     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1);
46     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2);
47     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 3), ==, 3);
48 
49 
50     /* Test field width calculation for multiple values
51      */
52     topo_info = (X86CPUTopoInfo) {1, 1, 1, 2};
53     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 1);
54     topo_info = (X86CPUTopoInfo) {1, 1, 1, 3};
55     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
56     topo_info = (X86CPUTopoInfo) {1, 1, 1, 4};
57     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
58 
59     topo_info = (X86CPUTopoInfo) {1, 1, 1, 14};
60     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
61     topo_info = (X86CPUTopoInfo) {1, 1, 1, 15};
62     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
63     topo_info = (X86CPUTopoInfo) {1, 1, 1, 16};
64     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
65     topo_info = (X86CPUTopoInfo) {1, 1, 1, 17};
66     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 5);
67 
68 
69     topo_info = (X86CPUTopoInfo) {1, 1, 30, 2};
70     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
71     topo_info = (X86CPUTopoInfo) {1, 1, 31, 2};
72     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
73     topo_info = (X86CPUTopoInfo) {1, 1, 32, 2};
74     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
75     topo_info = (X86CPUTopoInfo) {1, 1, 33, 2};
76     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 6);
77 
78     topo_info = (X86CPUTopoInfo) {1, 6, 30, 2};
79     g_assert_cmpuint(apicid_module_width(&topo_info), ==, 3);
80     topo_info = (X86CPUTopoInfo) {1, 7, 30, 2};
81     g_assert_cmpuint(apicid_module_width(&topo_info), ==, 3);
82     topo_info = (X86CPUTopoInfo) {1, 8, 30, 2};
83     g_assert_cmpuint(apicid_module_width(&topo_info), ==, 3);
84     topo_info = (X86CPUTopoInfo) {1, 9, 30, 2};
85     g_assert_cmpuint(apicid_module_width(&topo_info), ==, 4);
86 
87     topo_info = (X86CPUTopoInfo) {1, 6, 30, 2};
88     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 0);
89     topo_info = (X86CPUTopoInfo) {2, 6, 30, 2};
90     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 1);
91     topo_info = (X86CPUTopoInfo) {3, 6, 30, 2};
92     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 2);
93     topo_info = (X86CPUTopoInfo) {4, 6, 30, 2};
94     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 2);
95 
96     /* build a weird topology and see if IDs are calculated correctly
97      */
98 
99     /* This will use 2 bits for thread ID and 3 bits for core ID
100      */
101     topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
102     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
103     g_assert_cmpuint(apicid_core_offset(&topo_info), ==, 2);
104     g_assert_cmpuint(apicid_module_offset(&topo_info), ==, 5);
105     g_assert_cmpuint(apicid_die_offset(&topo_info), ==, 5);
106     g_assert_cmpuint(apicid_pkg_offset(&topo_info), ==, 5);
107 
108     topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
109     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0);
110     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1);
111     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2);
112 
113     topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
114     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 0), ==,
115                      (1 << 2) | 0);
116     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 1), ==,
117                      (1 << 2) | 1);
118     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 2), ==,
119                      (1 << 2) | 2);
120 
121     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 0), ==,
122                      (2 << 2) | 0);
123     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 1), ==,
124                      (2 << 2) | 1);
125     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 2), ==,
126                      (2 << 2) | 2);
127 
128     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 0), ==,
129                      (5 << 2) | 0);
130     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 1), ==,
131                      (5 << 2) | 1);
132     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 2), ==,
133                      (5 << 2) | 2);
134 
135     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
136                      1 * 6 * 3 + 0 * 3 + 0), ==, (1 << 5));
137     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
138                      1 * 6 * 3 + 1 * 3 + 1), ==, (1 << 5) | (1 << 2) | 1);
139     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
140                      3 * 6 * 3 + 5 * 3 + 2), ==, (3 << 5) | (5 << 2) | 2);
141 }
142 
main(int argc,char ** argv)143 int main(int argc, char **argv)
144 {
145     g_test_init(&argc, &argv, NULL);
146 
147     g_test_add_func("/cpuid/topology/basic", test_topo_bits);
148 
149     g_test_run();
150 
151     return 0;
152 }
153