pvmfw: Refactor MMIO_GUARD initialization

Refactor the MMIO_GUARD setup into its own module, add a clearer error
type allowing us to identify the faulting HVC in main_wrapper (e.g. for
logs - if they get through - or to map them to a future SYSTEM_RESET2
reboot reason) and let main() assume that logging is working.

This removes the only reason for main() to fail so temporarily remove
the Error type.

Test: atest MicrodroidTestApp
Change-Id: I3dba728fee2c21e24195005e39593305aa3650f6
diff --git a/pvmfw/src/mmio_guard.rs b/pvmfw/src/mmio_guard.rs
new file mode 100644
index 0000000..4cde737
--- /dev/null
+++ b/pvmfw/src/mmio_guard.rs
@@ -0,0 +1,53 @@
+// Copyright 2022, 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.
+
+//! Safe MMIO_GUARD support.
+
+use crate::helpers;
+use crate::smccc;
+use core::{fmt, result};
+
+#[derive(Debug, Clone)]
+pub enum Error {
+    /// Failed to obtain the MMIO_GUARD granule size.
+    InfoFailed(smccc::Error),
+    /// Failed to MMIO_GUARD_MAP a page.
+    MapFailed(smccc::Error),
+    /// The MMIO_GUARD granule used by the hypervisor is not supported.
+    UnsupportedGranule(usize),
+}
+
+type Result<T> = result::Result<T, Error>;
+
+impl fmt::Display for Error {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"),
+            Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"),
+            Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"),
+        }
+    }
+}
+
+pub fn init() -> Result<()> {
+    let mmio_granule = smccc::mmio_guard_info().map_err(Error::InfoFailed)? as usize;
+    if mmio_granule != helpers::SIZE_4KB {
+        return Err(Error::UnsupportedGranule(mmio_granule));
+    }
+    Ok(())
+}
+
+pub fn map(addr: usize) -> Result<()> {
+    smccc::mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed)
+}