17c88f3f6SAchin Gupta /* 2*0a33adc0SGovindraj 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> 1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1509d40e0eSAntonio Nino Diaz #include <common/debug.h> 1609d40e0eSAntonio Nino Diaz #include <lib/spinlock.h> 1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 185a06bb7eSDan Handley #include <platform_tsp.h> 19da0af78aSDan Handley #include "tsp_private.h" 207c88f3f6SAchin Gupta 214a8bfdb9SAchin Gupta #include <platform_def.h> 2267b6ff9fSAntonio Nino Diaz 2367b6ff9fSAntonio Nino Diaz /******************************************************************************* 247c88f3f6SAchin Gupta * TSP main entry point where it gets the opportunity to initialize its secure 257c88f3f6SAchin Gupta * state/applications. Once the state is initialized, it must return to the 26399fb08fSAndrew Thoelke * SPD with a pointer to the 'tsp_vector_table' jump table. 277c88f3f6SAchin Gupta ******************************************************************************/ 287c88f3f6SAchin Gupta uint64_t tsp_main(void) 297c88f3f6SAchin Gupta { 306ad2e461SDan Handley NOTICE("TSP: %s\n", version_string); 316ad2e461SDan Handley NOTICE("TSP: %s\n", build_message); 32a604623cSSandrine Bailleux INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE); 33a604623cSSandrine Bailleux INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE); 346ad2e461SDan Handley 35fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 367c88f3f6SAchin Gupta 377c88f3f6SAchin Gupta /* Initialize the platform */ 385a06bb7eSDan Handley tsp_platform_setup(); 397c88f3f6SAchin Gupta 407c88f3f6SAchin Gupta /* Initialize secure/applications state here */ 41a20a81e5SAchin Gupta tsp_generic_timer_start(); 427c88f3f6SAchin Gupta 437c88f3f6SAchin Gupta /* Update this cpu's statistics */ 447c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 457c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 467c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 477c88f3f6SAchin Gupta 48fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 49fd650ff6SSoby Mathew read_mpidr(), 507c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 517c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 527c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 53ae074b36SGovindraj Raja 54ae074b36SGovindraj Raja console_flush(); 55399fb08fSAndrew Thoelke return (uint64_t) &tsp_vector_table; 567c88f3f6SAchin Gupta } 577c88f3f6SAchin Gupta 587c88f3f6SAchin Gupta /******************************************************************************* 597c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 607c88f3f6SAchin Gupta * after this cpu's architectural state has been setup in response to an earlier 617c88f3f6SAchin Gupta * psci cpu_on request. 627c88f3f6SAchin Gupta ******************************************************************************/ 634a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_on_main(void) 647c88f3f6SAchin Gupta { 65fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 667c88f3f6SAchin Gupta 67a20a81e5SAchin Gupta /* Initialize secure/applications state here */ 68a20a81e5SAchin Gupta tsp_generic_timer_start(); 69a20a81e5SAchin Gupta 707c88f3f6SAchin Gupta /* Update this cpu's statistics */ 717c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 727c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 737c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 747c88f3f6SAchin Gupta 75fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx turned on\n", read_mpidr()); 76fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 77fd650ff6SSoby Mathew read_mpidr(), 787c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 797c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 807c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 817c88f3f6SAchin Gupta /* Indicate to the SPD that we have completed turned ourselves on */ 827c88f3f6SAchin Gupta return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0); 837c88f3f6SAchin Gupta } 847c88f3f6SAchin Gupta 857c88f3f6SAchin Gupta /******************************************************************************* 867c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 877c88f3f6SAchin Gupta * before this cpu is turned off in response to a psci cpu_off request. 887c88f3f6SAchin Gupta ******************************************************************************/ 894a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_off_main(uint64_t arg0, 907c88f3f6SAchin Gupta uint64_t arg1, 917c88f3f6SAchin Gupta uint64_t arg2, 927c88f3f6SAchin Gupta uint64_t arg3, 937c88f3f6SAchin Gupta uint64_t arg4, 947c88f3f6SAchin Gupta uint64_t arg5, 957c88f3f6SAchin Gupta uint64_t arg6, 967c88f3f6SAchin Gupta uint64_t arg7) 977c88f3f6SAchin Gupta { 98fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 997c88f3f6SAchin Gupta 100a20a81e5SAchin Gupta /* 101a20a81e5SAchin Gupta * This cpu is being turned off, so disable the timer to prevent the 102a20a81e5SAchin Gupta * secure timer interrupt from interfering with power down. A pending 103a20a81e5SAchin Gupta * interrupt will be lost but we do not care as we are turning off. 104a20a81e5SAchin Gupta */ 105a20a81e5SAchin Gupta tsp_generic_timer_stop(); 106a20a81e5SAchin Gupta 1077c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1087c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1097c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1107c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count++; 1117c88f3f6SAchin Gupta 112fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx off request\n", read_mpidr()); 113fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n", 114fd650ff6SSoby Mathew read_mpidr(), 1157c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1167c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1177c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count); 1187c88f3f6SAchin Gupta 119607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1207c88f3f6SAchin Gupta return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); 1217c88f3f6SAchin Gupta } 1227c88f3f6SAchin Gupta 1237c88f3f6SAchin Gupta /******************************************************************************* 1247c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload before 1257c88f3f6SAchin Gupta * this cpu's architectural state is saved in response to an earlier psci 1267c88f3f6SAchin Gupta * cpu_suspend request. 1277c88f3f6SAchin Gupta ******************************************************************************/ 1284a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_suspend_main(uint64_t arg0, 1297c88f3f6SAchin Gupta uint64_t arg1, 1307c88f3f6SAchin Gupta uint64_t arg2, 1317c88f3f6SAchin Gupta uint64_t arg3, 1327c88f3f6SAchin Gupta uint64_t arg4, 1337c88f3f6SAchin Gupta uint64_t arg5, 1347c88f3f6SAchin Gupta uint64_t arg6, 1357c88f3f6SAchin Gupta uint64_t arg7) 1367c88f3f6SAchin Gupta { 137fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1387c88f3f6SAchin Gupta 139a20a81e5SAchin Gupta /* 140a20a81e5SAchin Gupta * Save the time context and disable it to prevent the secure timer 141a20a81e5SAchin Gupta * interrupt from interfering with wakeup from the suspend state. 142a20a81e5SAchin Gupta */ 143a20a81e5SAchin Gupta tsp_generic_timer_save(); 144a20a81e5SAchin Gupta tsp_generic_timer_stop(); 145a20a81e5SAchin Gupta 1467c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1477c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1487c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1497c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count++; 1507c88f3f6SAchin Gupta 151dad25049SSandrine Bailleux INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n", 152fd650ff6SSoby Mathew read_mpidr(), 1537c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1547c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1557c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count); 1567c88f3f6SAchin Gupta 157607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1587c88f3f6SAchin Gupta return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0); 1597c88f3f6SAchin Gupta } 1607c88f3f6SAchin Gupta 1617c88f3f6SAchin Gupta /******************************************************************************* 1627c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload after this 1637c88f3f6SAchin Gupta * cpu's architectural state has been restored after wakeup from an earlier psci 1647c88f3f6SAchin Gupta * cpu_suspend request. 1657c88f3f6SAchin Gupta ******************************************************************************/ 1664a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl, 1677c88f3f6SAchin Gupta uint64_t arg1, 1687c88f3f6SAchin Gupta uint64_t arg2, 1697c88f3f6SAchin Gupta uint64_t arg3, 1707c88f3f6SAchin Gupta uint64_t arg4, 1717c88f3f6SAchin Gupta uint64_t arg5, 1727c88f3f6SAchin Gupta uint64_t arg6, 1737c88f3f6SAchin Gupta uint64_t arg7) 1747c88f3f6SAchin Gupta { 175fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1767c88f3f6SAchin Gupta 177a20a81e5SAchin Gupta /* Restore the generic timer context */ 178a20a81e5SAchin Gupta tsp_generic_timer_restore(); 179a20a81e5SAchin Gupta 1807c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1817c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1827c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1837c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_resume_count++; 1847c88f3f6SAchin Gupta 1854ce3e99aSScott Branden INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n", 186f1054c93SAchin Gupta read_mpidr(), max_off_pwrlvl); 187a16bc845SManish Pandey INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n", 188fd650ff6SSoby Mathew read_mpidr(), 1897c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1907c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 191a16bc845SManish Pandey tsp_stats[linear_id].cpu_resume_count); 192607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1937c88f3f6SAchin Gupta return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0); 1947c88f3f6SAchin Gupta } 1957c88f3f6SAchin Gupta 1967c88f3f6SAchin Gupta /******************************************************************************* 1977c88f3f6SAchin Gupta * TSP fast smc handler. The secure monitor jumps to this function by 1987c88f3f6SAchin Gupta * doing the ERET after populating X0-X7 registers. The arguments are received 1997c88f3f6SAchin Gupta * in the function arguments in order. Once the service is rendered, this 200239b04faSSoby Mathew * function returns to Secure Monitor by raising SMC. 2017c88f3f6SAchin Gupta ******************************************************************************/ 2024a8bfdb9SAchin Gupta smc_args_t *tsp_smc_handler(uint64_t func, 2037c88f3f6SAchin Gupta uint64_t arg1, 2047c88f3f6SAchin Gupta uint64_t arg2, 2057c88f3f6SAchin Gupta uint64_t arg3, 2067c88f3f6SAchin Gupta uint64_t arg4, 2077c88f3f6SAchin Gupta uint64_t arg5, 2087c88f3f6SAchin Gupta uint64_t arg6, 2097c88f3f6SAchin Gupta uint64_t arg7) 2107c88f3f6SAchin Gupta { 211caff3c87SAlexei Fedorov uint128_t service_args; 212caff3c87SAlexei Fedorov uint64_t service_arg0; 213caff3c87SAlexei Fedorov uint64_t service_arg1; 214916a2c1eSAchin Gupta uint64_t results[2]; 215fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 2164d482156SDaniel Boulby u_register_t dit; 2177c88f3f6SAchin Gupta 218916a2c1eSAchin Gupta /* Update this cpu's statistics */ 219916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count++; 220916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count++; 2217c88f3f6SAchin Gupta 2224ce3e99aSScott Branden INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(), 22316292f54SDavid Cunado ((func >> 31) & 1) == 1 ? "fast" : "yielding", 2246ad2e461SDan Handley func); 225fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(), 226916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count, 227916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count); 228916a2c1eSAchin Gupta 229916a2c1eSAchin Gupta /* Render secure services and obtain results here */ 2307c88f3f6SAchin Gupta results[0] = arg1; 2317c88f3f6SAchin Gupta results[1] = arg2; 2327c88f3f6SAchin Gupta 2337c88f3f6SAchin Gupta /* 234caff3c87SAlexei Fedorov * Request a service back from dispatcher/secure monitor. 235caff3c87SAlexei Fedorov * This call returns and thereafter resumes execution. 2367c88f3f6SAchin Gupta */ 237caff3c87SAlexei Fedorov service_args = tsp_get_magic(); 238caff3c87SAlexei Fedorov service_arg0 = (uint64_t)service_args; 239caff3c87SAlexei Fedorov service_arg1 = (uint64_t)(service_args >> 64U); 2407c88f3f6SAchin Gupta 241*0a33adc0SGovindraj Raja #if ENABLE_FEAT_MTE 2429dd94382SJustin Chadwell /* 2439dd94382SJustin Chadwell * Write a dummy value to an MTE register, to simulate usage in the 2449dd94382SJustin Chadwell * secure world 2459dd94382SJustin Chadwell */ 2469dd94382SJustin Chadwell write_gcr_el1(0x99); 2479dd94382SJustin Chadwell #endif 2489dd94382SJustin Chadwell 2497c88f3f6SAchin Gupta /* Determine the function to perform based on the function ID */ 250239b04faSSoby Mathew switch (TSP_BARE_FID(func)) { 251239b04faSSoby Mathew case TSP_ADD: 252caff3c87SAlexei Fedorov results[0] += service_arg0; 253caff3c87SAlexei Fedorov results[1] += service_arg1; 2547c88f3f6SAchin Gupta break; 255239b04faSSoby Mathew case TSP_SUB: 256caff3c87SAlexei Fedorov results[0] -= service_arg0; 257caff3c87SAlexei Fedorov results[1] -= service_arg1; 2587c88f3f6SAchin Gupta break; 259239b04faSSoby Mathew case TSP_MUL: 260caff3c87SAlexei Fedorov results[0] *= service_arg0; 261caff3c87SAlexei Fedorov results[1] *= service_arg1; 2627c88f3f6SAchin Gupta break; 263239b04faSSoby Mathew case TSP_DIV: 264caff3c87SAlexei Fedorov results[0] /= service_arg0 ? service_arg0 : 1; 265caff3c87SAlexei Fedorov results[1] /= service_arg1 ? service_arg1 : 1; 2667c88f3f6SAchin Gupta break; 2674d482156SDaniel Boulby case TSP_CHECK_DIT: 26888727fc3SAndre Przywara if (!is_feat_dit_supported()) { 2694d482156SDaniel Boulby ERROR("DIT not supported\n"); 2704d482156SDaniel Boulby results[0] = 0; 2714d482156SDaniel Boulby results[1] = 0xffff; 2724d482156SDaniel Boulby break; 2734d482156SDaniel Boulby } 2744d482156SDaniel Boulby dit = read_dit(); 2754d482156SDaniel Boulby results[0] = dit == service_arg0; 2764d482156SDaniel Boulby results[1] = dit; 2774d482156SDaniel Boulby /* Toggle the dit bit */ 2784d482156SDaniel Boulby write_dit(service_arg0 != 0U ? 0 : DIT_BIT); 2794d482156SDaniel Boulby break; 2807c88f3f6SAchin Gupta default: 2817c88f3f6SAchin Gupta break; 2827c88f3f6SAchin Gupta } 2837c88f3f6SAchin Gupta 284239b04faSSoby Mathew return set_smc_args(func, 0, 2857c88f3f6SAchin Gupta results[0], 2867c88f3f6SAchin Gupta results[1], 287239b04faSSoby Mathew 0, 0, 0, 0); 2887c88f3f6SAchin Gupta } 289