blob: af0858f0bbd1a10a1dbeae5653fdd1d4bba0c503 [file] [log] [blame]
developer550bf5e2016-07-11 16:05:23 +08001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
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#include <arm_gic.h>
31#include <assert.h>
32#include <arch_helpers.h>
33#include <bl_common.h>
34#include <cci.h>
Masahiro Yamada0fac5af2016-12-28 16:11:41 +090035#include <common_def.h>
developer550bf5e2016-07-11 16:05:23 +080036#include <console.h>
37#include <context_mgmt.h>
38#include <debug.h>
39#include <generic_delay_timer.h>
40#include <mcucfg.h>
41#include <mmio.h>
42#include <mtk_sip_svc.h>
43#include <mtk_plat_common.h>
44#include <mt_cpuxgpt.h>
45#include <platform.h>
46#include <plat_private.h>
47#include <string.h>
48#include <xlat_tables.h>
49/*******************************************************************************
50 * Declarations of linker defined symbols which will help us find the layout
51 * of trusted SRAM
52 ******************************************************************************/
53unsigned long __RO_START__;
54unsigned long __RO_END__;
55
developer550bf5e2016-07-11 16:05:23 +080056/*
57 * The next 2 constants identify the extents of the code & RO data region.
58 * These addresses are used by the MMU setup code and therefore they must be
59 * page-aligned. It is the responsibility of the linker script to ensure that
60 * __RO_START__ and __RO_END__ linker symbols refer to page-aligned addresses.
61 */
62#define BL31_RO_BASE (unsigned long)(&__RO_START__)
63#define BL31_RO_LIMIT (unsigned long)(&__RO_END__)
64
65/*
developer550bf5e2016-07-11 16:05:23 +080066 * Placeholder variables for copying the arguments that have been passed to
67 * BL3-1 from BL2.
68 */
69static entry_point_info_t bl32_image_ep_info;
70static entry_point_info_t bl33_image_ep_info;
71
72static const int cci_map[] = {
73 PLAT_MT_CCI_CLUSTER0_SL_IFACE_IX,
74 PLAT_MT_CCI_CLUSTER1_SL_IFACE_IX
75};
76
77static uint32_t cci_map_length = ARRAY_SIZE(cci_map);
78
79/* Table of regions to map using the MMU. */
80static const mmap_region_t plat_mmap[] = {
81 /* for TF text, RO, RW */
82 MAP_REGION_FLAT(MTK_DEV_RNG0_BASE, MTK_DEV_RNG0_SIZE,
83 MT_DEVICE | MT_RW | MT_SECURE),
84 MAP_REGION_FLAT(MTK_DEV_RNG1_BASE, MTK_DEV_RNG1_SIZE,
85 MT_DEVICE | MT_RW | MT_SECURE),
86 MAP_REGION_FLAT(RAM_CONSOLE_BASE & ~(PAGE_SIZE_MASK), RAM_CONSOLE_SIZE,
87 MT_DEVICE | MT_RW | MT_NS),
88 { 0 }
89
90};
91
92/*******************************************************************************
93 * Macro generating the code for the function setting up the pagetables as per
94 * the platform memory map & initialize the mmu, for the given exception level
95 ******************************************************************************/
96#define DEFINE_CONFIGURE_MMU_EL(_el) \
97 void plat_configure_mmu_el ## _el(unsigned long total_base, \
98 unsigned long total_size, \
99 unsigned long ro_start, \
100 unsigned long ro_limit, \
101 unsigned long coh_start, \
102 unsigned long coh_limit) \
103 { \
104 mmap_add_region(total_base, total_base, \
105 total_size, \
106 MT_MEMORY | MT_RW | MT_SECURE); \
107 mmap_add_region(ro_start, ro_start, \
108 ro_limit - ro_start, \
109 MT_MEMORY | MT_RO | MT_SECURE); \
110 mmap_add_region(coh_start, coh_start, \
111 coh_limit - coh_start, \
112 MT_DEVICE | MT_RW | MT_SECURE); \
113 mmap_add(plat_mmap); \
114 init_xlat_tables(); \
115 \
116 enable_mmu_el ## _el(0); \
117 }
118
119/* Define EL3 variants of the function initialising the MMU */
120DEFINE_CONFIGURE_MMU_EL(3)
121
122unsigned int plat_get_syscnt_freq2(void)
123{
124 return SYS_COUNTER_FREQ_IN_TICKS;
125}
126
127void plat_cci_init(void)
128{
129 /* Initialize CCI driver */
130 cci_init(PLAT_MT_CCI_BASE, cci_map, cci_map_length);
131}
132
133void plat_cci_enable(void)
134{
135 /*
136 * Enable CCI coherency for this cluster.
137 * No need for locks as no other cpu is active at the moment.
138 */
139 cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
140}
141
142void plat_cci_disable(void)
143{
144 cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
145}
146
147
148static void platform_setup_cpu(void)
149{
150 /* setup big cores */
151 mmio_write_32((uintptr_t)&mt6795_mcucfg->mp1_config_res,
152 MP1_DIS_RGU0_WAIT_PD_CPUS_L1_ACK |
153 MP1_DIS_RGU1_WAIT_PD_CPUS_L1_ACK |
154 MP1_DIS_RGU2_WAIT_PD_CPUS_L1_ACK |
155 MP1_DIS_RGU3_WAIT_PD_CPUS_L1_ACK |
156 MP1_DIS_RGU_NOCPU_WAIT_PD_CPUS_L1_ACK);
157 mmio_setbits_32((uintptr_t)&mt6795_mcucfg->mp1_miscdbg, MP1_AINACTS);
158 mmio_setbits_32((uintptr_t)&mt6795_mcucfg->mp1_clkenm_div,
159 MP1_SW_CG_GEN);
160 mmio_clrbits_32((uintptr_t)&mt6795_mcucfg->mp1_rst_ctl,
161 MP1_L2RSTDISABLE);
162
163 /* set big cores arm64 boot mode */
164 mmio_setbits_32((uintptr_t)&mt6795_mcucfg->mp1_cpucfg,
165 MP1_CPUCFG_64BIT);
166
167 /* set LITTLE cores arm64 boot mode */
168 mmio_setbits_32((uintptr_t)&mt6795_mcucfg->mp0_rv_addr[0].rv_addr_hw,
169 MP0_CPUCFG_64BIT);
170}
171
172/*******************************************************************************
173 * Return a pointer to the 'entry_point_info' structure of the next image for
174 * the security state specified. BL33 corresponds to the non-secure image type
175 * while BL32 corresponds to the secure image type. A NULL pointer is returned
176 * if the image does not exist.
177 ******************************************************************************/
178entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
179{
180 entry_point_info_t *next_image_info;
181
182 next_image_info = (type == NON_SECURE) ?
183 &bl33_image_ep_info : &bl32_image_ep_info;
184
185 /* None of the images on this platform can have 0x0 as the entrypoint */
186 if (next_image_info->pc)
187 return next_image_info;
188 else
189 return NULL;
190}
191
192/*******************************************************************************
193 * Perform any BL3-1 early platform setup. Here is an opportunity to copy
194 * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
195 * are lost (potentially). This needs to be done before the MMU is initialized
196 * so that the memory layout can be used while creating page tables.
197 * BL2 has flushed this information to memory, so we are guaranteed to pick up
198 * good data.
199 ******************************************************************************/
200void bl31_early_platform_setup(bl31_params_t *from_bl2,
201 void *plat_params_from_bl2)
202{
203 struct mtk_bl_param_t *pmtk_bl_param =
204 (struct mtk_bl_param_t *)from_bl2;
205 struct atf_arg_t *teearg;
206 unsigned long long normal_base;
207 unsigned long long atf_base;
208
209 assert(from_bl2 != NULL);
210 /*
211 * Mediatek preloader(i.e, BL2) is in 32 bit state, high 32bits
212 * of 64 bit GP registers are UNKNOWN if CPU warm reset from 32 bit
213 * to 64 bit state. So we need to clear high 32bit,
214 * which may be random value.
215 */
216 pmtk_bl_param =
217 (struct mtk_bl_param_t *)((uint64_t)pmtk_bl_param & 0x00000000ffffffff);
218 plat_params_from_bl2 =
219 (void *)((uint64_t)plat_params_from_bl2 & 0x00000000ffffffff);
220
221 teearg = (struct atf_arg_t *)pmtk_bl_param->tee_info_addr;
222
223 console_init(teearg->atf_log_port, UART_CLOCK, UART_BAUDRATE);
224 memcpy((void *)&gteearg, (void *)teearg, sizeof(struct atf_arg_t));
225
226 normal_base = 0;
227 /* in ATF boot time, timer for cntpct_el0 is not initialized
228 * so it will not count now.
229 */
230 atf_base = read_cntpct_el0();
231 sched_clock_init(normal_base, atf_base);
232
233 VERBOSE("bl31_setup\n");
234
235 /* Populate entry point information for BL3-2 and BL3-3 */
236 SET_PARAM_HEAD(&bl32_image_ep_info,
237 PARAM_EP,
238 VERSION_1,
239 0);
240 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
241 bl32_image_ep_info.pc = BL32_BASE;
242
243 SET_PARAM_HEAD(&bl33_image_ep_info,
244 PARAM_EP,
245 VERSION_1,
246 0);
247 /*
248 * Tell BL3-1 where the non-trusted software image
249 * is located and the entry state information
250 */
251 /* BL33_START_ADDRESS */
252 bl33_image_ep_info.pc = pmtk_bl_param->bl33_start_addr;
253 bl33_image_ep_info.spsr = plat_get_spsr_for_bl33_entry();
254 bl33_image_ep_info.args.arg4 = pmtk_bl_param->bootarg_loc;
255 bl33_image_ep_info.args.arg5 = pmtk_bl_param->bootarg_size;
256 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
257}
258/*******************************************************************************
259 * Perform any BL3-1 platform setup code
260 ******************************************************************************/
261
262void bl31_platform_setup(void)
263{
264 platform_setup_cpu();
265
266 generic_delay_timer_init();
267
268 plat_mt_gic_driver_init();
269 /* Initialize the gic cpu and distributor interfaces */
270 plat_mt_gic_init();
271
272 /* Topologies are best known to the platform. */
273 mt_setup_topology();
274}
275/*******************************************************************************
276 * Perform the very early platform specific architectural setup here. At the
277 * moment this is only intializes the mmu in a quick and dirty way.
278 * Init MTK propiartary log buffer control field.
279 ******************************************************************************/
280void bl31_plat_arch_setup(void)
281{
282 /* Enable non-secure access to CCI-400 registers */
283 mmio_write_32(CCI400_BASE + CCI_SEC_ACCESS_OFFSET, 0x1);
284
285 plat_cci_init();
286 plat_cci_enable();
287
288 if (gteearg.atf_log_buf_size != 0) {
289 INFO("mmap atf buffer : 0x%x, 0x%x\n\r",
290 gteearg.atf_log_buf_start,
291 gteearg.atf_log_buf_size);
292
293 mmap_add_region(
294 gteearg.atf_log_buf_start &
295 ~(PAGE_SIZE_2MB_MASK),
296 gteearg.atf_log_buf_start &
297 ~(PAGE_SIZE_2MB_MASK),
298 PAGE_SIZE_2MB,
299 MT_DEVICE | MT_RW | MT_NS);
300
301 INFO("mmap atf buffer (force 2MB aligned):0x%x, 0x%x\n",
302 (gteearg.atf_log_buf_start & ~(PAGE_SIZE_2MB_MASK)),
303 PAGE_SIZE_2MB);
304 }
305 /*
306 * add TZRAM_BASE to memory map
307 * then set RO and COHERENT to different attribute
308 */
309 plat_configure_mmu_el3(
310 (TZRAM_BASE & ~(PAGE_SIZE_MASK)),
311 (TZRAM_SIZE & ~(PAGE_SIZE_MASK)),
312 (BL31_RO_BASE & ~(PAGE_SIZE_MASK)),
313 BL31_RO_LIMIT,
Masahiro Yamada0fac5af2016-12-28 16:11:41 +0900314 BL_COHERENT_RAM_BASE,
315 BL_COHERENT_RAM_END);
developer550bf5e2016-07-11 16:05:23 +0800316 /* Initialize for ATF log buffer */
317 if (gteearg.atf_log_buf_size != 0) {
318 gteearg.atf_aee_debug_buf_size = ATF_AEE_BUFFER_SIZE;
319 gteearg.atf_aee_debug_buf_start =
320 gteearg.atf_log_buf_start +
321 gteearg.atf_log_buf_size - ATF_AEE_BUFFER_SIZE;
322 INFO("ATF log service is registered (0x%x, aee:0x%x)\n",
323 gteearg.atf_log_buf_start,
324 gteearg.atf_aee_debug_buf_start);
325 } else{
326 gteearg.atf_aee_debug_buf_size = 0;
327 gteearg.atf_aee_debug_buf_start = 0;
328 }
329
330 /* Platform code before bl31_main */
331 /* compatible to the earlier chipset */
332
333 /* Show to ATF log buffer & UART */
334 INFO("BL3-1: %s\n", version_string);
335 INFO("BL3-1: %s\n", build_message);
336
337}
338#if 0
339/* MTK Define */
340#define ACTLR_CPUECTLR_BIT (1 << 1)
341
342void enable_ns_access_to_cpuectlr(void)
343{
344 unsigned int next_actlr;
345
346
347 /* ACTLR_EL1 do not implement CUPECTLR */
348 next_actlr = read_actlr_el2();
349 next_actlr |= ACTLR_CPUECTLR_BIT;
350 write_actlr_el2(next_actlr);
351
352 next_actlr = read_actlr_el3();
353 next_actlr |= ACTLR_CPUECTLR_BIT;
354 write_actlr_el3(next_actlr);
355}
356#endif
357/*******************************************************************************
358 * This function prepare boot argument for 64 bit kernel entry
359 ******************************************************************************/
360static entry_point_info_t *bl31_plat_get_next_kernel64_ep_info(void)
361{
362 entry_point_info_t *next_image_info;
363 unsigned long el_status;
364 unsigned int mode;
365
366 el_status = 0;
367 mode = 0;
368
369 /* Kernel image is always non-secured */
370 next_image_info = &bl33_image_ep_info;
371
372 /* Figure out what mode we enter the non-secure world in */
373 el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
374 el_status &= ID_AA64PFR0_ELX_MASK;
375
376 if (el_status) {
377 INFO("Kernel_EL2\n");
378 mode = MODE_EL2;
379 } else{
380 INFO("Kernel_EL1\n");
381 mode = MODE_EL1;
382 }
383
384 INFO("Kernel is 64Bit\n");
385 next_image_info->spsr =
386 SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
387 next_image_info->pc = get_kernel_info_pc();
388 next_image_info->args.arg0 = get_kernel_info_r0();
389 next_image_info->args.arg1 = get_kernel_info_r1();
390
391 INFO("pc=0x%lx, r0=0x%lx, r1=0x%lx\n",
392 next_image_info->pc,
393 next_image_info->args.arg0,
394 next_image_info->args.arg1);
395
396
397 SET_SECURITY_STATE(next_image_info->h.attr, NON_SECURE);
398
399 /* None of the images on this platform can have 0x0 as the entrypoint */
400 if (next_image_info->pc)
401 return next_image_info;
402 else
403 return NULL;
404}
405
406/*******************************************************************************
407 * This function prepare boot argument for 32 bit kernel entry
408 ******************************************************************************/
409static entry_point_info_t *bl31_plat_get_next_kernel32_ep_info(void)
410{
411 entry_point_info_t *next_image_info;
412 unsigned int mode;
413
414 mode = 0;
415
416 /* Kernel image is always non-secured */
417 next_image_info = &bl33_image_ep_info;
418
419 /* Figure out what mode we enter the non-secure world in */
420 mode = MODE32_hyp;
421 /*
422 * TODO: Consider the possibility of specifying the SPSR in
423 * the FIP ToC and allowing the platform to have a say as
424 * well.
425 */
426
427 INFO("Kernel is 32Bit\n");
428 next_image_info->spsr =
429 SPSR_MODE32(mode, SPSR_T_ARM, SPSR_E_LITTLE,
430 (DAIF_FIQ_BIT | DAIF_IRQ_BIT | DAIF_ABT_BIT));
431 next_image_info->pc = get_kernel_info_pc();
432 next_image_info->args.arg0 = get_kernel_info_r0();
433 next_image_info->args.arg1 = get_kernel_info_r1();
434 next_image_info->args.arg2 = get_kernel_info_r2();
435
436 INFO("pc=0x%lx, r0=0x%lx, r1=0x%lx, r2=0x%lx\n",
437 next_image_info->pc,
438 next_image_info->args.arg0,
439 next_image_info->args.arg1,
440 next_image_info->args.arg2);
441
442
443 SET_SECURITY_STATE(next_image_info->h.attr, NON_SECURE);
444
445 /* None of the images on this platform can have 0x0 as the entrypoint */
446 if (next_image_info->pc)
447 return next_image_info;
448 else
449 return NULL;
450}
451
452/*******************************************************************************
453 * This function prepare boot argument for kernel entrypoint
454 ******************************************************************************/
455void bl31_prepare_kernel_entry(uint64_t k32_64)
456{
457 entry_point_info_t *next_image_info;
458 uint32_t image_type;
459
460 /* Determine which image to execute next */
461 /* image_type = bl31_get_next_image_type(); */
462 image_type = NON_SECURE;
463
464 /* Program EL3 registers to enable entry into the next EL */
465 if (k32_64 == 0)
466 next_image_info = bl31_plat_get_next_kernel32_ep_info();
467 else
468 next_image_info = bl31_plat_get_next_kernel64_ep_info();
469
470 assert(next_image_info);
471 assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr));
472
473 INFO("BL3-1: Preparing for EL3 exit to %s world, Kernel\n",
474 (image_type == SECURE) ? "secure" : "normal");
475 INFO("BL3-1: Next image address = 0x%llx\n",
476 (unsigned long long) next_image_info->pc);
477 INFO("BL3-1: Next image spsr = 0x%x\n", next_image_info->spsr);
478 cm_init_context(read_mpidr_el1(), next_image_info);
479 cm_prepare_el3_exit(image_type);
480}