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