blob: 3a14635c6b2f4a96ba09dcf1c275a3cd8beb945c [file] [log] [blame]
Jeenu Viswambharan10a67272017-09-22 08:32:10 +01001/*
Arvind Ram Prakash579a23c2024-02-05 16:19:37 -06002 * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
Jeenu Viswambharan10a67272017-09-22 08:32:10 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * Exception handlers at EL3, their priority levels, and management.
9 */
10
11#include <assert.h>
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010012#include <stdbool.h>
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010013
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <bl31/ehf.h>
15#include <bl31/interrupt_mgmt.h>
16#include <context.h>
17#include <common/debug.h>
18#include <drivers/arm/gic_common.h>
19#include <lib/el3_runtime/context_mgmt.h>
20#include <lib/el3_runtime/cpu_data.h>
21#include <lib/el3_runtime/pubsub_events.h>
22#include <plat/common/platform.h>
23
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010024/* Output EHF logs as verbose */
25#define EHF_LOG(...) VERBOSE("EHF: " __VA_ARGS__)
26
27#define EHF_INVALID_IDX (-1)
28
29/* For a valid handler, return the actual function pointer; otherwise, 0. */
30#define RAW_HANDLER(h) \
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010031 ((ehf_handler_t) ((((h) & EHF_PRI_VALID_) != 0U) ? \
32 ((h) & ~EHF_PRI_VALID_) : 0U))
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010033
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010034#define PRI_BIT(idx) (((ehf_pri_bits_t) 1u) << (idx))
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010035
36/*
37 * Convert index into secure priority using the platform-defined priority bits
38 * field.
39 */
40#define IDX_TO_PRI(idx) \
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010041 ((((unsigned) idx) << (7u - exception_data.pri_bits)) & 0x7fU)
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010042
43/* Check whether a given index is valid */
44#define IS_IDX_VALID(idx) \
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010045 ((exception_data.ehf_priorities[idx].ehf_handler & EHF_PRI_VALID_) != 0U)
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010046
47/* Returns whether given priority is in secure priority range */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010048#define IS_PRI_SECURE(pri) (((pri) & 0x80U) == 0U)
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010049
50/* To be defined by the platform */
51extern const ehf_priorities_t exception_data;
52
53/* Translate priority to the index in the priority array */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010054static unsigned int pri_to_idx(unsigned int priority)
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010055{
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010056 unsigned int idx;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010057
58 idx = EHF_PRI_TO_IDX(priority, exception_data.pri_bits);
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010059 assert(idx < exception_data.num_priorities);
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010060 assert(IS_IDX_VALID(idx));
61
62 return idx;
63}
64
65/* Return whether there are outstanding priority activation */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010066static bool has_valid_pri_activations(pe_exc_data_t *pe_data)
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010067{
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010068 return pe_data->active_pri_bits != 0U;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010069}
70
71static pe_exc_data_t *this_cpu_data(void)
72{
73 return &get_cpu_data(ehf_data);
74}
75
76/*
77 * Return the current priority index of this CPU. If no priority is active,
78 * return EHF_INVALID_IDX.
79 */
80static int get_pe_highest_active_idx(pe_exc_data_t *pe_data)
81{
82 if (!has_valid_pri_activations(pe_data))
83 return EHF_INVALID_IDX;
84
85 /* Current priority is the right-most bit */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +010086 return (int) __builtin_ctz(pe_data->active_pri_bits);
Jeenu Viswambharan10a67272017-09-22 08:32:10 +010087}
88
89/*
90 * Mark priority active by setting the corresponding bit in active_pri_bits and
91 * programming the priority mask.
92 *
93 * This API is to be used as part of delegating to lower ELs other than for
94 * interrupts; e.g. while handling synchronous exceptions.
95 *
96 * This API is expected to be invoked before restoring context (Secure or
97 * Non-secure) in preparation for the respective dispatch.
98 */
99void ehf_activate_priority(unsigned int priority)
100{
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100101 int cur_pri_idx;
102 unsigned int old_mask, run_pri, idx;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100103 pe_exc_data_t *pe_data = this_cpu_data();
104
105 /*
106 * Query interrupt controller for the running priority, or idle priority
107 * if no interrupts are being handled. The requested priority must be
108 * less (higher priority) than the active running priority.
109 */
110 run_pri = plat_ic_get_running_priority();
111 if (priority >= run_pri) {
112 ERROR("Running priority higher (0x%x) than requested (0x%x)\n",
113 run_pri, priority);
114 panic();
115 }
116
117 /*
118 * If there were priority activations already, the requested priority
119 * must be less (higher priority) than the current highest priority
120 * activation so far.
121 */
122 cur_pri_idx = get_pe_highest_active_idx(pe_data);
123 idx = pri_to_idx(priority);
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100124 if ((cur_pri_idx != EHF_INVALID_IDX) &&
125 (idx >= ((unsigned int) cur_pri_idx))) {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100126 ERROR("Activation priority mismatch: req=0x%x current=0x%x\n",
127 priority, IDX_TO_PRI(cur_pri_idx));
128 panic();
129 }
130
131 /* Set the bit corresponding to the requested priority */
132 pe_data->active_pri_bits |= PRI_BIT(idx);
133
134 /*
135 * Program priority mask for the activated level. Check that the new
136 * priority mask is setting a higher priority level than the existing
137 * mask.
138 */
139 old_mask = plat_ic_set_priority_mask(priority);
140 if (priority >= old_mask) {
141 ERROR("Requested priority (0x%x) lower than Priority Mask (0x%x)\n",
142 priority, old_mask);
143 panic();
144 }
145
146 /*
147 * If this is the first activation, save the priority mask. This will be
148 * restored after the last deactivation.
149 */
150 if (cur_pri_idx == EHF_INVALID_IDX)
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100151 pe_data->init_pri_mask = (uint8_t) old_mask;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100152
153 EHF_LOG("activate prio=%d\n", get_pe_highest_active_idx(pe_data));
154}
155
156/*
157 * Mark priority inactive by clearing the corresponding bit in active_pri_bits,
158 * and programming the priority mask.
159 *
160 * This API is expected to be used as part of delegating to to lower ELs other
161 * than for interrupts; e.g. while handling synchronous exceptions.
162 *
163 * This API is expected to be invoked after saving context (Secure or
164 * Non-secure), having concluded the respective dispatch.
165 */
166void ehf_deactivate_priority(unsigned int priority)
167{
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100168 int cur_pri_idx;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100169 pe_exc_data_t *pe_data = this_cpu_data();
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100170 unsigned int old_mask, run_pri, idx;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100171
172 /*
173 * Query interrupt controller for the running priority, or idle priority
174 * if no interrupts are being handled. The requested priority must be
175 * less (higher priority) than the active running priority.
176 */
177 run_pri = plat_ic_get_running_priority();
178 if (priority >= run_pri) {
179 ERROR("Running priority higher (0x%x) than requested (0x%x)\n",
180 run_pri, priority);
181 panic();
182 }
183
184 /*
185 * Deactivation is allowed only when there are priority activations, and
186 * the deactivation priority level must match the current activated
187 * priority.
188 */
189 cur_pri_idx = get_pe_highest_active_idx(pe_data);
190 idx = pri_to_idx(priority);
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100191 if ((cur_pri_idx == EHF_INVALID_IDX) ||
192 (idx != ((unsigned int) cur_pri_idx))) {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100193 ERROR("Deactivation priority mismatch: req=0x%x current=0x%x\n",
194 priority, IDX_TO_PRI(cur_pri_idx));
195 panic();
196 }
197
198 /* Clear bit corresponding to highest priority */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100199 pe_data->active_pri_bits &= (pe_data->active_pri_bits - 1u);
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100200
201 /*
202 * Restore priority mask corresponding to the next priority, or the
203 * one stashed earlier if there are no more to deactivate.
204 */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100205 cur_pri_idx = get_pe_highest_active_idx(pe_data);
Arvind Ram Prakash579a23c2024-02-05 16:19:37 -0600206
207#if GIC600_ERRATA_WA_2384374
208 if (cur_pri_idx == EHF_INVALID_IDX) {
209 old_mask = plat_ic_deactivate_priority(pe_data->init_pri_mask);
210 } else {
211 old_mask = plat_ic_deactivate_priority(priority);
212 }
213#else
214 if (cur_pri_idx == EHF_INVALID_IDX) {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100215 old_mask = plat_ic_set_priority_mask(pe_data->init_pri_mask);
Arvind Ram Prakash579a23c2024-02-05 16:19:37 -0600216 } else {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100217 old_mask = plat_ic_set_priority_mask(priority);
Arvind Ram Prakash579a23c2024-02-05 16:19:37 -0600218 }
219#endif
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100220
Jeenu Viswambharan7addf622018-02-05 11:20:37 +0000221 if (old_mask > priority) {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100222 ERROR("Deactivation priority (0x%x) lower than Priority Mask (0x%x)\n",
223 priority, old_mask);
224 panic();
225 }
226
227 EHF_LOG("deactivate prio=%d\n", get_pe_highest_active_idx(pe_data));
228}
229
230/*
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100231 * After leaving Non-secure world, stash current Non-secure Priority Mask, and
232 * set Priority Mask to the highest Non-secure priority so that Non-secure
233 * interrupts cannot preempt Secure execution.
234 *
235 * If the current running priority is in the secure range, or if there are
236 * outstanding priority activations, this function does nothing.
237 *
238 * This function subscribes to the 'cm_exited_normal_world' event published by
239 * the Context Management Library.
240 */
241static void *ehf_exited_normal_world(const void *arg)
242{
243 unsigned int run_pri;
244 pe_exc_data_t *pe_data = this_cpu_data();
245
246 /* If the running priority is in the secure range, do nothing */
247 run_pri = plat_ic_get_running_priority();
248 if (IS_PRI_SECURE(run_pri))
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100249 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100250
251 /* Do nothing if there are explicit activations */
252 if (has_valid_pri_activations(pe_data))
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100253 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100254
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100255 assert(pe_data->ns_pri_mask == 0u);
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100256
257 pe_data->ns_pri_mask =
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100258 (uint8_t) plat_ic_set_priority_mask(GIC_HIGHEST_NS_PRIORITY);
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100259
260 /* The previous Priority Mask is not expected to be in secure range */
261 if (IS_PRI_SECURE(pe_data->ns_pri_mask)) {
262 ERROR("Priority Mask (0x%x) already in secure range\n",
263 pe_data->ns_pri_mask);
264 panic();
265 }
266
267 EHF_LOG("Priority Mask: 0x%x => 0x%x\n", pe_data->ns_pri_mask,
268 GIC_HIGHEST_NS_PRIORITY);
269
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100270 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100271}
272
273/*
274 * Conclude Secure execution and prepare for return to Non-secure world. Restore
275 * the Non-secure Priority Mask previously stashed upon leaving Non-secure
276 * world.
277 *
278 * If there the current running priority is in the secure range, or if there are
279 * outstanding priority activations, this function does nothing.
280 *
281 * This function subscribes to the 'cm_entering_normal_world' event published by
282 * the Context Management Library.
283 */
284static void *ehf_entering_normal_world(const void *arg)
285{
286 unsigned int old_pmr, run_pri;
287 pe_exc_data_t *pe_data = this_cpu_data();
288
289 /* If the running priority is in the secure range, do nothing */
290 run_pri = plat_ic_get_running_priority();
291 if (IS_PRI_SECURE(run_pri))
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100292 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100293
294 /*
295 * If there are explicit activations, do nothing. The Priority Mask will
296 * be restored upon the last deactivation.
297 */
298 if (has_valid_pri_activations(pe_data))
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100299 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100300
301 /* Do nothing if we don't have a valid Priority Mask to restore */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100302 if (pe_data->ns_pri_mask == 0U)
303 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100304
305 old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
306
307 /*
308 * When exiting secure world, the current Priority Mask must be
309 * GIC_HIGHEST_NS_PRIORITY (as set during entry), or the Non-secure
310 * priority mask set upon calling ehf_allow_ns_preemption()
311 */
312 if ((old_pmr != GIC_HIGHEST_NS_PRIORITY) &&
313 (old_pmr != pe_data->ns_pri_mask)) {
314 ERROR("Invalid Priority Mask (0x%x) restored\n", old_pmr);
315 panic();
316 }
317
318 EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
319
320 pe_data->ns_pri_mask = 0;
321
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100322 return NULL;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100323}
324
325/*
326 * Program Priority Mask to the original Non-secure priority such that
Antonio Nino Diaz56b68ad2019-02-28 13:35:21 +0000327 * Non-secure interrupts may preempt Secure execution (for example, during
328 * Yielding SMC calls). The 'preempt_ret_code' parameter indicates the Yielding
329 * SMC's return value in case the call was preempted.
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100330 *
331 * This API is expected to be invoked before delegating a yielding SMC to Secure
332 * EL1. I.e. within the window of secure execution after Non-secure context is
333 * saved (after entry into EL3) and Secure context is restored (before entering
334 * Secure EL1).
335 */
Jeenu Viswambharaneeb353c2018-01-22 12:29:12 +0000336void ehf_allow_ns_preemption(uint64_t preempt_ret_code)
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100337{
Jeenu Viswambharaneeb353c2018-01-22 12:29:12 +0000338 cpu_context_t *ns_ctx;
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100339 unsigned int old_pmr __unused;
340 pe_exc_data_t *pe_data = this_cpu_data();
341
342 /*
343 * We should have been notified earlier of entering secure world, and
344 * therefore have stashed the Non-secure priority mask.
345 */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100346 assert(pe_data->ns_pri_mask != 0U);
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100347
348 /* Make sure no priority levels are active when requesting this */
349 if (has_valid_pri_activations(pe_data)) {
350 ERROR("PE %lx has priority activations: 0x%x\n",
351 read_mpidr_el1(), pe_data->active_pri_bits);
352 panic();
353 }
354
Jeenu Viswambharaneeb353c2018-01-22 12:29:12 +0000355 /*
356 * Program preempted return code to x0 right away so that, if the
357 * Yielding SMC was indeed preempted before a dispatcher gets a chance
358 * to populate it, the caller would find the correct return value.
359 */
360 ns_ctx = cm_get_context(NON_SECURE);
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100361 assert(ns_ctx != NULL);
Jeenu Viswambharaneeb353c2018-01-22 12:29:12 +0000362 write_ctx_reg(get_gpregs_ctx(ns_ctx), CTX_GPREG_X0, preempt_ret_code);
363
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100364 old_pmr = plat_ic_set_priority_mask(pe_data->ns_pri_mask);
365
366 EHF_LOG("Priority Mask: 0x%x => 0x%x\n", old_pmr, pe_data->ns_pri_mask);
367
368 pe_data->ns_pri_mask = 0;
369}
370
371/*
372 * Return whether Secure execution has explicitly allowed Non-secure interrupts
Antonio Nino Diaz56b68ad2019-02-28 13:35:21 +0000373 * to preempt itself (for example, during Yielding SMC calls).
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100374 */
375unsigned int ehf_is_ns_preemption_allowed(void)
376{
377 unsigned int run_pri;
378 pe_exc_data_t *pe_data = this_cpu_data();
379
380 /* If running priority is in secure range, return false */
381 run_pri = plat_ic_get_running_priority();
382 if (IS_PRI_SECURE(run_pri))
383 return 0;
384
385 /*
386 * If Non-secure preemption was permitted by calling
387 * ehf_allow_ns_preemption() earlier:
388 *
389 * - There wouldn't have been priority activations;
390 * - We would have cleared the stashed the Non-secure Priority Mask.
391 */
392 if (has_valid_pri_activations(pe_data))
393 return 0;
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100394 if (pe_data->ns_pri_mask != 0U)
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100395 return 0;
396
397 return 1;
398}
399
400/*
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100401 * Top-level EL3 interrupt handler.
402 */
403static uint64_t ehf_el3_interrupt_handler(uint32_t id, uint32_t flags,
404 void *handle, void *cookie)
405{
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100406 int ret = 0;
407 uint32_t intr_raw;
408 unsigned int intr, pri, idx;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100409 ehf_handler_t handler;
410
411 /*
412 * Top-level interrupt type handler from Interrupt Management Framework
413 * doesn't acknowledge the interrupt; so the interrupt ID must be
414 * invalid.
415 */
416 assert(id == INTR_ID_UNAVAILABLE);
417
418 /*
419 * Acknowledge interrupt. Proceed with handling only for valid interrupt
420 * IDs. This situation may arise because of Interrupt Management
421 * Framework identifying an EL3 interrupt, but before it's been
422 * acknowledged here, the interrupt was either deasserted, or there was
423 * a higher-priority interrupt of another type.
424 */
425 intr_raw = plat_ic_acknowledge_interrupt();
426 intr = plat_ic_get_interrupt_id(intr_raw);
427 if (intr == INTR_ID_UNAVAILABLE)
428 return 0;
429
430 /* Having acknowledged the interrupt, get the running priority */
431 pri = plat_ic_get_running_priority();
432
433 /* Check EL3 interrupt priority is in secure range */
434 assert(IS_PRI_SECURE(pri));
435
436 /*
437 * Translate the priority to a descriptor index. We do this by masking
438 * and shifting the running priority value (platform-supplied).
439 */
440 idx = pri_to_idx(pri);
441
442 /* Validate priority */
443 assert(pri == IDX_TO_PRI(idx));
444
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100445 handler = (ehf_handler_t) RAW_HANDLER(
446 exception_data.ehf_priorities[idx].ehf_handler);
447 if (handler == NULL) {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100448 ERROR("No EL3 exception handler for priority 0x%x\n",
449 IDX_TO_PRI(idx));
450 panic();
451 }
452
453 /*
454 * Call registered handler. Pass the raw interrupt value to registered
455 * handlers.
456 */
457 ret = handler(intr_raw, flags, handle, cookie);
458
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100459 return (uint64_t) ret;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100460}
461
462/*
463 * Initialize the EL3 exception handling.
464 */
Daniel Boulby5753e492018-09-20 14:12:46 +0100465void __init ehf_init(void)
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100466{
467 unsigned int flags = 0;
468 int ret __unused;
469
470 /* Ensure EL3 interrupts are supported */
Madhukar Pappireddy67ac77c2023-09-06 16:50:22 -0500471 assert(plat_ic_has_interrupt_type(INTR_TYPE_EL3));
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100472
473 /*
474 * Make sure that priority water mark has enough bits to represent the
475 * whole priority array.
476 */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100477 assert(exception_data.num_priorities <= (sizeof(ehf_pri_bits_t) * 8U));
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100478
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100479 assert(exception_data.ehf_priorities != NULL);
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100480
481 /*
482 * Bit 7 of GIC priority must be 0 for secure interrupts. This means
483 * platforms must use at least 1 of the remaining 7 bits.
484 */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100485 assert((exception_data.pri_bits >= 1U) ||
486 (exception_data.pri_bits < 8U));
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100487
Raghu Krishnamurthy669bf402022-07-25 14:44:33 -0700488 /* Route EL3 interrupts when in Non-secure. */
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100489 set_interrupt_rm_flag(flags, NON_SECURE);
Raghu Krishnamurthy669bf402022-07-25 14:44:33 -0700490
Manish Pandeya47a61a2023-11-20 12:22:08 +0000491 /* Route EL3 interrupts only when SPM_MM present in secure. */
492#if SPM_MM
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100493 set_interrupt_rm_flag(flags, SECURE);
Manish Pandeya47a61a2023-11-20 12:22:08 +0000494#endif
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100495
496 /* Register handler for EL3 interrupts */
497 ret = register_interrupt_type_handler(INTR_TYPE_EL3,
498 ehf_el3_interrupt_handler, flags);
499 assert(ret == 0);
500}
501
502/*
503 * Register a handler at the supplied priority. Registration is allowed only if
504 * a handler hasn't been registered before, or one wasn't provided at build
505 * time. The priority for which the handler is being registered must also accord
506 * with the platform-supplied data.
507 */
508void ehf_register_priority_handler(unsigned int pri, ehf_handler_t handler)
509{
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100510 unsigned int idx;
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100511
512 /* Sanity check for handler */
513 assert(handler != NULL);
514
515 /* Handler ought to be 4-byte aligned */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100516 assert((((uintptr_t) handler) & 3U) == 0U);
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100517
518 /* Ensure we register for valid priority */
519 idx = pri_to_idx(pri);
520 assert(idx < exception_data.num_priorities);
521 assert(IDX_TO_PRI(idx) == pri);
522
523 /* Return failure if a handler was already registered */
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100524 if (exception_data.ehf_priorities[idx].ehf_handler != EHF_NO_HANDLER_) {
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100525 ERROR("Handler already registered for priority 0x%x\n", pri);
526 panic();
527 }
528
529 /*
530 * Install handler, and retain the valid bit. We assume that the handler
531 * is 4-byte aligned, which is usually the case.
532 */
533 exception_data.ehf_priorities[idx].ehf_handler =
Jeenu Viswambharan837cc9c2018-08-02 10:14:12 +0100534 (((uintptr_t) handler) | EHF_PRI_VALID_);
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100535
536 EHF_LOG("register pri=0x%x handler=%p\n", pri, handler);
537}
Jeenu Viswambharan6c6f24d2017-10-04 12:21:34 +0100538
539SUBSCRIBE_TO_EVENT(cm_entering_normal_world, ehf_entering_normal_world);
540SUBSCRIBE_TO_EVENT(cm_exited_normal_world, ehf_exited_normal_world);