xref: /rk3399_ARM-atf/lib/cpus/aarch32/cpu_helpers.S (revision dd9fae1ce0e7b985c9fe8f8f8ae358b8c166c6a9)
1e33b78a6SSoby Mathew/*
242d4d3baSArvind Ram Prakash * Copyright (c) 2016-2023, 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>
11c2ad38ceSVarun Wadekar#include <common/bl_common.h>
12*dd9fae1cSBoyan Karatotev#include <lib/cpus/cpu_ops.h>
1309d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
14e33b78a6SSoby Mathew
1542d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) || defined(IMAGE_BL32) || \
1642d4d3baSArvind Ram Prakash	(defined(IMAGE_BL2) && RESET_TO_BL2)
17e33b78a6SSoby Mathew	/*
18e33b78a6SSoby Mathew	 * The reset handler common to all platforms.  After a matching
19e33b78a6SSoby Mathew	 * cpu_ops structure entry is found, the correponding reset_handler
20e33b78a6SSoby Mathew	 * in the cpu_ops is invoked. The reset handler is invoked very early
21e33b78a6SSoby Mathew	 * in the boot sequence and it is assumed that we can clobber r0 - r10
22e33b78a6SSoby Mathew	 * without the need to follow AAPCS.
23e33b78a6SSoby Mathew	 * Clobbers: r0 - r10
24e33b78a6SSoby Mathew	 */
25e33b78a6SSoby Mathew	.globl	reset_handler
26e33b78a6SSoby Mathewfunc reset_handler
27c6c10b02SHeiko Stuebner	mov	r8, lr
28e33b78a6SSoby Mathew
29c6c10b02SHeiko Stuebner	/* The plat_reset_handler can clobber r0 - r7 */
30e33b78a6SSoby Mathew	bl	plat_reset_handler
31e33b78a6SSoby Mathew
32e33b78a6SSoby Mathew	/* Get the matching cpu_ops pointer (clobbers: r0 - r5) */
33e33b78a6SSoby Mathew	bl	get_cpu_ops_ptr
34e33b78a6SSoby Mathew
35044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
36e33b78a6SSoby Mathew	cmp	r0, #0
37e33b78a6SSoby Mathew	ASM_ASSERT(ne)
38e33b78a6SSoby Mathew#endif
39e33b78a6SSoby Mathew
40e33b78a6SSoby Mathew	/* Get the cpu_ops reset handler */
41e33b78a6SSoby Mathew	ldr	r1, [r0, #CPU_RESET_FUNC]
42e33b78a6SSoby Mathew	cmp	r1, #0
43c6c10b02SHeiko Stuebner	mov	lr, r8
44e33b78a6SSoby Mathew	bxne	r1
45e33b78a6SSoby Mathew	bx	lr
46e33b78a6SSoby Mathewendfunc reset_handler
47e33b78a6SSoby Mathew
48b1d27b48SRoberto Vargas#endif
491a0a3f06SYatharth Kochar
503d8256b2SMasahiro Yamada#ifdef IMAGE_BL32 /* The power down core and cluster is needed only in  BL32 */
51e33b78a6SSoby Mathew	/*
525dd9dbb5SJeenu Viswambharan	 * void prepare_cpu_pwr_dwn(unsigned int power_level)
535dd9dbb5SJeenu Viswambharan	 *
545dd9dbb5SJeenu Viswambharan	 * Prepare CPU power down function for all platforms. The function takes
555dd9dbb5SJeenu Viswambharan	 * a domain level to be powered down as its parameter. After the cpu_ops
565dd9dbb5SJeenu Viswambharan	 * pointer is retrieved from cpu_data, the handler for requested power
575dd9dbb5SJeenu Viswambharan	 * level is called.
58e33b78a6SSoby Mathew	 */
595dd9dbb5SJeenu Viswambharan	.globl	prepare_cpu_pwr_dwn
605dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn
61e33b78a6SSoby Mathew	/*
625dd9dbb5SJeenu Viswambharan	 * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the
635dd9dbb5SJeenu Viswambharan	 * power down handler for the last power level
64e33b78a6SSoby Mathew	 */
655dd9dbb5SJeenu Viswambharan	mov	r2, #(CPU_MAX_PWR_DWN_OPS - 1)
665dd9dbb5SJeenu Viswambharan	cmp	r0, r2
675dd9dbb5SJeenu Viswambharan	movhi	r0, r2
68e33b78a6SSoby Mathew
695dd9dbb5SJeenu Viswambharan	push	{r0, lr}
705dd9dbb5SJeenu Viswambharan	bl	_cpu_data
715dd9dbb5SJeenu Viswambharan	pop	{r2, lr}
725dd9dbb5SJeenu Viswambharan
735dd9dbb5SJeenu Viswambharan	ldr	r0, [r0, #CPU_DATA_CPU_OPS_PTR]
74044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
755dd9dbb5SJeenu Viswambharan	cmp	r0, #0
76e33b78a6SSoby Mathew	ASM_ASSERT(ne)
77e33b78a6SSoby Mathew#endif
78e33b78a6SSoby Mathew
795dd9dbb5SJeenu Viswambharan	/* Get the appropriate power down handler */
805dd9dbb5SJeenu Viswambharan	mov	r1, #CPU_PWR_DWN_OPS
815dd9dbb5SJeenu Viswambharan	add	r1, r1, r2, lsl #2
825dd9dbb5SJeenu Viswambharan	ldr	r1, [r0, r1]
83e3b9cc12SYann Gautier#if ENABLE_ASSERTIONS
84e3b9cc12SYann Gautier	cmp	r1, #0
85e3b9cc12SYann Gautier	ASM_ASSERT(ne)
86e3b9cc12SYann Gautier#endif
875dd9dbb5SJeenu Viswambharan	bx	r1
885dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn
89e33b78a6SSoby Mathew
90e33b78a6SSoby Mathew	/*
91e33b78a6SSoby Mathew	 * Initializes the cpu_ops_ptr if not already initialized
92e33b78a6SSoby Mathew	 * in cpu_data. This must only be called after the data cache
93e33b78a6SSoby Mathew	 * is enabled. AAPCS is followed.
94e33b78a6SSoby Mathew	 */
95e33b78a6SSoby Mathew	.globl	init_cpu_ops
96e33b78a6SSoby Mathewfunc init_cpu_ops
97e33b78a6SSoby Mathew	push	{r4 - r6, lr}
98e33b78a6SSoby Mathew	bl	_cpu_data
99e33b78a6SSoby Mathew	mov	r6, r0
100e33b78a6SSoby Mathew	ldr	r1, [r0, #CPU_DATA_CPU_OPS_PTR]
101e33b78a6SSoby Mathew	cmp	r1, #0
102e33b78a6SSoby Mathew	bne	1f
103e33b78a6SSoby Mathew	bl	get_cpu_ops_ptr
104044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
105e33b78a6SSoby Mathew	cmp	r0, #0
106e33b78a6SSoby Mathew	ASM_ASSERT(ne)
107e33b78a6SSoby Mathew#endif
108e33b78a6SSoby Mathew	str	r0, [r6, #CPU_DATA_CPU_OPS_PTR]!
109e33b78a6SSoby Mathew1:
110e33b78a6SSoby Mathew	pop	{r4 - r6, pc}
111e33b78a6SSoby Mathewendfunc init_cpu_ops
112e33b78a6SSoby Mathew
1131a0a3f06SYatharth Kochar#endif /* IMAGE_BL32 */
1141a0a3f06SYatharth Kochar
115e33b78a6SSoby Mathew	/*
116e33b78a6SSoby Mathew	 * The below function returns the cpu_ops structure matching the
117e33b78a6SSoby Mathew	 * midr of the core. It reads the MIDR and finds the matching
118e33b78a6SSoby Mathew	 * entry in cpu_ops entries. Only the implementation and part number
119e33b78a6SSoby Mathew	 * are used to match the entries.
120e33b78a6SSoby Mathew	 * Return :
121e33b78a6SSoby Mathew	 *     r0 - The matching cpu_ops pointer on Success
122e33b78a6SSoby Mathew	 *     r0 - 0 on failure.
123e33b78a6SSoby Mathew	 * Clobbers: r0 - r5
124e33b78a6SSoby Mathew	 */
125e33b78a6SSoby Mathew	.globl	get_cpu_ops_ptr
126e33b78a6SSoby Mathewfunc get_cpu_ops_ptr
127e33b78a6SSoby Mathew	/* Get the cpu_ops start and end locations */
128e33b78a6SSoby Mathew	ldr	r4, =(__CPU_OPS_START__ + CPU_MIDR)
129e33b78a6SSoby Mathew	ldr	r5, =(__CPU_OPS_END__ + CPU_MIDR)
130e33b78a6SSoby Mathew
131e33b78a6SSoby Mathew	/* Initialize the return parameter */
132e33b78a6SSoby Mathew	mov	r0, #0
133e33b78a6SSoby Mathew
134e33b78a6SSoby Mathew	/* Read the MIDR_EL1 */
135e33b78a6SSoby Mathew	ldcopr	r2, MIDR
136e33b78a6SSoby Mathew	ldr	r3, =CPU_IMPL_PN_MASK
137e33b78a6SSoby Mathew
138e33b78a6SSoby Mathew	/* Retain only the implementation and part number using mask */
139e33b78a6SSoby Mathew	and	r2, r2, r3
140e33b78a6SSoby Mathew1:
141e33b78a6SSoby Mathew	/* Check if we have reached end of list */
142e33b78a6SSoby Mathew	cmp	r4, r5
143355a5d03SDouglas Raillard	bhs	error_exit
144e33b78a6SSoby Mathew
145e33b78a6SSoby Mathew	/* load the midr from the cpu_ops */
146e33b78a6SSoby Mathew	ldr	r1, [r4], #CPU_OPS_SIZE
147e33b78a6SSoby Mathew	and	r1, r1, r3
148e33b78a6SSoby Mathew
149e33b78a6SSoby Mathew	/* Check if midr matches to midr of this core */
150e33b78a6SSoby Mathew	cmp	r1, r2
151e33b78a6SSoby Mathew	bne	1b
152e33b78a6SSoby Mathew
153e33b78a6SSoby Mathew	/* Subtract the increment and offset to get the cpu-ops pointer */
154e33b78a6SSoby Mathew	sub	r0, r4, #(CPU_OPS_SIZE + CPU_MIDR)
155e3b9cc12SYann Gautier#if ENABLE_ASSERTIONS
156e3b9cc12SYann Gautier	cmp	r0, #0
157e3b9cc12SYann Gautier	ASM_ASSERT(ne)
158e3b9cc12SYann Gautier#endif
159e33b78a6SSoby Mathewerror_exit:
160e33b78a6SSoby Mathew	bx	lr
161e33b78a6SSoby Mathewendfunc get_cpu_ops_ptr
16210bcd761SJeenu Viswambharan
16310bcd761SJeenu Viswambharan/*
16410bcd761SJeenu Viswambharan * Extract CPU revision and variant, and combine them into a single numeric for
16510bcd761SJeenu Viswambharan * easier comparison.
16610bcd761SJeenu Viswambharan */
16710bcd761SJeenu Viswambharan	.globl	cpu_get_rev_var
16810bcd761SJeenu Viswambharanfunc cpu_get_rev_var
16910bcd761SJeenu Viswambharan	ldcopr	r1, MIDR
17010bcd761SJeenu Viswambharan
17110bcd761SJeenu Viswambharan	/*
17210bcd761SJeenu Viswambharan	 * Extract the variant[23:20] and revision[3:0] from r1 and pack it in
17310bcd761SJeenu Viswambharan	 * r0[0:7] as variant[7:4] and revision[3:0]:
17410bcd761SJeenu Viswambharan	 *
17510bcd761SJeenu Viswambharan	 * First extract r1[23:16] to r0[7:0] and zero fill the rest. Then
17610bcd761SJeenu Viswambharan	 * extract r1[3:0] into r0[3:0] retaining other bits.
17710bcd761SJeenu Viswambharan	 */
17810bcd761SJeenu Viswambharan	ubfx	r0, r1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS)
17910bcd761SJeenu Viswambharan	bfi	r0, r1, #MIDR_REV_SHIFT, #MIDR_REV_BITS
18010bcd761SJeenu Viswambharan	bx	lr
18110bcd761SJeenu Viswambharanendfunc cpu_get_rev_var
18210bcd761SJeenu Viswambharan
18310bcd761SJeenu Viswambharan/*
18410bcd761SJeenu Viswambharan * Compare the CPU's revision-variant (r0) with a given value (r1), for errata
18510bcd761SJeenu Viswambharan * application purposes. If the revision-variant is less than or same as a given
18610bcd761SJeenu Viswambharan * value, indicates that errata applies; otherwise not.
18710bcd761SJeenu Viswambharan */
18810bcd761SJeenu Viswambharan	.globl	cpu_rev_var_ls
18910bcd761SJeenu Viswambharanfunc cpu_rev_var_ls
19010bcd761SJeenu Viswambharan	cmp	r0, r1
19110bcd761SJeenu Viswambharan	movls	r0, #ERRATA_APPLIES
19210bcd761SJeenu Viswambharan	movhi	r0, #ERRATA_NOT_APPLIES
19310bcd761SJeenu Viswambharan	bx	lr
19410bcd761SJeenu Viswambharanendfunc cpu_rev_var_ls
19510bcd761SJeenu Viswambharan
19656e04999SDimitris Papastamos/*
19756e04999SDimitris Papastamos * Compare the CPU's revision-variant (r0) with a given value (r1), for errata
19856e04999SDimitris Papastamos * application purposes. If the revision-variant is higher than or same as a
19956e04999SDimitris Papastamos * given value, indicates that errata applies; otherwise not.
20056e04999SDimitris Papastamos */
20156e04999SDimitris Papastamos	.globl	cpu_rev_var_hs
20256e04999SDimitris Papastamosfunc cpu_rev_var_hs
20356e04999SDimitris Papastamos	cmp	r0, r1
20456e04999SDimitris Papastamos	movge	r0, #ERRATA_APPLIES
20556e04999SDimitris Papastamos	movlt	r0, #ERRATA_NOT_APPLIES
20656e04999SDimitris Papastamos	bx	lr
20756e04999SDimitris Papastamosendfunc cpu_rev_var_hs
208