cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

optee_private.h (9958B)


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