spmd_pm.c 4.63 KB
Newer Older
1
/*
2
 * Copyright (c) 2020-2021, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8
#include <errno.h>
9
#include <lib/el3_runtime/context_mgmt.h>
10
#include <lib/spinlock.h>
11
12
#include "spmd_private.h"

13
14
15
static struct {
	bool secondary_ep_locked;
	uintptr_t secondary_ep;
16
	spinlock_t lock;
17
18
} g_spmd_pm;

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*******************************************************************************
 * spmd_build_spmc_message
 *
 * Builds an SPMD to SPMC direct message request.
 ******************************************************************************/
static void spmd_build_spmc_message(gp_regs_t *gpregs, unsigned long long message)
{
	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
	write_ctx_reg(gpregs, CTX_GPREG_X1,
		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
		spmd_spmc_id_get());
	write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_PARAM_MBZ);
	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
}

34
/*******************************************************************************
35
 * spmd_pm_secondary_ep_register
36
 ******************************************************************************/
37
int spmd_pm_secondary_ep_register(uintptr_t entry_point)
38
{
39
40
41
42
	int ret = FFA_ERROR_INVALID_PARAMETER;

	spin_lock(&g_spmd_pm.lock);

43
	if (g_spmd_pm.secondary_ep_locked == true) {
44
		goto out;
45
46
47
48
49
50
51
	}

	/*
	 * Check entry_point address is a PA within
	 * load_address <= entry_point < load_address + binary_size
	 */
	if (!spmd_check_address_in_binary_image(entry_point)) {
52
53
		ERROR("%s entry point is not within image boundaries\n",
			__func__);
54
		goto out;
55
56
	}

57
58
	g_spmd_pm.secondary_ep = entry_point;
	g_spmd_pm.secondary_ep_locked = true;
59

60
	VERBOSE("%s %lx\n", __func__, entry_point);
61

62
63
64
65
66
67
	ret = 0;

out:
	spin_unlock(&g_spmd_pm.lock);

	return ret;
68
69
}

70
71
72
73
74
75
76
77
/*******************************************************************************
 * This CPU has been turned on. Enter SPMC to initialise S-EL1 or S-EL2. As part
 * of the SPMC initialization path, they will initialize any SPs that they
 * manage. Entry into SPMC is done after initialising minimal architectural
 * state that guarantees safe execution.
 ******************************************************************************/
static void spmd_cpu_on_finish_handler(u_register_t unused)
{
78
	entry_point_info_t *spmc_ep_info = spmd_spmc_ep_info_get();
79
	spmd_spm_core_context_t *ctx = spmd_get_context();
80
	unsigned int linear_id = plat_my_core_pos();
81
	uint64_t rc;
82

83
	assert(ctx != NULL);
84
	assert(ctx->state != SPMC_STATE_ON);
85
86
	assert(spmc_ep_info != NULL);

87
88
	spin_lock(&g_spmd_pm.lock);

89
	/*
90
91
92
	 * Leave the possibility that the SPMC does not call
	 * FFA_SECONDARY_EP_REGISTER in which case re-use the
	 * primary core address for booting secondary cores.
93
	 */
94
95
	if (g_spmd_pm.secondary_ep_locked == true) {
		spmc_ep_info->pc = g_spmd_pm.secondary_ep;
96
97
	}

98
99
	spin_unlock(&g_spmd_pm.lock);

100
101
102
103
	cm_setup_context(&ctx->cpu_ctx, spmc_ep_info);

	/* Mark CPU as initiating ON operation */
	ctx->state = SPMC_STATE_ON_PENDING;
104
105

	rc = spmd_spm_core_sync_entry(ctx);
106
107
	if (rc != 0ULL) {
		ERROR("%s failed (%llu) on CPU%u\n", __func__, rc,
108
			linear_id);
109
110
111
112
113
		ctx->state = SPMC_STATE_OFF;
		return;
	}

	ctx->state = SPMC_STATE_ON;
114
115
116
117
118
119
120
121
122
123
124

	VERBOSE("CPU %u on!\n", linear_id);
}

/*******************************************************************************
 * spmd_cpu_off_handler
 ******************************************************************************/
static int32_t spmd_cpu_off_handler(u_register_t unused)
{
	spmd_spm_core_context_t *ctx = spmd_get_context();
	unsigned int linear_id = plat_my_core_pos();
125
	int64_t rc;
126
127
128
129
130
131
132
133

	assert(ctx != NULL);
	assert(ctx->state != SPMC_STATE_OFF);

	/* Build an SPMD to SPMC direct message request. */
	spmd_build_spmc_message(get_gpregs_ctx(&ctx->cpu_ctx), PSCI_CPU_OFF);

	rc = spmd_spm_core_sync_entry(ctx);
134
135
	if (rc != 0ULL) {
		ERROR("%s failed (%llu) on CPU%u\n", __func__, rc, linear_id);
136
137
	}

138
139
140
141
142
143
144
145
	/* Expect a direct message response from the SPMC. */
	u_register_t ffa_resp_func = read_ctx_reg(get_gpregs_ctx(&ctx->cpu_ctx),
						  CTX_GPREG_X0);
	if (ffa_resp_func != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
		ERROR("%s invalid SPMC response (%lx).\n",
			__func__, ffa_resp_func);
		return -EINVAL;
	}
146
147
148
149
150
151

	ctx->state = SPMC_STATE_OFF;

	VERBOSE("CPU %u off!\n", linear_id);

	return 0;
152
153
154
155
156
157
158
159
}

/*******************************************************************************
 * Structure populated by the SPM Dispatcher to perform any bookkeeping before
 * PSCI executes a power mgmt. operation.
 ******************************************************************************/
const spd_pm_ops_t spmd_pm = {
	.svc_on_finish = spmd_cpu_on_finish_handler,
160
	.svc_off = spmd_cpu_off_handler
161
};