blob: c2ef981eed9baeb720a05a1efe5274e1c887a3b2 [file] [log] [blame]
Varun Wadekarcd5a2f52015-09-20 15:08:22 +05301/*
Harvey Hsiehb9b374f2016-11-15 22:04:51 +08002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekard292e5d2018-05-17 10:42:18 -07003 * Copyright (c) 2019-2020, NVIDIA Corporation. All rights reserved.
Varun Wadekarcd5a2f52015-09-20 15:08:22 +05304 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekarcd5a2f52015-09-20 15:08:22 +05306 */
7
Varun Wadekarcd5a2f52015-09-20 15:08:22 +05308#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <string.h>
10
11#include <arch_helpers.h>
12#include <common/bl_common.h>
13#include <common/debug.h>
14#include <lib/mmio.h>
15#include <lib/utils.h>
16#include <lib/xlat_tables/xlat_tables_v2.h>
17
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053018#include <mce.h>
19#include <memctrl.h>
20#include <memctrl_v2.h>
Varun Wadekar87e44ff2016-03-03 13:22:39 -080021#include <smmu.h>
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053022#include <tegra_def.h>
Varun Wadekare81177d2016-07-18 17:43:41 -070023#include <tegra_platform.h>
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053024
25/* Video Memory base and size (live values) */
26static uint64_t video_mem_base;
Varun Wadekar7058aee2016-04-25 09:01:46 -070027static uint64_t video_mem_size_mb;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053028
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053029/*
Varun Wadekar87e44ff2016-03-03 13:22:39 -080030 * Init Memory controller during boot.
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053031 */
32void tegra_memctrl_setup(void)
33{
34 uint32_t val;
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +053035 const uint32_t *mc_streamid_override_regs;
36 uint32_t num_streamid_override_regs;
37 const mc_streamid_security_cfg_t *mc_streamid_sec_cfgs;
38 uint32_t num_streamid_sec_cfgs;
Anthony Zhou0844b972017-06-28 16:35:54 +080039 const tegra_mc_settings_t *plat_mc_settings = tegra_get_mc_settings();
Varun Wadekarad45ef72017-04-03 13:44:57 -070040 uint32_t i;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053041
42 INFO("Tegra Memory Controller (v2)\n");
43
44 /* Program the SMMU pagesize */
Varun Wadekar87e44ff2016-03-03 13:22:39 -080045 tegra_smmu_init();
Varun Wadekarcba05292017-11-29 17:14:24 -080046
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +053047 /* Get the settings from the platform */
Anthony Zhou4408e882017-07-07 14:29:51 +080048 assert(plat_mc_settings != NULL);
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +053049 mc_streamid_override_regs = plat_mc_settings->streamid_override_cfg;
50 num_streamid_override_regs = plat_mc_settings->num_streamid_override_cfgs;
51 mc_streamid_sec_cfgs = plat_mc_settings->streamid_security_cfg;
52 num_streamid_sec_cfgs = plat_mc_settings->num_streamid_security_cfgs;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053053
54 /* Program all the Stream ID overrides */
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +053055 for (i = 0; i < num_streamid_override_regs; i++)
56 tegra_mc_streamid_write_32(mc_streamid_override_regs[i],
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053057 MC_STREAM_ID_MAX);
58
59 /* Program the security config settings for all Stream IDs */
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +053060 for (i = 0; i < num_streamid_sec_cfgs; i++) {
61 val = mc_streamid_sec_cfgs[i].override_enable << 16 |
62 mc_streamid_sec_cfgs[i].override_client_inputs << 8 |
63 mc_streamid_sec_cfgs[i].override_client_ns_flag << 0;
64 tegra_mc_streamid_write_32(mc_streamid_sec_cfgs[i].offset, val);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053065 }
66
67 /*
68 * All requests at boot time, and certain requests during
69 * normal run time, are physically addressed and must bypass
70 * the SMMU. The client hub logic implements a hardware bypass
71 * path around the Translation Buffer Units (TBU). During
72 * boot-time, the SMMU_BYPASS_CTRL register (which defaults to
73 * TBU_BYPASS mode) will be used to steer all requests around
74 * the uninitialized TBUs. During normal operation, this register
75 * is locked into TBU_BYPASS_SID config, which routes requests
76 * with special StreamID 0x7f on the bypass path and all others
77 * through the selected TBU. This is done to disable SMMU Bypass
78 * mode, as it could be used to circumvent SMMU security checks.
79 */
80 tegra_mc_write_32(MC_SMMU_BYPASS_CONFIG,
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +053081 MC_SMMU_BYPASS_CONFIG_SETTINGS);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053082
Varun Wadekarc9ac3e42016-02-17 15:07:49 -080083 /*
Varun Wadekara0f26972016-03-11 17:18:51 -080084 * Re-configure MSS to allow ROC to deal with ordering of the
85 * Memory Controller traffic. This is needed as the Memory Controller
86 * boots with MSS having all control, but ROC provides a performance
87 * boost as compared to MSS.
88 */
Puneet Saxenacf8c0e22017-08-04 17:19:55 +053089 if (plat_mc_settings->reconfig_mss_clients != NULL) {
90 plat_mc_settings->reconfig_mss_clients();
91 }
Varun Wadekara0f26972016-03-11 17:18:51 -080092
Varun Wadekarad45ef72017-04-03 13:44:57 -070093 /* Program overrides for MC transactions */
Puneet Saxenacf8c0e22017-08-04 17:19:55 +053094 if (plat_mc_settings->set_txn_overrides != NULL) {
95 plat_mc_settings->set_txn_overrides();
96 }
Varun Wadekar87e44ff2016-03-03 13:22:39 -080097}
Varun Wadekarc9ac3e42016-02-17 15:07:49 -080098
Varun Wadekar87e44ff2016-03-03 13:22:39 -080099/*
100 * Restore Memory Controller settings after "System Suspend"
101 */
102void tegra_memctrl_restore_settings(void)
103{
Puneet Saxenacf8c0e22017-08-04 17:19:55 +0530104 const tegra_mc_settings_t *plat_mc_settings = tegra_get_mc_settings();
105
106 assert(plat_mc_settings != NULL);
107
Varun Wadekara0f26972016-03-11 17:18:51 -0800108 /*
109 * Re-configure MSS to allow ROC to deal with ordering of the
110 * Memory Controller traffic. This is needed as the Memory Controller
111 * resets during System Suspend with MSS having all control, but ROC
112 * provides a performance boost as compared to MSS.
113 */
Puneet Saxenacf8c0e22017-08-04 17:19:55 +0530114 if (plat_mc_settings->reconfig_mss_clients != NULL) {
115 plat_mc_settings->reconfig_mss_clients();
116 }
Varun Wadekara0f26972016-03-11 17:18:51 -0800117
Varun Wadekarad45ef72017-04-03 13:44:57 -0700118 /* Program overrides for MC transactions */
Puneet Saxenacf8c0e22017-08-04 17:19:55 +0530119 if (plat_mc_settings->set_txn_overrides != NULL) {
120 plat_mc_settings->set_txn_overrides();
121 }
Varun Wadekarad45ef72017-04-03 13:44:57 -0700122
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530123 /* video memory carveout region */
Anthony Zhou0e07e452017-07-26 17:16:54 +0800124 if (video_mem_base != 0ULL) {
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530125 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO,
126 (uint32_t)video_mem_base);
127 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
128 (uint32_t)(video_mem_base >> 32));
Varun Wadekar7058aee2016-04-25 09:01:46 -0700129 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, video_mem_size_mb);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530130
131 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800132 * MCE propagates the VideoMem configuration values across the
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530133 * CCPLEX.
134 */
135 mce_update_gsc_videomem();
136 }
137}
138
139/*
140 * Secure the BL31 DRAM aperture.
141 *
142 * phys_base = physical base of TZDRAM aperture
143 * size_in_bytes = size of aperture in bytes
144 */
145void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes)
146{
147 /*
Varun Wadekarf3cd5092017-10-30 14:35:17 -0700148 * Perform platform specific steps.
Harvey Hsiehc95802d2016-07-29 20:10:59 +0800149 */
Varun Wadekarf3cd5092017-10-30 14:35:17 -0700150 plat_memctrl_tzdram_setup(phys_base, size_in_bytes);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530151}
152
153/*
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800154 * Secure the BL31 TZRAM aperture.
155 *
156 * phys_base = physical base of TZRAM aperture
157 * size_in_bytes = size of aperture in bytes
158 */
159void tegra_memctrl_tzram_setup(uint64_t phys_base, uint32_t size_in_bytes)
160{
Varun Wadekare6d43222016-05-25 16:35:04 -0700161 uint32_t index;
162 uint32_t total_128kb_blocks = size_in_bytes >> 17;
Varun Wadekar153982c2016-12-21 14:50:18 -0800163 uint32_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12;
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800164 uint32_t val;
165
Varun Wadekar153982c2016-12-21 14:50:18 -0800166 INFO("Configuring TrustZone SRAM Memory Carveout\n");
167
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800168 /*
Varun Wadekare6d43222016-05-25 16:35:04 -0700169 * Reset the access configuration registers to restrict access
170 * to the TZRAM aperture
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800171 */
Steven Kaob688d382017-09-06 13:32:21 +0800172 for (index = MC_TZRAM_CLIENT_ACCESS0_CFG0;
Varun Wadekar153982c2016-12-21 14:50:18 -0800173 index < ((uint32_t)MC_TZRAM_CARVEOUT_CFG + (uint32_t)MC_GSC_CONFIG_REGS_SIZE);
174 index += 4U) {
Varun Wadekare6d43222016-05-25 16:35:04 -0700175 tegra_mc_write_32(index, 0);
Varun Wadekar153982c2016-12-21 14:50:18 -0800176 }
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800177
178 /*
Steven Kaob688d382017-09-06 13:32:21 +0800179 * Enable CPU access configuration registers to access the TZRAM aperture
180 */
181 if (!tegra_chipid_is_t186()) {
182 val = tegra_mc_read_32(MC_TZRAM_CLIENT_ACCESS1_CFG0);
183 val |= TZRAM_ALLOW_MPCORER | TZRAM_ALLOW_MPCOREW;
184 tegra_mc_write_32(MC_TZRAM_CLIENT_ACCESS1_CFG0, val);
185 }
186
187 /*
Varun Wadekare6d43222016-05-25 16:35:04 -0700188 * Set the TZRAM base. TZRAM base must be 4k aligned, at least.
189 */
Varun Wadekar153982c2016-12-21 14:50:18 -0800190 assert((phys_base & (uint64_t)0xFFF) == 0U);
Varun Wadekare6d43222016-05-25 16:35:04 -0700191 tegra_mc_write_32(MC_TZRAM_BASE_LO, (uint32_t)phys_base);
192 tegra_mc_write_32(MC_TZRAM_BASE_HI,
Varun Wadekar153982c2016-12-21 14:50:18 -0800193 (uint32_t)(phys_base >> 32) & MC_GSC_BASE_HI_MASK);
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800194
Varun Wadekare6d43222016-05-25 16:35:04 -0700195 /*
196 * Set the TZRAM size
197 *
198 * total size = (number of 128KB blocks) + (number of remaining 4KB
199 * blocks)
200 *
201 */
Varun Wadekar153982c2016-12-21 14:50:18 -0800202 val = (residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) |
Varun Wadekare6d43222016-05-25 16:35:04 -0700203 total_128kb_blocks;
204 tegra_mc_write_32(MC_TZRAM_SIZE, val);
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800205
Varun Wadekare6d43222016-05-25 16:35:04 -0700206 /*
207 * Lock the configuration settings by disabling TZ-only lock
208 * and locking the configuration against any future changes
209 * at all.
210 */
211 val = tegra_mc_read_32(MC_TZRAM_CARVEOUT_CFG);
Anthony Zhou0844b972017-06-28 16:35:54 +0800212 val &= (uint32_t)~MC_GSC_ENABLE_TZ_LOCK_BIT;
Varun Wadekar153982c2016-12-21 14:50:18 -0800213 val |= MC_GSC_LOCK_CFG_SETTINGS_BIT;
Steven Kaob688d382017-09-06 13:32:21 +0800214 if (!tegra_chipid_is_t186()) {
215 val |= MC_GSC_ENABLE_CPU_SECURE_BIT;
216 }
Varun Wadekare6d43222016-05-25 16:35:04 -0700217 tegra_mc_write_32(MC_TZRAM_CARVEOUT_CFG, val);
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800218
219 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800220 * MCE propagates the security configuration values across the
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800221 * CCPLEX.
222 */
223 mce_update_gsc_tzram();
224}
225
Varun Wadekar153982c2016-12-21 14:50:18 -0800226static void tegra_lock_videomem_nonoverlap(uint64_t phys_base,
227 uint64_t size_in_bytes)
228{
229 uint32_t index;
230 uint64_t total_128kb_blocks = size_in_bytes >> 17;
231 uint64_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12;
232 uint64_t val;
233
234 /*
235 * Reset the access configuration registers to restrict access to
236 * old Videomem aperture
237 */
238 for (index = MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0;
239 index < ((uint32_t)MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0 + (uint32_t)MC_GSC_CONFIG_REGS_SIZE);
240 index += 4U) {
241 tegra_mc_write_32(index, 0);
242 }
243
244 /*
245 * Set the base. It must be 4k aligned, at least.
246 */
247 assert((phys_base & (uint64_t)0xFFF) == 0U);
248 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, (uint32_t)phys_base);
249 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI,
250 (uint32_t)(phys_base >> 32) & (uint32_t)MC_GSC_BASE_HI_MASK);
251
252 /*
253 * Set the aperture size
254 *
255 * total size = (number of 128KB blocks) + (number of remaining 4KB
256 * blocks)
257 *
258 */
259 val = (uint32_t)((residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) |
260 total_128kb_blocks);
261 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, (uint32_t)val);
262
263 /*
264 * Lock the configuration settings by enabling TZ-only lock and
265 * locking the configuration against any future changes from NS
266 * world.
267 */
268 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_CFG,
269 (uint32_t)MC_GSC_ENABLE_TZ_LOCK_BIT);
270
271 /*
272 * MCE propagates the GSC configuration values across the
273 * CCPLEX.
274 */
275}
276
277static void tegra_unlock_videomem_nonoverlap(void)
278{
279 /* Clear the base */
280 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, 0);
281 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI, 0);
282
283 /* Clear the size */
284 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, 0);
285}
286
287static void tegra_clear_videomem(uintptr_t non_overlap_area_start,
288 unsigned long long non_overlap_area_size)
289{
Varun Wadekar117a2e02017-08-03 11:40:34 -0700290 int ret;
291
Varun Wadekar153982c2016-12-21 14:50:18 -0800292 /*
293 * Map the NS memory first, clean it and then unmap it.
294 */
Varun Wadekar117a2e02017-08-03 11:40:34 -0700295 ret = mmap_add_dynamic_region(non_overlap_area_start, /* PA */
Varun Wadekar153982c2016-12-21 14:50:18 -0800296 non_overlap_area_start, /* VA */
297 non_overlap_area_size, /* size */
298 MT_NS | MT_RW | MT_EXECUTE_NEVER); /* attrs */
Varun Wadekar117a2e02017-08-03 11:40:34 -0700299 assert(ret == 0);
Varun Wadekar153982c2016-12-21 14:50:18 -0800300
301 zero_normalmem((void *)non_overlap_area_start, non_overlap_area_size);
302 flush_dcache_range(non_overlap_area_start, non_overlap_area_size);
303
Anthony Zhou0844b972017-06-28 16:35:54 +0800304 (void)mmap_remove_dynamic_region(non_overlap_area_start,
Varun Wadekar153982c2016-12-21 14:50:18 -0800305 non_overlap_area_size);
306}
307
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800308/*
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530309 * Program the Video Memory carveout region
310 *
311 * phys_base = physical base of aperture
312 * size_in_bytes = size of aperture in bytes
313 */
314void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes)
315{
Varun Wadekar153982c2016-12-21 14:50:18 -0800316 uintptr_t vmem_end_old = video_mem_base + (video_mem_size_mb << 20);
317 uintptr_t vmem_end_new = phys_base + size_in_bytes;
Varun Wadekar153982c2016-12-21 14:50:18 -0800318 unsigned long long non_overlap_area_size;
Varun Wadekare60f1bf2016-02-17 10:10:50 -0800319
320 /*
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530321 * Setup the Memory controller to restrict CPU accesses to the Video
322 * Memory region
323 */
324 INFO("Configuring Video Memory Carveout\n");
325
Varun Wadekar153982c2016-12-21 14:50:18 -0800326 /*
327 * Configure Memory Controller directly for the first time.
328 */
329 if (video_mem_base == 0U)
330 goto done;
331
332 /*
333 * Lock the non overlapping memory being cleared so that other masters
334 * do not accidently write to it. The memory would be unlocked once
335 * the non overlapping region is cleared and the new memory
336 * settings take effect.
337 */
338 tegra_lock_videomem_nonoverlap(video_mem_base,
339 video_mem_size_mb << 20);
340
341 /*
342 * Clear the old regions now being exposed. The following cases
343 * can occur -
344 *
345 * 1. clear whole old region (no overlap with new region)
346 * 2. clear old sub-region below new base
347 * 3. clear old sub-region above new end
348 */
349 INFO("Cleaning previous Video Memory Carveout\n");
350
Anthony Zhou0844b972017-06-28 16:35:54 +0800351 if ((phys_base > vmem_end_old) || (video_mem_base > vmem_end_new)) {
Varun Wadekar153982c2016-12-21 14:50:18 -0800352 tegra_clear_videomem(video_mem_base,
Varun Wadekar8b1c0042019-09-05 08:17:02 -0700353 video_mem_size_mb << 20U);
Varun Wadekar153982c2016-12-21 14:50:18 -0800354 } else {
355 if (video_mem_base < phys_base) {
356 non_overlap_area_size = phys_base - video_mem_base;
Varun Wadekar8b1c0042019-09-05 08:17:02 -0700357 tegra_clear_videomem(video_mem_base, non_overlap_area_size);
Varun Wadekar153982c2016-12-21 14:50:18 -0800358 }
359 if (vmem_end_old > vmem_end_new) {
360 non_overlap_area_size = vmem_end_old - vmem_end_new;
Varun Wadekar8b1c0042019-09-05 08:17:02 -0700361 tegra_clear_videomem(vmem_end_new, non_overlap_area_size);
Varun Wadekar153982c2016-12-21 14:50:18 -0800362 }
363 }
364
365done:
366 /* program the Videomem aperture */
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530367 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, (uint32_t)phys_base);
368 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
369 (uint32_t)(phys_base >> 32));
Varun Wadekar7058aee2016-04-25 09:01:46 -0700370 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530371
Varun Wadekar153982c2016-12-21 14:50:18 -0800372 /* unlock the previous locked nonoverlapping aperture */
373 tegra_unlock_videomem_nonoverlap();
374
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530375 /* store new values */
376 video_mem_base = phys_base;
Varun Wadekar7058aee2016-04-25 09:01:46 -0700377 video_mem_size_mb = size_in_bytes >> 20;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530378
379 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800380 * MCE propagates the VideoMem configuration values across the
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530381 * CCPLEX.
382 */
383 mce_update_gsc_videomem();
384}
Varun Wadekarc92050b2017-03-29 14:57:29 -0700385
386/*
387 * This feature exists only for v1 of the Tegra Memory Controller.
388 */
389void tegra_memctrl_disable_ahb_redirection(void)
390{
391 ; /* do nothing */
392}
Harvey Hsieh359be952017-08-21 15:01:53 +0800393
394void tegra_memctrl_clear_pending_interrupts(void)
395{
396 ; /* do nothing */
397}