Imre Kis | 9fcf841 | 2020-11-23 03:15:45 +0100 | [diff] [blame] | 1 | // 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 | */ |
| 20 | static 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 | */ |
| 30 | static 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 | */ |
| 46 | static 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 | |
| 53 | static 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 | |
| 66 | ffa_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 | |
| 90 | ffa_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 | |
| 111 | ffa_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 | |
| 126 | ffa_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 | |
| 147 | ffa_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 | |
| 162 | ffa_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 | |
| 183 | ffa_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 | |
| 201 | ffa_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 | |
| 226 | ffa_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 | |
| 253 | ffa_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 | } |
Imre Kis | e3112e0 | 2020-11-23 03:15:46 +0100 | [diff] [blame] | 279 | |
| 280 | ffa_result ffa_mem_donate(uint32_t total_length, uint32_t fragment_length, |
| 281 | void *buffer_address, uint32_t page_count, |
| 282 | uint64_t *handle) |
| 283 | { |
| 284 | struct ffa_params result = {0}; |
| 285 | |
| 286 | ffa_svc(FFA_MEM_DONATE_32, total_length, fragment_length, |
| 287 | (uintptr_t)buffer_address, page_count, FFA_PARAM_MBZ, |
| 288 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, &result); |
| 289 | |
| 290 | if (result.a0 == FFA_ERROR) { |
| 291 | *handle = 0U; |
| 292 | return ffa_get_errorcode(&result); |
| 293 | } |
| 294 | |
| 295 | assert(result.a0 == FFA_SUCCESS_32); |
| 296 | *handle = reg_pair_to_64(result.a3, result.a2); |
| 297 | return FFA_OK; |
| 298 | } |
| 299 | |
| 300 | ffa_result ffa_mem_donate_rxtx(uint32_t total_length, uint32_t fragment_length, |
| 301 | uint64_t *handle) |
| 302 | { |
| 303 | return ffa_mem_donate(total_length, fragment_length, NULL, 0, handle); |
| 304 | } |
| 305 | |
| 306 | ffa_result ffa_mem_lend(uint32_t total_length, uint32_t fragment_length, |
| 307 | void *buffer_address, uint32_t page_count, |
| 308 | uint64_t *handle) |
| 309 | { |
| 310 | struct ffa_params result = {0}; |
| 311 | |
| 312 | ffa_svc(FFA_MEM_LEND_32, total_length, fragment_length, |
| 313 | (uintptr_t)buffer_address, page_count, FFA_PARAM_MBZ, |
| 314 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, &result); |
| 315 | |
| 316 | if (result.a0 == FFA_ERROR) { |
| 317 | *handle = 0U; |
| 318 | return ffa_get_errorcode(&result); |
| 319 | } |
| 320 | |
| 321 | assert(result.a0 == FFA_SUCCESS_32); |
| 322 | *handle = reg_pair_to_64(result.a3, result.a2); |
| 323 | return FFA_OK; |
| 324 | } |
| 325 | |
| 326 | ffa_result ffa_mem_lend_rxtx(uint32_t total_length, uint32_t fragment_length, |
| 327 | uint64_t *handle) |
| 328 | { |
| 329 | return ffa_mem_lend(total_length, fragment_length, NULL, 0, handle); |
| 330 | } |
| 331 | |
| 332 | ffa_result ffa_mem_share(uint32_t total_length, uint32_t fragment_length, |
| 333 | void *buffer_address, uint32_t page_count, |
| 334 | uint64_t *handle) |
| 335 | { |
| 336 | struct ffa_params result = {0}; |
| 337 | |
| 338 | ffa_svc(FFA_MEM_SHARE_32, total_length, fragment_length, |
| 339 | (uintptr_t)buffer_address, page_count, FFA_PARAM_MBZ, |
| 340 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, &result); |
| 341 | |
| 342 | if (result.a0 == FFA_ERROR) { |
| 343 | *handle = 0U; |
| 344 | return ffa_get_errorcode(&result); |
| 345 | } |
| 346 | |
| 347 | assert(result.a0 == FFA_SUCCESS_32); |
| 348 | *handle = reg_pair_to_64(result.a3, result.a2); |
| 349 | return FFA_OK; |
| 350 | } |
| 351 | |
| 352 | ffa_result ffa_mem_share_rxtx(uint32_t total_length, uint32_t fragment_length, |
| 353 | uint64_t *handle) |
| 354 | { |
| 355 | return ffa_mem_share(total_length, fragment_length, NULL, 0, handle); |
| 356 | } |
| 357 | |
| 358 | ffa_result ffa_mem_retrieve_req(uint32_t total_length, uint32_t fragment_length, |
| 359 | void *buffer_address, uint32_t page_count, |
| 360 | uint32_t *resp_total_length, |
| 361 | uint32_t *resp_fragment_length) |
| 362 | { |
| 363 | struct ffa_params result = {0}; |
| 364 | |
| 365 | ffa_svc(FFA_MEM_RETRIEVE_REQ_32, total_length, fragment_length, |
| 366 | (uintptr_t)buffer_address, page_count, FFA_PARAM_MBZ, |
| 367 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, &result); |
| 368 | |
| 369 | if (result.a0 == FFA_ERROR) { |
| 370 | *resp_total_length = 0U; |
| 371 | *resp_fragment_length = 0U; |
| 372 | return ffa_get_errorcode(&result); |
| 373 | } |
| 374 | |
| 375 | assert(result.a0 == FFA_MEM_RETRIEVE_RESP); |
| 376 | *resp_total_length = result.a1; |
| 377 | *resp_fragment_length = result.a2; |
| 378 | return FFA_OK; |
| 379 | } |
| 380 | |
| 381 | ffa_result ffa_mem_retrieve_req_rxtx(uint32_t total_length, |
| 382 | uint32_t fragment_length, |
| 383 | uint32_t *resp_total_length, |
| 384 | uint32_t *resp_fragment_length) |
| 385 | { |
| 386 | return ffa_mem_retrieve_req(total_length, fragment_length, NULL, 0, |
| 387 | resp_total_length, resp_fragment_length); |
| 388 | } |
| 389 | |
| 390 | ffa_result ffa_mem_relinquish(void) |
| 391 | { |
| 392 | struct ffa_params result = {0}; |
| 393 | |
| 394 | ffa_svc(FFA_MEM_RELINQUISH, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 395 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 396 | &result); |
| 397 | |
| 398 | if (result.a0 == FFA_ERROR) |
| 399 | return ffa_get_errorcode(&result); |
| 400 | |
| 401 | assert(result.a0 == FFA_SUCCESS_32); |
| 402 | return FFA_OK; |
| 403 | } |
| 404 | |
| 405 | ffa_result ffa_mem_reclaim(uint64_t handle, uint32_t flags) |
| 406 | { |
| 407 | struct ffa_params result = {0}; |
| 408 | uint32_t handle_hi = 0; |
| 409 | uint32_t handle_lo = 0; |
| 410 | |
| 411 | reg_pair_from_64(handle, &handle_hi, &handle_lo); |
| 412 | |
| 413 | ffa_svc(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, FFA_PARAM_MBZ, |
| 414 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, &result); |
| 415 | |
| 416 | if (result.a0 == FFA_ERROR) |
| 417 | return ffa_get_errorcode(&result); |
| 418 | |
| 419 | assert(result.a0 == FFA_SUCCESS_32); |
| 420 | return FFA_OK; |
| 421 | } |