1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2015-2021, Linaro Limited
6 #ifndef OPTEE_PRIVATE_H
7 #define OPTEE_PRIVATE_H
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"
16 #define DRIVER_NAME "optee"
18 #define OPTEE_MAX_ARG_SIZE 1024
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
29 #define TEEC_ORIGIN_COMMS 0x00000002
32 * This value should be larger than the number threads in secure world to
33 * meet the need from secure world. The number of threads in secure world
34 * are usually not even close to 255 so we should be safe for now.
36 #define OPTEE_DEFAULT_MAX_NOTIF_VALUE 255
38 typedef void (optee_invoke_fn
)(unsigned long, unsigned long, unsigned long,
39 unsigned long, unsigned long, unsigned long,
40 unsigned long, unsigned long,
41 struct arm_smccc_res
*);
43 struct optee_call_waiter
{
44 struct list_head list_node
;
48 struct optee_call_queue
{
49 /* Serializes access to this struct */
51 struct list_head waiters
;
56 /* Serializes access to the elements below in this struct */
62 #define OPTEE_SHM_ARG_ALLOC_PRIV BIT(0)
63 #define OPTEE_SHM_ARG_SHARED BIT(1)
64 struct optee_shm_arg_entry
;
65 struct optee_shm_arg_cache
{
67 /* Serializes access to this struct */
69 struct list_head shm_args
;
73 * struct optee_supp - supplicant synchronization struct
74 * @ctx the context of current connected supplicant.
75 * if !NULL the supplicant device is available for use,
77 * @mutex: held while accessing content of this struct
78 * @req_id: current request id if supplicant is doing synchronous
79 * communication, else -1
80 * @reqs: queued request not yet retrieved by supplicant
81 * @idr: IDR holding all requests currently being processed
83 * @reqs_c: completion used by supplicant when waiting for a
84 * request to be queued.
87 /* Serializes access to this struct */
89 struct tee_context
*ctx
;
92 struct list_head reqs
;
94 struct completion reqs_c
;
98 * struct optee_pcpu - per cpu notif private struct passed to work functions
99 * @optee optee device reference
106 * struct optee_smc - optee smc communication struct
107 * @invoke_fn handler function to invoke secure monitor
108 * @memremaped_shm virtual address of memory in shared memory pool
109 * @sec_caps: secure world capabilities defined by
110 * OPTEE_SMC_SEC_CAP_* in optee_smc.h
111 * @notif_irq interrupt used as async notification by OP-TEE or 0
112 * @optee_pcpu per_cpu optee instance for per cpu work or NULL
113 * @notif_pcpu_wq workqueue for per cpu asynchronous notification or NULL
114 * @notif_pcpu_work work for per cpu asynchronous notification
115 * @notif_cpuhp_state CPU hotplug state assigned for pcpu interrupt management
118 optee_invoke_fn
*invoke_fn
;
119 void *memremaped_shm
;
121 unsigned int notif_irq
;
122 struct optee_pcpu __percpu
*optee_pcpu
;
123 struct workqueue_struct
*notif_pcpu_wq
;
124 struct work_struct notif_pcpu_work
;
125 unsigned int notif_cpuhp_state
;
129 * struct optee_ffa_data - FFA communication struct
130 * @ffa_dev FFA device, contains the destination id, the id of
131 * OP-TEE in secure world
132 * @ffa_ops FFA operations
133 * @mutex Serializes access to @global_ids
134 * @global_ids FF-A shared memory global handle translation
137 struct ffa_device
*ffa_dev
;
138 /* Serializes access to @global_ids */
140 struct rhashtable global_ids
;
146 * struct optee_ops - OP-TEE driver internal operations
147 * @do_call_with_arg: enters OP-TEE in secure world
148 * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters
149 * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param
151 * These OPs are only supposed to be used internally in the OP-TEE driver
152 * as a way of abstracting the different methogs of entering OP-TEE in
156 int (*do_call_with_arg
)(struct tee_context
*ctx
,
157 struct tee_shm
*shm_arg
, u_int offs
);
158 int (*to_msg_param
)(struct optee
*optee
,
159 struct optee_msg_param
*msg_params
,
160 size_t num_params
, const struct tee_param
*params
);
161 int (*from_msg_param
)(struct optee
*optee
, struct tee_param
*params
,
163 const struct optee_msg_param
*msg_params
);
167 * struct optee - main service struct
168 * @supp_teedev: supplicant device
169 * @teedev: client device
170 * @ops: internal callbacks for different ways to reach secure
172 * @ctx: driver internal TEE context
173 * @smc: specific to SMC ABI
174 * @ffa: specific to FF-A ABI
175 * @call_queue: queue of threads waiting to call @invoke_fn
176 * @notif: notification synchronization struct
177 * @supp: supplicant synchronization struct for RPC to supplicant
178 * @pool: shared memory pool
179 * @rpc_param_count: If > 0 number of RPC parameters to make room for
180 * @scan_bus_done flag if device registation was already done.
181 * @scan_bus_wq workqueue to scan optee bus and register optee drivers
182 * @scan_bus_work workq to scan optee bus and register optee drivers
185 struct tee_device
*supp_teedev
;
186 struct tee_device
*teedev
;
187 const struct optee_ops
*ops
;
188 struct tee_context
*ctx
;
190 struct optee_smc smc
;
191 struct optee_ffa ffa
;
193 struct optee_shm_arg_cache shm_arg_cache
;
194 struct optee_call_queue call_queue
;
195 struct optee_notif notif
;
196 struct optee_supp supp
;
197 struct tee_shm_pool
*pool
;
198 unsigned int rpc_param_count
;
200 struct workqueue_struct
*scan_bus_wq
;
201 struct work_struct scan_bus_work
;
204 struct optee_session
{
205 struct list_head list_node
;
209 struct optee_context_data
{
210 /* Serializes access to this struct */
212 struct list_head sess_list
;
215 struct optee_rpc_param
{
226 /* Holds context that is preserved during one STD call */
227 struct optee_call_ctx
{
228 /* information about pages list used in last allocation */
233 int optee_notif_init(struct optee
*optee
, u_int max_key
);
234 void optee_notif_uninit(struct optee
*optee
);
235 int optee_notif_wait(struct optee
*optee
, u_int key
);
236 int optee_notif_send(struct optee
*optee
, u_int key
);
238 u32
optee_supp_thrd_req(struct tee_context
*ctx
, u32 func
, size_t num_params
,
239 struct tee_param
*param
);
241 void optee_supp_init(struct optee_supp
*supp
);
242 void optee_supp_uninit(struct optee_supp
*supp
);
243 void optee_supp_release(struct optee_supp
*supp
);
245 int optee_supp_recv(struct tee_context
*ctx
, u32
*func
, u32
*num_params
,
246 struct tee_param
*param
);
247 int optee_supp_send(struct tee_context
*ctx
, u32 ret
, u32 num_params
,
248 struct tee_param
*param
);
250 int optee_open_session(struct tee_context
*ctx
,
251 struct tee_ioctl_open_session_arg
*arg
,
252 struct tee_param
*param
);
253 int optee_close_session_helper(struct tee_context
*ctx
, u32 session
);
254 int optee_close_session(struct tee_context
*ctx
, u32 session
);
255 int optee_invoke_func(struct tee_context
*ctx
, struct tee_ioctl_invoke_arg
*arg
,
256 struct tee_param
*param
);
257 int optee_cancel_req(struct tee_context
*ctx
, u32 cancel_id
, u32 session
);
259 #define PTA_CMD_GET_DEVICES 0x0
260 #define PTA_CMD_GET_DEVICES_SUPP 0x1
261 int optee_enumerate_devices(u32 func
);
262 void optee_unregister_devices(void);
264 int optee_pool_op_alloc_helper(struct tee_shm_pool
*pool
, struct tee_shm
*shm
,
265 size_t size
, size_t align
,
266 int (*shm_register
)(struct tee_context
*ctx
,
270 unsigned long start
));
271 void optee_pool_op_free_helper(struct tee_shm_pool
*pool
, struct tee_shm
*shm
,
272 int (*shm_unregister
)(struct tee_context
*ctx
,
273 struct tee_shm
*shm
));
276 void optee_remove_common(struct optee
*optee
);
277 int optee_open(struct tee_context
*ctx
, bool cap_memref_null
);
278 void optee_release(struct tee_context
*ctx
);
279 void optee_release_supp(struct tee_context
*ctx
);
281 static inline void optee_from_msg_param_value(struct tee_param
*p
, u32 attr
,
282 const struct optee_msg_param
*mp
)
284 p
->attr
= TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
+
285 attr
- OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
;
286 p
->u
.value
.a
= mp
->u
.value
.a
;
287 p
->u
.value
.b
= mp
->u
.value
.b
;
288 p
->u
.value
.c
= mp
->u
.value
.c
;
291 static inline void optee_to_msg_param_value(struct optee_msg_param
*mp
,
292 const struct tee_param
*p
)
294 mp
->attr
= OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
+ p
->attr
-
295 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
;
296 mp
->u
.value
.a
= p
->u
.value
.a
;
297 mp
->u
.value
.b
= p
->u
.value
.b
;
298 mp
->u
.value
.c
= p
->u
.value
.c
;
301 void optee_cq_wait_init(struct optee_call_queue
*cq
,
302 struct optee_call_waiter
*w
);
303 void optee_cq_wait_for_completion(struct optee_call_queue
*cq
,
304 struct optee_call_waiter
*w
);
305 void optee_cq_wait_final(struct optee_call_queue
*cq
,
306 struct optee_call_waiter
*w
);
307 int optee_check_mem_type(unsigned long start
, size_t num_pages
);
309 void optee_shm_arg_cache_init(struct optee
*optee
, u32 flags
);
310 void optee_shm_arg_cache_uninit(struct optee
*optee
);
311 struct optee_msg_arg
*optee_get_msg_arg(struct tee_context
*ctx
,
313 struct optee_shm_arg_entry
**entry
,
314 struct tee_shm
**shm_ret
,
316 void optee_free_msg_arg(struct tee_context
*ctx
,
317 struct optee_shm_arg_entry
*entry
, u_int offs
);
318 size_t optee_msg_arg_size(size_t rpc_param_count
);
321 struct tee_shm
*optee_rpc_cmd_alloc_suppl(struct tee_context
*ctx
, size_t sz
);
322 void optee_rpc_cmd_free_suppl(struct tee_context
*ctx
, struct tee_shm
*shm
);
323 void optee_rpc_cmd(struct tee_context
*ctx
, struct optee
*optee
,
324 struct optee_msg_arg
*arg
);
330 static inline void *reg_pair_to_ptr(u32 reg0
, u32 reg1
)
332 return (void *)(unsigned long)(((u64
)reg0
<< 32) | reg1
);
335 static inline void reg_pair_from_64(u32
*reg0
, u32
*reg1
, u64 val
)
341 /* Registration of the ABIs */
342 int optee_smc_abi_register(void);
343 void optee_smc_abi_unregister(void);
344 int optee_ffa_abi_register(void);
345 void optee_ffa_abi_unregister(void);
347 #endif /*OPTEE_PRIVATE_H*/