Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 1 | /* |
Harvey Hsieh | b9b374f | 2016-11-15 22:04:51 +0800 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #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 Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 17 | #include <mce.h> |
| 18 | #include <memctrl.h> |
| 19 | #include <memctrl_v2.h> |
Varun Wadekar | 87e44ff | 2016-03-03 13:22:39 -0800 | [diff] [blame] | 20 | #include <smmu.h> |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 21 | #include <tegra_def.h> |
Varun Wadekar | e81177d | 2016-07-18 17:43:41 -0700 | [diff] [blame] | 22 | #include <tegra_platform.h> |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 23 | |
| 24 | /* Video Memory base and size (live values) */ |
| 25 | static uint64_t video_mem_base; |
Varun Wadekar | 7058aee | 2016-04-25 09:01:46 -0700 | [diff] [blame] | 26 | static uint64_t video_mem_size_mb; |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 27 | |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 28 | /* |
Varun Wadekar | f3cd509 | 2017-10-30 14:35:17 -0700 | [diff] [blame] | 29 | * The following platform setup functions are weakly defined. They |
| 30 | * provide typical implementations that will be overridden by a SoC. |
| 31 | */ |
| 32 | #pragma weak plat_memctrl_tzdram_setup |
| 33 | void plat_memctrl_tzdram_setup(uint64_t phys_base, uint64_t size_in_bytes) |
| 34 | { |
| 35 | ; /* do nothing */ |
| 36 | } |
| 37 | |
| 38 | /* |
Varun Wadekar | 87e44ff | 2016-03-03 13:22:39 -0800 | [diff] [blame] | 39 | * Init Memory controller during boot. |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 40 | */ |
| 41 | void tegra_memctrl_setup(void) |
| 42 | { |
| 43 | uint32_t val; |
Pritesh Raithatha | 9eb5db5 | 2017-01-02 19:42:31 +0530 | [diff] [blame] | 44 | const uint32_t *mc_streamid_override_regs; |
| 45 | uint32_t num_streamid_override_regs; |
| 46 | const mc_streamid_security_cfg_t *mc_streamid_sec_cfgs; |
| 47 | uint32_t num_streamid_sec_cfgs; |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 48 | const tegra_mc_settings_t *plat_mc_settings = tegra_get_mc_settings(); |
Varun Wadekar | ad45ef7 | 2017-04-03 13:44:57 -0700 | [diff] [blame] | 49 | uint32_t i; |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 50 | |
| 51 | INFO("Tegra Memory Controller (v2)\n"); |
| 52 | |
Varun Wadekar | 6cb25f9 | 2016-12-19 11:17:54 -0800 | [diff] [blame] | 53 | #if ENABLE_SMMU_DEVICE |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 54 | /* Program the SMMU pagesize */ |
Varun Wadekar | 87e44ff | 2016-03-03 13:22:39 -0800 | [diff] [blame] | 55 | tegra_smmu_init(); |
Varun Wadekar | 6cb25f9 | 2016-12-19 11:17:54 -0800 | [diff] [blame] | 56 | #endif |
Pritesh Raithatha | 9eb5db5 | 2017-01-02 19:42:31 +0530 | [diff] [blame] | 57 | /* Get the settings from the platform */ |
Anthony Zhou | 4408e88 | 2017-07-07 14:29:51 +0800 | [diff] [blame] | 58 | assert(plat_mc_settings != NULL); |
Pritesh Raithatha | 9eb5db5 | 2017-01-02 19:42:31 +0530 | [diff] [blame] | 59 | mc_streamid_override_regs = plat_mc_settings->streamid_override_cfg; |
| 60 | num_streamid_override_regs = plat_mc_settings->num_streamid_override_cfgs; |
| 61 | mc_streamid_sec_cfgs = plat_mc_settings->streamid_security_cfg; |
| 62 | num_streamid_sec_cfgs = plat_mc_settings->num_streamid_security_cfgs; |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 63 | |
| 64 | /* Program all the Stream ID overrides */ |
Pritesh Raithatha | 9eb5db5 | 2017-01-02 19:42:31 +0530 | [diff] [blame] | 65 | for (i = 0; i < num_streamid_override_regs; i++) |
| 66 | tegra_mc_streamid_write_32(mc_streamid_override_regs[i], |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 67 | MC_STREAM_ID_MAX); |
| 68 | |
| 69 | /* Program the security config settings for all Stream IDs */ |
Pritesh Raithatha | 9eb5db5 | 2017-01-02 19:42:31 +0530 | [diff] [blame] | 70 | for (i = 0; i < num_streamid_sec_cfgs; i++) { |
| 71 | val = mc_streamid_sec_cfgs[i].override_enable << 16 | |
| 72 | mc_streamid_sec_cfgs[i].override_client_inputs << 8 | |
| 73 | mc_streamid_sec_cfgs[i].override_client_ns_flag << 0; |
| 74 | tegra_mc_streamid_write_32(mc_streamid_sec_cfgs[i].offset, val); |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
| 78 | * All requests at boot time, and certain requests during |
| 79 | * normal run time, are physically addressed and must bypass |
| 80 | * the SMMU. The client hub logic implements a hardware bypass |
| 81 | * path around the Translation Buffer Units (TBU). During |
| 82 | * boot-time, the SMMU_BYPASS_CTRL register (which defaults to |
| 83 | * TBU_BYPASS mode) will be used to steer all requests around |
| 84 | * the uninitialized TBUs. During normal operation, this register |
| 85 | * is locked into TBU_BYPASS_SID config, which routes requests |
| 86 | * with special StreamID 0x7f on the bypass path and all others |
| 87 | * through the selected TBU. This is done to disable SMMU Bypass |
| 88 | * mode, as it could be used to circumvent SMMU security checks. |
| 89 | */ |
| 90 | tegra_mc_write_32(MC_SMMU_BYPASS_CONFIG, |
Pritesh Raithatha | 9eb5db5 | 2017-01-02 19:42:31 +0530 | [diff] [blame] | 91 | MC_SMMU_BYPASS_CONFIG_SETTINGS); |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 92 | |
Varun Wadekar | c9ac3e4 | 2016-02-17 15:07:49 -0800 | [diff] [blame] | 93 | /* |
Varun Wadekar | a0f2697 | 2016-03-11 17:18:51 -0800 | [diff] [blame] | 94 | * Re-configure MSS to allow ROC to deal with ordering of the |
| 95 | * Memory Controller traffic. This is needed as the Memory Controller |
| 96 | * boots with MSS having all control, but ROC provides a performance |
| 97 | * boost as compared to MSS. |
| 98 | */ |
Puneet Saxena | cf8c0e2 | 2017-08-04 17:19:55 +0530 | [diff] [blame] | 99 | if (plat_mc_settings->reconfig_mss_clients != NULL) { |
| 100 | plat_mc_settings->reconfig_mss_clients(); |
| 101 | } |
Varun Wadekar | a0f2697 | 2016-03-11 17:18:51 -0800 | [diff] [blame] | 102 | |
Varun Wadekar | ad45ef7 | 2017-04-03 13:44:57 -0700 | [diff] [blame] | 103 | /* Program overrides for MC transactions */ |
Puneet Saxena | cf8c0e2 | 2017-08-04 17:19:55 +0530 | [diff] [blame] | 104 | if (plat_mc_settings->set_txn_overrides != NULL) { |
| 105 | plat_mc_settings->set_txn_overrides(); |
| 106 | } |
Varun Wadekar | 87e44ff | 2016-03-03 13:22:39 -0800 | [diff] [blame] | 107 | } |
Varun Wadekar | c9ac3e4 | 2016-02-17 15:07:49 -0800 | [diff] [blame] | 108 | |
Varun Wadekar | 87e44ff | 2016-03-03 13:22:39 -0800 | [diff] [blame] | 109 | /* |
| 110 | * Restore Memory Controller settings after "System Suspend" |
| 111 | */ |
| 112 | void tegra_memctrl_restore_settings(void) |
| 113 | { |
Puneet Saxena | cf8c0e2 | 2017-08-04 17:19:55 +0530 | [diff] [blame] | 114 | const tegra_mc_settings_t *plat_mc_settings = tegra_get_mc_settings(); |
| 115 | |
| 116 | assert(plat_mc_settings != NULL); |
| 117 | |
Varun Wadekar | a0f2697 | 2016-03-11 17:18:51 -0800 | [diff] [blame] | 118 | /* |
| 119 | * Re-configure MSS to allow ROC to deal with ordering of the |
| 120 | * Memory Controller traffic. This is needed as the Memory Controller |
| 121 | * resets during System Suspend with MSS having all control, but ROC |
| 122 | * provides a performance boost as compared to MSS. |
| 123 | */ |
Puneet Saxena | cf8c0e2 | 2017-08-04 17:19:55 +0530 | [diff] [blame] | 124 | if (plat_mc_settings->reconfig_mss_clients != NULL) { |
| 125 | plat_mc_settings->reconfig_mss_clients(); |
| 126 | } |
Varun Wadekar | a0f2697 | 2016-03-11 17:18:51 -0800 | [diff] [blame] | 127 | |
Varun Wadekar | ad45ef7 | 2017-04-03 13:44:57 -0700 | [diff] [blame] | 128 | /* Program overrides for MC transactions */ |
Puneet Saxena | cf8c0e2 | 2017-08-04 17:19:55 +0530 | [diff] [blame] | 129 | if (plat_mc_settings->set_txn_overrides != NULL) { |
| 130 | plat_mc_settings->set_txn_overrides(); |
| 131 | } |
Varun Wadekar | ad45ef7 | 2017-04-03 13:44:57 -0700 | [diff] [blame] | 132 | |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 133 | /* video memory carveout region */ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 134 | if (video_mem_base != 0ULL) { |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 135 | tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, |
| 136 | (uint32_t)video_mem_base); |
| 137 | tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI, |
| 138 | (uint32_t)(video_mem_base >> 32)); |
Varun Wadekar | 7058aee | 2016-04-25 09:01:46 -0700 | [diff] [blame] | 139 | tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, video_mem_size_mb); |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 140 | |
| 141 | /* |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 142 | * MCE propagates the VideoMem configuration values across the |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 143 | * CCPLEX. |
| 144 | */ |
| 145 | mce_update_gsc_videomem(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Secure the BL31 DRAM aperture. |
| 151 | * |
| 152 | * phys_base = physical base of TZDRAM aperture |
| 153 | * size_in_bytes = size of aperture in bytes |
| 154 | */ |
| 155 | void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes) |
| 156 | { |
| 157 | /* |
| 158 | * Setup the Memory controller to allow only secure accesses to |
| 159 | * the TZDRAM carveout |
| 160 | */ |
| 161 | INFO("Configuring TrustZone DRAM Memory Carveout\n"); |
| 162 | |
| 163 | tegra_mc_write_32(MC_SECURITY_CFG0_0, (uint32_t)phys_base); |
| 164 | tegra_mc_write_32(MC_SECURITY_CFG3_0, (uint32_t)(phys_base >> 32)); |
| 165 | tegra_mc_write_32(MC_SECURITY_CFG1_0, size_in_bytes >> 20); |
| 166 | |
| 167 | /* |
Varun Wadekar | f3cd509 | 2017-10-30 14:35:17 -0700 | [diff] [blame] | 168 | * Perform platform specific steps. |
Harvey Hsieh | c95802d | 2016-07-29 20:10:59 +0800 | [diff] [blame] | 169 | */ |
Varun Wadekar | f3cd509 | 2017-10-30 14:35:17 -0700 | [diff] [blame] | 170 | plat_memctrl_tzdram_setup(phys_base, size_in_bytes); |
Harvey Hsieh | c95802d | 2016-07-29 20:10:59 +0800 | [diff] [blame] | 171 | |
| 172 | /* |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 173 | * MCE propagates the security configuration values across the |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 174 | * CCPLEX. |
| 175 | */ |
| 176 | mce_update_gsc_tzdram(); |
| 177 | } |
| 178 | |
| 179 | /* |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 180 | * Secure the BL31 TZRAM aperture. |
| 181 | * |
| 182 | * phys_base = physical base of TZRAM aperture |
| 183 | * size_in_bytes = size of aperture in bytes |
| 184 | */ |
| 185 | void tegra_memctrl_tzram_setup(uint64_t phys_base, uint32_t size_in_bytes) |
| 186 | { |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 187 | uint32_t index; |
| 188 | uint32_t total_128kb_blocks = size_in_bytes >> 17; |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 189 | uint32_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12; |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 190 | uint32_t val; |
| 191 | |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 192 | INFO("Configuring TrustZone SRAM Memory Carveout\n"); |
| 193 | |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 194 | /* |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 195 | * Reset the access configuration registers to restrict access |
| 196 | * to the TZRAM aperture |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 197 | */ |
Steven Kao | b688d38 | 2017-09-06 13:32:21 +0800 | [diff] [blame] | 198 | for (index = MC_TZRAM_CLIENT_ACCESS0_CFG0; |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 199 | index < ((uint32_t)MC_TZRAM_CARVEOUT_CFG + (uint32_t)MC_GSC_CONFIG_REGS_SIZE); |
| 200 | index += 4U) { |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 201 | tegra_mc_write_32(index, 0); |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 202 | } |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 203 | |
| 204 | /* |
Steven Kao | b688d38 | 2017-09-06 13:32:21 +0800 | [diff] [blame] | 205 | * Enable CPU access configuration registers to access the TZRAM aperture |
| 206 | */ |
| 207 | if (!tegra_chipid_is_t186()) { |
| 208 | val = tegra_mc_read_32(MC_TZRAM_CLIENT_ACCESS1_CFG0); |
| 209 | val |= TZRAM_ALLOW_MPCORER | TZRAM_ALLOW_MPCOREW; |
| 210 | tegra_mc_write_32(MC_TZRAM_CLIENT_ACCESS1_CFG0, val); |
| 211 | } |
| 212 | |
| 213 | /* |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 214 | * Set the TZRAM base. TZRAM base must be 4k aligned, at least. |
| 215 | */ |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 216 | assert((phys_base & (uint64_t)0xFFF) == 0U); |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 217 | tegra_mc_write_32(MC_TZRAM_BASE_LO, (uint32_t)phys_base); |
| 218 | tegra_mc_write_32(MC_TZRAM_BASE_HI, |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 219 | (uint32_t)(phys_base >> 32) & MC_GSC_BASE_HI_MASK); |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 220 | |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 221 | /* |
| 222 | * Set the TZRAM size |
| 223 | * |
| 224 | * total size = (number of 128KB blocks) + (number of remaining 4KB |
| 225 | * blocks) |
| 226 | * |
| 227 | */ |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 228 | val = (residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) | |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 229 | total_128kb_blocks; |
| 230 | tegra_mc_write_32(MC_TZRAM_SIZE, val); |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 231 | |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 232 | /* |
| 233 | * Lock the configuration settings by disabling TZ-only lock |
| 234 | * and locking the configuration against any future changes |
| 235 | * at all. |
| 236 | */ |
| 237 | val = tegra_mc_read_32(MC_TZRAM_CARVEOUT_CFG); |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 238 | val &= (uint32_t)~MC_GSC_ENABLE_TZ_LOCK_BIT; |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 239 | val |= MC_GSC_LOCK_CFG_SETTINGS_BIT; |
Steven Kao | b688d38 | 2017-09-06 13:32:21 +0800 | [diff] [blame] | 240 | if (!tegra_chipid_is_t186()) { |
| 241 | val |= MC_GSC_ENABLE_CPU_SECURE_BIT; |
| 242 | } |
Varun Wadekar | e6d4322 | 2016-05-25 16:35:04 -0700 | [diff] [blame] | 243 | tegra_mc_write_32(MC_TZRAM_CARVEOUT_CFG, val); |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 244 | |
| 245 | /* |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 246 | * MCE propagates the security configuration values across the |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 247 | * CCPLEX. |
| 248 | */ |
| 249 | mce_update_gsc_tzram(); |
| 250 | } |
| 251 | |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 252 | static void tegra_lock_videomem_nonoverlap(uint64_t phys_base, |
| 253 | uint64_t size_in_bytes) |
| 254 | { |
| 255 | uint32_t index; |
| 256 | uint64_t total_128kb_blocks = size_in_bytes >> 17; |
| 257 | uint64_t residual_4kb_blocks = (size_in_bytes & (uint32_t)0x1FFFF) >> 12; |
| 258 | uint64_t val; |
| 259 | |
| 260 | /* |
| 261 | * Reset the access configuration registers to restrict access to |
| 262 | * old Videomem aperture |
| 263 | */ |
| 264 | for (index = MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0; |
| 265 | index < ((uint32_t)MC_VIDEO_PROTECT_CLEAR_ACCESS_CFG0 + (uint32_t)MC_GSC_CONFIG_REGS_SIZE); |
| 266 | index += 4U) { |
| 267 | tegra_mc_write_32(index, 0); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Set the base. It must be 4k aligned, at least. |
| 272 | */ |
| 273 | assert((phys_base & (uint64_t)0xFFF) == 0U); |
| 274 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, (uint32_t)phys_base); |
| 275 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI, |
| 276 | (uint32_t)(phys_base >> 32) & (uint32_t)MC_GSC_BASE_HI_MASK); |
| 277 | |
| 278 | /* |
| 279 | * Set the aperture size |
| 280 | * |
| 281 | * total size = (number of 128KB blocks) + (number of remaining 4KB |
| 282 | * blocks) |
| 283 | * |
| 284 | */ |
| 285 | val = (uint32_t)((residual_4kb_blocks << MC_GSC_SIZE_RANGE_4KB_SHIFT) | |
| 286 | total_128kb_blocks); |
| 287 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, (uint32_t)val); |
| 288 | |
| 289 | /* |
| 290 | * Lock the configuration settings by enabling TZ-only lock and |
| 291 | * locking the configuration against any future changes from NS |
| 292 | * world. |
| 293 | */ |
| 294 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_CFG, |
| 295 | (uint32_t)MC_GSC_ENABLE_TZ_LOCK_BIT); |
| 296 | |
| 297 | /* |
| 298 | * MCE propagates the GSC configuration values across the |
| 299 | * CCPLEX. |
| 300 | */ |
| 301 | } |
| 302 | |
| 303 | static void tegra_unlock_videomem_nonoverlap(void) |
| 304 | { |
| 305 | /* Clear the base */ |
| 306 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_LO, 0); |
| 307 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_BASE_HI, 0); |
| 308 | |
| 309 | /* Clear the size */ |
| 310 | tegra_mc_write_32(MC_VIDEO_PROTECT_CLEAR_SIZE, 0); |
| 311 | } |
| 312 | |
| 313 | static void tegra_clear_videomem(uintptr_t non_overlap_area_start, |
| 314 | unsigned long long non_overlap_area_size) |
| 315 | { |
Varun Wadekar | 117a2e0 | 2017-08-03 11:40:34 -0700 | [diff] [blame] | 316 | int ret; |
| 317 | |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 318 | /* |
| 319 | * Map the NS memory first, clean it and then unmap it. |
| 320 | */ |
Varun Wadekar | 117a2e0 | 2017-08-03 11:40:34 -0700 | [diff] [blame] | 321 | ret = mmap_add_dynamic_region(non_overlap_area_start, /* PA */ |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 322 | non_overlap_area_start, /* VA */ |
| 323 | non_overlap_area_size, /* size */ |
| 324 | MT_NS | MT_RW | MT_EXECUTE_NEVER); /* attrs */ |
Varun Wadekar | 117a2e0 | 2017-08-03 11:40:34 -0700 | [diff] [blame] | 325 | assert(ret == 0); |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 326 | |
| 327 | zero_normalmem((void *)non_overlap_area_start, non_overlap_area_size); |
| 328 | flush_dcache_range(non_overlap_area_start, non_overlap_area_size); |
| 329 | |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 330 | (void)mmap_remove_dynamic_region(non_overlap_area_start, |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 331 | non_overlap_area_size); |
| 332 | } |
| 333 | |
Varun Wadekar | 13e7dc4 | 2015-12-30 15:15:08 -0800 | [diff] [blame] | 334 | /* |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 335 | * Program the Video Memory carveout region |
| 336 | * |
| 337 | * phys_base = physical base of aperture |
| 338 | * size_in_bytes = size of aperture in bytes |
| 339 | */ |
| 340 | void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes) |
| 341 | { |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 342 | uintptr_t vmem_end_old = video_mem_base + (video_mem_size_mb << 20); |
| 343 | uintptr_t vmem_end_new = phys_base + size_in_bytes; |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 344 | unsigned long long non_overlap_area_size; |
Varun Wadekar | e60f1bf | 2016-02-17 10:10:50 -0800 | [diff] [blame] | 345 | |
| 346 | /* |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 347 | * Setup the Memory controller to restrict CPU accesses to the Video |
| 348 | * Memory region |
| 349 | */ |
| 350 | INFO("Configuring Video Memory Carveout\n"); |
| 351 | |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 352 | /* |
| 353 | * Configure Memory Controller directly for the first time. |
| 354 | */ |
| 355 | if (video_mem_base == 0U) |
| 356 | goto done; |
| 357 | |
| 358 | /* |
| 359 | * Lock the non overlapping memory being cleared so that other masters |
| 360 | * do not accidently write to it. The memory would be unlocked once |
| 361 | * the non overlapping region is cleared and the new memory |
| 362 | * settings take effect. |
| 363 | */ |
| 364 | tegra_lock_videomem_nonoverlap(video_mem_base, |
| 365 | video_mem_size_mb << 20); |
| 366 | |
| 367 | /* |
| 368 | * Clear the old regions now being exposed. The following cases |
| 369 | * can occur - |
| 370 | * |
| 371 | * 1. clear whole old region (no overlap with new region) |
| 372 | * 2. clear old sub-region below new base |
| 373 | * 3. clear old sub-region above new end |
| 374 | */ |
| 375 | INFO("Cleaning previous Video Memory Carveout\n"); |
| 376 | |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 377 | if ((phys_base > vmem_end_old) || (video_mem_base > vmem_end_new)) { |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 378 | tegra_clear_videomem(video_mem_base, |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 379 | (uint32_t)video_mem_size_mb << 20U); |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 380 | } else { |
| 381 | if (video_mem_base < phys_base) { |
| 382 | non_overlap_area_size = phys_base - video_mem_base; |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 383 | tegra_clear_videomem(video_mem_base, |
| 384 | (uint32_t)non_overlap_area_size); |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 385 | } |
| 386 | if (vmem_end_old > vmem_end_new) { |
| 387 | non_overlap_area_size = vmem_end_old - vmem_end_new; |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 388 | tegra_clear_videomem(vmem_end_new, |
| 389 | (uint32_t)non_overlap_area_size); |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | done: |
| 394 | /* program the Videomem aperture */ |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 395 | tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_LO, (uint32_t)phys_base); |
| 396 | tegra_mc_write_32(MC_VIDEO_PROTECT_BASE_HI, |
| 397 | (uint32_t)(phys_base >> 32)); |
Varun Wadekar | 7058aee | 2016-04-25 09:01:46 -0700 | [diff] [blame] | 398 | tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20); |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 399 | |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 400 | /* unlock the previous locked nonoverlapping aperture */ |
| 401 | tegra_unlock_videomem_nonoverlap(); |
| 402 | |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 403 | /* store new values */ |
| 404 | video_mem_base = phys_base; |
Varun Wadekar | 7058aee | 2016-04-25 09:01:46 -0700 | [diff] [blame] | 405 | video_mem_size_mb = size_in_bytes >> 20; |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 406 | |
| 407 | /* |
Varun Wadekar | 153982c | 2016-12-21 14:50:18 -0800 | [diff] [blame] | 408 | * MCE propagates the VideoMem configuration values across the |
Varun Wadekar | cd5a2f5 | 2015-09-20 15:08:22 +0530 | [diff] [blame] | 409 | * CCPLEX. |
| 410 | */ |
| 411 | mce_update_gsc_videomem(); |
| 412 | } |
Varun Wadekar | c92050b | 2017-03-29 14:57:29 -0700 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * This feature exists only for v1 of the Tegra Memory Controller. |
| 416 | */ |
| 417 | void tegra_memctrl_disable_ahb_redirection(void) |
| 418 | { |
| 419 | ; /* do nothing */ |
| 420 | } |
Harvey Hsieh | 359be95 | 2017-08-21 15:01:53 +0800 | [diff] [blame] | 421 | |
| 422 | void tegra_memctrl_clear_pending_interrupts(void) |
| 423 | { |
| 424 | ; /* do nothing */ |
| 425 | } |