1f91c3cb1SSiva Durga Prasad Paladugu /* 2619bc13eSMichal Simek * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved. 3652c1ab1SMichal Simek * Copyright (c) 2022-2024, Advanced Micro Devices, Inc. All rights reserved. 4f91c3cb1SSiva Durga Prasad Paladugu * 5f91c3cb1SSiva Durga Prasad Paladugu * SPDX-License-Identifier: BSD-3-Clause 6f91c3cb1SSiva Durga Prasad Paladugu */ 7f91c3cb1SSiva Durga Prasad Paladugu 85a8ffeabSTejas Patel #include <assert.h> 901a326abSPrasad Kummari 1009d40e0eSAntonio Nino Diaz #include <common/debug.h> 1109d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 1209d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 130abf4bbaSSaeed Nowshadi #include <plat/arm/common/plat_arm.h> 1401a326abSPrasad Kummari #include <plat/common/platform.h> 1501a326abSPrasad Kummari #include <plat_arm.h> 1609d40e0eSAntonio Nino Diaz 1788ee0816SJay Buddhabhatti #include "drivers/delay_timer.h" 1801a326abSPrasad Kummari #include <plat_private.h> 19394a65aaSTejas Patel #include "pm_api_sys.h" 20394a65aaSTejas Patel #include "pm_client.h" 2101a326abSPrasad Kummari #include <pm_common.h> 2288ee0816SJay Buddhabhatti #include "pm_ipi.h" 2388ee0816SJay Buddhabhatti #include "pm_svc_main.h" 24394a65aaSTejas Patel 25f91c3cb1SSiva Durga Prasad Paladugu static uintptr_t versal_sec_entry; 26f91c3cb1SSiva Durga Prasad Paladugu 27912b7a6fSVenkatesh Yadav Abbarapu static int32_t versal_pwr_domain_on(u_register_t mpidr) 28f91c3cb1SSiva Durga Prasad Paladugu { 29912b7a6fSVenkatesh Yadav Abbarapu int32_t cpu_id = plat_core_pos_by_mpidr(mpidr); 30394a65aaSTejas Patel const struct pm_proc *proc; 31*890781d1SMaheedhar Bollapalli int32_t ret = PSCI_E_INTERN_FAIL; 32f91c3cb1SSiva Durga Prasad Paladugu 33f91c3cb1SSiva Durga Prasad Paladugu VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr); 34f91c3cb1SSiva Durga Prasad Paladugu 35b9fa2d9fSAbhyuday Godhasara if (cpu_id == -1) { 36*890781d1SMaheedhar Bollapalli goto exit_label; 37b9fa2d9fSAbhyuday Godhasara } 38f91c3cb1SSiva Durga Prasad Paladugu 39912b7a6fSVenkatesh Yadav Abbarapu proc = pm_get_proc((uint32_t)cpu_id); 40655e62aaSRonak Jain if (proc == NULL) { 41*890781d1SMaheedhar Bollapalli goto exit_label; 42652c1ab1SMichal Simek } 43f91c3cb1SSiva Durga Prasad Paladugu 44394a65aaSTejas Patel /* Send request to PMC to wake up selected ACPU core */ 45526a1fd1SAbhyuday Godhasara (void)pm_req_wakeup(proc->node_id, (versal_sec_entry & 0xFFFFFFFFU) | 0x1U, 464697164aSTejas Patel versal_sec_entry >> 32, 0, SECURE_FLAG); 47f91c3cb1SSiva Durga Prasad Paladugu 48394a65aaSTejas Patel /* Clear power down request */ 49394a65aaSTejas Patel pm_client_wakeup(proc); 50f91c3cb1SSiva Durga Prasad Paladugu 51*890781d1SMaheedhar Bollapalli ret = PSCI_E_SUCCESS; 52*890781d1SMaheedhar Bollapalli 53*890781d1SMaheedhar Bollapalli exit_label: 54*890781d1SMaheedhar Bollapalli return ret; 55f91c3cb1SSiva Durga Prasad Paladugu } 56f91c3cb1SSiva Durga Prasad Paladugu 575a8ffeabSTejas Patel /** 585a8ffeabSTejas Patel * versal_pwr_domain_suspend() - This function sends request to PMC to suspend 595a8ffeabSTejas Patel * core. 60de7ed953SPrasad Kummari * @target_state: Targated state. 615a8ffeabSTejas Patel * 625a8ffeabSTejas Patel */ 635a8ffeabSTejas Patel static void versal_pwr_domain_suspend(const psci_power_state_t *target_state) 645a8ffeabSTejas Patel { 65912b7a6fSVenkatesh Yadav Abbarapu uint32_t state; 66912b7a6fSVenkatesh Yadav Abbarapu uint32_t cpu_id = plat_my_core_pos(); 675a8ffeabSTejas Patel const struct pm_proc *proc = pm_get_proc(cpu_id); 685a8ffeabSTejas Patel 69655e62aaSRonak Jain if (proc == NULL) { 70652c1ab1SMichal Simek return; 71652c1ab1SMichal Simek } 72652c1ab1SMichal Simek 730623dceaSAbhyuday Godhasara for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { 745a8ffeabSTejas Patel VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", 755a8ffeabSTejas Patel __func__, i, target_state->pwr_domain_state[i]); 76b9fa2d9fSAbhyuday Godhasara } 775a8ffeabSTejas Patel 785a8ffeabSTejas Patel plat_versal_gic_cpuif_disable(); 795a8ffeabSTejas Patel 80d4c7b550SRavi Patel if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { 815a8ffeabSTejas Patel plat_versal_gic_save(); 82d4c7b550SRavi Patel } 835a8ffeabSTejas Patel 840ed8b4bfSMaheedhar Bollapalli state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ? 855a8ffeabSTejas Patel PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE; 865a8ffeabSTejas Patel 875a8ffeabSTejas Patel /* Send request to PMC to suspend this core */ 88526a1fd1SAbhyuday Godhasara (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry, 894697164aSTejas Patel SECURE_FLAG); 905a8ffeabSTejas Patel 915a8ffeabSTejas Patel /* APU is to be turned off */ 925a8ffeabSTejas Patel if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { 935a8ffeabSTejas Patel /* disable coherency */ 945a8ffeabSTejas Patel plat_arm_interconnect_exit_coherency(); 955a8ffeabSTejas Patel } 965a8ffeabSTejas Patel } 975a8ffeabSTejas Patel 985a8ffeabSTejas Patel /** 995a8ffeabSTejas Patel * versal_pwr_domain_suspend_finish() - This function performs actions to finish 1005a8ffeabSTejas Patel * suspend procedure. 101de7ed953SPrasad Kummari * @target_state: Targated state. 1025a8ffeabSTejas Patel * 1035a8ffeabSTejas Patel */ 1045a8ffeabSTejas Patel static void versal_pwr_domain_suspend_finish( 1055a8ffeabSTejas Patel const psci_power_state_t *target_state) 1065a8ffeabSTejas Patel { 107912b7a6fSVenkatesh Yadav Abbarapu uint32_t cpu_id = plat_my_core_pos(); 1085a8ffeabSTejas Patel const struct pm_proc *proc = pm_get_proc(cpu_id); 1095a8ffeabSTejas Patel 110655e62aaSRonak Jain if (proc == NULL) { 111652c1ab1SMichal Simek return; 112652c1ab1SMichal Simek } 113652c1ab1SMichal Simek 1140623dceaSAbhyuday Godhasara for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { 1155a8ffeabSTejas Patel VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", 1165a8ffeabSTejas Patel __func__, i, target_state->pwr_domain_state[i]); 117b9fa2d9fSAbhyuday Godhasara } 1185a8ffeabSTejas Patel 1195a8ffeabSTejas Patel /* Clear the APU power control register for this cpu */ 1205a8ffeabSTejas Patel pm_client_wakeup(proc); 1215a8ffeabSTejas Patel 1225a8ffeabSTejas Patel /* enable coherency */ 1235a8ffeabSTejas Patel plat_arm_interconnect_enter_coherency(); 1245a8ffeabSTejas Patel 1255a8ffeabSTejas Patel /* APU was turned off, so restore GIC context */ 1265a8ffeabSTejas Patel if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { 1275a8ffeabSTejas Patel plat_versal_gic_resume(); 1285a8ffeabSTejas Patel } 129d4c7b550SRavi Patel 130d4c7b550SRavi Patel plat_versal_gic_cpuif_enable(); 1315a8ffeabSTejas Patel } 1325a8ffeabSTejas Patel 13316c611f8SMaheedhar Bollapalli static void versal_pwr_domain_on_finish(const psci_power_state_t *target_state) 134f91c3cb1SSiva Durga Prasad Paladugu { 135f91c3cb1SSiva Durga Prasad Paladugu /* Enable the gic cpu interface */ 136f91c3cb1SSiva Durga Prasad Paladugu plat_versal_gic_pcpu_init(); 137f91c3cb1SSiva Durga Prasad Paladugu 138f91c3cb1SSiva Durga Prasad Paladugu /* Program the gic per-cpu distributor or re-distributor interface */ 139f91c3cb1SSiva Durga Prasad Paladugu plat_versal_gic_cpuif_enable(); 140f91c3cb1SSiva Durga Prasad Paladugu } 141f91c3cb1SSiva Durga Prasad Paladugu 1425a8ffeabSTejas Patel /** 143de7ed953SPrasad Kummari * versal_system_off() - This function sends the system off request to firmware. 144de7ed953SPrasad Kummari * This function does not return. 145de7ed953SPrasad Kummari * 1460abf4bbaSSaeed Nowshadi */ 1470abf4bbaSSaeed Nowshadi static void __dead2 versal_system_off(void) 1480abf4bbaSSaeed Nowshadi { 1490abf4bbaSSaeed Nowshadi /* Send the power down request to the PMC */ 150526a1fd1SAbhyuday Godhasara (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN, 1514697164aSTejas Patel pm_get_shutdown_scope(), SECURE_FLAG); 1520abf4bbaSSaeed Nowshadi 15312475663SMaheedhar Bollapalli while (true) { 1540abf4bbaSSaeed Nowshadi wfi(); 1550abf4bbaSSaeed Nowshadi } 156b9fa2d9fSAbhyuday Godhasara } 1570abf4bbaSSaeed Nowshadi 1580abf4bbaSSaeed Nowshadi /** 159de7ed953SPrasad Kummari * versal_system_reset() - This function sends the reset request to firmware 160de7ed953SPrasad Kummari * for the system to reset. This function does not 161de7ed953SPrasad Kummari * return. 162de7ed953SPrasad Kummari * 1630abf4bbaSSaeed Nowshadi */ 1640abf4bbaSSaeed Nowshadi static void __dead2 versal_system_reset(void) 1650abf4bbaSSaeed Nowshadi { 16688ee0816SJay Buddhabhatti uint32_t ret, timeout = 10000U; 16788ee0816SJay Buddhabhatti 16888ee0816SJay Buddhabhatti request_cpu_pwrdwn(); 16988ee0816SJay Buddhabhatti 17088ee0816SJay Buddhabhatti /* 17188ee0816SJay Buddhabhatti * Send the system reset request to the firmware if power down request 17288ee0816SJay Buddhabhatti * is not received from firmware. 17388ee0816SJay Buddhabhatti */ 17488ee0816SJay Buddhabhatti if (!pwrdwn_req_received) { 175526a1fd1SAbhyuday Godhasara (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET, 1764697164aSTejas Patel pm_get_shutdown_scope(), SECURE_FLAG); 1770abf4bbaSSaeed Nowshadi 17888ee0816SJay Buddhabhatti /* 17988ee0816SJay Buddhabhatti * Wait for system shutdown request completed and idle callback 18088ee0816SJay Buddhabhatti * not received. 18188ee0816SJay Buddhabhatti */ 18288ee0816SJay Buddhabhatti do { 18388ee0816SJay Buddhabhatti ret = ipi_mb_enquire_status(primary_proc->ipi->local_ipi_id, 18488ee0816SJay Buddhabhatti primary_proc->ipi->remote_ipi_id); 18588ee0816SJay Buddhabhatti udelay(100); 18688ee0816SJay Buddhabhatti timeout--; 18788ee0816SJay Buddhabhatti } while ((ret != IPI_MB_STATUS_RECV_PENDING) && (timeout > 0U)); 18888ee0816SJay Buddhabhatti } 18988ee0816SJay Buddhabhatti 19088ee0816SJay Buddhabhatti (void)psci_cpu_off(); 19188ee0816SJay Buddhabhatti 19212475663SMaheedhar Bollapalli while (true) { 1930abf4bbaSSaeed Nowshadi wfi(); 1940abf4bbaSSaeed Nowshadi } 195b9fa2d9fSAbhyuday Godhasara } 1960abf4bbaSSaeed Nowshadi 1970abf4bbaSSaeed Nowshadi /** 198de7ed953SPrasad Kummari * versal_pwr_domain_off() - This function performs actions to turn off core. 199de7ed953SPrasad Kummari * @target_state: Targated state. 2005a8ffeabSTejas Patel * 2015a8ffeabSTejas Patel */ 2025a8ffeabSTejas Patel static void versal_pwr_domain_off(const psci_power_state_t *target_state) 2035a8ffeabSTejas Patel { 204e452826aSMaheedhar Bollapalli uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U}; 205912b7a6fSVenkatesh Yadav Abbarapu uint32_t cpu_id = plat_my_core_pos(); 2065a8ffeabSTejas Patel const struct pm_proc *proc = pm_get_proc(cpu_id); 2075a8ffeabSTejas Patel 208655e62aaSRonak Jain if (proc == NULL) { 209652c1ab1SMichal Simek return; 210652c1ab1SMichal Simek } 211652c1ab1SMichal Simek 2120623dceaSAbhyuday Godhasara for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { 2135a8ffeabSTejas Patel VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", 2145a8ffeabSTejas Patel __func__, i, target_state->pwr_domain_state[i]); 215b9fa2d9fSAbhyuday Godhasara } 2165a8ffeabSTejas Patel 2175a8ffeabSTejas Patel /* Prevent interrupts from spuriously waking up this cpu */ 2185a8ffeabSTejas Patel plat_versal_gic_cpuif_disable(); 2195a8ffeabSTejas Patel 2205a8ffeabSTejas Patel /* 2215a8ffeabSTejas Patel * Send request to PMC to power down the appropriate APU CPU 2225a8ffeabSTejas Patel * core. 2235a8ffeabSTejas Patel * According to PSCI specification, CPU_off function does not 2245a8ffeabSTejas Patel * have resume address and CPU core can only be woken up 2255a8ffeabSTejas Patel * invoking CPU_on function, during which resume address will 2265a8ffeabSTejas Patel * be set. 2275a8ffeabSTejas Patel */ 228e452826aSMaheedhar Bollapalli ret = pm_feature_check((uint32_t)PM_SELF_SUSPEND, &version_type[0], SECURE_FLAG); 229b802b278SMaheedhar Bollapalli if (ret == (uint32_t)PM_RET_SUCCESS) { 230e452826aSMaheedhar Bollapalli fw_api_version = version_type[0] & 0xFFFFU; 23159497016SJay Buddhabhatti if (fw_api_version >= 3U) { 23259497016SJay Buddhabhatti (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0, 23359497016SJay Buddhabhatti SECURE_FLAG); 23459497016SJay Buddhabhatti } else { 235526a1fd1SAbhyuday Godhasara (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0, 2364697164aSTejas Patel SECURE_FLAG); 2375a8ffeabSTejas Patel } 23859497016SJay Buddhabhatti } 23959497016SJay Buddhabhatti } 2405a8ffeabSTejas Patel 2415a8ffeabSTejas Patel /** 2425a8ffeabSTejas Patel * versal_validate_power_state() - This function ensures that the power state 2435a8ffeabSTejas Patel * parameter in request is valid. 244de7ed953SPrasad Kummari * @power_state: Power state of core. 245de7ed953SPrasad Kummari * @req_state: Requested state. 2465a8ffeabSTejas Patel * 247de7ed953SPrasad Kummari * Return: Returns status, either success or reason. 2485a8ffeabSTejas Patel * 2495a8ffeabSTejas Patel */ 250912b7a6fSVenkatesh Yadav Abbarapu static int32_t versal_validate_power_state(uint32_t power_state, 2515a8ffeabSTejas Patel psci_power_state_t *req_state) 2525a8ffeabSTejas Patel { 253*890781d1SMaheedhar Bollapalli int32_t ret = PSCI_E_SUCCESS; 2545a8ffeabSTejas Patel VERBOSE("%s: power_state: 0x%x\n", __func__, power_state); 2555a8ffeabSTejas Patel 256912b7a6fSVenkatesh Yadav Abbarapu uint32_t pstate = psci_get_pstate_type(power_state); 2575a8ffeabSTejas Patel 258b39c82e9SMaheedhar Bollapalli assert(req_state != NULL); 2595a8ffeabSTejas Patel 2605a8ffeabSTejas Patel /* Sanity check the requested state */ 261b9fa2d9fSAbhyuday Godhasara if (pstate == PSTATE_TYPE_STANDBY) { 2625a8ffeabSTejas Patel req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE; 263b9fa2d9fSAbhyuday Godhasara } else { 2645a8ffeabSTejas Patel req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE; 265b9fa2d9fSAbhyuday Godhasara } 2665a8ffeabSTejas Patel 2675a8ffeabSTejas Patel /* We expect the 'state id' to be zero */ 268a62c40d4SAbhyuday Godhasara if (psci_get_pstate_id(power_state) != 0U) { 269*890781d1SMaheedhar Bollapalli ret = PSCI_E_INVALID_PARAMS; 270b9fa2d9fSAbhyuday Godhasara } 2715a8ffeabSTejas Patel 272*890781d1SMaheedhar Bollapalli return ret; 2735a8ffeabSTejas Patel } 2745a8ffeabSTejas Patel 2755a8ffeabSTejas Patel /** 276de7ed953SPrasad Kummari * versal_get_sys_suspend_power_state() - Get power state for system suspend. 277de7ed953SPrasad Kummari * @req_state: Requested state. 2785a8ffeabSTejas Patel * 2795a8ffeabSTejas Patel */ 2805a8ffeabSTejas Patel static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state) 2815a8ffeabSTejas Patel { 2825a8ffeabSTejas Patel req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE; 2835a8ffeabSTejas Patel req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE; 2845a8ffeabSTejas Patel } 2855a8ffeabSTejas Patel 286f91c3cb1SSiva Durga Prasad Paladugu static const struct plat_psci_ops versal_nopmc_psci_ops = { 287394a65aaSTejas Patel .pwr_domain_on = versal_pwr_domain_on, 2885a8ffeabSTejas Patel .pwr_domain_off = versal_pwr_domain_off, 289f91c3cb1SSiva Durga Prasad Paladugu .pwr_domain_on_finish = versal_pwr_domain_on_finish, 2905a8ffeabSTejas Patel .pwr_domain_suspend = versal_pwr_domain_suspend, 2915a8ffeabSTejas Patel .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish, 2920abf4bbaSSaeed Nowshadi .system_off = versal_system_off, 2930abf4bbaSSaeed Nowshadi .system_reset = versal_system_reset, 2945a8ffeabSTejas Patel .validate_power_state = versal_validate_power_state, 2955a8ffeabSTejas Patel .get_sys_suspend_power_state = versal_get_sys_suspend_power_state, 296f91c3cb1SSiva Durga Prasad Paladugu }; 297f91c3cb1SSiva Durga Prasad Paladugu 298f91c3cb1SSiva Durga Prasad Paladugu /******************************************************************************* 299f91c3cb1SSiva Durga Prasad Paladugu * Export the platform specific power ops. 300f91c3cb1SSiva Durga Prasad Paladugu ******************************************************************************/ 301f7c48d9eSVenkatesh Yadav Abbarapu int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint, 302f91c3cb1SSiva Durga Prasad Paladugu const struct plat_psci_ops **psci_ops) 303f91c3cb1SSiva Durga Prasad Paladugu { 304f91c3cb1SSiva Durga Prasad Paladugu versal_sec_entry = sec_entrypoint; 305f91c3cb1SSiva Durga Prasad Paladugu 306f91c3cb1SSiva Durga Prasad Paladugu *psci_ops = &versal_nopmc_psci_ops; 307f91c3cb1SSiva Durga Prasad Paladugu 308f91c3cb1SSiva Durga Prasad Paladugu return 0; 309f91c3cb1SSiva Durga Prasad Paladugu } 310