Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.git] / drivers / cpufreq / cpufreq_interactive.c
index a0a7c3aef859a4ac003dd8a91ace0fb96ab21169..0be66df4a6e6b5d47fb80768367fe45e7424d87c 100644 (file)
@@ -47,9 +47,10 @@ struct cpufreq_interactive_cpuinfo {
        spinlock_t target_freq_lock; /*protects target freq */
        unsigned int target_freq;
        unsigned int floor_freq;
-       unsigned int max_freq;
-       u64 floor_validate_time;
-       u64 hispeed_validate_time;
+       u64 pol_floor_val_time; /* policy floor_validate_time */
+       u64 loc_floor_val_time; /* per-cpu floor_validate_time */
+       u64 pol_hispeed_val_time; /* policy hispeed_validate_time */
+       u64 loc_hispeed_val_time; /* per-cpu hispeed_validate_time */
        struct rw_semaphore enable_sem;
        int governor_enabled;
 };
@@ -105,6 +106,7 @@ struct cpufreq_interactive_tunables {
        int boostpulse_duration_val;
        /* End time of boost pulse in ktime converted to usecs */
        u64 boostpulse_endtime;
+       bool boosted;
        /*
         * Max additional time to wait in idle, beyond timer_rate, at speeds
         * above minimum before wakeup to reduce speed, or -1 if unnecessary.
@@ -114,6 +116,28 @@ struct cpufreq_interactive_tunables {
        bool io_is_busy;
 };
 
+/*
+ * HACK: FIXME: Bring back cpufreq_{get,put}_global_kobject()
+ * definition removed by upstream commit 8eec1020f0c0 "cpufreq:
+ * create cpu/cpufreq at boot time" to fix build failures.
+ */
+static int cpufreq_global_kobject_usage;
+
+int cpufreq_get_global_kobject(void)
+{
+       if (!cpufreq_global_kobject_usage++)
+               return kobject_add(cpufreq_global_kobject,
+                               &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
+
+       return 0;
+}
+
+void cpufreq_put_global_kobject(void)
+{
+       if (!--cpufreq_global_kobject_usage)
+               kobject_del(cpufreq_global_kobject);
+}
+
 /* For cases where we have single governor instance for system */
 static struct cpufreq_interactive_tunables *common_tunables;
 
@@ -344,7 +368,7 @@ static void cpufreq_interactive_timer(unsigned long data)
        unsigned int loadadjfreq;
        unsigned int index;
        unsigned long flags;
-       bool boosted;
+       u64 max_fvtime;
 
        if (!down_read_trylock(&pcpu->enable_sem))
                return;
@@ -363,11 +387,11 @@ static void cpufreq_interactive_timer(unsigned long data)
        spin_lock_irqsave(&pcpu->target_freq_lock, flags);
        do_div(cputime_speedadj, delta_time);
        loadadjfreq = (unsigned int)cputime_speedadj * 100;
-       cpu_load = loadadjfreq / pcpu->target_freq;
-       boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
+       cpu_load = loadadjfreq / pcpu->policy->cur;
+       tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
 
-       if (cpu_load >= tunables->go_hispeed_load || boosted) {
-               if (pcpu->target_freq < tunables->hispeed_freq) {
+       if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
+               if (pcpu->policy->cur < tunables->hispeed_freq) {
                        new_freq = tunables->hispeed_freq;
                } else {
                        new_freq = choose_freq(pcpu, loadadjfreq);
@@ -378,14 +402,14 @@ static void cpufreq_interactive_timer(unsigned long data)
        } else {
                new_freq = choose_freq(pcpu, loadadjfreq);
                if (new_freq > tunables->hispeed_freq &&
-                               pcpu->target_freq < tunables->hispeed_freq)
+                               pcpu->policy->cur < tunables->hispeed_freq)
                        new_freq = tunables->hispeed_freq;
        }
 
-       if (pcpu->target_freq >= tunables->hispeed_freq &&
-           new_freq > pcpu->target_freq &&
-           now - pcpu->hispeed_validate_time <
-           freq_to_above_hispeed_delay(tunables, pcpu->target_freq)) {
+       if (pcpu->policy->cur >= tunables->hispeed_freq &&
+           new_freq > pcpu->policy->cur &&
+           now - pcpu->pol_hispeed_val_time <
+           freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) {
                trace_cpufreq_interactive_notyet(
                        data, cpu_load, pcpu->target_freq,
                        pcpu->policy->cur, new_freq);
@@ -393,7 +417,7 @@ static void cpufreq_interactive_timer(unsigned long data)
                goto rearm;
        }
 
-       pcpu->hispeed_validate_time = now;
+       pcpu->loc_hispeed_val_time = now;
 
        if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
                                           new_freq, CPUFREQ_RELATION_L,
@@ -408,9 +432,10 @@ static void cpufreq_interactive_timer(unsigned long data)
         * Do not scale below floor_freq unless we have been at or above the
         * floor frequency for the minimum sample time since last validated.
         */
-       if (new_freq < pcpu->floor_freq) {
-               if (now - pcpu->floor_validate_time <
-                               tunables->min_sample_time) {
+       max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time);
+       if (new_freq < pcpu->floor_freq &&
+           pcpu->target_freq >= pcpu->policy->cur) {
+               if (now - max_fvtime < tunables->min_sample_time) {
                        trace_cpufreq_interactive_notyet(
                                data, cpu_load, pcpu->target_freq,
                                pcpu->policy->cur, new_freq);
@@ -427,17 +452,20 @@ static void cpufreq_interactive_timer(unsigned long data)
         * (or the indefinite boost is turned off).
         */
 
-       if (!boosted || new_freq > tunables->hispeed_freq) {
+       if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
                pcpu->floor_freq = new_freq;
-               pcpu->floor_validate_time = now;
+               if (pcpu->target_freq >= pcpu->policy->cur ||
+                   new_freq >= pcpu->policy->cur)
+                       pcpu->loc_floor_val_time = now;
        }
 
-       if (pcpu->target_freq == new_freq) {
+       if (pcpu->target_freq == new_freq &&
+                       pcpu->target_freq <= pcpu->policy->cur) {
                trace_cpufreq_interactive_already(
                        data, cpu_load, pcpu->target_freq,
                        pcpu->policy->cur, new_freq);
                spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
-               goto rearm_if_notmax;
+               goto rearm;
        }
 
        trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
@@ -450,14 +478,6 @@ static void cpufreq_interactive_timer(unsigned long data)
        spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
        wake_up_process(speedchange_task);
 
-rearm_if_notmax:
-       /*
-        * Already set max speed and don't see a need to change that,
-        * wait until next idle to re-evaluate, don't need timer.
-        */
-       if (pcpu->target_freq == pcpu->policy->max)
-               goto exit;
-
 rearm:
        if (!timer_pending(&pcpu->cpu_timer))
                cpufreq_interactive_timer_resched(pcpu);
@@ -467,37 +487,6 @@ exit:
        return;
 }
 
-static void cpufreq_interactive_idle_start(void)
-{
-       struct cpufreq_interactive_cpuinfo *pcpu =
-               &per_cpu(cpuinfo, smp_processor_id());
-       int pending;
-
-       if (!down_read_trylock(&pcpu->enable_sem))
-               return;
-       if (!pcpu->governor_enabled) {
-               up_read(&pcpu->enable_sem);
-               return;
-       }
-
-       pending = timer_pending(&pcpu->cpu_timer);
-
-       if (pcpu->target_freq != pcpu->policy->min) {
-               /*
-                * Entering idle while not at lowest speed.  On some
-                * platforms this can hold the other CPU(s) at that speed
-                * even though the CPU is idle. Set a timer to re-evaluate
-                * speed so this idle CPU doesn't hold the other CPUs above
-                * min indefinitely.  This should probably be a quirk of
-                * the CPUFreq driver.
-                */
-               if (!pending)
-                       cpufreq_interactive_timer_resched(pcpu);
-       }
-
-       up_read(&pcpu->enable_sem);
-}
-
 static void cpufreq_interactive_idle_end(void)
 {
        struct cpufreq_interactive_cpuinfo *pcpu =
@@ -552,6 +541,8 @@ static int cpufreq_interactive_speedchange_task(void *data)
                for_each_cpu(cpu, &tmp_mask) {
                        unsigned int j;
                        unsigned int max_freq = 0;
+                       struct cpufreq_interactive_cpuinfo *pjcpu;
+                       u64 hvt = ~0ULL, fvt = 0;
 
                        pcpu = &per_cpu(cpuinfo, cpu);
                        if (!down_read_trylock(&pcpu->enable_sem))
@@ -562,17 +553,30 @@ static int cpufreq_interactive_speedchange_task(void *data)
                        }
 
                        for_each_cpu(j, pcpu->policy->cpus) {
-                               struct cpufreq_interactive_cpuinfo *pjcpu =
-                                       &per_cpu(cpuinfo, j);
+                               pjcpu = &per_cpu(cpuinfo, j);
 
-                               if (pjcpu->target_freq > max_freq)
+                               fvt = max(fvt, pjcpu->loc_floor_val_time);
+                               if (pjcpu->target_freq > max_freq) {
                                        max_freq = pjcpu->target_freq;
+                                       hvt = pjcpu->loc_hispeed_val_time;
+                               } else if (pjcpu->target_freq == max_freq) {
+                                       hvt = min(hvt, pjcpu->loc_hispeed_val_time);
+                               }
+                       }
+                       for_each_cpu(j, pcpu->policy->cpus) {
+                               pjcpu = &per_cpu(cpuinfo, j);
+                               pjcpu->pol_floor_val_time = fvt;
                        }
 
-                       if (max_freq != pcpu->policy->cur)
+                       if (max_freq != pcpu->policy->cur) {
                                __cpufreq_driver_target(pcpu->policy,
                                                        max_freq,
                                                        CPUFREQ_RELATION_H);
+                               for_each_cpu(j, pcpu->policy->cpus) {
+                                       pjcpu = &per_cpu(cpuinfo, j);
+                                       pjcpu->pol_hispeed_val_time = hvt;
+                               }
+                       }
                        trace_cpufreq_interactive_setspeed(cpu,
                                                     pcpu->target_freq,
                                                     pcpu->policy->cur);
@@ -584,36 +588,30 @@ static int cpufreq_interactive_speedchange_task(void *data)
        return 0;
 }
 
-static void cpufreq_interactive_boost(void)
+static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
 {
        int i;
        int anyboost = 0;
        unsigned long flags[2];
        struct cpufreq_interactive_cpuinfo *pcpu;
-       struct cpufreq_interactive_tunables *tunables;
+
+       tunables->boosted = true;
 
        spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
 
        for_each_online_cpu(i) {
                pcpu = &per_cpu(cpuinfo, i);
-               tunables = pcpu->policy->governor_data;
+               if (tunables != pcpu->policy->governor_data)
+                       continue;
 
                spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
                if (pcpu->target_freq < tunables->hispeed_freq) {
                        pcpu->target_freq = tunables->hispeed_freq;
                        cpumask_set_cpu(i, &speedchange_cpumask);
-                       pcpu->hispeed_validate_time =
+                       pcpu->pol_hispeed_val_time =
                                ktime_to_us(ktime_get());
                        anyboost = 1;
                }
-
-               /*
-                * Set floor freq and (re)start timer for when last
-                * validated.
-                */
-
-               pcpu->floor_freq = tunables->hispeed_freq;
-               pcpu->floor_validate_time = ktime_to_us(ktime_get());
                spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
        }
 
@@ -805,7 +803,7 @@ static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
        int ret;
        long unsigned int val;
 
-       ret = strict_strtoul(buf, 0, &val);
+       ret = kstrtoul(buf, 0, &val);
        if (ret < 0)
                return ret;
        tunables->hispeed_freq = val;
@@ -824,7 +822,7 @@ static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
        int ret;
        unsigned long val;
 
-       ret = strict_strtoul(buf, 0, &val);
+       ret = kstrtoul(buf, 0, &val);
        if (ret < 0)
                return ret;
        tunables->go_hispeed_load = val;
@@ -843,7 +841,7 @@ static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
        int ret;
        unsigned long val;
 
-       ret = strict_strtoul(buf, 0, &val);
+       ret = kstrtoul(buf, 0, &val);
        if (ret < 0)
                return ret;
        tunables->min_sample_time = val;
@@ -860,12 +858,18 @@ static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
                const char *buf, size_t count)
 {
        int ret;
-       unsigned long val;
+       unsigned long val, val_round;
 
-       ret = strict_strtoul(buf, 0, &val);
+       ret = kstrtoul(buf, 0, &val);
        if (ret < 0)
                return ret;
-       tunables->timer_rate = val;
+
+       val_round = jiffies_to_usecs(usecs_to_jiffies(val));
+       if (val != val_round)
+               pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n",
+                       val_round);
+
+       tunables->timer_rate = val_round;
        return count;
 }
 
@@ -909,7 +913,8 @@ static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
 
        if (tunables->boost_val) {
                trace_cpufreq_interactive_boost("on");
-               cpufreq_interactive_boost();
+               if (!tunables->boosted)
+                       cpufreq_interactive_boost(tunables);
        } else {
                tunables->boostpulse_endtime = ktime_to_us(ktime_get());
                trace_cpufreq_interactive_unboost("off");
@@ -931,7 +936,8 @@ static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
        tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
                tunables->boostpulse_duration_val;
        trace_cpufreq_interactive_boost("pulse");
-       cpufreq_interactive_boost();
+       if (!tunables->boosted)
+               cpufreq_interactive_boost(tunables);
        return count;
 }
 
@@ -1105,14 +1111,8 @@ static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
                                             unsigned long val,
                                             void *data)
 {
-       switch (val) {
-       case IDLE_START:
-               cpufreq_interactive_idle_start();
-               break;
-       case IDLE_END:
+       if (val == IDLE_END)
                cpufreq_interactive_idle_end();
-               break;
-       }
 
        return 0;
 }
@@ -1180,8 +1180,10 @@ static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
                if (rc) {
                        kfree(tunables);
                        policy->governor_data = NULL;
-                       if (!have_governor_per_policy())
+                       if (!have_governor_per_policy()) {
                                common_tunables = NULL;
+                               cpufreq_put_global_kobject();
+                       }
                        return rc;
                }
 
@@ -1227,11 +1229,11 @@ static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
                        pcpu->target_freq = policy->cur;
                        pcpu->freq_table = freq_table;
                        pcpu->floor_freq = pcpu->target_freq;
-                       pcpu->floor_validate_time =
+                       pcpu->pol_floor_val_time =
                                ktime_to_us(ktime_get());
-                       pcpu->hispeed_validate_time =
-                               pcpu->floor_validate_time;
-                       pcpu->max_freq = policy->max;
+                       pcpu->loc_floor_val_time = pcpu->pol_floor_val_time;
+                       pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time;
+                       pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time;
                        down_write(&pcpu->enable_sem);
                        del_timer_sync(&pcpu->cpu_timer);
                        del_timer_sync(&pcpu->cpu_slack_timer);
@@ -1281,23 +1283,6 @@ static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
 
                        spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
                        up_read(&pcpu->enable_sem);
-
-                       /* Reschedule timer only if policy->max is raised.
-                        * Delete the timers, else the timer callback may
-                        * return without re-arm the timer when failed
-                        * acquire the semaphore. This race may cause timer
-                        * stopped unexpectedly.
-                        */
-
-                       if (policy->max > pcpu->max_freq) {
-                               down_write(&pcpu->enable_sem);
-                               del_timer_sync(&pcpu->cpu_timer);
-                               del_timer_sync(&pcpu->cpu_slack_timer);
-                               cpufreq_interactive_timer_start(tunables, j);
-                               up_write(&pcpu->enable_sem);
-                       }
-
-                       pcpu->max_freq = policy->max;
                }
                break;
        }