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/slab.h>
11 #include <linux/tee_drv.h>
12 #include "optee_private.h"
13 #include "optee_smc.h"
16 struct list_head link
;
21 void optee_wait_queue_init(struct optee_wait_queue
*priv
)
23 mutex_init(&priv
->mu
);
24 INIT_LIST_HEAD(&priv
->db
);
27 void optee_wait_queue_exit(struct optee_wait_queue
*priv
)
29 mutex_destroy(&priv
->mu
);
32 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg
*arg
)
36 if (arg
->num_params
!= 1)
38 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
39 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT
)
42 ktime_get_real_ts64(&ts
);
43 arg
->params
[0].u
.value
.a
= ts
.tv_sec
;
44 arg
->params
[0].u
.value
.b
= ts
.tv_nsec
;
46 arg
->ret
= TEEC_SUCCESS
;
49 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
52 static struct wq_entry
*wq_entry_get(struct optee_wait_queue
*wq
, u32 key
)
58 list_for_each_entry(w
, &wq
->db
, link
)
62 w
= kmalloc(sizeof(*w
), GFP_KERNEL
);
64 init_completion(&w
->c
);
66 list_add_tail(&w
->link
, &wq
->db
);
69 mutex_unlock(&wq
->mu
);
73 static void wq_sleep(struct optee_wait_queue
*wq
, u32 key
)
75 struct wq_entry
*w
= wq_entry_get(wq
, key
);
78 wait_for_completion(&w
->c
);
81 mutex_unlock(&wq
->mu
);
86 static void wq_wakeup(struct optee_wait_queue
*wq
, u32 key
)
88 struct wq_entry
*w
= wq_entry_get(wq
, key
);
94 static void handle_rpc_func_cmd_wq(struct optee
*optee
,
95 struct optee_msg_arg
*arg
)
97 if (arg
->num_params
!= 1)
100 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
101 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
)
104 switch (arg
->params
[0].u
.value
.a
) {
105 case OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
:
106 wq_sleep(&optee
->wait_queue
, arg
->params
[0].u
.value
.b
);
108 case OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
:
109 wq_wakeup(&optee
->wait_queue
, arg
->params
[0].u
.value
.b
);
115 arg
->ret
= TEEC_SUCCESS
;
118 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
121 static void handle_rpc_func_cmd_wait(struct optee_msg_arg
*arg
)
125 if (arg
->num_params
!= 1)
128 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
129 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
)
132 msec_to_wait
= arg
->params
[0].u
.value
.a
;
134 /* Go to interruptible sleep */
135 msleep_interruptible(msec_to_wait
);
137 arg
->ret
= TEEC_SUCCESS
;
140 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
143 static void handle_rpc_supp_cmd(struct tee_context
*ctx
,
144 struct optee_msg_arg
*arg
)
146 struct tee_param
*params
;
148 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
150 params
= kmalloc_array(arg
->num_params
, sizeof(struct tee_param
),
153 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
157 if (optee_from_msg_param(params
, arg
->num_params
, arg
->params
)) {
158 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
162 arg
->ret
= optee_supp_thrd_req(ctx
, arg
->cmd
, arg
->num_params
, params
);
164 if (optee_to_msg_param(arg
->params
, arg
->num_params
, params
))
165 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
170 static struct tee_shm
*cmd_alloc_suppl(struct tee_context
*ctx
, size_t sz
)
173 struct tee_param param
;
174 struct optee
*optee
= tee_get_drvdata(ctx
->teedev
);
177 param
.attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
;
178 param
.u
.value
.a
= OPTEE_MSG_RPC_SHM_TYPE_APPL
;
179 param
.u
.value
.b
= sz
;
182 ret
= optee_supp_thrd_req(ctx
, OPTEE_MSG_RPC_CMD_SHM_ALLOC
, 1, ¶m
);
184 return ERR_PTR(-ENOMEM
);
186 mutex_lock(&optee
->supp
.mutex
);
187 /* Increases count as secure world doesn't have a reference */
188 shm
= tee_shm_get_from_id(optee
->supp
.ctx
, param
.u
.value
.c
);
189 mutex_unlock(&optee
->supp
.mutex
);
193 static void handle_rpc_func_cmd_shm_alloc(struct tee_context
*ctx
,
194 struct optee_msg_arg
*arg
,
195 struct optee_call_ctx
*call_ctx
)
202 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
204 if (!arg
->num_params
||
205 arg
->params
[0].attr
!= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
) {
206 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
210 for (n
= 1; n
< arg
->num_params
; n
++) {
211 if (arg
->params
[n
].attr
!= OPTEE_MSG_ATTR_TYPE_NONE
) {
212 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
217 sz
= arg
->params
[0].u
.value
.b
;
218 switch (arg
->params
[0].u
.value
.a
) {
219 case OPTEE_MSG_RPC_SHM_TYPE_APPL
:
220 shm
= cmd_alloc_suppl(ctx
, sz
);
222 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL
:
223 shm
= tee_shm_alloc(ctx
, sz
, TEE_SHM_MAPPED
);
226 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
231 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
235 if (tee_shm_get_pa(shm
, 0, &pa
)) {
236 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
240 sz
= tee_shm_get_size(shm
);
242 if (tee_shm_is_registered(shm
)) {
247 pages
= tee_shm_get_pages(shm
, &page_num
);
248 if (!pages
|| !page_num
) {
249 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
253 pages_list
= optee_allocate_pages_list(page_num
);
255 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
259 call_ctx
->pages_list
= pages_list
;
260 call_ctx
->num_entries
= page_num
;
262 arg
->params
[0].attr
= OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT
|
263 OPTEE_MSG_ATTR_NONCONTIG
;
265 * In the least bits of u.tmem.buf_ptr we store buffer offset
266 * from 4k page, as described in OP-TEE ABI.
268 arg
->params
[0].u
.tmem
.buf_ptr
= virt_to_phys(pages_list
) |
269 (tee_shm_get_page_offset(shm
) &
270 (OPTEE_MSG_NONCONTIG_PAGE_SIZE
- 1));
271 arg
->params
[0].u
.tmem
.size
= tee_shm_get_size(shm
);
272 arg
->params
[0].u
.tmem
.shm_ref
= (unsigned long)shm
;
274 optee_fill_pages_list(pages_list
, pages
, page_num
,
275 tee_shm_get_page_offset(shm
));
277 arg
->params
[0].attr
= OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT
;
278 arg
->params
[0].u
.tmem
.buf_ptr
= pa
;
279 arg
->params
[0].u
.tmem
.size
= sz
;
280 arg
->params
[0].u
.tmem
.shm_ref
= (unsigned long)shm
;
283 arg
->ret
= TEEC_SUCCESS
;
289 static void cmd_free_suppl(struct tee_context
*ctx
, struct tee_shm
*shm
)
291 struct tee_param param
;
293 param
.attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
;
294 param
.u
.value
.a
= OPTEE_MSG_RPC_SHM_TYPE_APPL
;
295 param
.u
.value
.b
= tee_shm_get_id(shm
);
299 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
300 * world has released its reference.
302 * It's better to do this before sending the request to supplicant
303 * as we'd like to let the process doing the initial allocation to
304 * do release the last reference too in order to avoid stacking
305 * many pending fput() on the client process. This could otherwise
306 * happen if secure world does many allocate and free in a single
311 optee_supp_thrd_req(ctx
, OPTEE_MSG_RPC_CMD_SHM_FREE
, 1, ¶m
);
314 static void handle_rpc_func_cmd_shm_free(struct tee_context
*ctx
,
315 struct optee_msg_arg
*arg
)
319 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
321 if (arg
->num_params
!= 1 ||
322 arg
->params
[0].attr
!= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
) {
323 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
327 shm
= (struct tee_shm
*)(unsigned long)arg
->params
[0].u
.value
.b
;
328 switch (arg
->params
[0].u
.value
.a
) {
329 case OPTEE_MSG_RPC_SHM_TYPE_APPL
:
330 cmd_free_suppl(ctx
, shm
);
332 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL
:
336 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
338 arg
->ret
= TEEC_SUCCESS
;
341 static void free_pages_list(struct optee_call_ctx
*call_ctx
)
343 if (call_ctx
->pages_list
) {
344 optee_free_pages_list(call_ctx
->pages_list
,
345 call_ctx
->num_entries
);
346 call_ctx
->pages_list
= NULL
;
347 call_ctx
->num_entries
= 0;
351 void optee_rpc_finalize_call(struct optee_call_ctx
*call_ctx
)
353 free_pages_list(call_ctx
);
356 static void handle_rpc_func_cmd(struct tee_context
*ctx
, struct optee
*optee
,
358 struct optee_call_ctx
*call_ctx
)
360 struct optee_msg_arg
*arg
;
362 arg
= tee_shm_get_va(shm
, 0);
364 pr_err("%s: tee_shm_get_va %p failed\n", __func__
, shm
);
369 case OPTEE_MSG_RPC_CMD_GET_TIME
:
370 handle_rpc_func_cmd_get_time(arg
);
372 case OPTEE_MSG_RPC_CMD_WAIT_QUEUE
:
373 handle_rpc_func_cmd_wq(optee
, arg
);
375 case OPTEE_MSG_RPC_CMD_SUSPEND
:
376 handle_rpc_func_cmd_wait(arg
);
378 case OPTEE_MSG_RPC_CMD_SHM_ALLOC
:
379 free_pages_list(call_ctx
);
380 handle_rpc_func_cmd_shm_alloc(ctx
, arg
, call_ctx
);
382 case OPTEE_MSG_RPC_CMD_SHM_FREE
:
383 handle_rpc_func_cmd_shm_free(ctx
, arg
);
386 handle_rpc_supp_cmd(ctx
, arg
);
391 * optee_handle_rpc() - handle RPC from secure world
392 * @ctx: context doing the RPC
393 * @param: value of registers for the RPC
394 * @call_ctx: call context. Preserved during one OP-TEE invocation
396 * Result of RPC is written back into @param.
398 void optee_handle_rpc(struct tee_context
*ctx
, struct optee_rpc_param
*param
,
399 struct optee_call_ctx
*call_ctx
)
401 struct tee_device
*teedev
= ctx
->teedev
;
402 struct optee
*optee
= tee_get_drvdata(teedev
);
406 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param
->a0
)) {
407 case OPTEE_SMC_RPC_FUNC_ALLOC
:
408 shm
= tee_shm_alloc(ctx
, param
->a1
, TEE_SHM_MAPPED
);
409 if (!IS_ERR(shm
) && !tee_shm_get_pa(shm
, 0, &pa
)) {
410 reg_pair_from_64(¶m
->a1
, ¶m
->a2
, pa
);
411 reg_pair_from_64(¶m
->a4
, ¶m
->a5
,
420 case OPTEE_SMC_RPC_FUNC_FREE
:
421 shm
= reg_pair_to_ptr(param
->a1
, param
->a2
);
424 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR
:
426 * A foreign interrupt was raised while secure world was
427 * executing, since they are handled in Linux a dummy RPC is
428 * performed to let Linux take the interrupt through the normal
432 case OPTEE_SMC_RPC_FUNC_CMD
:
433 shm
= reg_pair_to_ptr(param
->a1
, param
->a2
);
434 handle_rpc_func_cmd(ctx
, optee
, shm
, call_ctx
);
437 pr_warn("Unknown RPC func 0x%x\n",
438 (u32
)OPTEE_SMC_RETURN_GET_RPC_FUNC(param
->a0
));
442 param
->a0
= OPTEE_SMC_CALL_RETURN_FROM_RPC
;