k3_psci.c 6.43 KB
Newer Older
Benjamin Fair's avatar
Benjamin Fair committed
1
/*
2
 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
Benjamin Fair's avatar
Benjamin Fair committed
3
4
5
6
7
8
9
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <stdbool.h>

10
11
12
13
14
15
#include <arch_helpers.h>
#include <common/debug.h>
#include <lib/el3_runtime/cpu_data.h>
#include <lib/psci/psci.h>
#include <plat/common/platform.h>

16
#include <ti_sci_protocol.h>
17
#include <k3_gicv3.h>
18
19
#include <ti_sci.h>

20
21
22
23
#define CORE_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL0])
#define CLUSTER_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL1])
#define SYSTEM_PWR_STATE(state) ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL])

Benjamin Fair's avatar
Benjamin Fair committed
24
25
26
27
uintptr_t k3_sec_entrypoint;

static void k3_cpu_standby(plat_local_state_t cpu_state)
{
28
	u_register_t scr;
29
30
31
32
33
34

	scr = read_scr_el3();
	/* Enable the Non secure interrupt to wake the CPU */
	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
	isb();
	/* dsb is good practice before using wfi to enter low power states */
Benjamin Fair's avatar
Benjamin Fair committed
35
	dsb();
36
	/* Enter standby state */
Benjamin Fair's avatar
Benjamin Fair committed
37
	wfi();
38
39
	/* Restore SCR */
	write_scr_el3(scr);
Benjamin Fair's avatar
Benjamin Fair committed
40
41
42
43
}

static int k3_pwr_domain_on(u_register_t mpidr)
{
44
	int core, proc_id, device_id, ret;
45

46
47
48
	core = plat_core_pos_by_mpidr(mpidr);
	if (core < 0) {
		ERROR("Could not get target core id: %d\n", core);
49
50
51
		return PSCI_E_INTERN_FAIL;
	}

52
53
	proc_id = PLAT_PROC_START_ID + core;
	device_id = PLAT_PROC_DEVICE_START_ID + core;
54

55
	ret = ti_sci_proc_request(proc_id);
56
57
58
59
60
	if (ret) {
		ERROR("Request for processor failed: %d\n", ret);
		return PSCI_E_INTERN_FAIL;
	}

61
	ret = ti_sci_proc_set_boot_cfg(proc_id, k3_sec_entrypoint, 0, 0);
62
63
64
65
66
	if (ret) {
		ERROR("Request to set core boot address failed: %d\n", ret);
		return PSCI_E_INTERN_FAIL;
	}

67
68
69
70
71
72
73
74
75
76
	/* sanity check these are off before starting a core */
	ret = ti_sci_proc_set_boot_ctrl(proc_id,
			0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ |
			   PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS |
			   PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM);
	if (ret) {
		ERROR("Request to clear boot configuration failed: %d\n", ret);
		return PSCI_E_INTERN_FAIL;
	}

77
	ret = ti_sci_device_get(device_id);
78
79
80
81
	if (ret) {
		ERROR("Request to start core failed: %d\n", ret);
		return PSCI_E_INTERN_FAIL;
	}
Benjamin Fair's avatar
Benjamin Fair committed
82
83
84
85
86
87

	return PSCI_E_SUCCESS;
}

void k3_pwr_domain_off(const psci_power_state_t *target_state)
{
88
89
90
91
	int core, cluster, proc_id, device_id, cluster_id, ret;

	/* At very least the local core should be powering down */
	assert(CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE);
92

Benjamin Fair's avatar
Benjamin Fair committed
93
94
95
	/* Prevent interrupts from spuriously waking up this cpu */
	k3_gic_cpuif_disable();

96
	core = plat_my_core_pos();
97
	cluster = MPIDR_AFFLVL1_VAL(read_mpidr_el1());
98
99
	proc_id = PLAT_PROC_START_ID + core;
	device_id = PLAT_PROC_DEVICE_START_ID + core;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	cluster_id = PLAT_CLUSTER_DEVICE_START_ID + (cluster * 2);

	/*
	 * If we are the last core in the cluster then we take a reference to
	 * the cluster device so that it does not get shutdown before we
	 * execute the entire cluster L2 cleaning sequence below.
	 */
	if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
		ret = ti_sci_device_get(cluster_id);
		if (ret) {
			ERROR("Request to get cluster failed: %d\n", ret);
			return;
		}
	}
114

115
	/* Start by sending wait for WFI command */
