121b818c0SJeenu Viswambharan /*
2*24a4a0a5SArvind Ram Prakash * Copyright (c) 2017-2024, 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 */
pri_to_idx(unsigned int priority)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 */
has_valid_pri_activations(pe_exc_data_t * pe_data)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
this_cpu_data(void)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 */
get_pe_highest_active_idx(pe_exc_data_t * pe_data)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 */
ehf_activate_priority(unsigned int priority)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 */
ehf_deactivate_priority(unsigned int priority)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);
206*24a4a0a5SArvind Ram Prakash
207*24a4a0a5SArvind Ram Prakash #if GIC600_ERRATA_WA_2384374
208*24a4a0a5SArvind Ram Prakash if (cur_pri_idx == EHF_INVALID_IDX) {
209*24a4a0a5SArvind Ram Prakash old_mask = plat_ic_deactivate_priority(pe_data->init_pri_mask);
210*24a4a0a5SArvind Ram Prakash } else {
211*24a4a0a5SArvind Ram Prakash old_mask = plat_ic_deactivate_priority(priority);
212*24a4a0a5SArvind Ram Prakash }
213*24a4a0a5SArvind Ram Prakash #else
214*24a4a0a5SArvind Ram Prakash if (cur_pri_idx == EHF_INVALID_IDX) {
21521b818c0SJeenu Viswambharan old_mask = plat_ic_set_priority_mask(pe_data->init_pri_mask);
216*24a4a0a5SArvind Ram Prakash } else {
21721b818c0SJeenu Viswambharan old_mask = plat_ic_set_priority_mask(priority);
218*24a4a0a5SArvind Ram Prakash }
219*24a4a0a5SArvind Ram Prakash #endif
22021b818c0SJeenu Viswambharan
221db1631efSJeenu Viswambharan if (old_mask > priority) {
22221b818c0SJeenu Viswambharan ERROR("Deactivation priority (0x%x) lower than Priority Mask (0x%x)\n",
22321b818c0SJeenu Viswambharan priority, old_mask);
22421b818c0SJeenu Viswambharan panic();
22521b818c0SJeenu Viswambharan }
22621b818c0SJeenu Viswambharan
22721b818c0SJeenu Viswambharan EHF_LOG("deactivate prio=%d\n", get_pe_highest_active_idx(pe_data));
22821b818c0SJeenu Viswambharan }
22921b818c0SJeenu Viswambharan
23021b818c0SJeenu Viswambharan /*
2313d732e23SJeenu Viswambharan * After leaving Non-secure world, stash current Non-secure Priority Mask, and
2323d732e23SJeenu Viswambharan * set Priority Mask to the highest Non-secure priority so that Non-secure
2333d732e23SJeenu Viswambharan * interrupts cannot preempt Secure execution.
2343d732e23SJeenu Viswambharan *
2353d732e23SJeenu Viswambharan * If the current running priority is in the secure range, or if there are
2363d732e23SJeenu Viswambharan * outstanding priority activations, this function does nothing.
2373d732e23SJeenu Viswambharan *
2383d732e23SJeenu Viswambharan * This function subscribes to the 'cm_exited_normal_world' event published by
2393d732e23SJeenu Viswambharan * the Context Management Library.
2403d732e23SJeenu Viswambharan */
ehf_exited_normal_world(const void * arg)2413d732e23SJeenu Viswambharan static void *ehf_exited_normal_world(const void *arg)
2423d732e23SJeenu Viswambharan {
2433d732e23SJeenu Viswambharan unsigned int run_pri;
2443d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data();
2453d732e23SJeenu Viswambharan
2463d732e23SJeenu Viswambharan /* If the running priority is in the secure range, do nothing */
2473d732e23SJeenu Viswambharan run_pri = plat_ic_get_running_priority();
2483d732e23SJeenu Viswambharan if (IS_PRI_SECURE(run_pri))
24903b645edSJeenu Viswambharan return NULL;
2503d732e23SJeenu Viswambharan
2513d732e23SJeenu Viswambharan /* Do nothing if there are explicit activations */
2523d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data))
25303b645edSJeenu Viswambharan return NULL;
2543d732e23SJeenu Viswambharan
25503b645edSJeenu Viswambharan assert(pe_data->ns_pri_mask == 0u);
2563d732e23SJeenu Viswambharan
2573d732e23SJeenu Viswambharan pe_data->ns_pri_mask =
25803b645edSJeenu Viswambharan (uint8_t) plat_ic_set_priority_mask(GIC_HIGHEST_NS_PRIORITY);
2593d732e23SJeenu Viswambharan
2603d732e23SJeenu Viswambharan /* The previous Priority Mask is not expected to be in secure range */
2613d732e23SJeenu Viswambharan if (IS_PRI_SECURE(pe_data->ns_pri_mask)) {
2623d732e23SJeenu Viswambharan ERROR("Priority Mask (0x%x) already in secure range\n",
2633d732e23SJeenu Viswambharan pe_data->ns_pri_mask);
2643d732e23SJeenu Viswambharan panic();
2653d732e23SJeenu Viswambharan }
2663d732e23SJeenu Viswambharan
2673d732e23SJeenu Viswambharan EHF_LOG("Priority Mask: 0x%x => 0x%x\n", pe_data->ns_pri_mask,
2683d732e23SJeenu Viswambharan GIC_HIGHEST_NS_PRIORITY);
2693d732e23SJeenu Viswambharan
27003b645edSJeenu Viswambharan return NULL;
2713d732e23SJeenu Viswambharan }
2723d732e23SJeenu Viswambharan
2733d732e23SJeenu Viswambharan /*
2743d732e23SJeenu Viswambharan * Conclude Secure execution and prepare for return to Non-secure world. Restore
2753d732e23SJeenu Viswambharan * the Non-secure Priority Mask previously stashed upon leaving Non-secure
2763d732e23SJeenu Viswambharan * world.
2773d732e23SJeenu Viswambharan *
2783d732e23SJeenu Viswambharan * If there the current running priority is in the secure range, or if there are
2793d732e23SJeenu Viswambharan * outstanding priority activations, this function does nothing.
2803d732e23SJeenu Viswambharan *
2813d732e23SJeenu Viswambharan * This function subscribes to the 'cm_entering_normal_world' event published by
2823d732e23SJeenu Viswambharan * the Context Management Library.
2833d732e23SJeenu Viswambharan */
ehf_entering_normal_world(const void * arg)2843d732e23SJeenu Viswambharan static void *ehf_entering_normal_world(const void *arg)
2853d732e23SJeenu Viswambharan {
2863d732e23SJeenu Viswambharan unsigned int old_pmr, run_pri;
2873d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data();
2883d732e23SJeenu Viswambharan
2893d732e23SJeenu Viswambharan /* If the running priority is in the secure range, do nothing */
2903d732e23SJeenu Viswambharan run_pri = plat_ic_get_running_priority();
2913d732e23SJeenu Viswambharan if (IS_PRI_SECURE(run_pri))
29203b645edSJeenu Viswambharan return NULL;
2933d732e23SJeenu Viswambharan
2943d732e23SJeenu Viswambharan /*
2953d732e23SJeenu Viswambharan * If there are explicit activations, do nothing. The Priority Mask will
2963d732e23SJeenu Viswambharan * be restored upon the last deactivation.
2973d732e23SJeenu Viswambharan */
2983d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data))
29903b645edSJeenu Viswambharan return NULL;
3003d732e23SJeenu Viswambharan
3013d732e23SJeenu Viswambharan /* Do nothing if we don't have a valid Priority Mask to restore */
30203b645edSJeenu Viswambharan if (pe_data->ns_pri_mask == 0U)
30303b645edSJeenu Viswambharan return NULL;
3043d732e23SJeenu Viswambharan
3053d732e23SJeenu Viswambharan old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
3063d732e23SJeenu Viswambharan
3073d732e23SJeenu Viswambharan /*
3083d732e23SJeenu Viswambharan * When exiting secure world, the current Priority Mask must be
3093d732e23SJeenu Viswambharan * GIC_HIGHEST_NS_PRIORITY (as set during entry), or the Non-secure
3103d732e23SJeenu Viswambharan * priority mask set upon calling ehf_allow_ns_preemption()
3113d732e23SJeenu Viswambharan */
3123d732e23SJeenu Viswambharan if ((old_pmr != GIC_HIGHEST_NS_PRIORITY) &&
3133d732e23SJeenu Viswambharan (old_pmr != pe_data->ns_pri_mask)) {
3143d732e23SJeenu Viswambharan ERROR("Invalid Priority Mask (0x%x) restored\n", old_pmr);
3153d732e23SJeenu Viswambharan panic();
3163d732e23SJeenu Viswambharan }
3173d732e23SJeenu Viswambharan
3183d732e23SJeenu Viswambharan EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
3193d732e23SJeenu Viswambharan
3203d732e23SJeenu Viswambharan pe_data->ns_pri_mask = 0;
3213d732e23SJeenu Viswambharan
32203b645edSJeenu Viswambharan return NULL;
3233d732e23SJeenu Viswambharan }
3243d732e23SJeenu Viswambharan
3253d732e23SJeenu Viswambharan /*
3263d732e23SJeenu Viswambharan * Program Priority Mask to the original Non-secure priority such that
32773308618SAntonio Nino Diaz * Non-secure interrupts may preempt Secure execution (for example, during
32873308618SAntonio Nino Diaz * Yielding SMC calls). The 'preempt_ret_code' parameter indicates the Yielding
32973308618SAntonio Nino Diaz * SMC's return value in case the call was preempted.
3303d732e23SJeenu Viswambharan *
3313d732e23SJeenu Viswambharan * This API is expected to be invoked before delegating a yielding SMC to Secure
3323d732e23SJeenu Viswambharan * EL1. I.e. within the window of secure execution after Non-secure context is
3333d732e23SJeenu Viswambharan * saved (after entry into EL3) and Secure context is restored (before entering
3343d732e23SJeenu Viswambharan * Secure EL1).
3353d732e23SJeenu Viswambharan */
ehf_allow_ns_preemption(uint64_t preempt_ret_code)336af34cd72SJeenu Viswambharan void ehf_allow_ns_preemption(uint64_t preempt_ret_code)
3373d732e23SJeenu Viswambharan {
338af34cd72SJeenu Viswambharan cpu_context_t *ns_ctx;
3393d732e23SJeenu Viswambharan unsigned int old_pmr __unused;
3403d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data();
3413d732e23SJeenu Viswambharan
3423d732e23SJeenu Viswambharan /*
3433d732e23SJeenu Viswambharan * We should have been notified earlier of entering secure world, and
3443d732e23SJeenu Viswambharan * therefore have stashed the Non-secure priority mask.
3453d732e23SJeenu Viswambharan */
34603b645edSJeenu Viswambharan assert(pe_data->ns_pri_mask != 0U);
3473d732e23SJeenu Viswambharan
3483d732e23SJeenu Viswambharan /* Make sure no priority levels are active when requesting this */
3493d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data)) {
3503d732e23SJeenu Viswambharan ERROR("PE %lx has priority activations: 0x%x\n",
3513d732e23SJeenu Viswambharan read_mpidr_el1(), pe_data->active_pri_bits);
3523d732e23SJeenu Viswambharan panic();
3533d732e23SJeenu Viswambharan }
3543d732e23SJeenu Viswambharan
355af34cd72SJeenu Viswambharan /*
356af34cd72SJeenu Viswambharan * Program preempted return code to x0 right away so that, if the
357af34cd72SJeenu Viswambharan * Yielding SMC was indeed preempted before a dispatcher gets a chance
358af34cd72SJeenu Viswambharan * to populate it, the caller would find the correct return value.
359af34cd72SJeenu Viswambharan */
360af34cd72SJeenu Viswambharan ns_ctx = cm_get_context(NON_SECURE);
36103b645edSJeenu Viswambharan assert(ns_ctx != NULL);
362af34cd72SJeenu Viswambharan write_ctx_reg(get_gpregs_ctx(ns_ctx), CTX_GPREG_X0, preempt_ret_code);
363af34cd72SJeenu Viswambharan
3643d732e23SJeenu Viswambharan old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
3653d732e23SJeenu Viswambharan
3663d732e23SJeenu Viswambharan EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
3673d732e23SJeenu Viswambharan
3683d732e23SJeenu Viswambharan pe_data->ns_pri_mask = 0;
3693d732e23SJeenu Viswambharan }
3703d732e23SJeenu Viswambharan
3713d732e23SJeenu Viswambharan /*
3723d732e23SJeenu Viswambharan * Return whether Secure execution has explicitly allowed Non-secure interrupts
37373308618SAntonio Nino Diaz * to preempt itself (for example, during Yielding SMC calls).
3743d732e23SJeenu Viswambharan */
ehf_is_ns_preemption_allowed(void)3753d732e23SJeenu Viswambharan unsigned int ehf_is_ns_preemption_allowed(void)
3763d732e23SJeenu Viswambharan {
3773d732e23SJeenu Viswambharan unsigned int run_pri;
3783d732e23SJeenu Viswambharan pe_exc_data_t *pe_data = this_cpu_data();
3793d732e23SJeenu Viswambharan
3803d732e23SJeenu Viswambharan /* If running priority is in secure range, return false */
3813d732e23SJeenu Viswambharan run_pri = plat_ic_get_running_priority();
3823d732e23SJeenu Viswambharan if (IS_PRI_SECURE(run_pri))
3833d732e23SJeenu Viswambharan return 0;
3843d732e23SJeenu Viswambharan
3853d732e23SJeenu Viswambharan /*
3863d732e23SJeenu Viswambharan * If Non-secure preemption was permitted by calling
3873d732e23SJeenu Viswambharan * ehf_allow_ns_preemption() earlier:
3883d732e23SJeenu Viswambharan *
3893d732e23SJeenu Viswambharan * - There wouldn't have been priority activations;
3903d732e23SJeenu Viswambharan * - We would have cleared the stashed the Non-secure Priority Mask.
3913d732e23SJeenu Viswambharan */
3923d732e23SJeenu Viswambharan if (has_valid_pri_activations(pe_data))
3933d732e23SJeenu Viswambharan return 0;
39403b645edSJeenu Viswambharan if (pe_data->ns_pri_mask != 0U)
3953d732e23SJeenu Viswambharan return 0;
3963d732e23SJeenu Viswambharan
3973d732e23SJeenu Viswambharan return 1;
3983d732e23SJeenu Viswambharan }
3993d732e23SJeenu Viswambharan
4003d732e23SJeenu Viswambharan /*
40121b818c0SJeenu Viswambharan * Top-level EL3 interrupt handler.
40221b818c0SJeenu Viswambharan */
ehf_el3_interrupt_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)40321b818c0SJeenu Viswambharan static uint64_t ehf_el3_interrupt_handler(uint32_t id, uint32_t flags,
40421b818c0SJeenu Viswambharan void *handle, void *cookie)
40521b818c0SJeenu Viswambharan {
40603b645edSJeenu Viswambharan int ret = 0;
40703b645edSJeenu Viswambharan uint32_t intr_raw;
40803b645edSJeenu Viswambharan unsigned int intr, pri, idx;
40921b818c0SJeenu Viswambharan ehf_handler_t handler;
41021b818c0SJeenu Viswambharan
41121b818c0SJeenu Viswambharan /*
41221b818c0SJeenu Viswambharan * Top-level interrupt type handler from Interrupt Management Framework
41321b818c0SJeenu Viswambharan * doesn't acknowledge the interrupt; so the interrupt ID must be
41421b818c0SJeenu Viswambharan * invalid.
41521b818c0SJeenu Viswambharan */
41621b818c0SJeenu Viswambharan assert(id == INTR_ID_UNAVAILABLE);
41721b818c0SJeenu Viswambharan
41821b818c0SJeenu Viswambharan /*
41921b818c0SJeenu Viswambharan * Acknowledge interrupt. Proceed with handling only for valid interrupt
42021b818c0SJeenu Viswambharan * IDs. This situation may arise because of Interrupt Management
42121b818c0SJeenu Viswambharan * Framework identifying an EL3 interrupt, but before it's been
42221b818c0SJeenu Viswambharan * acknowledged here, the interrupt was either deasserted, or there was
42321b818c0SJeenu Viswambharan * a higher-priority interrupt of another type.
42421b818c0SJeenu Viswambharan */
42521b818c0SJeenu Viswambharan intr_raw = plat_ic_acknowledge_interrupt();
42621b818c0SJeenu Viswambharan intr = plat_ic_get_interrupt_id(intr_raw);
42721b818c0SJeenu Viswambharan if (intr == INTR_ID_UNAVAILABLE)
42821b818c0SJeenu Viswambharan return 0;
42921b818c0SJeenu Viswambharan
43021b818c0SJeenu Viswambharan /* Having acknowledged the interrupt, get the running priority */
43121b818c0SJeenu Viswambharan pri = plat_ic_get_running_priority();
43221b818c0SJeenu Viswambharan
43321b818c0SJeenu Viswambharan /* Check EL3 interrupt priority is in secure range */
43421b818c0SJeenu Viswambharan assert(IS_PRI_SECURE(pri));
43521b818c0SJeenu Viswambharan
43621b818c0SJeenu Viswambharan /*
43721b818c0SJeenu Viswambharan * Translate the priority to a descriptor index. We do this by masking
43821b818c0SJeenu Viswambharan * and shifting the running priority value (platform-supplied).
43921b818c0SJeenu Viswambharan */
44021b818c0SJeenu Viswambharan idx = pri_to_idx(pri);
44121b818c0SJeenu Viswambharan
44221b818c0SJeenu Viswambharan /* Validate priority */
44321b818c0SJeenu Viswambharan assert(pri == IDX_TO_PRI(idx));
44421b818c0SJeenu Viswambharan
44503b645edSJeenu Viswambharan handler = (ehf_handler_t) RAW_HANDLER(
44603b645edSJeenu Viswambharan exception_data.ehf_priorities[idx].ehf_handler);
44703b645edSJeenu Viswambharan if (handler == NULL) {
44821b818c0SJeenu Viswambharan ERROR("No EL3 exception handler for priority 0x%x\n",
44921b818c0SJeenu Viswambharan IDX_TO_PRI(idx));
45021b818c0SJeenu Viswambharan panic();
45121b818c0SJeenu Viswambharan }
45221b818c0SJeenu Viswambharan
45321b818c0SJeenu Viswambharan /*
45421b818c0SJeenu Viswambharan * Call registered handler. Pass the raw interrupt value to registered
45521b818c0SJeenu Viswambharan * handlers.
45621b818c0SJeenu Viswambharan */
45721b818c0SJeenu Viswambharan ret = handler(intr_raw, flags, handle, cookie);
45821b818c0SJeenu Viswambharan
45903b645edSJeenu Viswambharan return (uint64_t) ret;
46021b818c0SJeenu Viswambharan }
46121b818c0SJeenu Viswambharan
46221b818c0SJeenu Viswambharan /*
46321b818c0SJeenu Viswambharan * Initialize the EL3 exception handling.
46421b818c0SJeenu Viswambharan */
ehf_init(void)46587c85134SDaniel Boulby void __init ehf_init(void)
46621b818c0SJeenu Viswambharan {
46721b818c0SJeenu Viswambharan unsigned int flags = 0;
46821b818c0SJeenu Viswambharan int ret __unused;
46921b818c0SJeenu Viswambharan
47021b818c0SJeenu Viswambharan /* Ensure EL3 interrupts are supported */
4711f6bb41dSMadhukar Pappireddy assert(plat_ic_has_interrupt_type(INTR_TYPE_EL3));
47221b818c0SJeenu Viswambharan
47321b818c0SJeenu Viswambharan /*
47421b818c0SJeenu Viswambharan * Make sure that priority water mark has enough bits to represent the
47521b818c0SJeenu Viswambharan * whole priority array.
47621b818c0SJeenu Viswambharan */
47703b645edSJeenu Viswambharan assert(exception_data.num_priorities <= (sizeof(ehf_pri_bits_t) * 8U));
47821b818c0SJeenu Viswambharan
47903b645edSJeenu Viswambharan assert(exception_data.ehf_priorities != NULL);
48021b818c0SJeenu Viswambharan
48121b818c0SJeenu Viswambharan /*
48221b818c0SJeenu Viswambharan * Bit 7 of GIC priority must be 0 for secure interrupts. This means
48321b818c0SJeenu Viswambharan * platforms must use at least 1 of the remaining 7 bits.
48421b818c0SJeenu Viswambharan */
48503b645edSJeenu Viswambharan assert((exception_data.pri_bits >= 1U) ||
48603b645edSJeenu Viswambharan (exception_data.pri_bits < 8U));
48721b818c0SJeenu Viswambharan
4887c2fe62fSRaghu Krishnamurthy /* Route EL3 interrupts when in Non-secure. */
48921b818c0SJeenu Viswambharan set_interrupt_rm_flag(flags, NON_SECURE);
4907c2fe62fSRaghu Krishnamurthy
4917671008fSManish Pandey /* Route EL3 interrupts only when SPM_MM present in secure. */
4927671008fSManish Pandey #if SPM_MM
49321b818c0SJeenu Viswambharan set_interrupt_rm_flag(flags, SECURE);
4947671008fSManish Pandey #endif
49521b818c0SJeenu Viswambharan
49621b818c0SJeenu Viswambharan /* Register handler for EL3 interrupts */
49721b818c0SJeenu Viswambharan ret = register_interrupt_type_handler(INTR_TYPE_EL3,
49821b818c0SJeenu Viswambharan ehf_el3_interrupt_handler, flags);
49921b818c0SJeenu Viswambharan assert(ret == 0);
50021b818c0SJeenu Viswambharan }
50121b818c0SJeenu Viswambharan
50221b818c0SJeenu Viswambharan /*
50321b818c0SJeenu Viswambharan * Register a handler at the supplied priority. Registration is allowed only if
50421b818c0SJeenu Viswambharan * a handler hasn't been registered before, or one wasn't provided at build
50521b818c0SJeenu Viswambharan * time. The priority for which the handler is being registered must also accord
50621b818c0SJeenu Viswambharan * with the platform-supplied data.
50721b818c0SJeenu Viswambharan */
ehf_register_priority_handler(unsigned int pri,ehf_handler_t handler)50821b818c0SJeenu Viswambharan void ehf_register_priority_handler(unsigned int pri, ehf_handler_t handler)
50921b818c0SJeenu Viswambharan {
51003b645edSJeenu Viswambharan unsigned int idx;
51121b818c0SJeenu Viswambharan
51221b818c0SJeenu Viswambharan /* Sanity check for handler */
51321b818c0SJeenu Viswambharan assert(handler != NULL);
51421b818c0SJeenu Viswambharan
51521b818c0SJeenu Viswambharan /* Handler ought to be 4-byte aligned */
51603b645edSJeenu Viswambharan assert((((uintptr_t) handler) & 3U) == 0U);
51721b818c0SJeenu Viswambharan
51821b818c0SJeenu Viswambharan /* Ensure we register for valid priority */
51921b818c0SJeenu Viswambharan idx = pri_to_idx(pri);
52021b818c0SJeenu Viswambharan assert(idx < exception_data.num_priorities);
52121b818c0SJeenu Viswambharan assert(IDX_TO_PRI(idx) == pri);
52221b818c0SJeenu Viswambharan
52321b818c0SJeenu Viswambharan /* Return failure if a handler was already registered */
52403b645edSJeenu Viswambharan if (exception_data.ehf_priorities[idx].ehf_handler != EHF_NO_HANDLER_) {
52521b818c0SJeenu Viswambharan ERROR("Handler already registered for priority 0x%x\n", pri);
52621b818c0SJeenu Viswambharan panic();
52721b818c0SJeenu Viswambharan }
52821b818c0SJeenu Viswambharan
52921b818c0SJeenu Viswambharan /*
53021b818c0SJeenu Viswambharan * Install handler, and retain the valid bit. We assume that the handler
53121b818c0SJeenu Viswambharan * is 4-byte aligned, which is usually the case.
53221b818c0SJeenu Viswambharan */
53321b818c0SJeenu Viswambharan exception_data.ehf_priorities[idx].ehf_handler =
53403b645edSJeenu Viswambharan (((uintptr_t) handler) | EHF_PRI_VALID_);
53521b818c0SJeenu Viswambharan
53621b818c0SJeenu Viswambharan EHF_LOG("register pri=0x%x handler=%p\n", pri, handler);
53721b818c0SJeenu Viswambharan }
5383d732e23SJeenu Viswambharan
5393d732e23SJeenu Viswambharan SUBSCRIBE_TO_EVENT(cm_entering_normal_world, ehf_entering_normal_world);
5403d732e23SJeenu Viswambharan SUBSCRIBE_TO_EVENT(cm_exited_normal_world, ehf_exited_normal_world);
541