18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * menu.c - the menu idle governor
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2009 Intel Corporation
78c2ecf20Sopenharmony_ci * Author:
88c2ecf20Sopenharmony_ci *        Arjan van de Ven <arjan@linux.intel.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/cpuidle.h>
138c2ecf20Sopenharmony_ci#include <linux/time.h>
148c2ecf20Sopenharmony_ci#include <linux/ktime.h>
158c2ecf20Sopenharmony_ci#include <linux/hrtimer.h>
168c2ecf20Sopenharmony_ci#include <linux/tick.h>
178c2ecf20Sopenharmony_ci#include <linux/sched.h>
188c2ecf20Sopenharmony_ci#include <linux/sched/loadavg.h>
198c2ecf20Sopenharmony_ci#include <linux/sched/stat.h>
208c2ecf20Sopenharmony_ci#include <linux/math64.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define BUCKETS 12
238c2ecf20Sopenharmony_ci#define INTERVAL_SHIFT 3
248c2ecf20Sopenharmony_ci#define INTERVALS (1UL << INTERVAL_SHIFT)
258c2ecf20Sopenharmony_ci#define RESOLUTION 1024
268c2ecf20Sopenharmony_ci#define DECAY 8
278c2ecf20Sopenharmony_ci#define MAX_INTERESTING (50000 * NSEC_PER_USEC)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Concepts and ideas behind the menu governor
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * For the menu governor, there are 3 decision factors for picking a C
338c2ecf20Sopenharmony_ci * state:
348c2ecf20Sopenharmony_ci * 1) Energy break even point
358c2ecf20Sopenharmony_ci * 2) Performance impact
368c2ecf20Sopenharmony_ci * 3) Latency tolerance (from pmqos infrastructure)
378c2ecf20Sopenharmony_ci * These these three factors are treated independently.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * Energy break even point
408c2ecf20Sopenharmony_ci * -----------------------
418c2ecf20Sopenharmony_ci * C state entry and exit have an energy cost, and a certain amount of time in
428c2ecf20Sopenharmony_ci * the  C state is required to actually break even on this cost. CPUIDLE
438c2ecf20Sopenharmony_ci * provides us this duration in the "target_residency" field. So all that we
448c2ecf20Sopenharmony_ci * need is a good prediction of how long we'll be idle. Like the traditional
458c2ecf20Sopenharmony_ci * menu governor, we start with the actual known "next timer event" time.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * Since there are other source of wakeups (interrupts for example) than
488c2ecf20Sopenharmony_ci * the next timer event, this estimation is rather optimistic. To get a
498c2ecf20Sopenharmony_ci * more realistic estimate, a correction factor is applied to the estimate,
508c2ecf20Sopenharmony_ci * that is based on historic behavior. For example, if in the past the actual
518c2ecf20Sopenharmony_ci * duration always was 50% of the next timer tick, the correction factor will
528c2ecf20Sopenharmony_ci * be 0.5.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * menu uses a running average for this correction factor, however it uses a
558c2ecf20Sopenharmony_ci * set of factors, not just a single factor. This stems from the realization
568c2ecf20Sopenharmony_ci * that the ratio is dependent on the order of magnitude of the expected
578c2ecf20Sopenharmony_ci * duration; if we expect 500 milliseconds of idle time the likelihood of
588c2ecf20Sopenharmony_ci * getting an interrupt very early is much higher than if we expect 50 micro
598c2ecf20Sopenharmony_ci * seconds of idle time. A second independent factor that has big impact on
608c2ecf20Sopenharmony_ci * the actual factor is if there is (disk) IO outstanding or not.
618c2ecf20Sopenharmony_ci * (as a special twist, we consider every sleep longer than 50 milliseconds
628c2ecf20Sopenharmony_ci * as perfect; there are no power gains for sleeping longer than this)
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * For these two reasons we keep an array of 12 independent factors, that gets
658c2ecf20Sopenharmony_ci * indexed based on the magnitude of the expected duration as well as the
668c2ecf20Sopenharmony_ci * "is IO outstanding" property.
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * Repeatable-interval-detector
698c2ecf20Sopenharmony_ci * ----------------------------
708c2ecf20Sopenharmony_ci * There are some cases where "next timer" is a completely unusable predictor:
718c2ecf20Sopenharmony_ci * Those cases where the interval is fixed, for example due to hardware
728c2ecf20Sopenharmony_ci * interrupt mitigation, but also due to fixed transfer rate devices such as
738c2ecf20Sopenharmony_ci * mice.
748c2ecf20Sopenharmony_ci * For this, we use a different predictor: We track the duration of the last 8
758c2ecf20Sopenharmony_ci * intervals and if the stand deviation of these 8 intervals is below a
768c2ecf20Sopenharmony_ci * threshold value, we use the average of these intervals as prediction.
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * Limiting Performance Impact
798c2ecf20Sopenharmony_ci * ---------------------------
808c2ecf20Sopenharmony_ci * C states, especially those with large exit latencies, can have a real
818c2ecf20Sopenharmony_ci * noticeable impact on workloads, which is not acceptable for most sysadmins,
828c2ecf20Sopenharmony_ci * and in addition, less performance has a power price of its own.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * As a general rule of thumb, menu assumes that the following heuristic
858c2ecf20Sopenharmony_ci * holds:
868c2ecf20Sopenharmony_ci *     The busier the system, the less impact of C states is acceptable
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * This rule-of-thumb is implemented using a performance-multiplier:
898c2ecf20Sopenharmony_ci * If the exit latency times the performance multiplier is longer than
908c2ecf20Sopenharmony_ci * the predicted duration, the C state is not considered a candidate
918c2ecf20Sopenharmony_ci * for selection due to a too high performance impact. So the higher
928c2ecf20Sopenharmony_ci * this multiplier is, the longer we need to be idle to pick a deep C
938c2ecf20Sopenharmony_ci * state, and thus the less likely a busy CPU will hit such a deep
948c2ecf20Sopenharmony_ci * C state.
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci * Two factors are used in determing this multiplier:
978c2ecf20Sopenharmony_ci * a value of 10 is added for each point of "per cpu load average" we have.
988c2ecf20Sopenharmony_ci * a value of 5 points is added for each process that is waiting for
998c2ecf20Sopenharmony_ci * IO on this CPU.
1008c2ecf20Sopenharmony_ci * (these values are experimentally determined)
1018c2ecf20Sopenharmony_ci *
1028c2ecf20Sopenharmony_ci * The load average factor gives a longer term (few seconds) input to the
1038c2ecf20Sopenharmony_ci * decision, while the iowait value gives a cpu local instantanious input.
1048c2ecf20Sopenharmony_ci * The iowait factor may look low, but realize that this is also already
1058c2ecf20Sopenharmony_ci * represented in the system load average.
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistruct menu_device {
1108c2ecf20Sopenharmony_ci	int             needs_update;
1118c2ecf20Sopenharmony_ci	int             tick_wakeup;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	u64		next_timer_ns;
1148c2ecf20Sopenharmony_ci	unsigned int	bucket;
1158c2ecf20Sopenharmony_ci	unsigned int	correction_factor[BUCKETS];
1168c2ecf20Sopenharmony_ci	unsigned int	intervals[INTERVALS];
1178c2ecf20Sopenharmony_ci	int		interval_ptr;
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline int which_bucket(u64 duration_ns, unsigned long nr_iowaiters)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	int bucket = 0;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/*
1258c2ecf20Sopenharmony_ci	 * We keep two groups of stats; one with no
1268c2ecf20Sopenharmony_ci	 * IO pending, one without.
1278c2ecf20Sopenharmony_ci	 * This allows us to calculate
1288c2ecf20Sopenharmony_ci	 * E(duration)|iowait
1298c2ecf20Sopenharmony_ci	 */
1308c2ecf20Sopenharmony_ci	if (nr_iowaiters)
1318c2ecf20Sopenharmony_ci		bucket = BUCKETS/2;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (duration_ns < 10ULL * NSEC_PER_USEC)
1348c2ecf20Sopenharmony_ci		return bucket;
1358c2ecf20Sopenharmony_ci	if (duration_ns < 100ULL * NSEC_PER_USEC)
1368c2ecf20Sopenharmony_ci		return bucket + 1;
1378c2ecf20Sopenharmony_ci	if (duration_ns < 1000ULL * NSEC_PER_USEC)
1388c2ecf20Sopenharmony_ci		return bucket + 2;
1398c2ecf20Sopenharmony_ci	if (duration_ns < 10000ULL * NSEC_PER_USEC)
1408c2ecf20Sopenharmony_ci		return bucket + 3;
1418c2ecf20Sopenharmony_ci	if (duration_ns < 100000ULL * NSEC_PER_USEC)
1428c2ecf20Sopenharmony_ci		return bucket + 4;
1438c2ecf20Sopenharmony_ci	return bucket + 5;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/*
1478c2ecf20Sopenharmony_ci * Return a multiplier for the exit latency that is intended
1488c2ecf20Sopenharmony_ci * to take performance requirements into account.
1498c2ecf20Sopenharmony_ci * The more performance critical we estimate the system
1508c2ecf20Sopenharmony_ci * to be, the higher this multiplier, and thus the higher
1518c2ecf20Sopenharmony_ci * the barrier to go to an expensive C state.
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_cistatic inline int performance_multiplier(unsigned long nr_iowaiters)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	/* for IO wait tasks (per cpu!) we add 10x each */
1568c2ecf20Sopenharmony_ci	return 1 + 10 * nr_iowaiters;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct menu_device, menu_devices);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * Try detecting repeating patterns by keeping track of the last 8
1658c2ecf20Sopenharmony_ci * intervals, and checking if the standard deviation of that set
1668c2ecf20Sopenharmony_ci * of points is below a threshold. If it is... then use the
1678c2ecf20Sopenharmony_ci * average of these 8 points as the estimated value.
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_cistatic unsigned int get_typical_interval(struct menu_device *data,
1708c2ecf20Sopenharmony_ci					 unsigned int predicted_us)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	int i, divisor;
1738c2ecf20Sopenharmony_ci	unsigned int min, max, thresh, avg;
1748c2ecf20Sopenharmony_ci	uint64_t sum, variance;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	thresh = INT_MAX; /* Discard outliers above this value */
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ciagain:
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	/* First calculate the average of past intervals */
1818c2ecf20Sopenharmony_ci	min = UINT_MAX;
1828c2ecf20Sopenharmony_ci	max = 0;
1838c2ecf20Sopenharmony_ci	sum = 0;
1848c2ecf20Sopenharmony_ci	divisor = 0;
1858c2ecf20Sopenharmony_ci	for (i = 0; i < INTERVALS; i++) {
1868c2ecf20Sopenharmony_ci		unsigned int value = data->intervals[i];
1878c2ecf20Sopenharmony_ci		if (value <= thresh) {
1888c2ecf20Sopenharmony_ci			sum += value;
1898c2ecf20Sopenharmony_ci			divisor++;
1908c2ecf20Sopenharmony_ci			if (value > max)
1918c2ecf20Sopenharmony_ci				max = value;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci			if (value < min)
1948c2ecf20Sopenharmony_ci				min = value;
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/*
1998c2ecf20Sopenharmony_ci	 * If the result of the computation is going to be discarded anyway,
2008c2ecf20Sopenharmony_ci	 * avoid the computation altogether.
2018c2ecf20Sopenharmony_ci	 */
2028c2ecf20Sopenharmony_ci	if (min >= predicted_us)
2038c2ecf20Sopenharmony_ci		return UINT_MAX;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (divisor == INTERVALS)
2068c2ecf20Sopenharmony_ci		avg = sum >> INTERVAL_SHIFT;
2078c2ecf20Sopenharmony_ci	else
2088c2ecf20Sopenharmony_ci		avg = div_u64(sum, divisor);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Then try to determine variance */
2118c2ecf20Sopenharmony_ci	variance = 0;
2128c2ecf20Sopenharmony_ci	for (i = 0; i < INTERVALS; i++) {
2138c2ecf20Sopenharmony_ci		unsigned int value = data->intervals[i];
2148c2ecf20Sopenharmony_ci		if (value <= thresh) {
2158c2ecf20Sopenharmony_ci			int64_t diff = (int64_t)value - avg;
2168c2ecf20Sopenharmony_ci			variance += diff * diff;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci	if (divisor == INTERVALS)
2208c2ecf20Sopenharmony_ci		variance >>= INTERVAL_SHIFT;
2218c2ecf20Sopenharmony_ci	else
2228c2ecf20Sopenharmony_ci		do_div(variance, divisor);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/*
2258c2ecf20Sopenharmony_ci	 * The typical interval is obtained when standard deviation is
2268c2ecf20Sopenharmony_ci	 * small (stddev <= 20 us, variance <= 400 us^2) or standard
2278c2ecf20Sopenharmony_ci	 * deviation is small compared to the average interval (avg >
2288c2ecf20Sopenharmony_ci	 * 6*stddev, avg^2 > 36*variance). The average is smaller than
2298c2ecf20Sopenharmony_ci	 * UINT_MAX aka U32_MAX, so computing its square does not
2308c2ecf20Sopenharmony_ci	 * overflow a u64. We simply reject this candidate average if
2318c2ecf20Sopenharmony_ci	 * the standard deviation is greater than 715 s (which is
2328c2ecf20Sopenharmony_ci	 * rather unlikely).
2338c2ecf20Sopenharmony_ci	 *
2348c2ecf20Sopenharmony_ci	 * Use this result only if there is no timer to wake us up sooner.
2358c2ecf20Sopenharmony_ci	 */
2368c2ecf20Sopenharmony_ci	if (likely(variance <= U64_MAX/36)) {
2378c2ecf20Sopenharmony_ci		if ((((u64)avg*avg > variance*36) && (divisor * 4 >= INTERVALS * 3))
2388c2ecf20Sopenharmony_ci							|| variance <= 400) {
2398c2ecf20Sopenharmony_ci			return avg;
2408c2ecf20Sopenharmony_ci		}
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/*
2448c2ecf20Sopenharmony_ci	 * If we have outliers to the upside in our distribution, discard
2458c2ecf20Sopenharmony_ci	 * those by setting the threshold to exclude these outliers, then
2468c2ecf20Sopenharmony_ci	 * calculate the average and standard deviation again. Once we get
2478c2ecf20Sopenharmony_ci	 * down to the bottom 3/4 of our samples, stop excluding samples.
2488c2ecf20Sopenharmony_ci	 *
2498c2ecf20Sopenharmony_ci	 * This can deal with workloads that have long pauses interspersed
2508c2ecf20Sopenharmony_ci	 * with sporadic activity with a bunch of short pauses.
2518c2ecf20Sopenharmony_ci	 */
2528c2ecf20Sopenharmony_ci	if ((divisor * 4) <= INTERVALS * 3)
2538c2ecf20Sopenharmony_ci		return UINT_MAX;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	thresh = max - 1;
2568c2ecf20Sopenharmony_ci	goto again;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/**
2608c2ecf20Sopenharmony_ci * menu_select - selects the next idle state to enter
2618c2ecf20Sopenharmony_ci * @drv: cpuidle driver containing state data
2628c2ecf20Sopenharmony_ci * @dev: the CPU
2638c2ecf20Sopenharmony_ci * @stop_tick: indication on whether or not to stop the tick
2648c2ecf20Sopenharmony_ci */
2658c2ecf20Sopenharmony_cistatic int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
2668c2ecf20Sopenharmony_ci		       bool *stop_tick)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct menu_device *data = this_cpu_ptr(&menu_devices);
2698c2ecf20Sopenharmony_ci	s64 latency_req = cpuidle_governor_latency_req(dev->cpu);
2708c2ecf20Sopenharmony_ci	unsigned int predicted_us;
2718c2ecf20Sopenharmony_ci	u64 predicted_ns;
2728c2ecf20Sopenharmony_ci	u64 interactivity_req;
2738c2ecf20Sopenharmony_ci	unsigned long nr_iowaiters;
2748c2ecf20Sopenharmony_ci	ktime_t delta_next;
2758c2ecf20Sopenharmony_ci	int i, idx;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (data->needs_update) {
2788c2ecf20Sopenharmony_ci		menu_update(drv, dev);
2798c2ecf20Sopenharmony_ci		data->needs_update = 0;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/* determine the expected residency time, round up */
2838c2ecf20Sopenharmony_ci	data->next_timer_ns = tick_nohz_get_sleep_length(&delta_next);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	nr_iowaiters = nr_iowait_cpu(dev->cpu);
2868c2ecf20Sopenharmony_ci	data->bucket = which_bucket(data->next_timer_ns, nr_iowaiters);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (unlikely(drv->state_count <= 1 || latency_req == 0) ||
2898c2ecf20Sopenharmony_ci	    ((data->next_timer_ns < drv->states[1].target_residency_ns ||
2908c2ecf20Sopenharmony_ci	      latency_req < drv->states[1].exit_latency_ns) &&
2918c2ecf20Sopenharmony_ci	     !dev->states_usage[0].disable)) {
2928c2ecf20Sopenharmony_ci		/*
2938c2ecf20Sopenharmony_ci		 * In this case state[0] will be used no matter what, so return
2948c2ecf20Sopenharmony_ci		 * it right away and keep the tick running if state[0] is a
2958c2ecf20Sopenharmony_ci		 * polling one.
2968c2ecf20Sopenharmony_ci		 */
2978c2ecf20Sopenharmony_ci		*stop_tick = !(drv->states[0].flags & CPUIDLE_FLAG_POLLING);
2988c2ecf20Sopenharmony_ci		return 0;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	/* Round up the result for half microseconds. */
3028c2ecf20Sopenharmony_ci	predicted_us = div_u64(data->next_timer_ns *
3038c2ecf20Sopenharmony_ci			       data->correction_factor[data->bucket] +
3048c2ecf20Sopenharmony_ci			       (RESOLUTION * DECAY * NSEC_PER_USEC) / 2,
3058c2ecf20Sopenharmony_ci			       RESOLUTION * DECAY * NSEC_PER_USEC);
3068c2ecf20Sopenharmony_ci	/* Use the lowest expected idle interval to pick the idle state. */
3078c2ecf20Sopenharmony_ci	predicted_ns = (u64)min(predicted_us,
3088c2ecf20Sopenharmony_ci				get_typical_interval(data, predicted_us)) *
3098c2ecf20Sopenharmony_ci				NSEC_PER_USEC;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (tick_nohz_tick_stopped()) {
3128c2ecf20Sopenharmony_ci		/*
3138c2ecf20Sopenharmony_ci		 * If the tick is already stopped, the cost of possible short
3148c2ecf20Sopenharmony_ci		 * idle duration misprediction is much higher, because the CPU
3158c2ecf20Sopenharmony_ci		 * may be stuck in a shallow idle state for a long time as a
3168c2ecf20Sopenharmony_ci		 * result of it.  In that case say we might mispredict and use
3178c2ecf20Sopenharmony_ci		 * the known time till the closest timer event for the idle
3188c2ecf20Sopenharmony_ci		 * state selection.
3198c2ecf20Sopenharmony_ci		 */
3208c2ecf20Sopenharmony_ci		if (predicted_ns < TICK_NSEC)
3218c2ecf20Sopenharmony_ci			predicted_ns = delta_next;
3228c2ecf20Sopenharmony_ci	} else {
3238c2ecf20Sopenharmony_ci		/*
3248c2ecf20Sopenharmony_ci		 * Use the performance multiplier and the user-configurable
3258c2ecf20Sopenharmony_ci		 * latency_req to determine the maximum exit latency.
3268c2ecf20Sopenharmony_ci		 */
3278c2ecf20Sopenharmony_ci		interactivity_req = div64_u64(predicted_ns,
3288c2ecf20Sopenharmony_ci					      performance_multiplier(nr_iowaiters));
3298c2ecf20Sopenharmony_ci		if (latency_req > interactivity_req)
3308c2ecf20Sopenharmony_ci			latency_req = interactivity_req;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/*
3348c2ecf20Sopenharmony_ci	 * Find the idle state with the lowest power while satisfying
3358c2ecf20Sopenharmony_ci	 * our constraints.
3368c2ecf20Sopenharmony_ci	 */
3378c2ecf20Sopenharmony_ci	idx = -1;
3388c2ecf20Sopenharmony_ci	for (i = 0; i < drv->state_count; i++) {
3398c2ecf20Sopenharmony_ci		struct cpuidle_state *s = &drv->states[i];
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci		if (dev->states_usage[i].disable)
3428c2ecf20Sopenharmony_ci			continue;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		if (idx == -1)
3458c2ecf20Sopenharmony_ci			idx = i; /* first enabled state */
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		if (s->target_residency_ns > predicted_ns) {
3488c2ecf20Sopenharmony_ci			/*
3498c2ecf20Sopenharmony_ci			 * Use a physical idle state, not busy polling, unless
3508c2ecf20Sopenharmony_ci			 * a timer is going to trigger soon enough.
3518c2ecf20Sopenharmony_ci			 */
3528c2ecf20Sopenharmony_ci			if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
3538c2ecf20Sopenharmony_ci			    s->exit_latency_ns <= latency_req &&
3548c2ecf20Sopenharmony_ci			    s->target_residency_ns <= data->next_timer_ns) {
3558c2ecf20Sopenharmony_ci				predicted_ns = s->target_residency_ns;
3568c2ecf20Sopenharmony_ci				idx = i;
3578c2ecf20Sopenharmony_ci				break;
3588c2ecf20Sopenharmony_ci			}
3598c2ecf20Sopenharmony_ci			if (predicted_ns < TICK_NSEC)
3608c2ecf20Sopenharmony_ci				break;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci			if (!tick_nohz_tick_stopped()) {
3638c2ecf20Sopenharmony_ci				/*
3648c2ecf20Sopenharmony_ci				 * If the state selected so far is shallow,
3658c2ecf20Sopenharmony_ci				 * waking up early won't hurt, so retain the
3668c2ecf20Sopenharmony_ci				 * tick in that case and let the governor run
3678c2ecf20Sopenharmony_ci				 * again in the next iteration of the loop.
3688c2ecf20Sopenharmony_ci				 */
3698c2ecf20Sopenharmony_ci				predicted_ns = drv->states[idx].target_residency_ns;
3708c2ecf20Sopenharmony_ci				break;
3718c2ecf20Sopenharmony_ci			}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci			/*
3748c2ecf20Sopenharmony_ci			 * If the state selected so far is shallow and this
3758c2ecf20Sopenharmony_ci			 * state's target residency matches the time till the
3768c2ecf20Sopenharmony_ci			 * closest timer event, select this one to avoid getting
3778c2ecf20Sopenharmony_ci			 * stuck in the shallow one for too long.
3788c2ecf20Sopenharmony_ci			 */
3798c2ecf20Sopenharmony_ci			if (drv->states[idx].target_residency_ns < TICK_NSEC &&
3808c2ecf20Sopenharmony_ci			    s->target_residency_ns <= delta_next)
3818c2ecf20Sopenharmony_ci				idx = i;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci			return idx;
3848c2ecf20Sopenharmony_ci		}
3858c2ecf20Sopenharmony_ci		if (s->exit_latency_ns > latency_req)
3868c2ecf20Sopenharmony_ci			break;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		idx = i;
3898c2ecf20Sopenharmony_ci	}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (idx == -1)
3928c2ecf20Sopenharmony_ci		idx = 0; /* No states enabled. Must use 0. */
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	/*
3958c2ecf20Sopenharmony_ci	 * Don't stop the tick if the selected state is a polling one or if the
3968c2ecf20Sopenharmony_ci	 * expected idle duration is shorter than the tick period length.
3978c2ecf20Sopenharmony_ci	 */
3988c2ecf20Sopenharmony_ci	if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) ||
3998c2ecf20Sopenharmony_ci	     predicted_ns < TICK_NSEC) && !tick_nohz_tick_stopped()) {
4008c2ecf20Sopenharmony_ci		*stop_tick = false;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci		if (idx > 0 && drv->states[idx].target_residency_ns > delta_next) {
4038c2ecf20Sopenharmony_ci			/*
4048c2ecf20Sopenharmony_ci			 * The tick is not going to be stopped and the target
4058c2ecf20Sopenharmony_ci			 * residency of the state to be returned is not within
4068c2ecf20Sopenharmony_ci			 * the time until the next timer event including the
4078c2ecf20Sopenharmony_ci			 * tick, so try to correct that.
4088c2ecf20Sopenharmony_ci			 */
4098c2ecf20Sopenharmony_ci			for (i = idx - 1; i >= 0; i--) {
4108c2ecf20Sopenharmony_ci				if (dev->states_usage[i].disable)
4118c2ecf20Sopenharmony_ci					continue;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci				idx = i;
4148c2ecf20Sopenharmony_ci				if (drv->states[i].target_residency_ns <= delta_next)
4158c2ecf20Sopenharmony_ci					break;
4168c2ecf20Sopenharmony_ci			}
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	return idx;
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci/**
4248c2ecf20Sopenharmony_ci * menu_reflect - records that data structures need update
4258c2ecf20Sopenharmony_ci * @dev: the CPU
4268c2ecf20Sopenharmony_ci * @index: the index of actual entered state
4278c2ecf20Sopenharmony_ci *
4288c2ecf20Sopenharmony_ci * NOTE: it's important to be fast here because this operation will add to
4298c2ecf20Sopenharmony_ci *       the overall exit latency.
4308c2ecf20Sopenharmony_ci */
4318c2ecf20Sopenharmony_cistatic void menu_reflect(struct cpuidle_device *dev, int index)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	struct menu_device *data = this_cpu_ptr(&menu_devices);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	dev->last_state_idx = index;
4368c2ecf20Sopenharmony_ci	data->needs_update = 1;
4378c2ecf20Sopenharmony_ci	data->tick_wakeup = tick_nohz_idle_got_tick();
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/**
4418c2ecf20Sopenharmony_ci * menu_update - attempts to guess what happened after entry
4428c2ecf20Sopenharmony_ci * @drv: cpuidle driver containing state data
4438c2ecf20Sopenharmony_ci * @dev: the CPU
4448c2ecf20Sopenharmony_ci */
4458c2ecf20Sopenharmony_cistatic void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct menu_device *data = this_cpu_ptr(&menu_devices);
4488c2ecf20Sopenharmony_ci	int last_idx = dev->last_state_idx;
4498c2ecf20Sopenharmony_ci	struct cpuidle_state *target = &drv->states[last_idx];
4508c2ecf20Sopenharmony_ci	u64 measured_ns;
4518c2ecf20Sopenharmony_ci	unsigned int new_factor;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	/*
4548c2ecf20Sopenharmony_ci	 * Try to figure out how much time passed between entry to low
4558c2ecf20Sopenharmony_ci	 * power state and occurrence of the wakeup event.
4568c2ecf20Sopenharmony_ci	 *
4578c2ecf20Sopenharmony_ci	 * If the entered idle state didn't support residency measurements,
4588c2ecf20Sopenharmony_ci	 * we use them anyway if they are short, and if long,
4598c2ecf20Sopenharmony_ci	 * truncate to the whole expected time.
4608c2ecf20Sopenharmony_ci	 *
4618c2ecf20Sopenharmony_ci	 * Any measured amount of time will include the exit latency.
4628c2ecf20Sopenharmony_ci	 * Since we are interested in when the wakeup begun, not when it
4638c2ecf20Sopenharmony_ci	 * was completed, we must subtract the exit latency. However, if
4648c2ecf20Sopenharmony_ci	 * the measured amount of time is less than the exit latency,
4658c2ecf20Sopenharmony_ci	 * assume the state was never reached and the exit latency is 0.
4668c2ecf20Sopenharmony_ci	 */
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	if (data->tick_wakeup && data->next_timer_ns > TICK_NSEC) {
4698c2ecf20Sopenharmony_ci		/*
4708c2ecf20Sopenharmony_ci		 * The nohz code said that there wouldn't be any events within
4718c2ecf20Sopenharmony_ci		 * the tick boundary (if the tick was stopped), but the idle
4728c2ecf20Sopenharmony_ci		 * duration predictor had a differing opinion.  Since the CPU
4738c2ecf20Sopenharmony_ci		 * was woken up by a tick (that wasn't stopped after all), the
4748c2ecf20Sopenharmony_ci		 * predictor was not quite right, so assume that the CPU could
4758c2ecf20Sopenharmony_ci		 * have been idle long (but not forever) to help the idle
4768c2ecf20Sopenharmony_ci		 * duration predictor do a better job next time.
4778c2ecf20Sopenharmony_ci		 */
4788c2ecf20Sopenharmony_ci		measured_ns = 9 * MAX_INTERESTING / 10;
4798c2ecf20Sopenharmony_ci	} else if ((drv->states[last_idx].flags & CPUIDLE_FLAG_POLLING) &&
4808c2ecf20Sopenharmony_ci		   dev->poll_time_limit) {
4818c2ecf20Sopenharmony_ci		/*
4828c2ecf20Sopenharmony_ci		 * The CPU exited the "polling" state due to a time limit, so
4838c2ecf20Sopenharmony_ci		 * the idle duration prediction leading to the selection of that
4848c2ecf20Sopenharmony_ci		 * state was inaccurate.  If a better prediction had been made,
4858c2ecf20Sopenharmony_ci		 * the CPU might have been woken up from idle by the next timer.
4868c2ecf20Sopenharmony_ci		 * Assume that to be the case.
4878c2ecf20Sopenharmony_ci		 */
4888c2ecf20Sopenharmony_ci		measured_ns = data->next_timer_ns;
4898c2ecf20Sopenharmony_ci	} else {
4908c2ecf20Sopenharmony_ci		/* measured value */
4918c2ecf20Sopenharmony_ci		measured_ns = dev->last_residency_ns;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		/* Deduct exit latency */
4948c2ecf20Sopenharmony_ci		if (measured_ns > 2 * target->exit_latency_ns)
4958c2ecf20Sopenharmony_ci			measured_ns -= target->exit_latency_ns;
4968c2ecf20Sopenharmony_ci		else
4978c2ecf20Sopenharmony_ci			measured_ns /= 2;
4988c2ecf20Sopenharmony_ci	}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* Make sure our coefficients do not exceed unity */
5018c2ecf20Sopenharmony_ci	if (measured_ns > data->next_timer_ns)
5028c2ecf20Sopenharmony_ci		measured_ns = data->next_timer_ns;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	/* Update our correction ratio */
5058c2ecf20Sopenharmony_ci	new_factor = data->correction_factor[data->bucket];
5068c2ecf20Sopenharmony_ci	new_factor -= new_factor / DECAY;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	if (data->next_timer_ns > 0 && measured_ns < MAX_INTERESTING)
5098c2ecf20Sopenharmony_ci		new_factor += div64_u64(RESOLUTION * measured_ns,
5108c2ecf20Sopenharmony_ci					data->next_timer_ns);
5118c2ecf20Sopenharmony_ci	else
5128c2ecf20Sopenharmony_ci		/*
5138c2ecf20Sopenharmony_ci		 * we were idle so long that we count it as a perfect
5148c2ecf20Sopenharmony_ci		 * prediction
5158c2ecf20Sopenharmony_ci		 */
5168c2ecf20Sopenharmony_ci		new_factor += RESOLUTION;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/*
5198c2ecf20Sopenharmony_ci	 * We don't want 0 as factor; we always want at least
5208c2ecf20Sopenharmony_ci	 * a tiny bit of estimated time. Fortunately, due to rounding,
5218c2ecf20Sopenharmony_ci	 * new_factor will stay nonzero regardless of measured_us values
5228c2ecf20Sopenharmony_ci	 * and the compiler can eliminate this test as long as DECAY > 1.
5238c2ecf20Sopenharmony_ci	 */
5248c2ecf20Sopenharmony_ci	if (DECAY == 1 && unlikely(new_factor == 0))
5258c2ecf20Sopenharmony_ci		new_factor = 1;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	data->correction_factor[data->bucket] = new_factor;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	/* update the repeating-pattern data */
5308c2ecf20Sopenharmony_ci	data->intervals[data->interval_ptr++] = ktime_to_us(measured_ns);
5318c2ecf20Sopenharmony_ci	if (data->interval_ptr >= INTERVALS)
5328c2ecf20Sopenharmony_ci		data->interval_ptr = 0;
5338c2ecf20Sopenharmony_ci}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci/**
5368c2ecf20Sopenharmony_ci * menu_enable_device - scans a CPU's states and does setup
5378c2ecf20Sopenharmony_ci * @drv: cpuidle driver
5388c2ecf20Sopenharmony_ci * @dev: the CPU
5398c2ecf20Sopenharmony_ci */
5408c2ecf20Sopenharmony_cistatic int menu_enable_device(struct cpuidle_driver *drv,
5418c2ecf20Sopenharmony_ci				struct cpuidle_device *dev)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
5448c2ecf20Sopenharmony_ci	int i;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	memset(data, 0, sizeof(struct menu_device));
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/*
5498c2ecf20Sopenharmony_ci	 * if the correction factor is 0 (eg first time init or cpu hotplug
5508c2ecf20Sopenharmony_ci	 * etc), we actually want to start out with a unity factor.
5518c2ecf20Sopenharmony_ci	 */
5528c2ecf20Sopenharmony_ci	for(i = 0; i < BUCKETS; i++)
5538c2ecf20Sopenharmony_ci		data->correction_factor[i] = RESOLUTION * DECAY;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return 0;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic struct cpuidle_governor menu_governor = {
5598c2ecf20Sopenharmony_ci	.name =		"menu",
5608c2ecf20Sopenharmony_ci	.rating =	20,
5618c2ecf20Sopenharmony_ci	.enable =	menu_enable_device,
5628c2ecf20Sopenharmony_ci	.select =	menu_select,
5638c2ecf20Sopenharmony_ci	.reflect =	menu_reflect,
5648c2ecf20Sopenharmony_ci};
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci/**
5678c2ecf20Sopenharmony_ci * init_menu - initializes the governor
5688c2ecf20Sopenharmony_ci */
5698c2ecf20Sopenharmony_cistatic int __init init_menu(void)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	return cpuidle_register_governor(&menu_governor);
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cipostcore_initcall(init_menu);
575