Skip to content

Commit

Permalink
tee: add AMD-TEE driver
Browse files Browse the repository at this point in the history
Adds AMD-TEE driver.
* targets AMD APUs which has AMD Secure Processor with software-based
  Trusted Execution Environment (TEE) support
* registers with TEE subsystem
* defines tee_driver_ops function callbacks
* kernel allocated memory is used as shared memory between normal
  world and secure world.
* acts as REE (Rich Execution Environment) communication agent, which
  uses the services of AMD Secure Processor driver to submit commands
  for processing in TEE environment

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Co-developed-by: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
Signed-off-by: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
Signed-off-by: Rijo Thomas <Rijo-john.Thomas@amd.com>
Reviewed-by: Gary R Hook <gary.hook@amd.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
Rijo Thomas authored and Herbert Xu committed Jan 4, 2020
1 parent 1a74fa3 commit 757cc3e
Show file tree
Hide file tree
Showing 10 changed files with 1,334 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/tee/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if TEE
menu "TEE drivers"

source "drivers/tee/optee/Kconfig"

source "drivers/tee/amdtee/Kconfig"
endmenu

endif
1 change: 1 addition & 0 deletions drivers/tee/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tee-objs += tee_core.o
tee-objs += tee_shm.o
tee-objs += tee_shm_pool.o
obj-$(CONFIG_OPTEE) += optee/
obj-$(CONFIG_AMDTEE) += amdtee/
8 changes: 8 additions & 0 deletions drivers/tee/amdtee/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: MIT
# AMD-TEE Trusted Execution Environment Configuration
config AMDTEE
tristate "AMD-TEE"
default m
depends on CRYPTO_DEV_SP_PSP
help
This implements AMD's Trusted Execution Environment (TEE) driver.
5 changes: 5 additions & 0 deletions drivers/tee/amdtee/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: MIT
obj-$(CONFIG_AMDTEE) += amdtee.o
amdtee-objs += core.o
amdtee-objs += call.o
amdtee-objs += shm_pool.o
183 changes: 183 additions & 0 deletions drivers/tee/amdtee/amdtee_if.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/* SPDX-License-Identifier: MIT */

/*
* Copyright 2019 Advanced Micro Devices, Inc.
*/

/*
* This file has definitions related to Host and AMD-TEE Trusted OS interface.
* These definitions must match the definitions on the TEE side.
*/

#ifndef AMDTEE_IF_H
#define AMDTEE_IF_H

#include <linux/types.h>

/*****************************************************************************
** TEE Param
******************************************************************************/
#define TEE_MAX_PARAMS 4

/**
* struct memref - memory reference structure
* @buf_id: buffer ID of the buffer mapped by TEE_CMD_ID_MAP_SHARED_MEM
* @offset: offset in bytes from beginning of the buffer
* @size: data size in bytes
*/
struct memref {
u32 buf_id;
u32 offset;
u32 size;
};

struct value {
u32 a;
u32 b;
};

/*
* Parameters passed to open_session or invoke_command
*/
union tee_op_param {
struct memref mref;
struct value val;
};

struct tee_operation {
u32 param_types;
union tee_op_param params[TEE_MAX_PARAMS];
};

/* Must be same as in GP TEE specification */
#define TEE_OP_PARAM_TYPE_NONE 0
#define TEE_OP_PARAM_TYPE_VALUE_INPUT 1
#define TEE_OP_PARAM_TYPE_VALUE_OUTPUT 2
#define TEE_OP_PARAM_TYPE_VALUE_INOUT 3
#define TEE_OP_PARAM_TYPE_INVALID 4
#define TEE_OP_PARAM_TYPE_MEMREF_INPUT 5
#define TEE_OP_PARAM_TYPE_MEMREF_OUTPUT 6
#define TEE_OP_PARAM_TYPE_MEMREF_INOUT 7

#define TEE_PARAM_TYPE_GET(t, i) (((t) >> ((i) * 4)) & 0xF)
#define TEE_PARAM_TYPES(t0, t1, t2, t3) \
((t0) | ((t1) << 4) | ((t2) << 8) | ((t3) << 12))

/*****************************************************************************
** TEE Commands
*****************************************************************************/

