blob: caeaa9e0bc4d7f0e1221c17b8df781eee6121dc0 [file] [log] [blame]
J-Alves7581c382020-05-07 18:34:20 +01001/*
Max Shvetsov103e0562021-02-04 16:58:31 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
J-Alves7581c382020-05-07 18:34:20 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef FFA_HELPERS_H
8#define FFA_HELPERS_H
9
J-Alves8f08a052020-05-26 17:14:40 +010010#include <ffa_svc.h>
J-Alves7581c382020-05-07 18:34:20 +010011#include <tftf_lib.h>
12#include <utils_def.h>
13
14/* This error code must be different to the ones used by FFA */
15#define FFA_TFTF_ERROR -42
16
J-Alves5aecd982020-06-11 10:25:33 +010017typedef unsigned short ffa_vm_id_t;
18typedef unsigned short ffa_vm_count_t;
19typedef unsigned short ffa_vcpu_count_t;
Manish Pandey6b3840a2020-09-15 22:31:58 +010020typedef uint32_t ffa_int_id_t;
J-Alvesf3a393c2020-10-23 16:00:39 +010021typedef uint64_t ffa_memory_handle_t;
22/** Flags to indicate properties of receivers during memory region retrieval. */
23typedef uint8_t ffa_memory_receiver_flags_t;
J-Alves5aecd982020-06-11 10:25:33 +010024
J-Alvesd708c032020-11-19 12:14:21 +000025struct ffa_uuid {
26 const uint32_t uuid[4];
27};
28
J-Alves7581c382020-05-07 18:34:20 +010029#ifndef __ASSEMBLY__
30
31#include <stdint.h>
32
Max Shvetsovc32f4782020-06-23 09:41:15 +010033struct ffa_partition_info {
34 /** The ID of the VM the information is about */
35 ffa_vm_id_t id;
36 /** The number of execution contexts implemented by the partition */
37 uint16_t exec_context;
38 /** The Partition's properties, e.g. supported messaging methods */
39 uint32_t properties;
40};
41
J-Alvesf3a393c2020-10-23 16:00:39 +010042enum ffa_data_access {
43 FFA_DATA_ACCESS_NOT_SPECIFIED,
44 FFA_DATA_ACCESS_RO,
45 FFA_DATA_ACCESS_RW,
46 FFA_DATA_ACCESS_RESERVED,
47};
48
49enum ffa_instruction_access {
50 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
51 FFA_INSTRUCTION_ACCESS_NX,
52 FFA_INSTRUCTION_ACCESS_X,
53 FFA_INSTRUCTION_ACCESS_RESERVED,
54};
55
56enum ffa_memory_type {
57 FFA_MEMORY_NOT_SPECIFIED_MEM,
58 FFA_MEMORY_DEVICE_MEM,
59 FFA_MEMORY_NORMAL_MEM,
60};
61
62enum ffa_memory_cacheability {
63 FFA_MEMORY_CACHE_RESERVED = 0x0,
64 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
65 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
66 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
67 FFA_MEMORY_DEV_NGNRNE = 0x0,
68 FFA_MEMORY_DEV_NGNRE = 0x1,
69 FFA_MEMORY_DEV_NGRE = 0x2,
70 FFA_MEMORY_DEV_GRE = 0x3,
71};
72
73enum ffa_memory_shareability {
74 FFA_MEMORY_SHARE_NON_SHAREABLE,
75 FFA_MEMORY_SHARE_RESERVED,
76 FFA_MEMORY_OUTER_SHAREABLE,
77 FFA_MEMORY_INNER_SHAREABLE,
78};
79
80typedef uint8_t ffa_memory_access_permissions_t;
81
82/**
83 * This corresponds to table "Memory region attributes descriptor" of the FF-A
84 * 1.0 specification.
85 */
86typedef uint8_t ffa_memory_attributes_t;
87
88#define FFA_DATA_ACCESS_OFFSET (0x0U)
89#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
90
91#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
92#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
93
94#define FFA_MEMORY_TYPE_OFFSET (0x4U)
95#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
96
97#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
98#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
99
100#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
101#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
102
103#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
104 static inline void ffa_set_##name##_attr(container_type *attr, \
105 const enum ffa_##name perm) \
106 { \
107 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
108 }
109
110#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
111 static inline enum ffa_##name ffa_get_##name##_attr( \
112 container_type attr) \
113 { \
114 return (enum ffa_##name)((attr & mask) >> offset); \
115 }
116
117ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
118 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
119ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
120 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
121
122ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
123 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
124ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
125 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
126
127ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
128 FFA_MEMORY_TYPE_MASK)
129ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
130 FFA_MEMORY_TYPE_MASK)
131
132ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
133 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
134ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
135 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
136
137ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
138 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
139ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
140 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
141
142#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
143 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
144#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
145 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
146#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
147
148/**
149 * A set of contiguous pages which is part of a memory region. This corresponds
150 * to table "Constituent memory region descriptor" of the FFA 1.0 specification.
151 */
152struct ffa_memory_region_constituent {
153 /**
154 * The base IPA of the constituent memory region, aligned to 4 kiB page
155 * size granularity.
156 */
157 void *address;
158 /** The number of 4 kiB pages in the constituent memory region. */
159 uint32_t page_count;
160 /** Reserved field, must be 0. */
161 uint32_t reserved;
162};
163
164/**
165 * A set of pages comprising a memory region. This corresponds to table
166 * "Composite memory region descriptor" of the FFA 1.0 specification.
167 */
168struct ffa_composite_memory_region {
169 /**
170 * The total number of 4 kiB pages included in this memory region. This
171 * must be equal to the sum of page counts specified in each
172 * `ffa_memory_region_constituent`.
173 */
174 uint32_t page_count;
175 /**
176 * The number of constituents (`ffa_memory_region_constituent`)
177 * included in this memory region range.
178 */
179 uint32_t constituent_count;
180 /** Reserved field, must be 0. */
181 uint64_t reserved_0;
182 /** An array of `constituent_count` memory region constituents. */
183 struct ffa_memory_region_constituent constituents[];
184};
185
186/**
187 * This corresponds to table "Memory access permissions descriptor" of the FFA
188 * 1.0 specification.
189 */
190struct ffa_memory_region_attributes {
191 /** The ID of the VM to which the memory is being given or shared. */
192 ffa_vm_id_t receiver;
193 /**
194 * The permissions with which the memory region should be mapped in the
195 * receiver's page table.
196 */
197 ffa_memory_access_permissions_t permissions;
198 /**
199 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
200 * for memory regions with multiple borrowers.
201 */
202 ffa_memory_receiver_flags_t flags;
203};
204
205/** Flags to control the behaviour of a memory sharing transaction. */
206typedef uint32_t ffa_memory_region_flags_t;
207
208/**
209 * Clear memory region contents after unmapping it from the sender and before
210 * mapping it for any receiver.
211 */
212#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1U
213
214/**
215 * Whether the hypervisor may time slice the memory sharing or retrieval
216 * operation.
217 */
218#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2U
219
220/**
221 * Whether the hypervisor should clear the memory region after the receiver
222 * relinquishes it or is aborted.
223 */
224#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4U
225
226#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
227#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
228#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
229#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
230#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
231
J-Alves0435cae2020-11-06 10:49:56 +0000232/** The maximum number of recipients a memory region may be sent to. */
233#define MAX_MEM_SHARE_RECIPIENTS 1U
234
J-Alvesf3a393c2020-10-23 16:00:39 +0100235/**
236 * This corresponds to table "Endpoint memory access descriptor" of the FFA 1.0
237 * specification.
238 */
239struct ffa_memory_access {
240 struct ffa_memory_region_attributes receiver_permissions;
241 /**
242 * Offset in bytes from the start of the outer `ffa_memory_region` to
243 * an `ffa_composite_memory_region` struct.
244 */
245 uint32_t composite_memory_region_offset;
246 uint64_t reserved_0;
247};
248
249/**
250 * Information about a set of pages which are being shared. This corresponds to
251 * table "Lend, donate or share memory transaction descriptor" of the FFA
252 * 1.0 specification. Note that it is also used for retrieve requests and
253 * responses.
254 */
255struct ffa_memory_region {
256 /**
257 * The ID of the VM which originally sent the memory region, i.e. the
258 * owner.
259 */
260 ffa_vm_id_t sender;
261 ffa_memory_attributes_t attributes;
262 /** Reserved field, must be 0. */
263 uint8_t reserved_0;
264 /** Flags to control behaviour of the transaction. */
265 ffa_memory_region_flags_t flags;
266 ffa_memory_handle_t handle;
267 /**
268 * An implementation defined value associated with the receiver and the
269 * memory region.
270 */
271 uint64_t tag;
272 /** Reserved field, must be 0. */
273 uint32_t reserved_1;
274 /**
275 * The number of `ffa_memory_access` entries included in this
276 * transaction.
277 */
278 uint32_t receiver_count;
279 /**
280 * An array of `attribute_count` endpoint memory access descriptors.
281 * Each one specifies a memory region offset, an endpoint and the
282 * attributes with which this memory region should be mapped in that
283 * endpoint's page table.
284 */
285 struct ffa_memory_access receivers[];
286};
287
288/**
289 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
290 * "Descriptor to relinquish a memory region" of the FFA 1.0 specification.
291 */
292struct ffa_mem_relinquish {
293 ffa_memory_handle_t handle;
294 ffa_memory_region_flags_t flags;
295 uint32_t endpoint_count;
296 ffa_vm_id_t endpoints[];
297};
298
299static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t h1, uint32_t h2)
300{
301 return (uint64_t)h1 | (uint64_t)h2 << 32;
302}
303
304static inline ffa_memory_handle_t ffa_mem_success_handle(smc_ret_values r)
305{
306 return ffa_assemble_handle(r.ret2, r.ret3);
307}
308
309/**
310 * Gets the `ffa_composite_memory_region` for the given receiver from an
311 * `ffa_memory_region`, or NULL if it is not valid.
312 */
313static inline struct ffa_composite_memory_region *
314ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
315 uint32_t receiver_index)
316{
317 uint32_t offset = memory_region->receivers[receiver_index]
318 .composite_memory_region_offset;
319
320 if (offset == 0) {
321 return NULL;
322 }
323
324 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
325 offset);
326}
327
328static inline uint32_t ffa_mem_relinquish_init(
329 struct ffa_mem_relinquish *relinquish_request,
330 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
331 ffa_vm_id_t sender)
332{
333 relinquish_request->handle = handle;
334 relinquish_request->flags = flags;
335 relinquish_request->endpoint_count = 1;
336 relinquish_request->endpoints[0] = sender;
337 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t);
338}
339
340uint32_t ffa_memory_retrieve_request_init(
341 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
342 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
343 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
344 enum ffa_instruction_access instruction_access,
345 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
346 enum ffa_memory_shareability shareability);
347
348uint32_t ffa_memory_region_init(
349 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
350 ffa_vm_id_t sender, ffa_vm_id_t receiver,
351 const struct ffa_memory_region_constituent constituents[],
352 uint32_t constituent_count, uint32_t tag,
353 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
354 enum ffa_instruction_access instruction_access,
355 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
356 enum ffa_memory_shareability shareability, uint32_t *total_length,
357 uint32_t *fragment_length);
358
J-Alves3106b072020-11-18 10:37:21 +0000359ffa_memory_handle_t ffa_memory_send(
360 struct ffa_memory_region *memory_region, uint32_t mem_func,
361 uint32_t fragment_length, uint32_t total_length);
362
363ffa_memory_handle_t ffa_memory_init_and_send(
364 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
365 ffa_vm_id_t sender, ffa_vm_id_t receiver,
366 const struct ffa_memory_region_constituent* constituents,
367 uint32_t constituents_count, uint32_t mem_func);
368
J-Alves7581c382020-05-07 18:34:20 +0100369smc_ret_values ffa_msg_send_direct_req(uint32_t source_id, uint32_t dest_id, uint32_t message);
370smc_ret_values ffa_msg_send_direct_req64(uint32_t source_id, uint32_t dest_id, uint64_t message);
J-Alvesd1aae292020-10-08 17:16:58 +0100371smc_ret_values ffa_msg_send_direct_req64_5args(uint32_t source_id, uint32_t dest_id,
372 uint64_t arg0, uint64_t arg1,
373 uint64_t arg2, uint64_t arg3,
374 uint64_t arg4);
375
J-Alves7581c382020-05-07 18:34:20 +0100376smc_ret_values ffa_run(uint32_t dest_id, uint32_t vcpu_id);
J-Alves8f08a052020-05-26 17:14:40 +0100377smc_ret_values ffa_version(uint32_t input_version);
J-Alves5aecd982020-06-11 10:25:33 +0100378smc_ret_values ffa_id_get(void);
379smc_ret_values ffa_msg_wait(void);
380smc_ret_values ffa_msg_send_direct_resp(ffa_vm_id_t source_id,
381 ffa_vm_id_t dest_id, uint32_t message);
382smc_ret_values ffa_error(int32_t error_code);
Max Shvetsovc17c1d32020-06-11 15:03:01 +0100383smc_ret_values ffa_features(uint32_t feature);
Max Shvetsovc32f4782020-06-23 09:41:15 +0100384smc_ret_values ffa_partition_info_get(const uint32_t uuid[4]);
385smc_ret_values ffa_rx_release(void);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100386smc_ret_values ffa_rxtx_map(uintptr_t send, uintptr_t recv, uint32_t pages);
J-Alves7581c382020-05-07 18:34:20 +0100387
J-Alves3ea46d12020-09-09 11:13:05 +0100388smc_ret_values ffa_mem_donate(uint32_t descriptor_length,
389 uint32_t fragment_length);
390smc_ret_values ffa_mem_lend(uint32_t descriptor_length,
391 uint32_t fragment_length);
392smc_ret_values ffa_mem_share(uint32_t descriptor_length,
393 uint32_t fragment_length);
394smc_ret_values ffa_mem_retrieve_req(uint32_t descriptor_length,
395 uint32_t fragment_length);
396smc_ret_values ffa_mem_relinquish(void);
397smc_ret_values ffa_mem_reclaim(uint64_t handle, uint32_t flags);
398
J-Alves7581c382020-05-07 18:34:20 +0100399#endif /* __ASSEMBLY__ */
400
401#endif /* FFA_HELPERS_H */