xref: /rk3399_ARM-atf/include/lib/cpus/aarch64/cpu_macros.S (revision 6bb96fa6d6e101ffeef16464f8a44104a112074f)
1add40351SSoby Mathew/*
2007433d8SBoyan Karatotev * Copyright (c) 2014-2023, ARM Limited and Contributors. All rights reserved.
3add40351SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5add40351SSoby Mathew */
6c3cf06f1SAntonio Nino Diaz#ifndef CPU_MACROS_S
7c3cf06f1SAntonio Nino Diaz#define CPU_MACROS_S
8add40351SSoby Mathew
9ff6f62e1SAntonio Nino Diaz#include <assert_macros.S>
10007433d8SBoyan Karatotev#include <lib/cpus/cpu_ops.h>
11*6bb96fa6SBoyan Karatotev#include <lib/cpus/errata.h>
12add40351SSoby Mathew
13add40351SSoby Mathew	/*
145dd9dbb5SJeenu Viswambharan	 * Write given expressions as quad words
155dd9dbb5SJeenu Viswambharan	 *
165dd9dbb5SJeenu Viswambharan	 * _count:
175dd9dbb5SJeenu Viswambharan	 *	Write at least _count quad words. If the given number of
185dd9dbb5SJeenu Viswambharan	 *	expressions is less than _count, repeat the last expression to
195dd9dbb5SJeenu Viswambharan	 *	fill _count quad words in total
205dd9dbb5SJeenu Viswambharan	 * _rest:
215dd9dbb5SJeenu Viswambharan	 *	Optional list of expressions. _this is for parameter extraction
225dd9dbb5SJeenu Viswambharan	 *	only, and has no significance to the caller
235dd9dbb5SJeenu Viswambharan	 *
245dd9dbb5SJeenu Viswambharan	 * Invoked as:
255dd9dbb5SJeenu Viswambharan	 *	fill_constants 2, foo, bar, blah, ...
26add40351SSoby Mathew	 */
275dd9dbb5SJeenu Viswambharan	.macro fill_constants _count:req, _this, _rest:vararg
285dd9dbb5SJeenu Viswambharan	  .ifgt \_count
295dd9dbb5SJeenu Viswambharan	    /* Write the current expression */
305dd9dbb5SJeenu Viswambharan	    .ifb \_this
315dd9dbb5SJeenu Viswambharan	      .error "Nothing to fill"
325dd9dbb5SJeenu Viswambharan	    .endif
335dd9dbb5SJeenu Viswambharan	    .quad \_this
345dd9dbb5SJeenu Viswambharan
355dd9dbb5SJeenu Viswambharan	    /* Invoke recursively for remaining expressions */
365dd9dbb5SJeenu Viswambharan	    .ifnb \_rest
375dd9dbb5SJeenu Viswambharan	      fill_constants \_count-1, \_rest
385dd9dbb5SJeenu Viswambharan	    .else
395dd9dbb5SJeenu Viswambharan	      fill_constants \_count-1, \_this
405dd9dbb5SJeenu Viswambharan	    .endif
415dd9dbb5SJeenu Viswambharan	  .endif
425dd9dbb5SJeenu Viswambharan	.endm
435dd9dbb5SJeenu Viswambharan
445dd9dbb5SJeenu Viswambharan	/*
455dd9dbb5SJeenu Viswambharan	 * Declare CPU operations
465dd9dbb5SJeenu Viswambharan	 *
475dd9dbb5SJeenu Viswambharan	 * _name:
485dd9dbb5SJeenu Viswambharan	 *	Name of the CPU for which operations are being specified
495dd9dbb5SJeenu Viswambharan	 * _midr:
505dd9dbb5SJeenu Viswambharan	 *	Numeric value expected to read from CPU's MIDR
515dd9dbb5SJeenu Viswambharan	 * _resetfunc:
525dd9dbb5SJeenu Viswambharan	 *	Reset function for the CPU. If there's no CPU reset function,
535dd9dbb5SJeenu Viswambharan	 *	specify CPU_NO_RESET_FUNC
54a205a56eSDimitris Papastamos	 * _extra1:
55a205a56eSDimitris Papastamos	 *	This is a placeholder for future per CPU operations.  Currently,
56a205a56eSDimitris Papastamos	 *	some CPUs use this entry to set a test function to determine if
57a205a56eSDimitris Papastamos	 *	the workaround for CVE-2017-5715 needs to be applied or not.
58fe007b2eSDimitris Papastamos	 * _extra2:
59fe007b2eSDimitris Papastamos	 *	This is a placeholder for future per CPU operations. Currently
60fe007b2eSDimitris Papastamos	 *	some CPUs use this entry to set a function to disable the
61fe007b2eSDimitris Papastamos	 *	workaround for CVE-2018-3639.
629b2510b6SBipin Ravi	 * _extra3:
639b2510b6SBipin Ravi	 *	This is a placeholder for future per CPU operations. Currently,
649b2510b6SBipin Ravi	 *	some CPUs use this entry to set a test function to determine if
659b2510b6SBipin Ravi	 *	the workaround for CVE-2022-23960 needs to be applied or not.
6680942622Slaurenw-arm	 * _e_handler:
6780942622Slaurenw-arm	 *	This is a placeholder for future per CPU exception handlers.
685dd9dbb5SJeenu Viswambharan	 * _power_down_ops:
695dd9dbb5SJeenu Viswambharan	 *	Comma-separated list of functions to perform power-down
705dd9dbb5SJeenu Viswambharan	 *	operatios on the CPU. At least one, and up to
715dd9dbb5SJeenu Viswambharan	 *	CPU_MAX_PWR_DWN_OPS number of functions may be specified.
725dd9dbb5SJeenu Viswambharan	 *	Starting at power level 0, these functions shall handle power
735dd9dbb5SJeenu Viswambharan	 *	down at subsequent power levels. If there aren't exactly
745dd9dbb5SJeenu Viswambharan	 *	CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
755dd9dbb5SJeenu Viswambharan	 *	used to handle power down at subsequent levels
765dd9dbb5SJeenu Viswambharan	 */
77a205a56eSDimitris Papastamos	.macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
789b2510b6SBipin Ravi		_extra1:req, _extra2:req, _extra3:req, _e_handler:req, _power_down_ops:vararg
79da04341eSChris Kay	.section .cpu_ops, "a"
805dd9dbb5SJeenu Viswambharan	.align 3
81add40351SSoby Mathew	.type cpu_ops_\_name, %object
82add40351SSoby Mathew	.quad \_midr
83b1d27b48SRoberto Vargas#if defined(IMAGE_AT_EL3)
845dd9dbb5SJeenu Viswambharan	.quad \_resetfunc
85add40351SSoby Mathew#endif
86a205a56eSDimitris Papastamos	.quad \_extra1
87fe007b2eSDimitris Papastamos	.quad \_extra2
889b2510b6SBipin Ravi	.quad \_extra3
8980942622Slaurenw-arm	.quad \_e_handler
903d8256b2SMasahiro Yamada#ifdef IMAGE_BL31
915dd9dbb5SJeenu Viswambharan	/* Insert list of functions */
925dd9dbb5SJeenu Viswambharan	fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
93add40351SSoby Mathew#endif
9410bcd761SJeenu Viswambharan
9510bcd761SJeenu Viswambharan#if REPORT_ERRATA
9610bcd761SJeenu Viswambharan	.ifndef \_name\()_cpu_str
9710bcd761SJeenu Viswambharan	  /*
9810bcd761SJeenu Viswambharan	   * Place errata reported flag, and the spinlock to arbitrate access to
9910bcd761SJeenu Viswambharan	   * it in the data section.
10010bcd761SJeenu Viswambharan	   */
10110bcd761SJeenu Viswambharan	  .pushsection .data
10210bcd761SJeenu Viswambharan	  define_asm_spinlock \_name\()_errata_lock
10310bcd761SJeenu Viswambharan	  \_name\()_errata_reported:
10410bcd761SJeenu Viswambharan	  .word	0
10510bcd761SJeenu Viswambharan	  .popsection
10610bcd761SJeenu Viswambharan
10710bcd761SJeenu Viswambharan	  /* Place CPU string in rodata */
10810bcd761SJeenu Viswambharan	  .pushsection .rodata
10910bcd761SJeenu Viswambharan	  \_name\()_cpu_str:
11010bcd761SJeenu Viswambharan	  .asciz "\_name"
11110bcd761SJeenu Viswambharan	  .popsection
11210bcd761SJeenu Viswambharan	.endif
11310bcd761SJeenu Viswambharan
11410bcd761SJeenu Viswambharan	/*
11512af5ed4SSoby Mathew	 * Mandatory errata status printing function for CPUs of
11610bcd761SJeenu Viswambharan	 * this class.
11710bcd761SJeenu Viswambharan	 */
11810bcd761SJeenu Viswambharan	.quad \_name\()_errata_report
11910bcd761SJeenu Viswambharan
12010bcd761SJeenu Viswambharan#ifdef IMAGE_BL31
12110bcd761SJeenu Viswambharan	/* Pointers to errata lock and reported flag */
12210bcd761SJeenu Viswambharan	.quad \_name\()_errata_lock
12310bcd761SJeenu Viswambharan	.quad \_name\()_errata_reported
12410bcd761SJeenu Viswambharan#endif
12510bcd761SJeenu Viswambharan#endif
12610bcd761SJeenu Viswambharan
1273d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
128d3f70af6SSoby Mathew	.quad \_name\()_cpu_reg_dump
129d3f70af6SSoby Mathew#endif
130add40351SSoby Mathew	.endm
131e2bf57f8SDan Handley
132a205a56eSDimitris Papastamos	.macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
133a205a56eSDimitris Papastamos		_power_down_ops:vararg
1349b2510b6SBipin Ravi		declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, 0, 0, \
135a205a56eSDimitris Papastamos			\_power_down_ops
136a205a56eSDimitris Papastamos	.endm
137a205a56eSDimitris Papastamos
13880942622Slaurenw-arm	.macro declare_cpu_ops_eh _name:req, _midr:req, _resetfunc:req, \
13980942622Slaurenw-arm		_e_handler:req, _power_down_ops:vararg
14080942622Slaurenw-arm		declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
1419b2510b6SBipin Ravi			0, 0, 0, \_e_handler, \_power_down_ops
14280942622Slaurenw-arm	.endm
14380942622Slaurenw-arm
144fe007b2eSDimitris Papastamos	.macro declare_cpu_ops_wa _name:req, _midr:req, \
145fe007b2eSDimitris Papastamos		_resetfunc:req, _extra1:req, _extra2:req, \
1469b2510b6SBipin Ravi		_extra3:req, _power_down_ops:vararg
147a205a56eSDimitris Papastamos		declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
1489b2510b6SBipin Ravi			\_extra1, \_extra2, \_extra3, 0, \_power_down_ops
149a205a56eSDimitris Papastamos	.endm
150a205a56eSDimitris Papastamos
15110bcd761SJeenu Viswambharan#if REPORT_ERRATA
15210bcd761SJeenu Viswambharan	/*
15310bcd761SJeenu Viswambharan	 * Print status of a CPU errata
15410bcd761SJeenu Viswambharan	 *
15510bcd761SJeenu Viswambharan	 * _chosen:
15610bcd761SJeenu Viswambharan	 *	Identifier indicating whether or not a CPU errata has been
15710bcd761SJeenu Viswambharan	 *	compiled in.
15810bcd761SJeenu Viswambharan	 * _cpu:
15910bcd761SJeenu Viswambharan	 *	Name of the CPU
16010bcd761SJeenu Viswambharan	 * _id:
16110bcd761SJeenu Viswambharan	 *	Errata identifier
16210bcd761SJeenu Viswambharan	 * _rev_var:
16310bcd761SJeenu Viswambharan	 *	Register containing the combined value CPU revision and variant
16410bcd761SJeenu Viswambharan	 *	- typically the return value of cpu_get_rev_var
16510bcd761SJeenu Viswambharan	 */
16610bcd761SJeenu Viswambharan	.macro report_errata _chosen, _cpu, _id, _rev_var=x8
16710bcd761SJeenu Viswambharan	/* Stash a string with errata ID */
16810bcd761SJeenu Viswambharan	.pushsection .rodata
16910bcd761SJeenu Viswambharan	\_cpu\()_errata_\_id\()_str:
17010bcd761SJeenu Viswambharan	.asciz	"\_id"
17110bcd761SJeenu Viswambharan	.popsection
17210bcd761SJeenu Viswambharan
17310bcd761SJeenu Viswambharan	/* Check whether errata applies */
17410bcd761SJeenu Viswambharan	mov	x0, \_rev_var
1759ec3921cSJonathan Wright	/* Shall clobber: x0-x7 */
17610bcd761SJeenu Viswambharan	bl	check_errata_\_id
17710bcd761SJeenu Viswambharan
17810bcd761SJeenu Viswambharan	.ifeq \_chosen
17910bcd761SJeenu Viswambharan	/*
18010bcd761SJeenu Viswambharan	 * Errata workaround has not been compiled in. If the errata would have
18110bcd761SJeenu Viswambharan	 * applied had it been compiled in, print its status as missing.
18210bcd761SJeenu Viswambharan	 */
18310bcd761SJeenu Viswambharan	cbz	x0, 900f
18410bcd761SJeenu Viswambharan	mov	x0, #ERRATA_MISSING
18510bcd761SJeenu Viswambharan	.endif
18610bcd761SJeenu Viswambharan900:
18710bcd761SJeenu Viswambharan	adr	x1, \_cpu\()_cpu_str
18810bcd761SJeenu Viswambharan	adr	x2, \_cpu\()_errata_\_id\()_str
18910bcd761SJeenu Viswambharan	bl	errata_print_msg
19010bcd761SJeenu Viswambharan	.endm
19110bcd761SJeenu Viswambharan#endif
19210bcd761SJeenu Viswambharan
1933991a6a4SDimitris Papastamos	/*
1943991a6a4SDimitris Papastamos	 * This macro is used on some CPUs to detect if they are vulnerable
1953991a6a4SDimitris Papastamos	 * to CVE-2017-5715.
1963991a6a4SDimitris Papastamos	 */
1973991a6a4SDimitris Papastamos	.macro	cpu_check_csv2 _reg _label
1983991a6a4SDimitris Papastamos	mrs	\_reg, id_aa64pfr0_el1
1993991a6a4SDimitris Papastamos	ubfx	\_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
2003991a6a4SDimitris Papastamos	/*
201ff6f62e1SAntonio Nino Diaz	 * If the field equals 1, branch targets trained in one context cannot
202ff6f62e1SAntonio Nino Diaz	 * affect speculative execution in a different context.
203ff6f62e1SAntonio Nino Diaz	 *
204ff6f62e1SAntonio Nino Diaz	 * If the field equals 2, it means that the system is also aware of
205ff6f62e1SAntonio Nino Diaz	 * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
206ff6f62e1SAntonio Nino Diaz	 * expect users of the registers to do the right thing.
207ff6f62e1SAntonio Nino Diaz	 *
208ff6f62e1SAntonio Nino Diaz	 * Only apply mitigations if the value of this field is 0.
2093991a6a4SDimitris Papastamos	 */
210ff6f62e1SAntonio Nino Diaz#if ENABLE_ASSERTIONS
211ff6f62e1SAntonio Nino Diaz	cmp	\_reg, #3 /* Only values 0 to 2 are expected */
212ff6f62e1SAntonio Nino Diaz	ASM_ASSERT(lo)
213ff6f62e1SAntonio Nino Diaz#endif
214ff6f62e1SAntonio Nino Diaz
215ff6f62e1SAntonio Nino Diaz	cmp	\_reg, #0
216ff6f62e1SAntonio Nino Diaz	bne	\_label
2173991a6a4SDimitris Papastamos	.endm
218da3b038fSDeepak Pandey
219da3b038fSDeepak Pandey	/*
220da3b038fSDeepak Pandey	 * Helper macro that reads the part number of the current
221da3b038fSDeepak Pandey	 * CPU and jumps to the given label if it matches the CPU
222da3b038fSDeepak Pandey	 * MIDR provided.
223da3b038fSDeepak Pandey	 *
224da3b038fSDeepak Pandey	 * Clobbers x0.
225da3b038fSDeepak Pandey	 */
226da3b038fSDeepak Pandey	.macro  jump_if_cpu_midr _cpu_midr, _label
227da3b038fSDeepak Pandey	mrs	x0, midr_el1
228da3b038fSDeepak Pandey	ubfx	x0, x0, MIDR_PN_SHIFT, #12
229da3b038fSDeepak Pandey	cmp	w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
230da3b038fSDeepak Pandey	b.eq	\_label
231da3b038fSDeepak Pandey	.endm
232c3cf06f1SAntonio Nino Diaz
233c3cf06f1SAntonio Nino Diaz#endif /* CPU_MACROS_S */
234