gxl_pm.c 4.83 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
9
10
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch_helpers.h>
#include <assert.h>
#include <common/debug.h>
#include <drivers/arm/gicv2.h>
Carlo Caione's avatar
Carlo Caione committed
11
12
#include <drivers/console.h>
#include <errno.h>
13
#include <lib/mmio.h>
Carlo Caione's avatar
Carlo Caione committed
14
#include <lib/psci/psci.h>
15
16
17
#include <plat/common/platform.h>
#include <platform_def.h>

18
#include "aml_private.h"
19
20
21
22
23
24
25
26

#define SCPI_POWER_ON		0
#define SCPI_POWER_RETENTION	1
#define SCPI_POWER_OFF		3

#define SCPI_SYSTEM_SHUTDOWN	0
#define SCPI_SYSTEM_REBOOT	1

27
28
static uintptr_t gxl_sec_entrypoint;
static volatile uint32_t gxl_cpu0_go;
29

30
static void gxl_pm_set_reset_addr(u_register_t mpidr, uint64_t value)
31
{
32
	unsigned int core = plat_calc_core_pos(mpidr);
33
	uintptr_t cpu_mailbox_addr = AML_PSCI_MAILBOX_BASE + (core << 4);
34
35

	mmio_write_64(cpu_mailbox_addr, value);
36
37
38
39
}

static void gxl_pm_reset(u_register_t mpidr)
{
40
	unsigned int core = plat_calc_core_pos(mpidr);
41
	uintptr_t cpu_mailbox_addr = AML_PSCI_MAILBOX_BASE + (core << 4) + 8;
42
43

	mmio_write_32(cpu_mailbox_addr, 0);
44
45
}

46
static void __dead2 gxl_system_reset(void)
47
48
49
{
	INFO("BL31: PSCI_SYSTEM_RESET\n");

50
	u_register_t mpidr = read_mpidr_el1();
51
	uint32_t status = mmio_read_32(AML_AO_RTI_STATUS_REG3);
52
	int ret;
53
54
55
56
57
58
59

	NOTICE("BL31: Reboot reason: 0x%x\n", status);

	status &= 0xFFFF0FF0;

	console_flush();

60
	mmio_write_32(AML_AO_RTI_STATUS_REG3, status);
61

62
	ret = aml_scpi_sys_power_state(SCPI_SYSTEM_REBOOT);
63
64

	if (ret != 0) {
65
		ERROR("BL31: PSCI_SYSTEM_RESET: SCP error: %i\n", ret);
66
67
68
		panic();
	}

69
70
	gxl_pm_reset(mpidr);

71
72
73
74
75
76
	wfi();

	ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
	panic();
}

77
static void __dead2 gxl_system_off(void)
78
79
80
{
	INFO("BL31: PSCI_SYSTEM_OFF\n");

81
82
83
	u_register_t mpidr = read_mpidr_el1();
	int ret;

84
	ret = aml_scpi_sys_power_state(SCPI_SYSTEM_SHUTDOWN);
85
86

	if (ret != 0) {
87
		ERROR("BL31: PSCI_SYSTEM_OFF: SCP error %i\n", ret);
88
89
90
		panic();
	}

91
92
	gxl_pm_set_reset_addr(mpidr, 0);
	gxl_pm_reset(mpidr);
93
94
95
96
97
98
99

	wfi();

	ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
	panic();
}

100
static int32_t gxl_pwr_domain_on(u_register_t mpidr)
101
{
102
	unsigned int core = plat_calc_core_pos(mpidr);
103
104

	/* CPU0 can't be turned OFF, emulate it with a WFE loop */
105
	if (core == AML_PRIMARY_CPU) {
106
107
		VERBOSE("BL31: Releasing CPU0 from wait loop...\n");

108
109
110
		gxl_cpu0_go = 1;
		flush_dcache_range((uintptr_t)&gxl_cpu0_go,
				sizeof(gxl_cpu0_go));
111
112
113
114
115
116
117
118
		dsb();
		isb();

		sev();

		return PSCI_E_SUCCESS;
	}

119
	gxl_pm_set_reset_addr(mpidr, gxl_sec_entrypoint);
120
121
	aml_scpi_set_css_power_state(mpidr,
				     SCPI_POWER_ON, SCPI_POWER_ON, SCPI_POWER_ON);
122
123
124
125
126
127
	dmbsy();
	sev();

	return PSCI_E_SUCCESS;
}

