Linux Audio

Check our new training course

Loading...
v5.14.15
  1/* SPDX-License-Identifier: GPL-2.0-only */
  2/*
  3 * Copyright (c) 2015-2016, Linaro Limited
  4 */
  5
  6#ifndef __TEE_DRV_H
  7#define __TEE_DRV_H
  8
  9#include <linux/device.h>
 10#include <linux/idr.h>
 11#include <linux/kref.h>
 12#include <linux/list.h>
 13#include <linux/mod_devicetable.h>
 14#include <linux/tee.h>
 15#include <linux/types.h>
 16#include <linux/uuid.h>
 17
 18/*
 19 * The file describes the API provided by the generic TEE driver to the
 20 * specific TEE driver.
 21 */
 22
 23#define TEE_SHM_MAPPED		BIT(0)	/* Memory mapped by the kernel */
 24#define TEE_SHM_DMA_BUF		BIT(1)	/* Memory with dma-buf handle */
 25#define TEE_SHM_EXT_DMA_BUF	BIT(2)	/* Memory with dma-buf handle */
 26#define TEE_SHM_REGISTER	BIT(3)  /* Memory registered in secure world */
 27#define TEE_SHM_USER_MAPPED	BIT(4)  /* Memory mapped in user space */
 28#define TEE_SHM_POOL		BIT(5)  /* Memory allocated from pool */
 29#define TEE_SHM_KERNEL_MAPPED	BIT(6)  /* Memory mapped in kernel space */
 30#define TEE_SHM_PRIV		BIT(7)  /* Memory private to TEE driver */
 31
 32struct device;
 33struct tee_device;
 34struct tee_shm;
 35struct tee_shm_pool;
 36
 37/**
 38 * struct tee_context - driver specific context on file pointer data
 39 * @teedev:	pointer to this drivers struct tee_device
 40 * @list_shm:	List of shared memory object owned by this context
 41 * @data:	driver specific context data, managed by the driver
 42 * @refcount:	reference counter for this structure
 43 * @releasing:  flag that indicates if context is being released right now.
 44 *		It is needed to break circular dependency on context during
 45 *              shared memory release.
 46 * @supp_nowait: flag that indicates that requests in this context should not
 47 *              wait for tee-supplicant daemon to be started if not present
 48 *              and just return with an error code. It is needed for requests
 49 *              that arises from TEE based kernel drivers that should be
 50 *              non-blocking in nature.
 51 * @cap_memref_null: flag indicating if the TEE Client support shared
 52 *                   memory buffer with a NULL pointer.
 53 */
 54struct tee_context {
 55	struct tee_device *teedev;
 56	void *data;
 57	struct kref refcount;
 58	bool releasing;
 59	bool supp_nowait;
 60	bool cap_memref_null;
 61};
 62
 63struct tee_param_memref {
 64	size_t shm_offs;
 65	size_t size;
 66	struct tee_shm *shm;
 67};
 68
 69struct tee_param_value {
 70	u64 a;
 71	u64 b;
 72	u64 c;
 73};
 74
 75struct tee_param {
 76	u64 attr;
 77	union {
 78		struct tee_param_memref memref;
 79		struct tee_param_value value;
 80	} u;
 81};
 82
 83/**
 84 * struct tee_driver_ops - driver operations vtable
 85 * @get_version:	returns version of driver
 86 * @open:		called when the device file is opened
 87 * @release:		release this open file
 88 * @open_session:	open a new session
 89 * @close_session:	close a session
 
 90 * @invoke_func:	invoke a trusted function
 91 * @cancel_req:		request cancel of an ongoing invoke or open
 92 * @supp_recv:		called for supplicant to get a command
 93 * @supp_send:		called for supplicant to send a response
 94 * @shm_register:	register shared memory buffer in TEE
 95 * @shm_unregister:	unregister shared memory buffer in TEE
 96 */
 97struct tee_driver_ops {
 98	void (*get_version)(struct tee_device *teedev,
 99			    struct tee_ioctl_version_data *vers);
100	int (*open)(struct tee_context *ctx);
101	void (*release)(struct tee_context *ctx);
102	int (*open_session)(struct tee_context *ctx,
103			    struct tee_ioctl_open_session_arg *arg,
104			    struct tee_param *param);
105	int (*close_session)(struct tee_context *ctx, u32 session);
 
106	int (*invoke_func)(struct tee_context *ctx,
107			   struct tee_ioctl_invoke_arg *arg,
108			   struct tee_param *param);
109	int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
110	int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
111			 struct tee_param *param);
112	int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
113			 struct tee_param *param);
114	int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
115			    struct page **pages, size_t num_pages,
116			    unsigned long start);
117	int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
118};
119
120/**
121 * struct tee_desc - Describes the TEE driver to the subsystem
122 * @name:	name of driver
123 * @ops:	driver operations vtable
124 * @owner:	module providing the driver
125 * @flags:	Extra properties of driver, defined by TEE_DESC_* below
126 */
127#define TEE_DESC_PRIVILEGED	0x1
128struct tee_desc {
129	const char *name;
130	const struct tee_driver_ops *ops;
131	struct module *owner;
132	u32 flags;
133};
134
135/**
136 * tee_device_alloc() - Allocate a new struct tee_device instance
137 * @teedesc:	Descriptor for this driver
138 * @dev:	Parent device for this device
139 * @pool:	Shared memory pool, NULL if not used
140 * @driver_data: Private driver data for this device
141 *
142 * Allocates a new struct tee_device instance. The device is
143 * removed by tee_device_unregister().
144 *
145 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
146 */
147struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
148				    struct device *dev,
149				    struct tee_shm_pool *pool,
150				    void *driver_data);
151
152/**
153 * tee_device_register() - Registers a TEE device
154 * @teedev:	Device to register
155 *
156 * tee_device_unregister() need to be called to remove the @teedev if
157 * this function fails.
158 *
159 * @returns < 0 on failure
160 */
161int tee_device_register(struct tee_device *teedev);
162
163/**
164 * tee_device_unregister() - Removes a TEE device
165 * @teedev:	Device to unregister
166 *
167 * This function should be called to remove the @teedev even if
168 * tee_device_register() hasn't been called yet. Does nothing if
169 * @teedev is NULL.
170 */
171void tee_device_unregister(struct tee_device *teedev);
172
173/**
174 * tee_session_calc_client_uuid() - Calculates client UUID for session
175 * @uuid:		Resulting UUID
176 * @connection_method:	Connection method for session (TEE_IOCTL_LOGIN_*)
177 * @connectuon_data:	Connection data for opening session
178 *
179 * Based on connection method calculates UUIDv5 based client UUID.
180 *
181 * For group based logins verifies that calling process has specified
182 * credentials.
183 *
184 * @return < 0 on failure
185 */
186int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
187				 const u8 connection_data[TEE_IOCTL_UUID_LEN]);
188
189/**
190 * struct tee_shm - shared memory object
191 * @ctx:	context using the object
192 * @paddr:	physical address of the shared memory
193 * @kaddr:	virtual address of the shared memory
194 * @size:	size of shared memory
195 * @offset:	offset of buffer in user space
196 * @pages:	locked pages from userspace
197 * @num_pages:	number of locked pages
198 * @dmabuf:	dmabuf used to for exporting to user space
199 * @flags:	defined by TEE_SHM_* in tee_drv.h
200 * @id:		unique id of a shared memory object on this device
 
 
 
 
201 *
202 * This pool is only supposed to be accessed directly from the TEE
203 * subsystem and from drivers that implements their own shm pool manager.
204 */
205struct tee_shm {
206	struct tee_context *ctx;
207	phys_addr_t paddr;
208	void *kaddr;
209	size_t size;
210	unsigned int offset;
211	struct page **pages;
212	size_t num_pages;
213	struct dma_buf *dmabuf;
214	u32 flags;
215	int id;
 
216};
217
218/**
219 * struct tee_shm_pool_mgr - shared memory manager
220 * @ops:		operations
221 * @private_data:	private data for the shared memory manager
222 */
223struct tee_shm_pool_mgr {
224	const struct tee_shm_pool_mgr_ops *ops;
225	void *private_data;
226};
227
228/**
229 * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
230 * @alloc:		called when allocating shared memory
231 * @free:		called when freeing shared memory
232 * @destroy_poolmgr:	called when destroying the pool manager
233 */
234struct tee_shm_pool_mgr_ops {
235	int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
236		     size_t size);
237	void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
238	void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr);
239};
240
241/**
242 * tee_shm_pool_alloc() - Create a shared memory pool from shm managers
243 * @priv_mgr:	manager for driver private shared memory allocations
244 * @dmabuf_mgr:	manager for dma-buf shared memory allocations
245 *
246 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
247 * in @dmabuf, others will use the range provided by @priv.
248 *
249 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
250 */
251struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
252					struct tee_shm_pool_mgr *dmabuf_mgr);
253
254/*
255 * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved
256 * memory
257 * @vaddr:	Virtual address of start of pool
258 * @paddr:	Physical address of start of pool
259 * @size:	Size in bytes of the pool
260 *
261 * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failure.
262 */
263struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
264							phys_addr_t paddr,
265							size_t size,
266							int min_alloc_order);
267
268/**
269 * tee_shm_pool_mgr_destroy() - Free a shared memory manager
270 */
271static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm)
272{
273	poolm->ops->destroy_poolmgr(poolm);
274}
275
276/**
277 * struct tee_shm_pool_mem_info - holds information needed to create a shared
278 * memory pool
279 * @vaddr:	Virtual address of start of pool
280 * @paddr:	Physical address of start of pool
281 * @size:	Size in bytes of the pool
282 */
283struct tee_shm_pool_mem_info {
284	unsigned long vaddr;
285	phys_addr_t paddr;
286	size_t size;
287};
288
289/**
290 * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
291 * memory range
292 * @priv_info:	 Information for driver private shared memory pool
293 * @dmabuf_info: Information for dma-buf shared memory pool
294 *
295 * Start and end of pools will must be page aligned.
296 *
297 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
298 * in @dmabuf, others will use the range provided by @priv.
299 *
300 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
301 */
302struct tee_shm_pool *
303tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
304			   struct tee_shm_pool_mem_info *dmabuf_info);
305
306/**
307 * tee_shm_pool_free() - Free a shared memory pool
308 * @pool:	The shared memory pool to free
309 *
310 * The must be no remaining shared memory allocated from this pool when
311 * this function is called.
312 */
313void tee_shm_pool_free(struct tee_shm_pool *pool);
 
 
 
