diff --git a/Makefile b/Makefile index 6ecfbbd..b21dfcb 100644 --- a/Makefile +++ b/Makefile @@ -32,8 +32,8 @@ ifneq ($(OS),Windows_NT) CFLAGS += -DARCH_PPC -std=gnu99 -fstack-protector-all -Wno-language-extension-token else ifeq ($(arch), $(filter $(arch), arm aarch64_be aarch64 arm64 armv8b armv8l armv7l armv6l)) SRC_DIR=src/arm/ - SOURCE += $(COMMON_SRC) $(SRC_DIR)midr.c $(SRC_DIR)uarch.c $(SRC_DIR)soc.c $(SRC_DIR)udev.c - HEADERS += $(COMMON_HDR) $(SRC_DIR)midr.h $(SRC_DIR)uarch.h $(SRC_DIR)soc.h $(SRC_DIR)udev.c $(SRC_DIR)socs.h + SOURCE += $(COMMON_SRC) $(SRC_DIR)midr.c $(SRC_DIR)uarch.c $(SRC_COMMON)soc.c $(SRC_DIR)soc.c $(SRC_DIR)udev.c + HEADERS += $(COMMON_HDR) $(SRC_DIR)midr.h $(SRC_DIR)uarch.h $(SRC_COMMON)soc.h $(SRC_DIR)soc.h $(SRC_DIR)udev.c $(SRC_DIR)socs.h CFLAGS += -DARCH_ARM -Wno-unused-parameter -std=c99 -fstack-protector-all os := $(shell uname -s) @@ -43,8 +43,8 @@ ifneq ($(OS),Windows_NT) endif else ifeq ($(arch), $(filter $(arch), riscv64 riscv32)) SRC_DIR=src/riscv/ - SOURCE += $(COMMON_SRC) $(SRC_DIR)riscv.c $(SRC_DIR)uarch.c $(SRC_DIR)soc.c $(SRC_DIR)udev.c - HEADERS += $(COMMON_HDR) $(SRC_DIR)riscv.h $(SRC_DIR)uarch.h $(SRC_DIR)soc.h $(SRC_DIR)udev.h $(SRC_DIR)socs.h + SOURCE += $(COMMON_SRC) $(SRC_DIR)riscv.c $(SRC_DIR)uarch.c $(SRC_COMMON)soc.c $(SRC_DIR)soc.c $(SRC_DIR)udev.c + HEADERS += $(COMMON_HDR) $(SRC_DIR)riscv.h $(SRC_DIR)uarch.h $(SRC_COMMON)soc.h $(SRC_DIR)soc.h $(SRC_DIR)udev.h $(SRC_DIR)socs.h CFLAGS += -DARCH_RISCV -Wno-unused-parameter -std=c99 -fstack-protector-all else # Error lines should not be tabulated because Makefile complains about it diff --git a/src/arm/midr.c b/src/arm/midr.c index 374e90a..90026bd 100644 --- a/src/arm/midr.c +++ b/src/arm/midr.c @@ -13,10 +13,10 @@ #endif #include "../common/global.h" +#include "../common/soc.h" #include "udev.h" #include "midr.h" #include "uarch.h" -#include "soc.h" bool cores_are_equal(int c1pos, int c2pos, uint32_t* midr_array, int32_t* freq_array) { return midr_array[c1pos] == midr_array[c2pos] && freq_array[c1pos] == freq_array[c2pos]; diff --git a/src/arm/soc.c b/src/arm/soc.c index 4f07827..14c7a93 100644 --- a/src/arm/soc.c +++ b/src/arm/soc.c @@ -15,17 +15,6 @@ #define min(a,b) (((a)<(b))?(a):(b)) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) -static char* soc_trademark_string[] = { - [SOC_VENDOR_SNAPDRAGON] = "Snapdragon ", - [SOC_VENDOR_MEDIATEK] = "MediaTek ", - [SOC_VENDOR_EXYNOS] = "Exynos ", - [SOC_VENDOR_KIRIN] = "Kirin ", - [SOC_VENDOR_BROADCOM] = "Broadcom BCM", - [SOC_VENDOR_APPLE] = "Apple ", - [SOC_VENDOR_ALLWINNER] = "Allwinner ", - [SOC_VENDOR_ROCKCHIP] = "Rockchip ", -}; - static char* soc_rpi_string[] = { "BCM2835", "BCM2836", @@ -33,30 +22,6 @@ static char* soc_rpi_string[] = { "BCM2711" }; -void fill_soc(struct system_on_chip* soc, char* soc_name, SOC soc_model, int32_t process) { - soc->soc_model = soc_model; - soc->soc_vendor = get_soc_vendor_from_soc(soc_model); - soc->process = process; - int len = strlen(soc_name) + strlen(soc_trademark_string[soc->soc_vendor]) + 1; - soc->soc_name = emalloc(sizeof(char) * len); - memset(soc->soc_name, 0, sizeof(char) * len); - sprintf(soc->soc_name, "%s%s", soc_trademark_string[soc->soc_vendor], soc_name); -} - -bool match_soc(struct system_on_chip* soc, char* raw_name, char* expected_name, char* soc_name, SOC soc_model, int32_t process) { - int len1 = strlen(raw_name); - int len2 = strlen(expected_name); - int len = min(len1, len2); - - if(strncmp(raw_name, expected_name, len) != 0) { - return false; - } - else { - fill_soc(soc, soc_name, soc_model, process); - return true; - } -} - char* toupperstr(char* str) { int len = strlen(str) + 1; char* ret = emalloc(sizeof(char) * len); @@ -123,11 +88,7 @@ bool get_sunxisoc_from_sid(struct system_on_chip* soc, char* raw_name, uint32_t return false; } -#define SOC_START if (false) {} -#define SOC_EQ(raw_name, expected_name, soc_name, soc_model, soc, process) \ - else if (match_soc(soc, raw_name, expected_name, soc_name, soc_model, process)) return true; -#define SOC_END else { return false; } -// Exynos special define +// Exynos special define (not included in src/common/soc.h) #define SOC_EXY_EQ(raw_name, tmpsoc, soc_name, soc_model, soc, process) \ sprintf(tmpsoc, "exynos%s", soc_name); \ if (match_soc(soc, raw_name, tmpsoc, soc_name, soc_model, process)) return true; \ @@ -871,28 +832,3 @@ struct system_on_chip* get_soc(void) { return soc; } - -char* get_soc_name(struct system_on_chip* soc) { - if(soc->soc_model == SOC_MODEL_UNKNOWN) - return soc->raw_name; - return soc->soc_name; -} - -VENDOR get_soc_vendor(struct system_on_chip* soc) { - return soc->soc_vendor; -} - -char* get_str_process(struct system_on_chip* soc) { - char* str; - - if(soc->process == UNKNOWN) { - str = emalloc(sizeof(char) * (strlen(STRING_UNKNOWN)+1)); - snprintf(str, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); - } - else { - str = emalloc(sizeof(char) * 5); - memset(str, 0, sizeof(char) * 5); - snprintf(str, 5, "%dnm", soc->process); - } - return str; -} diff --git a/src/arm/soc.h b/src/arm/soc.h index a8ffc53..b2607e7 100644 --- a/src/arm/soc.h +++ b/src/arm/soc.h @@ -1,34 +1,10 @@ -#ifndef __SOC__ -#define __SOC__ +#ifndef __SOC_ARM__ +#define __SOC_ARM__ #include "../common/cpu.h" +#include "../common/soc.h" #include -typedef int32_t SOC; - -enum { - SOC_VENDOR_UNKNOWN, - SOC_VENDOR_SNAPDRAGON, - SOC_VENDOR_MEDIATEK, - SOC_VENDOR_EXYNOS, - SOC_VENDOR_KIRIN, - SOC_VENDOR_BROADCOM, - SOC_VENDOR_APPLE, - SOC_VENDOR_ALLWINNER, - SOC_VENDOR_ROCKCHIP -}; - -struct system_on_chip { - SOC soc_model; - VENDOR soc_vendor; - int32_t process; - char* soc_name; - char* raw_name; -}; - struct system_on_chip* get_soc(void); -char* get_soc_name(struct system_on_chip* soc); -VENDOR get_soc_vendor(struct system_on_chip* soc); -char* get_str_process(struct system_on_chip* soc); #endif diff --git a/src/common/printer.c b/src/common/printer.c index 998ccb4..414dc66 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -20,6 +20,7 @@ #include "../arm/uarch.h" #include "../arm/midr.h" #include "../arm/soc.h" + #include "../common/soc.h" #elif ARCH_RISCV #include "../riscv/riscv.h" #include "../riscv/uarch.h" diff --git a/src/common/soc.c b/src/common/soc.c new file mode 100644 index 0000000..2e859c8 --- /dev/null +++ b/src/common/soc.c @@ -0,0 +1,75 @@ +#include "soc.h" +#ifdef ARCH_ARM + #include "../arm/socs.h" +#elif ARCH_RISCV + #include "../riscv/socs.h" +#endif +#include "udev.h" +#include "../common/global.h" + +#include + +static char* soc_trademark_string[] = { + // ARM + [SOC_VENDOR_SNAPDRAGON] = "Snapdragon ", + [SOC_VENDOR_MEDIATEK] = "MediaTek ", + [SOC_VENDOR_EXYNOS] = "Exynos ", + [SOC_VENDOR_KIRIN] = "Kirin ", + [SOC_VENDOR_BROADCOM] = "Broadcom BCM", + [SOC_VENDOR_APPLE] = "Apple ", + [SOC_VENDOR_ROCKCHIP] = "Rockchip ", + // RISC-V + [SOC_VENDOR_SIFIVE] = "SiFive ", + [SOC_VENDOR_STARFIVE] = "StarFive ", + // ARM & RISC-V + [SOC_VENDOR_ALLWINNER] = "Allwinner " +}; + +VENDOR get_soc_vendor(struct system_on_chip* soc) { + return soc->soc_vendor; +} + +char* get_str_process(struct system_on_chip* soc) { + char* str; + + if(soc->process == UNKNOWN) { + str = emalloc(sizeof(char) * (strlen(STRING_UNKNOWN)+1)); + snprintf(str, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); + } + else { + str = emalloc(sizeof(char) * 5); + memset(str, 0, sizeof(char) * 5); + snprintf(str, 5, "%dnm", soc->process); + } + return str; +} + +char* get_soc_name(struct system_on_chip* soc) { + if(soc->soc_model == SOC_MODEL_UNKNOWN) + return soc->raw_name; + return soc->soc_name; +} + +void fill_soc(struct system_on_chip* soc, char* soc_name, SOC soc_model, int32_t process) { + soc->soc_model = soc_model; + soc->soc_vendor = get_soc_vendor_from_soc(soc_model); + soc->process = process; + int len = strlen(soc_name) + strlen(soc_trademark_string[soc->soc_vendor]) + 1; + soc->soc_name = emalloc(sizeof(char) * len); + memset(soc->soc_name, 0, sizeof(char) * len); + sprintf(soc->soc_name, "%s%s", soc_trademark_string[soc->soc_vendor], soc_name); +} + +bool match_soc(struct system_on_chip* soc, char* raw_name, char* expected_name, char* soc_name, SOC soc_model, int32_t process) { + int len1 = strlen(raw_name); + int len2 = strlen(expected_name); + int len = min(len1, len2); + + if(strncmp(raw_name, expected_name, len) != 0) { + return false; + } + else { + fill_soc(soc, soc_name, soc_model, process); + return true; + } +} diff --git a/src/common/soc.h b/src/common/soc.h new file mode 100644 index 0000000..5b1d0b2 --- /dev/null +++ b/src/common/soc.h @@ -0,0 +1,52 @@ +#ifndef __SOC__ +#define __SOC__ + +// NOTE: +// soc.c/soc.h are used by +// ARM and RISC-V backends + +#include "../common/cpu.h" +#include + +#define UNKNOWN -1 + +typedef int32_t SOC; + +enum { + SOC_VENDOR_UNKNOWN, + // ARM + SOC_VENDOR_SNAPDRAGON, + SOC_VENDOR_MEDIATEK, + SOC_VENDOR_EXYNOS, + SOC_VENDOR_KIRIN, + SOC_VENDOR_BROADCOM, + SOC_VENDOR_APPLE, + SOC_VENDOR_ROCKCHIP, + // RISC-V + SOC_VENDOR_SIFIVE, + SOC_VENDOR_STARFIVE, + // ARM & RISC-V + SOC_VENDOR_ALLWINNER +}; + +struct system_on_chip { + SOC soc_model; + VENDOR soc_vendor; + int32_t process; + char* soc_name; + char* raw_name; +}; + +struct system_on_chip* get_soc(void); +char* get_soc_name(struct system_on_chip* soc); +VENDOR get_soc_vendor(struct system_on_chip* soc); +bool match_soc(struct system_on_chip* soc, char* raw_name, char* expected_name, char* soc_name, SOC soc_model, int32_t process); +char* get_str_process(struct system_on_chip* soc); +void fill_soc(struct system_on_chip* soc, char* soc_name, SOC soc_model, int32_t process); + +#define SOC_START if (false) {} +#define SOC_EQ(raw_name, expected_name, soc_name, soc_model, soc, process) \ + else if (match_soc(soc, raw_name, expected_name, soc_name, soc_model, process)) return true; +#define SOC_END else { return false; } + +#endif diff --git a/src/riscv/soc.c b/src/riscv/soc.c index 6f3c7c9..fdce84a 100644 --- a/src/riscv/soc.c +++ b/src/riscv/soc.c @@ -5,66 +5,6 @@ #include -VENDOR get_soc_vendor(struct system_on_chip* soc) { - return soc->soc_vendor; -} - -char* get_str_process(struct system_on_chip* soc) { - char* str; - - if(soc->process == UNKNOWN) { - str = emalloc(sizeof(char) * (strlen(STRING_UNKNOWN)+1)); - snprintf(str, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); - } - else { - str = emalloc(sizeof(char) * 5); - memset(str, 0, sizeof(char) * 5); - snprintf(str, 5, "%dnm", soc->process); - } - return str; -} - -char* get_soc_name(struct system_on_chip* soc) { - if(soc->soc_model == SOC_MODEL_UNKNOWN) - return soc->raw_name; - return soc->soc_name; -} - -static char* soc_trademark_string[] = { - [SOC_VENDOR_SIFIVE] = "SiFive ", - [SOC_VENDOR_STARFIVE] = "StarFive ", - [SOC_VENDOR_ALLWINNER] = "Allwinner " -}; - -void fill_soc(struct system_on_chip* soc, char* soc_name, SOC soc_model, int32_t process) { - soc->soc_model = soc_model; - soc->soc_vendor = get_soc_vendor_from_soc(soc_model); - soc->process = process; - int len = strlen(soc_name) + strlen(soc_trademark_string[soc->soc_vendor]) + 1; - soc->soc_name = emalloc(sizeof(char) * len); - memset(soc->soc_name, 0, sizeof(char) * len); - sprintf(soc->soc_name, "%s%s", soc_trademark_string[soc->soc_vendor], soc_name); -} - -bool match_soc(struct system_on_chip* soc, char* raw_name, char* expected_name, char* soc_name, SOC soc_model, int32_t process) { - int len1 = strlen(raw_name); - int len2 = strlen(expected_name); - int len = min(len1, len2); - - if(strncmp(raw_name, expected_name, len) != 0) { - return false; - } - else { - fill_soc(soc, soc_name, soc_model, process); - return true; - } -} - -#define SOC_START if (false) {} -#define SOC_EQ(raw_name, expected_name, soc_name, soc_model, soc, process) \ - else if (match_soc(soc, raw_name, expected_name, soc_name, soc_model, process)) return true; -#define SOC_END else { return false; } - bool match_sifive(char* soc_name, struct system_on_chip* soc) { char* tmp = soc_name; diff --git a/src/riscv/soc.h b/src/riscv/soc.h index b6ba2dd..cd1925f 100644 --- a/src/riscv/soc.h +++ b/src/riscv/soc.h @@ -1,29 +1,10 @@ -#ifndef __SOC__ -#define __SOC__ +#ifndef __SOC_RISCV__ +#define __SOC_RISCV__ +#include "../common/soc.h" #include "../common/cpu.h" #include -typedef int32_t SOC; - -enum { - SOC_VENDOR_UNKNOWN, - SOC_VENDOR_SIFIVE, - SOC_VENDOR_STARFIVE, - SOC_VENDOR_ALLWINNER -}; - -struct system_on_chip { - SOC soc_model; - VENDOR soc_vendor; - int32_t process; - char* soc_name; - char* raw_name; -}; - struct system_on_chip* get_soc(void); -char* get_soc_name(struct system_on_chip* soc); -VENDOR get_soc_vendor(struct system_on_chip* soc); -char* get_str_process(struct system_on_chip* soc); #endif