hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */
#ifndef __TEE_CORE_DRV_H__
#define __TEE_CORE_DRV_H__
 
#include <linux/klist.h>
#include <linux/device.h>
#include <linux/file.h>
#include <linux/cdev.h>
#include <linux/debugfs.h>
#include <linux/miscdevice.h>
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/scatterlist.h>
 
#include <linux/types.h>
 
#include <linux/tee_client_api.h>
 
struct tee_cmd_io;
struct tee_shm_io;
struct tee_rpc;
 
enum tee_state {
   TEE_OFFLINE = 0,
   TEE_ONLINE = 1,
   TEE_SUSPENDED = 2,
   TEE_RUNNING = 3,
   TEE_CRASHED = 4,
   TEE_LAST = 5,
};
 
#define TEE_CONF_TEST_MODE        0x01000000
#define TEE_CONF_FW_NOT_CAPABLE        0x00000001
 
struct tee_stats_entry {
   int count;
   int max;
};
 
#define TEE_STATS_CONTEXT_IDX   0
#define TEE_STATS_SESSION_IDX   1
#define TEE_STATS_SHM_IDX       2
 
#define TEE_MAX_TEE_DEV_NAME (64)
struct tee {
   struct klist_node node;
   char name[TEE_MAX_TEE_DEV_NAME];
   int id;
   void *priv;
   const struct tee_ops *ops;
   struct device *dev;
   struct miscdevice miscdev;
   struct tee_rpc *rpc;
   struct dentry *dbg_dir;
   atomic_t refcount;
   int max_refcount;
   struct tee_stats_entry stats[3];
   struct list_head list_ctx;
   struct list_head list_rpc_shm;
   struct mutex lock;
   unsigned int state;
   uint32_t shm_flags;    /* supported flags for shm allocation */
   uint32_t conf;
   uint32_t test;
};
 
#define _DEV(tee) (tee->miscdev.this_device)
 
#define TEE_MAX_CLIENT_NAME (128)
 
/**
 * struct tee_context - internal structure to store a TEE context.
 *
 * @tee: tee attached to the tee_context
 * @usr_client: flag to known if the client is user side client
 * @entry: list of tee_context
 * @list_sess: list of tee_session that denotes all tee_session attached
 * @list_shm: list of tee_shm that denotes all tee_shm attached
 * @refcount: number of objects which reference it (including itself)
 */
struct tee_context {
   struct tee *tee;
   char name[TEE_MAX_CLIENT_NAME];
   int tgid;
   int usr_client;
   struct list_head entry;
   struct list_head list_sess;
   struct list_head list_shm;
   struct kref refcount;
};
 
/**
 * struct tee_session - internal structure to store a TEE session.
 *
 * @entry: list of tee_context
 * @ctx: tee_context attached to the tee_session
 * @sessid: session ID returned by the secure world
 * @priv: exporter specific private data for this buffer object
 */
struct tee_session {
   struct list_head entry;
   struct tee_context *ctx;
   uint32_t sessid;
   void *priv;
};
 
struct tee_shm_dma_buf {
   struct dma_buf_attachment *attach;
   struct sg_table *sgt;
   bool tee_allocated;
};
 
/**
 * struct tee_shm - internal structure to store a shm object.
 *
 * @ctx: tee_context attached to the buffer.
 * @tee: tee attached to the buffer.
 * @dev: device attached to the buffer.
 * @size_req: requested size for the buffer
 * @size_alloc: effective size of the buffer
 * @kaddr: kernel address if mapped kernel side
 * @paddr: physical address
 * @flags: flags which denote the type of the buffer
 * @entry: list of tee_shm
 */
struct tee_shm {
   struct list_head entry;
   struct tee_context *ctx;
   struct tee *tee;
   struct device *dev;
   size_t size_req;
   size_t size_alloc;
   uint32_t flags;
   void *kaddr;
   dma_addr_t paddr;
   struct sg_table sgt;
   struct tee_shm_dma_buf *sdb;
};
 
#define TEE_SHM_MAPPED            0x01000000
#define TEE_SHM_TEMP            0x02000000
#define TEE_SHM_FROM_RPC        0x04000000
#define TEE_SHM_REGISTERED        0x08000000
#define TEE_SHM_MEMREF            0x10000000
#define TEE_SHM_CACHED            0x20000000
 
#define TEE_SHM_DRV_PRIV_MASK        0xFF000000
 
struct tee_data {
   uint32_t type;
   uint32_t type_original;
   TEEC_SharedMemory c_shm[TEEC_CONFIG_PAYLOAD_REF_COUNT];
   union {
       struct tee_shm *shm;
       TEEC_Value value;
   } params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
};
 
struct tee_cmd {
   TEEC_Result err;
   uint32_t origin;
   uint32_t cmd;
   struct tee_shm *uuid;
   struct tee_shm *ta;
   struct tee_data param;
};
 
struct tee_shm *tee_shm_alloc_from_rpc(struct tee *tee, size_t size);
void tee_shm_free_from_rpc(struct tee_shm *);
 
int tee_core_add(struct tee *tee);
int tee_core_del(struct tee *tee);
 
struct tee *tee_core_alloc(struct device *dev, char *name, int id,
              const struct tee_ops *ops, size_t len);
int tee_core_free(struct tee *tee);
 
struct tee_ops {
   struct module *owner;
   const char *type;
 
   int (*start)(struct tee *tee);
   int (*stop)(struct tee *tee);
   int (*open)(struct tee_session *sess, struct tee_cmd *cmd);
   int (*close)(struct tee_session *sess);
   int (*invoke)(struct tee_session *sess, struct tee_cmd *cmd);
   int (*cancel)(struct tee_session *sess, struct tee_cmd *cmd);
   struct tee_shm *(*alloc)(struct tee *tee, size_t size,
                 uint32_t flags);
   void (*free)(struct tee_shm *shm);
   int (*shm_inc_ref)(struct tee_shm *shm);
};
 
#endif /* __TEE_CORE_DRV_H__ */