Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_ondemand.c |
| 3 | * |
| 4 | * Copyright (C) 2001 Russell King |
| 5 | * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. |
| 6 | * Jun Nakajima <jun.nakajima@intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/cpufreq.h> |
Andrew Morton | 138a0128 | 2006-06-23 03:31:19 -0700 | [diff] [blame] | 17 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/kernel_stat.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 20 | #include <linux/mutex.h> |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 21 | #include <linux/hrtimer.h> |
| 22 | #include <linux/tick.h> |
| 23 | #include <linux/ktime.h> |
Thomas Renninger | 9411b4e | 2009-02-04 11:54:04 +0100 | [diff] [blame] | 24 | #include <linux/sched.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | #include <linux/input.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * dbs is used in this file as a shortform for demandbased switching |
| 31 | * It helps to keep variable names smaller, simpler |
| 32 | */ |
| 33 | |
venkatesh.pallipadi@intel.com | e9d95bf | 2008-08-04 11:59:10 -0700 | [diff] [blame] | 34 | #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 36 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
| 37 | #define MAX_SAMPLING_DOWN_FACTOR (100000) |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 38 | #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3) |
| 39 | #define MICRO_FREQUENCY_UP_THRESHOLD (95) |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 40 | #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000) |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 41 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 43 | #define MIN_FREQUENCY_DOWN_DIFFERENTIAL (1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 45 | /* |
| 46 | * The polling frequency of this governor depends on the capability of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * the processor. Default polling frequency is 1000 times the transition |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 48 | * latency of the processor. The governor will work on any processor with |
| 49 | * transition latency <= 10mS, using appropriate sampling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * rate. |
| 51 | * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL) |
| 52 | * this governor will not work. |
| 53 | * All times here are in uS. |
| 54 | */ |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 55 | #define MIN_SAMPLING_RATE_RATIO (2) |
Thomas Renninger | 112124a | 2009-02-04 11:55:12 +0100 | [diff] [blame] | 56 | |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 57 | static unsigned int min_sampling_rate; |
| 58 | |
Thomas Renninger | 112124a | 2009-02-04 11:55:12 +0100 | [diff] [blame] | 59 | #define LATENCY_MULTIPLIER (1000) |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 60 | #define MIN_LATENCY_MULTIPLIER (100) |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame] | 61 | #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 63 | #define POWERSAVE_BIAS_MAXLEVEL (1000) |
| 64 | #define POWERSAVE_BIAS_MINLEVEL (-1000) |
| 65 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 66 | static void do_dbs_timer(struct work_struct *work); |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 67 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 68 | unsigned int event); |
| 69 | |
| 70 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
| 71 | static |
| 72 | #endif |
| 73 | struct cpufreq_governor cpufreq_gov_ondemand = { |
| 74 | .name = "ondemand", |
| 75 | .governor = cpufreq_governor_dbs, |
| 76 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, |
| 77 | .owner = THIS_MODULE, |
| 78 | }; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 79 | |
| 80 | /* Sampling types */ |
Venkatesh Pallipadi | 529af7a | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 81 | enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | struct cpu_dbs_info_s { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 84 | cputime64_t prev_cpu_idle; |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 85 | cputime64_t prev_cpu_iowait; |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 86 | cputime64_t prev_cpu_wall; |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 87 | cputime64_t prev_cpu_nice; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 88 | struct cpufreq_policy *cur_policy; |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 89 | struct delayed_work work; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 90 | struct cpufreq_frequency_table *freq_table; |
| 91 | unsigned int freq_lo; |
| 92 | unsigned int freq_lo_jiffies; |
| 93 | unsigned int freq_hi_jiffies; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 94 | unsigned int rate_mult; |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 95 | unsigned int prev_load; |
| 96 | unsigned int max_load; |
Venkatesh Pallipadi | 529af7a | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 97 | int cpu; |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 98 | unsigned int sample_type:1; |
| 99 | /* |
| 100 | * percpu mutex that serializes governor limit change with |
| 101 | * do_dbs_timer invocation. We do not want do_dbs_timer to run |
| 102 | * when user is changing the governor or limits. |
| 103 | */ |
| 104 | struct mutex timer_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 106 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 108 | static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info); |
| 109 | static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info); |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | static unsigned int dbs_enable; /* number of CPUs using this policy */ |
| 112 | |
Venkatesh Pallipadi | 4ec223d | 2006-06-21 15:18:34 -0700 | [diff] [blame] | 113 | /* |
Thomas Renninger | 326c86d | 2011-03-03 21:31:27 +0100 | [diff] [blame] | 114 | * dbs_mutex protects dbs_enable in governor start/stop. |
Venkatesh Pallipadi | 4ec223d | 2006-06-21 15:18:34 -0700 | [diff] [blame] | 115 | */ |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 116 | static DEFINE_MUTEX(dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 118 | static struct workqueue_struct *input_wq; |
| 119 | |
Stephen Boyd | c8fc301 | 2012-10-31 17:43:08 -0700 | [diff] [blame] | 120 | struct dbs_work_struct { |
| 121 | struct work_struct work; |
| 122 | unsigned int cpu; |
| 123 | }; |
| 124 | |
| 125 | static DEFINE_PER_CPU(struct dbs_work_struct, dbs_refresh_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 127 | static struct dbs_tuners { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 128 | unsigned int sampling_rate; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 129 | unsigned int up_threshold; |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 130 | unsigned int up_threshold_multi_core; |
venkatesh.pallipadi@intel.com | e9d95bf | 2008-08-04 11:59:10 -0700 | [diff] [blame] | 131 | unsigned int down_differential; |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 132 | unsigned int down_differential_multi_core; |
| 133 | unsigned int optimal_freq; |
| 134 | unsigned int up_threshold_any_cpu_load; |
| 135 | unsigned int sync_freq; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 136 | unsigned int ignore_nice; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 137 | unsigned int sampling_down_factor; |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 138 | int powersave_bias; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 139 | unsigned int io_is_busy; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 140 | } dbs_tuners_ins = { |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 141 | .up_threshold_multi_core = DEF_FREQUENCY_UP_THRESHOLD, |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 142 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 143 | .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, |
venkatesh.pallipadi@intel.com | e9d95bf | 2008-08-04 11:59:10 -0700 | [diff] [blame] | 144 | .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL, |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 145 | .down_differential_multi_core = MICRO_FREQUENCY_DOWN_DIFFERENTIAL, |
| 146 | .up_threshold_any_cpu_load = DEF_FREQUENCY_UP_THRESHOLD, |
Eric Piel | 9cbad61 | 2006-03-10 11:35:27 +0200 | [diff] [blame] | 147 | .ignore_nice = 0, |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 148 | .powersave_bias = 0, |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 149 | .sync_freq = 0, |
| 150 | .optimal_freq = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 153 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 154 | { |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 155 | u64 idle_time; |
Martin Schwidefsky | 612ef28 | 2011-12-19 19:23:15 +0100 | [diff] [blame] | 156 | u64 cur_wall_time; |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 157 | u64 busy_time; |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 158 | |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 159 | cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 160 | |
Martin Schwidefsky | 612ef28 | 2011-12-19 19:23:15 +0100 | [diff] [blame] | 161 | busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; |
| 162 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 163 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; |
| 164 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; |
| 165 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; |
| 166 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 167 | |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 168 | idle_time = cur_wall_time - busy_time; |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 169 | if (wall) |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 170 | *wall = jiffies_to_usecs(cur_wall_time); |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 171 | |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 172 | return jiffies_to_usecs(idle_time); |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 173 | } |
| 174 | |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 175 | static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall) |
| 176 | { |
Michal Hocko | 6beea0c | 2011-08-24 09:37:48 +0200 | [diff] [blame] | 177 | u64 idle_time = get_cpu_idle_time_us(cpu, NULL); |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 178 | |
| 179 | if (idle_time == -1ULL) |
| 180 | return get_cpu_idle_time_jiffy(cpu, wall); |
Michal Hocko | 6beea0c | 2011-08-24 09:37:48 +0200 | [diff] [blame] | 181 | else |
| 182 | idle_time += get_cpu_iowait_time_us(cpu, wall); |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 183 | |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 184 | return idle_time; |
| 185 | } |
| 186 | |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 187 | static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall) |
| 188 | { |
| 189 | u64 iowait_time = get_cpu_iowait_time_us(cpu, wall); |
| 190 | |
| 191 | if (iowait_time == -1ULL) |
| 192 | return 0; |
| 193 | |
| 194 | return iowait_time; |
| 195 | } |
| 196 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 197 | /* |
| 198 | * Find right freq to be set now with powersave_bias on. |
| 199 | * Returns the freq_hi to be used right now and will set freq_hi_jiffies, |
| 200 | * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs. |
| 201 | */ |
Adrian Bunk | b5ecf60 | 2006-08-13 23:00:08 +0200 | [diff] [blame] | 202 | static unsigned int powersave_bias_target(struct cpufreq_policy *policy, |
| 203 | unsigned int freq_next, |
| 204 | unsigned int relation) |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 205 | { |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 206 | unsigned int freq_req, freq_avg; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 207 | unsigned int freq_hi, freq_lo; |
| 208 | unsigned int index = 0; |
| 209 | unsigned int jiffies_total, jiffies_hi, jiffies_lo; |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 210 | int freq_reduc; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 211 | struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
| 212 | policy->cpu); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 213 | |
| 214 | if (!dbs_info->freq_table) { |
| 215 | dbs_info->freq_lo = 0; |
| 216 | dbs_info->freq_lo_jiffies = 0; |
| 217 | return freq_next; |
| 218 | } |
| 219 | |
| 220 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next, |
| 221 | relation, &index); |
| 222 | freq_req = dbs_info->freq_table[index].frequency; |
| 223 | freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000; |
| 224 | freq_avg = freq_req - freq_reduc; |
| 225 | |
| 226 | /* Find freq bounds for freq_avg in freq_table */ |
| 227 | index = 0; |
| 228 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, |
| 229 | CPUFREQ_RELATION_H, &index); |
| 230 | freq_lo = dbs_info->freq_table[index].frequency; |
| 231 | index = 0; |
| 232 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, |
| 233 | CPUFREQ_RELATION_L, &index); |
| 234 | freq_hi = dbs_info->freq_table[index].frequency; |
| 235 | |
| 236 | /* Find out how long we have to be in hi and lo freqs */ |
| 237 | if (freq_hi == freq_lo) { |
| 238 | dbs_info->freq_lo = 0; |
| 239 | dbs_info->freq_lo_jiffies = 0; |
| 240 | return freq_lo; |
| 241 | } |
| 242 | jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
| 243 | jiffies_hi = (freq_avg - freq_lo) * jiffies_total; |
| 244 | jiffies_hi += ((freq_hi - freq_lo) / 2); |
| 245 | jiffies_hi /= (freq_hi - freq_lo); |
| 246 | jiffies_lo = jiffies_total - jiffies_hi; |
| 247 | dbs_info->freq_lo = freq_lo; |
| 248 | dbs_info->freq_lo_jiffies = jiffies_lo; |
| 249 | dbs_info->freq_hi_jiffies = jiffies_hi; |
| 250 | return freq_hi; |
| 251 | } |
| 252 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 253 | static int ondemand_powersave_bias_setspeed(struct cpufreq_policy *policy, |
| 254 | struct cpufreq_policy *altpolicy, |
| 255 | int level) |
| 256 | { |
| 257 | if (level == POWERSAVE_BIAS_MAXLEVEL) { |
| 258 | /* maximum powersave; set to lowest frequency */ |
| 259 | __cpufreq_driver_target(policy, |
| 260 | (altpolicy) ? altpolicy->min : policy->min, |
| 261 | CPUFREQ_RELATION_L); |
| 262 | return 1; |
| 263 | } else if (level == POWERSAVE_BIAS_MINLEVEL) { |
| 264 | /* minimum powersave; set to highest frequency */ |
| 265 | __cpufreq_driver_target(policy, |
| 266 | (altpolicy) ? altpolicy->max : policy->max, |
| 267 | CPUFREQ_RELATION_H); |
| 268 | return 1; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 273 | static void ondemand_powersave_bias_init_cpu(int cpu) |
| 274 | { |
Tejun Heo | 384be2b | 2009-08-14 14:41:02 +0900 | [diff] [blame] | 275 | struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 276 | dbs_info->freq_table = cpufreq_frequency_get_table(cpu); |
| 277 | dbs_info->freq_lo = 0; |
| 278 | } |
| 279 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 280 | static void ondemand_powersave_bias_init(void) |
| 281 | { |
| 282 | int i; |
| 283 | for_each_online_cpu(i) { |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 284 | ondemand_powersave_bias_init_cpu(i); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | /************************** sysfs interface ************************/ |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 289 | |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 290 | static ssize_t show_sampling_rate_min(struct kobject *kobj, |
| 291 | struct attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 293 | return sprintf(buf, "%u\n", min_sampling_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Borislav Petkov | 6dad2a2 | 2010-03-31 21:56:46 +0200 | [diff] [blame] | 296 | define_one_global_ro(sampling_rate_min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | /* cpufreq_ondemand Governor Tunables */ |
| 299 | #define show_one(file_name, object) \ |
| 300 | static ssize_t show_##file_name \ |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 301 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { \ |
| 303 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ |
| 304 | } |
| 305 | show_one(sampling_rate, sampling_rate); |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 306 | show_one(io_is_busy, io_is_busy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | show_one(up_threshold, up_threshold); |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 308 | show_one(up_threshold_multi_core, up_threshold_multi_core); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 309 | show_one(down_differential, down_differential); |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 310 | show_one(sampling_down_factor, sampling_down_factor); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 311 | show_one(ignore_nice_load, ignore_nice); |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 312 | show_one(optimal_freq, optimal_freq); |
| 313 | show_one(up_threshold_any_cpu_load, up_threshold_any_cpu_load); |
| 314 | show_one(sync_freq, sync_freq); |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 315 | |
| 316 | static ssize_t show_powersave_bias |
| 317 | (struct kobject *kobj, struct attribute *attr, char *buf) |
| 318 | { |
| 319 | return snprintf(buf, PAGE_SIZE, "%d\n", dbs_tuners_ins.powersave_bias); |
| 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 322 | /** |
| 323 | * update_sampling_rate - update sampling rate effective immediately if needed. |
| 324 | * @new_rate: new sampling rate |
| 325 | * |
| 326 | * If new rate is smaller than the old, simply updaing |
| 327 | * dbs_tuners_int.sampling_rate might not be appropriate. For example, |
| 328 | * if the original sampling_rate was 1 second and the requested new sampling |
| 329 | * rate is 10 ms because the user needs immediate reaction from ondemand |
| 330 | * governor, but not sure if higher frequency will be required or not, |
| 331 | * then, the governor may change the sampling rate too late; up to 1 second |
| 332 | * later. Thus, if we are reducing the sampling rate, we need to make the |
| 333 | * new value effective immediately. |
| 334 | */ |
| 335 | static void update_sampling_rate(unsigned int new_rate) |
| 336 | { |
| 337 | int cpu; |
| 338 | |
| 339 | dbs_tuners_ins.sampling_rate = new_rate |
| 340 | = max(new_rate, min_sampling_rate); |
| 341 | |
| 342 | for_each_online_cpu(cpu) { |
| 343 | struct cpufreq_policy *policy; |
| 344 | struct cpu_dbs_info_s *dbs_info; |
| 345 | unsigned long next_sampling, appointed_at; |
| 346 | |
| 347 | policy = cpufreq_cpu_get(cpu); |
| 348 | if (!policy) |
| 349 | continue; |
| 350 | dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu); |
| 351 | cpufreq_cpu_put(policy); |
| 352 | |
| 353 | mutex_lock(&dbs_info->timer_mutex); |
| 354 | |
| 355 | if (!delayed_work_pending(&dbs_info->work)) { |
| 356 | mutex_unlock(&dbs_info->timer_mutex); |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | next_sampling = jiffies + usecs_to_jiffies(new_rate); |
| 361 | appointed_at = dbs_info->work.timer.expires; |
| 362 | |
| 363 | |
| 364 | if (time_before(next_sampling, appointed_at)) { |
| 365 | |
| 366 | mutex_unlock(&dbs_info->timer_mutex); |
| 367 | cancel_delayed_work_sync(&dbs_info->work); |
| 368 | mutex_lock(&dbs_info->timer_mutex); |
| 369 | |
| 370 | schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, |
| 371 | usecs_to_jiffies(new_rate)); |
| 372 | |
| 373 | } |
| 374 | mutex_unlock(&dbs_info->timer_mutex); |
| 375 | } |
| 376 | } |
| 377 | |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 378 | static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b, |
| 379 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | unsigned int input; |
| 382 | int ret; |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 383 | ret = sscanf(buf, "%u", &input); |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 384 | if (ret != 1) |
| 385 | return -EINVAL; |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 386 | update_sampling_rate(input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return count; |
| 388 | } |
| 389 | |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 390 | static ssize_t store_sync_freq(struct kobject *a, struct attribute *b, |
| 391 | const char *buf, size_t count) |
| 392 | { |
| 393 | unsigned int input; |
| 394 | int ret; |
| 395 | |
| 396 | ret = sscanf(buf, "%u", &input); |
| 397 | if (ret != 1) |
| 398 | return -EINVAL; |
| 399 | dbs_tuners_ins.sync_freq = input; |
| 400 | return count; |
| 401 | } |
| 402 | |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 403 | static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b, |
| 404 | const char *buf, size_t count) |
| 405 | { |
| 406 | unsigned int input; |
| 407 | int ret; |
| 408 | |
| 409 | ret = sscanf(buf, "%u", &input); |
| 410 | if (ret != 1) |
| 411 | return -EINVAL; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 412 | dbs_tuners_ins.io_is_busy = !!input; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 413 | return count; |
| 414 | } |
| 415 | |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 416 | static ssize_t store_optimal_freq(struct kobject *a, struct attribute *b, |
| 417 | const char *buf, size_t count) |
| 418 | { |
| 419 | unsigned int input; |
| 420 | int ret; |
| 421 | |
| 422 | ret = sscanf(buf, "%u", &input); |
| 423 | if (ret != 1) |
| 424 | return -EINVAL; |
| 425 | dbs_tuners_ins.optimal_freq = input; |
| 426 | return count; |
| 427 | } |
| 428 | |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 429 | static ssize_t store_up_threshold(struct kobject *a, struct attribute *b, |
| 430 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | unsigned int input; |
| 433 | int ret; |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 434 | ret = sscanf(buf, "%u", &input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 436 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 437 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | dbs_tuners_ins.up_threshold = input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return count; |
| 442 | } |
| 443 | |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 444 | static ssize_t store_up_threshold_multi_core(struct kobject *a, |
| 445 | struct attribute *b, const char *buf, size_t count) |
| 446 | { |
| 447 | unsigned int input; |
| 448 | int ret; |
| 449 | ret = sscanf(buf, "%u", &input); |
| 450 | |
| 451 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
| 452 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | dbs_tuners_ins.up_threshold_multi_core = input; |
| 456 | return count; |
| 457 | } |
| 458 | |
| 459 | static ssize_t store_up_threshold_any_cpu_load(struct kobject *a, |
| 460 | struct attribute *b, const char *buf, size_t count) |
| 461 | { |
| 462 | unsigned int input; |
| 463 | int ret; |
| 464 | ret = sscanf(buf, "%u", &input); |
| 465 | |
| 466 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
| 467 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | dbs_tuners_ins.up_threshold_any_cpu_load = input; |
| 471 | return count; |
| 472 | } |
| 473 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | static ssize_t store_down_differential(struct kobject *a, struct attribute *b, |
| 475 | const char *buf, size_t count) |
| 476 | { |
| 477 | unsigned int input; |
| 478 | int ret; |
| 479 | ret = sscanf(buf, "%u", &input); |
| 480 | |
| 481 | if (ret != 1 || input >= dbs_tuners_ins.up_threshold || |
| 482 | input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) { |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | dbs_tuners_ins.down_differential = input; |
| 487 | |
| 488 | return count; |
| 489 | } |
| 490 | |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 491 | static ssize_t store_sampling_down_factor(struct kobject *a, |
| 492 | struct attribute *b, const char *buf, size_t count) |
| 493 | { |
| 494 | unsigned int input, j; |
| 495 | int ret; |
| 496 | ret = sscanf(buf, "%u", &input); |
| 497 | |
| 498 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
| 499 | return -EINVAL; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 500 | dbs_tuners_ins.sampling_down_factor = input; |
| 501 | |
| 502 | /* Reset down sampling multiplier in case it was active */ |
| 503 | for_each_online_cpu(j) { |
| 504 | struct cpu_dbs_info_s *dbs_info; |
| 505 | dbs_info = &per_cpu(od_cpu_dbs_info, j); |
| 506 | dbs_info->rate_mult = 1; |
| 507 | } |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 508 | return count; |
| 509 | } |
| 510 | |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 511 | static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b, |
| 512 | const char *buf, size_t count) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 513 | { |
| 514 | unsigned int input; |
| 515 | int ret; |
| 516 | |
| 517 | unsigned int j; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 518 | |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 519 | ret = sscanf(buf, "%u", &input); |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 520 | if (ret != 1) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 521 | return -EINVAL; |
| 522 | |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 523 | if (input > 1) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 524 | input = 1; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 525 | |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 526 | if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */ |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 527 | return count; |
| 528 | } |
| 529 | dbs_tuners_ins.ignore_nice = input; |
| 530 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 531 | /* we need to re-evaluate prev_cpu_idle */ |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 532 | for_each_online_cpu(j) { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 533 | struct cpu_dbs_info_s *dbs_info; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 534 | dbs_info = &per_cpu(od_cpu_dbs_info, j); |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 535 | dbs_info->prev_cpu_idle = get_cpu_idle_time(j, |
| 536 | &dbs_info->prev_cpu_wall); |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 537 | if (dbs_tuners_ins.ignore_nice) |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 538 | dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 539 | |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 540 | } |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 541 | return count; |
| 542 | } |
| 543 | |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 544 | static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b, |
| 545 | const char *buf, size_t count) |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 546 | { |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 547 | int input = 0; |
| 548 | int bypass = 0; |
Krishna Vanka | ebf80eb | 2012-04-19 13:11:20 +0530 | [diff] [blame] | 549 | int ret, cpu, reenable_timer, j; |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 550 | struct cpu_dbs_info_s *dbs_info; |
| 551 | |
Krishna Vanka | ebf80eb | 2012-04-19 13:11:20 +0530 | [diff] [blame] | 552 | struct cpumask cpus_timer_done; |
| 553 | cpumask_clear(&cpus_timer_done); |
| 554 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 555 | ret = sscanf(buf, "%d", &input); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 556 | |
| 557 | if (ret != 1) |
| 558 | return -EINVAL; |
| 559 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 560 | if (input >= POWERSAVE_BIAS_MAXLEVEL) { |
| 561 | input = POWERSAVE_BIAS_MAXLEVEL; |
| 562 | bypass = 1; |
| 563 | } else if (input <= POWERSAVE_BIAS_MINLEVEL) { |
| 564 | input = POWERSAVE_BIAS_MINLEVEL; |
| 565 | bypass = 1; |
| 566 | } |
| 567 | |
| 568 | if (input == dbs_tuners_ins.powersave_bias) { |
| 569 | /* no change */ |
| 570 | return count; |
| 571 | } |
| 572 | |
| 573 | reenable_timer = ((dbs_tuners_ins.powersave_bias == |
| 574 | POWERSAVE_BIAS_MAXLEVEL) || |
| 575 | (dbs_tuners_ins.powersave_bias == |
| 576 | POWERSAVE_BIAS_MINLEVEL)); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 577 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 578 | dbs_tuners_ins.powersave_bias = input; |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 579 | if (!bypass) { |
| 580 | if (reenable_timer) { |
| 581 | /* reinstate dbs timer */ |
| 582 | for_each_online_cpu(cpu) { |
| 583 | if (lock_policy_rwsem_write(cpu) < 0) |
| 584 | continue; |
| 585 | |
| 586 | dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Krishna Vanka | ebf80eb | 2012-04-19 13:11:20 +0530 | [diff] [blame] | 587 | |
| 588 | for_each_cpu(j, &cpus_timer_done) { |
| 589 | if (!dbs_info->cur_policy) { |
| 590 | pr_err("Dbs policy is NULL\n"); |
| 591 | goto skip_this_cpu; |
| 592 | } |
| 593 | if (cpumask_test_cpu(j, dbs_info-> |
| 594 | cur_policy->cpus)) |
| 595 | goto skip_this_cpu; |
| 596 | } |
| 597 | |
| 598 | cpumask_set_cpu(cpu, &cpus_timer_done); |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 599 | if (dbs_info->cur_policy) { |
| 600 | /* restart dbs timer */ |
| 601 | dbs_timer_init(dbs_info); |
| 602 | } |
Krishna Vanka | ebf80eb | 2012-04-19 13:11:20 +0530 | [diff] [blame] | 603 | skip_this_cpu: |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 604 | unlock_policy_rwsem_write(cpu); |
| 605 | } |
| 606 | } |
| 607 | ondemand_powersave_bias_init(); |
| 608 | } else { |
| 609 | /* running at maximum or minimum frequencies; cancel |
| 610 | dbs timer as periodic load sampling is not necessary */ |
| 611 | for_each_online_cpu(cpu) { |
| 612 | if (lock_policy_rwsem_write(cpu) < 0) |
| 613 | continue; |
| 614 | |
| 615 | dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Krishna Vanka | ebf80eb | 2012-04-19 13:11:20 +0530 | [diff] [blame] | 616 | |
| 617 | for_each_cpu(j, &cpus_timer_done) { |
| 618 | if (!dbs_info->cur_policy) { |
| 619 | pr_err("Dbs policy is NULL\n"); |
| 620 | goto skip_this_cpu_bypass; |
| 621 | } |
| 622 | if (cpumask_test_cpu(j, dbs_info-> |
| 623 | cur_policy->cpus)) |
| 624 | goto skip_this_cpu_bypass; |
| 625 | } |
| 626 | |
| 627 | cpumask_set_cpu(cpu, &cpus_timer_done); |
| 628 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 629 | if (dbs_info->cur_policy) { |
| 630 | /* cpu using ondemand, cancel dbs timer */ |
| 631 | mutex_lock(&dbs_info->timer_mutex); |
| 632 | dbs_timer_exit(dbs_info); |
| 633 | |
| 634 | ondemand_powersave_bias_setspeed( |
| 635 | dbs_info->cur_policy, |
| 636 | NULL, |
| 637 | input); |
| 638 | |
| 639 | mutex_unlock(&dbs_info->timer_mutex); |
| 640 | } |
Krishna Vanka | ebf80eb | 2012-04-19 13:11:20 +0530 | [diff] [blame] | 641 | skip_this_cpu_bypass: |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 642 | unlock_policy_rwsem_write(cpu); |
| 643 | } |
| 644 | } |
| 645 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 646 | return count; |
| 647 | } |
| 648 | |
Borislav Petkov | 6dad2a2 | 2010-03-31 21:56:46 +0200 | [diff] [blame] | 649 | define_one_global_rw(sampling_rate); |
Linus Torvalds | 07d7775 | 2010-05-18 08:49:13 -0700 | [diff] [blame] | 650 | define_one_global_rw(io_is_busy); |
Borislav Petkov | 6dad2a2 | 2010-03-31 21:56:46 +0200 | [diff] [blame] | 651 | define_one_global_rw(up_threshold); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | define_one_global_rw(down_differential); |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 653 | define_one_global_rw(sampling_down_factor); |
Borislav Petkov | 6dad2a2 | 2010-03-31 21:56:46 +0200 | [diff] [blame] | 654 | define_one_global_rw(ignore_nice_load); |
| 655 | define_one_global_rw(powersave_bias); |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 656 | define_one_global_rw(up_threshold_multi_core); |
| 657 | define_one_global_rw(optimal_freq); |
| 658 | define_one_global_rw(up_threshold_any_cpu_load); |
| 659 | define_one_global_rw(sync_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 661 | static struct attribute *dbs_attributes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | &sampling_rate_min.attr, |
| 663 | &sampling_rate.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | &up_threshold.attr, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 665 | &down_differential.attr, |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 666 | &sampling_down_factor.attr, |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 667 | &ignore_nice_load.attr, |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 668 | &powersave_bias.attr, |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 669 | &io_is_busy.attr, |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 670 | &up_threshold_multi_core.attr, |
| 671 | &optimal_freq.attr, |
| 672 | &up_threshold_any_cpu_load.attr, |
| 673 | &sync_freq.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | NULL |
| 675 | }; |
| 676 | |
| 677 | static struct attribute_group dbs_attr_group = { |
| 678 | .attrs = dbs_attributes, |
| 679 | .name = "ondemand", |
| 680 | }; |
| 681 | |
| 682 | /************************** sysfs end ************************/ |
| 683 | |
Mike Chan | 00e299f | 2010-01-26 17:06:47 -0800 | [diff] [blame] | 684 | static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq) |
| 685 | { |
| 686 | if (dbs_tuners_ins.powersave_bias) |
| 687 | freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H); |
| 688 | else if (p->cur == p->max) |
| 689 | return; |
| 690 | |
| 691 | __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ? |
| 692 | CPUFREQ_RELATION_L : CPUFREQ_RELATION_H); |
| 693 | } |
| 694 | |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 695 | static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
Anitha Anand | cbeef6a | 2012-03-05 18:10:52 -0800 | [diff] [blame] | 697 | /* Extrapolated load of this CPU */ |
| 698 | unsigned int load_at_max_freq = 0; |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 699 | unsigned int max_load_freq; |
Anitha Anand | cbeef6a | 2012-03-05 18:10:52 -0800 | [diff] [blame] | 700 | /* Current load across this CPU */ |
| 701 | unsigned int cur_load = 0; |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 702 | unsigned int max_load_other_cpu = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | struct cpufreq_policy *policy; |
| 704 | unsigned int j; |
| 705 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 706 | this_dbs_info->freq_lo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | policy = this_dbs_info->cur_policy; |
Venki Pallipadi | ea48761 | 2007-06-20 14:26:24 -0700 | [diff] [blame] | 708 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 709 | /* |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 710 | * Every sampling_rate, we check, if current idle time is less |
| 711 | * than 20% (default), then we try to increase frequency |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 712 | * Every sampling_rate, we look for a the lowest |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 713 | * frequency which can sustain the load while keeping idle time over |
| 714 | * 30%. If such a frequency exist, we try to decrease to this frequency. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 716 | * Any frequency increase takes it to the maximum frequency. |
| 717 | * Frequency reduction happens at minimum steps of |
| 718 | * 5% (default) of current frequency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | */ |
| 720 | |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 721 | /* Get Absolute Load - in terms of freq */ |
| 722 | max_load_freq = 0; |
| 723 | |
Rusty Russell | 835481d | 2009-01-04 05:18:06 -0800 | [diff] [blame] | 724 | for_each_cpu(j, policy->cpus) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | struct cpu_dbs_info_s *j_dbs_info; |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 726 | cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time; |
| 727 | unsigned int idle_time, wall_time, iowait_time; |
Anitha Anand | cbeef6a | 2012-03-05 18:10:52 -0800 | [diff] [blame] | 728 | unsigned int load_freq; |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 729 | int freq_avg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 731 | j_dbs_info = &per_cpu(od_cpu_dbs_info, j); |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 732 | |
| 733 | cur_idle_time = get_cpu_idle_time(j, &cur_wall_time); |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 734 | cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time); |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 735 | |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 736 | wall_time = (unsigned int) |
| 737 | (cur_wall_time - j_dbs_info->prev_cpu_wall); |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 738 | j_dbs_info->prev_cpu_wall = cur_wall_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 740 | idle_time = (unsigned int) |
| 741 | (cur_idle_time - j_dbs_info->prev_cpu_idle); |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 742 | j_dbs_info->prev_cpu_idle = cur_idle_time; |
| 743 | |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 744 | iowait_time = (unsigned int) |
| 745 | (cur_iowait_time - j_dbs_info->prev_cpu_iowait); |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 746 | j_dbs_info->prev_cpu_iowait = cur_iowait_time; |
| 747 | |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 748 | if (dbs_tuners_ins.ignore_nice) { |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 749 | u64 cur_nice; |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 750 | unsigned long cur_nice_jiffies; |
| 751 | |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 752 | cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] - |
| 753 | j_dbs_info->prev_cpu_nice; |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 754 | /* |
| 755 | * Assumption: nice time between sampling periods will |
| 756 | * be less than 2^32 jiffies for 32 bit sys |
| 757 | */ |
| 758 | cur_nice_jiffies = (unsigned long) |
| 759 | cputime64_to_jiffies64(cur_nice); |
| 760 | |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 761 | j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 762 | idle_time += jiffies_to_usecs(cur_nice_jiffies); |
| 763 | } |
| 764 | |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 765 | /* |
| 766 | * For the purpose of ondemand, waiting for disk IO is an |
| 767 | * indication that you're performance critical, and not that |
| 768 | * the system is actually idle. So subtract the iowait time |
| 769 | * from the cpu idle time. |
| 770 | */ |
| 771 | |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 772 | if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time) |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 773 | idle_time -= iowait_time; |
| 774 | |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 775 | if (unlikely(!wall_time || wall_time < idle_time)) |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 776 | continue; |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 777 | |
Anitha Anand | cbeef6a | 2012-03-05 18:10:52 -0800 | [diff] [blame] | 778 | cur_load = 100 * (wall_time - idle_time) / wall_time; |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 779 | j_dbs_info->max_load = max(cur_load, j_dbs_info->prev_load); |
| 780 | j_dbs_info->prev_load = cur_load; |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 781 | freq_avg = __cpufreq_driver_getavg(policy, j); |
| 782 | if (freq_avg <= 0) |
| 783 | freq_avg = policy->cur; |
| 784 | |
Anitha Anand | cbeef6a | 2012-03-05 18:10:52 -0800 | [diff] [blame] | 785 | load_freq = cur_load * freq_avg; |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 786 | if (load_freq > max_load_freq) |
| 787 | max_load_freq = load_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | } |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 789 | |
| 790 | for_each_online_cpu(j) { |
| 791 | struct cpu_dbs_info_s *j_dbs_info; |
| 792 | j_dbs_info = &per_cpu(od_cpu_dbs_info, j); |
| 793 | |
| 794 | if (j == policy->cpu) |
| 795 | continue; |
| 796 | |
| 797 | if (max_load_other_cpu < j_dbs_info->max_load) |
| 798 | max_load_other_cpu = j_dbs_info->max_load; |
| 799 | /* |
| 800 | * The other cpu could be running at higher frequency |
| 801 | * but may not have completed it's sampling_down_factor. |
| 802 | * For that case consider other cpu is loaded so that |
| 803 | * frequency imbalance does not occur. |
| 804 | */ |
| 805 | |
| 806 | if ((j_dbs_info->cur_policy != NULL) |
| 807 | && (j_dbs_info->cur_policy->cur == |
| 808 | j_dbs_info->cur_policy->max)) { |
| 809 | |
| 810 | if (policy->cur >= dbs_tuners_ins.optimal_freq) |
| 811 | max_load_other_cpu = |
| 812 | dbs_tuners_ins.up_threshold_any_cpu_load; |
| 813 | } |
| 814 | } |
| 815 | |
Anitha Anand | cbeef6a | 2012-03-05 18:10:52 -0800 | [diff] [blame] | 816 | /* calculate the scaled load across CPU */ |
| 817 | load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq; |
| 818 | |
| 819 | cpufreq_notify_utilization(policy, load_at_max_freq); |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 820 | /* Check for frequency increase */ |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 821 | if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) { |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 822 | /* If switching to max speed, apply sampling_down_factor */ |
| 823 | if (policy->cur < policy->max) |
| 824 | this_dbs_info->rate_mult = |
| 825 | dbs_tuners_ins.sampling_down_factor; |
Mike Chan | 00e299f | 2010-01-26 17:06:47 -0800 | [diff] [blame] | 826 | dbs_freq_increase(policy, policy->max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | return; |
| 828 | } |
| 829 | |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 830 | if (num_online_cpus() > 1) { |
| 831 | |
| 832 | if (max_load_other_cpu > |
| 833 | dbs_tuners_ins.up_threshold_any_cpu_load) { |
| 834 | if (policy->cur < dbs_tuners_ins.sync_freq) |
| 835 | dbs_freq_increase(policy, |
| 836 | dbs_tuners_ins.sync_freq); |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | if (max_load_freq > dbs_tuners_ins.up_threshold_multi_core * |
| 841 | policy->cur) { |
| 842 | if (policy->cur < dbs_tuners_ins.optimal_freq) |
| 843 | dbs_freq_increase(policy, |
| 844 | dbs_tuners_ins.optimal_freq); |
| 845 | return; |
| 846 | } |
| 847 | } |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* Check for frequency decrease */ |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 850 | /* if we cannot reduce the frequency anymore, break out early */ |
| 851 | if (policy->cur == policy->min) |
| 852 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 854 | /* |
| 855 | * The optimal frequency is the frequency that is the lowest that |
| 856 | * can support the current CPU usage without triggering the up |
| 857 | * policy. To be safe, we focus 10 points under the threshold. |
| 858 | */ |
venkatesh.pallipadi@intel.com | e9d95bf | 2008-08-04 11:59:10 -0700 | [diff] [blame] | 859 | if (max_load_freq < |
| 860 | (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) * |
| 861 | policy->cur) { |
venkatesh.pallipadi@intel.com | c43aa3b | 2008-08-04 11:59:08 -0700 | [diff] [blame] | 862 | unsigned int freq_next; |
venkatesh.pallipadi@intel.com | e9d95bf | 2008-08-04 11:59:10 -0700 | [diff] [blame] | 863 | freq_next = max_load_freq / |
| 864 | (dbs_tuners_ins.up_threshold - |
| 865 | dbs_tuners_ins.down_differential); |
Venkatesh Pallipadi | dfde5d6 | 2006-10-03 12:38:45 -0700 | [diff] [blame] | 866 | |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 867 | /* No longer fully busy, reset rate_mult */ |
| 868 | this_dbs_info->rate_mult = 1; |
| 869 | |
Nagananda.Chumbalkar@hp.com | 1dbf588 | 2009-12-21 23:40:52 +0100 | [diff] [blame] | 870 | if (freq_next < policy->min) |
| 871 | freq_next = policy->min; |
| 872 | |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 873 | if (num_online_cpus() > 1) { |
| 874 | if (max_load_other_cpu > |
| 875 | (dbs_tuners_ins.up_threshold_multi_core - |
| 876 | dbs_tuners_ins.down_differential) && |
| 877 | freq_next < dbs_tuners_ins.sync_freq) |
| 878 | freq_next = dbs_tuners_ins.sync_freq; |
| 879 | |
| 880 | if (max_load_freq > |
| 881 | (dbs_tuners_ins.up_threshold_multi_core - |
| 882 | dbs_tuners_ins.down_differential_multi_core) * |
| 883 | policy->cur) |
| 884 | freq_next = dbs_tuners_ins.optimal_freq; |
| 885 | |
| 886 | } |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 887 | if (!dbs_tuners_ins.powersave_bias) { |
| 888 | __cpufreq_driver_target(policy, freq_next, |
| 889 | CPUFREQ_RELATION_L); |
| 890 | } else { |
| 891 | int freq = powersave_bias_target(policy, freq_next, |
| 892 | CPUFREQ_RELATION_L); |
| 893 | __cpufreq_driver_target(policy, freq, |
| 894 | CPUFREQ_RELATION_L); |
| 895 | } |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 896 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 899 | static void do_dbs_timer(struct work_struct *work) |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 900 | { |
Venkatesh Pallipadi | 529af7a | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 901 | struct cpu_dbs_info_s *dbs_info = |
| 902 | container_of(work, struct cpu_dbs_info_s, work.work); |
| 903 | unsigned int cpu = dbs_info->cpu; |
| 904 | int sample_type = dbs_info->sample_type; |
| 905 | |
Vincent Guittot | 5cb2c3b | 2011-02-07 17:14:25 +0100 | [diff] [blame] | 906 | int delay; |
Jocelyn Falempe | a665df9 | 2010-03-11 14:01:11 -0800 | [diff] [blame] | 907 | |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 908 | mutex_lock(&dbs_info->timer_mutex); |
Venkatesh Pallipadi | 56463b7 | 2007-02-05 16:12:45 -0800 | [diff] [blame] | 909 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 910 | /* Common NORMAL_SAMPLE setup */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 911 | dbs_info->sample_type = DBS_NORMAL_SAMPLE; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 912 | if (!dbs_tuners_ins.powersave_bias || |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 913 | sample_type == DBS_NORMAL_SAMPLE) { |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 914 | dbs_check_cpu(dbs_info); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 915 | if (dbs_info->freq_lo) { |
| 916 | /* Setup timer for SUB_SAMPLE */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 917 | dbs_info->sample_type = DBS_SUB_SAMPLE; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 918 | delay = dbs_info->freq_hi_jiffies; |
Vincent Guittot | 5cb2c3b | 2011-02-07 17:14:25 +0100 | [diff] [blame] | 919 | } else { |
| 920 | /* We want all CPUs to do sampling nearly on |
| 921 | * same jiffy |
| 922 | */ |
| 923 | delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate |
| 924 | * dbs_info->rate_mult); |
| 925 | |
| 926 | if (num_online_cpus() > 1) |
| 927 | delay -= jiffies % delay; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 928 | } |
| 929 | } else { |
| 930 | __cpufreq_driver_target(dbs_info->cur_policy, |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 931 | dbs_info->freq_lo, CPUFREQ_RELATION_H); |
Vincent Guittot | 5cb2c3b | 2011-02-07 17:14:25 +0100 | [diff] [blame] | 932 | delay = dbs_info->freq_lo_jiffies; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 933 | } |
Tejun Heo | 57df557 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 934 | schedule_delayed_work_on(cpu, &dbs_info->work, delay); |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 935 | mutex_unlock(&dbs_info->timer_mutex); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 936 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Venkatesh Pallipadi | 529af7a | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 938 | static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | { |
Alexey Starikovskiy | 1ce28d6 | 2006-07-31 22:25:20 +0400 | [diff] [blame] | 940 | /* We want all CPUs to do sampling nearly on same jiffy */ |
| 941 | int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
Jocelyn Falempe | a665df9 | 2010-03-11 14:01:11 -0800 | [diff] [blame] | 942 | |
| 943 | if (num_online_cpus() > 1) |
| 944 | delay -= jiffies % delay; |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 945 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 946 | dbs_info->sample_type = DBS_NORMAL_SAMPLE; |
Venki Pallipadi | 2828703 | 2007-05-08 00:27:47 -0700 | [diff] [blame] | 947 | INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer); |
Tejun Heo | 57df557 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 948 | schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 951 | static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | { |
Mathieu Desnoyers | b14893a | 2009-05-17 10:30:45 -0400 | [diff] [blame] | 953 | cancel_delayed_work_sync(&dbs_info->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 956 | /* |
| 957 | * Not all CPUs want IO time to be accounted as busy; this dependson how |
| 958 | * efficient idling at a higher frequency/voltage is. |
| 959 | * Pavel Machek says this is not so for various generations of AMD and old |
| 960 | * Intel systems. |
| 961 | * Mike Chan (androidlcom) calis this is also not true for ARM. |
| 962 | * Because of this, whitelist specific known (series) of CPUs by default, and |
| 963 | * leave all others up to the user. |
| 964 | */ |
| 965 | static int should_io_be_busy(void) |
| 966 | { |
| 967 | #if defined(CONFIG_X86) |
| 968 | /* |
| 969 | * For Intel, Core 2 (model 15) andl later have an efficient idle. |
| 970 | */ |
| 971 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && |
| 972 | boot_cpu_data.x86 == 6 && |
| 973 | boot_cpu_data.x86_model >= 15) |
| 974 | return 1; |
| 975 | #endif |
| 976 | return 0; |
| 977 | } |
| 978 | |
Stephen Boyd | c8fc301 | 2012-10-31 17:43:08 -0700 | [diff] [blame] | 979 | static void dbs_refresh_callback(struct work_struct *work) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 980 | { |
| 981 | struct cpufreq_policy *policy; |
| 982 | struct cpu_dbs_info_s *this_dbs_info; |
Stephen Boyd | c8fc301 | 2012-10-31 17:43:08 -0700 | [diff] [blame] | 983 | struct dbs_work_struct *dbs_work; |
| 984 | unsigned int cpu; |
| 985 | |
| 986 | dbs_work = container_of(work, struct dbs_work_struct, work); |
| 987 | cpu = dbs_work->cpu; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 988 | |
Krishna Vanka | a3e04d8 | 2012-06-08 11:35:43 +0530 | [diff] [blame] | 989 | get_online_cpus(); |
| 990 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 991 | if (lock_policy_rwsem_write(cpu) < 0) |
Krishna Vanka | a3e04d8 | 2012-06-08 11:35:43 +0530 | [diff] [blame] | 992 | goto bail_acq_sema_failed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 993 | |
| 994 | this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
| 995 | policy = this_dbs_info->cur_policy; |
David Ng | 4a0a023 | 2011-08-03 14:04:43 -0700 | [diff] [blame] | 996 | if (!policy) { |
| 997 | /* CPU not using ondemand governor */ |
Krishna Vanka | a3e04d8 | 2012-06-08 11:35:43 +0530 | [diff] [blame] | 998 | goto bail_incorrect_governor; |
David Ng | 4a0a023 | 2011-08-03 14:04:43 -0700 | [diff] [blame] | 999 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (policy->cur < policy->max) { |
Anji Jonnala | f873232 | 2012-12-13 14:03:54 +0530 | [diff] [blame] | 1002 | /* |
| 1003 | * Arch specific cpufreq driver may fail. |
| 1004 | * Don't update governor frequency upon failure. |
| 1005 | */ |
| 1006 | if (__cpufreq_driver_target(policy, policy->max, |
| 1007 | CPUFREQ_RELATION_L) >= 0) |
| 1008 | policy->cur = policy->max; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1009 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1010 | this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu, |
| 1011 | &this_dbs_info->prev_cpu_wall); |
| 1012 | } |
Krishna Vanka | a3e04d8 | 2012-06-08 11:35:43 +0530 | [diff] [blame] | 1013 | |
| 1014 | bail_incorrect_governor: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1015 | unlock_policy_rwsem_write(cpu); |
Krishna Vanka | a3e04d8 | 2012-06-08 11:35:43 +0530 | [diff] [blame] | 1016 | |
| 1017 | bail_acq_sema_failed: |
| 1018 | put_online_cpus(); |
| 1019 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static void dbs_input_event(struct input_handle *handle, unsigned int type, |
| 1023 | unsigned int code, int value) |
| 1024 | { |
Matt Wagantall | 2100f00 | 2012-10-19 15:26:48 -0700 | [diff] [blame] | 1025 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1026 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 1027 | if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) || |
| 1028 | (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) { |
| 1029 | /* nothing to do */ |
| 1030 | return; |
| 1031 | } |
| 1032 | |
Stephen Boyd | c8fc301 | 2012-10-31 17:43:08 -0700 | [diff] [blame] | 1033 | for_each_online_cpu(i) |
| 1034 | queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i).work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static int dbs_input_connect(struct input_handler *handler, |
| 1038 | struct input_dev *dev, const struct input_device_id *id) |
| 1039 | { |
| 1040 | struct input_handle *handle; |
| 1041 | int error; |
| 1042 | |
| 1043 | handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); |
| 1044 | if (!handle) |
| 1045 | return -ENOMEM; |
| 1046 | |
| 1047 | handle->dev = dev; |
| 1048 | handle->handler = handler; |
| 1049 | handle->name = "cpufreq"; |
| 1050 | |
| 1051 | error = input_register_handle(handle); |
| 1052 | if (error) |
| 1053 | goto err2; |
| 1054 | |
| 1055 | error = input_open_device(handle); |
| 1056 | if (error) |
| 1057 | goto err1; |
| 1058 | |
| 1059 | return 0; |
| 1060 | err1: |
| 1061 | input_unregister_handle(handle); |
| 1062 | err2: |
| 1063 | kfree(handle); |
| 1064 | return error; |
| 1065 | } |
| 1066 | |
| 1067 | static void dbs_input_disconnect(struct input_handle *handle) |
| 1068 | { |
| 1069 | input_close_device(handle); |
| 1070 | input_unregister_handle(handle); |
| 1071 | kfree(handle); |
| 1072 | } |
| 1073 | |
| 1074 | static const struct input_device_id dbs_ids[] = { |
| 1075 | { .driver_info = 1 }, |
| 1076 | { }, |
| 1077 | }; |
| 1078 | |
| 1079 | static struct input_handler dbs_input_handler = { |
| 1080 | .event = dbs_input_event, |
| 1081 | .connect = dbs_input_connect, |
| 1082 | .disconnect = dbs_input_disconnect, |
| 1083 | .name = "cpufreq_ond", |
| 1084 | .id_table = dbs_ids, |
| 1085 | }; |
| 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 1088 | unsigned int event) |
| 1089 | { |
| 1090 | unsigned int cpu = policy->cpu; |
| 1091 | struct cpu_dbs_info_s *this_dbs_info; |
| 1092 | unsigned int j; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 1093 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1095 | this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | switch (event) { |
| 1098 | case CPUFREQ_GOV_START: |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 1099 | if ((!cpu_online(cpu)) || (!policy->cur)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | return -EINVAL; |
| 1101 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1102 | mutex_lock(&dbs_mutex); |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 1103 | |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 1104 | dbs_enable++; |
Rusty Russell | 835481d | 2009-01-04 05:18:06 -0800 | [diff] [blame] | 1105 | for_each_cpu(j, policy->cpus) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | struct cpu_dbs_info_s *j_dbs_info; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1107 | j_dbs_info = &per_cpu(od_cpu_dbs_info, j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | j_dbs_info->cur_policy = policy; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 1109 | |
venkatesh.pallipadi@intel.com | 3430502 | 2008-08-04 11:59:09 -0700 | [diff] [blame] | 1110 | j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j, |
| 1111 | &j_dbs_info->prev_cpu_wall); |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 1112 | if (dbs_tuners_ins.ignore_nice) |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 1113 | j_dbs_info->prev_cpu_nice = |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 1114 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | } |
Venkatesh Pallipadi | 529af7a | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1116 | this_dbs_info->cpu = cpu; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 1117 | this_dbs_info->rate_mult = 1; |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 1118 | ondemand_powersave_bias_init_cpu(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* |
| 1120 | * Start the timerschedule work, when this governor |
| 1121 | * is used for first time |
| 1122 | */ |
| 1123 | if (dbs_enable == 1) { |
| 1124 | unsigned int latency; |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 1125 | |
| 1126 | rc = sysfs_create_group(cpufreq_global_kobject, |
| 1127 | &dbs_attr_group); |
| 1128 | if (rc) { |
| 1129 | mutex_unlock(&dbs_mutex); |
| 1130 | return rc; |
| 1131 | } |
| 1132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | /* policy latency is in nS. Convert it to uS first */ |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 1134 | latency = policy->cpuinfo.transition_latency / 1000; |
| 1135 | if (latency == 0) |
| 1136 | latency = 1; |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 1137 | /* Bring kernel and HW constraints together */ |
| 1138 | min_sampling_rate = max(min_sampling_rate, |
| 1139 | MIN_LATENCY_MULTIPLIER * latency); |
| 1140 | dbs_tuners_ins.sampling_rate = |
| 1141 | max(min_sampling_rate, |
| 1142 | latency * LATENCY_MULTIPLIER); |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 1143 | dbs_tuners_ins.io_is_busy = should_io_be_busy(); |
Narayanan Gopalakrishnan | d3ca783 | 2012-10-19 17:24:53 -0700 | [diff] [blame] | 1144 | |
| 1145 | if (dbs_tuners_ins.optimal_freq == 0) |
| 1146 | dbs_tuners_ins.optimal_freq = policy->min; |
| 1147 | |
| 1148 | if (dbs_tuners_ins.sync_freq == 0) |
| 1149 | dbs_tuners_ins.sync_freq = policy->min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1151 | if (!cpu) |
| 1152 | rc = input_register_handler(&dbs_input_handler); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1153 | mutex_unlock(&dbs_mutex); |
venkatesh.pallipadi@intel.com | 7d26e2d | 2009-07-02 17:08:30 -0700 | [diff] [blame] | 1154 | |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 1155 | |
| 1156 | if (!ondemand_powersave_bias_setspeed( |
| 1157 | this_dbs_info->cur_policy, |
| 1158 | NULL, |
| 1159 | dbs_tuners_ins.powersave_bias)) |
| 1160 | dbs_timer_init(this_dbs_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | break; |
| 1162 | |
| 1163 | case CPUFREQ_GOV_STOP: |
Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 1164 | dbs_timer_exit(this_dbs_info); |
venkatesh.pallipadi@intel.com | 7d26e2d | 2009-07-02 17:08:30 -0700 | [diff] [blame] | 1165 | |
| 1166 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | dbs_enable--; |
Anitha Anand | 3dd6509 | 2012-01-18 17:17:40 -0800 | [diff] [blame] | 1168 | /* If device is being removed, policy is no longer |
| 1169 | * valid. */ |
| 1170 | this_dbs_info->cur_policy = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1171 | if (!cpu) |
| 1172 | input_unregister_handler(&dbs_input_handler); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1173 | mutex_unlock(&dbs_mutex); |
Thomas Renninger | 0e625ac | 2009-07-24 15:25:06 +0200 | [diff] [blame] | 1174 | if (!dbs_enable) |
| 1175 | sysfs_remove_group(cpufreq_global_kobject, |
| 1176 | &dbs_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
| 1178 | break; |
| 1179 | |
| 1180 | case CPUFREQ_GOV_LIMITS: |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 1181 | mutex_lock(&this_dbs_info->timer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | if (policy->max < this_dbs_info->cur_policy->cur) |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 1183 | __cpufreq_driver_target(this_dbs_info->cur_policy, |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 1184 | policy->max, CPUFREQ_RELATION_H); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | else if (policy->min > this_dbs_info->cur_policy->cur) |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 1186 | __cpufreq_driver_target(this_dbs_info->cur_policy, |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 1187 | policy->min, CPUFREQ_RELATION_L); |
David Ng | 8192a2f | 2012-01-19 14:16:19 -0800 | [diff] [blame] | 1188 | else if (dbs_tuners_ins.powersave_bias != 0) |
| 1189 | ondemand_powersave_bias_setspeed( |
| 1190 | this_dbs_info->cur_policy, |
| 1191 | policy, |
| 1192 | dbs_tuners_ins.powersave_bias); |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 1193 | mutex_unlock(&this_dbs_info->timer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | break; |
| 1195 | } |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | static int __init cpufreq_gov_dbs_init(void) |
| 1200 | { |
Andrea Righi | 4f6e6b9 | 2008-09-18 10:43:40 +0000 | [diff] [blame] | 1201 | u64 idle_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1202 | unsigned int i; |
Andrea Righi | 4f6e6b9 | 2008-09-18 10:43:40 +0000 | [diff] [blame] | 1203 | int cpu = get_cpu(); |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 1204 | |
Kamalesh Babulal | 21f2e3c | 2011-12-09 16:18:42 +0530 | [diff] [blame] | 1205 | idle_time = get_cpu_idle_time_us(cpu, NULL); |
Andrea Righi | 4f6e6b9 | 2008-09-18 10:43:40 +0000 | [diff] [blame] | 1206 | put_cpu(); |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 1207 | if (idle_time != -1ULL) { |
| 1208 | /* Idle micro accounting is supported. Use finer thresholds */ |
| 1209 | dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD; |
| 1210 | dbs_tuners_ins.down_differential = |
| 1211 | MICRO_FREQUENCY_DOWN_DIFFERENTIAL; |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 1212 | /* |
Paul Bolle | bd74b32 | 2011-08-06 14:33:43 +0200 | [diff] [blame] | 1213 | * In nohz/micro accounting case we set the minimum frequency |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 1214 | * not depending on HZ, but fixed (very low). The deferred |
| 1215 | * timer might skip some samples if idle/sleeping as needed. |
| 1216 | */ |
| 1217 | min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE; |
| 1218 | } else { |
| 1219 | /* For correct statistics, we need 10 ticks for each measure */ |
| 1220 | min_sampling_rate = |
| 1221 | MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10); |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 1222 | } |
Akinobu Mita | 888a794 | 2008-07-14 12:00:45 +0900 | [diff] [blame] | 1223 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1224 | input_wq = create_workqueue("iewq"); |
| 1225 | if (!input_wq) { |
| 1226 | printk(KERN_ERR "Failed to create iewq workqueue\n"); |
| 1227 | return -EFAULT; |
| 1228 | } |
| 1229 | for_each_possible_cpu(i) { |
Praveen Chidambaram | 457a445 | 2012-07-19 10:45:07 -0600 | [diff] [blame] | 1230 | struct cpu_dbs_info_s *this_dbs_info = |
| 1231 | &per_cpu(od_cpu_dbs_info, i); |
Stephen Boyd | c8fc301 | 2012-10-31 17:43:08 -0700 | [diff] [blame] | 1232 | struct dbs_work_struct *dbs_work = |
| 1233 | &per_cpu(dbs_refresh_work, i); |
| 1234 | |
Praveen Chidambaram | 457a445 | 2012-07-19 10:45:07 -0600 | [diff] [blame] | 1235 | mutex_init(&this_dbs_info->timer_mutex); |
Stephen Boyd | c8fc301 | 2012-10-31 17:43:08 -0700 | [diff] [blame] | 1236 | INIT_WORK(&dbs_work->work, dbs_refresh_callback); |
| 1237 | dbs_work->cpu = i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Tejun Heo | 57df557 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 1240 | return cpufreq_register_governor(&cpufreq_gov_ondemand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static void __exit cpufreq_gov_dbs_exit(void) |
| 1244 | { |
Anji Jonnala | 4c1485f | 2012-11-14 13:34:54 +0530 | [diff] [blame] | 1245 | unsigned int i; |
| 1246 | |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame] | 1247 | cpufreq_unregister_governor(&cpufreq_gov_ondemand); |
Anji Jonnala | 4c1485f | 2012-11-14 13:34:54 +0530 | [diff] [blame] | 1248 | for_each_possible_cpu(i) { |
| 1249 | struct cpu_dbs_info_s *this_dbs_info = |
| 1250 | &per_cpu(od_cpu_dbs_info, i); |
| 1251 | mutex_destroy(&this_dbs_info->timer_mutex); |
| 1252 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1253 | destroy_workqueue(input_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 1257 | MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); |
| 1258 | MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>"); |
| 1259 | MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for " |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 1260 | "Low Latency Frequency Transition capable processors"); |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 1261 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 1263 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
| 1264 | fs_initcall(cpufreq_gov_dbs_init); |
| 1265 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | module_init(cpufreq_gov_dbs_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 1267 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | module_exit(cpufreq_gov_dbs_exit); |