#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cpu_topology.c,v 1.21 2022/10/12 10:26:09 msaitoh Exp $");
#include "acpica.h"
#include <sys/param.h>
#include <sys/bitops.h>
#include <sys/cpu.h>
#include <machine/specialreg.h>
#include <dev/acpi/acpi_srat.h>
#include <x86/cpufunc.h>
#include <x86/cputypes.h>
#include <x86/cpuvar.h>
static uint32_t
x86_cpu_get_numa_node(uint32_t apic_id)
{
#if NACPICA > 0
uint32_t i, j, nn, nc;
struct acpisrat_cpu c;
nn = acpisrat_nodes();
for (i = 0; i < nn; i++) {
nc = acpisrat_node_cpus(i);
for (j = 0; j < nc; j++) {
acpisrat_cpu(i, j, &c);
if (c.apicid == apic_id) {
return c.nodeid;
}
}
}
#endif
return 0;
}
void
x86_cpu_topology(struct cpu_info *ci)
{
u_int lp_max;
u_int core_max;
int n, cpu_family, apic_id, smt_bits, core_bits = 0;
uint32_t descs[4];
u_int package_id, core_id, smt_id, numa_id;
apic_id = ci->ci_initapicid;
cpu_family = CPUID_TO_FAMILY(ci->ci_signature);
package_id = apic_id;
core_id = 0;
smt_id = 0;
numa_id = x86_cpu_get_numa_node(apic_id);
switch (cpu_vendor) {
case CPUVENDOR_INTEL:
if (cpu_family < 6) {
cpu_topology_set(ci, package_id, core_id, smt_id,
numa_id);
return;
}
break;
case CPUVENDOR_AMD:
if (cpu_family < 0xf) {
cpu_topology_set(ci, package_id, core_id, smt_id,
numa_id);
return;
}
break;
default:
return;
}
if ((ci->ci_feat_val[0] & CPUID_HTT) != 0) {
x86_cpuid(1, descs);
lp_max = __SHIFTOUT(descs[1], CPUID_HTT_CORES);
} else {
lp_max = 1;
}
switch (cpu_vendor) {
case CPUVENDOR_INTEL:
if (ci->ci_max_cpuid >= 4) {
x86_cpuid2(4, 0, descs);
core_max = __SHIFTOUT(descs[0], CPUID_DCP_CORE_P_PKG)
+ 1;
} else {
core_max = 1;
}
break;
case CPUVENDOR_AMD:
if ((ci->ci_feat_val[0] & CPUID_HTT) == 0) {
core_max = 1;
break;
}
if (cpu_family < 0x10 || ci->ci_max_ext_cpuid < 0x80000008) {
core_max = lp_max;
break;
}
x86_cpuid(0x80000008, descs);
core_max = __SHIFTOUT(descs[2], CPUID_CAPEX_NC) + 1;
n = __SHIFTOUT(descs[2], CPUID_CAPEX_ApicIdSize);
if (n != 0) {
core_bits = n;
}
break;
default:
core_max = 1;
}
KASSERT(lp_max >= core_max);
smt_bits = ilog2((lp_max / core_max) - 1) + 1;
if (core_bits == 0) {
core_bits = ilog2(core_max - 1) + 1;
}
if (cpu_vendor == CPUVENDOR_AMD && cpu_family < 0x11) {
const uint64_t reg = rdmsr(MSR_NB_CFG);
if ((reg & NB_CFG_INITAPICCPUIDLO) == 0) {
const u_int node_id = apic_id & __BITS(0, 2);
apic_id = (cpu_family == 0xf) ?
(apic_id >> core_bits) | (node_id << core_bits) :
(apic_id >> 5) | (node_id << 2);
}
}
if (cpu_vendor == CPUVENDOR_AMD && cpu_family >= 0x17) {
x86_cpuid(0x8000001e, descs);
const u_int threads = __SHIFTOUT(descs[1],
CPUID_AMD_PROCT_THREADS_PER_CORE) + 1;
KASSERT(smt_bits == 0);
smt_bits = ilog2(threads);
KASSERT(smt_bits <= core_bits);
core_bits -= smt_bits;
}
if (smt_bits + core_bits) {
if (smt_bits + core_bits < sizeof(apic_id) * NBBY)
package_id = apic_id >> (smt_bits + core_bits);
else
package_id = 0;
}
if (core_bits) {
u_int core_mask = __BITS(smt_bits, smt_bits + core_bits - 1);
core_id = __SHIFTOUT(apic_id, core_mask);
}
if (smt_bits) {
u_int smt_mask = __BITS(0, smt_bits - 1);
smt_id = __SHIFTOUT(apic_id, smt_mask);
}
cpu_topology_set(ci, package_id, core_id, smt_id, numa_id);
}