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