add cram-hal-service

this is the hub service for all the cramium-soc global hardware
issues. Kind of like the equivalent of `llio` on Precursor, but
hopefully slightly better thought-out.

The first service implemented here is an IFRAM allocator, which
is essential for all things DMA and UDMA.
This commit is contained in:
bunnie 2024-01-21 00:34:31 +08:00
parent 6786efbb01
commit c133d61336
5 changed files with 353 additions and 0 deletions

View file

@ -104,6 +104,7 @@ members = [
"libs/xous-pio",
"libs/xous-pl230",
"libs/cramium-hal",
"services/cram-hal-service",
]
resolver = "2"

View file

@ -0,0 +1,23 @@
[package]
name = "cram-hal-service"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
utralib = { version = "0.1.24", optional = true, default-features = false, features = [
"cramium-soc",
] }
xous-api-names = "0.9.55"
xous-api-ticktimer = "0.9.53"
xous = "0.9.57"
log-server = { package = "xous-api-log", version = "0.1.53" }
log = "0.4.14"
cramium-hal = { path = "../../libs/cramium-hal" }
num-derive = { version = "0.3.3", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
rkyv = { version = "0.4.3", default-features = false, features = [
"const_generics",
] }

View file

@ -0,0 +1,13 @@
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum Opcode {
/// Allocate an IFRAM block
MapIfram,
/// Deallocate an IFRAM block
UnmapIfram,
/// Gutter for Invalid Calls
InvalidCall,
/// Exit server
Quit,
}

View file

@ -0,0 +1,148 @@
pub mod api;
use core::sync::atomic::{AtomicU32, Ordering};
pub use api::*;
use num_traits::*;
use xous::{send_message, MemoryAddress, MemoryRange, MemorySize, Message, Result};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
pub const SERVER_NAME_CRAM_HAL: &str = "_Cramium-SoC HAL_";
pub enum IframBank {
Bank0,
Bank1,
}
/// `IframRange` is a range of memory that is suitable for use as a DMA target.
pub struct IframRange {
pub(crate) phys_addr: MemoryAddress,
pub(crate) memory: MemoryRange,
pub(crate) size: MemorySize,
pub(crate) conn: xous::CID,
}
impl IframRange {
/// Request `length` bytes of memory in an optional Bank
///
/// Safety: the caller must ensure that the `IframRange` object lives for the
/// entire lifetime of the *hardware* operation.
///
/// Example of how things can go wrong: `IframRange` is used to allocate
/// a buffer for data that takes a long time to send via a slow UART.
/// The DMA is initiated, and the function exits, dropping `IframRange`.
/// At this point, the range could be re-allocated for another purpose.
///
/// An `IframRange` would naturally live as long as a DMA request
/// if a sender synchronizes on the completion of the DMA request. Thus,
/// the `unsafe` bit happens in "fire-and-forget" contexts, and the
/// caller has to explicitly manage the lifetime of this object to
/// match the maximum duration of the call.
///
/// A simple way to do this is to simply bind the structure to a long-lived
/// scope, but the trade-off is that IFRAM is very limited in capacity
/// and hanging on to chunks much longer than necessary can lead to memory
/// exhaustion.
pub unsafe fn request(length: usize, bank: Option<IframBank>) -> Option<Self> {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_api_names::XousNames::new().unwrap();
let conn =
xns.request_connection(SERVER_NAME_CRAM_HAL).expect("Couldn't connect to Cramium HAL server");
let bank_code = match bank {
Some(IframBank::Bank0) => 0,
Some(IframBank::Bank1) => 1,
_ => 2,
};
match send_message(
conn,
Message::new_blocking_scalar(Opcode::MapIfram.to_usize().unwrap(), length, bank_code, 0, 0),
) {
Ok(Result::Scalar5(maybe_size, maybe_phys_address, _, _, _)) => {
if maybe_size != 0 && maybe_phys_address != 0 {
let mut page_aligned_size = maybe_size / 4096;
if maybe_size % 4096 != 0 {
page_aligned_size += 1;
}
let virtual_pages = xous::map_memory(
core::num::NonZeroUsize::new(maybe_phys_address),
None,
page_aligned_size,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
Some(IframRange {
phys_addr: MemoryAddress::new(maybe_phys_address).unwrap(),
memory: virtual_pages,
size: MemorySize::new(maybe_size).unwrap(),
conn,
})
} else {
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
// "almost" safe because we checked our reference count before disconnecting
// there is a race condition possibly if someone allocates a connection between
// the check above, and the disconnect below.
unsafe { xous::disconnect(conn).unwrap() }
};
None
}
}
_ => {
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
// "almost" safe because we checked our reference count before disconnecting
// there is a race condition possibly if someone allocates a connection between
// the check above, and the disconnect below.
unsafe { xous::disconnect(conn).unwrap() }
};
None
}
}
}
/// Returns the IframRange as a slice, useful for passing to the UDMA API calls.
/// This is `unsafe` because the slice is actually not accessible in virtual memory mode:
/// any attempt to reference the returned slice will result in a panic. The returned
/// slice is *only* useful as a range-checked base/bounds for UDMA API calls.
pub unsafe fn as_phys_slice(&self) -> &[u8] {
core::slice::from_raw_parts(self.phys_addr.get() as *const u8, self.size.get())
}
pub fn as_slice(&self) -> &[u8] {
// safe because `u8` is always representable on our system
unsafe { self.memory.as_slice() }
}
pub fn as_slice_mut(&mut self) -> &mut [u8] {
// safe because `u8` is always representable on our system
unsafe { self.memory.as_slice_mut() }
}
}
impl Drop for IframRange {
fn drop(&mut self) {
match send_message(
self.conn,
Message::new_blocking_scalar(
Opcode::UnmapIfram.to_usize().unwrap(),
self.size.get(),
self.phys_addr.get(),
0,
0,
),
) {
Ok(_) => (),
Err(e) => {
// This probably never happens, but also probably doesn't need to be a hard-panic
// if it does happen because it simply degrades performance; it does not impact
// correctness.
log::error!("Couldn't de-allocate IframRange: {:?}, IFRAM memory is leaking!", e)
}
}
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the
// connection.
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
unsafe {
xous::disconnect(self.conn).unwrap();
}
}
}
}

View file

@ -0,0 +1,168 @@
mod api;
use api::*;
use xous::sender::Sender;
fn try_alloc(ifram_allocs: &mut Vec<Option<Sender>>, size: usize, sender: Sender) -> Option<usize> {
let mut size_pages = size / 4096;
if size % 4096 != 0 {
size_pages += 1;
}
let mut free_start = None;
let mut found_len = 0;
for (index, page) in ifram_allocs.iter().enumerate() {
if page.is_some() {
free_start = None;
found_len = 0;
continue;
} else {
if free_start.is_some() {
found_len += 1;
if found_len >= size_pages {
break;
}
} else {
free_start = Some(index);
found_len = 1;
}
}
}
if let Some(start) = free_start {
if found_len >= size_pages {
// starting point found, and enough pages
assert!(
found_len == size_pages,
"Found pages should be exactly equal to size_pages at this point"
);
for i in ifram_allocs[start..start + found_len].iter_mut() {
*i = Some(sender);
}
// offset relative to start of IFRAM bank
Some(start * 4096)
} else {
// starting point found, but not enough pages
None
}
} else {
// no starting point found
None
}
}
fn main() {
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Debug);
let xns = xous_api_names::XousNames::new().unwrap();
let sid = xns.register_name(cram_hal_service::SERVER_NAME_CRAM_HAL, None).expect("can't register server");
let mut ifram_allocs = [Vec::new(), Vec::new()];
// code is written assuming the IFRAM blocks have the same size. Since this is fixed in
// hardware, it's a good assumption; but the assert is put here in case we port this to
// a new system where for some reason they have different sizes.
assert!(utralib::generated::HW_IFRAM0_MEM_LEN == utralib::generated::HW_IFRAM1_MEM_LEN);
let pages = utralib::generated::HW_IFRAM0_MEM_LEN / 4096;
for _ in 0..pages {
ifram_allocs[0].push(None);
ifram_allocs[1].push(None);
}
// Top page of IFRAM0 is occupied by the log server's Tx buffer. We can't know the
// `Sender` of it, so fill it with a value for `Some` that can't map to any PID.
ifram_allocs[0][31] = Some(Sender::from_usize(usize::MAX));
let mut msg_opt = None;
log::debug!("Starting main loop");
loop {
xous::reply_and_receive_next(sid, &mut msg_opt).unwrap();
let msg = msg_opt.as_mut().unwrap();
let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(api::Opcode::InvalidCall);
log::debug!("{:?}", opcode);
match opcode {
Opcode::MapIfram => {
if let Some(scalar) = msg.body.scalar_message_mut() {
let requested_size = scalar.arg1; // requested size
let requested_bank = scalar.arg2; // Specifies bank 0, 1, or don't care (any number but 0 or 1)
let mut allocated_address = None;
for (bank, table) in ifram_allocs.iter_mut().enumerate() {
if bank == requested_bank || bank > 1 {
match try_alloc(table, requested_size, msg.sender) {
Some(offset) => {
let base = if bank == 0 {
utralib::generated::HW_IFRAM0_MEM
} else {
utralib::generated::HW_IFRAM1_MEM
};
allocated_address = Some(base + offset);
break;
}
None => {}
}
}
}
// responds with size in arg1 (0 means could not be allocated/OOM)
// and address of allocation in arg2
if let Some(addr) = allocated_address {
log::debug!(
"Allocated IFRAM at 0x{:x} to hold at least 0x{:x} bytes",
addr,
requested_size
);
log::debug!("Alloc[0]: {:x?}", ifram_allocs[0]);
log::debug!("Alloc[1]: {:x?}", ifram_allocs[1]);
scalar.arg1 = requested_size;
scalar.arg2 = addr;
} else {
log::debug!(
"Could not allocate IFRAM request of 0x{:x} bytes in bank {}",
requested_size,
requested_bank
);
scalar.arg1 = 0;
scalar.arg2 = 0;
}
}
}
Opcode::UnmapIfram => {
if let Some(scalar) = msg.body.scalar_message() {
let mapped_size = scalar.arg1;
let phys_addr = scalar.arg2;
let bank: usize;
let offset = if utralib::generated::HW_IFRAM0_MEM <= phys_addr
&& phys_addr
< utralib::generated::HW_IFRAM0_MEM + utralib::generated::HW_IFRAM0_MEM_LEN
{
bank = 0;
phys_addr - utralib::generated::HW_IFRAM0_MEM
} else if utralib::generated::HW_IFRAM1_MEM <= phys_addr
&& phys_addr
< utralib::generated::HW_IFRAM1_MEM + utralib::generated::HW_IFRAM1_MEM_LEN
{
bank = 1;
phys_addr - utralib::generated::HW_IFRAM1_MEM
} else {
log::error!("Mapped IFRAM address 0x{:x} is invalid", phys_addr);
panic!("Mapped IFRAM address is invalid");
};
let mut mapped_pages = mapped_size / 4096;
if mapped_size % 4096 != 0 {
mapped_pages += 1;
}
for record in ifram_allocs[bank][offset..offset + mapped_pages].iter_mut() {
*record = None;
}
}
}
Opcode::InvalidCall => {
log::error!("Invalid opcode received: {:?}", msg);
}
Opcode::Quit => {
log::info!("Received quit opcode, exiting.");
break;
}
}
}
xns.unregister_server(sid).unwrap();
xous::destroy_server(sid).unwrap();
log::trace!("quitting");
xous::terminate_process(0)
}