xref: /openbmc/linux/arch/powerpc/include/asm/idle.h (revision 82df5b73)
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>
5 #include <asm/paca.h>
6 
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);
11 
12 static inline void snapshot_purr_idle_entry(void)
13 {
14 	*this_cpu_ptr(&idle_entry_purr_snap) = mfspr(SPRN_PURR);
15 }
16 
17 static inline void snapshot_spurr_idle_entry(void)
18 {
19 	*this_cpu_ptr(&idle_entry_spurr_snap) = mfspr(SPRN_SPURR);
20 }
21 
22 static inline void update_idle_purr_accounting(void)
23 {
24 	u64 wait_cycles;
25 	u64 in_purr = *this_cpu_ptr(&idle_entry_purr_snap);
26 
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);
30 }
31 
32 static inline void update_idle_spurr_accounting(void)
33 {
34 	u64 *idle_spurr_cycles_ptr = this_cpu_ptr(&idle_spurr_cycles);
35 	u64 in_spurr = *this_cpu_ptr(&idle_entry_spurr_snap);
36 
37 	*idle_spurr_cycles_ptr += mfspr(SPRN_SPURR) - in_spurr;
38 }
39 
40 static inline void pseries_idle_prolog(void)
41 {
42 	ppc64_runlatch_off();
43 	snapshot_purr_idle_entry();
44 	snapshot_spurr_idle_entry();
45 	/*
46 	 * Indicate to the HV that we are idle. Now would be
47 	 * a good time to find other work to dispatch.
48 	 */
49 	get_lppaca()->idle = 1;
50 }
51 
52 static inline void pseries_idle_epilog(void)
53 {
54 	update_idle_purr_accounting();
55 	update_idle_spurr_accounting();
56 	get_lppaca()->idle = 0;
57 	ppc64_runlatch_on();
58 }
59 
60 static inline u64 read_this_idle_purr(void)
61 {
62 	/*
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.
67 	 */
68 	if (unlikely(get_lppaca()->idle == 1)) {
69 		update_idle_purr_accounting();
70 		snapshot_purr_idle_entry();
71 	}
72 
73 	return be64_to_cpu(get_lppaca()->wait_state_cycles);
74 }
75 
76 static inline u64 read_this_idle_spurr(void)
77 {
78 	/*
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.
83 	 */
84 	if (get_lppaca()->idle == 1) {
85 		update_idle_spurr_accounting();
86 		snapshot_spurr_idle_entry();
87 	}
88 
89 	return *this_cpu_ptr(&idle_spurr_cycles);
90 }
91 
92 #endif /* CONFIG_PPC_PSERIES */
93 #endif
94