Patrick Georgi | 40a3e32 | 2015-06-22 19:41:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc. |
| 18 | */ |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <arch/io.h> |
| 22 | #include <console/console.h> |
| 23 | #include <soc/addressmap.h> |
| 24 | #include <soc/id.h> |
| 25 | #include <soc/mc.h> |
| 26 | #include <soc/sdram.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <symbols.h> |
| 29 | #include <soc/nvidia/tegra/types.h> |
| 30 | |
| 31 | static uintptr_t tz_base_mib; |
| 32 | static const size_t tz_size_mib = CONFIG_TRUSTZONE_CARVEOUT_SIZE_MB; |
| 33 | |
| 34 | /* returns total amount of DRAM (in MB) from memory controller registers */ |
| 35 | int sdram_size_mb(void) |
| 36 | { |
| 37 | struct tegra_mc_regs *mc = (struct tegra_mc_regs *)TEGRA_MC_BASE; |
| 38 | static int total_size = 0; |
| 39 | |
| 40 | if (total_size) |
| 41 | return total_size; |
| 42 | |
| 43 | /* |
| 44 | * This obtains memory size from the External Memory Aperture |
| 45 | * Configuration register. Nvidia confirmed that it is safe to assume |
| 46 | * this value represents the total physical DRAM size. |
| 47 | */ |
| 48 | total_size = (read32(&mc->emem_cfg) >> |
| 49 | MC_EMEM_CFG_SIZE_MB_SHIFT) & MC_EMEM_CFG_SIZE_MB_MASK; |
| 50 | |
| 51 | return total_size; |
| 52 | } |
| 53 | |
| 54 | static void carveout_from_regs(uintptr_t *base_mib, size_t *size_mib, |
| 55 | uint32_t bom, uint32_t bom_hi, uint32_t size) |
| 56 | { |
| 57 | |
| 58 | /* All size regs of carveouts are in MiB. */ |
| 59 | if (size == 0) |
| 60 | return; |
| 61 | |
| 62 | *size_mib = size; |
| 63 | bom >>= 20; |
| 64 | bom |= bom_hi << (32 - 20); |
| 65 | |
| 66 | *base_mib = bom; |
| 67 | } |
| 68 | |
| 69 | void carveout_range(int id, uintptr_t *base_mib, size_t *size_mib) |
| 70 | { |
| 71 | *base_mib = 0; |
| 72 | *size_mib = 0; |
| 73 | struct tegra_mc_regs * const mc = (struct tegra_mc_regs *)TEGRA_MC_BASE; |
| 74 | size_t region_size_mb; |
| 75 | |
| 76 | switch (id) { |
| 77 | case CARVEOUT_TZ: |
| 78 | *base_mib = tz_base_mib; |
| 79 | *size_mib = tz_size_mib; |
| 80 | break; |
| 81 | case CARVEOUT_SEC: |
| 82 | carveout_from_regs(base_mib, size_mib, |
| 83 | read32(&mc->sec_carveout_bom), |
| 84 | read32(&mc->sec_carveout_adr_hi), |
| 85 | read32(&mc->sec_carveout_size_mb)); |
| 86 | break; |
| 87 | case CARVEOUT_MTS: |
| 88 | carveout_from_regs(base_mib, size_mib, |
| 89 | read32(&mc->mts_carveout_bom), |
| 90 | read32(&mc->mts_carveout_adr_hi), |
| 91 | read32(&mc->mts_carveout_size_mb)); |
| 92 | break; |
| 93 | case CARVEOUT_VPR: |
| 94 | carveout_from_regs(base_mib, size_mib, |
| 95 | read32(&mc->video_protect_bom), |
| 96 | read32(&mc->video_protect_bom_adr_hi), |
| 97 | read32(&mc->video_protect_size_mb)); |
| 98 | break; |
| 99 | case CARVEOUT_GPU: |
| 100 | /* These carveout regs use 128KB granularity - convert to MB */ |
| 101 | region_size_mb = DIV_ROUND_UP(read32(&mc->security_carveout2_size_128kb), 8); |
| 102 | |
| 103 | /* BOM address set in gpu_region_init, below */ |
| 104 | carveout_from_regs(base_mib, size_mib, |
| 105 | read32(&mc->security_carveout2_bom), |
| 106 | read32(&mc->security_carveout2_bom_hi), |
| 107 | region_size_mb); |
| 108 | break; |
| 109 | default: |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void print_carveouts(void) |
| 115 | { |
| 116 | int i; |
| 117 | printk(BIOS_INFO, "Carveout ranges:\n"); |
| 118 | for (i = 0; i < CARVEOUT_NUM; i++) { |
| 119 | uintptr_t base, end; |
| 120 | size_t size; |
| 121 | carveout_range(i, &base, &size); |
| 122 | end = base + size; |
| 123 | if (end && base) |
| 124 | printk(BIOS_INFO, "ID:%d [%lx - %lx)\n", i, |
| 125 | (unsigned long)base * MiB, |
| 126 | (unsigned long)end * MiB); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Memory Map is as follows |
| 132 | * |
| 133 | * ------------------------------ <-- Start of DRAM |
| 134 | * | | |
| 135 | * | Available DRAM | |
| 136 | * |____________________________| |
| 137 | * | | |
| 138 | * | CBMEM | |
| 139 | * |____________________________| |
| 140 | * | | |
| 141 | * | Other carveouts | |
| 142 | * | (with dynamic allocation) | |
| 143 | * |____________________________| |
| 144 | * | | |
| 145 | * | TZ carveout of size | |
| 146 | * | TRUSTZONE_CARVEOUT_SIZE_MB | |
| 147 | * |____________________________| <-- 0x100000000 |
| 148 | * | | |
| 149 | * | Available DRAM | |
| 150 | * | | |
| 151 | * ------------------------------ <-- End of DRAM |
| 152 | * |
| 153 | */ |
| 154 | static void memory_in_range(uintptr_t *base_mib, uintptr_t *end_mib, |
| 155 | int ignore_carveout_id) |
| 156 | { |
| 157 | uintptr_t base; |
| 158 | uintptr_t end; |
| 159 | int i; |
| 160 | |
| 161 | base = (uintptr_t)_dram / MiB; |
| 162 | end = base + sdram_size_mb(); |
| 163 | |
| 164 | /* Requested limits out of range. */ |
| 165 | if (*end_mib <= base || *base_mib >= end) { |
| 166 | *end_mib = *base_mib = 0; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | /* Clip region to passed in limits. */ |
| 171 | if (*end_mib < end) |
| 172 | end = *end_mib; |
| 173 | if (*base_mib > base) |
| 174 | base = *base_mib; |
| 175 | |
| 176 | for (i = 0; i < CARVEOUT_NUM; i++) { |
| 177 | uintptr_t carveout_base; |
| 178 | uintptr_t carveout_end; |
| 179 | size_t carveout_size; |
| 180 | |
| 181 | if (i == ignore_carveout_id) |
| 182 | continue; |
| 183 | |
| 184 | carveout_range(i, &carveout_base, &carveout_size); |
| 185 | |
| 186 | if (carveout_size == 0) |
| 187 | continue; |
| 188 | |
| 189 | carveout_end = carveout_base + carveout_size; |
| 190 | |
| 191 | /* Bypass carveouts out of requested range. */ |
| 192 | if (carveout_base >= end || carveout_end <= base) |
| 193 | continue; |
| 194 | |
| 195 | /* |
| 196 | * This is crude, but the assumption is that carveouts live |
| 197 | * at the upper range of physical memory. Therefore, update |
| 198 | * the end address to be equal to the base of the carveout. |
| 199 | */ |
| 200 | end = carveout_base; |
| 201 | } |
| 202 | |
| 203 | *base_mib = base; |
| 204 | *end_mib = end; |
| 205 | } |
| 206 | |
| 207 | void memory_in_range_below_4gb(uintptr_t *base_mib, uintptr_t *end_mib) |
| 208 | { |
| 209 | *base_mib = 0; |
| 210 | *end_mib = 4096; |
| 211 | memory_in_range(base_mib, end_mib, CARVEOUT_NUM); |
| 212 | } |
| 213 | |
| 214 | void memory_in_range_above_4gb(uintptr_t *base_mib, uintptr_t *end_mib) |
| 215 | { |
| 216 | *base_mib = 4096; |
| 217 | *end_mib = ~0UL; |
| 218 | memory_in_range(base_mib, end_mib, CARVEOUT_NUM); |
| 219 | } |
| 220 | |
| 221 | void trustzone_region_init(void) |
| 222 | { |
| 223 | struct tegra_mc_regs * const mc = (void *)(uintptr_t)TEGRA_MC_BASE; |
| 224 | uintptr_t end = 4096; |
| 225 | |
| 226 | /* Already has been initialized. */ |
| 227 | if (tz_size_mib != 0 && tz_base_mib != 0) |
| 228 | return; |
| 229 | |
| 230 | /* |
| 231 | * Get memory layout below 4GiB ignoring the TZ carveout because |
| 232 | * that's the one to initialize. |
| 233 | */ |
| 234 | tz_base_mib = end - tz_size_mib; |
| 235 | memory_in_range(&tz_base_mib, &end, CARVEOUT_TZ); |
| 236 | |
| 237 | /* |
| 238 | * IMPORTANT!!!!! |
| 239 | * We need to ensure that trustzone region is located at the end of |
| 240 | * 32-bit address space. If any carveout is allocated space before |
| 241 | * trustzone_region_init is called, then this assert will ensure that |
| 242 | * the boot flow fails. If you are here because of this assert, please |
| 243 | * move your call to initialize carveout after trustzone_region_init in |
| 244 | * romstage and ramstage. |
| 245 | */ |
| 246 | assert(end == 4096); |
| 247 | |
| 248 | /* AVP cannot set the TZ registers proper as it is always non-secure. */ |
| 249 | if (context_avp()) |
| 250 | return; |
| 251 | |
| 252 | /* Set the carveout region. */ |
| 253 | write32(&mc->security_cfg0, tz_base_mib << 20); |
| 254 | write32(&mc->security_cfg1, tz_size_mib); |
| 255 | |
| 256 | /* Enable SMMU translations */ |
| 257 | write32(&mc->smmu_config, MC_SMMU_CONFIG_ENABLE); |
| 258 | } |
| 259 | |
| 260 | void gpu_region_init(void) |
| 261 | { |
| 262 | struct tegra_mc_regs * const mc = (void *)(uintptr_t)TEGRA_MC_BASE; |
| 263 | uintptr_t gpu_base_mib = 0, end = 4096; |
| 264 | size_t gpu_size_mib = GPU_CARVEOUT_SIZE_MB; |
| 265 | |
| 266 | /* Get memory layout below 4GiB */ |
| 267 | memory_in_range(&gpu_base_mib, &end, CARVEOUT_GPU); |
| 268 | gpu_base_mib = end - gpu_size_mib; |
| 269 | |
| 270 | /* Set the carveout2 base address. Everything else has been set in the BCT cfg/inc */ |
| 271 | write32(&mc->security_carveout2_bom, gpu_base_mib << 20); |
| 272 | write32(&mc->security_carveout2_bom_hi, 0); |
| 273 | |
| 274 | /* Set the locked bit. This will lock out any other writes! */ |
| 275 | setbits_le32(&mc->security_carveout2_cfg0, MC_SECURITY_CARVEOUT_LOCKED); |
| 276 | |
| 277 | /* Set the carveout3 base to 0, unused */ |
| 278 | write32(&mc->security_carveout3_bom, 0); |
| 279 | write32(&mc->security_carveout3_bom_hi, 0); |
| 280 | |
| 281 | /* Set the locked bit. This will lock out any other writes! */ |
| 282 | setbits_le32(&mc->security_carveout3_cfg0, MC_SECURITY_CARVEOUT_LOCKED); |
| 283 | } |