1247c9de1SEduardo Habkost /*
2af4c26e6SZhao Liu * Test code for x86 APIC ID and Topology functions
3247c9de1SEduardo Habkost *
4247c9de1SEduardo Habkost * Copyright (c) 2012 Red Hat Inc.
5247c9de1SEduardo Habkost *
6247c9de1SEduardo Habkost * Permission is hereby granted, free of charge, to any person obtaining a copy
7247c9de1SEduardo Habkost * of this software and associated documentation files (the "Software"), to deal
8247c9de1SEduardo Habkost * in the Software without restriction, including without limitation the rights
9247c9de1SEduardo Habkost * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10247c9de1SEduardo Habkost * copies of the Software, and to permit persons to whom the Software is
11247c9de1SEduardo Habkost * furnished to do so, subject to the following conditions:
12247c9de1SEduardo Habkost *
13247c9de1SEduardo Habkost * The above copyright notice and this permission notice shall be included in
14247c9de1SEduardo Habkost * all copies or substantial portions of the Software.
15247c9de1SEduardo Habkost *
16247c9de1SEduardo Habkost * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17247c9de1SEduardo Habkost * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18247c9de1SEduardo Habkost * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19247c9de1SEduardo Habkost * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20247c9de1SEduardo Habkost * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21247c9de1SEduardo Habkost * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22247c9de1SEduardo Habkost * THE SOFTWARE.
23247c9de1SEduardo Habkost */
24247c9de1SEduardo Habkost
25681c28a3SPeter Maydell #include "qemu/osdep.h"
26247c9de1SEduardo Habkost
27869b7649SEduardo Habkost #include "hw/i386/topology.h"
28247c9de1SEduardo Habkost
test_topo_bits(void)29247c9de1SEduardo Habkost static void test_topo_bits(void)
30247c9de1SEduardo Habkost {
3153a5e7bdSBabu Moger X86CPUTopoInfo topo_info = {0};
3253a5e7bdSBabu Moger
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};
38f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 0);
39f20dec0bSBabu Moger g_assert_cmpuint(apicid_core_width(&topo_info), ==, 0);
40*321d2599SZhuocheng Ding g_assert_cmpuint(apicid_module_width(&topo_info), ==, 0);
41f20dec0bSBabu Moger g_assert_cmpuint(apicid_die_width(&topo_info), ==, 0);
42247c9de1SEduardo Habkost
433568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 1};
4453a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0);
4553a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1);
4653a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2);
4753a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 3), ==, 3);
48247c9de1SEduardo Habkost
49247c9de1SEduardo Habkost
50247c9de1SEduardo Habkost /* Test field width calculation for multiple values
51247c9de1SEduardo Habkost */
523568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 2};
53f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 1);
543568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 3};
55f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
563568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 4};
57f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
58247c9de1SEduardo Habkost
593568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 14};
60f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
613568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 15};
62f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
633568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 16};
64f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 4);
653568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 1, 17};
66f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 5);
67247c9de1SEduardo Habkost
68247c9de1SEduardo Habkost
693568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 30, 2};
70f20dec0bSBabu Moger g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
713568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 31, 2};
72f20dec0bSBabu Moger g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
733568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 32, 2};
74f20dec0bSBabu Moger g_assert_cmpuint(apicid_core_width(&topo_info), ==, 5);
753568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 33, 2};
76f20dec0bSBabu Moger g_assert_cmpuint(apicid_core_width(&topo_info), ==, 6);
77247c9de1SEduardo Habkost
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};
88f20dec0bSBabu Moger g_assert_cmpuint(apicid_die_width(&topo_info), ==, 0);
89*321d2599SZhuocheng Ding topo_info = (X86CPUTopoInfo) {2, 6, 30, 2};
90f20dec0bSBabu Moger g_assert_cmpuint(apicid_die_width(&topo_info), ==, 1);
91*321d2599SZhuocheng Ding topo_info = (X86CPUTopoInfo) {3, 6, 30, 2};
92f20dec0bSBabu Moger g_assert_cmpuint(apicid_die_width(&topo_info), ==, 2);
93*321d2599SZhuocheng Ding topo_info = (X86CPUTopoInfo) {4, 6, 30, 2};
94f20dec0bSBabu Moger g_assert_cmpuint(apicid_die_width(&topo_info), ==, 2);
95247c9de1SEduardo Habkost
96247c9de1SEduardo Habkost /* build a weird topology and see if IDs are calculated correctly
97247c9de1SEduardo Habkost */
98247c9de1SEduardo Habkost
99247c9de1SEduardo Habkost /* This will use 2 bits for thread ID and 3 bits for core ID
100247c9de1SEduardo Habkost */
1013568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
102f20dec0bSBabu Moger g_assert_cmpuint(apicid_smt_width(&topo_info), ==, 2);
103f20dec0bSBabu Moger g_assert_cmpuint(apicid_core_offset(&topo_info), ==, 2);
104*321d2599SZhuocheng Ding g_assert_cmpuint(apicid_module_offset(&topo_info), ==, 5);
105f20dec0bSBabu Moger g_assert_cmpuint(apicid_die_offset(&topo_info), ==, 5);
106f20dec0bSBabu Moger g_assert_cmpuint(apicid_pkg_offset(&topo_info), ==, 5);
107247c9de1SEduardo Habkost
1083568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
10953a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0);
11053a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1);
11153a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2);
112247c9de1SEduardo Habkost
1133568adc9SZhao Liu topo_info = (X86CPUTopoInfo) {1, 1, 6, 3};
11453a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 0), ==,
115247c9de1SEduardo Habkost (1 << 2) | 0);
11653a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 1), ==,
117247c9de1SEduardo Habkost (1 << 2) | 1);
11853a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 2), ==,
119247c9de1SEduardo Habkost (1 << 2) | 2);
120247c9de1SEduardo Habkost
12153a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 0), ==,
122247c9de1SEduardo Habkost (2 << 2) | 0);
12353a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 1), ==,
124247c9de1SEduardo Habkost (2 << 2) | 1);
12553a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 2), ==,
126247c9de1SEduardo Habkost (2 << 2) | 2);
127247c9de1SEduardo Habkost
12853a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 0), ==,
129247c9de1SEduardo Habkost (5 << 2) | 0);
13053a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 1), ==,
131247c9de1SEduardo Habkost (5 << 2) | 1);
13253a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 2), ==,
133247c9de1SEduardo Habkost (5 << 2) | 2);
134247c9de1SEduardo Habkost
13553a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
136d65af288SLike Xu 1 * 6 * 3 + 0 * 3 + 0), ==, (1 << 5));
13753a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
138d65af288SLike Xu 1 * 6 * 3 + 1 * 3 + 1), ==, (1 << 5) | (1 << 2) | 1);
13953a5e7bdSBabu Moger g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info,
140d65af288SLike Xu 3 * 6 * 3 + 5 * 3 + 2), ==, (3 << 5) | (5 << 2) | 2);
141247c9de1SEduardo Habkost }
142247c9de1SEduardo Habkost
main(int argc,char ** argv)143247c9de1SEduardo Habkost int main(int argc, char **argv)
144247c9de1SEduardo Habkost {
145247c9de1SEduardo Habkost g_test_init(&argc, &argv, NULL);
146247c9de1SEduardo Habkost
147247c9de1SEduardo Habkost g_test_add_func("/cpuid/topology/basic", test_topo_bits);
148247c9de1SEduardo Habkost
149247c9de1SEduardo Habkost g_test_run();
150247c9de1SEduardo Habkost
151247c9de1SEduardo Habkost return 0;
152247c9de1SEduardo Habkost }
153