blob: 5b2b6b6d1ff45c26228c1eedac592326657f83cb [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 Pandruvadae0efd5b2018-06-05 14:42:39 -0700224 * @hwp_req_cached: Cached value of the last HWP Request MSR
225 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
Srinivas Pandruvada52ccc4312018-06-05 14:42:40 -0700226 * @last_io_update: Last time when IO wake flag was set
227 * @sched_flags: Store scheduler flags for possible cross CPU update
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700228 * @hwp_boost_min: Last HWP boosted min performance
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700229 *
230 * This structure stores per CPU instance data for all CPUs.
231 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800232struct cpudata {
233 int cpu;
234
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200235 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100236 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800237 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800238
Dirk Brandewie93f08222013-02-06 09:02:13 -0800239 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800240 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800241
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200242 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100243 u64 last_sample_time;
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700244 u64 aperf_mperf_shift;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800245 u64 prev_aperf;
246 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700247 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100248 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800249 struct sample sample;
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700250 int32_t min_perf_ratio;
251 int32_t max_perf_ratio;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700252#ifdef CONFIG_ACPI
253 struct acpi_processor_performance acpi_perf_data;
254 bool valid_pss_table;
255#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200256 unsigned int iowait_boost;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800257 s16 epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800258 s16 epp_policy;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800259 s16 epp_default;
260 s16 epp_saved;
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700261 u64 hwp_req_cached;
262 u64 hwp_cap_cached;
Srinivas Pandruvada52ccc4312018-06-05 14:42:40 -0700263 u64 last_io_update;
264 unsigned int sched_flags;
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700265 u32 hwp_boost_min;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800266};
267
268static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700269
270/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700271 * struct pstate_funcs - Per CPU model specific callbacks
272 * @get_max: Callback to get maximum non turbo effective P state
273 * @get_max_physical: Callback to get maximum non turbo physical P state
274 * @get_min: Callback to get minimum P state
275 * @get_turbo: Callback to get turbo P state
276 * @get_scaling: Callback to get frequency scaling factor
277 * @get_val: Callback to convert P state to actual MSR write value
278 * @get_vid: Callback to get VID data for Atom platforms
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700279 *
280 * Core and Atom CPU models have different way to get P State limits. This
281 * structure is used to store those callbacks.
282 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700283struct pstate_funcs {
284 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700285 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700286 int (*get_min)(void);
287 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700288 int (*get_scaling)(void);
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700289 int (*get_aperf_mperf_shift)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100290 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800291 void (*get_vid)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800292};
293
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800294static struct pstate_funcs pstate_funcs __read_mostly;
Rafael J. Wysocki5c439052017-03-28 00:05:44 +0200295
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800296static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700297static bool per_cpu_limits __read_mostly;
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700298static bool hwp_boost __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700299
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200300static struct cpufreq_driver *intel_pstate_driver __read_mostly;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100301
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700302#ifdef CONFIG_ACPI
303static bool acpi_ppc;
304#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700305
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100306static struct global_params global;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800307
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100308static DEFINE_MUTEX(intel_pstate_driver_lock);
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700309static DEFINE_MUTEX(intel_pstate_limits_lock);
310
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700311#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700312
313static bool intel_pstate_get_ppc_enable_status(void)
314{
315 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
316 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
317 return true;
318
319 return acpi_ppc;
320}
321
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800322#ifdef CONFIG_ACPI_CPPC_LIB
323
324/* The work item is needed to avoid CPU hotplug locking issues */
325static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
326{
327 sched_set_itmt_support();
328}
329
330static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
331
332static void intel_pstate_set_itmt_prio(int cpu)
333{
334 struct cppc_perf_caps cppc_perf;
335 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
336 int ret;
337
338 ret = cppc_get_perf_caps(cpu, &cppc_perf);
339 if (ret)
340 return;
341
342 /*
343 * The priorities can be set regardless of whether or not
344 * sched_set_itmt_support(true) has been called and it is valid to
345 * update them at any time after it has been called.
346 */
347 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
348
349 if (max_highest_perf <= min_highest_perf) {
350 if (cppc_perf.highest_perf > max_highest_perf)
351 max_highest_perf = cppc_perf.highest_perf;
352
353 if (cppc_perf.highest_perf < min_highest_perf)
354 min_highest_perf = cppc_perf.highest_perf;
355
356 if (max_highest_perf > min_highest_perf) {
357 /*
358 * This code can be run during CPU online under the
359 * CPU hotplug locks, so sched_set_itmt_support()
360 * cannot be called from here. Queue up a work item
361 * to invoke it.
362 */
363 schedule_work(&sched_itmt_work);
364 }
365 }
366}
367#else
368static void intel_pstate_set_itmt_prio(int cpu)
369{
370}
371#endif
372
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700373static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
374{
375 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700376 int ret;
377 int i;
378
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800379 if (hwp_active) {
380 intel_pstate_set_itmt_prio(policy->cpu);
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700381 return;
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800382 }
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700383
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700384 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700385 return;
386
387 cpu = all_cpu_data[policy->cpu];
388
389 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
390 policy->cpu);
391 if (ret)
392 return;
393
394 /*
395 * Check if the control value in _PSS is for PERF_CTL MSR, which should
396 * guarantee that the states returned by it map to the states in our
397 * list directly.
398 */
399 if (cpu->acpi_perf_data.control_register.space_id !=
400 ACPI_ADR_SPACE_FIXED_HARDWARE)
401 goto err;
402
403 /*
404 * If there is only one entry _PSS, simply ignore _PSS and continue as
405 * usual without taking _PSS into account
406 */
407 if (cpu->acpi_perf_data.state_count < 2)
408 goto err;
409
410 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
411 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
412 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
413 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
414 (u32) cpu->acpi_perf_data.states[i].core_frequency,
415 (u32) cpu->acpi_perf_data.states[i].power,
416 (u32) cpu->acpi_perf_data.states[i].control);
417 }
418
419 /*
420 * The _PSS table doesn't contain whole turbo frequency range.
421 * This just contains +1 MHZ above the max non turbo frequency,
422 * with control value corresponding to max turbo ratio. But
423 * when cpufreq set policy is called, it will call with this
424 * max frequency, which will cause a reduced performance as
425 * this driver uses real max turbo frequency as the max
426 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700427 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700428 * Also need to convert to MHz as _PSS freq is in MHz.
429 */
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100430 if (!global.turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700431 cpu->acpi_perf_data.states[0].core_frequency =
432 policy->cpuinfo.max_freq / 1000;
433 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700434 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700435
436 return;
437
438 err:
439 cpu->valid_pss_table = false;
440 acpi_processor_unregister_performance(policy->cpu);
441}
442
443static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
444{
445 struct cpudata *cpu;
446
447 cpu = all_cpu_data[policy->cpu];
448 if (!cpu->valid_pss_table)
449 return;
450
451 acpi_processor_unregister_performance(policy->cpu);
452}
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700453#else
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100454static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700455{
456}
457
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100458static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700459{
460}
461#endif
462
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700463static inline void update_turbo_state(void)
464{
465 u64 misc_en;
466 struct cpudata *cpu;
467
468 cpu = all_cpu_data[0];
469 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100470 global.turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700471 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
472 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
473}
474
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100475static int min_perf_pct_min(void)
476{
477 struct cpudata *cpu = all_cpu_data[0];
Rafael J. Wysocki57caf4e2017-06-05 14:51:18 +0200478 int turbo_pstate = cpu->pstate.turbo_pstate;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100479
Rafael J. Wysocki57caf4e2017-06-05 14:51:18 +0200480 return turbo_pstate ?
Srinivas Pandruvadad4436c02017-07-10 16:23:52 -0700481 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100482}
483
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800484static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
485{
486 u64 epb;
487 int ret;
488
489 if (!static_cpu_has(X86_FEATURE_EPB))
490 return -ENXIO;
491
492 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
493 if (ret)
494 return (s16)ret;
495
496 return (s16)(epb & 0x0f);
497}
498
499static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
500{
501 s16 epp;
502
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800503 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
504 /*
505 * When hwp_req_data is 0, means that caller didn't read
506 * MSR_HWP_REQUEST, so need to read and get EPP.
507 */
508 if (!hwp_req_data) {
509 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
510 &hwp_req_data);
511 if (epp)
512 return epp;
513 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800514 epp = (hwp_req_data >> 24) & 0xff;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800515 } else {
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800516 /* When there is no EPP present, HWP uses EPB settings */
517 epp = intel_pstate_get_epb(cpu_data);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800518 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800519
520 return epp;
521}
522
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800523static int intel_pstate_set_epb(int cpu, s16 pref)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800524{
525 u64 epb;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800526 int ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800527
528 if (!static_cpu_has(X86_FEATURE_EPB))
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800529 return -ENXIO;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800530
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800531 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
532 if (ret)
533 return ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800534
535 epb = (epb & ~0x0f) | pref;
536 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800537
538 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800539}
540
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800541/*
542 * EPP/EPB display strings corresponding to EPP index in the
543 * energy_perf_strings[]
544 * index String
545 *-------------------------------------
546 * 0 default
547 * 1 performance
548 * 2 balance_performance
549 * 3 balance_power
550 * 4 power
551 */
552static const char * const energy_perf_strings[] = {
553 "default",
554 "performance",
555 "balance_performance",
556 "balance_power",
557 "power",
558 NULL
559};
Len Brown3cedbc52017-05-01 23:06:08 -0400560static const unsigned int epp_values[] = {
561 HWP_EPP_PERFORMANCE,
562 HWP_EPP_BALANCE_PERFORMANCE,
563 HWP_EPP_BALANCE_POWERSAVE,
564 HWP_EPP_POWERSAVE
565};
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800566
567static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
568{
569 s16 epp;
570 int index = -EINVAL;
571
572 epp = intel_pstate_get_epp(cpu_data, 0);
573 if (epp < 0)
574 return epp;
575
576 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
Len Brown3cedbc52017-05-01 23:06:08 -0400577 if (epp == HWP_EPP_PERFORMANCE)
578 return 1;
579 if (epp <= HWP_EPP_BALANCE_PERFORMANCE)
580 return 2;
581 if (epp <= HWP_EPP_BALANCE_POWERSAVE)
582 return 3;
583 else
584 return 4;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800585 } else if (static_cpu_has(X86_FEATURE_EPB)) {
586 /*
587 * Range:
588 * 0x00-0x03 : Performance
589 * 0x04-0x07 : Balance performance
590 * 0x08-0x0B : Balance power
591 * 0x0C-0x0F : Power
592 * The EPB is a 4 bit value, but our ranges restrict the
593 * value which can be set. Here only using top two bits
594 * effectively.
595 */
596 index = (epp >> 2) + 1;
597 }
598
599 return index;
600}
601
602static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
603 int pref_index)
604{
605 int epp = -EINVAL;
606 int ret;
607
608 if (!pref_index)
609 epp = cpu_data->epp_default;
610
611 mutex_lock(&intel_pstate_limits_lock);
612
613 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
614 u64 value;
615
616 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
617 if (ret)
618 goto return_pref;
619
620 value &= ~GENMASK_ULL(31, 24);
621
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800622 if (epp == -EINVAL)
Len Brown3cedbc52017-05-01 23:06:08 -0400623 epp = epp_values[pref_index - 1];
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800624
625 value |= (u64)epp << 24;
626 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
627 } else {
628 if (epp == -EINVAL)
629 epp = (pref_index - 1) << 2;
630 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
631 }
632return_pref:
633 mutex_unlock(&intel_pstate_limits_lock);
634
635 return ret;
636}
637
638static ssize_t show_energy_performance_available_preferences(
639 struct cpufreq_policy *policy, char *buf)
640{
641 int i = 0;
642 int ret = 0;
643
644 while (energy_perf_strings[i] != NULL)
645 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
646
647 ret += sprintf(&buf[ret], "\n");
648
649 return ret;
650}
651
652cpufreq_freq_attr_ro(energy_performance_available_preferences);
653
654static ssize_t store_energy_performance_preference(
655 struct cpufreq_policy *policy, const char *buf, size_t count)
656{
657 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
658 char str_preference[21];
659 int ret, i = 0;
660
661 ret = sscanf(buf, "%20s", str_preference);
662 if (ret != 1)
663 return -EINVAL;
664
665 while (energy_perf_strings[i] != NULL) {
666 if (!strcmp(str_preference, energy_perf_strings[i])) {
667 intel_pstate_set_energy_pref_index(cpu_data, i);
668 return count;
669 }
670 ++i;
671 }
672
673 return -EINVAL;
674}
675
676static ssize_t show_energy_performance_preference(
677 struct cpufreq_policy *policy, char *buf)
678{
679 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
680 int preference;
681
682 preference = intel_pstate_get_energy_pref_index(cpu_data);
683 if (preference < 0)
684 return preference;
685
686 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
687}
688
689cpufreq_freq_attr_rw(energy_performance_preference);
690
691static struct freq_attr *hwp_cpufreq_attrs[] = {
692 &energy_performance_preference,
693 &energy_performance_available_preferences,
694 NULL,
695};
696
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700697static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
698 int *current_max)
699{
700 u64 cap;
701
702 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700703 WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap);
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700704 if (global.no_turbo)
705 *current_max = HWP_GUARANTEED_PERF(cap);
706 else
707 *current_max = HWP_HIGHEST_PERF(cap);
708
709 *phy_max = HWP_HIGHEST_PERF(cap);
710}
711
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200712static void intel_pstate_hwp_set(unsigned int cpu)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800713{
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200714 struct cpudata *cpu_data = all_cpu_data[cpu];
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700715 int max, min;
716 u64 value;
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200717 s16 epp;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700718
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700719 max = cpu_data->max_perf_ratio;
720 min = cpu_data->min_perf_ratio;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700721
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200722 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
723 min = max;
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700724
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200725 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700726
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200727 value &= ~HWP_MIN_PERF(~0L);
728 value |= HWP_MIN_PERF(min);
Srinivas Pandruvada3f8ed542017-03-13 18:30:12 -0700729
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200730 value &= ~HWP_MAX_PERF(~0L);
731 value |= HWP_MAX_PERF(max);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800732
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200733 if (cpu_data->epp_policy == cpu_data->policy)
734 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800735
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200736 cpu_data->epp_policy = cpu_data->policy;
737
738 if (cpu_data->epp_saved >= 0) {
739 epp = cpu_data->epp_saved;
740 cpu_data->epp_saved = -EINVAL;
741 goto update_epp;
742 }
743
744 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
745 epp = intel_pstate_get_epp(cpu_data, value);
746 cpu_data->epp_powersave = epp;
747 /* If EPP read was failed, then don't try to write */
748 if (epp < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800749 goto skip_epp;
750
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200751 epp = 0;
752 } else {
753 /* skip setting EPP, when saved value is invalid */
754 if (cpu_data->epp_powersave < 0)
755 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800756
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200757 /*
758 * No need to restore EPP when it is not zero. This
759 * means:
760 * - Policy is not changed
761 * - user has manually changed
762 * - Error reading EPB
763 */
764 epp = intel_pstate_get_epp(cpu_data, value);
765 if (epp)
766 goto skip_epp;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800767
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200768 epp = cpu_data->epp_powersave;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800769 }
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200770update_epp:
771 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
772 value &= ~GENMASK_ULL(31, 24);
773 value |= (u64)epp << 24;
774 } else {
775 intel_pstate_set_epb(cpu, epp);
776 }
777skip_epp:
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700778 WRITE_ONCE(cpu_data->hwp_req_cached, value);
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200779 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
Viresh Kumar41cfd642016-02-22 10:27:46 +0530780}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800781
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800782static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
783{
784 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
785
786 if (!hwp_active)
787 return 0;
788
789 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
790
791 return 0;
792}
793
Chen Yu70f6bf22018-01-29 10:27:57 +0800794static void intel_pstate_hwp_enable(struct cpudata *cpudata);
795
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800796static int intel_pstate_resume(struct cpufreq_policy *policy)
797{
798 if (!hwp_active)
799 return 0;
800
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100801 mutex_lock(&intel_pstate_limits_lock);
802
Chen Yu70f6bf22018-01-29 10:27:57 +0800803 if (policy->cpu == 0)
804 intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
805
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800806 all_cpu_data[policy->cpu]->epp_policy = 0;
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200807 intel_pstate_hwp_set(policy->cpu);
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100808
809 mutex_unlock(&intel_pstate_limits_lock);
810
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +0100811 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800812}
813
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100814static void intel_pstate_update_policies(void)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530815{
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100816 int cpu;
817
818 for_each_possible_cpu(cpu)
819 cpufreq_update_policy(cpu);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800820}
821
Dirk Brandewie93f08222013-02-06 09:02:13 -0800822/************************** sysfs begin ************************/
823#define show_one(file_name, object) \
824 static ssize_t show_##file_name \
825 (struct kobject *kobj, struct attribute *attr, char *buf) \
826 { \
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100827 return sprintf(buf, "%u\n", global.object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800828 }
829
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100830static ssize_t intel_pstate_show_status(char *buf);
831static int intel_pstate_update_status(const char *buf, size_t size);
832
833static ssize_t show_status(struct kobject *kobj,
834 struct attribute *attr, char *buf)
835{
836 ssize_t ret;
837
838 mutex_lock(&intel_pstate_driver_lock);
839 ret = intel_pstate_show_status(buf);
840 mutex_unlock(&intel_pstate_driver_lock);
841
842 return ret;
843}
844
845static ssize_t store_status(struct kobject *a, struct attribute *b,
846 const char *buf, size_t count)
847{
848 char *p = memchr(buf, '\n', count);
849 int ret;
850
851 mutex_lock(&intel_pstate_driver_lock);
852 ret = intel_pstate_update_status(buf, p ? p - buf : count);
853 mutex_unlock(&intel_pstate_driver_lock);
854
855 return ret < 0 ? ret : count;
856}
857
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800858static ssize_t show_turbo_pct(struct kobject *kobj,
859 struct attribute *attr, char *buf)
860{
861 struct cpudata *cpu;
862 int total, no_turbo, turbo_pct;
863 uint32_t turbo_fp;
864
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100865 mutex_lock(&intel_pstate_driver_lock);
866
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200867 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100868 mutex_unlock(&intel_pstate_driver_lock);
869 return -EAGAIN;
870 }
871
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800872 cpu = all_cpu_data[0];
873
874 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
875 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200876 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800877 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100878
879 mutex_unlock(&intel_pstate_driver_lock);
880
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800881 return sprintf(buf, "%u\n", turbo_pct);
882}
883
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800884static ssize_t show_num_pstates(struct kobject *kobj,
885 struct attribute *attr, char *buf)
886{
887 struct cpudata *cpu;
888 int total;
889
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100890 mutex_lock(&intel_pstate_driver_lock);
891
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200892 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100893 mutex_unlock(&intel_pstate_driver_lock);
894 return -EAGAIN;
895 }
896
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800897 cpu = all_cpu_data[0];
898 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100899
900 mutex_unlock(&intel_pstate_driver_lock);
901
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800902 return sprintf(buf, "%u\n", total);
903}
904
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700905static ssize_t show_no_turbo(struct kobject *kobj,
906 struct attribute *attr, char *buf)
907{
908 ssize_t ret;
909
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100910 mutex_lock(&intel_pstate_driver_lock);
911
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200912 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100913 mutex_unlock(&intel_pstate_driver_lock);
914 return -EAGAIN;
915 }
916
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700917 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100918 if (global.turbo_disabled)
919 ret = sprintf(buf, "%u\n", global.turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700920 else
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100921 ret = sprintf(buf, "%u\n", global.no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700922
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100923 mutex_unlock(&intel_pstate_driver_lock);
924
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700925 return ret;
926}
927
Dirk Brandewie93f08222013-02-06 09:02:13 -0800928static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700929 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800930{
931 unsigned int input;
932 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700933
Dirk Brandewie93f08222013-02-06 09:02:13 -0800934 ret = sscanf(buf, "%u", &input);
935 if (ret != 1)
936 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700937
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100938 mutex_lock(&intel_pstate_driver_lock);
939
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200940 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100941 mutex_unlock(&intel_pstate_driver_lock);
942 return -EAGAIN;
943 }
944
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700945 mutex_lock(&intel_pstate_limits_lock);
946
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700947 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100948 if (global.turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -0700949 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700950 mutex_unlock(&intel_pstate_limits_lock);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100951 mutex_unlock(&intel_pstate_driver_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700952 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700953 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800954
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100955 global.no_turbo = clamp_t(int, input, 0, 1);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100956
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100957 if (global.no_turbo) {
958 struct cpudata *cpu = all_cpu_data[0];
959 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
960
961 /* Squash the global minimum into the permitted range. */
962 if (global.min_perf_pct > pct)
963 global.min_perf_pct = pct;
964 }
965
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +0100966 mutex_unlock(&intel_pstate_limits_lock);
967
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100968 intel_pstate_update_policies();
969
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100970 mutex_unlock(&intel_pstate_driver_lock);
971
Dirk Brandewie93f08222013-02-06 09:02:13 -0800972 return count;
973}
974
975static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700976 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800977{
978 unsigned int input;
979 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700980
Dirk Brandewie93f08222013-02-06 09:02:13 -0800981 ret = sscanf(buf, "%u", &input);
982 if (ret != 1)
983 return -EINVAL;
984
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100985 mutex_lock(&intel_pstate_driver_lock);
986
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200987 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100988 mutex_unlock(&intel_pstate_driver_lock);
989 return -EAGAIN;
990 }
991
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700992 mutex_lock(&intel_pstate_limits_lock);
993
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100994 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100995
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +0100996 mutex_unlock(&intel_pstate_limits_lock);
997
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100998 intel_pstate_update_policies();
999
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001000 mutex_unlock(&intel_pstate_driver_lock);
1001
Dirk Brandewie93f08222013-02-06 09:02:13 -08001002 return count;
1003}
1004
1005static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001006 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001007{
1008 unsigned int input;
1009 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001010
Dirk Brandewie93f08222013-02-06 09:02:13 -08001011 ret = sscanf(buf, "%u", &input);
1012 if (ret != 1)
1013 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001014
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001015 mutex_lock(&intel_pstate_driver_lock);
1016
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02001017 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001018 mutex_unlock(&intel_pstate_driver_lock);
1019 return -EAGAIN;
1020 }
1021
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001022 mutex_lock(&intel_pstate_limits_lock);
1023
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001024 global.min_perf_pct = clamp_t(int, input,
1025 min_perf_pct_min(), global.max_perf_pct);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001026
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +01001027 mutex_unlock(&intel_pstate_limits_lock);
1028
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001029 intel_pstate_update_policies();
1030
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001031 mutex_unlock(&intel_pstate_driver_lock);
1032
Dirk Brandewie93f08222013-02-06 09:02:13 -08001033 return count;
1034}
1035
Dirk Brandewie93f08222013-02-06 09:02:13 -08001036show_one(max_perf_pct, max_perf_pct);
1037show_one(min_perf_pct, min_perf_pct);
1038
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001039define_one_global_rw(status);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001040define_one_global_rw(no_turbo);
1041define_one_global_rw(max_perf_pct);
1042define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001043define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001044define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001045
1046static struct attribute *intel_pstate_attributes[] = {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001047 &status.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001048 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001049 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001050 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001051 NULL
1052};
1053
Arvind Yadav106c9c72017-07-03 13:40:33 +05301054static const struct attribute_group intel_pstate_attr_group = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08001055 .attrs = intel_pstate_attributes,
1056};
Dirk Brandewie93f08222013-02-06 09:02:13 -08001057
Stratos Karafotis317dd502014-07-18 08:37:17 -07001058static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001059{
Stratos Karafotis317dd502014-07-18 08:37:17 -07001060 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001061 int rc;
1062
1063 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1064 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001065 if (WARN_ON(!intel_pstate_kobject))
1066 return;
1067
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -07001068 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001069 if (WARN_ON(rc))
1070 return;
1071
1072 /*
1073 * If per cpu limits are enforced there are no global limits, so
1074 * return without creating max/min_perf_pct attributes
1075 */
1076 if (per_cpu_limits)
1077 return;
1078
1079 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1080 WARN_ON(rc);
1081
1082 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1083 WARN_ON(rc);
1084
Dirk Brandewie93f08222013-02-06 09:02:13 -08001085}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001086/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001087
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001088static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001089{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001090 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -07001091 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1092 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001093
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001094 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001095 cpudata->epp_policy = 0;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001096 if (cpudata->epp_default == -EINVAL)
1097 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001098}
1099
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001100#define MSR_IA32_POWER_CTL_BIT_EE 19
1101
1102/* Disable energy efficiency optimization */
1103static void intel_pstate_disable_ee(int cpu)
1104{
1105 u64 power_ctl;
1106 int ret;
1107
1108 ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1109 if (ret)
1110 return;
1111
1112 if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1113 pr_info("Disabling energy efficiency optimization\n");
1114 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1115 wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1116 }
1117}
1118
Philippe Longepe938d21a2015-11-09 17:40:46 -08001119static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001120{
1121 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001122
Len Brown92134bd2017-02-25 16:55:17 -05001123 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001124 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001125}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001126
Philippe Longepe938d21a2015-11-09 17:40:46 -08001127static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001128{
1129 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001130
Len Brown92134bd2017-02-25 16:55:17 -05001131 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001132 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001133}
1134
Philippe Longepe938d21a2015-11-09 17:40:46 -08001135static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001136{
1137 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001138
Len Brown92134bd2017-02-25 16:55:17 -05001139 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001140 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001141}
1142
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001143static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001144{
1145 u64 val;
1146 int32_t vid_fp;
1147 u32 vid;
1148
Chen Yu144c8e12015-07-29 23:53:10 +08001149 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001150 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001151 val |= (u64)1 << 32;
1152
1153 vid_fp = cpudata->vid.min + mul_fp(
1154 int_tofp(pstate - cpudata->pstate.min_pstate),
1155 cpudata->vid.ratio);
1156
1157 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -07001158 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001159
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001160 if (pstate > cpudata->pstate.max_pstate)
1161 vid = cpudata->vid.turbo;
1162
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001163 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001164}
1165
Philippe Longepe1421df62015-11-09 17:40:47 -08001166static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001167{
1168 u64 value;
1169 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -08001170 /* Defined in Table 35-6 from SDM (Sept 2015) */
1171 static int silvermont_freq_table[] = {
1172 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001173
1174 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -08001175 i = value & 0x7;
1176 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001177
Philippe Longepe1421df62015-11-09 17:40:47 -08001178 return silvermont_freq_table[i];
1179}
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001180
Philippe Longepe1421df62015-11-09 17:40:47 -08001181static int airmont_get_scaling(void)
1182{
1183 u64 value;
1184 int i;
1185 /* Defined in Table 35-10 from SDM (Sept 2015) */
1186 static int airmont_freq_table[] = {
1187 83300, 100000, 133300, 116700, 80000,
1188 93300, 90000, 88900, 87500};
1189
1190 rdmsrl(MSR_FSB_FREQ, value);
1191 i = value & 0xF;
1192 WARN_ON(i > 8);
1193
1194 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001195}
1196
Philippe Longepe938d21a2015-11-09 17:40:46 -08001197static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001198{
1199 u64 value;
1200
Len Brown92134bd2017-02-25 16:55:17 -05001201 rdmsrl(MSR_ATOM_CORE_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001202 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1203 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001204 cpudata->vid.ratio = div_fp(
1205 cpudata->vid.max - cpudata->vid.min,
1206 int_tofp(cpudata->pstate.max_pstate -
1207 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001208
Len Brown92134bd2017-02-25 16:55:17 -05001209 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001210 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001211}
1212
Dirk Brandewie016c8152013-10-21 09:20:34 -07001213static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001214{
1215 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001216
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001217 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001218 return (value >> 40) & 0xFF;
1219}
1220
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001221static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001222{
1223 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001224
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001225 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001226 return (value >> 8) & 0xFF;
1227}
1228
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001229static int core_get_tdp_ratio(u64 plat_info)
1230{
1231 /* Check how many TDP levels present */
1232 if (plat_info & 0x600000000) {
1233 u64 tdp_ctrl;
1234 u64 tdp_ratio;
1235 int tdp_msr;
1236 int err;
1237
1238 /* Get the TDP level (0, 1, 2) to get ratios */
1239 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1240 if (err)
1241 return err;
1242
1243 /* TDP MSR are continuous starting at 0x648 */
1244 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1245 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1246 if (err)
1247 return err;
1248
1249 /* For level 1 and 2, bits[23:16] contain the ratio */
1250 if (tdp_ctrl & 0x03)
1251 tdp_ratio >>= 16;
1252
1253 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1254 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1255
1256 return (int)tdp_ratio;
1257 }
1258
1259 return -ENXIO;
1260}
1261
Dirk Brandewie93f08222013-02-06 09:02:13 -08001262static int core_get_max_pstate(void)
1263{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001264 u64 tar;
1265 u64 plat_info;
1266 int max_pstate;
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001267 int tdp_ratio;
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001268 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001269
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001270 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1271 max_pstate = (plat_info >> 8) & 0xFF;
1272
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001273 tdp_ratio = core_get_tdp_ratio(plat_info);
1274 if (tdp_ratio <= 0)
1275 return max_pstate;
1276
1277 if (hwp_active) {
1278 /* Turbo activation ratio is not used on HWP platforms */
1279 return tdp_ratio;
1280 }
1281
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001282 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1283 if (!err) {
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001284 int tar_levels;
1285
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001286 /* Do some sanity checking for safety */
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001287 tar_levels = tar & 0xff;
1288 if (tdp_ratio - 1 == tar_levels) {
1289 max_pstate = tar_levels;
1290 pr_debug("max_pstate=TAC %x\n", max_pstate);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001291 }
1292 }
1293
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001294 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001295}
1296
Dirk Brandewie016c8152013-10-21 09:20:34 -07001297static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001298{
1299 u64 value;
1300 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001301
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001302 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001303 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001304 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001305 if (ret <= nont)
1306 ret = nont;
1307 return ret;
1308}
1309
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001310static inline int core_get_scaling(void)
1311{
1312 return 100000;
1313}
1314
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001315static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001316{
1317 u64 val;
1318
Chen Yu144c8e12015-07-29 23:53:10 +08001319 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001320 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001321 val |= (u64)1 << 32;
1322
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001323 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001324}
1325
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001326static int knl_get_aperf_mperf_shift(void)
1327{
1328 return 10;
1329}
1330
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001331static int knl_get_turbo_pstate(void)
1332{
1333 u64 value;
1334 int nont, ret;
1335
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001336 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001337 nont = core_get_max_pstate();
1338 ret = (((value) >> 8) & 0xFF);
1339 if (ret <= nont)
1340 ret = nont;
1341 return ret;
1342}
1343
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001344static int intel_pstate_get_base_pstate(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001345{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001346 return global.no_turbo || global.turbo_disabled ?
1347 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001348}
1349
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001350static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001351{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001352 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1353 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001354 /*
1355 * Generally, there is no guarantee that this code will always run on
1356 * the CPU being updated, so force the register update to run on the
1357 * right CPU.
1358 */
1359 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1360 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001361}
1362
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001363static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1364{
1365 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1366}
1367
1368static void intel_pstate_max_within_limits(struct cpudata *cpu)
1369{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001370 int pstate;
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001371
1372 update_turbo_state();
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001373 pstate = intel_pstate_get_base_pstate(cpu);
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001374 pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001375 intel_pstate_set_pstate(cpu, pstate);
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001376}
1377
Dirk Brandewie93f08222013-02-06 09:02:13 -08001378static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1379{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001380 cpu->pstate.min_pstate = pstate_funcs.get_min();
1381 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001382 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001383 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001384 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001385 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1386 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001387
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001388 if (pstate_funcs.get_aperf_mperf_shift)
1389 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
1390
Dirk Brandewie007bea02013-12-18 10:32:39 -08001391 if (pstate_funcs.get_vid)
1392 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001393
1394 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001395}
1396
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001397/*
1398 * Long hold time will keep high perf limits for long time,
1399 * which negatively impacts perf/watt for some workloads,
1400 * like specpower. 3ms is based on experiements on some
1401 * workoads.
1402 */
1403static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
1404
1405static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
1406{
1407 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
1408 u32 max_limit = (hwp_req & 0xff00) >> 8;
1409 u32 min_limit = (hwp_req & 0xff);
1410 u32 boost_level1;
1411
1412 /*
1413 * Cases to consider (User changes via sysfs or boot time):
1414 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
1415 * No boost, return.
1416 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
1417 * Should result in one level boost only for P0.
1418 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
1419 * Should result in two level boost:
1420 * (min + p1)/2 and P1.
1421 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
1422 * Should result in three level boost:
1423 * (min + p1)/2, P1 and P0.
1424 */
1425
1426 /* If max and min are equal or already at max, nothing to boost */
1427 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
1428 return;
1429
1430 if (!cpu->hwp_boost_min)
1431 cpu->hwp_boost_min = min_limit;
1432
1433 /* level at half way mark between min and guranteed */
1434 boost_level1 = (HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) + min_limit) >> 1;
1435
1436 if (cpu->hwp_boost_min < boost_level1)
1437 cpu->hwp_boost_min = boost_level1;
1438 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1439 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(cpu->hwp_cap_cached);
1440 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) &&
1441 max_limit != HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1442 cpu->hwp_boost_min = max_limit;
1443 else
1444 return;
1445
1446 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
1447 wrmsrl(MSR_HWP_REQUEST, hwp_req);
1448 cpu->last_update = cpu->sample.time;
1449}
1450
1451static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
1452{
1453 if (cpu->hwp_boost_min) {
1454 bool expired;
1455
1456 /* Check if we are idle for hold time to boost down */
1457 expired = time_after64(cpu->sample.time, cpu->last_update +
1458 hwp_boost_hold_time_ns);
1459 if (expired) {
1460 wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
1461 cpu->hwp_boost_min = 0;
1462 }
1463 }
1464 cpu->last_update = cpu->sample.time;
1465}
1466
Srinivas Pandruvada52ccc4312018-06-05 14:42:40 -07001467static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
1468 u64 time)
1469{
1470 cpu->sample.time = time;
1471
1472 if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
1473 bool do_io = false;
1474
1475 cpu->sched_flags = 0;
1476 /*
1477 * Set iowait_boost flag and update time. Since IO WAIT flag
1478 * is set all the time, we can't just conclude that there is
1479 * some IO bound activity is scheduled on this CPU with just
1480 * one occurrence. If we receive at least two in two
1481 * consecutive ticks, then we treat as boost candidate.
1482 */
1483 if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
1484 do_io = true;
1485
1486 cpu->last_io_update = time;
1487
1488 if (do_io)
1489 intel_pstate_hwp_boost_up(cpu);
1490
1491 } else {
1492 intel_pstate_hwp_boost_down(cpu);
1493 }
1494}
1495
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001496static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
1497 u64 time, unsigned int flags)
1498{
Srinivas Pandruvada52ccc4312018-06-05 14:42:40 -07001499 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1500
1501 cpu->sched_flags |= flags;
1502
1503 if (smp_processor_id() == cpu->cpu)
1504 intel_pstate_update_util_hwp_local(cpu, time);
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001505}
1506
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001507static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001508{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001509 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001510
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001511 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001512}
1513
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001514static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001515{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001516 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001517 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001518 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001519
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001520 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001521 rdmsrl(MSR_IA32_APERF, aperf);
1522 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001523 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001524 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001525 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001526 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001527 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001528 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001529
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001530 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001531 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001532 cpu->sample.aperf = aperf;
1533 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001534 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001535 cpu->sample.aperf -= cpu->prev_aperf;
1536 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001537 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001538
Dirk Brandewie93f08222013-02-06 09:02:13 -08001539 cpu->prev_aperf = aperf;
1540 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001541 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001542 /*
1543 * First time this function is invoked in a given cycle, all of the
1544 * previous sample data fields are equal to zero or stale and they must
1545 * be populated with meaningful numbers for things to work, so assume
1546 * that sample.time will always be reset before setting the utilization
1547 * update hook and make the caller skip the sample then.
1548 */
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001549 if (cpu->last_sample_time) {
1550 intel_pstate_calc_avg_perf(cpu);
1551 return true;
1552 }
1553 return false;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001554}
1555
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001556static inline int32_t get_avg_frequency(struct cpudata *cpu)
1557{
Doug Smythiesc587c792017-08-08 14:05:12 -07001558 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001559}
1560
Philippe Longepebdcaa232016-04-22 11:46:09 -07001561static inline int32_t get_avg_pstate(struct cpudata *cpu)
1562{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001563 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1564 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001565}
1566
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001567static inline int32_t get_target_pstate(struct cpudata *cpu)
Philippe Longepee70eed22015-12-04 17:40:32 +01001568{
1569 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001570 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001571 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001572
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001573 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
1574 sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001575
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001576 boost = cpu->iowait_boost;
1577 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001578
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001579 if (busy_frac < boost)
1580 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001581
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001582 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001583
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001584 target = global.no_turbo || global.turbo_disabled ?
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001585 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1586 target += target >> 2;
1587 target = mul_fp(target, busy_frac);
1588 if (target < cpu->pstate.min_pstate)
1589 target = cpu->pstate.min_pstate;
1590
1591 /*
1592 * If the average P-state during the previous cycle was higher than the
1593 * current target, add 50% of the difference to the target to reduce
1594 * possible performance oscillations and offset possible performance
1595 * loss related to moving the workload from one CPU to another within
1596 * a package/module.
1597 */
1598 avg_pstate = get_avg_pstate(cpu);
1599 if (avg_pstate > target)
1600 target += (avg_pstate - target) >> 1;
1601
1602 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001603}
1604
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001605static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001606{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001607 int max_pstate = intel_pstate_get_base_pstate(cpu);
1608 int min_pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001609
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001610 min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1611 max_pstate = max(min_pstate, cpu->max_perf_ratio);
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001612 return clamp_t(int, pstate, min_pstate, max_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001613}
1614
1615static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1616{
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001617 if (pstate == cpu->pstate.current_pstate)
1618 return;
1619
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001620 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001621 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1622}
1623
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001624static void intel_pstate_adjust_pstate(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001625{
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001626 int from = cpu->pstate.current_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001627 struct sample *sample;
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001628 int target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001629
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001630 update_turbo_state();
1631
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001632 target_pstate = get_target_pstate(cpu);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001633 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1634 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001635 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001636
1637 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001638 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001639 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001640 from,
1641 cpu->pstate.current_pstate,
1642 sample->mperf,
1643 sample->aperf,
1644 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001645 get_avg_frequency(cpu),
1646 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001647}
1648
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001649static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001650 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001651{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001652 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001653 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001654
Viresh Kumar674e7542017-07-28 12:16:38 +05301655 /* Don't allow remote callbacks */
1656 if (smp_processor_id() != cpu->cpu)
1657 return;
1658
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001659 if (flags & SCHED_CPUFREQ_IOWAIT) {
1660 cpu->iowait_boost = int_tofp(1);
Srinivas Pandruvada7bde2d52017-08-03 19:03:14 -07001661 cpu->last_update = time;
1662 /*
1663 * The last time the busy was 100% so P-state was max anyway
1664 * so avoid overhead of computation.
1665 */
1666 if (fp_toint(cpu->sample.busy_scaled) == 100)
1667 return;
1668
1669 goto set_pstate;
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001670 } else if (cpu->iowait_boost) {
1671 /* Clear iowait_boost if the CPU may have been idle. */
1672 delta_ns = time - cpu->last_update;
1673 if (delta_ns > TICK_NSEC)
1674 cpu->iowait_boost = 0;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001675 }
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001676 cpu->last_update = time;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001677 delta_ns = time - cpu->sample.time;
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001678 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001679 return;
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001680
Srinivas Pandruvada7bde2d52017-08-03 19:03:14 -07001681set_pstate:
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001682 if (intel_pstate_sample(cpu, time))
1683 intel_pstate_adjust_pstate(cpu);
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001684}
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001685
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001686static struct pstate_funcs core_funcs = {
1687 .get_max = core_get_max_pstate,
1688 .get_max_physical = core_get_max_pstate_physical,
1689 .get_min = core_get_min_pstate,
1690 .get_turbo = core_get_turbo_pstate,
1691 .get_scaling = core_get_scaling,
1692 .get_val = core_get_val,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001693};
1694
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001695static const struct pstate_funcs silvermont_funcs = {
1696 .get_max = atom_get_max_pstate,
1697 .get_max_physical = atom_get_max_pstate,
1698 .get_min = atom_get_min_pstate,
1699 .get_turbo = atom_get_turbo_pstate,
1700 .get_val = atom_get_val,
1701 .get_scaling = silvermont_get_scaling,
1702 .get_vid = atom_get_vid,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001703};
1704
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001705static const struct pstate_funcs airmont_funcs = {
1706 .get_max = atom_get_max_pstate,
1707 .get_max_physical = atom_get_max_pstate,
1708 .get_min = atom_get_min_pstate,
1709 .get_turbo = atom_get_turbo_pstate,
1710 .get_val = atom_get_val,
1711 .get_scaling = airmont_get_scaling,
1712 .get_vid = atom_get_vid,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001713};
1714
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001715static const struct pstate_funcs knl_funcs = {
1716 .get_max = core_get_max_pstate,
1717 .get_max_physical = core_get_max_pstate_physical,
1718 .get_min = core_get_min_pstate,
1719 .get_turbo = knl_get_turbo_pstate,
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001720 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001721 .get_scaling = core_get_scaling,
1722 .get_val = core_get_val,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001723};
1724
Dirk Brandewie93f08222013-02-06 09:02:13 -08001725#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001726 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1727 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001728
1729static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001730 ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs),
1731 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs),
1732 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_funcs),
1733 ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs),
1734 ICPU(INTEL_FAM6_HASWELL_CORE, core_funcs),
1735 ICPU(INTEL_FAM6_BROADWELL_CORE, core_funcs),
1736 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs),
1737 ICPU(INTEL_FAM6_HASWELL_X, core_funcs),
1738 ICPU(INTEL_FAM6_HASWELL_ULT, core_funcs),
1739 ICPU(INTEL_FAM6_HASWELL_GT3E, core_funcs),
1740 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_funcs),
1741 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs),
1742 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_funcs),
1743 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1744 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs),
1745 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1746 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs),
1747 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs),
Srinivas Pandruvadadbd49b82018-01-10 11:38:51 -08001748 ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs),
1749 ICPU(INTEL_FAM6_ATOM_GEMINI_LAKE, core_funcs),
Srinivas Pandruvadad8de7a42018-01-10 11:38:52 -08001750 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001751 {}
1752};
1753MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1754
Jisheng Zhang29327c82016-06-27 18:07:17 +08001755static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001756 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1757 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1758 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001759 {}
1760};
1761
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001762static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001763 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs),
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001764 {}
1765};
1766
Dirk Brandewie93f08222013-02-06 09:02:13 -08001767static int intel_pstate_init_cpu(unsigned int cpunum)
1768{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001769 struct cpudata *cpu;
1770
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001771 cpu = all_cpu_data[cpunum];
1772
1773 if (!cpu) {
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001774 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001775 if (!cpu)
1776 return -ENOMEM;
1777
1778 all_cpu_data[cpunum] = cpu;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001779
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001780 cpu->epp_default = -EINVAL;
1781 cpu->epp_powersave = -EINVAL;
1782 cpu->epp_saved = -EINVAL;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001783 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001784
1785 cpu = all_cpu_data[cpunum];
1786
Dirk Brandewie93f08222013-02-06 09:02:13 -08001787 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001788
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001789 if (hwp_active) {
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001790 const struct x86_cpu_id *id;
1791
1792 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1793 if (id)
1794 intel_pstate_disable_ee(cpunum);
1795
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001796 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001797 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001798
Vincent Minet179e8472014-07-05 01:51:33 +02001799 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001800
Joe Perches4836df12016-04-05 13:28:23 -07001801 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001802
1803 return 0;
1804}
1805
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001806static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001807{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001808 struct cpudata *cpu = all_cpu_data[cpu_num];
1809
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001810 if (hwp_active && !hwp_boost)
Len Brown62611cb2017-06-23 22:11:53 -07001811 return;
1812
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001813 if (cpu->update_util_set)
1814 return;
1815
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001816 /* Prevent intel_pstate_update_util() from using stale data. */
1817 cpu->sample.time = 0;
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001818 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001819 (hwp_active ?
1820 intel_pstate_update_util_hwp :
1821 intel_pstate_update_util));
Chen Yu4578ee7e2016-05-11 14:33:08 +08001822 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001823}
1824
1825static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1826{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001827 struct cpudata *cpu_data = all_cpu_data[cpu];
1828
1829 if (!cpu_data->update_util_set)
1830 return;
1831
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001832 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001833 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001834 synchronize_sched();
1835}
1836
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001837static int intel_pstate_get_max_freq(struct cpudata *cpu)
1838{
1839 return global.turbo_disabled || global.no_turbo ?
1840 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1841}
1842
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001843static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001844 struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001845{
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001846 int max_freq = intel_pstate_get_max_freq(cpu);
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001847 int32_t max_policy_perf, min_policy_perf;
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001848 int max_state, turbo_max;
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001849
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001850 /*
1851 * HWP needs some special consideration, because on BDX the
1852 * HWP_REQUEST uses abstract value to represent performance
1853 * rather than pure ratios.
1854 */
1855 if (hwp_active) {
1856 intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
1857 } else {
1858 max_state = intel_pstate_get_base_pstate(cpu);
1859 turbo_max = cpu->pstate.turbo_pstate;
1860 }
1861
1862 max_policy_perf = max_state * policy->max / max_freq;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001863 if (policy->max == policy->min) {
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001864 min_policy_perf = max_policy_perf;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001865 } else {
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001866 min_policy_perf = max_state * policy->min / max_freq;
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001867 min_policy_perf = clamp_t(int32_t, min_policy_perf,
1868 0, max_policy_perf);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001869 }
Chen Yu43717aa2015-09-09 18:27:31 +08001870
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001871 pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
1872 policy->cpu, max_state,
1873 min_policy_perf, max_policy_perf);
1874
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001875 /* Normalize user input to [min_perf, max_perf] */
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001876 if (per_cpu_limits) {
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001877 cpu->min_perf_ratio = min_policy_perf;
1878 cpu->max_perf_ratio = max_policy_perf;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001879 } else {
1880 int32_t global_min, global_max;
Chen Yu43717aa2015-09-09 18:27:31 +08001881
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001882 /* Global limits are in percent of the maximum turbo P-state. */
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001883 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
1884 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001885 global_min = clamp_t(int32_t, global_min, 0, global_max);
1886
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001887 pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
1888 global_min, global_max);
1889
1890 cpu->min_perf_ratio = max(min_policy_perf, global_min);
1891 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
1892 cpu->max_perf_ratio = min(max_policy_perf, global_max);
1893 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001894
1895 /* Make sure min_perf <= max_perf */
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001896 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
1897 cpu->max_perf_ratio);
1898
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001899 }
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001900 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
1901 cpu->max_perf_ratio,
1902 cpu->min_perf_ratio);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001903}
1904
Dirk Brandewie93f08222013-02-06 09:02:13 -08001905static int intel_pstate_set_policy(struct cpufreq_policy *policy)
Pali Rohár36b4bed2014-10-16 01:16:51 +02001906{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001907 struct cpudata *cpu;
1908
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001909 if (!policy->cpuinfo.max_freq)
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001910 return -ENODEV;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001911
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001912 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001913 policy->cpuinfo.max_freq, policy->max);
1914
1915 cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001916 cpu->policy = policy->policy;
1917
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001918 mutex_lock(&intel_pstate_limits_lock);
1919
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001920 intel_pstate_update_perf_limits(policy, cpu);
Rafael J. Wysockia240c4a2017-03-02 23:29:12 +01001921
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001922 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001923 /*
1924 * NOHZ_FULL CPUs need this as the governor callback may not
1925 * be invoked on them.
1926 */
1927 intel_pstate_clear_update_util_hook(policy->cpu);
1928 intel_pstate_max_within_limits(cpu);
Len Brown82b4e032017-06-23 22:11:54 -07001929 } else {
1930 intel_pstate_set_update_util_hook(policy->cpu);
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001931 }
1932
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001933 if (hwp_active) {
1934 /*
1935 * When hwp_boost was active before and dynamically it
1936 * was turned off, in that case we need to clear the
1937 * update util hook.
1938 */
1939 if (!hwp_boost)
1940 intel_pstate_clear_update_util_hook(policy->cpu);
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +02001941 intel_pstate_hwp_set(policy->cpu);
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001942 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001943
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001944 mutex_unlock(&intel_pstate_limits_lock);
1945
Dirk Brandewie93f08222013-02-06 09:02:13 -08001946 return 0;
1947}
1948
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001949static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
1950 struct cpudata *cpu)
1951{
1952 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
1953 policy->max < policy->cpuinfo.max_freq &&
1954 policy->max > cpu->pstate.max_freq) {
1955 pr_debug("policy->max > max non turbo frequency\n");
1956 policy->max = policy->cpuinfo.max_freq;
1957 }
1958}
1959
Dirk Brandewie93f08222013-02-06 09:02:13 -08001960static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1961{
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08001962 struct cpudata *cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08001963
1964 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001965 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
1966 intel_pstate_get_max_freq(cpu));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001967
Stratos Karafotis285cb992014-07-18 08:37:21 -07001968 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001969 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001970 return -EINVAL;
1971
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001972 intel_pstate_adjust_policy_max(policy, cpu);
1973
Dirk Brandewie93f08222013-02-06 09:02:13 -08001974 return 0;
1975}
1976
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001977static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001978{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001979 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001980}
1981
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001982static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
1983{
1984 pr_debug("CPU %d exiting\n", policy->cpu);
1985
1986 intel_pstate_clear_update_util_hook(policy->cpu);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001987 if (hwp_active)
1988 intel_pstate_hwp_save_state(policy);
1989 else
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001990 intel_cpufreq_stop_cpu(policy);
1991}
1992
1993static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1994{
1995 intel_pstate_exit_perf_limits(policy);
1996
1997 policy->fast_switch_possible = false;
1998
1999 return 0;
2000}
2001
2002static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002003{
Dirk Brandewie93f08222013-02-06 09:02:13 -08002004 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07002005 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002006
2007 rc = intel_pstate_init_cpu(policy->cpu);
2008 if (rc)
2009 return rc;
2010
2011 cpu = all_cpu_data[policy->cpu];
2012
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07002013 cpu->max_perf_ratio = 0xFF;
2014 cpu->min_perf_ratio = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002015
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002016 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2017 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002018
2019 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002020 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07002021 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01002022 policy->cpuinfo.max_freq = global.turbo_disabled ?
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07002023 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2024 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2025
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002026 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002027
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002028 policy->fast_switch_possible = true;
2029
Dirk Brandewie93f08222013-02-06 09:02:13 -08002030 return 0;
2031}
2032
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002033static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002034{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002035 int ret = __intel_pstate_cpu_init(policy);
2036
2037 if (ret)
2038 return ret;
2039
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01002040 if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002041 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2042 else
2043 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002044
2045 return 0;
2046}
2047
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002048static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08002049 .flags = CPUFREQ_CONST_LOOPS,
2050 .verify = intel_pstate_verify_policy,
2051 .setpolicy = intel_pstate_set_policy,
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002052 .suspend = intel_pstate_hwp_save_state,
Srinivas Pandruvada84428852016-11-24 16:07:10 -08002053 .resume = intel_pstate_resume,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002054 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002055 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07002056 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002057 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08002058};
2059
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002060static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2061{
2062 struct cpudata *cpu = all_cpu_data[policy->cpu];
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002063
2064 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002065 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2066 intel_pstate_get_max_freq(cpu));
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002067
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002068 intel_pstate_adjust_policy_max(policy, cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002069
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002070 intel_pstate_update_perf_limits(policy, cpu);
2071
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002072 return 0;
2073}
2074
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002075/* Use of trace in passive mode:
2076 *
2077 * In passive mode the trace core_busy field (also known as the
2078 * performance field, and lablelled as such on the graphs; also known as
2079 * core_avg_perf) is not needed and so is re-assigned to indicate if the
2080 * driver call was via the normal or fast switch path. Various graphs
2081 * output from the intel_pstate_tracer.py utility that include core_busy
2082 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
2083 * so we use 10 to indicate the the normal path through the driver, and
2084 * 90 to indicate the fast switch path through the driver.
2085 * The scaled_busy field is not used, and is set to 0.
2086 */
2087
2088#define INTEL_PSTATE_TRACE_TARGET 10
2089#define INTEL_PSTATE_TRACE_FAST_SWITCH 90
2090
2091static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
2092{
2093 struct sample *sample;
2094
2095 if (!trace_pstate_sample_enabled())
2096 return;
2097
2098 if (!intel_pstate_sample(cpu, ktime_get()))
2099 return;
2100
2101 sample = &cpu->sample;
2102 trace_pstate_sample(trace_type,
2103 0,
2104 old_pstate,
2105 cpu->pstate.current_pstate,
2106 sample->mperf,
2107 sample->aperf,
2108 sample->tsc,
2109 get_avg_frequency(cpu),
2110 fp_toint(cpu->iowait_boost * 100));
2111}
2112
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002113static int intel_cpufreq_target(struct cpufreq_policy *policy,
2114 unsigned int target_freq,
2115 unsigned int relation)
2116{
2117 struct cpudata *cpu = all_cpu_data[policy->cpu];
2118 struct cpufreq_freqs freqs;
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002119 int target_pstate, old_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002120
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002121 update_turbo_state();
2122
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002123 freqs.old = policy->cur;
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002124 freqs.new = target_freq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002125
2126 cpufreq_freq_transition_begin(policy, &freqs);
2127 switch (relation) {
2128 case CPUFREQ_RELATION_L:
2129 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2130 break;
2131 case CPUFREQ_RELATION_H:
2132 target_pstate = freqs.new / cpu->pstate.scaling;
2133 break;
2134 default:
2135 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2136 break;
2137 }
2138 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002139 old_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002140 if (target_pstate != cpu->pstate.current_pstate) {
2141 cpu->pstate.current_pstate = target_pstate;
2142 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2143 pstate_funcs.get_val(cpu, target_pstate));
2144 }
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002145 freqs.new = target_pstate * cpu->pstate.scaling;
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002146 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002147 cpufreq_freq_transition_end(policy, &freqs, false);
2148
2149 return 0;
2150}
2151
2152static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2153 unsigned int target_freq)
2154{
2155 struct cpudata *cpu = all_cpu_data[policy->cpu];
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002156 int target_pstate, old_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002157
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002158 update_turbo_state();
2159
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002160 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002161 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002162 old_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002163 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002164 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002165 return target_pstate * cpu->pstate.scaling;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002166}
2167
2168static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2169{
2170 int ret = __intel_pstate_cpu_init(policy);
2171
2172 if (ret)
2173 return ret;
2174
2175 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
Rafael J. Wysocki1b72e7f2017-04-11 00:20:41 +02002176 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002177 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2178 policy->cur = policy->cpuinfo.min_freq;
2179
2180 return 0;
2181}
2182
2183static struct cpufreq_driver intel_cpufreq = {
2184 .flags = CPUFREQ_CONST_LOOPS,
2185 .verify = intel_cpufreq_verify_policy,
2186 .target = intel_cpufreq_target,
2187 .fast_switch = intel_cpufreq_fast_switch,
2188 .init = intel_cpufreq_cpu_init,
2189 .exit = intel_pstate_cpu_exit,
2190 .stop_cpu = intel_cpufreq_stop_cpu,
2191 .name = "intel_cpufreq",
2192};
2193
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002194static struct cpufreq_driver *default_driver = &intel_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002195
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002196static void intel_pstate_driver_cleanup(void)
2197{
2198 unsigned int cpu;
2199
2200 get_online_cpus();
2201 for_each_online_cpu(cpu) {
2202 if (all_cpu_data[cpu]) {
2203 if (intel_pstate_driver == &intel_pstate)
2204 intel_pstate_clear_update_util_hook(cpu);
2205
2206 kfree(all_cpu_data[cpu]);
2207 all_cpu_data[cpu] = NULL;
2208 }
2209 }
2210 put_online_cpus();
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002211 intel_pstate_driver = NULL;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002212}
2213
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002214static int intel_pstate_register_driver(struct cpufreq_driver *driver)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002215{
2216 int ret;
2217
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002218 memset(&global, 0, sizeof(global));
2219 global.max_perf_pct = 100;
Rafael J. Wysockic3a49c82017-02-28 00:05:01 +01002220
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002221 intel_pstate_driver = driver;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002222 ret = cpufreq_register_driver(intel_pstate_driver);
2223 if (ret) {
2224 intel_pstate_driver_cleanup();
2225 return ret;
2226 }
2227
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002228 global.min_perf_pct = min_perf_pct_min();
2229
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002230 return 0;
2231}
2232
2233static int intel_pstate_unregister_driver(void)
2234{
2235 if (hwp_active)
2236 return -EBUSY;
2237
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002238 cpufreq_unregister_driver(intel_pstate_driver);
2239 intel_pstate_driver_cleanup();
2240
2241 return 0;
2242}
2243
2244static ssize_t intel_pstate_show_status(char *buf)
2245{
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002246 if (!intel_pstate_driver)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002247 return sprintf(buf, "off\n");
2248
2249 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2250 "active" : "passive");
2251}
2252
2253static int intel_pstate_update_status(const char *buf, size_t size)
2254{
2255 int ret;
2256
2257 if (size == 3 && !strncmp(buf, "off", size))
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002258 return intel_pstate_driver ?
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002259 intel_pstate_unregister_driver() : -EINVAL;
2260
2261 if (size == 6 && !strncmp(buf, "active", size)) {
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002262 if (intel_pstate_driver) {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002263 if (intel_pstate_driver == &intel_pstate)
2264 return 0;
2265
2266 ret = intel_pstate_unregister_driver();
2267 if (ret)
2268 return ret;
2269 }
2270
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002271 return intel_pstate_register_driver(&intel_pstate);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002272 }
2273
2274 if (size == 7 && !strncmp(buf, "passive", size)) {
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002275 if (intel_pstate_driver) {
Rafael J. Wysocki0042b2c2017-03-28 00:14:08 +02002276 if (intel_pstate_driver == &intel_cpufreq)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002277 return 0;
2278
2279 ret = intel_pstate_unregister_driver();
2280 if (ret)
2281 return ret;
2282 }
2283
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002284 return intel_pstate_register_driver(&intel_cpufreq);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002285 }
2286
2287 return -EINVAL;
2288}
2289
Jisheng Zhangeed43602016-06-27 18:07:16 +08002290static int no_load __initdata;
2291static int no_hwp __initdata;
2292static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08002293static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01002294
Jisheng Zhang29327c82016-06-27 18:07:17 +08002295static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002296{
Dirk Brandewie016c8152013-10-21 09:20:34 -07002297 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07002298 !pstate_funcs.get_min() ||
2299 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002300 return -ENODEV;
2301
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002302 return 0;
2303}
Dirk Brandewie016c8152013-10-21 09:20:34 -07002304
Jisheng Zhang29327c82016-06-27 18:07:17 +08002305static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002306{
2307 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07002308 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002309 pstate_funcs.get_min = funcs->get_min;
2310 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002311 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01002312 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08002313 pstate_funcs.get_vid = funcs->get_vid;
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07002314 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002315}
2316
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002317#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002318
Jisheng Zhang29327c82016-06-27 18:07:17 +08002319static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002320{
2321 int i;
2322
2323 for_each_possible_cpu(i) {
2324 acpi_status status;
2325 union acpi_object *pss;
2326 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2327 struct acpi_processor *pr = per_cpu(processors, i);
2328
2329 if (!pr)
2330 continue;
2331
2332 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2333 if (ACPI_FAILURE(status))
2334 continue;
2335
2336 pss = buffer.pointer;
2337 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2338 kfree(pss);
2339 return false;
2340 }
2341
2342 kfree(pss);
2343 }
2344
2345 return true;
2346}
2347
Jisheng Zhang29327c82016-06-27 18:07:17 +08002348static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09002349{
2350 int i;
2351
2352 for_each_possible_cpu(i) {
2353 struct acpi_processor *pr = per_cpu(processors, i);
2354
2355 if (!pr)
2356 continue;
2357 if (acpi_has_method(pr->handle, "_PPC"))
2358 return true;
2359 }
2360 return false;
2361}
2362
2363enum {
2364 PSS,
2365 PPC,
2366};
2367
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002368/* Hardware vendor-specific info that has its own power management modes */
Toshi Kani5e932322017-08-23 16:54:44 -06002369static struct acpi_platform_list plat_info[] __initdata = {
2370 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS},
2371 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2372 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2373 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2374 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2375 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2376 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2377 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2378 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2379 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2380 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2381 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2382 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2383 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2384 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2385 { } /* End */
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002386};
2387
Jisheng Zhang29327c82016-06-27 18:07:17 +08002388static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002389{
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002390 const struct x86_cpu_id *id;
2391 u64 misc_pwr;
Toshi Kani5e932322017-08-23 16:54:44 -06002392 int idx;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002393
2394 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2395 if (id) {
2396 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2397 if ( misc_pwr & (1 << 8))
2398 return true;
2399 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002400
Toshi Kani5e932322017-08-23 16:54:44 -06002401 idx = acpi_match_platform_list(plat_info);
2402 if (idx < 0)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002403 return false;
2404
Toshi Kani5e932322017-08-23 16:54:44 -06002405 switch (plat_info[idx].data) {
2406 case PSS:
2407 return intel_pstate_no_acpi_pss();
2408 case PPC:
2409 return intel_pstate_has_acpi_ppc() && !force_load;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002410 }
2411
2412 return false;
2413}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002414
2415static void intel_pstate_request_control_from_smm(void)
2416{
2417 /*
2418 * It may be unsafe to request P-states control from SMM if _PPC support
2419 * has not been enabled.
2420 */
2421 if (acpi_ppc)
2422 acpi_processor_pstate_control();
2423}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002424#else /* CONFIG_ACPI not enabled */
2425static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002426static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002427static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002428#endif /* CONFIG_ACPI */
2429
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002430static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2431 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2432 {}
2433};
2434
Dirk Brandewie93f08222013-02-06 09:02:13 -08002435static int __init intel_pstate_init(void)
2436{
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002437 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002438
Dirk Brandewie6be26492013-02-15 22:55:10 +01002439 if (no_load)
2440 return -ENODEV;
2441
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002442 if (x86_match_cpu(hwp_support_ids)) {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02002443 copy_cpu_funcs(&core_funcs);
Rafael J. Wysockic4f3f702017-07-25 00:12:20 +02002444 if (!no_hwp) {
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002445 hwp_active++;
2446 intel_pstate.attr = hwp_cpufreq_attrs;
2447 goto hwp_cpu_matched;
2448 }
2449 } else {
2450 const struct x86_cpu_id *id;
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002451
2452 id = x86_match_cpu(intel_pstate_cpu_ids);
2453 if (!id)
2454 return -ENODEV;
2455
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02002456 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002457 }
2458
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002459 if (intel_pstate_msrs_not_valid())
2460 return -ENODEV;
2461
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002462hwp_cpu_matched:
2463 /*
2464 * The Intel pstate driver will be ignored if the platform
2465 * firmware has its own power management modes.
2466 */
2467 if (intel_pstate_platform_pwr_mgmt_exists())
2468 return -ENODEV;
2469
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002470 if (!hwp_active && hwp_only)
2471 return -ENOTSUPP;
2472
Joe Perches4836df12016-04-05 13:28:23 -07002473 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002474
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002475 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002476 if (!all_cpu_data)
2477 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002478
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002479 intel_pstate_request_control_from_smm();
2480
Dirk Brandewie93f08222013-02-06 09:02:13 -08002481 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002482
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002483 mutex_lock(&intel_pstate_driver_lock);
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002484 rc = intel_pstate_register_driver(default_driver);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002485 mutex_unlock(&intel_pstate_driver_lock);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002486 if (rc)
2487 return rc;
Dirk Brandewie907cc902013-03-05 14:15:27 -08002488
Dirk Brandewie907cc902013-03-05 14:15:27 -08002489 if (hwp_active)
2490 pr_info("HWP enabled\n");
2491
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002492 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002493}
2494device_initcall(intel_pstate_init);
2495
Dirk Brandewie6be26492013-02-15 22:55:10 +01002496static int __init intel_pstate_setup(char *str)
2497{
2498 if (!str)
2499 return -EINVAL;
2500
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002501 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002502 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002503 } else if (!strcmp(str, "passive")) {
2504 pr_info("Passive mode enabled\n");
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002505 default_driver = &intel_cpufreq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002506 no_hwp = 1;
2507 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002508 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002509 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002510 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002511 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002512 if (!strcmp(str, "force"))
2513 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002514 if (!strcmp(str, "hwp_only"))
2515 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002516 if (!strcmp(str, "per_cpu_perf_limits"))
2517 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002518
2519#ifdef CONFIG_ACPI
2520 if (!strcmp(str, "support_acpi_ppc"))
2521 acpi_ppc = true;
2522#endif
2523
Dirk Brandewie6be26492013-02-15 22:55:10 +01002524 return 0;
2525}
2526early_param("intel_pstate", intel_pstate_setup);
2527
Dirk Brandewie93f08222013-02-06 09:02:13 -08002528MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2529MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2530MODULE_LICENSE("GPL");