diff --git a/Makefile b/Makefile index 9e4235c..9553af0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY : clean debug -OBJS = evga-icx.o evga-card.o icx3.o board-sensors.o zen3-rapl.o +OBJS = card.o evga-icx.o evga-card.o icx3.o board-sensors.o zen3-rapl.o LDLIBS = -li2c -lm CFLAGS = -MD -I/usr/local/cuda/targets/x86_64-linux/include/ diff --git a/card.c b/card.c new file mode 100644 index 0000000..47ed1ba --- /dev/null +++ b/card.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +#include "card.h" + +/* Read a 16-bit unsigned int from a pci id file */ +unsigned short read_pci_id(char *device_path, char *field) +{ + char buf[16]; + char file_path[PATH_MAX]; + + strcpy(file_path, device_path); + strcat(file_path, field); + + FILE *fp = fopen(file_path, "r"); + + if (fp == NULL) + return 0; + + if (fgets(buf, sizeof(buf), fp) == NULL) { + fclose(fp); + return 0; + } + + fclose(fp); + + return (unsigned short)strtoul(buf, NULL, 16); +} + +char *read_nvidia_pci_address(char *device_path) +{ + char file_path[PATH_MAX]; + + char *ret = calloc(16 + 1, sizeof(char)); /* assuming pci ids could look as large as 00000000:0C:00.0 */ + + strcpy(file_path, device_path); + strcat(file_path, "/name"); + + FILE *fp = fopen(file_path, "r"); + + if (fp == NULL) { + free(ret); + return NULL; + } + + fscanf(fp, "NVIDIA i2c adapter %*u at %16s", ret); + fclose(fp); + + if (strlen(ret) == 0) { + free(ret); + return NULL; + } + + return ret; +} diff --git a/card.h b/card.h new file mode 100644 index 0000000..79d3e0b --- /dev/null +++ b/card.h @@ -0,0 +1,22 @@ +struct card_info { + char *card_name; /* The 'nice' name of the card */ + char *pci_id; /* PCI bus address in domain:bus:device.function format. May be shortened (e.g. c:00.0) */ + char *pci_dev_path; /* Path to the PCI device file */ + void *bar0_mmap; /* Mmap of the PCI BAR0 */ + unsigned short pci_device_id; /* The device ID of the card, i.e. corresponds to the NVIDIA model number */ + char *i2c_dev_path; /* Path to the i2c device file */ + int i2c_fd; /* File descriptor for the i2c device file, for re-use */ + int product_id; /* EVGA internal product ID, as reported by the iCX3 controller */ + void *nvml_device; /* Pointer to nvmlDevice_t for use in NVML calls */ +}; + +struct gpu_pci_info { + char *card_name; + unsigned short vendor_id; + unsigned short device_id; + unsigned short subvendor_id; + unsigned short subdevice_id; +}; + +unsigned short read_pci_id(char *device_path, char *field); +char *read_nvidia_pci_address(char *device_path); \ No newline at end of file diff --git a/evga-card.c b/evga-card.c index 377d411..0055d89 100644 --- a/evga-card.c +++ b/evga-card.c @@ -81,9 +81,9 @@ int find_evga_gpu_i2cs(struct card_info *infos, int max_gpus, int i2c_bus) /* Write our card info into the provided struct array */ infos[num_gpus].card_name = evga_pci_ids[i].card_name; infos[num_gpus].pci_id = pci_addr; + infos[num_gpus].pci_dev_path = strdup(pci_path); infos[num_gpus].pci_device_id = pci_device; - infos[num_gpus].i2c_dev_path = calloc(strlen(dev_file) + 1, sizeof(char)); - strcpy(infos[num_gpus].i2c_dev_path, dev_file); + infos[num_gpus].i2c_dev_path = strdup(dev_file); num_gpus++; break; } @@ -96,55 +96,4 @@ int find_evga_gpu_i2cs(struct card_info *infos, int max_gpus, int i2c_bus) } return num_gpus; -} - -/* Read a 16-bit unsigned int from a pci id file */ -unsigned short read_pci_id(char *device_path, char *field) -{ - char buf[16]; - char file_path[PATH_MAX]; - - strcpy(file_path, device_path); - strcat(file_path, field); - - FILE *fp = fopen(file_path, "r"); - - if (fp == NULL) - return 0; - - if (fgets(buf, sizeof(buf), fp) == NULL) { - fclose(fp); - return 0; - } - - fclose(fp); - - return (unsigned short)strtoul(buf, NULL, 16); -} - -char *read_nvidia_pci_address(char *device_path) -{ - char file_path[PATH_MAX]; - - char *ret = calloc(16 + 1, sizeof(char)); /* assuming pci ids could look as large as 00000000:0C:00.0 */ - - strcpy(file_path, device_path); - strcat(file_path, "/name"); - - FILE *fp = fopen(file_path, "r"); - - if (fp == NULL) { - free(ret); - return NULL; - } - - fscanf(fp, "NVIDIA i2c adapter %*u at %16s", ret); - fclose(fp); - - if (strlen(ret) == 0) { - free(ret); - return NULL; - } - - return ret; -} +} \ No newline at end of file diff --git a/evga-card.h b/evga-card.h index 4453ece..d8240b1 100644 --- a/evga-card.h +++ b/evga-card.h @@ -1,6 +1,8 @@ #ifndef EVGA_CARD_H #define EVGA_CARD_H +#include "card.h" + #define NVIDIA_VEN 0x10DE #define NVIDIA_RTX3060_DEV 0x2503 @@ -83,27 +85,6 @@ #define EVGA_RTX3090TI_FTW3_GAMING_SUB_DEV 0x4983 #define EVGA_RTX3090TI_FTW3_ULTRA_GAMING_SUB_DEV 0x4985 -struct card_info { - char *card_name; /* The 'nice' name of the card */ - char *pci_id; /* PCI bus address in domain:bus:device.function format. May be shortened (e.g. c:00.0) */ - unsigned short pci_device_id; /* The device ID of the card, i.e. corresponds to the NVIDIA model number */ - char *i2c_dev_path; /* Path to the i2c device file */ - int i2c_fd; /* File descriptor for the i2c device file, for re-use */ - int product_id; /* EVGA internal product ID, as reported by the iCX3 controller */ - unsigned int bar0; /* Address of the card's PCI base address register */ - void *nvml_device; /* Pointer to nvmlDevice_t for use in NVML calls */ - void *vram_addr; /* Memory mapping for GDDR6 temps */ - void *hotspot_addr; /* Memory mapping for hotspot temperature */ -}; - -struct gpu_pci_info { - char *card_name; - unsigned short vendor_id; - unsigned short device_id; - unsigned short subvendor_id; - unsigned short subdevice_id; -}; - static struct gpu_pci_info evga_pci_ids[] = { {"EVGA GeForce RTX 3060 Ti FTW3 Gaming" , NVIDIA_VEN, NVIDIA_RTX3060TI_DEV, EVGA_SUB_VEN, EVGA_RTX3060TI_FTW3_GAMING_SUB_DEV }, @@ -166,7 +147,5 @@ static struct gpu_pci_info evga_pci_ids[] = }; int find_evga_gpu_i2cs(struct card_info *infos, int max_gpus, int i2c_bus); -unsigned short read_pci_id(char *device_path, char *field); -char *read_nvidia_pci_address(char *device_path); #endif