xref: /rk3399_ARM-atf/plat/arm/board/juno/aarch64/juno_helpers.S (revision fb7d32e5881ef2445e8fe2305005f5590d4a7cfa)
185135283SDan Handley/*
21c3ea103SAntonio Nino Diaz * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
385135283SDan Handley *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
585135283SDan Handley */
685135283SDan Handley
785135283SDan Handley#include <arch.h>
885135283SDan Handley#include <asm_macros.S>
985135283SDan Handley#include <bl_common.h>
1023d39dbcSSandrine Bailleux#include <cortex_a53.h>
1185135283SDan Handley#include <cortex_a57.h>
121dbe3159SSandrine Bailleux#include <cortex_a72.h>
1307570d59SYatharth Kochar#include <cpu_macros.S>
1407570d59SYatharth Kochar#include <css_def.h>
1585135283SDan Handley#include <v2m_def.h>
1685135283SDan Handley#include "../juno_def.h"
1785135283SDan Handley
1885135283SDan Handley
1985135283SDan Handley	.globl	plat_reset_handler
20371d4399SDavid Wang	.globl	plat_arm_calc_core_pos
2107570d59SYatharth Kochar#if JUNO_AARCH32_EL3_RUNTIME
2207570d59SYatharth Kochar	.globl	plat_get_my_entrypoint
2307570d59SYatharth Kochar	.globl	juno_reset_to_aarch32_state
2407570d59SYatharth Kochar#endif
2585135283SDan Handley
2623d39dbcSSandrine Bailleux#define JUNO_REVISION(rev)	REV_JUNO_R##rev
2723d39dbcSSandrine Bailleux#define JUNO_HANDLER(rev)	plat_reset_handler_juno_r##rev
2823d39dbcSSandrine Bailleux#define JUMP_TO_HANDLER_IF_JUNO_R(revision)	\
2923d39dbcSSandrine Bailleux	jump_to_handler JUNO_REVISION(revision), JUNO_HANDLER(revision)
3023d39dbcSSandrine Bailleux
3185135283SDan Handley	/* --------------------------------------------------------------------
3223d39dbcSSandrine Bailleux	 * Helper macro to jump to the given handler if the board revision
3323d39dbcSSandrine Bailleux	 * matches.
3423d39dbcSSandrine Bailleux	 * Expects the Juno board revision in x0.
3523d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
3623d39dbcSSandrine Bailleux	 */
3723d39dbcSSandrine Bailleux	.macro jump_to_handler _revision, _handler
3823d39dbcSSandrine Bailleux	cmp	x0, #\_revision
3923d39dbcSSandrine Bailleux	b.eq	\_handler
4023d39dbcSSandrine Bailleux	.endm
4123d39dbcSSandrine Bailleux
4223d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
4323d39dbcSSandrine Bailleux	 * Helper macro that reads the part number of the current CPU and jumps
4423d39dbcSSandrine Bailleux	 * to the given label if it matches the CPU MIDR provided.
4585135283SDan Handley	 *
4623d39dbcSSandrine Bailleux	 * Clobbers x0.
4723d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
4823d39dbcSSandrine Bailleux	 */
4923d39dbcSSandrine Bailleux	.macro  jump_if_cpu_midr _cpu_midr, _label
5023d39dbcSSandrine Bailleux	mrs	x0, midr_el1
5123d39dbcSSandrine Bailleux	ubfx	x0, x0, MIDR_PN_SHIFT, #12
5223d39dbcSSandrine Bailleux	cmp     w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
5323d39dbcSSandrine Bailleux	b.eq	\_label
5423d39dbcSSandrine Bailleux	.endm
5523d39dbcSSandrine Bailleux
5623d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
5723d39dbcSSandrine Bailleux	 * Platform reset handler for Juno R0.
5823d39dbcSSandrine Bailleux	 *
5923d39dbcSSandrine Bailleux	 * Juno R0 has the following topology:
6023d39dbcSSandrine Bailleux	 * - Quad core Cortex-A53 processor cluster;
6123d39dbcSSandrine Bailleux	 * - Dual core Cortex-A57 processor cluster.
6223d39dbcSSandrine Bailleux	 *
6323d39dbcSSandrine Bailleux	 * This handler does the following:
6485135283SDan Handley	 * - Implement workaround for defect id 831273 by enabling an event
6585135283SDan Handley	 *   stream every 65536 cycles.
6685135283SDan Handley	 * - Set the L2 Data RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
6785135283SDan Handley	 * - Set the L2 Tag RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
6823d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
6923d39dbcSSandrine Bailleux	 */
7023d39dbcSSandrine Bailleuxfunc JUNO_HANDLER(0)
7123d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
7223d39dbcSSandrine Bailleux	 * Enable the event stream every 65536 cycles
7323d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
7423d39dbcSSandrine Bailleux	 */
7523d39dbcSSandrine Bailleux	mov     x0, #(0xf << EVNTI_SHIFT)
7623d39dbcSSandrine Bailleux	orr     x0, x0, #EVNTEN_BIT
7723d39dbcSSandrine Bailleux	msr     CNTKCTL_EL1, x0
7823d39dbcSSandrine Bailleux
7923d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
8023d39dbcSSandrine Bailleux	 * Nothing else to do on Cortex-A53.
8123d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
8223d39dbcSSandrine Bailleux	 */
8323d39dbcSSandrine Bailleux	jump_if_cpu_midr CORTEX_A53_MIDR, 1f
8423d39dbcSSandrine Bailleux
8523d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
8623d39dbcSSandrine Bailleux	 * Cortex-A57 specific settings
8723d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
8823d39dbcSSandrine Bailleux	 */
89*fb7d32e5SVarun Wadekar	mov	x0, #((CORTEX_A57_L2_DATA_RAM_LATENCY_3_CYCLES << CORTEX_A57_L2CTLR_DATA_RAM_LATENCY_SHIFT) |	\
90*fb7d32e5SVarun Wadekar		      (CORTEX_A57_L2_TAG_RAM_LATENCY_3_CYCLES << CORTEX_A57_L2CTLR_TAG_RAM_LATENCY_SHIFT))
91*fb7d32e5SVarun Wadekar	msr     CORTEX_A57_L2CTLR_EL1, x0
9223d39dbcSSandrine Bailleux1:
9323d39dbcSSandrine Bailleux	isb
9423d39dbcSSandrine Bailleux	ret
9523d39dbcSSandrine Bailleuxendfunc JUNO_HANDLER(0)
9623d39dbcSSandrine Bailleux
9723d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
9823d39dbcSSandrine Bailleux	 * Platform reset handler for Juno R1.
9985135283SDan Handley	 *
10023d39dbcSSandrine Bailleux	 * Juno R1 has the following topology:
10123d39dbcSSandrine Bailleux	 * - Quad core Cortex-A53 processor cluster;
10223d39dbcSSandrine Bailleux	 * - Dual core Cortex-A57 processor cluster.
10323d39dbcSSandrine Bailleux	 *
10423d39dbcSSandrine Bailleux	 * This handler does the following:
10585135283SDan Handley	 * - Set the L2 Data RAM latency to 2 (i.e. 3 cycles) for Cortex-A57
10623d39dbcSSandrine Bailleux	 *
10785135283SDan Handley	 * Note that:
10885135283SDan Handley	 * - The default value for the L2 Tag RAM latency for Cortex-A57 is
10985135283SDan Handley	 *   suitable.
11023d39dbcSSandrine Bailleux	 * - Defect #831273 doesn't affect Juno R1.
11185135283SDan Handley	 * --------------------------------------------------------------------
11285135283SDan Handley	 */
11323d39dbcSSandrine Bailleuxfunc JUNO_HANDLER(1)
11485135283SDan Handley	/* --------------------------------------------------------------------
11523d39dbcSSandrine Bailleux	 * Nothing to do on Cortex-A53.
11685135283SDan Handley	 * --------------------------------------------------------------------
11785135283SDan Handley	 */
11823d39dbcSSandrine Bailleux	jump_if_cpu_midr CORTEX_A57_MIDR, A57
11985135283SDan Handley	ret
12085135283SDan Handley
12185135283SDan HandleyA57:
12285135283SDan Handley	/* --------------------------------------------------------------------
12385135283SDan Handley	 * Cortex-A57 specific settings
12485135283SDan Handley	 * --------------------------------------------------------------------
12585135283SDan Handley	 */
126*fb7d32e5SVarun Wadekar	mov	x0, #(CORTEX_A57_L2_DATA_RAM_LATENCY_3_CYCLES << CORTEX_A57_L2CTLR_DATA_RAM_LATENCY_SHIFT)
127*fb7d32e5SVarun Wadekar	msr     CORTEX_A57_L2CTLR_EL1, x0
12885135283SDan Handley	isb
12985135283SDan Handley	ret
13023d39dbcSSandrine Bailleuxendfunc JUNO_HANDLER(1)
13123d39dbcSSandrine Bailleux
13223d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
13323d39dbcSSandrine Bailleux	 * Platform reset handler for Juno R2.
13423d39dbcSSandrine Bailleux	 *
13523d39dbcSSandrine Bailleux	 * Juno R2 has the following topology:
13623d39dbcSSandrine Bailleux	 * - Quad core Cortex-A53 processor cluster;
13723d39dbcSSandrine Bailleux	 * - Dual core Cortex-A72 processor cluster.
13823d39dbcSSandrine Bailleux	 *
1391dbe3159SSandrine Bailleux	 * This handler does the following:
1401dbe3159SSandrine Bailleux	 * - Set the L2 Data RAM latency to 2 (i.e. 3 cycles) for Cortex-A72
1411dbe3159SSandrine Bailleux	 * - Set the L2 Tag RAM latency to 1 (i.e. 2 cycles) for Cortex-A72
1421dbe3159SSandrine Bailleux	 *
1431dbe3159SSandrine Bailleux	 * Note that:
1441dbe3159SSandrine Bailleux	 * - Defect #831273 doesn't affect Juno R2.
14523d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
14623d39dbcSSandrine Bailleux	 */
14723d39dbcSSandrine Bailleuxfunc JUNO_HANDLER(2)
1481dbe3159SSandrine Bailleux	/* --------------------------------------------------------------------
1491dbe3159SSandrine Bailleux	 * Nothing to do on Cortex-A53.
1501dbe3159SSandrine Bailleux	 * --------------------------------------------------------------------
1511dbe3159SSandrine Bailleux	 */
1521dbe3159SSandrine Bailleux	jump_if_cpu_midr CORTEX_A72_MIDR, A72
1531dbe3159SSandrine Bailleux	ret
1541dbe3159SSandrine Bailleux
1551dbe3159SSandrine BailleuxA72:
1561dbe3159SSandrine Bailleux	/* --------------------------------------------------------------------
1571dbe3159SSandrine Bailleux	 * Cortex-A72 specific settings
1581dbe3159SSandrine Bailleux	 * --------------------------------------------------------------------
1591dbe3159SSandrine Bailleux	 */
160*fb7d32e5SVarun Wadekar	mov	x0, #((CORTEX_A72_L2_DATA_RAM_LATENCY_3_CYCLES << CORTEX_A72_L2CTLR_DATA_RAM_LATENCY_SHIFT) |	\
161*fb7d32e5SVarun Wadekar		      (CORTEX_A72_L2_TAG_RAM_LATENCY_2_CYCLES << CORTEX_A72_L2CTLR_TAG_RAM_LATENCY_SHIFT))
162*fb7d32e5SVarun Wadekar	msr     CORTEX_A57_L2CTLR_EL1, x0
1631dbe3159SSandrine Bailleux	isb
16423d39dbcSSandrine Bailleux	ret
16523d39dbcSSandrine Bailleuxendfunc JUNO_HANDLER(2)
16623d39dbcSSandrine Bailleux
16723d39dbcSSandrine Bailleux	/* --------------------------------------------------------------------
16823d39dbcSSandrine Bailleux	 * void plat_reset_handler(void);
16923d39dbcSSandrine Bailleux	 *
17023d39dbcSSandrine Bailleux	 * Determine the Juno board revision and call the appropriate reset
17123d39dbcSSandrine Bailleux	 * handler.
17223d39dbcSSandrine Bailleux	 * --------------------------------------------------------------------
17323d39dbcSSandrine Bailleux	 */
17423d39dbcSSandrine Bailleuxfunc plat_reset_handler
17523d39dbcSSandrine Bailleux	/* Read the V2M SYS_ID register */
17623d39dbcSSandrine Bailleux	mov_imm	x0, (V2M_SYSREGS_BASE + V2M_SYS_ID)
17723d39dbcSSandrine Bailleux	ldr	w1, [x0]
17823d39dbcSSandrine Bailleux	/* Extract board revision from the SYS_ID */
17923d39dbcSSandrine Bailleux	ubfx	x0, x1, #V2M_SYS_ID_REV_SHIFT, #4
18023d39dbcSSandrine Bailleux
18123d39dbcSSandrine Bailleux	JUMP_TO_HANDLER_IF_JUNO_R(0)
18223d39dbcSSandrine Bailleux	JUMP_TO_HANDLER_IF_JUNO_R(1)
18323d39dbcSSandrine Bailleux	JUMP_TO_HANDLER_IF_JUNO_R(2)
18423d39dbcSSandrine Bailleux
18523d39dbcSSandrine Bailleux	/* Board revision is not supported */
186a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
18723d39dbcSSandrine Bailleux
18885135283SDan Handleyendfunc plat_reset_handler
189371d4399SDavid Wang
190371d4399SDavid Wang	/* -----------------------------------------------------
19107570d59SYatharth Kochar	 *  void juno_do_reset_to_aarch32_state(void);
19207570d59SYatharth Kochar	 *
19307570d59SYatharth Kochar	 *  Request warm reset to AArch32 mode.
19407570d59SYatharth Kochar	 * -----------------------------------------------------
19507570d59SYatharth Kochar	 */
19607570d59SYatharth Kocharfunc juno_do_reset_to_aarch32_state
19707570d59SYatharth Kochar	mov	x0, #RMR_EL3_RR_BIT
19807570d59SYatharth Kochar	dsb	sy
19907570d59SYatharth Kochar	msr	rmr_el3, x0
20007570d59SYatharth Kochar	isb
20107570d59SYatharth Kochar	wfi
20207570d59SYatharth Kocharendfunc juno_do_reset_to_aarch32_state
20307570d59SYatharth Kochar
20407570d59SYatharth Kochar	/* -----------------------------------------------------
2054c0d0390SSoby Mathew	 *  unsigned int plat_arm_calc_core_pos(u_register_t mpidr)
206371d4399SDavid Wang	 *  Helper function to calculate the core position.
207371d4399SDavid Wang	 * -----------------------------------------------------
208371d4399SDavid Wang	 */
209371d4399SDavid Wangfunc plat_arm_calc_core_pos
210371d4399SDavid Wang	b	css_calc_core_pos_swap_cluster
211371d4399SDavid Wangendfunc plat_arm_calc_core_pos
21207570d59SYatharth Kochar
21307570d59SYatharth Kochar#if JUNO_AARCH32_EL3_RUNTIME
21407570d59SYatharth Kochar	/* ---------------------------------------------------------------------
21507570d59SYatharth Kochar	 * uintptr_t plat_get_my_entrypoint (void);
21607570d59SYatharth Kochar	 *
21707570d59SYatharth Kochar	 * Main job of this routine is to distinguish between a cold and a warm
21807570d59SYatharth Kochar	 * boot. On JUNO platform, this distinction is based on the contents of
21907570d59SYatharth Kochar	 * the Trusted Mailbox. It is initialised to zero by the SCP before the
22007570d59SYatharth Kochar	 * AP cores are released from reset. Therefore, a zero mailbox means
22107570d59SYatharth Kochar	 * it's a cold reset. If it is a warm boot then a request to reset to
22207570d59SYatharth Kochar	 * AArch32 state is issued. This is the only way to reset to AArch32
22307570d59SYatharth Kochar	 * in EL3 on Juno. A trampoline located at the high vector address
22407570d59SYatharth Kochar	 * has already been prepared by BL1.
22507570d59SYatharth Kochar	 *
22607570d59SYatharth Kochar	 * This functions returns the contents of the mailbox, i.e.:
22707570d59SYatharth Kochar	 *  - 0 for a cold boot;
22807570d59SYatharth Kochar	 *  - request warm reset in AArch32 state for warm boot case;
22907570d59SYatharth Kochar	 * ---------------------------------------------------------------------
23007570d59SYatharth Kochar	 */
23107570d59SYatharth Kocharfunc plat_get_my_entrypoint
23207570d59SYatharth Kochar	mov_imm	x0, PLAT_ARM_TRUSTED_MAILBOX_BASE
23307570d59SYatharth Kochar	ldr	x0, [x0]
23407570d59SYatharth Kochar	cbz	x0, return
23507570d59SYatharth Kochar	b	juno_do_reset_to_aarch32_state
23607570d59SYatharth Kochar1:
23707570d59SYatharth Kochar	b	1b
23807570d59SYatharth Kocharreturn:
23907570d59SYatharth Kochar	ret
24007570d59SYatharth Kocharendfunc plat_get_my_entrypoint
24107570d59SYatharth Kochar
24207570d59SYatharth Kochar/*
24307570d59SYatharth Kochar * Emit a "movw r0, #imm16" which moves the lower
24407570d59SYatharth Kochar * 16 bits of `_val` into r0.
24507570d59SYatharth Kochar */
24607570d59SYatharth Kochar.macro emit_movw _reg_d, _val
24707570d59SYatharth Kochar	mov_imm	\_reg_d, (0xe3000000 | \
24807570d59SYatharth Kochar			((\_val & 0xfff) | \
24907570d59SYatharth Kochar			((\_val & 0xf000) << 4)))
25007570d59SYatharth Kochar.endm
25107570d59SYatharth Kochar
25207570d59SYatharth Kochar/*
25307570d59SYatharth Kochar * Emit a "movt r0, #imm16" which moves the upper
25407570d59SYatharth Kochar * 16 bits of `_val` into r0.
25507570d59SYatharth Kochar */
25607570d59SYatharth Kochar.macro emit_movt _reg_d, _val
25707570d59SYatharth Kochar	mov_imm	\_reg_d, (0xe3400000 | \
25807570d59SYatharth Kochar			(((\_val & 0x0fff0000) >> 16) | \
25907570d59SYatharth Kochar			((\_val & 0xf0000000) >> 12)))
26007570d59SYatharth Kochar.endm
26107570d59SYatharth Kochar
26207570d59SYatharth Kochar/*
26307570d59SYatharth Kochar * This function writes the trampoline code at HI-VEC (0xFFFF0000)
26407570d59SYatharth Kochar * address which loads r0 with the entrypoint address for
26507570d59SYatharth Kochar * BL32 (a.k.a SP_MIN) when EL3 is in AArch32 mode. A warm reset
26607570d59SYatharth Kochar * to AArch32 mode is then requested by writing into RMR_EL3.
26707570d59SYatharth Kochar */
26807570d59SYatharth Kocharfunc juno_reset_to_aarch32_state
26907570d59SYatharth Kochar	emit_movw	w0, BL32_BASE
27007570d59SYatharth Kochar	emit_movt	w1, BL32_BASE
27107570d59SYatharth Kochar	/* opcode "bx r0" to branch using r0 in AArch32 mode */
27207570d59SYatharth Kochar	mov_imm	w2, 0xe12fff10
27307570d59SYatharth Kochar
27407570d59SYatharth Kochar	/* Write the above opcodes at HI-VECTOR location */
27507570d59SYatharth Kochar	mov_imm	x3, HI_VECTOR_BASE
27607570d59SYatharth Kochar	str	w0, [x3], #4
27707570d59SYatharth Kochar	str	w1, [x3], #4
27807570d59SYatharth Kochar	str	w2, [x3]
27907570d59SYatharth Kochar
28007570d59SYatharth Kochar	bl	juno_do_reset_to_aarch32_state
28107570d59SYatharth Kochar1:
28207570d59SYatharth Kochar	b	1b
28307570d59SYatharth Kocharendfunc juno_reset_to_aarch32_state
28407570d59SYatharth Kochar
28507570d59SYatharth Kochar#endif /* JUNO_AARCH32_EL3_RUNTIME */
286