Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1 | /* |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 2 | * intel_pstate.c: Native P state management for Intel processors |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 3 | * |
| 4 | * (C) Copyright 2012 Intel Corporation |
| 5 | * Author: Dirk Brandewie <dirk.j.brandewie@intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; version 2 |
| 10 | * of the License. |
| 11 | */ |
| 12 | |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/kernel_stat.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/ktime.h> |
| 19 | #include <linux/hrtimer.h> |
| 20 | #include <linux/tick.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/cpu.h> |
| 25 | #include <linux/cpufreq.h> |
| 26 | #include <linux/sysfs.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/debugfs.h> |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 30 | #include <linux/acpi.h> |
Stephen Rothwell | d647230 | 2015-06-02 19:01:38 +1000 | [diff] [blame] | 31 | #include <linux/vmalloc.h> |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 32 | #include <trace/events/power.h> |
| 33 | |
| 34 | #include <asm/div64.h> |
| 35 | #include <asm/msr.h> |
| 36 | #include <asm/cpu_device_id.h> |
Borislav Petkov | 64df1fd | 2015-04-03 15:19:53 +0200 | [diff] [blame] | 37 | #include <asm/cpufeature.h> |
Dave Hansen | 5b20c94 | 2016-06-02 17:19:45 -0700 | [diff] [blame] | 38 | #include <asm/intel-family.h> |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 39 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 40 | #define ATOM_RATIOS 0x66a |
| 41 | #define ATOM_VIDS 0x66b |
| 42 | #define ATOM_TURBO_RATIOS 0x66c |
| 43 | #define ATOM_TURBO_VIDS 0x66d |
Dirk Brandewie | 61d8d2a | 2014-02-12 10:01:07 -0800 | [diff] [blame] | 44 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_ACPI |
| 46 | #include <acpi/processor.h> |
| 47 | #endif |
| 48 | |
Dirk Brandewie | f0fe3cd | 2014-05-29 09:32:23 -0700 | [diff] [blame] | 49 | #define FRAC_BITS 8 |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 50 | #define int_tofp(X) ((int64_t)(X) << FRAC_BITS) |
| 51 | #define fp_toint(X) ((X) >> FRAC_BITS) |
Dirk Brandewie | f0fe3cd | 2014-05-29 09:32:23 -0700 | [diff] [blame] | 52 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 53 | #define EXT_BITS 6 |
| 54 | #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS) |
| 55 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 56 | static inline int32_t mul_fp(int32_t x, int32_t y) |
| 57 | { |
| 58 | return ((int64_t)x * (int64_t)y) >> FRAC_BITS; |
| 59 | } |
| 60 | |
Prarit Bhargava | 7180ddd | 2015-06-15 13:43:29 -0400 | [diff] [blame] | 61 | static inline int32_t div_fp(s64 x, s64 y) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 62 | { |
Prarit Bhargava | 7180ddd | 2015-06-15 13:43:29 -0400 | [diff] [blame] | 63 | return div64_s64((int64_t)x << FRAC_BITS, y); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Dirk Brandewie | d022a65 | 2014-10-13 08:37:44 -0700 | [diff] [blame] | 66 | static inline int ceiling_fp(int32_t x) |
| 67 | { |
| 68 | int mask, ret; |
| 69 | |
| 70 | ret = fp_toint(x); |
| 71 | mask = (1 << FRAC_BITS) - 1; |
| 72 | if (x & mask) |
| 73 | ret += 1; |
| 74 | return ret; |
| 75 | } |
| 76 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 77 | static inline u64 mul_ext_fp(u64 x, u64 y) |
| 78 | { |
| 79 | return (x * y) >> EXT_FRAC_BITS; |
| 80 | } |
| 81 | |
| 82 | static inline u64 div_ext_fp(u64 x, u64 y) |
| 83 | { |
| 84 | return div64_u64(x << EXT_FRAC_BITS, y); |
| 85 | } |
| 86 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 87 | /** |
| 88 | * struct sample - Store performance sample |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 89 | * @core_avg_perf: Ratio of APERF/MPERF which is the actual average |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 90 | * performance during last sample period |
| 91 | * @busy_scaled: Scaled busy value which is used to calculate next |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 92 | * P state. This can be different than core_avg_perf |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 93 | * to account for cpu idle period |
| 94 | * @aperf: Difference of actual performance frequency clock count |
| 95 | * read from APERF MSR between last and current sample |
| 96 | * @mperf: Difference of maximum performance frequency clock count |
| 97 | * read from MPERF MSR between last and current sample |
| 98 | * @tsc: Difference of time stamp counter between last and |
| 99 | * current sample |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 100 | * @time: Current time from scheduler |
| 101 | * |
| 102 | * This structure is used in the cpudata structure to store performance sample |
| 103 | * data for choosing next P State. |
| 104 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 105 | struct sample { |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 106 | int32_t core_avg_perf; |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 107 | int32_t busy_scaled; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 108 | u64 aperf; |
| 109 | u64 mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 110 | u64 tsc; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 111 | u64 time; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 114 | /** |
| 115 | * struct pstate_data - Store P state data |
| 116 | * @current_pstate: Current requested P state |
| 117 | * @min_pstate: Min P state possible for this platform |
| 118 | * @max_pstate: Max P state possible for this platform |
| 119 | * @max_pstate_physical:This is physical Max P state for a processor |
| 120 | * This can be higher than the max_pstate which can |
| 121 | * be limited by platform thermal design power limits |
| 122 | * @scaling: Scaling factor to convert frequency to cpufreq |
| 123 | * frequency units |
| 124 | * @turbo_pstate: Max Turbo P state possible for this platform |
| 125 | * |
| 126 | * Stores the per cpu model P state limits and current P state. |
| 127 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 128 | struct pstate_data { |
| 129 | int current_pstate; |
| 130 | int min_pstate; |
| 131 | int max_pstate; |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 132 | int max_pstate_physical; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 133 | int scaling; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 134 | int turbo_pstate; |
| 135 | }; |
| 136 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 137 | /** |
| 138 | * struct vid_data - Stores voltage information data |
| 139 | * @min: VID data for this platform corresponding to |
| 140 | * the lowest P state |
| 141 | * @max: VID data corresponding to the highest P State. |
| 142 | * @turbo: VID data for turbo P state |
| 143 | * @ratio: Ratio of (vid max - vid min) / |
| 144 | * (max P state - Min P State) |
| 145 | * |
| 146 | * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling) |
| 147 | * This data is used in Atom platforms, where in addition to target P state, |
| 148 | * the voltage data needs to be specified to select next P State. |
| 149 | */ |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 150 | struct vid_data { |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 151 | int min; |
| 152 | int max; |
| 153 | int turbo; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 154 | int32_t ratio; |
| 155 | }; |
| 156 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 157 | /** |
| 158 | * struct _pid - Stores PID data |
| 159 | * @setpoint: Target set point for busyness or performance |
| 160 | * @integral: Storage for accumulated error values |
| 161 | * @p_gain: PID proportional gain |
| 162 | * @i_gain: PID integral gain |
| 163 | * @d_gain: PID derivative gain |
| 164 | * @deadband: PID deadband |
| 165 | * @last_err: Last error storage for integral part of PID calculation |
| 166 | * |
| 167 | * Stores PID coefficients and last error for PID controller. |
| 168 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 169 | struct _pid { |
| 170 | int setpoint; |
| 171 | int32_t integral; |
| 172 | int32_t p_gain; |
| 173 | int32_t i_gain; |
| 174 | int32_t d_gain; |
| 175 | int deadband; |
Brennan Shacklett | d253d2a | 2013-10-21 09:20:32 -0700 | [diff] [blame] | 176 | int32_t last_err; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 179 | /** |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 180 | * struct perf_limits - Store user and policy limits |
| 181 | * @no_turbo: User requested turbo state from intel_pstate sysfs |
| 182 | * @turbo_disabled: Platform turbo status either from msr |
| 183 | * MSR_IA32_MISC_ENABLE or when maximum available pstate |
| 184 | * matches the maximum turbo pstate |
| 185 | * @max_perf_pct: Effective maximum performance limit in percentage, this |
| 186 | * is minimum of either limits enforced by cpufreq policy |
| 187 | * or limits from user set limits via intel_pstate sysfs |
| 188 | * @min_perf_pct: Effective minimum performance limit in percentage, this |
| 189 | * is maximum of either limits enforced by cpufreq policy |
| 190 | * or limits from user set limits via intel_pstate sysfs |
| 191 | * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct |
| 192 | * This value is used to limit max pstate |
| 193 | * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct |
| 194 | * This value is used to limit min pstate |
| 195 | * @max_policy_pct: The maximum performance in percentage enforced by |
| 196 | * cpufreq setpolicy interface |
| 197 | * @max_sysfs_pct: The maximum performance in percentage enforced by |
| 198 | * intel pstate sysfs interface, unused when per cpu |
| 199 | * controls are enforced |
| 200 | * @min_policy_pct: The minimum performance in percentage enforced by |
| 201 | * cpufreq setpolicy interface |
| 202 | * @min_sysfs_pct: The minimum performance in percentage enforced by |
| 203 | * intel pstate sysfs interface, unused when per cpu |
| 204 | * controls are enforced |
| 205 | * |
| 206 | * Storage for user and policy defined limits. |
| 207 | */ |
| 208 | struct perf_limits { |
| 209 | int no_turbo; |
| 210 | int turbo_disabled; |
| 211 | int max_perf_pct; |
| 212 | int min_perf_pct; |
| 213 | int32_t max_perf; |
| 214 | int32_t min_perf; |
| 215 | int max_policy_pct; |
| 216 | int max_sysfs_pct; |
| 217 | int min_policy_pct; |
| 218 | int min_sysfs_pct; |
| 219 | }; |
| 220 | |
| 221 | /** |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 222 | * struct cpudata - Per CPU instance data storage |
| 223 | * @cpu: CPU number for this instance data |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 224 | * @policy: CPUFreq policy value |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 225 | * @update_util: CPUFreq utility callback information |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 226 | * @update_util_set: CPUFreq utility callback is set |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 227 | * @iowait_boost: iowait-related boost fraction |
| 228 | * @last_update: Time of the last update. |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 229 | * @pstate: Stores P state limits for this CPU |
| 230 | * @vid: Stores VID limits for this CPU |
| 231 | * @pid: Stores PID parameters for this CPU |
| 232 | * @last_sample_time: Last Sample time |
| 233 | * @prev_aperf: Last APERF value read from APERF MSR |
| 234 | * @prev_mperf: Last MPERF value read from MPERF MSR |
| 235 | * @prev_tsc: Last timestamp counter (TSC) value |
| 236 | * @prev_cummulative_iowait: IO Wait time difference from last and |
| 237 | * current sample |
| 238 | * @sample: Storage for storing last Sample data |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 239 | * @perf_limits: Pointer to perf_limit unique to this CPU |
| 240 | * Not all field in the structure are applicable |
| 241 | * when per cpu controls are enforced |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 242 | * @acpi_perf_data: Stores ACPI perf information read from _PSS |
| 243 | * @valid_pss_table: Set to true for valid ACPI _PSS entries found |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 244 | * |
| 245 | * This structure stores per CPU instance data for all CPUs. |
| 246 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 247 | struct cpudata { |
| 248 | int cpu; |
| 249 | |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 250 | unsigned int policy; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 251 | struct update_util_data update_util; |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 252 | bool update_util_set; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 253 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 254 | struct pstate_data pstate; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 255 | struct vid_data vid; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 256 | struct _pid pid; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 257 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 258 | u64 last_update; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 259 | u64 last_sample_time; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 260 | u64 prev_aperf; |
| 261 | u64 prev_mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 262 | u64 prev_tsc; |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 263 | u64 prev_cummulative_iowait; |
Dirk Brandewie | d37e2b7 | 2014-02-12 10:01:04 -0800 | [diff] [blame] | 264 | struct sample sample; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 265 | struct perf_limits *perf_limits; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_ACPI |
| 267 | struct acpi_processor_performance acpi_perf_data; |
| 268 | bool valid_pss_table; |
| 269 | #endif |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 270 | unsigned int iowait_boost; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | static struct cpudata **all_cpu_data; |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 274 | |
| 275 | /** |
Rafael J. Wysocki | 3954517 | 2016-10-11 23:07:38 +0200 | [diff] [blame] | 276 | * struct pstate_adjust_policy - Stores static PID configuration data |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 277 | * @sample_rate_ms: PID calculation sample rate in ms |
| 278 | * @sample_rate_ns: Sample rate calculation in ns |
| 279 | * @deadband: PID deadband |
| 280 | * @setpoint: PID Setpoint |
| 281 | * @p_gain_pct: PID proportional gain |
| 282 | * @i_gain_pct: PID integral gain |
| 283 | * @d_gain_pct: PID derivative gain |
| 284 | * |
| 285 | * Stores per CPU model static PID configuration data. |
| 286 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 287 | struct pstate_adjust_policy { |
| 288 | int sample_rate_ms; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 289 | s64 sample_rate_ns; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 290 | int deadband; |
| 291 | int setpoint; |
| 292 | int p_gain_pct; |
| 293 | int d_gain_pct; |
| 294 | int i_gain_pct; |
| 295 | }; |
| 296 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 297 | /** |
| 298 | * struct pstate_funcs - Per CPU model specific callbacks |
| 299 | * @get_max: Callback to get maximum non turbo effective P state |
| 300 | * @get_max_physical: Callback to get maximum non turbo physical P state |
| 301 | * @get_min: Callback to get minimum P state |
| 302 | * @get_turbo: Callback to get turbo P state |
| 303 | * @get_scaling: Callback to get frequency scaling factor |
| 304 | * @get_val: Callback to convert P state to actual MSR write value |
| 305 | * @get_vid: Callback to get VID data for Atom platforms |
| 306 | * @get_target_pstate: Callback to a function to calculate next P state to use |
| 307 | * |
| 308 | * Core and Atom CPU models have different way to get P State limits. This |
| 309 | * structure is used to store those callbacks. |
| 310 | */ |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 311 | struct pstate_funcs { |
| 312 | int (*get_max)(void); |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 313 | int (*get_max_physical)(void); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 314 | int (*get_min)(void); |
| 315 | int (*get_turbo)(void); |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 316 | int (*get_scaling)(void); |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 317 | u64 (*get_val)(struct cpudata*, int pstate); |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 318 | void (*get_vid)(struct cpudata *); |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 319 | int32_t (*get_target_pstate)(struct cpudata *); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 320 | }; |
| 321 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 322 | /** |
| 323 | * struct cpu_defaults- Per CPU model default config data |
| 324 | * @pid_policy: PID config data |
| 325 | * @funcs: Callback function data |
| 326 | */ |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 327 | struct cpu_defaults { |
| 328 | struct pstate_adjust_policy pid_policy; |
| 329 | struct pstate_funcs funcs; |
| 330 | }; |
| 331 | |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 332 | static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu); |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 333 | static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu); |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 334 | |
Jisheng Zhang | 4a7cb7a | 2016-06-27 18:07:18 +0800 | [diff] [blame] | 335 | static struct pstate_adjust_policy pid_params __read_mostly; |
| 336 | static struct pstate_funcs pstate_funcs __read_mostly; |
| 337 | static int hwp_active __read_mostly; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 338 | static bool per_cpu_limits __read_mostly; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 339 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 340 | #ifdef CONFIG_ACPI |
| 341 | static bool acpi_ppc; |
| 342 | #endif |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 343 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 344 | static struct perf_limits performance_limits = { |
| 345 | .no_turbo = 0, |
| 346 | .turbo_disabled = 0, |
| 347 | .max_perf_pct = 100, |
| 348 | .max_perf = int_tofp(1), |
| 349 | .min_perf_pct = 100, |
| 350 | .min_perf = int_tofp(1), |
| 351 | .max_policy_pct = 100, |
| 352 | .max_sysfs_pct = 100, |
| 353 | .min_policy_pct = 0, |
| 354 | .min_sysfs_pct = 0, |
| 355 | }; |
| 356 | |
| 357 | static struct perf_limits powersave_limits = { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 358 | .no_turbo = 0, |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 359 | .turbo_disabled = 0, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 360 | .max_perf_pct = 100, |
| 361 | .max_perf = int_tofp(1), |
| 362 | .min_perf_pct = 0, |
| 363 | .min_perf = 0, |
Dirk Brandewie | d8f469e | 2013-05-07 08:20:26 -0700 | [diff] [blame] | 364 | .max_policy_pct = 100, |
| 365 | .max_sysfs_pct = 100, |
Kristen Carlson Accardi | a047599 | 2015-01-29 13:03:52 -0800 | [diff] [blame] | 366 | .min_policy_pct = 0, |
| 367 | .min_sysfs_pct = 0, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 368 | }; |
| 369 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 370 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE |
| 371 | static struct perf_limits *limits = &performance_limits; |
| 372 | #else |
| 373 | static struct perf_limits *limits = &powersave_limits; |
| 374 | #endif |
| 375 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 376 | #ifdef CONFIG_ACPI |
Srinivas Pandruvada | 2b3ec76 | 2016-04-27 15:48:08 -0700 | [diff] [blame] | 377 | |
| 378 | static bool intel_pstate_get_ppc_enable_status(void) |
| 379 | { |
| 380 | if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER || |
| 381 | acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER) |
| 382 | return true; |
| 383 | |
| 384 | return acpi_ppc; |
| 385 | } |
| 386 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 387 | static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy) |
| 388 | { |
| 389 | struct cpudata *cpu; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 390 | int ret; |
| 391 | int i; |
| 392 | |
Srinivas Pandruvada | e59a8f7 | 2016-05-04 15:07:34 -0700 | [diff] [blame] | 393 | if (hwp_active) |
| 394 | return; |
| 395 | |
Srinivas Pandruvada | 2b3ec76 | 2016-04-27 15:48:08 -0700 | [diff] [blame] | 396 | if (!intel_pstate_get_ppc_enable_status()) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 397 | return; |
| 398 | |
| 399 | cpu = all_cpu_data[policy->cpu]; |
| 400 | |
| 401 | ret = acpi_processor_register_performance(&cpu->acpi_perf_data, |
| 402 | policy->cpu); |
| 403 | if (ret) |
| 404 | return; |
| 405 | |
| 406 | /* |
| 407 | * Check if the control value in _PSS is for PERF_CTL MSR, which should |
| 408 | * guarantee that the states returned by it map to the states in our |
| 409 | * list directly. |
| 410 | */ |
| 411 | if (cpu->acpi_perf_data.control_register.space_id != |
| 412 | ACPI_ADR_SPACE_FIXED_HARDWARE) |
| 413 | goto err; |
| 414 | |
| 415 | /* |
| 416 | * If there is only one entry _PSS, simply ignore _PSS and continue as |
| 417 | * usual without taking _PSS into account |
| 418 | */ |
| 419 | if (cpu->acpi_perf_data.state_count < 2) |
| 420 | goto err; |
| 421 | |
| 422 | pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu); |
| 423 | for (i = 0; i < cpu->acpi_perf_data.state_count; i++) { |
| 424 | pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n", |
| 425 | (i == cpu->acpi_perf_data.state ? '*' : ' '), i, |
| 426 | (u32) cpu->acpi_perf_data.states[i].core_frequency, |
| 427 | (u32) cpu->acpi_perf_data.states[i].power, |
| 428 | (u32) cpu->acpi_perf_data.states[i].control); |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * The _PSS table doesn't contain whole turbo frequency range. |
| 433 | * This just contains +1 MHZ above the max non turbo frequency, |
| 434 | * with control value corresponding to max turbo ratio. But |
| 435 | * when cpufreq set policy is called, it will call with this |
| 436 | * max frequency, which will cause a reduced performance as |
| 437 | * this driver uses real max turbo frequency as the max |
| 438 | * frequency. So correct this frequency in _PSS table to |
Srinivas Pandruvada | b00345d | 2016-06-14 23:12:59 -0700 | [diff] [blame] | 439 | * correct max turbo frequency based on the turbo state. |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 440 | * Also need to convert to MHz as _PSS freq is in MHz. |
| 441 | */ |
Srinivas Pandruvada | b00345d | 2016-06-14 23:12:59 -0700 | [diff] [blame] | 442 | if (!limits->turbo_disabled) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 443 | cpu->acpi_perf_data.states[0].core_frequency = |
| 444 | policy->cpuinfo.max_freq / 1000; |
| 445 | cpu->valid_pss_table = true; |
Srinivas Pandruvada | 6cacd11 | 2016-05-29 23:31:23 -0700 | [diff] [blame] | 446 | pr_debug("_PPC limits will be enforced\n"); |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 447 | |
| 448 | return; |
| 449 | |
| 450 | err: |
| 451 | cpu->valid_pss_table = false; |
| 452 | acpi_processor_unregister_performance(policy->cpu); |
| 453 | } |
| 454 | |
| 455 | static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy) |
| 456 | { |
| 457 | struct cpudata *cpu; |
| 458 | |
| 459 | cpu = all_cpu_data[policy->cpu]; |
| 460 | if (!cpu->valid_pss_table) |
| 461 | return; |
| 462 | |
| 463 | acpi_processor_unregister_performance(policy->cpu); |
| 464 | } |
| 465 | |
| 466 | #else |
| 467 | static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy) |
| 468 | { |
| 469 | } |
| 470 | |
| 471 | static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy) |
| 472 | { |
| 473 | } |
| 474 | #endif |
| 475 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 476 | static inline void pid_reset(struct _pid *pid, int setpoint, int busy, |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 477 | int deadband, int integral) { |
Philippe Longepe | b54a0df | 2016-03-08 10:31:14 +0100 | [diff] [blame] | 478 | pid->setpoint = int_tofp(setpoint); |
| 479 | pid->deadband = int_tofp(deadband); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 480 | pid->integral = int_tofp(integral); |
Dirk Brandewie | d98d099 | 2014-02-12 10:01:05 -0800 | [diff] [blame] | 481 | pid->last_err = int_tofp(setpoint) - int_tofp(busy); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static inline void pid_p_gain_set(struct _pid *pid, int percent) |
| 485 | { |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 486 | pid->p_gain = div_fp(percent, 100); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static inline void pid_i_gain_set(struct _pid *pid, int percent) |
| 490 | { |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 491 | pid->i_gain = div_fp(percent, 100); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static inline void pid_d_gain_set(struct _pid *pid, int percent) |
| 495 | { |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 496 | pid->d_gain = div_fp(percent, 100); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Brennan Shacklett | d253d2a | 2013-10-21 09:20:32 -0700 | [diff] [blame] | 499 | static signed int pid_calc(struct _pid *pid, int32_t busy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 500 | { |
Brennan Shacklett | d253d2a | 2013-10-21 09:20:32 -0700 | [diff] [blame] | 501 | signed int result; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 502 | int32_t pterm, dterm, fp_error; |
| 503 | int32_t integral_limit; |
| 504 | |
Philippe Longepe | b54a0df | 2016-03-08 10:31:14 +0100 | [diff] [blame] | 505 | fp_error = pid->setpoint - busy; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 506 | |
Philippe Longepe | b54a0df | 2016-03-08 10:31:14 +0100 | [diff] [blame] | 507 | if (abs(fp_error) <= pid->deadband) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 508 | return 0; |
| 509 | |
| 510 | pterm = mul_fp(pid->p_gain, fp_error); |
| 511 | |
| 512 | pid->integral += fp_error; |
| 513 | |
Kristen Carlson Accardi | e0d4c8f | 2014-12-10 12:39:38 -0800 | [diff] [blame] | 514 | /* |
| 515 | * We limit the integral here so that it will never |
| 516 | * get higher than 30. This prevents it from becoming |
| 517 | * too large an input over long periods of time and allows |
| 518 | * it to get factored out sooner. |
| 519 | * |
| 520 | * The value of 30 was chosen through experimentation. |
| 521 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 522 | integral_limit = int_tofp(30); |
| 523 | if (pid->integral > integral_limit) |
| 524 | pid->integral = integral_limit; |
| 525 | if (pid->integral < -integral_limit) |
| 526 | pid->integral = -integral_limit; |
| 527 | |
Brennan Shacklett | d253d2a | 2013-10-21 09:20:32 -0700 | [diff] [blame] | 528 | dterm = mul_fp(pid->d_gain, fp_error - pid->last_err); |
| 529 | pid->last_err = fp_error; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 530 | |
| 531 | result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm; |
Doug Smythies | 51d211e | 2014-06-17 13:36:10 -0700 | [diff] [blame] | 532 | result = result + (1 << (FRAC_BITS-1)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 533 | return (signed int)fp_toint(result); |
| 534 | } |
| 535 | |
| 536 | static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu) |
| 537 | { |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 538 | pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct); |
| 539 | pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct); |
| 540 | pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 541 | |
Stratos Karafotis | 2d8d1f1 | 2014-07-18 08:37:20 -0700 | [diff] [blame] | 542 | pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 545 | static inline void intel_pstate_reset_all_pid(void) |
| 546 | { |
| 547 | unsigned int cpu; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 548 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 549 | for_each_online_cpu(cpu) { |
| 550 | if (all_cpu_data[cpu]) |
| 551 | intel_pstate_busy_pid_reset(all_cpu_data[cpu]); |
| 552 | } |
| 553 | } |
| 554 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 555 | static inline void update_turbo_state(void) |
| 556 | { |
| 557 | u64 misc_en; |
| 558 | struct cpudata *cpu; |
| 559 | |
| 560 | cpu = all_cpu_data[0]; |
| 561 | rdmsrl(MSR_IA32_MISC_ENABLE, misc_en); |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 562 | limits->turbo_disabled = |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 563 | (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE || |
| 564 | cpu->pstate.max_pstate == cpu->pstate.turbo_pstate); |
| 565 | } |
| 566 | |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 567 | static void intel_pstate_hwp_set(const struct cpumask *cpumask) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 568 | { |
Kristen Carlson Accardi | 74da56c | 2015-09-09 11:41:22 -0700 | [diff] [blame] | 569 | int min, hw_min, max, hw_max, cpu, range, adj_range; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 570 | struct perf_limits *perf_limits = limits; |
Kristen Carlson Accardi | 74da56c | 2015-09-09 11:41:22 -0700 | [diff] [blame] | 571 | u64 value, cap; |
| 572 | |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 573 | for_each_cpu(cpu, cpumask) { |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 574 | int max_perf_pct, min_perf_pct; |
| 575 | |
| 576 | if (per_cpu_limits) |
| 577 | perf_limits = all_cpu_data[cpu]->perf_limits; |
| 578 | |
Srinivas Pandruvada | f9f4872 | 2016-10-08 12:42:38 -0700 | [diff] [blame] | 579 | rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap); |
| 580 | hw_min = HWP_LOWEST_PERF(cap); |
| 581 | hw_max = HWP_HIGHEST_PERF(cap); |
| 582 | range = hw_max - hw_min; |
| 583 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 584 | max_perf_pct = perf_limits->max_perf_pct; |
| 585 | min_perf_pct = perf_limits->min_perf_pct; |
| 586 | |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 587 | rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 588 | adj_range = min_perf_pct * range / 100; |
Kristen Carlson Accardi | 74da56c | 2015-09-09 11:41:22 -0700 | [diff] [blame] | 589 | min = hw_min + adj_range; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 590 | value &= ~HWP_MIN_PERF(~0L); |
| 591 | value |= HWP_MIN_PERF(min); |
| 592 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 593 | adj_range = max_perf_pct * range / 100; |
Kristen Carlson Accardi | 74da56c | 2015-09-09 11:41:22 -0700 | [diff] [blame] | 594 | max = hw_min + adj_range; |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 595 | if (limits->no_turbo) { |
Kristen Carlson Accardi | 74da56c | 2015-09-09 11:41:22 -0700 | [diff] [blame] | 596 | hw_max = HWP_GUARANTEED_PERF(cap); |
| 597 | if (hw_max < max) |
| 598 | max = hw_max; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | value &= ~HWP_MAX_PERF(~0L); |
| 602 | value |= HWP_MAX_PERF(max); |
| 603 | wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value); |
| 604 | } |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 605 | } |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 606 | |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 607 | static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy) |
| 608 | { |
| 609 | if (hwp_active) |
| 610 | intel_pstate_hwp_set(policy->cpus); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 615 | static void intel_pstate_hwp_set_online_cpus(void) |
| 616 | { |
| 617 | get_online_cpus(); |
| 618 | intel_pstate_hwp_set(cpu_online_mask); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 619 | put_online_cpus(); |
| 620 | } |
| 621 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 622 | /************************** debugfs begin ************************/ |
| 623 | static int pid_param_set(void *data, u64 val) |
| 624 | { |
| 625 | *(u32 *)data = val; |
| 626 | intel_pstate_reset_all_pid(); |
| 627 | return 0; |
| 628 | } |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 629 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 630 | static int pid_param_get(void *data, u64 *val) |
| 631 | { |
| 632 | *val = *(u32 *)data; |
| 633 | return 0; |
| 634 | } |
Stratos Karafotis | 2d8d1f1 | 2014-07-18 08:37:20 -0700 | [diff] [blame] | 635 | DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n"); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 636 | |
| 637 | struct pid_param { |
| 638 | char *name; |
| 639 | void *value; |
| 640 | }; |
| 641 | |
| 642 | static struct pid_param pid_files[] = { |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 643 | {"sample_rate_ms", &pid_params.sample_rate_ms}, |
| 644 | {"d_gain_pct", &pid_params.d_gain_pct}, |
| 645 | {"i_gain_pct", &pid_params.i_gain_pct}, |
| 646 | {"deadband", &pid_params.deadband}, |
| 647 | {"setpoint", &pid_params.setpoint}, |
| 648 | {"p_gain_pct", &pid_params.p_gain_pct}, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 649 | {NULL, NULL} |
| 650 | }; |
| 651 | |
Stratos Karafotis | 317dd50 | 2014-07-18 08:37:17 -0700 | [diff] [blame] | 652 | static void __init intel_pstate_debug_expose_params(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 653 | { |
Stratos Karafotis | 317dd50 | 2014-07-18 08:37:17 -0700 | [diff] [blame] | 654 | struct dentry *debugfs_parent; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 655 | int i = 0; |
| 656 | |
Srinivas Pandruvada | 185d824 | 2016-10-21 09:38:20 -0700 | [diff] [blame] | 657 | if (hwp_active || |
| 658 | pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 659 | return; |
Srinivas Pandruvada | 185d824 | 2016-10-21 09:38:20 -0700 | [diff] [blame] | 660 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 661 | debugfs_parent = debugfs_create_dir("pstate_snb", NULL); |
| 662 | if (IS_ERR_OR_NULL(debugfs_parent)) |
| 663 | return; |
| 664 | while (pid_files[i].name) { |
| 665 | debugfs_create_file(pid_files[i].name, 0660, |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 666 | debugfs_parent, pid_files[i].value, |
| 667 | &fops_pid_param); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 668 | i++; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /************************** debugfs end ************************/ |
| 673 | |
| 674 | /************************** sysfs begin ************************/ |
| 675 | #define show_one(file_name, object) \ |
| 676 | static ssize_t show_##file_name \ |
| 677 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 678 | { \ |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 679 | return sprintf(buf, "%u\n", limits->object); \ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 682 | static ssize_t show_turbo_pct(struct kobject *kobj, |
| 683 | struct attribute *attr, char *buf) |
| 684 | { |
| 685 | struct cpudata *cpu; |
| 686 | int total, no_turbo, turbo_pct; |
| 687 | uint32_t turbo_fp; |
| 688 | |
| 689 | cpu = all_cpu_data[0]; |
| 690 | |
| 691 | total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; |
| 692 | no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1; |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 693 | turbo_fp = div_fp(no_turbo, total); |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 694 | turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100))); |
| 695 | return sprintf(buf, "%u\n", turbo_pct); |
| 696 | } |
| 697 | |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 698 | static ssize_t show_num_pstates(struct kobject *kobj, |
| 699 | struct attribute *attr, char *buf) |
| 700 | { |
| 701 | struct cpudata *cpu; |
| 702 | int total; |
| 703 | |
| 704 | cpu = all_cpu_data[0]; |
| 705 | total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; |
| 706 | return sprintf(buf, "%u\n", total); |
| 707 | } |
| 708 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 709 | static ssize_t show_no_turbo(struct kobject *kobj, |
| 710 | struct attribute *attr, char *buf) |
| 711 | { |
| 712 | ssize_t ret; |
| 713 | |
| 714 | update_turbo_state(); |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 715 | if (limits->turbo_disabled) |
| 716 | ret = sprintf(buf, "%u\n", limits->turbo_disabled); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 717 | else |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 718 | ret = sprintf(buf, "%u\n", limits->no_turbo); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 719 | |
| 720 | return ret; |
| 721 | } |
| 722 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 723 | static ssize_t store_no_turbo(struct kobject *a, struct attribute *b, |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 724 | const char *buf, size_t count) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 725 | { |
| 726 | unsigned int input; |
| 727 | int ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 728 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 729 | ret = sscanf(buf, "%u", &input); |
| 730 | if (ret != 1) |
| 731 | return -EINVAL; |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 732 | |
| 733 | update_turbo_state(); |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 734 | if (limits->turbo_disabled) { |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 735 | pr_warn("Turbo disabled by BIOS or unavailable on processor\n"); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 736 | return -EPERM; |
Dirk Brandewie | dd5fbf7 | 2014-06-20 07:27:59 -0700 | [diff] [blame] | 737 | } |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 738 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 739 | limits->no_turbo = clamp_t(int, input, 0, 1); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 740 | |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 741 | if (hwp_active) |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 742 | intel_pstate_hwp_set_online_cpus(); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 743 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 744 | return count; |
| 745 | } |
| 746 | |
| 747 | static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b, |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 748 | const char *buf, size_t count) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 749 | { |
| 750 | unsigned int input; |
| 751 | int ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 752 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 753 | ret = sscanf(buf, "%u", &input); |
| 754 | if (ret != 1) |
| 755 | return -EINVAL; |
| 756 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 757 | limits->max_sysfs_pct = clamp_t(int, input, 0 , 100); |
| 758 | limits->max_perf_pct = min(limits->max_policy_pct, |
| 759 | limits->max_sysfs_pct); |
| 760 | limits->max_perf_pct = max(limits->min_policy_pct, |
| 761 | limits->max_perf_pct); |
| 762 | limits->max_perf_pct = max(limits->min_perf_pct, |
| 763 | limits->max_perf_pct); |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 764 | limits->max_perf = div_fp(limits->max_perf_pct, 100); |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 765 | |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 766 | if (hwp_active) |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 767 | intel_pstate_hwp_set_online_cpus(); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 768 | return count; |
| 769 | } |
| 770 | |
| 771 | static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b, |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 772 | const char *buf, size_t count) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 773 | { |
| 774 | unsigned int input; |
| 775 | int ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 776 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 777 | ret = sscanf(buf, "%u", &input); |
| 778 | if (ret != 1) |
| 779 | return -EINVAL; |
Kristen Carlson Accardi | a047599 | 2015-01-29 13:03:52 -0800 | [diff] [blame] | 780 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 781 | limits->min_sysfs_pct = clamp_t(int, input, 0 , 100); |
| 782 | limits->min_perf_pct = max(limits->min_policy_pct, |
| 783 | limits->min_sysfs_pct); |
| 784 | limits->min_perf_pct = min(limits->max_policy_pct, |
| 785 | limits->min_perf_pct); |
| 786 | limits->min_perf_pct = min(limits->max_perf_pct, |
| 787 | limits->min_perf_pct); |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 788 | limits->min_perf = div_fp(limits->min_perf_pct, 100); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 789 | |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 790 | if (hwp_active) |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 791 | intel_pstate_hwp_set_online_cpus(); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 792 | return count; |
| 793 | } |
| 794 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 795 | show_one(max_perf_pct, max_perf_pct); |
| 796 | show_one(min_perf_pct, min_perf_pct); |
| 797 | |
| 798 | define_one_global_rw(no_turbo); |
| 799 | define_one_global_rw(max_perf_pct); |
| 800 | define_one_global_rw(min_perf_pct); |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 801 | define_one_global_ro(turbo_pct); |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 802 | define_one_global_ro(num_pstates); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 803 | |
| 804 | static struct attribute *intel_pstate_attributes[] = { |
| 805 | &no_turbo.attr, |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 806 | &turbo_pct.attr, |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 807 | &num_pstates.attr, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 808 | NULL |
| 809 | }; |
| 810 | |
| 811 | static struct attribute_group intel_pstate_attr_group = { |
| 812 | .attrs = intel_pstate_attributes, |
| 813 | }; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 814 | |
Stratos Karafotis | 317dd50 | 2014-07-18 08:37:17 -0700 | [diff] [blame] | 815 | static void __init intel_pstate_sysfs_expose_params(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 816 | { |
Stratos Karafotis | 317dd50 | 2014-07-18 08:37:17 -0700 | [diff] [blame] | 817 | struct kobject *intel_pstate_kobject; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 818 | int rc; |
| 819 | |
| 820 | intel_pstate_kobject = kobject_create_and_add("intel_pstate", |
| 821 | &cpu_subsys.dev_root->kobj); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 822 | if (WARN_ON(!intel_pstate_kobject)) |
| 823 | return; |
| 824 | |
Stratos Karafotis | 2d8d1f1 | 2014-07-18 08:37:20 -0700 | [diff] [blame] | 825 | rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 826 | if (WARN_ON(rc)) |
| 827 | return; |
| 828 | |
| 829 | /* |
| 830 | * If per cpu limits are enforced there are no global limits, so |
| 831 | * return without creating max/min_perf_pct attributes |
| 832 | */ |
| 833 | if (per_cpu_limits) |
| 834 | return; |
| 835 | |
| 836 | rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr); |
| 837 | WARN_ON(rc); |
| 838 | |
| 839 | rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr); |
| 840 | WARN_ON(rc); |
| 841 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 842 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 843 | /************************** sysfs end ************************/ |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 844 | |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 845 | static void intel_pstate_hwp_enable(struct cpudata *cpudata) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 846 | { |
Srinivas Pandruvada | f05c966 | 2016-02-25 15:09:31 -0800 | [diff] [blame] | 847 | /* First disable HWP notification interrupt as we don't process them */ |
Srinivas Pandruvada | da7de91 | 2016-07-19 16:52:01 -0700 | [diff] [blame] | 848 | if (static_cpu_has(X86_FEATURE_HWP_NOTIFY)) |
| 849 | wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00); |
Srinivas Pandruvada | f05c966 | 2016-02-25 15:09:31 -0800 | [diff] [blame] | 850 | |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 851 | wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 854 | static int atom_get_min_pstate(void) |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 855 | { |
| 856 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 857 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 858 | rdmsrl(ATOM_RATIOS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 859 | return (value >> 8) & 0x7F; |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 860 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 861 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 862 | static int atom_get_max_pstate(void) |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 863 | { |
| 864 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 865 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 866 | rdmsrl(ATOM_RATIOS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 867 | return (value >> 16) & 0x7F; |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 870 | static int atom_get_turbo_pstate(void) |
Dirk Brandewie | 61d8d2a | 2014-02-12 10:01:07 -0800 | [diff] [blame] | 871 | { |
| 872 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 873 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 874 | rdmsrl(ATOM_TURBO_RATIOS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 875 | return value & 0x7F; |
Dirk Brandewie | 61d8d2a | 2014-02-12 10:01:07 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 878 | static u64 atom_get_val(struct cpudata *cpudata, int pstate) |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 879 | { |
| 880 | u64 val; |
| 881 | int32_t vid_fp; |
| 882 | u32 vid; |
| 883 | |
Chen Yu | 144c8e1 | 2015-07-29 23:53:10 +0800 | [diff] [blame] | 884 | val = (u64)pstate << 8; |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 885 | if (limits->no_turbo && !limits->turbo_disabled) |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 886 | val |= (u64)1 << 32; |
| 887 | |
| 888 | vid_fp = cpudata->vid.min + mul_fp( |
| 889 | int_tofp(pstate - cpudata->pstate.min_pstate), |
| 890 | cpudata->vid.ratio); |
| 891 | |
| 892 | vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max); |
Dirk Brandewie | d022a65 | 2014-10-13 08:37:44 -0700 | [diff] [blame] | 893 | vid = ceiling_fp(vid_fp); |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 894 | |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 895 | if (pstate > cpudata->pstate.max_pstate) |
| 896 | vid = cpudata->vid.turbo; |
| 897 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 898 | return val | vid; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 901 | static int silvermont_get_scaling(void) |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 902 | { |
| 903 | u64 value; |
| 904 | int i; |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 905 | /* Defined in Table 35-6 from SDM (Sept 2015) */ |
| 906 | static int silvermont_freq_table[] = { |
| 907 | 83300, 100000, 133300, 116700, 80000}; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 908 | |
| 909 | rdmsrl(MSR_FSB_FREQ, value); |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 910 | i = value & 0x7; |
| 911 | WARN_ON(i > 4); |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 912 | |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 913 | return silvermont_freq_table[i]; |
| 914 | } |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 915 | |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 916 | static int airmont_get_scaling(void) |
| 917 | { |
| 918 | u64 value; |
| 919 | int i; |
| 920 | /* Defined in Table 35-10 from SDM (Sept 2015) */ |
| 921 | static int airmont_freq_table[] = { |
| 922 | 83300, 100000, 133300, 116700, 80000, |
| 923 | 93300, 90000, 88900, 87500}; |
| 924 | |
| 925 | rdmsrl(MSR_FSB_FREQ, value); |
| 926 | i = value & 0xF; |
| 927 | WARN_ON(i > 8); |
| 928 | |
| 929 | return airmont_freq_table[i]; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 932 | static void atom_get_vid(struct cpudata *cpudata) |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 933 | { |
| 934 | u64 value; |
| 935 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 936 | rdmsrl(ATOM_VIDS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 937 | cpudata->vid.min = int_tofp((value >> 8) & 0x7f); |
| 938 | cpudata->vid.max = int_tofp((value >> 16) & 0x7f); |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 939 | cpudata->vid.ratio = div_fp( |
| 940 | cpudata->vid.max - cpudata->vid.min, |
| 941 | int_tofp(cpudata->pstate.max_pstate - |
| 942 | cpudata->pstate.min_pstate)); |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 943 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 944 | rdmsrl(ATOM_TURBO_VIDS, value); |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 945 | cpudata->vid.turbo = value & 0x7f; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 948 | static int core_get_min_pstate(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 949 | { |
| 950 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 951 | |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 952 | rdmsrl(MSR_PLATFORM_INFO, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 953 | return (value >> 40) & 0xFF; |
| 954 | } |
| 955 | |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 956 | static int core_get_max_pstate_physical(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 957 | { |
| 958 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 959 | |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 960 | rdmsrl(MSR_PLATFORM_INFO, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 961 | return (value >> 8) & 0xFF; |
| 962 | } |
| 963 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 964 | static int core_get_max_pstate(void) |
| 965 | { |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 966 | u64 tar; |
| 967 | u64 plat_info; |
| 968 | int max_pstate; |
| 969 | int err; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 970 | |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 971 | rdmsrl(MSR_PLATFORM_INFO, plat_info); |
| 972 | max_pstate = (plat_info >> 8) & 0xFF; |
| 973 | |
| 974 | err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar); |
| 975 | if (!err) { |
| 976 | /* Do some sanity checking for safety */ |
| 977 | if (plat_info & 0x600000000) { |
| 978 | u64 tdp_ctrl; |
| 979 | u64 tdp_ratio; |
| 980 | int tdp_msr; |
| 981 | |
| 982 | err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl); |
| 983 | if (err) |
| 984 | goto skip_tar; |
| 985 | |
Jan Kiszka | 5fc8f707 | 2016-07-08 20:42:04 +0200 | [diff] [blame] | 986 | tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x3); |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 987 | err = rdmsrl_safe(tdp_msr, &tdp_ratio); |
| 988 | if (err) |
| 989 | goto skip_tar; |
| 990 | |
Srinivas Pandruvada | 1becf03 | 2016-04-22 19:53:59 -0700 | [diff] [blame] | 991 | /* For level 1 and 2, bits[23:16] contain the ratio */ |
| 992 | if (tdp_ctrl) |
| 993 | tdp_ratio >>= 16; |
| 994 | |
| 995 | tdp_ratio &= 0xff; /* ratios are only 8 bits long */ |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 996 | if (tdp_ratio - 1 == tar) { |
| 997 | max_pstate = tar; |
| 998 | pr_debug("max_pstate=TAC %x\n", max_pstate); |
| 999 | } else { |
| 1000 | goto skip_tar; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | skip_tar: |
| 1006 | return max_pstate; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1009 | static int core_get_turbo_pstate(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1010 | { |
| 1011 | u64 value; |
| 1012 | int nont, ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1013 | |
Srinivas Pandruvada | 100cf6f | 2016-07-06 16:07:55 -0700 | [diff] [blame] | 1014 | rdmsrl(MSR_TURBO_RATIO_LIMIT, value); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1015 | nont = core_get_max_pstate(); |
Stratos Karafotis | 285cb99 | 2014-07-18 08:37:21 -0700 | [diff] [blame] | 1016 | ret = (value) & 255; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1017 | if (ret <= nont) |
| 1018 | ret = nont; |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1022 | static inline int core_get_scaling(void) |
| 1023 | { |
| 1024 | return 100000; |
| 1025 | } |
| 1026 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1027 | static u64 core_get_val(struct cpudata *cpudata, int pstate) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1028 | { |
| 1029 | u64 val; |
| 1030 | |
Chen Yu | 144c8e1 | 2015-07-29 23:53:10 +0800 | [diff] [blame] | 1031 | val = (u64)pstate << 8; |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 1032 | if (limits->no_turbo && !limits->turbo_disabled) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1033 | val |= (u64)1 << 32; |
| 1034 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1035 | return val; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1038 | static int knl_get_turbo_pstate(void) |
| 1039 | { |
| 1040 | u64 value; |
| 1041 | int nont, ret; |
| 1042 | |
Srinivas Pandruvada | 100cf6f | 2016-07-06 16:07:55 -0700 | [diff] [blame] | 1043 | rdmsrl(MSR_TURBO_RATIO_LIMIT, value); |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1044 | nont = core_get_max_pstate(); |
| 1045 | ret = (((value) >> 8) & 0xFF); |
| 1046 | if (ret <= nont) |
| 1047 | ret = nont; |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1051 | static struct cpu_defaults core_params = { |
| 1052 | .pid_policy = { |
| 1053 | .sample_rate_ms = 10, |
| 1054 | .deadband = 0, |
| 1055 | .setpoint = 97, |
| 1056 | .p_gain_pct = 20, |
| 1057 | .d_gain_pct = 0, |
| 1058 | .i_gain_pct = 0, |
| 1059 | }, |
| 1060 | .funcs = { |
| 1061 | .get_max = core_get_max_pstate, |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 1062 | .get_max_physical = core_get_max_pstate_physical, |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1063 | .get_min = core_get_min_pstate, |
| 1064 | .get_turbo = core_get_turbo_pstate, |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1065 | .get_scaling = core_get_scaling, |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1066 | .get_val = core_get_val, |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1067 | .get_target_pstate = get_target_pstate_use_performance, |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1068 | }, |
| 1069 | }; |
| 1070 | |
Julia Lawall | 42ce892 | 2016-09-11 15:06:01 +0200 | [diff] [blame] | 1071 | static const struct cpu_defaults silvermont_params = { |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1072 | .pid_policy = { |
| 1073 | .sample_rate_ms = 10, |
| 1074 | .deadband = 0, |
Kristen Carlson Accardi | 6a82ba6 | 2015-04-10 11:06:43 -0700 | [diff] [blame] | 1075 | .setpoint = 60, |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1076 | .p_gain_pct = 14, |
| 1077 | .d_gain_pct = 0, |
| 1078 | .i_gain_pct = 4, |
| 1079 | }, |
| 1080 | .funcs = { |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 1081 | .get_max = atom_get_max_pstate, |
| 1082 | .get_max_physical = atom_get_max_pstate, |
| 1083 | .get_min = atom_get_min_pstate, |
| 1084 | .get_turbo = atom_get_turbo_pstate, |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1085 | .get_val = atom_get_val, |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1086 | .get_scaling = silvermont_get_scaling, |
| 1087 | .get_vid = atom_get_vid, |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1088 | .get_target_pstate = get_target_pstate_use_cpu_load, |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1089 | }, |
| 1090 | }; |
| 1091 | |
Julia Lawall | 42ce892 | 2016-09-11 15:06:01 +0200 | [diff] [blame] | 1092 | static const struct cpu_defaults airmont_params = { |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1093 | .pid_policy = { |
| 1094 | .sample_rate_ms = 10, |
| 1095 | .deadband = 0, |
| 1096 | .setpoint = 60, |
| 1097 | .p_gain_pct = 14, |
| 1098 | .d_gain_pct = 0, |
| 1099 | .i_gain_pct = 4, |
| 1100 | }, |
| 1101 | .funcs = { |
| 1102 | .get_max = atom_get_max_pstate, |
| 1103 | .get_max_physical = atom_get_max_pstate, |
| 1104 | .get_min = atom_get_min_pstate, |
| 1105 | .get_turbo = atom_get_turbo_pstate, |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1106 | .get_val = atom_get_val, |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1107 | .get_scaling = airmont_get_scaling, |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 1108 | .get_vid = atom_get_vid, |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1109 | .get_target_pstate = get_target_pstate_use_cpu_load, |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1110 | }, |
| 1111 | }; |
| 1112 | |
Julia Lawall | 42ce892 | 2016-09-11 15:06:01 +0200 | [diff] [blame] | 1113 | static const struct cpu_defaults knl_params = { |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1114 | .pid_policy = { |
| 1115 | .sample_rate_ms = 10, |
| 1116 | .deadband = 0, |
| 1117 | .setpoint = 97, |
| 1118 | .p_gain_pct = 20, |
| 1119 | .d_gain_pct = 0, |
| 1120 | .i_gain_pct = 0, |
| 1121 | }, |
| 1122 | .funcs = { |
| 1123 | .get_max = core_get_max_pstate, |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 1124 | .get_max_physical = core_get_max_pstate_physical, |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1125 | .get_min = core_get_min_pstate, |
| 1126 | .get_turbo = knl_get_turbo_pstate, |
Lukasz Anaczkowski | 69cefc2 | 2015-07-21 10:41:13 +0200 | [diff] [blame] | 1127 | .get_scaling = core_get_scaling, |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1128 | .get_val = core_get_val, |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1129 | .get_target_pstate = get_target_pstate_use_performance, |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1130 | }, |
| 1131 | }; |
| 1132 | |
Julia Lawall | 42ce892 | 2016-09-11 15:06:01 +0200 | [diff] [blame] | 1133 | static const struct cpu_defaults bxt_params = { |
Srinivas Pandruvada | 41bad47 | 2016-06-09 15:34:41 -0700 | [diff] [blame] | 1134 | .pid_policy = { |
| 1135 | .sample_rate_ms = 10, |
| 1136 | .deadband = 0, |
| 1137 | .setpoint = 60, |
| 1138 | .p_gain_pct = 14, |
| 1139 | .d_gain_pct = 0, |
| 1140 | .i_gain_pct = 4, |
| 1141 | }, |
| 1142 | .funcs = { |
| 1143 | .get_max = core_get_max_pstate, |
| 1144 | .get_max_physical = core_get_max_pstate_physical, |
| 1145 | .get_min = core_get_min_pstate, |
| 1146 | .get_turbo = core_get_turbo_pstate, |
| 1147 | .get_scaling = core_get_scaling, |
| 1148 | .get_val = core_get_val, |
| 1149 | .get_target_pstate = get_target_pstate_use_cpu_load, |
| 1150 | }, |
| 1151 | }; |
| 1152 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1153 | static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max) |
| 1154 | { |
| 1155 | int max_perf = cpu->pstate.turbo_pstate; |
Dirk Brandewie | 7244cb6 | 2013-10-21 09:20:33 -0700 | [diff] [blame] | 1156 | int max_perf_adj; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1157 | int min_perf; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1158 | struct perf_limits *perf_limits = limits; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1159 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 1160 | if (limits->no_turbo || limits->turbo_disabled) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1161 | max_perf = cpu->pstate.max_pstate; |
| 1162 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1163 | if (per_cpu_limits) |
| 1164 | perf_limits = cpu->perf_limits; |
| 1165 | |
Kristen Carlson Accardi | e0d4c8f | 2014-12-10 12:39:38 -0800 | [diff] [blame] | 1166 | /* |
| 1167 | * performance can be limited by user through sysfs, by cpufreq |
| 1168 | * policy, or by cpu specific default values determined through |
| 1169 | * experimentation. |
| 1170 | */ |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1171 | max_perf_adj = fp_toint(max_perf * perf_limits->max_perf); |
Rafael J. Wysocki | 799281a | 2015-11-18 23:29:56 +0100 | [diff] [blame] | 1172 | *max = clamp_t(int, max_perf_adj, |
| 1173 | cpu->pstate.min_pstate, cpu->pstate.turbo_pstate); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1174 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1175 | min_perf = fp_toint(max_perf * perf_limits->min_perf); |
Rafael J. Wysocki | 799281a | 2015-11-18 23:29:56 +0100 | [diff] [blame] | 1176 | *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1179 | static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate) |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1180 | { |
Rafael J. Wysocki | bc95a45 | 2016-07-19 15:10:37 +0200 | [diff] [blame] | 1181 | trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu); |
| 1182 | cpu->pstate.current_pstate = pstate; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1183 | /* |
| 1184 | * Generally, there is no guarantee that this code will always run on |
| 1185 | * the CPU being updated, so force the register update to run on the |
| 1186 | * right CPU. |
| 1187 | */ |
| 1188 | wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL, |
| 1189 | pstate_funcs.get_val(cpu, pstate)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1192 | static void intel_pstate_set_min_pstate(struct cpudata *cpu) |
| 1193 | { |
| 1194 | intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate); |
| 1195 | } |
| 1196 | |
| 1197 | static void intel_pstate_max_within_limits(struct cpudata *cpu) |
| 1198 | { |
| 1199 | int min_pstate, max_pstate; |
| 1200 | |
| 1201 | update_turbo_state(); |
| 1202 | intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate); |
| 1203 | intel_pstate_set_pstate(cpu, max_pstate); |
| 1204 | } |
| 1205 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1206 | static void intel_pstate_get_cpu_pstates(struct cpudata *cpu) |
| 1207 | { |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1208 | cpu->pstate.min_pstate = pstate_funcs.get_min(); |
| 1209 | cpu->pstate.max_pstate = pstate_funcs.get_max(); |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 1210 | cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical(); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1211 | cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(); |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1212 | cpu->pstate.scaling = pstate_funcs.get_scaling(); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1213 | |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1214 | if (pstate_funcs.get_vid) |
| 1215 | pstate_funcs.get_vid(cpu); |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1216 | |
| 1217 | intel_pstate_set_min_pstate(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1220 | static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1221 | { |
Stratos Karafotis | 6b17ddb | 2014-04-29 20:53:49 +0300 | [diff] [blame] | 1222 | struct sample *sample = &cpu->sample; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1223 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1224 | sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1227 | static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1228 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1229 | u64 aperf, mperf; |
Stratos Karafotis | 4ab60c3 | 2014-07-18 08:37:24 -0700 | [diff] [blame] | 1230 | unsigned long flags; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1231 | u64 tsc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1232 | |
Stratos Karafotis | 4ab60c3 | 2014-07-18 08:37:24 -0700 | [diff] [blame] | 1233 | local_irq_save(flags); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1234 | rdmsrl(MSR_IA32_APERF, aperf); |
| 1235 | rdmsrl(MSR_IA32_MPERF, mperf); |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1236 | tsc = rdtsc(); |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1237 | if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) { |
Srinivas Pandruvada | 8e601a9 | 2015-10-15 12:34:21 -0700 | [diff] [blame] | 1238 | local_irq_restore(flags); |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1239 | return false; |
Srinivas Pandruvada | 8e601a9 | 2015-10-15 12:34:21 -0700 | [diff] [blame] | 1240 | } |
Stratos Karafotis | 4ab60c3 | 2014-07-18 08:37:24 -0700 | [diff] [blame] | 1241 | local_irq_restore(flags); |
Dirk Brandewie | b69880f | 2014-01-16 10:32:25 -0800 | [diff] [blame] | 1242 | |
Dirk Brandewie | c4ee841 | 2014-05-29 09:32:24 -0700 | [diff] [blame] | 1243 | cpu->last_sample_time = cpu->sample.time; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1244 | cpu->sample.time = time; |
Dirk Brandewie | d37e2b7 | 2014-02-12 10:01:04 -0800 | [diff] [blame] | 1245 | cpu->sample.aperf = aperf; |
| 1246 | cpu->sample.mperf = mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1247 | cpu->sample.tsc = tsc; |
Dirk Brandewie | d37e2b7 | 2014-02-12 10:01:04 -0800 | [diff] [blame] | 1248 | cpu->sample.aperf -= cpu->prev_aperf; |
| 1249 | cpu->sample.mperf -= cpu->prev_mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1250 | cpu->sample.tsc -= cpu->prev_tsc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1251 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1252 | cpu->prev_aperf = aperf; |
| 1253 | cpu->prev_mperf = mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1254 | cpu->prev_tsc = tsc; |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1255 | /* |
| 1256 | * First time this function is invoked in a given cycle, all of the |
| 1257 | * previous sample data fields are equal to zero or stale and they must |
| 1258 | * be populated with meaningful numbers for things to work, so assume |
| 1259 | * that sample.time will always be reset before setting the utilization |
| 1260 | * update hook and make the caller skip the sample then. |
| 1261 | */ |
| 1262 | return !!cpu->last_sample_time; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Philippe Longepe | 8fa520a | 2016-03-06 08:34:06 +0100 | [diff] [blame] | 1265 | static inline int32_t get_avg_frequency(struct cpudata *cpu) |
| 1266 | { |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1267 | return mul_ext_fp(cpu->sample.core_avg_perf, |
| 1268 | cpu->pstate.max_pstate_physical * cpu->pstate.scaling); |
Philippe Longepe | 8fa520a | 2016-03-06 08:34:06 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
Philippe Longepe | bdcaa23 | 2016-04-22 11:46:09 -0700 | [diff] [blame] | 1271 | static inline int32_t get_avg_pstate(struct cpudata *cpu) |
| 1272 | { |
Rafael J. Wysocki | 8edb0a6 | 2016-05-11 19:10:42 +0200 | [diff] [blame] | 1273 | return mul_ext_fp(cpu->pstate.max_pstate_physical, |
| 1274 | cpu->sample.core_avg_perf); |
Philippe Longepe | bdcaa23 | 2016-04-22 11:46:09 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1277 | static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu) |
| 1278 | { |
| 1279 | struct sample *sample = &cpu->sample; |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1280 | int32_t busy_frac, boost; |
Rafael J. Wysocki | 0843e83 | 2016-10-06 14:07:51 +0200 | [diff] [blame] | 1281 | int target, avg_pstate; |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1282 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1283 | busy_frac = div_fp(sample->mperf, sample->tsc); |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 1284 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1285 | boost = cpu->iowait_boost; |
| 1286 | cpu->iowait_boost >>= 1; |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 1287 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1288 | if (busy_frac < boost) |
| 1289 | busy_frac = boost; |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 1290 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1291 | sample->busy_scaled = busy_frac * 100; |
Rafael J. Wysocki | 0843e83 | 2016-10-06 14:07:51 +0200 | [diff] [blame] | 1292 | |
| 1293 | target = limits->no_turbo || limits->turbo_disabled ? |
| 1294 | cpu->pstate.max_pstate : cpu->pstate.turbo_pstate; |
| 1295 | target += target >> 2; |
| 1296 | target = mul_fp(target, busy_frac); |
| 1297 | if (target < cpu->pstate.min_pstate) |
| 1298 | target = cpu->pstate.min_pstate; |
| 1299 | |
| 1300 | /* |
| 1301 | * If the average P-state during the previous cycle was higher than the |
| 1302 | * current target, add 50% of the difference to the target to reduce |
| 1303 | * possible performance oscillations and offset possible performance |
| 1304 | * loss related to moving the workload from one CPU to another within |
| 1305 | * a package/module. |
| 1306 | */ |
| 1307 | avg_pstate = get_avg_pstate(cpu); |
| 1308 | if (avg_pstate > target) |
| 1309 | target += (avg_pstate - target) >> 1; |
| 1310 | |
| 1311 | return target; |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1314 | static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1315 | { |
Rafael J. Wysocki | 1aa7a6e | 2016-05-11 19:11:26 +0200 | [diff] [blame] | 1316 | int32_t perf_scaled, max_pstate, current_pstate, sample_ratio; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1317 | u64 duration_ns; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1318 | |
Kristen Carlson Accardi | e0d4c8f | 2014-12-10 12:39:38 -0800 | [diff] [blame] | 1319 | /* |
Rafael J. Wysocki | f00593a | 2016-09-30 03:13:34 +0200 | [diff] [blame] | 1320 | * perf_scaled is the ratio of the average P-state during the last |
| 1321 | * sampling period to the P-state requested last time (in percent). |
| 1322 | * |
| 1323 | * That measures the system's response to the previous P-state |
| 1324 | * selection. |
Kristen Carlson Accardi | e0d4c8f | 2014-12-10 12:39:38 -0800 | [diff] [blame] | 1325 | */ |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 1326 | max_pstate = cpu->pstate.max_pstate_physical; |
| 1327 | current_pstate = cpu->pstate.current_pstate; |
Rafael J. Wysocki | 1aa7a6e | 2016-05-11 19:11:26 +0200 | [diff] [blame] | 1328 | perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf, |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1329 | div_fp(100 * max_pstate, current_pstate)); |
Dirk Brandewie | c4ee841 | 2014-05-29 09:32:24 -0700 | [diff] [blame] | 1330 | |
Kristen Carlson Accardi | e0d4c8f | 2014-12-10 12:39:38 -0800 | [diff] [blame] | 1331 | /* |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1332 | * Since our utilization update callback will not run unless we are |
| 1333 | * in C0, check if the actual elapsed time is significantly greater (3x) |
| 1334 | * than our sample interval. If it is, then we were idle for a long |
Rafael J. Wysocki | 1aa7a6e | 2016-05-11 19:11:26 +0200 | [diff] [blame] | 1335 | * enough period of time to adjust our performance metric. |
Kristen Carlson Accardi | e0d4c8f | 2014-12-10 12:39:38 -0800 | [diff] [blame] | 1336 | */ |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1337 | duration_ns = cpu->sample.time - cpu->last_sample_time; |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1338 | if ((s64)duration_ns > pid_params.sample_rate_ns * 3) { |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 1339 | sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns); |
Rafael J. Wysocki | 1aa7a6e | 2016-05-11 19:11:26 +0200 | [diff] [blame] | 1340 | perf_scaled = mul_fp(perf_scaled, sample_ratio); |
Rafael J. Wysocki | ffb8105 | 2016-04-10 05:59:10 +0200 | [diff] [blame] | 1341 | } else { |
| 1342 | sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc); |
| 1343 | if (sample_ratio < int_tofp(1)) |
Rafael J. Wysocki | 1aa7a6e | 2016-05-11 19:11:26 +0200 | [diff] [blame] | 1344 | perf_scaled = 0; |
Dirk Brandewie | c4ee841 | 2014-05-29 09:32:24 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Rafael J. Wysocki | 1aa7a6e | 2016-05-11 19:11:26 +0200 | [diff] [blame] | 1347 | cpu->sample.busy_scaled = perf_scaled; |
| 1348 | return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1351 | static inline void intel_pstate_update_pstate(struct cpudata *cpu, int pstate) |
| 1352 | { |
| 1353 | int max_perf, min_perf; |
| 1354 | |
| 1355 | update_turbo_state(); |
| 1356 | |
| 1357 | intel_pstate_get_min_max(cpu, &min_perf, &max_perf); |
| 1358 | pstate = clamp_t(int, pstate, min_perf, max_perf); |
Rafael J. Wysocki | bc95a45 | 2016-07-19 15:10:37 +0200 | [diff] [blame] | 1359 | trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu); |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1360 | if (pstate == cpu->pstate.current_pstate) |
| 1361 | return; |
| 1362 | |
Rafael J. Wysocki | bc95a45 | 2016-07-19 15:10:37 +0200 | [diff] [blame] | 1363 | cpu->pstate.current_pstate = pstate; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1364 | wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate)); |
| 1365 | } |
| 1366 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1367 | static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu) |
| 1368 | { |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1369 | int from, target_pstate; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1370 | struct sample *sample; |
| 1371 | |
| 1372 | from = cpu->pstate.current_pstate; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1373 | |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 1374 | target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ? |
| 1375 | cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1376 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1377 | intel_pstate_update_pstate(cpu, target_pstate); |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1378 | |
| 1379 | sample = &cpu->sample; |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1380 | trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf), |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1381 | fp_toint(sample->busy_scaled), |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1382 | from, |
| 1383 | cpu->pstate.current_pstate, |
| 1384 | sample->mperf, |
| 1385 | sample->aperf, |
| 1386 | sample->tsc, |
Srinivas Pandruvada | 3ba7bca | 2016-09-13 17:41:33 -0700 | [diff] [blame] | 1387 | get_avg_frequency(cpu), |
| 1388 | fp_toint(cpu->iowait_boost * 100)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1391 | static void intel_pstate_update_util(struct update_util_data *data, u64 time, |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 1392 | unsigned int flags) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1393 | { |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1394 | struct cpudata *cpu = container_of(data, struct cpudata, update_util); |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1395 | u64 delta_ns; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1396 | |
Rafael J. Wysocki | 1d29815 | 2016-10-19 02:53:26 +0200 | [diff] [blame] | 1397 | if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) { |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1398 | if (flags & SCHED_CPUFREQ_IOWAIT) { |
| 1399 | cpu->iowait_boost = int_tofp(1); |
| 1400 | } else if (cpu->iowait_boost) { |
| 1401 | /* Clear iowait_boost if the CPU may have been idle. */ |
| 1402 | delta_ns = time - cpu->last_update; |
| 1403 | if (delta_ns > TICK_NSEC) |
| 1404 | cpu->iowait_boost = 0; |
| 1405 | } |
| 1406 | cpu->last_update = time; |
| 1407 | } |
| 1408 | |
| 1409 | delta_ns = time - cpu->sample.time; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1410 | if ((s64)delta_ns >= pid_params.sample_rate_ns) { |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1411 | bool sample_taken = intel_pstate_sample(cpu, time); |
| 1412 | |
Rafael J. Wysocki | 6d45b71 | 2016-05-04 14:01:10 +0200 | [diff] [blame] | 1413 | if (sample_taken) { |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1414 | intel_pstate_calc_avg_perf(cpu); |
Rafael J. Wysocki | 6d45b71 | 2016-05-04 14:01:10 +0200 | [diff] [blame] | 1415 | if (!hwp_active) |
| 1416 | intel_pstate_adjust_busy_pstate(cpu); |
| 1417 | } |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1418 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | #define ICPU(model, policy) \ |
Dirk Brandewie | 6cbd7ee | 2014-01-06 10:59:16 -0800 | [diff] [blame] | 1422 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\ |
| 1423 | (unsigned long)&policy } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1424 | |
| 1425 | static const struct x86_cpu_id intel_pstate_cpu_ids[] = { |
Dave Hansen | 5b20c94 | 2016-06-02 17:19:45 -0700 | [diff] [blame] | 1426 | ICPU(INTEL_FAM6_SANDYBRIDGE, core_params), |
| 1427 | ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params), |
| 1428 | ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params), |
| 1429 | ICPU(INTEL_FAM6_IVYBRIDGE, core_params), |
| 1430 | ICPU(INTEL_FAM6_HASWELL_CORE, core_params), |
| 1431 | ICPU(INTEL_FAM6_BROADWELL_CORE, core_params), |
| 1432 | ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params), |
| 1433 | ICPU(INTEL_FAM6_HASWELL_X, core_params), |
| 1434 | ICPU(INTEL_FAM6_HASWELL_ULT, core_params), |
| 1435 | ICPU(INTEL_FAM6_HASWELL_GT3E, core_params), |
| 1436 | ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params), |
| 1437 | ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params), |
| 1438 | ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params), |
| 1439 | ICPU(INTEL_FAM6_BROADWELL_X, core_params), |
| 1440 | ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params), |
| 1441 | ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params), |
| 1442 | ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params), |
Srinivas Pandruvada | 41bad47 | 2016-06-09 15:34:41 -0700 | [diff] [blame] | 1443 | ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params), |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1444 | {} |
| 1445 | }; |
| 1446 | MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids); |
| 1447 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1448 | static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = { |
Dave Hansen | 5b20c94 | 2016-06-02 17:19:45 -0700 | [diff] [blame] | 1449 | ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params), |
Srinivas Pandruvada | 65c1262 | 2016-07-23 15:12:06 -0700 | [diff] [blame] | 1450 | ICPU(INTEL_FAM6_BROADWELL_X, core_params), |
| 1451 | ICPU(INTEL_FAM6_SKYLAKE_X, core_params), |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1452 | {} |
| 1453 | }; |
| 1454 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1455 | static int intel_pstate_init_cpu(unsigned int cpunum) |
| 1456 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1457 | struct cpudata *cpu; |
| 1458 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1459 | cpu = all_cpu_data[cpunum]; |
| 1460 | |
| 1461 | if (!cpu) { |
| 1462 | unsigned int size = sizeof(struct cpudata); |
| 1463 | |
| 1464 | if (per_cpu_limits) |
| 1465 | size += sizeof(struct perf_limits); |
| 1466 | |
| 1467 | cpu = kzalloc(size, GFP_KERNEL); |
| 1468 | if (!cpu) |
| 1469 | return -ENOMEM; |
| 1470 | |
| 1471 | all_cpu_data[cpunum] = cpu; |
| 1472 | if (per_cpu_limits) |
| 1473 | cpu->perf_limits = (struct perf_limits *)(cpu + 1); |
| 1474 | |
| 1475 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1476 | |
| 1477 | cpu = all_cpu_data[cpunum]; |
| 1478 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1479 | cpu->cpu = cpunum; |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1480 | |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1481 | if (hwp_active) { |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1482 | intel_pstate_hwp_enable(cpu); |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1483 | pid_params.sample_rate_ms = 50; |
| 1484 | pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC; |
| 1485 | } |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1486 | |
Vincent Minet | 179e847 | 2014-07-05 01:51:33 +0200 | [diff] [blame] | 1487 | intel_pstate_get_cpu_pstates(cpu); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1488 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1489 | intel_pstate_busy_pid_reset(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1490 | |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 1491 | pr_debug("controlling: cpu %d\n", cpunum); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | static unsigned int intel_pstate_get(unsigned int cpu_num) |
| 1497 | { |
Rafael J. Wysocki | f96fd0c | 2016-05-07 02:24:48 +0200 | [diff] [blame] | 1498 | struct cpudata *cpu = all_cpu_data[cpu_num]; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1499 | |
Rafael J. Wysocki | f96fd0c | 2016-05-07 02:24:48 +0200 | [diff] [blame] | 1500 | return cpu ? get_avg_frequency(cpu) : 0; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1503 | static void intel_pstate_set_update_util_hook(unsigned int cpu_num) |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1504 | { |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1505 | struct cpudata *cpu = all_cpu_data[cpu_num]; |
| 1506 | |
Rafael J. Wysocki | 5ab666e | 2016-06-27 23:47:15 +0200 | [diff] [blame] | 1507 | if (cpu->update_util_set) |
| 1508 | return; |
| 1509 | |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1510 | /* Prevent intel_pstate_update_util() from using stale data. */ |
| 1511 | cpu->sample.time = 0; |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 1512 | cpufreq_add_update_util_hook(cpu_num, &cpu->update_util, |
| 1513 | intel_pstate_update_util); |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 1514 | cpu->update_util_set = true; |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | static void intel_pstate_clear_update_util_hook(unsigned int cpu) |
| 1518 | { |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 1519 | struct cpudata *cpu_data = all_cpu_data[cpu]; |
| 1520 | |
| 1521 | if (!cpu_data->update_util_set) |
| 1522 | return; |
| 1523 | |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 1524 | cpufreq_remove_update_util_hook(cpu); |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 1525 | cpu_data->update_util_set = false; |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1526 | synchronize_sched(); |
| 1527 | } |
| 1528 | |
Srinivas Pandruvada | 30a3915 | 2016-04-03 19:42:11 -0700 | [diff] [blame] | 1529 | static void intel_pstate_set_performance_limits(struct perf_limits *limits) |
| 1530 | { |
| 1531 | limits->no_turbo = 0; |
| 1532 | limits->turbo_disabled = 0; |
| 1533 | limits->max_perf_pct = 100; |
| 1534 | limits->max_perf = int_tofp(1); |
| 1535 | limits->min_perf_pct = 100; |
| 1536 | limits->min_perf = int_tofp(1); |
| 1537 | limits->max_policy_pct = 100; |
| 1538 | limits->max_sysfs_pct = 100; |
| 1539 | limits->min_policy_pct = 0; |
| 1540 | limits->min_sysfs_pct = 0; |
| 1541 | } |
| 1542 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1543 | static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy, |
| 1544 | struct perf_limits *limits) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1545 | { |
Prarit Bhargava | 8478f53 | 2015-11-20 18:47:56 -0500 | [diff] [blame] | 1546 | limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100, |
| 1547 | policy->cpuinfo.max_freq); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1548 | limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0, 100); |
Srinivas Pandruvada | 5879f87 | 2016-10-25 13:20:41 -0700 | [diff] [blame^] | 1549 | if (policy->max == policy->min) { |
| 1550 | limits->min_policy_pct = limits->max_policy_pct; |
| 1551 | } else { |
| 1552 | limits->min_policy_pct = (policy->min * 100) / |
| 1553 | policy->cpuinfo.max_freq; |
| 1554 | limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, |
| 1555 | 0, 100); |
| 1556 | } |
Chen Yu | 43717aa | 2015-09-09 18:27:31 +0800 | [diff] [blame] | 1557 | |
| 1558 | /* Normalize user input to [min_policy_pct, max_policy_pct] */ |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 1559 | limits->min_perf_pct = max(limits->min_policy_pct, |
| 1560 | limits->min_sysfs_pct); |
| 1561 | limits->min_perf_pct = min(limits->max_policy_pct, |
| 1562 | limits->min_perf_pct); |
| 1563 | limits->max_perf_pct = min(limits->max_policy_pct, |
| 1564 | limits->max_sysfs_pct); |
| 1565 | limits->max_perf_pct = max(limits->min_policy_pct, |
| 1566 | limits->max_perf_pct); |
Chen Yu | 43717aa | 2015-09-09 18:27:31 +0800 | [diff] [blame] | 1567 | |
| 1568 | /* Make sure min_perf_pct <= max_perf_pct */ |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 1569 | limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct); |
Chen Yu | 43717aa | 2015-09-09 18:27:31 +0800 | [diff] [blame] | 1570 | |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 1571 | limits->min_perf = div_fp(limits->min_perf_pct, 100); |
| 1572 | limits->max_perf = div_fp(limits->max_perf_pct, 100); |
Srinivas Pandruvada | 2c2c1af | 2016-06-07 17:38:52 -0700 | [diff] [blame] | 1573 | limits->max_perf = round_up(limits->max_perf, FRAC_BITS); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1574 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1575 | pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu, |
| 1576 | limits->max_perf_pct, limits->min_perf_pct); |
| 1577 | } |
| 1578 | |
| 1579 | static int intel_pstate_set_policy(struct cpufreq_policy *policy) |
| 1580 | { |
| 1581 | struct cpudata *cpu; |
| 1582 | struct perf_limits *perf_limits = NULL; |
| 1583 | |
| 1584 | if (!policy->cpuinfo.max_freq) |
| 1585 | return -ENODEV; |
| 1586 | |
| 1587 | pr_debug("set_policy cpuinfo.max %u policy->max %u\n", |
| 1588 | policy->cpuinfo.max_freq, policy->max); |
| 1589 | |
| 1590 | cpu = all_cpu_data[policy->cpu]; |
| 1591 | cpu->policy = policy->policy; |
| 1592 | |
| 1593 | if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate && |
| 1594 | policy->max < policy->cpuinfo.max_freq && |
| 1595 | policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) { |
| 1596 | pr_debug("policy->max > max non turbo frequency\n"); |
| 1597 | policy->max = policy->cpuinfo.max_freq; |
| 1598 | } |
| 1599 | |
| 1600 | if (per_cpu_limits) |
| 1601 | perf_limits = cpu->perf_limits; |
| 1602 | |
| 1603 | if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) { |
| 1604 | if (!perf_limits) { |
| 1605 | limits = &performance_limits; |
| 1606 | perf_limits = limits; |
| 1607 | } |
| 1608 | if (policy->max >= policy->cpuinfo.max_freq) { |
| 1609 | pr_debug("set performance\n"); |
| 1610 | intel_pstate_set_performance_limits(perf_limits); |
| 1611 | goto out; |
| 1612 | } |
| 1613 | } else { |
| 1614 | pr_debug("set powersave\n"); |
| 1615 | if (!perf_limits) { |
| 1616 | limits = &powersave_limits; |
| 1617 | perf_limits = limits; |
| 1618 | } |
| 1619 | |
| 1620 | } |
| 1621 | |
| 1622 | intel_pstate_update_perf_limits(policy, perf_limits); |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1623 | out: |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 1624 | if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) { |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1625 | /* |
| 1626 | * NOHZ_FULL CPUs need this as the governor callback may not |
| 1627 | * be invoked on them. |
| 1628 | */ |
| 1629 | intel_pstate_clear_update_util_hook(policy->cpu); |
| 1630 | intel_pstate_max_within_limits(cpu); |
| 1631 | } |
| 1632 | |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1633 | intel_pstate_set_update_util_hook(policy->cpu); |
| 1634 | |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1635 | intel_pstate_hwp_set_policy(policy); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1636 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | static int intel_pstate_verify_policy(struct cpufreq_policy *policy) |
| 1641 | { |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 1642 | cpufreq_verify_within_cpu_limits(policy); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1643 | |
Stratos Karafotis | 285cb99 | 2014-07-18 08:37:21 -0700 | [diff] [blame] | 1644 | if (policy->policy != CPUFREQ_POLICY_POWERSAVE && |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 1645 | policy->policy != CPUFREQ_POLICY_PERFORMANCE) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1646 | return -EINVAL; |
| 1647 | |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
Dirk Brandewie | bb18008 | 2014-03-19 08:45:54 -0700 | [diff] [blame] | 1651 | static void intel_pstate_stop_cpu(struct cpufreq_policy *policy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1652 | { |
Dirk Brandewie | bb18008 | 2014-03-19 08:45:54 -0700 | [diff] [blame] | 1653 | int cpu_num = policy->cpu; |
| 1654 | struct cpudata *cpu = all_cpu_data[cpu_num]; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1655 | |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 1656 | pr_debug("CPU %d exiting\n", cpu_num); |
Dirk Brandewie | bb18008 | 2014-03-19 08:45:54 -0700 | [diff] [blame] | 1657 | |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1658 | intel_pstate_clear_update_util_hook(cpu_num); |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1659 | |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1660 | if (hwp_active) |
| 1661 | return; |
| 1662 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1663 | intel_pstate_set_min_pstate(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Paul Gortmaker | 2760984 | 2013-06-19 13:54:04 -0400 | [diff] [blame] | 1666 | static int intel_pstate_cpu_init(struct cpufreq_policy *policy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1667 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1668 | struct cpudata *cpu; |
Dirk Brandewie | 52e0a50 | 2013-10-15 11:06:14 -0700 | [diff] [blame] | 1669 | int rc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1670 | |
| 1671 | rc = intel_pstate_init_cpu(policy->cpu); |
| 1672 | if (rc) |
| 1673 | return rc; |
| 1674 | |
| 1675 | cpu = all_cpu_data[policy->cpu]; |
| 1676 | |
Prarit Bhargava | 51443fb | 2015-10-15 07:34:15 -0400 | [diff] [blame] | 1677 | if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1678 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; |
| 1679 | else |
| 1680 | policy->policy = CPUFREQ_POLICY_POWERSAVE; |
| 1681 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1682 | /* |
| 1683 | * We need sane value in the cpu->perf_limits, so inherit from global |
| 1684 | * perf_limits limits, which are seeded with values based on the |
| 1685 | * CONFIG_CPU_FREQ_DEFAULT_GOV_*, during boot up. |
| 1686 | */ |
| 1687 | if (per_cpu_limits) |
| 1688 | memcpy(cpu->perf_limits, limits, sizeof(struct perf_limits)); |
| 1689 | |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1690 | policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling; |
| 1691 | policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1692 | |
| 1693 | /* cpuinfo and default policy values */ |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1694 | policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling; |
Srinivas Pandruvada | 983e600 | 2016-06-07 17:38:53 -0700 | [diff] [blame] | 1695 | update_turbo_state(); |
| 1696 | policy->cpuinfo.max_freq = limits->turbo_disabled ? |
| 1697 | cpu->pstate.max_pstate : cpu->pstate.turbo_pstate; |
| 1698 | policy->cpuinfo.max_freq *= cpu->pstate.scaling; |
| 1699 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1700 | intel_pstate_init_acpi_perf_limits(policy); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1701 | policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL; |
| 1702 | cpumask_set_cpu(policy->cpu, policy->cpus); |
| 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1707 | static int intel_pstate_cpu_exit(struct cpufreq_policy *policy) |
| 1708 | { |
| 1709 | intel_pstate_exit_perf_limits(policy); |
| 1710 | |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1714 | static struct cpufreq_driver intel_pstate_driver = { |
| 1715 | .flags = CPUFREQ_CONST_LOOPS, |
| 1716 | .verify = intel_pstate_verify_policy, |
| 1717 | .setpolicy = intel_pstate_set_policy, |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1718 | .resume = intel_pstate_hwp_set_policy, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1719 | .get = intel_pstate_get, |
| 1720 | .init = intel_pstate_cpu_init, |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1721 | .exit = intel_pstate_cpu_exit, |
Dirk Brandewie | bb18008 | 2014-03-19 08:45:54 -0700 | [diff] [blame] | 1722 | .stop_cpu = intel_pstate_stop_cpu, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1723 | .name = "intel_pstate", |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1724 | }; |
| 1725 | |
Jisheng Zhang | eed4360 | 2016-06-27 18:07:16 +0800 | [diff] [blame] | 1726 | static int no_load __initdata; |
| 1727 | static int no_hwp __initdata; |
| 1728 | static int hwp_only __initdata; |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1729 | static unsigned int force_load __initdata; |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 1730 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1731 | static int __init intel_pstate_msrs_not_valid(void) |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 1732 | { |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1733 | if (!pstate_funcs.get_max() || |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 1734 | !pstate_funcs.get_min() || |
| 1735 | !pstate_funcs.get_turbo()) |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 1736 | return -ENODEV; |
| 1737 | |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 1738 | return 0; |
| 1739 | } |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1740 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1741 | static void __init copy_pid_params(struct pstate_adjust_policy *policy) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1742 | { |
| 1743 | pid_params.sample_rate_ms = policy->sample_rate_ms; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1744 | pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1745 | pid_params.p_gain_pct = policy->p_gain_pct; |
| 1746 | pid_params.i_gain_pct = policy->i_gain_pct; |
| 1747 | pid_params.d_gain_pct = policy->d_gain_pct; |
| 1748 | pid_params.deadband = policy->deadband; |
| 1749 | pid_params.setpoint = policy->setpoint; |
| 1750 | } |
| 1751 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1752 | static void __init copy_cpu_funcs(struct pstate_funcs *funcs) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1753 | { |
| 1754 | pstate_funcs.get_max = funcs->get_max; |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 1755 | pstate_funcs.get_max_physical = funcs->get_max_physical; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1756 | pstate_funcs.get_min = funcs->get_min; |
| 1757 | pstate_funcs.get_turbo = funcs->get_turbo; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1758 | pstate_funcs.get_scaling = funcs->get_scaling; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1759 | pstate_funcs.get_val = funcs->get_val; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1760 | pstate_funcs.get_vid = funcs->get_vid; |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1761 | pstate_funcs.get_target_pstate = funcs->get_target_pstate; |
| 1762 | |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1765 | #ifdef CONFIG_ACPI |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1766 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1767 | static bool __init intel_pstate_no_acpi_pss(void) |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1768 | { |
| 1769 | int i; |
| 1770 | |
| 1771 | for_each_possible_cpu(i) { |
| 1772 | acpi_status status; |
| 1773 | union acpi_object *pss; |
| 1774 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1775 | struct acpi_processor *pr = per_cpu(processors, i); |
| 1776 | |
| 1777 | if (!pr) |
| 1778 | continue; |
| 1779 | |
| 1780 | status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer); |
| 1781 | if (ACPI_FAILURE(status)) |
| 1782 | continue; |
| 1783 | |
| 1784 | pss = buffer.pointer; |
| 1785 | if (pss && pss->type == ACPI_TYPE_PACKAGE) { |
| 1786 | kfree(pss); |
| 1787 | return false; |
| 1788 | } |
| 1789 | |
| 1790 | kfree(pss); |
| 1791 | } |
| 1792 | |
| 1793 | return true; |
| 1794 | } |
| 1795 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1796 | static bool __init intel_pstate_has_acpi_ppc(void) |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 1797 | { |
| 1798 | int i; |
| 1799 | |
| 1800 | for_each_possible_cpu(i) { |
| 1801 | struct acpi_processor *pr = per_cpu(processors, i); |
| 1802 | |
| 1803 | if (!pr) |
| 1804 | continue; |
| 1805 | if (acpi_has_method(pr->handle, "_PPC")) |
| 1806 | return true; |
| 1807 | } |
| 1808 | return false; |
| 1809 | } |
| 1810 | |
| 1811 | enum { |
| 1812 | PSS, |
| 1813 | PPC, |
| 1814 | }; |
| 1815 | |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1816 | struct hw_vendor_info { |
| 1817 | u16 valid; |
| 1818 | char oem_id[ACPI_OEM_ID_SIZE]; |
| 1819 | char oem_table_id[ACPI_OEM_TABLE_ID_SIZE]; |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 1820 | int oem_pwr_table; |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1821 | }; |
| 1822 | |
| 1823 | /* Hardware vendor-specific info that has its own power management modes */ |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1824 | static struct hw_vendor_info vendor_info[] __initdata = { |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 1825 | {1, "HP ", "ProLiant", PSS}, |
| 1826 | {1, "ORACLE", "X4-2 ", PPC}, |
| 1827 | {1, "ORACLE", "X4-2L ", PPC}, |
| 1828 | {1, "ORACLE", "X4-2B ", PPC}, |
| 1829 | {1, "ORACLE", "X3-2 ", PPC}, |
| 1830 | {1, "ORACLE", "X3-2L ", PPC}, |
| 1831 | {1, "ORACLE", "X3-2B ", PPC}, |
| 1832 | {1, "ORACLE", "X4470M2 ", PPC}, |
| 1833 | {1, "ORACLE", "X4270M3 ", PPC}, |
| 1834 | {1, "ORACLE", "X4270M2 ", PPC}, |
| 1835 | {1, "ORACLE", "X4170M2 ", PPC}, |
Ethan Zhao | 5aecc3c | 2015-08-05 09:28:50 +0900 | [diff] [blame] | 1836 | {1, "ORACLE", "X4170 M3", PPC}, |
| 1837 | {1, "ORACLE", "X4275 M3", PPC}, |
| 1838 | {1, "ORACLE", "X6-2 ", PPC}, |
| 1839 | {1, "ORACLE", "Sudbury ", PPC}, |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1840 | {0, "", ""}, |
| 1841 | }; |
| 1842 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1843 | static bool __init intel_pstate_platform_pwr_mgmt_exists(void) |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1844 | { |
| 1845 | struct acpi_table_header hdr; |
| 1846 | struct hw_vendor_info *v_info; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1847 | const struct x86_cpu_id *id; |
| 1848 | u64 misc_pwr; |
| 1849 | |
| 1850 | id = x86_match_cpu(intel_pstate_cpu_oob_ids); |
| 1851 | if (id) { |
| 1852 | rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr); |
| 1853 | if ( misc_pwr & (1 << 8)) |
| 1854 | return true; |
| 1855 | } |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1856 | |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 1857 | if (acpi_disabled || |
| 1858 | ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr))) |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1859 | return false; |
| 1860 | |
| 1861 | for (v_info = vendor_info; v_info->valid; v_info++) { |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 1862 | if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) && |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 1863 | !strncmp(hdr.oem_table_id, v_info->oem_table_id, |
| 1864 | ACPI_OEM_TABLE_ID_SIZE)) |
| 1865 | switch (v_info->oem_pwr_table) { |
| 1866 | case PSS: |
| 1867 | return intel_pstate_no_acpi_pss(); |
| 1868 | case PPC: |
Ethan Zhao | aa4ea34 | 2014-12-09 10:43:19 +0900 | [diff] [blame] | 1869 | return intel_pstate_has_acpi_ppc() && |
| 1870 | (!force_load); |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 1871 | } |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | return false; |
| 1875 | } |
| 1876 | #else /* CONFIG_ACPI not enabled */ |
| 1877 | static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; } |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 1878 | static inline bool intel_pstate_has_acpi_ppc(void) { return false; } |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 1879 | #endif /* CONFIG_ACPI */ |
| 1880 | |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 1881 | static const struct x86_cpu_id hwp_support_ids[] __initconst = { |
| 1882 | { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP }, |
| 1883 | {} |
| 1884 | }; |
| 1885 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1886 | static int __init intel_pstate_init(void) |
| 1887 | { |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 1888 | int cpu, rc = 0; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1889 | const struct x86_cpu_id *id; |
Borislav Petkov | 64df1fd | 2015-04-03 15:19:53 +0200 | [diff] [blame] | 1890 | struct cpu_defaults *cpu_def; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1891 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 1892 | if (no_load) |
| 1893 | return -ENODEV; |
| 1894 | |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 1895 | if (x86_match_cpu(hwp_support_ids) && !no_hwp) { |
| 1896 | copy_cpu_funcs(&core_params.funcs); |
| 1897 | hwp_active++; |
| 1898 | goto hwp_cpu_matched; |
| 1899 | } |
| 1900 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1901 | id = x86_match_cpu(intel_pstate_cpu_ids); |
| 1902 | if (!id) |
| 1903 | return -ENODEV; |
| 1904 | |
Borislav Petkov | 64df1fd | 2015-04-03 15:19:53 +0200 | [diff] [blame] | 1905 | cpu_def = (struct cpu_defaults *)id->driver_data; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1906 | |
Borislav Petkov | 64df1fd | 2015-04-03 15:19:53 +0200 | [diff] [blame] | 1907 | copy_pid_params(&cpu_def->pid_policy); |
| 1908 | copy_cpu_funcs(&cpu_def->funcs); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1909 | |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 1910 | if (intel_pstate_msrs_not_valid()) |
| 1911 | return -ENODEV; |
| 1912 | |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 1913 | hwp_cpu_matched: |
| 1914 | /* |
| 1915 | * The Intel pstate driver will be ignored if the platform |
| 1916 | * firmware has its own power management modes. |
| 1917 | */ |
| 1918 | if (intel_pstate_platform_pwr_mgmt_exists()) |
| 1919 | return -ENODEV; |
| 1920 | |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 1921 | pr_info("Intel P-state driver initializing\n"); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1922 | |
Wei Yongjun | b57ffac | 2013-05-13 08:03:43 +0000 | [diff] [blame] | 1923 | all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus()); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1924 | if (!all_cpu_data) |
| 1925 | return -ENOMEM; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1926 | |
Kristen Carlson Accardi | d64c3b0 | 2015-02-06 13:41:55 -0800 | [diff] [blame] | 1927 | if (!hwp_active && hwp_only) |
| 1928 | goto out; |
| 1929 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1930 | rc = cpufreq_register_driver(&intel_pstate_driver); |
| 1931 | if (rc) |
| 1932 | goto out; |
| 1933 | |
| 1934 | intel_pstate_debug_expose_params(); |
| 1935 | intel_pstate_sysfs_expose_params(); |
Dirk Brandewie | b69880f | 2014-01-16 10:32:25 -0800 | [diff] [blame] | 1936 | |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 1937 | if (hwp_active) |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 1938 | pr_info("HWP enabled\n"); |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 1939 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1940 | return rc; |
| 1941 | out: |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 1942 | get_online_cpus(); |
| 1943 | for_each_online_cpu(cpu) { |
| 1944 | if (all_cpu_data[cpu]) { |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1945 | intel_pstate_clear_update_util_hook(cpu); |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 1946 | kfree(all_cpu_data[cpu]); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | put_online_cpus(); |
| 1951 | vfree(all_cpu_data); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1952 | return -ENODEV; |
| 1953 | } |
| 1954 | device_initcall(intel_pstate_init); |
| 1955 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 1956 | static int __init intel_pstate_setup(char *str) |
| 1957 | { |
| 1958 | if (!str) |
| 1959 | return -EINVAL; |
| 1960 | |
| 1961 | if (!strcmp(str, "disable")) |
| 1962 | no_load = 1; |
Prarit Bhargava | 539342f | 2015-10-22 09:43:31 -0400 | [diff] [blame] | 1963 | if (!strcmp(str, "no_hwp")) { |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 1964 | pr_info("HWP disabled\n"); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1965 | no_hwp = 1; |
Prarit Bhargava | 539342f | 2015-10-22 09:43:31 -0400 | [diff] [blame] | 1966 | } |
Ethan Zhao | aa4ea34 | 2014-12-09 10:43:19 +0900 | [diff] [blame] | 1967 | if (!strcmp(str, "force")) |
| 1968 | force_load = 1; |
Kristen Carlson Accardi | d64c3b0 | 2015-02-06 13:41:55 -0800 | [diff] [blame] | 1969 | if (!strcmp(str, "hwp_only")) |
| 1970 | hwp_only = 1; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1971 | if (!strcmp(str, "per_cpu_perf_limits")) |
| 1972 | per_cpu_limits = true; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1973 | |
| 1974 | #ifdef CONFIG_ACPI |
| 1975 | if (!strcmp(str, "support_acpi_ppc")) |
| 1976 | acpi_ppc = true; |
| 1977 | #endif |
| 1978 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 1979 | return 0; |
| 1980 | } |
| 1981 | early_param("intel_pstate", intel_pstate_setup); |
| 1982 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1983 | MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>"); |
| 1984 | MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors"); |
| 1985 | MODULE_LICENSE("GPL"); |