Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: MIT */
0002 
0003 /*
0004  * Copyright 2019 Advanced Micro Devices, Inc.
0005  */
0006 
0007 #ifndef AMDTEE_PRIVATE_H
0008 #define AMDTEE_PRIVATE_H
0009 
0010 #include <linux/mutex.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/tee_drv.h>
0013 #include <linux/kref.h>
0014 #include <linux/types.h>
0015 #include "amdtee_if.h"
0016 
0017 #define DRIVER_NAME "amdtee"
0018 #define DRIVER_AUTHOR   "AMD-TEE Linux driver team"
0019 
0020 /* Some GlobalPlatform error codes used in this driver */
0021 #define TEEC_SUCCESS            0x00000000
0022 #define TEEC_ERROR_GENERIC      0xFFFF0000
0023 #define TEEC_ERROR_BAD_PARAMETERS   0xFFFF0006
0024 #define TEEC_ERROR_OUT_OF_MEMORY    0xFFFF000C
0025 #define TEEC_ERROR_COMMUNICATION    0xFFFF000E
0026 
0027 #define TEEC_ORIGIN_COMMS       0x00000002
0028 
0029 /* Maximum number of sessions which can be opened with a Trusted Application */
0030 #define TEE_NUM_SESSIONS            32
0031 
0032 #define TA_LOAD_PATH                "/amdtee"
0033 #define TA_PATH_MAX             60
0034 
0035 /**
0036  * struct amdtee - main service struct
0037  * @teedev:     client device
0038  * @pool:       shared memory pool
0039  */
0040 struct amdtee {
0041     struct tee_device *teedev;
0042     struct tee_shm_pool *pool;
0043 };
0044 
0045 /**
0046  * struct amdtee_session - Trusted Application (TA) session related information.
0047  * @ta_handle:     handle to Trusted Application (TA) loaded in TEE environment
0048  * @refcount:      counter to keep track of sessions opened for the TA instance
0049  * @session_info:  an array pointing to TA allocated session data.
0050  * @sess_mask:     session usage bit-mask. If a particular bit is set, then the
0051  *                 corresponding @session_info entry is in use or valid.
0052  *
0053  * Session structure is updated on open_session and this information is used for
0054  * subsequent operations with the Trusted Application.
0055  */
0056 struct amdtee_session {
0057     struct list_head list_node;
0058     u32 ta_handle;
0059     struct kref refcount;
0060     u32 session_info[TEE_NUM_SESSIONS];
0061     DECLARE_BITMAP(sess_mask, TEE_NUM_SESSIONS);
0062     spinlock_t lock;    /* synchronizes access to @sess_mask */
0063 };
0064 
0065 /**
0066  * struct amdtee_context_data - AMD-TEE driver context data
0067  * @sess_list:    Keeps track of sessions opened in current TEE context
0068  * @shm_list:     Keeps track of buffers allocated and mapped in current TEE
0069  *                context
0070  */
0071 struct amdtee_context_data {
0072     struct list_head sess_list;
0073     struct list_head shm_list;
0074     struct mutex shm_mutex;   /* synchronizes access to @shm_list */
0075 };
0076 
0077 struct amdtee_driver_data {
0078     struct amdtee *amdtee;
0079 };
0080 
0081 struct shmem_desc {
0082     void *kaddr;
0083     u64 size;
0084 };
0085 
0086 /**
0087  * struct amdtee_shm_data - Shared memory data
0088  * @kaddr:  Kernel virtual address of shared memory
0089  * @buf_id: Buffer id of memory mapped by TEE_CMD_ID_MAP_SHARED_MEM
0090  */
0091 struct amdtee_shm_data {
0092     struct  list_head shm_node;
0093     void    *kaddr;
0094     u32     buf_id;
0095 };
0096 
0097 /**
0098  * struct amdtee_ta_data - Keeps track of all TAs loaded in AMD Secure
0099  *             Processor
0100  * @ta_handle:  Handle to TA loaded in TEE
0101  * @refcount:   Reference count for the loaded TA
0102  */
0103 struct amdtee_ta_data {
0104     struct list_head list_node;
0105     u32 ta_handle;
0106     u32 refcount;
0107 };
0108 
0109 #define LOWER_TWO_BYTE_MASK 0x0000FFFF
0110 
0111 /**
0112  * set_session_id() - Sets the session identifier.
0113  * @ta_handle:      [in] handle of the loaded Trusted Application (TA)
0114  * @session_index:  [in] Session index. Range: 0 to (TEE_NUM_SESSIONS - 1).
0115  * @session:        [out] Pointer to session id
0116  *
0117  * Lower two bytes of the session identifier represents the TA handle and the
0118  * upper two bytes is session index.
0119  */
0120 static inline void set_session_id(u32 ta_handle, u32 session_index,
0121                   u32 *session)
0122 {
0123     *session = (session_index << 16) | (LOWER_TWO_BYTE_MASK & ta_handle);
0124 }
0125 
0126 static inline u32 get_ta_handle(u32 session)
0127 {
0128     return session & LOWER_TWO_BYTE_MASK;
0129 }
0130 
0131 static inline u32 get_session_index(u32 session)
0132 {
0133     return (session >> 16) & LOWER_TWO_BYTE_MASK;
0134 }
0135 
0136 int amdtee_open_session(struct tee_context *ctx,
0137             struct tee_ioctl_open_session_arg *arg,
0138             struct tee_param *param);
0139 
0140 int amdtee_close_session(struct tee_context *ctx, u32 session);
0141 
0142 int amdtee_invoke_func(struct tee_context *ctx,
0143                struct tee_ioctl_invoke_arg *arg,
0144                struct tee_param *param);
0145 
0146 int amdtee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
0147 
0148 int amdtee_map_shmem(struct tee_shm *shm);
0149 
0150 void amdtee_unmap_shmem(struct tee_shm *shm);
0151 
0152 int handle_load_ta(void *data, u32 size,
0153            struct tee_ioctl_open_session_arg *arg);
0154 
0155 int handle_unload_ta(u32 ta_handle);
0156 
0157 int handle_open_session(struct tee_ioctl_open_session_arg *arg, u32 *info,
0158             struct tee_param *p);
0159 
0160 int handle_close_session(u32 ta_handle, u32 info);
0161 
0162 int handle_map_shmem(u32 count, struct shmem_desc *start, u32 *buf_id);
0163 
0164 void handle_unmap_shmem(u32 buf_id);
0165 
0166 int handle_invoke_cmd(struct tee_ioctl_invoke_arg *arg, u32 sinfo,
0167               struct tee_param *p);
0168 
0169 struct tee_shm_pool *amdtee_config_shm(void);
0170 
0171 u32 get_buffer_id(struct tee_shm *shm);
0172 #endif /*AMDTEE_PRIVATE_H*/