blob: c4170504c43801bbd2985fa053ff2fa9e267d9f2 [file] [log] [blame]
Varun Wadekarb316e242015-05-19 16:48:04 +05301/*
Douglas Raillard21362a92016-12-02 13:51:54 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekarb316e242015-05-19 16:48:04 +05303 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
Varun Wadekar7a269e22015-06-10 14:04:32 +053031#include <arch_helpers.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053032#include <assert.h>
33#include <debug.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053034#include <memctrl.h>
Varun Wadekar7a9a2852015-09-18 11:21:22 +053035#include <memctrl_v1.h>
36#include <mmio.h>
Varun Wadekar7a269e22015-06-10 14:04:32 +053037#include <string.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053038#include <tegra_def.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000039#include <utils.h>
Varun Wadekar7a269e22015-06-10 14:04:32 +053040#include <xlat_tables.h>
41
Varun Wadekar7a269e22015-06-10 14:04:32 +053042#define TEGRA_GPU_RESET_REG_OFFSET 0x28c
43#define GPU_RESET_BIT (1 << 24)
44
45/* Video Memory base and size (live values) */
46static uintptr_t video_mem_base;
47static uint64_t video_mem_size;
Varun Wadekarb316e242015-05-19 16:48:04 +053048
49/*
50 * Init SMMU.
51 */
52void tegra_memctrl_setup(void)
53{
54 /*
55 * Setup the Memory controller to allow only secure accesses to
56 * the TZDRAM carveout
57 */
Varun Wadekar7a9a2852015-09-18 11:21:22 +053058 INFO("Tegra Memory Controller (v1)\n");
Varun Wadekarb316e242015-05-19 16:48:04 +053059
60 /* allow translations for all MC engines */
61 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_0_0,
62 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
63 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_1_0,
64 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
65 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_2_0,
66 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
67 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_3_0,
68 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
69 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_4_0,
70 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
71
72 tegra_mc_write_32(MC_SMMU_ASID_SECURITY_0, MC_SMMU_ASID_SECURITY);
73
74 tegra_mc_write_32(MC_SMMU_TLB_CONFIG_0, MC_SMMU_TLB_CONFIG_0_RESET_VAL);
75 tegra_mc_write_32(MC_SMMU_PTC_CONFIG_0, MC_SMMU_PTC_CONFIG_0_RESET_VAL);
76
77 /* flush PTC and TLB */
78 tegra_mc_write_32(MC_SMMU_PTC_FLUSH_0, MC_SMMU_PTC_FLUSH_ALL);
79 (void)tegra_mc_read_32(MC_SMMU_CONFIG_0); /* read to flush writes */
80 tegra_mc_write_32(MC_SMMU_TLB_FLUSH_0, MC_SMMU_TLB_FLUSH_ALL);
81
82 /* enable SMMU */
83 tegra_mc_write_32(MC_SMMU_CONFIG_0,
84 MC_SMMU_CONFIG_0_SMMU_ENABLE_ENABLE);
85 (void)tegra_mc_read_32(MC_SMMU_CONFIG_0); /* read to flush writes */
Varun Wadekar7a269e22015-06-10 14:04:32 +053086
87 /* video memory carveout */
88 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE, video_mem_base);
89 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, video_mem_size);
Varun Wadekarb316e242015-05-19 16:48:04 +053090}
91
92/*
Varun Wadekar6eec6d62016-03-03 13:28:10 -080093 * Restore Memory Controller settings after "System Suspend"
94 */
95void tegra_memctrl_restore_settings(void)
96{
97 tegra_memctrl_setup();
98}
99
100/*
Varun Wadekarb316e242015-05-19 16:48:04 +0530101 * Secure the BL31 DRAM aperture.
102 *
103 * phys_base = physical base of TZDRAM aperture
104 * size_in_bytes = size of aperture in bytes
105 */
106void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes)
107{
108 /*
109 * Setup the Memory controller to allow only secure accesses to
110 * the TZDRAM carveout
111 */
112 INFO("Configuring TrustZone DRAM Memory Carveout\n");
113
114 tegra_mc_write_32(MC_SECURITY_CFG0_0, phys_base);
115 tegra_mc_write_32(MC_SECURITY_CFG1_0, size_in_bytes >> 20);
116}
Varun Wadekar7a269e22015-06-10 14:04:32 +0530117
Varun Wadekar0dc91812015-12-30 15:06:41 -0800118/*
119 * Secure the BL31 TZRAM aperture.
120 *
121 * phys_base = physical base of TZRAM aperture
122 * size_in_bytes = size of aperture in bytes
123 */
124void tegra_memctrl_tzram_setup(uint64_t phys_base, uint32_t size_in_bytes)
125{
126 /*
127 * The v1 hardware controller does not have any registers
128 * for setting up the on-chip TZRAM.
129 */
130}
131
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100132static void tegra_clear_videomem(uintptr_t non_overlap_area_start,
133 unsigned long long non_overlap_area_size)
134{
135 /*
136 * Perform cache maintenance to ensure that the non-overlapping area is
137 * zeroed out. The first invalidation of this range ensures that
138 * possible evictions of dirty cache lines do not interfere with the
Douglas Raillard21362a92016-12-02 13:51:54 +0000139 * 'zeromem' operation. Other CPUs could speculatively prefetch the
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100140 * main memory contents of this area between the first invalidation and
Douglas Raillard21362a92016-12-02 13:51:54 +0000141 * the 'zeromem' operation. The second invalidation ensures that any
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100142 * such cache lines are removed as well.
143 */
144 inv_dcache_range(non_overlap_area_start, non_overlap_area_size);
Douglas Raillard21362a92016-12-02 13:51:54 +0000145 zeromem((void *)non_overlap_area_start, non_overlap_area_size);
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100146 inv_dcache_range(non_overlap_area_start, non_overlap_area_size);
147}
148
Varun Wadekar7a269e22015-06-10 14:04:32 +0530149/*
150 * Program the Video Memory carveout region
151 *
152 * phys_base = physical base of aperture
153 * size_in_bytes = size of aperture in bytes
154 */
155void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes)
156{
157 uintptr_t vmem_end_old = video_mem_base + (video_mem_size << 20);
158 uintptr_t vmem_end_new = phys_base + size_in_bytes;
159 uint32_t regval;
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100160 unsigned long long non_overlap_area_size;
Varun Wadekar7a269e22015-06-10 14:04:32 +0530161
162 /*
163 * The GPU is the user of the Video Memory region. In order to
164 * transition to the new memory region smoothly, we program the
165 * new base/size ONLY if the GPU is in reset mode.
166 */
167 regval = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_GPU_RESET_REG_OFFSET);
168 if ((regval & GPU_RESET_BIT) == 0) {
169 ERROR("GPU not in reset! Video Memory setup failed\n");
170 return;
171 }
172
173 /*
174 * Setup the Memory controller to restrict CPU accesses to the Video
175 * Memory region
176 */
177 INFO("Configuring Video Memory Carveout\n");
178
179 /*
180 * Configure Memory Controller directly for the first time.
181 */
182 if (video_mem_base == 0)
183 goto done;
184
185 /*
186 * Clear the old regions now being exposed. The following cases
187 * can occur -
188 *
189 * 1. clear whole old region (no overlap with new region)
190 * 2. clear old sub-region below new base
191 * 3. clear old sub-region above new end
192 */
193 INFO("Cleaning previous Video Memory Carveout\n");
194
195 disable_mmu_el3();
Varun Wadekar1be2f972015-08-26 15:06:14 +0530196 if (phys_base > vmem_end_old || video_mem_base > vmem_end_new) {
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100197 tegra_clear_videomem(video_mem_base, video_mem_size << 20);
Varun Wadekar1be2f972015-08-26 15:06:14 +0530198 } else {
199 if (video_mem_base < phys_base) {
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100200 non_overlap_area_size = phys_base - video_mem_base;
201 tegra_clear_videomem(video_mem_base, non_overlap_area_size);
Varun Wadekar1be2f972015-08-26 15:06:14 +0530202 }
203 if (vmem_end_old > vmem_end_new) {
Vikram Kanigiri4489ad12015-09-10 14:12:36 +0100204 non_overlap_area_size = vmem_end_old - vmem_end_new;
205 tegra_clear_videomem(vmem_end_new, non_overlap_area_size);
Varun Wadekar1be2f972015-08-26 15:06:14 +0530206 }
207 }
Varun Wadekar7a269e22015-06-10 14:04:32 +0530208 enable_mmu_el3(0);
209
210done:
211 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE, phys_base);
212 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20);
213
214 /* store new values */
215 video_mem_base = phys_base;
216 video_mem_size = size_in_bytes >> 20;
217}