1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2015-2021, Linaro Limited
4  */
5 
6 #ifndef OPTEE_PRIVATE_H
7 #define OPTEE_PRIVATE_H
8 
9 #include <linux/arm-smccc.h>
10 #include <linux/rhashtable.h>
11 #include <linux/semaphore.h>
12 #include <linux/tee_drv.h>
13 #include <linux/types.h>
14 #include "optee_msg.h"
15 
16 #define DRIVER_NAME "optee"
17 
18 #define OPTEE_MAX_ARG_SIZE	1024
19 
20 /* Some Global Platform error codes used in this driver */
21 #define TEEC_SUCCESS			0x00000000
22 #define TEEC_ERROR_BAD_PARAMETERS	0xFFFF0006
23 #define TEEC_ERROR_NOT_SUPPORTED	0xFFFF000A
24 #define TEEC_ERROR_COMMUNICATION	0xFFFF000E
25 #define TEEC_ERROR_OUT_OF_MEMORY	0xFFFF000C
26 #define TEEC_ERROR_BUSY			0xFFFF000D
27 #define TEEC_ERROR_SHORT_BUFFER		0xFFFF0010
28 
29 #define TEEC_ORIGIN_COMMS		0x00000002
30 
31 typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
32 				unsigned long, unsigned long, unsigned long,
33 				unsigned long, unsigned long,
34 				struct arm_smccc_res *);
35 
36 struct optee_call_waiter {
37 	struct list_head list_node;
38 	struct completion c;
39 };
40 
41 struct optee_call_queue {
42 	/* Serializes access to this struct */
43 	struct mutex mutex;
44 	struct list_head waiters;
45 };
46 
47 struct optee_wait_queue {
48 	/* Serializes access to this struct */
49 	struct mutex mu;
50 	struct list_head db;
51 };
52 
53 /**
54  * struct optee_supp - supplicant synchronization struct
55  * @ctx			the context of current connected supplicant.
56  *			if !NULL the supplicant device is available for use,
57  *			else busy
58  * @mutex:		held while accessing content of this struct
59  * @req_id:		current request id if supplicant is doing synchronous
60  *			communication, else -1
61  * @reqs:		queued request not yet retrieved by supplicant
62  * @idr:		IDR holding all requests currently being processed
63  *			by supplicant
64  * @reqs_c:		completion used by supplicant when waiting for a
65  *			request to be queued.
66  */
67 struct optee_supp {
68 	/* Serializes access to this struct */
69 	struct mutex mutex;
70 	struct tee_context *ctx;
71 
72 	int req_id;
73 	struct list_head reqs;
74 	struct idr idr;
75 	struct completion reqs_c;
76 };
77 
78 struct optee_smc {
79 	optee_invoke_fn *invoke_fn;
80 	void *memremaped_shm;
81 	u32 sec_caps;
82 };
83 
84 /**
85  * struct optee_ffa_data -  FFA communication struct
86  * @ffa_dev		FFA device, contains the destination id, the id of
87  *			OP-TEE in secure world
88  * @ffa_ops		FFA operations
89  * @mutex		Serializes access to @global_ids
90  * @global_ids		FF-A shared memory global handle translation
91  */
92 struct optee_ffa {
93 	struct ffa_device *ffa_dev;
94 	const struct ffa_dev_ops *ffa_ops;
95 	/* Serializes access to @global_ids */
96 	struct mutex mutex;
97 	struct rhashtable global_ids;
98 };
99 
100 struct optee;
101 
102 /**
103  * struct optee_ops - OP-TEE driver internal operations
104  * @do_call_with_arg:	enters OP-TEE in secure world
105  * @to_msg_param:	converts from struct tee_param to OPTEE_MSG parameters
106  * @from_msg_param:	converts from OPTEE_MSG parameters to struct tee_param
107  *
108  * These OPs are only supposed to be used internally in the OP-TEE driver
109  * as a way of abstracting the different methogs of entering OP-TEE in
110  * secure world.
111  */
112 struct optee_ops {
113 	int (*do_call_with_arg)(struct tee_context *ctx,
114 				struct tee_shm *shm_arg);
115 	int (*to_msg_param)(struct optee *optee,
116 			    struct optee_msg_param *msg_params,
117 			    size_t num_params, const struct tee_param *params);
118 	int (*from_msg_param)(struct optee *optee, struct tee_param *params,
119 			      size_t num_params,
120 			      const struct optee_msg_param *msg_params);
121 };
122 
123 /**
124  * struct optee - main service struct
125  * @supp_teedev:	supplicant device
126  * @ops:		internal callbacks for different ways to reach secure
127  *			world
128  * @teedev:		client device
129  * @smc:		specific to SMC ABI
130  * @ffa:		specific to FF-A ABI
131  * @call_queue:		queue of threads waiting to call @invoke_fn
132  * @wait_queue:		queue of threads from secure world waiting for a
133  *			secure world sync object
134  * @supp:		supplicant synchronization struct for RPC to supplicant
135  * @pool:		shared memory pool
136  * @rpc_arg_count:	If > 0 number of RPC parameters to make room for
137  * @scan_bus_done	flag if device registation was already done.
138  * @scan_bus_wq		workqueue to scan optee bus and register optee drivers
139  * @scan_bus_work	workq to scan optee bus and register optee drivers
140  */
141 struct optee {
142 	struct tee_device *supp_teedev;
143 	struct tee_device *teedev;
144 	const struct optee_ops *ops;
145 	union {
146 		struct optee_smc smc;
147 		struct optee_ffa ffa;
148 	};
149 	struct optee_call_queue call_queue;
150 	struct optee_wait_queue wait_queue;
151 	struct optee_supp supp;
152 	struct tee_shm_pool *pool;
153 	unsigned int rpc_arg_count;
154 	bool   scan_bus_done;
155 	struct workqueue_struct *scan_bus_wq;
156 	struct work_struct scan_bus_work;
157 };
158 
159 struct optee_session {
160 	struct list_head list_node;
161 	u32 session_id;
162 };
163 
164 struct optee_context_data {
165 	/* Serializes access to this struct */
166 	struct mutex mutex;
167 	struct list_head sess_list;
168 };
169 
170 struct optee_rpc_param {
171 	u32	a0;
172 	u32	a1;
173 	u32	a2;
174 	u32	a3;
175 	u32	a4;
176 	u32	a5;
177 	u32	a6;
178 	u32	a7;
179 };
180 
181 /* Holds context that is preserved during one STD call */
182 struct optee_call_ctx {
183 	/* information about pages list used in last allocation */
184 	void *pages_list;
185 	size_t num_entries;
186 };
187 
188 void optee_wait_queue_init(struct optee_wait_queue *wq);
189 void optee_wait_queue_exit(struct optee_wait_queue *wq);
190 
191 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
192 			struct tee_param *param);
193 
194 int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
195 int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
196 void optee_supp_init(struct optee_supp *supp);
197 void optee_supp_uninit(struct optee_supp *supp);
198 void optee_supp_release(struct optee_supp *supp);
199 
200 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
201 		    struct tee_param *param);
202 int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
203 		    struct tee_param *param);
204 
205 int optee_open_session(struct tee_context *ctx,
206 		       struct tee_ioctl_open_session_arg *arg,
207 		       struct tee_param *param);
208 int optee_close_session_helper(struct tee_context *ctx, u32 session);
209 int optee_close_session(struct tee_context *ctx, u32 session);
210 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
211 		      struct tee_param *param);
212 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
213 
214 #define PTA_CMD_GET_DEVICES		0x0
215 #define PTA_CMD_GET_DEVICES_SUPP	0x1
216 int optee_enumerate_devices(u32 func);
217 void optee_unregister_devices(void);
218 
219 int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm,
220 			       struct tee_shm *shm, size_t size,
221 			       int (*shm_register)(struct tee_context *ctx,
222 						   struct tee_shm *shm,
223 						   struct page **pages,
224 						   size_t num_pages,
225 						   unsigned long start));
226 
227 
228 void optee_remove_common(struct optee *optee);
229 int optee_open(struct tee_context *ctx, bool cap_memref_null);
230 void optee_release(struct tee_context *ctx);
231 void optee_release_supp(struct tee_context *ctx);
232 
233 static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,
234 					      const struct optee_msg_param *mp)
235 {
236 	p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
237 		  attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
238 	p->u.value.a = mp->u.value.a;
239 	p->u.value.b = mp->u.value.b;
240 	p->u.value.c = mp->u.value.c;
241 }
242 
243 static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
244 					    const struct tee_param *p)
245 {
246 	mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
247 		   TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
248 	mp->u.value.a = p->u.value.a;
249 	mp->u.value.b = p->u.value.b;
250 	mp->u.value.c = p->u.value.c;
251 }
252 
253 void optee_cq_wait_init(struct optee_call_queue *cq,
254 			struct optee_call_waiter *w);
255 void optee_cq_wait_for_completion(struct optee_call_queue *cq,
256 				  struct optee_call_waiter *w);
257 void optee_cq_wait_final(struct optee_call_queue *cq,
258 			 struct optee_call_waiter *w);
259 int optee_check_mem_type(unsigned long start, size_t num_pages);
260 struct tee_shm *optee_get_msg_arg(struct tee_context *ctx, size_t num_params,
261 				  struct optee_msg_arg **msg_arg);
262 
263 struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
264 void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
265 void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
266 		   struct optee_msg_arg *arg);
267 
268 /*
269  * Small helpers
270  */
271 
272 static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
273 {
274 	return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
275 }
276 
277 static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
278 {
279 	*reg0 = val >> 32;
280 	*reg1 = val;
281 }
282 
283 /* Registration of the ABIs */
284 int optee_smc_abi_register(void);
285 void optee_smc_abi_unregister(void);
286 int optee_ffa_abi_register(void);
287 void optee_ffa_abi_unregister(void);
288 
289 #endif /*OPTEE_PRIVATE_H*/
290