Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 1 | /* |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 31 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 32 | #include <assert.h> |
| 33 | #include <bl_common.h> |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 34 | #include <context_mgmt.h> |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 35 | #include <debug.h> |
Dan Handley | ed6ff95 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 36 | #include <platform.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 37 | #include <tsp.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 38 | #include "tspd_private.h" |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 39 | |
| 40 | /******************************************************************************* |
| 41 | * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions |
| 42 | * needed. Nothing at the moment. |
| 43 | ******************************************************************************/ |
| 44 | static void tspd_cpu_on_handler(uint64_t target_cpu) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /******************************************************************************* |
| 49 | * This cpu is being turned off. Allow the TSPD/TSP to perform any actions |
| 50 | * needed |
| 51 | ******************************************************************************/ |
Soby Mathew | f512157 | 2014-09-30 11:19:51 +0100 | [diff] [blame] | 52 | static int32_t tspd_cpu_off_handler(uint64_t unused) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 53 | { |
| 54 | int32_t rc = 0; |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 55 | uint32_t linear_id = plat_my_core_pos(); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 56 | tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 57 | |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 58 | assert(tsp_vectors); |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 59 | assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 60 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Abort any preempted SMC request before overwriting the SECURE |
| 63 | * context. |
| 64 | */ |
| 65 | tspd_abort_preempted_smc(tsp_ctx); |
| 66 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 67 | /* Program the entry point and enter the TSP */ |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 68 | cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 69 | rc = tspd_synchronous_sp_entry(tsp_ctx); |
| 70 | |
| 71 | /* |
| 72 | * Read the response from the TSP. A non-zero return means that |
| 73 | * something went wrong while communicating with the TSP. |
| 74 | */ |
| 75 | if (rc != 0) |
| 76 | panic(); |
| 77 | |
| 78 | /* |
| 79 | * Reset TSP's context for a fresh start when this cpu is turned on |
| 80 | * subsequently. |
| 81 | */ |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 82 | set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 83 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 84 | return 0; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /******************************************************************************* |
| 88 | * This cpu is being suspended. S-EL1 state must have been saved in the |
| 89 | * resident cpu (mpidr format) if it is a UP/UP migratable TSP. |
| 90 | ******************************************************************************/ |
Achin Gupta | 9a0ff9b | 2015-09-07 20:43:27 +0100 | [diff] [blame] | 91 | static void tspd_cpu_suspend_handler(uint64_t max_off_pwrlvl) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 92 | { |
| 93 | int32_t rc = 0; |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 94 | uint32_t linear_id = plat_my_core_pos(); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 95 | tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 96 | |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 97 | assert(tsp_vectors); |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 98 | assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 99 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 100 | /* |
| 101 | * Abort any preempted SMC request before overwriting the SECURE |
| 102 | * context. |
| 103 | */ |
| 104 | tspd_abort_preempted_smc(tsp_ctx); |
| 105 | |
Soby Mathew | f512157 | 2014-09-30 11:19:51 +0100 | [diff] [blame] | 106 | /* Program the entry point and enter the TSP */ |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 107 | cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 108 | rc = tspd_synchronous_sp_entry(tsp_ctx); |
| 109 | |
| 110 | /* |
| 111 | * Read the response from the TSP. A non-zero return means that |
| 112 | * something went wrong while communicating with the TSP. |
| 113 | */ |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 114 | if (rc) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 115 | panic(); |
| 116 | |
| 117 | /* Update its context to reflect the state the TSP is in */ |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 118 | set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /******************************************************************************* |
| 122 | * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 123 | * before passing control back to the Secure Monitor. Entry in S-EL1 is done |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 124 | * after initialising minimal architectural state that guarantees safe |
| 125 | * execution. |
| 126 | ******************************************************************************/ |
Soby Mathew | f512157 | 2014-09-30 11:19:51 +0100 | [diff] [blame] | 127 | static void tspd_cpu_on_finish_handler(uint64_t unused) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 128 | { |
| 129 | int32_t rc = 0; |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 130 | uint32_t linear_id = plat_my_core_pos(); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 131 | tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; |
Vikram Kanigiri | 9d70f0f | 2014-07-15 16:46:43 +0100 | [diff] [blame] | 132 | entry_point_info_t tsp_on_entrypoint; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 133 | |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 134 | assert(tsp_vectors); |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 135 | assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 136 | |
Vikram Kanigiri | 9d70f0f | 2014-07-15 16:46:43 +0100 | [diff] [blame] | 137 | tspd_init_tsp_ep_state(&tsp_on_entrypoint, |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 138 | TSP_AARCH64, |
Vikram Kanigiri | 9d70f0f | 2014-07-15 16:46:43 +0100 | [diff] [blame] | 139 | (uint64_t) &tsp_vectors->cpu_on_entry, |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 140 | tsp_ctx); |
| 141 | |
Vikram Kanigiri | 9d70f0f | 2014-07-15 16:46:43 +0100 | [diff] [blame] | 142 | /* Initialise this cpu's secure context */ |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 143 | cm_init_my_context(&tsp_on_entrypoint); |
Vikram Kanigiri | 9d70f0f | 2014-07-15 16:46:43 +0100 | [diff] [blame] | 144 | |
Soby Mathew | bec9851 | 2015-09-03 18:29:38 +0100 | [diff] [blame] | 145 | #if TSP_NS_INTR_ASYNC_PREEMPT |
Soby Mathew | 47903c0 | 2015-01-13 15:48:26 +0000 | [diff] [blame] | 146 | /* |
| 147 | * Disable the NS interrupt locally since it will be enabled globally |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 148 | * within cm_init_my_context. |
Soby Mathew | 47903c0 | 2015-01-13 15:48:26 +0000 | [diff] [blame] | 149 | */ |
| 150 | disable_intr_rm_local(INTR_TYPE_NS, SECURE); |
| 151 | #endif |
| 152 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 153 | /* Enter the TSP */ |
| 154 | rc = tspd_synchronous_sp_entry(tsp_ctx); |
| 155 | |
| 156 | /* |
| 157 | * Read the response from the TSP. A non-zero return means that |
| 158 | * something went wrong while communicating with the SP. |
| 159 | */ |
| 160 | if (rc != 0) |
| 161 | panic(); |
| 162 | |
| 163 | /* Update its context to reflect the state the SP is in */ |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 164 | set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /******************************************************************************* |
| 168 | * This cpu has resumed from suspend. The SPD saved the TSP context when it |
| 169 | * completed the preceding suspend call. Use that context to program an entry |
| 170 | * into the TSP to allow it to do any remaining book keeping |
| 171 | ******************************************************************************/ |
Achin Gupta | 9a0ff9b | 2015-09-07 20:43:27 +0100 | [diff] [blame] | 172 | static void tspd_cpu_suspend_finish_handler(uint64_t max_off_pwrlvl) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 173 | { |
| 174 | int32_t rc = 0; |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 175 | uint32_t linear_id = plat_my_core_pos(); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 176 | tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 177 | |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 178 | assert(tsp_vectors); |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 179 | assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 180 | |
Achin Gupta | 9a0ff9b | 2015-09-07 20:43:27 +0100 | [diff] [blame] | 181 | /* Program the entry point, max_off_pwrlvl and enter the SP */ |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 182 | write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx), |
| 183 | CTX_GPREG_X0, |
Achin Gupta | 9a0ff9b | 2015-09-07 20:43:27 +0100 | [diff] [blame] | 184 | max_off_pwrlvl); |
Andrew Thoelke | 891c4ca | 2014-05-20 21:43:27 +0100 | [diff] [blame] | 185 | cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 186 | rc = tspd_synchronous_sp_entry(tsp_ctx); |
| 187 | |
| 188 | /* |
| 189 | * Read the response from the TSP. A non-zero return means that |
| 190 | * something went wrong while communicating with the TSP. |
| 191 | */ |
| 192 | if (rc != 0) |
| 193 | panic(); |
| 194 | |
| 195 | /* Update its context to reflect the state the SP is in */ |
Achin Gupta | 18d6eaf | 2014-05-04 18:23:26 +0100 | [diff] [blame] | 196 | set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /******************************************************************************* |
| 200 | * Return the type of TSP the TSPD is dealing with. Report the current resident |
| 201 | * cpu (mpidr format) if it is a UP/UP migratable TSP. |
| 202 | ******************************************************************************/ |
| 203 | static int32_t tspd_cpu_migrate_info(uint64_t *resident_cpu) |
| 204 | { |
| 205 | return TSP_MIGRATE_INFO; |
| 206 | } |
| 207 | |
| 208 | /******************************************************************************* |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 209 | * System is about to be switched off. Allow the TSPD/TSP to perform |
| 210 | * any actions needed. |
| 211 | ******************************************************************************/ |
| 212 | static void tspd_system_off(void) |
| 213 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 214 | uint32_t linear_id = plat_my_core_pos(); |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 215 | tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; |
| 216 | |
| 217 | assert(tsp_vectors); |
| 218 | assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON); |
| 219 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 220 | /* |
| 221 | * Abort any preempted SMC request before overwriting the SECURE |
| 222 | * context. |
| 223 | */ |
| 224 | tspd_abort_preempted_smc(tsp_ctx); |
| 225 | |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 226 | /* Program the entry point */ |
| 227 | cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_off_entry); |
| 228 | |
| 229 | /* Enter the TSP. We do not care about the return value because we |
| 230 | * must continue the shutdown anyway */ |
| 231 | tspd_synchronous_sp_entry(tsp_ctx); |
| 232 | } |
| 233 | |
| 234 | /******************************************************************************* |
| 235 | * System is about to be reset. Allow the TSPD/TSP to perform |
| 236 | * any actions needed. |
| 237 | ******************************************************************************/ |
| 238 | static void tspd_system_reset(void) |
| 239 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 240 | uint32_t linear_id = plat_my_core_pos(); |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 241 | tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; |
| 242 | |
| 243 | assert(tsp_vectors); |
| 244 | assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON); |
| 245 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 246 | /* |
| 247 | * Abort any preempted SMC request before overwriting the SECURE |
| 248 | * context. |
| 249 | */ |
| 250 | tspd_abort_preempted_smc(tsp_ctx); |
| 251 | |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 252 | /* Program the entry point */ |
| 253 | cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry); |
| 254 | |
Douglas Raillard | f212965 | 2016-11-24 15:43:19 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Enter the TSP. We do not care about the return value because we |
| 257 | * must continue the reset anyway |
| 258 | */ |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 259 | tspd_synchronous_sp_entry(tsp_ctx); |
| 260 | } |
| 261 | |
| 262 | /******************************************************************************* |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 263 | * Structure populated by the TSP Dispatcher to be given a chance to perform any |
| 264 | * TSP bookkeeping before PSCI executes a power mgmt. operation. |
| 265 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 266 | const spd_pm_ops_t tspd_pm = { |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 267 | .svc_on = tspd_cpu_on_handler, |
| 268 | .svc_off = tspd_cpu_off_handler, |
| 269 | .svc_suspend = tspd_cpu_suspend_handler, |
| 270 | .svc_on_finish = tspd_cpu_on_finish_handler, |
| 271 | .svc_suspend_finish = tspd_cpu_suspend_finish_handler, |
| 272 | .svc_migrate = NULL, |
| 273 | .svc_migrate_info = tspd_cpu_migrate_info, |
| 274 | .svc_system_off = tspd_system_off, |
| 275 | .svc_system_reset = tspd_system_reset |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 276 | }; |