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_core.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_OUT_OF_MEMORY 0xFFFF000C 25 #define TEEC_ERROR_COMMUNICATION 0xFFFF000E 26 27 #define TEEC_ORIGIN_COMMS 0x00000002 28 29 /* Maximum number of sessions which can be opened with a Trusted Application */ 30 #define TEE_NUM_SESSIONS 32 31 32 #define TA_LOAD_PATH "/amdtee" 33 #define TA_PATH_MAX 60 34 35 /** 36 * struct amdtee - main service struct 37 * @teedev: client device 38 * @pool: shared memory pool 39 */ 40 struct amdtee { 41 struct tee_device *teedev; 42 struct tee_shm_pool *pool; 43 }; 44 45 /** 46 * struct amdtee_session - Trusted Application (TA) session related information. 47 * @ta_handle: handle to Trusted Application (TA) loaded in TEE environment 48 * @refcount: counter to keep track of sessions opened for the TA instance 49 * @session_info: an array pointing to TA allocated session data. 50 * @sess_mask: session usage bit-mask. If a particular bit is set, then the 51 * corresponding @session_info entry is in use or valid. 52 * 53 * Session structure is updated on open_session and this information is used for 54 * subsequent operations with the Trusted Application. 55 */ 56 struct amdtee_session { 57 struct list_head list_node; 58 u32 ta_handle; 59 struct kref refcount; 60 u32 session_info[TEE_NUM_SESSIONS]; 61 DECLARE_BITMAP(sess_mask, TEE_NUM_SESSIONS); 62 spinlock_t lock; /* synchronizes access to @sess_mask */ 63 }; 64 65 /** 66 * struct amdtee_context_data - AMD-TEE driver context data 67 * @sess_list: Keeps track of sessions opened in current TEE context 68 */ 69 struct amdtee_context_data { 70 struct list_head sess_list; 71 }; 72 73 struct amdtee_driver_data { 74 struct amdtee *amdtee; 75 }; 76 77 struct shmem_desc { 78 void *kaddr; 79 u64 size; 80 }; 81 82 /** 83 * struct amdtee_ta_data - Keeps track of all TAs loaded in AMD Secure 84 * Processor 85 * @ta_handle: Handle to TA loaded in TEE 86 * @refcount: Reference count for the loaded TA 87 */ 88 struct amdtee_ta_data { 89 struct list_head list_node; 90 u32 ta_handle; 91 u32 refcount; 92 }; 93 94 #define LOWER_TWO_BYTE_MASK 0x0000FFFF 95 96 /** 97 * set_session_id() - Sets the session identifier. 98 * @ta_handle: [in] handle of the loaded Trusted Application (TA) 99 * @session_index: [in] Session index. Range: 0 to (TEE_NUM_SESSIONS - 1). 100 * @session: [out] Pointer to session id 101 * 102 * Lower two bytes of the session identifier represents the TA handle and the 103 * upper two bytes is session index. 104 */ 105 static inline void set_session_id(u32 ta_handle, u32 session_index, 106 u32 *session) 107 { 108 *session = (session_index << 16) | (LOWER_TWO_BYTE_MASK & ta_handle); 109 } 110 111 static inline u32 get_ta_handle(u32 session) 112 { 113 return session & LOWER_TWO_BYTE_MASK; 114 } 115 116 static inline u32 get_session_index(u32 session) 117 { 118 return (session >> 16) & LOWER_TWO_BYTE_MASK; 119 } 120 121 int amdtee_open_session(struct tee_context *ctx, 122 struct tee_ioctl_open_session_arg *arg, 123 struct tee_param *param); 124 125 int amdtee_close_session(struct tee_context *ctx, u32 session); 126 127 int amdtee_invoke_func(struct tee_context *ctx, 128 struct tee_ioctl_invoke_arg *arg, 129 struct tee_param *param); 130 131 int amdtee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); 132 133 int amdtee_map_shmem(struct tee_shm *shm); 134 135 void amdtee_unmap_shmem(struct tee_shm *shm); 136 137 int handle_load_ta(void *data, u32 size, 138 struct tee_ioctl_open_session_arg *arg); 139 140 int handle_unload_ta(u32 ta_handle); 141 142 int handle_open_session(struct tee_ioctl_open_session_arg *arg, u32 *info, 143 struct tee_param *p); 144 145 int handle_close_session(u32 ta_handle, u32 info); 146 147 int handle_map_shmem(u32 count, struct shmem_desc *start, u32 *buf_id); 148 149 void handle_unmap_shmem(u32 buf_id); 150 151 int handle_invoke_cmd(struct tee_ioctl_invoke_arg *arg, u32 sinfo, 152 struct tee_param *p); 153 154 struct tee_shm_pool *amdtee_config_shm(void); 155 156 #endif /*AMDTEE_PRIVATE_H*/ 157