Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[safe/jmp/linux-2.6] / drivers / cpufreq / cpufreq_ondemand.c
index e741c33..4b34ade 100644 (file)
@@ -32,6 +32,7 @@
 #define DEF_FREQUENCY_UP_THRESHOLD             (80)
 #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL      (3)
 #define MICRO_FREQUENCY_UP_THRESHOLD           (95)
+#define MICRO_FREQUENCY_MIN_SAMPLE_RATE                (10000)
 #define MIN_FREQUENCY_UP_THRESHOLD             (11)
 #define MAX_FREQUENCY_UP_THRESHOLD             (100)
 
  * 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 TRANSITION_LATENCY_LIMIT               (10 * 1000 * 1000)
 
 static void do_dbs_timer(struct work_struct *work);
+static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
+                               unsigned int event);
+
+#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
+static
+#endif
+struct cpufreq_governor cpufreq_gov_ondemand = {
+       .name                   = "ondemand",
+       .governor               = cpufreq_governor_dbs,
+       .max_transition_latency = TRANSITION_LATENCY_LIMIT,
+       .owner                  = THIS_MODULE,
+};
 
 /* Sampling types */
 enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
@@ -84,23 +82,21 @@ struct cpu_dbs_info_s {
        unsigned int freq_lo_jiffies;
        unsigned int freq_hi_jiffies;
        int cpu;
-       unsigned int enable:1,
-               sample_type:1;
+       unsigned int sample_type: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, od_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
- * DEADLOCK ALERT! (2) : do_dbs_timer() must not take the dbs_mutex, because it
- * would deadlock with cancel_delayed_work_sync(), which is needed for proper
- * raceless workqueue teardown.
+ * 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);
 
@@ -137,9 +133,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)
@@ -165,7 +161,8 @@ static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
        unsigned int freq_hi, freq_lo;
        unsigned int index = 0;
        unsigned int jiffies_total, jiffies_hi, jiffies_lo;
-       struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
+       struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
+                                                  policy->cpu);
 
        if (!dbs_info->freq_table) {
                dbs_info->freq_lo = 0;
@@ -206,45 +203,39 @@ static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
        return freq_hi;
 }
 
+static void ondemand_powersave_bias_init_cpu(int cpu)
+{
+       struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
+       dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
+       dbs_info->freq_lo = 0;
+}
+
 static void ondemand_powersave_bias_init(void)
 {
        int i;
        for_each_online_cpu(i) {
-               struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
-               dbs_info->freq_table = cpufreq_frequency_get_table(i);
-               dbs_info->freq_lo = 0;
+               ondemand_powersave_bias_init_cpu(i);
        }
 }
 
 /************************** 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: ondemand sampling_rate_max "
-                      "sysfs file is deprecated - used by: %s\n",
-                      current->comm);
-               print_once = 1;
-       }
-       return sprintf(buf, "%u\n", MAX_SAMPLING_RATE);
+static ssize_t show_sampling_rate_max(struct kobject *kobj,
+                                     struct attribute *attr, char *buf)
+{
+       printk_once(KERN_INFO "CPUFREQ: ondemand 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 ssize_t show_sampling_rate_min(struct kobject *kobj,
+                                     struct attribute *attr, char *buf)
 {
-       static int print_once;
-
-       if (!print_once) {
-               printk(KERN_INFO "CPUFREQ: ondemand sampling_rate_min "
-                      "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)           \
-static struct freq_attr _name =                \
+static struct global_attr _name =      \
 __ATTR(_name, 0444, show_##_name, NULL)
 
 define_one_ro(sampling_rate_max);
@@ -253,7 +244,7 @@ define_one_ro(sampling_rate_min);
 /* cpufreq_ondemand Governor Tunables */
 #define show_one(file_name, object)                                    \
 static ssize_t show_##file_name                                                \
-(struct cpufreq_policy *unused, char *buf)                             \
+(struct kobject *kobj, struct attribute *attr, char *buf)              \
 {                                                                      \
        return sprintf(buf, "%u\n", dbs_tuners_ins.object);             \
 }
@@ -262,46 +253,73 @@ show_one(up_threshold, up_threshold);
 show_one(ignore_nice_load, ignore_nice);
 show_one(powersave_bias, powersave_bias);
 
