xref: /rk3399_ARM-atf/lib/cpus/aarch64/cpu_helpers.S (revision 22fa58cbfaa99a6458247c713bd03c2cac2a68fe)
19b476841SSoby Mathew/*
210bcd761SJeenu Viswambharan * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
39b476841SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
59b476841SSoby Mathew */
69b476841SSoby Mathew
79b476841SSoby Mathew#include <arch.h>
89b476841SSoby Mathew#include <asm_macros.S>
99b476841SSoby Mathew#include <assert_macros.S>
103d8256b2SMasahiro Yamada#ifdef IMAGE_BL31
119b476841SSoby Mathew#include <cpu_data.h>
129b476841SSoby Mathew#endif
1355c70cb7SDavid Cunado#include <cpu_macros.S>
141319e7b1SSoby Mathew#include <debug.h>
1510bcd761SJeenu Viswambharan#include <errata_report.h>
169b476841SSoby Mathew
179b476841SSoby Mathew /* Reset fn is needed in BL at reset vector */
183d8256b2SMasahiro Yamada#if defined(IMAGE_BL1) || defined(IMAGE_BL31)
199b476841SSoby Mathew	/*
209b476841SSoby Mathew	 * The reset handler common to all platforms.  After a matching
219b476841SSoby Mathew	 * cpu_ops structure entry is found, the correponding reset_handler
229b476841SSoby Mathew	 * in the cpu_ops is invoked.
23683f788fSSoby Mathew	 * Clobbers: x0 - x19, x30
249b476841SSoby Mathew	 */
259b476841SSoby Mathew	.globl	reset_handler
269b476841SSoby Mathewfunc reset_handler
277395a725SSoby Mathew	mov	x19, x30
289b476841SSoby Mathew
29683f788fSSoby Mathew	/* The plat_reset_handler can clobber x0 - x18, x30 */
3024fb838fSSoby Mathew	bl	plat_reset_handler
3124fb838fSSoby Mathew
329b476841SSoby Mathew	/* Get the matching cpu_ops pointer */
339b476841SSoby Mathew	bl	get_cpu_ops_ptr
34044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
359b476841SSoby Mathew	cmp	x0, #0
369b476841SSoby Mathew	ASM_ASSERT(ne)
379b476841SSoby Mathew#endif
389b476841SSoby Mathew
399b476841SSoby Mathew	/* Get the cpu_ops reset handler */
409b476841SSoby Mathew	ldr	x2, [x0, #CPU_RESET_FUNC]
417395a725SSoby Mathew	mov	x30, x19
429b476841SSoby Mathew	cbz	x2, 1f
43683f788fSSoby Mathew
44683f788fSSoby Mathew	/* The cpu_ops reset handler can clobber x0 - x19, x30 */
457395a725SSoby Mathew	br	x2
469b476841SSoby Mathew1:
477395a725SSoby Mathew	ret
488b779620SKévin Petitendfunc reset_handler
4924fb838fSSoby Mathew
5079a97b2eSYatharth Kochar#endif /* IMAGE_BL1 || IMAGE_BL31 */
519b476841SSoby Mathew
523d8256b2SMasahiro Yamada#ifdef IMAGE_BL31 /* The power down core and cluster is needed only in  BL31 */
53add40351SSoby Mathew	/*
545dd9dbb5SJeenu Viswambharan	 * void prepare_cpu_pwr_dwn(unsigned int power_level)
555dd9dbb5SJeenu Viswambharan	 *
565dd9dbb5SJeenu Viswambharan	 * Prepare CPU power down function for all platforms. The function takes
575dd9dbb5SJeenu Viswambharan	 * a domain level to be powered down as its parameter. After the cpu_ops
585dd9dbb5SJeenu Viswambharan	 * pointer is retrieved from cpu_data, the handler for requested power
595dd9dbb5SJeenu Viswambharan	 * level is called.
60add40351SSoby Mathew	 */
615dd9dbb5SJeenu Viswambharan	.globl	prepare_cpu_pwr_dwn
625dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn
63add40351SSoby Mathew	/*
645dd9dbb5SJeenu Viswambharan	 * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the
655dd9dbb5SJeenu Viswambharan	 * power down handler for the last power level
66add40351SSoby Mathew	 */
675dd9dbb5SJeenu Viswambharan	mov_imm	x2, (CPU_MAX_PWR_DWN_OPS - 1)
685dd9dbb5SJeenu Viswambharan	cmp	x0, x2
695dd9dbb5SJeenu Viswambharan	csel	x2, x2, x0, hi
705dd9dbb5SJeenu Viswambharan
71add40351SSoby Mathew	mrs	x1, tpidr_el3
72add40351SSoby Mathew	ldr	x0, [x1, #CPU_DATA_CPU_OPS_PTR]
73044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
74add40351SSoby Mathew	cmp	x0, #0
75add40351SSoby Mathew	ASM_ASSERT(ne)
76add40351SSoby Mathew#endif
77add40351SSoby Mathew
785dd9dbb5SJeenu Viswambharan	/* Get the appropriate power down handler */
795dd9dbb5SJeenu Viswambharan	mov	x1, #CPU_PWR_DWN_OPS
805dd9dbb5SJeenu Viswambharan	add	x1, x1, x2, lsl #3
815dd9dbb5SJeenu Viswambharan	ldr	x1, [x0, x1]
82add40351SSoby Mathew	br	x1
835dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn
84add40351SSoby Mathew
85add40351SSoby Mathew
86add40351SSoby Mathew	/*
87add40351SSoby Mathew	 * Initializes the cpu_ops_ptr if not already initialized
8812e7c4abSVikram Kanigiri	 * in cpu_data. This can be called without a runtime stack, but may
8912e7c4abSVikram Kanigiri	 * only be called after the MMU is enabled.
90add40351SSoby Mathew	 * clobbers: x0 - x6, x10
91add40351SSoby Mathew	 */
92add40351SSoby Mathew	.globl	init_cpu_ops
93add40351SSoby Mathewfunc init_cpu_ops
94add40351SSoby Mathew	mrs	x6, tpidr_el3
95add40351SSoby Mathew	ldr	x0, [x6, #CPU_DATA_CPU_OPS_PTR]
96add40351SSoby Mathew	cbnz	x0, 1f
97add40351SSoby Mathew	mov	x10, x30
98add40351SSoby Mathew	bl	get_cpu_ops_ptr
99044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
100add40351SSoby Mathew	cmp	x0, #0
101add40351SSoby Mathew	ASM_ASSERT(ne)
102add40351SSoby Mathew#endif
10309997346SSoby Mathew	str	x0, [x6, #CPU_DATA_CPU_OPS_PTR]!
104add40351SSoby Mathew	mov x30, x10
105add40351SSoby Mathew1:
106add40351SSoby Mathew	ret
1078b779620SKévin Petitendfunc init_cpu_ops
108add40351SSoby Mathew#endif /* IMAGE_BL31 */
109add40351SSoby Mathew
1103d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
111d3f70af6SSoby Mathew	/*
112d3f70af6SSoby Mathew	 * The cpu specific registers which need to be reported in a crash
113d3f70af6SSoby Mathew	 * are reported via cpu_ops cpu_reg_dump function. After a matching
114d3f70af6SSoby Mathew	 * cpu_ops structure entry is found, the correponding cpu_reg_dump
115d3f70af6SSoby Mathew	 * in the cpu_ops is invoked.
116d3f70af6SSoby Mathew	 */
117d3f70af6SSoby Mathew	.globl	do_cpu_reg_dump
118d3f70af6SSoby Mathewfunc do_cpu_reg_dump
119d3f70af6SSoby Mathew	mov	x16, x30
120d3f70af6SSoby Mathew
121d3f70af6SSoby Mathew	/* Get the matching cpu_ops pointer */
122d3f70af6SSoby Mathew	bl	get_cpu_ops_ptr
123d3f70af6SSoby Mathew	cbz	x0, 1f
124d3f70af6SSoby Mathew
125d3f70af6SSoby Mathew	/* Get the cpu_ops cpu_reg_dump */
126d3f70af6SSoby Mathew	ldr	x2, [x0, #CPU_REG_DUMP]
127d3f70af6SSoby Mathew	cbz	x2, 1f
128d3f70af6SSoby Mathew	blr	x2
129d3f70af6SSoby Mathew1:
130d3f70af6SSoby Mathew	mov	x30, x16
131d3f70af6SSoby Mathew	ret
1328b779620SKévin Petitendfunc do_cpu_reg_dump
133d3f70af6SSoby Mathew#endif
134d3f70af6SSoby Mathew
1359b476841SSoby Mathew	/*
1369b476841SSoby Mathew	 * The below function returns the cpu_ops structure matching the
1379b476841SSoby Mathew	 * midr of the core. It reads the MIDR_EL1 and finds the matching
1389b476841SSoby Mathew	 * entry in cpu_ops entries. Only the implementation and part number
1399b476841SSoby Mathew	 * are used to match the entries.
1409b476841SSoby Mathew	 * Return :
1419b476841SSoby Mathew	 *     x0 - The matching cpu_ops pointer on Success
1429b476841SSoby Mathew	 *     x0 - 0 on failure.
1439b476841SSoby Mathew	 * Clobbers : x0 - x5
1449b476841SSoby Mathew	 */
1459b476841SSoby Mathew	.globl	get_cpu_ops_ptr
1469b476841SSoby Mathewfunc get_cpu_ops_ptr
1479b476841SSoby Mathew	/* Get the cpu_ops start and end locations */
1489b476841SSoby Mathew	adr	x4, (__CPU_OPS_START__ + CPU_MIDR)
1499b476841SSoby Mathew	adr	x5, (__CPU_OPS_END__ + CPU_MIDR)
1509b476841SSoby Mathew
1519b476841SSoby Mathew	/* Initialize the return parameter */
1529b476841SSoby Mathew	mov	x0, #0
1539b476841SSoby Mathew
1549b476841SSoby Mathew	/* Read the MIDR_EL1 */
1559b476841SSoby Mathew	mrs	x2, midr_el1
1569b476841SSoby Mathew	mov_imm	x3, CPU_IMPL_PN_MASK
1579b476841SSoby Mathew
1589b476841SSoby Mathew	/* Retain only the implementation and part number using mask */
1599b476841SSoby Mathew	and	w2, w2, w3
1609b476841SSoby Mathew1:
1619b476841SSoby Mathew	/* Check if we have reached end of list */
1629b476841SSoby Mathew	cmp	x4, x5
1639b476841SSoby Mathew	b.eq	error_exit
1649b476841SSoby Mathew
1659b476841SSoby Mathew	/* load the midr from the cpu_ops */
1669b476841SSoby Mathew	ldr	x1, [x4], #CPU_OPS_SIZE
1679b476841SSoby Mathew	and	w1, w1, w3
1689b476841SSoby Mathew
1699b476841SSoby Mathew	/* Check if midr matches to midr of this core */
1709b476841SSoby Mathew	cmp	w1, w2
1719b476841SSoby Mathew	b.ne	1b
1729b476841SSoby Mathew
1739b476841SSoby Mathew	/* Subtract the increment and offset to get the cpu-ops pointer */
1749b476841SSoby Mathew	sub	x0, x4, #(CPU_OPS_SIZE + CPU_MIDR)
1759b476841SSoby Mathewerror_exit:
1769b476841SSoby Mathew	ret
1778b779620SKévin Petitendfunc get_cpu_ops_ptr
1787395a725SSoby Mathew
17910bcd761SJeenu Viswambharan/*
18010bcd761SJeenu Viswambharan * Extract CPU revision and variant, and combine them into a single numeric for
18110bcd761SJeenu Viswambharan * easier comparison.
18210bcd761SJeenu Viswambharan */
18310bcd761SJeenu Viswambharan	.globl	cpu_get_rev_var
18410bcd761SJeenu Viswambharanfunc cpu_get_rev_var
18510bcd761SJeenu Viswambharan	mrs	x1, midr_el1
1867395a725SSoby Mathew
18754035fc4SSandrine Bailleux	/*
18810bcd761SJeenu Viswambharan	 * Extract the variant[23:20] and revision[3:0] from MIDR, and pack them
18910bcd761SJeenu Viswambharan	 * as variant[7:4] and revision[3:0] of x0.
19054035fc4SSandrine Bailleux	 *
19110bcd761SJeenu Viswambharan	 * First extract x1[23:16] to x0[7:0] and zero fill the rest. Then
19210bcd761SJeenu Viswambharan	 * extract x1[3:0] into x0[3:0] retaining other bits.
19354035fc4SSandrine Bailleux	 */
19410bcd761SJeenu Viswambharan	ubfx	x0, x1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS)
19510bcd761SJeenu Viswambharan	bfxil	x0, x1, #MIDR_REV_SHIFT, #MIDR_REV_BITS
19610bcd761SJeenu Viswambharan	ret
19710bcd761SJeenu Viswambharanendfunc cpu_get_rev_var
1987395a725SSoby Mathew
19910bcd761SJeenu Viswambharan/*
20010bcd761SJeenu Viswambharan * Compare the CPU's revision-variant (x0) with a given value (x1), for errata
20110bcd761SJeenu Viswambharan * application purposes. If the revision-variant is less than or same as a given
20210bcd761SJeenu Viswambharan * value, indicates that errata applies; otherwise not.
20310bcd761SJeenu Viswambharan */
20410bcd761SJeenu Viswambharan	.globl	cpu_rev_var_ls
20510bcd761SJeenu Viswambharanfunc cpu_rev_var_ls
20610bcd761SJeenu Viswambharan	mov	x2, #ERRATA_APPLIES
20710bcd761SJeenu Viswambharan	mov	x3, #ERRATA_NOT_APPLIES
20810bcd761SJeenu Viswambharan	cmp	x0, x1
20910bcd761SJeenu Viswambharan	csel	x0, x2, x3, ls
21010bcd761SJeenu Viswambharan	ret
21110bcd761SJeenu Viswambharanendfunc cpu_rev_var_ls
21210bcd761SJeenu Viswambharan
213b75dc0e4SAndre Przywara/*
214b75dc0e4SAndre Przywara * Compare the CPU's revision-variant (x0) with a given value (x1), for errata
215b75dc0e4SAndre Przywara * application purposes. If the revision-variant is higher than or same as a
216b75dc0e4SAndre Przywara * given value, indicates that errata applies; otherwise not.
217b75dc0e4SAndre Przywara */
218b75dc0e4SAndre Przywara	.globl	cpu_rev_var_hs
219b75dc0e4SAndre Przywarafunc cpu_rev_var_hs
220b75dc0e4SAndre Przywara	mov	x2, #ERRATA_APPLIES
221b75dc0e4SAndre Przywara	mov	x3, #ERRATA_NOT_APPLIES
222b75dc0e4SAndre Przywara	cmp	x0, x1
223b75dc0e4SAndre Przywara	csel	x0, x2, x3, hs
224b75dc0e4SAndre Przywara	ret
225b75dc0e4SAndre Przywaraendfunc cpu_rev_var_hs
226b75dc0e4SAndre Przywara
22710bcd761SJeenu Viswambharan#if REPORT_ERRATA
22810bcd761SJeenu Viswambharan/*
22910bcd761SJeenu Viswambharan * void print_errata_status(void);
23010bcd761SJeenu Viswambharan *
23110bcd761SJeenu Viswambharan * Function to print errata status for CPUs of its class. Must be called only:
23210bcd761SJeenu Viswambharan *
23310bcd761SJeenu Viswambharan *   - with MMU and data caches are enabled;
23410bcd761SJeenu Viswambharan *   - after cpu_ops have been initialized in per-CPU data.
23510bcd761SJeenu Viswambharan */
23610bcd761SJeenu Viswambharan	.globl print_errata_status
23710bcd761SJeenu Viswambharanfunc print_errata_status
23810bcd761SJeenu Viswambharan#ifdef IMAGE_BL1
23910bcd761SJeenu Viswambharan	/*
24010bcd761SJeenu Viswambharan	 * BL1 doesn't have per-CPU data. So retrieve the CPU operations
24110bcd761SJeenu Viswambharan	 * directly.
24210bcd761SJeenu Viswambharan	 */
24310bcd761SJeenu Viswambharan	stp	xzr, x30, [sp, #-16]!
24410bcd761SJeenu Viswambharan	bl	get_cpu_ops_ptr
24510bcd761SJeenu Viswambharan	ldp	xzr, x30, [sp], #16
24610bcd761SJeenu Viswambharan	ldr	x1, [x0, #CPU_ERRATA_FUNC]
24710bcd761SJeenu Viswambharan	cbnz	x1, .Lprint
24810bcd761SJeenu Viswambharan#else
24910bcd761SJeenu Viswambharan	/*
25010bcd761SJeenu Viswambharan	 * Retrieve pointer to cpu_ops from per-CPU data, and further, the
25110bcd761SJeenu Viswambharan	 * errata printing function. If it's non-NULL, jump to the function in
25210bcd761SJeenu Viswambharan	 * turn.
25310bcd761SJeenu Viswambharan	 */
25410bcd761SJeenu Viswambharan	mrs	x0, tpidr_el3
25510bcd761SJeenu Viswambharan	ldr	x1, [x0, #CPU_DATA_CPU_OPS_PTR]
25610bcd761SJeenu Viswambharan	ldr	x0, [x1, #CPU_ERRATA_FUNC]
25710bcd761SJeenu Viswambharan	cbz	x0, .Lnoprint
25810bcd761SJeenu Viswambharan
25910bcd761SJeenu Viswambharan	/*
26010bcd761SJeenu Viswambharan	 * Printing errata status requires atomically testing the printed flag.
26110bcd761SJeenu Viswambharan	 */
262*22fa58cbSdp-arm	stp	x19, x30, [sp, #-16]!
263*22fa58cbSdp-arm	mov	x19, x0
26410bcd761SJeenu Viswambharan
26510bcd761SJeenu Viswambharan	/*
26610bcd761SJeenu Viswambharan	 * Load pointers to errata lock and printed flag. Call
26710bcd761SJeenu Viswambharan	 * errata_needs_reporting to check whether this CPU needs to report
26810bcd761SJeenu Viswambharan	 * errata status pertaining to its class.
26910bcd761SJeenu Viswambharan	 */
27010bcd761SJeenu Viswambharan	ldr	x0, [x1, #CPU_ERRATA_LOCK]
27110bcd761SJeenu Viswambharan	ldr	x1, [x1, #CPU_ERRATA_PRINTED]
27210bcd761SJeenu Viswambharan	bl	errata_needs_reporting
273*22fa58cbSdp-arm	mov	x1, x19
274*22fa58cbSdp-arm	ldp	x19, x30, [sp], #16
27510bcd761SJeenu Viswambharan	cbnz	x0, .Lprint
27610bcd761SJeenu Viswambharan#endif
27710bcd761SJeenu Viswambharan.Lnoprint:
27810bcd761SJeenu Viswambharan	ret
27910bcd761SJeenu Viswambharan.Lprint:
28010bcd761SJeenu Viswambharan	/* Jump to errata reporting function for this CPU */
28110bcd761SJeenu Viswambharan	br	x1
28210bcd761SJeenu Viswambharanendfunc print_errata_status
28310bcd761SJeenu Viswambharan#endif
284