GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / tee / optee / core.c
1 /*
2  * Copyright (c) 2015, Linaro Limited
3  *
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.
7  *
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.
12  *
13  */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/arm-smccc.h>
18 #include <linux/errno.h>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/of.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"
31 #include "shm_pool.h"
32
33 #define DRIVER_NAME "optee"
34
35 #define OPTEE_SHM_NUM_PRIV_PAGES        CONFIG_OPTEE_SHM_NUM_PRIV_PAGES
36
37 /**
38  * optee_from_msg_param() - convert from OPTEE_MSG parameters to
39  *                          struct tee_param
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
44  */
45 int optee_from_msg_param(struct tee_param *params, size_t num_params,
46                          const struct optee_msg_param *msg_params)
47 {
48         int rc;
49         size_t n;
50         struct tee_shm *shm;
51         phys_addr_t pa;
52
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;
57
58                 switch (attr) {
59                 case OPTEE_MSG_ATTR_TYPE_NONE:
60                         p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
61                         memset(&p->u, 0, sizeof(p->u));
62                         break;
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;
71                         break;
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)
79                                 mp->u.tmem.shm_ref;
80                         if (!shm) {
81                                 p->u.memref.shm_offs = 0;
82                                 p->u.memref.shm = NULL;
83                                 break;
84                         }
85                         rc = tee_shm_get_pa(shm, 0, &pa);
86                         if (rc)
87                                 return rc;
88                         p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
89                         p->u.memref.shm = shm;
90                         break;
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)
98                                 mp->u.rmem.shm_ref;
99
100                         if (!shm) {
101                                 p->u.memref.shm_offs = 0;
102                                 p->u.memref.shm = NULL;
103                                 break;
104                         }
105                         p->u.memref.shm_offs = mp->u.rmem.offs;
106                         p->u.memref.shm = shm;
107
108                         break;
109
110                 default:
111                         return -EINVAL;
112                 }
113         }
114         return 0;
115 }
116
117 static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
118                                 const struct tee_param *p)
119 {
120         int rc;
121         phys_addr_t pa;
122
123         mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
124                    TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
125
126         mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
127         mp->u.tmem.size = p->u.memref.size;
128
129         if (!p->u.memref.shm) {
130                 mp->u.tmem.buf_ptr = 0;
131                 return 0;
132         }
133
134         rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
135         if (rc)
136                 return rc;
137
138         mp->u.tmem.buf_ptr = pa;
139         mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
140                     OPTEE_MSG_ATTR_CACHE_SHIFT;
141
142         return 0;
143 }
144
145 static int to_msg_param_reg_mem(struct optee_msg_param *mp,
146                                 const struct tee_param *p)
147 {
148         mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
149                    TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
150
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;
154         return 0;
155 }
156
157 /**
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
163  */
164 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
165                        const struct tee_param *params)
166 {
167         int rc;
168         size_t n;
169
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;
173
174                 switch (p->attr) {
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));
178                         break;
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;
187                         break;
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);
193                         else
194                                 rc = to_msg_param_tmp_mem(mp, p);
195                         if (rc)
196                                 return rc;
197                         break;
198                 default:
199                         return -EINVAL;
200                 }
201         }
202         return 0;
203 }
204
205 static void optee_get_version(struct tee_device *teedev,
206                               struct tee_ioctl_version_data *vers)
207 {
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,
212         };
213         struct optee *optee = tee_get_drvdata(teedev);
214
215         if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
216                 v.gen_caps |= TEE_GEN_CAP_REG_MEM;
217         *vers = v;
218 }
219
220 static int optee_open(struct tee_context *ctx)
221 {
222         struct optee_context_data *ctxdata;
223         struct tee_device *teedev = ctx->teedev;
224         struct optee *optee = tee_get_drvdata(teedev);
225
226         ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
227         if (!ctxdata)
228                 return -ENOMEM;
229
230         if (teedev == optee->supp_teedev) {
231                 bool busy = true;
232
233                 mutex_lock(&optee->supp.mutex);
234                 if (!optee->supp.ctx) {
235                         busy = false;
236                         optee->supp.ctx = ctx;
237                 }
238                 mutex_unlock(&optee->supp.mutex);
239                 if (busy) {
240                         kfree(ctxdata);
241                         return -EBUSY;
242                 }
243         }
244
245         mutex_init(&ctxdata->mutex);
246         INIT_LIST_HEAD(&ctxdata->sess_list);
247
248         ctx->data = ctxdata;
249         return 0;
250 }
251
252 static void optee_release(struct tee_context *ctx)
253 {
254         struct optee_context_data *ctxdata = ctx->data;
255         struct tee_device *teedev = ctx->teedev;
256         struct optee *optee = tee_get_drvdata(teedev);
257         struct tee_shm *shm;
258         struct optee_msg_arg *arg = NULL;
259         phys_addr_t parg;
260         struct optee_session *sess;
261         struct optee_session *sess_tmp;
262
263         if (!ctxdata)
264                 return;
265
266         shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED);
267         if (!IS_ERR(shm)) {
268                 arg = tee_shm_get_va(shm, 0);
269                 /*
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.
274                  */
275                 if (!IS_ERR(arg))
276                         if (tee_shm_va2pa(shm, arg, &parg))
277                                 arg = NULL; /* prevent usage of parg below */
278         }
279
280         list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
281                                  list_node) {
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);
288                 }
289                 kfree(sess);
290         }
291         kfree(ctxdata);
292
293         if (!IS_ERR(shm))
294                 tee_shm_free(shm);
295
296         ctx->data = NULL;
297
298         if (teedev == optee->supp_teedev)
299                 optee_supp_release(&optee->supp);
300 }
301
302 static const struct tee_driver_ops optee_ops = {
303         .get_version = optee_get_version,
304         .open = optee_open,
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,
312 };
313
314 static const struct tee_desc optee_desc = {
315         .name = DRIVER_NAME "-clnt",
316         .ops = &optee_ops,
317         .owner = THIS_MODULE,
318 };
319
320 static const struct tee_driver_ops optee_supp_ops = {
321         .get_version = optee_get_version,
322         .open = optee_open,
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,
328 };
329
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,
335 };
336
337 static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
338 {
339         struct arm_smccc_res res;
340
341         invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
342
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)
345                 return true;
346         return false;
347 }
348
349 static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn)
350 {
351         union {
352                 struct arm_smccc_res smccc;
353                 struct optee_smc_call_get_os_revision_result result;
354         } res = {
355                 .result = {
356                         .build_id = 0
357                 }
358         };
359
360         invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
361                   &res.smccc);
362
363         if (res.result.build_id)
364                 pr_info("revision %lu.%lu (%08lx)", res.result.major,
365                         res.result.minor, res.result.build_id);
366         else
367                 pr_info("revision %lu.%lu", res.result.major, res.result.minor);
368 }
369
370 static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
371 {
372         union {
373                 struct arm_smccc_res smccc;
374                 struct optee_smc_calls_revision_result result;
375         } res;
376
377         invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
378
379         if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
380             (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
381                 return true;
382         return false;
383 }
384
385 static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
386                                             u32 *sec_caps)
387 {
388         union {
389                 struct arm_smccc_res smccc;
390                 struct optee_smc_exchange_capabilities_result result;
391         } res;
392         u32 a1 = 0;
393
394         /*
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.
398          */
399         if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
400                 a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
401
402         invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
403                   &res.smccc);
404
405         if (res.result.status != OPTEE_SMC_RETURN_OK)
406                 return false;
407
408         *sec_caps = res.result.capabilities;
409         return true;
410 }
411
412 static struct tee_shm_pool *
413 optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
414                           u32 sec_caps)
415 {
416         union {
417                 struct arm_smccc_res smccc;
418                 struct optee_smc_get_shm_config_result result;
419         } res;
420         unsigned long vaddr;
421         phys_addr_t paddr;
422         size_t size;
423         phys_addr_t begin;
424         phys_addr_t end;
425         void *va;
426         struct tee_shm_pool_mgr *priv_mgr;
427         struct tee_shm_pool_mgr *dmabuf_mgr;
428         void *rc;
429
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);
434         }
435
436         if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
437                 pr_err("only normal cached shared memory supported\n");
438                 return ERR_PTR(-EINVAL);
439         }
440
441         begin = roundup(res.result.start, PAGE_SIZE);
442         end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
443         paddr = begin;
444         size = end - begin;
445
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);
449         }
450
451         va = memremap(paddr, size, MEMREMAP_WB);
452         if (!va) {
453                 pr_err("shared memory ioremap failed\n");
454                 return ERR_PTR(-EINVAL);
455         }
456         vaddr = (unsigned long)va;
457
458         /*
459          * If OP-TEE can work with unregistered SHM, we will use own pool
460          * for private shm
461          */
462         if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
463                 rc = optee_shm_pool_alloc_pages();
464                 if (IS_ERR(rc))
465                         goto err_memunmap;
466                 priv_mgr = rc;
467         } else {
468                 const size_t sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
469
470                 rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
471                                                     3 /* 8 bytes aligned */);
472                 if (IS_ERR(rc))
473                         goto err_memunmap;
474                 priv_mgr = rc;
475
476                 vaddr += sz;
477                 paddr += sz;
478                 size -= sz;
479         }
480
481         rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
482         if (IS_ERR(rc))
483                 goto err_free_priv_mgr;
484         dmabuf_mgr = rc;
485
486         rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
487         if (IS_ERR(rc))
488                 goto err_free_dmabuf_mgr;
489
490         *memremaped_shm = va;
491
492         return rc;
493
494 err_free_dmabuf_mgr:
495         tee_shm_pool_mgr_destroy(dmabuf_mgr);
496 err_free_priv_mgr:
497         tee_shm_pool_mgr_destroy(priv_mgr);
498 err_memunmap:
499         memunmap(va);
500         return rc;
501 }
502
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)
509 {
510         arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
511 }
512
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)
518 {
519         arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
520 }
521
522 static optee_invoke_fn *get_invoke_func(struct device_node *np)
523 {
524         const char *method;
525
526         pr_info("probing for conduit method from DT.\n");
527
528         if (of_property_read_string(np, "method", &method)) {
529                 pr_warn("missing \"method\" property\n");
530                 return ERR_PTR(-ENXIO);
531         }
532
533         if (!strcmp("hvc", method))
534                 return optee_smccc_hvc;
535         else if (!strcmp("smc", method))
536                 return optee_smccc_smc;
537
538         pr_warn("invalid \"method\" property: %s\n", method);
539         return ERR_PTR(-EINVAL);
540 }
541
542 static struct optee *optee_probe(struct device_node *np)
543 {
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;
549         u32 sec_caps;
550         int rc;
551
552         invoke_fn = get_invoke_func(np);
553         if (IS_ERR(invoke_fn))
554                 return (void *)invoke_fn;
555
556         if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
557                 pr_warn("api uid mismatch\n");
558                 return ERR_PTR(-EINVAL);
559         }
560
561         optee_msg_get_os_revision(invoke_fn);
562
563         if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
564                 pr_warn("api revision mismatch\n");
565                 return ERR_PTR(-EINVAL);
566         }
567
568         if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {
569                 pr_warn("capabilities mismatch\n");
570                 return ERR_PTR(-EINVAL);
571         }
572
573         /*
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.
576          */
577         if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
578                 return ERR_PTR(-EINVAL);
579
580         pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm, sec_caps);
581         if (IS_ERR(pool))
582                 return (void *)pool;
583
584         optee = kzalloc(sizeof(*optee), GFP_KERNEL);
585         if (!optee) {
586                 rc = -ENOMEM;
587                 goto err;
588         }
589
590         optee->invoke_fn = invoke_fn;
591         optee->sec_caps = sec_caps;
592
593         teedev = tee_device_alloc(&optee_desc, NULL, pool, optee);
594         if (IS_ERR(teedev)) {
595                 rc = PTR_ERR(teedev);
596                 goto err;
597         }
598         optee->teedev = teedev;
599
600         teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
601         if (IS_ERR(teedev)) {
602                 rc = PTR_ERR(teedev);
603                 goto err;
604         }
605         optee->supp_teedev = teedev;
606
607         rc = tee_device_register(optee->teedev);
608         if (rc)
609                 goto err;
610
611         rc = tee_device_register(optee->supp_teedev);
612         if (rc)
613                 goto err;
614
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;
620         optee->pool = pool;
621
622         /*
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
627          * shm cache.
628          */
629         optee_disable_unmapped_shm_cache(optee);
630
631         optee_enable_shm_cache(optee);
632
633         pr_info("initialized driver\n");
634         return optee;
635 err:
636         if (optee) {
637                 /*
638                  * tee_device_unregister() is safe to call even if the
639                  * devices hasn't been registered with
640                  * tee_device_register() yet.
641                  */
642                 tee_device_unregister(optee->supp_teedev);
643                 tee_device_unregister(optee->teedev);
644                 kfree(optee);
645         }
646         if (pool)
647                 tee_shm_pool_free(pool);
648         if (memremaped_shm)
649                 memunmap(memremaped_shm);
650         return ERR_PTR(rc);
651 }
652
653 static void optee_remove(struct optee *optee)
654 {
655         /*
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.
659          */
660         optee_disable_shm_cache(optee);
661
662         /*
663          * The two devices has to be unregistered before we can free the
664          * other resources.
665          */
666         tee_device_unregister(optee->supp_teedev);
667         tee_device_unregister(optee->teedev);
668
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);
675
676         kfree(optee);
677 }
678
679 static const struct of_device_id optee_match[] = {
680         { .compatible = "linaro,optee-tz" },
681         {},
682 };
683
684 static struct optee *optee_svc;
685
686 static int __init optee_driver_init(void)
687 {
688         struct device_node *fw_np;
689         struct device_node *np;
690         struct optee *optee;
691
692         /* Node is supposed to be below /firmware */
693         fw_np = of_find_node_by_name(NULL, "firmware");
694         if (!fw_np)
695                 return -ENODEV;
696
697         np = of_find_matching_node(fw_np, optee_match);
698         if (!np || !of_device_is_available(np)) {
699                 of_node_put(np);
700                 return -ENODEV;
701         }
702
703         optee = optee_probe(np);
704         of_node_put(np);
705
706         if (IS_ERR(optee))
707                 return PTR_ERR(optee);
708
709         optee_svc = optee;
710
711         return 0;
712 }
713 module_init(optee_driver_init);
714
715 static void __exit optee_driver_exit(void)
716 {
717         struct optee *optee = optee_svc;
718
719         optee_svc = NULL;
720         if (optee)
721                 optee_remove(optee);
722 }
723 module_exit(optee_driver_exit);
724
725 MODULE_AUTHOR("Linaro");
726 MODULE_DESCRIPTION("OP-TEE driver");
727 MODULE_SUPPORTED_DEVICE("");
728 MODULE_VERSION("1.0");
729 MODULE_LICENSE("GPL v2");