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 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);
73 EXPORT_SYMBOL_GPL(teedev_open);
75 void teedev_ctx_get(struct tee_context *ctx)
80 kref_get(&ctx->refcount);
83 static void teedev_ctx_release(struct kref *ref)
85 struct tee_context *ctx = container_of(ref, struct tee_context,
87 ctx->releasing = true;
88 ctx->teedev->desc->ops->release(ctx);
92 void teedev_ctx_put(struct tee_context *ctx)
97 kref_put(&ctx->refcount, teedev_ctx_release);
100 void teedev_close_context(struct tee_context *ctx)
102 struct tee_device *teedev = ctx->teedev;
105 tee_device_put(teedev);
107 EXPORT_SYMBOL_GPL(teedev_close_context);
109 static int tee_open(struct inode *inode, struct file *filp)
111 struct tee_context *ctx;
113 ctx = teedev_open(container_of(inode->i_cdev, struct tee_device, cdev));
118 * Default user-space behaviour is to wait for tee-supplicant
119 * if not present for any requests in this context.
121 ctx->supp_nowait = false;
122 filp->private_data = ctx;
126 static int tee_release(struct inode *inode, struct file *filp)
128 teedev_close_context(filp->private_data);
133 * uuid_v5() - Calculate UUIDv5
134 * @uuid: Resulting UUID
135 * @ns: Name space ID for UUIDv5 function
136 * @name: Name for UUIDv5 function
137 * @size: Size of name
139 * UUIDv5 is specific in RFC 4122.
141 * This implements section (for SHA-1):
142 * 4.3. Algorithm for Creating a Name-Based UUID
144 static int uuid_v5(uuid_t *uuid, const uuid_t *ns, const void *name,
147 unsigned char hash[SHA1_DIGEST_SIZE];
148 struct crypto_shash *shash = NULL;
149 struct shash_desc *desc = NULL;
152 shash = crypto_alloc_shash("sha1", 0, 0);
155 pr_err("shash(sha1) allocation failed\n");
159 desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
168 rc = crypto_shash_init(desc);
172 rc = crypto_shash_update(desc, (const u8 *)ns, sizeof(*ns));
176 rc = crypto_shash_update(desc, (const u8 *)name, size);
180 rc = crypto_shash_final(desc, hash);
184 memcpy(uuid->b, hash, UUID_SIZE);
186 /* Tag for version 5 */
187 uuid->b[6] = (hash[6] & 0x0F) | 0x50;
188 uuid->b[8] = (hash[8] & 0x3F) | 0x80;
194 crypto_free_shash(shash);
198 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
199 const u8 connection_data[TEE_IOCTL_UUID_LEN])
201 gid_t ns_grp = (gid_t)-1;
202 kgid_t grp = INVALID_GID;
207 if (connection_method == TEE_IOCTL_LOGIN_PUBLIC ||
208 connection_method == TEE_IOCTL_LOGIN_REE_KERNEL) {
209 /* Nil UUID to be passed to TEE environment */
210 uuid_copy(uuid, &uuid_null);
215 * In Linux environment client UUID is based on UUIDv5.
217 * Determine client UUID with following semantics for 'name':
219 * For TEEC_LOGIN_USER:
222 * For TEEC_LOGIN_GROUP:
227 name = kzalloc(TEE_UUID_NS_NAME_SIZE, GFP_KERNEL);
231 switch (connection_method) {
232 case TEE_IOCTL_LOGIN_USER:
233 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "uid=%x",
235 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
241 case TEE_IOCTL_LOGIN_GROUP:
242 memcpy(&ns_grp, connection_data, sizeof(gid_t));
243 grp = make_kgid(current_user_ns(), ns_grp);
244 if (!gid_valid(grp) || !in_egroup_p(grp)) {
249 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "gid=%x",
251 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
262 rc = uuid_v5(uuid, &tee_client_uuid_ns, name, name_len);
268 EXPORT_SYMBOL_GPL(tee_session_calc_client_uuid);
270 static int tee_ioctl_version(struct tee_context *ctx,
271 struct tee_ioctl_version_data __user *uvers)
273 struct tee_ioctl_version_data vers;
275 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
277 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
278 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
280 if (copy_to_user(uvers, &vers, sizeof(vers)))
286 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
287 struct tee_ioctl_shm_alloc_data __user *udata)
290 struct tee_ioctl_shm_alloc_data data;
293 if (copy_from_user(&data, udata, sizeof(data)))
296 /* Currently no input flags are supported */
300 shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
305 data.flags = shm->flags;
306 data.size = shm->size;
308 if (copy_to_user(udata, &data, sizeof(data)))
311 ret = tee_shm_get_fd(shm);
314 * When user space closes the file descriptor the shared memory
315 * should be freed or if tee_shm_get_fd() failed then it will
316 * be freed immediately.
323 tee_ioctl_shm_register(struct tee_context *ctx,
324 struct tee_ioctl_shm_register_data __user *udata)
327 struct tee_ioctl_shm_register_data data;
330 if (copy_from_user(&data, udata, sizeof(data)))
333 /* Currently no input flags are supported */
337 if (!access_ok((void __user *)(unsigned long)data.addr, data.length))
340 shm = tee_shm_register(ctx, data.addr, data.length,
341 TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
346 data.flags = shm->flags;
347 data.length = shm->size;
349 if (copy_to_user(udata, &data, sizeof(data)))
352 ret = tee_shm_get_fd(shm);
354 * When user space closes the file descriptor the shared memory
355 * should be freed or if tee_shm_get_fd() failed then it will
356 * be freed immediately.
362 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
364 struct tee_ioctl_param __user *uparams)
368 for (n = 0; n < num_params; n++) {
370 struct tee_ioctl_param ip;
372 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
375 /* All unused attribute bits has to be zero */
376 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
379 params[n].attr = ip.attr;
380 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
381 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
382 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
384 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
385 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
386 params[n].u.value.a = ip.a;
387 params[n].u.value.b = ip.b;
388 params[n].u.value.c = ip.c;
390 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
391 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
392 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
394 * If a NULL pointer is passed to a TA in the TEE,
395 * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
396 * indicating a NULL memory reference.
398 if (ip.c != TEE_MEMREF_NULL) {
400 * If we fail to get a pointer to a shared
401 * memory object (and increase the ref count)
402 * from an identifier we return an error. All
403 * pointers that has been added in params have
404 * an increased ref count. It's the callers
405 * responibility to do tee_shm_put() on all
408 shm = tee_shm_get_from_id(ctx, ip.c);
413 * Ensure offset + size does not overflow
414 * offset and does not overflow the size of
415 * the referred shared memory object.
417 if ((ip.a + ip.b) < ip.a ||
418 (ip.a + ip.b) > shm->size) {
422 } else if (ctx->cap_memref_null) {
423 /* Pass NULL pointer to OP-TEE */
429 params[n].u.memref.shm_offs = ip.a;
430 params[n].u.memref.size = ip.b;
431 params[n].u.memref.shm = shm;
434 /* Unknown attribute */
441 static int params_to_user(struct tee_ioctl_param __user *uparams,
442 size_t num_params, struct tee_param *params)
446 for (n = 0; n < num_params; n++) {
447 struct tee_ioctl_param __user *up = uparams + n;
448 struct tee_param *p = params + n;
451 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
452 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
453 if (put_user(p->u.value.a, &up->a) ||
454 put_user(p->u.value.b, &up->b) ||
455 put_user(p->u.value.c, &up->c))
458 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
459 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
460 if (put_user((u64)p->u.memref.size, &up->b))
470 static int tee_ioctl_open_session(struct tee_context *ctx,
471 struct tee_ioctl_buf_data __user *ubuf)
475 struct tee_ioctl_buf_data buf;
476 struct tee_ioctl_open_session_arg __user *uarg;
477 struct tee_ioctl_open_session_arg arg;
478 struct tee_ioctl_param __user *uparams = NULL;
479 struct tee_param *params = NULL;
480 bool have_session = false;
482 if (!ctx->teedev->desc->ops->open_session)
485 if (copy_from_user(&buf, ubuf, sizeof(buf)))
488 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
489 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
492 uarg = u64_to_user_ptr(buf.buf_ptr);
493 if (copy_from_user(&arg, uarg, sizeof(arg)))
496 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
499 if (arg.num_params) {
500 params = kcalloc(arg.num_params, sizeof(struct tee_param),
504 uparams = uarg->params;
505 rc = params_from_user(ctx, params, arg.num_params, uparams);
510 if (arg.clnt_login >= TEE_IOCTL_LOGIN_REE_KERNEL_MIN &&
511 arg.clnt_login <= TEE_IOCTL_LOGIN_REE_KERNEL_MAX) {
512 pr_debug("login method not allowed for user-space client\n");
517 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
522 if (put_user(arg.session, &uarg->session) ||
523 put_user(arg.ret, &uarg->ret) ||
524 put_user(arg.ret_origin, &uarg->ret_origin)) {
528 rc = params_to_user(uparams, arg.num_params, params);
531 * If we've succeeded to open the session but failed to communicate
532 * it back to user space, close the session again to avoid leakage.
534 if (rc && have_session && ctx->teedev->desc->ops->close_session)
535 ctx->teedev->desc->ops->close_session(ctx, arg.session);
538 /* Decrease ref count for all valid shared memory pointers */
539 for (n = 0; n < arg.num_params; n++)
540 if (tee_param_is_memref(params + n) &&
541 params[n].u.memref.shm)
542 tee_shm_put(params[n].u.memref.shm);
549 static int tee_ioctl_invoke(struct tee_context *ctx,
550 struct tee_ioctl_buf_data __user *ubuf)
554 struct tee_ioctl_buf_data buf;
555 struct tee_ioctl_invoke_arg __user *uarg;
556 struct tee_ioctl_invoke_arg arg;
557 struct tee_ioctl_param __user *uparams = NULL;
558 struct tee_param *params = NULL;
560 if (!ctx->teedev->desc->ops->invoke_func)
563 if (copy_from_user(&buf, ubuf, sizeof(buf)))
566 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
567 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
570 uarg = u64_to_user_ptr(buf.buf_ptr);
571 if (copy_from_user(&arg, uarg, sizeof(arg)))
574 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
577 if (arg.num_params) {
578 params = kcalloc(arg.num_params, sizeof(struct tee_param),
582 uparams = uarg->params;
583 rc = params_from_user(ctx, params, arg.num_params, uparams);
588 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
592 if (put_user(arg.ret, &uarg->ret) ||
593 put_user(arg.ret_origin, &uarg->ret_origin)) {
597 rc = params_to_user(uparams, arg.num_params, params);
600 /* Decrease ref count for all valid shared memory pointers */
601 for (n = 0; n < arg.num_params; n++)
602 if (tee_param_is_memref(params + n) &&
603 params[n].u.memref.shm)
604 tee_shm_put(params[n].u.memref.shm);
610 static int tee_ioctl_cancel(struct tee_context *ctx,
611 struct tee_ioctl_cancel_arg __user *uarg)
613 struct tee_ioctl_cancel_arg arg;
615 if (!ctx->teedev->desc->ops->cancel_req)
618 if (copy_from_user(&arg, uarg, sizeof(arg)))
621 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
626 tee_ioctl_close_session(struct tee_context *ctx,
627 struct tee_ioctl_close_session_arg __user *uarg)
629 struct tee_ioctl_close_session_arg arg;
631 if (!ctx->teedev->desc->ops->close_session)
634 if (copy_from_user(&arg, uarg, sizeof(arg)))
637 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
640 static int params_to_supp(struct tee_context *ctx,
641 struct tee_ioctl_param __user *uparams,
642 size_t num_params, struct tee_param *params)
646 for (n = 0; n < num_params; n++) {
647 struct tee_ioctl_param ip;
648 struct tee_param *p = params + n;
651 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
652 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
653 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
658 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
659 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
660 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
661 ip.b = p->u.memref.size;
662 if (!p->u.memref.shm) {
664 ip.c = (u64)-1; /* invalid shm id */
667 ip.a = p->u.memref.shm_offs;
668 ip.c = p->u.memref.shm->id;
677 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
684 static int tee_ioctl_supp_recv(struct tee_context *ctx,
685 struct tee_ioctl_buf_data __user *ubuf)
688 struct tee_ioctl_buf_data buf;
689 struct tee_iocl_supp_recv_arg __user *uarg;
690 struct tee_param *params;
694 if (!ctx->teedev->desc->ops->supp_recv)
697 if (copy_from_user(&buf, ubuf, sizeof(buf)))
700 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
701 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
704 uarg = u64_to_user_ptr(buf.buf_ptr);
705 if (get_user(num_params, &uarg->num_params))
708 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
711 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
715 rc = params_from_user(ctx, params, num_params, uarg->params);
719 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
723 if (put_user(func, &uarg->func) ||
724 put_user(num_params, &uarg->num_params)) {
729 rc = params_to_supp(ctx, uarg->params, num_params, params);
735 static int params_from_supp(struct tee_param *params, size_t num_params,
736 struct tee_ioctl_param __user *uparams)
740 for (n = 0; n < num_params; n++) {
741 struct tee_param *p = params + n;
742 struct tee_ioctl_param ip;
744 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
747 /* All unused attribute bits has to be zero */
748 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
752 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
753 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
754 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
755 /* Only out and in/out values can be updated */
760 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
761 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
763 * Only the size of the memref can be updated.
764 * Since we don't have access to the original
765 * parameters here, only store the supplied size.
766 * The driver will copy the updated size into the
767 * original parameters.
769 p->u.memref.shm = NULL;
770 p->u.memref.shm_offs = 0;
771 p->u.memref.size = ip.b;
774 memset(&p->u, 0, sizeof(p->u));
781 static int tee_ioctl_supp_send(struct tee_context *ctx,
782 struct tee_ioctl_buf_data __user *ubuf)
785 struct tee_ioctl_buf_data buf;
786 struct tee_iocl_supp_send_arg __user *uarg;
787 struct tee_param *params;
791 /* Not valid for this driver */
792 if (!ctx->teedev->desc->ops->supp_send)
795 if (copy_from_user(&buf, ubuf, sizeof(buf)))
798 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
799 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
802 uarg = u64_to_user_ptr(buf.buf_ptr);
803 if (get_user(ret, &uarg->ret) ||
804 get_user(num_params, &uarg->num_params))
807 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
810 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
814 rc = params_from_supp(params, num_params, uarg->params);
818 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
824 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
826 struct tee_context *ctx = filp->private_data;
827 void __user *uarg = (void __user *)arg;
830 case TEE_IOC_VERSION:
831 return tee_ioctl_version(ctx, uarg);
832 case TEE_IOC_SHM_ALLOC:
833 return tee_ioctl_shm_alloc(ctx, uarg);
834 case TEE_IOC_SHM_REGISTER:
835 return tee_ioctl_shm_register(ctx, uarg);
836 case TEE_IOC_OPEN_SESSION:
837 return tee_ioctl_open_session(ctx, uarg);
839 return tee_ioctl_invoke(ctx, uarg);
841 return tee_ioctl_cancel(ctx, uarg);
842 case TEE_IOC_CLOSE_SESSION:
843 return tee_ioctl_close_session(ctx, uarg);
844 case TEE_IOC_SUPPL_RECV:
845 return tee_ioctl_supp_recv(ctx, uarg);
846 case TEE_IOC_SUPPL_SEND:
847 return tee_ioctl_supp_send(ctx, uarg);
853 static const struct file_operations tee_fops = {
854 .owner = THIS_MODULE,
856 .release = tee_release,
857 .unlocked_ioctl = tee_ioctl,
858 .compat_ioctl = compat_ptr_ioctl,
861 static void tee_release_device(struct device *dev)
863 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
865 spin_lock(&driver_lock);
866 clear_bit(teedev->id, dev_mask);
867 spin_unlock(&driver_lock);
868 mutex_destroy(&teedev->mutex);
869 idr_destroy(&teedev->idr);
874 * tee_device_alloc() - Allocate a new struct tee_device instance
875 * @teedesc: Descriptor for this driver
876 * @dev: Parent device for this device
877 * @pool: Shared memory pool, NULL if not used
878 * @driver_data: Private driver data for this device
880 * Allocates a new struct tee_device instance. The device is
881 * removed by tee_device_unregister().
883 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
885 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
887 struct tee_shm_pool *pool,
890 struct tee_device *teedev;
895 if (!teedesc || !teedesc->name || !teedesc->ops ||
896 !teedesc->ops->get_version || !teedesc->ops->open ||
897 !teedesc->ops->release || !pool)
898 return ERR_PTR(-EINVAL);
900 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
902 ret = ERR_PTR(-ENOMEM);
906 max_id = TEE_NUM_DEVICES / 2;
908 if (teedesc->flags & TEE_DESC_PRIVILEGED) {
909 offs = TEE_NUM_DEVICES / 2;
910 max_id = TEE_NUM_DEVICES;
913 spin_lock(&driver_lock);
914 teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
915 if (teedev->id < max_id)
916 set_bit(teedev->id, dev_mask);
917 spin_unlock(&driver_lock);
919 if (teedev->id >= max_id) {
920 ret = ERR_PTR(-ENOMEM);
924 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
925 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
928 teedev->dev.class = tee_class;
929 teedev->dev.release = tee_release_device;
930 teedev->dev.parent = dev;
932 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
934 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
940 cdev_init(&teedev->cdev, &tee_fops);
941 teedev->cdev.owner = teedesc->owner;
943 dev_set_drvdata(&teedev->dev, driver_data);
944 device_initialize(&teedev->dev);
946 /* 1 as tee_device_unregister() does one final tee_device_put() */
947 teedev->num_users = 1;
948 init_completion(&teedev->c_no_users);
949 mutex_init(&teedev->mutex);
950 idr_init(&teedev->idr);
952 teedev->desc = teedesc;
957 unregister_chrdev_region(teedev->dev.devt, 1);
959 pr_err("could not register %s driver\n",
960 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
961 if (teedev && teedev->id < TEE_NUM_DEVICES) {
962 spin_lock(&driver_lock);
963 clear_bit(teedev->id, dev_mask);
964 spin_unlock(&driver_lock);
969 EXPORT_SYMBOL_GPL(tee_device_alloc);
971 static ssize_t implementation_id_show(struct device *dev,
972 struct device_attribute *attr, char *buf)
974 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
975 struct tee_ioctl_version_data vers;
977 teedev->desc->ops->get_version(teedev, &vers);
978 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
980 static DEVICE_ATTR_RO(implementation_id);
982 static struct attribute *tee_dev_attrs[] = {
983 &dev_attr_implementation_id.attr,
987 ATTRIBUTE_GROUPS(tee_dev);
990 * tee_device_register() - Registers a TEE device
991 * @teedev: Device to register
993 * tee_device_unregister() need to be called to remove the @teedev if
994 * this function fails.
996 * @returns < 0 on failure
998 int tee_device_register(struct tee_device *teedev)
1002 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
1003 dev_err(&teedev->dev, "attempt to register twice\n");
1007 teedev->dev.groups = tee_dev_groups;
1009 rc = cdev_device_add(&teedev->cdev, &teedev->dev);
1011 dev_err(&teedev->dev,
1012 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
1013 teedev->name, MAJOR(teedev->dev.devt),
1014 MINOR(teedev->dev.devt), rc);
1018 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
1021 EXPORT_SYMBOL_GPL(tee_device_register);
1023 void tee_device_put(struct tee_device *teedev)
1025 mutex_lock(&teedev->mutex);
1026 /* Shouldn't put in this state */
1027 if (!WARN_ON(!teedev->desc)) {
1028 teedev->num_users--;
1029 if (!teedev->num_users) {
1030 teedev->desc = NULL;
1031 complete(&teedev->c_no_users);
1034 mutex_unlock(&teedev->mutex);
1037 bool tee_device_get(struct tee_device *teedev)
1039 mutex_lock(&teedev->mutex);
1040 if (!teedev->desc) {
1041 mutex_unlock(&teedev->mutex);
1044 teedev->num_users++;
1045 mutex_unlock(&teedev->mutex);
1050 * tee_device_unregister() - Removes a TEE device
1051 * @teedev: Device to unregister
1053 * This function should be called to remove the @teedev even if
1054 * tee_device_register() hasn't been called yet. Does nothing if
1057 void tee_device_unregister(struct tee_device *teedev)
1062 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED)
1063 cdev_device_del(&teedev->cdev, &teedev->dev);
1065 tee_device_put(teedev);
1066 wait_for_completion(&teedev->c_no_users);
1069 * No need to take a mutex any longer now since teedev->desc was
1070 * set to NULL before teedev->c_no_users was completed.
1073 teedev->pool = NULL;
1075 put_device(&teedev->dev);
1077 EXPORT_SYMBOL_GPL(tee_device_unregister);
1080 * tee_get_drvdata() - Return driver_data pointer
1081 * @teedev: Device containing the driver_data pointer
1082 * @returns the driver_data pointer supplied to tee_register().
1084 void *tee_get_drvdata(struct tee_device *teedev)
1086 return dev_get_drvdata(&teedev->dev);
1088 EXPORT_SYMBOL_GPL(tee_get_drvdata);
1090 struct match_dev_data {
1091 struct tee_ioctl_version_data *vers;
1093 int (*match)(struct tee_ioctl_version_data *, const void *);
1096 static int match_dev(struct device *dev, const void *data)
1098 const struct match_dev_data *match_data = data;
1099 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
1101 teedev->desc->ops->get_version(teedev, match_data->vers);
1102 return match_data->match(match_data->vers, match_data->data);
1105 struct tee_context *
1106 tee_client_open_context(struct tee_context *start,
1107 int (*match)(struct tee_ioctl_version_data *,
1109 const void *data, struct tee_ioctl_version_data *vers)
1111 struct device *dev = NULL;
1112 struct device *put_dev = NULL;
1113 struct tee_context *ctx = NULL;
1114 struct tee_ioctl_version_data v;
1115 struct match_dev_data match_data = { vers ? vers : &v, data, match };
1118 dev = &start->teedev->dev;
1121 dev = class_find_device(tee_class, dev, &match_data, match_dev);
1123 ctx = ERR_PTR(-ENOENT);
1127 put_device(put_dev);
1130 ctx = teedev_open(container_of(dev, struct tee_device, dev));
1131 } while (IS_ERR(ctx) && PTR_ERR(ctx) != -ENOMEM);
1133 put_device(put_dev);
1135 * Default behaviour for in kernel client is to not wait for
1136 * tee-supplicant if not present for any requests in this context.
1137 * Also this flag could be configured again before call to
1138 * tee_client_open_session() if any in kernel client requires
1139 * different behaviour.
1142 ctx->supp_nowait = true;
1146 EXPORT_SYMBOL_GPL(tee_client_open_context);
1148 void tee_client_close_context(struct tee_context *ctx)
1150 teedev_close_context(ctx);
1152 EXPORT_SYMBOL_GPL(tee_client_close_context);
1154 void tee_client_get_version(struct tee_context *ctx,
1155 struct tee_ioctl_version_data *vers)
1157 ctx->teedev->desc->ops->get_version(ctx->teedev, vers);
1159 EXPORT_SYMBOL_GPL(tee_client_get_version);
1161 int tee_client_open_session(struct tee_context *ctx,
1162 struct tee_ioctl_open_session_arg *arg,
1163 struct tee_param *param)
1165 if (!ctx->teedev->desc->ops->open_session)
1167 return ctx->teedev->desc->ops->open_session(ctx, arg, param);
1169 EXPORT_SYMBOL_GPL(tee_client_open_session);
1171 int tee_client_close_session(struct tee_context *ctx, u32 session)
1173 if (!ctx->teedev->desc->ops->close_session)
1175 return ctx->teedev->desc->ops->close_session(ctx, session);
1177 EXPORT_SYMBOL_GPL(tee_client_close_session);
1179 int tee_client_invoke_func(struct tee_context *ctx,
1180 struct tee_ioctl_invoke_arg *arg,
1181 struct tee_param *param)
1183 if (!ctx->teedev->desc->ops->invoke_func)
1185 return ctx->teedev->desc->ops->invoke_func(ctx, arg, param);
1187 EXPORT_SYMBOL_GPL(tee_client_invoke_func);
1189 int tee_client_cancel_req(struct tee_context *ctx,
1190 struct tee_ioctl_cancel_arg *arg)
1192 if (!ctx->teedev->desc->ops->cancel_req)
1194 return ctx->teedev->desc->ops->cancel_req(ctx, arg->cancel_id,
1198 static int tee_client_device_match(struct device *dev,
1199 struct device_driver *drv)
1201 const struct tee_client_device_id *id_table;
1202 struct tee_client_device *tee_device;
1204 id_table = to_tee_client_driver(drv)->id_table;
1205 tee_device = to_tee_client_device(dev);
1207 while (!uuid_is_null(&id_table->uuid)) {
1208 if (uuid_equal(&tee_device->id.uuid, &id_table->uuid))
1216 static int tee_client_device_uevent(struct device *dev,
1217 struct kobj_uevent_env *env)
1219 uuid_t *dev_id = &to_tee_client_device(dev)->id.uuid;
1221 return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
1224 struct bus_type tee_bus_type = {
1226 .match = tee_client_device_match,
1227 .uevent = tee_client_device_uevent,
1229 EXPORT_SYMBOL_GPL(tee_bus_type);
1231 static int __init tee_init(void)
1235 tee_class = class_create(THIS_MODULE, "tee");
1236 if (IS_ERR(tee_class)) {
1237 pr_err("couldn't create class\n");
1238 return PTR_ERR(tee_class);
1241 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
1243 pr_err("failed to allocate char dev region\n");
1244 goto out_unreg_class;
1247 rc = bus_register(&tee_bus_type);
1249 pr_err("failed to register tee bus\n");
1250 goto out_unreg_chrdev;
1256 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1258 class_destroy(tee_class);
1264 static void __exit tee_exit(void)
1266 bus_unregister(&tee_bus_type);
1267 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1268 class_destroy(tee_class);
1272 subsys_initcall(tee_init);
1273 module_exit(tee_exit);
1275 MODULE_AUTHOR("Linaro");
1276 MODULE_DESCRIPTION("TEE Driver");
1277 MODULE_VERSION("1.0");
1278 MODULE_LICENSE("GPL v2");