1e33b78a6SSoby Mathew/* 2*42d4d3baSArvind Ram Prakash * Copyright (c) 2016-2023, ARM Limited and Contributors. All rights reserved. 3e33b78a6SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5e33b78a6SSoby Mathew */ 6c3cf06f1SAntonio Nino Diaz#ifndef CPU_MACROS_S 7c3cf06f1SAntonio Nino Diaz#define CPU_MACROS_S 8e33b78a6SSoby Mathew 9e33b78a6SSoby Mathew#include <arch.h> 1009d40e0eSAntonio Nino Diaz#include <lib/cpus/errata_report.h> 11e33b78a6SSoby Mathew 12*42d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) || defined(IMAGE_BL32) \ 13*42d4d3baSArvind Ram Prakash || (defined(IMAGE_BL2) && RESET_TO_BL2) 14b1d27b48SRoberto Vargas#define IMAGE_AT_EL3 15b1d27b48SRoberto Vargas#endif 16b1d27b48SRoberto Vargas 17e33b78a6SSoby Mathew#define CPU_IMPL_PN_MASK (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \ 18e33b78a6SSoby Mathew (MIDR_PN_MASK << MIDR_PN_SHIFT) 19e33b78a6SSoby Mathew 205dd9dbb5SJeenu Viswambharan/* The number of CPU operations allowed */ 215dd9dbb5SJeenu Viswambharan#define CPU_MAX_PWR_DWN_OPS 2 225dd9dbb5SJeenu Viswambharan 235dd9dbb5SJeenu Viswambharan/* Special constant to specify that CPU has no reset function */ 245dd9dbb5SJeenu Viswambharan#define CPU_NO_RESET_FUNC 0 255dd9dbb5SJeenu Viswambharan 265dd9dbb5SJeenu Viswambharan/* Word size for 32-bit CPUs */ 275dd9dbb5SJeenu Viswambharan#define CPU_WORD_SIZE 4 285dd9dbb5SJeenu Viswambharan 29e33b78a6SSoby Mathew/* 3010bcd761SJeenu Viswambharan * Whether errata status needs reporting. Errata status is printed in debug 3110bcd761SJeenu Viswambharan * builds for both BL1 and BL32 images. 3210bcd761SJeenu Viswambharan */ 3310bcd761SJeenu Viswambharan#if (defined(IMAGE_BL1) || defined(IMAGE_BL32)) && DEBUG 3410bcd761SJeenu Viswambharan# define REPORT_ERRATA 1 3510bcd761SJeenu Viswambharan#else 3610bcd761SJeenu Viswambharan# define REPORT_ERRATA 0 3710bcd761SJeenu Viswambharan#endif 3810bcd761SJeenu Viswambharan 39f21b9f6dSRoberto Vargas 40f21b9f6dSRoberto Vargas .equ CPU_MIDR_SIZE, CPU_WORD_SIZE 41f21b9f6dSRoberto Vargas .equ CPU_RESET_FUNC_SIZE, CPU_WORD_SIZE 42f21b9f6dSRoberto Vargas .equ CPU_PWR_DWN_OPS_SIZE, CPU_WORD_SIZE * CPU_MAX_PWR_DWN_OPS 43f21b9f6dSRoberto Vargas .equ CPU_ERRATA_FUNC_SIZE, CPU_WORD_SIZE 44f21b9f6dSRoberto Vargas .equ CPU_ERRATA_LOCK_SIZE, CPU_WORD_SIZE 45f21b9f6dSRoberto Vargas .equ CPU_ERRATA_PRINTED_SIZE, CPU_WORD_SIZE 46f21b9f6dSRoberto Vargas 47f21b9f6dSRoberto Vargas#ifndef IMAGE_AT_EL3 48f21b9f6dSRoberto Vargas .equ CPU_RESET_FUNC_SIZE, 0 49f21b9f6dSRoberto Vargas#endif 50f21b9f6dSRoberto Vargas 51f21b9f6dSRoberto Vargas/* The power down core and cluster is needed only in BL32 */ 52f21b9f6dSRoberto Vargas#ifndef IMAGE_BL32 53f21b9f6dSRoberto Vargas .equ CPU_PWR_DWN_OPS_SIZE, 0 54f21b9f6dSRoberto Vargas#endif 55f21b9f6dSRoberto Vargas 56f21b9f6dSRoberto Vargas/* Fields required to print errata status */ 57f21b9f6dSRoberto Vargas#if !REPORT_ERRATA 58f21b9f6dSRoberto Vargas .equ CPU_ERRATA_FUNC_SIZE, 0 59f21b9f6dSRoberto Vargas#endif 60f21b9f6dSRoberto Vargas 61f21b9f6dSRoberto Vargas/* Only BL32 requires mutual exclusion and printed flag. */ 62f21b9f6dSRoberto Vargas#if !(REPORT_ERRATA && defined(IMAGE_BL32)) 63f21b9f6dSRoberto Vargas .equ CPU_ERRATA_LOCK_SIZE, 0 64f21b9f6dSRoberto Vargas .equ CPU_ERRATA_PRINTED_SIZE, 0 65f21b9f6dSRoberto Vargas#endif 66f21b9f6dSRoberto Vargas 67f21b9f6dSRoberto Vargas 6810bcd761SJeenu Viswambharan/* 69e33b78a6SSoby Mathew * Define the offsets to the fields in cpu_ops structure. 70f21b9f6dSRoberto Vargas * Every offset is defined based on the offset and size of the previous 71f21b9f6dSRoberto Vargas * field. 72e33b78a6SSoby Mathew */ 73f21b9f6dSRoberto Vargas .equ CPU_MIDR, 0 74f21b9f6dSRoberto Vargas .equ CPU_RESET_FUNC, CPU_MIDR + CPU_MIDR_SIZE 75f21b9f6dSRoberto Vargas .equ CPU_PWR_DWN_OPS, CPU_RESET_FUNC + CPU_RESET_FUNC_SIZE 76f21b9f6dSRoberto Vargas .equ CPU_ERRATA_FUNC, CPU_PWR_DWN_OPS + CPU_PWR_DWN_OPS_SIZE 77f21b9f6dSRoberto Vargas .equ CPU_ERRATA_LOCK, CPU_ERRATA_FUNC + CPU_ERRATA_FUNC_SIZE 78f21b9f6dSRoberto Vargas .equ CPU_ERRATA_PRINTED, CPU_ERRATA_LOCK + CPU_ERRATA_LOCK_SIZE 79f21b9f6dSRoberto Vargas .equ CPU_OPS_SIZE, CPU_ERRATA_PRINTED + CPU_ERRATA_PRINTED_SIZE 80e33b78a6SSoby Mathew 81e33b78a6SSoby Mathew /* 825dd9dbb5SJeenu Viswambharan * Write given expressions as words 835dd9dbb5SJeenu Viswambharan * 845dd9dbb5SJeenu Viswambharan * _count: 855dd9dbb5SJeenu Viswambharan * Write at least _count words. If the given number of expressions 865dd9dbb5SJeenu Viswambharan * is less than _count, repeat the last expression to fill _count 875dd9dbb5SJeenu Viswambharan * words in total 885dd9dbb5SJeenu Viswambharan * _rest: 895dd9dbb5SJeenu Viswambharan * Optional list of expressions. _this is for parameter extraction 905dd9dbb5SJeenu Viswambharan * only, and has no significance to the caller 915dd9dbb5SJeenu Viswambharan * 925dd9dbb5SJeenu Viswambharan * Invoked as: 935dd9dbb5SJeenu Viswambharan * fill_constants 2, foo, bar, blah, ... 94e33b78a6SSoby Mathew */ 955dd9dbb5SJeenu Viswambharan .macro fill_constants _count:req, _this, _rest:vararg 965dd9dbb5SJeenu Viswambharan .ifgt \_count 975dd9dbb5SJeenu Viswambharan /* Write the current expression */ 985dd9dbb5SJeenu Viswambharan .ifb \_this 995dd9dbb5SJeenu Viswambharan .error "Nothing to fill" 1005dd9dbb5SJeenu Viswambharan .endif 1015dd9dbb5SJeenu Viswambharan .word \_this 1025dd9dbb5SJeenu Viswambharan 1035dd9dbb5SJeenu Viswambharan /* Invoke recursively for remaining expressions */ 1045dd9dbb5SJeenu Viswambharan .ifnb \_rest 1055dd9dbb5SJeenu Viswambharan fill_constants \_count-1, \_rest 1065dd9dbb5SJeenu Viswambharan .else 1075dd9dbb5SJeenu Viswambharan fill_constants \_count-1, \_this 1085dd9dbb5SJeenu Viswambharan .endif 1095dd9dbb5SJeenu Viswambharan .endif 1105dd9dbb5SJeenu Viswambharan .endm 1115dd9dbb5SJeenu Viswambharan 1125dd9dbb5SJeenu Viswambharan /* 1135dd9dbb5SJeenu Viswambharan * Declare CPU operations 1145dd9dbb5SJeenu Viswambharan * 1155dd9dbb5SJeenu Viswambharan * _name: 1165dd9dbb5SJeenu Viswambharan * Name of the CPU for which operations are being specified 1175dd9dbb5SJeenu Viswambharan * _midr: 1185dd9dbb5SJeenu Viswambharan * Numeric value expected to read from CPU's MIDR 1195dd9dbb5SJeenu Viswambharan * _resetfunc: 1205dd9dbb5SJeenu Viswambharan * Reset function for the CPU. If there's no CPU reset function, 1215dd9dbb5SJeenu Viswambharan * specify CPU_NO_RESET_FUNC 1225dd9dbb5SJeenu Viswambharan * _power_down_ops: 1235dd9dbb5SJeenu Viswambharan * Comma-separated list of functions to perform power-down 1245dd9dbb5SJeenu Viswambharan * operatios on the CPU. At least one, and up to 1255dd9dbb5SJeenu Viswambharan * CPU_MAX_PWR_DWN_OPS number of functions may be specified. 1265dd9dbb5SJeenu Viswambharan * Starting at power level 0, these functions shall handle power 1275dd9dbb5SJeenu Viswambharan * down at subsequent power levels. If there aren't exactly 1285dd9dbb5SJeenu Viswambharan * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be 1295dd9dbb5SJeenu Viswambharan * used to handle power down at subsequent levels 1305dd9dbb5SJeenu Viswambharan */ 1315dd9dbb5SJeenu Viswambharan .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \ 1325dd9dbb5SJeenu Viswambharan _power_down_ops:vararg 133da04341eSChris Kay .section .cpu_ops, "a" 134e33b78a6SSoby Mathew .align 2 135e33b78a6SSoby Mathew .type cpu_ops_\_name, %object 136e33b78a6SSoby Mathew .word \_midr 137b1d27b48SRoberto Vargas#if defined(IMAGE_AT_EL3) 1385dd9dbb5SJeenu Viswambharan .word \_resetfunc 1391a0a3f06SYatharth Kochar#endif 1403d8256b2SMasahiro Yamada#ifdef IMAGE_BL32 1415dd9dbb5SJeenu Viswambharan /* Insert list of functions */ 1425dd9dbb5SJeenu Viswambharan fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops 1431a0a3f06SYatharth Kochar#endif 14410bcd761SJeenu Viswambharan 14510bcd761SJeenu Viswambharan#if REPORT_ERRATA 14610bcd761SJeenu Viswambharan .ifndef \_name\()_cpu_str 14710bcd761SJeenu Viswambharan /* 14810bcd761SJeenu Viswambharan * Place errata reported flag, and the spinlock to arbitrate access to 14910bcd761SJeenu Viswambharan * it in the data section. 15010bcd761SJeenu Viswambharan */ 15110bcd761SJeenu Viswambharan .pushsection .data 15210bcd761SJeenu Viswambharan define_asm_spinlock \_name\()_errata_lock 15310bcd761SJeenu Viswambharan \_name\()_errata_reported: 15410bcd761SJeenu Viswambharan .word 0 15510bcd761SJeenu Viswambharan .popsection 15610bcd761SJeenu Viswambharan 15710bcd761SJeenu Viswambharan /* Place CPU string in rodata */ 15810bcd761SJeenu Viswambharan .pushsection .rodata 15910bcd761SJeenu Viswambharan \_name\()_cpu_str: 16010bcd761SJeenu Viswambharan .asciz "\_name" 16110bcd761SJeenu Viswambharan .popsection 16210bcd761SJeenu Viswambharan .endif 16310bcd761SJeenu Viswambharan 16410bcd761SJeenu Viswambharan /* 16512af5ed4SSoby Mathew * Mandatory errata status printing function for CPUs of 16610bcd761SJeenu Viswambharan * this class. 16710bcd761SJeenu Viswambharan */ 16810bcd761SJeenu Viswambharan .word \_name\()_errata_report 16910bcd761SJeenu Viswambharan 17010bcd761SJeenu Viswambharan#ifdef IMAGE_BL32 17110bcd761SJeenu Viswambharan /* Pointers to errata lock and reported flag */ 17210bcd761SJeenu Viswambharan .word \_name\()_errata_lock 17310bcd761SJeenu Viswambharan .word \_name\()_errata_reported 17410bcd761SJeenu Viswambharan#endif 17510bcd761SJeenu Viswambharan#endif 176e33b78a6SSoby Mathew .endm 177e33b78a6SSoby Mathew 17810bcd761SJeenu Viswambharan#if REPORT_ERRATA 17910bcd761SJeenu Viswambharan /* 18010bcd761SJeenu Viswambharan * Print status of a CPU errata 18110bcd761SJeenu Viswambharan * 18210bcd761SJeenu Viswambharan * _chosen: 18310bcd761SJeenu Viswambharan * Identifier indicating whether or not a CPU errata has been 18410bcd761SJeenu Viswambharan * compiled in. 18510bcd761SJeenu Viswambharan * _cpu: 18610bcd761SJeenu Viswambharan * Name of the CPU 18710bcd761SJeenu Viswambharan * _id: 18810bcd761SJeenu Viswambharan * Errata identifier 18910bcd761SJeenu Viswambharan * _rev_var: 19010bcd761SJeenu Viswambharan * Register containing the combined value CPU revision and variant 19110bcd761SJeenu Viswambharan * - typically the return value of cpu_get_rev_var 19210bcd761SJeenu Viswambharan */ 19310bcd761SJeenu Viswambharan .macro report_errata _chosen, _cpu, _id, _rev_var=r4 19410bcd761SJeenu Viswambharan /* Stash a string with errata ID */ 19510bcd761SJeenu Viswambharan .pushsection .rodata 19610bcd761SJeenu Viswambharan \_cpu\()_errata_\_id\()_str: 19710bcd761SJeenu Viswambharan .asciz "\_id" 19810bcd761SJeenu Viswambharan .popsection 19910bcd761SJeenu Viswambharan 20010bcd761SJeenu Viswambharan /* Check whether errata applies */ 20110bcd761SJeenu Viswambharan mov r0, \_rev_var 20210bcd761SJeenu Viswambharan bl check_errata_\_id 20310bcd761SJeenu Viswambharan 20410bcd761SJeenu Viswambharan .ifeq \_chosen 20510bcd761SJeenu Viswambharan /* 20610bcd761SJeenu Viswambharan * Errata workaround has not been compiled in. If the errata would have 20710bcd761SJeenu Viswambharan * applied had it been compiled in, print its status as missing. 20810bcd761SJeenu Viswambharan */ 20910bcd761SJeenu Viswambharan cmp r0, #0 21010bcd761SJeenu Viswambharan movne r0, #ERRATA_MISSING 21110bcd761SJeenu Viswambharan .endif 21210bcd761SJeenu Viswambharan ldr r1, =\_cpu\()_cpu_str 21310bcd761SJeenu Viswambharan ldr r2, =\_cpu\()_errata_\_id\()_str 21410bcd761SJeenu Viswambharan bl errata_print_msg 21510bcd761SJeenu Viswambharan .endm 21610bcd761SJeenu Viswambharan#endif 217da3b038fSDeepak Pandey /* 218da3b038fSDeepak Pandey * Helper macro that reads the part number of the current CPU and jumps 219da3b038fSDeepak Pandey * to the given label if it matches the CPU MIDR provided. 220da3b038fSDeepak Pandey * 221da3b038fSDeepak Pandey * Clobbers: r0-r1 222da3b038fSDeepak Pandey */ 223da3b038fSDeepak Pandey .macro jump_if_cpu_midr _cpu_midr, _label 224da3b038fSDeepak Pandey ldcopr r0, MIDR 225da3b038fSDeepak Pandey ubfx r0, r0, #MIDR_PN_SHIFT, #12 226da3b038fSDeepak Pandey ldr r1, =((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK) 227da3b038fSDeepak Pandey cmp r0, r1 228da3b038fSDeepak Pandey beq \_label 229da3b038fSDeepak Pandey .endm 23010bcd761SJeenu Viswambharan 231c3cf06f1SAntonio Nino Diaz#endif /* CPU_MACROS_S */ 232