1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2016, Linaro Limited
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/tee_drv.h>
13 #include "optee_private.h"
14 #include "optee_smc.h"
15 #include "optee_rpc_cmd.h"
18 struct list_head link;
23 void optee_wait_queue_init(struct optee_wait_queue *priv)
25 mutex_init(&priv->mu);
26 INIT_LIST_HEAD(&priv->db);
29 void optee_wait_queue_exit(struct optee_wait_queue *priv)
31 mutex_destroy(&priv->mu);
34 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
38 if (arg->num_params != 1)
40 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
41 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT)
44 ktime_get_real_ts64(&ts);
45 arg->params[0].u.value.a = ts.tv_sec;
46 arg->params[0].u.value.b = ts.tv_nsec;
48 arg->ret = TEEC_SUCCESS;
51 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
54 #if IS_REACHABLE(CONFIG_I2C)
55 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
56 struct optee_msg_arg *arg)
58 struct tee_param *params;
59 struct i2c_adapter *adapter;
60 struct i2c_msg msg = { };
62 int ret = -EOPNOTSUPP;
64 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT,
65 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT,
66 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT,
67 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT,
70 if (arg->num_params != ARRAY_SIZE(attr)) {
71 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
75 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
78 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
82 if (optee_from_msg_param(params, arg->num_params, arg->params))
85 for (i = 0; i < arg->num_params; i++) {
86 if (params[i].attr != attr[i])
90 adapter = i2c_get_adapter(params[0].u.value.b);
94 if (params[1].u.value.a & OPTEE_RPC_I2C_FLAGS_TEN_BIT) {
95 if (!i2c_check_functionality(adapter,
96 I2C_FUNC_10BIT_ADDR)) {
97 i2c_put_adapter(adapter);
101 msg.flags = I2C_M_TEN;
104 msg.addr = params[0].u.value.c;
105 msg.buf = params[2].u.memref.shm->kaddr;
106 msg.len = params[2].u.memref.size;
108 switch (params[0].u.value.a) {
109 case OPTEE_RPC_I2C_TRANSFER_RD:
110 msg.flags |= I2C_M_RD;
112 case OPTEE_RPC_I2C_TRANSFER_WR:
115 i2c_put_adapter(adapter);
119 ret = i2c_transfer(adapter, &msg, 1);
122 arg->ret = TEEC_ERROR_COMMUNICATION;
124 params[3].u.value.a = msg.len;
125 if (optee_to_msg_param(arg->params, arg->num_params, params))
126 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
128 arg->ret = TEEC_SUCCESS;
131 i2c_put_adapter(adapter);
136 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
139 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
140 struct optee_msg_arg *arg)
142 arg->ret = TEEC_ERROR_NOT_SUPPORTED;
146 static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key)
152 list_for_each_entry(w, &wq->db, link)
156 w = kmalloc(sizeof(*w), GFP_KERNEL);
158 init_completion(&w->c);
160 list_add_tail(&w->link, &wq->db);
163 mutex_unlock(&wq->mu);
167 static void wq_sleep(struct optee_wait_queue *wq, u32 key)
169 struct wq_entry *w = wq_entry_get(wq, key);
172 wait_for_completion(&w->c);
175 mutex_unlock(&wq->mu);
180 static void wq_wakeup(struct optee_wait_queue *wq, u32 key)
182 struct wq_entry *w = wq_entry_get(wq, key);
188 static void handle_rpc_func_cmd_wq(struct optee *optee,
189 struct optee_msg_arg *arg)
191 if (arg->num_params != 1)
194 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
195 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
198 switch (arg->params[0].u.value.a) {
199 case OPTEE_RPC_WAIT_QUEUE_SLEEP:
200 wq_sleep(&optee->wait_queue, arg->params[0].u.value.b);
202 case OPTEE_RPC_WAIT_QUEUE_WAKEUP:
203 wq_wakeup(&optee->wait_queue, arg->params[0].u.value.b);
209 arg->ret = TEEC_SUCCESS;
212 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
215 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
219 if (arg->num_params != 1)
222 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
223 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
226 msec_to_wait = arg->params[0].u.value.a;
228 /* Go to interruptible sleep */
229 msleep_interruptible(msec_to_wait);
231 arg->ret = TEEC_SUCCESS;
234 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
237 static void handle_rpc_supp_cmd(struct tee_context *ctx,
238 struct optee_msg_arg *arg)
240 struct tee_param *params;
242 arg->ret_origin = TEEC_ORIGIN_COMMS;
244 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
247 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
251 if (optee_from_msg_param(params, arg->num_params, arg->params)) {
252 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
256 arg->ret = optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params);
258 if (optee_to_msg_param(arg->params, arg->num_params, params))
259 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
264 static struct tee_shm *cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
267 struct tee_param param;
268 struct optee *optee = tee_get_drvdata(ctx->teedev);
271 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
272 param.u.value.a = OPTEE_RPC_SHM_TYPE_APPL;
273 param.u.value.b = sz;
276 ret = optee_supp_thrd_req(ctx, OPTEE_RPC_CMD_SHM_ALLOC, 1, ¶m);
278 return ERR_PTR(-ENOMEM);
280 mutex_lock(&optee->supp.mutex);
281 /* Increases count as secure world doesn't have a reference */
282 shm = tee_shm_get_from_id(optee->supp.ctx, param.u.value.c);
283 mutex_unlock(&optee->supp.mutex);
287 static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
288 struct optee_msg_arg *arg,
289 struct optee_call_ctx *call_ctx)
296 arg->ret_origin = TEEC_ORIGIN_COMMS;
298 if (!arg->num_params ||
299 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
300 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
304 for (n = 1; n < arg->num_params; n++) {
305 if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
306 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
311 sz = arg->params[0].u.value.b;
312 switch (arg->params[0].u.value.a) {
313 case OPTEE_RPC_SHM_TYPE_APPL:
314 shm = cmd_alloc_suppl(ctx, sz);
316 case OPTEE_RPC_SHM_TYPE_KERNEL:
317 shm = tee_shm_alloc(ctx, sz, TEE_SHM_MAPPED | TEE_SHM_PRIV);
320 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
325 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
329 if (tee_shm_get_pa(shm, 0, &pa)) {
330 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
334 sz = tee_shm_get_size(shm);
336 if (tee_shm_is_registered(shm)) {
341 pages = tee_shm_get_pages(shm, &page_num);
342 if (!pages || !page_num) {
343 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
347 pages_list = optee_allocate_pages_list(page_num);
349 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
353 call_ctx->pages_list = pages_list;
354 call_ctx->num_entries = page_num;
356 arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
357 OPTEE_MSG_ATTR_NONCONTIG;
359 * In the least bits of u.tmem.buf_ptr we store buffer offset
360 * from 4k page, as described in OP-TEE ABI.
362 arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
363 (tee_shm_get_page_offset(shm) &
364 (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
365 arg->params[0].u.tmem.size = tee_shm_get_size(shm);
366 arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
368 optee_fill_pages_list(pages_list, pages, page_num,
369 tee_shm_get_page_offset(shm));
371 arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
372 arg->params[0].u.tmem.buf_ptr = pa;
373 arg->params[0].u.tmem.size = sz;
374 arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
377 arg->ret = TEEC_SUCCESS;
383 static void cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm)
385 struct tee_param param;
387 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
388 param.u.value.a = OPTEE_RPC_SHM_TYPE_APPL;
389 param.u.value.b = tee_shm_get_id(shm);
393 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
394 * world has released its reference.
396 * It's better to do this before sending the request to supplicant
397 * as we'd like to let the process doing the initial allocation to
398 * do release the last reference too in order to avoid stacking
399 * many pending fput() on the client process. This could otherwise
400 * happen if secure world does many allocate and free in a single
405 optee_supp_thrd_req(ctx, OPTEE_RPC_CMD_SHM_FREE, 1, ¶m);
408 static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
409 struct optee_msg_arg *arg)
413 arg->ret_origin = TEEC_ORIGIN_COMMS;
415 if (arg->num_params != 1 ||
416 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
417 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
421 shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
422 switch (arg->params[0].u.value.a) {
423 case OPTEE_RPC_SHM_TYPE_APPL:
424 cmd_free_suppl(ctx, shm);
426 case OPTEE_RPC_SHM_TYPE_KERNEL:
430 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
432 arg->ret = TEEC_SUCCESS;
435 static void free_pages_list(struct optee_call_ctx *call_ctx)
437 if (call_ctx->pages_list) {
438 optee_free_pages_list(call_ctx->pages_list,
439 call_ctx->num_entries);
440 call_ctx->pages_list = NULL;
441 call_ctx->num_entries = 0;
445 void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx)
447 free_pages_list(call_ctx);
450 static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
452 struct optee_call_ctx *call_ctx)
454 struct optee_msg_arg *arg;
456 arg = tee_shm_get_va(shm, 0);
458 pr_err("%s: tee_shm_get_va %p failed\n", __func__, shm);
463 case OPTEE_RPC_CMD_GET_TIME:
464 handle_rpc_func_cmd_get_time(arg);
466 case OPTEE_RPC_CMD_WAIT_QUEUE:
467 handle_rpc_func_cmd_wq(optee, arg);
469 case OPTEE_RPC_CMD_SUSPEND:
470 handle_rpc_func_cmd_wait(arg);
472 case OPTEE_RPC_CMD_SHM_ALLOC:
473 free_pages_list(call_ctx);
474 handle_rpc_func_cmd_shm_alloc(ctx, arg, call_ctx);
476 case OPTEE_RPC_CMD_SHM_FREE:
477 handle_rpc_func_cmd_shm_free(ctx, arg);
479 case OPTEE_RPC_CMD_I2C_TRANSFER:
480 handle_rpc_func_cmd_i2c_transfer(ctx, arg);
483 handle_rpc_supp_cmd(ctx, arg);
488 * optee_handle_rpc() - handle RPC from secure world
489 * @ctx: context doing the RPC
490 * @param: value of registers for the RPC
491 * @call_ctx: call context. Preserved during one OP-TEE invocation
493 * Result of RPC is written back into @param.
495 void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
496 struct optee_call_ctx *call_ctx)
498 struct tee_device *teedev = ctx->teedev;
499 struct optee *optee = tee_get_drvdata(teedev);
503 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
504 case OPTEE_SMC_RPC_FUNC_ALLOC:
505 shm = tee_shm_alloc(ctx, param->a1,
506 TEE_SHM_MAPPED | TEE_SHM_PRIV);
507 if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
508 reg_pair_from_64(¶m->a1, ¶m->a2, pa);
509 reg_pair_from_64(¶m->a4, ¶m->a5,
518 case OPTEE_SMC_RPC_FUNC_FREE:
519 shm = reg_pair_to_ptr(param->a1, param->a2);
522 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
524 * A foreign interrupt was raised while secure world was
525 * executing, since they are handled in Linux a dummy RPC is
526 * performed to let Linux take the interrupt through the normal
530 case OPTEE_SMC_RPC_FUNC_CMD:
531 shm = reg_pair_to_ptr(param->a1, param->a2);
532 handle_rpc_func_cmd(ctx, optee, shm, call_ctx);
535 pr_warn("Unknown RPC func 0x%x\n",
536 (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
540 param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;