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