mpu_smc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2020 MediaTek Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <pal_typedefs.h>
  27. #include <pal_log.h>
  28. #include <platform.h>
  29. #ifdef MTK_ENABLE_MPU_HAL_SUPPORT
  30. #include <mpu_smc.h>
  31. #endif
  32. #ifdef MTK_SMC_ID_MGMT
  33. #include <mtk_secure_api.h>
  34. #else
  35. extern unsigned long mt_secure_call(unsigned long, unsigned long, unsigned long,
  36. unsigned long);
  37. #endif
  38. #define MPU_DEBUG_ENABLE (0)
  39. #if (MPU_DEBUG_ENABLE)
  40. #define MPU_DBG(fmt, ...) pal_log_info("[MPU]" fmt, ##__VA_ARGS__)
  41. #else
  42. #define MPU_DBG(fmt, ...) \
  43. do { \
  44. } while (0)
  45. #endif
  46. #define MPU_ERR(fmt, ...) pal_log_err("[MPU]" fmt, ##__VA_ARGS__)
  47. #define MTK_SIP_LK_MPU_PERM_SET_AARCH32 0x82000128
  48. #define MTK_SIP_LK_MPU_PERM_SET_AARCH64 0xC2000128
  49. static uint32_t get_zone_info(uint32_t zone_id, uint32_t op)
  50. {
  51. uint32_t zone_info;
  52. zone_info = (op & 0x0000FFFF);
  53. zone_info |= ((zone_id & 0x0000FFFF) << 16);
  54. return zone_info;
  55. }
  56. /* Cut lower 16-bits for 64KB alignment.
  57. * So that we can use 32-bit variable to carry 48-bit physical address range.
  58. */
  59. #define MPU_PHYSICAL_ADDR_SHIFT_BITS (16)
  60. static inline uint32_t get_encoded_phys_addr(uint64_t addr)
  61. {
  62. return (addr >> MPU_PHYSICAL_ADDR_SHIFT_BITS);
  63. }
  64. static inline uint32_t is_valid_zone(uint32_t zone)
  65. {
  66. return (zone < MPU_REQ_ORIGIN_LK_ZONE_MAX);
  67. }
  68. #define SIZE_64K 0x00010000
  69. static inline uint32_t is_valid_addr(uint64_t addr)
  70. {
  71. if (addr == 0)
  72. return 0;
  73. if ((addr % SIZE_64K) != 0)
  74. return 0;
  75. return 1;
  76. }
  77. static inline uint32_t is_valid_size(uint32_t size)
  78. {
  79. if (size == 0)
  80. return 0;
  81. if (size < SIZE_64K)
  82. return 0;
  83. if ((size % SIZE_64K) != 0)
  84. return 0;
  85. return 1;
  86. }
  87. #define DRAM_START_ADDR (0x40000000ULL)
  88. #define MAX_PROTECT_SIZE (0x100000000ULL)
  89. static inline uint32_t is_valid_range(uint64_t start, uint64_t end)
  90. {
  91. if (start < DRAM_START_ADDR)
  92. return 0;
  93. if (end <= start)
  94. return 0;
  95. if ((end - start) >= MAX_PROTECT_SIZE)
  96. return 0;
  97. return 1;
  98. }
  99. uint32_t sip_smc_protect_zone_request(enum MPU_REQ_ORIGIN_ZONE_ID zone_id,
  100. uint64_t start_addr, uint64_t end_addr)
  101. {
  102. uint32_t zone_size = (uint32_t)((end_addr + 1) - start_addr);
  103. uint32_t zone_info, encoded_addr;
  104. uint32_t smc_ret = 0;
  105. uint32_t enable = 1;
  106. if (!is_valid_zone(zone_id)) {
  107. MPU_ERR("Invalid zone: %d\n", zone_id);
  108. return 1;
  109. }
  110. if (!is_valid_addr(start_addr) || !is_valid_size(zone_size)) {
  111. MPU_ERR("Invalid addr or size! 0x%llx - 0x%llx (0x%x)\n",
  112. start_addr, end_addr, zone_size);
  113. return 2;
  114. }
  115. if (!is_valid_range(start_addr, end_addr)) {
  116. MPU_ERR("Invalid range! 0x%llx - 0x%llx\n", start_addr,
  117. end_addr);
  118. return 3;
  119. }
  120. zone_info = get_zone_info(zone_id, enable);
  121. encoded_addr = get_encoded_phys_addr(start_addr);
  122. MPU_DBG("SMC args: 0x%x, 0x%x, 0x%x\n", encoded_addr, zone_size,
  123. zone_info);
  124. #ifdef MTK_SMC_ID_MGMT
  125. smc_ret = mt_secure_call(MTK_SIP_LK_MPU_PERM_SET_AARCH32, encoded_addr,
  126. zone_size, zone_info, 0);
  127. #else
  128. smc_ret = mt_secure_call(MTK_SIP_LK_MPU_PERM_SET_AARCH32, encoded_addr,
  129. zone_size, zone_info);
  130. #endif
  131. if (smc_ret) {
  132. MPU_ERR("SMC Set failed: 0x%x!\n", smc_ret);
  133. return 4;
  134. }
  135. MPU_DBG("%s:%d SMC passed!\n", __func__, __LINE__);
  136. return 0;
  137. }