blob: 55b9152232ca1258cdc5fc50a89e297069084658 [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 Wadekarcd5a2f52015-09-20 15:08:22 +05303 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekarcd5a2f52015-09-20 15:08:22 +05305 */
6
Varun Wadekarcd5a2f52015-09-20 15:08:22 +05307#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
9
10#include <arch_helpers.h>
11#include <common/bl_common.h>
12#include <common/debug.h>
13#include <lib/mmio.h>
14#include <lib/utils.h>
15#include <lib/xlat_tables/xlat_tables_v2.h>
16
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053017#include <mce.h>
18#include <memctrl.h>
19#include <memctrl_v2.h>
Varun Wadekar87e44ff2016-03-03 13:22:39 -080020#include <smmu.h>
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053021#include <tegra_def.h>
Varun Wadekare81177d2016-07-18 17:43:41 -070022#include <tegra_platform.h>
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053023
24/* Video Memory base and size (live values) */
25static uint64_t video_mem_base;
Varun Wadekar7058aee2016-04-25 09:01:46 -070026static uint64_t video_mem_size_mb;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +053027
Varun Wadekara0f26972016-03-11 17:18:51 -080028static void tegra_memctrl_reconfig_mss_clients(void)
29{
30#if ENABLE_ROC_FOR_ORDERING_CLIENT_REQUESTS
31 uint32_t val, wdata_0, wdata_1;
32
33 /*
34 * Assert Memory Controller's HOTRESET_FLUSH_ENABLE signal for
35 * boot and strongly ordered MSS clients to flush existing memory
36 * traffic and stall future requests.
37 */
38 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_CTRL0);
39 assert(val == MC_CLIENT_HOTRESET_CTRL0_RESET_VAL);
40
Varun Wadekar4c7fa502016-12-13 13:13:42 -080041 wdata_0 = MC_CLIENT_HOTRESET_CTRL0_HDA_FLUSH_ENB |
42#if ENABLE_AFI_DEVICE
43 MC_CLIENT_HOTRESET_CTRL0_AFI_FLUSH_ENB |
44#endif
Varun Wadekara0f26972016-03-11 17:18:51 -080045 MC_CLIENT_HOTRESET_CTRL0_SATA_FLUSH_ENB |
46 MC_CLIENT_HOTRESET_CTRL0_XUSB_HOST_FLUSH_ENB |
47 MC_CLIENT_HOTRESET_CTRL0_XUSB_DEV_FLUSH_ENB;
48 tegra_mc_write_32(MC_CLIENT_HOTRESET_CTRL0, wdata_0);
49
50 /* Wait for HOTRESET STATUS to indicate FLUSH_DONE */
51 do {
52 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_STATUS0);
53 } while ((val & wdata_0) != wdata_0);
54
55 /* Wait one more time due to SW WAR for known legacy issue */
56 do {
57 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_STATUS0);
58 } while ((val & wdata_0) != wdata_0);
59
60 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_CTRL1);
61 assert(val == MC_CLIENT_HOTRESET_CTRL1_RESET_VAL);
62
63 wdata_1 = MC_CLIENT_HOTRESET_CTRL1_SDMMC4A_FLUSH_ENB |
64 MC_CLIENT_HOTRESET_CTRL1_APE_FLUSH_ENB |
65 MC_CLIENT_HOTRESET_CTRL1_SE_FLUSH_ENB |
66 MC_CLIENT_HOTRESET_CTRL1_ETR_FLUSH_ENB |
67 MC_CLIENT_HOTRESET_CTRL1_AXIS_FLUSH_ENB |
68 MC_CLIENT_HOTRESET_CTRL1_EQOS_FLUSH_ENB |
69 MC_CLIENT_HOTRESET_CTRL1_UFSHC_FLUSH_ENB |
70 MC_CLIENT_HOTRESET_CTRL1_BPMP_FLUSH_ENB |
71 MC_CLIENT_HOTRESET_CTRL1_AON_FLUSH_ENB |
72 MC_CLIENT_HOTRESET_CTRL1_SCE_FLUSH_ENB;
73 tegra_mc_write_32(MC_CLIENT_HOTRESET_CTRL1, wdata_1);
74
75 /* Wait for HOTRESET STATUS to indicate FLUSH_DONE */
76 do {
77 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_STATUS1);
78 } while ((val & wdata_1) != wdata_1);
79
80 /* Wait one more time due to SW WAR for known legacy issue */
81 do {
82 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_STATUS1);
83 } while ((val & wdata_1) != wdata_1);
84
85 /*
86 * Change MEMTYPE_OVERRIDE from SO_DEV -> PASSTHRU for boot and
87 * strongly ordered MSS clients. ROC needs to be single point
88 * of control on overriding the memory type. So, remove TSA's
89 * memtype override.
90 */
Varun Wadekar4c7fa502016-12-13 13:13:42 -080091#if ENABLE_AFI_DEVICE
Varun Wadekara0f26972016-03-11 17:18:51 -080092 mc_set_tsa_passthrough(AFIW);
Varun Wadekar4c7fa502016-12-13 13:13:42 -080093#endif
Varun Wadekara0f26972016-03-11 17:18:51 -080094 mc_set_tsa_passthrough(HDAW);
95 mc_set_tsa_passthrough(SATAW);
96 mc_set_tsa_passthrough(XUSB_HOSTW);
97 mc_set_tsa_passthrough(XUSB_DEVW);
98 mc_set_tsa_passthrough(SDMMCWAB);
99 mc_set_tsa_passthrough(APEDMAW);
100 mc_set_tsa_passthrough(SESWR);
101 mc_set_tsa_passthrough(ETRW);
102 mc_set_tsa_passthrough(AXISW);
103 mc_set_tsa_passthrough(EQOSW);
104 mc_set_tsa_passthrough(UFSHCW);
105 mc_set_tsa_passthrough(BPMPDMAW);
106 mc_set_tsa_passthrough(AONDMAW);
107 mc_set_tsa_passthrough(SCEDMAW);
108
109 /*
110 * Change COH_PATH_OVERRIDE_SO_DEV from NO_OVERRIDE -> FORCE_COHERENT
111 * for boot and strongly ordered MSS clients. This steers all sodev
112 * transactions to ROC.
113 *
114 * Change AXID_OVERRIDE/AXID_OVERRIDE_SO_DEV only for some clients
115 * whose AXI IDs we know and trust.
116 */
117
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800118#if ENABLE_AFI_DEVICE
Varun Wadekara0f26972016-03-11 17:18:51 -0800119 /* Match AFIW */
120 mc_set_forced_coherent_so_dev_cfg(AFIR);
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800121#endif
Varun Wadekara0f26972016-03-11 17:18:51 -0800122
123 /*
124 * See bug 200131110 comment #35 - there are no normal requests
125 * and AWID for SO/DEV requests is hardcoded in RTL for a
126 * particular PCIE controller
127 */
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800128#if ENABLE_AFI_DEVICE
Varun Wadekara0f26972016-03-11 17:18:51 -0800129 mc_set_forced_coherent_so_dev_cfg(AFIW);
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800130#endif
Varun Wadekara0f26972016-03-11 17:18:51 -0800131 mc_set_forced_coherent_cfg(HDAR);
132 mc_set_forced_coherent_cfg(HDAW);
133 mc_set_forced_coherent_cfg(SATAR);
134 mc_set_forced_coherent_cfg(SATAW);
135 mc_set_forced_coherent_cfg(XUSB_HOSTR);
136 mc_set_forced_coherent_cfg(XUSB_HOSTW);
137 mc_set_forced_coherent_cfg(XUSB_DEVR);
138 mc_set_forced_coherent_cfg(XUSB_DEVW);
139 mc_set_forced_coherent_cfg(SDMMCRAB);
140 mc_set_forced_coherent_cfg(SDMMCWAB);
141
142 /* Match APEDMAW */
143 mc_set_forced_coherent_axid_so_dev_cfg(APEDMAR);
144
145 /*
146 * See bug 200131110 comment #35 - AWID for normal requests
147 * is 0x80 and AWID for SO/DEV requests is 0x01
148 */
149 mc_set_forced_coherent_axid_so_dev_cfg(APEDMAW);
150 mc_set_forced_coherent_cfg(SESRD);
151 mc_set_forced_coherent_cfg(SESWR);
152 mc_set_forced_coherent_cfg(ETRR);
153 mc_set_forced_coherent_cfg(ETRW);
154 mc_set_forced_coherent_cfg(AXISR);
155 mc_set_forced_coherent_cfg(AXISW);
156 mc_set_forced_coherent_cfg(EQOSR);
157 mc_set_forced_coherent_cfg(EQOSW);
158 mc_set_forced_coherent_cfg(UFSHCR);
159 mc_set_forced_coherent_cfg(UFSHCW);
160 mc_set_forced_coherent_cfg(BPMPDMAR);
161 mc_set_forced_coherent_cfg(BPMPDMAW);
162 mc_set_forced_coherent_cfg(AONDMAR);
163 mc_set_forced_coherent_cfg(AONDMAW);
164 mc_set_forced_coherent_cfg(SCEDMAR);
165 mc_set_forced_coherent_cfg(SCEDMAW);
166
167 /*
168 * At this point, ordering can occur at ROC. So, remove PCFIFO's
169 * control over ordering requests.
170 *
171 * Change PCFIFO_*_ORDERED_CLIENT from ORDERED -> UNORDERED for
172 * boot and strongly ordered MSS clients
173 */
174 val = MC_PCFIFO_CLIENT_CONFIG1_RESET_VAL &
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800175#if ENABLE_AFI_DEVICE
Varun Wadekara0f26972016-03-11 17:18:51 -0800176 mc_set_pcfifo_unordered_boot_so_mss(1, AFIW) &
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800177#endif
Varun Wadekara0f26972016-03-11 17:18:51 -0800178 mc_set_pcfifo_unordered_boot_so_mss(1, HDAW) &
179 mc_set_pcfifo_unordered_boot_so_mss(1, SATAW);
180 tegra_mc_write_32(MC_PCFIFO_CLIENT_CONFIG1, val);
181
182 val = MC_PCFIFO_CLIENT_CONFIG2_RESET_VAL &
183 mc_set_pcfifo_unordered_boot_so_mss(2, XUSB_HOSTW) &
184 mc_set_pcfifo_unordered_boot_so_mss(2, XUSB_DEVW);
185 tegra_mc_write_32(MC_PCFIFO_CLIENT_CONFIG2, val);
186
187 val = MC_PCFIFO_CLIENT_CONFIG3_RESET_VAL &
188 mc_set_pcfifo_unordered_boot_so_mss(3, SDMMCWAB);
189 tegra_mc_write_32(MC_PCFIFO_CLIENT_CONFIG3, val);
190
191 val = MC_PCFIFO_CLIENT_CONFIG4_RESET_VAL &
192 mc_set_pcfifo_unordered_boot_so_mss(4, SESWR) &
193 mc_set_pcfifo_unordered_boot_so_mss(4, ETRW) &
194 mc_set_pcfifo_unordered_boot_so_mss(4, AXISW) &
195 mc_set_pcfifo_unordered_boot_so_mss(4, EQOSW) &
196 mc_set_pcfifo_unordered_boot_so_mss(4, UFSHCW) &
197 mc_set_pcfifo_unordered_boot_so_mss(4, BPMPDMAW) &
198 mc_set_pcfifo_unordered_boot_so_mss(4, AONDMAW) &
199 mc_set_pcfifo_unordered_boot_so_mss(4, SCEDMAW);
200 tegra_mc_write_32(MC_PCFIFO_CLIENT_CONFIG4, val);
201
202 val = MC_PCFIFO_CLIENT_CONFIG5_RESET_VAL &
203 mc_set_pcfifo_unordered_boot_so_mss(5, APEDMAW);
204 tegra_mc_write_32(MC_PCFIFO_CLIENT_CONFIG5, val);
205
206 /*
207 * At this point, ordering can occur at ROC. SMMU need not
208 * reorder any requests.
209 *
210 * Change SMMU_*_ORDERED_CLIENT from ORDERED -> UNORDERED
211 * for boot and strongly ordered MSS clients
212 */
213 val = MC_SMMU_CLIENT_CONFIG1_RESET_VAL &
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800214#if ENABLE_AFI_DEVICE
Varun Wadekara0f26972016-03-11 17:18:51 -0800215 mc_set_smmu_unordered_boot_so_mss(1, AFIW) &
Varun Wadekar4c7fa502016-12-13 13:13:42 -0800216#endif
Varun Wadekara0f26972016-03-11 17:18:51 -0800217 mc_set_smmu_unordered_boot_so_mss(1, HDAW) &
218 mc_set_smmu_unordered_boot_so_mss(1, SATAW);
219 tegra_mc_write_32(MC_SMMU_CLIENT_CONFIG1, val);
220
221 val = MC_SMMU_CLIENT_CONFIG2_RESET_VAL &
222 mc_set_smmu_unordered_boot_so_mss(2, XUSB_HOSTW) &
223 mc_set_smmu_unordered_boot_so_mss(2, XUSB_DEVW);
224 tegra_mc_write_32(MC_SMMU_CLIENT_CONFIG2, val);
225
226 val = MC_SMMU_CLIENT_CONFIG3_RESET_VAL &
227 mc_set_smmu_unordered_boot_so_mss(3, SDMMCWAB);
228 tegra_mc_write_32(MC_SMMU_CLIENT_CONFIG3, val);
229
230 val = MC_SMMU_CLIENT_CONFIG4_RESET_VAL &
231 mc_set_smmu_unordered_boot_so_mss(4, SESWR) &
232 mc_set_smmu_unordered_boot_so_mss(4, ETRW) &
233 mc_set_smmu_unordered_boot_so_mss(4, AXISW) &
234 mc_set_smmu_unordered_boot_so_mss(4, EQOSW) &
235 mc_set_smmu_unordered_boot_so_mss(4, UFSHCW) &
236 mc_set_smmu_unordered_boot_so_mss(4, BPMPDMAW) &
237 mc_set_smmu_unordered_boot_so_mss(4, AONDMAW) &
238 mc_set_smmu_unordered_boot_so_mss(4, SCEDMAW);
239 tegra_mc_write_32(MC_SMMU_CLIENT_CONFIG4, val);
240
241 val = MC_SMMU_CLIENT_CONFIG5_RESET_VAL &
242 mc_set_smmu_unordered_boot_so_mss(5, APEDMAW);
243 tegra_mc_write_32(MC_SMMU_CLIENT_CONFIG5, val);
244
245 /*
246 * Deassert HOTRESET FLUSH_ENABLE for boot and strongly ordered MSS
247 * clients to allow memory traffic from all clients to start passing
248 * through ROC
249 */
250 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_CTRL0);
251 assert(val == wdata_0);
252
253 wdata_0 = MC_CLIENT_HOTRESET_CTRL0_RESET_VAL;
254 tegra_mc_write_32(MC_CLIENT_HOTRESET_CTRL0, wdata_0);
255
Varun Wadekara0f26972016-03-11 17:18:51 -0800256 val = tegra_mc_read_32(MC_CLIENT_HOTRESET_CTRL1);
257 assert(val == wdata_1);
258
259 wdata_1 = MC_CLIENT_HOTRESET_CTRL1_RESET_VAL;
260 tegra_mc_write_32(MC_CLIENT_HOTRESET_CTRL1, wdata_1);
261
Varun Wadekara0f26972016-03-11 17:18:51 -0800262#endif
263}
264
Varun Wadekarad45ef72017-04-03 13:44:57 -0700265static void tegra_memctrl_set_overrides(void)
266{
267 tegra_mc_settings_t *plat_mc_settings = tegra_get_mc_settings();
268 const mc_txn_override_cfg_t *mc_txn_override_cfgs;
269 uint32_t num_txn_override_cfgs;
270 uint32_t i, val;
271
272 /* Get the settings from the platform */
273 assert(plat_mc_settings);
274 mc_txn_override_cfgs = plat_mc_settings->txn_override_cfg;
275 num_txn_override_cfgs = plat_mc_settings->num_txn_override_cfgs;
276
277 /*
278 * Set the MC_TXN_OVERRIDE registers for write clients.
279 */
280 if ((tegra_chipid_is_t186()) &&
281 (!tegra_platform_is_silicon() ||
282 (tegra_platform_is_silicon() && (tegra_get_chipid_minor() == 1)))) {
283
284 /*
285 * GPU and NVENC settings for Tegra186 simulation and
286 * Silicon rev. A01
287 */
288 val = tegra_mc_read_32(MC_TXN_OVERRIDE_CONFIG_GPUSWR);
289 val &= ~MC_TXN_OVERRIDE_CGID_TAG_MASK;
290 tegra_mc_write_32(MC_TXN_OVERRIDE_CONFIG_GPUSWR,
291 val | MC_TXN_OVERRIDE_CGID_TAG_ZERO);
292
293 val = tegra_mc_read_32(MC_TXN_OVERRIDE_CONFIG_GPUSWR2);
294 val &= ~MC_TXN_OVERRIDE_CGID_TAG_MASK;
295 tegra_mc_write_32(MC_TXN_OVERRIDE_CONFIG_GPUSWR2,
296 val | MC_TXN_OVERRIDE_CGID_TAG_ZERO);
297
298 val = tegra_mc_read_32(MC_TXN_OVERRIDE_CONFIG_NVENCSWR);
299 val &= ~MC_TXN_OVERRIDE_CGID_TAG_MASK;
300 tegra_mc_write_32(MC_TXN_OVERRIDE_CONFIG_NVENCSWR,
301 val | MC_TXN_OVERRIDE_CGID_TAG_CLIENT_AXI_ID);
302
303 } else {
304
305 /*
306 * Settings for Tegra186 silicon rev. A02 and onwards.
307 */
308 for (i = 0; i < num_txn_override_cfgs; i++) {
309 val = tegra_mc_read_32(mc_txn_override_cfgs[i].offset);
310 val &= ~MC_TXN_OVERRIDE_CGID_TAG_MASK;
311 tegra_mc_write_32(mc_txn_override_cfgs[i].offset,
312 val | mc_txn_override_cfgs[i].cgid_tag);
313 }
314 }
315}
316
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530317/*
Varun Wadekar87e44ff2016-03-03 13:22:39 -0800318 * Init Memory controller during boot.
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530319 */
320void tegra_memctrl_setup(void)
321{
322 uint32_t val;
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +0530323 const uint32_t *mc_streamid_override_regs;
324 uint32_t num_streamid_override_regs;
325 const mc_streamid_security_cfg_t *mc_streamid_sec_cfgs;
326 uint32_t num_streamid_sec_cfgs;
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +0530327 tegra_mc_settings_t *plat_mc_settings = tegra_get_mc_settings();
Varun Wadekarad45ef72017-04-03 13:44:57 -0700328 uint32_t i;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530329
330 INFO("Tegra Memory Controller (v2)\n");
331
Varun Wadekar6cb25f92016-12-19 11:17:54 -0800332#if ENABLE_SMMU_DEVICE
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530333 /* Program the SMMU pagesize */
Varun Wadekar87e44ff2016-03-03 13:22:39 -0800334 tegra_smmu_init();
Varun Wadekar6cb25f92016-12-19 11:17:54 -0800335#endif
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +0530336 /* Get the settings from the platform */
337 assert(plat_mc_settings);
338 mc_streamid_override_regs = plat_mc_settings->streamid_override_cfg;
339 num_streamid_override_regs = plat_mc_settings->num_streamid_override_cfgs;
340 mc_streamid_sec_cfgs = plat_mc_settings->streamid_security_cfg;
341 num_streamid_sec_cfgs = plat_mc_settings->num_streamid_security_cfgs;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530342
343 /* Program all the Stream ID overrides */
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +0530344 for (i = 0; i < num_streamid_override_regs; i++)
345 tegra_mc_streamid_write_32(mc_streamid_override_regs[i],
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530346 MC_STREAM_ID_MAX);
347
348 /* Program the security config settings for all Stream IDs */
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +0530349 for (i = 0; i < num_streamid_sec_cfgs; i++) {
350 val = mc_streamid_sec_cfgs[i].override_enable << 16 |
351 mc_streamid_sec_cfgs[i].override_client_inputs << 8 |
352 mc_streamid_sec_cfgs[i].override_client_ns_flag << 0;
353 tegra_mc_streamid_write_32(mc_streamid_sec_cfgs[i].offset, val);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530354 }
355
356 /*
357 * All requests at boot time, and certain requests during
358 * normal run time, are physically addressed and must bypass
359 * the SMMU. The client hub logic implements a hardware bypass
360 * path around the Translation Buffer Units (TBU). During
361 * boot-time, the SMMU_BYPASS_CTRL register (which defaults to
362 * TBU_BYPASS mode) will be used to steer all requests around
363 * the uninitialized TBUs. During normal operation, this register
364 * is locked into TBU_BYPASS_SID config, which routes requests
365 * with special StreamID 0x7f on the bypass path and all others
366 * through the selected TBU. This is done to disable SMMU Bypass
367 * mode, as it could be used to circumvent SMMU security checks.
368 */
369 tegra_mc_write_32(MC_SMMU_BYPASS_CONFIG,
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +0530370 MC_SMMU_BYPASS_CONFIG_SETTINGS);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530371
Varun Wadekarc9ac3e42016-02-17 15:07:49 -0800372 /*
Varun Wadekara0f26972016-03-11 17:18:51 -0800373 * Re-configure MSS to allow ROC to deal with ordering of the
374 * Memory Controller traffic. This is needed as the Memory Controller
375 * boots with MSS having all control, but ROC provides a performance
376 * boost as compared to MSS.
377 */
378 tegra_memctrl_reconfig_mss_clients();
379
Varun Wadekarad45ef72017-04-03 13:44:57 -0700380 /* Program overrides for MC transactions */
381 tegra_memctrl_set_overrides();
Varun Wadekar87e44ff2016-03-03 13:22:39 -0800382}
Varun Wadekarc9ac3e42016-02-17 15:07:49 -0800383
Varun Wadekar87e44ff2016-03-03 13:22:39 -0800384/*
385 * Restore Memory Controller settings after "System Suspend"
386 */
387void tegra_memctrl_restore_settings(void)
388{
Varun Wadekara0f26972016-03-11 17:18:51 -0800389 /*
390 * Re-configure MSS to allow ROC to deal with ordering of the
391 * Memory Controller traffic. This is needed as the Memory Controller
392 * resets during System Suspend with MSS having all control, but ROC
393 * provides a performance boost as compared to MSS.
394 */
395 tegra_memctrl_reconfig_mss_clients();
396
Varun Wadekarad45ef72017-04-03 13:44:57 -0700397 /* Program overrides for MC transactions */
398 tegra_memctrl_set_overrides();
399
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530400 /* video memory carveout region */
401 if (video_mem_base) {
402 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO,
403 (uint32_t)video_mem_base);
404 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
405 (uint32_t)(video_mem_base >> 32));
Varun Wadekar7058aee2016-04-25 09:01:46 -0700406 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, video_mem_size_mb);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530407
408 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800409 * MCE propagates the VideoMem configuration values across the
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530410 * CCPLEX.
411 */
412 mce_update_gsc_videomem();
413 }
414}
415
416/*
417 * Secure the BL31 DRAM aperture.
418 *
419 * phys_base = physical base of TZDRAM aperture
420 * size_in_bytes = size of aperture in bytes
421 */
422void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes)
423{
424 /*
425 * Setup the Memory controller to allow only secure accesses to
426 * the TZDRAM carveout
427 */
428 INFO("Configuring TrustZone DRAM Memory Carveout\n");
429
430 tegra_mc_write_32(MC_SECURITY_CFG0_0, (uint32_t)phys_base);
431 tegra_mc_write_32(MC_SECURITY_CFG3_0, (uint32_t)(phys_base >> 32));
432 tegra_mc_write_32(MC_SECURITY_CFG1_0, size_in_bytes >> 20);
433
434 /*
Harvey Hsiehc95802d2016-07-29 20:10:59 +0800435 * When TZ encryption enabled,
436 * We need setup TZDRAM before CPU to access TZ Carveout,
437 * otherwise CPU will fetch non-decrypted data.
438 * So save TZDRAM setting for retore by SC7 resume FW.
439 */
440
441 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV55_LO,
442 tegra_mc_read_32(MC_SECURITY_CFG0_0));
443 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV55_HI,
444 tegra_mc_read_32(MC_SECURITY_CFG3_0));
445 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV54_HI,
446 tegra_mc_read_32(MC_SECURITY_CFG1_0));
447
448 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800449 * MCE propagates the security configuration values across the
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530450 * CCPLEX.
451 */
452 mce_update_gsc_tzdram();
453}
454
455/*
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800456 * Secure the BL31 TZRAM aperture.
457 *
458 * phys_base = physical base of TZRAM aperture
459 * size_in_bytes = size of aperture in bytes
460 */
461void tegra_memctrl_tzram_setup(uint64_t phys_base, uint32_t size_in_bytes)
462{
Varun Wadekare6d43222016-05-25 16:35:04 -0700463 uint32_t index;
464 uint32_t total_128kb_blocks = size_in_bytes >> 17;
Varun Wadekar153982c2016-12-21 14:50:18 -0800465 uint32_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12;
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800466 uint32_t val;
467
Varun Wadekar153982c2016-12-21 14:50:18 -0800468 INFO("Configuring TrustZone SRAM Memory Carveout\n");
469
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800470 /*
Varun Wadekare6d43222016-05-25 16:35:04 -0700471 * Reset the access configuration registers to restrict access
472 * to the TZRAM aperture
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800473 */
Varun Wadekar153982c2016-12-21 14:50:18 -0800474 for (index = MC_TZRAM_CLIENT_ACCESS_CFG0;
475 index < ((uint32_t)MC_TZRAM_CARVEOUT_CFG + (uint32_t)MC_GSC_CONFIG_REGS_SIZE);
476 index += 4U) {
Varun Wadekare6d43222016-05-25 16:35:04 -0700477 tegra_mc_write_32(index, 0);
Varun Wadekar153982c2016-12-21 14:50:18 -0800478 }
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800479
480 /*
Varun Wadekare6d43222016-05-25 16:35:04 -0700481 * Set the TZRAM base. TZRAM base must be 4k aligned, at least.
482 */
Varun Wadekar153982c2016-12-21 14:50:18 -0800483 assert((phys_base & (uint64_t)0xFFF) == 0U);
Varun Wadekare6d43222016-05-25 16:35:04 -0700484 tegra_mc_write_32(MC_TZRAM_BASE_LO, (uint32_t)phys_base);
485 tegra_mc_write_32(MC_TZRAM_BASE_HI,
Varun Wadekar153982c2016-12-21 14:50:18 -0800486 (uint32_t)(phys_base >> 32) & MC_GSC_BASE_HI_MASK);
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800487
Varun Wadekare6d43222016-05-25 16:35:04 -0700488 /*
489 * Set the TZRAM size
490 *
491 * total size = (number of 128KB blocks) + (number of remaining 4KB
492 * blocks)
493 *
494 */
Varun Wadekar153982c2016-12-21 14:50:18 -0800495 val = (residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) |
Varun Wadekare6d43222016-05-25 16:35:04 -0700496 total_128kb_blocks;
497 tegra_mc_write_32(MC_TZRAM_SIZE, val);
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800498
Varun Wadekare6d43222016-05-25 16:35:04 -0700499 /*
500 * Lock the configuration settings by disabling TZ-only lock
501 * and locking the configuration against any future changes
502 * at all.
503 */
504 val = tegra_mc_read_32(MC_TZRAM_CARVEOUT_CFG);
Varun Wadekar153982c2016-12-21 14:50:18 -0800505 val &= ~MC_GSC_ENABLE_TZ_LOCK_BIT;
506 val |= MC_GSC_LOCK_CFG_SETTINGS_BIT;
Varun Wadekare6d43222016-05-25 16:35:04 -0700507 tegra_mc_write_32(MC_TZRAM_CARVEOUT_CFG, val);
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800508
509 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800510 * MCE propagates the security configuration values across the
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800511 * CCPLEX.
512 */
513 mce_update_gsc_tzram();
514}
515
Varun Wadekar153982c2016-12-21 14:50:18 -0800516static void tegra_lock_videomem_nonoverlap(uint64_t phys_base,
517 uint64_t size_in_bytes)
518{
519 uint32_t index;
520 uint64_t total_128kb_blocks = size_in_bytes >> 17;
521 uint64_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12;
522 uint64_t val;
523
524 /*
525 * Reset the access configuration registers to restrict access to
526 * old Videomem aperture
527 */
528 for (index = MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0;
529 index < ((uint32_t)MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0 + (uint32_t)MC_GSC_CONFIG_REGS_SIZE);
530 index += 4U) {
531 tegra_mc_write_32(index, 0);
532 }
533
534 /*
535 * Set the base. It must be 4k aligned, at least.
536 */
537 assert((phys_base & (uint64_t)0xFFF) == 0U);
538 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, (uint32_t)phys_base);
539 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI,
540 (uint32_t)(phys_base >> 32) & (uint32_t)MC_GSC_BASE_HI_MASK);
541
542 /*
543 * Set the aperture size
544 *
545 * total size = (number of 128KB blocks) + (number of remaining 4KB
546 * blocks)
547 *
548 */
549 val = (uint32_t)((residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) |
550 total_128kb_blocks);
551 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, (uint32_t)val);
552
553 /*
554 * Lock the configuration settings by enabling TZ-only lock and
555 * locking the configuration against any future changes from NS
556 * world.
557 */
558 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_CFG,
559 (uint32_t)MC_GSC_ENABLE_TZ_LOCK_BIT);
560
561 /*
562 * MCE propagates the GSC configuration values across the
563 * CCPLEX.
564 */
565}
566
567static void tegra_unlock_videomem_nonoverlap(void)
568{
569 /* Clear the base */
570 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, 0);
571 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI, 0);
572
573 /* Clear the size */
574 tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, 0);
575}
576
577static void tegra_clear_videomem(uintptr_t non_overlap_area_start,
578 unsigned long long non_overlap_area_size)
579{
580 /*
581 * Map the NS memory first, clean it and then unmap it.
582 */
583 mmap_add_dynamic_region(non_overlap_area_start, /* PA */
584 non_overlap_area_start, /* VA */
585 non_overlap_area_size, /* size */
586 MT_NS | MT_RW | MT_EXECUTE_NEVER); /* attrs */
587
588 zero_normalmem((void *)non_overlap_area_start, non_overlap_area_size);
589 flush_dcache_range(non_overlap_area_start, non_overlap_area_size);
590
591 mmap_remove_dynamic_region(non_overlap_area_start,
592 non_overlap_area_size);
593}
594
Varun Wadekar13e7dc42015-12-30 15:15:08 -0800595/*
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530596 * Program the Video Memory carveout region
597 *
598 * phys_base = physical base of aperture
599 * size_in_bytes = size of aperture in bytes
600 */
601void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes)
602{
Varun Wadekar153982c2016-12-21 14:50:18 -0800603 uintptr_t vmem_end_old = video_mem_base + (video_mem_size_mb << 20);
604 uintptr_t vmem_end_new = phys_base + size_in_bytes;
Varun Wadekar153982c2016-12-21 14:50:18 -0800605 unsigned long long non_overlap_area_size;
Varun Wadekare60f1bf2016-02-17 10:10:50 -0800606
607 /*
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530608 * Setup the Memory controller to restrict CPU accesses to the Video
609 * Memory region
610 */
611 INFO("Configuring Video Memory Carveout\n");
612
Varun Wadekar153982c2016-12-21 14:50:18 -0800613 /*
614 * Configure Memory Controller directly for the first time.
615 */
616 if (video_mem_base == 0U)
617 goto done;
618
619 /*
620 * Lock the non overlapping memory being cleared so that other masters
621 * do not accidently write to it. The memory would be unlocked once
622 * the non overlapping region is cleared and the new memory
623 * settings take effect.
624 */
625 tegra_lock_videomem_nonoverlap(video_mem_base,
626 video_mem_size_mb << 20);
627
628 /*
629 * Clear the old regions now being exposed. The following cases
630 * can occur -
631 *
632 * 1. clear whole old region (no overlap with new region)
633 * 2. clear old sub-region below new base
634 * 3. clear old sub-region above new end
635 */
636 INFO("Cleaning previous Video Memory Carveout\n");
637
638 if (phys_base > vmem_end_old || video_mem_base > vmem_end_new) {
639 tegra_clear_videomem(video_mem_base,
640 (uint64_t)video_mem_size_mb << 20);
641 } else {
642 if (video_mem_base < phys_base) {
643 non_overlap_area_size = phys_base - video_mem_base;
644 tegra_clear_videomem(video_mem_base, non_overlap_area_size);
645 }
646 if (vmem_end_old > vmem_end_new) {
647 non_overlap_area_size = vmem_end_old - vmem_end_new;
648 tegra_clear_videomem(vmem_end_new, non_overlap_area_size);
649 }
650 }
651
652done:
653 /* program the Videomem aperture */
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530654 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, (uint32_t)phys_base);
655 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI,
656 (uint32_t)(phys_base >> 32));
Varun Wadekar7058aee2016-04-25 09:01:46 -0700657 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20);
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530658
Varun Wadekar153982c2016-12-21 14:50:18 -0800659 /* unlock the previous locked nonoverlapping aperture */
660 tegra_unlock_videomem_nonoverlap();
661
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530662 /* store new values */
663 video_mem_base = phys_base;
Varun Wadekar7058aee2016-04-25 09:01:46 -0700664 video_mem_size_mb = size_in_bytes >> 20;
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530665
666 /*
Varun Wadekar153982c2016-12-21 14:50:18 -0800667 * MCE propagates the VideoMem configuration values across the
Varun Wadekarcd5a2f52015-09-20 15:08:22 +0530668 * CCPLEX.
669 */
670 mce_update_gsc_videomem();
671}
Varun Wadekarc92050b2017-03-29 14:57:29 -0700672
673/*
674 * This feature exists only for v1 of the Tegra Memory Controller.
675 */
676void tegra_memctrl_disable_ahb_redirection(void)
677{
678 ; /* do nothing */
679}