1add40351SSoby Mathew/* 2*ff6f62e1SAntonio Nino Diaz * Copyright (c) 2014-2019, ARM Limited and Contributors. All rights reserved. 3add40351SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5add40351SSoby Mathew */ 6c3cf06f1SAntonio Nino Diaz#ifndef CPU_MACROS_S 7c3cf06f1SAntonio Nino Diaz#define CPU_MACROS_S 8add40351SSoby Mathew 9add40351SSoby Mathew#include <arch.h> 10*ff6f62e1SAntonio Nino Diaz#include <assert_macros.S> 1109d40e0eSAntonio Nino Diaz#include <lib/cpus/errata_report.h> 12add40351SSoby Mathew 13add40351SSoby Mathew#define CPU_IMPL_PN_MASK (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \ 14add40351SSoby Mathew (MIDR_PN_MASK << MIDR_PN_SHIFT) 15add40351SSoby Mathew 165dd9dbb5SJeenu Viswambharan/* The number of CPU operations allowed */ 175dd9dbb5SJeenu Viswambharan#define CPU_MAX_PWR_DWN_OPS 2 185dd9dbb5SJeenu Viswambharan 195dd9dbb5SJeenu Viswambharan/* Special constant to specify that CPU has no reset function */ 205dd9dbb5SJeenu Viswambharan#define CPU_NO_RESET_FUNC 0 215dd9dbb5SJeenu Viswambharan 22fe007b2eSDimitris Papastamos#define CPU_NO_EXTRA1_FUNC 0 23fe007b2eSDimitris Papastamos#define CPU_NO_EXTRA2_FUNC 0 24fe007b2eSDimitris Papastamos 255dd9dbb5SJeenu Viswambharan/* Word size for 64-bit CPUs */ 265dd9dbb5SJeenu Viswambharan#define CPU_WORD_SIZE 8 275dd9dbb5SJeenu Viswambharan 28b1d27b48SRoberto Vargas#if defined(IMAGE_BL1) || defined(IMAGE_BL31) ||(defined(IMAGE_BL2) && BL2_AT_EL3) 29b1d27b48SRoberto Vargas#define IMAGE_AT_EL3 30b1d27b48SRoberto Vargas#endif 31b1d27b48SRoberto Vargas 32add40351SSoby Mathew/* 3310bcd761SJeenu Viswambharan * Whether errata status needs reporting. Errata status is printed in debug 3410bcd761SJeenu Viswambharan * builds for both BL1 and BL31 images. 3510bcd761SJeenu Viswambharan */ 3610bcd761SJeenu Viswambharan#if (defined(IMAGE_BL1) || defined(IMAGE_BL31)) && DEBUG 3710bcd761SJeenu Viswambharan# define REPORT_ERRATA 1 3810bcd761SJeenu Viswambharan#else 3910bcd761SJeenu Viswambharan# define REPORT_ERRATA 0 4010bcd761SJeenu Viswambharan#endif 4110bcd761SJeenu Viswambharan 42f21b9f6dSRoberto Vargas 43f21b9f6dSRoberto Vargas .equ CPU_MIDR_SIZE, CPU_WORD_SIZE 44f21b9f6dSRoberto Vargas .equ CPU_EXTRA1_FUNC_SIZE, CPU_WORD_SIZE 45f21b9f6dSRoberto Vargas .equ CPU_EXTRA2_FUNC_SIZE, CPU_WORD_SIZE 46f21b9f6dSRoberto Vargas .equ CPU_RESET_FUNC_SIZE, CPU_WORD_SIZE 47f21b9f6dSRoberto Vargas .equ CPU_PWR_DWN_OPS_SIZE, CPU_WORD_SIZE * CPU_MAX_PWR_DWN_OPS 48f21b9f6dSRoberto Vargas .equ CPU_ERRATA_FUNC_SIZE, CPU_WORD_SIZE 49f21b9f6dSRoberto Vargas .equ CPU_ERRATA_LOCK_SIZE, CPU_WORD_SIZE 50f21b9f6dSRoberto Vargas .equ CPU_ERRATA_PRINTED_SIZE, CPU_WORD_SIZE 51f21b9f6dSRoberto Vargas .equ CPU_REG_DUMP_SIZE, CPU_WORD_SIZE 52f21b9f6dSRoberto Vargas 53f21b9f6dSRoberto Vargas#ifndef IMAGE_AT_EL3 54f21b9f6dSRoberto Vargas .equ CPU_RESET_FUNC_SIZE, 0 55f21b9f6dSRoberto Vargas#endif 56f21b9f6dSRoberto Vargas 57f21b9f6dSRoberto Vargas/* The power down core and cluster is needed only in BL31 */ 58f21b9f6dSRoberto Vargas#ifndef IMAGE_BL31 59f21b9f6dSRoberto Vargas .equ CPU_PWR_DWN_OPS_SIZE, 0 60f21b9f6dSRoberto Vargas#endif 61f21b9f6dSRoberto Vargas 62f21b9f6dSRoberto Vargas/* Fields required to print errata status. */ 63f21b9f6dSRoberto Vargas#if !REPORT_ERRATA 64f21b9f6dSRoberto Vargas .equ CPU_ERRATA_FUNC_SIZE, 0 65f21b9f6dSRoberto Vargas#endif 66f21b9f6dSRoberto Vargas 67f21b9f6dSRoberto Vargas/* Only BL31 requieres mutual exclusion and printed flag. */ 68f21b9f6dSRoberto Vargas#if !(REPORT_ERRATA && defined(IMAGE_BL31)) 69f21b9f6dSRoberto Vargas .equ CPU_ERRATA_LOCK_SIZE, 0 70f21b9f6dSRoberto Vargas .equ CPU_ERRATA_PRINTED_SIZE, 0 71f21b9f6dSRoberto Vargas#endif 72f21b9f6dSRoberto Vargas 73f21b9f6dSRoberto Vargas#if !defined(IMAGE_BL31) || !CRASH_REPORTING 74f21b9f6dSRoberto Vargas .equ CPU_REG_DUMP_SIZE, 0 75f21b9f6dSRoberto Vargas#endif 76f21b9f6dSRoberto Vargas 7710bcd761SJeenu Viswambharan/* 78add40351SSoby Mathew * Define the offsets to the fields in cpu_ops structure. 79f21b9f6dSRoberto Vargas * Every offset is defined based in the offset and size of the previous 80f21b9f6dSRoberto Vargas * field. 81add40351SSoby Mathew */ 82f21b9f6dSRoberto Vargas .equ CPU_MIDR, 0 83f21b9f6dSRoberto Vargas .equ CPU_RESET_FUNC, CPU_MIDR + CPU_MIDR_SIZE 84f21b9f6dSRoberto Vargas .equ CPU_EXTRA1_FUNC, CPU_RESET_FUNC + CPU_RESET_FUNC_SIZE 85f21b9f6dSRoberto Vargas .equ CPU_EXTRA2_FUNC, CPU_EXTRA1_FUNC + CPU_EXTRA1_FUNC_SIZE 86f21b9f6dSRoberto Vargas .equ CPU_PWR_DWN_OPS, CPU_EXTRA2_FUNC + CPU_EXTRA2_FUNC_SIZE 87f21b9f6dSRoberto Vargas .equ CPU_ERRATA_FUNC, CPU_PWR_DWN_OPS + CPU_PWR_DWN_OPS_SIZE 88f21b9f6dSRoberto Vargas .equ CPU_ERRATA_LOCK, CPU_ERRATA_FUNC + CPU_ERRATA_FUNC_SIZE 89f21b9f6dSRoberto Vargas .equ CPU_ERRATA_PRINTED, CPU_ERRATA_LOCK + CPU_ERRATA_LOCK_SIZE 90f21b9f6dSRoberto Vargas .equ CPU_REG_DUMP, CPU_ERRATA_PRINTED + CPU_ERRATA_PRINTED_SIZE 91f21b9f6dSRoberto Vargas .equ CPU_OPS_SIZE, CPU_REG_DUMP + CPU_REG_DUMP_SIZE 92add40351SSoby Mathew 93add40351SSoby Mathew /* 945dd9dbb5SJeenu Viswambharan * Write given expressions as quad words 955dd9dbb5SJeenu Viswambharan * 965dd9dbb5SJeenu Viswambharan * _count: 975dd9dbb5SJeenu Viswambharan * Write at least _count quad words. If the given number of 985dd9dbb5SJeenu Viswambharan * expressions is less than _count, repeat the last expression to 995dd9dbb5SJeenu Viswambharan * fill _count quad words in total 1005dd9dbb5SJeenu Viswambharan * _rest: 1015dd9dbb5SJeenu Viswambharan * Optional list of expressions. _this is for parameter extraction 1025dd9dbb5SJeenu Viswambharan * only, and has no significance to the caller 1035dd9dbb5SJeenu Viswambharan * 1045dd9dbb5SJeenu Viswambharan * Invoked as: 1055dd9dbb5SJeenu Viswambharan * fill_constants 2, foo, bar, blah, ... 106add40351SSoby Mathew */ 1075dd9dbb5SJeenu Viswambharan .macro fill_constants _count:req, _this, _rest:vararg 1085dd9dbb5SJeenu Viswambharan .ifgt \_count 1095dd9dbb5SJeenu Viswambharan /* Write the current expression */ 1105dd9dbb5SJeenu Viswambharan .ifb \_this 1115dd9dbb5SJeenu Viswambharan .error "Nothing to fill" 1125dd9dbb5SJeenu Viswambharan .endif 1135dd9dbb5SJeenu Viswambharan .quad \_this 1145dd9dbb5SJeenu Viswambharan 1155dd9dbb5SJeenu Viswambharan /* Invoke recursively for remaining expressions */ 1165dd9dbb5SJeenu Viswambharan .ifnb \_rest 1175dd9dbb5SJeenu Viswambharan fill_constants \_count-1, \_rest 1185dd9dbb5SJeenu Viswambharan .else 1195dd9dbb5SJeenu Viswambharan fill_constants \_count-1, \_this 1205dd9dbb5SJeenu Viswambharan .endif 1215dd9dbb5SJeenu Viswambharan .endif 1225dd9dbb5SJeenu Viswambharan .endm 1235dd9dbb5SJeenu Viswambharan 1245dd9dbb5SJeenu Viswambharan /* 1255dd9dbb5SJeenu Viswambharan * Declare CPU operations 1265dd9dbb5SJeenu Viswambharan * 1275dd9dbb5SJeenu Viswambharan * _name: 1285dd9dbb5SJeenu Viswambharan * Name of the CPU for which operations are being specified 1295dd9dbb5SJeenu Viswambharan * _midr: 1305dd9dbb5SJeenu Viswambharan * Numeric value expected to read from CPU's MIDR 1315dd9dbb5SJeenu Viswambharan * _resetfunc: 1325dd9dbb5SJeenu Viswambharan * Reset function for the CPU. If there's no CPU reset function, 1335dd9dbb5SJeenu Viswambharan * specify CPU_NO_RESET_FUNC 134a205a56eSDimitris Papastamos * _extra1: 135a205a56eSDimitris Papastamos * This is a placeholder for future per CPU operations. Currently, 136a205a56eSDimitris Papastamos * some CPUs use this entry to set a test function to determine if 137a205a56eSDimitris Papastamos * the workaround for CVE-2017-5715 needs to be applied or not. 138fe007b2eSDimitris Papastamos * _extra2: 139fe007b2eSDimitris Papastamos * This is a placeholder for future per CPU operations. Currently 140fe007b2eSDimitris Papastamos * some CPUs use this entry to set a function to disable the 141fe007b2eSDimitris Papastamos * workaround for CVE-2018-3639. 1425dd9dbb5SJeenu Viswambharan * _power_down_ops: 1435dd9dbb5SJeenu Viswambharan * Comma-separated list of functions to perform power-down 1445dd9dbb5SJeenu Viswambharan * operatios on the CPU. At least one, and up to 1455dd9dbb5SJeenu Viswambharan * CPU_MAX_PWR_DWN_OPS number of functions may be specified. 1465dd9dbb5SJeenu Viswambharan * Starting at power level 0, these functions shall handle power 1475dd9dbb5SJeenu Viswambharan * down at subsequent power levels. If there aren't exactly 1485dd9dbb5SJeenu Viswambharan * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be 1495dd9dbb5SJeenu Viswambharan * used to handle power down at subsequent levels 1505dd9dbb5SJeenu Viswambharan */ 151a205a56eSDimitris Papastamos .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \ 152fe007b2eSDimitris Papastamos _extra1:req, _extra2:req, _power_down_ops:vararg 1535dd9dbb5SJeenu Viswambharan .section cpu_ops, "a" 1545dd9dbb5SJeenu Viswambharan .align 3 155add40351SSoby Mathew .type cpu_ops_\_name, %object 156add40351SSoby Mathew .quad \_midr 157b1d27b48SRoberto Vargas#if defined(IMAGE_AT_EL3) 1585dd9dbb5SJeenu Viswambharan .quad \_resetfunc 159add40351SSoby Mathew#endif 160a205a56eSDimitris Papastamos .quad \_extra1 161fe007b2eSDimitris Papastamos .quad \_extra2 1623d8256b2SMasahiro Yamada#ifdef IMAGE_BL31 1635dd9dbb5SJeenu Viswambharan /* Insert list of functions */ 1645dd9dbb5SJeenu Viswambharan fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops 165add40351SSoby Mathew#endif 16610bcd761SJeenu Viswambharan 16710bcd761SJeenu Viswambharan#if REPORT_ERRATA 16810bcd761SJeenu Viswambharan .ifndef \_name\()_cpu_str 16910bcd761SJeenu Viswambharan /* 17010bcd761SJeenu Viswambharan * Place errata reported flag, and the spinlock to arbitrate access to 17110bcd761SJeenu Viswambharan * it in the data section. 17210bcd761SJeenu Viswambharan */ 17310bcd761SJeenu Viswambharan .pushsection .data 17410bcd761SJeenu Viswambharan define_asm_spinlock \_name\()_errata_lock 17510bcd761SJeenu Viswambharan \_name\()_errata_reported: 17610bcd761SJeenu Viswambharan .word 0 17710bcd761SJeenu Viswambharan .popsection 17810bcd761SJeenu Viswambharan 17910bcd761SJeenu Viswambharan /* Place CPU string in rodata */ 18010bcd761SJeenu Viswambharan .pushsection .rodata 18110bcd761SJeenu Viswambharan \_name\()_cpu_str: 18210bcd761SJeenu Viswambharan .asciz "\_name" 18310bcd761SJeenu Viswambharan .popsection 18410bcd761SJeenu Viswambharan .endif 18510bcd761SJeenu Viswambharan 18610bcd761SJeenu Viswambharan /* 18712af5ed4SSoby Mathew * Mandatory errata status printing function for CPUs of 18810bcd761SJeenu Viswambharan * this class. 18910bcd761SJeenu Viswambharan */ 19010bcd761SJeenu Viswambharan .quad \_name\()_errata_report 19110bcd761SJeenu Viswambharan 19210bcd761SJeenu Viswambharan#ifdef IMAGE_BL31 19310bcd761SJeenu Viswambharan /* Pointers to errata lock and reported flag */ 19410bcd761SJeenu Viswambharan .quad \_name\()_errata_lock 19510bcd761SJeenu Viswambharan .quad \_name\()_errata_reported 19610bcd761SJeenu Viswambharan#endif 19710bcd761SJeenu Viswambharan#endif 19810bcd761SJeenu Viswambharan 1993d8256b2SMasahiro Yamada#if defined(IMAGE_BL31) && CRASH_REPORTING 200d3f70af6SSoby Mathew .quad \_name\()_cpu_reg_dump 201d3f70af6SSoby Mathew#endif 202add40351SSoby Mathew .endm 203e2bf57f8SDan Handley 204a205a56eSDimitris Papastamos .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \ 205a205a56eSDimitris Papastamos _power_down_ops:vararg 206fe007b2eSDimitris Papastamos declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, \ 207a205a56eSDimitris Papastamos \_power_down_ops 208a205a56eSDimitris Papastamos .endm 209a205a56eSDimitris Papastamos 210fe007b2eSDimitris Papastamos .macro declare_cpu_ops_wa _name:req, _midr:req, \ 211fe007b2eSDimitris Papastamos _resetfunc:req, _extra1:req, _extra2:req, \ 212fe007b2eSDimitris Papastamos _power_down_ops:vararg 213a205a56eSDimitris Papastamos declare_cpu_ops_base \_name, \_midr, \_resetfunc, \ 214fe007b2eSDimitris Papastamos \_extra1, \_extra2, \_power_down_ops 215a205a56eSDimitris Papastamos .endm 216a205a56eSDimitris Papastamos 21710bcd761SJeenu Viswambharan#if REPORT_ERRATA 21810bcd761SJeenu Viswambharan /* 21910bcd761SJeenu Viswambharan * Print status of a CPU errata 22010bcd761SJeenu Viswambharan * 22110bcd761SJeenu Viswambharan * _chosen: 22210bcd761SJeenu Viswambharan * Identifier indicating whether or not a CPU errata has been 22310bcd761SJeenu Viswambharan * compiled in. 22410bcd761SJeenu Viswambharan * _cpu: 22510bcd761SJeenu Viswambharan * Name of the CPU 22610bcd761SJeenu Viswambharan * _id: 22710bcd761SJeenu Viswambharan * Errata identifier 22810bcd761SJeenu Viswambharan * _rev_var: 22910bcd761SJeenu Viswambharan * Register containing the combined value CPU revision and variant 23010bcd761SJeenu Viswambharan * - typically the return value of cpu_get_rev_var 23110bcd761SJeenu Viswambharan */ 23210bcd761SJeenu Viswambharan .macro report_errata _chosen, _cpu, _id, _rev_var=x8 23310bcd761SJeenu Viswambharan /* Stash a string with errata ID */ 23410bcd761SJeenu Viswambharan .pushsection .rodata 23510bcd761SJeenu Viswambharan \_cpu\()_errata_\_id\()_str: 23610bcd761SJeenu Viswambharan .asciz "\_id" 23710bcd761SJeenu Viswambharan .popsection 23810bcd761SJeenu Viswambharan 23910bcd761SJeenu Viswambharan /* Check whether errata applies */ 24010bcd761SJeenu Viswambharan mov x0, \_rev_var 2419ec3921cSJonathan Wright /* Shall clobber: x0-x7 */ 24210bcd761SJeenu Viswambharan bl check_errata_\_id 24310bcd761SJeenu Viswambharan 24410bcd761SJeenu Viswambharan .ifeq \_chosen 24510bcd761SJeenu Viswambharan /* 24610bcd761SJeenu Viswambharan * Errata workaround has not been compiled in. If the errata would have 24710bcd761SJeenu Viswambharan * applied had it been compiled in, print its status as missing. 24810bcd761SJeenu Viswambharan */ 24910bcd761SJeenu Viswambharan cbz x0, 900f 25010bcd761SJeenu Viswambharan mov x0, #ERRATA_MISSING 25110bcd761SJeenu Viswambharan .endif 25210bcd761SJeenu Viswambharan900: 25310bcd761SJeenu Viswambharan adr x1, \_cpu\()_cpu_str 25410bcd761SJeenu Viswambharan adr x2, \_cpu\()_errata_\_id\()_str 25510bcd761SJeenu Viswambharan bl errata_print_msg 25610bcd761SJeenu Viswambharan .endm 25710bcd761SJeenu Viswambharan#endif 25810bcd761SJeenu Viswambharan 2593991a6a4SDimitris Papastamos /* 2603991a6a4SDimitris Papastamos * This macro is used on some CPUs to detect if they are vulnerable 2613991a6a4SDimitris Papastamos * to CVE-2017-5715. 2623991a6a4SDimitris Papastamos */ 2633991a6a4SDimitris Papastamos .macro cpu_check_csv2 _reg _label 2643991a6a4SDimitris Papastamos mrs \_reg, id_aa64pfr0_el1 2653991a6a4SDimitris Papastamos ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH 2663991a6a4SDimitris Papastamos /* 267*ff6f62e1SAntonio Nino Diaz * If the field equals 1, branch targets trained in one context cannot 268*ff6f62e1SAntonio Nino Diaz * affect speculative execution in a different context. 269*ff6f62e1SAntonio Nino Diaz * 270*ff6f62e1SAntonio Nino Diaz * If the field equals 2, it means that the system is also aware of 271*ff6f62e1SAntonio Nino Diaz * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we 272*ff6f62e1SAntonio Nino Diaz * expect users of the registers to do the right thing. 273*ff6f62e1SAntonio Nino Diaz * 274*ff6f62e1SAntonio Nino Diaz * Only apply mitigations if the value of this field is 0. 2753991a6a4SDimitris Papastamos */ 276*ff6f62e1SAntonio Nino Diaz#if ENABLE_ASSERTIONS 277*ff6f62e1SAntonio Nino Diaz cmp \_reg, #3 /* Only values 0 to 2 are expected */ 278*ff6f62e1SAntonio Nino Diaz ASM_ASSERT(lo) 279*ff6f62e1SAntonio Nino Diaz#endif 280*ff6f62e1SAntonio Nino Diaz 281*ff6f62e1SAntonio Nino Diaz cmp \_reg, #0 282*ff6f62e1SAntonio Nino Diaz bne \_label 2833991a6a4SDimitris Papastamos .endm 284da3b038fSDeepak Pandey 285da3b038fSDeepak Pandey /* 286da3b038fSDeepak Pandey * Helper macro that reads the part number of the current 287da3b038fSDeepak Pandey * CPU and jumps to the given label if it matches the CPU 288da3b038fSDeepak Pandey * MIDR provided. 289da3b038fSDeepak Pandey * 290da3b038fSDeepak Pandey * Clobbers x0. 291da3b038fSDeepak Pandey */ 292da3b038fSDeepak Pandey .macro jump_if_cpu_midr _cpu_midr, _label 293da3b038fSDeepak Pandey mrs x0, midr_el1 294da3b038fSDeepak Pandey ubfx x0, x0, MIDR_PN_SHIFT, #12 295da3b038fSDeepak Pandey cmp w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK) 296da3b038fSDeepak Pandey b.eq \_label 297da3b038fSDeepak Pandey .endm 298c3cf06f1SAntonio Nino Diaz 299c3cf06f1SAntonio Nino Diaz#endif /* CPU_MACROS_S */ 300