116
	ret = ti_sci_proc_wait_boot_status_no_wait(proc_id,
117
118
119
120
121
122
123
			/*
			 * Wait maximum time to give us the best chance to get
			 * to WFI before this command timeouts
			 */
			UINT8_MAX, 100, UINT8_MAX, UINT8_MAX,
			/* Wait for WFI */
			PROC_BOOT_STATUS_FLAG_ARMV8_WFI, 0, 0, 0);
124
	if (ret) {
125
126
127
128
129
		ERROR("Sending wait for WFI failed (%d)\n", ret);
		return;
	}

	/* Now queue up the core shutdown request */
130
	ret = ti_sci_device_put_no_wait(device_id);
131
132
	if (ret) {
		ERROR("Sending core shutdown message failed (%d)\n", ret);
133
134
		return;
	}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

	/* If our cluster is not going down we stop here */
	if (CLUSTER_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE)
		return;

	/* set AINACTS */
	ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
			PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS, 0);
	if (ret) {
		ERROR("Sending set control message failed (%d)\n", ret);
		return;
	}

	/* set L2FLUSHREQ */
	ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
			PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ, 0);
	if (ret) {
		ERROR("Sending set control message failed (%d)\n", ret);
		return;
	}

	/* wait for L2FLUSHDONE*/
	ret = ti_sci_proc_wait_boot_status_no_wait(proc_id,
			UINT8_MAX, 2, UINT8_MAX, UINT8_MAX,
			PROC_BOOT_STATUS_FLAG_ARMV8_L2F_DONE, 0, 0, 0);
	if (ret) {
		ERROR("Sending wait message failed (%d)\n", ret);
		return;
	}

	/* clear L2FLUSHREQ */
	ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
			0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ);
	if (ret) {
		ERROR("Sending set control message failed (%d)\n", ret);
		return;
	}

	/* set ACINACTM */
	ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
			PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM, 0);
	if (ret) {
		ERROR("Sending set control message failed (%d)\n", ret);
		return;
	}

	/* wait for STANDBYWFIL2 */
	ret = ti_sci_proc_wait_boot_status_no_wait(proc_id,
			UINT8_MAX, 2, UINT8_MAX, UINT8_MAX,
			PROC_BOOT_STATUS_FLAG_ARMV8_STANDBYWFIL2, 0, 0, 0);
	if (ret) {
		ERROR("Sending wait message failed (%d)\n", ret);
		return;
	}

	/* Now queue up the cluster shutdown request */
	ret = ti_sci_device_put_no_wait(cluster_id);
	if (ret) {
		ERROR("Sending cluster shutdown message failed (%d)\n", ret);
		return;
	}
Benjamin Fair's avatar
Benjamin Fair committed
196
197
198
199
200
201
202
203
204
205
}

void k3_pwr_domain_on_finish(const psci_power_state_t *target_state)
{
	/* TODO: Indicate to System firmware about completion */

	k3_gic_pcpu_init();
	k3_gic_cpuif_enable();
}

206
207
208
209
210
211
212
static void __dead2 k3_system_off(void)
{
	ERROR("System Off: operation not handled.\n");
	while (true)
		wfi();
}

Benjamin Fair's avatar
Benjamin Fair committed
213
214
static void __dead2 k3_system_reset(void)
{
215
216
	/* Send the system reset request to system firmware */
	ti_sci_core_reboot();
Benjamin Fair's avatar
Benjamin Fair committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

	while (true)
		wfi();
}

static int k3_validate_power_state(unsigned int power_state,
				   psci_power_state_t *req_state)
{
	/* TODO: perform the proper validation */

	return PSCI_E_SUCCESS;
}

static int k3_validate_ns_entrypoint(uintptr_t entrypoint)
{
	/* TODO: perform the proper validation */

	return PSCI_E_SUCCESS;
}

static const plat_psci_ops_t k3_plat_psci_ops = {
	.cpu_standby = k3_cpu_standby,
	.pwr_domain_on = k3_pwr_domain_on,
	.pwr_domain_off = k3_pwr_domain_off,
	.pwr_domain_on_finish = k3_pwr_domain_on_finish,
242
	.system_off = k3_system_off,
Benjamin Fair's avatar
Benjamin Fair committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
	.system_reset = k3_system_reset,
	.validate_power_state = k3_validate_power_state,
	.validate_ns_entrypoint = k3_validate_ns_entrypoint
};

int plat_setup_psci_ops(uintptr_t sec_entrypoint,
			const plat_psci_ops_t **psci_ops)
{
	k3_sec_entrypoint = sec_entrypoint;

	*psci_ops = &k3_plat_psci_ops;

	return 0;
}