aboutsummaryrefslogtreecommitdiff
path: root/components/rpc/ffarpc/endpoint/ffarpc_call_ep.c
blob: 99a50566400be7b7df723f252b94c29d781203ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "ffarpc_call_args.h"
#include "ffarpc_call_ep.h"
#include "ffarpc_call_ops.h"
#include <ffa_api.h>
#include <sp_memory_management.h>
#include <protocols/rpc/common/packed-c/status.h>
#include <trace.h>
#include <stddef.h>

/* TODO: remove this when own ID will be available in libsp */
extern uint16_t own_id;

static void set_resp_args(uint32_t *resp_args, uint32_t ifaceid_opcode, uint32_t data_len,
			  rpc_status_t rpc_status, uint32_t opstatus)
{
	resp_args[FFA_CALL_ARGS_IFACE_ID_OPCODE] = ifaceid_opcode;
	resp_args[FFA_CALL_ARGS_RESP_DATA_LEN] = data_len;
	resp_args[FFA_CALL_ARGS_RESP_RPC_STATUS] = rpc_status;
	resp_args[FFA_CALL_ARGS_RESP_OP_STATUS] = opstatus;
}

static void set_mgmt_resp_args(uint32_t *resp_args, uint32_t ifaceid_opcode,
			       rpc_status_t rpc_status)
{
	/*
	 * Sets arguments for responses that originate from the ffa_call_ep
	 * rather than from a higher layer service. These responses are not
	 * associated with a shared buffer for any additional message payload.
	 */
	set_resp_args(resp_args, ifaceid_opcode, 0, rpc_status, 0);
}

static int find_free_shm(struct ffa_call_ep *call_ep)
{
	int i;

	if (!call_ep)
		return -1;

	for (i = 0; i < NUM_MAX_SESS; i++)
		if (!call_ep->shmem_buf[i]) {
			DMSG("shm slot %u allocated for %p", i, call_ep);
			return i;
		}

	EMSG("shm slot allocation failed");
	return -1;
}

static int find_shm(struct ffa_call_ep *call_ep, uint16_t source_id)
{
	int i;

	if (!call_ep)
		return -1;

	for (i = 0; i < NUM_MAX_SESS; i++)
		if (call_ep->src_id[i] == source_id)
			return i;

	EMSG("shm not found for source 0x%x", source_id);
	return -1;
}

static void init_shmem_buf(struct ffa_call_ep *call_ep, uint16_t source_id,
			   const uint32_t *req_args, uint32_t *resp_args)
{
	sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
	struct sp_memory_descriptor desc = { };
	struct sp_memory_access_descriptor acc_desc = { };
	struct sp_memory_region region = { };
	uint32_t in_region_count = 1;
	uint32_t out_region_count = 1;
	uint64_t handle = 0;
	rpc_status_t rpc_status = TS_RPC_ERROR_INTERNAL;
	int idx = find_free_shm(call_ep);

	if (idx < 0) {
		EMSG("shm init error");
		goto out;
	}

	desc.sender_id = source_id;
	desc.memory_type = sp_memory_type_not_specified;
	desc.flags.transaction_type = sp_memory_transaction_type_share;
	acc_desc.receiver_id = own_id;
	acc_desc.data_access = sp_data_access_read_write;
	handle = req_args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW];
	handle = (handle << 32) | req_args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW];

	sp_res = sp_memory_retrieve(&desc, &acc_desc, &region, in_region_count,
				    &out_region_count, handle);

	if (sp_res == SP_RESULT_OK) {
		call_ep->shmem_buf[idx] = region.address;
		call_ep->shmem_buf_handle[idx] = handle;
		call_ep->shmem_buf_size[idx] = (size_t)req_args[FFA_CALL_ARGS_SHARE_MEM_SIZE];
		call_ep->src_id[idx] = source_id;
		rpc_status = TS_RPC_CALL_ACCEPTED;
	} else {
		EMSG("memory retrieve error: %d", sp_res);
	}

out:
	set_mgmt_resp_args(resp_args, req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE], rpc_status);
}

static void deinit_shmem_buf(struct ffa_call_ep *call_ep, uint16_t source_id,
			     const uint32_t *req_args, uint32_t *resp_args)
{
	sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
	rpc_status_t rpc_status = TS_RPC_ERROR_INTERNAL;
	uint64_t handle;
	uint16_t endpoints[1] = { own_id };
	uint32_t endpoint_cnt = 1;
	struct sp_memory_transaction_flags flags = {
		.zero_memory = false,
		.operation_time_slicing = false,
	};
	int idx = find_shm(call_ep, source_id);

	if (idx < 0) {
		EMSG("shm deinit error");
		goto out;
	}

	handle = call_ep->shmem_buf_handle[idx];

	sp_res = sp_memory_relinquish(handle, endpoints, endpoint_cnt, &flags);
	if (sp_res == SP_RESULT_OK) {
		call_ep->shmem_buf[idx] = NULL;
		call_ep->shmem_buf_handle[idx] = 0;
		call_ep->shmem_buf_size[idx] = 0;
		call_ep->src_id[idx] = 0xffff;
		rpc_status = TS_RPC_CALL_ACCEPTED;
	} else {
		EMSG("memory relinquish error: %d", sp_res);
	}

out:
	set_mgmt_resp_args(resp_args, req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE], rpc_status);
}

