xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_pm.c (revision 8723eaf2fea9d09526fd7e6bc544b9c3103240ac)
1b058f20aSOlivier Deprez /*
2*8723eaf2SMadhukar 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  ******************************************************************************/
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  ******************************************************************************/
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 
77a92bc73bSOlivier Deprez 	/*
78cdb49d47SOlivier Deprez 	 * Leave the possibility that the SPMC does not call
79cdb49d47SOlivier Deprez 	 * FFA_SECONDARY_EP_REGISTER in which case re-use the
80cdb49d47SOlivier Deprez 	 * primary core address for booting secondary cores.
81a92bc73bSOlivier Deprez 	 */
82cdb49d47SOlivier Deprez 	if (g_spmd_pm.secondary_ep_locked == true) {
83f2dcf418SOlivier Deprez 		/*
84f2dcf418SOlivier Deprez 		 * The CPU context has already been initialized at boot time
85f2dcf418SOlivier Deprez 		 * (in spmd_spmc_init by a call to cm_setup_context). Adjust
86f2dcf418SOlivier Deprez 		 * below the target core entry point based on the address
87f2dcf418SOlivier Deprez 		 * passed to by FFA_SECONDARY_EP_REGISTER.
88f2dcf418SOlivier Deprez 		 */
89f2dcf418SOlivier Deprez 		entry_point = g_spmd_pm.secondary_ep;
90f2dcf418SOlivier Deprez 		el3_state = get_el3state_ctx(&ctx->cpu_ctx);
91f2dcf418SOlivier Deprez 		write_ctx_reg(el3_state, CTX_ELR_EL3, entry_point);
92a92bc73bSOlivier Deprez 	}
93a92bc73bSOlivier Deprez 
94473ced56SOlivier Deprez 	spin_unlock(&g_spmd_pm.lock);
95473ced56SOlivier Deprez 
96f2dcf418SOlivier Deprez 	/* Mark CPU as initiating ON operation. */
97a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
98b058f20aSOlivier Deprez 
99b058f20aSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
10002d50bb0SOlivier Deprez 	if (rc != 0ULL) {
1014ce3e99aSScott Branden 		ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc,
102b058f20aSOlivier Deprez 			linear_id);
103b058f20aSOlivier Deprez 		ctx->state = SPMC_STATE_OFF;
104b058f20aSOlivier Deprez 		return;
105b058f20aSOlivier Deprez 	}
106b058f20aSOlivier Deprez 
107b058f20aSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
108a92bc73bSOlivier Deprez 
109a92bc73bSOlivier Deprez 	VERBOSE("CPU %u on!\n", linear_id);
110a92bc73bSOlivier Deprez }
111a92bc73bSOlivier Deprez 
112a92bc73bSOlivier Deprez /*******************************************************************************
113a92bc73bSOlivier Deprez  * spmd_cpu_off_handler
114a92bc73bSOlivier Deprez  ******************************************************************************/
115a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused)
116a92bc73bSOlivier Deprez {
117a92bc73bSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
118a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
11902d50bb0SOlivier Deprez 	int64_t rc;
120*8723eaf2SMadhukar Pappireddy 	uint32_t ffa_resp_func_id, msg_flags;
121*8723eaf2SMadhukar Pappireddy 	int status;
122a92bc73bSOlivier Deprez 
123a92bc73bSOlivier Deprez 	assert(ctx != NULL);
124a92bc73bSOlivier Deprez 	assert(ctx->state != SPMC_STATE_OFF);
125a92bc73bSOlivier Deprez 
126a92bc73bSOlivier Deprez 	/* Build an SPMD to SPMC direct message request. */
12776d53ee1SOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
12876d53ee1SOlivier Deprez 	spmd_build_spmc_message(gpregs, FFA_FWK_MSG_PSCI, PSCI_CPU_OFF);
12976d53ee1SOlivier Deprez 
13076d53ee1SOlivier Deprez 	/* Clear remaining x8 - x17 at EL3/SEL2 or EL3/SEL1 boundary. */
13176d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X8, 0);
13276d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X9, 0);
13376d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X10, 0);
13476d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X11, 0);
13576d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X12, 0);
13676d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X13, 0);
13776d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X14, 0);
13876d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X15, 0);
13976d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X16, 0);
14076d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X17, 0);
141a92bc73bSOlivier Deprez 
142*8723eaf2SMadhukar Pappireddy 	/* Mark current core as processing a PSCI operation. */
143*8723eaf2SMadhukar Pappireddy 	ctx->psci_operation_ongoing = true;
144*8723eaf2SMadhukar Pappireddy 
145a92bc73bSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
146*8723eaf2SMadhukar Pappireddy 
14702d50bb0SOlivier Deprez 	if (rc != 0ULL) {
1484ce3e99aSScott Branden 		ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, linear_id);
149a92bc73bSOlivier Deprez 	}
150a92bc73bSOlivier Deprez 
151*8723eaf2SMadhukar Pappireddy 	ctx->psci_operation_ongoing = false;
152*8723eaf2SMadhukar Pappireddy 
153cdb49d47SOlivier Deprez 	/* Expect a direct message response from the SPMC. */
154*8723eaf2SMadhukar Pappireddy 	ffa_resp_func_id = (uint32_t)read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
155cdb49d47SOlivier Deprez 						  CTX_GPREG_X0);
156*8723eaf2SMadhukar Pappireddy 
157*8723eaf2SMadhukar Pappireddy 	/*
158*8723eaf2SMadhukar Pappireddy 	 * Retrieve flags indicating framework message and power management
159*8723eaf2SMadhukar Pappireddy 	 * response.
160*8723eaf2SMadhukar Pappireddy 	 */
161*8723eaf2SMadhukar Pappireddy 	msg_flags = (uint32_t)read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
162*8723eaf2SMadhukar Pappireddy 						  CTX_GPREG_X2);
163*8723eaf2SMadhukar Pappireddy 
164*8723eaf2SMadhukar Pappireddy 	/* Retrieve error code indicating status of power management operation. */
165*8723eaf2SMadhukar Pappireddy 	status = (int)read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
166*8723eaf2SMadhukar Pappireddy 						  CTX_GPREG_X3);
167*8723eaf2SMadhukar Pappireddy 
168*8723eaf2SMadhukar Pappireddy 	if (ffa_resp_func_id == FFA_ERROR) {
169*8723eaf2SMadhukar Pappireddy 		/*
170*8723eaf2SMadhukar Pappireddy 		 * It is likely that SPMC does not support receiving PSCI
171*8723eaf2SMadhukar Pappireddy 		 * operation through framework message. SPMD takes an
172*8723eaf2SMadhukar Pappireddy 		 * implementation defined choice to not treat it as a fatal
173*8723eaf2SMadhukar Pappireddy 		 * error. Consequently, SPMD ignores the error and continues
174*8723eaf2SMadhukar Pappireddy 		 * with power management operation.
175*8723eaf2SMadhukar Pappireddy 		 */
176*8723eaf2SMadhukar Pappireddy 		VERBOSE("SPMC ignored PSCI CPU_OFF framework message\n");
177*8723eaf2SMadhukar Pappireddy 	} else if (ffa_resp_func_id != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
178*8723eaf2SMadhukar Pappireddy 		ERROR("%s invalid SPMC response (%x).\n",
179*8723eaf2SMadhukar Pappireddy 			__func__, ffa_resp_func_id);
180*8723eaf2SMadhukar Pappireddy 		panic();
181*8723eaf2SMadhukar Pappireddy 	} else if (((msg_flags & FFA_FWK_MSG_BIT) == 0U) ||
182*8723eaf2SMadhukar Pappireddy 			 ((msg_flags & FFA_FWK_MSG_MASK) != FFA_PM_MSG_PM_RESP)) {
183*8723eaf2SMadhukar Pappireddy 		ERROR("SPMC failed to send framework message response for power"
184*8723eaf2SMadhukar Pappireddy 			" management operation, message flags = (%x)\n",
185*8723eaf2SMadhukar Pappireddy 			 msg_flags);
186*8723eaf2SMadhukar Pappireddy 		panic();
187*8723eaf2SMadhukar Pappireddy 	} else if (status != PSCI_E_SUCCESS) {
188*8723eaf2SMadhukar Pappireddy 		ERROR("SPMC denied CPU_OFF power management request\n");
189*8723eaf2SMadhukar Pappireddy 		panic();
190*8723eaf2SMadhukar Pappireddy 	} else {
191*8723eaf2SMadhukar Pappireddy 		VERBOSE("CPU %u off!\n", linear_id);
192cdb49d47SOlivier Deprez 	}
193a92bc73bSOlivier Deprez 
194a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_OFF;
195a92bc73bSOlivier Deprez 
196a92bc73bSOlivier Deprez 	return 0;
197b058f20aSOlivier Deprez }
198b058f20aSOlivier Deprez 
199b058f20aSOlivier Deprez /*******************************************************************************
200b058f20aSOlivier Deprez  * Structure populated by the SPM Dispatcher to perform any bookkeeping before
201b058f20aSOlivier Deprez  * PSCI executes a power mgmt. operation.
202b058f20aSOlivier Deprez  ******************************************************************************/
203b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = {
204b058f20aSOlivier Deprez 	.svc_on_finish = spmd_cpu_on_finish_handler,
205a92bc73bSOlivier Deprez 	.svc_off = spmd_cpu_off_handler
206b058f20aSOlivier Deprez };
207