blob: f7223aa7510cbcdcd837bf30e4c888724a675f66 [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{
Imre Kisc674b5b2021-02-09 19:05:27 +010020 ffa_msg->source_id = msg->source_id;
21 ffa_msg->destination_id = msg->destination_id;
22
23 ffa_msg->args[0] = 0;
24 memcpy(&ffa_msg->args[SP_MSG_ARG_OFFSET], msg->args, sizeof(msg->args));
25}
26
27static void unpack_ffa_direct_msg(const struct ffa_direct_msg *ffa_msg,
28 struct sp_msg *msg)
29{
Imre Kisc674b5b2021-02-09 19:05:27 +010030 if (ffa_msg->function_id != FFA_SUCCESS_32) {
31 /*
32 * Handling request or response (error is handled before call)
33 */
34 msg->source_id = ffa_msg->source_id;
35 msg->destination_id = ffa_msg->destination_id;
36
37 memcpy(msg->args, &ffa_msg->args[SP_MSG_ARG_OFFSET],
38 sizeof(msg->args));
39 } else {
40 /* Success has no message parameters */
41 *msg = (struct sp_msg){ 0 };
42 }
43}
44
45sp_result sp_msg_wait(struct sp_msg *msg)
46{
47 ffa_result ffa_res = FFA_OK;
48 struct ffa_direct_msg ffa_msg = { 0 };
49
50 if (!msg)
51 return SP_RESULT_INVALID_PARAMETERS;
52
53 ffa_res = ffa_msg_wait(&ffa_msg);
54 if (ffa_res != FFA_OK) {
55 *msg = (struct sp_msg){ 0 };
56 return SP_RESULT_FFA(ffa_res);
57 }
58
Imre Kisbe97e772021-02-25 17:56:19 +010059#if FFA_DIRECT_MSG_ROUTING_EXTENSION
60 ffa_res = ffa_direct_msg_routing_ext_wait_post_hook(&ffa_msg);
61 if (ffa_res != FFA_OK) {
62 *msg = (struct sp_msg){ 0 };
63 return SP_RESULT_FFA(ffa_res);
64 }
65#endif
66
Imre Kisc674b5b2021-02-09 19:05:27 +010067 unpack_ffa_direct_msg(&ffa_msg, msg);
68
69 return SP_RESULT_OK;
70}
71
72sp_result sp_msg_send_direct_req(const struct sp_msg *req, struct sp_msg *resp)
73{
74 ffa_result ffa_res = FFA_OK;
75 struct ffa_direct_msg ffa_req = { 0 };
76 struct ffa_direct_msg ffa_resp = { 0 };
77
78 if (!resp)
79 return SP_RESULT_INVALID_PARAMETERS;
80
81 if (!req) {
82 *resp = (struct sp_msg){ 0 };
83 return SP_RESULT_INVALID_PARAMETERS;
84 }
85
86 pack_ffa_direct_msg(req, &ffa_req);
87
Imre Kisbe97e772021-02-25 17:56:19 +010088#if FFA_DIRECT_MSG_ROUTING_EXTENSION
89 ffa_direct_msg_routing_ext_req_pre_hook(&ffa_req);
90#endif
91
Imre Kisc674b5b2021-02-09 19:05:27 +010092 ffa_res = ffa_msg_send_direct_req(ffa_req.source_id,
93 ffa_req.destination_id,
94 ffa_req.args[0], ffa_req.args[1],
95 ffa_req.args[2], ffa_req.args[3],
96 ffa_req.args[4], &ffa_resp);
97
98 if (ffa_res != FFA_OK) {
Imre Kisbe97e772021-02-25 17:56:19 +010099#if FFA_DIRECT_MSG_ROUTING_EXTENSION
100 ffa_direct_msg_routing_ext_req_error_hook();
101#endif
Imre Kisc674b5b2021-02-09 19:05:27 +0100102 *resp = (struct sp_msg){ 0 };
103 return SP_RESULT_FFA(ffa_res);
104 }
105
Imre Kisbe97e772021-02-25 17:56:19 +0100106#if FFA_DIRECT_MSG_ROUTING_EXTENSION
107 ffa_res = ffa_direct_msg_routing_ext_req_post_hook(&ffa_resp);
108 if (ffa_res != SP_RESULT_OK) {
109 *resp = (struct sp_msg){ 0 };
110 return SP_RESULT_FFA(ffa_res);
111 }
112#endif
113
Imre Kisc674b5b2021-02-09 19:05:27 +0100114 unpack_ffa_direct_msg(&ffa_resp, resp);
115
116 return SP_RESULT_OK;
117}
118
119sp_result sp_msg_send_direct_resp(const struct sp_msg *resp, struct sp_msg *req)
120{
121 ffa_result ffa_res = FFA_OK;
122 struct ffa_direct_msg ffa_resp = { 0 };
123 struct ffa_direct_msg ffa_req = { 0 };
124
125 if (!req)
126 return SP_RESULT_INVALID_PARAMETERS;
127
128 if (!resp) {
129 *req = (struct sp_msg){ 0 };
130 return SP_RESULT_INVALID_PARAMETERS;
131 }
132
133 pack_ffa_direct_msg(resp, &ffa_resp);
134
Imre Kisbe97e772021-02-25 17:56:19 +0100135#if FFA_DIRECT_MSG_ROUTING_EXTENSION
136 ffa_direct_msg_routing_ext_resp_pre_hook(&ffa_resp);
137#endif
138
Imre Kisc674b5b2021-02-09 19:05:27 +0100139 ffa_res = ffa_msg_send_direct_resp(ffa_resp.source_id,
140 ffa_resp.destination_id,
141 ffa_resp.args[0], ffa_resp.args[1],
142 ffa_resp.args[2], ffa_resp.args[3],
143 ffa_resp.args[4], &ffa_req);
144
145 if (ffa_res != FFA_OK) {
Imre Kisbe97e772021-02-25 17:56:19 +0100146#if FFA_DIRECT_MSG_ROUTING_EXTENSION
147 ffa_direct_msg_routing_ext_resp_error_hook();
148#endif
Imre Kisc674b5b2021-02-09 19:05:27 +0100149 *req = (struct sp_msg){ 0 };
150 return SP_RESULT_FFA(ffa_res);
151 }
152
Imre Kisbe97e772021-02-25 17:56:19 +0100153#if FFA_DIRECT_MSG_ROUTING_EXTENSION
154 ffa_res = ffa_direct_msg_routing_ext_resp_post_hook(&ffa_req);
155 if (ffa_res != SP_RESULT_OK) {
156 *req = (struct sp_msg){ 0 };
157 return SP_RESULT_FFA(ffa_res);
158 }
159#endif
160
Imre Kisc674b5b2021-02-09 19:05:27 +0100161 unpack_ffa_direct_msg(&ffa_req, req);
162
163 return SP_RESULT_OK;
164}
Imre Kisbe97e772021-02-25 17:56:19 +0100165
166#if FFA_DIRECT_MSG_ROUTING_EXTENSION
167sp_result sp_msg_send_rc_req(const struct sp_msg *req, struct sp_msg *resp)
168{
169 ffa_result ffa_res = FFA_OK;
170 struct ffa_direct_msg ffa_req = { 0 };
171 struct ffa_direct_msg ffa_resp = { 0 };
172
173 if (!resp)
174 return SP_RESULT_INVALID_PARAMETERS;
175
176 if (!req) {
177 *resp = (struct sp_msg){ 0 };
178 return SP_RESULT_INVALID_PARAMETERS;
179 }
180
181 pack_ffa_direct_msg(req, &ffa_req);
182
183 ffa_direct_msg_routing_ext_rc_req_pre_hook(&ffa_req);
184
185 ffa_res = ffa_msg_send_direct_resp(ffa_req.source_id,
186 ffa_req.destination_id,
187 ffa_req.args[0], ffa_req.args[1],
188 ffa_req.args[2], ffa_req.args[3],
189 ffa_req.args[4], &ffa_resp);
190
191 if (ffa_res != FFA_OK) {
192 ffa_direct_msg_routing_ext_rc_req_error_hook();
193 *resp = (struct sp_msg){ 0 };
194 return SP_RESULT_FFA(ffa_res);
195 }
196
197 ffa_res = ffa_direct_msg_routing_ext_rc_req_post_hook(&ffa_resp);
198 if (ffa_res != SP_RESULT_OK) {
199 *resp = (struct sp_msg){ 0 };
200 return SP_RESULT_FFA(ffa_res);
201 }
202
203 unpack_ffa_direct_msg(&ffa_resp, resp);
204
205 return SP_RESULT_OK;
206}
207#endif