17c88f3f6SAchin Gupta /* 23df6012aSDouglas Raillard * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 37c88f3f6SAchin Gupta * 4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 57c88f3f6SAchin Gupta */ 67c88f3f6SAchin Gupta 77c88f3f6SAchin Gupta #include <arch_helpers.h> 897043ac9SDan Handley #include <bl_common.h> 97c88f3f6SAchin Gupta #include <debug.h> 1097043ac9SDan Handley #include <platform.h> 115f0cdb05SDan Handley #include <platform_def.h> 125a06bb7eSDan Handley #include <platform_tsp.h> 137c88f3f6SAchin Gupta #include <spinlock.h> 1497043ac9SDan Handley #include <tsp.h> 15da0af78aSDan Handley #include "tsp_private.h" 167c88f3f6SAchin Gupta 176871c5d3SVikram Kanigiri 186871c5d3SVikram Kanigiri /******************************************************************************* 197c88f3f6SAchin Gupta * Lock to control access to the console 207c88f3f6SAchin Gupta ******************************************************************************/ 217c88f3f6SAchin Gupta spinlock_t console_lock; 227c88f3f6SAchin Gupta 237c88f3f6SAchin Gupta /******************************************************************************* 247c88f3f6SAchin Gupta * Per cpu data structure to populate parameters for an SMC in C code and use 257c88f3f6SAchin Gupta * a pointer to this structure in assembler code to populate x0-x7 267c88f3f6SAchin Gupta ******************************************************************************/ 27fb037bfbSDan Handley static tsp_args_t tsp_smc_args[PLATFORM_CORE_COUNT]; 287c88f3f6SAchin Gupta 297c88f3f6SAchin Gupta /******************************************************************************* 307c88f3f6SAchin Gupta * Per cpu data structure to keep track of TSP activity 317c88f3f6SAchin Gupta ******************************************************************************/ 326cf89021SAchin Gupta work_statistics_t tsp_stats[PLATFORM_CORE_COUNT]; 337c88f3f6SAchin Gupta 347c88f3f6SAchin Gupta /******************************************************************************* 35a604623cSSandrine Bailleux * The TSP memory footprint starts at address BL32_BASE and ends with the 36a604623cSSandrine Bailleux * linker symbol __BL32_END__. Use these addresses to compute the TSP image 37a604623cSSandrine Bailleux * size. 386871c5d3SVikram Kanigiri ******************************************************************************/ 39ab8707e6SSoby Mathew #define BL32_TOTAL_LIMIT (unsigned long)(&__BL32_END__) 40a604623cSSandrine Bailleux #define BL32_TOTAL_SIZE (BL32_TOTAL_LIMIT - (unsigned long) BL32_BASE) 416871c5d3SVikram Kanigiri 42fb037bfbSDan Handley static tsp_args_t *set_smc_args(uint64_t arg0, 437c88f3f6SAchin Gupta uint64_t arg1, 447c88f3f6SAchin Gupta uint64_t arg2, 457c88f3f6SAchin Gupta uint64_t arg3, 467c88f3f6SAchin Gupta uint64_t arg4, 477c88f3f6SAchin Gupta uint64_t arg5, 487c88f3f6SAchin Gupta uint64_t arg6, 497c88f3f6SAchin Gupta uint64_t arg7) 507c88f3f6SAchin Gupta { 517c88f3f6SAchin Gupta uint32_t linear_id; 52fb037bfbSDan Handley tsp_args_t *pcpu_smc_args; 537c88f3f6SAchin Gupta 547c88f3f6SAchin Gupta /* 557c88f3f6SAchin Gupta * Return to Secure Monitor by raising an SMC. The results of the 567c88f3f6SAchin Gupta * service are passed as an arguments to the SMC 577c88f3f6SAchin Gupta */ 58fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 597c88f3f6SAchin Gupta pcpu_smc_args = &tsp_smc_args[linear_id]; 607c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG0, arg0); 617c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG1, arg1); 627c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG2, arg2); 637c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG3, arg3); 647c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG4, arg4); 657c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG5, arg5); 667c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG6, arg6); 677c88f3f6SAchin Gupta write_sp_arg(pcpu_smc_args, TSP_ARG7, arg7); 687c88f3f6SAchin Gupta 697c88f3f6SAchin Gupta return pcpu_smc_args; 707c88f3f6SAchin Gupta } 717c88f3f6SAchin Gupta 727c88f3f6SAchin Gupta /******************************************************************************* 737c88f3f6SAchin Gupta * TSP main entry point where it gets the opportunity to initialize its secure 747c88f3f6SAchin Gupta * state/applications. Once the state is initialized, it must return to the 75399fb08fSAndrew Thoelke * SPD with a pointer to the 'tsp_vector_table' jump table. 767c88f3f6SAchin Gupta ******************************************************************************/ 777c88f3f6SAchin Gupta uint64_t tsp_main(void) 787c88f3f6SAchin Gupta { 796ad2e461SDan Handley NOTICE("TSP: %s\n", version_string); 806ad2e461SDan Handley NOTICE("TSP: %s\n", build_message); 81a604623cSSandrine Bailleux INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE); 82a604623cSSandrine Bailleux INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE); 836ad2e461SDan Handley 84fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 857c88f3f6SAchin Gupta 867c88f3f6SAchin Gupta /* Initialize the platform */ 875a06bb7eSDan Handley tsp_platform_setup(); 887c88f3f6SAchin Gupta 897c88f3f6SAchin Gupta /* Initialize secure/applications state here */ 90a20a81e5SAchin Gupta tsp_generic_timer_start(); 917c88f3f6SAchin Gupta 927c88f3f6SAchin Gupta /* Update this cpu's statistics */ 937c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 947c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 957c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 967c88f3f6SAchin Gupta 976ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO 987c88f3f6SAchin Gupta spin_lock(&console_lock); 99fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 100fd650ff6SSoby Mathew read_mpidr(), 1017c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1027c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1037c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 1047c88f3f6SAchin Gupta spin_unlock(&console_lock); 1056ad2e461SDan Handley #endif 106399fb08fSAndrew Thoelke return (uint64_t) &tsp_vector_table; 1077c88f3f6SAchin Gupta } 1087c88f3f6SAchin Gupta 1097c88f3f6SAchin Gupta /******************************************************************************* 1107c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 1117c88f3f6SAchin Gupta * after this cpu's architectural state has been setup in response to an earlier 1127c88f3f6SAchin Gupta * psci cpu_on request. 1137c88f3f6SAchin Gupta ******************************************************************************/ 114fb037bfbSDan Handley tsp_args_t *tsp_cpu_on_main(void) 1157c88f3f6SAchin Gupta { 116fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1177c88f3f6SAchin Gupta 118a20a81e5SAchin Gupta /* Initialize secure/applications state here */ 119a20a81e5SAchin Gupta tsp_generic_timer_start(); 120a20a81e5SAchin Gupta 1217c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1227c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1237c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1247c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 1257c88f3f6SAchin Gupta 1266ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO 1277c88f3f6SAchin Gupta spin_lock(&console_lock); 128fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx turned on\n", read_mpidr()); 129fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 130fd650ff6SSoby Mathew read_mpidr(), 1317c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1327c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1337c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 1347c88f3f6SAchin Gupta spin_unlock(&console_lock); 1356ad2e461SDan Handley #endif 1367c88f3f6SAchin Gupta /* Indicate to the SPD that we have completed turned ourselves on */ 1377c88f3f6SAchin Gupta return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0); 1387c88f3f6SAchin Gupta } 1397c88f3f6SAchin Gupta 1407c88f3f6SAchin Gupta /******************************************************************************* 1417c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 1427c88f3f6SAchin Gupta * before this cpu is turned off in response to a psci cpu_off request. 1437c88f3f6SAchin Gupta ******************************************************************************/ 144fb037bfbSDan Handley tsp_args_t *tsp_cpu_off_main(uint64_t arg0, 1457c88f3f6SAchin Gupta uint64_t arg1, 1467c88f3f6SAchin Gupta uint64_t arg2, 1477c88f3f6SAchin Gupta uint64_t arg3, 1487c88f3f6SAchin Gupta uint64_t arg4, 1497c88f3f6SAchin Gupta uint64_t arg5, 1507c88f3f6SAchin Gupta uint64_t arg6, 1517c88f3f6SAchin Gupta uint64_t arg7) 1527c88f3f6SAchin Gupta { 153fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1547c88f3f6SAchin Gupta 155a20a81e5SAchin Gupta /* 156a20a81e5SAchin Gupta * This cpu is being turned off, so disable the timer to prevent the 157a20a81e5SAchin Gupta * secure timer interrupt from interfering with power down. A pending 158a20a81e5SAchin Gupta * interrupt will be lost but we do not care as we are turning off. 159a20a81e5SAchin Gupta */ 160a20a81e5SAchin Gupta tsp_generic_timer_stop(); 161a20a81e5SAchin Gupta 1627c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1637c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1647c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1657c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count++; 1667c88f3f6SAchin Gupta 1676ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO 1687c88f3f6SAchin Gupta spin_lock(&console_lock); 169fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx off request\n", read_mpidr()); 170fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n", 171fd650ff6SSoby Mathew read_mpidr(), 1727c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1737c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1747c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count); 1757c88f3f6SAchin Gupta spin_unlock(&console_lock); 1766ad2e461SDan Handley #endif 1777c88f3f6SAchin Gupta 178607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1797c88f3f6SAchin Gupta return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); 1807c88f3f6SAchin Gupta } 1817c88f3f6SAchin Gupta 1827c88f3f6SAchin Gupta /******************************************************************************* 1837c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload before 1847c88f3f6SAchin Gupta * this cpu's architectural state is saved in response to an earlier psci 1857c88f3f6SAchin Gupta * cpu_suspend request. 1867c88f3f6SAchin Gupta ******************************************************************************/ 18731244d74SSoby Mathew tsp_args_t *tsp_cpu_suspend_main(uint64_t arg0, 1887c88f3f6SAchin Gupta uint64_t arg1, 1897c88f3f6SAchin Gupta uint64_t arg2, 1907c88f3f6SAchin Gupta uint64_t arg3, 1917c88f3f6SAchin Gupta uint64_t arg4, 1927c88f3f6SAchin Gupta uint64_t arg5, 1937c88f3f6SAchin Gupta uint64_t arg6, 1947c88f3f6SAchin Gupta uint64_t arg7) 1957c88f3f6SAchin Gupta { 196fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1977c88f3f6SAchin Gupta 198a20a81e5SAchin Gupta /* 199a20a81e5SAchin Gupta * Save the time context and disable it to prevent the secure timer 200a20a81e5SAchin Gupta * interrupt from interfering with wakeup from the suspend state. 201a20a81e5SAchin Gupta */ 202a20a81e5SAchin Gupta tsp_generic_timer_save(); 203a20a81e5SAchin Gupta tsp_generic_timer_stop(); 204a20a81e5SAchin Gupta 2057c88f3f6SAchin Gupta /* Update this cpu's statistics */ 2067c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 2077c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 2087c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count++; 2097c88f3f6SAchin Gupta 2106ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO 2117c88f3f6SAchin Gupta spin_lock(&console_lock); 212dad25049SSandrine Bailleux INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n", 213fd650ff6SSoby Mathew read_mpidr(), 2147c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 2157c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 2167c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count); 2177c88f3f6SAchin Gupta spin_unlock(&console_lock); 2186ad2e461SDan Handley #endif 2197c88f3f6SAchin Gupta 220607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 2217c88f3f6SAchin Gupta return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0); 2227c88f3f6SAchin Gupta } 2237c88f3f6SAchin Gupta 2247c88f3f6SAchin Gupta /******************************************************************************* 2257c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload after this 2267c88f3f6SAchin Gupta * cpu's architectural state has been restored after wakeup from an earlier psci 2277c88f3f6SAchin Gupta * cpu_suspend request. 2287c88f3f6SAchin Gupta ******************************************************************************/ 229f1054c93SAchin Gupta tsp_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl, 2307c88f3f6SAchin Gupta uint64_t arg1, 2317c88f3f6SAchin Gupta uint64_t arg2, 2327c88f3f6SAchin Gupta uint64_t arg3, 2337c88f3f6SAchin Gupta uint64_t arg4, 2347c88f3f6SAchin Gupta uint64_t arg5, 2357c88f3f6SAchin Gupta uint64_t arg6, 2367c88f3f6SAchin Gupta uint64_t arg7) 2377c88f3f6SAchin Gupta { 238fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 2397c88f3f6SAchin Gupta 240a20a81e5SAchin Gupta /* Restore the generic timer context */ 241a20a81e5SAchin Gupta tsp_generic_timer_restore(); 242a20a81e5SAchin Gupta 2437c88f3f6SAchin Gupta /* Update this cpu's statistics */ 2447c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 2457c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 2467c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_resume_count++; 2477c88f3f6SAchin Gupta 2486ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO 2497c88f3f6SAchin Gupta spin_lock(&console_lock); 250f1054c93SAchin Gupta INFO("TSP: cpu 0x%lx resumed. maximum off power level %ld\n", 251f1054c93SAchin Gupta read_mpidr(), max_off_pwrlvl); 252dad25049SSandrine Bailleux INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n", 253fd650ff6SSoby Mathew read_mpidr(), 2547c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 2557c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 2567c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count); 2577c88f3f6SAchin Gupta spin_unlock(&console_lock); 2586ad2e461SDan Handley #endif 259607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 2607c88f3f6SAchin Gupta return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0); 2617c88f3f6SAchin Gupta } 2627c88f3f6SAchin Gupta 2637c88f3f6SAchin Gupta /******************************************************************************* 264d5f13093SJuan Castillo * This function performs any remaining bookkeeping in the test secure payload 265d5f13093SJuan Castillo * before the system is switched off (in response to a psci SYSTEM_OFF request) 266d5f13093SJuan Castillo ******************************************************************************/ 267d5f13093SJuan Castillo tsp_args_t *tsp_system_off_main(uint64_t arg0, 268d5f13093SJuan Castillo uint64_t arg1, 269d5f13093SJuan Castillo uint64_t arg2, 270d5f13093SJuan Castillo uint64_t arg3, 271d5f13093SJuan Castillo uint64_t arg4, 272d5f13093SJuan Castillo uint64_t arg5, 273d5f13093SJuan Castillo uint64_t arg6, 274d5f13093SJuan Castillo uint64_t arg7) 275d5f13093SJuan Castillo { 276fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 277d5f13093SJuan Castillo 278d5f13093SJuan Castillo /* Update this cpu's statistics */ 279d5f13093SJuan Castillo tsp_stats[linear_id].smc_count++; 280d5f13093SJuan Castillo tsp_stats[linear_id].eret_count++; 281d5f13093SJuan Castillo 282d5f13093SJuan Castillo #if LOG_LEVEL >= LOG_LEVEL_INFO 283d5f13093SJuan Castillo spin_lock(&console_lock); 284fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr()); 285fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(), 286d5f13093SJuan Castillo tsp_stats[linear_id].smc_count, 287d5f13093SJuan Castillo tsp_stats[linear_id].eret_count); 288d5f13093SJuan Castillo spin_unlock(&console_lock); 289d5f13093SJuan Castillo #endif 290d5f13093SJuan Castillo 291d5f13093SJuan Castillo /* Indicate to the SPD that we have completed this request */ 292d5f13093SJuan Castillo return set_smc_args(TSP_SYSTEM_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); 293d5f13093SJuan Castillo } 294d5f13093SJuan Castillo 295d5f13093SJuan Castillo /******************************************************************************* 296d5f13093SJuan Castillo * This function performs any remaining bookkeeping in the test secure payload 297d5f13093SJuan Castillo * before the system is reset (in response to a psci SYSTEM_RESET request) 298d5f13093SJuan Castillo ******************************************************************************/ 299d5f13093SJuan Castillo tsp_args_t *tsp_system_reset_main(uint64_t arg0, 300d5f13093SJuan Castillo uint64_t arg1, 301d5f13093SJuan Castillo uint64_t arg2, 302d5f13093SJuan Castillo uint64_t arg3, 303d5f13093SJuan Castillo uint64_t arg4, 304d5f13093SJuan Castillo uint64_t arg5, 305d5f13093SJuan Castillo uint64_t arg6, 306d5f13093SJuan Castillo uint64_t arg7) 307d5f13093SJuan Castillo { 308fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 309d5f13093SJuan Castillo 310d5f13093SJuan Castillo /* Update this cpu's statistics */ 311d5f13093SJuan Castillo tsp_stats[linear_id].smc_count++; 312d5f13093SJuan Castillo tsp_stats[linear_id].eret_count++; 313d5f13093SJuan Castillo 314d5f13093SJuan Castillo #if LOG_LEVEL >= LOG_LEVEL_INFO 315d5f13093SJuan Castillo spin_lock(&console_lock); 316fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr()); 317fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(), 318d5f13093SJuan Castillo tsp_stats[linear_id].smc_count, 319d5f13093SJuan Castillo tsp_stats[linear_id].eret_count); 320d5f13093SJuan Castillo spin_unlock(&console_lock); 321d5f13093SJuan Castillo #endif 322d5f13093SJuan Castillo 323d5f13093SJuan Castillo /* Indicate to the SPD that we have completed this request */ 324d5f13093SJuan Castillo return set_smc_args(TSP_SYSTEM_RESET_DONE, 0, 0, 0, 0, 0, 0, 0); 325d5f13093SJuan Castillo } 326d5f13093SJuan Castillo 327d5f13093SJuan Castillo /******************************************************************************* 3287c88f3f6SAchin Gupta * TSP fast smc handler. The secure monitor jumps to this function by 3297c88f3f6SAchin Gupta * doing the ERET after populating X0-X7 registers. The arguments are received 3307c88f3f6SAchin Gupta * in the function arguments in order. Once the service is rendered, this 331239b04faSSoby Mathew * function returns to Secure Monitor by raising SMC. 3327c88f3f6SAchin Gupta ******************************************************************************/ 333239b04faSSoby Mathew tsp_args_t *tsp_smc_handler(uint64_t func, 3347c88f3f6SAchin Gupta uint64_t arg1, 3357c88f3f6SAchin Gupta uint64_t arg2, 3367c88f3f6SAchin Gupta uint64_t arg3, 3377c88f3f6SAchin Gupta uint64_t arg4, 3387c88f3f6SAchin Gupta uint64_t arg5, 3397c88f3f6SAchin Gupta uint64_t arg6, 3407c88f3f6SAchin Gupta uint64_t arg7) 3417c88f3f6SAchin Gupta { 342916a2c1eSAchin Gupta uint64_t results[2]; 343916a2c1eSAchin Gupta uint64_t service_args[2]; 344fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 3457c88f3f6SAchin Gupta 346916a2c1eSAchin Gupta /* Update this cpu's statistics */ 347916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count++; 348916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count++; 3497c88f3f6SAchin Gupta 350fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx received %s smc 0x%lx\n", read_mpidr(), 3516ad2e461SDan Handley ((func >> 31) & 1) == 1 ? "fast" : "standard", 3526ad2e461SDan Handley func); 353fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(), 354916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count, 355916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count); 356916a2c1eSAchin Gupta 357916a2c1eSAchin Gupta /* Render secure services and obtain results here */ 3587c88f3f6SAchin Gupta results[0] = arg1; 3597c88f3f6SAchin Gupta results[1] = arg2; 3607c88f3f6SAchin Gupta 3617c88f3f6SAchin Gupta /* 3627c88f3f6SAchin Gupta * Request a service back from dispatcher/secure monitor. This call 3637c88f3f6SAchin Gupta * return and thereafter resume exectuion 3647c88f3f6SAchin Gupta */ 3657c88f3f6SAchin Gupta tsp_get_magic(service_args); 3667c88f3f6SAchin Gupta 3677c88f3f6SAchin Gupta /* Determine the function to perform based on the function ID */ 368239b04faSSoby Mathew switch (TSP_BARE_FID(func)) { 369239b04faSSoby Mathew case TSP_ADD: 3707c88f3f6SAchin Gupta results[0] += service_args[0]; 3717c88f3f6SAchin Gupta results[1] += service_args[1]; 3727c88f3f6SAchin Gupta break; 373239b04faSSoby Mathew case TSP_SUB: 3747c88f3f6SAchin Gupta results[0] -= service_args[0]; 3757c88f3f6SAchin Gupta results[1] -= service_args[1]; 3767c88f3f6SAchin Gupta break; 377239b04faSSoby Mathew case TSP_MUL: 3787c88f3f6SAchin Gupta results[0] *= service_args[0]; 3797c88f3f6SAchin Gupta results[1] *= service_args[1]; 3807c88f3f6SAchin Gupta break; 381239b04faSSoby Mathew case TSP_DIV: 3827c88f3f6SAchin Gupta results[0] /= service_args[0] ? service_args[0] : 1; 3837c88f3f6SAchin Gupta results[1] /= service_args[1] ? service_args[1] : 1; 3847c88f3f6SAchin Gupta break; 3857c88f3f6SAchin Gupta default: 3867c88f3f6SAchin Gupta break; 3877c88f3f6SAchin Gupta } 3887c88f3f6SAchin Gupta 389239b04faSSoby Mathew return set_smc_args(func, 0, 3907c88f3f6SAchin Gupta results[0], 3917c88f3f6SAchin Gupta results[1], 392239b04faSSoby Mathew 0, 0, 0, 0); 3937c88f3f6SAchin Gupta } 3947c88f3f6SAchin Gupta 3953df6012aSDouglas Raillard /******************************************************************************* 3963df6012aSDouglas Raillard * TSP smc abort handler. This function is called when aborting a preemtped 3973df6012aSDouglas Raillard * standard SMC request. It should cleanup all resources owned by the SMC 3983df6012aSDouglas Raillard * handler such as locks or dynamically allocated memory so following SMC 3993df6012aSDouglas Raillard * request are executed in a clean environment. 4003df6012aSDouglas Raillard ******************************************************************************/ 4013df6012aSDouglas Raillard tsp_args_t *tsp_abort_smc_handler(uint64_t func, 4023df6012aSDouglas Raillard uint64_t arg1, 4033df6012aSDouglas Raillard uint64_t arg2, 4043df6012aSDouglas Raillard uint64_t arg3, 4053df6012aSDouglas Raillard uint64_t arg4, 4063df6012aSDouglas Raillard uint64_t arg5, 4073df6012aSDouglas Raillard uint64_t arg6, 4083df6012aSDouglas Raillard uint64_t arg7) 4093df6012aSDouglas Raillard { 4103df6012aSDouglas Raillard return set_smc_args(TSP_ABORT_DONE, 0, 0, 0, 0, 0, 0, 0); 4113df6012aSDouglas Raillard } 412