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