xref: /rk3399_ARM-atf/lib/cpus/aarch64/cpu_helpers.S (revision 1319e7b19308e07bfa1234dd9aa785f72ab68cea)
19b476841SSoby Mathew/*
254035fc4SSandrine Bailleux * Copyright (c) 2014-2016, 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>
349b476841SSoby Mathew#include <cpu_macros.S>
359b476841SSoby Mathew#if IMAGE_BL31
369b476841SSoby Mathew#include <cpu_data.h>
379b476841SSoby Mathew#endif
38*1319e7b1SSoby Mathew#include <debug.h>
399b476841SSoby Mathew
409b476841SSoby Mathew /* Reset fn is needed in BL at reset vector */
4179a97b2eSYatharth Kochar#if IMAGE_BL1 || IMAGE_BL31
429b476841SSoby Mathew	/*
439b476841SSoby Mathew	 * The reset handler common to all platforms.  After a matching
449b476841SSoby Mathew	 * cpu_ops structure entry is found, the correponding reset_handler
459b476841SSoby Mathew	 * in the cpu_ops is invoked.
46683f788fSSoby Mathew	 * Clobbers: x0 - x19, x30
479b476841SSoby Mathew	 */
489b476841SSoby Mathew	.globl	reset_handler
499b476841SSoby Mathewfunc reset_handler
507395a725SSoby Mathew	mov	x19, x30
519b476841SSoby Mathew
52683f788fSSoby Mathew	/* The plat_reset_handler can clobber x0 - x18, x30 */
5324fb838fSSoby Mathew	bl	plat_reset_handler
5424fb838fSSoby Mathew
559b476841SSoby Mathew	/* Get the matching cpu_ops pointer */
569b476841SSoby Mathew	bl	get_cpu_ops_ptr
579b476841SSoby Mathew#if ASM_ASSERTION
589b476841SSoby Mathew	cmp	x0, #0
599b476841SSoby Mathew	ASM_ASSERT(ne)
609b476841SSoby Mathew#endif
619b476841SSoby Mathew
629b476841SSoby Mathew	/* Get the cpu_ops reset handler */
639b476841SSoby Mathew	ldr	x2, [x0, #CPU_RESET_FUNC]
647395a725SSoby Mathew	mov	x30, x19
659b476841SSoby Mathew	cbz	x2, 1f
66683f788fSSoby Mathew
67683f788fSSoby Mathew	/* The cpu_ops reset handler can clobber x0 - x19, x30 */
687395a725SSoby Mathew	br	x2
699b476841SSoby Mathew1:
707395a725SSoby Mathew	ret
718b779620SKévin Petitendfunc reset_handler
7224fb838fSSoby Mathew
7379a97b2eSYatharth Kochar#endif /* IMAGE_BL1 || IMAGE_BL31 */
749b476841SSoby Mathew
75add40351SSoby Mathew#if IMAGE_BL31 /* The power down core and cluster is needed only in  BL31 */
76add40351SSoby Mathew	/*
77add40351SSoby Mathew	 * The prepare core power down function for all platforms.  After
78add40351SSoby Mathew	 * the cpu_ops pointer is retrieved from cpu_data, the corresponding
79add40351SSoby Mathew	 * pwr_dwn_core in the cpu_ops is invoked.
80add40351SSoby Mathew	 */
81add40351SSoby Mathew	.globl	prepare_core_pwr_dwn
82add40351SSoby Mathewfunc prepare_core_pwr_dwn
83add40351SSoby Mathew	mrs	x1, tpidr_el3
84add40351SSoby Mathew	ldr	x0, [x1, #CPU_DATA_CPU_OPS_PTR]
85add40351SSoby Mathew#if ASM_ASSERTION
86add40351SSoby Mathew	cmp	x0, #0
87add40351SSoby Mathew	ASM_ASSERT(ne)
88add40351SSoby Mathew#endif
89add40351SSoby Mathew
90add40351SSoby Mathew	/* Get the cpu_ops core_pwr_dwn handler */
91add40351SSoby Mathew	ldr	x1, [x0, #CPU_PWR_DWN_CORE]
92add40351SSoby Mathew	br	x1
938b779620SKévin Petitendfunc prepare_core_pwr_dwn
94add40351SSoby Mathew
95add40351SSoby Mathew	/*
96add40351SSoby Mathew	 * The prepare cluster power down function for all platforms.  After
97add40351SSoby Mathew	 * the cpu_ops pointer is retrieved from cpu_data, the corresponding
98add40351SSoby Mathew	 * pwr_dwn_cluster in the cpu_ops is invoked.
99add40351SSoby Mathew	 */
100add40351SSoby Mathew	.globl	prepare_cluster_pwr_dwn
101add40351SSoby Mathewfunc prepare_cluster_pwr_dwn
102add40351SSoby Mathew	mrs	x1, tpidr_el3
103add40351SSoby Mathew	ldr	x0, [x1, #CPU_DATA_CPU_OPS_PTR]
104add40351SSoby Mathew#if ASM_ASSERTION
105add40351SSoby Mathew	cmp	x0, #0
106add40351SSoby Mathew	ASM_ASSERT(ne)
107add40351SSoby Mathew#endif
108add40351SSoby Mathew
109add40351SSoby Mathew	/* Get the cpu_ops cluster_pwr_dwn handler */
110add40351SSoby Mathew	ldr	x1, [x0, #CPU_PWR_DWN_CLUSTER]
111add40351SSoby Mathew	br	x1
1128b779620SKévin Petitendfunc prepare_cluster_pwr_dwn
113add40351SSoby Mathew
114add40351SSoby Mathew
115add40351SSoby Mathew	/*
116add40351SSoby Mathew	 * Initializes the cpu_ops_ptr if not already initialized
11712e7c4abSVikram Kanigiri	 * in cpu_data. This can be called without a runtime stack, but may
11812e7c4abSVikram Kanigiri	 * only be called after the MMU is enabled.
119add40351SSoby Mathew	 * clobbers: x0 - x6, x10
120add40351SSoby Mathew	 */
121add40351SSoby Mathew	.globl	init_cpu_ops
122add40351SSoby Mathewfunc init_cpu_ops
123add40351SSoby Mathew	mrs	x6, tpidr_el3
124add40351SSoby Mathew	ldr	x0, [x6, #CPU_DATA_CPU_OPS_PTR]
125add40351SSoby Mathew	cbnz	x0, 1f
126add40351SSoby Mathew	mov	x10, x30
127add40351SSoby Mathew	bl	get_cpu_ops_ptr
128add40351SSoby Mathew#if ASM_ASSERTION
129add40351SSoby Mathew	cmp	x0, #0
130add40351SSoby Mathew	ASM_ASSERT(ne)
131add40351SSoby Mathew#endif
13209997346SSoby Mathew	str	x0, [x6, #CPU_DATA_CPU_OPS_PTR]!
133add40351SSoby Mathew	mov x30, x10
134add40351SSoby Mathew1:
135add40351SSoby Mathew	ret
1368b779620SKévin Petitendfunc init_cpu_ops
137add40351SSoby Mathew#endif /* IMAGE_BL31 */
138add40351SSoby Mathew
139d3f70af6SSoby Mathew#if IMAGE_BL31 && CRASH_REPORTING
140d3f70af6SSoby Mathew	/*
141d3f70af6SSoby Mathew	 * The cpu specific registers which need to be reported in a crash
142d3f70af6SSoby Mathew	 * are reported via cpu_ops cpu_reg_dump function. After a matching
143d3f70af6SSoby Mathew	 * cpu_ops structure entry is found, the correponding cpu_reg_dump
144d3f70af6SSoby Mathew	 * in the cpu_ops is invoked.
145d3f70af6SSoby Mathew	 */
146d3f70af6SSoby Mathew	.globl	do_cpu_reg_dump
147d3f70af6SSoby Mathewfunc do_cpu_reg_dump
148d3f70af6SSoby Mathew	mov	x16, x30
149d3f70af6SSoby Mathew
150d3f70af6SSoby Mathew	/* Get the matching cpu_ops pointer */
151d3f70af6SSoby Mathew	bl	get_cpu_ops_ptr
152d3f70af6SSoby Mathew	cbz	x0, 1f
153d3f70af6SSoby Mathew
154d3f70af6SSoby Mathew	/* Get the cpu_ops cpu_reg_dump */
155d3f70af6SSoby Mathew	ldr	x2, [x0, #CPU_REG_DUMP]
156d3f70af6SSoby Mathew	cbz	x2, 1f
157d3f70af6SSoby Mathew	blr	x2
158d3f70af6SSoby Mathew1:
159d3f70af6SSoby Mathew	mov	x30, x16
160d3f70af6SSoby Mathew	ret
1618b779620SKévin Petitendfunc do_cpu_reg_dump
162d3f70af6SSoby Mathew#endif
163d3f70af6SSoby Mathew
1649b476841SSoby Mathew	/*
1659b476841SSoby Mathew	 * The below function returns the cpu_ops structure matching the
1669b476841SSoby Mathew	 * midr of the core. It reads the MIDR_EL1 and finds the matching
1679b476841SSoby Mathew	 * entry in cpu_ops entries. Only the implementation and part number
1689b476841SSoby Mathew	 * are used to match the entries.
1699b476841SSoby Mathew	 * Return :
1709b476841SSoby Mathew	 *     x0 - The matching cpu_ops pointer on Success
1719b476841SSoby Mathew	 *     x0 - 0 on failure.
1729b476841SSoby Mathew	 * Clobbers : x0 - x5
1739b476841SSoby Mathew	 */
1749b476841SSoby Mathew	.globl	get_cpu_ops_ptr
1759b476841SSoby Mathewfunc get_cpu_ops_ptr
1769b476841SSoby Mathew	/* Get the cpu_ops start and end locations */
1779b476841SSoby Mathew	adr	x4, (__CPU_OPS_START__ + CPU_MIDR)
1789b476841SSoby Mathew	adr	x5, (__CPU_OPS_END__ + CPU_MIDR)
1799b476841SSoby Mathew
1809b476841SSoby Mathew	/* Initialize the return parameter */
1819b476841SSoby Mathew	mov	x0, #0
1829b476841SSoby Mathew
1839b476841SSoby Mathew	/* Read the MIDR_EL1 */
1849b476841SSoby Mathew	mrs	x2, midr_el1
1859b476841SSoby Mathew	mov_imm	x3, CPU_IMPL_PN_MASK
1869b476841SSoby Mathew
1879b476841SSoby Mathew	/* Retain only the implementation and part number using mask */
1889b476841SSoby Mathew	and	w2, w2, w3
1899b476841SSoby Mathew1:
1909b476841SSoby Mathew	/* Check if we have reached end of list */
1919b476841SSoby Mathew	cmp	x4, x5
1929b476841SSoby Mathew	b.eq	error_exit
1939b476841SSoby Mathew
1949b476841SSoby Mathew	/* load the midr from the cpu_ops */
1959b476841SSoby Mathew	ldr	x1, [x4], #CPU_OPS_SIZE
1969b476841SSoby Mathew	and	w1, w1, w3
1979b476841SSoby Mathew
1989b476841SSoby Mathew	/* Check if midr matches to midr of this core */
1999b476841SSoby Mathew	cmp	w1, w2
2009b476841SSoby Mathew	b.ne	1b
2019b476841SSoby Mathew
2029b476841SSoby Mathew	/* Subtract the increment and offset to get the cpu-ops pointer */
2039b476841SSoby Mathew	sub	x0, x4, #(CPU_OPS_SIZE + CPU_MIDR)
2049b476841SSoby Mathewerror_exit:
2059b476841SSoby Mathew	ret
2068b779620SKévin Petitendfunc get_cpu_ops_ptr
2077395a725SSoby Mathew
208*1319e7b1SSoby Mathew#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
209*1319e7b1SSoby Mathew.section .rodata.rev_verbose_str, "aS"
210*1319e7b1SSoby Mathewrev_verbose_str:
211*1319e7b1SSoby Mathew	.asciz "VERBOSE: Skipping CPU specific reset operation for non-matching CPU revision number.\n"
2127395a725SSoby Mathew
21354035fc4SSandrine Bailleux	/*
21454035fc4SSandrine Bailleux	 * This function prints the above warning message to the crash console.
21554035fc4SSandrine Bailleux	 * It should be called when a CPU specific operation is enabled in the
21654035fc4SSandrine Bailleux	 * build but doesn't apply to this CPU revision/part number.
21754035fc4SSandrine Bailleux	 *
21854035fc4SSandrine Bailleux	 * Clobber: x30, x0 - x5
21954035fc4SSandrine Bailleux	 */
2207395a725SSoby Mathew	.globl	print_revision_warning
2217395a725SSoby Mathewfunc print_revision_warning
2227395a725SSoby Mathew	mov	x5, x30
2237395a725SSoby Mathew	/* Ensure the console is initialized */
2247395a725SSoby Mathew	bl	plat_crash_console_init
2257395a725SSoby Mathew	/* Check if the console is initialized */
2267395a725SSoby Mathew	cbz	x0, 1f
2277395a725SSoby Mathew	/* The console is initialized */
228*1319e7b1SSoby Mathew	adr	x4, rev_verbose_str
2297395a725SSoby Mathew	bl	asm_print_str
2307395a725SSoby Mathew1:
2317395a725SSoby Mathew	ret	x5
2328b779620SKévin Petitendfunc print_revision_warning
2337395a725SSoby Mathew#endif
2347395a725SSoby Mathew
235