From f4aa335af1c20cb9fb6813b2b9a41c7f26314d28 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Tue, 27 Jul 2021 20:26:17 +0200 Subject: [PATCH] [v0.98][PPC] Start PowerPC port. It just compiles but nothing is displayed --- Makefile | 6 ++ src/common/cpu.c | 6 +- src/common/cpu.h | 15 ++++- src/common/global.c | 2 +- src/common/main.c | 3 + src/common/printer.c | 128 +++++++++++++++++++++++++++++++++++++++++-- src/ppc/ppc.c | 28 ++++++++++ src/ppc/ppc.h | 9 +++ src/ppc/uarch.c | 7 +++ src/ppc/uarch.h | 6 ++ 10 files changed, 200 insertions(+), 10 deletions(-) create mode 100644 src/ppc/ppc.c create mode 100644 src/ppc/ppc.h create mode 100644 src/ppc/uarch.c create mode 100644 src/ppc/uarch.h diff --git a/Makefile b/Makefile index b33d800..c68385c 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,13 @@ ifneq ($(OS),Windows_NT) SOURCE += $(COMMON_SRC) $(SRC_DIR)cpuid.c $(SRC_DIR)apic.c $(SRC_DIR)cpuid_asm.c $(SRC_DIR)uarch.c HEADERS += $(COMMON_HDR) $(SRC_DIR)cpuid.h $(SRC_DIR)apic.h $(SRC_DIR)cpuid_asm.h $(SRC_DIR)uarch.h CFLAGS += -DARCH_X86 + else ifeq ($(arch), $(filter $(arch), ppc64el ppc64le ppc32)) + SRC_DIR=src/ppc/ + SOURCE += $(COMMON_SRC) $(SRC_DIR)ppc.c $(SRC_DIR)uarch.c + HEADERS += $(COMMON_HDR) $(SRC_DIR)ppc.h $(SRC_DIR)uarch.h + CFLAGS += -DARCH_PPC else + # Assume ARM 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 diff --git a/src/common/cpu.c b/src/common/cpu.c index cf93868..545b039 100644 --- a/src/common/cpu.c +++ b/src/common/cpu.c @@ -9,6 +9,8 @@ #ifdef ARCH_X86 #include "../x86/uarch.h" +#elif ARCH_PPC + #include "../ppc/uarch.h" #elif ARCH_ARM #include "../arm/uarch.h" #endif @@ -30,11 +32,13 @@ int64_t get_freq(struct frequency* freq) { return freq->max; } -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_PPC) char* get_str_cpu_name(struct cpuInfo* cpu) { return cpu->cpu_name; } +#endif +#ifdef ARCH_X86 char* get_str_sockets(struct topology* topo) { char* string = malloc(sizeof(char) * 2); int32_t sanity_ret = snprintf(string, 2, "%d", topo->sockets); diff --git a/src/common/cpu.h b/src/common/cpu.h index b107257..33b1b84 100644 --- a/src/common/cpu.h +++ b/src/common/cpu.h @@ -94,7 +94,9 @@ struct features { bool SSE4_2; bool FMA3; bool FMA4; - bool SHA; + bool SHA; +#elif ARCH_PPC + bool altivec; #elif ARCH_ARM bool NEON; bool SHA1; @@ -111,7 +113,12 @@ struct cpuInfo { struct cache* cach; struct topology* topo; struct features* feat; - + +#ifdef ARCH_PPC + // CPU name from model + char* cpu_name; +#endif + #ifdef ARCH_X86 // CPU name from model char* cpu_name; @@ -142,6 +149,10 @@ char* get_str_sockets(struct topology* topo); uint32_t get_nsockets(struct topology* topo); #endif +#ifdef ARCH_PPC +char* get_str_cpu_name(struct cpuInfo* cpu); +#endif + VENDOR get_cpu_vendor(struct cpuInfo* cpu); int64_t get_freq(struct frequency* freq); diff --git a/src/common/global.c b/src/common/global.c index 1079431..e4042aa 100644 --- a/src/common/global.c +++ b/src/common/global.c @@ -53,7 +53,7 @@ void printBug(const char *fmt, ...) { vsnprintf(buffer,buffer_size, fmt, args); va_end(args); fprintf(stderr,RED "[ERROR]: "RESET "%s\n",buffer); -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_PPC) fprintf(stderr,"Please, create a new issue with this error message and the output of 'cpufetch --debug' in https://github.com/Dr-Noob/cpufetch/issues\n"); #elif ARCH_ARM fprintf(stderr,"Please, create a new issue with this error message, your smartphone/computer model and the output of 'cpufetch --debug' in https://github.com/Dr-Noob/cpufetch/issues\n"); diff --git a/src/common/main.c b/src/common/main.c index 1737d45..6666cf0 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -9,6 +9,9 @@ #ifdef ARCH_X86 static const char* ARCH_STR = "x86_64 build"; #include "../x86/cpuid.h" +#elif ARCH_PPC + static const char* ARCH_STR = "PowerPC build"; + #include "../ppc/ppc.h" #elif ARCH_ARM static const char* ARCH_STR = "ARM build"; #include "../arm/midr.h" diff --git a/src/common/printer.c b/src/common/printer.c index e193920..9c3100e 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -11,6 +11,8 @@ #ifdef ARCH_X86 #include "../x86/uarch.h" #include "../x86/cpuid.h" +#elif ARCH_PPC + #include "../ppc/ppc.h" #else #include "../arm/uarch.h" #include "../arm/midr.h" @@ -45,7 +47,7 @@ #define COLOR_RESET "\x1b[m" enum { -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_PPC) ATTRIBUTE_NAME, #elif ARCH_ARM ATTRIBUTE_SOC, @@ -72,9 +74,9 @@ enum { }; static const char* ATTRIBUTE_FIELDS [] = { -#ifdef ARCH_X86 +#if defined(ARCH_X86) || defined(ARCH_PPC) "Name:", - #elif ARCH_ARM +#elif ARCH_ARM "SoC:", "", #endif @@ -88,7 +90,7 @@ static const char* ATTRIBUTE_FIELDS [] = { #ifdef ARCH_X86 "AVX:", "FMA:", -#elif ARCH_ARM +#elif defined(ARCH_ARM) || defined(ARCH_PPC) "Features: ", #endif "L1i Size:", @@ -176,6 +178,12 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) { printBug("Invalid CPU vendor in set_ascii (%d)", art->vendor); return NULL; } +#elif ARCH_PPC + COL_FANCY_1 = COLOR_BG_CYAN; + COL_FANCY_2 = COLOR_BG_WHITE; + COL_FANCY_3 = COLOR_FG_CYAN; + COL_FANCY_4 = COLOR_FG_WHITE; + art->ascii_chars[0] = '#'; #elif ARCH_ARM if(art->vendor == SOC_VENDOR_SNAPDRAGON) { COL_FANCY_1 = COLOR_BG_RED; @@ -314,7 +322,9 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) { else if(art->vendor == CPU_VENDOR_AMD) strcpy(tmp, AMD_ASCII); else - strcpy(tmp, UNKNOWN_ASCII); + strcpy(tmp, UNKNOWN_ASCII); +#elif ARCH_PPC + strcpy(tmp, UNKNOWN_ASCII); #elif ARCH_ARM if(art->vendor == SOC_VENDOR_SNAPDRAGON) strcpy(tmp, SNAPDRAGON_ASCII); @@ -507,6 +517,110 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) { } #endif +#ifdef ARCH_PPC +void print_algorithm_ppc(struct ascii* art, int n) { + for(int i=0; i < LINE_SIZE; i++) { + if(art->art[n][i] == '@') + printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset); + else if(art->art[n][i] == '#') + printf("%s%c%s", art->color2_ascii, art->ascii_chars[1], art->reset); + else + printf("%c",art->art[n][i]); + } +} + +void print_ascii_ppc(struct ascii* art, uint32_t la) { + int attr_to_print = 0; + int attr_type; + char* attr_value; + uint32_t space_right; + uint32_t space_up = (NUMBER_OF_LINES - art->n_attributes_set)/2; + uint32_t space_down = NUMBER_OF_LINES - art->n_attributes_set - space_up; + bool flag = false; + + printf("\n"); + for(uint32_t n=0;n space_up-1 && n < NUMBER_OF_LINES-space_down) { + attr_type = art->attributes[attr_to_print]->type; + attr_value = art->attributes[attr_to_print]->value; + attr_to_print++; + + space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_type])); + printf("%s%s%s%*s%s%s%s\n", art->color1_text, ATTRIBUTE_FIELDS[attr_type], art->reset, space_right, "", art->color2_text, attr_value, art->reset); + } + else printf("\n"); + } + printf("\n"); +} + +void print_ascii(struct ascii* art) { + uint32_t longest_attribute = longest_attribute_length(art); + + print_ascii_ppc(art, longest_attribute); +} + +bool print_cpufetch_ppc(struct cpuInfo* cpu, STYLE s, struct colors* cs) { + struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs); + if(art == NULL) + return false; + + /*char* uarch = get_str_uarch(cpu); + char* manufacturing_process = get_str_process(cpu); + char* sockets = get_str_sockets(cpu->topo); + char* max_frequency = get_str_freq(cpu->freq); + char* n_cores = get_str_topology(cpu, cpu->topo, false); + char* n_cores_dual = get_str_topology(cpu, cpu->topo, true); + */ + char* cpu_name = get_str_cpu_name(cpu); + + /* + char* l1i = get_str_l1i(cpu->cach); + char* l1d = get_str_l1d(cpu->cach); + char* l2 = get_str_l2(cpu->cach); + char* l3 = get_str_l3(cpu->cach); + char* pp = get_str_peak_performance(cpu,cpu->topo,get_freq(cpu->freq));*/ + + setAttribute(art,ATTRIBUTE_NAME,cpu_name); + /* + if(cpu->hv->present) { + setAttribute(art, ATTRIBUTE_HYPERVISOR, cpu->hv->hv_name); + } + setAttribute(art,ATTRIBUTE_UARCH,uarch); + setAttribute(art,ATTRIBUTE_TECHNOLOGY,manufacturing_process); + setAttribute(art,ATTRIBUTE_FREQUENCY,max_frequency); + uint32_t socket_num = get_nsockets(cpu->topo); + if (socket_num > 1) { + setAttribute(art, ATTRIBUTE_SOCKETS, sockets); + setAttribute(art, ATTRIBUTE_NCORES,n_cores); + setAttribute(art, ATTRIBUTE_NCORES_DUAL, n_cores_dual); + } + else { + setAttribute(art,ATTRIBUTE_NCORES,n_cores); + } + setAttribute(art,ATTRIBUTE_AVX,avx); + setAttribute(art,ATTRIBUTE_FMA,fma); + setAttribute(art,ATTRIBUTE_L1i,l1i); + setAttribute(art,ATTRIBUTE_L1d,l1d); + setAttribute(art,ATTRIBUTE_L2,l2); + if(l3 != NULL) { + setAttribute(art,ATTRIBUTE_L3,l3); + } + setAttribute(art,ATTRIBUTE_PEAK,pp); + */ + + if(art->n_attributes_set > NUMBER_OF_LINES) { + printBug("The number of attributes set is bigger than the max that can be displayed"); + return false; + } + + print_ascii(art); + + return true; +} +#endif + #ifdef ARCH_ARM void print_algorithm_snapd_mtk(struct ascii* art, int n) { for(int i=0; i < LINE_SIZE; i++) { @@ -715,7 +829,9 @@ bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) { #ifdef ARCH_X86 return print_cpufetch_x86(cpu, s, cs); +#elif ARCH_PPC + return print_cpufetch_ppc(cpu, s, cs); #elif ARCH_ARM - return print_cpufetch_arm(cpu, s, cs); + return print_cpufetch_arm(cpu, s, cs); #endif } diff --git a/src/ppc/ppc.c b/src/ppc/ppc.c new file mode 100644 index 0000000..64f6255 --- /dev/null +++ b/src/ppc/ppc.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "ppc.h" + +#define STRING_UNKNOWN "Unknown" + +struct cpuInfo* get_cpu_info() { + struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo)); + struct features* feat = malloc(sizeof(struct features)); + cpu->feat = feat; + + bool *ptr = &(feat->AES); + for(uint32_t i = 0; i < sizeof(struct features)/sizeof(bool); i++, ptr++) { + *ptr = false; + } + + cpu->cpu_name = malloc(sizeof(char) * strlen(STRING_UNKNOWN) + 1); + snprintf(cpu->cpu_name, strlen(STRING_UNKNOWN) + 1, STRING_UNKNOWN); + + return cpu; +} + +void print_debug(struct cpuInfo* cpu) { + printf("TODO\n"); +} diff --git a/src/ppc/ppc.h b/src/ppc/ppc.h new file mode 100644 index 0000000..d19ca87 --- /dev/null +++ b/src/ppc/ppc.h @@ -0,0 +1,9 @@ +#ifndef __POWERPC__ +#define __POWERPC__ + +#include "../common/cpu.h" + +struct cpuInfo* get_cpu_info(); +void print_debug(struct cpuInfo* cpu); + +#endif diff --git a/src/ppc/uarch.c b/src/ppc/uarch.c new file mode 100644 index 0000000..865aa10 --- /dev/null +++ b/src/ppc/uarch.c @@ -0,0 +1,7 @@ +struct uarch { + int d; +}; + +void free_uarch_struct(struct uarch* arch) { + +} diff --git a/src/ppc/uarch.h b/src/ppc/uarch.h new file mode 100644 index 0000000..0fa2d44 --- /dev/null +++ b/src/ppc/uarch.h @@ -0,0 +1,6 @@ +#ifndef __UARCH__ +#define __UARCH__ + +void free_uarch_struct(struct uarch* arch); + +#endif