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 <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 { 526cf89021SAchin Gupta uint64_t mpidr = read_mpidr(); 536cf89021SAchin Gupta uint32_t linear_id = platform_get_core_pos(mpidr); 546cf89021SAchin Gupta 556cf89021SAchin Gupta tsp_stats[linear_id].sync_fiq_count++; 566cf89021SAchin Gupta if (type == TSP_HANDLE_FIQ_AND_RETURN) 576cf89021SAchin Gupta tsp_stats[linear_id].sync_fiq_ret_count++; 586cf89021SAchin Gupta 596ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 606cf89021SAchin Gupta spin_lock(&console_lock); 616ad2e461SDan Handley VERBOSE("TSP: cpu 0x%x sync fiq request from 0x%llx\n", 626cf89021SAchin Gupta mpidr, elr_el3); 636ad2e461SDan Handley VERBOSE("TSP: cpu 0x%x: %d sync fiq requests, %d sync fiq returns\n", 646cf89021SAchin Gupta mpidr, 656cf89021SAchin Gupta tsp_stats[linear_id].sync_fiq_count, 666cf89021SAchin Gupta tsp_stats[linear_id].sync_fiq_ret_count); 676cf89021SAchin Gupta spin_unlock(&console_lock); 686ad2e461SDan Handley #endif 696cf89021SAchin Gupta } 706cf89021SAchin Gupta 716cf89021SAchin Gupta /******************************************************************************* 726cf89021SAchin Gupta * TSP FIQ handler called as a part of both synchronous and asynchronous 736cf89021SAchin Gupta * handling of FIQ interrupts. It returns 0 upon successfully handling a S-EL1 746cf89021SAchin Gupta * FIQ and treats all other FIQs as EL3 interrupts. It assumes that the GIC 756cf89021SAchin Gupta * architecture version in v2.0 and the secure physical timer interrupt is the 766cf89021SAchin Gupta * only S-EL1 interrupt that it needs to handle. 776cf89021SAchin Gupta ******************************************************************************/ 784f2104ffSJuan Castillo int32_t tsp_fiq_handler(void) 796cf89021SAchin Gupta { 806cf89021SAchin Gupta uint64_t mpidr = read_mpidr(); 816cf89021SAchin Gupta uint32_t linear_id = platform_get_core_pos(mpidr), id; 826cf89021SAchin Gupta 836cf89021SAchin Gupta /* 846cf89021SAchin Gupta * Get the highest priority pending interrupt id and see if it is the 856cf89021SAchin Gupta * secure physical generic timer interrupt in which case, handle it. 866cf89021SAchin Gupta * Otherwise throw this interrupt at the EL3 firmware. 876cf89021SAchin Gupta */ 889865ac15SDan Handley id = plat_ic_get_pending_interrupt_id(); 896cf89021SAchin Gupta 906cf89021SAchin Gupta /* TSP can only handle the secure physical timer interrupt */ 91*5a06bb7eSDan Handley if (id != TSP_IRQ_SEC_PHY_TIMER) 926cf89021SAchin Gupta return TSP_EL3_FIQ; 936cf89021SAchin Gupta 946cf89021SAchin Gupta /* 956cf89021SAchin Gupta * Handle the interrupt. Also sanity check if it has been preempted by 966cf89021SAchin Gupta * another secure interrupt through an assertion. 976cf89021SAchin Gupta */ 989865ac15SDan Handley id = plat_ic_acknowledge_interrupt(); 99*5a06bb7eSDan Handley assert(id == TSP_IRQ_SEC_PHY_TIMER); 1006cf89021SAchin Gupta tsp_generic_timer_handler(); 1019865ac15SDan Handley plat_ic_end_of_interrupt(id); 1026cf89021SAchin Gupta 1036cf89021SAchin Gupta /* Update the statistics and print some messages */ 1046cf89021SAchin Gupta tsp_stats[linear_id].fiq_count++; 1056ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 1066cf89021SAchin Gupta spin_lock(&console_lock); 1076ad2e461SDan Handley VERBOSE("TSP: cpu 0x%x handled fiq %d\n", 1086cf89021SAchin Gupta mpidr, id); 1096ad2e461SDan Handley VERBOSE("TSP: cpu 0x%x: %d fiq requests\n", 1106cf89021SAchin Gupta mpidr, tsp_stats[linear_id].fiq_count); 1116cf89021SAchin Gupta spin_unlock(&console_lock); 1126ad2e461SDan Handley #endif 1136cf89021SAchin Gupta return 0; 1146cf89021SAchin Gupta } 115239b04faSSoby Mathew 1164f2104ffSJuan Castillo int32_t tsp_irq_received(void) 117239b04faSSoby Mathew { 118239b04faSSoby Mathew uint64_t mpidr = read_mpidr(); 119239b04faSSoby Mathew uint32_t linear_id = platform_get_core_pos(mpidr); 120239b04faSSoby Mathew 121239b04faSSoby Mathew tsp_stats[linear_id].irq_count++; 1226ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 123239b04faSSoby Mathew spin_lock(&console_lock); 1246ad2e461SDan Handley VERBOSE("TSP: cpu 0x%x received irq\n", mpidr); 1256ad2e461SDan Handley VERBOSE("TSP: cpu 0x%x: %d irq requests\n", 126239b04faSSoby Mathew mpidr, tsp_stats[linear_id].irq_count); 127239b04faSSoby Mathew spin_unlock(&console_lock); 1286ad2e461SDan Handley #endif 129239b04faSSoby Mathew return TSP_PREEMPTED; 130239b04faSSoby Mathew } 131