18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_IDLE_H
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_IDLE_H
48c2ecf20Sopenharmony_ci#include <asm/runlatch.h>
58c2ecf20Sopenharmony_ci#include <asm/paca.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_PSERIES
88c2ecf20Sopenharmony_ciDECLARE_PER_CPU(u64, idle_spurr_cycles);
98c2ecf20Sopenharmony_ciDECLARE_PER_CPU(u64, idle_entry_purr_snap);
108c2ecf20Sopenharmony_ciDECLARE_PER_CPU(u64, idle_entry_spurr_snap);
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic inline void snapshot_purr_idle_entry(void)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	*this_cpu_ptr(&idle_entry_purr_snap) = mfspr(SPRN_PURR);
158c2ecf20Sopenharmony_ci}
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic inline void snapshot_spurr_idle_entry(void)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	*this_cpu_ptr(&idle_entry_spurr_snap) = mfspr(SPRN_SPURR);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic inline void update_idle_purr_accounting(void)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	u64 wait_cycles;
258c2ecf20Sopenharmony_ci	u64 in_purr = *this_cpu_ptr(&idle_entry_purr_snap);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
288c2ecf20Sopenharmony_ci	wait_cycles += mfspr(SPRN_PURR) - in_purr;
298c2ecf20Sopenharmony_ci	get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic inline void update_idle_spurr_accounting(void)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	u64 *idle_spurr_cycles_ptr = this_cpu_ptr(&idle_spurr_cycles);
358c2ecf20Sopenharmony_ci	u64 in_spurr = *this_cpu_ptr(&idle_entry_spurr_snap);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	*idle_spurr_cycles_ptr += mfspr(SPRN_SPURR) - in_spurr;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic inline void pseries_idle_prolog(void)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	ppc64_runlatch_off();
438c2ecf20Sopenharmony_ci	snapshot_purr_idle_entry();
448c2ecf20Sopenharmony_ci	snapshot_spurr_idle_entry();
458c2ecf20Sopenharmony_ci	/*
468c2ecf20Sopenharmony_ci	 * Indicate to the HV that we are idle. Now would be
478c2ecf20Sopenharmony_ci	 * a good time to find other work to dispatch.
488c2ecf20Sopenharmony_ci	 */
498c2ecf20Sopenharmony_ci	get_lppaca()->idle = 1;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic inline void pseries_idle_epilog(void)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	update_idle_purr_accounting();
558c2ecf20Sopenharmony_ci	update_idle_spurr_accounting();
568c2ecf20Sopenharmony_ci	get_lppaca()->idle = 0;
578c2ecf20Sopenharmony_ci	ppc64_runlatch_on();
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic inline u64 read_this_idle_purr(void)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * If we are reading from an idle context, update the
648c2ecf20Sopenharmony_ci	 * idle-purr cycles corresponding to the last idle period.
658c2ecf20Sopenharmony_ci	 * Since the idle context is not yet over, take a fresh
668c2ecf20Sopenharmony_ci	 * snapshot of the idle-purr.
678c2ecf20Sopenharmony_ci	 */
688c2ecf20Sopenharmony_ci	if (unlikely(get_lppaca()->idle == 1)) {
698c2ecf20Sopenharmony_ci		update_idle_purr_accounting();
708c2ecf20Sopenharmony_ci		snapshot_purr_idle_entry();
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return be64_to_cpu(get_lppaca()->wait_state_cycles);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic inline u64 read_this_idle_spurr(void)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	/*
798c2ecf20Sopenharmony_ci	 * If we are reading from an idle context, update the
808c2ecf20Sopenharmony_ci	 * idle-spurr cycles corresponding to the last idle period.
818c2ecf20Sopenharmony_ci	 * Since the idle context is not yet over, take a fresh
828c2ecf20Sopenharmony_ci	 * snapshot of the idle-spurr.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	if (get_lppaca()->idle == 1) {
858c2ecf20Sopenharmony_ci		update_idle_spurr_accounting();
868c2ecf20Sopenharmony_ci		snapshot_spurr_idle_entry();
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return *this_cpu_ptr(&idle_spurr_cycles);
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_PSERIES */
938c2ecf20Sopenharmony_ci#endif
94