plat_pm.c 15.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2019, MediaTek Inc. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/* common headers */
#include <arch_helpers.h>
#include <assert.h>
#include <common/debug.h>
#include <lib/mmio.h>
#include <lib/psci/psci.h>
#include <errno.h>

/* mediatek platform specific headers */
#include <platform_def.h>
#include <scu.h>
18
#include <mt_gic_v3.h>
19
#include <mtk_mcdi.h>
20
#include <mtk_plat_common.h>
21
#include <mtgpio.h>
22
23
24
#include <mtspmc.h>
#include <plat_dcm.h>
#include <plat_debug.h>
25
#include <plat_params.h>
26
#include <plat_private.h>
27
#include <power_tracer.h>
28
#include <pmic.h>
29
30
#include <spm.h>
#include <spm_suspend.h>
31
#include <sspm.h>
32
#include <rtc.h>
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Local power state for power domains in Run state. */
#define MTK_LOCAL_STATE_RUN	0
/* Local power state for retention. */
#define MTK_LOCAL_STATE_RET	1
/* Local power state for OFF/power-down. */
#define MTK_LOCAL_STATE_OFF	2

#if PSCI_EXTENDED_STATE_ID
/*
 * Macros used to parse state information from State-ID if it is using the
 * recommended encoding for State-ID.
 */
#define MTK_LOCAL_PSTATE_WIDTH		4
#define MTK_LOCAL_PSTATE_MASK		((1 << MTK_LOCAL_PSTATE_WIDTH) - 1)

/* Macros to construct the composite power state */

/* Make composite power state parameter till power level 0 */

#define mtk_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
	(((lvl0_state) << PSTATE_ID_SHIFT) | ((type) << PSTATE_TYPE_SHIFT))

#else /* !PSCI_EXTENDED_STATE_ID */

#define mtk_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
		(((lvl0_state) << PSTATE_ID_SHIFT) | \
		((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
		((type) << PSTATE_TYPE_SHIFT))

#endif /* PSCI_EXTENDED_STATE_ID */

/* Make composite power state parameter till power level 1 */
#define mtk_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
		(((lvl1_state) << MTK_LOCAL_PSTATE_WIDTH) | \
		mtk_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))

/* Make composite power state parameter till power level 2 */
#define mtk_make_pwrstate_lvl2( \
		lvl2_state, lvl1_state, lvl0_state, pwr_lvl, type) \
		(((lvl2_state) << (MTK_LOCAL_PSTATE_WIDTH * 2)) | \
		mtk_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type))

#define MTK_PWR_LVL0	0
#define MTK_PWR_LVL1	1
#define MTK_PWR_LVL2	2

/* Macros to read the MTK power domain state */
#define MTK_CORE_PWR_STATE(state)	(state)->pwr_domain_state[MTK_PWR_LVL0]
#define MTK_CLUSTER_PWR_STATE(state)	(state)->pwr_domain_state[MTK_PWR_LVL1]
#define MTK_SYSTEM_PWR_STATE(state)	((PLAT_MAX_PWR_LVL > MTK_PWR_LVL1) ? \
			(state)->pwr_domain_state[MTK_PWR_LVL2] : 0)

#if PSCI_EXTENDED_STATE_ID
/*
 *  The table storing the valid idle power states. Ensure that the
 *  array entries are populated in ascending order of state-id to
 *  enable us to use binary search during power state validation.
 *  The table must be terminated by a NULL entry.
 */
const unsigned int mtk_pm_idle_states[] = {
	/* State-id - 0x001 */
	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_RUN, MTK_LOCAL_STATE_RUN,
		MTK_LOCAL_STATE_RET, MTK_PWR_LVL0, PSTATE_TYPE_STANDBY),
	/* State-id - 0x002 */
	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_RUN, MTK_LOCAL_STATE_RUN,
		MTK_LOCAL_STATE_OFF, MTK_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
	/* State-id - 0x022 */
	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_RUN, MTK_LOCAL_STATE_OFF,
		MTK_LOCAL_STATE_OFF, MTK_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
#if PLAT_MAX_PWR_LVL > MTK_PWR_LVL1
	/* State-id - 0x222 */
	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_OFF, MTK_LOCAL_STATE_OFF,
		MTK_LOCAL_STATE_OFF, MTK_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
#endif
	0,
};
#endif

#define CPU_IDX(cluster, cpu)		((cluster << 2) + cpu)
#define ON	true
#define OFF	false

