Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v6.18-rc2 403 lines 13 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * Copyright (c) 2015-2021, 2023 Linaro Limited 4 */ 5 6#ifndef OPTEE_PRIVATE_H 7#define OPTEE_PRIVATE_H 8 9#include <linux/arm-smccc.h> 10#include <linux/notifier.h> 11#include <linux/rhashtable.h> 12#include <linux/rpmb.h> 13#include <linux/semaphore.h> 14#include <linux/tee_core.h> 15#include <linux/types.h> 16#include "optee_msg.h" 17 18#define DRIVER_NAME "optee" 19 20#define OPTEE_MAX_ARG_SIZE 1024 21 22/* Some Global Platform error codes used in this driver */ 23#define TEEC_SUCCESS 0x00000000 24#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 25#define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008 26#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A 27#define TEEC_ERROR_COMMUNICATION 0xFFFF000E 28#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C 29#define TEEC_ERROR_BUSY 0xFFFF000D 30#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010 31 32/* API Return Codes are from the GP TEE Internal Core API Specification */ 33#define TEE_ERROR_TIMEOUT 0xFFFF3001 34#define TEE_ERROR_STORAGE_NOT_AVAILABLE 0xF0100003 35 36#define TEEC_ORIGIN_COMMS 0x00000002 37 38/* 39 * This value should be larger than the number threads in secure world to 40 * meet the need from secure world. The number of threads in secure world 41 * are usually not even close to 255 so we should be safe for now. 42 */ 43#define OPTEE_DEFAULT_MAX_NOTIF_VALUE 255 44 45typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long, 46 unsigned long, unsigned long, unsigned long, 47 unsigned long, unsigned long, 48 struct arm_smccc_res *); 49 50/* 51 * struct optee_call_waiter - TEE entry may need to wait for a free TEE thread 52 * @list_node Reference in waiters list 53 * @c Waiting completion reference 54 * @sys_thread True if waiter belongs to a system thread 55 */ 56struct optee_call_waiter { 57 struct list_head list_node; 58 struct completion c; 59 bool sys_thread; 60}; 61 62/* 63 * struct optee_call_queue - OP-TEE call queue management 64 * @mutex Serializes access to this struct 65 * @waiters List of threads waiting to enter OP-TEE 66 * @total_thread_count Overall number of thread context in OP-TEE or 0 67 * @free_thread_count Number of threads context free in OP-TEE 68 * @sys_thread_req_count Number of registered system thread sessions 69 */ 70struct optee_call_queue { 71 /* Serializes access to this struct */ 72 struct mutex mutex; 73 struct list_head waiters; 74 int total_thread_count; 75 int free_thread_count; 76 int sys_thread_req_count; 77}; 78 79struct optee_notif { 80 u_int max_key; 81 /* Serializes access to the elements below in this struct */ 82 spinlock_t lock; 83 struct list_head db; 84 u_long *bitmap; 85}; 86 87#define OPTEE_SHM_ARG_ALLOC_PRIV BIT(0) 88#define OPTEE_SHM_ARG_SHARED BIT(1) 89struct optee_shm_arg_entry; 90struct optee_shm_arg_cache { 91 u32 flags; 92 /* Serializes access to this struct */ 93 struct mutex mutex; 94 struct list_head shm_args; 95}; 96 97/** 98 * struct optee_supp - supplicant synchronization struct 99 * @ctx the context of current connected supplicant. 100 * if !NULL the supplicant device is available for use, 101 * else busy 102 * @mutex: held while accessing content of this struct 103 * @req_id: current request id if supplicant is doing synchronous 104 * communication, else -1 105 * @reqs: queued request not yet retrieved by supplicant 106 * @idr: IDR holding all requests currently being processed 107 * by supplicant 108 * @reqs_c: completion used by supplicant when waiting for a 109 * request to be queued. 110 */ 111struct optee_supp { 112 /* Serializes access to this struct */ 113 struct mutex mutex; 114 struct tee_context *ctx; 115 116 int req_id; 117 struct list_head reqs; 118 struct idr idr; 119 struct completion reqs_c; 120}; 121 122/* 123 * struct optee_pcpu - per cpu notif private struct passed to work functions 124 * @optee optee device reference 125 */ 126struct optee_pcpu { 127 struct optee *optee; 128}; 129 130/* 131 * struct optee_smc - optee smc communication struct 132 * @invoke_fn handler function to invoke secure monitor 133 * @memremaped_shm virtual address of memory in shared memory pool 134 * @sec_caps: secure world capabilities defined by 135 * OPTEE_SMC_SEC_CAP_* in optee_smc.h 136 * @notif_irq interrupt used as async notification by OP-TEE or 0 137 * @optee_pcpu per_cpu optee instance for per cpu work or NULL 138 * @notif_pcpu_wq workqueue for per cpu asynchronous notification or NULL 139 * @notif_pcpu_work work for per cpu asynchronous notification 140 * @notif_cpuhp_state CPU hotplug state assigned for pcpu interrupt management 141 */ 142struct optee_smc { 143 optee_invoke_fn *invoke_fn; 144 void *memremaped_shm; 145 u32 sec_caps; 146 unsigned int notif_irq; 147 struct optee_pcpu __percpu *optee_pcpu; 148 struct workqueue_struct *notif_pcpu_wq; 149 struct work_struct notif_pcpu_work; 150 unsigned int notif_cpuhp_state; 151}; 152 153/** 154 * struct optee_ffa_data - FFA communication struct 155 * @ffa_dev FFA device, contains the destination id, the id of 156 * OP-TEE in secure world 157 * @bottom_half_value Notification ID used for bottom half signalling or 158 * U32_MAX if unused 159 * @mutex Serializes access to @global_ids 160 * @global_ids FF-A shared memory global handle translation 161 */ 162struct optee_ffa { 163 struct ffa_device *ffa_dev; 164 u32 bottom_half_value; 165 /* Serializes access to @global_ids */ 166 struct mutex mutex; 167 struct rhashtable global_ids; 168 struct workqueue_struct *notif_wq; 169 struct work_struct notif_work; 170}; 171 172struct optee; 173 174/** 175 * struct optee_ops - OP-TEE driver internal operations 176 * @do_call_with_arg: enters OP-TEE in secure world 177 * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters 178 * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param 179 * @lend_protmem: lends physically contiguous memory as restricted 180 * memory, inaccessible by the kernel 181 * @reclaim_protmem: reclaims restricted memory previously lent with 182 * @lend_protmem() and makes it accessible by the 183 * kernel again 184 * 185 * These OPs are only supposed to be used internally in the OP-TEE driver 186 * as a way of abstracting the different methods of entering OP-TEE in 187 * secure world. 188 */ 189struct optee_ops { 190 int (*do_call_with_arg)(struct tee_context *ctx, 191 struct tee_shm *shm_arg, u_int offs, 192 bool system_thread); 193 int (*to_msg_param)(struct optee *optee, 194 struct optee_msg_param *msg_params, 195 size_t num_params, const struct tee_param *params); 196 int (*from_msg_param)(struct optee *optee, struct tee_param *params, 197 size_t num_params, 198 const struct optee_msg_param *msg_params); 199 int (*lend_protmem)(struct optee *optee, struct tee_shm *protmem, 200 u32 *mem_attr, unsigned int ma_count, 201 u32 use_case); 202 int (*reclaim_protmem)(struct optee *optee, struct tee_shm *protmem); 203}; 204 205/** 206 * struct optee - main service struct 207 * @supp_teedev: supplicant device 208 * @teedev: client device 209 * @ops: internal callbacks for different ways to reach secure 210 * world 211 * @ctx: driver internal TEE context 212 * @smc: specific to SMC ABI 213 * @ffa: specific to FF-A ABI 214 * @call_queue: queue of threads waiting to call @invoke_fn 215 * @notif: notification synchronization struct 216 * @supp: supplicant synchronization struct for RPC to supplicant 217 * @pool: shared memory pool 218 * @mutex: mutex protecting @rpmb_dev 219 * @rpmb_dev: current RPMB device or NULL 220 * @rpmb_scan_bus_done flag if device registation of RPMB dependent devices 221 * was already done 222 * @rpmb_scan_bus_work workq to for an RPMB device and to scan optee bus 223 * and register RPMB dependent optee drivers 224 * @rpc_param_count: If > 0 number of RPC parameters to make room for 225 * @scan_bus_done flag if device registation was already done. 226 * @scan_bus_work workq to scan optee bus and register optee drivers 227 */ 228struct optee { 229 struct tee_device *supp_teedev; 230 struct tee_device *teedev; 231 const struct optee_ops *ops; 232 struct tee_context *ctx; 233 union { 234 struct optee_smc smc; 235 struct optee_ffa ffa; 236 }; 237 struct optee_shm_arg_cache shm_arg_cache; 238 struct optee_call_queue call_queue; 239 struct optee_notif notif; 240 struct optee_supp supp; 241 struct tee_shm_pool *pool; 242 /* Protects rpmb_dev pointer */ 243 struct mutex rpmb_dev_mutex; 244 struct rpmb_dev *rpmb_dev; 245 struct notifier_block rpmb_intf; 246 unsigned int rpc_param_count; 247 bool scan_bus_done; 248 bool rpmb_scan_bus_done; 249 bool in_kernel_rpmb_routing; 250 struct work_struct scan_bus_work; 251 struct work_struct rpmb_scan_bus_work; 252}; 253 254struct optee_session { 255 struct list_head list_node; 256 u32 session_id; 257 bool use_sys_thread; 258}; 259 260struct optee_context_data { 261 /* Serializes access to this struct */ 262 struct mutex mutex; 263 struct list_head sess_list; 264}; 265 266struct optee_rpc_param { 267 u32 a0; 268 u32 a1; 269 u32 a2; 270 u32 a3; 271 u32 a4; 272 u32 a5; 273 u32 a6; 274 u32 a7; 275}; 276 277/* Holds context that is preserved during one STD call */ 278struct optee_call_ctx { 279 /* information about pages list used in last allocation */ 280 void *pages_list; 281 size_t num_entries; 282}; 283 284extern struct blocking_notifier_head optee_rpmb_intf_added; 285 286int optee_set_dma_mask(struct optee *optee, u_int pa_width); 287 288int optee_notif_init(struct optee *optee, u_int max_key); 289void optee_notif_uninit(struct optee *optee); 290int optee_notif_wait(struct optee *optee, u_int key, u32 timeout); 291int optee_notif_send(struct optee *optee, u_int key); 292 293u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params, 294 struct tee_param *param); 295 296void optee_supp_init(struct optee_supp *supp); 297void optee_supp_uninit(struct optee_supp *supp); 298void optee_supp_release(struct optee_supp *supp); 299struct tee_protmem_pool *optee_protmem_alloc_dyn_pool(struct optee *optee, 300 enum tee_dma_heap_id id); 301 302int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params, 303 struct tee_param *param); 304int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, 305 struct tee_param *param); 306 307int optee_open_session(struct tee_context *ctx, 308 struct tee_ioctl_open_session_arg *arg, 309 struct tee_param *param); 310int optee_system_session(struct tee_context *ctx, u32 session); 311int optee_close_session_helper(struct tee_context *ctx, u32 session, 312 bool system_thread); 313int optee_close_session(struct tee_context *ctx, u32 session); 314int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, 315 struct tee_param *param); 316int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); 317 318#define PTA_CMD_GET_DEVICES 0x0 319#define PTA_CMD_GET_DEVICES_SUPP 0x1 320#define PTA_CMD_GET_DEVICES_RPMB 0x2 321int optee_enumerate_devices(u32 func); 322void optee_unregister_devices(void); 323void optee_bus_scan_rpmb(struct work_struct *work); 324int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action, 325 void *data); 326 327void optee_set_dev_group(struct optee *optee); 328void optee_remove_common(struct optee *optee); 329int optee_open(struct tee_context *ctx, bool cap_memref_null); 330void optee_release(struct tee_context *ctx); 331void optee_release_supp(struct tee_context *ctx); 332 333static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr, 334 const struct optee_msg_param *mp) 335{ 336 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + 337 attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; 338 p->u.value.a = mp->u.value.a; 339 p->u.value.b = mp->u.value.b; 340 p->u.value.c = mp->u.value.c; 341} 342 343static inline void optee_to_msg_param_value(struct optee_msg_param *mp, 344 const struct tee_param *p) 345{ 346 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - 347 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; 348 mp->u.value.a = p->u.value.a; 349 mp->u.value.b = p->u.value.b; 350 mp->u.value.c = p->u.value.c; 351} 352 353void optee_cq_init(struct optee_call_queue *cq, int thread_count); 354void optee_cq_wait_init(struct optee_call_queue *cq, 355 struct optee_call_waiter *w, bool sys_thread); 356void optee_cq_wait_for_completion(struct optee_call_queue *cq, 357 struct optee_call_waiter *w); 358void optee_cq_wait_final(struct optee_call_queue *cq, 359 struct optee_call_waiter *w); 360int optee_check_mem_type(unsigned long start, size_t num_pages); 361 362void optee_shm_arg_cache_init(struct optee *optee, u32 flags); 363void optee_shm_arg_cache_uninit(struct optee *optee); 364struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx, 365 size_t num_params, 366 struct optee_shm_arg_entry **entry, 367 struct tee_shm **shm_ret, 368 u_int *offs); 369void optee_free_msg_arg(struct tee_context *ctx, 370 struct optee_shm_arg_entry *entry, u_int offs); 371size_t optee_msg_arg_size(size_t rpc_param_count); 372 373 374struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz); 375void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm); 376void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee, 377 struct optee_msg_arg *arg); 378 379int optee_do_bottom_half(struct tee_context *ctx); 380int optee_stop_async_notif(struct tee_context *ctx); 381 382/* 383 * Small helpers 384 */ 385 386static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1) 387{ 388 return (void *)(unsigned long)(((u64)reg0 << 32) | reg1); 389} 390 391static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val) 392{ 393 *reg0 = val >> 32; 394 *reg1 = val; 395} 396 397/* Registration of the ABIs */ 398int optee_smc_abi_register(void); 399void optee_smc_abi_unregister(void); 400int optee_ffa_abi_register(void); 401void optee_ffa_abi_unregister(void); 402 403#endif /*OPTEE_PRIVATE_H*/