xref: /rk3399_ARM-atf/bl32/tsp/tsp_interrupt.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
16cf89021SAchin Gupta /*
21b70db06SDan Handley  * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
36cf89021SAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
56cf89021SAchin Gupta  */
66cf89021SAchin Gupta 
76cf89021SAchin Gupta #include <assert.h>
8*09d40e0eSAntonio Nino Diaz 
95f0cdb05SDan Handley #include <platform_def.h>
10*09d40e0eSAntonio Nino Diaz 
11*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
12*09d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h>
13*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
14*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
15*09d40e0eSAntonio Nino Diaz 
16da0af78aSDan Handley #include "tsp_private.h"
176cf89021SAchin Gupta 
186cf89021SAchin Gupta /*******************************************************************************
1902446137SSoby Mathew  * This function updates the TSP statistics for S-EL1 interrupts handled
2002446137SSoby Mathew  * synchronously i.e the ones that have been handed over by the TSPD. It also
2102446137SSoby Mathew  * keeps count of the number of times control was passed back to the TSPD
2202446137SSoby Mathew  * after handling the interrupt. In the future it will be possible that the
2302446137SSoby Mathew  * TSPD hands over an S-EL1 interrupt to the TSP but does not expect it to
2402446137SSoby Mathew  * return execution. This statistic will be useful to distinguish between these
2502446137SSoby Mathew  * two models of synchronous S-EL1 interrupt handling. The 'elr_el3' parameter
2602446137SSoby Mathew  * contains the address of the instruction in normal world where this S-EL1
2702446137SSoby Mathew  * interrupt was generated.
286cf89021SAchin Gupta  ******************************************************************************/
2902446137SSoby Mathew void tsp_update_sync_sel1_intr_stats(uint32_t type, uint64_t elr_el3)
306cf89021SAchin Gupta {
31fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
326cf89021SAchin Gupta 
3302446137SSoby Mathew 	tsp_stats[linear_id].sync_sel1_intr_count++;
3402446137SSoby Mathew 	if (type == TSP_HANDLE_SEL1_INTR_AND_RETURN)
3502446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count++;
366cf89021SAchin Gupta 
376ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
386cf89021SAchin Gupta 	spin_lock(&console_lock);
390a2d5b43SMasahiro Yamada 	VERBOSE("TSP: cpu 0x%lx sync s-el1 interrupt request from 0x%llx\n",
40fd650ff6SSoby Mathew 		read_mpidr(), elr_el3);
4102446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d sync s-el1 interrupt requests,"
4202446137SSoby Mathew 		" %d sync s-el1 interrupt returns\n",
43fd650ff6SSoby Mathew 		read_mpidr(),
4402446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_count,
4502446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count);
466cf89021SAchin Gupta 	spin_unlock(&console_lock);
476ad2e461SDan Handley #endif
486cf89021SAchin Gupta }
496cf89021SAchin Gupta 
50404dba53SSoby Mathew /******************************************************************************
51404dba53SSoby Mathew  * This function is invoked when a non S-EL1 interrupt is received and causes
52404dba53SSoby Mathew  * the preemption of TSP. This function returns TSP_PREEMPTED and results
53404dba53SSoby Mathew  * in the control being handed over to EL3 for handling the interrupt.
54404dba53SSoby Mathew  *****************************************************************************/
55404dba53SSoby Mathew int32_t tsp_handle_preemption(void)
56404dba53SSoby Mathew {
57404dba53SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
58404dba53SSoby Mathew 
59404dba53SSoby Mathew 	tsp_stats[linear_id].preempt_intr_count++;
60404dba53SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
61404dba53SSoby Mathew 	spin_lock(&console_lock);
62404dba53SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d preempt interrupt requests\n",
63404dba53SSoby Mathew 		read_mpidr(), tsp_stats[linear_id].preempt_intr_count);
64404dba53SSoby Mathew 	spin_unlock(&console_lock);
65404dba53SSoby Mathew #endif
66404dba53SSoby Mathew 	return TSP_PREEMPTED;
67404dba53SSoby Mathew }
68404dba53SSoby Mathew 
696cf89021SAchin Gupta /*******************************************************************************
7002446137SSoby Mathew  * TSP interrupt handler is called as a part of both synchronous and
7102446137SSoby Mathew  * asynchronous handling of TSP interrupts. Currently the physical timer
7202446137SSoby Mathew  * interrupt is the only S-EL1 interrupt that this handler expects. It returns
7302446137SSoby Mathew  * 0 upon successfully handling the expected interrupt and all other
7402446137SSoby Mathew  * interrupts are treated as normal world or EL3 interrupts.
756cf89021SAchin Gupta  ******************************************************************************/
7602446137SSoby Mathew int32_t tsp_common_int_handler(void)
776cf89021SAchin Gupta {
78fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos(), id;
796cf89021SAchin Gupta 
806cf89021SAchin Gupta 	/*
816cf89021SAchin Gupta 	 * Get the highest priority pending interrupt id and see if it is the
826cf89021SAchin Gupta 	 * secure physical generic timer interrupt in which case, handle it.
836cf89021SAchin Gupta 	 * Otherwise throw this interrupt at the EL3 firmware.
84404dba53SSoby Mathew 	 *
85404dba53SSoby Mathew 	 * There is a small time window between reading the highest priority
86404dba53SSoby Mathew 	 * pending interrupt and acknowledging it during which another
87404dba53SSoby Mathew 	 * interrupt of higher priority could become the highest pending
88404dba53SSoby Mathew 	 * interrupt. This is not expected to happen currently for TSP.
896cf89021SAchin Gupta 	 */
909865ac15SDan Handley 	id = plat_ic_get_pending_interrupt_id();
916cf89021SAchin Gupta 
926cf89021SAchin Gupta 	/* TSP can only handle the secure physical timer interrupt */
935a06bb7eSDan Handley 	if (id != TSP_IRQ_SEC_PHY_TIMER)
94404dba53SSoby Mathew 		return tsp_handle_preemption();
956cf89021SAchin Gupta 
966cf89021SAchin Gupta 	/*
97404dba53SSoby Mathew 	 * Acknowledge and handle the secure timer interrupt. Also sanity check
98404dba53SSoby Mathew 	 * if it has been preempted by another interrupt through an assertion.
996cf89021SAchin Gupta 	 */
1009865ac15SDan Handley 	id = plat_ic_acknowledge_interrupt();
1015a06bb7eSDan Handley 	assert(id == TSP_IRQ_SEC_PHY_TIMER);
1026cf89021SAchin Gupta 	tsp_generic_timer_handler();
1039865ac15SDan Handley 	plat_ic_end_of_interrupt(id);
1046cf89021SAchin Gupta 
1056cf89021SAchin Gupta 	/* Update the statistics and print some messages */
10602446137SSoby Mathew 	tsp_stats[linear_id].sel1_intr_count++;
1076ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
1086cf89021SAchin Gupta 	spin_lock(&console_lock);
10902446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx handled S-EL1 interrupt %d\n",
110fd650ff6SSoby Mathew 	       read_mpidr(), id);
11102446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d S-EL1 requests\n",
11202446137SSoby Mathew 	     read_mpidr(), tsp_stats[linear_id].sel1_intr_count);
1136cf89021SAchin Gupta 	spin_unlock(&console_lock);
1146ad2e461SDan Handley #endif
1156cf89021SAchin Gupta 	return 0;
1166cf89021SAchin Gupta }
117