use crate::error::Result;
use std::time::Duration;
mod ioctl_transport;
pub use ioctl_transport::IoctlTransport;
#[cfg(feature = "emu")]
mod ffi_transport;
#[cfg(feature = "emu")]
pub use ffi_transport::FfiTransport;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DramInfo {
pub base: u64,
pub size: u64,
pub dma_max_elem_size: u32,
pub dma_max_elem_count: u16,
pub dma_alignment: u16,
}
impl DramInfo {
pub fn alignment(&self) -> u64 {
(self.dma_alignment as u64).max(1).next_power_of_two()
}
}
pub trait DmaHostBuffer {
fn as_mut_slice(&mut self) -> &mut [u8];
fn as_slice(&self) -> &[u8];
fn virt_addr(&self) -> u64;
fn phys_addr(&self) -> u64;
}
pub struct VecDmaBuffer {
buf: Vec<u8>,
}
impl VecDmaBuffer {
pub fn new(size: usize) -> Self {
VecDmaBuffer {
buf: vec![0u8; size],
}
}
}
impl DmaHostBuffer for VecDmaBuffer {
fn as_mut_slice(&mut self) -> &mut [u8] {
&mut self.buf
}
fn as_slice(&self) -> &[u8] {
&self.buf
}
fn virt_addr(&self) -> u64 {
self.buf.as_ptr() as u64
}
fn phys_addr(&self) -> u64 {
0
}
}
#[derive(Clone, Debug)]
pub struct PoppedResponse {
pub bytes: Vec<u8>,
pub cq_index: u16,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeviceConfig {
pub shire_mask: u64,
pub cache_line: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeviceProperties {
pub total_l3_size: u32,
pub total_l2_size: u32,
pub total_scp_size: u32,
pub ddr_bandwidth: u32,
pub minion_boot_freq: Option<u32>,
pub shire_mask: u32,
pub form_factor: u8,
pub tdp: u8,
pub cache_line_size: u8,
pub num_l2_cache_banks: u8,
pub sync_min_shire_id: u8,
pub arch_rev: u8,
pub devnum: u8,
}
pub trait Transport {
fn dram_info(&self) -> Result<DramInfo>;
fn device_config(&self) -> Result<DeviceConfig> {
Ok(DeviceConfig {
shire_mask: 0x1,
cache_line: 64,
})
}
fn device_properties(&self) -> Result<DeviceProperties> {
let cfg = self.device_config()?;
Ok(DeviceProperties {
total_l3_size: 0,
total_l2_size: 0,
total_scp_size: 0,
ddr_bandwidth: 0,
minion_boot_freq: None,
shire_mask: cfg.shire_mask as u32,
form_factor: 0,
tdp: 0,
cache_line_size: cfg.cache_line as u8,
num_l2_cache_banks: 0,
sync_min_shire_id: 0,
arch_rev: 0,
devnum: 0,
})
}
fn fw_update(&self, image: &[u8]) -> Result<()>;
fn sq_count(&self) -> Result<u16>;
fn sq_max_msg_size(&self) -> Result<u16>;
fn push_sq(&self, sq_index: u16, cmd: &[u8], flags: u8) -> Result<bool>;
fn pop_cq(&self) -> Result<Option<PoppedResponse>>;
fn extract_trace(&self, trace_type: u8) -> Result<Vec<u8>>;
fn default_launch_timeout(&self) -> Duration {
Duration::from_secs(10)
}
fn wait_cq(&self, _timeout: Duration) -> Result<bool> {
Ok(true)
}
fn wait_sq(&self, _timeout: Duration) -> Result<bool> {
Ok(true)
}
fn dma_host_buffer(&self, size: usize) -> Result<Box<dyn DmaHostBuffer>> {
Ok(Box::new(VecDmaBuffer::new(size)))
}
}