/* Pause MCDI when CPU hotplug */
static bool HP_SSPM_PAUSE;
/* CPU Hotplug by SSPM */
static bool HP_SSPM_CTRL = true;
/* Turn off cluster when CPU hotplug off */
static bool HP_CLUSTER_OFF = true;
/* Turn off cluster when CPU MCDI off */
static bool MCDI_C2 = true;
/* Enable MCDI */
static bool MCDI_SSPM = true;
126
127
128
129
130
131
132
133
134
135

static uintptr_t secure_entrypoint;

static void mp1_L2_desel_config(void)
{
	mmio_write_64(MCUCFG_BASE + 0x2200, 0x2092c820);

	dsb();
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
static bool clst_single_pwr(int cluster, int cpu)
{
	uint32_t cpu_mask[2] = {0x00001e00, 0x000f0000};
	uint32_t cpu_pwr_bit[] = {9, 10, 11, 12, 16, 17, 18, 19};
	int my_idx = (cluster << 2) + cpu;
	uint32_t pwr_stat = mmio_read_32(0x10006180);

	return !(pwr_stat & (cpu_mask[cluster] & ~BIT(cpu_pwr_bit[my_idx])));
}

static bool clst_single_on(int cluster, int cpu)
{
	uint32_t cpu_mask[2] = {0x0f, 0xf0};
	int my_idx = (cluster << 2) + cpu;
	uint32_t on_stat = mcdi_avail_cpu_mask_read();

	return !(on_stat & (cpu_mask[cluster] & ~BIT(my_idx)));
}

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
static void plat_cpu_pwrdwn_common(void)
{
	/* Prevent interrupts from spuriously waking up this cpu */
	mt_gic_rdistif_save();
	mt_gic_cpuif_disable();
}

static void plat_cpu_pwron_common(void)
{
	/* Enable the gic cpu interface */
	mt_gic_cpuif_enable();
	mt_gic_rdistif_init();
	mt_gic_rdistif_restore();
}

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
static void plat_cluster_pwrdwn_common(uint64_t mpidr, int cluster)
{
	if (cluster > 0)
		mt_gic_sync_dcm_enable();

	/* Disable coherency */
	plat_mtk_cci_disable();
	disable_scu(mpidr);
}

static void plat_cluster_pwron_common(uint64_t mpidr, int cluster)
{
	if (cluster > 0) {
		l2c_parity_check_setup();
		circular_buffer_setup();
		mp1_L2_desel_config();
		mt_gic_sync_dcm_disable();
	}

	/* Enable coherency */
	enable_scu(mpidr);
	plat_mtk_cci_enable();
	/* Enable big core dcm */
	plat_dcm_restore_cluster_on(mpidr);
	/* Enable rgu dcm */
	plat_dcm_rgu_enable();
}

static void plat_cpu_standby(plat_local_state_t cpu_state)
{
	unsigned int scr;

	scr = read_scr_el3();
	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);

	isb();
	dsb();
	wfi();

	write_scr_el3(scr);
}

static void mcdi_ctrl_before_hotplug_on(int cluster, int cpu)
{
	if (!HP_SSPM_CTRL && HP_SSPM_PAUSE && MCDI_SSPM) {
		mcdi_pause_clr(cluster, CPU_IDX(cluster, cpu), OFF);
		mcdi_pause_set(cluster, CPU_IDX(cluster, cpu), ON);
	}
}

static void mcdi_ctrl_before_hotplug_off(int cluster, int cpu, bool cluster_off)
{
	if (!HP_SSPM_CTRL && HP_SSPM_PAUSE && MCDI_SSPM)
		mcdi_pause_set(cluster_off ? cluster : -1,
				CPU_IDX(cluster, cpu), OFF);
}

static void mcdi_ctrl_cluster_cpu_off(int cluster, int cpu, bool cluster_off)
{
	if (MCDI_SSPM) {
		sspm_set_bootaddr(secure_entrypoint);

		sspm_standbywfi_irq_enable(CPU_IDX(cluster, cpu));

		if (cluster_off)
			sspm_cluster_pwr_off_notify(cluster);
		else
			sspm_cluster_pwr_on_notify(cluster);
	}
}

static void mcdi_ctrl_suspend(void)
{
	if (MCDI_SSPM)
		mcdi_pause();
}

static void mcdi_ctrl_resume(void)
{
	if (MCDI_SSPM)
		mcdi_unpause();
}

