Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 1 | // 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 Wang | 0c21f81 | 2023-04-12 12:22:46 +0000 | [diff] [blame] | 15 | //! Structs and functions for making SMCCC calls. |
| 16 | |
Pierre-Clément Tosi | 8cb2317 | 2022-10-19 16:38:25 +0100 | [diff] [blame] | 17 | use core::{fmt, result}; |
Alice Wang | 0c21f81 | 2023-04-12 12:22:46 +0000 | [diff] [blame] | 18 | // 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. |
| 21 | pub use psci::smccc::hvc64; |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 22 | |
| 23 | /// Standard SMCCC error values as described in DEN 0028E. |
| 24 | #[derive(Debug, Clone)] |
| 25 | pub 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 | |
| 38 | impl 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 Tosi | 4cd3f0d | 2022-10-17 13:26:28 +0100 | [diff] [blame] | 44 | 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 Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Alice Wang | 0c21f81 | 2023-04-12 12:22:46 +0000 | [diff] [blame] | 50 | /// Result type with smccc::Error. |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 51 | pub type Result<T> = result::Result<T, Error>; |
Pierre-Clément Tosi | 8cb2317 | 2022-10-19 16:38:25 +0100 | [diff] [blame] | 52 | |
Alice Wang | 0c21f81 | 2023-04-12 12:22:46 +0000 | [diff] [blame] | 53 | /// 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 Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 55 | pub fn checked_hvc64_expect_zero(function: u32, args: [u64; 17]) -> Result<()> { |
| 56 | match checked_hvc64(function, args)? { |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 57 | 0 => Ok(()), |
| 58 | v => Err(Error::Unexpected(v)), |
| 59 | } |
| 60 | } |
| 61 | |
Alice Wang | 0c21f81 | 2023-04-12 12:22:46 +0000 | [diff] [blame] | 62 | /// 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 Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 64 | pub 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 Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 67 | -1 => Err(Error::NotSupported), |
| 68 | -2 => Err(Error::NotRequired), |
| 69 | -3 => Err(Error::InvalidParameter), |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 70 | ret => Err(Error::Unknown(ret)), |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 71 | } |
| 72 | } |