1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Linaro Limited
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/arm-smccc.h>
9 #include <linux/errno.h>
11 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/tee_drv.h>
18 #include <linux/types.h>
19 #include <linux/uaccess.h>
20 #include <linux/workqueue.h>
21 #include "optee_private.h"
22 #include "optee_smc.h"
25 #define DRIVER_NAME "optee"
27 #define OPTEE_SHM_NUM_PRIV_PAGES CONFIG_OPTEE_SHM_NUM_PRIV_PAGES
30 * optee_from_msg_param() - convert from OPTEE_MSG parameters to
32 * @params: subsystem internal parameter representation
33 * @num_params: number of elements in the parameter arrays
34 * @msg_params: OPTEE_MSG parameters
35 * Returns 0 on success or <0 on failure
37 int optee_from_msg_param(struct tee_param *params, size_t num_params,
38 const struct optee_msg_param *msg_params)
45 for (n = 0; n < num_params; n++) {
46 struct tee_param *p = params + n;
47 const struct optee_msg_param *mp = msg_params + n;
48 u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
51 case OPTEE_MSG_ATTR_TYPE_NONE:
52 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
53 memset(&p->u, 0, sizeof(p->u));
55 case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
56 case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
57 case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
58 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
59 attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
60 p->u.value.a = mp->u.value.a;
61 p->u.value.b = mp->u.value.b;
62 p->u.value.c = mp->u.value.c;
64 case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT:
65 case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT:
66 case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT:
67 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
68 attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
69 p->u.memref.size = mp->u.tmem.size;
70 shm = (struct tee_shm *)(unsigned long)
73 p->u.memref.shm_offs = 0;
74 p->u.memref.shm = NULL;
77 rc = tee_shm_get_pa(shm, 0, &pa);
80 p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
81 p->u.memref.shm = shm;
83 /* Check that the memref is covered by the shm object */
84 if (p->u.memref.size) {
85 size_t o = p->u.memref.shm_offs +
88 rc = tee_shm_get_pa(shm, o, NULL);
93 case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
94 case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
95 case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
96 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
97 attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
98 p->u.memref.size = mp->u.rmem.size;
99 shm = (struct tee_shm *)(unsigned long)
103 p->u.memref.shm_offs = 0;
104 p->u.memref.shm = NULL;
107 p->u.memref.shm_offs = mp->u.rmem.offs;
108 p->u.memref.shm = shm;
119 static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
120 const struct tee_param *p)
125 mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
126 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
128 mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
129 mp->u.tmem.size = p->u.memref.size;
131 if (!p->u.memref.shm) {
132 mp->u.tmem.buf_ptr = 0;
136 rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
140 mp->u.tmem.buf_ptr = pa;
141 mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
142 OPTEE_MSG_ATTR_CACHE_SHIFT;
147 static int to_msg_param_reg_mem(struct optee_msg_param *mp,
148 const struct tee_param *p)
150 mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
151 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
153 mp->u.rmem.shm_ref = (unsigned long)p->u.memref.shm;
154 mp->u.rmem.size = p->u.memref.size;
155 mp->u.rmem.offs = p->u.memref.shm_offs;
160 * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters
161 * @msg_params: OPTEE_MSG parameters
162 * @num_params: number of elements in the parameter arrays
163 * @params: subsystem itnernal parameter representation
164 * Returns 0 on success or <0 on failure
166 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
167 const struct tee_param *params)
172 for (n = 0; n < num_params; n++) {
173 const struct tee_param *p = params + n;
174 struct optee_msg_param *mp = msg_params + n;
177 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
178 mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
179 memset(&mp->u, 0, sizeof(mp->u));
181 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
182 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
183 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
184 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
185 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
186 mp->u.value.a = p->u.value.a;
187 mp->u.value.b = p->u.value.b;
188 mp->u.value.c = p->u.value.c;
190 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
191 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
192 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
193 if (tee_shm_is_registered(p->u.memref.shm))
194 rc = to_msg_param_reg_mem(mp, p);
196 rc = to_msg_param_tmp_mem(mp, p);
207 static void optee_get_version(struct tee_device *teedev,
208 struct tee_ioctl_version_data *vers)
210 struct tee_ioctl_version_data v = {
211 .impl_id = TEE_IMPL_ID_OPTEE,
212 .impl_caps = TEE_OPTEE_CAP_TZ,
213 .gen_caps = TEE_GEN_CAP_GP,
215 struct optee *optee = tee_get_drvdata(teedev);
217 if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
218 v.gen_caps |= TEE_GEN_CAP_REG_MEM;
219 if (optee->sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
220 v.gen_caps |= TEE_GEN_CAP_MEMREF_NULL;
224 static void optee_bus_scan(struct work_struct *work)
226 WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
229 static int optee_open(struct tee_context *ctx)
231 struct optee_context_data *ctxdata;
232 struct tee_device *teedev = ctx->teedev;
233 struct optee *optee = tee_get_drvdata(teedev);
235 ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
239 if (teedev == optee->supp_teedev) {
242 mutex_lock(&optee->supp.mutex);
243 if (!optee->supp.ctx) {
245 optee->supp.ctx = ctx;
247 mutex_unlock(&optee->supp.mutex);
253 if (!optee->scan_bus_done) {
254 INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
255 optee->scan_bus_wq = create_workqueue("optee_bus_scan");
256 if (!optee->scan_bus_wq) {
260 queue_work(optee->scan_bus_wq, &optee->scan_bus_work);
261 optee->scan_bus_done = true;
264 mutex_init(&ctxdata->mutex);
265 INIT_LIST_HEAD(&ctxdata->sess_list);
267 if (optee->sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
268 ctx->cap_memref_null = true;
270 ctx->cap_memref_null = false;
276 static void optee_release(struct tee_context *ctx)
278 struct optee_context_data *ctxdata = ctx->data;
279 struct tee_device *teedev = ctx->teedev;
280 struct optee *optee = tee_get_drvdata(teedev);
282 struct optee_msg_arg *arg = NULL;
284 struct optee_session *sess;
285 struct optee_session *sess_tmp;
290 shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED);
292 arg = tee_shm_get_va(shm, 0);
294 * If va2pa fails for some reason, we can't call into
295 * secure world, only free the memory. Secure OS will leak
296 * sessions and finally refuse more sessions, but we will
297 * at least let normal world reclaim its memory.
300 if (tee_shm_va2pa(shm, arg, &parg))
301 arg = NULL; /* prevent usage of parg below */
304 list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
306 list_del(&sess->list_node);
307 if (!IS_ERR_OR_NULL(arg)) {
308 memset(arg, 0, sizeof(*arg));
309 arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
310 arg->session = sess->session_id;
311 optee_do_call_with_arg(ctx, parg);
322 if (teedev == optee->supp_teedev) {
323 if (optee->scan_bus_wq) {
324 destroy_workqueue(optee->scan_bus_wq);
325 optee->scan_bus_wq = NULL;
327 optee_supp_release(&optee->supp);
331 static const struct tee_driver_ops optee_ops = {
332 .get_version = optee_get_version,
334 .release = optee_release,
335 .open_session = optee_open_session,
336 .close_session = optee_close_session,
337 .invoke_func = optee_invoke_func,
338 .cancel_req = optee_cancel_req,
339 .shm_register = optee_shm_register,
340 .shm_unregister = optee_shm_unregister,
343 static const struct tee_desc optee_desc = {
344 .name = DRIVER_NAME "-clnt",
346 .owner = THIS_MODULE,
349 static const struct tee_driver_ops optee_supp_ops = {
350 .get_version = optee_get_version,
352 .release = optee_release,
353 .supp_recv = optee_supp_recv,
354 .supp_send = optee_supp_send,
355 .shm_register = optee_shm_register_supp,
356 .shm_unregister = optee_shm_unregister_supp,
359 static const struct tee_desc optee_supp_desc = {
360 .name = DRIVER_NAME "-supp",
361 .ops = &optee_supp_ops,
362 .owner = THIS_MODULE,
363 .flags = TEE_DESC_PRIVILEGED,
366 static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
368 struct arm_smccc_res res;
370 invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
372 if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
373 res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3)
378 static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn)
381 struct arm_smccc_res smccc;
382 struct optee_smc_call_get_os_revision_result result;
389 invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
392 if (res.result.build_id)
393 pr_info("revision %lu.%lu (%08lx)", res.result.major,
394 res.result.minor, res.result.build_id);
396 pr_info("revision %lu.%lu", res.result.major, res.result.minor);
399 static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
402 struct arm_smccc_res smccc;
403 struct optee_smc_calls_revision_result result;
406 invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
408 if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
409 (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
414 static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
418 struct arm_smccc_res smccc;
419 struct optee_smc_exchange_capabilities_result result;
424 * TODO This isn't enough to tell if it's UP system (from kernel
425 * point of view) or not, is_smp() returns the the information
426 * needed, but can't be called directly from here.
428 if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
429 a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
431 invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
434 if (res.result.status != OPTEE_SMC_RETURN_OK)
437 *sec_caps = res.result.capabilities;
441 static struct tee_shm_pool *optee_config_dyn_shm(void)
443 struct tee_shm_pool_mgr *priv_mgr;
444 struct tee_shm_pool_mgr *dmabuf_mgr;
447 rc = optee_shm_pool_alloc_pages();
452 rc = optee_shm_pool_alloc_pages();
454 tee_shm_pool_mgr_destroy(priv_mgr);
459 rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
461 tee_shm_pool_mgr_destroy(priv_mgr);
462 tee_shm_pool_mgr_destroy(dmabuf_mgr);
468 static struct tee_shm_pool *
469 optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
472 struct arm_smccc_res smccc;
473 struct optee_smc_get_shm_config_result result;
481 struct tee_shm_pool_mgr *priv_mgr;
482 struct tee_shm_pool_mgr *dmabuf_mgr;
484 const int sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
486 invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
487 if (res.result.status != OPTEE_SMC_RETURN_OK) {
488 pr_err("static shm service not available\n");
489 return ERR_PTR(-ENOENT);
492 if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
493 pr_err("only normal cached shared memory supported\n");
494 return ERR_PTR(-EINVAL);
497 begin = roundup(res.result.start, PAGE_SIZE);
498 end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
502 if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) {
503 pr_err("too small shared memory area\n");
504 return ERR_PTR(-EINVAL);
507 va = memremap(paddr, size, MEMREMAP_WB);
509 pr_err("shared memory ioremap failed\n");
510 return ERR_PTR(-EINVAL);
512 vaddr = (unsigned long)va;
514 rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
515 3 /* 8 bytes aligned */);
524 rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
526 goto err_free_priv_mgr;
529 rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
531 goto err_free_dmabuf_mgr;
533 *memremaped_shm = va;
538 tee_shm_pool_mgr_destroy(dmabuf_mgr);
540 tee_shm_pool_mgr_destroy(priv_mgr);
546 /* Simple wrapper functions to be able to use a function pointer */
547 static void optee_smccc_smc(unsigned long a0, unsigned long a1,
548 unsigned long a2, unsigned long a3,
549 unsigned long a4, unsigned long a5,
550 unsigned long a6, unsigned long a7,
551 struct arm_smccc_res *res)
553 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
556 static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
557 unsigned long a2, unsigned long a3,
558 unsigned long a4, unsigned long a5,
559 unsigned long a6, unsigned long a7,
560 struct arm_smccc_res *res)
562 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
565 static optee_invoke_fn *get_invoke_func(struct device *dev)
569 pr_info("probing for conduit method.\n");
571 if (device_property_read_string(dev, "method", &method)) {
572 pr_warn("missing \"method\" property\n");
573 return ERR_PTR(-ENXIO);
576 if (!strcmp("hvc", method))
577 return optee_smccc_hvc;
578 else if (!strcmp("smc", method))
579 return optee_smccc_smc;
581 pr_warn("invalid \"method\" property: %s\n", method);
582 return ERR_PTR(-EINVAL);
585 static int optee_remove(struct platform_device *pdev)
587 struct optee *optee = platform_get_drvdata(pdev);
590 * Ask OP-TEE to free all cached shared memory objects to decrease
591 * reference counters and also avoid wild pointers in secure world
592 * into the old shared memory range.
594 optee_disable_shm_cache(optee);
597 * The two devices have to be unregistered before we can free the
600 tee_device_unregister(optee->supp_teedev);
601 tee_device_unregister(optee->teedev);
603 tee_shm_pool_free(optee->pool);
604 if (optee->memremaped_shm)
605 memunmap(optee->memremaped_shm);
606 optee_wait_queue_exit(&optee->wait_queue);
607 optee_supp_uninit(&optee->supp);
608 mutex_destroy(&optee->call_queue.mutex);
615 static int optee_probe(struct platform_device *pdev)
617 optee_invoke_fn *invoke_fn;
618 struct tee_shm_pool *pool = ERR_PTR(-EINVAL);
619 struct optee *optee = NULL;
620 void *memremaped_shm = NULL;
621 struct tee_device *teedev;
625 invoke_fn = get_invoke_func(&pdev->dev);
626 if (IS_ERR(invoke_fn))
627 return PTR_ERR(invoke_fn);
629 if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
630 pr_warn("api uid mismatch\n");
634 optee_msg_get_os_revision(invoke_fn);
636 if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
637 pr_warn("api revision mismatch\n");
641 if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {
642 pr_warn("capabilities mismatch\n");
647 * Try to use dynamic shared memory if possible
649 if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
650 pool = optee_config_dyn_shm();
653 * If dynamic shared memory is not available or failed - try static one
655 if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
656 pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
659 return PTR_ERR(pool);
661 optee = kzalloc(sizeof(*optee), GFP_KERNEL);
667 optee->invoke_fn = invoke_fn;
668 optee->sec_caps = sec_caps;
670 teedev = tee_device_alloc(&optee_desc, NULL, pool, optee);
671 if (IS_ERR(teedev)) {
672 rc = PTR_ERR(teedev);
675 optee->teedev = teedev;
677 teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
678 if (IS_ERR(teedev)) {
679 rc = PTR_ERR(teedev);
682 optee->supp_teedev = teedev;
684 rc = tee_device_register(optee->teedev);
688 rc = tee_device_register(optee->supp_teedev);
692 mutex_init(&optee->call_queue.mutex);
693 INIT_LIST_HEAD(&optee->call_queue.waiters);
694 optee_wait_queue_init(&optee->wait_queue);
695 optee_supp_init(&optee->supp);
696 optee->memremaped_shm = memremaped_shm;
699 optee_enable_shm_cache(optee);
701 if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
702 pr_info("dynamic shared memory is enabled\n");
704 platform_set_drvdata(pdev, optee);
706 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
712 pr_info("initialized driver\n");
717 * tee_device_unregister() is safe to call even if the
718 * devices hasn't been registered with
719 * tee_device_register() yet.
721 tee_device_unregister(optee->supp_teedev);
722 tee_device_unregister(optee->teedev);
726 tee_shm_pool_free(pool);
728 memunmap(memremaped_shm);
732 static const struct of_device_id optee_dt_match[] = {
733 { .compatible = "linaro,optee-tz" },
736 MODULE_DEVICE_TABLE(of, optee_dt_match);
738 static struct platform_driver optee_driver = {
739 .probe = optee_probe,
740 .remove = optee_remove,
743 .of_match_table = optee_dt_match,
746 module_platform_driver(optee_driver);
748 MODULE_AUTHOR("Linaro");
749 MODULE_DESCRIPTION("OP-TEE driver");
750 MODULE_SUPPORTED_DEVICE("");
751 MODULE_VERSION("1.0");
752 MODULE_LICENSE("GPL v2");
753 MODULE_ALIAS("platform:optee");