xref: /rk3399_ARM-atf/lib/cpus/aarch32/cpu_helpers.S (revision c2ad38ce4fe754c750f3db480f732ac280f508e4)
1e33b78a6SSoby Mathew/*
2*c2ad38ceSVarun Wadekar * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
3e33b78a6SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5e33b78a6SSoby Mathew */
6e33b78a6SSoby Mathew
7e33b78a6SSoby Mathew#include <arch.h>
8e33b78a6SSoby Mathew#include <asm_macros.S>
9e33b78a6SSoby Mathew#include <assert_macros.S>
10e33b78a6SSoby Mathew#include <cpu_macros.S>
11*c2ad38ceSVarun Wadekar#include <common/bl_common.h>
1209d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
13e33b78a6SSoby Mathew
14b1d27b48SRoberto Vargas#if defined(IMAGE_BL1) || defined(IMAGE_BL32) || (defined(IMAGE_BL2) && BL2_AT_EL3)
15e33b78a6SSoby Mathew	/*
16e33b78a6SSoby Mathew	 * The reset handler common to all platforms.  After a matching
17e33b78a6SSoby Mathew	 * cpu_ops structure entry is found, the correponding reset_handler
18e33b78a6SSoby Mathew	 * in the cpu_ops is invoked. The reset handler is invoked very early
19e33b78a6SSoby Mathew	 * in the boot sequence and it is assumed that we can clobber r0 - r10
20e33b78a6SSoby Mathew	 * without the need to follow AAPCS.
21e33b78a6SSoby Mathew	 * Clobbers: r0 - r10
22e33b78a6SSoby Mathew	 */
23e33b78a6SSoby Mathew	.globl	reset_handler
24e33b78a6SSoby Mathewfunc reset_handler
25e33b78a6SSoby Mathew	mov	r10, lr
26e33b78a6SSoby Mathew
27e33b78a6SSoby Mathew	/* The plat_reset_handler can clobber r0 - r9 */
28e33b78a6SSoby Mathew	bl	plat_reset_handler
29e33b78a6SSoby Mathew
30e33b78a6SSoby Mathew	/* Get the matching cpu_ops pointer (clobbers: r0 - r5) */
31e33b78a6SSoby Mathew	bl	get_cpu_ops_ptr
32e33b78a6SSoby Mathew
33044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
34e33b78a6SSoby Mathew	cmp	r0, #0
35e33b78a6SSoby Mathew	ASM_ASSERT(ne)
36e33b78a6SSoby Mathew#endif
37e33b78a6SSoby Mathew
38e33b78a6SSoby Mathew	/* Get the cpu_ops reset handler */
39e33b78a6SSoby Mathew	ldr	r1, [r0, #CPU_RESET_FUNC]
40e33b78a6SSoby Mathew	cmp	r1, #0
41e33b78a6SSoby Mathew	mov	lr, r10
42e33b78a6SSoby Mathew	bxne	r1
43e33b78a6SSoby Mathew	bx	lr
44e33b78a6SSoby Mathewendfunc reset_handler
45e33b78a6SSoby Mathew
46b1d27b48SRoberto Vargas#endif
471a0a3f06SYatharth Kochar
483d8256b2SMasahiro Yamada#ifdef IMAGE_BL32 /* The power down core and cluster is needed only in  BL32 */
49e33b78a6SSoby Mathew	/*
505dd9dbb5SJeenu Viswambharan	 * void prepare_cpu_pwr_dwn(unsigned int power_level)
515dd9dbb5SJeenu Viswambharan	 *
525dd9dbb5SJeenu Viswambharan	 * Prepare CPU power down function for all platforms. The function takes
535dd9dbb5SJeenu Viswambharan	 * a domain level to be powered down as its parameter. After the cpu_ops
545dd9dbb5SJeenu Viswambharan	 * pointer is retrieved from cpu_data, the handler for requested power
555dd9dbb5SJeenu Viswambharan	 * level is called.
56e33b78a6SSoby Mathew	 */
575dd9dbb5SJeenu Viswambharan	.globl	prepare_cpu_pwr_dwn
585dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn
59e33b78a6SSoby Mathew	/*
605dd9dbb5SJeenu Viswambharan	 * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the
615dd9dbb5SJeenu Viswambharan	 * power down handler for the last power level
62e33b78a6SSoby Mathew	 */
635dd9dbb5SJeenu Viswambharan	mov	r2, #(CPU_MAX_PWR_DWN_OPS - 1)
645dd9dbb5SJeenu Viswambharan	cmp	r0, r2
655dd9dbb5SJeenu Viswambharan	movhi	r0, r2
66e33b78a6SSoby Mathew
675dd9dbb5SJeenu Viswambharan	push	{r0, lr}
685dd9dbb5SJeenu Viswambharan	bl	_cpu_data
695dd9dbb5SJeenu Viswambharan	pop	{r2, lr}
705dd9dbb5SJeenu Viswambharan
715dd9dbb5SJeenu Viswambharan	ldr	r0, [r0, #CPU_DATA_CPU_OPS_PTR]
72044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
735dd9dbb5SJeenu Viswambharan	cmp	r0, #0
74e33b78a6SSoby Mathew	ASM_ASSERT(ne)
75e33b78a6SSoby Mathew#endif
76e33b78a6SSoby Mathew
775dd9dbb5SJeenu Viswambharan	/* Get the appropriate power down handler */
785dd9dbb5SJeenu Viswambharan	mov	r1, #CPU_PWR_DWN_OPS
795dd9dbb5SJeenu Viswambharan	add	r1, r1, r2, lsl #2
805dd9dbb5SJeenu Viswambharan	ldr	r1, [r0, r1]
815dd9dbb5SJeenu Viswambharan	bx	r1
825dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn
83e33b78a6SSoby Mathew
84e33b78a6SSoby Mathew	/*
85e33b78a6SSoby Mathew	 * Initializes the cpu_ops_ptr if not already initialized
86e33b78a6SSoby Mathew	 * in cpu_data. This must only be called after the data cache
87e33b78a6SSoby Mathew	 * is enabled. AAPCS is followed.
88e33b78a6SSoby Mathew	 */
89e33b78a6SSoby Mathew	.globl	init_cpu_ops
90e33b78a6SSoby Mathewfunc init_cpu_ops
91e33b78a6SSoby Mathew	push	{r4 - r6, lr}
92e33b78a6SSoby Mathew	bl	_cpu_data
93e33b78a6SSoby Mathew	mov	r6, r0
94e33b78a6SSoby Mathew	ldr	r1, [r0, #CPU_DATA_CPU_OPS_PTR]
95e33b78a6SSoby Mathew	cmp	r1, #0
96e33b78a6SSoby Mathew	bne	1f
97e33b78a6SSoby Mathew	bl	get_cpu_ops_ptr
98044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
99e33b78a6SSoby Mathew	cmp	r0, #0
100e33b78a6SSoby Mathew	ASM_ASSERT(ne)
101e33b78a6SSoby Mathew#endif
102e33b78a6SSoby Mathew	str	r0, [r6, #CPU_DATA_CPU_OPS_PTR]!
103e33b78a6SSoby Mathew1:
104e33b78a6SSoby Mathew	pop	{r4 - r6, pc}
105e33b78a6SSoby Mathewendfunc init_cpu_ops
106e33b78a6SSoby Mathew
1071a0a3f06SYatharth Kochar#endif /* IMAGE_BL32 */
1081a0a3f06SYatharth Kochar
109e33b78a6SSoby Mathew	/*
110e33b78a6SSoby Mathew	 * The below function returns the cpu_ops structure matching the
111e33b78a6SSoby Mathew	 * midr of the core. It reads the MIDR and finds the matching
112e33b78a6SSoby Mathew	 * entry in cpu_ops entries. Only the implementation and part number
113e33b78a6SSoby Mathew	 * are used to match the entries.
114e33b78a6SSoby Mathew	 * Return :
115e33b78a6SSoby Mathew	 *     r0 - The matching cpu_ops pointer on Success
116e33b78a6SSoby Mathew	 *     r0 - 0 on failure.
117e33b78a6SSoby Mathew	 * Clobbers: r0 - r5
118e33b78a6SSoby Mathew	 */
119e33b78a6SSoby Mathew	.globl	get_cpu_ops_ptr
120e33b78a6SSoby Mathewfunc get_cpu_ops_ptr
121e33b78a6SSoby Mathew	/* Get the cpu_ops start and end locations */
122e33b78a6SSoby Mathew	ldr	r4, =(__CPU_OPS_START__ + CPU_MIDR)
123e33b78a6SSoby Mathew	ldr	r5, =(__CPU_OPS_END__ + CPU_MIDR)
124e33b78a6SSoby Mathew
125e33b78a6SSoby Mathew	/* Initialize the return parameter */
126e33b78a6SSoby Mathew	mov	r0, #0
127e33b78a6SSoby Mathew
128e33b78a6SSoby Mathew	/* Read the MIDR_EL1 */
129e33b78a6SSoby Mathew	ldcopr	r2, MIDR
130e33b78a6SSoby Mathew	ldr	r3, =CPU_IMPL_PN_MASK
131e33b78a6SSoby Mathew
132e33b78a6SSoby Mathew	/* Retain only the implementation and part number using mask */
133e33b78a6SSoby Mathew	and	r2, r2, r3
134e33b78a6SSoby Mathew1:
135e33b78a6SSoby Mathew	/* Check if we have reached end of list */
136e33b78a6SSoby Mathew	cmp	r4, r5
137355a5d03SDouglas Raillard	bhs	error_exit
138e33b78a6SSoby Mathew
139e33b78a6SSoby Mathew	/* load the midr from the cpu_ops */
140e33b78a6SSoby Mathew	ldr	r1, [r4], #CPU_OPS_SIZE
141e33b78a6SSoby Mathew	and	r1, r1, r3
142e33b78a6SSoby Mathew
143e33b78a6SSoby Mathew	/* Check if midr matches to midr of this core */
144e33b78a6SSoby Mathew	cmp	r1, r2
145e33b78a6SSoby Mathew	bne	1b
146e33b78a6SSoby Mathew
147e33b78a6SSoby Mathew	/* Subtract the increment and offset to get the cpu-ops pointer */
148e33b78a6SSoby Mathew	sub	r0, r4, #(CPU_OPS_SIZE + CPU_MIDR)
149e33b78a6SSoby Mathewerror_exit:
150e33b78a6SSoby Mathew	bx	lr
151e33b78a6SSoby Mathewendfunc get_cpu_ops_ptr
15210bcd761SJeenu Viswambharan
15310bcd761SJeenu Viswambharan/*
15410bcd761SJeenu Viswambharan * Extract CPU revision and variant, and combine them into a single numeric for
15510bcd761SJeenu Viswambharan * easier comparison.
15610bcd761SJeenu Viswambharan */
15710bcd761SJeenu Viswambharan	.globl	cpu_get_rev_var
15810bcd761SJeenu Viswambharanfunc cpu_get_rev_var
15910bcd761SJeenu Viswambharan	ldcopr	r1, MIDR
16010bcd761SJeenu Viswambharan
16110bcd761SJeenu Viswambharan	/*
16210bcd761SJeenu Viswambharan	 * Extract the variant[23:20] and revision[3:0] from r1 and pack it in
16310bcd761SJeenu Viswambharan	 * r0[0:7] as variant[7:4] and revision[3:0]:
16410bcd761SJeenu Viswambharan	 *
16510bcd761SJeenu Viswambharan	 * First extract r1[23:16] to r0[7:0] and zero fill the rest. Then
16610bcd761SJeenu Viswambharan	 * extract r1[3:0] into r0[3:0] retaining other bits.
16710bcd761SJeenu Viswambharan	 */
16810bcd761SJeenu Viswambharan	ubfx	r0, r1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS)
16910bcd761SJeenu Viswambharan	bfi	r0, r1, #MIDR_REV_SHIFT, #MIDR_REV_BITS
17010bcd761SJeenu Viswambharan	bx	lr
17110bcd761SJeenu Viswambharanendfunc cpu_get_rev_var
17210bcd761SJeenu Viswambharan
17310bcd761SJeenu Viswambharan/*
17410bcd761SJeenu Viswambharan * Compare the CPU's revision-variant (r0) with a given value (r1), for errata
17510bcd761SJeenu Viswambharan * application purposes. If the revision-variant is less than or same as a given
17610bcd761SJeenu Viswambharan * value, indicates that errata applies; otherwise not.
17710bcd761SJeenu Viswambharan */
17810bcd761SJeenu Viswambharan	.globl	cpu_rev_var_ls
17910bcd761SJeenu Viswambharanfunc cpu_rev_var_ls
18010bcd761SJeenu Viswambharan	cmp	r0, r1
18110bcd761SJeenu Viswambharan	movls	r0, #ERRATA_APPLIES
18210bcd761SJeenu Viswambharan	movhi	r0, #ERRATA_NOT_APPLIES
18310bcd761SJeenu Viswambharan	bx	lr
18410bcd761SJeenu Viswambharanendfunc cpu_rev_var_ls
18510bcd761SJeenu Viswambharan
18656e04999SDimitris Papastamos/*
18756e04999SDimitris Papastamos * Compare the CPU's revision-variant (r0) with a given value (r1), for errata
18856e04999SDimitris Papastamos * application purposes. If the revision-variant is higher than or same as a
18956e04999SDimitris Papastamos * given value, indicates that errata applies; otherwise not.
19056e04999SDimitris Papastamos */
19156e04999SDimitris Papastamos	.globl	cpu_rev_var_hs
19256e04999SDimitris Papastamosfunc cpu_rev_var_hs
19356e04999SDimitris Papastamos	cmp	r0, r1
19456e04999SDimitris Papastamos	movge	r0, #ERRATA_APPLIES
19556e04999SDimitris Papastamos	movlt	r0, #ERRATA_NOT_APPLIES
19656e04999SDimitris Papastamos	bx	lr
19756e04999SDimitris Papastamosendfunc cpu_rev_var_hs
19856e04999SDimitris Papastamos
19910bcd761SJeenu Viswambharan#if REPORT_ERRATA
20010bcd761SJeenu Viswambharan/*
20110bcd761SJeenu Viswambharan * void print_errata_status(void);
20210bcd761SJeenu Viswambharan *
20310bcd761SJeenu Viswambharan * Function to print errata status for CPUs of its class. Must be called only:
20410bcd761SJeenu Viswambharan *
20510bcd761SJeenu Viswambharan *   - with MMU and data caches are enabled;
20610bcd761SJeenu Viswambharan *   - after cpu_ops have been initialized in per-CPU data.
20710bcd761SJeenu Viswambharan */
20810bcd761SJeenu Viswambharan	.globl print_errata_status
20910bcd761SJeenu Viswambharanfunc print_errata_status
2107af7038eSSoby Mathew	/* r12 is pushed only for the sake of 8-byte stack alignment */
2117af7038eSSoby Mathew	push	{r4, r5, r12, lr}
21210bcd761SJeenu Viswambharan#ifdef IMAGE_BL1
21310bcd761SJeenu Viswambharan	/*
21410bcd761SJeenu Viswambharan	 * BL1 doesn't have per-CPU data. So retrieve the CPU operations
21510bcd761SJeenu Viswambharan	 * directly.
21610bcd761SJeenu Viswambharan	 */
21710bcd761SJeenu Viswambharan	bl	get_cpu_ops_ptr
21810bcd761SJeenu Viswambharan	ldr	r0, [r0, #CPU_ERRATA_FUNC]
21910bcd761SJeenu Viswambharan	cmp	r0, #0
22010bcd761SJeenu Viswambharan	blxne	r0
22110bcd761SJeenu Viswambharan#else
22210bcd761SJeenu Viswambharan	/*
22310bcd761SJeenu Viswambharan	 * Retrieve pointer to cpu_ops, and further, the errata printing
22410bcd761SJeenu Viswambharan	 * function. If it's non-NULL, jump to the function in turn.
22510bcd761SJeenu Viswambharan	 */
22610bcd761SJeenu Viswambharan	bl	_cpu_data
22710bcd761SJeenu Viswambharan	ldr	r1, [r0, #CPU_DATA_CPU_OPS_PTR]
22810bcd761SJeenu Viswambharan	ldr	r0, [r1, #CPU_ERRATA_FUNC]
22910bcd761SJeenu Viswambharan	cmp	r0, #0
23010bcd761SJeenu Viswambharan	beq	1f
23110bcd761SJeenu Viswambharan
23210bcd761SJeenu Viswambharan	mov	r4, r0
23310bcd761SJeenu Viswambharan
23410bcd761SJeenu Viswambharan	/*
23510bcd761SJeenu Viswambharan	 * Load pointers to errata lock and printed flag. Call
23610bcd761SJeenu Viswambharan	 * errata_needs_reporting to check whether this CPU needs to report
23710bcd761SJeenu Viswambharan	 * errata status pertaining to its class.
23810bcd761SJeenu Viswambharan	 */
23910bcd761SJeenu Viswambharan	ldr	r0, [r1, #CPU_ERRATA_LOCK]
24010bcd761SJeenu Viswambharan	ldr	r1, [r1, #CPU_ERRATA_PRINTED]
24110bcd761SJeenu Viswambharan	bl	errata_needs_reporting
24210bcd761SJeenu Viswambharan	cmp	r0, #0
24310bcd761SJeenu Viswambharan	blxne	r4
24410bcd761SJeenu Viswambharan1:
24510bcd761SJeenu Viswambharan#endif
2467af7038eSSoby Mathew	pop	{r4, r5, r12, pc}
24710bcd761SJeenu Viswambharanendfunc print_errata_status
24810bcd761SJeenu Viswambharan#endif
249