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