Soby Mathew | c704cbc | 2014-08-14 11:33:56 +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. |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <asm_macros.S> |
| 9 | #include <assert_macros.S> |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 10 | #include <cpu_data.h> |
David Cunado | 1f5f812 | 2017-01-17 14:40:15 +0000 | [diff] [blame] | 11 | #include <cpu_macros.S> |
Soby Mathew | 6b28c57 | 2016-03-21 10:36:47 +0000 | [diff] [blame] | 12 | #include <debug.h> |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 13 | #include <errata_report.h> |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 14 | |
| 15 | /* Reset fn is needed in BL at reset vector */ |
Roberto Vargas | e0e9946 | 2017-10-30 14:43:43 +0000 | [diff] [blame] | 16 | #if defined(IMAGE_BL1) || defined(IMAGE_BL31) || (defined(IMAGE_BL2) && BL2_AT_EL3) |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 17 | /* |
| 18 | * The reset handler common to all platforms. After a matching |
| 19 | * cpu_ops structure entry is found, the correponding reset_handler |
| 20 | * in the cpu_ops is invoked. |
Soby Mathew | b5a6304 | 2015-01-29 12:00:58 +0000 | [diff] [blame] | 21 | * Clobbers: x0 - x19, x30 |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 22 | */ |
| 23 | .globl reset_handler |
| 24 | func reset_handler |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 25 | mov x19, x30 |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 26 | |
Soby Mathew | b5a6304 | 2015-01-29 12:00:58 +0000 | [diff] [blame] | 27 | /* The plat_reset_handler can clobber x0 - x18, x30 */ |
Soby Mathew | f1785fd | 2014-08-14 12:22:32 +0100 | [diff] [blame] | 28 | bl plat_reset_handler |
| 29 | |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 30 | /* Get the matching cpu_ops pointer */ |
| 31 | bl get_cpu_ops_ptr |
Antonio Nino Diaz | 7c65c1e | 2017-04-20 09:58:28 +0100 | [diff] [blame] | 32 | #if ENABLE_ASSERTIONS |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 33 | cmp x0, #0 |
| 34 | ASM_ASSERT(ne) |
| 35 | #endif |
| 36 | |
| 37 | /* Get the cpu_ops reset handler */ |
| 38 | ldr x2, [x0, #CPU_RESET_FUNC] |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 39 | mov x30, x19 |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 40 | cbz x2, 1f |
Soby Mathew | b5a6304 | 2015-01-29 12:00:58 +0000 | [diff] [blame] | 41 | |
| 42 | /* The cpu_ops reset handler can clobber x0 - x19, x30 */ |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 43 | br x2 |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 44 | 1: |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 45 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 46 | endfunc reset_handler |
Soby Mathew | f1785fd | 2014-08-14 12:22:32 +0100 | [diff] [blame] | 47 | |
Roberto Vargas | e0e9946 | 2017-10-30 14:43:43 +0000 | [diff] [blame] | 48 | #endif |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 49 | |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 50 | #ifdef IMAGE_BL31 /* The power down core and cluster is needed only in BL31 */ |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 51 | /* |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 52 | * void prepare_cpu_pwr_dwn(unsigned int power_level) |
| 53 | * |
| 54 | * Prepare CPU power down function for all platforms. The function takes |
| 55 | * a domain level to be powered down as its parameter. After the cpu_ops |
| 56 | * pointer is retrieved from cpu_data, the handler for requested power |
| 57 | * level is called. |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 58 | */ |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 59 | .globl prepare_cpu_pwr_dwn |
| 60 | func prepare_cpu_pwr_dwn |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 61 | /* |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 62 | * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the |
| 63 | * power down handler for the last power level |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 64 | */ |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 65 | mov_imm x2, (CPU_MAX_PWR_DWN_OPS - 1) |
| 66 | cmp x0, x2 |
| 67 | csel x2, x2, x0, hi |
| 68 | |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 69 | mrs x1, tpidr_el3 |
| 70 | ldr x0, [x1, #CPU_DATA_CPU_OPS_PTR] |
Antonio Nino Diaz | 7c65c1e | 2017-04-20 09:58:28 +0100 | [diff] [blame] | 71 | #if ENABLE_ASSERTIONS |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 72 | cmp x0, #0 |
| 73 | ASM_ASSERT(ne) |
| 74 | #endif |
| 75 | |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 76 | /* Get the appropriate power down handler */ |
| 77 | mov x1, #CPU_PWR_DWN_OPS |
| 78 | add x1, x1, x2, lsl #3 |
| 79 | ldr x1, [x0, x1] |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 80 | br x1 |
Jeenu Viswambharan | ee5eb80 | 2016-11-18 12:58:28 +0000 | [diff] [blame] | 81 | endfunc prepare_cpu_pwr_dwn |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 82 | |
| 83 | |
| 84 | /* |
| 85 | * Initializes the cpu_ops_ptr if not already initialized |
Vikram Kanigiri | 9b38fc8 | 2015-01-29 18:27:38 +0000 | [diff] [blame] | 86 | * in cpu_data. This can be called without a runtime stack, but may |
| 87 | * only be called after the MMU is enabled. |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 88 | * clobbers: x0 - x6, x10 |
| 89 | */ |
| 90 | .globl init_cpu_ops |
| 91 | func init_cpu_ops |
| 92 | mrs x6, tpidr_el3 |
| 93 | ldr x0, [x6, #CPU_DATA_CPU_OPS_PTR] |
| 94 | cbnz x0, 1f |
| 95 | mov x10, x30 |
| 96 | bl get_cpu_ops_ptr |
Antonio Nino Diaz | 7c65c1e | 2017-04-20 09:58:28 +0100 | [diff] [blame] | 97 | #if ENABLE_ASSERTIONS |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 98 | cmp x0, #0 |
| 99 | ASM_ASSERT(ne) |
| 100 | #endif |
Soby Mathew | 7d861ea | 2014-11-18 10:14:14 +0000 | [diff] [blame] | 101 | str x0, [x6, #CPU_DATA_CPU_OPS_PTR]! |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 102 | mov x30, x10 |
| 103 | 1: |
| 104 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 105 | endfunc init_cpu_ops |
Soby Mathew | 8e2f287 | 2014-08-14 12:49:05 +0100 | [diff] [blame] | 106 | #endif /* IMAGE_BL31 */ |
| 107 | |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 108 | #if defined(IMAGE_BL31) && CRASH_REPORTING |
Soby Mathew | 38b4bc9 | 2014-08-14 13:36:41 +0100 | [diff] [blame] | 109 | /* |
| 110 | * The cpu specific registers which need to be reported in a crash |
| 111 | * are reported via cpu_ops cpu_reg_dump function. After a matching |
| 112 | * cpu_ops structure entry is found, the correponding cpu_reg_dump |
| 113 | * in the cpu_ops is invoked. |
| 114 | */ |
| 115 | .globl do_cpu_reg_dump |
| 116 | func do_cpu_reg_dump |
| 117 | mov x16, x30 |
| 118 | |
| 119 | /* Get the matching cpu_ops pointer */ |
| 120 | bl get_cpu_ops_ptr |
| 121 | cbz x0, 1f |
| 122 | |
| 123 | /* Get the cpu_ops cpu_reg_dump */ |
| 124 | ldr x2, [x0, #CPU_REG_DUMP] |
| 125 | cbz x2, 1f |
| 126 | blr x2 |
| 127 | 1: |
| 128 | mov x30, x16 |
| 129 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 130 | endfunc do_cpu_reg_dump |
Soby Mathew | 38b4bc9 | 2014-08-14 13:36:41 +0100 | [diff] [blame] | 131 | #endif |
| 132 | |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 133 | /* |
| 134 | * The below function returns the cpu_ops structure matching the |
| 135 | * midr of the core. It reads the MIDR_EL1 and finds the matching |
| 136 | * entry in cpu_ops entries. Only the implementation and part number |
| 137 | * are used to match the entries. |
| 138 | * Return : |
| 139 | * x0 - The matching cpu_ops pointer on Success |
| 140 | * x0 - 0 on failure. |
| 141 | * Clobbers : x0 - x5 |
| 142 | */ |
| 143 | .globl get_cpu_ops_ptr |
| 144 | func get_cpu_ops_ptr |
| 145 | /* Get the cpu_ops start and end locations */ |
| 146 | adr x4, (__CPU_OPS_START__ + CPU_MIDR) |
| 147 | adr x5, (__CPU_OPS_END__ + CPU_MIDR) |
| 148 | |
| 149 | /* Initialize the return parameter */ |
| 150 | mov x0, #0 |
| 151 | |
| 152 | /* Read the MIDR_EL1 */ |
| 153 | mrs x2, midr_el1 |
| 154 | mov_imm x3, CPU_IMPL_PN_MASK |
| 155 | |
| 156 | /* Retain only the implementation and part number using mask */ |
| 157 | and w2, w2, w3 |
| 158 | 1: |
| 159 | /* Check if we have reached end of list */ |
| 160 | cmp x4, x5 |
| 161 | b.eq error_exit |
| 162 | |
| 163 | /* load the midr from the cpu_ops */ |
| 164 | ldr x1, [x4], #CPU_OPS_SIZE |
| 165 | and w1, w1, w3 |
| 166 | |
| 167 | /* Check if midr matches to midr of this core */ |
| 168 | cmp w1, w2 |
| 169 | b.ne 1b |
| 170 | |
| 171 | /* Subtract the increment and offset to get the cpu-ops pointer */ |
| 172 | sub x0, x4, #(CPU_OPS_SIZE + CPU_MIDR) |
| 173 | error_exit: |
| 174 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 175 | endfunc get_cpu_ops_ptr |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 176 | |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 177 | /* |
| 178 | * Extract CPU revision and variant, and combine them into a single numeric for |
| 179 | * easier comparison. |
| 180 | */ |
| 181 | .globl cpu_get_rev_var |
| 182 | func cpu_get_rev_var |
| 183 | mrs x1, midr_el1 |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 184 | |
Sandrine Bailleux | d481759 | 2016-01-13 14:57:38 +0000 | [diff] [blame] | 185 | /* |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 186 | * Extract the variant[23:20] and revision[3:0] from MIDR, and pack them |
| 187 | * as variant[7:4] and revision[3:0] of x0. |
Sandrine Bailleux | d481759 | 2016-01-13 14:57:38 +0000 | [diff] [blame] | 188 | * |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 189 | * First extract x1[23:16] to x0[7:0] and zero fill the rest. Then |
| 190 | * extract x1[3:0] into x0[3:0] retaining other bits. |
Sandrine Bailleux | d481759 | 2016-01-13 14:57:38 +0000 | [diff] [blame] | 191 | */ |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 192 | ubfx x0, x1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS) |
| 193 | bfxil x0, x1, #MIDR_REV_SHIFT, #MIDR_REV_BITS |
| 194 | ret |
| 195 | endfunc cpu_get_rev_var |
| 196 | |
| 197 | /* |
| 198 | * Compare the CPU's revision-variant (x0) with a given value (x1), for errata |
| 199 | * application purposes. If the revision-variant is less than or same as a given |
| 200 | * value, indicates that errata applies; otherwise not. |
Jonathan Wright | efb1f33 | 2018-03-28 15:52:03 +0100 | [diff] [blame] | 201 | * |
| 202 | * Shall clobber: x0-x3 |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 203 | */ |
| 204 | .globl cpu_rev_var_ls |
| 205 | func cpu_rev_var_ls |
| 206 | mov x2, #ERRATA_APPLIES |
| 207 | mov x3, #ERRATA_NOT_APPLIES |
| 208 | cmp x0, x1 |
| 209 | csel x0, x2, x3, ls |
| 210 | ret |
| 211 | endfunc cpu_rev_var_ls |
| 212 | |
Andre Przywara | 00eefd9 | 2016-10-06 16:54:53 +0100 | [diff] [blame] | 213 | /* |
| 214 | * Compare the CPU's revision-variant (x0) with a given value (x1), for errata |
| 215 | * application purposes. If the revision-variant is higher than or same as a |
| 216 | * given value, indicates that errata applies; otherwise not. |
Jonathan Wright | efb1f33 | 2018-03-28 15:52:03 +0100 | [diff] [blame] | 217 | * |
| 218 | * Shall clobber: x0-x3 |
Andre Przywara | 00eefd9 | 2016-10-06 16:54:53 +0100 | [diff] [blame] | 219 | */ |
| 220 | .globl cpu_rev_var_hs |
| 221 | func cpu_rev_var_hs |
| 222 | mov x2, #ERRATA_APPLIES |
| 223 | mov x3, #ERRATA_NOT_APPLIES |
| 224 | cmp x0, x1 |
| 225 | csel x0, x2, x3, hs |
| 226 | ret |
| 227 | endfunc cpu_rev_var_hs |
| 228 | |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 229 | #if REPORT_ERRATA |
| 230 | /* |
| 231 | * void print_errata_status(void); |
| 232 | * |
| 233 | * Function to print errata status for CPUs of its class. Must be called only: |
| 234 | * |
| 235 | * - with MMU and data caches are enabled; |
| 236 | * - after cpu_ops have been initialized in per-CPU data. |
| 237 | */ |
| 238 | .globl print_errata_status |
| 239 | func print_errata_status |
| 240 | #ifdef IMAGE_BL1 |
| 241 | /* |
| 242 | * BL1 doesn't have per-CPU data. So retrieve the CPU operations |
| 243 | * directly. |
| 244 | */ |
| 245 | stp xzr, x30, [sp, #-16]! |
| 246 | bl get_cpu_ops_ptr |
| 247 | ldp xzr, x30, [sp], #16 |
| 248 | ldr x1, [x0, #CPU_ERRATA_FUNC] |
| 249 | cbnz x1, .Lprint |
| 250 | #else |
| 251 | /* |
| 252 | * Retrieve pointer to cpu_ops from per-CPU data, and further, the |
| 253 | * errata printing function. If it's non-NULL, jump to the function in |
| 254 | * turn. |
| 255 | */ |
| 256 | mrs x0, tpidr_el3 |
| 257 | ldr x1, [x0, #CPU_DATA_CPU_OPS_PTR] |
| 258 | ldr x0, [x1, #CPU_ERRATA_FUNC] |
| 259 | cbz x0, .Lnoprint |
| 260 | |
| 261 | /* |
| 262 | * Printing errata status requires atomically testing the printed flag. |
| 263 | */ |
dp-arm | 815faa8 | 2017-05-05 12:21:03 +0100 | [diff] [blame] | 264 | stp x19, x30, [sp, #-16]! |
| 265 | mov x19, x0 |
Soby Mathew | c088433 | 2014-09-22 12:11:36 +0100 | [diff] [blame] | 266 | |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 267 | /* |
| 268 | * Load pointers to errata lock and printed flag. Call |
| 269 | * errata_needs_reporting to check whether this CPU needs to report |
| 270 | * errata status pertaining to its class. |
| 271 | */ |
| 272 | ldr x0, [x1, #CPU_ERRATA_LOCK] |
| 273 | ldr x1, [x1, #CPU_ERRATA_PRINTED] |
| 274 | bl errata_needs_reporting |
dp-arm | 815faa8 | 2017-05-05 12:21:03 +0100 | [diff] [blame] | 275 | mov x1, x19 |
| 276 | ldp x19, x30, [sp], #16 |
Jeenu Viswambharan | d5ec367 | 2017-01-03 11:01:51 +0000 | [diff] [blame] | 277 | cbnz x0, .Lprint |
| 278 | #endif |
| 279 | .Lnoprint: |
| 280 | ret |
| 281 | .Lprint: |
| 282 | /* Jump to errata reporting function for this CPU */ |
| 283 | br x1 |
| 284 | endfunc print_errata_status |
| 285 | #endif |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 286 | |
| 287 | /* |
Dimitris Papastamos | 570c06a | 2018-04-06 15:29:34 +0100 | [diff] [blame^] | 288 | * int check_wa_cve_2017_5715(void); |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 289 | * |
| 290 | * This function returns: |
| 291 | * - ERRATA_APPLIES when firmware mitigation is required. |
| 292 | * - ERRATA_NOT_APPLIES when firmware mitigation is _not_ required. |
| 293 | * - ERRATA_MISSING when firmware mitigation would be required but |
| 294 | * is not compiled in. |
| 295 | * |
| 296 | * NOTE: Must be called only after cpu_ops have been initialized |
| 297 | * in per-CPU data. |
| 298 | */ |
Dimitris Papastamos | 570c06a | 2018-04-06 15:29:34 +0100 | [diff] [blame^] | 299 | .globl check_wa_cve_2017_5715 |
| 300 | func check_wa_cve_2017_5715 |
Dimitris Papastamos | 914757c | 2018-03-12 14:47:09 +0000 | [diff] [blame] | 301 | mrs x0, tpidr_el3 |
| 302 | #if ENABLE_ASSERTIONS |
| 303 | cmp x0, #0 |
| 304 | ASM_ASSERT(ne) |
| 305 | #endif |
| 306 | ldr x0, [x0, #CPU_DATA_CPU_OPS_PTR] |
| 307 | ldr x0, [x0, #CPU_EXTRA1_FUNC] |
| 308 | /* |
| 309 | * If the reserved function pointer is NULL, this CPU |
| 310 | * is unaffected by CVE-2017-5715 so bail out. |
| 311 | */ |
| 312 | cmp x0, #0 |
| 313 | beq 1f |
| 314 | br x0 |
| 315 | 1: |
| 316 | mov x0, #ERRATA_NOT_APPLIES |
| 317 | ret |
Dimitris Papastamos | 570c06a | 2018-04-06 15:29:34 +0100 | [diff] [blame^] | 318 | endfunc check_wa_cve_2017_5715 |