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)) {
158 optee_handle_rpc(ctx, ¶m, &call_ctx);
165 optee_rpc_finalize_call(&call_ctx);
167 * We're done with our thread in secure world, if there's any
168 * thread waiters wake up one.
170 optee_cq_wait_final(&optee->call_queue, &w);
175 static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params,
176 struct optee_msg_arg **msg_arg,
177 phys_addr_t *msg_parg)
181 struct optee_msg_arg *ma;
183 shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
188 ma = tee_shm_get_va(shm, 0);
194 rc = tee_shm_get_pa(shm, 0, msg_parg);
198 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
199 ma->num_params = num_params;
210 int optee_open_session(struct tee_context *ctx,
211 struct tee_ioctl_open_session_arg *arg,
212 struct tee_param *param)
214 struct optee_context_data *ctxdata = ctx->data;
217 struct optee_msg_arg *msg_arg;
218 phys_addr_t msg_parg;
219 struct optee_session *sess = NULL;
221 /* +2 for the meta parameters added below */
222 shm = get_msg_arg(ctx, arg->num_params + 2, &msg_arg, &msg_parg);
226 msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
227 msg_arg->cancel_id = arg->cancel_id;
230 * Initialize and add the meta parameters needed when opening a
233 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
235 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
237 memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
238 msg_arg->params[1].u.value.c = arg->clnt_login;
240 rc = tee_session_calc_client_uuid((uuid_t *)&msg_arg->params[1].u.value,
241 arg->clnt_login, arg->clnt_uuid);
245 rc = optee_to_msg_param(msg_arg->params + 2, arg->num_params, param);
249 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
255 if (optee_do_call_with_arg(ctx, msg_parg)) {
256 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
257 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
260 if (msg_arg->ret == TEEC_SUCCESS) {
261 /* A new session has been created, add it to the list. */
262 sess->session_id = msg_arg->session;
263 mutex_lock(&ctxdata->mutex);
264 list_add(&sess->list_node, &ctxdata->sess_list);
265 mutex_unlock(&ctxdata->mutex);
270 if (optee_from_msg_param(param, arg->num_params, msg_arg->params + 2)) {
271 arg->ret = TEEC_ERROR_COMMUNICATION;
272 arg->ret_origin = TEEC_ORIGIN_COMMS;
273 /* Close session again to avoid leakage */
274 optee_close_session(ctx, msg_arg->session);
276 arg->session = msg_arg->session;
277 arg->ret = msg_arg->ret;
278 arg->ret_origin = msg_arg->ret_origin;
286 int optee_close_session(struct tee_context *ctx, u32 session)
288 struct optee_context_data *ctxdata = ctx->data;
290 struct optee_msg_arg *msg_arg;
291 phys_addr_t msg_parg;
292 struct optee_session *sess;
294 /* Check that the session is valid and remove it from the list */
295 mutex_lock(&ctxdata->mutex);
296 sess = find_session(ctxdata, session);
298 list_del(&sess->list_node);
299 mutex_unlock(&ctxdata->mutex);
304 shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
308 msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
309 msg_arg->session = session;
310 optee_do_call_with_arg(ctx, msg_parg);
316 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
317 struct tee_param *param)
319 struct optee_context_data *ctxdata = ctx->data;
321 struct optee_msg_arg *msg_arg;
322 phys_addr_t msg_parg;
323 struct optee_session *sess;
326 /* Check that the session is valid */
327 mutex_lock(&ctxdata->mutex);
328 sess = find_session(ctxdata, arg->session);
329 mutex_unlock(&ctxdata->mutex);
333 shm = get_msg_arg(ctx, arg->num_params, &msg_arg, &msg_parg);
336 msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
337 msg_arg->func = arg->func;
338 msg_arg->session = arg->session;
339 msg_arg->cancel_id = arg->cancel_id;
341 rc = optee_to_msg_param(msg_arg->params, arg->num_params, param);
345 if (optee_do_call_with_arg(ctx, msg_parg)) {
346 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
347 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
350 if (optee_from_msg_param(param, arg->num_params, msg_arg->params)) {
351 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
352 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
355 arg->ret = msg_arg->ret;
356 arg->ret_origin = msg_arg->ret_origin;
362 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
364 struct optee_context_data *ctxdata = ctx->data;
366 struct optee_msg_arg *msg_arg;
367 phys_addr_t msg_parg;
368 struct optee_session *sess;
370 /* Check that the session is valid */
371 mutex_lock(&ctxdata->mutex);
372 sess = find_session(ctxdata, session);
373 mutex_unlock(&ctxdata->mutex);
377 shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
381 msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
382 msg_arg->session = session;
383 msg_arg->cancel_id = cancel_id;
384 optee_do_call_with_arg(ctx, msg_parg);
391 * optee_enable_shm_cache() - Enables caching of some shared memory allocation
393 * @optee: main service struct
395 void optee_enable_shm_cache(struct optee *optee)
397 struct optee_call_waiter w;
399 /* We need to retry until secure world isn't busy. */
400 optee_cq_wait_init(&optee->call_queue, &w);
402 struct arm_smccc_res res;
404 optee->invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
406 if (res.a0 == OPTEE_SMC_RETURN_OK)
408 optee_cq_wait_for_completion(&optee->call_queue, &w);
410 optee_cq_wait_final(&optee->call_queue, &w);
414 * optee_disable_shm_cache() - Disables caching of some shared memory allocation
416 * @optee: main service struct
418 void optee_disable_shm_cache(struct optee *optee)
420 struct optee_call_waiter w;
422 /* We need to retry until secure world isn't busy. */
423 optee_cq_wait_init(&optee->call_queue, &w);
426 struct arm_smccc_res smccc;
427 struct optee_smc_disable_shm_cache_result result;
430 optee->invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
432 if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
433 break; /* All shm's freed */
434 if (res.result.status == OPTEE_SMC_RETURN_OK) {
437 shm = reg_pair_to_ptr(res.result.shm_upper32,
438 res.result.shm_lower32);
441 optee_cq_wait_for_completion(&optee->call_queue, &w);
444 optee_cq_wait_final(&optee->call_queue, &w);
447 #define PAGELIST_ENTRIES_PER_PAGE \
448 ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
451 * optee_fill_pages_list() - write list of user pages to given shared
454 * @dst: page-aligned buffer where list of pages will be stored
455 * @pages: array of pages that represents shared buffer
456 * @num_pages: number of entries in @pages
457 * @page_offset: offset of user buffer from page start
459 * @dst should be big enough to hold list of user page addresses and
460 * links to the next pages of buffer
462 void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
466 phys_addr_t optee_page;
468 * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
472 u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
477 * Currently OP-TEE uses 4k page size and it does not looks
478 * like this will change in the future. On other hand, there are
479 * no know ARM architectures with page size < 4k.
480 * Thus the next built assert looks redundant. But the following
481 * code heavily relies on this assumption, so it is better be
484 BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
486 pages_data = (void *)dst;
488 * If linux page is bigger than 4k, and user buffer offset is
489 * larger than 4k/8k/12k/etc this will skip first 4k pages,
490 * because they bear no value data for OP-TEE.
492 optee_page = page_to_phys(*pages) +
493 round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
496 pages_data->pages_list[n++] = optee_page;
498 if (n == PAGELIST_ENTRIES_PER_PAGE) {
499 pages_data->next_page_data =
500 virt_to_phys(pages_data + 1);
505 optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
506 if (!(optee_page & ~PAGE_MASK)) {
510 optee_page = page_to_phys(*pages);
516 * The final entry in each pagelist page is a pointer to the next
519 static size_t get_pages_list_size(size_t num_entries)
521 int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
523 return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
526 u64 *optee_allocate_pages_list(size_t num_entries)
528 return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
531 void optee_free_pages_list(void *list, size_t num_entries)
533 free_pages_exact(list, get_pages_list_size(num_entries));
536 static bool is_normal_memory(pgprot_t p)
538 #if defined(CONFIG_ARM)
539 return (((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEALLOC) ||
540 ((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEBACK));
541 #elif defined(CONFIG_ARM64)
542 return (pgprot_val(p) & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL);
544 #error "Unuspported architecture"
548 static int __check_mem_type(struct vm_area_struct *vma, unsigned long end)
550 while (vma && is_normal_memory(vma->vm_page_prot)) {
551 if (vma->vm_end >= end)
559 static int check_mem_type(unsigned long start, size_t num_pages)
561 struct mm_struct *mm = current->mm;
565 * Allow kernel address to register with OP-TEE as kernel
566 * pages are configured as normal memory only.
568 if (virt_addr_valid(start))
572 rc = __check_mem_type(find_vma(mm, start),
573 start + num_pages * PAGE_SIZE);
574 mmap_read_unlock(mm);
579 int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
580 struct page **pages, size_t num_pages,
583 struct tee_shm *shm_arg = NULL;
584 struct optee_msg_arg *msg_arg;
586 phys_addr_t msg_parg;
592 rc = check_mem_type(start, num_pages);
596 pages_list = optee_allocate_pages_list(num_pages);
600 shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
601 if (IS_ERR(shm_arg)) {
602 rc = PTR_ERR(shm_arg);
606 optee_fill_pages_list(pages_list, pages, num_pages,
607 tee_shm_get_page_offset(shm));
609 msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
610 msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
611 OPTEE_MSG_ATTR_NONCONTIG;
612 msg_arg->params->u.tmem.shm_ref = (unsigned long)shm;
613 msg_arg->params->u.tmem.size = tee_shm_get_size(shm);
615 * In the least bits of msg_arg->params->u.tmem.buf_ptr we
616 * store buffer offset from 4k page, as described in OP-TEE ABI.
618 msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
619 (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
621 if (optee_do_call_with_arg(ctx, msg_parg) ||
622 msg_arg->ret != TEEC_SUCCESS)
625 tee_shm_free(shm_arg);
627 optee_free_pages_list(pages_list, num_pages);
631 int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
633 struct tee_shm *shm_arg;
634 struct optee_msg_arg *msg_arg;
635 phys_addr_t msg_parg;
638 shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
640 return PTR_ERR(shm_arg);
642 msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
644 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
645 msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
647 if (optee_do_call_with_arg(ctx, msg_parg) ||
648 msg_arg->ret != TEEC_SUCCESS)
650 tee_shm_free(shm_arg);
654 int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
655 struct page **pages, size_t num_pages,
659 * We don't want to register supplicant memory in OP-TEE.
660 * Instead information about it will be passed in RPC code.
662 return check_mem_type(start, num_pages);
665 int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm)