1 /* SPDX-License-Identifier: MIT */ 2 3 /* 4 * Copyright 2019 Advanced Micro Devices, Inc. 5 */ 6 7 #ifndef AMDTEE_PRIVATE_H 8 #define AMDTEE_PRIVATE_H 9 10 #include <linux/mutex.h> 11 #include <linux/spinlock.h> 12 #include <linux/tee_drv.h> 13 #include <linux/kref.h> 14 #include <linux/types.h> 15 #include "amdtee_if.h" 16 17 #define DRIVER_NAME "amdtee" 18 #define DRIVER_AUTHOR "AMD-TEE Linux driver team" 19 20 /* Some GlobalPlatform error codes used in this driver */ 21 #define TEEC_SUCCESS 0x00000000 22 #define TEEC_ERROR_GENERIC 0xFFFF0000 23 #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 24 #define TEEC_ERROR_COMMUNICATION 0xFFFF000E 25 26 #define TEEC_ORIGIN_COMMS 0x00000002 27 28 /* Maximum number of sessions which can be opened with a Trusted Application */ 29 #define TEE_NUM_SESSIONS 32 30 31 #define TA_LOAD_PATH "/amdtee" 32 #define TA_PATH_MAX 60 33 34 /** 35 * struct amdtee - main service struct 36 * @teedev: client device 37 * @pool: shared memory pool 38 */ 39 struct amdtee { 40 struct tee_device *teedev; 41 struct tee_shm_pool *pool; 42 }; 43 44 /** 45 * struct amdtee_session - Trusted Application (TA) session related information. 46 * @ta_handle: handle to Trusted Application (TA) loaded in TEE environment 47 * @refcount: counter to keep track of sessions opened for the TA instance 48 * @session_info: an array pointing to TA allocated session data. 49 * @sess_mask: session usage bit-mask. If a particular bit is set, then the 50 * corresponding @session_info entry is in use or valid. 51 * 52 * Session structure is updated on open_session and this information is used for 53 * subsequent operations with the Trusted Application. 54 */ 55 struct amdtee_session { 56 struct list_head list_node; 57 u32 ta_handle; 58 struct kref refcount; 59 u32 session_info[TEE_NUM_SESSIONS]; 60 DECLARE_BITMAP(sess_mask, TEE_NUM_SESSIONS); 61 spinlock_t lock; /* synchronizes access to @sess_mask */ 62 }; 63 64 /** 65 * struct amdtee_context_data - AMD-TEE driver context data 66 * @sess_list: Keeps track of sessions opened in current TEE context 67 * @shm_list: Keeps track of buffers allocated and mapped in current TEE 68 * context 69 */ 70 struct amdtee_context_data { 71 struct list_head sess_list; 72 struct list_head shm_list; 73 struct mutex shm_mutex; /* synchronizes access to @shm_list */ 74 }; 75 76 struct amdtee_driver_data { 77 struct amdtee *amdtee; 78 }; 79 80 struct shmem_desc { 81 void *kaddr; 82 u64 size; 83 }; 84 85 /** 86 * struct amdtee_shm_data - Shared memory data 87 * @kaddr: Kernel virtual address of shared memory 88 * @buf_id: Buffer id of memory mapped by TEE_CMD_ID_MAP_SHARED_MEM 89 */ 90 struct amdtee_shm_data { 91 struct list_head shm_node; 92 void *kaddr; 93 u32 buf_id; 94 }; 95 96 #define LOWER_TWO_BYTE_MASK 0x0000FFFF 97 98 /** 99 * set_session_id() - Sets the session identifier. 100 * @ta_handle: [in] handle of the loaded Trusted Application (TA) 101 * @session_index: [in] Session index. Range: 0 to (TEE_NUM_SESSIONS - 1). 102 * @session: [out] Pointer to session id 103 * 104 * Lower two bytes of the session identifier represents the TA handle and the 105 * upper two bytes is session index. 106 */ 107 static inline void set_session_id(u32 ta_handle, u32 session_index, 108 u32 *session) 109 { 110 *session = (session_index << 16) | (LOWER_TWO_BYTE_MASK & ta_handle); 111 } 112 113 static inline u32 get_ta_handle(u32 session) 114 { 115 return session & LOWER_TWO_BYTE_MASK; 116 } 117 118 static inline u32 get_session_index(u32 session) 119 { 120 return (session >> 16) & LOWER_TWO_BYTE_MASK; 121 } 122 123 int amdtee_open_session(struct tee_context *ctx, 124 struct tee_ioctl_open_session_arg *arg, 125 struct tee_param *param); 126 127 int amdtee_close_session(struct tee_context *ctx, u32 session); 128 129 int amdtee_invoke_func(struct tee_context *ctx, 130 struct tee_ioctl_invoke_arg *arg, 131 struct tee_param *param); 132 133 int amdtee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); 134 135 int amdtee_map_shmem(struct tee_shm *shm); 136 137 void amdtee_unmap_shmem(struct tee_shm *shm); 138 139 int handle_load_ta(void *data, u32 size, 140 struct tee_ioctl_open_session_arg *arg); 141 142 int handle_unload_ta(u32 ta_handle); 143 144 int handle_open_session(struct tee_ioctl_open_session_arg *arg, u32 *info, 145 struct tee_param *p); 146 147 int handle_close_session(u32 ta_handle, u32 info); 148 149 int handle_map_shmem(u32 count, struct shmem_desc *start, u32 *buf_id); 150 151 void handle_unmap_shmem(u32 buf_id); 152 153 int handle_invoke_cmd(struct tee_ioctl_invoke_arg *arg, u32 sinfo, 154 struct tee_param *p); 155 156 struct tee_shm_pool *amdtee_config_shm(void); 157 158 u32 get_buffer_id(struct tee_shm *shm); 159 #endif /*AMDTEE_PRIVATE_H*/ 160