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