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 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 15 | #include <linux/cpu.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 16 | #include <linux/percpu-defs.h> |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 17 | #include <linux/slab.h> |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 18 | #include <linux/tick.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 19 | #include "cpufreq_governor.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 21 | /* On-demand governor macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 23 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
| 24 | #define MAX_SAMPLING_DOWN_FACTOR (100000) |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 25 | #define MICRO_FREQUENCY_UP_THRESHOLD (95) |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 26 | #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000) |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 27 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
| 29 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 30 | static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info); |
Thomas Renninger | 112124a | 2009-02-04 11:55:12 +0100 | [diff] [blame] | 31 | |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 32 | static struct od_ops od_ops; |
| 33 | |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame] | 34 | static unsigned int default_powersave_bias; |
| 35 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 36 | static void ondemand_powersave_bias_init_cpu(int cpu) |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 37 | { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 38 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 39 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 40 | dbs_info->freq_table = cpufreq_frequency_get_table(cpu); |
| 41 | dbs_info->freq_lo = 0; |
| 42 | } |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 43 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 44 | /* |
| 45 | * Not all CPUs want IO time to be accounted as busy; this depends on how |
| 46 | * efficient idling at a higher frequency/voltage is. |
| 47 | * Pavel Machek says this is not so for various generations of AMD and old |
| 48 | * Intel systems. |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 49 | * Mike Chan (android.com) claims this is also not true for ARM. |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 50 | * Because of this, whitelist specific known (series) of CPUs by default, and |
| 51 | * leave all others up to the user. |
| 52 | */ |
| 53 | static int should_io_be_busy(void) |
| 54 | { |
| 55 | #if defined(CONFIG_X86) |
| 56 | /* |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 57 | * For Intel, Core 2 (model 15) and later have an efficient idle. |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 58 | */ |
| 59 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && |
| 60 | boot_cpu_data.x86 == 6 && |
| 61 | boot_cpu_data.x86_model >= 15) |
| 62 | return 1; |
| 63 | #endif |
| 64 | return 0; |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 67 | /* |
| 68 | * Find right freq to be set now with powersave_bias on. |
| 69 | * Returns the freq_hi to be used right now and will set freq_hi_jiffies, |
| 70 | * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs. |
| 71 | */ |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 72 | static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 73 | unsigned int freq_next, unsigned int relation) |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 74 | { |
| 75 | unsigned int freq_req, freq_reduc, freq_avg; |
| 76 | unsigned int freq_hi, freq_lo; |
| 77 | unsigned int index = 0; |
| 78 | unsigned int jiffies_total, jiffies_hi, jiffies_lo; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 79 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 80 | policy->cpu); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 81 | struct dbs_data *dbs_data = policy->governor_data; |
| 82 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 83 | |
| 84 | if (!dbs_info->freq_table) { |
| 85 | dbs_info->freq_lo = 0; |
| 86 | dbs_info->freq_lo_jiffies = 0; |
| 87 | return freq_next; |
| 88 | } |
| 89 | |
| 90 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next, |
| 91 | relation, &index); |
| 92 | freq_req = dbs_info->freq_table[index].frequency; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 93 | freq_reduc = freq_req * od_tuners->powersave_bias / 1000; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 94 | freq_avg = freq_req - freq_reduc; |
| 95 | |
| 96 | /* Find freq bounds for freq_avg in freq_table */ |
| 97 | index = 0; |
| 98 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, |
| 99 | CPUFREQ_RELATION_H, &index); |
| 100 | freq_lo = dbs_info->freq_table[index].frequency; |
| 101 | index = 0; |
| 102 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, |
| 103 | CPUFREQ_RELATION_L, &index); |
| 104 | freq_hi = dbs_info->freq_table[index].frequency; |
| 105 | |
| 106 | /* Find out how long we have to be in hi and lo freqs */ |
| 107 | if (freq_hi == freq_lo) { |
| 108 | dbs_info->freq_lo = 0; |
| 109 | dbs_info->freq_lo_jiffies = 0; |
| 110 | return freq_lo; |
| 111 | } |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 112 | jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 113 | jiffies_hi = (freq_avg - freq_lo) * jiffies_total; |
| 114 | jiffies_hi += ((freq_hi - freq_lo) / 2); |
| 115 | jiffies_hi /= (freq_hi - freq_lo); |
| 116 | jiffies_lo = jiffies_total - jiffies_hi; |
| 117 | dbs_info->freq_lo = freq_lo; |
| 118 | dbs_info->freq_lo_jiffies = jiffies_lo; |
| 119 | dbs_info->freq_hi_jiffies = jiffies_hi; |
| 120 | return freq_hi; |
| 121 | } |
| 122 | |
| 123 | static void ondemand_powersave_bias_init(void) |
| 124 | { |
| 125 | int i; |
| 126 | for_each_online_cpu(i) { |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 127 | ondemand_powersave_bias_init_cpu(i); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 131 | static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 132 | { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 133 | struct dbs_data *dbs_data = policy->governor_data; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 134 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
| 135 | |
| 136 | if (od_tuners->powersave_bias) |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 137 | freq = od_ops.powersave_bias_target(policy, freq, |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 138 | CPUFREQ_RELATION_H); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 139 | else if (policy->cur == policy->max) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 140 | return; |
| 141 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 142 | __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ? |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 143 | CPUFREQ_RELATION_L : CPUFREQ_RELATION_H); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Every sampling_rate, we check, if current idle time is less than 20% |
Stratos Karafotis | dfa5bb6 | 2013-06-05 19:01:25 +0300 | [diff] [blame] | 148 | * (default), then we try to increase frequency. Else, we adjust the frequency |
| 149 | * proportional to load. |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 150 | */ |
Stratos Karafotis | dfa5bb6 | 2013-06-05 19:01:25 +0300 | [diff] [blame] | 151 | static void od_check_cpu(int cpu, unsigned int load) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 152 | { |
| 153 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 154 | struct cpufreq_policy *policy = dbs_info->cdbs.shared->policy; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 155 | struct dbs_data *dbs_data = policy->governor_data; |
| 156 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 157 | |
| 158 | dbs_info->freq_lo = 0; |
| 159 | |
| 160 | /* Check for frequency increase */ |
Stratos Karafotis | dfa5bb6 | 2013-06-05 19:01:25 +0300 | [diff] [blame] | 161 | if (load > od_tuners->up_threshold) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 162 | /* If switching to max speed, apply sampling_down_factor */ |
| 163 | if (policy->cur < policy->max) |
| 164 | dbs_info->rate_mult = |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 165 | od_tuners->sampling_down_factor; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 166 | dbs_freq_increase(policy, policy->max); |
Stratos Karafotis | dfa5bb6 | 2013-06-05 19:01:25 +0300 | [diff] [blame] | 167 | } else { |
| 168 | /* Calculate the next frequency proportional to load */ |
Stratos Karafotis | 6393d6a | 2014-06-30 19:59:34 +0300 | [diff] [blame] | 169 | unsigned int freq_next, min_f, max_f; |
| 170 | |
| 171 | min_f = policy->cpuinfo.min_freq; |
| 172 | max_f = policy->cpuinfo.max_freq; |
| 173 | freq_next = min_f + load * (max_f - min_f) / 100; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 174 | |
| 175 | /* No longer fully busy, reset rate_mult */ |
| 176 | dbs_info->rate_mult = 1; |
| 177 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 178 | if (!od_tuners->powersave_bias) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 179 | __cpufreq_driver_target(policy, freq_next, |
Stratos Karafotis | 6393d6a | 2014-06-30 19:59:34 +0300 | [diff] [blame] | 180 | CPUFREQ_RELATION_C); |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 181 | return; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 182 | } |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 183 | |
| 184 | freq_next = od_ops.powersave_bias_target(policy, freq_next, |
| 185 | CPUFREQ_RELATION_L); |
Stratos Karafotis | 6393d6a | 2014-06-30 19:59:34 +0300 | [diff] [blame] | 186 | __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 190 | static unsigned int od_dbs_timer(struct cpufreq_policy *policy) |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 191 | { |
Viresh Kumar | affde5d | 2015-12-03 09:37:51 +0530 | [diff] [blame] | 192 | struct dbs_data *dbs_data = policy->governor_data; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 193 | unsigned int cpu = policy->cpu; |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 194 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 195 | cpu); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 196 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 197 | int delay = 0, sample_type = dbs_info->sample_type; |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 198 | |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 199 | /* Common NORMAL_SAMPLE setup */ |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 200 | dbs_info->sample_type = OD_NORMAL_SAMPLE; |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 201 | if (sample_type == OD_SUB_SAMPLE) { |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 202 | delay = dbs_info->freq_lo_jiffies; |
| 203 | __cpufreq_driver_target(policy, dbs_info->freq_lo, |
Viresh Kumar | 42994af | 2015-06-19 17:18:05 +0530 | [diff] [blame] | 204 | CPUFREQ_RELATION_H); |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 205 | } else { |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 206 | dbs_check_cpu(dbs_data, cpu); |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 207 | if (dbs_info->freq_lo) { |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 208 | /* Setup timer for SUB_SAMPLE */ |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 209 | dbs_info->sample_type = OD_SUB_SAMPLE; |
| 210 | delay = dbs_info->freq_hi_jiffies; |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 211 | } |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 212 | } |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 213 | |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 214 | if (!delay) |
| 215 | delay = delay_for_sampling_rate(od_tuners->sampling_rate |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 216 | * dbs_info->rate_mult); |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 217 | |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 218 | return delay; |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /************************** sysfs interface ************************/ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 222 | static struct common_dbs_data od_dbs_cdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 224 | /** |
| 225 | * update_sampling_rate - update sampling rate effective immediately if needed. |
| 226 | * @new_rate: new sampling rate |
| 227 | * |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 228 | * If new rate is smaller than the old, simply updating |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 229 | * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the |
| 230 | * original sampling_rate was 1 second and the requested new sampling rate is 10 |
| 231 | * ms because the user needs immediate reaction from ondemand governor, but not |
| 232 | * sure if higher frequency will be required or not, then, the governor may |
| 233 | * change the sampling rate too late; up to 1 second later. Thus, if we are |
| 234 | * reducing the sampling rate, we need to make the new value effective |
| 235 | * immediately. |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 236 | */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 237 | static void update_sampling_rate(struct dbs_data *dbs_data, |
| 238 | unsigned int new_rate) |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 239 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 240 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Viresh Kumar | f08f638 | 2015-12-03 09:37:54 +0530 | [diff] [blame] | 241 | struct cpumask cpumask; |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 242 | int cpu; |
| 243 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 244 | od_tuners->sampling_rate = new_rate = max(new_rate, |
| 245 | dbs_data->min_sampling_rate); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 246 | |
Viresh Kumar | e128c86 | 2015-12-03 09:37:49 +0530 | [diff] [blame] | 247 | /* |
| 248 | * Lock governor so that governor start/stop can't execute in parallel. |
| 249 | */ |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 250 | mutex_lock(&dbs_data_mutex); |
Viresh Kumar | e128c86 | 2015-12-03 09:37:49 +0530 | [diff] [blame] | 251 | |
Viresh Kumar | f08f638 | 2015-12-03 09:37:54 +0530 | [diff] [blame] | 252 | cpumask_copy(&cpumask, cpu_online_mask); |
| 253 | |
| 254 | for_each_cpu(cpu, &cpumask) { |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 255 | struct cpufreq_policy *policy; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 256 | struct od_cpu_dbs_info_s *dbs_info; |
Viresh Kumar | e128c86 | 2015-12-03 09:37:49 +0530 | [diff] [blame] | 257 | struct cpu_dbs_info *cdbs; |
| 258 | struct cpu_common_dbs_info *shared; |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 259 | |
Fabio Baltieri | 8ee2ec5 | 2012-12-27 14:55:42 +0000 | [diff] [blame] | 260 | dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Viresh Kumar | e128c86 | 2015-12-03 09:37:49 +0530 | [diff] [blame] | 261 | cdbs = &dbs_info->cdbs; |
| 262 | shared = cdbs->shared; |
| 263 | |
| 264 | /* |
| 265 | * A valid shared and shared->policy means governor hasn't |
| 266 | * stopped or exited yet. |
| 267 | */ |
| 268 | if (!shared || !shared->policy) |
| 269 | continue; |
| 270 | |
| 271 | policy = shared->policy; |
| 272 | |
Viresh Kumar | f08f638 | 2015-12-03 09:37:54 +0530 | [diff] [blame] | 273 | /* clear all CPUs of this policy */ |
| 274 | cpumask_andnot(&cpumask, &cpumask, policy->cpus); |
| 275 | |
Viresh Kumar | e128c86 | 2015-12-03 09:37:49 +0530 | [diff] [blame] | 276 | /* |
| 277 | * Update sampling rate for CPUs whose policy is governed by |
| 278 | * dbs_data. In case of governor_per_policy, only a single |
| 279 | * policy will be governed by dbs_data, otherwise there can be |
| 280 | * multiple policies that are governed by the same dbs_data. |
| 281 | */ |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 282 | if (dbs_data == policy->governor_data) { |
| 283 | mutex_lock(&shared->timer_mutex); |
| 284 | /* |
| 285 | * On 32-bit architectures this may race with the |
| 286 | * sample_delay_ns read in dbs_update_util_handler(), |
| 287 | * but that really doesn't matter. If the read returns |
| 288 | * a value that's too big, the sample will be skipped, |
| 289 | * but the next invocation of dbs_update_util_handler() |
| 290 | * (when the update has been completed) will take a |
| 291 | * sample. If the returned value is too small, the |
| 292 | * sample will be taken immediately, but that isn't a |
| 293 | * problem, as we want the new rate to take effect |
| 294 | * immediately anyway. |
| 295 | * |
| 296 | * If this runs in parallel with dbs_work_handler(), we |
| 297 | * may end up overwriting the sample_delay_ns value that |
| 298 | * it has just written, but the difference should not be |
| 299 | * too big and it will be corrected next time a sample |
| 300 | * is taken, so it shouldn't be significant. |
| 301 | */ |
| 302 | gov_update_sample_delay(shared, new_rate); |
| 303 | mutex_unlock(&shared->timer_mutex); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 304 | } |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 305 | } |
Viresh Kumar | e128c86 | 2015-12-03 09:37:49 +0530 | [diff] [blame] | 306 | |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 307 | mutex_unlock(&dbs_data_mutex); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 310 | static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf, |
| 311 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | unsigned int input; |
| 314 | int ret; |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 315 | ret = sscanf(buf, "%u", &input); |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 316 | if (ret != 1) |
| 317 | return -EINVAL; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 318 | |
| 319 | update_sampling_rate(dbs_data, input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return count; |
| 321 | } |
| 322 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 323 | static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf, |
| 324 | size_t count) |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 325 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 326 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 327 | unsigned int input; |
| 328 | int ret; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 329 | unsigned int j; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 330 | |
| 331 | ret = sscanf(buf, "%u", &input); |
| 332 | if (ret != 1) |
| 333 | return -EINVAL; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 334 | od_tuners->io_is_busy = !!input; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 335 | |
| 336 | /* we need to re-evaluate prev_cpu_idle */ |
| 337 | for_each_online_cpu(j) { |
| 338 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
| 339 | j); |
| 340 | dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j, |
| 341 | &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy); |
| 342 | } |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 343 | return count; |
| 344 | } |
| 345 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 346 | static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf, |
| 347 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 349 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | unsigned int input; |
| 351 | int ret; |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 352 | ret = sscanf(buf, "%u", &input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 354 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 355 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return -EINVAL; |
| 357 | } |
Stratos Karafotis | 4bd4e42 | 2013-02-06 13:34:00 +0100 | [diff] [blame] | 358 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 359 | od_tuners->up_threshold = input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return count; |
| 361 | } |
| 362 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 363 | static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data, |
| 364 | const char *buf, size_t count) |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 365 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 366 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 367 | unsigned int input, j; |
| 368 | int ret; |
| 369 | ret = sscanf(buf, "%u", &input); |
| 370 | |
| 371 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
| 372 | return -EINVAL; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 373 | od_tuners->sampling_down_factor = input; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 374 | |
| 375 | /* Reset down sampling multiplier in case it was active */ |
| 376 | for_each_online_cpu(j) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 377 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
| 378 | j); |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 379 | dbs_info->rate_mult = 1; |
| 380 | } |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 381 | return count; |
| 382 | } |
| 383 | |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 384 | static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data, |
| 385 | const char *buf, size_t count) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 386 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 387 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 388 | unsigned int input; |
| 389 | int ret; |
| 390 | |
| 391 | unsigned int j; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 392 | |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 393 | ret = sscanf(buf, "%u", &input); |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 394 | if (ret != 1) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 395 | return -EINVAL; |
| 396 | |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 397 | if (input > 1) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 398 | input = 1; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 399 | |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 400 | if (input == od_tuners->ignore_nice_load) { /* nothing to do */ |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 401 | return count; |
| 402 | } |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 403 | od_tuners->ignore_nice_load = input; |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 404 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 405 | /* we need to re-evaluate prev_cpu_idle */ |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 406 | for_each_online_cpu(j) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 407 | struct od_cpu_dbs_info_s *dbs_info; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 408 | dbs_info = &per_cpu(od_cpu_dbs_info, j); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 409 | dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j, |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 410 | &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy); |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 411 | if (od_tuners->ignore_nice_load) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 412 | dbs_info->cdbs.prev_cpu_nice = |
| 413 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 414 | |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 415 | } |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 416 | return count; |
| 417 | } |
| 418 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 419 | static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf, |
| 420 | size_t count) |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 421 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 422 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 423 | unsigned int input; |
| 424 | int ret; |
| 425 | ret = sscanf(buf, "%u", &input); |
| 426 | |
| 427 | if (ret != 1) |
| 428 | return -EINVAL; |
| 429 | |
| 430 | if (input > 1000) |
| 431 | input = 1000; |
| 432 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 433 | od_tuners->powersave_bias = input; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 434 | ondemand_powersave_bias_init(); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 435 | return count; |
| 436 | } |
| 437 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 438 | show_store_one(od, sampling_rate); |
| 439 | show_store_one(od, io_is_busy); |
| 440 | show_store_one(od, up_threshold); |
| 441 | show_store_one(od, sampling_down_factor); |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 442 | show_store_one(od, ignore_nice_load); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 443 | show_store_one(od, powersave_bias); |
| 444 | declare_show_sampling_rate_min(od); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 445 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 446 | gov_sys_pol_attr_rw(sampling_rate); |
| 447 | gov_sys_pol_attr_rw(io_is_busy); |
| 448 | gov_sys_pol_attr_rw(up_threshold); |
| 449 | gov_sys_pol_attr_rw(sampling_down_factor); |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 450 | gov_sys_pol_attr_rw(ignore_nice_load); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 451 | gov_sys_pol_attr_rw(powersave_bias); |
| 452 | gov_sys_pol_attr_ro(sampling_rate_min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 454 | static struct attribute *dbs_attributes_gov_sys[] = { |
| 455 | &sampling_rate_min_gov_sys.attr, |
| 456 | &sampling_rate_gov_sys.attr, |
| 457 | &up_threshold_gov_sys.attr, |
| 458 | &sampling_down_factor_gov_sys.attr, |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 459 | &ignore_nice_load_gov_sys.attr, |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 460 | &powersave_bias_gov_sys.attr, |
| 461 | &io_is_busy_gov_sys.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | NULL |
| 463 | }; |
| 464 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 465 | static struct attribute_group od_attr_group_gov_sys = { |
| 466 | .attrs = dbs_attributes_gov_sys, |
| 467 | .name = "ondemand", |
| 468 | }; |
| 469 | |
| 470 | static struct attribute *dbs_attributes_gov_pol[] = { |
| 471 | &sampling_rate_min_gov_pol.attr, |
| 472 | &sampling_rate_gov_pol.attr, |
| 473 | &up_threshold_gov_pol.attr, |
| 474 | &sampling_down_factor_gov_pol.attr, |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 475 | &ignore_nice_load_gov_pol.attr, |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 476 | &powersave_bias_gov_pol.attr, |
| 477 | &io_is_busy_gov_pol.attr, |
| 478 | NULL |
| 479 | }; |
| 480 | |
| 481 | static struct attribute_group od_attr_group_gov_pol = { |
| 482 | .attrs = dbs_attributes_gov_pol, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | .name = "ondemand", |
| 484 | }; |
| 485 | |
| 486 | /************************** sysfs end ************************/ |
| 487 | |
Viresh Kumar | 8e0484d | 2015-06-03 15:57:11 +0530 | [diff] [blame] | 488 | static int od_init(struct dbs_data *dbs_data, bool notify) |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 489 | { |
| 490 | struct od_dbs_tuners *tuners; |
| 491 | u64 idle_time; |
| 492 | int cpu; |
| 493 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 494 | tuners = kzalloc(sizeof(*tuners), GFP_KERNEL); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 495 | if (!tuners) { |
| 496 | pr_err("%s: kzalloc failed\n", __func__); |
| 497 | return -ENOMEM; |
| 498 | } |
| 499 | |
| 500 | cpu = get_cpu(); |
| 501 | idle_time = get_cpu_idle_time_us(cpu, NULL); |
| 502 | put_cpu(); |
| 503 | if (idle_time != -1ULL) { |
| 504 | /* Idle micro accounting is supported. Use finer thresholds */ |
| 505 | tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 506 | /* |
| 507 | * In nohz/micro accounting case we set the minimum frequency |
| 508 | * not depending on HZ, but fixed (very low). The deferred |
| 509 | * timer might skip some samples if idle/sleeping as needed. |
| 510 | */ |
| 511 | dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE; |
| 512 | } else { |
| 513 | tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 514 | |
| 515 | /* For correct statistics, we need 10 ticks for each measure */ |
| 516 | dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO * |
| 517 | jiffies_to_usecs(10); |
| 518 | } |
| 519 | |
| 520 | tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR; |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 521 | tuners->ignore_nice_load = 0; |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame] | 522 | tuners->powersave_bias = default_powersave_bias; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 523 | tuners->io_is_busy = should_io_be_busy(); |
| 524 | |
| 525 | dbs_data->tuners = tuners; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Viresh Kumar | 8e0484d | 2015-06-03 15:57:11 +0530 | [diff] [blame] | 529 | static void od_exit(struct dbs_data *dbs_data, bool notify) |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 530 | { |
| 531 | kfree(dbs_data->tuners); |
| 532 | } |
| 533 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 534 | define_get_cpu_dbs_routines(od_cpu_dbs_info); |
Mike Chan | 00e299f | 2010-01-26 17:06:47 -0800 | [diff] [blame] | 535 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 536 | static struct od_ops od_ops = { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 537 | .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu, |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 538 | .powersave_bias_target = generic_powersave_bias_target, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 539 | .freq_increase = dbs_freq_increase, |
| 540 | }; |
| 541 | |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 542 | static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 543 | unsigned int event); |
| 544 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 545 | static struct common_dbs_data od_dbs_cdata = { |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 546 | .gov = { |
| 547 | .name = "ondemand", |
| 548 | .governor = od_cpufreq_governor_dbs, |
| 549 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, |
| 550 | .owner = THIS_MODULE, |
| 551 | }, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 552 | .governor = GOV_ONDEMAND, |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 553 | .attr_group_gov_sys = &od_attr_group_gov_sys, |
| 554 | .attr_group_gov_pol = &od_attr_group_gov_pol, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 555 | .get_cpu_cdbs = get_cpu_cdbs, |
| 556 | .get_cpu_dbs_info_s = get_cpu_dbs_info_s, |
| 557 | .gov_dbs_timer = od_dbs_timer, |
| 558 | .gov_check_cpu = od_check_cpu, |
| 559 | .gov_ops = &od_ops, |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 560 | .init = od_init, |
| 561 | .exit = od_exit, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 562 | }; |
| 563 | |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 564 | #define CPU_FREQ_GOV_ONDEMAND (&od_dbs_cdata.gov) |
| 565 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 566 | static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 567 | unsigned int event) |
| 568 | { |
| 569 | return cpufreq_governor_dbs(policy, &od_dbs_cdata, event); |
| 570 | } |
| 571 | |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 572 | static void od_set_powersave_bias(unsigned int powersave_bias) |
| 573 | { |
| 574 | struct cpufreq_policy *policy; |
| 575 | struct dbs_data *dbs_data; |
| 576 | struct od_dbs_tuners *od_tuners; |
| 577 | unsigned int cpu; |
| 578 | cpumask_t done; |
| 579 | |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame] | 580 | default_powersave_bias = powersave_bias; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 581 | cpumask_clear(&done); |
| 582 | |
| 583 | get_online_cpus(); |
| 584 | for_each_online_cpu(cpu) { |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 585 | struct cpu_common_dbs_info *shared; |
| 586 | |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 587 | if (cpumask_test_cpu(cpu, &done)) |
| 588 | continue; |
| 589 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 590 | shared = per_cpu(od_cpu_dbs_info, cpu).cdbs.shared; |
| 591 | if (!shared) |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame] | 592 | continue; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 593 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 594 | policy = shared->policy; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 595 | cpumask_or(&done, &done, policy->cpus); |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame] | 596 | |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 597 | if (policy->governor != CPU_FREQ_GOV_ONDEMAND) |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame] | 598 | continue; |
| 599 | |
| 600 | dbs_data = policy->governor_data; |
| 601 | od_tuners = dbs_data->tuners; |
| 602 | od_tuners->powersave_bias = default_powersave_bias; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 603 | } |
| 604 | put_online_cpus(); |
| 605 | } |
| 606 | |
| 607 | void od_register_powersave_bias_handler(unsigned int (*f) |
| 608 | (struct cpufreq_policy *, unsigned int, unsigned int), |
| 609 | unsigned int powersave_bias) |
| 610 | { |
| 611 | od_ops.powersave_bias_target = f; |
| 612 | od_set_powersave_bias(powersave_bias); |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler); |
| 615 | |
| 616 | void od_unregister_powersave_bias_handler(void) |
| 617 | { |
| 618 | od_ops.powersave_bias_target = generic_powersave_bias_target; |
| 619 | od_set_powersave_bias(0); |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler); |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | static int __init cpufreq_gov_dbs_init(void) |
| 624 | { |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 625 | return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | static void __exit cpufreq_gov_dbs_exit(void) |
| 629 | { |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 630 | cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 633 | MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); |
| 634 | MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>"); |
| 635 | MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for " |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 636 | "Low Latency Frequency Transition capable processors"); |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 637 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 639 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 640 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 641 | { |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame^] | 642 | return CPU_FREQ_GOV_ONDEMAND; |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 645 | fs_initcall(cpufreq_gov_dbs_init); |
| 646 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | module_init(cpufreq_gov_dbs_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 648 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | module_exit(cpufreq_gov_dbs_exit); |