314
315/**
316 * tee_get_drvdata() - Return driver_data pointer
317 * @returns the driver_data pointer supplied to tee_register().
318 */
319void *tee_get_drvdata(struct tee_device *teedev);
320
321/**
322 * tee_shm_alloc() - Allocate shared memory
323 * @ctx:	Context that allocates the shared memory
324 * @size:	Requested size of shared memory
325 * @flags:	Flags setting properties for the requested shared memory.
326 *
327 * Memory allocated as global shared memory is automatically freed when the
328 * TEE file pointer is closed. The @flags field uses the bits defined by
329 * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
330 * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
331 * with a dma-buf handle, else driver private memory.
332 *
333 * @returns a pointer to 'struct tee_shm'
334 */
335struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
336struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);
337
338/**
339 * tee_shm_register() - Register shared memory buffer
340 * @ctx:	Context that registers the shared memory
341 * @addr:	Address is userspace of the shared buffer
342 * @length:	Length of the shared buffer
343 * @flags:	Flags setting properties for the requested shared memory.
344 *
345 * @returns a pointer to 'struct tee_shm'
346 */
347struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
348				 size_t length, u32 flags);
349
350/**
351 * tee_shm_is_registered() - Check if shared memory object in registered in TEE
352 * @shm:	Shared memory handle
353 * @returns true if object is registered in TEE
354 */
355static inline bool tee_shm_is_registered(struct tee_shm *shm)
356{
357	return shm && (shm->flags & TEE_SHM_REGISTER);
358}
359
360/**
361 * tee_shm_free() - Free shared memory
362 * @shm:	Handle to shared memory to free
363 */
364void tee_shm_free(struct tee_shm *shm);
365
366/**
367 * tee_shm_put() - Decrease reference count on a shared memory handle
368 * @shm:	Shared memory handle
369 */
370void tee_shm_put(struct tee_shm *shm);
371
372/**
373 * tee_shm_va2pa() - Get physical address of a virtual address
374 * @shm:	Shared memory handle
375 * @va:		Virtual address to tranlsate
376 * @pa:		Returned physical address
377 * @returns 0 on success and < 0 on failure
378 */
379int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
380
381/**
382 * tee_shm_pa2va() - Get virtual address of a physical address
383 * @shm:	Shared memory handle
384 * @pa:		Physical address to tranlsate
385 * @va:		Returned virtual address
386 * @returns 0 on success and < 0 on failure
387 */
388int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
389
390/**
391 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
392 * @shm:	Shared memory handle
393 * @offs:	Offset from start of this shared memory
394 * @returns virtual address of the shared memory + offs if offs is within
395 *	the bounds of this shared memory, else an ERR_PTR
396 */
397void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
398
399/**
400 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
401 * @shm:	Shared memory handle
402 * @offs:	Offset from start of this shared memory
403 * @pa:		Physical address to return
404 * @returns 0 if offs is within the bounds of this shared memory, else an
405 *	error code.
406 */
407int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
408
409/**
410 * tee_shm_get_size() - Get size of shared memory buffer
411 * @shm:	Shared memory handle
412 * @returns size of shared memory
413 */
414static inline size_t tee_shm_get_size(struct tee_shm *shm)
415{
416	return shm->size;
417}
418
419/**
420 * tee_shm_get_pages() - Get list of pages that hold shared buffer
421 * @shm:	Shared memory handle
422 * @num_pages:	Number of pages will be stored there
423 * @returns pointer to pages array
424 */
425static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
426					      size_t *num_pages)
427{
428	*num_pages = shm->num_pages;
429	return shm->pages;
430}
431
432/**
433 * tee_shm_get_page_offset() - Get shared buffer offset from page start
434 * @shm:	Shared memory handle
435 * @returns page offset of shared buffer
436 */
437static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
438{
439	return shm->offset;
440}
441
442/**
443 * tee_shm_get_id() - Get id of a shared memory object
444 * @shm:	Shared memory handle
445 * @returns id
446 */
447static inline int tee_shm_get_id(struct tee_shm *shm)
448{
449	return shm->id;
450}
451
452/**
453 * tee_shm_get_from_id() - Find shared memory object and increase reference
454 * count
455 * @ctx:	Context owning the shared memory
456 * @id:		Id of shared memory object
457 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
458 */
459struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
460
461/**
462 * tee_client_open_context() - Open a TEE context
463 * @start:	if not NULL, continue search after this context
464 * @match:	function to check TEE device
465 * @data:	data for match function
466 * @vers:	if not NULL, version data of TEE device of the context returned
467 *
468 * This function does an operation similar to open("/dev/teeX") in user space.
469 * A returned context must be released with tee_client_close_context().
470 *
471 * Returns a TEE context of the first TEE device matched by the match()
472 * callback or an ERR_PTR.
473 */
474struct tee_context *
475tee_client_open_context(struct tee_context *start,
476			int (*match)(struct tee_ioctl_version_data *,
477				     const void *),
478			const void *data, struct tee_ioctl_version_data *vers);
479
480/**
481 * tee_client_close_context() - Close a TEE context
482 * @ctx:	TEE context to close
483 *
484 * Note that all sessions previously opened with this context will be
485 * closed when this function is called.
486 */
487void tee_client_close_context(struct tee_context *ctx);
488
489/**
490 * tee_client_get_version() - Query version of TEE
491 * @ctx:	TEE context to TEE to query
492 * @vers:	Pointer to version data
493 */
494void tee_client_get_version(struct tee_context *ctx,
495			    struct tee_ioctl_version_data *vers);
496
497/**
498 * tee_client_open_session() - Open a session to a Trusted Application
499 * @ctx:	TEE context
500 * @arg:	Open session arguments, see description of
501 *		struct tee_ioctl_open_session_arg
502 * @param:	Parameters passed to the Trusted Application
503 *
504 * Returns < 0 on error else see @arg->ret for result. If @arg->ret
505 * is TEEC_SUCCESS the session identifier is available in @arg->session.
506 */
507int tee_client_open_session(struct tee_context *ctx,
508			    struct tee_ioctl_open_session_arg *arg,
509			    struct tee_param *param);
510
511/**
512 * tee_client_close_session() - Close a session to a Trusted Application
513 * @ctx:	TEE Context
514 * @session:	Session id
515 *
516 * Return < 0 on error else 0, regardless the session will not be
517 * valid after this function has returned.
518 */
519int tee_client_close_session(struct tee_context *ctx, u32 session);
520
521/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522 * tee_client_invoke_func() - Invoke a function in a Trusted Application
523 * @ctx:	TEE Context
524 * @arg:	Invoke arguments, see description of
525 *		struct tee_ioctl_invoke_arg
526 * @param:	Parameters passed to the Trusted Application
527 *
528 * Returns < 0 on error else see @arg->ret for result.
529 */
530int tee_client_invoke_func(struct tee_context *ctx,
531			   struct tee_ioctl_invoke_arg *arg,
532			   struct tee_param *param);
533
534/**
535 * tee_client_cancel_req() - Request cancellation of the previous open-session
536 * or invoke-command operations in a Trusted Application
537 * @ctx:       TEE Context
538 * @arg:       Cancellation arguments, see description of
539 *             struct tee_ioctl_cancel_arg
540 *
541 * Returns < 0 on error else 0 if the cancellation was successfully requested.
542 */
543int tee_client_cancel_req(struct tee_context *ctx,
544			  struct tee_ioctl_cancel_arg *arg);
545
546static inline bool tee_param_is_memref(struct tee_param *param)
547{
548	switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
549	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
550	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
551	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
552		return true;
553	default:
554		return false;
555	}
556}
557
558extern struct bus_type tee_bus_type;
559
560/**
561 * struct tee_client_device - tee based device
562 * @id:			device identifier
563 * @dev:		device structure
564 */
565struct tee_client_device {
566	struct tee_client_device_id id;
567	struct device dev;
568};
569
570#define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
571
572/**
573 * struct tee_client_driver - tee client driver
574 * @id_table:		device id table supported by this driver
575 * @driver:		driver structure
576 */
577struct tee_client_driver {
578	const struct tee_client_device_id *id_table;
579	struct device_driver driver;
580};
581
582#define to_tee_client_driver(d) \
583		container_of(d, struct tee_client_driver, driver)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
585#endif /*__TEE_DRV_H*/
v6.9.4
  1/* SPDX-License-Identifier: GPL-2.0-only */
  2/*
  3 * Copyright (c) 2015-2022 Linaro Limited
  4 */
  5
  6#ifndef __TEE_DRV_H
  7#define __TEE_DRV_H
  8
  9#include <linux/device.h>
 10#include <linux/idr.h>
 11#include <linux/kref.h>
 12#include <linux/list.h>
 13#include <linux/mod_devicetable.h>
 14#include <linux/tee.h>
 15#include <linux/types.h>
 16#include <linux/uuid.h>
 17
 18/*
 19 * The file describes the API provided by the generic TEE driver to the
 20 * specific TEE driver.
 21 */
 22
 23#define TEE_SHM_DYNAMIC		BIT(0)  /* Dynamic shared memory registered */
 24					/* in secure world */
 25#define TEE_SHM_USER_MAPPED	BIT(1)  /* Memory mapped in user space */
 26#define TEE_SHM_POOL		BIT(2)  /* Memory allocated from pool */
 27#define TEE_SHM_PRIV		BIT(3)  /* Memory private to TEE driver */
 
 
 
 28
 29struct device;
 30struct tee_device;
 31struct tee_shm;
 32struct tee_shm_pool;
 33
 34/**
 35 * struct tee_context - driver specific context on file pointer data
 36 * @teedev:	pointer to this drivers struct tee_device
 37 * @list_shm:	List of shared memory object owned by this context
 38 * @data:	driver specific context data, managed by the driver
 39 * @refcount:	reference counter for this structure
 40 * @releasing:  flag that indicates if context is being released right now.
 41 *		It is needed to break circular dependency on context during
 42 *              shared memory release.
 43 * @supp_nowait: flag that indicates that requests in this context should not
 44 *              wait for tee-supplicant daemon to be started if not present
 45 *              and just return with an error code. It is needed for requests
 46 *              that arises from TEE based kernel drivers that should be
 47 *              non-blocking in nature.
 48 * @cap_memref_null: flag indicating if the TEE Client support shared
 49 *                   memory buffer with a NULL pointer.
 50 */
 51struct tee_context {
 52	struct tee_device *teedev;
 53	void *data;
 54	struct kref refcount;
 55	bool releasing;
 56	bool supp_nowait;
 57	bool cap_memref_null;
 58};
 59
 60struct tee_param_memref {
 61	size_t shm_offs;
 62	size_t size;
 63	struct tee_shm *shm;
 64};
 65
 66struct tee_param_value {
 67	u64 a;
 68	u64 b;
 69	u64 c;
 70};
 71
 72struct tee_param {
 73	u64 attr;
 74	union {
 75		struct tee_param_memref memref;
 76		struct tee_param_value value;
 77	} u;
 78};
 79
 80/**
 81 * struct tee_driver_ops - driver operations vtable
 82 * @get_version:	returns version of driver
 83 * @open:		called when the device file is opened
 84 * @release:		release this open file
 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 * @cancel_req:		request cancel of an ongoing invoke or open
 90 * @supp_recv:		called for supplicant to get a command
 91 * @supp_send:		called for supplicant to send a response
 92 * @shm_register:	register shared memory buffer in TEE
 93 * @shm_unregister:	unregister shared memory buffer in TEE
 94 */
 95struct tee_driver_ops {
 96	void (*get_version)(struct tee_device *teedev,
 97			    struct tee_ioctl_version_data *vers);
 98	int (*open)(struct tee_context *ctx);
 99	void (*release)(struct tee_context *ctx);
100	int (*open_session)(struct tee_context *ctx,
101			    struct tee_ioctl_open_session_arg *arg,
102			    struct tee_param *param);
103	int (*close_session)(struct tee_context *ctx, u32 session);
104	int (*system_session)(struct tee_context *ctx, u32 session);
105	int (*invoke_func)(struct tee_context *ctx,
106			   struct tee_ioctl_invoke_arg *arg,
107			   struct tee_param *param);
108	int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
109	int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
110			 struct tee_param *param);
111	int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
112			 struct tee_param *param);
113	int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
114			    struct page **pages, size_t num_pages,
115			    unsigned long start);
116	int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
117};
118
119/**
120 * struct tee_desc - Describes the TEE driver to the subsystem
121 * @name:	name of driver
122 * @ops:	driver operations vtable
123 * @owner:	module providing the driver
124 * @flags:	Extra properties of driver, defined by TEE_DESC_* below
125 */
126#define TEE_DESC_PRIVILEGED	0x1
127struct tee_desc {
128	const char *name;
129	const struct tee_driver_ops *ops;
130	struct module *owner;
131	u32 flags;
132};
133
134/**
135 * tee_device_alloc() - Allocate a new struct tee_device instance
136 * @teedesc:	Descriptor for this driver
137 * @dev:	Parent device for this device
138 * @pool:	Shared memory pool, NULL if not used
139 * @driver_data: Private driver data for this device
140 *
141 * Allocates a new struct tee_device instance. The device is
142 * removed by tee_device_unregister().
143 *
144 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
145 */
146struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
147				    struct device *dev,
148				    struct tee_shm_pool *pool,
149				    void *driver_data);
150
151/**
152 * tee_device_register() - Registers a TEE device
153 * @teedev:	Device to register
154 *
155 * tee_device_unregister() need to be called to remove the @teedev if
156 * this function fails.
157 *
158 * @returns < 0 on failure
159 */
160int tee_device_register(struct tee_device *teedev);
161
162/**
163 * tee_device_unregister() - Removes a TEE device
164 * @teedev:	Device to unregister
165 *
166 * This function should be called to remove the @teedev even if
167 * tee_device_register() hasn't been called yet. Does nothing if
168 * @teedev is NULL.
169 */
170void tee_device_unregister(struct tee_device *teedev);
171
172/**
173 * tee_session_calc_client_uuid() - Calculates client UUID for session
174 * @uuid:		Resulting UUID
175 * @connection_method:	Connection method for session (TEE_IOCTL_LOGIN_*)
176 * @connectuon_data:	Connection data for opening session
177 *
178 * Based on connection method calculates UUIDv5 based client UUID.
179 *
180 * For group based logins verifies that calling process has specified
181 * credentials.
182 *
183 * @return < 0 on failure
184 */
185int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
186				 const u8 connection_data[TEE_IOCTL_UUID_LEN]);
187
188/**
189 * struct tee_shm - shared memory object
190 * @ctx:	context using the object
191 * @paddr:	physical address of the shared memory
192 * @kaddr:	virtual address of the shared memory
193 * @size:	size of shared memory
194 * @offset:	offset of buffer in user space
195 * @pages:	locked pages from userspace
196 * @num_pages:	number of locked pages
197 * @refcount:	reference counter
198 * @flags:	defined by TEE_SHM_* in tee_drv.h
199 * @id:		unique id of a shared memory object on this device, shared
200 *		with user space
201 * @sec_world_id:
202 *		secure world assigned id of this shared memory object, not
203 *		used by all drivers
204 *
205 * This pool is only supposed to be accessed directly from the TEE
206 * subsystem and from drivers that implements their own shm pool manager.
207 */
208struct tee_shm {
209	struct tee_context *ctx;
210	phys_addr_t paddr;
211	void *kaddr;
212	size_t size;
213	unsigned int offset;
214	struct page **pages;
215	size_t num_pages;
216	refcount_t refcount;
217	u32 flags;
218	int id;
219	u64 sec_world_id;
220};
221
222/**
223 * struct tee_shm_pool - shared memory pool
224 * @ops:		operations
225 * @private_data:	private data for the shared memory manager
226 */
227struct tee_shm_pool {
228	const struct tee_shm_pool_ops *ops;
229	void *private_data;
230};
231
232/**
233 * struct tee_shm_pool_ops - shared memory pool operations
234 * @alloc:		called when allocating shared memory
235 * @free:		called when freeing shared memory
236 * @destroy_pool:	called when destroying the pool
237 */
238struct tee_shm_pool_ops {
239	int (*alloc)(struct tee_shm_pool *pool, struct tee_shm *shm,
240		     size_t size, size_t align);
241	void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm);
242	void (*destroy_pool)(struct tee_shm_pool *pool);
243};
244
 
 
 
 
 
 
 
 
 
 
 
 
 