/*
* The shared memory between rich world and secure world may be physically
* non-contiguous. Below structures are meant to describe a shared memory region
* via scatter/gather (sg) list
*/

/**
* struct tee_sg_desc - sg descriptor for a physically contiguous buffer
* @low_addr: [in] bits[31:0] of buffer's physical address. Must be 4KB aligned
* @hi_addr: [in] bits[63:32] of the buffer's physical address
* @size: [in] size in bytes (must be multiple of 4KB)
*/
struct tee_sg_desc {
u32 low_addr;
u32 hi_addr;
u32 size;
};

/**
* struct tee_sg_list - structure describing a scatter/gather list
* @count: [in] number of sg descriptors
* @size: [in] total size of all buffers in the list. Must be multiple of 4KB
* @buf: [in] list of sg buffer descriptors
*/
#define TEE_MAX_SG_DESC 64
struct tee_sg_list {
u32 count;
u32 size;
struct tee_sg_desc buf[TEE_MAX_SG_DESC];
};

/**
* struct tee_cmd_map_shared_mem - command to map shared memory
* @buf_id: [out] return buffer ID value
* @sg_list: [in] list describing memory to be mapped
*/
struct tee_cmd_map_shared_mem {
u32 buf_id;
struct tee_sg_list sg_list;
};

/**
* struct tee_cmd_unmap_shared_mem - command to unmap shared memory
* @buf_id: [in] buffer ID of memory to be unmapped
*/
struct tee_cmd_unmap_shared_mem {
u32 buf_id;
};

/**
* struct tee_cmd_load_ta - load Trusted Application (TA) binary into TEE
* @low_addr: [in] bits [31:0] of the physical address of the TA binary
* @hi_addr: [in] bits [63:32] of the physical address of the TA binary
* @size: [in] size of TA binary in bytes
* @ta_handle: [out] return handle of the loaded TA
*/
struct tee_cmd_load_ta {
u32 low_addr;
u32 hi_addr;
u32 size;
u32 ta_handle;
};

/**
* struct tee_cmd_unload_ta - command to unload TA binary from TEE environment
* @ta_handle: [in] handle of the loaded TA to be unloaded
*/
struct tee_cmd_unload_ta {
u32 ta_handle;
};

/**
* struct tee_cmd_open_session - command to call TA_OpenSessionEntryPoint in TA
* @ta_handle: [in] handle of the loaded TA
* @session_info: [out] pointer to TA allocated session data
* @op: [in/out] operation parameters
* @return_origin: [out] origin of return code after TEE processing
*/
struct tee_cmd_open_session {
u32 ta_handle;
u32 session_info;
struct tee_operation op;
u32 return_origin;
};

/**
* struct tee_cmd_close_session - command to call TA_CloseSessionEntryPoint()
* in TA
* @ta_handle: [in] handle of the loaded TA
* @session_info: [in] pointer to TA allocated session data
*/
struct tee_cmd_close_session {
u32 ta_handle;
u32 session_info;
};

/**
* struct tee_cmd_invoke_cmd - command to call TA_InvokeCommandEntryPoint() in
* TA
* @ta_handle: [in] handle of the loaded TA
* @cmd_id: [in] TA command ID
* @session_info: [in] pointer to TA allocated session data
* @op: [in/out] operation parameters
* @return_origin: [out] origin of return code after TEE processing
*/
struct tee_cmd_invoke_cmd {
u32 ta_handle;
u32 cmd_id;
u32 session_info;
struct tee_operation op;
u32 return_origin;
};

#endif /*AMDTEE_IF_H*/
159 changes: 159 additions & 0 deletions drivers/tee/amdtee/amdtee_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* SPDX-License-Identifier: MIT */

/*
* Copyright 2019 Advanced Micro Devices, Inc.
*/

#ifndef AMDTEE_PRIVATE_H
#define AMDTEE_PRIVATE_H

#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/tee_drv.h>
#include <linux/kref.h>
#include <linux/types.h>
#include "amdtee_if.h"

#define DRIVER_NAME "amdtee"
#define DRIVER_AUTHOR "AMD-TEE Linux driver team"

/* Some GlobalPlatform error codes used in this driver */
#define TEEC_SUCCESS 0x00000000
#define TEEC_ERROR_GENERIC 0xFFFF0000
#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
#define TEEC_ERROR_COMMUNICATION 0xFFFF000E

