amdtee_private.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright 2019 Advanced Micro Devices, Inc.
  4. */
  5. #ifndef AMDTEE_PRIVATE_H
  6. #define AMDTEE_PRIVATE_H
  7. #include <linux/mutex.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/tee_drv.h>
  10. #include <linux/kref.h>
  11. #include <linux/types.h>
  12. #include "amdtee_if.h"
  13. #define DRIVER_NAME "amdtee"
  14. #define DRIVER_AUTHOR "AMD-TEE Linux driver team"
  15. /* Some GlobalPlatform error codes used in this driver */
  16. #define TEEC_SUCCESS 0x00000000
  17. #define TEEC_ERROR_GENERIC 0xFFFF0000
  18. #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
  19. #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
  20. #define TEEC_ERROR_COMMUNICATION 0xFFFF000E
  21. #define TEEC_ORIGIN_COMMS 0x00000002
  22. /* Maximum number of sessions which can be opened with a Trusted Application */
  23. #define TEE_NUM_SESSIONS 32
  24. #define TA_LOAD_PATH "/amdtee"
  25. #define TA_PATH_MAX 60
  26. /**
  27. * struct amdtee - main service struct
  28. * @teedev: client device
  29. * @pool: shared memory pool
  30. */
  31. struct amdtee {
  32. struct tee_device *teedev;
  33. struct tee_shm_pool *pool;
  34. };
  35. /**
  36. * struct amdtee_session - Trusted Application (TA) session related information.
  37. * @ta_handle: handle to Trusted Application (TA) loaded in TEE environment
  38. * @refcount: counter to keep track of sessions opened for the TA instance
  39. * @session_info: an array pointing to TA allocated session data.
  40. * @sess_mask: session usage bit-mask. If a particular bit is set, then the
  41. * corresponding @session_info entry is in use or valid.
  42. *
  43. * Session structure is updated on open_session and this information is used for
  44. * subsequent operations with the Trusted Application.
  45. */
  46. struct amdtee_session {
  47. struct list_head list_node;
  48. u32 ta_handle;
  49. struct kref refcount;
  50. u32 session_info[TEE_NUM_SESSIONS];
  51. DECLARE_BITMAP(sess_mask, TEE_NUM_SESSIONS);
  52. spinlock_t lock; /* synchronizes access to @sess_mask */
  53. };
  54. /**
  55. * struct amdtee_context_data - AMD-TEE driver context data
  56. * @sess_list: Keeps track of sessions opened in current TEE context
  57. * @shm_list: Keeps track of buffers allocated and mapped in current TEE
  58. * context
  59. */
  60. struct amdtee_context_data {
  61. struct list_head sess_list;
  62. struct list_head shm_list;
  63. struct mutex shm_mutex; /* synchronizes access to @shm_list */
  64. };
  65. struct amdtee_driver_data {
  66. struct amdtee *amdtee;
  67. };
  68. struct shmem_desc {
  69. void *kaddr;
  70. u64 size;
  71. };
  72. /**
  73. * struct amdtee_shm_data - Shared memory data
  74. * @kaddr: Kernel virtual address of shared memory
  75. * @buf_id: Buffer id of memory mapped by TEE_CMD_ID_MAP_SHARED_MEM
  76. */
  77. struct amdtee_shm_data {
  78. struct list_head shm_node;
  79. void *kaddr;
  80. u32 buf_id;
  81. };
  82. /**
  83. * struct amdtee_ta_data - Keeps track of all TAs loaded in AMD Secure
  84. * Processor
  85. * @ta_handle: Handle to TA loaded in TEE
  86. * @refcount: Reference count for the loaded TA
  87. */
  88. struct amdtee_ta_data {
  89. struct list_head list_node;
  90. u32 ta_handle;
  91. u32 refcount;
  92. };
  93. #define LOWER_TWO_BYTE_MASK 0x0000FFFF
  94. /**
  95. * set_session_id() - Sets the session identifier.
  96. * @ta_handle: [in] handle of the loaded Trusted Application (TA)
  97. * @session_index: [in] Session index. Range: 0 to (TEE_NUM_SESSIONS - 1).
  98. * @session: [out] Pointer to session id
  99. *
  100. * Lower two bytes of the session identifier represents the TA handle and the
  101. * upper two bytes is session index.
  102. */
  103. static inline void set_session_id(u32 ta_handle, u32 session_index,
  104. u32 *session)
  105. {
  106. *session = (session_index << 16) | (LOWER_TWO_BYTE_MASK & ta_handle);
  107. }
  108. static inline u32 get_ta_handle(u32 session)
  109. {
  110. return session & LOWER_TWO_BYTE_MASK;
  111. }
  112. static inline u32 get_session_index(u32 session)
  113. {
  114. return (session >> 16) & LOWER_TWO_BYTE_MASK;
  115. }
  116. int amdtee_open_session(struct tee_context *ctx,
  117. struct tee_ioctl_open_session_arg *arg,
  118. struct tee_param *param);
  119. int amdtee_close_session(struct tee_context *ctx, u32 session);
  120. int amdtee_invoke_func(struct tee_context *ctx,
  121. struct tee_ioctl_invoke_arg *arg,
  122. struct tee_param *param);
  123. int amdtee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
  124. int amdtee_map_shmem(struct tee_shm *shm);
  125. void amdtee_unmap_shmem(struct tee_shm *shm);
  126. int handle_load_ta(void *data, u32 size,
  127. struct tee_ioctl_open_session_arg *arg);
  128. int handle_unload_ta(u32 ta_handle);
  129. int handle_open_session(struct tee_ioctl_open_session_arg *arg, u32 *info,
  130. struct tee_param *p);
  131. int handle_close_session(u32 ta_handle, u32 info);
  132. int handle_map_shmem(u32 count, struct shmem_desc *start, u32 *buf_id);
  133. void handle_unmap_shmem(u32 buf_id);
  134. int handle_invoke_cmd(struct tee_ioctl_invoke_arg *arg, u32 sinfo,
  135. struct tee_param *p);
  136. struct tee_shm_pool *amdtee_config_shm(void);
  137. u32 get_buffer_id(struct tee_shm *shm);
  138. #endif /*AMDTEE_PRIVATE_H*/