Merge branch 'pm-cpufreq'
[platform/kernel/linux-starfive.git] / kernel / sched / cpufreq_schedutil.c
index b0ad37b..6931f0c 100644 (file)
@@ -53,6 +53,7 @@ struct sugov_cpu {
        unsigned int            iowait_boost;
        u64                     last_update;
 
+       unsigned long           util;
        unsigned long           bw_dl;
        unsigned long           max;
 
@@ -276,16 +277,15 @@ unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
        return min(max, util);
 }
 
-static unsigned long sugov_get_util(struct sugov_cpu *sg_cpu)
+static void sugov_get_util(struct sugov_cpu *sg_cpu)
 {
        struct rq *rq = cpu_rq(sg_cpu->cpu);
-       unsigned long util = cpu_util_cfs(rq);
        unsigned long max = arch_scale_cpu_capacity(sg_cpu->cpu);
 
        sg_cpu->max = max;
        sg_cpu->bw_dl = cpu_bw_dl(rq);
-
-       return schedutil_cpu_util(sg_cpu->cpu, util, max, FREQUENCY_UTIL, NULL);
+       sg_cpu->util = schedutil_cpu_util(sg_cpu->cpu, cpu_util_cfs(rq), max,
+                                         FREQUENCY_UTIL, NULL);
 }
 
 /**
@@ -362,8 +362,6 @@ static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
  * sugov_iowait_apply() - Apply the IO boost to a CPU.
  * @sg_cpu: the sugov data for the cpu to boost
  * @time: the update time from the caller
- * @util: the utilization to (eventually) boost
- * @max: the maximum value the utilization can be boosted to
  *
  * A CPU running a task which woken up after an IO operation can have its
  * utilization boosted to speed up the completion of those IO operations.
@@ -377,18 +375,17 @@ static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
  * This mechanism is designed to boost high frequently IO waiting tasks, while
  * being more conservative on tasks which does sporadic IO operations.
  */
-static unsigned long sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time,
-                                       unsigned long util, unsigned long max)
+static void sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time)
 {
        unsigned long boost;
 
        /* No boost currently required */
        if (!sg_cpu->iowait_boost)
-               return util;
+               return;
 
        /* Reset boost if the CPU appears to have been idle enough */
        if (sugov_iowait_reset(sg_cpu, time, false))
-               return util;
+               return;
 
        if (!sg_cpu->iowait_boost_pending) {
                /*
@@ -397,18 +394,19 @@ static unsigned long sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time,
                sg_cpu->iowait_boost >>= 1;
                if (sg_cpu->iowait_boost < IOWAIT_BOOST_MIN) {
                        sg_cpu->iowait_boost = 0;
-                       return util;
+                       return;
                }
        }
 
        sg_cpu->iowait_boost_pending = false;
 
        /*
-        * @util is already in capacity scale; convert iowait_boost
+        * sg_cpu->util is already in capacity scale; convert iowait_boost
         * into the same scale so we can compare.
         */
-       boost = (sg_cpu->iowait_boost * max) >> SCHED_CAPACITY_SHIFT;
-       return max(boost, util);
+       boost = (sg_cpu->iowait_boost * sg_cpu->max) >> SCHED_CAPACITY_SHIFT;
+       if (sg_cpu->util < boost)
+               sg_cpu->util = boost;
 }
 
 #ifdef CONFIG_NO_HZ_COMMON
@@ -434,14 +432,10 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu, struct sugov_p
                sg_policy->limits_changed = true;
 }
 
