libhypervisor_backends: gunyah: Implement device assigner

Implement device assigner trait for Gunyah hypervisor.
Also implements get_granule_size for gunyah.

Test: m pvmfw_img
Bug: 385802423
Change-Id: I188a9e984d1f90f9d41a48d7c3b80fa54a3f2735
Signed-off-by: Sreenad Menon <quic_sreemeno@quicinc.com>
diff --git a/libs/libhypervisor_backends/src/error.rs b/libs/libhypervisor_backends/src/error.rs
index 3046b0c..ffc6c57 100644
--- a/libs/libhypervisor_backends/src/error.rs
+++ b/libs/libhypervisor_backends/src/error.rs
@@ -18,6 +18,8 @@
 
 #[cfg(target_arch = "aarch64")]
 use super::hypervisor::GeniezoneError;
+#[cfg(target_arch = "aarch64")]
+use super::hypervisor::GunyahError;
 use super::hypervisor::KvmError;
 #[cfg(target_arch = "aarch64")]
 use uuid::Uuid;
@@ -41,6 +43,9 @@
     #[cfg(target_arch = "x86_64")]
     /// Unsupported x86_64 Hypervisor
     UnsupportedHypervisor(u128),
+    #[cfg(target_arch = "aarch64")]
+    /// Failed to invoke Gunyah HVC.
+    GunyahError(GunyahError),
 }
 
 impl fmt::Display for Error {
@@ -58,6 +63,10 @@
                 )
             }
             #[cfg(target_arch = "aarch64")]
+            Self::GunyahError(e) => {
+                write!(f, "Failed to invoke Gunyah HVC: {e}")
+            }
+            #[cfg(target_arch = "aarch64")]
             Self::UnsupportedHypervisorUuid(u) => {
                 write!(f, "Unsupported Hypervisor UUID {u}")
             }
diff --git a/libs/libhypervisor_backends/src/hypervisor.rs b/libs/libhypervisor_backends/src/hypervisor.rs
index 7c274f5..81008f1 100644
--- a/libs/libhypervisor_backends/src/hypervisor.rs
+++ b/libs/libhypervisor_backends/src/hypervisor.rs
@@ -39,6 +39,9 @@
 #[cfg(target_arch = "aarch64")]
 pub use geniezone::GeniezoneError;
 
+#[cfg(target_arch = "aarch64")]
+pub use gunyah::GunyahError;
+
 use alloc::boxed::Box;
 use common::Hypervisor;
 pub use common::{DeviceAssigningHypervisor, MemSharingHypervisor, MmioGuardedHypervisor};
diff --git a/libs/libhypervisor_backends/src/hypervisor/gunyah.rs b/libs/libhypervisor_backends/src/hypervisor/gunyah.rs
index 45c01bf..f25d15a 100644
--- a/libs/libhypervisor_backends/src/hypervisor/gunyah.rs
+++ b/libs/libhypervisor_backends/src/hypervisor/gunyah.rs
@@ -1,10 +1,42 @@
 use super::common::Hypervisor;
+use super::DeviceAssigningHypervisor;
+use crate::{Error, Result};
+use thiserror::Error;
 use uuid::{uuid, Uuid};
 
+const SIZE_4KB: usize = 4 << 10;
+
 pub(super) struct GunyahHypervisor;
 
+/// Error from a Gunyah HVC call.
+#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
+pub enum GunyahError {
+    /// The call is not supported by the implementation.
+    #[error("Gunyah call not supported")]
+    NotSupported,
+}
+
 impl GunyahHypervisor {
     pub const UUID: Uuid = uuid!("c1d58fcd-a453-5fdb-9265-ce36673d5f14");
 }
 
-impl Hypervisor for GunyahHypervisor {}
+impl Hypervisor for GunyahHypervisor {
+    fn as_device_assigner(&self) -> Option<&dyn DeviceAssigningHypervisor> {
+        Some(self)
+    }
+
+    fn get_granule_size(&self) -> Option<usize> {
+        Some(SIZE_4KB)
+    }
+}
+
+impl DeviceAssigningHypervisor for GunyahHypervisor {
+    fn get_phys_mmio_token(&self, base_ipa: u64) -> Result<u64> {
+        // PA = IPA for now.
+        Ok(base_ipa)
+    }
+
+    fn get_phys_iommu_token(&self, _pviommu_id: u64, _vsid: u64) -> Result<(u64, u64)> {
+        Err(Error::GunyahError(GunyahError::NotSupported))
+    }
+}