[vmbase] Move SwiotlbInfo from pvmfw to vmbase
for reuse in both pvmfw and rialto to init shared memory.
Bug: 284462758
Test: m pvmfw_img
Change-Id: Ibe7b8b1bb8d10fbbdc4ec3348e801084c9a93bfd
diff --git a/vmbase/Android.bp b/vmbase/Android.bp
index 6db9ff8..0ef47db 100644
--- a/vmbase/Android.bp
+++ b/vmbase/Android.bp
@@ -67,6 +67,7 @@
"libbuddy_system_allocator",
"libfdtpci",
"libhyp",
+ "liblibfdt",
"liblog_rust_nostd",
"libonce_cell_nostd",
"libsmccc",
diff --git a/vmbase/src/fdt.rs b/vmbase/src/fdt.rs
new file mode 100644
index 0000000..537ca03
--- /dev/null
+++ b/vmbase/src/fdt.rs
@@ -0,0 +1,54 @@
+// Copyright 2023, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! High-level FDT functions.
+
+use crate::cstr;
+use core::ops::Range;
+use libfdt::{self, Fdt, FdtError};
+
+/// Represents information about a SWIOTLB buffer.
+#[derive(Debug)]
+pub struct SwiotlbInfo {
+ /// The address of the SWIOTLB buffer, if available.
+ pub addr: Option<usize>,
+ /// The size of the SWIOTLB buffer.
+ pub size: usize,
+ /// The alignment of the SWIOTLB buffer, if available.
+ pub align: Option<usize>,
+}
+
+impl SwiotlbInfo {
+ /// Creates a `SwiotlbInfo` struct from the given device tree.
+ pub fn new_from_fdt(fdt: &Fdt) -> libfdt::Result<SwiotlbInfo> {
+ let node =
+ fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next().ok_or(FdtError::NotFound)?;
+
+ let (addr, size, align) = if let Some(mut reg) = node.reg()? {
+ let reg = reg.next().ok_or(FdtError::NotFound)?;
+ let size = reg.size.ok_or(FdtError::NotFound)?;
+ (Some(reg.addr.try_into().unwrap()), size.try_into().unwrap(), None)
+ } else {
+ let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?;
+ let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?;
+ (None, size.try_into().unwrap(), Some(align.try_into().unwrap()))
+ };
+ Ok(Self { addr, size, align })
+ }
+
+ /// Returns the fixed range of memory mapped by the SWIOTLB buffer, if available.
+ pub fn fixed_range(&self) -> Option<Range<usize>> {
+ self.addr.map(|addr| addr..addr + self.size)
+ }
+}
diff --git a/vmbase/src/lib.rs b/vmbase/src/lib.rs
index ebb3707..54f3384 100644
--- a/vmbase/src/lib.rs
+++ b/vmbase/src/lib.rs
@@ -22,6 +22,7 @@
mod bionic;
pub mod console;
mod entry;
+pub mod fdt;
pub mod layout;
mod linker;
pub mod logger;
diff --git a/vmbase/src/util.rs b/vmbase/src/util.rs
index 8c230a1..7fe6015 100644
--- a/vmbase/src/util.rs
+++ b/vmbase/src/util.rs
@@ -16,6 +16,14 @@
use core::ops::Range;
+/// Create &CStr out of &str literal
+#[macro_export]
+macro_rules! cstr {
+ ($str:literal) => {{
+ core::ffi::CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
+ }};
+}
+
/// Flatten [[T; N]] into &[T]
/// TODO: use slice::flatten when it graduates from experimental
pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {