1532ed618SSoby Mathew /* 2532ed618SSoby Mathew * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 4532ed618SSoby Mathew * Redistribution and use in source and binary forms, with or without 5532ed618SSoby Mathew * modification, are permitted provided that the following conditions are met: 6532ed618SSoby Mathew * 7532ed618SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8532ed618SSoby Mathew * list of conditions and the following disclaimer. 9532ed618SSoby Mathew * 10532ed618SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11532ed618SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12532ed618SSoby Mathew * and/or other materials provided with the distribution. 13532ed618SSoby Mathew * 14532ed618SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15532ed618SSoby Mathew * to endorse or promote products derived from this software without specific 16532ed618SSoby Mathew * prior written permission. 17532ed618SSoby Mathew * 18532ed618SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19532ed618SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20532ed618SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21532ed618SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22532ed618SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23532ed618SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24532ed618SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25532ed618SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26532ed618SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27532ed618SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28532ed618SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29532ed618SSoby Mathew */ 30532ed618SSoby Mathew 31532ed618SSoby Mathew #include <arch.h> 32532ed618SSoby Mathew #include <arch_helpers.h> 33532ed618SSoby Mathew #include <assert.h> 34532ed618SSoby Mathew #include <debug.h> 35532ed618SSoby Mathew #include <platform.h> 36*872be88aSdp-arm #include <pmf.h> 37*872be88aSdp-arm #include <runtime_instr.h> 38532ed618SSoby Mathew #include <string.h> 39532ed618SSoby Mathew #include "psci_private.h" 40532ed618SSoby Mathew 41532ed618SSoby Mathew /****************************************************************************** 42532ed618SSoby Mathew * Construct the psci_power_state to request power OFF at all power levels. 43532ed618SSoby Mathew ******************************************************************************/ 44532ed618SSoby Mathew static void psci_set_power_off_state(psci_power_state_t *state_info) 45532ed618SSoby Mathew { 46532ed618SSoby Mathew int lvl; 47532ed618SSoby Mathew 48532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++) 49532ed618SSoby Mathew state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE; 50532ed618SSoby Mathew } 51532ed618SSoby Mathew 52532ed618SSoby Mathew /****************************************************************************** 53532ed618SSoby Mathew * Top level handler which is called when a cpu wants to power itself down. 54532ed618SSoby Mathew * It's assumed that along with turning the cpu power domain off, power 55532ed618SSoby Mathew * domains at higher levels will be turned off as far as possible. It finds 56532ed618SSoby Mathew * the highest level where a domain has to be powered off by traversing the 57532ed618SSoby Mathew * node information and then performs generic, architectural, platform setup 58532ed618SSoby Mathew * and state management required to turn OFF that power domain and domains 59532ed618SSoby Mathew * below it. e.g. For a cpu that's to be powered OFF, it could mean programming 60532ed618SSoby Mathew * the power controller whereas for a cluster that's to be powered off, it will 61532ed618SSoby Mathew * call the platform specific code which will disable coherency at the 62532ed618SSoby Mathew * interconnect level if the cpu is the last in the cluster and also the 63532ed618SSoby Mathew * program the power controller. 64532ed618SSoby Mathew ******************************************************************************/ 65532ed618SSoby Mathew int psci_do_cpu_off(unsigned int end_pwrlvl) 66532ed618SSoby Mathew { 67532ed618SSoby Mathew int rc = PSCI_E_SUCCESS, idx = plat_my_core_pos(); 68532ed618SSoby Mathew psci_power_state_t state_info; 69532ed618SSoby Mathew 70532ed618SSoby Mathew /* 71532ed618SSoby Mathew * This function must only be called on platforms where the 72532ed618SSoby Mathew * CPU_OFF platform hooks have been implemented. 73532ed618SSoby Mathew */ 74532ed618SSoby Mathew assert(psci_plat_pm_ops->pwr_domain_off); 75532ed618SSoby Mathew 76532ed618SSoby Mathew /* 77532ed618SSoby Mathew * This function acquires the lock corresponding to each power 78532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 79532ed618SSoby Mathew * is snapshot and state management can be done safely. 80532ed618SSoby Mathew */ 81532ed618SSoby Mathew psci_acquire_pwr_domain_locks(end_pwrlvl, 82532ed618SSoby Mathew idx); 83532ed618SSoby Mathew 84532ed618SSoby Mathew /* 85532ed618SSoby Mathew * Call the cpu off handler registered by the Secure Payload Dispatcher 86532ed618SSoby Mathew * to let it do any bookkeeping. Assume that the SPD always reports an 87532ed618SSoby Mathew * E_DENIED error if SP refuse to power down 88532ed618SSoby Mathew */ 89532ed618SSoby Mathew if (psci_spd_pm && psci_spd_pm->svc_off) { 90532ed618SSoby Mathew rc = psci_spd_pm->svc_off(0); 91532ed618SSoby Mathew if (rc) 92532ed618SSoby Mathew goto exit; 93532ed618SSoby Mathew } 94532ed618SSoby Mathew 95532ed618SSoby Mathew /* Construct the psci_power_state for CPU_OFF */ 96532ed618SSoby Mathew psci_set_power_off_state(&state_info); 97532ed618SSoby Mathew 98532ed618SSoby Mathew /* 99532ed618SSoby Mathew * This function is passed the requested state info and 100532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 101532ed618SSoby Mathew * the end level specified. 102532ed618SSoby Mathew */ 103532ed618SSoby Mathew psci_do_state_coordination(end_pwrlvl, &state_info); 104532ed618SSoby Mathew 105532ed618SSoby Mathew #if ENABLE_PSCI_STAT 106532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 107532ed618SSoby Mathew psci_stats_update_pwr_down(end_pwrlvl, &state_info); 108532ed618SSoby Mathew #endif 109532ed618SSoby Mathew 110532ed618SSoby Mathew /* 111532ed618SSoby Mathew * Arch. management. Perform the necessary steps to flush all 112532ed618SSoby Mathew * cpu caches. 113532ed618SSoby Mathew */ 114532ed618SSoby Mathew psci_do_pwrdown_cache_maintenance(psci_find_max_off_lvl(&state_info)); 115532ed618SSoby Mathew 116532ed618SSoby Mathew /* 117532ed618SSoby Mathew * Plat. management: Perform platform specific actions to turn this 118532ed618SSoby Mathew * cpu off e.g. exit cpu coherency, program the power controller etc. 119532ed618SSoby Mathew */ 120532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_off(&state_info); 121532ed618SSoby Mathew 122532ed618SSoby Mathew #if ENABLE_PSCI_STAT 123532ed618SSoby Mathew /* 124532ed618SSoby Mathew * Capture time-stamp while entering low power state. 125532ed618SSoby Mathew * No cache maintenance needed because caches are off 126532ed618SSoby Mathew * and writes are direct to main memory. 127532ed618SSoby Mathew */ 128532ed618SSoby Mathew PMF_CAPTURE_TIMESTAMP(psci_svc, PSCI_STAT_ID_ENTER_LOW_PWR, 129532ed618SSoby Mathew PMF_NO_CACHE_MAINT); 130532ed618SSoby Mathew #endif 131532ed618SSoby Mathew 132532ed618SSoby Mathew exit: 133532ed618SSoby Mathew /* 134532ed618SSoby Mathew * Release the locks corresponding to each power level in the 135532ed618SSoby Mathew * reverse order to which they were acquired. 136532ed618SSoby Mathew */ 137532ed618SSoby Mathew psci_release_pwr_domain_locks(end_pwrlvl, 138532ed618SSoby Mathew idx); 139532ed618SSoby Mathew 140532ed618SSoby Mathew /* 141532ed618SSoby Mathew * Check if all actions needed to safely power down this cpu have 142532ed618SSoby Mathew * successfully completed. 143532ed618SSoby Mathew */ 144532ed618SSoby Mathew if (rc == PSCI_E_SUCCESS) { 145532ed618SSoby Mathew /* 146532ed618SSoby Mathew * Set the affinity info state to OFF. This writes directly to 147532ed618SSoby Mathew * main memory as caches are disabled, so cache maintenance is 148532ed618SSoby Mathew * required to ensure that later cached reads of aff_info_state 149532ed618SSoby Mathew * return AFF_STATE_OFF. A dsbish() ensures ordering of the 150532ed618SSoby Mathew * update to the affinity info state prior to cache line 151532ed618SSoby Mathew * invalidation. 152532ed618SSoby Mathew */ 153532ed618SSoby Mathew flush_cpu_data(psci_svc_cpu_data.aff_info_state); 154532ed618SSoby Mathew psci_set_aff_info_state(AFF_STATE_OFF); 155532ed618SSoby Mathew dsbish(); 156532ed618SSoby Mathew inv_cpu_data(psci_svc_cpu_data.aff_info_state); 157532ed618SSoby Mathew 158*872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 159*872be88aSdp-arm 160*872be88aSdp-arm /* 161*872be88aSdp-arm * Update the timestamp with cache off. We assume this 162*872be88aSdp-arm * timestamp can only be read from the current CPU and the 163*872be88aSdp-arm * timestamp cache line will be flushed before return to 164*872be88aSdp-arm * normal world on wakeup. 165*872be88aSdp-arm */ 166*872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 167*872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 168*872be88aSdp-arm PMF_NO_CACHE_MAINT); 169*872be88aSdp-arm #endif 170*872be88aSdp-arm 171532ed618SSoby Mathew if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi) { 172532ed618SSoby Mathew /* This function must not return */ 173532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_pwr_down_wfi(&state_info); 174532ed618SSoby Mathew } else { 175532ed618SSoby Mathew /* 176532ed618SSoby Mathew * Enter a wfi loop which will allow the power 177532ed618SSoby Mathew * controller to physically power down this cpu. 178532ed618SSoby Mathew */ 179532ed618SSoby Mathew psci_power_down_wfi(); 180532ed618SSoby Mathew } 181532ed618SSoby Mathew } 182532ed618SSoby Mathew 183532ed618SSoby Mathew return rc; 184532ed618SSoby Mathew } 185