245/*
246 * tee_shm_pool_alloc_res_mem() - Create a shm manager for reserved memory
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247 * @vaddr:	Virtual address of start of pool
248 * @paddr:	Physical address of start of pool
249 * @size:	Size in bytes of the pool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250 *
251 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
252 */
253struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr,
254						phys_addr_t paddr, size_t size,
255						int min_alloc_order);
256
257/**
258 * tee_shm_pool_free() - Free a shared memory pool
259 * @pool:	The shared memory pool to free
260 *
261 * The must be no remaining shared memory allocated from this pool when
262 * this function is called.
263 */
264static inline void tee_shm_pool_free(struct tee_shm_pool *pool)
265{
266	pool->ops->destroy_pool(pool);
267}
268
269/**
270 * tee_get_drvdata() - Return driver_data pointer
271 * @returns the driver_data pointer supplied to tee_register().
272 */
273void *tee_get_drvdata(struct tee_device *teedev);
274
275struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);
277
278struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
279					    void *addr, size_t length);
 
 
 
 
 
 
 
 
 
280
281/**
282 * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
283 * @shm:	Shared memory handle
284 * @returns true if object is dynamic shared memory
285 */
286static inline bool tee_shm_is_dynamic(struct tee_shm *shm)
287{
288	return shm && (shm->flags & TEE_SHM_DYNAMIC);
289}
290
291/**
292 * tee_shm_free() - Free shared memory
293 * @shm:	Handle to shared memory to free
294 */
295void tee_shm_free(struct tee_shm *shm);
296
297/**
298 * tee_shm_put() - Decrease reference count on a shared memory handle
299 * @shm:	Shared memory handle
300 */
301void tee_shm_put(struct tee_shm *shm);
302
303/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
304 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
305 * @shm:	Shared memory handle
306 * @offs:	Offset from start of this shared memory
307 * @returns virtual address of the shared memory + offs if offs is within
308 *	the bounds of this shared memory, else an ERR_PTR
309 */
310void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
311
312/**
313 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
314 * @shm:	Shared memory handle
315 * @offs:	Offset from start of this shared memory
316 * @pa:		Physical address to return
317 * @returns 0 if offs is within the bounds of this shared memory, else an
318 *	error code.
319 */
320int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
321
322/**
323 * tee_shm_get_size() - Get size of shared memory buffer
324 * @shm:	Shared memory handle
325 * @returns size of shared memory
326 */
327static inline size_t tee_shm_get_size(struct tee_shm *shm)
328{
329	return shm->size;
330}
331
332/**
333 * tee_shm_get_pages() - Get list of pages that hold shared buffer
334 * @shm:	Shared memory handle
335 * @num_pages:	Number of pages will be stored there
336 * @returns pointer to pages array
337 */
338static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
339					      size_t *num_pages)
340{
341	*num_pages = shm->num_pages;
342	return shm->pages;
343}
344
345/**
346 * tee_shm_get_page_offset() - Get shared buffer offset from page start
347 * @shm:	Shared memory handle
348 * @returns page offset of shared buffer
349 */
350static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
351{
352	return shm->offset;
353}
354
355/**
356 * tee_shm_get_id() - Get id of a shared memory object
357 * @shm:	Shared memory handle
358 * @returns id
359 */
360static inline int tee_shm_get_id(struct tee_shm *shm)
361{
362	return shm->id;
363}
364
365/**
366 * tee_shm_get_from_id() - Find shared memory object and increase reference
367 * count
368 * @ctx:	Context owning the shared memory
369 * @id:		Id of shared memory object
370 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
371 */
372struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
373
374/**
375 * tee_client_open_context() - Open a TEE context
376 * @start:	if not NULL, continue search after this context
377 * @match:	function to check TEE device
378 * @data:	data for match function
379 * @vers:	if not NULL, version data of TEE device of the context returned
380 *
381 * This function does an operation similar to open("/dev/teeX") in user space.
382 * A returned context must be released with tee_client_close_context().
383 *
384 * Returns a TEE context of the first TEE device matched by the match()
385 * callback or an ERR_PTR.
386 */
387struct tee_context *
388tee_client_open_context(struct tee_context *start,
389			int (*match)(struct tee_ioctl_version_data *,
390				     const void *),
391			const void *data, struct tee_ioctl_version_data *vers);
392
393/**
394 * tee_client_close_context() - Close a TEE context
395 * @ctx:	TEE context to close
396 *
397 * Note that all sessions previously opened with this context will be
398 * closed when this function is called.
399 */
400void tee_client_close_context(struct tee_context *ctx);
401
402/**
403 * tee_client_get_version() - Query version of TEE
404 * @ctx:	TEE context to TEE to query
405 * @vers:	Pointer to version data
406 */
407void tee_client_get_version(struct tee_context *ctx,
408			    struct tee_ioctl_version_data *vers);
409
410/**
411 * tee_client_open_session() - Open a session to a Trusted Application
412 * @ctx:	TEE context
413 * @arg:	Open session arguments, see description of
414 *		struct tee_ioctl_open_session_arg
415 * @param:	Parameters passed to the Trusted Application
416 *
417 * Returns < 0 on error else see @arg->ret for result. If @arg->ret
418 * is TEEC_SUCCESS the session identifier is available in @arg->session.
419 */
420int tee_client_open_session(struct tee_context *ctx,
421			    struct tee_ioctl_open_session_arg *arg,
422			    struct tee_param *param);
423
424/**
425 * tee_client_close_session() - Close a session to a Trusted Application
426 * @ctx:	TEE Context
427 * @session:	Session id
428 *
429 * Return < 0 on error else 0, regardless the session will not be
430 * valid after this function has returned.
431 */
432int tee_client_close_session(struct tee_context *ctx, u32 session);
433
434/**
435 * tee_client_system_session() - Declare session as a system session
436 * @ctx:	TEE Context
437 * @session:	Session id
438 *
439 * This function requests TEE to provision an entry context ready to use for
440 * that session only. The provisioned entry context is used for command
441 * invocation and session closure, not for command cancelling requests.
442 * TEE releases the provisioned context upon session closure.
443 *
444 * Return < 0 on error else 0 if an entry context has been provisioned.
445 */
446int tee_client_system_session(struct tee_context *ctx, u32 session);
447
448/**
449 * tee_client_invoke_func() - Invoke a function in a Trusted Application
450 * @ctx:	TEE Context
451 * @arg:	Invoke arguments, see description of
452 *		struct tee_ioctl_invoke_arg
453 * @param:	Parameters passed to the Trusted Application
454 *
455 * Returns < 0 on error else see @arg->ret for result.
456 */
457int tee_client_invoke_func(struct tee_context *ctx,
458			   struct tee_ioctl_invoke_arg *arg,
459			   struct tee_param *param);
460
461/**
462 * tee_client_cancel_req() - Request cancellation of the previous open-session
463 * or invoke-command operations in a Trusted Application
464 * @ctx:       TEE Context
465 * @arg:       Cancellation arguments, see description of
466 *             struct tee_ioctl_cancel_arg
467 *
468 * Returns < 0 on error else 0 if the cancellation was successfully requested.
469 */
470int tee_client_cancel_req(struct tee_context *ctx,
471			  struct tee_ioctl_cancel_arg *arg);
472
473static inline bool tee_param_is_memref(struct tee_param *param)
474{
475	switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
476	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
477	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
478	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
479		return true;
480	default:
481		return false;
482	}
483}
484
485extern const struct bus_type tee_bus_type;
486
487/**
488 * struct tee_client_device - tee based device
489 * @id:			device identifier
490 * @dev:		device structure
491 */
492struct tee_client_device {
493	struct tee_client_device_id id;
494	struct device dev;
495};
496
497#define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
498
499/**
500 * struct tee_client_driver - tee client driver
501 * @id_table:		device id table supported by this driver
502 * @driver:		driver structure
503 */
504struct tee_client_driver {
505	const struct tee_client_device_id *id_table;
506	struct device_driver driver;
507};
508
509#define to_tee_client_driver(d) \
510		container_of(d, struct tee_client_driver, driver)
511
512/**
513 * teedev_open() - Open a struct tee_device
514 * @teedev:	Device to open
515 *
516 * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
517 */
518struct tee_context *teedev_open(struct tee_device *teedev);
519
520/**
521 * teedev_close_context() - closes a struct tee_context
522 * @ctx:	The struct tee_context to close
523 */
524void teedev_close_context(struct tee_context *ctx);
525
526#endif /*__TEE_DRV_H*/