1b7cb133eSJeenu Viswambharan /* 28e3032f9SJeenu Viswambharan * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 3b7cb133eSJeenu Viswambharan * 4b7cb133eSJeenu Viswambharan * SPDX-License-Identifier: BSD-3-Clause 5b7cb133eSJeenu Viswambharan */ 6b7cb133eSJeenu Viswambharan 7b7cb133eSJeenu Viswambharan #include <assert.h> 8b7cb133eSJeenu Viswambharan #include <string.h> 9*09d40e0eSAntonio Nino Diaz 10*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 11*09d40e0eSAntonio Nino Diaz #include <bl31/ehf.h> 12*09d40e0eSAntonio Nino Diaz #include <bl31/interrupt_mgmt.h> 13*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 14*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 15*09d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 16*09d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 17*09d40e0eSAntonio Nino Diaz #include <services/sdei.h> 18*09d40e0eSAntonio Nino Diaz 19b7cb133eSJeenu Viswambharan #include "sdei_private.h" 20b7cb133eSJeenu Viswambharan 21b7cb133eSJeenu Viswambharan /* x0-x17 GPREGS context */ 22ba6e5ca6SJeenu Viswambharan #define SDEI_SAVED_GPREGS 18U 23b7cb133eSJeenu Viswambharan 24b7cb133eSJeenu Viswambharan /* Maximum preemption nesting levels: Critical priority and Normal priority */ 25ba6e5ca6SJeenu Viswambharan #define MAX_EVENT_NESTING 2U 26b7cb133eSJeenu Viswambharan 27b7cb133eSJeenu Viswambharan /* Per-CPU SDEI state access macro */ 28ba6e5ca6SJeenu Viswambharan #define sdei_get_this_pe_state() (&cpu_state[plat_my_core_pos()]) 29b7cb133eSJeenu Viswambharan 30b7cb133eSJeenu Viswambharan /* Structure to store information about an outstanding dispatch */ 31b7cb133eSJeenu Viswambharan typedef struct sdei_dispatch_context { 32b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 33b7cb133eSJeenu Viswambharan uint64_t x[SDEI_SAVED_GPREGS]; 34cdb6ac94SJeenu Viswambharan struct jmpbuf *dispatch_jmp; 35b7cb133eSJeenu Viswambharan 36b7cb133eSJeenu Viswambharan /* Exception state registers */ 37b7cb133eSJeenu Viswambharan uint64_t elr_el3; 38b7cb133eSJeenu Viswambharan uint64_t spsr_el3; 396f03bc77SDimitris Papastamos 406f03bc77SDimitris Papastamos #if DYNAMIC_WORKAROUND_CVE_2018_3639 416f03bc77SDimitris Papastamos /* CVE-2018-3639 mitigation state */ 426f03bc77SDimitris Papastamos uint64_t disable_cve_2018_3639; 436f03bc77SDimitris Papastamos #endif 44b7cb133eSJeenu Viswambharan } sdei_dispatch_context_t; 45b7cb133eSJeenu Viswambharan 46b7cb133eSJeenu Viswambharan /* Per-CPU SDEI state data */ 47b7cb133eSJeenu Viswambharan typedef struct sdei_cpu_state { 48b7cb133eSJeenu Viswambharan sdei_dispatch_context_t dispatch_stack[MAX_EVENT_NESTING]; 49b7cb133eSJeenu Viswambharan unsigned short stack_top; /* Empty ascending */ 50ba6e5ca6SJeenu Viswambharan bool pe_masked; 51ba6e5ca6SJeenu Viswambharan bool pending_enables; 52b7cb133eSJeenu Viswambharan } sdei_cpu_state_t; 53b7cb133eSJeenu Viswambharan 54b7cb133eSJeenu Viswambharan /* SDEI states for all cores in the system */ 55ba6e5ca6SJeenu Viswambharan static sdei_cpu_state_t cpu_state[PLATFORM_CORE_COUNT]; 56b7cb133eSJeenu Viswambharan 57ba6e5ca6SJeenu Viswambharan int64_t sdei_pe_mask(void) 58b7cb133eSJeenu Viswambharan { 59ba6e5ca6SJeenu Viswambharan int64_t ret = 0; 60b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 61b7cb133eSJeenu Viswambharan 62b7cb133eSJeenu Viswambharan /* 63b7cb133eSJeenu Viswambharan * Return value indicates whether this call had any effect in the mask 64b7cb133eSJeenu Viswambharan * status of this PE. 65b7cb133eSJeenu Viswambharan */ 66ba6e5ca6SJeenu Viswambharan if (!state->pe_masked) { 67ba6e5ca6SJeenu Viswambharan state->pe_masked = true; 68ba6e5ca6SJeenu Viswambharan ret = 1; 69ba6e5ca6SJeenu Viswambharan } 70b7cb133eSJeenu Viswambharan 71b7cb133eSJeenu Viswambharan return ret; 72b7cb133eSJeenu Viswambharan } 73b7cb133eSJeenu Viswambharan 74b7cb133eSJeenu Viswambharan void sdei_pe_unmask(void) 75b7cb133eSJeenu Viswambharan { 76ba6e5ca6SJeenu Viswambharan unsigned int i; 77b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 78b7cb133eSJeenu Viswambharan sdei_entry_t *se; 79b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 80b7cb133eSJeenu Viswambharan uint64_t my_mpidr = read_mpidr_el1() & MPIDR_AFFINITY_MASK; 81b7cb133eSJeenu Viswambharan 82b7cb133eSJeenu Viswambharan /* 83b7cb133eSJeenu Viswambharan * If there are pending enables, iterate through the private mappings 84b7cb133eSJeenu Viswambharan * and enable those bound maps that are in enabled state. Also, iterate 85b7cb133eSJeenu Viswambharan * through shared mappings and enable interrupts of events that are 86b7cb133eSJeenu Viswambharan * targeted to this PE. 87b7cb133eSJeenu Viswambharan */ 88b7cb133eSJeenu Viswambharan if (state->pending_enables) { 89b7cb133eSJeenu Viswambharan for_each_private_map(i, map) { 90b7cb133eSJeenu Viswambharan se = get_event_entry(map); 91b7cb133eSJeenu Viswambharan if (is_map_bound(map) && GET_EV_STATE(se, ENABLED)) 92b7cb133eSJeenu Viswambharan plat_ic_enable_interrupt(map->intr); 93b7cb133eSJeenu Viswambharan } 94b7cb133eSJeenu Viswambharan 95b7cb133eSJeenu Viswambharan for_each_shared_map(i, map) { 96b7cb133eSJeenu Viswambharan se = get_event_entry(map); 97b7cb133eSJeenu Viswambharan 98b7cb133eSJeenu Viswambharan sdei_map_lock(map); 99ba6e5ca6SJeenu Viswambharan if (is_map_bound(map) && GET_EV_STATE(se, ENABLED) && 100b7cb133eSJeenu Viswambharan (se->reg_flags == SDEI_REGF_RM_PE) && 101b7cb133eSJeenu Viswambharan (se->affinity == my_mpidr)) { 102b7cb133eSJeenu Viswambharan plat_ic_enable_interrupt(map->intr); 103b7cb133eSJeenu Viswambharan } 104b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 105b7cb133eSJeenu Viswambharan } 106b7cb133eSJeenu Viswambharan } 107b7cb133eSJeenu Viswambharan 108ba6e5ca6SJeenu Viswambharan state->pending_enables = false; 109ba6e5ca6SJeenu Viswambharan state->pe_masked = false; 110b7cb133eSJeenu Viswambharan } 111b7cb133eSJeenu Viswambharan 112b7cb133eSJeenu Viswambharan /* Push a dispatch context to the dispatch stack */ 113b7cb133eSJeenu Viswambharan static sdei_dispatch_context_t *push_dispatch(void) 114b7cb133eSJeenu Viswambharan { 115b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 116b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 117b7cb133eSJeenu Viswambharan 118b7cb133eSJeenu Viswambharan /* Cannot have more than max events */ 119b7cb133eSJeenu Viswambharan assert(state->stack_top < MAX_EVENT_NESTING); 120b7cb133eSJeenu Viswambharan 121b7cb133eSJeenu Viswambharan disp_ctx = &state->dispatch_stack[state->stack_top]; 122b7cb133eSJeenu Viswambharan state->stack_top++; 123b7cb133eSJeenu Viswambharan 124b7cb133eSJeenu Viswambharan return disp_ctx; 125b7cb133eSJeenu Viswambharan } 126b7cb133eSJeenu Viswambharan 127b7cb133eSJeenu Viswambharan /* Pop a dispatch context to the dispatch stack */ 128b7cb133eSJeenu Viswambharan static sdei_dispatch_context_t *pop_dispatch(void) 129b7cb133eSJeenu Viswambharan { 130b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 131b7cb133eSJeenu Viswambharan 132ba6e5ca6SJeenu Viswambharan if (state->stack_top == 0U) 133b7cb133eSJeenu Viswambharan return NULL; 134b7cb133eSJeenu Viswambharan 135b7cb133eSJeenu Viswambharan assert(state->stack_top <= MAX_EVENT_NESTING); 136b7cb133eSJeenu Viswambharan 137b7cb133eSJeenu Viswambharan state->stack_top--; 138b7cb133eSJeenu Viswambharan 139b7cb133eSJeenu Viswambharan return &state->dispatch_stack[state->stack_top]; 140b7cb133eSJeenu Viswambharan } 141b7cb133eSJeenu Viswambharan 142b7cb133eSJeenu Viswambharan /* Retrieve the context at the top of dispatch stack */ 143b7cb133eSJeenu Viswambharan static sdei_dispatch_context_t *get_outstanding_dispatch(void) 144b7cb133eSJeenu Viswambharan { 145b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 146b7cb133eSJeenu Viswambharan 147ba6e5ca6SJeenu Viswambharan if (state->stack_top == 0U) 148b7cb133eSJeenu Viswambharan return NULL; 149b7cb133eSJeenu Viswambharan 150b7cb133eSJeenu Viswambharan assert(state->stack_top <= MAX_EVENT_NESTING); 151b7cb133eSJeenu Viswambharan 152ba6e5ca6SJeenu Viswambharan return &state->dispatch_stack[state->stack_top - 1U]; 153b7cb133eSJeenu Viswambharan } 154b7cb133eSJeenu Viswambharan 155cdb6ac94SJeenu Viswambharan static sdei_dispatch_context_t *save_event_ctx(sdei_ev_map_t *map, 156cdb6ac94SJeenu Viswambharan void *tgt_ctx) 157b7cb133eSJeenu Viswambharan { 158b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 159ba6e5ca6SJeenu Viswambharan const gp_regs_t *tgt_gpregs; 160ba6e5ca6SJeenu Viswambharan const el3_state_t *tgt_el3; 161b7cb133eSJeenu Viswambharan 162ba6e5ca6SJeenu Viswambharan assert(tgt_ctx != NULL); 163b7cb133eSJeenu Viswambharan tgt_gpregs = get_gpregs_ctx(tgt_ctx); 164b7cb133eSJeenu Viswambharan tgt_el3 = get_el3state_ctx(tgt_ctx); 165b7cb133eSJeenu Viswambharan 166b7cb133eSJeenu Viswambharan disp_ctx = push_dispatch(); 167ba6e5ca6SJeenu Viswambharan assert(disp_ctx != NULL); 168b7cb133eSJeenu Viswambharan disp_ctx->map = map; 169b7cb133eSJeenu Viswambharan 170b7cb133eSJeenu Viswambharan /* Save general purpose and exception registers */ 171b7cb133eSJeenu Viswambharan memcpy(disp_ctx->x, tgt_gpregs, sizeof(disp_ctx->x)); 172b7cb133eSJeenu Viswambharan disp_ctx->spsr_el3 = read_ctx_reg(tgt_el3, CTX_SPSR_EL3); 173b7cb133eSJeenu Viswambharan disp_ctx->elr_el3 = read_ctx_reg(tgt_el3, CTX_ELR_EL3); 1746f03bc77SDimitris Papastamos 175cdb6ac94SJeenu Viswambharan return disp_ctx; 176b7cb133eSJeenu Viswambharan } 177b7cb133eSJeenu Viswambharan 178ba6e5ca6SJeenu Viswambharan static void restore_event_ctx(const sdei_dispatch_context_t *disp_ctx, void *tgt_ctx) 179b7cb133eSJeenu Viswambharan { 180b7cb133eSJeenu Viswambharan gp_regs_t *tgt_gpregs; 181b7cb133eSJeenu Viswambharan el3_state_t *tgt_el3; 182b7cb133eSJeenu Viswambharan 183ba6e5ca6SJeenu Viswambharan assert(tgt_ctx != NULL); 184b7cb133eSJeenu Viswambharan tgt_gpregs = get_gpregs_ctx(tgt_ctx); 185b7cb133eSJeenu Viswambharan tgt_el3 = get_el3state_ctx(tgt_ctx); 186b7cb133eSJeenu Viswambharan 187b7cb133eSJeenu Viswambharan CASSERT(sizeof(disp_ctx->x) == (SDEI_SAVED_GPREGS * sizeof(uint64_t)), 188b7cb133eSJeenu Viswambharan foo); 189b7cb133eSJeenu Viswambharan 190b7cb133eSJeenu Viswambharan /* Restore general purpose and exception registers */ 191b7cb133eSJeenu Viswambharan memcpy(tgt_gpregs, disp_ctx->x, sizeof(disp_ctx->x)); 192b7cb133eSJeenu Viswambharan write_ctx_reg(tgt_el3, CTX_SPSR_EL3, disp_ctx->spsr_el3); 193b7cb133eSJeenu Viswambharan write_ctx_reg(tgt_el3, CTX_ELR_EL3, disp_ctx->elr_el3); 1946f03bc77SDimitris Papastamos 1956f03bc77SDimitris Papastamos #if DYNAMIC_WORKAROUND_CVE_2018_3639 1966f03bc77SDimitris Papastamos cve_2018_3639_t *tgt_cve_2018_3639; 1976f03bc77SDimitris Papastamos tgt_cve_2018_3639 = get_cve_2018_3639_ctx(tgt_ctx); 1986f03bc77SDimitris Papastamos 1996f03bc77SDimitris Papastamos /* Restore CVE-2018-3639 mitigation state */ 2006f03bc77SDimitris Papastamos write_ctx_reg(tgt_cve_2018_3639, CTX_CVE_2018_3639_DISABLE, 2016f03bc77SDimitris Papastamos disp_ctx->disable_cve_2018_3639); 2026f03bc77SDimitris Papastamos #endif 203b7cb133eSJeenu Viswambharan } 204b7cb133eSJeenu Viswambharan 205b7cb133eSJeenu Viswambharan static void save_secure_context(void) 206b7cb133eSJeenu Viswambharan { 207b7cb133eSJeenu Viswambharan cm_el1_sysregs_context_save(SECURE); 208b7cb133eSJeenu Viswambharan } 209b7cb133eSJeenu Viswambharan 210b7cb133eSJeenu Viswambharan /* Restore Secure context and arrange to resume it at the next ERET */ 211b7cb133eSJeenu Viswambharan static void restore_and_resume_secure_context(void) 212b7cb133eSJeenu Viswambharan { 213b7cb133eSJeenu Viswambharan cm_el1_sysregs_context_restore(SECURE); 214b7cb133eSJeenu Viswambharan cm_set_next_eret_context(SECURE); 215b7cb133eSJeenu Viswambharan } 216b7cb133eSJeenu Viswambharan 217b7cb133eSJeenu Viswambharan /* 218b7cb133eSJeenu Viswambharan * Restore Non-secure context and arrange to resume it at the next ERET. Return 219b7cb133eSJeenu Viswambharan * pointer to the Non-secure context. 220b7cb133eSJeenu Viswambharan */ 221b7cb133eSJeenu Viswambharan static cpu_context_t *restore_and_resume_ns_context(void) 222b7cb133eSJeenu Viswambharan { 223b7cb133eSJeenu Viswambharan cpu_context_t *ns_ctx; 224b7cb133eSJeenu Viswambharan 225b7cb133eSJeenu Viswambharan cm_el1_sysregs_context_restore(NON_SECURE); 226b7cb133eSJeenu Viswambharan cm_set_next_eret_context(NON_SECURE); 227b7cb133eSJeenu Viswambharan 228b7cb133eSJeenu Viswambharan ns_ctx = cm_get_context(NON_SECURE); 229ba6e5ca6SJeenu Viswambharan assert(ns_ctx != NULL); 230b7cb133eSJeenu Viswambharan 231b7cb133eSJeenu Viswambharan return ns_ctx; 232b7cb133eSJeenu Viswambharan } 233b7cb133eSJeenu Viswambharan 234b7cb133eSJeenu Viswambharan /* 235b7cb133eSJeenu Viswambharan * Populate the Non-secure context so that the next ERET will dispatch to the 236b7cb133eSJeenu Viswambharan * SDEI client. 237b7cb133eSJeenu Viswambharan */ 238b7cb133eSJeenu Viswambharan static void setup_ns_dispatch(sdei_ev_map_t *map, sdei_entry_t *se, 239cdb6ac94SJeenu Viswambharan cpu_context_t *ctx, struct jmpbuf *dispatch_jmp) 240b7cb133eSJeenu Viswambharan { 241cdb6ac94SJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 242b7cb133eSJeenu Viswambharan 243b7cb133eSJeenu Viswambharan /* Push the event and context */ 244cdb6ac94SJeenu Viswambharan disp_ctx = save_event_ctx(map, ctx); 245b7cb133eSJeenu Viswambharan 246b7cb133eSJeenu Viswambharan /* 247b7cb133eSJeenu Viswambharan * Setup handler arguments: 248b7cb133eSJeenu Viswambharan * 249b7cb133eSJeenu Viswambharan * - x0: Event number 250b7cb133eSJeenu Viswambharan * - x1: Handler argument supplied at the time of event registration 251b7cb133eSJeenu Viswambharan * - x2: Interrupted PC 252b7cb133eSJeenu Viswambharan * - x3: Interrupted SPSR 253b7cb133eSJeenu Viswambharan */ 254ba6e5ca6SJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X0, (uint64_t) map->ev_num); 255b7cb133eSJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X1, se->arg); 256cdb6ac94SJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X2, disp_ctx->elr_el3); 257cdb6ac94SJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X3, disp_ctx->spsr_el3); 258b7cb133eSJeenu Viswambharan 259b7cb133eSJeenu Viswambharan /* 260b7cb133eSJeenu Viswambharan * Prepare for ERET: 261b7cb133eSJeenu Viswambharan * 262b7cb133eSJeenu Viswambharan * - Set PC to the registered handler address 263b7cb133eSJeenu Viswambharan * - Set SPSR to jump to client EL with exceptions masked 264b7cb133eSJeenu Viswambharan */ 265b7cb133eSJeenu Viswambharan cm_set_elr_spsr_el3(NON_SECURE, (uintptr_t) se->ep, 266b7cb133eSJeenu Viswambharan SPSR_64(sdei_client_el(), MODE_SP_ELX, 267b7cb133eSJeenu Viswambharan DISABLE_ALL_EXCEPTIONS)); 268cdb6ac94SJeenu Viswambharan 269cdb6ac94SJeenu Viswambharan #if DYNAMIC_WORKAROUND_CVE_2018_3639 270cdb6ac94SJeenu Viswambharan cve_2018_3639_t *tgt_cve_2018_3639; 271cdb6ac94SJeenu Viswambharan tgt_cve_2018_3639 = get_cve_2018_3639_ctx(ctx); 272cdb6ac94SJeenu Viswambharan 273cdb6ac94SJeenu Viswambharan /* Save CVE-2018-3639 mitigation state */ 274cdb6ac94SJeenu Viswambharan disp_ctx->disable_cve_2018_3639 = read_ctx_reg(tgt_cve_2018_3639, 275cdb6ac94SJeenu Viswambharan CTX_CVE_2018_3639_DISABLE); 276cdb6ac94SJeenu Viswambharan 277cdb6ac94SJeenu Viswambharan /* Force SDEI handler to execute with mitigation enabled by default */ 278cdb6ac94SJeenu Viswambharan write_ctx_reg(tgt_cve_2018_3639, CTX_CVE_2018_3639_DISABLE, 0); 279cdb6ac94SJeenu Viswambharan #endif 280cdb6ac94SJeenu Viswambharan 281cdb6ac94SJeenu Viswambharan disp_ctx->dispatch_jmp = dispatch_jmp; 282b7cb133eSJeenu Viswambharan } 283b7cb133eSJeenu Viswambharan 284b7cb133eSJeenu Viswambharan /* Handle a triggered SDEI interrupt while events were masked on this PE */ 285b7cb133eSJeenu Viswambharan static void handle_masked_trigger(sdei_ev_map_t *map, sdei_entry_t *se, 286b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state, unsigned int intr_raw) 287b7cb133eSJeenu Viswambharan { 288b7cb133eSJeenu Viswambharan uint64_t my_mpidr __unused = (read_mpidr_el1() & MPIDR_AFFINITY_MASK); 289ba6e5ca6SJeenu Viswambharan bool disable = false; 290b7cb133eSJeenu Viswambharan 291b7cb133eSJeenu Viswambharan /* Nothing to do for event 0 */ 292b7cb133eSJeenu Viswambharan if (map->ev_num == SDEI_EVENT_0) 293b7cb133eSJeenu Viswambharan return; 294b7cb133eSJeenu Viswambharan 295b7cb133eSJeenu Viswambharan /* 296b7cb133eSJeenu Viswambharan * For a private event, or for a shared event specifically routed to 297b7cb133eSJeenu Viswambharan * this CPU, we disable interrupt, leave the interrupt pending, and do 298b7cb133eSJeenu Viswambharan * EOI. 299b7cb133eSJeenu Viswambharan */ 300ba6e5ca6SJeenu Viswambharan if (is_event_private(map) || (se->reg_flags == SDEI_REGF_RM_PE)) 301ba6e5ca6SJeenu Viswambharan disable = true; 302ba6e5ca6SJeenu Viswambharan 303ba6e5ca6SJeenu Viswambharan if (se->reg_flags == SDEI_REGF_RM_PE) 304b7cb133eSJeenu Viswambharan assert(se->affinity == my_mpidr); 305b7cb133eSJeenu Viswambharan 306b7cb133eSJeenu Viswambharan if (disable) { 307b7cb133eSJeenu Viswambharan plat_ic_disable_interrupt(map->intr); 308b7cb133eSJeenu Viswambharan plat_ic_set_interrupt_pending(map->intr); 309b7cb133eSJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 310ba6e5ca6SJeenu Viswambharan state->pending_enables = true; 311b7cb133eSJeenu Viswambharan 312b7cb133eSJeenu Viswambharan return; 313b7cb133eSJeenu Viswambharan } 314b7cb133eSJeenu Viswambharan 315b7cb133eSJeenu Viswambharan /* 316b7cb133eSJeenu Viswambharan * We just received a shared event with routing set to ANY PE. The 317b7cb133eSJeenu Viswambharan * interrupt can't be delegated on this PE as SDEI events are masked. 318b7cb133eSJeenu Viswambharan * However, because its routing mode is ANY, it is possible that the 319b7cb133eSJeenu Viswambharan * event can be delegated on any other PE that hasn't masked events. 320b7cb133eSJeenu Viswambharan * Therefore, we set the interrupt back pending so as to give other 321b7cb133eSJeenu Viswambharan * suitable PEs a chance of handling it. 322b7cb133eSJeenu Viswambharan */ 323ba6e5ca6SJeenu Viswambharan assert(plat_ic_is_spi(map->intr) != 0); 324b7cb133eSJeenu Viswambharan plat_ic_set_interrupt_pending(map->intr); 325b7cb133eSJeenu Viswambharan 326b7cb133eSJeenu Viswambharan /* 327b7cb133eSJeenu Viswambharan * Leaving the same interrupt pending also means that the same interrupt 328b7cb133eSJeenu Viswambharan * can target this PE again as soon as this PE leaves EL3. Whether and 329b7cb133eSJeenu Viswambharan * how often that happens depends on the implementation of GIC. 330b7cb133eSJeenu Viswambharan * 331b7cb133eSJeenu Viswambharan * We therefore call a platform handler to resolve this situation. 332b7cb133eSJeenu Viswambharan */ 333b7cb133eSJeenu Viswambharan plat_sdei_handle_masked_trigger(my_mpidr, map->intr); 334b7cb133eSJeenu Viswambharan 335b7cb133eSJeenu Viswambharan /* This PE is masked. We EOI the interrupt, as it can't be delegated */ 336b7cb133eSJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 337b7cb133eSJeenu Viswambharan } 338b7cb133eSJeenu Viswambharan 339b7cb133eSJeenu Viswambharan /* SDEI main interrupt handler */ 340b7cb133eSJeenu Viswambharan int sdei_intr_handler(uint32_t intr_raw, uint32_t flags, void *handle, 341b7cb133eSJeenu Viswambharan void *cookie) 342b7cb133eSJeenu Viswambharan { 343b7cb133eSJeenu Viswambharan sdei_entry_t *se; 344b7cb133eSJeenu Viswambharan cpu_context_t *ctx; 345b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 346ba6e5ca6SJeenu Viswambharan const sdei_dispatch_context_t *disp_ctx; 347b7cb133eSJeenu Viswambharan unsigned int sec_state; 348b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state; 349b7cb133eSJeenu Viswambharan uint32_t intr; 350cdb6ac94SJeenu Viswambharan struct jmpbuf dispatch_jmp; 351ba6e5ca6SJeenu Viswambharan const uint64_t mpidr = read_mpidr_el1(); 352b7cb133eSJeenu Viswambharan 353b7cb133eSJeenu Viswambharan /* 354b7cb133eSJeenu Viswambharan * To handle an event, the following conditions must be true: 355b7cb133eSJeenu Viswambharan * 356b7cb133eSJeenu Viswambharan * 1. Event must be signalled 357b7cb133eSJeenu Viswambharan * 2. Event must be enabled 358b7cb133eSJeenu Viswambharan * 3. This PE must be a target PE for the event 359b7cb133eSJeenu Viswambharan * 4. PE must be unmasked for SDEI 360b7cb133eSJeenu Viswambharan * 5. If this is a normal event, no event must be running 361b7cb133eSJeenu Viswambharan * 6. If this is a critical event, no critical event must be running 362b7cb133eSJeenu Viswambharan * 363b7cb133eSJeenu Viswambharan * (1) and (2) are true when this function is running 364b7cb133eSJeenu Viswambharan * (3) is enforced in GIC by selecting the appropriate routing option 365b7cb133eSJeenu Viswambharan * (4) is satisfied by client calling PE_UNMASK 366b7cb133eSJeenu Viswambharan * (5) and (6) is enforced using interrupt priority, the RPR, in GIC: 367b7cb133eSJeenu Viswambharan * - Normal SDEI events belong to Normal SDE priority class 368b7cb133eSJeenu Viswambharan * - Critical SDEI events belong to Critical CSDE priority class 369b7cb133eSJeenu Viswambharan * 370b7cb133eSJeenu Viswambharan * The interrupt has already been acknowledged, and therefore is active, 371b7cb133eSJeenu Viswambharan * so no other PE can handle this event while we are at it. 372b7cb133eSJeenu Viswambharan * 373b7cb133eSJeenu Viswambharan * Find if this is an SDEI interrupt. There must be an event mapped to 374b7cb133eSJeenu Viswambharan * this interrupt 375b7cb133eSJeenu Viswambharan */ 376b7cb133eSJeenu Viswambharan intr = plat_ic_get_interrupt_id(intr_raw); 377ba6e5ca6SJeenu Viswambharan map = find_event_map_by_intr(intr, (plat_ic_is_spi(intr) != 0)); 378ba6e5ca6SJeenu Viswambharan if (map == NULL) { 379b7cb133eSJeenu Viswambharan ERROR("No SDEI map for interrupt %u\n", intr); 380b7cb133eSJeenu Viswambharan panic(); 381b7cb133eSJeenu Viswambharan } 382b7cb133eSJeenu Viswambharan 383b7cb133eSJeenu Viswambharan /* 384b7cb133eSJeenu Viswambharan * Received interrupt number must either correspond to event 0, or must 385b7cb133eSJeenu Viswambharan * be bound interrupt. 386b7cb133eSJeenu Viswambharan */ 387b7cb133eSJeenu Viswambharan assert((map->ev_num == SDEI_EVENT_0) || is_map_bound(map)); 388b7cb133eSJeenu Viswambharan 389b7cb133eSJeenu Viswambharan se = get_event_entry(map); 390b7cb133eSJeenu Viswambharan state = sdei_get_this_pe_state(); 391b7cb133eSJeenu Viswambharan 392ba6e5ca6SJeenu Viswambharan if (state->pe_masked) { 393b7cb133eSJeenu Viswambharan /* 394b7cb133eSJeenu Viswambharan * Interrupts received while this PE was masked can't be 395b7cb133eSJeenu Viswambharan * dispatched. 396b7cb133eSJeenu Viswambharan */ 397ba6e5ca6SJeenu Viswambharan SDEI_LOG("interrupt %u on %llx while PE masked\n", map->intr, 398ba6e5ca6SJeenu Viswambharan mpidr); 399b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 400b7cb133eSJeenu Viswambharan sdei_map_lock(map); 401b7cb133eSJeenu Viswambharan 402b7cb133eSJeenu Viswambharan handle_masked_trigger(map, se, state, intr_raw); 403b7cb133eSJeenu Viswambharan 404b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 405b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 406b7cb133eSJeenu Viswambharan 407b7cb133eSJeenu Viswambharan return 0; 408b7cb133eSJeenu Viswambharan } 409b7cb133eSJeenu Viswambharan 410b7cb133eSJeenu Viswambharan /* Insert load barrier for signalled SDEI event */ 411b7cb133eSJeenu Viswambharan if (map->ev_num == SDEI_EVENT_0) 412b7cb133eSJeenu Viswambharan dmbld(); 413b7cb133eSJeenu Viswambharan 414b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 415b7cb133eSJeenu Viswambharan sdei_map_lock(map); 416b7cb133eSJeenu Viswambharan 417b7cb133eSJeenu Viswambharan /* Assert shared event routed to this PE had been configured so */ 418b7cb133eSJeenu Viswambharan if (is_event_shared(map) && (se->reg_flags == SDEI_REGF_RM_PE)) { 419ba6e5ca6SJeenu Viswambharan assert(se->affinity == (mpidr & MPIDR_AFFINITY_MASK)); 420b7cb133eSJeenu Viswambharan } 421b7cb133eSJeenu Viswambharan 422b7cb133eSJeenu Viswambharan if (!can_sdei_state_trans(se, DO_DISPATCH)) { 423b7cb133eSJeenu Viswambharan SDEI_LOG("SDEI event 0x%x can't be dispatched; state=0x%x\n", 424b7cb133eSJeenu Viswambharan map->ev_num, se->state); 425b7cb133eSJeenu Viswambharan 426b7cb133eSJeenu Viswambharan /* 427b7cb133eSJeenu Viswambharan * If the event is registered, leave the interrupt pending so 428b7cb133eSJeenu Viswambharan * that it's delivered when the event is enabled. 429b7cb133eSJeenu Viswambharan */ 430b7cb133eSJeenu Viswambharan if (GET_EV_STATE(se, REGISTERED)) 431b7cb133eSJeenu Viswambharan plat_ic_set_interrupt_pending(map->intr); 432b7cb133eSJeenu Viswambharan 433b7cb133eSJeenu Viswambharan /* 434b7cb133eSJeenu Viswambharan * The interrupt was disabled or unregistered after the handler 435b7cb133eSJeenu Viswambharan * started to execute, which means now the interrupt is already 436b7cb133eSJeenu Viswambharan * disabled and we just need to EOI the interrupt. 437b7cb133eSJeenu Viswambharan */ 438b7cb133eSJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 439b7cb133eSJeenu Viswambharan 440b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 441b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 442b7cb133eSJeenu Viswambharan 443b7cb133eSJeenu Viswambharan return 0; 444b7cb133eSJeenu Viswambharan } 445b7cb133eSJeenu Viswambharan 446b7cb133eSJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 447b7cb133eSJeenu Viswambharan if (is_event_critical(map)) { 448b7cb133eSJeenu Viswambharan /* 449b7cb133eSJeenu Viswambharan * If this event is Critical, and if there's an outstanding 450b7cb133eSJeenu Viswambharan * dispatch, assert the latter is a Normal dispatch. Critical 451b7cb133eSJeenu Viswambharan * events can preempt an outstanding Normal event dispatch. 452b7cb133eSJeenu Viswambharan */ 453ba6e5ca6SJeenu Viswambharan if (disp_ctx != NULL) 454b7cb133eSJeenu Viswambharan assert(is_event_normal(disp_ctx->map)); 455b7cb133eSJeenu Viswambharan } else { 456b7cb133eSJeenu Viswambharan /* 457b7cb133eSJeenu Viswambharan * If this event is Normal, assert that there are no outstanding 458b7cb133eSJeenu Viswambharan * dispatches. Normal events can't preempt any outstanding event 459b7cb133eSJeenu Viswambharan * dispatches. 460b7cb133eSJeenu Viswambharan */ 461b7cb133eSJeenu Viswambharan assert(disp_ctx == NULL); 462b7cb133eSJeenu Viswambharan } 463b7cb133eSJeenu Viswambharan 464b7cb133eSJeenu Viswambharan sec_state = get_interrupt_src_ss(flags); 465b7cb133eSJeenu Viswambharan 466b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 467b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 468b7cb133eSJeenu Viswambharan 469ba6e5ca6SJeenu Viswambharan SDEI_LOG("ACK %llx, ev:%d ss:%d spsr:%lx ELR:%lx\n", mpidr, map->ev_num, 470ba6e5ca6SJeenu Viswambharan sec_state, read_spsr_el3(), read_elr_el3()); 471b7cb133eSJeenu Viswambharan 472b7cb133eSJeenu Viswambharan ctx = handle; 473b7cb133eSJeenu Viswambharan 474b7cb133eSJeenu Viswambharan /* 475b7cb133eSJeenu Viswambharan * Check if we interrupted secure state. Perform a context switch so 476b7cb133eSJeenu Viswambharan * that we can delegate to NS. 477b7cb133eSJeenu Viswambharan */ 478b7cb133eSJeenu Viswambharan if (sec_state == SECURE) { 479b7cb133eSJeenu Viswambharan save_secure_context(); 480b7cb133eSJeenu Viswambharan ctx = restore_and_resume_ns_context(); 481b7cb133eSJeenu Viswambharan } 482b7cb133eSJeenu Viswambharan 483cdb6ac94SJeenu Viswambharan /* Synchronously dispatch event */ 484cdb6ac94SJeenu Viswambharan setup_ns_dispatch(map, se, ctx, &dispatch_jmp); 485cdb6ac94SJeenu Viswambharan begin_sdei_synchronous_dispatch(&dispatch_jmp); 486b7cb133eSJeenu Viswambharan 487b7cb133eSJeenu Viswambharan /* 488cdb6ac94SJeenu Viswambharan * We reach here when client completes the event. 489cdb6ac94SJeenu Viswambharan * 49090a9213bSJeenu Viswambharan * If the cause of dispatch originally interrupted the Secure world, 491cdb6ac94SJeenu Viswambharan * resume Secure. 492cdb6ac94SJeenu Viswambharan * 493cdb6ac94SJeenu Viswambharan * No need to save the Non-secure context ahead of a world switch: the 494cdb6ac94SJeenu Viswambharan * Non-secure context was fully saved before dispatch, and has been 495cdb6ac94SJeenu Viswambharan * returned to its pre-dispatch state. 496b7cb133eSJeenu Viswambharan */ 49790a9213bSJeenu Viswambharan if (sec_state == SECURE) 498cdb6ac94SJeenu Viswambharan restore_and_resume_secure_context(); 499cdb6ac94SJeenu Viswambharan 500cdb6ac94SJeenu Viswambharan /* 501cdb6ac94SJeenu Viswambharan * The event was dispatched after receiving SDEI interrupt. With 502cdb6ac94SJeenu Viswambharan * the event handling completed, EOI the corresponding 503cdb6ac94SJeenu Viswambharan * interrupt. 504cdb6ac94SJeenu Viswambharan */ 505297a9a0fSJeenu Viswambharan if ((map->ev_num != SDEI_EVENT_0) && !is_map_bound(map)) { 506cdb6ac94SJeenu Viswambharan ERROR("Invalid SDEI mapping: ev=%u\n", map->ev_num); 507cdb6ac94SJeenu Viswambharan panic(); 508cdb6ac94SJeenu Viswambharan } 509cdb6ac94SJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 510cdb6ac94SJeenu Viswambharan 511b7cb133eSJeenu Viswambharan return 0; 512b7cb133eSJeenu Viswambharan } 513b7cb133eSJeenu Viswambharan 514cdb6ac94SJeenu Viswambharan /* 515cdb6ac94SJeenu Viswambharan * Explicitly dispatch the given SDEI event. 516cdb6ac94SJeenu Viswambharan * 517cdb6ac94SJeenu Viswambharan * When calling this API, the caller must be prepared for the SDEI dispatcher to 518cdb6ac94SJeenu Viswambharan * restore and make Non-secure context as active. This call returns only after 519cdb6ac94SJeenu Viswambharan * the client has completed the dispatch. Then, the Non-secure context will be 520cdb6ac94SJeenu Viswambharan * active, and the following ERET will return to Non-secure. 521cdb6ac94SJeenu Viswambharan * 522cdb6ac94SJeenu Viswambharan * Should the caller require re-entry to Secure, it must restore the Secure 523cdb6ac94SJeenu Viswambharan * context and program registers for ERET. 524cdb6ac94SJeenu Viswambharan */ 525cdb6ac94SJeenu Viswambharan int sdei_dispatch_event(int ev_num) 52655a1266eSJeenu Viswambharan { 52755a1266eSJeenu Viswambharan sdei_entry_t *se; 52855a1266eSJeenu Viswambharan sdei_ev_map_t *map; 529cdb6ac94SJeenu Viswambharan cpu_context_t *ns_ctx; 53055a1266eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 53155a1266eSJeenu Viswambharan sdei_cpu_state_t *state; 532cdb6ac94SJeenu Viswambharan struct jmpbuf dispatch_jmp; 53355a1266eSJeenu Viswambharan 53455a1266eSJeenu Viswambharan /* Can't dispatch if events are masked on this PE */ 53555a1266eSJeenu Viswambharan state = sdei_get_this_pe_state(); 536ba6e5ca6SJeenu Viswambharan if (state->pe_masked) 53755a1266eSJeenu Viswambharan return -1; 53855a1266eSJeenu Viswambharan 53955a1266eSJeenu Viswambharan /* Event 0 can't be dispatched */ 54055a1266eSJeenu Viswambharan if (ev_num == SDEI_EVENT_0) 54155a1266eSJeenu Viswambharan return -1; 54255a1266eSJeenu Viswambharan 54355a1266eSJeenu Viswambharan /* Locate mapping corresponding to this event */ 54455a1266eSJeenu Viswambharan map = find_event_map(ev_num); 545ba6e5ca6SJeenu Viswambharan if (map == NULL) 54655a1266eSJeenu Viswambharan return -1; 54755a1266eSJeenu Viswambharan 548af2c9ecdSJeenu Viswambharan /* Only explicit events can be dispatched */ 549af2c9ecdSJeenu Viswambharan if (!is_map_explicit(map)) 55055a1266eSJeenu Viswambharan return -1; 55155a1266eSJeenu Viswambharan 55255a1266eSJeenu Viswambharan /* Examine state of dispatch stack */ 55355a1266eSJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 554ba6e5ca6SJeenu Viswambharan if (disp_ctx != NULL) { 55555a1266eSJeenu Viswambharan /* 55655a1266eSJeenu Viswambharan * There's an outstanding dispatch. If the outstanding dispatch 55755a1266eSJeenu Viswambharan * is critical, no more dispatches are possible. 55855a1266eSJeenu Viswambharan */ 55955a1266eSJeenu Viswambharan if (is_event_critical(disp_ctx->map)) 56055a1266eSJeenu Viswambharan return -1; 56155a1266eSJeenu Viswambharan 56255a1266eSJeenu Viswambharan /* 56355a1266eSJeenu Viswambharan * If the outstanding dispatch is Normal, only critical events 56455a1266eSJeenu Viswambharan * can be dispatched. 56555a1266eSJeenu Viswambharan */ 56655a1266eSJeenu Viswambharan if (is_event_normal(map)) 56755a1266eSJeenu Viswambharan return -1; 56855a1266eSJeenu Viswambharan } 56955a1266eSJeenu Viswambharan 57055a1266eSJeenu Viswambharan se = get_event_entry(map); 57155a1266eSJeenu Viswambharan if (!can_sdei_state_trans(se, DO_DISPATCH)) 57255a1266eSJeenu Viswambharan return -1; 57355a1266eSJeenu Viswambharan 57455a1266eSJeenu Viswambharan /* Activate the priority corresponding to the event being dispatched */ 57555a1266eSJeenu Viswambharan ehf_activate_priority(sdei_event_priority(map)); 57655a1266eSJeenu Viswambharan 57755a1266eSJeenu Viswambharan /* 578cdb6ac94SJeenu Viswambharan * Prepare for NS dispatch by restoring the Non-secure context and 579cdb6ac94SJeenu Viswambharan * marking that as active. 58055a1266eSJeenu Viswambharan */ 581cdb6ac94SJeenu Viswambharan ns_ctx = restore_and_resume_ns_context(); 582cdb6ac94SJeenu Viswambharan 583cdb6ac94SJeenu Viswambharan /* Dispatch event synchronously */ 584cdb6ac94SJeenu Viswambharan setup_ns_dispatch(map, se, ns_ctx, &dispatch_jmp); 585cdb6ac94SJeenu Viswambharan begin_sdei_synchronous_dispatch(&dispatch_jmp); 58655a1266eSJeenu Viswambharan 58755a1266eSJeenu Viswambharan /* 588cdb6ac94SJeenu Viswambharan * We reach here when client completes the event. 589cdb6ac94SJeenu Viswambharan * 590cdb6ac94SJeenu Viswambharan * Deactivate the priority level that was activated at the time of 591cdb6ac94SJeenu Viswambharan * explicit dispatch. 59255a1266eSJeenu Viswambharan */ 593cdb6ac94SJeenu Viswambharan ehf_deactivate_priority(sdei_event_priority(map)); 59455a1266eSJeenu Viswambharan 59555a1266eSJeenu Viswambharan return 0; 59655a1266eSJeenu Viswambharan } 59755a1266eSJeenu Viswambharan 5985e60c39aSJeenu Viswambharan static void end_sdei_synchronous_dispatch(struct jmpbuf *buffer) 599cdb6ac94SJeenu Viswambharan { 600cdb6ac94SJeenu Viswambharan longjmp(buffer); 601cdb6ac94SJeenu Viswambharan } 602cdb6ac94SJeenu Viswambharan 603ba6e5ca6SJeenu Viswambharan int sdei_event_complete(bool resume, uint64_t pc) 604b7cb133eSJeenu Viswambharan { 605b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 606b7cb133eSJeenu Viswambharan sdei_entry_t *se; 607b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 608b7cb133eSJeenu Viswambharan cpu_context_t *ctx; 609b7cb133eSJeenu Viswambharan sdei_action_t act; 610b7cb133eSJeenu Viswambharan unsigned int client_el = sdei_client_el(); 611b7cb133eSJeenu Viswambharan 612b7cb133eSJeenu Viswambharan /* Return error if called without an active event */ 6138e3032f9SJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 614ba6e5ca6SJeenu Viswambharan if (disp_ctx == NULL) 615b7cb133eSJeenu Viswambharan return SDEI_EDENY; 616b7cb133eSJeenu Viswambharan 617b7cb133eSJeenu Viswambharan /* Validate resumption point */ 618b7cb133eSJeenu Viswambharan if (resume && (plat_sdei_validate_entry_point(pc, client_el) != 0)) 619b7cb133eSJeenu Viswambharan return SDEI_EDENY; 620b7cb133eSJeenu Viswambharan 621b7cb133eSJeenu Viswambharan map = disp_ctx->map; 622ba6e5ca6SJeenu Viswambharan assert(map != NULL); 623b7cb133eSJeenu Viswambharan se = get_event_entry(map); 624b7cb133eSJeenu Viswambharan 625611eb9cfSJeenu Viswambharan if (is_event_shared(map)) 626611eb9cfSJeenu Viswambharan sdei_map_lock(map); 627611eb9cfSJeenu Viswambharan 628b7cb133eSJeenu Viswambharan act = resume ? DO_COMPLETE_RESUME : DO_COMPLETE; 629b7cb133eSJeenu Viswambharan if (!can_sdei_state_trans(se, act)) { 630b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 631b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 632b7cb133eSJeenu Viswambharan return SDEI_EDENY; 633b7cb133eSJeenu Viswambharan } 634b7cb133eSJeenu Viswambharan 635611eb9cfSJeenu Viswambharan if (is_event_shared(map)) 636611eb9cfSJeenu Viswambharan sdei_map_unlock(map); 637611eb9cfSJeenu Viswambharan 6388e3032f9SJeenu Viswambharan /* Having done sanity checks, pop dispatch */ 639ba6e5ca6SJeenu Viswambharan (void) pop_dispatch(); 6408e3032f9SJeenu Viswambharan 6418e3032f9SJeenu Viswambharan SDEI_LOG("EOI:%lx, %d spsr:%lx elr:%lx\n", read_mpidr_el1(), 6428e3032f9SJeenu Viswambharan map->ev_num, read_spsr_el3(), read_elr_el3()); 6438e3032f9SJeenu Viswambharan 644b7cb133eSJeenu Viswambharan /* 645b7cb133eSJeenu Viswambharan * Restore Non-secure to how it was originally interrupted. Once done, 646b7cb133eSJeenu Viswambharan * it's up-to-date with the saved copy. 647b7cb133eSJeenu Viswambharan */ 648b7cb133eSJeenu Viswambharan ctx = cm_get_context(NON_SECURE); 649b7cb133eSJeenu Viswambharan restore_event_ctx(disp_ctx, ctx); 650b7cb133eSJeenu Viswambharan 651b7cb133eSJeenu Viswambharan if (resume) { 652b7cb133eSJeenu Viswambharan /* 653b7cb133eSJeenu Viswambharan * Complete-and-resume call. Prepare the Non-secure context 654b7cb133eSJeenu Viswambharan * (currently active) for complete and resume. 655b7cb133eSJeenu Viswambharan */ 656b7cb133eSJeenu Viswambharan cm_set_elr_spsr_el3(NON_SECURE, pc, SPSR_64(client_el, 657b7cb133eSJeenu Viswambharan MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 658b7cb133eSJeenu Viswambharan 659b7cb133eSJeenu Viswambharan /* 660b7cb133eSJeenu Viswambharan * Make it look as if a synchronous exception were taken at the 661b7cb133eSJeenu Viswambharan * supplied Non-secure resumption point. Populate SPSR and 662b7cb133eSJeenu Viswambharan * ELR_ELx so that an ERET from there works as expected. 663b7cb133eSJeenu Viswambharan * 664b7cb133eSJeenu Viswambharan * The assumption is that the client, if necessary, would have 665b7cb133eSJeenu Viswambharan * saved any live content in these registers before making this 666b7cb133eSJeenu Viswambharan * call. 667b7cb133eSJeenu Viswambharan */ 668b7cb133eSJeenu Viswambharan if (client_el == MODE_EL2) { 669b7cb133eSJeenu Viswambharan write_elr_el2(disp_ctx->elr_el3); 670b7cb133eSJeenu Viswambharan write_spsr_el2(disp_ctx->spsr_el3); 671b7cb133eSJeenu Viswambharan } else { 672b7cb133eSJeenu Viswambharan /* EL1 */ 673b7cb133eSJeenu Viswambharan write_elr_el1(disp_ctx->elr_el3); 674b7cb133eSJeenu Viswambharan write_spsr_el1(disp_ctx->spsr_el3); 675b7cb133eSJeenu Viswambharan } 676b7cb133eSJeenu Viswambharan } 677b7cb133eSJeenu Viswambharan 678cdb6ac94SJeenu Viswambharan /* End the outstanding dispatch */ 6795e60c39aSJeenu Viswambharan end_sdei_synchronous_dispatch(disp_ctx->dispatch_jmp); 680b7cb133eSJeenu Viswambharan 681b7cb133eSJeenu Viswambharan return 0; 682b7cb133eSJeenu Viswambharan } 683b7cb133eSJeenu Viswambharan 684ba6e5ca6SJeenu Viswambharan int64_t sdei_event_context(void *handle, unsigned int param) 685b7cb133eSJeenu Viswambharan { 686b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 687b7cb133eSJeenu Viswambharan 688b7cb133eSJeenu Viswambharan if (param >= SDEI_SAVED_GPREGS) 689b7cb133eSJeenu Viswambharan return SDEI_EINVAL; 690b7cb133eSJeenu Viswambharan 691b7cb133eSJeenu Viswambharan /* Get outstanding dispatch on this CPU */ 692b7cb133eSJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 693ba6e5ca6SJeenu Viswambharan if (disp_ctx == NULL) 694b7cb133eSJeenu Viswambharan return SDEI_EDENY; 695b7cb133eSJeenu Viswambharan 696ba6e5ca6SJeenu Viswambharan assert(disp_ctx->map != NULL); 697b7cb133eSJeenu Viswambharan 698b7cb133eSJeenu Viswambharan if (!can_sdei_state_trans(get_event_entry(disp_ctx->map), DO_CONTEXT)) 699b7cb133eSJeenu Viswambharan return SDEI_EDENY; 700b7cb133eSJeenu Viswambharan 701b7cb133eSJeenu Viswambharan /* 702b7cb133eSJeenu Viswambharan * No locking is required for the Running status as this is the only CPU 703b7cb133eSJeenu Viswambharan * which can complete the event 704b7cb133eSJeenu Viswambharan */ 705b7cb133eSJeenu Viswambharan 706ba6e5ca6SJeenu Viswambharan return (int64_t) disp_ctx->x[param]; 707b7cb133eSJeenu Viswambharan } 708