xref: /rk3399_ARM-atf/plat/arm/board/juno/aarch64/juno_helpers.S (revision 07570d592ea5b8a0ea22f23d2d502782b9d6c1c5)
185135283SDan Handley/*
21c3ea103SAntonio Nino Diaz * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
385135283SDan Handley *
485135283SDan Handley * Redistribution and use in source and binary forms, with or without
585135283SDan Handley * modification, are permitted provided that the following conditions are met:
685135283SDan Handley *
785135283SDan Handley * Redistributions of source code must retain the above copyright notice, this
885135283SDan Handley * list of conditions and the following disclaimer.
985135283SDan Handley *
1085135283SDan Handley * Redistributions in binary form must reproduce the above copyright notice,
1185135283SDan Handley * this list of conditions and the following disclaimer in the documentation
1285135283SDan Handley * and/or other materials provided with the distribution.
1385135283SDan Handley *
1485135283SDan Handley * Neither the name of ARM nor the names of its contributors may be used
1585135283SDan Handley * to endorse or promote products derived from this software without specific
1685135283SDan Handley * prior written permission.
1785135283SDan Handley *
1885135283SDan Handley * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1985135283SDan Handley * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2085135283SDan Handley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2185135283SDan Handley * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2285135283SDan Handley * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2385135283SDan Handley * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2485135283SDan Handley * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2585135283SDan Handley * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2685135283SDan Handley * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2785135283SDan Handley * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2885135283SDan Handley * POSSIBILITY OF SUCH DAMAGE.
2985135283SDan Handley */
3085135283SDan Handley
3185135283SDan Handley#include <arch.h>
3285135283SDan Handley#include <asm_macros.S>
3385135283SDan Handley#include <bl_common.h>
3423d39dbcSSandrine Bailleux#include <cortex_a53.h>
3585135283SDan Handley#include <cortex_a57.h>
361dbe3159SSandrine Bailleux#include <cortex_a72.h>
37*07570d59SYatharth Kochar#include <cpu_macros.S>
38*07570d59SYatharth Kochar#include <css_def.h>
3985135283SDan Handley#include <v2m_def.h>
4085135283SDan Handley#include "../juno_def.h"
4185135283SDan Handley
4285135283SDan Handley
4385135283SDan Handley	.globl	plat_reset_handler
44371d4399SDavid Wang	.globl	plat_arm_calc_core_pos
45*07570d59SYatharth Kochar#if JUNO_AARCH32_EL3_RUNTIME
46*07570d59SYatharth Kochar	.globl	plat_get_my_entrypoint
47*07570d59SYatharth Kochar	.globl	juno_reset_to_aarch32_state
48*07570d59SYatharth Kochar#endif
4985135283SDan Handley
5023d39dbcSSandrine Bailleux#define JUNO_REVISION(rev)	REV_JUNO_R##rev
5123d39dbcSSandrine Bailleux#define JUNO_HANDLER(rev)	plat_reset_handler_juno_r##rev
5223d39dbcSSandrine Bailleux#define JUMP_TO_HANDLER_IF_JUNO_R(revision)	\
5323d39dbcSSandrine Bailleux	jump_to_handler JUNO_REVISION(revision), JUNO_HANDLER(revision)
5423d39dbcSSandrine Bailleux
5585135283SDan Handley	/* --------------------------------------------------------------------
5623d39dbcSSandrine Bailleux	 * Helper macro to jump to the given handler if the board revision
5723d39dbcSSandrine Bailleux	 * matches.
5823d39dbcSSandrine Bailleux	 * Expects the Juno board revision in x0.
5923d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
6023d39dbcSSandrine Bailleux	 */
6123d39dbcSSandrine Bailleux	.macro jump_to_handler _revision, _handler
6223d39dbcSSandrine Bailleux	cmp	x0, #\_revision
6323d39dbcSSandrine Bailleux	b.eq	\_handler
6423d39dbcSSandrine Bailleux	.endm
6523d39dbcSSandrine Bailleux
6623d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
6723d39dbcSSandrine Bailleux	 * Helper macro that reads the part number of the current CPU and jumps
6823d39dbcSSandrine Bailleux	 * to the given label if it matches the CPU MIDR provided.
6985135283SDan Handley	 *
7023d39dbcSSandrine Bailleux	 * Clobbers x0.
7123d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
7223d39dbcSSandrine Bailleux	 */
7323d39dbcSSandrine Bailleux	.macro  jump_if_cpu_midr _cpu_midr, _label
7423d39dbcSSandrine Bailleux	mrs	x0, midr_el1
7523d39dbcSSandrine Bailleux	ubfx	x0, x0, MIDR_PN_SHIFT, #12
7623d39dbcSSandrine Bailleux	cmp     w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
7723d39dbcSSandrine Bailleux	b.eq	\_label
7823d39dbcSSandrine Bailleux	.endm
7923d39dbcSSandrine Bailleux
8023d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
8123d39dbcSSandrine Bailleux	 * Platform reset handler for Juno R0.
8223d39dbcSSandrine Bailleux	 *
8323d39dbcSSandrine Bailleux	 * Juno R0 has the following topology:
8423d39dbcSSandrine Bailleux	 * - Quad core Cortex-A53 processor cluster;
8523d39dbcSSandrine Bailleux	 * - Dual core Cortex-A57 processor cluster.
8623d39dbcSSandrine Bailleux	 *
8723d39dbcSSandrine Bailleux	 * This handler does the following:
8885135283SDan Handley	 * - Implement workaround for defect id 831273 by enabling an event
8985135283SDan Handley	 *   stream every 65536 cycles.
9085135283SDan Handley	 * - Set the L2 Data RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
9185135283SDan Handley	 * - Set the L2 Tag RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
9223d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
9323d39dbcSSandrine Bailleux	 */
9423d39dbcSSandrine Bailleuxfunc JUNO_HANDLER(0)
9523d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
9623d39dbcSSandrine Bailleux	 * Enable the event stream every 65536 cycles
9723d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
9823d39dbcSSandrine Bailleux	 */
9923d39dbcSSandrine Bailleux	mov     x0, #(0xf << EVNTI_SHIFT)
10023d39dbcSSandrine Bailleux	orr     x0, x0, #EVNTEN_BIT
10123d39dbcSSandrine Bailleux	msr     CNTKCTL_EL1, x0
10223d39dbcSSandrine Bailleux
10323d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
10423d39dbcSSandrine Bailleux	 * Nothing else to do on Cortex-A53.
10523d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
10623d39dbcSSandrine Bailleux	 */
10723d39dbcSSandrine Bailleux	jump_if_cpu_midr CORTEX_A53_MIDR, 1f
10823d39dbcSSandrine Bailleux
10923d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
11023d39dbcSSandrine Bailleux	 * Cortex-A57 specific settings
11123d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
11223d39dbcSSandrine Bailleux	 */
11323d39dbcSSandrine Bailleux	mov	x0, #((L2_DATA_RAM_LATENCY_3_CYCLES << L2CTLR_DATA_RAM_LATENCY_SHIFT) |	\
11423d39dbcSSandrine Bailleux		      (L2_TAG_RAM_LATENCY_3_CYCLES << L2CTLR_TAG_RAM_LATENCY_SHIFT))
11523d39dbcSSandrine Bailleux	msr     L2CTLR_EL1, x0
11623d39dbcSSandrine Bailleux1:
11723d39dbcSSandrine Bailleux	isb
11823d39dbcSSandrine Bailleux	ret
11923d39dbcSSandrine Bailleuxendfunc JUNO_HANDLER(0)
12023d39dbcSSandrine Bailleux
12123d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
12223d39dbcSSandrine Bailleux	 * Platform reset handler for Juno R1.
12385135283SDan Handley	 *
12423d39dbcSSandrine Bailleux	 * Juno R1 has the following topology:
12523d39dbcSSandrine Bailleux	 * - Quad core Cortex-A53 processor cluster;
12623d39dbcSSandrine Bailleux	 * - Dual core Cortex-A57 processor cluster.
12723d39dbcSSandrine Bailleux	 *
12823d39dbcSSandrine Bailleux	 * This handler does the following:
12985135283SDan Handley	 * - Set the L2 Data RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
13023d39dbcSSandrine Bailleux	 *
13185135283SDan Handley	 * Note that:
13285135283SDan Handley	 * - The default value for the L2 Tag RAM latency for Cortex-A57 is
13385135283SDan Handley	 *   suitable.
13423d39dbcSSandrine Bailleux	 * - Defect #831273 doesn't affect Juno R1.
13585135283SDan Handley	 * --------------------------------------------------------------------
13685135283SDan Handley	 */
13723d39dbcSSandrine Bailleuxfunc JUNO_HANDLER(1)
13885135283SDan Handley	/* --------------------------------------------------------------------
13923d39dbcSSandrine Bailleux	 * Nothing to do on Cortex-A53.
14085135283SDan Handley	 * --------------------------------------------------------------------
14185135283SDan Handley	 */
14223d39dbcSSandrine Bailleux	jump_if_cpu_midr CORTEX_A57_MIDR, A57
14385135283SDan Handley	ret
14485135283SDan Handley
14585135283SDan HandleyA57:
14685135283SDan Handley	/* --------------------------------------------------------------------
14785135283SDan Handley	 * Cortex-A57 specific settings
14885135283SDan Handley	 * --------------------------------------------------------------------
14985135283SDan Handley	 */
15023d39dbcSSandrine Bailleux	mov	x0, #(L2_DATA_RAM_LATENCY_3_CYCLES << L2CTLR_DATA_RAM_LATENCY_SHIFT)
15185135283SDan Handley	msr     L2CTLR_EL1, x0
15285135283SDan Handley	isb
15385135283SDan Handley	ret
15423d39dbcSSandrine Bailleuxendfunc JUNO_HANDLER(1)
15523d39dbcSSandrine Bailleux
15623d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
15723d39dbcSSandrine Bailleux	 * Platform reset handler for Juno R2.
15823d39dbcSSandrine Bailleux	 *
15923d39dbcSSandrine Bailleux	 * Juno R2 has the following topology:
16023d39dbcSSandrine Bailleux	 * - Quad core Cortex-A53 processor cluster;
16123d39dbcSSandrine Bailleux	 * - Dual core Cortex-A72 processor cluster.
16223d39dbcSSandrine Bailleux	 *
1631dbe3159SSandrine Bailleux	 * This handler does the following:
1641dbe3159SSandrine Bailleux	 * - Set the L2 Data RAM latency to 2 (i.e. 3 cycles) for Cortex-A72
1651dbe3159SSandrine Bailleux	 * - Set the L2 Tag RAM latency to 1 (i.e. 2 cycles) for Cortex-A72
1661dbe3159SSandrine Bailleux	 *
1671dbe3159SSandrine Bailleux	 * Note that:
1681dbe3159SSandrine Bailleux	 * - Defect #831273 doesn't affect Juno R2.
16923d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
17023d39dbcSSandrine Bailleux	 */
17123d39dbcSSandrine Bailleuxfunc JUNO_HANDLER(2)
1721dbe3159SSandrine Bailleux	/* --------------------------------------------------------------------
1731dbe3159SSandrine Bailleux	 * Nothing to do on Cortex-A53.
1741dbe3159SSandrine Bailleux	 * --------------------------------------------------------------------
1751dbe3159SSandrine Bailleux	 */
1761dbe3159SSandrine Bailleux	jump_if_cpu_midr CORTEX_A72_MIDR, A72
1771dbe3159SSandrine Bailleux	ret
1781dbe3159SSandrine Bailleux
1791dbe3159SSandrine BailleuxA72:
1801dbe3159SSandrine Bailleux	/* --------------------------------------------------------------------
1811dbe3159SSandrine Bailleux	 * Cortex-A72 specific settings
1821dbe3159SSandrine Bailleux	 * --------------------------------------------------------------------
1831dbe3159SSandrine Bailleux	 */
1841dbe3159SSandrine Bailleux	mov	x0, #((L2_DATA_RAM_LATENCY_3_CYCLES << L2CTLR_DATA_RAM_LATENCY_SHIFT) |	\
1851dbe3159SSandrine Bailleux		      (L2_TAG_RAM_LATENCY_2_CYCLES << L2CTLR_TAG_RAM_LATENCY_SHIFT))
1861dbe3159SSandrine Bailleux	msr     L2CTLR_EL1, x0
1871dbe3159SSandrine Bailleux	isb
18823d39dbcSSandrine Bailleux	ret
18923d39dbcSSandrine Bailleuxendfunc JUNO_HANDLER(2)
19023d39dbcSSandrine Bailleux
19123d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
19223d39dbcSSandrine Bailleux	 * void plat_reset_handler(void);
19323d39dbcSSandrine Bailleux	 *
19423d39dbcSSandrine Bailleux	 * Determine the Juno board revision and call the appropriate reset
19523d39dbcSSandrine Bailleux	 * handler.
19623d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
19723d39dbcSSandrine Bailleux	 */
19823d39dbcSSandrine Bailleuxfunc plat_reset_handler
19923d39dbcSSandrine Bailleux	/* Read the V2M SYS_ID register */
20023d39dbcSSandrine Bailleux	mov_imm	x0, (V2M_SYSREGS_BASE + V2M_SYS_ID)
20123d39dbcSSandrine Bailleux	ldr	w1, [x0]
20223d39dbcSSandrine Bailleux	/* Extract board revision from the SYS_ID */
20323d39dbcSSandrine Bailleux	ubfx	x0, x1, #V2M_SYS_ID_REV_SHIFT, #4
20423d39dbcSSandrine Bailleux
20523d39dbcSSandrine Bailleux	JUMP_TO_HANDLER_IF_JUNO_R(0)
20623d39dbcSSandrine Bailleux	JUMP_TO_HANDLER_IF_JUNO_R(1)
20723d39dbcSSandrine Bailleux	JUMP_TO_HANDLER_IF_JUNO_R(2)
20823d39dbcSSandrine Bailleux
20923d39dbcSSandrine Bailleux	/* Board revision is not supported */
210a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
21123d39dbcSSandrine Bailleux
21285135283SDan Handleyendfunc plat_reset_handler
213371d4399SDavid Wang
214371d4399SDavid Wang	/* -----------------------------------------------------
215*07570d59SYatharth Kochar	 *  void juno_do_reset_to_aarch32_state(void);
216*07570d59SYatharth Kochar	 *
217*07570d59SYatharth Kochar	 *  Request warm reset to AArch32 mode.
218*07570d59SYatharth Kochar	 * -----------------------------------------------------
219*07570d59SYatharth Kochar	 */
220*07570d59SYatharth Kocharfunc juno_do_reset_to_aarch32_state
221*07570d59SYatharth Kochar	mov	x0, #RMR_EL3_RR_BIT
222*07570d59SYatharth Kochar	dsb	sy
223*07570d59SYatharth Kochar	msr	rmr_el3, x0
224*07570d59SYatharth Kochar	isb
225*07570d59SYatharth Kochar	wfi
226*07570d59SYatharth Kocharendfunc juno_do_reset_to_aarch32_state
227*07570d59SYatharth Kochar
228*07570d59SYatharth Kochar	/* -----------------------------------------------------
2294c0d0390SSoby Mathew	 *  unsigned int plat_arm_calc_core_pos(u_register_t mpidr)
230371d4399SDavid Wang	 *  Helper function to calculate the core position.
231371d4399SDavid Wang	 * -----------------------------------------------------
232371d4399SDavid Wang	 */
233371d4399SDavid Wangfunc plat_arm_calc_core_pos
234371d4399SDavid Wang	b	css_calc_core_pos_swap_cluster
235371d4399SDavid Wangendfunc plat_arm_calc_core_pos
236*07570d59SYatharth Kochar
237*07570d59SYatharth Kochar#if JUNO_AARCH32_EL3_RUNTIME
238*07570d59SYatharth Kochar	/* ---------------------------------------------------------------------
239*07570d59SYatharth Kochar	 * uintptr_t plat_get_my_entrypoint (void);
240*07570d59SYatharth Kochar	 *
241*07570d59SYatharth Kochar	 * Main job of this routine is to distinguish between a cold and a warm
242*07570d59SYatharth Kochar	 * boot. On JUNO platform, this distinction is based on the contents of
243*07570d59SYatharth Kochar	 * the Trusted Mailbox. It is initialised to zero by the SCP before the
244*07570d59SYatharth Kochar	 * AP cores are released from reset. Therefore, a zero mailbox means
245*07570d59SYatharth Kochar	 * it's a cold reset. If it is a warm boot then a request to reset to
246*07570d59SYatharth Kochar	 * AArch32 state is issued. This is the only way to reset to AArch32
247*07570d59SYatharth Kochar	 * in EL3 on Juno. A trampoline located at the high vector address
248*07570d59SYatharth Kochar	 * has already been prepared by BL1.
249*07570d59SYatharth Kochar	 *
250*07570d59SYatharth Kochar	 * This functions returns the contents of the mailbox, i.e.:
251*07570d59SYatharth Kochar	 *  - 0 for a cold boot;
252*07570d59SYatharth Kochar	 *  - request warm reset in AArch32 state for warm boot case;
253*07570d59SYatharth Kochar	 * ---------------------------------------------------------------------
254*07570d59SYatharth Kochar	 */
255*07570d59SYatharth Kocharfunc plat_get_my_entrypoint
256*07570d59SYatharth Kochar	mov_imm	x0, PLAT_ARM_TRUSTED_MAILBOX_BASE
257*07570d59SYatharth Kochar	ldr	x0, [x0]
258*07570d59SYatharth Kochar	cbz	x0, return
259*07570d59SYatharth Kochar	b	juno_do_reset_to_aarch32_state
260*07570d59SYatharth Kochar1:
261*07570d59SYatharth Kochar	b	1b
262*07570d59SYatharth Kocharreturn:
263*07570d59SYatharth Kochar	ret
264*07570d59SYatharth Kocharendfunc plat_get_my_entrypoint
265*07570d59SYatharth Kochar
266*07570d59SYatharth Kochar/*
267*07570d59SYatharth Kochar * Emit a "movw r0, #imm16" which moves the lower
268*07570d59SYatharth Kochar * 16 bits of `_val` into r0.
269*07570d59SYatharth Kochar */
270*07570d59SYatharth Kochar.macro emit_movw _reg_d, _val
271*07570d59SYatharth Kochar	mov_imm	\_reg_d, (0xe3000000 | \
272*07570d59SYatharth Kochar			((\_val & 0xfff) | \
273*07570d59SYatharth Kochar			((\_val & 0xf000) << 4)))
274*07570d59SYatharth Kochar.endm
275*07570d59SYatharth Kochar
276*07570d59SYatharth Kochar/*
277*07570d59SYatharth Kochar * Emit a "movt r0, #imm16" which moves the upper
278*07570d59SYatharth Kochar * 16 bits of `_val` into r0.
279*07570d59SYatharth Kochar */
280*07570d59SYatharth Kochar.macro emit_movt _reg_d, _val
281*07570d59SYatharth Kochar	mov_imm	\_reg_d, (0xe3400000 | \
282*07570d59SYatharth Kochar			(((\_val & 0x0fff0000) >> 16) | \
283*07570d59SYatharth Kochar			((\_val & 0xf0000000) >> 12)))
284*07570d59SYatharth Kochar.endm
285*07570d59SYatharth Kochar
286*07570d59SYatharth Kochar/*
287*07570d59SYatharth Kochar * This function writes the trampoline code at HI-VEC (0xFFFF0000)
288*07570d59SYatharth Kochar * address which loads r0 with the entrypoint address for
289*07570d59SYatharth Kochar * BL32 (a.k.a SP_MIN) when EL3 is in AArch32 mode. A warm reset
290*07570d59SYatharth Kochar * to AArch32 mode is then requested by writing into RMR_EL3.
291*07570d59SYatharth Kochar */
292*07570d59SYatharth Kocharfunc juno_reset_to_aarch32_state
293*07570d59SYatharth Kochar	emit_movw	w0, BL32_BASE
294*07570d59SYatharth Kochar	emit_movt	w1, BL32_BASE
295*07570d59SYatharth Kochar	/* opcode "bx r0" to branch using r0 in AArch32 mode */
296*07570d59SYatharth Kochar	mov_imm	w2, 0xe12fff10
297*07570d59SYatharth Kochar
298*07570d59SYatharth Kochar	/* Write the above opcodes at HI-VECTOR location */
299*07570d59SYatharth Kochar	mov_imm	x3, HI_VECTOR_BASE
300*07570d59SYatharth Kochar	str	w0, [x3], #4
301*07570d59SYatharth Kochar	str	w1, [x3], #4
302*07570d59SYatharth Kochar	str	w2, [x3]
303*07570d59SYatharth Kochar
304*07570d59SYatharth Kochar	bl	juno_do_reset_to_aarch32_state
305*07570d59SYatharth Kochar1:
306*07570d59SYatharth Kochar	b	1b
307*07570d59SYatharth Kocharendfunc juno_reset_to_aarch32_state
308*07570d59SYatharth Kochar
309*07570d59SYatharth Kochar#endif /* JUNO_AARCH32_EL3_RUNTIME */
310