xref: /linux-6.15/include/linux/tee_core.h (revision c30b855e)
10439fcffSSumit Garg /* SPDX-License-Identifier: GPL-2.0-only */
20439fcffSSumit Garg /*
30439fcffSSumit Garg  * Copyright (c) 2024 Linaro Limited
40439fcffSSumit Garg  */
50439fcffSSumit Garg 
60439fcffSSumit Garg #ifndef __TEE_CORE_H
70439fcffSSumit Garg #define __TEE_CORE_H
80439fcffSSumit Garg 
90439fcffSSumit Garg #include <linux/cdev.h>
100439fcffSSumit Garg #include <linux/device.h>
110439fcffSSumit Garg #include <linux/idr.h>
120439fcffSSumit Garg #include <linux/kref.h>
130439fcffSSumit Garg #include <linux/list.h>
140439fcffSSumit Garg #include <linux/tee.h>
150439fcffSSumit Garg #include <linux/tee_drv.h>
160439fcffSSumit Garg #include <linux/types.h>
170439fcffSSumit Garg #include <linux/uuid.h>
180439fcffSSumit Garg 
190439fcffSSumit Garg /*
200439fcffSSumit Garg  * The file describes the API provided by the generic TEE driver to the
210439fcffSSumit Garg  * specific TEE driver.
220439fcffSSumit Garg  */
230439fcffSSumit Garg 
240439fcffSSumit Garg #define TEE_SHM_DYNAMIC		BIT(0)  /* Dynamic shared memory registered */
250439fcffSSumit Garg 					/* in secure world */
260439fcffSSumit Garg #define TEE_SHM_USER_MAPPED	BIT(1)  /* Memory mapped in user space */
270439fcffSSumit Garg #define TEE_SHM_POOL		BIT(2)  /* Memory allocated from pool */
280439fcffSSumit Garg #define TEE_SHM_PRIV		BIT(3)  /* Memory private to TEE driver */
290439fcffSSumit Garg 
300439fcffSSumit Garg #define TEE_DEVICE_FLAG_REGISTERED	0x1
310439fcffSSumit Garg #define TEE_MAX_DEV_NAME_LEN		32
320439fcffSSumit Garg 
330439fcffSSumit Garg /**
340439fcffSSumit Garg  * struct tee_device - TEE Device representation
350439fcffSSumit Garg  * @name:	name of device
360439fcffSSumit Garg  * @desc:	description of device
370439fcffSSumit Garg  * @id:		unique id of device
380439fcffSSumit Garg  * @flags:	represented by TEE_DEVICE_FLAG_REGISTERED above
390439fcffSSumit Garg  * @dev:	embedded basic device structure
400439fcffSSumit Garg  * @cdev:	embedded cdev
410439fcffSSumit Garg  * @num_users:	number of active users of this device
420439fcffSSumit Garg  * @c_no_user:	completion used when unregistering the device
430439fcffSSumit Garg  * @mutex:	mutex protecting @num_users and @idr
440439fcffSSumit Garg  * @idr:	register of user space shared memory objects allocated or
450439fcffSSumit Garg  *		registered on this device
460439fcffSSumit Garg  * @pool:	shared memory pool
470439fcffSSumit Garg  */
480439fcffSSumit Garg struct tee_device {
490439fcffSSumit Garg 	char name[TEE_MAX_DEV_NAME_LEN];
500439fcffSSumit Garg 	const struct tee_desc *desc;
510439fcffSSumit Garg 	int id;
520439fcffSSumit Garg 	unsigned int flags;
530439fcffSSumit Garg 
540439fcffSSumit Garg 	struct device dev;
550439fcffSSumit Garg 	struct cdev cdev;
560439fcffSSumit Garg 
570439fcffSSumit Garg 	size_t num_users;
580439fcffSSumit Garg 	struct completion c_no_users;
590439fcffSSumit Garg 	struct mutex mutex;	/* protects num_users and idr */
600439fcffSSumit Garg 
610439fcffSSumit Garg 	struct idr idr;
620439fcffSSumit Garg 	struct tee_shm_pool *pool;
630439fcffSSumit Garg };
640439fcffSSumit Garg 
650439fcffSSumit Garg /**
660439fcffSSumit Garg  * struct tee_driver_ops - driver operations vtable
670439fcffSSumit Garg  * @get_version:	returns version of driver
680439fcffSSumit Garg  * @open:		called when the device file is opened
690439fcffSSumit Garg  * @release:		release this open file
700439fcffSSumit Garg  * @open_session:	open a new session
710439fcffSSumit Garg  * @close_session:	close a session
720439fcffSSumit Garg  * @system_session:	declare session as a system session
730439fcffSSumit Garg  * @invoke_func:	invoke a trusted function
740439fcffSSumit Garg  * @cancel_req:		request cancel of an ongoing invoke or open
750439fcffSSumit Garg  * @supp_recv:		called for supplicant to get a command
760439fcffSSumit Garg  * @supp_send:		called for supplicant to send a response
770439fcffSSumit Garg  * @shm_register:	register shared memory buffer in TEE
780439fcffSSumit Garg  * @shm_unregister:	unregister shared memory buffer in TEE
790439fcffSSumit Garg  */
800439fcffSSumit Garg struct tee_driver_ops {
810439fcffSSumit Garg 	void (*get_version)(struct tee_device *teedev,
820439fcffSSumit Garg 			    struct tee_ioctl_version_data *vers);
830439fcffSSumit Garg 	int (*open)(struct tee_context *ctx);
840439fcffSSumit Garg 	void (*release)(struct tee_context *ctx);
850439fcffSSumit Garg 	int (*open_session)(struct tee_context *ctx,
860439fcffSSumit Garg 			    struct tee_ioctl_open_session_arg *arg,
870439fcffSSumit Garg 			    struct tee_param *param);
880439fcffSSumit Garg 	int (*close_session)(struct tee_context *ctx, u32 session);
890439fcffSSumit Garg 	int (*system_session)(struct tee_context *ctx, u32 session);
900439fcffSSumit Garg 	int (*invoke_func)(struct tee_context *ctx,
910439fcffSSumit Garg 			   struct tee_ioctl_invoke_arg *arg,
920439fcffSSumit Garg 			   struct tee_param *param);
930439fcffSSumit Garg 	int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
940439fcffSSumit Garg 	int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
950439fcffSSumit Garg 			 struct tee_param *param);
960439fcffSSumit Garg 	int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
970439fcffSSumit Garg 			 struct tee_param *param);
980439fcffSSumit Garg 	int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
990439fcffSSumit Garg 			    struct page **pages, size_t num_pages,
1000439fcffSSumit Garg 			    unsigned long start);
1010439fcffSSumit Garg 	int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
1020439fcffSSumit Garg };
1030439fcffSSumit Garg 
1040439fcffSSumit Garg /**
1050439fcffSSumit Garg  * struct tee_desc - Describes the TEE driver to the subsystem
1060439fcffSSumit Garg  * @name:	name of driver
1070439fcffSSumit Garg  * @ops:	driver operations vtable
1080439fcffSSumit Garg  * @owner:	module providing the driver
1090439fcffSSumit Garg  * @flags:	Extra properties of driver, defined by TEE_DESC_* below
1100439fcffSSumit Garg  */
1110439fcffSSumit Garg #define TEE_DESC_PRIVILEGED	0x1
1120439fcffSSumit Garg struct tee_desc {
1130439fcffSSumit Garg 	const char *name;
1140439fcffSSumit Garg 	const struct tee_driver_ops *ops;
1150439fcffSSumit Garg 	struct module *owner;
1160439fcffSSumit Garg 	u32 flags;
1170439fcffSSumit Garg };
1180439fcffSSumit Garg 
1190439fcffSSumit Garg /**
1200439fcffSSumit Garg  * tee_device_alloc() - Allocate a new struct tee_device instance
1210439fcffSSumit Garg  * @teedesc:	Descriptor for this driver
1220439fcffSSumit Garg  * @dev:	Parent device for this device
1230439fcffSSumit Garg  * @pool:	Shared memory pool, NULL if not used
1240439fcffSSumit Garg  * @driver_data: Private driver data for this device
1250439fcffSSumit Garg  *
1260439fcffSSumit Garg  * Allocates a new struct tee_device instance. The device is
1270439fcffSSumit Garg  * removed by tee_device_unregister().
1280439fcffSSumit Garg  *
1290439fcffSSumit Garg  * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
1300439fcffSSumit Garg  */
1310439fcffSSumit Garg struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
1320439fcffSSumit Garg 				    struct device *dev,
1330439fcffSSumit Garg 				    struct tee_shm_pool *pool,
1340439fcffSSumit Garg 				    void *driver_data);
1350439fcffSSumit Garg 
1360439fcffSSumit Garg /**
1370439fcffSSumit Garg  * tee_device_register() - Registers a TEE device
1380439fcffSSumit Garg  * @teedev:	Device to register
1390439fcffSSumit Garg  *
1400439fcffSSumit Garg  * tee_device_unregister() need to be called to remove the @teedev if
1410439fcffSSumit Garg  * this function fails.
1420439fcffSSumit Garg  *
1430439fcffSSumit Garg  * @returns < 0 on failure
1440439fcffSSumit Garg  */
1450439fcffSSumit Garg int tee_device_register(struct tee_device *teedev);
1460439fcffSSumit Garg 
1470439fcffSSumit Garg /**
1480439fcffSSumit Garg  * tee_device_unregister() - Removes a TEE device
1490439fcffSSumit Garg  * @teedev:	Device to unregister
1500439fcffSSumit Garg  *
1510439fcffSSumit Garg  * This function should be called to remove the @teedev even if
1520439fcffSSumit Garg  * tee_device_register() hasn't been called yet. Does nothing if
1530439fcffSSumit Garg  * @teedev is NULL.
1540439fcffSSumit Garg  */
1550439fcffSSumit Garg void tee_device_unregister(struct tee_device *teedev);
1560439fcffSSumit Garg 
1570439fcffSSumit Garg /**
158*c30b855eSJens Wiklander  * tee_device_set_dev_groups() - Set device attribute groups
159*c30b855eSJens Wiklander  * @teedev:	Device to register
160*c30b855eSJens Wiklander  * @dev_groups: Attribute groups
161*c30b855eSJens Wiklander  *
162*c30b855eSJens Wiklander  * Assigns the provided @dev_groups to the @teedev to be registered later
163*c30b855eSJens Wiklander  * with tee_device_register(). Calling this function is optional, but if
164*c30b855eSJens Wiklander  * it's called it must be called before tee_device_register().
165*c30b855eSJens Wiklander  */
166*c30b855eSJens Wiklander void tee_device_set_dev_groups(struct tee_device *teedev,
167*c30b855eSJens Wiklander 			       const struct attribute_group **dev_groups);
168*c30b855eSJens Wiklander 
169*c30b855eSJens Wiklander /**
1700439fcffSSumit Garg  * tee_session_calc_client_uuid() - Calculates client UUID for session
1710439fcffSSumit Garg  * @uuid:		Resulting UUID
1720439fcffSSumit Garg  * @connection_method:	Connection method for session (TEE_IOCTL_LOGIN_*)
1730439fcffSSumit Garg  * @connectuon_data:	Connection data for opening session
1740439fcffSSumit Garg  *
1750439fcffSSumit Garg  * Based on connection method calculates UUIDv5 based client UUID.
1760439fcffSSumit Garg  *
1770439fcffSSumit Garg  * For group based logins verifies that calling process has specified
1780439fcffSSumit Garg  * credentials.
1790439fcffSSumit Garg  *
1800439fcffSSumit Garg  * @return < 0 on failure
1810439fcffSSumit Garg  */
1820439fcffSSumit Garg int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
1830439fcffSSumit Garg 				 const u8 connection_data[TEE_IOCTL_UUID_LEN]);
1840439fcffSSumit Garg 
1850439fcffSSumit Garg /**
1860439fcffSSumit Garg  * struct tee_shm_pool - shared memory pool
1870439fcffSSumit Garg  * @ops:		operations
1880439fcffSSumit Garg  * @private_data:	private data for the shared memory manager
1890439fcffSSumit Garg  */
1900439fcffSSumit Garg struct tee_shm_pool {
1910439fcffSSumit Garg 	const struct tee_shm_pool_ops *ops;
1920439fcffSSumit Garg 	void *private_data;
1930439fcffSSumit Garg };
1940439fcffSSumit Garg 
1950439fcffSSumit Garg /**
1960439fcffSSumit Garg  * struct tee_shm_pool_ops - shared memory pool operations
1970439fcffSSumit Garg  * @alloc:		called when allocating shared memory
1980439fcffSSumit Garg  * @free:		called when freeing shared memory
1990439fcffSSumit Garg  * @destroy_pool:	called when destroying the pool
2000439fcffSSumit Garg  */
2010439fcffSSumit Garg struct tee_shm_pool_ops {
2020439fcffSSumit Garg 	int (*alloc)(struct tee_shm_pool *pool, struct tee_shm *shm,
2030439fcffSSumit Garg 		     size_t size, size_t align);
2040439fcffSSumit Garg 	void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm);
2050439fcffSSumit Garg 	void (*destroy_pool)(struct tee_shm_pool *pool);
2060439fcffSSumit Garg };
2070439fcffSSumit Garg 
2080439fcffSSumit Garg /*
2090439fcffSSumit Garg  * tee_shm_pool_alloc_res_mem() - Create a shm manager for reserved memory
2100439fcffSSumit Garg  * @vaddr:	Virtual address of start of pool
2110439fcffSSumit Garg  * @paddr:	Physical address of start of pool
2120439fcffSSumit Garg  * @size:	Size in bytes of the pool
2130439fcffSSumit Garg  *
2140439fcffSSumit Garg  * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
2150439fcffSSumit Garg  */
2160439fcffSSumit Garg struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr,
2170439fcffSSumit Garg 						phys_addr_t paddr, size_t size,
2180439fcffSSumit Garg 						int min_alloc_order);
2190439fcffSSumit Garg 
2200439fcffSSumit Garg /**
2210439fcffSSumit Garg  * tee_shm_pool_free() - Free a shared memory pool
2220439fcffSSumit Garg  * @pool:	The shared memory pool to free
2230439fcffSSumit Garg  *
2240439fcffSSumit Garg  * The must be no remaining shared memory allocated from this pool when
2250439fcffSSumit Garg  * this function is called.
2260439fcffSSumit Garg  */
tee_shm_pool_free(struct tee_shm_pool * pool)2270439fcffSSumit Garg static inline void tee_shm_pool_free(struct tee_shm_pool *pool)
2280439fcffSSumit Garg {
2290439fcffSSumit Garg 	pool->ops->destroy_pool(pool);
2300439fcffSSumit Garg }
2310439fcffSSumit Garg 
2320439fcffSSumit Garg /**
2330439fcffSSumit Garg  * tee_get_drvdata() - Return driver_data pointer
2340439fcffSSumit Garg  * @returns the driver_data pointer supplied to tee_register().
2350439fcffSSumit Garg  */
2360439fcffSSumit Garg void *tee_get_drvdata(struct tee_device *teedev);
2370439fcffSSumit Garg 
2380439fcffSSumit Garg /**
2390439fcffSSumit Garg  * tee_shm_alloc_priv_buf() - Allocate shared memory for private use by specific
2400439fcffSSumit Garg  *                            TEE driver
2410439fcffSSumit Garg  * @ctx:	The TEE context for shared memory allocation
2420439fcffSSumit Garg  * @size:	Shared memory allocation size
2430439fcffSSumit Garg  * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
2440439fcffSSumit Garg  */
2450439fcffSSumit Garg struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
2460439fcffSSumit Garg 
247cf444150SBalint Dobszay int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
248cf444150SBalint Dobszay 			     int (*shm_register)(struct tee_context *ctx,
249cf444150SBalint Dobszay 						 struct tee_shm *shm,
250cf444150SBalint Dobszay 						 struct page **pages,
251cf444150SBalint Dobszay 						 size_t num_pages,
252cf444150SBalint Dobszay 						 unsigned long start));
253cf444150SBalint Dobszay void tee_dyn_shm_free_helper(struct tee_shm *shm,
254cf444150SBalint Dobszay 			     int (*shm_unregister)(struct tee_context *ctx,
255cf444150SBalint Dobszay 						   struct tee_shm *shm));
256cf444150SBalint Dobszay 
2570439fcffSSumit Garg /**
2580439fcffSSumit Garg  * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
2590439fcffSSumit Garg  * @shm:	Shared memory handle
2600439fcffSSumit Garg  * @returns true if object is dynamic shared memory
2610439fcffSSumit Garg  */
tee_shm_is_dynamic(struct tee_shm * shm)2620439fcffSSumit Garg static inline bool tee_shm_is_dynamic(struct tee_shm *shm)
2630439fcffSSumit Garg {
2640439fcffSSumit Garg 	return shm && (shm->flags & TEE_SHM_DYNAMIC);
2650439fcffSSumit Garg }
2660439fcffSSumit Garg 
2670439fcffSSumit Garg /**
2680439fcffSSumit Garg  * tee_shm_put() - Decrease reference count on a shared memory handle
2690439fcffSSumit Garg  * @shm:	Shared memory handle
2700439fcffSSumit Garg  */
2710439fcffSSumit Garg void tee_shm_put(struct tee_shm *shm);
2720439fcffSSumit Garg 
2730439fcffSSumit Garg /**
2740439fcffSSumit Garg  * tee_shm_get_id() - Get id of a shared memory object
2750439fcffSSumit Garg  * @shm:	Shared memory handle
2760439fcffSSumit Garg  * @returns id
2770439fcffSSumit Garg  */
tee_shm_get_id(struct tee_shm * shm)2780439fcffSSumit Garg static inline int tee_shm_get_id(struct tee_shm *shm)
2790439fcffSSumit Garg {
2800439fcffSSumit Garg 	return shm->id;
2810439fcffSSumit Garg }
2820439fcffSSumit Garg 
2830439fcffSSumit Garg /**
2840439fcffSSumit Garg  * tee_shm_get_from_id() - Find shared memory object and increase reference
2850439fcffSSumit Garg  * count
2860439fcffSSumit Garg  * @ctx:	Context owning the shared memory
2870439fcffSSumit Garg  * @id:		Id of shared memory object
2880439fcffSSumit Garg  * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
2890439fcffSSumit Garg  */
2900439fcffSSumit Garg struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
2910439fcffSSumit Garg 
tee_param_is_memref(struct tee_param * param)2920439fcffSSumit Garg static inline bool tee_param_is_memref(struct tee_param *param)
2930439fcffSSumit Garg {
2940439fcffSSumit Garg 	switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
2950439fcffSSumit Garg 	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
2960439fcffSSumit Garg 	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
2970439fcffSSumit Garg 	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
2980439fcffSSumit Garg 		return true;
2990439fcffSSumit Garg 	default:
3000439fcffSSumit Garg 		return false;
3010439fcffSSumit Garg 	}
3020439fcffSSumit Garg }
3030439fcffSSumit Garg 
3040439fcffSSumit Garg /**
3050439fcffSSumit Garg  * teedev_open() - Open a struct tee_device
3060439fcffSSumit Garg  * @teedev:	Device to open
3070439fcffSSumit Garg  *
3080439fcffSSumit Garg  * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
3090439fcffSSumit Garg  */
3100439fcffSSumit Garg struct tee_context *teedev_open(struct tee_device *teedev);
3110439fcffSSumit Garg 
3120439fcffSSumit Garg /**
3130439fcffSSumit Garg  * teedev_close_context() - closes a struct tee_context
3140439fcffSSumit Garg  * @ctx:	The struct tee_context to close
3150439fcffSSumit Garg  */
3160439fcffSSumit Garg void teedev_close_context(struct tee_context *ctx);
3170439fcffSSumit Garg 
3180439fcffSSumit Garg #endif /*__TEE_CORE_H*/
319