| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <stdbool.h> |
| 9 |
|
| 10 |
#include <arch_helpers.h> |
| 11 |
#include <common/debug.h> |
| 12 |
#include <lib/el3_runtime/cpu_data.h> |
| 13 |
#include <lib/psci/psci.h> |
| 14 |
#include <plat/common/platform.h> |
| 15 |
|
| 16 |
#include <ti_sci_protocol.h> |
| 17 |
#include <k3_gicv3.h> |
| 18 |
#include <ti_sci.h> |
| 19 |
|
| 20 |
#define CORE_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL0]) |
| 21 |
#define CLUSTER_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL1]) |
| 22 |
#define SYSTEM_PWR_STATE(state) ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL]) |
| 23 |
|
| 24 |
uintptr_t k3_sec_entrypoint; |
| 25 |
|
| 26 |
static void k3_cpu_standby(plat_local_state_t cpu_state) |
| 27 |
{ |
| 28 |
u_register_t scr; |
| 29 |
|
| 30 |
scr = read_scr_el3(); |
| 31 |
|
| 32 |
write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT); |
| 33 |
isb(); |
| 34 |
|
| 35 |
dsb(); |
| 36 |
|
| 37 |
wfi(); |
| 38 |
|
| 39 |
write_scr_el3(scr); |
| 40 |
} |
| 41 |
|
| 42 |
static int k3_pwr_domain_on(u_register_t mpidr) |
| 43 |
{ |
| 44 |
int core, proc_id, device_id, ret; |
| 45 |
|
| 46 |
core = plat_core_pos_by_mpidr(mpidr); |
| 47 |
if (core < 0) { |
| 48 |
ERROR("Could not get target core id: %d\n", core); |
| 49 |
return PSCI_E_INTERN_FAIL; |
| 50 |
} |
| 51 |
|
| 52 |
proc_id = PLAT_PROC_START_ID + core; |
| 53 |
device_id = PLAT_PROC_DEVICE_START_ID + core; |
| 54 |
|
| 55 |
ret = ti_sci_proc_request(proc_id); |
| 56 |
if (ret) { |
| 57 |
ERROR("Request for processor failed: %d\n", ret); |
| 58 |
return PSCI_E_INTERN_FAIL; |
| 59 |
} |
| 60 |
|
| 61 |
ret = ti_sci_proc_set_boot_cfg(proc_id, k3_sec_entrypoint, 0, 0); |
| 62 |
if (ret) { |
| 63 |
ERROR("Request to set core boot address failed: %d\n", ret); |
| 64 |
return PSCI_E_INTERN_FAIL; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
ret = ti_sci_proc_set_boot_ctrl(proc_id, |
| 69 |
0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ | |
| 70 |
PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS | |
| 71 |
PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM); |
| 72 |
if (ret) { |
| 73 |
ERROR("Request to clear boot configuration failed: %d\n", ret); |
| 74 |
return PSCI_E_INTERN_FAIL; |
| 75 |
} |
| 76 |
|
| 77 |
ret = ti_sci_device_get(device_id); |
| 78 |
if (ret) { |
| 79 |
ERROR("Request to start core failed: %d\n", ret); |
| 80 |
return PSCI_E_INTERN_FAIL; |
| 81 |
} |
| 82 |
|
| 83 |
return PSCI_E_SUCCESS; |
| 84 |
} |
| 85 |
|
| 86 |
void k3_pwr_domain_off(const psci_power_state_t *target_state) |
| 87 |
{ |
| 88 |
int core, cluster, proc_id, device_id, cluster_id, ret; |
| 89 |
|
| 90 |
|
| 91 |
assert(CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE); |
| 92 |
|
| 93 |
|
| 94 |
k3_gic_cpuif_disable(); |
| 95 |
|
| 96 |
core = plat_my_core_pos(); |
| 97 |
cluster = MPIDR_AFFLVL1_VAL(read_mpidr_el1()); |
| 98 |
proc_id = PLAT_PROC_START_ID + core; |
| 99 |
device_id = PLAT_PROC_DEVICE_START_ID + core; |
| 100 |
cluster_id = PLAT_CLUSTER_DEVICE_START_ID + (cluster * 2); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) { |
| 108 |
ret = ti_sci_device_get(cluster_id); |
| 109 |
if (ret) { |
| 110 |
ERROR("Request to get cluster failed: %d\n", ret); |
| 111 |
return; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
ret = ti_sci_proc_wait_boot_status_no_wait(proc_id, |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
UINT8_MAX, 100, UINT8_MAX, UINT8_MAX, |
| 122 |
|
| 123 |
PROC_BOOT_STATUS_FLAG_ARMV8_WFI, 0, 0, 0); |
| 124 |
if (ret) { |
| 125 |
ERROR("Sending wait for WFI failed (%d)\n", ret); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
ret = ti_sci_device_put_no_wait(device_id); |
| 131 |
if (ret) { |
| 132 |
ERROR("Sending core shutdown message failed (%d)\n", ret); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if (CLUSTER_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE) |
| 138 |
return; |
| 139 |
|
| 140 |
|
| 141 |
ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, |
| 142 |
PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS, 0); |
| 143 |
if (ret) { |
| 144 |
ERROR("Sending set control message failed (%d)\n", ret); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, |
| 150 |
PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ, 0); |
| 151 |
if (ret) { |
| 152 |
ERROR("Sending set control message failed (%d)\n", ret); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
ret = ti_sci_proc_wait_boot_status_no_wait(proc_id, |
| 158 |
UINT8_MAX, 2, UINT8_MAX, UINT8_MAX, |
| 159 |
PROC_BOOT_STATUS_FLAG_ARMV8_L2F_DONE, 0, 0, 0); |
| 160 |
if (ret) { |
| 161 |
ERROR("Sending wait message failed (%d)\n", ret); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, |
| 167 |
0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ); |
| 168 |
if (ret) { |
| 169 |
ERROR("Sending set control message failed (%d)\n", ret); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, |
| 175 |
PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM, 0); |
| 176 |
if (ret) { |
| 177 |
ERROR("Sending set control message failed (%d)\n", ret); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
ret = ti_sci_proc_wait_boot_status_no_wait(proc_id, |
| 183 |
UINT8_MAX, 2, UINT8_MAX, UINT8_MAX, |
| 184 |
PROC_BOOT_STATUS_FLAG_ARMV8_STANDBYWFIL2, 0, 0, 0); |
| 185 |
if (ret) { |
| 186 |
ERROR("Sending wait message failed (%d)\n", ret); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
ret = ti_sci_device_put_no_wait(cluster_id); |
| 192 |
if (ret) { |
| 193 |
ERROR("Sending cluster shutdown message failed (%d)\n", ret); |
| 194 |
return; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void k3_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 199 |
{ |
| 200 |
|
| 201 |
|
| 202 |
k3_gic_pcpu_init(); |
| 203 |
k3_gic_cpuif_enable(); |
| 204 |
} |
| 205 |
|
| 206 |
static void __dead2 k3_system_off(void) |
| 207 |
{ |
| 208 |
int ret; |
| 209 |
|
| 210 |
|
| 211 |
ret = ti_sci_device_put_no_wait(PLAT_BOARD_DEVICE_ID); |
| 212 |
if (ret != 0) { |
| 213 |
ERROR("Sending system shutdown message failed (%d)\n", ret); |
| 214 |
} |
| 215 |
|
| 216 |
while (true) |
| 217 |
wfi(); |
| 218 |
} |
| 219 |
|
| 220 |
static void __dead2 k3_system_reset(void) |
| 221 |
{ |
| 222 |
|
| 223 |
ti_sci_core_reboot(); |
| 224 |
|
| 225 |
while (true) |
| 226 |
wfi(); |
| 227 |
} |
| 228 |
|
| 229 |
static int k3_validate_power_state(unsigned int power_state, |
| 230 |
psci_power_state_t *req_state) |
| 231 |
{ |
| 232 |
|
| 233 |
|
| 234 |
return PSCI_E_SUCCESS; |
| 235 |
} |
| 236 |
|
| 237 |
static void k3_pwr_domain_suspend_to_mode(const psci_power_state_t *target_state, uint8_t mode) |
| 238 |
{ |
| 239 |
unsigned int core, proc_id; |
| 240 |
|
| 241 |
core = plat_my_core_pos(); |
| 242 |
proc_id = PLAT_PROC_START_ID + core; |
| 243 |
|
| 244 |
|
| 245 |
k3_gic_cpuif_disable(); |
| 246 |
k3_gic_save_context(); |
| 247 |
|
| 248 |
k3_pwr_domain_off(target_state); |
| 249 |
|
| 250 |
ti_sci_enter_sleep(proc_id, mode, k3_sec_entrypoint); |
| 251 |
} |
| 252 |
|
| 253 |
static void k3_pwr_domain_suspend_dm_managed(const psci_power_state_t *target_state) |
| 254 |
{ |
| 255 |
uint8_t mode = MSG_VALUE_SLEEP_MODE_DEEP_SLEEP; |
| 256 |
int ret; |
| 257 |
|
| 258 |
ret = ti_sci_lpm_get_next_sys_mode(&mode); |
| 259 |
if (ret != 0) { |
| 260 |
ERROR("Failed to fetch next system mode\n"); |
| 261 |
} |
| 262 |
|
| 263 |
k3_pwr_domain_suspend_to_mode(target_state, mode); |
| 264 |
} |
| 265 |
|
| 266 |
static void k3_pwr_domain_suspend(const psci_power_state_t *target_state) |
| 267 |
{ |
| 268 |
k3_pwr_domain_suspend_to_mode(target_state, MSG_VALUE_SLEEP_MODE_DEEP_SLEEP); |
| 269 |
} |
| 270 |
|
| 271 |
static void k3_pwr_domain_suspend_finish(const psci_power_state_t *target_state) |
| 272 |
{ |
| 273 |
k3_gic_restore_context(); |
| 274 |
k3_gic_cpuif_enable(); |
| 275 |
} |
| 276 |
|
| 277 |
static void k3_get_sys_suspend_power_state(psci_power_state_t *req_state) |
| 278 |
{ |
| 279 |
unsigned int i; |
| 280 |
|
| 281 |
|
| 282 |
for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) { |
| 283 |
req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
static plat_psci_ops_t k3_plat_psci_ops = { |
| 288 |
.cpu_standby = k3_cpu_standby, |
| 289 |
.pwr_domain_on = k3_pwr_domain_on, |
| 290 |
.pwr_domain_off = k3_pwr_domain_off, |
| 291 |
.pwr_domain_on_finish = k3_pwr_domain_on_finish, |
| 292 |
.pwr_domain_suspend = k3_pwr_domain_suspend, |
| 293 |
.pwr_domain_suspend_finish = k3_pwr_domain_suspend_finish, |
| 294 |
.get_sys_suspend_power_state = k3_get_sys_suspend_power_state, |
| 295 |
.system_off = k3_system_off, |
| 296 |
.system_reset = k3_system_reset, |
| 297 |
.validate_power_state = k3_validate_power_state, |
| 298 |
}; |
| 299 |
|
| 300 |
int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 301 |
const plat_psci_ops_t **psci_ops) |
| 302 |
{ |
| 303 |
uint64_t fw_caps = 0; |
| 304 |
int ret; |
| 305 |
|
| 306 |
k3_sec_entrypoint = sec_entrypoint; |
| 307 |
|
| 308 |
ret = ti_sci_query_fw_caps(&fw_caps); |
| 309 |
if (ret) { |
| 310 |
ERROR("Unable to query firmware capabilities (%d)\n", ret); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
if (!(fw_caps & (MSG_FLAG_CAPS_LPM_DEEP_SLEEP | |
| 315 |
MSG_FLAG_CAPS_LPM_MCU_ONLY | |
| 316 |
MSG_FLAG_CAPS_LPM_STANDBY | |
| 317 |
MSG_FLAG_CAPS_LPM_PARTIAL_IO))) { |
| 318 |
|
| 319 |
k3_plat_psci_ops.pwr_domain_suspend = NULL; |
| 320 |
k3_plat_psci_ops.pwr_domain_suspend_finish = NULL; |
| 321 |
k3_plat_psci_ops.get_sys_suspend_power_state = NULL; |
| 322 |
} else if (fw_caps & MSG_FLAG_CAPS_LPM_DM_MANAGED) { |
| 323 |
k3_plat_psci_ops.pwr_domain_suspend = k3_pwr_domain_suspend_dm_managed; |
| 324 |
} |
| 325 |
|
| 326 |
*psci_ops = &k3_plat_psci_ops; |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |