1
2
3
4
5
6
7
8
9
10
11
12
13
14
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>
21
22#include <asm/io.h>
23#include <linux/uaccess.h>
24
25#define PROMOTION_COUNT 4
26#define DEMOTION_COUNT 1
27
28struct ladder_device_state {
29 struct {
30 u32 promotion_count;
31 u32 demotion_count;
32 u32 promotion_time;
33 u32 demotion_time;
34 } threshold;
35 struct {
36 int promotion_count;
37 int demotion_count;
38 } stats;
39};
40
41struct ladder_device {
42 struct ladder_device_state states[CPUIDLE_STATE_MAX];
43 int last_state_idx;
44};
45
46static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
47
48
49
50
51
52
53
54static inline void ladder_do_selection(struct ladder_device *ldev,
55 int old_idx, int new_idx)
56{
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;
60}
61
62
63
64
65
66
67
68static int ladder_select_state(struct cpuidle_driver *drv,
69 struct cpuidle_device *dev, bool *dummy)
70{
71 struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
72 struct device *device = get_cpu_device(dev->cpu);
73 struct ladder_device_state *last_state;
74 int last_residency, last_idx = ldev->last_state_idx;
75 int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
76 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
77 int resume_latency = dev_pm_qos_raw_read_value(device);
78
79 if (resume_latency < latency_req &&
80 resume_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
81 latency_req = resume_latency;
82
83
84 if (unlikely(latency_req == 0)) {
85 ladder_do_selection(ldev, last_idx, 0);
86 return 0;
87 }
88
89 last_state = &ldev->states[last_idx];
90
91 last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency;
92
93
94 if (last_idx < drv->state_count - 1 &&
95 !drv->states[last_idx + 1].disabled &&
96 !dev->states_usage[last_idx + 1].disable &&
97 last_residency > last_state->threshold.promotion_time &&
98 drv->states[last_idx + 1].exit_latency <= latency_req) {
99 last_state->stats.promotion_count++;
100 last_state->stats.demotion_count = 0;
101 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
102 ladder_do_selection(ldev, last_idx, last_idx + 1);
103 return last_idx + 1;
104 }
105 }
106
107
108 if (last_idx > first_idx &&
109 (drv->states[last_idx].disabled ||
110 dev->states_usage[last_idx].disable ||
111 drv->states[last_idx].exit_latency > latency_req)) {
112 int i;
113
114 for (i = last_idx - 1; i > first_idx; i--) {
115 if (drv->states[i].exit_latency <= latency_req)
116 break;
117 }
118 ladder_do_selection(ldev, last_idx, i);
119 return i;
120 }
121
122 if (last_idx > first_idx &&
123 last_residency < last_state->threshold.demotion_time) {
124 last_state->stats.demotion_count++;
125 last_state->stats.promotion_count = 0;
126 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
127 ladder_do_selection(ldev, last_idx, last_idx - 1);
128 return last_idx - 1;
129 }
130 }
131
132
133 return last_idx;
134}
135
136
137
138
139
140
141static int ladder_enable_device(struct cpuidle_driver *drv,
142 struct cpuidle_device *dev)
143{
144 int i;
145 int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
146 struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
147 struct ladder_device_state *lstate;
148 struct cpuidle_state *state;
149
150 ldev->last_state_idx = first_idx;
151
152 for (i = first_idx; i < drv->state_count; i++) {
153 state = &drv->states[i];
154 lstate = &ldev->states[i];
155
156 lstate->stats.promotion_count = 0;
157 lstate->stats.demotion_count = 0;
158
159 lstate->threshold.promotion_count = PROMOTION_COUNT;
160 lstate->threshold.demotion_count = DEMOTION_COUNT;
161
162 if (i < drv->state_count - 1)
163 lstate->threshold.promotion_time = state->exit_latency;
164 if (i > first_idx)
165 lstate->threshold.demotion_time = state->exit_latency;
166 }
167
168 return 0;
169}
170
171
172
173
174
175
176static void ladder_reflect(struct cpuidle_device *dev, int index)
177{
178 struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
179 if (index > 0)
180 ldev->last_state_idx = index;
181}
182
183static struct cpuidle_governor ladder_governor = {
184 .name = "ladder",
185 .rating = 10,
186 .enable = ladder_enable_device,
187 .select = ladder_select_state,
188 .reflect = ladder_reflect,
189};
190
191
192
193
194static int __init init_ladder(void)
195{
196
197
198
199
200
201 if (!tick_nohz_enabled)
202 ladder_governor.rating = 25;
203
204 return cpuidle_register_governor(&ladder_governor);
205}
206
207postcore_initcall(init_ladder);
208