plat_tzc400.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <common/debug.h>
  8. #include <plat_tzc400.h>
  9. #pragma weak populate_tzc400_reg_list
  10. #ifdef DEFAULT_TZASC_CONFIG
  11. /*
  12. * Typical Memory map of DRAM0
  13. * |-----------NXP_NS_DRAM_ADDR ( = NXP_DRAM0_ADDR)----------|
  14. * | |
  15. * | |
  16. * | Non-SECURE REGION |
  17. * | |
  18. * | |
  19. * | |
  20. * |------- (NXP_NS_DRAM_ADDR + NXP_NS_DRAM_SIZE - 1) -------|
  21. * |-----------------NXP_SECURE_DRAM_ADDR--------------------|
  22. * | |
  23. * | |
  24. * | |
  25. * | SECURE REGION (= 64MB) |
  26. * | |
  27. * | |
  28. * | |
  29. * |--- (NXP_SECURE_DRAM_ADDR + NXP_SECURE_DRAM_SIZE - 1)----|
  30. * |-----------------NXP_SP_SHRD_DRAM_ADDR-------------------|
  31. * | |
  32. * | Secure EL1 Payload SHARED REGION (= 2MB) |
  33. * | |
  34. * |-----------(NXP_DRAM0_ADDR + NXP_DRAM0_SIZE - 1)---------|
  35. *
  36. *
  37. *
  38. * Typical Memory map of DRAM1
  39. * |---------------------NXP_DRAM1_ADDR----------------------|
  40. * | |
  41. * | |
  42. * | Non-SECURE REGION |
  43. * | |
  44. * | |
  45. * |---(NXP_DRAM1_ADDR + Dynamically calculated Size - 1) ---|
  46. *
  47. *
  48. * Typical Memory map of DRAM2
  49. * |---------------------NXP_DRAM2_ADDR----------------------|
  50. * | |
  51. * | |
  52. * | Non-SECURE REGION |
  53. * | |
  54. * | |
  55. * |---(NXP_DRAM2_ADDR + Dynamically calculated Size - 1) ---|
  56. */
  57. /*****************************************************************************
  58. * This function sets up access permissions on memory regions
  59. *
  60. * Input:
  61. * tzc400_reg_list : TZC400 Region List
  62. * dram_idx : DRAM index
  63. * list_idx : TZC400 Region List Index
  64. * dram_start_addr : Start address of DRAM at dram_idx.
  65. * dram_size : Size of DRAM at dram_idx.
  66. * secure_dram_sz : Secure DRAM Size
  67. * shrd_dram_sz : Shared DRAM Size
  68. *
  69. * Out:
  70. * list_idx : last populated index + 1
  71. *
  72. ****************************************************************************/
  73. int populate_tzc400_reg_list(struct tzc400_reg *tzc400_reg_list,
  74. int dram_idx, int list_idx,
  75. uint64_t dram_start_addr,
  76. uint64_t dram_size,
  77. uint32_t secure_dram_sz,
  78. uint32_t shrd_dram_sz)
  79. {
  80. if (list_idx == 0) {
  81. /* No need to configure TZC Region 0 in this list.
  82. */
  83. list_idx++;
  84. }
  85. /* Continue with list entries for index > 0 */
  86. if (dram_idx == 0) {
  87. /* TZC Region 1 on DRAM0 for Secure Memory*/
  88. tzc400_reg_list[list_idx].reg_filter_en = 1;
  89. tzc400_reg_list[list_idx].start_addr = dram_start_addr + dram_size;
  90. tzc400_reg_list[list_idx].end_addr = dram_start_addr + dram_size
  91. + secure_dram_sz - 1;
  92. tzc400_reg_list[list_idx].sec_attr = TZC_REGION_S_RDWR;
  93. tzc400_reg_list[list_idx].nsaid_permissions = TZC_REGION_NS_NONE;
  94. list_idx++;
  95. /* TZC Region 2 on DRAM0 for Shared Memory*/
  96. tzc400_reg_list[list_idx].reg_filter_en = 1;
  97. tzc400_reg_list[list_idx].start_addr = dram_start_addr + dram_size
  98. + secure_dram_sz;
  99. tzc400_reg_list[list_idx].end_addr = dram_start_addr + dram_size
  100. + secure_dram_sz
  101. + shrd_dram_sz
  102. - 1;
  103. tzc400_reg_list[list_idx].sec_attr = TZC_REGION_S_RDWR;
  104. tzc400_reg_list[list_idx].nsaid_permissions = TZC_NS_ACCESS_ID;
  105. list_idx++;
  106. /* TZC Region 3 on DRAM0 for Non-Secure Memory*/
  107. tzc400_reg_list[list_idx].reg_filter_en = 1;
  108. tzc400_reg_list[list_idx].start_addr = dram_start_addr;
  109. tzc400_reg_list[list_idx].end_addr = dram_start_addr + dram_size
  110. - 1;
  111. tzc400_reg_list[list_idx].sec_attr = TZC_REGION_S_RDWR;
  112. tzc400_reg_list[list_idx].nsaid_permissions = TZC_NS_ACCESS_ID;
  113. list_idx++;
  114. } else {
  115. /* TZC Region 3+i on DRAM(> 0) for Non-Secure Memory*/
  116. tzc400_reg_list[list_idx].reg_filter_en = 1;
  117. tzc400_reg_list[list_idx].start_addr = dram_start_addr;
  118. tzc400_reg_list[list_idx].end_addr = dram_start_addr + dram_size
  119. - 1;
  120. tzc400_reg_list[list_idx].sec_attr = TZC_REGION_S_RDWR;
  121. tzc400_reg_list[list_idx].nsaid_permissions = TZC_NS_ACCESS_ID;
  122. list_idx++;
  123. }
  124. return list_idx;
  125. }
  126. #else
  127. int populate_tzc400_reg_list(struct tzc400_reg *tzc400_reg_list,
  128. int dram_idx, int list_idx,
  129. uint64_t dram_start_addr,
  130. uint64_t dram_size,
  131. uint32_t secure_dram_sz,
  132. uint32_t shrd_dram_sz)
  133. {
  134. ERROR("tzc400_reg_list used is not a default list\n");
  135. ERROR("%s needs to be over-written.\n", __func__);
  136. return 0;
  137. }
  138. #endif /* DEFAULT_TZASC_CONFIG */
  139. /*******************************************************************************
  140. * Configure memory access permissions
  141. * - Region 0 with no access;
  142. * - Region 1 to 4 as per the tzc400_reg_list populated by
  143. * function populate_tzc400_reg_list() with default for all the SoC.
  144. ******************************************************************************/
  145. void mem_access_setup(uintptr_t base, uint32_t total_regions,
  146. struct tzc400_reg *tzc400_reg_list)
  147. {
  148. uint32_t list_indx = 0U;
  149. INFO("Configuring TrustZone Controller\n");
  150. tzc400_init(base);
  151. /* Disable filters. */
  152. tzc400_disable_filters();
  153. /* Region 0 set to no access by default */
  154. tzc400_configure_region0(TZC_REGION_S_NONE, 0U);
  155. for (list_indx = 1U; list_indx < total_regions; list_indx++) {
  156. tzc400_configure_region(
  157. tzc400_reg_list[list_indx].reg_filter_en,
  158. list_indx,
  159. tzc400_reg_list[list_indx].start_addr,
  160. tzc400_reg_list[list_indx].end_addr,
  161. tzc400_reg_list[list_indx].sec_attr,
  162. tzc400_reg_list[list_indx].nsaid_permissions);
  163. }
  164. /*
  165. * Raise an exception if a NS device tries to access secure memory
  166. * TODO: Add interrupt handling support.
  167. */
  168. tzc400_set_action(TZC_ACTION_ERR);
  169. /* Enable filters. */
  170. tzc400_enable_filters();
  171. }