blob: dc965ef3e9c9c4d313bc39468abdf405e98421c6 [file] [log] [blame]
Balint Dobszay5bf492f2024-07-29 17:21:32 +02001// 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 Dobszaya5846852025-02-26 15:38:53 +01005#![deny(clippy::undocumented_unsafe_blocks)]
6#![deny(unsafe_op_in_unsafe_fn)]
7#![doc = include_str!("../README.md")]
Balint Dobszay5bf492f2024-07-29 17:21:32 +02008
Andrew Walbran19970ba2024-11-25 15:35:00 +00009use core::fmt::{self, Debug, Display, Formatter};
Andrew Walbran44029a02024-11-25 15:34:31 +000010use num_enum::{IntoPrimitive, TryFromPrimitive};
11use thiserror::Error;
Balint Dobszay5bf492f2024-07-29 17:21:32 +020012use uuid::Uuid;
13
14pub mod boot_info;
Balint Dobszayb2ff2bc2024-12-19 18:59:38 +010015mod ffa_v1_1;
Balint Dobszayde0dc802025-02-28 14:16:52 +010016mod ffa_v1_2;
Balint Dobszay5bf492f2024-07-29 17:21:32 +020017pub mod memory_management;
18pub mod partition_info;
19
Balint Dobszaya5846852025-02-26 15:38:53 +010020/// 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 Dobszay3aad9572025-01-17 16:54:11 +010022pub const FFA_PAGE_SIZE_4K: usize = 4096;
23
Balint Dobszaya5846852025-02-26 15:38:53 +010024/// Rich error types returned by this module. Should be converted to [`crate::FfaError`] when used
25/// with the `FFA_ERROR` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +010026#[derive(Debug, Error)]
27pub 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ález4d5b0ba2025-03-03 17:15:55 +000034 #[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 Dobszay3aad9572025-01-17 16:54:11 +010044}
45
46impl 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ález4d5b0ba2025-03-03 17:15:55 +000052 Error::UnrecognisedErrorCode(_)
53 | Error::UnrecognisedFwkMsg(_)
54 | Error::InvalidVersion(_)
55 | Error::UnrecognisedMsgWaitFlag(_)
56 | Error::UnrecognisedVmAvailabilityStatus(_)
57 | Error::UnrecognisedWarmBootType(_) => Self::InvalidParameters,
Balint Dobszay3aad9572025-01-17 16:54:11 +010058 }
59 }
60}
Balint Dobszay5bf492f2024-07-29 17:21:32 +020061
Balint Dobszaya5846852025-02-26 15:38:53 +010062/// An FF-A instance is a valid combination of two FF-A components at an exception level boundary.
Balint Dobszay5bf492f2024-07-29 17:21:32 +020063#[derive(PartialEq, Clone, Copy)]
64pub enum Instance {
Balint Dobszaya5846852025-02-26 15:38:53 +010065 /// The instance between the SPMC and SPMD.
Balint Dobszay5bf492f2024-07-29 17:21:32 +020066 SecurePhysical,
Balint Dobszaya5846852025-02-26 15:38:53 +010067 /// The instance between the SPMC and a physical SP (contains the SP's endpoint ID).
Balint Dobszay5bf492f2024-07-29 17:21:32 +020068 SecureVirtual(u16),
69}
70
Balint Dobszaya5846852025-02-26 15:38:53 +010071/// Function IDs of the various FF-A interfaces.
Andrew Walbran969b9252024-11-25 15:35:42 +000072#[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
Balint Dobszay3aad9572025-01-17 16:54:11 +010073#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFunctionId))]
Balint Dobszay5bf492f2024-07-29 17:21:32 +020074#[repr(u32)]
75pub 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 Dobszaye6aa4862025-02-28 16:37:56 +010088 PartitionInfoGetRegs = 0xc400008b,
Balint Dobszay5bf492f2024-07-29 17:21:32 +020089 IdGet = 0x84000069,
90 SpmIdGet = 0x84000085,
Balint Dobszaye6aa4862025-02-28 16:37:56 +010091 ConsoleLog32 = 0x8400008a,
92 ConsoleLog64 = 0xc400008a,
Balint Dobszay5bf492f2024-07-29 17:21:32 +020093 MsgWait = 0x8400006b,
94 Yield = 0x8400006c,
95 Run = 0x8400006d,
96 NormalWorldResume = 0x8400007c,
97 MsgSend2 = 0x84000086,
98 MsgSendDirectReq32 = 0x8400006f,
99 MsgSendDirectReq64 = 0xc400006f,
Balint Dobszayde0dc802025-02-28 14:16:52 +0100100 MsgSendDirectReq64_2 = 0xc400008d,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200101 MsgSendDirectResp32 = 0x84000070,
102 MsgSendDirectResp64 = 0xc4000070,
Balint Dobszayde0dc802025-02-28 14:16:52 +0100103 MsgSendDirectResp64_2 = 0xc400008e,
Balint Dobszaye6aa4862025-02-28 16:37:56 +0100104 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ález17b92442025-03-10 16:45:04 +0000113 SecondaryEpRegister32 = 0x84000087,
114 SecondaryEpRegister64 = 0xc4000087,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200115 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 Dobszay5bf492f2024-07-29 17:21:32 +0200130}
131
Balint Dobszayde0dc802025-02-28 14:16:52 +0100132impl 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 Dobszaya5846852025-02-26 15:38:53 +0100139/// Error status codes used by the `FFA_ERROR` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100140#[derive(Clone, Copy, Debug, Eq, Error, IntoPrimitive, PartialEq, TryFromPrimitive)]
141#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedErrorCode))]
142#[repr(i32)]
143pub 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 Dobszaya5846852025-02-26 15:38:53 +0100164/// Endpoint ID and vCPU ID pair, used by `FFA_ERROR`, `FFA_INTERRUPT` and `FFA_RUN` interfaces.
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200165#[derive(Debug, Eq, PartialEq, Clone, Copy)]
Balint Dobszay3aad9572025-01-17 16:54:11 +0100166pub struct TargetInfo {
167 pub endpoint_id: u16,
168 pub vcpu_id: u16,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200169}
170
Balint Dobszay3aad9572025-01-17 16:54:11 +0100171impl 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 Dobszay5bf492f2024-07-29 17:21:32 +0200177 }
178}
179
Balint Dobszay3aad9572025-01-17 16:54:11 +0100180impl From<TargetInfo> for u32 {
181 fn from(value: TargetInfo) -> Self {
Balint Dobszaye9a3e762025-02-26 17:29:57 +0100182 ((value.endpoint_id as u32) << 16) | value.vcpu_id as u32
Andrew Walbran0d315812024-11-25 15:36:36 +0000183 }
Balint Dobszay3aad9572025-01-17 16:54:11 +0100184}
Andrew Walbran0d315812024-11-25 15:36:36 +0000185
Balint Dobszaya5846852025-02-26 15:38:53 +0100186/// Arguments for the `FFA_SUCCESS` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100187#[derive(Debug, Eq, PartialEq, Clone, Copy)]
188pub enum SuccessArgs {
189 Result32([u32; 6]),
190 Result64([u64; 6]),
Balint Dobszayde0dc802025-02-28 14:16:52 +0100191 Result64_2([u64; 16]),
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200192}
193
Tomás González17b92442025-03-10 16:45:04 +0000194/// Entrypoint address argument for `FFA_SECONDARY_EP_REGISTER` interface.
195#[derive(Debug, Eq, PartialEq, Clone, Copy)]
196pub enum SecondaryEpRegisterAddr {
197 Addr32(u32),
198 Addr64(u64),
199}
200
Balint Dobszaya5846852025-02-26 15:38:53 +0100201/// Version number of the FF-A implementation, `.0` is the major, `.1` is minor the version.
Balint Dobszayde0dc802025-02-28 14:16:52 +0100202#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200203pub struct Version(pub u16, pub u16);
204
Tomás González1f794352025-03-03 16:47:06 +0000205impl Version {
Tomás González83146af2025-03-04 11:32:41 +0000206 // 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ález1f794352025-03-03 16:47:06 +0000209 /// 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ález83146af2025-03-04 11:32:41 +0000216impl 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 Dobszay5bf492f2024-07-29 17:21:32 +0200225 }
226}
227
228impl From<Version> for u32 {
229 fn from(v: Version) -> Self {
Tomás González83146af2025-03-04 11:32:41 +0000230 let v_u32 = ((v.0 as u32) << 16) | v.1 as u32;
231 assert!(v_u32 & Version::MBZ_BITS == 0);
232 v_u32
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200233 }
234}
235
Andrew Walbran19970ba2024-11-25 15:35:00 +0000236impl Display for Version {
237 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
238 write!(f, "{}.{}", self.0, self.1)
239 }
240}
241
242impl Debug for Version {
243 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
244 Display::fmt(self, f)
245 }
246}
247
Balint Dobszaya5846852025-02-26 15:38:53 +0100248/// Feature IDs used by the `FFA_FEATURES` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100249#[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
250#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFeatureId))]
251#[repr(u8)]
252pub enum FeatureId {
253 NotificationPendingInterrupt = 0x1,
254 ScheduleReceiverInterrupt = 0x2,
255 ManagedExitInterrupt = 0x3,
256}
Balint Dobszayc8802492025-01-15 18:11:39 +0100257
Balint Dobszaya5846852025-02-26 15:38:53 +0100258/// Arguments for the `FFA_FEATURES` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100259#[derive(Debug, Eq, PartialEq, Clone, Copy)]
260pub enum Feature {
261 FuncId(FuncId),
262 FeatureId(FeatureId),
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100263 Unknown(u32),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100264}
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200265
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100266impl 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 Dobszay3aad9572025-01-17 16:54:11 +0100273 } else {
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100274 Self::Unknown(value)
275 }
Balint Dobszay3aad9572025-01-17 16:54:11 +0100276 }
277}
278
279impl 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 Dobszayc31e0b92025-03-03 20:16:56 +0100284 Feature::Unknown(id) => panic!("Unknown feature or function ID {:#x?}", id),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100285 }
286 }
287}
288
Balint Dobszaya5846852025-02-26 15:38:53 +0100289/// RXTX buffer descriptor, used by `FFA_RXTX_MAP`.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100290#[derive(Debug, Eq, PartialEq, Clone, Copy)]
291pub enum RxTxAddr {
292 Addr32 { rx: u32, tx: u32 },
293 Addr64 { rx: u64, tx: u64 },
294}
295
Tomás González4d5b0ba2025-03-03 17:15:55 +0000296/// 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)]
302pub enum VmAvailabilityStatus {
303 Success,
304 Error(FfaError),
305}
306
307impl 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
317impl 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)]
330pub enum WarmBootType {
331 ExitFromSuspend = 0,
332 ExitFromLowPower = 1,
333}
334
Balint Dobszaya5846852025-02-26 15:38:53 +0100335/// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100336#[derive(Debug, Eq, PartialEq, Clone, Copy)]
337pub enum DirectMsgArgs {
338 Args32([u32; 5]),
339 Args64([u64; 5]),
Tomás González4d5b0ba2025-03-03 17:15:55 +0000340 /// 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
400impl 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 Dobszay3aad9572025-01-17 16:54:11 +0100413}
414
Balint Dobszayde0dc802025-02-28 14:16:52 +0100415/// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}2` interfaces.
416#[derive(Debug, Eq, PartialEq, Clone, Copy)]
417pub struct DirectMsg2Args([u64; 14]);
418
Balint Dobszaya5846852025-02-26 15:38:53 +0100419/// 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 Dobszay3aad9572025-01-17 16:54:11 +0100422#[derive(Debug, Eq, PartialEq, Clone, Copy)]
423pub enum MemOpBuf {
424 Buf32 { addr: u32, page_cnt: u32 },
425 Buf64 { addr: u64, page_cnt: u32 },
426}
427
Balint Dobszaya5846852025-02-26 15:38:53 +0100428/// Memory address argument for `FFA_MEM_PERM_{GET,SET}` interfaces.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100429#[derive(Debug, Eq, PartialEq, Clone, Copy)]
430pub enum MemAddr {
431 Addr32(u32),
432 Addr64(u64),
433}
434
Balint Dobszayde0dc802025-02-28 14:16:52 +0100435/// Argument for the `FFA_CONSOLE_LOG` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100436#[derive(Debug, Eq, PartialEq, Clone, Copy)]
437pub enum ConsoleLogChars {
438 Reg32([u32; 6]),
Balint Dobszayde0dc802025-02-28 14:16:52 +0100439 Reg64([u64; 16]),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100440}
441
Balint Dobszaya5846852025-02-26 15:38:53 +0100442/// 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 Dobszay3aad9572025-01-17 16:54:11 +0100445#[derive(Debug, Eq, PartialEq, Clone, Copy)]
446pub 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ález17b92442025-03-10 16:45:04 +0000494 SecondaryEpRegister {
495 entrypoint: SecondaryEpRegisterAddr,
496 },
Balint Dobszay3aad9572025-01-17 16:54:11 +0100497 MsgSend2 {
498 sender_vm_id: u16,
499 flags: u32,
500 },
501 MsgSendDirectReq {
502 src_id: u16,
503 dst_id: u16,
Balint Dobszay3aad9572025-01-17 16:54:11 +0100504 args: DirectMsgArgs,
505 },
506 MsgSendDirectResp {
507 src_id: u16,
508 dst_id: u16,
Balint Dobszay3aad9572025-01-17 16:54:11 +0100509 args: DirectMsgArgs,
510 },
Balint Dobszayde0dc802025-02-28 14:16:52 +0100511 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 Dobszay3aad9572025-01-17 16:54:11 +0100522 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 Dobszayde0dc802025-02-28 14:16:52 +0100553 page_cnt: Option<u32>,
Balint Dobszay3aad9572025-01-17 16:54:11 +0100554 },
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 Dobszayde0dc802025-02-28 14:16:52 +0100566impl 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ález17b92442025-03-10 16:45:04 +0000593 Interface::SecondaryEpRegister { entrypoint } => match entrypoint {
594 SecondaryEpRegisterAddr::Addr32 { .. } => Some(FuncId::SecondaryEpRegister32),
595 SecondaryEpRegisterAddr::Addr64 { .. } => Some(FuncId::SecondaryEpRegister64),
596 },
Balint Dobszayde0dc802025-02-28 14:16:52 +0100597 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ález4d5b0ba2025-03-03 17:15:55 +0000601 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 Dobszayde0dc802025-02-28 14:16:52 +0100608 },
609 Interface::MsgSendDirectResp { args, .. } => match args {
610 DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectResp32),
611 DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectResp64),
Tomás González4d5b0ba2025-03-03 17:15:55 +0000612 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 Dobszayde0dc802025-02-28 14:16:52 +0100617 },
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 Dobszay3aad9572025-01-17 16:54:11 +0100653
Balint Dobszayde0dc802025-02-28 14:16:52 +0100654 /// 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 Dobszay3aad9572025-01-17 16:54:11 +0100692 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ález83146af2025-03-04 11:32:41 +0000719 input_version: (regs[1] as u32).try_into()?,
Balint Dobszay3aad9572025-01-17 16:54:11 +0100720 },
721 FuncId::Features => Self::Features {
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100722 feat_id: (regs[1] as u32).into(),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100723 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ález17b92442025-03-10 16:45:04 +0000774 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 Dobszay3aad9572025-01-17 16:54:11 +0100780 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ález4d5b0ba2025-03-03 17:15:55 +0000787 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 Dobszay3aad9572025-01-17 16:54:11 +0100824 },
825 FuncId::MsgSendDirectReq64 => Self::MsgSendDirectReq {
826 src_id: (regs[1] >> 16) as u16,
827 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +0000828 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 Dobszay3aad9572025-01-17 16:54:11 +0100839 },
840 FuncId::MsgSendDirectResp32 => Self::MsgSendDirectResp {
841 src_id: (regs[1] >> 16) as u16,
842 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +0000843 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 Dobszay3aad9572025-01-17 16:54:11 +0100874 },
875 FuncId::MsgSendDirectResp64 => Self::MsgSendDirectResp {
876 src_id: (regs[1] >> 16) as u16,
877 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +0000878 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 Dobszay3aad9572025-01-17 16:54:11 +0100883 },
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 Dobszayde0dc802025-02-28 14:16:52 +0100991 page_cnt: if version >= Version(1, 3) {
992 Some(regs[2] as u32)
993 } else {
994 None
995 },
Balint Dobszay3aad9572025-01-17 16:54:11 +0100996 },
997 FuncId::MemPermGet64 => Self::MemPermGet {
998 addr: MemAddr::Addr64(regs[1]),
Balint Dobszayde0dc802025-02-28 14:16:52 +0100999 page_cnt: if version >= Version(1, 3) {
1000 Some(regs[2] as u32)
1001 } else {
1002 None
1003 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001004 },
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 Dobszayde0dc802025-02-28 14:16:52 +01001026 _ => panic!("Invalid number of registers (8) for function {:#x?}", fid),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001027 };
1028
1029 Ok(msg)
1030 }
Balint Dobszay3aad9572025-01-17 16:54:11 +01001031
Balint Dobszayde0dc802025-02-28 14:16:52 +01001032 fn unpack_regs18(version: Version, regs: &[u64; 18]) -> Result<Self, Error> {
1033 assert!(version >= Version(1, 2));
Balint Dobszay5bf492f2024-07-29 17:21:32 +02001034
Balint Dobszayde0dc802025-02-28 14:16:52 +01001035 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 Dobszay3aad9572025-01-17 16:54:11 +01001061 }
1062
Balint Dobszaya5846852025-02-26 15:38:53 +01001063 /// Create register contents for an interface.
Balint Dobszayde0dc802025-02-28 14:16:52 +01001064 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 Dobszay3aad9572025-01-17 16:54:11 +01001098 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 Dobszayde0dc802025-02-28 14:16:52 +01001130 _ => panic!("{:#x?} requires 18 registers", args),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001131 }
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ález17b92442025-03-10 16:45:04 +00001188 Interface::SecondaryEpRegister { entrypoint } => match entrypoint {
1189 SecondaryEpRegisterAddr::Addr32(addr) => a[1] = addr as u64,
1190 SecondaryEpRegisterAddr::Addr64(addr) => a[1] = addr,
1191 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001192 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 Dobszay3aad9572025-01-17 16:54:11 +01001202 args,
1203 } => {
Balint Dobszaye9a3e762025-02-26 17:29:57 +01001204 a[1] = ((src_id as u64) << 16) | dst_id as u64;
Balint Dobszay3aad9572025-01-17 16:54:11 +01001205 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ález4d5b0ba2025-03-03 17:15:55 +00001220 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 Dobszay3aad9572025-01-17 16:54:11 +01001263 }
1264 }
1265 Interface::MsgSendDirectResp {
1266 src_id,
1267 dst_id,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001268 args,
1269 } => {
Balint Dobszaye9a3e762025-02-26 17:29:57 +01001270 a[1] = ((src_id as u64) << 16) | dst_id as u64;
Balint Dobszay3aad9572025-01-17 16:54:11 +01001271 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ález4d5b0ba2025-03-03 17:15:55 +00001286 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 Dobszay3aad9572025-01-17 16:54:11 +01001306 }
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 Dobszayde0dc802025-02-28 14:16:52 +01001374 Interface::MemPermGet { addr, page_cnt } => {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001375 a[1] = match addr {
1376 MemAddr::Addr32(addr) => addr.into(),
1377 MemAddr::Addr64(addr) => addr,
1378 };
Balint Dobszayde0dc802025-02-28 14:16:52 +01001379 a[2] = if version >= Version(1, 3) {
1380 page_cnt.unwrap().into()
1381 } else {
1382 assert!(page_cnt.is_none());
1383 0
1384 }
Balint Dobszay3aad9572025-01-17 16:54:11 +01001385 }
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 Dobszayde0dc802025-02-28 14:16:52 +01001412 _ => panic!("{:#x?} requires 18 registers", char_lists),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001413 }
1414 }
Balint Dobszayde0dc802025-02-28 14:16:52 +01001415 _ => 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(&regs[..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(&regs[..16]),
1462 _ => panic!("{:#x?} requires 8 registers", char_lists),
1463 }
1464 }
1465 _ => panic!("{:#x?} requires 8 registers", self),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001466 }
1467 }
1468
Balint Dobszaya5846852025-02-26 15:38:53 +01001469 /// Helper function to create an `FFA_SUCCESS` interface without any arguments.
Balint Dobszay3aad9572025-01-17 16:54:11 +01001470 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ález4c8c7d22025-03-10 17:14:57 +00001477 /// 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 Dobszaya5846852025-02-26 15:38:53 +01001485 /// Helper function to create an `FFA_ERROR` interface with an error code.
Balint Dobszay3aad9572025-01-17 16:54:11 +01001486 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 Dobszaya5846852025-02-26 15:38:53 +01001497/// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG32` message.
1498pub const CONSOLE_LOG_32_MAX_CHAR_CNT: u8 = 24;
Balint Dobszayde0dc802025-02-28 14:16:52 +01001499/// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG64` message.
1500pub const CONSOLE_LOG_64_MAX_CHAR_CNT: u8 = 128;
Balint Dobszay3aad9572025-01-17 16:54:11 +01001501
Balint Dobszaya5846852025-02-26 15:38:53 +01001502/// Helper function to convert the "Tightly packed list of characters" format used by the
1503/// `FFA_CONSOLE_LOG` interface into a byte slice.
Balint Dobszay3aad9572025-01-17 16:54:11 +01001504pub 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 Dobszaya5846852025-02-26 15:38:53 +01001511 if !(1..=CONSOLE_LOG_32_MAX_CHAR_CNT).contains(&char_cnt) {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001512 return Err(FfaError::InvalidParameters);
1513 }
1514 for (i, reg) in regs.iter().enumerate() {
1515 log_bytes[4 * i..4 * (i + 1)].copy_from_slice(&reg.to_le_bytes());
1516 }
1517 }
1518 ConsoleLogChars::Reg64(regs) => {
Balint Dobszaya5846852025-02-26 15:38:53 +01001519 if !(1..=CONSOLE_LOG_64_MAX_CHAR_CNT).contains(&char_cnt) {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001520 return Err(FfaError::InvalidParameters);
1521 }
1522 for (i, reg) in regs.iter().enumerate() {
1523 log_bytes[8 * i..8 * (i + 1)].copy_from_slice(&reg.to_le_bytes());
1524 }
1525 }
Balint Dobszay5bf492f2024-07-29 17:21:32 +02001526 }
1527
Balint Dobszayc8802492025-01-15 18:11:39 +01001528 Ok(())
Balint Dobszay5bf492f2024-07-29 17:21:32 +02001529}