Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 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 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 26 | |
Eduardo Habkost | 869b764 | 2014-12-18 23:41:06 -0200 | [diff] [blame] | 27 | #include "hw/i386/topology.h" |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 28 | |
| 29 | static void test_topo_bits(void) |
| 30 | { |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 31 | /* simple tests for 1 thread per core, 1 core per die, 1 die per package */ |
| 32 | g_assert_cmpuint(apicid_smt_width(1, 1, 1), ==, 0); |
| 33 | g_assert_cmpuint(apicid_core_width(1, 1, 1), ==, 0); |
| 34 | g_assert_cmpuint(apicid_die_width(1, 1, 1), ==, 0); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 35 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 36 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 1, 0), ==, 0); |
| 37 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 1, 1), ==, 1); |
| 38 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 1, 2), ==, 2); |
| 39 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 1, 1, 3), ==, 3); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /* Test field width calculation for multiple values |
| 43 | */ |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 44 | g_assert_cmpuint(apicid_smt_width(1, 1, 2), ==, 1); |
| 45 | g_assert_cmpuint(apicid_smt_width(1, 1, 3), ==, 2); |
| 46 | g_assert_cmpuint(apicid_smt_width(1, 1, 4), ==, 2); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 47 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 48 | g_assert_cmpuint(apicid_smt_width(1, 1, 14), ==, 4); |
| 49 | g_assert_cmpuint(apicid_smt_width(1, 1, 15), ==, 4); |
| 50 | g_assert_cmpuint(apicid_smt_width(1, 1, 16), ==, 4); |
| 51 | g_assert_cmpuint(apicid_smt_width(1, 1, 17), ==, 5); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 52 | |
| 53 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 54 | g_assert_cmpuint(apicid_core_width(1, 30, 2), ==, 5); |
| 55 | g_assert_cmpuint(apicid_core_width(1, 31, 2), ==, 5); |
| 56 | g_assert_cmpuint(apicid_core_width(1, 32, 2), ==, 5); |
| 57 | g_assert_cmpuint(apicid_core_width(1, 33, 2), ==, 6); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 58 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 59 | g_assert_cmpuint(apicid_die_width(1, 30, 2), ==, 0); |
| 60 | g_assert_cmpuint(apicid_die_width(2, 30, 2), ==, 1); |
| 61 | g_assert_cmpuint(apicid_die_width(3, 30, 2), ==, 2); |
| 62 | g_assert_cmpuint(apicid_die_width(4, 30, 2), ==, 2); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 63 | |
| 64 | /* build a weird topology and see if IDs are calculated correctly |
| 65 | */ |
| 66 | |
| 67 | /* This will use 2 bits for thread ID and 3 bits for core ID |
| 68 | */ |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 69 | g_assert_cmpuint(apicid_smt_width(1, 6, 3), ==, 2); |
| 70 | g_assert_cmpuint(apicid_core_offset(1, 6, 3), ==, 2); |
| 71 | g_assert_cmpuint(apicid_die_offset(1, 6, 3), ==, 5); |
| 72 | g_assert_cmpuint(apicid_pkg_offset(1, 6, 3), ==, 5); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 73 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 74 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 0), ==, 0); |
| 75 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 1), ==, 1); |
| 76 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 2), ==, 2); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 77 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 78 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 1 * 3 + 0), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 79 | (1 << 2) | 0); |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 80 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 1 * 3 + 1), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 81 | (1 << 2) | 1); |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 82 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 1 * 3 + 2), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 83 | (1 << 2) | 2); |
| 84 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 85 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 2 * 3 + 0), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 86 | (2 << 2) | 0); |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 87 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 2 * 3 + 1), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 88 | (2 << 2) | 1); |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 89 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 2 * 3 + 2), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 90 | (2 << 2) | 2); |
| 91 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 92 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 5 * 3 + 0), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 93 | (5 << 2) | 0); |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 94 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 5 * 3 + 1), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 95 | (5 << 2) | 1); |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 96 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, 5 * 3 + 2), ==, |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 97 | (5 << 2) | 2); |
| 98 | |
Like Xu | d65af28 | 2019-06-12 16:40:59 +0800 | [diff] [blame] | 99 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, |
| 100 | 1 * 6 * 3 + 0 * 3 + 0), ==, (1 << 5)); |
| 101 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, |
| 102 | 1 * 6 * 3 + 1 * 3 + 1), ==, (1 << 5) | (1 << 2) | 1); |
| 103 | g_assert_cmpuint(x86_apicid_from_cpu_idx(1, 6, 3, |
| 104 | 3 * 6 * 3 + 5 * 3 + 2), ==, (3 << 5) | (5 << 2) | 2); |
Eduardo Habkost | 247c9de | 2013-01-23 15:58:27 -0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int main(int argc, char **argv) |
| 108 | { |
| 109 | g_test_init(&argc, &argv, NULL); |
| 110 | |
| 111 | g_test_add_func("/cpuid/topology/basic", test_topo_bits); |
| 112 | |
| 113 | g_test_run(); |
| 114 | |
| 115 | return 0; |
| 116 | } |