blob: 6fecb40f4f86cca6f2e2b5fb0fdfbbe35aa75fcd [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Boyan Karatoteve7d7c272023-01-25 16:55:18 +00002 * Copyright (c) 2014-2023, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00006#ifndef CPU_MACROS_S
7#define CPU_MACROS_S
Achin Gupta4f6ad662013-10-25 09:08:21 +01008
Antonio Nino Diaza9044872019-02-12 11:25:02 +00009#include <assert_macros.S>
Boyan Karatoteve7d7c272023-01-25 16:55:18 +000010#include <lib/cpus/cpu_ops.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <lib/cpus/errata_report.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010012
Soby Mathewc704cbc2014-08-14 11:33:56 +010013 /*
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000014 * Write given expressions as quad words
15 *
16 * _count:
17 * Write at least _count quad words. If the given number of
18 * expressions is less than _count, repeat the last expression to
19 * fill _count quad words in total
20 * _rest:
21 * Optional list of expressions. _this is for parameter extraction
22 * only, and has no significance to the caller
23 *
24 * Invoked as:
25 * fill_constants 2, foo, bar, blah, ...
Achin Gupta4f6ad662013-10-25 09:08:21 +010026 */
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000027 .macro fill_constants _count:req, _this, _rest:vararg
28 .ifgt \_count
29 /* Write the current expression */
30 .ifb \_this
31 .error "Nothing to fill"
32 .endif
33 .quad \_this
34
35 /* Invoke recursively for remaining expressions */
36 .ifnb \_rest
37 fill_constants \_count-1, \_rest
38 .else
39 fill_constants \_count-1, \_this
40 .endif
41 .endif
42 .endm
43
44 /*
45 * Declare CPU operations
46 *
47 * _name:
48 * Name of the CPU for which operations are being specified
49 * _midr:
50 * Numeric value expected to read from CPU's MIDR
51 * _resetfunc:
52 * Reset function for the CPU. If there's no CPU reset function,
53 * specify CPU_NO_RESET_FUNC
Dimitris Papastamos914757c2018-03-12 14:47:09 +000054 * _extra1:
55 * This is a placeholder for future per CPU operations. Currently,
56 * some CPUs use this entry to set a test function to determine if
57 * the workaround for CVE-2017-5715 needs to be applied or not.
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +010058 * _extra2:
Bipin Ravicaa2e052022-02-23 23:45:50 -060059 * This is a placeholder for future per CPU operations. Currently
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +010060 * some CPUs use this entry to set a function to disable the
61 * workaround for CVE-2018-3639.
Bipin Ravicaa2e052022-02-23 23:45:50 -060062 * _extra3:
63 * This is a placeholder for future per CPU operations. Currently,
64 * some CPUs use this entry to set a test function to determine if
65 * the workaround for CVE-2022-23960 needs to be applied or not.
laurenw-arm94accd32019-08-20 15:51:24 -050066 * _e_handler:
67 * This is a placeholder for future per CPU exception handlers.
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000068 * _power_down_ops:
69 * Comma-separated list of functions to perform power-down
70 * operatios on the CPU. At least one, and up to
71 * CPU_MAX_PWR_DWN_OPS number of functions may be specified.
72 * Starting at power level 0, these functions shall handle power
73 * down at subsequent power levels. If there aren't exactly
74 * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
75 * used to handle power down at subsequent levels
76 */
Dimitris Papastamos914757c2018-03-12 14:47:09 +000077 .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
Bipin Ravicaa2e052022-02-23 23:45:50 -060078 _extra1:req, _extra2:req, _extra3:req, _e_handler:req, _power_down_ops:vararg
Chris Kay33bfc5e2023-02-14 11:30:04 +000079 .section .cpu_ops, "a"
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000080 .align 3
Soby Mathewc704cbc2014-08-14 11:33:56 +010081 .type cpu_ops_\_name, %object
82 .quad \_midr
Roberto Vargase0e99462017-10-30 14:43:43 +000083#if defined(IMAGE_AT_EL3)
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000084 .quad \_resetfunc
Soby Mathewc704cbc2014-08-14 11:33:56 +010085#endif
Dimitris Papastamos914757c2018-03-12 14:47:09 +000086 .quad \_extra1
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +010087 .quad \_extra2
Bipin Ravicaa2e052022-02-23 23:45:50 -060088 .quad \_extra3
laurenw-arm94accd32019-08-20 15:51:24 -050089 .quad \_e_handler
Masahiro Yamada441bfdd2016-12-25 23:36:24 +090090#ifdef IMAGE_BL31
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000091 /* Insert list of functions */
92 fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
Soby Mathew8e2f2872014-08-14 12:49:05 +010093#endif
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000094
95#if REPORT_ERRATA
96 .ifndef \_name\()_cpu_str
97 /*
98 * Place errata reported flag, and the spinlock to arbitrate access to
99 * it in the data section.
100 */
101 .pushsection .data
102 define_asm_spinlock \_name\()_errata_lock
103 \_name\()_errata_reported:
104 .word 0
105 .popsection
106
107 /* Place CPU string in rodata */
108 .pushsection .rodata
109 \_name\()_cpu_str:
110 .asciz "\_name"
111 .popsection
112 .endif
113
114 /*
Soby Mathew0980dce2018-09-17 04:34:35 +0100115 * Mandatory errata status printing function for CPUs of
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000116 * this class.
117 */
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000118 .quad \_name\()_errata_report
119
120#ifdef IMAGE_BL31
121 /* Pointers to errata lock and reported flag */
122 .quad \_name\()_errata_lock
123 .quad \_name\()_errata_reported
124#endif
125#endif
126
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900127#if defined(IMAGE_BL31) && CRASH_REPORTING
Soby Mathew38b4bc92014-08-14 13:36:41 +0100128 .quad \_name\()_cpu_reg_dump
129#endif
Soby Mathewc704cbc2014-08-14 11:33:56 +0100130 .endm
Dan Handleyea596682015-04-01 17:34:24 +0100131
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000132 .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
133 _power_down_ops:vararg
Bipin Ravicaa2e052022-02-23 23:45:50 -0600134 declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, 0, 0, \
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000135 \_power_down_ops
136 .endm
137
laurenw-arm94accd32019-08-20 15:51:24 -0500138 .macro declare_cpu_ops_eh _name:req, _midr:req, _resetfunc:req, \
139 _e_handler:req, _power_down_ops:vararg
140 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
Bipin Ravicaa2e052022-02-23 23:45:50 -0600141 0, 0, 0, \_e_handler, \_power_down_ops
laurenw-arm94accd32019-08-20 15:51:24 -0500142 .endm
143
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100144 .macro declare_cpu_ops_wa _name:req, _midr:req, \
145 _resetfunc:req, _extra1:req, _extra2:req, \
Bipin Ravicaa2e052022-02-23 23:45:50 -0600146 _extra3:req, _power_down_ops:vararg
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000147 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
Bipin Ravicaa2e052022-02-23 23:45:50 -0600148 \_extra1, \_extra2, \_extra3, 0, \_power_down_ops
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000149 .endm
150
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000151#if REPORT_ERRATA
152 /*
153 * Print status of a CPU errata
154 *
155 * _chosen:
156 * Identifier indicating whether or not a CPU errata has been
157 * compiled in.
158 * _cpu:
159 * Name of the CPU
160 * _id:
161 * Errata identifier
162 * _rev_var:
163 * Register containing the combined value CPU revision and variant
164 * - typically the return value of cpu_get_rev_var
165 */
166 .macro report_errata _chosen, _cpu, _id, _rev_var=x8
167 /* Stash a string with errata ID */
168 .pushsection .rodata
169 \_cpu\()_errata_\_id\()_str:
170 .asciz "\_id"
171 .popsection
172
173 /* Check whether errata applies */
174 mov x0, \_rev_var
Jonathan Wrightefb1f332018-03-28 15:52:03 +0100175 /* Shall clobber: x0-x7 */
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000176 bl check_errata_\_id
177
178 .ifeq \_chosen
179 /*
180 * Errata workaround has not been compiled in. If the errata would have
181 * applied had it been compiled in, print its status as missing.
182 */
183 cbz x0, 900f
184 mov x0, #ERRATA_MISSING
185 .endif
186900:
187 adr x1, \_cpu\()_cpu_str
188 adr x2, \_cpu\()_errata_\_id\()_str
189 bl errata_print_msg
190 .endm
191#endif
192
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000193 /*
194 * This macro is used on some CPUs to detect if they are vulnerable
195 * to CVE-2017-5715.
196 */
197 .macro cpu_check_csv2 _reg _label
198 mrs \_reg, id_aa64pfr0_el1
199 ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
200 /*
Antonio Nino Diaza9044872019-02-12 11:25:02 +0000201 * If the field equals 1, branch targets trained in one context cannot
202 * affect speculative execution in a different context.
203 *
204 * If the field equals 2, it means that the system is also aware of
205 * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
206 * expect users of the registers to do the right thing.
207 *
208 * Only apply mitigations if the value of this field is 0.
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000209 */
Antonio Nino Diaza9044872019-02-12 11:25:02 +0000210#if ENABLE_ASSERTIONS
211 cmp \_reg, #3 /* Only values 0 to 2 are expected */
212 ASM_ASSERT(lo)
213#endif
214
215 cmp \_reg, #0
216 bne \_label
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000217 .endm
Deepak Pandeyb5615362018-10-11 13:44:43 +0530218
219 /*
220 * Helper macro that reads the part number of the current
221 * CPU and jumps to the given label if it matches the CPU
222 * MIDR provided.
223 *
224 * Clobbers x0.
225 */
226 .macro jump_if_cpu_midr _cpu_midr, _label
227 mrs x0, midr_el1
228 ubfx x0, x0, MIDR_PN_SHIFT, #12
229 cmp w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
230 b.eq \_label
231 .endm
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000232
233#endif /* CPU_MACROS_S */