blob: bfe2449e9a05f04d916c21ae7def2472fc78126e [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dimitris Papastamos914757c2018-03-12 14:47:09 +00002 * Copyright (c) 2014-2018, 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 */
Dan Handleyea596682015-04-01 17:34:24 +01006#ifndef __CPU_MACROS_S__
7#define __CPU_MACROS_S__
Achin Gupta4f6ad662013-10-25 09:08:21 +01008
9#include <arch.h>
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000010#include <errata_report.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010011
Soby Mathewc704cbc2014-08-14 11:33:56 +010012#define CPU_IMPL_PN_MASK (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \
13 (MIDR_PN_MASK << MIDR_PN_SHIFT)
Achin Gupta4f6ad662013-10-25 09:08:21 +010014
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000015/* The number of CPU operations allowed */
16#define CPU_MAX_PWR_DWN_OPS 2
17
18/* Special constant to specify that CPU has no reset function */
19#define CPU_NO_RESET_FUNC 0
20
21/* Word size for 64-bit CPUs */
22#define CPU_WORD_SIZE 8
23
Roberto Vargase0e99462017-10-30 14:43:43 +000024#if defined(IMAGE_BL1) || defined(IMAGE_BL31) ||(defined(IMAGE_BL2) && BL2_AT_EL3)
25#define IMAGE_AT_EL3
26#endif
27
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000028/*
29 * Whether errata status needs reporting. Errata status is printed in debug
30 * builds for both BL1 and BL31 images.
31 */
32#if (defined(IMAGE_BL1) || defined(IMAGE_BL31)) && DEBUG
33# define REPORT_ERRATA 1
34#else
35# define REPORT_ERRATA 0
36#endif
37
Soby Mathewc704cbc2014-08-14 11:33:56 +010038 /*
39 * Define the offsets to the fields in cpu_ops structure.
40 */
41 .struct 0
42CPU_MIDR: /* cpu_ops midr */
43 .space 8
44/* Reset fn is needed in BL at reset vector */
Roberto Vargase0e99462017-10-30 14:43:43 +000045#if defined(IMAGE_AT_EL3)
Soby Mathewc704cbc2014-08-14 11:33:56 +010046CPU_RESET_FUNC: /* cpu_ops reset_func */
47 .space 8
48#endif
Dimitris Papastamos914757c2018-03-12 14:47:09 +000049CPU_EXTRA1_FUNC:
50 .space 8
Masahiro Yamada441bfdd2016-12-25 23:36:24 +090051#ifdef IMAGE_BL31 /* The power down core and cluster is needed only in BL31 */
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000052CPU_PWR_DWN_OPS: /* cpu_ops power down functions */
53 .space (8 * CPU_MAX_PWR_DWN_OPS)
Soby Mathew8e2f2872014-08-14 12:49:05 +010054#endif
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000055
56/*
57 * Fields required to print errata status. Only in BL31 that the printing
58 * require mutual exclusion and printed flag.
59 */
60#if REPORT_ERRATA
61CPU_ERRATA_FUNC:
62 .space 8
Roberto Vargase0e99462017-10-30 14:43:43 +000063#if defined(IMAGE_BL31)
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +000064CPU_ERRATA_LOCK:
65 .space 8
66CPU_ERRATA_PRINTED:
67 .space 8
68#endif
69#endif
70
Masahiro Yamada441bfdd2016-12-25 23:36:24 +090071#if defined(IMAGE_BL31) && CRASH_REPORTING
Soby Mathew38b4bc92014-08-14 13:36:41 +010072CPU_REG_DUMP: /* cpu specific register dump for crash reporting */
73 .space 8
74#endif
Soby Mathewc704cbc2014-08-14 11:33:56 +010075CPU_OPS_SIZE = .
Achin Gupta4f6ad662013-10-25 09:08:21 +010076
Soby Mathewc704cbc2014-08-14 11:33:56 +010077 /*
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000078 * Write given expressions as quad words
79 *
80 * _count:
81 * Write at least _count quad words. If the given number of
82 * expressions is less than _count, repeat the last expression to
83 * fill _count quad words in total
84 * _rest:
85 * Optional list of expressions. _this is for parameter extraction
86 * only, and has no significance to the caller
87 *
88 * Invoked as:
89 * fill_constants 2, foo, bar, blah, ...
Achin Gupta4f6ad662013-10-25 09:08:21 +010090 */
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +000091 .macro fill_constants _count:req, _this, _rest:vararg
92 .ifgt \_count
93 /* Write the current expression */
94 .ifb \_this
95 .error "Nothing to fill"
96 .endif
97 .quad \_this
98
99 /* Invoke recursively for remaining expressions */
100 .ifnb \_rest
101 fill_constants \_count-1, \_rest
102 .else
103 fill_constants \_count-1, \_this
104 .endif
105 .endif
106 .endm
107
108 /*
109 * Declare CPU operations
110 *
111 * _name:
112 * Name of the CPU for which operations are being specified
113 * _midr:
114 * Numeric value expected to read from CPU's MIDR
115 * _resetfunc:
116 * Reset function for the CPU. If there's no CPU reset function,
117 * specify CPU_NO_RESET_FUNC
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000118 * _extra1:
119 * This is a placeholder for future per CPU operations. Currently,
120 * some CPUs use this entry to set a test function to determine if
121 * the workaround for CVE-2017-5715 needs to be applied or not.
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000122 * _power_down_ops:
123 * Comma-separated list of functions to perform power-down
124 * operatios on the CPU. At least one, and up to
125 * CPU_MAX_PWR_DWN_OPS number of functions may be specified.
126 * Starting at power level 0, these functions shall handle power
127 * down at subsequent power levels. If there aren't exactly
128 * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
129 * used to handle power down at subsequent levels
130 */
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000131 .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
132 _extra1:req, _power_down_ops:vararg
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000133 .section cpu_ops, "a"
134 .align 3
Soby Mathewc704cbc2014-08-14 11:33:56 +0100135 .type cpu_ops_\_name, %object
136 .quad \_midr
Roberto Vargase0e99462017-10-30 14:43:43 +0000137#if defined(IMAGE_AT_EL3)
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +0000138 .quad \_resetfunc
Soby Mathewc704cbc2014-08-14 11:33:56 +0100139#endif
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000140 .quad \_extra1
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900141#ifdef IMAGE_BL31
Jeenu Viswambharanee5eb802016-11-18 12:58:28 +00001421:
143 /* Insert list of functions */
144 fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
1452:
146 /*
147 * Error if no or more than CPU_MAX_PWR_DWN_OPS were specified in the
148 * list
149 */
150 .ifeq 2b - 1b
151 .error "At least one power down function must be specified"
152 .else
153 .iflt 2b - 1b - (CPU_MAX_PWR_DWN_OPS * CPU_WORD_SIZE)
154 .error "More than CPU_MAX_PWR_DWN_OPS functions specified"
155 .endif
156 .endif
Soby Mathew8e2f2872014-08-14 12:49:05 +0100157#endif
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000158
159#if REPORT_ERRATA
160 .ifndef \_name\()_cpu_str
161 /*
162 * Place errata reported flag, and the spinlock to arbitrate access to
163 * it in the data section.
164 */
165 .pushsection .data
166 define_asm_spinlock \_name\()_errata_lock
167 \_name\()_errata_reported:
168 .word 0
169 .popsection
170
171 /* Place CPU string in rodata */
172 .pushsection .rodata
173 \_name\()_cpu_str:
174 .asciz "\_name"
175 .popsection
176 .endif
177
178 /*
179 * Weakly-bound, optional errata status printing function for CPUs of
180 * this class.
181 */
182 .weak \_name\()_errata_report
183 .quad \_name\()_errata_report
184
185#ifdef IMAGE_BL31
186 /* Pointers to errata lock and reported flag */
187 .quad \_name\()_errata_lock
188 .quad \_name\()_errata_reported
189#endif
190#endif
191
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900192#if defined(IMAGE_BL31) && CRASH_REPORTING
Soby Mathew38b4bc92014-08-14 13:36:41 +0100193 .quad \_name\()_cpu_reg_dump
194#endif
Soby Mathewc704cbc2014-08-14 11:33:56 +0100195 .endm
Dan Handleyea596682015-04-01 17:34:24 +0100196
Dimitris Papastamos914757c2018-03-12 14:47:09 +0000197 .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
198 _power_down_ops:vararg
199 declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, \
200 \_power_down_ops
201 .endm
202
203 .macro declare_cpu_ops_workaround_cve_2017_5715 _name:req, _midr:req, \
204 _resetfunc:req, _extra1:req, _power_down_ops:vararg
205 declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
206 \_extra1, \_power_down_ops
207 .endm
208
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000209#if REPORT_ERRATA
210 /*
211 * Print status of a CPU errata
212 *
213 * _chosen:
214 * Identifier indicating whether or not a CPU errata has been
215 * compiled in.
216 * _cpu:
217 * Name of the CPU
218 * _id:
219 * Errata identifier
220 * _rev_var:
221 * Register containing the combined value CPU revision and variant
222 * - typically the return value of cpu_get_rev_var
223 */
224 .macro report_errata _chosen, _cpu, _id, _rev_var=x8
225 /* Stash a string with errata ID */
226 .pushsection .rodata
227 \_cpu\()_errata_\_id\()_str:
228 .asciz "\_id"
229 .popsection
230
231 /* Check whether errata applies */
232 mov x0, \_rev_var
Jonathan Wrightefb1f332018-03-28 15:52:03 +0100233 /* Shall clobber: x0-x7 */
Jeenu Viswambharand5ec3672017-01-03 11:01:51 +0000234 bl check_errata_\_id
235
236 .ifeq \_chosen
237 /*
238 * Errata workaround has not been compiled in. If the errata would have
239 * applied had it been compiled in, print its status as missing.
240 */
241 cbz x0, 900f
242 mov x0, #ERRATA_MISSING
243 .endif
244900:
245 adr x1, \_cpu\()_cpu_str
246 adr x2, \_cpu\()_errata_\_id\()_str
247 bl errata_print_msg
248 .endm
249#endif
250
Dan Handleyea596682015-04-01 17:34:24 +0100251#endif /* __CPU_MACROS_S__ */
Dimitris Papastamos780cc952018-03-12 13:27:02 +0000252
253 /*
254 * This macro is used on some CPUs to detect if they are vulnerable
255 * to CVE-2017-5715.
256 */
257 .macro cpu_check_csv2 _reg _label
258 mrs \_reg, id_aa64pfr0_el1
259 ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
260 /*
261 * If the field equals to 1 then branch targets trained in one
262 * context cannot affect speculative execution in a different context.
263 */
264 cmp \_reg, #1
265 beq \_label
266 .endm