16cf89021SAchin Gupta /*
2*3b06438dSYann Gautier * Copyright (c) 2014-2025, 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>
84ef449c1SManish 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 ******************************************************************************/
tsp_update_sync_sel1_intr_stats(uint32_t type,uint64_t elr_el3)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
384ef449c1SManish Pandey VERBOSE("TSP: cpu 0x%lx sync s-el1 interrupt request from 0x%" PRIx64 "\n",
39fd650ff6SSoby Mathew read_mpidr(), elr_el3);
40*3b06438dSYann Gautier VERBOSE("TSP: cpu 0x%lx: %u sync s-el1 interrupt requests,"
41*3b06438dSYann Gautier " %u sync s-el1 interrupt returns\n",
42fd650ff6SSoby Mathew read_mpidr(),
4302446137SSoby Mathew tsp_stats[linear_id].sync_sel1_intr_count,
4402446137SSoby Mathew tsp_stats[linear_id].sync_sel1_intr_ret_count);
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 *****************************************************************************/
tsp_handle_preemption(void)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++;
57*3b06438dSYann Gautier VERBOSE("TSP: cpu 0x%lx: %u preempt interrupt requests\n",
58404dba53SSoby Mathew read_mpidr(), tsp_stats[linear_id].preempt_intr_count);
59404dba53SSoby Mathew return TSP_PREEMPTED;
60404dba53SSoby Mathew }
61404dba53SSoby Mathew
626cf89021SAchin Gupta /*******************************************************************************
6302446137SSoby Mathew * TSP interrupt handler is called as a part of both synchronous and
6402446137SSoby Mathew * asynchronous handling of TSP interrupts. Currently the physical timer
6502446137SSoby Mathew * interrupt is the only S-EL1 interrupt that this handler expects. It returns
6602446137SSoby Mathew * 0 upon successfully handling the expected interrupt and all other
6702446137SSoby Mathew * interrupts are treated as normal world or EL3 interrupts.
686cf89021SAchin Gupta ******************************************************************************/
tsp_common_int_handler(void)6902446137SSoby Mathew int32_t tsp_common_int_handler(void)
706cf89021SAchin Gupta {
71fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), id;
726cf89021SAchin Gupta
736cf89021SAchin Gupta /*
746cf89021SAchin Gupta * Get the highest priority pending interrupt id and see if it is the
756cf89021SAchin Gupta * secure physical generic timer interrupt in which case, handle it.
766cf89021SAchin Gupta * Otherwise throw this interrupt at the EL3 firmware.
77404dba53SSoby Mathew *
78404dba53SSoby Mathew * There is a small time window between reading the highest priority
79404dba53SSoby Mathew * pending interrupt and acknowledging it during which another
80404dba53SSoby Mathew * interrupt of higher priority could become the highest pending
81404dba53SSoby Mathew * interrupt. This is not expected to happen currently for TSP.
826cf89021SAchin Gupta */
839865ac15SDan Handley id = plat_ic_get_pending_interrupt_id();
846cf89021SAchin Gupta
856cf89021SAchin Gupta /* TSP can only handle the secure physical timer interrupt */
864a8bfdb9SAchin Gupta if (id != TSP_IRQ_SEC_PHY_TIMER) {
874a8bfdb9SAchin Gupta #if SPMC_AT_EL3
884a8bfdb9SAchin Gupta /*
894a8bfdb9SAchin Gupta * With the EL3 FF-A SPMC we expect only Timer secure interrupt to fire in
904a8bfdb9SAchin Gupta * the TSP, so panic if any other interrupt does.
914a8bfdb9SAchin Gupta */
924a8bfdb9SAchin Gupta ERROR("Unexpected interrupt id %u\n", id);
934a8bfdb9SAchin Gupta panic();
944a8bfdb9SAchin Gupta #else
95404dba53SSoby Mathew return tsp_handle_preemption();
964a8bfdb9SAchin Gupta #endif
974a8bfdb9SAchin Gupta }
986cf89021SAchin Gupta
996cf89021SAchin Gupta /*
100404dba53SSoby Mathew * Acknowledge and handle the secure timer interrupt. Also sanity check
101404dba53SSoby Mathew * if it has been preempted by another interrupt through an assertion.
1026cf89021SAchin Gupta */
1039865ac15SDan Handley id = plat_ic_acknowledge_interrupt();
1045a06bb7eSDan Handley assert(id == TSP_IRQ_SEC_PHY_TIMER);
1056cf89021SAchin Gupta tsp_generic_timer_handler();
1069865ac15SDan Handley plat_ic_end_of_interrupt(id);
1076cf89021SAchin Gupta
1086cf89021SAchin Gupta /* Update the statistics and print some messages */
10902446137SSoby Mathew tsp_stats[linear_id].sel1_intr_count++;
110*3b06438dSYann Gautier VERBOSE("TSP: cpu 0x%lx handled S-EL1 interrupt %u\n",
111fd650ff6SSoby Mathew read_mpidr(), id);
112*3b06438dSYann Gautier VERBOSE("TSP: cpu 0x%lx: %u S-EL1 requests\n",
11302446137SSoby Mathew read_mpidr(), tsp_stats[linear_id].sel1_intr_count);
1146cf89021SAchin Gupta return 0;
1156cf89021SAchin Gupta }
116