xref: /linux/drivers/tee/optee/core.c (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/arm-smccc.h>
9 #include <linux/errno.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.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"
23 #include "shm_pool.h"
24 
25 #define DRIVER_NAME "optee"
26 
27 #define OPTEE_SHM_NUM_PRIV_PAGES	CONFIG_OPTEE_SHM_NUM_PRIV_PAGES
28 
29 /**
30  * optee_from_msg_param() - convert from OPTEE_MSG parameters to
31  *			    struct tee_param
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
36  */
37 int optee_from_msg_param(struct tee_param *params, size_t num_params,
38 			 const struct optee_msg_param *msg_params)
39 {
40 	int rc;
41 	size_t n;
42 	struct tee_shm *shm;
43 	phys_addr_t pa;
44 
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;
49 
50 		switch (attr) {
51 		case OPTEE_MSG_ATTR_TYPE_NONE:
52 			p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
53 			memset(&p->u, 0, sizeof(p->u));
54 			break;
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;
63 			break;
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)
71 				mp->u.tmem.shm_ref;
72 			if (!shm) {
73 				p->u.memref.shm_offs = 0;
74 				p->u.memref.shm = NULL;
75 				break;
76 			}
77 			rc = tee_shm_get_pa(shm, 0, &pa);
78 			if (rc)
79 				return rc;
80 			p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
81 			p->u.memref.shm = shm;
82 			break;
83 		case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
84 		case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
85 		case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
86 			p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
87 				  attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
88 			p->u.memref.size = mp->u.rmem.size;
89 			shm = (struct tee_shm *)(unsigned long)
90 				mp->u.rmem.shm_ref;
91 
92 			if (!shm) {
93 				p->u.memref.shm_offs = 0;
94 				p->u.memref.shm = NULL;
95 				break;
96 			}
97 			p->u.memref.shm_offs = mp->u.rmem.offs;
98 			p->u.memref.shm = shm;
99 
100 			break;
101 
102 		default:
103 			return -EINVAL;
104 		}
105 	}
106 	return 0;
107 }
108 
109 static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
110 				const struct tee_param *p)
111 {
112 	int rc;
113 	phys_addr_t pa;
114 
115 	mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
116 		   TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
117 
118 	mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
119 	mp->u.tmem.size = p->u.memref.size;
120 
121 	if (!p->u.memref.shm) {
122 		mp->u.tmem.buf_ptr = 0;
123 		return 0;
124 	}
125 
126 	rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
127 	if (rc)
128 		return rc;
129 
130 	mp->u.tmem.buf_ptr = pa;
131 	mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
132 		    OPTEE_MSG_ATTR_CACHE_SHIFT;
133 
134 	return 0;
135 }
136 
137 static int to_msg_param_reg_mem(struct optee_msg_param *mp,
138 				const struct tee_param *p)
139 {
140 	mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
141 		   TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
142 
143 	mp->u.rmem.shm_ref = (unsigned long)p->u.memref.shm;
144 	mp->u.rmem.size = p->u.memref.size;
145 	mp->u.rmem.offs = p->u.memref.shm_offs;
146 	return 0;
147 }
148 
149 /**
150  * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters
151  * @msg_params:	OPTEE_MSG parameters
152  * @num_params:	number of elements in the parameter arrays
153  * @params:	subsystem itnernal parameter representation
154  * Returns 0 on success or <0 on failure
155  */
156 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
157 		       const struct tee_param *params)
158 {
159 	int rc;
160 	size_t n;
161 
162 	for (n = 0; n < num_params; n++) {
163 		const struct tee_param *p = params + n;
164 		struct optee_msg_param *mp = msg_params + n;
165 
166 		switch (p->attr) {
167 		case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
168 			mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
169 			memset(&mp->u, 0, sizeof(mp->u));
170 			break;
171 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
172 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
173 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
174 			mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
175 				   TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
176 			mp->u.value.a = p->u.value.a;
177 			mp->u.value.b = p->u.value.b;
178 			mp->u.value.c = p->u.value.c;
179 			break;
180 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
181 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
182 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
183 			if (tee_shm_is_registered(p->u.memref.shm))
184 				rc = to_msg_param_reg_mem(mp, p);
185 			else
186 				rc = to_msg_param_tmp_mem(mp, p);
187 			if (rc)
188 				return rc;
189 			break;
190 		default:
191 			return -EINVAL;
192 		}
193 	}
194 	return 0;
195 }
196 
197 static void optee_get_version(struct tee_device *teedev,
198 			      struct tee_ioctl_version_data *vers)
199 {
200 	struct tee_ioctl_version_data v = {
201 		.impl_id = TEE_IMPL_ID_OPTEE,
202 		.impl_caps = TEE_OPTEE_CAP_TZ,
203 		.gen_caps = TEE_GEN_CAP_GP,
204 	};
205 	struct optee *optee = tee_get_drvdata(teedev);
206 
207 	if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
208 		v.gen_caps |= TEE_GEN_CAP_REG_MEM;
209 	if (optee->sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
210 		v.gen_caps |= TEE_GEN_CAP_MEMREF_NULL;
211 	*vers = v;
212 }
213 
214 static void optee_bus_scan(struct work_struct *work)
215 {
216 	WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
217 }
218 
219 static int optee_open(struct tee_context *ctx)
220 {
221 	struct optee_context_data *ctxdata;
222 	struct tee_device *teedev = ctx->teedev;
223 	struct optee *optee = tee_get_drvdata(teedev);
224 
225 	ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
226 	if (!ctxdata)
227 		return -ENOMEM;
228 
229 	if (teedev == optee->supp_teedev) {
230 		bool busy = true;
231 
232 		mutex_lock(&optee->supp.mutex);
233 		if (!optee->supp.ctx) {
234 			busy = false;
235 			optee->supp.ctx = ctx;
236 		}
237 		mutex_unlock(&optee->supp.mutex);
238 		if (busy) {
239 			kfree(ctxdata);
240 			return -EBUSY;
241 		}
242 
243 		if (!optee->scan_bus_done) {
244 			INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
245 			optee->scan_bus_wq = create_workqueue("optee_bus_scan");
246 			if (!optee->scan_bus_wq) {
247 				kfree(ctxdata);
248 				return -ECHILD;
249 			}
250 			queue_work(optee->scan_bus_wq, &optee->scan_bus_work);
251 			optee->scan_bus_done = true;
252 		}
253 	}
254 	mutex_init(&ctxdata->mutex);
255 	INIT_LIST_HEAD(&ctxdata->sess_list);
256 
257 	if (optee->sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
258 		ctx->cap_memref_null  = true;
259 	else
260 		ctx->cap_memref_null = false;
261 
262 	ctx->data = ctxdata;
263 	return 0;
264 }
265 
266 static void optee_release(struct tee_context *ctx)
267 {
268 	struct optee_context_data *ctxdata = ctx->data;
269 	struct tee_device *teedev = ctx->teedev;
270 	struct optee *optee = tee_get_drvdata(teedev);
271 	struct tee_shm *shm;
272 	struct optee_msg_arg *arg = NULL;
273 	phys_addr_t parg;
274 	struct optee_session *sess;
275 	struct optee_session *sess_tmp;
276 
277 	if (!ctxdata)
278 		return;
279 
280 	shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED);
281 	if (!IS_ERR(shm)) {
282 		arg = tee_shm_get_va(shm, 0);
283 		/*
284 		 * If va2pa fails for some reason, we can't call into
285 		 * secure world, only free the memory. Secure OS will leak
286 		 * sessions and finally refuse more sessions, but we will
287 		 * at least let normal world reclaim its memory.
288 		 */
289 		if (!IS_ERR(arg))
290 			if (tee_shm_va2pa(shm, arg, &parg))
291 				arg = NULL; /* prevent usage of parg below */
292 	}
293 
294 	list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
295 				 list_node) {
296 		list_del(&sess->list_node);
297 		if (!IS_ERR_OR_NULL(arg)) {
298 			memset(arg, 0, sizeof(*arg));
299 			arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
300 			arg->session = sess->session_id;
301 			optee_do_call_with_arg(ctx, parg);
302 		}
303 		kfree(sess);
304 	}
305 	kfree(ctxdata);
306 
307 	if (!IS_ERR(shm))
308 		tee_shm_free(shm);
309 
310 	ctx->data = NULL;
311 
312 	if (teedev == optee->supp_teedev) {
313 		if (optee->scan_bus_wq) {
314 			destroy_workqueue(optee->scan_bus_wq);
315 			optee->scan_bus_wq = NULL;
316 		}
317 		optee_supp_release(&optee->supp);
318 	}
319 }
320 
321 static const struct tee_driver_ops optee_ops = {
322 	.get_version = optee_get_version,
323 	.open = optee_open,
324 	.release = optee_release,
325 	.open_session = optee_open_session,
326 	.close_session = optee_close_session,
327 	.invoke_func = optee_invoke_func,
328 	.cancel_req = optee_cancel_req,
329 	.shm_register = optee_shm_register,
330 	.shm_unregister = optee_shm_unregister,
331 };
332 
333 static const struct tee_desc optee_desc = {
334 	.name = DRIVER_NAME "-clnt",
335 	.ops = &optee_ops,
336 	.owner = THIS_MODULE,
337 };
338 
339 static const struct tee_driver_ops optee_supp_ops = {
340 	.get_version = optee_get_version,
341 	.open = optee_open,
342 	.release = optee_release,
343 	.supp_recv = optee_supp_recv,
344 	.supp_send = optee_supp_send,
345 	.shm_register = optee_shm_register_supp,
346 	.shm_unregister = optee_shm_unregister_supp,
347 };
348 
349 static const struct tee_desc optee_supp_desc = {
350 	.name = DRIVER_NAME "-supp",
351 	.ops = &optee_supp_ops,
352 	.owner = THIS_MODULE,
353 	.flags = TEE_DESC_PRIVILEGED,
354 };
355 
356 static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
357 {
358 	struct arm_smccc_res res;
359 
360 	invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
361 
362 	if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
363 	    res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3)
364 		return true;
365 	return false;
366 }
367 
368 static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn)
369 {
370 	union {
371 		struct arm_smccc_res smccc;
372 		struct optee_smc_call_get_os_revision_result result;
373 	} res = {
374 		.result = {
375 			.build_id = 0
376 		}
377 	};
378 
379 	invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
380 		  &res.smccc);
381 
382 	if (res.result.build_id)
383 		pr_info("revision %lu.%lu (%08lx)", res.result.major,
384 			res.result.minor, res.result.build_id);
385 	else
386 		pr_info("revision %lu.%lu", res.result.major, res.result.minor);
387 }
388 
389 static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
390 {
391 	union {
392 		struct arm_smccc_res smccc;
393 		struct optee_smc_calls_revision_result result;
394 	} res;
395 
396 	invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
397 
398 	if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
399 	    (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
400 		return true;
401 	return false;
402 }
403 
404 static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
405 					    u32 *sec_caps)
406 {
407 	union {
408 		struct arm_smccc_res smccc;
409 		struct optee_smc_exchange_capabilities_result result;
410 	} res;
411 	u32 a1 = 0;
412 
413 	/*
414 	 * TODO This isn't enough to tell if it's UP system (from kernel
415 	 * point of view) or not, is_smp() returns the the information
416 	 * needed, but can't be called directly from here.
417 	 */
418 	if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
419 		a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
420 
421 	invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
422 		  &res.smccc);
423 
424 	if (res.result.status != OPTEE_SMC_RETURN_OK)
425 		return false;
426 
427 	*sec_caps = res.result.capabilities;
428 	return true;
429 }
430 
431 static struct tee_shm_pool *optee_config_dyn_shm(void)
432 {
433 	struct tee_shm_pool_mgr *priv_mgr;
434 	struct tee_shm_pool_mgr *dmabuf_mgr;
435 	void *rc;
436 
437 	rc = optee_shm_pool_alloc_pages();
438 	if (IS_ERR(rc))
439 		return rc;
440 	priv_mgr = rc;
441 
442 	rc = optee_shm_pool_alloc_pages();
443 	if (IS_ERR(rc)) {
444 		tee_shm_pool_mgr_destroy(priv_mgr);
445 		return rc;
446 	}
447 	dmabuf_mgr = rc;
448 
449 	rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
450 	if (IS_ERR(rc)) {
451 		tee_shm_pool_mgr_destroy(priv_mgr);
452 		tee_shm_pool_mgr_destroy(dmabuf_mgr);
453 	}
454 
455 	return rc;
456 }
457 
458 static struct tee_shm_pool *
459 optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
460 {
461 	union {
462 		struct arm_smccc_res smccc;
463 		struct optee_smc_get_shm_config_result result;
464 	} res;
465 	unsigned long vaddr;
466 	phys_addr_t paddr;
467 	size_t size;
468 	phys_addr_t begin;
469 	phys_addr_t end;
470 	void *va;
471 	struct tee_shm_pool_mgr *priv_mgr;
472 	struct tee_shm_pool_mgr *dmabuf_mgr;
473 	void *rc;
474 	const int sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
475 
476 	invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
477 	if (res.result.status != OPTEE_SMC_RETURN_OK) {
478 		pr_err("static shm service not available\n");
479 		return ERR_PTR(-ENOENT);
480 	}
481 
482 	if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
483 		pr_err("only normal cached shared memory supported\n");
484 		return ERR_PTR(-EINVAL);
485 	}
486 
487 	begin = roundup(res.result.start, PAGE_SIZE);
488 	end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
489 	paddr = begin;
490 	size = end - begin;
491 
492 	if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) {
493 		pr_err("too small shared memory area\n");
494 		return ERR_PTR(-EINVAL);
495 	}
496 
497 	va = memremap(paddr, size, MEMREMAP_WB);
498 	if (!va) {
499 		pr_err("shared memory ioremap failed\n");
500 		return ERR_PTR(-EINVAL);
501 	}
502 	vaddr = (unsigned long)va;
503 
504 	rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
505 					    3 /* 8 bytes aligned */);
506 	if (IS_ERR(rc))
507 		goto err_memunmap;
508 	priv_mgr = rc;
509 
510 	vaddr += sz;
511 	paddr += sz;
512 	size -= sz;
513 
514 	rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
515 	if (IS_ERR(rc))
516 		goto err_free_priv_mgr;
517 	dmabuf_mgr = rc;
518 
519 	rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
520 	if (IS_ERR(rc))
521 		goto err_free_dmabuf_mgr;
522 
523 	*memremaped_shm = va;
524 
525 	return rc;
526 
527 err_free_dmabuf_mgr:
528 	tee_shm_pool_mgr_destroy(dmabuf_mgr);
529 err_free_priv_mgr:
530 	tee_shm_pool_mgr_destroy(priv_mgr);
531 err_memunmap:
532 	memunmap(va);
533 	return rc;
534 }
535 
536 /* Simple wrapper functions to be able to use a function pointer */
537 static void optee_smccc_smc(unsigned long a0, unsigned long a1,
538 			    unsigned long a2, unsigned long a3,
539 			    unsigned long a4, unsigned long a5,
540 			    unsigned long a6, unsigned long a7,
541 			    struct arm_smccc_res *res)
542 {
543 	arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
544 }
545 
546 static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
547 			    unsigned long a2, unsigned long a3,
548 			    unsigned long a4, unsigned long a5,
549 			    unsigned long a6, unsigned long a7,
550 			    struct arm_smccc_res *res)
551 {
552 	arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
553 }
554 
555 static optee_invoke_fn *get_invoke_func(struct device *dev)
556 {
557 	const char *method;
558 
559 	pr_info("probing for conduit method.\n");
560 
561 	if (device_property_read_string(dev, "method", &method)) {
562 		pr_warn("missing \"method\" property\n");
563 		return ERR_PTR(-ENXIO);
564 	}
565 
566 	if (!strcmp("hvc", method))
567 		return optee_smccc_hvc;
568 	else if (!strcmp("smc", method))
569 		return optee_smccc_smc;
570 
571 	pr_warn("invalid \"method\" property: %s\n", method);
572 	return ERR_PTR(-EINVAL);
573 }
574 
575 static int optee_remove(struct platform_device *pdev)
576 {
577 	struct optee *optee = platform_get_drvdata(pdev);
578 
579 	/*
580 	 * Ask OP-TEE to free all cached shared memory objects to decrease
581 	 * reference counters and also avoid wild pointers in secure world
582 	 * into the old shared memory range.
583 	 */
584 	optee_disable_shm_cache(optee);
585 
586 	/*
587 	 * The two devices have to be unregistered before we can free the
588 	 * other resources.
589 	 */
590 	tee_device_unregister(optee->supp_teedev);
591 	tee_device_unregister(optee->teedev);
592 
593 	tee_shm_pool_free(optee->pool);
594 	if (optee->memremaped_shm)
595 		memunmap(optee->memremaped_shm);
596 	optee_wait_queue_exit(&optee->wait_queue);
597 	optee_supp_uninit(&optee->supp);
598 	mutex_destroy(&optee->call_queue.mutex);
599 
600 	kfree(optee);
601 
602 	return 0;
603 }
604 
605 static int optee_probe(struct platform_device *pdev)
606 {
607 	optee_invoke_fn *invoke_fn;
608 	struct tee_shm_pool *pool = ERR_PTR(-EINVAL);
609 	struct optee *optee = NULL;
610 	void *memremaped_shm = NULL;
611 	struct tee_device *teedev;
612 	u32 sec_caps;
613 	int rc;
614 
615 	invoke_fn = get_invoke_func(&pdev->dev);
616 	if (IS_ERR(invoke_fn))
617 		return PTR_ERR(invoke_fn);
618 
619 	if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
620 		pr_warn("api uid mismatch\n");
621 		return -EINVAL;
622 	}
623 
624 	optee_msg_get_os_revision(invoke_fn);
625 
626 	if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
627 		pr_warn("api revision mismatch\n");
628 		return -EINVAL;
629 	}
630 
631 	if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {
632 		pr_warn("capabilities mismatch\n");
633 		return -EINVAL;
634 	}
635 
636 	/*
637 	 * Try to use dynamic shared memory if possible
638 	 */
639 	if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
640 		pool = optee_config_dyn_shm();
641 
642 	/*
643 	 * If dynamic shared memory is not available or failed - try static one
644 	 */
645 	if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
646 		pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
647 
648 	if (IS_ERR(pool))
649 		return PTR_ERR(pool);
650 
651 	optee = kzalloc(sizeof(*optee), GFP_KERNEL);
652 	if (!optee) {
653 		rc = -ENOMEM;
654 		goto err;
655 	}
656 
657 	optee->invoke_fn = invoke_fn;
658 	optee->sec_caps = sec_caps;
659 
660 	teedev = tee_device_alloc(&optee_desc, NULL, pool, optee);
661 	if (IS_ERR(teedev)) {
662 		rc = PTR_ERR(teedev);
663 		goto err;
664 	}
665 	optee->teedev = teedev;
666 
667 	teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
668 	if (IS_ERR(teedev)) {
669 		rc = PTR_ERR(teedev);
670 		goto err;
671 	}
672 	optee->supp_teedev = teedev;
673 
674 	rc = tee_device_register(optee->teedev);
675 	if (rc)
676 		goto err;
677 
678 	rc = tee_device_register(optee->supp_teedev);
679 	if (rc)
680 		goto err;
681 
682 	mutex_init(&optee->call_queue.mutex);
683 	INIT_LIST_HEAD(&optee->call_queue.waiters);
684 	optee_wait_queue_init(&optee->wait_queue);
685 	optee_supp_init(&optee->supp);
686 	optee->memremaped_shm = memremaped_shm;
687 	optee->pool = pool;
688 
689 	optee_enable_shm_cache(optee);
690 
691 	if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
692 		pr_info("dynamic shared memory is enabled\n");
693 
694 	platform_set_drvdata(pdev, optee);
695 
696 	rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
697 	if (rc) {
698 		optee_remove(pdev);
699 		return rc;
700 	}
701 
702 	pr_info("initialized driver\n");
703 	return 0;
704 err:
705 	if (optee) {
706 		/*
707 		 * tee_device_unregister() is safe to call even if the
708 		 * devices hasn't been registered with
709 		 * tee_device_register() yet.
710 		 */
711 		tee_device_unregister(optee->supp_teedev);
712 		tee_device_unregister(optee->teedev);
713 		kfree(optee);
714 	}
715 	if (pool)
716 		tee_shm_pool_free(pool);
717 	if (memremaped_shm)
718 		memunmap(memremaped_shm);
719 	return rc;
720 }
721 
722 static const struct of_device_id optee_dt_match[] = {
723 	{ .compatible = "linaro,optee-tz" },
724 	{},
725 };
726 MODULE_DEVICE_TABLE(of, optee_dt_match);
727 
728 static struct platform_driver optee_driver = {
729 	.probe  = optee_probe,
730 	.remove = optee_remove,
731 	.driver = {
732 		.name = "optee",
733 		.of_match_table = optee_dt_match,
734 	},
735 };
736 module_platform_driver(optee_driver);
737 
738 MODULE_AUTHOR("Linaro");
739 MODULE_DESCRIPTION("OP-TEE driver");
740 MODULE_VERSION("1.0");
741 MODULE_LICENSE("GPL v2");
742 MODULE_ALIAS("platform:optee");
743