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