1 /*
2  * Copyright (c) 2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #pragma once
8 
9 /*
10  * Subset of Arm PSA Firmware Framework for Arm v8-A 1.0 EAC
11  * (https://developer.arm.com/docs/den0077/a) needed for shared memory.
12  */
13 
14 #include <services/ffa_svc.h>
15 #include "smcall.h"
16 
17 #ifndef STATIC_ASSERT
18 #define STATIC_ASSERT(e) _Static_assert(e, #e)
19 #endif
20 
21 #define TRUSTY_FFA_CURRENT_VERSION_MAJOR (1U)
22 #define TRUSTY_FFA_CURRENT_VERSION_MINOR (0U)
23 
24 #define FFA_VERSION_TO_MAJOR(V) ((V) >> FFA_VERSION_MAJOR_SHIFT)
25 #define MAKE_TRUSTY_FFA_CURRENT_VERSION	\
26 	MAKE_FFA_VERSION(TRUSTY_FFA_CURRENT_VERSION_MAJOR, \
27 			 TRUSTY_FFA_CURRENT_VERSION_MINOR)
28 
29 #define FFA_NWLD_ID_BASE	0x0
30 #define FFA_SWLD_ID_BASE	0x8000
31 
32 #define SMC_ENTITY_SHARED_MEMORY 4
33 
34 #define SMC_FASTCALL_NR_SHARED_MEMORY(nr) \
35     SMC_FASTCALL_NR(SMC_ENTITY_SHARED_MEMORY, nr)
36 #define SMC_FASTCALL64_NR_SHARED_MEMORY(nr) \
37     SMC_FASTCALL64_NR(SMC_ENTITY_SHARED_MEMORY, nr)
38 
39 #define FFA_PAGE_SIZE (4096)
40 
41 /**
42  * typedef ffa_endpoint_id16_t - Endpoint ID
43  *
44  * Current implementation only supports VMIDs. FFA spec also support stream
45  * endpoint ids.
46  */
47 typedef uint16_t ffa_endpoint_id16_t;
48 
49 /**
50  * struct ffa_cons_mrd - Constituent memory region descriptor
51  * @address:
52  *         Start address of contiguous memory region. Must be 4K page aligned.
53  * @page_count:
54  *         Number of 4K pages in region.
55  * @reserved_12_15:
56  *         Reserve bytes 12-15 to pad struct size to 16 bytes.
57  */
58 struct ffa_cons_mrd {
59     uint64_t address;
60     uint32_t page_count;
61     uint32_t reserved_12_15;
62 };
63 STATIC_ASSERT(sizeof(struct ffa_cons_mrd) == 16);
64 
65 /**
66  * struct ffa_comp_mrd - Composite memory region descriptor
67  * @total_page_count:
68  *         Number of 4k pages in memory region. Must match sum of
69  *         @address_range_array[].page_count.
70  * @address_range_count:
71  *         Number of entries in @address_range_array.
72  * @reserved_8_15:
73  *         Reserve bytes 8-15 to pad struct size to 16 byte alignment and
74  *         make @address_range_array 16 byte aligned.
75  * @address_range_array:
76  *         Array of &struct ffa_cons_mrd entries.
77  */
78 struct ffa_comp_mrd {
79     uint32_t total_page_count;
80     uint32_t address_range_count;
81     uint64_t reserved_8_15;
82     struct ffa_cons_mrd address_range_array[];
83 };
84 STATIC_ASSERT(sizeof(struct ffa_comp_mrd) == 16);
85 
86 /**
87  * typedef ffa_mem_attr8_t - Memory region attributes
88  *
89  * * @FFA_MEM_ATTR_DEVICE_NGNRNE:
90  *     Device-nGnRnE.
91  * * @FFA_MEM_ATTR_DEVICE_NGNRE:
92  *     Device-nGnRE.
93  * * @FFA_MEM_ATTR_DEVICE_NGRE:
94  *     Device-nGRE.
95  * * @FFA_MEM_ATTR_DEVICE_GRE:
96  *     Device-GRE.
97  * * @FFA_MEM_ATTR_NORMAL_MEMORY_UNCACHED
98  *     Normal memory. Non-cacheable.
99  * * @FFA_MEM_ATTR_NORMAL_MEMORY_CACHED_WB
100  *     Normal memory. Write-back cached.
101  * * @FFA_MEM_ATTR_NON_SHAREABLE
102  *     Non-shareable. Combine with FFA_MEM_ATTR_NORMAL_MEMORY_*.
103  * * @FFA_MEM_ATTR_OUTER_SHAREABLE
104  *     Outer Shareable. Combine with FFA_MEM_ATTR_NORMAL_MEMORY_*.
105  * * @FFA_MEM_ATTR_INNER_SHAREABLE
106  *     Inner Shareable. Combine with FFA_MEM_ATTR_NORMAL_MEMORY_*.
107  * * @FFA_MEM_ATTR_NONSECURE
108  *     Set on retrieve if memory is nonsecure and client requested it be set.
109  */
110 typedef uint8_t ffa_mem_attr8_t;
111 #define FFA_MEM_ATTR_DEVICE_NGNRNE ((1U << 4) | (0x0U << 2))
112 #define FFA_MEM_ATTR_DEVICE_NGNRE ((1U << 4) | (0x1U << 2))
113 #define FFA_MEM_ATTR_DEVICE_NGRE ((1U << 4) | (0x2U << 2))
114 #define FFA_MEM_ATTR_DEVICE_GRE ((1U << 4) | (0x3U << 2))
115 #define FFA_MEM_ATTR_NORMAL_MEMORY_UNCACHED ((2U << 4) | (0x1U << 2))
116 #define FFA_MEM_ATTR_NORMAL_MEMORY_CACHED_WB ((2U << 4) | (0x3U << 2))
117 #define FFA_MEM_ATTR_NON_SHAREABLE (0x0U << 0)
118 #define FFA_MEM_ATTR_OUTER_SHAREABLE (0x2U << 0)
119 #define FFA_MEM_ATTR_INNER_SHAREABLE (0x3U << 0)
120 #define FFA_MEM_ATTR_NONSECURE (1U << 6)
121 
122 /**
123  * typedef ffa_mem_perm8_t - Memory access permissions
124  *
125  * * @FFA_MEM_ATTR_RO
126  *     Request or specify read-only mapping.
127  * * @FFA_MEM_ATTR_RW
128  *     Request or allow read-write mapping.
129  * * @FFA_MEM_PERM_NX
130  *     Deny executable mapping.
131  * * @FFA_MEM_PERM_X
132  *     Request executable mapping.
133  */
134 typedef uint8_t ffa_mem_perm8_t;
135 #define FFA_MEM_PERM_RO (1U << 0)
136 #define FFA_MEM_PERM_RW (1U << 1)
137 #define FFA_MEM_PERM_NX (1U << 2)
138 #define FFA_MEM_PERM_X (1U << 3)
139 
140 /**
141  * typedef ffa_mem_flag8_t - Endpoint memory flags
142  *
143  * * @FFA_MEM_FLAG_NON_RETRIEVAL_BORROWER
144  *     Non-retrieval Borrower. Memory region must not be or was not retrieved on
145  *     behalf of this endpoint.
146  */
147 typedef uint8_t ffa_mem_flag8_t;
148 #define FFA_MEM_FLAG_NON_RETRIEVAL_BORROWER (1U << 0)
149 
150 /**
151  * typedef ffa_mtd_flag32_t - Memory transaction descriptor flags
152  *
153  * * @FFA_MTD_FLAG_ZERO_MEMORY
154  *     Zero memory after unmapping from sender (must be 0 for share).
155  * * @FFA_MTD_FLAG_TIME_SLICING
156  *     Not supported by this implementation.
157  * * @FFA_MTD_FLAG_ZERO_MEMORY_AFTER_RELINQUISH
158  *     Zero memory after unmapping from borrowers (must be 0 for share).
159  * * @FFA_MTD_FLAG_TYPE_MASK
160  *     Bit-mask to extract memory management transaction type from flags.
161  * * @FFA_MTD_FLAG_TYPE_SHARE_MEMORY
162  *     Share memory transaction flag.
163  *     Used by @SMC_FC_FFA_MEM_RETRIEVE_RESP to indicate that memory came from
164  *     @SMC_FC_FFA_MEM_SHARE and by @SMC_FC_FFA_MEM_RETRIEVE_REQ to specify that
165  *     it must have.
166  * * @FFA_MTD_FLAG_TYPE_LEND_MEMORY
167  *     Lend memory transaction flag.
168  *     Used by @SMC_FC_FFA_MEM_RETRIEVE_RESP to indicate that memory came from
169  *     @SMC_FC_FFA_MEM_LEND and by @SMC_FC_FFA_MEM_RETRIEVE_REQ to specify that
170  *     it must have.
171  * * @FFA_MTD_FLAG_ADDRESS_RANGE_ALIGNMENT_HINT_MASK
172  *     Not supported by this implementation.
173  */
174 typedef uint32_t ffa_mtd_flag32_t;
175 #define FFA_MTD_FLAG_ZERO_MEMORY (1U << 0)
176 #define FFA_MTD_FLAG_TIME_SLICING (1U << 1)
177 #define FFA_MTD_FLAG_ZERO_MEMORY_AFTER_RELINQUISH (1U << 2)
178 #define FFA_MTD_FLAG_TYPE_MASK (3U << 3)
179 #define FFA_MTD_FLAG_TYPE_SHARE_MEMORY (1U << 3)
180 #define FFA_MTD_FLAG_TYPE_LEND_MEMORY (2U << 3)
181 #define FFA_MTD_FLAG_ADDRESS_RANGE_ALIGNMENT_HINT_MASK (0x1FU << 5)
182 
183 /**
184  * struct ffa_mapd - Memory access permissions descriptor
185  * @endpoint_id:
186  *         Endpoint id that @memory_access_permissions and @flags apply to.
187  *         (&typedef ffa_endpoint_id16_t).
188  * @memory_access_permissions:
189  *         FFA_MEM_PERM_* values or'ed together (&typedef ffa_mem_perm8_t).
190  * @flags:
191  *         FFA_MEM_FLAG_* values or'ed together (&typedef ffa_mem_flag8_t).
192  */
193 struct ffa_mapd {
194     ffa_endpoint_id16_t endpoint_id;
195     ffa_mem_perm8_t memory_access_permissions;
196     ffa_mem_flag8_t flags;
197 };
198 STATIC_ASSERT(sizeof(struct ffa_mapd) == 4);
199 
200 /**
201  * struct ffa_emad - Endpoint memory access descriptor.
202  * @mapd:  &struct ffa_mapd.
203  * @comp_mrd_offset:
204  *         Offset of &struct ffa_comp_mrd form start of &struct ffa_mtd.
205  * @reserved_8_15:
206  *         Reserved bytes 8-15. Must be 0.
207  */
208 struct ffa_emad {
209     struct ffa_mapd mapd;
210     uint32_t comp_mrd_offset;
211     uint64_t reserved_8_15;
212 };
213 STATIC_ASSERT(sizeof(struct ffa_emad) == 16);
214 
215 /**
216  * struct ffa_mtd - Memory transaction descriptor.
217  * @sender_id:
218  *         Sender endpoint id.
219  * @memory_region_attributes:
220  *         FFA_MEM_ATTR_* values or'ed together (&typedef ffa_mem_attr8_t).
221  * @reserved_3:
222  *         Reserved bytes 3. Must be 0.
223  * @flags:
224  *         FFA_MTD_FLAG_* values or'ed together (&typedef ffa_mtd_flag32_t).
225  * @handle:
226  *         Id of shared memory object. Most be 0 for MEM_SHARE.
227  * @tag:   Client allocated tag. Must match original value.
228  * @reserved_24_27:
229  *         Reserved bytes 24-27. Must be 0.
230  * @emad_count:
231  *         Number of entries in @emad. Must be 1 in current implementation.
232  *         FFA spec allows more entries.
233  * @emad:
234  *         Endpoint memory access descriptor array (see @struct ffa_emad).
235  */
236 struct ffa_mtd {
237     ffa_endpoint_id16_t sender_id;
238     ffa_mem_attr8_t memory_region_attributes;
239     uint8_t reserved_3;
240     ffa_mtd_flag32_t flags;
241     uint64_t handle;
242     uint64_t tag;
243     uint32_t reserved_24_27;
244     uint32_t emad_count;
245     struct ffa_emad emad[];
246 };
247 STATIC_ASSERT(sizeof(struct ffa_mtd) == 32);
248 
249 /**
250  * struct ffa_mem_relinquish_descriptor - Relinquish request descriptor.
251  * @handle:
252  *         Id of shared memory object to relinquish.
253  * @flags:
254  *         If bit 0 is set clear memory after unmapping from borrower. Must be 0
255  *         for share. Bit[1]: Time slicing. Not supported, must be 0. All other
256  *         bits are reserved 0.
257  * @endpoint_count:
258  *         Number of entries in @endpoint_array.
259  * @endpoint_array:
260  *         Array of endpoint ids.
261  */
262 struct ffa_mem_relinquish_descriptor {
263     uint64_t handle;
264     uint32_t flags;
265     uint32_t endpoint_count;
266     ffa_endpoint_id16_t endpoint_array[];
267 };
268 STATIC_ASSERT(sizeof(struct ffa_mem_relinquish_descriptor) == 16);
269 
270 /**
271  * struct ffa_partition_info - FFA partition info descriptor.
272  * @id:
273  *         16-bit ID of the partition
274  * @execution_ctx_count:
275  *         Number of execution contexts implemented by this partition
276  * @properties:
277  *         Flags to determine partition properties. Like direct/indirect
278  *         messages send/receive capabilities.
279  */
280 struct ffa_partition_info {
281 	uint16_t id;
282 	uint16_t execution_ctx_count;
283 #define FFA_PART_PROP_RECV_DIRECT (1U)
284 #define FFA_PART_PROP_SEND_DIRECT (1U << 1)
285 #define FFA_PART_PROP_VM_MSGS (1U << 6)
286 	uint32_t properties;
287 };
288 
289 /**
290  * typedef ffa_features2_t - FFA_FEATURES values returned in w2
291  *
292  * * @FFA_FEATURES2_RXTX_MAP_BUF_SIZE_MASK
293  *     For RXTX_MAP: min buffer size and alignment boundary mask.
294  * * @FFA_FEATURES2_RXTX_MAP_BUF_SIZE_4K
295  *     For RXTX_MAP: min buffer size and alignment boundary is 4K.
296  * * @FFA_FEATURES2_RXTX_MAP_BUF_SIZE_64K
297  *     For RXTX_MAP: min buffer size and alignment boundary is 64K.
298  * * @FFA_FEATURES2_RXTX_MAP_BUF_SIZE_16K
299  *     For RXTX_MAP: min buffer size and alignment boundary is 16K.
300  * * @FFA_FEATURES2_MEM_DYNAMIC_BUFFER
301  *     Supports custom buffers for memory transactions.
302  * * @FFA_FEATURES2_MEM_RETRIEVE_REQ_NS_BIT
303  *     Supports setting the NS bit on retrieved descriptors.
304  *
305  * For all other bits and commands: must be 0.
306  */
307 typedef uint32_t ffa_features2_t;
308 #define FFA_FEATURES2_RXTX_MAP_BUF_SIZE_MASK 0x3U
309 #define FFA_FEATURES2_RXTX_MAP_BUF_SIZE_4K 0x0U
310 #define FFA_FEATURES2_RXTX_MAP_BUF_SIZE_64K 0x1U
311 #define FFA_FEATURES2_RXTX_MAP_BUF_SIZE_16K 0x2U
312 #define FFA_FEATURES2_MEM_DYNAMIC_BUFFER 0x1U
313 #define FFA_FEATURES2_MEM_RETRIEVE_REQ_NS_BIT 0x2U
314 
315 /**
316  * typedef ffa_features3_t - FFA_FEATURES values returned in w3
317  *
318  * * @FFA_FEATURES3_MEM_RETRIEVE_REQ_REFCOUNT_MASK
319  *     For FFA_MEM_RETRIEVE_REQ, bit[7-0]: Number of times receiver can
320  *     retrieve each memory region before relinquishing it specified as
321  *     ((1U << (value + 1)) - 1 (or value = bits in reference count - 1).
322  *
323  * For all other bits and commands: must be 0.
324  */
325 typedef uint32_t ffa_features3_t;
326 #define FFA_FEATURES3_MEM_RETRIEVE_REQ_REFCOUNT_MASK 0xffU
327 
328 /**
329  * SMC_FC32_FFA_MIN - First 32 bit SMC opcode reserved for FFA
330  */
331 #define SMC_FC32_FFA_MIN FFA_FID(SMC_32, FFA_FNUM_ERROR)
332 
333 /**
334  * SMC_FC32_FFA_MAX - Last 32 bit SMC opcode reserved for FFA
335  * For FFA version 1.0
336  */
337 #define SMC_FC32_FFA_MAX FFA_FID(SMC_32, FFA_FNUM_MEM_FRAG_TX)
338 
339 /**
340  * SMC_FC64_FFA_MIN - First 64 bit SMC opcode reserved for FFA
341  */
342 #define SMC_FC64_FFA_MIN FFA_FID(SMC_64, FFA_FNUM_ERROR)
343 
344 /**
345  * SMC_FC64_FFA_MAX - Last 64 bit SMC opcode reserved for FFA
346  * For FFA version 1.0
347  */
348 #define SMC_FC64_FFA_MAX FFA_FID(SMC_64, FFA_FNUM_MEM_FRAG_TX)
349