Merge branch 'next-devicetree' of git://git.secretlab.ca/git/linux-2.6
[safe/jmp/linux-2.6] / drivers / cpufreq / cpufreq_conservative.c
index 2ecd95e..c7b081b 100644 (file)
  * this governor will not work.
  * All times here are in uS.
  */
-static unsigned int def_sampling_rate;
 #define MIN_SAMPLING_RATE_RATIO                        (2)
-/* for correct statistics, we need at least 10 ticks between each measure */
-#define MIN_STAT_SAMPLING_RATE                         \
-                       (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
-#define MIN_SAMPLING_RATE                      \
-                       (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
-/* Above MIN_SAMPLING_RATE will vanish with its sysfs file soon
- * Define the minimal settable sampling rate to the greater of:
- *   - "HW transition latency" * 100 (same as default sampling / 10)
- *   - MIN_STAT_SAMPLING_RATE
- * To avoid that userspace shoots itself.
-*/
-static unsigned int minimum_sampling_rate(void)
-{
-       return max(def_sampling_rate / 10, MIN_STAT_SAMPLING_RATE);
-}
 
-/* This will also vanish soon with removing sampling_rate_max */
-#define MAX_SAMPLING_RATE                      (500 * def_sampling_rate)
+static unsigned int min_sampling_rate;
+
 #define LATENCY_MULTIPLIER                     (1000)
+#define MIN_LATENCY_MULTIPLIER                 (100)
 #define DEF_SAMPLING_DOWN_FACTOR               (1)
 #define MAX_SAMPLING_DOWN_FACTOR               (10)
 #define TRANSITION_LATENCY_LIMIT               (10 * 1000 * 1000)
@@ -79,18 +64,20 @@ struct cpu_dbs_info_s {
        unsigned int requested_freq;
        int cpu;
        unsigned int enable:1;
+       /*
+        * percpu mutex that serializes governor limit change with
+        * do_dbs_timer invocation. We do not want do_dbs_timer to run
+        * when user is changing the governor or limits.
+        */
+       struct mutex timer_mutex;
 };
-static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
+static DEFINE_PER_CPU(struct cpu_dbs_info_s, cs_cpu_dbs_info);
 
 static unsigned int dbs_enable;        /* number of CPUs using this policy */
 
 /*
- * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
- * lock and dbs_mutex. cpu_hotplug lock should always be held before
- * dbs_mutex. If any function that can potentially take cpu_hotplug lock
- * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
- * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
- * is recursive for the same process. -Venki
+ * dbs_mutex protects data in dbs_tuners_ins from concurrent changes on
+ * different CPUs. It protects dbs_enable in governor start/stop.
  */
 static DEFINE_MUTEX(dbs_mutex);
 
@@ -129,9 +116,9 @@ static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
 
        idle_time = cputime64_sub(cur_wall_time, busy_time);
        if (wall)
-               *wall = cur_wall_time;
+               *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
 
-       return idle_time;
+       return (cputime64_t)jiffies_to_usecs(idle_time);;
 }
 
 static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
@@ -150,7 +137,7 @@ dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
                     void *data)
 {
        struct cpufreq_freqs *freq = data;
-       struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info,
+       struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cs_cpu_dbs_info,
                                                        freq->cpu);
 
        struct cpufreq_policy *policy;
@@ -179,27 +166,14 @@ static struct notifier_block dbs_cpufreq_notifier_block = {
 /************************** sysfs interface ************************/
 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
 {
-       static int print_once;
-
-       if (!print_once) {
-               printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
-                      "sysfs file is deprecated - used by: %s\n",
-                      current->comm);
-               print_once = 1;
-       }
-       return sprintf(buf, "%u\n", MAX_SAMPLING_RATE);
+       printk_once(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
+                   "sysfs file is deprecated - used by: %s\n", current->comm);
+       return sprintf(buf, "%u\n", -1U);
 }
 
 static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
 {
-       static int print_once;
-
-       if (!print_once) {
-               printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
-                      "sysfs file is deprecated - used by: %s\n", current->comm);
-               print_once = 1;
-       }
-       return sprintf(buf, "%u\n", MIN_SAMPLING_RATE);
+       return sprintf(buf, "%u\n", min_sampling_rate);
 }
 
 #define define_one_ro(_name)           \
@@ -251,7 +225,7 @@ static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
                return -EINVAL;
 
        mutex_lock(&dbs_mutex);
-       dbs_tuners_ins.sampling_rate = max(input, minimum_sampling_rate());
+       dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
        mutex_unlock(&dbs_mutex);
 
        return count;
