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 |
| 31 | |
| 32 | #define TEEC_ORIGIN_COMMS 0x00000002 |
| 33 | |
| 34 | typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long, |
| 35 | unsigned long, unsigned long, unsigned long, |
| 36 | unsigned long, unsigned long, |
| 37 | struct arm_smccc_res *); |
| 38 | |
| 39 | struct optee_call_queue { |
| 40 | /* Serializes access to this struct */ |
| 41 | struct mutex mutex; |
| 42 | struct list_head waiters; |
| 43 | }; |
| 44 | |
| 45 | struct optee_wait_queue { |
| 46 | /* Serializes access to this struct */ |
| 47 | struct mutex mu; |
| 48 | struct list_head db; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * struct optee_supp - supplicant synchronization struct |
| 53 | * @ctx the context of current connected supplicant. |
| 54 | * if !NULL the supplicant device is available for use, |
| 55 | * else busy |
Jens Wiklander | 1647a5a | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 56 | * @mutex: held while accessing content of this struct |
| 57 | * @req_id: current request id if supplicant is doing synchronous |
| 58 | * communication, else -1 |
| 59 | * @reqs: queued request not yet retrieved by supplicant |
| 60 | * @idr: IDR holding all requests currently being processed |
| 61 | * by supplicant |
| 62 | * @reqs_c: completion used by supplicant when waiting for a |
| 63 | * request to be queued. |
Jens Wiklander | 4fb0a5e | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 64 | */ |
| 65 | struct optee_supp { |
Jens Wiklander | 1647a5a | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 66 | /* Serializes access to this struct */ |
| 67 | struct mutex mutex; |
Jens Wiklander | 4fb0a5e | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 68 | struct tee_context *ctx; |
Jens Wiklander | 4fb0a5e | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 69 | |
Jens Wiklander | 1647a5a | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 70 | int req_id; |
| 71 | struct list_head reqs; |
| 72 | struct idr idr; |
| 73 | struct completion reqs_c; |
Jens Wiklander | 4fb0a5e | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * struct optee - main service struct |
| 78 | * @supp_teedev: supplicant device |
| 79 | * @teedev: client device |
| 80 | * @invoke_fn: function to issue smc or hvc |
| 81 | * @call_queue: queue of threads waiting to call @invoke_fn |
| 82 | * @wait_queue: queue of threads from secure world waiting for a |
| 83 | * secure world sync object |
| 84 | * @supp: supplicant synchronization struct for RPC to supplicant |
| 85 | * @pool: shared memory pool |
| 86 | * @memremaped_shm virtual address of memory in shared memory pool |
| 87 | */ |
| 88 | struct optee { |
| 89 | struct tee_device *supp_teedev; |
| 90 | struct tee_device *teedev; |
| 91 | optee_invoke_fn *invoke_fn; |
| 92 | struct optee_call_queue call_queue; |
| 93 | struct optee_wait_queue wait_queue; |
| 94 | struct optee_supp supp; |
| 95 | struct tee_shm_pool *pool; |
| 96 | void *memremaped_shm; |
| 97 | }; |
| 98 | |
| 99 | struct optee_session { |
| 100 | struct list_head list_node; |
| 101 | u32 session_id; |
| 102 | }; |
| 103 | |
| 104 | struct optee_context_data { |
| 105 | /* Serializes access to this struct */ |
| 106 | struct mutex mutex; |
| 107 | struct list_head sess_list; |
| 108 | }; |
| 109 | |
| 110 | struct optee_rpc_param { |
| 111 | u32 a0; |
| 112 | u32 a1; |
| 113 | u32 a2; |
| 114 | u32 a3; |
| 115 | u32 a4; |
| 116 | u32 a5; |
| 117 | u32 a6; |
| 118 | u32 a7; |
| 119 | }; |
| 120 | |
| 121 | void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param); |
| 122 | |
| 123 | void optee_wait_queue_init(struct optee_wait_queue *wq); |
| 124 | void optee_wait_queue_exit(struct optee_wait_queue *wq); |
| 125 | |
| 126 | u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params, |
| 127 | struct tee_param *param); |
| 128 | |
| 129 | int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len); |
| 130 | int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len); |
| 131 | void optee_supp_init(struct optee_supp *supp); |
| 132 | void optee_supp_uninit(struct optee_supp *supp); |
Jens Wiklander | 1647a5a | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 133 | void optee_supp_release(struct optee_supp *supp); |
Jens Wiklander | 4fb0a5e | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 134 | |
| 135 | int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params, |
| 136 | struct tee_param *param); |
| 137 | int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, |
| 138 | struct tee_param *param); |
| 139 | |
| 140 | u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg); |
| 141 | int optee_open_session(struct tee_context *ctx, |
| 142 | struct tee_ioctl_open_session_arg *arg, |
| 143 | struct tee_param *param); |
| 144 | int optee_close_session(struct tee_context *ctx, u32 session); |
| 145 | int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, |
| 146 | struct tee_param *param); |
| 147 | int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); |
| 148 | |
| 149 | void optee_enable_shm_cache(struct optee *optee); |
| 150 | void optee_disable_shm_cache(struct optee *optee); |
| 151 | |
| 152 | int optee_from_msg_param(struct tee_param *params, size_t num_params, |
| 153 | const struct optee_msg_param *msg_params); |
| 154 | int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params, |
| 155 | const struct tee_param *params); |
| 156 | |
| 157 | /* |
| 158 | * Small helpers |
| 159 | */ |
| 160 | |
| 161 | static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1) |
| 162 | { |
| 163 | return (void *)(unsigned long)(((u64)reg0 << 32) | reg1); |
| 164 | } |
| 165 | |
| 166 | static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val) |
| 167 | { |
| 168 | *reg0 = val >> 32; |
| 169 | *reg1 = val; |
| 170 | } |
| 171 | |
| 172 | #endif /*OPTEE_PRIVATE_H*/ |