1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. 4 */ 5 6 #ifndef QCOMTEE_H 7 #define QCOMTEE_H 8 9 #include <linux/kobject.h> 10 #include <linux/tee_core.h> 11 12 #include "qcomtee_msg.h" 13 #include "qcomtee_object.h" 14 15 /* Flags relating to object reference. */ 16 #define QCOMTEE_OBJREF_FLAG_TEE BIT(0) 17 #define QCOMTEE_OBJREF_FLAG_USER BIT(1) 18 #define QCOMTEE_OBJREF_FLAG_MEM BIT(2) 19 20 /** 21 * struct qcomtee - Main service struct. 22 * @teedev: client device. 23 * @pool: shared memory pool. 24 * @ctx: driver private context. 25 * @oic: context to use for the current driver invocation. 26 * @wq: workqueue for QTEE async operations. 27 * @xa_local_objects: array of objects exported to QTEE. 28 * @xa_last_id: next ID to allocate. 29 * @qtee_version: QTEE version. 30 */ 31 struct qcomtee { 32 struct tee_device *teedev; 33 struct tee_shm_pool *pool; 34 struct tee_context *ctx; 35 struct qcomtee_object_invoke_ctx oic; 36 struct workqueue_struct *wq; 37 struct xarray xa_local_objects; 38 u32 xa_last_id; 39 u32 qtee_version; 40 }; 41 42 void qcomtee_fetch_async_reqs(struct qcomtee_object_invoke_ctx *oic); 43 struct qcomtee_object *qcomtee_idx_erase(struct qcomtee_object_invoke_ctx *oic, 44 u32 idx); 45 46 struct tee_shm_pool *qcomtee_shm_pool_alloc(void); 47 void qcomtee_msg_buffers_free(struct qcomtee_object_invoke_ctx *oic); 48 int qcomtee_msg_buffers_alloc(struct qcomtee_object_invoke_ctx *oic, 49 struct qcomtee_arg *u); 50 51 /** 52 * qcomtee_object_do_invoke_internal() - Submit an invocation for an object. 53 * @oic: context to use for the current invocation. 54 * @object: object being invoked. 55 * @op: requested operation on the object. 56 * @u: array of arguments for the current invocation. 57 * @result: result returned from QTEE. 58 * 59 * The caller is responsible for keeping track of the refcount for each 60 * object, including @object. On return, the caller loses ownership of all 61 * input objects of type %QCOMTEE_OBJECT_TYPE_CB. 62 * 63 * Return: On success, returns 0; on failure, returns < 0. 64 */ 65 int qcomtee_object_do_invoke_internal(struct qcomtee_object_invoke_ctx *oic, 66 struct qcomtee_object *object, u32 op, 67 struct qcomtee_arg *u, int *result); 68 69 /** 70 * struct qcomtee_context_data - Clients' or supplicants' context. 71 * @qtee_objects_idr: QTEE objects in this context. 72 * @qtee_lock: mutex for @qtee_objects_idr. 73 * @reqs_idr: requests in this context that hold ID. 74 * @reqs_list: FIFO for requests in PROCESSING or QUEUED state. 75 * @reqs_lock: mutex for @reqs_idr, @reqs_list and request states. 76 * @req_c: completion used when the supplicant is waiting for requests. 77 * @released: state of this context. 78 */ 79 struct qcomtee_context_data { 80 struct idr qtee_objects_idr; 81 /* Synchronize access to @qtee_objects_idr. */ 82 struct mutex qtee_lock; 83 84 struct idr reqs_idr; 85 struct list_head reqs_list; 86 /* Synchronize access to @reqs_idr, @reqs_list and updating requests states. */ 87 struct mutex reqs_lock; 88 89 struct completion req_c; 90 91 bool released; 92 }; 93 94 int qcomtee_context_add_qtee_object(struct tee_param *param, 95 struct qcomtee_object *object, 96 struct tee_context *ctx); 97 int qcomtee_context_find_qtee_object(struct qcomtee_object **object, 98 struct tee_param *param, 99 struct tee_context *ctx); 100 void qcomtee_context_del_qtee_object(struct tee_param *param, 101 struct tee_context *ctx); 102 103 int qcomtee_objref_to_arg(struct qcomtee_arg *arg, struct tee_param *param, 104 struct tee_context *ctx); 105 int qcomtee_objref_from_arg(struct tee_param *param, struct qcomtee_arg *arg, 106 struct tee_context *ctx); 107 108 /* OBJECTS: */ 109 110 /* (1) User Object API. */ 111 112 int is_qcomtee_user_object(struct qcomtee_object *object); 113 void qcomtee_user_object_set_notify(struct qcomtee_object *object, bool notify); 114 void qcomtee_requests_destroy(struct qcomtee_context_data *ctxdata); 115 int qcomtee_user_param_to_object(struct qcomtee_object **object, 116 struct tee_param *param, 117 struct tee_context *ctx); 118 int qcomtee_user_param_from_object(struct tee_param *param, 119 struct qcomtee_object *object, 120 struct tee_context *ctx); 121 122 /** 123 * struct qcomtee_user_object_request_data - Data for user object request. 124 * @id: ID assigned to the request. 125 * @object_id: Object ID being invoked by QTEE. 126 * @op: Requested operation on object. 127 * @np: Number of parameters in the request. 128 */ 129 struct qcomtee_user_object_request_data { 130 int id; 131 u64 object_id; 132 u32 op; 133 int np; 134 }; 135 136 int qcomtee_user_object_select(struct tee_context *ctx, 137 struct tee_param *params, int num_params, 138 void __user *uaddr, size_t size, 139 struct qcomtee_user_object_request_data *data); 140 int qcomtee_user_object_submit(struct tee_context *ctx, 141 struct tee_param *params, int num_params, 142 int req_id, int errno); 143 144 /* (2) Primordial Object. */ 145 extern struct qcomtee_object qcomtee_primordial_object; 146 147 /* (3) Memory Object API. */ 148 149 /* Is it a memory object using tee_shm? */ 150 int is_qcomtee_memobj_object(struct qcomtee_object *object); 151 152 /** 153 * qcomtee_memobj_param_to_object() - OBJREF parameter to &struct qcomtee_object. 154 * @object: object returned. 155 * @param: TEE parameter. 156 * @ctx: context in which the conversion should happen. 157 * 158 * @param is an OBJREF with %QCOMTEE_OBJREF_FLAG_MEM flags. 159 * 160 * Return: On success return 0 or <0 on failure. 161 */ 162 int qcomtee_memobj_param_to_object(struct qcomtee_object **object, 163 struct tee_param *param, 164 struct tee_context *ctx); 165 166 /* Reverse what qcomtee_memobj_param_to_object() does. */ 167 int qcomtee_memobj_param_from_object(struct tee_param *param, 168 struct qcomtee_object *object, 169 struct tee_context *ctx); 170 171 /** 172 * qcomtee_mem_object_map() - Map a memory object. 173 * @object: memory object. 174 * @map_object: created mapping object. 175 * @mem_paddr: physical address of the memory. 176 * @mem_size: size of the memory. 177 * @perms: QTEE access permissions. 178 * 179 * Return: On success return 0 or <0 on failure. 180 */ 181 int qcomtee_mem_object_map(struct qcomtee_object *object, 182 struct qcomtee_object **map_object, u64 *mem_paddr, 183 u64 *mem_size, u32 *perms); 184 185 #endif /* QCOMTEE_H */ 186