Add some `radeon` support. Link with libdrm etc. directly.
parent
14507c402d
commit
365c4bef97
@ -0,0 +1,202 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "gpu.h"
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <radeon_drm.h>
|
||||
#include <xf86drm.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "auth.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
#ifndef RADEON_INFO_VRAM_USAGE
|
||||
#define RADEON_INFO_VRAM_USAGE 0x1e
|
||||
#endif
|
||||
#ifndef RADEON_INFO_READ_REG
|
||||
#define RADEON_INFO_READ_REG 0x24
|
||||
#endif
|
||||
#ifndef RADEON_INFO_CURRENT_GPU_SCLK
|
||||
#define RADEON_INFO_CURRENT_GPU_SCLK 0x22
|
||||
#endif
|
||||
#ifndef RADEON_INFO_CURRENT_GPU_MCLK
|
||||
#define RADEON_INFO_CURRENT_GPU_MCLK 0x23
|
||||
#endif
|
||||
|
||||
static const char* get_enum_str(uint32_t request)
|
||||
{
|
||||
switch (request)
|
||||
{
|
||||
case RADEON_INFO_READ_REG: return "RADEON_INFO_READ_REG";
|
||||
case RADEON_INFO_VRAM_USAGE: return "RADEON_INFO_VRAM_USAGE";
|
||||
case RADEON_INFO_CURRENT_GPU_SCLK: return "RADEON_INFO_CURRENT_GPU_SCLK";
|
||||
case RADEON_INFO_CURRENT_GPU_MCLK: return "RADEON_INFO_CURRENT_GPU_MCLK";
|
||||
default: break;
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
#define DRM_ATLEAST_VERSION(ver, maj, min) \
|
||||
(ver->version_major > maj || (ver->version_major == maj && ver->version_minor >= min))
|
||||
|
||||
static int get_radeon_drm_value(int fd, uint32_t request, void *out)
|
||||
{
|
||||
struct drm_radeon_info info {};
|
||||
info.value = reinterpret_cast<uint64_t>(out);
|
||||
info.request = request;
|
||||
|
||||
// Or ioctl(fd, DRM_IOCTL_RADEON_INFO, &info);
|
||||
int ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
|
||||
if (ret)
|
||||
SPDLOG_ERROR("radeon drm error: {}", get_enum_str(request));
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct radeon_handles
|
||||
{
|
||||
int fd;
|
||||
uint32_t version_major, version_minor, gui_percent {0};
|
||||
uint32_t ticks = 60, ticks_per_sec = 120;
|
||||
std::chrono::nanoseconds sleep_interval {};
|
||||
|
||||
bool quit = false;
|
||||
std::thread collector;
|
||||
|
||||
radeon_handles(int fd_, uint32_t major, uint32_t minor)
|
||||
: fd(fd_)
|
||||
, version_major(major)
|
||||
, version_minor(minor)
|
||||
{
|
||||
set_sampling_period(500000000 /* 500ms */);
|
||||
collector = std::thread(&radeon_handles::poll, this);
|
||||
}
|
||||
|
||||
~radeon_handles()
|
||||
{
|
||||
quit = true;
|
||||
if (collector.joinable())
|
||||
collector.join();
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void set_sampling_period(uint32_t period)
|
||||
{
|
||||
if (period < 10000000)
|
||||
period = 10000000; /* 10ms */
|
||||
ticks = ticks_per_sec * std::chrono::nanoseconds(period) / 1s;
|
||||
sleep_interval = std::chrono::nanoseconds(period) / ticks;
|
||||
SPDLOG_DEBUG("ticks: {}, {}ns", ticks, sleep_interval.count());
|
||||
}
|
||||
|
||||
void poll()
|
||||
{
|
||||
uint32_t stat = 0, gui = 0, curr = 0;
|
||||
while (!quit)
|
||||
{
|
||||
stat = GRBM_STATUS;
|
||||
if (get_radeon_drm_value(fd, RADEON_INFO_READ_REG, &stat))
|
||||
{
|
||||
gui_percent = 0;
|
||||
curr = 0;
|
||||
gui = 0;
|
||||
std::this_thread::sleep_for(sleep_interval * ticks);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stat & (1U << 31)) // gui
|
||||
gui++;
|
||||
|
||||
std::this_thread::sleep_for(sleep_interval);
|
||||
curr++;
|
||||
curr %= ticks;
|
||||
if (!curr)
|
||||
{
|
||||
gui_percent = gui * 100 / ticks;
|
||||
gui = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::unique_ptr<radeon_handles> radeon_ptr;
|
||||
static radeon_ptr radeon_dev;
|
||||
|
||||
void radeon_set_sampling_period(uint32_t period)
|
||||
{
|
||||
if (radeon_dev)
|
||||
radeon_dev->set_sampling_period(period);
|
||||
}
|
||||
|
||||
bool radeon_open(const char *path)
|
||||
{
|
||||
uint32_t drm_major = 0, drm_minor = 0;
|
||||
|
||||
int fd = open(path, O_RDWR | O_CLOEXEC);
|
||||
|
||||
if (fd < 0) {
|
||||
SPDLOG_ERROR("Failed to open DRM device: {}", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
drmVersionPtr ver = drmGetVersion(fd);
|
||||
|
||||
if (!ver) {
|
||||
SPDLOG_ERROR("Failed to query driver version: {}", strerror(errno));
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strcmp(ver->name, "radeon") || !DRM_ATLEAST_VERSION(ver, 2, 42)) {
|
||||
SPDLOG_ERROR("Unsupported driver/version: {} {}.{}.{}",
|
||||
ver->name, ver->version_major, ver->version_minor, ver->version_patchlevel);
|
||||
close(fd);
|
||||
drmFreeVersion(ver);
|
||||
return false;
|
||||
}
|
||||
|
||||
drm_major = ver->version_major;
|
||||
drm_minor = ver->version_minor;
|
||||
drmFreeVersion(ver);
|
||||
|
||||
if (!authenticate_drm(fd)) {
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
radeon_dev = std::make_unique<radeon_handles>(fd, drm_major, drm_minor);
|
||||
return true;
|
||||
}
|
||||
|
||||
void getRadeonInfo_libdrm()
|
||||
{
|
||||
uint64_t value = 0;
|
||||
uint32_t value32 = 0;
|
||||
|
||||
if (!radeon_dev)
|
||||
return;
|
||||
|
||||
gpu_info.load = radeon_dev->gui_percent;
|
||||
|
||||
// TODO one shot?
|
||||
struct drm_radeon_gem_info buffer {};
|
||||
int ret = 0;
|
||||
if (!(ret = ioctl(radeon_dev->fd, DRM_IOCTL_RADEON_GEM_INFO, &buffer)))
|
||||
gpu_info.memoryTotal = buffer.vram_size / (1024.f * 1024.f * 1024.f);
|
||||
else
|
||||
SPDLOG_ERROR("DRM_IOCTL_RADEON_GEM_INFO failed: {}", ret);
|
||||
|
||||
if (!get_radeon_drm_value(radeon_dev->fd, RADEON_INFO_VRAM_USAGE, &value))
|
||||
gpu_info.memoryUsed = value / (1024.f * 1024.f * 1024.f);
|
||||
|
||||
if (!get_radeon_drm_value(radeon_dev->fd, RADEON_INFO_CURRENT_GPU_SCLK, &value32))
|
||||
gpu_info.CoreClock = value32;
|
||||
|
||||
if (!get_radeon_drm_value(radeon_dev->fd, RADEON_INFO_CURRENT_GPU_MCLK, &value32))
|
||||
gpu_info.MemClock = value32;
|
||||
|
||||
if (!get_radeon_drm_value(radeon_dev->fd, RADEON_INFO_CURRENT_GPU_TEMP, &value32))
|
||||
gpu_info.temp = value32 / 1000;
|
||||
|
||||
gpu_info.powerUsage = 0;
|
||||
}
|
@ -1,177 +0,0 @@
|
||||
|
||||
#include "loaders/loader_libdrm.h"
|
||||
#include <iostream>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
// Put these sanity checks here so that they fire at most once
|
||||
// (to avoid cluttering the build output).
|
||||
#if !defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN) && !defined(LIBRARY_LOADER_LIBDRM_H_DT_NEEDED)
|
||||
#error neither LIBRARY_LOADER_LIBDRM_H_DLOPEN nor LIBRARY_LOADER_LIBDRM_H_DT_NEEDED defined
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN) && defined(LIBRARY_LOADER_LIBDRM_H_DT_NEEDED)
|
||||
#error both LIBRARY_LOADER_LIBDRM_H_DLOPEN and LIBRARY_LOADER_LIBDRM_H_DT_NEEDED defined
|
||||
#endif
|
||||
|
||||
libdrm_loader::libdrm_loader() : loaded_(false) {
|
||||
Load();
|
||||
}
|
||||
|
||||
libdrm_loader::~libdrm_loader() {
|
||||
CleanUp(loaded_);
|
||||
}
|
||||
|
||||
bool libdrm_loader::Load() {
|
||||
if (loaded_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN)
|
||||
library = dlopen("libdrm.so.2", RTLD_LAZY);
|
||||
if (!library) {
|
||||
SPDLOG_ERROR("Failed to open " MANGOHUD_ARCH " libdrm.so.2: {}", dlerror());
|
||||
return false;
|
||||
}
|
||||
|
||||
drmGetVersion =
|
||||
reinterpret_cast<decltype(this->drmGetVersion)>(
|
||||
dlsym(library, "drmGetVersion"));
|
||||
if (!drmGetVersion) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
drmFreeVersion =
|
||||
reinterpret_cast<decltype(this->drmFreeVersion)>(
|
||||
dlsym(library, "drmFreeVersion"));
|
||||
if (!drmFreeVersion) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
drmCommandWriteRead =
|
||||
reinterpret_cast<decltype(this->drmCommandWriteRead)>(
|
||||
dlsym(library, "drmCommandWriteRead"));
|
||||
if (!drmCommandWriteRead) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DT_NEEDED)
|
||||
drmGetVersion = &::drmGetVersion;
|
||||
drmFreeVersion = &::drmFreeVersion;
|
||||
drmCommandWriteRead = &::drmCommandWriteRead;
|
||||
#endif
|
||||
|
||||
loaded_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void libdrm_loader::CleanUp(bool unload) {
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN)
|
||||
if (unload) {
|
||||
dlclose(library);
|
||||
library = nullptr;
|
||||
}
|
||||
#endif
|
||||
loaded_ = false;
|
||||
drmGetVersion = nullptr;
|
||||
drmFreeVersion = nullptr;
|
||||
drmCommandWriteRead = nullptr;
|
||||
|
||||
}
|
||||
|
||||
libdrm_amdgpu_loader::libdrm_amdgpu_loader() : loaded_(false) {
|
||||
Load();
|
||||
}
|
||||
|
||||
libdrm_amdgpu_loader::~libdrm_amdgpu_loader() {
|
||||
CleanUp(loaded_);
|
||||
}
|
||||
|
||||
bool libdrm_amdgpu_loader::Load() {
|
||||
if (loaded_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN)
|
||||
library = dlopen("libdrm_amdgpu.so.1", RTLD_LAZY);
|
||||
if (!library) {
|
||||
SPDLOG_ERROR("Failed to open " MANGOHUD_ARCH " libdrm_amdgpu.so.1: {}", dlerror());
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
amdgpu_device_initialize =
|
||||
reinterpret_cast<decltype(this->amdgpu_device_initialize)>(
|
||||
dlsym(library, "amdgpu_device_initialize"));
|
||||
if (!amdgpu_device_initialize) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
amdgpu_device_deinitialize =
|
||||
reinterpret_cast<decltype(this->amdgpu_device_deinitialize)>(
|
||||
dlsym(library, "amdgpu_device_deinitialize"));
|
||||
if (!amdgpu_device_deinitialize) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
amdgpu_query_info =
|
||||
reinterpret_cast<decltype(this->amdgpu_query_info)>(
|
||||
dlsym(library, "amdgpu_query_info"));
|
||||
if (!amdgpu_query_info) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
amdgpu_query_sensor_info =
|
||||
reinterpret_cast<decltype(this->amdgpu_query_sensor_info)>(
|
||||
dlsym(library, "amdgpu_query_sensor_info"));
|
||||
if (!amdgpu_query_sensor_info) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
amdgpu_read_mm_registers =
|
||||
reinterpret_cast<decltype(this->amdgpu_read_mm_registers)>(
|
||||
dlsym(library, "amdgpu_read_mm_registers"));
|
||||
if (!amdgpu_read_mm_registers) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DT_NEEDED)
|
||||
amdgpu_device_initialize = &::amdgpu_device_initialize;
|
||||
amdgpu_device_deinitialize = &::amdgpu_device_deinitialize;
|
||||
amdgpu_query_info = &::amdgpu_query_info;
|
||||
amdgpu_query_sensor_info = &::amdgpu_query_sensor_info;
|
||||
amdgpu_read_mm_registers = &::amdgpu_read_mm_registers;
|
||||
|
||||
#endif
|
||||
|
||||
loaded_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void libdrm_amdgpu_loader::CleanUp(bool unload) {
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN)
|
||||
if (unload) {
|
||||
dlclose(library);
|
||||
library = nullptr;
|
||||
}
|
||||
#endif
|
||||
loaded_ = false;
|
||||
amdgpu_device_initialize = nullptr;
|
||||
amdgpu_device_deinitialize = nullptr;
|
||||
amdgpu_query_info = nullptr;
|
||||
amdgpu_query_sensor_info = nullptr;
|
||||
amdgpu_read_mm_registers = nullptr;
|
||||
|
||||
}
|
||||
|
||||
libdrm_loader g_libdrm;
|
@ -1,82 +0,0 @@
|
||||
|
||||
#ifndef LIBRARY_LOADER_LIBDRM_H
|
||||
#define LIBRARY_LOADER_LIBDRM_H
|
||||
|
||||
#define LIBRARY_LOADER_LIBDRM_H_DLOPEN
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <xf86drm.h>
|
||||
//#include <libdrm/amdgpu_drm.h>
|
||||
//#include <libdrm/amdgpu.h>
|
||||
|
||||
typedef struct amdgpu_device *amdgpu_device_handle;
|
||||
int amdgpu_device_initialize(int fd,
|
||||
uint32_t *major_version,
|
||||
uint32_t *minor_version,
|
||||
amdgpu_device_handle *device_handle);
|
||||
int amdgpu_device_deinitialize(amdgpu_device_handle device_handle);
|
||||
int amdgpu_query_info(amdgpu_device_handle dev, unsigned info_id,
|
||||
unsigned size, void *value);
|
||||
int amdgpu_query_sensor_info(amdgpu_device_handle dev, unsigned sensor_type,
|
||||
unsigned size, void *value);
|
||||
int amdgpu_read_mm_registers(amdgpu_device_handle dev, unsigned dword_offset,
|
||||
unsigned count, uint32_t instance, uint32_t flags,
|
||||
uint32_t *values);
|
||||
|
||||
class libdrm_loader {
|
||||
public:
|
||||
libdrm_loader();
|
||||
~libdrm_loader();
|
||||
|
||||
bool Load();
|
||||
bool IsLoaded() { return loaded_; }
|
||||
|
||||
decltype(&::drmGetVersion) drmGetVersion;
|
||||
decltype(&::drmFreeVersion) drmFreeVersion;
|
||||
decltype(&::drmCommandWriteRead) drmCommandWriteRead;
|
||||
|
||||
private:
|
||||
void CleanUp(bool unload);
|
||||
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN)
|
||||
void* library;
|
||||
#endif
|
||||
|
||||
bool loaded_;
|
||||
|
||||
// Disallow copy constructor and assignment operator.
|
||||
libdrm_loader(const libdrm_loader&);
|
||||
void operator=(const libdrm_loader&);
|
||||
};
|
||||
|
||||
class libdrm_amdgpu_loader {
|
||||
public:
|
||||
libdrm_amdgpu_loader();
|
||||
~libdrm_amdgpu_loader();
|
||||
|
||||
bool Load();
|
||||
bool IsLoaded() { return loaded_; }
|
||||
|
||||
decltype(&::amdgpu_device_initialize) amdgpu_device_initialize;
|
||||
decltype(&::amdgpu_device_deinitialize) amdgpu_device_deinitialize;
|
||||
decltype(&::amdgpu_query_info) amdgpu_query_info;
|
||||
decltype(&::amdgpu_query_sensor_info) amdgpu_query_sensor_info;
|
||||
decltype(&::amdgpu_read_mm_registers) amdgpu_read_mm_registers;
|
||||
|
||||
private:
|
||||
void CleanUp(bool unload);
|
||||
|
||||
#if defined(LIBRARY_LOADER_LIBDRM_H_DLOPEN)
|
||||
void* library;
|
||||
#endif
|
||||
|
||||
bool loaded_;
|
||||
|
||||
// Disallow copy constructor and assignment operator.
|
||||
libdrm_amdgpu_loader(const libdrm_amdgpu_loader&);
|
||||
void operator=(const libdrm_amdgpu_loader&);
|
||||
};
|
||||
|
||||
extern libdrm_loader g_libdrm;
|
||||
|
||||
#endif // LIBRARY_LOADER_LIBDRM_H
|
Loading…
Reference in New Issue