xref: /rk3399_ARM-atf/include/lib/cpus/aarch64/cpu_macros.S (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
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 */
6c3cf06f1SAntonio Nino Diaz#ifndef CPU_MACROS_S
7c3cf06f1SAntonio Nino Diaz#define CPU_MACROS_S
8add40351SSoby Mathew
9add40351SSoby Mathew#include <arch.h>
10*09d40e0eSAntonio Nino Diaz#include <lib/cpus/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
41f21b9f6dSRoberto Vargas
42f21b9f6dSRoberto Vargas	.equ	CPU_MIDR_SIZE, CPU_WORD_SIZE
43f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA1_FUNC_SIZE, CPU_WORD_SIZE
44f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA2_FUNC_SIZE, CPU_WORD_SIZE
45f21b9f6dSRoberto Vargas	.equ	CPU_RESET_FUNC_SIZE, CPU_WORD_SIZE
46f21b9f6dSRoberto Vargas	.equ	CPU_PWR_DWN_OPS_SIZE, CPU_WORD_SIZE * CPU_MAX_PWR_DWN_OPS
47f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_FUNC_SIZE, CPU_WORD_SIZE
48f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_LOCK_SIZE, CPU_WORD_SIZE
49f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_PRINTED_SIZE, CPU_WORD_SIZE
50f21b9f6dSRoberto Vargas	.equ	CPU_REG_DUMP_SIZE, CPU_WORD_SIZE
51f21b9f6dSRoberto Vargas
52f21b9f6dSRoberto Vargas#ifndef IMAGE_AT_EL3
53f21b9f6dSRoberto Vargas	.equ	CPU_RESET_FUNC_SIZE, 0
54f21b9f6dSRoberto Vargas#endif
55f21b9f6dSRoberto Vargas
56f21b9f6dSRoberto Vargas/* The power down core and cluster is needed only in BL31 */
57f21b9f6dSRoberto Vargas#ifndef IMAGE_BL31
58f21b9f6dSRoberto Vargas	.equ	CPU_PWR_DWN_OPS_SIZE, 0
59f21b9f6dSRoberto Vargas#endif
60f21b9f6dSRoberto Vargas
61f21b9f6dSRoberto Vargas/* Fields required to print errata status. */
62f21b9f6dSRoberto Vargas#if !REPORT_ERRATA
63f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_FUNC_SIZE, 0
64f21b9f6dSRoberto Vargas#endif
65f21b9f6dSRoberto Vargas
66f21b9f6dSRoberto Vargas/* Only BL31 requieres mutual exclusion and printed flag.  */
67f21b9f6dSRoberto Vargas#if !(REPORT_ERRATA && defined(IMAGE_BL31))
68f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_LOCK_SIZE, 0
69f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_PRINTED_SIZE, 0
70f21b9f6dSRoberto Vargas#endif
71f21b9f6dSRoberto Vargas
72f21b9f6dSRoberto Vargas#if !defined(IMAGE_BL31) || !CRASH_REPORTING
73f21b9f6dSRoberto Vargas	.equ	CPU_REG_DUMP_SIZE, 0
74f21b9f6dSRoberto Vargas#endif
75f21b9f6dSRoberto Vargas
7610bcd761SJeenu Viswambharan/*
77add40351SSoby Mathew * Define the offsets to the fields in cpu_ops structure.
78f21b9f6dSRoberto Vargas * Every offset is defined based in the offset and size of the previous
79f21b9f6dSRoberto Vargas * field.
80add40351SSoby Mathew */
81f21b9f6dSRoberto Vargas	.equ	CPU_MIDR, 0
82f21b9f6dSRoberto Vargas	.equ	CPU_RESET_FUNC, CPU_MIDR + CPU_MIDR_SIZE
83f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA1_FUNC, CPU_RESET_FUNC + CPU_RESET_FUNC_SIZE
84f21b9f6dSRoberto Vargas	.equ	CPU_EXTRA2_FUNC, CPU_EXTRA1_FUNC + CPU_EXTRA1_FUNC_SIZE
85f21b9f6dSRoberto Vargas	.equ	CPU_PWR_DWN_OPS, CPU_EXTRA2_FUNC + CPU_EXTRA2_FUNC_SIZE
86f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_FUNC, CPU_PWR_DWN_OPS + CPU_PWR_DWN_OPS_SIZE
87f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_LOCK, CPU_ERRATA_FUNC + CPU_ERRATA_FUNC_SIZE
88f21b9f6dSRoberto Vargas	.equ	CPU_ERRATA_PRINTED, CPU_ERRATA_LOCK + CPU_ERRATA_LOCK_SIZE
89f21b9f6dSRoberto Vargas	.equ	CPU_REG_DUMP, CPU_ERRATA_PRINTED + CPU_ERRATA_PRINTED_SIZE
90f21b9f6dSRoberto 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 Viswambharan	/* Insert list of functions */
1635dd9dbb5SJeenu Viswambharan	fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
164add40351SSoby Mathew#endif
16510bcd761SJeenu Viswambharan
16610bcd761SJeenu Viswambharan#if REPORT_ERRATA
16710bcd761SJeenu Viswambharan	.ifndef \_name\()_cpu_str
16810bcd761SJeenu Viswambharan	  /*
16910bcd761SJeenu Viswambharan	   * Place errata reported flag, and the spinlock to arbitrate access to
17010bcd761SJeenu Viswambharan	   * it in the data section.
17110bcd761SJeenu Viswambharan	   */
17210bcd761SJeenu Viswambharan	  .pushsection .data
17310bcd761SJeenu Viswambharan	  define_asm_spinlock \_name\()_errata_lock
17410bcd761SJeenu Viswambharan	  \_name\()_errata_reported:
17510bcd761SJeenu Viswambharan	  .word	0
17610bcd761SJeenu Viswambharan	  .popsection
17710bcd761SJeenu Viswambharan
17810bcd761SJeenu Viswambharan	  /* Place CPU string in rodata */
17910bcd761SJeenu Viswambharan	  .pushsection .rodata
18010bcd761SJeenu Viswambharan	  \_name\()_cpu_str:
18110bcd761SJeenu Viswambharan	  .asciz "\_name"
18210bcd761SJeenu Viswambharan	  .popsection
18310bcd761SJeenu Viswambharan	.endif
18410bcd761SJeenu Viswambharan
18510bcd761SJeenu Viswambharan	/*
18612af5ed4SSoby Mathew	 * Mandatory errata status printing function for CPUs of
18710bcd761SJeenu Viswambharan	 * this class.
18810bcd761SJeenu Viswambharan	 */
18910bcd761SJeenu Viswambharan	.quad \_name\()_errata_report
19010bcd761SJeenu Viswambharan
19110bcd761SJeenu Viswambharan#ifdef IMAGE_BL31
19210bcd761SJeenu Viswambharan	/* Pointers to errata lock and reported flag */
19310bcd761SJeenu Viswambharan	.quad \_name\()_errata_lock
19410bcd761SJeenu Viswambharan	.quad \_name\()_errata_reported
19510bcd761SJeenu Viswambharan#endif
19610bcd761SJeenu Viswambharan#endif
19710bcd761SJeenu Viswambharan
1983d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING
199d3f70af6SSoby Mathew	.quad \_name\()_cpu_reg_dump
200d3f70af6SSoby Mathew#endif
201add40351SSoby Mathew	.endm
202e2bf57f8SDan Handley
203a205a56eSDimitris Papastamos	.macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
204a205a56eSDimitris Papastamos		_power_down_ops:vararg
205fe007b2eSDimitris Papastamos		declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, \
206a205a56eSDimitris Papastamos			\_power_down_ops
207a205a56eSDimitris Papastamos	.endm
208a205a56eSDimitris Papastamos
209fe007b2eSDimitris Papastamos	.macro declare_cpu_ops_wa _name:req, _midr:req, \
210fe007b2eSDimitris Papastamos		_resetfunc:req, _extra1:req, _extra2:req, \
211fe007b2eSDimitris Papastamos		_power_down_ops:vararg
212a205a56eSDimitris Papastamos		declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
213fe007b2eSDimitris Papastamos			\_extra1, \_extra2, \_power_down_ops
214a205a56eSDimitris Papastamos	.endm
215a205a56eSDimitris Papastamos
21610bcd761SJeenu Viswambharan#if REPORT_ERRATA
21710bcd761SJeenu Viswambharan	/*
21810bcd761SJeenu Viswambharan	 * Print status of a CPU errata
21910bcd761SJeenu Viswambharan	 *
22010bcd761SJeenu Viswambharan	 * _chosen:
22110bcd761SJeenu Viswambharan	 *	Identifier indicating whether or not a CPU errata has been
22210bcd761SJeenu Viswambharan	 *	compiled in.
22310bcd761SJeenu Viswambharan	 * _cpu:
22410bcd761SJeenu Viswambharan	 *	Name of the CPU
22510bcd761SJeenu Viswambharan	 * _id:
22610bcd761SJeenu Viswambharan	 *	Errata identifier
22710bcd761SJeenu Viswambharan	 * _rev_var:
22810bcd761SJeenu Viswambharan	 *	Register containing the combined value CPU revision and variant
22910bcd761SJeenu Viswambharan	 *	- typically the return value of cpu_get_rev_var
23010bcd761SJeenu Viswambharan	 */
23110bcd761SJeenu Viswambharan	.macro report_errata _chosen, _cpu, _id, _rev_var=x8
23210bcd761SJeenu Viswambharan	/* Stash a string with errata ID */
23310bcd761SJeenu Viswambharan	.pushsection .rodata
23410bcd761SJeenu Viswambharan	\_cpu\()_errata_\_id\()_str:
23510bcd761SJeenu Viswambharan	.asciz	"\_id"
23610bcd761SJeenu Viswambharan	.popsection
23710bcd761SJeenu Viswambharan
23810bcd761SJeenu Viswambharan	/* Check whether errata applies */
23910bcd761SJeenu Viswambharan	mov	x0, \_rev_var
2409ec3921cSJonathan Wright	/* Shall clobber: x0-x7 */
24110bcd761SJeenu Viswambharan	bl	check_errata_\_id
24210bcd761SJeenu Viswambharan
24310bcd761SJeenu Viswambharan	.ifeq \_chosen
24410bcd761SJeenu Viswambharan	/*
24510bcd761SJeenu Viswambharan	 * Errata workaround has not been compiled in. If the errata would have
24610bcd761SJeenu Viswambharan	 * applied had it been compiled in, print its status as missing.
24710bcd761SJeenu Viswambharan	 */
24810bcd761SJeenu Viswambharan	cbz	x0, 900f
24910bcd761SJeenu Viswambharan	mov	x0, #ERRATA_MISSING
25010bcd761SJeenu Viswambharan	.endif
25110bcd761SJeenu Viswambharan900:
25210bcd761SJeenu Viswambharan	adr	x1, \_cpu\()_cpu_str
25310bcd761SJeenu Viswambharan	adr	x2, \_cpu\()_errata_\_id\()_str
25410bcd761SJeenu Viswambharan	bl	errata_print_msg
25510bcd761SJeenu Viswambharan	.endm
25610bcd761SJeenu Viswambharan#endif
25710bcd761SJeenu Viswambharan
2583991a6a4SDimitris Papastamos	/*
2593991a6a4SDimitris Papastamos	 * This macro is used on some CPUs to detect if they are vulnerable
2603991a6a4SDimitris Papastamos	 * to CVE-2017-5715.
2613991a6a4SDimitris Papastamos	 */
2623991a6a4SDimitris Papastamos	.macro	cpu_check_csv2 _reg _label
2633991a6a4SDimitris Papastamos	mrs	\_reg, id_aa64pfr0_el1
2643991a6a4SDimitris Papastamos	ubfx	\_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
2653991a6a4SDimitris Papastamos	/*
2663991a6a4SDimitris Papastamos	 * If the field equals to 1 then branch targets trained in one
2673991a6a4SDimitris Papastamos	 * context cannot affect speculative execution in a different context.
2683991a6a4SDimitris Papastamos	 */
2693991a6a4SDimitris Papastamos	cmp	\_reg, #1
2703991a6a4SDimitris Papastamos	beq	\_label
2713991a6a4SDimitris Papastamos	.endm
272da3b038fSDeepak Pandey
273da3b038fSDeepak Pandey	/*
274da3b038fSDeepak Pandey	 * Helper macro that reads the part number of the current
275da3b038fSDeepak Pandey	 * CPU and jumps to the given label if it matches the CPU
276da3b038fSDeepak Pandey	 * MIDR provided.
277da3b038fSDeepak Pandey	 *
278da3b038fSDeepak Pandey	 * Clobbers x0.
279da3b038fSDeepak Pandey	 */
280da3b038fSDeepak Pandey	.macro  jump_if_cpu_midr _cpu_midr, _label
281da3b038fSDeepak Pandey	mrs	x0, midr_el1
282da3b038fSDeepak Pandey	ubfx	x0, x0, MIDR_PN_SHIFT, #12
283da3b038fSDeepak Pandey	cmp	w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
284da3b038fSDeepak Pandey	b.eq	\_label
285da3b038fSDeepak Pandey	.endm
286c3cf06f1SAntonio Nino Diaz
287c3cf06f1SAntonio Nino Diaz#endif /* CPU_MACROS_S */
288