Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 1 | /* |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020-2021, ARM Limited and Contributors. All rights reserved. |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 9 | #include <inttypes.h> |
| 10 | #include <stdint.h> |
| 11 | |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 12 | #include <lib/el3_runtime/context_mgmt.h> |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 13 | #include <lib/spinlock.h> |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 14 | #include "spmd_private.h" |
| 15 | |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 16 | static struct { |
| 17 | bool secondary_ep_locked; |
| 18 | uintptr_t secondary_ep; |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 19 | spinlock_t lock; |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 20 | } g_spmd_pm; |
| 21 | |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 22 | /******************************************************************************* |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 23 | * spmd_build_spmc_message |
| 24 | * |
| 25 | * Builds an SPMD to SPMC direct message request. |
| 26 | ******************************************************************************/ |
| 27 | static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message) |
| 28 | { |
| 29 | write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); |
| 30 | write_ctx_reg(gpregs, CTX_GPREG_X1, |
| 31 | (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | |
| 32 | spmd_spmc_id_get()); |
| 33 | write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ); |
| 34 | write_ctx_reg(gpregs, CTX_GPREG_X3, message); |
| 35 | } |
| 36 | |
| 37 | /******************************************************************************* |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 38 | * spmd_pm_secondary_ep_register |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 39 | ******************************************************************************/ |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 40 | int spmd_pm_secondary_ep_register(uintptr_t entry_point) |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 41 | { |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 42 | int ret = FFA_ERROR_INVALID_PARAMETER; |
| 43 | |
| 44 | spin_lock(&g_spmd_pm.lock); |
| 45 | |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 46 | if (g_spmd_pm.secondary_ep_locked == true) { |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 47 | goto out; |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 50 | /* |
| 51 | * Check entry_point address is a PA within |
| 52 | * load_address <= entry_point < load_address + binary_size |
| 53 | */ |
| 54 | if (!spmd_check_address_in_binary_image(entry_point)) { |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 55 | ERROR("%s entry point is not within image boundaries\n", |
| 56 | __func__); |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 57 | goto out; |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 60 | g_spmd_pm.secondary_ep = entry_point; |
| 61 | g_spmd_pm.secondary_ep_locked = true; |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 62 | |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 63 | VERBOSE("%s %lx\n", __func__, entry_point); |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 64 | |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 65 | ret = 0; |
| 66 | |
| 67 | out: |
| 68 | spin_unlock(&g_spmd_pm.lock); |
| 69 | |
| 70 | return ret; |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 73 | /******************************************************************************* |
| 74 | * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part |
| 75 | * of the SPMC initialization path, they will initialize any SPs that they |
| 76 | * manage. Entry into SPMC is done after initialising minimal architectural |
| 77 | * state that guarantees safe execution. |
| 78 | ******************************************************************************/ |
| 79 | static void spmd_cpu_on_finish_handler(u_register_t unused) |
| 80 | { |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 81 | spmd_spm_core_context_t *ctx = spmd_get_context(); |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 82 | unsigned int linear_id = plat_my_core_pos(); |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 83 | el3_state_t *el3_state; |
| 84 | uintptr_t entry_point; |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 85 | uint64_t rc; |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 86 | |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 87 | assert(ctx != NULL); |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 88 | assert(ctx->state != SPMC_STATE_ON); |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 89 | |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 90 | spin_lock(&g_spmd_pm.lock); |
| 91 | |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 92 | /* |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 93 | * Leave the possibility that the SPMC does not call |
| 94 | * FFA_SECONDARY_EP_REGISTER in which case re-use the |
| 95 | * primary core address for booting secondary cores. |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 96 | */ |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 97 | if (g_spmd_pm.secondary_ep_locked == true) { |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 98 | /* |
| 99 | * The CPU context has already been initialized at boot time |
| 100 | * (in spmd_spmc_init by a call to cm_setup_context). Adjust |
| 101 | * below the target core entry point based on the address |
| 102 | * passed to by FFA_SECONDARY_EP_REGISTER. |
| 103 | */ |
| 104 | entry_point = g_spmd_pm.secondary_ep; |
| 105 | el3_state = get_el3state_ctx(&ctx->cpu_ctx); |
| 106 | write_ctx_reg(el3_state, CTX_ELR_EL3, entry_point); |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Olivier Deprez | e799f48 | 2021-03-02 17:31:22 +0100 | [diff] [blame] | 109 | spin_unlock(&g_spmd_pm.lock); |
| 110 | |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 111 | /* Mark CPU as initiating ON operation. */ |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 112 | ctx->state = SPMC_STATE_ON_PENDING; |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 113 | |
| 114 | rc = spmd_spm_core_sync_entry(ctx); |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 115 | if (rc != 0ULL) { |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 116 | ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 117 | linear_id); |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 118 | ctx->state = SPMC_STATE_OFF; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | ctx->state = SPMC_STATE_ON; |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 123 | |
| 124 | VERBOSE("CPU %u on!\n", linear_id); |
| 125 | } |
| 126 | |
| 127 | /******************************************************************************* |
| 128 | * spmd_cpu_off_handler |
| 129 | ******************************************************************************/ |
| 130 | static int32_t spmd_cpu_off_handler(u_register_t unused) |
| 131 | { |
| 132 | spmd_spm_core_context_t *ctx = spmd_get_context(); |
| 133 | unsigned int linear_id = plat_my_core_pos(); |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 134 | int64_t rc; |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 135 | |
| 136 | assert(ctx != NULL); |
| 137 | assert(ctx->state != SPMC_STATE_OFF); |
| 138 | |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 139 | /* Build an SPMD to SPMC direct message request. */ |
| 140 | spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF); |
| 141 | |
| 142 | rc = spmd_spm_core_sync_entry(ctx); |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 143 | if (rc != 0ULL) { |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 144 | ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, linear_id); |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 147 | /* Expect a direct message response from the SPMC. */ |
| 148 | u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx), |
| 149 | CTX_GPREG_X0); |
| 150 | if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) { |
| 151 | ERROR("%s invalid SPMC response (%lx).\n", |
| 152 | __func__, ffa_resp_func); |
| 153 | return -EINVAL; |
| 154 | } |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 155 | |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 156 | ctx->state = SPMC_STATE_OFF; |
| 157 | |
| 158 | VERBOSE("CPU %u off!\n", linear_id); |
| 159 | |
| 160 | return 0; |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /******************************************************************************* |
| 164 | * Structure populated by the SPM Dispatcher to perform any bookkeeping before |
| 165 | * PSCI executes a power mgmt. operation. |
| 166 | ******************************************************************************/ |
| 167 | const spd_pm_ops_t spmd_pm = { |
| 168 | .svc_on_finish = spmd_cpu_on_finish_handler, |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 169 | .svc_off = spmd_cpu_off_handler |
Olivier Deprez | be67111 | 2019-10-28 09:07:50 +0000 | [diff] [blame] | 170 | }; |