162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Timer events oriented CPU idle governor
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * TEO governor:
662306a36Sopenharmony_ci * Copyright (C) 2018 - 2021 Intel Corporation
762306a36Sopenharmony_ci * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Util-awareness mechanism:
1062306a36Sopenharmony_ci * Copyright (C) 2022 Arm Ltd.
1162306a36Sopenharmony_ci * Author: Kajetan Puchalski <kajetan.puchalski@arm.com>
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/**
1562306a36Sopenharmony_ci * DOC: teo-description
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * The idea of this governor is based on the observation that on many systems
1862306a36Sopenharmony_ci * timer events are two or more orders of magnitude more frequent than any
1962306a36Sopenharmony_ci * other interrupts, so they are likely to be the most significant cause of CPU
2062306a36Sopenharmony_ci * wakeups from idle states.  Moreover, information about what happened in the
2162306a36Sopenharmony_ci * (relatively recent) past can be used to estimate whether or not the deepest
2262306a36Sopenharmony_ci * idle state with target residency within the (known) time till the closest
2362306a36Sopenharmony_ci * timer event, referred to as the sleep length, is likely to be suitable for
2462306a36Sopenharmony_ci * the upcoming CPU idle period and, if not, then which of the shallower idle
2562306a36Sopenharmony_ci * states to choose instead of it.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * Of course, non-timer wakeup sources are more important in some use cases
2862306a36Sopenharmony_ci * which can be covered by taking a few most recent idle time intervals of the
2962306a36Sopenharmony_ci * CPU into account.  However, even in that context it is not necessary to
3062306a36Sopenharmony_ci * consider idle duration values greater than the sleep length, because the
3162306a36Sopenharmony_ci * closest timer will ultimately wake up the CPU anyway unless it is woken up
3262306a36Sopenharmony_ci * earlier.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * Thus this governor estimates whether or not the prospective idle duration of
3562306a36Sopenharmony_ci * a CPU is likely to be significantly shorter than the sleep length and selects
3662306a36Sopenharmony_ci * an idle state for it accordingly.
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * The computations carried out by this governor are based on using bins whose
3962306a36Sopenharmony_ci * boundaries are aligned with the target residency parameter values of the CPU
4062306a36Sopenharmony_ci * idle states provided by the %CPUIdle driver in the ascending order.  That is,
4162306a36Sopenharmony_ci * the first bin spans from 0 up to, but not including, the target residency of
4262306a36Sopenharmony_ci * the second idle state (idle state 1), the second bin spans from the target
4362306a36Sopenharmony_ci * residency of idle state 1 up to, but not including, the target residency of
4462306a36Sopenharmony_ci * idle state 2, the third bin spans from the target residency of idle state 2
4562306a36Sopenharmony_ci * up to, but not including, the target residency of idle state 3 and so on.
4662306a36Sopenharmony_ci * The last bin spans from the target residency of the deepest idle state
4762306a36Sopenharmony_ci * supplied by the driver to infinity.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * Two metrics called "hits" and "intercepts" are associated with each bin.
5062306a36Sopenharmony_ci * They are updated every time before selecting an idle state for the given CPU
5162306a36Sopenharmony_ci * in accordance with what happened last time.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * The "hits" metric reflects the relative frequency of situations in which the
5462306a36Sopenharmony_ci * sleep length and the idle duration measured after CPU wakeup fall into the
5562306a36Sopenharmony_ci * same bin (that is, the CPU appears to wake up "on time" relative to the sleep
5662306a36Sopenharmony_ci * length).  In turn, the "intercepts" metric reflects the relative frequency of
5762306a36Sopenharmony_ci * situations in which the measured idle duration is so much shorter than the
5862306a36Sopenharmony_ci * sleep length that the bin it falls into corresponds to an idle state
5962306a36Sopenharmony_ci * shallower than the one whose bin is fallen into by the sleep length (these
6062306a36Sopenharmony_ci * situations are referred to as "intercepts" below).
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * In addition to the metrics described above, the governor counts recent
6362306a36Sopenharmony_ci * intercepts (that is, intercepts that have occurred during the last
6462306a36Sopenharmony_ci * %NR_RECENT invocations of it for the given CPU) for each bin.
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * In order to select an idle state for a CPU, the governor takes the following
6762306a36Sopenharmony_ci * steps (modulo the possible latency constraint that must be taken into account
6862306a36Sopenharmony_ci * too):
6962306a36Sopenharmony_ci *
7062306a36Sopenharmony_ci * 1. Find the deepest CPU idle state whose target residency does not exceed
7162306a36Sopenharmony_ci *    the current sleep length (the candidate idle state) and compute 3 sums as
7262306a36Sopenharmony_ci *    follows:
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci *    - The sum of the "hits" and "intercepts" metrics for the candidate state
7562306a36Sopenharmony_ci *      and all of the deeper idle states (it represents the cases in which the
7662306a36Sopenharmony_ci *      CPU was idle long enough to avoid being intercepted if the sleep length
7762306a36Sopenharmony_ci *      had been equal to the current one).
7862306a36Sopenharmony_ci *
7962306a36Sopenharmony_ci *    - The sum of the "intercepts" metrics for all of the idle states shallower
8062306a36Sopenharmony_ci *      than the candidate one (it represents the cases in which the CPU was not
8162306a36Sopenharmony_ci *      idle long enough to avoid being intercepted if the sleep length had been
8262306a36Sopenharmony_ci *      equal to the current one).
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci *    - The sum of the numbers of recent intercepts for all of the idle states
8562306a36Sopenharmony_ci *      shallower than the candidate one.
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * 2. If the second sum is greater than the first one or the third sum is
8862306a36Sopenharmony_ci *    greater than %NR_RECENT / 2, the CPU is likely to wake up early, so look
8962306a36Sopenharmony_ci *    for an alternative idle state to select.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci *    - Traverse the idle states shallower than the candidate one in the
9262306a36Sopenharmony_ci *      descending order.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci *    - For each of them compute the sum of the "intercepts" metrics and the sum
9562306a36Sopenharmony_ci *      of the numbers of recent intercepts over all of the idle states between
9662306a36Sopenharmony_ci *      it and the candidate one (including the former and excluding the
9762306a36Sopenharmony_ci *      latter).
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci *    - If each of these sums that needs to be taken into account (because the
10062306a36Sopenharmony_ci *      check related to it has indicated that the CPU is likely to wake up
10162306a36Sopenharmony_ci *      early) is greater than a half of the corresponding sum computed in step
10262306a36Sopenharmony_ci *      1 (which means that the target residency of the state in question had
10362306a36Sopenharmony_ci *      not exceeded the idle duration in over a half of the relevant cases),
10462306a36Sopenharmony_ci *      select the given idle state instead of the candidate one.
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * 3. By default, select the candidate state.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * Util-awareness mechanism:
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * The idea behind the util-awareness extension is that there are two distinct
11162306a36Sopenharmony_ci * scenarios for the CPU which should result in two different approaches to idle
11262306a36Sopenharmony_ci * state selection - utilized and not utilized.
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * In this case, 'utilized' means that the average runqueue util of the CPU is
11562306a36Sopenharmony_ci * above a certain threshold.
11662306a36Sopenharmony_ci *
11762306a36Sopenharmony_ci * When the CPU is utilized while going into idle, more likely than not it will
11862306a36Sopenharmony_ci * be woken up to do more work soon and so a shallower idle state should be
11962306a36Sopenharmony_ci * selected to minimise latency and maximise performance. When the CPU is not
12062306a36Sopenharmony_ci * being utilized, the usual metrics-based approach to selecting the deepest
12162306a36Sopenharmony_ci * available idle state should be preferred to take advantage of the power
12262306a36Sopenharmony_ci * saving.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * In order to achieve this, the governor uses a utilization threshold.
12562306a36Sopenharmony_ci * The threshold is computed per-CPU as a percentage of the CPU's capacity
12662306a36Sopenharmony_ci * by bit shifting the capacity value. Based on testing, the shift of 6 (~1.56%)
12762306a36Sopenharmony_ci * seems to be getting the best results.
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci * Before selecting the next idle state, the governor compares the current CPU
13062306a36Sopenharmony_ci * util to the precomputed util threshold. If it's below, it defaults to the
13162306a36Sopenharmony_ci * TEO metrics mechanism. If it's above, the closest shallower idle state will
13262306a36Sopenharmony_ci * be selected instead, as long as is not a polling state.
13362306a36Sopenharmony_ci */
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#include <linux/cpuidle.h>
13662306a36Sopenharmony_ci#include <linux/jiffies.h>
13762306a36Sopenharmony_ci#include <linux/kernel.h>
13862306a36Sopenharmony_ci#include <linux/sched.h>
13962306a36Sopenharmony_ci#include <linux/sched/clock.h>
14062306a36Sopenharmony_ci#include <linux/sched/topology.h>
14162306a36Sopenharmony_ci#include <linux/tick.h>
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci#include "gov.h"
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/*
14662306a36Sopenharmony_ci * The number of bits to shift the CPU's capacity by in order to determine
14762306a36Sopenharmony_ci * the utilized threshold.
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci * 6 was chosen based on testing as the number that achieved the best balance
15062306a36Sopenharmony_ci * of power and performance on average.
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci * The resulting threshold is high enough to not be triggered by background
15362306a36Sopenharmony_ci * noise and low enough to react quickly when activity starts to ramp up.
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_ci#define UTIL_THRESHOLD_SHIFT 6
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci/*
15862306a36Sopenharmony_ci * The PULSE value is added to metrics when they grow and the DECAY_SHIFT value
15962306a36Sopenharmony_ci * is used for decreasing metrics on a regular basis.
16062306a36Sopenharmony_ci */
16162306a36Sopenharmony_ci#define PULSE		1024
16262306a36Sopenharmony_ci#define DECAY_SHIFT	3
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci/*
16562306a36Sopenharmony_ci * Number of the most recent idle duration values to take into consideration for
16662306a36Sopenharmony_ci * the detection of recent early wakeup patterns.
16762306a36Sopenharmony_ci */
16862306a36Sopenharmony_ci#define NR_RECENT	9
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci/**
17162306a36Sopenharmony_ci * struct teo_bin - Metrics used by the TEO cpuidle governor.
17262306a36Sopenharmony_ci * @intercepts: The "intercepts" metric.
17362306a36Sopenharmony_ci * @hits: The "hits" metric.
17462306a36Sopenharmony_ci * @recent: The number of recent "intercepts".
17562306a36Sopenharmony_ci */
17662306a36Sopenharmony_cistruct teo_bin {
17762306a36Sopenharmony_ci	unsigned int intercepts;
17862306a36Sopenharmony_ci	unsigned int hits;
17962306a36Sopenharmony_ci	unsigned int recent;
18062306a36Sopenharmony_ci};
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/**
18362306a36Sopenharmony_ci * struct teo_cpu - CPU data used by the TEO cpuidle governor.
18462306a36Sopenharmony_ci * @time_span_ns: Time between idle state selection and post-wakeup update.
18562306a36Sopenharmony_ci * @sleep_length_ns: Time till the closest timer event (at the selection time).
18662306a36Sopenharmony_ci * @state_bins: Idle state data bins for this CPU.
18762306a36Sopenharmony_ci * @total: Grand total of the "intercepts" and "hits" metrics for all bins.
18862306a36Sopenharmony_ci * @next_recent_idx: Index of the next @recent_idx entry to update.
18962306a36Sopenharmony_ci * @recent_idx: Indices of bins corresponding to recent "intercepts".
19062306a36Sopenharmony_ci * @tick_hits: Number of "hits" after TICK_NSEC.
19162306a36Sopenharmony_ci * @util_threshold: Threshold above which the CPU is considered utilized
19262306a36Sopenharmony_ci */
19362306a36Sopenharmony_cistruct teo_cpu {
19462306a36Sopenharmony_ci	s64 time_span_ns;
19562306a36Sopenharmony_ci	s64 sleep_length_ns;
19662306a36Sopenharmony_ci	struct teo_bin state_bins[CPUIDLE_STATE_MAX];
19762306a36Sopenharmony_ci	unsigned int total;
19862306a36Sopenharmony_ci	int next_recent_idx;
19962306a36Sopenharmony_ci	int recent_idx[NR_RECENT];
20062306a36Sopenharmony_ci	unsigned int tick_hits;
20162306a36Sopenharmony_ci	unsigned long util_threshold;
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic DEFINE_PER_CPU(struct teo_cpu, teo_cpus);
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci/**
20762306a36Sopenharmony_ci * teo_cpu_is_utilized - Check if the CPU's util is above the threshold
20862306a36Sopenharmony_ci * @cpu: Target CPU
20962306a36Sopenharmony_ci * @cpu_data: Governor CPU data for the target CPU
21062306a36Sopenharmony_ci */
21162306a36Sopenharmony_ci#ifdef CONFIG_SMP
21262306a36Sopenharmony_cistatic bool teo_cpu_is_utilized(int cpu, struct teo_cpu *cpu_data)
21362306a36Sopenharmony_ci{
21462306a36Sopenharmony_ci	return sched_cpu_util(cpu) > cpu_data->util_threshold;
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci#else
21762306a36Sopenharmony_cistatic bool teo_cpu_is_utilized(int cpu, struct teo_cpu *cpu_data)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	return false;
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci#endif
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci/**
22462306a36Sopenharmony_ci * teo_update - Update CPU metrics after wakeup.
22562306a36Sopenharmony_ci * @drv: cpuidle driver containing state data.
22662306a36Sopenharmony_ci * @dev: Target CPU.
22762306a36Sopenharmony_ci */
22862306a36Sopenharmony_cistatic void teo_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
22962306a36Sopenharmony_ci{
23062306a36Sopenharmony_ci	struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
23162306a36Sopenharmony_ci	int i, idx_timer = 0, idx_duration = 0;
23262306a36Sopenharmony_ci	s64 target_residency_ns;
23362306a36Sopenharmony_ci	u64 measured_ns;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	if (cpu_data->time_span_ns >= cpu_data->sleep_length_ns) {
23662306a36Sopenharmony_ci		/*
23762306a36Sopenharmony_ci		 * One of the safety nets has triggered or the wakeup was close
23862306a36Sopenharmony_ci		 * enough to the closest timer event expected at the idle state
23962306a36Sopenharmony_ci		 * selection time to be discarded.
24062306a36Sopenharmony_ci		 */
24162306a36Sopenharmony_ci		measured_ns = U64_MAX;
24262306a36Sopenharmony_ci	} else {
24362306a36Sopenharmony_ci		u64 lat_ns = drv->states[dev->last_state_idx].exit_latency_ns;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci		/*
24662306a36Sopenharmony_ci		 * The computations below are to determine whether or not the
24762306a36Sopenharmony_ci		 * (saved) time till the next timer event and the measured idle
24862306a36Sopenharmony_ci		 * duration fall into the same "bin", so use last_residency_ns
24962306a36Sopenharmony_ci		 * for that instead of time_span_ns which includes the cpuidle
25062306a36Sopenharmony_ci		 * overhead.
25162306a36Sopenharmony_ci		 */
25262306a36Sopenharmony_ci		measured_ns = dev->last_residency_ns;
25362306a36Sopenharmony_ci		/*
25462306a36Sopenharmony_ci		 * The delay between the wakeup and the first instruction
25562306a36Sopenharmony_ci		 * executed by the CPU is not likely to be worst-case every
25662306a36Sopenharmony_ci		 * time, so take 1/2 of the exit latency as a very rough
25762306a36Sopenharmony_ci		 * approximation of the average of it.
25862306a36Sopenharmony_ci		 */
25962306a36Sopenharmony_ci		if (measured_ns >= lat_ns)
26062306a36Sopenharmony_ci			measured_ns -= lat_ns / 2;
26162306a36Sopenharmony_ci		else
26262306a36Sopenharmony_ci			measured_ns /= 2;
26362306a36Sopenharmony_ci	}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	cpu_data->total = 0;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	/*
26862306a36Sopenharmony_ci	 * Decay the "hits" and "intercepts" metrics for all of the bins and
26962306a36Sopenharmony_ci	 * find the bins that the sleep length and the measured idle duration
27062306a36Sopenharmony_ci	 * fall into.
27162306a36Sopenharmony_ci	 */
27262306a36Sopenharmony_ci	for (i = 0; i < drv->state_count; i++) {
27362306a36Sopenharmony_ci		struct teo_bin *bin = &cpu_data->state_bins[i];
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci		bin->hits -= bin->hits >> DECAY_SHIFT;
27662306a36Sopenharmony_ci		bin->intercepts -= bin->intercepts >> DECAY_SHIFT;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci		cpu_data->total += bin->hits + bin->intercepts;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci		target_residency_ns = drv->states[i].target_residency_ns;
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci		if (target_residency_ns <= cpu_data->sleep_length_ns) {
28362306a36Sopenharmony_ci			idx_timer = i;
28462306a36Sopenharmony_ci			if (target_residency_ns <= measured_ns)
28562306a36Sopenharmony_ci				idx_duration = i;
28662306a36Sopenharmony_ci		}
28762306a36Sopenharmony_ci	}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	i = cpu_data->next_recent_idx++;
29062306a36Sopenharmony_ci	if (cpu_data->next_recent_idx >= NR_RECENT)
29162306a36Sopenharmony_ci		cpu_data->next_recent_idx = 0;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	if (cpu_data->recent_idx[i] >= 0)
29462306a36Sopenharmony_ci		cpu_data->state_bins[cpu_data->recent_idx[i]].recent--;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	/*
29762306a36Sopenharmony_ci	 * If the deepest state's target residency is below the tick length,
29862306a36Sopenharmony_ci	 * make a record of it to help teo_select() decide whether or not
29962306a36Sopenharmony_ci	 * to stop the tick.  This effectively adds an extra hits-only bin
30062306a36Sopenharmony_ci	 * beyond the last state-related one.
30162306a36Sopenharmony_ci	 */
30262306a36Sopenharmony_ci	if (target_residency_ns < TICK_NSEC) {
30362306a36Sopenharmony_ci		cpu_data->tick_hits -= cpu_data->tick_hits >> DECAY_SHIFT;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci		cpu_data->total += cpu_data->tick_hits;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci		if (TICK_NSEC <= cpu_data->sleep_length_ns) {
30862306a36Sopenharmony_ci			idx_timer = drv->state_count;
30962306a36Sopenharmony_ci			if (TICK_NSEC <= measured_ns) {
31062306a36Sopenharmony_ci				cpu_data->tick_hits += PULSE;
31162306a36Sopenharmony_ci				goto end;
31262306a36Sopenharmony_ci			}
31362306a36Sopenharmony_ci		}
31462306a36Sopenharmony_ci	}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	/*
31762306a36Sopenharmony_ci	 * If the measured idle duration falls into the same bin as the sleep
31862306a36Sopenharmony_ci	 * length, this is a "hit", so update the "hits" metric for that bin.
31962306a36Sopenharmony_ci	 * Otherwise, update the "intercepts" metric for the bin fallen into by
32062306a36Sopenharmony_ci	 * the measured idle duration.
32162306a36Sopenharmony_ci	 */
32262306a36Sopenharmony_ci	if (idx_timer == idx_duration) {
32362306a36Sopenharmony_ci		cpu_data->state_bins[idx_timer].hits += PULSE;
32462306a36Sopenharmony_ci		cpu_data->recent_idx[i] = -1;
32562306a36Sopenharmony_ci	} else {
32662306a36Sopenharmony_ci		cpu_data->state_bins[idx_duration].intercepts += PULSE;
32762306a36Sopenharmony_ci		cpu_data->state_bins[idx_duration].recent++;
32862306a36Sopenharmony_ci		cpu_data->recent_idx[i] = idx_duration;
32962306a36Sopenharmony_ci	}
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ciend:
33262306a36Sopenharmony_ci	cpu_data->total += PULSE;
33362306a36Sopenharmony_ci}
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_cistatic bool teo_state_ok(int i, struct cpuidle_driver *drv)
33662306a36Sopenharmony_ci{
33762306a36Sopenharmony_ci	return !tick_nohz_tick_stopped() ||
33862306a36Sopenharmony_ci		drv->states[i].target_residency_ns >= TICK_NSEC;
33962306a36Sopenharmony_ci}
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci/**
34262306a36Sopenharmony_ci * teo_find_shallower_state - Find shallower idle state matching given duration.
34362306a36Sopenharmony_ci * @drv: cpuidle driver containing state data.
34462306a36Sopenharmony_ci * @dev: Target CPU.
34562306a36Sopenharmony_ci * @state_idx: Index of the capping idle state.
34662306a36Sopenharmony_ci * @duration_ns: Idle duration value to match.
34762306a36Sopenharmony_ci * @no_poll: Don't consider polling states.
34862306a36Sopenharmony_ci */
34962306a36Sopenharmony_cistatic int teo_find_shallower_state(struct cpuidle_driver *drv,
35062306a36Sopenharmony_ci				    struct cpuidle_device *dev, int state_idx,
35162306a36Sopenharmony_ci				    s64 duration_ns, bool no_poll)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	int i;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	for (i = state_idx - 1; i >= 0; i--) {
35662306a36Sopenharmony_ci		if (dev->states_usage[i].disable ||
35762306a36Sopenharmony_ci				(no_poll && drv->states[i].flags & CPUIDLE_FLAG_POLLING))
35862306a36Sopenharmony_ci			continue;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci		state_idx = i;
36162306a36Sopenharmony_ci		if (drv->states[i].target_residency_ns <= duration_ns)
36262306a36Sopenharmony_ci			break;
36362306a36Sopenharmony_ci	}
36462306a36Sopenharmony_ci	return state_idx;
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/**
36862306a36Sopenharmony_ci * teo_select - Selects the next idle state to enter.
36962306a36Sopenharmony_ci * @drv: cpuidle driver containing state data.
37062306a36Sopenharmony_ci * @dev: Target CPU.
37162306a36Sopenharmony_ci * @stop_tick: Indication on whether or not to stop the scheduler tick.
37262306a36Sopenharmony_ci */
37362306a36Sopenharmony_cistatic int teo_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
37462306a36Sopenharmony_ci		      bool *stop_tick)
37562306a36Sopenharmony_ci{
37662306a36Sopenharmony_ci	struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
37762306a36Sopenharmony_ci	s64 latency_req = cpuidle_governor_latency_req(dev->cpu);
37862306a36Sopenharmony_ci	ktime_t delta_tick = TICK_NSEC / 2;
37962306a36Sopenharmony_ci	unsigned int tick_intercept_sum = 0;
38062306a36Sopenharmony_ci	unsigned int idx_intercept_sum = 0;
38162306a36Sopenharmony_ci	unsigned int intercept_sum = 0;
38262306a36Sopenharmony_ci	unsigned int idx_recent_sum = 0;
38362306a36Sopenharmony_ci	unsigned int recent_sum = 0;
38462306a36Sopenharmony_ci	unsigned int idx_hit_sum = 0;
38562306a36Sopenharmony_ci	unsigned int hit_sum = 0;
38662306a36Sopenharmony_ci	int constraint_idx = 0;
38762306a36Sopenharmony_ci	int idx0 = 0, idx = -1;
38862306a36Sopenharmony_ci	bool alt_intercepts, alt_recent;
38962306a36Sopenharmony_ci	bool cpu_utilized;
39062306a36Sopenharmony_ci	s64 duration_ns;
39162306a36Sopenharmony_ci	int i;
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	if (dev->last_state_idx >= 0) {
39462306a36Sopenharmony_ci		teo_update(drv, dev);
39562306a36Sopenharmony_ci		dev->last_state_idx = -1;
39662306a36Sopenharmony_ci	}
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	cpu_data->time_span_ns = local_clock();
39962306a36Sopenharmony_ci	/*
40062306a36Sopenharmony_ci	 * Set the expected sleep length to infinity in case of an early
40162306a36Sopenharmony_ci	 * return.
40262306a36Sopenharmony_ci	 */
40362306a36Sopenharmony_ci	cpu_data->sleep_length_ns = KTIME_MAX;
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	/* Check if there is any choice in the first place. */
40662306a36Sopenharmony_ci	if (drv->state_count < 2) {
40762306a36Sopenharmony_ci		idx = 0;
40862306a36Sopenharmony_ci		goto out_tick;
40962306a36Sopenharmony_ci	}
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	if (!dev->states_usage[0].disable)
41262306a36Sopenharmony_ci		idx = 0;
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	cpu_utilized = teo_cpu_is_utilized(dev->cpu, cpu_data);
41562306a36Sopenharmony_ci	/*
41662306a36Sopenharmony_ci	 * If the CPU is being utilized over the threshold and there are only 2
41762306a36Sopenharmony_ci	 * states to choose from, the metrics need not be considered, so choose
41862306a36Sopenharmony_ci	 * the shallowest non-polling state and exit.
41962306a36Sopenharmony_ci	 */
42062306a36Sopenharmony_ci	if (drv->state_count < 3 && cpu_utilized) {
42162306a36Sopenharmony_ci		/*
42262306a36Sopenharmony_ci		 * If state 0 is enabled and it is not a polling one, select it
42362306a36Sopenharmony_ci		 * right away unless the scheduler tick has been stopped, in
42462306a36Sopenharmony_ci		 * which case care needs to be taken to leave the CPU in a deep
42562306a36Sopenharmony_ci		 * enough state in case it is not woken up any time soon after
42662306a36Sopenharmony_ci		 * all.  If state 1 is disabled, though, state 0 must be used
42762306a36Sopenharmony_ci		 * anyway.
42862306a36Sopenharmony_ci		 */
42962306a36Sopenharmony_ci		if ((!idx && !(drv->states[0].flags & CPUIDLE_FLAG_POLLING) &&
43062306a36Sopenharmony_ci		    teo_state_ok(0, drv)) || dev->states_usage[1].disable) {
43162306a36Sopenharmony_ci			idx = 0;
43262306a36Sopenharmony_ci			goto out_tick;
43362306a36Sopenharmony_ci		}
43462306a36Sopenharmony_ci		/* Assume that state 1 is not a polling one and use it. */
43562306a36Sopenharmony_ci		idx = 1;
43662306a36Sopenharmony_ci		duration_ns = drv->states[1].target_residency_ns;
43762306a36Sopenharmony_ci		goto end;
43862306a36Sopenharmony_ci	}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci	/* Compute the sums of metrics for early wakeup pattern detection. */
44162306a36Sopenharmony_ci	for (i = 1; i < drv->state_count; i++) {
44262306a36Sopenharmony_ci		struct teo_bin *prev_bin = &cpu_data->state_bins[i-1];
44362306a36Sopenharmony_ci		struct cpuidle_state *s = &drv->states[i];
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci		/*
44662306a36Sopenharmony_ci		 * Update the sums of idle state mertics for all of the states
44762306a36Sopenharmony_ci		 * shallower than the current one.
44862306a36Sopenharmony_ci		 */
44962306a36Sopenharmony_ci		intercept_sum += prev_bin->intercepts;
45062306a36Sopenharmony_ci		hit_sum += prev_bin->hits;
45162306a36Sopenharmony_ci		recent_sum += prev_bin->recent;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci		if (dev->states_usage[i].disable)
45462306a36Sopenharmony_ci			continue;
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci		if (idx < 0)
45762306a36Sopenharmony_ci			idx0 = i; /* first enabled state */
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci		idx = i;
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci		if (s->exit_latency_ns <= latency_req)
46262306a36Sopenharmony_ci			constraint_idx = i;
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci		/* Save the sums for the current state. */
46562306a36Sopenharmony_ci		idx_intercept_sum = intercept_sum;
46662306a36Sopenharmony_ci		idx_hit_sum = hit_sum;
46762306a36Sopenharmony_ci		idx_recent_sum = recent_sum;
46862306a36Sopenharmony_ci	}
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci	/* Avoid unnecessary overhead. */
47162306a36Sopenharmony_ci	if (idx < 0) {
47262306a36Sopenharmony_ci		idx = 0; /* No states enabled, must use 0. */
47362306a36Sopenharmony_ci		goto out_tick;
47462306a36Sopenharmony_ci	}
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	if (idx == idx0) {
47762306a36Sopenharmony_ci		/*
47862306a36Sopenharmony_ci		 * Only one idle state is enabled, so use it, but do not
47962306a36Sopenharmony_ci		 * allow the tick to be stopped it is shallow enough.
48062306a36Sopenharmony_ci		 */
48162306a36Sopenharmony_ci		duration_ns = drv->states[idx].target_residency_ns;
48262306a36Sopenharmony_ci		goto end;
48362306a36Sopenharmony_ci	}
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	tick_intercept_sum = intercept_sum +
48662306a36Sopenharmony_ci			cpu_data->state_bins[drv->state_count-1].intercepts;
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	/*
48962306a36Sopenharmony_ci	 * If the sum of the intercepts metric for all of the idle states
49062306a36Sopenharmony_ci	 * shallower than the current candidate one (idx) is greater than the
49162306a36Sopenharmony_ci	 * sum of the intercepts and hits metrics for the candidate state and
49262306a36Sopenharmony_ci	 * all of the deeper states, or the sum of the numbers of recent
49362306a36Sopenharmony_ci	 * intercepts over all of the states shallower than the candidate one
49462306a36Sopenharmony_ci	 * is greater than a half of the number of recent events taken into
49562306a36Sopenharmony_ci	 * account, a shallower idle state is likely to be a better choice.
49662306a36Sopenharmony_ci	 */
49762306a36Sopenharmony_ci	alt_intercepts = 2 * idx_intercept_sum > cpu_data->total - idx_hit_sum;
49862306a36Sopenharmony_ci	alt_recent = idx_recent_sum > NR_RECENT / 2;
49962306a36Sopenharmony_ci	if (alt_recent || alt_intercepts) {
50062306a36Sopenharmony_ci		int first_suitable_idx = idx;
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci		/*
50362306a36Sopenharmony_ci		 * Look for the deepest idle state whose target residency had
50462306a36Sopenharmony_ci		 * not exceeded the idle duration in over a half of the relevant
50562306a36Sopenharmony_ci		 * cases (both with respect to intercepts overall and with
50662306a36Sopenharmony_ci		 * respect to the recent intercepts only) in the past.
50762306a36Sopenharmony_ci		 *
50862306a36Sopenharmony_ci		 * Take the possible duration limitation present if the tick
50962306a36Sopenharmony_ci		 * has been stopped already into account.
51062306a36Sopenharmony_ci		 */
51162306a36Sopenharmony_ci		intercept_sum = 0;
51262306a36Sopenharmony_ci		recent_sum = 0;
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci		for (i = idx - 1; i >= 0; i--) {
51562306a36Sopenharmony_ci			struct teo_bin *bin = &cpu_data->state_bins[i];
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci			intercept_sum += bin->intercepts;
51862306a36Sopenharmony_ci			recent_sum += bin->recent;
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci			if ((!alt_recent || 2 * recent_sum > idx_recent_sum) &&
52162306a36Sopenharmony_ci			    (!alt_intercepts ||
52262306a36Sopenharmony_ci			     2 * intercept_sum > idx_intercept_sum)) {
52362306a36Sopenharmony_ci				/*
52462306a36Sopenharmony_ci				 * Use the current state unless it is too
52562306a36Sopenharmony_ci				 * shallow or disabled, in which case take the
52662306a36Sopenharmony_ci				 * first enabled state that is deep enough.
52762306a36Sopenharmony_ci				 */
52862306a36Sopenharmony_ci				if (teo_state_ok(i, drv) &&
52962306a36Sopenharmony_ci				    !dev->states_usage[i].disable)
53062306a36Sopenharmony_ci					idx = i;
53162306a36Sopenharmony_ci				else
53262306a36Sopenharmony_ci					idx = first_suitable_idx;
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci				break;
53562306a36Sopenharmony_ci			}
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci			if (dev->states_usage[i].disable)
53862306a36Sopenharmony_ci				continue;
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci			if (!teo_state_ok(i, drv)) {
54162306a36Sopenharmony_ci				/*
54262306a36Sopenharmony_ci				 * The current state is too shallow, but if an
54362306a36Sopenharmony_ci				 * alternative candidate state has been found,
54462306a36Sopenharmony_ci				 * it may still turn out to be a better choice.
54562306a36Sopenharmony_ci				 */
54662306a36Sopenharmony_ci				if (first_suitable_idx != idx)
54762306a36Sopenharmony_ci					continue;
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci				break;
55062306a36Sopenharmony_ci			}
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci			first_suitable_idx = i;
55362306a36Sopenharmony_ci		}
55462306a36Sopenharmony_ci	}
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	/*
55762306a36Sopenharmony_ci	 * If there is a latency constraint, it may be necessary to select an
55862306a36Sopenharmony_ci	 * idle state shallower than the current candidate one.
55962306a36Sopenharmony_ci	 */
56062306a36Sopenharmony_ci	if (idx > constraint_idx)
56162306a36Sopenharmony_ci		idx = constraint_idx;
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci	/*
56462306a36Sopenharmony_ci	 * If the CPU is being utilized over the threshold, choose a shallower
56562306a36Sopenharmony_ci	 * non-polling state to improve latency, unless the scheduler tick has
56662306a36Sopenharmony_ci	 * been stopped already and the shallower state's target residency is
56762306a36Sopenharmony_ci	 * not sufficiently large.
56862306a36Sopenharmony_ci	 */
56962306a36Sopenharmony_ci	if (cpu_utilized) {
57062306a36Sopenharmony_ci		i = teo_find_shallower_state(drv, dev, idx, KTIME_MAX, true);
57162306a36Sopenharmony_ci		if (teo_state_ok(i, drv))
57262306a36Sopenharmony_ci			idx = i;
57362306a36Sopenharmony_ci	}
57462306a36Sopenharmony_ci
57562306a36Sopenharmony_ci	/*
57662306a36Sopenharmony_ci	 * Skip the timers check if state 0 is the current candidate one,
57762306a36Sopenharmony_ci	 * because an immediate non-timer wakeup is expected in that case.
57862306a36Sopenharmony_ci	 */
57962306a36Sopenharmony_ci	if (!idx)
58062306a36Sopenharmony_ci		goto out_tick;
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci	/*
58362306a36Sopenharmony_ci	 * If state 0 is a polling one, check if the target residency of
58462306a36Sopenharmony_ci	 * the current candidate state is low enough and skip the timers
58562306a36Sopenharmony_ci	 * check in that case too.
58662306a36Sopenharmony_ci	 */
58762306a36Sopenharmony_ci	if ((drv->states[0].flags & CPUIDLE_FLAG_POLLING) &&
58862306a36Sopenharmony_ci	    drv->states[idx].target_residency_ns < RESIDENCY_THRESHOLD_NS)
58962306a36Sopenharmony_ci		goto out_tick;
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	duration_ns = tick_nohz_get_sleep_length(&delta_tick);
59262306a36Sopenharmony_ci	cpu_data->sleep_length_ns = duration_ns;
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	/*
59562306a36Sopenharmony_ci	 * If the closest expected timer is before the terget residency of the
59662306a36Sopenharmony_ci	 * candidate state, a shallower one needs to be found.
59762306a36Sopenharmony_ci	 */
59862306a36Sopenharmony_ci	if (drv->states[idx].target_residency_ns > duration_ns) {
59962306a36Sopenharmony_ci		i = teo_find_shallower_state(drv, dev, idx, duration_ns, false);
60062306a36Sopenharmony_ci		if (teo_state_ok(i, drv))
60162306a36Sopenharmony_ci			idx = i;
60262306a36Sopenharmony_ci	}
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci	/*
60562306a36Sopenharmony_ci	 * If the selected state's target residency is below the tick length
60662306a36Sopenharmony_ci	 * and intercepts occurring before the tick length are the majority of
60762306a36Sopenharmony_ci	 * total wakeup events, do not stop the tick.
60862306a36Sopenharmony_ci	 */
60962306a36Sopenharmony_ci	if (drv->states[idx].target_residency_ns < TICK_NSEC &&
61062306a36Sopenharmony_ci	    tick_intercept_sum > cpu_data->total / 2 + cpu_data->total / 8)
61162306a36Sopenharmony_ci		duration_ns = TICK_NSEC / 2;
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ciend:
61462306a36Sopenharmony_ci	/*
61562306a36Sopenharmony_ci	 * Allow the tick to be stopped unless the selected state is a polling
61662306a36Sopenharmony_ci	 * one or the expected idle duration is shorter than the tick period
61762306a36Sopenharmony_ci	 * length.
61862306a36Sopenharmony_ci	 */
61962306a36Sopenharmony_ci	if ((!(drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
62062306a36Sopenharmony_ci	    duration_ns >= TICK_NSEC) || tick_nohz_tick_stopped())
62162306a36Sopenharmony_ci		return idx;
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci	/*
62462306a36Sopenharmony_ci	 * The tick is not going to be stopped, so if the target residency of
62562306a36Sopenharmony_ci	 * the state to be returned is not within the time till the closest
62662306a36Sopenharmony_ci	 * timer including the tick, try to correct that.
62762306a36Sopenharmony_ci	 */
62862306a36Sopenharmony_ci	if (idx > idx0 &&
62962306a36Sopenharmony_ci	    drv->states[idx].target_residency_ns > delta_tick)
63062306a36Sopenharmony_ci		idx = teo_find_shallower_state(drv, dev, idx, delta_tick, false);
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ciout_tick:
63362306a36Sopenharmony_ci	*stop_tick = false;
63462306a36Sopenharmony_ci	return idx;
63562306a36Sopenharmony_ci}
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci/**
63862306a36Sopenharmony_ci * teo_reflect - Note that governor data for the CPU need to be updated.
63962306a36Sopenharmony_ci * @dev: Target CPU.
64062306a36Sopenharmony_ci * @state: Entered state.
64162306a36Sopenharmony_ci */
64262306a36Sopenharmony_cistatic void teo_reflect(struct cpuidle_device *dev, int state)
64362306a36Sopenharmony_ci{
64462306a36Sopenharmony_ci	struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	dev->last_state_idx = state;
64762306a36Sopenharmony_ci	/*
64862306a36Sopenharmony_ci	 * If the wakeup was not "natural", but triggered by one of the safety
64962306a36Sopenharmony_ci	 * nets, assume that the CPU might have been idle for the entire sleep
65062306a36Sopenharmony_ci	 * length time.
65162306a36Sopenharmony_ci	 */
65262306a36Sopenharmony_ci	if (dev->poll_time_limit ||
65362306a36Sopenharmony_ci	    (tick_nohz_idle_got_tick() && cpu_data->sleep_length_ns > TICK_NSEC)) {
65462306a36Sopenharmony_ci		dev->poll_time_limit = false;
65562306a36Sopenharmony_ci		cpu_data->time_span_ns = cpu_data->sleep_length_ns;
65662306a36Sopenharmony_ci	} else {
65762306a36Sopenharmony_ci		cpu_data->time_span_ns = local_clock() - cpu_data->time_span_ns;
65862306a36Sopenharmony_ci	}
65962306a36Sopenharmony_ci}
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci/**
66262306a36Sopenharmony_ci * teo_enable_device - Initialize the governor's data for the target CPU.
66362306a36Sopenharmony_ci * @drv: cpuidle driver (not used).
66462306a36Sopenharmony_ci * @dev: Target CPU.
66562306a36Sopenharmony_ci */
66662306a36Sopenharmony_cistatic int teo_enable_device(struct cpuidle_driver *drv,
66762306a36Sopenharmony_ci			     struct cpuidle_device *dev)
66862306a36Sopenharmony_ci{
66962306a36Sopenharmony_ci	struct teo_cpu *cpu_data = per_cpu_ptr(&teo_cpus, dev->cpu);
67062306a36Sopenharmony_ci	unsigned long max_capacity = arch_scale_cpu_capacity(dev->cpu);
67162306a36Sopenharmony_ci	int i;
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	memset(cpu_data, 0, sizeof(*cpu_data));
67462306a36Sopenharmony_ci	cpu_data->util_threshold = max_capacity >> UTIL_THRESHOLD_SHIFT;
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	for (i = 0; i < NR_RECENT; i++)
67762306a36Sopenharmony_ci		cpu_data->recent_idx[i] = -1;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	return 0;
68062306a36Sopenharmony_ci}
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_cistatic struct cpuidle_governor teo_governor = {
68362306a36Sopenharmony_ci	.name =		"teo",
68462306a36Sopenharmony_ci	.rating =	19,
68562306a36Sopenharmony_ci	.enable =	teo_enable_device,
68662306a36Sopenharmony_ci	.select =	teo_select,
68762306a36Sopenharmony_ci	.reflect =	teo_reflect,
68862306a36Sopenharmony_ci};
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_cistatic int __init teo_governor_init(void)
69162306a36Sopenharmony_ci{
69262306a36Sopenharmony_ci	return cpuidle_register_governor(&teo_governor);
69362306a36Sopenharmony_ci}
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_cipostcore_initcall(teo_governor_init);
696