xref: /rk3399_ARM-atf/include/lib/cpus/aarch64/cpu_macros.S (revision 94a75ad456a8bda75ca1e4343f00be249a201a69)
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>
116bb96fa6SBoyan 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
943f4c1e1eSBoyan Karatotev	/*
953f4c1e1eSBoyan Karatotev	 * It is possible (although unlikely) that a cpu may have no errata in
963f4c1e1eSBoyan Karatotev	 * code. In that case the start label will not be defined. The list is
973f4c1e1eSBoyan Karatotev	 * intended to be used in a loop, so define it as zero-length for
983f4c1e1eSBoyan Karatotev	 * predictable behaviour. Since this macro is always called at the end
993f4c1e1eSBoyan Karatotev	 * of the cpu file (after all errata have been parsed) we can be sure
1003f4c1e1eSBoyan Karatotev	 * that we are at the end of the list. Some cpus call declare_cpu_ops
1013f4c1e1eSBoyan Karatotev	 * twice, so only do this once.
1023f4c1e1eSBoyan Karatotev	 */
1033f4c1e1eSBoyan Karatotev	.pushsection .rodata.errata_entries
1043f4c1e1eSBoyan Karatotev	.ifndef \_name\()_errata_list_start
1053f4c1e1eSBoyan Karatotev		\_name\()_errata_list_start:
1063f4c1e1eSBoyan Karatotev	.endif
1073f4c1e1eSBoyan Karatotev	.ifndef \_name\()_errata_list_end
1083f4c1e1eSBoyan Karatotev		\_name\()_errata_list_end:
1093f4c1e1eSBoyan Karatotev	.endif
1103f4c1e1eSBoyan Karatotev	.popsection
1113f4c1e1eSBoyan Karatotev
1123f4c1e1eSBoyan Karatotev	/* and now put them in cpu_ops */
1133f4c1e1eSBoyan Karatotev	.quad \_name\()_errata_list_start
1143f4c1e1eSBoyan Karatotev	.quad \_name\()_errata_list_end
11510bcd761SJeenu Viswambharan
11610bcd761SJeenu Viswambharan#if REPORT_ERRATA
11710bcd761SJeenu Viswambharan	.ifndef \_name\()_cpu_str
11810bcd761SJeenu Viswambharan	  /*
11910bcd761SJeenu Viswambharan	   * Place errata reported flag, and the spinlock to arbitrate access to
12010bcd761SJeenu Viswambharan	   * it in the data section.
12110bcd761SJeenu Viswambharan	   */
12210bcd761SJeenu Viswambharan	  .pushsection .data
12310bcd761SJeenu Viswambharan	  define_asm_spinlock \_name\()_errata_lock
12410bcd761SJeenu Viswambharan	  \_name\()_errata_reported:
12510bcd761SJeenu Viswambharan	  .word	0
12610bcd761SJeenu Viswambharan	  .popsection
12710bcd761SJeenu Viswambharan
12810bcd761SJeenu Viswambharan	  /* Place CPU string in rodata */
12910bcd761SJeenu Viswambharan	  .pushsection .rodata
13010bcd761SJeenu Viswambharan	  \_name\()_cpu_str:
13110bcd761SJeenu Viswambharan	  .asciz "\_name"
13210bcd761SJeenu Viswambharan	  .popsection
13310bcd761SJeenu Viswambharan	.endif
13410bcd761SJeenu Viswambharan
1353f4c1e1eSBoyan Karatotev
13610bcd761SJeenu Viswambharan	/*
13712af5ed4SSoby Mathew	 * Mandatory errata status printing function for CPUs of
13810bcd761SJeenu Viswambharan	 * this class.
13910bcd761SJeenu Viswambharan	 */
14010bcd761SJeenu Viswambharan	.quad \_name\()_errata_report
1413f4c1e1eSBoyan Karatotev	.quad \_name\()_cpu_str
14210bcd761SJeenu Viswambharan
14310bcd761SJeenu Viswambharan#ifdef IMAGE_BL31
14410bcd761SJeenu Viswambharan	/* Pointers to errata lock and reported flag */
14510bcd761SJeenu Viswambharan	.quad \_name\()_errata_lock
14610bcd761SJeenu Viswambharan	.quad \_name\()_errata_reported
1473f4c1e1eSBoyan Karatotev#endif /* IMAGE_BL31 */
1483f4c1e1eSBoyan Karatotev#endif /* REPORT_ERRATA */
14910bcd761SJeenu Viswambharan
1503d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
151d3f70af6SSoby Mathew	.quad \_name\()_cpu_reg_dump
152d3f70af6SSoby Mathew#endif
153add40351SSoby Mathew	.endm
154e2bf57f8SDan Handley
155a205a56eSDimitris Papastamos	.macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
156a205a56eSDimitris Papastamos		_power_down_ops:vararg
1579b2510b6SBipin Ravi		declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, 0, 0, \
158a205a56eSDimitris Papastamos			\_power_down_ops
159a205a56eSDimitris Papastamos	.endm
160a205a56eSDimitris Papastamos
16180942622Slaurenw-arm	.macro declare_cpu_ops_eh _name:req, _midr:req, _resetfunc:req, \
16280942622Slaurenw-arm		_e_handler:req, _power_down_ops:vararg
16380942622Slaurenw-arm		declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
1649b2510b6SBipin Ravi			0, 0, 0, \_e_handler, \_power_down_ops
16580942622Slaurenw-arm	.endm
16680942622Slaurenw-arm
167fe007b2eSDimitris Papastamos	.macro declare_cpu_ops_wa _name:req, _midr:req, \
168fe007b2eSDimitris Papastamos		_resetfunc:req, _extra1:req, _extra2:req, \
1699b2510b6SBipin Ravi		_extra3:req, _power_down_ops:vararg
170a205a56eSDimitris Papastamos		declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
1719b2510b6SBipin Ravi			\_extra1, \_extra2, \_extra3, 0, \_power_down_ops
172a205a56eSDimitris Papastamos	.endm
173a205a56eSDimitris Papastamos
1743f4c1e1eSBoyan Karatotev/* TODO can be deleted once all CPUs have been converted */
17510bcd761SJeenu Viswambharan#if REPORT_ERRATA
17610bcd761SJeenu Viswambharan	/*
17710bcd761SJeenu Viswambharan	 * Print status of a CPU errata
17810bcd761SJeenu Viswambharan	 *
17910bcd761SJeenu Viswambharan	 * _chosen:
18010bcd761SJeenu Viswambharan	 *	Identifier indicating whether or not a CPU errata has been
18110bcd761SJeenu Viswambharan	 *	compiled in.
18210bcd761SJeenu Viswambharan	 * _cpu:
18310bcd761SJeenu Viswambharan	 *	Name of the CPU
18410bcd761SJeenu Viswambharan	 * _id:
18510bcd761SJeenu Viswambharan	 *	Errata identifier
18610bcd761SJeenu Viswambharan	 * _rev_var:
18710bcd761SJeenu Viswambharan	 *	Register containing the combined value CPU revision and variant
18810bcd761SJeenu Viswambharan	 *	- typically the return value of cpu_get_rev_var
18910bcd761SJeenu Viswambharan	 */
19010bcd761SJeenu Viswambharan	.macro report_errata _chosen, _cpu, _id, _rev_var=x8
19110bcd761SJeenu Viswambharan	/* Stash a string with errata ID */
19210bcd761SJeenu Viswambharan	.pushsection .rodata
19310bcd761SJeenu Viswambharan	\_cpu\()_errata_\_id\()_str:
19410bcd761SJeenu Viswambharan	.asciz	"\_id"
19510bcd761SJeenu Viswambharan	.popsection
19610bcd761SJeenu Viswambharan
19710bcd761SJeenu Viswambharan	/* Check whether errata applies */
19810bcd761SJeenu Viswambharan	mov	x0, \_rev_var
1999ec3921cSJonathan Wright	/* Shall clobber: x0-x7 */
20010bcd761SJeenu Viswambharan	bl	check_errata_\_id
20110bcd761SJeenu Viswambharan
20210bcd761SJeenu Viswambharan	.ifeq \_chosen
20310bcd761SJeenu Viswambharan	/*
20410bcd761SJeenu Viswambharan	 * Errata workaround has not been compiled in. If the errata would have
20510bcd761SJeenu Viswambharan	 * applied had it been compiled in, print its status as missing.
20610bcd761SJeenu Viswambharan	 */
20710bcd761SJeenu Viswambharan	cbz	x0, 900f
20810bcd761SJeenu Viswambharan	mov	x0, #ERRATA_MISSING
20910bcd761SJeenu Viswambharan	.endif
21010bcd761SJeenu Viswambharan900:
21110bcd761SJeenu Viswambharan	adr	x1, \_cpu\()_cpu_str
21210bcd761SJeenu Viswambharan	adr	x2, \_cpu\()_errata_\_id\()_str
21310bcd761SJeenu Viswambharan	bl	errata_print_msg
21410bcd761SJeenu Viswambharan	.endm
21510bcd761SJeenu Viswambharan#endif
21610bcd761SJeenu Viswambharan
2173991a6a4SDimitris Papastamos	/*
2183991a6a4SDimitris Papastamos	 * This macro is used on some CPUs to detect if they are vulnerable
2193991a6a4SDimitris Papastamos	 * to CVE-2017-5715.
2203991a6a4SDimitris Papastamos	 */
2213991a6a4SDimitris Papastamos	.macro	cpu_check_csv2 _reg _label
2223991a6a4SDimitris Papastamos	mrs	\_reg, id_aa64pfr0_el1
2233991a6a4SDimitris Papastamos	ubfx	\_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
2243991a6a4SDimitris Papastamos	/*
225ff6f62e1SAntonio Nino Diaz	 * If the field equals 1, branch targets trained in one context cannot
226ff6f62e1SAntonio Nino Diaz	 * affect speculative execution in a different context.
227ff6f62e1SAntonio Nino Diaz	 *
228ff6f62e1SAntonio Nino Diaz	 * If the field equals 2, it means that the system is also aware of
229ff6f62e1SAntonio Nino Diaz	 * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
230ff6f62e1SAntonio Nino Diaz	 * expect users of the registers to do the right thing.
231ff6f62e1SAntonio Nino Diaz	 *
232ff6f62e1SAntonio Nino Diaz	 * Only apply mitigations if the value of this field is 0.
2333991a6a4SDimitris Papastamos	 */
234ff6f62e1SAntonio Nino Diaz#if ENABLE_ASSERTIONS
235ff6f62e1SAntonio Nino Diaz	cmp	\_reg, #3 /* Only values 0 to 2 are expected */
236ff6f62e1SAntonio Nino Diaz	ASM_ASSERT(lo)
237ff6f62e1SAntonio Nino Diaz#endif
238ff6f62e1SAntonio Nino Diaz
239ff6f62e1SAntonio Nino Diaz	cmp	\_reg, #0
240ff6f62e1SAntonio Nino Diaz	bne	\_label
2413991a6a4SDimitris Papastamos	.endm
242da3b038fSDeepak Pandey
243da3b038fSDeepak Pandey	/*
244da3b038fSDeepak Pandey	 * Helper macro that reads the part number of the current
245da3b038fSDeepak Pandey	 * CPU and jumps to the given label if it matches the CPU
246da3b038fSDeepak Pandey	 * MIDR provided.
247da3b038fSDeepak Pandey	 *
248da3b038fSDeepak Pandey	 * Clobbers x0.
249da3b038fSDeepak Pandey	 */
250da3b038fSDeepak Pandey	.macro  jump_if_cpu_midr _cpu_midr, _label
251da3b038fSDeepak Pandey	mrs	x0, midr_el1
252da3b038fSDeepak Pandey	ubfx	x0, x0, MIDR_PN_SHIFT, #12
253da3b038fSDeepak Pandey	cmp	w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
254da3b038fSDeepak Pandey	b.eq	\_label
255da3b038fSDeepak Pandey	.endm
256c3cf06f1SAntonio Nino Diaz
2573f4c1e1eSBoyan Karatotev
2583f4c1e1eSBoyan Karatotev/*
2593f4c1e1eSBoyan Karatotev * Workaround wrappers for errata that apply at reset or runtime. Reset errata
2603f4c1e1eSBoyan Karatotev * will be applied automatically
2613f4c1e1eSBoyan Karatotev *
2623f4c1e1eSBoyan Karatotev * _cpu:
2633f4c1e1eSBoyan Karatotev *	Name of cpu as given to declare_cpu_ops
2643f4c1e1eSBoyan Karatotev *
2653f4c1e1eSBoyan Karatotev * _cve:
2663f4c1e1eSBoyan Karatotev *	Whether erratum is a CVE. CVE year if yes, 0 otherwise
2673f4c1e1eSBoyan Karatotev *
2683f4c1e1eSBoyan Karatotev * _id:
2693f4c1e1eSBoyan Karatotev *	Erratum or CVE number. Please combine with previous field with ERRATUM
2703f4c1e1eSBoyan Karatotev *	or CVE macros
2713f4c1e1eSBoyan Karatotev *
2723f4c1e1eSBoyan Karatotev * _chosen:
2733f4c1e1eSBoyan Karatotev *	Compile time flag on whether the erratum is included
2743f4c1e1eSBoyan Karatotev *
2753f4c1e1eSBoyan Karatotev * _apply_at_reset:
2763f4c1e1eSBoyan Karatotev *	Whether the erratum should be automatically applied at reset
2773f4c1e1eSBoyan Karatotev */
2783f4c1e1eSBoyan Karatotev.macro add_erratum_entry _cpu:req, _cve:req, _id:req, _chosen:req, _apply_at_reset:req
2793f4c1e1eSBoyan Karatotev	.pushsection .rodata.errata_entries
2803f4c1e1eSBoyan Karatotev		.align	3
2813f4c1e1eSBoyan Karatotev		.ifndef \_cpu\()_errata_list_start
2823f4c1e1eSBoyan Karatotev		\_cpu\()_errata_list_start:
2833f4c1e1eSBoyan Karatotev		.endif
2843f4c1e1eSBoyan Karatotev
2853f4c1e1eSBoyan Karatotev		/* check if unused and compile out if no references */
2863f4c1e1eSBoyan Karatotev		.if \_apply_at_reset && \_chosen
2873f4c1e1eSBoyan Karatotev			.quad	erratum_\_cpu\()_\_id\()_wa
2883f4c1e1eSBoyan Karatotev		.else
2893f4c1e1eSBoyan Karatotev			.quad	0
2903f4c1e1eSBoyan Karatotev		.endif
2913f4c1e1eSBoyan Karatotev		/* TODO(errata ABI): this prevents all checker functions from
2923f4c1e1eSBoyan Karatotev		 * being optimised away. Can be done away with unless the ABI
2933f4c1e1eSBoyan Karatotev		 * needs them */
2943f4c1e1eSBoyan Karatotev		.quad	check_erratum_\_cpu\()_\_id
2953f4c1e1eSBoyan Karatotev		/* Will fit CVEs with up to 10 character in the ID field */
2963f4c1e1eSBoyan Karatotev		.word	\_id
2973f4c1e1eSBoyan Karatotev		.hword	\_cve
2983f4c1e1eSBoyan Karatotev		.byte	\_chosen
2993f4c1e1eSBoyan Karatotev		/* TODO(errata ABI): mitigated field for known but unmitigated
3003f4c1e1eSBoyan Karatotev		 * errata */
3013f4c1e1eSBoyan Karatotev		.byte	0x1
3023f4c1e1eSBoyan Karatotev	.popsection
3033f4c1e1eSBoyan Karatotev.endm
3043f4c1e1eSBoyan Karatotev
3053f4c1e1eSBoyan Karatotev.macro _workaround_start _cpu:req, _cve:req, _id:req, _chosen:req, _apply_at_reset:req
3063f4c1e1eSBoyan Karatotev	add_erratum_entry \_cpu, \_cve, \_id, \_chosen, \_apply_at_reset
3073f4c1e1eSBoyan Karatotev
3083f4c1e1eSBoyan Karatotev	func erratum_\_cpu\()_\_id\()_wa
3093f4c1e1eSBoyan Karatotev		mov	x8, x30
3103f4c1e1eSBoyan Karatotev
3113f4c1e1eSBoyan Karatotev		/* save rev_var for workarounds that might need it but don't
3123f4c1e1eSBoyan Karatotev		 * restore to x0 because few will care */
3133f4c1e1eSBoyan Karatotev		mov	x7, x0
3143f4c1e1eSBoyan Karatotev		bl	check_erratum_\_cpu\()_\_id
3153f4c1e1eSBoyan Karatotev		cbz	x0, erratum_\_cpu\()_\_id\()_skip
3163f4c1e1eSBoyan Karatotev.endm
3173f4c1e1eSBoyan Karatotev
3183f4c1e1eSBoyan Karatotev.macro _workaround_end _cpu:req, _id:req
3193f4c1e1eSBoyan Karatotev	erratum_\_cpu\()_\_id\()_skip:
3203f4c1e1eSBoyan Karatotev		ret	x8
3213f4c1e1eSBoyan Karatotev	endfunc erratum_\_cpu\()_\_id\()_wa
3223f4c1e1eSBoyan Karatotev.endm
3233f4c1e1eSBoyan Karatotev
3243f4c1e1eSBoyan Karatotev/*******************************************************************************
3253f4c1e1eSBoyan Karatotev * Errata workaround wrappers
3263f4c1e1eSBoyan Karatotev ******************************************************************************/
3273f4c1e1eSBoyan Karatotev/*
3283f4c1e1eSBoyan Karatotev * Workaround wrappers for errata that apply at reset or runtime. Reset errata
3293f4c1e1eSBoyan Karatotev * will be applied automatically
3303f4c1e1eSBoyan Karatotev *
3313f4c1e1eSBoyan Karatotev * _cpu:
3323f4c1e1eSBoyan Karatotev *	Name of cpu as given to declare_cpu_ops
3333f4c1e1eSBoyan Karatotev *
3343f4c1e1eSBoyan Karatotev * _cve:
3353f4c1e1eSBoyan Karatotev *	Whether erratum is a CVE. CVE year if yes, 0 otherwise
3363f4c1e1eSBoyan Karatotev *
3373f4c1e1eSBoyan Karatotev * _id:
3383f4c1e1eSBoyan Karatotev *	Erratum or CVE number. Please combine with previous field with ERRATUM
3393f4c1e1eSBoyan Karatotev *	or CVE macros
3403f4c1e1eSBoyan Karatotev *
3413f4c1e1eSBoyan Karatotev * _chosen:
3423f4c1e1eSBoyan Karatotev *	Compile time flag on whether the erratum is included
3433f4c1e1eSBoyan Karatotev *
3443f4c1e1eSBoyan Karatotev * in body:
3453f4c1e1eSBoyan Karatotev *	clobber x0 to x7 (please only use those)
3463f4c1e1eSBoyan Karatotev *	argument x7 - cpu_rev_var
3473f4c1e1eSBoyan Karatotev *
3483f4c1e1eSBoyan Karatotev * _wa clobbers: x0-x8 (PCS compliant)
3493f4c1e1eSBoyan Karatotev */
3503f4c1e1eSBoyan Karatotev.macro workaround_reset_start _cpu:req, _cve:req, _id:req, _chosen:req
3513f4c1e1eSBoyan Karatotev	_workaround_start \_cpu, \_cve, \_id, \_chosen, 1
3523f4c1e1eSBoyan Karatotev.endm
3533f4c1e1eSBoyan Karatotev
3543f4c1e1eSBoyan Karatotev/*
3553f4c1e1eSBoyan Karatotev * See `workaround_reset_start` for usage info. Additional arguments:
3563f4c1e1eSBoyan Karatotev *
3573f4c1e1eSBoyan Karatotev * _midr:
3583f4c1e1eSBoyan Karatotev *	Check if CPU's MIDR matches the CPU it's meant for. Must be specified
3593f4c1e1eSBoyan Karatotev *	for errata applied in generic code
3603f4c1e1eSBoyan Karatotev */
3613f4c1e1eSBoyan Karatotev.macro workaround_runtime_start _cpu:req, _cve:req, _id:req, _chosen:req, _midr
3623f4c1e1eSBoyan Karatotev	/*
3633f4c1e1eSBoyan Karatotev	 * Let errata specify if they need MIDR checking. Sadly, storing the
3643f4c1e1eSBoyan Karatotev	 * MIDR in an .equ to retrieve automatically blows up as it stores some
3653f4c1e1eSBoyan Karatotev	 * brackets in the symbol
3663f4c1e1eSBoyan Karatotev	 */
3673f4c1e1eSBoyan Karatotev	.ifnb \_midr
3683f4c1e1eSBoyan Karatotev		jump_if_cpu_midr \_midr, 1f
3693f4c1e1eSBoyan Karatotev		b	erratum_\_cpu\()_\_id\()_skip
3703f4c1e1eSBoyan Karatotev
3713f4c1e1eSBoyan Karatotev		1:
3723f4c1e1eSBoyan Karatotev	.endif
3733f4c1e1eSBoyan Karatotev	_workaround_start \_cpu, \_cve, \_id, \_chosen, 0
3743f4c1e1eSBoyan Karatotev.endm
3753f4c1e1eSBoyan Karatotev
3763f4c1e1eSBoyan Karatotev/*
3773f4c1e1eSBoyan Karatotev * Usage and arguments identical to `workaround_reset_start`. The _cve argument
3783f4c1e1eSBoyan Karatotev * is kept here so the same #define can be used as that macro
3793f4c1e1eSBoyan Karatotev */
3803f4c1e1eSBoyan Karatotev.macro workaround_reset_end _cpu:req, _cve:req, _id:req
3813f4c1e1eSBoyan Karatotev	_workaround_end \_cpu, \_id
3823f4c1e1eSBoyan Karatotev.endm
3833f4c1e1eSBoyan Karatotev
3843f4c1e1eSBoyan Karatotev/*
3853f4c1e1eSBoyan Karatotev * See `workaround_reset_start` for usage info. The _cve argument is kept here
3863f4c1e1eSBoyan Karatotev * so the same #define can be used as that macro. Additional arguments:
3873f4c1e1eSBoyan Karatotev *
3883f4c1e1eSBoyan Karatotev * _no_isb:
3893f4c1e1eSBoyan Karatotev *	Optionally do not include the trailing isb. Please disable with the
3903f4c1e1eSBoyan Karatotev *	NO_ISB macro
3913f4c1e1eSBoyan Karatotev */
3923f4c1e1eSBoyan Karatotev.macro workaround_runtime_end _cpu:req, _cve:req, _id:req, _no_isb
3933f4c1e1eSBoyan Karatotev	/*
3943f4c1e1eSBoyan Karatotev	 * Runtime errata do not have a reset function to call the isb for them
3953f4c1e1eSBoyan Karatotev	 * and missing the isb could be very problematic. It is also likely as
3963f4c1e1eSBoyan Karatotev	 * they tend to be scattered in generic code.
3973f4c1e1eSBoyan Karatotev	 */
3983f4c1e1eSBoyan Karatotev	.ifb \_no_isb
3993f4c1e1eSBoyan Karatotev		isb
4003f4c1e1eSBoyan Karatotev	.endif
4013f4c1e1eSBoyan Karatotev	_workaround_end \_cpu, \_id
4023f4c1e1eSBoyan Karatotev.endm
4033f4c1e1eSBoyan Karatotev
4043f4c1e1eSBoyan Karatotev/*******************************************************************************
4053f4c1e1eSBoyan Karatotev * Errata workaround helpers
4063f4c1e1eSBoyan Karatotev ******************************************************************************/
4073f4c1e1eSBoyan Karatotev/*
4083f4c1e1eSBoyan Karatotev * Set a bit in a system register. Can set multiple bits but is limited by the
4093f4c1e1eSBoyan Karatotev *  way the ORR instruction encodes them.
4103f4c1e1eSBoyan Karatotev *
4113f4c1e1eSBoyan Karatotev * _reg:
4123f4c1e1eSBoyan Karatotev *	Register to write to
4133f4c1e1eSBoyan Karatotev *
4143f4c1e1eSBoyan Karatotev * _bit:
4153f4c1e1eSBoyan Karatotev *	Bit to set. Please use a descriptive #define
4163f4c1e1eSBoyan Karatotev *
4173f4c1e1eSBoyan Karatotev * _assert:
4183f4c1e1eSBoyan Karatotev *	Optionally whether to read back and assert that the bit has been
4193f4c1e1eSBoyan Karatotev *	written. Please disable with NO_ASSERT macro
4203f4c1e1eSBoyan Karatotev *
4213f4c1e1eSBoyan Karatotev * clobbers: x1
4223f4c1e1eSBoyan Karatotev */
4233f4c1e1eSBoyan Karatotev.macro sysreg_bit_set _reg:req, _bit:req, _assert=1
4243f4c1e1eSBoyan Karatotev	mrs	x1, \_reg
4253f4c1e1eSBoyan Karatotev	orr	x1, x1, #\_bit
4263f4c1e1eSBoyan Karatotev	msr	\_reg, x1
4273f4c1e1eSBoyan Karatotev.endm
4283f4c1e1eSBoyan Karatotev
4293f4c1e1eSBoyan Karatotev/*
430*94a75ad4SBoyan Karatotev * Clear a bit in a system register. Can clear multiple bits but is limited by
431*94a75ad4SBoyan Karatotev *  the way the BIC instrucion encodes them.
432*94a75ad4SBoyan Karatotev *
433*94a75ad4SBoyan Karatotev * see sysreg_bit_set for usage
434*94a75ad4SBoyan Karatotev */
435*94a75ad4SBoyan Karatotev.macro sysreg_bit_clear _reg:req, _bit:req
436*94a75ad4SBoyan Karatotev	mrs	x1, \_reg
437*94a75ad4SBoyan Karatotev	bic	x1, x1, #\_bit
438*94a75ad4SBoyan Karatotev	msr	\_reg, x1
439*94a75ad4SBoyan Karatotev.endm
440*94a75ad4SBoyan Karatotev
441*94a75ad4SBoyan Karatotev.macro override_vector_table _table:req
442*94a75ad4SBoyan Karatotev	adr	x1, \_table
443*94a75ad4SBoyan Karatotev	msr	vbar_el3, x1
444*94a75ad4SBoyan Karatotev.endm
445*94a75ad4SBoyan Karatotev
446*94a75ad4SBoyan Karatotev/*
4473f4c1e1eSBoyan Karatotev * Apply erratum
4483f4c1e1eSBoyan Karatotev *
4493f4c1e1eSBoyan Karatotev * _cpu:
4503f4c1e1eSBoyan Karatotev *	Name of cpu as given to declare_cpu_ops
4513f4c1e1eSBoyan Karatotev *
4523f4c1e1eSBoyan Karatotev * _cve:
4533f4c1e1eSBoyan Karatotev *	Whether erratum is a CVE. CVE year if yes, 0 otherwise
4543f4c1e1eSBoyan Karatotev *
4553f4c1e1eSBoyan Karatotev * _id:
4563f4c1e1eSBoyan Karatotev *	Erratum or CVE number. Please combine with previous field with ERRATUM
4573f4c1e1eSBoyan Karatotev *	or CVE macros
4583f4c1e1eSBoyan Karatotev *
4593f4c1e1eSBoyan Karatotev * _chosen:
4603f4c1e1eSBoyan Karatotev *	Compile time flag on whether the erratum is included
4613f4c1e1eSBoyan Karatotev *
4623f4c1e1eSBoyan Karatotev * clobbers: x0-x9 (PCS compliant)
4633f4c1e1eSBoyan Karatotev */
4643f4c1e1eSBoyan Karatotev.macro apply_erratum _cpu:req, _cve:req, _id:req, _chosen:req
4653f4c1e1eSBoyan Karatotev	.if \_chosen
4663f4c1e1eSBoyan Karatotev		mov	x9, x30
4673f4c1e1eSBoyan Karatotev		bl	cpu_get_rev_var
4683f4c1e1eSBoyan Karatotev		bl	erratum_\_cpu\()_\_id\()_wa
4693f4c1e1eSBoyan Karatotev		mov	x30, x9
4703f4c1e1eSBoyan Karatotev
4713f4c1e1eSBoyan Karatotev	.endif
4723f4c1e1eSBoyan Karatotev.endm
4733f4c1e1eSBoyan Karatotev
4743f4c1e1eSBoyan Karatotev/*
4753f4c1e1eSBoyan Karatotev * Helpers to select which revisions errata apply to. Don't leave a link
4763f4c1e1eSBoyan Karatotev * register as the cpu_rev_var_*** will call the ret and we can save on one.
4773f4c1e1eSBoyan Karatotev *
4783f4c1e1eSBoyan Karatotev * _cpu:
4793f4c1e1eSBoyan Karatotev *	Name of cpu as given to declare_cpu_ops
4803f4c1e1eSBoyan Karatotev *
4813f4c1e1eSBoyan Karatotev * _cve:
4823f4c1e1eSBoyan Karatotev *	Whether erratum is a CVE. CVE year if yes, 0 otherwise
4833f4c1e1eSBoyan Karatotev *
4843f4c1e1eSBoyan Karatotev * _id:
4853f4c1e1eSBoyan Karatotev *	Erratum or CVE number. Please combine with previous field with ERRATUM
4863f4c1e1eSBoyan Karatotev *	or CVE macros
4873f4c1e1eSBoyan Karatotev *
4883f4c1e1eSBoyan Karatotev * _rev_num:
4893f4c1e1eSBoyan Karatotev *	Revision to apply to
4903f4c1e1eSBoyan Karatotev *
4913f4c1e1eSBoyan Karatotev * in body:
4923f4c1e1eSBoyan Karatotev *	clobber: x0 to x4
4933f4c1e1eSBoyan Karatotev *	argument: x0 - cpu_rev_var
4943f4c1e1eSBoyan Karatotev */
4953f4c1e1eSBoyan Karatotev.macro check_erratum_ls _cpu:req, _cve:req, _id:req, _rev_num:req
4963f4c1e1eSBoyan Karatotev	func check_erratum_\_cpu\()_\_id
4973f4c1e1eSBoyan Karatotev		mov	x1, #\_rev_num
4983f4c1e1eSBoyan Karatotev		b	cpu_rev_var_ls
4993f4c1e1eSBoyan Karatotev	endfunc check_erratum_\_cpu\()_\_id
5003f4c1e1eSBoyan Karatotev.endm
5013f4c1e1eSBoyan Karatotev
5023f4c1e1eSBoyan Karatotev.macro check_erratum_hs _cpu:req, _cve:req, _id:req, _rev_num:req
5033f4c1e1eSBoyan Karatotev	func check_erratum_\_cpu\()_\_id
5043f4c1e1eSBoyan Karatotev		mov	x1, #\_rev_num
5053f4c1e1eSBoyan Karatotev		b	cpu_rev_var_hs
5063f4c1e1eSBoyan Karatotev	endfunc check_erratum_\_cpu\()_\_id
5073f4c1e1eSBoyan Karatotev.endm
5083f4c1e1eSBoyan Karatotev
5093f4c1e1eSBoyan Karatotev.macro check_erratum_range _cpu:req, _cve:req, _id:req, _rev_num_lo:req, _rev_num_hi:req
5103f4c1e1eSBoyan Karatotev	func check_erratum_\_cpu\()_\_id
5113f4c1e1eSBoyan Karatotev		mov	x1, #\_rev_num_lo
5123f4c1e1eSBoyan Karatotev		mov	x2, #\_rev_num_hi
5133f4c1e1eSBoyan Karatotev		b	cpu_rev_var_range
5143f4c1e1eSBoyan Karatotev	endfunc check_erratum_\_cpu\()_\_id
5153f4c1e1eSBoyan Karatotev.endm
5163f4c1e1eSBoyan Karatotev
517*94a75ad4SBoyan Karatotev.macro check_erratum_chosen _cpu:req, _cve:req, _id:req, _chosen:req
518*94a75ad4SBoyan Karatotev	func check_erratum_\_cpu\()_\_id
519*94a75ad4SBoyan Karatotev		.if \_chosen
520*94a75ad4SBoyan Karatotev			mov	x0, #ERRATA_APPLIES
521*94a75ad4SBoyan Karatotev		.else
522*94a75ad4SBoyan Karatotev			mov	x0, #ERRATA_MISSING
523*94a75ad4SBoyan Karatotev		.endif
524*94a75ad4SBoyan Karatotev		ret
525*94a75ad4SBoyan Karatotev	endfunc check_erratum_\_cpu\()_\_id
526*94a75ad4SBoyan Karatotev.endm
527*94a75ad4SBoyan Karatotev
528*94a75ad4SBoyan Karatotev/* provide a shorthand for the name format for annoying errata */
529*94a75ad4SBoyan Karatotev.macro check_erratum_custom_start _cpu:req, _cve:req, _id:req
530*94a75ad4SBoyan Karatotev	func check_erratum_\_cpu\()_\_id
531*94a75ad4SBoyan Karatotev.endm
532*94a75ad4SBoyan Karatotev
533*94a75ad4SBoyan Karatotev.macro check_erratum_custom_end _cpu:req, _cve:req, _id:req
534*94a75ad4SBoyan Karatotev	endfunc check_erratum_\_cpu\()_\_id
535*94a75ad4SBoyan Karatotev.endm
536*94a75ad4SBoyan Karatotev
537*94a75ad4SBoyan Karatotev
5383f4c1e1eSBoyan Karatotev/*******************************************************************************
5393f4c1e1eSBoyan Karatotev * CPU reset function wrapper
5403f4c1e1eSBoyan Karatotev ******************************************************************************/
5413f4c1e1eSBoyan Karatotev
5423f4c1e1eSBoyan Karatotev/*
5433f4c1e1eSBoyan Karatotev * Wrapper to automatically apply all reset-time errata. Will end with an isb.
5443f4c1e1eSBoyan Karatotev *
5453f4c1e1eSBoyan Karatotev * _cpu:
5463f4c1e1eSBoyan Karatotev *	Name of cpu as given to declare_cpu_ops
5473f4c1e1eSBoyan Karatotev *
5483f4c1e1eSBoyan Karatotev * in body:
5493f4c1e1eSBoyan Karatotev *	clobber x8 to x14
5503f4c1e1eSBoyan Karatotev *	argument x14 - cpu_rev_var
5513f4c1e1eSBoyan Karatotev */
5523f4c1e1eSBoyan Karatotev.macro cpu_reset_func_start _cpu:req
5533f4c1e1eSBoyan Karatotev	func \_cpu\()_reset_func
5543f4c1e1eSBoyan Karatotev		mov	x15, x30
5553f4c1e1eSBoyan Karatotev		bl	cpu_get_rev_var
5563f4c1e1eSBoyan Karatotev		mov	x14, x0
5573f4c1e1eSBoyan Karatotev
5583f4c1e1eSBoyan Karatotev		/* short circuit the location to avoid searching the list */
5593f4c1e1eSBoyan Karatotev		adrp	x12, \_cpu\()_errata_list_start
5603f4c1e1eSBoyan Karatotev		add	x12, x12, :lo12:\_cpu\()_errata_list_start
5613f4c1e1eSBoyan Karatotev		adrp	x13, \_cpu\()_errata_list_end
5623f4c1e1eSBoyan Karatotev		add	x13, x13, :lo12:\_cpu\()_errata_list_end
5633f4c1e1eSBoyan Karatotev
5643f4c1e1eSBoyan Karatotev	errata_begin:
5653f4c1e1eSBoyan Karatotev		/* if head catches up with end of list, exit */
5663f4c1e1eSBoyan Karatotev		cmp	x12, x13
5673f4c1e1eSBoyan Karatotev		b.eq	errata_end
5683f4c1e1eSBoyan Karatotev
5693f4c1e1eSBoyan Karatotev		ldr	x10, [x12, #ERRATUM_WA_FUNC]
5703f4c1e1eSBoyan Karatotev		/* TODO(errata ABI): check mitigated and checker function fields
5713f4c1e1eSBoyan Karatotev		 * for 0 */
5723f4c1e1eSBoyan Karatotev		ldrb	w11, [x12, #ERRATUM_CHOSEN]
5733f4c1e1eSBoyan Karatotev
5743f4c1e1eSBoyan Karatotev		/* skip if not chosen */
5753f4c1e1eSBoyan Karatotev		cbz	x11, 1f
5763f4c1e1eSBoyan Karatotev		/* skip if runtime erratum */
5773f4c1e1eSBoyan Karatotev		cbz	x10, 1f
5783f4c1e1eSBoyan Karatotev
5793f4c1e1eSBoyan Karatotev		/* put cpu revision in x0 and call workaround */
5803f4c1e1eSBoyan Karatotev		mov	x0, x14
5813f4c1e1eSBoyan Karatotev		blr	x10
5823f4c1e1eSBoyan Karatotev	1:
5833f4c1e1eSBoyan Karatotev		add	x12, x12, #ERRATUM_ENTRY_SIZE
5843f4c1e1eSBoyan Karatotev		b	errata_begin
5853f4c1e1eSBoyan Karatotev	errata_end:
5863f4c1e1eSBoyan Karatotev.endm
5873f4c1e1eSBoyan Karatotev
5883f4c1e1eSBoyan Karatotev.macro cpu_reset_func_end _cpu:req
5893f4c1e1eSBoyan Karatotev		isb
5903f4c1e1eSBoyan Karatotev		ret	x15
5913f4c1e1eSBoyan Karatotev	endfunc \_cpu\()_reset_func
5923f4c1e1eSBoyan Karatotev.endm
5934f748cc4SBoyan Karatotev
5944f748cc4SBoyan Karatotev/*
5954f748cc4SBoyan Karatotev * Maintain compatibility with the old scheme of each cpu has its own reporting.
5964f748cc4SBoyan Karatotev * TODO remove entirely once all cpus have been converted. This includes the
5974f748cc4SBoyan Karatotev * cpu_ops entry, as print_errata_status can call this directly for all cpus
5984f748cc4SBoyan Karatotev */
5994f748cc4SBoyan Karatotev.macro errata_report_shim _cpu:req
6004f748cc4SBoyan Karatotev	#if REPORT_ERRATA
6014f748cc4SBoyan Karatotev	func \_cpu\()_errata_report
6024f748cc4SBoyan Karatotev		/* normal stack frame for pretty debugging */
6034f748cc4SBoyan Karatotev		stp	x29, x30, [sp, #-16]!
6044f748cc4SBoyan Karatotev		mov	x29, sp
6054f748cc4SBoyan Karatotev
6064f748cc4SBoyan Karatotev		bl	generic_errata_report
6074f748cc4SBoyan Karatotev
6084f748cc4SBoyan Karatotev		ldp	x29, x30, [sp], #16
6094f748cc4SBoyan Karatotev		ret
6104f748cc4SBoyan Karatotev	endfunc \_cpu\()_errata_report
6114f748cc4SBoyan Karatotev	#endif
6124f748cc4SBoyan Karatotev.endm
613c3cf06f1SAntonio Nino Diaz#endif /* CPU_MACROS_S */
614