xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_pm.c (revision 473ced567028bc3a88c45cbd033e5ebfed40b7d2)
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>
9a92bc73bSOlivier Deprez #include <lib/el3_runtime/context_mgmt.h>
10*473ced56SOlivier Deprez #include <lib/spinlock.h>
11b058f20aSOlivier Deprez #include "spmd_private.h"
12b058f20aSOlivier Deprez 
13cdb49d47SOlivier Deprez static struct {
14cdb49d47SOlivier Deprez 	bool secondary_ep_locked;
15cdb49d47SOlivier Deprez 	uintptr_t secondary_ep;
16*473ced56SOlivier Deprez 	spinlock_t lock;
17cdb49d47SOlivier Deprez } g_spmd_pm;
18cdb49d47SOlivier Deprez 
19f0d743dbSOlivier Deprez /*******************************************************************************
20a92bc73bSOlivier Deprez  * spmd_build_spmc_message
21a92bc73bSOlivier Deprez  *
22a92bc73bSOlivier Deprez  * Builds an SPMD to SPMC direct message request.
23a92bc73bSOlivier Deprez  ******************************************************************************/
24a92bc73bSOlivier Deprez static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message)
25a92bc73bSOlivier Deprez {
26a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
27a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1,
28a92bc73bSOlivier Deprez 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
29a92bc73bSOlivier Deprez 		spmd_spmc_id_get());
30a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ);
31a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
32a92bc73bSOlivier Deprez }
33a92bc73bSOlivier Deprez 
34a92bc73bSOlivier Deprez /*******************************************************************************
35cdb49d47SOlivier Deprez  * spmd_pm_secondary_ep_register
36f0d743dbSOlivier Deprez  ******************************************************************************/
37cdb49d47SOlivier Deprez int spmd_pm_secondary_ep_register(uintptr_t entry_point)
38f0d743dbSOlivier Deprez {
39*473ced56SOlivier Deprez 	int ret = FFA_ERROR_INVALID_PARAMETER;
40*473ced56SOlivier Deprez 
41*473ced56SOlivier Deprez 	spin_lock(&g_spmd_pm.lock);
42*473ced56SOlivier Deprez 
43cdb49d47SOlivier Deprez 	if (g_spmd_pm.secondary_ep_locked == true) {
44*473ced56SOlivier Deprez 		goto out;
45f0d743dbSOlivier Deprez 	}
46f0d743dbSOlivier Deprez 
47f0d743dbSOlivier Deprez 	/*
48f0d743dbSOlivier Deprez 	 * Check entry_point address is a PA within
49f0d743dbSOlivier Deprez 	 * load_address <= entry_point < load_address + binary_size
50f0d743dbSOlivier Deprez 	 */
51f0d743dbSOlivier Deprez 	if (!spmd_check_address_in_binary_image(entry_point)) {
52cdb49d47SOlivier Deprez 		ERROR("%s entry point is not within image boundaries\n",
53cdb49d47SOlivier Deprez 			__func__);
54*473ced56SOlivier Deprez 		goto out;
55f0d743dbSOlivier Deprez 	}
56f0d743dbSOlivier Deprez 
57cdb49d47SOlivier Deprez 	g_spmd_pm.secondary_ep = entry_point;
58cdb49d47SOlivier Deprez 	g_spmd_pm.secondary_ep_locked = true;
5902d50bb0SOlivier Deprez 
60cdb49d47SOlivier Deprez 	VERBOSE("%s %lx\n", __func__, entry_point);
61f0d743dbSOlivier Deprez 
62*473ced56SOlivier Deprez 	ret = 0;
63*473ced56SOlivier Deprez 
64*473ced56SOlivier Deprez out:
65*473ced56SOlivier Deprez 	spin_unlock(&g_spmd_pm.lock);
66*473ced56SOlivier Deprez 
67*473ced56SOlivier Deprez 	return ret;
68f0d743dbSOlivier Deprez }
69f0d743dbSOlivier Deprez 
70b058f20aSOlivier Deprez /*******************************************************************************
71b058f20aSOlivier Deprez  * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part
72b058f20aSOlivier Deprez  * of the SPMC initialization path, they will initialize any SPs that they
73b058f20aSOlivier Deprez  * manage. Entry into SPMC is done after initialising minimal architectural
74b058f20aSOlivier Deprez  * state that guarantees safe execution.
75b058f20aSOlivier Deprez  ******************************************************************************/
76b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused)
77b058f20aSOlivier Deprez {
78a92bc73bSOlivier Deprez 	entry_point_info_t *spmc_ep_info = spmd_spmc_ep_info_get();
79b058f20aSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
80a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
8102d50bb0SOlivier Deprez 	uint64_t rc;
82b058f20aSOlivier Deprez 
83a92bc73bSOlivier Deprez 	assert(ctx != NULL);
84b058f20aSOlivier Deprez 	assert(ctx->state != SPMC_STATE_ON);
85a92bc73bSOlivier Deprez 	assert(spmc_ep_info != NULL);
86a92bc73bSOlivier Deprez 
87*473ced56SOlivier Deprez 	spin_lock(&g_spmd_pm.lock);
88*473ced56SOlivier Deprez 
89a92bc73bSOlivier Deprez 	/*
90cdb49d47SOlivier Deprez 	 * Leave the possibility that the SPMC does not call
91cdb49d47SOlivier Deprez 	 * FFA_SECONDARY_EP_REGISTER in which case re-use the
92cdb49d47SOlivier Deprez 	 * primary core address for booting secondary cores.
93a92bc73bSOlivier Deprez 	 */
94cdb49d47SOlivier Deprez 	if (g_spmd_pm.secondary_ep_locked == true) {
95cdb49d47SOlivier Deprez 		spmc_ep_info->pc = g_spmd_pm.secondary_ep;
96a92bc73bSOlivier Deprez 	}
97a92bc73bSOlivier Deprez 
98*473ced56SOlivier Deprez 	spin_unlock(&g_spmd_pm.lock);
99*473ced56SOlivier Deprez 
100a92bc73bSOlivier Deprez 	cm_setup_context(&ctx->cpu_ctx, spmc_ep_info);
101a92bc73bSOlivier Deprez 
102a92bc73bSOlivier Deprez 	/* Mark CPU as initiating ON operation */
103a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
104b058f20aSOlivier Deprez 
105b058f20aSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
10602d50bb0SOlivier Deprez 	if (rc != 0ULL) {
10702d50bb0SOlivier Deprez 		ERROR("%s failed (%llu) on CPU%u\n", __func__, rc,
108b058f20aSOlivier Deprez 			linear_id);
109b058f20aSOlivier Deprez 		ctx->state = SPMC_STATE_OFF;
110b058f20aSOlivier Deprez 		return;
111b058f20aSOlivier Deprez 	}
112b058f20aSOlivier Deprez 
113b058f20aSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
114a92bc73bSOlivier Deprez 
115a92bc73bSOlivier Deprez 	VERBOSE("CPU %u on!\n", linear_id);
116a92bc73bSOlivier Deprez }
117a92bc73bSOlivier Deprez 
118a92bc73bSOlivier Deprez /*******************************************************************************
119a92bc73bSOlivier Deprez  * spmd_cpu_off_handler
120a92bc73bSOlivier Deprez  ******************************************************************************/
121a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused)
122a92bc73bSOlivier Deprez {
123a92bc73bSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
124a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
12502d50bb0SOlivier Deprez 	int64_t rc;
126a92bc73bSOlivier Deprez 
127a92bc73bSOlivier Deprez 	assert(ctx != NULL);
128a92bc73bSOlivier Deprez 	assert(ctx->state != SPMC_STATE_OFF);
129a92bc73bSOlivier Deprez 
130a92bc73bSOlivier Deprez 	/* Build an SPMD to SPMC direct message request. */
131a92bc73bSOlivier Deprez 	spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF);
132a92bc73bSOlivier Deprez 
133a92bc73bSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
13402d50bb0SOlivier Deprez 	if (rc != 0ULL) {
13502d50bb0SOlivier Deprez 		ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id);
136a92bc73bSOlivier Deprez 	}
137a92bc73bSOlivier Deprez 
138cdb49d47SOlivier Deprez 	/* Expect a direct message response from the SPMC. */
139cdb49d47SOlivier Deprez 	u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
140cdb49d47SOlivier Deprez 						  CTX_GPREG_X0);
141cdb49d47SOlivier Deprez 	if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
142cdb49d47SOlivier Deprez 		ERROR("%s invalid SPMC response (%lx).\n",
143cdb49d47SOlivier Deprez 			__func__, ffa_resp_func);
144cdb49d47SOlivier Deprez 		return -EINVAL;
145cdb49d47SOlivier Deprez 	}
146a92bc73bSOlivier Deprez 
147a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_OFF;
148a92bc73bSOlivier Deprez 
149a92bc73bSOlivier Deprez 	VERBOSE("CPU %u off!\n", linear_id);
150a92bc73bSOlivier Deprez 
151a92bc73bSOlivier Deprez 	return 0;
152b058f20aSOlivier Deprez }
153b058f20aSOlivier Deprez 
154b058f20aSOlivier Deprez /*******************************************************************************
155b058f20aSOlivier Deprez  * Structure populated by the SPM Dispatcher to perform any bookkeeping before
156b058f20aSOlivier Deprez  * PSCI executes a power mgmt. operation.
157b058f20aSOlivier Deprez  ******************************************************************************/
158b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = {
159b058f20aSOlivier Deprez 	.svc_on_finish = spmd_cpu_on_finish_handler,
160a92bc73bSOlivier Deprez 	.svc_off = spmd_cpu_off_handler
161b058f20aSOlivier Deprez };
162