]>
Commit | Line | Data |
---|---|---|
4f86d3a8 LB |
1 | /* |
2 | * ladder.c - the residency ladder algorithm | |
3 | * | |
4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
6 | * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de> | |
7 | * | |
8 | * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> | |
9 | * Shaohua Li <shaohua.li@intel.com> | |
10 | * Adam Belay <abelay@novell.com> | |
11 | * | |
12 | * This code is licenced under the GPL. | |
13 | */ | |
14 | ||
15 | #include <linux/kernel.h> | |
16 | #include <linux/cpuidle.h> | |
4f86d3a8 | 17 | #include <linux/jiffies.h> |
66a5f6b6 | 18 | #include <linux/tick.h> |
4f86d3a8 LB |
19 | |
20 | #include <asm/io.h> | |
7c0f6ba6 | 21 | #include <linux/uaccess.h> |
4f86d3a8 LB |
22 | |
23 | #define PROMOTION_COUNT 4 | |
24 | #define DEMOTION_COUNT 1 | |
25 | ||
26 | struct ladder_device_state { | |
27 | struct { | |
28 | u32 promotion_count; | |
29 | u32 demotion_count; | |
c1d51f68 RW |
30 | u64 promotion_time_ns; |
31 | u64 demotion_time_ns; | |
4f86d3a8 LB |
32 | } threshold; |
33 | struct { | |
34 | int promotion_count; | |
35 | int demotion_count; | |
36 | } stats; | |
37 | }; | |
38 | ||
39 | struct ladder_device { | |
40 | struct ladder_device_state states[CPUIDLE_STATE_MAX]; | |
4f86d3a8 LB |
41 | }; |
42 | ||
43 | static DEFINE_PER_CPU(struct ladder_device, ladder_devices); | |
44 | ||
45 | /** | |
46 | * ladder_do_selection - prepares private data for a state change | |
47 | * @ldev: the ladder device | |
48 | * @old_idx: the current state index | |
49 | * @new_idx: the new target state index | |
50 | */ | |
7d4daeed MT |
51 | static inline void ladder_do_selection(struct cpuidle_device *dev, |
52 | struct ladder_device *ldev, | |
4f86d3a8 LB |
53 | int old_idx, int new_idx) |
54 | { | |
55 | ldev->states[old_idx].stats.promotion_count = 0; | |
56 | ldev->states[old_idx].stats.demotion_count = 0; | |
7d4daeed | 57 | dev->last_state_idx = new_idx; |
4f86d3a8 LB |
58 | } |
59 | ||
60 | /** | |
61 | * ladder_select_state - selects the next state to enter | |
46bcfad7 | 62 | * @drv: cpuidle driver |
4f86d3a8 | 63 | * @dev: the CPU |
45f1ff59 | 64 | * @dummy: not used |
4f86d3a8 | 65 | */ |
46bcfad7 | 66 | static int ladder_select_state(struct cpuidle_driver *drv, |
45f1ff59 | 67 | struct cpuidle_device *dev, bool *dummy) |
4f86d3a8 | 68 | { |
229b6863 | 69 | struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); |
4f86d3a8 | 70 | struct ladder_device_state *last_state; |
c1d51f68 | 71 | int last_idx = dev->last_state_idx; |
dc2251bf | 72 | int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; |
c1d51f68 RW |
73 | s64 latency_req = cpuidle_governor_latency_req(dev->cpu); |
74 | s64 last_residency; | |
4f86d3a8 | 75 | |
a2bd9202 | 76 | /* Special case when user has set very strict latency requirement */ |
77 | if (unlikely(latency_req == 0)) { | |
7d4daeed | 78 | ladder_do_selection(dev, ldev, last_idx, 0); |
a2bd9202 | 79 | return 0; |
80 | } | |
81 | ||
4f86d3a8 LB |
82 | last_state = &ldev->states[last_idx]; |
83 | ||
c1d51f68 | 84 | last_residency = dev->last_residency_ns - drv->states[last_idx].exit_latency_ns; |
4f86d3a8 LB |
85 | |
86 | /* consider promotion */ | |
46bcfad7 | 87 | if (last_idx < drv->state_count - 1 && |
62d6ae88 | 88 | !dev->states_usage[last_idx + 1].disable && |
c1d51f68 RW |
89 | last_residency > last_state->threshold.promotion_time_ns && |
90 | drv->states[last_idx + 1].exit_latency_ns <= latency_req) { | |
4f86d3a8 LB |
91 | last_state->stats.promotion_count++; |
92 | last_state->stats.demotion_count = 0; | |
93 | if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { | |
7d4daeed | 94 | ladder_do_selection(dev, ldev, last_idx, last_idx + 1); |
4f86d3a8 LB |
95 | return last_idx + 1; |
96 | } | |
97 | } | |
98 | ||
99 | /* consider demotion */ | |
dc2251bf | 100 | if (last_idx > first_idx && |
99e98d3f | 101 | (dev->states_usage[last_idx].disable || |
c1d51f68 | 102 | drv->states[last_idx].exit_latency_ns > latency_req)) { |
06d9e908 | 103 | int i; |
104 | ||
dc2251bf | 105 | for (i = last_idx - 1; i > first_idx; i--) { |
c1d51f68 | 106 | if (drv->states[i].exit_latency_ns <= latency_req) |
06d9e908 | 107 | break; |
108 | } | |
7d4daeed | 109 | ladder_do_selection(dev, ldev, last_idx, i); |
06d9e908 | 110 | return i; |
111 | } | |
112 | ||
dc2251bf | 113 | if (last_idx > first_idx && |
c1d51f68 | 114 | last_residency < last_state->threshold.demotion_time_ns) { |
4f86d3a8 LB |
115 | last_state->stats.demotion_count++; |
116 | last_state->stats.promotion_count = 0; | |
117 | if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) { | |
7d4daeed | 118 | ladder_do_selection(dev, ldev, last_idx, last_idx - 1); |
4f86d3a8 LB |
119 | return last_idx - 1; |
120 | } | |
121 | } | |
122 | ||
123 | /* otherwise remain at the current state */ | |
124 | return last_idx; | |
125 | } | |
126 | ||
127 | /** | |
128 | * ladder_enable_device - setup for the governor | |
46bcfad7 | 129 | * @drv: cpuidle driver |
4f86d3a8 LB |
130 | * @dev: the CPU |
131 | */ | |
46bcfad7 DD |
132 | static int ladder_enable_device(struct cpuidle_driver *drv, |
133 | struct cpuidle_device *dev) | |
4f86d3a8 LB |
134 | { |
135 | int i; | |
dc2251bf | 136 | int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; |
4f86d3a8 LB |
137 | struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); |
138 | struct ladder_device_state *lstate; | |
139 | struct cpuidle_state *state; | |
140 | ||
7d4daeed | 141 | dev->last_state_idx = first_idx; |
4f86d3a8 | 142 | |
dc2251bf | 143 | for (i = first_idx; i < drv->state_count; i++) { |
46bcfad7 | 144 | state = &drv->states[i]; |
4f86d3a8 LB |
145 | lstate = &ldev->states[i]; |
146 | ||
147 | lstate->stats.promotion_count = 0; | |
148 | lstate->stats.demotion_count = 0; | |
149 | ||
150 | lstate->threshold.promotion_count = PROMOTION_COUNT; | |
151 | lstate->threshold.demotion_count = DEMOTION_COUNT; | |
152 | ||
46bcfad7 | 153 | if (i < drv->state_count - 1) |
c1d51f68 | 154 | lstate->threshold.promotion_time_ns = state->exit_latency_ns; |
dc2251bf | 155 | if (i > first_idx) |
c1d51f68 | 156 | lstate->threshold.demotion_time_ns = state->exit_latency_ns; |
4f86d3a8 LB |
157 | } |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
e978aa7d DD |
162 | /** |
163 | * ladder_reflect - update the correct last_state_idx | |
164 | * @dev: the CPU | |
165 | * @index: the index of actual state entered | |
166 | */ | |
167 | static void ladder_reflect(struct cpuidle_device *dev, int index) | |
168 | { | |
e978aa7d | 169 | if (index > 0) |
7d4daeed | 170 | dev->last_state_idx = index; |
e978aa7d DD |
171 | } |
172 | ||
4f86d3a8 LB |
173 | static struct cpuidle_governor ladder_governor = { |
174 | .name = "ladder", | |
175 | .rating = 10, | |
176 | .enable = ladder_enable_device, | |
177 | .select = ladder_select_state, | |
e978aa7d | 178 | .reflect = ladder_reflect, |
4f86d3a8 LB |
179 | }; |
180 | ||
181 | /** | |
182 | * init_ladder - initializes the governor | |
183 | */ | |
184 | static int __init init_ladder(void) | |
185 | { | |
66a5f6b6 JD |
186 | /* |
187 | * When NO_HZ is disabled, or when booting with nohz=off, the ladder | |
188 | * governor is better so give it a higher rating than the menu | |
189 | * governor. | |
190 | */ | |
191 | if (!tick_nohz_enabled) | |
192 | ladder_governor.rating = 25; | |
193 | ||
4f86d3a8 LB |
194 | return cpuidle_register_governor(&ladder_governor); |
195 | } | |
196 | ||
137b944e | 197 | postcore_initcall(init_ladder); |