121b818c0SJeenu Viswambharan /* 2*1f6bb41dSMadhukar Pappireddy * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved. 321b818c0SJeenu Viswambharan * 421b818c0SJeenu Viswambharan * SPDX-License-Identifier: BSD-3-Clause 521b818c0SJeenu Viswambharan */ 621b818c0SJeenu Viswambharan 721b818c0SJeenu Viswambharan /* 821b818c0SJeenu Viswambharan * Exception handlers at EL3, their priority levels, and management. 921b818c0SJeenu Viswambharan */ 1021b818c0SJeenu Viswambharan 1121b818c0SJeenu Viswambharan #include <assert.h> 1203b645edSJeenu Viswambharan #include <stdbool.h> 1321b818c0SJeenu Viswambharan 1409d40e0eSAntonio Nino Diaz #include <bl31/ehf.h> 1509d40e0eSAntonio Nino Diaz #include <bl31/interrupt_mgmt.h> 1609d40e0eSAntonio Nino Diaz #include <context.h> 1709d40e0eSAntonio Nino Diaz #include <common/debug.h> 1809d40e0eSAntonio Nino Diaz #include <drivers/arm/gic_common.h> 1909d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 2009d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 2109d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub_events.h> 2209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2309d40e0eSAntonio Nino Diaz 2421b818c0SJeenu Viswambharan /* Output EHF logs as verbose */ 2521b818c0SJeenu Viswambharan #define EHF_LOG(...) VERBOSE("EHF: " __VA_ARGS__) 2621b818c0SJeenu Viswambharan 2721b818c0SJeenu Viswambharan #define EHF_INVALID_IDX (-1) 2821b818c0SJeenu Viswambharan 2921b818c0SJeenu Viswambharan /* For a valid handler, return the actual function pointer; otherwise, 0. */ 3021b818c0SJeenu Viswambharan #define RAW_HANDLER(h) \ 3103b645edSJeenu Viswambharan ((ehf_handler_t) ((((h) & EHF_PRI_VALID_) != 0U) ? \ 3203b645edSJeenu Viswambharan ((h) & ~EHF_PRI_VALID_) : 0U)) 3321b818c0SJeenu Viswambharan 3403b645edSJeenu Viswambharan #define PRI_BIT(idx) (((ehf_pri_bits_t) 1u) << (idx)) 3521b818c0SJeenu Viswambharan 3621b818c0SJeenu Viswambharan /* 3721b818c0SJeenu Viswambharan * Convert index into secure priority using the platform-defined priority bits 3821b818c0SJeenu Viswambharan * field. 3921b818c0SJeenu Viswambharan */ 4021b818c0SJeenu Viswambharan #define IDX_TO_PRI(idx) \ 4103b645edSJeenu Viswambharan ((((unsigned) idx) << (7u - exception_data.pri_bits)) & 0x7fU) 4221b818c0SJeenu Viswambharan 4321b818c0SJeenu Viswambharan /* Check whether a given index is valid */ 4421b818c0SJeenu Viswambharan #define IS_IDX_VALID(idx) \ 4503b645edSJeenu Viswambharan ((exception_data.ehf_priorities[idx].ehf_handler & EHF_PRI_VALID_) != 0U) 4621b818c0SJeenu Viswambharan 4721b818c0SJeenu Viswambharan /* Returns whether given priority is in secure priority range */ 4803b645edSJeenu Viswambharan #define IS_PRI_SECURE(pri) (((pri) & 0x80U) == 0U) 4921b818c0SJeenu Viswambharan 5021b818c0SJeenu Viswambharan /* To be defined by the platform */ 5121b818c0SJeenu Viswambharan extern const ehf_priorities_t exception_data; 5221b818c0SJeenu Viswambharan 5321b818c0SJeenu Viswambharan /* Translate priority to the index in the priority array */ 5403b645edSJeenu Viswambharan static unsigned int pri_to_idx(unsigned int priority) 5521b818c0SJeenu Viswambharan { 5603b645edSJeenu Viswambharan unsigned int idx; 5721b818c0SJeenu Viswambharan 5821b818c0SJeenu Viswambharan idx = EHF_PRI_TO_IDX(priority, exception_data.pri_bits); 5903b645edSJeenu Viswambharan assert(idx < exception_data.num_priorities); 6021b818c0SJeenu Viswambharan assert(IS_IDX_VALID(idx)); 6121b818c0SJeenu Viswambharan 6221b818c0SJeenu Viswambharan return idx; 6321b818c0SJeenu Viswambharan } 6421b818c0SJeenu Viswambharan 6521b818c0SJeenu Viswambharan /* Return whether there are outstanding priority activation */ 6603b645edSJeenu Viswambharan static bool has_valid_pri_activations(pe_exc_data_t *pe_data) 6721b818c0SJeenu Viswambharan { 6803b645edSJeenu Viswambharan return pe_data->active_pri_bits != 0U; 6921b818c0SJeenu Viswambharan } 7021b818c0SJeenu Viswambharan 7121b818c0SJeenu Viswambharan static pe_exc_data_t *this_cpu_data(void) 7221b818c0SJeenu Viswambharan { 7321b818c0SJeenu Viswambharan return &get_cpu_data(ehf_data); 7421b818c0SJeenu Viswambharan } 7521b818c0SJeenu Viswambharan 7621b818c0SJeenu Viswambharan /* 7721b818c0SJeenu Viswambharan * Return the current priority index of this CPU. If no priority is active, 7821b818c0SJeenu Viswambharan * return EHF_INVALID_IDX. 7921b818c0SJeenu Viswambharan */ 8021b818c0SJeenu Viswambharan static int get_pe_highest_active_idx(pe_exc_data_t *pe_data) 8121b818c0SJeenu Viswambharan { 8221b818c0SJeenu Viswambharan if (!has_valid_pri_activations(pe_data)) 8321b818c0SJeenu Viswambharan return EHF_INVALID_IDX; 8421b818c0SJeenu Viswambharan 8521b818c0SJeenu Viswambharan /* Current priority is the right-most bit */ 8603b645edSJeenu Viswambharan return (int) __builtin_ctz(pe_data->active_pri_bits); 8721b818c0SJeenu Viswambharan } 8821b818c0SJeenu Viswambharan 8921b818c0SJeenu Viswambharan /* 9021b818c0SJeenu Viswambharan * Mark priority active by setting the corresponding bit in active_pri_bits and 9121b818c0SJeenu Viswambharan * programming the priority mask. 9221b818c0SJeenu Viswambharan * 9321b818c0SJeenu Viswambharan * This API is to be used as part of delegating to lower ELs other than for 9421b818c0SJeenu Viswambharan * interrupts; e.g. while handling synchronous exceptions. 9521b818c0SJeenu Viswambharan * 9621b818c0SJeenu Viswambharan * This API is expected to be invoked before restoring context (Secure or 9721b818c0SJeenu Viswambharan * Non-secure) in preparation for the respective dispatch. 9821b818c0SJeenu Viswambharan */ 9921b818c0SJeenu Viswambharan void ehf_activate_priority(unsigned int priority) 10021b818c0SJeenu Viswambharan { 10103b645edSJeenu Viswambharan int cur_pri_idx; 10203b645edSJeenu Viswambharan unsigned int old_mask, run_pri, idx; 10321b818c0SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data(); 10421b818c0SJeenu Viswambharan 10521b818c0SJeenu Viswambharan /* 10621b818c0SJeenu Viswambharan * Query interrupt controller for the running priority, or idle priority 10721b818c0SJeenu Viswambharan * if no interrupts are being handled. The requested priority must be 10821b818c0SJeenu Viswambharan * less (higher priority) than the active running priority. 10921b818c0SJeenu Viswambharan */ 11021b818c0SJeenu Viswambharan run_pri = plat_ic_get_running_priority(); 11121b818c0SJeenu Viswambharan if (priority >= run_pri) { 11221b818c0SJeenu Viswambharan ERROR("Running priority higher (0x%x) than requested (0x%x)\n", 11321b818c0SJeenu Viswambharan run_pri, priority); 11421b818c0SJeenu Viswambharan panic(); 11521b818c0SJeenu Viswambharan } 11621b818c0SJeenu Viswambharan 11721b818c0SJeenu Viswambharan /* 11821b818c0SJeenu Viswambharan * If there were priority activations already, the requested priority 11921b818c0SJeenu Viswambharan * must be less (higher priority) than the current highest priority 12021b818c0SJeenu Viswambharan * activation so far. 12121b818c0SJeenu Viswambharan */ 12221b818c0SJeenu Viswambharan cur_pri_idx = get_pe_highest_active_idx(pe_data); 12321b818c0SJeenu Viswambharan idx = pri_to_idx(priority); 12403b645edSJeenu Viswambharan if ((cur_pri_idx != EHF_INVALID_IDX) && 12503b645edSJeenu Viswambharan (idx >= ((unsigned int) cur_pri_idx))) { 12621b818c0SJeenu Viswambharan ERROR("Activation priority mismatch: req=0x%x current=0x%x\n", 12721b818c0SJeenu Viswambharan priority, IDX_TO_PRI(cur_pri_idx)); 12821b818c0SJeenu Viswambharan panic(); 12921b818c0SJeenu Viswambharan } 13021b818c0SJeenu Viswambharan 13121b818c0SJeenu Viswambharan /* Set the bit corresponding to the requested priority */ 13221b818c0SJeenu Viswambharan pe_data->active_pri_bits |= PRI_BIT(idx); 13321b818c0SJeenu Viswambharan 13421b818c0SJeenu Viswambharan /* 13521b818c0SJeenu Viswambharan * Program priority mask for the activated level. Check that the new 13621b818c0SJeenu Viswambharan * priority mask is setting a higher priority level than the existing 13721b818c0SJeenu Viswambharan * mask. 13821b818c0SJeenu Viswambharan */ 13921b818c0SJeenu Viswambharan old_mask = plat_ic_set_priority_mask(priority); 14021b818c0SJeenu Viswambharan if (priority >= old_mask) { 14121b818c0SJeenu Viswambharan ERROR("Requested priority (0x%x) lower than Priority Mask (0x%x)\n", 14221b818c0SJeenu Viswambharan priority, old_mask); 14321b818c0SJeenu Viswambharan panic(); 14421b818c0SJeenu Viswambharan } 14521b818c0SJeenu Viswambharan 14621b818c0SJeenu Viswambharan /* 14721b818c0SJeenu Viswambharan * If this is the first activation, save the priority mask. This will be 14821b818c0SJeenu Viswambharan * restored after the last deactivation. 14921b818c0SJeenu Viswambharan */ 15021b818c0SJeenu Viswambharan if (cur_pri_idx == EHF_INVALID_IDX) 15103b645edSJeenu Viswambharan pe_data->init_pri_mask = (uint8_t) old_mask; 15221b818c0SJeenu Viswambharan 15321b818c0SJeenu Viswambharan EHF_LOG("activate prio=%d\n", get_pe_highest_active_idx(pe_data)); 15421b818c0SJeenu Viswambharan } 15521b818c0SJeenu Viswambharan 15621b818c0SJeenu Viswambharan /* 15721b818c0SJeenu Viswambharan * Mark priority inactive by clearing the corresponding bit in active_pri_bits, 15821b818c0SJeenu Viswambharan * and programming the priority mask. 15921b818c0SJeenu Viswambharan * 16021b818c0SJeenu Viswambharan * This API is expected to be used as part of delegating to to lower ELs other 16121b818c0SJeenu Viswambharan * than for interrupts; e.g. while handling synchronous exceptions. 16221b818c0SJeenu Viswambharan * 16321b818c0SJeenu Viswambharan * This API is expected to be invoked after saving context (Secure or 16421b818c0SJeenu Viswambharan * Non-secure), having concluded the respective dispatch. 16521b818c0SJeenu Viswambharan */ 16621b818c0SJeenu Viswambharan void ehf_deactivate_priority(unsigned int priority) 16721b818c0SJeenu Viswambharan { 16803b645edSJeenu Viswambharan int cur_pri_idx; 16921b818c0SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data(); 17003b645edSJeenu Viswambharan unsigned int old_mask, run_pri, idx; 17121b818c0SJeenu Viswambharan 17221b818c0SJeenu Viswambharan /* 17321b818c0SJeenu Viswambharan * Query interrupt controller for the running priority, or idle priority 17421b818c0SJeenu Viswambharan * if no interrupts are being handled. The requested priority must be 17521b818c0SJeenu Viswambharan * less (higher priority) than the active running priority. 17621b818c0SJeenu Viswambharan */ 17721b818c0SJeenu Viswambharan run_pri = plat_ic_get_running_priority(); 17821b818c0SJeenu Viswambharan if (priority >= run_pri) { 17921b818c0SJeenu Viswambharan ERROR("Running priority higher (0x%x) than requested (0x%x)\n", 18021b818c0SJeenu Viswambharan run_pri, priority); 18121b818c0SJeenu Viswambharan panic(); 18221b818c0SJeenu Viswambharan } 18321b818c0SJeenu Viswambharan 18421b818c0SJeenu Viswambharan /* 18521b818c0SJeenu Viswambharan * Deactivation is allowed only when there are priority activations, and 18621b818c0SJeenu Viswambharan * the deactivation priority level must match the current activated 18721b818c0SJeenu Viswambharan * priority. 18821b818c0SJeenu Viswambharan */ 18921b818c0SJeenu Viswambharan cur_pri_idx = get_pe_highest_active_idx(pe_data); 19021b818c0SJeenu Viswambharan idx = pri_to_idx(priority); 19103b645edSJeenu Viswambharan if ((cur_pri_idx == EHF_INVALID_IDX) || 19203b645edSJeenu Viswambharan (idx != ((unsigned int) cur_pri_idx))) { 19321b818c0SJeenu Viswambharan ERROR("Deactivation priority mismatch: req=0x%x current=0x%x\n", 19421b818c0SJeenu Viswambharan priority, IDX_TO_PRI(cur_pri_idx)); 19521b818c0SJeenu Viswambharan panic(); 19621b818c0SJeenu Viswambharan } 19721b818c0SJeenu Viswambharan 19821b818c0SJeenu Viswambharan /* Clear bit corresponding to highest priority */ 19903b645edSJeenu Viswambharan pe_data->active_pri_bits &= (pe_data->active_pri_bits - 1u); 20021b818c0SJeenu Viswambharan 20121b818c0SJeenu Viswambharan /* 20221b818c0SJeenu Viswambharan * Restore priority mask corresponding to the next priority, or the 20321b818c0SJeenu Viswambharan * one stashed earlier if there are no more to deactivate. 20421b818c0SJeenu Viswambharan */ 20503b645edSJeenu Viswambharan cur_pri_idx = get_pe_highest_active_idx(pe_data); 20603b645edSJeenu Viswambharan if (cur_pri_idx == EHF_INVALID_IDX) 20721b818c0SJeenu Viswambharan old_mask = plat_ic_set_priority_mask(pe_data->init_pri_mask); 20821b818c0SJeenu Viswambharan else 20921b818c0SJeenu Viswambharan old_mask = plat_ic_set_priority_mask(priority); 21021b818c0SJeenu Viswambharan 211db1631efSJeenu Viswambharan if (old_mask > priority) { 21221b818c0SJeenu Viswambharan ERROR("Deactivation priority (0x%x) lower than Priority Mask (0x%x)\n", 21321b818c0SJeenu Viswambharan priority, old_mask); 21421b818c0SJeenu Viswambharan panic(); 21521b818c0SJeenu Viswambharan } 21621b818c0SJeenu Viswambharan 21721b818c0SJeenu Viswambharan EHF_LOG("deactivate prio=%d\n", get_pe_highest_active_idx(pe_data)); 21821b818c0SJeenu Viswambharan } 21921b818c0SJeenu Viswambharan 22021b818c0SJeenu Viswambharan /* 2213d732e23SJeenu Viswambharan * After leaving Non-secure world, stash current Non-secure Priority Mask, and 2223d732e23SJeenu Viswambharan * set Priority Mask to the highest Non-secure priority so that Non-secure 2233d732e23SJeenu Viswambharan * interrupts cannot preempt Secure execution. 2243d732e23SJeenu Viswambharan * 2253d732e23SJeenu Viswambharan * If the current running priority is in the secure range, or if there are 2263d732e23SJeenu Viswambharan * outstanding priority activations, this function does nothing. 2273d732e23SJeenu Viswambharan * 2283d732e23SJeenu Viswambharan * This function subscribes to the 'cm_exited_normal_world' event published by 2293d732e23SJeenu Viswambharan * the Context Management Library. 2303d732e23SJeenu Viswambharan */ 2313d732e23SJeenu Viswambharan static void *ehf_exited_normal_world(const void *arg) 2323d732e23SJeenu Viswambharan { 2333d732e23SJeenu Viswambharan unsigned int run_pri; 2343d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data(); 2353d732e23SJeenu Viswambharan 2363d732e23SJeenu Viswambharan /* If the running priority is in the secure range, do nothing */ 2373d732e23SJeenu Viswambharan run_pri = plat_ic_get_running_priority(); 2383d732e23SJeenu Viswambharan if (IS_PRI_SECURE(run_pri)) 23903b645edSJeenu Viswambharan return NULL; 2403d732e23SJeenu Viswambharan 2413d732e23SJeenu Viswambharan /* Do nothing if there are explicit activations */ 2423d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data)) 24303b645edSJeenu Viswambharan return NULL; 2443d732e23SJeenu Viswambharan 24503b645edSJeenu Viswambharan assert(pe_data->ns_pri_mask == 0u); 2463d732e23SJeenu Viswambharan 2473d732e23SJeenu Viswambharan pe_data->ns_pri_mask = 24803b645edSJeenu Viswambharan (uint8_t) plat_ic_set_priority_mask(GIC_HIGHEST_NS_PRIORITY); 2493d732e23SJeenu Viswambharan 2503d732e23SJeenu Viswambharan /* The previous Priority Mask is not expected to be in secure range */ 2513d732e23SJeenu Viswambharan if (IS_PRI_SECURE(pe_data->ns_pri_mask)) { 2523d732e23SJeenu Viswambharan ERROR("Priority Mask (0x%x) already in secure range\n", 2533d732e23SJeenu Viswambharan pe_data->ns_pri_mask); 2543d732e23SJeenu Viswambharan panic(); 2553d732e23SJeenu Viswambharan } 2563d732e23SJeenu Viswambharan 2573d732e23SJeenu Viswambharan EHF_LOG("Priority Mask: 0x%x => 0x%x\n", pe_data->ns_pri_mask, 2583d732e23SJeenu Viswambharan GIC_HIGHEST_NS_PRIORITY); 2593d732e23SJeenu Viswambharan 26003b645edSJeenu Viswambharan return NULL; 2613d732e23SJeenu Viswambharan } 2623d732e23SJeenu Viswambharan 2633d732e23SJeenu Viswambharan /* 2643d732e23SJeenu Viswambharan * Conclude Secure execution and prepare for return to Non-secure world. Restore 2653d732e23SJeenu Viswambharan * the Non-secure Priority Mask previously stashed upon leaving Non-secure 2663d732e23SJeenu Viswambharan * world. 2673d732e23SJeenu Viswambharan * 2683d732e23SJeenu Viswambharan * If there the current running priority is in the secure range, or if there are 2693d732e23SJeenu Viswambharan * outstanding priority activations, this function does nothing. 2703d732e23SJeenu Viswambharan * 2713d732e23SJeenu Viswambharan * This function subscribes to the 'cm_entering_normal_world' event published by 2723d732e23SJeenu Viswambharan * the Context Management Library. 2733d732e23SJeenu Viswambharan */ 2743d732e23SJeenu Viswambharan static void *ehf_entering_normal_world(const void *arg) 2753d732e23SJeenu Viswambharan { 2763d732e23SJeenu Viswambharan unsigned int old_pmr, run_pri; 2773d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data(); 2783d732e23SJeenu Viswambharan 2793d732e23SJeenu Viswambharan /* If the running priority is in the secure range, do nothing */ 2803d732e23SJeenu Viswambharan run_pri = plat_ic_get_running_priority(); 2813d732e23SJeenu Viswambharan if (IS_PRI_SECURE(run_pri)) 28203b645edSJeenu Viswambharan return NULL; 2833d732e23SJeenu Viswambharan 2843d732e23SJeenu Viswambharan /* 2853d732e23SJeenu Viswambharan * If there are explicit activations, do nothing. The Priority Mask will 2863d732e23SJeenu Viswambharan * be restored upon the last deactivation. 2873d732e23SJeenu Viswambharan */ 2883d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data)) 28903b645edSJeenu Viswambharan return NULL; 2903d732e23SJeenu Viswambharan 2913d732e23SJeenu Viswambharan /* Do nothing if we don't have a valid Priority Mask to restore */ 29203b645edSJeenu Viswambharan if (pe_data->ns_pri_mask == 0U) 29303b645edSJeenu Viswambharan return NULL; 2943d732e23SJeenu Viswambharan 2953d732e23SJeenu Viswambharan old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask); 2963d732e23SJeenu Viswambharan 2973d732e23SJeenu Viswambharan /* 2983d732e23SJeenu Viswambharan * When exiting secure world, the current Priority Mask must be 2993d732e23SJeenu Viswambharan * GIC_HIGHEST_NS_PRIORITY (as set during entry), or the Non-secure 3003d732e23SJeenu Viswambharan * priority mask set upon calling ehf_allow_ns_preemption() 3013d732e23SJeenu Viswambharan */ 3023d732e23SJeenu Viswambharan if ((old_pmr != GIC_HIGHEST_NS_PRIORITY) && 3033d732e23SJeenu Viswambharan (old_pmr != pe_data->ns_pri_mask)) { 3043d732e23SJeenu Viswambharan ERROR("Invalid Priority Mask (0x%x) restored\n", old_pmr); 3053d732e23SJeenu Viswambharan panic(); 3063d732e23SJeenu Viswambharan } 3073d732e23SJeenu Viswambharan 3083d732e23SJeenu Viswambharan EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask); 3093d732e23SJeenu Viswambharan 3103d732e23SJeenu Viswambharan pe_data->ns_pri_mask = 0; 3113d732e23SJeenu Viswambharan 31203b645edSJeenu Viswambharan return NULL; 3133d732e23SJeenu Viswambharan } 3143d732e23SJeenu Viswambharan 3153d732e23SJeenu Viswambharan /* 3163d732e23SJeenu Viswambharan * Program Priority Mask to the original Non-secure priority such that 31773308618SAntonio Nino Diaz * Non-secure interrupts may preempt Secure execution (for example, during 31873308618SAntonio Nino Diaz * Yielding SMC calls). The 'preempt_ret_code' parameter indicates the Yielding 31973308618SAntonio Nino Diaz * SMC's return value in case the call was preempted. 3203d732e23SJeenu Viswambharan * 3213d732e23SJeenu Viswambharan * This API is expected to be invoked before delegating a yielding SMC to Secure 3223d732e23SJeenu Viswambharan * EL1. I.e. within the window of secure execution after Non-secure context is 3233d732e23SJeenu Viswambharan * saved (after entry into EL3) and Secure context is restored (before entering 3243d732e23SJeenu Viswambharan * Secure EL1). 3253d732e23SJeenu Viswambharan */ 326af34cd72SJeenu Viswambharan void ehf_allow_ns_preemption(uint64_t preempt_ret_code) 3273d732e23SJeenu Viswambharan { 328af34cd72SJeenu Viswambharan cpu_context_t *ns_ctx; 3293d732e23SJeenu Viswambharan unsigned int old_pmr __unused; 3303d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data(); 3313d732e23SJeenu Viswambharan 3323d732e23SJeenu Viswambharan /* 3333d732e23SJeenu Viswambharan * We should have been notified earlier of entering secure world, and 3343d732e23SJeenu Viswambharan * therefore have stashed the Non-secure priority mask. 3353d732e23SJeenu Viswambharan */ 33603b645edSJeenu Viswambharan assert(pe_data->ns_pri_mask != 0U); 3373d732e23SJeenu Viswambharan 3383d732e23SJeenu Viswambharan /* Make sure no priority levels are active when requesting this */ 3393d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data)) { 3403d732e23SJeenu Viswambharan ERROR("PE %lx has priority activations: 0x%x\n", 3413d732e23SJeenu Viswambharan read_mpidr_el1(), pe_data->active_pri_bits); 3423d732e23SJeenu Viswambharan panic(); 3433d732e23SJeenu Viswambharan } 3443d732e23SJeenu Viswambharan 345af34cd72SJeenu Viswambharan /* 346af34cd72SJeenu Viswambharan * Program preempted return code to x0 right away so that, if the 347af34cd72SJeenu Viswambharan * Yielding SMC was indeed preempted before a dispatcher gets a chance 348af34cd72SJeenu Viswambharan * to populate it, the caller would find the correct return value. 349af34cd72SJeenu Viswambharan */ 350af34cd72SJeenu Viswambharan ns_ctx = cm_get_context(NON_SECURE); 35103b645edSJeenu Viswambharan assert(ns_ctx != NULL); 352af34cd72SJeenu Viswambharan write_ctx_reg(get_gpregs_ctx(ns_ctx), CTX_GPREG_X0, preempt_ret_code); 353af34cd72SJeenu Viswambharan 3543d732e23SJeenu Viswambharan old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask); 3553d732e23SJeenu Viswambharan 3563d732e23SJeenu Viswambharan EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask); 3573d732e23SJeenu Viswambharan 3583d732e23SJeenu Viswambharan pe_data->ns_pri_mask = 0; 3593d732e23SJeenu Viswambharan } 3603d732e23SJeenu Viswambharan 3613d732e23SJeenu Viswambharan /* 3623d732e23SJeenu Viswambharan * Return whether Secure execution has explicitly allowed Non-secure interrupts 36373308618SAntonio Nino Diaz * to preempt itself (for example, during Yielding SMC calls). 3643d732e23SJeenu Viswambharan */ 3653d732e23SJeenu Viswambharan unsigned int ehf_is_ns_preemption_allowed(void) 3663d732e23SJeenu Viswambharan { 3673d732e23SJeenu Viswambharan unsigned int run_pri; 3683d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data(); 3693d732e23SJeenu Viswambharan 3703d732e23SJeenu Viswambharan /* If running priority is in secure range, return false */ 3713d732e23SJeenu Viswambharan run_pri = plat_ic_get_running_priority(); 3723d732e23SJeenu Viswambharan if (IS_PRI_SECURE(run_pri)) 3733d732e23SJeenu Viswambharan return 0; 3743d732e23SJeenu Viswambharan 3753d732e23SJeenu Viswambharan /* 3763d732e23SJeenu Viswambharan * If Non-secure preemption was permitted by calling 3773d732e23SJeenu Viswambharan * ehf_allow_ns_preemption() earlier: 3783d732e23SJeenu Viswambharan * 3793d732e23SJeenu Viswambharan * - There wouldn't have been priority activations; 3803d732e23SJeenu Viswambharan * - We would have cleared the stashed the Non-secure Priority Mask. 3813d732e23SJeenu Viswambharan */ 3823d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data)) 3833d732e23SJeenu Viswambharan return 0; 38403b645edSJeenu Viswambharan if (pe_data->ns_pri_mask != 0U) 3853d732e23SJeenu Viswambharan return 0; 3863d732e23SJeenu Viswambharan 3873d732e23SJeenu Viswambharan return 1; 3883d732e23SJeenu Viswambharan } 3893d732e23SJeenu Viswambharan 3903d732e23SJeenu Viswambharan /* 39121b818c0SJeenu Viswambharan * Top-level EL3 interrupt handler. 39221b818c0SJeenu Viswambharan */ 39321b818c0SJeenu Viswambharan static uint64_t ehf_el3_interrupt_handler(uint32_t id, uint32_t flags, 39421b818c0SJeenu Viswambharan void *handle, void *cookie) 39521b818c0SJeenu Viswambharan { 39603b645edSJeenu Viswambharan int ret = 0; 39703b645edSJeenu Viswambharan uint32_t intr_raw; 39803b645edSJeenu Viswambharan unsigned int intr, pri, idx; 39921b818c0SJeenu Viswambharan ehf_handler_t handler; 40021b818c0SJeenu Viswambharan 40121b818c0SJeenu Viswambharan /* 40221b818c0SJeenu Viswambharan * Top-level interrupt type handler from Interrupt Management Framework 40321b818c0SJeenu Viswambharan * doesn't acknowledge the interrupt; so the interrupt ID must be 40421b818c0SJeenu Viswambharan * invalid. 40521b818c0SJeenu Viswambharan */ 40621b818c0SJeenu Viswambharan assert(id == INTR_ID_UNAVAILABLE); 40721b818c0SJeenu Viswambharan 40821b818c0SJeenu Viswambharan /* 40921b818c0SJeenu Viswambharan * Acknowledge interrupt. Proceed with handling only for valid interrupt 41021b818c0SJeenu Viswambharan * IDs. This situation may arise because of Interrupt Management 41121b818c0SJeenu Viswambharan * Framework identifying an EL3 interrupt, but before it's been 41221b818c0SJeenu Viswambharan * acknowledged here, the interrupt was either deasserted, or there was 41321b818c0SJeenu Viswambharan * a higher-priority interrupt of another type. 41421b818c0SJeenu Viswambharan */ 41521b818c0SJeenu Viswambharan intr_raw = plat_ic_acknowledge_interrupt(); 41621b818c0SJeenu Viswambharan intr = plat_ic_get_interrupt_id(intr_raw); 41721b818c0SJeenu Viswambharan if (intr == INTR_ID_UNAVAILABLE) 41821b818c0SJeenu Viswambharan return 0; 41921b818c0SJeenu Viswambharan 42021b818c0SJeenu Viswambharan /* Having acknowledged the interrupt, get the running priority */ 42121b818c0SJeenu Viswambharan pri = plat_ic_get_running_priority(); 42221b818c0SJeenu Viswambharan 42321b818c0SJeenu Viswambharan /* Check EL3 interrupt priority is in secure range */ 42421b818c0SJeenu Viswambharan assert(IS_PRI_SECURE(pri)); 42521b818c0SJeenu Viswambharan 42621b818c0SJeenu Viswambharan /* 42721b818c0SJeenu Viswambharan * Translate the priority to a descriptor index. We do this by masking 42821b818c0SJeenu Viswambharan * and shifting the running priority value (platform-supplied). 42921b818c0SJeenu Viswambharan */ 43021b818c0SJeenu Viswambharan idx = pri_to_idx(pri); 43121b818c0SJeenu Viswambharan 43221b818c0SJeenu Viswambharan /* Validate priority */ 43321b818c0SJeenu Viswambharan assert(pri == IDX_TO_PRI(idx)); 43421b818c0SJeenu Viswambharan 43503b645edSJeenu Viswambharan handler = (ehf_handler_t) RAW_HANDLER( 43603b645edSJeenu Viswambharan exception_data.ehf_priorities[idx].ehf_handler); 43703b645edSJeenu Viswambharan if (handler == NULL) { 43821b818c0SJeenu Viswambharan ERROR("No EL3 exception handler for priority 0x%x\n", 43921b818c0SJeenu Viswambharan IDX_TO_PRI(idx)); 44021b818c0SJeenu Viswambharan panic(); 44121b818c0SJeenu Viswambharan } 44221b818c0SJeenu Viswambharan 44321b818c0SJeenu Viswambharan /* 44421b818c0SJeenu Viswambharan * Call registered handler. Pass the raw interrupt value to registered 44521b818c0SJeenu Viswambharan * handlers. 44621b818c0SJeenu Viswambharan */ 44721b818c0SJeenu Viswambharan ret = handler(intr_raw, flags, handle, cookie); 44821b818c0SJeenu Viswambharan 44903b645edSJeenu Viswambharan return (uint64_t) ret; 45021b818c0SJeenu Viswambharan } 45121b818c0SJeenu Viswambharan 45221b818c0SJeenu Viswambharan /* 45321b818c0SJeenu Viswambharan * Initialize the EL3 exception handling. 45421b818c0SJeenu Viswambharan */ 45587c85134SDaniel Boulby void __init ehf_init(void) 45621b818c0SJeenu Viswambharan { 45721b818c0SJeenu Viswambharan unsigned int flags = 0; 45821b818c0SJeenu Viswambharan int ret __unused; 45921b818c0SJeenu Viswambharan 46021b818c0SJeenu Viswambharan /* Ensure EL3 interrupts are supported */ 461*1f6bb41dSMadhukar Pappireddy assert(plat_ic_has_interrupt_type(INTR_TYPE_EL3)); 46221b818c0SJeenu Viswambharan 46321b818c0SJeenu Viswambharan /* 46421b818c0SJeenu Viswambharan * Make sure that priority water mark has enough bits to represent the 46521b818c0SJeenu Viswambharan * whole priority array. 46621b818c0SJeenu Viswambharan */ 46703b645edSJeenu Viswambharan assert(exception_data.num_priorities <= (sizeof(ehf_pri_bits_t) * 8U)); 46821b818c0SJeenu Viswambharan 46903b645edSJeenu Viswambharan assert(exception_data.ehf_priorities != NULL); 47021b818c0SJeenu Viswambharan 47121b818c0SJeenu Viswambharan /* 47221b818c0SJeenu Viswambharan * Bit 7 of GIC priority must be 0 for secure interrupts. This means 47321b818c0SJeenu Viswambharan * platforms must use at least 1 of the remaining 7 bits. 47421b818c0SJeenu Viswambharan */ 47503b645edSJeenu Viswambharan assert((exception_data.pri_bits >= 1U) || 47603b645edSJeenu Viswambharan (exception_data.pri_bits < 8U)); 47721b818c0SJeenu Viswambharan 4787c2fe62fSRaghu Krishnamurthy /* Route EL3 interrupts when in Non-secure. */ 47921b818c0SJeenu Viswambharan set_interrupt_rm_flag(flags, NON_SECURE); 4807c2fe62fSRaghu Krishnamurthy 4817c2fe62fSRaghu Krishnamurthy /* 4827c2fe62fSRaghu Krishnamurthy * Route EL3 interrupts when in secure, only when SPMC is not present 4837c2fe62fSRaghu Krishnamurthy * in S-EL2. 4847c2fe62fSRaghu Krishnamurthy */ 4857c2fe62fSRaghu Krishnamurthy #if !(defined(SPD_spmd) && (SPMD_SPM_AT_SEL2 == 1)) 48621b818c0SJeenu Viswambharan set_interrupt_rm_flag(flags, SECURE); 4877c2fe62fSRaghu Krishnamurthy #endif /* !(defined(SPD_spmd) && (SPMD_SPM_AT_SEL2 == 1)) */ 48821b818c0SJeenu Viswambharan 48921b818c0SJeenu Viswambharan /* Register handler for EL3 interrupts */ 49021b818c0SJeenu Viswambharan ret = register_interrupt_type_handler(INTR_TYPE_EL3, 49121b818c0SJeenu Viswambharan ehf_el3_interrupt_handler, flags); 49221b818c0SJeenu Viswambharan assert(ret == 0); 49321b818c0SJeenu Viswambharan } 49421b818c0SJeenu Viswambharan 49521b818c0SJeenu Viswambharan /* 49621b818c0SJeenu Viswambharan * Register a handler at the supplied priority. Registration is allowed only if 49721b818c0SJeenu Viswambharan * a handler hasn't been registered before, or one wasn't provided at build 49821b818c0SJeenu Viswambharan * time. The priority for which the handler is being registered must also accord 49921b818c0SJeenu Viswambharan * with the platform-supplied data. 50021b818c0SJeenu Viswambharan */ 50121b818c0SJeenu Viswambharan void ehf_register_priority_handler(unsigned int pri, ehf_handler_t handler) 50221b818c0SJeenu Viswambharan { 50303b645edSJeenu Viswambharan unsigned int idx; 50421b818c0SJeenu Viswambharan 50521b818c0SJeenu Viswambharan /* Sanity check for handler */ 50621b818c0SJeenu Viswambharan assert(handler != NULL); 50721b818c0SJeenu Viswambharan 50821b818c0SJeenu Viswambharan /* Handler ought to be 4-byte aligned */ 50903b645edSJeenu Viswambharan assert((((uintptr_t) handler) & 3U) == 0U); 51021b818c0SJeenu Viswambharan 51121b818c0SJeenu Viswambharan /* Ensure we register for valid priority */ 51221b818c0SJeenu Viswambharan idx = pri_to_idx(pri); 51321b818c0SJeenu Viswambharan assert(idx < exception_data.num_priorities); 51421b818c0SJeenu Viswambharan assert(IDX_TO_PRI(idx) == pri); 51521b818c0SJeenu Viswambharan 51621b818c0SJeenu Viswambharan /* Return failure if a handler was already registered */ 51703b645edSJeenu Viswambharan if (exception_data.ehf_priorities[idx].ehf_handler != EHF_NO_HANDLER_) { 51821b818c0SJeenu Viswambharan ERROR("Handler already registered for priority 0x%x\n", pri); 51921b818c0SJeenu Viswambharan panic(); 52021b818c0SJeenu Viswambharan } 52121b818c0SJeenu Viswambharan 52221b818c0SJeenu Viswambharan /* 52321b818c0SJeenu Viswambharan * Install handler, and retain the valid bit. We assume that the handler 52421b818c0SJeenu Viswambharan * is 4-byte aligned, which is usually the case. 52521b818c0SJeenu Viswambharan */ 52621b818c0SJeenu Viswambharan exception_data.ehf_priorities[idx].ehf_handler = 52703b645edSJeenu Viswambharan (((uintptr_t) handler) | EHF_PRI_VALID_); 52821b818c0SJeenu Viswambharan 52921b818c0SJeenu Viswambharan EHF_LOG("register pri=0x%x handler=%p\n", pri, handler); 53021b818c0SJeenu Viswambharan } 5313d732e23SJeenu Viswambharan 5323d732e23SJeenu Viswambharan SUBSCRIBE_TO_EVENT(cm_entering_normal_world, ehf_entering_normal_world); 5333d732e23SJeenu Viswambharan SUBSCRIBE_TO_EVENT(cm_exited_normal_world, ehf_exited_normal_world); 534