blob: 3b300ab9e2f0af5adb670ce6ce1a6b81e47649b2 [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<()> {
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010044 let mmio_granule = mmio_guard_info().map_err(Error::InfoFailed)? as usize;
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010045 if mmio_granule != helpers::SIZE_4KB {
46 return Err(Error::UnsupportedGranule(mmio_granule));
47 }
48 Ok(())
49}
50
51pub fn map(addr: usize) -> Result<()> {
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010052 mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed)
53}
54
55fn mmio_guard_info() -> smccc::Result<u64> {
56 const VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID: u32 = 0xc6000005;
57 let args = [0u64; 17];
58
59 smccc::checked_hvc64(VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID, args)
60}
61
62fn mmio_guard_map(ipa: u64) -> smccc::Result<()> {
63 const VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: u32 = 0xc6000007;
64 let mut args = [0u64; 17];
65 args[0] = ipa;
66
67 smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID, args)
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010068}