blob: 6ebafcaa7bef507c50702c7c3e77bbeeec6c6846 [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>
Scott Brandene5dcf982020-08-25 13:49:32 -07009#include <inttypes.h>
10#include <stdint.h>
11
Olivier Deprezc7631a52020-03-23 09:53:06 +010012#include <lib/el3_runtime/context_mgmt.h>
Olivier Depreze799f482021-03-02 17:31:22 +010013#include <lib/spinlock.h>
Olivier Deprezbe671112019-10-28 09:07:50 +000014#include "spmd_private.h"
15
Olivier Deprezeae45962021-01-19 15:06:47 +010016static struct {
17 bool secondary_ep_locked;
18 uintptr_t secondary_ep;
Olivier Depreze799f482021-03-02 17:31:22 +010019 spinlock_t lock;
Olivier Deprezeae45962021-01-19 15:06:47 +010020} g_spmd_pm;
21
Olivier Deprez33e44122020-04-16 17:54:27 +020022/*******************************************************************************
Olivier Deprezc7631a52020-03-23 09:53:06 +010023 * spmd_build_spmc_message
24 *
25 * Builds an SPMD to SPMC direct message request.
26 ******************************************************************************/
27static 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 Deprezeae45962021-01-19 15:06:47 +010038 * spmd_pm_secondary_ep_register
Olivier Deprez33e44122020-04-16 17:54:27 +020039 ******************************************************************************/
Olivier Deprezeae45962021-01-19 15:06:47 +010040int spmd_pm_secondary_ep_register(uintptr_t entry_point)
Olivier Deprez33e44122020-04-16 17:54:27 +020041{
Olivier Depreze799f482021-03-02 17:31:22 +010042 int ret = FFA_ERROR_INVALID_PARAMETER;
43
44 spin_lock(&g_spmd_pm.lock);
45
Olivier Deprezeae45962021-01-19 15:06:47 +010046 if (g_spmd_pm.secondary_ep_locked == true) {
Olivier Depreze799f482021-03-02 17:31:22 +010047 goto out;
Olivier Deprez33e44122020-04-16 17:54:27 +020048 }
49
Olivier Deprez33e44122020-04-16 17:54:27 +020050 /*
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 Deprezeae45962021-01-19 15:06:47 +010055 ERROR("%s entry point is not within image boundaries\n",
56 __func__);
Olivier Depreze799f482021-03-02 17:31:22 +010057 goto out;
Olivier Deprez33e44122020-04-16 17:54:27 +020058 }
59
Olivier Deprezeae45962021-01-19 15:06:47 +010060 g_spmd_pm.secondary_ep = entry_point;
61 g_spmd_pm.secondary_ep_locked = true;
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020062
Olivier Deprezeae45962021-01-19 15:06:47 +010063 VERBOSE("%s %lx\n", __func__, entry_point);
Olivier Deprez33e44122020-04-16 17:54:27 +020064
Olivier Depreze799f482021-03-02 17:31:22 +010065 ret = 0;
66
67out:
68 spin_unlock(&g_spmd_pm.lock);
69
70 return ret;
Olivier Deprez33e44122020-04-16 17:54:27 +020071}
72
Olivier Deprezbe671112019-10-28 09:07:50 +000073/*******************************************************************************
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 ******************************************************************************/
79static void spmd_cpu_on_finish_handler(u_register_t unused)
80{
Olivier Deprezbe671112019-10-28 09:07:50 +000081 spmd_spm_core_context_t *ctx = spmd_get_context();
Olivier Deprezc7631a52020-03-23 09:53:06 +010082 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +020083 el3_state_t *el3_state;
84 uintptr_t entry_point;
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020085 uint64_t rc;
Olivier Deprezbe671112019-10-28 09:07:50 +000086
Olivier Deprezc7631a52020-03-23 09:53:06 +010087 assert(ctx != NULL);
Olivier Deprezbe671112019-10-28 09:07:50 +000088 assert(ctx->state != SPMC_STATE_ON);
Olivier Deprezc7631a52020-03-23 09:53:06 +010089
Olivier Depreze799f482021-03-02 17:31:22 +010090 spin_lock(&g_spmd_pm.lock);
91
Olivier Deprezc7631a52020-03-23 09:53:06 +010092 /*
Olivier Deprezeae45962021-01-19 15:06:47 +010093 * 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 Deprezc7631a52020-03-23 09:53:06 +010096 */
Olivier Deprezeae45962021-01-19 15:06:47 +010097 if (g_spmd_pm.secondary_ep_locked == true) {
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +020098 /*
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 Deprezc7631a52020-03-23 09:53:06 +0100107 }
108
Olivier Depreze799f482021-03-02 17:31:22 +0100109 spin_unlock(&g_spmd_pm.lock);
110
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200111 /* Mark CPU as initiating ON operation. */
Olivier Deprezc7631a52020-03-23 09:53:06 +0100112 ctx->state = SPMC_STATE_ON_PENDING;
Olivier Deprezbe671112019-10-28 09:07:50 +0000113
114 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200115 if (rc != 0ULL) {
Scott Brandene5dcf982020-08-25 13:49:32 -0700116 ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc,
Olivier Deprezc7631a52020-03-23 09:53:06 +0100117 linear_id);
Olivier Deprezbe671112019-10-28 09:07:50 +0000118 ctx->state = SPMC_STATE_OFF;
119 return;
120 }
121
122 ctx->state = SPMC_STATE_ON;
Olivier Deprezc7631a52020-03-23 09:53:06 +0100123
124 VERBOSE("CPU %u on!\n", linear_id);
125}
126
127/*******************************************************************************
128 * spmd_cpu_off_handler
129 ******************************************************************************/
130static 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 Deprez73ef0dc2020-06-19 15:33:41 +0200134 int64_t rc;
Olivier Deprezc7631a52020-03-23 09:53:06 +0100135
136 assert(ctx != NULL);
137 assert(ctx->state != SPMC_STATE_OFF);
138
Olivier Deprezc7631a52020-03-23 09:53:06 +0100139 /* 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 Deprez73ef0dc2020-06-19 15:33:41 +0200143 if (rc != 0ULL) {
Scott Brandene5dcf982020-08-25 13:49:32 -0700144 ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, linear_id);
Olivier Deprezc7631a52020-03-23 09:53:06 +0100145 }
146
Olivier Deprezeae45962021-01-19 15:06:47 +0100147 /* 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 Deprezc7631a52020-03-23 09:53:06 +0100155
Olivier Deprezc7631a52020-03-23 09:53:06 +0100156 ctx->state = SPMC_STATE_OFF;
157
158 VERBOSE("CPU %u off!\n", linear_id);
159
160 return 0;
Olivier Deprezbe671112019-10-28 09:07:50 +0000161}
162
163/*******************************************************************************
164 * Structure populated by the SPM Dispatcher to perform any bookkeeping before
165 * PSCI executes a power mgmt. operation.
166 ******************************************************************************/
167const spd_pm_ops_t spmd_pm = {
168 .svc_on_finish = spmd_cpu_on_finish_handler,
Olivier Deprezc7631a52020-03-23 09:53:06 +0100169 .svc_off = spmd_cpu_off_handler
Olivier Deprezbe671112019-10-28 09:07:50 +0000170};