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