xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_pm.c (revision 9944f55761c4d5cc1feefaf5e33bf7fb83d8f5f3)
1b058f20aSOlivier Deprez /*
2*9944f557SDaniel Boulby  * Copyright (c) 2020-2022, 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;
120a92bc73bSOlivier Deprez 
121a92bc73bSOlivier Deprez 	assert(ctx != NULL);
122a92bc73bSOlivier Deprez 	assert(ctx->state != SPMC_STATE_OFF);
123a92bc73bSOlivier Deprez 
124a92bc73bSOlivier Deprez 	/* Build an SPMD to SPMC direct message request. */
125*9944f557SDaniel Boulby 	spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx),
126*9944f557SDaniel Boulby 				SPMD_FWK_MSG_PSCI, PSCI_CPU_OFF);
127a92bc73bSOlivier Deprez 
128a92bc73bSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
12902d50bb0SOlivier Deprez 	if (rc != 0ULL) {
1304ce3e99aSScott Branden 		ERROR("%s failed (%" PRIu64 ") on CPU%u\n", __func__, rc, linear_id);
131a92bc73bSOlivier Deprez 	}
132a92bc73bSOlivier Deprez 
133cdb49d47SOlivier Deprez 	/* Expect a direct message response from the SPMC. */
134cdb49d47SOlivier Deprez 	u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
135cdb49d47SOlivier Deprez 						  CTX_GPREG_X0);
136cdb49d47SOlivier Deprez 	if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
137cdb49d47SOlivier Deprez 		ERROR("%s invalid SPMC response (%lx).\n",
138cdb49d47SOlivier Deprez 			__func__, ffa_resp_func);
139cdb49d47SOlivier Deprez 		return -EINVAL;
140cdb49d47SOlivier Deprez 	}
141a92bc73bSOlivier Deprez 
142a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_OFF;
143a92bc73bSOlivier Deprez 
144a92bc73bSOlivier Deprez 	VERBOSE("CPU %u off!\n", linear_id);
145a92bc73bSOlivier Deprez 
146a92bc73bSOlivier Deprez 	return 0;
147b058f20aSOlivier Deprez }
148b058f20aSOlivier Deprez 
149b058f20aSOlivier Deprez /*******************************************************************************
150b058f20aSOlivier Deprez  * Structure populated by the SPM Dispatcher to perform any bookkeeping before
151b058f20aSOlivier Deprez  * PSCI executes a power mgmt. operation.
152b058f20aSOlivier Deprez  ******************************************************************************/
153b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = {
154b058f20aSOlivier Deprez 	.svc_on_finish = spmd_cpu_on_finish_handler,
155a92bc73bSOlivier Deprez 	.svc_off = spmd_cpu_off_handler
156b058f20aSOlivier Deprez };
157