Files
evga-icx/card.c
T
moosecrap bfbc978da8 Blackwell die, system, and VRAM temps.
Code reorganization to better support non-iCX cards.
2026-09-30 00:16:02 -07:00

96 lines
2.0 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#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);
}
/* Read a PCI ID from the name of an Nvidia i2c device */
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;
}
/* Open an mmap to the BAR0 of a PCI device using its /sys/bus/pci device files */
void mmap_bar0(struct card_info *card)
{
char resource_path[PATH_MAX];
snprintf(resource_path, sizeof(resource_path), "%s/resource0", card->pci_dev_path);
/* Open FD on the resource file */
int fd = open(resource_path, O_RDONLY);
if (fd == -1) {
printf("Unable to open %s\n", resource_path);
card->bar0_mmap = NULL;
return;
}
/* Figure out the size of BAR0 fomr the size of the resource file */
struct stat st;
if (fstat(fd, &st) == -1) {
printf("Unable to stat %s\n", resource_path);
close(fd);
card->bar0_mmap = NULL;
return;
}
/* mmap the bar0 resource file */
card->bar0_mmap = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (card->bar0_mmap == MAP_FAILED) {
printf("Unable to mmap %s\n", resource_path);
card->bar0_mmap = NULL;
}
/* mmap stays open after the FD has been closed */
close(fd);
}