blob: 92bc47bef95ffcb65a85990dbb44e6af234da4a6 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +02002/*
Jens Wiklander4602c582021-03-25 15:08:50 +01003 * Copyright (c) 2015-2021, Linaro Limited
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +02004 */
5
6#ifndef OPTEE_PRIVATE_H
7#define OPTEE_PRIVATE_H
8
9#include <linux/arm-smccc.h>
Jens Wiklander4615e5a2021-07-21 17:45:21 +020010#include <linux/rhashtable.h>
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020011#include <linux/semaphore.h>
12#include <linux/tee_drv.h>
13#include <linux/types.h>
14#include "optee_msg.h"
15
Jens Wiklanderc51a5642021-07-21 16:30:28 +020016#define DRIVER_NAME "optee"
17
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020018#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
Jorge Ramirez-Ortizc05210a2020-08-14 13:12:21 +020023#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020024#define TEEC_ERROR_COMMUNICATION 0xFFFF000E
25#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
Jens Wiklander4615e5a2021-07-21 17:45:21 +020026#define TEEC_ERROR_BUSY 0xFFFF000D
Sumit Gargc3fa24a2019-01-29 11:19:37 +053027#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020028
29#define TEEC_ORIGIN_COMMS 0x00000002
30
Jens Wiklander787c80c2021-06-15 22:23:53 +020031/*
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.
35 */
36#define OPTEE_DEFAULT_MAX_NOTIF_VALUE 255
37
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020038typedef 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 *);
42
Jens Wiklanderc51a5642021-07-21 16:30:28 +020043struct optee_call_waiter {
44 struct list_head list_node;
45 struct completion c;
46};
47
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020048struct optee_call_queue {
49 /* Serializes access to this struct */
50 struct mutex mutex;
51 struct list_head waiters;
52};
53
Jens Wiklander787c80c2021-06-15 22:23:53 +020054struct optee_notif {
55 u_int max_key;
56 /* Serializes access to the elements below in this struct */
57 spinlock_t lock;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020058 struct list_head db;
Jens Wiklander787c80c2021-06-15 22:23:53 +020059 u_long *bitmap;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020060};
61
62/**
63 * struct optee_supp - supplicant synchronization struct
64 * @ctx the context of current connected supplicant.
65 * if !NULL the supplicant device is available for use,
66 * else busy
Jens Wiklander1647a5a2016-12-23 13:13:39 +010067 * @mutex: held while accessing content of this struct
68 * @req_id: current request id if supplicant is doing synchronous
69 * communication, else -1
70 * @reqs: queued request not yet retrieved by supplicant
71 * @idr: IDR holding all requests currently being processed
72 * by supplicant
73 * @reqs_c: completion used by supplicant when waiting for a
74 * request to be queued.
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020075 */
76struct optee_supp {
Jens Wiklander1647a5a2016-12-23 13:13:39 +010077 /* Serializes access to this struct */
78 struct mutex mutex;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020079 struct tee_context *ctx;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020080
Jens Wiklander1647a5a2016-12-23 13:13:39 +010081 int req_id;
82 struct list_head reqs;
83 struct idr idr;
84 struct completion reqs_c;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020085};
86
Jens Wiklanderc51a5642021-07-21 16:30:28 +020087struct optee_smc {
88 optee_invoke_fn *invoke_fn;
89 void *memremaped_shm;
90 u32 sec_caps;
Jens Wiklander6749e692021-06-15 22:23:54 +020091 unsigned int notif_irq;
Jens Wiklanderc51a5642021-07-21 16:30:28 +020092};
93
Jens Wiklander4615e5a2021-07-21 17:45:21 +020094/**
95 * struct optee_ffa_data - FFA communication struct
96 * @ffa_dev FFA device, contains the destination id, the id of
97 * OP-TEE in secure world
98 * @ffa_ops FFA operations
99 * @mutex Serializes access to @global_ids
100 * @global_ids FF-A shared memory global handle translation
101 */
102struct optee_ffa {
103 struct ffa_device *ffa_dev;
104 const struct ffa_dev_ops *ffa_ops;
105 /* Serializes access to @global_ids */
106 struct mutex mutex;
107 struct rhashtable global_ids;
108};
109
Jens Wiklander4602c582021-03-25 15:08:50 +0100110struct optee;
111
112/**
113 * struct optee_ops - OP-TEE driver internal operations
114 * @do_call_with_arg: enters OP-TEE in secure world
115 * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters
116 * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param
117 *
118 * These OPs are only supposed to be used internally in the OP-TEE driver
119 * as a way of abstracting the different methogs of entering OP-TEE in
120 * secure world.
121 */
122struct optee_ops {
123 int (*do_call_with_arg)(struct tee_context *ctx,
124 struct tee_shm *shm_arg);
125 int (*to_msg_param)(struct optee *optee,
126 struct optee_msg_param *msg_params,
127 size_t num_params, const struct tee_param *params);
128 int (*from_msg_param)(struct optee *optee, struct tee_param *params,
129 size_t num_params,
130 const struct optee_msg_param *msg_params);
131};
132
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200133/**
134 * struct optee - main service struct
135 * @supp_teedev: supplicant device
Jens Wiklanderaceeafe2022-01-27 15:29:39 +0100136 * @teedev: client device
Jens Wiklander4602c582021-03-25 15:08:50 +0100137 * @ops: internal callbacks for different ways to reach secure
138 * world
Jens Wiklanderaceeafe2022-01-27 15:29:39 +0100139 * @ctx: driver internal TEE context
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200140 * @smc: specific to SMC ABI
Jens Wiklander4615e5a2021-07-21 17:45:21 +0200141 * @ffa: specific to FF-A ABI
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200142 * @call_queue: queue of threads waiting to call @invoke_fn
Jens Wiklander787c80c2021-06-15 22:23:53 +0200143 * @notif: notification synchronization struct
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200144 * @supp: supplicant synchronization struct for RPC to supplicant
145 * @pool: shared memory pool
Jens Wiklander4615e5a2021-07-21 17:45:21 +0200146 * @rpc_arg_count: If > 0 number of RPC parameters to make room for
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300147 * @scan_bus_done flag if device registation was already done.
148 * @scan_bus_wq workqueue to scan optee bus and register optee drivers
149 * @scan_bus_work workq to scan optee bus and register optee drivers
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200150 */
151struct optee {
152 struct tee_device *supp_teedev;
153 struct tee_device *teedev;
Jens Wiklander4602c582021-03-25 15:08:50 +0100154 const struct optee_ops *ops;
Jens Wiklanderaceeafe2022-01-27 15:29:39 +0100155 struct tee_context *ctx;
Jens Wiklander4615e5a2021-07-21 17:45:21 +0200156 union {
157 struct optee_smc smc;
158 struct optee_ffa ffa;
159 };
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200160 struct optee_call_queue call_queue;
Jens Wiklander787c80c2021-06-15 22:23:53 +0200161 struct optee_notif notif;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200162 struct optee_supp supp;
163 struct tee_shm_pool *pool;
Jens Wiklander4615e5a2021-07-21 17:45:21 +0200164 unsigned int rpc_arg_count;
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300165 bool scan_bus_done;
166 struct workqueue_struct *scan_bus_wq;
167 struct work_struct scan_bus_work;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200168};
169
170struct optee_session {
171 struct list_head list_node;
172 u32 session_id;
173};
174
175struct optee_context_data {
176 /* Serializes access to this struct */
177 struct mutex mutex;
178 struct list_head sess_list;
179};
180
181struct optee_rpc_param {
182 u32 a0;
183 u32 a1;
184 u32 a2;
185 u32 a3;
186 u32 a4;
187 u32 a5;
188 u32 a6;
189 u32 a7;
190};
191
Volodymyr Babchuk53a107c2017-11-29 14:48:33 +0200192/* Holds context that is preserved during one STD call */
193struct optee_call_ctx {
194 /* information about pages list used in last allocation */
195 void *pages_list;
196 size_t num_entries;
197};
198
Jens Wiklander787c80c2021-06-15 22:23:53 +0200199int optee_notif_init(struct optee *optee, u_int max_key);
200void optee_notif_uninit(struct optee *optee);
201int optee_notif_wait(struct optee *optee, u_int key);
202int optee_notif_send(struct optee *optee, u_int key);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200203
204u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
205 struct tee_param *param);
206
207int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
208int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
209void optee_supp_init(struct optee_supp *supp);
210void optee_supp_uninit(struct optee_supp *supp);
Jens Wiklander1647a5a2016-12-23 13:13:39 +0100211void optee_supp_release(struct optee_supp *supp);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200212
213int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
214 struct tee_param *param);
215int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
216 struct tee_param *param);
217
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200218int optee_open_session(struct tee_context *ctx,
219 struct tee_ioctl_open_session_arg *arg,
220 struct tee_param *param);
Jens Wiklanderc0ab6db2021-03-25 15:08:46 +0100221int optee_close_session_helper(struct tee_context *ctx, u32 session);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200222int optee_close_session(struct tee_context *ctx, u32 session);
223int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
224 struct tee_param *param);
225int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
226
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300227#define PTA_CMD_GET_DEVICES 0x0
228#define PTA_CMD_GET_DEVICES_SUPP 0x1
229int optee_enumerate_devices(u32 func);
Sumit Garg7f565d02021-10-12 13:01:16 +0530230void optee_unregister_devices(void);
Sumit Gargc3fa24a2019-01-29 11:19:37 +0530231
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200232int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm,
233 struct tee_shm *shm, size_t size,
234 int (*shm_register)(struct tee_context *ctx,
235 struct tee_shm *shm,
236 struct page **pages,
237 size_t num_pages,
238 unsigned long start));
239
240
241void optee_remove_common(struct optee *optee);
242int optee_open(struct tee_context *ctx, bool cap_memref_null);
243void optee_release(struct tee_context *ctx);
244void optee_release_supp(struct tee_context *ctx);
245
246static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,
247 const struct optee_msg_param *mp)
248{
249 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
250 attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
251 p->u.value.a = mp->u.value.a;
252 p->u.value.b = mp->u.value.b;
253 p->u.value.c = mp->u.value.c;
254}
255
256static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
257 const struct tee_param *p)
258{
259 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
260 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
261 mp->u.value.a = p->u.value.a;
262 mp->u.value.b = p->u.value.b;
263 mp->u.value.c = p->u.value.c;
264}
265
266void optee_cq_wait_init(struct optee_call_queue *cq,
267 struct optee_call_waiter *w);
268void optee_cq_wait_for_completion(struct optee_call_queue *cq,
269 struct optee_call_waiter *w);
270void optee_cq_wait_final(struct optee_call_queue *cq,
271 struct optee_call_waiter *w);
272int optee_check_mem_type(unsigned long start, size_t num_pages);
273struct tee_shm *optee_get_msg_arg(struct tee_context *ctx, size_t num_params,
274 struct optee_msg_arg **msg_arg);
275
276struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
277void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
278void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
279 struct optee_msg_arg *arg);
280
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200281/*
282 * Small helpers
283 */
284
285static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
286{
287 return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
288}
289
290static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
291{
292 *reg0 = val >> 32;
293 *reg1 = val;
294}
295
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200296/* Registration of the ABIs */
297int optee_smc_abi_register(void);
298void optee_smc_abi_unregister(void);
Jens Wiklander4615e5a2021-07-21 17:45:21 +0200299int optee_ffa_abi_register(void);
300void optee_ffa_abi_unregister(void);
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200301
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200302#endif /*OPTEE_PRIVATE_H*/