xref: /rk3399_ARM-atf/bl32/tsp/tsp_interrupt.c (revision 0a2d5b43c81ed6132761023bf43755f13122ddf0)
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 <arch_helpers.h>
86cf89021SAchin Gupta #include <assert.h>
96cf89021SAchin Gupta #include <debug.h>
106cf89021SAchin Gupta #include <platform.h>
115f0cdb05SDan Handley #include <platform_def.h>
12da0af78aSDan Handley #include <tsp.h>
13da0af78aSDan Handley #include "tsp_private.h"
146cf89021SAchin Gupta 
156cf89021SAchin Gupta /*******************************************************************************
1602446137SSoby Mathew  * This function updates the TSP statistics for S-EL1 interrupts handled
1702446137SSoby Mathew  * synchronously i.e the ones that have been handed over by the TSPD. It also
1802446137SSoby Mathew  * keeps count of the number of times control was passed back to the TSPD
1902446137SSoby Mathew  * after handling the interrupt. In the future it will be possible that the
2002446137SSoby Mathew  * TSPD hands over an S-EL1 interrupt to the TSP but does not expect it to
2102446137SSoby Mathew  * return execution. This statistic will be useful to distinguish between these
2202446137SSoby Mathew  * two models of synchronous S-EL1 interrupt handling. The 'elr_el3' parameter
2302446137SSoby Mathew  * contains the address of the instruction in normal world where this S-EL1
2402446137SSoby Mathew  * interrupt was generated.
256cf89021SAchin Gupta  ******************************************************************************/
2602446137SSoby Mathew void tsp_update_sync_sel1_intr_stats(uint32_t type, uint64_t elr_el3)
276cf89021SAchin Gupta {
28fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
296cf89021SAchin Gupta 
3002446137SSoby Mathew 	tsp_stats[linear_id].sync_sel1_intr_count++;
3102446137SSoby Mathew 	if (type == TSP_HANDLE_SEL1_INTR_AND_RETURN)
3202446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count++;
336cf89021SAchin Gupta 
346ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
356cf89021SAchin Gupta 	spin_lock(&console_lock);
36*0a2d5b43SMasahiro Yamada 	VERBOSE("TSP: cpu 0x%lx sync s-el1 interrupt request from 0x%llx\n",
37fd650ff6SSoby Mathew 		read_mpidr(), elr_el3);
3802446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d sync s-el1 interrupt requests,"
3902446137SSoby Mathew 		" %d sync s-el1 interrupt returns\n",
40fd650ff6SSoby Mathew 		read_mpidr(),
4102446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_count,
4202446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count);
436cf89021SAchin Gupta 	spin_unlock(&console_lock);
446ad2e461SDan Handley #endif
456cf89021SAchin Gupta }
466cf89021SAchin Gupta 
47404dba53SSoby Mathew /******************************************************************************
48404dba53SSoby Mathew  * This function is invoked when a non S-EL1 interrupt is received and causes
49404dba53SSoby Mathew  * the preemption of TSP. This function returns TSP_PREEMPTED and results
50404dba53SSoby Mathew  * in the control being handed over to EL3 for handling the interrupt.
51404dba53SSoby Mathew  *****************************************************************************/
52404dba53SSoby Mathew int32_t tsp_handle_preemption(void)
53404dba53SSoby Mathew {
54404dba53SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
55404dba53SSoby Mathew 
56404dba53SSoby Mathew 	tsp_stats[linear_id].preempt_intr_count++;
57404dba53SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
58404dba53SSoby Mathew 	spin_lock(&console_lock);
59404dba53SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d preempt interrupt requests\n",
60404dba53SSoby Mathew 		read_mpidr(), tsp_stats[linear_id].preempt_intr_count);
61404dba53SSoby Mathew 	spin_unlock(&console_lock);
62404dba53SSoby Mathew #endif
63404dba53SSoby Mathew 	return TSP_PREEMPTED;
64404dba53SSoby Mathew }
65404dba53SSoby Mathew 
666cf89021SAchin Gupta /*******************************************************************************
6702446137SSoby Mathew  * TSP interrupt handler is called as a part of both synchronous and
6802446137SSoby Mathew  * asynchronous handling of TSP interrupts. Currently the physical timer
6902446137SSoby Mathew  * interrupt is the only S-EL1 interrupt that this handler expects. It returns
7002446137SSoby Mathew  * 0 upon successfully handling the expected interrupt and all other
7102446137SSoby Mathew  * interrupts are treated as normal world or EL3 interrupts.
726cf89021SAchin Gupta  ******************************************************************************/
7302446137SSoby Mathew int32_t tsp_common_int_handler(void)
746cf89021SAchin Gupta {
75fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos(), id;
766cf89021SAchin Gupta 
776cf89021SAchin Gupta 	/*
786cf89021SAchin Gupta 	 * Get the highest priority pending interrupt id and see if it is the
796cf89021SAchin Gupta 	 * secure physical generic timer interrupt in which case, handle it.
806cf89021SAchin Gupta 	 * Otherwise throw this interrupt at the EL3 firmware.
81404dba53SSoby Mathew 	 *
82404dba53SSoby Mathew 	 * There is a small time window between reading the highest priority
83404dba53SSoby Mathew 	 * pending interrupt and acknowledging it during which another
84404dba53SSoby Mathew 	 * interrupt of higher priority could become the highest pending
85404dba53SSoby Mathew 	 * interrupt. This is not expected to happen currently for TSP.
866cf89021SAchin Gupta 	 */
879865ac15SDan Handley 	id = plat_ic_get_pending_interrupt_id();
886cf89021SAchin Gupta 
896cf89021SAchin Gupta 	/* TSP can only handle the secure physical timer interrupt */
905a06bb7eSDan Handley 	if (id != TSP_IRQ_SEC_PHY_TIMER)
91404dba53SSoby Mathew 		return tsp_handle_preemption();
926cf89021SAchin Gupta 
936cf89021SAchin Gupta 	/*
94404dba53SSoby Mathew 	 * Acknowledge and handle the secure timer interrupt. Also sanity check
95404dba53SSoby Mathew 	 * if it has been preempted by another interrupt through an assertion.
966cf89021SAchin Gupta 	 */
979865ac15SDan Handley 	id = plat_ic_acknowledge_interrupt();
985a06bb7eSDan Handley 	assert(id == TSP_IRQ_SEC_PHY_TIMER);
996cf89021SAchin Gupta 	tsp_generic_timer_handler();
1009865ac15SDan Handley 	plat_ic_end_of_interrupt(id);
1016cf89021SAchin Gupta 
1026cf89021SAchin Gupta 	/* Update the statistics and print some messages */
10302446137SSoby Mathew 	tsp_stats[linear_id].sel1_intr_count++;
1046ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
1056cf89021SAchin Gupta 	spin_lock(&console_lock);
10602446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx handled S-EL1 interrupt %d\n",
107fd650ff6SSoby Mathew 	       read_mpidr(), id);
10802446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d S-EL1 requests\n",
10902446137SSoby Mathew 	     read_mpidr(), tsp_stats[linear_id].sel1_intr_count);
1106cf89021SAchin Gupta 	spin_unlock(&console_lock);
1116ad2e461SDan Handley #endif
1126cf89021SAchin Gupta 	return 0;
1136cf89021SAchin Gupta }
114