use core::ffi::{c_int, c_void};
use core::ptr::NonNull;
use libloading::Library;
use super::mapped::{Line, LineDriver, LineError};
use crate::core::line::{CompletionLine, Slot};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HipStream(usize);
impl HipStream {
#[must_use]
pub const unsafe fn from_raw(raw: usize) -> Self {
Self(raw)
}
#[must_use]
pub const fn raw(self) -> usize {
self.0
}
}
const MAPPED: u32 = 0x2;
const COHERENT: u32 = 0x4000_0000;
type HostMalloc = unsafe extern "C" fn(*mut *mut c_void, usize, u32) -> i32;
type HostDevPtr = unsafe extern "C" fn(*mut *mut c_void, *mut c_void, u32) -> i32;
type HostFree = unsafe extern "C" fn(*mut c_void) -> i32;
type GetDevice = unsafe extern "C" fn(*mut c_int) -> i32;
type SetDevice = unsafe extern "C" fn(c_int) -> i32;
type DevSync = unsafe extern "C" fn() -> i32;
type StreamDevice = unsafe extern "C" fn(*mut c_void, *mut c_int) -> i32;
type WriteValue64 = unsafe extern "C" fn(*mut c_void, *mut c_void, u64, u32) -> i32;
pub(crate) struct HipDriver {
malloc: HostMalloc,
dev_ptr: HostDevPtr,
free: HostFree,
get_device: GetDevice,
set_device: SetDevice,
dev_sync: DevSync,
stream_device: StreamDevice,
write: WriteValue64,
_lib: Library,
}
macro_rules! sym {
($lib:ident, $ty:ty, $name:literal) => {
*unsafe { $lib.get::<$ty>($name) }.ok()?
};
}
impl HipDriver {
pub(crate) fn resolve() -> Option<Self> {
let lib = unsafe { Library::new("libamdhip64.so") }.ok()?;
Some(Self {
malloc: sym!(lib, HostMalloc, b"hipHostMalloc"),
dev_ptr: sym!(lib, HostDevPtr, b"hipHostGetDevicePointer"),
free: sym!(lib, HostFree, b"hipHostFree"),
get_device: sym!(lib, GetDevice, b"hipGetDevice"),
set_device: sym!(lib, SetDevice, b"hipSetDevice"),
dev_sync: sym!(lib, DevSync, b"hipDeviceSynchronize"),
stream_device: sym!(lib, StreamDevice, b"hipStreamGetDevice"),
write: sym!(lib, WriteValue64, b"hipStreamWriteValue64"),
_lib: lib,
})
}
}
fn ok(status: i32) -> Result<(), LineError> {
if status == 0 { Ok(()) } else { Err(LineError::Status(status)) }
}
fn domain_of(device: c_int) -> Result<u64, LineError> {
u64::try_from(device)
.map(|d| d + 1)
.map_err(|_| LineError::NoDomain)
}
impl LineDriver for HipDriver {
type Stream = HipStream;
fn domain_current(&self) -> Result<u64, LineError> {
let mut d: c_int = -1;
ok(unsafe { (self.get_device)(&raw mut d) })?;
domain_of(d)
}
fn alloc_host(&self, bytes: usize) -> Result<NonNull<c_void>, LineError> {
let mut page: *mut c_void = core::ptr::null_mut();
ok(unsafe { (self.malloc)(&raw mut page, bytes, MAPPED | COHERENT) })?;
NonNull::new(page).ok_or(LineError::Unavailable)
}
fn device_address(&self, host: NonNull<c_void>) -> Result<u64, LineError> {
let mut device: *mut c_void = core::ptr::null_mut();
ok(unsafe { (self.dev_ptr)(&raw mut device, host.as_ptr(), 0) })?;
Ok(device as u64)
}
fn free_host(&self, host: NonNull<c_void>) {
let _ = unsafe { (self.free)(host.as_ptr()) };
}
fn stream_domain(&self, stream: HipStream) -> Result<u64, LineError> {
let mut d: c_int = -1;
ok(unsafe { (self.stream_device)(stream.0 as *mut c_void, &raw mut d) })?;
domain_of(d)
}
fn write_value(&self, stream: HipStream, addr: u64, value: u64) -> Result<(), LineError> {
let ptr = usize::try_from(addr).map_err(|_| LineError::Unavailable)? as *mut c_void;
ok(unsafe { (self.write)(stream.0 as *mut c_void, ptr, value, 0) })
}
fn settle(&self, domain: u64) -> bool {
let Some(device) = domain.checked_sub(1).and_then(|d| c_int::try_from(d).ok()) else {
return false;
};
let mut prev: c_int = 0;
unsafe {
if (self.get_device)(&raw mut prev) != 0 || (self.set_device)(device) != 0 {
return false;
}
let synced = (self.dev_sync)() == 0;
let restored = (self.set_device)(prev) == 0;
synced && restored
}
}
}
pub struct MappedLine(Line<HipDriver>);
impl MappedLine {
pub fn new() -> Result<Self, LineError> {
Line::with_driver(HipDriver::resolve().ok_or(LineError::Unavailable)?).map(Self)
}
#[must_use]
pub fn line(&self) -> &CompletionLine {
self.0.line()
}
#[must_use]
pub fn device_address(&self) -> u64 {
self.0.device_address()
}
pub fn enqueue_store(&self, stream: HipStream, slot: Slot, value: u64) -> Result<(), LineError> {
self.0.enqueue_store(stream, slot, value)
}
}