blob: 3949e3861f55cf10b5359a920ba52edc1e3a096f [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
226 * @hwp_boost_min: Last HWP boosted min performance
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700227 *
228 * This structure stores per CPU instance data for all CPUs.
229 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800230struct cpudata {
231 int cpu;
232
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200233 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100234 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800235 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800236
Dirk Brandewie93f08222013-02-06 09:02:13 -0800237 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800238 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800239
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200240 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100241 u64 last_sample_time;
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700242 u64 aperf_mperf_shift;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800243 u64 prev_aperf;
244 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700245 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100246 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800247 struct sample sample;
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700248 int32_t min_perf_ratio;
249 int32_t max_perf_ratio;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700250#ifdef CONFIG_ACPI
251 struct acpi_processor_performance acpi_perf_data;
252 bool valid_pss_table;
253#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200254 unsigned int iowait_boost;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800255 s16 epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800256 s16 epp_policy;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800257 s16 epp_default;
258 s16 epp_saved;
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700259 u64 hwp_req_cached;
260 u64 hwp_cap_cached;
261 u32 hwp_boost_min;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800262};
263
264static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700265
266/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700267 * struct pstate_funcs - Per CPU model specific callbacks
268 * @get_max: Callback to get maximum non turbo effective P state
269 * @get_max_physical: Callback to get maximum non turbo physical P state
270 * @get_min: Callback to get minimum P state
271 * @get_turbo: Callback to get turbo P state
272 * @get_scaling: Callback to get frequency scaling factor
273 * @get_val: Callback to convert P state to actual MSR write value
274 * @get_vid: Callback to get VID data for Atom platforms
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700275 *
276 * Core and Atom CPU models have different way to get P State limits. This
277 * structure is used to store those callbacks.
278 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700279struct pstate_funcs {
280 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700281 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700282 int (*get_min)(void);
283 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700284 int (*get_scaling)(void);
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -0700285 int (*get_aperf_mperf_shift)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100286 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800287 void (*get_vid)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800288};
289
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800290static struct pstate_funcs pstate_funcs __read_mostly;
Rafael J. Wysocki5c439052017-03-28 00:05:44 +0200291
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800292static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700293static bool per_cpu_limits __read_mostly;
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700294static bool hwp_boost __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700295
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200296static struct cpufreq_driver *intel_pstate_driver __read_mostly;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100297
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700298#ifdef CONFIG_ACPI
299static bool acpi_ppc;
300#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700301
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100302static struct global_params global;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800303
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100304static DEFINE_MUTEX(intel_pstate_driver_lock);
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700305static DEFINE_MUTEX(intel_pstate_limits_lock);
306
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700307#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700308
309static bool intel_pstate_get_ppc_enable_status(void)
310{
311 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
312 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
313 return true;
314
315 return acpi_ppc;
316}
317
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800318#ifdef CONFIG_ACPI_CPPC_LIB
319
320/* The work item is needed to avoid CPU hotplug locking issues */
321static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
322{
323 sched_set_itmt_support();
324}
325
326static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
327
328static void intel_pstate_set_itmt_prio(int cpu)
329{
330 struct cppc_perf_caps cppc_perf;
331 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
332 int ret;
333
334 ret = cppc_get_perf_caps(cpu, &cppc_perf);
335 if (ret)
336 return;
337
338 /*
339 * The priorities can be set regardless of whether or not
340 * sched_set_itmt_support(true) has been called and it is valid to
341 * update them at any time after it has been called.
342 */
343 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
344
345 if (max_highest_perf <= min_highest_perf) {
346 if (cppc_perf.highest_perf > max_highest_perf)
347 max_highest_perf = cppc_perf.highest_perf;
348
349 if (cppc_perf.highest_perf < min_highest_perf)
350 min_highest_perf = cppc_perf.highest_perf;
351
352 if (max_highest_perf > min_highest_perf) {
353 /*
354 * This code can be run during CPU online under the
355 * CPU hotplug locks, so sched_set_itmt_support()
356 * cannot be called from here. Queue up a work item
357 * to invoke it.
358 */
359 schedule_work(&sched_itmt_work);
360 }
361 }
362}
363#else
364static void intel_pstate_set_itmt_prio(int cpu)
365{
366}
367#endif
368
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700369static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
370{
371 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700372 int ret;
373 int i;
374
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800375 if (hwp_active) {
376 intel_pstate_set_itmt_prio(policy->cpu);
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700377 return;
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800378 }
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700379
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700380 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700381 return;
382
383 cpu = all_cpu_data[policy->cpu];
384
385 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
386 policy->cpu);
387 if (ret)
388 return;
389
390 /*
391 * Check if the control value in _PSS is for PERF_CTL MSR, which should
392 * guarantee that the states returned by it map to the states in our
393 * list directly.
394 */
395 if (cpu->acpi_perf_data.control_register.space_id !=
396 ACPI_ADR_SPACE_FIXED_HARDWARE)
397 goto err;
398
399 /*
400 * If there is only one entry _PSS, simply ignore _PSS and continue as
401 * usual without taking _PSS into account
402 */
403 if (cpu->acpi_perf_data.state_count < 2)
404 goto err;
405
406 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
407 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
408 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
409 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
410 (u32) cpu->acpi_perf_data.states[i].core_frequency,
411 (u32) cpu->acpi_perf_data.states[i].power,
412 (u32) cpu->acpi_perf_data.states[i].control);
413 }
414
415 /*
416 * The _PSS table doesn't contain whole turbo frequency range.
417 * This just contains +1 MHZ above the max non turbo frequency,
418 * with control value corresponding to max turbo ratio. But
419 * when cpufreq set policy is called, it will call with this
420 * max frequency, which will cause a reduced performance as
421 * this driver uses real max turbo frequency as the max
422 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700423 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700424 * Also need to convert to MHz as _PSS freq is in MHz.
425 */
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100426 if (!global.turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700427 cpu->acpi_perf_data.states[0].core_frequency =
428 policy->cpuinfo.max_freq / 1000;
429 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700430 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700431
432 return;
433
434 err:
435 cpu->valid_pss_table = false;
436 acpi_processor_unregister_performance(policy->cpu);
437}
438
439static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
440{
441 struct cpudata *cpu;
442
443 cpu = all_cpu_data[policy->cpu];
444 if (!cpu->valid_pss_table)
445 return;
446
447 acpi_processor_unregister_performance(policy->cpu);
448}
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700449#else
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100450static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700451{
452}
453
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100454static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700455{
456}
457#endif
458
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700459static inline void update_turbo_state(void)
460{
461 u64 misc_en;
462 struct cpudata *cpu;
463
464 cpu = all_cpu_data[0];
465 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100466 global.turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700467 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
468 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
469}
470
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100471static int min_perf_pct_min(void)
472{
473 struct cpudata *cpu = all_cpu_data[0];
Rafael J. Wysocki57caf4e2017-06-05 14:51:18 +0200474 int turbo_pstate = cpu->pstate.turbo_pstate;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100475
Rafael J. Wysocki57caf4e2017-06-05 14:51:18 +0200476 return turbo_pstate ?
Srinivas Pandruvadad4436c02017-07-10 16:23:52 -0700477 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100478}
479
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800480static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
481{
482 u64 epb;
483 int ret;
484
485 if (!static_cpu_has(X86_FEATURE_EPB))
486 return -ENXIO;
487
488 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
489 if (ret)
490 return (s16)ret;
491
492 return (s16)(epb & 0x0f);
493}
494
495static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
496{
497 s16 epp;
498
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800499 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
500 /*
501 * When hwp_req_data is 0, means that caller didn't read
502 * MSR_HWP_REQUEST, so need to read and get EPP.
503 */
504 if (!hwp_req_data) {
505 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
506 &hwp_req_data);
507 if (epp)
508 return epp;
509 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800510 epp = (hwp_req_data >> 24) & 0xff;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800511 } else {
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800512 /* When there is no EPP present, HWP uses EPB settings */
513 epp = intel_pstate_get_epb(cpu_data);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800514 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800515
516 return epp;
517}
518
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800519static int intel_pstate_set_epb(int cpu, s16 pref)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800520{
521 u64 epb;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800522 int ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800523
524 if (!static_cpu_has(X86_FEATURE_EPB))
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800525 return -ENXIO;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800526
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800527 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
528 if (ret)
529 return ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800530
531 epb = (epb & ~0x0f) | pref;
532 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800533
534 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800535}
536
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800537/*
538 * EPP/EPB display strings corresponding to EPP index in the
539 * energy_perf_strings[]
540 * index String
541 *-------------------------------------
542 * 0 default
543 * 1 performance
544 * 2 balance_performance
545 * 3 balance_power
546 * 4 power
547 */
548static const char * const energy_perf_strings[] = {
549 "default",
550 "performance",
551 "balance_performance",
552 "balance_power",
553 "power",
554 NULL
555};
Len Brown3cedbc52017-05-01 23:06:08 -0400556static const unsigned int epp_values[] = {
557 HWP_EPP_PERFORMANCE,
558 HWP_EPP_BALANCE_PERFORMANCE,
559 HWP_EPP_BALANCE_POWERSAVE,
560 HWP_EPP_POWERSAVE
561};
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800562
563static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
564{
565 s16 epp;
566 int index = -EINVAL;
567
568 epp = intel_pstate_get_epp(cpu_data, 0);
569 if (epp < 0)
570 return epp;
571
572 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
Len Brown3cedbc52017-05-01 23:06:08 -0400573 if (epp == HWP_EPP_PERFORMANCE)
574 return 1;
575 if (epp <= HWP_EPP_BALANCE_PERFORMANCE)
576 return 2;
577 if (epp <= HWP_EPP_BALANCE_POWERSAVE)
578 return 3;
579 else
580 return 4;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800581 } else if (static_cpu_has(X86_FEATURE_EPB)) {
582 /*
583 * Range:
584 * 0x00-0x03 : Performance
585 * 0x04-0x07 : Balance performance
586 * 0x08-0x0B : Balance power
587 * 0x0C-0x0F : Power
588 * The EPB is a 4 bit value, but our ranges restrict the
589 * value which can be set. Here only using top two bits
590 * effectively.
591 */
592 index = (epp >> 2) + 1;
593 }
594
595 return index;
596}
597
598static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
599 int pref_index)
600{
601 int epp = -EINVAL;
602 int ret;
603
604 if (!pref_index)
605 epp = cpu_data->epp_default;
606
607 mutex_lock(&intel_pstate_limits_lock);
608
609 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
610 u64 value;
611
612 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
613 if (ret)
614 goto return_pref;
615
616 value &= ~GENMASK_ULL(31, 24);
617
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800618 if (epp == -EINVAL)
Len Brown3cedbc52017-05-01 23:06:08 -0400619 epp = epp_values[pref_index - 1];
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800620
621 value |= (u64)epp << 24;
622 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
623 } else {
624 if (epp == -EINVAL)
625 epp = (pref_index - 1) << 2;
626 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
627 }
628return_pref:
629 mutex_unlock(&intel_pstate_limits_lock);
630
631 return ret;
632}
633
634static ssize_t show_energy_performance_available_preferences(
635 struct cpufreq_policy *policy, char *buf)
636{
637 int i = 0;
638 int ret = 0;
639
640 while (energy_perf_strings[i] != NULL)
641 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
642
643 ret += sprintf(&buf[ret], "\n");
644
645 return ret;
646}
647
648cpufreq_freq_attr_ro(energy_performance_available_preferences);
649
650static ssize_t store_energy_performance_preference(
651 struct cpufreq_policy *policy, const char *buf, size_t count)
652{
653 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
654 char str_preference[21];
655 int ret, i = 0;
656
657 ret = sscanf(buf, "%20s", str_preference);
658 if (ret != 1)
659 return -EINVAL;
660
661 while (energy_perf_strings[i] != NULL) {
662 if (!strcmp(str_preference, energy_perf_strings[i])) {
663 intel_pstate_set_energy_pref_index(cpu_data, i);
664 return count;
665 }
666 ++i;
667 }
668
669 return -EINVAL;
670}
671
672static ssize_t show_energy_performance_preference(
673 struct cpufreq_policy *policy, char *buf)
674{
675 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
676 int preference;
677
678 preference = intel_pstate_get_energy_pref_index(cpu_data);
679 if (preference < 0)
680 return preference;
681
682 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
683}
684
685cpufreq_freq_attr_rw(energy_performance_preference);
686
687static struct freq_attr *hwp_cpufreq_attrs[] = {
688 &energy_performance_preference,
689 &energy_performance_available_preferences,
690 NULL,
691};
692
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700693static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
694 int *current_max)
695{
696 u64 cap;
697
698 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700699 WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap);
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700700 if (global.no_turbo)
701 *current_max = HWP_GUARANTEED_PERF(cap);
702 else
703 *current_max = HWP_HIGHEST_PERF(cap);
704
705 *phy_max = HWP_HIGHEST_PERF(cap);
706}
707
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200708static void intel_pstate_hwp_set(unsigned int cpu)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800709{
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200710 struct cpudata *cpu_data = all_cpu_data[cpu];
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700711 int max, min;
712 u64 value;
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200713 s16 epp;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700714
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -0700715 max = cpu_data->max_perf_ratio;
716 min = cpu_data->min_perf_ratio;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700717
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200718 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
719 min = max;
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700720
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200721 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700722
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200723 value &= ~HWP_MIN_PERF(~0L);
724 value |= HWP_MIN_PERF(min);
Srinivas Pandruvada3f8ed542017-03-13 18:30:12 -0700725
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200726 value &= ~HWP_MAX_PERF(~0L);
727 value |= HWP_MAX_PERF(max);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800728
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200729 if (cpu_data->epp_policy == cpu_data->policy)
730 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800731
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200732 cpu_data->epp_policy = cpu_data->policy;
733
734 if (cpu_data->epp_saved >= 0) {
735 epp = cpu_data->epp_saved;
736 cpu_data->epp_saved = -EINVAL;
737 goto update_epp;
738 }
739
740 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
741 epp = intel_pstate_get_epp(cpu_data, value);
742 cpu_data->epp_powersave = epp;
743 /* If EPP read was failed, then don't try to write */
744 if (epp < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800745 goto skip_epp;
746
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200747 epp = 0;
748 } else {
749 /* skip setting EPP, when saved value is invalid */
750 if (cpu_data->epp_powersave < 0)
751 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800752
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200753 /*
754 * No need to restore EPP when it is not zero. This
755 * means:
756 * - Policy is not changed
757 * - user has manually changed
758 * - Error reading EPB
759 */
760 epp = intel_pstate_get_epp(cpu_data, value);
761 if (epp)
762 goto skip_epp;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800763
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200764 epp = cpu_data->epp_powersave;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800765 }
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200766update_epp:
767 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
768 value &= ~GENMASK_ULL(31, 24);
769 value |= (u64)epp << 24;
770 } else {
771 intel_pstate_set_epb(cpu, epp);
772 }
773skip_epp:
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -0700774 WRITE_ONCE(cpu_data->hwp_req_cached, value);
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200775 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
Viresh Kumar41cfd642016-02-22 10:27:46 +0530776}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800777
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800778static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
779{
780 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
781
782 if (!hwp_active)
783 return 0;
784
785 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
786
787 return 0;
788}
789
Chen Yu70f6bf22018-01-29 10:27:57 +0800790static void intel_pstate_hwp_enable(struct cpudata *cpudata);
791
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800792static int intel_pstate_resume(struct cpufreq_policy *policy)
793{
794 if (!hwp_active)
795 return 0;
796
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100797 mutex_lock(&intel_pstate_limits_lock);
798
Chen Yu70f6bf22018-01-29 10:27:57 +0800799 if (policy->cpu == 0)
800 intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
801
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800802 all_cpu_data[policy->cpu]->epp_policy = 0;
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +0200803 intel_pstate_hwp_set(policy->cpu);
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100804
805 mutex_unlock(&intel_pstate_limits_lock);
806
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +0100807 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800808}
809
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100810static void intel_pstate_update_policies(void)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530811{
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100812 int cpu;
813
814 for_each_possible_cpu(cpu)
815 cpufreq_update_policy(cpu);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800816}
817
Dirk Brandewie93f08222013-02-06 09:02:13 -0800818/************************** sysfs begin ************************/
819#define show_one(file_name, object) \
820 static ssize_t show_##file_name \
821 (struct kobject *kobj, struct attribute *attr, char *buf) \
822 { \
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100823 return sprintf(buf, "%u\n", global.object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800824 }
825
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100826static ssize_t intel_pstate_show_status(char *buf);
827static int intel_pstate_update_status(const char *buf, size_t size);
828
829static ssize_t show_status(struct kobject *kobj,
830 struct attribute *attr, char *buf)
831{
832 ssize_t ret;
833
834 mutex_lock(&intel_pstate_driver_lock);
835 ret = intel_pstate_show_status(buf);
836 mutex_unlock(&intel_pstate_driver_lock);
837
838 return ret;
839}
840
841static ssize_t store_status(struct kobject *a, struct attribute *b,
842 const char *buf, size_t count)
843{
844 char *p = memchr(buf, '\n', count);
845 int ret;
846
847 mutex_lock(&intel_pstate_driver_lock);
848 ret = intel_pstate_update_status(buf, p ? p - buf : count);
849 mutex_unlock(&intel_pstate_driver_lock);
850
851 return ret < 0 ? ret : count;
852}
853
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800854static ssize_t show_turbo_pct(struct kobject *kobj,
855 struct attribute *attr, char *buf)
856{
857 struct cpudata *cpu;
858 int total, no_turbo, turbo_pct;
859 uint32_t turbo_fp;
860
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100861 mutex_lock(&intel_pstate_driver_lock);
862
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200863 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100864 mutex_unlock(&intel_pstate_driver_lock);
865 return -EAGAIN;
866 }
867
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800868 cpu = all_cpu_data[0];
869
870 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
871 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200872 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800873 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100874
875 mutex_unlock(&intel_pstate_driver_lock);
876
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800877 return sprintf(buf, "%u\n", turbo_pct);
878}
879
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800880static ssize_t show_num_pstates(struct kobject *kobj,
881 struct attribute *attr, char *buf)
882{
883 struct cpudata *cpu;
884 int total;
885
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100886 mutex_lock(&intel_pstate_driver_lock);
887
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200888 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100889 mutex_unlock(&intel_pstate_driver_lock);
890 return -EAGAIN;
891 }
892
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800893 cpu = all_cpu_data[0];
894 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100895
896 mutex_unlock(&intel_pstate_driver_lock);
897
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800898 return sprintf(buf, "%u\n", total);
899}
900
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700901static ssize_t show_no_turbo(struct kobject *kobj,
902 struct attribute *attr, char *buf)
903{
904 ssize_t ret;
905
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100906 mutex_lock(&intel_pstate_driver_lock);
907
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200908 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100909 mutex_unlock(&intel_pstate_driver_lock);
910 return -EAGAIN;
911 }
912
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700913 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100914 if (global.turbo_disabled)
915 ret = sprintf(buf, "%u\n", global.turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700916 else
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100917 ret = sprintf(buf, "%u\n", global.no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700918
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100919 mutex_unlock(&intel_pstate_driver_lock);
920
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700921 return ret;
922}
923
Dirk Brandewie93f08222013-02-06 09:02:13 -0800924static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700925 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800926{
927 unsigned int input;
928 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700929
Dirk Brandewie93f08222013-02-06 09:02:13 -0800930 ret = sscanf(buf, "%u", &input);
931 if (ret != 1)
932 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700933
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100934 mutex_lock(&intel_pstate_driver_lock);
935
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200936 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100937 mutex_unlock(&intel_pstate_driver_lock);
938 return -EAGAIN;
939 }
940
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700941 mutex_lock(&intel_pstate_limits_lock);
942
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700943 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100944 if (global.turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -0700945 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700946 mutex_unlock(&intel_pstate_limits_lock);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100947 mutex_unlock(&intel_pstate_driver_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700948 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700949 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800950
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100951 global.no_turbo = clamp_t(int, input, 0, 1);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100952
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100953 if (global.no_turbo) {
954 struct cpudata *cpu = all_cpu_data[0];
955 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
956
957 /* Squash the global minimum into the permitted range. */
958 if (global.min_perf_pct > pct)
959 global.min_perf_pct = pct;
960 }
961
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +0100962 mutex_unlock(&intel_pstate_limits_lock);
963
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100964 intel_pstate_update_policies();
965
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100966 mutex_unlock(&intel_pstate_driver_lock);
967
Dirk Brandewie93f08222013-02-06 09:02:13 -0800968 return count;
969}
970
971static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700972 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800973{
974 unsigned int input;
975 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700976
Dirk Brandewie93f08222013-02-06 09:02:13 -0800977 ret = sscanf(buf, "%u", &input);
978 if (ret != 1)
979 return -EINVAL;
980
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100981 mutex_lock(&intel_pstate_driver_lock);
982
Rafael J. Wysockiee8df892017-03-28 00:13:00 +0200983 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100984 mutex_unlock(&intel_pstate_driver_lock);
985 return -EAGAIN;
986 }
987
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700988 mutex_lock(&intel_pstate_limits_lock);
989
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100990 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100991
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +0100992 mutex_unlock(&intel_pstate_limits_lock);
993
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100994 intel_pstate_update_policies();
995
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100996 mutex_unlock(&intel_pstate_driver_lock);
997
Dirk Brandewie93f08222013-02-06 09:02:13 -0800998 return count;
999}
1000
1001static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001002 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001003{
1004 unsigned int input;
1005 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001006
Dirk Brandewie93f08222013-02-06 09:02:13 -08001007 ret = sscanf(buf, "%u", &input);
1008 if (ret != 1)
1009 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001010
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001011 mutex_lock(&intel_pstate_driver_lock);
1012
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02001013 if (!intel_pstate_driver) {
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001014 mutex_unlock(&intel_pstate_driver_lock);
1015 return -EAGAIN;
1016 }
1017
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001018 mutex_lock(&intel_pstate_limits_lock);
1019
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001020 global.min_perf_pct = clamp_t(int, input,
1021 min_perf_pct_min(), global.max_perf_pct);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001022
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +01001023 mutex_unlock(&intel_pstate_limits_lock);
1024
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001025 intel_pstate_update_policies();
1026
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001027 mutex_unlock(&intel_pstate_driver_lock);
1028
Dirk Brandewie93f08222013-02-06 09:02:13 -08001029 return count;
1030}
1031
Dirk Brandewie93f08222013-02-06 09:02:13 -08001032show_one(max_perf_pct, max_perf_pct);
1033show_one(min_perf_pct, min_perf_pct);
1034
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001035define_one_global_rw(status);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001036define_one_global_rw(no_turbo);
1037define_one_global_rw(max_perf_pct);
1038define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001039define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001040define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001041
1042static struct attribute *intel_pstate_attributes[] = {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001043 &status.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001044 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001045 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001046 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001047 NULL
1048};
1049
Arvind Yadav106c9c72017-07-03 13:40:33 +05301050static const struct attribute_group intel_pstate_attr_group = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08001051 .attrs = intel_pstate_attributes,
1052};
Dirk Brandewie93f08222013-02-06 09:02:13 -08001053
Stratos Karafotis317dd502014-07-18 08:37:17 -07001054static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001055{
Stratos Karafotis317dd502014-07-18 08:37:17 -07001056 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001057 int rc;
1058
1059 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1060 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001061 if (WARN_ON(!intel_pstate_kobject))
1062 return;
1063
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -07001064 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001065 if (WARN_ON(rc))
1066 return;
1067
1068 /*
1069 * If per cpu limits are enforced there are no global limits, so
1070 * return without creating max/min_perf_pct attributes
1071 */
1072 if (per_cpu_limits)
1073 return;
1074
1075 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1076 WARN_ON(rc);
1077
1078 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1079 WARN_ON(rc);
1080
Dirk Brandewie93f08222013-02-06 09:02:13 -08001081}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001082/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001083
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001084static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001085{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001086 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -07001087 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1088 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001089
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001090 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001091 cpudata->epp_policy = 0;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001092 if (cpudata->epp_default == -EINVAL)
1093 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001094}
1095
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001096#define MSR_IA32_POWER_CTL_BIT_EE 19
1097
1098/* Disable energy efficiency optimization */
1099static void intel_pstate_disable_ee(int cpu)
1100{
1101 u64 power_ctl;
1102 int ret;
1103
1104 ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1105 if (ret)
1106 return;
1107
1108 if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1109 pr_info("Disabling energy efficiency optimization\n");
1110 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1111 wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1112 }
1113}
1114
Philippe Longepe938d21a2015-11-09 17:40:46 -08001115static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001116{
1117 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001118
Len Brown92134bd2017-02-25 16:55:17 -05001119 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001120 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001121}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001122
Philippe Longepe938d21a2015-11-09 17:40:46 -08001123static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001124{
1125 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001126
Len Brown92134bd2017-02-25 16:55:17 -05001127 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001128 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001129}
1130
Philippe Longepe938d21a2015-11-09 17:40:46 -08001131static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001132{
1133 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001134
Len Brown92134bd2017-02-25 16:55:17 -05001135 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001136 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001137}
1138
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001139static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001140{
1141 u64 val;
1142 int32_t vid_fp;
1143 u32 vid;
1144
Chen Yu144c8e12015-07-29 23:53:10 +08001145 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001146 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001147 val |= (u64)1 << 32;
1148
1149 vid_fp = cpudata->vid.min + mul_fp(
1150 int_tofp(pstate - cpudata->pstate.min_pstate),
1151 cpudata->vid.ratio);
1152
1153 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -07001154 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001155
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001156 if (pstate > cpudata->pstate.max_pstate)
1157 vid = cpudata->vid.turbo;
1158
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001159 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001160}
1161
Philippe Longepe1421df62015-11-09 17:40:47 -08001162static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001163{
1164 u64 value;
1165 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -08001166 /* Defined in Table 35-6 from SDM (Sept 2015) */
1167 static int silvermont_freq_table[] = {
1168 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001169
1170 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -08001171 i = value & 0x7;
1172 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001173
Philippe Longepe1421df62015-11-09 17:40:47 -08001174 return silvermont_freq_table[i];
1175}
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001176
Philippe Longepe1421df62015-11-09 17:40:47 -08001177static int airmont_get_scaling(void)
1178{
1179 u64 value;
1180 int i;
1181 /* Defined in Table 35-10 from SDM (Sept 2015) */
1182 static int airmont_freq_table[] = {
1183 83300, 100000, 133300, 116700, 80000,
1184 93300, 90000, 88900, 87500};
1185
1186 rdmsrl(MSR_FSB_FREQ, value);
1187 i = value & 0xF;
1188 WARN_ON(i > 8);
1189
1190 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001191}
1192
Philippe Longepe938d21a2015-11-09 17:40:46 -08001193static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001194{
1195 u64 value;
1196
Len Brown92134bd2017-02-25 16:55:17 -05001197 rdmsrl(MSR_ATOM_CORE_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001198 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1199 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001200 cpudata->vid.ratio = div_fp(
1201 cpudata->vid.max - cpudata->vid.min,
1202 int_tofp(cpudata->pstate.max_pstate -
1203 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001204
Len Brown92134bd2017-02-25 16:55:17 -05001205 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001206 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001207}
1208
Dirk Brandewie016c8152013-10-21 09:20:34 -07001209static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001210{
1211 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001212
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001213 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001214 return (value >> 40) & 0xFF;
1215}
1216
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001217static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001218{
1219 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001220
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001221 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001222 return (value >> 8) & 0xFF;
1223}
1224
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001225static int core_get_tdp_ratio(u64 plat_info)
1226{
1227 /* Check how many TDP levels present */
1228 if (plat_info & 0x600000000) {
1229 u64 tdp_ctrl;
1230 u64 tdp_ratio;
1231 int tdp_msr;
1232 int err;
1233
1234 /* Get the TDP level (0, 1, 2) to get ratios */
1235 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1236 if (err)
1237 return err;
1238
1239 /* TDP MSR are continuous starting at 0x648 */
1240 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1241 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1242 if (err)
1243 return err;
1244
1245 /* For level 1 and 2, bits[23:16] contain the ratio */
1246 if (tdp_ctrl & 0x03)
1247 tdp_ratio >>= 16;
1248
1249 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1250 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1251
1252 return (int)tdp_ratio;
1253 }
1254
1255 return -ENXIO;
1256}
1257
Dirk Brandewie93f08222013-02-06 09:02:13 -08001258static int core_get_max_pstate(void)
1259{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001260 u64 tar;
1261 u64 plat_info;
1262 int max_pstate;
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001263 int tdp_ratio;
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001264 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001265
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001266 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1267 max_pstate = (plat_info >> 8) & 0xFF;
1268
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001269 tdp_ratio = core_get_tdp_ratio(plat_info);
1270 if (tdp_ratio <= 0)
1271 return max_pstate;
1272
1273 if (hwp_active) {
1274 /* Turbo activation ratio is not used on HWP platforms */
1275 return tdp_ratio;
1276 }
1277
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001278 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1279 if (!err) {
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001280 int tar_levels;
1281
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001282 /* Do some sanity checking for safety */
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001283 tar_levels = tar & 0xff;
1284 if (tdp_ratio - 1 == tar_levels) {
1285 max_pstate = tar_levels;
1286 pr_debug("max_pstate=TAC %x\n", max_pstate);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001287 }
1288 }
1289
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001290 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001291}
1292
Dirk Brandewie016c8152013-10-21 09:20:34 -07001293static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001294{
1295 u64 value;
1296 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001297
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001298 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001299 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001300 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001301 if (ret <= nont)
1302 ret = nont;
1303 return ret;
1304}
1305
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001306static inline int core_get_scaling(void)
1307{
1308 return 100000;
1309}
1310
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001311static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001312{
1313 u64 val;
1314
Chen Yu144c8e12015-07-29 23:53:10 +08001315 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001316 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001317 val |= (u64)1 << 32;
1318
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001319 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001320}
1321
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001322static int knl_get_aperf_mperf_shift(void)
1323{
1324 return 10;
1325}
1326
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001327static int knl_get_turbo_pstate(void)
1328{
1329 u64 value;
1330 int nont, ret;
1331
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001332 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001333 nont = core_get_max_pstate();
1334 ret = (((value) >> 8) & 0xFF);
1335 if (ret <= nont)
1336 ret = nont;
1337 return ret;
1338}
1339
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001340static int intel_pstate_get_base_pstate(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001341{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001342 return global.no_turbo || global.turbo_disabled ?
1343 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001344}
1345
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001346static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001347{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001348 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1349 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001350 /*
1351 * Generally, there is no guarantee that this code will always run on
1352 * the CPU being updated, so force the register update to run on the
1353 * right CPU.
1354 */
1355 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1356 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001357}
1358
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001359static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1360{
1361 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1362}
1363
1364static void intel_pstate_max_within_limits(struct cpudata *cpu)
1365{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001366 int pstate;
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001367
1368 update_turbo_state();
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001369 pstate = intel_pstate_get_base_pstate(cpu);
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001370 pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001371 intel_pstate_set_pstate(cpu, pstate);
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001372}
1373
Dirk Brandewie93f08222013-02-06 09:02:13 -08001374static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1375{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001376 cpu->pstate.min_pstate = pstate_funcs.get_min();
1377 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001378 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001379 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001380 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001381 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1382 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001383
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001384 if (pstate_funcs.get_aperf_mperf_shift)
1385 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
1386
Dirk Brandewie007bea02013-12-18 10:32:39 -08001387 if (pstate_funcs.get_vid)
1388 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001389
1390 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001391}
1392
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001393/*
1394 * Long hold time will keep high perf limits for long time,
1395 * which negatively impacts perf/watt for some workloads,
1396 * like specpower. 3ms is based on experiements on some
1397 * workoads.
1398 */
1399static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
1400
1401static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
1402{
1403 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
1404 u32 max_limit = (hwp_req & 0xff00) >> 8;
1405 u32 min_limit = (hwp_req & 0xff);
1406 u32 boost_level1;
1407
1408 /*
1409 * Cases to consider (User changes via sysfs or boot time):
1410 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
1411 * No boost, return.
1412 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
1413 * Should result in one level boost only for P0.
1414 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
1415 * Should result in two level boost:
1416 * (min + p1)/2 and P1.
1417 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
1418 * Should result in three level boost:
1419 * (min + p1)/2, P1 and P0.
1420 */
1421
1422 /* If max and min are equal or already at max, nothing to boost */
1423 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
1424 return;
1425
1426 if (!cpu->hwp_boost_min)
1427 cpu->hwp_boost_min = min_limit;
1428
1429 /* level at half way mark between min and guranteed */
1430 boost_level1 = (HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) + min_limit) >> 1;
1431
1432 if (cpu->hwp_boost_min < boost_level1)
1433 cpu->hwp_boost_min = boost_level1;
1434 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1435 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(cpu->hwp_cap_cached);
1436 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) &&
1437 max_limit != HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1438 cpu->hwp_boost_min = max_limit;
1439 else
1440 return;
1441
1442 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
1443 wrmsrl(MSR_HWP_REQUEST, hwp_req);
1444 cpu->last_update = cpu->sample.time;
1445}
1446
1447static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
1448{
1449 if (cpu->hwp_boost_min) {
1450 bool expired;
1451
1452 /* Check if we are idle for hold time to boost down */
1453 expired = time_after64(cpu->sample.time, cpu->last_update +
1454 hwp_boost_hold_time_ns);
1455 if (expired) {
1456 wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
1457 cpu->hwp_boost_min = 0;
1458 }
1459 }
1460 cpu->last_update = cpu->sample.time;
1461}
1462
1463static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
1464 u64 time, unsigned int flags)
1465{
1466}
1467
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001468static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001469{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001470 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001471
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001472 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001473}
1474
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001475static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001476{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001477 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001478 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001479 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001480
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001481 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001482 rdmsrl(MSR_IA32_APERF, aperf);
1483 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001484 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001485 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001486 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001487 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001488 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001489 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001490
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001491 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001492 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001493 cpu->sample.aperf = aperf;
1494 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001495 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001496 cpu->sample.aperf -= cpu->prev_aperf;
1497 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001498 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001499
Dirk Brandewie93f08222013-02-06 09:02:13 -08001500 cpu->prev_aperf = aperf;
1501 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001502 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001503 /*
1504 * First time this function is invoked in a given cycle, all of the
1505 * previous sample data fields are equal to zero or stale and they must
1506 * be populated with meaningful numbers for things to work, so assume
1507 * that sample.time will always be reset before setting the utilization
1508 * update hook and make the caller skip the sample then.
1509 */
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001510 if (cpu->last_sample_time) {
1511 intel_pstate_calc_avg_perf(cpu);
1512 return true;
1513 }
1514 return false;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001515}
1516
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001517static inline int32_t get_avg_frequency(struct cpudata *cpu)
1518{
Doug Smythiesc587c792017-08-08 14:05:12 -07001519 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001520}
1521
Philippe Longepebdcaa232016-04-22 11:46:09 -07001522static inline int32_t get_avg_pstate(struct cpudata *cpu)
1523{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001524 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1525 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001526}
1527
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001528static inline int32_t get_target_pstate(struct cpudata *cpu)
Philippe Longepee70eed22015-12-04 17:40:32 +01001529{
1530 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001531 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001532 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001533
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001534 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
1535 sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001536
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001537 boost = cpu->iowait_boost;
1538 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001539
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001540 if (busy_frac < boost)
1541 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001542
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001543 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001544
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001545 target = global.no_turbo || global.turbo_disabled ?
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001546 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1547 target += target >> 2;
1548 target = mul_fp(target, busy_frac);
1549 if (target < cpu->pstate.min_pstate)
1550 target = cpu->pstate.min_pstate;
1551
1552 /*
1553 * If the average P-state during the previous cycle was higher than the
1554 * current target, add 50% of the difference to the target to reduce
1555 * possible performance oscillations and offset possible performance
1556 * loss related to moving the workload from one CPU to another within
1557 * a package/module.
1558 */
1559 avg_pstate = get_avg_pstate(cpu);
1560 if (avg_pstate > target)
1561 target += (avg_pstate - target) >> 1;
1562
1563 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001564}
1565
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001566static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001567{
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001568 int max_pstate = intel_pstate_get_base_pstate(cpu);
1569 int min_pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001570
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001571 min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1572 max_pstate = max(min_pstate, cpu->max_perf_ratio);
Rafael J. Wysockib02aabe2017-03-28 00:24:26 +02001573 return clamp_t(int, pstate, min_pstate, max_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001574}
1575
1576static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1577{
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001578 if (pstate == cpu->pstate.current_pstate)
1579 return;
1580
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001581 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001582 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1583}
1584
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001585static void intel_pstate_adjust_pstate(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001586{
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001587 int from = cpu->pstate.current_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001588 struct sample *sample;
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001589 int target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001590
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001591 update_turbo_state();
1592
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001593 target_pstate = get_target_pstate(cpu);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001594 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1595 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001596 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001597
1598 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001599 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001600 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001601 from,
1602 cpu->pstate.current_pstate,
1603 sample->mperf,
1604 sample->aperf,
1605 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001606 get_avg_frequency(cpu),
1607 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001608}
1609
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001610static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001611 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001612{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001613 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001614 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001615
Viresh Kumar674e7542017-07-28 12:16:38 +05301616 /* Don't allow remote callbacks */
1617 if (smp_processor_id() != cpu->cpu)
1618 return;
1619
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001620 if (flags & SCHED_CPUFREQ_IOWAIT) {
1621 cpu->iowait_boost = int_tofp(1);
Srinivas Pandruvada7bde2d52017-08-03 19:03:14 -07001622 cpu->last_update = time;
1623 /*
1624 * The last time the busy was 100% so P-state was max anyway
1625 * so avoid overhead of computation.
1626 */
1627 if (fp_toint(cpu->sample.busy_scaled) == 100)
1628 return;
1629
1630 goto set_pstate;
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001631 } else if (cpu->iowait_boost) {
1632 /* Clear iowait_boost if the CPU may have been idle. */
1633 delta_ns = time - cpu->last_update;
1634 if (delta_ns > TICK_NSEC)
1635 cpu->iowait_boost = 0;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001636 }
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001637 cpu->last_update = time;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001638 delta_ns = time - cpu->sample.time;
Rafael J. Wysockid77d4882017-08-10 01:09:16 +02001639 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001640 return;
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001641
Srinivas Pandruvada7bde2d52017-08-03 19:03:14 -07001642set_pstate:
Rafael J. Wysockia8912832017-08-10 01:08:56 +02001643 if (intel_pstate_sample(cpu, time))
1644 intel_pstate_adjust_pstate(cpu);
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001645}
Rafael J. Wysockieabd22c2017-03-28 00:15:37 +02001646
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001647static struct pstate_funcs core_funcs = {
1648 .get_max = core_get_max_pstate,
1649 .get_max_physical = core_get_max_pstate_physical,
1650 .get_min = core_get_min_pstate,
1651 .get_turbo = core_get_turbo_pstate,
1652 .get_scaling = core_get_scaling,
1653 .get_val = core_get_val,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001654};
1655
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001656static const struct pstate_funcs silvermont_funcs = {
1657 .get_max = atom_get_max_pstate,
1658 .get_max_physical = atom_get_max_pstate,
1659 .get_min = atom_get_min_pstate,
1660 .get_turbo = atom_get_turbo_pstate,
1661 .get_val = atom_get_val,
1662 .get_scaling = silvermont_get_scaling,
1663 .get_vid = atom_get_vid,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001664};
1665
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001666static const struct pstate_funcs airmont_funcs = {
1667 .get_max = atom_get_max_pstate,
1668 .get_max_physical = atom_get_max_pstate,
1669 .get_min = atom_get_min_pstate,
1670 .get_turbo = atom_get_turbo_pstate,
1671 .get_val = atom_get_val,
1672 .get_scaling = airmont_get_scaling,
1673 .get_vid = atom_get_vid,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001674};
1675
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001676static const struct pstate_funcs knl_funcs = {
1677 .get_max = core_get_max_pstate,
1678 .get_max_physical = core_get_max_pstate_physical,
1679 .get_min = core_get_min_pstate,
1680 .get_turbo = knl_get_turbo_pstate,
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07001681 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001682 .get_scaling = core_get_scaling,
1683 .get_val = core_get_val,
Rafael J. Wysockide4a76c2017-03-28 00:18:02 +02001684};
1685
Dirk Brandewie93f08222013-02-06 09:02:13 -08001686#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001687 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1688 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001689
1690static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001691 ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs),
1692 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs),
1693 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_funcs),
1694 ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs),
1695 ICPU(INTEL_FAM6_HASWELL_CORE, core_funcs),
1696 ICPU(INTEL_FAM6_BROADWELL_CORE, core_funcs),
1697 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs),
1698 ICPU(INTEL_FAM6_HASWELL_X, core_funcs),
1699 ICPU(INTEL_FAM6_HASWELL_ULT, core_funcs),
1700 ICPU(INTEL_FAM6_HASWELL_GT3E, core_funcs),
1701 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_funcs),
1702 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs),
1703 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_funcs),
1704 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1705 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs),
1706 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1707 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs),
1708 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs),
Srinivas Pandruvadadbd49b82018-01-10 11:38:51 -08001709 ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs),
1710 ICPU(INTEL_FAM6_ATOM_GEMINI_LAKE, core_funcs),
Srinivas Pandruvadad8de7a42018-01-10 11:38:52 -08001711 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001712 {}
1713};
1714MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1715
Jisheng Zhang29327c82016-06-27 18:07:17 +08001716static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001717 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1718 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1719 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001720 {}
1721};
1722
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001723static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02001724 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs),
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001725 {}
1726};
1727
Dirk Brandewie93f08222013-02-06 09:02:13 -08001728static int intel_pstate_init_cpu(unsigned int cpunum)
1729{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001730 struct cpudata *cpu;
1731
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001732 cpu = all_cpu_data[cpunum];
1733
1734 if (!cpu) {
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001735 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001736 if (!cpu)
1737 return -ENOMEM;
1738
1739 all_cpu_data[cpunum] = cpu;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001740
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001741 cpu->epp_default = -EINVAL;
1742 cpu->epp_powersave = -EINVAL;
1743 cpu->epp_saved = -EINVAL;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001744 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001745
1746 cpu = all_cpu_data[cpunum];
1747
Dirk Brandewie93f08222013-02-06 09:02:13 -08001748 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001749
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001750 if (hwp_active) {
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001751 const struct x86_cpu_id *id;
1752
1753 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1754 if (id)
1755 intel_pstate_disable_ee(cpunum);
1756
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001757 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001758 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001759
Vincent Minet179e8472014-07-05 01:51:33 +02001760 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001761
Joe Perches4836df12016-04-05 13:28:23 -07001762 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001763
1764 return 0;
1765}
1766
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001767static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001768{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001769 struct cpudata *cpu = all_cpu_data[cpu_num];
1770
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001771 if (hwp_active && !hwp_boost)
Len Brown62611cb2017-06-23 22:11:53 -07001772 return;
1773
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001774 if (cpu->update_util_set)
1775 return;
1776
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001777 /* Prevent intel_pstate_update_util() from using stale data. */
1778 cpu->sample.time = 0;
Rafael J. Wysocki67dd9bf2017-03-28 00:17:10 +02001779 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001780 (hwp_active ?
1781 intel_pstate_update_util_hwp :
1782 intel_pstate_update_util));
Chen Yu4578ee7e2016-05-11 14:33:08 +08001783 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001784}
1785
1786static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1787{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001788 struct cpudata *cpu_data = all_cpu_data[cpu];
1789
1790 if (!cpu_data->update_util_set)
1791 return;
1792
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001793 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001794 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001795 synchronize_sched();
1796}
1797
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001798static int intel_pstate_get_max_freq(struct cpudata *cpu)
1799{
1800 return global.turbo_disabled || global.no_turbo ?
1801 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1802}
1803
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001804static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001805 struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001806{
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001807 int max_freq = intel_pstate_get_max_freq(cpu);
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001808 int32_t max_policy_perf, min_policy_perf;
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001809 int max_state, turbo_max;
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001810
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001811 /*
1812 * HWP needs some special consideration, because on BDX the
1813 * HWP_REQUEST uses abstract value to represent performance
1814 * rather than pure ratios.
1815 */
1816 if (hwp_active) {
1817 intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
1818 } else {
1819 max_state = intel_pstate_get_base_pstate(cpu);
1820 turbo_max = cpu->pstate.turbo_pstate;
1821 }
1822
1823 max_policy_perf = max_state * policy->max / max_freq;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001824 if (policy->max == policy->min) {
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001825 min_policy_perf = max_policy_perf;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001826 } else {
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001827 min_policy_perf = max_state * policy->min / max_freq;
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001828 min_policy_perf = clamp_t(int32_t, min_policy_perf,
1829 0, max_policy_perf);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001830 }
Chen Yu43717aa2015-09-09 18:27:31 +08001831
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001832 pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
1833 policy->cpu, max_state,
1834 min_policy_perf, max_policy_perf);
1835
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01001836 /* Normalize user input to [min_perf, max_perf] */
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001837 if (per_cpu_limits) {
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001838 cpu->min_perf_ratio = min_policy_perf;
1839 cpu->max_perf_ratio = max_policy_perf;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001840 } else {
1841 int32_t global_min, global_max;
Chen Yu43717aa2015-09-09 18:27:31 +08001842
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001843 /* Global limits are in percent of the maximum turbo P-state. */
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001844 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
1845 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001846 global_min = clamp_t(int32_t, global_min, 0, global_max);
1847
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001848 pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
1849 global_min, global_max);
1850
1851 cpu->min_perf_ratio = max(min_policy_perf, global_min);
1852 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
1853 cpu->max_perf_ratio = min(max_policy_perf, global_max);
1854 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001855
1856 /* Make sure min_perf <= max_perf */
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001857 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
1858 cpu->max_perf_ratio);
1859
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001860 }
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001861 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
1862 cpu->max_perf_ratio,
1863 cpu->min_perf_ratio);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001864}
1865
Dirk Brandewie93f08222013-02-06 09:02:13 -08001866static int intel_pstate_set_policy(struct cpufreq_policy *policy)
Pali Rohár36b4bed2014-10-16 01:16:51 +02001867{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001868 struct cpudata *cpu;
1869
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001870 if (!policy->cpuinfo.max_freq)
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001871 return -ENODEV;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001872
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001873 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001874 policy->cpuinfo.max_freq, policy->max);
1875
1876 cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001877 cpu->policy = policy->policy;
1878
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001879 mutex_lock(&intel_pstate_limits_lock);
1880
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001881 intel_pstate_update_perf_limits(policy, cpu);
Rafael J. Wysockia240c4a2017-03-02 23:29:12 +01001882
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001883 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001884 /*
1885 * NOHZ_FULL CPUs need this as the governor callback may not
1886 * be invoked on them.
1887 */
1888 intel_pstate_clear_update_util_hook(policy->cpu);
1889 intel_pstate_max_within_limits(cpu);
Len Brown82b4e032017-06-23 22:11:54 -07001890 } else {
1891 intel_pstate_set_update_util_hook(policy->cpu);
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001892 }
1893
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001894 if (hwp_active) {
1895 /*
1896 * When hwp_boost was active before and dynamically it
1897 * was turned off, in that case we need to clear the
1898 * update util hook.
1899 */
1900 if (!hwp_boost)
1901 intel_pstate_clear_update_util_hook(policy->cpu);
Rafael J. Wysocki2bfc4cb2017-03-28 00:22:16 +02001902 intel_pstate_hwp_set(policy->cpu);
Srinivas Pandruvadae0efd5b2018-06-05 14:42:39 -07001903 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001904
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001905 mutex_unlock(&intel_pstate_limits_lock);
1906
Dirk Brandewie93f08222013-02-06 09:02:13 -08001907 return 0;
1908}
1909
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001910static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
1911 struct cpudata *cpu)
1912{
1913 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
1914 policy->max < policy->cpuinfo.max_freq &&
1915 policy->max > cpu->pstate.max_freq) {
1916 pr_debug("policy->max > max non turbo frequency\n");
1917 policy->max = policy->cpuinfo.max_freq;
1918 }
1919}
1920
Dirk Brandewie93f08222013-02-06 09:02:13 -08001921static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1922{
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08001923 struct cpudata *cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08001924
1925 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001926 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
1927 intel_pstate_get_max_freq(cpu));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001928
Stratos Karafotis285cb992014-07-18 08:37:21 -07001929 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001930 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001931 return -EINVAL;
1932
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01001933 intel_pstate_adjust_policy_max(policy, cpu);
1934
Dirk Brandewie93f08222013-02-06 09:02:13 -08001935 return 0;
1936}
1937
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001938static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001939{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001940 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001941}
1942
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001943static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
1944{
1945 pr_debug("CPU %d exiting\n", policy->cpu);
1946
1947 intel_pstate_clear_update_util_hook(policy->cpu);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001948 if (hwp_active)
1949 intel_pstate_hwp_save_state(policy);
1950 else
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001951 intel_cpufreq_stop_cpu(policy);
1952}
1953
1954static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1955{
1956 intel_pstate_exit_perf_limits(policy);
1957
1958 policy->fast_switch_possible = false;
1959
1960 return 0;
1961}
1962
1963static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001964{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001965 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07001966 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001967
1968 rc = intel_pstate_init_cpu(policy->cpu);
1969 if (rc)
1970 return rc;
1971
1972 cpu = all_cpu_data[policy->cpu];
1973
Srinivas Pandruvada1a4fe382017-06-12 16:30:27 -07001974 cpu->max_perf_ratio = 0xFF;
1975 cpu->min_perf_ratio = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001976
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001977 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1978 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001979
1980 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001981 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07001982 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001983 policy->cpuinfo.max_freq = global.turbo_disabled ?
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07001984 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1985 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
1986
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001987 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001988
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001989 policy->fast_switch_possible = true;
1990
Dirk Brandewie93f08222013-02-06 09:02:13 -08001991 return 0;
1992}
1993
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001994static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001995{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001996 int ret = __intel_pstate_cpu_init(policy);
1997
1998 if (ret)
1999 return ret;
2000
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01002001 if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002002 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2003 else
2004 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002005
2006 return 0;
2007}
2008
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002009static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08002010 .flags = CPUFREQ_CONST_LOOPS,
2011 .verify = intel_pstate_verify_policy,
2012 .setpolicy = intel_pstate_set_policy,
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002013 .suspend = intel_pstate_hwp_save_state,
Srinivas Pandruvada84428852016-11-24 16:07:10 -08002014 .resume = intel_pstate_resume,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002015 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002016 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07002017 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002018 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08002019};
2020
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002021static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2022{
2023 struct cpudata *cpu = all_cpu_data[policy->cpu];
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002024
2025 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002026 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2027 intel_pstate_get_max_freq(cpu));
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002028
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002029 intel_pstate_adjust_policy_max(policy, cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002030
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002031 intel_pstate_update_perf_limits(policy, cpu);
2032
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002033 return 0;
2034}
2035
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002036/* Use of trace in passive mode:
2037 *
2038 * In passive mode the trace core_busy field (also known as the
2039 * performance field, and lablelled as such on the graphs; also known as
2040 * core_avg_perf) is not needed and so is re-assigned to indicate if the
2041 * driver call was via the normal or fast switch path. Various graphs
2042 * output from the intel_pstate_tracer.py utility that include core_busy
2043 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
2044 * so we use 10 to indicate the the normal path through the driver, and
2045 * 90 to indicate the fast switch path through the driver.
2046 * The scaled_busy field is not used, and is set to 0.
2047 */
2048
2049#define INTEL_PSTATE_TRACE_TARGET 10
2050#define INTEL_PSTATE_TRACE_FAST_SWITCH 90
2051
2052static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
2053{
2054 struct sample *sample;
2055
2056 if (!trace_pstate_sample_enabled())
2057 return;
2058
2059 if (!intel_pstate_sample(cpu, ktime_get()))
2060 return;
2061
2062 sample = &cpu->sample;
2063 trace_pstate_sample(trace_type,
2064 0,
2065 old_pstate,
2066 cpu->pstate.current_pstate,
2067 sample->mperf,
2068 sample->aperf,
2069 sample->tsc,
2070 get_avg_frequency(cpu),
2071 fp_toint(cpu->iowait_boost * 100));
2072}
2073
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002074static int intel_cpufreq_target(struct cpufreq_policy *policy,
2075 unsigned int target_freq,
2076 unsigned int relation)
2077{
2078 struct cpudata *cpu = all_cpu_data[policy->cpu];
2079 struct cpufreq_freqs freqs;
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002080 int target_pstate, old_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002081
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002082 update_turbo_state();
2083
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002084 freqs.old = policy->cur;
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002085 freqs.new = target_freq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002086
2087 cpufreq_freq_transition_begin(policy, &freqs);
2088 switch (relation) {
2089 case CPUFREQ_RELATION_L:
2090 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2091 break;
2092 case CPUFREQ_RELATION_H:
2093 target_pstate = freqs.new / cpu->pstate.scaling;
2094 break;
2095 default:
2096 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2097 break;
2098 }
2099 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002100 old_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002101 if (target_pstate != cpu->pstate.current_pstate) {
2102 cpu->pstate.current_pstate = target_pstate;
2103 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2104 pstate_funcs.get_val(cpu, target_pstate));
2105 }
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002106 freqs.new = target_pstate * cpu->pstate.scaling;
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002107 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002108 cpufreq_freq_transition_end(policy, &freqs, false);
2109
2110 return 0;
2111}
2112
2113static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2114 unsigned int target_freq)
2115{
2116 struct cpudata *cpu = all_cpu_data[policy->cpu];
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002117 int target_pstate, old_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002118
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002119 update_turbo_state();
2120
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002121 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002122 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002123 old_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002124 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies50e9ffa2018-05-14 08:35:49 -07002125 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002126 return target_pstate * cpu->pstate.scaling;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002127}
2128
2129static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2130{
2131 int ret = __intel_pstate_cpu_init(policy);
2132
2133 if (ret)
2134 return ret;
2135
2136 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
Rafael J. Wysocki1b72e7f2017-04-11 00:20:41 +02002137 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002138 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2139 policy->cur = policy->cpuinfo.min_freq;
2140
2141 return 0;
2142}
2143
2144static struct cpufreq_driver intel_cpufreq = {
2145 .flags = CPUFREQ_CONST_LOOPS,
2146 .verify = intel_cpufreq_verify_policy,
2147 .target = intel_cpufreq_target,
2148 .fast_switch = intel_cpufreq_fast_switch,
2149 .init = intel_cpufreq_cpu_init,
2150 .exit = intel_pstate_cpu_exit,
2151 .stop_cpu = intel_cpufreq_stop_cpu,
2152 .name = "intel_cpufreq",
2153};
2154
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002155static struct cpufreq_driver *default_driver = &intel_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002156
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002157static void intel_pstate_driver_cleanup(void)
2158{
2159 unsigned int cpu;
2160
2161 get_online_cpus();
2162 for_each_online_cpu(cpu) {
2163 if (all_cpu_data[cpu]) {
2164 if (intel_pstate_driver == &intel_pstate)
2165 intel_pstate_clear_update_util_hook(cpu);
2166
2167 kfree(all_cpu_data[cpu]);
2168 all_cpu_data[cpu] = NULL;
2169 }
2170 }
2171 put_online_cpus();
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002172 intel_pstate_driver = NULL;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002173}
2174
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002175static int intel_pstate_register_driver(struct cpufreq_driver *driver)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002176{
2177 int ret;
2178
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002179 memset(&global, 0, sizeof(global));
2180 global.max_perf_pct = 100;
Rafael J. Wysockic3a49c82017-02-28 00:05:01 +01002181
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002182 intel_pstate_driver = driver;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002183 ret = cpufreq_register_driver(intel_pstate_driver);
2184 if (ret) {
2185 intel_pstate_driver_cleanup();
2186 return ret;
2187 }
2188
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002189 global.min_perf_pct = min_perf_pct_min();
2190
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002191 return 0;
2192}
2193
2194static int intel_pstate_unregister_driver(void)
2195{
2196 if (hwp_active)
2197 return -EBUSY;
2198
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002199 cpufreq_unregister_driver(intel_pstate_driver);
2200 intel_pstate_driver_cleanup();
2201
2202 return 0;
2203}
2204
2205static ssize_t intel_pstate_show_status(char *buf)
2206{
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002207 if (!intel_pstate_driver)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002208 return sprintf(buf, "off\n");
2209
2210 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2211 "active" : "passive");
2212}
2213
2214static int intel_pstate_update_status(const char *buf, size_t size)
2215{
2216 int ret;
2217
2218 if (size == 3 && !strncmp(buf, "off", size))
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002219 return intel_pstate_driver ?
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002220 intel_pstate_unregister_driver() : -EINVAL;
2221
2222 if (size == 6 && !strncmp(buf, "active", size)) {
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002223 if (intel_pstate_driver) {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002224 if (intel_pstate_driver == &intel_pstate)
2225 return 0;
2226
2227 ret = intel_pstate_unregister_driver();
2228 if (ret)
2229 return ret;
2230 }
2231
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002232 return intel_pstate_register_driver(&intel_pstate);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002233 }
2234
2235 if (size == 7 && !strncmp(buf, "passive", size)) {
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002236 if (intel_pstate_driver) {
Rafael J. Wysocki0042b2c2017-03-28 00:14:08 +02002237 if (intel_pstate_driver == &intel_cpufreq)
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002238 return 0;
2239
2240 ret = intel_pstate_unregister_driver();
2241 if (ret)
2242 return ret;
2243 }
2244
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002245 return intel_pstate_register_driver(&intel_cpufreq);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002246 }
2247
2248 return -EINVAL;
2249}
2250
Jisheng Zhangeed43602016-06-27 18:07:16 +08002251static int no_load __initdata;
2252static int no_hwp __initdata;
2253static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08002254static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01002255
Jisheng Zhang29327c82016-06-27 18:07:17 +08002256static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002257{
Dirk Brandewie016c8152013-10-21 09:20:34 -07002258 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07002259 !pstate_funcs.get_min() ||
2260 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002261 return -ENODEV;
2262
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002263 return 0;
2264}
Dirk Brandewie016c8152013-10-21 09:20:34 -07002265
Jisheng Zhang29327c82016-06-27 18:07:17 +08002266static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002267{
2268 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07002269 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002270 pstate_funcs.get_min = funcs->get_min;
2271 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002272 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01002273 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08002274 pstate_funcs.get_vid = funcs->get_vid;
Srinivas Pandruvada6e34e1f2017-07-13 15:03:51 -07002275 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002276}
2277
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002278#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002279
Jisheng Zhang29327c82016-06-27 18:07:17 +08002280static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002281{
2282 int i;
2283
2284 for_each_possible_cpu(i) {
2285 acpi_status status;
2286 union acpi_object *pss;
2287 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2288 struct acpi_processor *pr = per_cpu(processors, i);
2289
2290 if (!pr)
2291 continue;
2292
2293 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2294 if (ACPI_FAILURE(status))
2295 continue;
2296
2297 pss = buffer.pointer;
2298 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2299 kfree(pss);
2300 return false;
2301 }
2302
2303 kfree(pss);
2304 }
2305
2306 return true;
2307}
2308
Jisheng Zhang29327c82016-06-27 18:07:17 +08002309static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09002310{
2311 int i;
2312
2313 for_each_possible_cpu(i) {
2314 struct acpi_processor *pr = per_cpu(processors, i);
2315
2316 if (!pr)
2317 continue;
2318 if (acpi_has_method(pr->handle, "_PPC"))
2319 return true;
2320 }
2321 return false;
2322}
2323
2324enum {
2325 PSS,
2326 PPC,
2327};
2328
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002329/* Hardware vendor-specific info that has its own power management modes */
Toshi Kani5e932322017-08-23 16:54:44 -06002330static struct acpi_platform_list plat_info[] __initdata = {
2331 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS},
2332 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2333 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2334 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2335 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2336 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2337 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2338 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2339 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2340 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2341 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2342 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2343 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2344 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2345 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2346 { } /* End */
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002347};
2348
Jisheng Zhang29327c82016-06-27 18:07:17 +08002349static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002350{
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002351 const struct x86_cpu_id *id;
2352 u64 misc_pwr;
Toshi Kani5e932322017-08-23 16:54:44 -06002353 int idx;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002354
2355 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2356 if (id) {
2357 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2358 if ( misc_pwr & (1 << 8))
2359 return true;
2360 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002361
Toshi Kani5e932322017-08-23 16:54:44 -06002362 idx = acpi_match_platform_list(plat_info);
2363 if (idx < 0)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002364 return false;
2365
Toshi Kani5e932322017-08-23 16:54:44 -06002366 switch (plat_info[idx].data) {
2367 case PSS:
2368 return intel_pstate_no_acpi_pss();
2369 case PPC:
2370 return intel_pstate_has_acpi_ppc() && !force_load;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002371 }
2372
2373 return false;
2374}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002375
2376static void intel_pstate_request_control_from_smm(void)
2377{
2378 /*
2379 * It may be unsafe to request P-states control from SMM if _PPC support
2380 * has not been enabled.
2381 */
2382 if (acpi_ppc)
2383 acpi_processor_pstate_control();
2384}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002385#else /* CONFIG_ACPI not enabled */
2386static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002387static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002388static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002389#endif /* CONFIG_ACPI */
2390
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002391static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2392 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2393 {}
2394};
2395
Dirk Brandewie93f08222013-02-06 09:02:13 -08002396static int __init intel_pstate_init(void)
2397{
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002398 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002399
Dirk Brandewie6be26492013-02-15 22:55:10 +01002400 if (no_load)
2401 return -ENODEV;
2402
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002403 if (x86_match_cpu(hwp_support_ids)) {
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02002404 copy_cpu_funcs(&core_funcs);
Rafael J. Wysockic4f3f702017-07-25 00:12:20 +02002405 if (!no_hwp) {
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002406 hwp_active++;
2407 intel_pstate.attr = hwp_cpufreq_attrs;
2408 goto hwp_cpu_matched;
2409 }
2410 } else {
2411 const struct x86_cpu_id *id;
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002412
2413 id = x86_match_cpu(intel_pstate_cpu_ids);
2414 if (!id)
2415 return -ENODEV;
2416
Rafael J. Wysocki2f49afc2017-03-28 00:19:03 +02002417 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002418 }
2419
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002420 if (intel_pstate_msrs_not_valid())
2421 return -ENODEV;
2422
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002423hwp_cpu_matched:
2424 /*
2425 * The Intel pstate driver will be ignored if the platform
2426 * firmware has its own power management modes.
2427 */
2428 if (intel_pstate_platform_pwr_mgmt_exists())
2429 return -ENODEV;
2430
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002431 if (!hwp_active && hwp_only)
2432 return -ENOTSUPP;
2433
Joe Perches4836df12016-04-05 13:28:23 -07002434 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002435
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002436 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002437 if (!all_cpu_data)
2438 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002439
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002440 intel_pstate_request_control_from_smm();
2441
Dirk Brandewie93f08222013-02-06 09:02:13 -08002442 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002443
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002444 mutex_lock(&intel_pstate_driver_lock);
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002445 rc = intel_pstate_register_driver(default_driver);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002446 mutex_unlock(&intel_pstate_driver_lock);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002447 if (rc)
2448 return rc;
Dirk Brandewie907cc902013-03-05 14:15:27 -08002449
Dirk Brandewie907cc902013-03-05 14:15:27 -08002450 if (hwp_active)
2451 pr_info("HWP enabled\n");
2452
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002453 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002454}
2455device_initcall(intel_pstate_init);
2456
Dirk Brandewie6be26492013-02-15 22:55:10 +01002457static int __init intel_pstate_setup(char *str)
2458{
2459 if (!str)
2460 return -EINVAL;
2461
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002462 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002463 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002464 } else if (!strcmp(str, "passive")) {
2465 pr_info("Passive mode enabled\n");
Rafael J. Wysockiee8df892017-03-28 00:13:00 +02002466 default_driver = &intel_cpufreq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002467 no_hwp = 1;
2468 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002469 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002470 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002471 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002472 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002473 if (!strcmp(str, "force"))
2474 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002475 if (!strcmp(str, "hwp_only"))
2476 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002477 if (!strcmp(str, "per_cpu_perf_limits"))
2478 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002479
2480#ifdef CONFIG_ACPI
2481 if (!strcmp(str, "support_acpi_ppc"))
2482 acpi_ppc = true;
2483#endif
2484
Dirk Brandewie6be26492013-02-15 22:55:10 +01002485 return 0;
2486}
2487early_param("intel_pstate", intel_pstate_setup);
2488
Dirk Brandewie93f08222013-02-06 09:02:13 -08002489MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2490MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2491MODULE_LICENSE("GPL");