blob: ba07ce15800638cb5dcff716998d12a2058c0729 [file] [log] [blame]
Imre Kisc674b5b2021-02-09 19:05:27 +01001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
4 */
5
6#include "ffa_api.h"
7#include "sp_api_defines.h"
8#include "sp_messaging.h"
Imre Kisbe97e772021-02-25 17:56:19 +01009#if FFA_DIRECT_MSG_ROUTING_EXTENSION
10#include "ffa_direct_msg_routing_extension.h"
11#endif
Imre Kisc674b5b2021-02-09 19:05:27 +010012
13#include <string.h>
14
15#define SP_MSG_ARG_OFFSET (1)
16
17static void pack_ffa_direct_msg(const struct sp_msg *msg,
18 struct ffa_direct_msg *ffa_msg)
19{
20 uint32_t i = 0;
21
22 ffa_msg->source_id = msg->source_id;
23 ffa_msg->destination_id = msg->destination_id;
24
25 ffa_msg->args[0] = 0;
26 memcpy(&ffa_msg->args[SP_MSG_ARG_OFFSET], msg->args, sizeof(msg->args));
27}
28
29static void unpack_ffa_direct_msg(const struct ffa_direct_msg *ffa_msg,
30 struct sp_msg *msg)
31{
32 uint32_t i = 0;
33
34 if (ffa_msg->function_id != FFA_SUCCESS_32) {
35 /*
36 * Handling request or response (error is handled before call)
37 */
38 msg->source_id = ffa_msg->source_id;
39 msg->destination_id = ffa_msg->destination_id;
40
41 memcpy(msg->args, &ffa_msg->args[SP_MSG_ARG_OFFSET],
42 sizeof(msg->args));
43 } else {
44 /* Success has no message parameters */
45 *msg = (struct sp_msg){ 0 };
46 }
47}
48
49sp_result sp_msg_wait(struct sp_msg *msg)
50{
51 ffa_result ffa_res = FFA_OK;
52 struct ffa_direct_msg ffa_msg = { 0 };
53
54 if (!msg)
55 return SP_RESULT_INVALID_PARAMETERS;
56
57 ffa_res = ffa_msg_wait(&ffa_msg);
58 if (ffa_res != FFA_OK) {
59 *msg = (struct sp_msg){ 0 };
60 return SP_RESULT_FFA(ffa_res);
61 }
62
Imre Kisbe97e772021-02-25 17:56:19 +010063#if FFA_DIRECT_MSG_ROUTING_EXTENSION
64 ffa_res = ffa_direct_msg_routing_ext_wait_post_hook(&ffa_msg);
65 if (ffa_res != FFA_OK) {
66 *msg = (struct sp_msg){ 0 };
67 return SP_RESULT_FFA(ffa_res);
68 }
69#endif
70
Imre Kisc674b5b2021-02-09 19:05:27 +010071 unpack_ffa_direct_msg(&ffa_msg, msg);
72
73 return SP_RESULT_OK;
74}
75
76sp_result sp_msg_send_direct_req(const struct sp_msg *req, struct sp_msg *resp)
77{
78 ffa_result ffa_res = FFA_OK;
79 struct ffa_direct_msg ffa_req = { 0 };
80 struct ffa_direct_msg ffa_resp = { 0 };
81
82 if (!resp)
83 return SP_RESULT_INVALID_PARAMETERS;
84
85 if (!req) {
86 *resp = (struct sp_msg){ 0 };
87 return SP_RESULT_INVALID_PARAMETERS;
88 }
89
90 pack_ffa_direct_msg(req, &ffa_req);
91
Imre Kisbe97e772021-02-25 17:56:19 +010092#if FFA_DIRECT_MSG_ROUTING_EXTENSION
93 ffa_direct_msg_routing_ext_req_pre_hook(&ffa_req);
94#endif
95
Imre Kisc674b5b2021-02-09 19:05:27 +010096 ffa_res = ffa_msg_send_direct_req(ffa_req.source_id,
97 ffa_req.destination_id,
98 ffa_req.args[0], ffa_req.args[1],
99 ffa_req.args[2], ffa_req.args[3],
100 ffa_req.args[4], &ffa_resp);
101
102 if (ffa_res != FFA_OK) {
Imre Kisbe97e772021-02-25 17:56:19 +0100103#if FFA_DIRECT_MSG_ROUTING_EXTENSION
104 ffa_direct_msg_routing_ext_req_error_hook();
105#endif
Imre Kisc674b5b2021-02-09 19:05:27 +0100106 *resp = (struct sp_msg){ 0 };
107 return SP_RESULT_FFA(ffa_res);
108 }
109
Imre Kisbe97e772021-02-25 17:56:19 +0100110#if FFA_DIRECT_MSG_ROUTING_EXTENSION
111 ffa_res = ffa_direct_msg_routing_ext_req_post_hook(&ffa_resp);
112 if (ffa_res != SP_RESULT_OK) {
113 *resp = (struct sp_msg){ 0 };
114 return SP_RESULT_FFA(ffa_res);
115 }
116#endif
117
Imre Kisc674b5b2021-02-09 19:05:27 +0100118 unpack_ffa_direct_msg(&ffa_resp, resp);
119
120 return SP_RESULT_OK;
121}
122
123sp_result sp_msg_send_direct_resp(const struct sp_msg *resp, struct sp_msg *req)
124{
125 ffa_result ffa_res = FFA_OK;
126 struct ffa_direct_msg ffa_resp = { 0 };
127 struct ffa_direct_msg ffa_req = { 0 };
128
129 if (!req)
130 return SP_RESULT_INVALID_PARAMETERS;
131
132 if (!resp) {
133 *req = (struct sp_msg){ 0 };
134 return SP_RESULT_INVALID_PARAMETERS;
135 }
136
137 pack_ffa_direct_msg(resp, &ffa_resp);
138
Imre Kisbe97e772021-02-25 17:56:19 +0100139#if FFA_DIRECT_MSG_ROUTING_EXTENSION
140 ffa_direct_msg_routing_ext_resp_pre_hook(&ffa_resp);
141#endif
142
Imre Kisc674b5b2021-02-09 19:05:27 +0100143 ffa_res = ffa_msg_send_direct_resp(ffa_resp.source_id,
144 ffa_resp.destination_id,
145 ffa_resp.args[0], ffa_resp.args[1],
146 ffa_resp.args[2], ffa_resp.args[3],
147 ffa_resp.args[4], &ffa_req);
148
149 if (ffa_res != FFA_OK) {
Imre Kisbe97e772021-02-25 17:56:19 +0100150#if FFA_DIRECT_MSG_ROUTING_EXTENSION
151 ffa_direct_msg_routing_ext_resp_error_hook();
152#endif
Imre Kisc674b5b2021-02-09 19:05:27 +0100153 *req = (struct sp_msg){ 0 };
154 return SP_RESULT_FFA(ffa_res);
155 }
156
Imre Kisbe97e772021-02-25 17:56:19 +0100157#if FFA_DIRECT_MSG_ROUTING_EXTENSION
158 ffa_res = ffa_direct_msg_routing_ext_resp_post_hook(&ffa_req);
159 if (ffa_res != SP_RESULT_OK) {
160 *req = (struct sp_msg){ 0 };
161 return SP_RESULT_FFA(ffa_res);
162 }
163#endif
164
Imre Kisc674b5b2021-02-09 19:05:27 +0100165 unpack_ffa_direct_msg(&ffa_req, req);
166
167 return SP_RESULT_OK;
168}
Imre Kisbe97e772021-02-25 17:56:19 +0100169
170#if FFA_DIRECT_MSG_ROUTING_EXTENSION
171sp_result sp_msg_send_rc_req(const struct sp_msg *req, struct sp_msg *resp)
172{
173 ffa_result ffa_res = FFA_OK;
174 struct ffa_direct_msg ffa_req = { 0 };
175 struct ffa_direct_msg ffa_resp = { 0 };
176
177 if (!resp)
178 return SP_RESULT_INVALID_PARAMETERS;
179
180 if (!req) {
181 *resp = (struct sp_msg){ 0 };
182 return SP_RESULT_INVALID_PARAMETERS;
183 }
184
185 pack_ffa_direct_msg(req, &ffa_req);
186
187 ffa_direct_msg_routing_ext_rc_req_pre_hook(&ffa_req);
188
189 ffa_res = ffa_msg_send_direct_resp(ffa_req.source_id,
190 ffa_req.destination_id,
191 ffa_req.args[0], ffa_req.args[1],
192 ffa_req.args[2], ffa_req.args[3],
193 ffa_req.args[4], &ffa_resp);
194
195 if (ffa_res != FFA_OK) {
196 ffa_direct_msg_routing_ext_rc_req_error_hook();
197 *resp = (struct sp_msg){ 0 };
198 return SP_RESULT_FFA(ffa_res);
199 }
200
201 ffa_res = ffa_direct_msg_routing_ext_rc_req_post_hook(&ffa_resp);
202 if (ffa_res != SP_RESULT_OK) {
203 *resp = (struct sp_msg){ 0 };
204 return SP_RESULT_FFA(ffa_res);
205 }
206
207 unpack_ffa_direct_msg(&ffa_resp, resp);
208
209 return SP_RESULT_OK;
210}
211#endif