static void hotplug_ctrl_cluster_on(int cluster, int cpu)
{
	if (HP_SSPM_CTRL && MCDI_SSPM) {
		mcdi_hotplug_clr(cluster, CPU_IDX(cluster, cpu), OFF);
		mcdi_hotplug_set(cluster, -1, ON);
		mcdi_hotplug_wait_ack(cluster, -1, ON);
	} else {
		/* power on cluster */
		if (!spm_get_cluster_powerstate(cluster))
			spm_poweron_cluster(cluster);
	}
}

static void hotplug_ctrl_cpu_on(int cluster, int cpu)
{
	if (HP_SSPM_CTRL && MCDI_SSPM)
		mcdi_hotplug_set(cluster, CPU_IDX(cluster, cpu), ON);
	else
		spm_poweron_cpu(cluster, cpu);
}

static void hotplug_ctrl_cpu_on_finish(int cluster, int cpu)
{
	spm_disable_cpu_auto_off(cluster, cpu);

	if (HP_SSPM_CTRL && MCDI_SSPM)
		mcdi_hotplug_clr(cluster, CPU_IDX(cluster, cpu), ON);
	else if (HP_SSPM_PAUSE && MCDI_SSPM)
		mcdi_pause_clr(cluster, CPU_IDX(cluster, cpu), ON);

	mcdi_avail_cpu_mask_set(BIT(CPU_IDX(cluster, cpu)));
}

static void hotplug_ctrl_cluster_cpu_off(int cluster, int cpu, bool cluster_off)
{
	mcdi_avail_cpu_mask_clr(BIT(CPU_IDX(cluster, cpu)));

	if (HP_SSPM_CTRL && MCDI_SSPM) {
		mcdi_hotplug_set(cluster_off ? cluster : -1,
				CPU_IDX(cluster, cpu), OFF);
	} else {
		spm_enable_cpu_auto_off(cluster, cpu);

		if (cluster_off)
			spm_enable_cluster_auto_off(cluster);

		spm_set_cpu_power_off(cluster, cpu);
	}
}

303
304
305
306
static int plat_mtk_power_domain_on(unsigned long mpidr)
{
	int cpu = MPIDR_AFFLVL0_VAL(mpidr);
	int cluster = MPIDR_AFFLVL1_VAL(mpidr);
307
308
	int clst_pwr = spm_get_cluster_powerstate(cluster);
	unsigned int i;
309

310
311
	mcdi_ctrl_before_hotplug_on(cluster, cpu);
	hotplug_ctrl_cluster_on(cluster, cpu);
312

313
314
315
316
317
318
319
	if (clst_pwr == 0) {
		/* init cpu reset arch as AARCH64 of cluster */
		for (i = 0; i < PLATFORM_MAX_CPUS_PER_CLUSTER; i++) {
			mcucfg_init_archstate(cluster, i, 1);
			mcucfg_set_bootaddr(cluster, i, secure_entrypoint);
		}
	}
320

321
	hotplug_ctrl_cpu_on(cluster, cpu);
322
323
324
325
326
327
328
329
330

	return PSCI_E_SUCCESS;
}

static void plat_mtk_power_domain_off(const psci_power_state_t *state)
{
	uint64_t mpidr = read_mpidr();
	int cpu = MPIDR_AFFLVL0_VAL(mpidr);
	int cluster = MPIDR_AFFLVL1_VAL(mpidr);
331
332
333
334
	const plat_local_state_t *pds = state->pwr_domain_state;
	bool afflvl1 = (pds[MPIDR_AFFLVL1] == MTK_LOCAL_STATE_OFF);
	bool cluster_off = (HP_CLUSTER_OFF && afflvl1 &&
					clst_single_on(cluster, cpu));
335

336
	plat_cpu_pwrdwn_common();
337

338
339
	if (cluster_off)
		plat_cluster_pwrdwn_common(mpidr, cluster);
340

341
342
343
	mcdi_ctrl_before_hotplug_off(cluster, cpu, cluster_off);
	hotplug_ctrl_cluster_cpu_off(cluster, cpu, cluster_off);
}
344

345
346
347
348
349
350
351
352
353
354
static void plat_mtk_power_domain_on_finish(const psci_power_state_t *state)
{
	uint64_t mpidr = read_mpidr();
	int cpu = MPIDR_AFFLVL0_VAL(mpidr);
	int cluster = MPIDR_AFFLVL1_VAL(mpidr);
	const plat_local_state_t *pds = state->pwr_domain_state;
	bool afflvl1 = (pds[MPIDR_AFFLVL1] == MTK_LOCAL_STATE_OFF);

	if (afflvl1)
		plat_cluster_pwron_common(mpidr, cluster);
355

356
	plat_cpu_pwron_common();
357
358

	hotplug_ctrl_cpu_on_finish(cluster, cpu);
359
360
}

