xref: /aosp_15_r20/external/arm-trusted-firmware/bl32/tsp/tsp_interrupt.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <inttypes.h>
9*54fd6939SJiyong Park 
10*54fd6939SJiyong Park #include <platform_def.h>
11*54fd6939SJiyong Park 
12*54fd6939SJiyong Park #include <arch_helpers.h>
13*54fd6939SJiyong Park #include <bl32/tsp/tsp.h>
14*54fd6939SJiyong Park #include <common/debug.h>
15*54fd6939SJiyong Park #include <plat/common/platform.h>
16*54fd6939SJiyong Park 
17*54fd6939SJiyong Park #include "tsp_private.h"
18*54fd6939SJiyong Park 
19*54fd6939SJiyong Park /*******************************************************************************
20*54fd6939SJiyong Park  * This function updates the TSP statistics for S-EL1 interrupts handled
21*54fd6939SJiyong Park  * synchronously i.e the ones that have been handed over by the TSPD. It also
22*54fd6939SJiyong Park  * keeps count of the number of times control was passed back to the TSPD
23*54fd6939SJiyong Park  * after handling the interrupt. In the future it will be possible that the
24*54fd6939SJiyong Park  * TSPD hands over an S-EL1 interrupt to the TSP but does not expect it to
25*54fd6939SJiyong Park  * return execution. This statistic will be useful to distinguish between these
26*54fd6939SJiyong Park  * two models of synchronous S-EL1 interrupt handling. The 'elr_el3' parameter
27*54fd6939SJiyong Park  * contains the address of the instruction in normal world where this S-EL1
28*54fd6939SJiyong Park  * interrupt was generated.
29*54fd6939SJiyong Park  ******************************************************************************/
tsp_update_sync_sel1_intr_stats(uint32_t type,uint64_t elr_el3)30*54fd6939SJiyong Park void tsp_update_sync_sel1_intr_stats(uint32_t type, uint64_t elr_el3)
31*54fd6939SJiyong Park {
32*54fd6939SJiyong Park 	uint32_t linear_id = plat_my_core_pos();
33*54fd6939SJiyong Park 
34*54fd6939SJiyong Park 	tsp_stats[linear_id].sync_sel1_intr_count++;
35*54fd6939SJiyong Park 	if (type == TSP_HANDLE_SEL1_INTR_AND_RETURN)
36*54fd6939SJiyong Park 		tsp_stats[linear_id].sync_sel1_intr_ret_count++;
37*54fd6939SJiyong Park 
38*54fd6939SJiyong Park #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
39*54fd6939SJiyong Park 	spin_lock(&console_lock);
40*54fd6939SJiyong Park 	VERBOSE("TSP: cpu 0x%lx sync s-el1 interrupt request from 0x%" PRIx64 "\n",
41*54fd6939SJiyong Park 		read_mpidr(), elr_el3);
42*54fd6939SJiyong Park 	VERBOSE("TSP: cpu 0x%lx: %d sync s-el1 interrupt requests,"
43*54fd6939SJiyong Park 		" %d sync s-el1 interrupt returns\n",
44*54fd6939SJiyong Park 		read_mpidr(),
45*54fd6939SJiyong Park 		tsp_stats[linear_id].sync_sel1_intr_count,
46*54fd6939SJiyong Park 		tsp_stats[linear_id].sync_sel1_intr_ret_count);
47*54fd6939SJiyong Park 	spin_unlock(&console_lock);
48*54fd6939SJiyong Park #endif
49*54fd6939SJiyong Park }
50*54fd6939SJiyong Park 
51*54fd6939SJiyong Park /******************************************************************************
52*54fd6939SJiyong Park  * This function is invoked when a non S-EL1 interrupt is received and causes
53*54fd6939SJiyong Park  * the preemption of TSP. This function returns TSP_PREEMPTED and results
54*54fd6939SJiyong Park  * in the control being handed over to EL3 for handling the interrupt.
55*54fd6939SJiyong Park  *****************************************************************************/
tsp_handle_preemption(void)56*54fd6939SJiyong Park int32_t tsp_handle_preemption(void)
57*54fd6939SJiyong Park {
58*54fd6939SJiyong Park 	uint32_t linear_id = plat_my_core_pos();
59*54fd6939SJiyong Park 
60*54fd6939SJiyong Park 	tsp_stats[linear_id].preempt_intr_count++;
61*54fd6939SJiyong Park #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
62*54fd6939SJiyong Park 	spin_lock(&console_lock);
63*54fd6939SJiyong Park 	VERBOSE("TSP: cpu 0x%lx: %d preempt interrupt requests\n",
64*54fd6939SJiyong Park 		read_mpidr(), tsp_stats[linear_id].preempt_intr_count);
65*54fd6939SJiyong Park 	spin_unlock(&console_lock);
66*54fd6939SJiyong Park #endif
67*54fd6939SJiyong Park 	return TSP_PREEMPTED;
68*54fd6939SJiyong Park }
69*54fd6939SJiyong Park 
70*54fd6939SJiyong Park /*******************************************************************************
71*54fd6939SJiyong Park  * TSP interrupt handler is called as a part of both synchronous and
72*54fd6939SJiyong Park  * asynchronous handling of TSP interrupts. Currently the physical timer
73*54fd6939SJiyong Park  * interrupt is the only S-EL1 interrupt that this handler expects. It returns
74*54fd6939SJiyong Park  * 0 upon successfully handling the expected interrupt and all other
75*54fd6939SJiyong Park  * interrupts are treated as normal world or EL3 interrupts.
76*54fd6939SJiyong Park  ******************************************************************************/
tsp_common_int_handler(void)77*54fd6939SJiyong Park int32_t tsp_common_int_handler(void)
78*54fd6939SJiyong Park {
79*54fd6939SJiyong Park 	uint32_t linear_id = plat_my_core_pos(), id;
80*54fd6939SJiyong Park 
81*54fd6939SJiyong Park 	/*
82*54fd6939SJiyong Park 	 * Get the highest priority pending interrupt id and see if it is the
83*54fd6939SJiyong Park 	 * secure physical generic timer interrupt in which case, handle it.
84*54fd6939SJiyong Park 	 * Otherwise throw this interrupt at the EL3 firmware.
85*54fd6939SJiyong Park 	 *
86*54fd6939SJiyong Park 	 * There is a small time window between reading the highest priority
87*54fd6939SJiyong Park 	 * pending interrupt and acknowledging it during which another
88*54fd6939SJiyong Park 	 * interrupt of higher priority could become the highest pending
89*54fd6939SJiyong Park 	 * interrupt. This is not expected to happen currently for TSP.
90*54fd6939SJiyong Park 	 */
91*54fd6939SJiyong Park 	id = plat_ic_get_pending_interrupt_id();
92*54fd6939SJiyong Park 
93*54fd6939SJiyong Park 	/* TSP can only handle the secure physical timer interrupt */
94*54fd6939SJiyong Park 	if (id != TSP_IRQ_SEC_PHY_TIMER)
95*54fd6939SJiyong Park 		return tsp_handle_preemption();
96*54fd6939SJiyong Park 
97*54fd6939SJiyong Park 	/*
98*54fd6939SJiyong Park 	 * Acknowledge and handle the secure timer interrupt. Also sanity check
99*54fd6939SJiyong Park 	 * if it has been preempted by another interrupt through an assertion.
100*54fd6939SJiyong Park 	 */
101*54fd6939SJiyong Park 	id = plat_ic_acknowledge_interrupt();
102*54fd6939SJiyong Park 	assert(id == TSP_IRQ_SEC_PHY_TIMER);
103*54fd6939SJiyong Park 	tsp_generic_timer_handler();
104*54fd6939SJiyong Park 	plat_ic_end_of_interrupt(id);
105*54fd6939SJiyong Park 
106*54fd6939SJiyong Park 	/* Update the statistics and print some messages */
107*54fd6939SJiyong Park 	tsp_stats[linear_id].sel1_intr_count++;
108*54fd6939SJiyong Park #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
109*54fd6939SJiyong Park 	spin_lock(&console_lock);
110*54fd6939SJiyong Park 	VERBOSE("TSP: cpu 0x%lx handled S-EL1 interrupt %d\n",
111*54fd6939SJiyong Park 	       read_mpidr(), id);
112*54fd6939SJiyong Park 	VERBOSE("TSP: cpu 0x%lx: %d S-EL1 requests\n",
113*54fd6939SJiyong Park 	     read_mpidr(), tsp_stats[linear_id].sel1_intr_count);
114*54fd6939SJiyong Park 	spin_unlock(&console_lock);
115*54fd6939SJiyong Park #endif
116*54fd6939SJiyong Park 	return 0;
117*54fd6939SJiyong Park }
118