blob: 044aacaf41fa771be3a4c79a8f6cec3d88f9277f [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Antonio Nino Diaza9044872019-02-12 11:25:02 +00002 * Copyright (c) 2014-2019, 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
9#include <arch.h>
Antonio Nino Diaza9044872019-02-12 11:25:02 +000010#include <assert_macros.S>
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#define CPU_IMPL_PN_MASK (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \
14 (MIDR_PN_MASK << MIDR_PN_SHIFT)
Achin Gupta4f6ad662013-10-25 09:08:21 +010015
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000016/* The number of CPU operations allowed */
17#define CPU_MAX_PWR_DWN_OPS 2
18
19/* Special constant to specify that CPU has no reset function */
20#define CPU_NO_RESET_FUNC 0
21
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +010022#define CPU_NO_EXTRA1_FUNC 0
23#define CPU_NO_EXTRA2_FUNC 0
24
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000025/* Word size for 64-bit CPUs */
26#define CPU_WORD_SIZE 8
27
Roberto Vargase0e99462017-10-30 14:43:43 +000028#if defined(IMAGE_BL1) || defined(IMAGE_BL31) ||(defined(IMAGE_BL2) && BL2_AT_EL3)
29#define IMAGE_AT_EL3
30#endif
31
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000032/*
33 * Whether errata status needs reporting. Errata status is printed in debug
34 * builds for both BL1 and BL31 images.
35 */
36#if (defined(IMAGE_BL1) || defined(IMAGE_BL31)) && DEBUG
37# define REPORT_ERRATA 1
38#else
39# define REPORT_ERRATA 0
40#endif
41
Roberto Vargas67762d92018-05-01 09:54:54 +010042
43 .equ CPU_MIDR_SIZE, CPU_WORD_SIZE
44 .equ CPU_EXTRA1_FUNC_SIZE, CPU_WORD_SIZE
45 .equ CPU_EXTRA2_FUNC_SIZE, CPU_WORD_SIZE
46 .equ CPU_RESET_FUNC_SIZE, CPU_WORD_SIZE
47 .equ CPU_PWR_DWN_OPS_SIZE, CPU_WORD_SIZE * CPU_MAX_PWR_DWN_OPS
48 .equ CPU_ERRATA_FUNC_SIZE, CPU_WORD_SIZE
49 .equ CPU_ERRATA_LOCK_SIZE, CPU_WORD_SIZE
50 .equ CPU_ERRATA_PRINTED_SIZE, CPU_WORD_SIZE
51 .equ CPU_REG_DUMP_SIZE, CPU_WORD_SIZE
52
53#ifndef IMAGE_AT_EL3
54 .equ CPU_RESET_FUNC_SIZE, 0
Soby Mathewc704cbc2014-08-14 11:33:56 +010055#endif
Roberto Vargas67762d92018-05-01 09:54:54 +010056
57/* The power down core and cluster is needed only in BL31 */
58#ifndef IMAGE_BL31
59 .equ CPU_PWR_DWN_OPS_SIZE, 0
Soby Mathew8e2f2872014-08-14 12:49:05 +010060#endif
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000061
Roberto Vargas67762d92018-05-01 09:54:54 +010062/* Fields required to print errata status. */
63#if !REPORT_ERRATA
64 .equ CPU_ERRATA_FUNC_SIZE, 0
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000065#endif
Roberto Vargas67762d92018-05-01 09:54:54 +010066
67/* Only BL31 requieres mutual exclusion and printed flag. */
68#if !(REPORT_ERRATA && defined(IMAGE_BL31))
69 .equ CPU_ERRATA_LOCK_SIZE, 0
70 .equ CPU_ERRATA_PRINTED_SIZE, 0
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000071#endif
72
Roberto Vargas67762d92018-05-01 09:54:54 +010073#if !defined(IMAGE_BL31) || !CRASH_REPORTING
74 .equ CPU_REG_DUMP_SIZE, 0
Soby Mathew38b4bc92014-08-14 13:36:41 +010075#endif
Roberto Vargas67762d92018-05-01 09:54:54 +010076
77/*
78 * Define the offsets to the fields in cpu_ops structure.
79 * Every offset is defined based in the offset and size of the previous
80 * field.
81 */
82 .equ CPU_MIDR, 0
83 .equ CPU_RESET_FUNC, CPU_MIDR + CPU_MIDR_SIZE
84 .equ CPU_EXTRA1_FUNC, CPU_RESET_FUNC + CPU_RESET_FUNC_SIZE
85 .equ CPU_EXTRA2_FUNC, CPU_EXTRA1_FUNC + CPU_EXTRA1_FUNC_SIZE
86 .equ CPU_PWR_DWN_OPS, CPU_EXTRA2_FUNC + CPU_EXTRA2_FUNC_SIZE
87 .equ CPU_ERRATA_FUNC, CPU_PWR_DWN_OPS + CPU_PWR_DWN_OPS_SIZE
88 .equ CPU_ERRATA_LOCK, CPU_ERRATA_FUNC + CPU_ERRATA_FUNC_SIZE
89 .equ CPU_ERRATA_PRINTED, CPU_ERRATA_LOCK + CPU_ERRATA_LOCK_SIZE
90 .equ CPU_REG_DUMP, CPU_ERRATA_PRINTED + CPU_ERRATA_PRINTED_SIZE
91 .equ CPU_OPS_SIZE, CPU_REG_DUMP + CPU_REG_DUMP_SIZE
Achin Gupta4f6ad662013-10-25 09:08:21 +010092
Soby Mathewc704cbc2014-08-14 11:33:56 +010093 /*
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000094 * Write given expressions as quad words
95 *
96 * _count:
97 * Write at least _count quad words. If the given number of
98 * expressions is less than _count, repeat the last expression to
99 * fill _count quad words in total
100 * _rest:
101 * Optional list of expressions. _this is for parameter extraction
102 * only, and has no significance to the caller
103 *
104 * Invoked as:
105 * fill_constants 2, foo, bar, blah, ...
Achin Gupta4f6ad662013-10-25 09:08:21 +0100106 */
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000107 .macro fill_constants _count:req, _this, _rest:vararg
108 .ifgt \_count
109 /* Write the current expression */
110 .ifb \_this
111 .error "Nothing to fill"
112 .endif
113 .quad \_this
114
115 /* Invoke recursively for remaining expressions */
116 .ifnb \_rest
117 fill_constants \_count-1, \_rest
118 .else
119 fill_constants \_count-1, \_this
120 .endif
121 .endif
122 .endm
123
124 /*
125 * Declare CPU operations
126 *
127 * _name:
128 * Name of the CPU for which operations are being specified
129 * _midr:
130 * Numeric value expected to read from CPU's MIDR
131 * _resetfunc:
132 * Reset function for the CPU. If there's no CPU reset function,
133 * specify CPU_NO_RESET_FUNC
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000134 * _extra1:
135 * This is a placeholder for future per CPU operations. Currently,
136 * some CPUs use this entry to set a test function to determine if
137 * the workaround for CVE-2017-5715 needs to be applied or not.
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100138 * _extra2:
139 * This is a placeholder for future per CPU operations. Currently
140 * some CPUs use this entry to set a function to disable the
141 * workaround for CVE-2018-3639.
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000142 * _power_down_ops:
143 * Comma-separated list of functions to perform power-down
144 * operatios on the CPU. At least one, and up to
145 * CPU_MAX_PWR_DWN_OPS number of functions may be specified.
146 * Starting at power level 0, these functions shall handle power
147 * down at subsequent power levels. If there aren't exactly
148 * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
149 * used to handle power down at subsequent levels
150 */
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000151 .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100152 _extra1:req, _extra2:req, _power_down_ops:vararg
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000153 .section cpu_ops, "a"
154 .align 3
Soby Mathewc704cbc2014-08-14 11:33:56 +0100155 .type cpu_ops_\_name, %object
156 .quad \_midr
Roberto Vargase0e99462017-10-30 14:43:43 +0000157#if defined(IMAGE_AT_EL3)
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000158 .quad \_resetfunc
Soby Mathewc704cbc2014-08-14 11:33:56 +0100159#endif
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000160 .quad \_extra1
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100161 .quad \_extra2
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900162#ifdef IMAGE_BL31
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000163 /* Insert list of functions */
164 fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
Soby Mathew8e2f2872014-08-14 12:49:05 +0100165#endif
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000166
167#if REPORT_ERRATA
168 .ifndef \_name\()_cpu_str
169 /*
170 * Place errata reported flag, and the spinlock to arbitrate access to
171 * it in the data section.
172 */
173 .pushsection .data
174 define_asm_spinlock \_name\()_errata_lock
175 \_name\()_errata_reported:
176 .word 0
177 .popsection
178
179 /* Place CPU string in rodata */
180 .pushsection .rodata
181 \_name\()_cpu_str:
182 .asciz "\_name"
183 .popsection
184 .endif
185
186 /*
Soby Mathew0980dce2018-09-17 04:34:35 +0100187 * Mandatory errata status printing function for CPUs of
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000188 * this class.
189 */
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000190 .quad \_name\()_errata_report
191
192#ifdef IMAGE_BL31
193 /* Pointers to errata lock and reported flag */
194 .quad \_name\()_errata_lock
195 .quad \_name\()_errata_reported
196#endif
197#endif
198
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900199#if defined(IMAGE_BL31) && CRASH_REPORTING
Soby Mathew38b4bc92014-08-14 13:36:41 +0100200 .quad \_name\()_cpu_reg_dump
201#endif
Soby Mathewc704cbc2014-08-14 11:33:56 +0100202 .endm
Dan Handleyea596682015-04-01 17:34:24 +0100203
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000204 .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
205 _power_down_ops:vararg
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100206 declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, \
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000207 \_power_down_ops
208 .endm
209
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100210 .macro declare_cpu_ops_wa _name:req, _midr:req, \
211 _resetfunc:req, _extra1:req, _extra2:req, \
212 _power_down_ops:vararg
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000213 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100214 \_extra1, \_extra2, \_power_down_ops
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000215 .endm
216
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000217#if REPORT_ERRATA
218 /*
219 * Print status of a CPU errata
220 *
221 * _chosen:
222 * Identifier indicating whether or not a CPU errata has been
223 * compiled in.
224 * _cpu:
225 * Name of the CPU
226 * _id:
227 * Errata identifier
228 * _rev_var:
229 * Register containing the combined value CPU revision and variant
230 * - typically the return value of cpu_get_rev_var
231 */
232 .macro report_errata _chosen, _cpu, _id, _rev_var=x8
233 /* Stash a string with errata ID */
234 .pushsection .rodata
235 \_cpu\()_errata_\_id\()_str:
236 .asciz "\_id"
237 .popsection
238
239 /* Check whether errata applies */
240 mov x0, \_rev_var
Jonathan Wrightefb1f332018-03-28 15:52:03 +0100241 /* Shall clobber: x0-x7 */
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000242 bl check_errata_\_id
243
244 .ifeq \_chosen
245 /*
246 * Errata workaround has not been compiled in. If the errata would have
247 * applied had it been compiled in, print its status as missing.
248 */
249 cbz x0, 900f
250 mov x0, #ERRATA_MISSING
251 .endif
252900:
253 adr x1, \_cpu\()_cpu_str
254 adr x2, \_cpu\()_errata_\_id\()_str
255 bl errata_print_msg
256 .endm
257#endif
258
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000259 /*
260 * This macro is used on some CPUs to detect if they are vulnerable
261 * to CVE-2017-5715.
262 */
263 .macro cpu_check_csv2 _reg _label
264 mrs \_reg, id_aa64pfr0_el1
265 ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
266 /*
Antonio Nino Diaza9044872019-02-12 11:25:02 +0000267 * If the field equals 1, branch targets trained in one context cannot
268 * affect speculative execution in a different context.
269 *
270 * If the field equals 2, it means that the system is also aware of
271 * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
272 * expect users of the registers to do the right thing.
273 *
274 * Only apply mitigations if the value of this field is 0.
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000275 */
Antonio Nino Diaza9044872019-02-12 11:25:02 +0000276#if ENABLE_ASSERTIONS
277 cmp \_reg, #3 /* Only values 0 to 2 are expected */
278 ASM_ASSERT(lo)
279#endif
280
281 cmp \_reg, #0
282 bne \_label
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000283 .endm
Deepak Pandeyb5615362018-10-11 13:44:43 +0530284
285 /*
286 * Helper macro that reads the part number of the current
287 * CPU and jumps to the given label if it matches the CPU
288 * MIDR provided.
289 *
290 * Clobbers x0.
291 */
292 .macro jump_if_cpu_midr _cpu_midr, _label
293 mrs x0, midr_el1
294 ubfx x0, x0, MIDR_PN_SHIFT, #12
295 cmp w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
296 b.eq \_label
297 .endm
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000298
299#endif /* CPU_MACROS_S */