xref: /rk3399_ARM-atf/bl32/tsp/tsp_interrupt.c (revision 4ef449c15a4055d92632cb7e72267f525a7e2fca)
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*4ef449c1SManish Pandey #include <inttypes.h>
909d40e0eSAntonio Nino Diaz 
105f0cdb05SDan Handley #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz 
1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h>
1409d40e0eSAntonio Nino Diaz #include <common/debug.h>
1509d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1609d40e0eSAntonio Nino Diaz 
17da0af78aSDan Handley #include "tsp_private.h"
186cf89021SAchin Gupta 
196cf89021SAchin Gupta /*******************************************************************************
2002446137SSoby Mathew  * This function updates the TSP statistics for S-EL1 interrupts handled
2102446137SSoby Mathew  * synchronously i.e the ones that have been handed over by the TSPD. It also
2202446137SSoby Mathew  * keeps count of the number of times control was passed back to the TSPD
2302446137SSoby Mathew  * after handling the interrupt. In the future it will be possible that the
2402446137SSoby Mathew  * TSPD hands over an S-EL1 interrupt to the TSP but does not expect it to
2502446137SSoby Mathew  * return execution. This statistic will be useful to distinguish between these
2602446137SSoby Mathew  * two models of synchronous S-EL1 interrupt handling. The 'elr_el3' parameter
2702446137SSoby Mathew  * contains the address of the instruction in normal world where this S-EL1
2802446137SSoby Mathew  * interrupt was generated.
296cf89021SAchin Gupta  ******************************************************************************/
3002446137SSoby Mathew void tsp_update_sync_sel1_intr_stats(uint32_t type, uint64_t elr_el3)
316cf89021SAchin Gupta {
32fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
336cf89021SAchin Gupta 
3402446137SSoby Mathew 	tsp_stats[linear_id].sync_sel1_intr_count++;
3502446137SSoby Mathew 	if (type == TSP_HANDLE_SEL1_INTR_AND_RETURN)
3602446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count++;
376cf89021SAchin Gupta 
386ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
396cf89021SAchin Gupta 	spin_lock(&console_lock);
40*4ef449c1SManish Pandey 	VERBOSE("TSP: cpu 0x%lx sync s-el1 interrupt request from 0x%" PRIx64 "\n",
41fd650ff6SSoby Mathew 		read_mpidr(), elr_el3);
4202446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d sync s-el1 interrupt requests,"
4302446137SSoby Mathew 		" %d sync s-el1 interrupt returns\n",
44fd650ff6SSoby Mathew 		read_mpidr(),
4502446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_count,
4602446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count);
476cf89021SAchin Gupta 	spin_unlock(&console_lock);
486ad2e461SDan Handley #endif
496cf89021SAchin Gupta }
506cf89021SAchin Gupta 
51404dba53SSoby Mathew /******************************************************************************
52404dba53SSoby Mathew  * This function is invoked when a non S-EL1 interrupt is received and causes
53404dba53SSoby Mathew  * the preemption of TSP. This function returns TSP_PREEMPTED and results
54404dba53SSoby Mathew  * in the control being handed over to EL3 for handling the interrupt.
55404dba53SSoby Mathew  *****************************************************************************/
56404dba53SSoby Mathew int32_t tsp_handle_preemption(void)
57404dba53SSoby Mathew {
58404dba53SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
59404dba53SSoby Mathew 
60404dba53SSoby Mathew 	tsp_stats[linear_id].preempt_intr_count++;
61404dba53SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
62404dba53SSoby Mathew 	spin_lock(&console_lock);
63404dba53SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d preempt interrupt requests\n",
64404dba53SSoby Mathew 		read_mpidr(), tsp_stats[linear_id].preempt_intr_count);
65404dba53SSoby Mathew 	spin_unlock(&console_lock);
66404dba53SSoby Mathew #endif
67404dba53SSoby Mathew 	return TSP_PREEMPTED;
68404dba53SSoby Mathew }
69404dba53SSoby Mathew 
706cf89021SAchin Gupta /*******************************************************************************
7102446137SSoby Mathew  * TSP interrupt handler is called as a part of both synchronous and
7202446137SSoby Mathew  * asynchronous handling of TSP interrupts. Currently the physical timer
7302446137SSoby Mathew  * interrupt is the only S-EL1 interrupt that this handler expects. It returns
7402446137SSoby Mathew  * 0 upon successfully handling the expected interrupt and all other
7502446137SSoby Mathew  * interrupts are treated as normal world or EL3 interrupts.
766cf89021SAchin Gupta  ******************************************************************************/
7702446137SSoby Mathew int32_t tsp_common_int_handler(void)
786cf89021SAchin Gupta {
79fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos(), id;
806cf89021SAchin Gupta 
816cf89021SAchin Gupta 	/*
826cf89021SAchin Gupta 	 * Get the highest priority pending interrupt id and see if it is the
836cf89021SAchin Gupta 	 * secure physical generic timer interrupt in which case, handle it.
846cf89021SAchin Gupta 	 * Otherwise throw this interrupt at the EL3 firmware.
85404dba53SSoby Mathew 	 *
86404dba53SSoby Mathew 	 * There is a small time window between reading the highest priority
87404dba53SSoby Mathew 	 * pending interrupt and acknowledging it during which another
88404dba53SSoby Mathew 	 * interrupt of higher priority could become the highest pending
89404dba53SSoby Mathew 	 * interrupt. This is not expected to happen currently for TSP.
906cf89021SAchin Gupta 	 */
919865ac15SDan Handley 	id = plat_ic_get_pending_interrupt_id();
926cf89021SAchin Gupta 
936cf89021SAchin Gupta 	/* TSP can only handle the secure physical timer interrupt */
945a06bb7eSDan Handley 	if (id != TSP_IRQ_SEC_PHY_TIMER)
95404dba53SSoby Mathew 		return tsp_handle_preemption();
966cf89021SAchin Gupta 
976cf89021SAchin Gupta 	/*
98404dba53SSoby Mathew 	 * Acknowledge and handle the secure timer interrupt. Also sanity check
99404dba53SSoby Mathew 	 * if it has been preempted by another interrupt through an assertion.
1006cf89021SAchin Gupta 	 */
1019865ac15SDan Handley 	id = plat_ic_acknowledge_interrupt();
1025a06bb7eSDan Handley 	assert(id == TSP_IRQ_SEC_PHY_TIMER);
1036cf89021SAchin Gupta 	tsp_generic_timer_handler();
1049865ac15SDan Handley 	plat_ic_end_of_interrupt(id);
1056cf89021SAchin Gupta 
1066cf89021SAchin Gupta 	/* Update the statistics and print some messages */
10702446137SSoby Mathew 	tsp_stats[linear_id].sel1_intr_count++;
1086ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
1096cf89021SAchin Gupta 	spin_lock(&console_lock);
11002446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx handled S-EL1 interrupt %d\n",
111fd650ff6SSoby Mathew 	       read_mpidr(), id);
11202446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d S-EL1 requests\n",
11302446137SSoby Mathew 	     read_mpidr(), tsp_stats[linear_id].sel1_intr_count);
1146cf89021SAchin Gupta 	spin_unlock(&console_lock);
1156ad2e461SDan Handley #endif
1166cf89021SAchin Gupta 	return 0;
1176cf89021SAchin Gupta }
118