emi_mpu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2021-2022, 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. #if ENABLE_EMI_MPU_SW_LOCK
  11. static unsigned char region_lock_state[EMI_MPU_REGION_NUM];
  12. #endif
  13. #define EMI_MPU_START_MASK (0x00FFFFFF)
  14. #define EMI_MPU_END_MASK (0x00FFFFFF)
  15. #define EMI_MPU_APC_SW_LOCK_MASK (0x00FFFFFF)
  16. #define EMI_MPU_APC_HW_LOCK_MASK (0x80FFFFFF)
  17. static int _emi_mpu_set_protection(unsigned int start, unsigned int end,
  18. unsigned int apc)
  19. {
  20. unsigned int dgroup;
  21. unsigned int region;
  22. region = (start >> 24) & 0xFF;
  23. start &= EMI_MPU_START_MASK;
  24. dgroup = (end >> 24) & 0xFF;
  25. end &= EMI_MPU_END_MASK;
  26. if ((region >= EMI_MPU_REGION_NUM) || (dgroup > EMI_MPU_DGROUP_NUM)) {
  27. WARN("invalid region, domain\n");
  28. return -1;
  29. }
  30. #if ENABLE_EMI_MPU_SW_LOCK
  31. if (region_lock_state[region] == 1) {
  32. WARN("invalid region\n");
  33. return -1;
  34. }
  35. if ((dgroup == 0U) && ((apc >> 31) & 0x1)) {
  36. region_lock_state[region] = 1;
  37. }
  38. apc &= EMI_MPU_APC_SW_LOCK_MASK;
  39. #else
  40. apc &= EMI_MPU_APC_HW_LOCK_MASK;
  41. #endif
  42. if ((start >= DRAM_OFFSET) && (end >= start)) {
  43. start -= DRAM_OFFSET;
  44. end -= DRAM_OFFSET;
  45. } else {
  46. WARN("invalid range\n");
  47. return -1;
  48. }
  49. mmio_write_32(EMI_MPU_SA(region), start);
  50. mmio_write_32(EMI_MPU_EA(region), end);
  51. mmio_write_32(EMI_MPU_APC(region, dgroup), apc);
  52. #if defined(SUB_EMI_MPU_BASE)
  53. mmio_write_32(SUB_EMI_MPU_SA(region), start);
  54. mmio_write_32(SUB_EMI_MPU_EA(region), end);
  55. mmio_write_32(SUB_EMI_MPU_APC(region, dgroup), apc);
  56. #endif
  57. return 0;
  58. }
  59. int emi_mpu_set_protection(struct emi_region_info_t *region_info)
  60. {
  61. unsigned int start, end;
  62. int i;
  63. if (region_info->region >= EMI_MPU_REGION_NUM) {
  64. WARN("invalid region\n");
  65. return -1;
  66. }
  67. start = (unsigned int)(region_info->start >> EMI_MPU_ALIGN_BITS) |
  68. (region_info->region << 24);
  69. for (i = EMI_MPU_DGROUP_NUM - 1; i >= 0; i--) {
  70. end = (unsigned int)(region_info->end >> EMI_MPU_ALIGN_BITS) | (i << 24);
  71. if (_emi_mpu_set_protection(start, end, region_info->apc[i]) < 0) {
  72. WARN("failed to set emi mpu protection(%d, %d, %d)\n",
  73. start, end, region_info->apc[i]);
  74. }
  75. }
  76. return 0;
  77. }
  78. void emi_mpu_init(void)
  79. {
  80. struct emi_region_info_t region_info;
  81. /* SCP DRAM */
  82. region_info.start = 0x50000000ULL;
  83. region_info.end = 0x5109FFFFULL;
  84. region_info.region = 2;
  85. SET_ACCESS_PERMISSION(region_info.apc, 1,
  86. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  87. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  88. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION,
  89. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  90. emi_mpu_set_protection(&region_info);
  91. /* DSP protect address */
  92. region_info.start = 0x60000000ULL; /* dram base addr */
  93. region_info.end = 0x610FFFFFULL;
  94. region_info.region = 3;
  95. SET_ACCESS_PERMISSION(region_info.apc, 1,
  96. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  97. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  98. FORBIDDEN, NO_PROTECTION, FORBIDDEN, FORBIDDEN,
  99. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  100. emi_mpu_set_protection(&region_info);
  101. /* Forbidden All */
  102. region_info.start = 0x40000000ULL; /* dram base addr */
  103. region_info.end = 0x1FFFF0000ULL;
  104. region_info.region = 4;
  105. SET_ACCESS_PERMISSION(region_info.apc, 1,
  106. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  107. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  108. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  109. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROTECTION);
  110. emi_mpu_set_protection(&region_info);
  111. }