1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_IDLE_H
3 #define _ASM_POWERPC_IDLE_H
4 #include <asm/runlatch.h>
7 #ifdef CONFIG_PPC_PSERIES
8 DECLARE_PER_CPU(u64, idle_spurr_cycles);
9 DECLARE_PER_CPU(u64, idle_entry_purr_snap);
10 DECLARE_PER_CPU(u64, idle_entry_spurr_snap);
12 static __always_inline void snapshot_purr_idle_entry(void)
14 *this_cpu_ptr(&idle_entry_purr_snap) = mfspr(SPRN_PURR);
17 static __always_inline void snapshot_spurr_idle_entry(void)
19 *this_cpu_ptr(&idle_entry_spurr_snap) = mfspr(SPRN_SPURR);
22 static __always_inline void update_idle_purr_accounting(void)
25 u64 in_purr = *this_cpu_ptr(&idle_entry_purr_snap);
27 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
28 wait_cycles += mfspr(SPRN_PURR) - in_purr;
29 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
32 static __always_inline void update_idle_spurr_accounting(void)
34 u64 *idle_spurr_cycles_ptr = this_cpu_ptr(&idle_spurr_cycles);
35 u64 in_spurr = *this_cpu_ptr(&idle_entry_spurr_snap);
37 *idle_spurr_cycles_ptr += mfspr(SPRN_SPURR) - in_spurr;
40 static __always_inline void pseries_idle_prolog(void)
43 snapshot_purr_idle_entry();
44 snapshot_spurr_idle_entry();
46 * Indicate to the HV that we are idle. Now would be
47 * a good time to find other work to dispatch.
49 get_lppaca()->idle = 1;
52 static __always_inline void pseries_idle_epilog(void)
54 update_idle_purr_accounting();
55 update_idle_spurr_accounting();
56 get_lppaca()->idle = 0;
60 static inline u64 read_this_idle_purr(void)
63 * If we are reading from an idle context, update the
64 * idle-purr cycles corresponding to the last idle period.
65 * Since the idle context is not yet over, take a fresh
66 * snapshot of the idle-purr.
68 if (unlikely(get_lppaca()->idle == 1)) {
69 update_idle_purr_accounting();
70 snapshot_purr_idle_entry();
73 return be64_to_cpu(get_lppaca()->wait_state_cycles);
76 static inline u64 read_this_idle_spurr(void)
79 * If we are reading from an idle context, update the
80 * idle-spurr cycles corresponding to the last idle period.
81 * Since the idle context is not yet over, take a fresh
82 * snapshot of the idle-spurr.
84 if (get_lppaca()->idle == 1) {
85 update_idle_spurr_accounting();
86 snapshot_spurr_idle_entry();
89 return *this_cpu_ptr(&idle_spurr_cycles);
92 #endif /* CONFIG_PPC_PSERIES */