1b058f20aSOlivier Deprez /*
28723eaf2SMadhukar Pappireddy * Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
3b058f20aSOlivier Deprez *
4b058f20aSOlivier Deprez * SPDX-License-Identifier: BSD-3-Clause
5b058f20aSOlivier Deprez */
6b058f20aSOlivier Deprez
7b058f20aSOlivier Deprez #include <assert.h>
8f0d743dbSOlivier Deprez #include <errno.h>
94ce3e99aSScott Branden #include <inttypes.h>
104ce3e99aSScott Branden #include <stdint.h>
114ce3e99aSScott Branden
12a92bc73bSOlivier Deprez #include <lib/el3_runtime/context_mgmt.h>
13473ced56SOlivier Deprez #include <lib/spinlock.h>
14b058f20aSOlivier Deprez #include "spmd_private.h"
15b058f20aSOlivier Deprez
16cdb49d47SOlivier Deprez static struct {
17cdb49d47SOlivier Deprez bool secondary_ep_locked;
18cdb49d47SOlivier Deprez uintptr_t secondary_ep;
19473ced56SOlivier Deprez spinlock_t lock;
20cdb49d47SOlivier Deprez } g_spmd_pm;
21cdb49d47SOlivier Deprez
22f0d743dbSOlivier Deprez /*******************************************************************************
23cdb49d47SOlivier Deprez * spmd_pm_secondary_ep_register
24f0d743dbSOlivier Deprez ******************************************************************************/
spmd_pm_secondary_ep_register(uintptr_t entry_point)25cdb49d47SOlivier Deprez int spmd_pm_secondary_ep_register(uintptr_t entry_point)
26f0d743dbSOlivier Deprez {
27473ced56SOlivier Deprez int ret = FFA_ERROR_INVALID_PARAMETER;
28473ced56SOlivier Deprez
29473ced56SOlivier Deprez spin_lock(&g_spmd_pm.lock);
30473ced56SOlivier Deprez
31cdb49d47SOlivier Deprez if (g_spmd_pm.secondary_ep_locked == true) {
32473ced56SOlivier Deprez goto out;
33f0d743dbSOlivier Deprez }
34f0d743dbSOlivier Deprez
35f0d743dbSOlivier Deprez /*
36f0d743dbSOlivier Deprez * Check entry_point address is a PA within
37f0d743dbSOlivier Deprez * load_address <= entry_point < load_address + binary_size
38f0d743dbSOlivier Deprez */
39f0d743dbSOlivier Deprez if (!spmd_check_address_in_binary_image(entry_point)) {
40cdb49d47SOlivier Deprez ERROR("%s entry point is not within image boundaries\n",
41cdb49d47SOlivier Deprez __func__);
42473ced56SOlivier Deprez goto out;
43f0d743dbSOlivier Deprez }
44f0d743dbSOlivier Deprez
45cdb49d47SOlivier Deprez g_spmd_pm.secondary_ep = entry_point;
46cdb49d47SOlivier Deprez g_spmd_pm.secondary_ep_locked = true;
4702d50bb0SOlivier Deprez
48cdb49d47SOlivier Deprez VERBOSE("%s %lx\n", __func__, entry_point);
49f0d743dbSOlivier Deprez
50473ced56SOlivier Deprez ret = 0;
51473ced56SOlivier Deprez
52473ced56SOlivier Deprez out:
53473ced56SOlivier Deprez spin_unlock(&g_spmd_pm.lock);
54473ced56SOlivier Deprez
55473ced56SOlivier Deprez return ret;
56f0d743dbSOlivier Deprez }
57f0d743dbSOlivier Deprez
58b058f20aSOlivier Deprez /*******************************************************************************
59b058f20aSOlivier Deprez * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part
60b058f20aSOlivier Deprez * of the SPMC initialization path, they will initialize any SPs that they
61b058f20aSOlivier Deprez * manage. Entry into SPMC is done after initialising minimal architectural
62b058f20aSOlivier Deprez * state that guarantees safe execution.
63b058f20aSOlivier Deprez ******************************************************************************/
spmd_cpu_on_finish_handler(u_register_t unused)64b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused)
65b058f20aSOlivier Deprez {
66b058f20aSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context();
67a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos();
68f2dcf418SOlivier Deprez el3_state_t *el3_state;
69f2dcf418SOlivier Deprez uintptr_t entry_point;
7002d50bb0SOlivier Deprez uint64_t rc;
71b058f20aSOlivier Deprez
72a92bc73bSOlivier Deprez assert(ctx != NULL);
73b058f20aSOlivier Deprez assert(ctx->state != SPMC_STATE_ON);
74a92bc73bSOlivier Deprez
75473ced56SOlivier Deprez spin_lock(&g_spmd_pm.lock);
76473ced56SOlivier Deprez
77*9f3f4d87SBoyan Karatotev spmd_setup_context(linear_id);
78*9f3f4d87SBoyan Karatotev
79a92bc73bSOlivier Deprez /*
80cdb49d47SOlivier Deprez * Leave the possibility that the SPMC does not call
81cdb49d47SOlivier Deprez * FFA_SECONDARY_EP_REGISTER in which case re-use the
82cdb49d47SOlivier Deprez * primary core address for booting secondary cores.
83a92bc73bSOlivier Deprez */
84cdb49d47SOlivier Deprez if (g_spmd_pm.secondary_ep_locked == true) {
85f2dcf418SOlivier Deprez /*
86*9f3f4d87SBoyan Karatotev * The CPU context has already been initialized
87*9f3f4d87SBoyan Karatotev * (in spmd_setup_context by a call to cm_setup_context). Adjust
88f2dcf418SOlivier Deprez * below the target core entry point based on the address
89f2dcf418SOlivier Deprez * passed to by FFA_SECONDARY_EP_REGISTER.
90f2dcf418SOlivier Deprez */
91f2dcf418SOlivier Deprez entry_point = g_spmd_pm.secondary_ep;
92f2dcf418SOlivier Deprez el3_state = get_el3state_ctx(&ctx->cpu_ctx);
93f2dcf418SOlivier Deprez write_ctx_reg(el3_state, CTX_ELR_EL3, entry_point);
94a92bc73bSOlivier Deprez }
95a92bc73bSOlivier Deprez
96473ced56SOlivier Deprez spin_unlock(&g_spmd_pm.lock);
97473ced56SOlivier Deprez
98f2dcf418SOlivier Deprez /* Mark CPU as initiating ON operation. */
99a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING;
100b058f20aSOlivier Deprez
101b058f20aSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx);
10202d50bb0SOlivier Deprez if (rc != 0ULL) {
1034ce3e99aSScott Branden ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc,
104b058f20aSOlivier Deprez linear_id);
105b058f20aSOlivier Deprez ctx->state = SPMC_STATE_OFF;
106b058f20aSOlivier Deprez return;
107b058f20aSOlivier Deprez }
108b058f20aSOlivier Deprez
109b058f20aSOlivier Deprez ctx->state = SPMC_STATE_ON;
110a92bc73bSOlivier Deprez
111a92bc73bSOlivier Deprez VERBOSE("CPU %u on!\n", linear_id);
112a92bc73bSOlivier Deprez }
113a92bc73bSOlivier Deprez
114a92bc73bSOlivier Deprez /*******************************************************************************
115a92bc73bSOlivier Deprez * spmd_cpu_off_handler
116a92bc73bSOlivier Deprez ******************************************************************************/
spmd_cpu_off_handler(u_register_t unused)117a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused)
118a92bc73bSOlivier Deprez {
119a92bc73bSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context();
120a92bc73bSOlivier Deprez unsigned int linear_id = plat_my_core_pos();
12102d50bb0SOlivier Deprez int64_t rc;
1228723eaf2SMadhukar Pappireddy uint32_t ffa_resp_func_id, msg_flags;
1238723eaf2SMadhukar Pappireddy int status;
124a92bc73bSOlivier Deprez
125a92bc73bSOlivier Deprez assert(ctx != NULL);
126a92bc73bSOlivier Deprez assert(ctx->state != SPMC_STATE_OFF);
127a92bc73bSOlivier Deprez
128a92bc73bSOlivier Deprez /* Build an SPMD to SPMC direct message request. */
12976d53ee1SOlivier Deprez gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
13076d53ee1SOlivier Deprez spmd_build_spmc_message(gpregs, FFA_FWK_MSG_PSCI, PSCI_CPU_OFF);
13176d53ee1SOlivier Deprez
13276d53ee1SOlivier Deprez /* Clear remaining x8 - x17 at EL3/SEL2 or EL3/SEL1 boundary. */
13376d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X8, 0);
13476d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X9, 0);
13576d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X10, 0);
13676d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X11, 0);
13776d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X12, 0);
13876d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X13, 0);
13976d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X14, 0);
14076d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X15, 0);
14176d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X16, 0);
14276d53ee1SOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X17, 0);
143a92bc73bSOlivier Deprez
1448723eaf2SMadhukar Pappireddy /* Mark current core as processing a PSCI operation. */
1458723eaf2SMadhukar Pappireddy ctx->psci_operation_ongoing = true;
1468723eaf2SMadhukar Pappireddy
147a92bc73bSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx);
1488723eaf2SMadhukar Pappireddy
14902d50bb0SOlivier Deprez if (rc != 0ULL) {
1504ce3e99aSScott Branden ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, linear_id);
151a92bc73bSOlivier Deprez }
152a92bc73bSOlivier Deprez
1538723eaf2SMadhukar Pappireddy ctx->psci_operation_ongoing = false;
1548723eaf2SMadhukar Pappireddy
155cdb49d47SOlivier Deprez /* Expect a direct message response from the SPMC. */
1568723eaf2SMadhukar Pappireddy ffa_resp_func_id = (uint32_t)read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
157cdb49d47SOlivier Deprez CTX_GPREG_X0);
1588723eaf2SMadhukar Pappireddy
1598723eaf2SMadhukar Pappireddy /*
1608723eaf2SMadhukar Pappireddy * Retrieve flags indicating framework message and power management
1618723eaf2SMadhukar Pappireddy * response.
1628723eaf2SMadhukar Pappireddy */
1638723eaf2SMadhukar Pappireddy msg_flags = (uint32_t)read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
1648723eaf2SMadhukar Pappireddy CTX_GPREG_X2);
1658723eaf2SMadhukar Pappireddy
1668723eaf2SMadhukar Pappireddy /* Retrieve error code indicating status of power management operation. */
1678723eaf2SMadhukar Pappireddy status = (int)read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
1688723eaf2SMadhukar Pappireddy CTX_GPREG_X3);
1698723eaf2SMadhukar Pappireddy
1708723eaf2SMadhukar Pappireddy if (ffa_resp_func_id == FFA_ERROR) {
1718723eaf2SMadhukar Pappireddy /*
1728723eaf2SMadhukar Pappireddy * It is likely that SPMC does not support receiving PSCI
1738723eaf2SMadhukar Pappireddy * operation through framework message. SPMD takes an
1748723eaf2SMadhukar Pappireddy * implementation defined choice to not treat it as a fatal
1758723eaf2SMadhukar Pappireddy * error. Consequently, SPMD ignores the error and continues
1768723eaf2SMadhukar Pappireddy * with power management operation.
1778723eaf2SMadhukar Pappireddy */
1788723eaf2SMadhukar Pappireddy VERBOSE("SPMC ignored PSCI CPU_OFF framework message\n");
1798723eaf2SMadhukar Pappireddy } else if (ffa_resp_func_id != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
1808723eaf2SMadhukar Pappireddy ERROR("%s invalid SPMC response (%x).\n",
1818723eaf2SMadhukar Pappireddy __func__, ffa_resp_func_id);
1828723eaf2SMadhukar Pappireddy panic();
1838723eaf2SMadhukar Pappireddy } else if (((msg_flags & FFA_FWK_MSG_BIT) == 0U) ||
1848723eaf2SMadhukar Pappireddy ((msg_flags & FFA_FWK_MSG_MASK) != FFA_PM_MSG_PM_RESP)) {
1858723eaf2SMadhukar Pappireddy ERROR("SPMC failed to send framework message response for power"
1868723eaf2SMadhukar Pappireddy " management operation, message flags = (%x)\n",
1878723eaf2SMadhukar Pappireddy msg_flags);
1888723eaf2SMadhukar Pappireddy panic();
1898723eaf2SMadhukar Pappireddy } else if (status != PSCI_E_SUCCESS) {
1908723eaf2SMadhukar Pappireddy ERROR("SPMC denied CPU_OFF power management request\n");
1918723eaf2SMadhukar Pappireddy panic();
1928723eaf2SMadhukar Pappireddy } else {
1938723eaf2SMadhukar Pappireddy VERBOSE("CPU %u off!\n", linear_id);
194cdb49d47SOlivier Deprez }
195a92bc73bSOlivier Deprez
196a92bc73bSOlivier Deprez ctx->state = SPMC_STATE_OFF;
197a92bc73bSOlivier Deprez
198a92bc73bSOlivier Deprez return 0;
199b058f20aSOlivier Deprez }
200b058f20aSOlivier Deprez
201b058f20aSOlivier Deprez /*******************************************************************************
202b058f20aSOlivier Deprez * Structure populated by the SPM Dispatcher to perform any bookkeeping before
203b058f20aSOlivier Deprez * PSCI executes a power mgmt. operation.
204b058f20aSOlivier Deprez ******************************************************************************/
205b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = {
206b058f20aSOlivier Deprez .svc_on_finish = spmd_cpu_on_finish_handler,
207a92bc73bSOlivier Deprez .svc_off = spmd_cpu_off_handler
208b058f20aSOlivier Deprez };
209