Skip to content

Commit a6220fc

Browse files
committed
PM / suspend: Always use deepest C-state in the "freeze" sleep state
If freeze_enter() is called, we want to bypass the current cpuidle governor and always use the deepest available (that is, not disabled) C-state, because we want to save as much energy as reasonably possible then and runtime latency constraints don't matter at that point, since the system is in a sleep state anyway. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Tested-by: Aubrey Li <aubrey.li@linux.intel.com>
1 parent bed4d59 commit a6220fc

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

drivers/cpuidle/cpuidle.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LIST_HEAD(cpuidle_detected_devices);
3232
static int enabled_devices;
3333
static int off __read_mostly;
3434
static int initialized __read_mostly;
35+
static bool use_deepest_state __read_mostly;
3536

3637
int cpuidle_disabled(void)
3738
{
@@ -64,6 +65,45 @@ int cpuidle_play_dead(void)
6465
return -ENODEV;
6566
}
6667

68+
/**
69+
* cpuidle_use_deepest_state - Enable/disable the "deepest idle" mode.
70+
* @enable: Whether enable or disable the feature.
71+
*
72+
* If the "deepest idle" mode is enabled, cpuidle will ignore the governor and
73+
* always use the state with the greatest exit latency (out of the states that
74+
* are not disabled).
75+
*
76+
* This function can only be called after cpuidle_pause() to avoid races.
77+
*/
78+
void cpuidle_use_deepest_state(bool enable)
79+
{
80+
use_deepest_state = enable;
81+
}
82+
83+
/**
84+
* cpuidle_find_deepest_state - Find the state of the greatest exit latency.
85+
* @drv: cpuidle driver for a given CPU.
86+
* @dev: cpuidle device for a given CPU.
87+
*/
88+
static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
89+
struct cpuidle_device *dev)
90+
{
91+
unsigned int latency_req = 0;
92+
int i, ret = CPUIDLE_DRIVER_STATE_START - 1;
93+
94+
for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
95+
struct cpuidle_state *s = &drv->states[i];
96+
struct cpuidle_state_usage *su = &dev->states_usage[i];
97+
98+
if (s->disabled || su->disable || s->exit_latency <= latency_req)
99+
continue;
100+
101+
latency_req = s->exit_latency;
102+
ret = i;
103+
}
104+
return ret;
105+
}
106+
67107
/**
68108
* cpuidle_enter_state - enter the state and update stats
69109
* @dev: cpuidle device for this cpu
@@ -124,6 +164,9 @@ int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
124164
if (!drv || !dev || !dev->enabled)
125165
return -EBUSY;
126166

167+
if (unlikely(use_deepest_state))
168+
return cpuidle_find_deepest_state(drv, dev);
169+
127170
return cpuidle_curr_governor->select(drv, dev);
128171
}
129172

@@ -155,7 +198,7 @@ int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
155198
*/
156199
void cpuidle_reflect(struct cpuidle_device *dev, int index)
157200
{
158-
if (cpuidle_curr_governor->reflect)
201+
if (cpuidle_curr_governor->reflect && !unlikely(use_deepest_state))
159202
cpuidle_curr_governor->reflect(dev, index);
160203
}
161204

include/linux/cpuidle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ extern void cpuidle_resume(void);
143143
extern int cpuidle_enable_device(struct cpuidle_device *dev);
144144
extern void cpuidle_disable_device(struct cpuidle_device *dev);
145145
extern int cpuidle_play_dead(void);
146+
extern void cpuidle_use_deepest_state(bool enable);
146147

147148
extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
148149
#else
@@ -175,6 +176,7 @@ static inline int cpuidle_enable_device(struct cpuidle_device *dev)
175176
{return -ENODEV; }
176177
static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
177178
static inline int cpuidle_play_dead(void) {return -ENODEV; }
179+
static inline void cpuidle_use_deepest_state(bool enable) {}
178180
static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
179181
struct cpuidle_device *dev) {return NULL; }
180182
#endif

kernel/power/suspend.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ static void freeze_begin(void)
5454

5555
static void freeze_enter(void)
5656
{
57+
cpuidle_use_deepest_state(true);
5758
cpuidle_resume();
5859
wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
5960
cpuidle_pause();
61+
cpuidle_use_deepest_state(false);
6062
}
6163

6264
void freeze_wake(void)

0 commit comments

Comments
 (0)