1*bbf8f6f9SYatharth Kochar /* 2*bbf8f6f9SYatharth Kochar * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. 3*bbf8f6f9SYatharth Kochar * 4*bbf8f6f9SYatharth Kochar * Redistribution and use in source and binary forms, with or without 5*bbf8f6f9SYatharth Kochar * modification, are permitted provided that the following conditions are met: 6*bbf8f6f9SYatharth Kochar * 7*bbf8f6f9SYatharth Kochar * Redistributions of source code must retain the above copyright notice, this 8*bbf8f6f9SYatharth Kochar * list of conditions and the following disclaimer. 9*bbf8f6f9SYatharth Kochar * 10*bbf8f6f9SYatharth Kochar * Redistributions in binary form must reproduce the above copyright notice, 11*bbf8f6f9SYatharth Kochar * this list of conditions and the following disclaimer in the documentation 12*bbf8f6f9SYatharth Kochar * and/or other materials provided with the distribution. 13*bbf8f6f9SYatharth Kochar * 14*bbf8f6f9SYatharth Kochar * Neither the name of ARM nor the names of its contributors may be used 15*bbf8f6f9SYatharth Kochar * to endorse or promote products derived from this software without specific 16*bbf8f6f9SYatharth Kochar * prior written permission. 17*bbf8f6f9SYatharth Kochar * 18*bbf8f6f9SYatharth Kochar * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*bbf8f6f9SYatharth Kochar * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*bbf8f6f9SYatharth Kochar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*bbf8f6f9SYatharth Kochar * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*bbf8f6f9SYatharth Kochar * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*bbf8f6f9SYatharth Kochar * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*bbf8f6f9SYatharth Kochar * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*bbf8f6f9SYatharth Kochar * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*bbf8f6f9SYatharth Kochar * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*bbf8f6f9SYatharth Kochar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*bbf8f6f9SYatharth Kochar * POSSIBILITY OF SUCH DAMAGE. 29*bbf8f6f9SYatharth Kochar */ 30*bbf8f6f9SYatharth Kochar 31*bbf8f6f9SYatharth Kochar #include <assert.h> 32*bbf8f6f9SYatharth Kochar #include <bl31.h> 33*bbf8f6f9SYatharth Kochar #include <context.h> 34*bbf8f6f9SYatharth Kochar #include <context_mgmt.h> 35*bbf8f6f9SYatharth Kochar #include <cpu_data.h> 36*bbf8f6f9SYatharth Kochar #include <platform.h> 37*bbf8f6f9SYatharth Kochar 38*bbf8f6f9SYatharth Kochar 39*bbf8f6f9SYatharth Kochar /******************************************************************************* 40*bbf8f6f9SYatharth Kochar * This function returns a pointer to the most recent 'cpu_context' structure 41*bbf8f6f9SYatharth Kochar * for the calling CPU that was set as the context for the specified security 42*bbf8f6f9SYatharth Kochar * state. NULL is returned if no such structure has been specified. 43*bbf8f6f9SYatharth Kochar ******************************************************************************/ 44*bbf8f6f9SYatharth Kochar void *cm_get_context(uint32_t security_state) 45*bbf8f6f9SYatharth Kochar { 46*bbf8f6f9SYatharth Kochar assert(security_state <= NON_SECURE); 47*bbf8f6f9SYatharth Kochar 48*bbf8f6f9SYatharth Kochar return get_cpu_data(cpu_context[security_state]); 49*bbf8f6f9SYatharth Kochar } 50*bbf8f6f9SYatharth Kochar 51*bbf8f6f9SYatharth Kochar /******************************************************************************* 52*bbf8f6f9SYatharth Kochar * This function sets the pointer to the current 'cpu_context' structure for the 53*bbf8f6f9SYatharth Kochar * specified security state for the calling CPU 54*bbf8f6f9SYatharth Kochar ******************************************************************************/ 55*bbf8f6f9SYatharth Kochar void cm_set_context(void *context, uint32_t security_state) 56*bbf8f6f9SYatharth Kochar { 57*bbf8f6f9SYatharth Kochar assert(security_state <= NON_SECURE); 58*bbf8f6f9SYatharth Kochar 59*bbf8f6f9SYatharth Kochar set_cpu_data(cpu_context[security_state], context); 60*bbf8f6f9SYatharth Kochar } 61*bbf8f6f9SYatharth Kochar 62*bbf8f6f9SYatharth Kochar /******************************************************************************* 63*bbf8f6f9SYatharth Kochar * This function returns a pointer to the most recent 'cpu_context' structure 64*bbf8f6f9SYatharth Kochar * for the CPU identified by `cpu_idx` that was set as the context for the 65*bbf8f6f9SYatharth Kochar * specified security state. NULL is returned if no such structure has been 66*bbf8f6f9SYatharth Kochar * specified. 67*bbf8f6f9SYatharth Kochar ******************************************************************************/ 68*bbf8f6f9SYatharth Kochar void *cm_get_context_by_index(unsigned int cpu_idx, 69*bbf8f6f9SYatharth Kochar unsigned int security_state) 70*bbf8f6f9SYatharth Kochar { 71*bbf8f6f9SYatharth Kochar assert(sec_state_is_valid(security_state)); 72*bbf8f6f9SYatharth Kochar 73*bbf8f6f9SYatharth Kochar return get_cpu_data_by_index(cpu_idx, cpu_context[security_state]); 74*bbf8f6f9SYatharth Kochar } 75*bbf8f6f9SYatharth Kochar 76*bbf8f6f9SYatharth Kochar /******************************************************************************* 77*bbf8f6f9SYatharth Kochar * This function sets the pointer to the current 'cpu_context' structure for the 78*bbf8f6f9SYatharth Kochar * specified security state for the CPU identified by CPU index. 79*bbf8f6f9SYatharth Kochar ******************************************************************************/ 80*bbf8f6f9SYatharth Kochar void cm_set_context_by_index(unsigned int cpu_idx, void *context, 81*bbf8f6f9SYatharth Kochar unsigned int security_state) 82*bbf8f6f9SYatharth Kochar { 83*bbf8f6f9SYatharth Kochar assert(sec_state_is_valid(security_state)); 84*bbf8f6f9SYatharth Kochar 85*bbf8f6f9SYatharth Kochar set_cpu_data_by_index(cpu_idx, cpu_context[security_state], context); 86*bbf8f6f9SYatharth Kochar } 87*bbf8f6f9SYatharth Kochar 88*bbf8f6f9SYatharth Kochar #if !ERROR_DEPRECATED 89*bbf8f6f9SYatharth Kochar /* 90*bbf8f6f9SYatharth Kochar * These context management helpers are deprecated but are maintained for use 91*bbf8f6f9SYatharth Kochar * by SPDs which have not migrated to the new API. If ERROR_DEPRECATED 92*bbf8f6f9SYatharth Kochar * is enabled, these are excluded from the build so as to force users to 93*bbf8f6f9SYatharth Kochar * migrate to the new API. 94*bbf8f6f9SYatharth Kochar */ 95*bbf8f6f9SYatharth Kochar 96*bbf8f6f9SYatharth Kochar /******************************************************************************* 97*bbf8f6f9SYatharth Kochar * This function returns a pointer to the most recent 'cpu_context' structure 98*bbf8f6f9SYatharth Kochar * for the CPU identified by MPIDR that was set as the context for the specified 99*bbf8f6f9SYatharth Kochar * security state. NULL is returned if no such structure has been specified. 100*bbf8f6f9SYatharth Kochar ******************************************************************************/ 101*bbf8f6f9SYatharth Kochar void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state) 102*bbf8f6f9SYatharth Kochar { 103*bbf8f6f9SYatharth Kochar assert(sec_state_is_valid(security_state)); 104*bbf8f6f9SYatharth Kochar 105*bbf8f6f9SYatharth Kochar return cm_get_context_by_index(platform_get_core_pos(mpidr), security_state); 106*bbf8f6f9SYatharth Kochar } 107*bbf8f6f9SYatharth Kochar 108*bbf8f6f9SYatharth Kochar /******************************************************************************* 109*bbf8f6f9SYatharth Kochar * This function sets the pointer to the current 'cpu_context' structure for the 110*bbf8f6f9SYatharth Kochar * specified security state for the CPU identified by MPIDR 111*bbf8f6f9SYatharth Kochar ******************************************************************************/ 112*bbf8f6f9SYatharth Kochar void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state) 113*bbf8f6f9SYatharth Kochar { 114*bbf8f6f9SYatharth Kochar assert(sec_state_is_valid(security_state)); 115*bbf8f6f9SYatharth Kochar 116*bbf8f6f9SYatharth Kochar cm_set_context_by_index(platform_get_core_pos(mpidr), 117*bbf8f6f9SYatharth Kochar context, security_state); 118*bbf8f6f9SYatharth Kochar } 119*bbf8f6f9SYatharth Kochar 120*bbf8f6f9SYatharth Kochar /******************************************************************************* 121*bbf8f6f9SYatharth Kochar * The following function provides a compatibility function for SPDs using the 122*bbf8f6f9SYatharth Kochar * existing cm library routines. This function is expected to be invoked for 123*bbf8f6f9SYatharth Kochar * initializing the cpu_context for the CPU specified by MPIDR for first use. 124*bbf8f6f9SYatharth Kochar ******************************************************************************/ 125*bbf8f6f9SYatharth Kochar void cm_init_context(unsigned long mpidr, const entry_point_info_t *ep) 126*bbf8f6f9SYatharth Kochar { 127*bbf8f6f9SYatharth Kochar if ((mpidr & MPIDR_AFFINITY_MASK) == 128*bbf8f6f9SYatharth Kochar (read_mpidr_el1() & MPIDR_AFFINITY_MASK)) 129*bbf8f6f9SYatharth Kochar cm_init_my_context(ep); 130*bbf8f6f9SYatharth Kochar else 131*bbf8f6f9SYatharth Kochar cm_init_context_by_index(platform_get_core_pos(mpidr), ep); 132*bbf8f6f9SYatharth Kochar } 133*bbf8f6f9SYatharth Kochar #endif