@@ -323,7 +297,7 @@ static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
        /* we need to re-evaluate prev_cpu_idle */
        for_each_online_cpu(j) {
                struct cpu_dbs_info_s *dbs_info;
-               dbs_info = &per_cpu(cpu_dbs_info, j);
+               dbs_info = &per_cpu(cs_cpu_dbs_info, j);
                dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
                                                &dbs_info->prev_cpu_wall);
                if (dbs_tuners_ins.ignore_nice)
@@ -413,7 +387,7 @@ static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
                cputime64_t cur_wall_time, cur_idle_time;
                unsigned int idle_time, wall_time;
 
-               j_dbs_info = &per_cpu(cpu_dbs_info, j);
+               j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
 
                cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
 
@@ -513,18 +487,12 @@ static void do_dbs_timer(struct work_struct *work)
 
        delay -= jiffies % delay;
 
-       if (lock_policy_rwsem_write(cpu) < 0)
-               return;
-
-       if (!dbs_info->enable) {
-               unlock_policy_rwsem_write(cpu);
-               return;
-       }
+       mutex_lock(&dbs_info->timer_mutex);
 
        dbs_check_cpu(dbs_info);
 
        queue_delayed_work_on(cpu, kconservative_wq, &dbs_info->work, delay);
-       unlock_policy_rwsem_write(cpu);
+       mutex_unlock(&dbs_info->timer_mutex);
 }
 
 static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
@@ -542,7 +510,7 @@ static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
 static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
 {
        dbs_info->enable = 0;
-       cancel_delayed_work(&dbs_info->work);
+       cancel_delayed_work_sync(&dbs_info->work);
 }
 
 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
@@ -553,16 +521,13 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
        unsigned int j;
        int rc;
 
-       this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
+       this_dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
 
        switch (event) {
        case CPUFREQ_GOV_START:
                if ((!cpu_online(cpu)) || (!policy->cur))
                        return -EINVAL;
 
-               if (this_dbs_info->enable) /* Already enabled */
-                       break;
-
                mutex_lock(&dbs_mutex);
 
                rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
@@ -573,7 +538,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
 
                for_each_cpu(j, policy->cpus) {
                        struct cpu_dbs_info_s *j_dbs_info;
-                       j_dbs_info = &per_cpu(cpu_dbs_info, j);
+                       j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
                        j_dbs_info->cur_policy = policy;
 
                        j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
@@ -586,6 +551,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                this_dbs_info->down_skip = 0;
                this_dbs_info->requested_freq = policy->cur;
 
+               mutex_init(&this_dbs_info->timer_mutex);
                dbs_enable++;
                /*
                 * Start the timerschedule work, when this governor
@@ -598,27 +564,36 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                        if (latency == 0)
                                latency = 1;
 
-                       def_sampling_rate =
-                               max(latency * LATENCY_MULTIPLIER,
-                                   MIN_STAT_SAMPLING_RATE);
-
-                       dbs_tuners_ins.sampling_rate = def_sampling_rate;
+                       /*
+                        * conservative does not implement micro like ondemand
+                        * governor, thus we are bound to jiffes/HZ
+                        */
+                       min_sampling_rate =
+                               MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
+                       /* Bring kernel and HW constraints together */
+                       min_sampling_rate = max(min_sampling_rate,
+                                       MIN_LATENCY_MULTIPLIER * latency);
+                       dbs_tuners_ins.sampling_rate =
+                               max(min_sampling_rate,
+                                   latency * LATENCY_MULTIPLIER);
 
                        cpufreq_register_notifier(
                                        &dbs_cpufreq_notifier_block,
                                        CPUFREQ_TRANSITION_NOTIFIER);
                }
-               dbs_timer_init(this_dbs_info);
-
                mutex_unlock(&dbs_mutex);
 
+               dbs_timer_init(this_dbs_info);
+
                break;
 
        case CPUFREQ_GOV_STOP:
-               mutex_lock(&dbs_mutex);
                dbs_timer_exit(this_dbs_info);
+
+               mutex_lock(&dbs_mutex);
                sysfs_remove_group(&policy->kobj, &dbs_attr_group);
                dbs_enable--;
+               mutex_destroy(&this_dbs_info->timer_mutex);
 
                /*
                 * Stop the timerschedule work, when this governor
@@ -634,7 +609,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                break;
 
        case CPUFREQ_GOV_LIMITS:
-               mutex_lock(&dbs_mutex);
+               mutex_lock(&this_dbs_info->timer_mutex);
                if (policy->max < this_dbs_info->cur_policy->cur)
                        __cpufreq_driver_target(
                                        this_dbs_info->cur_policy,
@@ -643,7 +618,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                        __cpufreq_driver_target(
                                        this_dbs_info->cur_policy,
                                        policy->min, CPUFREQ_RELATION_L);
-               mutex_unlock(&dbs_mutex);
+               mutex_unlock(&this_dbs_info->timer_mutex);
 
                break;
        }