memctrl_v1.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <lib/mmio.h>
  12. #include <lib/utils.h>
  13. #include <lib/xlat_tables/xlat_tables_v2.h>
  14. #include <memctrl.h>
  15. #include <memctrl_v1.h>
  16. #include <tegra_def.h>
  17. /* Video Memory base and size (live values) */
  18. static uint64_t video_mem_base;
  19. static uint64_t video_mem_size;
  20. /*
  21. * Init SMMU.
  22. */
  23. void tegra_memctrl_setup(void)
  24. {
  25. /*
  26. * Setup the Memory controller to allow only secure accesses to
  27. * the TZDRAM carveout
  28. */
  29. INFO("Tegra Memory Controller (v1)\n");
  30. /* allow translations for all MC engines */
  31. tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_0_0,
  32. (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
  33. tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_1_0,
  34. (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
  35. tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_2_0,
  36. (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
  37. tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_3_0,
  38. (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
  39. tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_4_0,
  40. (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
  41. tegra_mc_write_32(MC_SMMU_ASID_SECURITY_0, MC_SMMU_ASID_SECURITY);
  42. tegra_mc_write_32(MC_SMMU_TLB_CONFIG_0, MC_SMMU_TLB_CONFIG_0_RESET_VAL);
  43. tegra_mc_write_32(MC_SMMU_PTC_CONFIG_0, MC_SMMU_PTC_CONFIG_0_RESET_VAL);
  44. /* flush PTC and TLB */
  45. tegra_mc_write_32(MC_SMMU_PTC_FLUSH_0, MC_SMMU_PTC_FLUSH_ALL);
  46. (void)tegra_mc_read_32(MC_SMMU_CONFIG_0); /* read to flush writes */
  47. tegra_mc_write_32(MC_SMMU_TLB_FLUSH_0, MC_SMMU_TLB_FLUSH_ALL);
  48. /* enable SMMU */
  49. tegra_mc_write_32(MC_SMMU_CONFIG_0,
  50. MC_SMMU_CONFIG_0_SMMU_ENABLE_ENABLE);
  51. (void)tegra_mc_read_32(MC_SMMU_CONFIG_0); /* read to flush writes */
  52. /* video memory carveout */
  53. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
  54. (uint32_t)(video_mem_base >> 32));
  55. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, (uint32_t)video_mem_base);
  56. tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, video_mem_size);
  57. }
  58. /*
  59. * Restore Memory Controller settings after "System Suspend"
  60. */
  61. void tegra_memctrl_restore_settings(void)
  62. {
  63. tegra_memctrl_setup();
  64. }
  65. /*
  66. * Secure the BL31 DRAM aperture.
  67. *
  68. * phys_base = physical base of TZDRAM aperture
  69. * size_in_bytes = size of aperture in bytes
  70. */
  71. void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes)
  72. {
  73. /*
  74. * Setup the Memory controller to allow only secure accesses to
  75. * the TZDRAM carveout
  76. */
  77. INFO("Configuring TrustZone DRAM Memory Carveout\n");
  78. tegra_mc_write_32(MC_SECURITY_CFG0_0, phys_base);
  79. tegra_mc_write_32(MC_SECURITY_CFG1_0, size_in_bytes >> 20);
  80. }
  81. static void tegra_clear_videomem(uintptr_t non_overlap_area_start,
  82. unsigned long long non_overlap_area_size)
  83. {
  84. int ret;
  85. /*
  86. * Map the NS memory first, clean it and then unmap it.
  87. */
  88. ret = mmap_add_dynamic_region(non_overlap_area_start, /* PA */
  89. non_overlap_area_start, /* VA */
  90. non_overlap_area_size, /* size */
  91. MT_NS | MT_RW | MT_EXECUTE_NEVER |
  92. MT_NON_CACHEABLE); /* attrs */
  93. assert(ret == 0);
  94. zeromem((void *)non_overlap_area_start, non_overlap_area_size);
  95. flush_dcache_range(non_overlap_area_start, non_overlap_area_size);
  96. mmap_remove_dynamic_region(non_overlap_area_start,
  97. non_overlap_area_size);
  98. }
  99. /*
  100. * Program the Video Memory carveout region
  101. *
  102. * phys_base = physical base of aperture
  103. * size_in_bytes = size of aperture in bytes
  104. */
  105. void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes)
  106. {
  107. uintptr_t vmem_end_old = video_mem_base + (video_mem_size << 20);
  108. uintptr_t vmem_end_new = phys_base + size_in_bytes;
  109. unsigned long long non_overlap_area_size;
  110. /*
  111. * Setup the Memory controller to restrict CPU accesses to the Video
  112. * Memory region
  113. */
  114. INFO("Configuring Video Memory Carveout\n");
  115. /*
  116. * Configure Memory Controller directly for the first time.
  117. */
  118. if (video_mem_base == 0)
  119. goto done;
  120. /*
  121. * Clear the old regions now being exposed. The following cases
  122. * can occur -
  123. *
  124. * 1. clear whole old region (no overlap with new region)
  125. * 2. clear old sub-region below new base
  126. * 3. clear old sub-region above new end
  127. */
  128. INFO("Cleaning previous Video Memory Carveout\n");
  129. if (phys_base > vmem_end_old || video_mem_base > vmem_end_new) {
  130. tegra_clear_videomem(video_mem_base, video_mem_size << 20);
  131. } else {
  132. if (video_mem_base < phys_base) {
  133. non_overlap_area_size = phys_base - video_mem_base;
  134. tegra_clear_videomem(video_mem_base, non_overlap_area_size);
  135. }
  136. if (vmem_end_old > vmem_end_new) {
  137. non_overlap_area_size = vmem_end_old - vmem_end_new;
  138. tegra_clear_videomem(vmem_end_new, non_overlap_area_size);
  139. }
  140. }
  141. done:
  142. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI, (uint32_t)(phys_base >> 32));
  143. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, (uint32_t)phys_base);
  144. tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20);
  145. /* store new values */
  146. video_mem_base = phys_base;
  147. video_mem_size = size_in_bytes >> 20;
  148. }
  149. /*
  150. * During boot, USB3 and flash media (SDMMC/SATA) devices need access to
  151. * IRAM. Because these clients connect to the MC and do not have a direct
  152. * path to the IRAM, the MC implements AHB redirection during boot to allow
  153. * path to IRAM. In this mode, accesses to a programmed memory address aperture
  154. * are directed to the AHB bus, allowing access to the IRAM. The AHB aperture
  155. * is defined by the IRAM_BASE_LO and IRAM_BASE_HI registers, which are
  156. * initialized to disable this aperture.
  157. *
  158. * Once bootup is complete, we must program IRAM base to 0xffffffff and
  159. * IRAM top to 0x00000000, thus disabling access to IRAM. DRAM is then
  160. * potentially accessible in this address range. These aperture registers
  161. * also have an access_control/lock bit. After disabling the aperture, the
  162. * access_control register should be programmed to lock the registers.
  163. */
  164. void tegra_memctrl_disable_ahb_redirection(void)
  165. {
  166. /* program the aperture registers */
  167. tegra_mc_write_32(MC_IRAM_BASE_LO, 0xFFFFFFFF);
  168. tegra_mc_write_32(MC_IRAM_TOP_LO, 0);
  169. tegra_mc_write_32(MC_IRAM_BASE_TOP_HI, 0);
  170. /* lock the aperture registers */
  171. tegra_mc_write_32(MC_IRAM_REG_CTRL, MC_DISABLE_IRAM_CFG_WRITES);
  172. }
  173. void tegra_memctrl_clear_pending_interrupts(void)
  174. {
  175. uint32_t mcerr;
  176. /* check if there are any pending interrupts */
  177. mcerr = mmio_read_32(TEGRA_MC_BASE + MC_INTSTATUS);
  178. if (mcerr != (uint32_t)0U) { /* should not see error here */
  179. WARN("MC_INTSTATUS = 0x%x (should be zero)\n", mcerr);
  180. mmio_write_32((TEGRA_MC_BASE + MC_INTSTATUS), mcerr);
  181. }
  182. }