signal: Document glibc's si_code of SI_ASYNCNL
[cris-mirror.git] / drivers / tee / optee / call.c
blobf7b7b404c990bf41e60daa3a963cd14e85f3aafd
1 /*
2 * Copyright (c) 2015, Linaro Limited
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/arm-smccc.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/tee_drv.h>
20 #include <linux/types.h>
21 #include <linux/uaccess.h>
22 #include "optee_private.h"
23 #include "optee_smc.h"
25 struct optee_call_waiter {
26 struct list_head list_node;
27 struct completion c;
30 static void optee_cq_wait_init(struct optee_call_queue *cq,
31 struct optee_call_waiter *w)
34 * We're preparing to make a call to secure world. In case we can't
35 * allocate a thread in secure world we'll end up waiting in
36 * optee_cq_wait_for_completion().
38 * Normally if there's no contention in secure world the call will
39 * complete and we can cleanup directly with optee_cq_wait_final().
41 mutex_lock(&cq->mutex);
44 * We add ourselves to the queue, but we don't wait. This
45 * guarantees that we don't lose a completion if secure world
46 * returns busy and another thread just exited and try to complete
47 * someone.
49 init_completion(&w->c);
50 list_add_tail(&w->list_node, &cq->waiters);
52 mutex_unlock(&cq->mutex);
55 static void optee_cq_wait_for_completion(struct optee_call_queue *cq,
56 struct optee_call_waiter *w)
58 wait_for_completion(&w->c);
60 mutex_lock(&cq->mutex);
62 /* Move to end of list to get out of the way for other waiters */
63 list_del(&w->list_node);
64 reinit_completion(&w->c);
65 list_add_tail(&w->list_node, &cq->waiters);
67 mutex_unlock(&cq->mutex);
70 static void optee_cq_complete_one(struct optee_call_queue *cq)
72 struct optee_call_waiter *w;
74 list_for_each_entry(w, &cq->waiters, list_node) {
75 if (!completion_done(&w->c)) {
76 complete(&w->c);
77 break;
82 static void optee_cq_wait_final(struct optee_call_queue *cq,
83 struct optee_call_waiter *w)
86 * We're done with the call to secure world. The thread in secure
87 * world that was used for this call is now available for some
88 * other task to use.
90 mutex_lock(&cq->mutex);
92 /* Get out of the list */
93 list_del(&w->list_node);
95 /* Wake up one eventual waiting task */
96 optee_cq_complete_one(cq);
99 * If we're completed we've got a completion from another task that
100 * was just done with its call to secure world. Since yet another
101 * thread now is available in secure world wake up another eventual
102 * waiting task.
104 if (completion_done(&w->c))
105 optee_cq_complete_one(cq);
107 mutex_unlock(&cq->mutex);
110 /* Requires the filpstate mutex to be held */
111 static struct optee_session *find_session(struct optee_context_data *ctxdata,
112 u32 session_id)
114 struct optee_session *sess;
116 list_for_each_entry(sess, &ctxdata->sess_list, list_node)
117 if (sess->session_id == session_id)
118 return sess;
120 return NULL;
124 * optee_do_call_with_arg() - Do an SMC to OP-TEE in secure world
125 * @ctx: calling context
126 * @parg: physical address of message to pass to secure world
128 * Does and SMC to OP-TEE in secure world and handles eventual resulting
129 * Remote Procedure Calls (RPC) from OP-TEE.
131 * Returns return code from secure world, 0 is OK
133 u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg)
135 struct optee *optee = tee_get_drvdata(ctx->teedev);
136 struct optee_call_waiter w;
137 struct optee_rpc_param param = { };
138 u32 ret;
140 param.a0 = OPTEE_SMC_CALL_WITH_ARG;
141 reg_pair_from_64(&param.a1, &param.a2, parg);
142 /* Initialize waiter */
143 optee_cq_wait_init(&optee->call_queue, &w);
144 while (true) {
145 struct arm_smccc_res res;
147 optee->invoke_fn(param.a0, param.a1, param.a2, param.a3,
148 param.a4, param.a5, param.a6, param.a7,
149 &res);
151 if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
153 * Out of threads in secure world, wait for a thread
154 * become available.
156 optee_cq_wait_for_completion(&optee->call_queue, &w);
157 } else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
158 param.a0 = res.a0;
159 param.a1 = res.a1;
160 param.a2 = res.a2;
161 param.a3 = res.a3;
162 optee_handle_rpc(ctx, &param);
163 } else {
164 ret = res.a0;
165 break;
170 * We're done with our thread in secure world, if there's any
171 * thread waiters wake up one.
173 optee_cq_wait_final(&optee->call_queue, &w);
175 return ret;
178 static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params,
179 struct optee_msg_arg **msg_arg,
180 phys_addr_t *msg_parg)
182 int rc;
183 struct tee_shm *shm;
184 struct optee_msg_arg *ma;
186 shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
187 TEE_SHM_MAPPED);
188 if (IS_ERR(shm))
189 return shm;
191 ma = tee_shm_get_va(shm, 0);
192 if (IS_ERR(ma)) {
193 rc = PTR_ERR(ma);
194 goto out;
197 rc = tee_shm_get_pa(shm, 0, msg_parg);
198 if (rc)
199 goto out;
201 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
202 ma->num_params = num_params;
203 *msg_arg = ma;
204 out:
205 if (rc) {
206 tee_shm_free(shm);
207 return ERR_PTR(rc);
210 return shm;
213 int optee_open_session(struct tee_context *ctx,
214 struct tee_ioctl_open_session_arg *arg,
215 struct tee_param *param)
217 struct optee_context_data *ctxdata = ctx->data;
218 int rc;
219 struct tee_shm *shm;
220 struct optee_msg_arg *msg_arg;
221 phys_addr_t msg_parg;
222 struct optee_session *sess = NULL;
224 /* +2 for the meta parameters added below */
225 shm = get_msg_arg(ctx, arg->num_params + 2, &msg_arg, &msg_parg);
226 if (IS_ERR(shm))
227 return PTR_ERR(shm);
229 msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
230 msg_arg->cancel_id = arg->cancel_id;
233 * Initialize and add the meta parameters needed when opening a
234 * session.
236 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
237 OPTEE_MSG_ATTR_META;
238 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
239 OPTEE_MSG_ATTR_META;
240 memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
241 memcpy(&msg_arg->params[1].u.value, arg->uuid, sizeof(arg->clnt_uuid));
242 msg_arg->params[1].u.value.c = arg->clnt_login;
244 rc = optee_to_msg_param(msg_arg->params + 2, arg->num_params, param);
245 if (rc)
246 goto out;
248 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
249 if (!sess) {
250 rc = -ENOMEM;
251 goto out;
254 if (optee_do_call_with_arg(ctx, msg_parg)) {
255 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
256 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
259 if (msg_arg->ret == TEEC_SUCCESS) {
260 /* A new session has been created, add it to the list. */
261 sess->session_id = msg_arg->session;
262 mutex_lock(&ctxdata->mutex);
263 list_add(&sess->list_node, &ctxdata->sess_list);
264 mutex_unlock(&ctxdata->mutex);
265 } else {
266 kfree(sess);
269 if (optee_from_msg_param(param, arg->num_params, msg_arg->params + 2)) {
270 arg->ret = TEEC_ERROR_COMMUNICATION;
271 arg->ret_origin = TEEC_ORIGIN_COMMS;
272 /* Close session again to avoid leakage */
273 optee_close_session(ctx, msg_arg->session);
274 } else {
275 arg->session = msg_arg->session;
276 arg->ret = msg_arg->ret;
277 arg->ret_origin = msg_arg->ret_origin;
279 out:
280 tee_shm_free(shm);
282 return rc;
285 int optee_close_session(struct tee_context *ctx, u32 session)
287 struct optee_context_data *ctxdata = ctx->data;
288 struct tee_shm *shm;
289 struct optee_msg_arg *msg_arg;
290 phys_addr_t msg_parg;
291 struct optee_session *sess;
293 /* Check that the session is valid and remove it from the list */
294 mutex_lock(&ctxdata->mutex);
295 sess = find_session(ctxdata, session);
296 if (sess)
297 list_del(&sess->list_node);
298 mutex_unlock(&ctxdata->mutex);
299 if (!sess)
300 return -EINVAL;
301 kfree(sess);
303 shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
304 if (IS_ERR(shm))
305 return PTR_ERR(shm);
307 msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
308 msg_arg->session = session;
309 optee_do_call_with_arg(ctx, msg_parg);
311 tee_shm_free(shm);
312 return 0;
315 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
316 struct tee_param *param)
318 struct optee_context_data *ctxdata = ctx->data;
319 struct tee_shm *shm;
320 struct optee_msg_arg *msg_arg;
321 phys_addr_t msg_parg;
322 struct optee_session *sess;
323 int rc;
325 /* Check that the session is valid */
326 mutex_lock(&ctxdata->mutex);
327 sess = find_session(ctxdata, arg->session);
328 mutex_unlock(&ctxdata->mutex);
329 if (!sess)
330 return -EINVAL;
332 shm = get_msg_arg(ctx, arg->num_params, &msg_arg, &msg_parg);
333 if (IS_ERR(shm))
334 return PTR_ERR(shm);
335 msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
336 msg_arg->func = arg->func;
337 msg_arg->session = arg->session;
338 msg_arg->cancel_id = arg->cancel_id;
340 rc = optee_to_msg_param(msg_arg->params, arg->num_params, param);
341 if (rc)
342 goto out;
344 if (optee_do_call_with_arg(ctx, msg_parg)) {
345 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
346 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
349 if (optee_from_msg_param(param, arg->num_params, msg_arg->params)) {
350 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
351 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
354 arg->ret = msg_arg->ret;
355 arg->ret_origin = msg_arg->ret_origin;
356 out:
357 tee_shm_free(shm);
358 return rc;
361 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
363 struct optee_context_data *ctxdata = ctx->data;
364 struct tee_shm *shm;
365 struct optee_msg_arg *msg_arg;
366 phys_addr_t msg_parg;
367 struct optee_session *sess;
369 /* Check that the session is valid */
370 mutex_lock(&ctxdata->mutex);
371 sess = find_session(ctxdata, session);
372 mutex_unlock(&ctxdata->mutex);
373 if (!sess)
374 return -EINVAL;
376 shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
377 if (IS_ERR(shm))
378 return PTR_ERR(shm);
380 msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
381 msg_arg->session = session;
382 msg_arg->cancel_id = cancel_id;
383 optee_do_call_with_arg(ctx, msg_parg);
385 tee_shm_free(shm);
386 return 0;
390 * optee_enable_shm_cache() - Enables caching of some shared memory allocation
391 * in OP-TEE
392 * @optee: main service struct
394 void optee_enable_shm_cache(struct optee *optee)
396 struct optee_call_waiter w;
398 /* We need to retry until secure world isn't busy. */
399 optee_cq_wait_init(&optee->call_queue, &w);
400 while (true) {
401 struct arm_smccc_res res;
403 optee->invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
404 0, &res);
405 if (res.a0 == OPTEE_SMC_RETURN_OK)
406 break;
407 optee_cq_wait_for_completion(&optee->call_queue, &w);
409 optee_cq_wait_final(&optee->call_queue, &w);
413 * optee_disable_shm_cache() - Disables caching of some shared memory allocation
414 * in OP-TEE
415 * @optee: main service struct
417 void optee_disable_shm_cache(struct optee *optee)
419 struct optee_call_waiter w;
421 /* We need to retry until secure world isn't busy. */
422 optee_cq_wait_init(&optee->call_queue, &w);
423 while (true) {
424 union {
425 struct arm_smccc_res smccc;
426 struct optee_smc_disable_shm_cache_result result;
427 } res;
429 optee->invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
430 0, &res.smccc);
431 if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
432 break; /* All shm's freed */
433 if (res.result.status == OPTEE_SMC_RETURN_OK) {
434 struct tee_shm *shm;
436 shm = reg_pair_to_ptr(res.result.shm_upper32,
437 res.result.shm_lower32);
438 tee_shm_free(shm);
439 } else {
440 optee_cq_wait_for_completion(&optee->call_queue, &w);
443 optee_cq_wait_final(&optee->call_queue, &w);