128
static void gxl_pwr_domain_on_finish(const psci_power_state_t *target_state)
129
{
130
	unsigned int core = plat_calc_core_pos(read_mpidr_el1());
131
132
133
134

	assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
					PLAT_LOCAL_STATE_OFF);

135
	if (core == AML_PRIMARY_CPU) {
136
137
138
		gxl_cpu0_go = 0;
		flush_dcache_range((uintptr_t)&gxl_cpu0_go,
				sizeof(gxl_cpu0_go));
139
140
141
142
143
144
145
146
		dsb();
		isb();
	}

	gicv2_pcpu_distif_init();
	gicv2_cpuif_enable();
}

147
static void gxl_pwr_domain_off(const psci_power_state_t *target_state)
148
149
{
	u_register_t mpidr = read_mpidr_el1();
150
	unsigned int core = plat_calc_core_pos(mpidr);
151
152
153
154

	gicv2_cpuif_disable();

	/* CPU0 can't be turned OFF, emulate it with a WFE loop */
155
	if (core == AML_PRIMARY_CPU)
156
157
		return;

158
159
	aml_scpi_set_css_power_state(mpidr,
				     SCPI_POWER_OFF, SCPI_POWER_ON, SCPI_POWER_ON);
160
161
}

162
static void __dead2 gxl_pwr_domain_pwr_down_wfi(const psci_power_state_t
163
164
						 *target_state)
{
Remi Pommarel's avatar
Remi Pommarel committed
165
	u_register_t mpidr = read_mpidr_el1();
166
	unsigned int core = plat_calc_core_pos(mpidr);
167
168

	/* CPU0 can't be turned OFF, emulate it with a WFE loop */
169
	if (core == AML_PRIMARY_CPU) {
170
171
		VERBOSE("BL31: CPU0 entering wait loop...\n");

172
		while (gxl_cpu0_go == 0)
173
174
175
176
			wfe();

		VERBOSE("BL31: CPU0 resumed.\n");

Remi Pommarel's avatar
Remi Pommarel committed
177
178
179
180
181
182
183
		/*
		 * Because setting CPU0's warm reset entrypoint through PSCI
		 * mailbox and/or mmio mapped RVBAR (0xda834650) does not seem
		 * to work, jump to it manually.
		 * In order to avoid an assert, mmu has to be disabled.
		 */
		disable_mmu_el3();
184
		((void(*)(void))gxl_sec_entrypoint)();
185
186
187
	}

	dsbsy();
Remi Pommarel's avatar
Remi Pommarel committed
188
189
	gxl_pm_set_reset_addr(mpidr, 0);
	gxl_pm_reset(mpidr);
190
191
192
193
194
195
196
197

	for (;;)
		wfi();
}

/*******************************************************************************
 * Platform handlers and setup function.
 ******************************************************************************/
198
199
200
201
202
203
204
static const plat_psci_ops_t gxl_ops = {
	.pwr_domain_on			= gxl_pwr_domain_on,
	.pwr_domain_on_finish		= gxl_pwr_domain_on_finish,
	.pwr_domain_off			= gxl_pwr_domain_off,
	.pwr_domain_pwr_down_wfi	= gxl_pwr_domain_pwr_down_wfi,
	.system_off			= gxl_system_off,
	.system_reset			= gxl_system_reset,
205
206
207
208
209
};

int plat_setup_psci_ops(uintptr_t sec_entrypoint,
			const plat_psci_ops_t **psci_ops)
{
210
211
212
	gxl_sec_entrypoint = sec_entrypoint;
	*psci_ops = &gxl_ops;
	gxl_cpu0_go = 0;
213
214
	return 0;
}