xref: /rk3399_ARM-atf/include/lib/cpus/aarch64/cpu_macros.S (revision f21b9f6d6e6ddda6d92ec25b01f70c30bcc82d57)
1add40351SSoby Mathew/*
2a205a56eSDimitris Papastamos * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
3add40351SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5add40351SSoby Mathew */
6e2bf57f8SDan Handley#ifndef __CPU_MACROS_S__
7e2bf57f8SDan Handley#define __CPU_MACROS_S__
8add40351SSoby Mathew
9add40351SSoby Mathew#include <arch.h>
1010bcd761SJeenu Viswambharan#include <errata_report.h>
11add40351SSoby Mathew
12add40351SSoby Mathew#define CPU_IMPL_PN_MASK	(MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \
13add40351SSoby Mathew				(MIDR_PN_MASK << MIDR_PN_SHIFT)
14add40351SSoby Mathew
155dd9dbb5SJeenu Viswambharan/* The number of CPU operations allowed */
165dd9dbb5SJeenu Viswambharan#define CPU_MAX_PWR_DWN_OPS		2
175dd9dbb5SJeenu Viswambharan
185dd9dbb5SJeenu Viswambharan/* Special constant to specify that CPU has no reset function */
195dd9dbb5SJeenu Viswambharan#define CPU_NO_RESET_FUNC		0
205dd9dbb5SJeenu Viswambharan
21fe007b2eSDimitris Papastamos#define CPU_NO_EXTRA1_FUNC		0
22fe007b2eSDimitris Papastamos#define CPU_NO_EXTRA2_FUNC		0
23fe007b2eSDimitris Papastamos
245dd9dbb5SJeenu Viswambharan/* Word size for 64-bit CPUs */
255dd9dbb5SJeenu Viswambharan#define CPU_WORD_SIZE			8
265dd9dbb5SJeenu Viswambharan
27b1d27b48SRoberto Vargas#if defined(IMAGE_BL1) || defined(IMAGE_BL31) ||(defined(IMAGE_BL2) && BL2_AT_EL3)
28b1d27b48SRoberto Vargas#define IMAGE_AT_EL3
29b1d27b48SRoberto Vargas#endif
30b1d27b48SRoberto Vargas
31add40351SSoby Mathew/*
3210bcd761SJeenu Viswambharan * Whether errata status needs reporting. Errata status is printed in debug
3310bcd761SJeenu Viswambharan * builds for both BL1 and BL31 images.
3410bcd761SJeenu Viswambharan */
3510bcd761SJeenu Viswambharan#if (defined(IMAGE_BL1) || defined(IMAGE_BL31)) && DEBUG
3610bcd761SJeenu Viswambharan# define REPORT_ERRATA	1
3710bcd761SJeenu Viswambharan#else
3810bcd761SJeenu Viswambharan# define REPORT_ERRATA	0
3910bcd761SJeenu Viswambharan#endif
4010bcd761SJeenu Viswambharan
41*f21b9f6dSRoberto Vargas
42*f21b9f6dSRoberto Vargas	.equ	CPU_MIDR_SIZE, CPU_WORD_SIZE
43*f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA1_FUNC_SIZE, CPU_WORD_SIZE
44*f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA2_FUNC_SIZE, CPU_WORD_SIZE
45*f21b9f6dSRoberto Vargas	.equ	CPU_RESET_FUNC_SIZE, CPU_WORD_SIZE
46*f21b9f6dSRoberto Vargas	.equ	CPU_PWR_DWN_OPS_SIZE, CPU_WORD_SIZE * CPU_MAX_PWR_DWN_OPS
47*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_FUNC_SIZE, CPU_WORD_SIZE
48*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_LOCK_SIZE, CPU_WORD_SIZE
49*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_PRINTED_SIZE, CPU_WORD_SIZE
50*f21b9f6dSRoberto Vargas	.equ	CPU_REG_DUMP_SIZE, CPU_WORD_SIZE
51*f21b9f6dSRoberto Vargas
52*f21b9f6dSRoberto Vargas#ifndef IMAGE_AT_EL3
53*f21b9f6dSRoberto Vargas	.equ	CPU_RESET_FUNC_SIZE, 0
54*f21b9f6dSRoberto Vargas#endif
55*f21b9f6dSRoberto Vargas
56*f21b9f6dSRoberto Vargas/* The power down core and cluster is needed only in BL31 */
57*f21b9f6dSRoberto Vargas#ifndef IMAGE_BL31
58*f21b9f6dSRoberto Vargas	.equ	CPU_PWR_DWN_OPS_SIZE, 0
59*f21b9f6dSRoberto Vargas#endif
60*f21b9f6dSRoberto Vargas
61*f21b9f6dSRoberto Vargas/* Fields required to print errata status. */
62*f21b9f6dSRoberto Vargas#if !REPORT_ERRATA
63*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_FUNC_SIZE, 0
64*f21b9f6dSRoberto Vargas#endif
65*f21b9f6dSRoberto Vargas
66*f21b9f6dSRoberto Vargas/* Only BL31 requieres mutual exclusion and printed flag.  */
67*f21b9f6dSRoberto Vargas#if !(REPORT_ERRATA && defined(IMAGE_BL31))
68*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_LOCK_SIZE, 0
69*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_PRINTED_SIZE, 0
70*f21b9f6dSRoberto Vargas#endif
71*f21b9f6dSRoberto Vargas
72*f21b9f6dSRoberto Vargas#if !defined(IMAGE_BL31) || !CRASH_REPORTING
73*f21b9f6dSRoberto Vargas	.equ	CPU_REG_DUMP_SIZE, 0
74*f21b9f6dSRoberto Vargas#endif
75*f21b9f6dSRoberto Vargas
7610bcd761SJeenu Viswambharan/*
77add40351SSoby Mathew * Define the offsets to the fields in cpu_ops structure.
78*f21b9f6dSRoberto Vargas * Every offset is defined based in the offset and size of the previous
79*f21b9f6dSRoberto Vargas * field.
80add40351SSoby Mathew */
81*f21b9f6dSRoberto Vargas	.equ	CPU_MIDR, 0
82*f21b9f6dSRoberto Vargas	.equ	CPU_RESET_FUNC, CPU_MIDR + CPU_MIDR_SIZE
83*f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA1_FUNC, CPU_RESET_FUNC + CPU_RESET_FUNC_SIZE
84*f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA2_FUNC, CPU_EXTRA1_FUNC + CPU_EXTRA1_FUNC_SIZE
85*f21b9f6dSRoberto Vargas	.equ	CPU_PWR_DWN_OPS, CPU_EXTRA2_FUNC + CPU_EXTRA2_FUNC_SIZE
86*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_FUNC, CPU_PWR_DWN_OPS + CPU_PWR_DWN_OPS_SIZE
87*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_LOCK, CPU_ERRATA_FUNC + CPU_ERRATA_FUNC_SIZE
88*f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_PRINTED, CPU_ERRATA_LOCK + CPU_ERRATA_LOCK_SIZE
89*f21b9f6dSRoberto Vargas	.equ	CPU_REG_DUMP, CPU_ERRATA_PRINTED + CPU_ERRATA_PRINTED_SIZE
90*f21b9f6dSRoberto Vargas	.equ	CPU_OPS_SIZE, CPU_REG_DUMP + CPU_REG_DUMP_SIZE
91add40351SSoby Mathew
92add40351SSoby Mathew	/*
935dd9dbb5SJeenu Viswambharan	 * Write given expressions as quad words
945dd9dbb5SJeenu Viswambharan	 *
955dd9dbb5SJeenu Viswambharan	 * _count:
965dd9dbb5SJeenu Viswambharan	 *	Write at least _count quad words. If the given number of
975dd9dbb5SJeenu Viswambharan	 *	expressions is less than _count, repeat the last expression to
985dd9dbb5SJeenu Viswambharan	 *	fill _count quad words in total
995dd9dbb5SJeenu Viswambharan	 * _rest:
1005dd9dbb5SJeenu Viswambharan	 *	Optional list of expressions. _this is for parameter extraction
1015dd9dbb5SJeenu Viswambharan	 *	only, and has no significance to the caller
1025dd9dbb5SJeenu Viswambharan	 *
1035dd9dbb5SJeenu Viswambharan	 * Invoked as:
1045dd9dbb5SJeenu Viswambharan	 *	fill_constants 2, foo, bar, blah, ...
105add40351SSoby Mathew	 */
1065dd9dbb5SJeenu Viswambharan	.macro fill_constants _count:req, _this, _rest:vararg
1075dd9dbb5SJeenu Viswambharan	  .ifgt \_count
1085dd9dbb5SJeenu Viswambharan	    /* Write the current expression */
1095dd9dbb5SJeenu Viswambharan	    .ifb \_this
1105dd9dbb5SJeenu Viswambharan	      .error "Nothing to fill"
1115dd9dbb5SJeenu Viswambharan	    .endif
1125dd9dbb5SJeenu Viswambharan	    .quad \_this
1135dd9dbb5SJeenu Viswambharan
1145dd9dbb5SJeenu Viswambharan	    /* Invoke recursively for remaining expressions */
1155dd9dbb5SJeenu Viswambharan	    .ifnb \_rest
1165dd9dbb5SJeenu Viswambharan	      fill_constants \_count-1, \_rest
1175dd9dbb5SJeenu Viswambharan	    .else
1185dd9dbb5SJeenu Viswambharan	      fill_constants \_count-1, \_this
1195dd9dbb5SJeenu Viswambharan	    .endif
1205dd9dbb5SJeenu Viswambharan	  .endif
1215dd9dbb5SJeenu Viswambharan	.endm
1225dd9dbb5SJeenu Viswambharan
1235dd9dbb5SJeenu Viswambharan	/*
1245dd9dbb5SJeenu Viswambharan	 * Declare CPU operations
1255dd9dbb5SJeenu Viswambharan	 *
1265dd9dbb5SJeenu Viswambharan	 * _name:
1275dd9dbb5SJeenu Viswambharan	 *	Name of the CPU for which operations are being specified
1285dd9dbb5SJeenu Viswambharan	 * _midr:
1295dd9dbb5SJeenu Viswambharan	 *	Numeric value expected to read from CPU's MIDR
1305dd9dbb5SJeenu Viswambharan	 * _resetfunc:
1315dd9dbb5SJeenu Viswambharan	 *	Reset function for the CPU. If there's no CPU reset function,
1325dd9dbb5SJeenu Viswambharan	 *	specify CPU_NO_RESET_FUNC
133a205a56eSDimitris Papastamos	 * _extra1:
134a205a56eSDimitris Papastamos	 *	This is a placeholder for future per CPU operations.  Currently,
135a205a56eSDimitris Papastamos	 *	some CPUs use this entry to set a test function to determine if
136a205a56eSDimitris Papastamos	 *	the workaround for CVE-2017-5715 needs to be applied or not.
137fe007b2eSDimitris Papastamos	 * _extra2:
138fe007b2eSDimitris Papastamos	 *	This is a placeholder for future per CPU operations.  Currently
139fe007b2eSDimitris Papastamos	 *	some CPUs use this entry to set a function to disable the
140fe007b2eSDimitris Papastamos	 *	workaround for CVE-2018-3639.
1415dd9dbb5SJeenu Viswambharan	 * _power_down_ops:
1425dd9dbb5SJeenu Viswambharan	 *	Comma-separated list of functions to perform power-down
1435dd9dbb5SJeenu Viswambharan	 *	operatios on the CPU. At least one, and up to
1445dd9dbb5SJeenu Viswambharan	 *	CPU_MAX_PWR_DWN_OPS number of functions may be specified.
1455dd9dbb5SJeenu Viswambharan	 *	Starting at power level 0, these functions shall handle power
1465dd9dbb5SJeenu Viswambharan	 *	down at subsequent power levels. If there aren't exactly
1475dd9dbb5SJeenu Viswambharan	 *	CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
1485dd9dbb5SJeenu Viswambharan	 *	used to handle power down at subsequent levels
1495dd9dbb5SJeenu Viswambharan	 */
150a205a56eSDimitris Papastamos	.macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
151fe007b2eSDimitris Papastamos		_extra1:req, _extra2:req, _power_down_ops:vararg
1525dd9dbb5SJeenu Viswambharan	.section cpu_ops, "a"
1535dd9dbb5SJeenu Viswambharan	.align 3
154add40351SSoby Mathew	.type cpu_ops_\_name, %object
155add40351SSoby Mathew	.quad \_midr
156b1d27b48SRoberto Vargas#if defined(IMAGE_AT_EL3)
1575dd9dbb5SJeenu Viswambharan	.quad \_resetfunc
158add40351SSoby Mathew#endif
159a205a56eSDimitris Papastamos	.quad \_extra1
160fe007b2eSDimitris Papastamos	.quad \_extra2
1613d8256b2SMasahiro Yamada#ifdef IMAGE_BL31
1625dd9dbb5SJeenu Viswambharan1:
1635dd9dbb5SJeenu Viswambharan	/* Insert list of functions */
1645dd9dbb5SJeenu Viswambharan	fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
1655dd9dbb5SJeenu Viswambharan2:
1665dd9dbb5SJeenu Viswambharan	/*
1675dd9dbb5SJeenu Viswambharan	 * Error if no or more than CPU_MAX_PWR_DWN_OPS were specified in the
1685dd9dbb5SJeenu Viswambharan	 * list
1695dd9dbb5SJeenu Viswambharan	 */
1705dd9dbb5SJeenu Viswambharan	.ifeq 2b - 1b
1715dd9dbb5SJeenu Viswambharan	  .error "At least one power down function must be specified"
1725dd9dbb5SJeenu Viswambharan	.else
1735dd9dbb5SJeenu Viswambharan	  .iflt 2b - 1b - (CPU_MAX_PWR_DWN_OPS * CPU_WORD_SIZE)
1745dd9dbb5SJeenu Viswambharan	    .error "More than CPU_MAX_PWR_DWN_OPS functions specified"
1755dd9dbb5SJeenu Viswambharan	  .endif
1765dd9dbb5SJeenu Viswambharan	.endif
177add40351SSoby Mathew#endif
17810bcd761SJeenu Viswambharan
17910bcd761SJeenu Viswambharan#if REPORT_ERRATA
18010bcd761SJeenu Viswambharan	.ifndef \_name\()_cpu_str
18110bcd761SJeenu Viswambharan	  /*
18210bcd761SJeenu Viswambharan	   * Place errata reported flag, and the spinlock to arbitrate access to
18310bcd761SJeenu Viswambharan	   * it in the data section.
18410bcd761SJeenu Viswambharan	   */
18510bcd761SJeenu Viswambharan	  .pushsection .data
18610bcd761SJeenu Viswambharan	  define_asm_spinlock \_name\()_errata_lock
18710bcd761SJeenu Viswambharan	  \_name\()_errata_reported:
18810bcd761SJeenu Viswambharan	  .word	0
18910bcd761SJeenu Viswambharan	  .popsection
19010bcd761SJeenu Viswambharan
19110bcd761SJeenu Viswambharan	  /* Place CPU string in rodata */
19210bcd761SJeenu Viswambharan	  .pushsection .rodata
19310bcd761SJeenu Viswambharan	  \_name\()_cpu_str:
19410bcd761SJeenu Viswambharan	  .asciz "\_name"
19510bcd761SJeenu Viswambharan	  .popsection
19610bcd761SJeenu Viswambharan	.endif
19710bcd761SJeenu Viswambharan
19810bcd761SJeenu Viswambharan	/*
19910bcd761SJeenu Viswambharan	 * Weakly-bound, optional errata status printing function for CPUs of
20010bcd761SJeenu Viswambharan	 * this class.
20110bcd761SJeenu Viswambharan	 */
20210bcd761SJeenu Viswambharan	.weak \_name\()_errata_report
20310bcd761SJeenu Viswambharan	.quad \_name\()_errata_report
20410bcd761SJeenu Viswambharan
20510bcd761SJeenu Viswambharan#ifdef IMAGE_BL31
20610bcd761SJeenu Viswambharan	/* Pointers to errata lock and reported flag */
20710bcd761SJeenu Viswambharan	.quad \_name\()_errata_lock
20810bcd761SJeenu Viswambharan	.quad \_name\()_errata_reported
20910bcd761SJeenu Viswambharan#endif
21010bcd761SJeenu Viswambharan#endif
21110bcd761SJeenu Viswambharan
2123d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
213d3f70af6SSoby Mathew	.quad \_name\()_cpu_reg_dump
214d3f70af6SSoby Mathew#endif
215add40351SSoby Mathew	.endm
216e2bf57f8SDan Handley
217a205a56eSDimitris Papastamos	.macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
218a205a56eSDimitris Papastamos		_power_down_ops:vararg
219fe007b2eSDimitris Papastamos		declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, \
220a205a56eSDimitris Papastamos			\_power_down_ops
221a205a56eSDimitris Papastamos	.endm
222a205a56eSDimitris Papastamos
223fe007b2eSDimitris Papastamos	.macro declare_cpu_ops_wa _name:req, _midr:req, \
224fe007b2eSDimitris Papastamos		_resetfunc:req, _extra1:req, _extra2:req, \
225fe007b2eSDimitris Papastamos		_power_down_ops:vararg
226a205a56eSDimitris Papastamos		declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
227fe007b2eSDimitris Papastamos			\_extra1, \_extra2, \_power_down_ops
228a205a56eSDimitris Papastamos	.endm
229a205a56eSDimitris Papastamos
23010bcd761SJeenu Viswambharan#if REPORT_ERRATA
23110bcd761SJeenu Viswambharan	/*
23210bcd761SJeenu Viswambharan	 * Print status of a CPU errata
23310bcd761SJeenu Viswambharan	 *
23410bcd761SJeenu Viswambharan	 * _chosen:
23510bcd761SJeenu Viswambharan	 *	Identifier indicating whether or not a CPU errata has been
23610bcd761SJeenu Viswambharan	 *	compiled in.
23710bcd761SJeenu Viswambharan	 * _cpu:
23810bcd761SJeenu Viswambharan	 *	Name of the CPU
23910bcd761SJeenu Viswambharan	 * _id:
24010bcd761SJeenu Viswambharan	 *	Errata identifier
24110bcd761SJeenu Viswambharan	 * _rev_var:
24210bcd761SJeenu Viswambharan	 *	Register containing the combined value CPU revision and variant
24310bcd761SJeenu Viswambharan	 *	- typically the return value of cpu_get_rev_var
24410bcd761SJeenu Viswambharan	 */
24510bcd761SJeenu Viswambharan	.macro report_errata _chosen, _cpu, _id, _rev_var=x8
24610bcd761SJeenu Viswambharan	/* Stash a string with errata ID */
24710bcd761SJeenu Viswambharan	.pushsection .rodata
24810bcd761SJeenu Viswambharan	\_cpu\()_errata_\_id\()_str:
24910bcd761SJeenu Viswambharan	.asciz	"\_id"
25010bcd761SJeenu Viswambharan	.popsection
25110bcd761SJeenu Viswambharan
25210bcd761SJeenu Viswambharan	/* Check whether errata applies */
25310bcd761SJeenu Viswambharan	mov	x0, \_rev_var
2549ec3921cSJonathan Wright	/* Shall clobber: x0-x7 */
25510bcd761SJeenu Viswambharan	bl	check_errata_\_id
25610bcd761SJeenu Viswambharan
25710bcd761SJeenu Viswambharan	.ifeq \_chosen
25810bcd761SJeenu Viswambharan	/*
25910bcd761SJeenu Viswambharan	 * Errata workaround has not been compiled in. If the errata would have
26010bcd761SJeenu Viswambharan	 * applied had it been compiled in, print its status as missing.
26110bcd761SJeenu Viswambharan	 */
26210bcd761SJeenu Viswambharan	cbz	x0, 900f
26310bcd761SJeenu Viswambharan	mov	x0, #ERRATA_MISSING
26410bcd761SJeenu Viswambharan	.endif
26510bcd761SJeenu Viswambharan900:
26610bcd761SJeenu Viswambharan	adr	x1, \_cpu\()_cpu_str
26710bcd761SJeenu Viswambharan	adr	x2, \_cpu\()_errata_\_id\()_str
26810bcd761SJeenu Viswambharan	bl	errata_print_msg
26910bcd761SJeenu Viswambharan	.endm
27010bcd761SJeenu Viswambharan#endif
27110bcd761SJeenu Viswambharan
272e2bf57f8SDan Handley#endif /* __CPU_MACROS_S__ */
2733991a6a4SDimitris Papastamos
2743991a6a4SDimitris Papastamos	/*
2753991a6a4SDimitris Papastamos	 * This macro is used on some CPUs to detect if they are vulnerable
2763991a6a4SDimitris Papastamos	 * to CVE-2017-5715.
2773991a6a4SDimitris Papastamos	 */
2783991a6a4SDimitris Papastamos	.macro	cpu_check_csv2 _reg _label
2793991a6a4SDimitris Papastamos	mrs	\_reg, id_aa64pfr0_el1
2803991a6a4SDimitris Papastamos	ubfx	\_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
2813991a6a4SDimitris Papastamos	/*
2823991a6a4SDimitris Papastamos	 * If the field equals to 1 then branch targets trained in one
2833991a6a4SDimitris Papastamos	 * context cannot affect speculative execution in a different context.
2843991a6a4SDimitris Papastamos	 */
2853991a6a4SDimitris Papastamos	cmp	\_reg, #1
2863991a6a4SDimitris Papastamos	beq	\_label
2873991a6a4SDimitris Papastamos	.endm
288