2 * ladder.c - the residency ladder algorithm
12 * This code is licenced under the GPL.
15 #include <linux/kernel.h>
16 #include <linux/cpuidle.h>
17 #include <linux/pm_qos.h>
18 #include <linux/jiffies.h>
19 #include <linux/tick.h>
20 #include <linux/cpu.h>
23 #include <linux/uaccess.h>
25 #define PROMOTION_COUNT 4
26 #define DEMOTION_COUNT 1
28 struct ladder_device_state {
41 struct ladder_device {
42 struct ladder_device_state states[CPUIDLE_STATE_MAX];
46 static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
49 * ladder_do_selection - prepares private data for a state change
50 * @ldev: the ladder device
51 * @old_idx: the current state index
52 * @new_idx: the new target state index
54 static inline void ladder_do_selection(struct ladder_device *ldev,
55 int old_idx, int new_idx)
57 ldev->states[old_idx].stats.promotion_count = 0;
58 ldev->states[old_idx].stats.demotion_count = 0;
59 ldev->last_state_idx = new_idx;
63 * ladder_select_state - selects the next state to enter
64 * @drv: cpuidle driver
67 static int ladder_select_state(struct cpuidle_driver *drv,
68 struct cpuidle_device *dev)
70 struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
71 struct device *device = get_cpu_device(dev->cpu);
72 struct ladder_device_state *last_state;
73 int last_residency, last_idx = ldev->last_state_idx;
74 int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
75 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
76 int resume_latency = dev_pm_qos_raw_read_value(device);
78 if (resume_latency < latency_req &&
79 resume_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
80 latency_req = resume_latency;
82 /* Special case when user has set very strict latency requirement */
83 if (unlikely(latency_req == 0)) {
84 ladder_do_selection(ldev, last_idx, 0);
88 last_state = &ldev->states[last_idx];
90 last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency;
92 /* consider promotion */
93 if (last_idx < drv->state_count - 1 &&
94 !drv->states[last_idx + 1].disabled &&
95 !dev->states_usage[last_idx + 1].disable &&
96 last_residency > last_state->threshold.promotion_time &&
97 drv->states[last_idx + 1].exit_latency <= latency_req) {
98 last_state->stats.promotion_count++;
99 last_state->stats.demotion_count = 0;
100 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
101 ladder_do_selection(ldev, last_idx, last_idx + 1);
106 /* consider demotion */
107 if (last_idx > first_idx &&
108 (drv->states[last_idx].disabled ||
109 dev->states_usage[last_idx].disable ||
110 drv->states[last_idx].exit_latency > latency_req)) {
113 for (i = last_idx - 1; i > first_idx; i--) {
114 if (drv->states[i].exit_latency <= latency_req)
117 ladder_do_selection(ldev, last_idx, i);
121 if (last_idx > first_idx &&
122 last_residency < last_state->threshold.demotion_time) {
123 last_state->stats.demotion_count++;
124 last_state->stats.promotion_count = 0;
125 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
126 ladder_do_selection(ldev, last_idx, last_idx - 1);
131 /* otherwise remain at the current state */
136 * ladder_enable_device - setup for the governor
137 * @drv: cpuidle driver
140 static int ladder_enable_device(struct cpuidle_driver *drv,
141 struct cpuidle_device *dev)
144 int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
145 struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
146 struct ladder_device_state *lstate;
147 struct cpuidle_state *state;
149 ldev->last_state_idx = first_idx;
151 for (i = first_idx; i < drv->state_count; i++) {
152 state = &drv->states[i];
153 lstate = &ldev->states[i];
155 lstate->stats.promotion_count = 0;
156 lstate->stats.demotion_count = 0;
158 lstate->threshold.promotion_count = PROMOTION_COUNT;
159 lstate->threshold.demotion_count = DEMOTION_COUNT;
161 if (i < drv->state_count - 1)
162 lstate->threshold.promotion_time = state->exit_latency;
164 lstate->threshold.demotion_time = state->exit_latency;
171 * ladder_reflect - update the correct last_state_idx
173 * @index: the index of actual state entered
175 static void ladder_reflect(struct cpuidle_device *dev, int index)
177 struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
179 ldev->last_state_idx = index;
182 static struct cpuidle_governor ladder_governor = {
185 .enable = ladder_enable_device,
186 .select = ladder_select_state,
187 .reflect = ladder_reflect,
191 * init_ladder - initializes the governor
193 static int __init init_ladder(void)
196 * When NO_HZ is disabled, or when booting with nohz=off, the ladder
197 * governor is better so give it a higher rating than the menu
200 if (!tick_nohz_enabled)
201 ladder_governor.rating = 25;
203 return cpuidle_register_governor(&ladder_governor);
206 postcore_initcall(init_ladder);