tegra_pm.c 12.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
 */

#include <assert.h>
8
9
10
11
12
13

#include <platform_def.h>

#include <arch_helpers.h>
#include <common/bl_common.h>
#include <common/debug.h>
14
#include <context.h>
15
16
17
18
19
20
#include <drivers/console.h>
#include <lib/el3_runtime/context_mgmt.h>
#include <lib/mmio.h>
#include <lib/psci/psci.h>
#include <plat/common/platform.h>

21
22
23
#include <memctrl.h>
#include <pmc.h>
#include <tegra_def.h>
24
#include <tegra_platform.h>
25
26
27
#include <tegra_private.h>

extern uint64_t tegra_bl31_phys_base;
28
extern uint64_t tegra_sec_entry_point;
29
30
31
32
33

/*
 * The following platform setup functions are weakly defined. They
 * provide typical implementations that will be overridden by a SoC.
 */
34
#pragma weak tegra_soc_pwr_domain_suspend_pwrdown_early
35
#pragma weak tegra_soc_cpu_standby
36
37
38
39
#pragma weak tegra_soc_pwr_domain_suspend
#pragma weak tegra_soc_pwr_domain_on
#pragma weak tegra_soc_pwr_domain_off
#pragma weak tegra_soc_pwr_domain_on_finish
40
#pragma weak tegra_soc_pwr_domain_power_down_wfi
41
#pragma weak tegra_soc_prepare_system_reset
42
#pragma weak tegra_soc_prepare_system_off
43
#pragma weak tegra_soc_get_target_pwr_state
44

Anthony Zhou's avatar
Anthony Zhou committed
45
int32_t tegra_soc_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
46
47
48
49
{
	return PSCI_E_NOT_SUPPORTED;
}

50
51
52
53
54
55
int32_t tegra_soc_cpu_standby(plat_local_state_t cpu_state)
{
	(void)cpu_state;
	return PSCI_E_SUCCESS;
}

Anthony Zhou's avatar
Anthony Zhou committed
56
int32_t tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
57
{
Anthony Zhou's avatar
Anthony Zhou committed
58
	(void)target_state;
59
60
61
	return PSCI_E_NOT_SUPPORTED;
}

Anthony Zhou's avatar
Anthony Zhou committed
62
int32_t tegra_soc_pwr_domain_on(u_register_t mpidr)
63
{
Anthony Zhou's avatar
Anthony Zhou committed
64
	(void)mpidr;
65
66
67
	return PSCI_E_SUCCESS;
}

Anthony Zhou's avatar
Anthony Zhou committed
68
int32_t tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
69
{
Anthony Zhou's avatar
Anthony Zhou committed
70
	(void)target_state;
71
72
73
	return PSCI_E_SUCCESS;
}

Anthony Zhou's avatar
Anthony Zhou committed
74
int32_t tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
75
{
Anthony Zhou's avatar
Anthony Zhou committed
76
	(void)target_state;
77
78
79
	return PSCI_E_SUCCESS;
}

Anthony Zhou's avatar
Anthony Zhou committed
80
int32_t tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
81
{
Anthony Zhou's avatar
Anthony Zhou committed
82
	(void)target_state;
83
84
85
	return PSCI_E_SUCCESS;
}

Anthony Zhou's avatar
Anthony Zhou committed
86
int32_t tegra_soc_prepare_system_reset(void)
87
88
89
90
{
	return PSCI_E_SUCCESS;
}

91
92
93
94
95
96
__dead2 void tegra_soc_prepare_system_off(void)
{
	ERROR("Tegra System Off: operation not handled.\n");
	panic();
}

Anthony Zhou's avatar
Anthony Zhou committed
97
plat_local_state_t tegra_soc_get_target_pwr_state(uint32_t lvl,
98
					     const plat_local_state_t *states,
Anthony Zhou's avatar
Anthony Zhou committed
99
					     uint32_t ncpu)
100
{
101
	plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
Anthony Zhou's avatar
Anthony Zhou committed
102
103
104
105
	uint32_t num_cpu = ncpu;
	const plat_local_state_t *local_state = states;

	(void)lvl;
106

107
	assert(ncpu != 0U);
108
109

	do {
Anthony Zhou's avatar
Anthony Zhou committed
110
111
		temp = *local_state;
		if ((temp < target)) {
112
			target = temp;
Anthony Zhou's avatar
Anthony Zhou committed
113
114
115
116
		}
		--num_cpu;
		local_state++;
	} while (num_cpu != 0U);
117
118
119
120

	return target;
}

121
/*******************************************************************************
122
123
124
125
126
127
 * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
 * call to get the `power_state` parameter. This allows the platform to encode
 * the appropriate State-ID field within the `power_state` parameter which can
 * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
******************************************************************************/
void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
128
{
129
	/* all affinities use system suspend state id */
Anthony Zhou's avatar
Anthony Zhou committed
130
	for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) {
131
		req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
Anthony Zhou's avatar
Anthony Zhou committed
132
	}
133
134
135
136
137
}

