^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2015, Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef OPTEE_PRIVATE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define OPTEE_PRIVATE_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/arm-smccc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/semaphore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/tee_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "optee_msg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define OPTEE_MAX_ARG_SIZE 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* Some Global Platform error codes used in this driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define TEEC_SUCCESS 0x00000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define TEEC_ERROR_COMMUNICATION 0xFFFF000E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define TEEC_ORIGIN_COMMS 0x00000002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long, unsigned long, unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long, unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct arm_smccc_res *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct optee_call_queue {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Serializes access to this struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct list_head waiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct optee_wait_queue {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Serializes access to this struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct mutex mu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct list_head db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * struct optee_supp - supplicant synchronization struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @ctx the context of current connected supplicant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * if !NULL the supplicant device is available for use,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * else busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @mutex: held while accessing content of this struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @req_id: current request id if supplicant is doing synchronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * communication, else -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @reqs: queued request not yet retrieved by supplicant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @idr: IDR holding all requests currently being processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * by supplicant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @reqs_c: completion used by supplicant when waiting for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * request to be queued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct optee_supp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Serializes access to this struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct tee_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int req_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct list_head reqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct idr idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct completion reqs_c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * struct optee - main service struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * @supp_teedev: supplicant device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * @teedev: client device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @ctx: driver internal TEE context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @invoke_fn: function to issue smc or hvc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @call_queue: queue of threads waiting to call @invoke_fn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @wait_queue: queue of threads from secure world waiting for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * secure world sync object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @supp: supplicant synchronization struct for RPC to supplicant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @pool: shared memory pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @memremaped_shm virtual address of memory in shared memory pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @sec_caps: secure world capabilities defined by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * OPTEE_SMC_SEC_CAP_* in optee_smc.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @scan_bus_done flag if device registation was already done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @scan_bus_wq workqueue to scan optee bus and register optee drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @scan_bus_work workq to scan optee bus and register optee drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct optee {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct tee_device *supp_teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct tee_device *teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) optee_invoke_fn *invoke_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct tee_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct optee_call_queue call_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct optee_wait_queue wait_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct optee_supp supp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct tee_shm_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) void *memremaped_shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) u32 sec_caps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) bool scan_bus_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct workqueue_struct *scan_bus_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct work_struct scan_bus_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct optee_session {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct list_head list_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u32 session_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct optee_context_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Serializes access to this struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct list_head sess_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct optee_rpc_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) u32 a0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) u32 a1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) u32 a2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) u32 a3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) u32 a4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) u32 a5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) u32 a6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u32 a7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* Holds context that is preserved during one STD call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct optee_call_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* information about pages list used in last allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void *pages_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) size_t num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct optee_call_ctx *call_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void optee_wait_queue_init(struct optee_wait_queue *wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void optee_wait_queue_exit(struct optee_wait_queue *wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct tee_param *param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void optee_supp_init(struct optee_supp *supp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) void optee_supp_uninit(struct optee_supp *supp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void optee_supp_release(struct optee_supp *supp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct tee_param *param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct tee_param *param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int optee_open_session(struct tee_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct tee_ioctl_open_session_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct tee_param *param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int optee_close_session(struct tee_context *ctx, u32 session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct tee_param *param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) void optee_enable_shm_cache(struct optee *optee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) void optee_disable_shm_cache(struct optee *optee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void optee_disable_unmapped_shm_cache(struct optee *optee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct page **pages, size_t num_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) unsigned long start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct page **pages, size_t num_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned long start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int optee_from_msg_param(struct tee_param *params, size_t num_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) const struct optee_msg_param *msg_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) const struct tee_param *params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) u64 *optee_allocate_pages_list(size_t num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void optee_free_pages_list(void *array, size_t num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) size_t page_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #define PTA_CMD_GET_DEVICES 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #define PTA_CMD_GET_DEVICES_SUPP 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int optee_enumerate_devices(u32 func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) void optee_unregister_devices(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Small helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *reg0 = val >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *reg1 = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #endif /*OPTEE_PRIVATE_H*/