-static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
-               const char *buf, size_t count)
+/*** delete after deprecation time ***/
+
+#define DEPRECATION_MSG(file_name)                                     \
+       printk_once(KERN_INFO "CPUFREQ: Per core ondemand sysfs "       \
+                   "interface is deprecated - " #file_name "\n");
+
+#define show_one_old(file_name)                                                \
+static ssize_t show_##file_name##_old                                  \
+(struct cpufreq_policy *unused, char *buf)                             \
+{                                                                      \
+       printk_once(KERN_INFO "CPUFREQ: Per core ondemand sysfs "       \
+                   "interface is deprecated - " #file_name "\n");      \
+       return show_##file_name(NULL, NULL, buf);                       \
+}
+show_one_old(sampling_rate);
+show_one_old(up_threshold);
+show_one_old(ignore_nice_load);
+show_one_old(powersave_bias);
+show_one_old(sampling_rate_min);
+show_one_old(sampling_rate_max);
+
+#define define_one_ro_old(object, _name)       \
+static struct freq_attr object =               \
+__ATTR(_name, 0444, show_##_name##_old, NULL)
+
+define_one_ro_old(sampling_rate_min_old, sampling_rate_min);
+define_one_ro_old(sampling_rate_max_old, sampling_rate_max);
+
+/*** delete after deprecation time ***/
+
+static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
+                                  const char *buf, size_t count)
 {
        unsigned int input;
        int ret;
        ret = sscanf(buf, "%u", &input);
+       if (ret != 1)
+               return -EINVAL;
 
        mutex_lock(&dbs_mutex);
-       if (ret != 1) {
-               mutex_unlock(&dbs_mutex);
-               return -EINVAL;
-       }
-       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;
 }
 
-static ssize_t store_up_threshold(struct cpufreq_policy *unused,
-               const char *buf, size_t count)
+static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
+                                 const char *buf, size_t count)
 {
        unsigned int input;
        int ret;
        ret = sscanf(buf, "%u", &input);
 
-       mutex_lock(&dbs_mutex);
        if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
                        input < MIN_FREQUENCY_UP_THRESHOLD) {
-               mutex_unlock(&dbs_mutex);
                return -EINVAL;
        }
 
+       mutex_lock(&dbs_mutex);
        dbs_tuners_ins.up_threshold = input;
        mutex_unlock(&dbs_mutex);
 
        return count;
 }
 
-static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
-               const char *buf, size_t count)
+static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
+                                     const char *buf, size_t count)
 {
        unsigned int input;
        int ret;
@@ -325,7 +343,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(od_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)
@@ -337,8 +355,8 @@ static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
        return count;
 }
 
-static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
-               const char *buf, size_t count)
+static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
+                                   const char *buf, size_t count)
 {
        unsigned int input;
        int ret;
@@ -359,7 +377,7 @@ static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
 }
 
 #define define_one_rw(_name) \
-static struct freq_attr _name = \
+static struct global_attr _name = \
 __ATTR(_name, 0644, show_##_name, store_##_name)
 
 define_one_rw(sampling_rate);
@@ -382,6 +400,47 @@ static struct attribute_group dbs_attr_group = {
        .name = "ondemand",
 };
 
+/*** delete after deprecation time ***/
+
+#define write_one_old(file_name)                                       \
+static ssize_t store_##file_name##_old                                 \
+(struct cpufreq_policy *unused, const char *buf, size_t count)         \
+{                                                                      \
+       printk_once(KERN_INFO "CPUFREQ: Per core ondemand sysfs "       \
+                  "interface is deprecated - " #file_name "\n");       \
+       return store_##file_name(NULL, NULL, buf, count);               \
+}
+write_one_old(sampling_rate);
+write_one_old(up_threshold);
+write_one_old(ignore_nice_load);
+write_one_old(powersave_bias);
+
+#define define_one_rw_old(object, _name)       \
+static struct freq_attr object =               \
+__ATTR(_name, 0644, show_##_name##_old, store_##_name##_old)
+
+define_one_rw_old(sampling_rate_old, sampling_rate);
+define_one_rw_old(up_threshold_old, up_threshold);
+define_one_rw_old(ignore_nice_load_old, ignore_nice_load);
+define_one_rw_old(powersave_bias_old, powersave_bias);
+
+static struct attribute *dbs_attributes_old[] = {
+       &sampling_rate_max_old.attr,
+       &sampling_rate_min_old.attr,
+       &sampling_rate_old.attr,
+       &up_threshold_old.attr,
+       &ignore_nice_load_old.attr,
+       &powersave_bias_old.attr,
+       NULL
+};
+
+static struct attribute_group dbs_attr_group_old = {
+       .attrs = dbs_attributes_old,
+       .name = "ondemand",
+};
+
+/*** delete after deprecation time ***/
+
 /************************** sysfs end ************************/
 
 static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
@@ -391,9 +450,6 @@ static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
        struct cpufreq_policy *policy;
        unsigned int j;
 
-       if (!this_dbs_info->enable)
-               return;
-
        this_dbs_info->freq_lo = 0;
        policy = this_dbs_info->cur_policy;
 
@@ -419,7 +475,7 @@ static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
                unsigned int load, load_freq;
                int freq_avg;
 
-               j_dbs_info = &per_cpu(cpu_dbs_info, j);
+               j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
 
                cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
 
@@ -521,14 +577,7 @@ static void do_dbs_timer(struct work_struct *work)
        int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
 
        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);
 
        /* Common NORMAL_SAMPLE setup */
        dbs_info->sample_type = DBS_NORMAL_SAMPLE;
@@ -545,7 +594,7 @@ static void do_dbs_timer(struct work_struct *work)
                        dbs_info->freq_lo, CPUFREQ_RELATION_H);
        }
        queue_delayed_work_on(cpu, kondemand_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)
