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