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 { 52fd650ff6SSoby 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", 61fd650ff6SSoby Mathew read_mpidr(), elr_el3); 621b70db06SDan Handley VERBOSE("TSP: cpu 0x%lx: %d sync fiq requests, %d sync fiq returns\n", 63fd650ff6SSoby 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 70*404dba53SSoby Mathew /****************************************************************************** 71*404dba53SSoby Mathew * This function is invoked when a non S-EL1 interrupt is received and causes 72*404dba53SSoby Mathew * the preemption of TSP. This function returns TSP_PREEMPTED and results 73*404dba53SSoby Mathew * in the control being handed over to EL3 for handling the interrupt. 74*404dba53SSoby Mathew *****************************************************************************/ 75*404dba53SSoby Mathew int32_t tsp_handle_preemption(void) 76*404dba53SSoby Mathew { 77*404dba53SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 78*404dba53SSoby Mathew 79*404dba53SSoby Mathew tsp_stats[linear_id].preempt_intr_count++; 80*404dba53SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 81*404dba53SSoby Mathew spin_lock(&console_lock); 82*404dba53SSoby Mathew VERBOSE("TSP: cpu 0x%lx: %d preempt interrupt requests\n", 83*404dba53SSoby Mathew read_mpidr(), tsp_stats[linear_id].preempt_intr_count); 84*404dba53SSoby Mathew spin_unlock(&console_lock); 85*404dba53SSoby Mathew #endif 86*404dba53SSoby Mathew return TSP_PREEMPTED; 87*404dba53SSoby Mathew } 88*404dba53SSoby Mathew 896cf89021SAchin Gupta /******************************************************************************* 906cf89021SAchin Gupta * TSP FIQ handler called as a part of both synchronous and asynchronous 916cf89021SAchin Gupta * handling of FIQ interrupts. It returns 0 upon successfully handling a S-EL1 926cf89021SAchin Gupta * FIQ and treats all other FIQs as EL3 interrupts. It assumes that the GIC 936cf89021SAchin Gupta * architecture version in v2.0 and the secure physical timer interrupt is the 946cf89021SAchin Gupta * only S-EL1 interrupt that it needs to handle. 956cf89021SAchin Gupta ******************************************************************************/ 964f2104ffSJuan Castillo int32_t tsp_fiq_handler(void) 976cf89021SAchin Gupta { 98fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), id; 996cf89021SAchin Gupta 1006cf89021SAchin Gupta /* 1016cf89021SAchin Gupta * Get the highest priority pending interrupt id and see if it is the 1026cf89021SAchin Gupta * secure physical generic timer interrupt in which case, handle it. 1036cf89021SAchin Gupta * Otherwise throw this interrupt at the EL3 firmware. 104*404dba53SSoby Mathew * 105*404dba53SSoby Mathew * There is a small time window between reading the highest priority 106*404dba53SSoby Mathew * pending interrupt and acknowledging it during which another 107*404dba53SSoby Mathew * interrupt of higher priority could become the highest pending 108*404dba53SSoby Mathew * interrupt. This is not expected to happen currently for TSP. 1096cf89021SAchin Gupta */ 1109865ac15SDan Handley id = plat_ic_get_pending_interrupt_id(); 1116cf89021SAchin Gupta 1126cf89021SAchin Gupta /* TSP can only handle the secure physical timer interrupt */ 1135a06bb7eSDan Handley if (id != TSP_IRQ_SEC_PHY_TIMER) 114*404dba53SSoby Mathew return tsp_handle_preemption(); 1156cf89021SAchin Gupta 1166cf89021SAchin Gupta /* 117*404dba53SSoby Mathew * Acknowledge and handle the secure timer interrupt. Also sanity check 118*404dba53SSoby Mathew * if it has been preempted by another interrupt through an assertion. 1196cf89021SAchin Gupta */ 1209865ac15SDan Handley id = plat_ic_acknowledge_interrupt(); 1215a06bb7eSDan Handley assert(id == TSP_IRQ_SEC_PHY_TIMER); 1226cf89021SAchin Gupta tsp_generic_timer_handler(); 1239865ac15SDan Handley plat_ic_end_of_interrupt(id); 1246cf89021SAchin Gupta 1256cf89021SAchin Gupta /* Update the statistics and print some messages */ 1266cf89021SAchin Gupta tsp_stats[linear_id].fiq_count++; 1276ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 1286cf89021SAchin Gupta spin_lock(&console_lock); 1291b70db06SDan Handley VERBOSE("TSP: cpu 0x%lx handled fiq %d\n", 130fd650ff6SSoby Mathew read_mpidr(), id); 1311b70db06SDan Handley VERBOSE("TSP: cpu 0x%lx: %d fiq requests\n", 132fd650ff6SSoby Mathew read_mpidr(), tsp_stats[linear_id].fiq_count); 1336cf89021SAchin Gupta spin_unlock(&console_lock); 1346ad2e461SDan Handley #endif 1356cf89021SAchin Gupta return 0; 1366cf89021SAchin Gupta } 137