Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (c) 2015-2021, Linaro Limited
0004  */
0005 
0006 #ifndef OPTEE_PRIVATE_H
0007 #define OPTEE_PRIVATE_H
0008 
0009 #include <linux/arm-smccc.h>
0010 #include <linux/rhashtable.h>
0011 #include <linux/semaphore.h>
0012 #include <linux/tee_drv.h>
0013 #include <linux/types.h>
0014 #include "optee_msg.h"
0015 
0016 #define DRIVER_NAME "optee"
0017 
0018 #define OPTEE_MAX_ARG_SIZE  1024
0019 
0020 /* Some Global Platform error codes used in this driver */
0021 #define TEEC_SUCCESS            0x00000000
0022 #define TEEC_ERROR_BAD_PARAMETERS   0xFFFF0006
0023 #define TEEC_ERROR_NOT_SUPPORTED    0xFFFF000A
0024 #define TEEC_ERROR_COMMUNICATION    0xFFFF000E
0025 #define TEEC_ERROR_OUT_OF_MEMORY    0xFFFF000C
0026 #define TEEC_ERROR_BUSY         0xFFFF000D
0027 #define TEEC_ERROR_SHORT_BUFFER     0xFFFF0010
0028 
0029 #define TEEC_ORIGIN_COMMS       0x00000002
0030 
0031 /*
0032  * This value should be larger than the number threads in secure world to
0033  * meet the need from secure world. The number of threads in secure world
0034  * are usually not even close to 255 so we should be safe for now.
0035  */
0036 #define OPTEE_DEFAULT_MAX_NOTIF_VALUE   255
0037 
0038 typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
0039                 unsigned long, unsigned long, unsigned long,
0040                 unsigned long, unsigned long,
0041                 struct arm_smccc_res *);
0042 
0043 struct optee_call_waiter {
0044     struct list_head list_node;
0045     struct completion c;
0046 };
0047 
0048 struct optee_call_queue {
0049     /* Serializes access to this struct */
0050     struct mutex mutex;
0051     struct list_head waiters;
0052 };
0053 
0054 struct optee_notif {
0055     u_int max_key;
0056     /* Serializes access to the elements below in this struct */
0057     spinlock_t lock;
0058     struct list_head db;
0059     u_long *bitmap;
0060 };
0061 
0062 #define OPTEE_SHM_ARG_ALLOC_PRIV    BIT(0)
0063 #define OPTEE_SHM_ARG_SHARED        BIT(1)
0064 struct optee_shm_arg_entry;
0065 struct optee_shm_arg_cache {
0066     u32 flags;
0067     /* Serializes access to this struct */
0068     struct mutex mutex;
0069     struct list_head shm_args;
0070 };
0071 
0072 /**
0073  * struct optee_supp - supplicant synchronization struct
0074  * @ctx         the context of current connected supplicant.
0075  *          if !NULL the supplicant device is available for use,
0076  *          else busy
0077  * @mutex:      held while accessing content of this struct
0078  * @req_id:     current request id if supplicant is doing synchronous
0079  *          communication, else -1
0080  * @reqs:       queued request not yet retrieved by supplicant
0081  * @idr:        IDR holding all requests currently being processed
0082  *          by supplicant
0083  * @reqs_c:     completion used by supplicant when waiting for a
0084  *          request to be queued.
0085  */
0086 struct optee_supp {
0087     /* Serializes access to this struct */
0088     struct mutex mutex;
0089     struct tee_context *ctx;
0090 
0091     int req_id;
0092     struct list_head reqs;
0093     struct idr idr;
0094     struct completion reqs_c;
0095 };
0096 
0097 struct optee_smc {
0098     optee_invoke_fn *invoke_fn;
0099     void *memremaped_shm;
0100     u32 sec_caps;
0101     unsigned int notif_irq;
0102 };
0103 
0104 /**
0105  * struct optee_ffa_data -  FFA communication struct
0106  * @ffa_dev     FFA device, contains the destination id, the id of
0107  *          OP-TEE in secure world
0108  * @ffa_ops     FFA operations
0109  * @mutex       Serializes access to @global_ids
0110  * @global_ids      FF-A shared memory global handle translation
0111  */
0112 struct optee_ffa {
0113     struct ffa_device *ffa_dev;
0114     const struct ffa_dev_ops *ffa_ops;
0115     /* Serializes access to @global_ids */
0116     struct mutex mutex;
0117     struct rhashtable global_ids;
0118 };
0119 
0120 struct optee;
0121 
0122 /**
0123  * struct optee_ops - OP-TEE driver internal operations
0124  * @do_call_with_arg:   enters OP-TEE in secure world
0125  * @to_msg_param:   converts from struct tee_param to OPTEE_MSG parameters
0126  * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param
0127  *
0128  * These OPs are only supposed to be used internally in the OP-TEE driver
0129  * as a way of abstracting the different methogs of entering OP-TEE in
0130  * secure world.
0131  */
0132 struct optee_ops {
0133     int (*do_call_with_arg)(struct tee_context *ctx,
0134                 struct tee_shm *shm_arg, u_int offs);
0135     int (*to_msg_param)(struct optee *optee,
0136                 struct optee_msg_param *msg_params,
0137                 size_t num_params, const struct tee_param *params);
0138     int (*from_msg_param)(struct optee *optee, struct tee_param *params,
0139                   size_t num_params,
0140                   const struct optee_msg_param *msg_params);
0141 };
0142 
0143 /**
0144  * struct optee - main service struct
0145  * @supp_teedev:    supplicant device
0146  * @teedev:     client device
0147  * @ops:        internal callbacks for different ways to reach secure
0148  *          world
0149  * @ctx:        driver internal TEE context
0150  * @smc:        specific to SMC ABI
0151  * @ffa:        specific to FF-A ABI
0152  * @call_queue:     queue of threads waiting to call @invoke_fn
0153  * @notif:      notification synchronization struct
0154  * @supp:       supplicant synchronization struct for RPC to supplicant
0155  * @pool:       shared memory pool
0156  * @rpc_param_count:    If > 0 number of RPC parameters to make room for
0157  * @scan_bus_done   flag if device registation was already done.
0158  * @scan_bus_wq     workqueue to scan optee bus and register optee drivers
0159  * @scan_bus_work   workq to scan optee bus and register optee drivers
0160  */
0161 struct optee {
0162     struct tee_device *supp_teedev;
0163     struct tee_device *teedev;
0164     const struct optee_ops *ops;
0165     struct tee_context *ctx;
0166     union {
0167         struct optee_smc smc;
0168         struct optee_ffa ffa;
0169     };
0170     struct optee_shm_arg_cache shm_arg_cache;
0171     struct optee_call_queue call_queue;
0172     struct optee_notif notif;
0173     struct optee_supp supp;
0174     struct tee_shm_pool *pool;
0175     unsigned int rpc_param_count;
0176     bool   scan_bus_done;
0177     struct workqueue_struct *scan_bus_wq;
0178     struct work_struct scan_bus_work;
0179 };
0180 
0181 struct optee_session {
0182     struct list_head list_node;
0183     u32 session_id;
0184 };
0185 
0186 struct optee_context_data {
0187     /* Serializes access to this struct */
0188     struct mutex mutex;
0189     struct list_head sess_list;
0190 };
0191 
0192 struct optee_rpc_param {
0193     u32 a0;
0194     u32 a1;
0195     u32 a2;
0196     u32 a3;
0197     u32 a4;
0198     u32 a5;
0199     u32 a6;
0200     u32 a7;
0201 };
0202 
0203 /* Holds context that is preserved during one STD call */
0204 struct optee_call_ctx {
0205     /* information about pages list used in last allocation */
0206     void *pages_list;
0207     size_t num_entries;
0208 };
0209 
0210 int optee_notif_init(struct optee *optee, u_int max_key);
0211 void optee_notif_uninit(struct optee *optee);
0212 int optee_notif_wait(struct optee *optee, u_int key);
0213 int optee_notif_send(struct optee *optee, u_int key);
0214 
0215 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
0216             struct tee_param *param);
0217 
0218 int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
0219 int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
0220 void optee_supp_init(struct optee_supp *supp);
0221 void optee_supp_uninit(struct optee_supp *supp);
0222 void optee_supp_release(struct optee_supp *supp);
0223 
0224 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
0225             struct tee_param *param);
0226 int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
0227             struct tee_param *param);
0228 
0229 int optee_open_session(struct tee_context *ctx,
0230                struct tee_ioctl_open_session_arg *arg,
0231                struct tee_param *param);
0232 int optee_close_session_helper(struct tee_context *ctx, u32 session);
0233 int optee_close_session(struct tee_context *ctx, u32 session);
0234 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
0235               struct tee_param *param);
0236 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
0237 
0238 #define PTA_CMD_GET_DEVICES     0x0
0239 #define PTA_CMD_GET_DEVICES_SUPP    0x1
0240 int optee_enumerate_devices(u32 func);
0241 void optee_unregister_devices(void);
0242 
0243 int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
0244                    size_t size, size_t align,
0245                    int (*shm_register)(struct tee_context *ctx,
0246                            struct tee_shm *shm,
0247                            struct page **pages,
0248                            size_t num_pages,
0249                            unsigned long start));
0250 void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
0251                    int (*shm_unregister)(struct tee_context *ctx,
0252                              struct tee_shm *shm));
0253 
0254 
0255 void optee_remove_common(struct optee *optee);
0256 int optee_open(struct tee_context *ctx, bool cap_memref_null);
0257 void optee_release(struct tee_context *ctx);
0258 void optee_release_supp(struct tee_context *ctx);
0259 
0260 static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,
0261                           const struct optee_msg_param *mp)
0262 {
0263     p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
0264           attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
0265     p->u.value.a = mp->u.value.a;
0266     p->u.value.b = mp->u.value.b;
0267     p->u.value.c = mp->u.value.c;
0268 }
0269 
0270 static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
0271                         const struct tee_param *p)
0272 {
0273     mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
0274            TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
0275     mp->u.value.a = p->u.value.a;
0276     mp->u.value.b = p->u.value.b;
0277     mp->u.value.c = p->u.value.c;
0278 }
0279 
0280 void optee_cq_wait_init(struct optee_call_queue *cq,
0281             struct optee_call_waiter *w);
0282 void optee_cq_wait_for_completion(struct optee_call_queue *cq,
0283                   struct optee_call_waiter *w);
0284 void optee_cq_wait_final(struct optee_call_queue *cq,
0285              struct optee_call_waiter *w);
0286 int optee_check_mem_type(unsigned long start, size_t num_pages);
0287 
0288 void optee_shm_arg_cache_init(struct optee *optee, u32 flags);
0289 void optee_shm_arg_cache_uninit(struct optee *optee);
0290 struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,
0291                     size_t num_params,
0292                     struct optee_shm_arg_entry **entry,
0293                     struct tee_shm **shm_ret,
0294                     u_int *offs);
0295 void optee_free_msg_arg(struct tee_context *ctx,
0296             struct optee_shm_arg_entry *entry, u_int offs);
0297 size_t optee_msg_arg_size(size_t rpc_param_count);
0298 
0299 
0300 struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
0301 void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
0302 void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
0303            struct optee_msg_arg *arg);
0304 
0305 /*
0306  * Small helpers
0307  */
0308 
0309 static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
0310 {
0311     return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
0312 }
0313 
0314 static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
0315 {
0316     *reg0 = val >> 32;
0317     *reg1 = val;
0318 }
0319 
0320 /* Registration of the ABIs */
0321 int optee_smc_abi_register(void);
0322 void optee_smc_abi_unregister(void);
0323 int optee_ffa_abi_register(void);
0324 void optee_ffa_abi_unregister(void);
0325 
0326 #endif /*OPTEE_PRIVATE_H*/