static void handle_service_msg(struct ffa_call_ep *call_ep, uint16_t source_id,
			       const uint32_t *req_args, uint32_t *resp_args)
{
	rpc_status_t rpc_status = TS_RPC_ERROR_INTERNAL;
	struct call_req call_req;

	uint32_t ifaceid_opcode = req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE];
	int idx = find_shm(call_ep, source_id);

	if (idx < 0) {
		EMSG("handle service msg error");
		goto out;
	}

	call_req.caller_id = source_id;
	call_req.interface_id = FFA_CALL_ARGS_EXTRACT_IFACE(ifaceid_opcode);
	call_req.opcode = FFA_CALL_ARGS_EXTRACT_OPCODE(ifaceid_opcode);
	call_req.encoding = req_args[FFA_CALL_ARGS_ENCODING];

	call_req.req_buf.data = call_ep->shmem_buf[idx];
	call_req.req_buf.data_len = req_args[FFA_CALL_ARGS_REQ_DATA_LEN];
	call_req.req_buf.size = call_ep->shmem_buf_size[idx];

	call_req.resp_buf.data = call_ep->shmem_buf[idx];
	call_req.resp_buf.data_len = 0;
	call_req.resp_buf.size = call_ep->shmem_buf_size[idx];

	rpc_status = rpc_interface_receive(call_ep->iface, &call_req);

out:
	set_resp_args(resp_args,
		      ifaceid_opcode,
		      call_req.resp_buf.data_len,
		      rpc_status,
		      call_req.opstatus);
}

static void handle_mgmt_msg(struct ffa_call_ep *call_ep, uint16_t source_id,
			    const uint32_t *req_args, uint32_t *resp_args)
{
	uint32_t ifaceid_opcode = req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE];
	uint32_t opcode = FFA_CALL_ARGS_EXTRACT_OPCODE(ifaceid_opcode);

	switch (opcode) {
	case FFA_CALL_OPCODE_SHARE_BUF:
		init_shmem_buf(call_ep, source_id, req_args, resp_args);
		break;
	case FFA_CALL_OPCODE_UNSHARE_BUF:
		deinit_shmem_buf(call_ep, source_id, req_args, resp_args);
		break;
	default:
		set_mgmt_resp_args(resp_args, ifaceid_opcode, TS_RPC_ERROR_INVALID_OPCODE);
		break;
	}
}

void ffa_call_ep_init(struct ffa_call_ep *ffa_call_ep, struct rpc_interface *iface)
{
	int i;

	ffa_call_ep->iface = iface;

	for (i = 0; i < NUM_MAX_SESS; i++) {
		ffa_call_ep->shmem_buf_handle[i] = 0;
		ffa_call_ep->shmem_buf_size[i] = 0;
		ffa_call_ep->shmem_buf[i] = NULL;
		ffa_call_ep->src_id[i] = 0xffff;
	}
}

void ffa_call_ep_receive(struct ffa_call_ep *call_ep,
			 const struct ffa_direct_msg *req_msg,
			 struct ffa_direct_msg *resp_msg)
{
	const uint32_t *req_args = req_msg->args;
	uint32_t *resp_args = resp_msg->args;
	int idx;

	uint16_t source_id = req_msg->source_id;
	uint32_t ifaceid_opcode = req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE];

	if (FFA_CALL_ARGS_EXTRACT_IFACE(ifaceid_opcode) == FFA_CALL_MGMT_IFACE_ID) {
		/* It's an RPC layer management request */
		handle_mgmt_msg(call_ep, source_id, req_args, resp_args);
	} else {
		/*
		 * Assume anything else is a service request. Service requests
		 * rely on a buffer being shared from the requesting client.
		 * If it hasn't been set-up, fail the request.
		 */
		idx = find_shm(call_ep, source_id);

		if (idx >= 0 && call_ep->shmem_buf[idx]) {
			handle_service_msg(call_ep, source_id, req_args, resp_args);
		} else {
			EMSG("shared buffer not found or NULL");
			set_mgmt_resp_args(resp_args, ifaceid_opcode, TS_RPC_ERROR_NOT_READY);
		}
	}
}