blob: 011d9418848cf0f2c447e55063b97612e49663e6 [file] [log] [blame]
Imre Kis9fcf8412020-11-23 03:15:45 +01001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
4 */
5
6#include <assert.h> // for assert
7#include <stddef.h> // for size_t
8#include <stdint.h> // for uint32_t, uint16_t, uintptr_t, U
9#include "ffa_api.h" // for FFA_OK, ffa_interrupt_handler, ffa_fea...
10#include "ffa_api_defines.h" // for FFA_PARAM_MBZ, FFA_OK, FFA_ERROR, FFA_...
11#include "ffa_api_types.h" // for ffa_result, ffa_direct_msg, ffa_uuid
12#include "ffa_internal_api.h" // for ffa_params, ffa_svc
13#include "util.h" // for GENMASK_32, SHIFT_U32, BIT
14
15/*
16 * Unpacks the error code from the FFA_ERROR message. It is a signed 32 bit
17 * value in an unsigned 64 bit field so proper casting must be used to avoid
18 * compiler dependent behavior.
19 */
20static inline ffa_result ffa_get_errorcode(struct ffa_params *result)
21{
22 uint32_t raw_value = result->a2;
23
24 return *(ffa_result *)(&raw_value);
25}
26
27/*
28 * Unpacks the content of the SVC result into an ffa_direct_msg structure.
29 */
30static inline void ffa_unpack_direct_msg(struct ffa_params *svc_result,
31 struct ffa_direct_msg *msg)
32{
33 msg->function_id = svc_result->a0;
34 msg->source_id = (svc_result->a1 >> 16);
35 msg->destination_id = svc_result->a1;
36 msg->args[0] = svc_result->a3;
37 msg->args[1] = svc_result->a4;
38 msg->args[2] = svc_result->a5;
39 msg->args[3] = svc_result->a6;
40 msg->args[4] = svc_result->a7;
41}
42
43/*
44 * The end of the interrupt handler is indicated by an FFA_MSG_WAIT call.
45 */
46static inline void ffa_return_from_interrupt(struct ffa_params *result)
47{
48 ffa_svc(FFA_MSG_WAIT, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
49 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
50 result);
51}
52
53static inline void ffa_uuid_to_abi_format(const struct ffa_uuid *uuid,
54 uint32_t *result)
55{
56 size_t i = 0;
57
58 for (i = 0; i < 4; i++) {
59 result[i] = uuid->uuid[4 * i];
60 result[i] |= SHIFT_U32(uuid->uuid[4 * i + 1], 8);
61 result[i] |= SHIFT_U32(uuid->uuid[4 * i + 2], 16);
62 result[i] |= SHIFT_U32(uuid->uuid[4 * i + 3], 24);
63 }
64}
65
66ffa_result ffa_version(uint32_t *version)
67{
68 struct ffa_params result = {0};
69 uint32_t self_version = 0;
70
71 self_version = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_SHIFT) |
72 (FFA_VERSION_MINOR << FFA_VERSION_MINOR);
73
74 ffa_svc(FFA_VERSION, self_version, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
75 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
76 &result);
77
78 if (result.a0 & BIT(31)) {
79 uint32_t raw_error = result.a0;
80
81 *version = 0;
82
83 return *(ffa_result *)(&raw_error);
84 }
85
86 *version = result.a0;
87 return FFA_OK;
88}
89
90ffa_result ffa_features(uint32_t ffa_function_id,
91 struct ffa_interface_properties *interface_properties)
92{
93 struct ffa_params result = {0};
94
95 ffa_svc(FFA_FEATURES, ffa_function_id, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
96 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
97 &result);
98
99 if (result.a0 == FFA_ERROR) {
100 interface_properties->interface_properties[0] = 0;
101 interface_properties->interface_properties[1] = 0;
102 return ffa_get_errorcode(&result);
103 }
104
105 assert(result.a0 == FFA_SUCCESS_32);
106 interface_properties->interface_properties[0] = result.a2;
107 interface_properties->interface_properties[1] = result.a3;
108 return FFA_OK;
109}
110
111ffa_result ffa_rx_release(void)
112{
113 struct ffa_params result = {0};
114
115 ffa_svc(FFA_RX_RELEASE, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
116 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
117 &result);
118
119 if (result.a0 == FFA_ERROR)
120 return ffa_get_errorcode(&result);
121
122 assert(result.a0 == FFA_SUCCESS_32);
123 return FFA_OK;
124}
125
126ffa_result ffa_rxtx_map(const void *tx_buffer, const void *rx_buffer,
127 uint32_t page_count)
128{
129 struct ffa_params result = {0};
130
131 assert(page_count <= FFA_RXTX_MAP_PAGE_COUNT_MAX);
132
133 page_count = SHIFT_U32(page_count & FFA_RXTX_MAP_PAGE_COUNT_MASK,
134 FFA_RXTX_MAP_PAGE_COUNT_SHIFT);
135
136 ffa_svc(FFA_RXTX_MAP_32, (uintptr_t)tx_buffer, (uintptr_t)rx_buffer,
137 page_count, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
138 FFA_PARAM_MBZ, &result);
139
140 if (result.a0 == FFA_ERROR)
141 return ffa_get_errorcode(&result);
142
143 assert(result.a0 == FFA_SUCCESS_32);
144 return FFA_OK;
145}
146
147ffa_result ffa_rxtx_unmap(uint16_t id)
148{
149 struct ffa_params result = {0};
150
151 ffa_svc(FFA_RXTX_UNMAP, SHIFT_U32(id, FFA_RXTX_UNMAP_ID_SHIFT),
152 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
153 FFA_PARAM_MBZ, FFA_PARAM_MBZ, &result);
154
155 if (result.a0 == FFA_ERROR)
156 return ffa_get_errorcode(&result);
157
158 assert(result.a0 == FFA_SUCCESS_32);
159 return FFA_OK;
160}
161
162ffa_result ffa_partition_info_get(const struct ffa_uuid *uuid, uint32_t *count)
163{
164 struct ffa_params result = {0};
165 uint32_t abi_uuid[4] = {0};
166
167 ffa_uuid_to_abi_format(uuid, abi_uuid);
168
169 ffa_svc(FFA_PARTITION_INFO_GET, abi_uuid[0], abi_uuid[1], abi_uuid[2],
170 abi_uuid[3], FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
171 &result);
172
173 if (result.a0 == FFA_ERROR) {
174 *count = UINT32_C(0);
175 return ffa_get_errorcode(&result);
176 }
177
178 assert(result.a0 == FFA_SUCCESS_32);
179 *count = result.a2;
180 return FFA_OK;
181}
182
183ffa_result ffa_id_get(uint16_t *id)
184{
185 struct ffa_params result = {0};
186
187 ffa_svc(FFA_ID_GET, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
188 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
189 &result);
190
191 if (result.a0 == FFA_ERROR) {
192 *id = FFA_ID_GET_ID_MASK;
193 return ffa_get_errorcode(&result);
194 }
195
196 assert(result.a0 == FFA_SUCCESS_32);
197 *id = (result.a2 >> FFA_ID_GET_ID_SHIFT) & FFA_ID_GET_ID_MASK;
198 return FFA_OK;
199}
200
201ffa_result ffa_msg_wait(struct ffa_direct_msg *msg)
202{
203 struct ffa_params result = {0};
204
205 ffa_svc(FFA_MSG_WAIT, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
206 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
207 &result);
208
209 while (result.a0 == FFA_INTERRUPT) {
210 ffa_interrupt_handler(result.a2);
211 ffa_return_from_interrupt(&result);
212 }
213
214 if (result.a0 == FFA_ERROR) {
215 return ffa_get_errorcode(&result);
216 } else if (result.a0 == FFA_MSG_SEND_DIRECT_REQ_32) {
217 ffa_unpack_direct_msg(&result, msg);
218 } else {
219 assert(result.a0 == FFA_SUCCESS_32);
220 *msg = (struct ffa_direct_msg){.function_id = result.a0};
221 }
222
223 return FFA_OK;
224}
225
226ffa_result ffa_msg_send_direct_req(uint16_t source, uint16_t dest, uint32_t a0,
227 uint32_t a1, uint32_t a2, uint32_t a3,
228 uint32_t a4, struct ffa_direct_msg *msg)
229{
230 struct ffa_params result = {0};
231
232 ffa_svc(FFA_MSG_SEND_DIRECT_REQ_32,
233 SHIFT_U32(source, FFA_MSG_SEND_DIRECT_REQ_SOURCE_ID_SHIFT) |
234 dest, FFA_PARAM_MBZ, a0, a1, a2, a3, a4, &result);
235
236 while (result.a0 == FFA_INTERRUPT) {
237 ffa_interrupt_handler(result.a2);
238 ffa_return_from_interrupt(&result);
239 }
240
241 if (result.a0 == FFA_ERROR) {
242 return ffa_get_errorcode(&result);
243 } else if (result.a0 == FFA_MSG_SEND_DIRECT_RESP_32) {
244 ffa_unpack_direct_msg(&result, msg);
245 } else {
246 assert(result.a0 == FFA_SUCCESS_32);
247 *msg = (struct ffa_direct_msg){.function_id = result.a0};
248 }
249
250 return FFA_OK;
251}
252
253ffa_result ffa_msg_send_direct_resp(uint16_t source, uint16_t dest, uint32_t a0,
254 uint32_t a1, uint32_t a2, uint32_t a3,
255 uint32_t a4, struct ffa_direct_msg *msg)
256{
257 struct ffa_params result = {0};
258
259 ffa_svc(FFA_MSG_SEND_DIRECT_RESP_32,
260 SHIFT_U32(source, FFA_MSG_SEND_DIRECT_RESP_SOURCE_ID_SHIFT) |
261 dest, FFA_PARAM_MBZ, a0, a1, a2, a3, a4, &result);
262
263 while (result.a0 == FFA_INTERRUPT) {
264 ffa_interrupt_handler(result.a2);
265 ffa_return_from_interrupt(&result);
266 }
267
268 if (result.a0 == FFA_ERROR) {
269 return ffa_get_errorcode(&result);
270 } else if (result.a0 == FFA_MSG_SEND_DIRECT_REQ_32) {
271 ffa_unpack_direct_msg(&result, msg);
272 } else {
273 assert(result.a0 == FFA_SUCCESS_32);
274 *msg = (struct ffa_direct_msg){.function_id = result.a0};
275 }
276
277 return FFA_OK;
278}