blob: ac962eaa2621799416aff49b589a8c77e92f5507 [file] [log] [blame]
Olivier Deprezbe671112019-10-28 09:07:50 +00001/*
Olivier Deprezeae45962021-01-19 15:06:47 +01002 * Copyright (c) 2020-2021, ARM Limited and Contributors. All rights reserved.
Olivier Deprezbe671112019-10-28 09:07:50 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Olivier Deprez33e44122020-04-16 17:54:27 +02008#include <errno.h>
Olivier Deprezc7631a52020-03-23 09:53:06 +01009#include <lib/el3_runtime/context_mgmt.h>
Olivier Depreze799f482021-03-02 17:31:22 +010010#include <lib/spinlock.h>
Olivier Deprezbe671112019-10-28 09:07:50 +000011#include "spmd_private.h"
12
Olivier Deprezeae45962021-01-19 15:06:47 +010013static struct {
14 bool secondary_ep_locked;
15 uintptr_t secondary_ep;
Olivier Depreze799f482021-03-02 17:31:22 +010016 spinlock_t lock;
Olivier Deprezeae45962021-01-19 15:06:47 +010017} g_spmd_pm;
18
Olivier Deprez33e44122020-04-16 17:54:27 +020019/*******************************************************************************
Olivier Deprezc7631a52020-03-23 09:53:06 +010020 * spmd_build_spmc_message
21 *
22 * Builds an SPMD to SPMC direct message request.
23 ******************************************************************************/
24static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message)
25{
26 write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
27 write_ctx_reg(gpregs, CTX_GPREG_X1,
28 (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
29 spmd_spmc_id_get());
30 write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ);
31 write_ctx_reg(gpregs, CTX_GPREG_X3, message);
32}
33
34/*******************************************************************************
Olivier Deprezeae45962021-01-19 15:06:47 +010035 * spmd_pm_secondary_ep_register
Olivier Deprez33e44122020-04-16 17:54:27 +020036 ******************************************************************************/
Olivier Deprezeae45962021-01-19 15:06:47 +010037int spmd_pm_secondary_ep_register(uintptr_t entry_point)
Olivier Deprez33e44122020-04-16 17:54:27 +020038{
Olivier Depreze799f482021-03-02 17:31:22 +010039 int ret = FFA_ERROR_INVALID_PARAMETER;
40
41 spin_lock(&g_spmd_pm.lock);
42
Olivier Deprezeae45962021-01-19 15:06:47 +010043 if (g_spmd_pm.secondary_ep_locked == true) {
Olivier Depreze799f482021-03-02 17:31:22 +010044 goto out;
Olivier Deprez33e44122020-04-16 17:54:27 +020045 }
46
Olivier Deprez33e44122020-04-16 17:54:27 +020047 /*
48 * Check entry_point address is a PA within
49 * load_address <= entry_point < load_address + binary_size
50 */
51 if (!spmd_check_address_in_binary_image(entry_point)) {
Olivier Deprezeae45962021-01-19 15:06:47 +010052 ERROR("%s entry point is not within image boundaries\n",
53 __func__);
Olivier Depreze799f482021-03-02 17:31:22 +010054 goto out;
Olivier Deprez33e44122020-04-16 17:54:27 +020055 }
56
Olivier Deprezeae45962021-01-19 15:06:47 +010057 g_spmd_pm.secondary_ep = entry_point;
58 g_spmd_pm.secondary_ep_locked = true;
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020059
Olivier Deprezeae45962021-01-19 15:06:47 +010060 VERBOSE("%s %lx\n", __func__, entry_point);
Olivier Deprez33e44122020-04-16 17:54:27 +020061
Olivier Depreze799f482021-03-02 17:31:22 +010062 ret = 0;
63
64out:
65 spin_unlock(&g_spmd_pm.lock);
66
67 return ret;
Olivier Deprez33e44122020-04-16 17:54:27 +020068}
69
Olivier Deprezbe671112019-10-28 09:07:50 +000070/*******************************************************************************
71 * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part
72 * of the SPMC initialization path, they will initialize any SPs that they
73 * manage. Entry into SPMC is done after initialising minimal architectural
74 * state that guarantees safe execution.
75 ******************************************************************************/
76static void spmd_cpu_on_finish_handler(u_register_t unused)
77{
Olivier Deprezbe671112019-10-28 09:07:50 +000078 spmd_spm_core_context_t *ctx = spmd_get_context();
Olivier Deprezc7631a52020-03-23 09:53:06 +010079 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +020080 el3_state_t *el3_state;
81 uintptr_t entry_point;
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020082 uint64_t rc;
Olivier Deprezbe671112019-10-28 09:07:50 +000083
Olivier Deprezc7631a52020-03-23 09:53:06 +010084 assert(ctx != NULL);
Olivier Deprezbe671112019-10-28 09:07:50 +000085 assert(ctx->state != SPMC_STATE_ON);
Olivier Deprezc7631a52020-03-23 09:53:06 +010086
Olivier Depreze799f482021-03-02 17:31:22 +010087 spin_lock(&g_spmd_pm.lock);
88
Olivier Deprezc7631a52020-03-23 09:53:06 +010089 /*
Olivier Deprezeae45962021-01-19 15:06:47 +010090 * Leave the possibility that the SPMC does not call
91 * FFA_SECONDARY_EP_REGISTER in which case re-use the
92 * primary core address for booting secondary cores.
Olivier Deprezc7631a52020-03-23 09:53:06 +010093 */
Olivier Deprezeae45962021-01-19 15:06:47 +010094 if (g_spmd_pm.secondary_ep_locked == true) {
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +020095 /*
96 * The CPU context has already been initialized at boot time
97 * (in spmd_spmc_init by a call to cm_setup_context). Adjust
98 * below the target core entry point based on the address
99 * passed to by FFA_SECONDARY_EP_REGISTER.
100 */
101 entry_point = g_spmd_pm.secondary_ep;
102 el3_state = get_el3state_ctx(&ctx->cpu_ctx);
103 write_ctx_reg(el3_state, CTX_ELR_EL3, entry_point);
Olivier Deprezc7631a52020-03-23 09:53:06 +0100104 }
105
Olivier Depreze799f482021-03-02 17:31:22 +0100106 spin_unlock(&g_spmd_pm.lock);
107
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200108 /* Mark CPU as initiating ON operation. */
Olivier Deprezc7631a52020-03-23 09:53:06 +0100109 ctx->state = SPMC_STATE_ON_PENDING;
Olivier Deprezbe671112019-10-28 09:07:50 +0000110
111 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200112 if (rc != 0ULL) {
113 ERROR("%s failed (%llu) on CPU%u\n", __func__, rc,
Olivier Deprezc7631a52020-03-23 09:53:06 +0100114 linear_id);
Olivier Deprezbe671112019-10-28 09:07:50 +0000115 ctx->state = SPMC_STATE_OFF;
116 return;
117 }
118
119 ctx->state = SPMC_STATE_ON;
Olivier Deprezc7631a52020-03-23 09:53:06 +0100120
121 VERBOSE("CPU %u on!\n", linear_id);
122}
123
124/*******************************************************************************
125 * spmd_cpu_off_handler
126 ******************************************************************************/
127static int32_t spmd_cpu_off_handler(u_register_t unused)
128{
129 spmd_spm_core_context_t *ctx = spmd_get_context();
130 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200131 int64_t rc;
Olivier Deprezc7631a52020-03-23 09:53:06 +0100132
133 assert(ctx != NULL);
134 assert(ctx->state != SPMC_STATE_OFF);
135
Olivier Deprezc7631a52020-03-23 09:53:06 +0100136 /* Build an SPMD to SPMC direct message request. */
137 spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF);
138
139 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200140 if (rc != 0ULL) {
141 ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id);
Olivier Deprezc7631a52020-03-23 09:53:06 +0100142 }
143
Olivier Deprezeae45962021-01-19 15:06:47 +0100144 /* Expect a direct message response from the SPMC. */
145 u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
146 CTX_GPREG_X0);
147 if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
148 ERROR("%s invalid SPMC response (%lx).\n",
149 __func__, ffa_resp_func);
150 return -EINVAL;
151 }
Olivier Deprezc7631a52020-03-23 09:53:06 +0100152
Olivier Deprezc7631a52020-03-23 09:53:06 +0100153 ctx->state = SPMC_STATE_OFF;
154
155 VERBOSE("CPU %u off!\n", linear_id);
156
157 return 0;
Olivier Deprezbe671112019-10-28 09:07:50 +0000158}
159
160/*******************************************************************************
161 * Structure populated by the SPM Dispatcher to perform any bookkeeping before
162 * PSCI executes a power mgmt. operation.
163 ******************************************************************************/
164const spd_pm_ops_t spmd_pm = {
165 .svc_on_finish = spmd_cpu_on_finish_handler,
Olivier Deprezc7631a52020-03-23 09:53:06 +0100166 .svc_off = spmd_cpu_off_handler
Olivier Deprezbe671112019-10-28 09:07:50 +0000167};