Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 1 | /* |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 8 | #include <inttypes.h> |
| 9 | #include <stdint.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 11 | #include <arch_features.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <arch_helpers.h> |
| 13 | #include <bl32/tsp/tsp.h> |
| 14 | #include <common/bl_common.h> |
| 15 | #include <common/debug.h> |
| 16 | #include <lib/spinlock.h> |
| 17 | #include <plat/common/platform.h> |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 18 | #include <platform_def.h> |
Dan Handley | 4fd2f5c | 2014-08-04 11:41:20 +0100 | [diff] [blame] | 19 | #include <platform_tsp.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 20 | |
Dan Handley | e2c27f5 | 2014-08-01 17:58:27 +0100 | [diff] [blame] | 21 | #include "tsp_private.h" |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 22 | |
Vikram Kanigiri | d8c9d26 | 2014-05-16 18:48:12 +0100 | [diff] [blame] | 23 | |
| 24 | /******************************************************************************* |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 25 | * Lock to control access to the console |
| 26 | ******************************************************************************/ |
| 27 | spinlock_t console_lock; |
| 28 | |
| 29 | /******************************************************************************* |
| 30 | * Per cpu data structure to populate parameters for an SMC in C code and use |
| 31 | * a pointer to this structure in assembler code to populate x0-x7 |
| 32 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 33 | static tsp_args_t tsp_smc_args[PLATFORM_CORE_COUNT]; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 34 | |
| 35 | /******************************************************************************* |
| 36 | * Per cpu data structure to keep track of TSP activity |
| 37 | ******************************************************************************/ |
Achin Gupta | 7671789 | 2014-05-09 11:42:56 +0100 | [diff] [blame] | 38 | work_statistics_t tsp_stats[PLATFORM_CORE_COUNT]; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 39 | |
| 40 | /******************************************************************************* |
Sandrine Bailleux | bdba5e5 | 2016-06-16 14:24:26 +0100 | [diff] [blame] | 41 | * The TSP memory footprint starts at address BL32_BASE and ends with the |
| 42 | * linker symbol __BL32_END__. Use these addresses to compute the TSP image |
| 43 | * size. |
Vikram Kanigiri | d8c9d26 | 2014-05-16 18:48:12 +0100 | [diff] [blame] | 44 | ******************************************************************************/ |
Antonio Nino Diaz | de97ff3 | 2019-01-25 13:28:38 +0000 | [diff] [blame] | 45 | #define BL32_TOTAL_LIMIT BL32_END |
Sandrine Bailleux | bdba5e5 | 2016-06-16 14:24:26 +0100 | [diff] [blame] | 46 | #define BL32_TOTAL_SIZE (BL32_TOTAL_LIMIT - (unsigned long) BL32_BASE) |
Vikram Kanigiri | d8c9d26 | 2014-05-16 18:48:12 +0100 | [diff] [blame] | 47 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 48 | static tsp_args_t *set_smc_args(uint64_t arg0, |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 49 | uint64_t arg1, |
| 50 | uint64_t arg2, |
| 51 | uint64_t arg3, |
| 52 | uint64_t arg4, |
| 53 | uint64_t arg5, |
| 54 | uint64_t arg6, |
| 55 | uint64_t arg7) |
| 56 | { |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 57 | uint32_t linear_id; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 58 | tsp_args_t *pcpu_smc_args; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Return to Secure Monitor by raising an SMC. The results of the |
| 62 | * service are passed as an arguments to the SMC |
| 63 | */ |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 64 | linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 65 | pcpu_smc_args = &tsp_smc_args[linear_id]; |
| 66 | write_sp_arg(pcpu_smc_args, TSP_ARG0, arg0); |
| 67 | write_sp_arg(pcpu_smc_args, TSP_ARG1, arg1); |
| 68 | write_sp_arg(pcpu_smc_args, TSP_ARG2, arg2); |
| 69 | write_sp_arg(pcpu_smc_args, TSP_ARG3, arg3); |
| 70 | write_sp_arg(pcpu_smc_args, TSP_ARG4, arg4); |
| 71 | write_sp_arg(pcpu_smc_args, TSP_ARG5, arg5); |
| 72 | write_sp_arg(pcpu_smc_args, TSP_ARG6, arg6); |
| 73 | write_sp_arg(pcpu_smc_args, TSP_ARG7, arg7); |
| 74 | |
| 75 | return pcpu_smc_args; |
| 76 | } |
| 77 | |
| 78 | /******************************************************************************* |
Antonio Nino Diaz | e61ece0 | 2019-02-26 11:41:03 +0000 | [diff] [blame] | 79 | * Setup function for TSP. |
| 80 | ******************************************************************************/ |
| 81 | void tsp_setup(void) |
| 82 | { |
| 83 | /* Perform early platform-specific setup */ |
| 84 | tsp_early_platform_setup(); |
| 85 | |
Antonio Nino Diaz | e61ece0 | 2019-02-26 11:41:03 +0000 | [diff] [blame] | 86 | /* Perform late platform-specific setup */ |
| 87 | tsp_plat_arch_setup(); |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 88 | |
| 89 | #if ENABLE_PAUTH |
| 90 | /* |
| 91 | * Assert that the ARMv8.3-PAuth registers are present or an access |
| 92 | * fault will be triggered when they are being saved or restored. |
| 93 | */ |
| 94 | assert(is_armv8_3_pauth_present()); |
| 95 | #endif /* ENABLE_PAUTH */ |
Antonio Nino Diaz | e61ece0 | 2019-02-26 11:41:03 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /******************************************************************************* |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 99 | * TSP main entry point where it gets the opportunity to initialize its secure |
| 100 | * state/applications. Once the state is initialized, it must return to the |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 101 | * SPD with a pointer to the 'tsp_vector_table' jump table. |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 102 | ******************************************************************************/ |
| 103 | uint64_t tsp_main(void) |
| 104 | { |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 105 | NOTICE("TSP: %s\n", version_string); |
| 106 | NOTICE("TSP: %s\n", build_message); |
Sandrine Bailleux | bdba5e5 | 2016-06-16 14:24:26 +0100 | [diff] [blame] | 107 | INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE); |
| 108 | INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE); |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 109 | |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 110 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 111 | |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 112 | /* Initialize the platform */ |
Dan Handley | 4fd2f5c | 2014-08-04 11:41:20 +0100 | [diff] [blame] | 113 | tsp_platform_setup(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 114 | |
| 115 | /* Initialize secure/applications state here */ |
Achin Gupta | bbc33f2 | 2014-05-09 13:33:42 +0100 | [diff] [blame] | 116 | tsp_generic_timer_start(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 117 | |
| 118 | /* Update this cpu's statistics */ |
| 119 | tsp_stats[linear_id].smc_count++; |
| 120 | tsp_stats[linear_id].eret_count++; |
| 121 | tsp_stats[linear_id].cpu_on_count++; |
| 122 | |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 123 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 124 | spin_lock(&console_lock); |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 125 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", |
| 126 | read_mpidr(), |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 127 | tsp_stats[linear_id].smc_count, |
| 128 | tsp_stats[linear_id].eret_count, |
| 129 | tsp_stats[linear_id].cpu_on_count); |
| 130 | spin_unlock(&console_lock); |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 131 | #endif |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 132 | return (uint64_t) &tsp_vector_table; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /******************************************************************************* |
| 136 | * This function performs any remaining book keeping in the test secure payload |
| 137 | * after this cpu's architectural state has been setup in response to an earlier |
| 138 | * psci cpu_on request. |
| 139 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 140 | tsp_args_t *tsp_cpu_on_main(void) |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 141 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 142 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 143 | |
Achin Gupta | bbc33f2 | 2014-05-09 13:33:42 +0100 | [diff] [blame] | 144 | /* Initialize secure/applications state here */ |
| 145 | tsp_generic_timer_start(); |
| 146 | |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 147 | /* Update this cpu's statistics */ |
| 148 | tsp_stats[linear_id].smc_count++; |
| 149 | tsp_stats[linear_id].eret_count++; |
| 150 | tsp_stats[linear_id].cpu_on_count++; |
| 151 | |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 152 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 153 | spin_lock(&console_lock); |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 154 | INFO("TSP: cpu 0x%lx turned on\n", read_mpidr()); |
| 155 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n", |
| 156 | read_mpidr(), |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 157 | tsp_stats[linear_id].smc_count, |
| 158 | tsp_stats[linear_id].eret_count, |
| 159 | tsp_stats[linear_id].cpu_on_count); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 160 | spin_unlock(&console_lock); |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 161 | #endif |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 162 | /* Indicate to the SPD that we have completed turned ourselves on */ |
| 163 | return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 164 | } |
| 165 | |
| 166 | /******************************************************************************* |
| 167 | * This function performs any remaining book keeping in the test secure payload |
| 168 | * before this cpu is turned off in response to a psci cpu_off request. |
| 169 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 170 | tsp_args_t *tsp_cpu_off_main(uint64_t arg0, |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 171 | uint64_t arg1, |
| 172 | uint64_t arg2, |
| 173 | uint64_t arg3, |
| 174 | uint64_t arg4, |
| 175 | uint64_t arg5, |
| 176 | uint64_t arg6, |
| 177 | uint64_t arg7) |
| 178 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 179 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 180 | |
Achin Gupta | bbc33f2 | 2014-05-09 13:33:42 +0100 | [diff] [blame] | 181 | /* |
| 182 | * This cpu is being turned off, so disable the timer to prevent the |
| 183 | * secure timer interrupt from interfering with power down. A pending |
| 184 | * interrupt will be lost but we do not care as we are turning off. |
| 185 | */ |
| 186 | tsp_generic_timer_stop(); |
| 187 | |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 188 | /* Update this cpu's statistics */ |
| 189 | tsp_stats[linear_id].smc_count++; |
| 190 | tsp_stats[linear_id].eret_count++; |
| 191 | tsp_stats[linear_id].cpu_off_count++; |
| 192 | |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 193 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 194 | spin_lock(&console_lock); |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 195 | INFO("TSP: cpu 0x%lx off request\n", read_mpidr()); |
| 196 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n", |
| 197 | read_mpidr(), |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 198 | tsp_stats[linear_id].smc_count, |
| 199 | tsp_stats[linear_id].eret_count, |
| 200 | tsp_stats[linear_id].cpu_off_count); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 201 | spin_unlock(&console_lock); |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 202 | #endif |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 203 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 204 | /* Indicate to the SPD that we have completed this request */ |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 205 | return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 206 | } |
| 207 | |
| 208 | /******************************************************************************* |
| 209 | * This function performs any book keeping in the test secure payload before |
| 210 | * this cpu's architectural state is saved in response to an earlier psci |
| 211 | * cpu_suspend request. |
| 212 | ******************************************************************************/ |
Soby Mathew | f512157 | 2014-09-30 11:19:51 +0100 | [diff] [blame] | 213 | tsp_args_t *tsp_cpu_suspend_main(uint64_t arg0, |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 214 | uint64_t arg1, |
| 215 | uint64_t arg2, |
| 216 | uint64_t arg3, |
| 217 | uint64_t arg4, |
| 218 | uint64_t arg5, |
| 219 | uint64_t arg6, |
| 220 | uint64_t arg7) |
| 221 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 222 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 223 | |
Achin Gupta | bbc33f2 | 2014-05-09 13:33:42 +0100 | [diff] [blame] | 224 | /* |
| 225 | * Save the time context and disable it to prevent the secure timer |
| 226 | * interrupt from interfering with wakeup from the suspend state. |
| 227 | */ |
| 228 | tsp_generic_timer_save(); |
| 229 | tsp_generic_timer_stop(); |
| 230 | |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 231 | /* Update this cpu's statistics */ |
| 232 | tsp_stats[linear_id].smc_count++; |
| 233 | tsp_stats[linear_id].eret_count++; |
| 234 | tsp_stats[linear_id].cpu_suspend_count++; |
| 235 | |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 236 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 237 | spin_lock(&console_lock); |
Sandrine Bailleux | 8723adf | 2015-02-05 15:42:31 +0000 | [diff] [blame] | 238 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n", |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 239 | read_mpidr(), |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 240 | tsp_stats[linear_id].smc_count, |
| 241 | tsp_stats[linear_id].eret_count, |
| 242 | tsp_stats[linear_id].cpu_suspend_count); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 243 | spin_unlock(&console_lock); |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 244 | #endif |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 245 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 246 | /* Indicate to the SPD that we have completed this request */ |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 247 | return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 248 | } |
| 249 | |
| 250 | /******************************************************************************* |
| 251 | * This function performs any book keeping in the test secure payload after this |
| 252 | * cpu's architectural state has been restored after wakeup from an earlier psci |
| 253 | * cpu_suspend request. |
| 254 | ******************************************************************************/ |
Achin Gupta | 9a0ff9b | 2015-09-07 20:43:27 +0100 | [diff] [blame] | 255 | tsp_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl, |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 256 | uint64_t arg1, |
| 257 | uint64_t arg2, |
| 258 | uint64_t arg3, |
| 259 | uint64_t arg4, |
| 260 | uint64_t arg5, |
| 261 | uint64_t arg6, |
| 262 | uint64_t arg7) |
| 263 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 264 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 265 | |
Achin Gupta | bbc33f2 | 2014-05-09 13:33:42 +0100 | [diff] [blame] | 266 | /* Restore the generic timer context */ |
| 267 | tsp_generic_timer_restore(); |
| 268 | |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 269 | /* Update this cpu's statistics */ |
| 270 | tsp_stats[linear_id].smc_count++; |
| 271 | tsp_stats[linear_id].eret_count++; |
| 272 | tsp_stats[linear_id].cpu_resume_count++; |
| 273 | |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 274 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 275 | spin_lock(&console_lock); |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 276 | INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n", |
Achin Gupta | 9a0ff9b | 2015-09-07 20:43:27 +0100 | [diff] [blame] | 277 | read_mpidr(), max_off_pwrlvl); |
Manish Pandey | c4b47a2 | 2020-03-06 14:36:25 +0000 | [diff] [blame] | 278 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n", |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 279 | read_mpidr(), |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 280 | tsp_stats[linear_id].smc_count, |
| 281 | tsp_stats[linear_id].eret_count, |
Manish Pandey | c4b47a2 | 2020-03-06 14:36:25 +0000 | [diff] [blame] | 282 | tsp_stats[linear_id].cpu_resume_count); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 283 | spin_unlock(&console_lock); |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 284 | #endif |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 285 | /* Indicate to the SPD that we have completed this request */ |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 286 | return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 287 | } |
| 288 | |
| 289 | /******************************************************************************* |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 290 | * This function performs any remaining bookkeeping in the test secure payload |
| 291 | * before the system is switched off (in response to a psci SYSTEM_OFF request) |
| 292 | ******************************************************************************/ |
| 293 | tsp_args_t *tsp_system_off_main(uint64_t arg0, |
| 294 | uint64_t arg1, |
| 295 | uint64_t arg2, |
| 296 | uint64_t arg3, |
| 297 | uint64_t arg4, |
| 298 | uint64_t arg5, |
| 299 | uint64_t arg6, |
| 300 | uint64_t arg7) |
| 301 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 302 | uint32_t linear_id = plat_my_core_pos(); |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 303 | |
| 304 | /* Update this cpu's statistics */ |
| 305 | tsp_stats[linear_id].smc_count++; |
| 306 | tsp_stats[linear_id].eret_count++; |
| 307 | |
| 308 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
| 309 | spin_lock(&console_lock); |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 310 | INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr()); |
| 311 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(), |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 312 | tsp_stats[linear_id].smc_count, |
| 313 | tsp_stats[linear_id].eret_count); |
| 314 | spin_unlock(&console_lock); |
| 315 | #endif |
| 316 | |
| 317 | /* Indicate to the SPD that we have completed this request */ |
| 318 | return set_smc_args(TSP_SYSTEM_OFF_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 319 | } |
| 320 | |
| 321 | /******************************************************************************* |
| 322 | * This function performs any remaining bookkeeping in the test secure payload |
| 323 | * before the system is reset (in response to a psci SYSTEM_RESET request) |
| 324 | ******************************************************************************/ |
| 325 | tsp_args_t *tsp_system_reset_main(uint64_t arg0, |
| 326 | uint64_t arg1, |
| 327 | uint64_t arg2, |
| 328 | uint64_t arg3, |
| 329 | uint64_t arg4, |
| 330 | uint64_t arg5, |
| 331 | uint64_t arg6, |
| 332 | uint64_t arg7) |
| 333 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 334 | uint32_t linear_id = plat_my_core_pos(); |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 335 | |
| 336 | /* Update this cpu's statistics */ |
| 337 | tsp_stats[linear_id].smc_count++; |
| 338 | tsp_stats[linear_id].eret_count++; |
| 339 | |
| 340 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
| 341 | spin_lock(&console_lock); |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 342 | INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr()); |
| 343 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(), |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 344 | tsp_stats[linear_id].smc_count, |
| 345 | tsp_stats[linear_id].eret_count); |
| 346 | spin_unlock(&console_lock); |
| 347 | #endif |
| 348 | |
| 349 | /* Indicate to the SPD that we have completed this request */ |
| 350 | return set_smc_args(TSP_SYSTEM_RESET_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 351 | } |
| 352 | |
| 353 | /******************************************************************************* |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 354 | * TSP fast smc handler. The secure monitor jumps to this function by |
| 355 | * doing the ERET after populating X0-X7 registers. The arguments are received |
| 356 | * in the function arguments in order. Once the service is rendered, this |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 357 | * function returns to Secure Monitor by raising SMC. |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 358 | ******************************************************************************/ |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 359 | tsp_args_t *tsp_smc_handler(uint64_t func, |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 360 | uint64_t arg1, |
| 361 | uint64_t arg2, |
| 362 | uint64_t arg3, |
| 363 | uint64_t arg4, |
| 364 | uint64_t arg5, |
| 365 | uint64_t arg6, |
| 366 | uint64_t arg7) |
| 367 | { |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 368 | uint128_t service_args; |
| 369 | uint64_t service_arg0; |
| 370 | uint64_t service_arg1; |
Achin Gupta | 916a2c1 | 2014-02-09 23:11:46 +0000 | [diff] [blame] | 371 | uint64_t results[2]; |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 372 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 373 | |
Achin Gupta | 916a2c1 | 2014-02-09 23:11:46 +0000 | [diff] [blame] | 374 | /* Update this cpu's statistics */ |
| 375 | tsp_stats[linear_id].smc_count++; |
| 376 | tsp_stats[linear_id].eret_count++; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 377 | |
Madhukar Pappireddy | 2598c41 | 2020-03-20 01:46:21 -0500 | [diff] [blame] | 378 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
| 379 | spin_lock(&console_lock); |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 380 | INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(), |
David Cunado | 28f69ab | 2017-04-05 11:34:03 +0100 | [diff] [blame] | 381 | ((func >> 31) & 1) == 1 ? "fast" : "yielding", |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 382 | func); |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 383 | INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(), |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 384 | tsp_stats[linear_id].smc_count, |
| 385 | tsp_stats[linear_id].eret_count); |
Madhukar Pappireddy | 2598c41 | 2020-03-20 01:46:21 -0500 | [diff] [blame] | 386 | spin_unlock(&console_lock); |
| 387 | #endif |
Achin Gupta | 916a2c1 | 2014-02-09 23:11:46 +0000 | [diff] [blame] | 388 | |
| 389 | /* Render secure services and obtain results here */ |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 390 | results[0] = arg1; |
| 391 | results[1] = arg2; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 392 | |
| 393 | /* |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 394 | * Request a service back from dispatcher/secure monitor. |
| 395 | * This call returns and thereafter resumes execution. |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 396 | */ |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 397 | service_args = tsp_get_magic(); |
| 398 | service_arg0 = (uint64_t)service_args; |
| 399 | service_arg1 = (uint64_t)(service_args >> 64U); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 400 | |
Justin Chadwell | 1c7c13a | 2019-07-18 14:25:33 +0100 | [diff] [blame] | 401 | #if CTX_INCLUDE_MTE_REGS |
| 402 | /* |
| 403 | * Write a dummy value to an MTE register, to simulate usage in the |
| 404 | * secure world |
| 405 | */ |
| 406 | write_gcr_el1(0x99); |
| 407 | #endif |
| 408 | |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 409 | /* Determine the function to perform based on the function ID */ |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 410 | switch (TSP_BARE_FID(func)) { |
| 411 | case TSP_ADD: |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 412 | results[0] += service_arg0; |
| 413 | results[1] += service_arg1; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 414 | break; |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 415 | case TSP_SUB: |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 416 | results[0] -= service_arg0; |
| 417 | results[1] -= service_arg1; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 418 | break; |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 419 | case TSP_MUL: |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 420 | results[0] *= service_arg0; |
| 421 | results[1] *= service_arg1; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 422 | break; |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 423 | case TSP_DIV: |
Alexei Fedorov | 7d616ee | 2020-11-13 12:36:49 +0000 | [diff] [blame] | 424 | results[0] /= service_arg0 ? service_arg0 : 1; |
| 425 | results[1] /= service_arg1 ? service_arg1 : 1; |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 426 | break; |
| 427 | default: |
| 428 | break; |
| 429 | } |
| 430 | |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 431 | return set_smc_args(func, 0, |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 432 | results[0], |
| 433 | results[1], |
Soby Mathew | 9f71f70 | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 434 | 0, 0, 0, 0); |
Achin Gupta | 7c88f3f | 2014-02-18 18:09:12 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 437 | /******************************************************************************* |
Paul Beesley | 1fbc97b | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 438 | * TSP smc abort handler. This function is called when aborting a preempted |
David Cunado | 28f69ab | 2017-04-05 11:34:03 +0100 | [diff] [blame] | 439 | * yielding SMC request. It should cleanup all resources owned by the SMC |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 440 | * handler such as locks or dynamically allocated memory so following SMC |
| 441 | * request are executed in a clean environment. |
| 442 | ******************************************************************************/ |
| 443 | tsp_args_t *tsp_abort_smc_handler(uint64_t func, |
| 444 | uint64_t arg1, |
| 445 | uint64_t arg2, |
| 446 | uint64_t arg3, |
| 447 | uint64_t arg4, |
| 448 | uint64_t arg5, |
| 449 | uint64_t arg6, |
| 450 | uint64_t arg7) |
| 451 | { |
| 452 | return set_smc_args(TSP_ABORT_DONE, 0, 0, 0, 0, 0, 0, 0); |
| 453 | } |