From 5560190b36a75d9f943c2455cee7997378ab3565 Mon Sep 17 00:00:00 2001 From: Nick Chan Date: Mon, 23 Sep 2024 23:20:27 +0800 Subject: [PATCH] utils: Add more functions to detect CPU features These functions will be used to skip over parts not supported by A7-A11 SoCs. Signed-off-by: Nick Chan --- src/cpu_regs.h | 10 ++++++++++ src/main.c | 13 +++++++++++-- src/string.c | 24 ++++++++++++++++++++++++ src/utils.c | 18 ++++++++++++++++++ src/utils.h | 4 ++++ sysinc/string.h | 1 + 6 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/cpu_regs.h b/src/cpu_regs.h index c10ca4a5..98cce409 100644 --- a/src/cpu_regs.h +++ b/src/cpu_regs.h @@ -629,3 +629,13 @@ /* Lockdown registers */ #define SYS_IMP_APL_SPR_LOCKDOWN_EL2 sys_reg(3, 4, 15, 0, 5) #define SYS_IMP_APL_SPR_LOCKDOWN_EL1 sys_reg(3, 4, 15, 0, 6) + +/* Feature ID registers */ +#define AIDR_EL1_MUL53 BIT(0) +#define AIDR_EL1_WKDM BIT(1) +#define AIDR_EL1_ARCH_RETENTION BIT(2) +#define AIDR_EL1_AMX BIT(4) +#define AIDR_EL1_TSO BIT(9) +#define AIDR_EL1_GXF BIT(16) +#define AIDR_EL1_APFLG BIT(19) +#define AIDR_EL1_PSRV BIT(20) diff --git a/src/main.c b/src/main.c index 66c05ba9..d922cb18 100644 --- a/src/main.c +++ b/src/main.c @@ -39,9 +39,18 @@ u32 board_id = ~0, chip_id = ~0; void get_device_info(void) { + const char *model = (const char *)adt_getprop(adt, 0, "model", NULL); + const char *target = (const char *)adt_getprop(adt, 0, "target-type", NULL); + printf("Device info:\n"); - printf(" Model: %s\n", (const char *)adt_getprop(adt, 0, "model", NULL)); - printf(" Target: %s\n", (const char *)adt_getprop(adt, 0, "target-type", NULL)); + + if (model) + printf(" Model: %s\n", model); + + if (target) + printf(" Target: %s\n", target); + + is_mac = !!strstr(model, "Mac"); int chosen = adt_path_offset(adt, "/chosen"); if (chosen > 0) { diff --git a/src/string.c b/src/string.c index 318d0fcb..8681be33 100644 --- a/src/string.c +++ b/src/string.c @@ -188,6 +188,30 @@ char *strrchr(const char *s, int c) return NULL; } +char *strstr(const char *s1, const char *s2) +{ + const char *p1 = s1; + const char *p2; + + while (*s1) { + p2 = s2; + + while (*p2 && (*p1 == *p2)) { + ++p1; + ++p2; + } + + if (!*p2) { + return (char *)s1; + } + + ++s1; + p1 = s1; + } + + return NULL; +} + /* Very naive, no attempt to check for errors */ long atol(const char *s) { diff --git a/src/utils.c b/src/utils.c index ff8dd84b..3ed569fe 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,12 +4,15 @@ #include #include "utils.h" +#include "cpu_regs.h" #include "iodev.h" #include "smp.h" #include "types.h" #include "vsprintf.h" #include "xnuboot.h" +bool is_mac = false; + static char ascii(char s) { if (s < 0x20) @@ -181,6 +184,21 @@ bool is_heap(void *addr) return p > top_of_kernel_data && p < top_of_ram; } +bool supports_arch_retention(void) +{ + return mrs(AIDR_EL1) & AIDR_EL1_ARCH_RETENTION; +} + +bool supports_gxf(void) +{ + return mrs(AIDR_EL1) & AIDR_EL1_GXF; +} + +bool supports_pan(void) +{ + return (mrs(ID_AA64MMFR1_EL1) >> 20) & 0xf; +} + // TODO: update mapping? u64 top_of_memory_alloc(size_t size) { diff --git a/src/utils.h b/src/utils.h index f4646a53..fe3eb1b3 100644 --- a/src/utils.h +++ b/src/utils.h @@ -429,6 +429,7 @@ struct vector_args { }; extern u32 board_id, chip_id; +extern bool is_mac; extern bool cpufeat_actlr_el2; extern struct vector_args next_stage; @@ -437,6 +438,9 @@ void cpu_sleep(bool deep) __attribute__((noreturn)); void deep_wfi(void); bool is_heap(void *addr); +bool supports_arch_retention(void); +bool supports_gxf(void); +bool supports_pan(void); u64 top_of_memory_alloc(size_t size); #endif diff --git a/sysinc/string.h b/sysinc/string.h index 1aa9e267..def429c1 100644 --- a/sysinc/string.h +++ b/sysinc/string.h @@ -18,6 +18,7 @@ size_t strlen(const char *s); size_t strnlen(const char *s, size_t n); char *strchr(const char *s, int c); char *strrchr(const char *s, int c); +char *strstr(const char *s1, const char *s2); long atol(const char *s); static inline int tolower(int c)