Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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