David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 1 | /* |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 8 | #include "spm_ipc.h" |
Mingyang Sun | 133a792 | 2021-07-08 16:01:26 +0800 | [diff] [blame] | 9 | #include "ffm/psa_api.h" |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 10 | #include "tfm_rpc.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 11 | #include "utilities.h" |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 12 | #include "load/partition_defs.h" |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame^] | 13 | #include "tfm_psa_call_param.h" |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 14 | |
| 15 | static void default_handle_req(void) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | static void default_mailbox_reply(const void *owner, int32_t ret) |
| 20 | { |
| 21 | (void)owner; |
| 22 | (void)ret; |
| 23 | } |
| 24 | |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 25 | static const void *default_get_caller_data(int32_t client_id) |
| 26 | { |
| 27 | (void)client_id; |
| 28 | |
| 29 | return NULL; |
| 30 | } |
| 31 | |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 32 | static struct tfm_rpc_ops_t rpc_ops = { |
| 33 | .handle_req = default_handle_req, |
| 34 | .reply = default_mailbox_reply, |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 35 | .get_caller_data = default_get_caller_data, |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | uint32_t tfm_rpc_psa_framework_version(void) |
| 39 | { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 40 | return tfm_spm_client_psa_framework_version(); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 43 | uint32_t tfm_rpc_psa_version(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 44 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 45 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 46 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 47 | return tfm_spm_client_psa_version(params->sid); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 50 | psa_status_t tfm_rpc_psa_connect(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 51 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 52 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 53 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 54 | return tfm_spm_client_psa_connect(params->sid, params->version); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 57 | psa_status_t tfm_rpc_psa_call(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 58 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 59 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 60 | |
Mingyang Sun | eeca465 | 2021-07-15 15:19:16 +0800 | [diff] [blame^] | 61 | return tfm_spm_client_psa_call(params->handle, |
| 62 | PARAM_PACK(params->type, |
| 63 | params->in_len, |
| 64 | params->out_len), |
| 65 | params->in_vec, params->out_vec); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 68 | void tfm_rpc_psa_close(const struct client_call_params_t *params) |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 69 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 70 | TFM_CORE_ASSERT(params != NULL); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 71 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 72 | tfm_spm_client_psa_close(params->handle); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int32_t tfm_rpc_register_ops(const struct tfm_rpc_ops_t *ops_ptr) |
| 76 | { |
| 77 | if (!ops_ptr) { |
| 78 | return TFM_RPC_INVAL_PARAM; |
| 79 | } |
| 80 | |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 81 | if (!ops_ptr->handle_req || !ops_ptr->reply || !ops_ptr->get_caller_data) { |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 82 | return TFM_RPC_INVAL_PARAM; |
| 83 | } |
| 84 | |
| 85 | /* Currently, one and only one mailbox implementation is supported. */ |
| 86 | if ((rpc_ops.handle_req != default_handle_req) || |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 87 | (rpc_ops.reply != default_mailbox_reply) || |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 88 | (rpc_ops.get_caller_data != default_get_caller_data)) { |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 89 | return TFM_RPC_CONFLICT_CALLBACK; |
| 90 | } |
| 91 | |
| 92 | rpc_ops.handle_req = ops_ptr->handle_req; |
| 93 | rpc_ops.reply = ops_ptr->reply; |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 94 | rpc_ops.get_caller_data = ops_ptr->get_caller_data; |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 95 | |
| 96 | return TFM_RPC_SUCCESS; |
| 97 | } |
| 98 | |
| 99 | void tfm_rpc_unregister_ops(void) |
| 100 | { |
| 101 | rpc_ops.handle_req = default_handle_req; |
| 102 | rpc_ops.reply = default_mailbox_reply; |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 103 | rpc_ops.get_caller_data = default_get_caller_data; |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void tfm_rpc_client_call_handler(void) |
| 107 | { |
| 108 | rpc_ops.handle_req(); |
| 109 | } |
| 110 | |
| 111 | void tfm_rpc_client_call_reply(const void *owner, int32_t ret) |
| 112 | { |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 113 | const struct tfm_msg_body_t *msg = (const struct tfm_msg_body_t *)owner; |
| 114 | |
| 115 | rpc_ops.reply(msg->caller_data, ret); |
| 116 | } |
| 117 | |
| 118 | void tfm_rpc_set_caller_data(struct tfm_msg_body_t *msg, int32_t client_id) |
| 119 | { |
David Hu | a79c7c1 | 2019-12-11 18:47:06 +0800 | [diff] [blame] | 120 | msg->caller_data = rpc_ops.get_caller_data(client_id); |
David Hu | 5923bc2 | 2019-09-23 15:44:06 +0800 | [diff] [blame] | 121 | } |