@@ -554,8 +603,6 @@ static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
        int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
        delay -= jiffies % delay;
 
-       dbs_info->enable = 1;
-       ondemand_powersave_bias_init();
        dbs_info->sample_type = DBS_NORMAL_SAMPLE;
        INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
        queue_delayed_work_on(dbs_info->cpu, kondemand_wq, &dbs_info->work,
@@ -564,7 +611,6 @@ 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_sync(&dbs_info->work);
 }
 
@@ -576,29 +622,25 @@ 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(od_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);
-               dbs_enable++;
 
-               rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
+               rc = sysfs_create_group(&policy->kobj, &dbs_attr_group_old);
                if (rc) {
-                       dbs_enable--;
                        mutex_unlock(&dbs_mutex);
                        return rc;
                }
 
+               dbs_enable++;
                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(od_cpu_dbs_info, j);
                        j_dbs_info->cur_policy = policy;
 
                        j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
@@ -609,61 +651,66 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
                        }
                }
                this_dbs_info->cpu = cpu;
+               ondemand_powersave_bias_init_cpu(cpu);
                /*
                 * Start the timerschedule work, when this governor
                 * is used for first time
                 */
                if (dbs_enable == 1) {
                        unsigned int latency;
+
+                       rc = sysfs_create_group(cpufreq_global_kobject,
+                                               &dbs_attr_group);
+                       if (rc) {
+                               mutex_unlock(&dbs_mutex);
+                               return rc;
+                       }
+
                        /* policy latency is in nS. Convert it to uS first */
                        latency = policy->cpuinfo.transition_latency / 1000;
                        if (latency == 0)
                                latency = 1;
-
-                       def_sampling_rate =
-                               max(latency * LATENCY_MULTIPLIER,
-                                   MIN_STAT_SAMPLING_RATE);
-
-                       dbs_tuners_ins.sampling_rate = def_sampling_rate;
+                       /* 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);
                }
-               dbs_timer_init(this_dbs_info);
-
                mutex_unlock(&dbs_mutex);
+
+               mutex_init(&this_dbs_info->timer_mutex);
+               dbs_timer_init(this_dbs_info);
                break;
 
        case CPUFREQ_GOV_STOP:
-               mutex_lock(&dbs_mutex);
                dbs_timer_exit(this_dbs_info);
-               sysfs_remove_group(&policy->kobj, &dbs_attr_group);
+
+               mutex_lock(&dbs_mutex);
+               sysfs_remove_group(&policy->kobj, &dbs_attr_group_old);
+               mutex_destroy(&this_dbs_info->timer_mutex);
                dbs_enable--;
                mutex_unlock(&dbs_mutex);
+               if (!dbs_enable)
+                       sysfs_remove_group(cpufreq_global_kobject,
+                                          &dbs_attr_group);
 
                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,
                                policy->max, CPUFREQ_RELATION_H);
                else if (policy->min > this_dbs_info->cur_policy->cur)
                        __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;
        }
        return 0;
 }
 
-#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
-static
-#endif
-struct cpufreq_governor cpufreq_gov_ondemand = {
-       .name                   = "ondemand",
-       .governor               = cpufreq_governor_dbs,
-       .max_transition_latency = TRANSITION_LATENCY_LIMIT,
-       .owner                  = THIS_MODULE,
-};
-
 static int __init cpufreq_gov_dbs_init(void)
 {
        int err;
@@ -678,6 +725,16 @@ static int __init cpufreq_gov_dbs_init(void)
                dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
                dbs_tuners_ins.down_differential =
                                        MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
+               /*
+                * In no_hz/micro accounting case we set the minimum frequency
+                * not depending on HZ, but fixed (very low). The deferred
+                * timer might skip some samples if idle/sleeping as needed.
+               */
+               min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
+       } else {
+               /* For correct statistics, we need 10 ticks for each measure */
+               min_sampling_rate =
+                       MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
        }
 
        kondemand_wq = create_workqueue("kondemand");