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> |
Ingo Molnar | 55687da | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 22 | #include <linux/sched/cpufreq.h> |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 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> |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 29 | #include <linux/acpi.h> |
Stephen Rothwell | d647230 | 2015-06-02 19:01:38 +1000 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 31 | #include <trace/events/power.h> |
| 32 | |
| 33 | #include <asm/div64.h> |
| 34 | #include <asm/msr.h> |
| 35 | #include <asm/cpu_device_id.h> |
Borislav Petkov | 64df1fd | 2015-04-03 15:19:53 +0200 | [diff] [blame] | 36 | #include <asm/cpufeature.h> |
Dave Hansen | 5b20c94 | 2016-06-02 17:19:45 -0700 | [diff] [blame] | 37 | #include <asm/intel-family.h> |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 38 | |
Rafael J. Wysocki | d77d488 | 2017-08-10 01:09:16 +0200 | [diff] [blame] | 39 | #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC) |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 40 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 41 | #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000 |
Rafael J. Wysocki | 1b72e7f | 2017-04-11 00:20:41 +0200 | [diff] [blame] | 42 | #define INTEL_CPUFREQ_TRANSITION_DELAY 500 |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 43 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_ACPI |
| 45 | #include <acpi/processor.h> |
Rafael J. Wysocki | 1766900 | 2016-11-22 12:24:00 -0800 | [diff] [blame] | 46 | #include <acpi/cppc_acpi.h> |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 47 | #endif |
| 48 | |
Dirk Brandewie | f0fe3cd | 2014-05-29 09:32:23 -0700 | [diff] [blame] | 49 | #define FRAC_BITS 8 |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 50 | #define int_tofp(X) ((int64_t)(X) << FRAC_BITS) |
| 51 | #define fp_toint(X) ((X) >> FRAC_BITS) |
Dirk Brandewie | f0fe3cd | 2014-05-29 09:32:23 -0700 | [diff] [blame] | 52 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 53 | #define EXT_BITS 6 |
| 54 | #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS) |
Srinivas Pandruvada | d5dd33d | 2016-11-21 16:33:20 -0800 | [diff] [blame] | 55 | #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS) |
| 56 | #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS) |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 57 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 58 | static inline int32_t mul_fp(int32_t x, int32_t y) |
| 59 | { |
| 60 | return ((int64_t)x * (int64_t)y) >> FRAC_BITS; |
| 61 | } |
| 62 | |
Prarit Bhargava | 7180ddd | 2015-06-15 13:43:29 -0400 | [diff] [blame] | 63 | static inline int32_t div_fp(s64 x, s64 y) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 64 | { |
Prarit Bhargava | 7180ddd | 2015-06-15 13:43:29 -0400 | [diff] [blame] | 65 | return div64_s64((int64_t)x << FRAC_BITS, y); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Dirk Brandewie | d022a65 | 2014-10-13 08:37:44 -0700 | [diff] [blame] | 68 | static inline int ceiling_fp(int32_t x) |
| 69 | { |
| 70 | int mask, ret; |
| 71 | |
| 72 | ret = fp_toint(x); |
| 73 | mask = (1 << FRAC_BITS) - 1; |
| 74 | if (x & mask) |
| 75 | ret += 1; |
| 76 | return ret; |
| 77 | } |
| 78 | |
Rafael J. Wysocki | ff35f02 | 2017-03-28 00:09:18 +0200 | [diff] [blame] | 79 | static inline int32_t percent_fp(int percent) |
| 80 | { |
| 81 | return div_fp(percent, 100); |
| 82 | } |
| 83 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 84 | static inline u64 mul_ext_fp(u64 x, u64 y) |
| 85 | { |
| 86 | return (x * y) >> EXT_FRAC_BITS; |
| 87 | } |
| 88 | |
| 89 | static inline u64 div_ext_fp(u64 x, u64 y) |
| 90 | { |
| 91 | return div64_u64(x << EXT_FRAC_BITS, y); |
| 92 | } |
| 93 | |
Rafael J. Wysocki | e4c204c | 2017-03-14 16:18:34 +0100 | [diff] [blame] | 94 | static inline int32_t percent_ext_fp(int percent) |
| 95 | { |
| 96 | return div_ext_fp(percent, 100); |
| 97 | } |
| 98 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 99 | /** |
| 100 | * struct sample - Store performance sample |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 101 | * @core_avg_perf: Ratio of APERF/MPERF which is the actual average |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 102 | * performance during last sample period |
| 103 | * @busy_scaled: Scaled busy value which is used to calculate next |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 104 | * P state. This can be different than core_avg_perf |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 105 | * to account for cpu idle period |
| 106 | * @aperf: Difference of actual performance frequency clock count |
| 107 | * read from APERF MSR between last and current sample |
| 108 | * @mperf: Difference of maximum performance frequency clock count |
| 109 | * read from MPERF MSR between last and current sample |
| 110 | * @tsc: Difference of time stamp counter between last and |
| 111 | * current sample |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 112 | * @time: Current time from scheduler |
| 113 | * |
| 114 | * This structure is used in the cpudata structure to store performance sample |
| 115 | * data for choosing next P State. |
| 116 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 117 | struct sample { |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 118 | int32_t core_avg_perf; |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 119 | int32_t busy_scaled; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 120 | u64 aperf; |
| 121 | u64 mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 122 | u64 tsc; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 123 | u64 time; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 126 | /** |
| 127 | * struct pstate_data - Store P state data |
| 128 | * @current_pstate: Current requested P state |
| 129 | * @min_pstate: Min P state possible for this platform |
| 130 | * @max_pstate: Max P state possible for this platform |
| 131 | * @max_pstate_physical:This is physical Max P state for a processor |
| 132 | * This can be higher than the max_pstate which can |
| 133 | * be limited by platform thermal design power limits |
| 134 | * @scaling: Scaling factor to convert frequency to cpufreq |
| 135 | * frequency units |
| 136 | * @turbo_pstate: Max Turbo P state possible for this platform |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 137 | * @max_freq: @max_pstate frequency in cpufreq units |
| 138 | * @turbo_freq: @turbo_pstate frequency in cpufreq units |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 139 | * |
| 140 | * Stores the per cpu model P state limits and current P state. |
| 141 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 142 | struct pstate_data { |
| 143 | int current_pstate; |
| 144 | int min_pstate; |
| 145 | int max_pstate; |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 146 | int max_pstate_physical; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 147 | int scaling; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 148 | int turbo_pstate; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 149 | unsigned int max_freq; |
| 150 | unsigned int turbo_freq; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 153 | /** |
| 154 | * struct vid_data - Stores voltage information data |
| 155 | * @min: VID data for this platform corresponding to |
| 156 | * the lowest P state |
| 157 | * @max: VID data corresponding to the highest P State. |
| 158 | * @turbo: VID data for turbo P state |
| 159 | * @ratio: Ratio of (vid max - vid min) / |
| 160 | * (max P state - Min P State) |
| 161 | * |
| 162 | * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling) |
| 163 | * This data is used in Atom platforms, where in addition to target P state, |
| 164 | * the voltage data needs to be specified to select next P State. |
| 165 | */ |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 166 | struct vid_data { |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 167 | int min; |
| 168 | int max; |
| 169 | int turbo; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 170 | int32_t ratio; |
| 171 | }; |
| 172 | |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 173 | /** |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 174 | * struct global_params - Global parameters, mostly tunable via sysfs. |
| 175 | * @no_turbo: Whether or not to use turbo P-states. |
| 176 | * @turbo_disabled: Whethet or not turbo P-states are available at all, |
| 177 | * based on the MSR_IA32_MISC_ENABLE value and whether or |
| 178 | * not the maximum reported turbo P-state is different from |
| 179 | * the maximum reported non-turbo one. |
| 180 | * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo |
| 181 | * P-state capacity. |
| 182 | * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo |
| 183 | * P-state capacity. |
| 184 | */ |
| 185 | struct global_params { |
| 186 | bool no_turbo; |
| 187 | bool turbo_disabled; |
| 188 | int max_perf_pct; |
| 189 | int min_perf_pct; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /** |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 193 | * struct cpudata - Per CPU instance data storage |
| 194 | * @cpu: CPU number for this instance data |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 195 | * @policy: CPUFreq policy value |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 196 | * @update_util: CPUFreq utility callback information |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 197 | * @update_util_set: CPUFreq utility callback is set |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 198 | * @iowait_boost: iowait-related boost fraction |
| 199 | * @last_update: Time of the last update. |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 200 | * @pstate: Stores P state limits for this CPU |
| 201 | * @vid: Stores VID limits for this CPU |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 202 | * @last_sample_time: Last Sample time |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 203 | * @aperf_mperf_shift: Number of clock cycles after aperf, merf is incremented |
| 204 | * This shift is a multiplier to mperf delta to |
| 205 | * calculate CPU busy. |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 206 | * @prev_aperf: Last APERF value read from APERF MSR |
| 207 | * @prev_mperf: Last MPERF value read from MPERF MSR |
| 208 | * @prev_tsc: Last timestamp counter (TSC) value |
| 209 | * @prev_cummulative_iowait: IO Wait time difference from last and |
| 210 | * current sample |
| 211 | * @sample: Storage for storing last Sample data |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 212 | * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios |
| 213 | * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 214 | * @acpi_perf_data: Stores ACPI perf information read from _PSS |
| 215 | * @valid_pss_table: Set to true for valid ACPI _PSS entries found |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 216 | * @epp_powersave: Last saved HWP energy performance preference |
| 217 | * (EPP) or energy performance bias (EPB), |
| 218 | * when policy switched to performance |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 219 | * @epp_policy: Last saved policy used to set EPP/EPB |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 220 | * @epp_default: Power on default HWP energy performance |
| 221 | * preference/bias |
| 222 | * @epp_saved: Saved EPP/EPB during system suspend or CPU offline |
| 223 | * operation |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 224 | * @hwp_req_cached: Cached value of the last HWP Request MSR |
| 225 | * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR |
| 226 | * @hwp_boost_min: Last HWP boosted min performance |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 227 | * |
| 228 | * This structure stores per CPU instance data for all CPUs. |
| 229 | */ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 230 | struct cpudata { |
| 231 | int cpu; |
| 232 | |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 233 | unsigned int policy; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 234 | struct update_util_data update_util; |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 235 | bool update_util_set; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 236 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 237 | struct pstate_data pstate; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 238 | struct vid_data vid; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 239 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 240 | u64 last_update; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 241 | u64 last_sample_time; |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 242 | u64 aperf_mperf_shift; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 243 | u64 prev_aperf; |
| 244 | u64 prev_mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 245 | u64 prev_tsc; |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 246 | u64 prev_cummulative_iowait; |
Dirk Brandewie | d37e2b7 | 2014-02-12 10:01:04 -0800 | [diff] [blame] | 247 | struct sample sample; |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 248 | int32_t min_perf_ratio; |
| 249 | int32_t max_perf_ratio; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 250 | #ifdef CONFIG_ACPI |
| 251 | struct acpi_processor_performance acpi_perf_data; |
| 252 | bool valid_pss_table; |
| 253 | #endif |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 254 | unsigned int iowait_boost; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 255 | s16 epp_powersave; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 256 | s16 epp_policy; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 257 | s16 epp_default; |
| 258 | s16 epp_saved; |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 259 | u64 hwp_req_cached; |
| 260 | u64 hwp_cap_cached; |
| 261 | u32 hwp_boost_min; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | static struct cpudata **all_cpu_data; |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 265 | |
| 266 | /** |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 267 | * struct pstate_funcs - Per CPU model specific callbacks |
| 268 | * @get_max: Callback to get maximum non turbo effective P state |
| 269 | * @get_max_physical: Callback to get maximum non turbo physical P state |
| 270 | * @get_min: Callback to get minimum P state |
| 271 | * @get_turbo: Callback to get turbo P state |
| 272 | * @get_scaling: Callback to get frequency scaling factor |
| 273 | * @get_val: Callback to convert P state to actual MSR write value |
| 274 | * @get_vid: Callback to get VID data for Atom platforms |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 275 | * |
| 276 | * Core and Atom CPU models have different way to get P State limits. This |
| 277 | * structure is used to store those callbacks. |
| 278 | */ |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 279 | struct pstate_funcs { |
| 280 | int (*get_max)(void); |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 281 | int (*get_max_physical)(void); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 282 | int (*get_min)(void); |
| 283 | int (*get_turbo)(void); |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 284 | int (*get_scaling)(void); |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 285 | int (*get_aperf_mperf_shift)(void); |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 286 | u64 (*get_val)(struct cpudata*, int pstate); |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 287 | void (*get_vid)(struct cpudata *); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 288 | }; |
| 289 | |
Jisheng Zhang | 4a7cb7a | 2016-06-27 18:07:18 +0800 | [diff] [blame] | 290 | static struct pstate_funcs pstate_funcs __read_mostly; |
Rafael J. Wysocki | 5c43905 | 2017-03-28 00:05:44 +0200 | [diff] [blame] | 291 | |
Jisheng Zhang | 4a7cb7a | 2016-06-27 18:07:18 +0800 | [diff] [blame] | 292 | static int hwp_active __read_mostly; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 293 | static bool per_cpu_limits __read_mostly; |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 294 | static bool hwp_boost __read_mostly; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 295 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 296 | static struct cpufreq_driver *intel_pstate_driver __read_mostly; |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 297 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_ACPI |
| 299 | static bool acpi_ppc; |
| 300 | #endif |
Srinivas Pandruvada | 13ad770 | 2016-04-03 13:06:46 -0700 | [diff] [blame] | 301 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 302 | static struct global_params global; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 303 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 304 | static DEFINE_MUTEX(intel_pstate_driver_lock); |
Srinivas Pandruvada | a410c03d | 2016-10-28 10:44:52 -0700 | [diff] [blame] | 305 | static DEFINE_MUTEX(intel_pstate_limits_lock); |
| 306 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 307 | #ifdef CONFIG_ACPI |
Srinivas Pandruvada | 2b3ec76 | 2016-04-27 15:48:08 -0700 | [diff] [blame] | 308 | |
| 309 | static bool intel_pstate_get_ppc_enable_status(void) |
| 310 | { |
| 311 | if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER || |
| 312 | acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER) |
| 313 | return true; |
| 314 | |
| 315 | return acpi_ppc; |
| 316 | } |
| 317 | |
Rafael J. Wysocki | 1766900 | 2016-11-22 12:24:00 -0800 | [diff] [blame] | 318 | #ifdef CONFIG_ACPI_CPPC_LIB |
| 319 | |
| 320 | /* The work item is needed to avoid CPU hotplug locking issues */ |
| 321 | static void intel_pstste_sched_itmt_work_fn(struct work_struct *work) |
| 322 | { |
| 323 | sched_set_itmt_support(); |
| 324 | } |
| 325 | |
| 326 | static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn); |
| 327 | |
| 328 | static void intel_pstate_set_itmt_prio(int cpu) |
| 329 | { |
| 330 | struct cppc_perf_caps cppc_perf; |
| 331 | static u32 max_highest_perf = 0, min_highest_perf = U32_MAX; |
| 332 | int ret; |
| 333 | |
| 334 | ret = cppc_get_perf_caps(cpu, &cppc_perf); |
| 335 | if (ret) |
| 336 | return; |
| 337 | |
| 338 | /* |
| 339 | * The priorities can be set regardless of whether or not |
| 340 | * sched_set_itmt_support(true) has been called and it is valid to |
| 341 | * update them at any time after it has been called. |
| 342 | */ |
| 343 | sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu); |
| 344 | |
| 345 | if (max_highest_perf <= min_highest_perf) { |
| 346 | if (cppc_perf.highest_perf > max_highest_perf) |
| 347 | max_highest_perf = cppc_perf.highest_perf; |
| 348 | |
| 349 | if (cppc_perf.highest_perf < min_highest_perf) |
| 350 | min_highest_perf = cppc_perf.highest_perf; |
| 351 | |
| 352 | if (max_highest_perf > min_highest_perf) { |
| 353 | /* |
| 354 | * This code can be run during CPU online under the |
| 355 | * CPU hotplug locks, so sched_set_itmt_support() |
| 356 | * cannot be called from here. Queue up a work item |
| 357 | * to invoke it. |
| 358 | */ |
| 359 | schedule_work(&sched_itmt_work); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | #else |
| 364 | static void intel_pstate_set_itmt_prio(int cpu) |
| 365 | { |
| 366 | } |
| 367 | #endif |
| 368 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 369 | static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy) |
| 370 | { |
| 371 | struct cpudata *cpu; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 372 | int ret; |
| 373 | int i; |
| 374 | |
Rafael J. Wysocki | 1766900 | 2016-11-22 12:24:00 -0800 | [diff] [blame] | 375 | if (hwp_active) { |
| 376 | intel_pstate_set_itmt_prio(policy->cpu); |
Srinivas Pandruvada | e59a8f7 | 2016-05-04 15:07:34 -0700 | [diff] [blame] | 377 | return; |
Rafael J. Wysocki | 1766900 | 2016-11-22 12:24:00 -0800 | [diff] [blame] | 378 | } |
Srinivas Pandruvada | e59a8f7 | 2016-05-04 15:07:34 -0700 | [diff] [blame] | 379 | |
Srinivas Pandruvada | 2b3ec76 | 2016-04-27 15:48:08 -0700 | [diff] [blame] | 380 | if (!intel_pstate_get_ppc_enable_status()) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 381 | return; |
| 382 | |
| 383 | cpu = all_cpu_data[policy->cpu]; |
| 384 | |
| 385 | ret = acpi_processor_register_performance(&cpu->acpi_perf_data, |
| 386 | policy->cpu); |
| 387 | if (ret) |
| 388 | return; |
| 389 | |
| 390 | /* |
| 391 | * Check if the control value in _PSS is for PERF_CTL MSR, which should |
| 392 | * guarantee that the states returned by it map to the states in our |
| 393 | * list directly. |
| 394 | */ |
| 395 | if (cpu->acpi_perf_data.control_register.space_id != |
| 396 | ACPI_ADR_SPACE_FIXED_HARDWARE) |
| 397 | goto err; |
| 398 | |
| 399 | /* |
| 400 | * If there is only one entry _PSS, simply ignore _PSS and continue as |
| 401 | * usual without taking _PSS into account |
| 402 | */ |
| 403 | if (cpu->acpi_perf_data.state_count < 2) |
| 404 | goto err; |
| 405 | |
| 406 | pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu); |
| 407 | for (i = 0; i < cpu->acpi_perf_data.state_count; i++) { |
| 408 | pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n", |
| 409 | (i == cpu->acpi_perf_data.state ? '*' : ' '), i, |
| 410 | (u32) cpu->acpi_perf_data.states[i].core_frequency, |
| 411 | (u32) cpu->acpi_perf_data.states[i].power, |
| 412 | (u32) cpu->acpi_perf_data.states[i].control); |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * The _PSS table doesn't contain whole turbo frequency range. |
| 417 | * This just contains +1 MHZ above the max non turbo frequency, |
| 418 | * with control value corresponding to max turbo ratio. But |
| 419 | * when cpufreq set policy is called, it will call with this |
| 420 | * max frequency, which will cause a reduced performance as |
| 421 | * this driver uses real max turbo frequency as the max |
| 422 | * frequency. So correct this frequency in _PSS table to |
Srinivas Pandruvada | b00345d | 2016-06-14 23:12:59 -0700 | [diff] [blame] | 423 | * correct max turbo frequency based on the turbo state. |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 424 | * Also need to convert to MHz as _PSS freq is in MHz. |
| 425 | */ |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 426 | if (!global.turbo_disabled) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 427 | cpu->acpi_perf_data.states[0].core_frequency = |
| 428 | policy->cpuinfo.max_freq / 1000; |
| 429 | cpu->valid_pss_table = true; |
Srinivas Pandruvada | 6cacd11 | 2016-05-29 23:31:23 -0700 | [diff] [blame] | 430 | pr_debug("_PPC limits will be enforced\n"); |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 431 | |
| 432 | return; |
| 433 | |
| 434 | err: |
| 435 | cpu->valid_pss_table = false; |
| 436 | acpi_processor_unregister_performance(policy->cpu); |
| 437 | } |
| 438 | |
| 439 | static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy) |
| 440 | { |
| 441 | struct cpudata *cpu; |
| 442 | |
| 443 | cpu = all_cpu_data[policy->cpu]; |
| 444 | if (!cpu->valid_pss_table) |
| 445 | return; |
| 446 | |
| 447 | acpi_processor_unregister_performance(policy->cpu); |
| 448 | } |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 449 | #else |
Arnd Bergmann | 7a3ba76 | 2016-11-25 17:50:20 +0100 | [diff] [blame] | 450 | static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 451 | { |
| 452 | } |
| 453 | |
Arnd Bergmann | 7a3ba76 | 2016-11-25 17:50:20 +0100 | [diff] [blame] | 454 | static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 455 | { |
| 456 | } |
| 457 | #endif |
| 458 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 459 | static inline void update_turbo_state(void) |
| 460 | { |
| 461 | u64 misc_en; |
| 462 | struct cpudata *cpu; |
| 463 | |
| 464 | cpu = all_cpu_data[0]; |
| 465 | rdmsrl(MSR_IA32_MISC_ENABLE, misc_en); |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 466 | global.turbo_disabled = |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 467 | (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE || |
| 468 | cpu->pstate.max_pstate == cpu->pstate.turbo_pstate); |
| 469 | } |
| 470 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 471 | static int min_perf_pct_min(void) |
| 472 | { |
| 473 | struct cpudata *cpu = all_cpu_data[0]; |
Rafael J. Wysocki | 57caf4e | 2017-06-05 14:51:18 +0200 | [diff] [blame] | 474 | int turbo_pstate = cpu->pstate.turbo_pstate; |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 475 | |
Rafael J. Wysocki | 57caf4e | 2017-06-05 14:51:18 +0200 | [diff] [blame] | 476 | return turbo_pstate ? |
Srinivas Pandruvada | d4436c0 | 2017-07-10 16:23:52 -0700 | [diff] [blame] | 477 | (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0; |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 480 | static s16 intel_pstate_get_epb(struct cpudata *cpu_data) |
| 481 | { |
| 482 | u64 epb; |
| 483 | int ret; |
| 484 | |
| 485 | if (!static_cpu_has(X86_FEATURE_EPB)) |
| 486 | return -ENXIO; |
| 487 | |
| 488 | ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb); |
| 489 | if (ret) |
| 490 | return (s16)ret; |
| 491 | |
| 492 | return (s16)(epb & 0x0f); |
| 493 | } |
| 494 | |
| 495 | static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data) |
| 496 | { |
| 497 | s16 epp; |
| 498 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 499 | if (static_cpu_has(X86_FEATURE_HWP_EPP)) { |
| 500 | /* |
| 501 | * When hwp_req_data is 0, means that caller didn't read |
| 502 | * MSR_HWP_REQUEST, so need to read and get EPP. |
| 503 | */ |
| 504 | if (!hwp_req_data) { |
| 505 | epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, |
| 506 | &hwp_req_data); |
| 507 | if (epp) |
| 508 | return epp; |
| 509 | } |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 510 | epp = (hwp_req_data >> 24) & 0xff; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 511 | } else { |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 512 | /* When there is no EPP present, HWP uses EPB settings */ |
| 513 | epp = intel_pstate_get_epb(cpu_data); |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 514 | } |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 515 | |
| 516 | return epp; |
| 517 | } |
| 518 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 519 | static int intel_pstate_set_epb(int cpu, s16 pref) |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 520 | { |
| 521 | u64 epb; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 522 | int ret; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 523 | |
| 524 | if (!static_cpu_has(X86_FEATURE_EPB)) |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 525 | return -ENXIO; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 526 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 527 | ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb); |
| 528 | if (ret) |
| 529 | return ret; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 530 | |
| 531 | epb = (epb & ~0x0f) | pref; |
| 532 | wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb); |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 533 | |
| 534 | return 0; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 537 | /* |
| 538 | * EPP/EPB display strings corresponding to EPP index in the |
| 539 | * energy_perf_strings[] |
| 540 | * index String |
| 541 | *------------------------------------- |
| 542 | * 0 default |
| 543 | * 1 performance |
| 544 | * 2 balance_performance |
| 545 | * 3 balance_power |
| 546 | * 4 power |
| 547 | */ |
| 548 | static const char * const energy_perf_strings[] = { |
| 549 | "default", |
| 550 | "performance", |
| 551 | "balance_performance", |
| 552 | "balance_power", |
| 553 | "power", |
| 554 | NULL |
| 555 | }; |
Len Brown | 3cedbc5 | 2017-05-01 23:06:08 -0400 | [diff] [blame] | 556 | static const unsigned int epp_values[] = { |
| 557 | HWP_EPP_PERFORMANCE, |
| 558 | HWP_EPP_BALANCE_PERFORMANCE, |
| 559 | HWP_EPP_BALANCE_POWERSAVE, |
| 560 | HWP_EPP_POWERSAVE |
| 561 | }; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 562 | |
| 563 | static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data) |
| 564 | { |
| 565 | s16 epp; |
| 566 | int index = -EINVAL; |
| 567 | |
| 568 | epp = intel_pstate_get_epp(cpu_data, 0); |
| 569 | if (epp < 0) |
| 570 | return epp; |
| 571 | |
| 572 | if (static_cpu_has(X86_FEATURE_HWP_EPP)) { |
Len Brown | 3cedbc5 | 2017-05-01 23:06:08 -0400 | [diff] [blame] | 573 | if (epp == HWP_EPP_PERFORMANCE) |
| 574 | return 1; |
| 575 | if (epp <= HWP_EPP_BALANCE_PERFORMANCE) |
| 576 | return 2; |
| 577 | if (epp <= HWP_EPP_BALANCE_POWERSAVE) |
| 578 | return 3; |
| 579 | else |
| 580 | return 4; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 581 | } else if (static_cpu_has(X86_FEATURE_EPB)) { |
| 582 | /* |
| 583 | * Range: |
| 584 | * 0x00-0x03 : Performance |
| 585 | * 0x04-0x07 : Balance performance |
| 586 | * 0x08-0x0B : Balance power |
| 587 | * 0x0C-0x0F : Power |
| 588 | * The EPB is a 4 bit value, but our ranges restrict the |
| 589 | * value which can be set. Here only using top two bits |
| 590 | * effectively. |
| 591 | */ |
| 592 | index = (epp >> 2) + 1; |
| 593 | } |
| 594 | |
| 595 | return index; |
| 596 | } |
| 597 | |
| 598 | static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data, |
| 599 | int pref_index) |
| 600 | { |
| 601 | int epp = -EINVAL; |
| 602 | int ret; |
| 603 | |
| 604 | if (!pref_index) |
| 605 | epp = cpu_data->epp_default; |
| 606 | |
| 607 | mutex_lock(&intel_pstate_limits_lock); |
| 608 | |
| 609 | if (static_cpu_has(X86_FEATURE_HWP_EPP)) { |
| 610 | u64 value; |
| 611 | |
| 612 | ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value); |
| 613 | if (ret) |
| 614 | goto return_pref; |
| 615 | |
| 616 | value &= ~GENMASK_ULL(31, 24); |
| 617 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 618 | if (epp == -EINVAL) |
Len Brown | 3cedbc5 | 2017-05-01 23:06:08 -0400 | [diff] [blame] | 619 | epp = epp_values[pref_index - 1]; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 620 | |
| 621 | value |= (u64)epp << 24; |
| 622 | ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value); |
| 623 | } else { |
| 624 | if (epp == -EINVAL) |
| 625 | epp = (pref_index - 1) << 2; |
| 626 | ret = intel_pstate_set_epb(cpu_data->cpu, epp); |
| 627 | } |
| 628 | return_pref: |
| 629 | mutex_unlock(&intel_pstate_limits_lock); |
| 630 | |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | static ssize_t show_energy_performance_available_preferences( |
| 635 | struct cpufreq_policy *policy, char *buf) |
| 636 | { |
| 637 | int i = 0; |
| 638 | int ret = 0; |
| 639 | |
| 640 | while (energy_perf_strings[i] != NULL) |
| 641 | ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]); |
| 642 | |
| 643 | ret += sprintf(&buf[ret], "\n"); |
| 644 | |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | cpufreq_freq_attr_ro(energy_performance_available_preferences); |
| 649 | |
| 650 | static ssize_t store_energy_performance_preference( |
| 651 | struct cpufreq_policy *policy, const char *buf, size_t count) |
| 652 | { |
| 653 | struct cpudata *cpu_data = all_cpu_data[policy->cpu]; |
| 654 | char str_preference[21]; |
| 655 | int ret, i = 0; |
| 656 | |
| 657 | ret = sscanf(buf, "%20s", str_preference); |
| 658 | if (ret != 1) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | while (energy_perf_strings[i] != NULL) { |
| 662 | if (!strcmp(str_preference, energy_perf_strings[i])) { |
| 663 | intel_pstate_set_energy_pref_index(cpu_data, i); |
| 664 | return count; |
| 665 | } |
| 666 | ++i; |
| 667 | } |
| 668 | |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | |
| 672 | static ssize_t show_energy_performance_preference( |
| 673 | struct cpufreq_policy *policy, char *buf) |
| 674 | { |
| 675 | struct cpudata *cpu_data = all_cpu_data[policy->cpu]; |
| 676 | int preference; |
| 677 | |
| 678 | preference = intel_pstate_get_energy_pref_index(cpu_data); |
| 679 | if (preference < 0) |
| 680 | return preference; |
| 681 | |
| 682 | return sprintf(buf, "%s\n", energy_perf_strings[preference]); |
| 683 | } |
| 684 | |
| 685 | cpufreq_freq_attr_rw(energy_performance_preference); |
| 686 | |
| 687 | static struct freq_attr *hwp_cpufreq_attrs[] = { |
| 688 | &energy_performance_preference, |
| 689 | &energy_performance_available_preferences, |
| 690 | NULL, |
| 691 | }; |
| 692 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 693 | static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max, |
| 694 | int *current_max) |
| 695 | { |
| 696 | u64 cap; |
| 697 | |
| 698 | rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap); |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 699 | WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap); |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 700 | if (global.no_turbo) |
| 701 | *current_max = HWP_GUARANTEED_PERF(cap); |
| 702 | else |
| 703 | *current_max = HWP_HIGHEST_PERF(cap); |
| 704 | |
| 705 | *phy_max = HWP_HIGHEST_PERF(cap); |
| 706 | } |
| 707 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 708 | static void intel_pstate_hwp_set(unsigned int cpu) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 709 | { |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 710 | struct cpudata *cpu_data = all_cpu_data[cpu]; |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 711 | int max, min; |
| 712 | u64 value; |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 713 | s16 epp; |
Kristen Carlson Accardi | 74da56c | 2015-09-09 11:41:22 -0700 | [diff] [blame] | 714 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 715 | max = cpu_data->max_perf_ratio; |
| 716 | min = cpu_data->min_perf_ratio; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 717 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 718 | if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) |
| 719 | min = max; |
Srinivas Pandruvada | f9f4872 | 2016-10-08 12:42:38 -0700 | [diff] [blame] | 720 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 721 | rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 722 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 723 | value &= ~HWP_MIN_PERF(~0L); |
| 724 | value |= HWP_MIN_PERF(min); |
Srinivas Pandruvada | 3f8ed54 | 2017-03-13 18:30:12 -0700 | [diff] [blame] | 725 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 726 | value &= ~HWP_MAX_PERF(~0L); |
| 727 | value |= HWP_MAX_PERF(max); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 728 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 729 | if (cpu_data->epp_policy == cpu_data->policy) |
| 730 | goto skip_epp; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 731 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 732 | cpu_data->epp_policy = cpu_data->policy; |
| 733 | |
| 734 | if (cpu_data->epp_saved >= 0) { |
| 735 | epp = cpu_data->epp_saved; |
| 736 | cpu_data->epp_saved = -EINVAL; |
| 737 | goto update_epp; |
| 738 | } |
| 739 | |
| 740 | if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) { |
| 741 | epp = intel_pstate_get_epp(cpu_data, value); |
| 742 | cpu_data->epp_powersave = epp; |
| 743 | /* If EPP read was failed, then don't try to write */ |
| 744 | if (epp < 0) |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 745 | goto skip_epp; |
| 746 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 747 | epp = 0; |
| 748 | } else { |
| 749 | /* skip setting EPP, when saved value is invalid */ |
| 750 | if (cpu_data->epp_powersave < 0) |
| 751 | goto skip_epp; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 752 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 753 | /* |
| 754 | * No need to restore EPP when it is not zero. This |
| 755 | * means: |
| 756 | * - Policy is not changed |
| 757 | * - user has manually changed |
| 758 | * - Error reading EPB |
| 759 | */ |
| 760 | epp = intel_pstate_get_epp(cpu_data, value); |
| 761 | if (epp) |
| 762 | goto skip_epp; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 763 | |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 764 | epp = cpu_data->epp_powersave; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 765 | } |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 766 | update_epp: |
| 767 | if (static_cpu_has(X86_FEATURE_HWP_EPP)) { |
| 768 | value &= ~GENMASK_ULL(31, 24); |
| 769 | value |= (u64)epp << 24; |
| 770 | } else { |
| 771 | intel_pstate_set_epb(cpu, epp); |
| 772 | } |
| 773 | skip_epp: |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 774 | WRITE_ONCE(cpu_data->hwp_req_cached, value); |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 775 | wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value); |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 776 | } |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 777 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 778 | static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy) |
| 779 | { |
| 780 | struct cpudata *cpu_data = all_cpu_data[policy->cpu]; |
| 781 | |
| 782 | if (!hwp_active) |
| 783 | return 0; |
| 784 | |
| 785 | cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0); |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
Chen Yu | 70f6bf2 | 2018-01-29 10:27:57 +0800 | [diff] [blame] | 790 | static void intel_pstate_hwp_enable(struct cpudata *cpudata); |
| 791 | |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 792 | static int intel_pstate_resume(struct cpufreq_policy *policy) |
| 793 | { |
| 794 | if (!hwp_active) |
| 795 | return 0; |
| 796 | |
Rafael J. Wysocki | aa43924 | 2016-12-30 15:56:14 +0100 | [diff] [blame] | 797 | mutex_lock(&intel_pstate_limits_lock); |
| 798 | |
Chen Yu | 70f6bf2 | 2018-01-29 10:27:57 +0800 | [diff] [blame] | 799 | if (policy->cpu == 0) |
| 800 | intel_pstate_hwp_enable(all_cpu_data[policy->cpu]); |
| 801 | |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 802 | all_cpu_data[policy->cpu]->epp_policy = 0; |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 803 | intel_pstate_hwp_set(policy->cpu); |
Rafael J. Wysocki | aa43924 | 2016-12-30 15:56:14 +0100 | [diff] [blame] | 804 | |
| 805 | mutex_unlock(&intel_pstate_limits_lock); |
| 806 | |
Rafael J. Wysocki | 5f98ced | 2017-03-09 16:30:38 +0100 | [diff] [blame] | 807 | return 0; |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Rafael J. Wysocki | 111b8b3 | 2016-12-30 15:58:21 +0100 | [diff] [blame] | 810 | static void intel_pstate_update_policies(void) |
Viresh Kumar | 41cfd64 | 2016-02-22 10:27:46 +0530 | [diff] [blame] | 811 | { |
Rafael J. Wysocki | 111b8b3 | 2016-12-30 15:58:21 +0100 | [diff] [blame] | 812 | int cpu; |
| 813 | |
| 814 | for_each_possible_cpu(cpu) |
| 815 | cpufreq_update_policy(cpu); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 818 | /************************** sysfs begin ************************/ |
| 819 | #define show_one(file_name, object) \ |
| 820 | static ssize_t show_##file_name \ |
| 821 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 822 | { \ |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 823 | return sprintf(buf, "%u\n", global.object); \ |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 826 | static ssize_t intel_pstate_show_status(char *buf); |
| 827 | static int intel_pstate_update_status(const char *buf, size_t size); |
| 828 | |
| 829 | static ssize_t show_status(struct kobject *kobj, |
| 830 | struct attribute *attr, char *buf) |
| 831 | { |
| 832 | ssize_t ret; |
| 833 | |
| 834 | mutex_lock(&intel_pstate_driver_lock); |
| 835 | ret = intel_pstate_show_status(buf); |
| 836 | mutex_unlock(&intel_pstate_driver_lock); |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static ssize_t store_status(struct kobject *a, struct attribute *b, |
| 842 | const char *buf, size_t count) |
| 843 | { |
| 844 | char *p = memchr(buf, '\n', count); |
| 845 | int ret; |
| 846 | |
| 847 | mutex_lock(&intel_pstate_driver_lock); |
| 848 | ret = intel_pstate_update_status(buf, p ? p - buf : count); |
| 849 | mutex_unlock(&intel_pstate_driver_lock); |
| 850 | |
| 851 | return ret < 0 ? ret : count; |
| 852 | } |
| 853 | |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 854 | static ssize_t show_turbo_pct(struct kobject *kobj, |
| 855 | struct attribute *attr, char *buf) |
| 856 | { |
| 857 | struct cpudata *cpu; |
| 858 | int total, no_turbo, turbo_pct; |
| 859 | uint32_t turbo_fp; |
| 860 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 861 | mutex_lock(&intel_pstate_driver_lock); |
| 862 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 863 | if (!intel_pstate_driver) { |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 864 | mutex_unlock(&intel_pstate_driver_lock); |
| 865 | return -EAGAIN; |
| 866 | } |
| 867 | |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 868 | cpu = all_cpu_data[0]; |
| 869 | |
| 870 | total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; |
| 871 | no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1; |
Rafael J. Wysocki | 22590ef | 2016-04-09 01:25:58 +0200 | [diff] [blame] | 872 | turbo_fp = div_fp(no_turbo, total); |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 873 | turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100))); |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 874 | |
| 875 | mutex_unlock(&intel_pstate_driver_lock); |
| 876 | |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 877 | return sprintf(buf, "%u\n", turbo_pct); |
| 878 | } |
| 879 | |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 880 | static ssize_t show_num_pstates(struct kobject *kobj, |
| 881 | struct attribute *attr, char *buf) |
| 882 | { |
| 883 | struct cpudata *cpu; |
| 884 | int total; |
| 885 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 886 | mutex_lock(&intel_pstate_driver_lock); |
| 887 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 888 | if (!intel_pstate_driver) { |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 889 | mutex_unlock(&intel_pstate_driver_lock); |
| 890 | return -EAGAIN; |
| 891 | } |
| 892 | |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 893 | cpu = all_cpu_data[0]; |
| 894 | total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 895 | |
| 896 | mutex_unlock(&intel_pstate_driver_lock); |
| 897 | |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 898 | return sprintf(buf, "%u\n", total); |
| 899 | } |
| 900 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 901 | static ssize_t show_no_turbo(struct kobject *kobj, |
| 902 | struct attribute *attr, char *buf) |
| 903 | { |
| 904 | ssize_t ret; |
| 905 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 906 | mutex_lock(&intel_pstate_driver_lock); |
| 907 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 908 | if (!intel_pstate_driver) { |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 909 | mutex_unlock(&intel_pstate_driver_lock); |
| 910 | return -EAGAIN; |
| 911 | } |
| 912 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 913 | update_turbo_state(); |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 914 | if (global.turbo_disabled) |
| 915 | ret = sprintf(buf, "%u\n", global.turbo_disabled); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 916 | else |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 917 | ret = sprintf(buf, "%u\n", global.no_turbo); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 918 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 919 | mutex_unlock(&intel_pstate_driver_lock); |
| 920 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 921 | return ret; |
| 922 | } |
| 923 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 924 | static ssize_t store_no_turbo(struct kobject *a, struct attribute *b, |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 925 | const char *buf, size_t count) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 926 | { |
| 927 | unsigned int input; |
| 928 | int ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 929 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 930 | ret = sscanf(buf, "%u", &input); |
| 931 | if (ret != 1) |
| 932 | return -EINVAL; |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 933 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 934 | mutex_lock(&intel_pstate_driver_lock); |
| 935 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 936 | if (!intel_pstate_driver) { |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 937 | mutex_unlock(&intel_pstate_driver_lock); |
| 938 | return -EAGAIN; |
| 939 | } |
| 940 | |
Srinivas Pandruvada | a410c03d | 2016-10-28 10:44:52 -0700 | [diff] [blame] | 941 | mutex_lock(&intel_pstate_limits_lock); |
| 942 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 943 | update_turbo_state(); |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 944 | if (global.turbo_disabled) { |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 945 | pr_warn("Turbo disabled by BIOS or unavailable on processor\n"); |
Srinivas Pandruvada | a410c03d | 2016-10-28 10:44:52 -0700 | [diff] [blame] | 946 | mutex_unlock(&intel_pstate_limits_lock); |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 947 | mutex_unlock(&intel_pstate_driver_lock); |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 948 | return -EPERM; |
Dirk Brandewie | dd5fbf7 | 2014-06-20 07:27:59 -0700 | [diff] [blame] | 949 | } |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 950 | |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 951 | global.no_turbo = clamp_t(int, input, 0, 1); |
Rafael J. Wysocki | 111b8b3 | 2016-12-30 15:58:21 +0100 | [diff] [blame] | 952 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 953 | if (global.no_turbo) { |
| 954 | struct cpudata *cpu = all_cpu_data[0]; |
| 955 | int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate; |
| 956 | |
| 957 | /* Squash the global minimum into the permitted range. */ |
| 958 | if (global.min_perf_pct > pct) |
| 959 | global.min_perf_pct = pct; |
| 960 | } |
| 961 | |
Rafael J. Wysocki | cd59b4be | 2017-03-01 00:07:36 +0100 | [diff] [blame] | 962 | mutex_unlock(&intel_pstate_limits_lock); |
| 963 | |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 964 | intel_pstate_update_policies(); |
| 965 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 966 | mutex_unlock(&intel_pstate_driver_lock); |
| 967 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 968 | return count; |
| 969 | } |
| 970 | |
| 971 | 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] | 972 | const char *buf, size_t count) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 973 | { |
| 974 | unsigned int input; |
| 975 | int ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 976 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 977 | ret = sscanf(buf, "%u", &input); |
| 978 | if (ret != 1) |
| 979 | return -EINVAL; |
| 980 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 981 | mutex_lock(&intel_pstate_driver_lock); |
| 982 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 983 | if (!intel_pstate_driver) { |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 984 | mutex_unlock(&intel_pstate_driver_lock); |
| 985 | return -EAGAIN; |
| 986 | } |
| 987 | |
Srinivas Pandruvada | a410c03d | 2016-10-28 10:44:52 -0700 | [diff] [blame] | 988 | mutex_lock(&intel_pstate_limits_lock); |
| 989 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 990 | global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100); |
Rafael J. Wysocki | 111b8b3 | 2016-12-30 15:58:21 +0100 | [diff] [blame] | 991 | |
Rafael J. Wysocki | cd59b4be | 2017-03-01 00:07:36 +0100 | [diff] [blame] | 992 | mutex_unlock(&intel_pstate_limits_lock); |
| 993 | |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 994 | intel_pstate_update_policies(); |
| 995 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 996 | mutex_unlock(&intel_pstate_driver_lock); |
| 997 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 998 | return count; |
| 999 | } |
| 1000 | |
| 1001 | 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] | 1002 | const char *buf, size_t count) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1003 | { |
| 1004 | unsigned int input; |
| 1005 | int ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1006 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1007 | ret = sscanf(buf, "%u", &input); |
| 1008 | if (ret != 1) |
| 1009 | return -EINVAL; |
Kristen Carlson Accardi | a047599 | 2015-01-29 13:03:52 -0800 | [diff] [blame] | 1010 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 1011 | mutex_lock(&intel_pstate_driver_lock); |
| 1012 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 1013 | if (!intel_pstate_driver) { |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 1014 | mutex_unlock(&intel_pstate_driver_lock); |
| 1015 | return -EAGAIN; |
| 1016 | } |
| 1017 | |
Srinivas Pandruvada | a410c03d | 2016-10-28 10:44:52 -0700 | [diff] [blame] | 1018 | mutex_lock(&intel_pstate_limits_lock); |
| 1019 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1020 | global.min_perf_pct = clamp_t(int, input, |
| 1021 | min_perf_pct_min(), global.max_perf_pct); |
Rafael J. Wysocki | 111b8b3 | 2016-12-30 15:58:21 +0100 | [diff] [blame] | 1022 | |
Rafael J. Wysocki | cd59b4be | 2017-03-01 00:07:36 +0100 | [diff] [blame] | 1023 | mutex_unlock(&intel_pstate_limits_lock); |
| 1024 | |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 1025 | intel_pstate_update_policies(); |
| 1026 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 1027 | mutex_unlock(&intel_pstate_driver_lock); |
| 1028 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1029 | return count; |
| 1030 | } |
| 1031 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1032 | show_one(max_perf_pct, max_perf_pct); |
| 1033 | show_one(min_perf_pct, min_perf_pct); |
| 1034 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 1035 | define_one_global_rw(status); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1036 | define_one_global_rw(no_turbo); |
| 1037 | define_one_global_rw(max_perf_pct); |
| 1038 | define_one_global_rw(min_perf_pct); |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 1039 | define_one_global_ro(turbo_pct); |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 1040 | define_one_global_ro(num_pstates); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1041 | |
| 1042 | static struct attribute *intel_pstate_attributes[] = { |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 1043 | &status.attr, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1044 | &no_turbo.attr, |
Kristen Carlson Accardi | d01b1f4 | 2015-01-28 15:03:27 -0800 | [diff] [blame] | 1045 | &turbo_pct.attr, |
Kristen Carlson Accardi | 0522424 | 2015-01-28 15:03:28 -0800 | [diff] [blame] | 1046 | &num_pstates.attr, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1047 | NULL |
| 1048 | }; |
| 1049 | |
Arvind Yadav | 106c9c7 | 2017-07-03 13:40:33 +0530 | [diff] [blame] | 1050 | static const struct attribute_group intel_pstate_attr_group = { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1051 | .attrs = intel_pstate_attributes, |
| 1052 | }; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1053 | |
Stratos Karafotis | 317dd50 | 2014-07-18 08:37:17 -0700 | [diff] [blame] | 1054 | static void __init intel_pstate_sysfs_expose_params(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1055 | { |
Stratos Karafotis | 317dd50 | 2014-07-18 08:37:17 -0700 | [diff] [blame] | 1056 | struct kobject *intel_pstate_kobject; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1057 | int rc; |
| 1058 | |
| 1059 | intel_pstate_kobject = kobject_create_and_add("intel_pstate", |
| 1060 | &cpu_subsys.dev_root->kobj); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1061 | if (WARN_ON(!intel_pstate_kobject)) |
| 1062 | return; |
| 1063 | |
Stratos Karafotis | 2d8d1f1 | 2014-07-18 08:37:20 -0700 | [diff] [blame] | 1064 | rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1065 | if (WARN_ON(rc)) |
| 1066 | return; |
| 1067 | |
| 1068 | /* |
| 1069 | * If per cpu limits are enforced there are no global limits, so |
| 1070 | * return without creating max/min_perf_pct attributes |
| 1071 | */ |
| 1072 | if (per_cpu_limits) |
| 1073 | return; |
| 1074 | |
| 1075 | rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr); |
| 1076 | WARN_ON(rc); |
| 1077 | |
| 1078 | rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr); |
| 1079 | WARN_ON(rc); |
| 1080 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1081 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1082 | /************************** sysfs end ************************/ |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1083 | |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1084 | static void intel_pstate_hwp_enable(struct cpudata *cpudata) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1085 | { |
Srinivas Pandruvada | f05c966 | 2016-02-25 15:09:31 -0800 | [diff] [blame] | 1086 | /* First disable HWP notification interrupt as we don't process them */ |
Srinivas Pandruvada | da7de91 | 2016-07-19 16:52:01 -0700 | [diff] [blame] | 1087 | if (static_cpu_has(X86_FEATURE_HWP_NOTIFY)) |
| 1088 | wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00); |
Srinivas Pandruvada | f05c966 | 2016-02-25 15:09:31 -0800 | [diff] [blame] | 1089 | |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1090 | wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1); |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 1091 | cpudata->epp_policy = 0; |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 1092 | if (cpudata->epp_default == -EINVAL) |
| 1093 | cpudata->epp_default = intel_pstate_get_epp(cpudata, 0); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Srinivas Pandruvada | 6e978b2 | 2017-02-03 14:18:39 -0800 | [diff] [blame] | 1096 | #define MSR_IA32_POWER_CTL_BIT_EE 19 |
| 1097 | |
| 1098 | /* Disable energy efficiency optimization */ |
| 1099 | static void intel_pstate_disable_ee(int cpu) |
| 1100 | { |
| 1101 | u64 power_ctl; |
| 1102 | int ret; |
| 1103 | |
| 1104 | ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl); |
| 1105 | if (ret) |
| 1106 | return; |
| 1107 | |
| 1108 | if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) { |
| 1109 | pr_info("Disabling energy efficiency optimization\n"); |
| 1110 | power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE); |
| 1111 | wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 1115 | static int atom_get_min_pstate(void) |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1116 | { |
| 1117 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1118 | |
Len Brown | 92134bd | 2017-02-25 16:55:17 -0500 | [diff] [blame] | 1119 | rdmsrl(MSR_ATOM_CORE_RATIOS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 1120 | return (value >> 8) & 0x7F; |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1121 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1122 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 1123 | static int atom_get_max_pstate(void) |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1124 | { |
| 1125 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1126 | |
Len Brown | 92134bd | 2017-02-25 16:55:17 -0500 | [diff] [blame] | 1127 | rdmsrl(MSR_ATOM_CORE_RATIOS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 1128 | return (value >> 16) & 0x7F; |
Dirk Brandewie | 19e77c2 | 2013-10-21 09:20:35 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 1131 | static int atom_get_turbo_pstate(void) |
Dirk Brandewie | 61d8d2a | 2014-02-12 10:01:07 -0800 | [diff] [blame] | 1132 | { |
| 1133 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1134 | |
Len Brown | 92134bd | 2017-02-25 16:55:17 -0500 | [diff] [blame] | 1135 | rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 1136 | return value & 0x7F; |
Dirk Brandewie | 61d8d2a | 2014-02-12 10:01:07 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1139 | static u64 atom_get_val(struct cpudata *cpudata, int pstate) |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1140 | { |
| 1141 | u64 val; |
| 1142 | int32_t vid_fp; |
| 1143 | u32 vid; |
| 1144 | |
Chen Yu | 144c8e1 | 2015-07-29 23:53:10 +0800 | [diff] [blame] | 1145 | val = (u64)pstate << 8; |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 1146 | if (global.no_turbo && !global.turbo_disabled) |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1147 | val |= (u64)1 << 32; |
| 1148 | |
| 1149 | vid_fp = cpudata->vid.min + mul_fp( |
| 1150 | int_tofp(pstate - cpudata->pstate.min_pstate), |
| 1151 | cpudata->vid.ratio); |
| 1152 | |
| 1153 | 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] | 1154 | vid = ceiling_fp(vid_fp); |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1155 | |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 1156 | if (pstate > cpudata->pstate.max_pstate) |
| 1157 | vid = cpudata->vid.turbo; |
| 1158 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1159 | return val | vid; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1162 | static int silvermont_get_scaling(void) |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1163 | { |
| 1164 | u64 value; |
| 1165 | int i; |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1166 | /* Defined in Table 35-6 from SDM (Sept 2015) */ |
| 1167 | static int silvermont_freq_table[] = { |
| 1168 | 83300, 100000, 133300, 116700, 80000}; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1169 | |
| 1170 | rdmsrl(MSR_FSB_FREQ, value); |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1171 | i = value & 0x7; |
| 1172 | WARN_ON(i > 4); |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1173 | |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1174 | return silvermont_freq_table[i]; |
| 1175 | } |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1176 | |
Philippe Longepe | 1421df6 | 2015-11-09 17:40:47 -0800 | [diff] [blame] | 1177 | static int airmont_get_scaling(void) |
| 1178 | { |
| 1179 | u64 value; |
| 1180 | int i; |
| 1181 | /* Defined in Table 35-10 from SDM (Sept 2015) */ |
| 1182 | static int airmont_freq_table[] = { |
| 1183 | 83300, 100000, 133300, 116700, 80000, |
| 1184 | 93300, 90000, 88900, 87500}; |
| 1185 | |
| 1186 | rdmsrl(MSR_FSB_FREQ, value); |
| 1187 | i = value & 0xF; |
| 1188 | WARN_ON(i > 8); |
| 1189 | |
| 1190 | return airmont_freq_table[i]; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Philippe Longepe | 938d21a | 2015-11-09 17:40:46 -0800 | [diff] [blame] | 1193 | static void atom_get_vid(struct cpudata *cpudata) |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1194 | { |
| 1195 | u64 value; |
| 1196 | |
Len Brown | 92134bd | 2017-02-25 16:55:17 -0500 | [diff] [blame] | 1197 | rdmsrl(MSR_ATOM_CORE_VIDS, value); |
Dirk Brandewie | c16ed06 | 2014-06-20 07:27:58 -0700 | [diff] [blame] | 1198 | cpudata->vid.min = int_tofp((value >> 8) & 0x7f); |
| 1199 | cpudata->vid.max = int_tofp((value >> 16) & 0x7f); |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1200 | cpudata->vid.ratio = div_fp( |
| 1201 | cpudata->vid.max - cpudata->vid.min, |
| 1202 | int_tofp(cpudata->pstate.max_pstate - |
| 1203 | cpudata->pstate.min_pstate)); |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 1204 | |
Len Brown | 92134bd | 2017-02-25 16:55:17 -0500 | [diff] [blame] | 1205 | rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value); |
Dirk Brandewie | 21855ff | 2014-05-08 12:57:23 -0700 | [diff] [blame] | 1206 | cpudata->vid.turbo = value & 0x7f; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1209 | static int core_get_min_pstate(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1210 | { |
| 1211 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1212 | |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 1213 | rdmsrl(MSR_PLATFORM_INFO, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1214 | return (value >> 40) & 0xFF; |
| 1215 | } |
| 1216 | |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 1217 | static int core_get_max_pstate_physical(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1218 | { |
| 1219 | u64 value; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1220 | |
Konrad Rzeszutek Wilk | 05e99c8cf | 2013-03-20 14:21:10 +0000 | [diff] [blame] | 1221 | rdmsrl(MSR_PLATFORM_INFO, value); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1222 | return (value >> 8) & 0xFF; |
| 1223 | } |
| 1224 | |
Srinivas Pandruvada | 8fc7554 | 2017-01-19 15:03:14 -0800 | [diff] [blame] | 1225 | static int core_get_tdp_ratio(u64 plat_info) |
| 1226 | { |
| 1227 | /* Check how many TDP levels present */ |
| 1228 | if (plat_info & 0x600000000) { |
| 1229 | u64 tdp_ctrl; |
| 1230 | u64 tdp_ratio; |
| 1231 | int tdp_msr; |
| 1232 | int err; |
| 1233 | |
| 1234 | /* Get the TDP level (0, 1, 2) to get ratios */ |
| 1235 | err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl); |
| 1236 | if (err) |
| 1237 | return err; |
| 1238 | |
| 1239 | /* TDP MSR are continuous starting at 0x648 */ |
| 1240 | tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03); |
| 1241 | err = rdmsrl_safe(tdp_msr, &tdp_ratio); |
| 1242 | if (err) |
| 1243 | return err; |
| 1244 | |
| 1245 | /* For level 1 and 2, bits[23:16] contain the ratio */ |
| 1246 | if (tdp_ctrl & 0x03) |
| 1247 | tdp_ratio >>= 16; |
| 1248 | |
| 1249 | tdp_ratio &= 0xff; /* ratios are only 8 bits long */ |
| 1250 | pr_debug("tdp_ratio %x\n", (int)tdp_ratio); |
| 1251 | |
| 1252 | return (int)tdp_ratio; |
| 1253 | } |
| 1254 | |
| 1255 | return -ENXIO; |
| 1256 | } |
| 1257 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1258 | static int core_get_max_pstate(void) |
| 1259 | { |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1260 | u64 tar; |
| 1261 | u64 plat_info; |
| 1262 | int max_pstate; |
Srinivas Pandruvada | 8fc7554 | 2017-01-19 15:03:14 -0800 | [diff] [blame] | 1263 | int tdp_ratio; |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1264 | int err; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1265 | |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1266 | rdmsrl(MSR_PLATFORM_INFO, plat_info); |
| 1267 | max_pstate = (plat_info >> 8) & 0xFF; |
| 1268 | |
Srinivas Pandruvada | 8fc7554 | 2017-01-19 15:03:14 -0800 | [diff] [blame] | 1269 | tdp_ratio = core_get_tdp_ratio(plat_info); |
| 1270 | if (tdp_ratio <= 0) |
| 1271 | return max_pstate; |
| 1272 | |
| 1273 | if (hwp_active) { |
| 1274 | /* Turbo activation ratio is not used on HWP platforms */ |
| 1275 | return tdp_ratio; |
| 1276 | } |
| 1277 | |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1278 | err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar); |
| 1279 | if (!err) { |
Srinivas Pandruvada | 8fc7554 | 2017-01-19 15:03:14 -0800 | [diff] [blame] | 1280 | int tar_levels; |
| 1281 | |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1282 | /* Do some sanity checking for safety */ |
Srinivas Pandruvada | 8fc7554 | 2017-01-19 15:03:14 -0800 | [diff] [blame] | 1283 | tar_levels = tar & 0xff; |
| 1284 | if (tdp_ratio - 1 == tar_levels) { |
| 1285 | max_pstate = tar_levels; |
| 1286 | pr_debug("max_pstate=TAC %x\n", max_pstate); |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Srinivas Pandruvada | 6a35fc2 | 2015-10-14 16:11:59 -0700 | [diff] [blame] | 1290 | return max_pstate; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1293 | static int core_get_turbo_pstate(void) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1294 | { |
| 1295 | u64 value; |
| 1296 | int nont, ret; |
Stratos Karafotis | 845c1cb | 2014-07-18 08:37:19 -0700 | [diff] [blame] | 1297 | |
Srinivas Pandruvada | 100cf6f | 2016-07-06 16:07:55 -0700 | [diff] [blame] | 1298 | rdmsrl(MSR_TURBO_RATIO_LIMIT, value); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1299 | nont = core_get_max_pstate(); |
Stratos Karafotis | 285cb99 | 2014-07-18 08:37:21 -0700 | [diff] [blame] | 1300 | ret = (value) & 255; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1301 | if (ret <= nont) |
| 1302 | ret = nont; |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1306 | static inline int core_get_scaling(void) |
| 1307 | { |
| 1308 | return 100000; |
| 1309 | } |
| 1310 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1311 | static u64 core_get_val(struct cpudata *cpudata, int pstate) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1312 | { |
| 1313 | u64 val; |
| 1314 | |
Chen Yu | 144c8e1 | 2015-07-29 23:53:10 +0800 | [diff] [blame] | 1315 | val = (u64)pstate << 8; |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 1316 | if (global.no_turbo && !global.turbo_disabled) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1317 | val |= (u64)1 << 32; |
| 1318 | |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1319 | return val; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 1322 | static int knl_get_aperf_mperf_shift(void) |
| 1323 | { |
| 1324 | return 10; |
| 1325 | } |
| 1326 | |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1327 | static int knl_get_turbo_pstate(void) |
| 1328 | { |
| 1329 | u64 value; |
| 1330 | int nont, ret; |
| 1331 | |
Srinivas Pandruvada | 100cf6f | 2016-07-06 16:07:55 -0700 | [diff] [blame] | 1332 | rdmsrl(MSR_TURBO_RATIO_LIMIT, value); |
Dasaratharaman Chandramouli | b34ef93 | 2015-04-10 10:22:18 -0700 | [diff] [blame] | 1333 | nont = core_get_max_pstate(); |
| 1334 | ret = (((value) >> 8) & 0xFF); |
| 1335 | if (ret <= nont) |
| 1336 | ret = nont; |
| 1337 | return ret; |
| 1338 | } |
| 1339 | |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1340 | static int intel_pstate_get_base_pstate(struct cpudata *cpu) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1341 | { |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1342 | return global.no_turbo || global.turbo_disabled ? |
| 1343 | cpu->pstate.max_pstate : cpu->pstate.turbo_pstate; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1346 | static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate) |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1347 | { |
Rafael J. Wysocki | bc95a45 | 2016-07-19 15:10:37 +0200 | [diff] [blame] | 1348 | trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu); |
| 1349 | cpu->pstate.current_pstate = pstate; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1350 | /* |
| 1351 | * Generally, there is no guarantee that this code will always run on |
| 1352 | * the CPU being updated, so force the register update to run on the |
| 1353 | * right CPU. |
| 1354 | */ |
| 1355 | wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL, |
| 1356 | pstate_funcs.get_val(cpu, pstate)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1359 | static void intel_pstate_set_min_pstate(struct cpudata *cpu) |
| 1360 | { |
| 1361 | intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate); |
| 1362 | } |
| 1363 | |
| 1364 | static void intel_pstate_max_within_limits(struct cpudata *cpu) |
| 1365 | { |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1366 | int pstate; |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1367 | |
| 1368 | update_turbo_state(); |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1369 | pstate = intel_pstate_get_base_pstate(cpu); |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1370 | pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio); |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1371 | intel_pstate_set_pstate(cpu, pstate); |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1374 | static void intel_pstate_get_cpu_pstates(struct cpudata *cpu) |
| 1375 | { |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1376 | cpu->pstate.min_pstate = pstate_funcs.get_min(); |
| 1377 | cpu->pstate.max_pstate = pstate_funcs.get_max(); |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 1378 | cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical(); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1379 | cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(); |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1380 | cpu->pstate.scaling = pstate_funcs.get_scaling(); |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1381 | cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling; |
| 1382 | cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1383 | |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 1384 | if (pstate_funcs.get_aperf_mperf_shift) |
| 1385 | cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift(); |
| 1386 | |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 1387 | if (pstate_funcs.get_vid) |
| 1388 | pstate_funcs.get_vid(cpu); |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1389 | |
| 1390 | intel_pstate_set_min_pstate(cpu); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 1393 | /* |
| 1394 | * Long hold time will keep high perf limits for long time, |
| 1395 | * which negatively impacts perf/watt for some workloads, |
| 1396 | * like specpower. 3ms is based on experiements on some |
| 1397 | * workoads. |
| 1398 | */ |
| 1399 | static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC; |
| 1400 | |
| 1401 | static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu) |
| 1402 | { |
| 1403 | u64 hwp_req = READ_ONCE(cpu->hwp_req_cached); |
| 1404 | u32 max_limit = (hwp_req & 0xff00) >> 8; |
| 1405 | u32 min_limit = (hwp_req & 0xff); |
| 1406 | u32 boost_level1; |
| 1407 | |
| 1408 | /* |
| 1409 | * Cases to consider (User changes via sysfs or boot time): |
| 1410 | * If, P0 (Turbo max) = P1 (Guaranteed max) = min: |
| 1411 | * No boost, return. |
| 1412 | * If, P0 (Turbo max) > P1 (Guaranteed max) = min: |
| 1413 | * Should result in one level boost only for P0. |
| 1414 | * If, P0 (Turbo max) = P1 (Guaranteed max) > min: |
| 1415 | * Should result in two level boost: |
| 1416 | * (min + p1)/2 and P1. |
| 1417 | * If, P0 (Turbo max) > P1 (Guaranteed max) > min: |
| 1418 | * Should result in three level boost: |
| 1419 | * (min + p1)/2, P1 and P0. |
| 1420 | */ |
| 1421 | |
| 1422 | /* If max and min are equal or already at max, nothing to boost */ |
| 1423 | if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit) |
| 1424 | return; |
| 1425 | |
| 1426 | if (!cpu->hwp_boost_min) |
| 1427 | cpu->hwp_boost_min = min_limit; |
| 1428 | |
| 1429 | /* level at half way mark between min and guranteed */ |
| 1430 | boost_level1 = (HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) + min_limit) >> 1; |
| 1431 | |
| 1432 | if (cpu->hwp_boost_min < boost_level1) |
| 1433 | cpu->hwp_boost_min = boost_level1; |
| 1434 | else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(cpu->hwp_cap_cached)) |
| 1435 | cpu->hwp_boost_min = HWP_GUARANTEED_PERF(cpu->hwp_cap_cached); |
| 1436 | else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) && |
| 1437 | max_limit != HWP_GUARANTEED_PERF(cpu->hwp_cap_cached)) |
| 1438 | cpu->hwp_boost_min = max_limit; |
| 1439 | else |
| 1440 | return; |
| 1441 | |
| 1442 | hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min; |
| 1443 | wrmsrl(MSR_HWP_REQUEST, hwp_req); |
| 1444 | cpu->last_update = cpu->sample.time; |
| 1445 | } |
| 1446 | |
| 1447 | static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu) |
| 1448 | { |
| 1449 | if (cpu->hwp_boost_min) { |
| 1450 | bool expired; |
| 1451 | |
| 1452 | /* Check if we are idle for hold time to boost down */ |
| 1453 | expired = time_after64(cpu->sample.time, cpu->last_update + |
| 1454 | hwp_boost_hold_time_ns); |
| 1455 | if (expired) { |
| 1456 | wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached); |
| 1457 | cpu->hwp_boost_min = 0; |
| 1458 | } |
| 1459 | } |
| 1460 | cpu->last_update = cpu->sample.time; |
| 1461 | } |
| 1462 | |
| 1463 | static inline void intel_pstate_update_util_hwp(struct update_util_data *data, |
| 1464 | u64 time, unsigned int flags) |
| 1465 | { |
| 1466 | } |
| 1467 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1468 | static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1469 | { |
Stratos Karafotis | 6b17ddb | 2014-04-29 20:53:49 +0300 | [diff] [blame] | 1470 | struct sample *sample = &cpu->sample; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1471 | |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1472 | sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1475 | static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1476 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1477 | u64 aperf, mperf; |
Stratos Karafotis | 4ab60c3 | 2014-07-18 08:37:24 -0700 | [diff] [blame] | 1478 | unsigned long flags; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1479 | u64 tsc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1480 | |
Stratos Karafotis | 4ab60c3 | 2014-07-18 08:37:24 -0700 | [diff] [blame] | 1481 | local_irq_save(flags); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1482 | rdmsrl(MSR_IA32_APERF, aperf); |
| 1483 | rdmsrl(MSR_IA32_MPERF, mperf); |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1484 | tsc = rdtsc(); |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1485 | if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) { |
Srinivas Pandruvada | 8e601a9 | 2015-10-15 12:34:21 -0700 | [diff] [blame] | 1486 | local_irq_restore(flags); |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1487 | return false; |
Srinivas Pandruvada | 8e601a9 | 2015-10-15 12:34:21 -0700 | [diff] [blame] | 1488 | } |
Stratos Karafotis | 4ab60c3 | 2014-07-18 08:37:24 -0700 | [diff] [blame] | 1489 | local_irq_restore(flags); |
Dirk Brandewie | b69880f | 2014-01-16 10:32:25 -0800 | [diff] [blame] | 1490 | |
Dirk Brandewie | c4ee841 | 2014-05-29 09:32:24 -0700 | [diff] [blame] | 1491 | cpu->last_sample_time = cpu->sample.time; |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1492 | cpu->sample.time = time; |
Dirk Brandewie | d37e2b7 | 2014-02-12 10:01:04 -0800 | [diff] [blame] | 1493 | cpu->sample.aperf = aperf; |
| 1494 | cpu->sample.mperf = mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1495 | cpu->sample.tsc = tsc; |
Dirk Brandewie | d37e2b7 | 2014-02-12 10:01:04 -0800 | [diff] [blame] | 1496 | cpu->sample.aperf -= cpu->prev_aperf; |
| 1497 | cpu->sample.mperf -= cpu->prev_mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1498 | cpu->sample.tsc -= cpu->prev_tsc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1499 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1500 | cpu->prev_aperf = aperf; |
| 1501 | cpu->prev_mperf = mperf; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1502 | cpu->prev_tsc = tsc; |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1503 | /* |
| 1504 | * First time this function is invoked in a given cycle, all of the |
| 1505 | * previous sample data fields are equal to zero or stale and they must |
| 1506 | * be populated with meaningful numbers for things to work, so assume |
| 1507 | * that sample.time will always be reset before setting the utilization |
| 1508 | * update hook and make the caller skip the sample then. |
| 1509 | */ |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 1510 | if (cpu->last_sample_time) { |
| 1511 | intel_pstate_calc_avg_perf(cpu); |
| 1512 | return true; |
| 1513 | } |
| 1514 | return false; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Philippe Longepe | 8fa520a | 2016-03-06 08:34:06 +0100 | [diff] [blame] | 1517 | static inline int32_t get_avg_frequency(struct cpudata *cpu) |
| 1518 | { |
Doug Smythies | c587c79 | 2017-08-08 14:05:12 -0700 | [diff] [blame] | 1519 | return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz); |
Philippe Longepe | 8fa520a | 2016-03-06 08:34:06 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
Philippe Longepe | bdcaa23 | 2016-04-22 11:46:09 -0700 | [diff] [blame] | 1522 | static inline int32_t get_avg_pstate(struct cpudata *cpu) |
| 1523 | { |
Rafael J. Wysocki | 8edb0a6 | 2016-05-11 19:10:42 +0200 | [diff] [blame] | 1524 | return mul_ext_fp(cpu->pstate.max_pstate_physical, |
| 1525 | cpu->sample.core_avg_perf); |
Philippe Longepe | bdcaa23 | 2016-04-22 11:46:09 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Rafael J. Wysocki | d77d488 | 2017-08-10 01:09:16 +0200 | [diff] [blame] | 1528 | static inline int32_t get_target_pstate(struct cpudata *cpu) |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1529 | { |
| 1530 | struct sample *sample = &cpu->sample; |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1531 | int32_t busy_frac, boost; |
Rafael J. Wysocki | 0843e83 | 2016-10-06 14:07:51 +0200 | [diff] [blame] | 1532 | int target, avg_pstate; |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1533 | |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 1534 | busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift, |
| 1535 | sample->tsc); |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 1536 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1537 | boost = cpu->iowait_boost; |
| 1538 | cpu->iowait_boost >>= 1; |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 1539 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1540 | if (busy_frac < boost) |
| 1541 | busy_frac = boost; |
Philippe Longepe | 63d1d65 | 2015-12-04 17:40:35 +0100 | [diff] [blame] | 1542 | |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1543 | sample->busy_scaled = busy_frac * 100; |
Rafael J. Wysocki | 0843e83 | 2016-10-06 14:07:51 +0200 | [diff] [blame] | 1544 | |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 1545 | target = global.no_turbo || global.turbo_disabled ? |
Rafael J. Wysocki | 0843e83 | 2016-10-06 14:07:51 +0200 | [diff] [blame] | 1546 | cpu->pstate.max_pstate : cpu->pstate.turbo_pstate; |
| 1547 | target += target >> 2; |
| 1548 | target = mul_fp(target, busy_frac); |
| 1549 | if (target < cpu->pstate.min_pstate) |
| 1550 | target = cpu->pstate.min_pstate; |
| 1551 | |
| 1552 | /* |
| 1553 | * If the average P-state during the previous cycle was higher than the |
| 1554 | * current target, add 50% of the difference to the target to reduce |
| 1555 | * possible performance oscillations and offset possible performance |
| 1556 | * loss related to moving the workload from one CPU to another within |
| 1557 | * a package/module. |
| 1558 | */ |
| 1559 | avg_pstate = get_avg_pstate(cpu); |
| 1560 | if (avg_pstate > target) |
| 1561 | target += (avg_pstate - target) >> 1; |
| 1562 | |
| 1563 | return target; |
Philippe Longepe | e70eed2 | 2015-12-04 17:40:32 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1566 | static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate) |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1567 | { |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1568 | int max_pstate = intel_pstate_get_base_pstate(cpu); |
| 1569 | int min_pstate; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1570 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1571 | min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio); |
| 1572 | max_pstate = max(min_pstate, cpu->max_perf_ratio); |
Rafael J. Wysocki | b02aabe | 2017-03-28 00:24:26 +0200 | [diff] [blame] | 1573 | return clamp_t(int, pstate, min_pstate, max_pstate); |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate) |
| 1577 | { |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1578 | if (pstate == cpu->pstate.current_pstate) |
| 1579 | return; |
| 1580 | |
Rafael J. Wysocki | bc95a45 | 2016-07-19 15:10:37 +0200 | [diff] [blame] | 1581 | cpu->pstate.current_pstate = pstate; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1582 | wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate)); |
| 1583 | } |
| 1584 | |
Rafael J. Wysocki | a891283 | 2017-08-10 01:08:56 +0200 | [diff] [blame] | 1585 | static void intel_pstate_adjust_pstate(struct cpudata *cpu) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1586 | { |
Rafael J. Wysocki | 67dd9bf | 2017-03-28 00:17:10 +0200 | [diff] [blame] | 1587 | int from = cpu->pstate.current_pstate; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1588 | struct sample *sample; |
Rafael J. Wysocki | a891283 | 2017-08-10 01:08:56 +0200 | [diff] [blame] | 1589 | int target_pstate; |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1590 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1591 | update_turbo_state(); |
| 1592 | |
Rafael J. Wysocki | d77d488 | 2017-08-10 01:09:16 +0200 | [diff] [blame] | 1593 | target_pstate = get_target_pstate(cpu); |
Rafael J. Wysocki | 6407829 | 2017-03-03 23:51:31 +0100 | [diff] [blame] | 1594 | target_pstate = intel_pstate_prepare_request(cpu, target_pstate); |
| 1595 | trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu); |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 1596 | intel_pstate_update_pstate(cpu, target_pstate); |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1597 | |
| 1598 | sample = &cpu->sample; |
Rafael J. Wysocki | a1c9787 | 2016-05-11 19:09:12 +0200 | [diff] [blame] | 1599 | trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf), |
Philippe Longepe | 157386b | 2015-12-04 17:40:30 +0100 | [diff] [blame] | 1600 | fp_toint(sample->busy_scaled), |
Doug Smythies | 4055fad | 2015-04-11 21:10:26 -0700 | [diff] [blame] | 1601 | from, |
| 1602 | cpu->pstate.current_pstate, |
| 1603 | sample->mperf, |
| 1604 | sample->aperf, |
| 1605 | sample->tsc, |
Srinivas Pandruvada | 3ba7bca | 2016-09-13 17:41:33 -0700 | [diff] [blame] | 1606 | get_avg_frequency(cpu), |
| 1607 | fp_toint(cpu->iowait_boost * 100)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1610 | 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] | 1611 | unsigned int flags) |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1612 | { |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1613 | struct cpudata *cpu = container_of(data, struct cpudata, update_util); |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1614 | u64 delta_ns; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1615 | |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 1616 | /* Don't allow remote callbacks */ |
| 1617 | if (smp_processor_id() != cpu->cpu) |
| 1618 | return; |
| 1619 | |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 1620 | if (flags & SCHED_CPUFREQ_IOWAIT) { |
| 1621 | cpu->iowait_boost = int_tofp(1); |
Srinivas Pandruvada | 7bde2d5 | 2017-08-03 19:03:14 -0700 | [diff] [blame] | 1622 | cpu->last_update = time; |
| 1623 | /* |
| 1624 | * The last time the busy was 100% so P-state was max anyway |
| 1625 | * so avoid overhead of computation. |
| 1626 | */ |
| 1627 | if (fp_toint(cpu->sample.busy_scaled) == 100) |
| 1628 | return; |
| 1629 | |
| 1630 | goto set_pstate; |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 1631 | } else if (cpu->iowait_boost) { |
| 1632 | /* Clear iowait_boost if the CPU may have been idle. */ |
| 1633 | delta_ns = time - cpu->last_update; |
| 1634 | if (delta_ns > TICK_NSEC) |
| 1635 | cpu->iowait_boost = 0; |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1636 | } |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 1637 | cpu->last_update = time; |
Rafael J. Wysocki | 09c448d | 2016-09-14 02:28:13 +0200 | [diff] [blame] | 1638 | delta_ns = time - cpu->sample.time; |
Rafael J. Wysocki | d77d488 | 2017-08-10 01:09:16 +0200 | [diff] [blame] | 1639 | if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL) |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 1640 | return; |
Rafael J. Wysocki | 4fec7ad | 2016-03-10 23:45:19 +0100 | [diff] [blame] | 1641 | |
Srinivas Pandruvada | 7bde2d5 | 2017-08-03 19:03:14 -0700 | [diff] [blame] | 1642 | set_pstate: |
Rafael J. Wysocki | a891283 | 2017-08-10 01:08:56 +0200 | [diff] [blame] | 1643 | if (intel_pstate_sample(cpu, time)) |
| 1644 | intel_pstate_adjust_pstate(cpu); |
Rafael J. Wysocki | 67dd9bf | 2017-03-28 00:17:10 +0200 | [diff] [blame] | 1645 | } |
Rafael J. Wysocki | eabd22c | 2017-03-28 00:15:37 +0200 | [diff] [blame] | 1646 | |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1647 | static struct pstate_funcs core_funcs = { |
| 1648 | .get_max = core_get_max_pstate, |
| 1649 | .get_max_physical = core_get_max_pstate_physical, |
| 1650 | .get_min = core_get_min_pstate, |
| 1651 | .get_turbo = core_get_turbo_pstate, |
| 1652 | .get_scaling = core_get_scaling, |
| 1653 | .get_val = core_get_val, |
Rafael J. Wysocki | de4a76c | 2017-03-28 00:18:02 +0200 | [diff] [blame] | 1654 | }; |
| 1655 | |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1656 | static const struct pstate_funcs silvermont_funcs = { |
| 1657 | .get_max = atom_get_max_pstate, |
| 1658 | .get_max_physical = atom_get_max_pstate, |
| 1659 | .get_min = atom_get_min_pstate, |
| 1660 | .get_turbo = atom_get_turbo_pstate, |
| 1661 | .get_val = atom_get_val, |
| 1662 | .get_scaling = silvermont_get_scaling, |
| 1663 | .get_vid = atom_get_vid, |
Rafael J. Wysocki | de4a76c | 2017-03-28 00:18:02 +0200 | [diff] [blame] | 1664 | }; |
| 1665 | |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1666 | static const struct pstate_funcs airmont_funcs = { |
| 1667 | .get_max = atom_get_max_pstate, |
| 1668 | .get_max_physical = atom_get_max_pstate, |
| 1669 | .get_min = atom_get_min_pstate, |
| 1670 | .get_turbo = atom_get_turbo_pstate, |
| 1671 | .get_val = atom_get_val, |
| 1672 | .get_scaling = airmont_get_scaling, |
| 1673 | .get_vid = atom_get_vid, |
Rafael J. Wysocki | de4a76c | 2017-03-28 00:18:02 +0200 | [diff] [blame] | 1674 | }; |
| 1675 | |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1676 | static const struct pstate_funcs knl_funcs = { |
| 1677 | .get_max = core_get_max_pstate, |
| 1678 | .get_max_physical = core_get_max_pstate_physical, |
| 1679 | .get_min = core_get_min_pstate, |
| 1680 | .get_turbo = knl_get_turbo_pstate, |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 1681 | .get_aperf_mperf_shift = knl_get_aperf_mperf_shift, |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1682 | .get_scaling = core_get_scaling, |
| 1683 | .get_val = core_get_val, |
Rafael J. Wysocki | de4a76c | 2017-03-28 00:18:02 +0200 | [diff] [blame] | 1684 | }; |
| 1685 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1686 | #define ICPU(model, policy) \ |
Dirk Brandewie | 6cbd7ee | 2014-01-06 10:59:16 -0800 | [diff] [blame] | 1687 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\ |
| 1688 | (unsigned long)&policy } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1689 | |
| 1690 | static const struct x86_cpu_id intel_pstate_cpu_ids[] = { |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1691 | ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs), |
| 1692 | ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs), |
| 1693 | ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_funcs), |
| 1694 | ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs), |
| 1695 | ICPU(INTEL_FAM6_HASWELL_CORE, core_funcs), |
| 1696 | ICPU(INTEL_FAM6_BROADWELL_CORE, core_funcs), |
| 1697 | ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs), |
| 1698 | ICPU(INTEL_FAM6_HASWELL_X, core_funcs), |
| 1699 | ICPU(INTEL_FAM6_HASWELL_ULT, core_funcs), |
| 1700 | ICPU(INTEL_FAM6_HASWELL_GT3E, core_funcs), |
| 1701 | ICPU(INTEL_FAM6_BROADWELL_GT3E, core_funcs), |
| 1702 | ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs), |
| 1703 | ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_funcs), |
| 1704 | ICPU(INTEL_FAM6_BROADWELL_X, core_funcs), |
| 1705 | ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs), |
| 1706 | ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs), |
| 1707 | ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs), |
| 1708 | ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs), |
Srinivas Pandruvada | dbd49b8 | 2018-01-10 11:38:51 -0800 | [diff] [blame] | 1709 | ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs), |
| 1710 | ICPU(INTEL_FAM6_ATOM_GEMINI_LAKE, core_funcs), |
Srinivas Pandruvada | d8de7a4 | 2018-01-10 11:38:52 -0800 | [diff] [blame] | 1711 | ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs), |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1712 | {} |
| 1713 | }; |
| 1714 | MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids); |
| 1715 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 1716 | static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = { |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1717 | ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs), |
| 1718 | ICPU(INTEL_FAM6_BROADWELL_X, core_funcs), |
| 1719 | ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs), |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1720 | {} |
| 1721 | }; |
| 1722 | |
Srinivas Pandruvada | 6e978b2 | 2017-02-03 14:18:39 -0800 | [diff] [blame] | 1723 | static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = { |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 1724 | ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs), |
Srinivas Pandruvada | 6e978b2 | 2017-02-03 14:18:39 -0800 | [diff] [blame] | 1725 | {} |
| 1726 | }; |
| 1727 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1728 | static int intel_pstate_init_cpu(unsigned int cpunum) |
| 1729 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1730 | struct cpudata *cpu; |
| 1731 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1732 | cpu = all_cpu_data[cpunum]; |
| 1733 | |
| 1734 | if (!cpu) { |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1735 | cpu = kzalloc(sizeof(*cpu), GFP_KERNEL); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1736 | if (!cpu) |
| 1737 | return -ENOMEM; |
| 1738 | |
| 1739 | all_cpu_data[cpunum] = cpu; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1740 | |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 1741 | cpu->epp_default = -EINVAL; |
| 1742 | cpu->epp_powersave = -EINVAL; |
| 1743 | cpu->epp_saved = -EINVAL; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1744 | } |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1745 | |
| 1746 | cpu = all_cpu_data[cpunum]; |
| 1747 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1748 | cpu->cpu = cpunum; |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1749 | |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1750 | if (hwp_active) { |
Srinivas Pandruvada | 6e978b2 | 2017-02-03 14:18:39 -0800 | [diff] [blame] | 1751 | const struct x86_cpu_id *id; |
| 1752 | |
| 1753 | id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids); |
| 1754 | if (id) |
| 1755 | intel_pstate_disable_ee(cpunum); |
| 1756 | |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1757 | intel_pstate_hwp_enable(cpu); |
Rafael J. Wysocki | a4675fb | 2016-02-05 01:45:30 +0100 | [diff] [blame] | 1758 | } |
Kristen Carlson Accardi | ba88d43 | 2015-07-14 09:46:23 -0700 | [diff] [blame] | 1759 | |
Vincent Minet | 179e847 | 2014-07-05 01:51:33 +0200 | [diff] [blame] | 1760 | intel_pstate_get_cpu_pstates(cpu); |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 1761 | |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 1762 | pr_debug("controlling: cpu %d\n", cpunum); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1763 | |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1767 | 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] | 1768 | { |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1769 | struct cpudata *cpu = all_cpu_data[cpu_num]; |
| 1770 | |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 1771 | if (hwp_active && !hwp_boost) |
Len Brown | 62611cb | 2017-06-23 22:11:53 -0700 | [diff] [blame] | 1772 | return; |
| 1773 | |
Rafael J. Wysocki | 5ab666e | 2016-06-27 23:47:15 +0200 | [diff] [blame] | 1774 | if (cpu->update_util_set) |
| 1775 | return; |
| 1776 | |
Rafael J. Wysocki | febce40 | 2016-04-02 01:06:21 +0200 | [diff] [blame] | 1777 | /* Prevent intel_pstate_update_util() from using stale data. */ |
| 1778 | cpu->sample.time = 0; |
Rafael J. Wysocki | 67dd9bf | 2017-03-28 00:17:10 +0200 | [diff] [blame] | 1779 | cpufreq_add_update_util_hook(cpu_num, &cpu->update_util, |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 1780 | (hwp_active ? |
| 1781 | intel_pstate_update_util_hwp : |
| 1782 | intel_pstate_update_util)); |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 1783 | cpu->update_util_set = true; |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | static void intel_pstate_clear_update_util_hook(unsigned int cpu) |
| 1787 | { |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 1788 | struct cpudata *cpu_data = all_cpu_data[cpu]; |
| 1789 | |
| 1790 | if (!cpu_data->update_util_set) |
| 1791 | return; |
| 1792 | |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 1793 | cpufreq_remove_update_util_hook(cpu); |
Chen Yu | 4578ee7e | 2016-05-11 14:33:08 +0800 | [diff] [blame] | 1794 | cpu_data->update_util_set = false; |
Rafael J. Wysocki | bb6ab52 | 2016-03-31 17:42:15 +0200 | [diff] [blame] | 1795 | synchronize_sched(); |
| 1796 | } |
| 1797 | |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 1798 | static int intel_pstate_get_max_freq(struct cpudata *cpu) |
| 1799 | { |
| 1800 | return global.turbo_disabled || global.no_turbo ? |
| 1801 | cpu->pstate.max_freq : cpu->pstate.turbo_freq; |
| 1802 | } |
| 1803 | |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1804 | static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy, |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1805 | struct cpudata *cpu) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1806 | { |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 1807 | int max_freq = intel_pstate_get_max_freq(cpu); |
Rafael J. Wysocki | e4c204c | 2017-03-14 16:18:34 +0100 | [diff] [blame] | 1808 | int32_t max_policy_perf, min_policy_perf; |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1809 | int max_state, turbo_max; |
Srinivas Pandruvada | a410c03d | 2016-10-28 10:44:52 -0700 | [diff] [blame] | 1810 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1811 | /* |
| 1812 | * HWP needs some special consideration, because on BDX the |
| 1813 | * HWP_REQUEST uses abstract value to represent performance |
| 1814 | * rather than pure ratios. |
| 1815 | */ |
| 1816 | if (hwp_active) { |
| 1817 | intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state); |
| 1818 | } else { |
| 1819 | max_state = intel_pstate_get_base_pstate(cpu); |
| 1820 | turbo_max = cpu->pstate.turbo_pstate; |
| 1821 | } |
| 1822 | |
| 1823 | max_policy_perf = max_state * policy->max / max_freq; |
Srinivas Pandruvada | 5879f87 | 2016-10-25 13:20:41 -0700 | [diff] [blame] | 1824 | if (policy->max == policy->min) { |
Rafael J. Wysocki | e4c204c | 2017-03-14 16:18:34 +0100 | [diff] [blame] | 1825 | min_policy_perf = max_policy_perf; |
Srinivas Pandruvada | 5879f87 | 2016-10-25 13:20:41 -0700 | [diff] [blame] | 1826 | } else { |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1827 | min_policy_perf = max_state * policy->min / max_freq; |
Rafael J. Wysocki | e4c204c | 2017-03-14 16:18:34 +0100 | [diff] [blame] | 1828 | min_policy_perf = clamp_t(int32_t, min_policy_perf, |
| 1829 | 0, max_policy_perf); |
Srinivas Pandruvada | 5879f87 | 2016-10-25 13:20:41 -0700 | [diff] [blame] | 1830 | } |
Chen Yu | 43717aa | 2015-09-09 18:27:31 +0800 | [diff] [blame] | 1831 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1832 | pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n", |
| 1833 | policy->cpu, max_state, |
| 1834 | min_policy_perf, max_policy_perf); |
| 1835 | |
Rafael J. Wysocki | e4c204c | 2017-03-14 16:18:34 +0100 | [diff] [blame] | 1836 | /* Normalize user input to [min_perf, max_perf] */ |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1837 | if (per_cpu_limits) { |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1838 | cpu->min_perf_ratio = min_policy_perf; |
| 1839 | cpu->max_perf_ratio = max_policy_perf; |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1840 | } else { |
| 1841 | int32_t global_min, global_max; |
Chen Yu | 43717aa | 2015-09-09 18:27:31 +0800 | [diff] [blame] | 1842 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1843 | /* Global limits are in percent of the maximum turbo P-state. */ |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1844 | global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100); |
| 1845 | global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100); |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1846 | global_min = clamp_t(int32_t, global_min, 0, global_max); |
| 1847 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1848 | pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu, |
| 1849 | global_min, global_max); |
| 1850 | |
| 1851 | cpu->min_perf_ratio = max(min_policy_perf, global_min); |
| 1852 | cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf); |
| 1853 | cpu->max_perf_ratio = min(max_policy_perf, global_max); |
| 1854 | cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio); |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1855 | |
| 1856 | /* Make sure min_perf <= max_perf */ |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1857 | cpu->min_perf_ratio = min(cpu->min_perf_ratio, |
| 1858 | cpu->max_perf_ratio); |
| 1859 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1860 | } |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1861 | pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu, |
| 1862 | cpu->max_perf_ratio, |
| 1863 | cpu->min_perf_ratio); |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1866 | static int intel_pstate_set_policy(struct cpufreq_policy *policy) |
Pali Rohár | 36b4bed | 2014-10-16 01:16:51 +0200 | [diff] [blame] | 1867 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1868 | struct cpudata *cpu; |
| 1869 | |
Gabriele Mazzotta | 4521e1a0 | 2014-10-13 08:37:41 -0700 | [diff] [blame] | 1870 | if (!policy->cpuinfo.max_freq) |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 1871 | return -ENODEV; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1872 | |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1873 | pr_debug("set_policy cpuinfo.max %u policy->max %u\n", |
Kristen Carlson Accardi | a047599 | 2015-01-29 13:03:52 -0800 | [diff] [blame] | 1874 | policy->cpuinfo.max_freq, policy->max); |
| 1875 | |
| 1876 | cpu = all_cpu_data[policy->cpu]; |
Srinivas Pandruvada | d1b6848 | 2013-04-09 22:38:18 +0000 | [diff] [blame] | 1877 | cpu->policy = policy->policy; |
| 1878 | |
Srinivas Pandruvada | b59fe54 | 2016-12-06 13:32:15 -0800 | [diff] [blame] | 1879 | mutex_lock(&intel_pstate_limits_lock); |
| 1880 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 1881 | intel_pstate_update_perf_limits(policy, cpu); |
Rafael J. Wysocki | a240c4a | 2017-03-02 23:29:12 +0100 | [diff] [blame] | 1882 | |
Rafael J. Wysocki | 2f1d407 | 2016-10-24 23:20:25 +0200 | [diff] [blame] | 1883 | if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) { |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1884 | /* |
| 1885 | * NOHZ_FULL CPUs need this as the governor callback may not |
| 1886 | * be invoked on them. |
| 1887 | */ |
| 1888 | intel_pstate_clear_update_util_hook(policy->cpu); |
| 1889 | intel_pstate_max_within_limits(cpu); |
Len Brown | 82b4e03 | 2017-06-23 22:11:54 -0700 | [diff] [blame] | 1890 | } else { |
| 1891 | intel_pstate_set_update_util_hook(policy->cpu); |
Rafael J. Wysocki | a6c6ead | 2016-10-19 02:57:22 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 1894 | if (hwp_active) { |
| 1895 | /* |
| 1896 | * When hwp_boost was active before and dynamically it |
| 1897 | * was turned off, in that case we need to clear the |
| 1898 | * update util hook. |
| 1899 | */ |
| 1900 | if (!hwp_boost) |
| 1901 | intel_pstate_clear_update_util_hook(policy->cpu); |
Rafael J. Wysocki | 2bfc4cb | 2017-03-28 00:22:16 +0200 | [diff] [blame] | 1902 | intel_pstate_hwp_set(policy->cpu); |
Srinivas Pandruvada | e0efd5b | 2018-06-05 14:42:39 -0700 | [diff] [blame^] | 1903 | } |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 1904 | |
Srinivas Pandruvada | b59fe54 | 2016-12-06 13:32:15 -0800 | [diff] [blame] | 1905 | mutex_unlock(&intel_pstate_limits_lock); |
| 1906 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1907 | return 0; |
| 1908 | } |
| 1909 | |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 1910 | static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy, |
| 1911 | struct cpudata *cpu) |
| 1912 | { |
| 1913 | if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate && |
| 1914 | policy->max < policy->cpuinfo.max_freq && |
| 1915 | policy->max > cpu->pstate.max_freq) { |
| 1916 | pr_debug("policy->max > max non turbo frequency\n"); |
| 1917 | policy->max = policy->cpuinfo.max_freq; |
| 1918 | } |
| 1919 | } |
| 1920 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1921 | static int intel_pstate_verify_policy(struct cpufreq_policy *policy) |
| 1922 | { |
Srinivas Pandruvada | 7d9a8a9 | 2017-01-18 10:48:23 -0800 | [diff] [blame] | 1923 | struct cpudata *cpu = all_cpu_data[policy->cpu]; |
Srinivas Pandruvada | 7d9a8a9 | 2017-01-18 10:48:23 -0800 | [diff] [blame] | 1924 | |
| 1925 | update_turbo_state(); |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 1926 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 1927 | intel_pstate_get_max_freq(cpu)); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1928 | |
Stratos Karafotis | 285cb99 | 2014-07-18 08:37:21 -0700 | [diff] [blame] | 1929 | if (policy->policy != CPUFREQ_POLICY_POWERSAVE && |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 1930 | policy->policy != CPUFREQ_POLICY_PERFORMANCE) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1931 | return -EINVAL; |
| 1932 | |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 1933 | intel_pstate_adjust_policy_max(policy, cpu); |
| 1934 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1935 | return 0; |
| 1936 | } |
| 1937 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1938 | static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1939 | { |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1940 | intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1941 | } |
| 1942 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1943 | static void intel_pstate_stop_cpu(struct cpufreq_policy *policy) |
| 1944 | { |
| 1945 | pr_debug("CPU %d exiting\n", policy->cpu); |
| 1946 | |
| 1947 | intel_pstate_clear_update_util_hook(policy->cpu); |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 1948 | if (hwp_active) |
| 1949 | intel_pstate_hwp_save_state(policy); |
| 1950 | else |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1951 | intel_cpufreq_stop_cpu(policy); |
| 1952 | } |
| 1953 | |
| 1954 | static int intel_pstate_cpu_exit(struct cpufreq_policy *policy) |
| 1955 | { |
| 1956 | intel_pstate_exit_perf_limits(policy); |
| 1957 | |
| 1958 | policy->fast_switch_possible = false; |
| 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | static int __intel_pstate_cpu_init(struct cpufreq_policy *policy) |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1964 | { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1965 | struct cpudata *cpu; |
Dirk Brandewie | 52e0a50 | 2013-10-15 11:06:14 -0700 | [diff] [blame] | 1966 | int rc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1967 | |
| 1968 | rc = intel_pstate_init_cpu(policy->cpu); |
| 1969 | if (rc) |
| 1970 | return rc; |
| 1971 | |
| 1972 | cpu = all_cpu_data[policy->cpu]; |
| 1973 | |
Srinivas Pandruvada | 1a4fe38 | 2017-06-12 16:30:27 -0700 | [diff] [blame] | 1974 | cpu->max_perf_ratio = 0xFF; |
| 1975 | cpu->min_perf_ratio = 0; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1976 | |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1977 | policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling; |
| 1978 | policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1979 | |
| 1980 | /* cpuinfo and default policy values */ |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 1981 | policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling; |
Srinivas Pandruvada | 983e600 | 2016-06-07 17:38:53 -0700 | [diff] [blame] | 1982 | update_turbo_state(); |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 1983 | policy->cpuinfo.max_freq = global.turbo_disabled ? |
Srinivas Pandruvada | 983e600 | 2016-06-07 17:38:53 -0700 | [diff] [blame] | 1984 | cpu->pstate.max_pstate : cpu->pstate.turbo_pstate; |
| 1985 | policy->cpuinfo.max_freq *= cpu->pstate.scaling; |
| 1986 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1987 | intel_pstate_init_acpi_perf_limits(policy); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1988 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1989 | policy->fast_switch_possible = true; |
| 1990 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 1991 | return 0; |
| 1992 | } |
| 1993 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1994 | static int intel_pstate_cpu_init(struct cpufreq_policy *policy) |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 1995 | { |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 1996 | int ret = __intel_pstate_cpu_init(policy); |
| 1997 | |
| 1998 | if (ret) |
| 1999 | return ret; |
| 2000 | |
Rafael J. Wysocki | 7de3255 | 2017-03-18 00:57:39 +0100 | [diff] [blame] | 2001 | if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE)) |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2002 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; |
| 2003 | else |
| 2004 | policy->policy = CPUFREQ_POLICY_POWERSAVE; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 2005 | |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2009 | static struct cpufreq_driver intel_pstate = { |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2010 | .flags = CPUFREQ_CONST_LOOPS, |
| 2011 | .verify = intel_pstate_verify_policy, |
| 2012 | .setpolicy = intel_pstate_set_policy, |
Srinivas Pandruvada | 984edbd | 2016-12-06 13:32:16 -0800 | [diff] [blame] | 2013 | .suspend = intel_pstate_hwp_save_state, |
Srinivas Pandruvada | 8442885 | 2016-11-24 16:07:10 -0800 | [diff] [blame] | 2014 | .resume = intel_pstate_resume, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2015 | .init = intel_pstate_cpu_init, |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 2016 | .exit = intel_pstate_cpu_exit, |
Dirk Brandewie | bb18008 | 2014-03-19 08:45:54 -0700 | [diff] [blame] | 2017 | .stop_cpu = intel_pstate_stop_cpu, |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2018 | .name = "intel_pstate", |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2019 | }; |
| 2020 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2021 | static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy) |
| 2022 | { |
| 2023 | struct cpudata *cpu = all_cpu_data[policy->cpu]; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2024 | |
| 2025 | update_turbo_state(); |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 2026 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 2027 | intel_pstate_get_max_freq(cpu)); |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2028 | |
Rafael J. Wysocki | 80b120c | 2017-03-23 00:00:47 +0100 | [diff] [blame] | 2029 | intel_pstate_adjust_policy_max(policy, cpu); |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2030 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 2031 | intel_pstate_update_perf_limits(policy, cpu); |
| 2032 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2033 | return 0; |
| 2034 | } |
| 2035 | |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2036 | /* Use of trace in passive mode: |
| 2037 | * |
| 2038 | * In passive mode the trace core_busy field (also known as the |
| 2039 | * performance field, and lablelled as such on the graphs; also known as |
| 2040 | * core_avg_perf) is not needed and so is re-assigned to indicate if the |
| 2041 | * driver call was via the normal or fast switch path. Various graphs |
| 2042 | * output from the intel_pstate_tracer.py utility that include core_busy |
| 2043 | * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%, |
| 2044 | * so we use 10 to indicate the the normal path through the driver, and |
| 2045 | * 90 to indicate the fast switch path through the driver. |
| 2046 | * The scaled_busy field is not used, and is set to 0. |
| 2047 | */ |
| 2048 | |
| 2049 | #define INTEL_PSTATE_TRACE_TARGET 10 |
| 2050 | #define INTEL_PSTATE_TRACE_FAST_SWITCH 90 |
| 2051 | |
| 2052 | static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate) |
| 2053 | { |
| 2054 | struct sample *sample; |
| 2055 | |
| 2056 | if (!trace_pstate_sample_enabled()) |
| 2057 | return; |
| 2058 | |
| 2059 | if (!intel_pstate_sample(cpu, ktime_get())) |
| 2060 | return; |
| 2061 | |
| 2062 | sample = &cpu->sample; |
| 2063 | trace_pstate_sample(trace_type, |
| 2064 | 0, |
| 2065 | old_pstate, |
| 2066 | cpu->pstate.current_pstate, |
| 2067 | sample->mperf, |
| 2068 | sample->aperf, |
| 2069 | sample->tsc, |
| 2070 | get_avg_frequency(cpu), |
| 2071 | fp_toint(cpu->iowait_boost * 100)); |
| 2072 | } |
| 2073 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2074 | static int intel_cpufreq_target(struct cpufreq_policy *policy, |
| 2075 | unsigned int target_freq, |
| 2076 | unsigned int relation) |
| 2077 | { |
| 2078 | struct cpudata *cpu = all_cpu_data[policy->cpu]; |
| 2079 | struct cpufreq_freqs freqs; |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2080 | int target_pstate, old_pstate; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2081 | |
Rafael J. Wysocki | 64897b2 | 2017-03-21 22:19:07 +0100 | [diff] [blame] | 2082 | update_turbo_state(); |
| 2083 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2084 | freqs.old = policy->cur; |
Rafael J. Wysocki | 64897b2 | 2017-03-21 22:19:07 +0100 | [diff] [blame] | 2085 | freqs.new = target_freq; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2086 | |
| 2087 | cpufreq_freq_transition_begin(policy, &freqs); |
| 2088 | switch (relation) { |
| 2089 | case CPUFREQ_RELATION_L: |
| 2090 | target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling); |
| 2091 | break; |
| 2092 | case CPUFREQ_RELATION_H: |
| 2093 | target_pstate = freqs.new / cpu->pstate.scaling; |
| 2094 | break; |
| 2095 | default: |
| 2096 | target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling); |
| 2097 | break; |
| 2098 | } |
| 2099 | target_pstate = intel_pstate_prepare_request(cpu, target_pstate); |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2100 | old_pstate = cpu->pstate.current_pstate; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2101 | if (target_pstate != cpu->pstate.current_pstate) { |
| 2102 | cpu->pstate.current_pstate = target_pstate; |
| 2103 | wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, |
| 2104 | pstate_funcs.get_val(cpu, target_pstate)); |
| 2105 | } |
Rafael J. Wysocki | 6407829 | 2017-03-03 23:51:31 +0100 | [diff] [blame] | 2106 | freqs.new = target_pstate * cpu->pstate.scaling; |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2107 | intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate); |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2108 | cpufreq_freq_transition_end(policy, &freqs, false); |
| 2109 | |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
| 2113 | static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy, |
| 2114 | unsigned int target_freq) |
| 2115 | { |
| 2116 | struct cpudata *cpu = all_cpu_data[policy->cpu]; |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2117 | int target_pstate, old_pstate; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2118 | |
Rafael J. Wysocki | 64897b2 | 2017-03-21 22:19:07 +0100 | [diff] [blame] | 2119 | update_turbo_state(); |
| 2120 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2121 | target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling); |
Rafael J. Wysocki | 6407829 | 2017-03-03 23:51:31 +0100 | [diff] [blame] | 2122 | target_pstate = intel_pstate_prepare_request(cpu, target_pstate); |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2123 | old_pstate = cpu->pstate.current_pstate; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2124 | intel_pstate_update_pstate(cpu, target_pstate); |
Doug Smythies | 50e9ffa | 2018-05-14 08:35:49 -0700 | [diff] [blame] | 2125 | intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate); |
Rafael J. Wysocki | 6407829 | 2017-03-03 23:51:31 +0100 | [diff] [blame] | 2126 | return target_pstate * cpu->pstate.scaling; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy) |
| 2130 | { |
| 2131 | int ret = __intel_pstate_cpu_init(policy); |
| 2132 | |
| 2133 | if (ret) |
| 2134 | return ret; |
| 2135 | |
| 2136 | policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY; |
Rafael J. Wysocki | 1b72e7f | 2017-04-11 00:20:41 +0200 | [diff] [blame] | 2137 | policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2138 | /* This reflects the intel_pstate_get_cpu_pstates() setting. */ |
| 2139 | policy->cur = policy->cpuinfo.min_freq; |
| 2140 | |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | static struct cpufreq_driver intel_cpufreq = { |
| 2145 | .flags = CPUFREQ_CONST_LOOPS, |
| 2146 | .verify = intel_cpufreq_verify_policy, |
| 2147 | .target = intel_cpufreq_target, |
| 2148 | .fast_switch = intel_cpufreq_fast_switch, |
| 2149 | .init = intel_cpufreq_cpu_init, |
| 2150 | .exit = intel_pstate_cpu_exit, |
| 2151 | .stop_cpu = intel_cpufreq_stop_cpu, |
| 2152 | .name = "intel_cpufreq", |
| 2153 | }; |
| 2154 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2155 | static struct cpufreq_driver *default_driver = &intel_pstate; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2156 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2157 | static void intel_pstate_driver_cleanup(void) |
| 2158 | { |
| 2159 | unsigned int cpu; |
| 2160 | |
| 2161 | get_online_cpus(); |
| 2162 | for_each_online_cpu(cpu) { |
| 2163 | if (all_cpu_data[cpu]) { |
| 2164 | if (intel_pstate_driver == &intel_pstate) |
| 2165 | intel_pstate_clear_update_util_hook(cpu); |
| 2166 | |
| 2167 | kfree(all_cpu_data[cpu]); |
| 2168 | all_cpu_data[cpu] = NULL; |
| 2169 | } |
| 2170 | } |
| 2171 | put_online_cpus(); |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2172 | intel_pstate_driver = NULL; |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2175 | static int intel_pstate_register_driver(struct cpufreq_driver *driver) |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2176 | { |
| 2177 | int ret; |
| 2178 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 2179 | memset(&global, 0, sizeof(global)); |
| 2180 | global.max_perf_pct = 100; |
Rafael J. Wysocki | c3a49c8 | 2017-02-28 00:05:01 +0100 | [diff] [blame] | 2181 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2182 | intel_pstate_driver = driver; |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2183 | ret = cpufreq_register_driver(intel_pstate_driver); |
| 2184 | if (ret) { |
| 2185 | intel_pstate_driver_cleanup(); |
| 2186 | return ret; |
| 2187 | } |
| 2188 | |
Rafael J. Wysocki | c5a2ee7 | 2017-03-22 23:58:57 +0100 | [diff] [blame] | 2189 | global.min_perf_pct = min_perf_pct_min(); |
| 2190 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | static int intel_pstate_unregister_driver(void) |
| 2195 | { |
| 2196 | if (hwp_active) |
| 2197 | return -EBUSY; |
| 2198 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2199 | cpufreq_unregister_driver(intel_pstate_driver); |
| 2200 | intel_pstate_driver_cleanup(); |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
| 2205 | static ssize_t intel_pstate_show_status(char *buf) |
| 2206 | { |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2207 | if (!intel_pstate_driver) |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2208 | return sprintf(buf, "off\n"); |
| 2209 | |
| 2210 | return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ? |
| 2211 | "active" : "passive"); |
| 2212 | } |
| 2213 | |
| 2214 | static int intel_pstate_update_status(const char *buf, size_t size) |
| 2215 | { |
| 2216 | int ret; |
| 2217 | |
| 2218 | if (size == 3 && !strncmp(buf, "off", size)) |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2219 | return intel_pstate_driver ? |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2220 | intel_pstate_unregister_driver() : -EINVAL; |
| 2221 | |
| 2222 | if (size == 6 && !strncmp(buf, "active", size)) { |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2223 | if (intel_pstate_driver) { |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2224 | if (intel_pstate_driver == &intel_pstate) |
| 2225 | return 0; |
| 2226 | |
| 2227 | ret = intel_pstate_unregister_driver(); |
| 2228 | if (ret) |
| 2229 | return ret; |
| 2230 | } |
| 2231 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2232 | return intel_pstate_register_driver(&intel_pstate); |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | if (size == 7 && !strncmp(buf, "passive", size)) { |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2236 | if (intel_pstate_driver) { |
Rafael J. Wysocki | 0042b2c | 2017-03-28 00:14:08 +0200 | [diff] [blame] | 2237 | if (intel_pstate_driver == &intel_cpufreq) |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2238 | return 0; |
| 2239 | |
| 2240 | ret = intel_pstate_unregister_driver(); |
| 2241 | if (ret) |
| 2242 | return ret; |
| 2243 | } |
| 2244 | |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2245 | return intel_pstate_register_driver(&intel_cpufreq); |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | return -EINVAL; |
| 2249 | } |
| 2250 | |
Jisheng Zhang | eed4360 | 2016-06-27 18:07:16 +0800 | [diff] [blame] | 2251 | static int no_load __initdata; |
| 2252 | static int no_hwp __initdata; |
| 2253 | static int hwp_only __initdata; |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 2254 | static unsigned int force_load __initdata; |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 2255 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 2256 | static int __init intel_pstate_msrs_not_valid(void) |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 2257 | { |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 2258 | if (!pstate_funcs.get_max() || |
Stratos Karafotis | c410833 | 2014-07-18 08:37:23 -0700 | [diff] [blame] | 2259 | !pstate_funcs.get_min() || |
| 2260 | !pstate_funcs.get_turbo()) |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 2261 | return -ENODEV; |
| 2262 | |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 2263 | return 0; |
| 2264 | } |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 2265 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 2266 | static void __init copy_cpu_funcs(struct pstate_funcs *funcs) |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 2267 | { |
| 2268 | pstate_funcs.get_max = funcs->get_max; |
Srinivas Pandruvada | 3bcc6fa | 2015-10-14 16:12:00 -0700 | [diff] [blame] | 2269 | pstate_funcs.get_max_physical = funcs->get_max_physical; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 2270 | pstate_funcs.get_min = funcs->get_min; |
| 2271 | pstate_funcs.get_turbo = funcs->get_turbo; |
Dirk Brandewie | b27580b | 2014-10-13 08:37:43 -0700 | [diff] [blame] | 2272 | pstate_funcs.get_scaling = funcs->get_scaling; |
Rafael J. Wysocki | fdfdb2b | 2016-03-18 23:20:02 +0100 | [diff] [blame] | 2273 | pstate_funcs.get_val = funcs->get_val; |
Dirk Brandewie | 007bea0 | 2013-12-18 10:32:39 -0800 | [diff] [blame] | 2274 | pstate_funcs.get_vid = funcs->get_vid; |
Srinivas Pandruvada | 6e34e1f | 2017-07-13 15:03:51 -0700 | [diff] [blame] | 2275 | pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift; |
Dirk Brandewie | 016c815 | 2013-10-21 09:20:34 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 2278 | #ifdef CONFIG_ACPI |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2279 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 2280 | static bool __init intel_pstate_no_acpi_pss(void) |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2281 | { |
| 2282 | int i; |
| 2283 | |
| 2284 | for_each_possible_cpu(i) { |
| 2285 | acpi_status status; |
| 2286 | union acpi_object *pss; |
| 2287 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 2288 | struct acpi_processor *pr = per_cpu(processors, i); |
| 2289 | |
| 2290 | if (!pr) |
| 2291 | continue; |
| 2292 | |
| 2293 | status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer); |
| 2294 | if (ACPI_FAILURE(status)) |
| 2295 | continue; |
| 2296 | |
| 2297 | pss = buffer.pointer; |
| 2298 | if (pss && pss->type == ACPI_TYPE_PACKAGE) { |
| 2299 | kfree(pss); |
| 2300 | return false; |
| 2301 | } |
| 2302 | |
| 2303 | kfree(pss); |
| 2304 | } |
| 2305 | |
| 2306 | return true; |
| 2307 | } |
| 2308 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 2309 | static bool __init intel_pstate_has_acpi_ppc(void) |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 2310 | { |
| 2311 | int i; |
| 2312 | |
| 2313 | for_each_possible_cpu(i) { |
| 2314 | struct acpi_processor *pr = per_cpu(processors, i); |
| 2315 | |
| 2316 | if (!pr) |
| 2317 | continue; |
| 2318 | if (acpi_has_method(pr->handle, "_PPC")) |
| 2319 | return true; |
| 2320 | } |
| 2321 | return false; |
| 2322 | } |
| 2323 | |
| 2324 | enum { |
| 2325 | PSS, |
| 2326 | PPC, |
| 2327 | }; |
| 2328 | |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2329 | /* Hardware vendor-specific info that has its own power management modes */ |
Toshi Kani | 5e93232 | 2017-08-23 16:54:44 -0600 | [diff] [blame] | 2330 | static struct acpi_platform_list plat_info[] __initdata = { |
| 2331 | {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS}, |
| 2332 | {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2333 | {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2334 | {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2335 | {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2336 | {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2337 | {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2338 | {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2339 | {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2340 | {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2341 | {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2342 | {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2343 | {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2344 | {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2345 | {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC}, |
| 2346 | { } /* End */ |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2347 | }; |
| 2348 | |
Jisheng Zhang | 29327c8 | 2016-06-27 18:07:17 +0800 | [diff] [blame] | 2349 | static bool __init intel_pstate_platform_pwr_mgmt_exists(void) |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2350 | { |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 2351 | const struct x86_cpu_id *id; |
| 2352 | u64 misc_pwr; |
Toshi Kani | 5e93232 | 2017-08-23 16:54:44 -0600 | [diff] [blame] | 2353 | int idx; |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 2354 | |
| 2355 | id = x86_match_cpu(intel_pstate_cpu_oob_ids); |
| 2356 | if (id) { |
| 2357 | rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr); |
| 2358 | if ( misc_pwr & (1 << 8)) |
| 2359 | return true; |
| 2360 | } |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2361 | |
Toshi Kani | 5e93232 | 2017-08-23 16:54:44 -0600 | [diff] [blame] | 2362 | idx = acpi_match_platform_list(plat_info); |
| 2363 | if (idx < 0) |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2364 | return false; |
| 2365 | |
Toshi Kani | 5e93232 | 2017-08-23 16:54:44 -0600 | [diff] [blame] | 2366 | switch (plat_info[idx].data) { |
| 2367 | case PSS: |
| 2368 | return intel_pstate_no_acpi_pss(); |
| 2369 | case PPC: |
| 2370 | return intel_pstate_has_acpi_ppc() && !force_load; |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | return false; |
| 2374 | } |
Rafael J. Wysocki | d0ea59e | 2016-11-17 22:47:47 +0100 | [diff] [blame] | 2375 | |
| 2376 | static void intel_pstate_request_control_from_smm(void) |
| 2377 | { |
| 2378 | /* |
| 2379 | * It may be unsafe to request P-states control from SMM if _PPC support |
| 2380 | * has not been enabled. |
| 2381 | */ |
| 2382 | if (acpi_ppc) |
| 2383 | acpi_processor_pstate_control(); |
| 2384 | } |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2385 | #else /* CONFIG_ACPI not enabled */ |
| 2386 | static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; } |
ethan zhao | 966916e | 2014-12-01 11:32:08 +0900 | [diff] [blame] | 2387 | static inline bool intel_pstate_has_acpi_ppc(void) { return false; } |
Rafael J. Wysocki | d0ea59e | 2016-11-17 22:47:47 +0100 | [diff] [blame] | 2388 | static inline void intel_pstate_request_control_from_smm(void) {} |
Adrian Huang | fbbcdc0 | 2013-10-31 23:24:05 +0800 | [diff] [blame] | 2389 | #endif /* CONFIG_ACPI */ |
| 2390 | |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 2391 | static const struct x86_cpu_id hwp_support_ids[] __initconst = { |
| 2392 | { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP }, |
| 2393 | {} |
| 2394 | }; |
| 2395 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2396 | static int __init intel_pstate_init(void) |
| 2397 | { |
Rafael J. Wysocki | eb5139d | 2017-03-22 23:52:18 +0100 | [diff] [blame] | 2398 | int rc; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2399 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 2400 | if (no_load) |
| 2401 | return -ENODEV; |
| 2402 | |
Rafael J. Wysocki | eb5139d | 2017-03-22 23:52:18 +0100 | [diff] [blame] | 2403 | if (x86_match_cpu(hwp_support_ids)) { |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 2404 | copy_cpu_funcs(&core_funcs); |
Rafael J. Wysocki | c4f3f70 | 2017-07-25 00:12:20 +0200 | [diff] [blame] | 2405 | if (!no_hwp) { |
Rafael J. Wysocki | eb5139d | 2017-03-22 23:52:18 +0100 | [diff] [blame] | 2406 | hwp_active++; |
| 2407 | intel_pstate.attr = hwp_cpufreq_attrs; |
| 2408 | goto hwp_cpu_matched; |
| 2409 | } |
| 2410 | } else { |
| 2411 | const struct x86_cpu_id *id; |
Rafael J. Wysocki | eb5139d | 2017-03-22 23:52:18 +0100 | [diff] [blame] | 2412 | |
| 2413 | id = x86_match_cpu(intel_pstate_cpu_ids); |
| 2414 | if (!id) |
| 2415 | return -ENODEV; |
| 2416 | |
Rafael J. Wysocki | 2f49afc | 2017-03-28 00:19:03 +0200 | [diff] [blame] | 2417 | copy_cpu_funcs((struct pstate_funcs *)id->driver_data); |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 2418 | } |
| 2419 | |
Dirk Brandewie | b563b4e | 2013-03-22 01:29:28 +0100 | [diff] [blame] | 2420 | if (intel_pstate_msrs_not_valid()) |
| 2421 | return -ENODEV; |
| 2422 | |
Srinivas Pandruvada | 7791e4a | 2016-02-25 15:09:19 -0800 | [diff] [blame] | 2423 | hwp_cpu_matched: |
| 2424 | /* |
| 2425 | * The Intel pstate driver will be ignored if the platform |
| 2426 | * firmware has its own power management modes. |
| 2427 | */ |
| 2428 | if (intel_pstate_platform_pwr_mgmt_exists()) |
| 2429 | return -ENODEV; |
| 2430 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2431 | if (!hwp_active && hwp_only) |
| 2432 | return -ENOTSUPP; |
| 2433 | |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 2434 | pr_info("Intel P-state driver initializing\n"); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2435 | |
Wei Yongjun | b57ffac | 2013-05-13 08:03:43 +0000 | [diff] [blame] | 2436 | all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus()); |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2437 | if (!all_cpu_data) |
| 2438 | return -ENOMEM; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2439 | |
Rafael J. Wysocki | d0ea59e | 2016-11-17 22:47:47 +0100 | [diff] [blame] | 2440 | intel_pstate_request_control_from_smm(); |
| 2441 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2442 | intel_pstate_sysfs_expose_params(); |
Dirk Brandewie | b69880f | 2014-01-16 10:32:25 -0800 | [diff] [blame] | 2443 | |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 2444 | mutex_lock(&intel_pstate_driver_lock); |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2445 | rc = intel_pstate_register_driver(default_driver); |
Rafael J. Wysocki | 0c30b65 | 2017-01-11 04:12:16 +0100 | [diff] [blame] | 2446 | mutex_unlock(&intel_pstate_driver_lock); |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2447 | if (rc) |
| 2448 | return rc; |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 2449 | |
Dirk Brandewie | 907cc90 | 2013-03-05 14:15:27 -0800 | [diff] [blame] | 2450 | if (hwp_active) |
| 2451 | pr_info("HWP enabled\n"); |
| 2452 | |
Rafael J. Wysocki | fb1fe10 | 2017-01-05 02:53:12 +0100 | [diff] [blame] | 2453 | return 0; |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2454 | } |
| 2455 | device_initcall(intel_pstate_init); |
| 2456 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 2457 | static int __init intel_pstate_setup(char *str) |
| 2458 | { |
| 2459 | if (!str) |
| 2460 | return -EINVAL; |
| 2461 | |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2462 | if (!strcmp(str, "disable")) { |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 2463 | no_load = 1; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2464 | } else if (!strcmp(str, "passive")) { |
| 2465 | pr_info("Passive mode enabled\n"); |
Rafael J. Wysocki | ee8df89 | 2017-03-28 00:13:00 +0200 | [diff] [blame] | 2466 | default_driver = &intel_cpufreq; |
Rafael J. Wysocki | 001c76f | 2016-11-17 23:34:17 +0100 | [diff] [blame] | 2467 | no_hwp = 1; |
| 2468 | } |
Prarit Bhargava | 539342f | 2015-10-22 09:43:31 -0400 | [diff] [blame] | 2469 | if (!strcmp(str, "no_hwp")) { |
Joe Perches | 4836df1 | 2016-04-05 13:28:23 -0700 | [diff] [blame] | 2470 | pr_info("HWP disabled\n"); |
Dirk Brandewie | 2f86dc4 | 2014-11-06 09:40:47 -0800 | [diff] [blame] | 2471 | no_hwp = 1; |
Prarit Bhargava | 539342f | 2015-10-22 09:43:31 -0400 | [diff] [blame] | 2472 | } |
Ethan Zhao | aa4ea34 | 2014-12-09 10:43:19 +0900 | [diff] [blame] | 2473 | if (!strcmp(str, "force")) |
| 2474 | force_load = 1; |
Kristen Carlson Accardi | d64c3b0 | 2015-02-06 13:41:55 -0800 | [diff] [blame] | 2475 | if (!strcmp(str, "hwp_only")) |
| 2476 | hwp_only = 1; |
Srinivas Pandruvada | eae48f0 | 2016-10-25 13:20:40 -0700 | [diff] [blame] | 2477 | if (!strcmp(str, "per_cpu_perf_limits")) |
| 2478 | per_cpu_limits = true; |
Srinivas Pandruvada | 9522a2f | 2016-04-27 15:48:06 -0700 | [diff] [blame] | 2479 | |
| 2480 | #ifdef CONFIG_ACPI |
| 2481 | if (!strcmp(str, "support_acpi_ppc")) |
| 2482 | acpi_ppc = true; |
| 2483 | #endif |
| 2484 | |
Dirk Brandewie | 6be2649 | 2013-02-15 22:55:10 +0100 | [diff] [blame] | 2485 | return 0; |
| 2486 | } |
| 2487 | early_param("intel_pstate", intel_pstate_setup); |
| 2488 | |
Dirk Brandewie | 93f0822 | 2013-02-06 09:02:13 -0800 | [diff] [blame] | 2489 | MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>"); |
| 2490 | MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors"); |
| 2491 | MODULE_LICENSE("GPL"); |