xref: /rk3399_ARM-atf/bl32/tsp/tsp_interrupt.c (revision 9865ac15765f260069047c0e7c56623eb1a70b9a)
16cf89021SAchin Gupta /*
26cf89021SAchin Gupta  * Copyright (c) 2014, 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 <tsp.h>
366cf89021SAchin Gupta #include <platform.h>
375f0cdb05SDan Handley #include <platform_def.h>
386cf89021SAchin Gupta 
396cf89021SAchin Gupta /*******************************************************************************
406cf89021SAchin Gupta  * This function updates the TSP statistics for FIQs handled synchronously i.e
416cf89021SAchin Gupta  * the ones that have been handed over by the TSPD. It also keeps count of the
426cf89021SAchin Gupta  * number of times control was passed back to the TSPD after handling an FIQ.
436cf89021SAchin Gupta  * In the future it will be possible that the TSPD hands over an FIQ to the TSP
446cf89021SAchin Gupta  * but does not expect it to return execution. This statistic will be useful to
456cf89021SAchin Gupta  * distinguish between these two models of synchronous FIQ handling.
466cf89021SAchin Gupta  * The 'elr_el3' parameter contains the address of the instruction in normal
476cf89021SAchin Gupta  * world where this FIQ was generated.
486cf89021SAchin Gupta  ******************************************************************************/
496cf89021SAchin Gupta void tsp_update_sync_fiq_stats(uint32_t type, uint64_t elr_el3)
506cf89021SAchin Gupta {
516cf89021SAchin Gupta 	uint64_t mpidr = read_mpidr();
526cf89021SAchin Gupta 	uint32_t linear_id = platform_get_core_pos(mpidr);
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 
586cf89021SAchin Gupta 	spin_lock(&console_lock);
596cf89021SAchin Gupta 	printf("TSP: cpu 0x%x sync fiq request from 0x%llx \n\r",
606cf89021SAchin Gupta 	       mpidr, elr_el3);
616cf89021SAchin Gupta 	INFO("cpu 0x%x: %d sync fiq requests, %d sync fiq returns\n",
626cf89021SAchin Gupta 	     mpidr,
636cf89021SAchin Gupta 	     tsp_stats[linear_id].sync_fiq_count,
646cf89021SAchin Gupta 	     tsp_stats[linear_id].sync_fiq_ret_count);
656cf89021SAchin Gupta 	spin_unlock(&console_lock);
666cf89021SAchin Gupta }
676cf89021SAchin Gupta 
686cf89021SAchin Gupta /*******************************************************************************
696cf89021SAchin Gupta  * TSP FIQ handler called as a part of both synchronous and asynchronous
706cf89021SAchin Gupta  * handling of FIQ interrupts. It returns 0 upon successfully handling a S-EL1
716cf89021SAchin Gupta  * FIQ and treats all other FIQs as EL3 interrupts. It assumes that the GIC
726cf89021SAchin Gupta  * architecture version in v2.0 and the secure physical timer interrupt is the
736cf89021SAchin Gupta  * only S-EL1 interrupt that it needs to handle.
746cf89021SAchin Gupta  ******************************************************************************/
756cf89021SAchin Gupta int32_t tsp_fiq_handler()
766cf89021SAchin Gupta {
776cf89021SAchin Gupta 	uint64_t mpidr = read_mpidr();
786cf89021SAchin Gupta 	uint32_t linear_id = platform_get_core_pos(mpidr), 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.
846cf89021SAchin Gupta 	 */
85*9865ac15SDan Handley 	id = plat_ic_get_pending_interrupt_id();
866cf89021SAchin Gupta 
876cf89021SAchin Gupta 	/* TSP can only handle the secure physical timer interrupt */
886cf89021SAchin Gupta 	if (id != IRQ_SEC_PHY_TIMER)
896cf89021SAchin Gupta 		return TSP_EL3_FIQ;
906cf89021SAchin Gupta 
916cf89021SAchin Gupta 	/*
926cf89021SAchin Gupta 	 * Handle the interrupt. Also sanity check if it has been preempted by
936cf89021SAchin Gupta 	 * another secure interrupt through an assertion.
946cf89021SAchin Gupta 	 */
95*9865ac15SDan Handley 	id = plat_ic_acknowledge_interrupt();
966cf89021SAchin Gupta 	assert(id == IRQ_SEC_PHY_TIMER);
976cf89021SAchin Gupta 	tsp_generic_timer_handler();
98*9865ac15SDan Handley 	plat_ic_end_of_interrupt(id);
996cf89021SAchin Gupta 
1006cf89021SAchin Gupta 	/* Update the statistics and print some messages */
1016cf89021SAchin Gupta 	tsp_stats[linear_id].fiq_count++;
1026cf89021SAchin Gupta 	spin_lock(&console_lock);
1036cf89021SAchin Gupta 	printf("TSP: cpu 0x%x handled fiq %d \n\r",
1046cf89021SAchin Gupta 	       mpidr, id);
1056cf89021SAchin Gupta 	INFO("cpu 0x%x: %d fiq requests \n",
1066cf89021SAchin Gupta 	     mpidr, tsp_stats[linear_id].fiq_count);
1076cf89021SAchin Gupta 	spin_unlock(&console_lock);
1086cf89021SAchin Gupta 
1096cf89021SAchin Gupta 	return 0;
1106cf89021SAchin Gupta }
111239b04faSSoby Mathew 
112239b04faSSoby Mathew int32_t tsp_irq_received()
113239b04faSSoby Mathew {
114239b04faSSoby Mathew 	uint64_t mpidr = read_mpidr();
115239b04faSSoby Mathew 	uint32_t linear_id = platform_get_core_pos(mpidr);
116239b04faSSoby Mathew 
117239b04faSSoby Mathew 	tsp_stats[linear_id].irq_count++;
118239b04faSSoby Mathew 	spin_lock(&console_lock);
119239b04faSSoby Mathew 	printf("TSP: cpu 0x%x received irq\n\r", mpidr);
120239b04faSSoby Mathew 	INFO("cpu 0x%x: %d irq requests \n",
121239b04faSSoby Mathew 	     mpidr, tsp_stats[linear_id].irq_count);
122239b04faSSoby Mathew 	spin_unlock(&console_lock);
123239b04faSSoby Mathew 
124239b04faSSoby Mathew 	return TSP_PREEMPTED;
125239b04faSSoby Mathew }
126