blob: 17e566afbb41c3da0a720a56905972dffb229ec6 [file] [log] [blame]
Dirk Brandewie93f08222013-02-06 09:02:13 -08001/*
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00002 * intel_pstate.c: Native P state management for Intel processors
Dirk Brandewie93f08222013-02-06 09:02:13 -08003 *
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 Perches4836df12016-04-05 13:28:23 -070013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Dirk Brandewie93f08222013-02-06 09:02:13 -080015#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 Molnar55687da2017-02-08 18:51:31 +010022#include <linux/sched/cpufreq.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080023#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 Huangfbbcdc02013-10-31 23:24:05 +080029#include <linux/acpi.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100030#include <linux/vmalloc.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080031#include <trace/events/power.h>
32
33#include <asm/div64.h>
34#include <asm/msr.h>
35#include <asm/cpu_device_id.h>
Borislav Petkov64df1fd2015-04-03 15:19:53 +020036#include <asm/cpufeature.h>
Dave Hansen5b20c942016-06-02 17:19:45 -070037#include <asm/intel-family.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080038
Rafael J. Wysockid77d4882017-08-10 01:09:16 +020039#define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC)
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +020040
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +010041#define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
Rafael J. Wysocki1b72e7f2017-04-11 00:20:41 +020042#define INTEL_CPUFREQ_TRANSITION_DELAY 500
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +010043
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070044#ifdef CONFIG_ACPI
45#include <acpi/processor.h>
Rafael J. Wysocki17669002016-11-22 12:24:00 -080046#include <acpi/cppc_acpi.h>
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070047#endif
48
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070049#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080050#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
51#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070052
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020053#define EXT_BITS 6
54#define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -080055#define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
56#define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020057
Dirk Brandewie93f08222013-02-06 09:02:13 -080058static 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 Bhargava7180ddd2015-06-15 13:43:29 -040063static inline int32_t div_fp(s64 x, s64 y)
Dirk Brandewie93f08222013-02-06 09:02:13 -080064{
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040065 return div64_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080066}
67
Dirk Brandewied022a652014-10-13 08:37:44 -070068static 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. Wysockiff35f022017-03-28 00:09:18 +020079static inline int32_t percent_fp(int percent)
80{
81 return div_fp(percent, 100);
82}
83
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020084static inline u64 mul_ext_fp(u64 x, u64 y)
85{
86 return (x * y) >> EXT_FRAC_BITS;
87}
88
89static inline u64 div_ext_fp(u64 x, u64 y)
90{
91 return div64_u64(x << EXT_FRAC_BITS, y);
92}
93
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +010094static inline int32_t percent_ext_fp(int percent)
95{
96 return div_ext_fp(percent, 100);
97}
98
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070099/**
100 * struct sample - Store performance sample
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200101 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700102 * performance during last sample period
103 * @busy_scaled: Scaled busy value which is used to calculate next
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200104 * P state. This can be different than core_avg_perf
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700105 * 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 Pandruvada13ad7702016-04-03 13:06:46 -0700112 * @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 Brandewie93f08222013-02-06 09:02:13 -0800117struct sample {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200118 int32_t core_avg_perf;
Philippe Longepe157386b2015-12-04 17:40:30 +0100119 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800120 u64 aperf;
121 u64 mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700122 u64 tsc;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100123 u64 time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800124};
125
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700126/**
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. Wysocki001c76f2016-11-17 23:34:17 +0100137 * @max_freq: @max_pstate frequency in cpufreq units
138 * @turbo_freq: @turbo_pstate frequency in cpufreq units
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700139 *
140 * Stores the per cpu model P state limits and current P state.
141 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800142struct pstate_data {
143 int current_pstate;
144 int min_pstate;
145 int max_pstate;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700146 int max_pstate_physical;
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700147 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800148 int turbo_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100149 unsigned int max_freq;
150 unsigned int turbo_freq;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800151};
152
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700153/**
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 Brandewie007bea02013-12-18 10:32:39 -0800166struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700167 int min;
168 int max;
169 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800170 int32_t ratio;
171};
172
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700173/**
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100174 * 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 */
185struct global_params {
186 bool no_turbo;
187 bool turbo_disabled;
188 int max_perf_pct;
189 int min_perf_pct;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700190};
191
192/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700193 * struct cpudata - Per CPU instance data storage
194 * @cpu: CPU number for this instance data
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200195 * @policy: CPUFreq policy value
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700196 * @update_util: CPUFreq utility callback information
Chen Yu4578ee7e2016-05-11 14:33:08 +0800197 * @update_util_set: CPUFreq utility callback is set
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200198 * @iowait_boost: iowait-related boost fraction
199 * @last_update: Time of the last update.
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700200 * @pstate: Stores P state limits for this CPU
201 * @vid: Stores VID limits for this CPU
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700202 * @last_sample_time: Last Sample time
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700203 * @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 Pandruvada13ad7702016-04-03 13:06:46 -0700206 * @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 Pandruvada1a4fe382017-06-12 16:30:27 -0700212 * @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 Pandruvada9522a2f2016-04-27 15:48:06 -0700214 * @acpi_perf_data: Stores ACPI perf information read from _PSS
215 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800216 * @epp_powersave: Last saved HWP energy performance preference
217 * (EPP) or energy performance bias (EPB),
218 * when policy switched to performance
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800219 * @epp_policy: Last saved policy used to set EPP/EPB
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800220 * @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 Pandruvada13ad7702016-04-03 13:06:46 -0700224 *
225 * This structure stores per CPU instance data for all CPUs.
226 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800227struct cpudata {
228 int cpu;
229
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200230 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100231 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800232 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800233
Dirk Brandewie93f08222013-02-06 09:02:13 -0800234 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800235 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800236
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200237 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100238 u64 last_sample_time;
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700239 u64 aperf_mperf_shift;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800240 u64 prev_aperf;
241 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700242 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100243 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800244 struct sample sample;
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700245 int32_t min_perf_ratio;
246 int32_t max_perf_ratio;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700247#ifdef CONFIG_ACPI
248 struct acpi_processor_performance acpi_perf_data;
249 bool valid_pss_table;
250#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200251 unsigned int iowait_boost;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800252 s16 epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800253 s16 epp_policy;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800254 s16 epp_default;
255 s16 epp_saved;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800256};
257
258static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700259
260/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700261 * struct pstate_funcs - Per CPU model specific callbacks
262 * @get_max: Callback to get maximum non turbo effective P state
263 * @get_max_physical: Callback to get maximum non turbo physical P state
264 * @get_min: Callback to get minimum P state
265 * @get_turbo: Callback to get turbo P state
266 * @get_scaling: Callback to get frequency scaling factor
267 * @get_val: Callback to convert P state to actual MSR write value
268 * @get_vid: Callback to get VID data for Atom platforms
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700269 *
270 * Core and Atom CPU models have different way to get P State limits. This
271 * structure is used to store those callbacks.
272 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700273struct pstate_funcs {
274 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700275 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700276 int (*get_min)(void);
277 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700278 int (*get_scaling)(void);
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700279 int (*get_aperf_mperf_shift)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100280 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800281 void (*get_vid)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800282};
283
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800284static struct pstate_funcs pstate_funcs __read_mostly;
Rafael J. Wysocki5c439052017-03-28 00:05:44 +0200285
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800286static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700287static bool per_cpu_limits __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700288
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200289static struct cpufreq_driver *intel_pstate_driver __read_mostly;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100290
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700291#ifdef CONFIG_ACPI
292static bool acpi_ppc;
293#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700294
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100295static struct global_params global;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800296
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100297static DEFINE_MUTEX(intel_pstate_driver_lock);
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700298static DEFINE_MUTEX(intel_pstate_limits_lock);
299
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700300#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700301
302static bool intel_pstate_get_ppc_enable_status(void)
303{
304 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
305 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
306 return true;
307
308 return acpi_ppc;
309}
310
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800311#ifdef CONFIG_ACPI_CPPC_LIB
312
313/* The work item is needed to avoid CPU hotplug locking issues */
314static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
315{
316 sched_set_itmt_support();
317}
318
319static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
320
321static void intel_pstate_set_itmt_prio(int cpu)
322{
323 struct cppc_perf_caps cppc_perf;
324 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
325 int ret;
326
327 ret = cppc_get_perf_caps(cpu, &cppc_perf);
328 if (ret)
329 return;
330
331 /*
332 * The priorities can be set regardless of whether or not
333 * sched_set_itmt_support(true) has been called and it is valid to
334 * update them at any time after it has been called.
335 */
336 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
337
338 if (max_highest_perf <= min_highest_perf) {
339 if (cppc_perf.highest_perf > max_highest_perf)
340 max_highest_perf = cppc_perf.highest_perf;
341
342 if (cppc_perf.highest_perf < min_highest_perf)
343 min_highest_perf = cppc_perf.highest_perf;
344
345 if (max_highest_perf > min_highest_perf) {
346 /*
347 * This code can be run during CPU online under the
348 * CPU hotplug locks, so sched_set_itmt_support()
349 * cannot be called from here. Queue up a work item
350 * to invoke it.
351 */
352 schedule_work(&sched_itmt_work);
353 }
354 }
355}
356#else
357static void intel_pstate_set_itmt_prio(int cpu)
358{
359}
360#endif
361
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700362static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
363{
364 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700365 int ret;
366 int i;
367
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800368 if (hwp_active) {
369 intel_pstate_set_itmt_prio(policy->cpu);
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700370 return;
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800371 }
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700372
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700373 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700374 return;
375
376 cpu = all_cpu_data[policy->cpu];
377
378 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
379 policy->cpu);
380 if (ret)
381 return;
382
383 /*
384 * Check if the control value in _PSS is for PERF_CTL MSR, which should
385 * guarantee that the states returned by it map to the states in our
386 * list directly.
387 */
388 if (cpu->acpi_perf_data.control_register.space_id !=
389 ACPI_ADR_SPACE_FIXED_HARDWARE)
390 goto err;
391
392 /*
393 * If there is only one entry _PSS, simply ignore _PSS and continue as
394 * usual without taking _PSS into account
395 */
396 if (cpu->acpi_perf_data.state_count < 2)
397 goto err;
398
399 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
400 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
401 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
402 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
403 (u32) cpu->acpi_perf_data.states[i].core_frequency,
404 (u32) cpu->acpi_perf_data.states[i].power,
405 (u32) cpu->acpi_perf_data.states[i].control);
406 }
407
408 /*
409 * The _PSS table doesn't contain whole turbo frequency range.
410 * This just contains +1 MHZ above the max non turbo frequency,
411 * with control value corresponding to max turbo ratio. But
412 * when cpufreq set policy is called, it will call with this
413 * max frequency, which will cause a reduced performance as
414 * this driver uses real max turbo frequency as the max
415 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700416 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700417 * Also need to convert to MHz as _PSS freq is in MHz.
418 */
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100419 if (!global.turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700420 cpu->acpi_perf_data.states[0].core_frequency =
421 policy->cpuinfo.max_freq / 1000;
422 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700423 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700424
425 return;
426
427 err:
428 cpu->valid_pss_table = false;
429 acpi_processor_unregister_performance(policy->cpu);
430}
431
432static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
433{
434 struct cpudata *cpu;
435
436 cpu = all_cpu_data[policy->cpu];
437 if (!cpu->valid_pss_table)
438 return;
439
440 acpi_processor_unregister_performance(policy->cpu);
441}
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700442#else
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100443static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700444{
445}
446
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100447static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700448{
449}
450#endif
451
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700452static inline void update_turbo_state(void)
453{
454 u64 misc_en;
455 struct cpudata *cpu;
456
457 cpu = all_cpu_data[0];
458 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100459 global.turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700460 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
461 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
462}
463
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100464static int min_perf_pct_min(void)
465{
466 struct cpudata *cpu = all_cpu_data[0];
Rafael J. Wysocki57caf4e2017-06-05 14:51:18 +0200467 int turbo_pstate = cpu->pstate.turbo_pstate;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100468
Rafael J. Wysocki57caf4e2017-06-05 14:51:18 +0200469 return turbo_pstate ?
Srinivas Pandruvadad4436c02017-07-10 16:23:52 -0700470 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100471}
472
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800473static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
474{
475 u64 epb;
476 int ret;
477
478 if (!static_cpu_has(X86_FEATURE_EPB))
479 return -ENXIO;
480
481 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
482 if (ret)
483 return (s16)ret;
484
485 return (s16)(epb & 0x0f);
486}
487
488static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
489{
490 s16 epp;
491
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800492 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
493 /*
494 * When hwp_req_data is 0, means that caller didn't read
495 * MSR_HWP_REQUEST, so need to read and get EPP.
496 */
497 if (!hwp_req_data) {
498 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
499 &hwp_req_data);
500 if (epp)
501 return epp;
502 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800503 epp = (hwp_req_data >> 24) & 0xff;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800504 } else {
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800505 /* When there is no EPP present, HWP uses EPB settings */
506 epp = intel_pstate_get_epb(cpu_data);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800507 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800508
509 return epp;
510}
511
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800512static int intel_pstate_set_epb(int cpu, s16 pref)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800513{
514 u64 epb;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800515 int ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800516
517 if (!static_cpu_has(X86_FEATURE_EPB))
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800518 return -ENXIO;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800519
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800520 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
521 if (ret)
522 return ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800523
524 epb = (epb & ~0x0f) | pref;
525 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800526
527 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800528}
529
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800530/*
531 * EPP/EPB display strings corresponding to EPP index in the
532 * energy_perf_strings[]
533 * index String
534 *-------------------------------------
535 * 0 default
536 * 1 performance
537 * 2 balance_performance
538 * 3 balance_power
539 * 4 power
540 */
541static const char * const energy_perf_strings[] = {
542 "default",
543 "performance",
544 "balance_performance",
545 "balance_power",
546 "power",
547 NULL
548};
Len Brown3cedbc52017-05-01 23:06:08 -0400549static const unsigned int epp_values[] = {
550 HWP_EPP_PERFORMANCE,
551 HWP_EPP_BALANCE_PERFORMANCE,
552 HWP_EPP_BALANCE_POWERSAVE,
553 HWP_EPP_POWERSAVE
554};
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800555
556static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
557{
558 s16 epp;
559 int index = -EINVAL;
560
561 epp = intel_pstate_get_epp(cpu_data, 0);
562 if (epp < 0)
563 return epp;
564
565 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
Len Brown3cedbc52017-05-01 23:06:08 -0400566 if (epp == HWP_EPP_PERFORMANCE)
567 return 1;
568 if (epp <= HWP_EPP_BALANCE_PERFORMANCE)
569 return 2;
570 if (epp <= HWP_EPP_BALANCE_POWERSAVE)
571 return 3;
572 else
573 return 4;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800574 } else if (static_cpu_has(X86_FEATURE_EPB)) {
575 /*
576 * Range:
577 * 0x00-0x03 : Performance
578 * 0x04-0x07 : Balance performance
579 * 0x08-0x0B : Balance power
580 * 0x0C-0x0F : Power
581 * The EPB is a 4 bit value, but our ranges restrict the
582 * value which can be set. Here only using top two bits
583 * effectively.
584 */
585 index = (epp >> 2) + 1;
586 }
587
588 return index;
589}
590
591static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
592 int pref_index)
593{
594 int epp = -EINVAL;
595 int ret;
596
597 if (!pref_index)
598 epp = cpu_data->epp_default;
599
600 mutex_lock(&intel_pstate_limits_lock);
601
602 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
603 u64 value;
604
605 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
606 if (ret)
607 goto return_pref;
608
609 value &= ~GENMASK_ULL(31, 24);
610
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800611 if (epp == -EINVAL)
Len Brown3cedbc52017-05-01 23:06:08 -0400612 epp = epp_values[pref_index - 1];
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800613
614 value |= (u64)epp << 24;
615 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
616 } else {
617 if (epp == -EINVAL)
618 epp = (pref_index - 1) << 2;
619 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
620 }
621return_pref:
622 mutex_unlock(&intel_pstate_limits_lock);
623
624 return ret;
625}
626
627static ssize_t show_energy_performance_available_preferences(
628 struct cpufreq_policy *policy, char *buf)
629{
630 int i = 0;
631 int ret = 0;
632
633 while (energy_perf_strings[i] != NULL)
634 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
635
636 ret += sprintf(&buf[ret], "\n");
637
638 return ret;
639}
640
641cpufreq_freq_attr_ro(energy_performance_available_preferences);
642
643static ssize_t store_energy_performance_preference(
644 struct cpufreq_policy *policy, const char *buf, size_t count)
645{
646 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
647 char str_preference[21];
648 int ret, i = 0;
649
650 ret = sscanf(buf, "%20s", str_preference);
651 if (ret != 1)
652 return -EINVAL;
653
654 while (energy_perf_strings[i] != NULL) {
655 if (!strcmp(str_preference, energy_perf_strings[i])) {
656 intel_pstate_set_energy_pref_index(cpu_data, i);
657 return count;
658 }
659 ++i;
660 }
661
662 return -EINVAL;
663}
664
665static ssize_t show_energy_performance_preference(
666 struct cpufreq_policy *policy, char *buf)
667{
668 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
669 int preference;
670
671 preference = intel_pstate_get_energy_pref_index(cpu_data);
672 if (preference < 0)
673 return preference;
674
675 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
676}
677
678cpufreq_freq_attr_rw(energy_performance_preference);
679
680static struct freq_attr *hwp_cpufreq_attrs[] = {
681 &energy_performance_preference,
682 &energy_performance_available_preferences,
683 NULL,
684};
685
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700686static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
687 int *current_max)
688{
689 u64 cap;
690
691 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
692 if (global.no_turbo)
693 *current_max = HWP_GUARANTEED_PERF(cap);
694 else
695 *current_max = HWP_HIGHEST_PERF(cap);
696
697 *phy_max = HWP_HIGHEST_PERF(cap);
698}
699
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200700static void intel_pstate_hwp_set(unsigned int cpu)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800701{
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200702 struct cpudata *cpu_data = all_cpu_data[cpu];
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700703 int max, min;
704 u64 value;
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200705 s16 epp;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700706
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700707 max = cpu_data->max_perf_ratio;
708 min = cpu_data->min_perf_ratio;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700709
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200710 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
711 min = max;
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700712
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200713 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700714
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200715 value &= ~HWP_MIN_PERF(~0L);
716 value |= HWP_MIN_PERF(min);
Srinivas Pandruvada3f8ed542017-03-13 18:30:12 -0700717
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200718 value &= ~HWP_MAX_PERF(~0L);
719 value |= HWP_MAX_PERF(max);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800720
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200721 if (cpu_data->epp_policy == cpu_data->policy)
722 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800723
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200724 cpu_data->epp_policy = cpu_data->policy;
725
726 if (cpu_data->epp_saved >= 0) {
727 epp = cpu_data->epp_saved;
728 cpu_data->epp_saved = -EINVAL;
729 goto update_epp;
730 }
731
732 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
733 epp = intel_pstate_get_epp(cpu_data, value);
734 cpu_data->epp_powersave = epp;
735 /* If EPP read was failed, then don't try to write */
736 if (epp < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800737 goto skip_epp;
738
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200739 epp = 0;
740 } else {
741 /* skip setting EPP, when saved value is invalid */
742 if (cpu_data->epp_powersave < 0)
743 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800744
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200745 /*
746 * No need to restore EPP when it is not zero. This
747 * means:
748 * - Policy is not changed
749 * - user has manually changed
750 * - Error reading EPB
751 */
752 epp = intel_pstate_get_epp(cpu_data, value);
753 if (epp)
754 goto skip_epp;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800755
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200756 epp = cpu_data->epp_powersave;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800757 }
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200758update_epp:
759 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
760 value &= ~GENMASK_ULL(31, 24);
761 value |= (u64)epp << 24;
762 } else {
763 intel_pstate_set_epb(cpu, epp);
764 }
765skip_epp:
766 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
Viresh Kumar41cfd642016-02-22 10:27:46 +0530767}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800768
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800769static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
770{
771 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
772
773 if (!hwp_active)
774 return 0;
775
776 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
777
778 return 0;
779}
780
Chen Yu70f6bf22018-01-29 10:27:57 +0800781static void intel_pstate_hwp_enable(struct cpudata *cpudata);
782
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800783static int intel_pstate_resume(struct cpufreq_policy *policy)
784{
785 if (!hwp_active)
786 return 0;
787
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100788 mutex_lock(&intel_pstate_limits_lock);
789
Chen Yu70f6bf22018-01-29 10:27:57 +0800790 if (policy->cpu == 0)
791 intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
792
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800793 all_cpu_data[policy->cpu]->epp_policy = 0;
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200794 intel_pstate_hwp_set(policy->cpu);
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100795
796 mutex_unlock(&intel_pstate_limits_lock);
797
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +0100798 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800799}
800
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100801static void intel_pstate_update_policies(void)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530802{
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100803 int cpu;
804
805 for_each_possible_cpu(cpu)
806 cpufreq_update_policy(cpu);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800807}
808
Dirk Brandewie93f08222013-02-06 09:02:13 -0800809/************************** sysfs begin ************************/
810#define show_one(file_name, object) \
811 static ssize_t show_##file_name \
812 (struct kobject *kobj, struct attribute *attr, char *buf) \
813 { \
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100814 return sprintf(buf, "%u\n", global.object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800815 }
816
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100817static ssize_t intel_pstate_show_status(char *buf);
818static int intel_pstate_update_status(const char *buf, size_t size);
819
820static ssize_t show_status(struct kobject *kobj,
821 struct attribute *attr, char *buf)
822{
823 ssize_t ret;
824
825 mutex_lock(&intel_pstate_driver_lock);
826 ret = intel_pstate_show_status(buf);
827 mutex_unlock(&intel_pstate_driver_lock);
828
829 return ret;
830}
831
832static ssize_t store_status(struct kobject *a, struct attribute *b,
833 const char *buf, size_t count)
834{
835 char *p = memchr(buf, '\n', count);
836 int ret;
837
838 mutex_lock(&intel_pstate_driver_lock);
839 ret = intel_pstate_update_status(buf, p ? p - buf : count);
840 mutex_unlock(&intel_pstate_driver_lock);
841
842 return ret < 0 ? ret : count;
843}
844
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800845static ssize_t show_turbo_pct(struct kobject *kobj,
846 struct attribute *attr, char *buf)
847{
848 struct cpudata *cpu;
849 int total, no_turbo, turbo_pct;
850 uint32_t turbo_fp;
851
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100852 mutex_lock(&intel_pstate_driver_lock);
853
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200854 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100855 mutex_unlock(&intel_pstate_driver_lock);
856 return -EAGAIN;
857 }
858
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800859 cpu = all_cpu_data[0];
860
861 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
862 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200863 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800864 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100865
866 mutex_unlock(&intel_pstate_driver_lock);
867
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800868 return sprintf(buf, "%u\n", turbo_pct);
869}
870
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800871static ssize_t show_num_pstates(struct kobject *kobj,
872 struct attribute *attr, char *buf)
873{
874 struct cpudata *cpu;
875 int total;
876
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100877 mutex_lock(&intel_pstate_driver_lock);
878
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200879 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100880 mutex_unlock(&intel_pstate_driver_lock);
881 return -EAGAIN;
882 }
883
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800884 cpu = all_cpu_data[0];
885 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100886
887 mutex_unlock(&intel_pstate_driver_lock);
888
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800889 return sprintf(buf, "%u\n", total);
890}
891
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700892static ssize_t show_no_turbo(struct kobject *kobj,
893 struct attribute *attr, char *buf)
894{
895 ssize_t ret;
896
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100897 mutex_lock(&intel_pstate_driver_lock);
898
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200899 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100900 mutex_unlock(&intel_pstate_driver_lock);
901 return -EAGAIN;
902 }
903
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700904 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100905 if (global.turbo_disabled)
906 ret = sprintf(buf, "%u\n", global.turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700907 else
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100908 ret = sprintf(buf, "%u\n", global.no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700909
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100910 mutex_unlock(&intel_pstate_driver_lock);
911
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700912 return ret;
913}
914
Dirk Brandewie93f08222013-02-06 09:02:13 -0800915static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700916 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800917{
918 unsigned int input;
919 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700920
Dirk Brandewie93f08222013-02-06 09:02:13 -0800921 ret = sscanf(buf, "%u", &input);
922 if (ret != 1)
923 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700924
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100925 mutex_lock(&intel_pstate_driver_lock);
926
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200927 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100928 mutex_unlock(&intel_pstate_driver_lock);
929 return -EAGAIN;
930 }
931
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700932 mutex_lock(&intel_pstate_limits_lock);
933
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700934 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100935 if (global.turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -0700936 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700937 mutex_unlock(&intel_pstate_limits_lock);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100938 mutex_unlock(&intel_pstate_driver_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700939 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700940 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800941
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100942 global.no_turbo = clamp_t(int, input, 0, 1);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100943
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100944 if (global.no_turbo) {
945 struct cpudata *cpu = all_cpu_data[0];
946 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
947
948 /* Squash the global minimum into the permitted range. */
949 if (global.min_perf_pct > pct)
950 global.min_perf_pct = pct;
951 }
952
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +0100953 mutex_unlock(&intel_pstate_limits_lock);
954
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100955 intel_pstate_update_policies();
956
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100957 mutex_unlock(&intel_pstate_driver_lock);
958
Dirk Brandewie93f08222013-02-06 09:02:13 -0800959 return count;
960}
961
962static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700963 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800964{
965 unsigned int input;
966 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700967
Dirk Brandewie93f08222013-02-06 09:02:13 -0800968 ret = sscanf(buf, "%u", &input);
969 if (ret != 1)
970 return -EINVAL;
971
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100972 mutex_lock(&intel_pstate_driver_lock);
973
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200974 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100975 mutex_unlock(&intel_pstate_driver_lock);
976 return -EAGAIN;
977 }
978
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700979 mutex_lock(&intel_pstate_limits_lock);
980
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100981 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100982
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +0100983 mutex_unlock(&intel_pstate_limits_lock);
984
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100985 intel_pstate_update_policies();
986
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100987 mutex_unlock(&intel_pstate_driver_lock);
988
Dirk Brandewie93f08222013-02-06 09:02:13 -0800989 return count;
990}
991
992static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700993 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800994{
995 unsigned int input;
996 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700997
Dirk Brandewie93f08222013-02-06 09:02:13 -0800998 ret = sscanf(buf, "%u", &input);
999 if (ret != 1)
1000 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001001
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001002 mutex_lock(&intel_pstate_driver_lock);
1003
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02001004 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001005 mutex_unlock(&intel_pstate_driver_lock);
1006 return -EAGAIN;
1007 }
1008
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001009 mutex_lock(&intel_pstate_limits_lock);
1010
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001011 global.min_perf_pct = clamp_t(int, input,
1012 min_perf_pct_min(), global.max_perf_pct);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001013
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +01001014 mutex_unlock(&intel_pstate_limits_lock);
1015
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001016 intel_pstate_update_policies();
1017
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001018 mutex_unlock(&intel_pstate_driver_lock);
1019
Dirk Brandewie93f08222013-02-06 09:02:13 -08001020 return count;
1021}
1022
Dirk Brandewie93f08222013-02-06 09:02:13 -08001023show_one(max_perf_pct, max_perf_pct);
1024show_one(min_perf_pct, min_perf_pct);
1025
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001026define_one_global_rw(status);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001027define_one_global_rw(no_turbo);
1028define_one_global_rw(max_perf_pct);
1029define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001030define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001031define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001032
1033static struct attribute *intel_pstate_attributes[] = {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001034 &status.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001035 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001036 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001037 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001038 NULL
1039};
1040
Arvind Yadav106c9c72017-07-03 13:40:33 +05301041static const struct attribute_group intel_pstate_attr_group = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08001042 .attrs = intel_pstate_attributes,
1043};
Dirk Brandewie93f08222013-02-06 09:02:13 -08001044
Stratos Karafotis317dd502014-07-18 08:37:17 -07001045static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001046{
Stratos Karafotis317dd502014-07-18 08:37:17 -07001047 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001048 int rc;
1049
1050 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1051 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001052 if (WARN_ON(!intel_pstate_kobject))
1053 return;
1054
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -07001055 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001056 if (WARN_ON(rc))
1057 return;
1058
1059 /*
1060 * If per cpu limits are enforced there are no global limits, so
1061 * return without creating max/min_perf_pct attributes
1062 */
1063 if (per_cpu_limits)
1064 return;
1065
1066 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1067 WARN_ON(rc);
1068
1069 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1070 WARN_ON(rc);
1071
Dirk Brandewie93f08222013-02-06 09:02:13 -08001072}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001073/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001074
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001075static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001076{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001077 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -07001078 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1079 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001080
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001081 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001082 cpudata->epp_policy = 0;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001083 if (cpudata->epp_default == -EINVAL)
1084 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001085}
1086
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001087#define MSR_IA32_POWER_CTL_BIT_EE 19
1088
1089/* Disable energy efficiency optimization */
1090static void intel_pstate_disable_ee(int cpu)
1091{
1092 u64 power_ctl;
1093 int ret;
1094
1095 ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1096 if (ret)
1097 return;
1098
1099 if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1100 pr_info("Disabling energy efficiency optimization\n");
1101 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1102 wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1103 }
1104}
1105
Philippe Longepe938d21a2015-11-09 17:40:46 -08001106static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001107{
1108 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001109
Len Brown92134bd2017-02-25 16:55:17 -05001110 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001111 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001112}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001113
Philippe Longepe938d21a2015-11-09 17:40:46 -08001114static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001115{
1116 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001117
Len Brown92134bd2017-02-25 16:55:17 -05001118 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001119 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001120}
1121
Philippe Longepe938d21a2015-11-09 17:40:46 -08001122static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001123{
1124 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001125
Len Brown92134bd2017-02-25 16:55:17 -05001126 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001127 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001128}
1129
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001130static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001131{
1132 u64 val;
1133 int32_t vid_fp;
1134 u32 vid;
1135
Chen Yu144c8e12015-07-29 23:53:10 +08001136 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001137 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001138 val |= (u64)1 << 32;
1139
1140 vid_fp = cpudata->vid.min + mul_fp(
1141 int_tofp(pstate - cpudata->pstate.min_pstate),
1142 cpudata->vid.ratio);
1143
1144 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -07001145 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001146
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001147 if (pstate > cpudata->pstate.max_pstate)
1148 vid = cpudata->vid.turbo;
1149
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001150 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001151}
1152
Philippe Longepe1421df62015-11-09 17:40:47 -08001153static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001154{
1155 u64 value;
1156 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -08001157 /* Defined in Table 35-6 from SDM (Sept 2015) */
1158 static int silvermont_freq_table[] = {
1159 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001160
1161 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -08001162 i = value & 0x7;
1163 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001164
Philippe Longepe1421df62015-11-09 17:40:47 -08001165 return silvermont_freq_table[i];
1166}
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001167
Philippe Longepe1421df62015-11-09 17:40:47 -08001168static int airmont_get_scaling(void)
1169{
1170 u64 value;
1171 int i;
1172 /* Defined in Table 35-10 from SDM (Sept 2015) */
1173 static int airmont_freq_table[] = {
1174 83300, 100000, 133300, 116700, 80000,
1175 93300, 90000, 88900, 87500};
1176
1177 rdmsrl(MSR_FSB_FREQ, value);
1178 i = value & 0xF;
1179 WARN_ON(i > 8);
1180
1181 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001182}
1183
Philippe Longepe938d21a2015-11-09 17:40:46 -08001184static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001185{
1186 u64 value;
1187
Len Brown92134bd2017-02-25 16:55:17 -05001188 rdmsrl(MSR_ATOM_CORE_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001189 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1190 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001191 cpudata->vid.ratio = div_fp(
1192 cpudata->vid.max - cpudata->vid.min,
1193 int_tofp(cpudata->pstate.max_pstate -
1194 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001195
Len Brown92134bd2017-02-25 16:55:17 -05001196 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001197 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001198}
1199
Dirk Brandewie016c8152013-10-21 09:20:34 -07001200static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001201{
1202 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001203
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001204 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001205 return (value >> 40) & 0xFF;
1206}
1207
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001208static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001209{
1210 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001211
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001212 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001213 return (value >> 8) & 0xFF;
1214}
1215
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001216static int core_get_tdp_ratio(u64 plat_info)
1217{
1218 /* Check how many TDP levels present */
1219 if (plat_info & 0x600000000) {
1220 u64 tdp_ctrl;
1221 u64 tdp_ratio;
1222 int tdp_msr;
1223 int err;
1224
1225 /* Get the TDP level (0, 1, 2) to get ratios */
1226 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1227 if (err)
1228 return err;
1229
1230 /* TDP MSR are continuous starting at 0x648 */
1231 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1232 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1233 if (err)
1234 return err;
1235
1236 /* For level 1 and 2, bits[23:16] contain the ratio */
1237 if (tdp_ctrl & 0x03)
1238 tdp_ratio >>= 16;
1239
1240 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1241 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1242
1243 return (int)tdp_ratio;
1244 }
1245
1246 return -ENXIO;
1247}
1248
Dirk Brandewie93f08222013-02-06 09:02:13 -08001249static int core_get_max_pstate(void)
1250{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001251 u64 tar;
1252 u64 plat_info;
1253 int max_pstate;
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001254 int tdp_ratio;
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001255 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001256
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001257 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1258 max_pstate = (plat_info >> 8) & 0xFF;
1259
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001260 tdp_ratio = core_get_tdp_ratio(plat_info);
1261 if (tdp_ratio <= 0)
1262 return max_pstate;
1263
1264 if (hwp_active) {
1265 /* Turbo activation ratio is not used on HWP platforms */
1266 return tdp_ratio;
1267 }
1268
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001269 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1270 if (!err) {
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001271 int tar_levels;
1272
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001273 /* Do some sanity checking for safety */
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001274 tar_levels = tar & 0xff;
1275 if (tdp_ratio - 1 == tar_levels) {
1276 max_pstate = tar_levels;
1277 pr_debug("max_pstate=TAC %x\n", max_pstate);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001278 }
1279 }
1280
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001281 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001282}
1283
Dirk Brandewie016c8152013-10-21 09:20:34 -07001284static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001285{
1286 u64 value;
1287 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001288
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001289 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001290 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001291 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001292 if (ret <= nont)
1293 ret = nont;
1294 return ret;
1295}
1296
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001297static inline int core_get_scaling(void)
1298{
1299 return 100000;
1300}
1301
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001302static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001303{
1304 u64 val;
1305
Chen Yu144c8e12015-07-29 23:53:10 +08001306 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001307 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001308 val |= (u64)1 << 32;
1309
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001310 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001311}
1312
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001313static int knl_get_aperf_mperf_shift(void)
1314{
1315 return 10;
1316}
1317
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001318static int knl_get_turbo_pstate(void)
1319{
1320 u64 value;
1321 int nont, ret;
1322
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001323 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001324 nont = core_get_max_pstate();
1325 ret = (((value) >> 8) & 0xFF);
1326 if (ret <= nont)
1327 ret = nont;
1328 return ret;
1329}
1330
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001331static int intel_pstate_get_base_pstate(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001332{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001333 return global.no_turbo || global.turbo_disabled ?
1334 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001335}
1336
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001337static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001338{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001339 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1340 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001341 /*
1342 * Generally, there is no guarantee that this code will always run on
1343 * the CPU being updated, so force the register update to run on the
1344 * right CPU.
1345 */
1346 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1347 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001348}
1349
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001350static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1351{
1352 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1353}
1354
1355static void intel_pstate_max_within_limits(struct cpudata *cpu)
1356{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001357 int pstate;
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001358
1359 update_turbo_state();
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001360 pstate = intel_pstate_get_base_pstate(cpu);
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001361 pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001362 intel_pstate_set_pstate(cpu, pstate);
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001363}
1364
Dirk Brandewie93f08222013-02-06 09:02:13 -08001365static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1366{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001367 cpu->pstate.min_pstate = pstate_funcs.get_min();
1368 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001369 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001370 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001371 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001372 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1373 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001374
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001375 if (pstate_funcs.get_aperf_mperf_shift)
1376 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
1377
Dirk Brandewie007bea02013-12-18 10:32:39 -08001378 if (pstate_funcs.get_vid)
1379 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001380
1381 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001382}
1383
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001384static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001385{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001386 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001387
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001388 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001389}
1390
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001391static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001392{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001393 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001394 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001395 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001396
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001397 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001398 rdmsrl(MSR_IA32_APERF, aperf);
1399 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001400 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001401 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001402 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001403 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001404 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001405 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001406
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001407 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001408 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001409 cpu->sample.aperf = aperf;
1410 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001411 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001412 cpu->sample.aperf -= cpu->prev_aperf;
1413 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001414 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001415
Dirk Brandewie93f08222013-02-06 09:02:13 -08001416 cpu->prev_aperf = aperf;
1417 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001418 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001419 /*
1420 * First time this function is invoked in a given cycle, all of the
1421 * previous sample data fields are equal to zero or stale and they must
1422 * be populated with meaningful numbers for things to work, so assume
1423 * that sample.time will always be reset before setting the utilization
1424 * update hook and make the caller skip the sample then.
1425 */
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001426 if (cpu->last_sample_time) {
1427 intel_pstate_calc_avg_perf(cpu);
1428 return true;
1429 }
1430 return false;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001431}
1432
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001433static inline int32_t get_avg_frequency(struct cpudata *cpu)
1434{
Doug Smythiesc587c792017-08-08 14:05:12 -07001435 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001436}
1437
Philippe Longepebdcaa232016-04-22 11:46:09 -07001438static inline int32_t get_avg_pstate(struct cpudata *cpu)
1439{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001440 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1441 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001442}
1443
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001444static inline int32_t get_target_pstate(struct cpudata *cpu)
Philippe Longepee70eed22015-12-04 17:40:32 +01001445{
1446 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001447 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001448 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001449
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001450 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
1451 sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001452
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001453 boost = cpu->iowait_boost;
1454 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001455
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001456 if (busy_frac < boost)
1457 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001458
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001459 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001460
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001461 target = global.no_turbo || global.turbo_disabled ?
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001462 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1463 target += target >> 2;
1464 target = mul_fp(target, busy_frac);
1465 if (target < cpu->pstate.min_pstate)
1466 target = cpu->pstate.min_pstate;
1467
1468 /*
1469 * If the average P-state during the previous cycle was higher than the
1470 * current target, add 50% of the difference to the target to reduce
1471 * possible performance oscillations and offset possible performance
1472 * loss related to moving the workload from one CPU to another within
1473 * a package/module.
1474 */
1475 avg_pstate = get_avg_pstate(cpu);
1476 if (avg_pstate > target)
1477 target += (avg_pstate - target) >> 1;
1478
1479 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001480}
1481
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001482static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001483{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001484 int max_pstate = intel_pstate_get_base_pstate(cpu);
1485 int min_pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001486
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001487 min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1488 max_pstate = max(min_pstate, cpu->max_perf_ratio);
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001489 return clamp_t(int, pstate, min_pstate, max_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001490}
1491
1492static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1493{
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001494 if (pstate == cpu->pstate.current_pstate)
1495 return;
1496
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001497 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001498 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1499}
1500
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001501static void intel_pstate_adjust_pstate(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001502{
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001503 int from = cpu->pstate.current_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001504 struct sample *sample;
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001505 int target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001506
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001507 update_turbo_state();
1508
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001509 target_pstate = get_target_pstate(cpu);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001510 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1511 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001512 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001513
1514 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001515 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001516 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001517 from,
1518 cpu->pstate.current_pstate,
1519 sample->mperf,
1520 sample->aperf,
1521 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001522 get_avg_frequency(cpu),
1523 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001524}
1525
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001526static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001527 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001528{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001529 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001530 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001531
Viresh Kumar674e7542017-07-28 12:16:38 +05301532 /* Don't allow remote callbacks */
1533 if (smp_processor_id() != cpu->cpu)
1534 return;
1535
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001536 if (flags & SCHED_CPUFREQ_IOWAIT) {
1537 cpu->iowait_boost = int_tofp(1);
Srinivas Pandruvada7bde2d52017-08-03 19:03:14 -07001538 cpu->last_update = time;
1539 /*
1540 * The last time the busy was 100% so P-state was max anyway
1541 * so avoid overhead of computation.
1542 */
1543 if (fp_toint(cpu->sample.busy_scaled) == 100)
1544 return;
1545
1546 goto set_pstate;
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001547 } else if (cpu->iowait_boost) {
1548 /* Clear iowait_boost if the CPU may have been idle. */
1549 delta_ns = time - cpu->last_update;
1550 if (delta_ns > TICK_NSEC)
1551 cpu->iowait_boost = 0;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001552 }
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001553 cpu->last_update = time;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001554 delta_ns = time - cpu->sample.time;
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001555 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001556 return;
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001557
Srinivas Pandruvada7bde2d52017-08-03 19:03:14 -07001558set_pstate:
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001559 if (intel_pstate_sample(cpu, time))
1560 intel_pstate_adjust_pstate(cpu);
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001561}
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001562
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001563static struct pstate_funcs core_funcs = {
1564 .get_max = core_get_max_pstate,
1565 .get_max_physical = core_get_max_pstate_physical,
1566 .get_min = core_get_min_pstate,
1567 .get_turbo = core_get_turbo_pstate,
1568 .get_scaling = core_get_scaling,
1569 .get_val = core_get_val,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001570};
1571
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001572static const struct pstate_funcs silvermont_funcs = {
1573 .get_max = atom_get_max_pstate,
1574 .get_max_physical = atom_get_max_pstate,
1575 .get_min = atom_get_min_pstate,
1576 .get_turbo = atom_get_turbo_pstate,
1577 .get_val = atom_get_val,
1578 .get_scaling = silvermont_get_scaling,
1579 .get_vid = atom_get_vid,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001580};
1581
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001582static const struct pstate_funcs airmont_funcs = {
1583 .get_max = atom_get_max_pstate,
1584 .get_max_physical = atom_get_max_pstate,
1585 .get_min = atom_get_min_pstate,
1586 .get_turbo = atom_get_turbo_pstate,
1587 .get_val = atom_get_val,
1588 .get_scaling = airmont_get_scaling,
1589 .get_vid = atom_get_vid,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001590};
1591
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001592static const struct pstate_funcs knl_funcs = {
1593 .get_max = core_get_max_pstate,
1594 .get_max_physical = core_get_max_pstate_physical,
1595 .get_min = core_get_min_pstate,
1596 .get_turbo = knl_get_turbo_pstate,
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001597 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001598 .get_scaling = core_get_scaling,
1599 .get_val = core_get_val,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001600};
1601
Dirk Brandewie93f08222013-02-06 09:02:13 -08001602#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001603 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1604 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001605
1606static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001607 ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs),
1608 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs),
1609 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_funcs),
1610 ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs),
1611 ICPU(INTEL_FAM6_HASWELL_CORE, core_funcs),
1612 ICPU(INTEL_FAM6_BROADWELL_CORE, core_funcs),
1613 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs),
1614 ICPU(INTEL_FAM6_HASWELL_X, core_funcs),
1615 ICPU(INTEL_FAM6_HASWELL_ULT, core_funcs),
1616 ICPU(INTEL_FAM6_HASWELL_GT3E, core_funcs),
1617 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_funcs),
1618 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs),
1619 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_funcs),
1620 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1621 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs),
1622 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1623 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs),
1624 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs),
Srinivas Pandruvadadbd49b82018-01-10 11:38:51 -08001625 ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs),
1626 ICPU(INTEL_FAM6_ATOM_GEMINI_LAKE, core_funcs),
Srinivas Pandruvadad8de7a42018-01-10 11:38:52 -08001627 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001628 {}
1629};
1630MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1631
Jisheng Zhang29327c82016-06-27 18:07:17 +08001632static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001633 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1634 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1635 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001636 {}
1637};
1638
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001639static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001640 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs),
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001641 {}
1642};
1643
Dirk Brandewie93f08222013-02-06 09:02:13 -08001644static int intel_pstate_init_cpu(unsigned int cpunum)
1645{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001646 struct cpudata *cpu;
1647
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001648 cpu = all_cpu_data[cpunum];
1649
1650 if (!cpu) {
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001651 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001652 if (!cpu)
1653 return -ENOMEM;
1654
1655 all_cpu_data[cpunum] = cpu;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001656
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001657 cpu->epp_default = -EINVAL;
1658 cpu->epp_powersave = -EINVAL;
1659 cpu->epp_saved = -EINVAL;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001660 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001661
1662 cpu = all_cpu_data[cpunum];
1663
Dirk Brandewie93f08222013-02-06 09:02:13 -08001664 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001665
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001666 if (hwp_active) {
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001667 const struct x86_cpu_id *id;
1668
1669 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1670 if (id)
1671 intel_pstate_disable_ee(cpunum);
1672
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001673 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001674 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001675
Vincent Minet179e8472014-07-05 01:51:33 +02001676 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001677
Joe Perches4836df12016-04-05 13:28:23 -07001678 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001679
1680 return 0;
1681}
1682
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001683static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001684{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001685 struct cpudata *cpu = all_cpu_data[cpu_num];
1686
Len Brown62611cb2017-06-23 22:11:53 -07001687 if (hwp_active)
1688 return;
1689
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001690 if (cpu->update_util_set)
1691 return;
1692
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001693 /* Prevent intel_pstate_update_util() from using stale data. */
1694 cpu->sample.time = 0;
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001695 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
Rafael J. Wysockic4f3f702017-07-25 00:12:20 +02001696 intel_pstate_update_util);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001697 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001698}
1699
1700static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1701{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001702 struct cpudata *cpu_data = all_cpu_data[cpu];
1703
1704 if (!cpu_data->update_util_set)
1705 return;
1706
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001707 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001708 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001709 synchronize_sched();
1710}
1711
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001712static int intel_pstate_get_max_freq(struct cpudata *cpu)
1713{
1714 return global.turbo_disabled || global.no_turbo ?
1715 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1716}
1717
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001718static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001719 struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001720{
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001721 int max_freq = intel_pstate_get_max_freq(cpu);
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001722 int32_t max_policy_perf, min_policy_perf;
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001723 int max_state, turbo_max;
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001724
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001725 /*
1726 * HWP needs some special consideration, because on BDX the
1727 * HWP_REQUEST uses abstract value to represent performance
1728 * rather than pure ratios.
1729 */
1730 if (hwp_active) {
1731 intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
1732 } else {
1733 max_state = intel_pstate_get_base_pstate(cpu);
1734 turbo_max = cpu->pstate.turbo_pstate;
1735 }
1736
1737 max_policy_perf = max_state * policy->max / max_freq;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001738 if (policy->max == policy->min) {
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001739 min_policy_perf = max_policy_perf;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001740 } else {
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001741 min_policy_perf = max_state * policy->min / max_freq;
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001742 min_policy_perf = clamp_t(int32_t, min_policy_perf,
1743 0, max_policy_perf);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001744 }
Chen Yu43717aa2015-09-09 18:27:31 +08001745
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001746 pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
1747 policy->cpu, max_state,
1748 min_policy_perf, max_policy_perf);
1749
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001750 /* Normalize user input to [min_perf, max_perf] */
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001751 if (per_cpu_limits) {
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001752 cpu->min_perf_ratio = min_policy_perf;
1753 cpu->max_perf_ratio = max_policy_perf;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001754 } else {
1755 int32_t global_min, global_max;
Chen Yu43717aa2015-09-09 18:27:31 +08001756
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001757 /* Global limits are in percent of the maximum turbo P-state. */
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001758 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
1759 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001760 global_min = clamp_t(int32_t, global_min, 0, global_max);
1761
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001762 pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
1763 global_min, global_max);
1764
1765 cpu->min_perf_ratio = max(min_policy_perf, global_min);
1766 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
1767 cpu->max_perf_ratio = min(max_policy_perf, global_max);
1768 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001769
1770 /* Make sure min_perf <= max_perf */
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001771 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
1772 cpu->max_perf_ratio);
1773
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001774 }
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001775 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
1776 cpu->max_perf_ratio,
1777 cpu->min_perf_ratio);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001778}
1779
Dirk Brandewie93f08222013-02-06 09:02:13 -08001780static int intel_pstate_set_policy(struct cpufreq_policy *policy)
Pali Rohár36b4bed2014-10-16 01:16:51 +02001781{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001782 struct cpudata *cpu;
1783
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001784 if (!policy->cpuinfo.max_freq)
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001785 return -ENODEV;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001786
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001787 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001788 policy->cpuinfo.max_freq, policy->max);
1789
1790 cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001791 cpu->policy = policy->policy;
1792
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001793 mutex_lock(&intel_pstate_limits_lock);
1794
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001795 intel_pstate_update_perf_limits(policy, cpu);
Rafael J. Wysockia240c4a2017-03-02 23:29:12 +01001796
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001797 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001798 /*
1799 * NOHZ_FULL CPUs need this as the governor callback may not
1800 * be invoked on them.
1801 */
1802 intel_pstate_clear_update_util_hook(policy->cpu);
1803 intel_pstate_max_within_limits(cpu);
Len Brown82b4e032017-06-23 22:11:54 -07001804 } else {
1805 intel_pstate_set_update_util_hook(policy->cpu);
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001806 }
1807
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +01001808 if (hwp_active)
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +02001809 intel_pstate_hwp_set(policy->cpu);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001810
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001811 mutex_unlock(&intel_pstate_limits_lock);
1812
Dirk Brandewie93f08222013-02-06 09:02:13 -08001813 return 0;
1814}
1815
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001816static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
1817 struct cpudata *cpu)
1818{
1819 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
1820 policy->max < policy->cpuinfo.max_freq &&
1821 policy->max > cpu->pstate.max_freq) {
1822 pr_debug("policy->max > max non turbo frequency\n");
1823 policy->max = policy->cpuinfo.max_freq;
1824 }
1825}
1826
Dirk Brandewie93f08222013-02-06 09:02:13 -08001827static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1828{
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08001829 struct cpudata *cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08001830
1831 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001832 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
1833 intel_pstate_get_max_freq(cpu));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001834
Stratos Karafotis285cb992014-07-18 08:37:21 -07001835 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001836 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001837 return -EINVAL;
1838
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001839 intel_pstate_adjust_policy_max(policy, cpu);
1840
Dirk Brandewie93f08222013-02-06 09:02:13 -08001841 return 0;
1842}
1843
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001844static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001845{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001846 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001847}
1848
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001849static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
1850{
1851 pr_debug("CPU %d exiting\n", policy->cpu);
1852
1853 intel_pstate_clear_update_util_hook(policy->cpu);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001854 if (hwp_active)
1855 intel_pstate_hwp_save_state(policy);
1856 else
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001857 intel_cpufreq_stop_cpu(policy);
1858}
1859
1860static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1861{
1862 intel_pstate_exit_perf_limits(policy);
1863
1864 policy->fast_switch_possible = false;
1865
1866 return 0;
1867}
1868
1869static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001870{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001871 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07001872 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001873
1874 rc = intel_pstate_init_cpu(policy->cpu);
1875 if (rc)
1876 return rc;
1877
1878 cpu = all_cpu_data[policy->cpu];
1879
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001880 cpu->max_perf_ratio = 0xFF;
1881 cpu->min_perf_ratio = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001882
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001883 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1884 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001885
1886 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001887 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07001888 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001889 policy->cpuinfo.max_freq = global.turbo_disabled ?
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07001890 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1891 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
1892
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001893 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001894
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001895 policy->fast_switch_possible = true;
1896
Dirk Brandewie93f08222013-02-06 09:02:13 -08001897 return 0;
1898}
1899
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001900static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001901{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001902 int ret = __intel_pstate_cpu_init(policy);
1903
1904 if (ret)
1905 return ret;
1906
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001907 if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001908 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1909 else
1910 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001911
1912 return 0;
1913}
1914
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001915static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08001916 .flags = CPUFREQ_CONST_LOOPS,
1917 .verify = intel_pstate_verify_policy,
1918 .setpolicy = intel_pstate_set_policy,
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001919 .suspend = intel_pstate_hwp_save_state,
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001920 .resume = intel_pstate_resume,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001921 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001922 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07001923 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001924 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08001925};
1926
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001927static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
1928{
1929 struct cpudata *cpu = all_cpu_data[policy->cpu];
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001930
1931 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001932 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
1933 intel_pstate_get_max_freq(cpu));
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001934
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001935 intel_pstate_adjust_policy_max(policy, cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001936
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001937 intel_pstate_update_perf_limits(policy, cpu);
1938
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001939 return 0;
1940}
1941
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001942static int intel_cpufreq_target(struct cpufreq_policy *policy,
1943 unsigned int target_freq,
1944 unsigned int relation)
1945{
1946 struct cpudata *cpu = all_cpu_data[policy->cpu];
1947 struct cpufreq_freqs freqs;
1948 int target_pstate;
1949
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01001950 update_turbo_state();
1951
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001952 freqs.old = policy->cur;
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01001953 freqs.new = target_freq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001954
1955 cpufreq_freq_transition_begin(policy, &freqs);
1956 switch (relation) {
1957 case CPUFREQ_RELATION_L:
1958 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
1959 break;
1960 case CPUFREQ_RELATION_H:
1961 target_pstate = freqs.new / cpu->pstate.scaling;
1962 break;
1963 default:
1964 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
1965 break;
1966 }
1967 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1968 if (target_pstate != cpu->pstate.current_pstate) {
1969 cpu->pstate.current_pstate = target_pstate;
1970 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
1971 pstate_funcs.get_val(cpu, target_pstate));
1972 }
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001973 freqs.new = target_pstate * cpu->pstate.scaling;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001974 cpufreq_freq_transition_end(policy, &freqs, false);
1975
1976 return 0;
1977}
1978
1979static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
1980 unsigned int target_freq)
1981{
1982 struct cpudata *cpu = all_cpu_data[policy->cpu];
1983 int target_pstate;
1984
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01001985 update_turbo_state();
1986
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001987 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001988 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001989 intel_pstate_update_pstate(cpu, target_pstate);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001990 return target_pstate * cpu->pstate.scaling;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001991}
1992
1993static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
1994{
1995 int ret = __intel_pstate_cpu_init(policy);
1996
1997 if (ret)
1998 return ret;
1999
2000 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
Rafael J. Wysocki1b72e7f2017-04-11 00:20:41 +02002001 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002002 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2003 policy->cur = policy->cpuinfo.min_freq;
2004
2005 return 0;
2006}
2007
2008static struct cpufreq_driver intel_cpufreq = {
2009 .flags = CPUFREQ_CONST_LOOPS,
2010 .verify = intel_cpufreq_verify_policy,
2011 .target = intel_cpufreq_target,
2012 .fast_switch = intel_cpufreq_fast_switch,
2013 .init = intel_cpufreq_cpu_init,
2014 .exit = intel_pstate_cpu_exit,
2015 .stop_cpu = intel_cpufreq_stop_cpu,
2016 .name = "intel_cpufreq",
2017};
2018
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002019static struct cpufreq_driver *default_driver = &intel_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002020
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002021static void intel_pstate_driver_cleanup(void)
2022{
2023 unsigned int cpu;
2024
2025 get_online_cpus();
2026 for_each_online_cpu(cpu) {
2027 if (all_cpu_data[cpu]) {
2028 if (intel_pstate_driver == &intel_pstate)
2029 intel_pstate_clear_update_util_hook(cpu);
2030
2031 kfree(all_cpu_data[cpu]);
2032 all_cpu_data[cpu] = NULL;
2033 }
2034 }
2035 put_online_cpus();
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002036 intel_pstate_driver = NULL;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002037}
2038
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002039static int intel_pstate_register_driver(struct cpufreq_driver *driver)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002040{
2041 int ret;
2042
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002043 memset(&global, 0, sizeof(global));
2044 global.max_perf_pct = 100;
Rafael J. Wysockic3a49c82017-02-28 00:05:01 +01002045
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002046 intel_pstate_driver = driver;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002047 ret = cpufreq_register_driver(intel_pstate_driver);
2048 if (ret) {
2049 intel_pstate_driver_cleanup();
2050 return ret;
2051 }
2052
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002053 global.min_perf_pct = min_perf_pct_min();
2054
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002055 return 0;
2056}
2057
2058static int intel_pstate_unregister_driver(void)
2059{
2060 if (hwp_active)
2061 return -EBUSY;
2062
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002063 cpufreq_unregister_driver(intel_pstate_driver);
2064 intel_pstate_driver_cleanup();
2065
2066 return 0;
2067}
2068
2069static ssize_t intel_pstate_show_status(char *buf)
2070{
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002071 if (!intel_pstate_driver)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002072 return sprintf(buf, "off\n");
2073
2074 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2075 "active" : "passive");
2076}
2077
2078static int intel_pstate_update_status(const char *buf, size_t size)
2079{
2080 int ret;
2081
2082 if (size == 3 && !strncmp(buf, "off", size))
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002083 return intel_pstate_driver ?
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002084 intel_pstate_unregister_driver() : -EINVAL;
2085
2086 if (size == 6 && !strncmp(buf, "active", size)) {
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002087 if (intel_pstate_driver) {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002088 if (intel_pstate_driver == &intel_pstate)
2089 return 0;
2090
2091 ret = intel_pstate_unregister_driver();
2092 if (ret)
2093 return ret;
2094 }
2095
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002096 return intel_pstate_register_driver(&intel_pstate);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002097 }
2098
2099 if (size == 7 && !strncmp(buf, "passive", size)) {
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002100 if (intel_pstate_driver) {
Rafael J. Wysocki0042b2c2017-03-28 00:14:08 +02002101 if (intel_pstate_driver == &intel_cpufreq)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002102 return 0;
2103
2104 ret = intel_pstate_unregister_driver();
2105 if (ret)
2106 return ret;
2107 }
2108
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002109 return intel_pstate_register_driver(&intel_cpufreq);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002110 }
2111
2112 return -EINVAL;
2113}
2114
Jisheng Zhangeed43602016-06-27 18:07:16 +08002115static int no_load __initdata;
2116static int no_hwp __initdata;
2117static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08002118static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01002119
Jisheng Zhang29327c82016-06-27 18:07:17 +08002120static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002121{
Dirk Brandewie016c8152013-10-21 09:20:34 -07002122 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07002123 !pstate_funcs.get_min() ||
2124 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002125 return -ENODEV;
2126
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002127 return 0;
2128}
Dirk Brandewie016c8152013-10-21 09:20:34 -07002129
Jisheng Zhang29327c82016-06-27 18:07:17 +08002130static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002131{
2132 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07002133 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002134 pstate_funcs.get_min = funcs->get_min;
2135 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002136 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01002137 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08002138 pstate_funcs.get_vid = funcs->get_vid;
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07002139 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002140}
2141
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002142#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002143
Jisheng Zhang29327c82016-06-27 18:07:17 +08002144static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002145{
2146 int i;
2147
2148 for_each_possible_cpu(i) {
2149 acpi_status status;
2150 union acpi_object *pss;
2151 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2152 struct acpi_processor *pr = per_cpu(processors, i);
2153
2154 if (!pr)
2155 continue;
2156
2157 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2158 if (ACPI_FAILURE(status))
2159 continue;
2160
2161 pss = buffer.pointer;
2162 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2163 kfree(pss);
2164 return false;
2165 }
2166
2167 kfree(pss);
2168 }
2169
2170 return true;
2171}
2172
Jisheng Zhang29327c82016-06-27 18:07:17 +08002173static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09002174{
2175 int i;
2176
2177 for_each_possible_cpu(i) {
2178 struct acpi_processor *pr = per_cpu(processors, i);
2179
2180 if (!pr)
2181 continue;
2182 if (acpi_has_method(pr->handle, "_PPC"))
2183 return true;
2184 }
2185 return false;
2186}
2187
2188enum {
2189 PSS,
2190 PPC,
2191};
2192
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002193/* Hardware vendor-specific info that has its own power management modes */
Toshi Kani5e932322017-08-23 16:54:44 -06002194static struct acpi_platform_list plat_info[] __initdata = {
2195 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS},
2196 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2197 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2198 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2199 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2200 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2201 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2202 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2203 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2204 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2205 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2206 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2207 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2208 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2209 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2210 { } /* End */
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002211};
2212
Jisheng Zhang29327c82016-06-27 18:07:17 +08002213static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002214{
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002215 const struct x86_cpu_id *id;
2216 u64 misc_pwr;
Toshi Kani5e932322017-08-23 16:54:44 -06002217 int idx;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002218
2219 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2220 if (id) {
2221 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2222 if ( misc_pwr & (1 << 8))
2223 return true;
2224 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002225
Toshi Kani5e932322017-08-23 16:54:44 -06002226 idx = acpi_match_platform_list(plat_info);
2227 if (idx < 0)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002228 return false;
2229
Toshi Kani5e932322017-08-23 16:54:44 -06002230 switch (plat_info[idx].data) {
2231 case PSS:
2232 return intel_pstate_no_acpi_pss();
2233 case PPC:
2234 return intel_pstate_has_acpi_ppc() && !force_load;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002235 }
2236
2237 return false;
2238}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002239
2240static void intel_pstate_request_control_from_smm(void)
2241{
2242 /*
2243 * It may be unsafe to request P-states control from SMM if _PPC support
2244 * has not been enabled.
2245 */
2246 if (acpi_ppc)
2247 acpi_processor_pstate_control();
2248}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002249#else /* CONFIG_ACPI not enabled */
2250static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002251static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002252static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002253#endif /* CONFIG_ACPI */
2254
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002255static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2256 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2257 {}
2258};
2259
Dirk Brandewie93f08222013-02-06 09:02:13 -08002260static int __init intel_pstate_init(void)
2261{
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002262 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002263
Dirk Brandewie6be26492013-02-15 22:55:10 +01002264 if (no_load)
2265 return -ENODEV;
2266
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002267 if (x86_match_cpu(hwp_support_ids)) {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02002268 copy_cpu_funcs(&core_funcs);
Rafael J. Wysockic4f3f702017-07-25 00:12:20 +02002269 if (!no_hwp) {
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002270 hwp_active++;
2271 intel_pstate.attr = hwp_cpufreq_attrs;
2272 goto hwp_cpu_matched;
2273 }
2274 } else {
2275 const struct x86_cpu_id *id;
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002276
2277 id = x86_match_cpu(intel_pstate_cpu_ids);
2278 if (!id)
2279 return -ENODEV;
2280
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02002281 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002282 }
2283
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002284 if (intel_pstate_msrs_not_valid())
2285 return -ENODEV;
2286
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002287hwp_cpu_matched:
2288 /*
2289 * The Intel pstate driver will be ignored if the platform
2290 * firmware has its own power management modes.
2291 */
2292 if (intel_pstate_platform_pwr_mgmt_exists())
2293 return -ENODEV;
2294
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002295 if (!hwp_active && hwp_only)
2296 return -ENOTSUPP;
2297
Joe Perches4836df12016-04-05 13:28:23 -07002298 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002299
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002300 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002301 if (!all_cpu_data)
2302 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002303
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002304 intel_pstate_request_control_from_smm();
2305
Dirk Brandewie93f08222013-02-06 09:02:13 -08002306 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002307
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002308 mutex_lock(&intel_pstate_driver_lock);
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002309 rc = intel_pstate_register_driver(default_driver);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002310 mutex_unlock(&intel_pstate_driver_lock);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002311 if (rc)
2312 return rc;
Dirk Brandewie907cc902013-03-05 14:15:27 -08002313
Dirk Brandewie907cc902013-03-05 14:15:27 -08002314 if (hwp_active)
2315 pr_info("HWP enabled\n");
2316
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002317 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002318}
2319device_initcall(intel_pstate_init);
2320
Dirk Brandewie6be26492013-02-15 22:55:10 +01002321static int __init intel_pstate_setup(char *str)
2322{
2323 if (!str)
2324 return -EINVAL;
2325
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002326 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002327 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002328 } else if (!strcmp(str, "passive")) {
2329 pr_info("Passive mode enabled\n");
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002330 default_driver = &intel_cpufreq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002331 no_hwp = 1;
2332 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002333 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002334 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002335 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002336 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002337 if (!strcmp(str, "force"))
2338 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002339 if (!strcmp(str, "hwp_only"))
2340 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002341 if (!strcmp(str, "per_cpu_perf_limits"))
2342 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002343
2344#ifdef CONFIG_ACPI
2345 if (!strcmp(str, "support_acpi_ppc"))
2346 acpi_ppc = true;
2347#endif
2348
Dirk Brandewie6be26492013-02-15 22:55:10 +01002349 return 0;
2350}
2351early_param("intel_pstate", intel_pstate_setup);
2352
Dirk Brandewie93f08222013-02-06 09:02:13 -08002353MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2354MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2355MODULE_LICENSE("GPL");