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