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