19b476841SSoby Mathew/* 2c2ad38ceSVarun Wadekar * Copyright (c) 2014-2019, ARM Limited and Contributors. All rights reserved. 39b476841SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 59b476841SSoby Mathew */ 69b476841SSoby Mathew 79b476841SSoby Mathew#include <arch.h> 89b476841SSoby Mathew#include <asm_macros.S> 99b476841SSoby Mathew#include <assert_macros.S> 10c2ad38ceSVarun Wadekar#include <common/bl_common.h> 1109d40e0eSAntonio Nino Diaz#include <common/debug.h> 1255c70cb7SDavid Cunado#include <cpu_macros.S> 1309d40e0eSAntonio Nino Diaz#include <lib/cpus/errata_report.h> 1409d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h> 159b476841SSoby Mathew 169b476841SSoby Mathew /* Reset fn is needed in BL at reset vector */ 17b1d27b48SRoberto Vargas#if defined(IMAGE_BL1) || defined(IMAGE_BL31) || (defined(IMAGE_BL2) && BL2_AT_EL3) 189b476841SSoby Mathew /* 199b476841SSoby Mathew * The reset handler common to all platforms. After a matching 209b476841SSoby Mathew * cpu_ops structure entry is found, the correponding reset_handler 219b476841SSoby Mathew * in the cpu_ops is invoked. 22683f788fSSoby Mathew * Clobbers: x0 - x19, x30 239b476841SSoby Mathew */ 249b476841SSoby Mathew .globl reset_handler 259b476841SSoby Mathewfunc reset_handler 267395a725SSoby Mathew mov x19, x30 279b476841SSoby Mathew 28683f788fSSoby Mathew /* The plat_reset_handler can clobber x0 - x18, x30 */ 2924fb838fSSoby Mathew bl plat_reset_handler 3024fb838fSSoby Mathew 319b476841SSoby Mathew /* Get the matching cpu_ops pointer */ 329b476841SSoby Mathew bl get_cpu_ops_ptr 33044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS 349b476841SSoby Mathew cmp x0, #0 359b476841SSoby Mathew ASM_ASSERT(ne) 369b476841SSoby Mathew#endif 379b476841SSoby Mathew 389b476841SSoby Mathew /* Get the cpu_ops reset handler */ 399b476841SSoby Mathew ldr x2, [x0, #CPU_RESET_FUNC] 407395a725SSoby Mathew mov x30, x19 419b476841SSoby Mathew cbz x2, 1f 42683f788fSSoby Mathew 43683f788fSSoby Mathew /* The cpu_ops reset handler can clobber x0 - x19, x30 */ 447395a725SSoby Mathew br x2 459b476841SSoby Mathew1: 467395a725SSoby Mathew ret 478b779620SKévin Petitendfunc reset_handler 4824fb838fSSoby Mathew 49b1d27b48SRoberto Vargas#endif 509b476841SSoby Mathew 513d8256b2SMasahiro Yamada#ifdef IMAGE_BL31 /* The power down core and cluster is needed only in BL31 */ 52add40351SSoby Mathew /* 535dd9dbb5SJeenu Viswambharan * void prepare_cpu_pwr_dwn(unsigned int power_level) 545dd9dbb5SJeenu Viswambharan * 555dd9dbb5SJeenu Viswambharan * Prepare CPU power down function for all platforms. The function takes 565dd9dbb5SJeenu Viswambharan * a domain level to be powered down as its parameter. After the cpu_ops 575dd9dbb5SJeenu Viswambharan * pointer is retrieved from cpu_data, the handler for requested power 585dd9dbb5SJeenu Viswambharan * level is called. 59add40351SSoby Mathew */ 605dd9dbb5SJeenu Viswambharan .globl prepare_cpu_pwr_dwn 615dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn 62add40351SSoby Mathew /* 635dd9dbb5SJeenu Viswambharan * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the 645dd9dbb5SJeenu Viswambharan * power down handler for the last power level 65add40351SSoby Mathew */ 665dd9dbb5SJeenu Viswambharan mov_imm x2, (CPU_MAX_PWR_DWN_OPS - 1) 675dd9dbb5SJeenu Viswambharan cmp x0, x2 685dd9dbb5SJeenu Viswambharan csel x2, x2, x0, hi 695dd9dbb5SJeenu Viswambharan 70add40351SSoby Mathew mrs x1, tpidr_el3 71add40351SSoby Mathew ldr x0, [x1, #CPU_DATA_CPU_OPS_PTR] 72044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS 73add40351SSoby Mathew cmp x0, #0 74add40351SSoby Mathew ASM_ASSERT(ne) 75add40351SSoby Mathew#endif 76add40351SSoby Mathew 775dd9dbb5SJeenu Viswambharan /* Get the appropriate power down handler */ 785dd9dbb5SJeenu Viswambharan mov x1, #CPU_PWR_DWN_OPS 795dd9dbb5SJeenu Viswambharan add x1, x1, x2, lsl #3 805dd9dbb5SJeenu Viswambharan ldr x1, [x0, x1] 81add40351SSoby Mathew br x1 825dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn 83add40351SSoby Mathew 84add40351SSoby Mathew 85add40351SSoby Mathew /* 86add40351SSoby Mathew * Initializes the cpu_ops_ptr if not already initialized 8712e7c4abSVikram Kanigiri * in cpu_data. This can be called without a runtime stack, but may 8812e7c4abSVikram Kanigiri * only be called after the MMU is enabled. 89add40351SSoby Mathew * clobbers: x0 - x6, x10 90add40351SSoby Mathew */ 91add40351SSoby Mathew .globl init_cpu_ops 92add40351SSoby Mathewfunc init_cpu_ops 93add40351SSoby Mathew mrs x6, tpidr_el3 94add40351SSoby Mathew ldr x0, [x6, #CPU_DATA_CPU_OPS_PTR] 95add40351SSoby Mathew cbnz x0, 1f 96add40351SSoby Mathew mov x10, x30 97add40351SSoby Mathew bl get_cpu_ops_ptr 98044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS 99add40351SSoby Mathew cmp x0, #0 100add40351SSoby Mathew ASM_ASSERT(ne) 101add40351SSoby Mathew#endif 10209997346SSoby Mathew str x0, [x6, #CPU_DATA_CPU_OPS_PTR]! 103add40351SSoby Mathew mov x30, x10 104add40351SSoby Mathew1: 105add40351SSoby Mathew ret 1068b779620SKévin Petitendfunc init_cpu_ops 107add40351SSoby Mathew#endif /* IMAGE_BL31 */ 108add40351SSoby Mathew 1093d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING 110d3f70af6SSoby Mathew /* 111d3f70af6SSoby Mathew * The cpu specific registers which need to be reported in a crash 112d3f70af6SSoby Mathew * are reported via cpu_ops cpu_reg_dump function. After a matching 113d3f70af6SSoby Mathew * cpu_ops structure entry is found, the correponding cpu_reg_dump 114d3f70af6SSoby Mathew * in the cpu_ops is invoked. 115d3f70af6SSoby Mathew */ 116d3f70af6SSoby Mathew .globl do_cpu_reg_dump 117d3f70af6SSoby Mathewfunc do_cpu_reg_dump 118d3f70af6SSoby Mathew mov x16, x30 119d3f70af6SSoby Mathew 120d3f70af6SSoby Mathew /* Get the matching cpu_ops pointer */ 121d3f70af6SSoby Mathew bl get_cpu_ops_ptr 122d3f70af6SSoby Mathew cbz x0, 1f 123d3f70af6SSoby Mathew 124d3f70af6SSoby Mathew /* Get the cpu_ops cpu_reg_dump */ 125d3f70af6SSoby Mathew ldr x2, [x0, #CPU_REG_DUMP] 126d3f70af6SSoby Mathew cbz x2, 1f 127d3f70af6SSoby Mathew blr x2 128d3f70af6SSoby Mathew1: 129d3f70af6SSoby Mathew mov x30, x16 130d3f70af6SSoby Mathew ret 1318b779620SKévin Petitendfunc do_cpu_reg_dump 132d3f70af6SSoby Mathew#endif 133d3f70af6SSoby Mathew 1349b476841SSoby Mathew /* 1359b476841SSoby Mathew * The below function returns the cpu_ops structure matching the 1369b476841SSoby Mathew * midr of the core. It reads the MIDR_EL1 and finds the matching 1379b476841SSoby Mathew * entry in cpu_ops entries. Only the implementation and part number 1389b476841SSoby Mathew * are used to match the entries. 1399b476841SSoby Mathew * Return : 1409b476841SSoby Mathew * x0 - The matching cpu_ops pointer on Success 1419b476841SSoby Mathew * x0 - 0 on failure. 1429b476841SSoby Mathew * Clobbers : x0 - x5 1439b476841SSoby Mathew */ 1449b476841SSoby Mathew .globl get_cpu_ops_ptr 1459b476841SSoby Mathewfunc get_cpu_ops_ptr 1469b476841SSoby Mathew /* Get the cpu_ops start and end locations */ 1479b476841SSoby Mathew adr x4, (__CPU_OPS_START__ + CPU_MIDR) 1489b476841SSoby Mathew adr x5, (__CPU_OPS_END__ + CPU_MIDR) 1499b476841SSoby Mathew 1509b476841SSoby Mathew /* Initialize the return parameter */ 1519b476841SSoby Mathew mov x0, #0 1529b476841SSoby Mathew 1539b476841SSoby Mathew /* Read the MIDR_EL1 */ 1549b476841SSoby Mathew mrs x2, midr_el1 1559b476841SSoby Mathew mov_imm x3, CPU_IMPL_PN_MASK 1569b476841SSoby Mathew 1579b476841SSoby Mathew /* Retain only the implementation and part number using mask */ 1589b476841SSoby Mathew and w2, w2, w3 1599b476841SSoby Mathew1: 1609b476841SSoby Mathew /* Check if we have reached end of list */ 1619b476841SSoby Mathew cmp x4, x5 1629b476841SSoby Mathew b.eq error_exit 1639b476841SSoby Mathew 1649b476841SSoby Mathew /* load the midr from the cpu_ops */ 1659b476841SSoby Mathew ldr x1, [x4], #CPU_OPS_SIZE 1669b476841SSoby Mathew and w1, w1, w3 1679b476841SSoby Mathew 1689b476841SSoby Mathew /* Check if midr matches to midr of this core */ 1699b476841SSoby Mathew cmp w1, w2 1709b476841SSoby Mathew b.ne 1b 1719b476841SSoby Mathew 1729b476841SSoby Mathew /* Subtract the increment and offset to get the cpu-ops pointer */ 1739b476841SSoby Mathew sub x0, x4, #(CPU_OPS_SIZE + CPU_MIDR) 1749b476841SSoby Mathewerror_exit: 1759b476841SSoby Mathew ret 1768b779620SKévin Petitendfunc get_cpu_ops_ptr 1777395a725SSoby Mathew 17810bcd761SJeenu Viswambharan/* 17910bcd761SJeenu Viswambharan * Extract CPU revision and variant, and combine them into a single numeric for 18010bcd761SJeenu Viswambharan * easier comparison. 18110bcd761SJeenu Viswambharan */ 18210bcd761SJeenu Viswambharan .globl cpu_get_rev_var 18310bcd761SJeenu Viswambharanfunc cpu_get_rev_var 18410bcd761SJeenu Viswambharan mrs x1, midr_el1 1857395a725SSoby Mathew 18654035fc4SSandrine Bailleux /* 18710bcd761SJeenu Viswambharan * Extract the variant[23:20] and revision[3:0] from MIDR, and pack them 18810bcd761SJeenu Viswambharan * as variant[7:4] and revision[3:0] of x0. 18954035fc4SSandrine Bailleux * 19010bcd761SJeenu Viswambharan * First extract x1[23:16] to x0[7:0] and zero fill the rest. Then 19110bcd761SJeenu Viswambharan * extract x1[3:0] into x0[3:0] retaining other bits. 19254035fc4SSandrine Bailleux */ 19310bcd761SJeenu Viswambharan ubfx x0, x1, #(MIDR_VAR_SHIFT - MIDR_REV_BITS), #(MIDR_REV_BITS + MIDR_VAR_BITS) 19410bcd761SJeenu Viswambharan bfxil x0, x1, #MIDR_REV_SHIFT, #MIDR_REV_BITS 19510bcd761SJeenu Viswambharan ret 19610bcd761SJeenu Viswambharanendfunc cpu_get_rev_var 1977395a725SSoby Mathew 19810bcd761SJeenu Viswambharan/* 19910bcd761SJeenu Viswambharan * Compare the CPU's revision-variant (x0) with a given value (x1), for errata 20010bcd761SJeenu Viswambharan * application purposes. If the revision-variant is less than or same as a given 20110bcd761SJeenu Viswambharan * value, indicates that errata applies; otherwise not. 2029ec3921cSJonathan Wright * 2039ec3921cSJonathan Wright * Shall clobber: x0-x3 20410bcd761SJeenu Viswambharan */ 20510bcd761SJeenu Viswambharan .globl cpu_rev_var_ls 20610bcd761SJeenu Viswambharanfunc cpu_rev_var_ls 20710bcd761SJeenu Viswambharan mov x2, #ERRATA_APPLIES 20810bcd761SJeenu Viswambharan mov x3, #ERRATA_NOT_APPLIES 20910bcd761SJeenu Viswambharan cmp x0, x1 21010bcd761SJeenu Viswambharan csel x0, x2, x3, ls 21110bcd761SJeenu Viswambharan ret 21210bcd761SJeenu Viswambharanendfunc cpu_rev_var_ls 21310bcd761SJeenu Viswambharan 214b75dc0e4SAndre Przywara/* 215b75dc0e4SAndre Przywara * Compare the CPU's revision-variant (x0) with a given value (x1), for errata 216b75dc0e4SAndre Przywara * application purposes. If the revision-variant is higher than or same as a 217b75dc0e4SAndre Przywara * given value, indicates that errata applies; otherwise not. 2189ec3921cSJonathan Wright * 2199ec3921cSJonathan Wright * Shall clobber: x0-x3 220b75dc0e4SAndre Przywara */ 221b75dc0e4SAndre Przywara .globl cpu_rev_var_hs 222b75dc0e4SAndre Przywarafunc cpu_rev_var_hs 223b75dc0e4SAndre Przywara mov x2, #ERRATA_APPLIES 224b75dc0e4SAndre Przywara mov x3, #ERRATA_NOT_APPLIES 225b75dc0e4SAndre Przywara cmp x0, x1 226b75dc0e4SAndre Przywara csel x0, x2, x3, hs 227b75dc0e4SAndre Przywara ret 228b75dc0e4SAndre Przywaraendfunc cpu_rev_var_hs 229b75dc0e4SAndre Przywara 230*80942622Slaurenw-arm/* 231*80942622Slaurenw-arm * Compare the CPU's revision-variant (x0) with a given range (x1 - x2), for errata 232*80942622Slaurenw-arm * application purposes. If the revision-variant is between or includes the given 233*80942622Slaurenw-arm * values, this indicates that errata applies; otherwise not. 234*80942622Slaurenw-arm * 235*80942622Slaurenw-arm * Shall clobber: x0-x4 236*80942622Slaurenw-arm */ 237*80942622Slaurenw-arm .globl cpu_rev_var_range 238*80942622Slaurenw-armfunc cpu_rev_var_range 239*80942622Slaurenw-arm mov x3, #ERRATA_APPLIES 240*80942622Slaurenw-arm mov x4, #ERRATA_NOT_APPLIES 241*80942622Slaurenw-arm cmp x0, x1 242*80942622Slaurenw-arm csel x1, x3, x4, hs 243*80942622Slaurenw-arm cbz x1, 1f 244*80942622Slaurenw-arm cmp x0, x2 245*80942622Slaurenw-arm csel x1, x3, x4, ls 246*80942622Slaurenw-arm1: 247*80942622Slaurenw-arm mov x0, x1 248*80942622Slaurenw-arm ret 249*80942622Slaurenw-armendfunc cpu_rev_var_range 250*80942622Slaurenw-arm 25110bcd761SJeenu Viswambharan#if REPORT_ERRATA 25210bcd761SJeenu Viswambharan/* 25310bcd761SJeenu Viswambharan * void print_errata_status(void); 25410bcd761SJeenu Viswambharan * 25510bcd761SJeenu Viswambharan * Function to print errata status for CPUs of its class. Must be called only: 25610bcd761SJeenu Viswambharan * 25710bcd761SJeenu Viswambharan * - with MMU and data caches are enabled; 25810bcd761SJeenu Viswambharan * - after cpu_ops have been initialized in per-CPU data. 25910bcd761SJeenu Viswambharan */ 26010bcd761SJeenu Viswambharan .globl print_errata_status 26110bcd761SJeenu Viswambharanfunc print_errata_status 26210bcd761SJeenu Viswambharan#ifdef IMAGE_BL1 26310bcd761SJeenu Viswambharan /* 26410bcd761SJeenu Viswambharan * BL1 doesn't have per-CPU data. So retrieve the CPU operations 26510bcd761SJeenu Viswambharan * directly. 26610bcd761SJeenu Viswambharan */ 26710bcd761SJeenu Viswambharan stp xzr, x30, [sp, #-16]! 26810bcd761SJeenu Viswambharan bl get_cpu_ops_ptr 26910bcd761SJeenu Viswambharan ldp xzr, x30, [sp], #16 27010bcd761SJeenu Viswambharan ldr x1, [x0, #CPU_ERRATA_FUNC] 27110bcd761SJeenu Viswambharan cbnz x1, .Lprint 27210bcd761SJeenu Viswambharan#else 27310bcd761SJeenu Viswambharan /* 27410bcd761SJeenu Viswambharan * Retrieve pointer to cpu_ops from per-CPU data, and further, the 27510bcd761SJeenu Viswambharan * errata printing function. If it's non-NULL, jump to the function in 27610bcd761SJeenu Viswambharan * turn. 27710bcd761SJeenu Viswambharan */ 27810bcd761SJeenu Viswambharan mrs x0, tpidr_el3 27910bcd761SJeenu Viswambharan ldr x1, [x0, #CPU_DATA_CPU_OPS_PTR] 28010bcd761SJeenu Viswambharan ldr x0, [x1, #CPU_ERRATA_FUNC] 28110bcd761SJeenu Viswambharan cbz x0, .Lnoprint 28210bcd761SJeenu Viswambharan 28310bcd761SJeenu Viswambharan /* 28410bcd761SJeenu Viswambharan * Printing errata status requires atomically testing the printed flag. 28510bcd761SJeenu Viswambharan */ 28622fa58cbSdp-arm stp x19, x30, [sp, #-16]! 28722fa58cbSdp-arm mov x19, x0 28810bcd761SJeenu Viswambharan 28910bcd761SJeenu Viswambharan /* 29010bcd761SJeenu Viswambharan * Load pointers to errata lock and printed flag. Call 29110bcd761SJeenu Viswambharan * errata_needs_reporting to check whether this CPU needs to report 29210bcd761SJeenu Viswambharan * errata status pertaining to its class. 29310bcd761SJeenu Viswambharan */ 29410bcd761SJeenu Viswambharan ldr x0, [x1, #CPU_ERRATA_LOCK] 29510bcd761SJeenu Viswambharan ldr x1, [x1, #CPU_ERRATA_PRINTED] 29610bcd761SJeenu Viswambharan bl errata_needs_reporting 29722fa58cbSdp-arm mov x1, x19 29822fa58cbSdp-arm ldp x19, x30, [sp], #16 29910bcd761SJeenu Viswambharan cbnz x0, .Lprint 30010bcd761SJeenu Viswambharan#endif 30110bcd761SJeenu Viswambharan.Lnoprint: 30210bcd761SJeenu Viswambharan ret 30310bcd761SJeenu Viswambharan.Lprint: 30410bcd761SJeenu Viswambharan /* Jump to errata reporting function for this CPU */ 30510bcd761SJeenu Viswambharan br x1 30610bcd761SJeenu Viswambharanendfunc print_errata_status 30710bcd761SJeenu Viswambharan#endif 308a205a56eSDimitris Papastamos 309a205a56eSDimitris Papastamos/* 3102c3a1078SDimitris Papastamos * int check_wa_cve_2017_5715(void); 311a205a56eSDimitris Papastamos * 312a205a56eSDimitris Papastamos * This function returns: 313a205a56eSDimitris Papastamos * - ERRATA_APPLIES when firmware mitigation is required. 314a205a56eSDimitris Papastamos * - ERRATA_NOT_APPLIES when firmware mitigation is _not_ required. 315a205a56eSDimitris Papastamos * - ERRATA_MISSING when firmware mitigation would be required but 316a205a56eSDimitris Papastamos * is not compiled in. 317a205a56eSDimitris Papastamos * 318a205a56eSDimitris Papastamos * NOTE: Must be called only after cpu_ops have been initialized 319a205a56eSDimitris Papastamos * in per-CPU data. 320a205a56eSDimitris Papastamos */ 3212c3a1078SDimitris Papastamos .globl check_wa_cve_2017_5715 3222c3a1078SDimitris Papastamosfunc check_wa_cve_2017_5715 323a205a56eSDimitris Papastamos mrs x0, tpidr_el3 324a205a56eSDimitris Papastamos#if ENABLE_ASSERTIONS 325a205a56eSDimitris Papastamos cmp x0, #0 326a205a56eSDimitris Papastamos ASM_ASSERT(ne) 327a205a56eSDimitris Papastamos#endif 328a205a56eSDimitris Papastamos ldr x0, [x0, #CPU_DATA_CPU_OPS_PTR] 329a205a56eSDimitris Papastamos ldr x0, [x0, #CPU_EXTRA1_FUNC] 330a205a56eSDimitris Papastamos /* 331a205a56eSDimitris Papastamos * If the reserved function pointer is NULL, this CPU 332a205a56eSDimitris Papastamos * is unaffected by CVE-2017-5715 so bail out. 333a205a56eSDimitris Papastamos */ 334a205a56eSDimitris Papastamos cmp x0, #0 335a205a56eSDimitris Papastamos beq 1f 336a205a56eSDimitris Papastamos br x0 337a205a56eSDimitris Papastamos1: 338a205a56eSDimitris Papastamos mov x0, #ERRATA_NOT_APPLIES 339a205a56eSDimitris Papastamos ret 3402c3a1078SDimitris Papastamosendfunc check_wa_cve_2017_5715 341fe007b2eSDimitris Papastamos 342fe007b2eSDimitris Papastamos/* 343fe007b2eSDimitris Papastamos * void *wa_cve_2018_3639_get_disable_ptr(void); 344fe007b2eSDimitris Papastamos * 345fe007b2eSDimitris Papastamos * Returns a function pointer which is used to disable mitigation 346fe007b2eSDimitris Papastamos * for CVE-2018-3639. 347fe007b2eSDimitris Papastamos * The function pointer is only returned on cores that employ 348fe007b2eSDimitris Papastamos * dynamic mitigation. If the core uses static mitigation or is 349fe007b2eSDimitris Papastamos * unaffected by CVE-2018-3639 this function returns NULL. 350fe007b2eSDimitris Papastamos * 351fe007b2eSDimitris Papastamos * NOTE: Must be called only after cpu_ops have been initialized 352fe007b2eSDimitris Papastamos * in per-CPU data. 353fe007b2eSDimitris Papastamos */ 354fe007b2eSDimitris Papastamos .globl wa_cve_2018_3639_get_disable_ptr 355fe007b2eSDimitris Papastamosfunc wa_cve_2018_3639_get_disable_ptr 356fe007b2eSDimitris Papastamos mrs x0, tpidr_el3 357fe007b2eSDimitris Papastamos#if ENABLE_ASSERTIONS 358fe007b2eSDimitris Papastamos cmp x0, #0 359fe007b2eSDimitris Papastamos ASM_ASSERT(ne) 360fe007b2eSDimitris Papastamos#endif 361fe007b2eSDimitris Papastamos ldr x0, [x0, #CPU_DATA_CPU_OPS_PTR] 362fe007b2eSDimitris Papastamos ldr x0, [x0, #CPU_EXTRA2_FUNC] 363fe007b2eSDimitris Papastamos ret 364fe007b2eSDimitris Papastamosendfunc wa_cve_2018_3639_get_disable_ptr 365