Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 1 | /* |
Bence Balogh | 7527ddf | 2025-02-25 08:18:14 +0100 | [diff] [blame] | 2 | * Copyright (c) 2022-2025, Arm Limited. All rights reserved. |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 10 | #include "protocols/rpc/common/packed-c/status.h" |
| 11 | #include "psa/client.h" |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 12 | #include "rse_comms_caller.h" |
| 13 | #include "rse_comms_protocol.h" |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 14 | #include "trace.h" |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 15 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 16 | /* This value should be set according to the implemented channels |
| 17 | * in the MHU. As only the Embed protocol is supported at the moment, |
| 18 | * it is set big enough to fit even the largest messages. |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 19 | */ |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 20 | #ifndef COMMS_MHU_MSG_SIZE |
| 21 | #define COMMS_MHU_MSG_SIZE 0x2200 |
| 22 | #endif |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 23 | |
Bence Balogh | 7527ddf | 2025-02-25 08:18:14 +0100 | [diff] [blame] | 24 | /* |
| 25 | * This must be set beforehand, during the SP's initialization. This is |
| 26 | * the FF-A ID of the SP. |
| 27 | */ |
| 28 | extern uint16_t own_id; |
| 29 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 30 | static uint8_t select_protocol_version(const struct psa_invec *in_vec, size_t in_len, |
| 31 | const struct psa_outvec *out_vec, size_t out_len) |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 32 | { |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 33 | size_t comms_embed_msg_min_size; |
| 34 | size_t comms_embed_reply_min_size; |
| 35 | size_t in_size_total = 0; |
| 36 | size_t out_size_total = 0; |
| 37 | size_t i; |
| 38 | |
| 39 | for (i = 0U; i < in_len; ++i) { |
| 40 | in_size_total += in_vec[i].len; |
| 41 | } |
| 42 | for (i = 0U; i < out_len; ++i) { |
| 43 | out_size_total += out_vec[i].len; |
| 44 | } |
| 45 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 46 | comms_embed_msg_min_size = sizeof(struct serialized_rse_comms_header_t) + |
| 47 | sizeof(struct rse_embed_msg_t) - PLAT_RSE_COMMS_PAYLOAD_MAX_SIZE; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 48 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 49 | comms_embed_reply_min_size = sizeof(struct serialized_rse_comms_header_t) + |
| 50 | sizeof(struct rse_embed_reply_t) - |
| 51 | PLAT_RSE_COMMS_PAYLOAD_MAX_SIZE; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 52 | |
| 53 | /* Use embed if we can pack into one message and reply, else use |
| 54 | * pointer_access. The underlying MHU transport protocol uses a |
| 55 | * single uint32_t to track the length, so the amount of data that |
| 56 | * can be in a message is 4 bytes less than mhu_get_max_message_size |
| 57 | * reports. |
| 58 | * |
| 59 | * TODO tune this with real performance numbers, it's possible a |
| 60 | * pointer_access message is less performant than multiple embed |
| 61 | * messages due to ATU configuration costs to allow access to the |
| 62 | * pointers. |
| 63 | */ |
| 64 | if ((comms_embed_msg_min_size + in_size_total > |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 65 | COMMS_MHU_MSG_SIZE - sizeof(uint32_t)) || |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 66 | (comms_embed_reply_min_size + out_size_total > |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 67 | COMMS_MHU_MSG_SIZE - sizeof(uint32_t))) { |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 68 | return RSE_COMMS_PROTOCOL_POINTER_ACCESS; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 69 | } else { |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 70 | return RSE_COMMS_PROTOCOL_EMBED; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 74 | psa_status_t __psa_call(struct rpc_caller_interface *caller, psa_handle_t handle, int32_t client_id, |
| 75 | int32_t type, const struct psa_invec *in_vec, size_t in_len, |
| 76 | struct psa_outvec *out_vec, size_t out_len) |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 77 | { |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 78 | static uint8_t seq_num = 1U; |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 79 | rpc_status_t rpc_status = RPC_ERROR_INTERNAL; |
| 80 | psa_status_t psa_status = PSA_ERROR_INVALID_ARGUMENT; |
| 81 | psa_status_t return_val = PSA_ERROR_INVALID_ARGUMENT; |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 82 | rse_comms_call_handle rpc_handle = (rse_comms_call_handle)caller; |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 83 | size_t req_len = 0; |
| 84 | size_t resp_len = 0; |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 85 | struct serialized_rse_comms_msg_t *req = NULL; |
| 86 | struct serialized_rse_comms_reply_t *reply = NULL; |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 87 | uint8_t protocol_ver = 0; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 88 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 89 | protocol_ver = select_protocol_version(in_vec, in_len, out_vec, out_len); |
| 90 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 91 | psa_status = rse_protocol_calculate_msg_len(handle, protocol_ver, in_vec, in_len, &req_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 92 | if (psa_status != PSA_SUCCESS) { |
| 93 | EMSG("Message size calculation failed: %d", psa_status); |
| 94 | return psa_status; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 97 | rpc_handle = rse_comms_caller_begin(caller, (uint8_t **)&req, req_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 98 | if (!rpc_handle) { |
| 99 | EMSG("Could not get handle"); |
| 100 | return PSA_ERROR_GENERIC_ERROR; |
| 101 | } |
| 102 | |
| 103 | req->header.seq_num = seq_num; |
Bence Balogh | 7527ddf | 2025-02-25 08:18:14 +0100 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * This is needed because the "0" is not accepted in TF-M so it has to be remapped |
| 107 | * to a different value. |
| 108 | * The SE-Proxy's own FFA ID is used as the new value. This is a viable option |
| 109 | * because the SE-Proxy SP never originates requests itself, it just |
| 110 | * forwards the requests of the other endpoints. |
| 111 | */ |
| 112 | |
| 113 | if (client_id == 0) |
| 114 | req->header.client_id = own_id; |
| 115 | else |
| 116 | req->header.client_id = client_id; |
| 117 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 118 | req->header.protocol_ver = protocol_ver; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 119 | |
Michael Zhao | d556737 | 2024-11-15 11:45:00 +0000 | [diff] [blame^] | 120 | psa_status = rse_protocol_serialize_msg(caller, handle, type, in_vec, in_len, out_vec, |
| 121 | out_len, req, &req_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 122 | if (psa_status != PSA_SUCCESS) { |
| 123 | EMSG("Serialize msg failed: %d", psa_status); |
| 124 | return psa_status; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 127 | DMSG("Sending rse_comms message"); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 128 | DMSG("protocol_ver=%u", req->header.protocol_ver); |
| 129 | DMSG("seq_num=%u", req->header.seq_num); |
| 130 | DMSG("client_id=%u", req->header.client_id); |
| 131 | |
| 132 | resp_len = sizeof(*reply); |
| 133 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 134 | rpc_status = rse_comms_caller_invoke(rpc_handle, 0, (uint8_t **)&reply, &resp_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 135 | if (rpc_status != RPC_SUCCESS) { |
| 136 | EMSG("Invoke failed: %d", rpc_status); |
| 137 | return PSA_ERROR_GENERIC_ERROR; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 140 | DMSG("Received rse_comms reply"); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 141 | DMSG("protocol_ver=%u", reply->header.protocol_ver); |
| 142 | DMSG("seq_num=%u", reply->header.seq_num); |
| 143 | DMSG("client_id=%u", reply->header.client_id); |
| 144 | DMSG("resp_len=%lu", resp_len); |
| 145 | |
Michael Zhao | d556737 | 2024-11-15 11:45:00 +0000 | [diff] [blame^] | 146 | psa_status = rse_protocol_deserialize_reply(caller, out_vec, out_len, &return_val, reply, |
| 147 | resp_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 148 | if (psa_status != PSA_SUCCESS) { |
| 149 | EMSG("Protocol deserialize reply failed: %d", psa_status); |
| 150 | return psa_status; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 153 | DMSG("Return_val=%d", return_val); |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 154 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame] | 155 | rse_comms_caller_end(rpc_handle); |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 156 | |
| 157 | seq_num++; |
| 158 | |
| 159 | return return_val; |
| 160 | } |
| 161 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 162 | psa_status_t psa_call_client_id(struct rpc_caller_interface *caller, psa_handle_t psa_handle, |
| 163 | int32_t client_id, int32_t type, const struct psa_invec *in_vec, |
| 164 | size_t in_len, struct psa_outvec *out_vec, size_t out_len) |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 165 | { |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 166 | return __psa_call(caller, psa_handle, client_id, type, in_vec, in_len, out_vec, out_len); |
| 167 | } |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 168 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 169 | psa_status_t psa_call(struct rpc_caller_interface *caller, psa_handle_t psa_handle, int32_t type, |
| 170 | const struct psa_invec *in_vec, size_t in_len, struct psa_outvec *out_vec, |
| 171 | size_t out_len) |
| 172 | { |
| 173 | return __psa_call(caller, psa_handle, 0, type, in_vec, in_len, out_vec, out_len); |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 174 | } |