361
static void plat_mtk_power_domain_suspend(const psci_power_state_t *state)
362
363
364
365
{
	uint64_t mpidr = read_mpidr();
	int cpu = MPIDR_AFFLVL0_VAL(mpidr);
	int cluster = MPIDR_AFFLVL1_VAL(mpidr);
366
367
368
369
	const plat_local_state_t *pds = state->pwr_domain_state;
	bool afflvl1 = (pds[MPIDR_AFFLVL1] == MTK_LOCAL_STATE_OFF);
	bool afflvl2 = (pds[MPIDR_AFFLVL2] == MTK_LOCAL_STATE_OFF);
	bool cluster_off = MCDI_C2 && afflvl1 && clst_single_pwr(cluster, cpu);
370

371
372
	plat_cpu_pwrdwn_common();

373
	plat_dcm_mcsi_a_backup();
374

375
376
	if (cluster_off || afflvl2)
		plat_cluster_pwrdwn_common(mpidr, cluster);
377

378
379
380
381
	if (afflvl2) {
		spm_data_t spm_d = { .cmd = SPM_SUSPEND };
		uint32_t *d = (uint32_t *)&spm_d;
		uint32_t l = sizeof(spm_d) / sizeof(uint32_t);
382

383
384
385
386
387
388
389
390
391
392
393
394
		mcdi_ctrl_suspend();

		spm_set_bootaddr(secure_entrypoint);

		if (MCDI_SSPM)
			sspm_ipi_send_non_blocking(IPI_ID_SUSPEND, d);

		spm_system_suspend();

		if (MCDI_SSPM)
			while (sspm_ipi_recv_non_blocking(IPI_ID_SUSPEND, d, l))
				;
395
396

		mt_gic_distif_save();
397
398
	} else {
		mcdi_ctrl_cluster_cpu_off(cluster, cpu, cluster_off);
399
	}
400
}
401

402
403
404
405
406
407
static void plat_mtk_power_domain_suspend_finish(const psci_power_state_t *state)
{
	uint64_t mpidr = read_mpidr();
	int cluster = MPIDR_AFFLVL1_VAL(mpidr);
	const plat_local_state_t *pds = state->pwr_domain_state;
	bool afflvl2 = (pds[MPIDR_AFFLVL2] == MTK_LOCAL_STATE_OFF);
408

409
410
411
412
413
414
	if (afflvl2) {
		spm_data_t spm_d = { .cmd = SPM_RESUME };
		uint32_t *d = (uint32_t *)&spm_d;
		uint32_t l = sizeof(spm_d) / sizeof(uint32_t);

		mt_gic_init();
415
416
417
		mt_gic_distif_restore();
		mt_gic_rdistif_restore();

418
419
420
421
422
423
424
425
426
427
428
429
		mmio_write_32(EMI_WFIFO, 0xf);

		if (MCDI_SSPM)
			sspm_ipi_send_non_blocking(IPI_ID_SUSPEND, d);

		spm_system_suspend_finish();

		if (MCDI_SSPM)
			while (sspm_ipi_recv_non_blocking(IPI_ID_SUSPEND, d, l))
				;

		mcdi_ctrl_resume();
430
431
	} else {
		plat_cpu_pwron_common();
432
433
434
435
436
	}

	plat_cluster_pwron_common(mpidr, cluster);

	plat_dcm_mcsi_a_restore();
437
438
}

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#if PSCI_EXTENDED_STATE_ID

static int plat_mtk_validate_power_state(unsigned int power_state,
				psci_power_state_t *req_state)
{
	unsigned int state_id;
	int i;

	assert(req_state);

	if (!MCDI_SSPM)
		return PSCI_E_INVALID_PARAMS;

	/*
	 *  Currently we are using a linear search for finding the matching
	 *  entry in the idle power state array. This can be made a binary
	 *  search if the number of entries justify the additional complexity.
	 */
	for (i = 0; !!mtk_pm_idle_states[i]; i++) {
		if (power_state == mtk_pm_idle_states[i])
			break;
	}

	/* Return error if entry not found in the idle state array */
	if (!mtk_pm_idle_states[i])
		return PSCI_E_INVALID_PARAMS;

	i = 0;
	state_id = psci_get_pstate_id(power_state);

	/* Parse the State ID and populate the state info parameter */
	while (state_id) {
		req_state->pwr_domain_state[i++] = state_id &
						MTK_LOCAL_PSTATE_MASK;
		state_id >>= MTK_LOCAL_PSTATE_WIDTH;
	}

	return PSCI_E_SUCCESS;
}

