xref: /openbmc/qemu/tests/unit/test-x86-topo.c (revision 3568adc9)
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 
29af4c26e6SZhao Liu static void test_topo_bits(void)
30af4c26e6SZhao Liu {
31af4c26e6SZhao Liu     X86CPUTopoInfo topo_info = {0};
32af4c26e6SZhao Liu 
33*3568adc9SZhao Liu     /*
34*3568adc9SZhao Liu      * simple tests for 1 thread per core, 1 core per module,
35*3568adc9SZhao Liu      *                  1 module per die, 1 die per package
36*3568adc9SZhao Liu      */
37*3568adc9SZhao 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);
40af4c26e6SZhao Liu     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 0);
41af4c26e6SZhao Liu 
42*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 1};
43af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0);
44af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1);
45af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2);
46af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 3), ==, 3);
47af4c26e6SZhao Liu 
48af4c26e6SZhao Liu 
49af4c26e6SZhao Liu     /* Test field width calculation for multiple values
50af4c26e6SZhao Liu      */
51*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 2};
52af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 1);
53*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 3};
54af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
55*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 4};
56af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
57af4c26e6SZhao Liu 
58*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 14};
59af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
60*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 15};
61af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
62*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 16};
63af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
64*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 1, 17};
65af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 5);
66af4c26e6SZhao Liu 
67af4c26e6SZhao Liu 
68*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 30, 2};
69af4c26e6SZhao Liu     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
70*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 31, 2};
71af4c26e6SZhao Liu     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
72*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 32, 2};
73af4c26e6SZhao Liu     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
74*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 33, 2};
75af4c26e6SZhao Liu     g_assert_cmpuint(apicid_core_width(&topo_info), ==, 6);
76af4c26e6SZhao Liu 
77*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 30, 2};
78af4c26e6SZhao Liu     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 0);
79*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {2, 1, 30, 2};
80af4c26e6SZhao Liu     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 1);
81*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {3, 1, 30, 2};
82af4c26e6SZhao Liu     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 2);
83*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {4, 1, 30, 2};
84af4c26e6SZhao Liu     g_assert_cmpuint(apicid_die_width(&topo_info), ==, 2);
85af4c26e6SZhao Liu 
86af4c26e6SZhao Liu     /* build a weird topology and see if IDs are calculated correctly
87af4c26e6SZhao Liu      */
88af4c26e6SZhao Liu 
89af4c26e6SZhao Liu     /* This will use 2 bits for thread ID and 3 bits for core ID
90af4c26e6SZhao Liu      */
91*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
92af4c26e6SZhao Liu     g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
93af4c26e6SZhao Liu     g_assert_cmpuint(apicid_core_offset(&topo_info), ==, 2);
94af4c26e6SZhao Liu     g_assert_cmpuint(apicid_die_offset(&topo_info), ==, 5);
95af4c26e6SZhao Liu     g_assert_cmpuint(apicid_pkg_offset(&topo_info), ==, 5);
96af4c26e6SZhao Liu 
97*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
98af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0);
99af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1);
100af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2);
101af4c26e6SZhao Liu 
102*3568adc9SZhao Liu     topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
103af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 0), ==,
104af4c26e6SZhao Liu                      (1 << 2) | 0);
105af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 1), ==,
106af4c26e6SZhao Liu                      (1 << 2) | 1);
107af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 2), ==,
108af4c26e6SZhao Liu                      (1 << 2) | 2);
109af4c26e6SZhao Liu 
110af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 0), ==,
111af4c26e6SZhao Liu                      (2 << 2) | 0);
112af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 1), ==,
113af4c26e6SZhao Liu                      (2 << 2) | 1);
114af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 2), ==,
115af4c26e6SZhao Liu                      (2 << 2) | 2);
116af4c26e6SZhao Liu 
117af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 0), ==,
118af4c26e6SZhao Liu                      (5 << 2) | 0);
119af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 1), ==,
120af4c26e6SZhao Liu                      (5 << 2) | 1);
121af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 2), ==,
122af4c26e6SZhao Liu                      (5 << 2) | 2);
123af4c26e6SZhao Liu 
124af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
125af4c26e6SZhao Liu                      1 * 6 * 3 + 0 * 3 + 0), ==, (1 << 5));
126af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
127af4c26e6SZhao Liu                      1 * 6 * 3 + 1 * 3 + 1), ==, (1 << 5) | (1 << 2) | 1);
128af4c26e6SZhao Liu     g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
129af4c26e6SZhao Liu                      3 * 6 * 3 + 5 * 3 + 2), ==, (3 << 5) | (5 << 2) | 2);
130af4c26e6SZhao Liu }
131af4c26e6SZhao Liu 
132af4c26e6SZhao Liu int main(int argc, char **argv)
133af4c26e6SZhao Liu {
134af4c26e6SZhao Liu     g_test_init(&argc, &argv, NULL);
135af4c26e6SZhao Liu 
136af4c26e6SZhao Liu     g_test_add_func("/cpuid/topology/basic", test_topo_bits);
137af4c26e6SZhao Liu 
138af4c26e6SZhao Liu     g_test_run();
139af4c26e6SZhao Liu 
140af4c26e6SZhao Liu     return 0;
141af4c26e6SZhao Liu }
142