xref: /qemu/include/hw/i386/topology.h (revision 3c6712eca07255803b61ca3d632f61a65c078c36)
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 
27247c9de1SEduardo Habkost /* This file implements the APIC-ID-based CPU topology enumeration logic,
28247c9de1SEduardo Habkost  * documented at the following document:
29247c9de1SEduardo Habkost  *   Intel® 64 Architecture Processor Topology Enumeration
30247c9de1SEduardo Habkost  *   http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/
31247c9de1SEduardo Habkost  *
32247c9de1SEduardo Habkost  * This code should be compatible with AMD's "Extended Method" described at:
33247c9de1SEduardo Habkost  *   AMD CPUID Specification (Publication #25481)
34247c9de1SEduardo Habkost  *   Section 3: Multiple Core Calcuation
35247c9de1SEduardo Habkost  * as long as:
36247c9de1SEduardo Habkost  *  nr_threads is set to 1;
37247c9de1SEduardo Habkost  *  OFFSET_IDX is assumed to be 0;
38247c9de1SEduardo Habkost  *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().
39247c9de1SEduardo Habkost  */
40247c9de1SEduardo Habkost 
41247c9de1SEduardo Habkost 
42247c9de1SEduardo Habkost #include "qemu/bitops.h"
43247c9de1SEduardo Habkost 
44247c9de1SEduardo Habkost /* APIC IDs can be 32-bit, but beware: APIC IDs > 255 require x2APIC support
45247c9de1SEduardo Habkost  */
46247c9de1SEduardo Habkost typedef uint32_t apic_id_t;
47247c9de1SEduardo Habkost 
48dcf08bc6SBabu Moger typedef struct X86CPUTopoIDs {
49ed256144SChen Fan     unsigned pkg_id;
50176d2cdaSLike Xu     unsigned die_id;
51ed256144SChen Fan     unsigned core_id;
52ed256144SChen Fan     unsigned smt_id;
53dcf08bc6SBabu Moger } X86CPUTopoIDs;
54ed256144SChen Fan 
5553a5e7bdSBabu Moger typedef struct X86CPUTopoInfo {
56c24a41bbSBabu Moger     unsigned nodes_per_pkg;
5753a5e7bdSBabu Moger     unsigned dies_per_pkg;
5853a5e7bdSBabu Moger     unsigned cores_per_die;
5953a5e7bdSBabu Moger     unsigned threads_per_core;
6053a5e7bdSBabu Moger } X86CPUTopoInfo;
6153a5e7bdSBabu Moger 
62247c9de1SEduardo Habkost /* Return the bit width needed for 'count' IDs
63247c9de1SEduardo Habkost  */
64247c9de1SEduardo Habkost static unsigned apicid_bitwidth_for_count(unsigned count)
65247c9de1SEduardo Habkost {
66247c9de1SEduardo Habkost     g_assert(count >= 1);
6714e53426SRichard Henderson     count -= 1;
6814e53426SRichard Henderson     return count ? 32 - clz32(count) : 0;
69247c9de1SEduardo Habkost }
70247c9de1SEduardo Habkost 
71247c9de1SEduardo Habkost /* Bit width of the SMT_ID (thread ID) field on the APIC ID
72247c9de1SEduardo Habkost  */
73f20dec0bSBabu Moger static inline unsigned apicid_smt_width(X86CPUTopoInfo *topo_info)
74247c9de1SEduardo Habkost {
75f20dec0bSBabu Moger     return apicid_bitwidth_for_count(topo_info->threads_per_core);
76247c9de1SEduardo Habkost }
77247c9de1SEduardo Habkost 
78247c9de1SEduardo Habkost /* Bit width of the Core_ID field
79247c9de1SEduardo Habkost  */
80f20dec0bSBabu Moger static inline unsigned apicid_core_width(X86CPUTopoInfo *topo_info)
81247c9de1SEduardo Habkost {
82f20dec0bSBabu Moger     return apicid_bitwidth_for_count(topo_info->cores_per_die);
83247c9de1SEduardo Habkost }
84247c9de1SEduardo Habkost 
85d65af288SLike Xu /* Bit width of the Die_ID field */
86f20dec0bSBabu Moger static inline unsigned apicid_die_width(X86CPUTopoInfo *topo_info)
87247c9de1SEduardo Habkost {
88f20dec0bSBabu Moger     return apicid_bitwidth_for_count(topo_info->dies_per_pkg);
89d65af288SLike Xu }
90d65af288SLike Xu 
91d65af288SLike Xu /* Bit offset of the Core_ID field
92d65af288SLike Xu  */
93f20dec0bSBabu Moger static inline unsigned apicid_core_offset(X86CPUTopoInfo *topo_info)
94d65af288SLike Xu {
95f20dec0bSBabu Moger     return apicid_smt_width(topo_info);
96d65af288SLike Xu }
97d65af288SLike Xu 
98d65af288SLike Xu /* Bit offset of the Die_ID field */
99f20dec0bSBabu Moger static inline unsigned apicid_die_offset(X86CPUTopoInfo *topo_info)
100d65af288SLike Xu {
101f20dec0bSBabu Moger     return apicid_core_offset(topo_info) + apicid_core_width(topo_info);
102247c9de1SEduardo Habkost }
103247c9de1SEduardo Habkost 
104247c9de1SEduardo Habkost /* Bit offset of the Pkg_ID (socket ID) field
105247c9de1SEduardo Habkost  */
106f20dec0bSBabu Moger static inline unsigned apicid_pkg_offset(X86CPUTopoInfo *topo_info)
107247c9de1SEduardo Habkost {
108f20dec0bSBabu Moger     return apicid_die_offset(topo_info) + apicid_die_width(topo_info);
109247c9de1SEduardo Habkost }
110247c9de1SEduardo Habkost 
111247c9de1SEduardo Habkost /* Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID
112247c9de1SEduardo Habkost  *
113247c9de1SEduardo Habkost  * The caller must make sure core_id < nr_cores and smt_id < nr_threads.
114247c9de1SEduardo Habkost  */
115*3c6712ecSBabu Moger static inline apic_id_t x86_apicid_from_topo_ids(X86CPUTopoInfo *topo_info,
116dcf08bc6SBabu Moger                                                  const X86CPUTopoIDs *topo_ids)
117247c9de1SEduardo Habkost {
118f20dec0bSBabu Moger     return (topo_ids->pkg_id  << apicid_pkg_offset(topo_info)) |
119f20dec0bSBabu Moger            (topo_ids->die_id  << apicid_die_offset(topo_info)) |
120f20dec0bSBabu Moger            (topo_ids->core_id << apicid_core_offset(topo_info)) |
121dcf08bc6SBabu Moger            topo_ids->smt_id;
122247c9de1SEduardo Habkost }
123247c9de1SEduardo Habkost 
124247c9de1SEduardo Habkost /* Calculate thread/core/package IDs for a specific topology,
125247c9de1SEduardo Habkost  * based on (contiguous) CPU index
126247c9de1SEduardo Habkost  */
12753a5e7bdSBabu Moger static inline void x86_topo_ids_from_idx(X86CPUTopoInfo *topo_info,
128247c9de1SEduardo Habkost                                          unsigned cpu_index,
129dcf08bc6SBabu Moger                                          X86CPUTopoIDs *topo_ids)
130247c9de1SEduardo Habkost {
13153a5e7bdSBabu Moger     unsigned nr_dies = topo_info->dies_per_pkg;
13253a5e7bdSBabu Moger     unsigned nr_cores = topo_info->cores_per_die;
13353a5e7bdSBabu Moger     unsigned nr_threads = topo_info->threads_per_core;
13453a5e7bdSBabu Moger 
135dcf08bc6SBabu Moger     topo_ids->pkg_id = cpu_index / (nr_dies * nr_cores * nr_threads);
136dcf08bc6SBabu Moger     topo_ids->die_id = cpu_index / (nr_cores * nr_threads) % nr_dies;
137dcf08bc6SBabu Moger     topo_ids->core_id = cpu_index / nr_threads % nr_cores;
138dcf08bc6SBabu Moger     topo_ids->smt_id = cpu_index % nr_threads;
139247c9de1SEduardo Habkost }
140247c9de1SEduardo Habkost 
1419f3aab58SIgor Mammedov /* Calculate thread/core/package IDs for a specific topology,
1429f3aab58SIgor Mammedov  * based on APIC ID
1439f3aab58SIgor Mammedov  */
1449f3aab58SIgor Mammedov static inline void x86_topo_ids_from_apicid(apic_id_t apicid,
14553a5e7bdSBabu Moger                                             X86CPUTopoInfo *topo_info,
146dcf08bc6SBabu Moger                                             X86CPUTopoIDs *topo_ids)
1479f3aab58SIgor Mammedov {
148dcf08bc6SBabu Moger     topo_ids->smt_id = apicid &
149f20dec0bSBabu Moger             ~(0xFFFFFFFFUL << apicid_smt_width(topo_info));
150dcf08bc6SBabu Moger     topo_ids->core_id =
151f20dec0bSBabu Moger             (apicid >> apicid_core_offset(topo_info)) &
152f20dec0bSBabu Moger             ~(0xFFFFFFFFUL << apicid_core_width(topo_info));
153dcf08bc6SBabu Moger     topo_ids->die_id =
154f20dec0bSBabu Moger             (apicid >> apicid_die_offset(topo_info)) &
155f20dec0bSBabu Moger             ~(0xFFFFFFFFUL << apicid_die_width(topo_info));
156f20dec0bSBabu Moger     topo_ids->pkg_id = apicid >> apicid_pkg_offset(topo_info);
1579f3aab58SIgor Mammedov }
1589f3aab58SIgor Mammedov 
159247c9de1SEduardo Habkost /* Make APIC ID for the CPU 'cpu_index'
160247c9de1SEduardo Habkost  *
161247c9de1SEduardo Habkost  * 'cpu_index' is a sequential, contiguous ID for the CPU.
162247c9de1SEduardo Habkost  */
16353a5e7bdSBabu Moger static inline apic_id_t x86_apicid_from_cpu_idx(X86CPUTopoInfo *topo_info,
164247c9de1SEduardo Habkost                                                 unsigned cpu_index)
165247c9de1SEduardo Habkost {
166dcf08bc6SBabu Moger     X86CPUTopoIDs topo_ids;
16753a5e7bdSBabu Moger     x86_topo_ids_from_idx(topo_info, cpu_index, &topo_ids);
168*3c6712ecSBabu Moger     return x86_apicid_from_topo_ids(topo_info, &topo_ids);
169247c9de1SEduardo Habkost }
170247c9de1SEduardo Habkost 
171869b7649SEduardo Habkost #endif /* HW_I386_TOPOLOGY_H */
172