xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_pm.c (revision 4ce3e99a336b74611349595ea7fd5ed0277c3eeb)
1b058f20aSOlivier Deprez /*
2cdb49d47SOlivier Deprez  * Copyright (c) 2020-2021, 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>
9*4ce3e99aSScott Branden #include <inttypes.h>
10*4ce3e99aSScott Branden #include <stdint.h>
11*4ce3e99aSScott 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 /*******************************************************************************
23a92bc73bSOlivier Deprez  * spmd_build_spmc_message
24a92bc73bSOlivier Deprez  *
25a92bc73bSOlivier Deprez  * Builds an SPMD to SPMC direct message request.
26a92bc73bSOlivier Deprez  ******************************************************************************/
27a92bc73bSOlivier Deprez static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message)
28a92bc73bSOlivier Deprez {
29a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
30a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1,
31a92bc73bSOlivier Deprez 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
32a92bc73bSOlivier Deprez 		spmd_spmc_id_get());
33a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ);
34a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
35a92bc73bSOlivier Deprez }
36a92bc73bSOlivier Deprez 
37a92bc73bSOlivier Deprez /*******************************************************************************
38cdb49d47SOlivier Deprez  * spmd_pm_secondary_ep_register
39f0d743dbSOlivier Deprez  ******************************************************************************/
40cdb49d47SOlivier Deprez int spmd_pm_secondary_ep_register(uintptr_t entry_point)
41f0d743dbSOlivier Deprez {
42473ced56SOlivier Deprez 	int ret = FFA_ERROR_INVALID_PARAMETER;
43473ced56SOlivier Deprez 
44473ced56SOlivier Deprez 	spin_lock(&g_spmd_pm.lock);
45473ced56SOlivier Deprez 
46cdb49d47SOlivier Deprez 	if (g_spmd_pm.secondary_ep_locked == true) {
47473ced56SOlivier Deprez 		goto out;
48f0d743dbSOlivier Deprez 	}
49f0d743dbSOlivier Deprez 
50f0d743dbSOlivier Deprez 	/*
51f0d743dbSOlivier Deprez 	 * Check entry_point address is a PA within
52f0d743dbSOlivier Deprez 	 * load_address <= entry_point < load_address + binary_size
53f0d743dbSOlivier Deprez 	 */
54f0d743dbSOlivier Deprez 	if (!spmd_check_address_in_binary_image(entry_point)) {
55cdb49d47SOlivier Deprez 		ERROR("%s entry point is not within image boundaries\n",
56cdb49d47SOlivier Deprez 			__func__);
57473ced56SOlivier Deprez 		goto out;
58f0d743dbSOlivier Deprez 	}
59f0d743dbSOlivier Deprez 
60cdb49d47SOlivier Deprez 	g_spmd_pm.secondary_ep = entry_point;
61cdb49d47SOlivier Deprez 	g_spmd_pm.secondary_ep_locked = true;
6202d50bb0SOlivier Deprez 
63cdb49d47SOlivier Deprez 	VERBOSE("%s %lx\n", __func__, entry_point);
64f0d743dbSOlivier Deprez 
65473ced56SOlivier Deprez 	ret = 0;
66473ced56SOlivier Deprez 
67473ced56SOlivier Deprez out:
68473ced56SOlivier Deprez 	spin_unlock(&g_spmd_pm.lock);
69473ced56SOlivier Deprez 
70473ced56SOlivier Deprez 	return ret;
71f0d743dbSOlivier Deprez }
72f0d743dbSOlivier Deprez 
73b058f20aSOlivier Deprez /*******************************************************************************
74b058f20aSOlivier Deprez  * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part
75b058f20aSOlivier Deprez  * of the SPMC initialization path, they will initialize any SPs that they
76b058f20aSOlivier Deprez  * manage. Entry into SPMC is done after initialising minimal architectural
77b058f20aSOlivier Deprez  * state that guarantees safe execution.
78b058f20aSOlivier Deprez  ******************************************************************************/
79b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused)
80b058f20aSOlivier Deprez {
81b058f20aSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
82a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
83f2dcf418SOlivier Deprez 	el3_state_t *el3_state;
84f2dcf418SOlivier Deprez 	uintptr_t entry_point;
8502d50bb0SOlivier Deprez 	uint64_t rc;
86b058f20aSOlivier Deprez 
87a92bc73bSOlivier Deprez 	assert(ctx != NULL);
88b058f20aSOlivier Deprez 	assert(ctx->state != SPMC_STATE_ON);
89a92bc73bSOlivier Deprez 
90473ced56SOlivier Deprez 	spin_lock(&g_spmd_pm.lock);
91473ced56SOlivier Deprez 
92a92bc73bSOlivier Deprez 	/*
93cdb49d47SOlivier Deprez 	 * Leave the possibility that the SPMC does not call
94cdb49d47SOlivier Deprez 	 * FFA_SECONDARY_EP_REGISTER in which case re-use the
95cdb49d47SOlivier Deprez 	 * primary core address for booting secondary cores.
96a92bc73bSOlivier Deprez 	 */
97cdb49d47SOlivier Deprez 	if (g_spmd_pm.secondary_ep_locked == true) {
98f2dcf418SOlivier Deprez 		/*
99f2dcf418SOlivier Deprez 		 * The CPU context has already been initialized at boot time
100f2dcf418SOlivier Deprez 		 * (in spmd_spmc_init by a call to cm_setup_context). Adjust
101f2dcf418SOlivier Deprez 		 * below the target core entry point based on the address
102f2dcf418SOlivier Deprez 		 * passed to by FFA_SECONDARY_EP_REGISTER.
103f2dcf418SOlivier Deprez 		 */
104f2dcf418SOlivier Deprez 		entry_point = g_spmd_pm.secondary_ep;
105f2dcf418SOlivier Deprez 		el3_state = get_el3state_ctx(&ctx->cpu_ctx);
106f2dcf418SOlivier Deprez 		write_ctx_reg(el3_state, CTX_ELR_EL3, entry_point);
107a92bc73bSOlivier Deprez 	}
108a92bc73bSOlivier Deprez 
109473ced56SOlivier Deprez 	spin_unlock(&g_spmd_pm.lock);
110473ced56SOlivier Deprez 
111f2dcf418SOlivier Deprez 	/* Mark CPU as initiating ON operation. */
112a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
113b058f20aSOlivier Deprez 
114b058f20aSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
11502d50bb0SOlivier Deprez 	if (rc != 0ULL) {
116*4ce3e99aSScott Branden 		ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc,
117b058f20aSOlivier Deprez 			linear_id);
118b058f20aSOlivier Deprez 		ctx->state = SPMC_STATE_OFF;
119b058f20aSOlivier Deprez 		return;
120b058f20aSOlivier Deprez 	}
121b058f20aSOlivier Deprez 
122b058f20aSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
123a92bc73bSOlivier Deprez 
124a92bc73bSOlivier Deprez 	VERBOSE("CPU %u on!\n", linear_id);
125a92bc73bSOlivier Deprez }
126a92bc73bSOlivier Deprez 
127a92bc73bSOlivier Deprez /*******************************************************************************
128a92bc73bSOlivier Deprez  * spmd_cpu_off_handler
129a92bc73bSOlivier Deprez  ******************************************************************************/
130a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused)
131a92bc73bSOlivier Deprez {
132a92bc73bSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
133a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
13402d50bb0SOlivier Deprez 	int64_t rc;
135a92bc73bSOlivier Deprez 
136a92bc73bSOlivier Deprez 	assert(ctx != NULL);
137a92bc73bSOlivier Deprez 	assert(ctx->state != SPMC_STATE_OFF);
138a92bc73bSOlivier Deprez 
139a92bc73bSOlivier Deprez 	/* Build an SPMD to SPMC direct message request. */
140a92bc73bSOlivier Deprez 	spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF);
141a92bc73bSOlivier Deprez 
142a92bc73bSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
14302d50bb0SOlivier Deprez 	if (rc != 0ULL) {
144*4ce3e99aSScott Branden 		ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, linear_id);
145a92bc73bSOlivier Deprez 	}
146a92bc73bSOlivier Deprez 
147cdb49d47SOlivier Deprez 	/* Expect a direct message response from the SPMC. */
148cdb49d47SOlivier Deprez 	u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
149cdb49d47SOlivier Deprez 						  CTX_GPREG_X0);
150cdb49d47SOlivier Deprez 	if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
151cdb49d47SOlivier Deprez 		ERROR("%s invalid SPMC response (%lx).\n",
152cdb49d47SOlivier Deprez 			__func__, ffa_resp_func);
153cdb49d47SOlivier Deprez 		return -EINVAL;
154cdb49d47SOlivier Deprez 	}
155a92bc73bSOlivier Deprez 
156a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_OFF;
157a92bc73bSOlivier Deprez 
158a92bc73bSOlivier Deprez 	VERBOSE("CPU %u off!\n", linear_id);
159a92bc73bSOlivier Deprez 
160a92bc73bSOlivier Deprez 	return 0;
161b058f20aSOlivier Deprez }
162b058f20aSOlivier Deprez 
163b058f20aSOlivier Deprez /*******************************************************************************
164b058f20aSOlivier Deprez  * Structure populated by the SPM Dispatcher to perform any bookkeeping before
165b058f20aSOlivier Deprez  * PSCI executes a power mgmt. operation.
166b058f20aSOlivier Deprez  ******************************************************************************/
167b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = {
168b058f20aSOlivier Deprez 	.svc_on_finish = spmd_cpu_on_finish_handler,
169a92bc73bSOlivier Deprez 	.svc_off = spmd_cpu_off_handler
170b058f20aSOlivier Deprez };
171