1b7cb133eSJeenu Viswambharan /* 2*0a33adc0SGovindraj Raja * Copyright (c) 2017-2024, 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> 84ce3e99aSScott Branden #include <inttypes.h> 94ce3e99aSScott Branden #include <stdint.h> 10b7cb133eSJeenu Viswambharan #include <string.h> 1109d40e0eSAntonio Nino Diaz 1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1337596fcbSDaniel Boulby #include <arch_features.h> 1409d40e0eSAntonio Nino Diaz #include <bl31/ehf.h> 1509d40e0eSAntonio Nino Diaz #include <bl31/interrupt_mgmt.h> 1609d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1709d40e0eSAntonio Nino Diaz #include <common/debug.h> 1809d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 1909d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 2009d40e0eSAntonio Nino Diaz #include <services/sdei.h> 2109d40e0eSAntonio Nino Diaz 22b7cb133eSJeenu Viswambharan #include "sdei_private.h" 23b7cb133eSJeenu Viswambharan 24b7cb133eSJeenu Viswambharan /* x0-x17 GPREGS context */ 25ba6e5ca6SJeenu Viswambharan #define SDEI_SAVED_GPREGS 18U 26b7cb133eSJeenu Viswambharan 27b7cb133eSJeenu Viswambharan /* Maximum preemption nesting levels: Critical priority and Normal priority */ 28ba6e5ca6SJeenu Viswambharan #define MAX_EVENT_NESTING 2U 29b7cb133eSJeenu Viswambharan 30b7cb133eSJeenu Viswambharan /* Per-CPU SDEI state access macro */ 31ba6e5ca6SJeenu Viswambharan #define sdei_get_this_pe_state() (&cpu_state[plat_my_core_pos()]) 32b7cb133eSJeenu Viswambharan 33b7cb133eSJeenu Viswambharan /* Structure to store information about an outstanding dispatch */ 34b7cb133eSJeenu Viswambharan typedef struct sdei_dispatch_context { 35b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 36b7cb133eSJeenu Viswambharan uint64_t x[SDEI_SAVED_GPREGS]; 37e0566305SAntonio Nino Diaz jmp_buf *dispatch_jmp; 38b7cb133eSJeenu Viswambharan 39b7cb133eSJeenu Viswambharan /* Exception state registers */ 40b7cb133eSJeenu Viswambharan uint64_t elr_el3; 41b7cb133eSJeenu Viswambharan uint64_t spsr_el3; 426f03bc77SDimitris Papastamos 436f03bc77SDimitris Papastamos #if DYNAMIC_WORKAROUND_CVE_2018_3639 446f03bc77SDimitris Papastamos /* CVE-2018-3639 mitigation state */ 456f03bc77SDimitris Papastamos uint64_t disable_cve_2018_3639; 466f03bc77SDimitris Papastamos #endif 47b7cb133eSJeenu Viswambharan } sdei_dispatch_context_t; 48b7cb133eSJeenu Viswambharan 49b7cb133eSJeenu Viswambharan /* Per-CPU SDEI state data */ 50b7cb133eSJeenu Viswambharan typedef struct sdei_cpu_state { 51b7cb133eSJeenu Viswambharan sdei_dispatch_context_t dispatch_stack[MAX_EVENT_NESTING]; 52b7cb133eSJeenu Viswambharan unsigned short stack_top; /* Empty ascending */ 53ba6e5ca6SJeenu Viswambharan bool pe_masked; 54ba6e5ca6SJeenu Viswambharan bool pending_enables; 55b7cb133eSJeenu Viswambharan } sdei_cpu_state_t; 56b7cb133eSJeenu Viswambharan 57b7cb133eSJeenu Viswambharan /* SDEI states for all cores in the system */ 58ba6e5ca6SJeenu Viswambharan static sdei_cpu_state_t cpu_state[PLATFORM_CORE_COUNT]; 59b7cb133eSJeenu Viswambharan 60ba6e5ca6SJeenu Viswambharan int64_t sdei_pe_mask(void) 61b7cb133eSJeenu Viswambharan { 62ba6e5ca6SJeenu Viswambharan int64_t ret = 0; 63b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 64b7cb133eSJeenu Viswambharan 65b7cb133eSJeenu Viswambharan /* 66b7cb133eSJeenu Viswambharan * Return value indicates whether this call had any effect in the mask 67b7cb133eSJeenu Viswambharan * status of this PE. 68b7cb133eSJeenu Viswambharan */ 69ba6e5ca6SJeenu Viswambharan if (!state->pe_masked) { 70ba6e5ca6SJeenu Viswambharan state->pe_masked = true; 71ba6e5ca6SJeenu Viswambharan ret = 1; 72ba6e5ca6SJeenu Viswambharan } 73b7cb133eSJeenu Viswambharan 74b7cb133eSJeenu Viswambharan return ret; 75b7cb133eSJeenu Viswambharan } 76b7cb133eSJeenu Viswambharan 77b7cb133eSJeenu Viswambharan void sdei_pe_unmask(void) 78b7cb133eSJeenu Viswambharan { 79ba6e5ca6SJeenu Viswambharan unsigned int i; 80b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 81b7cb133eSJeenu Viswambharan sdei_entry_t *se; 82b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 83b7cb133eSJeenu Viswambharan uint64_t my_mpidr = read_mpidr_el1() & MPIDR_AFFINITY_MASK; 84b7cb133eSJeenu Viswambharan 85b7cb133eSJeenu Viswambharan /* 86b7cb133eSJeenu Viswambharan * If there are pending enables, iterate through the private mappings 87b7cb133eSJeenu Viswambharan * and enable those bound maps that are in enabled state. Also, iterate 88b7cb133eSJeenu Viswambharan * through shared mappings and enable interrupts of events that are 89b7cb133eSJeenu Viswambharan * targeted to this PE. 90b7cb133eSJeenu Viswambharan */ 91b7cb133eSJeenu Viswambharan if (state->pending_enables) { 92b7cb133eSJeenu Viswambharan for_each_private_map(i, map) { 93b7cb133eSJeenu Viswambharan se = get_event_entry(map); 94b7cb133eSJeenu Viswambharan if (is_map_bound(map) && GET_EV_STATE(se, ENABLED)) 95b7cb133eSJeenu Viswambharan plat_ic_enable_interrupt(map->intr); 96b7cb133eSJeenu Viswambharan } 97b7cb133eSJeenu Viswambharan 98b7cb133eSJeenu Viswambharan for_each_shared_map(i, map) { 99b7cb133eSJeenu Viswambharan se = get_event_entry(map); 100b7cb133eSJeenu Viswambharan 101b7cb133eSJeenu Viswambharan sdei_map_lock(map); 102ba6e5ca6SJeenu Viswambharan if (is_map_bound(map) && GET_EV_STATE(se, ENABLED) && 103b7cb133eSJeenu Viswambharan (se->reg_flags == SDEI_REGF_RM_PE) && 104b7cb133eSJeenu Viswambharan (se->affinity == my_mpidr)) { 105b7cb133eSJeenu Viswambharan plat_ic_enable_interrupt(map->intr); 106b7cb133eSJeenu Viswambharan } 107b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 108b7cb133eSJeenu Viswambharan } 109b7cb133eSJeenu Viswambharan } 110b7cb133eSJeenu Viswambharan 111ba6e5ca6SJeenu Viswambharan state->pending_enables = false; 112ba6e5ca6SJeenu Viswambharan state->pe_masked = false; 113b7cb133eSJeenu Viswambharan } 114b7cb133eSJeenu Viswambharan 115b7cb133eSJeenu Viswambharan /* Push a dispatch context to the dispatch stack */ 116b7cb133eSJeenu Viswambharan static sdei_dispatch_context_t *push_dispatch(void) 117b7cb133eSJeenu Viswambharan { 118b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 119b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 120b7cb133eSJeenu Viswambharan 121b7cb133eSJeenu Viswambharan /* Cannot have more than max events */ 122b7cb133eSJeenu Viswambharan assert(state->stack_top < MAX_EVENT_NESTING); 123b7cb133eSJeenu Viswambharan 124b7cb133eSJeenu Viswambharan disp_ctx = &state->dispatch_stack[state->stack_top]; 125b7cb133eSJeenu Viswambharan state->stack_top++; 126b7cb133eSJeenu Viswambharan 127b7cb133eSJeenu Viswambharan return disp_ctx; 128b7cb133eSJeenu Viswambharan } 129b7cb133eSJeenu Viswambharan 130b7cb133eSJeenu Viswambharan /* Pop a dispatch context to the dispatch stack */ 131b7cb133eSJeenu Viswambharan static sdei_dispatch_context_t *pop_dispatch(void) 132b7cb133eSJeenu Viswambharan { 133b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 134b7cb133eSJeenu Viswambharan 135ba6e5ca6SJeenu Viswambharan if (state->stack_top == 0U) 136b7cb133eSJeenu Viswambharan return NULL; 137b7cb133eSJeenu Viswambharan 138b7cb133eSJeenu Viswambharan assert(state->stack_top <= MAX_EVENT_NESTING); 139b7cb133eSJeenu Viswambharan 140b7cb133eSJeenu Viswambharan state->stack_top--; 141b7cb133eSJeenu Viswambharan 142b7cb133eSJeenu Viswambharan return &state->dispatch_stack[state->stack_top]; 143b7cb133eSJeenu Viswambharan } 144b7cb133eSJeenu Viswambharan 145b7cb133eSJeenu Viswambharan /* Retrieve the context at the top of dispatch stack */ 146b7cb133eSJeenu Viswambharan static sdei_dispatch_context_t *get_outstanding_dispatch(void) 147b7cb133eSJeenu Viswambharan { 148b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state = sdei_get_this_pe_state(); 149b7cb133eSJeenu Viswambharan 150ba6e5ca6SJeenu Viswambharan if (state->stack_top == 0U) 151b7cb133eSJeenu Viswambharan return NULL; 152b7cb133eSJeenu Viswambharan 153b7cb133eSJeenu Viswambharan assert(state->stack_top <= MAX_EVENT_NESTING); 154b7cb133eSJeenu Viswambharan 155ba6e5ca6SJeenu Viswambharan return &state->dispatch_stack[state->stack_top - 1U]; 156b7cb133eSJeenu Viswambharan } 157b7cb133eSJeenu Viswambharan 158cdb6ac94SJeenu Viswambharan static sdei_dispatch_context_t *save_event_ctx(sdei_ev_map_t *map, 159cdb6ac94SJeenu Viswambharan void *tgt_ctx) 160b7cb133eSJeenu Viswambharan { 161b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 162ba6e5ca6SJeenu Viswambharan const gp_regs_t *tgt_gpregs; 163ba6e5ca6SJeenu Viswambharan const el3_state_t *tgt_el3; 164b7cb133eSJeenu Viswambharan 165ba6e5ca6SJeenu Viswambharan assert(tgt_ctx != NULL); 166b7cb133eSJeenu Viswambharan tgt_gpregs = get_gpregs_ctx(tgt_ctx); 167b7cb133eSJeenu Viswambharan tgt_el3 = get_el3state_ctx(tgt_ctx); 168b7cb133eSJeenu Viswambharan 169b7cb133eSJeenu Viswambharan disp_ctx = push_dispatch(); 170ba6e5ca6SJeenu Viswambharan assert(disp_ctx != NULL); 171b7cb133eSJeenu Viswambharan disp_ctx->map = map; 172b7cb133eSJeenu Viswambharan 173b7cb133eSJeenu Viswambharan /* Save general purpose and exception registers */ 174b7cb133eSJeenu Viswambharan memcpy(disp_ctx->x, tgt_gpregs, sizeof(disp_ctx->x)); 175b7cb133eSJeenu Viswambharan disp_ctx->spsr_el3 = read_ctx_reg(tgt_el3, CTX_SPSR_EL3); 176b7cb133eSJeenu Viswambharan disp_ctx->elr_el3 = read_ctx_reg(tgt_el3, CTX_ELR_EL3); 1776f03bc77SDimitris Papastamos 178cdb6ac94SJeenu Viswambharan return disp_ctx; 179b7cb133eSJeenu Viswambharan } 180b7cb133eSJeenu Viswambharan 181ba6e5ca6SJeenu Viswambharan static void restore_event_ctx(const sdei_dispatch_context_t *disp_ctx, void *tgt_ctx) 182b7cb133eSJeenu Viswambharan { 183b7cb133eSJeenu Viswambharan gp_regs_t *tgt_gpregs; 184b7cb133eSJeenu Viswambharan el3_state_t *tgt_el3; 185b7cb133eSJeenu Viswambharan 186ba6e5ca6SJeenu Viswambharan assert(tgt_ctx != NULL); 187b7cb133eSJeenu Viswambharan tgt_gpregs = get_gpregs_ctx(tgt_ctx); 188b7cb133eSJeenu Viswambharan tgt_el3 = get_el3state_ctx(tgt_ctx); 189b7cb133eSJeenu Viswambharan 190b7cb133eSJeenu Viswambharan CASSERT(sizeof(disp_ctx->x) == (SDEI_SAVED_GPREGS * sizeof(uint64_t)), 191b7cb133eSJeenu Viswambharan foo); 192b7cb133eSJeenu Viswambharan 193b7cb133eSJeenu Viswambharan /* Restore general purpose and exception registers */ 194b7cb133eSJeenu Viswambharan memcpy(tgt_gpregs, disp_ctx->x, sizeof(disp_ctx->x)); 195b7cb133eSJeenu Viswambharan write_ctx_reg(tgt_el3, CTX_SPSR_EL3, disp_ctx->spsr_el3); 196b7cb133eSJeenu Viswambharan write_ctx_reg(tgt_el3, CTX_ELR_EL3, disp_ctx->elr_el3); 1976f03bc77SDimitris Papastamos 1986f03bc77SDimitris Papastamos #if DYNAMIC_WORKAROUND_CVE_2018_3639 1996f03bc77SDimitris Papastamos cve_2018_3639_t *tgt_cve_2018_3639; 2006f03bc77SDimitris Papastamos tgt_cve_2018_3639 = get_cve_2018_3639_ctx(tgt_ctx); 2016f03bc77SDimitris Papastamos 2026f03bc77SDimitris Papastamos /* Restore CVE-2018-3639 mitigation state */ 2036f03bc77SDimitris Papastamos write_ctx_reg(tgt_cve_2018_3639, CTX_CVE_2018_3639_DISABLE, 2046f03bc77SDimitris Papastamos disp_ctx->disable_cve_2018_3639); 2056f03bc77SDimitris Papastamos #endif 206b7cb133eSJeenu Viswambharan } 207b7cb133eSJeenu Viswambharan 208b7cb133eSJeenu Viswambharan static void save_secure_context(void) 209b7cb133eSJeenu Viswambharan { 210b7cb133eSJeenu Viswambharan cm_el1_sysregs_context_save(SECURE); 211b7cb133eSJeenu Viswambharan } 212b7cb133eSJeenu Viswambharan 213b7cb133eSJeenu Viswambharan /* Restore Secure context and arrange to resume it at the next ERET */ 214b7cb133eSJeenu Viswambharan static void restore_and_resume_secure_context(void) 215b7cb133eSJeenu Viswambharan { 216b7cb133eSJeenu Viswambharan cm_el1_sysregs_context_restore(SECURE); 217b7cb133eSJeenu Viswambharan cm_set_next_eret_context(SECURE); 218b7cb133eSJeenu Viswambharan } 219b7cb133eSJeenu Viswambharan 220b7cb133eSJeenu Viswambharan /* 221b7cb133eSJeenu Viswambharan * Restore Non-secure context and arrange to resume it at the next ERET. Return 222b7cb133eSJeenu Viswambharan * pointer to the Non-secure context. 223b7cb133eSJeenu Viswambharan */ 224b7cb133eSJeenu Viswambharan static cpu_context_t *restore_and_resume_ns_context(void) 225b7cb133eSJeenu Viswambharan { 226b7cb133eSJeenu Viswambharan cpu_context_t *ns_ctx; 227b7cb133eSJeenu Viswambharan 228b7cb133eSJeenu Viswambharan cm_el1_sysregs_context_restore(NON_SECURE); 229b7cb133eSJeenu Viswambharan cm_set_next_eret_context(NON_SECURE); 230b7cb133eSJeenu Viswambharan 231b7cb133eSJeenu Viswambharan ns_ctx = cm_get_context(NON_SECURE); 232ba6e5ca6SJeenu Viswambharan assert(ns_ctx != NULL); 233b7cb133eSJeenu Viswambharan 234b7cb133eSJeenu Viswambharan return ns_ctx; 235b7cb133eSJeenu Viswambharan } 236b7cb133eSJeenu Viswambharan 237b7cb133eSJeenu Viswambharan /* 23837596fcbSDaniel Boulby * Prepare for ERET: 23937596fcbSDaniel Boulby * - Set the ELR to the registered handler address 24037596fcbSDaniel Boulby * - Set the SPSR register as described in the SDEI documentation and 24137596fcbSDaniel Boulby * the AArch64.TakeException() pseudocode function in 24237596fcbSDaniel Boulby * ARM DDI 0487F.c page J1-7635 24337596fcbSDaniel Boulby */ 24437596fcbSDaniel Boulby 24537596fcbSDaniel Boulby static void sdei_set_elr_spsr(sdei_entry_t *se, sdei_dispatch_context_t *disp_ctx) 24637596fcbSDaniel Boulby { 24737596fcbSDaniel Boulby unsigned int client_el = sdei_client_el(); 24837596fcbSDaniel Boulby u_register_t sdei_spsr = SPSR_64(client_el, MODE_SP_ELX, 24937596fcbSDaniel Boulby DISABLE_ALL_EXCEPTIONS); 25037596fcbSDaniel Boulby 25137596fcbSDaniel Boulby u_register_t interrupted_pstate = disp_ctx->spsr_el3; 25237596fcbSDaniel Boulby 25337596fcbSDaniel Boulby /* Check the SPAN bit in the client el SCTLR */ 25437596fcbSDaniel Boulby u_register_t client_el_sctlr; 25537596fcbSDaniel Boulby 25637596fcbSDaniel Boulby if (client_el == MODE_EL2) { 25737596fcbSDaniel Boulby client_el_sctlr = read_sctlr_el2(); 25837596fcbSDaniel Boulby } else { 25937596fcbSDaniel Boulby client_el_sctlr = read_sctlr_el1(); 26037596fcbSDaniel Boulby } 26137596fcbSDaniel Boulby 26237596fcbSDaniel Boulby /* 26337596fcbSDaniel Boulby * Check whether to force the PAN bit or use the value in the 26437596fcbSDaniel Boulby * interrupted EL according to the check described in 26537596fcbSDaniel Boulby * TakeException. Since the client can only be Non-Secure 26637596fcbSDaniel Boulby * EL2 or El1 some of the conditions in ElIsInHost() we know 26737596fcbSDaniel Boulby * will always be True. 26837596fcbSDaniel Boulby * When the client_el is EL2 we know that there will be a SPAN 26937596fcbSDaniel Boulby * bit in SCTLR_EL2 as we have already checked for the condition 27037596fcbSDaniel Boulby * HCR_EL2.E2H = 1 and HCR_EL2.TGE = 1 27137596fcbSDaniel Boulby */ 27237596fcbSDaniel Boulby u_register_t hcr_el2 = read_hcr(); 273ea735bf5SAndre Przywara bool el_is_in_host = (read_feat_vhe_id_field() != 0U) && 27437596fcbSDaniel Boulby (hcr_el2 & HCR_TGE_BIT) && 27537596fcbSDaniel Boulby (hcr_el2 & HCR_E2H_BIT); 27637596fcbSDaniel Boulby 2774f5ef849SAndre Przywara if (is_feat_pan_supported() && 27837596fcbSDaniel Boulby ((client_el == MODE_EL1) || 27937596fcbSDaniel Boulby (client_el == MODE_EL2 && el_is_in_host)) && 28037596fcbSDaniel Boulby ((client_el_sctlr & SCTLR_SPAN_BIT) == 0U)) { 28137596fcbSDaniel Boulby sdei_spsr |= SPSR_PAN_BIT; 28237596fcbSDaniel Boulby } else { 28337596fcbSDaniel Boulby sdei_spsr |= (interrupted_pstate & SPSR_PAN_BIT); 28437596fcbSDaniel Boulby } 28537596fcbSDaniel Boulby 28637596fcbSDaniel Boulby /* If SSBS is implemented, take the value from the client el SCTLR */ 28737596fcbSDaniel Boulby u_register_t ssbs_enabled = (read_id_aa64pfr1_el1() 28837596fcbSDaniel Boulby >> ID_AA64PFR1_EL1_SSBS_SHIFT) 28937596fcbSDaniel Boulby & ID_AA64PFR1_EL1_SSBS_MASK; 29037596fcbSDaniel Boulby if (ssbs_enabled != SSBS_UNAVAILABLE) { 29137596fcbSDaniel Boulby u_register_t ssbs_bit = ((client_el_sctlr & SCTLR_DSSBS_BIT) 29237596fcbSDaniel Boulby >> SCTLR_DSSBS_SHIFT) 29337596fcbSDaniel Boulby << SPSR_SSBS_SHIFT_AARCH64; 29437596fcbSDaniel Boulby sdei_spsr |= ssbs_bit; 29537596fcbSDaniel Boulby } 29637596fcbSDaniel Boulby 29737596fcbSDaniel Boulby /* If MTE is implemented in the client el set the TCO bit */ 298*0a33adc0SGovindraj Raja if (is_feat_mte_supported()) { 29937596fcbSDaniel Boulby sdei_spsr |= SPSR_TCO_BIT_AARCH64; 30037596fcbSDaniel Boulby } 30137596fcbSDaniel Boulby 30237596fcbSDaniel Boulby /* Take the DIT field from the pstate of the interrupted el */ 30337596fcbSDaniel Boulby sdei_spsr |= (interrupted_pstate & SPSR_DIT_BIT); 30437596fcbSDaniel Boulby 30537596fcbSDaniel Boulby cm_set_elr_spsr_el3(NON_SECURE, (uintptr_t) se->ep, sdei_spsr); 30637596fcbSDaniel Boulby } 30737596fcbSDaniel Boulby 30837596fcbSDaniel Boulby /* 309b7cb133eSJeenu Viswambharan * Populate the Non-secure context so that the next ERET will dispatch to the 310b7cb133eSJeenu Viswambharan * SDEI client. 311b7cb133eSJeenu Viswambharan */ 312b7cb133eSJeenu Viswambharan static void setup_ns_dispatch(sdei_ev_map_t *map, sdei_entry_t *se, 313e0566305SAntonio Nino Diaz cpu_context_t *ctx, jmp_buf *dispatch_jmp) 314b7cb133eSJeenu Viswambharan { 315cdb6ac94SJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 316b7cb133eSJeenu Viswambharan 317b7cb133eSJeenu Viswambharan /* Push the event and context */ 318cdb6ac94SJeenu Viswambharan disp_ctx = save_event_ctx(map, ctx); 319b7cb133eSJeenu Viswambharan 320b7cb133eSJeenu Viswambharan /* 321b7cb133eSJeenu Viswambharan * Setup handler arguments: 322b7cb133eSJeenu Viswambharan * 323b7cb133eSJeenu Viswambharan * - x0: Event number 324b7cb133eSJeenu Viswambharan * - x1: Handler argument supplied at the time of event registration 325b7cb133eSJeenu Viswambharan * - x2: Interrupted PC 326b7cb133eSJeenu Viswambharan * - x3: Interrupted SPSR 327b7cb133eSJeenu Viswambharan */ 328ba6e5ca6SJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X0, (uint64_t) map->ev_num); 329b7cb133eSJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X1, se->arg); 330cdb6ac94SJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X2, disp_ctx->elr_el3); 331cdb6ac94SJeenu Viswambharan SMC_SET_GP(ctx, CTX_GPREG_X3, disp_ctx->spsr_el3); 332b7cb133eSJeenu Viswambharan 33337596fcbSDaniel Boulby /* Setup the elr and spsr register to prepare for ERET */ 33437596fcbSDaniel Boulby sdei_set_elr_spsr(se, disp_ctx); 335cdb6ac94SJeenu Viswambharan 336cdb6ac94SJeenu Viswambharan #if DYNAMIC_WORKAROUND_CVE_2018_3639 337cdb6ac94SJeenu Viswambharan cve_2018_3639_t *tgt_cve_2018_3639; 338cdb6ac94SJeenu Viswambharan tgt_cve_2018_3639 = get_cve_2018_3639_ctx(ctx); 339cdb6ac94SJeenu Viswambharan 340cdb6ac94SJeenu Viswambharan /* Save CVE-2018-3639 mitigation state */ 341cdb6ac94SJeenu Viswambharan disp_ctx->disable_cve_2018_3639 = read_ctx_reg(tgt_cve_2018_3639, 342cdb6ac94SJeenu Viswambharan CTX_CVE_2018_3639_DISABLE); 343cdb6ac94SJeenu Viswambharan 344cdb6ac94SJeenu Viswambharan /* Force SDEI handler to execute with mitigation enabled by default */ 345cdb6ac94SJeenu Viswambharan write_ctx_reg(tgt_cve_2018_3639, CTX_CVE_2018_3639_DISABLE, 0); 346cdb6ac94SJeenu Viswambharan #endif 347cdb6ac94SJeenu Viswambharan 348cdb6ac94SJeenu Viswambharan disp_ctx->dispatch_jmp = dispatch_jmp; 349b7cb133eSJeenu Viswambharan } 350b7cb133eSJeenu Viswambharan 351b7cb133eSJeenu Viswambharan /* Handle a triggered SDEI interrupt while events were masked on this PE */ 352b7cb133eSJeenu Viswambharan static void handle_masked_trigger(sdei_ev_map_t *map, sdei_entry_t *se, 353b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state, unsigned int intr_raw) 354b7cb133eSJeenu Viswambharan { 355b7cb133eSJeenu Viswambharan uint64_t my_mpidr __unused = (read_mpidr_el1() & MPIDR_AFFINITY_MASK); 356ba6e5ca6SJeenu Viswambharan bool disable = false; 357b7cb133eSJeenu Viswambharan 358b7cb133eSJeenu Viswambharan /* Nothing to do for event 0 */ 359b7cb133eSJeenu Viswambharan if (map->ev_num == SDEI_EVENT_0) 360b7cb133eSJeenu Viswambharan return; 361b7cb133eSJeenu Viswambharan 362b7cb133eSJeenu Viswambharan /* 363b7cb133eSJeenu Viswambharan * For a private event, or for a shared event specifically routed to 364b7cb133eSJeenu Viswambharan * this CPU, we disable interrupt, leave the interrupt pending, and do 365b7cb133eSJeenu Viswambharan * EOI. 366b7cb133eSJeenu Viswambharan */ 367ba6e5ca6SJeenu Viswambharan if (is_event_private(map) || (se->reg_flags == SDEI_REGF_RM_PE)) 368ba6e5ca6SJeenu Viswambharan disable = true; 369ba6e5ca6SJeenu Viswambharan 370ba6e5ca6SJeenu Viswambharan if (se->reg_flags == SDEI_REGF_RM_PE) 371b7cb133eSJeenu Viswambharan assert(se->affinity == my_mpidr); 372b7cb133eSJeenu Viswambharan 373b7cb133eSJeenu Viswambharan if (disable) { 374b7cb133eSJeenu Viswambharan plat_ic_disable_interrupt(map->intr); 375b7cb133eSJeenu Viswambharan plat_ic_set_interrupt_pending(map->intr); 376b7cb133eSJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 377ba6e5ca6SJeenu Viswambharan state->pending_enables = true; 378b7cb133eSJeenu Viswambharan 379b7cb133eSJeenu Viswambharan return; 380b7cb133eSJeenu Viswambharan } 381b7cb133eSJeenu Viswambharan 382b7cb133eSJeenu Viswambharan /* 383b7cb133eSJeenu Viswambharan * We just received a shared event with routing set to ANY PE. The 384b7cb133eSJeenu Viswambharan * interrupt can't be delegated on this PE as SDEI events are masked. 385b7cb133eSJeenu Viswambharan * However, because its routing mode is ANY, it is possible that the 386b7cb133eSJeenu Viswambharan * event can be delegated on any other PE that hasn't masked events. 387b7cb133eSJeenu Viswambharan * Therefore, we set the interrupt back pending so as to give other 388b7cb133eSJeenu Viswambharan * suitable PEs a chance of handling it. 389b7cb133eSJeenu Viswambharan */ 390ba6e5ca6SJeenu Viswambharan assert(plat_ic_is_spi(map->intr) != 0); 391b7cb133eSJeenu Viswambharan plat_ic_set_interrupt_pending(map->intr); 392b7cb133eSJeenu Viswambharan 393b7cb133eSJeenu Viswambharan /* 394b7cb133eSJeenu Viswambharan * Leaving the same interrupt pending also means that the same interrupt 395b7cb133eSJeenu Viswambharan * can target this PE again as soon as this PE leaves EL3. Whether and 396b7cb133eSJeenu Viswambharan * how often that happens depends on the implementation of GIC. 397b7cb133eSJeenu Viswambharan * 398b7cb133eSJeenu Viswambharan * We therefore call a platform handler to resolve this situation. 399b7cb133eSJeenu Viswambharan */ 400b7cb133eSJeenu Viswambharan plat_sdei_handle_masked_trigger(my_mpidr, map->intr); 401b7cb133eSJeenu Viswambharan 402b7cb133eSJeenu Viswambharan /* This PE is masked. We EOI the interrupt, as it can't be delegated */ 403b7cb133eSJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 404b7cb133eSJeenu Viswambharan } 405b7cb133eSJeenu Viswambharan 406b7cb133eSJeenu Viswambharan /* SDEI main interrupt handler */ 407b7cb133eSJeenu Viswambharan int sdei_intr_handler(uint32_t intr_raw, uint32_t flags, void *handle, 408b7cb133eSJeenu Viswambharan void *cookie) 409b7cb133eSJeenu Viswambharan { 410b7cb133eSJeenu Viswambharan sdei_entry_t *se; 411b7cb133eSJeenu Viswambharan cpu_context_t *ctx; 412b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 413ba6e5ca6SJeenu Viswambharan const sdei_dispatch_context_t *disp_ctx; 414b7cb133eSJeenu Viswambharan unsigned int sec_state; 415b7cb133eSJeenu Viswambharan sdei_cpu_state_t *state; 416b7cb133eSJeenu Viswambharan uint32_t intr; 417e0566305SAntonio Nino Diaz jmp_buf dispatch_jmp; 418ba6e5ca6SJeenu Viswambharan const uint64_t mpidr = read_mpidr_el1(); 419b7cb133eSJeenu Viswambharan 420b7cb133eSJeenu Viswambharan /* 421b7cb133eSJeenu Viswambharan * To handle an event, the following conditions must be true: 422b7cb133eSJeenu Viswambharan * 423b7cb133eSJeenu Viswambharan * 1. Event must be signalled 424b7cb133eSJeenu Viswambharan * 2. Event must be enabled 425b7cb133eSJeenu Viswambharan * 3. This PE must be a target PE for the event 426b7cb133eSJeenu Viswambharan * 4. PE must be unmasked for SDEI 427b7cb133eSJeenu Viswambharan * 5. If this is a normal event, no event must be running 428b7cb133eSJeenu Viswambharan * 6. If this is a critical event, no critical event must be running 429b7cb133eSJeenu Viswambharan * 430b7cb133eSJeenu Viswambharan * (1) and (2) are true when this function is running 431b7cb133eSJeenu Viswambharan * (3) is enforced in GIC by selecting the appropriate routing option 432b7cb133eSJeenu Viswambharan * (4) is satisfied by client calling PE_UNMASK 433b7cb133eSJeenu Viswambharan * (5) and (6) is enforced using interrupt priority, the RPR, in GIC: 434b7cb133eSJeenu Viswambharan * - Normal SDEI events belong to Normal SDE priority class 435b7cb133eSJeenu Viswambharan * - Critical SDEI events belong to Critical CSDE priority class 436b7cb133eSJeenu Viswambharan * 437b7cb133eSJeenu Viswambharan * The interrupt has already been acknowledged, and therefore is active, 438b7cb133eSJeenu Viswambharan * so no other PE can handle this event while we are at it. 439b7cb133eSJeenu Viswambharan * 440b7cb133eSJeenu Viswambharan * Find if this is an SDEI interrupt. There must be an event mapped to 441b7cb133eSJeenu Viswambharan * this interrupt 442b7cb133eSJeenu Viswambharan */ 443b7cb133eSJeenu Viswambharan intr = plat_ic_get_interrupt_id(intr_raw); 444ba6e5ca6SJeenu Viswambharan map = find_event_map_by_intr(intr, (plat_ic_is_spi(intr) != 0)); 445ba6e5ca6SJeenu Viswambharan if (map == NULL) { 446b7cb133eSJeenu Viswambharan ERROR("No SDEI map for interrupt %u\n", intr); 447b7cb133eSJeenu Viswambharan panic(); 448b7cb133eSJeenu Viswambharan } 449b7cb133eSJeenu Viswambharan 450b7cb133eSJeenu Viswambharan /* 451b7cb133eSJeenu Viswambharan * Received interrupt number must either correspond to event 0, or must 452b7cb133eSJeenu Viswambharan * be bound interrupt. 453b7cb133eSJeenu Viswambharan */ 454b7cb133eSJeenu Viswambharan assert((map->ev_num == SDEI_EVENT_0) || is_map_bound(map)); 455b7cb133eSJeenu Viswambharan 456b7cb133eSJeenu Viswambharan se = get_event_entry(map); 457b7cb133eSJeenu Viswambharan state = sdei_get_this_pe_state(); 458b7cb133eSJeenu Viswambharan 459ba6e5ca6SJeenu Viswambharan if (state->pe_masked) { 460b7cb133eSJeenu Viswambharan /* 461b7cb133eSJeenu Viswambharan * Interrupts received while this PE was masked can't be 462b7cb133eSJeenu Viswambharan * dispatched. 463b7cb133eSJeenu Viswambharan */ 4644ce3e99aSScott Branden SDEI_LOG("interrupt %u on %" PRIx64 " while PE masked\n", 4654ce3e99aSScott Branden map->intr, mpidr); 466b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 467b7cb133eSJeenu Viswambharan sdei_map_lock(map); 468b7cb133eSJeenu Viswambharan 469b7cb133eSJeenu Viswambharan handle_masked_trigger(map, se, state, intr_raw); 470b7cb133eSJeenu Viswambharan 471b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 472b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 473b7cb133eSJeenu Viswambharan 474b7cb133eSJeenu Viswambharan return 0; 475b7cb133eSJeenu Viswambharan } 476b7cb133eSJeenu Viswambharan 477b7cb133eSJeenu Viswambharan /* Insert load barrier for signalled SDEI event */ 478b7cb133eSJeenu Viswambharan if (map->ev_num == SDEI_EVENT_0) 479b7cb133eSJeenu Viswambharan dmbld(); 480b7cb133eSJeenu Viswambharan 481b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 482b7cb133eSJeenu Viswambharan sdei_map_lock(map); 483b7cb133eSJeenu Viswambharan 484b7cb133eSJeenu Viswambharan /* Assert shared event routed to this PE had been configured so */ 485b7cb133eSJeenu Viswambharan if (is_event_shared(map) && (se->reg_flags == SDEI_REGF_RM_PE)) { 486ba6e5ca6SJeenu Viswambharan assert(se->affinity == (mpidr & MPIDR_AFFINITY_MASK)); 487b7cb133eSJeenu Viswambharan } 488b7cb133eSJeenu Viswambharan 489b7cb133eSJeenu Viswambharan if (!can_sdei_state_trans(se, DO_DISPATCH)) { 490b7cb133eSJeenu Viswambharan SDEI_LOG("SDEI event 0x%x can't be dispatched; state=0x%x\n", 491b7cb133eSJeenu Viswambharan map->ev_num, se->state); 492b7cb133eSJeenu Viswambharan 493b7cb133eSJeenu Viswambharan /* 494b7cb133eSJeenu Viswambharan * If the event is registered, leave the interrupt pending so 495b7cb133eSJeenu Viswambharan * that it's delivered when the event is enabled. 496b7cb133eSJeenu Viswambharan */ 497b7cb133eSJeenu Viswambharan if (GET_EV_STATE(se, REGISTERED)) 498b7cb133eSJeenu Viswambharan plat_ic_set_interrupt_pending(map->intr); 499b7cb133eSJeenu Viswambharan 500b7cb133eSJeenu Viswambharan /* 501b7cb133eSJeenu Viswambharan * The interrupt was disabled or unregistered after the handler 502b7cb133eSJeenu Viswambharan * started to execute, which means now the interrupt is already 503b7cb133eSJeenu Viswambharan * disabled and we just need to EOI the interrupt. 504b7cb133eSJeenu Viswambharan */ 505b7cb133eSJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 506b7cb133eSJeenu Viswambharan 507b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 508b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 509b7cb133eSJeenu Viswambharan 510b7cb133eSJeenu Viswambharan return 0; 511b7cb133eSJeenu Viswambharan } 512b7cb133eSJeenu Viswambharan 513b7cb133eSJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 514b7cb133eSJeenu Viswambharan if (is_event_critical(map)) { 515b7cb133eSJeenu Viswambharan /* 516b7cb133eSJeenu Viswambharan * If this event is Critical, and if there's an outstanding 517b7cb133eSJeenu Viswambharan * dispatch, assert the latter is a Normal dispatch. Critical 518b7cb133eSJeenu Viswambharan * events can preempt an outstanding Normal event dispatch. 519b7cb133eSJeenu Viswambharan */ 520ba6e5ca6SJeenu Viswambharan if (disp_ctx != NULL) 521b7cb133eSJeenu Viswambharan assert(is_event_normal(disp_ctx->map)); 522b7cb133eSJeenu Viswambharan } else { 523b7cb133eSJeenu Viswambharan /* 524b7cb133eSJeenu Viswambharan * If this event is Normal, assert that there are no outstanding 525b7cb133eSJeenu Viswambharan * dispatches. Normal events can't preempt any outstanding event 526b7cb133eSJeenu Viswambharan * dispatches. 527b7cb133eSJeenu Viswambharan */ 528b7cb133eSJeenu Viswambharan assert(disp_ctx == NULL); 529b7cb133eSJeenu Viswambharan } 530b7cb133eSJeenu Viswambharan 531b7cb133eSJeenu Viswambharan sec_state = get_interrupt_src_ss(flags); 532b7cb133eSJeenu Viswambharan 533b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 534b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 535b7cb133eSJeenu Viswambharan 5364ce3e99aSScott Branden SDEI_LOG("ACK %" PRIx64 ", ev:0x%x ss:%d spsr:%lx ELR:%lx\n", 5374ce3e99aSScott Branden mpidr, map->ev_num, sec_state, read_spsr_el3(), read_elr_el3()); 538b7cb133eSJeenu Viswambharan 539b7cb133eSJeenu Viswambharan ctx = handle; 540b7cb133eSJeenu Viswambharan 541b7cb133eSJeenu Viswambharan /* 542b7cb133eSJeenu Viswambharan * Check if we interrupted secure state. Perform a context switch so 543b7cb133eSJeenu Viswambharan * that we can delegate to NS. 544b7cb133eSJeenu Viswambharan */ 545b7cb133eSJeenu Viswambharan if (sec_state == SECURE) { 546b7cb133eSJeenu Viswambharan save_secure_context(); 547b7cb133eSJeenu Viswambharan ctx = restore_and_resume_ns_context(); 548b7cb133eSJeenu Viswambharan } 549b7cb133eSJeenu Viswambharan 550cdb6ac94SJeenu Viswambharan /* Synchronously dispatch event */ 551cdb6ac94SJeenu Viswambharan setup_ns_dispatch(map, se, ctx, &dispatch_jmp); 552cdb6ac94SJeenu Viswambharan begin_sdei_synchronous_dispatch(&dispatch_jmp); 553b7cb133eSJeenu Viswambharan 554b7cb133eSJeenu Viswambharan /* 555cdb6ac94SJeenu Viswambharan * We reach here when client completes the event. 556cdb6ac94SJeenu Viswambharan * 55790a9213bSJeenu Viswambharan * If the cause of dispatch originally interrupted the Secure world, 558cdb6ac94SJeenu Viswambharan * resume Secure. 559cdb6ac94SJeenu Viswambharan * 560cdb6ac94SJeenu Viswambharan * No need to save the Non-secure context ahead of a world switch: the 561cdb6ac94SJeenu Viswambharan * Non-secure context was fully saved before dispatch, and has been 562cdb6ac94SJeenu Viswambharan * returned to its pre-dispatch state. 563b7cb133eSJeenu Viswambharan */ 56490a9213bSJeenu Viswambharan if (sec_state == SECURE) 565cdb6ac94SJeenu Viswambharan restore_and_resume_secure_context(); 566cdb6ac94SJeenu Viswambharan 567cdb6ac94SJeenu Viswambharan /* 568cdb6ac94SJeenu Viswambharan * The event was dispatched after receiving SDEI interrupt. With 569cdb6ac94SJeenu Viswambharan * the event handling completed, EOI the corresponding 570cdb6ac94SJeenu Viswambharan * interrupt. 571cdb6ac94SJeenu Viswambharan */ 572297a9a0fSJeenu Viswambharan if ((map->ev_num != SDEI_EVENT_0) && !is_map_bound(map)) { 5736b94356bSVasyl Gomonovych ERROR("Invalid SDEI mapping: ev=0x%x\n", map->ev_num); 574cdb6ac94SJeenu Viswambharan panic(); 575cdb6ac94SJeenu Viswambharan } 576cdb6ac94SJeenu Viswambharan plat_ic_end_of_interrupt(intr_raw); 577cdb6ac94SJeenu Viswambharan 578b7cb133eSJeenu Viswambharan return 0; 579b7cb133eSJeenu Viswambharan } 580b7cb133eSJeenu Viswambharan 581cdb6ac94SJeenu Viswambharan /* 582cdb6ac94SJeenu Viswambharan * Explicitly dispatch the given SDEI event. 583cdb6ac94SJeenu Viswambharan * 584cdb6ac94SJeenu Viswambharan * When calling this API, the caller must be prepared for the SDEI dispatcher to 585cdb6ac94SJeenu Viswambharan * restore and make Non-secure context as active. This call returns only after 586cdb6ac94SJeenu Viswambharan * the client has completed the dispatch. Then, the Non-secure context will be 587cdb6ac94SJeenu Viswambharan * active, and the following ERET will return to Non-secure. 588cdb6ac94SJeenu Viswambharan * 589cdb6ac94SJeenu Viswambharan * Should the caller require re-entry to Secure, it must restore the Secure 590cdb6ac94SJeenu Viswambharan * context and program registers for ERET. 591cdb6ac94SJeenu Viswambharan */ 592cdb6ac94SJeenu Viswambharan int sdei_dispatch_event(int ev_num) 59355a1266eSJeenu Viswambharan { 59455a1266eSJeenu Viswambharan sdei_entry_t *se; 59555a1266eSJeenu Viswambharan sdei_ev_map_t *map; 596cdb6ac94SJeenu Viswambharan cpu_context_t *ns_ctx; 59755a1266eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 59855a1266eSJeenu Viswambharan sdei_cpu_state_t *state; 599e0566305SAntonio Nino Diaz jmp_buf dispatch_jmp; 60055a1266eSJeenu Viswambharan 60155a1266eSJeenu Viswambharan /* Can't dispatch if events are masked on this PE */ 60255a1266eSJeenu Viswambharan state = sdei_get_this_pe_state(); 603ba6e5ca6SJeenu Viswambharan if (state->pe_masked) 60455a1266eSJeenu Viswambharan return -1; 60555a1266eSJeenu Viswambharan 60655a1266eSJeenu Viswambharan /* Event 0 can't be dispatched */ 60755a1266eSJeenu Viswambharan if (ev_num == SDEI_EVENT_0) 60855a1266eSJeenu Viswambharan return -1; 60955a1266eSJeenu Viswambharan 61055a1266eSJeenu Viswambharan /* Locate mapping corresponding to this event */ 61155a1266eSJeenu Viswambharan map = find_event_map(ev_num); 612ba6e5ca6SJeenu Viswambharan if (map == NULL) 61355a1266eSJeenu Viswambharan return -1; 61455a1266eSJeenu Viswambharan 615af2c9ecdSJeenu Viswambharan /* Only explicit events can be dispatched */ 616af2c9ecdSJeenu Viswambharan if (!is_map_explicit(map)) 61755a1266eSJeenu Viswambharan return -1; 61855a1266eSJeenu Viswambharan 61955a1266eSJeenu Viswambharan /* Examine state of dispatch stack */ 62055a1266eSJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 621ba6e5ca6SJeenu Viswambharan if (disp_ctx != NULL) { 62255a1266eSJeenu Viswambharan /* 62355a1266eSJeenu Viswambharan * There's an outstanding dispatch. If the outstanding dispatch 62455a1266eSJeenu Viswambharan * is critical, no more dispatches are possible. 62555a1266eSJeenu Viswambharan */ 62655a1266eSJeenu Viswambharan if (is_event_critical(disp_ctx->map)) 62755a1266eSJeenu Viswambharan return -1; 62855a1266eSJeenu Viswambharan 62955a1266eSJeenu Viswambharan /* 63055a1266eSJeenu Viswambharan * If the outstanding dispatch is Normal, only critical events 63155a1266eSJeenu Viswambharan * can be dispatched. 63255a1266eSJeenu Viswambharan */ 63355a1266eSJeenu Viswambharan if (is_event_normal(map)) 63455a1266eSJeenu Viswambharan return -1; 63555a1266eSJeenu Viswambharan } 63655a1266eSJeenu Viswambharan 63755a1266eSJeenu Viswambharan se = get_event_entry(map); 63855a1266eSJeenu Viswambharan if (!can_sdei_state_trans(se, DO_DISPATCH)) 63955a1266eSJeenu Viswambharan return -1; 64055a1266eSJeenu Viswambharan 64155a1266eSJeenu Viswambharan /* 642cdb6ac94SJeenu Viswambharan * Prepare for NS dispatch by restoring the Non-secure context and 643cdb6ac94SJeenu Viswambharan * marking that as active. 64455a1266eSJeenu Viswambharan */ 645cdb6ac94SJeenu Viswambharan ns_ctx = restore_and_resume_ns_context(); 646cdb6ac94SJeenu Viswambharan 647d21f1ddbSMing Huang /* Activate the priority corresponding to the event being dispatched */ 648d21f1ddbSMing Huang ehf_activate_priority(sdei_event_priority(map)); 649d21f1ddbSMing Huang 650cdb6ac94SJeenu Viswambharan /* Dispatch event synchronously */ 651cdb6ac94SJeenu Viswambharan setup_ns_dispatch(map, se, ns_ctx, &dispatch_jmp); 652cdb6ac94SJeenu Viswambharan begin_sdei_synchronous_dispatch(&dispatch_jmp); 65355a1266eSJeenu Viswambharan 65455a1266eSJeenu Viswambharan /* 655cdb6ac94SJeenu Viswambharan * We reach here when client completes the event. 656cdb6ac94SJeenu Viswambharan * 657cdb6ac94SJeenu Viswambharan * Deactivate the priority level that was activated at the time of 658cdb6ac94SJeenu Viswambharan * explicit dispatch. 65955a1266eSJeenu Viswambharan */ 660cdb6ac94SJeenu Viswambharan ehf_deactivate_priority(sdei_event_priority(map)); 66155a1266eSJeenu Viswambharan 66255a1266eSJeenu Viswambharan return 0; 66355a1266eSJeenu Viswambharan } 66455a1266eSJeenu Viswambharan 665e0566305SAntonio Nino Diaz static void end_sdei_synchronous_dispatch(jmp_buf *buffer) 666cdb6ac94SJeenu Viswambharan { 667e0566305SAntonio Nino Diaz longjmp(*buffer, 1); 668cdb6ac94SJeenu Viswambharan } 669cdb6ac94SJeenu Viswambharan 670ba6e5ca6SJeenu Viswambharan int sdei_event_complete(bool resume, uint64_t pc) 671b7cb133eSJeenu Viswambharan { 672b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 673b7cb133eSJeenu Viswambharan sdei_entry_t *se; 674b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 675b7cb133eSJeenu Viswambharan cpu_context_t *ctx; 676b7cb133eSJeenu Viswambharan sdei_action_t act; 677b7cb133eSJeenu Viswambharan unsigned int client_el = sdei_client_el(); 678b7cb133eSJeenu Viswambharan 679b7cb133eSJeenu Viswambharan /* Return error if called without an active event */ 6808e3032f9SJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 681ba6e5ca6SJeenu Viswambharan if (disp_ctx == NULL) 682b7cb133eSJeenu Viswambharan return SDEI_EDENY; 683b7cb133eSJeenu Viswambharan 684b7cb133eSJeenu Viswambharan /* Validate resumption point */ 685b7cb133eSJeenu Viswambharan if (resume && (plat_sdei_validate_entry_point(pc, client_el) != 0)) 686b7cb133eSJeenu Viswambharan return SDEI_EDENY; 687b7cb133eSJeenu Viswambharan 688b7cb133eSJeenu Viswambharan map = disp_ctx->map; 689ba6e5ca6SJeenu Viswambharan assert(map != NULL); 690b7cb133eSJeenu Viswambharan se = get_event_entry(map); 691b7cb133eSJeenu Viswambharan 692611eb9cfSJeenu Viswambharan if (is_event_shared(map)) 693611eb9cfSJeenu Viswambharan sdei_map_lock(map); 694611eb9cfSJeenu Viswambharan 695b7cb133eSJeenu Viswambharan act = resume ? DO_COMPLETE_RESUME : DO_COMPLETE; 696b7cb133eSJeenu Viswambharan if (!can_sdei_state_trans(se, act)) { 697b7cb133eSJeenu Viswambharan if (is_event_shared(map)) 698b7cb133eSJeenu Viswambharan sdei_map_unlock(map); 699b7cb133eSJeenu Viswambharan return SDEI_EDENY; 700b7cb133eSJeenu Viswambharan } 701b7cb133eSJeenu Viswambharan 702611eb9cfSJeenu Viswambharan if (is_event_shared(map)) 703611eb9cfSJeenu Viswambharan sdei_map_unlock(map); 704611eb9cfSJeenu Viswambharan 7058e3032f9SJeenu Viswambharan /* Having done sanity checks, pop dispatch */ 706ba6e5ca6SJeenu Viswambharan (void) pop_dispatch(); 7078e3032f9SJeenu Viswambharan 7084ce3e99aSScott Branden SDEI_LOG("EOI:%lx, %d spsr:%lx elr:%lx\n", read_mpidr_el1(), 7098e3032f9SJeenu Viswambharan map->ev_num, read_spsr_el3(), read_elr_el3()); 7108e3032f9SJeenu Viswambharan 711b7cb133eSJeenu Viswambharan /* 712b7cb133eSJeenu Viswambharan * Restore Non-secure to how it was originally interrupted. Once done, 713b7cb133eSJeenu Viswambharan * it's up-to-date with the saved copy. 714b7cb133eSJeenu Viswambharan */ 715b7cb133eSJeenu Viswambharan ctx = cm_get_context(NON_SECURE); 716b7cb133eSJeenu Viswambharan restore_event_ctx(disp_ctx, ctx); 717b7cb133eSJeenu Viswambharan 718b7cb133eSJeenu Viswambharan if (resume) { 719b7cb133eSJeenu Viswambharan /* 720b7cb133eSJeenu Viswambharan * Complete-and-resume call. Prepare the Non-secure context 721b7cb133eSJeenu Viswambharan * (currently active) for complete and resume. 722b7cb133eSJeenu Viswambharan */ 723b7cb133eSJeenu Viswambharan cm_set_elr_spsr_el3(NON_SECURE, pc, SPSR_64(client_el, 724b7cb133eSJeenu Viswambharan MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 725b7cb133eSJeenu Viswambharan 726b7cb133eSJeenu Viswambharan /* 727b7cb133eSJeenu Viswambharan * Make it look as if a synchronous exception were taken at the 728b7cb133eSJeenu Viswambharan * supplied Non-secure resumption point. Populate SPSR and 729b7cb133eSJeenu Viswambharan * ELR_ELx so that an ERET from there works as expected. 730b7cb133eSJeenu Viswambharan * 731b7cb133eSJeenu Viswambharan * The assumption is that the client, if necessary, would have 732b7cb133eSJeenu Viswambharan * saved any live content in these registers before making this 733b7cb133eSJeenu Viswambharan * call. 734b7cb133eSJeenu Viswambharan */ 735b7cb133eSJeenu Viswambharan if (client_el == MODE_EL2) { 736b7cb133eSJeenu Viswambharan write_elr_el2(disp_ctx->elr_el3); 737b7cb133eSJeenu Viswambharan write_spsr_el2(disp_ctx->spsr_el3); 738b7cb133eSJeenu Viswambharan } else { 739b7cb133eSJeenu Viswambharan /* EL1 */ 740b7cb133eSJeenu Viswambharan write_elr_el1(disp_ctx->elr_el3); 741b7cb133eSJeenu Viswambharan write_spsr_el1(disp_ctx->spsr_el3); 742b7cb133eSJeenu Viswambharan } 743b7cb133eSJeenu Viswambharan } 744b7cb133eSJeenu Viswambharan 745cdb6ac94SJeenu Viswambharan /* End the outstanding dispatch */ 7465e60c39aSJeenu Viswambharan end_sdei_synchronous_dispatch(disp_ctx->dispatch_jmp); 747b7cb133eSJeenu Viswambharan 748b7cb133eSJeenu Viswambharan return 0; 749b7cb133eSJeenu Viswambharan } 750b7cb133eSJeenu Viswambharan 751ba6e5ca6SJeenu Viswambharan int64_t sdei_event_context(void *handle, unsigned int param) 752b7cb133eSJeenu Viswambharan { 753b7cb133eSJeenu Viswambharan sdei_dispatch_context_t *disp_ctx; 754b7cb133eSJeenu Viswambharan 755b7cb133eSJeenu Viswambharan if (param >= SDEI_SAVED_GPREGS) 756b7cb133eSJeenu Viswambharan return SDEI_EINVAL; 757b7cb133eSJeenu Viswambharan 758b7cb133eSJeenu Viswambharan /* Get outstanding dispatch on this CPU */ 759b7cb133eSJeenu Viswambharan disp_ctx = get_outstanding_dispatch(); 760ba6e5ca6SJeenu Viswambharan if (disp_ctx == NULL) 761b7cb133eSJeenu Viswambharan return SDEI_EDENY; 762b7cb133eSJeenu Viswambharan 763ba6e5ca6SJeenu Viswambharan assert(disp_ctx->map != NULL); 764b7cb133eSJeenu Viswambharan 765b7cb133eSJeenu Viswambharan if (!can_sdei_state_trans(get_event_entry(disp_ctx->map), DO_CONTEXT)) 766b7cb133eSJeenu Viswambharan return SDEI_EDENY; 767b7cb133eSJeenu Viswambharan 768b7cb133eSJeenu Viswambharan /* 769b7cb133eSJeenu Viswambharan * No locking is required for the Running status as this is the only CPU 770b7cb133eSJeenu Viswambharan * which can complete the event 771b7cb133eSJeenu Viswambharan */ 772b7cb133eSJeenu Viswambharan 773ba6e5ca6SJeenu Viswambharan return (int64_t) disp_ctx->x[param]; 774b7cb133eSJeenu Viswambharan } 775