emi_mpu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2020, 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 <lib/mmio.h>
  9. /*
  10. * emi_mpu_set_region_protection: protect a region.
  11. * @start: start address of the region
  12. * @end: end address of the region
  13. * @access_permission: EMI MPU access permission
  14. * Return 0 for success, otherwise negative status code.
  15. */
  16. static int _emi_mpu_set_protection(
  17. unsigned long start, unsigned long end,
  18. unsigned int apc)
  19. {
  20. unsigned int dgroup;
  21. unsigned int region;
  22. region = (start >> 24) & 0xFF;
  23. start &= 0x00FFFFFF;
  24. dgroup = (end >> 24) & 0xFF;
  25. end &= 0x00FFFFFF;
  26. if ((region >= EMI_MPU_REGION_NUM) || (dgroup > EMI_MPU_DGROUP_NUM)) {
  27. WARN("Region:%u or dgroup:%u is wrong!\n", region, dgroup);
  28. return -1;
  29. }
  30. apc &= 0x80FFFFFF;
  31. if ((start >= DRAM_OFFSET) && (end >= start)) {
  32. start -= DRAM_OFFSET;
  33. end -= DRAM_OFFSET;
  34. } else {
  35. WARN("start:0x%lx or end:0x%lx address is wrong!\n",
  36. start, end);
  37. return -2;
  38. }
  39. mmio_write_32(EMI_MPU_SA(region), start);
  40. mmio_write_32(EMI_MPU_EA(region), end);
  41. mmio_write_32(EMI_MPU_APC(region, dgroup), apc);
  42. return 0;
  43. }
  44. void dump_emi_mpu_regions(void)
  45. {
  46. unsigned long apc[EMI_MPU_DGROUP_NUM], sa, ea;
  47. int region, i;
  48. /* Only dump 8 regions(max: EMI_MPU_REGION_NUM --> 32) */
  49. for (region = 0; region < 8; ++region) {
  50. for (i = 0; i < EMI_MPU_DGROUP_NUM; ++i)
  51. apc[i] = mmio_read_32(EMI_MPU_APC(region, i));
  52. sa = mmio_read_32(EMI_MPU_SA(region));
  53. ea = mmio_read_32(EMI_MPU_EA(region));
  54. WARN("region %d:\n", region);
  55. WARN("\tsa:0x%lx, ea:0x%lx, apc0: 0x%lx apc1: 0x%lx\n",
  56. sa, ea, apc[0], apc[1]);
  57. }
  58. }
  59. int emi_mpu_set_protection(struct emi_region_info_t *region_info)
  60. {
  61. unsigned long start, end;
  62. int i;
  63. if (region_info->region >= EMI_MPU_REGION_NUM)
  64. return -1;
  65. start = (unsigned long)(region_info->start >> EMI_MPU_ALIGN_BITS) |
  66. (region_info->region << 24);
  67. for (i = EMI_MPU_DGROUP_NUM - 1; i >= 0; i--) {
  68. end = (unsigned long)(region_info->end >> EMI_MPU_ALIGN_BITS) |
  69. (i << 24);
  70. _emi_mpu_set_protection(start, end, region_info->apc[i]);
  71. }
  72. return 0;
  73. }
  74. void emi_mpu_init(void)
  75. {
  76. struct emi_region_info_t region_info;
  77. /* reserve region 0 for future use */
  78. /* PCI-e protect address(64MB) */
  79. region_info.start = 0xC0000000ULL;
  80. region_info.end = 0xC3FFFFFFULL;
  81. region_info.region = 1;
  82. SET_ACCESS_PERMISSION(region_info.apc, 1,
  83. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  84. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  85. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  86. FORBIDDEN, FORBIDDEN, NO_PROT, NO_PROT);
  87. emi_mpu_set_protection(&region_info);
  88. /* SCP protect address */
  89. region_info.start = 0x50000000ULL;
  90. region_info.end = 0x513FFFFFULL;
  91. region_info.region = 2;
  92. SET_ACCESS_PERMISSION(region_info.apc, 1,
  93. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  94. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  95. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  96. NO_PROT, FORBIDDEN, FORBIDDEN, NO_PROT);
  97. emi_mpu_set_protection(&region_info);
  98. /* Forbidden All */
  99. region_info.start = 0x40000000ULL; /* dram base addr */
  100. region_info.end = 0x1FFFFFFFFULL;
  101. region_info.region = 3;
  102. SET_ACCESS_PERMISSION(region_info.apc, 1,
  103. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  104. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  105. FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN,
  106. FORBIDDEN, FORBIDDEN, FORBIDDEN, NO_PROT);
  107. emi_mpu_set_protection(&region_info);
  108. dump_emi_mpu_regions();
  109. }