blob: d9b69ef8235b03e16a0d235efb35fea52afa2831 [file] [log] [blame]
Antonio Nino Diaz7298c1f2018-12-05 00:09:30 +00001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <drivers/console.h>
10#include <common/debug.h>
11#include <errno.h>
12#include <drivers/arm/gicv2.h>
13#include <lib/mmio.h>
14#include <plat/common/platform.h>
15#include <platform_def.h>
16#include <lib/psci/psci.h>
17
18#include "gxl_private.h"
19
20#define SCPI_POWER_ON 0
21#define SCPI_POWER_RETENTION 1
22#define SCPI_POWER_OFF 3
23
24#define SCPI_SYSTEM_SHUTDOWN 0
25#define SCPI_SYSTEM_REBOOT 1
26
27static uintptr_t gxbb_sec_entrypoint;
28static volatile uint32_t gxbb_cpu0_go;
29
30static void gxbb_program_mailbox(u_register_t mpidr, uint64_t value)
31{
32 unsigned int core = plat_gxbb_calc_core_pos(mpidr);
33 uintptr_t cpu_mailbox_addr = GXBB_PSCI_MAILBOX_BASE + (core << 4);
34
35 mmio_write_64(cpu_mailbox_addr, value);
36 flush_dcache_range(cpu_mailbox_addr, sizeof(uint64_t));
37}
38
39static void __dead2 gxbb_system_reset(void)
40{
41 INFO("BL31: PSCI_SYSTEM_RESET\n");
42
43 uint32_t status = mmio_read_32(GXBB_AO_RTI_STATUS_REG3);
44
45 NOTICE("BL31: Reboot reason: 0x%x\n", status);
46
47 status &= 0xFFFF0FF0;
48
49 console_flush();
50
51 mmio_write_32(GXBB_AO_RTI_STATUS_REG3, status);
52
53 int ret = scpi_sys_power_state(SCPI_SYSTEM_REBOOT);
54
55 if (ret != 0) {
56 ERROR("BL31: PSCI_SYSTEM_RESET: SCP error: %u\n", ret);
57 panic();
58 }
59
60 wfi();
61
62 ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
63 panic();
64}
65
66static void __dead2 gxbb_system_off(void)
67{
68 INFO("BL31: PSCI_SYSTEM_OFF\n");
69
70 unsigned int ret = scpi_sys_power_state(SCPI_SYSTEM_SHUTDOWN);
71
72 if (ret != 0) {
73 ERROR("BL31: PSCI_SYSTEM_OFF: SCP error %u\n", ret);
74 panic();
75 }
76
77 gxbb_program_mailbox(read_mpidr_el1(), 0);
78
79 wfi();
80
81 ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
82 panic();
83}
84
85static int32_t gxbb_pwr_domain_on(u_register_t mpidr)
86{
87 unsigned int core = plat_gxbb_calc_core_pos(mpidr);
88
89 /* CPU0 can't be turned OFF, emulate it with a WFE loop */
90 if (core == GXBB_PRIMARY_CPU) {
91 VERBOSE("BL31: Releasing CPU0 from wait loop...\n");
92
93 gxbb_cpu0_go = 1;
94 flush_dcache_range((uintptr_t)&gxbb_cpu0_go,
95 sizeof(gxbb_cpu0_go));
96 dsb();
97 isb();
98
99 sev();
100
101 return PSCI_E_SUCCESS;
102 }
103
104 gxbb_program_mailbox(mpidr, gxbb_sec_entrypoint);
105 scpi_set_css_power_state(mpidr,
106 SCPI_POWER_ON, SCPI_POWER_ON, SCPI_POWER_ON);
107 dmbsy();
108 sev();
109
110 return PSCI_E_SUCCESS;
111}
112
113static void gxbb_pwr_domain_on_finish(const psci_power_state_t *target_state)
114{
115 unsigned int core = plat_gxbb_calc_core_pos(read_mpidr_el1());
116
117 assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
118 PLAT_LOCAL_STATE_OFF);
119
120 if (core == GXBB_PRIMARY_CPU) {
121 gxbb_cpu0_go = 0;
122 flush_dcache_range((uintptr_t)&gxbb_cpu0_go,
123 sizeof(gxbb_cpu0_go));
124 dsb();
125 isb();
126 }
127
128 gicv2_pcpu_distif_init();
129 gicv2_cpuif_enable();
130}
131
132static void gxbb_pwr_domain_off(const psci_power_state_t *target_state)
133{
134 u_register_t mpidr = read_mpidr_el1();
135 unsigned int core = plat_gxbb_calc_core_pos(mpidr);
136 uintptr_t addr = GXBB_PSCI_MAILBOX_BASE + 8 + (core << 4);
137
138 mmio_write_32(addr, 0xFFFFFFFF);
139 flush_dcache_range(addr, sizeof(uint32_t));
140
141 gicv2_cpuif_disable();
142
143 /* CPU0 can't be turned OFF, emulate it with a WFE loop */
144 if (core == GXBB_PRIMARY_CPU)
145 return;
146
147 scpi_set_css_power_state(mpidr,
148 SCPI_POWER_OFF, SCPI_POWER_ON, SCPI_POWER_ON);
149}
150
151static void __dead2 gxbb_pwr_domain_pwr_down_wfi(const psci_power_state_t
152 *target_state)
153{
154 unsigned int core = plat_gxbb_calc_core_pos(read_mpidr_el1());
155
156 /* CPU0 can't be turned OFF, emulate it with a WFE loop */
157 if (core == GXBB_PRIMARY_CPU) {
158 VERBOSE("BL31: CPU0 entering wait loop...\n");
159
160 while (gxbb_cpu0_go == 0)
161 wfe();
162
163 VERBOSE("BL31: CPU0 resumed.\n");
164
165 write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
166 }
167
168 dsbsy();
169
170 for (;;)
171 wfi();
172}
173
174/*******************************************************************************
175 * Platform handlers and setup function.
176 ******************************************************************************/
177static const plat_psci_ops_t gxbb_ops = {
178 .pwr_domain_on = gxbb_pwr_domain_on,
179 .pwr_domain_on_finish = gxbb_pwr_domain_on_finish,
180 .pwr_domain_off = gxbb_pwr_domain_off,
181 .pwr_domain_pwr_down_wfi = gxbb_pwr_domain_pwr_down_wfi,
182 .system_off = gxbb_system_off,
183 .system_reset = gxbb_system_reset,
184};
185
186int plat_setup_psci_ops(uintptr_t sec_entrypoint,
187 const plat_psci_ops_t **psci_ops)
188{
189 gxbb_sec_entrypoint = sec_entrypoint;
190 *psci_ops = &gxbb_ops;
191 gxbb_cpu0_go = 0;
192 return 0;
193}