xref: /rk3399_ARM-atf/bl32/tsp/tsp_interrupt.c (revision fd650ff61b80a2155002def233ffddb439e3c071)
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 <gic_v2.h>
356cf89021SAchin Gupta #include <platform.h>
365f0cdb05SDan Handley #include <platform_def.h>
37da0af78aSDan Handley #include <tsp.h>
38da0af78aSDan Handley #include "tsp_private.h"
396cf89021SAchin Gupta 
406cf89021SAchin Gupta /*******************************************************************************
416cf89021SAchin Gupta  * This function updates the TSP statistics for FIQs handled synchronously i.e
426cf89021SAchin Gupta  * the ones that have been handed over by the TSPD. It also keeps count of the
436cf89021SAchin Gupta  * number of times control was passed back to the TSPD after handling an FIQ.
446cf89021SAchin Gupta  * In the future it will be possible that the TSPD hands over an FIQ to the TSP
456cf89021SAchin Gupta  * but does not expect it to return execution. This statistic will be useful to
466cf89021SAchin Gupta  * distinguish between these two models of synchronous FIQ handling.
476cf89021SAchin Gupta  * The 'elr_el3' parameter contains the address of the instruction in normal
486cf89021SAchin Gupta  * world where this FIQ was generated.
496cf89021SAchin Gupta  ******************************************************************************/
506cf89021SAchin Gupta void tsp_update_sync_fiq_stats(uint32_t type, uint64_t elr_el3)
516cf89021SAchin Gupta {
52*fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
536cf89021SAchin Gupta 
546cf89021SAchin Gupta 	tsp_stats[linear_id].sync_fiq_count++;
556cf89021SAchin Gupta 	if (type == TSP_HANDLE_FIQ_AND_RETURN)
566cf89021SAchin Gupta 		tsp_stats[linear_id].sync_fiq_ret_count++;
576cf89021SAchin Gupta 
586ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
596cf89021SAchin Gupta 	spin_lock(&console_lock);
601b70db06SDan Handley 	VERBOSE("TSP: cpu 0x%lx sync fiq request from 0x%lx\n",
61*fd650ff6SSoby Mathew 		read_mpidr(), elr_el3);
621b70db06SDan Handley 	VERBOSE("TSP: cpu 0x%lx: %d sync fiq requests, %d sync fiq returns\n",
63*fd650ff6SSoby Mathew 		read_mpidr(),
646cf89021SAchin Gupta 		tsp_stats[linear_id].sync_fiq_count,
656cf89021SAchin Gupta 		tsp_stats[linear_id].sync_fiq_ret_count);
666cf89021SAchin Gupta 	spin_unlock(&console_lock);
676ad2e461SDan Handley #endif
686cf89021SAchin Gupta }
696cf89021SAchin Gupta 
706cf89021SAchin Gupta /*******************************************************************************
716cf89021SAchin Gupta  * TSP FIQ handler called as a part of both synchronous and asynchronous
726cf89021SAchin Gupta  * handling of FIQ interrupts. It returns 0 upon successfully handling a S-EL1
736cf89021SAchin Gupta  * FIQ and treats all other FIQs as EL3 interrupts. It assumes that the GIC
746cf89021SAchin Gupta  * architecture version in v2.0 and the secure physical timer interrupt is the
756cf89021SAchin Gupta  * only S-EL1 interrupt that it needs to handle.
766cf89021SAchin Gupta  ******************************************************************************/
774f2104ffSJuan Castillo int32_t tsp_fiq_handler(void)
786cf89021SAchin Gupta {
79*fd650ff6SSoby 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.
856cf89021SAchin Gupta 	 */
869865ac15SDan Handley 	id = plat_ic_get_pending_interrupt_id();
876cf89021SAchin Gupta 
886cf89021SAchin Gupta 	/* TSP can only handle the secure physical timer interrupt */
895a06bb7eSDan Handley 	if (id != TSP_IRQ_SEC_PHY_TIMER)
906cf89021SAchin Gupta 		return TSP_EL3_FIQ;
916cf89021SAchin Gupta 
926cf89021SAchin Gupta 	/*
936cf89021SAchin Gupta 	 * Handle the interrupt. Also sanity check if it has been preempted by
946cf89021SAchin Gupta 	 * another secure interrupt through an assertion.
956cf89021SAchin Gupta 	 */
969865ac15SDan Handley 	id = plat_ic_acknowledge_interrupt();
975a06bb7eSDan Handley 	assert(id == TSP_IRQ_SEC_PHY_TIMER);
986cf89021SAchin Gupta 	tsp_generic_timer_handler();
999865ac15SDan Handley 	plat_ic_end_of_interrupt(id);
1006cf89021SAchin Gupta 
1016cf89021SAchin Gupta 	/* Update the statistics and print some messages */
1026cf89021SAchin Gupta 	tsp_stats[linear_id].fiq_count++;
1036ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
1046cf89021SAchin Gupta 	spin_lock(&console_lock);
1051b70db06SDan Handley 	VERBOSE("TSP: cpu 0x%lx handled fiq %d\n",
106*fd650ff6SSoby Mathew 	       read_mpidr(), id);
1071b70db06SDan Handley 	VERBOSE("TSP: cpu 0x%lx: %d fiq requests\n",
108*fd650ff6SSoby Mathew 	     read_mpidr(), tsp_stats[linear_id].fiq_count);
1096cf89021SAchin Gupta 	spin_unlock(&console_lock);
1106ad2e461SDan Handley #endif
1116cf89021SAchin Gupta 	return 0;
1126cf89021SAchin Gupta }
113239b04faSSoby Mathew 
1144f2104ffSJuan Castillo int32_t tsp_irq_received(void)
115239b04faSSoby Mathew {
116*fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
117239b04faSSoby Mathew 
118239b04faSSoby Mathew 	tsp_stats[linear_id].irq_count++;
1196ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
120239b04faSSoby Mathew 	spin_lock(&console_lock);
121*fd650ff6SSoby Mathew 	VERBOSE("TSP: cpu 0x%lx received irq\n", read_mpidr());
1221b70db06SDan Handley 	VERBOSE("TSP: cpu 0x%lx: %d irq requests\n",
123*fd650ff6SSoby Mathew 		read_mpidr(), tsp_stats[linear_id].irq_count);
124239b04faSSoby Mathew 	spin_unlock(&console_lock);
1256ad2e461SDan Handley #endif
126239b04faSSoby Mathew 	return TSP_PREEMPTED;
127239b04faSSoby Mathew }
128