#else /* if !PSCI_EXTENDED_STATE_ID */

static int plat_mtk_validate_power_state(unsigned int power_state,
					psci_power_state_t *req_state)
{
	int pstate = psci_get_pstate_type(power_state);
	int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
	int i;

	assert(req_state);

	if (pwr_lvl > PLAT_MAX_PWR_LVL)
		return PSCI_E_INVALID_PARAMS;

	/* Sanity check the requested state */
	if (pstate == PSTATE_TYPE_STANDBY) {
		/*
		 * It's possible to enter standby only on power level 0
		 * Ignore any other power level.
		 */
		if (pwr_lvl != 0)
			return PSCI_E_INVALID_PARAMS;

		req_state->pwr_domain_state[MTK_PWR_LVL0] = MTK_LOCAL_STATE_RET;
	} else if (!MCDI_SSPM) {
		return PSCI_E_INVALID_PARAMS;
	} else {
		for (i = 0; i <= pwr_lvl; i++)
			req_state->pwr_domain_state[i] = MTK_LOCAL_STATE_OFF;
	}

	return PSCI_E_SUCCESS;
}

#endif /* PSCI_EXTENDED_STATE_ID */

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*******************************************************************************
 * MTK handlers to shutdown/reboot the system
 ******************************************************************************/
static void __dead2 plat_mtk_system_off(void)
{
	INFO("MTK System Off\n");

	rtc_power_off_sequence();
	wk_pmic_enable_sdn_delay();
	pmic_power_off();

	wfi();
	ERROR("MTK System Off: operation not handled.\n");
	panic();
}

531
532
533
534
535
536
537
538
539
540
541
542
543
static void __dead2 plat_mtk_system_reset(void)
{
	struct bl_aux_gpio_info *gpio_reset = plat_get_mtk_gpio_reset();

	INFO("MTK System Reset\n");

	mt_set_gpio_out(gpio_reset->index, gpio_reset->polarity);

	wfi();
	ERROR("MTK System Reset: operation not handled.\n");
	panic();
}

544
545
546
547
548
549
550
551
static void plat_mtk_get_sys_suspend_power_state(psci_power_state_t *req_state)
{
	assert(PLAT_MAX_PWR_LVL >= 2);

	for (int i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
		req_state->pwr_domain_state[i] = MTK_LOCAL_STATE_OFF;
}

552
553
554
555
556
/*******************************************************************************
 * MTK_platform handler called when an affinity instance is about to be turned
 * on. The level and mpidr determine the affinity instance.
 ******************************************************************************/
static const plat_psci_ops_t plat_plat_pm_ops = {
557
	.cpu_standby			= plat_cpu_standby,
558
559
560
	.pwr_domain_on			= plat_mtk_power_domain_on,
	.pwr_domain_on_finish		= plat_mtk_power_domain_on_finish,
	.pwr_domain_off			= plat_mtk_power_domain_off,
561
562
	.pwr_domain_suspend		= plat_mtk_power_domain_suspend,
	.pwr_domain_suspend_finish	= plat_mtk_power_domain_suspend_finish,
563
	.system_off			= plat_mtk_system_off,
564
	.system_reset			= plat_mtk_system_reset,
565
	.validate_power_state		= plat_mtk_validate_power_state,
566
	.get_sys_suspend_power_state	= plat_mtk_get_sys_suspend_power_state
567
568
569
570
571
};

int plat_setup_psci_ops(uintptr_t sec_entrypoint,
			const plat_psci_ops_t **psci_ops)
{
572
573
	unsigned int i;

574
575
	*psci_ops = &plat_plat_pm_ops;
	secure_entrypoint = sec_entrypoint;
576

577
578
579
580
581
582
	/* Init cpu reset arch as AARCH64 of cluster 0 */
	for (i = 0; i < PLATFORM_MAX_CPUS_PER_CLUSTER; i++) {
		mcucfg_init_archstate(0, i, 1);
		mcucfg_set_bootaddr(0, i, secure_entrypoint);
	}

583
584
585
586
587
	if (!check_mcdi_ctl_stat()) {
		HP_SSPM_CTRL = false;
		MCDI_SSPM = false;
	}

588
589
	return 0;
}