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