1c11ba852SSoby Mathew /* 2*32f0d3c6SDouglas Raillard * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3c11ba852SSoby Mathew * 4c11ba852SSoby Mathew * Redistribution and use in source and binary forms, with or without 5c11ba852SSoby Mathew * modification, are permitted provided that the following conditions are met: 6c11ba852SSoby Mathew * 7c11ba852SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8c11ba852SSoby Mathew * list of conditions and the following disclaimer. 9c11ba852SSoby Mathew * 10c11ba852SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11c11ba852SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12c11ba852SSoby Mathew * and/or other materials provided with the distribution. 13c11ba852SSoby Mathew * 14c11ba852SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15c11ba852SSoby Mathew * to endorse or promote products derived from this software without specific 16c11ba852SSoby Mathew * prior written permission. 17c11ba852SSoby Mathew * 18c11ba852SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19c11ba852SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20c11ba852SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21c11ba852SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22c11ba852SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23c11ba852SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24c11ba852SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25c11ba852SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26c11ba852SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27c11ba852SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28c11ba852SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29c11ba852SSoby Mathew */ 30c11ba852SSoby Mathew 31c11ba852SSoby Mathew #include <arch.h> 32c11ba852SSoby Mathew #include <arch_helpers.h> 33c11ba852SSoby Mathew #include <assert.h> 34c11ba852SSoby Mathew #include <bl_common.h> 35c11ba852SSoby Mathew #include <context.h> 36c11ba852SSoby Mathew #include <context_mgmt.h> 37c11ba852SSoby Mathew #include <debug.h> 38c11ba852SSoby Mathew #include <platform.h> 39c11ba852SSoby Mathew #include <platform_def.h> 40c11ba852SSoby Mathew #include <platform_sp_min.h> 41c11ba852SSoby Mathew #include <psci.h> 42c11ba852SSoby Mathew #include <runtime_svc.h> 43c11ba852SSoby Mathew #include <smcc_helpers.h> 44c11ba852SSoby Mathew #include <stddef.h> 45c11ba852SSoby Mathew #include <stdint.h> 46c11ba852SSoby Mathew #include <string.h> 47c11ba852SSoby Mathew #include <types.h> 48*32f0d3c6SDouglas Raillard #include <utils.h> 49c11ba852SSoby Mathew #include "sp_min_private.h" 50c11ba852SSoby Mathew 51c11ba852SSoby Mathew /* Pointers to per-core cpu contexts */ 52c11ba852SSoby Mathew static void *sp_min_cpu_ctx_ptr[PLATFORM_CORE_COUNT]; 53c11ba852SSoby Mathew 54c11ba852SSoby Mathew /* SP_MIN only stores the non secure smc context */ 55c11ba852SSoby Mathew static smc_ctx_t sp_min_smc_context[PLATFORM_CORE_COUNT]; 56c11ba852SSoby Mathew 57c11ba852SSoby Mathew /****************************************************************************** 58c11ba852SSoby Mathew * Define the smcc helper library API's 59c11ba852SSoby Mathew *****************************************************************************/ 60c11ba852SSoby Mathew void *smc_get_ctx(int security_state) 61c11ba852SSoby Mathew { 62c11ba852SSoby Mathew assert(security_state == NON_SECURE); 63c11ba852SSoby Mathew return &sp_min_smc_context[plat_my_core_pos()]; 64c11ba852SSoby Mathew } 65c11ba852SSoby Mathew 66c11ba852SSoby Mathew void smc_set_next_ctx(int security_state) 67c11ba852SSoby Mathew { 68c11ba852SSoby Mathew assert(security_state == NON_SECURE); 69c11ba852SSoby Mathew /* SP_MIN stores only non secure smc context. Nothing to do here */ 70c11ba852SSoby Mathew } 71c11ba852SSoby Mathew 72c11ba852SSoby Mathew void *smc_get_next_ctx(void) 73c11ba852SSoby Mathew { 74c11ba852SSoby Mathew return &sp_min_smc_context[plat_my_core_pos()]; 75c11ba852SSoby Mathew } 76c11ba852SSoby Mathew 77c11ba852SSoby Mathew /******************************************************************************* 78c11ba852SSoby Mathew * This function returns a pointer to the most recent 'cpu_context' structure 79c11ba852SSoby Mathew * for the calling CPU that was set as the context for the specified security 80c11ba852SSoby Mathew * state. NULL is returned if no such structure has been specified. 81c11ba852SSoby Mathew ******************************************************************************/ 82c11ba852SSoby Mathew void *cm_get_context(uint32_t security_state) 83c11ba852SSoby Mathew { 84c11ba852SSoby Mathew assert(security_state == NON_SECURE); 85c11ba852SSoby Mathew return sp_min_cpu_ctx_ptr[plat_my_core_pos()]; 86c11ba852SSoby Mathew } 87c11ba852SSoby Mathew 88c11ba852SSoby Mathew /******************************************************************************* 89c11ba852SSoby Mathew * This function sets the pointer to the current 'cpu_context' structure for the 90c11ba852SSoby Mathew * specified security state for the calling CPU 91c11ba852SSoby Mathew ******************************************************************************/ 92c11ba852SSoby Mathew void cm_set_context(void *context, uint32_t security_state) 93c11ba852SSoby Mathew { 94c11ba852SSoby Mathew assert(security_state == NON_SECURE); 95c11ba852SSoby Mathew sp_min_cpu_ctx_ptr[plat_my_core_pos()] = context; 96c11ba852SSoby Mathew } 97c11ba852SSoby Mathew 98c11ba852SSoby Mathew /******************************************************************************* 99c11ba852SSoby Mathew * This function returns a pointer to the most recent 'cpu_context' structure 100c11ba852SSoby Mathew * for the CPU identified by `cpu_idx` that was set as the context for the 101c11ba852SSoby Mathew * specified security state. NULL is returned if no such structure has been 102c11ba852SSoby Mathew * specified. 103c11ba852SSoby Mathew ******************************************************************************/ 104c11ba852SSoby Mathew void *cm_get_context_by_index(unsigned int cpu_idx, 105c11ba852SSoby Mathew unsigned int security_state) 106c11ba852SSoby Mathew { 107c11ba852SSoby Mathew assert(security_state == NON_SECURE); 108c11ba852SSoby Mathew return sp_min_cpu_ctx_ptr[cpu_idx]; 109c11ba852SSoby Mathew } 110c11ba852SSoby Mathew 111c11ba852SSoby Mathew /******************************************************************************* 112c11ba852SSoby Mathew * This function sets the pointer to the current 'cpu_context' structure for the 113c11ba852SSoby Mathew * specified security state for the CPU identified by CPU index. 114c11ba852SSoby Mathew ******************************************************************************/ 115c11ba852SSoby Mathew void cm_set_context_by_index(unsigned int cpu_idx, void *context, 116c11ba852SSoby Mathew unsigned int security_state) 117c11ba852SSoby Mathew { 118c11ba852SSoby Mathew assert(security_state == NON_SECURE); 119c11ba852SSoby Mathew sp_min_cpu_ctx_ptr[cpu_idx] = context; 120c11ba852SSoby Mathew } 121c11ba852SSoby Mathew 122c11ba852SSoby Mathew static void copy_cpu_ctx_to_smc_stx(const regs_t *cpu_reg_ctx, 123c11ba852SSoby Mathew smc_ctx_t *next_smc_ctx) 124c11ba852SSoby Mathew { 125c11ba852SSoby Mathew next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0); 126c11ba852SSoby Mathew next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR); 127c11ba852SSoby Mathew next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR); 128c11ba852SSoby Mathew } 129c11ba852SSoby Mathew 130c11ba852SSoby Mathew /******************************************************************************* 131c11ba852SSoby Mathew * This function invokes the PSCI library interface to initialize the 132c11ba852SSoby Mathew * non secure cpu context and copies the relevant cpu context register values 133c11ba852SSoby Mathew * to smc context. These registers will get programmed during `smc_exit`. 134c11ba852SSoby Mathew ******************************************************************************/ 135c11ba852SSoby Mathew static void sp_min_prepare_next_image_entry(void) 136c11ba852SSoby Mathew { 137c11ba852SSoby Mathew entry_point_info_t *next_image_info; 138c11ba852SSoby Mathew 139c11ba852SSoby Mathew /* Program system registers to proceed to non-secure */ 140c11ba852SSoby Mathew next_image_info = sp_min_plat_get_bl33_ep_info(); 141c11ba852SSoby Mathew assert(next_image_info); 142c11ba852SSoby Mathew assert(NON_SECURE == GET_SECURITY_STATE(next_image_info->h.attr)); 143c11ba852SSoby Mathew 144c11ba852SSoby Mathew INFO("SP_MIN: Preparing exit to normal world\n"); 145c11ba852SSoby Mathew 146c11ba852SSoby Mathew psci_prepare_next_non_secure_ctx(next_image_info); 147c11ba852SSoby Mathew smc_set_next_ctx(NON_SECURE); 148c11ba852SSoby Mathew 149c11ba852SSoby Mathew /* Copy r0, lr and spsr from cpu context to SMC context */ 150c11ba852SSoby Mathew copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)), 151c11ba852SSoby Mathew smc_get_next_ctx()); 152c11ba852SSoby Mathew } 153c11ba852SSoby Mathew 154c11ba852SSoby Mathew /****************************************************************************** 15558e946aeSSoby Mathew * Implement the ARM Standard Service function to get arguments for a 15658e946aeSSoby Mathew * particular service. 15758e946aeSSoby Mathew *****************************************************************************/ 15858e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask) 15958e946aeSSoby Mathew { 16058e946aeSSoby Mathew /* Setup the arguments for PSCI Library */ 16158e946aeSSoby Mathew DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, sp_min_warm_entrypoint); 16258e946aeSSoby Mathew 16358e946aeSSoby Mathew /* PSCI is the only ARM Standard Service implemented */ 16458e946aeSSoby Mathew assert(svc_mask == PSCI_FID_MASK); 16558e946aeSSoby Mathew 16658e946aeSSoby Mathew return (uintptr_t)&psci_args; 16758e946aeSSoby Mathew } 16858e946aeSSoby Mathew 16958e946aeSSoby Mathew /****************************************************************************** 170c11ba852SSoby Mathew * The SP_MIN main function. Do the platform and PSCI Library setup. Also 171c11ba852SSoby Mathew * initialize the runtime service framework. 172c11ba852SSoby Mathew *****************************************************************************/ 173c11ba852SSoby Mathew void sp_min_main(void) 174c11ba852SSoby Mathew { 175f426fc05SSoby Mathew NOTICE("SP_MIN: %s\n", version_string); 176f426fc05SSoby Mathew NOTICE("SP_MIN: %s\n", build_message); 177f426fc05SSoby Mathew 178f426fc05SSoby Mathew /* Perform the SP_MIN platform setup */ 179c11ba852SSoby Mathew sp_min_platform_setup(); 180c11ba852SSoby Mathew 18158e946aeSSoby Mathew /* Initialize the runtime services e.g. psci */ 182c11ba852SSoby Mathew INFO("SP_MIN: Initializing runtime services\n"); 183c11ba852SSoby Mathew runtime_svc_init(); 184c11ba852SSoby Mathew 185c11ba852SSoby Mathew /* 186c11ba852SSoby Mathew * We are ready to enter the next EL. Prepare entry into the image 187c11ba852SSoby Mathew * corresponding to the desired security state after the next ERET. 188c11ba852SSoby Mathew */ 189c11ba852SSoby Mathew sp_min_prepare_next_image_entry(); 190c11ba852SSoby Mathew } 191c11ba852SSoby Mathew 192c11ba852SSoby Mathew /****************************************************************************** 193c11ba852SSoby Mathew * This function is invoked during warm boot. Invoke the PSCI library 194c11ba852SSoby Mathew * warm boot entry point which takes care of Architectural and platform setup/ 195c11ba852SSoby Mathew * restore. Copy the relevant cpu_context register values to smc context which 196c11ba852SSoby Mathew * will get programmed during `smc_exit`. 197c11ba852SSoby Mathew *****************************************************************************/ 198c11ba852SSoby Mathew void sp_min_warm_boot(void) 199c11ba852SSoby Mathew { 200c11ba852SSoby Mathew smc_ctx_t *next_smc_ctx; 201c11ba852SSoby Mathew 202c11ba852SSoby Mathew psci_warmboot_entrypoint(); 203c11ba852SSoby Mathew 204c11ba852SSoby Mathew smc_set_next_ctx(NON_SECURE); 205c11ba852SSoby Mathew 206c11ba852SSoby Mathew next_smc_ctx = smc_get_next_ctx(); 207*32f0d3c6SDouglas Raillard zeromem(next_smc_ctx, sizeof(smc_ctx_t)); 208c11ba852SSoby Mathew 209c11ba852SSoby Mathew copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)), 210c11ba852SSoby Mathew next_smc_ctx); 211c11ba852SSoby Mathew } 212