emi_mpu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2021-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include <emi_mpu.h>
  10. #include <mtk_sip_svc.h>
  11. #if ENABLE_EMI_MPU_SW_LOCK
  12. static unsigned char region_lock_state[EMI_MPU_REGION_NUM];
  13. #endif
  14. #define EMI_MPU_START_MASK (0x00FFFFFF)
  15. #define EMI_MPU_END_MASK (0x00FFFFFF)
  16. #define EMI_MPU_APC_SW_LOCK_MASK (0x00FFFFFF)
  17. #define EMI_MPU_APC_HW_LOCK_MASK (0x80FFFFFF)
  18. #define MPU_PHYSICAL_ADDR_SHIFT_BITS (16)
  19. static int _emi_mpu_set_protection(unsigned int start, unsigned int end,
  20. unsigned int apc)
  21. {
  22. unsigned int dgroup;
  23. unsigned int region;
  24. region = (start >> 24) & 0xFF;
  25. start &= EMI_MPU_START_MASK;
  26. dgroup = (end >> 24) & 0xFF;
  27. end &= EMI_MPU_END_MASK;
  28. if ((region >= EMI_MPU_REGION_NUM) || (dgroup > EMI_MPU_DGROUP_NUM)) {
  29. WARN("invalid region, domain\n");
  30. return -1;
  31. }
  32. #if ENABLE_EMI_MPU_SW_LOCK
  33. if (region_lock_state[region] == 1) {
  34. WARN("invalid region\n");
  35. return -1;
  36. }
  37. if ((dgroup == 0) && ((apc >> 31) & 0x1)) {
  38. region_lock_state[region] = 1;
  39. }
  40. apc &= EMI_MPU_APC_SW_LOCK_MASK;
  41. #else
  42. apc &= EMI_MPU_APC_HW_LOCK_MASK;
  43. #endif
  44. if ((start >= DRAM_OFFSET) && (end >= start)) {
  45. start -= DRAM_OFFSET;
  46. end -= DRAM_OFFSET;
  47. } else {
  48. WARN("invalid range\n");
  49. return -1;
  50. }
  51. mmio_write_32(EMI_MPU_SA(region), start);
  52. mmio_write_32(EMI_MPU_EA(region), end);
  53. mmio_write_32(EMI_MPU_APC(region, dgroup), apc);
  54. #if defined(SUB_EMI_MPU_BASE)
  55. mmio_write_32(SUB_EMI_MPU_SA(region), start);
  56. mmio_write_32(SUB_EMI_MPU_EA(region), end);
  57. mmio_write_32(SUB_EMI_MPU_APC(region, dgroup), apc);
  58. #endif
  59. return 1;
  60. }
  61. int emi_mpu_set_protection(struct emi_region_info_t *region_info)
  62. {
  63. unsigned int start, end;
  64. int i;
  65. if (region_info->region >= EMI_MPU_REGION_NUM) {
  66. WARN("invalid region\n");
  67. return -1;
  68. }
  69. start = (unsigned int)(region_info->start >> EMI_MPU_ALIGN_BITS) |
  70. (region_info->region << 24);
  71. for (i = EMI_MPU_DGROUP_NUM - 1; i >= 0; i--) {
  72. end = (unsigned int)(region_info->end >> EMI_MPU_ALIGN_BITS) |
  73. (i << 24);
  74. _emi_mpu_set_protection(start, end, region_info->apc[i]);
  75. }
  76. return 0;
  77. }
  78. void dump_emi_mpu_regions(void)
  79. {
  80. unsigned long apc[EMI_MPU_DGROUP_NUM], sa, ea;
  81. int region, i;
  82. /* Only dump 8 regions(max: EMI_MPU_REGION_NUM --> 32) */
  83. for (region = 0; region < 8; ++region) {
  84. for (i = 0; i < EMI_MPU_DGROUP_NUM; ++i)
  85. apc[i] = mmio_read_32(EMI_MPU_APC(region, i));
  86. sa = mmio_read_32(EMI_MPU_SA(region));
  87. ea = mmio_read_32(EMI_MPU_EA(region));
  88. INFO("region %d:\n", region);
  89. INFO("\tsa:0x%lx, ea:0x%lx, apc0: 0x%lx apc1: 0x%lx\n",
  90. sa, ea, apc[0], apc[1]);
  91. }
  92. }
  93. void emi_mpu_init(void)
  94. {
  95. struct emi_region_info_t region_info;
  96. /* SCP DRAM */
  97. region_info.start = 0x50000000ULL;
  98. region_info.end = 0x513FFFFFULL;
  99. region_info.region = 2;
  100. SET_ACCESS_PERMISSION(region_info.apc, 1,
  101. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  102. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  103. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  104. NO_PROTECTION, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  105. emi_mpu_set_protection(&region_info);
  106. /* DSP protect address */
  107. region_info.start = 0x60000000ULL; /* dram base addr */
  108. region_info.end = 0x610FFFFFULL;
  109. region_info.region = 3;
  110. SET_ACCESS_PERMISSION(region_info.apc, 1,
  111. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  112. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  113. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION,
  114. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  115. emi_mpu_set_protection(&region_info);
  116. /* Forbidden All */
  117. region_info.start = 0x40000000ULL; /* dram base addr */
  118. region_info.end = 0x1FFFF0000ULL;
  119. region_info.region = 5;
  120. SET_ACCESS_PERMISSION(region_info.apc, 1,
  121. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  122. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  123. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  124. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  125. emi_mpu_set_protection(&region_info);
  126. dump_emi_mpu_regions();
  127. }
  128. static inline uint64_t get_decoded_phys_addr(uint64_t addr)
  129. {
  130. return (addr << MPU_PHYSICAL_ADDR_SHIFT_BITS);
  131. }
  132. static inline uint32_t get_decoded_zone_id(uint32_t info)
  133. {
  134. return ((info & 0xFFFF0000) >> MPU_PHYSICAL_ADDR_SHIFT_BITS);
  135. }
  136. int32_t emi_mpu_sip_handler(uint64_t encoded_addr, uint64_t zone_size, uint64_t zone_info)
  137. {
  138. uint64_t phys_addr = get_decoded_phys_addr(encoded_addr);
  139. struct emi_region_info_t region_info;
  140. enum MPU_REQ_ORIGIN_ZONE_ID zone_id = get_decoded_zone_id(zone_info);
  141. INFO("encoded_addr = 0x%lx, zone_size = 0x%lx, zone_info = 0x%lx\n",
  142. encoded_addr, zone_size, zone_info);
  143. if (zone_id != MPU_REQ_ORIGIN_TEE_ZONE_SVP) {
  144. ERROR("Invalid param %s, %d\n", __func__, __LINE__);
  145. return MTK_SIP_E_INVALID_PARAM;
  146. }
  147. /* SVP DRAM */
  148. region_info.start = phys_addr;
  149. region_info.end = phys_addr + zone_size;
  150. region_info.region = 4;
  151. SET_ACCESS_PERMISSION(region_info.apc, 1,
  152. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  153. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  154. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  155. FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW);
  156. emi_mpu_set_protection(&region_info);
  157. return 0;
  158. }