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"
17 struct list_head link
;
22 void optee_wait_queue_init(struct optee_wait_queue
*priv
)
24 mutex_init(&priv
->mu
);
25 INIT_LIST_HEAD(&priv
->db
);
28 void optee_wait_queue_exit(struct optee_wait_queue
*priv
)
30 mutex_destroy(&priv
->mu
);
33 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg
*arg
)
37 if (arg
->num_params
!= 1)
39 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
40 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT
)
43 ktime_get_real_ts64(&ts
);
44 arg
->params
[0].u
.value
.a
= ts
.tv_sec
;
45 arg
->params
[0].u
.value
.b
= ts
.tv_nsec
;
47 arg
->ret
= TEEC_SUCCESS
;
50 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
53 #if IS_REACHABLE(CONFIG_I2C)
54 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context
*ctx
,
55 struct optee_msg_arg
*arg
)
57 struct i2c_client client
= { 0 };
58 struct tee_param
*params
;
60 int ret
= -EOPNOTSUPP
;
62 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
,
63 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
,
64 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
,
65 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
,
68 if (arg
->num_params
!= ARRAY_SIZE(attr
)) {
69 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
73 params
= kmalloc_array(arg
->num_params
, sizeof(struct tee_param
),
76 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
80 if (optee_from_msg_param(params
, arg
->num_params
, arg
->params
))
83 for (i
= 0; i
< arg
->num_params
; i
++) {
84 if (params
[i
].attr
!= attr
[i
])
88 client
.adapter
= i2c_get_adapter(params
[0].u
.value
.b
);
92 if (params
[1].u
.value
.a
& OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT
) {
93 if (!i2c_check_functionality(client
.adapter
,
94 I2C_FUNC_10BIT_ADDR
)) {
95 i2c_put_adapter(client
.adapter
);
99 client
.flags
= I2C_CLIENT_TEN
;
102 client
.addr
= params
[0].u
.value
.c
;
103 snprintf(client
.name
, I2C_NAME_SIZE
, "i2c%d", client
.adapter
->nr
);
105 switch (params
[0].u
.value
.a
) {
106 case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD
:
107 ret
= i2c_master_recv(&client
, params
[2].u
.memref
.shm
->kaddr
,
108 params
[2].u
.memref
.size
);
110 case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR
:
111 ret
= i2c_master_send(&client
, params
[2].u
.memref
.shm
->kaddr
,
112 params
[2].u
.memref
.size
);
115 i2c_put_adapter(client
.adapter
);
120 arg
->ret
= TEEC_ERROR_COMMUNICATION
;
122 params
[3].u
.value
.a
= ret
;
123 if (optee_to_msg_param(arg
->params
, arg
->num_params
, params
))
124 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
126 arg
->ret
= TEEC_SUCCESS
;
129 i2c_put_adapter(client
.adapter
);
134 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
137 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context
*ctx
,
138 struct optee_msg_arg
*arg
)
140 arg
->ret
= TEEC_ERROR_NOT_SUPPORTED
;
144 static struct wq_entry
*wq_entry_get(struct optee_wait_queue
*wq
, u32 key
)
150 list_for_each_entry(w
, &wq
->db
, link
)
154 w
= kmalloc(sizeof(*w
), GFP_KERNEL
);
156 init_completion(&w
->c
);
158 list_add_tail(&w
->link
, &wq
->db
);
161 mutex_unlock(&wq
->mu
);
165 static void wq_sleep(struct optee_wait_queue
*wq
, u32 key
)
167 struct wq_entry
*w
= wq_entry_get(wq
, key
);
170 wait_for_completion(&w
->c
);
173 mutex_unlock(&wq
->mu
);
178 static void wq_wakeup(struct optee_wait_queue
*wq
, u32 key
)
180 struct wq_entry
*w
= wq_entry_get(wq
, key
);
186 static void handle_rpc_func_cmd_wq(struct optee
*optee
,
187 struct optee_msg_arg
*arg
)
189 if (arg
->num_params
!= 1)
192 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
193 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
)
196 switch (arg
->params
[0].u
.value
.a
) {
197 case OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
:
198 wq_sleep(&optee
->wait_queue
, arg
->params
[0].u
.value
.b
);
200 case OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
:
201 wq_wakeup(&optee
->wait_queue
, arg
->params
[0].u
.value
.b
);
207 arg
->ret
= TEEC_SUCCESS
;
210 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
213 static void handle_rpc_func_cmd_wait(struct optee_msg_arg
*arg
)
217 if (arg
->num_params
!= 1)
220 if ((arg
->params
[0].attr
& OPTEE_MSG_ATTR_TYPE_MASK
) !=
221 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
)
224 msec_to_wait
= arg
->params
[0].u
.value
.a
;
226 /* Go to interruptible sleep */
227 msleep_interruptible(msec_to_wait
);
229 arg
->ret
= TEEC_SUCCESS
;
232 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
235 static void handle_rpc_supp_cmd(struct tee_context
*ctx
,
236 struct optee_msg_arg
*arg
)
238 struct tee_param
*params
;
240 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
242 params
= kmalloc_array(arg
->num_params
, sizeof(struct tee_param
),
245 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
249 if (optee_from_msg_param(params
, arg
->num_params
, arg
->params
)) {
250 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
254 arg
->ret
= optee_supp_thrd_req(ctx
, arg
->cmd
, arg
->num_params
, params
);
256 if (optee_to_msg_param(arg
->params
, arg
->num_params
, params
))
257 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
262 static struct tee_shm
*cmd_alloc_suppl(struct tee_context
*ctx
, size_t sz
)
265 struct tee_param param
;
266 struct optee
*optee
= tee_get_drvdata(ctx
->teedev
);
269 param
.attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
;
270 param
.u
.value
.a
= OPTEE_MSG_RPC_SHM_TYPE_APPL
;
271 param
.u
.value
.b
= sz
;
274 ret
= optee_supp_thrd_req(ctx
, OPTEE_MSG_RPC_CMD_SHM_ALLOC
, 1, ¶m
);
276 return ERR_PTR(-ENOMEM
);
278 mutex_lock(&optee
->supp
.mutex
);
279 /* Increases count as secure world doesn't have a reference */
280 shm
= tee_shm_get_from_id(optee
->supp
.ctx
, param
.u
.value
.c
);
281 mutex_unlock(&optee
->supp
.mutex
);
285 static void handle_rpc_func_cmd_shm_alloc(struct tee_context
*ctx
,
286 struct optee_msg_arg
*arg
,
287 struct optee_call_ctx
*call_ctx
)
294 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
296 if (!arg
->num_params
||
297 arg
->params
[0].attr
!= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
) {
298 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
302 for (n
= 1; n
< arg
->num_params
; n
++) {
303 if (arg
->params
[n
].attr
!= OPTEE_MSG_ATTR_TYPE_NONE
) {
304 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
309 sz
= arg
->params
[0].u
.value
.b
;
310 switch (arg
->params
[0].u
.value
.a
) {
311 case OPTEE_MSG_RPC_SHM_TYPE_APPL
:
312 shm
= cmd_alloc_suppl(ctx
, sz
);
314 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL
:
315 shm
= tee_shm_alloc(ctx
, sz
, TEE_SHM_MAPPED
);
318 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
323 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
327 if (tee_shm_get_pa(shm
, 0, &pa
)) {
328 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
332 sz
= tee_shm_get_size(shm
);
334 if (tee_shm_is_registered(shm
)) {
339 pages
= tee_shm_get_pages(shm
, &page_num
);
340 if (!pages
|| !page_num
) {
341 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
345 pages_list
= optee_allocate_pages_list(page_num
);
347 arg
->ret
= TEEC_ERROR_OUT_OF_MEMORY
;
351 call_ctx
->pages_list
= pages_list
;
352 call_ctx
->num_entries
= page_num
;
354 arg
->params
[0].attr
= OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT
|
355 OPTEE_MSG_ATTR_NONCONTIG
;
357 * In the least bits of u.tmem.buf_ptr we store buffer offset
358 * from 4k page, as described in OP-TEE ABI.
360 arg
->params
[0].u
.tmem
.buf_ptr
= virt_to_phys(pages_list
) |
361 (tee_shm_get_page_offset(shm
) &
362 (OPTEE_MSG_NONCONTIG_PAGE_SIZE
- 1));
363 arg
->params
[0].u
.tmem
.size
= tee_shm_get_size(shm
);
364 arg
->params
[0].u
.tmem
.shm_ref
= (unsigned long)shm
;
366 optee_fill_pages_list(pages_list
, pages
, page_num
,
367 tee_shm_get_page_offset(shm
));
369 arg
->params
[0].attr
= OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT
;
370 arg
->params
[0].u
.tmem
.buf_ptr
= pa
;
371 arg
->params
[0].u
.tmem
.size
= sz
;
372 arg
->params
[0].u
.tmem
.shm_ref
= (unsigned long)shm
;
375 arg
->ret
= TEEC_SUCCESS
;
381 static void cmd_free_suppl(struct tee_context
*ctx
, struct tee_shm
*shm
)
383 struct tee_param param
;
385 param
.attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
;
386 param
.u
.value
.a
= OPTEE_MSG_RPC_SHM_TYPE_APPL
;
387 param
.u
.value
.b
= tee_shm_get_id(shm
);
391 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
392 * world has released its reference.
394 * It's better to do this before sending the request to supplicant
395 * as we'd like to let the process doing the initial allocation to
396 * do release the last reference too in order to avoid stacking
397 * many pending fput() on the client process. This could otherwise
398 * happen if secure world does many allocate and free in a single
403 optee_supp_thrd_req(ctx
, OPTEE_MSG_RPC_CMD_SHM_FREE
, 1, ¶m
);
406 static void handle_rpc_func_cmd_shm_free(struct tee_context
*ctx
,
407 struct optee_msg_arg
*arg
)
411 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
413 if (arg
->num_params
!= 1 ||
414 arg
->params
[0].attr
!= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
) {
415 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
419 shm
= (struct tee_shm
*)(unsigned long)arg
->params
[0].u
.value
.b
;
420 switch (arg
->params
[0].u
.value
.a
) {
421 case OPTEE_MSG_RPC_SHM_TYPE_APPL
:
422 cmd_free_suppl(ctx
, shm
);
424 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL
:
428 arg
->ret
= TEEC_ERROR_BAD_PARAMETERS
;
430 arg
->ret
= TEEC_SUCCESS
;
433 static void free_pages_list(struct optee_call_ctx
*call_ctx
)
435 if (call_ctx
->pages_list
) {
436 optee_free_pages_list(call_ctx
->pages_list
,
437 call_ctx
->num_entries
);
438 call_ctx
->pages_list
= NULL
;
439 call_ctx
->num_entries
= 0;
443 void optee_rpc_finalize_call(struct optee_call_ctx
*call_ctx
)
445 free_pages_list(call_ctx
);
448 static void handle_rpc_func_cmd(struct tee_context
*ctx
, struct optee
*optee
,
450 struct optee_call_ctx
*call_ctx
)
452 struct optee_msg_arg
*arg
;
454 arg
= tee_shm_get_va(shm
, 0);
456 pr_err("%s: tee_shm_get_va %p failed\n", __func__
, shm
);
461 case OPTEE_MSG_RPC_CMD_GET_TIME
:
462 handle_rpc_func_cmd_get_time(arg
);
464 case OPTEE_MSG_RPC_CMD_WAIT_QUEUE
:
465 handle_rpc_func_cmd_wq(optee
, arg
);
467 case OPTEE_MSG_RPC_CMD_SUSPEND
:
468 handle_rpc_func_cmd_wait(arg
);
470 case OPTEE_MSG_RPC_CMD_SHM_ALLOC
:
471 free_pages_list(call_ctx
);
472 handle_rpc_func_cmd_shm_alloc(ctx
, arg
, call_ctx
);
474 case OPTEE_MSG_RPC_CMD_SHM_FREE
:
475 handle_rpc_func_cmd_shm_free(ctx
, arg
);
477 case OPTEE_MSG_RPC_CMD_I2C_TRANSFER
:
478 handle_rpc_func_cmd_i2c_transfer(ctx
, arg
);
481 handle_rpc_supp_cmd(ctx
, arg
);
486 * optee_handle_rpc() - handle RPC from secure world
487 * @ctx: context doing the RPC
488 * @param: value of registers for the RPC
489 * @call_ctx: call context. Preserved during one OP-TEE invocation
491 * Result of RPC is written back into @param.
493 void optee_handle_rpc(struct tee_context
*ctx
, struct optee_rpc_param
*param
,
494 struct optee_call_ctx
*call_ctx
)
496 struct tee_device
*teedev
= ctx
->teedev
;
497 struct optee
*optee
= tee_get_drvdata(teedev
);
501 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param
->a0
)) {
502 case OPTEE_SMC_RPC_FUNC_ALLOC
:
503 shm
= tee_shm_alloc(ctx
, param
->a1
, TEE_SHM_MAPPED
);
504 if (!IS_ERR(shm
) && !tee_shm_get_pa(shm
, 0, &pa
)) {
505 reg_pair_from_64(¶m
->a1
, ¶m
->a2
, pa
);
506 reg_pair_from_64(¶m
->a4
, ¶m
->a5
,
515 case OPTEE_SMC_RPC_FUNC_FREE
:
516 shm
= reg_pair_to_ptr(param
->a1
, param
->a2
);
519 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR
:
521 * A foreign interrupt was raised while secure world was
522 * executing, since they are handled in Linux a dummy RPC is
523 * performed to let Linux take the interrupt through the normal
527 case OPTEE_SMC_RPC_FUNC_CMD
:
528 shm
= reg_pair_to_ptr(param
->a1
, param
->a2
);
529 handle_rpc_func_cmd(ctx
, optee
, shm
, call_ctx
);
532 pr_warn("Unknown RPC func 0x%x\n",
533 (u32
)OPTEE_SMC_RETURN_GET_RPC_FUNC(param
->a0
));
537 param
->a0
= OPTEE_SMC_CALL_RETURN_FROM_RPC
;