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