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 const struct class tee_class = {
47 static dev_t tee_devt;
49 struct tee_context *teedev_open(struct tee_device *teedev)
52 struct tee_context *ctx;
54 if (!tee_device_get(teedev))
55 return ERR_PTR(-EINVAL);
57 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
63 kref_init(&ctx->refcount);
65 rc = teedev->desc->ops->open(ctx);
72 tee_device_put(teedev);
76 EXPORT_SYMBOL_GPL(teedev_open);
78 void teedev_ctx_get(struct tee_context *ctx)
83 kref_get(&ctx->refcount);
86 static void teedev_ctx_release(struct kref *ref)
88 struct tee_context *ctx = container_of(ref, struct tee_context,
90 ctx->releasing = true;
91 ctx->teedev->desc->ops->release(ctx);
95 void teedev_ctx_put(struct tee_context *ctx)
100 kref_put(&ctx->refcount, teedev_ctx_release);
103 void teedev_close_context(struct tee_context *ctx)
105 struct tee_device *teedev = ctx->teedev;
108 tee_device_put(teedev);
110 EXPORT_SYMBOL_GPL(teedev_close_context);
112 static int tee_open(struct inode *inode, struct file *filp)
114 struct tee_context *ctx;
116 ctx = teedev_open(container_of(inode->i_cdev, struct tee_device, cdev));
121 * Default user-space behaviour is to wait for tee-supplicant
122 * if not present for any requests in this context.
124 ctx->supp_nowait = false;
125 filp->private_data = ctx;
129 static int tee_release(struct inode *inode, struct file *filp)
131 teedev_close_context(filp->private_data);
136 * uuid_v5() - Calculate UUIDv5
137 * @uuid: Resulting UUID
138 * @ns: Name space ID for UUIDv5 function
139 * @name: Name for UUIDv5 function
140 * @size: Size of name
142 * UUIDv5 is specific in RFC 4122.
144 * This implements section (for SHA-1):
145 * 4.3. Algorithm for Creating a Name-Based UUID
147 static int uuid_v5(uuid_t *uuid, const uuid_t *ns, const void *name,
150 unsigned char hash[SHA1_DIGEST_SIZE];
151 struct crypto_shash *shash = NULL;
152 struct shash_desc *desc = NULL;
155 shash = crypto_alloc_shash("sha1", 0, 0);
158 pr_err("shash(sha1) allocation failed\n");
162 desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
171 rc = crypto_shash_init(desc);
175 rc = crypto_shash_update(desc, (const u8 *)ns, sizeof(*ns));
179 rc = crypto_shash_update(desc, (const u8 *)name, size);
183 rc = crypto_shash_final(desc, hash);
187 memcpy(uuid->b, hash, UUID_SIZE);
189 /* Tag for version 5 */
190 uuid->b[6] = (hash[6] & 0x0F) | 0x50;
191 uuid->b[8] = (hash[8] & 0x3F) | 0x80;
197 crypto_free_shash(shash);
201 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
202 const u8 connection_data[TEE_IOCTL_UUID_LEN])
204 gid_t ns_grp = (gid_t)-1;
205 kgid_t grp = INVALID_GID;
210 if (connection_method == TEE_IOCTL_LOGIN_PUBLIC ||
211 connection_method == TEE_IOCTL_LOGIN_REE_KERNEL) {
212 /* Nil UUID to be passed to TEE environment */
213 uuid_copy(uuid, &uuid_null);
218 * In Linux environment client UUID is based on UUIDv5.
220 * Determine client UUID with following semantics for 'name':
222 * For TEEC_LOGIN_USER:
225 * For TEEC_LOGIN_GROUP:
230 name = kzalloc(TEE_UUID_NS_NAME_SIZE, GFP_KERNEL);
234 switch (connection_method) {
235 case TEE_IOCTL_LOGIN_USER:
236 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "uid=%x",
238 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
244 case TEE_IOCTL_LOGIN_GROUP:
245 memcpy(&ns_grp, connection_data, sizeof(gid_t));
246 grp = make_kgid(current_user_ns(), ns_grp);
247 if (!gid_valid(grp) || !in_egroup_p(grp)) {
252 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "gid=%x",
254 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
265 rc = uuid_v5(uuid, &tee_client_uuid_ns, name, name_len);
271 EXPORT_SYMBOL_GPL(tee_session_calc_client_uuid);
273 static int tee_ioctl_version(struct tee_context *ctx,
274 struct tee_ioctl_version_data __user *uvers)
276 struct tee_ioctl_version_data vers;
278 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
280 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
281 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
283 if (copy_to_user(uvers, &vers, sizeof(vers)))
289 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
290 struct tee_ioctl_shm_alloc_data __user *udata)
293 struct tee_ioctl_shm_alloc_data data;
296 if (copy_from_user(&data, udata, sizeof(data)))
299 /* Currently no input flags are supported */
303 shm = tee_shm_alloc_user_buf(ctx, data.size);
308 data.size = shm->size;
310 if (copy_to_user(udata, &data, sizeof(data)))
313 ret = tee_shm_get_fd(shm);
316 * When user space closes the file descriptor the shared memory
317 * should be freed or if tee_shm_get_fd() failed then it will
318 * be freed immediately.
325 tee_ioctl_shm_register(struct tee_context *ctx,
326 struct tee_ioctl_shm_register_data __user *udata)
329 struct tee_ioctl_shm_register_data data;
332 if (copy_from_user(&data, udata, sizeof(data)))
335 /* Currently no input flags are supported */
339 shm = tee_shm_register_user_buf(ctx, data.addr, data.length);
344 data.length = shm->size;
346 if (copy_to_user(udata, &data, sizeof(data)))
349 ret = tee_shm_get_fd(shm);
351 * When user space closes the file descriptor the shared memory
352 * should be freed or if tee_shm_get_fd() failed then it will
353 * be freed immediately.
359 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
361 struct tee_ioctl_param __user *uparams)
365 for (n = 0; n < num_params; n++) {
367 struct tee_ioctl_param ip;
369 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
372 /* All unused attribute bits has to be zero */
373 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
376 params[n].attr = ip.attr;
377 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
378 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
379 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
381 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
382 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
383 params[n].u.value.a = ip.a;
384 params[n].u.value.b = ip.b;
385 params[n].u.value.c = ip.c;
387 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
388 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
389 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
391 * If a NULL pointer is passed to a TA in the TEE,
392 * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
393 * indicating a NULL memory reference.
395 if (ip.c != TEE_MEMREF_NULL) {
397 * If we fail to get a pointer to a shared
398 * memory object (and increase the ref count)
399 * from an identifier we return an error. All
400 * pointers that has been added in params have
401 * an increased ref count. It's the callers
402 * responibility to do tee_shm_put() on all
405 shm = tee_shm_get_from_id(ctx, ip.c);
410 * Ensure offset + size does not overflow
411 * offset and does not overflow the size of
412 * the referred shared memory object.
414 if ((ip.a + ip.b) < ip.a ||
415 (ip.a + ip.b) > shm->size) {
419 } else if (ctx->cap_memref_null) {
420 /* Pass NULL pointer to OP-TEE */
426 params[n].u.memref.shm_offs = ip.a;
427 params[n].u.memref.size = ip.b;
428 params[n].u.memref.shm = shm;
431 /* Unknown attribute */
438 static int params_to_user(struct tee_ioctl_param __user *uparams,
439 size_t num_params, struct tee_param *params)
443 for (n = 0; n < num_params; n++) {
444 struct tee_ioctl_param __user *up = uparams + n;
445 struct tee_param *p = params + n;
448 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
449 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
450 if (put_user(p->u.value.a, &up->a) ||
451 put_user(p->u.value.b, &up->b) ||
452 put_user(p->u.value.c, &up->c))
455 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
456 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
457 if (put_user((u64)p->u.memref.size, &up->b))
467 static int tee_ioctl_open_session(struct tee_context *ctx,
468 struct tee_ioctl_buf_data __user *ubuf)
472 struct tee_ioctl_buf_data buf;
473 struct tee_ioctl_open_session_arg __user *uarg;
474 struct tee_ioctl_open_session_arg arg;
475 struct tee_ioctl_param __user *uparams = NULL;
476 struct tee_param *params = NULL;
477 bool have_session = false;
479 if (!ctx->teedev->desc->ops->open_session)
482 if (copy_from_user(&buf, ubuf, sizeof(buf)))
485 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
486 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
489 uarg = u64_to_user_ptr(buf.buf_ptr);
490 if (copy_from_user(&arg, uarg, sizeof(arg)))
493 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
496 if (arg.num_params) {
497 params = kcalloc(arg.num_params, sizeof(struct tee_param),
501 uparams = uarg->params;
502 rc = params_from_user(ctx, params, arg.num_params, uparams);
507 if (arg.clnt_login >= TEE_IOCTL_LOGIN_REE_KERNEL_MIN &&
508 arg.clnt_login <= TEE_IOCTL_LOGIN_REE_KERNEL_MAX) {
509 pr_debug("login method not allowed for user-space client\n");
514 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
519 if (put_user(arg.session, &uarg->session) ||
520 put_user(arg.ret, &uarg->ret) ||
521 put_user(arg.ret_origin, &uarg->ret_origin)) {
525 rc = params_to_user(uparams, arg.num_params, params);
528 * If we've succeeded to open the session but failed to communicate
529 * it back to user space, close the session again to avoid leakage.
531 if (rc && have_session && ctx->teedev->desc->ops->close_session)
532 ctx->teedev->desc->ops->close_session(ctx, arg.session);
535 /* Decrease ref count for all valid shared memory pointers */
536 for (n = 0; n < arg.num_params; n++)
537 if (tee_param_is_memref(params + n) &&
538 params[n].u.memref.shm)
539 tee_shm_put(params[n].u.memref.shm);
546 static int tee_ioctl_invoke(struct tee_context *ctx,
547 struct tee_ioctl_buf_data __user *ubuf)
551 struct tee_ioctl_buf_data buf;
552 struct tee_ioctl_invoke_arg __user *uarg;
553 struct tee_ioctl_invoke_arg arg;
554 struct tee_ioctl_param __user *uparams = NULL;
555 struct tee_param *params = NULL;
557 if (!ctx->teedev->desc->ops->invoke_func)
560 if (copy_from_user(&buf, ubuf, sizeof(buf)))
563 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
564 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
567 uarg = u64_to_user_ptr(buf.buf_ptr);
568 if (copy_from_user(&arg, uarg, sizeof(arg)))
571 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
574 if (arg.num_params) {
575 params = kcalloc(arg.num_params, sizeof(struct tee_param),
579 uparams = uarg->params;
580 rc = params_from_user(ctx, params, arg.num_params, uparams);
585 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
589 if (put_user(arg.ret, &uarg->ret) ||
590 put_user(arg.ret_origin, &uarg->ret_origin)) {
594 rc = params_to_user(uparams, arg.num_params, params);
597 /* Decrease ref count for all valid shared memory pointers */
598 for (n = 0; n < arg.num_params; n++)
599 if (tee_param_is_memref(params + n) &&
600 params[n].u.memref.shm)
601 tee_shm_put(params[n].u.memref.shm);
607 static int tee_ioctl_cancel(struct tee_context *ctx,
608 struct tee_ioctl_cancel_arg __user *uarg)
610 struct tee_ioctl_cancel_arg arg;
612 if (!ctx->teedev->desc->ops->cancel_req)
615 if (copy_from_user(&arg, uarg, sizeof(arg)))
618 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
623 tee_ioctl_close_session(struct tee_context *ctx,
624 struct tee_ioctl_close_session_arg __user *uarg)
626 struct tee_ioctl_close_session_arg arg;
628 if (!ctx->teedev->desc->ops->close_session)
631 if (copy_from_user(&arg, uarg, sizeof(arg)))
634 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
637 static int params_to_supp(struct tee_context *ctx,
638 struct tee_ioctl_param __user *uparams,
639 size_t num_params, struct tee_param *params)
643 for (n = 0; n < num_params; n++) {
644 struct tee_ioctl_param ip;
645 struct tee_param *p = params + n;
648 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
649 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
650 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
655 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
656 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
657 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
658 ip.b = p->u.memref.size;
659 if (!p->u.memref.shm) {
661 ip.c = (u64)-1; /* invalid shm id */
664 ip.a = p->u.memref.shm_offs;
665 ip.c = p->u.memref.shm->id;
674 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
681 static int tee_ioctl_supp_recv(struct tee_context *ctx,
682 struct tee_ioctl_buf_data __user *ubuf)
685 struct tee_ioctl_buf_data buf;
686 struct tee_iocl_supp_recv_arg __user *uarg;
687 struct tee_param *params;
691 if (!ctx->teedev->desc->ops->supp_recv)
694 if (copy_from_user(&buf, ubuf, sizeof(buf)))
697 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
698 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
701 uarg = u64_to_user_ptr(buf.buf_ptr);
702 if (get_user(num_params, &uarg->num_params))
705 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
708 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
712 rc = params_from_user(ctx, params, num_params, uarg->params);
716 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
720 if (put_user(func, &uarg->func) ||
721 put_user(num_params, &uarg->num_params)) {
726 rc = params_to_supp(ctx, uarg->params, num_params, params);
732 static int params_from_supp(struct tee_param *params, size_t num_params,
733 struct tee_ioctl_param __user *uparams)
737 for (n = 0; n < num_params; n++) {
738 struct tee_param *p = params + n;
739 struct tee_ioctl_param ip;
741 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
744 /* All unused attribute bits has to be zero */
745 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
749 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
750 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
751 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
752 /* Only out and in/out values can be updated */
757 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
758 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
760 * Only the size of the memref can be updated.
761 * Since we don't have access to the original
762 * parameters here, only store the supplied size.
763 * The driver will copy the updated size into the
764 * original parameters.
766 p->u.memref.shm = NULL;
767 p->u.memref.shm_offs = 0;
768 p->u.memref.size = ip.b;
771 memset(&p->u, 0, sizeof(p->u));
778 static int tee_ioctl_supp_send(struct tee_context *ctx,
779 struct tee_ioctl_buf_data __user *ubuf)
782 struct tee_ioctl_buf_data buf;
783 struct tee_iocl_supp_send_arg __user *uarg;
784 struct tee_param *params;
788 /* Not valid for this driver */
789 if (!ctx->teedev->desc->ops->supp_send)
792 if (copy_from_user(&buf, ubuf, sizeof(buf)))
795 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
796 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
799 uarg = u64_to_user_ptr(buf.buf_ptr);
800 if (get_user(ret, &uarg->ret) ||
801 get_user(num_params, &uarg->num_params))
804 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
807 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
811 rc = params_from_supp(params, num_params, uarg->params);
815 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
821 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
823 struct tee_context *ctx = filp->private_data;
824 void __user *uarg = (void __user *)arg;
827 case TEE_IOC_VERSION:
828 return tee_ioctl_version(ctx, uarg);
829 case TEE_IOC_SHM_ALLOC:
830 return tee_ioctl_shm_alloc(ctx, uarg);
831 case TEE_IOC_SHM_REGISTER:
832 return tee_ioctl_shm_register(ctx, uarg);
833 case TEE_IOC_OPEN_SESSION:
834 return tee_ioctl_open_session(ctx, uarg);
836 return tee_ioctl_invoke(ctx, uarg);
838 return tee_ioctl_cancel(ctx, uarg);
839 case TEE_IOC_CLOSE_SESSION:
840 return tee_ioctl_close_session(ctx, uarg);
841 case TEE_IOC_SUPPL_RECV:
842 return tee_ioctl_supp_recv(ctx, uarg);
843 case TEE_IOC_SUPPL_SEND:
844 return tee_ioctl_supp_send(ctx, uarg);
850 static const struct file_operations tee_fops = {
851 .owner = THIS_MODULE,
853 .release = tee_release,
854 .unlocked_ioctl = tee_ioctl,
855 .compat_ioctl = compat_ptr_ioctl,
858 static void tee_release_device(struct device *dev)
860 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
862 spin_lock(&driver_lock);
863 clear_bit(teedev->id, dev_mask);
864 spin_unlock(&driver_lock);
865 mutex_destroy(&teedev->mutex);
866 idr_destroy(&teedev->idr);
871 * tee_device_alloc() - Allocate a new struct tee_device instance
872 * @teedesc: Descriptor for this driver
873 * @dev: Parent device for this device
874 * @pool: Shared memory pool, NULL if not used
875 * @driver_data: Private driver data for this device
877 * Allocates a new struct tee_device instance. The device is
878 * removed by tee_device_unregister().
880 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
882 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
884 struct tee_shm_pool *pool,
887 struct tee_device *teedev;
892 if (!teedesc || !teedesc->name || !teedesc->ops ||
893 !teedesc->ops->get_version || !teedesc->ops->open ||
894 !teedesc->ops->release || !pool)
895 return ERR_PTR(-EINVAL);
897 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
899 ret = ERR_PTR(-ENOMEM);
903 max_id = TEE_NUM_DEVICES / 2;
905 if (teedesc->flags & TEE_DESC_PRIVILEGED) {
906 offs = TEE_NUM_DEVICES / 2;
907 max_id = TEE_NUM_DEVICES;
910 spin_lock(&driver_lock);
911 teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
912 if (teedev->id < max_id)
913 set_bit(teedev->id, dev_mask);
914 spin_unlock(&driver_lock);
916 if (teedev->id >= max_id) {
917 ret = ERR_PTR(-ENOMEM);
921 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
922 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
925 teedev->dev.class = &tee_class;
926 teedev->dev.release = tee_release_device;
927 teedev->dev.parent = dev;
929 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
931 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
937 cdev_init(&teedev->cdev, &tee_fops);
938 teedev->cdev.owner = teedesc->owner;
940 dev_set_drvdata(&teedev->dev, driver_data);
941 device_initialize(&teedev->dev);
943 /* 1 as tee_device_unregister() does one final tee_device_put() */
944 teedev->num_users = 1;
945 init_completion(&teedev->c_no_users);
946 mutex_init(&teedev->mutex);
947 idr_init(&teedev->idr);
949 teedev->desc = teedesc;
954 unregister_chrdev_region(teedev->dev.devt, 1);
956 pr_err("could not register %s driver\n",
957 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
958 if (teedev && teedev->id < TEE_NUM_DEVICES) {
959 spin_lock(&driver_lock);
960 clear_bit(teedev->id, dev_mask);
961 spin_unlock(&driver_lock);
966 EXPORT_SYMBOL_GPL(tee_device_alloc);
968 static ssize_t implementation_id_show(struct device *dev,
969 struct device_attribute *attr, char *buf)
971 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
972 struct tee_ioctl_version_data vers;
974 teedev->desc->ops->get_version(teedev, &vers);
975 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
977 static DEVICE_ATTR_RO(implementation_id);
979 static struct attribute *tee_dev_attrs[] = {
980 &dev_attr_implementation_id.attr,
984 ATTRIBUTE_GROUPS(tee_dev);
987 * tee_device_register() - Registers a TEE device
988 * @teedev: Device to register
990 * tee_device_unregister() need to be called to remove the @teedev if
991 * this function fails.
993 * @returns < 0 on failure
995 int tee_device_register(struct tee_device *teedev)
999 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
1000 dev_err(&teedev->dev, "attempt to register twice\n");
1004 teedev->dev.groups = tee_dev_groups;
1006 rc = cdev_device_add(&teedev->cdev, &teedev->dev);
1008 dev_err(&teedev->dev,
1009 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
1010 teedev->name, MAJOR(teedev->dev.devt),
1011 MINOR(teedev->dev.devt), rc);
1015 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
1018 EXPORT_SYMBOL_GPL(tee_device_register);
1020 void tee_device_put(struct tee_device *teedev)
1022 mutex_lock(&teedev->mutex);
1023 /* Shouldn't put in this state */
1024 if (!WARN_ON(!teedev->desc)) {
1025 teedev->num_users--;
1026 if (!teedev->num_users) {
1027 teedev->desc = NULL;
1028 complete(&teedev->c_no_users);
1031 mutex_unlock(&teedev->mutex);
1034 bool tee_device_get(struct tee_device *teedev)
1036 mutex_lock(&teedev->mutex);
1037 if (!teedev->desc) {
1038 mutex_unlock(&teedev->mutex);
1041 teedev->num_users++;
1042 mutex_unlock(&teedev->mutex);
1047 * tee_device_unregister() - Removes a TEE device
1048 * @teedev: Device to unregister
1050 * This function should be called to remove the @teedev even if
1051 * tee_device_register() hasn't been called yet. Does nothing if
1054 void tee_device_unregister(struct tee_device *teedev)
1059 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED)
1060 cdev_device_del(&teedev->cdev, &teedev->dev);
1062 tee_device_put(teedev);
1063 wait_for_completion(&teedev->c_no_users);
1066 * No need to take a mutex any longer now since teedev->desc was
1067 * set to NULL before teedev->c_no_users was completed.
1070 teedev->pool = NULL;
1072 put_device(&teedev->dev);
1074 EXPORT_SYMBOL_GPL(tee_device_unregister);
1077 * tee_get_drvdata() - Return driver_data pointer
1078 * @teedev: Device containing the driver_data pointer
1079 * @returns the driver_data pointer supplied to tee_device_alloc().
1081 void *tee_get_drvdata(struct tee_device *teedev)
1083 return dev_get_drvdata(&teedev->dev);
1085 EXPORT_SYMBOL_GPL(tee_get_drvdata);
1087 struct match_dev_data {
1088 struct tee_ioctl_version_data *vers;
1090 int (*match)(struct tee_ioctl_version_data *, const void *);
1093 static int match_dev(struct device *dev, const void *data)
1095 const struct match_dev_data *match_data = data;
1096 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
1098 teedev->desc->ops->get_version(teedev, match_data->vers);
1099 return match_data->match(match_data->vers, match_data->data);
1102 struct tee_context *
1103 tee_client_open_context(struct tee_context *start,
1104 int (*match)(struct tee_ioctl_version_data *,
1106 const void *data, struct tee_ioctl_version_data *vers)
1108 struct device *dev = NULL;
1109 struct device *put_dev = NULL;
1110 struct tee_context *ctx = NULL;
1111 struct tee_ioctl_version_data v;
1112 struct match_dev_data match_data = { vers ? vers : &v, data, match };
1115 dev = &start->teedev->dev;
1118 dev = class_find_device(&tee_class, dev, &match_data, match_dev);
1120 ctx = ERR_PTR(-ENOENT);
1124 put_device(put_dev);
1127 ctx = teedev_open(container_of(dev, struct tee_device, dev));
1128 } while (IS_ERR(ctx) && PTR_ERR(ctx) != -ENOMEM);
1130 put_device(put_dev);
1132 * Default behaviour for in kernel client is to not wait for
1133 * tee-supplicant if not present for any requests in this context.
1134 * Also this flag could be configured again before call to
1135 * tee_client_open_session() if any in kernel client requires
1136 * different behaviour.
1139 ctx->supp_nowait = true;
1143 EXPORT_SYMBOL_GPL(tee_client_open_context);
1145 void tee_client_close_context(struct tee_context *ctx)
1147 teedev_close_context(ctx);
1149 EXPORT_SYMBOL_GPL(tee_client_close_context);
1151 void tee_client_get_version(struct tee_context *ctx,
1152 struct tee_ioctl_version_data *vers)
1154 ctx->teedev->desc->ops->get_version(ctx->teedev, vers);
1156 EXPORT_SYMBOL_GPL(tee_client_get_version);
1158 int tee_client_open_session(struct tee_context *ctx,
1159 struct tee_ioctl_open_session_arg *arg,
1160 struct tee_param *param)
1162 if (!ctx->teedev->desc->ops->open_session)
1164 return ctx->teedev->desc->ops->open_session(ctx, arg, param);
1166 EXPORT_SYMBOL_GPL(tee_client_open_session);
1168 int tee_client_close_session(struct tee_context *ctx, u32 session)
1170 if (!ctx->teedev->desc->ops->close_session)
1172 return ctx->teedev->desc->ops->close_session(ctx, session);
1174 EXPORT_SYMBOL_GPL(tee_client_close_session);
1176 int tee_client_system_session(struct tee_context *ctx, u32 session)
1178 if (!ctx->teedev->desc->ops->system_session)
1180 return ctx->teedev->desc->ops->system_session(ctx, session);
1182 EXPORT_SYMBOL_GPL(tee_client_system_session);
1184 int tee_client_invoke_func(struct tee_context *ctx,
1185 struct tee_ioctl_invoke_arg *arg,
1186 struct tee_param *param)
1188 if (!ctx->teedev->desc->ops->invoke_func)
1190 return ctx->teedev->desc->ops->invoke_func(ctx, arg, param);
1192 EXPORT_SYMBOL_GPL(tee_client_invoke_func);
1194 int tee_client_cancel_req(struct tee_context *ctx,
1195 struct tee_ioctl_cancel_arg *arg)
1197 if (!ctx->teedev->desc->ops->cancel_req)
1199 return ctx->teedev->desc->ops->cancel_req(ctx, arg->cancel_id,
1203 static int tee_client_device_match(struct device *dev,
1204 struct device_driver *drv)
1206 const struct tee_client_device_id *id_table;
1207 struct tee_client_device *tee_device;
1209 id_table = to_tee_client_driver(drv)->id_table;
1210 tee_device = to_tee_client_device(dev);
1212 while (!uuid_is_null(&id_table->uuid)) {
1213 if (uuid_equal(&tee_device->id.uuid, &id_table->uuid))
1221 static int tee_client_device_uevent(const struct device *dev,
1222 struct kobj_uevent_env *env)
1224 uuid_t *dev_id = &to_tee_client_device(dev)->id.uuid;
1226 return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
1229 struct bus_type tee_bus_type = {
1231 .match = tee_client_device_match,
1232 .uevent = tee_client_device_uevent,
1234 EXPORT_SYMBOL_GPL(tee_bus_type);
1236 static int __init tee_init(void)
1240 rc = class_register(&tee_class);
1242 pr_err("couldn't create class\n");
1246 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
1248 pr_err("failed to allocate char dev region\n");
1249 goto out_unreg_class;
1252 rc = bus_register(&tee_bus_type);
1254 pr_err("failed to register tee bus\n");
1255 goto out_unreg_chrdev;
1261 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1263 class_unregister(&tee_class);
1268 static void __exit tee_exit(void)
1270 bus_unregister(&tee_bus_type);
1271 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1272 class_unregister(&tee_class);
1275 subsys_initcall(tee_init);
1276 module_exit(tee_exit);
1278 MODULE_AUTHOR("Linaro");
1279 MODULE_DESCRIPTION("TEE Driver");
1280 MODULE_VERSION("1.0");
1281 MODULE_LICENSE("GPL v2");