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 48*dcf08bc6SBabu Moger typedef struct X86CPUTopoIDs { 49ed256144SChen Fan unsigned pkg_id; 50176d2cdaSLike Xu unsigned die_id; 51ed256144SChen Fan unsigned core_id; 52ed256144SChen Fan unsigned smt_id; 53*dcf08bc6SBabu Moger } X86CPUTopoIDs; 54ed256144SChen Fan 55247c9de1SEduardo Habkost /* Return the bit width needed for 'count' IDs 56247c9de1SEduardo Habkost */ 57247c9de1SEduardo Habkost static unsigned apicid_bitwidth_for_count(unsigned count) 58247c9de1SEduardo Habkost { 59247c9de1SEduardo Habkost g_assert(count >= 1); 6014e53426SRichard Henderson count -= 1; 6114e53426SRichard Henderson return count ? 32 - clz32(count) : 0; 62247c9de1SEduardo Habkost } 63247c9de1SEduardo Habkost 64247c9de1SEduardo Habkost /* Bit width of the SMT_ID (thread ID) field on the APIC ID 65247c9de1SEduardo Habkost */ 66d65af288SLike Xu static inline unsigned apicid_smt_width(unsigned nr_dies, 67d65af288SLike Xu unsigned nr_cores, 68d65af288SLike Xu unsigned nr_threads) 69247c9de1SEduardo Habkost { 70247c9de1SEduardo Habkost return apicid_bitwidth_for_count(nr_threads); 71247c9de1SEduardo Habkost } 72247c9de1SEduardo Habkost 73247c9de1SEduardo Habkost /* Bit width of the Core_ID field 74247c9de1SEduardo Habkost */ 75d65af288SLike Xu static inline unsigned apicid_core_width(unsigned nr_dies, 76d65af288SLike Xu unsigned nr_cores, 77d65af288SLike Xu unsigned nr_threads) 78247c9de1SEduardo Habkost { 79247c9de1SEduardo Habkost return apicid_bitwidth_for_count(nr_cores); 80247c9de1SEduardo Habkost } 81247c9de1SEduardo Habkost 82d65af288SLike Xu /* Bit width of the Die_ID field */ 83d65af288SLike Xu static inline unsigned apicid_die_width(unsigned nr_dies, 84d65af288SLike Xu unsigned nr_cores, 85247c9de1SEduardo Habkost unsigned nr_threads) 86247c9de1SEduardo Habkost { 87d65af288SLike Xu return apicid_bitwidth_for_count(nr_dies); 88d65af288SLike Xu } 89d65af288SLike Xu 90d65af288SLike Xu /* Bit offset of the Core_ID field 91d65af288SLike Xu */ 92d65af288SLike Xu static inline unsigned apicid_core_offset(unsigned nr_dies, 93d65af288SLike Xu unsigned nr_cores, 94d65af288SLike Xu unsigned nr_threads) 95d65af288SLike Xu { 96d65af288SLike Xu return apicid_smt_width(nr_dies, nr_cores, nr_threads); 97d65af288SLike Xu } 98d65af288SLike Xu 99d65af288SLike Xu /* Bit offset of the Die_ID field */ 100d65af288SLike Xu static inline unsigned apicid_die_offset(unsigned nr_dies, 101d65af288SLike Xu unsigned nr_cores, 102d65af288SLike Xu unsigned nr_threads) 103d65af288SLike Xu { 104d65af288SLike Xu return apicid_core_offset(nr_dies, nr_cores, nr_threads) + 105d65af288SLike Xu apicid_core_width(nr_dies, nr_cores, nr_threads); 106247c9de1SEduardo Habkost } 107247c9de1SEduardo Habkost 108247c9de1SEduardo Habkost /* Bit offset of the Pkg_ID (socket ID) field 109247c9de1SEduardo Habkost */ 110d65af288SLike Xu static inline unsigned apicid_pkg_offset(unsigned nr_dies, 111d65af288SLike Xu unsigned nr_cores, 112d65af288SLike Xu unsigned nr_threads) 113247c9de1SEduardo Habkost { 114d65af288SLike Xu return apicid_die_offset(nr_dies, nr_cores, nr_threads) + 115d65af288SLike Xu apicid_die_width(nr_dies, nr_cores, nr_threads); 116247c9de1SEduardo Habkost } 117247c9de1SEduardo Habkost 118247c9de1SEduardo Habkost /* Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID 119247c9de1SEduardo Habkost * 120247c9de1SEduardo Habkost * The caller must make sure core_id < nr_cores and smt_id < nr_threads. 121247c9de1SEduardo Habkost */ 122d65af288SLike Xu static inline apic_id_t apicid_from_topo_ids(unsigned nr_dies, 123d65af288SLike Xu unsigned nr_cores, 124247c9de1SEduardo Habkost unsigned nr_threads, 125*dcf08bc6SBabu Moger const X86CPUTopoIDs *topo_ids) 126247c9de1SEduardo Habkost { 127*dcf08bc6SBabu Moger return (topo_ids->pkg_id << 128*dcf08bc6SBabu Moger apicid_pkg_offset(nr_dies, nr_cores, nr_threads)) | 129*dcf08bc6SBabu Moger (topo_ids->die_id << 130*dcf08bc6SBabu Moger apicid_die_offset(nr_dies, nr_cores, nr_threads)) | 131*dcf08bc6SBabu Moger (topo_ids->core_id << 132*dcf08bc6SBabu Moger apicid_core_offset(nr_dies, nr_cores, nr_threads)) | 133*dcf08bc6SBabu Moger topo_ids->smt_id; 134247c9de1SEduardo Habkost } 135247c9de1SEduardo Habkost 136247c9de1SEduardo Habkost /* Calculate thread/core/package IDs for a specific topology, 137247c9de1SEduardo Habkost * based on (contiguous) CPU index 138247c9de1SEduardo Habkost */ 139d65af288SLike Xu static inline void x86_topo_ids_from_idx(unsigned nr_dies, 140d65af288SLike Xu unsigned nr_cores, 141247c9de1SEduardo Habkost unsigned nr_threads, 142247c9de1SEduardo Habkost unsigned cpu_index, 143*dcf08bc6SBabu Moger X86CPUTopoIDs *topo_ids) 144247c9de1SEduardo Habkost { 145*dcf08bc6SBabu Moger topo_ids->pkg_id = cpu_index / (nr_dies * nr_cores * nr_threads); 146*dcf08bc6SBabu Moger topo_ids->die_id = cpu_index / (nr_cores * nr_threads) % nr_dies; 147*dcf08bc6SBabu Moger topo_ids->core_id = cpu_index / nr_threads % nr_cores; 148*dcf08bc6SBabu Moger topo_ids->smt_id = cpu_index % nr_threads; 149247c9de1SEduardo Habkost } 150247c9de1SEduardo Habkost 1519f3aab58SIgor Mammedov /* Calculate thread/core/package IDs for a specific topology, 1529f3aab58SIgor Mammedov * based on APIC ID 1539f3aab58SIgor Mammedov */ 1549f3aab58SIgor Mammedov static inline void x86_topo_ids_from_apicid(apic_id_t apicid, 155d65af288SLike Xu unsigned nr_dies, 1569f3aab58SIgor Mammedov unsigned nr_cores, 1579f3aab58SIgor Mammedov unsigned nr_threads, 158*dcf08bc6SBabu Moger X86CPUTopoIDs *topo_ids) 1599f3aab58SIgor Mammedov { 160*dcf08bc6SBabu Moger topo_ids->smt_id = apicid & 161d65af288SLike Xu ~(0xFFFFFFFFUL << apicid_smt_width(nr_dies, nr_cores, nr_threads)); 162*dcf08bc6SBabu Moger topo_ids->core_id = 163d65af288SLike Xu (apicid >> apicid_core_offset(nr_dies, nr_cores, nr_threads)) & 164d65af288SLike Xu ~(0xFFFFFFFFUL << apicid_core_width(nr_dies, nr_cores, nr_threads)); 165*dcf08bc6SBabu Moger topo_ids->die_id = 166d65af288SLike Xu (apicid >> apicid_die_offset(nr_dies, nr_cores, nr_threads)) & 167d65af288SLike Xu ~(0xFFFFFFFFUL << apicid_die_width(nr_dies, nr_cores, nr_threads)); 168*dcf08bc6SBabu Moger topo_ids->pkg_id = 169*dcf08bc6SBabu Moger apicid >> apicid_pkg_offset(nr_dies, nr_cores, nr_threads); 1709f3aab58SIgor Mammedov } 1719f3aab58SIgor Mammedov 172247c9de1SEduardo Habkost /* Make APIC ID for the CPU 'cpu_index' 173247c9de1SEduardo Habkost * 174247c9de1SEduardo Habkost * 'cpu_index' is a sequential, contiguous ID for the CPU. 175247c9de1SEduardo Habkost */ 176d65af288SLike Xu static inline apic_id_t x86_apicid_from_cpu_idx(unsigned nr_dies, 177d65af288SLike Xu unsigned nr_cores, 178247c9de1SEduardo Habkost unsigned nr_threads, 179247c9de1SEduardo Habkost unsigned cpu_index) 180247c9de1SEduardo Habkost { 181*dcf08bc6SBabu Moger X86CPUTopoIDs topo_ids; 182*dcf08bc6SBabu Moger x86_topo_ids_from_idx(nr_dies, nr_cores, nr_threads, cpu_index, &topo_ids); 183*dcf08bc6SBabu Moger return apicid_from_topo_ids(nr_dies, nr_cores, nr_threads, &topo_ids); 184247c9de1SEduardo Habkost } 185247c9de1SEduardo Habkost 186869b7649SEduardo Habkost #endif /* HW_I386_TOPOLOGY_H */ 187