tee: generic TEE subsystem
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / tee / tee_private.h
1 /*
2 * Copyright (c) 2015-2016, Linaro Limited
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14 #ifndef TEE_PRIVATE_H
15 #define TEE_PRIVATE_H
16
17 #include <linux/cdev.h>
18 #include <linux/completion.h>
19 #include <linux/device.h>
20 #include <linux/kref.h>
21 #include <linux/mutex.h>
22 #include <linux/types.h>
23
24 struct tee_device;
25
26 /**
27 * struct tee_shm - shared memory object
28 * @teedev: device used to allocate the object
29 * @ctx: context using the object, if NULL the context is gone
30 * @link link element
31 * @paddr: physical address of the shared memory
32 * @kaddr: virtual address of the shared memory
33 * @size: size of shared memory
34 * @dmabuf: dmabuf used to for exporting to user space
35 * @flags: defined by TEE_SHM_* in tee_drv.h
36 * @id: unique id of a shared memory object on this device
37 */
38 struct tee_shm {
39 struct tee_device *teedev;
40 struct tee_context *ctx;
41 struct list_head link;
42 phys_addr_t paddr;
43 void *kaddr;
44 size_t size;
45 struct dma_buf *dmabuf;
46 u32 flags;
47 int id;
48 };
49
50 struct tee_shm_pool_mgr;
51
52 /**
53 * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
54 * @alloc: called when allocating shared memory
55 * @free: called when freeing shared memory
56 */
57 struct tee_shm_pool_mgr_ops {
58 int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
59 size_t size);
60 void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
61 };
62
63 /**
64 * struct tee_shm_pool_mgr - shared memory manager
65 * @ops: operations
66 * @private_data: private data for the shared memory manager
67 */
68 struct tee_shm_pool_mgr {
69 const struct tee_shm_pool_mgr_ops *ops;
70 void *private_data;
71 };
72
73 /**
74 * struct tee_shm_pool - shared memory pool
75 * @private_mgr: pool manager for shared memory only between kernel
76 * and secure world
77 * @dma_buf_mgr: pool manager for shared memory exported to user space
78 * @destroy: called when destroying the pool
79 * @private_data: private data for the pool
80 */
81 struct tee_shm_pool {
82 struct tee_shm_pool_mgr private_mgr;
83 struct tee_shm_pool_mgr dma_buf_mgr;
84 void (*destroy)(struct tee_shm_pool *pool);
85 void *private_data;
86 };
87
88 #define TEE_DEVICE_FLAG_REGISTERED 0x1
89 #define TEE_MAX_DEV_NAME_LEN 32
90
91 /**
92 * struct tee_device - TEE Device representation
93 * @name: name of device
94 * @desc: description of device
95 * @id: unique id of device
96 * @flags: represented by TEE_DEVICE_FLAG_REGISTERED above
97 * @dev: embedded basic device structure
98 * @cdev: embedded cdev
99 * @num_users: number of active users of this device
100 * @c_no_user: completion used when unregistering the device
101 * @mutex: mutex protecting @num_users and @idr
102 * @idr: register of shared memory object allocated on this device
103 * @pool: shared memory pool
104 */
105 struct tee_device {
106 char name[TEE_MAX_DEV_NAME_LEN];
107 const struct tee_desc *desc;
108 int id;
109 unsigned int flags;
110
111 struct device dev;
112 struct cdev cdev;
113
114 size_t num_users;
115 struct completion c_no_users;
116 struct mutex mutex; /* protects num_users and idr */
117
118 struct idr idr;
119 struct tee_shm_pool *pool;
120 };
121
122 int tee_shm_init(void);
123
124 int tee_shm_get_fd(struct tee_shm *shm);
125
126 bool tee_device_get(struct tee_device *teedev);
127 void tee_device_put(struct tee_device *teedev);
128
129 #endif /*TEE_PRIVATE_H*/