1247c9de1SEduardo Habkost /* 2247c9de1SEduardo Habkost * x86 CPU topology data structures and 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 */ 24869b7649SEduardo Habkost #ifndef HW_I386_TOPOLOGY_H 25869b7649SEduardo Habkost #define HW_I386_TOPOLOGY_H 26247c9de1SEduardo Habkost 275f0d69b5SZhao Liu /* 285f0d69b5SZhao Liu * This file implements the APIC-ID-based CPU topology enumeration logic, 29247c9de1SEduardo Habkost * documented at the following document: 30247c9de1SEduardo Habkost * Intel® 64 Architecture Processor Topology Enumeration 31247c9de1SEduardo Habkost * http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/ 32247c9de1SEduardo Habkost * 33247c9de1SEduardo Habkost * This code should be compatible with AMD's "Extended Method" described at: 34247c9de1SEduardo Habkost * AMD CPUID Specification (Publication #25481) 35bad5cfcdSMichael Tokarev * Section 3: Multiple Core Calculation 36247c9de1SEduardo Habkost * as long as: 37247c9de1SEduardo Habkost * nr_threads is set to 1; 38247c9de1SEduardo Habkost * OFFSET_IDX is assumed to be 0; 39247c9de1SEduardo Habkost * CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width(). 40247c9de1SEduardo Habkost */ 41247c9de1SEduardo Habkost 42247c9de1SEduardo Habkost 43247c9de1SEduardo Habkost #include "qemu/bitops.h" 44247c9de1SEduardo Habkost 455f0d69b5SZhao Liu /* 465f0d69b5SZhao Liu * APIC IDs can be 32-bit, but beware: APIC IDs > 255 require x2APIC support 47247c9de1SEduardo Habkost */ 48247c9de1SEduardo Habkost typedef uint32_t apic_id_t; 49247c9de1SEduardo Habkost 50dcf08bc6SBabu Moger typedef struct X86CPUTopoIDs { 51ed256144SChen Fan unsigned pkg_id; 52176d2cdaSLike Xu unsigned die_id; 53ed256144SChen Fan unsigned core_id; 54ed256144SChen Fan unsigned smt_id; 55dcf08bc6SBabu Moger } X86CPUTopoIDs; 56ed256144SChen Fan 5753a5e7bdSBabu Moger typedef struct X86CPUTopoInfo { 5853a5e7bdSBabu Moger unsigned dies_per_pkg; 5953a5e7bdSBabu Moger unsigned cores_per_die; 6053a5e7bdSBabu Moger unsigned threads_per_core; 6153a5e7bdSBabu Moger } X86CPUTopoInfo; 6253a5e7bdSBabu Moger 63*6ddeb0ecSZhao Liu /* 64*6ddeb0ecSZhao Liu * CPUTopoLevel is the general i386 topology hierarchical representation, 65*6ddeb0ecSZhao Liu * ordered by increasing hierarchical relationship. 66*6ddeb0ecSZhao Liu * Its enumeration value is not bound to the type value of Intel (CPUID[0x1F]) 67*6ddeb0ecSZhao Liu * or AMD (CPUID[0x80000026]). 68*6ddeb0ecSZhao Liu */ 69*6ddeb0ecSZhao Liu enum CPUTopoLevel { 70*6ddeb0ecSZhao Liu CPU_TOPO_LEVEL_INVALID, 71*6ddeb0ecSZhao Liu CPU_TOPO_LEVEL_SMT, 72*6ddeb0ecSZhao Liu CPU_TOPO_LEVEL_CORE, 73*6ddeb0ecSZhao Liu CPU_TOPO_LEVEL_DIE, 74*6ddeb0ecSZhao Liu CPU_TOPO_LEVEL_PACKAGE, 75*6ddeb0ecSZhao Liu CPU_TOPO_LEVEL_MAX, 76*6ddeb0ecSZhao Liu }; 77*6ddeb0ecSZhao Liu 785f0d69b5SZhao Liu /* Return the bit width needed for 'count' IDs */ 79247c9de1SEduardo Habkost static unsigned apicid_bitwidth_for_count(unsigned count) 80247c9de1SEduardo Habkost { 81247c9de1SEduardo Habkost g_assert(count >= 1); 8214e53426SRichard Henderson count -= 1; 8314e53426SRichard Henderson return count ? 32 - clz32(count) : 0; 84247c9de1SEduardo Habkost } 85247c9de1SEduardo Habkost 865f0d69b5SZhao Liu /* Bit width of the SMT_ID (thread ID) field on the APIC ID */ 87f20dec0bSBabu Moger static inline unsigned apicid_smt_width(X86CPUTopoInfo *topo_info) 88247c9de1SEduardo Habkost { 89f20dec0bSBabu Moger return apicid_bitwidth_for_count(topo_info->threads_per_core); 90247c9de1SEduardo Habkost } 91247c9de1SEduardo Habkost 925f0d69b5SZhao Liu /* Bit width of the Core_ID field */ 93f20dec0bSBabu Moger static inline unsigned apicid_core_width(X86CPUTopoInfo *topo_info) 94247c9de1SEduardo Habkost { 95f20dec0bSBabu Moger return apicid_bitwidth_for_count(topo_info->cores_per_die); 96247c9de1SEduardo Habkost } 97247c9de1SEduardo Habkost 98d65af288SLike Xu /* Bit width of the Die_ID field */ 99f20dec0bSBabu Moger static inline unsigned apicid_die_width(X86CPUTopoInfo *topo_info) 100247c9de1SEduardo Habkost { 101f20dec0bSBabu Moger return apicid_bitwidth_for_count(topo_info->dies_per_pkg); 102d65af288SLike Xu } 103d65af288SLike Xu 1045f0d69b5SZhao Liu /* Bit offset of the Core_ID field */ 105f20dec0bSBabu Moger static inline unsigned apicid_core_offset(X86CPUTopoInfo *topo_info) 106d65af288SLike Xu { 107f20dec0bSBabu Moger return apicid_smt_width(topo_info); 108d65af288SLike Xu } 109d65af288SLike Xu 110d65af288SLike Xu /* Bit offset of the Die_ID field */ 111f20dec0bSBabu Moger static inline unsigned apicid_die_offset(X86CPUTopoInfo *topo_info) 112d65af288SLike Xu { 113f20dec0bSBabu Moger return apicid_core_offset(topo_info) + apicid_core_width(topo_info); 114247c9de1SEduardo Habkost } 115247c9de1SEduardo Habkost 1165f0d69b5SZhao Liu /* Bit offset of the Pkg_ID (socket ID) field */ 117f20dec0bSBabu Moger static inline unsigned apicid_pkg_offset(X86CPUTopoInfo *topo_info) 118247c9de1SEduardo Habkost { 119f20dec0bSBabu Moger return apicid_die_offset(topo_info) + apicid_die_width(topo_info); 120247c9de1SEduardo Habkost } 121247c9de1SEduardo Habkost 1225f0d69b5SZhao Liu /* 1235f0d69b5SZhao Liu * Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID 124247c9de1SEduardo Habkost * 125247c9de1SEduardo Habkost * The caller must make sure core_id < nr_cores and smt_id < nr_threads. 126247c9de1SEduardo Habkost */ 1273c6712ecSBabu Moger static inline apic_id_t x86_apicid_from_topo_ids(X86CPUTopoInfo *topo_info, 128dcf08bc6SBabu Moger const X86CPUTopoIDs *topo_ids) 129247c9de1SEduardo Habkost { 130f20dec0bSBabu Moger return (topo_ids->pkg_id << apicid_pkg_offset(topo_info)) | 131f20dec0bSBabu Moger (topo_ids->die_id << apicid_die_offset(topo_info)) | 132f20dec0bSBabu Moger (topo_ids->core_id << apicid_core_offset(topo_info)) | 133dcf08bc6SBabu Moger topo_ids->smt_id; 134247c9de1SEduardo Habkost } 135247c9de1SEduardo Habkost 1365f0d69b5SZhao Liu /* 1375f0d69b5SZhao Liu * Calculate thread/core/package IDs for a specific topology, 138247c9de1SEduardo Habkost * based on (contiguous) CPU index 139247c9de1SEduardo Habkost */ 14053a5e7bdSBabu Moger static inline void x86_topo_ids_from_idx(X86CPUTopoInfo *topo_info, 141247c9de1SEduardo Habkost unsigned cpu_index, 142dcf08bc6SBabu Moger X86CPUTopoIDs *topo_ids) 143247c9de1SEduardo Habkost { 14453a5e7bdSBabu Moger unsigned nr_dies = topo_info->dies_per_pkg; 14553a5e7bdSBabu Moger unsigned nr_cores = topo_info->cores_per_die; 14653a5e7bdSBabu Moger unsigned nr_threads = topo_info->threads_per_core; 14753a5e7bdSBabu Moger 148dcf08bc6SBabu Moger topo_ids->pkg_id = cpu_index / (nr_dies * nr_cores * nr_threads); 149dcf08bc6SBabu Moger topo_ids->die_id = cpu_index / (nr_cores * nr_threads) % nr_dies; 150dcf08bc6SBabu Moger topo_ids->core_id = cpu_index / nr_threads % nr_cores; 151dcf08bc6SBabu Moger topo_ids->smt_id = cpu_index % nr_threads; 152247c9de1SEduardo Habkost } 153247c9de1SEduardo Habkost 1545f0d69b5SZhao Liu /* 1555f0d69b5SZhao Liu * Calculate thread/core/package IDs for a specific topology, 1569f3aab58SIgor Mammedov * based on APIC ID 1579f3aab58SIgor Mammedov */ 1589f3aab58SIgor Mammedov static inline void x86_topo_ids_from_apicid(apic_id_t apicid, 15953a5e7bdSBabu Moger X86CPUTopoInfo *topo_info, 160dcf08bc6SBabu Moger X86CPUTopoIDs *topo_ids) 1619f3aab58SIgor Mammedov { 162dcf08bc6SBabu Moger topo_ids->smt_id = apicid & 163f20dec0bSBabu Moger ~(0xFFFFFFFFUL << apicid_smt_width(topo_info)); 164dcf08bc6SBabu Moger topo_ids->core_id = 165f20dec0bSBabu Moger (apicid >> apicid_core_offset(topo_info)) & 166f20dec0bSBabu Moger ~(0xFFFFFFFFUL << apicid_core_width(topo_info)); 167dcf08bc6SBabu Moger topo_ids->die_id = 168f20dec0bSBabu Moger (apicid >> apicid_die_offset(topo_info)) & 169f20dec0bSBabu Moger ~(0xFFFFFFFFUL << apicid_die_width(topo_info)); 170f20dec0bSBabu Moger topo_ids->pkg_id = apicid >> apicid_pkg_offset(topo_info); 1719f3aab58SIgor Mammedov } 1729f3aab58SIgor Mammedov 1735f0d69b5SZhao Liu /* 1745f0d69b5SZhao Liu * Make APIC ID for the CPU 'cpu_index' 175247c9de1SEduardo Habkost * 176247c9de1SEduardo Habkost * 'cpu_index' is a sequential, contiguous ID for the CPU. 177247c9de1SEduardo Habkost */ 17853a5e7bdSBabu Moger static inline apic_id_t x86_apicid_from_cpu_idx(X86CPUTopoInfo *topo_info, 179247c9de1SEduardo Habkost unsigned cpu_index) 180247c9de1SEduardo Habkost { 181dcf08bc6SBabu Moger X86CPUTopoIDs topo_ids; 18253a5e7bdSBabu Moger x86_topo_ids_from_idx(topo_info, cpu_index, &topo_ids); 1833c6712ecSBabu Moger return x86_apicid_from_topo_ids(topo_info, &topo_ids); 184247c9de1SEduardo Habkost } 185247c9de1SEduardo Habkost 186*6ddeb0ecSZhao Liu /* 187*6ddeb0ecSZhao Liu * Check whether there's extended topology level (die)? 188*6ddeb0ecSZhao Liu */ 189*6ddeb0ecSZhao Liu static inline bool x86_has_extended_topo(unsigned long *topo_bitmap) 190*6ddeb0ecSZhao Liu { 191*6ddeb0ecSZhao Liu return test_bit(CPU_TOPO_LEVEL_DIE, topo_bitmap); 192*6ddeb0ecSZhao Liu } 193*6ddeb0ecSZhao Liu 194869b7649SEduardo Habkost #endif /* HW_I386_TOPOLOGY_H */ 195