2 * Copyright (c) 2015, 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.
14 #include <linux/arm-smccc.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/tee_drv.h>
20 #include <linux/types.h>
21 #include <linux/uaccess.h>
22 #include "optee_private.h"
23 #include "optee_smc.h"
25 struct optee_call_waiter
{
26 struct list_head list_node
;
30 static void optee_cq_wait_init(struct optee_call_queue
*cq
,
31 struct optee_call_waiter
*w
)
34 * We're preparing to make a call to secure world. In case we can't
35 * allocate a thread in secure world we'll end up waiting in
36 * optee_cq_wait_for_completion().
38 * Normally if there's no contention in secure world the call will
39 * complete and we can cleanup directly with optee_cq_wait_final().
41 mutex_lock(&cq
->mutex
);
44 * We add ourselves to the queue, but we don't wait. This
45 * guarantees that we don't lose a completion if secure world
46 * returns busy and another thread just exited and try to complete
49 init_completion(&w
->c
);
50 list_add_tail(&w
->list_node
, &cq
->waiters
);
52 mutex_unlock(&cq
->mutex
);
55 static void optee_cq_wait_for_completion(struct optee_call_queue
*cq
,
56 struct optee_call_waiter
*w
)
58 wait_for_completion(&w
->c
);
60 mutex_lock(&cq
->mutex
);
62 /* Move to end of list to get out of the way for other waiters */
63 list_del(&w
->list_node
);
64 reinit_completion(&w
->c
);
65 list_add_tail(&w
->list_node
, &cq
->waiters
);
67 mutex_unlock(&cq
->mutex
);
70 static void optee_cq_complete_one(struct optee_call_queue
*cq
)
72 struct optee_call_waiter
*w
;
74 list_for_each_entry(w
, &cq
->waiters
, list_node
) {
75 if (!completion_done(&w
->c
)) {
82 static void optee_cq_wait_final(struct optee_call_queue
*cq
,
83 struct optee_call_waiter
*w
)
86 * We're done with the call to secure world. The thread in secure
87 * world that was used for this call is now available for some
90 mutex_lock(&cq
->mutex
);
92 /* Get out of the list */
93 list_del(&w
->list_node
);
95 /* Wake up one eventual waiting task */
96 optee_cq_complete_one(cq
);
99 * If we're completed we've got a completion from another task that
100 * was just done with its call to secure world. Since yet another
101 * thread now is available in secure world wake up another eventual
104 if (completion_done(&w
->c
))
105 optee_cq_complete_one(cq
);
107 mutex_unlock(&cq
->mutex
);
110 /* Requires the filpstate mutex to be held */
111 static struct optee_session
*find_session(struct optee_context_data
*ctxdata
,
114 struct optee_session
*sess
;
116 list_for_each_entry(sess
, &ctxdata
->sess_list
, list_node
)
117 if (sess
->session_id
== session_id
)
124 * optee_do_call_with_arg() - Do an SMC to OP-TEE in secure world
125 * @ctx: calling context
126 * @parg: physical address of message to pass to secure world
128 * Does and SMC to OP-TEE in secure world and handles eventual resulting
129 * Remote Procedure Calls (RPC) from OP-TEE.
131 * Returns return code from secure world, 0 is OK
133 u32
optee_do_call_with_arg(struct tee_context
*ctx
, phys_addr_t parg
)
135 struct optee
*optee
= tee_get_drvdata(ctx
->teedev
);
136 struct optee_call_waiter w
;
137 struct optee_rpc_param param
= { };
140 param
.a0
= OPTEE_SMC_CALL_WITH_ARG
;
141 reg_pair_from_64(¶m
.a1
, ¶m
.a2
, parg
);
142 /* Initialize waiter */
143 optee_cq_wait_init(&optee
->call_queue
, &w
);
145 struct arm_smccc_res res
;
147 optee
->invoke_fn(param
.a0
, param
.a1
, param
.a2
, param
.a3
,
148 param
.a4
, param
.a5
, param
.a6
, param
.a7
,
151 if (res
.a0
== OPTEE_SMC_RETURN_ETHREAD_LIMIT
) {
153 * Out of threads in secure world, wait for a thread
156 optee_cq_wait_for_completion(&optee
->call_queue
, &w
);
157 } else if (OPTEE_SMC_RETURN_IS_RPC(res
.a0
)) {
162 optee_handle_rpc(ctx
, ¶m
);
170 * We're done with our thread in secure world, if there's any
171 * thread waiters wake up one.
173 optee_cq_wait_final(&optee
->call_queue
, &w
);
178 static struct tee_shm
*get_msg_arg(struct tee_context
*ctx
, size_t num_params
,
179 struct optee_msg_arg
**msg_arg
,
180 phys_addr_t
*msg_parg
)
184 struct optee_msg_arg
*ma
;
186 shm
= tee_shm_alloc(ctx
, OPTEE_MSG_GET_ARG_SIZE(num_params
),
191 ma
= tee_shm_get_va(shm
, 0);
197 rc
= tee_shm_get_pa(shm
, 0, msg_parg
);
201 memset(ma
, 0, OPTEE_MSG_GET_ARG_SIZE(num_params
));
202 ma
->num_params
= num_params
;
213 int optee_open_session(struct tee_context
*ctx
,
214 struct tee_ioctl_open_session_arg
*arg
,
215 struct tee_param
*param
)
217 struct optee_context_data
*ctxdata
= ctx
->data
;
220 struct optee_msg_arg
*msg_arg
;
221 phys_addr_t msg_parg
;
222 struct optee_session
*sess
= NULL
;
224 /* +2 for the meta parameters added below */
225 shm
= get_msg_arg(ctx
, arg
->num_params
+ 2, &msg_arg
, &msg_parg
);
229 msg_arg
->cmd
= OPTEE_MSG_CMD_OPEN_SESSION
;
230 msg_arg
->cancel_id
= arg
->cancel_id
;
233 * Initialize and add the meta parameters needed when opening a
236 msg_arg
->params
[0].attr
= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
|
238 msg_arg
->params
[1].attr
= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
|
240 memcpy(&msg_arg
->params
[0].u
.value
, arg
->uuid
, sizeof(arg
->uuid
));
241 memcpy(&msg_arg
->params
[1].u
.value
, arg
->uuid
, sizeof(arg
->clnt_uuid
));
242 msg_arg
->params
[1].u
.value
.c
= arg
->clnt_login
;
244 rc
= optee_to_msg_param(msg_arg
->params
+ 2, arg
->num_params
, param
);
248 sess
= kzalloc(sizeof(*sess
), GFP_KERNEL
);
254 if (optee_do_call_with_arg(ctx
, msg_parg
)) {
255 msg_arg
->ret
= TEEC_ERROR_COMMUNICATION
;
256 msg_arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
259 if (msg_arg
->ret
== TEEC_SUCCESS
) {
260 /* A new session has been created, add it to the list. */
261 sess
->session_id
= msg_arg
->session
;
262 mutex_lock(&ctxdata
->mutex
);
263 list_add(&sess
->list_node
, &ctxdata
->sess_list
);
264 mutex_unlock(&ctxdata
->mutex
);
269 if (optee_from_msg_param(param
, arg
->num_params
, msg_arg
->params
+ 2)) {
270 arg
->ret
= TEEC_ERROR_COMMUNICATION
;
271 arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
272 /* Close session again to avoid leakage */
273 optee_close_session(ctx
, msg_arg
->session
);
275 arg
->session
= msg_arg
->session
;
276 arg
->ret
= msg_arg
->ret
;
277 arg
->ret_origin
= msg_arg
->ret_origin
;
285 int optee_close_session(struct tee_context
*ctx
, u32 session
)
287 struct optee_context_data
*ctxdata
= ctx
->data
;
289 struct optee_msg_arg
*msg_arg
;
290 phys_addr_t msg_parg
;
291 struct optee_session
*sess
;
293 /* Check that the session is valid and remove it from the list */
294 mutex_lock(&ctxdata
->mutex
);
295 sess
= find_session(ctxdata
, session
);
297 list_del(&sess
->list_node
);
298 mutex_unlock(&ctxdata
->mutex
);
303 shm
= get_msg_arg(ctx
, 0, &msg_arg
, &msg_parg
);
307 msg_arg
->cmd
= OPTEE_MSG_CMD_CLOSE_SESSION
;
308 msg_arg
->session
= session
;
309 optee_do_call_with_arg(ctx
, msg_parg
);
315 int optee_invoke_func(struct tee_context
*ctx
, struct tee_ioctl_invoke_arg
*arg
,
316 struct tee_param
*param
)
318 struct optee_context_data
*ctxdata
= ctx
->data
;
320 struct optee_msg_arg
*msg_arg
;
321 phys_addr_t msg_parg
;
322 struct optee_session
*sess
;
325 /* Check that the session is valid */
326 mutex_lock(&ctxdata
->mutex
);
327 sess
= find_session(ctxdata
, arg
->session
);
328 mutex_unlock(&ctxdata
->mutex
);
332 shm
= get_msg_arg(ctx
, arg
->num_params
, &msg_arg
, &msg_parg
);
335 msg_arg
->cmd
= OPTEE_MSG_CMD_INVOKE_COMMAND
;
336 msg_arg
->func
= arg
->func
;
337 msg_arg
->session
= arg
->session
;
338 msg_arg
->cancel_id
= arg
->cancel_id
;
340 rc
= optee_to_msg_param(msg_arg
->params
, arg
->num_params
, param
);
344 if (optee_do_call_with_arg(ctx
, msg_parg
)) {
345 msg_arg
->ret
= TEEC_ERROR_COMMUNICATION
;
346 msg_arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
349 if (optee_from_msg_param(param
, arg
->num_params
, msg_arg
->params
)) {
350 msg_arg
->ret
= TEEC_ERROR_COMMUNICATION
;
351 msg_arg
->ret_origin
= TEEC_ORIGIN_COMMS
;
354 arg
->ret
= msg_arg
->ret
;
355 arg
->ret_origin
= msg_arg
->ret_origin
;
361 int optee_cancel_req(struct tee_context
*ctx
, u32 cancel_id
, u32 session
)
363 struct optee_context_data
*ctxdata
= ctx
->data
;
365 struct optee_msg_arg
*msg_arg
;
366 phys_addr_t msg_parg
;
367 struct optee_session
*sess
;
369 /* Check that the session is valid */
370 mutex_lock(&ctxdata
->mutex
);
371 sess
= find_session(ctxdata
, session
);
372 mutex_unlock(&ctxdata
->mutex
);
376 shm
= get_msg_arg(ctx
, 0, &msg_arg
, &msg_parg
);
380 msg_arg
->cmd
= OPTEE_MSG_CMD_CANCEL
;
381 msg_arg
->session
= session
;
382 msg_arg
->cancel_id
= cancel_id
;
383 optee_do_call_with_arg(ctx
, msg_parg
);
390 * optee_enable_shm_cache() - Enables caching of some shared memory allocation
392 * @optee: main service struct
394 void optee_enable_shm_cache(struct optee
*optee
)
396 struct optee_call_waiter w
;
398 /* We need to retry until secure world isn't busy. */
399 optee_cq_wait_init(&optee
->call_queue
, &w
);
401 struct arm_smccc_res res
;
403 optee
->invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE
, 0, 0, 0, 0, 0, 0,
405 if (res
.a0
== OPTEE_SMC_RETURN_OK
)
407 optee_cq_wait_for_completion(&optee
->call_queue
, &w
);
409 optee_cq_wait_final(&optee
->call_queue
, &w
);
413 * optee_disable_shm_cache() - Disables caching of some shared memory allocation
415 * @optee: main service struct
417 void optee_disable_shm_cache(struct optee
*optee
)
419 struct optee_call_waiter w
;
421 /* We need to retry until secure world isn't busy. */
422 optee_cq_wait_init(&optee
->call_queue
, &w
);
425 struct arm_smccc_res smccc
;
426 struct optee_smc_disable_shm_cache_result result
;
429 optee
->invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE
, 0, 0, 0, 0, 0, 0,
431 if (res
.result
.status
== OPTEE_SMC_RETURN_ENOTAVAIL
)
432 break; /* All shm's freed */
433 if (res
.result
.status
== OPTEE_SMC_RETURN_OK
) {
436 shm
= reg_pair_to_ptr(res
.result
.shm_upper32
,
437 res
.result
.shm_lower32
);
440 optee_cq_wait_for_completion(&optee
->call_queue
, &w
);
443 optee_cq_wait_final(&optee
->call_queue
, &w
);