106 lines
2.8 KiB
C
106 lines
2.8 KiB
C
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <pci/pci.h>
|
|
#include <sys/mman.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
#define PG_SZ sysconf(_SC_PAGE_SIZE)
|
|
|
|
#include "gddr6.h"
|
|
|
|
void init_gddr6(struct card_info *card)
|
|
{
|
|
/* Parse the address of the card to get the PCI info */
|
|
char pci_address[] = "00000000:00:00.0";
|
|
int len = strlen(card->pci_id);
|
|
strcpy(&pci_address[sizeof(pci_address) - len - 1], card->pci_id);
|
|
|
|
int domain = 0;
|
|
int bus = 0;
|
|
int dev = 0;
|
|
int func = 0;
|
|
|
|
sscanf(pci_address, "%x:%x:%x.%x", &domain, &bus, &dev, &func);
|
|
|
|
struct pci_access *pacc = NULL;
|
|
struct pci_dev *pci_dev = NULL;
|
|
|
|
pacc = pci_alloc();
|
|
pci_init(pacc);
|
|
|
|
pci_dev = pci_get_dev(pacc, domain, bus, dev, func);
|
|
|
|
pci_fill_info(pci_dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
|
|
|
|
card->bar0 = (pci_dev->base_addr[0] & 0xFFFFFFFF);
|
|
|
|
pci_cleanup(pacc);
|
|
}
|
|
|
|
float get_vram_temp(struct card_info *card)
|
|
{
|
|
int fd;
|
|
float temp = 0.0;
|
|
|
|
if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
|
|
{
|
|
printf("Can't read memory. If you are root, enable kernel parameter iomem=relaxed\n");
|
|
return 0;
|
|
}
|
|
|
|
for (int i = 0; i < sizeof(device_offset_info) / sizeof(struct device_offset); i++) {
|
|
if (card->pci_device_id == device_offset_info[i].device_id){
|
|
unsigned int phys_addr = (card->bar0 + device_offset_info[i].vram_offset);
|
|
unsigned int base_offset = phys_addr & ~(PG_SZ-1);
|
|
void *map_base = mmap(0, PG_SZ, PROT_READ, MAP_SHARED, fd, base_offset);
|
|
if(map_base == (void *) -1)
|
|
{
|
|
if (fd != -1)
|
|
close(fd);
|
|
printf("Can't read memory for VRAM temperature. If you are root, enable kernel parameter iomem=relaxed\n");
|
|
}
|
|
void *virt_addr = (char *) map_base + (phys_addr - base_offset);
|
|
int read_result = *((unsigned int *) virt_addr);
|
|
temp = ((read_result & 0x00000fff) / 0x20);
|
|
munmap(map_base, PG_SZ);
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
return temp;
|
|
}
|
|
|
|
float get_hotspot_temp(struct card_info *card)
|
|
{
|
|
int fd;
|
|
float temp = 0.0;
|
|
|
|
if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
|
|
{
|
|
printf("Can't read memory. If you are root, enable kernel parameter iomem=relaxed\n");
|
|
return 0;
|
|
}
|
|
|
|
for (int i = 0; i < sizeof(device_offset_info) / sizeof(struct device_offset); i++) {
|
|
if (card->pci_device_id == device_offset_info[i].device_id){
|
|
unsigned int phys_addr = (card->bar0 + device_offset_info[i].hotspot_offset);
|
|
unsigned int base_offset = phys_addr & ~(PG_SZ-1);
|
|
void *map_base = mmap(0, PG_SZ, PROT_READ, MAP_SHARED, fd, base_offset);
|
|
if(map_base == (void *) -1)
|
|
{
|
|
if (fd != -1)
|
|
close(fd);
|
|
printf("Can't read memory for hotspot. If you are root, enable kernel parameter iomem=relaxed\n");
|
|
}
|
|
void *virt_addr = (char *) map_base + (phys_addr - base_offset);
|
|
int read_result = *((unsigned int *) virt_addr);
|
|
temp = (read_result >> 8) & 0xff;
|
|
munmap(map_base, PG_SZ);
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
return temp;
|
|
}
|