/*******************************************************************************
 * Handler called when an affinity instance is about to enter standby.
 ******************************************************************************/
138
void tegra_cpu_standby(plat_local_state_t cpu_state)
139
{
140
141
	u_register_t saved_scr_el3;

Anthony Zhou's avatar
Anthony Zhou committed
142
143
	(void)cpu_state;

144
145
146
147
	/* Tegra SoC specific handler */
	if (tegra_soc_cpu_standby(cpu_state) != PSCI_E_SUCCESS)
		ERROR("%s failed\n", __func__);

148
149
150
151
152
153
154
155
156
	saved_scr_el3 = read_scr_el3();

	/*
	 * As per ARM ARM D1.17.2, any physical IRQ interrupt received by the
	 * PE will be treated as a wake-up event, if SCR_EL3.IRQ is set to '1',
	 * irrespective of the value of the PSTATE.I bit value.
	 */
	write_scr_el3(saved_scr_el3 | SCR_IRQ_BIT);

157
158
	/*
	 * Enter standby state
159
160
	 *
	 * dsb & isb is good practice before using wfi to enter low power states
161
162
	 */
	dsb();
163
	isb();
164
	wfi();
165
166
167
168
169
170

	/*
	 * Restore saved scr_el3 that has IRQ bit cleared as we don't want EL3
	 * handling any further interrupts
	 */
	write_scr_el3(saved_scr_el3);
171
172
173
174
175
176
}

/*******************************************************************************
 * Handler called when an affinity instance is about to be turned on. The
 * level and mpidr determine the affinity instance.
 ******************************************************************************/
Anthony Zhou's avatar
Anthony Zhou committed
177
int32_t tegra_pwr_domain_on(u_register_t mpidr)
178
{
179
	return tegra_soc_pwr_domain_on(mpidr);
180
181
182
}

/*******************************************************************************
183
184
 * Handler called when a power domain is about to be turned off. The
 * target_state encodes the power state that each level should transition to.
185
 ******************************************************************************/
186
void tegra_pwr_domain_off(const psci_power_state_t *target_state)
187
{
Anthony Zhou's avatar
Anthony Zhou committed
188
	(void)tegra_soc_pwr_domain_off(target_state);
189
190
}

191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Handler called when a power domain is about to be suspended. The
 * target_state encodes the power state that each level should transition to.
 * This handler is called with SMP and data cache enabled, when
 * HW_ASSISTED_COHERENCY = 0
 ******************************************************************************/
void tegra_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
{
	tegra_soc_pwr_domain_suspend_pwrdown_early(target_state);
}

202
/*******************************************************************************
203
 * Handler called when a power domain is about to be suspended. The
204
 * target_state encodes the power state that each level should transition to.
205
 ******************************************************************************/
206
void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
207
{
Anthony Zhou's avatar
Anthony Zhou committed
208
	(void)tegra_soc_pwr_domain_suspend(target_state);
209

210
211
	/* Disable console if we are entering deep sleep. */
	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
Anthony Zhou's avatar
Anthony Zhou committed
212
			PSTATE_ID_SOC_POWERDN) {
213
214
		(void)console_flush();
		console_switch_state(0);
Anthony Zhou's avatar
Anthony Zhou committed
215
	}
216

217
218
219
220
	/* disable GICC */
	tegra_gic_cpuif_deactivate();
}

221
222
223
224
225
226
227
228
/*******************************************************************************
 * Handler called at the end of the power domain suspend sequence. The
 * target_state encodes the power state that each level should transition to.
 ******************************************************************************/
__dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
					     *target_state)
{
	/* call the chip's power down handler */
Anthony Zhou's avatar
Anthony Zhou committed
229
	(void)tegra_soc_pwr_domain_power_down_wfi(target_state);
230

231
	wfi();
232
233
234
	panic();
}

235
/*******************************************************************************
236
237
238
 * Handler called when a power domain has just been powered on after
 * being turned off earlier. The target_state encodes the low power state that
 * each level has woken up from.
239
 ******************************************************************************/
240
void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
241
{
Anthony Zhou's avatar
Anthony Zhou committed
242
	const plat_params_from_bl2_t *plat_params;
243
244
245
246

	/*
	 * Initialize the GIC cpu and distributor interfaces
	 */
247
	tegra_gic_init();
248
249
250
251

	/*
	 * Check if we are exiting from deep sleep.
	 */
252
253
	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
			PSTATE_ID_SOC_POWERDN) {
254

255
256
		/* Restart console output. */
		console_switch_state(CONSOLE_FLAG_RUNTIME);
257

258
		/*
259
260
		 * Restore Memory Controller settings as it loses state
		 * during system suspend.
261
		 */
262
		tegra_memctrl_restore_settings();
263
264
265
266
267

		/*
		 * Security configuration to allow DRAM/device access.
		 */
		plat_params = bl31_get_plat_params();
268
		tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
Anthony Zhou's avatar
Anthony Zhou committed
269
			(uint32_t)plat_params->tzdram_size);
270
271
272
273
274
275

		/*
		 * Set up the TZRAM memory aperture to allow only secure world
		 * access
		 */
		tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
