se.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <drivers/delay_timer.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <bpmp_ipc.h>
  11. #include <pmc.h>
  12. #include <security_engine.h>
  13. #include <tegra_private.h>
  14. #include "se_private.h"
  15. /*******************************************************************************
  16. * Constants and Macros
  17. ******************************************************************************/
  18. #define SE0_MAX_BUSY_TIMEOUT_MS U(100) /* 100ms */
  19. #define BYTES_IN_WORD U(4)
  20. #define SHA256_MAX_HASH_RESULT U(7)
  21. #define SHA256_DST_SIZE U(32)
  22. #define SHA_FIRST_OP U(1)
  23. #define MAX_SHA_ENGINE_CHUNK_SIZE U(0xFFFFFF)
  24. #define SHA256_MSG_LENGTH_ONETIME U(0xffff)
  25. /*
  26. * Check that SE operation has completed after kickoff
  27. * This function is invoked after an SE operation has been started,
  28. * and it checks the following conditions:
  29. * 1. SE0_INT_STATUS = SE0_OP_DONE
  30. * 2. SE0_STATUS = IDLE
  31. * 3. SE0_ERR_STATUS is clean.
  32. */
  33. static int32_t tegra_se_operation_complete(void)
  34. {
  35. uint32_t val = 0U;
  36. /* Read SE0 interrupt register to ensure H/W operation complete */
  37. val = tegra_se_read_32(SE0_INT_STATUS_REG_OFFSET);
  38. if (SE0_INT_OP_DONE(val) == SE0_INT_OP_DONE_CLEAR) {
  39. ERROR("%s: Engine busy state too many times! val = 0x%x\n",
  40. __func__, val);
  41. return -ETIMEDOUT;
  42. }
  43. /* Read SE0 status idle to ensure H/W operation complete */
  44. val = tegra_se_read_32(SE0_SHA_STATUS_0);
  45. if (val != SE0_SHA_STATUS_IDLE) {
  46. ERROR("%s: Idle state timeout! val = 0x%x\n", __func__,
  47. val);
  48. return -ETIMEDOUT;
  49. }
  50. /* Ensure that no errors are thrown during operation */
  51. val = tegra_se_read_32(SE0_ERR_STATUS_REG_OFFSET);
  52. if (val != SE0_ERR_STATUS_CLEAR) {
  53. ERROR("%s: Error during SE operation! val = 0x%x",
  54. __func__, val);
  55. return -ENOTSUP;
  56. }
  57. return 0;
  58. }
  59. /*
  60. * Security engine primitive normal operations
  61. */
  62. static int32_t tegra_se_start_normal_operation(uint64_t src_addr,
  63. uint32_t nbytes, uint32_t last_buf, uint32_t src_len_inbytes)
  64. {
  65. int32_t ret = 0;
  66. uint32_t val = 0U;
  67. uint32_t src_in_lo;
  68. uint32_t src_in_msb;
  69. uint32_t src_in_hi;
  70. if ((src_addr == 0UL) || (nbytes == 0U))
  71. return -EINVAL;
  72. src_in_lo = (uint32_t)src_addr;
  73. src_in_msb = ((uint32_t)(src_addr >> 32U) & 0xffU);
  74. src_in_hi = ((src_in_msb << SE0_IN_HI_ADDR_HI_0_MSB_SHIFT) |
  75. (nbytes & 0xffffffU));
  76. /* set SRC_IN_ADDR_LO and SRC_IN_ADDR_HI*/
  77. tegra_se_write_32(SE0_IN_ADDR, src_in_lo);
  78. tegra_se_write_32(SE0_IN_HI_ADDR_HI, src_in_hi);
  79. val = tegra_se_read_32(SE0_INT_STATUS_REG_OFFSET);
  80. if (val > 0U) {
  81. tegra_se_write_32(SE0_INT_STATUS_REG_OFFSET, 0x00000U);
  82. }
  83. /* Enable SHA interrupt for SE0 Operation */
  84. tegra_se_write_32(SE0_SHA_INT_ENABLE, 0x1aU);
  85. /* flush to DRAM for SE to use the updated contents */
  86. flush_dcache_range(src_addr, src_len_inbytes);
  87. /* Start SHA256 operation */
  88. if (last_buf == 1U) {
  89. tegra_se_write_32(SE0_OPERATION_REG_OFFSET, SE0_OP_START |
  90. SE0_UNIT_OPERATION_PKT_LASTBUF_FIELD);
  91. } else {
  92. tegra_se_write_32(SE0_OPERATION_REG_OFFSET, SE0_OP_START);
  93. }
  94. /* Wait for SE-operation to finish */
  95. udelay(SE0_MAX_BUSY_TIMEOUT_MS * 100U);
  96. /* Check SE0 operation status */
  97. ret = tegra_se_operation_complete();
  98. if (ret != 0) {
  99. ERROR("SE operation complete Failed! 0x%x", ret);
  100. return ret;
  101. }
  102. return 0;
  103. }
  104. static int32_t tegra_se_calculate_sha256_hash(uint64_t src_addr,
  105. uint32_t src_len_inbyte)
  106. {
  107. uint32_t val, last_buf, i;
  108. int32_t ret = 0;
  109. uint32_t operations;
  110. uint64_t src_len_inbits;
  111. uint32_t len_bits_msb;
  112. uint32_t len_bits_lsb;
  113. uint32_t number_of_operations, max_bytes, bytes_left, remaining_bytes;
  114. if (src_len_inbyte > MAX_SHA_ENGINE_CHUNK_SIZE) {
  115. ERROR("SHA input chunk size too big: 0x%x\n", src_len_inbyte);
  116. return -EINVAL;
  117. }
  118. if (src_addr == 0UL) {
  119. return -EINVAL;
  120. }
  121. /* number of bytes per operation */
  122. max_bytes = SHA256_HASH_SIZE_BYTES * SHA256_MSG_LENGTH_ONETIME;
  123. src_len_inbits = src_len_inbyte * 8U;
  124. len_bits_msb = (uint32_t)(src_len_inbits >> 32U);
  125. len_bits_lsb = (uint32_t)(src_len_inbits & 0xFFFFFFFF);
  126. /* program SE0_CONFIG for SHA256 operation */
  127. val = SE0_CONFIG_ENC_ALG_SHA | SE0_CONFIG_ENC_MODE_SHA256 |
  128. SE0_CONFIG_DEC_ALG_NOP | SE0_CONFIG_DST_HASHREG;
  129. tegra_se_write_32(SE0_SHA_CONFIG, val);
  130. /* set SE0_SHA_MSG_LENGTH registers */
  131. tegra_se_write_32(SE0_SHA_MSG_LENGTH_0, len_bits_lsb);
  132. tegra_se_write_32(SE0_SHA_MSG_LEFT_0, len_bits_lsb);
  133. tegra_se_write_32(SE0_SHA_MSG_LENGTH_1, len_bits_msb);
  134. /* zero out unused SE0_SHA_MSG_LENGTH and SE0_SHA_MSG_LEFT */
  135. tegra_se_write_32(SE0_SHA_MSG_LENGTH_2, 0U);
  136. tegra_se_write_32(SE0_SHA_MSG_LENGTH_3, 0U);
  137. tegra_se_write_32(SE0_SHA_MSG_LEFT_1, 0U);
  138. tegra_se_write_32(SE0_SHA_MSG_LEFT_2, 0U);
  139. tegra_se_write_32(SE0_SHA_MSG_LEFT_3, 0U);
  140. number_of_operations = src_len_inbyte / max_bytes;
  141. remaining_bytes = src_len_inbyte % max_bytes;
  142. if (remaining_bytes > 0U) {
  143. number_of_operations += 1U;
  144. }
  145. /*
  146. * 1. Operations == 1: program SE0_SHA_TASK register to initiate SHA256
  147. * hash generation by setting
  148. * 1(SE0_SHA_CONFIG_HW_INIT_HASH) to SE0_SHA_TASK
  149. * and start SHA256-normal operation.
  150. * 2. 1 < Operations < number_of_operations: program SE0_SHA_TASK to
  151. * 0(SE0_SHA_CONFIG_HW_INIT_HASH_DISABLE) to load
  152. * intermediate SHA256 digest result from
  153. * HASH_RESULT register to continue SHA256
  154. * generation and start SHA256-normal operation.
  155. * 3. Operations == number_of_operations: continue with step 2 and set
  156. * max_bytes to bytes_left to process final
  157. * hash-result generation and
  158. * start SHA256-normal operation.
  159. */
  160. bytes_left = src_len_inbyte;
  161. for (operations = 1U; operations <= number_of_operations;
  162. operations++) {
  163. if (operations == SHA_FIRST_OP) {
  164. val = SE0_SHA_CONFIG_HW_INIT_HASH;
  165. } else {
  166. /* Load intermediate SHA digest result to
  167. * SHA:HASH_RESULT(0..7) to continue the SHA
  168. * calculation and tell the SHA engine to use it.
  169. */
  170. for (i = 0U; (i / BYTES_IN_WORD) <=
  171. SHA256_MAX_HASH_RESULT; i += BYTES_IN_WORD) {
  172. val = tegra_se_read_32(SE0_SHA_HASH_RESULT_0 +
  173. i);
  174. tegra_se_write_32(SE0_SHA_HASH_RESULT_0 + i,
  175. val);
  176. }
  177. val = SE0_SHA_CONFIG_HW_INIT_HASH_DISABLE;
  178. if (len_bits_lsb <= (max_bytes * 8U)) {
  179. len_bits_lsb = (remaining_bytes * 8U);
  180. } else {
  181. len_bits_lsb -= (max_bytes * 8U);
  182. }
  183. tegra_se_write_32(SE0_SHA_MSG_LEFT_0, len_bits_lsb);
  184. }
  185. tegra_se_write_32(SE0_SHA_TASK_CONFIG, val);
  186. max_bytes = (SHA256_HASH_SIZE_BYTES *
  187. SHA256_MSG_LENGTH_ONETIME);
  188. if (bytes_left < max_bytes) {
  189. max_bytes = bytes_left;
  190. last_buf = 1U;
  191. } else {
  192. bytes_left = bytes_left - max_bytes;
  193. last_buf = 0U;
  194. }
  195. /* start operation */
  196. ret = tegra_se_start_normal_operation(src_addr, max_bytes,
  197. last_buf, src_len_inbyte);
  198. if (ret != 0) {
  199. ERROR("Error during SE operation! 0x%x", ret);
  200. return -EINVAL;
  201. }
  202. }
  203. return ret;
  204. }
  205. /*
  206. * Handler to generate SHA256 and save SHA256 hash to PMC-Scratch register.
  207. */
  208. int32_t tegra_se_save_sha256_hash(uint64_t bl31_base, uint32_t src_len_inbyte)
  209. {
  210. int32_t ret = 0;
  211. uint32_t val = 0U, hash_offset = 0U, scratch_offset = 0U, security;
  212. /*
  213. * Set SE_SOFT_SETTINGS=SE_SECURE to prevent NS process to change SE
  214. * registers.
  215. */
  216. security = tegra_se_read_32(SE0_SECURITY);
  217. tegra_se_write_32(SE0_SECURITY, security | SE0_SECURITY_SE_SOFT_SETTING);
  218. ret = tegra_se_calculate_sha256_hash(bl31_base, src_len_inbyte);
  219. if (ret != 0L) {
  220. ERROR("%s: SHA256 generation failed\n", __func__);
  221. return ret;
  222. }
  223. /*
  224. * Reset SE_SECURE to previous value.
  225. */
  226. tegra_se_write_32(SE0_SECURITY, security);
  227. /* read SHA256_HASH_RESULT and save to PMC Scratch registers */
  228. scratch_offset = SECURE_SCRATCH_TZDRAM_SHA256_HASH_START;
  229. while (scratch_offset <= SECURE_SCRATCH_TZDRAM_SHA256_HASH_END) {
  230. val = tegra_se_read_32(SE0_SHA_HASH_RESULT_0 + hash_offset);
  231. mmio_write_32(TEGRA_SCRATCH_BASE + scratch_offset, val);
  232. hash_offset += BYTES_IN_WORD;
  233. scratch_offset += BYTES_IN_WORD;
  234. }
  235. return ret;
  236. }