refactor
This commit is contained in:
88
gddr6.c
88
gddr6.c
@@ -8,7 +8,7 @@
|
||||
#include <sys/mman.h>
|
||||
#include <pci/pci.h>
|
||||
|
||||
#define PRINT_ERROR \
|
||||
#define PRINT_ERROR() \
|
||||
do { \
|
||||
fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
|
||||
__LINE__, __FILE__, errno, strerror(errno)); exit(1); \
|
||||
@@ -19,15 +19,17 @@ struct device
|
||||
uint32_t bar0;
|
||||
uint32_t offset;
|
||||
uint16_t dev_id;
|
||||
const char *vram;
|
||||
const char *arch;
|
||||
const char *name;
|
||||
};
|
||||
|
||||
static struct device devices[] = {
|
||||
{ .bar0 = 0xEC000000, .offset = 0x0000E2A8, .dev_id = 0x2684, .arch = "AD102", .name = "RTX 4090" },
|
||||
{ .bar0 = 0xFB000000, .offset = 0x0000E2A8, .dev_id = 0x2204, .arch = "GA102", .name = "RTX 3090" },
|
||||
{ .bar0 = 0xFB000000, .offset = 0x0000EE50, .dev_id = 0x2484, .arch = "GA104", .name = "RTX 3070" },
|
||||
{ .bar0 = 0xFB000000, .offset = 0x0000EE50, .dev_id = 0x2488, .arch = "GA104", .name = "RTX 3070-LHR" },
|
||||
static struct device dev_table[] =
|
||||
{
|
||||
{ .bar0 = 0xEC000000, .offset = 0x0000E2A8, .dev_id = 0x2684, .vram = "GDDR6X", .arch = "AD102", .name = "RTX 4090" },
|
||||
{ .bar0 = 0xFB000000, .offset = 0x0000E2A8, .dev_id = 0x2204, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3090" },
|
||||
{ .bar0 = 0xFB000000, .offset = 0x0000EE50, .dev_id = 0x2484, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070" },
|
||||
{ .bar0 = 0xFB000000, .offset = 0x0000EE50, .dev_id = 0x2488, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070-LHR" },
|
||||
};
|
||||
|
||||
struct device * pci_detect_dev()
|
||||
@@ -35,16 +37,28 @@ struct device * pci_detect_dev()
|
||||
struct pci_access *pacc = NULL;
|
||||
struct pci_dev *dev = NULL;
|
||||
struct device *device = NULL;
|
||||
ssize_t dev_table_size = (sizeof(dev_table)/sizeof(struct device));
|
||||
|
||||
pacc = pci_alloc();
|
||||
pci_init(pacc);
|
||||
pci_scan_bus(pacc);
|
||||
|
||||
for (dev = pacc->devices; dev; dev = dev->next) {
|
||||
for (dev = pacc->devices; dev; dev = dev->next)
|
||||
{
|
||||
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
|
||||
for (uint32_t i = 0; i < 3; i++) {
|
||||
if (dev->device_id == devices[i].dev_id) {
|
||||
device = &devices[i];
|
||||
|
||||
for (uint32_t i = 0; i < dev_table_size; i++)
|
||||
{
|
||||
if (dev->device_id == dev_table[i].dev_id)
|
||||
{
|
||||
device = &dev_table[i];
|
||||
|
||||
if (dev->base_addr[0] != device->bar0)
|
||||
{
|
||||
device->bar0 = dev->base_addr[0];
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,49 +70,57 @@ struct device * pci_detect_dev()
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd;
|
||||
int map_size = 4096UL;
|
||||
void *map_base, *virt_addr;
|
||||
uint32_t read_result, base_offset, temp;
|
||||
uint32_t offset;
|
||||
void *map_base;
|
||||
void *virt_addr;
|
||||
uint32_t temp;
|
||||
uint32_t phys_addr;
|
||||
uint32_t read_result;
|
||||
uint32_t base_offset;
|
||||
uint32_t map_size = 0x1000;
|
||||
struct device *device = NULL;
|
||||
|
||||
char *MEM_RES = "\x2f\x64\x65\x76\x2f\x6d\x65\x6d";
|
||||
char *MEM = "\x2f\x64\x65\x76\x2f\x6d\x65\x6d";
|
||||
|
||||
device = pci_detect_dev();
|
||||
offset = device->bar0 + device->offset;
|
||||
phys_addr = device->bar0 + device->offset;
|
||||
|
||||
if (device == NULL) {
|
||||
if (device == NULL)
|
||||
{
|
||||
printf("No compatible devices device. \n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("Device: %s (%s / 0x%04x)\n", device->name, device->arch, device->dev_id);
|
||||
|
||||
while (1) {
|
||||
if ((fd = open(MEM_RES, O_RDWR | O_SYNC)) == -1) {
|
||||
printf("Can't read memory. Are you r00t?\n");
|
||||
exit(-1);
|
||||
if ((fd = open(MEM, O_RDWR | O_SYNC)) == -1)
|
||||
{
|
||||
printf("Can't read memory. If you are root, enable kernel parameter iomem=relaxed\n");
|
||||
PRINT_ERROR();
|
||||
}
|
||||
|
||||
base_offset = offset & ~(sysconf(_SC_PAGE_SIZE)-1);
|
||||
base_offset = phys_addr & ~(sysconf(_SC_PAGE_SIZE)-1);
|
||||
map_base = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base_offset);
|
||||
|
||||
if(map_base == (void *) -1)
|
||||
PRINT_ERROR;
|
||||
{
|
||||
close(fd);
|
||||
PRINT_ERROR();
|
||||
}
|
||||
|
||||
virt_addr = map_base + offset - base_offset;
|
||||
while (1)
|
||||
{
|
||||
virt_addr = (map_base + (phys_addr - base_offset));
|
||||
read_result = *((uint32_t *) virt_addr);
|
||||
temp = (read_result & 0x00000fff) / 0x20;
|
||||
printf("\rGDDR6X VRAM Temp: %d°c", temp);
|
||||
|
||||
temp = ((read_result & 0x00000fff) / 0x20);
|
||||
|
||||
printf("\r%s VRAM Temp: %d°c", device->vram, temp);
|
||||
fflush(stdout);
|
||||
|
||||
if (munmap(map_base, map_size) == -1) {
|
||||
printf("Can't unmap memory!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
munmap(map_base, map_size);
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user