1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2021, Linaro Limited
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/tee_drv.h>
12 #include "optee_private.h"
13 #include "optee_rpc_cmd.h"
15 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
19 if (arg->num_params != 1)
21 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
22 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT)
25 ktime_get_real_ts64(&ts);
26 arg->params[0].u.value.a = ts.tv_sec;
27 arg->params[0].u.value.b = ts.tv_nsec;
29 arg->ret = TEEC_SUCCESS;
32 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
35 #if IS_REACHABLE(CONFIG_I2C)
36 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
37 struct optee_msg_arg *arg)
39 struct optee *optee = tee_get_drvdata(ctx->teedev);
40 struct tee_param *params;
41 struct i2c_adapter *adapter;
42 struct i2c_msg msg = { };
44 int ret = -EOPNOTSUPP;
46 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT,
47 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT,
48 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT,
49 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT,
52 if (arg->num_params != ARRAY_SIZE(attr)) {
53 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
57 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
60 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
64 if (optee->ops->from_msg_param(optee, params, arg->num_params,
68 for (i = 0; i < arg->num_params; i++) {
69 if (params[i].attr != attr[i])
73 adapter = i2c_get_adapter(params[0].u.value.b);
77 if (params[1].u.value.a & OPTEE_RPC_I2C_FLAGS_TEN_BIT) {
78 if (!i2c_check_functionality(adapter,
79 I2C_FUNC_10BIT_ADDR)) {
80 i2c_put_adapter(adapter);
84 msg.flags = I2C_M_TEN;
87 msg.addr = params[0].u.value.c;
88 msg.buf = params[2].u.memref.shm->kaddr;
89 msg.len = params[2].u.memref.size;
91 switch (params[0].u.value.a) {
92 case OPTEE_RPC_I2C_TRANSFER_RD:
93 msg.flags |= I2C_M_RD;
95 case OPTEE_RPC_I2C_TRANSFER_WR:
98 i2c_put_adapter(adapter);
102 ret = i2c_transfer(adapter, &msg, 1);
105 arg->ret = TEEC_ERROR_COMMUNICATION;
107 params[3].u.value.a = msg.len;
108 if (optee->ops->to_msg_param(optee, arg->params,
109 arg->num_params, params))
110 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
112 arg->ret = TEEC_SUCCESS;
115 i2c_put_adapter(adapter);
120 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
123 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
124 struct optee_msg_arg *arg)
126 arg->ret = TEEC_ERROR_NOT_SUPPORTED;
130 static void handle_rpc_func_cmd_wq(struct optee *optee,
131 struct optee_msg_arg *arg)
133 if (arg->num_params != 1)
136 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
137 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
140 switch (arg->params[0].u.value.a) {
141 case OPTEE_RPC_NOTIFICATION_WAIT:
142 if (optee_notif_wait(optee, arg->params[0].u.value.b))
145 case OPTEE_RPC_NOTIFICATION_SEND:
146 if (optee_notif_send(optee, arg->params[0].u.value.b))
153 arg->ret = TEEC_SUCCESS;
156 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
159 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
163 if (arg->num_params != 1)
166 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
167 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
170 msec_to_wait = arg->params[0].u.value.a;
172 /* Go to interruptible sleep */
173 msleep_interruptible(msec_to_wait);
175 arg->ret = TEEC_SUCCESS;
178 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
181 static void handle_rpc_supp_cmd(struct tee_context *ctx, struct optee *optee,
182 struct optee_msg_arg *arg)
184 struct tee_param *params;
186 arg->ret_origin = TEEC_ORIGIN_COMMS;
188 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
191 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
195 if (optee->ops->from_msg_param(optee, params, arg->num_params,
197 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
201 arg->ret = optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params);
203 if (optee->ops->to_msg_param(optee, arg->params, arg->num_params,
205 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
210 struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
213 struct tee_param param;
214 struct optee *optee = tee_get_drvdata(ctx->teedev);
217 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
218 param.u.value.a = OPTEE_RPC_SHM_TYPE_APPL;
219 param.u.value.b = sz;
222 ret = optee_supp_thrd_req(ctx, OPTEE_RPC_CMD_SHM_ALLOC, 1, ¶m);
224 return ERR_PTR(-ENOMEM);
226 mutex_lock(&optee->supp.mutex);
227 /* Increases count as secure world doesn't have a reference */
228 shm = tee_shm_get_from_id(optee->supp.ctx, param.u.value.c);
229 mutex_unlock(&optee->supp.mutex);
233 void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm)
235 struct tee_param param;
237 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
238 param.u.value.a = OPTEE_RPC_SHM_TYPE_APPL;
239 param.u.value.b = tee_shm_get_id(shm);
243 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
244 * world has released its reference.
246 * It's better to do this before sending the request to supplicant
247 * as we'd like to let the process doing the initial allocation to
248 * do release the last reference too in order to avoid stacking
249 * many pending fput() on the client process. This could otherwise
250 * happen if secure world does many allocate and free in a single
255 optee_supp_thrd_req(ctx, OPTEE_RPC_CMD_SHM_FREE, 1, ¶m);
258 void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
259 struct optee_msg_arg *arg)
262 case OPTEE_RPC_CMD_GET_TIME:
263 handle_rpc_func_cmd_get_time(arg);
265 case OPTEE_RPC_CMD_NOTIFICATION:
266 handle_rpc_func_cmd_wq(optee, arg);
268 case OPTEE_RPC_CMD_SUSPEND:
269 handle_rpc_func_cmd_wait(arg);
271 case OPTEE_RPC_CMD_I2C_TRANSFER:
272 handle_rpc_func_cmd_i2c_transfer(ctx, arg);
275 handle_rpc_supp_cmd(ctx, optee, arg);