blob: c0070e05a67c52c786abf7cdb5e43a81eee78778 [file] [log] [blame]
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +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
Alice Wang0c21f812023-04-12 12:22:46 +000015//! Structs and functions for making SMCCC calls.
16
Pierre-Clément Tosi8cb23172022-10-19 16:38:25 +010017use core::{fmt, result};
Alice Wang0c21f812023-04-12 12:22:46 +000018// Ideally, smccc shouldn't depend on psci. Smccc isn't split as a separate
19// upstream crate currently mostly for maintenance consideration.
20// See b/245889995 for more context.
21pub use psci::smccc::hvc64;
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010022
23/// Standard SMCCC error values as described in DEN 0028E.
24#[derive(Debug, Clone)]
25pub enum Error {
26 /// The call is not supported by the implementation.
27 NotSupported,
28 /// The call is deemed not required by the implementation.
29 NotRequired,
30 /// One of the call parameters has a non-supported value.
31 InvalidParameter,
32 /// Negative values indicate error.
33 Unknown(i64),
34 /// The call returned a positive value when 0 was expected.
35 Unexpected(u64),
36}
37
38impl fmt::Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 match self {
41 Self::NotSupported => write!(f, "SMCCC call not supported"),
42 Self::NotRequired => write!(f, "SMCCC call not required"),
43 Self::InvalidParameter => write!(f, "SMCCC call received non-supported value"),
Pierre-Clément Tosi4cd3f0d2022-10-17 13:26:28 +010044 Self::Unexpected(v) => write!(f, "Unexpected SMCCC return value {} ({0:#x})", v),
45 Self::Unknown(e) => write!(f, "Unknown SMCCC return value {} ({0:#x})", e),
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010046 }
47 }
48}
49
Alice Wang0c21f812023-04-12 12:22:46 +000050/// Result type with smccc::Error.
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010051pub type Result<T> = result::Result<T, Error>;
Pierre-Clément Tosi8cb23172022-10-19 16:38:25 +010052
Alice Wang0c21f812023-04-12 12:22:46 +000053/// Makes a checked HVC64 call to the hypervisor, following the SMC Calling Convention version 1.4.
54/// Returns Ok only when the return code is 0.
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010055pub fn checked_hvc64_expect_zero(function: u32, args: [u64; 17]) -> Result<()> {
56 match checked_hvc64(function, args)? {
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010057 0 => Ok(()),
58 v => Err(Error::Unexpected(v)),
59 }
60}
61
Alice Wang0c21f812023-04-12 12:22:46 +000062/// Makes a checked HVC64 call to the hypervisor, following the SMC Calling Convention version 1.4.
63/// Returns Ok with the return code only when the return code >= 0.
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010064pub fn checked_hvc64(function: u32, args: [u64; 17]) -> Result<u64> {
65 match hvc64(function, args)[0] as i64 {
66 ret if ret >= 0 => Ok(ret as u64),
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010067 -1 => Err(Error::NotSupported),
68 -2 => Err(Error::NotRequired),
69 -3 => Err(Error::InvalidParameter),
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010070 ret => Err(Error::Unknown(ret)),
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010071 }
72}