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