From 14c745f7cb0e78384eb391356ce27e55298d2e85 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Sat, 11 Oct 2025 16:38:32 +0200 Subject: [PATCH] Working on the skeleton of HSA, fetching name works, need to work on printer next --- src/common/ascii.hpp | 40 +++++++++++++++++++++++++ src/common/gpu.hpp | 1 + src/common/master.cpp | 5 ++++ src/common/printer.cpp | 23 +++++++++++++- src/hsa/hsa.cpp | 68 +++++++++++++++++++++++++++++++----------- src/hsa/hsa.hpp | 1 - 6 files changed, 118 insertions(+), 20 deletions(-) diff --git a/src/common/ascii.hpp b/src/common/ascii.hpp index 00a8254..152395c 100644 --- a/src/common/ascii.hpp +++ b/src/common/ascii.hpp @@ -34,6 +34,23 @@ $C2## ## ## ## ## ## ## ## #: :# \ $C2## ## ## ## ## ## ## ## ####### \ $C2## ## ### ## ###### ## ## ## " +#define ASCII_AMD \ +"$C2 '############### \ +$C2 ,############# \ +$C2 .#### \ +$C2 #. .#### \ +$C2 :##. .#### \ +$C2 :###. .#### \ +$C2 #########. :## \ +$C2 #######. ; \ +$C1 \ +$C1 ### ### ### ####### \ +$C1 ## ## ##### ##### ## ## \ +$C1 ## ## ### #### ### ## ## \ +$C1 ######### ### ## ### ## ## \ +$C1## ## ### ### ## ## \ +$C1## ## ### ### ####### " + #define ASCII_INTEL \ "$C1 .#################. \ $C1 .#### ####. \ @@ -68,6 +85,27 @@ $C1 olcc::; ,:ccloMMMMMMMMM \ $C1 :......oMMMMMMMMMMMMMMMMMMMMMM \ $C1 :lllMMMMMMMMMMMMMMMMMMMMMMMMMM " +#define ASCII_AMD_L \ +"$C1 \ +$C1 \ +$C1 \ +$C1 \ +$C1 \ +$C1 \ +$C1 @@@@ @@@ @@@ @@@@@@@@ $C2 ############ \ +$C1 @@@@@@ @@@@@ @@@@@ @@@ @@@ $C2 ########## \ +$C1 @@@ @@@ @@@@@@@@@@@@@ @@@ @@ $C2 # ##### \ +$C1 @@@ @@@ @@@ @@@ @@@ @@@ @@ $C2 ### ##### \ +$C1 @@@@@@@@@@@@ @@@ @@@ @@@ @@@ $C2######### ### \ +$C1 @@@ @@@ @@@ @@@ @@@@@@@@@ $C2######## ## \ +$C1 \ +$C1 \ +$C1 \ +$C1 \ +$C1 \ +$C1 \ +$C1 " + #define ASCII_INTEL_L \ "$C1 ###############@ \ $C1 ######@ ######@ \ @@ -95,9 +133,11 @@ typedef struct ascii_logo asciiL; // | LOGO | W | H | REPLACE | COLORS LOGO | COLORS TEXT | // ------------------------------------------------------------------------------------------ asciiL logo_nvidia = { ASCII_NVIDIA, 45, 19, false, {C_FG_GREEN, C_FG_WHITE}, {C_FG_WHITE, C_FG_GREEN} }; +asciiL logo_amd = { ASCII_AMD, 39, 15, false, {C_FG_WHITE, C_FG_GREEN}, {C_FG_WHITE, C_FG_GREEN} }; asciiL logo_intel = { ASCII_INTEL, 48, 14, false, {C_FG_CYAN}, {C_FG_CYAN, C_FG_WHITE} }; // Long variants | ---------------------------------------------------------------------------------------| asciiL logo_nvidia_l = { ASCII_NVIDIA_L, 50, 15, false, {C_FG_GREEN, C_FG_WHITE}, {C_FG_WHITE, C_FG_GREEN} }; +asciiL logo_amd_l = { ASCII_AMD_L, 62, 19, true, {C_BG_WHITE, C_BG_GREEN}, {C_FG_WHITE, C_FG_GREEN} }; asciiL logo_intel_l = { ASCII_INTEL_L, 62, 19, true, {C_BG_CYAN, C_BG_WHITE}, {C_FG_CYAN, C_FG_WHITE} }; asciiL logo_unknown = { NULL, 0, 0, false, {C_NONE}, {C_NONE, C_NONE} }; diff --git a/src/common/gpu.hpp b/src/common/gpu.hpp index 53092c7..5e5620c 100644 --- a/src/common/gpu.hpp +++ b/src/common/gpu.hpp @@ -9,6 +9,7 @@ enum { GPU_VENDOR_NVIDIA, + GPU_VENDOR_AMD, GPU_VENDOR_INTEL }; diff --git a/src/common/master.cpp b/src/common/master.cpp index bc8bc45..9353630 100644 --- a/src/common/master.cpp +++ b/src/common/master.cpp @@ -64,6 +64,11 @@ bool print_gpus_list(struct gpu_list* list) { print_gpu_cuda(list->gpus[i]); #endif } + else if(list->gpus[i]->vendor == GPU_VENDOR_AMD) { + #ifdef BACKEND_AMD + print_gpu_hsa(list->gpus[i]); + #endif + } else if(list->gpus[i]->vendor == GPU_VENDOR_INTEL) { #ifdef BACKEND_INTEL print_gpu_intel(list->gpus[i]); diff --git a/src/common/printer.cpp b/src/common/printer.cpp index e182d5a..62ada3e 100644 --- a/src/common/printer.cpp +++ b/src/common/printer.cpp @@ -233,6 +233,9 @@ void choose_ascii_art(struct ascii* art, struct color** cs, struct terminal* ter if(art->vendor == GPU_VENDOR_NVIDIA) { art->art = choose_ascii_art_aux(&logo_nvidia_l, &logo_nvidia, term, lf); } + else if(art->vendor == GPU_VENDOR_AMD) { + art->art = choose_ascii_art_aux(&logo_amd_l, &logo_amd, term, lf); + } else if(art->vendor == GPU_VENDOR_INTEL) { art->art = choose_ascii_art_aux(&logo_intel_l, &logo_intel, term, lf); } @@ -478,6 +481,13 @@ bool print_gpufetch_cuda(struct gpu_info* gpu, STYLE s, struct color** cs, struc } #endif +#ifdef BACKEND_HSA +bool print_gpufetch_amd(struct gpu_info* gpu, STYLE s, struct color** cs, struct terminal* term) { + printErr("AMD TODO"); + return true; +} +#endif + struct terminal* get_terminal_size() { struct terminal* term = (struct terminal*) emalloc(sizeof(struct terminal)); @@ -517,11 +527,22 @@ bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs) { return false; #endif } - else { + else if(gpu->vendor == GPU_VENDOR_AMD) { + #ifdef BACKEND_HSA + return print_gpufetch_amd(gpu, s, cs, term); + #else + return false; + #endif + } + else if(gpu->vendor == GPU_VENDOR_INTEL) { #ifdef BACKEND_INTEL return print_gpufetch_intel(gpu, s, cs, term); #else return false; #endif } + else { + printErr("Invalid GPU vendor: %d", gpu->vendor); + return false; + } } diff --git a/src/hsa/hsa.cpp b/src/hsa/hsa.cpp index 42e5134..643db0f 100644 --- a/src/hsa/hsa.cpp +++ b/src/hsa/hsa.cpp @@ -11,6 +11,42 @@ #include "../common/global.hpp" #include "../common/uarch.hpp" +struct agent_info { + unsigned deviceId; // ID of the target GPU device + char gpu_name[64]; +}; + +#define RET_IF_HSA_ERR(err) { \ + if ((err) != HSA_STATUS_SUCCESS) { \ + char err_val[12]; \ + char* err_str = NULL; \ + if (hsa_status_string(err, \ + (const char**)&err_str) != HSA_STATUS_SUCCESS) { \ + snprintf(&(err_val[0]), sizeof(err_val), "%#x", (uint32_t)err); \ + err_str = &(err_val[0]); \ + } \ + printErr("HSA failure at: %s:%d\n", \ + __FILE__, __LINE__); \ + printErr("Call returned %s\n", err_str); \ + return (err); \ + } \ +} + +hsa_status_t agent_callback(hsa_agent_t agent, void *data) { + struct agent_info* info = reinterpret_cast(data); + + hsa_device_type_t type; + hsa_status_t err = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &type); + RET_IF_HSA_ERR(err); + + if (type == HSA_DEVICE_TYPE_GPU) { + err = hsa_agent_get_info(agent, HSA_AGENT_INFO_NAME, info->gpu_name); + RET_IF_HSA_ERR(err); + } + + return HSA_STATUS_SUCCESS; +} + struct gpu_info* get_gpu_info_hsa(struct pci_dev *devices, int gpu_idx) { struct gpu_info* gpu = (struct gpu_info*) emalloc(sizeof(struct gpu_info)); gpu->pci = NULL; @@ -21,6 +57,11 @@ struct gpu_info* get_gpu_info_hsa(struct pci_dev *devices, int gpu_idx) { return NULL; } + if(gpu->idx > 0) { + // Currently we only support fetching GPU 0. + return NULL; + } + hsa_status_t status; // Initialize the HSA runtime @@ -30,31 +71,22 @@ struct gpu_info* get_gpu_info_hsa(struct pci_dev *devices, int gpu_idx) { return NULL; } - // Lambda for iterating over agents - auto agent_callback = [](hsa_agent_t agent, void* data) -> hsa_status_t { - hsa_device_type_t type; - if (hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &type) != HSA_STATUS_SUCCESS) - return HSA_STATUS_SUCCESS; - - if (type == HSA_DEVICE_TYPE_GPU) { - char name[64] = {0}; - if (hsa_agent_get_info(agent, HSA_AGENT_INFO_NAME, name) == HSA_STATUS_SUCCESS) { - std::cout << name << std::endl; - } - } - - return HSA_STATUS_SUCCESS; - }; + struct agent_info info; + info.deviceId = gpu_idx; // Iterate over all agents in the system - status = hsa_iterate_agents(agent_callback, nullptr); + status = hsa_iterate_agents(agent_callback, &info); if (status != HSA_STATUS_SUCCESS) { - std::cerr << "Failed to iterate HSA agents.\n"; + printErr("Failed to iterate HSA agents"); hsa_shut_down(); return NULL; } + gpu->vendor = GPU_VENDOR_AMD; + gpu->name = (char *) emalloc(sizeof(char) * (strlen(info.gpu_name) + 1)); + strcpy(gpu->name, info.gpu_name); + // Shut down the HSA runtime hsa_shut_down(); - return NULL; + return gpu; } diff --git a/src/hsa/hsa.hpp b/src/hsa/hsa.hpp index b8c688d..151b291 100644 --- a/src/hsa/hsa.hpp +++ b/src/hsa/hsa.hpp @@ -2,7 +2,6 @@ #define __HSA_GPU__ #include "../common/gpu.hpp" -// #define CUDA_DRIVER_START_WARNING "Waiting for CUDA driver to start..." struct gpu_info* get_gpu_info_hsa(struct pci_dev *devices, int gpu_idx);