1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2016, Linaro Limited
6 #define pr_fmt(fmt) "%s: " fmt, __func__
8 #include <linux/cdev.h>
9 #include <linux/cred.h>
11 #include <linux/idr.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/tee_drv.h>
15 #include <linux/uaccess.h>
16 #include <crypto/hash.h>
17 #include <crypto/sha1.h>
18 #include "tee_private.h"
20 #define TEE_NUM_DEVICES 32
22 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
24 #define TEE_UUID_NS_NAME_SIZE 128
27 * TEE Client UUID name space identifier (UUIDv4)
29 * Value here is random UUID that is allocated as name space identifier for
30 * forming Client UUID's for TEE environment using UUIDv5 scheme.
32 static const uuid_t tee_client_uuid_ns = UUID_INIT(0x58ac9ca0, 0x2086, 0x4683,
33 0xa1, 0xb8, 0xec, 0x4b,
34 0xc0, 0x8e, 0x01, 0xb6);
37 * Unprivileged devices in the lower half range and privileged devices in
38 * the upper half range.
40 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
41 static DEFINE_SPINLOCK(driver_lock);
43 static struct class *tee_class;
44 static dev_t tee_devt;
46 static struct tee_context *teedev_open(struct tee_device *teedev)
49 struct tee_context *ctx;
51 if (!tee_device_get(teedev))
52 return ERR_PTR(-EINVAL);
54 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
60 kref_init(&ctx->refcount);
62 rc = teedev->desc->ops->open(ctx);
69 tee_device_put(teedev);
74 void teedev_ctx_get(struct tee_context *ctx)
79 kref_get(&ctx->refcount);
82 static void teedev_ctx_release(struct kref *ref)
84 struct tee_context *ctx = container_of(ref, struct tee_context,
86 ctx->releasing = true;
87 ctx->teedev->desc->ops->release(ctx);
91 void teedev_ctx_put(struct tee_context *ctx)
96 kref_put(&ctx->refcount, teedev_ctx_release);
99 static void teedev_close_context(struct tee_context *ctx)
101 struct tee_device *teedev = ctx->teedev;
104 tee_device_put(teedev);
107 static int tee_open(struct inode *inode, struct file *filp)
109 struct tee_context *ctx;
111 ctx = teedev_open(container_of(inode->i_cdev, struct tee_device, cdev));
116 * Default user-space behaviour is to wait for tee-supplicant
117 * if not present for any requests in this context.
119 ctx->supp_nowait = false;
120 filp->private_data = ctx;
124 static int tee_release(struct inode *inode, struct file *filp)
126 teedev_close_context(filp->private_data);
131 * uuid_v5() - Calculate UUIDv5
132 * @uuid: Resulting UUID
133 * @ns: Name space ID for UUIDv5 function
134 * @name: Name for UUIDv5 function
135 * @size: Size of name
137 * UUIDv5 is specific in RFC 4122.
139 * This implements section (for SHA-1):
140 * 4.3. Algorithm for Creating a Name-Based UUID
142 static int uuid_v5(uuid_t *uuid, const uuid_t *ns, const void *name,
145 unsigned char hash[SHA1_DIGEST_SIZE];
146 struct crypto_shash *shash = NULL;
147 struct shash_desc *desc = NULL;
150 shash = crypto_alloc_shash("sha1", 0, 0);
153 pr_err("shash(sha1) allocation failed\n");
157 desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
166 rc = crypto_shash_init(desc);
170 rc = crypto_shash_update(desc, (const u8 *)ns, sizeof(*ns));
174 rc = crypto_shash_update(desc, (const u8 *)name, size);
178 rc = crypto_shash_final(desc, hash);
182 memcpy(uuid->b, hash, UUID_SIZE);
184 /* Tag for version 5 */
185 uuid->b[6] = (hash[6] & 0x0F) | 0x50;
186 uuid->b[8] = (hash[8] & 0x3F) | 0x80;
192 crypto_free_shash(shash);
196 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
197 const u8 connection_data[TEE_IOCTL_UUID_LEN])
199 gid_t ns_grp = (gid_t)-1;
200 kgid_t grp = INVALID_GID;
205 if (connection_method == TEE_IOCTL_LOGIN_PUBLIC ||
206 connection_method == TEE_IOCTL_LOGIN_REE_KERNEL) {
207 /* Nil UUID to be passed to TEE environment */
208 uuid_copy(uuid, &uuid_null);
213 * In Linux environment client UUID is based on UUIDv5.
215 * Determine client UUID with following semantics for 'name':
217 * For TEEC_LOGIN_USER:
220 * For TEEC_LOGIN_GROUP:
225 name = kzalloc(TEE_UUID_NS_NAME_SIZE, GFP_KERNEL);
229 switch (connection_method) {
230 case TEE_IOCTL_LOGIN_USER:
231 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "uid=%x",
233 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
239 case TEE_IOCTL_LOGIN_GROUP:
240 memcpy(&ns_grp, connection_data, sizeof(gid_t));
241 grp = make_kgid(current_user_ns(), ns_grp);
242 if (!gid_valid(grp) || !in_egroup_p(grp)) {
247 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "gid=%x",
249 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
260 rc = uuid_v5(uuid, &tee_client_uuid_ns, name, name_len);
266 EXPORT_SYMBOL_GPL(tee_session_calc_client_uuid);
268 static int tee_ioctl_version(struct tee_context *ctx,
269 struct tee_ioctl_version_data __user *uvers)
271 struct tee_ioctl_version_data vers;
273 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
275 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
276 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
278 if (copy_to_user(uvers, &vers, sizeof(vers)))
284 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
285 struct tee_ioctl_shm_alloc_data __user *udata)
288 struct tee_ioctl_shm_alloc_data data;
291 if (copy_from_user(&data, udata, sizeof(data)))
294 /* Currently no input flags are supported */
298 shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
303 data.flags = shm->flags;
304 data.size = shm->size;
306 if (copy_to_user(udata, &data, sizeof(data)))
309 ret = tee_shm_get_fd(shm);
312 * When user space closes the file descriptor the shared memory
313 * should be freed or if tee_shm_get_fd() failed then it will
314 * be freed immediately.
321 tee_ioctl_shm_register(struct tee_context *ctx,
322 struct tee_ioctl_shm_register_data __user *udata)
325 struct tee_ioctl_shm_register_data data;
328 if (copy_from_user(&data, udata, sizeof(data)))
331 /* Currently no input flags are supported */
335 shm = tee_shm_register(ctx, data.addr, data.length,
336 TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
341 data.flags = shm->flags;
342 data.length = shm->size;
344 if (copy_to_user(udata, &data, sizeof(data)))
347 ret = tee_shm_get_fd(shm);
349 * When user space closes the file descriptor the shared memory
350 * should be freed or if tee_shm_get_fd() failed then it will
351 * be freed immediately.
357 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
359 struct tee_ioctl_param __user *uparams)
363 for (n = 0; n < num_params; n++) {
365 struct tee_ioctl_param ip;
367 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
370 /* All unused attribute bits has to be zero */
371 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
374 params[n].attr = ip.attr;
375 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
376 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
377 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
379 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
380 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
381 params[n].u.value.a = ip.a;
382 params[n].u.value.b = ip.b;
383 params[n].u.value.c = ip.c;
385 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
386 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
387 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
389 * If a NULL pointer is passed to a TA in the TEE,
390 * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
391 * indicating a NULL memory reference.
393 if (ip.c != TEE_MEMREF_NULL) {
395 * If we fail to get a pointer to a shared
396 * memory object (and increase the ref count)
397 * from an identifier we return an error. All
398 * pointers that has been added in params have
399 * an increased ref count. It's the callers
400 * responibility to do tee_shm_put() on all
403 shm = tee_shm_get_from_id(ctx, ip.c);
408 * Ensure offset + size does not overflow
409 * offset and does not overflow the size of
410 * the referred shared memory object.
412 if ((ip.a + ip.b) < ip.a ||
413 (ip.a + ip.b) > shm->size) {
417 } else if (ctx->cap_memref_null) {
418 /* Pass NULL pointer to OP-TEE */
424 params[n].u.memref.shm_offs = ip.a;
425 params[n].u.memref.size = ip.b;
426 params[n].u.memref.shm = shm;
429 /* Unknown attribute */
436 static int params_to_user(struct tee_ioctl_param __user *uparams,
437 size_t num_params, struct tee_param *params)
441 for (n = 0; n < num_params; n++) {
442 struct tee_ioctl_param __user *up = uparams + n;
443 struct tee_param *p = params + n;
446 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
447 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
448 if (put_user(p->u.value.a, &up->a) ||
449 put_user(p->u.value.b, &up->b) ||
450 put_user(p->u.value.c, &up->c))
453 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
454 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
455 if (put_user((u64)p->u.memref.size, &up->b))
465 static int tee_ioctl_open_session(struct tee_context *ctx,
466 struct tee_ioctl_buf_data __user *ubuf)
470 struct tee_ioctl_buf_data buf;
471 struct tee_ioctl_open_session_arg __user *uarg;
472 struct tee_ioctl_open_session_arg arg;
473 struct tee_ioctl_param __user *uparams = NULL;
474 struct tee_param *params = NULL;
475 bool have_session = false;
477 if (!ctx->teedev->desc->ops->open_session)
480 if (copy_from_user(&buf, ubuf, sizeof(buf)))
483 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
484 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
487 uarg = u64_to_user_ptr(buf.buf_ptr);
488 if (copy_from_user(&arg, uarg, sizeof(arg)))
491 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
494 if (arg.num_params) {
495 params = kcalloc(arg.num_params, sizeof(struct tee_param),
499 uparams = uarg->params;
500 rc = params_from_user(ctx, params, arg.num_params, uparams);
505 if (arg.clnt_login >= TEE_IOCTL_LOGIN_REE_KERNEL_MIN &&
506 arg.clnt_login <= TEE_IOCTL_LOGIN_REE_KERNEL_MAX) {
507 pr_debug("login method not allowed for user-space client\n");
512 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
517 if (put_user(arg.session, &uarg->session) ||
518 put_user(arg.ret, &uarg->ret) ||
519 put_user(arg.ret_origin, &uarg->ret_origin)) {
523 rc = params_to_user(uparams, arg.num_params, params);
526 * If we've succeeded to open the session but failed to communicate
527 * it back to user space, close the session again to avoid leakage.
529 if (rc && have_session && ctx->teedev->desc->ops->close_session)
530 ctx->teedev->desc->ops->close_session(ctx, arg.session);
533 /* Decrease ref count for all valid shared memory pointers */
534 for (n = 0; n < arg.num_params; n++)
535 if (tee_param_is_memref(params + n) &&
536 params[n].u.memref.shm)
537 tee_shm_put(params[n].u.memref.shm);
544 static int tee_ioctl_invoke(struct tee_context *ctx,
545 struct tee_ioctl_buf_data __user *ubuf)
549 struct tee_ioctl_buf_data buf;
550 struct tee_ioctl_invoke_arg __user *uarg;
551 struct tee_ioctl_invoke_arg arg;
552 struct tee_ioctl_param __user *uparams = NULL;
553 struct tee_param *params = NULL;
555 if (!ctx->teedev->desc->ops->invoke_func)
558 if (copy_from_user(&buf, ubuf, sizeof(buf)))
561 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
562 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
565 uarg = u64_to_user_ptr(buf.buf_ptr);
566 if (copy_from_user(&arg, uarg, sizeof(arg)))
569 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
572 if (arg.num_params) {
573 params = kcalloc(arg.num_params, sizeof(struct tee_param),
577 uparams = uarg->params;
578 rc = params_from_user(ctx, params, arg.num_params, uparams);
583 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
587 if (put_user(arg.ret, &uarg->ret) ||
588 put_user(arg.ret_origin, &uarg->ret_origin)) {
592 rc = params_to_user(uparams, arg.num_params, params);
595 /* Decrease ref count for all valid shared memory pointers */
596 for (n = 0; n < arg.num_params; n++)
597 if (tee_param_is_memref(params + n) &&
598 params[n].u.memref.shm)
599 tee_shm_put(params[n].u.memref.shm);
605 static int tee_ioctl_cancel(struct tee_context *ctx,
606 struct tee_ioctl_cancel_arg __user *uarg)
608 struct tee_ioctl_cancel_arg arg;
610 if (!ctx->teedev->desc->ops->cancel_req)
613 if (copy_from_user(&arg, uarg, sizeof(arg)))
616 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
621 tee_ioctl_close_session(struct tee_context *ctx,
622 struct tee_ioctl_close_session_arg __user *uarg)
624 struct tee_ioctl_close_session_arg arg;
626 if (!ctx->teedev->desc->ops->close_session)
629 if (copy_from_user(&arg, uarg, sizeof(arg)))
632 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
635 static int params_to_supp(struct tee_context *ctx,
636 struct tee_ioctl_param __user *uparams,
637 size_t num_params, struct tee_param *params)
641 for (n = 0; n < num_params; n++) {
642 struct tee_ioctl_param ip;
643 struct tee_param *p = params + n;
646 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
647 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
648 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
653 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
654 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
655 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
656 ip.b = p->u.memref.size;
657 if (!p->u.memref.shm) {
659 ip.c = (u64)-1; /* invalid shm id */
662 ip.a = p->u.memref.shm_offs;
663 ip.c = p->u.memref.shm->id;
672 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
679 static int tee_ioctl_supp_recv(struct tee_context *ctx,
680 struct tee_ioctl_buf_data __user *ubuf)
683 struct tee_ioctl_buf_data buf;
684 struct tee_iocl_supp_recv_arg __user *uarg;
685 struct tee_param *params;
689 if (!ctx->teedev->desc->ops->supp_recv)
692 if (copy_from_user(&buf, ubuf, sizeof(buf)))
695 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
696 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
699 uarg = u64_to_user_ptr(buf.buf_ptr);
700 if (get_user(num_params, &uarg->num_params))
703 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
706 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
710 rc = params_from_user(ctx, params, num_params, uarg->params);
714 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
718 if (put_user(func, &uarg->func) ||
719 put_user(num_params, &uarg->num_params)) {
724 rc = params_to_supp(ctx, uarg->params, num_params, params);
730 static int params_from_supp(struct tee_param *params, size_t num_params,
731 struct tee_ioctl_param __user *uparams)
735 for (n = 0; n < num_params; n++) {
736 struct tee_param *p = params + n;
737 struct tee_ioctl_param ip;
739 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
742 /* All unused attribute bits has to be zero */
743 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
747 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
748 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
749 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
750 /* Only out and in/out values can be updated */
755 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
756 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
758 * Only the size of the memref can be updated.
759 * Since we don't have access to the original
760 * parameters here, only store the supplied size.
761 * The driver will copy the updated size into the
762 * original parameters.
764 p->u.memref.shm = NULL;
765 p->u.memref.shm_offs = 0;
766 p->u.memref.size = ip.b;
769 memset(&p->u, 0, sizeof(p->u));
776 static int tee_ioctl_supp_send(struct tee_context *ctx,
777 struct tee_ioctl_buf_data __user *ubuf)
780 struct tee_ioctl_buf_data buf;
781 struct tee_iocl_supp_send_arg __user *uarg;
782 struct tee_param *params;
786 /* Not valid for this driver */
787 if (!ctx->teedev->desc->ops->supp_send)
790 if (copy_from_user(&buf, ubuf, sizeof(buf)))
793 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
794 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
797 uarg = u64_to_user_ptr(buf.buf_ptr);
798 if (get_user(ret, &uarg->ret) ||
799 get_user(num_params, &uarg->num_params))
802 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
805 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
809 rc = params_from_supp(params, num_params, uarg->params);
813 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
819 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
821 struct tee_context *ctx = filp->private_data;
822 void __user *uarg = (void __user *)arg;
825 case TEE_IOC_VERSION:
826 return tee_ioctl_version(ctx, uarg);
827 case TEE_IOC_SHM_ALLOC:
828 return tee_ioctl_shm_alloc(ctx, uarg);
829 case TEE_IOC_SHM_REGISTER:
830 return tee_ioctl_shm_register(ctx, uarg);
831 case TEE_IOC_OPEN_SESSION:
832 return tee_ioctl_open_session(ctx, uarg);
834 return tee_ioctl_invoke(ctx, uarg);
836 return tee_ioctl_cancel(ctx, uarg);
837 case TEE_IOC_CLOSE_SESSION:
838 return tee_ioctl_close_session(ctx, uarg);
839 case TEE_IOC_SUPPL_RECV:
840 return tee_ioctl_supp_recv(ctx, uarg);
841 case TEE_IOC_SUPPL_SEND:
842 return tee_ioctl_supp_send(ctx, uarg);
848 static const struct file_operations tee_fops = {
849 .owner = THIS_MODULE,
851 .release = tee_release,
852 .unlocked_ioctl = tee_ioctl,
853 .compat_ioctl = compat_ptr_ioctl,
856 static void tee_release_device(struct device *dev)
858 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
860 spin_lock(&driver_lock);
861 clear_bit(teedev->id, dev_mask);
862 spin_unlock(&driver_lock);
863 mutex_destroy(&teedev->mutex);
864 idr_destroy(&teedev->idr);
869 * tee_device_alloc() - Allocate a new struct tee_device instance
870 * @teedesc: Descriptor for this driver
871 * @dev: Parent device for this device
872 * @pool: Shared memory pool, NULL if not used
873 * @driver_data: Private driver data for this device
875 * Allocates a new struct tee_device instance. The device is
876 * removed by tee_device_unregister().
878 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
880 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
882 struct tee_shm_pool *pool,
885 struct tee_device *teedev;
890 if (!teedesc || !teedesc->name || !teedesc->ops ||
891 !teedesc->ops->get_version || !teedesc->ops->open ||
892 !teedesc->ops->release || !pool)
893 return ERR_PTR(-EINVAL);
895 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
897 ret = ERR_PTR(-ENOMEM);
901 max_id = TEE_NUM_DEVICES / 2;
903 if (teedesc->flags & TEE_DESC_PRIVILEGED) {
904 offs = TEE_NUM_DEVICES / 2;
905 max_id = TEE_NUM_DEVICES;
908 spin_lock(&driver_lock);
909 teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
910 if (teedev->id < max_id)
911 set_bit(teedev->id, dev_mask);
912 spin_unlock(&driver_lock);
914 if (teedev->id >= max_id) {
915 ret = ERR_PTR(-ENOMEM);
919 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
920 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
923 teedev->dev.class = tee_class;
924 teedev->dev.release = tee_release_device;
925 teedev->dev.parent = dev;
927 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
929 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
935 cdev_init(&teedev->cdev, &tee_fops);
936 teedev->cdev.owner = teedesc->owner;
938 dev_set_drvdata(&teedev->dev, driver_data);
939 device_initialize(&teedev->dev);
941 /* 1 as tee_device_unregister() does one final tee_device_put() */
942 teedev->num_users = 1;
943 init_completion(&teedev->c_no_users);
944 mutex_init(&teedev->mutex);
945 idr_init(&teedev->idr);
947 teedev->desc = teedesc;
952 unregister_chrdev_region(teedev->dev.devt, 1);
954 pr_err("could not register %s driver\n",
955 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
956 if (teedev && teedev->id < TEE_NUM_DEVICES) {
957 spin_lock(&driver_lock);
958 clear_bit(teedev->id, dev_mask);
959 spin_unlock(&driver_lock);
964 EXPORT_SYMBOL_GPL(tee_device_alloc);
966 static ssize_t implementation_id_show(struct device *dev,
967 struct device_attribute *attr, char *buf)
969 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
970 struct tee_ioctl_version_data vers;
972 teedev->desc->ops->get_version(teedev, &vers);
973 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
975 static DEVICE_ATTR_RO(implementation_id);
977 static struct attribute *tee_dev_attrs[] = {
978 &dev_attr_implementation_id.attr,
982 ATTRIBUTE_GROUPS(tee_dev);
985 * tee_device_register() - Registers a TEE device
986 * @teedev: Device to register
988 * tee_device_unregister() need to be called to remove the @teedev if
989 * this function fails.
991 * @returns < 0 on failure
993 int tee_device_register(struct tee_device *teedev)
997 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
998 dev_err(&teedev->dev, "attempt to register twice\n");
1002 teedev->dev.groups = tee_dev_groups;
1004 rc = cdev_device_add(&teedev->cdev, &teedev->dev);
1006 dev_err(&teedev->dev,
1007 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
1008 teedev->name, MAJOR(teedev->dev.devt),
1009 MINOR(teedev->dev.devt), rc);
1013 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
1016 EXPORT_SYMBOL_GPL(tee_device_register);
1018 void tee_device_put(struct tee_device *teedev)
1020 mutex_lock(&teedev->mutex);
1021 /* Shouldn't put in this state */
1022 if (!WARN_ON(!teedev->desc)) {
1023 teedev->num_users--;
1024 if (!teedev->num_users) {
1025 teedev->desc = NULL;
1026 complete(&teedev->c_no_users);
1029 mutex_unlock(&teedev->mutex);
1032 bool tee_device_get(struct tee_device *teedev)
1034 mutex_lock(&teedev->mutex);
1035 if (!teedev->desc) {
1036 mutex_unlock(&teedev->mutex);
1039 teedev->num_users++;
1040 mutex_unlock(&teedev->mutex);
1045 * tee_device_unregister() - Removes a TEE device
1046 * @teedev: Device to unregister
1048 * This function should be called to remove the @teedev even if
1049 * tee_device_register() hasn't been called yet. Does nothing if
1052 void tee_device_unregister(struct tee_device *teedev)
1057 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED)
1058 cdev_device_del(&teedev->cdev, &teedev->dev);
1060 tee_device_put(teedev);
1061 wait_for_completion(&teedev->c_no_users);
1064 * No need to take a mutex any longer now since teedev->desc was
1065 * set to NULL before teedev->c_no_users was completed.
1068 teedev->pool = NULL;
1070 put_device(&teedev->dev);
1072 EXPORT_SYMBOL_GPL(tee_device_unregister);
1075 * tee_get_drvdata() - Return driver_data pointer
1076 * @teedev: Device containing the driver_data pointer
1077 * @returns the driver_data pointer supplied to tee_register().
1079 void *tee_get_drvdata(struct tee_device *teedev)
1081 return dev_get_drvdata(&teedev->dev);
1083 EXPORT_SYMBOL_GPL(tee_get_drvdata);
1085 struct match_dev_data {
1086 struct tee_ioctl_version_data *vers;
1088 int (*match)(struct tee_ioctl_version_data *, const void *);
1091 static int match_dev(struct device *dev, const void *data)
1093 const struct match_dev_data *match_data = data;
1094 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
1096 teedev->desc->ops->get_version(teedev, match_data->vers);
1097 return match_data->match(match_data->vers, match_data->data);
1100 struct tee_context *
1101 tee_client_open_context(struct tee_context *start,
1102 int (*match)(struct tee_ioctl_version_data *,
1104 const void *data, struct tee_ioctl_version_data *vers)
1106 struct device *dev = NULL;
1107 struct device *put_dev = NULL;
1108 struct tee_context *ctx = NULL;
1109 struct tee_ioctl_version_data v;
1110 struct match_dev_data match_data = { vers ? vers : &v, data, match };
1113 dev = &start->teedev->dev;
1116 dev = class_find_device(tee_class, dev, &match_data, match_dev);
1118 ctx = ERR_PTR(-ENOENT);
1122 put_device(put_dev);
1125 ctx = teedev_open(container_of(dev, struct tee_device, dev));
1126 } while (IS_ERR(ctx) && PTR_ERR(ctx) != -ENOMEM);
1128 put_device(put_dev);
1130 * Default behaviour for in kernel client is to not wait for
1131 * tee-supplicant if not present for any requests in this context.
1132 * Also this flag could be configured again before call to
1133 * tee_client_open_session() if any in kernel client requires
1134 * different behaviour.
1137 ctx->supp_nowait = true;
1141 EXPORT_SYMBOL_GPL(tee_client_open_context);
1143 void tee_client_close_context(struct tee_context *ctx)
1145 teedev_close_context(ctx);
1147 EXPORT_SYMBOL_GPL(tee_client_close_context);
1149 void tee_client_get_version(struct tee_context *ctx,
1150 struct tee_ioctl_version_data *vers)
1152 ctx->teedev->desc->ops->get_version(ctx->teedev, vers);
1154 EXPORT_SYMBOL_GPL(tee_client_get_version);
1156 int tee_client_open_session(struct tee_context *ctx,
1157 struct tee_ioctl_open_session_arg *arg,
1158 struct tee_param *param)
1160 if (!ctx->teedev->desc->ops->open_session)
1162 return ctx->teedev->desc->ops->open_session(ctx, arg, param);
1164 EXPORT_SYMBOL_GPL(tee_client_open_session);
1166 int tee_client_close_session(struct tee_context *ctx, u32 session)
1168 if (!ctx->teedev->desc->ops->close_session)
1170 return ctx->teedev->desc->ops->close_session(ctx, session);
1172 EXPORT_SYMBOL_GPL(tee_client_close_session);
1174 int tee_client_invoke_func(struct tee_context *ctx,
1175 struct tee_ioctl_invoke_arg *arg,
1176 struct tee_param *param)
1178 if (!ctx->teedev->desc->ops->invoke_func)
1180 return ctx->teedev->desc->ops->invoke_func(ctx, arg, param);
1182 EXPORT_SYMBOL_GPL(tee_client_invoke_func);
1184 int tee_client_cancel_req(struct tee_context *ctx,
1185 struct tee_ioctl_cancel_arg *arg)
1187 if (!ctx->teedev->desc->ops->cancel_req)
1189 return ctx->teedev->desc->ops->cancel_req(ctx, arg->cancel_id,
1193 static int tee_client_device_match(struct device *dev,
1194 struct device_driver *drv)
1196 const struct tee_client_device_id *id_table;
1197 struct tee_client_device *tee_device;
1199 id_table = to_tee_client_driver(drv)->id_table;
1200 tee_device = to_tee_client_device(dev);
1202 while (!uuid_is_null(&id_table->uuid)) {
1203 if (uuid_equal(&tee_device->id.uuid, &id_table->uuid))
1211 static int tee_client_device_uevent(struct device *dev,
1212 struct kobj_uevent_env *env)
1214 uuid_t *dev_id = &to_tee_client_device(dev)->id.uuid;
1216 return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
1219 struct bus_type tee_bus_type = {
1221 .match = tee_client_device_match,
1222 .uevent = tee_client_device_uevent,
1224 EXPORT_SYMBOL_GPL(tee_bus_type);
1226 static int __init tee_init(void)
1230 tee_class = class_create(THIS_MODULE, "tee");
1231 if (IS_ERR(tee_class)) {
1232 pr_err("couldn't create class\n");
1233 return PTR_ERR(tee_class);
1236 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
1238 pr_err("failed to allocate char dev region\n");
1239 goto out_unreg_class;
1242 rc = bus_register(&tee_bus_type);
1244 pr_err("failed to register tee bus\n");
1245 goto out_unreg_chrdev;
1251 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1253 class_destroy(tee_class);
1259 static void __exit tee_exit(void)
1261 bus_unregister(&tee_bus_type);
1262 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1263 class_destroy(tee_class);
1267 subsys_initcall(tee_init);
1268 module_exit(tee_exit);
1270 MODULE_AUTHOR("Linaro");
1271 MODULE_DESCRIPTION("TEE Driver");
1272 MODULE_VERSION("1.0");
1273 MODULE_LICENSE("GPL v2");