17c88f3f6SAchin Gupta /* 20a33adc0SGovindraj Raja * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved. 37c88f3f6SAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 57c88f3f6SAchin Gupta */ 67c88f3f6SAchin Gupta 7ed108b56SAlexei Fedorov #include <assert.h> 84ce3e99aSScott Branden #include <inttypes.h> 94ce3e99aSScott Branden #include <stdint.h> 1009d40e0eSAntonio Nino Diaz 11ed108b56SAlexei Fedorov #include <arch_features.h> 1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1309d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h> 14*7623e085SJayanth Dodderi Chidanand #include <bl32/tsp/tsp_el1_context.h> 1509d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 16758ccb80SChris Kay #include <common/build_message.h> 1709d40e0eSAntonio Nino Diaz #include <common/debug.h> 1809d40e0eSAntonio Nino Diaz #include <lib/spinlock.h> 1909d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 205a06bb7eSDan Handley #include <platform_tsp.h> 21da0af78aSDan Handley #include "tsp_private.h" 227c88f3f6SAchin Gupta 234a8bfdb9SAchin Gupta #include <platform_def.h> 2467b6ff9fSAntonio Nino Diaz 2567b6ff9fSAntonio Nino Diaz /******************************************************************************* 267c88f3f6SAchin Gupta * TSP main entry point where it gets the opportunity to initialize its secure 277c88f3f6SAchin Gupta * state/applications. Once the state is initialized, it must return to the 28399fb08fSAndrew Thoelke * SPD with a pointer to the 'tsp_vector_table' jump table. 297c88f3f6SAchin Gupta ******************************************************************************/ 307c88f3f6SAchin Gupta uint64_t tsp_main(void) 317c88f3f6SAchin Gupta { 32758ccb80SChris Kay NOTICE("TSP: %s\n", build_version_string); 336ad2e461SDan Handley NOTICE("TSP: %s\n", build_message); 34a604623cSSandrine Bailleux INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE); 35a604623cSSandrine Bailleux INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE); 366ad2e461SDan Handley 37fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 387c88f3f6SAchin Gupta 397c88f3f6SAchin Gupta /* Initialize the platform */ 405a06bb7eSDan Handley tsp_platform_setup(); 417c88f3f6SAchin Gupta 427c88f3f6SAchin Gupta /* Initialize secure/applications state here */ 43a20a81e5SAchin Gupta tsp_generic_timer_start(); 447c88f3f6SAchin Gupta 457c88f3f6SAchin Gupta /* Update this cpu's statistics */ 467c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 477c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 487c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 497c88f3f6SAchin Gupta 50fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 51fd650ff6SSoby Mathew read_mpidr(), 527c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 537c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 547c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 55ae074b36SGovindraj Raja 56ae074b36SGovindraj Raja console_flush(); 57399fb08fSAndrew Thoelke return (uint64_t) &tsp_vector_table; 587c88f3f6SAchin Gupta } 597c88f3f6SAchin Gupta 607c88f3f6SAchin Gupta /******************************************************************************* 617c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 627c88f3f6SAchin Gupta * after this cpu's architectural state has been setup in response to an earlier 637c88f3f6SAchin Gupta * psci cpu_on request. 647c88f3f6SAchin Gupta ******************************************************************************/ 654a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_on_main(void) 667c88f3f6SAchin Gupta { 67fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 687c88f3f6SAchin Gupta 69a20a81e5SAchin Gupta /* Initialize secure/applications state here */ 70a20a81e5SAchin Gupta tsp_generic_timer_start(); 71a20a81e5SAchin Gupta 727c88f3f6SAchin Gupta /* Update this cpu's statistics */ 737c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 747c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 757c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 767c88f3f6SAchin Gupta 77fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx turned on\n", read_mpidr()); 78fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 79fd650ff6SSoby Mathew read_mpidr(), 807c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 817c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 827c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 837c88f3f6SAchin Gupta /* Indicate to the SPD that we have completed turned ourselves on */ 847c88f3f6SAchin Gupta return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0); 857c88f3f6SAchin Gupta } 867c88f3f6SAchin Gupta 877c88f3f6SAchin Gupta /******************************************************************************* 887c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 897c88f3f6SAchin Gupta * before this cpu is turned off in response to a psci cpu_off request. 907c88f3f6SAchin Gupta ******************************************************************************/ 914a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_off_main(uint64_t arg0, 927c88f3f6SAchin Gupta uint64_t arg1, 937c88f3f6SAchin Gupta uint64_t arg2, 947c88f3f6SAchin Gupta uint64_t arg3, 957c88f3f6SAchin Gupta uint64_t arg4, 967c88f3f6SAchin Gupta uint64_t arg5, 977c88f3f6SAchin Gupta uint64_t arg6, 987c88f3f6SAchin Gupta uint64_t arg7) 997c88f3f6SAchin Gupta { 100fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1017c88f3f6SAchin Gupta 102a20a81e5SAchin Gupta /* 103a20a81e5SAchin Gupta * This cpu is being turned off, so disable the timer to prevent the 104a20a81e5SAchin Gupta * secure timer interrupt from interfering with power down. A pending 105a20a81e5SAchin Gupta * interrupt will be lost but we do not care as we are turning off. 106a20a81e5SAchin Gupta */ 107a20a81e5SAchin Gupta tsp_generic_timer_stop(); 108a20a81e5SAchin Gupta 1097c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1107c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1117c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1127c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count++; 1137c88f3f6SAchin Gupta 114fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx off request\n", read_mpidr()); 115fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n", 116fd650ff6SSoby Mathew read_mpidr(), 1177c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1187c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1197c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count); 1207c88f3f6SAchin Gupta 121607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1227c88f3f6SAchin Gupta return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); 1237c88f3f6SAchin Gupta } 1247c88f3f6SAchin Gupta 1257c88f3f6SAchin Gupta /******************************************************************************* 1267c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload before 1277c88f3f6SAchin Gupta * this cpu's architectural state is saved in response to an earlier psci 1287c88f3f6SAchin Gupta * cpu_suspend request. 1297c88f3f6SAchin Gupta ******************************************************************************/ 1304a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_suspend_main(uint64_t arg0, 1317c88f3f6SAchin Gupta uint64_t arg1, 1327c88f3f6SAchin Gupta uint64_t arg2, 1337c88f3f6SAchin Gupta uint64_t arg3, 1347c88f3f6SAchin Gupta uint64_t arg4, 1357c88f3f6SAchin Gupta uint64_t arg5, 1367c88f3f6SAchin Gupta uint64_t arg6, 1377c88f3f6SAchin Gupta uint64_t arg7) 1387c88f3f6SAchin Gupta { 139fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1407c88f3f6SAchin Gupta 141a20a81e5SAchin Gupta /* 142a20a81e5SAchin Gupta * Save the time context and disable it to prevent the secure timer 143a20a81e5SAchin Gupta * interrupt from interfering with wakeup from the suspend state. 144a20a81e5SAchin Gupta */ 145a20a81e5SAchin Gupta tsp_generic_timer_save(); 146a20a81e5SAchin Gupta tsp_generic_timer_stop(); 147a20a81e5SAchin Gupta 1487c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1497c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1507c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1517c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count++; 1527c88f3f6SAchin Gupta 153dad25049SSandrine Bailleux INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n", 154fd650ff6SSoby Mathew read_mpidr(), 1557c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1567c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1577c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count); 1587c88f3f6SAchin Gupta 159607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1607c88f3f6SAchin Gupta return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0); 1617c88f3f6SAchin Gupta } 1627c88f3f6SAchin Gupta 1637c88f3f6SAchin Gupta /******************************************************************************* 1647c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload after this 1657c88f3f6SAchin Gupta * cpu's architectural state has been restored after wakeup from an earlier psci 1667c88f3f6SAchin Gupta * cpu_suspend request. 1677c88f3f6SAchin Gupta ******************************************************************************/ 1684a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl, 1697c88f3f6SAchin Gupta uint64_t arg1, 1707c88f3f6SAchin Gupta uint64_t arg2, 1717c88f3f6SAchin Gupta uint64_t arg3, 1727c88f3f6SAchin Gupta uint64_t arg4, 1737c88f3f6SAchin Gupta uint64_t arg5, 1747c88f3f6SAchin Gupta uint64_t arg6, 1757c88f3f6SAchin Gupta uint64_t arg7) 1767c88f3f6SAchin Gupta { 177fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1787c88f3f6SAchin Gupta 179a20a81e5SAchin Gupta /* Restore the generic timer context */ 180a20a81e5SAchin Gupta tsp_generic_timer_restore(); 181a20a81e5SAchin Gupta 1827c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1837c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1847c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1857c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_resume_count++; 1867c88f3f6SAchin Gupta 1874ce3e99aSScott Branden INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n", 188f1054c93SAchin Gupta read_mpidr(), max_off_pwrlvl); 189a16bc845SManish Pandey INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n", 190fd650ff6SSoby Mathew read_mpidr(), 1917c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1927c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 193a16bc845SManish Pandey tsp_stats[linear_id].cpu_resume_count); 194607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1957c88f3f6SAchin Gupta return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0); 1967c88f3f6SAchin Gupta } 1977c88f3f6SAchin Gupta 1987c88f3f6SAchin Gupta /******************************************************************************* 1997c88f3f6SAchin Gupta * TSP fast smc handler. The secure monitor jumps to this function by 2007c88f3f6SAchin Gupta * doing the ERET after populating X0-X7 registers. The arguments are received 2017c88f3f6SAchin Gupta * in the function arguments in order. Once the service is rendered, this 202239b04faSSoby Mathew * function returns to Secure Monitor by raising SMC. 2037c88f3f6SAchin Gupta ******************************************************************************/ 2044a8bfdb9SAchin Gupta smc_args_t *tsp_smc_handler(uint64_t func, 2057c88f3f6SAchin Gupta uint64_t arg1, 2067c88f3f6SAchin Gupta uint64_t arg2, 2077c88f3f6SAchin Gupta uint64_t arg3, 2087c88f3f6SAchin Gupta uint64_t arg4, 2097c88f3f6SAchin Gupta uint64_t arg5, 2107c88f3f6SAchin Gupta uint64_t arg6, 2117c88f3f6SAchin Gupta uint64_t arg7) 2127c88f3f6SAchin Gupta { 213caff3c87SAlexei Fedorov uint128_t service_args; 214caff3c87SAlexei Fedorov uint64_t service_arg0; 215caff3c87SAlexei Fedorov uint64_t service_arg1; 216916a2c1eSAchin Gupta uint64_t results[2]; 217fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 2184d482156SDaniel Boulby u_register_t dit; 2197c88f3f6SAchin Gupta 220916a2c1eSAchin Gupta /* Update this cpu's statistics */ 221916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count++; 222916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count++; 2237c88f3f6SAchin Gupta 2244ce3e99aSScott Branden INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(), 22516292f54SDavid Cunado ((func >> 31) & 1) == 1 ? "fast" : "yielding", 2266ad2e461SDan Handley func); 227fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(), 228916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count, 229916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count); 230916a2c1eSAchin Gupta 231916a2c1eSAchin Gupta /* Render secure services and obtain results here */ 2327c88f3f6SAchin Gupta results[0] = arg1; 2337c88f3f6SAchin Gupta results[1] = arg2; 2347c88f3f6SAchin Gupta 2357c88f3f6SAchin Gupta /* 236caff3c87SAlexei Fedorov * Request a service back from dispatcher/secure monitor. 237caff3c87SAlexei Fedorov * This call returns and thereafter resumes execution. 2387c88f3f6SAchin Gupta */ 239caff3c87SAlexei Fedorov service_args = tsp_get_magic(); 240caff3c87SAlexei Fedorov service_arg0 = (uint64_t)service_args; 241caff3c87SAlexei Fedorov service_arg1 = (uint64_t)(service_args >> 64U); 2427c88f3f6SAchin Gupta 2439dd94382SJustin Chadwell /* 244c282384dSGovindraj Raja * Write a dummy value to an MTE2 register, to simulate usage in the 2459dd94382SJustin Chadwell * secure world 2469dd94382SJustin Chadwell */ 247c282384dSGovindraj Raja if (is_feat_mte2_supported()) { 2489dd94382SJustin Chadwell write_gcr_el1(0x99); 2498e397889SGovindraj Raja } 2509dd94382SJustin Chadwell 2517c88f3f6SAchin Gupta /* Determine the function to perform based on the function ID */ 252239b04faSSoby Mathew switch (TSP_BARE_FID(func)) { 253239b04faSSoby Mathew case TSP_ADD: 254caff3c87SAlexei Fedorov results[0] += service_arg0; 255caff3c87SAlexei Fedorov results[1] += service_arg1; 2567c88f3f6SAchin Gupta break; 257239b04faSSoby Mathew case TSP_SUB: 258caff3c87SAlexei Fedorov results[0] -= service_arg0; 259caff3c87SAlexei Fedorov results[1] -= service_arg1; 2607c88f3f6SAchin Gupta break; 261239b04faSSoby Mathew case TSP_MUL: 262caff3c87SAlexei Fedorov results[0] *= service_arg0; 263caff3c87SAlexei Fedorov results[1] *= service_arg1; 2647c88f3f6SAchin Gupta break; 265239b04faSSoby Mathew case TSP_DIV: 266caff3c87SAlexei Fedorov results[0] /= service_arg0 ? service_arg0 : 1; 267caff3c87SAlexei Fedorov results[1] /= service_arg1 ? service_arg1 : 1; 2687c88f3f6SAchin Gupta break; 2694d482156SDaniel Boulby case TSP_CHECK_DIT: 27088727fc3SAndre Przywara if (!is_feat_dit_supported()) { 2714d482156SDaniel Boulby ERROR("DIT not supported\n"); 2724d482156SDaniel Boulby results[0] = 0; 2734d482156SDaniel Boulby results[1] = 0xffff; 2744d482156SDaniel Boulby break; 2754d482156SDaniel Boulby } 2764d482156SDaniel Boulby dit = read_dit(); 2774d482156SDaniel Boulby results[0] = dit == service_arg0; 2784d482156SDaniel Boulby results[1] = dit; 2794d482156SDaniel Boulby /* Toggle the dit bit */ 2804d482156SDaniel Boulby write_dit(service_arg0 != 0U ? 0 : DIT_BIT); 2814d482156SDaniel Boulby break; 282*7623e085SJayanth Dodderi Chidanand case TSP_MODIFY_EL1_CTX: 283*7623e085SJayanth Dodderi Chidanand /* 284*7623e085SJayanth Dodderi Chidanand * Write dummy values to EL1 context registers, to simulate 285*7623e085SJayanth Dodderi Chidanand * their usage in the secure world. 286*7623e085SJayanth Dodderi Chidanand */ 287*7623e085SJayanth Dodderi Chidanand if (arg1 == TSP_CORRUPT_EL1_REGS) { 288*7623e085SJayanth Dodderi Chidanand modify_el1_ctx_regs(TSP_CORRUPT_EL1_REGS); 289*7623e085SJayanth Dodderi Chidanand } else { 290*7623e085SJayanth Dodderi Chidanand modify_el1_ctx_regs(TSP_RESTORE_EL1_REGS); 291*7623e085SJayanth Dodderi Chidanand } 292*7623e085SJayanth Dodderi Chidanand break; 2937c88f3f6SAchin Gupta default: 2947c88f3f6SAchin Gupta break; 2957c88f3f6SAchin Gupta } 2967c88f3f6SAchin Gupta 297239b04faSSoby Mathew return set_smc_args(func, 0, 2987c88f3f6SAchin Gupta results[0], 2997c88f3f6SAchin Gupta results[1], 300239b04faSSoby Mathew 0, 0, 0, 0); 3017c88f3f6SAchin Gupta } 302