1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Linaro Limited
5 #include <linux/arm-smccc.h>
6 #include <linux/device.h>
8 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/tee_drv.h>
13 #include <linux/types.h>
14 #include <linux/uaccess.h>
15 #include "optee_private.h"
16 #include "optee_smc.h"
18 struct optee_call_waiter {
19 struct list_head list_node;
23 static void optee_cq_wait_init(struct optee_call_queue *cq,
24 struct optee_call_waiter *w)
27 * We're preparing to make a call to secure world. In case we can't
28 * allocate a thread in secure world we'll end up waiting in
29 * optee_cq_wait_for_completion().
31 * Normally if there's no contention in secure world the call will
32 * complete and we can cleanup directly with optee_cq_wait_final().
34 mutex_lock(&cq->mutex);
37 * We add ourselves to the queue, but we don't wait. This
38 * guarantees that we don't lose a completion if secure world
39 * returns busy and another thread just exited and try to complete
42 init_completion(&w->c);
43 list_add_tail(&w->list_node, &cq->waiters);
45 mutex_unlock(&cq->mutex);
48 static void optee_cq_wait_for_completion(struct optee_call_queue *cq,
49 struct optee_call_waiter *w)
51 wait_for_completion(&w->c);
53 mutex_lock(&cq->mutex);
55 /* Move to end of list to get out of the way for other waiters */
56 list_del(&w->list_node);
57 reinit_completion(&w->c);
58 list_add_tail(&w->list_node, &cq->waiters);
60 mutex_unlock(&cq->mutex);
63 static void optee_cq_complete_one(struct optee_call_queue *cq)
65 struct optee_call_waiter *w;
67 list_for_each_entry(w, &cq->waiters, list_node) {
68 if (!completion_done(&w->c)) {
75 static void optee_cq_wait_final(struct optee_call_queue *cq,
76 struct optee_call_waiter *w)
79 * We're done with the call to secure world. The thread in secure
80 * world that was used for this call is now available for some
83 mutex_lock(&cq->mutex);
85 /* Get out of the list */
86 list_del(&w->list_node);
88 /* Wake up one eventual waiting task */
89 optee_cq_complete_one(cq);
92 * If we're completed we've got a completion from another task that
93 * was just done with its call to secure world. Since yet another
94 * thread now is available in secure world wake up another eventual
97 if (completion_done(&w->c))
98 optee_cq_complete_one(cq);
100 mutex_unlock(&cq->mutex);
103 /* Requires the filpstate mutex to be held */
104 static struct optee_session *find_session(struct optee_context_data *ctxdata,
107 struct optee_session *sess;
109 list_for_each_entry(sess, &ctxdata->sess_list, list_node)
110 if (sess->session_id == session_id)
117 * optee_do_call_with_arg() - Do an SMC to OP-TEE in secure world
118 * @ctx: calling context
119 * @parg: physical address of message to pass to secure world
121 * Does and SMC to OP-TEE in secure world and handles eventual resulting
122 * Remote Procedure Calls (RPC) from OP-TEE.
124 * Returns return code from secure world, 0 is OK
126 u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg)
128 struct optee *optee = tee_get_drvdata(ctx->teedev);
129 struct optee_call_waiter w;
130 struct optee_rpc_param param = { };
131 struct optee_call_ctx call_ctx = { };
134 param.a0 = OPTEE_SMC_CALL_WITH_ARG;
135 reg_pair_from_64(¶m.a1, ¶m.a2, parg);
136 /* Initialize waiter */
137 optee_cq_wait_init(&optee->call_queue, &w);
139 struct arm_smccc_res res;
141 optee->invoke_fn(param.a0, param.a1, param.a2, param.a3,
142 param.a4, param.a5, param.a6, param.a7,
145 if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
147 * Out of threads in secure world, wait for a thread
150 optee_cq_wait_for_completion(&optee->call_queue, &w);
151 } else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
157 optee_handle_rpc(ctx, ¶m, &call_ctx);
164 optee_rpc_finalize_call(&call_ctx);
166 * We're done with our thread in secure world, if there's any
167 * thread waiters wake up one.
169 optee_cq_wait_final(&optee->call_queue, &w);
174 static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params,
175 struct optee_msg_arg **msg_arg,
176 phys_addr_t *msg_parg)
180 struct optee_msg_arg *ma;
182 shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
187 ma = tee_shm_get_va(shm, 0);
193 rc = tee_shm_get_pa(shm, 0, msg_parg);
197 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
198 ma->num_params = num_params;
209 int optee_open_session(struct tee_context *ctx,
210 struct tee_ioctl_open_session_arg *arg,
211 struct tee_param *param)
213 struct optee_context_data *ctxdata = ctx->data;
216 struct optee_msg_arg *msg_arg;
217 phys_addr_t msg_parg;
218 struct optee_session *sess = NULL;
220 /* +2 for the meta parameters added below */
221 shm = get_msg_arg(ctx, arg->num_params + 2, &msg_arg, &msg_parg);
225 msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
226 msg_arg->cancel_id = arg->cancel_id;
229 * Initialize and add the meta parameters needed when opening a
232 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
234 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
236 memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
237 msg_arg->params[1].u.value.c = arg->clnt_login;
239 rc = tee_session_calc_client_uuid((uuid_t *)&msg_arg->params[1].u.value,
240 arg->clnt_login, arg->clnt_uuid);
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);
446 #define PAGELIST_ENTRIES_PER_PAGE \
447 ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
450 * optee_fill_pages_list() - write list of user pages to given shared
453 * @dst: page-aligned buffer where list of pages will be stored
454 * @pages: array of pages that represents shared buffer
455 * @num_pages: number of entries in @pages
456 * @page_offset: offset of user buffer from page start
458 * @dst should be big enough to hold list of user page addresses and
459 * links to the next pages of buffer
461 void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
465 phys_addr_t optee_page;
467 * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
471 u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
476 * Currently OP-TEE uses 4k page size and it does not looks
477 * like this will change in the future. On other hand, there are
478 * no know ARM architectures with page size < 4k.
479 * Thus the next built assert looks redundant. But the following
480 * code heavily relies on this assumption, so it is better be
483 BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
485 pages_data = (void *)dst;
487 * If linux page is bigger than 4k, and user buffer offset is
488 * larger than 4k/8k/12k/etc this will skip first 4k pages,
489 * because they bear no value data for OP-TEE.
491 optee_page = page_to_phys(*pages) +
492 round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
495 pages_data->pages_list[n++] = optee_page;
497 if (n == PAGELIST_ENTRIES_PER_PAGE) {
498 pages_data->next_page_data =
499 virt_to_phys(pages_data + 1);
504 optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
505 if (!(optee_page & ~PAGE_MASK)) {
509 optee_page = page_to_phys(*pages);
515 * The final entry in each pagelist page is a pointer to the next
518 static size_t get_pages_list_size(size_t num_entries)
520 int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
522 return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
525 u64 *optee_allocate_pages_list(size_t num_entries)
527 return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
530 void optee_free_pages_list(void *list, size_t num_entries)
532 free_pages_exact(list, get_pages_list_size(num_entries));
535 static bool is_normal_memory(pgprot_t p)
537 #if defined(CONFIG_ARM)
538 return (((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEALLOC) ||
539 ((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEBACK));
540 #elif defined(CONFIG_ARM64)
541 return (pgprot_val(p) & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL);
543 #error "Unuspported architecture"
547 static int __check_mem_type(struct vm_area_struct *vma, unsigned long end)
549 while (vma && is_normal_memory(vma->vm_page_prot)) {
550 if (vma->vm_end >= end)
558 static int check_mem_type(unsigned long start, size_t num_pages)
560 struct mm_struct *mm = current->mm;
564 * Allow kernel address to register with OP-TEE as kernel
565 * pages are configured as normal memory only.
567 if (virt_addr_valid(start))
571 rc = __check_mem_type(find_vma(mm, start),
572 start + num_pages * PAGE_SIZE);
573 mmap_read_unlock(mm);
578 int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
579 struct page **pages, size_t num_pages,
582 struct tee_shm *shm_arg = NULL;
583 struct optee_msg_arg *msg_arg;
585 phys_addr_t msg_parg;
591 rc = check_mem_type(start, num_pages);
595 pages_list = optee_allocate_pages_list(num_pages);
599 shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
600 if (IS_ERR(shm_arg)) {
601 rc = PTR_ERR(shm_arg);
605 optee_fill_pages_list(pages_list, pages, num_pages,
606 tee_shm_get_page_offset(shm));
608 msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
609 msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
610 OPTEE_MSG_ATTR_NONCONTIG;
611 msg_arg->params->u.tmem.shm_ref = (unsigned long)shm;
612 msg_arg->params->u.tmem.size = tee_shm_get_size(shm);
614 * In the least bits of msg_arg->params->u.tmem.buf_ptr we
615 * store buffer offset from 4k page, as described in OP-TEE ABI.
617 msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
618 (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
620 if (optee_do_call_with_arg(ctx, msg_parg) ||
621 msg_arg->ret != TEEC_SUCCESS)
624 tee_shm_free(shm_arg);
626 optee_free_pages_list(pages_list, num_pages);
630 int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
632 struct tee_shm *shm_arg;
633 struct optee_msg_arg *msg_arg;
634 phys_addr_t msg_parg;
637 shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
639 return PTR_ERR(shm_arg);
641 msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
643 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
644 msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
646 if (optee_do_call_with_arg(ctx, msg_parg) ||
647 msg_arg->ret != TEEC_SUCCESS)
649 tee_shm_free(shm_arg);
653 int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
654 struct page **pages, size_t num_pages,
658 * We don't want to register supplicant memory in OP-TEE.
659 * Instead information about it will be passed in RPC code.
661 return check_mem_type(start, num_pages);
664 int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm)