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