diff --git a/CMakeLists.txt b/CMakeLists.txt index 62c9cd6..f15affd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,7 @@ set(CMAKE_CXX_FLAGS "${SANITY_FLAGS} -Wall -Wextra -pedantic -fstack-protector-a if(ENABLE_INTEL_BACKEND) target_compile_definitions(gpufetch PUBLIC BACKEND_INTEL) - add_library(intel_backend STATIC ${INTEL_DIR}/intel.cpp) + add_library(intel_backend STATIC ${INTEL_DIR}/intel.cpp ${INTEL_DIR}/pci.cpp ${INTEL_DIR}/uarch.cpp) if(NOT ${PCIUTILS_FOUND}) add_dependencies(intel_backend pciutils) diff --git a/src/common/master.cpp b/src/common/master.cpp index 219b36a..d86711a 100644 --- a/src/common/master.cpp +++ b/src/common/master.cpp @@ -20,6 +20,8 @@ struct gpu_list* get_gpu_list() { list->gpus = (struct gpu_info**) malloc(sizeof(struct info*) * MAX_GPUS); #ifdef BACKEND_CUDA + bool valid = true; + while(valid) { list->gpus[idx] = get_gpu_info_cuda(idx); if(list->gpus[idx] != NULL) idx++; diff --git a/src/common/pci.cpp b/src/common/pci.cpp index 2a25ad6..3f374da 100644 --- a/src/common/pci.cpp +++ b/src/common/pci.cpp @@ -2,33 +2,39 @@ #include "pci.hpp" #include -/* - * doc: https://wiki.osdev.org/PCI#Class_Codes - * https://pci-ids.ucw.cz/read/PC - */ -#define VENDOR_ID_NVIDIA 0x10de #define CLASS_VGA_CONTROLLER 0x0300 -uint16_t pciutils_get_pci_vendor_id(struct pci_dev *devices) { +uint16_t pciutils_get_pci_vendor_id(struct pci_dev *devices, int id) { for(struct pci_dev *dev=devices; dev != NULL; dev=dev->next) { - if(dev->vendor_id == VENDOR_ID_NVIDIA && dev->device_class == CLASS_VGA_CONTROLLER) { + if(dev->vendor_id == id && dev->device_class == CLASS_VGA_CONTROLLER) { return dev->vendor_id; } } - printErr("Unable to find a CUDA device using pciutils"); + + printErr("Unable to find a valid device for id %d using pciutils", id); return 0; } -uint16_t pciutils_get_pci_device_id(struct pci_dev *devices) { +uint16_t pciutils_get_pci_device_id(struct pci_dev *devices, int id) { for(struct pci_dev *dev=devices; dev != NULL; dev=dev->next) { - if(dev->vendor_id == VENDOR_ID_NVIDIA && dev->device_class == CLASS_VGA_CONTROLLER) { + if(dev->vendor_id == id && dev->device_class == CLASS_VGA_CONTROLLER) { return dev->device_id; } } - printErr("Unable to find a CUDA device using pciutils"); + + printErr("Unable to find a valid device for id %d using pciutils", id); return 0; } +struct pci* get_pci_from_pciutils(struct pci_dev *devices, int id) { + struct pci* pci = (struct pci*) emalloc(sizeof(struct pci)); + + pci->vendor_id = pciutils_get_pci_vendor_id(devices, id); + pci->device_id = pciutils_get_pci_device_id(devices, id); + + return pci; +} + struct pci_dev *get_pci_devices_from_pciutils() { struct pci_access *pacc; struct pci_dev *dev; diff --git a/src/common/pci.hpp b/src/common/pci.hpp index b3d3b94..73cf372 100644 --- a/src/common/pci.hpp +++ b/src/common/pci.hpp @@ -6,8 +6,12 @@ extern "C" { #include } -uint16_t pciutils_get_pci_vendor_id(struct pci_dev *devices); -uint16_t pciutils_get_pci_device_id(struct pci_dev *devices); +struct pci { + uint16_t vendor_id; + uint16_t device_id; +}; + +struct pci* get_pci_from_pciutils(struct pci_dev *devices, int id); struct pci_dev *get_pci_devices_from_pciutils(); #endif diff --git a/src/common/uarch.hpp b/src/common/uarch.hpp new file mode 100644 index 0000000..98efa50 --- /dev/null +++ b/src/common/uarch.hpp @@ -0,0 +1,20 @@ +#ifndef __COMMON_UARCH__ +#define __COMMON_UARCH__ + +typedef uint32_t GPUCHIP; +typedef uint32_t MICROARCH; + +struct uarch { + int32_t cc_major; + int32_t cc_minor; + int32_t compute_capability; + + MICROARCH uarch; + GPUCHIP chip; + + int32_t process; + char* uarch_str; + char* chip_str; +}; + +#endif diff --git a/src/cuda/chips.hpp b/src/cuda/chips.hpp index 53e6535..6e1e290 100644 --- a/src/cuda/chips.hpp +++ b/src/cuda/chips.hpp @@ -1,5 +1,5 @@ -#ifndef __GPUCHIPS__ -#define __GPUCHIPS__ +#ifndef __CUDA_GPUCHIPS__ +#define __CUDA_GPUCHIPS__ typedef uint32_t GPUCHIP; diff --git a/src/cuda/cuda.cpp b/src/cuda/cuda.cpp index d542936..6554dfc 100644 --- a/src/cuda/cuda.cpp +++ b/src/cuda/cuda.cpp @@ -133,7 +133,7 @@ struct gpu_info* get_gpu_info_cuda(int gpu_idx) { strcpy(gpu->name, deviceProp.name); struct pci_dev *devices = get_pci_devices_from_pciutils(); - gpu->pci = get_pci_from_pciutils(devices); + gpu->pci = get_pci_from_pciutils(devices, PCI_VENDOR_ID_NVIDIA); gpu->arch = get_uarch_from_cuda(gpu); gpu->cach = get_cache_info(deviceProp); gpu->mem = get_memory_info(gpu, deviceProp); diff --git a/src/cuda/pci.cpp b/src/cuda/pci.cpp index 051f324..6e0e8cc 100644 --- a/src/cuda/pci.cpp +++ b/src/cuda/pci.cpp @@ -10,20 +10,6 @@ else if (pci->device_id == id) return chip; #define CHECK_PCI_END else { printBug("TODOO"); return CHIP_UNKNOWN; } -struct pci { - uint16_t vendor_id; - uint16_t device_id; -}; - -struct pci* get_pci_from_pciutils(struct pci_dev *devices) { - struct pci* pci = (struct pci*) emalloc(sizeof(struct pci)); - - pci->vendor_id = pciutils_get_pci_vendor_id(devices); - pci->device_id = pciutils_get_pci_device_id(devices); - - return pci; -} - /* * pci ids were retrieved using https://github.com/pciutils/pciids * and parsed using a custom script to take only the relevant diff --git a/src/cuda/pci.hpp b/src/cuda/pci.hpp index 203d2db..b30edd3 100644 --- a/src/cuda/pci.hpp +++ b/src/cuda/pci.hpp @@ -6,6 +6,12 @@ #include "../common/pci.hpp" #include "chips.hpp" +/* + * doc: https://wiki.osdev.org/PCI#Class_Codes + * https://pci-ids.ucw.cz/read/PC + */ +#define PCI_VENDOR_ID_NVIDIA 0x10de + struct pci; struct pci* get_pci_from_pciutils(struct pci_dev *devices); diff --git a/src/cuda/uarch.cpp b/src/cuda/uarch.cpp index 6a9f144..b0aeea1 100644 --- a/src/cuda/uarch.cpp +++ b/src/cuda/uarch.cpp @@ -3,12 +3,11 @@ #include #include +#include "../common/uarch.hpp" #include "../common/global.hpp" #include "../common/gpu.hpp" #include "chips.hpp" -typedef uint32_t MICROARCH; - // Any clock multiplier #define CM_ANY -1 @@ -43,19 +42,6 @@ static const char *uarch_str[] = { /*[ARCH_AMPERE] = */ "Ampere", }; -struct uarch { - int32_t cc_major; - int32_t cc_minor; - int32_t compute_capability; - - MICROARCH uarch; - GPUCHIP chip; - - int32_t process; - char* uarch_str; - char* chip_str; -}; - #define CHECK_UARCH_START if (false) {} #define CHECK_UARCH(arch, chip_, str, uarch, process) \ else if (arch->chip == chip_) fill_uarch(arch, str, uarch, process); diff --git a/src/intel/chips.hpp b/src/intel/chips.hpp new file mode 100644 index 0000000..77a1189 --- /dev/null +++ b/src/intel/chips.hpp @@ -0,0 +1,13 @@ +#ifndef __INTEL_GPUCHIPS__ +#define __INTEL_GPUCHIPS__ + +#include + +typedef uint32_t GPUCHIP; + +enum { + CHIP_UNKNOWN_INTEL, + CHIP_UHDG_620, +}; + +#endif diff --git a/src/intel/intel.cpp b/src/intel/intel.cpp index ecc0712..fe23880 100644 --- a/src/intel/intel.cpp +++ b/src/intel/intel.cpp @@ -3,6 +3,7 @@ #include "intel.hpp" #include "uarch.hpp" +#include "chips.hpp" #include "../common/pci.hpp" #include "../common/global.hpp" @@ -11,8 +12,11 @@ struct gpu_info* get_gpu_info_intel() { const char* name = "UHD Graphics XXX"; gpu->vendor = GPU_VENDOR_INTEL; - gpu->name = (char *) emalloc(sizeof(char) * (strlen(name) + 1)); - strcpy(gpu->name, name); + + struct pci_dev *devices = get_pci_devices_from_pciutils(); + gpu->pci = get_pci_from_pciutils(devices, PCI_VENDOR_ID_INTEL); + gpu->arch = get_uarch_from_pci(gpu->pci); + gpu->name = get_name_from_uarch(gpu->arch); return gpu; } diff --git a/src/intel/pci.cpp b/src/intel/pci.cpp new file mode 100644 index 0000000..6772777 --- /dev/null +++ b/src/intel/pci.cpp @@ -0,0 +1,17 @@ +#include + +#include "pci.hpp" +#include "chips.hpp" +#include "../common/global.hpp" +#include "../common/pci.hpp" + +#define CHECK_PCI_START if (false) {} +#define CHECK_PCI(pci, id, chip) \ + else if (pci->device_id == id) return chip; +#define CHECK_PCI_END else { printBug("TODOO"); return CHIP_UNKNOWN_INTEL; } + +GPUCHIP get_chip_from_pci(struct pci* pci) { + CHECK_PCI_START + CHECK_PCI(pci, 0x5917, CHIP_UHDG_620) + CHECK_PCI_END +} diff --git a/src/intel/pci.hpp b/src/intel/pci.hpp new file mode 100644 index 0000000..d9401a8 --- /dev/null +++ b/src/intel/pci.hpp @@ -0,0 +1,20 @@ +#ifndef __PCI_INTEL__ +#define __PCI_INTEL__ + +#include + +#include "../common/pci.hpp" +#include "chips.hpp" + +/* + * doc: https://wiki.osdev.org/PCI#Class_Codes + * https://pci-ids.ucw.cz/read/PC + */ +#define PCI_VENDOR_ID_INTEL 0x8086 + +struct pci; + +struct pci* get_pci_from_pciutils(struct pci_dev *devices); +GPUCHIP get_chip_from_pci(struct pci* pci); + +#endif diff --git a/src/intel/uarch.cpp b/src/intel/uarch.cpp new file mode 100644 index 0000000..3404c73 --- /dev/null +++ b/src/intel/uarch.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +#include "../common/uarch.hpp" +#include "../common/global.hpp" +#include "../common/gpu.hpp" +#include "chips.hpp" + +// Data not available +#define NA -1 + +// Unknown manufacturing process +#define UNK -1 + +// MICROARCH values +enum { + UARCH_UNKNOWN, + UARCH_GEN9, + UARCH_GEN9_5, +}; + +static const char *uarch_str[] = { + /*[ARCH_UNKNOWN = */ STRING_UNKNOWN, + /*[ARCH_GEN9] = */ "Gen9", + /*[ARCH_GEN9.5] = */ "Gen9.5", +}; + +#define CHECK_UARCH_START if (false) {} +#define CHECK_UARCH(arch, chip_, str, uarch, process) \ + else if (arch->chip == chip_) fill_uarch(arch, str, uarch, process); +#define CHECK_UARCH_END else { printBug("map_chip_to_uarch: Unknown chip id: %d", arch->chip); fill_uarch(arch, STRING_UNKNOWN, UARCH_UNKNOWN, 0); } + +void fill_uarch(struct uarch* arch, char const *str, MICROARCH u, uint32_t process) { + arch->chip_str = (char *) emalloc(sizeof(char) * (strlen(str)+1)); + strcpy(arch->chip_str, str); + arch->uarch = u; + arch->process = process; +} + +void map_chip_to_uarch(struct uarch* arch) { + CHECK_UARCH_START + CHECK_UARCH(arch, CHIP_UHDG_620, "UHD Graphics 620", UARCH_GEN9_5, 14) + CHECK_UARCH_END +} + +struct uarch* get_uarch_from_pci(struct pci* pci) { + struct uarch* arch = (struct uarch*) emalloc(sizeof(struct uarch)); + + arch->chip_str = NULL; + arch->chip = get_chip_from_pci(pci); + map_chip_to_uarch(arch); + + return arch; +} + +char* get_name_from_uarch(struct uarch* arch) { + return arch->chip_str; +} diff --git a/src/intel/uarch.hpp b/src/intel/uarch.hpp index 2076854..5b6dbdd 100644 --- a/src/intel/uarch.hpp +++ b/src/intel/uarch.hpp @@ -3,4 +3,9 @@ #include "../common/gpu.hpp" +struct uarch; + +struct uarch* get_uarch_from_pci(struct pci* pci); +char* get_name_from_uarch(struct uarch* arch); + #endif