xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_pm.c (revision a92bc73b8e93d24a9813c2a6a6c841b3d5cc836c)
1b058f20aSOlivier Deprez /*
2b058f20aSOlivier Deprez  * Copyright (c) 2020, 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*a92bc73bSOlivier Deprez #include <lib/el3_runtime/context_mgmt.h>
10b058f20aSOlivier Deprez #include "spmd_private.h"
11b058f20aSOlivier Deprez 
12f0d743dbSOlivier Deprez struct spmd_pm_secondary_ep_t {
13f0d743dbSOlivier Deprez 	uintptr_t entry_point;
14f0d743dbSOlivier Deprez 	uintptr_t context;
15f0d743dbSOlivier Deprez 	bool locked;
16f0d743dbSOlivier Deprez };
17f0d743dbSOlivier Deprez 
18f0d743dbSOlivier Deprez static struct spmd_pm_secondary_ep_t spmd_pm_secondary_ep[PLATFORM_CORE_COUNT];
19f0d743dbSOlivier Deprez 
20f0d743dbSOlivier Deprez /*******************************************************************************
21*a92bc73bSOlivier Deprez  * spmd_build_spmc_message
22*a92bc73bSOlivier Deprez  *
23*a92bc73bSOlivier Deprez  * Builds an SPMD to SPMC direct message request.
24*a92bc73bSOlivier Deprez  ******************************************************************************/
25*a92bc73bSOlivier Deprez static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message)
26*a92bc73bSOlivier Deprez {
27*a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
28*a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1,
29*a92bc73bSOlivier Deprez 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
30*a92bc73bSOlivier Deprez 		spmd_spmc_id_get());
31*a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ);
32*a92bc73bSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
33*a92bc73bSOlivier Deprez }
34*a92bc73bSOlivier Deprez 
35*a92bc73bSOlivier Deprez /*******************************************************************************
36f0d743dbSOlivier Deprez  * spmd_pm_secondary_core_set_ep
37f0d743dbSOlivier Deprez  ******************************************************************************/
38*a92bc73bSOlivier Deprez int spmd_pm_secondary_core_set_ep(unsigned long long mpidr,
39*a92bc73bSOlivier Deprez 		uintptr_t entry_point, unsigned long long context)
40f0d743dbSOlivier Deprez {
41f0d743dbSOlivier Deprez 	int id = plat_core_pos_by_mpidr(mpidr);
42f0d743dbSOlivier Deprez 
43f0d743dbSOlivier Deprez 	if ((id < 0) || (id >= PLATFORM_CORE_COUNT)) {
44f0d743dbSOlivier Deprez 		ERROR("%s inconsistent MPIDR (%llx)\n", __func__, mpidr);
45f0d743dbSOlivier Deprez 		return -EINVAL;
46f0d743dbSOlivier Deprez 	}
47f0d743dbSOlivier Deprez 
48f0d743dbSOlivier Deprez 	if (spmd_pm_secondary_ep[id].locked) {
49f0d743dbSOlivier Deprez 		ERROR("%s entry locked (%llx)\n", __func__, mpidr);
50f0d743dbSOlivier Deprez 		return -EINVAL;
51f0d743dbSOlivier Deprez 	}
52f0d743dbSOlivier Deprez 
53f0d743dbSOlivier Deprez 	/*
54f0d743dbSOlivier Deprez 	 * Check entry_point address is a PA within
55f0d743dbSOlivier Deprez 	 * load_address <= entry_point < load_address + binary_size
56f0d743dbSOlivier Deprez 	 */
57f0d743dbSOlivier Deprez 	if (!spmd_check_address_in_binary_image(entry_point)) {
58f0d743dbSOlivier Deprez 		ERROR("%s entry point is not within image boundaries (%llx)\n",
59f0d743dbSOlivier Deprez 		      __func__, mpidr);
60f0d743dbSOlivier Deprez 		return -EINVAL;
61f0d743dbSOlivier Deprez 	}
62f0d743dbSOlivier Deprez 
63f0d743dbSOlivier Deprez 	/* Fill new entry to corresponding secondary core id and lock it */
64f0d743dbSOlivier Deprez 	spmd_pm_secondary_ep[id].entry_point = entry_point;
65f0d743dbSOlivier Deprez 	spmd_pm_secondary_ep[id].context = context;
66f0d743dbSOlivier Deprez 	spmd_pm_secondary_ep[id].locked = true;
67f0d743dbSOlivier Deprez 
68f0d743dbSOlivier Deprez 	VERBOSE("%s %d %llx %lx %llx\n",
69f0d743dbSOlivier Deprez 		__func__, id, mpidr, entry_point, context);
70f0d743dbSOlivier Deprez 
71f0d743dbSOlivier Deprez 	return 0;
72f0d743dbSOlivier Deprez }
73f0d743dbSOlivier Deprez 
74b058f20aSOlivier Deprez /*******************************************************************************
75b058f20aSOlivier Deprez  * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part
76b058f20aSOlivier Deprez  * of the SPMC initialization path, they will initialize any SPs that they
77b058f20aSOlivier Deprez  * manage. Entry into SPMC is done after initialising minimal architectural
78b058f20aSOlivier Deprez  * state that guarantees safe execution.
79b058f20aSOlivier Deprez  ******************************************************************************/
80b058f20aSOlivier Deprez static void spmd_cpu_on_finish_handler(u_register_t unused)
81b058f20aSOlivier Deprez {
82*a92bc73bSOlivier Deprez 	entry_point_info_t *spmc_ep_info = spmd_spmc_ep_info_get();
83b058f20aSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
84*a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
85b058f20aSOlivier Deprez 	int rc;
86b058f20aSOlivier Deprez 
87*a92bc73bSOlivier Deprez 	assert(ctx != NULL);
88b058f20aSOlivier Deprez 	assert(ctx->state != SPMC_STATE_ON);
89*a92bc73bSOlivier Deprez 	assert(spmc_ep_info != NULL);
90*a92bc73bSOlivier Deprez 
91*a92bc73bSOlivier Deprez 	/*
92*a92bc73bSOlivier Deprez 	 * TODO: this might require locking the spmc_ep_info structure,
93*a92bc73bSOlivier Deprez 	 * or provisioning one structure per cpu
94*a92bc73bSOlivier Deprez 	 */
95*a92bc73bSOlivier Deprez 	if (spmd_pm_secondary_ep[linear_id].entry_point == 0) {
96*a92bc73bSOlivier Deprez 		goto exit;
97*a92bc73bSOlivier Deprez 	}
98*a92bc73bSOlivier Deprez 
99*a92bc73bSOlivier Deprez 	spmc_ep_info->pc = spmd_pm_secondary_ep[linear_id].entry_point;
100*a92bc73bSOlivier Deprez 	cm_setup_context(&ctx->cpu_ctx, spmc_ep_info);
101*a92bc73bSOlivier Deprez 	write_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx), CTX_GPREG_X0,
102*a92bc73bSOlivier Deprez 		      spmd_pm_secondary_ep[linear_id].context);
103*a92bc73bSOlivier Deprez 
104*a92bc73bSOlivier Deprez 	/* Mark CPU as initiating ON operation */
105*a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
106b058f20aSOlivier Deprez 
107b058f20aSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
108b058f20aSOlivier Deprez 	if (rc != 0) {
109*a92bc73bSOlivier Deprez 		ERROR("%s failed failed (%d) on CPU%u\n", __func__, rc,
110b058f20aSOlivier Deprez 			linear_id);
111b058f20aSOlivier Deprez 		ctx->state = SPMC_STATE_OFF;
112b058f20aSOlivier Deprez 		return;
113b058f20aSOlivier Deprez 	}
114b058f20aSOlivier Deprez 
115*a92bc73bSOlivier Deprez exit:
116b058f20aSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
117*a92bc73bSOlivier Deprez 
118*a92bc73bSOlivier Deprez 	VERBOSE("CPU %u on!\n", linear_id);
119*a92bc73bSOlivier Deprez }
120*a92bc73bSOlivier Deprez 
121*a92bc73bSOlivier Deprez /*******************************************************************************
122*a92bc73bSOlivier Deprez  * spmd_cpu_off_handler
123*a92bc73bSOlivier Deprez  ******************************************************************************/
124*a92bc73bSOlivier Deprez static int32_t spmd_cpu_off_handler(u_register_t unused)
125*a92bc73bSOlivier Deprez {
126*a92bc73bSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
127*a92bc73bSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
128*a92bc73bSOlivier Deprez 	int32_t rc;
129*a92bc73bSOlivier Deprez 
130*a92bc73bSOlivier Deprez 	assert(ctx != NULL);
131*a92bc73bSOlivier Deprez 	assert(ctx->state != SPMC_STATE_OFF);
132*a92bc73bSOlivier Deprez 
133*a92bc73bSOlivier Deprez 	if (spmd_pm_secondary_ep[linear_id].entry_point == 0) {
134*a92bc73bSOlivier Deprez 		goto exit;
135*a92bc73bSOlivier Deprez 	}
136*a92bc73bSOlivier Deprez 
137*a92bc73bSOlivier Deprez 	/* Build an SPMD to SPMC direct message request. */
138*a92bc73bSOlivier Deprez 	spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF);
139*a92bc73bSOlivier Deprez 
140*a92bc73bSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
141*a92bc73bSOlivier Deprez 	if (rc != 0) {
142*a92bc73bSOlivier Deprez 		ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id);
143*a92bc73bSOlivier Deprez 	}
144*a92bc73bSOlivier Deprez 
145*a92bc73bSOlivier Deprez 	/* TODO expect FFA_DIRECT_MSG_RESP returned from SPMC */
146*a92bc73bSOlivier Deprez 
147*a92bc73bSOlivier Deprez exit:
148*a92bc73bSOlivier Deprez 	ctx->state = SPMC_STATE_OFF;
149*a92bc73bSOlivier Deprez 
150*a92bc73bSOlivier Deprez 	VERBOSE("CPU %u off!\n", linear_id);
151*a92bc73bSOlivier Deprez 
152*a92bc73bSOlivier Deprez 	return 0;
153b058f20aSOlivier Deprez }
154b058f20aSOlivier Deprez 
155b058f20aSOlivier Deprez /*******************************************************************************
156b058f20aSOlivier Deprez  * Structure populated by the SPM Dispatcher to perform any bookkeeping before
157b058f20aSOlivier Deprez  * PSCI executes a power mgmt. operation.
158b058f20aSOlivier Deprez  ******************************************************************************/
159b058f20aSOlivier Deprez const spd_pm_ops_t spmd_pm = {
160b058f20aSOlivier Deprez 	.svc_on_finish = spmd_cpu_on_finish_handler,
161*a92bc73bSOlivier Deprez 	.svc_off = spmd_cpu_off_handler
162b058f20aSOlivier Deprez };
163