]>
Commit | Line | Data |
---|---|---|
247c9de1 EH |
1 | /* |
2 | * Test code for x86 CPUID 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 | ||
681c28a3 | 25 | #include "qemu/osdep.h" |
247c9de1 | 26 | |
869b7649 | 27 | #include "hw/i386/topology.h" |
247c9de1 EH |
28 | |
29 | static void test_topo_bits(void) | |
30 | { | |
53a5e7bd BM |
31 | X86CPUTopoInfo topo_info = {0}; |
32 | ||
d65af288 LX |
33 | /* simple tests for 1 thread per core, 1 core per die, 1 die per package */ |
34 | g_assert_cmpuint(apicid_smt_width(1, 1, 1), ==, 0); | |
35 | g_assert_cmpuint(apicid_core_width(1, 1, 1), ==, 0); | |
36 | g_assert_cmpuint(apicid_die_width(1, 1, 1), ==, 0); | |
247c9de1 | 37 | |
53a5e7bd BM |
38 | topo_info = (X86CPUTopoInfo) {1, 1, 1}; |
39 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0); | |
40 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1); | |
41 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2); | |
42 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 3), ==, 3); | |
247c9de1 EH |
43 | |
44 | ||
45 | /* Test field width calculation for multiple values | |
46 | */ | |
d65af288 LX |
47 | g_assert_cmpuint(apicid_smt_width(1, 1, 2), ==, 1); |
48 | g_assert_cmpuint(apicid_smt_width(1, 1, 3), ==, 2); | |
49 | g_assert_cmpuint(apicid_smt_width(1, 1, 4), ==, 2); | |
247c9de1 | 50 | |
d65af288 LX |
51 | g_assert_cmpuint(apicid_smt_width(1, 1, 14), ==, 4); |
52 | g_assert_cmpuint(apicid_smt_width(1, 1, 15), ==, 4); | |
53 | g_assert_cmpuint(apicid_smt_width(1, 1, 16), ==, 4); | |
54 | g_assert_cmpuint(apicid_smt_width(1, 1, 17), ==, 5); | |
247c9de1 EH |
55 | |
56 | ||
d65af288 LX |
57 | g_assert_cmpuint(apicid_core_width(1, 30, 2), ==, 5); |
58 | g_assert_cmpuint(apicid_core_width(1, 31, 2), ==, 5); | |
59 | g_assert_cmpuint(apicid_core_width(1, 32, 2), ==, 5); | |
60 | g_assert_cmpuint(apicid_core_width(1, 33, 2), ==, 6); | |
247c9de1 | 61 | |
d65af288 LX |
62 | g_assert_cmpuint(apicid_die_width(1, 30, 2), ==, 0); |
63 | g_assert_cmpuint(apicid_die_width(2, 30, 2), ==, 1); | |
64 | g_assert_cmpuint(apicid_die_width(3, 30, 2), ==, 2); | |
65 | g_assert_cmpuint(apicid_die_width(4, 30, 2), ==, 2); | |
247c9de1 EH |
66 | |
67 | /* build a weird topology and see if IDs are calculated correctly | |
68 | */ | |
69 | ||
70 | /* This will use 2 bits for thread ID and 3 bits for core ID | |
71 | */ | |
d65af288 LX |
72 | g_assert_cmpuint(apicid_smt_width(1, 6, 3), ==, 2); |
73 | g_assert_cmpuint(apicid_core_offset(1, 6, 3), ==, 2); | |
74 | g_assert_cmpuint(apicid_die_offset(1, 6, 3), ==, 5); | |
75 | g_assert_cmpuint(apicid_pkg_offset(1, 6, 3), ==, 5); | |
247c9de1 | 76 | |
53a5e7bd BM |
77 | topo_info = (X86CPUTopoInfo) {1, 6, 3}; |
78 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 0), ==, 0); | |
79 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1), ==, 1); | |
80 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2), ==, 2); | |
247c9de1 | 81 | |
53a5e7bd BM |
82 | topo_info = (X86CPUTopoInfo) {1, 6, 3}; |
83 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 0), ==, | |
247c9de1 | 84 | (1 << 2) | 0); |
53a5e7bd | 85 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 1), ==, |
247c9de1 | 86 | (1 << 2) | 1); |
53a5e7bd | 87 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 1 * 3 + 2), ==, |
247c9de1 EH |
88 | (1 << 2) | 2); |
89 | ||
53a5e7bd | 90 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 0), ==, |
247c9de1 | 91 | (2 << 2) | 0); |
53a5e7bd | 92 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 1), ==, |
247c9de1 | 93 | (2 << 2) | 1); |
53a5e7bd | 94 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 2 * 3 + 2), ==, |
247c9de1 EH |
95 | (2 << 2) | 2); |
96 | ||
53a5e7bd | 97 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 0), ==, |
247c9de1 | 98 | (5 << 2) | 0); |
53a5e7bd | 99 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 1), ==, |
247c9de1 | 100 | (5 << 2) | 1); |
53a5e7bd | 101 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, 5 * 3 + 2), ==, |
247c9de1 EH |
102 | (5 << 2) | 2); |
103 | ||
53a5e7bd | 104 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, |
d65af288 | 105 | 1 * 6 * 3 + 0 * 3 + 0), ==, (1 << 5)); |
53a5e7bd | 106 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, |
d65af288 | 107 | 1 * 6 * 3 + 1 * 3 + 1), ==, (1 << 5) | (1 << 2) | 1); |
53a5e7bd | 108 | g_assert_cmpuint(x86_apicid_from_cpu_idx(&topo_info, |
d65af288 | 109 | 3 * 6 * 3 + 5 * 3 + 2), ==, (3 << 5) | (5 << 2) | 2); |
247c9de1 EH |
110 | } |
111 | ||
112 | int main(int argc, char **argv) | |
113 | { | |
114 | g_test_init(&argc, &argv, NULL); | |
115 | ||
116 | g_test_add_func("/cpuid/topology/basic", test_topo_bits); | |
117 | ||
118 | g_test_run(); | |
119 | ||
120 | return 0; | |
121 | } |