Skip to content

Commit

Permalink
[v1.06][ARM] Add support for Apple SoCs in Asahi Linux (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Noob committed Aug 23, 2024
1 parent 321a1ec commit 7689355
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/arm/soc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/arm/uarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/common/udev.c
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/common/udev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7689355

Please sign in to comment.