xref: /openbmc/linux/arch/powerpc/include/asm/idle.h (revision 18b5e717)
1e4a884ccSGautham R. Shenoy /* SPDX-License-Identifier: GPL-2.0-or-later */
2e4a884ccSGautham R. Shenoy #ifndef _ASM_POWERPC_IDLE_H
3e4a884ccSGautham R. Shenoy #define _ASM_POWERPC_IDLE_H
4e4a884ccSGautham R. Shenoy #include <asm/runlatch.h>
5e4a884ccSGautham R. Shenoy #include <asm/paca.h>
6e4a884ccSGautham R. Shenoy 
7e4a884ccSGautham R. Shenoy #ifdef CONFIG_PPC_PSERIES
8dc8afce5SGautham R. Shenoy DECLARE_PER_CPU(u64, idle_spurr_cycles);
9c4019198SGautham R. Shenoy DECLARE_PER_CPU(u64, idle_entry_purr_snap);
10dc8afce5SGautham R. Shenoy DECLARE_PER_CPU(u64, idle_entry_spurr_snap);
11c4019198SGautham R. Shenoy 
snapshot_purr_idle_entry(void)12*18b5e717SMichael Ellerman static __always_inline void snapshot_purr_idle_entry(void)
13c4019198SGautham R. Shenoy {
14c4019198SGautham R. Shenoy 	*this_cpu_ptr(&idle_entry_purr_snap) = mfspr(SPRN_PURR);
15c4019198SGautham R. Shenoy }
16c4019198SGautham R. Shenoy 
snapshot_spurr_idle_entry(void)17*18b5e717SMichael Ellerman static __always_inline void snapshot_spurr_idle_entry(void)
18dc8afce5SGautham R. Shenoy {
19dc8afce5SGautham R. Shenoy 	*this_cpu_ptr(&idle_entry_spurr_snap) = mfspr(SPRN_SPURR);
20dc8afce5SGautham R. Shenoy }
21dc8afce5SGautham R. Shenoy 
update_idle_purr_accounting(void)22*18b5e717SMichael Ellerman static __always_inline void update_idle_purr_accounting(void)
23c4019198SGautham R. Shenoy {
24c4019198SGautham R. Shenoy 	u64 wait_cycles;
25c4019198SGautham R. Shenoy 	u64 in_purr = *this_cpu_ptr(&idle_entry_purr_snap);
26c4019198SGautham R. Shenoy 
27c4019198SGautham R. Shenoy 	wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
28c4019198SGautham R. Shenoy 	wait_cycles += mfspr(SPRN_PURR) - in_purr;
29c4019198SGautham R. Shenoy 	get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
30c4019198SGautham R. Shenoy }
31c4019198SGautham R. Shenoy 
update_idle_spurr_accounting(void)32*18b5e717SMichael Ellerman static __always_inline void update_idle_spurr_accounting(void)
33dc8afce5SGautham R. Shenoy {
34dc8afce5SGautham R. Shenoy 	u64 *idle_spurr_cycles_ptr = this_cpu_ptr(&idle_spurr_cycles);
35dc8afce5SGautham R. Shenoy 	u64 in_spurr = *this_cpu_ptr(&idle_entry_spurr_snap);
36dc8afce5SGautham R. Shenoy 
37dc8afce5SGautham R. Shenoy 	*idle_spurr_cycles_ptr += mfspr(SPRN_SPURR) - in_spurr;
38dc8afce5SGautham R. Shenoy }
39dc8afce5SGautham R. Shenoy 
pseries_idle_prolog(void)40*18b5e717SMichael Ellerman static __always_inline void pseries_idle_prolog(void)
41e4a884ccSGautham R. Shenoy {
42e4a884ccSGautham R. Shenoy 	ppc64_runlatch_off();
43c4019198SGautham R. Shenoy 	snapshot_purr_idle_entry();
44dc8afce5SGautham R. Shenoy 	snapshot_spurr_idle_entry();
45e4a884ccSGautham R. Shenoy 	/*
46e4a884ccSGautham R. Shenoy 	 * Indicate to the HV that we are idle. Now would be
47e4a884ccSGautham R. Shenoy 	 * a good time to find other work to dispatch.
48e4a884ccSGautham R. Shenoy 	 */
49e4a884ccSGautham R. Shenoy 	get_lppaca()->idle = 1;
50e4a884ccSGautham R. Shenoy }
51e4a884ccSGautham R. Shenoy 
pseries_idle_epilog(void)52*18b5e717SMichael Ellerman static __always_inline void pseries_idle_epilog(void)
53e4a884ccSGautham R. Shenoy {
54c4019198SGautham R. Shenoy 	update_idle_purr_accounting();
55dc8afce5SGautham R. Shenoy 	update_idle_spurr_accounting();
56e4a884ccSGautham R. Shenoy 	get_lppaca()->idle = 0;
57e4a884ccSGautham R. Shenoy 	ppc64_runlatch_on();
58e4a884ccSGautham R. Shenoy }
59c4019198SGautham R. Shenoy 
read_this_idle_purr(void)606909f179SGautham R. Shenoy static inline u64 read_this_idle_purr(void)
616909f179SGautham R. Shenoy {
626909f179SGautham R. Shenoy 	/*
636909f179SGautham R. Shenoy 	 * If we are reading from an idle context, update the
646909f179SGautham R. Shenoy 	 * idle-purr cycles corresponding to the last idle period.
656909f179SGautham R. Shenoy 	 * Since the idle context is not yet over, take a fresh
666909f179SGautham R. Shenoy 	 * snapshot of the idle-purr.
676909f179SGautham R. Shenoy 	 */
686909f179SGautham R. Shenoy 	if (unlikely(get_lppaca()->idle == 1)) {
696909f179SGautham R. Shenoy 		update_idle_purr_accounting();
706909f179SGautham R. Shenoy 		snapshot_purr_idle_entry();
716909f179SGautham R. Shenoy 	}
726909f179SGautham R. Shenoy 
736909f179SGautham R. Shenoy 	return be64_to_cpu(get_lppaca()->wait_state_cycles);
746909f179SGautham R. Shenoy }
756909f179SGautham R. Shenoy 
read_this_idle_spurr(void)766909f179SGautham R. Shenoy static inline u64 read_this_idle_spurr(void)
776909f179SGautham R. Shenoy {
786909f179SGautham R. Shenoy 	/*
796909f179SGautham R. Shenoy 	 * If we are reading from an idle context, update the
806909f179SGautham R. Shenoy 	 * idle-spurr cycles corresponding to the last idle period.
816909f179SGautham R. Shenoy 	 * Since the idle context is not yet over, take a fresh
826909f179SGautham R. Shenoy 	 * snapshot of the idle-spurr.
836909f179SGautham R. Shenoy 	 */
846909f179SGautham R. Shenoy 	if (get_lppaca()->idle == 1) {
856909f179SGautham R. Shenoy 		update_idle_spurr_accounting();
866909f179SGautham R. Shenoy 		snapshot_spurr_idle_entry();
876909f179SGautham R. Shenoy 	}
886909f179SGautham R. Shenoy 
896909f179SGautham R. Shenoy 	return *this_cpu_ptr(&idle_spurr_cycles);
906909f179SGautham R. Shenoy }
916909f179SGautham R. Shenoy 
92e4a884ccSGautham R. Shenoy #endif /* CONFIG_PPC_PSERIES */
93e4a884ccSGautham R. Shenoy #endif
94