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; 31890781d1SMaheedhar 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) { 36890781d1SMaheedhar 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) { 41890781d1SMaheedhar 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 51890781d1SMaheedhar Bollapalli ret = PSCI_E_SUCCESS; 52890781d1SMaheedhar Bollapalli 53890781d1SMaheedhar Bollapalli exit_label: 54890781d1SMaheedhar 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 197*435bc14aSMaheedhar Bollapalli static int32_t versal_validate_ns_entrypoint(uint64_t ns_entrypoint) 198*435bc14aSMaheedhar Bollapalli { 199*435bc14aSMaheedhar Bollapalli int32_t ret = PSCI_E_SUCCESS; 200*435bc14aSMaheedhar Bollapalli 201*435bc14aSMaheedhar Bollapalli if (((ns_entrypoint >= PLAT_DDR_LOWMEM_MAX) && (ns_entrypoint <= PLAT_DDR_HIGHMEM_MAX)) || 202*435bc14aSMaheedhar Bollapalli ((ns_entrypoint >= BL31_BASE) && (ns_entrypoint <= BL31_LIMIT))) { 203*435bc14aSMaheedhar Bollapalli ret = PSCI_E_INVALID_ADDRESS; 204*435bc14aSMaheedhar Bollapalli } 205*435bc14aSMaheedhar Bollapalli 206*435bc14aSMaheedhar Bollapalli return ret; 207*435bc14aSMaheedhar Bollapalli } 208*435bc14aSMaheedhar Bollapalli 2090abf4bbaSSaeed Nowshadi /** 210de7ed953SPrasad Kummari * versal_pwr_domain_off() - This function performs actions to turn off core. 211de7ed953SPrasad Kummari * @target_state: Targated state. 2125a8ffeabSTejas Patel * 2135a8ffeabSTejas Patel */ 2145a8ffeabSTejas Patel static void versal_pwr_domain_off(const psci_power_state_t *target_state) 2155a8ffeabSTejas Patel { 216e452826aSMaheedhar Bollapalli uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U}; 217912b7a6fSVenkatesh Yadav Abbarapu uint32_t cpu_id = plat_my_core_pos(); 2185a8ffeabSTejas Patel const struct pm_proc *proc = pm_get_proc(cpu_id); 2195a8ffeabSTejas Patel 220655e62aaSRonak Jain if (proc == NULL) { 221652c1ab1SMichal Simek return; 222652c1ab1SMichal Simek } 223652c1ab1SMichal Simek 2240623dceaSAbhyuday Godhasara for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { 2255a8ffeabSTejas Patel VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", 2265a8ffeabSTejas Patel __func__, i, target_state->pwr_domain_state[i]); 227b9fa2d9fSAbhyuday Godhasara } 2285a8ffeabSTejas Patel 2295a8ffeabSTejas Patel /* Prevent interrupts from spuriously waking up this cpu */ 2305a8ffeabSTejas Patel plat_versal_gic_cpuif_disable(); 2315a8ffeabSTejas Patel 2325a8ffeabSTejas Patel /* 2335a8ffeabSTejas Patel * Send request to PMC to power down the appropriate APU CPU 2345a8ffeabSTejas Patel * core. 2355a8ffeabSTejas Patel * According to PSCI specification, CPU_off function does not 2365a8ffeabSTejas Patel * have resume address and CPU core can only be woken up 2375a8ffeabSTejas Patel * invoking CPU_on function, during which resume address will 2385a8ffeabSTejas Patel * be set. 2395a8ffeabSTejas Patel */ 240e452826aSMaheedhar Bollapalli ret = pm_feature_check((uint32_t)PM_SELF_SUSPEND, &version_type[0], SECURE_FLAG); 241b802b278SMaheedhar Bollapalli if (ret == (uint32_t)PM_RET_SUCCESS) { 242e452826aSMaheedhar Bollapalli fw_api_version = version_type[0] & 0xFFFFU; 24359497016SJay Buddhabhatti if (fw_api_version >= 3U) { 24459497016SJay Buddhabhatti (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0, 24559497016SJay Buddhabhatti SECURE_FLAG); 24659497016SJay Buddhabhatti } else { 247526a1fd1SAbhyuday Godhasara (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0, 2484697164aSTejas Patel SECURE_FLAG); 2495a8ffeabSTejas Patel } 25059497016SJay Buddhabhatti } 25159497016SJay Buddhabhatti } 2525a8ffeabSTejas Patel 2535a8ffeabSTejas Patel /** 2545a8ffeabSTejas Patel * versal_validate_power_state() - This function ensures that the power state 2555a8ffeabSTejas Patel * parameter in request is valid. 256de7ed953SPrasad Kummari * @power_state: Power state of core. 257de7ed953SPrasad Kummari * @req_state: Requested state. 2585a8ffeabSTejas Patel * 259de7ed953SPrasad Kummari * Return: Returns status, either success or reason. 2605a8ffeabSTejas Patel * 2615a8ffeabSTejas Patel */ 262912b7a6fSVenkatesh Yadav Abbarapu static int32_t versal_validate_power_state(uint32_t power_state, 2635a8ffeabSTejas Patel psci_power_state_t *req_state) 2645a8ffeabSTejas Patel { 265890781d1SMaheedhar Bollapalli int32_t ret = PSCI_E_SUCCESS; 2665a8ffeabSTejas Patel VERBOSE("%s: power_state: 0x%x\n", __func__, power_state); 2675a8ffeabSTejas Patel 268912b7a6fSVenkatesh Yadav Abbarapu uint32_t pstate = psci_get_pstate_type(power_state); 2695a8ffeabSTejas Patel 270b39c82e9SMaheedhar Bollapalli assert(req_state != NULL); 2715a8ffeabSTejas Patel 2725a8ffeabSTejas Patel /* Sanity check the requested state */ 273b9fa2d9fSAbhyuday Godhasara if (pstate == PSTATE_TYPE_STANDBY) { 2745a8ffeabSTejas Patel req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE; 275b9fa2d9fSAbhyuday Godhasara } else { 2765a8ffeabSTejas Patel req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE; 277b9fa2d9fSAbhyuday Godhasara } 2785a8ffeabSTejas Patel 2795a8ffeabSTejas Patel /* We expect the 'state id' to be zero */ 280a62c40d4SAbhyuday Godhasara if (psci_get_pstate_id(power_state) != 0U) { 281890781d1SMaheedhar Bollapalli ret = PSCI_E_INVALID_PARAMS; 282b9fa2d9fSAbhyuday Godhasara } 2835a8ffeabSTejas Patel 284890781d1SMaheedhar Bollapalli return ret; 2855a8ffeabSTejas Patel } 2865a8ffeabSTejas Patel 2875a8ffeabSTejas Patel /** 288de7ed953SPrasad Kummari * versal_get_sys_suspend_power_state() - Get power state for system suspend. 289de7ed953SPrasad Kummari * @req_state: Requested state. 2905a8ffeabSTejas Patel * 2915a8ffeabSTejas Patel */ 2925a8ffeabSTejas Patel static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state) 2935a8ffeabSTejas Patel { 2945a8ffeabSTejas Patel req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE; 2955a8ffeabSTejas Patel req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE; 2965a8ffeabSTejas Patel } 2975a8ffeabSTejas Patel 298f91c3cb1SSiva Durga Prasad Paladugu static const struct plat_psci_ops versal_nopmc_psci_ops = { 299394a65aaSTejas Patel .pwr_domain_on = versal_pwr_domain_on, 3005a8ffeabSTejas Patel .pwr_domain_off = versal_pwr_domain_off, 301f91c3cb1SSiva Durga Prasad Paladugu .pwr_domain_on_finish = versal_pwr_domain_on_finish, 3025a8ffeabSTejas Patel .pwr_domain_suspend = versal_pwr_domain_suspend, 3035a8ffeabSTejas Patel .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish, 3040abf4bbaSSaeed Nowshadi .system_off = versal_system_off, 3050abf4bbaSSaeed Nowshadi .system_reset = versal_system_reset, 306*435bc14aSMaheedhar Bollapalli .validate_ns_entrypoint = versal_validate_ns_entrypoint, 3075a8ffeabSTejas Patel .validate_power_state = versal_validate_power_state, 3085a8ffeabSTejas Patel .get_sys_suspend_power_state = versal_get_sys_suspend_power_state, 309f91c3cb1SSiva Durga Prasad Paladugu }; 310f91c3cb1SSiva Durga Prasad Paladugu 311f91c3cb1SSiva Durga Prasad Paladugu /******************************************************************************* 312f91c3cb1SSiva Durga Prasad Paladugu * Export the platform specific power ops. 313f91c3cb1SSiva Durga Prasad Paladugu ******************************************************************************/ 314f7c48d9eSVenkatesh Yadav Abbarapu int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint, 315f91c3cb1SSiva Durga Prasad Paladugu const struct plat_psci_ops **psci_ops) 316f91c3cb1SSiva Durga Prasad Paladugu { 317f91c3cb1SSiva Durga Prasad Paladugu versal_sec_entry = sec_entrypoint; 318f91c3cb1SSiva Durga Prasad Paladugu 319f91c3cb1SSiva Durga Prasad Paladugu *psci_ops = &versal_nopmc_psci_ops; 320f91c3cb1SSiva Durga Prasad Paladugu 321f91c3cb1SSiva Durga Prasad Paladugu return 0; 322f91c3cb1SSiva Durga Prasad Paladugu } 323