blob: 689f2d7cd9bd78555fe7b7593f8e4e797a7e807e [file] [log] [blame]
Varun Wadekar08438e22015-05-19 16:48:04 +05301/*
Douglas Raillard308d3592016-12-02 13:51:54 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekar08438e22015-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 Wadekar9a964512015-06-10 14:04:32 +053031#include <arch_helpers.h>
Varun Wadekar08438e22015-05-19 16:48:04 +053032#include <assert.h>
33#include <debug.h>
34#include <mmio.h>
35#include <memctrl.h>
Varun Wadekar9a964512015-06-10 14:04:32 +053036#include <string.h>
Varun Wadekar08438e22015-05-19 16:48:04 +053037#include <tegra_def.h>
Varun Wadekar9a964512015-06-10 14:04:32 +053038#include <xlat_tables.h>
39
Varun Wadekar9a964512015-06-10 14:04:32 +053040#define TEGRA_GPU_RESET_REG_OFFSET 0x28c
41#define GPU_RESET_BIT (1 << 24)
42
43/* Video Memory base and size (live values) */
44static uintptr_t video_mem_base;
45static uint64_t video_mem_size;
Varun Wadekar08438e22015-05-19 16:48:04 +053046
47/*
48 * Init SMMU.
49 */
50void tegra_memctrl_setup(void)
51{
52 /*
53 * Setup the Memory controller to allow only secure accesses to
54 * the TZDRAM carveout
55 */
56 INFO("Configuring SMMU\n");
57
58 /* allow translations for all MC engines */
59 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_0_0,
60 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
61 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_1_0,
62 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
63 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_2_0,
64 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
65 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_3_0,
66 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
67 tegra_mc_write_32(MC_SMMU_TRANSLATION_ENABLE_4_0,
68 (unsigned int)MC_SMMU_TRANSLATION_ENABLE);
69
70 tegra_mc_write_32(MC_SMMU_ASID_SECURITY_0, MC_SMMU_ASID_SECURITY);
71
72 tegra_mc_write_32(MC_SMMU_TLB_CONFIG_0, MC_SMMU_TLB_CONFIG_0_RESET_VAL);
73 tegra_mc_write_32(MC_SMMU_PTC_CONFIG_0, MC_SMMU_PTC_CONFIG_0_RESET_VAL);
74
75 /* flush PTC and TLB */
76 tegra_mc_write_32(MC_SMMU_PTC_FLUSH_0, MC_SMMU_PTC_FLUSH_ALL);
77 (void)tegra_mc_read_32(MC_SMMU_CONFIG_0); /* read to flush writes */
78 tegra_mc_write_32(MC_SMMU_TLB_FLUSH_0, MC_SMMU_TLB_FLUSH_ALL);
79
80 /* enable SMMU */
81 tegra_mc_write_32(MC_SMMU_CONFIG_0,
82 MC_SMMU_CONFIG_0_SMMU_ENABLE_ENABLE);
83 (void)tegra_mc_read_32(MC_SMMU_CONFIG_0); /* read to flush writes */
Varun Wadekar9a964512015-06-10 14:04:32 +053084
85 /* video memory carveout */
86 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE, video_mem_base);
87 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, video_mem_size);
Varun Wadekar08438e22015-05-19 16:48:04 +053088}
89
90/*
91 * Secure the BL31 DRAM aperture.
92 *
93 * phys_base = physical base of TZDRAM aperture
94 * size_in_bytes = size of aperture in bytes
95 */
96void tegra_memctrl_tzdram_setup(uint64_t phys_base, uint32_t size_in_bytes)
97{
98 /*
99 * Setup the Memory controller to allow only secure accesses to
100 * the TZDRAM carveout
101 */
102 INFO("Configuring TrustZone DRAM Memory Carveout\n");
103
104 tegra_mc_write_32(MC_SECURITY_CFG0_0, phys_base);
105 tegra_mc_write_32(MC_SECURITY_CFG1_0, size_in_bytes >> 20);
106}
Varun Wadekar9a964512015-06-10 14:04:32 +0530107
Vikram Kanigirie3616812015-09-10 14:12:36 +0100108static void tegra_clear_videomem(uintptr_t non_overlap_area_start,
109 unsigned long long non_overlap_area_size)
110{
111 /*
112 * Perform cache maintenance to ensure that the non-overlapping area is
113 * zeroed out. The first invalidation of this range ensures that
114 * possible evictions of dirty cache lines do not interfere with the
Douglas Raillard308d3592016-12-02 13:51:54 +0000115 * 'zeromem' operation. Other CPUs could speculatively prefetch the
Vikram Kanigirie3616812015-09-10 14:12:36 +0100116 * main memory contents of this area between the first invalidation and
Douglas Raillard308d3592016-12-02 13:51:54 +0000117 * the 'zeromem' operation. The second invalidation ensures that any
Vikram Kanigirie3616812015-09-10 14:12:36 +0100118 * such cache lines are removed as well.
119 */
120 inv_dcache_range(non_overlap_area_start, non_overlap_area_size);
Douglas Raillard308d3592016-12-02 13:51:54 +0000121 zeromem((void *)non_overlap_area_start, non_overlap_area_size);
Vikram Kanigirie3616812015-09-10 14:12:36 +0100122 inv_dcache_range(non_overlap_area_start, non_overlap_area_size);
123}
124
Varun Wadekar9a964512015-06-10 14:04:32 +0530125/*
126 * Program the Video Memory carveout region
127 *
128 * phys_base = physical base of aperture
129 * size_in_bytes = size of aperture in bytes
130 */
131void tegra_memctrl_videomem_setup(uint64_t phys_base, uint32_t size_in_bytes)
132{
133 uintptr_t vmem_end_old = video_mem_base + (video_mem_size << 20);
134 uintptr_t vmem_end_new = phys_base + size_in_bytes;
135 uint32_t regval;
Vikram Kanigirie3616812015-09-10 14:12:36 +0100136 unsigned long long non_overlap_area_size;
Varun Wadekar9a964512015-06-10 14:04:32 +0530137
138 /*
139 * The GPU is the user of the Video Memory region. In order to
140 * transition to the new memory region smoothly, we program the
141 * new base/size ONLY if the GPU is in reset mode.
142 */
143 regval = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_GPU_RESET_REG_OFFSET);
144 if ((regval & GPU_RESET_BIT) == 0) {
145 ERROR("GPU not in reset! Video Memory setup failed\n");
146 return;
147 }
148
149 /*
150 * Setup the Memory controller to restrict CPU accesses to the Video
151 * Memory region
152 */
153 INFO("Configuring Video Memory Carveout\n");
154
155 /*
156 * Configure Memory Controller directly for the first time.
157 */
158 if (video_mem_base == 0)
159 goto done;
160
161 /*
162 * Clear the old regions now being exposed. The following cases
163 * can occur -
164 *
165 * 1. clear whole old region (no overlap with new region)
166 * 2. clear old sub-region below new base
167 * 3. clear old sub-region above new end
168 */
169 INFO("Cleaning previous Video Memory Carveout\n");
170
171 disable_mmu_el3();
Varun Wadekard49b9c82015-08-26 15:06:14 +0530172 if (phys_base > vmem_end_old || video_mem_base > vmem_end_new) {
Vikram Kanigirie3616812015-09-10 14:12:36 +0100173 tegra_clear_videomem(video_mem_base, video_mem_size << 20);
Varun Wadekard49b9c82015-08-26 15:06:14 +0530174 } else {
175 if (video_mem_base < phys_base) {
Vikram Kanigirie3616812015-09-10 14:12:36 +0100176 non_overlap_area_size = phys_base - video_mem_base;
177 tegra_clear_videomem(video_mem_base, non_overlap_area_size);
Varun Wadekard49b9c82015-08-26 15:06:14 +0530178 }
179 if (vmem_end_old > vmem_end_new) {
Vikram Kanigirie3616812015-09-10 14:12:36 +0100180 non_overlap_area_size = vmem_end_old - vmem_end_new;
181 tegra_clear_videomem(vmem_end_new, non_overlap_area_size);
Varun Wadekard49b9c82015-08-26 15:06:14 +0530182 }
183 }
Varun Wadekar9a964512015-06-10 14:04:32 +0530184 enable_mmu_el3(0);
185
186done:
187 tegra_mc_write_32(MC_VIDEO_PROTECT_BASE, phys_base);
188 tegra_mc_write_32(MC_VIDEO_PROTECT_SIZE_MB, size_in_bytes >> 20);
189
190 /* store new values */
191 video_mem_base = phys_base;
192 video_mem_size = size_in_bytes >> 20;
193}