389 lines · c
1/* SPDX-License-Identifier: GPL-2.0-only */2/*3 * Copyright (c) 2015-2021, 2023 Linaro Limited4 */5 6#ifndef OPTEE_PRIVATE_H7#define OPTEE_PRIVATE_H8 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 102421 22/* Some Global Platform error codes used in this driver */23#define TEEC_SUCCESS 0x0000000024#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF000625#define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF000826#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A27#define TEEC_ERROR_COMMUNICATION 0xFFFF000E28#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C29#define TEEC_ERROR_BUSY 0xFFFF000D30#define TEEC_ERROR_SHORT_BUFFER 0xFFFF001031 32/* API Return Codes are from the GP TEE Internal Core API Specification */33#define TEE_ERROR_TIMEOUT 0xFFFF300134#define TEE_ERROR_STORAGE_NOT_AVAILABLE 0xF010000335 36#define TEEC_ORIGIN_COMMS 0x0000000237 38/*39 * This value should be larger than the number threads in secure world to40 * meet the need from secure world. The number of threads in secure world41 * are usually not even close to 255 so we should be safe for now.42 */43#define OPTEE_DEFAULT_MAX_NOTIF_VALUE 25544 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 thread52 * @list_node Reference in waiters list53 * @c Waiting completion reference54 * @sys_thread True if waiter belongs to a system thread55 */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 management64 * @mutex Serializes access to this struct65 * @waiters List of threads waiting to enter OP-TEE66 * @total_thread_count Overall number of thread context in OP-TEE or 067 * @free_thread_count Number of threads context free in OP-TEE68 * @sys_thread_req_count Number of registered system thread sessions69 */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 struct99 * @ctx the context of current connected supplicant.100 * if !NULL the supplicant device is available for use,101 * else busy102 * @mutex: held while accessing content of this struct103 * @req_id: current request id if supplicant is doing synchronous104 * communication, else -1105 * @reqs: queued request not yet retrieved by supplicant106 * @idr: IDR holding all requests currently being processed107 * by supplicant108 * @reqs_c: completion used by supplicant when waiting for a109 * 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 functions124 * @optee optee device reference125 */126struct optee_pcpu {127 struct optee *optee;128};129 130/*131 * struct optee_smc - optee smc communication struct132 * @invoke_fn handler function to invoke secure monitor133 * @memremaped_shm virtual address of memory in shared memory pool134 * @sec_caps: secure world capabilities defined by135 * OPTEE_SMC_SEC_CAP_* in optee_smc.h136 * @notif_irq interrupt used as async notification by OP-TEE or 0137 * @optee_pcpu per_cpu optee instance for per cpu work or NULL138 * @notif_pcpu_wq workqueue for per cpu asynchronous notification or NULL139 * @notif_pcpu_work work for per cpu asynchronous notification140 * @notif_cpuhp_state CPU hotplug state assigned for pcpu interrupt management141 */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 struct155 * @ffa_dev FFA device, contains the destination id, the id of156 * OP-TEE in secure world157 * @bottom_half_value Notification ID used for bottom half signalling or158 * U32_MAX if unused159 * @mutex Serializes access to @global_ids160 * @global_ids FF-A shared memory global handle translation161 */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};169 170struct optee;171 172/**173 * struct optee_ops - OP-TEE driver internal operations174 * @do_call_with_arg: enters OP-TEE in secure world175 * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters176 * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param177 *178 * These OPs are only supposed to be used internally in the OP-TEE driver179 * as a way of abstracting the different methogs of entering OP-TEE in180 * secure world.181 */182struct optee_ops {183 int (*do_call_with_arg)(struct tee_context *ctx,184 struct tee_shm *shm_arg, u_int offs,185 bool system_thread);186 int (*to_msg_param)(struct optee *optee,187 struct optee_msg_param *msg_params,188 size_t num_params, const struct tee_param *params);189 int (*from_msg_param)(struct optee *optee, struct tee_param *params,190 size_t num_params,191 const struct optee_msg_param *msg_params);192};193 194/**195 * struct optee - main service struct196 * @supp_teedev: supplicant device197 * @teedev: client device198 * @ops: internal callbacks for different ways to reach secure199 * world200 * @ctx: driver internal TEE context201 * @smc: specific to SMC ABI202 * @ffa: specific to FF-A ABI203 * @call_queue: queue of threads waiting to call @invoke_fn204 * @notif: notification synchronization struct205 * @supp: supplicant synchronization struct for RPC to supplicant206 * @pool: shared memory pool207 * @mutex: mutex protecting @rpmb_dev208 * @rpmb_dev: current RPMB device or NULL209 * @rpmb_scan_bus_done flag if device registation of RPMB dependent devices210 * was already done211 * @rpmb_scan_bus_work workq to for an RPMB device and to scan optee bus212 * and register RPMB dependent optee drivers213 * @rpc_param_count: If > 0 number of RPC parameters to make room for214 * @scan_bus_done flag if device registation was already done.215 * @scan_bus_work workq to scan optee bus and register optee drivers216 */217struct optee {218 struct tee_device *supp_teedev;219 struct tee_device *teedev;220 const struct optee_ops *ops;221 struct tee_context *ctx;222 union {223 struct optee_smc smc;224 struct optee_ffa ffa;225 };226 struct optee_shm_arg_cache shm_arg_cache;227 struct optee_call_queue call_queue;228 struct optee_notif notif;229 struct optee_supp supp;230 struct tee_shm_pool *pool;231 /* Protects rpmb_dev pointer */232 struct mutex rpmb_dev_mutex;233 struct rpmb_dev *rpmb_dev;234 struct notifier_block rpmb_intf;235 unsigned int rpc_param_count;236 bool scan_bus_done;237 bool rpmb_scan_bus_done;238 bool in_kernel_rpmb_routing;239 struct work_struct scan_bus_work;240 struct work_struct rpmb_scan_bus_work;241};242 243struct optee_session {244 struct list_head list_node;245 u32 session_id;246 bool use_sys_thread;247};248 249struct optee_context_data {250 /* Serializes access to this struct */251 struct mutex mutex;252 struct list_head sess_list;253};254 255struct optee_rpc_param {256 u32 a0;257 u32 a1;258 u32 a2;259 u32 a3;260 u32 a4;261 u32 a5;262 u32 a6;263 u32 a7;264};265 266/* Holds context that is preserved during one STD call */267struct optee_call_ctx {268 /* information about pages list used in last allocation */269 void *pages_list;270 size_t num_entries;271};272 273extern struct blocking_notifier_head optee_rpmb_intf_added;274 275int optee_notif_init(struct optee *optee, u_int max_key);276void optee_notif_uninit(struct optee *optee);277int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);278int optee_notif_send(struct optee *optee, u_int key);279 280u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,281 struct tee_param *param);282 283void optee_supp_init(struct optee_supp *supp);284void optee_supp_uninit(struct optee_supp *supp);285void optee_supp_release(struct optee_supp *supp);286 287int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,288 struct tee_param *param);289int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,290 struct tee_param *param);291 292int optee_open_session(struct tee_context *ctx,293 struct tee_ioctl_open_session_arg *arg,294 struct tee_param *param);295int optee_system_session(struct tee_context *ctx, u32 session);296int optee_close_session_helper(struct tee_context *ctx, u32 session,297 bool system_thread);298int optee_close_session(struct tee_context *ctx, u32 session);299int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,300 struct tee_param *param);301int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);302 303#define PTA_CMD_GET_DEVICES 0x0304#define PTA_CMD_GET_DEVICES_SUPP 0x1305#define PTA_CMD_GET_DEVICES_RPMB 0x2306int optee_enumerate_devices(u32 func);307void optee_unregister_devices(void);308void optee_bus_scan_rpmb(struct work_struct *work);309int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action,310 void *data);311 312void optee_set_dev_group(struct optee *optee);313void optee_remove_common(struct optee *optee);314int optee_open(struct tee_context *ctx, bool cap_memref_null);315void optee_release(struct tee_context *ctx);316void optee_release_supp(struct tee_context *ctx);317 318static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,319 const struct optee_msg_param *mp)320{321 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +322 attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;323 p->u.value.a = mp->u.value.a;324 p->u.value.b = mp->u.value.b;325 p->u.value.c = mp->u.value.c;326}327 328static inline void optee_to_msg_param_value(struct optee_msg_param *mp,329 const struct tee_param *p)330{331 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -332 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;333 mp->u.value.a = p->u.value.a;334 mp->u.value.b = p->u.value.b;335 mp->u.value.c = p->u.value.c;336}337 338void optee_cq_init(struct optee_call_queue *cq, int thread_count);339void optee_cq_wait_init(struct optee_call_queue *cq,340 struct optee_call_waiter *w, bool sys_thread);341void optee_cq_wait_for_completion(struct optee_call_queue *cq,342 struct optee_call_waiter *w);343void optee_cq_wait_final(struct optee_call_queue *cq,344 struct optee_call_waiter *w);345int optee_check_mem_type(unsigned long start, size_t num_pages);346 347void optee_shm_arg_cache_init(struct optee *optee, u32 flags);348void optee_shm_arg_cache_uninit(struct optee *optee);349struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,350 size_t num_params,351 struct optee_shm_arg_entry **entry,352 struct tee_shm **shm_ret,353 u_int *offs);354void optee_free_msg_arg(struct tee_context *ctx,355 struct optee_shm_arg_entry *entry, u_int offs);356size_t optee_msg_arg_size(size_t rpc_param_count);357 358 359struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);360void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);361void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,362 struct optee_msg_arg *arg);363 364int optee_do_bottom_half(struct tee_context *ctx);365int optee_stop_async_notif(struct tee_context *ctx);366 367/*368 * Small helpers369 */370 371static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)372{373 return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);374}375 376static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)377{378 *reg0 = val >> 32;379 *reg1 = val;380}381 382/* Registration of the ABIs */383int optee_smc_abi_register(void);384void optee_smc_abi_unregister(void);385int optee_ffa_abi_register(void);386void optee_ffa_abi_unregister(void);387 388#endif /*OPTEE_PRIVATE_H*/389