Compare commits

..

10 Commits
i280 ... i290

Author SHA1 Message Date
Dr-Noob
ad5c9e6037 Fix warning 2024-10-07 21:39:30 +01:00
Dr-Noob
6ada3ec361 Fix printer bug and improve colors 2024-10-07 21:30:59 +01:00
Dr-Noob
91c92bbdf6 Fix: Do not enter get_uarch_from_riscv_cpuinfo with a zero marchid and use strtoul instead of strtol for unsigned long 2024-10-07 08:44:55 +01:00
Dr-Noob
d0cc04529c Some documentation 2024-10-06 18:18:42 +01:00
Dr-Noob
f826125395 Update SoC name 2024-10-06 18:15:18 +01:00
Dr-Noob
2272423163 Implement get_riscv_cpuinfo. Needs testing and further documentation 2024-10-02 08:43:18 +01:00
Dr-Noob
5194976739 WIP 2024-09-29 14:21:11 +01:00
Dr-Noob
417d7ce730 WIP 2024-09-22 22:33:04 +01:00
Dr-Noob
01406778be [v1.06][ARM] Add new Dimensity MTK SoCs (#288) 2024-09-22 22:13:50 +01:00
Dr-Noob
7d786ca1b7 [v1.06][ARM] Implement automatic SoC inferring from device tree
This is a major change in the SoC detection methodology.

Previous to this commit, the SoC (model, vendor and manufacturing
process) would only appear if the exact model was found in the LUT.
In other words, every supported SoC must be added manually to the LUT.
This would allow to show the precise SoC model and the manufacturing
process, which can only be hardcoded (i.e., it cannot be dynamically
determined).

This commit introduces guess_raw_soc_from_devtree, a new way of
inferring the SoC. This simply reads from the compatible file in the
device tree and tries to find a matching vendor to that string. If
there is a match, then we simply copy the model also from the
compatible string and show it directly.

This new implementation will show a less "precise" SoC name and
no manufacturing process because there would be no hardcoded value for
that SoC. However, it improves the scalability of the SoC detection
significantly because there is no longer a need to hardcode every
single SoC.

Lastly, there are some SoC vendors intentionally left outside the
scope of this function: For now I prefer to keep updating manually
the LUT for those to ensure the highest quality detection (e.g.,
showing precise SoC name and manufacturing process).
2024-09-22 17:11:31 +01:00
15 changed files with 236 additions and 37 deletions

View File

@@ -306,7 +306,21 @@ bool match_mediatek(char* soc_name, struct system_on_chip* soc) {
soc->vendor = SOC_VENDOR_MEDIATEK; soc->vendor = SOC_VENDOR_MEDIATEK;
SOC_START SOC_START
// Dimensity // // TODO
// Dimensity 6000 Series //
// Dimensity 7000 Series //
// Dimensity 8000 Series //
// END TODO
// Dimensity 9000 Series //
SOC_EQ(tmp, "MT6983Z", "Dimensity 9000", SOC_MTK_MT6983Z, soc, 4)
SOC_EQ(tmp, "MT8798Z/C","Dimensity 9000", SOC_MTK_MT8798ZC, soc, 4)
SOC_EQ(tmp, "MT6983W", "Dimensity 9000+", SOC_MTK_MT6983W, soc, 4)
SOC_EQ(tmp, "MT8798Z/T","Dimensity 9000+", SOC_MTK_MT8798ZT, soc, 4)
SOC_EQ(tmp, "MT6985W", "Dimensity 9200+", SOC_MTK_MT6985W, soc, 4)
SOC_EQ(tmp, "MT6985", "Dimensity 9200", SOC_MTK_MT6985, soc, 4)
SOC_EQ(tmp, "MT6989", "Dimensity 9300", SOC_MTK_MT6989, soc, 4)
SOC_EQ(tmp, "MT8796", "Dimensity 9300", SOC_MTK_MT8796, soc, 4)
// Dimensity 1000 //
SOC_EQ(tmp, "MT6893Z", "Dimensity 1300", SOC_MTK_MT6893Z, soc, 6) SOC_EQ(tmp, "MT6893Z", "Dimensity 1300", SOC_MTK_MT6893Z, soc, 6)
SOC_EQ(tmp, "MT6893", "Dimensity 1200", SOC_MTK_MT6893, soc, 6) SOC_EQ(tmp, "MT6893", "Dimensity 1200", SOC_MTK_MT6893, soc, 6)
SOC_EQ(tmp, "MT6891", "Dimensity 1100", SOC_MTK_MT6891, soc, 6) SOC_EQ(tmp, "MT6891", "Dimensity 1100", SOC_MTK_MT6891, soc, 6)
@@ -316,12 +330,21 @@ bool match_mediatek(char* soc_name, struct system_on_chip* soc) {
SOC_EQ(tmp, "MT6885Z", "Dimensity 1000L", SOC_MTK_MT6885Z, soc, 7) SOC_EQ(tmp, "MT6885Z", "Dimensity 1000L", SOC_MTK_MT6885Z, soc, 7)
SOC_EQ(tmp, "MT6889Z", "Dimensity 1000+", SOC_MTK_MT6889Z, soc, 7) SOC_EQ(tmp, "MT6889Z", "Dimensity 1000+", SOC_MTK_MT6889Z, soc, 7)
SOC_EQ(tmp, "MT6883Z", "Dimensity 1000C", SOC_MTK_MT6883Z, soc, 7) SOC_EQ(tmp, "MT6883Z", "Dimensity 1000C", SOC_MTK_MT6883Z, soc, 7)
SOC_EQ(tmp, "MT6833", "Dimensity 700", SOC_MTK_MT6833, soc, 7) // Dimensity 900
SOC_EQ(tmp, "MT6853", "Dimensity 720", SOC_MTK_MT6853, soc, 7) SOC_EQ(tmp, "MT6877V/Z","Dimensity 900", SOC_MTK_MT6877VZ, soc, 6)
SOC_EQ(tmp, "MT6877T" ,"Dimensity 920", SOC_MTK_MT6877T, soc, 6)
SOC_EQ(tmp, "MT6855" ,"Dimensity 930", SOC_MTK_MT6855, soc, 6)
// Dimensity 800
SOC_EQ(tmp, "MT6873", "Dimensity 800", SOC_MTK_MT6873, soc, 7) SOC_EQ(tmp, "MT6873", "Dimensity 800", SOC_MTK_MT6873, soc, 7)
SOC_EQ(tmp, "MT6853V", "Dimensity 800U", SOC_MTK_MT6853V, soc, 7) SOC_EQ(tmp, "MT6853V/T","Dimensity 800U", SOC_MTK_MT6853VT, soc, 7)
SOC_EQ(tmp, "MT6833", "Dimensity 810", SOC_MTK_MT6833, soc, 6) SOC_EQ(tmp, "MT6853T", "Dimensity 800U", SOC_MTK_MT6853T, soc, 7)
SOC_EQ(tmp, "MT6833P", "Dimensity 810", SOC_MTK_MT6833P, soc, 6)
SOC_EQ(tmp, "MT6833GP", "Dimensity 810", SOC_MTK_MT6833GP, soc, 6)
SOC_EQ(tmp, "MT6833V", "Dimensity 810", SOC_MTK_MT6833V, soc, 6)
SOC_EQ(tmp, "MT6875", "Dimensity 820", SOC_MTK_MT6875, soc, 7) SOC_EQ(tmp, "MT6875", "Dimensity 820", SOC_MTK_MT6875, soc, 7)
// Dimensity 700
SOC_EQ(tmp, "MT6833", "Dimensity 700", SOC_MTK_MT6833, soc, 7)
SOC_EQ(tmp, "MT6853V", "Dimensity 720", SOC_MTK_MT6853, soc, 7)
// Helio // // Helio //
SOC_EQ(tmp, "MT6761D", "Helio A20", SOC_MTK_MT6761D, soc, 12) SOC_EQ(tmp, "MT6761D", "Helio A20", SOC_MTK_MT6761D, soc, 12)
SOC_EQ(tmp, "MT6761", "Helio A22", SOC_MTK_MT6761, soc, 12) SOC_EQ(tmp, "MT6761", "Helio A22", SOC_MTK_MT6761, soc, 12)
@@ -1091,6 +1114,8 @@ struct system_on_chip* guess_raw_soc_from_devtree(struct system_on_chip* soc) {
for (int i=0; i < num_vendors; i++) { for (int i=0; i < num_vendors; i++) {
if (strcmp(socFromDevtree[index].compatible, dt_vendors[i]->vendor) == 0) { if (strcmp(socFromDevtree[index].compatible, dt_vendors[i]->vendor) == 0) {
fill_soc_raw(soc, dt_vendors[i]->model, socFromDevtree[index].soc_vendor); fill_soc_raw(soc, dt_vendors[i]->model, socFromDevtree[index].soc_vendor);
printWarn("Your SoC is unsupported by cpufetch but could still be detected successfully. If you want to help improve the project, please paste the output of 'cpufetch --verbose' on https://github.com/Dr-Noob/cpufetch/issues");
return soc;
} }
} }
index++; index++;
@@ -1357,9 +1382,9 @@ struct system_on_chip* get_soc(struct cpuInfo* cpu) {
soc->model = SOC_MODEL_UNKNOWN; soc->model = SOC_MODEL_UNKNOWN;
soc->process = UNKNOWN; soc->process = UNKNOWN;
#else #else
if(soc->model == SOC_MODEL_UNKNOWN) { if(soc->raw_name == NULL) {
// raw_name might not be NULL, but if we were unable to find // We were unable to find the SoC, so just initialize raw_name
// the exact SoC, just print "Unkwnown" // with the unknown string
soc->raw_name = emalloc(sizeof(char) * (strlen(STRING_UNKNOWN)+1)); soc->raw_name = emalloc(sizeof(char) * (strlen(STRING_UNKNOWN)+1));
snprintf(soc->raw_name, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); snprintf(soc->raw_name, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN);
} }

View File

@@ -192,6 +192,22 @@ enum {
SOC_MTK_MT9950, SOC_MTK_MT9950,
SOC_MTK_MT9972, SOC_MTK_MT9972,
SOC_MTK_MT9982, SOC_MTK_MT9982,
SOC_MTK_MT6983Z,
SOC_MTK_MT8798ZC,
SOC_MTK_MT6983W,
SOC_MTK_MT8798ZT,
SOC_MTK_MT6985W,
SOC_MTK_MT6985,
SOC_MTK_MT6989,
SOC_MTK_MT8796,
SOC_MTK_MT6877VZ,
SOC_MTK_MT6877T,
SOC_MTK_MT6855,
SOC_MTK_MT6853VT,
SOC_MTK_MT6853T,
SOC_MTK_MT6833P,
SOC_MTK_MT6833GP,
SOC_MTK_MT6833V,
// Snapdragon // // Snapdragon //
SOC_SNAPD_QSD8650, SOC_SNAPD_QSD8650,
SOC_SNAPD_QSD8250, SOC_SNAPD_QSD8250,
@@ -427,7 +443,7 @@ inline static VENDOR get_soc_vendor_from_soc(SOC soc) {
else if(soc >= SOC_HISILICON_3620 && soc <= SOC_HISILICON_9000S) return SOC_VENDOR_KIRIN; else if(soc >= SOC_HISILICON_3620 && soc <= SOC_HISILICON_9000S) return SOC_VENDOR_KIRIN;
else if(soc >= SOC_KUNPENG_920 && soc <= SOC_KUNPENG_930) return SOC_VENDOR_KUNPENG; else if(soc >= SOC_KUNPENG_920 && soc <= SOC_KUNPENG_930) return SOC_VENDOR_KUNPENG;
else if(soc >= SOC_EXYNOS_3475 && soc <= SOC_EXYNOS_880) return SOC_VENDOR_EXYNOS; else if(soc >= SOC_EXYNOS_3475 && soc <= SOC_EXYNOS_880) return SOC_VENDOR_EXYNOS;
else if(soc >= SOC_MTK_MT6893 && soc <= SOC_MTK_MT8783) return SOC_VENDOR_MEDIATEK; else if(soc >= SOC_MTK_MT5327 && soc <= SOC_MTK_MT6833V) return SOC_VENDOR_MEDIATEK;
else if(soc >= SOC_SNAPD_QSD8650 && soc <= SOC_SNAPD_SC8280XP) return SOC_VENDOR_SNAPDRAGON; else if(soc >= SOC_SNAPD_QSD8650 && soc <= SOC_SNAPD_SC8280XP) return SOC_VENDOR_SNAPDRAGON;
else if(soc >= SOC_APPLE_M1 && soc <= SOC_APPLE_M3_MAX) return SOC_VENDOR_APPLE; else if(soc >= SOC_APPLE_M1 && soc <= SOC_APPLE_M3_MAX) return SOC_VENDOR_APPLE;
else if(soc >= SOC_ALLWINNER_A10 && soc <= SOC_ALLWINNER_R328) return SOC_VENDOR_ALLWINNER; else if(soc >= SOC_ALLWINNER_A10 && soc <= SOC_ALLWINNER_R328) return SOC_VENDOR_ALLWINNER;

View File

@@ -445,6 +445,26 @@ $C1 .######### |__________|###### |__________| \
$C1 ########### ___########### __________ \ $C1 ########### ___########### __________ \
$C1.########## |__________| |__________| " $C1.########## |__________| |__________| "
#define ASCII_SPACEMIT \
"$C1 :#: \
$C1 :####: \
$C1 :#######: \
$C1 :#########: \
$C1 :#########: \
$C1 :#######: \
$C1 :####: \
$C1 :#: \
$C1:##: :#: \
$C1:####: :###: \
$C1:#######: :####: \
$C1:##########: :###: \
$C1:###########: :#: \
$C1:###########: \
$C1 :##########: \
$C1 :#######: \
$C1 :####: \
$C1 :##: "
// --------------------- LONG LOGOS ------------------------- // // --------------------- LONG LOGOS ------------------------- //
#define ASCII_AMD_L \ #define ASCII_AMD_L \
"$C1 \ "$C1 \
@@ -624,6 +644,7 @@ asciiL logo_ampere = { ASCII_AMPERE, 50, 17, false, {C_FG_RED},
asciiL logo_nxp = { ASCII_NXP, 55, 8, false, {C_FG_YELLOW, C_FG_CYAN, C_FG_GREEN}, {C_FG_CYAN, C_FG_WHITE} }; asciiL logo_nxp = { ASCII_NXP, 55, 8, false, {C_FG_YELLOW, C_FG_CYAN, C_FG_GREEN}, {C_FG_CYAN, C_FG_WHITE} };
asciiL logo_amlogic = { ASCII_AMLOGIC, 58, 8, false, {C_FG_BLUE}, {C_FG_BLUE, C_FG_B_WHITE} }; asciiL logo_amlogic = { ASCII_AMLOGIC, 58, 8, false, {C_FG_BLUE}, {C_FG_BLUE, C_FG_B_WHITE} };
asciiL logo_marvell = { ASCII_MARVELL, 56, 10, false, {C_FG_B_BLACK}, {C_FG_B_BLACK, C_FG_B_WHITE} }; asciiL logo_marvell = { ASCII_MARVELL, 56, 10, false, {C_FG_B_BLACK}, {C_FG_B_BLACK, C_FG_B_WHITE} };
asciiL logo_spacemit = { ASCII_SPACEMIT, 26, 18, false, {C_FG_B_GREEN}, {C_FG_B_GREEN, C_FG_B_WHITE} };
// Long variants | ----------------------------------------------------------------------------------------------------------------| // Long variants | ----------------------------------------------------------------------------------------------------------------|
asciiL logo_amd_l = { ASCII_AMD_L, 62, 19, true, {C_BG_WHITE, C_BG_GREEN}, {C_FG_WHITE, C_FG_GREEN} }; asciiL logo_amd_l = { ASCII_AMD_L, 62, 19, true, {C_BG_WHITE, C_BG_GREEN}, {C_FG_WHITE, C_FG_GREEN} };

View File

@@ -25,6 +25,7 @@ enum {
CPU_VENDOR_RISCV, CPU_VENDOR_RISCV,
CPU_VENDOR_SIFIVE, CPU_VENDOR_SIFIVE,
CPU_VENDOR_THEAD, CPU_VENDOR_THEAD,
CPU_VENDOR_SPACEMIT,
// OTHERS // OTHERS
CPU_VENDOR_UNKNOWN, CPU_VENDOR_UNKNOWN,
CPU_VENDOR_INVALID CPU_VENDOR_INVALID

View File

@@ -20,6 +20,7 @@
#include "../arm/uarch.h" #include "../arm/uarch.h"
#include "../arm/midr.h" #include "../arm/midr.h"
#include "../arm/soc.h" #include "../arm/soc.h"
#include "../arm/socs.h"
#include "../common/soc.h" #include "../common/soc.h"
#elif ARCH_RISCV #elif ARCH_RISCV
#include "../riscv/riscv.h" #include "../riscv/riscv.h"
@@ -411,6 +412,8 @@ void choose_ascii_art(struct ascii* art, struct color** cs, struct terminal* ter
art->art = &logo_allwinner; art->art = &logo_allwinner;
else if(art->vendor == SOC_VENDOR_SIPEED) else if(art->vendor == SOC_VENDOR_SIPEED)
art->art = &logo_sipeed; art->art = &logo_sipeed;
else if(art->vendor == SOC_VENDOR_SPACEMIT)
art->art = &logo_spacemit;
else else
art->art = &logo_riscv; art->art = &logo_riscv;
#endif #endif
@@ -892,6 +895,11 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct color** cs, struct
// https://github.com/Dr-Noob/cpufetch/pull/273 // https://github.com/Dr-Noob/cpufetch/pull/273
// Hide manufacturing process // Hide manufacturing process
#if !defined(_WIN32) #if !defined(_WIN32)
// In the case that the model is unknown but the vendor isn't (this is, when
// guess_raw_soc_from_devtree succeeded), do not show the manufacturing process
// (as it will be unknown)
if (cpu->soc->model != SOC_MODEL_UNKNOWN ||
(cpu->soc->model == SOC_MODEL_UNKNOWN && cpu->soc->vendor == SOC_VENDOR_UNKNOWN))
setAttribute(art, ATTRIBUTE_TECHNOLOGY, manufacturing_process); setAttribute(art, ATTRIBUTE_TECHNOLOGY, manufacturing_process);
#endif #endif

View File

@@ -29,6 +29,7 @@ static char* soc_trademark_string[] = {
[SOC_VENDOR_SIFIVE] = "SiFive ", [SOC_VENDOR_SIFIVE] = "SiFive ",
[SOC_VENDOR_STARFIVE] = "StarFive ", [SOC_VENDOR_STARFIVE] = "StarFive ",
[SOC_VENDOR_SIPEED] = "Sipeed ", [SOC_VENDOR_SIPEED] = "Sipeed ",
[SOC_VENDOR_SPACEMIT] = "SpacemiT ",
// ARM & RISC-V // ARM & RISC-V
[SOC_VENDOR_ALLWINNER] = "Allwinner " [SOC_VENDOR_ALLWINNER] = "Allwinner "
}; };
@@ -85,8 +86,8 @@ void fill_soc_raw(struct system_on_chip* soc, char* soc_name, VENDOR vendor) {
soc->process = UNKNOWN; soc->process = UNKNOWN;
int len = strlen(soc_name) + strlen(soc_trademark_string[soc->vendor]) + 1; int len = strlen(soc_name) + strlen(soc_trademark_string[soc->vendor]) + 1;
soc->name = emalloc(sizeof(char) * len); soc->raw_name = emalloc(sizeof(char) * len);
sprintf(soc->name, "%s%s", soc_trademark_string[soc->vendor], soc_name); sprintf(soc->raw_name, "%s%s", soc_trademark_string[soc->vendor], soc_name);
} }
#ifdef _WIN32 #ifdef _WIN32

View File

@@ -33,6 +33,7 @@ enum {
SOC_VENDOR_SIFIVE, SOC_VENDOR_SIFIVE,
SOC_VENDOR_STARFIVE, SOC_VENDOR_STARFIVE,
SOC_VENDOR_SIPEED, SOC_VENDOR_SIPEED,
SOC_VENDOR_SPACEMIT,
// ARM & RISC-V // ARM & RISC-V
SOC_VENDOR_ALLWINNER SOC_VENDOR_ALLWINNER
}; };

View File

@@ -362,12 +362,13 @@ char* get_devtree_compatible(int *filelen) {
return buf; return buf;
} }
// TODO: // Returns a list of structs devtree, each containing both the vendor and the
// Returns a list of strings containing the vendors of the compatible // model, coming from the compatible file from the device tree. In this
// file from the device tree. In this context, vendor refers to the first // context, vendor refers to the first string of every entry and the model to
// string of every entry. For instance, given a compatible file with: // the second. For instance, given a compatible file with:
// "str1,foo1.str2,foo2" (where . denotes the NULL byte, i.e., the separator), // "str1,foo1.str2,foo2" (where . denotes the NULL byte, i.e., the separator),
// then this function will return a list with str1,str2. // then this function will return a list with two structs, the first one
// containing str1 and foo1 and the other containing str2 and foo2.
struct devtree** get_devtree_compatible_struct(int *num_vendors_ptr) { struct devtree** get_devtree_compatible_struct(int *num_vendors_ptr) {
int len; int len;
char* dt = get_devtree_compatible(&len); char* dt = get_devtree_compatible(&len);
@@ -379,7 +380,12 @@ struct devtree** get_devtree_compatible_struct(int *num_vendors_ptr) {
char* ptr = dt; char* ptr = dt;
for (int ptrpos = 0; ptrpos < len; ptrpos = (ptr-dt)) { for (int ptrpos = 0; ptrpos < len; ptrpos = (ptr-dt)) {
ptr = memchr(ptr, '\0', len)+1; ptr = memchr(ptr, '\0', len);
if (ptr == NULL) {
printBug("get_devtree_compatible_struct: Unable to find delimiter (1) (num_vendors=%d)", num_vendors);
return NULL;
}
ptr++;
num_vendors++; num_vendors++;
} }
@@ -387,21 +393,35 @@ struct devtree** get_devtree_compatible_struct(int *num_vendors_ptr) {
ptr = dt; ptr = dt;
for (int ptrpos = 0, i = 0; ptrpos < len; ptrpos = (ptr-dt), i++) { for (int ptrpos = 0, i = 0; ptrpos < len; ptrpos = (ptr-dt), i++) {
char* comma_ptr = strstr(ptr, ",")-1; char* comma_ptr = strstr(ptr, ",");
char* end_ptr = memchr(comma_ptr, '\0', ptrpos - len); if (comma_ptr == NULL) {
printBug("get_devtree_compatible_struct: Unable to find comma (num_vendors=%d)", num_vendors);
return NULL;
}
comma_ptr = comma_ptr-1; // Point right before comma
char* end_ptr = memchr(comma_ptr, '\0', len - ptrpos);
if (end_ptr == NULL) {
printBug("get_devtree_compatible_struct: Unable to find delimiter (2) (num_vendors=%d)", num_vendors);
return NULL;
}
// TODO check NULL
int vendor_str_len = (comma_ptr-ptr)+1; int vendor_str_len = (comma_ptr-ptr)+1;
int model_str_len = (end_ptr-comma_ptr)+1; int model_str_len = (end_ptr-(comma_ptr+2))+1;
vendors[i] = emalloc(sizeof(struct devtree)); vendors[i] = emalloc(sizeof(struct devtree));
vendors[i]->vendor = ecalloc(vendor_str_len, sizeof(char)); vendors[i]->vendor = ecalloc(vendor_str_len, sizeof(char));
vendors[i]->model = ecalloc(model_str_len, sizeof(char)); vendors[i]->model = ecalloc(model_str_len, sizeof(char));
strncpy(vendors[i]->vendor, ptr, vendor_str_len); strncpy(vendors[i]->vendor, ptr, vendor_str_len);
strncpy(vendors[i]->model, comma_ptr, model_str_len); strncpy(vendors[i]->model, comma_ptr+2, model_str_len);
ptr = memchr(ptr, '\0', len)+1; ptr = memchr(ptr, '\0', len);
if (ptr == NULL) {
printBug("get_devtree_compatible_struct: Unable to find delimiter (3) (num_vendors=%d)", num_vendors);
return NULL;
}
ptr++; // Point right after delimiter
} }
*num_vendors_ptr = num_vendors; *num_vendors_ptr = num_vendors;

View File

@@ -101,8 +101,8 @@ struct extensions* get_extensions_from_str(char* str) {
return ext; return ext;
} }
int len = strlen(str); int len = strlen(str)+1;
ext->str = ecalloc(len+1, sizeof(char)); ext->str = emalloc(len * sizeof(char));
strncpy(ext->str, str, sizeof(char) * len); strncpy(ext->str, str, sizeof(char) * len);
// Code inspired in Linux kernel (riscv_fill_hwcap): // Code inspired in Linux kernel (riscv_fill_hwcap):
@@ -157,13 +157,12 @@ struct cpuInfo* get_cpu_info(void) {
topo->cach = NULL; topo->cach = NULL;
cpu->topo = topo; cpu->topo = topo;
char* cpuinfo_str = get_uarch_from_cpuinfo();
char* ext_str = get_extensions_from_cpuinfo(); char* ext_str = get_extensions_from_cpuinfo();
cpu->hv = emalloc(sizeof(struct hypervisor)); cpu->hv = emalloc(sizeof(struct hypervisor));
cpu->hv->present = false; cpu->hv->present = false;
cpu->ext = get_extensions_from_str(ext_str); cpu->ext = get_extensions_from_str(ext_str);
if(cpu->ext->str != NULL && cpu->ext->mask == 0) return NULL; if(cpu->ext->str != NULL && cpu->ext->mask == 0) return NULL;
cpu->arch = get_uarch_from_cpuinfo_str(cpuinfo_str, cpu); cpu->arch = get_uarch(cpu);
cpu->soc = get_soc(cpu); cpu->soc = get_soc(cpu);
cpu->freq = get_frequency_info(0); cpu->freq = get_frequency_info(0);
cpu->peak_performance = get_peak_performance(cpu); cpu->peak_performance = get_peak_performance(cpu);

View File

@@ -38,6 +38,12 @@ bool match_sipeed(char* soc_name, struct system_on_chip* soc) {
SOC_END SOC_END
} }
bool match_spacemit(char* soc_name, struct system_on_chip* soc) {
SOC_START
SOC_EQ(soc_name, "k1-x", "K1-X", SOC_SPACEMIT_K1X, soc, 22) // https://github.com/Dr-Noob/cpufetch/issues/286 https://www.spacemit.com/en/spacemit-x60-core/
SOC_END
}
struct system_on_chip* parse_soc_from_string(struct system_on_chip* soc) { struct system_on_chip* parse_soc_from_string(struct system_on_chip* soc) {
char* raw_name = soc->raw_name; char* raw_name = soc->raw_name;
@@ -50,6 +56,9 @@ struct system_on_chip* parse_soc_from_string(struct system_on_chip* soc) {
if(match_sifive(raw_name, soc)) if(match_sifive(raw_name, soc))
return soc; return soc;
if(match_spacemit(raw_name, soc))
return soc;
match_sipeed(raw_name, soc); match_sipeed(raw_name, soc);
return soc; return soc;
} }

View File

@@ -13,6 +13,8 @@ enum {
SOC_ALLWINNER_D1H, SOC_ALLWINNER_D1H,
// SIPEED // SIPEED
SOC_SIPEED_LICHEEPI4A, SOC_SIPEED_LICHEEPI4A,
// SPACEMIT
SOC_SPACEMIT_K1X,
// UNKNOWN // UNKNOWN
SOC_MODEL_UNKNOWN SOC_MODEL_UNKNOWN
}; };
@@ -22,6 +24,7 @@ inline static VENDOR get_soc_vendor_from_soc(SOC soc) {
if(soc >= SOC_STARFIVE_VF2 && soc <= SOC_STARFIVE_VF2) return SOC_VENDOR_STARFIVE; if(soc >= SOC_STARFIVE_VF2 && soc <= SOC_STARFIVE_VF2) return SOC_VENDOR_STARFIVE;
if(soc >= SOC_ALLWINNER_D1H && soc <= SOC_ALLWINNER_D1H) return SOC_VENDOR_ALLWINNER; if(soc >= SOC_ALLWINNER_D1H && soc <= SOC_ALLWINNER_D1H) return SOC_VENDOR_ALLWINNER;
if(soc >= SOC_SIPEED_LICHEEPI4A && soc <= SOC_SIPEED_LICHEEPI4A) return SOC_VENDOR_SIPEED; if(soc >= SOC_SIPEED_LICHEEPI4A && soc <= SOC_SIPEED_LICHEEPI4A) return SOC_VENDOR_SIPEED;
if(soc >= SOC_SPACEMIT_K1X && soc <= SOC_SPACEMIT_K1X) return SOC_VENDOR_SPACEMIT;
return SOC_VENDOR_UNKNOWN; return SOC_VENDOR_UNKNOWN;
} }

View File

@@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include "uarch.h" #include "uarch.h"
#include "udev.h"
#include "../common/global.h" #include "../common/global.h"
typedef uint32_t MICROARCH; typedef uint32_t MICROARCH;
@@ -12,6 +13,7 @@ struct uarch {
MICROARCH uarch; MICROARCH uarch;
char* uarch_str; char* uarch_str;
char* cpuinfo_str; char* cpuinfo_str;
struct riscv_cpuinfo* ci;
}; };
enum { enum {
@@ -21,13 +23,20 @@ enum {
UARCH_U74, UARCH_U74,
// THEAD // THEAD
UARCH_C906, UARCH_C906,
UARCH_C910 UARCH_C910,
// SPACEMIT
UARCH_X60
}; };
#define UARCH_START if (false) {} #define UARCH_START if (false) {}
#define CHECK_UARCH(arch, cpu, cpuinfo_str, uarch_str, str, uarch, vendor) \ #define CHECK_UARCH(arch, cpu, cpuinfo_str, uarch_str, str, uarch, vendor) \
else if (strcmp(cpuinfo_str, uarch_str) == 0) fill_uarch(arch, cpu, str, uarch, vendor); else if (strcmp(cpuinfo_str, uarch_str) == 0) fill_uarch(arch, cpu, str, uarch, vendor);
#define UARCH_END else { printBug("Unknown microarchitecture detected: uarch='%s'", cpuinfo_str); fill_uarch(arch, cpu, "Unknown", UARCH_UNKNOWN, CPU_VENDOR_UNKNOWN); } #define UARCH_END else { printWarn("Unknown microarchitecture detected: uarch='%s'", cpuinfo_str); fill_uarch(arch, cpu, "Unknown", UARCH_UNKNOWN, CPU_VENDOR_UNKNOWN); }
#define ARCHID_START if (false) {}
#define CHECK_ARCHID(arch, marchid_val, str, uarch, vendor) \
else if (arch->ci->marchid == (unsigned long) marchid_val) fill_uarch(arch, cpu, str, uarch, vendor);
#define ARCHID_END else { printWarn("Unknown microarchitecture detected: marchid=0x%.8X", arch->ci->marchid); fill_uarch(arch, cpu, "Unknown", UARCH_UNKNOWN, CPU_VENDOR_UNKNOWN); }
void fill_uarch(struct uarch* arch, struct cpuInfo* cpu, char* str, MICROARCH u, VENDOR vendor) { void fill_uarch(struct uarch* arch, struct cpuInfo* cpu, char* str, MICROARCH u, VENDOR vendor) {
arch->uarch = u; arch->uarch = u;
@@ -39,14 +48,8 @@ void fill_uarch(struct uarch* arch, struct cpuInfo* cpu, char* str, MICROARCH u,
// https://elixir.bootlin.com/linux/latest/source/Documentation/devicetree/bindings/riscv/cpus.yaml // https://elixir.bootlin.com/linux/latest/source/Documentation/devicetree/bindings/riscv/cpus.yaml
// SiFive: https://www.sifive.com/risc-v-core-ip // SiFive: https://www.sifive.com/risc-v-core-ip
// T-Head: https://www.t-head.cn/product/c906 // T-Head: https://www.t-head.cn/product/c906
struct uarch* get_uarch_from_cpuinfo_str(char* cpuinfo_str, struct cpuInfo* cpu) { struct uarch* get_uarch_from_cpuinfo_str(char* cpuinfo_str, struct cpuInfo* cpu, struct uarch* arch) {
struct uarch* arch = emalloc(sizeof(struct uarch));
arch->cpuinfo_str = cpuinfo_str; arch->cpuinfo_str = cpuinfo_str;
if(cpuinfo_str == NULL) {
printWarn("get_uarch_from_cpuinfo: Unable to detect microarchitecture, cpuinfo_str is NULL");
fill_uarch(arch, cpu, "Unknown", UARCH_UNKNOWN, CPU_VENDOR_UNKNOWN);
return arch;
}
// U74/U74-MC: // U74/U74-MC:
// SiFive says that U74-MC is "Multicore: four U74 cores and one S76 core" while // SiFive says that U74-MC is "Multicore: four U74 cores and one S76 core" while
@@ -70,6 +73,41 @@ struct uarch* get_uarch_from_cpuinfo_str(char* cpuinfo_str, struct cpuInfo* cpu)
return arch; return arch;
} }
// Use marchid to get the microarchitecture
struct uarch* get_uarch_from_riscv_cpuinfo(struct cpuInfo* cpu, struct uarch* arch) {
ARCHID_START
CHECK_ARCHID(arch, 0x8000000058000001, "X60", UARCH_X60, CPU_VENDOR_SPACEMIT) // https://github.com/Dr-Noob/cpufetch/issues/286
ARCHID_END
return arch;
}
struct uarch* get_uarch(struct cpuInfo* cpu) {
char* cpuinfo_str = get_uarch_from_cpuinfo();
struct uarch* arch = emalloc(sizeof(struct uarch));
arch->uarch = UARCH_UNKNOWN;
arch->ci = NULL;
if (cpuinfo_str == NULL) {
printWarn("get_uarch_from_cpuinfo: Unable to detect microarchitecture using uarch: cpuinfo_str is NULL");
arch->ci = get_riscv_cpuinfo();
if (arch->ci == NULL || arch->ci->marchid == 0)
printWarn("get_riscv_cpuinfo: Unable to get marchid from udev");
else
arch = get_uarch_from_riscv_cpuinfo(cpu, arch);
}
else {
arch = get_uarch_from_cpuinfo_str(cpuinfo_str, cpu, arch);
}
if (arch->uarch == UARCH_UNKNOWN)
fill_uarch(arch, cpu, "Unknown", UARCH_UNKNOWN, CPU_VENDOR_UNKNOWN);
return arch;
}
char* get_str_uarch(struct cpuInfo* cpu) { char* get_str_uarch(struct cpuInfo* cpu) {
return cpu->arch->uarch_str; return cpu->arch->uarch_str;
} }

View File

@@ -9,6 +9,6 @@ struct uarch;
char* get_arch_cpuinfo_str(struct cpuInfo* cpu); char* get_arch_cpuinfo_str(struct cpuInfo* cpu);
char* get_str_uarch(struct cpuInfo* cpu); char* get_str_uarch(struct cpuInfo* cpu);
void free_uarch_struct(struct uarch* arch); void free_uarch_struct(struct uarch* arch);
struct uarch* get_uarch_from_cpuinfo_str(char* cpuinfo_str, struct cpuInfo* cpu); struct uarch* get_uarch(struct cpuInfo* cpu);
#endif #endif

View File

@@ -7,6 +7,9 @@
#define _PATH_DEVTREE "/proc/device-tree/compatible" #define _PATH_DEVTREE "/proc/device-tree/compatible"
#define CPUINFO_UARCH_STR "uarch\t\t: " #define CPUINFO_UARCH_STR "uarch\t\t: "
#define CPUINFO_EXTENSIONS_STR "isa\t\t: " #define CPUINFO_EXTENSIONS_STR "isa\t\t: "
#define CPUINFO_RISCV_MVENDORID "mvendorid\t:"
#define CPUINFO_RISCV_MARCHID "marchid\t\t:"
#define CPUINFO_RISCV_MIMPID "mimpid\t\t:"
#define DEVTREE_HARDWARE_FIELD 0 #define DEVTREE_HARDWARE_FIELD 0
char* get_field_from_devtree(int DEVTREE_FIELD) { char* get_field_from_devtree(int DEVTREE_FIELD) {
@@ -75,6 +78,52 @@ char* parse_cpuinfo_field(char* field_str) {
return ret; return ret;
} }
unsigned long parse_cpuinfo_field_uint64(char* field_str) {
int filelen;
char* buf;
if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) {
printWarn("read_file: %s: %s", _PATH_CPUINFO, strerror(errno));
return 0;
}
char* tmp = strstr(buf, field_str);
if(tmp == NULL) return 0;
tmp += strlen(field_str);
char* end;
errno = 0;
unsigned long ret = strtoul(tmp, &end, 16);
if (errno != 0) {
printWarn("strtoul: %s: %s", strerror(errno), tmp);
return 0;
}
return ret;
}
// Creates and fills in the riscv_cpuinfo struct (which contains
// mvendorid, marchid and mimpid) using cpuinfo to fetch the values.
//
// Every RISC-V hart (hardware thread) [1] provides a
// marchid (Machine Architecture ID register) CSR that encodes its
// base microarchitecture [2]. For more information about
// marchid and the rest of values, see [3].
// [1] https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/QKjUDjz_vKo
// [2] https://github.com/riscv/riscv-isa-manual/blob/main/marchid.md
// [3] https://five-embeddev.com/riscv-priv-isa-manual/Priv-v1.12/machine.html#machine-architecture-id-register-marchid
struct riscv_cpuinfo *get_riscv_cpuinfo(void) {
struct riscv_cpuinfo* ci = emalloc(sizeof(struct riscv_cpuinfo));
ci->mvendorid = parse_cpuinfo_field_uint64(CPUINFO_RISCV_MVENDORID);
ci->marchid = parse_cpuinfo_field_uint64(CPUINFO_RISCV_MARCHID);
ci->mimpid = parse_cpuinfo_field_uint64(CPUINFO_RISCV_MIMPID);
if (ci->mvendorid == 0 && ci->mvendorid == 0 && ci->mvendorid == 0)
return NULL;
return ci;
}
char* get_hardware_from_devtree(void) { char* get_hardware_from_devtree(void) {
return get_field_from_devtree(DEVTREE_HARDWARE_FIELD); return get_field_from_devtree(DEVTREE_HARDWARE_FIELD);
} }

View File

@@ -5,8 +5,16 @@
#define UNKNOWN -1 #define UNKNOWN -1
// https://elixir.bootlin.com/linux/v6.10.6/source/arch/riscv/include/asm/cpufeature.h#L21
struct riscv_cpuinfo {
unsigned long mvendorid;
unsigned long marchid;
unsigned long mimpid;
};
char* get_hardware_from_devtree(void); char* get_hardware_from_devtree(void);
char* get_uarch_from_cpuinfo(void); char* get_uarch_from_cpuinfo(void);
char* get_extensions_from_cpuinfo(void); char* get_extensions_from_cpuinfo(void);
struct riscv_cpuinfo *get_riscv_cpuinfo(void);
#endif #endif