blob: 4cde73740ca748003270320c9adb646edc56c175 [file] [log] [blame]
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +01001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Safe MMIO_GUARD support.
16
17use crate::helpers;
18use crate::smccc;
19use core::{fmt, result};
20
21#[derive(Debug, Clone)]
22pub enum Error {
23 /// Failed to obtain the MMIO_GUARD granule size.
24 InfoFailed(smccc::Error),
25 /// Failed to MMIO_GUARD_MAP a page.
26 MapFailed(smccc::Error),
27 /// The MMIO_GUARD granule used by the hypervisor is not supported.
28 UnsupportedGranule(usize),
29}
30
31type Result<T> = result::Result<T, Error>;
32
33impl fmt::Display for Error {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 match self {
36 Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"),
37 Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"),
38 Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"),
39 }
40 }
41}
42
43pub fn init() -> Result<()> {
44 let mmio_granule = smccc::mmio_guard_info().map_err(Error::InfoFailed)? as usize;
45 if mmio_granule != helpers::SIZE_4KB {
46 return Err(Error::UnsupportedGranule(mmio_granule));
47 }
48 Ok(())
49}
50
51pub fn map(addr: usize) -> Result<()> {
52 smccc::mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed)
53}