Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
| 4 | #![cfg_attr(not(test), no_std)] |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 5 | #![deny(clippy::undocumented_unsafe_blocks)] |
| 6 | #![deny(unsafe_op_in_unsafe_fn)] |
| 7 | #![doc = include_str!("../README.md")] |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 8 | |
Andrew Walbran | 19970ba | 2024-11-25 15:35:00 +0000 | [diff] [blame] | 9 | use core::fmt::{self, Debug, Display, Formatter}; |
Andrew Walbran | 44029a0 | 2024-11-25 15:34:31 +0000 | [diff] [blame] | 10 | use num_enum::{IntoPrimitive, TryFromPrimitive}; |
| 11 | use thiserror::Error; |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 12 | use uuid::Uuid; |
| 13 | |
| 14 | pub mod boot_info; |
Balint Dobszay | b2ff2bc | 2024-12-19 18:59:38 +0100 | [diff] [blame] | 15 | mod ffa_v1_1; |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 16 | mod ffa_v1_2; |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 17 | pub mod memory_management; |
| 18 | pub mod partition_info; |
| 19 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 20 | /// Constant for 4K page size. On many occasions the FF-A spec defines memory size as count of 4K |
| 21 | /// pages, regardless of the current translation granule. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 22 | pub const FFA_PAGE_SIZE_4K: usize = 4096; |
| 23 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 24 | /// Rich error types returned by this module. Should be converted to [`crate::FfaError`] when used |
| 25 | /// with the `FFA_ERROR` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 26 | #[derive(Debug, Error)] |
| 27 | pub enum Error { |
| 28 | #[error("Unrecognised FF-A function ID {0}")] |
| 29 | UnrecognisedFunctionId(u32), |
| 30 | #[error("Unrecognised FF-A feature ID {0}")] |
| 31 | UnrecognisedFeatureId(u8), |
| 32 | #[error("Unrecognised FF-A error code {0}")] |
| 33 | UnrecognisedErrorCode(i32), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 34 | #[error("Unrecognised FF-A Framework Message {0}")] |
| 35 | UnrecognisedFwkMsg(u32), |
| 36 | #[error("Unrecognised FF-A Msg Wait Flag {0}")] |
| 37 | UnrecognisedMsgWaitFlag(u32), |
| 38 | #[error("Unrecognised VM availability status {0}")] |
| 39 | UnrecognisedVmAvailabilityStatus(i32), |
| 40 | #[error("Unrecognised FF-A Warm Boot Type {0}")] |
| 41 | UnrecognisedWarmBootType(u32), |
| 42 | #[error("Invalid version {0}")] |
| 43 | InvalidVersion(u32), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | impl From<Error> for FfaError { |
| 47 | fn from(value: Error) -> Self { |
| 48 | match value { |
| 49 | Error::UnrecognisedFunctionId(_) | Error::UnrecognisedFeatureId(_) => { |
| 50 | Self::NotSupported |
| 51 | } |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 52 | Error::UnrecognisedErrorCode(_) |
| 53 | | Error::UnrecognisedFwkMsg(_) |
| 54 | | Error::InvalidVersion(_) |
| 55 | | Error::UnrecognisedMsgWaitFlag(_) |
| 56 | | Error::UnrecognisedVmAvailabilityStatus(_) |
| 57 | | Error::UnrecognisedWarmBootType(_) => Self::InvalidParameters, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 61 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 62 | /// An FF-A instance is a valid combination of two FF-A components at an exception level boundary. |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 63 | #[derive(PartialEq, Clone, Copy)] |
| 64 | pub enum Instance { |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 65 | /// The instance between the SPMC and SPMD. |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 66 | SecurePhysical, |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 67 | /// The instance between the SPMC and a physical SP (contains the SP's endpoint ID). |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 68 | SecureVirtual(u16), |
| 69 | } |
| 70 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 71 | /// Function IDs of the various FF-A interfaces. |
Andrew Walbran | 969b925 | 2024-11-25 15:35:42 +0000 | [diff] [blame] | 72 | #[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)] |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 73 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFunctionId))] |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 74 | #[repr(u32)] |
| 75 | pub enum FuncId { |
| 76 | Error = 0x84000060, |
| 77 | Success32 = 0x84000061, |
| 78 | Success64 = 0xc4000061, |
| 79 | Interrupt = 0x84000062, |
| 80 | Version = 0x84000063, |
| 81 | Features = 0x84000064, |
| 82 | RxAcquire = 0x84000084, |
| 83 | RxRelease = 0x84000065, |
| 84 | RxTxMap32 = 0x84000066, |
| 85 | RxTxMap64 = 0xc4000066, |
| 86 | RxTxUnmap = 0x84000067, |
| 87 | PartitionInfoGet = 0x84000068, |
Balint Dobszay | e6aa486 | 2025-02-28 16:37:56 +0100 | [diff] [blame] | 88 | PartitionInfoGetRegs = 0xc400008b, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 89 | IdGet = 0x84000069, |
| 90 | SpmIdGet = 0x84000085, |
Balint Dobszay | e6aa486 | 2025-02-28 16:37:56 +0100 | [diff] [blame] | 91 | ConsoleLog32 = 0x8400008a, |
| 92 | ConsoleLog64 = 0xc400008a, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 93 | MsgWait = 0x8400006b, |
| 94 | Yield = 0x8400006c, |
| 95 | Run = 0x8400006d, |
| 96 | NormalWorldResume = 0x8400007c, |
| 97 | MsgSend2 = 0x84000086, |
| 98 | MsgSendDirectReq32 = 0x8400006f, |
| 99 | MsgSendDirectReq64 = 0xc400006f, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 100 | MsgSendDirectReq64_2 = 0xc400008d, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 101 | MsgSendDirectResp32 = 0x84000070, |
| 102 | MsgSendDirectResp64 = 0xc4000070, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 103 | MsgSendDirectResp64_2 = 0xc400008e, |
Balint Dobszay | e6aa486 | 2025-02-28 16:37:56 +0100 | [diff] [blame] | 104 | NotificationBitmapCreate = 0x8400007d, |
| 105 | NotificationBitmapDestroy = 0x8400007e, |
| 106 | NotificationBind = 0x8400007f, |
| 107 | NotificationUnbind = 0x84000080, |
| 108 | NotificationSet = 0x84000081, |
| 109 | NotificationGet = 0x84000082, |
| 110 | NotificationInfoGet32 = 0x84000083, |
| 111 | NotificationInfoGet64 = 0xc4000083, |
| 112 | El3IntrHandle = 0x8400008c, |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 113 | SecondaryEpRegister32 = 0x84000087, |
| 114 | SecondaryEpRegister64 = 0xc4000087, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 115 | MemDonate32 = 0x84000071, |
| 116 | MemDonate64 = 0xc4000071, |
| 117 | MemLend32 = 0x84000072, |
| 118 | MemLend64 = 0xc4000072, |
| 119 | MemShare32 = 0x84000073, |
| 120 | MemShare64 = 0xc4000073, |
| 121 | MemRetrieveReq32 = 0x84000074, |
| 122 | MemRetrieveReq64 = 0xc4000074, |
| 123 | MemRetrieveResp = 0x84000075, |
| 124 | MemRelinquish = 0x84000076, |
| 125 | MemReclaim = 0x84000077, |
| 126 | MemPermGet32 = 0x84000088, |
| 127 | MemPermGet64 = 0xc4000088, |
| 128 | MemPermSet32 = 0x84000089, |
| 129 | MemPermSet64 = 0xc4000089, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 132 | impl FuncId { |
| 133 | /// Returns true if this is a 32-bit call, or false if it is a 64-bit call. |
| 134 | pub fn is_32bit(&self) -> bool { |
| 135 | u32::from(*self) & (1 << 30) != 0 |
| 136 | } |
| 137 | } |
| 138 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 139 | /// Error status codes used by the `FFA_ERROR` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 140 | #[derive(Clone, Copy, Debug, Eq, Error, IntoPrimitive, PartialEq, TryFromPrimitive)] |
| 141 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedErrorCode))] |
| 142 | #[repr(i32)] |
| 143 | pub enum FfaError { |
| 144 | #[error("Not supported")] |
| 145 | NotSupported = -1, |
| 146 | #[error("Invalid parameters")] |
| 147 | InvalidParameters = -2, |
| 148 | #[error("No memory")] |
| 149 | NoMemory = -3, |
| 150 | #[error("Busy")] |
| 151 | Busy = -4, |
| 152 | #[error("Interrupted")] |
| 153 | Interrupted = -5, |
| 154 | #[error("Denied")] |
| 155 | Denied = -6, |
| 156 | #[error("Retry")] |
| 157 | Retry = -7, |
| 158 | #[error("Aborted")] |
| 159 | Aborted = -8, |
| 160 | #[error("No data")] |
| 161 | NoData = -9, |
| 162 | } |
| 163 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 164 | /// Endpoint ID and vCPU ID pair, used by `FFA_ERROR`, `FFA_INTERRUPT` and `FFA_RUN` interfaces. |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 165 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 166 | pub struct TargetInfo { |
| 167 | pub endpoint_id: u16, |
| 168 | pub vcpu_id: u16, |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 171 | impl From<u32> for TargetInfo { |
| 172 | fn from(value: u32) -> Self { |
| 173 | Self { |
| 174 | endpoint_id: (value >> 16) as u16, |
| 175 | vcpu_id: value as u16, |
| 176 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 180 | impl From<TargetInfo> for u32 { |
| 181 | fn from(value: TargetInfo) -> Self { |
Balint Dobszay | e9a3e76 | 2025-02-26 17:29:57 +0100 | [diff] [blame] | 182 | ((value.endpoint_id as u32) << 16) | value.vcpu_id as u32 |
Andrew Walbran | 0d31581 | 2024-11-25 15:36:36 +0000 | [diff] [blame] | 183 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 184 | } |
Andrew Walbran | 0d31581 | 2024-11-25 15:36:36 +0000 | [diff] [blame] | 185 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 186 | /// Arguments for the `FFA_SUCCESS` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 187 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 188 | pub enum SuccessArgs { |
| 189 | Result32([u32; 6]), |
| 190 | Result64([u64; 6]), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 191 | Result64_2([u64; 16]), |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 194 | /// Entrypoint address argument for `FFA_SECONDARY_EP_REGISTER` interface. |
| 195 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 196 | pub enum SecondaryEpRegisterAddr { |
| 197 | Addr32(u32), |
| 198 | Addr64(u64), |
| 199 | } |
| 200 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 201 | /// Version number of the FF-A implementation, `.0` is the major, `.1` is minor the version. |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 202 | #[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)] |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 203 | pub struct Version(pub u16, pub u16); |
| 204 | |
Tomás González | 1f79435 | 2025-03-03 16:47:06 +0000 | [diff] [blame] | 205 | impl Version { |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame^] | 206 | // The FF-A spec mandates that bit[31] of a version number must be 0 |
| 207 | const MBZ_BITS: u32 = 1 << 31; |
| 208 | |
Tomás González | 1f79435 | 2025-03-03 16:47:06 +0000 | [diff] [blame] | 209 | /// Returns whether the caller's version (self) is compatible with the callee's version (input |
| 210 | /// parameter) |
| 211 | pub fn is_compatible_to(&self, callee_version: &Version) -> bool { |
| 212 | self.0 == callee_version.0 && self.1 <= callee_version.1 |
| 213 | } |
| 214 | } |
| 215 | |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame^] | 216 | impl TryFrom<u32> for Version { |
| 217 | type Error = Error; |
| 218 | |
| 219 | fn try_from(val: u32) -> Result<Self, Self::Error> { |
| 220 | if (val & Self::MBZ_BITS) != 0 { |
| 221 | Err(Error::InvalidVersion(val)) |
| 222 | } else { |
| 223 | Ok(Self((val >> 16) as u16, val as u16)) |
| 224 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | impl From<Version> for u32 { |
| 229 | fn from(v: Version) -> Self { |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame^] | 230 | let v_u32 = ((v.0 as u32) << 16) | v.1 as u32; |
| 231 | assert!(v_u32 & Version::MBZ_BITS == 0); |
| 232 | v_u32 |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Andrew Walbran | 19970ba | 2024-11-25 15:35:00 +0000 | [diff] [blame] | 236 | impl Display for Version { |
| 237 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 238 | write!(f, "{}.{}", self.0, self.1) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | impl Debug for Version { |
| 243 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 244 | Display::fmt(self, f) |
| 245 | } |
| 246 | } |
| 247 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 248 | /// Feature IDs used by the `FFA_FEATURES` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 249 | #[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)] |
| 250 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFeatureId))] |
| 251 | #[repr(u8)] |
| 252 | pub enum FeatureId { |
| 253 | NotificationPendingInterrupt = 0x1, |
| 254 | ScheduleReceiverInterrupt = 0x2, |
| 255 | ManagedExitInterrupt = 0x3, |
| 256 | } |
Balint Dobszay | c880249 | 2025-01-15 18:11:39 +0100 | [diff] [blame] | 257 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 258 | /// Arguments for the `FFA_FEATURES` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 259 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 260 | pub enum Feature { |
| 261 | FuncId(FuncId), |
| 262 | FeatureId(FeatureId), |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 263 | Unknown(u32), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 264 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 265 | |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 266 | impl From<u32> for Feature { |
| 267 | fn from(value: u32) -> Self { |
| 268 | // Bit[31] is set for all valid FF-A function IDs so we don't have to check it separately |
| 269 | if let Ok(func_id) = value.try_into() { |
| 270 | Self::FuncId(func_id) |
| 271 | } else if let Ok(feat_id) = (value as u8).try_into() { |
| 272 | Self::FeatureId(feat_id) |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 273 | } else { |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 274 | Self::Unknown(value) |
| 275 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | impl From<Feature> for u32 { |
| 280 | fn from(value: Feature) -> Self { |
| 281 | match value { |
| 282 | Feature::FuncId(func_id) => (1 << 31) | func_id as u32, |
| 283 | Feature::FeatureId(feature_id) => feature_id as u32, |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 284 | Feature::Unknown(id) => panic!("Unknown feature or function ID {:#x?}", id), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 289 | /// RXTX buffer descriptor, used by `FFA_RXTX_MAP`. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 290 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 291 | pub enum RxTxAddr { |
| 292 | Addr32 { rx: u32, tx: u32 }, |
| 293 | Addr64 { rx: u64, tx: u64 }, |
| 294 | } |
| 295 | |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 296 | /// Composite type for capturing success and error return codes for the VM availability messages. |
| 297 | /// |
| 298 | /// Error codes are handled by the `FfaError` type. Having a separate type for errors helps using |
| 299 | /// `Result<(), FfaError>`. If a single type would include both success and error values, |
| 300 | /// then `Err(FfaError::Success)` would be incomprehensible. |
| 301 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 302 | pub enum VmAvailabilityStatus { |
| 303 | Success, |
| 304 | Error(FfaError), |
| 305 | } |
| 306 | |
| 307 | impl TryFrom<i32> for VmAvailabilityStatus { |
| 308 | type Error = Error; |
| 309 | fn try_from(value: i32) -> Result<Self, <Self as TryFrom<i32>>::Error> { |
| 310 | Ok(match value { |
| 311 | 0 => Self::Success, |
| 312 | error_code => Self::Error(FfaError::try_from(error_code)?), |
| 313 | }) |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | impl From<VmAvailabilityStatus> for i32 { |
| 318 | fn from(value: VmAvailabilityStatus) -> Self { |
| 319 | match value { |
| 320 | VmAvailabilityStatus::Success => 0, |
| 321 | VmAvailabilityStatus::Error(error_code) => error_code.into(), |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /// Arguments for the Power Warm Boot `FFA_MSG_SEND_DIRECT_REQ` interface. |
| 327 | #[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)] |
| 328 | #[num_enum(error_type(name = Error, constructor = Error::UnrecognisedWarmBootType))] |
| 329 | #[repr(u32)] |
| 330 | pub enum WarmBootType { |
| 331 | ExitFromSuspend = 0, |
| 332 | ExitFromLowPower = 1, |
| 333 | } |
| 334 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 335 | /// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 336 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 337 | pub enum DirectMsgArgs { |
| 338 | Args32([u32; 5]), |
| 339 | Args64([u64; 5]), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 340 | /// Message for forwarding FFA_VERSION call from Normal world to the SPMC |
| 341 | VersionReq { |
| 342 | version: Version, |
| 343 | }, |
| 344 | /// Response message to forwarded FFA_VERSION call from the Normal world |
| 345 | /// Contains the version returned by the SPMC or None |
| 346 | VersionResp { |
| 347 | version: Option<Version>, |
| 348 | }, |
| 349 | /// Message for a power management operation initiated by a PSCI function |
| 350 | PowerPsciReq32 { |
| 351 | function_id: u32, |
| 352 | // params[i]: Input parameter in w[i] in PSCI function invocation at EL3. |
| 353 | params: [u32; 3], |
| 354 | }, |
| 355 | /// Message for a power management operation initiated by a PSCI function |
| 356 | PowerPsciReq64 { |
| 357 | function_id: u32, |
| 358 | // params[i]: Input parameter in x[i] in PSCI function invocation at EL3. |
| 359 | params: [u64; 3], |
| 360 | }, |
| 361 | /// Message for a warm boot |
| 362 | PowerWarmBootReq { |
| 363 | boot_type: WarmBootType, |
| 364 | }, |
| 365 | /// Response message to indicate return status of the last power management request message |
| 366 | /// Return error code SUCCESS or DENIED as defined in PSCI spec. Caller is left to do the |
| 367 | /// parsing of the return status. |
| 368 | PowerPsciResp { |
| 369 | // TODO: Use arm-psci crate's return status here. |
| 370 | psci_status: i32, |
| 371 | }, |
| 372 | /// Message to signal creation of a VM |
| 373 | VmCreated { |
| 374 | // Globally unique Handle to identify a memory region that contains IMPLEMENTATION DEFINED |
| 375 | // information associated with the created VM. |
| 376 | // The invalid memory region handle must be specified by the Hypervisor if this field is not |
| 377 | // used. |
| 378 | handle: memory_management::Handle, |
| 379 | vm_id: u16, |
| 380 | }, |
| 381 | /// Message to acknowledge creation of a VM |
| 382 | VmCreatedAck { |
| 383 | sp_status: VmAvailabilityStatus, |
| 384 | }, |
| 385 | /// Message to signal destruction of a VM |
| 386 | VmDestructed { |
| 387 | // Globally unique Handle to identify a memory region that contains IMPLEMENTATION DEFINED |
| 388 | // information associated with the created VM. |
| 389 | // The invalid memory region handle must be specified by the Hypervisor if this field is not |
| 390 | // used. |
| 391 | handle: memory_management::Handle, |
| 392 | vm_id: u16, |
| 393 | }, |
| 394 | /// Message to acknowledge destruction of a VM |
| 395 | VmDestructedAck { |
| 396 | sp_status: VmAvailabilityStatus, |
| 397 | }, |
| 398 | } |
| 399 | |
| 400 | impl DirectMsgArgs { |
| 401 | // Flags for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces. |
| 402 | |
| 403 | const FWK_MSG_BITS: u32 = 1 << 31; |
| 404 | const VERSION_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b1000; |
| 405 | const VERSION_RESP: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b1001; |
| 406 | const POWER_PSCI_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS; |
| 407 | const POWER_WARM_BOOT_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0001; |
| 408 | const POWER_PSCI_RESP: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0010; |
| 409 | const VM_CREATED: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0100; |
| 410 | const VM_CREATED_ACK: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0101; |
| 411 | const VM_DESTRUCTED: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0110; |
| 412 | const VM_DESTRUCTED_ACK: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0111; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 415 | /// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}2` interfaces. |
| 416 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 417 | pub struct DirectMsg2Args([u64; 14]); |
| 418 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 419 | /// Descriptor for a dynamically allocated memory buffer that contains the memory transaction |
| 420 | /// descriptor. Used by `FFA_MEM_{DONATE,LEND,SHARE,RETRIEVE_REQ}` interfaces, only when the TX |
| 421 | /// buffer is not used to transmit the transaction descriptor. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 422 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 423 | pub enum MemOpBuf { |
| 424 | Buf32 { addr: u32, page_cnt: u32 }, |
| 425 | Buf64 { addr: u64, page_cnt: u32 }, |
| 426 | } |
| 427 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 428 | /// Memory address argument for `FFA_MEM_PERM_{GET,SET}` interfaces. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 429 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 430 | pub enum MemAddr { |
| 431 | Addr32(u32), |
| 432 | Addr64(u64), |
| 433 | } |
| 434 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 435 | /// Argument for the `FFA_CONSOLE_LOG` interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 436 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 437 | pub enum ConsoleLogChars { |
| 438 | Reg32([u32; 6]), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 439 | Reg64([u64; 16]), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 442 | /// FF-A "message types", the terminology used by the spec is "interfaces". The interfaces are used |
| 443 | /// by FF-A components for communication at an FF-A instance. The spec also describes the valid FF-A |
| 444 | /// instances and conduits for each interface. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 445 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 446 | pub enum Interface { |
| 447 | Error { |
| 448 | target_info: TargetInfo, |
| 449 | error_code: FfaError, |
| 450 | }, |
| 451 | Success { |
| 452 | target_info: u32, |
| 453 | args: SuccessArgs, |
| 454 | }, |
| 455 | Interrupt { |
| 456 | target_info: TargetInfo, |
| 457 | interrupt_id: u32, |
| 458 | }, |
| 459 | Version { |
| 460 | input_version: Version, |
| 461 | }, |
| 462 | VersionOut { |
| 463 | output_version: Version, |
| 464 | }, |
| 465 | Features { |
| 466 | feat_id: Feature, |
| 467 | input_properties: u32, |
| 468 | }, |
| 469 | RxAcquire { |
| 470 | vm_id: u16, |
| 471 | }, |
| 472 | RxRelease { |
| 473 | vm_id: u16, |
| 474 | }, |
| 475 | RxTxMap { |
| 476 | addr: RxTxAddr, |
| 477 | page_cnt: u32, |
| 478 | }, |
| 479 | RxTxUnmap { |
| 480 | id: u16, |
| 481 | }, |
| 482 | PartitionInfoGet { |
| 483 | uuid: Uuid, |
| 484 | flags: u32, |
| 485 | }, |
| 486 | IdGet, |
| 487 | SpmIdGet, |
| 488 | MsgWait, |
| 489 | Yield, |
| 490 | Run { |
| 491 | target_info: TargetInfo, |
| 492 | }, |
| 493 | NormalWorldResume, |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 494 | SecondaryEpRegister { |
| 495 | entrypoint: SecondaryEpRegisterAddr, |
| 496 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 497 | MsgSend2 { |
| 498 | sender_vm_id: u16, |
| 499 | flags: u32, |
| 500 | }, |
| 501 | MsgSendDirectReq { |
| 502 | src_id: u16, |
| 503 | dst_id: u16, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 504 | args: DirectMsgArgs, |
| 505 | }, |
| 506 | MsgSendDirectResp { |
| 507 | src_id: u16, |
| 508 | dst_id: u16, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 509 | args: DirectMsgArgs, |
| 510 | }, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 511 | MsgSendDirectReq2 { |
| 512 | src_id: u16, |
| 513 | dst_id: u16, |
| 514 | uuid: Uuid, |
| 515 | args: DirectMsg2Args, |
| 516 | }, |
| 517 | MsgSendDirectResp2 { |
| 518 | src_id: u16, |
| 519 | dst_id: u16, |
| 520 | args: DirectMsg2Args, |
| 521 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 522 | MemDonate { |
| 523 | total_len: u32, |
| 524 | frag_len: u32, |
| 525 | buf: Option<MemOpBuf>, |
| 526 | }, |
| 527 | MemLend { |
| 528 | total_len: u32, |
| 529 | frag_len: u32, |
| 530 | buf: Option<MemOpBuf>, |
| 531 | }, |
| 532 | MemShare { |
| 533 | total_len: u32, |
| 534 | frag_len: u32, |
| 535 | buf: Option<MemOpBuf>, |
| 536 | }, |
| 537 | MemRetrieveReq { |
| 538 | total_len: u32, |
| 539 | frag_len: u32, |
| 540 | buf: Option<MemOpBuf>, |
| 541 | }, |
| 542 | MemRetrieveResp { |
| 543 | total_len: u32, |
| 544 | frag_len: u32, |
| 545 | }, |
| 546 | MemRelinquish, |
| 547 | MemReclaim { |
| 548 | handle: memory_management::Handle, |
| 549 | flags: u32, |
| 550 | }, |
| 551 | MemPermGet { |
| 552 | addr: MemAddr, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 553 | page_cnt: Option<u32>, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 554 | }, |
| 555 | MemPermSet { |
| 556 | addr: MemAddr, |
| 557 | page_cnt: u32, |
| 558 | mem_perm: u32, |
| 559 | }, |
| 560 | ConsoleLog { |
| 561 | char_cnt: u8, |
| 562 | char_lists: ConsoleLogChars, |
| 563 | }, |
| 564 | } |
| 565 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 566 | impl Interface { |
| 567 | /// Returns the function ID for the call, if it has one. |
| 568 | pub fn function_id(&self) -> Option<FuncId> { |
| 569 | match self { |
| 570 | Interface::Error { .. } => Some(FuncId::Error), |
| 571 | Interface::Success { args, .. } => match args { |
| 572 | SuccessArgs::Result32(..) => Some(FuncId::Success32), |
| 573 | SuccessArgs::Result64(..) | SuccessArgs::Result64_2(..) => Some(FuncId::Success64), |
| 574 | }, |
| 575 | Interface::Interrupt { .. } => Some(FuncId::Interrupt), |
| 576 | Interface::Version { .. } => Some(FuncId::Version), |
| 577 | Interface::VersionOut { .. } => None, |
| 578 | Interface::Features { .. } => Some(FuncId::Features), |
| 579 | Interface::RxAcquire { .. } => Some(FuncId::RxAcquire), |
| 580 | Interface::RxRelease { .. } => Some(FuncId::RxRelease), |
| 581 | Interface::RxTxMap { addr, .. } => match addr { |
| 582 | RxTxAddr::Addr32 { .. } => Some(FuncId::RxTxMap32), |
| 583 | RxTxAddr::Addr64 { .. } => Some(FuncId::RxTxMap64), |
| 584 | }, |
| 585 | Interface::RxTxUnmap { .. } => Some(FuncId::RxTxUnmap), |
| 586 | Interface::PartitionInfoGet { .. } => Some(FuncId::PartitionInfoGet), |
| 587 | Interface::IdGet => Some(FuncId::IdGet), |
| 588 | Interface::SpmIdGet => Some(FuncId::SpmIdGet), |
| 589 | Interface::MsgWait => Some(FuncId::MsgWait), |
| 590 | Interface::Yield => Some(FuncId::Yield), |
| 591 | Interface::Run { .. } => Some(FuncId::Run), |
| 592 | Interface::NormalWorldResume => Some(FuncId::NormalWorldResume), |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 593 | Interface::SecondaryEpRegister { entrypoint } => match entrypoint { |
| 594 | SecondaryEpRegisterAddr::Addr32 { .. } => Some(FuncId::SecondaryEpRegister32), |
| 595 | SecondaryEpRegisterAddr::Addr64 { .. } => Some(FuncId::SecondaryEpRegister64), |
| 596 | }, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 597 | Interface::MsgSend2 { .. } => Some(FuncId::MsgSend2), |
| 598 | Interface::MsgSendDirectReq { args, .. } => match args { |
| 599 | DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectReq32), |
| 600 | DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectReq64), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 601 | DirectMsgArgs::VersionReq { .. } => Some(FuncId::MsgSendDirectReq32), |
| 602 | DirectMsgArgs::PowerPsciReq32 { .. } => Some(FuncId::MsgSendDirectReq32), |
| 603 | DirectMsgArgs::PowerPsciReq64 { .. } => Some(FuncId::MsgSendDirectReq64), |
| 604 | DirectMsgArgs::PowerWarmBootReq { .. } => Some(FuncId::MsgSendDirectReq32), |
| 605 | DirectMsgArgs::VmCreated { .. } => Some(FuncId::MsgSendDirectReq32), |
| 606 | DirectMsgArgs::VmDestructed { .. } => Some(FuncId::MsgSendDirectReq32), |
| 607 | _ => None, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 608 | }, |
| 609 | Interface::MsgSendDirectResp { args, .. } => match args { |
| 610 | DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectResp32), |
| 611 | DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectResp64), |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 612 | DirectMsgArgs::VersionResp { .. } => Some(FuncId::MsgSendDirectResp32), |
| 613 | DirectMsgArgs::PowerPsciResp { .. } => Some(FuncId::MsgSendDirectResp32), |
| 614 | DirectMsgArgs::VmCreatedAck { .. } => Some(FuncId::MsgSendDirectResp32), |
| 615 | DirectMsgArgs::VmDestructedAck { .. } => Some(FuncId::MsgSendDirectResp32), |
| 616 | _ => None, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 617 | }, |
| 618 | Interface::MsgSendDirectReq2 { .. } => Some(FuncId::MsgSendDirectReq64_2), |
| 619 | Interface::MsgSendDirectResp2 { .. } => Some(FuncId::MsgSendDirectResp64_2), |
| 620 | Interface::MemDonate { buf, .. } => match buf { |
| 621 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemDonate64), |
| 622 | _ => Some(FuncId::MemDonate32), |
| 623 | }, |
| 624 | Interface::MemLend { buf, .. } => match buf { |
| 625 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemLend64), |
| 626 | _ => Some(FuncId::MemLend32), |
| 627 | }, |
| 628 | Interface::MemShare { buf, .. } => match buf { |
| 629 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemShare64), |
| 630 | _ => Some(FuncId::MemShare32), |
| 631 | }, |
| 632 | Interface::MemRetrieveReq { buf, .. } => match buf { |
| 633 | Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemRetrieveReq64), |
| 634 | _ => Some(FuncId::MemRetrieveReq32), |
| 635 | }, |
| 636 | Interface::MemRetrieveResp { .. } => Some(FuncId::MemRetrieveResp), |
| 637 | Interface::MemRelinquish => Some(FuncId::MemRelinquish), |
| 638 | Interface::MemReclaim { .. } => Some(FuncId::MemReclaim), |
| 639 | Interface::MemPermGet { addr, .. } => match addr { |
| 640 | MemAddr::Addr32(_) => Some(FuncId::MemPermGet32), |
| 641 | MemAddr::Addr64(_) => Some(FuncId::MemPermGet64), |
| 642 | }, |
| 643 | Interface::MemPermSet { addr, .. } => match addr { |
| 644 | MemAddr::Addr32(_) => Some(FuncId::MemPermSet32), |
| 645 | MemAddr::Addr64(_) => Some(FuncId::MemPermSet64), |
| 646 | }, |
| 647 | Interface::ConsoleLog { char_lists, .. } => match char_lists { |
| 648 | ConsoleLogChars::Reg32(_) => Some(FuncId::ConsoleLog32), |
| 649 | ConsoleLogChars::Reg64(_) => Some(FuncId::ConsoleLog64), |
| 650 | }, |
| 651 | } |
| 652 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 653 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 654 | /// Returns true if this is a 32-bit call, or false if it is a 64-bit call. |
| 655 | pub fn is_32bit(&self) -> bool { |
| 656 | // TODO: self should always have a function ID? |
| 657 | self.function_id().unwrap().is_32bit() |
| 658 | } |
| 659 | |
| 660 | /// Parse interface from register contents. The caller must ensure that the `regs` argument has |
| 661 | /// the correct length: 8 registers for FF-A v1.1 and lower, 18 registers for v1.2 and higher. |
| 662 | pub fn from_regs(version: Version, regs: &[u64]) -> Result<Self, Error> { |
| 663 | let reg_cnt = regs.len(); |
| 664 | |
| 665 | let msg = match reg_cnt { |
| 666 | 8 => { |
| 667 | assert!(version <= Version(1, 1)); |
| 668 | Interface::unpack_regs8(version, regs.try_into().unwrap())? |
| 669 | } |
| 670 | 18 => { |
| 671 | assert!(version >= Version(1, 2)); |
| 672 | match FuncId::try_from(regs[0] as u32)? { |
| 673 | FuncId::ConsoleLog64 |
| 674 | | FuncId::Success64 |
| 675 | | FuncId::MsgSendDirectReq64_2 |
| 676 | | FuncId::MsgSendDirectResp64_2 => { |
| 677 | Interface::unpack_regs18(version, regs.try_into().unwrap())? |
| 678 | } |
| 679 | _ => Interface::unpack_regs8(version, regs[..8].try_into().unwrap())?, |
| 680 | } |
| 681 | } |
| 682 | _ => panic!( |
| 683 | "Invalid number of registers ({}) for FF-A version {}", |
| 684 | reg_cnt, version |
| 685 | ), |
| 686 | }; |
| 687 | |
| 688 | Ok(msg) |
| 689 | } |
| 690 | |
| 691 | fn unpack_regs8(version: Version, regs: &[u64; 8]) -> Result<Self, Error> { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 692 | let fid = FuncId::try_from(regs[0] as u32)?; |
| 693 | |
| 694 | let msg = match fid { |
| 695 | FuncId::Error => Self::Error { |
| 696 | target_info: (regs[1] as u32).into(), |
| 697 | error_code: FfaError::try_from(regs[2] as i32)?, |
| 698 | }, |
| 699 | FuncId::Success32 => Self::Success { |
| 700 | target_info: regs[1] as u32, |
| 701 | args: SuccessArgs::Result32([ |
| 702 | regs[2] as u32, |
| 703 | regs[3] as u32, |
| 704 | regs[4] as u32, |
| 705 | regs[5] as u32, |
| 706 | regs[6] as u32, |
| 707 | regs[7] as u32, |
| 708 | ]), |
| 709 | }, |
| 710 | FuncId::Success64 => Self::Success { |
| 711 | target_info: regs[1] as u32, |
| 712 | args: SuccessArgs::Result64([regs[2], regs[3], regs[4], regs[5], regs[6], regs[7]]), |
| 713 | }, |
| 714 | FuncId::Interrupt => Self::Interrupt { |
| 715 | target_info: (regs[1] as u32).into(), |
| 716 | interrupt_id: regs[2] as u32, |
| 717 | }, |
| 718 | FuncId::Version => Self::Version { |
Tomás González | 83146af | 2025-03-04 11:32:41 +0000 | [diff] [blame^] | 719 | input_version: (regs[1] as u32).try_into()?, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 720 | }, |
| 721 | FuncId::Features => Self::Features { |
Balint Dobszay | c31e0b9 | 2025-03-03 20:16:56 +0100 | [diff] [blame] | 722 | feat_id: (regs[1] as u32).into(), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 723 | input_properties: regs[2] as u32, |
| 724 | }, |
| 725 | FuncId::RxAcquire => Self::RxAcquire { |
| 726 | vm_id: regs[1] as u16, |
| 727 | }, |
| 728 | FuncId::RxRelease => Self::RxRelease { |
| 729 | vm_id: regs[1] as u16, |
| 730 | }, |
| 731 | FuncId::RxTxMap32 => { |
| 732 | let addr = RxTxAddr::Addr32 { |
| 733 | rx: regs[2] as u32, |
| 734 | tx: regs[1] as u32, |
| 735 | }; |
| 736 | let page_cnt = regs[3] as u32; |
| 737 | |
| 738 | Self::RxTxMap { addr, page_cnt } |
| 739 | } |
| 740 | FuncId::RxTxMap64 => { |
| 741 | let addr = RxTxAddr::Addr64 { |
| 742 | rx: regs[2], |
| 743 | tx: regs[1], |
| 744 | }; |
| 745 | let page_cnt = regs[3] as u32; |
| 746 | |
| 747 | Self::RxTxMap { addr, page_cnt } |
| 748 | } |
| 749 | FuncId::RxTxUnmap => Self::RxTxUnmap { id: regs[1] as u16 }, |
| 750 | FuncId::PartitionInfoGet => { |
| 751 | let uuid_words = [ |
| 752 | regs[1] as u32, |
| 753 | regs[2] as u32, |
| 754 | regs[3] as u32, |
| 755 | regs[4] as u32, |
| 756 | ]; |
| 757 | let mut bytes: [u8; 16] = [0; 16]; |
| 758 | for (i, b) in uuid_words.iter().flat_map(|w| w.to_le_bytes()).enumerate() { |
| 759 | bytes[i] = b; |
| 760 | } |
| 761 | Self::PartitionInfoGet { |
| 762 | uuid: Uuid::from_bytes(bytes), |
| 763 | flags: regs[5] as u32, |
| 764 | } |
| 765 | } |
| 766 | FuncId::IdGet => Self::IdGet, |
| 767 | FuncId::SpmIdGet => Self::SpmIdGet, |
| 768 | FuncId::MsgWait => Self::MsgWait, |
| 769 | FuncId::Yield => Self::Yield, |
| 770 | FuncId::Run => Self::Run { |
| 771 | target_info: (regs[1] as u32).into(), |
| 772 | }, |
| 773 | FuncId::NormalWorldResume => Self::NormalWorldResume, |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 774 | FuncId::SecondaryEpRegister32 => Self::SecondaryEpRegister { |
| 775 | entrypoint: SecondaryEpRegisterAddr::Addr32(regs[1] as u32), |
| 776 | }, |
| 777 | FuncId::SecondaryEpRegister64 => Self::SecondaryEpRegister { |
| 778 | entrypoint: SecondaryEpRegisterAddr::Addr64(regs[1]), |
| 779 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 780 | FuncId::MsgSend2 => Self::MsgSend2 { |
| 781 | sender_vm_id: regs[1] as u16, |
| 782 | flags: regs[2] as u32, |
| 783 | }, |
| 784 | FuncId::MsgSendDirectReq32 => Self::MsgSendDirectReq { |
| 785 | src_id: (regs[1] >> 16) as u16, |
| 786 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 787 | args: if (regs[2] as u32 & DirectMsgArgs::FWK_MSG_BITS) != 0 { |
| 788 | match regs[2] as u32 { |
| 789 | DirectMsgArgs::VERSION_REQ => DirectMsgArgs::VersionReq { |
| 790 | version: Version::try_from(regs[3] as u32)?, |
| 791 | }, |
| 792 | DirectMsgArgs::POWER_PSCI_REQ => DirectMsgArgs::PowerPsciReq32 { |
| 793 | function_id: regs[3] as u32, |
| 794 | params: [regs[4] as u32, regs[5] as u32, regs[6] as u32], |
| 795 | }, |
| 796 | DirectMsgArgs::POWER_WARM_BOOT_REQ => DirectMsgArgs::PowerWarmBootReq { |
| 797 | boot_type: WarmBootType::try_from(regs[3] as u32)?, |
| 798 | }, |
| 799 | DirectMsgArgs::VM_CREATED => DirectMsgArgs::VmCreated { |
| 800 | handle: memory_management::Handle::from([ |
| 801 | regs[3] as u32, |
| 802 | regs[4] as u32, |
| 803 | ]), |
| 804 | vm_id: regs[5] as u16, |
| 805 | }, |
| 806 | DirectMsgArgs::VM_DESTRUCTED => DirectMsgArgs::VmDestructed { |
| 807 | handle: memory_management::Handle::from([ |
| 808 | regs[3] as u32, |
| 809 | regs[4] as u32, |
| 810 | ]), |
| 811 | vm_id: regs[5] as u16, |
| 812 | }, |
| 813 | _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)), |
| 814 | } |
| 815 | } else { |
| 816 | DirectMsgArgs::Args32([ |
| 817 | regs[3] as u32, |
| 818 | regs[4] as u32, |
| 819 | regs[5] as u32, |
| 820 | regs[6] as u32, |
| 821 | regs[7] as u32, |
| 822 | ]) |
| 823 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 824 | }, |
| 825 | FuncId::MsgSendDirectReq64 => Self::MsgSendDirectReq { |
| 826 | src_id: (regs[1] >> 16) as u16, |
| 827 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 828 | args: if (regs[2] & DirectMsgArgs::FWK_MSG_BITS as u64) != 0 { |
| 829 | match regs[2] as u32 { |
| 830 | DirectMsgArgs::POWER_PSCI_REQ => DirectMsgArgs::PowerPsciReq64 { |
| 831 | function_id: regs[3] as u32, |
| 832 | params: [regs[4], regs[5], regs[6]], |
| 833 | }, |
| 834 | _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)), |
| 835 | } |
| 836 | } else { |
| 837 | DirectMsgArgs::Args64([regs[3], regs[4], regs[5], regs[6], regs[7]]) |
| 838 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 839 | }, |
| 840 | FuncId::MsgSendDirectResp32 => Self::MsgSendDirectResp { |
| 841 | src_id: (regs[1] >> 16) as u16, |
| 842 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 843 | args: if (regs[2] as u32 & DirectMsgArgs::FWK_MSG_BITS) != 0 { |
| 844 | match regs[2] as u32 { |
| 845 | DirectMsgArgs::VERSION_RESP => { |
| 846 | if regs[3] as i32 == FfaError::NotSupported.into() { |
| 847 | DirectMsgArgs::VersionResp { version: None } |
| 848 | } else { |
| 849 | DirectMsgArgs::VersionResp { |
| 850 | version: Some(Version::try_from(regs[3] as u32)?), |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | DirectMsgArgs::POWER_PSCI_RESP => DirectMsgArgs::PowerPsciResp { |
| 855 | psci_status: regs[3] as i32, |
| 856 | }, |
| 857 | DirectMsgArgs::VM_CREATED_ACK => DirectMsgArgs::VmCreatedAck { |
| 858 | sp_status: (regs[3] as i32).try_into()?, |
| 859 | }, |
| 860 | DirectMsgArgs::VM_DESTRUCTED_ACK => DirectMsgArgs::VmDestructedAck { |
| 861 | sp_status: (regs[3] as i32).try_into()?, |
| 862 | }, |
| 863 | _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)), |
| 864 | } |
| 865 | } else { |
| 866 | DirectMsgArgs::Args32([ |
| 867 | regs[3] as u32, |
| 868 | regs[4] as u32, |
| 869 | regs[5] as u32, |
| 870 | regs[6] as u32, |
| 871 | regs[7] as u32, |
| 872 | ]) |
| 873 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 874 | }, |
| 875 | FuncId::MsgSendDirectResp64 => Self::MsgSendDirectResp { |
| 876 | src_id: (regs[1] >> 16) as u16, |
| 877 | dst_id: regs[1] as u16, |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 878 | args: if (regs[2] & DirectMsgArgs::FWK_MSG_BITS as u64) != 0 { |
| 879 | return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)); |
| 880 | } else { |
| 881 | DirectMsgArgs::Args64([regs[3], regs[4], regs[5], regs[6], regs[7]]) |
| 882 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 883 | }, |
| 884 | FuncId::MemDonate32 => Self::MemDonate { |
| 885 | total_len: regs[1] as u32, |
| 886 | frag_len: regs[2] as u32, |
| 887 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 888 | Some(MemOpBuf::Buf32 { |
| 889 | addr: regs[3] as u32, |
| 890 | page_cnt: regs[4] as u32, |
| 891 | }) |
| 892 | } else { |
| 893 | None |
| 894 | }, |
| 895 | }, |
| 896 | FuncId::MemDonate64 => Self::MemDonate { |
| 897 | total_len: regs[1] as u32, |
| 898 | frag_len: regs[2] as u32, |
| 899 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 900 | Some(MemOpBuf::Buf64 { |
| 901 | addr: regs[3], |
| 902 | page_cnt: regs[4] as u32, |
| 903 | }) |
| 904 | } else { |
| 905 | None |
| 906 | }, |
| 907 | }, |
| 908 | FuncId::MemLend32 => Self::MemLend { |
| 909 | total_len: regs[1] as u32, |
| 910 | frag_len: regs[2] as u32, |
| 911 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 912 | Some(MemOpBuf::Buf32 { |
| 913 | addr: regs[3] as u32, |
| 914 | page_cnt: regs[4] as u32, |
| 915 | }) |
| 916 | } else { |
| 917 | None |
| 918 | }, |
| 919 | }, |
| 920 | FuncId::MemLend64 => Self::MemLend { |
| 921 | total_len: regs[1] as u32, |
| 922 | frag_len: regs[2] as u32, |
| 923 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 924 | Some(MemOpBuf::Buf64 { |
| 925 | addr: regs[3], |
| 926 | page_cnt: regs[4] as u32, |
| 927 | }) |
| 928 | } else { |
| 929 | None |
| 930 | }, |
| 931 | }, |
| 932 | FuncId::MemShare32 => Self::MemShare { |
| 933 | total_len: regs[1] as u32, |
| 934 | frag_len: regs[2] as u32, |
| 935 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 936 | Some(MemOpBuf::Buf32 { |
| 937 | addr: regs[3] as u32, |
| 938 | page_cnt: regs[4] as u32, |
| 939 | }) |
| 940 | } else { |
| 941 | None |
| 942 | }, |
| 943 | }, |
| 944 | FuncId::MemShare64 => Self::MemShare { |
| 945 | total_len: regs[1] as u32, |
| 946 | frag_len: regs[2] as u32, |
| 947 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 948 | Some(MemOpBuf::Buf64 { |
| 949 | addr: regs[3], |
| 950 | page_cnt: regs[4] as u32, |
| 951 | }) |
| 952 | } else { |
| 953 | None |
| 954 | }, |
| 955 | }, |
| 956 | FuncId::MemRetrieveReq32 => Self::MemRetrieveReq { |
| 957 | total_len: regs[1] as u32, |
| 958 | frag_len: regs[2] as u32, |
| 959 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 960 | Some(MemOpBuf::Buf32 { |
| 961 | addr: regs[3] as u32, |
| 962 | page_cnt: regs[4] as u32, |
| 963 | }) |
| 964 | } else { |
| 965 | None |
| 966 | }, |
| 967 | }, |
| 968 | FuncId::MemRetrieveReq64 => Self::MemRetrieveReq { |
| 969 | total_len: regs[1] as u32, |
| 970 | frag_len: regs[2] as u32, |
| 971 | buf: if regs[3] != 0 && regs[4] != 0 { |
| 972 | Some(MemOpBuf::Buf64 { |
| 973 | addr: regs[3], |
| 974 | page_cnt: regs[4] as u32, |
| 975 | }) |
| 976 | } else { |
| 977 | None |
| 978 | }, |
| 979 | }, |
| 980 | FuncId::MemRetrieveResp => Self::MemRetrieveResp { |
| 981 | total_len: regs[1] as u32, |
| 982 | frag_len: regs[2] as u32, |
| 983 | }, |
| 984 | FuncId::MemRelinquish => Self::MemRelinquish, |
| 985 | FuncId::MemReclaim => Self::MemReclaim { |
| 986 | handle: memory_management::Handle::from([regs[1] as u32, regs[2] as u32]), |
| 987 | flags: regs[3] as u32, |
| 988 | }, |
| 989 | FuncId::MemPermGet32 => Self::MemPermGet { |
| 990 | addr: MemAddr::Addr32(regs[1] as u32), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 991 | page_cnt: if version >= Version(1, 3) { |
| 992 | Some(regs[2] as u32) |
| 993 | } else { |
| 994 | None |
| 995 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 996 | }, |
| 997 | FuncId::MemPermGet64 => Self::MemPermGet { |
| 998 | addr: MemAddr::Addr64(regs[1]), |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 999 | page_cnt: if version >= Version(1, 3) { |
| 1000 | Some(regs[2] as u32) |
| 1001 | } else { |
| 1002 | None |
| 1003 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1004 | }, |
| 1005 | FuncId::MemPermSet32 => Self::MemPermSet { |
| 1006 | addr: MemAddr::Addr32(regs[1] as u32), |
| 1007 | page_cnt: regs[2] as u32, |
| 1008 | mem_perm: regs[3] as u32, |
| 1009 | }, |
| 1010 | FuncId::MemPermSet64 => Self::MemPermSet { |
| 1011 | addr: MemAddr::Addr64(regs[1]), |
| 1012 | page_cnt: regs[2] as u32, |
| 1013 | mem_perm: regs[3] as u32, |
| 1014 | }, |
| 1015 | FuncId::ConsoleLog32 => Self::ConsoleLog { |
| 1016 | char_cnt: regs[1] as u8, |
| 1017 | char_lists: ConsoleLogChars::Reg32([ |
| 1018 | regs[2] as u32, |
| 1019 | regs[3] as u32, |
| 1020 | regs[4] as u32, |
| 1021 | regs[5] as u32, |
| 1022 | regs[6] as u32, |
| 1023 | regs[7] as u32, |
| 1024 | ]), |
| 1025 | }, |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1026 | _ => panic!("Invalid number of registers (8) for function {:#x?}", fid), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1027 | }; |
| 1028 | |
| 1029 | Ok(msg) |
| 1030 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1031 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1032 | fn unpack_regs18(version: Version, regs: &[u64; 18]) -> Result<Self, Error> { |
| 1033 | assert!(version >= Version(1, 2)); |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 1034 | |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1035 | let fid = FuncId::try_from(regs[0] as u32)?; |
| 1036 | |
| 1037 | let msg = match fid { |
| 1038 | FuncId::Success64 => Self::Success { |
| 1039 | target_info: regs[1] as u32, |
| 1040 | args: SuccessArgs::Result64_2(regs[2..18].try_into().unwrap()), |
| 1041 | }, |
| 1042 | FuncId::MsgSendDirectReq64_2 => Self::MsgSendDirectReq2 { |
| 1043 | src_id: (regs[1] >> 16) as u16, |
| 1044 | dst_id: regs[1] as u16, |
| 1045 | uuid: Uuid::from_u64_pair(regs[2], regs[3]), |
| 1046 | args: DirectMsg2Args(regs[4..18].try_into().unwrap()), |
| 1047 | }, |
| 1048 | FuncId::MsgSendDirectResp64_2 => Self::MsgSendDirectResp2 { |
| 1049 | src_id: (regs[1] >> 16) as u16, |
| 1050 | dst_id: regs[1] as u16, |
| 1051 | args: DirectMsg2Args(regs[4..18].try_into().unwrap()), |
| 1052 | }, |
| 1053 | FuncId::ConsoleLog64 => Self::ConsoleLog { |
| 1054 | char_cnt: regs[1] as u8, |
| 1055 | char_lists: ConsoleLogChars::Reg64(regs[2..18].try_into().unwrap()), |
| 1056 | }, |
| 1057 | _ => panic!("Invalid number of registers (18) for function {:#x?}", fid), |
| 1058 | }; |
| 1059 | |
| 1060 | Ok(msg) |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1063 | /// Create register contents for an interface. |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1064 | pub fn to_regs(&self, version: Version, regs: &mut [u64]) { |
| 1065 | let reg_cnt = regs.len(); |
| 1066 | |
| 1067 | match reg_cnt { |
| 1068 | 8 => { |
| 1069 | assert!(version <= Version(1, 1)); |
| 1070 | self.pack_regs8(version, (&mut regs[..8]).try_into().unwrap()); |
| 1071 | } |
| 1072 | 18 => { |
| 1073 | assert!(version >= Version(1, 2)); |
| 1074 | |
| 1075 | match self { |
| 1076 | Interface::ConsoleLog { |
| 1077 | char_lists: ConsoleLogChars::Reg64(_), |
| 1078 | .. |
| 1079 | } |
| 1080 | | Interface::Success { |
| 1081 | args: SuccessArgs::Result64_2(_), |
| 1082 | .. |
| 1083 | } |
| 1084 | | Interface::MsgSendDirectReq2 { .. } |
| 1085 | | Interface::MsgSendDirectResp2 { .. } => { |
| 1086 | self.pack_regs18(version, regs.try_into().unwrap()); |
| 1087 | } |
| 1088 | _ => { |
| 1089 | self.pack_regs8(version, (&mut regs[..8]).try_into().unwrap()); |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | _ => panic!("Invalid number of registers {}", reg_cnt), |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | fn pack_regs8(&self, version: Version, a: &mut [u64; 8]) { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1098 | a.fill(0); |
| 1099 | if let Some(function_id) = self.function_id() { |
| 1100 | a[0] = function_id as u64; |
| 1101 | } |
| 1102 | |
| 1103 | match *self { |
| 1104 | Interface::Error { |
| 1105 | target_info, |
| 1106 | error_code, |
| 1107 | } => { |
| 1108 | a[1] = u32::from(target_info).into(); |
| 1109 | a[2] = (error_code as u32).into(); |
| 1110 | } |
| 1111 | Interface::Success { target_info, args } => { |
| 1112 | a[1] = target_info.into(); |
| 1113 | match args { |
| 1114 | SuccessArgs::Result32(regs) => { |
| 1115 | a[2] = regs[0].into(); |
| 1116 | a[3] = regs[1].into(); |
| 1117 | a[4] = regs[2].into(); |
| 1118 | a[5] = regs[3].into(); |
| 1119 | a[6] = regs[4].into(); |
| 1120 | a[7] = regs[5].into(); |
| 1121 | } |
| 1122 | SuccessArgs::Result64(regs) => { |
| 1123 | a[2] = regs[0]; |
| 1124 | a[3] = regs[1]; |
| 1125 | a[4] = regs[2]; |
| 1126 | a[5] = regs[3]; |
| 1127 | a[6] = regs[4]; |
| 1128 | a[7] = regs[5]; |
| 1129 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1130 | _ => panic!("{:#x?} requires 18 registers", args), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | Interface::Interrupt { |
| 1134 | target_info, |
| 1135 | interrupt_id, |
| 1136 | } => { |
| 1137 | a[1] = u32::from(target_info).into(); |
| 1138 | a[2] = interrupt_id.into(); |
| 1139 | } |
| 1140 | Interface::Version { input_version } => { |
| 1141 | a[1] = u32::from(input_version).into(); |
| 1142 | } |
| 1143 | Interface::VersionOut { output_version } => { |
| 1144 | a[0] = u32::from(output_version).into(); |
| 1145 | } |
| 1146 | Interface::Features { |
| 1147 | feat_id, |
| 1148 | input_properties, |
| 1149 | } => { |
| 1150 | a[1] = u32::from(feat_id).into(); |
| 1151 | a[2] = input_properties.into(); |
| 1152 | } |
| 1153 | Interface::RxAcquire { vm_id } => { |
| 1154 | a[1] = vm_id.into(); |
| 1155 | } |
| 1156 | Interface::RxRelease { vm_id } => { |
| 1157 | a[1] = vm_id.into(); |
| 1158 | } |
| 1159 | Interface::RxTxMap { addr, page_cnt } => { |
| 1160 | match addr { |
| 1161 | RxTxAddr::Addr32 { rx, tx } => { |
| 1162 | a[1] = tx.into(); |
| 1163 | a[2] = rx.into(); |
| 1164 | } |
| 1165 | RxTxAddr::Addr64 { rx, tx } => { |
| 1166 | a[1] = tx; |
| 1167 | a[2] = rx; |
| 1168 | } |
| 1169 | } |
| 1170 | a[3] = page_cnt.into(); |
| 1171 | } |
| 1172 | Interface::RxTxUnmap { id } => { |
| 1173 | a[1] = id.into(); |
| 1174 | } |
| 1175 | Interface::PartitionInfoGet { uuid, flags } => { |
| 1176 | let bytes = uuid.into_bytes(); |
| 1177 | a[1] = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]).into(); |
| 1178 | a[2] = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]).into(); |
| 1179 | a[3] = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]).into(); |
| 1180 | a[4] = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]).into(); |
| 1181 | a[5] = flags.into(); |
| 1182 | } |
| 1183 | Interface::IdGet | Interface::SpmIdGet | Interface::MsgWait | Interface::Yield => {} |
| 1184 | Interface::Run { target_info } => { |
| 1185 | a[1] = u32::from(target_info).into(); |
| 1186 | } |
| 1187 | Interface::NormalWorldResume => {} |
Tomás González | 17b9244 | 2025-03-10 16:45:04 +0000 | [diff] [blame] | 1188 | Interface::SecondaryEpRegister { entrypoint } => match entrypoint { |
| 1189 | SecondaryEpRegisterAddr::Addr32(addr) => a[1] = addr as u64, |
| 1190 | SecondaryEpRegisterAddr::Addr64(addr) => a[1] = addr, |
| 1191 | }, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1192 | Interface::MsgSend2 { |
| 1193 | sender_vm_id, |
| 1194 | flags, |
| 1195 | } => { |
| 1196 | a[1] = sender_vm_id.into(); |
| 1197 | a[2] = flags.into(); |
| 1198 | } |
| 1199 | Interface::MsgSendDirectReq { |
| 1200 | src_id, |
| 1201 | dst_id, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1202 | args, |
| 1203 | } => { |
Balint Dobszay | e9a3e76 | 2025-02-26 17:29:57 +0100 | [diff] [blame] | 1204 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1205 | match args { |
| 1206 | DirectMsgArgs::Args32(args) => { |
| 1207 | a[3] = args[0].into(); |
| 1208 | a[4] = args[1].into(); |
| 1209 | a[5] = args[2].into(); |
| 1210 | a[6] = args[3].into(); |
| 1211 | a[7] = args[4].into(); |
| 1212 | } |
| 1213 | DirectMsgArgs::Args64(args) => { |
| 1214 | a[3] = args[0]; |
| 1215 | a[4] = args[1]; |
| 1216 | a[5] = args[2]; |
| 1217 | a[6] = args[3]; |
| 1218 | a[7] = args[4]; |
| 1219 | } |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1220 | DirectMsgArgs::VersionReq { version } => { |
| 1221 | a[2] = DirectMsgArgs::VERSION_REQ.into(); |
| 1222 | a[3] = u32::from(version).into(); |
| 1223 | } |
| 1224 | DirectMsgArgs::PowerPsciReq32 { |
| 1225 | function_id, |
| 1226 | params, |
| 1227 | } => { |
| 1228 | a[2] = DirectMsgArgs::POWER_PSCI_REQ.into(); |
| 1229 | a[3] = function_id.into(); |
| 1230 | a[4] = params[0].into(); |
| 1231 | a[5] = params[1].into(); |
| 1232 | a[6] = params[2].into(); |
| 1233 | } |
| 1234 | DirectMsgArgs::PowerPsciReq64 { |
| 1235 | function_id, |
| 1236 | params, |
| 1237 | } => { |
| 1238 | a[2] = DirectMsgArgs::POWER_PSCI_REQ.into(); |
| 1239 | a[3] = function_id.into(); |
| 1240 | a[4] = params[0]; |
| 1241 | a[5] = params[1]; |
| 1242 | a[6] = params[2]; |
| 1243 | } |
| 1244 | DirectMsgArgs::PowerWarmBootReq { boot_type } => { |
| 1245 | a[2] = DirectMsgArgs::POWER_WARM_BOOT_REQ.into(); |
| 1246 | a[3] = u32::from(boot_type).into(); |
| 1247 | } |
| 1248 | DirectMsgArgs::VmCreated { handle, vm_id } => { |
| 1249 | a[2] = DirectMsgArgs::VM_CREATED.into(); |
| 1250 | let handle_regs: [u32; 2] = handle.into(); |
| 1251 | a[3] = handle_regs[0].into(); |
| 1252 | a[4] = handle_regs[1].into(); |
| 1253 | a[5] = vm_id.into(); |
| 1254 | } |
| 1255 | DirectMsgArgs::VmDestructed { handle, vm_id } => { |
| 1256 | a[2] = DirectMsgArgs::VM_DESTRUCTED.into(); |
| 1257 | let handle_regs: [u32; 2] = handle.into(); |
| 1258 | a[3] = handle_regs[0].into(); |
| 1259 | a[4] = handle_regs[1].into(); |
| 1260 | a[5] = vm_id.into(); |
| 1261 | } |
| 1262 | _ => panic!("Malformed MsgSendDirectReq interface"), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | Interface::MsgSendDirectResp { |
| 1266 | src_id, |
| 1267 | dst_id, |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1268 | args, |
| 1269 | } => { |
Balint Dobszay | e9a3e76 | 2025-02-26 17:29:57 +0100 | [diff] [blame] | 1270 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1271 | match args { |
| 1272 | DirectMsgArgs::Args32(args) => { |
| 1273 | a[3] = args[0].into(); |
| 1274 | a[4] = args[1].into(); |
| 1275 | a[5] = args[2].into(); |
| 1276 | a[6] = args[3].into(); |
| 1277 | a[7] = args[4].into(); |
| 1278 | } |
| 1279 | DirectMsgArgs::Args64(args) => { |
| 1280 | a[3] = args[0]; |
| 1281 | a[4] = args[1]; |
| 1282 | a[5] = args[2]; |
| 1283 | a[6] = args[3]; |
| 1284 | a[7] = args[4]; |
| 1285 | } |
Tomás González | 4d5b0ba | 2025-03-03 17:15:55 +0000 | [diff] [blame] | 1286 | DirectMsgArgs::VersionResp { version } => { |
| 1287 | a[2] = DirectMsgArgs::VERSION_RESP.into(); |
| 1288 | match version { |
| 1289 | None => a[3] = i32::from(FfaError::NotSupported) as u64, |
| 1290 | Some(ver) => a[3] = u32::from(ver).into(), |
| 1291 | } |
| 1292 | } |
| 1293 | DirectMsgArgs::PowerPsciResp { psci_status } => { |
| 1294 | a[2] = DirectMsgArgs::POWER_PSCI_RESP.into(); |
| 1295 | a[3] = psci_status as u64; |
| 1296 | } |
| 1297 | DirectMsgArgs::VmCreatedAck { sp_status } => { |
| 1298 | a[2] = DirectMsgArgs::VM_CREATED_ACK.into(); |
| 1299 | a[4] = i32::from(sp_status) as u64; |
| 1300 | } |
| 1301 | DirectMsgArgs::VmDestructedAck { sp_status } => { |
| 1302 | a[2] = DirectMsgArgs::VM_DESTRUCTED_ACK.into(); |
| 1303 | a[3] = i32::from(sp_status) as u64; |
| 1304 | } |
| 1305 | _ => panic!("Malformed MsgSendDirectResp interface"), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | Interface::MemDonate { |
| 1309 | total_len, |
| 1310 | frag_len, |
| 1311 | buf, |
| 1312 | } => { |
| 1313 | a[1] = total_len.into(); |
| 1314 | a[2] = frag_len.into(); |
| 1315 | (a[3], a[4]) = match buf { |
| 1316 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 1317 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 1318 | None => (0, 0), |
| 1319 | }; |
| 1320 | } |
| 1321 | Interface::MemLend { |
| 1322 | total_len, |
| 1323 | frag_len, |
| 1324 | buf, |
| 1325 | } => { |
| 1326 | a[1] = total_len.into(); |
| 1327 | a[2] = frag_len.into(); |
| 1328 | (a[3], a[4]) = match buf { |
| 1329 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 1330 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 1331 | None => (0, 0), |
| 1332 | }; |
| 1333 | } |
| 1334 | Interface::MemShare { |
| 1335 | total_len, |
| 1336 | frag_len, |
| 1337 | buf, |
| 1338 | } => { |
| 1339 | a[1] = total_len.into(); |
| 1340 | a[2] = frag_len.into(); |
| 1341 | (a[3], a[4]) = match buf { |
| 1342 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 1343 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 1344 | None => (0, 0), |
| 1345 | }; |
| 1346 | } |
| 1347 | Interface::MemRetrieveReq { |
| 1348 | total_len, |
| 1349 | frag_len, |
| 1350 | buf, |
| 1351 | } => { |
| 1352 | a[1] = total_len.into(); |
| 1353 | a[2] = frag_len.into(); |
| 1354 | (a[3], a[4]) = match buf { |
| 1355 | Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()), |
| 1356 | Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()), |
| 1357 | None => (0, 0), |
| 1358 | }; |
| 1359 | } |
| 1360 | Interface::MemRetrieveResp { |
| 1361 | total_len, |
| 1362 | frag_len, |
| 1363 | } => { |
| 1364 | a[1] = total_len.into(); |
| 1365 | a[2] = frag_len.into(); |
| 1366 | } |
| 1367 | Interface::MemRelinquish => {} |
| 1368 | Interface::MemReclaim { handle, flags } => { |
| 1369 | let handle_regs: [u32; 2] = handle.into(); |
| 1370 | a[1] = handle_regs[0].into(); |
| 1371 | a[2] = handle_regs[1].into(); |
| 1372 | a[3] = flags.into(); |
| 1373 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1374 | Interface::MemPermGet { addr, page_cnt } => { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1375 | a[1] = match addr { |
| 1376 | MemAddr::Addr32(addr) => addr.into(), |
| 1377 | MemAddr::Addr64(addr) => addr, |
| 1378 | }; |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1379 | a[2] = if version >= Version(1, 3) { |
| 1380 | page_cnt.unwrap().into() |
| 1381 | } else { |
| 1382 | assert!(page_cnt.is_none()); |
| 1383 | 0 |
| 1384 | } |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1385 | } |
| 1386 | Interface::MemPermSet { |
| 1387 | addr, |
| 1388 | page_cnt, |
| 1389 | mem_perm, |
| 1390 | } => { |
| 1391 | a[1] = match addr { |
| 1392 | MemAddr::Addr32(addr) => addr.into(), |
| 1393 | MemAddr::Addr64(addr) => addr, |
| 1394 | }; |
| 1395 | a[2] = page_cnt.into(); |
| 1396 | a[3] = mem_perm.into(); |
| 1397 | } |
| 1398 | Interface::ConsoleLog { |
| 1399 | char_cnt, |
| 1400 | char_lists, |
| 1401 | } => { |
| 1402 | a[1] = char_cnt.into(); |
| 1403 | match char_lists { |
| 1404 | ConsoleLogChars::Reg32(regs) => { |
| 1405 | a[2] = regs[0].into(); |
| 1406 | a[3] = regs[1].into(); |
| 1407 | a[4] = regs[2].into(); |
| 1408 | a[5] = regs[3].into(); |
| 1409 | a[6] = regs[4].into(); |
| 1410 | a[7] = regs[5].into(); |
| 1411 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1412 | _ => panic!("{:#x?} requires 18 registers", char_lists), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1413 | } |
| 1414 | } |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1415 | _ => panic!("{:#x?} requires 18 registers", self), |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | fn pack_regs18(&self, version: Version, a: &mut [u64; 18]) { |
| 1420 | assert!(version >= Version(1, 2)); |
| 1421 | |
| 1422 | a.fill(0); |
| 1423 | if let Some(function_id) = self.function_id() { |
| 1424 | a[0] = function_id as u64; |
| 1425 | } |
| 1426 | |
| 1427 | match *self { |
| 1428 | Interface::Success { target_info, args } => { |
| 1429 | a[1] = target_info.into(); |
| 1430 | match args { |
| 1431 | SuccessArgs::Result64_2(regs) => a[2..18].copy_from_slice(®s[..16]), |
| 1432 | _ => panic!("{:#x?} requires 8 registers", args), |
| 1433 | } |
| 1434 | } |
| 1435 | Interface::MsgSendDirectReq2 { |
| 1436 | src_id, |
| 1437 | dst_id, |
| 1438 | uuid, |
| 1439 | args, |
| 1440 | } => { |
| 1441 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
| 1442 | (a[2], a[3]) = uuid.as_u64_pair(); |
| 1443 | a[4..18].copy_from_slice(&args.0[..14]); |
| 1444 | } |
| 1445 | Interface::MsgSendDirectResp2 { |
| 1446 | src_id, |
| 1447 | dst_id, |
| 1448 | args, |
| 1449 | } => { |
| 1450 | a[1] = ((src_id as u64) << 16) | dst_id as u64; |
| 1451 | a[2] = 0; |
| 1452 | a[3] = 0; |
| 1453 | a[4..18].copy_from_slice(&args.0[..14]); |
| 1454 | } |
| 1455 | Interface::ConsoleLog { |
| 1456 | char_cnt, |
| 1457 | char_lists, |
| 1458 | } => { |
| 1459 | a[1] = char_cnt.into(); |
| 1460 | match char_lists { |
| 1461 | ConsoleLogChars::Reg64(regs) => a[2..18].copy_from_slice(®s[..16]), |
| 1462 | _ => panic!("{:#x?} requires 8 registers", char_lists), |
| 1463 | } |
| 1464 | } |
| 1465 | _ => panic!("{:#x?} requires 8 registers", self), |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1469 | /// Helper function to create an `FFA_SUCCESS` interface without any arguments. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1470 | pub fn success32_noargs() -> Self { |
| 1471 | Self::Success { |
| 1472 | target_info: 0, |
| 1473 | args: SuccessArgs::Result32([0, 0, 0, 0, 0, 0]), |
| 1474 | } |
| 1475 | } |
| 1476 | |
Tomás González | 4c8c7d2 | 2025-03-10 17:14:57 +0000 | [diff] [blame] | 1477 | /// Helper function to create an `FFA_SUCCESS` interface without any arguments. |
| 1478 | pub fn success64_noargs() -> Self { |
| 1479 | Self::Success { |
| 1480 | target_info: 0, |
| 1481 | args: SuccessArgs::Result64([0, 0, 0, 0, 0, 0]), |
| 1482 | } |
| 1483 | } |
| 1484 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1485 | /// Helper function to create an `FFA_ERROR` interface with an error code. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1486 | pub fn error(error_code: FfaError) -> Self { |
| 1487 | Self::Error { |
| 1488 | target_info: TargetInfo { |
| 1489 | endpoint_id: 0, |
| 1490 | vcpu_id: 0, |
| 1491 | }, |
| 1492 | error_code, |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1497 | /// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG32` message. |
| 1498 | pub const CONSOLE_LOG_32_MAX_CHAR_CNT: u8 = 24; |
Balint Dobszay | de0dc80 | 2025-02-28 14:16:52 +0100 | [diff] [blame] | 1499 | /// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG64` message. |
| 1500 | pub const CONSOLE_LOG_64_MAX_CHAR_CNT: u8 = 128; |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1501 | |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1502 | /// Helper function to convert the "Tightly packed list of characters" format used by the |
| 1503 | /// `FFA_CONSOLE_LOG` interface into a byte slice. |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1504 | pub fn parse_console_log( |
| 1505 | char_cnt: u8, |
| 1506 | char_lists: &ConsoleLogChars, |
| 1507 | log_bytes: &mut [u8], |
| 1508 | ) -> Result<(), FfaError> { |
| 1509 | match char_lists { |
| 1510 | ConsoleLogChars::Reg32(regs) => { |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1511 | if !(1..=CONSOLE_LOG_32_MAX_CHAR_CNT).contains(&char_cnt) { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1512 | return Err(FfaError::InvalidParameters); |
| 1513 | } |
| 1514 | for (i, reg) in regs.iter().enumerate() { |
| 1515 | log_bytes[4 * i..4 * (i + 1)].copy_from_slice(®.to_le_bytes()); |
| 1516 | } |
| 1517 | } |
| 1518 | ConsoleLogChars::Reg64(regs) => { |
Balint Dobszay | a584685 | 2025-02-26 15:38:53 +0100 | [diff] [blame] | 1519 | if !(1..=CONSOLE_LOG_64_MAX_CHAR_CNT).contains(&char_cnt) { |
Balint Dobszay | 3aad957 | 2025-01-17 16:54:11 +0100 | [diff] [blame] | 1520 | return Err(FfaError::InvalidParameters); |
| 1521 | } |
| 1522 | for (i, reg) in regs.iter().enumerate() { |
| 1523 | log_bytes[8 * i..8 * (i + 1)].copy_from_slice(®.to_le_bytes()); |
| 1524 | } |
| 1525 | } |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
Balint Dobszay | c880249 | 2025-01-15 18:11:39 +0100 | [diff] [blame] | 1528 | Ok(()) |
Balint Dobszay | 5bf492f | 2024-07-29 17:21:32 +0200 | [diff] [blame] | 1529 | } |