xref: /rk3399_ARM-atf/bl32/tsp/tsp_interrupt.c (revision 02446137a4e2a504706fb1f4059467643e2930a5)
16cf89021SAchin Gupta /*
21b70db06SDan Handley  * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
36cf89021SAchin Gupta  *
46cf89021SAchin Gupta  * Redistribution and use in source and binary forms, with or without
56cf89021SAchin Gupta  * modification, are permitted provided that the following conditions are met:
66cf89021SAchin Gupta  *
76cf89021SAchin Gupta  * Redistributions of source code must retain the above copyright notice, this
86cf89021SAchin Gupta  * list of conditions and the following disclaimer.
96cf89021SAchin Gupta  *
106cf89021SAchin Gupta  * Redistributions in binary form must reproduce the above copyright notice,
116cf89021SAchin Gupta  * this list of conditions and the following disclaimer in the documentation
126cf89021SAchin Gupta  * and/or other materials provided with the distribution.
136cf89021SAchin Gupta  *
146cf89021SAchin Gupta  * Neither the name of ARM nor the names of its contributors may be used
156cf89021SAchin Gupta  * to endorse or promote products derived from this software without specific
166cf89021SAchin Gupta  * prior written permission.
176cf89021SAchin Gupta  *
186cf89021SAchin Gupta  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
196cf89021SAchin Gupta  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206cf89021SAchin Gupta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
216cf89021SAchin Gupta  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
226cf89021SAchin Gupta  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
236cf89021SAchin Gupta  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
246cf89021SAchin Gupta  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
256cf89021SAchin Gupta  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
266cf89021SAchin Gupta  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
276cf89021SAchin Gupta  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
286cf89021SAchin Gupta  * POSSIBILITY OF SUCH DAMAGE.
296cf89021SAchin Gupta  */
306cf89021SAchin Gupta 
316cf89021SAchin Gupta #include <arch_helpers.h>
326cf89021SAchin Gupta #include <assert.h>
336cf89021SAchin Gupta #include <debug.h>
346cf89021SAchin Gupta #include <platform.h>
355f0cdb05SDan Handley #include <platform_def.h>
36da0af78aSDan Handley #include <tsp.h>
37da0af78aSDan Handley #include "tsp_private.h"
386cf89021SAchin Gupta 
396cf89021SAchin Gupta /*******************************************************************************
40*02446137SSoby Mathew  * This function updates the TSP statistics for S-EL1 interrupts handled
41*02446137SSoby Mathew  * synchronously i.e the ones that have been handed over by the TSPD. It also
42*02446137SSoby Mathew  * keeps count of the number of times control was passed back to the TSPD
43*02446137SSoby Mathew  * after handling the interrupt. In the future it will be possible that the
44*02446137SSoby Mathew  * TSPD hands over an S-EL1 interrupt to the TSP but does not expect it to
45*02446137SSoby Mathew  * return execution. This statistic will be useful to distinguish between these
46*02446137SSoby Mathew  * two models of synchronous S-EL1 interrupt handling. The 'elr_el3' parameter
47*02446137SSoby Mathew  * contains the address of the instruction in normal world where this S-EL1
48*02446137SSoby Mathew  * interrupt was generated.
496cf89021SAchin Gupta  ******************************************************************************/
50*02446137SSoby Mathew void tsp_update_sync_sel1_intr_stats(uint32_t type, uint64_t elr_el3)
516cf89021SAchin Gupta {
52fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
536cf89021SAchin Gupta 
54*02446137SSoby Mathew 	tsp_stats[linear_id].sync_sel1_intr_count++;
55*02446137SSoby Mathew 	if (type == TSP_HANDLE_SEL1_INTR_AND_RETURN)
56*02446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count++;
576cf89021SAchin Gupta 
586ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
596cf89021SAchin Gupta 	spin_lock(&console_lock);
60*02446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx sync s-el1 interrupt request from 0x%lx\n",
61fd650ff6SSoby Mathew 		read_mpidr(), elr_el3);
62*02446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d sync s-el1 interrupt requests,"
63*02446137SSoby Mathew 		" %d sync s-el1 interrupt returns\n",
64fd650ff6SSoby Mathew 		read_mpidr(),
65*02446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_count,
66*02446137SSoby Mathew 		tsp_stats[linear_id].sync_sel1_intr_ret_count);
676cf89021SAchin Gupta 	spin_unlock(&console_lock);
686ad2e461SDan Handley #endif
696cf89021SAchin Gupta }
706cf89021SAchin Gupta 
71404dba53SSoby Mathew /******************************************************************************
72404dba53SSoby Mathew  * This function is invoked when a non S-EL1 interrupt is received and causes
73404dba53SSoby Mathew  * the preemption of TSP. This function returns TSP_PREEMPTED and results
74404dba53SSoby Mathew  * in the control being handed over to EL3 for handling the interrupt.
75404dba53SSoby Mathew  *****************************************************************************/
76404dba53SSoby Mathew int32_t tsp_handle_preemption(void)
77404dba53SSoby Mathew {
78404dba53SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
79404dba53SSoby Mathew 
80404dba53SSoby Mathew 	tsp_stats[linear_id].preempt_intr_count++;
81404dba53SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
82404dba53SSoby Mathew 	spin_lock(&console_lock);
83404dba53SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d preempt interrupt requests\n",
84404dba53SSoby Mathew 		read_mpidr(), tsp_stats[linear_id].preempt_intr_count);
85404dba53SSoby Mathew 	spin_unlock(&console_lock);
86404dba53SSoby Mathew #endif
87404dba53SSoby Mathew 	return TSP_PREEMPTED;
88404dba53SSoby Mathew }
89404dba53SSoby Mathew 
906cf89021SAchin Gupta /*******************************************************************************
91*02446137SSoby Mathew  * TSP interrupt handler is called as a part of both synchronous and
92*02446137SSoby Mathew  * asynchronous handling of TSP interrupts. Currently the physical timer
93*02446137SSoby Mathew  * interrupt is the only S-EL1 interrupt that this handler expects. It returns
94*02446137SSoby Mathew  * 0 upon successfully handling the expected interrupt and all other
95*02446137SSoby Mathew  * interrupts are treated as normal world or EL3 interrupts.
966cf89021SAchin Gupta  ******************************************************************************/
97*02446137SSoby Mathew int32_t tsp_common_int_handler(void)
986cf89021SAchin Gupta {
99fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos(), id;
1006cf89021SAchin Gupta 
1016cf89021SAchin Gupta 	/*
1026cf89021SAchin Gupta 	 * Get the highest priority pending interrupt id and see if it is the
1036cf89021SAchin Gupta 	 * secure physical generic timer interrupt in which case, handle it.
1046cf89021SAchin Gupta 	 * Otherwise throw this interrupt at the EL3 firmware.
105404dba53SSoby Mathew 	 *
106404dba53SSoby Mathew 	 * There is a small time window between reading the highest priority
107404dba53SSoby Mathew 	 * pending interrupt and acknowledging it during which another
108404dba53SSoby Mathew 	 * interrupt of higher priority could become the highest pending
109404dba53SSoby Mathew 	 * interrupt. This is not expected to happen currently for TSP.
1106cf89021SAchin Gupta 	 */
1119865ac15SDan Handley 	id = plat_ic_get_pending_interrupt_id();
1126cf89021SAchin Gupta 
1136cf89021SAchin Gupta 	/* TSP can only handle the secure physical timer interrupt */
1145a06bb7eSDan Handley 	if (id != TSP_IRQ_SEC_PHY_TIMER)
115404dba53SSoby Mathew 		return tsp_handle_preemption();
1166cf89021SAchin Gupta 
1176cf89021SAchin Gupta 	/*
118404dba53SSoby Mathew 	 * Acknowledge and handle the secure timer interrupt. Also sanity check
119404dba53SSoby Mathew 	 * if it has been preempted by another interrupt through an assertion.
1206cf89021SAchin Gupta 	 */
1219865ac15SDan Handley 	id = plat_ic_acknowledge_interrupt();
1225a06bb7eSDan Handley 	assert(id == TSP_IRQ_SEC_PHY_TIMER);
1236cf89021SAchin Gupta 	tsp_generic_timer_handler();
1249865ac15SDan Handley 	plat_ic_end_of_interrupt(id);
1256cf89021SAchin Gupta 
1266cf89021SAchin Gupta 	/* Update the statistics and print some messages */
127*02446137SSoby Mathew 	tsp_stats[linear_id].sel1_intr_count++;
1286ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
1296cf89021SAchin Gupta 	spin_lock(&console_lock);
130*02446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx handled S-EL1 interrupt %d\n",
131fd650ff6SSoby Mathew 	       read_mpidr(), id);
132*02446137SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx: %d S-EL1 requests\n",
133*02446137SSoby Mathew 	     read_mpidr(), tsp_stats[linear_id].sel1_intr_count);
1346cf89021SAchin Gupta 	spin_unlock(&console_lock);
1356ad2e461SDan Handley #endif
1366cf89021SAchin Gupta 	return 0;
1376cf89021SAchin Gupta }
138