Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022-2024, Arm Limited. All rights reserved. |
| 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 | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 24 | static uint8_t select_protocol_version(const struct psa_invec *in_vec, size_t in_len, |
| 25 | const struct psa_outvec *out_vec, size_t out_len) |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 26 | { |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 27 | size_t comms_embed_msg_min_size; |
| 28 | size_t comms_embed_reply_min_size; |
| 29 | size_t in_size_total = 0; |
| 30 | size_t out_size_total = 0; |
| 31 | size_t i; |
| 32 | |
| 33 | for (i = 0U; i < in_len; ++i) { |
| 34 | in_size_total += in_vec[i].len; |
| 35 | } |
| 36 | for (i = 0U; i < out_len; ++i) { |
| 37 | out_size_total += out_vec[i].len; |
| 38 | } |
| 39 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 40 | comms_embed_msg_min_size = sizeof(struct serialized_rse_comms_header_t) + |
| 41 | sizeof(struct rse_embed_msg_t) - PLAT_RSE_COMMS_PAYLOAD_MAX_SIZE; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 42 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 43 | comms_embed_reply_min_size = sizeof(struct serialized_rse_comms_header_t) + |
| 44 | sizeof(struct rse_embed_reply_t) - |
| 45 | PLAT_RSE_COMMS_PAYLOAD_MAX_SIZE; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 46 | |
| 47 | /* Use embed if we can pack into one message and reply, else use |
| 48 | * pointer_access. The underlying MHU transport protocol uses a |
| 49 | * single uint32_t to track the length, so the amount of data that |
| 50 | * can be in a message is 4 bytes less than mhu_get_max_message_size |
| 51 | * reports. |
| 52 | * |
| 53 | * TODO tune this with real performance numbers, it's possible a |
| 54 | * pointer_access message is less performant than multiple embed |
| 55 | * messages due to ATU configuration costs to allow access to the |
| 56 | * pointers. |
| 57 | */ |
| 58 | if ((comms_embed_msg_min_size + in_size_total > |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 59 | COMMS_MHU_MSG_SIZE - sizeof(uint32_t)) || |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 60 | (comms_embed_reply_min_size + out_size_total > |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 61 | COMMS_MHU_MSG_SIZE - sizeof(uint32_t))) { |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 62 | return RSE_COMMS_PROTOCOL_POINTER_ACCESS; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 63 | } else { |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 64 | return RSE_COMMS_PROTOCOL_EMBED; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 68 | psa_status_t __psa_call(struct rpc_caller_interface *caller, psa_handle_t handle, int32_t client_id, |
| 69 | int32_t type, const struct psa_invec *in_vec, size_t in_len, |
| 70 | struct psa_outvec *out_vec, size_t out_len) |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 71 | { |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 72 | static uint8_t seq_num = 1U; |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 73 | rpc_status_t rpc_status = RPC_ERROR_INTERNAL; |
| 74 | psa_status_t psa_status = PSA_ERROR_INVALID_ARGUMENT; |
| 75 | psa_status_t return_val = PSA_ERROR_INVALID_ARGUMENT; |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 76 | rse_comms_call_handle rpc_handle = (rse_comms_call_handle)caller; |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 77 | size_t req_len = 0; |
| 78 | size_t resp_len = 0; |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 79 | struct serialized_rse_comms_msg_t *req = NULL; |
| 80 | struct serialized_rse_comms_reply_t *reply = NULL; |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 81 | uint8_t protocol_ver = 0; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 82 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 83 | protocol_ver = select_protocol_version(in_vec, in_len, out_vec, out_len); |
| 84 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 85 | 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] | 86 | if (psa_status != PSA_SUCCESS) { |
| 87 | EMSG("Message size calculation failed: %d", psa_status); |
| 88 | return psa_status; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 91 | rpc_handle = rse_comms_caller_begin(caller, (uint8_t **)&req, req_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 92 | if (!rpc_handle) { |
| 93 | EMSG("Could not get handle"); |
| 94 | return PSA_ERROR_GENERIC_ERROR; |
| 95 | } |
| 96 | |
| 97 | req->header.seq_num = seq_num; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 98 | /* No need to distinguish callers (currently concurrent calls are not supported). */ |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 99 | req->header.client_id = client_id; |
| 100 | req->header.protocol_ver = protocol_ver; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 101 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 102 | psa_status = rse_protocol_serialize_msg(handle, type, in_vec, in_len, out_vec, out_len, req, |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 103 | &req_len); |
| 104 | if (psa_status != PSA_SUCCESS) { |
| 105 | EMSG("Serialize msg failed: %d", psa_status); |
| 106 | return psa_status; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 109 | DMSG("Sending rse_comms message"); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 110 | DMSG("protocol_ver=%u", req->header.protocol_ver); |
| 111 | DMSG("seq_num=%u", req->header.seq_num); |
| 112 | DMSG("client_id=%u", req->header.client_id); |
| 113 | |
| 114 | resp_len = sizeof(*reply); |
| 115 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 116 | 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] | 117 | if (rpc_status != RPC_SUCCESS) { |
| 118 | EMSG("Invoke failed: %d", rpc_status); |
| 119 | return PSA_ERROR_GENERIC_ERROR; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 122 | DMSG("Received rse_comms reply"); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 123 | DMSG("protocol_ver=%u", reply->header.protocol_ver); |
| 124 | DMSG("seq_num=%u", reply->header.seq_num); |
| 125 | DMSG("client_id=%u", reply->header.client_id); |
| 126 | DMSG("resp_len=%lu", resp_len); |
| 127 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 128 | psa_status = rse_protocol_deserialize_reply(out_vec, out_len, &return_val, reply, resp_len); |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 129 | if (psa_status != PSA_SUCCESS) { |
| 130 | EMSG("Protocol deserialize reply failed: %d", psa_status); |
| 131 | return psa_status; |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 134 | DMSG("Return_val=%d", return_val); |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 135 | |
Gyorgy Szing | e6b3846 | 2024-10-24 17:36:45 +0200 | [diff] [blame^] | 136 | rse_comms_caller_end(rpc_handle); |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 137 | |
| 138 | seq_num++; |
| 139 | |
| 140 | return return_val; |
| 141 | } |
| 142 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 143 | psa_status_t psa_call_client_id(struct rpc_caller_interface *caller, psa_handle_t psa_handle, |
| 144 | int32_t client_id, int32_t type, const struct psa_invec *in_vec, |
| 145 | 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] | 146 | { |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 147 | return __psa_call(caller, psa_handle, client_id, type, in_vec, in_len, out_vec, out_len); |
| 148 | } |
Bence Balogh | 0c582be | 2024-01-11 22:21:31 +0100 | [diff] [blame] | 149 | |
Bence Balogh | db87226 | 2024-02-22 16:59:35 +0100 | [diff] [blame] | 150 | psa_status_t psa_call(struct rpc_caller_interface *caller, psa_handle_t psa_handle, int32_t type, |
| 151 | const struct psa_invec *in_vec, size_t in_len, struct psa_outvec *out_vec, |
| 152 | size_t out_len) |
| 153 | { |
| 154 | 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] | 155 | } |