162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_IDLE_H 362306a36Sopenharmony_ci#define _ASM_POWERPC_IDLE_H 462306a36Sopenharmony_ci#include <asm/runlatch.h> 562306a36Sopenharmony_ci#include <asm/paca.h> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#ifdef CONFIG_PPC_PSERIES 862306a36Sopenharmony_ciDECLARE_PER_CPU(u64, idle_spurr_cycles); 962306a36Sopenharmony_ciDECLARE_PER_CPU(u64, idle_entry_purr_snap); 1062306a36Sopenharmony_ciDECLARE_PER_CPU(u64, idle_entry_spurr_snap); 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_cistatic __always_inline void snapshot_purr_idle_entry(void) 1362306a36Sopenharmony_ci{ 1462306a36Sopenharmony_ci *this_cpu_ptr(&idle_entry_purr_snap) = mfspr(SPRN_PURR); 1562306a36Sopenharmony_ci} 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cistatic __always_inline void snapshot_spurr_idle_entry(void) 1862306a36Sopenharmony_ci{ 1962306a36Sopenharmony_ci *this_cpu_ptr(&idle_entry_spurr_snap) = mfspr(SPRN_SPURR); 2062306a36Sopenharmony_ci} 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_cistatic __always_inline void update_idle_purr_accounting(void) 2362306a36Sopenharmony_ci{ 2462306a36Sopenharmony_ci u64 wait_cycles; 2562306a36Sopenharmony_ci u64 in_purr = *this_cpu_ptr(&idle_entry_purr_snap); 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles); 2862306a36Sopenharmony_ci wait_cycles += mfspr(SPRN_PURR) - in_purr; 2962306a36Sopenharmony_ci get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles); 3062306a36Sopenharmony_ci} 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_cistatic __always_inline void update_idle_spurr_accounting(void) 3362306a36Sopenharmony_ci{ 3462306a36Sopenharmony_ci u64 *idle_spurr_cycles_ptr = this_cpu_ptr(&idle_spurr_cycles); 3562306a36Sopenharmony_ci u64 in_spurr = *this_cpu_ptr(&idle_entry_spurr_snap); 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci *idle_spurr_cycles_ptr += mfspr(SPRN_SPURR) - in_spurr; 3862306a36Sopenharmony_ci} 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_cistatic __always_inline void pseries_idle_prolog(void) 4162306a36Sopenharmony_ci{ 4262306a36Sopenharmony_ci ppc64_runlatch_off(); 4362306a36Sopenharmony_ci snapshot_purr_idle_entry(); 4462306a36Sopenharmony_ci snapshot_spurr_idle_entry(); 4562306a36Sopenharmony_ci /* 4662306a36Sopenharmony_ci * Indicate to the HV that we are idle. Now would be 4762306a36Sopenharmony_ci * a good time to find other work to dispatch. 4862306a36Sopenharmony_ci */ 4962306a36Sopenharmony_ci get_lppaca()->idle = 1; 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_cistatic __always_inline void pseries_idle_epilog(void) 5362306a36Sopenharmony_ci{ 5462306a36Sopenharmony_ci update_idle_purr_accounting(); 5562306a36Sopenharmony_ci update_idle_spurr_accounting(); 5662306a36Sopenharmony_ci get_lppaca()->idle = 0; 5762306a36Sopenharmony_ci ppc64_runlatch_on(); 5862306a36Sopenharmony_ci} 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_cistatic inline u64 read_this_idle_purr(void) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci /* 6362306a36Sopenharmony_ci * If we are reading from an idle context, update the 6462306a36Sopenharmony_ci * idle-purr cycles corresponding to the last idle period. 6562306a36Sopenharmony_ci * Since the idle context is not yet over, take a fresh 6662306a36Sopenharmony_ci * snapshot of the idle-purr. 6762306a36Sopenharmony_ci */ 6862306a36Sopenharmony_ci if (unlikely(get_lppaca()->idle == 1)) { 6962306a36Sopenharmony_ci update_idle_purr_accounting(); 7062306a36Sopenharmony_ci snapshot_purr_idle_entry(); 7162306a36Sopenharmony_ci } 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci return be64_to_cpu(get_lppaca()->wait_state_cycles); 7462306a36Sopenharmony_ci} 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_cistatic inline u64 read_this_idle_spurr(void) 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci /* 7962306a36Sopenharmony_ci * If we are reading from an idle context, update the 8062306a36Sopenharmony_ci * idle-spurr cycles corresponding to the last idle period. 8162306a36Sopenharmony_ci * Since the idle context is not yet over, take a fresh 8262306a36Sopenharmony_ci * snapshot of the idle-spurr. 8362306a36Sopenharmony_ci */ 8462306a36Sopenharmony_ci if (get_lppaca()->idle == 1) { 8562306a36Sopenharmony_ci update_idle_spurr_accounting(); 8662306a36Sopenharmony_ci snapshot_spurr_idle_entry(); 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci return *this_cpu_ptr(&idle_spurr_cycles); 9062306a36Sopenharmony_ci} 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci#endif /* CONFIG_PPC_PSERIES */ 9362306a36Sopenharmony_ci#endif 94