276
277
278
279
280
	}

	/*
	 * Reset hardware settings.
	 */
Anthony Zhou's avatar
Anthony Zhou committed
281
	(void)tegra_soc_pwr_domain_on_finish(target_state);
282
283
284
}

/*******************************************************************************
285
286
287
 * Handler called when a power domain has just been powered on after
 * having been suspended earlier. The target_state encodes the low power state
 * that each level has woken up from.
288
 ******************************************************************************/
289
void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
290
{
291
	tegra_pwr_domain_on_finish(target_state);
292
293
294
295
296
297
298
}

/*******************************************************************************
 * Handler called when the system wants to be powered off
 ******************************************************************************/
__dead2 void tegra_system_off(void)
{
299
300
301
	INFO("Powering down system...\n");

	tegra_soc_prepare_system_off();
302
303
304
305
306
307
308
}

/*******************************************************************************
 * Handler called when the system wants to be restarted.
 ******************************************************************************/
__dead2 void tegra_system_reset(void)
{
309
310
	INFO("Restarting system...\n");

311
	/* per-SoC system reset handler */
Anthony Zhou's avatar
Anthony Zhou committed
312
	(void)tegra_soc_prepare_system_reset();
313

314
315
316
317
318
319
	/*
	 * Program the PMC in order to restart the system.
	 */
	tegra_pmc_system_reset();
}

320
321
322
/*******************************************************************************
 * Handler called to check the validity of the power state parameter.
 ******************************************************************************/
Anthony Zhou's avatar
Anthony Zhou committed
323
int32_t tegra_validate_power_state(uint32_t power_state,
324
325
				   psci_power_state_t *req_state)
{
326
	assert(req_state != NULL);
327
328
329
330
331
332
333

	return tegra_soc_validate_power_state(power_state, req_state);
}

/*******************************************************************************
 * Platform handler called to check the validity of the non secure entrypoint.
 ******************************************************************************/
Anthony Zhou's avatar
Anthony Zhou committed
334
int32_t tegra_validate_ns_entrypoint(uintptr_t entrypoint)
335
{
Anthony Zhou's avatar
Anthony Zhou committed
336
337
	int32_t ret = PSCI_E_INVALID_ADDRESS;

338
339
340
341
	/*
	 * Check if the non secure entrypoint lies within the non
	 * secure DRAM.
	 */
Anthony Zhou's avatar
Anthony Zhou committed
342
343
344
	if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END)) {
		ret = PSCI_E_SUCCESS;
	}
345

Anthony Zhou's avatar
Anthony Zhou committed
346
	return ret;
347
348
}

349
350
351
/*******************************************************************************
 * Export the platform handlers to enable psci to invoke them
 ******************************************************************************/
352
353
354
355
static const plat_psci_ops_t tegra_plat_psci_ops = {
	.cpu_standby			= tegra_cpu_standby,
	.pwr_domain_on			= tegra_pwr_domain_on,
	.pwr_domain_off			= tegra_pwr_domain_off,
356
	.pwr_domain_suspend_pwrdown_early = tegra_pwr_domain_suspend_pwrdown_early,
357
358
359
	.pwr_domain_suspend		= tegra_pwr_domain_suspend,
	.pwr_domain_on_finish		= tegra_pwr_domain_on_finish,
	.pwr_domain_suspend_finish	= tegra_pwr_domain_suspend_finish,
360
	.pwr_domain_pwr_down_wfi	= tegra_pwr_domain_power_down_wfi,
361
362
363
364
365
	.system_off			= tegra_system_off,
	.system_reset			= tegra_system_reset,
	.validate_power_state		= tegra_validate_power_state,
	.validate_ns_entrypoint		= tegra_validate_ns_entrypoint,
	.get_sys_suspend_power_state	= tegra_get_sys_suspend_power_state,
366
367
368
};

/*******************************************************************************
369
 * Export the platform specific power ops and initialize Power Controller
370
 ******************************************************************************/
371
372
int plat_setup_psci_ops(uintptr_t sec_entrypoint,
			const plat_psci_ops_t **psci_ops)
373
{
374
375
376
377
378
379
380
381
382
	psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };

	/*
	 * Flush entrypoint variable to PoC since it will be
	 * accessed after a reset with the caches turned off.
	 */
	tegra_sec_entry_point = sec_entrypoint;
	flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));

383
384
385
	/*
	 * Reset hardware settings.
	 */
Anthony Zhou's avatar
Anthony Zhou committed
386
	(void)tegra_soc_pwr_domain_on_finish(&target_state);
387
388

	/*
389
	 * Initialize PSCI ops struct
390
	 */
391
	*psci_ops = &tegra_plat_psci_ops;
392
393
394

	return 0;
}
395
396
397
398
399
400
401
402
403

/*******************************************************************************
 * Platform handler to calculate the proper target power level at the
 * specified affinity level
 ******************************************************************************/
plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
					     const plat_local_state_t *states,
					     unsigned int ncpu)
{
404
	return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
405
}