qcomtee.h 6.0 KB

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