18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: MIT */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2019,2021 Advanced Micro Devices, Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Rijo Thomas <Rijo-john.Thomas@amd.com>
68c2ecf20Sopenharmony_ci * Author: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/* This file describes the TEE communication interface between host and AMD
118c2ecf20Sopenharmony_ci * Secure Processor
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifndef __TEE_DEV_H__
158c2ecf20Sopenharmony_ci#define __TEE_DEV_H__
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/device.h>
188c2ecf20Sopenharmony_ci#include <linux/mutex.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define TEE_DEFAULT_TIMEOUT		10
218c2ecf20Sopenharmony_ci#define MAX_BUFFER_SIZE			988
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/**
248c2ecf20Sopenharmony_ci * enum tee_ring_cmd_id - TEE interface commands for ring buffer configuration
258c2ecf20Sopenharmony_ci * @TEE_RING_INIT_CMD:		Initialize ring buffer
268c2ecf20Sopenharmony_ci * @TEE_RING_DESTROY_CMD:	Destroy ring buffer
278c2ecf20Sopenharmony_ci * @TEE_RING_MAX_CMD:		Maximum command id
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_cienum tee_ring_cmd_id {
308c2ecf20Sopenharmony_ci	TEE_RING_INIT_CMD		= 0x00010000,
318c2ecf20Sopenharmony_ci	TEE_RING_DESTROY_CMD		= 0x00020000,
328c2ecf20Sopenharmony_ci	TEE_RING_MAX_CMD		= 0x000F0000,
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/**
368c2ecf20Sopenharmony_ci * struct tee_init_ring_cmd - Command to init TEE ring buffer
378c2ecf20Sopenharmony_ci * @low_addr:  bits [31:0] of the physical address of ring buffer
388c2ecf20Sopenharmony_ci * @hi_addr:   bits [63:32] of the physical address of ring buffer
398c2ecf20Sopenharmony_ci * @size:      size of ring buffer in bytes
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cistruct tee_init_ring_cmd {
428c2ecf20Sopenharmony_ci	u32 low_addr;
438c2ecf20Sopenharmony_ci	u32 hi_addr;
448c2ecf20Sopenharmony_ci	u32 size;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define MAX_RING_BUFFER_ENTRIES		32
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/**
508c2ecf20Sopenharmony_ci * struct ring_buf_manager - Helper structure to manage ring buffer.
518c2ecf20Sopenharmony_ci * @ring_start:  starting address of ring buffer
528c2ecf20Sopenharmony_ci * @ring_size:   size of ring buffer in bytes
538c2ecf20Sopenharmony_ci * @ring_pa:     physical address of ring buffer
548c2ecf20Sopenharmony_ci * @wptr:        index to the last written entry in ring buffer
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_cistruct ring_buf_manager {
578c2ecf20Sopenharmony_ci	struct mutex mutex;	/* synchronizes access to ring buffer */
588c2ecf20Sopenharmony_ci	void *ring_start;
598c2ecf20Sopenharmony_ci	u32 ring_size;
608c2ecf20Sopenharmony_ci	phys_addr_t ring_pa;
618c2ecf20Sopenharmony_ci	u32 wptr;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct psp_tee_device {
658c2ecf20Sopenharmony_ci	struct device *dev;
668c2ecf20Sopenharmony_ci	struct psp_device *psp;
678c2ecf20Sopenharmony_ci	void __iomem *io_regs;
688c2ecf20Sopenharmony_ci	struct tee_vdata *vdata;
698c2ecf20Sopenharmony_ci	struct ring_buf_manager rb_mgr;
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/**
738c2ecf20Sopenharmony_ci * enum tee_cmd_state - TEE command states for the ring buffer interface
748c2ecf20Sopenharmony_ci * @TEE_CMD_STATE_INIT:      initial state of command when sent from host
758c2ecf20Sopenharmony_ci * @TEE_CMD_STATE_PROCESS:   command being processed by TEE environment
768c2ecf20Sopenharmony_ci * @TEE_CMD_STATE_COMPLETED: command processing completed
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_cienum tee_cmd_state {
798c2ecf20Sopenharmony_ci	TEE_CMD_STATE_INIT,
808c2ecf20Sopenharmony_ci	TEE_CMD_STATE_PROCESS,
818c2ecf20Sopenharmony_ci	TEE_CMD_STATE_COMPLETED,
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/**
858c2ecf20Sopenharmony_ci * enum cmd_resp_state - TEE command's response status maintained by driver
868c2ecf20Sopenharmony_ci * @CMD_RESPONSE_INVALID:      initial state when no command is written to ring
878c2ecf20Sopenharmony_ci * @CMD_WAITING_FOR_RESPONSE:  driver waiting for response from TEE
888c2ecf20Sopenharmony_ci * @CMD_RESPONSE_TIMEDOUT:     failed to get response from TEE
898c2ecf20Sopenharmony_ci * @CMD_RESPONSE_COPIED:       driver has copied response from TEE
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cienum cmd_resp_state {
928c2ecf20Sopenharmony_ci	CMD_RESPONSE_INVALID,
938c2ecf20Sopenharmony_ci	CMD_WAITING_FOR_RESPONSE,
948c2ecf20Sopenharmony_ci	CMD_RESPONSE_TIMEDOUT,
958c2ecf20Sopenharmony_ci	CMD_RESPONSE_COPIED,
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/**
998c2ecf20Sopenharmony_ci * struct tee_ring_cmd - Structure of the command buffer in TEE ring
1008c2ecf20Sopenharmony_ci * @cmd_id:      refers to &enum tee_cmd_id. Command id for the ring buffer
1018c2ecf20Sopenharmony_ci *               interface
1028c2ecf20Sopenharmony_ci * @cmd_state:   refers to &enum tee_cmd_state
1038c2ecf20Sopenharmony_ci * @status:      status of TEE command execution
1048c2ecf20Sopenharmony_ci * @res0:        reserved region
1058c2ecf20Sopenharmony_ci * @pdata:       private data (currently unused)
1068c2ecf20Sopenharmony_ci * @res1:        reserved region
1078c2ecf20Sopenharmony_ci * @buf:         TEE command specific buffer
1088c2ecf20Sopenharmony_ci * @flag:	 refers to &enum cmd_resp_state
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cistruct tee_ring_cmd {
1118c2ecf20Sopenharmony_ci	u32 cmd_id;
1128c2ecf20Sopenharmony_ci	u32 cmd_state;
1138c2ecf20Sopenharmony_ci	u32 status;
1148c2ecf20Sopenharmony_ci	u32 res0[1];
1158c2ecf20Sopenharmony_ci	u64 pdata;
1168c2ecf20Sopenharmony_ci	u32 res1[2];
1178c2ecf20Sopenharmony_ci	u8 buf[MAX_BUFFER_SIZE];
1188c2ecf20Sopenharmony_ci	u32 flag;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Total size: 1024 bytes */
1218c2ecf20Sopenharmony_ci} __packed;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciint tee_dev_init(struct psp_device *psp);
1248c2ecf20Sopenharmony_civoid tee_dev_destroy(struct psp_device *psp);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#endif /* __TEE_DEV_H__ */
127