GNU Linux-libre 4.19.268-gnu1
[releases.git] / drivers / tee / tee_core.c
1 /*
2  * Copyright (c) 2015-2016, 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) "%s: " fmt, __func__
16
17 #include <linux/cdev.h>
18 #include <linux/device.h>
19 #include <linux/fs.h>
20 #include <linux/idr.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/tee_drv.h>
24 #include <linux/uaccess.h>
25 #include "tee_private.h"
26
27 #define TEE_NUM_DEVICES 32
28
29 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
30
31 /*
32  * Unprivileged devices in the lower half range and privileged devices in
33  * the upper half range.
34  */
35 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
36 static DEFINE_SPINLOCK(driver_lock);
37
38 static struct class *tee_class;
39 static dev_t tee_devt;
40
41 static int tee_open(struct inode *inode, struct file *filp)
42 {
43         int rc;
44         struct tee_device *teedev;
45         struct tee_context *ctx;
46
47         teedev = container_of(inode->i_cdev, struct tee_device, cdev);
48         if (!tee_device_get(teedev))
49                 return -EINVAL;
50
51         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
52         if (!ctx) {
53                 rc = -ENOMEM;
54                 goto err;
55         }
56
57         kref_init(&ctx->refcount);
58         ctx->teedev = teedev;
59         INIT_LIST_HEAD(&ctx->list_shm);
60         filp->private_data = ctx;
61         rc = teedev->desc->ops->open(ctx);
62         if (rc)
63                 goto err;
64
65         return 0;
66 err:
67         kfree(ctx);
68         tee_device_put(teedev);
69         return rc;
70 }
71
72 void teedev_ctx_get(struct tee_context *ctx)
73 {
74         if (ctx->releasing)
75                 return;
76
77         kref_get(&ctx->refcount);
78 }
79
80 static void teedev_ctx_release(struct kref *ref)
81 {
82         struct tee_context *ctx = container_of(ref, struct tee_context,
83                                                refcount);
84         ctx->releasing = true;
85         ctx->teedev->desc->ops->release(ctx);
86         kfree(ctx);
87 }
88
89 void teedev_ctx_put(struct tee_context *ctx)
90 {
91         if (ctx->releasing)
92                 return;
93
94         kref_put(&ctx->refcount, teedev_ctx_release);
95 }
96
97 static void teedev_close_context(struct tee_context *ctx)
98 {
99         struct tee_device *teedev = ctx->teedev;
100
101         teedev_ctx_put(ctx);
102         tee_device_put(teedev);
103 }
104
105 static int tee_release(struct inode *inode, struct file *filp)
106 {
107         teedev_close_context(filp->private_data);
108         return 0;
109 }
110
111 static int tee_ioctl_version(struct tee_context *ctx,
112                              struct tee_ioctl_version_data __user *uvers)
113 {
114         struct tee_ioctl_version_data vers;
115
116         ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
117
118         if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
119                 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
120
121         if (copy_to_user(uvers, &vers, sizeof(vers)))
122                 return -EFAULT;
123
124         return 0;
125 }
126
127 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
128                                struct tee_ioctl_shm_alloc_data __user *udata)
129 {
130         long ret;
131         struct tee_ioctl_shm_alloc_data data;
132         struct tee_shm *shm;
133
134         if (copy_from_user(&data, udata, sizeof(data)))
135                 return -EFAULT;
136
137         /* Currently no input flags are supported */
138         if (data.flags)
139                 return -EINVAL;
140
141         shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
142         if (IS_ERR(shm))
143                 return PTR_ERR(shm);
144
145         data.id = shm->id;
146         data.flags = shm->flags;
147         data.size = shm->size;
148
149         if (copy_to_user(udata, &data, sizeof(data)))
150                 ret = -EFAULT;
151         else
152                 ret = tee_shm_get_fd(shm);
153
154         /*
155          * When user space closes the file descriptor the shared memory
156          * should be freed or if tee_shm_get_fd() failed then it will
157          * be freed immediately.
158          */
159         tee_shm_put(shm);
160         return ret;
161 }
162
163 static int
164 tee_ioctl_shm_register(struct tee_context *ctx,
165                        struct tee_ioctl_shm_register_data __user *udata)
166 {
167         long ret;
168         struct tee_ioctl_shm_register_data data;
169         struct tee_shm *shm;
170
171         if (copy_from_user(&data, udata, sizeof(data)))
172                 return -EFAULT;
173
174         /* Currently no input flags are supported */
175         if (data.flags)
176                 return -EINVAL;
177
178         if (!access_ok(VERIFY_WRITE, (void __user *)(unsigned long)data.addr,
179                        data.length))
180                 return -EFAULT;
181
182         shm = tee_shm_register(ctx, data.addr, data.length,
183                                TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
184         if (IS_ERR(shm))
185                 return PTR_ERR(shm);
186
187         data.id = shm->id;
188         data.flags = shm->flags;
189         data.length = shm->size;
190
191         if (copy_to_user(udata, &data, sizeof(data)))
192                 ret = -EFAULT;
193         else
194                 ret = tee_shm_get_fd(shm);
195         /*
196          * When user space closes the file descriptor the shared memory
197          * should be freed or if tee_shm_get_fd() failed then it will
198          * be freed immediately.
199          */
200         tee_shm_put(shm);
201         return ret;
202 }
203
204 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
205                             size_t num_params,
206                             struct tee_ioctl_param __user *uparams)
207 {
208         size_t n;
209
210         for (n = 0; n < num_params; n++) {
211                 struct tee_shm *shm;
212                 struct tee_ioctl_param ip;
213
214                 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
215                         return -EFAULT;
216
217                 /* All unused attribute bits has to be zero */
218                 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
219                         return -EINVAL;
220
221                 params[n].attr = ip.attr;
222                 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
223                 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
224                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
225                         break;
226                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
227                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
228                         params[n].u.value.a = ip.a;
229                         params[n].u.value.b = ip.b;
230                         params[n].u.value.c = ip.c;
231                         break;
232                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
233                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
234                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
235                         /*
236                          * If we fail to get a pointer to a shared memory
237                          * object (and increase the ref count) from an
238                          * identifier we return an error. All pointers that
239                          * has been added in params have an increased ref
240                          * count. It's the callers responibility to do
241                          * tee_shm_put() on all resolved pointers.
242                          */
243                         shm = tee_shm_get_from_id(ctx, ip.c);
244                         if (IS_ERR(shm))
245                                 return PTR_ERR(shm);
246
247                         /*
248                          * Ensure offset + size does not overflow offset
249                          * and does not overflow the size of the referred
250                          * shared memory object.
251                          */
252                         if ((ip.a + ip.b) < ip.a ||
253                             (ip.a + ip.b) > shm->size) {
254                                 tee_shm_put(shm);
255                                 return -EINVAL;
256                         }
257
258                         params[n].u.memref.shm_offs = ip.a;
259                         params[n].u.memref.size = ip.b;
260                         params[n].u.memref.shm = shm;
261                         break;
262                 default:
263                         /* Unknown attribute */
264                         return -EINVAL;
265                 }
266         }
267         return 0;
268 }
269
270 static int params_to_user(struct tee_ioctl_param __user *uparams,
271                           size_t num_params, struct tee_param *params)
272 {
273         size_t n;
274
275         for (n = 0; n < num_params; n++) {
276                 struct tee_ioctl_param __user *up = uparams + n;
277                 struct tee_param *p = params + n;
278
279                 switch (p->attr) {
280                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
281                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
282                         if (put_user(p->u.value.a, &up->a) ||
283                             put_user(p->u.value.b, &up->b) ||
284                             put_user(p->u.value.c, &up->c))
285                                 return -EFAULT;
286                         break;
287                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
288                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
289                         if (put_user((u64)p->u.memref.size, &up->b))
290                                 return -EFAULT;
291                 default:
292                         break;
293                 }
294         }
295         return 0;
296 }
297
298 static int tee_ioctl_open_session(struct tee_context *ctx,
299                                   struct tee_ioctl_buf_data __user *ubuf)
300 {
301         int rc;
302         size_t n;
303         struct tee_ioctl_buf_data buf;
304         struct tee_ioctl_open_session_arg __user *uarg;
305         struct tee_ioctl_open_session_arg arg;
306         struct tee_ioctl_param __user *uparams = NULL;
307         struct tee_param *params = NULL;
308         bool have_session = false;
309
310         if (!ctx->teedev->desc->ops->open_session)
311                 return -EINVAL;
312
313         if (copy_from_user(&buf, ubuf, sizeof(buf)))
314                 return -EFAULT;
315
316         if (buf.buf_len > TEE_MAX_ARG_SIZE ||
317             buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
318                 return -EINVAL;
319
320         uarg = u64_to_user_ptr(buf.buf_ptr);
321         if (copy_from_user(&arg, uarg, sizeof(arg)))
322                 return -EFAULT;
323
324         if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
325                 return -EINVAL;
326
327         if (arg.num_params) {
328                 params = kcalloc(arg.num_params, sizeof(struct tee_param),
329                                  GFP_KERNEL);
330                 if (!params)
331                         return -ENOMEM;
332                 uparams = uarg->params;
333                 rc = params_from_user(ctx, params, arg.num_params, uparams);
334                 if (rc)
335                         goto out;
336         }
337
338         rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
339         if (rc)
340                 goto out;
341         have_session = true;
342
343         if (put_user(arg.session, &uarg->session) ||
344             put_user(arg.ret, &uarg->ret) ||
345             put_user(arg.ret_origin, &uarg->ret_origin)) {
346                 rc = -EFAULT;
347                 goto out;
348         }
349         rc = params_to_user(uparams, arg.num_params, params);
350 out:
351         /*
352          * If we've succeeded to open the session but failed to communicate
353          * it back to user space, close the session again to avoid leakage.
354          */
355         if (rc && have_session && ctx->teedev->desc->ops->close_session)
356                 ctx->teedev->desc->ops->close_session(ctx, arg.session);
357
358         if (params) {
359                 /* Decrease ref count for all valid shared memory pointers */
360                 for (n = 0; n < arg.num_params; n++)
361                         if (tee_param_is_memref(params + n) &&
362                             params[n].u.memref.shm)
363                                 tee_shm_put(params[n].u.memref.shm);
364                 kfree(params);
365         }
366
367         return rc;
368 }
369
370 static int tee_ioctl_invoke(struct tee_context *ctx,
371                             struct tee_ioctl_buf_data __user *ubuf)
372 {
373         int rc;
374         size_t n;
375         struct tee_ioctl_buf_data buf;
376         struct tee_ioctl_invoke_arg __user *uarg;
377         struct tee_ioctl_invoke_arg arg;
378         struct tee_ioctl_param __user *uparams = NULL;
379         struct tee_param *params = NULL;
380
381         if (!ctx->teedev->desc->ops->invoke_func)
382                 return -EINVAL;
383
384         if (copy_from_user(&buf, ubuf, sizeof(buf)))
385                 return -EFAULT;
386
387         if (buf.buf_len > TEE_MAX_ARG_SIZE ||
388             buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
389                 return -EINVAL;
390
391         uarg = u64_to_user_ptr(buf.buf_ptr);
392         if (copy_from_user(&arg, uarg, sizeof(arg)))
393                 return -EFAULT;
394
395         if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
396                 return -EINVAL;
397
398         if (arg.num_params) {
399                 params = kcalloc(arg.num_params, sizeof(struct tee_param),
400                                  GFP_KERNEL);
401                 if (!params)
402                         return -ENOMEM;
403                 uparams = uarg->params;
404                 rc = params_from_user(ctx, params, arg.num_params, uparams);
405                 if (rc)
406                         goto out;
407         }
408
409         rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
410         if (rc)
411                 goto out;
412
413         if (put_user(arg.ret, &uarg->ret) ||
414             put_user(arg.ret_origin, &uarg->ret_origin)) {
415                 rc = -EFAULT;
416                 goto out;
417         }
418         rc = params_to_user(uparams, arg.num_params, params);
419 out:
420         if (params) {
421                 /* Decrease ref count for all valid shared memory pointers */
422                 for (n = 0; n < arg.num_params; n++)
423                         if (tee_param_is_memref(params + n) &&
424                             params[n].u.memref.shm)
425                                 tee_shm_put(params[n].u.memref.shm);
426                 kfree(params);
427         }
428         return rc;
429 }
430
431 static int tee_ioctl_cancel(struct tee_context *ctx,
432                             struct tee_ioctl_cancel_arg __user *uarg)
433 {
434         struct tee_ioctl_cancel_arg arg;
435
436         if (!ctx->teedev->desc->ops->cancel_req)
437                 return -EINVAL;
438
439         if (copy_from_user(&arg, uarg, sizeof(arg)))
440                 return -EFAULT;
441
442         return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
443                                                   arg.session);
444 }
445
446 static int
447 tee_ioctl_close_session(struct tee_context *ctx,
448                         struct tee_ioctl_close_session_arg __user *uarg)
449 {
450         struct tee_ioctl_close_session_arg arg;
451
452         if (!ctx->teedev->desc->ops->close_session)
453                 return -EINVAL;
454
455         if (copy_from_user(&arg, uarg, sizeof(arg)))
456                 return -EFAULT;
457
458         return ctx->teedev->desc->ops->close_session(ctx, arg.session);
459 }
460
461 static int params_to_supp(struct tee_context *ctx,
462                           struct tee_ioctl_param __user *uparams,
463                           size_t num_params, struct tee_param *params)
464 {
465         size_t n;
466
467         for (n = 0; n < num_params; n++) {
468                 struct tee_ioctl_param ip;
469                 struct tee_param *p = params + n;
470
471                 ip.attr = p->attr;
472                 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
473                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
474                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
475                         ip.a = p->u.value.a;
476                         ip.b = p->u.value.b;
477                         ip.c = p->u.value.c;
478                         break;
479                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
480                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
481                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
482                         ip.b = p->u.memref.size;
483                         if (!p->u.memref.shm) {
484                                 ip.a = 0;
485                                 ip.c = (u64)-1; /* invalid shm id */
486                                 break;
487                         }
488                         ip.a = p->u.memref.shm_offs;
489                         ip.c = p->u.memref.shm->id;
490                         break;
491                 default:
492                         ip.a = 0;
493                         ip.b = 0;
494                         ip.c = 0;
495                         break;
496                 }
497
498                 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
499                         return -EFAULT;
500         }
501
502         return 0;
503 }
504
505 static int tee_ioctl_supp_recv(struct tee_context *ctx,
506                                struct tee_ioctl_buf_data __user *ubuf)
507 {
508         int rc;
509         struct tee_ioctl_buf_data buf;
510         struct tee_iocl_supp_recv_arg __user *uarg;
511         struct tee_param *params;
512         u32 num_params;
513         u32 func;
514
515         if (!ctx->teedev->desc->ops->supp_recv)
516                 return -EINVAL;
517
518         if (copy_from_user(&buf, ubuf, sizeof(buf)))
519                 return -EFAULT;
520
521         if (buf.buf_len > TEE_MAX_ARG_SIZE ||
522             buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
523                 return -EINVAL;
524
525         uarg = u64_to_user_ptr(buf.buf_ptr);
526         if (get_user(num_params, &uarg->num_params))
527                 return -EFAULT;
528
529         if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
530                 return -EINVAL;
531
532         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
533         if (!params)
534                 return -ENOMEM;
535
536         rc = params_from_user(ctx, params, num_params, uarg->params);
537         if (rc)
538                 goto out;
539
540         rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
541         if (rc)
542                 goto out;
543
544         if (put_user(func, &uarg->func) ||
545             put_user(num_params, &uarg->num_params)) {
546                 rc = -EFAULT;
547                 goto out;
548         }
549
550         rc = params_to_supp(ctx, uarg->params, num_params, params);
551 out:
552         kfree(params);
553         return rc;
554 }
555
556 static int params_from_supp(struct tee_param *params, size_t num_params,
557                             struct tee_ioctl_param __user *uparams)
558 {
559         size_t n;
560
561         for (n = 0; n < num_params; n++) {
562                 struct tee_param *p = params + n;
563                 struct tee_ioctl_param ip;
564
565                 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
566                         return -EFAULT;
567
568                 /* All unused attribute bits has to be zero */
569                 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
570                         return -EINVAL;
571
572                 p->attr = ip.attr;
573                 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
574                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
575                 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
576                         /* Only out and in/out values can be updated */
577                         p->u.value.a = ip.a;
578                         p->u.value.b = ip.b;
579                         p->u.value.c = ip.c;
580                         break;
581                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
582                 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
583                         /*
584                          * Only the size of the memref can be updated.
585                          * Since we don't have access to the original
586                          * parameters here, only store the supplied size.
587                          * The driver will copy the updated size into the
588                          * original parameters.
589                          */
590                         p->u.memref.shm = NULL;
591                         p->u.memref.shm_offs = 0;
592                         p->u.memref.size = ip.b;
593                         break;
594                 default:
595                         memset(&p->u, 0, sizeof(p->u));
596                         break;
597                 }
598         }
599         return 0;
600 }
601
602 static int tee_ioctl_supp_send(struct tee_context *ctx,
603                                struct tee_ioctl_buf_data __user *ubuf)
604 {
605         long rc;
606         struct tee_ioctl_buf_data buf;
607         struct tee_iocl_supp_send_arg __user *uarg;
608         struct tee_param *params;
609         u32 num_params;
610         u32 ret;
611
612         /* Not valid for this driver */
613         if (!ctx->teedev->desc->ops->supp_send)
614                 return -EINVAL;
615
616         if (copy_from_user(&buf, ubuf, sizeof(buf)))
617                 return -EFAULT;
618
619         if (buf.buf_len > TEE_MAX_ARG_SIZE ||
620             buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
621                 return -EINVAL;
622
623         uarg = u64_to_user_ptr(buf.buf_ptr);
624         if (get_user(ret, &uarg->ret) ||
625             get_user(num_params, &uarg->num_params))
626                 return -EFAULT;
627
628         if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
629                 return -EINVAL;
630
631         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
632         if (!params)
633                 return -ENOMEM;
634
635         rc = params_from_supp(params, num_params, uarg->params);
636         if (rc)
637                 goto out;
638
639         rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
640 out:
641         kfree(params);
642         return rc;
643 }
644
645 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
646 {
647         struct tee_context *ctx = filp->private_data;
648         void __user *uarg = (void __user *)arg;
649
650         switch (cmd) {
651         case TEE_IOC_VERSION:
652                 return tee_ioctl_version(ctx, uarg);
653         case TEE_IOC_SHM_ALLOC:
654                 return tee_ioctl_shm_alloc(ctx, uarg);
655         case TEE_IOC_SHM_REGISTER:
656                 return tee_ioctl_shm_register(ctx, uarg);
657         case TEE_IOC_OPEN_SESSION:
658                 return tee_ioctl_open_session(ctx, uarg);
659         case TEE_IOC_INVOKE:
660                 return tee_ioctl_invoke(ctx, uarg);
661         case TEE_IOC_CANCEL:
662                 return tee_ioctl_cancel(ctx, uarg);
663         case TEE_IOC_CLOSE_SESSION:
664                 return tee_ioctl_close_session(ctx, uarg);
665         case TEE_IOC_SUPPL_RECV:
666                 return tee_ioctl_supp_recv(ctx, uarg);
667         case TEE_IOC_SUPPL_SEND:
668                 return tee_ioctl_supp_send(ctx, uarg);
669         default:
670                 return -EINVAL;
671         }
672 }
673
674 static const struct file_operations tee_fops = {
675         .owner = THIS_MODULE,
676         .open = tee_open,
677         .release = tee_release,
678         .unlocked_ioctl = tee_ioctl,
679         .compat_ioctl = tee_ioctl,
680 };
681
682 static void tee_release_device(struct device *dev)
683 {
684         struct tee_device *teedev = container_of(dev, struct tee_device, dev);
685
686         spin_lock(&driver_lock);
687         clear_bit(teedev->id, dev_mask);
688         spin_unlock(&driver_lock);
689         mutex_destroy(&teedev->mutex);
690         idr_destroy(&teedev->idr);
691         kfree(teedev);
692 }
693
694 /**
695  * tee_device_alloc() - Allocate a new struct tee_device instance
696  * @teedesc:    Descriptor for this driver
697  * @dev:        Parent device for this device
698  * @pool:       Shared memory pool, NULL if not used
699  * @driver_data: Private driver data for this device
700  *
701  * Allocates a new struct tee_device instance. The device is
702  * removed by tee_device_unregister().
703  *
704  * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
705  */
706 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
707                                     struct device *dev,
708                                     struct tee_shm_pool *pool,
709                                     void *driver_data)
710 {
711         struct tee_device *teedev;
712         void *ret;
713         int rc, max_id;
714         int offs = 0;
715
716         if (!teedesc || !teedesc->name || !teedesc->ops ||
717             !teedesc->ops->get_version || !teedesc->ops->open ||
718             !teedesc->ops->release || !pool)
719                 return ERR_PTR(-EINVAL);
720
721         teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
722         if (!teedev) {
723                 ret = ERR_PTR(-ENOMEM);
724                 goto err;
725         }
726
727         max_id = TEE_NUM_DEVICES / 2;
728
729         if (teedesc->flags & TEE_DESC_PRIVILEGED) {
730                 offs = TEE_NUM_DEVICES / 2;
731                 max_id = TEE_NUM_DEVICES;
732         }
733
734         spin_lock(&driver_lock);
735         teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
736         if (teedev->id < max_id)
737                 set_bit(teedev->id, dev_mask);
738         spin_unlock(&driver_lock);
739
740         if (teedev->id >= max_id) {
741                 ret = ERR_PTR(-ENOMEM);
742                 goto err;
743         }
744
745         snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
746                  teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
747                  teedev->id - offs);
748
749         teedev->dev.class = tee_class;
750         teedev->dev.release = tee_release_device;
751         teedev->dev.parent = dev;
752
753         teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
754
755         rc = dev_set_name(&teedev->dev, "%s", teedev->name);
756         if (rc) {
757                 ret = ERR_PTR(rc);
758                 goto err_devt;
759         }
760
761         cdev_init(&teedev->cdev, &tee_fops);
762         teedev->cdev.owner = teedesc->owner;
763         teedev->cdev.kobj.parent = &teedev->dev.kobj;
764
765         dev_set_drvdata(&teedev->dev, driver_data);
766         device_initialize(&teedev->dev);
767
768         /* 1 as tee_device_unregister() does one final tee_device_put() */
769         teedev->num_users = 1;
770         init_completion(&teedev->c_no_users);
771         mutex_init(&teedev->mutex);
772         idr_init(&teedev->idr);
773
774         teedev->desc = teedesc;
775         teedev->pool = pool;
776
777         return teedev;
778 err_devt:
779         unregister_chrdev_region(teedev->dev.devt, 1);
780 err:
781         pr_err("could not register %s driver\n",
782                teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
783         if (teedev && teedev->id < TEE_NUM_DEVICES) {
784                 spin_lock(&driver_lock);
785                 clear_bit(teedev->id, dev_mask);
786                 spin_unlock(&driver_lock);
787         }
788         kfree(teedev);
789         return ret;
790 }
791 EXPORT_SYMBOL_GPL(tee_device_alloc);
792
793 static ssize_t implementation_id_show(struct device *dev,
794                                       struct device_attribute *attr, char *buf)
795 {
796         struct tee_device *teedev = container_of(dev, struct tee_device, dev);
797         struct tee_ioctl_version_data vers;
798
799         teedev->desc->ops->get_version(teedev, &vers);
800         return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
801 }
802 static DEVICE_ATTR_RO(implementation_id);
803
804 static struct attribute *tee_dev_attrs[] = {
805         &dev_attr_implementation_id.attr,
806         NULL
807 };
808
809 static const struct attribute_group tee_dev_group = {
810         .attrs = tee_dev_attrs,
811 };
812
813 /**
814  * tee_device_register() - Registers a TEE device
815  * @teedev:     Device to register
816  *
817  * tee_device_unregister() need to be called to remove the @teedev if
818  * this function fails.
819  *
820  * @returns < 0 on failure
821  */
822 int tee_device_register(struct tee_device *teedev)
823 {
824         int rc;
825
826         if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
827                 dev_err(&teedev->dev, "attempt to register twice\n");
828                 return -EINVAL;
829         }
830
831         rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1);
832         if (rc) {
833                 dev_err(&teedev->dev,
834                         "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
835                         teedev->name, MAJOR(teedev->dev.devt),
836                         MINOR(teedev->dev.devt), rc);
837                 return rc;
838         }
839
840         rc = device_add(&teedev->dev);
841         if (rc) {
842                 dev_err(&teedev->dev,
843                         "unable to device_add() %s, major %d, minor %d, err=%d\n",
844                         teedev->name, MAJOR(teedev->dev.devt),
845                         MINOR(teedev->dev.devt), rc);
846                 goto err_device_add;
847         }
848
849         rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
850         if (rc) {
851                 dev_err(&teedev->dev,
852                         "failed to create sysfs attributes, err=%d\n", rc);
853                 goto err_sysfs_create_group;
854         }
855
856         teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
857         return 0;
858
859 err_sysfs_create_group:
860         device_del(&teedev->dev);
861 err_device_add:
862         cdev_del(&teedev->cdev);
863         return rc;
864 }
865 EXPORT_SYMBOL_GPL(tee_device_register);
866
867 void tee_device_put(struct tee_device *teedev)
868 {
869         mutex_lock(&teedev->mutex);
870         /* Shouldn't put in this state */
871         if (!WARN_ON(!teedev->desc)) {
872                 teedev->num_users--;
873                 if (!teedev->num_users) {
874                         teedev->desc = NULL;
875                         complete(&teedev->c_no_users);
876                 }
877         }
878         mutex_unlock(&teedev->mutex);
879 }
880
881 bool tee_device_get(struct tee_device *teedev)
882 {
883         mutex_lock(&teedev->mutex);
884         if (!teedev->desc) {
885                 mutex_unlock(&teedev->mutex);
886                 return false;
887         }
888         teedev->num_users++;
889         mutex_unlock(&teedev->mutex);
890         return true;
891 }
892
893 /**
894  * tee_device_unregister() - Removes a TEE device
895  * @teedev:     Device to unregister
896  *
897  * This function should be called to remove the @teedev even if
898  * tee_device_register() hasn't been called yet. Does nothing if
899  * @teedev is NULL.
900  */
901 void tee_device_unregister(struct tee_device *teedev)
902 {
903         if (!teedev)
904                 return;
905
906         if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
907                 sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
908                 cdev_del(&teedev->cdev);
909                 device_del(&teedev->dev);
910         }
911
912         tee_device_put(teedev);
913         wait_for_completion(&teedev->c_no_users);
914
915         /*
916          * No need to take a mutex any longer now since teedev->desc was
917          * set to NULL before teedev->c_no_users was completed.
918          */
919
920         teedev->pool = NULL;
921
922         put_device(&teedev->dev);
923 }
924 EXPORT_SYMBOL_GPL(tee_device_unregister);
925
926 /**
927  * tee_get_drvdata() - Return driver_data pointer
928  * @teedev:     Device containing the driver_data pointer
929  * @returns the driver_data pointer supplied to tee_register().
930  */
931 void *tee_get_drvdata(struct tee_device *teedev)
932 {
933         return dev_get_drvdata(&teedev->dev);
934 }
935 EXPORT_SYMBOL_GPL(tee_get_drvdata);
936
937 static int __init tee_init(void)
938 {
939         int rc;
940
941         tee_class = class_create(THIS_MODULE, "tee");
942         if (IS_ERR(tee_class)) {
943                 pr_err("couldn't create class\n");
944                 return PTR_ERR(tee_class);
945         }
946
947         rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
948         if (rc) {
949                 pr_err("failed to allocate char dev region\n");
950                 class_destroy(tee_class);
951                 tee_class = NULL;
952         }
953
954         return rc;
955 }
956
957 static void __exit tee_exit(void)
958 {
959         class_destroy(tee_class);
960         tee_class = NULL;
961         unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
962 }
963
964 subsys_initcall(tee_init);
965 module_exit(tee_exit);
966
967 MODULE_AUTHOR("Linaro");
968 MODULE_DESCRIPTION("TEE Driver");
969 MODULE_VERSION("1.0");
970 MODULE_LICENSE("GPL v2");