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