1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright (C) 2019,2021 Advanced Micro Devices, Inc.
4  *
5  * Author: Rijo Thomas <Rijo-john.Thomas@amd.com>
6  * Author: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
7  *
8  */
9 
10 /* This file describes the TEE communication interface between host and AMD
11  * Secure Processor
12  */
13 
14 #ifndef __TEE_DEV_H__
15 #define __TEE_DEV_H__
16 
17 #include <linux/device.h>
18 #include <linux/mutex.h>
19 
20 #define TEE_DEFAULT_TIMEOUT		10
21 #define MAX_BUFFER_SIZE			988
22 
23 /**
24  * enum tee_ring_cmd_id - TEE interface commands for ring buffer configuration
25  * @TEE_RING_INIT_CMD:		Initialize ring buffer
26  * @TEE_RING_DESTROY_CMD:	Destroy ring buffer
27  * @TEE_RING_MAX_CMD:		Maximum command id
28  */
29 enum tee_ring_cmd_id {
30 	TEE_RING_INIT_CMD		= 0x00010000,
31 	TEE_RING_DESTROY_CMD		= 0x00020000,
32 	TEE_RING_MAX_CMD		= 0x000F0000,
33 };
34 
35 /**
36  * struct tee_init_ring_cmd - Command to init TEE ring buffer
37  * @low_addr:  bits [31:0] of the physical address of ring buffer
38  * @hi_addr:   bits [63:32] of the physical address of ring buffer
39  * @size:      size of ring buffer in bytes
40  */
41 struct tee_init_ring_cmd {
42 	u32 low_addr;
43 	u32 hi_addr;
44 	u32 size;
45 };
46 
47 #define MAX_RING_BUFFER_ENTRIES		32
48 
49 /**
50  * struct ring_buf_manager - Helper structure to manage ring buffer.
51  * @ring_start:  starting address of ring buffer
52  * @ring_size:   size of ring buffer in bytes
53  * @ring_pa:     physical address of ring buffer
54  * @wptr:        index to the last written entry in ring buffer
55  */
56 struct ring_buf_manager {
57 	struct mutex mutex;	/* synchronizes access to ring buffer */
58 	void *ring_start;
59 	u32 ring_size;
60 	phys_addr_t ring_pa;
61 	u32 wptr;
62 };
63 
64 struct psp_tee_device {
65 	struct device *dev;
66 	struct psp_device *psp;
67 	void __iomem *io_regs;
68 	struct tee_vdata *vdata;
69 	struct ring_buf_manager rb_mgr;
70 };
71 
72 /**
73  * enum tee_cmd_state - TEE command states for the ring buffer interface
74  * @TEE_CMD_STATE_INIT:      initial state of command when sent from host
75  * @TEE_CMD_STATE_PROCESS:   command being processed by TEE environment
76  * @TEE_CMD_STATE_COMPLETED: command processing completed
77  */
78 enum tee_cmd_state {
79 	TEE_CMD_STATE_INIT,
80 	TEE_CMD_STATE_PROCESS,
81 	TEE_CMD_STATE_COMPLETED,
82 };
83 
84 /**
85  * enum cmd_resp_state - TEE command's response status maintained by driver
86  * @CMD_RESPONSE_INVALID:      initial state when no command is written to ring
87  * @CMD_WAITING_FOR_RESPONSE:  driver waiting for response from TEE
88  * @CMD_RESPONSE_TIMEDOUT:     failed to get response from TEE
89  * @CMD_RESPONSE_COPIED:       driver has copied response from TEE
90  */
91 enum cmd_resp_state {
92 	CMD_RESPONSE_INVALID,
93 	CMD_WAITING_FOR_RESPONSE,
94 	CMD_RESPONSE_TIMEDOUT,
95 	CMD_RESPONSE_COPIED,
96 };
97 
98 /**
99  * struct tee_ring_cmd - Structure of the command buffer in TEE ring
100  * @cmd_id:      refers to &enum tee_cmd_id. Command id for the ring buffer
101  *               interface
102  * @cmd_state:   refers to &enum tee_cmd_state
103  * @status:      status of TEE command execution
104  * @res0:        reserved region
105  * @pdata:       private data (currently unused)
106  * @res1:        reserved region
107  * @buf:         TEE command specific buffer
108  * @flag:	 refers to &enum cmd_resp_state
109  */
110 struct tee_ring_cmd {
111 	u32 cmd_id;
112 	u32 cmd_state;
113 	u32 status;
114 	u32 res0[1];
115 	u64 pdata;
116 	u32 res1[2];
117 	u8 buf[MAX_BUFFER_SIZE];
118 	u32 flag;
119 
120 	/* Total size: 1024 bytes */
121 } __packed;
122 
123 int tee_dev_init(struct psp_device *psp);
124 void tee_dev_destroy(struct psp_device *psp);
125 
126 #endif /* __TEE_DEV_H__ */
127