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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/arm-smccc.h>
18 #include <linux/errno.h>
20 #include <linux/module.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/tee_drv.h>
27 #include <linux/types.h>
28 #include <linux/uaccess.h>
29 #include "optee_private.h"
30 #include "optee_smc.h"
33 #define DRIVER_NAME "optee"
35 #define OPTEE_SHM_NUM_PRIV_PAGES CONFIG_OPTEE_SHM_NUM_PRIV_PAGES
38 * optee_from_msg_param() - convert from OPTEE_MSG parameters to
40 * @params: subsystem internal parameter representation
41 * @num_params: number of elements in the parameter arrays
42 * @msg_params: OPTEE_MSG parameters
43 * Returns 0 on success or <0 on failure
45 int optee_from_msg_param(struct tee_param *params, size_t num_params,
46 const struct optee_msg_param *msg_params)
53 for (n = 0; n < num_params; n++) {
54 struct tee_param *p = params + n;
55 const struct optee_msg_param *mp = msg_params + n;
56 u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
59 case OPTEE_MSG_ATTR_TYPE_NONE:
60 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
61 memset(&p->u, 0, sizeof(p->u));
63 case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
64 case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
65 case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
66 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
67 attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
68 p->u.value.a = mp->u.value.a;
69 p->u.value.b = mp->u.value.b;
70 p->u.value.c = mp->u.value.c;
72 case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT:
73 case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT:
74 case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT:
75 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
76 attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
77 p->u.memref.size = mp->u.tmem.size;
78 shm = (struct tee_shm *)(unsigned long)
81 p->u.memref.shm_offs = 0;
82 p->u.memref.shm = NULL;
85 rc = tee_shm_get_pa(shm, 0, &pa);
88 p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
89 p->u.memref.shm = shm;
91 case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
92 case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
93 case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
94 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
95 attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
96 p->u.memref.size = mp->u.rmem.size;
97 shm = (struct tee_shm *)(unsigned long)
101 p->u.memref.shm_offs = 0;
102 p->u.memref.shm = NULL;
105 p->u.memref.shm_offs = mp->u.rmem.offs;
106 p->u.memref.shm = shm;
117 static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
118 const struct tee_param *p)
123 mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
124 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
126 mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
127 mp->u.tmem.size = p->u.memref.size;
129 if (!p->u.memref.shm) {
130 mp->u.tmem.buf_ptr = 0;
134 rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
138 mp->u.tmem.buf_ptr = pa;
139 mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
140 OPTEE_MSG_ATTR_CACHE_SHIFT;
145 static int to_msg_param_reg_mem(struct optee_msg_param *mp,
146 const struct tee_param *p)
148 mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
149 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
151 mp->u.rmem.shm_ref = (unsigned long)p->u.memref.shm;
152 mp->u.rmem.size = p->u.memref.size;
153 mp->u.rmem.offs = p->u.memref.shm_offs;
158 * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters
159 * @msg_params: OPTEE_MSG parameters
160 * @num_params: number of elements in the parameter arrays
161 * @params: subsystem itnernal parameter representation
162 * Returns 0 on success or <0 on failure
164 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
165 const struct tee_param *params)
170 for (n = 0; n < num_params; n++) {
171 const struct tee_param *p = params + n;
172 struct optee_msg_param *mp = msg_params + n;
175 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
176 mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
177 memset(&mp->u, 0, sizeof(mp->u));
179 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
180 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
181 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
182 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
183 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
184 mp->u.value.a = p->u.value.a;
185 mp->u.value.b = p->u.value.b;
186 mp->u.value.c = p->u.value.c;
188 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
189 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
190 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
191 if (tee_shm_is_registered(p->u.memref.shm))
192 rc = to_msg_param_reg_mem(mp, p);
194 rc = to_msg_param_tmp_mem(mp, p);
205 static void optee_get_version(struct tee_device *teedev,
206 struct tee_ioctl_version_data *vers)
208 struct tee_ioctl_version_data v = {
209 .impl_id = TEE_IMPL_ID_OPTEE,
210 .impl_caps = TEE_OPTEE_CAP_TZ,
211 .gen_caps = TEE_GEN_CAP_GP,
213 struct optee *optee = tee_get_drvdata(teedev);
215 if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
216 v.gen_caps |= TEE_GEN_CAP_REG_MEM;
220 static int optee_open(struct tee_context *ctx)
222 struct optee_context_data *ctxdata;
223 struct tee_device *teedev = ctx->teedev;
224 struct optee *optee = tee_get_drvdata(teedev);
226 ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
230 if (teedev == optee->supp_teedev) {
233 mutex_lock(&optee->supp.mutex);
234 if (!optee->supp.ctx) {
236 optee->supp.ctx = ctx;
238 mutex_unlock(&optee->supp.mutex);
245 mutex_init(&ctxdata->mutex);
246 INIT_LIST_HEAD(&ctxdata->sess_list);
252 static void optee_release(struct tee_context *ctx)
254 struct optee_context_data *ctxdata = ctx->data;
255 struct tee_device *teedev = ctx->teedev;
256 struct optee *optee = tee_get_drvdata(teedev);
258 struct optee_msg_arg *arg = NULL;
260 struct optee_session *sess;
261 struct optee_session *sess_tmp;
266 shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED);
268 arg = tee_shm_get_va(shm, 0);
270 * If va2pa fails for some reason, we can't call into
271 * secure world, only free the memory. Secure OS will leak
272 * sessions and finally refuse more sessions, but we will
273 * at least let normal world reclaim its memory.
276 if (tee_shm_va2pa(shm, arg, &parg))
277 arg = NULL; /* prevent usage of parg below */
280 list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
282 list_del(&sess->list_node);
283 if (!IS_ERR_OR_NULL(arg)) {
284 memset(arg, 0, sizeof(*arg));
285 arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
286 arg->session = sess->session_id;
287 optee_do_call_with_arg(ctx, parg);
298 if (teedev == optee->supp_teedev)
299 optee_supp_release(&optee->supp);
302 static const struct tee_driver_ops optee_ops = {
303 .get_version = optee_get_version,
305 .release = optee_release,
306 .open_session = optee_open_session,
307 .close_session = optee_close_session,
308 .invoke_func = optee_invoke_func,
309 .cancel_req = optee_cancel_req,
310 .shm_register = optee_shm_register,
311 .shm_unregister = optee_shm_unregister,
314 static const struct tee_desc optee_desc = {
315 .name = DRIVER_NAME "-clnt",
317 .owner = THIS_MODULE,
320 static const struct tee_driver_ops optee_supp_ops = {
321 .get_version = optee_get_version,
323 .release = optee_release,
324 .supp_recv = optee_supp_recv,
325 .supp_send = optee_supp_send,
326 .shm_register = optee_shm_register_supp,
327 .shm_unregister = optee_shm_unregister_supp,
330 static const struct tee_desc optee_supp_desc = {
331 .name = DRIVER_NAME "-supp",
332 .ops = &optee_supp_ops,
333 .owner = THIS_MODULE,
334 .flags = TEE_DESC_PRIVILEGED,
337 static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
339 struct arm_smccc_res res;
341 invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
343 if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
344 res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3)
349 static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn)
352 struct arm_smccc_res smccc;
353 struct optee_smc_call_get_os_revision_result result;
360 invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
363 if (res.result.build_id)
364 pr_info("revision %lu.%lu (%08lx)", res.result.major,
365 res.result.minor, res.result.build_id);
367 pr_info("revision %lu.%lu", res.result.major, res.result.minor);
370 static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
373 struct arm_smccc_res smccc;
374 struct optee_smc_calls_revision_result result;
377 invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
379 if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
380 (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
385 static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
389 struct arm_smccc_res smccc;
390 struct optee_smc_exchange_capabilities_result result;
395 * TODO This isn't enough to tell if it's UP system (from kernel
396 * point of view) or not, is_smp() returns the the information
397 * needed, but can't be called directly from here.
399 if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
400 a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
402 invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
405 if (res.result.status != OPTEE_SMC_RETURN_OK)
408 *sec_caps = res.result.capabilities;
412 static struct tee_shm_pool *
413 optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
417 struct arm_smccc_res smccc;
418 struct optee_smc_get_shm_config_result result;
426 struct tee_shm_pool_mgr *priv_mgr;
427 struct tee_shm_pool_mgr *dmabuf_mgr;
430 invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
431 if (res.result.status != OPTEE_SMC_RETURN_OK) {
432 pr_info("shm service not available\n");
433 return ERR_PTR(-ENOENT);
436 if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
437 pr_err("only normal cached shared memory supported\n");
438 return ERR_PTR(-EINVAL);
441 begin = roundup(res.result.start, PAGE_SIZE);
442 end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
446 if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) {
447 pr_err("too small shared memory area\n");
448 return ERR_PTR(-EINVAL);
451 va = memremap(paddr, size, MEMREMAP_WB);
453 pr_err("shared memory ioremap failed\n");
454 return ERR_PTR(-EINVAL);
456 vaddr = (unsigned long)va;
459 * If OP-TEE can work with unregistered SHM, we will use own pool
462 if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
463 rc = optee_shm_pool_alloc_pages();
468 const size_t sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
470 rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
471 3 /* 8 bytes aligned */);
481 rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
483 goto err_free_priv_mgr;
486 rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
488 goto err_free_dmabuf_mgr;
490 *memremaped_shm = va;
495 tee_shm_pool_mgr_destroy(dmabuf_mgr);
497 tee_shm_pool_mgr_destroy(priv_mgr);
503 /* Simple wrapper functions to be able to use a function pointer */
504 static void optee_smccc_smc(unsigned long a0, unsigned long a1,
505 unsigned long a2, unsigned long a3,
506 unsigned long a4, unsigned long a5,
507 unsigned long a6, unsigned long a7,
508 struct arm_smccc_res *res)
510 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
513 static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
514 unsigned long a2, unsigned long a3,
515 unsigned long a4, unsigned long a5,
516 unsigned long a6, unsigned long a7,
517 struct arm_smccc_res *res)
519 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
522 static optee_invoke_fn *get_invoke_func(struct device_node *np)
526 pr_info("probing for conduit method from DT.\n");
528 if (of_property_read_string(np, "method", &method)) {
529 pr_warn("missing \"method\" property\n");
530 return ERR_PTR(-ENXIO);
533 if (!strcmp("hvc", method))
534 return optee_smccc_hvc;
535 else if (!strcmp("smc", method))
536 return optee_smccc_smc;
538 pr_warn("invalid \"method\" property: %s\n", method);
539 return ERR_PTR(-EINVAL);
542 static struct optee *optee_probe(struct device_node *np)
544 optee_invoke_fn *invoke_fn;
545 struct tee_shm_pool *pool;
546 struct optee *optee = NULL;
547 void *memremaped_shm = NULL;
548 struct tee_device *teedev;
552 invoke_fn = get_invoke_func(np);
553 if (IS_ERR(invoke_fn))
554 return (void *)invoke_fn;
556 if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
557 pr_warn("api uid mismatch\n");
558 return ERR_PTR(-EINVAL);
561 optee_msg_get_os_revision(invoke_fn);
563 if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
564 pr_warn("api revision mismatch\n");
565 return ERR_PTR(-EINVAL);
568 if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {
569 pr_warn("capabilities mismatch\n");
570 return ERR_PTR(-EINVAL);
574 * We have no other option for shared memory, if secure world
575 * doesn't have any reserved memory we can use we can't continue.
577 if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
578 return ERR_PTR(-EINVAL);
580 pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm, sec_caps);
584 optee = kzalloc(sizeof(*optee), GFP_KERNEL);
590 optee->invoke_fn = invoke_fn;
591 optee->sec_caps = sec_caps;
593 teedev = tee_device_alloc(&optee_desc, NULL, pool, optee);
594 if (IS_ERR(teedev)) {
595 rc = PTR_ERR(teedev);
598 optee->teedev = teedev;
600 teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
601 if (IS_ERR(teedev)) {
602 rc = PTR_ERR(teedev);
605 optee->supp_teedev = teedev;
607 rc = tee_device_register(optee->teedev);
611 rc = tee_device_register(optee->supp_teedev);
615 mutex_init(&optee->call_queue.mutex);
616 INIT_LIST_HEAD(&optee->call_queue.waiters);
617 optee_wait_queue_init(&optee->wait_queue);
618 optee_supp_init(&optee->supp);
619 optee->memremaped_shm = memremaped_shm;
623 * Ensure that there are no pre-existing shm objects before enabling
624 * the shm cache so that there's no chance of receiving an invalid
625 * address during shutdown. This could occur, for example, if we're
626 * kexec booting from an older kernel that did not properly cleanup the
629 optee_disable_unmapped_shm_cache(optee);
631 optee_enable_shm_cache(optee);
633 pr_info("initialized driver\n");
638 * tee_device_unregister() is safe to call even if the
639 * devices hasn't been registered with
640 * tee_device_register() yet.
642 tee_device_unregister(optee->supp_teedev);
643 tee_device_unregister(optee->teedev);
647 tee_shm_pool_free(pool);
649 memunmap(memremaped_shm);
653 static void optee_remove(struct optee *optee)
656 * Ask OP-TEE to free all cached shared memory objects to decrease
657 * reference counters and also avoid wild pointers in secure world
658 * into the old shared memory range.
660 optee_disable_shm_cache(optee);
663 * The two devices has to be unregistered before we can free the
666 tee_device_unregister(optee->supp_teedev);
667 tee_device_unregister(optee->teedev);
669 tee_shm_pool_free(optee->pool);
670 if (optee->memremaped_shm)
671 memunmap(optee->memremaped_shm);
672 optee_wait_queue_exit(&optee->wait_queue);
673 optee_supp_uninit(&optee->supp);
674 mutex_destroy(&optee->call_queue.mutex);
679 static const struct of_device_id optee_match[] = {
680 { .compatible = "linaro,optee-tz" },
684 static struct optee *optee_svc;
686 static int __init optee_driver_init(void)
688 struct device_node *fw_np;
689 struct device_node *np;
692 /* Node is supposed to be below /firmware */
693 fw_np = of_find_node_by_name(NULL, "firmware");
697 np = of_find_matching_node(fw_np, optee_match);
698 if (!np || !of_device_is_available(np)) {
703 optee = optee_probe(np);
707 return PTR_ERR(optee);
713 module_init(optee_driver_init);
715 static void __exit optee_driver_exit(void)
717 struct optee *optee = optee_svc;
723 module_exit(optee_driver_exit);
725 MODULE_AUTHOR("Linaro");
726 MODULE_DESCRIPTION("OP-TEE driver");
727 MODULE_SUPPORTED_DEVICE("");
728 MODULE_VERSION("1.0");
729 MODULE_LICENSE("GPL v2");