1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Timer events oriented CPU idle governor
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2018 Intel Corporation
6*4882a593Smuzhiyun * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The idea of this governor is based on the observation that on many systems
9*4882a593Smuzhiyun * timer events are two or more orders of magnitude more frequent than any
10*4882a593Smuzhiyun * other interrupts, so they are likely to be the most significant source of CPU
11*4882a593Smuzhiyun * wakeups from idle states. Moreover, information about what happened in the
12*4882a593Smuzhiyun * (relatively recent) past can be used to estimate whether or not the deepest
13*4882a593Smuzhiyun * idle state with target residency within the time to the closest timer is
14*4882a593Smuzhiyun * likely to be suitable for the upcoming idle time of the CPU and, if not, then
15*4882a593Smuzhiyun * which of the shallower idle states to choose.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Of course, non-timer wakeup sources are more important in some use cases and
18*4882a593Smuzhiyun * they can be covered by taking a few most recent idle time intervals of the
19*4882a593Smuzhiyun * CPU into account. However, even in that case it is not necessary to consider
20*4882a593Smuzhiyun * idle duration values greater than the time till the closest timer, as the
21*4882a593Smuzhiyun * patterns that they may belong to produce average values close enough to
22*4882a593Smuzhiyun * the time till the closest timer (sleep length) anyway.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Thus this governor estimates whether or not the upcoming idle time of the CPU
25*4882a593Smuzhiyun * is likely to be significantly shorter than the sleep length and selects an
26*4882a593Smuzhiyun * idle state for it in accordance with that, as follows:
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * - Find an idle state on the basis of the sleep length and state statistics
29*4882a593Smuzhiyun * collected over time:
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * o Find the deepest idle state whose target residency is less than or equal
32*4882a593Smuzhiyun * to the sleep length.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * o Select it if it matched both the sleep length and the observed idle
35*4882a593Smuzhiyun * duration in the past more often than it matched the sleep length alone
36*4882a593Smuzhiyun * (i.e. the observed idle duration was significantly shorter than the sleep
37*4882a593Smuzhiyun * length matched by it).
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * o Otherwise, select the shallower state with the greatest matched "early"
40*4882a593Smuzhiyun * wakeups metric.
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * - If the majority of the most recent idle duration values are below the
43*4882a593Smuzhiyun * target residency of the idle state selected so far, use those values to
44*4882a593Smuzhiyun * compute the new expected idle duration and find an idle state matching it
45*4882a593Smuzhiyun * (which has to be shallower than the one selected so far).
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #include <linux/cpuidle.h>
49*4882a593Smuzhiyun #include <linux/jiffies.h>
50*4882a593Smuzhiyun #include <linux/kernel.h>
51*4882a593Smuzhiyun #include <linux/sched/clock.h>
52*4882a593Smuzhiyun #include <linux/tick.h>
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun * The PULSE value is added to metrics when they grow and the DECAY_SHIFT value
56*4882a593Smuzhiyun * is used for decreasing metrics on a regular basis.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun #define PULSE 1024
59*4882a593Smuzhiyun #define DECAY_SHIFT 3
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * Number of the most recent idle duration values to take into consideration for
63*4882a593Smuzhiyun * the detection of wakeup patterns.
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun #define INTERVALS 8
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun * struct teo_idle_state - Idle state data used by the TEO cpuidle governor.
69*4882a593Smuzhiyun * @early_hits: "Early" CPU wakeups "matching" this state.
70*4882a593Smuzhiyun * @hits: "On time" CPU wakeups "matching" this state.
71*4882a593Smuzhiyun * @misses: CPU wakeups "missing" this state.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * A CPU wakeup is "matched" by a given idle state if the idle duration measured
74*4882a593Smuzhiyun * after the wakeup is between the target residency of that state and the target
75*4882a593Smuzhiyun * residency of the next one (or if this is the deepest available idle state, it
76*4882a593Smuzhiyun * "matches" a CPU wakeup when the measured idle duration is at least equal to
77*4882a593Smuzhiyun * its target residency).
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * Also, from the TEO governor perspective, a CPU wakeup from idle is "early" if
80*4882a593Smuzhiyun * it occurs significantly earlier than the closest expected timer event (that
81*4882a593Smuzhiyun * is, early enough to match an idle state shallower than the one matching the
82*4882a593Smuzhiyun * time till the closest timer event). Otherwise, the wakeup is "on time", or
83*4882a593Smuzhiyun * it is a "hit".
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * A "miss" occurs when the given state doesn't match the wakeup, but it matches
86*4882a593Smuzhiyun * the time till the closest timer event used for idle state selection.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun struct teo_idle_state {
89*4882a593Smuzhiyun unsigned int early_hits;
90*4882a593Smuzhiyun unsigned int hits;
91*4882a593Smuzhiyun unsigned int misses;
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun * struct teo_cpu - CPU data used by the TEO cpuidle governor.
96*4882a593Smuzhiyun * @time_span_ns: Time between idle state selection and post-wakeup update.
97*4882a593Smuzhiyun * @sleep_length_ns: Time till the closest timer event (at the selection time).
98*4882a593Smuzhiyun * @states: Idle states data corresponding to this CPU.
99*4882a593Smuzhiyun * @interval_idx: Index of the most recent saved idle interval.
100*4882a593Smuzhiyun * @intervals: Saved idle duration values.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun struct teo_cpu {
103*4882a593Smuzhiyun u64 time_span_ns;
104*4882a593Smuzhiyun u64 sleep_length_ns;
105*4882a593Smuzhiyun struct teo_idle_state states[CPUIDLE_STATE_MAX];
106*4882a593Smuzhiyun int interval_idx;
107*4882a593Smuzhiyun u64 intervals[INTERVALS];
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun static DEFINE_PER_CPU(struct teo_cpu, teo_cpus);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun * teo_update - Update CPU data after wakeup.
114*4882a593Smuzhiyun * @drv: cpuidle driver containing state data.
115*4882a593Smuzhiyun * @dev: Target CPU.
116*4882a593Smuzhiyun */
teo_update(struct cpuidle_driver * drv,struct cpuidle_device * dev)117*4882a593Smuzhiyun static void teo_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
120*4882a593Smuzhiyun int i, idx_hit = -1, idx_timer = -1;
121*4882a593Smuzhiyun u64 measured_ns;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun if (cpu_data->time_span_ns >= cpu_data->sleep_length_ns) {
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun * One of the safety nets has triggered or the wakeup was close
126*4882a593Smuzhiyun * enough to the closest timer event expected at the idle state
127*4882a593Smuzhiyun * selection time to be discarded.
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun measured_ns = U64_MAX;
130*4882a593Smuzhiyun } else {
131*4882a593Smuzhiyun u64 lat_ns = drv->states[dev->last_state_idx].exit_latency_ns;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*
134*4882a593Smuzhiyun * The computations below are to determine whether or not the
135*4882a593Smuzhiyun * (saved) time till the next timer event and the measured idle
136*4882a593Smuzhiyun * duration fall into the same "bin", so use last_residency_ns
137*4882a593Smuzhiyun * for that instead of time_span_ns which includes the cpuidle
138*4882a593Smuzhiyun * overhead.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun measured_ns = dev->last_residency_ns;
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * The delay between the wakeup and the first instruction
143*4882a593Smuzhiyun * executed by the CPU is not likely to be worst-case every
144*4882a593Smuzhiyun * time, so take 1/2 of the exit latency as a very rough
145*4882a593Smuzhiyun * approximation of the average of it.
146*4882a593Smuzhiyun */
147*4882a593Smuzhiyun if (measured_ns >= lat_ns)
148*4882a593Smuzhiyun measured_ns -= lat_ns / 2;
149*4882a593Smuzhiyun else
150*4882a593Smuzhiyun measured_ns /= 2;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * Decay the "early hits" metric for all of the states and find the
155*4882a593Smuzhiyun * states matching the sleep length and the measured idle duration.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun for (i = 0; i < drv->state_count; i++) {
158*4882a593Smuzhiyun unsigned int early_hits = cpu_data->states[i].early_hits;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun cpu_data->states[i].early_hits -= early_hits >> DECAY_SHIFT;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (drv->states[i].target_residency_ns <= cpu_data->sleep_length_ns) {
163*4882a593Smuzhiyun idx_timer = i;
164*4882a593Smuzhiyun if (drv->states[i].target_residency_ns <= measured_ns)
165*4882a593Smuzhiyun idx_hit = i;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * Update the "hits" and "misses" data for the state matching the sleep
171*4882a593Smuzhiyun * length. If it matches the measured idle duration too, this is a hit,
172*4882a593Smuzhiyun * so increase the "hits" metric for it then. Otherwise, this is a
173*4882a593Smuzhiyun * miss, so increase the "misses" metric for it. In the latter case
174*4882a593Smuzhiyun * also increase the "early hits" metric for the state that actually
175*4882a593Smuzhiyun * matches the measured idle duration.
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun if (idx_timer >= 0) {
178*4882a593Smuzhiyun unsigned int hits = cpu_data->states[idx_timer].hits;
179*4882a593Smuzhiyun unsigned int misses = cpu_data->states[idx_timer].misses;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun hits -= hits >> DECAY_SHIFT;
182*4882a593Smuzhiyun misses -= misses >> DECAY_SHIFT;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun if (idx_timer > idx_hit) {
185*4882a593Smuzhiyun misses += PULSE;
186*4882a593Smuzhiyun if (idx_hit >= 0)
187*4882a593Smuzhiyun cpu_data->states[idx_hit].early_hits += PULSE;
188*4882a593Smuzhiyun } else {
189*4882a593Smuzhiyun hits += PULSE;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun cpu_data->states[idx_timer].misses = misses;
193*4882a593Smuzhiyun cpu_data->states[idx_timer].hits = hits;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /*
197*4882a593Smuzhiyun * Save idle duration values corresponding to non-timer wakeups for
198*4882a593Smuzhiyun * pattern detection.
199*4882a593Smuzhiyun */
200*4882a593Smuzhiyun cpu_data->intervals[cpu_data->interval_idx++] = measured_ns;
201*4882a593Smuzhiyun if (cpu_data->interval_idx >= INTERVALS)
202*4882a593Smuzhiyun cpu_data->interval_idx = 0;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
teo_time_ok(u64 interval_ns)205*4882a593Smuzhiyun static bool teo_time_ok(u64 interval_ns)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun return !tick_nohz_tick_stopped() || interval_ns >= TICK_NSEC;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /**
211*4882a593Smuzhiyun * teo_find_shallower_state - Find shallower idle state matching given duration.
212*4882a593Smuzhiyun * @drv: cpuidle driver containing state data.
213*4882a593Smuzhiyun * @dev: Target CPU.
214*4882a593Smuzhiyun * @state_idx: Index of the capping idle state.
215*4882a593Smuzhiyun * @duration_ns: Idle duration value to match.
216*4882a593Smuzhiyun */
teo_find_shallower_state(struct cpuidle_driver * drv,struct cpuidle_device * dev,int state_idx,u64 duration_ns)217*4882a593Smuzhiyun static int teo_find_shallower_state(struct cpuidle_driver *drv,
218*4882a593Smuzhiyun struct cpuidle_device *dev, int state_idx,
219*4882a593Smuzhiyun u64 duration_ns)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun int i;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun for (i = state_idx - 1; i >= 0; i--) {
224*4882a593Smuzhiyun if (dev->states_usage[i].disable)
225*4882a593Smuzhiyun continue;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun state_idx = i;
228*4882a593Smuzhiyun if (drv->states[i].target_residency_ns <= duration_ns)
229*4882a593Smuzhiyun break;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun return state_idx;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun * teo_select - Selects the next idle state to enter.
236*4882a593Smuzhiyun * @drv: cpuidle driver containing state data.
237*4882a593Smuzhiyun * @dev: Target CPU.
238*4882a593Smuzhiyun * @stop_tick: Indication on whether or not to stop the scheduler tick.
239*4882a593Smuzhiyun */
teo_select(struct cpuidle_driver * drv,struct cpuidle_device * dev,bool * stop_tick)240*4882a593Smuzhiyun static int teo_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
241*4882a593Smuzhiyun bool *stop_tick)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
244*4882a593Smuzhiyun s64 latency_req = cpuidle_governor_latency_req(dev->cpu);
245*4882a593Smuzhiyun u64 duration_ns;
246*4882a593Smuzhiyun unsigned int hits, misses, early_hits;
247*4882a593Smuzhiyun int max_early_idx, prev_max_early_idx, constraint_idx, idx, i;
248*4882a593Smuzhiyun ktime_t delta_tick;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun if (dev->last_state_idx >= 0) {
251*4882a593Smuzhiyun teo_update(drv, dev);
252*4882a593Smuzhiyun dev->last_state_idx = -1;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun cpu_data->time_span_ns = local_clock();
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun duration_ns = tick_nohz_get_sleep_length(&delta_tick);
258*4882a593Smuzhiyun cpu_data->sleep_length_ns = duration_ns;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun hits = 0;
261*4882a593Smuzhiyun misses = 0;
262*4882a593Smuzhiyun early_hits = 0;
263*4882a593Smuzhiyun max_early_idx = -1;
264*4882a593Smuzhiyun prev_max_early_idx = -1;
265*4882a593Smuzhiyun constraint_idx = drv->state_count;
266*4882a593Smuzhiyun idx = -1;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun for (i = 0; i < drv->state_count; i++) {
269*4882a593Smuzhiyun struct cpuidle_state *s = &drv->states[i];
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun if (dev->states_usage[i].disable) {
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun * Ignore disabled states with target residencies beyond
274*4882a593Smuzhiyun * the anticipated idle duration.
275*4882a593Smuzhiyun */
276*4882a593Smuzhiyun if (s->target_residency_ns > duration_ns)
277*4882a593Smuzhiyun continue;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * This state is disabled, so the range of idle duration
281*4882a593Smuzhiyun * values corresponding to it is covered by the current
282*4882a593Smuzhiyun * candidate state, but still the "hits" and "misses"
283*4882a593Smuzhiyun * metrics of the disabled state need to be used to
284*4882a593Smuzhiyun * decide whether or not the state covering the range in
285*4882a593Smuzhiyun * question is good enough.
286*4882a593Smuzhiyun */
287*4882a593Smuzhiyun hits = cpu_data->states[i].hits;
288*4882a593Smuzhiyun misses = cpu_data->states[i].misses;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun if (early_hits >= cpu_data->states[i].early_hits ||
291*4882a593Smuzhiyun idx < 0)
292*4882a593Smuzhiyun continue;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun * If the current candidate state has been the one with
296*4882a593Smuzhiyun * the maximum "early hits" metric so far, the "early
297*4882a593Smuzhiyun * hits" metric of the disabled state replaces the
298*4882a593Smuzhiyun * current "early hits" count to avoid selecting a
299*4882a593Smuzhiyun * deeper state with lower "early hits" metric.
300*4882a593Smuzhiyun */
301*4882a593Smuzhiyun if (max_early_idx == idx) {
302*4882a593Smuzhiyun early_hits = cpu_data->states[i].early_hits;
303*4882a593Smuzhiyun continue;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /*
307*4882a593Smuzhiyun * The current candidate state is closer to the disabled
308*4882a593Smuzhiyun * one than the current maximum "early hits" state, so
309*4882a593Smuzhiyun * replace the latter with it, but in case the maximum
310*4882a593Smuzhiyun * "early hits" state index has not been set so far,
311*4882a593Smuzhiyun * check if the current candidate state is not too
312*4882a593Smuzhiyun * shallow for that role.
313*4882a593Smuzhiyun */
314*4882a593Smuzhiyun if (teo_time_ok(drv->states[idx].target_residency_ns)) {
315*4882a593Smuzhiyun prev_max_early_idx = max_early_idx;
316*4882a593Smuzhiyun early_hits = cpu_data->states[i].early_hits;
317*4882a593Smuzhiyun max_early_idx = idx;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun continue;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (idx < 0) {
324*4882a593Smuzhiyun idx = i; /* first enabled state */
325*4882a593Smuzhiyun hits = cpu_data->states[i].hits;
326*4882a593Smuzhiyun misses = cpu_data->states[i].misses;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (s->target_residency_ns > duration_ns)
330*4882a593Smuzhiyun break;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (s->exit_latency_ns > latency_req && constraint_idx > i)
333*4882a593Smuzhiyun constraint_idx = i;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun idx = i;
336*4882a593Smuzhiyun hits = cpu_data->states[i].hits;
337*4882a593Smuzhiyun misses = cpu_data->states[i].misses;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun if (early_hits < cpu_data->states[i].early_hits &&
340*4882a593Smuzhiyun teo_time_ok(drv->states[i].target_residency_ns)) {
341*4882a593Smuzhiyun prev_max_early_idx = max_early_idx;
342*4882a593Smuzhiyun early_hits = cpu_data->states[i].early_hits;
343*4882a593Smuzhiyun max_early_idx = i;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun * If the "hits" metric of the idle state matching the sleep length is
349*4882a593Smuzhiyun * greater than its "misses" metric, that is the one to use. Otherwise,
350*4882a593Smuzhiyun * it is more likely that one of the shallower states will match the
351*4882a593Smuzhiyun * idle duration observed after wakeup, so take the one with the maximum
352*4882a593Smuzhiyun * "early hits" metric, but if that cannot be determined, just use the
353*4882a593Smuzhiyun * state selected so far.
354*4882a593Smuzhiyun */
355*4882a593Smuzhiyun if (hits <= misses) {
356*4882a593Smuzhiyun /*
357*4882a593Smuzhiyun * The current candidate state is not suitable, so take the one
358*4882a593Smuzhiyun * whose "early hits" metric is the maximum for the range of
359*4882a593Smuzhiyun * shallower states.
360*4882a593Smuzhiyun */
361*4882a593Smuzhiyun if (idx == max_early_idx)
362*4882a593Smuzhiyun max_early_idx = prev_max_early_idx;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun if (max_early_idx >= 0) {
365*4882a593Smuzhiyun idx = max_early_idx;
366*4882a593Smuzhiyun duration_ns = drv->states[idx].target_residency_ns;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun * If there is a latency constraint, it may be necessary to use a
372*4882a593Smuzhiyun * shallower idle state than the one selected so far.
373*4882a593Smuzhiyun */
374*4882a593Smuzhiyun if (constraint_idx < idx)
375*4882a593Smuzhiyun idx = constraint_idx;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if (idx < 0) {
378*4882a593Smuzhiyun idx = 0; /* No states enabled. Must use 0. */
379*4882a593Smuzhiyun } else if (idx > 0) {
380*4882a593Smuzhiyun unsigned int count = 0;
381*4882a593Smuzhiyun u64 sum = 0;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun /*
384*4882a593Smuzhiyun * Count and sum the most recent idle duration values less than
385*4882a593Smuzhiyun * the current expected idle duration value.
386*4882a593Smuzhiyun */
387*4882a593Smuzhiyun for (i = 0; i < INTERVALS; i++) {
388*4882a593Smuzhiyun u64 val = cpu_data->intervals[i];
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (val >= duration_ns)
391*4882a593Smuzhiyun continue;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun count++;
394*4882a593Smuzhiyun sum += val;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * Give up unless the majority of the most recent idle duration
399*4882a593Smuzhiyun * values are in the interesting range.
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun if (count > INTERVALS / 2) {
402*4882a593Smuzhiyun u64 avg_ns = div64_u64(sum, count);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun * Avoid spending too much time in an idle state that
406*4882a593Smuzhiyun * would be too shallow.
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun if (teo_time_ok(avg_ns)) {
409*4882a593Smuzhiyun duration_ns = avg_ns;
410*4882a593Smuzhiyun if (drv->states[idx].target_residency_ns > avg_ns)
411*4882a593Smuzhiyun idx = teo_find_shallower_state(drv, dev,
412*4882a593Smuzhiyun idx, avg_ns);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun * Don't stop the tick if the selected state is a polling one or if the
419*4882a593Smuzhiyun * expected idle duration is shorter than the tick period length.
420*4882a593Smuzhiyun */
421*4882a593Smuzhiyun if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) ||
422*4882a593Smuzhiyun duration_ns < TICK_NSEC) && !tick_nohz_tick_stopped()) {
423*4882a593Smuzhiyun *stop_tick = false;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun /*
426*4882a593Smuzhiyun * The tick is not going to be stopped, so if the target
427*4882a593Smuzhiyun * residency of the state to be returned is not within the time
428*4882a593Smuzhiyun * till the closest timer including the tick, try to correct
429*4882a593Smuzhiyun * that.
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun if (idx > 0 && drv->states[idx].target_residency_ns > delta_tick)
432*4882a593Smuzhiyun idx = teo_find_shallower_state(drv, dev, idx, delta_tick);
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun return idx;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun /**
439*4882a593Smuzhiyun * teo_reflect - Note that governor data for the CPU need to be updated.
440*4882a593Smuzhiyun * @dev: Target CPU.
441*4882a593Smuzhiyun * @state: Entered state.
442*4882a593Smuzhiyun */
teo_reflect(struct cpuidle_device * dev,int state)443*4882a593Smuzhiyun static void teo_reflect(struct cpuidle_device *dev, int state)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun dev->last_state_idx = state;
448*4882a593Smuzhiyun /*
449*4882a593Smuzhiyun * If the wakeup was not "natural", but triggered by one of the safety
450*4882a593Smuzhiyun * nets, assume that the CPU might have been idle for the entire sleep
451*4882a593Smuzhiyun * length time.
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun if (dev->poll_time_limit ||
454*4882a593Smuzhiyun (tick_nohz_idle_got_tick() && cpu_data->sleep_length_ns > TICK_NSEC)) {
455*4882a593Smuzhiyun dev->poll_time_limit = false;
456*4882a593Smuzhiyun cpu_data->time_span_ns = cpu_data->sleep_length_ns;
457*4882a593Smuzhiyun } else {
458*4882a593Smuzhiyun cpu_data->time_span_ns = local_clock() - cpu_data->time_span_ns;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /**
463*4882a593Smuzhiyun * teo_enable_device - Initialize the governor's data for the target CPU.
464*4882a593Smuzhiyun * @drv: cpuidle driver (not used).
465*4882a593Smuzhiyun * @dev: Target CPU.
466*4882a593Smuzhiyun */
teo_enable_device(struct cpuidle_driver * drv,struct cpuidle_device * dev)467*4882a593Smuzhiyun static int teo_enable_device(struct cpuidle_driver *drv,
468*4882a593Smuzhiyun struct cpuidle_device *dev)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
471*4882a593Smuzhiyun int i;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun memset(cpu_data, 0, sizeof(*cpu_data));
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun for (i = 0; i < INTERVALS; i++)
476*4882a593Smuzhiyun cpu_data->intervals[i] = U64_MAX;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun return 0;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun static struct cpuidle_governor teo_governor = {
482*4882a593Smuzhiyun .name = "teo",
483*4882a593Smuzhiyun .rating = 19,
484*4882a593Smuzhiyun .enable = teo_enable_device,
485*4882a593Smuzhiyun .select = teo_select,
486*4882a593Smuzhiyun .reflect = teo_reflect,
487*4882a593Smuzhiyun };
488*4882a593Smuzhiyun
teo_governor_init(void)489*4882a593Smuzhiyun static int __init teo_governor_init(void)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun return cpuidle_register_governor(&teo_governor);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun postcore_initcall(teo_governor_init);
495