1 /* 2 * Copyright (c) 2015-2016, 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 __TEE_DRV_H 16 #define __TEE_DRV_H 17 18 #include <linux/types.h> 19 #include <linux/idr.h> 20 #include <linux/list.h> 21 #include <linux/tee.h> 22 23 /* 24 * The file describes the API provided by the generic TEE driver to the 25 * specific TEE driver. 26 */ 27 28 #define TEE_SHM_MAPPED 0x1 /* Memory mapped by the kernel */ 29 #define TEE_SHM_DMA_BUF 0x2 /* Memory with dma-buf handle */ 30 31 struct tee_device; 32 struct tee_shm; 33 struct tee_shm_pool; 34 35 /** 36 * struct tee_context - driver specific context on file pointer data 37 * @teedev: pointer to this drivers struct tee_device 38 * @list_shm: List of shared memory object owned by this context 39 * @data: driver specific context data, managed by the driver 40 */ 41 struct tee_context { 42 struct tee_device *teedev; 43 struct list_head list_shm; 44 void *data; 45 }; 46 47 struct tee_param_memref { 48 size_t shm_offs; 49 size_t size; 50 struct tee_shm *shm; 51 }; 52 53 struct tee_param_value { 54 u64 a; 55 u64 b; 56 u64 c; 57 }; 58 59 struct tee_param { 60 u64 attr; 61 union { 62 struct tee_param_memref memref; 63 struct tee_param_value value; 64 } u; 65 }; 66 67 /** 68 * struct tee_driver_ops - driver operations vtable 69 * @get_version: returns version of driver 70 * @open: called when the device file is opened 71 * @release: release this open file 72 * @open_session: open a new session 73 * @close_session: close a session 74 * @invoke_func: invoke a trusted function 75 * @cancel_req: request cancel of an ongoing invoke or open 76 * @supp_revc: called for supplicant to get a command 77 * @supp_send: called for supplicant to send a response 78 */ 79 struct tee_driver_ops { 80 void (*get_version)(struct tee_device *teedev, 81 struct tee_ioctl_version_data *vers); 82 int (*open)(struct tee_context *ctx); 83 void (*release)(struct tee_context *ctx); 84 int (*open_session)(struct tee_context *ctx, 85 struct tee_ioctl_open_session_arg *arg, 86 struct tee_param *param); 87 int (*close_session)(struct tee_context *ctx, u32 session); 88 int (*invoke_func)(struct tee_context *ctx, 89 struct tee_ioctl_invoke_arg *arg, 90 struct tee_param *param); 91 int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session); 92 int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params, 93 struct tee_param *param); 94 int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params, 95 struct tee_param *param); 96 }; 97 98 /** 99 * struct tee_desc - Describes the TEE driver to the subsystem 100 * @name: name of driver 101 * @ops: driver operations vtable 102 * @owner: module providing the driver 103 * @flags: Extra properties of driver, defined by TEE_DESC_* below 104 */ 105 #define TEE_DESC_PRIVILEGED 0x1 106 struct tee_desc { 107 const char *name; 108 const struct tee_driver_ops *ops; 109 struct module *owner; 110 u32 flags; 111 }; 112 113 /** 114 * tee_device_alloc() - Allocate a new struct tee_device instance 115 * @teedesc: Descriptor for this driver 116 * @dev: Parent device for this device 117 * @pool: Shared memory pool, NULL if not used 118 * @driver_data: Private driver data for this device 119 * 120 * Allocates a new struct tee_device instance. The device is 121 * removed by tee_device_unregister(). 122 * 123 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure 124 */ 125 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, 126 struct device *dev, 127 struct tee_shm_pool *pool, 128 void *driver_data); 129 130 /** 131 * tee_device_register() - Registers a TEE device 132 * @teedev: Device to register 133 * 134 * tee_device_unregister() need to be called to remove the @teedev if 135 * this function fails. 136 * 137 * @returns < 0 on failure 138 */ 139 int tee_device_register(struct tee_device *teedev); 140 141 /** 142 * tee_device_unregister() - Removes a TEE device 143 * @teedev: Device to unregister 144 * 145 * This function should be called to remove the @teedev even if 146 * tee_device_register() hasn't been called yet. Does nothing if 147 * @teedev is NULL. 148 */ 149 void tee_device_unregister(struct tee_device *teedev); 150 151 /** 152 * struct tee_shm_pool_mem_info - holds information needed to create a shared 153 * memory pool 154 * @vaddr: Virtual address of start of pool 155 * @paddr: Physical address of start of pool 156 * @size: Size in bytes of the pool 157 */ 158 struct tee_shm_pool_mem_info { 159 unsigned long vaddr; 160 phys_addr_t paddr; 161 size_t size; 162 }; 163 164 /** 165 * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved 166 * memory range 167 * @priv_info: Information for driver private shared memory pool 168 * @dmabuf_info: Information for dma-buf shared memory pool 169 * 170 * Start and end of pools will must be page aligned. 171 * 172 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied 173 * in @dmabuf, others will use the range provided by @priv. 174 * 175 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. 176 */ 177 struct tee_shm_pool * 178 tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info, 179 struct tee_shm_pool_mem_info *dmabuf_info); 180 181 /** 182 * tee_shm_pool_free() - Free a shared memory pool 183 * @pool: The shared memory pool to free 184 * 185 * The must be no remaining shared memory allocated from this pool when 186 * this function is called. 187 */ 188 void tee_shm_pool_free(struct tee_shm_pool *pool); 189 190 /** 191 * tee_get_drvdata() - Return driver_data pointer 192 * @returns the driver_data pointer supplied to tee_register(). 193 */ 194 void *tee_get_drvdata(struct tee_device *teedev); 195 196 /** 197 * tee_shm_alloc() - Allocate shared memory 198 * @ctx: Context that allocates the shared memory 199 * @size: Requested size of shared memory 200 * @flags: Flags setting properties for the requested shared memory. 201 * 202 * Memory allocated as global shared memory is automatically freed when the 203 * TEE file pointer is closed. The @flags field uses the bits defined by 204 * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If 205 * TEE_SHM_DMA_BUF global shared memory will be allocated and associated 206 * with a dma-buf handle, else driver private memory. 207 * 208 * @returns a pointer to 'struct tee_shm' 209 */ 210 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags); 211 212 /** 213 * tee_shm_free() - Free shared memory 214 * @shm: Handle to shared memory to free 215 */ 216 void tee_shm_free(struct tee_shm *shm); 217 218 /** 219 * tee_shm_put() - Decrease reference count on a shared memory handle 220 * @shm: Shared memory handle 221 */ 222 void tee_shm_put(struct tee_shm *shm); 223 224 /** 225 * tee_shm_va2pa() - Get physical address of a virtual address 226 * @shm: Shared memory handle 227 * @va: Virtual address to tranlsate 228 * @pa: Returned physical address 229 * @returns 0 on success and < 0 on failure 230 */ 231 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa); 232 233 /** 234 * tee_shm_pa2va() - Get virtual address of a physical address 235 * @shm: Shared memory handle 236 * @pa: Physical address to tranlsate 237 * @va: Returned virtual address 238 * @returns 0 on success and < 0 on failure 239 */ 240 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va); 241 242 /** 243 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset 244 * @shm: Shared memory handle 245 * @offs: Offset from start of this shared memory 246 * @returns virtual address of the shared memory + offs if offs is within 247 * the bounds of this shared memory, else an ERR_PTR 248 */ 249 void *tee_shm_get_va(struct tee_shm *shm, size_t offs); 250 251 /** 252 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset 253 * @shm: Shared memory handle 254 * @offs: Offset from start of this shared memory 255 * @pa: Physical address to return 256 * @returns 0 if offs is within the bounds of this shared memory, else an 257 * error code. 258 */ 259 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa); 260 261 /** 262 * tee_shm_get_id() - Get id of a shared memory object 263 * @shm: Shared memory handle 264 * @returns id 265 */ 266 int tee_shm_get_id(struct tee_shm *shm); 267 268 /** 269 * tee_shm_get_from_id() - Find shared memory object and increase reference 270 * count 271 * @ctx: Context owning the shared memory 272 * @id: Id of shared memory object 273 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure 274 */ 275 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id); 276 277 #endif /*__TEE_DRV_H*/ 278