tzc_common_private.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2016-2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef TZC_COMMON_PRIVATE_H
  7. #define TZC_COMMON_PRIVATE_H
  8. #include <arch.h>
  9. #include <arch_helpers.h>
  10. #include <drivers/arm/tzc_common.h>
  11. #include <lib/mmio.h>
  12. #define DEFINE_TZC_COMMON_WRITE_ACTION(fn_name, macro_name) \
  13. static inline void _tzc##fn_name##_write_action( \
  14. uintptr_t base, \
  15. unsigned int action) \
  16. { \
  17. mmio_write_32(base + TZC_##macro_name##_ACTION_OFF, \
  18. action); \
  19. }
  20. #define DEFINE_TZC_COMMON_WRITE_REGION_BASE(fn_name, macro_name) \
  21. static inline void _tzc##fn_name##_write_region_base( \
  22. uintptr_t base, \
  23. unsigned int region_no, \
  24. unsigned long long region_base) \
  25. { \
  26. mmio_write_32(base + \
  27. TZC_REGION_OFFSET( \
  28. TZC_##macro_name##_REGION_SIZE, \
  29. (u_register_t)region_no) + \
  30. TZC_##macro_name##_REGION_BASE_LOW_0_OFFSET, \
  31. (uint32_t)region_base); \
  32. mmio_write_32(base + \
  33. TZC_REGION_OFFSET( \
  34. TZC_##macro_name##_REGION_SIZE, \
  35. (u_register_t)region_no) + \
  36. TZC_##macro_name##_REGION_BASE_HIGH_0_OFFSET, \
  37. (uint32_t)(region_base >> 32)); \
  38. }
  39. #define DEFINE_TZC_COMMON_WRITE_REGION_TOP(fn_name, macro_name) \
  40. static inline void _tzc##fn_name##_write_region_top( \
  41. uintptr_t base, \
  42. unsigned int region_no, \
  43. unsigned long long region_top) \
  44. { \
  45. mmio_write_32(base + \
  46. TZC_REGION_OFFSET( \
  47. TZC_##macro_name##_REGION_SIZE, \
  48. (u_register_t)region_no) + \
  49. TZC_##macro_name##_REGION_TOP_LOW_0_OFFSET, \
  50. (uint32_t)region_top); \
  51. mmio_write_32(base + \
  52. TZC_REGION_OFFSET( \
  53. TZC_##macro_name##_REGION_SIZE, \
  54. (u_register_t)region_no) + \
  55. TZC_##macro_name##_REGION_TOP_HIGH_0_OFFSET, \
  56. (uint32_t)(region_top >> 32)); \
  57. }
  58. #define DEFINE_TZC_COMMON_WRITE_REGION_ATTRIBUTES(fn_name, macro_name) \
  59. static inline void _tzc##fn_name##_write_region_attributes( \
  60. uintptr_t base, \
  61. unsigned int region_no, \
  62. unsigned int attr) \
  63. { \
  64. mmio_write_32(base + \
  65. TZC_REGION_OFFSET( \
  66. TZC_##macro_name##_REGION_SIZE, \
  67. (u_register_t)region_no) + \
  68. TZC_##macro_name##_REGION_ATTR_0_OFFSET, \
  69. attr); \
  70. }
  71. #define DEFINE_TZC_COMMON_WRITE_REGION_ID_ACCESS(fn_name, macro_name) \
  72. static inline void _tzc##fn_name##_write_region_id_access( \
  73. uintptr_t base, \
  74. unsigned int region_no, \
  75. unsigned int val) \
  76. { \
  77. mmio_write_32(base + \
  78. TZC_REGION_OFFSET( \
  79. TZC_##macro_name##_REGION_SIZE, \
  80. (u_register_t)region_no) + \
  81. TZC_##macro_name##_REGION_ID_ACCESS_0_OFFSET, \
  82. val); \
  83. }
  84. /*
  85. * It is used to modify the filters status for a defined region.
  86. */
  87. #define DEFINE_TZC_COMMON_UPDATE_FILTERS(fn_name, macro_name) \
  88. static inline void _tzc##fn_name##_update_filters( \
  89. uintptr_t base, \
  90. unsigned int region_no, \
  91. unsigned int nbfilters, \
  92. unsigned int filters) \
  93. { \
  94. uint32_t filters_mask = GENMASK(nbfilters - 1U, 0); \
  95. \
  96. mmio_clrsetbits_32(base + \
  97. TZC_REGION_OFFSET( \
  98. TZC_##macro_name##_REGION_SIZE, \
  99. region_no) + \
  100. TZC_##macro_name##_REGION_ATTR_0_OFFSET, \
  101. filters_mask << TZC_REGION_ATTR_F_EN_SHIFT, \
  102. filters << TZC_REGION_ATTR_F_EN_SHIFT); \
  103. }
  104. /*
  105. * It is used to program region 0 ATTRIBUTES and ACCESS register.
  106. */
  107. #define DEFINE_TZC_COMMON_CONFIGURE_REGION0(fn_name) \
  108. static void _tzc##fn_name##_configure_region0(uintptr_t base, \
  109. unsigned int sec_attr, \
  110. unsigned int ns_device_access) \
  111. { \
  112. assert(base != 0U); \
  113. VERBOSE("TrustZone : Configuring region 0 " \
  114. "(TZC Interface Base=0x%lx sec_attr=0x%x," \
  115. " ns_devs=0x%x)\n", base, \
  116. sec_attr, ns_device_access); \
  117. \
  118. /* Set secure attributes on region 0 */ \
  119. _tzc##fn_name##_write_region_attributes(base, 0, \
  120. sec_attr << TZC_REGION_ATTR_SEC_SHIFT); \
  121. \
  122. /***************************************************/ \
  123. /* Specify which non-secure devices have permission*/ \
  124. /* to access region 0. */ \
  125. /***************************************************/ \
  126. _tzc##fn_name##_write_region_id_access(base, \
  127. 0, \
  128. ns_device_access); \
  129. }
  130. /*
  131. * It is used to program a region from 1 to 8 in the TrustZone controller.
  132. * NOTE:
  133. * Region 0 is special; it is preferable to use
  134. * ##fn_name##_configure_region0 for this region (see comment for
  135. * that function).
  136. */
  137. #define DEFINE_TZC_COMMON_CONFIGURE_REGION(fn_name) \
  138. static void _tzc##fn_name##_configure_region(uintptr_t base, \
  139. unsigned int filters, \
  140. unsigned int region_no, \
  141. unsigned long long region_base, \
  142. unsigned long long region_top, \
  143. unsigned int sec_attr, \
  144. unsigned int nsaid_permissions) \
  145. { \
  146. assert(base != 0U); \
  147. VERBOSE("TrustZone : Configuring region " \
  148. "(TZC Interface Base: 0x%lx, region_no = %u)" \
  149. "...\n", base, region_no); \
  150. VERBOSE("TrustZone : ... base = %llx, top = %llx," \
  151. "\n", region_base, region_top); \
  152. VERBOSE("TrustZone : ... sec_attr = 0x%x," \
  153. " ns_devs = 0x%x)\n", \
  154. sec_attr, nsaid_permissions); \
  155. \
  156. /***************************************************/ \
  157. /* Inputs look ok, start programming registers. */ \
  158. /* All the address registers are 32 bits wide and */ \
  159. /* have a LOW and HIGH */ \
  160. /* component used to construct an address up to a */ \
  161. /* 64bit. */ \
  162. /***************************************************/ \
  163. _tzc##fn_name##_write_region_base(base, \
  164. region_no, region_base); \
  165. _tzc##fn_name##_write_region_top(base, \
  166. region_no, region_top); \
  167. \
  168. /* Enable filter to the region and set secure attributes */\
  169. _tzc##fn_name##_write_region_attributes(base, \
  170. region_no, \
  171. (sec_attr << TZC_REGION_ATTR_SEC_SHIFT) |\
  172. (filters << TZC_REGION_ATTR_F_EN_SHIFT));\
  173. \
  174. /***************************************************/ \
  175. /* Specify which non-secure devices have permission*/ \
  176. /* to access this region. */ \
  177. /***************************************************/ \
  178. _tzc##fn_name##_write_region_id_access(base, \
  179. region_no, \
  180. nsaid_permissions); \
  181. }
  182. static inline unsigned int _tzc_read_peripheral_id(uintptr_t base)
  183. {
  184. unsigned int id;
  185. id = mmio_read_32(base + PID0_OFF);
  186. /* Masks DESC part in PID1 */
  187. id |= ((mmio_read_32(base + PID1_OFF) & 0xFU) << 8U);
  188. return id;
  189. }
  190. #endif /* TZC_COMMON_PRIVATE_H */