2020-05-24 15:30:55 -05:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2022-01-31 16:59:14 -06:00
|
|
|
// Copyright (C) 2020-2022 Martin Whitaker.
|
2022-04-11 12:37:52 -05:00
|
|
|
// Copyright (C) 2004-2022 Sam Demeulemeester.
|
2020-05-24 15:30:55 -05:00
|
|
|
//
|
|
|
|
// Derived from memtest86+ cpuid.h
|
2022-04-11 12:37:52 -05:00
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "cpuid.h"
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Public Variables
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
cpuid_info_t cpuid_info;
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Public Functions
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void cpuid_init(void)
|
|
|
|
{
|
2022-04-11 12:37:52 -05:00
|
|
|
uint32_t reg[4];
|
2020-05-24 15:30:55 -05:00
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
// Get the max standard cpuid & vendor ID.
|
|
|
|
cpuid(0x0, 0,
|
2022-01-31 16:59:14 -06:00
|
|
|
&cpuid_info.max_cpuid,
|
2020-05-24 15:30:55 -05:00
|
|
|
&cpuid_info.vendor_id.raw[0],
|
|
|
|
&cpuid_info.vendor_id.raw[2],
|
|
|
|
&cpuid_info.vendor_id.raw[1]
|
|
|
|
);
|
|
|
|
cpuid_info.vendor_id.str[CPUID_VENDOR_STR_LENGTH - 1] = '\0';
|
|
|
|
|
|
|
|
// Get the processor family information & feature flags.
|
2022-01-31 16:59:14 -06:00
|
|
|
if (cpuid_info.max_cpuid >= 1) {
|
2020-05-24 15:30:55 -05:00
|
|
|
cpuid(0x1, 0,
|
|
|
|
&cpuid_info.version.raw,
|
|
|
|
&cpuid_info.proc_info.raw,
|
|
|
|
&cpuid_info.flags.raw[1],
|
|
|
|
&cpuid_info.flags.raw[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the digital thermal sensor & power management status bits.
|
2022-01-31 16:59:14 -06:00
|
|
|
if (cpuid_info.max_cpuid >= 6) {
|
2020-05-24 15:30:55 -05:00
|
|
|
cpuid(0x6, 0,
|
|
|
|
&cpuid_info.dts_pmp,
|
2022-04-11 12:37:52 -05:00
|
|
|
®[0],
|
|
|
|
®[1],
|
|
|
|
®[2]
|
2020-05-24 15:30:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the max extended cpuid.
|
|
|
|
cpuid(0x80000000, 0,
|
|
|
|
&cpuid_info.max_xcpuid,
|
2022-04-11 12:37:52 -05:00
|
|
|
®[0],
|
|
|
|
®[1],
|
|
|
|
®[2]
|
2020-05-24 15:30:55 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Get extended feature flags, only save EDX.
|
|
|
|
if (cpuid_info.max_xcpuid >= 0x80000001) {
|
|
|
|
cpuid(0x80000001, 0,
|
2022-04-11 12:37:52 -05:00
|
|
|
®[0],
|
|
|
|
®[1],
|
|
|
|
®[2],
|
2020-05-24 15:30:55 -05:00
|
|
|
&cpuid_info.flags.raw[2]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the brand ID.
|
|
|
|
if (cpuid_info.max_xcpuid >= 0x80000004) {
|
|
|
|
cpuid(0x80000002, 0,
|
|
|
|
&cpuid_info.brand_id.raw[0],
|
|
|
|
&cpuid_info.brand_id.raw[1],
|
|
|
|
&cpuid_info.brand_id.raw[2],
|
|
|
|
&cpuid_info.brand_id.raw[3]
|
|
|
|
);
|
|
|
|
cpuid(0x80000003, 0,
|
|
|
|
&cpuid_info.brand_id.raw[4],
|
|
|
|
&cpuid_info.brand_id.raw[5],
|
|
|
|
&cpuid_info.brand_id.raw[6],
|
|
|
|
&cpuid_info.brand_id.raw[7]
|
|
|
|
);
|
|
|
|
cpuid(0x80000004, 0,
|
|
|
|
&cpuid_info.brand_id.raw[8],
|
|
|
|
&cpuid_info.brand_id.raw[9],
|
|
|
|
&cpuid_info.brand_id.raw[10],
|
|
|
|
&cpuid_info.brand_id.raw[11]
|
|
|
|
);
|
|
|
|
cpuid_info.brand_id.str[CPUID_BRAND_STR_LENGTH - 1] = '\0';
|
|
|
|
}
|
|
|
|
// Intel chips right-justify this string for some reason - undo that.
|
|
|
|
p = q = &cpuid_info.brand_id.str[0];
|
|
|
|
while (*p == ' ') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (p != q) {
|
|
|
|
while (*p) {
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
while (q <= &cpuid_info.brand_id.str[CPUID_BRAND_STR_LENGTH]) {
|
|
|
|
*q++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get cache information.
|
|
|
|
switch (cpuid_info.vendor_id.str[0]) {
|
|
|
|
case 'A':
|
|
|
|
// AMD Processors
|
|
|
|
if (cpuid_info.max_xcpuid >= 0x80000005) {
|
|
|
|
cpuid(0x80000005, 0,
|
2022-04-11 12:37:52 -05:00
|
|
|
®[0],
|
|
|
|
®[1],
|
2020-05-24 15:30:55 -05:00
|
|
|
&cpuid_info.cache_info.raw[0],
|
|
|
|
&cpuid_info.cache_info.raw[1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (cpuid_info.max_xcpuid >= 0x80000006) {
|
|
|
|
cpuid(0x80000006, 0,
|
2022-04-11 12:37:52 -05:00
|
|
|
®[0],
|
|
|
|
®[1],
|
2020-05-24 15:30:55 -05:00
|
|
|
&cpuid_info.cache_info.raw[2],
|
|
|
|
&cpuid_info.cache_info.raw[3]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
// Intel Processors
|
|
|
|
// No cpuid info to read.
|
|
|
|
break;
|
|
|
|
}
|
2022-04-11 12:37:52 -05:00
|
|
|
|
|
|
|
// Detect CPU Topology (Core/Thread) infos
|
|
|
|
cpuid_info.topology.core_count = -1;
|
|
|
|
cpuid_info.topology.thread_count = -1;
|
|
|
|
cpuid_info.topology.is_hybrid = 0;
|
|
|
|
cpuid_info.topology.ecore_count = -1;
|
|
|
|
cpuid_info.topology.pcore_count = -1;
|
|
|
|
|
|
|
|
int thread_per_core = 1;
|
|
|
|
|
2022-04-16 11:34:49 -05:00
|
|
|
// Set correct HTT flag according to AP-485
|
|
|
|
if (cpuid_info.max_cpuid >= 1 && cpuid_info.flags.htt) {
|
|
|
|
cpuid(1, 0,®[0], ®[1], ®[2], ®[3]);
|
|
|
|
if(((reg[1] >> 16) & 0xFF) <= 1) {
|
|
|
|
cpuid_info.flags.htt = !cpuid_info.flags.htt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 12:37:52 -05:00
|
|
|
switch (cpuid_info.vendor_id.str[0]) {
|
|
|
|
case 'A':
|
|
|
|
// AMD Processors
|
|
|
|
if (cpuid_info.max_xcpuid >= 0x80000008) {
|
|
|
|
|
|
|
|
cpuid(0x80000008, 0, ®[0], ®[1], ®[2], ®[3]);
|
|
|
|
cpuid_info.topology.thread_count = (reg[2] & 0xFF) + 1;
|
|
|
|
|
|
|
|
if (cpuid_info.max_xcpuid >= 0x8000001E) {
|
|
|
|
cpuid(0x8000001E, 0, ®[0], ®[1], ®[2], ®[3]);
|
|
|
|
|
|
|
|
if (((reg[1] >> 8) & 0x3) > 0) {
|
|
|
|
thread_per_core = 2;
|
|
|
|
}
|
|
|
|
} else if (cpuid_info.flags.htt) {
|
2022-05-20 18:31:06 -05:00
|
|
|
if (cpuid_info.version.extendedFamily >= 8) {
|
|
|
|
thread_per_core = 2;
|
|
|
|
} else {
|
|
|
|
cpuid_info.flags.htt = 0; // Pre-ZEN never has SMT
|
|
|
|
}
|
2022-04-11 12:37:52 -05:00
|
|
|
}
|
|
|
|
cpuid_info.topology.core_count = cpuid_info.topology.thread_count / thread_per_core;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
// VIA / CentaurHauls
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
if (cpuid_info.vendor_id.str[7] == 'T') break; // Transmeta
|
|
|
|
// Intel
|
|
|
|
if (cpuid_info.max_cpuid >= 0xB) {
|
|
|
|
|
|
|
|
// Populate Hybrid Status (CPUID 7.EDX[15]) for Alder Lake+
|
|
|
|
cpuid(0x7, 0, ®[0], ®[1], ®[2], ®[3]);
|
|
|
|
if (reg[3] & (1 << 15)) {
|
|
|
|
cpuid_info.topology.is_hybrid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i < 4; i++) {
|
|
|
|
cpuid(0xB, i, ®[0], ®[1], ®[2], ®[3]);
|
|
|
|
|
|
|
|
switch((reg[2] >> 8) & 0xFF) {
|
|
|
|
case 1: // SMT
|
|
|
|
thread_per_core = reg[1] & 0xFF;
|
|
|
|
break;
|
|
|
|
case 2: // Cores
|
|
|
|
cpuid_info.topology.thread_count = reg[1] & 0xFFFF;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cpuid_info.topology.core_count = cpuid_info.topology.thread_count / thread_per_core;
|
|
|
|
|
|
|
|
} else if (cpuid_info.max_cpuid >= 0x4) {
|
|
|
|
cpuid(4, 0, ®[0], ®[1], ®[2], ®[3]);
|
|
|
|
|
|
|
|
cpuid_info.topology.core_count = (reg[0] >> 26) + 1;
|
|
|
|
cpuid_info.topology.thread_count = cpuid_info.topology.core_count;
|
|
|
|
|
|
|
|
if (cpuid_info.flags.htt){
|
|
|
|
cpuid_info.topology.thread_count *= 2;
|
|
|
|
}
|
|
|
|
} else if (cpuid_info.max_cpuid >= 0x2) {
|
|
|
|
if(cpuid_info.flags.htt){
|
|
|
|
cpuid_info.topology.core_count = 1;
|
|
|
|
cpuid_info.topology.thread_count = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-05-24 15:30:55 -05:00
|
|
|
}
|