feat: generate drm bindings for xe

This commit is contained in:
Ilya Zlobintsev 2025-01-01 13:35:38 +02:00
parent bf73acbdc2
commit 02e4e400e0
9 changed files with 131 additions and 25 deletions

View File

@ -17,7 +17,7 @@ serde = { workspace = true, features = ["rc"] }
serde_with = { workspace = true }
serde_json = { workspace = true }
tracing-subscriber = { workspace = true }
nix = { workspace = true, features = ["user", "fs"] }
nix = { workspace = true, features = ["user", "fs", "ioctl"] }
chrono = { workspace = true }
tokio = { workspace = true, features = [
"rt",

View File

@ -7,14 +7,21 @@ fn main() {
println!("cargo::rerun-if-changed=wrapper/");
let bindings = bindgen::builder()
.header("wrapper/intel.h")
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindgen::builder()
.header("wrapper/i915.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate intel bindings");
.expect("Unable to generate intel bindings")
.write_to_file(out_path.join("i915_bindings.rs"))
.expect("Couldn't write bindings!");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("intel_bindings.rs"))
bindgen::builder()
.header("wrapper/xe.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate intel bindings")
.write_to_file(out_path.join("xe_bindings.rs"))
.expect("Couldn't write bindings!");
}

View File

@ -1,7 +1,10 @@
mod drm;
use super::GpuController;
use crate::{config, server::vulkan::get_vulkan_info};
use amdgpu_sysfs::gpu_handle::power_profile_mode::PowerProfileModesTable;
use anyhow::{anyhow, Context};
use drm::bindings::i915;
use futures::future::LocalBoxFuture;
use lact_schema::{
ClocksInfo, ClocksTable, ClockspeedStats, DeviceInfo, DeviceStats, DrmInfo, GpuPciInfo,
@ -16,21 +19,9 @@ use std::{
};
use tracing::{debug, error, info, trace, warn};
#[allow(
non_upper_case_globals,
non_camel_case_types,
non_snake_case,
unused,
clippy::upper_case_acronyms,
clippy::unreadable_literal
)]
mod drm {
include!(concat!(env!("OUT_DIR"), "/intel_bindings.rs"));
}
enum DriverType {
Xe,
I915,
Xe,
}
pub struct IntelGpuController {
@ -146,9 +137,9 @@ impl GpuController for IntelGpuController {
};
let drm_info = DrmInfo {
intel: IntelDrmInfo {
execution_units: self.drm_try(drm::drm_intel_get_eu_total),
subslices: self.drm_try(drm::drm_intel_get_subslice_total),
intel: match self.driver_type {
DriverType::I915 => self.get_drm_info_i915(),
DriverType::Xe => self.get_drm_info_xe(),
},
vram_clock_ratio: 1.0,
..Default::default()
@ -228,7 +219,7 @@ impl GpuController for IntelGpuController {
clockspeed,
vram: VramStats {
total: self
.drm_try_2(drm::drm_intel_get_aperture_sizes)
.drm_try_2(i915::drm_intel_get_aperture_sizes)
.map(|(_, total)| total as u64),
used: None,
},
@ -365,6 +356,20 @@ impl IntelGpuController {
}
}
fn get_drm_info_i915(&self) -> IntelDrmInfo {
IntelDrmInfo {
execution_units: self.drm_try(i915::drm_intel_get_eu_total),
subslices: self.drm_try(i915::drm_intel_get_subslice_total),
}
}
fn get_drm_info_xe(&self) -> IntelDrmInfo {
IntelDrmInfo {
execution_units: None,
subslices: None,
}
}
#[cfg_attr(test, allow(unreachable_code, unused_variables))]
fn drm_try<T: Default>(&self, f: unsafe extern "C" fn(c_int, *mut T) -> c_int) -> Option<T> {
#[cfg(test)]

View File

@ -0,0 +1,2 @@
pub mod bindings;
pub mod xe;

View File

@ -0,0 +1,16 @@
#![allow(
non_upper_case_globals,
non_camel_case_types,
non_snake_case,
unused,
clippy::upper_case_acronyms,
clippy::unreadable_literal
)]
pub mod i915 {
include!(concat!(env!("OUT_DIR"), "/i915_bindings.rs"));
}
pub mod xe {
include!(concat!(env!("OUT_DIR"), "/xe_bindings.rs"));
}

View File

@ -0,0 +1,75 @@
/*use super::bindings::xe::{drm_xe_device_query, DRM_XE_DEVICE_QUERY, DRM_XE_DEVICE_QUERY_ENGINES};
use crate::server::gpu_controller::intel::bindings::xe::{
drm_xe_query_engines, DRM_COMMAND_BASE, DRM_IOCTL_BASE, DRM_XE_DEVICE_QUERY_HWCONFIG,
};
use nix::{errno::Errno, ioctl_readwrite};
use std::{
alloc::{self, dealloc},
fs::File,
mem,
os::fd::AsRawFd,
};
ioctl_readwrite!(
xe_device_query,
DRM_IOCTL_BASE,
DRM_COMMAND_BASE + DRM_XE_DEVICE_QUERY,
drm_xe_device_query
);
pub fn query_engines(fd: &File) -> Result<(), Errno> {
unsafe {
let mut query = drm_xe_device_query {
extensions: 0,
query: DRM_XE_DEVICE_QUERY_ENGINES,
size: 0,
data: 0,
reserved: mem::zeroed(),
};
xe_device_query(fd.as_raw_fd(), &mut query)?;
let layout = alloc::Layout::from_size_align(
query.size as usize,
mem::align_of::<drm_xe_query_engines>(),
)
.unwrap();
#[allow(clippy::cast_ptr_alignment)]
let query_engines = alloc::alloc(layout) as *const drm_xe_query_engines;
query.data = query_engines as u64;
xe_device_query(fd.as_raw_fd(), &mut query)?;
println!("query data: {query:?}");
for engine in (*query_engines)
.engines
.as_slice((*query_engines).num_engines as usize)
{
println!("Engine {engine:?}");
}
dealloc(query_engines as *mut u8, layout);
Ok(())
}
}
pub fn query_hwconfig(fd: &File) -> Result<(), Errno> {
unsafe {
let mut query = drm_xe_device_query {
extensions: 0,
query: DRM_XE_DEVICE_QUERY_HWCONFIG,
size: 0,
data: 0,
reserved: mem::zeroed(),
};
xe_device_query(fd.as_raw_fd(), &mut query)?;
println!("{query:?}");
Ok(())
}
}*/

View File

@ -0,0 +1 @@
#include "libdrm/intel_bufmgr.h"

View File

@ -1 +0,0 @@
#include "libdrm/intel_bufmgr.h"

1
lact-daemon/wrapper/xe.h Normal file
View File

@ -0,0 +1 @@
#include "drm/xe_drm.h"