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 tee_device_put(ctx
->teedev
);
105 static int tee_open(struct inode
*inode
, struct file
*filp
)
107 struct tee_context
*ctx
;
109 ctx
= teedev_open(container_of(inode
->i_cdev
, struct tee_device
, cdev
));
114 * Default user-space behaviour is to wait for tee-supplicant
115 * if not present for any requests in this context.
117 ctx
->supp_nowait
= false;
118 filp
->private_data
= ctx
;
122 static int tee_release(struct inode
*inode
, struct file
*filp
)
124 teedev_close_context(filp
->private_data
);
129 * uuid_v5() - Calculate UUIDv5
130 * @uuid: Resulting UUID
131 * @ns: Name space ID for UUIDv5 function
132 * @name: Name for UUIDv5 function
133 * @size: Size of name
135 * UUIDv5 is specific in RFC 4122.
137 * This implements section (for SHA-1):
138 * 4.3. Algorithm for Creating a Name-Based UUID
140 static int uuid_v5(uuid_t
*uuid
, const uuid_t
*ns
, const void *name
,
143 unsigned char hash
[SHA1_DIGEST_SIZE
];
144 struct crypto_shash
*shash
= NULL
;
145 struct shash_desc
*desc
= NULL
;
148 shash
= crypto_alloc_shash("sha1", 0, 0);
151 pr_err("shash(sha1) allocation failed\n");
155 desc
= kzalloc(sizeof(*desc
) + crypto_shash_descsize(shash
),
164 rc
= crypto_shash_init(desc
);
168 rc
= crypto_shash_update(desc
, (const u8
*)ns
, sizeof(*ns
));
172 rc
= crypto_shash_update(desc
, (const u8
*)name
, size
);
176 rc
= crypto_shash_final(desc
, hash
);
180 memcpy(uuid
->b
, hash
, UUID_SIZE
);
182 /* Tag for version 5 */
183 uuid
->b
[6] = (hash
[6] & 0x0F) | 0x50;
184 uuid
->b
[8] = (hash
[8] & 0x3F) | 0x80;
190 crypto_free_shash(shash
);
194 int tee_session_calc_client_uuid(uuid_t
*uuid
, u32 connection_method
,
195 const u8 connection_data
[TEE_IOCTL_UUID_LEN
])
197 gid_t ns_grp
= (gid_t
)-1;
198 kgid_t grp
= INVALID_GID
;
203 if (connection_method
== TEE_IOCTL_LOGIN_PUBLIC
||
204 connection_method
== TEE_IOCTL_LOGIN_REE_KERNEL
) {
205 /* Nil UUID to be passed to TEE environment */
206 uuid_copy(uuid
, &uuid_null
);
211 * In Linux environment client UUID is based on UUIDv5.
213 * Determine client UUID with following semantics for 'name':
215 * For TEEC_LOGIN_USER:
218 * For TEEC_LOGIN_GROUP:
223 name
= kzalloc(TEE_UUID_NS_NAME_SIZE
, GFP_KERNEL
);
227 switch (connection_method
) {
228 case TEE_IOCTL_LOGIN_USER
:
229 name_len
= snprintf(name
, TEE_UUID_NS_NAME_SIZE
, "uid=%x",
231 if (name_len
>= TEE_UUID_NS_NAME_SIZE
) {
237 case TEE_IOCTL_LOGIN_GROUP
:
238 memcpy(&ns_grp
, connection_data
, sizeof(gid_t
));
239 grp
= make_kgid(current_user_ns(), ns_grp
);
240 if (!gid_valid(grp
) || !in_egroup_p(grp
)) {
245 name_len
= snprintf(name
, TEE_UUID_NS_NAME_SIZE
, "gid=%x",
247 if (name_len
>= TEE_UUID_NS_NAME_SIZE
) {
258 rc
= uuid_v5(uuid
, &tee_client_uuid_ns
, name
, name_len
);
264 EXPORT_SYMBOL_GPL(tee_session_calc_client_uuid
);
266 static int tee_ioctl_version(struct tee_context
*ctx
,
267 struct tee_ioctl_version_data __user
*uvers
)
269 struct tee_ioctl_version_data vers
;
271 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, &vers
);
273 if (ctx
->teedev
->desc
->flags
& TEE_DESC_PRIVILEGED
)
274 vers
.gen_caps
|= TEE_GEN_CAP_PRIVILEGED
;
276 if (copy_to_user(uvers
, &vers
, sizeof(vers
)))
282 static int tee_ioctl_shm_alloc(struct tee_context
*ctx
,
283 struct tee_ioctl_shm_alloc_data __user
*udata
)
286 struct tee_ioctl_shm_alloc_data data
;
289 if (copy_from_user(&data
, udata
, sizeof(data
)))
292 /* Currently no input flags are supported */
296 shm
= tee_shm_alloc(ctx
, data
.size
, TEE_SHM_MAPPED
| TEE_SHM_DMA_BUF
);
301 data
.flags
= shm
->flags
;
302 data
.size
= shm
->size
;
304 if (copy_to_user(udata
, &data
, sizeof(data
)))
307 ret
= tee_shm_get_fd(shm
);
310 * When user space closes the file descriptor the shared memory
311 * should be freed or if tee_shm_get_fd() failed then it will
312 * be freed immediately.
319 tee_ioctl_shm_register(struct tee_context
*ctx
,
320 struct tee_ioctl_shm_register_data __user
*udata
)
323 struct tee_ioctl_shm_register_data data
;
326 if (copy_from_user(&data
, udata
, sizeof(data
)))
329 /* Currently no input flags are supported */
333 shm
= tee_shm_register(ctx
, data
.addr
, data
.length
,
334 TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
);
339 data
.flags
= shm
->flags
;
340 data
.length
= shm
->size
;
342 if (copy_to_user(udata
, &data
, sizeof(data
)))
345 ret
= tee_shm_get_fd(shm
);
347 * When user space closes the file descriptor the shared memory
348 * should be freed or if tee_shm_get_fd() failed then it will
349 * be freed immediately.
355 static int params_from_user(struct tee_context
*ctx
, struct tee_param
*params
,
357 struct tee_ioctl_param __user
*uparams
)
361 for (n
= 0; n
< num_params
; n
++) {
363 struct tee_ioctl_param ip
;
365 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
368 /* All unused attribute bits has to be zero */
369 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
372 params
[n
].attr
= ip
.attr
;
373 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
374 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE
:
375 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
377 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
378 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
379 params
[n
].u
.value
.a
= ip
.a
;
380 params
[n
].u
.value
.b
= ip
.b
;
381 params
[n
].u
.value
.c
= ip
.c
;
383 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
384 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
385 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
387 * If a NULL pointer is passed to a TA in the TEE,
388 * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
389 * indicating a NULL memory reference.
391 if (ip
.c
!= TEE_MEMREF_NULL
) {
393 * If we fail to get a pointer to a shared
394 * memory object (and increase the ref count)
395 * from an identifier we return an error. All
396 * pointers that has been added in params have
397 * an increased ref count. It's the callers
398 * responibility to do tee_shm_put() on all
401 shm
= tee_shm_get_from_id(ctx
, ip
.c
);
406 * Ensure offset + size does not overflow
407 * offset and does not overflow the size of
408 * the referred shared memory object.
410 if ((ip
.a
+ ip
.b
) < ip
.a
||
411 (ip
.a
+ ip
.b
) > shm
->size
) {
415 } else if (ctx
->cap_memref_null
) {
416 /* Pass NULL pointer to OP-TEE */
422 params
[n
].u
.memref
.shm_offs
= ip
.a
;
423 params
[n
].u
.memref
.size
= ip
.b
;
424 params
[n
].u
.memref
.shm
= shm
;
427 /* Unknown attribute */
434 static int params_to_user(struct tee_ioctl_param __user
*uparams
,
435 size_t num_params
, struct tee_param
*params
)
439 for (n
= 0; n
< num_params
; n
++) {
440 struct tee_ioctl_param __user
*up
= uparams
+ n
;
441 struct tee_param
*p
= params
+ n
;
444 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
445 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
446 if (put_user(p
->u
.value
.a
, &up
->a
) ||
447 put_user(p
->u
.value
.b
, &up
->b
) ||
448 put_user(p
->u
.value
.c
, &up
->c
))
451 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
452 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
453 if (put_user((u64
)p
->u
.memref
.size
, &up
->b
))
462 static int tee_ioctl_open_session(struct tee_context
*ctx
,
463 struct tee_ioctl_buf_data __user
*ubuf
)
467 struct tee_ioctl_buf_data buf
;
468 struct tee_ioctl_open_session_arg __user
*uarg
;
469 struct tee_ioctl_open_session_arg arg
;
470 struct tee_ioctl_param __user
*uparams
= NULL
;
471 struct tee_param
*params
= NULL
;
472 bool have_session
= false;
474 if (!ctx
->teedev
->desc
->ops
->open_session
)
477 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
480 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
481 buf
.buf_len
< sizeof(struct tee_ioctl_open_session_arg
))
484 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
485 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
488 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
491 if (arg
.num_params
) {
492 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
496 uparams
= uarg
->params
;
497 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
502 if (arg
.clnt_login
>= TEE_IOCTL_LOGIN_REE_KERNEL_MIN
&&
503 arg
.clnt_login
<= TEE_IOCTL_LOGIN_REE_KERNEL_MAX
) {
504 pr_debug("login method not allowed for user-space client\n");
509 rc
= ctx
->teedev
->desc
->ops
->open_session(ctx
, &arg
, params
);
514 if (put_user(arg
.session
, &uarg
->session
) ||
515 put_user(arg
.ret
, &uarg
->ret
) ||
516 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
520 rc
= params_to_user(uparams
, arg
.num_params
, params
);
523 * If we've succeeded to open the session but failed to communicate
524 * it back to user space, close the session again to avoid leakage.
526 if (rc
&& have_session
&& ctx
->teedev
->desc
->ops
->close_session
)
527 ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
530 /* Decrease ref count for all valid shared memory pointers */
531 for (n
= 0; n
< arg
.num_params
; n
++)
532 if (tee_param_is_memref(params
+ n
) &&
533 params
[n
].u
.memref
.shm
)
534 tee_shm_put(params
[n
].u
.memref
.shm
);
541 static int tee_ioctl_invoke(struct tee_context
*ctx
,
542 struct tee_ioctl_buf_data __user
*ubuf
)
546 struct tee_ioctl_buf_data buf
;
547 struct tee_ioctl_invoke_arg __user
*uarg
;
548 struct tee_ioctl_invoke_arg arg
;
549 struct tee_ioctl_param __user
*uparams
= NULL
;
550 struct tee_param
*params
= NULL
;
552 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
555 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
558 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
559 buf
.buf_len
< sizeof(struct tee_ioctl_invoke_arg
))
562 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
563 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
566 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
569 if (arg
.num_params
) {
570 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
574 uparams
= uarg
->params
;
575 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
580 rc
= ctx
->teedev
->desc
->ops
->invoke_func(ctx
, &arg
, params
);
584 if (put_user(arg
.ret
, &uarg
->ret
) ||
585 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
589 rc
= params_to_user(uparams
, arg
.num_params
, params
);
592 /* Decrease ref count for all valid shared memory pointers */
593 for (n
= 0; n
< arg
.num_params
; n
++)
594 if (tee_param_is_memref(params
+ n
) &&
595 params
[n
].u
.memref
.shm
)
596 tee_shm_put(params
[n
].u
.memref
.shm
);
602 static int tee_ioctl_cancel(struct tee_context
*ctx
,
603 struct tee_ioctl_cancel_arg __user
*uarg
)
605 struct tee_ioctl_cancel_arg arg
;
607 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
610 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
613 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
.cancel_id
,
618 tee_ioctl_close_session(struct tee_context
*ctx
,
619 struct tee_ioctl_close_session_arg __user
*uarg
)
621 struct tee_ioctl_close_session_arg arg
;
623 if (!ctx
->teedev
->desc
->ops
->close_session
)
626 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
629 return ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
632 static int params_to_supp(struct tee_context
*ctx
,
633 struct tee_ioctl_param __user
*uparams
,
634 size_t num_params
, struct tee_param
*params
)
638 for (n
= 0; n
< num_params
; n
++) {
639 struct tee_ioctl_param ip
;
640 struct tee_param
*p
= params
+ n
;
643 switch (p
->attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
644 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
645 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
650 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
651 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
652 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
653 ip
.b
= p
->u
.memref
.size
;
654 if (!p
->u
.memref
.shm
) {
656 ip
.c
= (u64
)-1; /* invalid shm id */
659 ip
.a
= p
->u
.memref
.shm_offs
;
660 ip
.c
= p
->u
.memref
.shm
->id
;
669 if (copy_to_user(uparams
+ n
, &ip
, sizeof(ip
)))
676 static int tee_ioctl_supp_recv(struct tee_context
*ctx
,
677 struct tee_ioctl_buf_data __user
*ubuf
)
680 struct tee_ioctl_buf_data buf
;
681 struct tee_iocl_supp_recv_arg __user
*uarg
;
682 struct tee_param
*params
;
686 if (!ctx
->teedev
->desc
->ops
->supp_recv
)
689 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
692 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
693 buf
.buf_len
< sizeof(struct tee_iocl_supp_recv_arg
))
696 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
697 if (get_user(num_params
, &uarg
->num_params
))
700 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) != buf
.buf_len
)
703 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
707 rc
= params_from_user(ctx
, params
, num_params
, uarg
->params
);
711 rc
= ctx
->teedev
->desc
->ops
->supp_recv(ctx
, &func
, &num_params
, params
);
715 if (put_user(func
, &uarg
->func
) ||
716 put_user(num_params
, &uarg
->num_params
)) {
721 rc
= params_to_supp(ctx
, uarg
->params
, num_params
, params
);
727 static int params_from_supp(struct tee_param
*params
, size_t num_params
,
728 struct tee_ioctl_param __user
*uparams
)
732 for (n
= 0; n
< num_params
; n
++) {
733 struct tee_param
*p
= params
+ n
;
734 struct tee_ioctl_param ip
;
736 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
739 /* All unused attribute bits has to be zero */
740 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
744 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
745 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
746 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
747 /* Only out and in/out values can be updated */
752 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
753 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
755 * Only the size of the memref can be updated.
756 * Since we don't have access to the original
757 * parameters here, only store the supplied size.
758 * The driver will copy the updated size into the
759 * original parameters.
761 p
->u
.memref
.shm
= NULL
;
762 p
->u
.memref
.shm_offs
= 0;
763 p
->u
.memref
.size
= ip
.b
;
766 memset(&p
->u
, 0, sizeof(p
->u
));
773 static int tee_ioctl_supp_send(struct tee_context
*ctx
,
774 struct tee_ioctl_buf_data __user
*ubuf
)
777 struct tee_ioctl_buf_data buf
;
778 struct tee_iocl_supp_send_arg __user
*uarg
;
779 struct tee_param
*params
;
783 /* Not valid for this driver */
784 if (!ctx
->teedev
->desc
->ops
->supp_send
)
787 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
790 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
791 buf
.buf_len
< sizeof(struct tee_iocl_supp_send_arg
))
794 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
795 if (get_user(ret
, &uarg
->ret
) ||
796 get_user(num_params
, &uarg
->num_params
))
799 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) > buf
.buf_len
)
802 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
806 rc
= params_from_supp(params
, num_params
, uarg
->params
);
810 rc
= ctx
->teedev
->desc
->ops
->supp_send(ctx
, ret
, num_params
, params
);
816 static long tee_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
818 struct tee_context
*ctx
= filp
->private_data
;
819 void __user
*uarg
= (void __user
*)arg
;
822 case TEE_IOC_VERSION
:
823 return tee_ioctl_version(ctx
, uarg
);
824 case TEE_IOC_SHM_ALLOC
:
825 return tee_ioctl_shm_alloc(ctx
, uarg
);
826 case TEE_IOC_SHM_REGISTER
:
827 return tee_ioctl_shm_register(ctx
, uarg
);
828 case TEE_IOC_OPEN_SESSION
:
829 return tee_ioctl_open_session(ctx
, uarg
);
831 return tee_ioctl_invoke(ctx
, uarg
);
833 return tee_ioctl_cancel(ctx
, uarg
);
834 case TEE_IOC_CLOSE_SESSION
:
835 return tee_ioctl_close_session(ctx
, uarg
);
836 case TEE_IOC_SUPPL_RECV
:
837 return tee_ioctl_supp_recv(ctx
, uarg
);
838 case TEE_IOC_SUPPL_SEND
:
839 return tee_ioctl_supp_send(ctx
, uarg
);
845 static const struct file_operations tee_fops
= {
846 .owner
= THIS_MODULE
,
848 .release
= tee_release
,
849 .unlocked_ioctl
= tee_ioctl
,
850 .compat_ioctl
= compat_ptr_ioctl
,
853 static void tee_release_device(struct device
*dev
)
855 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
857 spin_lock(&driver_lock
);
858 clear_bit(teedev
->id
, dev_mask
);
859 spin_unlock(&driver_lock
);
860 mutex_destroy(&teedev
->mutex
);
861 idr_destroy(&teedev
->idr
);
866 * tee_device_alloc() - Allocate a new struct tee_device instance
867 * @teedesc: Descriptor for this driver
868 * @dev: Parent device for this device
869 * @pool: Shared memory pool, NULL if not used
870 * @driver_data: Private driver data for this device
872 * Allocates a new struct tee_device instance. The device is
873 * removed by tee_device_unregister().
875 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
877 struct tee_device
*tee_device_alloc(const struct tee_desc
*teedesc
,
879 struct tee_shm_pool
*pool
,
882 struct tee_device
*teedev
;
887 if (!teedesc
|| !teedesc
->name
|| !teedesc
->ops
||
888 !teedesc
->ops
->get_version
|| !teedesc
->ops
->open
||
889 !teedesc
->ops
->release
|| !pool
)
890 return ERR_PTR(-EINVAL
);
892 teedev
= kzalloc(sizeof(*teedev
), GFP_KERNEL
);
894 ret
= ERR_PTR(-ENOMEM
);
898 max_id
= TEE_NUM_DEVICES
/ 2;
900 if (teedesc
->flags
& TEE_DESC_PRIVILEGED
) {
901 offs
= TEE_NUM_DEVICES
/ 2;
902 max_id
= TEE_NUM_DEVICES
;
905 spin_lock(&driver_lock
);
906 teedev
->id
= find_next_zero_bit(dev_mask
, max_id
, offs
);
907 if (teedev
->id
< max_id
)
908 set_bit(teedev
->id
, dev_mask
);
909 spin_unlock(&driver_lock
);
911 if (teedev
->id
>= max_id
) {
912 ret
= ERR_PTR(-ENOMEM
);
916 snprintf(teedev
->name
, sizeof(teedev
->name
), "tee%s%d",
917 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "priv" : "",
920 teedev
->dev
.class = tee_class
;
921 teedev
->dev
.release
= tee_release_device
;
922 teedev
->dev
.parent
= dev
;
924 teedev
->dev
.devt
= MKDEV(MAJOR(tee_devt
), teedev
->id
);
926 rc
= dev_set_name(&teedev
->dev
, "%s", teedev
->name
);
932 cdev_init(&teedev
->cdev
, &tee_fops
);
933 teedev
->cdev
.owner
= teedesc
->owner
;
935 dev_set_drvdata(&teedev
->dev
, driver_data
);
936 device_initialize(&teedev
->dev
);
938 /* 1 as tee_device_unregister() does one final tee_device_put() */
939 teedev
->num_users
= 1;
940 init_completion(&teedev
->c_no_users
);
941 mutex_init(&teedev
->mutex
);
942 idr_init(&teedev
->idr
);
944 teedev
->desc
= teedesc
;
949 unregister_chrdev_region(teedev
->dev
.devt
, 1);
951 pr_err("could not register %s driver\n",
952 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "privileged" : "client");
953 if (teedev
&& teedev
->id
< TEE_NUM_DEVICES
) {
954 spin_lock(&driver_lock
);
955 clear_bit(teedev
->id
, dev_mask
);
956 spin_unlock(&driver_lock
);
961 EXPORT_SYMBOL_GPL(tee_device_alloc
);
963 static ssize_t
implementation_id_show(struct device
*dev
,
964 struct device_attribute
*attr
, char *buf
)
966 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
967 struct tee_ioctl_version_data vers
;
969 teedev
->desc
->ops
->get_version(teedev
, &vers
);
970 return scnprintf(buf
, PAGE_SIZE
, "%d\n", vers
.impl_id
);
972 static DEVICE_ATTR_RO(implementation_id
);
974 static struct attribute
*tee_dev_attrs
[] = {
975 &dev_attr_implementation_id
.attr
,
979 ATTRIBUTE_GROUPS(tee_dev
);
982 * tee_device_register() - Registers a TEE device
983 * @teedev: Device to register
985 * tee_device_unregister() need to be called to remove the @teedev if
986 * this function fails.
988 * @returns < 0 on failure
990 int tee_device_register(struct tee_device
*teedev
)
994 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
995 dev_err(&teedev
->dev
, "attempt to register twice\n");
999 teedev
->dev
.groups
= tee_dev_groups
;
1001 rc
= cdev_device_add(&teedev
->cdev
, &teedev
->dev
);
1003 dev_err(&teedev
->dev
,
1004 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
1005 teedev
->name
, MAJOR(teedev
->dev
.devt
),
1006 MINOR(teedev
->dev
.devt
), rc
);
1010 teedev
->flags
|= TEE_DEVICE_FLAG_REGISTERED
;
1013 EXPORT_SYMBOL_GPL(tee_device_register
);
1015 void tee_device_put(struct tee_device
*teedev
)
1017 mutex_lock(&teedev
->mutex
);
1018 /* Shouldn't put in this state */
1019 if (!WARN_ON(!teedev
->desc
)) {
1020 teedev
->num_users
--;
1021 if (!teedev
->num_users
) {
1022 teedev
->desc
= NULL
;
1023 complete(&teedev
->c_no_users
);
1026 mutex_unlock(&teedev
->mutex
);
1029 bool tee_device_get(struct tee_device
*teedev
)
1031 mutex_lock(&teedev
->mutex
);
1032 if (!teedev
->desc
) {
1033 mutex_unlock(&teedev
->mutex
);
1036 teedev
->num_users
++;
1037 mutex_unlock(&teedev
->mutex
);
1042 * tee_device_unregister() - Removes a TEE device
1043 * @teedev: Device to unregister
1045 * This function should be called to remove the @teedev even if
1046 * tee_device_register() hasn't been called yet. Does nothing if
1049 void tee_device_unregister(struct tee_device
*teedev
)
1054 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
)
1055 cdev_device_del(&teedev
->cdev
, &teedev
->dev
);
1057 tee_device_put(teedev
);
1058 wait_for_completion(&teedev
->c_no_users
);
1061 * No need to take a mutex any longer now since teedev->desc was
1062 * set to NULL before teedev->c_no_users was completed.
1065 teedev
->pool
= NULL
;
1067 put_device(&teedev
->dev
);
1069 EXPORT_SYMBOL_GPL(tee_device_unregister
);
1072 * tee_get_drvdata() - Return driver_data pointer
1073 * @teedev: Device containing the driver_data pointer
1074 * @returns the driver_data pointer supplied to tee_register().
1076 void *tee_get_drvdata(struct tee_device
*teedev
)
1078 return dev_get_drvdata(&teedev
->dev
);
1080 EXPORT_SYMBOL_GPL(tee_get_drvdata
);
1082 struct match_dev_data
{
1083 struct tee_ioctl_version_data
*vers
;
1085 int (*match
)(struct tee_ioctl_version_data
*, const void *);
1088 static int match_dev(struct device
*dev
, const void *data
)
1090 const struct match_dev_data
*match_data
= data
;
1091 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
1093 teedev
->desc
->ops
->get_version(teedev
, match_data
->vers
);
1094 return match_data
->match(match_data
->vers
, match_data
->data
);
1097 struct tee_context
*
1098 tee_client_open_context(struct tee_context
*start
,
1099 int (*match
)(struct tee_ioctl_version_data
*,
1101 const void *data
, struct tee_ioctl_version_data
*vers
)
1103 struct device
*dev
= NULL
;
1104 struct device
*put_dev
= NULL
;
1105 struct tee_context
*ctx
= NULL
;
1106 struct tee_ioctl_version_data v
;
1107 struct match_dev_data match_data
= { vers
? vers
: &v
, data
, match
};
1110 dev
= &start
->teedev
->dev
;
1113 dev
= class_find_device(tee_class
, dev
, &match_data
, match_dev
);
1115 ctx
= ERR_PTR(-ENOENT
);
1119 put_device(put_dev
);
1122 ctx
= teedev_open(container_of(dev
, struct tee_device
, dev
));
1123 } while (IS_ERR(ctx
) && PTR_ERR(ctx
) != -ENOMEM
);
1125 put_device(put_dev
);
1127 * Default behaviour for in kernel client is to not wait for
1128 * tee-supplicant if not present for any requests in this context.
1129 * Also this flag could be configured again before call to
1130 * tee_client_open_session() if any in kernel client requires
1131 * different behaviour.
1134 ctx
->supp_nowait
= true;
1138 EXPORT_SYMBOL_GPL(tee_client_open_context
);
1140 void tee_client_close_context(struct tee_context
*ctx
)
1142 teedev_close_context(ctx
);
1144 EXPORT_SYMBOL_GPL(tee_client_close_context
);
1146 void tee_client_get_version(struct tee_context
*ctx
,
1147 struct tee_ioctl_version_data
*vers
)
1149 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, vers
);
1151 EXPORT_SYMBOL_GPL(tee_client_get_version
);
1153 int tee_client_open_session(struct tee_context
*ctx
,
1154 struct tee_ioctl_open_session_arg
*arg
,
1155 struct tee_param
*param
)
1157 if (!ctx
->teedev
->desc
->ops
->open_session
)
1159 return ctx
->teedev
->desc
->ops
->open_session(ctx
, arg
, param
);
1161 EXPORT_SYMBOL_GPL(tee_client_open_session
);
1163 int tee_client_close_session(struct tee_context
*ctx
, u32 session
)
1165 if (!ctx
->teedev
->desc
->ops
->close_session
)
1167 return ctx
->teedev
->desc
->ops
->close_session(ctx
, session
);
1169 EXPORT_SYMBOL_GPL(tee_client_close_session
);
1171 int tee_client_invoke_func(struct tee_context
*ctx
,
1172 struct tee_ioctl_invoke_arg
*arg
,
1173 struct tee_param
*param
)
1175 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
1177 return ctx
->teedev
->desc
->ops
->invoke_func(ctx
, arg
, param
);
1179 EXPORT_SYMBOL_GPL(tee_client_invoke_func
);
1181 int tee_client_cancel_req(struct tee_context
*ctx
,
1182 struct tee_ioctl_cancel_arg
*arg
)
1184 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
1186 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
->cancel_id
,
1190 static int tee_client_device_match(struct device
*dev
,
1191 struct device_driver
*drv
)
1193 const struct tee_client_device_id
*id_table
;
1194 struct tee_client_device
*tee_device
;
1196 id_table
= to_tee_client_driver(drv
)->id_table
;
1197 tee_device
= to_tee_client_device(dev
);
1199 while (!uuid_is_null(&id_table
->uuid
)) {
1200 if (uuid_equal(&tee_device
->id
.uuid
, &id_table
->uuid
))
1208 static int tee_client_device_uevent(struct device
*dev
,
1209 struct kobj_uevent_env
*env
)
1211 uuid_t
*dev_id
= &to_tee_client_device(dev
)->id
.uuid
;
1213 return add_uevent_var(env
, "MODALIAS=tee:%pUb", dev_id
);
1216 struct bus_type tee_bus_type
= {
1218 .match
= tee_client_device_match
,
1219 .uevent
= tee_client_device_uevent
,
1221 EXPORT_SYMBOL_GPL(tee_bus_type
);
1223 static int __init
tee_init(void)
1227 tee_class
= class_create(THIS_MODULE
, "tee");
1228 if (IS_ERR(tee_class
)) {
1229 pr_err("couldn't create class\n");
1230 return PTR_ERR(tee_class
);
1233 rc
= alloc_chrdev_region(&tee_devt
, 0, TEE_NUM_DEVICES
, "tee");
1235 pr_err("failed to allocate char dev region\n");
1236 goto out_unreg_class
;
1239 rc
= bus_register(&tee_bus_type
);
1241 pr_err("failed to register tee bus\n");
1242 goto out_unreg_chrdev
;
1248 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1250 class_destroy(tee_class
);
1256 static void __exit
tee_exit(void)
1258 bus_unregister(&tee_bus_type
);
1259 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1260 class_destroy(tee_class
);
1264 subsys_initcall(tee_init
);
1265 module_exit(tee_exit
);
1267 MODULE_AUTHOR("Linaro");
1268 MODULE_DESCRIPTION("TEE Driver");
1269 MODULE_VERSION("1.0");
1270 MODULE_LICENSE("GPL v2");