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