xref: /openbmc/qemu/include/hw/i386/topology.h (revision 63dc36944383f70f1c7a20f6104966d8560300fa)
1869b7649SEduardo Habkost /*
2869b7649SEduardo Habkost  *  x86 CPU topology data structures and functions
3869b7649SEduardo Habkost  *
4869b7649SEduardo Habkost  *  Copyright (c) 2012 Red Hat Inc.
5869b7649SEduardo Habkost  *
6869b7649SEduardo Habkost  * Permission is hereby granted, free of charge, to any person obtaining a copy
7869b7649SEduardo Habkost  * of this software and associated documentation files (the "Software"), to deal
8869b7649SEduardo Habkost  * in the Software without restriction, including without limitation the rights
9869b7649SEduardo Habkost  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10869b7649SEduardo Habkost  * copies of the Software, and to permit persons to whom the Software is
11869b7649SEduardo Habkost  * furnished to do so, subject to the following conditions:
12869b7649SEduardo Habkost  *
13869b7649SEduardo Habkost  * The above copyright notice and this permission notice shall be included in
14869b7649SEduardo Habkost  * all copies or substantial portions of the Software.
15869b7649SEduardo Habkost  *
16869b7649SEduardo Habkost  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17869b7649SEduardo Habkost  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18869b7649SEduardo Habkost  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19869b7649SEduardo Habkost  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20869b7649SEduardo Habkost  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21869b7649SEduardo Habkost  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22869b7649SEduardo Habkost  * THE SOFTWARE.
23869b7649SEduardo Habkost  */
24869b7649SEduardo Habkost #ifndef HW_I386_TOPOLOGY_H
25869b7649SEduardo Habkost #define HW_I386_TOPOLOGY_H
26869b7649SEduardo Habkost 
275f0d69b5SZhao Liu /*
285f0d69b5SZhao Liu  * This file implements the APIC-ID-based CPU topology enumeration logic,
29869b7649SEduardo Habkost  * documented at the following document:
30869b7649SEduardo Habkost  *   Intel® 64 Architecture Processor Topology Enumeration
31869b7649SEduardo Habkost  *   http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/
32869b7649SEduardo Habkost  *
33869b7649SEduardo Habkost  * This code should be compatible with AMD's "Extended Method" described at:
34869b7649SEduardo Habkost  *   AMD CPUID Specification (Publication #25481)
35bad5cfcdSMichael Tokarev  *   Section 3: Multiple Core Calculation
36869b7649SEduardo Habkost  * as long as:
37869b7649SEduardo Habkost  *  nr_threads is set to 1;
38869b7649SEduardo Habkost  *  OFFSET_IDX is assumed to be 0;
39869b7649SEduardo Habkost  *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().
40869b7649SEduardo Habkost  */
41869b7649SEduardo Habkost 
42*e823ebe7SZhao Liu #include "qapi/qapi-types-machine-common.h"
43869b7649SEduardo Habkost #include "qemu/bitops.h"
44869b7649SEduardo Habkost 
455f0d69b5SZhao Liu /*
465f0d69b5SZhao Liu  * APIC IDs can be 32-bit, but beware: APIC IDs > 255 require x2APIC support
47869b7649SEduardo Habkost  */
48869b7649SEduardo Habkost typedef uint32_t apic_id_t;
49869b7649SEduardo Habkost 
50dcf08bc6SBabu Moger typedef struct X86CPUTopoIDs {
51ed256144SChen Fan     unsigned pkg_id;
52176d2cdaSLike Xu     unsigned die_id;
53b17a26bcSZhao Liu     unsigned module_id;
54ed256144SChen Fan     unsigned core_id;
55ed256144SChen Fan     unsigned smt_id;
56dcf08bc6SBabu Moger } X86CPUTopoIDs;
57ed256144SChen Fan 
5853a5e7bdSBabu Moger typedef struct X86CPUTopoInfo {
5953a5e7bdSBabu Moger     unsigned dies_per_pkg;
603568adc9SZhao Liu     unsigned modules_per_die;
613568adc9SZhao Liu     unsigned cores_per_module;
6253a5e7bdSBabu Moger     unsigned threads_per_core;
6353a5e7bdSBabu Moger } X86CPUTopoInfo;
6453a5e7bdSBabu Moger 
65*e823ebe7SZhao Liu #define CPU_TOPOLOGY_LEVEL_INVALID CPU_TOPOLOGY_LEVEL__MAX
666ddeb0ecSZhao Liu 
675f0d69b5SZhao Liu /* Return the bit width needed for 'count' IDs */
apicid_bitwidth_for_count(unsigned count)68869b7649SEduardo Habkost static unsigned apicid_bitwidth_for_count(unsigned count)
69869b7649SEduardo Habkost {
70869b7649SEduardo Habkost     g_assert(count >= 1);
71869b7649SEduardo Habkost     count -= 1;
72869b7649SEduardo Habkost     return count ? 32 - clz32(count) : 0;
73869b7649SEduardo Habkost }
74869b7649SEduardo Habkost 
755f0d69b5SZhao Liu /* Bit width of the SMT_ID (thread ID) field on the APIC ID */
apicid_smt_width(X86CPUTopoInfo * topo_info)76f20dec0bSBabu Moger static inline unsigned apicid_smt_width(X86CPUTopoInfo *topo_info)
77869b7649SEduardo Habkost {
78f20dec0bSBabu Moger     return apicid_bitwidth_for_count(topo_info->threads_per_core);
79869b7649SEduardo Habkost }
80869b7649SEduardo Habkost 
815f0d69b5SZhao Liu /* Bit width of the Core_ID field */
apicid_core_width(X86CPUTopoInfo * topo_info)82f20dec0bSBabu Moger static inline unsigned apicid_core_width(X86CPUTopoInfo *topo_info)
83869b7649SEduardo Habkost {
843568adc9SZhao Liu     return apicid_bitwidth_for_count(topo_info->cores_per_module);
853568adc9SZhao Liu }
863568adc9SZhao Liu 
873568adc9SZhao Liu /* Bit width of the Module_ID field */
apicid_module_width(X86CPUTopoInfo * topo_info)883568adc9SZhao Liu static inline unsigned apicid_module_width(X86CPUTopoInfo *topo_info)
893568adc9SZhao Liu {
903568adc9SZhao Liu     return apicid_bitwidth_for_count(topo_info->modules_per_die);
91869b7649SEduardo Habkost }
92869b7649SEduardo Habkost 
93d65af288SLike Xu /* Bit width of the Die_ID field */
apicid_die_width(X86CPUTopoInfo * topo_info)94f20dec0bSBabu Moger static inline unsigned apicid_die_width(X86CPUTopoInfo *topo_info)
95869b7649SEduardo Habkost {
96f20dec0bSBabu Moger     return apicid_bitwidth_for_count(topo_info->dies_per_pkg);
97d65af288SLike Xu }
98d65af288SLike Xu 
995f0d69b5SZhao Liu /* Bit offset of the Core_ID field */
apicid_core_offset(X86CPUTopoInfo * topo_info)100f20dec0bSBabu Moger static inline unsigned apicid_core_offset(X86CPUTopoInfo *topo_info)
101d65af288SLike Xu {
102f20dec0bSBabu Moger     return apicid_smt_width(topo_info);
103d65af288SLike Xu }
104d65af288SLike Xu 
1053568adc9SZhao Liu /* Bit offset of the Module_ID field */
apicid_module_offset(X86CPUTopoInfo * topo_info)1063568adc9SZhao Liu static inline unsigned apicid_module_offset(X86CPUTopoInfo *topo_info)
1073568adc9SZhao Liu {
1083568adc9SZhao Liu     return apicid_core_offset(topo_info) + apicid_core_width(topo_info);
1093568adc9SZhao Liu }
1103568adc9SZhao Liu 
111d65af288SLike Xu /* Bit offset of the Die_ID field */
apicid_die_offset(X86CPUTopoInfo * topo_info)112f20dec0bSBabu Moger static inline unsigned apicid_die_offset(X86CPUTopoInfo *topo_info)
113d65af288SLike Xu {
1143568adc9SZhao Liu     return apicid_module_offset(topo_info) + apicid_module_width(topo_info);
115869b7649SEduardo Habkost }
116869b7649SEduardo Habkost 
1175f0d69b5SZhao Liu /* Bit offset of the Pkg_ID (socket ID) field */
apicid_pkg_offset(X86CPUTopoInfo * topo_info)118f20dec0bSBabu Moger static inline unsigned apicid_pkg_offset(X86CPUTopoInfo *topo_info)
119869b7649SEduardo Habkost {
120f20dec0bSBabu Moger     return apicid_die_offset(topo_info) + apicid_die_width(topo_info);
121869b7649SEduardo Habkost }
122869b7649SEduardo Habkost 
1235f0d69b5SZhao Liu /*
1245f0d69b5SZhao Liu  * Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID
125869b7649SEduardo Habkost  *
126869b7649SEduardo Habkost  * The caller must make sure core_id < nr_cores and smt_id < nr_threads.
127869b7649SEduardo Habkost  */
x86_apicid_from_topo_ids(X86CPUTopoInfo * topo_info,const X86CPUTopoIDs * topo_ids)1283c6712ecSBabu Moger static inline apic_id_t x86_apicid_from_topo_ids(X86CPUTopoInfo *topo_info,
129dcf08bc6SBabu Moger                                                  const X86CPUTopoIDs *topo_ids)
130869b7649SEduardo Habkost {
131f20dec0bSBabu Moger     return (topo_ids->pkg_id  << apicid_pkg_offset(topo_info)) |
132f20dec0bSBabu Moger            (topo_ids->die_id  << apicid_die_offset(topo_info)) |
133b17a26bcSZhao Liu            (topo_ids->module_id << apicid_module_offset(topo_info)) |
134f20dec0bSBabu Moger            (topo_ids->core_id << apicid_core_offset(topo_info)) |
135dcf08bc6SBabu Moger            topo_ids->smt_id;
136869b7649SEduardo Habkost }
137869b7649SEduardo Habkost 
1385f0d69b5SZhao Liu /*
1395f0d69b5SZhao Liu  * Calculate thread/core/package IDs for a specific topology,
140869b7649SEduardo Habkost  * based on (contiguous) CPU index
141869b7649SEduardo Habkost  */
x86_topo_ids_from_idx(X86CPUTopoInfo * topo_info,unsigned cpu_index,X86CPUTopoIDs * topo_ids)14253a5e7bdSBabu Moger static inline void x86_topo_ids_from_idx(X86CPUTopoInfo *topo_info,
143869b7649SEduardo Habkost                                          unsigned cpu_index,
144dcf08bc6SBabu Moger                                          X86CPUTopoIDs *topo_ids)
145869b7649SEduardo Habkost {
14653a5e7bdSBabu Moger     unsigned nr_dies = topo_info->dies_per_pkg;
147b17a26bcSZhao Liu     unsigned nr_modules = topo_info->modules_per_die;
148b17a26bcSZhao Liu     unsigned nr_cores = topo_info->cores_per_module;
14953a5e7bdSBabu Moger     unsigned nr_threads = topo_info->threads_per_core;
15053a5e7bdSBabu Moger 
151b17a26bcSZhao Liu     topo_ids->pkg_id = cpu_index / (nr_dies * nr_modules *
152b17a26bcSZhao Liu                        nr_cores * nr_threads);
153b17a26bcSZhao Liu     topo_ids->die_id = cpu_index / (nr_modules * nr_cores *
154b17a26bcSZhao Liu                        nr_threads) % nr_dies;
155b17a26bcSZhao Liu     topo_ids->module_id = cpu_index / (nr_cores * nr_threads) %
156b17a26bcSZhao Liu                           nr_modules;
157dcf08bc6SBabu Moger     topo_ids->core_id = cpu_index / nr_threads % nr_cores;
158dcf08bc6SBabu Moger     topo_ids->smt_id = cpu_index % nr_threads;
159869b7649SEduardo Habkost }
160869b7649SEduardo Habkost 
1615f0d69b5SZhao Liu /*
1625f0d69b5SZhao Liu  * Calculate thread/core/package IDs for a specific topology,
1639f3aab58SIgor Mammedov  * based on APIC ID
1649f3aab58SIgor Mammedov  */
x86_topo_ids_from_apicid(apic_id_t apicid,X86CPUTopoInfo * topo_info,X86CPUTopoIDs * topo_ids)1659f3aab58SIgor Mammedov static inline void x86_topo_ids_from_apicid(apic_id_t apicid,
16653a5e7bdSBabu Moger                                             X86CPUTopoInfo *topo_info,
167dcf08bc6SBabu Moger                                             X86CPUTopoIDs *topo_ids)
1689f3aab58SIgor Mammedov {
169dcf08bc6SBabu Moger     topo_ids->smt_id = apicid &
170f20dec0bSBabu Moger             ~(0xFFFFFFFFUL << apicid_smt_width(topo_info));
171dcf08bc6SBabu Moger     topo_ids->core_id =
172f20dec0bSBabu Moger             (apicid >> apicid_core_offset(topo_info)) &
173f20dec0bSBabu Moger             ~(0xFFFFFFFFUL << apicid_core_width(topo_info));
174b17a26bcSZhao Liu     topo_ids->module_id =
175b17a26bcSZhao Liu             (apicid >> apicid_module_offset(topo_info)) &
176b17a26bcSZhao Liu             ~(0xFFFFFFFFUL << apicid_module_width(topo_info));
177dcf08bc6SBabu Moger     topo_ids->die_id =
178f20dec0bSBabu Moger             (apicid >> apicid_die_offset(topo_info)) &
179f20dec0bSBabu Moger             ~(0xFFFFFFFFUL << apicid_die_width(topo_info));
180f20dec0bSBabu Moger     topo_ids->pkg_id = apicid >> apicid_pkg_offset(topo_info);
1819f3aab58SIgor Mammedov }
1829f3aab58SIgor Mammedov 
1835f0d69b5SZhao Liu /*
1845f0d69b5SZhao Liu  * Make APIC ID for the CPU 'cpu_index'
185869b7649SEduardo Habkost  *
186869b7649SEduardo Habkost  * 'cpu_index' is a sequential, contiguous ID for the CPU.
187869b7649SEduardo Habkost  */
x86_apicid_from_cpu_idx(X86CPUTopoInfo * topo_info,unsigned cpu_index)18853a5e7bdSBabu Moger static inline apic_id_t x86_apicid_from_cpu_idx(X86CPUTopoInfo *topo_info,
189869b7649SEduardo Habkost                                                 unsigned cpu_index)
190869b7649SEduardo Habkost {
191dcf08bc6SBabu Moger     X86CPUTopoIDs topo_ids;
19253a5e7bdSBabu Moger     x86_topo_ids_from_idx(topo_info, cpu_index, &topo_ids);
1933c6712ecSBabu Moger     return x86_apicid_from_topo_ids(topo_info, &topo_ids);
194869b7649SEduardo Habkost }
195869b7649SEduardo Habkost 
1966ddeb0ecSZhao Liu /*
1975304873aSZhao Liu  * Check whether there's extended topology level (module or die)?
1986ddeb0ecSZhao Liu  */
x86_has_extended_topo(unsigned long * topo_bitmap)1996ddeb0ecSZhao Liu static inline bool x86_has_extended_topo(unsigned long *topo_bitmap)
2006ddeb0ecSZhao Liu {
201*e823ebe7SZhao Liu     return test_bit(CPU_TOPOLOGY_LEVEL_MODULE, topo_bitmap) ||
202*e823ebe7SZhao Liu            test_bit(CPU_TOPOLOGY_LEVEL_DIE, topo_bitmap);
2036ddeb0ecSZhao Liu }
2046ddeb0ecSZhao Liu 
205869b7649SEduardo Habkost #endif /* HW_I386_TOPOLOGY_H */
206