blob: f340ec26d2d07d4812ad4bd7d970782c33bb8a74 [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;
Imre Kis787c5002025-04-10 14:25:51 +020013use zerocopy::transmute;
Balint Dobszay5bf492f2024-07-29 17:21:32 +020014
15pub mod boot_info;
Balint Dobszayb2ff2bc2024-12-19 18:59:38 +010016mod ffa_v1_1;
Balint Dobszayde0dc802025-02-28 14:16:52 +010017mod ffa_v1_2;
Balint Dobszay5bf492f2024-07-29 17:21:32 +020018pub mod memory_management;
19pub mod partition_info;
20
Balint Dobszaya5846852025-02-26 15:38:53 +010021/// Constant for 4K page size. On many occasions the FF-A spec defines memory size as count of 4K
22/// pages, regardless of the current translation granule.
Balint Dobszay3aad9572025-01-17 16:54:11 +010023pub const FFA_PAGE_SIZE_4K: usize = 4096;
24
Balint Dobszaya5846852025-02-26 15:38:53 +010025/// Rich error types returned by this module. Should be converted to [`crate::FfaError`] when used
26/// with the `FFA_ERROR` interface.
Tomás González0a058bc2025-03-11 11:20:55 +000027#[derive(Debug, Error, PartialEq)]
Balint Dobszay3aad9572025-01-17 16:54:11 +010028pub enum Error {
29 #[error("Unrecognised FF-A function ID {0}")]
30 UnrecognisedFunctionId(u32),
31 #[error("Unrecognised FF-A feature ID {0}")]
32 UnrecognisedFeatureId(u8),
33 #[error("Unrecognised FF-A error code {0}")]
34 UnrecognisedErrorCode(i32),
Tomás González4d5b0ba2025-03-03 17:15:55 +000035 #[error("Unrecognised FF-A Framework Message {0}")]
36 UnrecognisedFwkMsg(u32),
Tomás González092202a2025-03-05 11:56:45 +000037 #[error("Invalid FF-A Msg Wait Flag {0}")]
38 InvalidMsgWaitFlag(u32),
Tomás González4d5b0ba2025-03-03 17:15:55 +000039 #[error("Unrecognised VM availability status {0}")]
40 UnrecognisedVmAvailabilityStatus(i32),
41 #[error("Unrecognised FF-A Warm Boot Type {0}")]
42 UnrecognisedWarmBootType(u32),
43 #[error("Invalid version {0}")]
44 InvalidVersion(u32),
Tomás González0a058bc2025-03-11 11:20:55 +000045 #[error("Invalid Information Tag {0}")]
46 InvalidInformationTag(u16),
Tomás González7ffb6132025-04-03 12:28:58 +010047 #[error("Invalid Flag for Notification Set")]
48 InvalidNotificationSetFlag(u32),
49 #[error("Invalid Vm ID")]
50 InvalidVmId(u32),
Imre Kise295adb2025-04-10 13:26:28 +020051 #[error("Invalid FF-A Partition Info Get Flag {0}")]
52 InvalidPartitionInfoGetFlag(u32),
Imre Kis839eaef2025-04-11 17:38:36 +020053 #[error("Invalid success argument variant")]
54 InvalidSuccessArgsVariant,
Imre Kis787c5002025-04-10 14:25:51 +020055 #[error("Invalid notification count")]
56 InvalidNotificationCount,
Imre Kis92b663e2025-04-10 14:15:05 +020057 #[error("Invalid Partition Info Get Regs response")]
58 InvalidPartitionInfoGetRegsResponse,
Balint Dobszay3aad9572025-01-17 16:54:11 +010059}
60
61impl From<Error> for FfaError {
62 fn from(value: Error) -> Self {
63 match value {
64 Error::UnrecognisedFunctionId(_) | Error::UnrecognisedFeatureId(_) => {
65 Self::NotSupported
66 }
Tomás González0a058bc2025-03-11 11:20:55 +000067 Error::InvalidInformationTag(_) => Self::Retry,
Tomás González4d5b0ba2025-03-03 17:15:55 +000068 Error::UnrecognisedErrorCode(_)
69 | Error::UnrecognisedFwkMsg(_)
70 | Error::InvalidVersion(_)
Tomás González092202a2025-03-05 11:56:45 +000071 | Error::InvalidMsgWaitFlag(_)
Tomás González4d5b0ba2025-03-03 17:15:55 +000072 | Error::UnrecognisedVmAvailabilityStatus(_)
Tomás González7ffb6132025-04-03 12:28:58 +010073 | Error::InvalidNotificationSetFlag(_)
74 | Error::InvalidVmId(_)
Imre Kise295adb2025-04-10 13:26:28 +020075 | Error::UnrecognisedWarmBootType(_)
Imre Kis839eaef2025-04-11 17:38:36 +020076 | Error::InvalidPartitionInfoGetFlag(_)
Imre Kis787c5002025-04-10 14:25:51 +020077 | Error::InvalidSuccessArgsVariant
Imre Kis92b663e2025-04-10 14:15:05 +020078 | Error::InvalidNotificationCount
79 | Error::InvalidPartitionInfoGetRegsResponse => Self::InvalidParameters,
Balint Dobszay3aad9572025-01-17 16:54:11 +010080 }
81 }
82}
Balint Dobszay5bf492f2024-07-29 17:21:32 +020083
Balint Dobszaya5846852025-02-26 15:38:53 +010084/// 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 +020085#[derive(PartialEq, Clone, Copy)]
86pub enum Instance {
Balint Dobszaya5846852025-02-26 15:38:53 +010087 /// The instance between the SPMC and SPMD.
Balint Dobszay5bf492f2024-07-29 17:21:32 +020088 SecurePhysical,
Balint Dobszaya5846852025-02-26 15:38:53 +010089 /// The instance between the SPMC and a physical SP (contains the SP's endpoint ID).
Balint Dobszay5bf492f2024-07-29 17:21:32 +020090 SecureVirtual(u16),
91}
92
Balint Dobszaya5846852025-02-26 15:38:53 +010093/// Function IDs of the various FF-A interfaces.
Andrew Walbran969b9252024-11-25 15:35:42 +000094#[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
Balint Dobszay3aad9572025-01-17 16:54:11 +010095#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFunctionId))]
Balint Dobszay5bf492f2024-07-29 17:21:32 +020096#[repr(u32)]
97pub enum FuncId {
98 Error = 0x84000060,
99 Success32 = 0x84000061,
100 Success64 = 0xc4000061,
101 Interrupt = 0x84000062,
102 Version = 0x84000063,
103 Features = 0x84000064,
104 RxAcquire = 0x84000084,
105 RxRelease = 0x84000065,
106 RxTxMap32 = 0x84000066,
107 RxTxMap64 = 0xc4000066,
108 RxTxUnmap = 0x84000067,
109 PartitionInfoGet = 0x84000068,
Balint Dobszaye6aa4862025-02-28 16:37:56 +0100110 PartitionInfoGetRegs = 0xc400008b,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200111 IdGet = 0x84000069,
112 SpmIdGet = 0x84000085,
Balint Dobszaye6aa4862025-02-28 16:37:56 +0100113 ConsoleLog32 = 0x8400008a,
114 ConsoleLog64 = 0xc400008a,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200115 MsgWait = 0x8400006b,
116 Yield = 0x8400006c,
117 Run = 0x8400006d,
118 NormalWorldResume = 0x8400007c,
119 MsgSend2 = 0x84000086,
120 MsgSendDirectReq32 = 0x8400006f,
121 MsgSendDirectReq64 = 0xc400006f,
Balint Dobszayde0dc802025-02-28 14:16:52 +0100122 MsgSendDirectReq64_2 = 0xc400008d,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200123 MsgSendDirectResp32 = 0x84000070,
124 MsgSendDirectResp64 = 0xc4000070,
Balint Dobszayde0dc802025-02-28 14:16:52 +0100125 MsgSendDirectResp64_2 = 0xc400008e,
Balint Dobszaye6aa4862025-02-28 16:37:56 +0100126 NotificationBitmapCreate = 0x8400007d,
127 NotificationBitmapDestroy = 0x8400007e,
128 NotificationBind = 0x8400007f,
129 NotificationUnbind = 0x84000080,
130 NotificationSet = 0x84000081,
131 NotificationGet = 0x84000082,
132 NotificationInfoGet32 = 0x84000083,
133 NotificationInfoGet64 = 0xc4000083,
134 El3IntrHandle = 0x8400008c,
Tomás González17b92442025-03-10 16:45:04 +0000135 SecondaryEpRegister32 = 0x84000087,
136 SecondaryEpRegister64 = 0xc4000087,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200137 MemDonate32 = 0x84000071,
138 MemDonate64 = 0xc4000071,
139 MemLend32 = 0x84000072,
140 MemLend64 = 0xc4000072,
141 MemShare32 = 0x84000073,
142 MemShare64 = 0xc4000073,
143 MemRetrieveReq32 = 0x84000074,
144 MemRetrieveReq64 = 0xc4000074,
145 MemRetrieveResp = 0x84000075,
146 MemRelinquish = 0x84000076,
147 MemReclaim = 0x84000077,
148 MemPermGet32 = 0x84000088,
149 MemPermGet64 = 0xc4000088,
150 MemPermSet32 = 0x84000089,
151 MemPermSet64 = 0xc4000089,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200152}
153
Balint Dobszayde0dc802025-02-28 14:16:52 +0100154impl FuncId {
155 /// Returns true if this is a 32-bit call, or false if it is a 64-bit call.
156 pub fn is_32bit(&self) -> bool {
Tomás González6ccba0a2025-04-09 13:31:29 +0100157 u32::from(*self) & (1 << 30) == 0
Balint Dobszayde0dc802025-02-28 14:16:52 +0100158 }
159}
160
Balint Dobszaya5846852025-02-26 15:38:53 +0100161/// Error status codes used by the `FFA_ERROR` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100162#[derive(Clone, Copy, Debug, Eq, Error, IntoPrimitive, PartialEq, TryFromPrimitive)]
163#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedErrorCode))]
164#[repr(i32)]
165pub enum FfaError {
166 #[error("Not supported")]
167 NotSupported = -1,
168 #[error("Invalid parameters")]
169 InvalidParameters = -2,
170 #[error("No memory")]
171 NoMemory = -3,
172 #[error("Busy")]
173 Busy = -4,
174 #[error("Interrupted")]
175 Interrupted = -5,
176 #[error("Denied")]
177 Denied = -6,
178 #[error("Retry")]
179 Retry = -7,
180 #[error("Aborted")]
181 Aborted = -8,
182 #[error("No data")]
183 NoData = -9,
184}
185
Balint Dobszaya5846852025-02-26 15:38:53 +0100186/// Endpoint ID and vCPU ID pair, used by `FFA_ERROR`, `FFA_INTERRUPT` and `FFA_RUN` interfaces.
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200187#[derive(Debug, Eq, PartialEq, Clone, Copy)]
Balint Dobszay3aad9572025-01-17 16:54:11 +0100188pub struct TargetInfo {
189 pub endpoint_id: u16,
190 pub vcpu_id: u16,
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200191}
192
Balint Dobszay3aad9572025-01-17 16:54:11 +0100193impl From<u32> for TargetInfo {
194 fn from(value: u32) -> Self {
195 Self {
196 endpoint_id: (value >> 16) as u16,
197 vcpu_id: value as u16,
198 }
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200199 }
200}
201
Balint Dobszay3aad9572025-01-17 16:54:11 +0100202impl From<TargetInfo> for u32 {
203 fn from(value: TargetInfo) -> Self {
Balint Dobszaye9a3e762025-02-26 17:29:57 +0100204 ((value.endpoint_id as u32) << 16) | value.vcpu_id as u32
Andrew Walbran0d315812024-11-25 15:36:36 +0000205 }
Balint Dobszay3aad9572025-01-17 16:54:11 +0100206}
Andrew Walbran0d315812024-11-25 15:36:36 +0000207
Imre Kis839eaef2025-04-11 17:38:36 +0200208/// Generic arguments of the `FFA_SUCCESS` interface. The interpretation of the arguments depends on
209/// the interface that initiated the request. The application code has knowledge of the request, so
210/// it has to convert `SuccessArgs` into/from a specific success args structure that matches the
211/// request.
Imre Kis4e9d8bc2025-04-10 13:48:26 +0200212///
213/// The current specialized success arguments types are:
214/// * `FFA_FEATURES` - [`SuccessArgsFeatures`]
Imre Kisbbef2872025-04-10 14:11:29 +0200215/// * `FFA_ID_GET` - [`SuccessArgsIdGet`]
216/// * `FFA_SPM_ID_GET` - [`SuccessArgsSpmIdGet`]
Imre Kis61c34092025-04-10 14:14:38 +0200217/// * `FFA_PARTITION_INFO_GET` - [`partition_info::SuccessArgsPartitionInfoGet`]
Imre Kis92b663e2025-04-10 14:15:05 +0200218/// * `FFA_PARTITION_INFO_GET_REGS` - [`partition_info::SuccessArgsPartitionInfoGetRegs`]
Imre Kis9959e062025-04-10 14:16:10 +0200219/// * `FFA_NOTIFICATION_GET` - [`SuccessArgsNotificationGet`]
Imre Kis787c5002025-04-10 14:25:51 +0200220/// * `FFA_NOTIFICATION_INFO_GET_32` - [`SuccessArgsNotificationInfoGet32`]
221/// * `FFA_NOTIFICATION_INFO_GET_64` - [`SuccessArgsNotificationInfoGet64`]
Balint Dobszay3aad9572025-01-17 16:54:11 +0100222#[derive(Debug, Eq, PartialEq, Clone, Copy)]
223pub enum SuccessArgs {
Imre Kis54773b62025-04-10 13:47:39 +0200224 Args32([u32; 6]),
225 Args64([u64; 6]),
226 Args64_2([u64; 16]),
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200227}
228
Imre Kis839eaef2025-04-11 17:38:36 +0200229impl SuccessArgs {
230 fn try_get_args32(self) -> Result<[u32; 6], Error> {
231 match self {
232 SuccessArgs::Args32(args) => Ok(args),
233 SuccessArgs::Args64(_) | SuccessArgs::Args64_2(_) => {
234 Err(Error::InvalidSuccessArgsVariant)
235 }
236 }
237 }
238
239 fn try_get_args64(self) -> Result<[u64; 6], Error> {
240 match self {
241 SuccessArgs::Args64(args) => Ok(args),
242 SuccessArgs::Args32(_) | SuccessArgs::Args64_2(_) => {
243 Err(Error::InvalidSuccessArgsVariant)
244 }
245 }
246 }
247
248 fn try_get_args64_2(self) -> Result<[u64; 16], Error> {
249 match self {
250 SuccessArgs::Args64_2(args) => Ok(args),
251 SuccessArgs::Args32(_) | SuccessArgs::Args64(_) => {
252 Err(Error::InvalidSuccessArgsVariant)
253 }
254 }
255 }
256}
257
Tomás González17b92442025-03-10 16:45:04 +0000258/// Entrypoint address argument for `FFA_SECONDARY_EP_REGISTER` interface.
259#[derive(Debug, Eq, PartialEq, Clone, Copy)]
260pub enum SecondaryEpRegisterAddr {
261 Addr32(u32),
262 Addr64(u64),
263}
264
Balint Dobszaya5846852025-02-26 15:38:53 +0100265/// Version number of the FF-A implementation, `.0` is the major, `.1` is minor the version.
Balint Dobszayde0dc802025-02-28 14:16:52 +0100266#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200267pub struct Version(pub u16, pub u16);
268
Tomás González1f794352025-03-03 16:47:06 +0000269impl Version {
Tomás González83146af2025-03-04 11:32:41 +0000270 // The FF-A spec mandates that bit[31] of a version number must be 0
271 const MBZ_BITS: u32 = 1 << 31;
272
Tomás González1f794352025-03-03 16:47:06 +0000273 /// Returns whether the caller's version (self) is compatible with the callee's version (input
274 /// parameter)
275 pub fn is_compatible_to(&self, callee_version: &Version) -> bool {
276 self.0 == callee_version.0 && self.1 <= callee_version.1
277 }
278}
279
Tomás González83146af2025-03-04 11:32:41 +0000280impl TryFrom<u32> for Version {
281 type Error = Error;
282
283 fn try_from(val: u32) -> Result<Self, Self::Error> {
284 if (val & Self::MBZ_BITS) != 0 {
285 Err(Error::InvalidVersion(val))
286 } else {
287 Ok(Self((val >> 16) as u16, val as u16))
288 }
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200289 }
290}
291
292impl From<Version> for u32 {
293 fn from(v: Version) -> Self {
Tomás González83146af2025-03-04 11:32:41 +0000294 let v_u32 = ((v.0 as u32) << 16) | v.1 as u32;
295 assert!(v_u32 & Version::MBZ_BITS == 0);
296 v_u32
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200297 }
298}
299
Andrew Walbran19970ba2024-11-25 15:35:00 +0000300impl Display for Version {
301 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
302 write!(f, "{}.{}", self.0, self.1)
303 }
304}
305
306impl Debug for Version {
307 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
308 Display::fmt(self, f)
309 }
310}
311
Balint Dobszaya5846852025-02-26 15:38:53 +0100312/// Feature IDs used by the `FFA_FEATURES` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100313#[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
314#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedFeatureId))]
315#[repr(u8)]
316pub enum FeatureId {
317 NotificationPendingInterrupt = 0x1,
318 ScheduleReceiverInterrupt = 0x2,
319 ManagedExitInterrupt = 0x3,
320}
Balint Dobszayc8802492025-01-15 18:11:39 +0100321
Balint Dobszaya5846852025-02-26 15:38:53 +0100322/// Arguments for the `FFA_FEATURES` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100323#[derive(Debug, Eq, PartialEq, Clone, Copy)]
324pub enum Feature {
325 FuncId(FuncId),
326 FeatureId(FeatureId),
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100327 Unknown(u32),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100328}
Balint Dobszay5bf492f2024-07-29 17:21:32 +0200329
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100330impl From<u32> for Feature {
331 fn from(value: u32) -> Self {
332 // Bit[31] is set for all valid FF-A function IDs so we don't have to check it separately
333 if let Ok(func_id) = value.try_into() {
334 Self::FuncId(func_id)
335 } else if let Ok(feat_id) = (value as u8).try_into() {
336 Self::FeatureId(feat_id)
Balint Dobszay3aad9572025-01-17 16:54:11 +0100337 } else {
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100338 Self::Unknown(value)
339 }
Balint Dobszay3aad9572025-01-17 16:54:11 +0100340 }
341}
342
343impl From<Feature> for u32 {
344 fn from(value: Feature) -> Self {
345 match value {
346 Feature::FuncId(func_id) => (1 << 31) | func_id as u32,
347 Feature::FeatureId(feature_id) => feature_id as u32,
Balint Dobszayc31e0b92025-03-03 20:16:56 +0100348 Feature::Unknown(id) => panic!("Unknown feature or function ID {:#x?}", id),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100349 }
350 }
351}
352
Imre Kis4e9d8bc2025-04-10 13:48:26 +0200353/// `FFA_FEATURES` specific success argument structure. This type needs further specialization based
354/// on 'FF-A function ID or Feature ID' field of the preceeding `FFA_FEATURES` request.
355#[derive(Debug, Eq, PartialEq, Clone, Copy)]
356pub struct SuccessArgsFeatures {
357 pub properties: [u32; 2],
358}
359
360impl From<SuccessArgsFeatures> for SuccessArgs {
361 fn from(value: SuccessArgsFeatures) -> Self {
362 Self::Args32([value.properties[0], value.properties[1], 0, 0, 0, 0])
363 }
364}
365
366impl TryFrom<SuccessArgs> for SuccessArgsFeatures {
367 type Error = Error;
368
369 fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> {
370 let args = value.try_get_args32()?;
371
372 Ok(Self {
373 properties: [args[0], args[1]],
374 })
375 }
376}
377
Balint Dobszaya5846852025-02-26 15:38:53 +0100378/// RXTX buffer descriptor, used by `FFA_RXTX_MAP`.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100379#[derive(Debug, Eq, PartialEq, Clone, Copy)]
380pub enum RxTxAddr {
381 Addr32 { rx: u32, tx: u32 },
382 Addr64 { rx: u64, tx: u64 },
383}
384
Imre Kisbbef2872025-04-10 14:11:29 +0200385/// `FFA_ID_GET` specific success argument structure.
386#[derive(Debug, Eq, PartialEq, Clone, Copy)]
387pub struct SuccessArgsIdGet {
388 pub id: u16,
389}
390
391impl From<SuccessArgsIdGet> for SuccessArgs {
392 fn from(value: SuccessArgsIdGet) -> Self {
393 SuccessArgs::Args32([value.id as u32, 0, 0, 0, 0, 0])
394 }
395}
396
397impl TryFrom<SuccessArgs> for SuccessArgsIdGet {
398 type Error = Error;
399
400 fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> {
401 let args = value.try_get_args32()?;
402 Ok(Self { id: args[0] as u16 })
403 }
404}
405
406/// `FFA_SPM_ID_GET` specific success argument structure.
407#[derive(Debug, Eq, PartialEq, Clone, Copy)]
408pub struct SuccessArgsSpmIdGet {
409 pub id: u16,
410}
411
412impl From<SuccessArgsSpmIdGet> for SuccessArgs {
413 fn from(value: SuccessArgsSpmIdGet) -> Self {
414 SuccessArgs::Args32([value.id as u32, 0, 0, 0, 0, 0])
415 }
416}
417
418impl TryFrom<SuccessArgs> for SuccessArgsSpmIdGet {
419 type Error = Error;
420
421 fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> {
422 let args = value.try_get_args32()?;
423 Ok(Self { id: args[0] as u16 })
424 }
425}
426
Imre Kise295adb2025-04-10 13:26:28 +0200427/// Flags of the `FFA_PARTITION_INFO_GET` interface.
428#[derive(Debug, Eq, PartialEq, Clone, Copy)]
429pub struct PartitionInfoGetFlags {
430 pub count_only: bool,
431}
432
433impl PartitionInfoGetFlags {
434 const RETURN_INFORMATION_TYPE_FLAG: u32 = 1 << 0;
435 const MBZ_BITS: u32 = 0xffff_fffe;
436}
437
438impl TryFrom<u32> for PartitionInfoGetFlags {
439 type Error = Error;
440
441 fn try_from(val: u32) -> Result<Self, Self::Error> {
442 if (val & Self::MBZ_BITS) != 0 {
443 Err(Error::InvalidPartitionInfoGetFlag(val))
444 } else {
445 Ok(Self {
446 count_only: val & Self::RETURN_INFORMATION_TYPE_FLAG != 0,
447 })
448 }
449 }
450}
451
452impl From<PartitionInfoGetFlags> for u32 {
453 fn from(flags: PartitionInfoGetFlags) -> Self {
454 let mut bits: u32 = 0;
455 if flags.count_only {
456 bits |= PartitionInfoGetFlags::RETURN_INFORMATION_TYPE_FLAG;
457 }
458 bits
459 }
460}
461
Tomás González4d5b0ba2025-03-03 17:15:55 +0000462/// Composite type for capturing success and error return codes for the VM availability messages.
463///
464/// Error codes are handled by the `FfaError` type. Having a separate type for errors helps using
465/// `Result<(), FfaError>`. If a single type would include both success and error values,
466/// then `Err(FfaError::Success)` would be incomprehensible.
467#[derive(Debug, Eq, PartialEq, Clone, Copy)]
468pub enum VmAvailabilityStatus {
469 Success,
470 Error(FfaError),
471}
472
473impl TryFrom<i32> for VmAvailabilityStatus {
474 type Error = Error;
475 fn try_from(value: i32) -> Result<Self, <Self as TryFrom<i32>>::Error> {
476 Ok(match value {
477 0 => Self::Success,
478 error_code => Self::Error(FfaError::try_from(error_code)?),
479 })
480 }
481}
482
483impl From<VmAvailabilityStatus> for i32 {
484 fn from(value: VmAvailabilityStatus) -> Self {
485 match value {
486 VmAvailabilityStatus::Success => 0,
487 VmAvailabilityStatus::Error(error_code) => error_code.into(),
488 }
489 }
490}
491
492/// Arguments for the Power Warm Boot `FFA_MSG_SEND_DIRECT_REQ` interface.
493#[derive(Clone, Copy, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
494#[num_enum(error_type(name = Error, constructor = Error::UnrecognisedWarmBootType))]
495#[repr(u32)]
496pub enum WarmBootType {
497 ExitFromSuspend = 0,
498 ExitFromLowPower = 1,
499}
500
Balint Dobszaya5846852025-02-26 15:38:53 +0100501/// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100502#[derive(Debug, Eq, PartialEq, Clone, Copy)]
503pub enum DirectMsgArgs {
504 Args32([u32; 5]),
505 Args64([u64; 5]),
Tomás González4d5b0ba2025-03-03 17:15:55 +0000506 /// Message for forwarding FFA_VERSION call from Normal world to the SPMC
507 VersionReq {
508 version: Version,
509 },
510 /// Response message to forwarded FFA_VERSION call from the Normal world
511 /// Contains the version returned by the SPMC or None
512 VersionResp {
513 version: Option<Version>,
514 },
515 /// Message for a power management operation initiated by a PSCI function
516 PowerPsciReq32 {
Tomás González4d5b0ba2025-03-03 17:15:55 +0000517 // params[i]: Input parameter in w[i] in PSCI function invocation at EL3.
Tomás González67f92c72025-03-20 16:50:42 +0000518 // params[0]: Function ID.
519 params: [u32; 4],
Tomás González4d5b0ba2025-03-03 17:15:55 +0000520 },
521 /// Message for a power management operation initiated by a PSCI function
522 PowerPsciReq64 {
Tomás González4d5b0ba2025-03-03 17:15:55 +0000523 // params[i]: Input parameter in x[i] in PSCI function invocation at EL3.
Tomás González67f92c72025-03-20 16:50:42 +0000524 // params[0]: Function ID.
525 params: [u64; 4],
Tomás González4d5b0ba2025-03-03 17:15:55 +0000526 },
527 /// Message for a warm boot
528 PowerWarmBootReq {
529 boot_type: WarmBootType,
530 },
531 /// Response message to indicate return status of the last power management request message
532 /// Return error code SUCCESS or DENIED as defined in PSCI spec. Caller is left to do the
533 /// parsing of the return status.
534 PowerPsciResp {
Tomás González4d5b0ba2025-03-03 17:15:55 +0000535 psci_status: i32,
536 },
537 /// Message to signal creation of a VM
538 VmCreated {
539 // Globally unique Handle to identify a memory region that contains IMPLEMENTATION DEFINED
540 // information associated with the created VM.
541 // The invalid memory region handle must be specified by the Hypervisor if this field is not
542 // used.
543 handle: memory_management::Handle,
544 vm_id: u16,
545 },
546 /// Message to acknowledge creation of a VM
547 VmCreatedAck {
548 sp_status: VmAvailabilityStatus,
549 },
550 /// Message to signal destruction of a VM
551 VmDestructed {
552 // Globally unique Handle to identify a memory region that contains IMPLEMENTATION DEFINED
553 // information associated with the created VM.
554 // The invalid memory region handle must be specified by the Hypervisor if this field is not
555 // used.
556 handle: memory_management::Handle,
557 vm_id: u16,
558 },
559 /// Message to acknowledge destruction of a VM
560 VmDestructedAck {
561 sp_status: VmAvailabilityStatus,
562 },
563}
564
565impl DirectMsgArgs {
566 // Flags for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}` interfaces.
567
568 const FWK_MSG_BITS: u32 = 1 << 31;
569 const VERSION_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b1000;
570 const VERSION_RESP: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b1001;
571 const POWER_PSCI_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS;
572 const POWER_WARM_BOOT_REQ: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0001;
573 const POWER_PSCI_RESP: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0010;
574 const VM_CREATED: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0100;
575 const VM_CREATED_ACK: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0101;
576 const VM_DESTRUCTED: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0110;
577 const VM_DESTRUCTED_ACK: u32 = DirectMsgArgs::FWK_MSG_BITS | 0b0111;
Balint Dobszay3aad9572025-01-17 16:54:11 +0100578}
579
Balint Dobszayde0dc802025-02-28 14:16:52 +0100580/// Arguments for the `FFA_MSG_SEND_DIRECT_{REQ,RESP}2` interfaces.
581#[derive(Debug, Eq, PartialEq, Clone, Copy)]
582pub struct DirectMsg2Args([u64; 14]);
583
Tomás González092202a2025-03-05 11:56:45 +0000584#[derive(Debug, Eq, PartialEq, Clone, Copy)]
585pub struct MsgWaitFlags {
586 retain_rx_buffer: bool,
587}
588
589impl MsgWaitFlags {
590 const RETAIN_RX_BUFFER: u32 = 0x01;
591 const MBZ_BITS: u32 = 0xfffe;
592}
593
594impl TryFrom<u32> for MsgWaitFlags {
595 type Error = Error;
596
597 fn try_from(val: u32) -> Result<Self, Self::Error> {
598 if (val & Self::MBZ_BITS) != 0 {
599 Err(Error::InvalidMsgWaitFlag(val))
600 } else {
601 Ok(MsgWaitFlags {
602 retain_rx_buffer: val & Self::RETAIN_RX_BUFFER != 0,
603 })
604 }
605 }
606}
607
608impl From<MsgWaitFlags> for u32 {
609 fn from(flags: MsgWaitFlags) -> Self {
610 let mut bits: u32 = 0;
611 if flags.retain_rx_buffer {
612 bits |= MsgWaitFlags::RETAIN_RX_BUFFER;
613 }
614 bits
615 }
616}
617
Balint Dobszaya5846852025-02-26 15:38:53 +0100618/// Descriptor for a dynamically allocated memory buffer that contains the memory transaction
Tomás Gonzálezf268e322025-03-05 11:18:11 +0000619/// descriptor.
620///
621/// Used by `FFA_MEM_{DONATE,LEND,SHARE,RETRIEVE_REQ}` interfaces, only when the TX buffer is not
622/// used to transmit the transaction descriptor.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100623#[derive(Debug, Eq, PartialEq, Clone, Copy)]
624pub enum MemOpBuf {
625 Buf32 { addr: u32, page_cnt: u32 },
626 Buf64 { addr: u64, page_cnt: u32 },
627}
628
Balint Dobszaya5846852025-02-26 15:38:53 +0100629/// Memory address argument for `FFA_MEM_PERM_{GET,SET}` interfaces.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100630#[derive(Debug, Eq, PartialEq, Clone, Copy)]
631pub enum MemAddr {
632 Addr32(u32),
633 Addr64(u64),
634}
635
Balint Dobszayde0dc802025-02-28 14:16:52 +0100636/// Argument for the `FFA_CONSOLE_LOG` interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +0100637#[derive(Debug, Eq, PartialEq, Clone, Copy)]
638pub enum ConsoleLogChars {
639 Reg32([u32; 6]),
Balint Dobszayde0dc802025-02-28 14:16:52 +0100640 Reg64([u64; 16]),
Balint Dobszay3aad9572025-01-17 16:54:11 +0100641}
642
Tomás González7ffb6132025-04-03 12:28:58 +0100643#[derive(Debug, Eq, PartialEq, Clone, Copy)]
644pub struct NotificationBindFlags {
645 per_vcpu_notification: bool,
646}
647
648impl NotificationBindFlags {
649 const PER_VCPU_NOTIFICATION: u32 = 1;
650}
651
652impl From<NotificationBindFlags> for u32 {
653 fn from(flags: NotificationBindFlags) -> Self {
654 let mut bits: u32 = 0;
655 if flags.per_vcpu_notification {
656 bits |= NotificationBindFlags::PER_VCPU_NOTIFICATION;
657 }
658 bits
659 }
660}
661
662impl From<u32> for NotificationBindFlags {
663 fn from(flags: u32) -> Self {
664 Self {
665 per_vcpu_notification: flags & Self::PER_VCPU_NOTIFICATION != 0,
666 }
667 }
668}
669
670#[derive(Debug, Eq, PartialEq, Clone, Copy)]
671pub struct NotificationSetFlags {
672 delay_schedule_receiver: bool,
673 vcpu_id: Option<u16>,
674}
675
676impl NotificationSetFlags {
677 const PER_VCP_NOTIFICATION: u32 = 1 << 0;
678 const DELAY_SCHEDULE_RECEIVER: u32 = 1 << 1;
679 const VCPU_ID_SHIFT: u32 = 16;
680
681 const MBZ_BITS: u32 = 0xfffc;
682}
683
684impl From<NotificationSetFlags> for u32 {
685 fn from(flags: NotificationSetFlags) -> Self {
686 let mut bits: u32 = 0;
687
688 if flags.delay_schedule_receiver {
689 bits |= NotificationSetFlags::DELAY_SCHEDULE_RECEIVER;
690 }
691 if let Some(vcpu_id) = flags.vcpu_id {
692 bits |= NotificationSetFlags::PER_VCP_NOTIFICATION;
693 bits |= u32::from(vcpu_id) << NotificationSetFlags::VCPU_ID_SHIFT;
694 }
695
696 bits
697 }
698}
699
700impl TryFrom<u32> for NotificationSetFlags {
701 type Error = Error;
702
703 fn try_from(flags: u32) -> Result<Self, Self::Error> {
704 if (flags & Self::MBZ_BITS) != 0 {
705 return Err(Error::InvalidNotificationSetFlag(flags));
706 }
707
708 let tentative_vcpu_id = (flags >> Self::VCPU_ID_SHIFT) as u16;
709
710 let vcpu_id = if (flags & Self::PER_VCP_NOTIFICATION) != 0 {
711 Some(tentative_vcpu_id)
712 } else {
713 if tentative_vcpu_id != 0 {
714 return Err(Error::InvalidNotificationSetFlag(flags));
715 }
716 None
717 };
718
719 Ok(Self {
720 delay_schedule_receiver: (flags & Self::DELAY_SCHEDULE_RECEIVER) != 0,
721 vcpu_id,
722 })
723 }
724}
725
726#[derive(Debug, Eq, PartialEq, Clone, Copy)]
727pub struct NotificationGetFlags {
728 sp_bitmap_id: bool,
729 vm_bitmap_id: bool,
730 spm_bitmap_id: bool,
731 hyp_bitmap_id: bool,
732}
733
734impl NotificationGetFlags {
735 const SP_BITMAP_ID: u32 = 1;
736 const VM_BITMAP_ID: u32 = 1 << 1;
737 const SPM_BITMAP_ID: u32 = 1 << 2;
738 const HYP_BITMAP_ID: u32 = 1 << 3;
739}
740
741impl From<NotificationGetFlags> for u32 {
742 fn from(flags: NotificationGetFlags) -> Self {
743 let mut bits: u32 = 0;
744 if flags.sp_bitmap_id {
745 bits |= NotificationGetFlags::SP_BITMAP_ID;
746 }
747 if flags.vm_bitmap_id {
748 bits |= NotificationGetFlags::VM_BITMAP_ID;
749 }
750 if flags.spm_bitmap_id {
751 bits |= NotificationGetFlags::SPM_BITMAP_ID;
752 }
753 if flags.hyp_bitmap_id {
754 bits |= NotificationGetFlags::HYP_BITMAP_ID;
755 }
756 bits
757 }
758}
759
760impl From<u32> for NotificationGetFlags {
761 // This is a "from" instead of a "try_from" because Reserved Bits are SBZ, *not* MBZ.
762 fn from(flags: u32) -> Self {
763 Self {
764 sp_bitmap_id: (flags & Self::SP_BITMAP_ID) != 0,
765 vm_bitmap_id: (flags & Self::VM_BITMAP_ID) != 0,
766 spm_bitmap_id: (flags & Self::SPM_BITMAP_ID) != 0,
767 hyp_bitmap_id: (flags & Self::HYP_BITMAP_ID) != 0,
768 }
769 }
770}
771
Imre Kis9959e062025-04-10 14:16:10 +0200772/// `FFA_NOTIFICATION_GET` specific success argument structure.
773#[derive(Debug, Eq, PartialEq, Clone, Copy)]
774pub struct SuccessArgsNotificationGet {
775 pub sp_notifications: Option<u64>,
776 pub vm_notifications: Option<u64>,
777 pub spm_notifications: Option<u32>,
778 pub hypervisor_notifications: Option<u32>,
779}
780
781impl From<SuccessArgsNotificationGet> for SuccessArgs {
782 fn from(value: SuccessArgsNotificationGet) -> Self {
783 let mut args = [0; 6];
784
785 if let Some(bitmap) = value.sp_notifications {
786 args[0] = bitmap as u32;
787 args[1] = (bitmap >> 32) as u32;
788 }
789
790 if let Some(bitmap) = value.vm_notifications {
791 args[2] = bitmap as u32;
792 args[3] = (bitmap >> 32) as u32;
793 }
794
795 if let Some(bitmap) = value.spm_notifications {
796 args[4] = bitmap;
797 }
798
799 if let Some(bitmap) = value.hypervisor_notifications {
800 args[5] = bitmap;
801 }
802
803 Self::Args32(args)
804 }
805}
806
807impl TryFrom<(NotificationGetFlags, SuccessArgs)> for SuccessArgsNotificationGet {
808 type Error = Error;
809
810 fn try_from(value: (NotificationGetFlags, SuccessArgs)) -> Result<Self, Self::Error> {
811 let (flags, value) = value;
812 let args = value.try_get_args32()?;
813
814 let sp_notifications = if flags.sp_bitmap_id {
815 Some(u64::from(args[0]) | (u64::from(args[1]) << 32))
816 } else {
817 None
818 };
819
820 let vm_notifications = if flags.vm_bitmap_id {
821 Some(u64::from(args[2]) | (u64::from(args[3]) << 32))
822 } else {
823 None
824 };
825
826 let spm_notifications = if flags.spm_bitmap_id {
827 Some(args[4])
828 } else {
829 None
830 };
831
832 let hypervisor_notifications = if flags.hyp_bitmap_id {
833 Some(args[5])
834 } else {
835 None
836 };
837
838 Ok(Self {
839 sp_notifications,
840 vm_notifications,
841 spm_notifications,
842 hypervisor_notifications,
843 })
844 }
845}
Imre Kis787c5002025-04-10 14:25:51 +0200846
847/// `FFA_NOTIFICATION_INFO_GET` specific success argument structure. The `MAX_COUNT` parameter
848/// depends on the 32-bit or 64-bit packing.
849#[derive(Debug, Eq, PartialEq, Clone, Copy)]
850pub struct SuccessArgsNotificationInfoGet<const MAX_COUNT: usize> {
851 pub more_pending_notifications: bool,
852 list_count: usize,
853 id_counts: [u8; MAX_COUNT],
854 ids: [u16; MAX_COUNT],
855}
856
857impl<const MAX_COUNT: usize> Default for SuccessArgsNotificationInfoGet<MAX_COUNT> {
858 fn default() -> Self {
859 Self {
860 more_pending_notifications: false,
861 list_count: 0,
862 id_counts: [0; MAX_COUNT],
863 ids: [0; MAX_COUNT],
864 }
865 }
866}
867
868impl<const MAX_COUNT: usize> SuccessArgsNotificationInfoGet<MAX_COUNT> {
869 const MORE_PENDING_NOTIFICATIONS_FLAG: u64 = 1 << 0;
870 const LIST_COUNT_SHIFT: usize = 7;
871 const LIST_COUNT_MASK: u64 = 0x1f;
872 const ID_COUNT_SHIFT: usize = 12;
873 const ID_COUNT_MASK: u64 = 0x03;
874 const ID_COUNT_BITS: usize = 2;
875
876 pub fn add_list(&mut self, endpoint: u16, vcpu_ids: &[u16]) -> Result<(), Error> {
877 if self.list_count >= MAX_COUNT || vcpu_ids.len() > Self::ID_COUNT_MASK as usize {
878 return Err(Error::InvalidNotificationCount);
879 }
880
881 // Each list contains at least one ID: the partition ID, followed by vCPU IDs. The number
882 // of vCPU IDs is recorded in `id_counts`.
883 let mut current_id_index = self.list_count + self.id_counts.iter().sum::<u8>() as usize;
884 if current_id_index + 1 + vcpu_ids.len() > MAX_COUNT {
885 // The new list does not fit into the available space for IDs.
886 return Err(Error::InvalidNotificationCount);
887 }
888
889 self.id_counts[self.list_count] = vcpu_ids.len() as u8;
890 self.list_count += 1;
891
892 // The first ID is the endpoint ID.
893 self.ids[current_id_index] = endpoint;
894 current_id_index += 1;
895
896 // Insert the vCPU IDs.
897 self.ids[current_id_index..current_id_index + vcpu_ids.len()].copy_from_slice(vcpu_ids);
898
899 Ok(())
900 }
901
902 pub fn iter(&self) -> NotificationInfoGetIterator<'_> {
903 NotificationInfoGetIterator {
904 list_index: 0,
905 id_index: 0,
906 id_count: &self.id_counts[0..self.list_count],
907 ids: &self.ids,
908 }
909 }
910
911 /// Pack flags field and IDs.
912 fn pack(self) -> (u64, [u16; MAX_COUNT]) {
913 let mut flags = if self.more_pending_notifications {
914 Self::MORE_PENDING_NOTIFICATIONS_FLAG
915 } else {
916 0
917 };
918
919 flags |= (self.list_count as u64) << Self::LIST_COUNT_SHIFT;
920 for (count, shift) in self.id_counts.iter().take(self.list_count).zip(
921 (Self::ID_COUNT_SHIFT..Self::ID_COUNT_SHIFT + Self::ID_COUNT_BITS * MAX_COUNT)
922 .step_by(Self::ID_COUNT_BITS),
923 ) {
924 flags |= u64::from(*count) << shift;
925 }
926
927 (flags, self.ids)
928 }
929
930 /// Unpack flags field and IDs.
931 fn unpack(flags: u64, ids: [u16; MAX_COUNT]) -> Result<Self, Error> {
932 let count_of_lists = ((flags >> Self::LIST_COUNT_SHIFT) & Self::LIST_COUNT_MASK) as usize;
933
934 if count_of_lists > MAX_COUNT {
935 return Err(Error::InvalidNotificationCount);
936 }
937
938 let mut count_of_ids = [0; MAX_COUNT];
939 let mut count_of_ids_bits = flags >> Self::ID_COUNT_SHIFT;
940
941 for id in count_of_ids.iter_mut().take(count_of_lists) {
942 *id = (count_of_ids_bits & Self::ID_COUNT_MASK) as u8;
943 count_of_ids_bits >>= Self::ID_COUNT_BITS;
944 }
945
946 Ok(Self {
947 more_pending_notifications: (flags & Self::MORE_PENDING_NOTIFICATIONS_FLAG) != 0,
948 list_count: count_of_lists,
949 id_counts: count_of_ids,
950 ids,
951 })
952 }
953}
954
955/// `FFA_NOTIFICATION_INFO_GET_32` specific success argument structure.
956pub type SuccessArgsNotificationInfoGet32 = SuccessArgsNotificationInfoGet<10>;
957
958impl From<SuccessArgsNotificationInfoGet32> for SuccessArgs {
959 fn from(value: SuccessArgsNotificationInfoGet32) -> Self {
960 let (flags, ids) = value.pack();
961 let id_regs: [u32; 5] = transmute!(ids);
962
963 let mut args = [0; 6];
964 args[0] = flags as u32;
965 args[1..6].copy_from_slice(&id_regs);
966
967 SuccessArgs::Args32(args)
968 }
969}
970
971impl TryFrom<SuccessArgs> for SuccessArgsNotificationInfoGet32 {
972 type Error = Error;
973
974 fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> {
975 let args = value.try_get_args32()?;
976 let flags = args[0].into();
977 let id_regs: [u32; 5] = args[1..6].try_into().unwrap();
978 Self::unpack(flags, transmute!(id_regs))
979 }
980}
981
982/// `FFA_NOTIFICATION_INFO_GET_64` specific success argument structure.
983pub type SuccessArgsNotificationInfoGet64 = SuccessArgsNotificationInfoGet<20>;
984
985impl From<SuccessArgsNotificationInfoGet64> for SuccessArgs {
986 fn from(value: SuccessArgsNotificationInfoGet64) -> Self {
987 let (flags, ids) = value.pack();
988 let id_regs: [u64; 5] = transmute!(ids);
989
990 let mut args = [0; 6];
991 args[0] = flags;
992 args[1..6].copy_from_slice(&id_regs);
993
994 SuccessArgs::Args64(args)
995 }
996}
997
998impl TryFrom<SuccessArgs> for SuccessArgsNotificationInfoGet64 {
999 type Error = Error;
1000
1001 fn try_from(value: SuccessArgs) -> Result<Self, Self::Error> {
1002 let args = value.try_get_args64()?;
1003 let flags = args[0];
1004 let id_regs: [u64; 5] = args[1..6].try_into().unwrap();
1005 Self::unpack(flags, transmute!(id_regs))
1006 }
1007}
1008
1009pub struct NotificationInfoGetIterator<'a> {
1010 list_index: usize,
1011 id_index: usize,
1012 id_count: &'a [u8],
1013 ids: &'a [u16],
1014}
1015
1016impl<'a> Iterator for NotificationInfoGetIterator<'a> {
1017 type Item = (u16, &'a [u16]);
1018
1019 fn next(&mut self) -> Option<Self::Item> {
1020 if self.list_index < self.id_count.len() {
1021 let partition_id = self.ids[self.id_index];
1022 let id_range =
1023 (self.id_index + 1)..=(self.id_index + self.id_count[self.list_index] as usize);
1024
1025 self.id_index += 1 + self.id_count[self.list_index] as usize;
1026 self.list_index += 1;
1027
1028 Some((partition_id, &self.ids[id_range]))
1029 } else {
1030 None
1031 }
1032 }
1033}
1034
Tomás Gonzálezf268e322025-03-05 11:18:11 +00001035/// FF-A "message types", the terminology used by the spec is "interfaces".
1036///
1037/// The interfaces are used by FF-A components for communication at an FF-A instance. The spec also
1038/// describes the valid FF-A instances and conduits for each interface.
Balint Dobszay3aad9572025-01-17 16:54:11 +01001039#[derive(Debug, Eq, PartialEq, Clone, Copy)]
1040pub enum Interface {
1041 Error {
1042 target_info: TargetInfo,
1043 error_code: FfaError,
Balint Dobszayb727aab2025-04-07 10:24:59 +02001044 error_arg: u32,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001045 },
1046 Success {
1047 target_info: u32,
1048 args: SuccessArgs,
1049 },
1050 Interrupt {
1051 target_info: TargetInfo,
1052 interrupt_id: u32,
1053 },
1054 Version {
1055 input_version: Version,
1056 },
1057 VersionOut {
1058 output_version: Version,
1059 },
1060 Features {
1061 feat_id: Feature,
1062 input_properties: u32,
1063 },
1064 RxAcquire {
1065 vm_id: u16,
1066 },
1067 RxRelease {
1068 vm_id: u16,
1069 },
1070 RxTxMap {
1071 addr: RxTxAddr,
1072 page_cnt: u32,
1073 },
1074 RxTxUnmap {
1075 id: u16,
1076 },
1077 PartitionInfoGet {
1078 uuid: Uuid,
Imre Kise295adb2025-04-10 13:26:28 +02001079 flags: PartitionInfoGetFlags,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001080 },
Tomás González0a058bc2025-03-11 11:20:55 +00001081 PartitionInfoGetRegs {
1082 uuid: Uuid,
1083 start_index: u16,
1084 info_tag: u16,
1085 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001086 IdGet,
1087 SpmIdGet,
Tomás González092202a2025-03-05 11:56:45 +00001088 MsgWait {
1089 flags: Option<MsgWaitFlags>,
1090 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001091 Yield,
1092 Run {
1093 target_info: TargetInfo,
1094 },
1095 NormalWorldResume,
Tomás González17b92442025-03-10 16:45:04 +00001096 SecondaryEpRegister {
1097 entrypoint: SecondaryEpRegisterAddr,
1098 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001099 MsgSend2 {
1100 sender_vm_id: u16,
1101 flags: u32,
1102 },
1103 MsgSendDirectReq {
1104 src_id: u16,
1105 dst_id: u16,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001106 args: DirectMsgArgs,
1107 },
1108 MsgSendDirectResp {
1109 src_id: u16,
1110 dst_id: u16,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001111 args: DirectMsgArgs,
1112 },
Balint Dobszayde0dc802025-02-28 14:16:52 +01001113 MsgSendDirectReq2 {
1114 src_id: u16,
1115 dst_id: u16,
1116 uuid: Uuid,
1117 args: DirectMsg2Args,
1118 },
1119 MsgSendDirectResp2 {
1120 src_id: u16,
1121 dst_id: u16,
1122 args: DirectMsg2Args,
1123 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001124 MemDonate {
1125 total_len: u32,
1126 frag_len: u32,
1127 buf: Option<MemOpBuf>,
1128 },
1129 MemLend {
1130 total_len: u32,
1131 frag_len: u32,
1132 buf: Option<MemOpBuf>,
1133 },
1134 MemShare {
1135 total_len: u32,
1136 frag_len: u32,
1137 buf: Option<MemOpBuf>,
1138 },
1139 MemRetrieveReq {
1140 total_len: u32,
1141 frag_len: u32,
1142 buf: Option<MemOpBuf>,
1143 },
1144 MemRetrieveResp {
1145 total_len: u32,
1146 frag_len: u32,
1147 },
1148 MemRelinquish,
1149 MemReclaim {
1150 handle: memory_management::Handle,
1151 flags: u32,
1152 },
1153 MemPermGet {
1154 addr: MemAddr,
Balint Dobszayde0dc802025-02-28 14:16:52 +01001155 page_cnt: Option<u32>,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001156 },
1157 MemPermSet {
1158 addr: MemAddr,
1159 page_cnt: u32,
1160 mem_perm: u32,
1161 },
1162 ConsoleLog {
1163 char_cnt: u8,
1164 char_lists: ConsoleLogChars,
1165 },
Tomás González7ffb6132025-04-03 12:28:58 +01001166 NotificationBitmapCreate {
1167 vm_id: u16,
1168 vcpu_cnt: u32,
1169 },
1170 NotificationBitmapDestroy {
1171 vm_id: u16,
1172 },
1173 NotificationBind {
1174 sender_id: u16,
1175 receiver_id: u16,
1176 flags: NotificationBindFlags,
1177 bitmap: u64,
1178 },
1179 NotificationUnBind {
1180 sender_id: u16,
1181 receiver_id: u16,
1182 bitmap: u64,
1183 },
1184 NotificationSet {
1185 sender_id: u16,
1186 receiver_id: u16,
1187 flags: NotificationSetFlags,
1188 bitmap: u64,
1189 },
1190 NotificationGet {
1191 vcpu_id: u16,
1192 endpoint_id: u16,
1193 flags: NotificationGetFlags,
1194 },
1195 NotificationInfoGet {
1196 is_32bit: bool,
1197 },
Tomás Gonzáleze6fe75f2025-04-04 09:46:50 +01001198 El3IntrHandle,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001199}
1200
Balint Dobszayde0dc802025-02-28 14:16:52 +01001201impl Interface {
1202 /// Returns the function ID for the call, if it has one.
1203 pub fn function_id(&self) -> Option<FuncId> {
1204 match self {
1205 Interface::Error { .. } => Some(FuncId::Error),
1206 Interface::Success { args, .. } => match args {
Imre Kis54773b62025-04-10 13:47:39 +02001207 SuccessArgs::Args32(..) => Some(FuncId::Success32),
1208 SuccessArgs::Args64(..) | SuccessArgs::Args64_2(..) => Some(FuncId::Success64),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001209 },
1210 Interface::Interrupt { .. } => Some(FuncId::Interrupt),
1211 Interface::Version { .. } => Some(FuncId::Version),
1212 Interface::VersionOut { .. } => None,
1213 Interface::Features { .. } => Some(FuncId::Features),
1214 Interface::RxAcquire { .. } => Some(FuncId::RxAcquire),
1215 Interface::RxRelease { .. } => Some(FuncId::RxRelease),
1216 Interface::RxTxMap { addr, .. } => match addr {
1217 RxTxAddr::Addr32 { .. } => Some(FuncId::RxTxMap32),
1218 RxTxAddr::Addr64 { .. } => Some(FuncId::RxTxMap64),
1219 },
1220 Interface::RxTxUnmap { .. } => Some(FuncId::RxTxUnmap),
1221 Interface::PartitionInfoGet { .. } => Some(FuncId::PartitionInfoGet),
Tomás González0a058bc2025-03-11 11:20:55 +00001222 Interface::PartitionInfoGetRegs { .. } => Some(FuncId::PartitionInfoGetRegs),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001223 Interface::IdGet => Some(FuncId::IdGet),
1224 Interface::SpmIdGet => Some(FuncId::SpmIdGet),
Tomás González092202a2025-03-05 11:56:45 +00001225 Interface::MsgWait { .. } => Some(FuncId::MsgWait),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001226 Interface::Yield => Some(FuncId::Yield),
1227 Interface::Run { .. } => Some(FuncId::Run),
1228 Interface::NormalWorldResume => Some(FuncId::NormalWorldResume),
Tomás González17b92442025-03-10 16:45:04 +00001229 Interface::SecondaryEpRegister { entrypoint } => match entrypoint {
1230 SecondaryEpRegisterAddr::Addr32 { .. } => Some(FuncId::SecondaryEpRegister32),
1231 SecondaryEpRegisterAddr::Addr64 { .. } => Some(FuncId::SecondaryEpRegister64),
1232 },
Balint Dobszayde0dc802025-02-28 14:16:52 +01001233 Interface::MsgSend2 { .. } => Some(FuncId::MsgSend2),
1234 Interface::MsgSendDirectReq { args, .. } => match args {
1235 DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectReq32),
1236 DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectReq64),
Tomás González4d5b0ba2025-03-03 17:15:55 +00001237 DirectMsgArgs::VersionReq { .. } => Some(FuncId::MsgSendDirectReq32),
1238 DirectMsgArgs::PowerPsciReq32 { .. } => Some(FuncId::MsgSendDirectReq32),
1239 DirectMsgArgs::PowerPsciReq64 { .. } => Some(FuncId::MsgSendDirectReq64),
1240 DirectMsgArgs::PowerWarmBootReq { .. } => Some(FuncId::MsgSendDirectReq32),
1241 DirectMsgArgs::VmCreated { .. } => Some(FuncId::MsgSendDirectReq32),
1242 DirectMsgArgs::VmDestructed { .. } => Some(FuncId::MsgSendDirectReq32),
1243 _ => None,
Balint Dobszayde0dc802025-02-28 14:16:52 +01001244 },
1245 Interface::MsgSendDirectResp { args, .. } => match args {
1246 DirectMsgArgs::Args32(_) => Some(FuncId::MsgSendDirectResp32),
1247 DirectMsgArgs::Args64(_) => Some(FuncId::MsgSendDirectResp64),
Tomás González4d5b0ba2025-03-03 17:15:55 +00001248 DirectMsgArgs::VersionResp { .. } => Some(FuncId::MsgSendDirectResp32),
1249 DirectMsgArgs::PowerPsciResp { .. } => Some(FuncId::MsgSendDirectResp32),
1250 DirectMsgArgs::VmCreatedAck { .. } => Some(FuncId::MsgSendDirectResp32),
1251 DirectMsgArgs::VmDestructedAck { .. } => Some(FuncId::MsgSendDirectResp32),
1252 _ => None,
Balint Dobszayde0dc802025-02-28 14:16:52 +01001253 },
1254 Interface::MsgSendDirectReq2 { .. } => Some(FuncId::MsgSendDirectReq64_2),
1255 Interface::MsgSendDirectResp2 { .. } => Some(FuncId::MsgSendDirectResp64_2),
1256 Interface::MemDonate { buf, .. } => match buf {
1257 Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemDonate64),
1258 _ => Some(FuncId::MemDonate32),
1259 },
1260 Interface::MemLend { buf, .. } => match buf {
1261 Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemLend64),
1262 _ => Some(FuncId::MemLend32),
1263 },
1264 Interface::MemShare { buf, .. } => match buf {
1265 Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemShare64),
1266 _ => Some(FuncId::MemShare32),
1267 },
1268 Interface::MemRetrieveReq { buf, .. } => match buf {
1269 Some(MemOpBuf::Buf64 { .. }) => Some(FuncId::MemRetrieveReq64),
1270 _ => Some(FuncId::MemRetrieveReq32),
1271 },
1272 Interface::MemRetrieveResp { .. } => Some(FuncId::MemRetrieveResp),
1273 Interface::MemRelinquish => Some(FuncId::MemRelinquish),
1274 Interface::MemReclaim { .. } => Some(FuncId::MemReclaim),
1275 Interface::MemPermGet { addr, .. } => match addr {
1276 MemAddr::Addr32(_) => Some(FuncId::MemPermGet32),
1277 MemAddr::Addr64(_) => Some(FuncId::MemPermGet64),
1278 },
1279 Interface::MemPermSet { addr, .. } => match addr {
1280 MemAddr::Addr32(_) => Some(FuncId::MemPermSet32),
1281 MemAddr::Addr64(_) => Some(FuncId::MemPermSet64),
1282 },
1283 Interface::ConsoleLog { char_lists, .. } => match char_lists {
1284 ConsoleLogChars::Reg32(_) => Some(FuncId::ConsoleLog32),
1285 ConsoleLogChars::Reg64(_) => Some(FuncId::ConsoleLog64),
1286 },
Tomás González7ffb6132025-04-03 12:28:58 +01001287 Interface::NotificationBitmapCreate { .. } => Some(FuncId::NotificationBitmapCreate),
1288 Interface::NotificationBitmapDestroy { .. } => Some(FuncId::NotificationBitmapDestroy),
1289 Interface::NotificationBind { .. } => Some(FuncId::NotificationBind),
1290 Interface::NotificationUnBind { .. } => Some(FuncId::NotificationUnbind),
1291 Interface::NotificationSet { .. } => Some(FuncId::NotificationSet),
1292 Interface::NotificationGet { .. } => Some(FuncId::NotificationGet),
1293 Interface::NotificationInfoGet { is_32bit } => match is_32bit {
1294 true => Some(FuncId::NotificationInfoGet32),
1295 false => Some(FuncId::NotificationInfoGet64),
1296 },
Tomás Gonzáleze6fe75f2025-04-04 09:46:50 +01001297 Interface::El3IntrHandle => Some(FuncId::El3IntrHandle),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001298 }
1299 }
Balint Dobszay3aad9572025-01-17 16:54:11 +01001300
Balint Dobszayde0dc802025-02-28 14:16:52 +01001301 /// Returns true if this is a 32-bit call, or false if it is a 64-bit call.
1302 pub fn is_32bit(&self) -> bool {
1303 // TODO: self should always have a function ID?
1304 self.function_id().unwrap().is_32bit()
1305 }
1306
1307 /// Parse interface from register contents. The caller must ensure that the `regs` argument has
1308 /// the correct length: 8 registers for FF-A v1.1 and lower, 18 registers for v1.2 and higher.
1309 pub fn from_regs(version: Version, regs: &[u64]) -> Result<Self, Error> {
1310 let reg_cnt = regs.len();
1311
1312 let msg = match reg_cnt {
1313 8 => {
1314 assert!(version <= Version(1, 1));
1315 Interface::unpack_regs8(version, regs.try_into().unwrap())?
1316 }
1317 18 => {
1318 assert!(version >= Version(1, 2));
1319 match FuncId::try_from(regs[0] as u32)? {
1320 FuncId::ConsoleLog64
1321 | FuncId::Success64
1322 | FuncId::MsgSendDirectReq64_2
Tomás González0a058bc2025-03-11 11:20:55 +00001323 | FuncId::MsgSendDirectResp64_2
1324 | FuncId::PartitionInfoGetRegs => {
Balint Dobszayde0dc802025-02-28 14:16:52 +01001325 Interface::unpack_regs18(version, regs.try_into().unwrap())?
1326 }
1327 _ => Interface::unpack_regs8(version, regs[..8].try_into().unwrap())?,
1328 }
1329 }
1330 _ => panic!(
1331 "Invalid number of registers ({}) for FF-A version {}",
1332 reg_cnt, version
1333 ),
1334 };
1335
1336 Ok(msg)
1337 }
1338
1339 fn unpack_regs8(version: Version, regs: &[u64; 8]) -> Result<Self, Error> {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001340 let fid = FuncId::try_from(regs[0] as u32)?;
1341
1342 let msg = match fid {
1343 FuncId::Error => Self::Error {
1344 target_info: (regs[1] as u32).into(),
1345 error_code: FfaError::try_from(regs[2] as i32)?,
Balint Dobszayb727aab2025-04-07 10:24:59 +02001346 error_arg: regs[3] as u32,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001347 },
1348 FuncId::Success32 => Self::Success {
1349 target_info: regs[1] as u32,
Imre Kis54773b62025-04-10 13:47:39 +02001350 args: SuccessArgs::Args32([
Balint Dobszay3aad9572025-01-17 16:54:11 +01001351 regs[2] as u32,
1352 regs[3] as u32,
1353 regs[4] as u32,
1354 regs[5] as u32,
1355 regs[6] as u32,
1356 regs[7] as u32,
1357 ]),
1358 },
1359 FuncId::Success64 => Self::Success {
1360 target_info: regs[1] as u32,
Imre Kis54773b62025-04-10 13:47:39 +02001361 args: SuccessArgs::Args64([regs[2], regs[3], regs[4], regs[5], regs[6], regs[7]]),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001362 },
1363 FuncId::Interrupt => Self::Interrupt {
1364 target_info: (regs[1] as u32).into(),
1365 interrupt_id: regs[2] as u32,
1366 },
1367 FuncId::Version => Self::Version {
Tomás González83146af2025-03-04 11:32:41 +00001368 input_version: (regs[1] as u32).try_into()?,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001369 },
1370 FuncId::Features => Self::Features {
Balint Dobszayc31e0b92025-03-03 20:16:56 +01001371 feat_id: (regs[1] as u32).into(),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001372 input_properties: regs[2] as u32,
1373 },
1374 FuncId::RxAcquire => Self::RxAcquire {
1375 vm_id: regs[1] as u16,
1376 },
1377 FuncId::RxRelease => Self::RxRelease {
1378 vm_id: regs[1] as u16,
1379 },
1380 FuncId::RxTxMap32 => {
1381 let addr = RxTxAddr::Addr32 {
1382 rx: regs[2] as u32,
1383 tx: regs[1] as u32,
1384 };
1385 let page_cnt = regs[3] as u32;
1386
1387 Self::RxTxMap { addr, page_cnt }
1388 }
1389 FuncId::RxTxMap64 => {
1390 let addr = RxTxAddr::Addr64 {
1391 rx: regs[2],
1392 tx: regs[1],
1393 };
1394 let page_cnt = regs[3] as u32;
1395
1396 Self::RxTxMap { addr, page_cnt }
1397 }
1398 FuncId::RxTxUnmap => Self::RxTxUnmap { id: regs[1] as u16 },
1399 FuncId::PartitionInfoGet => {
1400 let uuid_words = [
1401 regs[1] as u32,
1402 regs[2] as u32,
1403 regs[3] as u32,
1404 regs[4] as u32,
1405 ];
1406 let mut bytes: [u8; 16] = [0; 16];
1407 for (i, b) in uuid_words.iter().flat_map(|w| w.to_le_bytes()).enumerate() {
1408 bytes[i] = b;
1409 }
1410 Self::PartitionInfoGet {
1411 uuid: Uuid::from_bytes(bytes),
Imre Kise295adb2025-04-10 13:26:28 +02001412 flags: PartitionInfoGetFlags::try_from(regs[5] as u32)?,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001413 }
1414 }
1415 FuncId::IdGet => Self::IdGet,
1416 FuncId::SpmIdGet => Self::SpmIdGet,
Tomás González092202a2025-03-05 11:56:45 +00001417 FuncId::MsgWait => Self::MsgWait {
1418 flags: if version >= Version(1, 2) {
1419 Some(MsgWaitFlags::try_from(regs[2] as u32)?)
1420 } else {
1421 None
1422 },
1423 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001424 FuncId::Yield => Self::Yield,
1425 FuncId::Run => Self::Run {
1426 target_info: (regs[1] as u32).into(),
1427 },
1428 FuncId::NormalWorldResume => Self::NormalWorldResume,
Tomás González17b92442025-03-10 16:45:04 +00001429 FuncId::SecondaryEpRegister32 => Self::SecondaryEpRegister {
1430 entrypoint: SecondaryEpRegisterAddr::Addr32(regs[1] as u32),
1431 },
1432 FuncId::SecondaryEpRegister64 => Self::SecondaryEpRegister {
1433 entrypoint: SecondaryEpRegisterAddr::Addr64(regs[1]),
1434 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001435 FuncId::MsgSend2 => Self::MsgSend2 {
1436 sender_vm_id: regs[1] as u16,
1437 flags: regs[2] as u32,
1438 },
1439 FuncId::MsgSendDirectReq32 => Self::MsgSendDirectReq {
1440 src_id: (regs[1] >> 16) as u16,
1441 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +00001442 args: if (regs[2] as u32 & DirectMsgArgs::FWK_MSG_BITS) != 0 {
1443 match regs[2] as u32 {
1444 DirectMsgArgs::VERSION_REQ => DirectMsgArgs::VersionReq {
1445 version: Version::try_from(regs[3] as u32)?,
1446 },
1447 DirectMsgArgs::POWER_PSCI_REQ => DirectMsgArgs::PowerPsciReq32 {
Tomás González67f92c72025-03-20 16:50:42 +00001448 params: [
1449 regs[3] as u32,
1450 regs[4] as u32,
1451 regs[5] as u32,
1452 regs[6] as u32,
1453 ],
Tomás González4d5b0ba2025-03-03 17:15:55 +00001454 },
1455 DirectMsgArgs::POWER_WARM_BOOT_REQ => DirectMsgArgs::PowerWarmBootReq {
1456 boot_type: WarmBootType::try_from(regs[3] as u32)?,
1457 },
1458 DirectMsgArgs::VM_CREATED => DirectMsgArgs::VmCreated {
1459 handle: memory_management::Handle::from([
1460 regs[3] as u32,
1461 regs[4] as u32,
1462 ]),
1463 vm_id: regs[5] as u16,
1464 },
1465 DirectMsgArgs::VM_DESTRUCTED => DirectMsgArgs::VmDestructed {
1466 handle: memory_management::Handle::from([
1467 regs[3] as u32,
1468 regs[4] as u32,
1469 ]),
1470 vm_id: regs[5] as u16,
1471 },
1472 _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)),
1473 }
1474 } else {
1475 DirectMsgArgs::Args32([
1476 regs[3] as u32,
1477 regs[4] as u32,
1478 regs[5] as u32,
1479 regs[6] as u32,
1480 regs[7] as u32,
1481 ])
1482 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001483 },
1484 FuncId::MsgSendDirectReq64 => Self::MsgSendDirectReq {
1485 src_id: (regs[1] >> 16) as u16,
1486 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +00001487 args: if (regs[2] & DirectMsgArgs::FWK_MSG_BITS as u64) != 0 {
1488 match regs[2] as u32 {
1489 DirectMsgArgs::POWER_PSCI_REQ => DirectMsgArgs::PowerPsciReq64 {
Tomás González67f92c72025-03-20 16:50:42 +00001490 params: [regs[3], regs[4], regs[5], regs[6]],
Tomás González4d5b0ba2025-03-03 17:15:55 +00001491 },
1492 _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)),
1493 }
1494 } else {
1495 DirectMsgArgs::Args64([regs[3], regs[4], regs[5], regs[6], regs[7]])
1496 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001497 },
1498 FuncId::MsgSendDirectResp32 => Self::MsgSendDirectResp {
1499 src_id: (regs[1] >> 16) as u16,
1500 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +00001501 args: if (regs[2] as u32 & DirectMsgArgs::FWK_MSG_BITS) != 0 {
1502 match regs[2] as u32 {
1503 DirectMsgArgs::VERSION_RESP => {
1504 if regs[3] as i32 == FfaError::NotSupported.into() {
1505 DirectMsgArgs::VersionResp { version: None }
1506 } else {
1507 DirectMsgArgs::VersionResp {
1508 version: Some(Version::try_from(regs[3] as u32)?),
1509 }
1510 }
1511 }
1512 DirectMsgArgs::POWER_PSCI_RESP => DirectMsgArgs::PowerPsciResp {
1513 psci_status: regs[3] as i32,
1514 },
1515 DirectMsgArgs::VM_CREATED_ACK => DirectMsgArgs::VmCreatedAck {
1516 sp_status: (regs[3] as i32).try_into()?,
1517 },
1518 DirectMsgArgs::VM_DESTRUCTED_ACK => DirectMsgArgs::VmDestructedAck {
1519 sp_status: (regs[3] as i32).try_into()?,
1520 },
1521 _ => return Err(Error::UnrecognisedFwkMsg(regs[2] as u32)),
1522 }
1523 } else {
1524 DirectMsgArgs::Args32([
1525 regs[3] as u32,
1526 regs[4] as u32,
1527 regs[5] as u32,
1528 regs[6] as u32,
1529 regs[7] as u32,
1530 ])
1531 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001532 },
1533 FuncId::MsgSendDirectResp64 => Self::MsgSendDirectResp {
1534 src_id: (regs[1] >> 16) as u16,
1535 dst_id: regs[1] as u16,
Tomás González4d5b0ba2025-03-03 17:15:55 +00001536 args: if (regs[2] & DirectMsgArgs::FWK_MSG_BITS as u64) != 0 {
1537 return Err(Error::UnrecognisedFwkMsg(regs[2] as u32));
1538 } else {
1539 DirectMsgArgs::Args64([regs[3], regs[4], regs[5], regs[6], regs[7]])
1540 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001541 },
1542 FuncId::MemDonate32 => Self::MemDonate {
1543 total_len: regs[1] as u32,
1544 frag_len: regs[2] as u32,
1545 buf: if regs[3] != 0 && regs[4] != 0 {
1546 Some(MemOpBuf::Buf32 {
1547 addr: regs[3] as u32,
1548 page_cnt: regs[4] as u32,
1549 })
1550 } else {
1551 None
1552 },
1553 },
1554 FuncId::MemDonate64 => Self::MemDonate {
1555 total_len: regs[1] as u32,
1556 frag_len: regs[2] as u32,
1557 buf: if regs[3] != 0 && regs[4] != 0 {
1558 Some(MemOpBuf::Buf64 {
1559 addr: regs[3],
1560 page_cnt: regs[4] as u32,
1561 })
1562 } else {
1563 None
1564 },
1565 },
1566 FuncId::MemLend32 => Self::MemLend {
1567 total_len: regs[1] as u32,
1568 frag_len: regs[2] as u32,
1569 buf: if regs[3] != 0 && regs[4] != 0 {
1570 Some(MemOpBuf::Buf32 {
1571 addr: regs[3] as u32,
1572 page_cnt: regs[4] as u32,
1573 })
1574 } else {
1575 None
1576 },
1577 },
1578 FuncId::MemLend64 => Self::MemLend {
1579 total_len: regs[1] as u32,
1580 frag_len: regs[2] as u32,
1581 buf: if regs[3] != 0 && regs[4] != 0 {
1582 Some(MemOpBuf::Buf64 {
1583 addr: regs[3],
1584 page_cnt: regs[4] as u32,
1585 })
1586 } else {
1587 None
1588 },
1589 },
1590 FuncId::MemShare32 => Self::MemShare {
1591 total_len: regs[1] as u32,
1592 frag_len: regs[2] as u32,
1593 buf: if regs[3] != 0 && regs[4] != 0 {
1594 Some(MemOpBuf::Buf32 {
1595 addr: regs[3] as u32,
1596 page_cnt: regs[4] as u32,
1597 })
1598 } else {
1599 None
1600 },
1601 },
1602 FuncId::MemShare64 => Self::MemShare {
1603 total_len: regs[1] as u32,
1604 frag_len: regs[2] as u32,
1605 buf: if regs[3] != 0 && regs[4] != 0 {
1606 Some(MemOpBuf::Buf64 {
1607 addr: regs[3],
1608 page_cnt: regs[4] as u32,
1609 })
1610 } else {
1611 None
1612 },
1613 },
1614 FuncId::MemRetrieveReq32 => Self::MemRetrieveReq {
1615 total_len: regs[1] as u32,
1616 frag_len: regs[2] as u32,
1617 buf: if regs[3] != 0 && regs[4] != 0 {
1618 Some(MemOpBuf::Buf32 {
1619 addr: regs[3] as u32,
1620 page_cnt: regs[4] as u32,
1621 })
1622 } else {
1623 None
1624 },
1625 },
1626 FuncId::MemRetrieveReq64 => Self::MemRetrieveReq {
1627 total_len: regs[1] as u32,
1628 frag_len: regs[2] as u32,
1629 buf: if regs[3] != 0 && regs[4] != 0 {
1630 Some(MemOpBuf::Buf64 {
1631 addr: regs[3],
1632 page_cnt: regs[4] as u32,
1633 })
1634 } else {
1635 None
1636 },
1637 },
1638 FuncId::MemRetrieveResp => Self::MemRetrieveResp {
1639 total_len: regs[1] as u32,
1640 frag_len: regs[2] as u32,
1641 },
1642 FuncId::MemRelinquish => Self::MemRelinquish,
1643 FuncId::MemReclaim => Self::MemReclaim {
1644 handle: memory_management::Handle::from([regs[1] as u32, regs[2] as u32]),
1645 flags: regs[3] as u32,
1646 },
1647 FuncId::MemPermGet32 => Self::MemPermGet {
1648 addr: MemAddr::Addr32(regs[1] as u32),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001649 page_cnt: if version >= Version(1, 3) {
1650 Some(regs[2] as u32)
1651 } else {
1652 None
1653 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001654 },
1655 FuncId::MemPermGet64 => Self::MemPermGet {
1656 addr: MemAddr::Addr64(regs[1]),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001657 page_cnt: if version >= Version(1, 3) {
1658 Some(regs[2] as u32)
1659 } else {
1660 None
1661 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001662 },
1663 FuncId::MemPermSet32 => Self::MemPermSet {
1664 addr: MemAddr::Addr32(regs[1] as u32),
1665 page_cnt: regs[2] as u32,
1666 mem_perm: regs[3] as u32,
1667 },
1668 FuncId::MemPermSet64 => Self::MemPermSet {
1669 addr: MemAddr::Addr64(regs[1]),
1670 page_cnt: regs[2] as u32,
1671 mem_perm: regs[3] as u32,
1672 },
1673 FuncId::ConsoleLog32 => Self::ConsoleLog {
1674 char_cnt: regs[1] as u8,
1675 char_lists: ConsoleLogChars::Reg32([
1676 regs[2] as u32,
1677 regs[3] as u32,
1678 regs[4] as u32,
1679 regs[5] as u32,
1680 regs[6] as u32,
1681 regs[7] as u32,
1682 ]),
1683 },
Tomás González7ffb6132025-04-03 12:28:58 +01001684 FuncId::NotificationBitmapCreate => {
1685 let tentative_vm_id = regs[1] as u32;
1686 if (tentative_vm_id >> 16) != 0 {
1687 return Err(Error::InvalidVmId(tentative_vm_id));
1688 }
1689 Self::NotificationBitmapCreate {
1690 vm_id: tentative_vm_id as u16,
1691 vcpu_cnt: regs[2] as u32,
1692 }
1693 }
1694 FuncId::NotificationBitmapDestroy => {
1695 let tentative_vm_id = regs[1] as u32;
1696 if (tentative_vm_id >> 16) != 0 {
1697 return Err(Error::InvalidVmId(tentative_vm_id));
1698 }
1699 Self::NotificationBitmapDestroy {
1700 vm_id: tentative_vm_id as u16,
1701 }
1702 }
1703 FuncId::NotificationBind => Self::NotificationBind {
1704 sender_id: (regs[1] >> 16) as u16,
1705 receiver_id: regs[1] as u16,
1706 flags: (regs[2] as u32).into(),
1707 bitmap: (regs[4] << 32) | (regs[3] & 0xffff_ffff),
1708 },
1709 FuncId::NotificationUnbind => Self::NotificationUnBind {
1710 sender_id: (regs[1] >> 16) as u16,
1711 receiver_id: regs[1] as u16,
1712 bitmap: (regs[4] << 32) | (regs[3] & 0xffff_ffff),
1713 },
1714 FuncId::NotificationSet => Self::NotificationSet {
1715 sender_id: (regs[1] >> 16) as u16,
1716 receiver_id: regs[1] as u16,
1717 flags: (regs[2] as u32).try_into()?,
1718 bitmap: (regs[4] << 32) | (regs[3] & 0xffff_ffff),
1719 },
1720 FuncId::NotificationGet => Self::NotificationGet {
1721 vcpu_id: (regs[1] >> 16) as u16,
1722 endpoint_id: regs[1] as u16,
1723 flags: (regs[2] as u32).into(),
1724 },
1725 FuncId::NotificationInfoGet32 => Self::NotificationInfoGet { is_32bit: true },
1726 FuncId::NotificationInfoGet64 => Self::NotificationInfoGet { is_32bit: false },
Tomás Gonzáleze6fe75f2025-04-04 09:46:50 +01001727 FuncId::El3IntrHandle => Self::El3IntrHandle,
Balint Dobszayde0dc802025-02-28 14:16:52 +01001728 _ => panic!("Invalid number of registers (8) for function {:#x?}", fid),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001729 };
1730
1731 Ok(msg)
1732 }
Balint Dobszay3aad9572025-01-17 16:54:11 +01001733
Balint Dobszayde0dc802025-02-28 14:16:52 +01001734 fn unpack_regs18(version: Version, regs: &[u64; 18]) -> Result<Self, Error> {
1735 assert!(version >= Version(1, 2));
Balint Dobszay5bf492f2024-07-29 17:21:32 +02001736
Balint Dobszayde0dc802025-02-28 14:16:52 +01001737 let fid = FuncId::try_from(regs[0] as u32)?;
1738
1739 let msg = match fid {
1740 FuncId::Success64 => Self::Success {
1741 target_info: regs[1] as u32,
Imre Kis54773b62025-04-10 13:47:39 +02001742 args: SuccessArgs::Args64_2(regs[2..18].try_into().unwrap()),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001743 },
1744 FuncId::MsgSendDirectReq64_2 => Self::MsgSendDirectReq2 {
1745 src_id: (regs[1] >> 16) as u16,
1746 dst_id: regs[1] as u16,
Tomás Gonzálezce3bc222025-03-25 14:30:42 +00001747 uuid: Uuid::from_u64_pair(regs[2].swap_bytes(), regs[3].swap_bytes()),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001748 args: DirectMsg2Args(regs[4..18].try_into().unwrap()),
1749 },
1750 FuncId::MsgSendDirectResp64_2 => Self::MsgSendDirectResp2 {
1751 src_id: (regs[1] >> 16) as u16,
1752 dst_id: regs[1] as u16,
1753 args: DirectMsg2Args(regs[4..18].try_into().unwrap()),
1754 },
1755 FuncId::ConsoleLog64 => Self::ConsoleLog {
1756 char_cnt: regs[1] as u8,
1757 char_lists: ConsoleLogChars::Reg64(regs[2..18].try_into().unwrap()),
1758 },
Tomás González0a058bc2025-03-11 11:20:55 +00001759 FuncId::PartitionInfoGetRegs => {
1760 // Bits[15:0]: Start index
1761 let start_index = (regs[3] & 0xffff) as u16;
1762 let info_tag = ((regs[3] >> 16) & 0xffff) as u16;
1763 Self::PartitionInfoGetRegs {
1764 uuid: Uuid::from_u64_pair(regs[1].swap_bytes(), regs[2].swap_bytes()),
1765 start_index,
1766 info_tag: if start_index == 0 && info_tag != 0 {
1767 return Err(Error::InvalidInformationTag(info_tag));
1768 } else {
1769 info_tag
1770 },
1771 }
1772 }
Balint Dobszayde0dc802025-02-28 14:16:52 +01001773 _ => panic!("Invalid number of registers (18) for function {:#x?}", fid),
1774 };
1775
1776 Ok(msg)
Balint Dobszay3aad9572025-01-17 16:54:11 +01001777 }
1778
Balint Dobszaya5846852025-02-26 15:38:53 +01001779 /// Create register contents for an interface.
Balint Dobszayde0dc802025-02-28 14:16:52 +01001780 pub fn to_regs(&self, version: Version, regs: &mut [u64]) {
1781 let reg_cnt = regs.len();
1782
1783 match reg_cnt {
1784 8 => {
1785 assert!(version <= Version(1, 1));
Balint Dobszay91bea9b2025-04-09 13:16:06 +02001786 regs.fill(0);
1787
Balint Dobszayde0dc802025-02-28 14:16:52 +01001788 self.pack_regs8(version, (&mut regs[..8]).try_into().unwrap());
1789 }
1790 18 => {
1791 assert!(version >= Version(1, 2));
Balint Dobszay91bea9b2025-04-09 13:16:06 +02001792 regs.fill(0);
Balint Dobszayde0dc802025-02-28 14:16:52 +01001793
1794 match self {
1795 Interface::ConsoleLog {
1796 char_lists: ConsoleLogChars::Reg64(_),
1797 ..
1798 }
1799 | Interface::Success {
Imre Kis54773b62025-04-10 13:47:39 +02001800 args: SuccessArgs::Args64_2(_),
Balint Dobszayde0dc802025-02-28 14:16:52 +01001801 ..
1802 }
1803 | Interface::MsgSendDirectReq2 { .. }
Tomás González0a058bc2025-03-11 11:20:55 +00001804 | Interface::MsgSendDirectResp2 { .. }
1805 | Interface::PartitionInfoGetRegs { .. } => {
Balint Dobszayde0dc802025-02-28 14:16:52 +01001806 self.pack_regs18(version, regs.try_into().unwrap());
1807 }
1808 _ => {
1809 self.pack_regs8(version, (&mut regs[..8]).try_into().unwrap());
1810 }
1811 }
1812 }
1813 _ => panic!("Invalid number of registers {}", reg_cnt),
1814 }
1815 }
1816
1817 fn pack_regs8(&self, version: Version, a: &mut [u64; 8]) {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001818 if let Some(function_id) = self.function_id() {
1819 a[0] = function_id as u64;
1820 }
1821
1822 match *self {
1823 Interface::Error {
1824 target_info,
1825 error_code,
Balint Dobszayb727aab2025-04-07 10:24:59 +02001826 error_arg,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001827 } => {
1828 a[1] = u32::from(target_info).into();
1829 a[2] = (error_code as u32).into();
Balint Dobszayb727aab2025-04-07 10:24:59 +02001830 a[3] = error_arg.into();
Balint Dobszay3aad9572025-01-17 16:54:11 +01001831 }
1832 Interface::Success { target_info, args } => {
1833 a[1] = target_info.into();
1834 match args {
Imre Kis54773b62025-04-10 13:47:39 +02001835 SuccessArgs::Args32(regs) => {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001836 a[2] = regs[0].into();
1837 a[3] = regs[1].into();
1838 a[4] = regs[2].into();
1839 a[5] = regs[3].into();
1840 a[6] = regs[4].into();
1841 a[7] = regs[5].into();
1842 }
Imre Kis54773b62025-04-10 13:47:39 +02001843 SuccessArgs::Args64(regs) => {
Balint Dobszay3aad9572025-01-17 16:54:11 +01001844 a[2] = regs[0];
1845 a[3] = regs[1];
1846 a[4] = regs[2];
1847 a[5] = regs[3];
1848 a[6] = regs[4];
1849 a[7] = regs[5];
1850 }
Balint Dobszayde0dc802025-02-28 14:16:52 +01001851 _ => panic!("{:#x?} requires 18 registers", args),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001852 }
1853 }
1854 Interface::Interrupt {
1855 target_info,
1856 interrupt_id,
1857 } => {
1858 a[1] = u32::from(target_info).into();
1859 a[2] = interrupt_id.into();
1860 }
1861 Interface::Version { input_version } => {
1862 a[1] = u32::from(input_version).into();
1863 }
1864 Interface::VersionOut { output_version } => {
1865 a[0] = u32::from(output_version).into();
1866 }
1867 Interface::Features {
1868 feat_id,
1869 input_properties,
1870 } => {
1871 a[1] = u32::from(feat_id).into();
1872 a[2] = input_properties.into();
1873 }
1874 Interface::RxAcquire { vm_id } => {
1875 a[1] = vm_id.into();
1876 }
1877 Interface::RxRelease { vm_id } => {
1878 a[1] = vm_id.into();
1879 }
1880 Interface::RxTxMap { addr, page_cnt } => {
1881 match addr {
1882 RxTxAddr::Addr32 { rx, tx } => {
1883 a[1] = tx.into();
1884 a[2] = rx.into();
1885 }
1886 RxTxAddr::Addr64 { rx, tx } => {
1887 a[1] = tx;
1888 a[2] = rx;
1889 }
1890 }
1891 a[3] = page_cnt.into();
1892 }
1893 Interface::RxTxUnmap { id } => {
1894 a[1] = id.into();
1895 }
1896 Interface::PartitionInfoGet { uuid, flags } => {
1897 let bytes = uuid.into_bytes();
1898 a[1] = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]).into();
1899 a[2] = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]).into();
1900 a[3] = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]).into();
1901 a[4] = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]).into();
Imre Kise295adb2025-04-10 13:26:28 +02001902 a[5] = u32::from(flags).into();
Balint Dobszay3aad9572025-01-17 16:54:11 +01001903 }
Tomás González092202a2025-03-05 11:56:45 +00001904 Interface::MsgWait { flags } => {
1905 if version >= Version(1, 2) {
1906 if let Some(flags) = flags {
1907 a[2] = u32::from(flags).into();
1908 }
1909 }
1910 }
1911 Interface::IdGet | Interface::SpmIdGet | Interface::Yield => {}
Balint Dobszay3aad9572025-01-17 16:54:11 +01001912 Interface::Run { target_info } => {
1913 a[1] = u32::from(target_info).into();
1914 }
1915 Interface::NormalWorldResume => {}
Tomás González17b92442025-03-10 16:45:04 +00001916 Interface::SecondaryEpRegister { entrypoint } => match entrypoint {
1917 SecondaryEpRegisterAddr::Addr32(addr) => a[1] = addr as u64,
1918 SecondaryEpRegisterAddr::Addr64(addr) => a[1] = addr,
1919 },
Balint Dobszay3aad9572025-01-17 16:54:11 +01001920 Interface::MsgSend2 {
1921 sender_vm_id,
1922 flags,
1923 } => {
1924 a[1] = sender_vm_id.into();
1925 a[2] = flags.into();
1926 }
1927 Interface::MsgSendDirectReq {
1928 src_id,
1929 dst_id,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001930 args,
1931 } => {
Balint Dobszaye9a3e762025-02-26 17:29:57 +01001932 a[1] = ((src_id as u64) << 16) | dst_id as u64;
Balint Dobszay3aad9572025-01-17 16:54:11 +01001933 match args {
1934 DirectMsgArgs::Args32(args) => {
1935 a[3] = args[0].into();
1936 a[4] = args[1].into();
1937 a[5] = args[2].into();
1938 a[6] = args[3].into();
1939 a[7] = args[4].into();
1940 }
1941 DirectMsgArgs::Args64(args) => {
1942 a[3] = args[0];
1943 a[4] = args[1];
1944 a[5] = args[2];
1945 a[6] = args[3];
1946 a[7] = args[4];
1947 }
Tomás González4d5b0ba2025-03-03 17:15:55 +00001948 DirectMsgArgs::VersionReq { version } => {
1949 a[2] = DirectMsgArgs::VERSION_REQ.into();
1950 a[3] = u32::from(version).into();
1951 }
Tomás González67f92c72025-03-20 16:50:42 +00001952 DirectMsgArgs::PowerPsciReq32 { params } => {
Tomás González4d5b0ba2025-03-03 17:15:55 +00001953 a[2] = DirectMsgArgs::POWER_PSCI_REQ.into();
Tomás González67f92c72025-03-20 16:50:42 +00001954 a[3] = params[0].into();
1955 a[4] = params[1].into();
1956 a[5] = params[2].into();
1957 a[6] = params[3].into();
Tomás González4d5b0ba2025-03-03 17:15:55 +00001958 }
Tomás González67f92c72025-03-20 16:50:42 +00001959 DirectMsgArgs::PowerPsciReq64 { params } => {
Tomás González4d5b0ba2025-03-03 17:15:55 +00001960 a[2] = DirectMsgArgs::POWER_PSCI_REQ.into();
Tomás González67f92c72025-03-20 16:50:42 +00001961 a[3] = params[0];
1962 a[4] = params[1];
1963 a[5] = params[2];
1964 a[6] = params[3];
Tomás González4d5b0ba2025-03-03 17:15:55 +00001965 }
1966 DirectMsgArgs::PowerWarmBootReq { boot_type } => {
1967 a[2] = DirectMsgArgs::POWER_WARM_BOOT_REQ.into();
1968 a[3] = u32::from(boot_type).into();
1969 }
1970 DirectMsgArgs::VmCreated { handle, vm_id } => {
1971 a[2] = DirectMsgArgs::VM_CREATED.into();
1972 let handle_regs: [u32; 2] = handle.into();
1973 a[3] = handle_regs[0].into();
1974 a[4] = handle_regs[1].into();
1975 a[5] = vm_id.into();
1976 }
1977 DirectMsgArgs::VmDestructed { handle, vm_id } => {
1978 a[2] = DirectMsgArgs::VM_DESTRUCTED.into();
1979 let handle_regs: [u32; 2] = handle.into();
1980 a[3] = handle_regs[0].into();
1981 a[4] = handle_regs[1].into();
1982 a[5] = vm_id.into();
1983 }
1984 _ => panic!("Malformed MsgSendDirectReq interface"),
Balint Dobszay3aad9572025-01-17 16:54:11 +01001985 }
1986 }
1987 Interface::MsgSendDirectResp {
1988 src_id,
1989 dst_id,
Balint Dobszay3aad9572025-01-17 16:54:11 +01001990 args,
1991 } => {
Balint Dobszaye9a3e762025-02-26 17:29:57 +01001992 a[1] = ((src_id as u64) << 16) | dst_id as u64;
Balint Dobszay3aad9572025-01-17 16:54:11 +01001993 match args {
1994 DirectMsgArgs::Args32(args) => {
1995 a[3] = args[0].into();
1996 a[4] = args[1].into();
1997 a[5] = args[2].into();
1998 a[6] = args[3].into();
1999 a[7] = args[4].into();
2000 }
2001 DirectMsgArgs::Args64(args) => {
2002 a[3] = args[0];
2003 a[4] = args[1];
2004 a[5] = args[2];
2005 a[6] = args[3];
2006 a[7] = args[4];
2007 }
Tomás González4d5b0ba2025-03-03 17:15:55 +00002008 DirectMsgArgs::VersionResp { version } => {
2009 a[2] = DirectMsgArgs::VERSION_RESP.into();
2010 match version {
Tomás González67f92c72025-03-20 16:50:42 +00002011 None => a[3] = (i32::from(FfaError::NotSupported) as u32).into(),
Tomás González4d5b0ba2025-03-03 17:15:55 +00002012 Some(ver) => a[3] = u32::from(ver).into(),
2013 }
2014 }
2015 DirectMsgArgs::PowerPsciResp { psci_status } => {
2016 a[2] = DirectMsgArgs::POWER_PSCI_RESP.into();
2017 a[3] = psci_status as u64;
2018 }
2019 DirectMsgArgs::VmCreatedAck { sp_status } => {
2020 a[2] = DirectMsgArgs::VM_CREATED_ACK.into();
Tomás González67f92c72025-03-20 16:50:42 +00002021 a[3] = (i32::from(sp_status) as u32).into();
Tomás González4d5b0ba2025-03-03 17:15:55 +00002022 }
2023 DirectMsgArgs::VmDestructedAck { sp_status } => {
2024 a[2] = DirectMsgArgs::VM_DESTRUCTED_ACK.into();
Tomás González67f92c72025-03-20 16:50:42 +00002025 a[3] = (i32::from(sp_status) as u32).into();
Tomás González4d5b0ba2025-03-03 17:15:55 +00002026 }
2027 _ => panic!("Malformed MsgSendDirectResp interface"),
Balint Dobszay3aad9572025-01-17 16:54:11 +01002028 }
2029 }
2030 Interface::MemDonate {
2031 total_len,
2032 frag_len,
2033 buf,
2034 } => {
2035 a[1] = total_len.into();
2036 a[2] = frag_len.into();
2037 (a[3], a[4]) = match buf {
2038 Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()),
2039 Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()),
2040 None => (0, 0),
2041 };
2042 }
2043 Interface::MemLend {
2044 total_len,
2045 frag_len,
2046 buf,
2047 } => {
2048 a[1] = total_len.into();
2049 a[2] = frag_len.into();
2050 (a[3], a[4]) = match buf {
2051 Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()),
2052 Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()),
2053 None => (0, 0),
2054 };
2055 }
2056 Interface::MemShare {
2057 total_len,
2058 frag_len,
2059 buf,
2060 } => {
2061 a[1] = total_len.into();
2062 a[2] = frag_len.into();
2063 (a[3], a[4]) = match buf {
2064 Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()),
2065 Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()),
2066 None => (0, 0),
2067 };
2068 }
2069 Interface::MemRetrieveReq {
2070 total_len,
2071 frag_len,
2072 buf,
2073 } => {
2074 a[1] = total_len.into();
2075 a[2] = frag_len.into();
2076 (a[3], a[4]) = match buf {
2077 Some(MemOpBuf::Buf32 { addr, page_cnt }) => (addr.into(), page_cnt.into()),
2078 Some(MemOpBuf::Buf64 { addr, page_cnt }) => (addr, page_cnt.into()),
2079 None => (0, 0),
2080 };
2081 }
2082 Interface::MemRetrieveResp {
2083 total_len,
2084 frag_len,
2085 } => {
2086 a[1] = total_len.into();
2087 a[2] = frag_len.into();
2088 }
2089 Interface::MemRelinquish => {}
2090 Interface::MemReclaim { handle, flags } => {
2091 let handle_regs: [u32; 2] = handle.into();
2092 a[1] = handle_regs[0].into();
2093 a[2] = handle_regs[1].into();
2094 a[3] = flags.into();
2095 }
Balint Dobszayde0dc802025-02-28 14:16:52 +01002096 Interface::MemPermGet { addr, page_cnt } => {
Balint Dobszay3aad9572025-01-17 16:54:11 +01002097 a[1] = match addr {
2098 MemAddr::Addr32(addr) => addr.into(),
2099 MemAddr::Addr64(addr) => addr,
2100 };
Balint Dobszayde0dc802025-02-28 14:16:52 +01002101 a[2] = if version >= Version(1, 3) {
2102 page_cnt.unwrap().into()
2103 } else {
2104 assert!(page_cnt.is_none());
2105 0
2106 }
Balint Dobszay3aad9572025-01-17 16:54:11 +01002107 }
2108 Interface::MemPermSet {
2109 addr,
2110 page_cnt,
2111 mem_perm,
2112 } => {
2113 a[1] = match addr {
2114 MemAddr::Addr32(addr) => addr.into(),
2115 MemAddr::Addr64(addr) => addr,
2116 };
2117 a[2] = page_cnt.into();
2118 a[3] = mem_perm.into();
2119 }
2120 Interface::ConsoleLog {
2121 char_cnt,
2122 char_lists,
2123 } => {
2124 a[1] = char_cnt.into();
2125 match char_lists {
2126 ConsoleLogChars::Reg32(regs) => {
2127 a[2] = regs[0].into();
2128 a[3] = regs[1].into();
2129 a[4] = regs[2].into();
2130 a[5] = regs[3].into();
2131 a[6] = regs[4].into();
2132 a[7] = regs[5].into();
2133 }
Balint Dobszayde0dc802025-02-28 14:16:52 +01002134 _ => panic!("{:#x?} requires 18 registers", char_lists),
Balint Dobszay3aad9572025-01-17 16:54:11 +01002135 }
2136 }
Tomás González7ffb6132025-04-03 12:28:58 +01002137 Interface::NotificationBitmapCreate { vm_id, vcpu_cnt } => {
2138 a[1] = vm_id.into();
2139 a[2] = vcpu_cnt.into();
2140 }
2141 Interface::NotificationBitmapDestroy { vm_id } => {
2142 a[1] = vm_id.into();
2143 }
2144 Interface::NotificationBind {
2145 sender_id,
2146 receiver_id,
2147 flags,
2148 bitmap,
2149 } => {
2150 a[1] = (u64::from(sender_id) << 16) | u64::from(receiver_id);
2151 a[2] = u32::from(flags).into();
2152 a[3] = bitmap & 0xffff_ffff;
2153 a[4] = bitmap >> 32;
2154 }
2155 Interface::NotificationUnBind {
2156 sender_id,
2157 receiver_id,
2158 bitmap,
2159 } => {
2160 a[1] = (u64::from(sender_id) << 16) | u64::from(receiver_id);
2161 a[3] = bitmap & 0xffff_ffff;
2162 a[4] = bitmap >> 32;
2163 }
2164 Interface::NotificationSet {
2165 sender_id,
2166 receiver_id,
2167 flags,
2168 bitmap,
2169 } => {
2170 a[1] = (u64::from(sender_id) << 16) | u64::from(receiver_id);
2171 a[2] = u32::from(flags).into();
2172 a[3] = bitmap & 0xffff_ffff;
2173 a[4] = bitmap >> 32;
2174 }
2175 Interface::NotificationGet {
2176 vcpu_id,
2177 endpoint_id,
2178 flags,
2179 } => {
2180 a[1] = (u64::from(vcpu_id) << 16) | u64::from(endpoint_id);
2181 a[2] = u32::from(flags).into();
2182 }
2183 Interface::NotificationInfoGet { .. } => {}
Tomás Gonzáleze6fe75f2025-04-04 09:46:50 +01002184 Interface::El3IntrHandle => {}
Balint Dobszayde0dc802025-02-28 14:16:52 +01002185 _ => panic!("{:#x?} requires 18 registers", self),
2186 }
2187 }
2188
2189 fn pack_regs18(&self, version: Version, a: &mut [u64; 18]) {
2190 assert!(version >= Version(1, 2));
2191
Balint Dobszayde0dc802025-02-28 14:16:52 +01002192 if let Some(function_id) = self.function_id() {
2193 a[0] = function_id as u64;
2194 }
2195
2196 match *self {
2197 Interface::Success { target_info, args } => {
2198 a[1] = target_info.into();
2199 match args {
Imre Kis54773b62025-04-10 13:47:39 +02002200 SuccessArgs::Args64_2(regs) => a[2..18].copy_from_slice(&regs[..16]),
Balint Dobszayde0dc802025-02-28 14:16:52 +01002201 _ => panic!("{:#x?} requires 8 registers", args),
2202 }
2203 }
2204 Interface::MsgSendDirectReq2 {
2205 src_id,
2206 dst_id,
2207 uuid,
2208 args,
2209 } => {
2210 a[1] = ((src_id as u64) << 16) | dst_id as u64;
Tomás Gonzálezce3bc222025-03-25 14:30:42 +00002211 let (uuid_msb, uuid_lsb) = uuid.as_u64_pair();
2212 (a[2], a[3]) = (uuid_msb.swap_bytes(), uuid_lsb.swap_bytes());
Balint Dobszayde0dc802025-02-28 14:16:52 +01002213 a[4..18].copy_from_slice(&args.0[..14]);
2214 }
2215 Interface::MsgSendDirectResp2 {
2216 src_id,
2217 dst_id,
2218 args,
2219 } => {
2220 a[1] = ((src_id as u64) << 16) | dst_id as u64;
2221 a[2] = 0;
2222 a[3] = 0;
2223 a[4..18].copy_from_slice(&args.0[..14]);
2224 }
2225 Interface::ConsoleLog {
2226 char_cnt,
2227 char_lists,
2228 } => {
2229 a[1] = char_cnt.into();
2230 match char_lists {
2231 ConsoleLogChars::Reg64(regs) => a[2..18].copy_from_slice(&regs[..16]),
2232 _ => panic!("{:#x?} requires 8 registers", char_lists),
2233 }
2234 }
Tomás González0a058bc2025-03-11 11:20:55 +00002235 Interface::PartitionInfoGetRegs {
2236 uuid,
2237 start_index,
2238 info_tag,
2239 } => {
2240 if start_index == 0 && info_tag != 0 {
2241 panic!("Information Tag MBZ if start index is 0: {:#x?}", self);
2242 }
2243 let (uuid_msb, uuid_lsb) = uuid.as_u64_pair();
2244 (a[1], a[2]) = (uuid_msb.swap_bytes(), uuid_lsb.swap_bytes());
2245 a[3] = (u64::from(info_tag) << 16) | u64::from(start_index);
2246 }
Balint Dobszayde0dc802025-02-28 14:16:52 +01002247 _ => panic!("{:#x?} requires 8 registers", self),
Balint Dobszay3aad9572025-01-17 16:54:11 +01002248 }
2249 }
2250
Balint Dobszaya5846852025-02-26 15:38:53 +01002251 /// Helper function to create an `FFA_SUCCESS` interface without any arguments.
Balint Dobszay3aad9572025-01-17 16:54:11 +01002252 pub fn success32_noargs() -> Self {
2253 Self::Success {
2254 target_info: 0,
Imre Kis54773b62025-04-10 13:47:39 +02002255 args: SuccessArgs::Args32([0; 6]),
Balint Dobszay3aad9572025-01-17 16:54:11 +01002256 }
2257 }
2258
Tomás González4c8c7d22025-03-10 17:14:57 +00002259 /// Helper function to create an `FFA_SUCCESS` interface without any arguments.
2260 pub fn success64_noargs() -> Self {
2261 Self::Success {
2262 target_info: 0,
Imre Kis54773b62025-04-10 13:47:39 +02002263 args: SuccessArgs::Args64([0; 6]),
Tomás González4c8c7d22025-03-10 17:14:57 +00002264 }
2265 }
2266
Balint Dobszaya5846852025-02-26 15:38:53 +01002267 /// Helper function to create an `FFA_ERROR` interface with an error code.
Balint Dobszay3aad9572025-01-17 16:54:11 +01002268 pub fn error(error_code: FfaError) -> Self {
2269 Self::Error {
2270 target_info: TargetInfo {
2271 endpoint_id: 0,
2272 vcpu_id: 0,
2273 },
2274 error_code,
Balint Dobszayb727aab2025-04-07 10:24:59 +02002275 error_arg: 0,
Balint Dobszay3aad9572025-01-17 16:54:11 +01002276 }
2277 }
2278}
2279
Balint Dobszaya5846852025-02-26 15:38:53 +01002280/// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG32` message.
2281pub const CONSOLE_LOG_32_MAX_CHAR_CNT: u8 = 24;
Balint Dobszayde0dc802025-02-28 14:16:52 +01002282/// Maximum number of characters transmitted in a single `FFA_CONSOLE_LOG64` message.
2283pub const CONSOLE_LOG_64_MAX_CHAR_CNT: u8 = 128;
Balint Dobszay3aad9572025-01-17 16:54:11 +01002284
Balint Dobszaya5846852025-02-26 15:38:53 +01002285/// Helper function to convert the "Tightly packed list of characters" format used by the
2286/// `FFA_CONSOLE_LOG` interface into a byte slice.
Balint Dobszay3aad9572025-01-17 16:54:11 +01002287pub fn parse_console_log(
2288 char_cnt: u8,
2289 char_lists: &ConsoleLogChars,
2290 log_bytes: &mut [u8],
2291) -> Result<(), FfaError> {
2292 match char_lists {
2293 ConsoleLogChars::Reg32(regs) => {
Balint Dobszaya5846852025-02-26 15:38:53 +01002294 if !(1..=CONSOLE_LOG_32_MAX_CHAR_CNT).contains(&char_cnt) {
Balint Dobszay3aad9572025-01-17 16:54:11 +01002295 return Err(FfaError::InvalidParameters);
2296 }
2297 for (i, reg) in regs.iter().enumerate() {
2298 log_bytes[4 * i..4 * (i + 1)].copy_from_slice(&reg.to_le_bytes());
2299 }
2300 }
2301 ConsoleLogChars::Reg64(regs) => {
Balint Dobszaya5846852025-02-26 15:38:53 +01002302 if !(1..=CONSOLE_LOG_64_MAX_CHAR_CNT).contains(&char_cnt) {
Balint Dobszay3aad9572025-01-17 16:54:11 +01002303 return Err(FfaError::InvalidParameters);
2304 }
2305 for (i, reg) in regs.iter().enumerate() {
2306 log_bytes[8 * i..8 * (i + 1)].copy_from_slice(&reg.to_le_bytes());
2307 }
2308 }
Balint Dobszay5bf492f2024-07-29 17:21:32 +02002309 }
2310
Balint Dobszayc8802492025-01-15 18:11:39 +01002311 Ok(())
Balint Dobszay5bf492f2024-07-29 17:21:32 +02002312}
Tomás González0a058bc2025-03-11 11:20:55 +00002313
2314#[cfg(test)]
2315mod tests {
2316 use super::*;
2317
2318 #[test]
2319 fn part_info_get_regs() {
2320 let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8").unwrap();
2321 let uuid_bytes = uuid.as_bytes();
2322 let test_info_tag = 0b1101_1101;
2323 let test_start_index = 0b1101;
2324 let start_index_and_tag = (test_info_tag << 16) | test_start_index;
2325 let version = Version(1, 2);
2326
2327 // From spec:
2328 // Bytes[0...7] of UUID with byte 0 in the low-order bits.
2329 let reg_x1 = (uuid_bytes[7] as u64) << 56
2330 | (uuid_bytes[6] as u64) << 48
2331 | (uuid_bytes[5] as u64) << 40
2332 | (uuid_bytes[4] as u64) << 32
2333 | (uuid_bytes[3] as u64) << 24
2334 | (uuid_bytes[2] as u64) << 16
2335 | (uuid_bytes[1] as u64) << 8
2336 | (uuid_bytes[0] as u64);
2337
2338 // From spec:
2339 // Bytes[8...15] of UUID with byte 8 in the low-order bits.
2340 let reg_x2 = (uuid_bytes[15] as u64) << 56
2341 | (uuid_bytes[14] as u64) << 48
2342 | (uuid_bytes[13] as u64) << 40
2343 | (uuid_bytes[12] as u64) << 32
2344 | (uuid_bytes[11] as u64) << 24
2345 | (uuid_bytes[10] as u64) << 16
2346 | (uuid_bytes[9] as u64) << 8
2347 | (uuid_bytes[8] as u64);
2348
2349 // First, test for wrong tag:
2350 {
2351 let mut regs = [0u64; 18];
2352 regs[0] = FuncId::PartitionInfoGetRegs as u64;
2353 regs[1] = reg_x1;
2354 regs[2] = reg_x2;
2355 regs[3] = test_info_tag << 16;
2356
2357 assert!(Interface::from_regs(version, &regs).is_err_and(
2358 |e| e == Error::InvalidInformationTag(test_info_tag.try_into().unwrap())
2359 ));
2360 }
2361
2362 // Test for regs -> Interface -> regs
2363 {
2364 let mut orig_regs = [0u64; 18];
2365 orig_regs[0] = FuncId::PartitionInfoGetRegs as u64;
2366 orig_regs[1] = reg_x1;
2367 orig_regs[2] = reg_x2;
2368 orig_regs[3] = start_index_and_tag;
2369
2370 let mut test_regs = orig_regs.clone();
2371 let interface = Interface::from_regs(version, &mut test_regs).unwrap();
2372 match &interface {
2373 Interface::PartitionInfoGetRegs {
2374 info_tag,
2375 start_index,
2376 uuid: int_uuid,
2377 } => {
2378 assert_eq!(u64::from(*info_tag), test_info_tag);
2379 assert_eq!(u64::from(*start_index), test_start_index);
2380 assert_eq!(*int_uuid, uuid);
2381 }
2382 _ => panic!("Expecting Interface::PartitionInfoGetRegs!"),
2383 }
2384 test_regs.fill(0);
2385 interface.to_regs(version, &mut test_regs);
2386 assert_eq!(orig_regs, test_regs);
2387 }
2388
2389 // Test for Interface -> regs -> Interface
2390 {
2391 let interface = Interface::PartitionInfoGetRegs {
2392 info_tag: test_info_tag.try_into().unwrap(),
2393 start_index: test_start_index.try_into().unwrap(),
2394 uuid,
2395 };
2396
2397 let mut regs: [u64; 18] = [0; 18];
2398 interface.to_regs(version, &mut regs);
2399
2400 assert_eq!(Some(FuncId::PartitionInfoGetRegs), interface.function_id());
2401 assert_eq!(regs[0], interface.function_id().unwrap() as u64);
2402 assert_eq!(regs[1], reg_x1);
2403 assert_eq!(regs[2], reg_x2);
2404 assert_eq!(regs[3], (test_info_tag << 16) | test_start_index);
2405
2406 assert_eq!(Interface::from_regs(version, &regs).unwrap(), interface);
2407 }
2408 }
Tomás Gonzálezce3bc222025-03-25 14:30:42 +00002409
2410 #[test]
2411 fn msg_send_direct_req2() {
2412 let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8").unwrap();
2413 let uuid_bytes = uuid.as_bytes();
2414
2415 // From spec:
2416 // Bytes[0...7] of UUID with byte 0 in the low-order bits.
2417 let reg_x2 = (uuid_bytes[7] as u64) << 56
2418 | (uuid_bytes[6] as u64) << 48
2419 | (uuid_bytes[5] as u64) << 40
2420 | (uuid_bytes[4] as u64) << 32
2421 | (uuid_bytes[3] as u64) << 24
2422 | (uuid_bytes[2] as u64) << 16
2423 | (uuid_bytes[1] as u64) << 8
2424 | (uuid_bytes[0] as u64);
2425
2426 // From spec:
2427 // Bytes[8...15] of UUID with byte 8 in the low-order bits.
2428 let reg_x3 = (uuid_bytes[15] as u64) << 56
2429 | (uuid_bytes[14] as u64) << 48
2430 | (uuid_bytes[13] as u64) << 40
2431 | (uuid_bytes[12] as u64) << 32
2432 | (uuid_bytes[11] as u64) << 24
2433 | (uuid_bytes[10] as u64) << 16
2434 | (uuid_bytes[9] as u64) << 8
2435 | (uuid_bytes[8] as u64);
2436
2437 let test_sender = 0b1101_1101;
2438 let test_receiver = 0b1101;
2439 let test_sender_receiver = (test_sender << 16) | test_receiver;
2440 let version = Version(1, 2);
2441
2442 // Test for regs -> Interface -> regs
2443 {
2444 let mut orig_regs = [0u64; 18];
2445 orig_regs[0] = FuncId::MsgSendDirectReq64_2 as u64;
2446 orig_regs[1] = test_sender_receiver;
2447 orig_regs[2] = reg_x2;
2448 orig_regs[3] = reg_x3;
2449
2450 let mut test_regs = orig_regs.clone();
2451 let interface = Interface::from_regs(version, &mut test_regs).unwrap();
2452 match &interface {
2453 Interface::MsgSendDirectReq2 {
2454 dst_id,
2455 src_id,
2456 args: _,
2457 uuid: int_uuid,
2458 } => {
2459 assert_eq!(u64::from(*src_id), test_sender);
2460 assert_eq!(u64::from(*dst_id), test_receiver);
2461 assert_eq!(*int_uuid, uuid);
2462 }
2463 _ => panic!("Expecting Interface::MsgSendDirectReq2!"),
2464 }
2465 test_regs.fill(0);
2466 interface.to_regs(version, &mut test_regs);
2467 assert_eq!(orig_regs, test_regs);
2468 }
2469
2470 // Test for Interface -> regs -> Interface
2471 {
2472 let rest_of_regs: [u64; 14] = [0; 14];
2473
2474 let interface = Interface::MsgSendDirectReq2 {
2475 src_id: test_sender.try_into().unwrap(),
2476 dst_id: test_receiver.try_into().unwrap(),
2477 uuid,
2478 args: DirectMsg2Args(rest_of_regs),
2479 };
2480
2481 let mut regs: [u64; 18] = [0; 18];
2482 interface.to_regs(version, &mut regs);
2483
2484 assert_eq!(Some(FuncId::MsgSendDirectReq64_2), interface.function_id());
2485 assert_eq!(regs[0], interface.function_id().unwrap() as u64);
2486 assert_eq!(regs[1], test_sender_receiver);
2487 assert_eq!(regs[2], reg_x2);
2488 assert_eq!(regs[3], reg_x3);
2489 assert_eq!(regs[4], 0);
2490
2491 assert_eq!(Interface::from_regs(version, &regs).unwrap(), interface);
2492 }
2493 }
Tomás González6ccba0a2025-04-09 13:31:29 +01002494
2495 #[test]
2496 fn is_32bit() {
2497 let interface_64 = Interface::MsgSendDirectReq {
2498 src_id: 0,
2499 dst_id: 1,
2500 args: DirectMsgArgs::Args64([0, 0, 0, 0, 0]),
2501 };
2502 assert!(!interface_64.is_32bit());
2503
2504 let interface_32 = Interface::MsgSendDirectReq {
2505 src_id: 0,
2506 dst_id: 1,
2507 args: DirectMsgArgs::Args32([0, 0, 0, 0, 0]),
2508 };
2509 assert!(interface_32.is_32bit());
2510 }
Imre Kis787c5002025-04-10 14:25:51 +02002511
2512 #[test]
2513 fn success_args_notification_info_get32() {
2514 let mut notifications = SuccessArgsNotificationInfoGet32::default();
2515
2516 // 16.7.1.1 Example usage
2517 notifications.add_list(0x0000, &[0, 2, 3]).unwrap();
2518 notifications.add_list(0x0000, &[4, 6]).unwrap();
2519 notifications.add_list(0x0002, &[]).unwrap();
2520 notifications.add_list(0x0003, &[1]).unwrap();
2521
2522 let args: SuccessArgs = notifications.into();
2523 assert_eq!(
2524 SuccessArgs::Args32([
2525 0x0004_b200,
2526 0x0000_0000,
2527 0x0003_0002,
2528 0x0004_0000,
2529 0x0002_0006,
2530 0x0001_0003
2531 ]),
2532 args
2533 );
2534
2535 let notifications = SuccessArgsNotificationInfoGet32::try_from(args).unwrap();
2536 let mut iter = notifications.iter();
2537 assert_eq!(Some((0x0000, &[0, 2, 3][..])), iter.next());
2538 assert_eq!(Some((0x0000, &[4, 6][..])), iter.next());
2539 assert_eq!(Some((0x0002, &[][..])), iter.next());
2540 assert_eq!(Some((0x0003, &[1][..])), iter.next());
2541 }
2542
2543 #[test]
2544 fn success_args_notification_info_get64() {
2545 let mut notifications = SuccessArgsNotificationInfoGet64::default();
2546
2547 // 16.7.1.1 Example usage
2548 notifications.add_list(0x0000, &[0, 2, 3]).unwrap();
2549 notifications.add_list(0x0000, &[4, 6]).unwrap();
2550 notifications.add_list(0x0002, &[]).unwrap();
2551 notifications.add_list(0x0003, &[1]).unwrap();
2552
2553 let args: SuccessArgs = notifications.into();
2554 assert_eq!(
2555 SuccessArgs::Args64([
2556 0x0004_b200,
2557 0x0003_0002_0000_0000,
2558 0x0002_0006_0004_0000,
2559 0x0000_0000_0001_0003,
2560 0x0000_0000_0000_0000,
2561 0x0000_0000_0000_0000,
2562 ]),
2563 args
2564 );
2565
2566 let notifications = SuccessArgsNotificationInfoGet64::try_from(args).unwrap();
2567 let mut iter = notifications.iter();
2568 assert_eq!(Some((0x0000, &[0, 2, 3][..])), iter.next());
2569 assert_eq!(Some((0x0000, &[4, 6][..])), iter.next());
2570 assert_eq!(Some((0x0002, &[][..])), iter.next());
2571 assert_eq!(Some((0x0003, &[1][..])), iter.next());
2572 }
Tomás González0a058bc2025-03-11 11:20:55 +00002573}