xref: /rk3399_ARM-atf/lib/cpus/aarch64/cpu_helpers.S (revision 10bcd761574a5aaa208041382399e05275011603)
19b476841SSoby Mathew/*
2*10bcd761SJeenu Viswambharan * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
39b476841SSoby Mathew *
49b476841SSoby Mathew * Redistribution and use in source and binary forms, with or without
59b476841SSoby Mathew * modification, are permitted provided that the following conditions are met:
69b476841SSoby Mathew *
79b476841SSoby Mathew * Redistributions of source code must retain the above copyright notice, this
89b476841SSoby Mathew * list of conditions and the following disclaimer.
99b476841SSoby Mathew *
109b476841SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice,
119b476841SSoby Mathew * this list of conditions and the following disclaimer in the documentation
129b476841SSoby Mathew * and/or other materials provided with the distribution.
139b476841SSoby Mathew *
149b476841SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used
159b476841SSoby Mathew * to endorse or promote products derived from this software without specific
169b476841SSoby Mathew * prior written permission.
179b476841SSoby Mathew *
189b476841SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
199b476841SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209b476841SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219b476841SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
229b476841SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
239b476841SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
249b476841SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
259b476841SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
269b476841SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
279b476841SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
289b476841SSoby Mathew * POSSIBILITY OF SUCH DAMAGE.
299b476841SSoby Mathew */
309b476841SSoby Mathew
319b476841SSoby Mathew#include <arch.h>
329b476841SSoby Mathew#include <asm_macros.S>
339b476841SSoby Mathew#include <assert_macros.S>
343d8256b2SMasahiro Yamada#ifdef IMAGE_BL31
359b476841SSoby Mathew#include <cpu_data.h>
369b476841SSoby Mathew#endif
3755c70cb7SDavid Cunado#include <cpu_macros.S>
381319e7b1SSoby Mathew#include <debug.h>
39*10bcd761SJeenu Viswambharan#include <errata_report.h>
409b476841SSoby Mathew
419b476841SSoby Mathew /* Reset fn is needed in BL at reset vector */
423d8256b2SMasahiro Yamada#if defined(IMAGE_BL1) || defined(IMAGE_BL31)
439b476841SSoby Mathew	/*
449b476841SSoby Mathew	 * The reset handler common to all platforms.  After a matching
459b476841SSoby Mathew	 * cpu_ops structure entry is found, the correponding reset_handler
469b476841SSoby Mathew	 * in the cpu_ops is invoked.
47683f788fSSoby Mathew	 * Clobbers: x0 - x19, x30
489b476841SSoby Mathew	 */
499b476841SSoby Mathew	.globl	reset_handler
509b476841SSoby Mathewfunc reset_handler
517395a725SSoby Mathew	mov	x19, x30
529b476841SSoby Mathew
53683f788fSSoby Mathew	/* The plat_reset_handler can clobber x0 - x18, x30 */
5424fb838fSSoby Mathew	bl	plat_reset_handler
5524fb838fSSoby Mathew
569b476841SSoby Mathew	/* Get the matching cpu_ops pointer */
579b476841SSoby Mathew	bl	get_cpu_ops_ptr
589b476841SSoby Mathew#if ASM_ASSERTION
599b476841SSoby Mathew	cmp	x0, #0
609b476841SSoby Mathew	ASM_ASSERT(ne)
619b476841SSoby Mathew#endif
629b476841SSoby Mathew
639b476841SSoby Mathew	/* Get the cpu_ops reset handler */
649b476841SSoby Mathew	ldr	x2, [x0, #CPU_RESET_FUNC]
657395a725SSoby Mathew	mov	x30, x19
669b476841SSoby Mathew	cbz	x2, 1f
67683f788fSSoby Mathew
68683f788fSSoby Mathew	/* The cpu_ops reset handler can clobber x0 - x19, x30 */
697395a725SSoby Mathew	br	x2
709b476841SSoby Mathew1:
717395a725SSoby Mathew	ret
728b779620SKévin Petitendfunc reset_handler
7324fb838fSSoby Mathew
7479a97b2eSYatharth Kochar#endif /* IMAGE_BL1 || IMAGE_BL31 */
759b476841SSoby Mathew
763d8256b2SMasahiro Yamada#ifdef IMAGE_BL31 /* The power down core and cluster is needed only in  BL31 */
77add40351SSoby Mathew	/*
785dd9dbb5SJeenu Viswambharan	 * void prepare_cpu_pwr_dwn(unsigned int power_level)
795dd9dbb5SJeenu Viswambharan	 *
805dd9dbb5SJeenu Viswambharan	 * Prepare CPU power down function for all platforms. The function takes
815dd9dbb5SJeenu Viswambharan	 * a domain level to be powered down as its parameter. After the cpu_ops
825dd9dbb5SJeenu Viswambharan	 * pointer is retrieved from cpu_data, the handler for requested power
835dd9dbb5SJeenu Viswambharan	 * level is called.
84add40351SSoby Mathew	 */
855dd9dbb5SJeenu Viswambharan	.globl	prepare_cpu_pwr_dwn
865dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn
87add40351SSoby Mathew	/*
885dd9dbb5SJeenu Viswambharan	 * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the
895dd9dbb5SJeenu Viswambharan	 * power down handler for the last power level
90add40351SSoby Mathew	 */
915dd9dbb5SJeenu Viswambharan	mov_imm	x2, (CPU_MAX_PWR_DWN_OPS - 1)
925dd9dbb5SJeenu Viswambharan	cmp	x0, x2
935dd9dbb5SJeenu Viswambharan	csel	x2, x2, x0, hi
945dd9dbb5SJeenu Viswambharan
95add40351SSoby Mathew	mrs	x1, tpidr_el3
96add40351SSoby Mathew	ldr	x0, [x1, #CPU_DATA_CPU_OPS_PTR]
97add40351SSoby Mathew#if ASM_ASSERTION
98add40351SSoby Mathew	cmp	x0, #0
99add40351SSoby Mathew	ASM_ASSERT(ne)
100add40351SSoby Mathew#endif
101add40351SSoby Mathew
1025dd9dbb5SJeenu Viswambharan	/* Get the appropriate power down handler */
1035dd9dbb5SJeenu Viswambharan	mov	x1, #CPU_PWR_DWN_OPS
1045dd9dbb5SJeenu Viswambharan	add	x1, x1, x2, lsl #3
1055dd9dbb5SJeenu Viswambharan	ldr	x1, [x0, x1]
106add40351SSoby Mathew	br	x1
1075dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn
108add40351SSoby Mathew
109add40351SSoby Mathew
110add40351SSoby Mathew	/*
111add40351SSoby Mathew	 * Initializes the cpu_ops_ptr if not already initialized
11212e7c4abSVikram Kanigiri	 * in cpu_data. This can be called without a runtime stack, but may
11312e7c4abSVikram Kanigiri	 * only be called after the MMU is enabled.
114add40351SSoby Mathew	 * clobbers: x0 - x6, x10
115add40351SSoby Mathew	 */
116add40351SSoby Mathew	.globl	init_cpu_ops
117add40351SSoby Mathewfunc init_cpu_ops
118add40351SSoby Mathew	mrs	x6, tpidr_el3
119add40351SSoby Mathew	ldr	x0, [x6, #CPU_DATA_CPU_OPS_PTR]
120add40351SSoby Mathew	cbnz	x0, 1f
121add40351SSoby Mathew	mov	x10, x30
122add40351SSoby Mathew	bl	get_cpu_ops_ptr
123add40351SSoby Mathew#if ASM_ASSERTION
124add40351SSoby Mathew	cmp	x0, #0
125add40351SSoby Mathew	ASM_ASSERT(ne)
126add40351SSoby Mathew#endif
12709997346SSoby Mathew	str	x0, [x6, #CPU_DATA_CPU_OPS_PTR]!
128add40351SSoby Mathew	mov x30, x10
129add40351SSoby Mathew1:
130add40351SSoby Mathew	ret
1318b779620SKévin Petitendfunc init_cpu_ops
132add40351SSoby Mathew#endif /* IMAGE_BL31 */
133add40351SSoby Mathew
1343d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
135d3f70af6SSoby Mathew	/*
136d3f70af6SSoby Mathew	 * The cpu specific registers which need to be reported in a crash
137d3f70af6SSoby Mathew	 * are reported via cpu_ops cpu_reg_dump function. After a matching
138d3f70af6SSoby Mathew	 * cpu_ops structure entry is found, the correponding cpu_reg_dump
139d3f70af6SSoby Mathew	 * in the cpu_ops is invoked.
140d3f70af6SSoby Mathew	 */
141d3f70af6SSoby Mathew	.globl	do_cpu_reg_dump
142d3f70af6SSoby Mathewfunc do_cpu_reg_dump
143d3f70af6SSoby Mathew	mov	x16, x30
144d3f70af6SSoby Mathew
145d3f70af6SSoby Mathew	/* Get the matching cpu_ops pointer */
146d3f70af6SSoby Mathew	bl	get_cpu_ops_ptr
147d3f70af6SSoby Mathew	cbz	x0, 1f
148d3f70af6SSoby Mathew
149d3f70af6SSoby Mathew	/* Get the cpu_ops cpu_reg_dump */
150d3f70af6SSoby Mathew	ldr	x2, [x0, #CPU_REG_DUMP]
151d3f70af6SSoby Mathew	cbz	x2, 1f
152d3f70af6SSoby Mathew	blr	x2
153d3f70af6SSoby Mathew1:
154d3f70af6SSoby Mathew	mov	x30, x16
155d3f70af6SSoby Mathew	ret
1568b779620SKévin Petitendfunc do_cpu_reg_dump
157d3f70af6SSoby Mathew#endif
158d3f70af6SSoby Mathew
1599b476841SSoby Mathew	/*
1609b476841SSoby Mathew	 * The below function returns the cpu_ops structure matching the
1619b476841SSoby Mathew	 * midr of the core. It reads the MIDR_EL1 and finds the matching
1629b476841SSoby Mathew	 * entry in cpu_ops entries. Only the implementation and part number
1639b476841SSoby Mathew	 * are used to match the entries.
1649b476841SSoby Mathew	 * Return :
1659b476841SSoby Mathew	 *     x0 - The matching cpu_ops pointer on Success
1669b476841SSoby Mathew	 *     x0 - 0 on failure.
1679b476841SSoby Mathew	 * Clobbers : x0 - x5
1689b476841SSoby Mathew	 */
1699b476841SSoby Mathew	.globl	get_cpu_ops_ptr
1709b476841SSoby Mathewfunc get_cpu_ops_ptr
1719b476841SSoby Mathew	/* Get the cpu_ops start and end locations */
1729b476841SSoby Mathew	adr	x4, (__CPU_OPS_START__ + CPU_MIDR)
1739b476841SSoby Mathew	adr	x5, (__CPU_OPS_END__ + CPU_MIDR)
1749b476841SSoby Mathew
1759b476841SSoby Mathew	/* Initialize the return parameter */
1769b476841SSoby Mathew	mov	x0, #0
1779b476841SSoby Mathew
1789b476841SSoby Mathew	/* Read the MIDR_EL1 */
1799b476841SSoby Mathew	mrs	x2, midr_el1
1809b476841SSoby Mathew	mov_imm	x3, CPU_IMPL_PN_MASK
1819b476841SSoby Mathew
1829b476841SSoby Mathew	/* Retain only the implementation and part number using mask */
1839b476841SSoby Mathew	and	w2, w2, w3
1849b476841SSoby Mathew1:
1859b476841SSoby Mathew	/* Check if we have reached end of list */
1869b476841SSoby Mathew	cmp	x4, x5
1879b476841SSoby Mathew	b.eq	error_exit
1889b476841SSoby Mathew
1899b476841SSoby Mathew	/* load the midr from the cpu_ops */
1909b476841SSoby Mathew	ldr	x1, [x4], #CPU_OPS_SIZE
1919b476841SSoby Mathew	and	w1, w1, w3
1929b476841SSoby Mathew
1939b476841SSoby Mathew	/* Check if midr matches to midr of this core */
1949b476841SSoby Mathew	cmp	w1, w2
1959b476841SSoby Mathew	b.ne	1b
1969b476841SSoby Mathew
1979b476841SSoby Mathew	/* Subtract the increment and offset to get the cpu-ops pointer */
1989b476841SSoby Mathew	sub	x0, x4, #(CPU_OPS_SIZE + CPU_MIDR)
1999b476841SSoby Mathewerror_exit:
2009b476841SSoby Mathew	ret
2018b779620SKévin Petitendfunc get_cpu_ops_ptr
2027395a725SSoby Mathew
203*10bcd761SJeenu Viswambharan/*
204*10bcd761SJeenu Viswambharan * Extract CPU revision and variant, and combine them into a single numeric for
205*10bcd761SJeenu Viswambharan * easier comparison.
206*10bcd761SJeenu Viswambharan */
207*10bcd761SJeenu Viswambharan	.globl	cpu_get_rev_var
208*10bcd761SJeenu Viswambharanfunc cpu_get_rev_var
209*10bcd761SJeenu Viswambharan	mrs	x1, midr_el1
2107395a725SSoby Mathew
21154035fc4SSandrine Bailleux	/*
212*10bcd761SJeenu Viswambharan	 * Extract the variant[23:20] and revision[3:0] from MIDR, and pack them
213*10bcd761SJeenu Viswambharan	 * as variant[7:4] and revision[3:0] of x0.
21454035fc4SSandrine Bailleux	 *
215*10bcd761SJeenu Viswambharan	 * First extract x1[23:16] to x0[7:0] and zero fill the rest. Then
216*10bcd761SJeenu Viswambharan	 * extract x1[3:0] into x0[3:0] retaining other bits.
21754035fc4SSandrine Bailleux	 */
218*10bcd761SJeenu Viswambharan	ubfx	x0, x1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS)
219*10bcd761SJeenu Viswambharan	bfxil	x0, x1, #MIDR_REV_SHIFT, #MIDR_REV_BITS
220*10bcd761SJeenu Viswambharan	ret
221*10bcd761SJeenu Viswambharanendfunc cpu_get_rev_var
2227395a725SSoby Mathew
223*10bcd761SJeenu Viswambharan/*
224*10bcd761SJeenu Viswambharan * Compare the CPU's revision-variant (x0) with a given value (x1), for errata
225*10bcd761SJeenu Viswambharan * application purposes. If the revision-variant is less than or same as a given
226*10bcd761SJeenu Viswambharan * value, indicates that errata applies; otherwise not.
227*10bcd761SJeenu Viswambharan */
228*10bcd761SJeenu Viswambharan	.globl	cpu_rev_var_ls
229*10bcd761SJeenu Viswambharanfunc cpu_rev_var_ls
230*10bcd761SJeenu Viswambharan	mov	x2, #ERRATA_APPLIES
231*10bcd761SJeenu Viswambharan	mov	x3, #ERRATA_NOT_APPLIES
232*10bcd761SJeenu Viswambharan	cmp	x0, x1
233*10bcd761SJeenu Viswambharan	csel	x0, x2, x3, ls
234*10bcd761SJeenu Viswambharan	ret
235*10bcd761SJeenu Viswambharanendfunc cpu_rev_var_ls
236*10bcd761SJeenu Viswambharan
237*10bcd761SJeenu Viswambharan#if REPORT_ERRATA
238*10bcd761SJeenu Viswambharan/*
239*10bcd761SJeenu Viswambharan * void print_errata_status(void);
240*10bcd761SJeenu Viswambharan *
241*10bcd761SJeenu Viswambharan * Function to print errata status for CPUs of its class. Must be called only:
242*10bcd761SJeenu Viswambharan *
243*10bcd761SJeenu Viswambharan *   - with MMU and data caches are enabled;
244*10bcd761SJeenu Viswambharan *   - after cpu_ops have been initialized in per-CPU data.
245*10bcd761SJeenu Viswambharan */
246*10bcd761SJeenu Viswambharan	.globl print_errata_status
247*10bcd761SJeenu Viswambharanfunc print_errata_status
248*10bcd761SJeenu Viswambharan#ifdef IMAGE_BL1
249*10bcd761SJeenu Viswambharan	/*
250*10bcd761SJeenu Viswambharan	 * BL1 doesn't have per-CPU data. So retrieve the CPU operations
251*10bcd761SJeenu Viswambharan	 * directly.
252*10bcd761SJeenu Viswambharan	 */
253*10bcd761SJeenu Viswambharan	stp	xzr, x30, [sp, #-16]!
254*10bcd761SJeenu Viswambharan	bl	get_cpu_ops_ptr
255*10bcd761SJeenu Viswambharan	ldp	xzr, x30, [sp], #16
256*10bcd761SJeenu Viswambharan	ldr	x1, [x0, #CPU_ERRATA_FUNC]
257*10bcd761SJeenu Viswambharan	cbnz	x1, .Lprint
258*10bcd761SJeenu Viswambharan#else
259*10bcd761SJeenu Viswambharan	/*
260*10bcd761SJeenu Viswambharan	 * Retrieve pointer to cpu_ops from per-CPU data, and further, the
261*10bcd761SJeenu Viswambharan	 * errata printing function. If it's non-NULL, jump to the function in
262*10bcd761SJeenu Viswambharan	 * turn.
263*10bcd761SJeenu Viswambharan	 */
264*10bcd761SJeenu Viswambharan	mrs	x0, tpidr_el3
265*10bcd761SJeenu Viswambharan	ldr	x1, [x0, #CPU_DATA_CPU_OPS_PTR]
266*10bcd761SJeenu Viswambharan	ldr	x0, [x1, #CPU_ERRATA_FUNC]
267*10bcd761SJeenu Viswambharan	cbz	x0, .Lnoprint
268*10bcd761SJeenu Viswambharan
269*10bcd761SJeenu Viswambharan	/*
270*10bcd761SJeenu Viswambharan	 * Printing errata status requires atomically testing the printed flag.
271*10bcd761SJeenu Viswambharan	 */
272*10bcd761SJeenu Viswambharan	stp	x8, x30, [sp, #-16]!
273*10bcd761SJeenu Viswambharan	mov	x8, x0
274*10bcd761SJeenu Viswambharan
275*10bcd761SJeenu Viswambharan	/*
276*10bcd761SJeenu Viswambharan	 * Load pointers to errata lock and printed flag. Call
277*10bcd761SJeenu Viswambharan	 * errata_needs_reporting to check whether this CPU needs to report
278*10bcd761SJeenu Viswambharan	 * errata status pertaining to its class.
279*10bcd761SJeenu Viswambharan	 */
280*10bcd761SJeenu Viswambharan	ldr	x0, [x1, #CPU_ERRATA_LOCK]
281*10bcd761SJeenu Viswambharan	ldr	x1, [x1, #CPU_ERRATA_PRINTED]
282*10bcd761SJeenu Viswambharan	bl	errata_needs_reporting
283*10bcd761SJeenu Viswambharan	mov	x1, x8
284*10bcd761SJeenu Viswambharan	ldp	x8, x30, [sp], #16
285*10bcd761SJeenu Viswambharan	cbnz	x0, .Lprint
286*10bcd761SJeenu Viswambharan#endif
287*10bcd761SJeenu Viswambharan.Lnoprint:
288*10bcd761SJeenu Viswambharan	ret
289*10bcd761SJeenu Viswambharan.Lprint:
290*10bcd761SJeenu Viswambharan	/* Jump to errata reporting function for this CPU */
291*10bcd761SJeenu Viswambharan	br	x1
292*10bcd761SJeenu Viswambharanendfunc print_errata_status
293*10bcd761SJeenu Viswambharan#endif
294