#define TEEC_ORIGIN_COMMS 0x00000002

/* Maximum number of sessions which can be opened with a Trusted Application */
#define TEE_NUM_SESSIONS 32

#define TA_LOAD_PATH "/amdtee"
#define TA_PATH_MAX 60

/**
* struct amdtee - main service struct
* @teedev: client device
* @pool: shared memory pool
*/
struct amdtee {
struct tee_device *teedev;
struct tee_shm_pool *pool;
};

/**
* struct amdtee_session - Trusted Application (TA) session related information.
* @ta_handle: handle to Trusted Application (TA) loaded in TEE environment
* @refcount: counter to keep track of sessions opened for the TA instance
* @session_info: an array pointing to TA allocated session data.
* @sess_mask: session usage bit-mask. If a particular bit is set, then the
* corresponding @session_info entry is in use or valid.
*
* Session structure is updated on open_session and this information is used for
* subsequent operations with the Trusted Application.
*/
struct amdtee_session {
struct list_head list_node;
u32 ta_handle;
struct kref refcount;
u32 session_info[TEE_NUM_SESSIONS];
DECLARE_BITMAP(sess_mask, TEE_NUM_SESSIONS);
spinlock_t lock; /* synchronizes access to @sess_mask */
};

/**
* struct amdtee_context_data - AMD-TEE driver context data
* @sess_list: Keeps track of sessions opened in current TEE context
*/
struct amdtee_context_data {
struct list_head sess_list;
};

struct amdtee_driver_data {
struct amdtee *amdtee;
};

struct shmem_desc {
void *kaddr;
u64 size;
};

/**
* struct amdtee_shm_data - Shared memory data
* @kaddr: Kernel virtual address of shared memory
* @buf_id: Buffer id of memory mapped by TEE_CMD_ID_MAP_SHARED_MEM
*/
struct amdtee_shm_data {
struct list_head shm_node;
void *kaddr;
u32 buf_id;
};

struct amdtee_shm_context {
struct list_head shmdata_list;
};

#define LOWER_TWO_BYTE_MASK 0x0000FFFF

/**
* set_session_id() - Sets the session identifier.
* @ta_handle: [in] handle of the loaded Trusted Application (TA)
* @session_index: [in] Session index. Range: 0 to (TEE_NUM_SESSIONS - 1).
* @session: [out] Pointer to session id
*
* Lower two bytes of the session identifier represents the TA handle and the
* upper two bytes is session index.
*/
static inline void set_session_id(u32 ta_handle, u32 session_index,
u32 *session)
{
*session = (session_index << 16) | (LOWER_TWO_BYTE_MASK & ta_handle);
}

static inline u32 get_ta_handle(u32 session)
{
return session & LOWER_TWO_BYTE_MASK;
}

static inline u32 get_session_index(u32 session)
{
return (session >> 16) & LOWER_TWO_BYTE_MASK;
}

int amdtee_open_session(struct tee_context *ctx,
struct tee_ioctl_open_session_arg *arg,
struct tee_param *param);

int amdtee_close_session(struct tee_context *ctx, u32 session);

int amdtee_invoke_func(struct tee_context *ctx,
struct tee_ioctl_invoke_arg *arg,
struct tee_param *param);

int amdtee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);

int amdtee_map_shmem(struct tee_shm *shm);

void amdtee_unmap_shmem(struct tee_shm *shm);

int handle_load_ta(void *data, u32 size,
struct tee_ioctl_open_session_arg *arg);

int handle_unload_ta(u32 ta_handle);

int handle_open_session(struct tee_ioctl_open_session_arg *arg, u32 *info,
struct tee_param *p);

int handle_close_session(u32 ta_handle, u32 info);

int handle_map_shmem(u32 count, struct shmem_desc *start, u32 *buf_id);

void handle_unmap_shmem(u32 buf_id);

int handle_invoke_cmd(struct tee_ioctl_invoke_arg *arg, u32 sinfo,
struct tee_param *p);

struct tee_shm_pool *amdtee_config_shm(void);

u32 get_buffer_id(struct tee_shm *shm);
#endif /*AMDTEE_PRIVATE_H*/
Loading

0 comments on commit 757cc3e

Please sign in to comment.