blob: 167cab3d059d945a1cda194a410f9adc4006cbbf [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Jayanth Dodderi Chidanandd45aac62023-06-19 16:20:02 +01002 * Copyright (c) 2014-2023, Arm Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
Antonio Nino Diazc3cf06f2018-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 Diazff6f62e2019-02-12 11:25:02 +00009#include <assert_macros.S>
Boyan Karatotev49d12002023-01-25 16:55:18 +000010#include <lib/cpus/cpu_ops.h>
Boyan Karatotevf8b21662023-01-27 09:37:07 +000011#include <lib/cpus/errata.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010012
Soby Mathew9b476842014-08-14 11:33:56 +010013 /*
Jeenu Viswambharan5dd9dbb2016-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, ...
Soby Mathew9b476842014-08-14 11:33:56 +010026 */
Jeenu Viswambharan5dd9dbb2016-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 Papastamosa205a562018-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 Papastamosfe007b22018-05-16 11:36:14 +010058 * _extra2:
Bipin Ravi9b2510b2022-02-23 23:45:50 -060059 * This is a placeholder for future per CPU operations. Currently
Dimitris Papastamosfe007b22018-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 Ravi9b2510b2022-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.
Arvind Ram Prakash8fc0fa72024-09-16 16:57:33 -050066 * _extra4:
67 * This is a placeholder for future per CPU operations. Currently,
68 * some CPUs use this entry to set a test function to determine if
69 * the workaround for CVE-2024-7881 needs to be applied or not.
laurenw-arm80942622019-08-20 15:51:24 -050070 * _e_handler:
71 * This is a placeholder for future per CPU exception handlers.
Jeenu Viswambharan5dd9dbb2016-11-18 12:58:28 +000072 * _power_down_ops:
73 * Comma-separated list of functions to perform power-down
74 * operatios on the CPU. At least one, and up to
75 * CPU_MAX_PWR_DWN_OPS number of functions may be specified.
76 * Starting at power level 0, these functions shall handle power
77 * down at subsequent power levels. If there aren't exactly
78 * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
79 * used to handle power down at subsequent levels
80 */
Dimitris Papastamosa205a562018-03-12 14:47:09 +000081 .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
Arvind Ram Prakash8fc0fa72024-09-16 16:57:33 -050082 _extra1:req, _extra2:req, _extra3:req, _extra4:req, \
83 _e_handler:req, _power_down_ops:vararg
Chris Kaycf5e7d82025-02-03 17:37:06 +010084 .section .cpu_ops, "a"
Jeenu Viswambharan5dd9dbb2016-11-18 12:58:28 +000085 .align 3
Soby Mathew9b476842014-08-14 11:33:56 +010086 .type cpu_ops_\_name, %object
87 .quad \_midr
Roberto Vargasb1d27b42017-10-30 14:43:43 +000088#if defined(IMAGE_AT_EL3)
Jeenu Viswambharan5dd9dbb2016-11-18 12:58:28 +000089 .quad \_resetfunc
Soby Mathew9b476842014-08-14 11:33:56 +010090#endif
Dimitris Papastamosa205a562018-03-12 14:47:09 +000091 .quad \_extra1
Dimitris Papastamosfe007b22018-05-16 11:36:14 +010092 .quad \_extra2
Bipin Ravi9b2510b2022-02-23 23:45:50 -060093 .quad \_extra3
Arvind Ram Prakash8fc0fa72024-09-16 16:57:33 -050094 .quad \_extra4
laurenw-arm80942622019-08-20 15:51:24 -050095 .quad \_e_handler
Masahiro Yamada3d8256b2016-12-25 23:36:24 +090096#ifdef IMAGE_BL31
Jeenu Viswambharan5dd9dbb2016-11-18 12:58:28 +000097 /* Insert list of functions */
98 fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
Soby Mathewadd40352014-08-14 12:49:05 +010099#endif
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000100 /*
101 * It is possible (although unlikely) that a cpu may have no errata in
102 * code. In that case the start label will not be defined. The list is
103 * intended to be used in a loop, so define it as zero-length for
104 * predictable behaviour. Since this macro is always called at the end
105 * of the cpu file (after all errata have been parsed) we can be sure
106 * that we are at the end of the list. Some cpus call declare_cpu_ops
107 * twice, so only do this once.
108 */
109 .pushsection .rodata.errata_entries
110 .ifndef \_name\()_errata_list_start
111 \_name\()_errata_list_start:
112 .endif
113 .ifndef \_name\()_errata_list_end
114 \_name\()_errata_list_end:
115 .endif
116 .popsection
117
118 /* and now put them in cpu_ops */
119 .quad \_name\()_errata_list_start
120 .quad \_name\()_errata_list_end
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000121
122#if REPORT_ERRATA
123 .ifndef \_name\()_cpu_str
124 /*
125 * Place errata reported flag, and the spinlock to arbitrate access to
126 * it in the data section.
127 */
128 .pushsection .data
129 define_asm_spinlock \_name\()_errata_lock
130 \_name\()_errata_reported:
131 .word 0
132 .popsection
133
134 /* Place CPU string in rodata */
135 .pushsection .rodata
136 \_name\()_cpu_str:
137 .asciz "\_name"
138 .popsection
139 .endif
140
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000141
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000142 /*
Soby Mathew12af5ed2018-09-17 04:34:35 +0100143 * Mandatory errata status printing function for CPUs of
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000144 * this class.
145 */
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000146 .quad \_name\()_errata_report
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000147 .quad \_name\()_cpu_str
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000148
149#ifdef IMAGE_BL31
150 /* Pointers to errata lock and reported flag */
151 .quad \_name\()_errata_lock
152 .quad \_name\()_errata_reported
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000153#endif /* IMAGE_BL31 */
154#endif /* REPORT_ERRATA */
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000155
Masahiro Yamada3d8256b2016-12-25 23:36:24 +0900156#if defined(IMAGE_BL31) && CRASH_REPORTING
Soby Mathewd3f70af2014-08-14 13:36:41 +0100157 .quad \_name\()_cpu_reg_dump
158#endif
Soby Mathew9b476842014-08-14 11:33:56 +0100159 .endm
Dan Handleye2bf57f2015-04-01 17:34:24 +0100160
Dimitris Papastamosa205a562018-03-12 14:47:09 +0000161 .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
162 _power_down_ops:vararg
Arvind Ram Prakash8fc0fa72024-09-16 16:57:33 -0500163 declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, 0, 0, 0, \
Dimitris Papastamosa205a562018-03-12 14:47:09 +0000164 \_power_down_ops
165 .endm
166
laurenw-arm80942622019-08-20 15:51:24 -0500167 .macro declare_cpu_ops_eh _name:req, _midr:req, _resetfunc:req, \
168 _e_handler:req, _power_down_ops:vararg
169 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
Arvind Ram Prakash8fc0fa72024-09-16 16:57:33 -0500170 0, 0, 0, 0, \_e_handler, \_power_down_ops
laurenw-arm80942622019-08-20 15:51:24 -0500171 .endm
172
Dimitris Papastamosfe007b22018-05-16 11:36:14 +0100173 .macro declare_cpu_ops_wa _name:req, _midr:req, \
174 _resetfunc:req, _extra1:req, _extra2:req, \
Bipin Ravi9b2510b2022-02-23 23:45:50 -0600175 _extra3:req, _power_down_ops:vararg
Dimitris Papastamosa205a562018-03-12 14:47:09 +0000176 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
Arvind Ram Prakash8fc0fa72024-09-16 16:57:33 -0500177 \_extra1, \_extra2, \_extra3, 0, 0, \_power_down_ops
178 .endm
179
180 .macro declare_cpu_ops_wa_4 _name:req, _midr:req, \
181 _resetfunc:req, _extra1:req, _extra2:req, \
182 _extra3:req, _extra4:req, _power_down_ops:vararg
183 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
184 \_extra1, \_extra2, \_extra3, \_extra4, 0, \_power_down_ops
Dimitris Papastamosa205a562018-03-12 14:47:09 +0000185 .endm
186
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000187/* TODO can be deleted once all CPUs have been converted */
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000188#if REPORT_ERRATA
189 /*
190 * Print status of a CPU errata
191 *
192 * _chosen:
193 * Identifier indicating whether or not a CPU errata has been
194 * compiled in.
195 * _cpu:
196 * Name of the CPU
197 * _id:
198 * Errata identifier
199 * _rev_var:
200 * Register containing the combined value CPU revision and variant
201 * - typically the return value of cpu_get_rev_var
202 */
203 .macro report_errata _chosen, _cpu, _id, _rev_var=x8
204 /* Stash a string with errata ID */
205 .pushsection .rodata
206 \_cpu\()_errata_\_id\()_str:
207 .asciz "\_id"
208 .popsection
209
210 /* Check whether errata applies */
211 mov x0, \_rev_var
Jonathan Wright9ec39212018-03-28 15:52:03 +0100212 /* Shall clobber: x0-x7 */
Jeenu Viswambharan10bcd762017-01-03 11:01:51 +0000213 bl check_errata_\_id
214
215 .ifeq \_chosen
216 /*
217 * Errata workaround has not been compiled in. If the errata would have
218 * applied had it been compiled in, print its status as missing.
219 */
220 cbz x0, 900f
221 mov x0, #ERRATA_MISSING
222 .endif
223900:
224 adr x1, \_cpu\()_cpu_str
225 adr x2, \_cpu\()_errata_\_id\()_str
226 bl errata_print_msg
227 .endm
228#endif
229
Dimitris Papastamos3991a6a2018-03-12 13:27:02 +0000230 /*
231 * This macro is used on some CPUs to detect if they are vulnerable
232 * to CVE-2017-5715.
233 */
234 .macro cpu_check_csv2 _reg _label
235 mrs \_reg, id_aa64pfr0_el1
236 ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
237 /*
Antonio Nino Diazff6f62e2019-02-12 11:25:02 +0000238 * If the field equals 1, branch targets trained in one context cannot
239 * affect speculative execution in a different context.
240 *
241 * If the field equals 2, it means that the system is also aware of
242 * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
243 * expect users of the registers to do the right thing.
244 *
245 * Only apply mitigations if the value of this field is 0.
Dimitris Papastamos3991a6a2018-03-12 13:27:02 +0000246 */
Antonio Nino Diazff6f62e2019-02-12 11:25:02 +0000247#if ENABLE_ASSERTIONS
248 cmp \_reg, #3 /* Only values 0 to 2 are expected */
249 ASM_ASSERT(lo)
250#endif
251
252 cmp \_reg, #0
253 bne \_label
Dimitris Papastamos3991a6a2018-03-12 13:27:02 +0000254 .endm
Deepak Pandeyda3b0382018-10-11 13:44:43 +0530255
256 /*
257 * Helper macro that reads the part number of the current
258 * CPU and jumps to the given label if it matches the CPU
259 * MIDR provided.
260 *
261 * Clobbers x0.
262 */
263 .macro jump_if_cpu_midr _cpu_midr, _label
264 mrs x0, midr_el1
265 ubfx x0, x0, MIDR_PN_SHIFT, #12
266 cmp w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
267 b.eq \_label
268 .endm
Antonio Nino Diazc3cf06f2018-11-08 10:20:19 +0000269
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000270
271/*
272 * Workaround wrappers for errata that apply at reset or runtime. Reset errata
273 * will be applied automatically
274 *
275 * _cpu:
276 * Name of cpu as given to declare_cpu_ops
277 *
278 * _cve:
279 * Whether erratum is a CVE. CVE year if yes, 0 otherwise
280 *
281 * _id:
282 * Erratum or CVE number. Please combine with previous field with ERRATUM
283 * or CVE macros
284 *
285 * _chosen:
286 * Compile time flag on whether the erratum is included
287 *
288 * _apply_at_reset:
289 * Whether the erratum should be automatically applied at reset
290 */
291.macro add_erratum_entry _cpu:req, _cve:req, _id:req, _chosen:req, _apply_at_reset:req
292 .pushsection .rodata.errata_entries
293 .align 3
294 .ifndef \_cpu\()_errata_list_start
295 \_cpu\()_errata_list_start:
296 .endif
297
298 /* check if unused and compile out if no references */
299 .if \_apply_at_reset && \_chosen
300 .quad erratum_\_cpu\()_\_id\()_wa
301 .else
302 .quad 0
303 .endif
304 /* TODO(errata ABI): this prevents all checker functions from
305 * being optimised away. Can be done away with unless the ABI
306 * needs them */
307 .quad check_erratum_\_cpu\()_\_id
308 /* Will fit CVEs with up to 10 character in the ID field */
309 .word \_id
310 .hword \_cve
311 .byte \_chosen
312 /* TODO(errata ABI): mitigated field for known but unmitigated
313 * errata */
314 .byte 0x1
315 .popsection
316.endm
317
318.macro _workaround_start _cpu:req, _cve:req, _id:req, _chosen:req, _apply_at_reset:req
319 add_erratum_entry \_cpu, \_cve, \_id, \_chosen, \_apply_at_reset
320
321 func erratum_\_cpu\()_\_id\()_wa
322 mov x8, x30
323
324 /* save rev_var for workarounds that might need it but don't
325 * restore to x0 because few will care */
326 mov x7, x0
327 bl check_erratum_\_cpu\()_\_id
328 cbz x0, erratum_\_cpu\()_\_id\()_skip
329.endm
330
331.macro _workaround_end _cpu:req, _id:req
332 erratum_\_cpu\()_\_id\()_skip:
333 ret x8
334 endfunc erratum_\_cpu\()_\_id\()_wa
335.endm
336
337/*******************************************************************************
338 * Errata workaround wrappers
339 ******************************************************************************/
340/*
341 * Workaround wrappers for errata that apply at reset or runtime. Reset errata
342 * will be applied automatically
343 *
344 * _cpu:
345 * Name of cpu as given to declare_cpu_ops
346 *
347 * _cve:
348 * Whether erratum is a CVE. CVE year if yes, 0 otherwise
349 *
350 * _id:
351 * Erratum or CVE number. Please combine with previous field with ERRATUM
352 * or CVE macros
353 *
354 * _chosen:
355 * Compile time flag on whether the erratum is included
356 *
357 * in body:
358 * clobber x0 to x7 (please only use those)
359 * argument x7 - cpu_rev_var
360 *
361 * _wa clobbers: x0-x8 (PCS compliant)
362 */
363.macro workaround_reset_start _cpu:req, _cve:req, _id:req, _chosen:req
364 _workaround_start \_cpu, \_cve, \_id, \_chosen, 1
365.endm
366
367/*
368 * See `workaround_reset_start` for usage info. Additional arguments:
369 *
370 * _midr:
371 * Check if CPU's MIDR matches the CPU it's meant for. Must be specified
372 * for errata applied in generic code
373 */
374.macro workaround_runtime_start _cpu:req, _cve:req, _id:req, _chosen:req, _midr
375 /*
376 * Let errata specify if they need MIDR checking. Sadly, storing the
377 * MIDR in an .equ to retrieve automatically blows up as it stores some
378 * brackets in the symbol
379 */
380 .ifnb \_midr
381 jump_if_cpu_midr \_midr, 1f
382 b erratum_\_cpu\()_\_id\()_skip
383
384 1:
385 .endif
386 _workaround_start \_cpu, \_cve, \_id, \_chosen, 0
387.endm
388
389/*
390 * Usage and arguments identical to `workaround_reset_start`. The _cve argument
391 * is kept here so the same #define can be used as that macro
392 */
393.macro workaround_reset_end _cpu:req, _cve:req, _id:req
394 _workaround_end \_cpu, \_id
395.endm
396
397/*
398 * See `workaround_reset_start` for usage info. The _cve argument is kept here
399 * so the same #define can be used as that macro. Additional arguments:
400 *
401 * _no_isb:
402 * Optionally do not include the trailing isb. Please disable with the
403 * NO_ISB macro
404 */
405.macro workaround_runtime_end _cpu:req, _cve:req, _id:req, _no_isb
406 /*
407 * Runtime errata do not have a reset function to call the isb for them
408 * and missing the isb could be very problematic. It is also likely as
409 * they tend to be scattered in generic code.
410 */
411 .ifb \_no_isb
412 isb
413 .endif
414 _workaround_end \_cpu, \_id
415.endm
416
417/*******************************************************************************
418 * Errata workaround helpers
419 ******************************************************************************/
420/*
421 * Set a bit in a system register. Can set multiple bits but is limited by the
422 * way the ORR instruction encodes them.
423 *
424 * _reg:
425 * Register to write to
426 *
427 * _bit:
428 * Bit to set. Please use a descriptive #define
429 *
430 * _assert:
431 * Optionally whether to read back and assert that the bit has been
432 * written. Please disable with NO_ASSERT macro
433 *
434 * clobbers: x1
435 */
436.macro sysreg_bit_set _reg:req, _bit:req, _assert=1
437 mrs x1, \_reg
438 orr x1, x1, #\_bit
439 msr \_reg, x1
440.endm
441
442/*
Jayanth Dodderi Chidanandd45aac62023-06-19 16:20:02 +0100443 * Clear a bit in a system register. Can clear multiple bits but is limited by
444 * the way the BIC instrucion encodes them.
445 *
446 * see sysreg_bit_set for usage
447 */
448.macro sysreg_bit_clear _reg:req, _bit:req
449 mrs x1, \_reg
450 bic x1, x1, #\_bit
451 msr \_reg, x1
452.endm
453
454.macro override_vector_table _table:req
455 adr x1, \_table
456 msr vbar_el3, x1
457.endm
458
459/*
460 * BFI : Inserts bitfield into a system register.
461 *
462 * BFI{cond} Rd, Rn, #lsb, #width
463 */
464.macro sysreg_bitfield_insert _reg:req, _src:req, _lsb:req, _width:req
465 /* Source value for BFI */
466 mov x1, #\_src
467 mrs x0, \_reg
468 bfi x0, x1, #\_lsb, #\_width
469 msr \_reg, x0
470.endm
471
472/*
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000473 * Apply erratum
474 *
475 * _cpu:
476 * Name of cpu as given to declare_cpu_ops
477 *
478 * _cve:
479 * Whether erratum is a CVE. CVE year if yes, 0 otherwise
480 *
481 * _id:
482 * Erratum or CVE number. Please combine with previous field with ERRATUM
483 * or CVE macros
484 *
485 * _chosen:
486 * Compile time flag on whether the erratum is included
487 *
Harrison Mutai2c3d9c92023-06-26 16:25:21 +0100488 * _get_rev:
489 * Optional parameter that determines whether to insert a call to the CPU revision fetching
Boyan Karatotev9f342212024-09-26 17:09:53 +0100490 * procedure. Stores the result of this in the temporary register x10 to allow for chaining
Harrison Mutai2c3d9c92023-06-26 16:25:21 +0100491 *
492 * clobbers: x0-x10 (PCS compliant)
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000493 */
Harrison Mutai2c3d9c92023-06-26 16:25:21 +0100494.macro apply_erratum _cpu:req, _cve:req, _id:req, _chosen:req, _get_rev=GET_CPU_REV
495 .if (\_chosen & \_get_rev)
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000496 mov x9, x30
497 bl cpu_get_rev_var
Harrison Mutai2c3d9c92023-06-26 16:25:21 +0100498 mov x10, x0
499 .elseif (\_chosen)
500 mov x9, x30
501 mov x0, x10
502 .endif
503
504 .if \_chosen
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000505 bl erratum_\_cpu\()_\_id\()_wa
506 mov x30, x9
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000507 .endif
508.endm
509
510/*
Boyan Karatotevf67c3ea2025-01-21 08:44:52 +0000511 * Helpers to report if an erratum applies. Compares the given revision variant
512 * to the given value. Return ERRATA_APPLIES or ERRATA_NOT_APPLIES accordingly.
513 *
514 * _rev_num: the given revision variant. Or
515 * _rev_num_lo,_rev_num_hi: the lower and upper bounds of the revision variant
516 *
517 * in body:
518 * clobber: x0
519 * argument: x0 - cpu_rev_var
520 */
521.macro cpu_rev_var_ls _rev_num:req
522 cmp x0, #\_rev_num
523 cset x0, ls
524.endm
525
526.macro cpu_rev_var_hs _rev_num:req
527 cmp x0, #\_rev_num
528 cset x0, hs
529.endm
530
531.macro cpu_rev_var_range _rev_num_lo:req, _rev_num_hi:req
532 cmp x0, #\_rev_num_lo
533 mov x1, #\_rev_num_hi
534 ccmp x0, x1, #2, hs
535 cset x0, ls
536.endm
537
538/*
539 * Helpers to select which revisions errata apply to.
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000540 *
541 * _cpu:
542 * Name of cpu as given to declare_cpu_ops
543 *
544 * _cve:
545 * Whether erratum is a CVE. CVE year if yes, 0 otherwise
546 *
547 * _id:
548 * Erratum or CVE number. Please combine with previous field with ERRATUM
549 * or CVE macros
550 *
551 * _rev_num:
552 * Revision to apply to
553 *
554 * in body:
Boyan Karatotevf67c3ea2025-01-21 08:44:52 +0000555 * clobber: x0 to x1
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000556 * argument: x0 - cpu_rev_var
557 */
558.macro check_erratum_ls _cpu:req, _cve:req, _id:req, _rev_num:req
559 func check_erratum_\_cpu\()_\_id
Boyan Karatotevf67c3ea2025-01-21 08:44:52 +0000560 cpu_rev_var_ls \_rev_num
561 ret
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000562 endfunc check_erratum_\_cpu\()_\_id
563.endm
564
565.macro check_erratum_hs _cpu:req, _cve:req, _id:req, _rev_num:req
566 func check_erratum_\_cpu\()_\_id
Boyan Karatotevf67c3ea2025-01-21 08:44:52 +0000567 cpu_rev_var_hs \_rev_num
568 ret
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000569 endfunc check_erratum_\_cpu\()_\_id
570.endm
571
572.macro check_erratum_range _cpu:req, _cve:req, _id:req, _rev_num_lo:req, _rev_num_hi:req
573 func check_erratum_\_cpu\()_\_id
Boyan Karatotevf67c3ea2025-01-21 08:44:52 +0000574 cpu_rev_var_range \_rev_num_lo, \_rev_num_hi
575 ret
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000576 endfunc check_erratum_\_cpu\()_\_id
577.endm
578
Boyan Karatotevea077922023-04-04 11:29:00 +0100579.macro check_erratum_chosen _cpu:req, _cve:req, _id:req, _chosen:req
580 func check_erratum_\_cpu\()_\_id
581 .if \_chosen
582 mov x0, #ERRATA_APPLIES
583 .else
584 mov x0, #ERRATA_MISSING
585 .endif
586 ret
587 endfunc check_erratum_\_cpu\()_\_id
588.endm
589
Boyan Karatotevf67c3ea2025-01-21 08:44:52 +0000590/*
591 * provide a shorthand for the name format for annoying errata
592 * body: clobber x0 to x3
593 */
Boyan Karatotevea077922023-04-04 11:29:00 +0100594.macro check_erratum_custom_start _cpu:req, _cve:req, _id:req
595 func check_erratum_\_cpu\()_\_id
596.endm
597
598.macro check_erratum_custom_end _cpu:req, _cve:req, _id:req
599 endfunc check_erratum_\_cpu\()_\_id
600.endm
601
602
Boyan Karatotev0fe4e972023-01-27 09:35:10 +0000603/*******************************************************************************
604 * CPU reset function wrapper
605 ******************************************************************************/
606
607/*
608 * Wrapper to automatically apply all reset-time errata. Will end with an isb.
609 *
610 * _cpu:
611 * Name of cpu as given to declare_cpu_ops
612 *
613 * in body:
614 * clobber x8 to x14
615 * argument x14 - cpu_rev_var
616 */
617.macro cpu_reset_func_start _cpu:req
618 func \_cpu\()_reset_func
619 mov x15, x30
620 bl cpu_get_rev_var
621 mov x14, x0
622
623 /* short circuit the location to avoid searching the list */
624 adrp x12, \_cpu\()_errata_list_start
625 add x12, x12, :lo12:\_cpu\()_errata_list_start
626 adrp x13, \_cpu\()_errata_list_end
627 add x13, x13, :lo12:\_cpu\()_errata_list_end
628
629 errata_begin:
630 /* if head catches up with end of list, exit */
631 cmp x12, x13
632 b.eq errata_end
633
634 ldr x10, [x12, #ERRATUM_WA_FUNC]
635 /* TODO(errata ABI): check mitigated and checker function fields
636 * for 0 */
637 ldrb w11, [x12, #ERRATUM_CHOSEN]
638
639 /* skip if not chosen */
640 cbz x11, 1f
641 /* skip if runtime erratum */
642 cbz x10, 1f
643
644 /* put cpu revision in x0 and call workaround */
645 mov x0, x14
646 blr x10
647 1:
648 add x12, x12, #ERRATUM_ENTRY_SIZE
649 b errata_begin
650 errata_end:
651.endm
652
653.macro cpu_reset_func_end _cpu:req
654 isb
655 ret x15
656 endfunc \_cpu\()_reset_func
657.endm
Boyan Karatotev58c9e892023-01-27 09:38:15 +0000658
659/*
660 * Maintain compatibility with the old scheme of each cpu has its own reporting.
661 * TODO remove entirely once all cpus have been converted. This includes the
662 * cpu_ops entry, as print_errata_status can call this directly for all cpus
663 */
664.macro errata_report_shim _cpu:req
665 #if REPORT_ERRATA
666 func \_cpu\()_errata_report
667 /* normal stack frame for pretty debugging */
668 stp x29, x30, [sp, #-16]!
669 mov x29, sp
670
671 bl generic_errata_report
672
673 ldp x29, x30, [sp], #16
674 ret
675 endfunc \_cpu\()_errata_report
676 #endif
677.endm
Antonio Nino Diazc3cf06f2018-11-08 10:20:19 +0000678#endif /* CPU_MACROS_S */