xref: /rk3399_ARM-atf/lib/cpus/aarch64/cpu_helpers.S (revision c2ad38ce4fe754c750f3db480f732ac280f508e4)
19b476841SSoby Mathew/*
2*c2ad38ceSVarun Wadekar * Copyright (c) 2014-2019, 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>
10*c2ad38ceSVarun Wadekar#include <common/bl_common.h>
1109d40e0eSAntonio Nino Diaz#include <common/debug.h>
1255c70cb7SDavid Cunado#include <cpu_macros.S>
1309d40e0eSAntonio Nino Diaz#include <lib/cpus/errata_report.h>
1409d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
159b476841SSoby Mathew
169b476841SSoby Mathew /* Reset fn is needed in BL at reset vector */
17b1d27b48SRoberto Vargas#if defined(IMAGE_BL1) || defined(IMAGE_BL31) || (defined(IMAGE_BL2) && BL2_AT_EL3)
189b476841SSoby Mathew	/*
199b476841SSoby Mathew	 * The reset handler common to all platforms.  After a matching
209b476841SSoby Mathew	 * cpu_ops structure entry is found, the correponding reset_handler
219b476841SSoby Mathew	 * in the cpu_ops is invoked.
22683f788fSSoby Mathew	 * Clobbers: x0 - x19, x30
239b476841SSoby Mathew	 */
249b476841SSoby Mathew	.globl	reset_handler
259b476841SSoby Mathewfunc reset_handler
267395a725SSoby Mathew	mov	x19, x30
279b476841SSoby Mathew
28683f788fSSoby Mathew	/* The plat_reset_handler can clobber x0 - x18, x30 */
2924fb838fSSoby Mathew	bl	plat_reset_handler
3024fb838fSSoby Mathew
319b476841SSoby Mathew	/* Get the matching cpu_ops pointer */
329b476841SSoby Mathew	bl	get_cpu_ops_ptr
33044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
349b476841SSoby Mathew	cmp	x0, #0
359b476841SSoby Mathew	ASM_ASSERT(ne)
369b476841SSoby Mathew#endif
379b476841SSoby Mathew
389b476841SSoby Mathew	/* Get the cpu_ops reset handler */
399b476841SSoby Mathew	ldr	x2, [x0, #CPU_RESET_FUNC]
407395a725SSoby Mathew	mov	x30, x19
419b476841SSoby Mathew	cbz	x2, 1f
42683f788fSSoby Mathew
43683f788fSSoby Mathew	/* The cpu_ops reset handler can clobber x0 - x19, x30 */
447395a725SSoby Mathew	br	x2
459b476841SSoby Mathew1:
467395a725SSoby Mathew	ret
478b779620SKévin Petitendfunc reset_handler
4824fb838fSSoby Mathew
49b1d27b48SRoberto Vargas#endif
509b476841SSoby Mathew
513d8256b2SMasahiro Yamada#ifdef IMAGE_BL31 /* The power down core and cluster is needed only in  BL31 */
52add40351SSoby Mathew	/*
535dd9dbb5SJeenu Viswambharan	 * void prepare_cpu_pwr_dwn(unsigned int power_level)
545dd9dbb5SJeenu Viswambharan	 *
555dd9dbb5SJeenu Viswambharan	 * Prepare CPU power down function for all platforms. The function takes
565dd9dbb5SJeenu Viswambharan	 * a domain level to be powered down as its parameter. After the cpu_ops
575dd9dbb5SJeenu Viswambharan	 * pointer is retrieved from cpu_data, the handler for requested power
585dd9dbb5SJeenu Viswambharan	 * level is called.
59add40351SSoby Mathew	 */
605dd9dbb5SJeenu Viswambharan	.globl	prepare_cpu_pwr_dwn
615dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn
62add40351SSoby Mathew	/*
635dd9dbb5SJeenu Viswambharan	 * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the
645dd9dbb5SJeenu Viswambharan	 * power down handler for the last power level
65add40351SSoby Mathew	 */
665dd9dbb5SJeenu Viswambharan	mov_imm	x2, (CPU_MAX_PWR_DWN_OPS - 1)
675dd9dbb5SJeenu Viswambharan	cmp	x0, x2
685dd9dbb5SJeenu Viswambharan	csel	x2, x2, x0, hi
695dd9dbb5SJeenu Viswambharan
70add40351SSoby Mathew	mrs	x1, tpidr_el3
71add40351SSoby Mathew	ldr	x0, [x1, #CPU_DATA_CPU_OPS_PTR]
72044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
73add40351SSoby Mathew	cmp	x0, #0
74add40351SSoby Mathew	ASM_ASSERT(ne)
75add40351SSoby Mathew#endif
76add40351SSoby Mathew
775dd9dbb5SJeenu Viswambharan	/* Get the appropriate power down handler */
785dd9dbb5SJeenu Viswambharan	mov	x1, #CPU_PWR_DWN_OPS
795dd9dbb5SJeenu Viswambharan	add	x1, x1, x2, lsl #3
805dd9dbb5SJeenu Viswambharan	ldr	x1, [x0, x1]
81add40351SSoby Mathew	br	x1
825dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn
83add40351SSoby Mathew
84add40351SSoby Mathew
85add40351SSoby Mathew	/*
86add40351SSoby Mathew	 * Initializes the cpu_ops_ptr if not already initialized
8712e7c4abSVikram Kanigiri	 * in cpu_data. This can be called without a runtime stack, but may
8812e7c4abSVikram Kanigiri	 * only be called after the MMU is enabled.
89add40351SSoby Mathew	 * clobbers: x0 - x6, x10
90add40351SSoby Mathew	 */
91add40351SSoby Mathew	.globl	init_cpu_ops
92add40351SSoby Mathewfunc init_cpu_ops
93add40351SSoby Mathew	mrs	x6, tpidr_el3
94add40351SSoby Mathew	ldr	x0, [x6, #CPU_DATA_CPU_OPS_PTR]
95add40351SSoby Mathew	cbnz	x0, 1f
96add40351SSoby Mathew	mov	x10, x30
97add40351SSoby Mathew	bl	get_cpu_ops_ptr
98044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
99add40351SSoby Mathew	cmp	x0, #0
100add40351SSoby Mathew	ASM_ASSERT(ne)
101add40351SSoby Mathew#endif
10209997346SSoby Mathew	str	x0, [x6, #CPU_DATA_CPU_OPS_PTR]!
103add40351SSoby Mathew	mov x30, x10
104add40351SSoby Mathew1:
105add40351SSoby Mathew	ret
1068b779620SKévin Petitendfunc init_cpu_ops
107add40351SSoby Mathew#endif /* IMAGE_BL31 */
108add40351SSoby Mathew
1093d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
110d3f70af6SSoby Mathew	/*
111d3f70af6SSoby Mathew	 * The cpu specific registers which need to be reported in a crash
112d3f70af6SSoby Mathew	 * are reported via cpu_ops cpu_reg_dump function. After a matching
113d3f70af6SSoby Mathew	 * cpu_ops structure entry is found, the correponding cpu_reg_dump
114d3f70af6SSoby Mathew	 * in the cpu_ops is invoked.
115d3f70af6SSoby Mathew	 */
116d3f70af6SSoby Mathew	.globl	do_cpu_reg_dump
117d3f70af6SSoby Mathewfunc do_cpu_reg_dump
118d3f70af6SSoby Mathew	mov	x16, x30
119d3f70af6SSoby Mathew
120d3f70af6SSoby Mathew	/* Get the matching cpu_ops pointer */
121d3f70af6SSoby Mathew	bl	get_cpu_ops_ptr
122d3f70af6SSoby Mathew	cbz	x0, 1f
123d3f70af6SSoby Mathew
124d3f70af6SSoby Mathew	/* Get the cpu_ops cpu_reg_dump */
125d3f70af6SSoby Mathew	ldr	x2, [x0, #CPU_REG_DUMP]
126d3f70af6SSoby Mathew	cbz	x2, 1f
127d3f70af6SSoby Mathew	blr	x2
128d3f70af6SSoby Mathew1:
129d3f70af6SSoby Mathew	mov	x30, x16
130d3f70af6SSoby Mathew	ret
1318b779620SKévin Petitendfunc do_cpu_reg_dump
132d3f70af6SSoby Mathew#endif
133d3f70af6SSoby Mathew
1349b476841SSoby Mathew	/*
1359b476841SSoby Mathew	 * The below function returns the cpu_ops structure matching the
1369b476841SSoby Mathew	 * midr of the core. It reads the MIDR_EL1 and finds the matching
1379b476841SSoby Mathew	 * entry in cpu_ops entries. Only the implementation and part number
1389b476841SSoby Mathew	 * are used to match the entries.
1399b476841SSoby Mathew	 * Return :
1409b476841SSoby Mathew	 *     x0 - The matching cpu_ops pointer on Success
1419b476841SSoby Mathew	 *     x0 - 0 on failure.
1429b476841SSoby Mathew	 * Clobbers : x0 - x5
1439b476841SSoby Mathew	 */
1449b476841SSoby Mathew	.globl	get_cpu_ops_ptr
1459b476841SSoby Mathewfunc get_cpu_ops_ptr
1469b476841SSoby Mathew	/* Get the cpu_ops start and end locations */
1479b476841SSoby Mathew	adr	x4, (__CPU_OPS_START__ + CPU_MIDR)
1489b476841SSoby Mathew	adr	x5, (__CPU_OPS_END__ + CPU_MIDR)
1499b476841SSoby Mathew
1509b476841SSoby Mathew	/* Initialize the return parameter */
1519b476841SSoby Mathew	mov	x0, #0
1529b476841SSoby Mathew
1539b476841SSoby Mathew	/* Read the MIDR_EL1 */
1549b476841SSoby Mathew	mrs	x2, midr_el1
1559b476841SSoby Mathew	mov_imm	x3, CPU_IMPL_PN_MASK
1569b476841SSoby Mathew
1579b476841SSoby Mathew	/* Retain only the implementation and part number using mask */
1589b476841SSoby Mathew	and	w2, w2, w3
1599b476841SSoby Mathew1:
1609b476841SSoby Mathew	/* Check if we have reached end of list */
1619b476841SSoby Mathew	cmp	x4, x5
1629b476841SSoby Mathew	b.eq	error_exit
1639b476841SSoby Mathew
1649b476841SSoby Mathew	/* load the midr from the cpu_ops */
1659b476841SSoby Mathew	ldr	x1, [x4], #CPU_OPS_SIZE
1669b476841SSoby Mathew	and	w1, w1, w3
1679b476841SSoby Mathew
1689b476841SSoby Mathew	/* Check if midr matches to midr of this core */
1699b476841SSoby Mathew	cmp	w1, w2
1709b476841SSoby Mathew	b.ne	1b
1719b476841SSoby Mathew
1729b476841SSoby Mathew	/* Subtract the increment and offset to get the cpu-ops pointer */
1739b476841SSoby Mathew	sub	x0, x4, #(CPU_OPS_SIZE + CPU_MIDR)
1749b476841SSoby Mathewerror_exit:
1759b476841SSoby Mathew	ret
1768b779620SKévin Petitendfunc get_cpu_ops_ptr
1777395a725SSoby Mathew
17810bcd761SJeenu Viswambharan/*
17910bcd761SJeenu Viswambharan * Extract CPU revision and variant, and combine them into a single numeric for
18010bcd761SJeenu Viswambharan * easier comparison.
18110bcd761SJeenu Viswambharan */
18210bcd761SJeenu Viswambharan	.globl	cpu_get_rev_var
18310bcd761SJeenu Viswambharanfunc cpu_get_rev_var
18410bcd761SJeenu Viswambharan	mrs	x1, midr_el1
1857395a725SSoby Mathew
18654035fc4SSandrine Bailleux	/*
18710bcd761SJeenu Viswambharan	 * Extract the variant[23:20] and revision[3:0] from MIDR, and pack them
18810bcd761SJeenu Viswambharan	 * as variant[7:4] and revision[3:0] of x0.
18954035fc4SSandrine Bailleux	 *
19010bcd761SJeenu Viswambharan	 * First extract x1[23:16] to x0[7:0] and zero fill the rest. Then
19110bcd761SJeenu Viswambharan	 * extract x1[3:0] into x0[3:0] retaining other bits.
19254035fc4SSandrine Bailleux	 */
19310bcd761SJeenu Viswambharan	ubfx	x0, x1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS)
19410bcd761SJeenu Viswambharan	bfxil	x0, x1, #MIDR_REV_SHIFT, #MIDR_REV_BITS
19510bcd761SJeenu Viswambharan	ret
19610bcd761SJeenu Viswambharanendfunc cpu_get_rev_var
1977395a725SSoby Mathew
19810bcd761SJeenu Viswambharan/*
19910bcd761SJeenu Viswambharan * Compare the CPU's revision-variant (x0) with a given value (x1), for errata
20010bcd761SJeenu Viswambharan * application purposes. If the revision-variant is less than or same as a given
20110bcd761SJeenu Viswambharan * value, indicates that errata applies; otherwise not.
2029ec3921cSJonathan Wright *
2039ec3921cSJonathan Wright * Shall clobber: x0-x3
20410bcd761SJeenu Viswambharan */
20510bcd761SJeenu Viswambharan	.globl	cpu_rev_var_ls
20610bcd761SJeenu Viswambharanfunc cpu_rev_var_ls
20710bcd761SJeenu Viswambharan	mov	x2, #ERRATA_APPLIES
20810bcd761SJeenu Viswambharan	mov	x3, #ERRATA_NOT_APPLIES
20910bcd761SJeenu Viswambharan	cmp	x0, x1
21010bcd761SJeenu Viswambharan	csel	x0, x2, x3, ls
21110bcd761SJeenu Viswambharan	ret
21210bcd761SJeenu Viswambharanendfunc cpu_rev_var_ls
21310bcd761SJeenu Viswambharan
214b75dc0e4SAndre Przywara/*
215b75dc0e4SAndre Przywara * Compare the CPU's revision-variant (x0) with a given value (x1), for errata
216b75dc0e4SAndre Przywara * application purposes. If the revision-variant is higher than or same as a
217b75dc0e4SAndre Przywara * given value, indicates that errata applies; otherwise not.
2189ec3921cSJonathan Wright *
2199ec3921cSJonathan Wright * Shall clobber: x0-x3
220b75dc0e4SAndre Przywara */
221b75dc0e4SAndre Przywara	.globl	cpu_rev_var_hs
222b75dc0e4SAndre Przywarafunc cpu_rev_var_hs
223b75dc0e4SAndre Przywara	mov	x2, #ERRATA_APPLIES
224b75dc0e4SAndre Przywara	mov	x3, #ERRATA_NOT_APPLIES
225b75dc0e4SAndre Przywara	cmp	x0, x1
226b75dc0e4SAndre Przywara	csel	x0, x2, x3, hs
227b75dc0e4SAndre Przywara	ret
228b75dc0e4SAndre Przywaraendfunc cpu_rev_var_hs
229b75dc0e4SAndre Przywara
23010bcd761SJeenu Viswambharan#if REPORT_ERRATA
23110bcd761SJeenu Viswambharan/*
23210bcd761SJeenu Viswambharan * void print_errata_status(void);
23310bcd761SJeenu Viswambharan *
23410bcd761SJeenu Viswambharan * Function to print errata status for CPUs of its class. Must be called only:
23510bcd761SJeenu Viswambharan *
23610bcd761SJeenu Viswambharan *   - with MMU and data caches are enabled;
23710bcd761SJeenu Viswambharan *   - after cpu_ops have been initialized in per-CPU data.
23810bcd761SJeenu Viswambharan */
23910bcd761SJeenu Viswambharan	.globl print_errata_status
24010bcd761SJeenu Viswambharanfunc print_errata_status
24110bcd761SJeenu Viswambharan#ifdef IMAGE_BL1
24210bcd761SJeenu Viswambharan	/*
24310bcd761SJeenu Viswambharan	 * BL1 doesn't have per-CPU data. So retrieve the CPU operations
24410bcd761SJeenu Viswambharan	 * directly.
24510bcd761SJeenu Viswambharan	 */
24610bcd761SJeenu Viswambharan	stp	xzr, x30, [sp, #-16]!
24710bcd761SJeenu Viswambharan	bl	get_cpu_ops_ptr
24810bcd761SJeenu Viswambharan	ldp	xzr, x30, [sp], #16
24910bcd761SJeenu Viswambharan	ldr	x1, [x0, #CPU_ERRATA_FUNC]
25010bcd761SJeenu Viswambharan	cbnz	x1, .Lprint
25110bcd761SJeenu Viswambharan#else
25210bcd761SJeenu Viswambharan	/*
25310bcd761SJeenu Viswambharan	 * Retrieve pointer to cpu_ops from per-CPU data, and further, the
25410bcd761SJeenu Viswambharan	 * errata printing function. If it's non-NULL, jump to the function in
25510bcd761SJeenu Viswambharan	 * turn.
25610bcd761SJeenu Viswambharan	 */
25710bcd761SJeenu Viswambharan	mrs	x0, tpidr_el3
25810bcd761SJeenu Viswambharan	ldr	x1, [x0, #CPU_DATA_CPU_OPS_PTR]
25910bcd761SJeenu Viswambharan	ldr	x0, [x1, #CPU_ERRATA_FUNC]
26010bcd761SJeenu Viswambharan	cbz	x0, .Lnoprint
26110bcd761SJeenu Viswambharan
26210bcd761SJeenu Viswambharan	/*
26310bcd761SJeenu Viswambharan	 * Printing errata status requires atomically testing the printed flag.
26410bcd761SJeenu Viswambharan	 */
26522fa58cbSdp-arm	stp	x19, x30, [sp, #-16]!
26622fa58cbSdp-arm	mov	x19, x0
26710bcd761SJeenu Viswambharan
26810bcd761SJeenu Viswambharan	/*
26910bcd761SJeenu Viswambharan	 * Load pointers to errata lock and printed flag. Call
27010bcd761SJeenu Viswambharan	 * errata_needs_reporting to check whether this CPU needs to report
27110bcd761SJeenu Viswambharan	 * errata status pertaining to its class.
27210bcd761SJeenu Viswambharan	 */
27310bcd761SJeenu Viswambharan	ldr	x0, [x1, #CPU_ERRATA_LOCK]
27410bcd761SJeenu Viswambharan	ldr	x1, [x1, #CPU_ERRATA_PRINTED]
27510bcd761SJeenu Viswambharan	bl	errata_needs_reporting
27622fa58cbSdp-arm	mov	x1, x19
27722fa58cbSdp-arm	ldp	x19, x30, [sp], #16
27810bcd761SJeenu Viswambharan	cbnz	x0, .Lprint
27910bcd761SJeenu Viswambharan#endif
28010bcd761SJeenu Viswambharan.Lnoprint:
28110bcd761SJeenu Viswambharan	ret
28210bcd761SJeenu Viswambharan.Lprint:
28310bcd761SJeenu Viswambharan	/* Jump to errata reporting function for this CPU */
28410bcd761SJeenu Viswambharan	br	x1
28510bcd761SJeenu Viswambharanendfunc print_errata_status
28610bcd761SJeenu Viswambharan#endif
287a205a56eSDimitris Papastamos
288a205a56eSDimitris Papastamos/*
2892c3a1078SDimitris Papastamos * int check_wa_cve_2017_5715(void);
290a205a56eSDimitris Papastamos *
291a205a56eSDimitris Papastamos * This function returns:
292a205a56eSDimitris Papastamos *  - ERRATA_APPLIES when firmware mitigation is required.
293a205a56eSDimitris Papastamos *  - ERRATA_NOT_APPLIES when firmware mitigation is _not_ required.
294a205a56eSDimitris Papastamos *  - ERRATA_MISSING when firmware mitigation would be required but
295a205a56eSDimitris Papastamos *    is not compiled in.
296a205a56eSDimitris Papastamos *
297a205a56eSDimitris Papastamos * NOTE: Must be called only after cpu_ops have been initialized
298a205a56eSDimitris Papastamos *       in per-CPU data.
299a205a56eSDimitris Papastamos */
3002c3a1078SDimitris Papastamos	.globl	check_wa_cve_2017_5715
3012c3a1078SDimitris Papastamosfunc check_wa_cve_2017_5715
302a205a56eSDimitris Papastamos	mrs	x0, tpidr_el3
303a205a56eSDimitris Papastamos#if ENABLE_ASSERTIONS
304a205a56eSDimitris Papastamos	cmp	x0, #0
305a205a56eSDimitris Papastamos	ASM_ASSERT(ne)
306a205a56eSDimitris Papastamos#endif
307a205a56eSDimitris Papastamos	ldr	x0, [x0, #CPU_DATA_CPU_OPS_PTR]
308a205a56eSDimitris Papastamos	ldr	x0, [x0, #CPU_EXTRA1_FUNC]
309a205a56eSDimitris Papastamos	/*
310a205a56eSDimitris Papastamos	 * If the reserved function pointer is NULL, this CPU
311a205a56eSDimitris Papastamos	 * is unaffected by CVE-2017-5715 so bail out.
312a205a56eSDimitris Papastamos	 */
313a205a56eSDimitris Papastamos	cmp	x0, #0
314a205a56eSDimitris Papastamos	beq	1f
315a205a56eSDimitris Papastamos	br	x0
316a205a56eSDimitris Papastamos1:
317a205a56eSDimitris Papastamos	mov	x0, #ERRATA_NOT_APPLIES
318a205a56eSDimitris Papastamos	ret
3192c3a1078SDimitris Papastamosendfunc check_wa_cve_2017_5715
320fe007b2eSDimitris Papastamos
321fe007b2eSDimitris Papastamos/*
322fe007b2eSDimitris Papastamos * void *wa_cve_2018_3639_get_disable_ptr(void);
323fe007b2eSDimitris Papastamos *
324fe007b2eSDimitris Papastamos * Returns a function pointer which is used to disable mitigation
325fe007b2eSDimitris Papastamos * for CVE-2018-3639.
326fe007b2eSDimitris Papastamos * The function pointer is only returned on cores that employ
327fe007b2eSDimitris Papastamos * dynamic mitigation.  If the core uses static mitigation or is
328fe007b2eSDimitris Papastamos * unaffected by CVE-2018-3639 this function returns NULL.
329fe007b2eSDimitris Papastamos *
330fe007b2eSDimitris Papastamos * NOTE: Must be called only after cpu_ops have been initialized
331fe007b2eSDimitris Papastamos *       in per-CPU data.
332fe007b2eSDimitris Papastamos */
333fe007b2eSDimitris Papastamos	.globl	wa_cve_2018_3639_get_disable_ptr
334fe007b2eSDimitris Papastamosfunc wa_cve_2018_3639_get_disable_ptr
335fe007b2eSDimitris Papastamos	mrs	x0, tpidr_el3
336fe007b2eSDimitris Papastamos#if ENABLE_ASSERTIONS
337fe007b2eSDimitris Papastamos	cmp	x0, #0
338fe007b2eSDimitris Papastamos	ASM_ASSERT(ne)
339fe007b2eSDimitris Papastamos#endif
340fe007b2eSDimitris Papastamos	ldr	x0, [x0, #CPU_DATA_CPU_OPS_PTR]
341fe007b2eSDimitris Papastamos	ldr	x0, [x0, #CPU_EXTRA2_FUNC]
342fe007b2eSDimitris Papastamos	ret
343fe007b2eSDimitris Papastamosendfunc wa_cve_2018_3639_get_disable_ptr
344