1e33b78a6SSoby Mathew/* 2e33b78a6SSoby Mathew * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3e33b78a6SSoby Mathew * 4e33b78a6SSoby Mathew * Redistribution and use in source and binary forms, with or without 5e33b78a6SSoby Mathew * modification, are permitted provided that the following conditions are met: 6e33b78a6SSoby Mathew * 7e33b78a6SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8e33b78a6SSoby Mathew * list of conditions and the following disclaimer. 9e33b78a6SSoby Mathew * 10e33b78a6SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11e33b78a6SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12e33b78a6SSoby Mathew * and/or other materials provided with the distribution. 13e33b78a6SSoby Mathew * 14e33b78a6SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15e33b78a6SSoby Mathew * to endorse or promote products derived from this software without specific 16e33b78a6SSoby Mathew * prior written permission. 17e33b78a6SSoby Mathew * 18e33b78a6SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19e33b78a6SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20e33b78a6SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21e33b78a6SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22e33b78a6SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23e33b78a6SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24e33b78a6SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25e33b78a6SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26e33b78a6SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27e33b78a6SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28e33b78a6SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29e33b78a6SSoby Mathew */ 30e33b78a6SSoby Mathew 31e33b78a6SSoby Mathew#include <arch.h> 32e33b78a6SSoby Mathew#include <asm_macros.S> 33e33b78a6SSoby Mathew#include <assert_macros.S> 34e33b78a6SSoby Mathew#include <cpu_data.h> 35e33b78a6SSoby Mathew#include <cpu_macros.S> 36e33b78a6SSoby Mathew 371a0a3f06SYatharth Kochar#if IMAGE_BL1 || IMAGE_BL32 38e33b78a6SSoby Mathew /* 39e33b78a6SSoby Mathew * The reset handler common to all platforms. After a matching 40e33b78a6SSoby Mathew * cpu_ops structure entry is found, the correponding reset_handler 41e33b78a6SSoby Mathew * in the cpu_ops is invoked. The reset handler is invoked very early 42e33b78a6SSoby Mathew * in the boot sequence and it is assumed that we can clobber r0 - r10 43e33b78a6SSoby Mathew * without the need to follow AAPCS. 44e33b78a6SSoby Mathew * Clobbers: r0 - r10 45e33b78a6SSoby Mathew */ 46e33b78a6SSoby Mathew .globl reset_handler 47e33b78a6SSoby Mathewfunc reset_handler 48e33b78a6SSoby Mathew mov r10, lr 49e33b78a6SSoby Mathew 50e33b78a6SSoby Mathew /* The plat_reset_handler can clobber r0 - r9 */ 51e33b78a6SSoby Mathew bl plat_reset_handler 52e33b78a6SSoby Mathew 53e33b78a6SSoby Mathew /* Get the matching cpu_ops pointer (clobbers: r0 - r5) */ 54e33b78a6SSoby Mathew bl get_cpu_ops_ptr 55e33b78a6SSoby Mathew 56e33b78a6SSoby Mathew#if ASM_ASSERTION 57e33b78a6SSoby Mathew cmp r0, #0 58e33b78a6SSoby Mathew ASM_ASSERT(ne) 59e33b78a6SSoby Mathew#endif 60e33b78a6SSoby Mathew 61e33b78a6SSoby Mathew /* Get the cpu_ops reset handler */ 62e33b78a6SSoby Mathew ldr r1, [r0, #CPU_RESET_FUNC] 63e33b78a6SSoby Mathew cmp r1, #0 64e33b78a6SSoby Mathew mov lr, r10 65e33b78a6SSoby Mathew bxne r1 66e33b78a6SSoby Mathew bx lr 67e33b78a6SSoby Mathewendfunc reset_handler 68e33b78a6SSoby Mathew 691a0a3f06SYatharth Kochar#endif /* IMAGE_BL1 || IMAGE_BL32 */ 701a0a3f06SYatharth Kochar 711a0a3f06SYatharth Kochar#if IMAGE_BL32 /* The power down core and cluster is needed only in BL32 */ 72e33b78a6SSoby Mathew /* 73*5dd9dbb5SJeenu Viswambharan * void prepare_cpu_pwr_dwn(unsigned int power_level) 74*5dd9dbb5SJeenu Viswambharan * 75*5dd9dbb5SJeenu Viswambharan * Prepare CPU power down function for all platforms. The function takes 76*5dd9dbb5SJeenu Viswambharan * a domain level to be powered down as its parameter. After the cpu_ops 77*5dd9dbb5SJeenu Viswambharan * pointer is retrieved from cpu_data, the handler for requested power 78*5dd9dbb5SJeenu Viswambharan * level is called. 79e33b78a6SSoby Mathew */ 80*5dd9dbb5SJeenu Viswambharan .globl prepare_cpu_pwr_dwn 81*5dd9dbb5SJeenu Viswambharanfunc prepare_cpu_pwr_dwn 82e33b78a6SSoby Mathew /* 83*5dd9dbb5SJeenu Viswambharan * If the given power level exceeds CPU_MAX_PWR_DWN_OPS, we call the 84*5dd9dbb5SJeenu Viswambharan * power down handler for the last power level 85e33b78a6SSoby Mathew */ 86*5dd9dbb5SJeenu Viswambharan mov r2, #(CPU_MAX_PWR_DWN_OPS - 1) 87*5dd9dbb5SJeenu Viswambharan cmp r0, r2 88*5dd9dbb5SJeenu Viswambharan movhi r0, r2 89e33b78a6SSoby Mathew 90*5dd9dbb5SJeenu Viswambharan push {r0, lr} 91*5dd9dbb5SJeenu Viswambharan bl _cpu_data 92*5dd9dbb5SJeenu Viswambharan pop {r2, lr} 93*5dd9dbb5SJeenu Viswambharan 94*5dd9dbb5SJeenu Viswambharan ldr r0, [r0, #CPU_DATA_CPU_OPS_PTR] 95e33b78a6SSoby Mathew#if ASM_ASSERTION 96*5dd9dbb5SJeenu Viswambharan cmp r0, #0 97e33b78a6SSoby Mathew ASM_ASSERT(ne) 98e33b78a6SSoby Mathew#endif 99e33b78a6SSoby Mathew 100*5dd9dbb5SJeenu Viswambharan /* Get the appropriate power down handler */ 101*5dd9dbb5SJeenu Viswambharan mov r1, #CPU_PWR_DWN_OPS 102*5dd9dbb5SJeenu Viswambharan add r1, r1, r2, lsl #2 103*5dd9dbb5SJeenu Viswambharan ldr r1, [r0, r1] 104*5dd9dbb5SJeenu Viswambharan bx r1 105*5dd9dbb5SJeenu Viswambharanendfunc prepare_cpu_pwr_dwn 106e33b78a6SSoby Mathew 107e33b78a6SSoby Mathew /* 108e33b78a6SSoby Mathew * Initializes the cpu_ops_ptr if not already initialized 109e33b78a6SSoby Mathew * in cpu_data. This must only be called after the data cache 110e33b78a6SSoby Mathew * is enabled. AAPCS is followed. 111e33b78a6SSoby Mathew */ 112e33b78a6SSoby Mathew .globl init_cpu_ops 113e33b78a6SSoby Mathewfunc init_cpu_ops 114e33b78a6SSoby Mathew push {r4 - r6, lr} 115e33b78a6SSoby Mathew bl _cpu_data 116e33b78a6SSoby Mathew mov r6, r0 117e33b78a6SSoby Mathew ldr r1, [r0, #CPU_DATA_CPU_OPS_PTR] 118e33b78a6SSoby Mathew cmp r1, #0 119e33b78a6SSoby Mathew bne 1f 120e33b78a6SSoby Mathew bl get_cpu_ops_ptr 121e33b78a6SSoby Mathew#if ASM_ASSERTION 122e33b78a6SSoby Mathew cmp r0, #0 123e33b78a6SSoby Mathew ASM_ASSERT(ne) 124e33b78a6SSoby Mathew#endif 125e33b78a6SSoby Mathew str r0, [r6, #CPU_DATA_CPU_OPS_PTR]! 126e33b78a6SSoby Mathew1: 127e33b78a6SSoby Mathew pop {r4 - r6, pc} 128e33b78a6SSoby Mathewendfunc init_cpu_ops 129e33b78a6SSoby Mathew 1301a0a3f06SYatharth Kochar#endif /* IMAGE_BL32 */ 1311a0a3f06SYatharth Kochar 132e33b78a6SSoby Mathew /* 133e33b78a6SSoby Mathew * The below function returns the cpu_ops structure matching the 134e33b78a6SSoby Mathew * midr of the core. It reads the MIDR and finds the matching 135e33b78a6SSoby Mathew * entry in cpu_ops entries. Only the implementation and part number 136e33b78a6SSoby Mathew * are used to match the entries. 137e33b78a6SSoby Mathew * Return : 138e33b78a6SSoby Mathew * r0 - The matching cpu_ops pointer on Success 139e33b78a6SSoby Mathew * r0 - 0 on failure. 140e33b78a6SSoby Mathew * Clobbers: r0 - r5 141e33b78a6SSoby Mathew */ 142e33b78a6SSoby Mathew .globl get_cpu_ops_ptr 143e33b78a6SSoby Mathewfunc get_cpu_ops_ptr 144e33b78a6SSoby Mathew /* Get the cpu_ops start and end locations */ 145e33b78a6SSoby Mathew ldr r4, =(__CPU_OPS_START__ + CPU_MIDR) 146e33b78a6SSoby Mathew ldr r5, =(__CPU_OPS_END__ + CPU_MIDR) 147e33b78a6SSoby Mathew 148e33b78a6SSoby Mathew /* Initialize the return parameter */ 149e33b78a6SSoby Mathew mov r0, #0 150e33b78a6SSoby Mathew 151e33b78a6SSoby Mathew /* Read the MIDR_EL1 */ 152e33b78a6SSoby Mathew ldcopr r2, MIDR 153e33b78a6SSoby Mathew ldr r3, =CPU_IMPL_PN_MASK 154e33b78a6SSoby Mathew 155e33b78a6SSoby Mathew /* Retain only the implementation and part number using mask */ 156e33b78a6SSoby Mathew and r2, r2, r3 157e33b78a6SSoby Mathew1: 158e33b78a6SSoby Mathew /* Check if we have reached end of list */ 159e33b78a6SSoby Mathew cmp r4, r5 160e33b78a6SSoby Mathew bge error_exit 161e33b78a6SSoby Mathew 162e33b78a6SSoby Mathew /* load the midr from the cpu_ops */ 163e33b78a6SSoby Mathew ldr r1, [r4], #CPU_OPS_SIZE 164e33b78a6SSoby Mathew and r1, r1, r3 165e33b78a6SSoby Mathew 166e33b78a6SSoby Mathew /* Check if midr matches to midr of this core */ 167e33b78a6SSoby Mathew cmp r1, r2 168e33b78a6SSoby Mathew bne 1b 169e33b78a6SSoby Mathew 170e33b78a6SSoby Mathew /* Subtract the increment and offset to get the cpu-ops pointer */ 171e33b78a6SSoby Mathew sub r0, r4, #(CPU_OPS_SIZE + CPU_MIDR) 172e33b78a6SSoby Mathewerror_exit: 173e33b78a6SSoby Mathew bx lr 174e33b78a6SSoby Mathewendfunc get_cpu_ops_ptr 175