Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 6 | #ifndef __CPU_MACROS_S__ |
| 7 | #define __CPU_MACROS_S__ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 8 | |
| 9 | #include <arch.h> |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 10 | #include <errata_report.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 11 | |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 12 | #define CPU_IMPL_PN_MASK (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \ |
| 13 | (MIDR_PN_MASK << MIDR_PN_SHIFT) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 14 | |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 15 | /* 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 Vargas | e0e9946 | 2017-10-30 14:43:43 +0000 | [diff] [blame] | 24 | #if defined(IMAGE_BL1) || defined(IMAGE_BL31) ||(defined(IMAGE_BL2) && BL2_AT_EL3) |
| 25 | #define IMAGE_AT_EL3 |
| 26 | #endif |
| 27 | |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 28 | /* |
| 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 Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Define the offsets to the fields in cpu_ops structure. |
| 40 | */ |
| 41 | .struct 0 |
| 42 | CPU_MIDR: /* cpu_ops midr */ |
| 43 | .space 8 |
| 44 | /* Reset fn is needed in BL at reset vector */ |
Roberto Vargas | e0e9946 | 2017-10-30 14:43:43 +0000 | [diff] [blame] | 45 | #if defined(IMAGE_AT_EL3) |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 46 | CPU_RESET_FUNC: /* cpu_ops reset_func */ |
| 47 | .space 8 |
| 48 | #endif |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 49 | CPU_EXTRA1_FUNC: |
| 50 | .space 8 |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 51 | #ifdef IMAGE_BL31 /* The power down core and cluster is needed only in BL31 */ |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 52 | CPU_PWR_DWN_OPS: /* cpu_ops power down functions */ |
| 53 | .space (8 * CPU_MAX_PWR_DWN_OPS) |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 54 | #endif |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 55 | |
| 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 |
| 61 | CPU_ERRATA_FUNC: |
| 62 | .space 8 |
Roberto Vargas | e0e9946 | 2017-10-30 14:43:43 +0000 | [diff] [blame] | 63 | #if defined(IMAGE_BL31) |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 64 | CPU_ERRATA_LOCK: |
| 65 | .space 8 |
| 66 | CPU_ERRATA_PRINTED: |
| 67 | .space 8 |
| 68 | #endif |
| 69 | #endif |
| 70 | |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 71 | #if defined(IMAGE_BL31) && CRASH_REPORTING |
Soby Mathew | 38b4bc9 | 2014-08-14 13:36:41 +0100 | [diff] [blame] | 72 | CPU_REG_DUMP: /* cpu specific register dump for crash reporting */ |
| 73 | .space 8 |
| 74 | #endif |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 75 | CPU_OPS_SIZE = . |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 76 | |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 77 | /* |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 78 | * 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 Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 90 | */ |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 91 | .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 Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 118 | * _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 Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 122 | * _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 Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 131 | .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \ |
| 132 | _extra1:req, _power_down_ops:vararg |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 133 | .section cpu_ops, "a" |
| 134 | .align 3 |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 135 | .type cpu_ops_\_name, %object |
| 136 | .quad \_midr |
Roberto Vargas | e0e9946 | 2017-10-30 14:43:43 +0000 | [diff] [blame] | 137 | #if defined(IMAGE_AT_EL3) |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 138 | .quad \_resetfunc |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 139 | #endif |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 140 | .quad \_extra1 |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 141 | #ifdef IMAGE_BL31 |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 142 | 1: |
| 143 | /* Insert list of functions */ |
| 144 | fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops |
| 145 | 2: |
| 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 Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 157 | #endif |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 158 | |
| 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 Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 192 | #if defined(IMAGE_BL31) && CRASH_REPORTING |
Soby Mathew | 38b4bc9 | 2014-08-14 13:36:41 +0100 | [diff] [blame] | 193 | .quad \_name\()_cpu_reg_dump |
| 194 | #endif |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 195 | .endm |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 196 | |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 197 | .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 Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 209 | #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 Wright | efb1f33 | 2018-03-28 15:52:03 +0100 | [diff] [blame] | 233 | /* Shall clobber: x0-x7 */ |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 234 | 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 |
| 244 | 900: |
| 245 | adr x1, \_cpu\()_cpu_str |
| 246 | adr x2, \_cpu\()_errata_\_id\()_str |
| 247 | bl errata_print_msg |
| 248 | .endm |
| 249 | #endif |
| 250 | |
Dan Handley | ea59668 | 2015-04-01 17:34:24 +0100 | [diff] [blame] | 251 | #endif /* __CPU_MACROS_S__ */ |
Dimitris Papastamos | 780cc95 | 2018-03-12 13:27:02 +0000 | [diff] [blame] | 252 | |
| 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 |