From 7689355a721cc86d70103fc130271ad2229cc9e1 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Fri, 23 Aug 2024 08:37:13 +0100 Subject: [PATCH] [v1.06][ARM] Add support for Apple SoCs in Asahi Linux (#263) --- src/arm/soc.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++ src/arm/uarch.c | 2 ++ src/common/udev.c | 13 ++++++++ src/common/udev.h | 1 + 4 files changed, 93 insertions(+) diff --git a/src/arm/soc.c b/src/arm/soc.c index c76a876..868ac26 100644 --- a/src/arm/soc.c +++ b/src/arm/soc.c @@ -895,6 +895,79 @@ struct system_on_chip* guess_soc_from_uarch(struct system_on_chip* soc, struct c return soc; } +// Return the dt string without the NULL characters. +char* get_dt_str(char* dt, int filelen) { + char* dt_without_null = (char *) malloc(sizeof(char) * filelen); + memcpy(dt_without_null, dt, filelen); + + for (int i=0; i < filelen-1; i++) { + if (dt_without_null[i] == '\0') + dt_without_null[i] = ','; + } + return dt_without_null; +} + +bool match_dt(struct system_on_chip* soc, char* dt, int filelen, char* expected_name, char* soc_name, SOC soc_model, int32_t process) { + // The /proc/device-tree/compatible file (passed by dt) uses NULL + // to separate the strings, so we need to make an special case here + // and iterate over the NULL characters, thus iterating over each + // individual compatible strings. + + if (strstr(dt, expected_name) != NULL) { + fill_soc(soc, soc_name, soc_model, process); + return true; + } + + char *compatible = dt; + char *end_of_dt = dt + filelen; + + while ((compatible = strchr(compatible, '\0')) != end_of_dt) { + compatible++; + if (strstr(compatible, expected_name) != NULL) { + fill_soc(soc, soc_name, soc_model, process); + return true; + } + } + + return false; +} + +#define DT_START if (false) {} +#define DT_EQ(dt, filelen, soc, expected_name, soc_name, soc_model, process) \ + else if (match_dt(soc, dt, filelen, expected_name, soc_name, soc_model, process)) return soc; +#define DT_END(dt, filelen) else { printWarn("guess_soc_from_devtree: No match found for '%s'", get_dt_str(dt, filelen)); return soc; } + +// TODO: Move this to doc +// The number of fields seems non-standard, so for now it seems wiser +// to just get the entire string with all fields and just look for the +// substring. +// TODO: Implement this by going trough NULL-separated fields rather than +// using strstr. +struct system_on_chip* guess_soc_from_devtree(struct system_on_chip* soc) { + int len; + char* dt = get_devtree_compatible(&len); + if (dt == NULL) { + return soc; + } + + // The following are internal codenames of Asahi Linux + // https://github.com/AsahiLinux/docs/wiki/Codenames + DT_START + DT_EQ(dt, len, soc, "apple,t8103", "M1", SOC_APPLE_M1, 5) + DT_EQ(dt, len, soc, "apple,t6000", "M1 Pro", SOC_APPLE_M1_PRO, 5) + DT_EQ(dt, len, soc, "apple,t6001", "M1 Max", SOC_APPLE_M1_MAX, 5) + DT_EQ(dt, len, soc, "apple,t6002", "M1 Ultra", SOC_APPLE_M1_ULTRA, 5) + DT_EQ(dt, len, soc, "apple,t8112", "M2", SOC_APPLE_M2, 5) + DT_EQ(dt, len, soc, "apple,t6020", "M2 Pro", SOC_APPLE_M2_PRO, 5) + DT_EQ(dt, len, soc, "apple,t6021", "M2 Max", SOC_APPLE_M2_MAX, 5) + DT_EQ(dt, len, soc, "apple,t6022", "M2 Ultra", SOC_APPLE_M2_ULTRA, 5) + DT_EQ(dt, len, soc, "apple,t8122", "M3", SOC_APPLE_M3, 3) + DT_EQ(dt, len, soc, "apple,t6030", "M3 Pro", SOC_APPLE_M3_PRO, 3) + DT_EQ(dt, len, soc, "apple,t6031", "M3 Max", SOC_APPLE_M3_MAX, 3) + DT_EQ(dt, len, soc, "apple,t6034", "M3 Max", SOC_APPLE_M3_MAX, 3) + DT_END(dt, len) +} + struct system_on_chip* guess_soc_from_pci(struct system_on_chip* soc, struct cpuInfo* cpu) { struct pci_devices * pci = get_pci_devices(); if (pci == NULL) { @@ -1103,6 +1176,10 @@ struct system_on_chip* get_soc(struct cpuInfo* cpu) { printWarn("SoC detection failed using Android: Found '%s' string", soc->raw_name); } #endif // ifdef __ANDROID__ + // If previous steps failed, try with the device tree + if (soc->soc_vendor == SOC_VENDOR_UNKNOWN) { + soc = guess_soc_from_devtree(soc); + } // If previous steps failed, try with nvmem if(soc->soc_vendor == SOC_VENDOR_UNKNOWN) { soc = guess_soc_from_nvmem(soc); diff --git a/src/arm/uarch.c b/src/arm/uarch.c index 32d6e33..08865b9 100644 --- a/src/arm/uarch.c +++ b/src/arm/uarch.c @@ -248,6 +248,8 @@ struct uarch* get_uarch_from_midr(uint32_t midr, struct cpuInfo* cpu) { CHECK_UARCH(arch, cpu, 'a', 0x022, NA, NA, "Icestorm", UARCH_ICESTORM, CPU_VENDOR_APPLE) CHECK_UARCH(arch, cpu, 'a', 0x023, NA, NA, "Firestorm", UARCH_FIRESTORM, CPU_VENDOR_APPLE) + CHECK_UARCH(arch, cpu, 'a', 0x024, NA, NA, "Icestorm", UARCH_ICESTORM, CPU_VENDOR_APPLE) // https://github.com/Dr-Noob/cpufetch/issues/263 + CHECK_UARCH(arch, cpu, 'a', 0x025, NA, NA, "Firestorm", UARCH_FIRESTORM, CPU_VENDOR_APPLE) // https://github.com/Dr-Noob/cpufetch/issues/263 CHECK_UARCH(arch, cpu, 'a', 0x030, NA, NA, "Blizzard", UARCH_BLIZZARD, CPU_VENDOR_APPLE) CHECK_UARCH(arch, cpu, 'a', 0x031, NA, NA, "Avalanche", UARCH_AVALANCHE, CPU_VENDOR_APPLE) CHECK_UARCH(arch, cpu, 'a', 0x048, NA, NA, "Sawtooth", UARCH_SAWTOOTH, CPU_VENDOR_APPLE) diff --git a/src/common/udev.c b/src/common/udev.c index 953ae71..d7eb1f3 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -1,7 +1,10 @@ +#include "../common/global.h" #include "udev.h" #include "global.h" #include "cpu.h" +#define _PATH_DEVTREE "/proc/device-tree/compatible" + // https://www.kernel.org/doc/html/latest/core-api/cpu_hotplug.html int get_ncores_from_cpuinfo(void) { // Examples: @@ -349,3 +352,13 @@ bool is_devtree_compatible(char* str) { } return true; } + +char* get_devtree_compatible(int *filelen) { + char* buf; + + if ((buf = read_file(_PATH_DEVTREE, filelen)) == NULL) { + printWarn("read_file: %s: %s", _PATH_DEVTREE, strerror(errno)); + } + + return buf; +} diff --git a/src/common/udev.h b/src/common/udev.h index f2c4c2e..92ff1d1 100644 --- a/src/common/udev.h +++ b/src/common/udev.h @@ -43,5 +43,6 @@ int get_num_sockets_package_cpus(struct topology* topo); int get_ncores_from_cpuinfo(void); char* get_field_from_cpuinfo(char* CPUINFO_FIELD); bool is_devtree_compatible(char* str); +char* get_devtree_compatible(int *filelen); #endif