memctrl_v2.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2019-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/bl_common.h>
  11. #include <common/debug.h>
  12. #include <lib/mmio.h>
  13. #include <lib/utils.h>
  14. #include <lib/xlat_tables/xlat_tables_v2.h>
  15. #include <mce.h>
  16. #include <memctrl.h>
  17. #include <memctrl_v2.h>
  18. #include <smmu.h>
  19. #include <tegra_def.h>
  20. #include <tegra_platform.h>
  21. #include <tegra_private.h>
  22. /* Video Memory base and size (live values) */
  23. static uint64_t video_mem_base;
  24. static uint64_t video_mem_size_mb;
  25. /*
  26. * Init Memory controller during boot.
  27. */
  28. void tegra_memctrl_setup(void)
  29. {
  30. INFO("Tegra Memory Controller (v2)\n");
  31. /* Initialize the System memory management unit */
  32. tegra_smmu_init();
  33. /* allow platforms to program custom memory controller settings */
  34. plat_memctrl_setup();
  35. /*
  36. * All requests at boot time, and certain requests during
  37. * normal run time, are physically addressed and must bypass
  38. * the SMMU. The client hub logic implements a hardware bypass
  39. * path around the Translation Buffer Units (TBU). During
  40. * boot-time, the SMMU_BYPASS_CTRL register (which defaults to
  41. * TBU_BYPASS mode) will be used to steer all requests around
  42. * the uninitialized TBUs. During normal operation, this register
  43. * is locked into TBU_BYPASS_SID config, which routes requests
  44. * with special StreamID 0x7f on the bypass path and all others
  45. * through the selected TBU. This is done to disable SMMU Bypass
  46. * mode, as it could be used to circumvent SMMU security checks.
  47. */
  48. tegra_mc_write_32(MC_SMMU_BYPASS_CONFIG,
  49. MC_SMMU_BYPASS_CONFIG_SETTINGS);
  50. }
  51. /*
  52. * Restore Memory Controller settings after "System Suspend"
  53. */
  54. void tegra_memctrl_restore_settings(void)
  55. {
  56. /* restore platform's memory controller settings */
  57. plat_memctrl_restore();
  58. /* video memory carveout region */
  59. if (video_mem_base != 0ULL) {
  60. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO,
  61. (uint32_t)video_mem_base);
  62. assert(tegra_mc_read_32(MC_VIDEO_PROTECT_BASE_LO)
  63. == (uint32_t)video_mem_base);
  64. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
  65. (uint32_t)(video_mem_base >> 32));
  66. assert(tegra_mc_read_32(MC_VIDEO_PROTECT_BASE_HI)
  67. == (uint32_t)(video_mem_base >> 32));
  68. tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB,
  69. (uint32_t)video_mem_size_mb);
  70. assert(tegra_mc_read_32(MC_VIDEO_PROTECT_SIZE_MB)
  71. == (uint32_t)video_mem_size_mb);
  72. /*
  73. * MCE propagates the VideoMem configuration values across the
  74. * CCPLEX.
  75. */
  76. mce_update_gsc_videomem();
  77. }
  78. }
  79. /*
  80. * Secure the BL31 DRAM aperture.
  81. *
  82. * phys_base = physical base of TZDRAM aperture
  83. * size_in_bytes = size of aperture in bytes
  84. */
  85. void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes)
  86. {
  87. /*
  88. * Perform platform specific steps.
  89. */
  90. plat_memctrl_tzdram_setup(phys_base, size_in_bytes);
  91. }
  92. /*
  93. * Secure the BL31 TZRAM aperture.
  94. *
  95. * phys_base = physical base of TZRAM aperture
  96. * size_in_bytes = size of aperture in bytes
  97. */
  98. void tegra_memctrl_tzram_setup(uint64_t phys_base, uint32_t size_in_bytes)
  99. {
  100. ; /* do nothing */
  101. }
  102. /*
  103. * Save MC settings before "System Suspend" to TZDRAM
  104. */
  105. void tegra_mc_save_context(uint64_t mc_ctx_addr)
  106. {
  107. uint32_t i, num_entries = 0;
  108. mc_regs_t *mc_ctx_regs;
  109. const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
  110. uint64_t tzdram_base = params_from_bl2->tzdram_base;
  111. uint64_t tzdram_end = tzdram_base + params_from_bl2->tzdram_size;
  112. assert((mc_ctx_addr >= tzdram_base) && (mc_ctx_addr <= tzdram_end));
  113. /* get MC context table */
  114. mc_ctx_regs = plat_memctrl_get_sys_suspend_ctx();
  115. assert(mc_ctx_regs != NULL);
  116. /*
  117. * mc_ctx_regs[0].val contains the size of the context table minus
  118. * the last entry. Sanity check the table size before we start with
  119. * the context save operation.
  120. */
  121. while (mc_ctx_regs[num_entries].reg != 0xFFFFFFFFU) {
  122. num_entries++;
  123. }
  124. /* panic if the sizes do not match */
  125. if (num_entries != mc_ctx_regs[0].val) {
  126. ERROR("MC context size mismatch!");
  127. panic();
  128. }
  129. /* save MC register values */
  130. for (i = 1U; i < num_entries; i++) {
  131. mc_ctx_regs[i].val = mmio_read_32(mc_ctx_regs[i].reg);
  132. }
  133. /* increment by 1 to take care of the last entry */
  134. num_entries++;
  135. /* Save MC config settings */
  136. (void)memcpy((void *)mc_ctx_addr, mc_ctx_regs,
  137. sizeof(mc_regs_t) * num_entries);
  138. /* save the MC table address */
  139. mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_MC_TABLE_ADDR_LO,
  140. (uint32_t)mc_ctx_addr);
  141. assert(mmio_read_32(TEGRA_SCRATCH_BASE + SCRATCH_MC_TABLE_ADDR_LO)
  142. == (uint32_t)mc_ctx_addr);
  143. mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_MC_TABLE_ADDR_HI,
  144. (uint32_t)(mc_ctx_addr >> 32));
  145. assert(mmio_read_32(TEGRA_SCRATCH_BASE + SCRATCH_MC_TABLE_ADDR_HI)
  146. == (uint32_t)(mc_ctx_addr >> 32));
  147. }
  148. static void tegra_lock_videomem_nonoverlap(uint64_t phys_base,
  149. uint64_t size_in_bytes)
  150. {
  151. uint32_t index;
  152. uint64_t total_128kb_blocks = size_in_bytes >> 17;
  153. uint64_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12;
  154. uint64_t val;
  155. /*
  156. * Reset the access configuration registers to restrict access to
  157. * old Videomem aperture
  158. */
  159. for (index = MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0;
  160. index < ((uint32_t)MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0 + (uint32_t)MC_GSC_CONFIG_REGS_SIZE);
  161. index += 4U) {
  162. tegra_mc_write_32(index, 0);
  163. }
  164. /*
  165. * Set the base. It must be 4k aligned, at least.
  166. */
  167. assert((phys_base & (uint64_t)0xFFF) == 0U);
  168. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, (uint32_t)phys_base);
  169. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI,
  170. (uint32_t)(phys_base >> 32) & (uint32_t)MC_GSC_BASE_HI_MASK);
  171. /*
  172. * Set the aperture size
  173. *
  174. * total size = (number of 128KB blocks) + (number of remaining 4KB
  175. * blocks)
  176. *
  177. */
  178. val = (uint32_t)((residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) |
  179. total_128kb_blocks);
  180. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, (uint32_t)val);
  181. /*
  182. * Lock the configuration settings by enabling TZ-only lock and
  183. * locking the configuration against any future changes from NS
  184. * world.
  185. */
  186. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_CFG,
  187. (uint32_t)MC_GSC_ENABLE_TZ_LOCK_BIT);
  188. /*
  189. * MCE propagates the GSC configuration values across the
  190. * CCPLEX.
  191. */
  192. }
  193. static void tegra_unlock_videomem_nonoverlap(void)
  194. {
  195. /* Clear the base */
  196. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, 0);
  197. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI, 0);
  198. /* Clear the size */
  199. tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, 0);
  200. }
  201. static void tegra_clear_videomem(uintptr_t non_overlap_area_start,
  202. unsigned long long non_overlap_area_size)
  203. {
  204. int ret;
  205. INFO("Cleaning previous Video Memory Carveout\n");
  206. /*
  207. * Map the NS memory first, clean it and then unmap it.
  208. */
  209. ret = mmap_add_dynamic_region(non_overlap_area_start, /* PA */
  210. non_overlap_area_start, /* VA */
  211. non_overlap_area_size, /* size */
  212. MT_DEVICE | MT_RW | MT_NS); /* attrs */
  213. assert(ret == 0);
  214. zeromem((void *)non_overlap_area_start, non_overlap_area_size);
  215. flush_dcache_range(non_overlap_area_start, non_overlap_area_size);
  216. ret = mmap_remove_dynamic_region(non_overlap_area_start,
  217. non_overlap_area_size);
  218. assert(ret == 0);
  219. }
  220. static void tegra_clear_videomem_nonoverlap(uintptr_t phys_base,
  221. unsigned long size_in_bytes)
  222. {
  223. uintptr_t vmem_end_old = video_mem_base + (video_mem_size_mb << 20);
  224. uintptr_t vmem_end_new = phys_base + size_in_bytes;
  225. unsigned long long non_overlap_area_size;
  226. /*
  227. * Clear the old regions now being exposed. The following cases
  228. * can occur -
  229. *
  230. * 1. clear whole old region (no overlap with new region)
  231. * 2. clear old sub-region below new base
  232. * 3. clear old sub-region above new end
  233. */
  234. if ((phys_base > vmem_end_old) || (video_mem_base > vmem_end_new)) {
  235. tegra_clear_videomem(video_mem_base,
  236. video_mem_size_mb << 20U);
  237. } else {
  238. if (video_mem_base < phys_base) {
  239. non_overlap_area_size = phys_base - video_mem_base;
  240. tegra_clear_videomem(video_mem_base, non_overlap_area_size);
  241. }
  242. if (vmem_end_old > vmem_end_new) {
  243. non_overlap_area_size = vmem_end_old - vmem_end_new;
  244. tegra_clear_videomem(vmem_end_new, non_overlap_area_size);
  245. }
  246. }
  247. }
  248. /*
  249. * Program the Video Memory carveout region
  250. *
  251. * phys_base = physical base of aperture
  252. * size_in_bytes = size of aperture in bytes
  253. */
  254. void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes)
  255. {
  256. /*
  257. * Setup the Memory controller to restrict CPU accesses to the Video
  258. * Memory region
  259. */
  260. INFO("Configuring Video Memory Carveout\n");
  261. if (video_mem_base != 0U) {
  262. /*
  263. * Lock the non overlapping memory being cleared so that
  264. * other masters do not accidentally write to it. The memory
  265. * would be unlocked once the non overlapping region is
  266. * cleared and the new memory settings take effect.
  267. */
  268. tegra_lock_videomem_nonoverlap(video_mem_base,
  269. video_mem_size_mb << 20);
  270. }
  271. /* program the Videomem aperture */
  272. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, (uint32_t)phys_base);
  273. tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
  274. (uint32_t)(phys_base >> 32));
  275. tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20);
  276. /* Redundancy check for Video Protect setting */
  277. assert(tegra_mc_read_32(MC_VIDEO_PROTECT_BASE_LO)
  278. == (uint32_t)phys_base);
  279. assert(tegra_mc_read_32(MC_VIDEO_PROTECT_BASE_HI)
  280. == (uint32_t)(phys_base >> 32));
  281. assert(tegra_mc_read_32(MC_VIDEO_PROTECT_SIZE_MB)
  282. == (size_in_bytes >> 20));
  283. /*
  284. * MCE propagates the VideoMem configuration values across the
  285. * CCPLEX.
  286. */
  287. (void)mce_update_gsc_videomem();
  288. /* Clear the non-overlapping memory */
  289. if (video_mem_base != 0U) {
  290. tegra_clear_videomem_nonoverlap(phys_base, size_in_bytes);
  291. tegra_unlock_videomem_nonoverlap();
  292. }
  293. /* store new values */
  294. video_mem_base = phys_base;
  295. video_mem_size_mb = (uint64_t)size_in_bytes >> 20;
  296. }
  297. /*
  298. * This feature exists only for v1 of the Tegra Memory Controller.
  299. */
  300. void tegra_memctrl_disable_ahb_redirection(void)
  301. {
  302. ; /* do nothing */
  303. }
  304. void tegra_memctrl_clear_pending_interrupts(void)
  305. {
  306. ; /* do nothing */
  307. }