drm/i915/dsi: Control panel and backlight enable GPIOs on BYT
[linux/fpc-iii.git] / drivers / tee / tee_core.c
blob37d22e39fd8d21ea282f8cdf17da1dd5775b6ecb
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015-2016, Linaro Limited
4 */
6 #define pr_fmt(fmt) "%s: " fmt, __func__
8 #include <linux/cdev.h>
9 #include <linux/fs.h>
10 #include <linux/idr.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/tee_drv.h>
14 #include <linux/uaccess.h>
15 #include "tee_private.h"
17 #define TEE_NUM_DEVICES 32
19 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
22 * Unprivileged devices in the lower half range and privileged devices in
23 * the upper half range.
25 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
26 static DEFINE_SPINLOCK(driver_lock);
28 static struct class *tee_class;
29 static dev_t tee_devt;
31 static struct tee_context *teedev_open(struct tee_device *teedev)
33 int rc;
34 struct tee_context *ctx;
36 if (!tee_device_get(teedev))
37 return ERR_PTR(-EINVAL);
39 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
40 if (!ctx) {
41 rc = -ENOMEM;
42 goto err;
45 kref_init(&ctx->refcount);
46 ctx->teedev = teedev;
47 INIT_LIST_HEAD(&ctx->list_shm);
48 rc = teedev->desc->ops->open(ctx);
49 if (rc)
50 goto err;
52 return ctx;
53 err:
54 kfree(ctx);
55 tee_device_put(teedev);
56 return ERR_PTR(rc);
60 void teedev_ctx_get(struct tee_context *ctx)
62 if (ctx->releasing)
63 return;
65 kref_get(&ctx->refcount);
68 static void teedev_ctx_release(struct kref *ref)
70 struct tee_context *ctx = container_of(ref, struct tee_context,
71 refcount);
72 ctx->releasing = true;
73 ctx->teedev->desc->ops->release(ctx);
74 kfree(ctx);
77 void teedev_ctx_put(struct tee_context *ctx)
79 if (ctx->releasing)
80 return;
82 kref_put(&ctx->refcount, teedev_ctx_release);
85 static void teedev_close_context(struct tee_context *ctx)
87 tee_device_put(ctx->teedev);
88 teedev_ctx_put(ctx);
91 static int tee_open(struct inode *inode, struct file *filp)
93 struct tee_context *ctx;
95 ctx = teedev_open(container_of(inode->i_cdev, struct tee_device, cdev));
96 if (IS_ERR(ctx))
97 return PTR_ERR(ctx);
100 * Default user-space behaviour is to wait for tee-supplicant
101 * if not present for any requests in this context.
103 ctx->supp_nowait = false;
104 filp->private_data = ctx;
105 return 0;
108 static int tee_release(struct inode *inode, struct file *filp)
110 teedev_close_context(filp->private_data);
111 return 0;
114 static int tee_ioctl_version(struct tee_context *ctx,
115 struct tee_ioctl_version_data __user *uvers)
117 struct tee_ioctl_version_data vers;
119 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
121 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
122 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
124 if (copy_to_user(uvers, &vers, sizeof(vers)))
125 return -EFAULT;
127 return 0;
130 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
131 struct tee_ioctl_shm_alloc_data __user *udata)
133 long ret;
134 struct tee_ioctl_shm_alloc_data data;
135 struct tee_shm *shm;
137 if (copy_from_user(&data, udata, sizeof(data)))
138 return -EFAULT;
140 /* Currently no input flags are supported */
141 if (data.flags)
142 return -EINVAL;
144 shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
145 if (IS_ERR(shm))
146 return PTR_ERR(shm);
148 data.id = shm->id;
149 data.flags = shm->flags;
150 data.size = shm->size;
152 if (copy_to_user(udata, &data, sizeof(data)))
153 ret = -EFAULT;
154 else
155 ret = tee_shm_get_fd(shm);
158 * When user space closes the file descriptor the shared memory
159 * should be freed or if tee_shm_get_fd() failed then it will
160 * be freed immediately.
162 tee_shm_put(shm);
163 return ret;
166 static int
167 tee_ioctl_shm_register(struct tee_context *ctx,
168 struct tee_ioctl_shm_register_data __user *udata)
170 long ret;
171 struct tee_ioctl_shm_register_data data;
172 struct tee_shm *shm;
174 if (copy_from_user(&data, udata, sizeof(data)))
175 return -EFAULT;
177 /* Currently no input flags are supported */
178 if (data.flags)
179 return -EINVAL;
181 shm = tee_shm_register(ctx, data.addr, data.length,
182 TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
183 if (IS_ERR(shm))
184 return PTR_ERR(shm);
186 data.id = shm->id;
187 data.flags = shm->flags;
188 data.length = shm->size;
190 if (copy_to_user(udata, &data, sizeof(data)))
191 ret = -EFAULT;
192 else
193 ret = tee_shm_get_fd(shm);
195 * When user space closes the file descriptor the shared memory
196 * should be freed or if tee_shm_get_fd() failed then it will
197 * be freed immediately.
199 tee_shm_put(shm);
200 return ret;
203 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
204 size_t num_params,
205 struct tee_ioctl_param __user *uparams)
207 size_t n;
209 for (n = 0; n < num_params; n++) {
210 struct tee_shm *shm;
211 struct tee_ioctl_param ip;
213 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
214 return -EFAULT;
216 /* All unused attribute bits has to be zero */
217 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
218 return -EINVAL;
220 params[n].attr = ip.attr;
221 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
222 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
223 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
224 break;
225 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
226 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
227 params[n].u.value.a = ip.a;
228 params[n].u.value.b = ip.b;
229 params[n].u.value.c = ip.c;
230 break;
231 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
232 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
233 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
235 * If we fail to get a pointer to a shared memory
236 * object (and increase the ref count) from an
237 * identifier we return an error. All pointers that
238 * has been added in params have an increased ref
239 * count. It's the callers responibility to do
240 * tee_shm_put() on all resolved pointers.
242 shm = tee_shm_get_from_id(ctx, ip.c);
243 if (IS_ERR(shm))
244 return PTR_ERR(shm);
247 * Ensure offset + size does not overflow offset
248 * and does not overflow the size of the referred
249 * shared memory object.
251 if ((ip.a + ip.b) < ip.a ||
252 (ip.a + ip.b) > shm->size) {
253 tee_shm_put(shm);
254 return -EINVAL;
257 params[n].u.memref.shm_offs = ip.a;
258 params[n].u.memref.size = ip.b;
259 params[n].u.memref.shm = shm;
260 break;
261 default:
262 /* Unknown attribute */
263 return -EINVAL;
266 return 0;
269 static int params_to_user(struct tee_ioctl_param __user *uparams,
270 size_t num_params, struct tee_param *params)
272 size_t n;
274 for (n = 0; n < num_params; n++) {
275 struct tee_ioctl_param __user *up = uparams + n;
276 struct tee_param *p = params + n;
278 switch (p->attr) {
279 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
280 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
281 if (put_user(p->u.value.a, &up->a) ||
282 put_user(p->u.value.b, &up->b) ||
283 put_user(p->u.value.c, &up->c))
284 return -EFAULT;
285 break;
286 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
287 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
288 if (put_user((u64)p->u.memref.size, &up->b))
289 return -EFAULT;
290 default:
291 break;
294 return 0;
297 static int tee_ioctl_open_session(struct tee_context *ctx,
298 struct tee_ioctl_buf_data __user *ubuf)
300 int rc;
301 size_t n;
302 struct tee_ioctl_buf_data buf;
303 struct tee_ioctl_open_session_arg __user *uarg;
304 struct tee_ioctl_open_session_arg arg;
305 struct tee_ioctl_param __user *uparams = NULL;
306 struct tee_param *params = NULL;
307 bool have_session = false;
309 if (!ctx->teedev->desc->ops->open_session)
310 return -EINVAL;
312 if (copy_from_user(&buf, ubuf, sizeof(buf)))
313 return -EFAULT;
315 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
316 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
317 return -EINVAL;
319 uarg = u64_to_user_ptr(buf.buf_ptr);
320 if (copy_from_user(&arg, uarg, sizeof(arg)))
321 return -EFAULT;
323 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
324 return -EINVAL;
326 if (arg.num_params) {
327 params = kcalloc(arg.num_params, sizeof(struct tee_param),
328 GFP_KERNEL);
329 if (!params)
330 return -ENOMEM;
331 uparams = uarg->params;
332 rc = params_from_user(ctx, params, arg.num_params, uparams);
333 if (rc)
334 goto out;
337 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
338 if (rc)
339 goto out;
340 have_session = true;
342 if (put_user(arg.session, &uarg->session) ||
343 put_user(arg.ret, &uarg->ret) ||
344 put_user(arg.ret_origin, &uarg->ret_origin)) {
345 rc = -EFAULT;
346 goto out;
348 rc = params_to_user(uparams, arg.num_params, params);
349 out:
351 * If we've succeeded to open the session but failed to communicate
352 * it back to user space, close the session again to avoid leakage.
354 if (rc && have_session && ctx->teedev->desc->ops->close_session)
355 ctx->teedev->desc->ops->close_session(ctx, arg.session);
357 if (params) {
358 /* Decrease ref count for all valid shared memory pointers */
359 for (n = 0; n < arg.num_params; n++)
360 if (tee_param_is_memref(params + n) &&
361 params[n].u.memref.shm)
362 tee_shm_put(params[n].u.memref.shm);
363 kfree(params);
366 return rc;
369 static int tee_ioctl_invoke(struct tee_context *ctx,
370 struct tee_ioctl_buf_data __user *ubuf)
372 int rc;
373 size_t n;
374 struct tee_ioctl_buf_data buf;
375 struct tee_ioctl_invoke_arg __user *uarg;
376 struct tee_ioctl_invoke_arg arg;
377 struct tee_ioctl_param __user *uparams = NULL;
378 struct tee_param *params = NULL;
380 if (!ctx->teedev->desc->ops->invoke_func)
381 return -EINVAL;
383 if (copy_from_user(&buf, ubuf, sizeof(buf)))
384 return -EFAULT;
386 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
387 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
388 return -EINVAL;
390 uarg = u64_to_user_ptr(buf.buf_ptr);
391 if (copy_from_user(&arg, uarg, sizeof(arg)))
392 return -EFAULT;
394 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
395 return -EINVAL;
397 if (arg.num_params) {
398 params = kcalloc(arg.num_params, sizeof(struct tee_param),
399 GFP_KERNEL);
400 if (!params)
401 return -ENOMEM;
402 uparams = uarg->params;
403 rc = params_from_user(ctx, params, arg.num_params, uparams);
404 if (rc)
405 goto out;
408 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
409 if (rc)
410 goto out;
412 if (put_user(arg.ret, &uarg->ret) ||
413 put_user(arg.ret_origin, &uarg->ret_origin)) {
414 rc = -EFAULT;
415 goto out;
417 rc = params_to_user(uparams, arg.num_params, params);
418 out:
419 if (params) {
420 /* Decrease ref count for all valid shared memory pointers */
421 for (n = 0; n < arg.num_params; n++)
422 if (tee_param_is_memref(params + n) &&
423 params[n].u.memref.shm)
424 tee_shm_put(params[n].u.memref.shm);
425 kfree(params);
427 return rc;
430 static int tee_ioctl_cancel(struct tee_context *ctx,
431 struct tee_ioctl_cancel_arg __user *uarg)
433 struct tee_ioctl_cancel_arg arg;
435 if (!ctx->teedev->desc->ops->cancel_req)
436 return -EINVAL;
438 if (copy_from_user(&arg, uarg, sizeof(arg)))
439 return -EFAULT;
441 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
442 arg.session);
445 static int
446 tee_ioctl_close_session(struct tee_context *ctx,
447 struct tee_ioctl_close_session_arg __user *uarg)
449 struct tee_ioctl_close_session_arg arg;
451 if (!ctx->teedev->desc->ops->close_session)
452 return -EINVAL;
454 if (copy_from_user(&arg, uarg, sizeof(arg)))
455 return -EFAULT;
457 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
460 static int params_to_supp(struct tee_context *ctx,
461 struct tee_ioctl_param __user *uparams,
462 size_t num_params, struct tee_param *params)
464 size_t n;
466 for (n = 0; n < num_params; n++) {
467 struct tee_ioctl_param ip;
468 struct tee_param *p = params + n;
470 ip.attr = p->attr;
471 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
472 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
473 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
474 ip.a = p->u.value.a;
475 ip.b = p->u.value.b;
476 ip.c = p->u.value.c;
477 break;
478 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
479 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
480 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
481 ip.b = p->u.memref.size;
482 if (!p->u.memref.shm) {
483 ip.a = 0;
484 ip.c = (u64)-1; /* invalid shm id */
485 break;
487 ip.a = p->u.memref.shm_offs;
488 ip.c = p->u.memref.shm->id;
489 break;
490 default:
491 ip.a = 0;
492 ip.b = 0;
493 ip.c = 0;
494 break;
497 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
498 return -EFAULT;
501 return 0;
504 static int tee_ioctl_supp_recv(struct tee_context *ctx,
505 struct tee_ioctl_buf_data __user *ubuf)
507 int rc;
508 struct tee_ioctl_buf_data buf;
509 struct tee_iocl_supp_recv_arg __user *uarg;
510 struct tee_param *params;
511 u32 num_params;
512 u32 func;
514 if (!ctx->teedev->desc->ops->supp_recv)
515 return -EINVAL;
517 if (copy_from_user(&buf, ubuf, sizeof(buf)))
518 return -EFAULT;
520 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
521 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
522 return -EINVAL;
524 uarg = u64_to_user_ptr(buf.buf_ptr);
525 if (get_user(num_params, &uarg->num_params))
526 return -EFAULT;
528 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
529 return -EINVAL;
531 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
532 if (!params)
533 return -ENOMEM;
535 rc = params_from_user(ctx, params, num_params, uarg->params);
536 if (rc)
537 goto out;
539 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
540 if (rc)
541 goto out;
543 if (put_user(func, &uarg->func) ||
544 put_user(num_params, &uarg->num_params)) {
545 rc = -EFAULT;
546 goto out;
549 rc = params_to_supp(ctx, uarg->params, num_params, params);
550 out:
551 kfree(params);
552 return rc;
555 static int params_from_supp(struct tee_param *params, size_t num_params,
556 struct tee_ioctl_param __user *uparams)
558 size_t n;
560 for (n = 0; n < num_params; n++) {
561 struct tee_param *p = params + n;
562 struct tee_ioctl_param ip;
564 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
565 return -EFAULT;
567 /* All unused attribute bits has to be zero */
568 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
569 return -EINVAL;
571 p->attr = ip.attr;
572 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
573 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
574 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
575 /* Only out and in/out values can be updated */
576 p->u.value.a = ip.a;
577 p->u.value.b = ip.b;
578 p->u.value.c = ip.c;
579 break;
580 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
581 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
583 * Only the size of the memref can be updated.
584 * Since we don't have access to the original
585 * parameters here, only store the supplied size.
586 * The driver will copy the updated size into the
587 * original parameters.
589 p->u.memref.shm = NULL;
590 p->u.memref.shm_offs = 0;
591 p->u.memref.size = ip.b;
592 break;
593 default:
594 memset(&p->u, 0, sizeof(p->u));
595 break;
598 return 0;
601 static int tee_ioctl_supp_send(struct tee_context *ctx,
602 struct tee_ioctl_buf_data __user *ubuf)
604 long rc;
605 struct tee_ioctl_buf_data buf;
606 struct tee_iocl_supp_send_arg __user *uarg;
607 struct tee_param *params;
608 u32 num_params;
609 u32 ret;
611 /* Not valid for this driver */
612 if (!ctx->teedev->desc->ops->supp_send)
613 return -EINVAL;
615 if (copy_from_user(&buf, ubuf, sizeof(buf)))
616 return -EFAULT;
618 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
619 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
620 return -EINVAL;
622 uarg = u64_to_user_ptr(buf.buf_ptr);
623 if (get_user(ret, &uarg->ret) ||
624 get_user(num_params, &uarg->num_params))
625 return -EFAULT;
627 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
628 return -EINVAL;
630 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
631 if (!params)
632 return -ENOMEM;
634 rc = params_from_supp(params, num_params, uarg->params);
635 if (rc)
636 goto out;
638 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
639 out:
640 kfree(params);
641 return rc;
644 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
646 struct tee_context *ctx = filp->private_data;
647 void __user *uarg = (void __user *)arg;
649 switch (cmd) {
650 case TEE_IOC_VERSION:
651 return tee_ioctl_version(ctx, uarg);
652 case TEE_IOC_SHM_ALLOC:
653 return tee_ioctl_shm_alloc(ctx, uarg);
654 case TEE_IOC_SHM_REGISTER:
655 return tee_ioctl_shm_register(ctx, uarg);
656 case TEE_IOC_OPEN_SESSION:
657 return tee_ioctl_open_session(ctx, uarg);
658 case TEE_IOC_INVOKE:
659 return tee_ioctl_invoke(ctx, uarg);
660 case TEE_IOC_CANCEL:
661 return tee_ioctl_cancel(ctx, uarg);
662 case TEE_IOC_CLOSE_SESSION:
663 return tee_ioctl_close_session(ctx, uarg);
664 case TEE_IOC_SUPPL_RECV:
665 return tee_ioctl_supp_recv(ctx, uarg);
666 case TEE_IOC_SUPPL_SEND:
667 return tee_ioctl_supp_send(ctx, uarg);
668 default:
669 return -EINVAL;
673 static const struct file_operations tee_fops = {
674 .owner = THIS_MODULE,
675 .open = tee_open,
676 .release = tee_release,
677 .unlocked_ioctl = tee_ioctl,
678 .compat_ioctl = compat_ptr_ioctl,
681 static void tee_release_device(struct device *dev)
683 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
685 spin_lock(&driver_lock);
686 clear_bit(teedev->id, dev_mask);
687 spin_unlock(&driver_lock);
688 mutex_destroy(&teedev->mutex);
689 idr_destroy(&teedev->idr);
690 kfree(teedev);
694 * tee_device_alloc() - Allocate a new struct tee_device instance
695 * @teedesc: Descriptor for this driver
696 * @dev: Parent device for this device
697 * @pool: Shared memory pool, NULL if not used
698 * @driver_data: Private driver data for this device
700 * Allocates a new struct tee_device instance. The device is
701 * removed by tee_device_unregister().
703 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
705 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
706 struct device *dev,
707 struct tee_shm_pool *pool,
708 void *driver_data)
710 struct tee_device *teedev;
711 void *ret;
712 int rc, max_id;
713 int offs = 0;
715 if (!teedesc || !teedesc->name || !teedesc->ops ||
716 !teedesc->ops->get_version || !teedesc->ops->open ||
717 !teedesc->ops->release || !pool)
718 return ERR_PTR(-EINVAL);
720 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
721 if (!teedev) {
722 ret = ERR_PTR(-ENOMEM);
723 goto err;
726 max_id = TEE_NUM_DEVICES / 2;
728 if (teedesc->flags & TEE_DESC_PRIVILEGED) {
729 offs = TEE_NUM_DEVICES / 2;
730 max_id = TEE_NUM_DEVICES;
733 spin_lock(&driver_lock);
734 teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
735 if (teedev->id < max_id)
736 set_bit(teedev->id, dev_mask);
737 spin_unlock(&driver_lock);
739 if (teedev->id >= max_id) {
740 ret = ERR_PTR(-ENOMEM);
741 goto err;
744 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
745 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
746 teedev->id - offs);
748 teedev->dev.class = tee_class;
749 teedev->dev.release = tee_release_device;
750 teedev->dev.parent = dev;
752 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
754 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
755 if (rc) {
756 ret = ERR_PTR(rc);
757 goto err_devt;
760 cdev_init(&teedev->cdev, &tee_fops);
761 teedev->cdev.owner = teedesc->owner;
762 teedev->cdev.kobj.parent = &teedev->dev.kobj;
764 dev_set_drvdata(&teedev->dev, driver_data);
765 device_initialize(&teedev->dev);
767 /* 1 as tee_device_unregister() does one final tee_device_put() */
768 teedev->num_users = 1;
769 init_completion(&teedev->c_no_users);
770 mutex_init(&teedev->mutex);
771 idr_init(&teedev->idr);
773 teedev->desc = teedesc;
774 teedev->pool = pool;
776 return teedev;
777 err_devt:
778 unregister_chrdev_region(teedev->dev.devt, 1);
779 err:
780 pr_err("could not register %s driver\n",
781 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
782 if (teedev && teedev->id < TEE_NUM_DEVICES) {
783 spin_lock(&driver_lock);
784 clear_bit(teedev->id, dev_mask);
785 spin_unlock(&driver_lock);
787 kfree(teedev);
788 return ret;
790 EXPORT_SYMBOL_GPL(tee_device_alloc);
792 static ssize_t implementation_id_show(struct device *dev,
793 struct device_attribute *attr, char *buf)
795 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
796 struct tee_ioctl_version_data vers;
798 teedev->desc->ops->get_version(teedev, &vers);
799 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
801 static DEVICE_ATTR_RO(implementation_id);
803 static struct attribute *tee_dev_attrs[] = {
804 &dev_attr_implementation_id.attr,
805 NULL
808 static const struct attribute_group tee_dev_group = {
809 .attrs = tee_dev_attrs,
813 * tee_device_register() - Registers a TEE device
814 * @teedev: Device to register
816 * tee_device_unregister() need to be called to remove the @teedev if
817 * this function fails.
819 * @returns < 0 on failure
821 int tee_device_register(struct tee_device *teedev)
823 int rc;
825 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
826 dev_err(&teedev->dev, "attempt to register twice\n");
827 return -EINVAL;
830 rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1);
831 if (rc) {
832 dev_err(&teedev->dev,
833 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
834 teedev->name, MAJOR(teedev->dev.devt),
835 MINOR(teedev->dev.devt), rc);
836 return rc;
839 rc = device_add(&teedev->dev);
840 if (rc) {
841 dev_err(&teedev->dev,
842 "unable to device_add() %s, major %d, minor %d, err=%d\n",
843 teedev->name, MAJOR(teedev->dev.devt),
844 MINOR(teedev->dev.devt), rc);
845 goto err_device_add;
848 rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
849 if (rc) {
850 dev_err(&teedev->dev,
851 "failed to create sysfs attributes, err=%d\n", rc);
852 goto err_sysfs_create_group;
855 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
856 return 0;
858 err_sysfs_create_group:
859 device_del(&teedev->dev);
860 err_device_add:
861 cdev_del(&teedev->cdev);
862 return rc;
864 EXPORT_SYMBOL_GPL(tee_device_register);
866 void tee_device_put(struct tee_device *teedev)
868 mutex_lock(&teedev->mutex);
869 /* Shouldn't put in this state */
870 if (!WARN_ON(!teedev->desc)) {
871 teedev->num_users--;
872 if (!teedev->num_users) {
873 teedev->desc = NULL;
874 complete(&teedev->c_no_users);
877 mutex_unlock(&teedev->mutex);
880 bool tee_device_get(struct tee_device *teedev)
882 mutex_lock(&teedev->mutex);
883 if (!teedev->desc) {
884 mutex_unlock(&teedev->mutex);
885 return false;
887 teedev->num_users++;
888 mutex_unlock(&teedev->mutex);
889 return true;
893 * tee_device_unregister() - Removes a TEE device
894 * @teedev: Device to unregister
896 * This function should be called to remove the @teedev even if
897 * tee_device_register() hasn't been called yet. Does nothing if
898 * @teedev is NULL.
900 void tee_device_unregister(struct tee_device *teedev)
902 if (!teedev)
903 return;
905 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
906 sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
907 cdev_del(&teedev->cdev);
908 device_del(&teedev->dev);
911 tee_device_put(teedev);
912 wait_for_completion(&teedev->c_no_users);
915 * No need to take a mutex any longer now since teedev->desc was
916 * set to NULL before teedev->c_no_users was completed.
919 teedev->pool = NULL;
921 put_device(&teedev->dev);
923 EXPORT_SYMBOL_GPL(tee_device_unregister);
926 * tee_get_drvdata() - Return driver_data pointer
927 * @teedev: Device containing the driver_data pointer
928 * @returns the driver_data pointer supplied to tee_register().
930 void *tee_get_drvdata(struct tee_device *teedev)
932 return dev_get_drvdata(&teedev->dev);
934 EXPORT_SYMBOL_GPL(tee_get_drvdata);
936 struct match_dev_data {
937 struct tee_ioctl_version_data *vers;
938 const void *data;
939 int (*match)(struct tee_ioctl_version_data *, const void *);
942 static int match_dev(struct device *dev, const void *data)
944 const struct match_dev_data *match_data = data;
945 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
947 teedev->desc->ops->get_version(teedev, match_data->vers);
948 return match_data->match(match_data->vers, match_data->data);
951 struct tee_context *
952 tee_client_open_context(struct tee_context *start,
953 int (*match)(struct tee_ioctl_version_data *,
954 const void *),
955 const void *data, struct tee_ioctl_version_data *vers)
957 struct device *dev = NULL;
958 struct device *put_dev = NULL;
959 struct tee_context *ctx = NULL;
960 struct tee_ioctl_version_data v;
961 struct match_dev_data match_data = { vers ? vers : &v, data, match };
963 if (start)
964 dev = &start->teedev->dev;
966 do {
967 dev = class_find_device(tee_class, dev, &match_data, match_dev);
968 if (!dev) {
969 ctx = ERR_PTR(-ENOENT);
970 break;
973 put_device(put_dev);
974 put_dev = dev;
976 ctx = teedev_open(container_of(dev, struct tee_device, dev));
977 } while (IS_ERR(ctx) && PTR_ERR(ctx) != -ENOMEM);
979 put_device(put_dev);
981 * Default behaviour for in kernel client is to not wait for
982 * tee-supplicant if not present for any requests in this context.
983 * Also this flag could be configured again before call to
984 * tee_client_open_session() if any in kernel client requires
985 * different behaviour.
987 if (!IS_ERR(ctx))
988 ctx->supp_nowait = true;
990 return ctx;
992 EXPORT_SYMBOL_GPL(tee_client_open_context);
994 void tee_client_close_context(struct tee_context *ctx)
996 teedev_close_context(ctx);
998 EXPORT_SYMBOL_GPL(tee_client_close_context);
1000 void tee_client_get_version(struct tee_context *ctx,
1001 struct tee_ioctl_version_data *vers)
1003 ctx->teedev->desc->ops->get_version(ctx->teedev, vers);
1005 EXPORT_SYMBOL_GPL(tee_client_get_version);
1007 int tee_client_open_session(struct tee_context *ctx,
1008 struct tee_ioctl_open_session_arg *arg,
1009 struct tee_param *param)
1011 if (!ctx->teedev->desc->ops->open_session)
1012 return -EINVAL;
1013 return ctx->teedev->desc->ops->open_session(ctx, arg, param);
1015 EXPORT_SYMBOL_GPL(tee_client_open_session);
1017 int tee_client_close_session(struct tee_context *ctx, u32 session)
1019 if (!ctx->teedev->desc->ops->close_session)
1020 return -EINVAL;
1021 return ctx->teedev->desc->ops->close_session(ctx, session);
1023 EXPORT_SYMBOL_GPL(tee_client_close_session);
1025 int tee_client_invoke_func(struct tee_context *ctx,
1026 struct tee_ioctl_invoke_arg *arg,
1027 struct tee_param *param)
1029 if (!ctx->teedev->desc->ops->invoke_func)
1030 return -EINVAL;
1031 return ctx->teedev->desc->ops->invoke_func(ctx, arg, param);
1033 EXPORT_SYMBOL_GPL(tee_client_invoke_func);
1035 int tee_client_cancel_req(struct tee_context *ctx,
1036 struct tee_ioctl_cancel_arg *arg)
1038 if (!ctx->teedev->desc->ops->cancel_req)
1039 return -EINVAL;
1040 return ctx->teedev->desc->ops->cancel_req(ctx, arg->cancel_id,
1041 arg->session);
1044 static int tee_client_device_match(struct device *dev,
1045 struct device_driver *drv)
1047 const struct tee_client_device_id *id_table;
1048 struct tee_client_device *tee_device;
1050 id_table = to_tee_client_driver(drv)->id_table;
1051 tee_device = to_tee_client_device(dev);
1053 while (!uuid_is_null(&id_table->uuid)) {
1054 if (uuid_equal(&tee_device->id.uuid, &id_table->uuid))
1055 return 1;
1056 id_table++;
1059 return 0;
1062 static int tee_client_device_uevent(struct device *dev,
1063 struct kobj_uevent_env *env)
1065 uuid_t *dev_id = &to_tee_client_device(dev)->id.uuid;
1067 return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
1070 struct bus_type tee_bus_type = {
1071 .name = "tee",
1072 .match = tee_client_device_match,
1073 .uevent = tee_client_device_uevent,
1075 EXPORT_SYMBOL_GPL(tee_bus_type);
1077 static int __init tee_init(void)
1079 int rc;
1081 tee_class = class_create(THIS_MODULE, "tee");
1082 if (IS_ERR(tee_class)) {
1083 pr_err("couldn't create class\n");
1084 return PTR_ERR(tee_class);
1087 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
1088 if (rc) {
1089 pr_err("failed to allocate char dev region\n");
1090 goto out_unreg_class;
1093 rc = bus_register(&tee_bus_type);
1094 if (rc) {
1095 pr_err("failed to register tee bus\n");
1096 goto out_unreg_chrdev;
1099 return 0;
1101 out_unreg_chrdev:
1102 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1103 out_unreg_class:
1104 class_destroy(tee_class);
1105 tee_class = NULL;
1107 return rc;
1110 static void __exit tee_exit(void)
1112 bus_unregister(&tee_bus_type);
1113 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1114 class_destroy(tee_class);
1115 tee_class = NULL;
1118 subsys_initcall(tee_init);
1119 module_exit(tee_exit);
1121 MODULE_AUTHOR("Linaro");
1122 MODULE_DESCRIPTION("TEE Driver");
1123 MODULE_VERSION("1.0");
1124 MODULE_LICENSE("GPL v2");