2 * Copyright (c) 2015-2016, 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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/tee_drv.h>
21 #include "optee_private.h"
22 #include "optee_smc.h"
25 struct list_head link
;
30 void optee_wait_queue_init(struct optee_wait_queue
*priv
)
32 mutex_init(&priv
->mu
);
33 INIT_LIST_HEAD(&priv
->db
);
36 void optee_wait_queue_exit(struct optee_wait_queue
*priv
)
38 mutex_destroy(&priv
->mu
);
41 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg
*arg
)
45 if (arg
->num_params
!= 1)
47 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
48 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT
)
51 getnstimeofday64(&ts
);
52 arg
->params
[0].u
.value
.a
= ts
.tv_sec
;
53 arg
->params
[0].u
.value
.b
= ts
.tv_nsec
;
55 arg
->ret
= TEEC_SUCCESS
;
58 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
61 static struct wq_entry
*wq_entry_get(struct optee_wait_queue
*wq
, u32 key
)
67 list_for_each_entry(w
, &wq
->db
, link
)
71 w
= kmalloc(sizeof(*w
), GFP_KERNEL
);
73 init_completion(&w
->c
);
75 list_add_tail(&w
->link
, &wq
->db
);
78 mutex_unlock(&wq
->mu
);
82 static void wq_sleep(struct optee_wait_queue
*wq
, u32 key
)
84 struct wq_entry
*w
= wq_entry_get(wq
, key
);
87 wait_for_completion(&w
->c
);
90 mutex_unlock(&wq
->mu
);
95 static void wq_wakeup(struct optee_wait_queue
*wq
, u32 key
)
97 struct wq_entry
*w
= wq_entry_get(wq
, key
);
103 static void handle_rpc_func_cmd_wq(struct optee
*optee
,
104 struct optee_msg_arg
*arg
)
106 if (arg
->num_params
!= 1)
109 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
110 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
)
113 switch (arg
->params
[0].u
.value
.a
) {
114 case OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
:
115 wq_sleep(&optee
->wait_queue
, arg
->params
[0].u
.value
.b
);
117 case OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
:
118 wq_wakeup(&optee
->wait_queue
, arg
->params
[0].u
.value
.b
);
124 arg
->ret
= TEEC_SUCCESS
;
127 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
130 static void handle_rpc_func_cmd_wait(struct optee_msg_arg
*arg
)
134 if (arg
->num_params
!= 1)
137 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
138 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
)
141 msec_to_wait
= arg
->params
[0].u
.value
.a
;
143 /* Go to interruptible sleep */
144 msleep_interruptible(msec_to_wait
);
146 arg
->ret
= TEEC_SUCCESS
;
149 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
152 static void handle_rpc_supp_cmd(struct tee_context
*ctx
,
153 struct optee_msg_arg
*arg
)
155 struct tee_param
*params
;
157 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
159 params
= kmalloc_array(arg
->num_params
, sizeof(struct tee_param
),
162 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
166 if (optee_from_msg_param(params
, arg
->num_params
, arg
->params
)) {
167 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
171 arg
->ret
= optee_supp_thrd_req(ctx
, arg
->cmd
, arg
->num_params
, params
);
173 if (optee_to_msg_param(arg
->params
, arg
->num_params
, params
))
174 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
179 static struct tee_shm
*cmd_alloc_suppl(struct tee_context
*ctx
, size_t sz
)
182 struct tee_param param
;
183 struct optee
*optee
= tee_get_drvdata(ctx
->teedev
);
186 param
.attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
;
187 param
.u
.value
.a
= OPTEE_MSG_RPC_SHM_TYPE_APPL
;
188 param
.u
.value
.b
= sz
;
191 ret
= optee_supp_thrd_req(ctx
, OPTEE_MSG_RPC_CMD_SHM_ALLOC
, 1, ¶m
);
193 return ERR_PTR(-ENOMEM
);
195 mutex_lock(&optee
->supp
.ctx_mutex
);
196 /* Increases count as secure world doesn't have a reference */
197 shm
= tee_shm_get_from_id(optee
->supp
.ctx
, param
.u
.value
.c
);
198 mutex_unlock(&optee
->supp
.ctx_mutex
);
202 static void handle_rpc_func_cmd_shm_alloc(struct tee_context
*ctx
,
203 struct optee_msg_arg
*arg
)
210 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
212 if (!arg
->num_params
||
213 arg
->params
[0].attr
!= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
) {
214 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
218 for (n
= 1; n
< arg
->num_params
; n
++) {
219 if (arg
->params
[n
].attr
!= OPTEE_MSG_ATTR_TYPE_NONE
) {
220 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
225 sz
= arg
->params
[0].u
.value
.b
;
226 switch (arg
->params
[0].u
.value
.a
) {
227 case OPTEE_MSG_RPC_SHM_TYPE_APPL
:
228 shm
= cmd_alloc_suppl(ctx
, sz
);
230 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL
:
231 shm
= tee_shm_alloc(ctx
, sz
, TEE_SHM_MAPPED
);
234 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
239 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
243 if (tee_shm_get_pa(shm
, 0, &pa
)) {
244 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
248 arg
->params
[0].attr
= OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT
;
249 arg
->params
[0].u
.tmem
.buf_ptr
= pa
;
250 arg
->params
[0].u
.tmem
.size
= sz
;
251 arg
->params
[0].u
.tmem
.shm_ref
= (unsigned long)shm
;
252 arg
->ret
= TEEC_SUCCESS
;
258 static void cmd_free_suppl(struct tee_context
*ctx
, struct tee_shm
*shm
)
260 struct tee_param param
;
262 param
.attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
;
263 param
.u
.value
.a
= OPTEE_MSG_RPC_SHM_TYPE_APPL
;
264 param
.u
.value
.b
= tee_shm_get_id(shm
);
268 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
269 * world has released its reference.
271 * It's better to do this before sending the request to supplicant
272 * as we'd like to let the process doing the initial allocation to
273 * do release the last reference too in order to avoid stacking
274 * many pending fput() on the client process. This could otherwise
275 * happen if secure world does many allocate and free in a single
280 optee_supp_thrd_req(ctx
, OPTEE_MSG_RPC_CMD_SHM_FREE
, 1, ¶m
);
283 static void handle_rpc_func_cmd_shm_free(struct tee_context
*ctx
,
284 struct optee_msg_arg
*arg
)
288 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
290 if (arg
->num_params
!= 1 ||
291 arg
->params
[0].attr
!= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
) {
292 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
296 shm
= (struct tee_shm
*)(unsigned long)arg
->params
[0].u
.value
.b
;
297 switch (arg
->params
[0].u
.value
.a
) {
298 case OPTEE_MSG_RPC_SHM_TYPE_APPL
:
299 cmd_free_suppl(ctx
, shm
);
301 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL
:
305 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
307 arg
->ret
= TEEC_SUCCESS
;
310 static void handle_rpc_func_cmd(struct tee_context
*ctx
, struct optee
*optee
,
313 struct optee_msg_arg
*arg
;
315 arg
= tee_shm_get_va(shm
, 0);
317 pr_err("%s: tee_shm_get_va %p failed\n", __func__
, shm
);
322 case OPTEE_MSG_RPC_CMD_GET_TIME
:
323 handle_rpc_func_cmd_get_time(arg
);
325 case OPTEE_MSG_RPC_CMD_WAIT_QUEUE
:
326 handle_rpc_func_cmd_wq(optee
, arg
);
328 case OPTEE_MSG_RPC_CMD_SUSPEND
:
329 handle_rpc_func_cmd_wait(arg
);
331 case OPTEE_MSG_RPC_CMD_SHM_ALLOC
:
332 handle_rpc_func_cmd_shm_alloc(ctx
, arg
);
334 case OPTEE_MSG_RPC_CMD_SHM_FREE
:
335 handle_rpc_func_cmd_shm_free(ctx
, arg
);
338 handle_rpc_supp_cmd(ctx
, arg
);
343 * optee_handle_rpc() - handle RPC from secure world
344 * @ctx: context doing the RPC
345 * @param: value of registers for the RPC
347 * Result of RPC is written back into @param.
349 void optee_handle_rpc(struct tee_context
*ctx
, struct optee_rpc_param
*param
)
351 struct tee_device
*teedev
= ctx
->teedev
;
352 struct optee
*optee
= tee_get_drvdata(teedev
);
356 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param
->a0
)) {
357 case OPTEE_SMC_RPC_FUNC_ALLOC
:
358 shm
= tee_shm_alloc(ctx
, param
->a1
, TEE_SHM_MAPPED
);
359 if (!IS_ERR(shm
) && !tee_shm_get_pa(shm
, 0, &pa
)) {
360 reg_pair_from_64(¶m
->a1
, ¶m
->a2
, pa
);
361 reg_pair_from_64(¶m
->a4
, ¶m
->a5
,
370 case OPTEE_SMC_RPC_FUNC_FREE
:
371 shm
= reg_pair_to_ptr(param
->a1
, param
->a2
);
374 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR
:
376 * A foreign interrupt was raised while secure world was
377 * executing, since they are handled in Linux a dummy RPC is
378 * performed to let Linux take the interrupt through the normal
382 case OPTEE_SMC_RPC_FUNC_CMD
:
383 shm
= reg_pair_to_ptr(param
->a1
, param
->a2
);
384 handle_rpc_func_cmd(ctx
, optee
, shm
);
387 pr_warn("Unknown RPC func 0x%x\n",
388 (u32
)OPTEE_SMC_RETURN_GET_RPC_FUNC(param
->a0
));
392 param
->a0
= OPTEE_SMC_CALL_RETURN_FROM_RPC
;