17c88f3f6SAchin Gupta /* 24d482156SDaniel Boulby * Copyright (c) 2013-2022, 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); 53399fb08fSAndrew Thoelke return (uint64_t) &tsp_vector_table; 547c88f3f6SAchin Gupta } 557c88f3f6SAchin Gupta 567c88f3f6SAchin Gupta /******************************************************************************* 577c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 587c88f3f6SAchin Gupta * after this cpu's architectural state has been setup in response to an earlier 597c88f3f6SAchin Gupta * psci cpu_on request. 607c88f3f6SAchin Gupta ******************************************************************************/ 614a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_on_main(void) 627c88f3f6SAchin Gupta { 63fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 647c88f3f6SAchin Gupta 65a20a81e5SAchin Gupta /* Initialize secure/applications state here */ 66a20a81e5SAchin Gupta tsp_generic_timer_start(); 67a20a81e5SAchin Gupta 687c88f3f6SAchin Gupta /* Update this cpu's statistics */ 697c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 707c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 717c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count++; 727c88f3f6SAchin Gupta 73fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx turned on\n", read_mpidr()); 74fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", 75fd650ff6SSoby Mathew read_mpidr(), 767c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 777c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 787c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_on_count); 797c88f3f6SAchin Gupta /* Indicate to the SPD that we have completed turned ourselves on */ 807c88f3f6SAchin Gupta return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0); 817c88f3f6SAchin Gupta } 827c88f3f6SAchin Gupta 837c88f3f6SAchin Gupta /******************************************************************************* 847c88f3f6SAchin Gupta * This function performs any remaining book keeping in the test secure payload 857c88f3f6SAchin Gupta * before this cpu is turned off in response to a psci cpu_off request. 867c88f3f6SAchin Gupta ******************************************************************************/ 874a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_off_main(uint64_t arg0, 887c88f3f6SAchin Gupta uint64_t arg1, 897c88f3f6SAchin Gupta uint64_t arg2, 907c88f3f6SAchin Gupta uint64_t arg3, 917c88f3f6SAchin Gupta uint64_t arg4, 927c88f3f6SAchin Gupta uint64_t arg5, 937c88f3f6SAchin Gupta uint64_t arg6, 947c88f3f6SAchin Gupta uint64_t arg7) 957c88f3f6SAchin Gupta { 96fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 977c88f3f6SAchin Gupta 98a20a81e5SAchin Gupta /* 99a20a81e5SAchin Gupta * This cpu is being turned off, so disable the timer to prevent the 100a20a81e5SAchin Gupta * secure timer interrupt from interfering with power down. A pending 101a20a81e5SAchin Gupta * interrupt will be lost but we do not care as we are turning off. 102a20a81e5SAchin Gupta */ 103a20a81e5SAchin Gupta tsp_generic_timer_stop(); 104a20a81e5SAchin Gupta 1057c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1067c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1077c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1087c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count++; 1097c88f3f6SAchin Gupta 110fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx off request\n", read_mpidr()); 111fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n", 112fd650ff6SSoby Mathew read_mpidr(), 1137c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1147c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1157c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_off_count); 1167c88f3f6SAchin Gupta 117607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1187c88f3f6SAchin Gupta return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); 1197c88f3f6SAchin Gupta } 1207c88f3f6SAchin Gupta 1217c88f3f6SAchin Gupta /******************************************************************************* 1227c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload before 1237c88f3f6SAchin Gupta * this cpu's architectural state is saved in response to an earlier psci 1247c88f3f6SAchin Gupta * cpu_suspend request. 1257c88f3f6SAchin Gupta ******************************************************************************/ 1264a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_suspend_main(uint64_t arg0, 1277c88f3f6SAchin Gupta uint64_t arg1, 1287c88f3f6SAchin Gupta uint64_t arg2, 1297c88f3f6SAchin Gupta uint64_t arg3, 1307c88f3f6SAchin Gupta uint64_t arg4, 1317c88f3f6SAchin Gupta uint64_t arg5, 1327c88f3f6SAchin Gupta uint64_t arg6, 1337c88f3f6SAchin Gupta uint64_t arg7) 1347c88f3f6SAchin Gupta { 135fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1367c88f3f6SAchin Gupta 137a20a81e5SAchin Gupta /* 138a20a81e5SAchin Gupta * Save the time context and disable it to prevent the secure timer 139a20a81e5SAchin Gupta * interrupt from interfering with wakeup from the suspend state. 140a20a81e5SAchin Gupta */ 141a20a81e5SAchin Gupta tsp_generic_timer_save(); 142a20a81e5SAchin Gupta tsp_generic_timer_stop(); 143a20a81e5SAchin Gupta 1447c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1457c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1467c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1477c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count++; 1487c88f3f6SAchin Gupta 149dad25049SSandrine Bailleux INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n", 150fd650ff6SSoby Mathew read_mpidr(), 1517c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1527c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 1537c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_suspend_count); 1547c88f3f6SAchin Gupta 155607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1567c88f3f6SAchin Gupta return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0); 1577c88f3f6SAchin Gupta } 1587c88f3f6SAchin Gupta 1597c88f3f6SAchin Gupta /******************************************************************************* 1607c88f3f6SAchin Gupta * This function performs any book keeping in the test secure payload after this 1617c88f3f6SAchin Gupta * cpu's architectural state has been restored after wakeup from an earlier psci 1627c88f3f6SAchin Gupta * cpu_suspend request. 1637c88f3f6SAchin Gupta ******************************************************************************/ 1644a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl, 1657c88f3f6SAchin Gupta uint64_t arg1, 1667c88f3f6SAchin Gupta uint64_t arg2, 1677c88f3f6SAchin Gupta uint64_t arg3, 1687c88f3f6SAchin Gupta uint64_t arg4, 1697c88f3f6SAchin Gupta uint64_t arg5, 1707c88f3f6SAchin Gupta uint64_t arg6, 1717c88f3f6SAchin Gupta uint64_t arg7) 1727c88f3f6SAchin Gupta { 173fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 1747c88f3f6SAchin Gupta 175a20a81e5SAchin Gupta /* Restore the generic timer context */ 176a20a81e5SAchin Gupta tsp_generic_timer_restore(); 177a20a81e5SAchin Gupta 1787c88f3f6SAchin Gupta /* Update this cpu's statistics */ 1797c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count++; 1807c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count++; 1817c88f3f6SAchin Gupta tsp_stats[linear_id].cpu_resume_count++; 1827c88f3f6SAchin Gupta 1834ce3e99aSScott Branden INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n", 184f1054c93SAchin Gupta read_mpidr(), max_off_pwrlvl); 185a16bc845SManish Pandey INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n", 186fd650ff6SSoby Mathew read_mpidr(), 1877c88f3f6SAchin Gupta tsp_stats[linear_id].smc_count, 1887c88f3f6SAchin Gupta tsp_stats[linear_id].eret_count, 189a16bc845SManish Pandey tsp_stats[linear_id].cpu_resume_count); 190607084eeSAchin Gupta /* Indicate to the SPD that we have completed this request */ 1917c88f3f6SAchin Gupta return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0); 1927c88f3f6SAchin Gupta } 1937c88f3f6SAchin Gupta 1947c88f3f6SAchin Gupta /******************************************************************************* 1957c88f3f6SAchin Gupta * TSP fast smc handler. The secure monitor jumps to this function by 1967c88f3f6SAchin Gupta * doing the ERET after populating X0-X7 registers. The arguments are received 1977c88f3f6SAchin Gupta * in the function arguments in order. Once the service is rendered, this 198239b04faSSoby Mathew * function returns to Secure Monitor by raising SMC. 1997c88f3f6SAchin Gupta ******************************************************************************/ 2004a8bfdb9SAchin Gupta smc_args_t *tsp_smc_handler(uint64_t func, 2017c88f3f6SAchin Gupta uint64_t arg1, 2027c88f3f6SAchin Gupta uint64_t arg2, 2037c88f3f6SAchin Gupta uint64_t arg3, 2047c88f3f6SAchin Gupta uint64_t arg4, 2057c88f3f6SAchin Gupta uint64_t arg5, 2067c88f3f6SAchin Gupta uint64_t arg6, 2077c88f3f6SAchin Gupta uint64_t arg7) 2087c88f3f6SAchin Gupta { 209caff3c87SAlexei Fedorov uint128_t service_args; 210caff3c87SAlexei Fedorov uint64_t service_arg0; 211caff3c87SAlexei Fedorov uint64_t service_arg1; 212916a2c1eSAchin Gupta uint64_t results[2]; 213fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 2144d482156SDaniel Boulby u_register_t dit; 2157c88f3f6SAchin Gupta 216916a2c1eSAchin Gupta /* Update this cpu's statistics */ 217916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count++; 218916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count++; 2197c88f3f6SAchin Gupta 2204ce3e99aSScott Branden INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(), 22116292f54SDavid Cunado ((func >> 31) & 1) == 1 ? "fast" : "yielding", 2226ad2e461SDan Handley func); 223fd650ff6SSoby Mathew INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(), 224916a2c1eSAchin Gupta tsp_stats[linear_id].smc_count, 225916a2c1eSAchin Gupta tsp_stats[linear_id].eret_count); 226916a2c1eSAchin Gupta 227916a2c1eSAchin Gupta /* Render secure services and obtain results here */ 2287c88f3f6SAchin Gupta results[0] = arg1; 2297c88f3f6SAchin Gupta results[1] = arg2; 2307c88f3f6SAchin Gupta 2317c88f3f6SAchin Gupta /* 232caff3c87SAlexei Fedorov * Request a service back from dispatcher/secure monitor. 233caff3c87SAlexei Fedorov * This call returns and thereafter resumes execution. 2347c88f3f6SAchin Gupta */ 235caff3c87SAlexei Fedorov service_args = tsp_get_magic(); 236caff3c87SAlexei Fedorov service_arg0 = (uint64_t)service_args; 237caff3c87SAlexei Fedorov service_arg1 = (uint64_t)(service_args >> 64U); 2387c88f3f6SAchin Gupta 2399dd94382SJustin Chadwell #if CTX_INCLUDE_MTE_REGS 2409dd94382SJustin Chadwell /* 2419dd94382SJustin Chadwell * Write a dummy value to an MTE register, to simulate usage in the 2429dd94382SJustin Chadwell * secure world 2439dd94382SJustin Chadwell */ 2449dd94382SJustin Chadwell write_gcr_el1(0x99); 2459dd94382SJustin Chadwell #endif 2469dd94382SJustin Chadwell 2477c88f3f6SAchin Gupta /* Determine the function to perform based on the function ID */ 248239b04faSSoby Mathew switch (TSP_BARE_FID(func)) { 249239b04faSSoby Mathew case TSP_ADD: 250caff3c87SAlexei Fedorov results[0] += service_arg0; 251caff3c87SAlexei Fedorov results[1] += service_arg1; 2527c88f3f6SAchin Gupta break; 253239b04faSSoby Mathew case TSP_SUB: 254caff3c87SAlexei Fedorov results[0] -= service_arg0; 255caff3c87SAlexei Fedorov results[1] -= service_arg1; 2567c88f3f6SAchin Gupta break; 257239b04faSSoby Mathew case TSP_MUL: 258caff3c87SAlexei Fedorov results[0] *= service_arg0; 259caff3c87SAlexei Fedorov results[1] *= service_arg1; 2607c88f3f6SAchin Gupta break; 261239b04faSSoby Mathew case TSP_DIV: 262caff3c87SAlexei Fedorov results[0] /= service_arg0 ? service_arg0 : 1; 263caff3c87SAlexei Fedorov results[1] /= service_arg1 ? service_arg1 : 1; 2647c88f3f6SAchin Gupta break; 2654d482156SDaniel Boulby case TSP_CHECK_DIT: 266*88727fc3SAndre Przywara if (!is_feat_dit_supported()) { 2674d482156SDaniel Boulby ERROR("DIT not supported\n"); 2684d482156SDaniel Boulby results[0] = 0; 2694d482156SDaniel Boulby results[1] = 0xffff; 2704d482156SDaniel Boulby break; 2714d482156SDaniel Boulby } 2724d482156SDaniel Boulby dit = read_dit(); 2734d482156SDaniel Boulby results[0] = dit == service_arg0; 2744d482156SDaniel Boulby results[1] = dit; 2754d482156SDaniel Boulby /* Toggle the dit bit */ 2764d482156SDaniel Boulby write_dit(service_arg0 != 0U ? 0 : DIT_BIT); 2774d482156SDaniel Boulby break; 2787c88f3f6SAchin Gupta default: 2797c88f3f6SAchin Gupta break; 2807c88f3f6SAchin Gupta } 2817c88f3f6SAchin Gupta 282239b04faSSoby Mathew return set_smc_args(func, 0, 2837c88f3f6SAchin Gupta results[0], 2847c88f3f6SAchin Gupta results[1], 285239b04faSSoby Mathew 0, 0, 0, 0); 2867c88f3f6SAchin Gupta } 287