emi_mpu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2022-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <emi_mpu.h>
  8. #include <mtk_sip_svc.h>
  9. #define MPU_PHYSICAL_ADDR_SHIFT_BITS (16)
  10. void set_emi_mpu_regions(void)
  11. {
  12. struct emi_region_info_t region_info;
  13. /* BL31 address */
  14. region_info.start = TZRAM_BASE;
  15. region_info.end = TZRAM_BASE + TZRAM_SIZE - 1;
  16. region_info.region = BL31_EMI_REGION_ID;
  17. SET_ACCESS_PERMISSION(region_info.apc, LOCK,
  18. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  19. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  20. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  21. FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW);
  22. emi_mpu_set_protection(&region_info);
  23. #ifndef SPD_NONE
  24. /* BL32 address */
  25. region_info.start = BL32_REGION_BASE;
  26. region_info.end = BL32_REGION_BASE + BL32_REGION_SIZE - 1;
  27. region_info.region = BL32_REGION_ID;
  28. SET_ACCESS_PERMISSION(region_info.apc, LOCK,
  29. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  30. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  31. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  32. FORBIDDEN, FORBIDDEN, SEC_RW, SEC_RW);
  33. emi_mpu_set_protection(&region_info);
  34. #endif
  35. /* SCP core0 DRAM */
  36. region_info.start = SCP_CORE0_REGION_BASE;
  37. region_info.end = SCP_CORE0_REGION_BASE + SCP_CORE0_REGION_SIZE - 1;
  38. region_info.region = SCP_CORE0_REGION_ID;
  39. SET_ACCESS_PERMISSION(region_info.apc, LOCK,
  40. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  41. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION,
  42. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  43. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  44. emi_mpu_set_protection(&region_info);
  45. /* SCP core1 DRAM */
  46. region_info.start = SCP_CORE1_REGION_BASE;
  47. region_info.end = SCP_CORE1_REGION_BASE + SCP_CORE1_REGION_SIZE - 1;
  48. region_info.region = SCP_CORE1_REGION_ID;
  49. SET_ACCESS_PERMISSION(region_info.apc, LOCK,
  50. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  51. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION,
  52. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  53. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  54. emi_mpu_set_protection(&region_info);
  55. /* DSP protect address */
  56. region_info.start = DSP_PROTECT_REGION_BASE;
  57. region_info.end = DSP_PROTECT_REGION_BASE + DSP_PROTECT_REGION_SIZE - 1;
  58. region_info.region = DSP_PROTECT_REGION_ID;
  59. SET_ACCESS_PERMISSION(region_info.apc, LOCK,
  60. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  61. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  62. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION,
  63. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  64. emi_mpu_set_protection(&region_info);
  65. /* All default settings */
  66. region_info.start = DRAM_START_ADDR;
  67. region_info.end = DRAM_START_ADDR + DRAM_MAX_SIZE - 1;
  68. region_info.region = ALL_DEFAULT_REGION_ID;
  69. SET_ACCESS_PERMISSION(region_info.apc, LOCK,
  70. FORBIDDEN, FORBIDDEN, NO_PROTECTION, NO_PROTECTION,
  71. NO_PROTECTION, FORBIDDEN, NO_PROTECTION, NO_PROTECTION,
  72. NO_PROTECTION, SEC_R_NSEC_RW, NO_PROTECTION, FORBIDDEN,
  73. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION);
  74. emi_mpu_set_protection(&region_info);
  75. }
  76. int set_apu_emi_mpu_region(void)
  77. {
  78. struct emi_region_info_t region_info;
  79. region_info.start = (unsigned long long)APUSYS_SEC_BUF_PA;
  80. region_info.end = (unsigned long long)(APUSYS_SEC_BUF_PA + APUSYS_SEC_BUF_SZ) - 1;
  81. region_info.region = APUSYS_SEC_BUF_EMI_REGION_ID;
  82. SET_ACCESS_PERMISSION(region_info.apc, UNLOCK,
  83. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  84. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  85. NO_PROTECTION, FORBIDDEN, NO_PROTECTION, FORBIDDEN,
  86. FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW);
  87. return emi_mpu_set_protection(&region_info);
  88. }
  89. static inline uint64_t get_decoded_phys_addr(uint64_t addr)
  90. {
  91. return (addr << MPU_PHYSICAL_ADDR_SHIFT_BITS);
  92. }
  93. static inline uint32_t get_decoded_zone_id(uint32_t info)
  94. {
  95. return ((info & 0xFFFF0000) >> MPU_PHYSICAL_ADDR_SHIFT_BITS);
  96. }
  97. static inline uint32_t get_decoded_set_clear_info(uint32_t info)
  98. {
  99. return (info & 0x0000FFFF);
  100. }
  101. int emi_mpu_optee_handler(uint64_t encoded_addr, uint64_t zone_size,
  102. uint64_t zone_info)
  103. {
  104. uint64_t phys_addr = get_decoded_phys_addr(encoded_addr);
  105. struct emi_region_info_t region_info;
  106. enum region_ids zone_id = get_decoded_zone_id(zone_info);
  107. uint32_t is_set = get_decoded_set_clear_info(zone_info);
  108. INFO("encoded_addr = 0x%lx, zone_size = 0x%lx, zone_info = 0x%lx\n",
  109. encoded_addr, zone_size, zone_info);
  110. if (zone_id < SVP_DRAM_REGION_ID_START || zone_id > SVP_DRAM_REGION_ID_END) {
  111. ERROR("Invalid param %s, %d\n", __func__, __LINE__);
  112. return MTK_SIP_E_INVALID_PARAM;
  113. }
  114. if (is_set > 0) {
  115. /* SVP DRAM */
  116. region_info.start = phys_addr;
  117. region_info.end = phys_addr + zone_size - 1;
  118. region_info.region = zone_id;
  119. SET_ACCESS_PERMISSION(region_info.apc, UNLOCK,
  120. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  121. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  122. FORBIDDEN, SEC_RW, FORBIDDEN, FORBIDDEN,
  123. FORBIDDEN, FORBIDDEN, SEC_RW, SEC_RW);
  124. emi_mpu_set_protection(&region_info);
  125. } else { /* clear region protection */
  126. emi_mpu_clear_protection(zone_id);
  127. }
  128. return 0;
  129. }