blob: 074609c89c1ed58cd528c392e861d0fa5b42b361 [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 Deprezc7631a52020-03-23 09:53:06 +010078 entry_point_info_t *spmc_ep_info = spmd_spmc_ep_info_get();
Olivier Deprezbe671112019-10-28 09:07:50 +000079 spmd_spm_core_context_t *ctx = spmd_get_context();
Olivier Deprezc7631a52020-03-23 09:53:06 +010080 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020081 uint64_t rc;
Olivier Deprezbe671112019-10-28 09:07:50 +000082
Olivier Deprezc7631a52020-03-23 09:53:06 +010083 assert(ctx != NULL);
Olivier Deprezbe671112019-10-28 09:07:50 +000084 assert(ctx->state != SPMC_STATE_ON);
Olivier Deprezc7631a52020-03-23 09:53:06 +010085 assert(spmc_ep_info != NULL);
86
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) {
95 spmc_ep_info->pc = g_spmd_pm.secondary_ep;
Olivier Deprezc7631a52020-03-23 09:53:06 +010096 }
97
Olivier Depreze799f482021-03-02 17:31:22 +010098 spin_unlock(&g_spmd_pm.lock);
99
Olivier Deprezc7631a52020-03-23 09:53:06 +0100100 cm_setup_context(&ctx->cpu_ctx, spmc_ep_info);
Olivier Deprezc7631a52020-03-23 09:53:06 +0100101
102 /* Mark CPU as initiating ON operation */
103 ctx->state = SPMC_STATE_ON_PENDING;
Olivier Deprezbe671112019-10-28 09:07:50 +0000104
105 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200106 if (rc != 0ULL) {
107 ERROR("%s failed (%llu) on CPU%u\n", __func__, rc,
Olivier Deprezc7631a52020-03-23 09:53:06 +0100108 linear_id);
Olivier Deprezbe671112019-10-28 09:07:50 +0000109 ctx->state = SPMC_STATE_OFF;
110 return;
111 }
112
113 ctx->state = SPMC_STATE_ON;
Olivier Deprezc7631a52020-03-23 09:53:06 +0100114
115 VERBOSE("CPU %u on!\n", linear_id);
116}
117
118/*******************************************************************************
119 * spmd_cpu_off_handler
120 ******************************************************************************/
121static int32_t spmd_cpu_off_handler(u_register_t unused)
122{
123 spmd_spm_core_context_t *ctx = spmd_get_context();
124 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200125 int64_t rc;
Olivier Deprezc7631a52020-03-23 09:53:06 +0100126
127 assert(ctx != NULL);
128 assert(ctx->state != SPMC_STATE_OFF);
129
Olivier Deprezc7631a52020-03-23 09:53:06 +0100130 /* Build an SPMD to SPMC direct message request. */
131 spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF);
132
133 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez73ef0dc2020-06-19 15:33:41 +0200134 if (rc != 0ULL) {
135 ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id);
Olivier Deprezc7631a52020-03-23 09:53:06 +0100136 }
137
Olivier Deprezeae45962021-01-19 15:06:47 +0100138 /* Expect a direct message response from the SPMC. */
139 u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
140 CTX_GPREG_X0);
141 if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
142 ERROR("%s invalid SPMC response (%lx).\n",
143 __func__, ffa_resp_func);
144 return -EINVAL;
145 }
Olivier Deprezc7631a52020-03-23 09:53:06 +0100146
Olivier Deprezc7631a52020-03-23 09:53:06 +0100147 ctx->state = SPMC_STATE_OFF;
148
149 VERBOSE("CPU %u off!\n", linear_id);
150
151 return 0;
Olivier Deprezbe671112019-10-28 09:07:50 +0000152}
153
154/*******************************************************************************
155 * Structure populated by the SPM Dispatcher to perform any bookkeeping before
156 * PSCI executes a power mgmt. operation.
157 ******************************************************************************/
158const spd_pm_ops_t spmd_pm = {
159 .svc_on_finish = spmd_cpu_on_finish_handler,
Olivier Deprezc7631a52020-03-23 09:53:06 +0100160 .svc_off = spmd_cpu_off_handler
Olivier Deprezbe671112019-10-28 09:07:50 +0000161};