-static void sugov_update_single(struct update_util_data *hook, u64 time,
-                               unsigned int flags)
+static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu,
+                                             u64 time, unsigned int flags)
 {
-       struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
        struct sugov_policy *sg_policy = sg_cpu->sg_policy;
-       unsigned long util, max;
-       unsigned int next_f;
-       unsigned int cached_freq = sg_policy->cached_raw_freq;
 
        sugov_iowait_boost(sg_cpu, time, flags);
        sg_cpu->last_update = time;
@@ -449,12 +443,26 @@ static void sugov_update_single(struct update_util_data *hook, u64 time,
        ignore_dl_rate_limit(sg_cpu, sg_policy);
 
        if (!sugov_should_update_freq(sg_policy, time))
+               return false;
+
+       sugov_get_util(sg_cpu);
+       sugov_iowait_apply(sg_cpu, time);
+
+       return true;
+}
+
+static void sugov_update_single_freq(struct update_util_data *hook, u64 time,
+                                    unsigned int flags)
+{
+       struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
+       struct sugov_policy *sg_policy = sg_cpu->sg_policy;
+       unsigned int cached_freq = sg_policy->cached_raw_freq;
+       unsigned int next_f;
+
+       if (!sugov_update_single_common(sg_cpu, time, flags))
                return;
 
-       util = sugov_get_util(sg_cpu);
-       max = sg_cpu->max;
-       util = sugov_iowait_apply(sg_cpu, time, util, max);
-       next_f = get_next_freq(sg_policy, util, max);
+       next_f = get_next_freq(sg_policy, sg_cpu->util, sg_cpu->max);
        /*
         * Do not reduce the frequency if the CPU has not been idle
         * recently, as the reduction is likely to be premature then.
@@ -480,6 +488,38 @@ static void sugov_update_single(struct update_util_data *hook, u64 time,
        }
 }
 
+static void sugov_update_single_perf(struct update_util_data *hook, u64 time,
+                                    unsigned int flags)
+{
+       struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
+       unsigned long prev_util = sg_cpu->util;
+
+       /*
+        * Fall back to the "frequency" path if frequency invariance is not
+        * supported, because the direct mapping between the utilization and
+        * the performance levels depends on the frequency invariance.
+        */
+       if (!arch_scale_freq_invariant()) {
+               sugov_update_single_freq(hook, time, flags);
+               return;
+       }
+
+       if (!sugov_update_single_common(sg_cpu, time, flags))
+               return;
+
+       /*
+        * Do not reduce the target performance level if the CPU has not been
+        * idle recently, as the reduction is likely to be premature then.
+        */
+       if (sugov_cpu_is_busy(sg_cpu) && sg_cpu->util < prev_util)
+               sg_cpu->util = prev_util;
+
+       cpufreq_driver_adjust_perf(sg_cpu->cpu, map_util_perf(sg_cpu->bw_dl),
+                                  map_util_perf(sg_cpu->util), sg_cpu->max);
+
+       sg_cpu->sg_policy->last_freq_update_time = time;
+}
+
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
 {
        struct sugov_policy *sg_policy = sg_cpu->sg_policy;
@@ -491,9 +531,10 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
                struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
                unsigned long j_util, j_max;
 
-               j_util = sugov_get_util(j_sg_cpu);
+               sugov_get_util(j_sg_cpu);
+               sugov_iowait_apply(j_sg_cpu, time);
+               j_util = j_sg_cpu->util;
                j_max = j_sg_cpu->max;
-               j_util = sugov_iowait_apply(j_sg_cpu, time, j_util, j_max);
 
                if (j_util * max > j_max * util) {
                        util = j_util;
@@ -817,6 +858,7 @@ static void sugov_exit(struct cpufreq_policy *policy)
 static int sugov_start(struct cpufreq_policy *policy)
 {
        struct sugov_policy *sg_policy = policy->governor_data;
+       void (*uu)(struct update_util_data *data, u64 time, unsigned int flags);
        unsigned int cpu;
 
        sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
@@ -836,13 +878,17 @@ static int sugov_start(struct cpufreq_policy *policy)
                sg_cpu->sg_policy               = sg_policy;
        }
 
+       if (policy_is_shared(policy))
+               uu = sugov_update_shared;
+       else if (policy->fast_switch_enabled && cpufreq_driver_has_adjust_perf())
+               uu = sugov_update_single_perf;
+       else
+               uu = sugov_update_single_freq;
+
        for_each_cpu(cpu, policy->cpus) {
                struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
 
-               cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
-                                            policy_is_shared(policy) ?
-                                                       sugov_update_shared :
-                                                       sugov_update_single);
+               cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, uu);
        }
        return 0;
 }