1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (c) 2024 Linaro Limited
4 */
5
6 #ifndef __TEE_CORE_H
7 #define __TEE_CORE_H
8
9 #include <linux/cdev.h>
10 #include <linux/device.h>
11 #include <linux/dma-buf.h>
12 #include <linux/idr.h>
13 #include <linux/kref.h>
14 #include <linux/list.h>
15 #include <linux/scatterlist.h>
16 #include <linux/tee.h>
17 #include <linux/tee_drv.h>
18 #include <linux/types.h>
19 #include <linux/uuid.h>
20
21 /*
22 * The file describes the API provided by the generic TEE driver to the
23 * specific TEE driver.
24 */
25
26 #define TEE_SHM_DYNAMIC BIT(0) /* Dynamic shared memory registered */
27 /* in secure world */
28 #define TEE_SHM_USER_MAPPED BIT(1) /* Memory mapped in user space */
29 #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */
30 #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */
31 #define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */
32 #define TEE_SHM_DMA_MEM BIT(5) /* Memory allocated with */
33 /* dma_alloc_pages() */
34
35 #define TEE_DEVICE_FLAG_REGISTERED 0x1
36 #define TEE_MAX_DEV_NAME_LEN 32
37
38 enum tee_dma_heap_id {
39 TEE_DMA_HEAP_SECURE_VIDEO_PLAY = 1,
40 TEE_DMA_HEAP_TRUSTED_UI,
41 TEE_DMA_HEAP_SECURE_VIDEO_RECORD,
42 };
43
44 /**
45 * struct tee_device - TEE Device representation
46 * @name: name of device
47 * @desc: description of device
48 * @id: unique id of device
49 * @flags: represented by TEE_DEVICE_FLAG_REGISTERED above
50 * @dev: embedded basic device structure
51 * @cdev: embedded cdev
52 * @num_users: number of active users of this device
53 * @c_no_user: completion used when unregistering the device
54 * @mutex: mutex protecting @num_users and @idr
55 * @idr: register of user space shared memory objects allocated or
56 * registered on this device
57 * @pool: shared memory pool
58 */
59 struct tee_device {
60 char name[TEE_MAX_DEV_NAME_LEN];
61 const struct tee_desc *desc;
62 int id;
63 unsigned int flags;
64
65 struct device dev;
66 struct cdev cdev;
67
68 size_t num_users;
69 struct completion c_no_users;
70 struct mutex mutex; /* protects num_users and idr */
71
72 struct idr idr;
73 struct tee_shm_pool *pool;
74 };
75
76 /**
77 * struct tee_driver_ops - driver operations vtable
78 * @get_version: returns version of driver
79 * @get_tee_revision: returns revision string (diagnostic only);
80 * do not infer feature support from this, use
81 * TEE_IOC_VERSION instead
82 * @open: called for a context when the device file is opened
83 * @close_context: called when the device file is closed
84 * @release: called to release the context
85 * @open_session: open a new session
86 * @close_session: close a session
87 * @system_session: declare session as a system session
88 * @invoke_func: invoke a trusted function
89 * @object_invoke_func: invoke a TEE object
90 * @cancel_req: request cancel of an ongoing invoke or open
91 * @supp_recv: called for supplicant to get a command
92 * @supp_send: called for supplicant to send a response
93 * @shm_register: register shared memory buffer in TEE
94 * @shm_unregister: unregister shared memory buffer in TEE
95 *
96 * The context given to @open might last longer than the device file if it is
97 * tied to other resources in the TEE driver. @close_context is called when the
98 * client closes the device file, even if there are existing references to the
99 * context. The TEE driver can use @close_context to start cleaning up.
100 */
101
102 struct tee_driver_ops {
103 void (*get_version)(struct tee_device *teedev,
104 struct tee_ioctl_version_data *vers);
105 int (*get_tee_revision)(struct tee_device *teedev,
106 char *buf, size_t len);
107 int (*open)(struct tee_context *ctx);
108 void (*close_context)(struct tee_context *ctx);
109 void (*release)(struct tee_context *ctx);
110 int (*open_session)(struct tee_context *ctx,
111 struct tee_ioctl_open_session_arg *arg,
112 struct tee_param *param);
113 int (*close_session)(struct tee_context *ctx, u32 session);
114 int (*system_session)(struct tee_context *ctx, u32 session);
115 int (*invoke_func)(struct tee_context *ctx,
116 struct tee_ioctl_invoke_arg *arg,
117 struct tee_param *param);
118 int (*object_invoke_func)(struct tee_context *ctx,
119 struct tee_ioctl_object_invoke_arg *arg,
120 struct tee_param *param);
121 int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
122 int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
123 struct tee_param *param);
124 int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
125 struct tee_param *param);
126 int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
127 struct page **pages, size_t num_pages,
128 unsigned long start);
129 int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
130 };
131
132 /* Size for TEE revision string buffer used by get_tee_revision(). */
133 #define TEE_REVISION_STR_SIZE 128
134
135 /**
136 * struct tee_desc - Describes the TEE driver to the subsystem
137 * @name: name of driver
138 * @ops: driver operations vtable
139 * @owner: module providing the driver
140 * @flags: Extra properties of driver, defined by TEE_DESC_* below
141 */
142 #define TEE_DESC_PRIVILEGED 0x1
143 struct tee_desc {
144 const char *name;
145 const struct tee_driver_ops *ops;
146 struct module *owner;
147 u32 flags;
148 };
149
150 /**
151 * struct tee_protmem_pool - protected memory pool
152 * @ops: operations
153 *
154 * This is an abstract interface where this struct is expected to be
155 * embedded in another struct specific to the implementation.
156 */
157 struct tee_protmem_pool {
158 const struct tee_protmem_pool_ops *ops;
159 };
160
161 /**
162 * struct tee_protmem_pool_ops - protected memory pool operations
163 * @alloc: called when allocating protected memory
164 * @free: called when freeing protected memory
165 * @update_shm: called when registering a dma-buf to update the @shm
166 * with physical address of the buffer or to return the
167 * @parent_shm of the memory pool
168 * @destroy_pool: called when destroying the pool
169 */
170 struct tee_protmem_pool_ops {
171 int (*alloc)(struct tee_protmem_pool *pool, struct sg_table *sgt,
172 size_t size, size_t *offs);
173 void (*free)(struct tee_protmem_pool *pool, struct sg_table *sgt);
174 int (*update_shm)(struct tee_protmem_pool *pool, struct sg_table *sgt,
175 size_t offs, struct tee_shm *shm,
176 struct tee_shm **parent_shm);
177 void (*destroy_pool)(struct tee_protmem_pool *pool);
178 };
179
180 /**
181 * tee_device_alloc() - Allocate a new struct tee_device instance
182 * @teedesc: Descriptor for this driver
183 * @dev: Parent device for this device
184 * @pool: Shared memory pool, NULL if not used
185 * @driver_data: Private driver data for this device
186 *
187 * Allocates a new struct tee_device instance. The device is
188 * removed by tee_device_unregister().
189 *
190 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
191 */
192 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
193 struct device *dev,
194 struct tee_shm_pool *pool,
195 void *driver_data);
196
197 /**
198 * tee_device_register() - Registers a TEE device
199 * @teedev: Device to register
200 *
201 * tee_device_unregister() need to be called to remove the @teedev if
202 * this function fails.
203 *
204 * @returns < 0 on failure
205 */
206 int tee_device_register(struct tee_device *teedev);
207
208 /**
209 * tee_device_unregister() - Removes a TEE device
210 * @teedev: Device to unregister
211 *
212 * This function should be called to remove the @teedev even if
213 * tee_device_register() hasn't been called yet. Does nothing if
214 * @teedev is NULL.
215 */
216 void tee_device_unregister(struct tee_device *teedev);
217
218 int tee_device_register_dma_heap(struct tee_device *teedev,
219 enum tee_dma_heap_id id,
220 struct tee_protmem_pool *pool);
221 void tee_device_put_all_dma_heaps(struct tee_device *teedev);
222
223 /**
224 * tee_device_get() - Increment the user count for a tee_device
225 * @teedev: Pointer to the tee_device
226 *
227 * If tee_device_unregister() has been called and the final user of @teedev
228 * has already released the device, this function will fail to prevent new users
229 * from accessing the device during the unregistration process.
230 *
231 * Returns: true if @teedev remains valid, otherwise false
232 */
233 bool tee_device_get(struct tee_device *teedev);
234
235 /**
236 * tee_device_put() - Decrease the user count for a tee_device
237 * @teedev: pointer to the tee_device
238 */
239 void tee_device_put(struct tee_device *teedev);
240
241 /**
242 * tee_device_set_dev_groups() - Set device attribute groups
243 * @teedev: Device to register
244 * @dev_groups: Attribute groups
245 *
246 * Assigns the provided @dev_groups to the @teedev to be registered later
247 * with tee_device_register(). Calling this function is optional, but if
248 * it's called it must be called before tee_device_register().
249 */
250 void tee_device_set_dev_groups(struct tee_device *teedev,
251 const struct attribute_group **dev_groups);
252
253 /**
254 * tee_session_calc_client_uuid() - Calculates client UUID for session
255 * @uuid: Resulting UUID
256 * @connection_method: Connection method for session (TEE_IOCTL_LOGIN_*)
257 * @connectuon_data: Connection data for opening session
258 *
259 * Based on connection method calculates UUIDv5 based client UUID.
260 *
261 * For group based logins verifies that calling process has specified
262 * credentials.
263 *
264 * @return < 0 on failure
265 */
266 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
267 const u8 connection_data[TEE_IOCTL_UUID_LEN]);
268
269 /**
270 * struct tee_shm_pool - shared memory pool
271 * @ops: operations
272 * @private_data: private data for the shared memory manager
273 */
274 struct tee_shm_pool {
275 const struct tee_shm_pool_ops *ops;
276 void *private_data;
277 };
278
279 /**
280 * struct tee_shm_pool_ops - shared memory pool operations
281 * @alloc: called when allocating shared memory
282 * @free: called when freeing shared memory
283 * @destroy_pool: called when destroying the pool
284 */
285 struct tee_shm_pool_ops {
286 int (*alloc)(struct tee_shm_pool *pool, struct tee_shm *shm,
287 size_t size, size_t align);
288 void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm);
289 void (*destroy_pool)(struct tee_shm_pool *pool);
290 };
291
292 /*
293 * tee_shm_pool_alloc_res_mem() - Create a shm manager for reserved memory
294 * @vaddr: Virtual address of start of pool
295 * @paddr: Physical address of start of pool
296 * @size: Size in bytes of the pool
297 *
298 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
299 */
300 struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr,
301 phys_addr_t paddr, size_t size,
302 int min_alloc_order);
303
304 /**
305 * tee_shm_pool_free() - Free a shared memory pool
306 * @pool: The shared memory pool to free
307 *
308 * The must be no remaining shared memory allocated from this pool when
309 * this function is called.
310 */
tee_shm_pool_free(struct tee_shm_pool * pool)311 static inline void tee_shm_pool_free(struct tee_shm_pool *pool)
312 {
313 pool->ops->destroy_pool(pool);
314 }
315
316 /**
317 * tee_protmem_static_pool_alloc() - Create a protected memory manager
318 * @paddr: Physical address of start of pool
319 * @size: Size in bytes of the pool
320 *
321 * @returns pointer to a 'struct tee_protmem_pool' or an ERR_PTR on failure.
322 */
323 struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr,
324 size_t size);
325
326 /**
327 * tee_get_drvdata() - Return driver_data pointer
328 * @returns the driver_data pointer supplied to tee_register().
329 */
330 void *tee_get_drvdata(struct tee_device *teedev);
331
332 /**
333 * tee_shm_alloc_priv_buf() - Allocate shared memory for private use by specific
334 * TEE driver
335 * @ctx: The TEE context for shared memory allocation
336 * @size: Shared memory allocation size
337 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
338 */
339 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
340
341 struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx,
342 size_t page_count);
343
344 int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
345 int (*shm_register)(struct tee_context *ctx,
346 struct tee_shm *shm,
347 struct page **pages,
348 size_t num_pages,
349 unsigned long start));
350 void tee_dyn_shm_free_helper(struct tee_shm *shm,
351 int (*shm_unregister)(struct tee_context *ctx,
352 struct tee_shm *shm));
353
354 /**
355 * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
356 * @shm: Shared memory handle
357 * @returns true if object is dynamic shared memory
358 */
tee_shm_is_dynamic(struct tee_shm * shm)359 static inline bool tee_shm_is_dynamic(struct tee_shm *shm)
360 {
361 return shm && (shm->flags & TEE_SHM_DYNAMIC);
362 }
363
364 /**
365 * tee_shm_put() - Decrease reference count on a shared memory handle
366 * @shm: Shared memory handle
367 */
368 void tee_shm_put(struct tee_shm *shm);
369
370 /**
371 * tee_shm_get_id() - Get id of a shared memory object
372 * @shm: Shared memory handle
373 * @returns id
374 */
tee_shm_get_id(struct tee_shm * shm)375 static inline int tee_shm_get_id(struct tee_shm *shm)
376 {
377 return shm->id;
378 }
379
380 /**
381 * tee_shm_get_from_id() - Find shared memory object and increase reference
382 * count
383 * @ctx: Context owning the shared memory
384 * @id: Id of shared memory object
385 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
386 */
387 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
388
tee_param_is_memref(struct tee_param * param)389 static inline bool tee_param_is_memref(struct tee_param *param)
390 {
391 switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
392 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
393 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
394 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
395 return true;
396 default:
397 return false;
398 }
399 }
400
401 /**
402 * teedev_open() - Open a struct tee_device
403 * @teedev: Device to open
404 *
405 * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
406 */
407 struct tee_context *teedev_open(struct tee_device *teedev);
408
409 /**
410 * teedev_close_context() - closes a struct tee_context
411 * @ctx: The struct tee_context to close
412 */
413 void teedev_close_context(struct tee_context *ctx);
414
415 /**
416 * teedev_ctx_get() - Increment the reference count of a context
417 * @ctx: Pointer to the context
418 *
419 * This function increases the refcount of the context, which is tied to
420 * resources shared by the same tee_device. During the unregistration process,
421 * the context may remain valid even after tee_device_unregister() has returned.
422 *
423 * Users should ensure that the context's refcount is properly decreased before
424 * calling tee_device_put(), typically within the context's release() function.
425 * Alternatively, users can call tee_device_get() and teedev_ctx_get() together
426 * and release them simultaneously (see shm_alloc_helper()).
427 */
428 void teedev_ctx_get(struct tee_context *ctx);
429
430 /**
431 * teedev_ctx_put() - Decrease reference count on a context
432 * @ctx: pointer to the context
433 */
434 void teedev_ctx_put(struct tee_context *ctx);
435
436 #endif /*__TEE_CORE_H*/
437