multi-gpu support + fix indent

This commit is contained in:
javi
2023-05-13 19:12:36 +02:00
parent 4999b7b3f0
commit e91f846160

97
gddr6.c
View File

@@ -19,43 +19,44 @@
} while(0) } while(0)
// variables // device struct
int fd; struct device
void *map_base; {
// device struct
struct device
{
uint32_t bar0; uint32_t bar0;
uint8_t bus, dev, func;
uint32_t offset; uint32_t offset;
uint16_t dev_id; uint16_t dev_id;
const char *vram; const char *vram;
const char *arch; const char *arch;
const char *name; const char *name;
}; };
// device table // variables
struct device dev_table[] = int fd;
{ void *map_base;
struct device devices[32];
// device table
struct device dev_table[] =
{
{ .offset = 0x0000E2A8, .dev_id = 0x2684, .vram = "GDDR6X", .arch = "AD102", .name = "RTX 4090" }, { .offset = 0x0000E2A8, .dev_id = 0x2684, .vram = "GDDR6X", .arch = "AD102", .name = "RTX 4090" },
{ .offset = 0x0000E2A8, .dev_id = 0x2782, .vram = "GDDR6X", .arch = "AD104", .name = "RTX 4070 Ti" },
{ .offset = 0x0000E2A8, .dev_id = 0x2204, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3090" }, { .offset = 0x0000E2A8, .dev_id = 0x2204, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3090" },
{ .offset = 0x0000EE50, .dev_id = 0x2484, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070" }, { .offset = 0x0000EE50, .dev_id = 0x2484, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070" },
{ .offset = 0x0000EE50, .dev_id = 0x2488, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070-LHR" }, { .offset = 0x0000EE50, .dev_id = 0x2488, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070-LHR" },
}; };
// prototypes // prototypes
void cleanup(int signal); void cleanup(int signal);
void cleanup_sig_handler(void); void cleanup_sig_handler(void);
struct device *pci_detect_dev(void); int pci_detect_dev(void);
// cleanup // cleanup
void cleanup(int signal) void cleanup(int signal)
{ {
if (signal == SIGHUP || signal == SIGINT || signal == SIGTERM) if (signal == SIGHUP || signal == SIGINT || signal == SIGTERM)
{ {
if (map_base != (void *) -1) if (map_base != (void *) -1)
@@ -64,12 +65,12 @@
close(fd); close(fd);
exit(0); exit(0);
} }
} }
// cleanup signal handler // cleanup signal handler
void cleanup_sig_handler(void) void cleanup_sig_handler(void)
{ {
struct sigaction sa; struct sigaction sa;
sa.sa_handler = &cleanup; sa.sa_handler = &cleanup;
sa.sa_flags = 0; sa.sa_flags = 0;
@@ -83,15 +84,15 @@
if (sigaction(SIGTERM, &sa, NULL) < 0) if (sigaction(SIGTERM, &sa, NULL) < 0)
perror("Cannot handle SIGTERM"); perror("Cannot handle SIGTERM");
} }
// pci device detection // pci device detection
struct device *pci_detect_dev(void) int pci_detect_dev(void)
{ {
struct pci_access *pacc = NULL; struct pci_access *pacc = NULL;
struct pci_dev *pci_dev = NULL; struct pci_dev *pci_dev = NULL;
struct device *device = NULL; int num_devs = 0;
ssize_t dev_table_size = (sizeof(dev_table)/sizeof(struct device)); ssize_t dev_table_size = (sizeof(dev_table)/sizeof(struct device));
pacc = pci_alloc(); pacc = pci_alloc();
@@ -106,15 +107,18 @@
{ {
if (pci_dev->device_id == dev_table[i].dev_id) if (pci_dev->device_id == dev_table[i].dev_id)
{ {
device = (struct device *) &dev_table[i]; devices[num_devs] = dev_table[i];
device->bar0 = (pci_dev->base_addr[0] & 0xFFFFFFFF); devices[num_devs].bar0 = (pci_dev->base_addr[0] & 0xFFFFFFFF);
break; devices[num_devs].bus = pci_dev->bus;
devices[num_devs].dev = pci_dev->dev;
devices[num_devs].func = pci_dev->func;
num_devs++;
} }
} }
} }
pci_cleanup(pacc); pci_cleanup(pacc);
return device; return num_devs;
} }
@@ -128,18 +132,22 @@ int main(int argc, char **argv)
uint32_t read_result; uint32_t read_result;
uint32_t base_offset; uint32_t base_offset;
struct device *device = NULL; int num_devs;
char *MEM = "\x2f\x64\x65\x76\x2f\x6d\x65\x6d"; char *MEM = "\x2f\x64\x65\x76\x2f\x6d\x65\x6d";
device = pci_detect_dev(); num_devs = pci_detect_dev();
if (device == NULL) if (num_devs == 0)
{ {
printf("No compatible GPU found\n."); printf("No compatible GPU found\n.");
exit(-1); exit(-1);
} }
printf("Device: %s (%s / 0x%04x)\n", device->name, device->arch, device->dev_id); for (int i = 0; i < num_devs; i++) {
struct device *device = &devices[i];
printf("Device: %s %s (%s / 0x%04x) pci=%x:%x:%x\n", device->name, device->vram,
device->arch, device->dev_id, device->bus, device->dev, device->func);
}
if ((fd = open(MEM, O_RDWR | O_SYNC)) == -1) if ((fd = open(MEM, O_RDWR | O_SYNC)) == -1)
{ {
@@ -149,6 +157,13 @@ int main(int argc, char **argv)
cleanup_sig_handler(); cleanup_sig_handler();
while (1)
{
printf("\rVRAM Temps: |");
for (int i = 0; i < num_devs; i++) {
struct device *device = &devices[i];
phys_addr = (device->bar0 + device->offset); phys_addr = (device->bar0 + device->offset);
base_offset = phys_addr & ~(PG_SZ-1); base_offset = phys_addr & ~(PG_SZ-1);
map_base = mmap(0, PG_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base_offset); map_base = mmap(0, PG_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base_offset);
@@ -160,14 +175,12 @@ int main(int argc, char **argv)
printf("Can't read memory. If you are root, enable kernel parameter iomem=relaxed\n"); printf("Can't read memory. If you are root, enable kernel parameter iomem=relaxed\n");
PRINT_ERROR(); PRINT_ERROR();
} }
while (1)
{
virt_addr = (uint8_t *) map_base + (phys_addr - base_offset); virt_addr = (uint8_t *) map_base + (phys_addr - base_offset);
read_result = *((uint32_t *) virt_addr); read_result = *((uint32_t *) virt_addr);
temp = ((read_result & 0x00000fff) / 0x20); temp = ((read_result & 0x00000fff) / 0x20);
printf("\r%s VRAM Temp: %.1u°c", device->vram, temp); printf(" %3u°c |", temp);
}
fflush(stdout); fflush(stdout);
sleep(1); sleep(1);
} }