12e9c9e82SBenjamin Fair /* 2f1be00daSLouis Mayencourt * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved. 32e9c9e82SBenjamin Fair * 42e9c9e82SBenjamin Fair * SPDX-License-Identifier: BSD-3-Clause 52e9c9e82SBenjamin Fair */ 62e9c9e82SBenjamin Fair 72e9c9e82SBenjamin Fair #include <assert.h> 82e9c9e82SBenjamin Fair #include <stdbool.h> 92e9c9e82SBenjamin Fair 1009d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1109d40e0eSAntonio Nino Diaz #include <common/debug.h> 1209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 1309d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 1409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 1509d40e0eSAntonio Nino Diaz 16a9ae424eSAndrew F. Davis #include <ti_sci_protocol.h> 1709d40e0eSAntonio Nino Diaz #include <k3_gicv3.h> 18df83b034SAndrew F. Davis #include <ti_sci.h> 19df83b034SAndrew F. Davis 20586621f1SAndrew F. Davis #define CORE_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL0]) 21586621f1SAndrew F. Davis #define CLUSTER_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL1]) 22586621f1SAndrew F. Davis #define SYSTEM_PWR_STATE(state) ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL]) 23586621f1SAndrew F. Davis 242e9c9e82SBenjamin Fair uintptr_t k3_sec_entrypoint; 252e9c9e82SBenjamin Fair 262e9c9e82SBenjamin Fair static void k3_cpu_standby(plat_local_state_t cpu_state) 272e9c9e82SBenjamin Fair { 28f1be00daSLouis Mayencourt u_register_t scr; 29deed2b83SAndrew F. Davis 30deed2b83SAndrew F. Davis scr = read_scr_el3(); 31deed2b83SAndrew F. Davis /* Enable the Non secure interrupt to wake the CPU */ 32deed2b83SAndrew F. Davis write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT); 33deed2b83SAndrew F. Davis isb(); 34deed2b83SAndrew F. Davis /* dsb is good practice before using wfi to enter low power states */ 352e9c9e82SBenjamin Fair dsb(); 36deed2b83SAndrew F. Davis /* Enter standby state */ 372e9c9e82SBenjamin Fair wfi(); 38deed2b83SAndrew F. Davis /* Restore SCR */ 39deed2b83SAndrew F. Davis write_scr_el3(scr); 402e9c9e82SBenjamin Fair } 412e9c9e82SBenjamin Fair 422e9c9e82SBenjamin Fair static int k3_pwr_domain_on(u_register_t mpidr) 432e9c9e82SBenjamin Fair { 449f49a177SAndrew F. Davis int core, proc_id, device_id, ret; 452e9c9e82SBenjamin Fair 469f49a177SAndrew F. Davis core = plat_core_pos_by_mpidr(mpidr); 479f49a177SAndrew F. Davis if (core < 0) { 489f49a177SAndrew F. Davis ERROR("Could not get target core id: %d\n", core); 49df83b034SAndrew F. Davis return PSCI_E_INTERN_FAIL; 50df83b034SAndrew F. Davis } 51df83b034SAndrew F. Davis 529f49a177SAndrew F. Davis proc_id = PLAT_PROC_START_ID + core; 539f49a177SAndrew F. Davis device_id = PLAT_PROC_DEVICE_START_ID + core; 54df83b034SAndrew F. Davis 559f49a177SAndrew F. Davis ret = ti_sci_proc_request(proc_id); 56df83b034SAndrew F. Davis if (ret) { 57df83b034SAndrew F. Davis ERROR("Request for processor failed: %d\n", ret); 58df83b034SAndrew F. Davis return PSCI_E_INTERN_FAIL; 59df83b034SAndrew F. Davis } 60df83b034SAndrew F. Davis 619f49a177SAndrew F. Davis ret = ti_sci_proc_set_boot_cfg(proc_id, k3_sec_entrypoint, 0, 0); 62df83b034SAndrew F. Davis if (ret) { 63df83b034SAndrew F. Davis ERROR("Request to set core boot address failed: %d\n", ret); 64df83b034SAndrew F. Davis return PSCI_E_INTERN_FAIL; 65df83b034SAndrew F. Davis } 66df83b034SAndrew F. Davis 67586621f1SAndrew F. Davis /* sanity check these are off before starting a core */ 68586621f1SAndrew F. Davis ret = ti_sci_proc_set_boot_ctrl(proc_id, 69586621f1SAndrew F. Davis 0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ | 70586621f1SAndrew F. Davis PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS | 71586621f1SAndrew F. Davis PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM); 72586621f1SAndrew F. Davis if (ret) { 73586621f1SAndrew F. Davis ERROR("Request to clear boot configuration failed: %d\n", ret); 74586621f1SAndrew F. Davis return PSCI_E_INTERN_FAIL; 75586621f1SAndrew F. Davis } 76586621f1SAndrew F. Davis 779f49a177SAndrew F. Davis ret = ti_sci_device_get(device_id); 78df83b034SAndrew F. Davis if (ret) { 79df83b034SAndrew F. Davis ERROR("Request to start core failed: %d\n", ret); 80df83b034SAndrew F. Davis return PSCI_E_INTERN_FAIL; 81df83b034SAndrew F. Davis } 82df83b034SAndrew F. Davis 832e9c9e82SBenjamin Fair return PSCI_E_SUCCESS; 842e9c9e82SBenjamin Fair } 852e9c9e82SBenjamin Fair 862e9c9e82SBenjamin Fair void k3_pwr_domain_off(const psci_power_state_t *target_state) 872e9c9e82SBenjamin Fair { 88586621f1SAndrew F. Davis int core, cluster, proc_id, device_id, cluster_id, ret; 89586621f1SAndrew F. Davis 90586621f1SAndrew F. Davis /* At very least the local core should be powering down */ 91586621f1SAndrew F. Davis assert(CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE); 9234cae37fSAndrew F. Davis 932e9c9e82SBenjamin Fair /* Prevent interrupts from spuriously waking up this cpu */ 942e9c9e82SBenjamin Fair k3_gic_cpuif_disable(); 952e9c9e82SBenjamin Fair 969f49a177SAndrew F. Davis core = plat_my_core_pos(); 97586621f1SAndrew F. Davis cluster = MPIDR_AFFLVL1_VAL(read_mpidr_el1()); 989f49a177SAndrew F. Davis proc_id = PLAT_PROC_START_ID + core; 999f49a177SAndrew F. Davis device_id = PLAT_PROC_DEVICE_START_ID + core; 100586621f1SAndrew F. Davis cluster_id = PLAT_CLUSTER_DEVICE_START_ID + (cluster * 2); 101586621f1SAndrew F. Davis 102586621f1SAndrew F. Davis /* 103586621f1SAndrew F. Davis * If we are the last core in the cluster then we take a reference to 104586621f1SAndrew F. Davis * the cluster device so that it does not get shutdown before we 105586621f1SAndrew F. Davis * execute the entire cluster L2 cleaning sequence below. 106586621f1SAndrew F. Davis */ 107586621f1SAndrew F. Davis if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) { 108586621f1SAndrew F. Davis ret = ti_sci_device_get(cluster_id); 109586621f1SAndrew F. Davis if (ret) { 110586621f1SAndrew F. Davis ERROR("Request to get cluster failed: %d\n", ret); 111586621f1SAndrew F. Davis return; 112586621f1SAndrew F. Davis } 113586621f1SAndrew F. Davis } 11434cae37fSAndrew F. Davis 115a9ae424eSAndrew F. Davis /* Start by sending wait for WFI command */ 1169f49a177SAndrew F. Davis ret = ti_sci_proc_wait_boot_status_no_wait(proc_id, 117a9ae424eSAndrew F. Davis /* 118a9ae424eSAndrew F. Davis * Wait maximum time to give us the best chance to get 119a9ae424eSAndrew F. Davis * to WFI before this command timeouts 120a9ae424eSAndrew F. Davis */ 121a9ae424eSAndrew F. Davis UINT8_MAX, 100, UINT8_MAX, UINT8_MAX, 122a9ae424eSAndrew F. Davis /* Wait for WFI */ 123a9ae424eSAndrew F. Davis PROC_BOOT_STATUS_FLAG_ARMV8_WFI, 0, 0, 0); 12434cae37fSAndrew F. Davis if (ret) { 125a9ae424eSAndrew F. Davis ERROR("Sending wait for WFI failed (%d)\n", ret); 126a9ae424eSAndrew F. Davis return; 127a9ae424eSAndrew F. Davis } 128a9ae424eSAndrew F. Davis 129a9ae424eSAndrew F. Davis /* Now queue up the core shutdown request */ 1309f49a177SAndrew F. Davis ret = ti_sci_device_put_no_wait(device_id); 131a9ae424eSAndrew F. Davis if (ret) { 132a9ae424eSAndrew F. Davis ERROR("Sending core shutdown message failed (%d)\n", ret); 13334cae37fSAndrew F. Davis return; 13434cae37fSAndrew F. Davis } 135586621f1SAndrew F. Davis 136586621f1SAndrew F. Davis /* If our cluster is not going down we stop here */ 137586621f1SAndrew F. Davis if (CLUSTER_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE) 138586621f1SAndrew F. Davis return; 139586621f1SAndrew F. Davis 140586621f1SAndrew F. Davis /* set AINACTS */ 141586621f1SAndrew F. Davis ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, 142586621f1SAndrew F. Davis PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS, 0); 143586621f1SAndrew F. Davis if (ret) { 144586621f1SAndrew F. Davis ERROR("Sending set control message failed (%d)\n", ret); 145586621f1SAndrew F. Davis return; 146586621f1SAndrew F. Davis } 147586621f1SAndrew F. Davis 148586621f1SAndrew F. Davis /* set L2FLUSHREQ */ 149586621f1SAndrew F. Davis ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, 150586621f1SAndrew F. Davis PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ, 0); 151586621f1SAndrew F. Davis if (ret) { 152586621f1SAndrew F. Davis ERROR("Sending set control message failed (%d)\n", ret); 153586621f1SAndrew F. Davis return; 154586621f1SAndrew F. Davis } 155586621f1SAndrew F. Davis 156586621f1SAndrew F. Davis /* wait for L2FLUSHDONE*/ 157586621f1SAndrew F. Davis ret = ti_sci_proc_wait_boot_status_no_wait(proc_id, 158586621f1SAndrew F. Davis UINT8_MAX, 2, UINT8_MAX, UINT8_MAX, 159586621f1SAndrew F. Davis PROC_BOOT_STATUS_FLAG_ARMV8_L2F_DONE, 0, 0, 0); 160586621f1SAndrew F. Davis if (ret) { 161586621f1SAndrew F. Davis ERROR("Sending wait message failed (%d)\n", ret); 162586621f1SAndrew F. Davis return; 163586621f1SAndrew F. Davis } 164586621f1SAndrew F. Davis 165586621f1SAndrew F. Davis /* clear L2FLUSHREQ */ 166586621f1SAndrew F. Davis ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, 167586621f1SAndrew F. Davis 0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ); 168586621f1SAndrew F. Davis if (ret) { 169586621f1SAndrew F. Davis ERROR("Sending set control message failed (%d)\n", ret); 170586621f1SAndrew F. Davis return; 171586621f1SAndrew F. Davis } 172586621f1SAndrew F. Davis 173586621f1SAndrew F. Davis /* set ACINACTM */ 174586621f1SAndrew F. Davis ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id, 175586621f1SAndrew F. Davis PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM, 0); 176586621f1SAndrew F. Davis if (ret) { 177586621f1SAndrew F. Davis ERROR("Sending set control message failed (%d)\n", ret); 178586621f1SAndrew F. Davis return; 179586621f1SAndrew F. Davis } 180586621f1SAndrew F. Davis 181586621f1SAndrew F. Davis /* wait for STANDBYWFIL2 */ 182586621f1SAndrew F. Davis ret = ti_sci_proc_wait_boot_status_no_wait(proc_id, 183586621f1SAndrew F. Davis UINT8_MAX, 2, UINT8_MAX, UINT8_MAX, 184586621f1SAndrew F. Davis PROC_BOOT_STATUS_FLAG_ARMV8_STANDBYWFIL2, 0, 0, 0); 185586621f1SAndrew F. Davis if (ret) { 186586621f1SAndrew F. Davis ERROR("Sending wait message failed (%d)\n", ret); 187586621f1SAndrew F. Davis return; 188586621f1SAndrew F. Davis } 189586621f1SAndrew F. Davis 190586621f1SAndrew F. Davis /* Now queue up the cluster shutdown request */ 191586621f1SAndrew F. Davis ret = ti_sci_device_put_no_wait(cluster_id); 192586621f1SAndrew F. Davis if (ret) { 193586621f1SAndrew F. Davis ERROR("Sending cluster shutdown message failed (%d)\n", ret); 194586621f1SAndrew F. Davis return; 195586621f1SAndrew F. Davis } 1962e9c9e82SBenjamin Fair } 1972e9c9e82SBenjamin Fair 1982e9c9e82SBenjamin Fair void k3_pwr_domain_on_finish(const psci_power_state_t *target_state) 1992e9c9e82SBenjamin Fair { 2002e9c9e82SBenjamin Fair /* TODO: Indicate to System firmware about completion */ 2012e9c9e82SBenjamin Fair 2022e9c9e82SBenjamin Fair k3_gic_pcpu_init(); 2032e9c9e82SBenjamin Fair k3_gic_cpuif_enable(); 2042e9c9e82SBenjamin Fair } 2052e9c9e82SBenjamin Fair 20642d9b3aaSJan Kiszka static void __dead2 k3_system_off(void) 20742d9b3aaSJan Kiszka { 208*0bdef264SAndrew Davis int ret; 209*0bdef264SAndrew Davis 210*0bdef264SAndrew Davis /* Queue up the system shutdown request */ 211*0bdef264SAndrew Davis ret = ti_sci_device_put_no_wait(PLAT_BOARD_DEVICE_ID); 212*0bdef264SAndrew Davis if (ret != 0) { 213*0bdef264SAndrew Davis ERROR("Sending system shutdown message failed (%d)\n", ret); 214*0bdef264SAndrew Davis } 215*0bdef264SAndrew Davis 21642d9b3aaSJan Kiszka while (true) 21742d9b3aaSJan Kiszka wfi(); 21842d9b3aaSJan Kiszka } 21942d9b3aaSJan Kiszka 2202e9c9e82SBenjamin Fair static void __dead2 k3_system_reset(void) 2212e9c9e82SBenjamin Fair { 222c8761b4dSAndrew F. Davis /* Send the system reset request to system firmware */ 223c8761b4dSAndrew F. Davis ti_sci_core_reboot(); 2242e9c9e82SBenjamin Fair 2252e9c9e82SBenjamin Fair while (true) 2262e9c9e82SBenjamin Fair wfi(); 2272e9c9e82SBenjamin Fair } 2282e9c9e82SBenjamin Fair 2292e9c9e82SBenjamin Fair static int k3_validate_power_state(unsigned int power_state, 2302e9c9e82SBenjamin Fair psci_power_state_t *req_state) 2312e9c9e82SBenjamin Fair { 2322e9c9e82SBenjamin Fair /* TODO: perform the proper validation */ 2332e9c9e82SBenjamin Fair 2342e9c9e82SBenjamin Fair return PSCI_E_SUCCESS; 2352e9c9e82SBenjamin Fair } 2362e9c9e82SBenjamin Fair 2372e9c9e82SBenjamin Fair static int k3_validate_ns_entrypoint(uintptr_t entrypoint) 2382e9c9e82SBenjamin Fair { 2392e9c9e82SBenjamin Fair /* TODO: perform the proper validation */ 2402e9c9e82SBenjamin Fair 2412e9c9e82SBenjamin Fair return PSCI_E_SUCCESS; 2422e9c9e82SBenjamin Fair } 2432e9c9e82SBenjamin Fair 2442393c276SDave Gerlach #if K3_PM_SYSTEM_SUSPEND 2452393c276SDave Gerlach static void k3_pwr_domain_suspend(const psci_power_state_t *target_state) 2462393c276SDave Gerlach { 2472393c276SDave Gerlach unsigned int core, proc_id; 2482393c276SDave Gerlach 2492393c276SDave Gerlach core = plat_my_core_pos(); 2502393c276SDave Gerlach proc_id = PLAT_PROC_START_ID + core; 2512393c276SDave Gerlach 2522393c276SDave Gerlach /* Prevent interrupts from spuriously waking up this cpu */ 2532393c276SDave Gerlach k3_gic_cpuif_disable(); 2542393c276SDave Gerlach k3_gic_save_context(); 2552393c276SDave Gerlach 2562393c276SDave Gerlach k3_pwr_domain_off(target_state); 2572393c276SDave Gerlach 2582393c276SDave Gerlach ti_sci_enter_sleep(proc_id, 0, k3_sec_entrypoint); 2592393c276SDave Gerlach } 2602393c276SDave Gerlach 2612393c276SDave Gerlach static void k3_pwr_domain_suspend_finish(const psci_power_state_t *target_state) 2622393c276SDave Gerlach { 2632393c276SDave Gerlach k3_gic_restore_context(); 2642393c276SDave Gerlach k3_gic_cpuif_enable(); 2652393c276SDave Gerlach } 2662393c276SDave Gerlach 2672393c276SDave Gerlach static void k3_get_sys_suspend_power_state(psci_power_state_t *req_state) 2682393c276SDave Gerlach { 2692393c276SDave Gerlach unsigned int i; 2702393c276SDave Gerlach 2712393c276SDave Gerlach /* CPU & cluster off, system in retention */ 2722393c276SDave Gerlach for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) { 2732393c276SDave Gerlach req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; 2742393c276SDave Gerlach } 2752393c276SDave Gerlach } 2762393c276SDave Gerlach #endif 2772393c276SDave Gerlach 2782e9c9e82SBenjamin Fair static const plat_psci_ops_t k3_plat_psci_ops = { 2792e9c9e82SBenjamin Fair .cpu_standby = k3_cpu_standby, 2802e9c9e82SBenjamin Fair .pwr_domain_on = k3_pwr_domain_on, 2812e9c9e82SBenjamin Fair .pwr_domain_off = k3_pwr_domain_off, 2822e9c9e82SBenjamin Fair .pwr_domain_on_finish = k3_pwr_domain_on_finish, 2832393c276SDave Gerlach #if K3_PM_SYSTEM_SUSPEND 2842393c276SDave Gerlach .pwr_domain_suspend = k3_pwr_domain_suspend, 2852393c276SDave Gerlach .pwr_domain_suspend_finish = k3_pwr_domain_suspend_finish, 2862393c276SDave Gerlach .get_sys_suspend_power_state = k3_get_sys_suspend_power_state, 2872393c276SDave Gerlach #endif 28842d9b3aaSJan Kiszka .system_off = k3_system_off, 2892e9c9e82SBenjamin Fair .system_reset = k3_system_reset, 2902e9c9e82SBenjamin Fair .validate_power_state = k3_validate_power_state, 2912e9c9e82SBenjamin Fair .validate_ns_entrypoint = k3_validate_ns_entrypoint 2922e9c9e82SBenjamin Fair }; 2932e9c9e82SBenjamin Fair 2942e9c9e82SBenjamin Fair int plat_setup_psci_ops(uintptr_t sec_entrypoint, 2952e9c9e82SBenjamin Fair const plat_psci_ops_t **psci_ops) 2962e9c9e82SBenjamin Fair { 2972e9c9e82SBenjamin Fair k3_sec_entrypoint = sec_entrypoint; 2982e9c9e82SBenjamin Fair 2992e9c9e82SBenjamin Fair *psci_ops = &k3_plat_psci_ops; 3002e9c9e82SBenjamin Fair 3012e9c9e82SBenjamin Fair return 0; 3022e9c9e82SBenjamin Fair } 303