]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/cpufreq/intel_pstate.c
cpufreq: intel_pstate: Expose global sysfs attributes upfront
[mirror_ubuntu-bionic-kernel.git] / drivers / cpufreq / intel_pstate.c
CommitLineData
93f0822d 1/*
d1b68485 2 * intel_pstate.c: Native P state management for Intel processors
93f0822d
DB
3 *
4 * (C) Copyright 2012 Intel Corporation
5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 */
12
4836df17
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
93f0822d
DB
15#include <linux/kernel.h>
16#include <linux/kernel_stat.h>
17#include <linux/module.h>
18#include <linux/ktime.h>
19#include <linux/hrtimer.h>
20#include <linux/tick.h>
21#include <linux/slab.h>
22#include <linux/sched.h>
23#include <linux/list.h>
24#include <linux/cpu.h>
25#include <linux/cpufreq.h>
26#include <linux/sysfs.h>
27#include <linux/types.h>
28#include <linux/fs.h>
29#include <linux/debugfs.h>
fbbcdc07 30#include <linux/acpi.h>
d6472302 31#include <linux/vmalloc.h>
93f0822d
DB
32#include <trace/events/power.h>
33
34#include <asm/div64.h>
35#include <asm/msr.h>
36#include <asm/cpu_device_id.h>
64df1fdf 37#include <asm/cpufeature.h>
5b20c944 38#include <asm/intel-family.h>
93f0822d 39
001c76f0
RW
40#define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
41
938d21a2
PL
42#define ATOM_RATIOS 0x66a
43#define ATOM_VIDS 0x66b
44#define ATOM_TURBO_RATIOS 0x66c
45#define ATOM_TURBO_VIDS 0x66d
61d8d2ab 46
9522a2ff
SP
47#ifdef CONFIG_ACPI
48#include <acpi/processor.h>
17669006 49#include <acpi/cppc_acpi.h>
9522a2ff
SP
50#endif
51
f0fe3cd7 52#define FRAC_BITS 8
93f0822d
DB
53#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
54#define fp_toint(X) ((X) >> FRAC_BITS)
f0fe3cd7 55
a1c9787d
RW
56#define EXT_BITS 6
57#define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
d5dd33d9
SP
58#define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
59#define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
a1c9787d 60
93f0822d
DB
61static inline int32_t mul_fp(int32_t x, int32_t y)
62{
63 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
64}
65
7180dddf 66static inline int32_t div_fp(s64 x, s64 y)
93f0822d 67{
7180dddf 68 return div64_s64((int64_t)x << FRAC_BITS, y);
93f0822d
DB
69}
70
d022a65e
DB
71static inline int ceiling_fp(int32_t x)
72{
73 int mask, ret;
74
75 ret = fp_toint(x);
76 mask = (1 << FRAC_BITS) - 1;
77 if (x & mask)
78 ret += 1;
79 return ret;
80}
81
a1c9787d
RW
82static inline u64 mul_ext_fp(u64 x, u64 y)
83{
84 return (x * y) >> EXT_FRAC_BITS;
85}
86
87static inline u64 div_ext_fp(u64 x, u64 y)
88{
89 return div64_u64(x << EXT_FRAC_BITS, y);
90}
91
13ad7701
SP
92/**
93 * struct sample - Store performance sample
a1c9787d 94 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
13ad7701
SP
95 * performance during last sample period
96 * @busy_scaled: Scaled busy value which is used to calculate next
a1c9787d 97 * P state. This can be different than core_avg_perf
13ad7701
SP
98 * to account for cpu idle period
99 * @aperf: Difference of actual performance frequency clock count
100 * read from APERF MSR between last and current sample
101 * @mperf: Difference of maximum performance frequency clock count
102 * read from MPERF MSR between last and current sample
103 * @tsc: Difference of time stamp counter between last and
104 * current sample
13ad7701
SP
105 * @time: Current time from scheduler
106 *
107 * This structure is used in the cpudata structure to store performance sample
108 * data for choosing next P State.
109 */
93f0822d 110struct sample {
a1c9787d 111 int32_t core_avg_perf;
157386b6 112 int32_t busy_scaled;
93f0822d
DB
113 u64 aperf;
114 u64 mperf;
4055fad3 115 u64 tsc;
a4675fbc 116 u64 time;
93f0822d
DB
117};
118
13ad7701
SP
119/**
120 * struct pstate_data - Store P state data
121 * @current_pstate: Current requested P state
122 * @min_pstate: Min P state possible for this platform
123 * @max_pstate: Max P state possible for this platform
124 * @max_pstate_physical:This is physical Max P state for a processor
125 * This can be higher than the max_pstate which can
126 * be limited by platform thermal design power limits
127 * @scaling: Scaling factor to convert frequency to cpufreq
128 * frequency units
129 * @turbo_pstate: Max Turbo P state possible for this platform
001c76f0
RW
130 * @max_freq: @max_pstate frequency in cpufreq units
131 * @turbo_freq: @turbo_pstate frequency in cpufreq units
13ad7701
SP
132 *
133 * Stores the per cpu model P state limits and current P state.
134 */
93f0822d
DB
135struct pstate_data {
136 int current_pstate;
137 int min_pstate;
138 int max_pstate;
3bcc6fa9 139 int max_pstate_physical;
b27580b0 140 int scaling;
93f0822d 141 int turbo_pstate;
001c76f0
RW
142 unsigned int max_freq;
143 unsigned int turbo_freq;
93f0822d
DB
144};
145
13ad7701
SP
146/**
147 * struct vid_data - Stores voltage information data
148 * @min: VID data for this platform corresponding to
149 * the lowest P state
150 * @max: VID data corresponding to the highest P State.
151 * @turbo: VID data for turbo P state
152 * @ratio: Ratio of (vid max - vid min) /
153 * (max P state - Min P State)
154 *
155 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
156 * This data is used in Atom platforms, where in addition to target P state,
157 * the voltage data needs to be specified to select next P State.
158 */
007bea09 159struct vid_data {
21855ff5
DB
160 int min;
161 int max;
162 int turbo;
007bea09
DB
163 int32_t ratio;
164};
165
13ad7701
SP
166/**
167 * struct _pid - Stores PID data
168 * @setpoint: Target set point for busyness or performance
169 * @integral: Storage for accumulated error values
170 * @p_gain: PID proportional gain
171 * @i_gain: PID integral gain
172 * @d_gain: PID derivative gain
173 * @deadband: PID deadband
174 * @last_err: Last error storage for integral part of PID calculation
175 *
176 * Stores PID coefficients and last error for PID controller.
177 */
93f0822d
DB
178struct _pid {
179 int setpoint;
180 int32_t integral;
181 int32_t p_gain;
182 int32_t i_gain;
183 int32_t d_gain;
184 int deadband;
d253d2a5 185 int32_t last_err;
93f0822d
DB
186};
187
eae48f04
SP
188/**
189 * struct perf_limits - Store user and policy limits
190 * @no_turbo: User requested turbo state from intel_pstate sysfs
191 * @turbo_disabled: Platform turbo status either from msr
192 * MSR_IA32_MISC_ENABLE or when maximum available pstate
193 * matches the maximum turbo pstate
194 * @max_perf_pct: Effective maximum performance limit in percentage, this
195 * is minimum of either limits enforced by cpufreq policy
196 * or limits from user set limits via intel_pstate sysfs
197 * @min_perf_pct: Effective minimum performance limit in percentage, this
198 * is maximum of either limits enforced by cpufreq policy
199 * or limits from user set limits via intel_pstate sysfs
200 * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
201 * This value is used to limit max pstate
202 * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
203 * This value is used to limit min pstate
204 * @max_policy_pct: The maximum performance in percentage enforced by
205 * cpufreq setpolicy interface
206 * @max_sysfs_pct: The maximum performance in percentage enforced by
207 * intel pstate sysfs interface, unused when per cpu
208 * controls are enforced
209 * @min_policy_pct: The minimum performance in percentage enforced by
210 * cpufreq setpolicy interface
211 * @min_sysfs_pct: The minimum performance in percentage enforced by
212 * intel pstate sysfs interface, unused when per cpu
213 * controls are enforced
214 *
215 * Storage for user and policy defined limits.
216 */
217struct perf_limits {
218 int no_turbo;
219 int turbo_disabled;
220 int max_perf_pct;
221 int min_perf_pct;
222 int32_t max_perf;
223 int32_t min_perf;
224 int max_policy_pct;
225 int max_sysfs_pct;
226 int min_policy_pct;
227 int min_sysfs_pct;
228};
229
13ad7701
SP
230/**
231 * struct cpudata - Per CPU instance data storage
232 * @cpu: CPU number for this instance data
2f1d407a 233 * @policy: CPUFreq policy value
13ad7701 234 * @update_util: CPUFreq utility callback information
4578ee7e 235 * @update_util_set: CPUFreq utility callback is set
09c448d3
RW
236 * @iowait_boost: iowait-related boost fraction
237 * @last_update: Time of the last update.
13ad7701
SP
238 * @pstate: Stores P state limits for this CPU
239 * @vid: Stores VID limits for this CPU
240 * @pid: Stores PID parameters for this CPU
241 * @last_sample_time: Last Sample time
242 * @prev_aperf: Last APERF value read from APERF MSR
243 * @prev_mperf: Last MPERF value read from MPERF MSR
244 * @prev_tsc: Last timestamp counter (TSC) value
245 * @prev_cummulative_iowait: IO Wait time difference from last and
246 * current sample
247 * @sample: Storage for storing last Sample data
eae48f04
SP
248 * @perf_limits: Pointer to perf_limit unique to this CPU
249 * Not all field in the structure are applicable
250 * when per cpu controls are enforced
9522a2ff
SP
251 * @acpi_perf_data: Stores ACPI perf information read from _PSS
252 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
984edbdc
SP
253 * @epp_powersave: Last saved HWP energy performance preference
254 * (EPP) or energy performance bias (EPB),
255 * when policy switched to performance
8442885f 256 * @epp_policy: Last saved policy used to set EPP/EPB
984edbdc
SP
257 * @epp_default: Power on default HWP energy performance
258 * preference/bias
259 * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
260 * operation
13ad7701
SP
261 *
262 * This structure stores per CPU instance data for all CPUs.
263 */
93f0822d
DB
264struct cpudata {
265 int cpu;
266
2f1d407a 267 unsigned int policy;
a4675fbc 268 struct update_util_data update_util;
4578ee7e 269 bool update_util_set;
93f0822d 270
93f0822d 271 struct pstate_data pstate;
007bea09 272 struct vid_data vid;
93f0822d 273 struct _pid pid;
93f0822d 274
09c448d3 275 u64 last_update;
a4675fbc 276 u64 last_sample_time;
93f0822d
DB
277 u64 prev_aperf;
278 u64 prev_mperf;
4055fad3 279 u64 prev_tsc;
63d1d656 280 u64 prev_cummulative_iowait;
d37e2b76 281 struct sample sample;
eae48f04 282 struct perf_limits *perf_limits;
9522a2ff
SP
283#ifdef CONFIG_ACPI
284 struct acpi_processor_performance acpi_perf_data;
285 bool valid_pss_table;
286#endif
09c448d3 287 unsigned int iowait_boost;
984edbdc 288 s16 epp_powersave;
8442885f 289 s16 epp_policy;
984edbdc
SP
290 s16 epp_default;
291 s16 epp_saved;
93f0822d
DB
292};
293
294static struct cpudata **all_cpu_data;
13ad7701
SP
295
296/**
3954517e 297 * struct pstate_adjust_policy - Stores static PID configuration data
13ad7701
SP
298 * @sample_rate_ms: PID calculation sample rate in ms
299 * @sample_rate_ns: Sample rate calculation in ns
300 * @deadband: PID deadband
301 * @setpoint: PID Setpoint
302 * @p_gain_pct: PID proportional gain
303 * @i_gain_pct: PID integral gain
304 * @d_gain_pct: PID derivative gain
305 *
306 * Stores per CPU model static PID configuration data.
307 */
93f0822d
DB
308struct pstate_adjust_policy {
309 int sample_rate_ms;
a4675fbc 310 s64 sample_rate_ns;
93f0822d
DB
311 int deadband;
312 int setpoint;
313 int p_gain_pct;
314 int d_gain_pct;
315 int i_gain_pct;
316};
317
13ad7701
SP
318/**
319 * struct pstate_funcs - Per CPU model specific callbacks
320 * @get_max: Callback to get maximum non turbo effective P state
321 * @get_max_physical: Callback to get maximum non turbo physical P state
322 * @get_min: Callback to get minimum P state
323 * @get_turbo: Callback to get turbo P state
324 * @get_scaling: Callback to get frequency scaling factor
325 * @get_val: Callback to convert P state to actual MSR write value
326 * @get_vid: Callback to get VID data for Atom platforms
327 * @get_target_pstate: Callback to a function to calculate next P state to use
328 *
329 * Core and Atom CPU models have different way to get P State limits. This
330 * structure is used to store those callbacks.
331 */
016c8150
DB
332struct pstate_funcs {
333 int (*get_max)(void);
3bcc6fa9 334 int (*get_max_physical)(void);
016c8150
DB
335 int (*get_min)(void);
336 int (*get_turbo)(void);
b27580b0 337 int (*get_scaling)(void);
fdfdb2b1 338 u64 (*get_val)(struct cpudata*, int pstate);
007bea09 339 void (*get_vid)(struct cpudata *);
157386b6 340 int32_t (*get_target_pstate)(struct cpudata *);
93f0822d
DB
341};
342
13ad7701
SP
343/**
344 * struct cpu_defaults- Per CPU model default config data
345 * @pid_policy: PID config data
346 * @funcs: Callback function data
347 */
016c8150
DB
348struct cpu_defaults {
349 struct pstate_adjust_policy pid_policy;
350 struct pstate_funcs funcs;
93f0822d
DB
351};
352
157386b6 353static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
e70eed2b 354static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
157386b6 355
4a7cb7a9
JZ
356static struct pstate_adjust_policy pid_params __read_mostly;
357static struct pstate_funcs pstate_funcs __read_mostly;
358static int hwp_active __read_mostly;
eae48f04 359static bool per_cpu_limits __read_mostly;
016c8150 360
0c30b65b
RW
361static bool driver_registered __read_mostly;
362
9522a2ff
SP
363#ifdef CONFIG_ACPI
364static bool acpi_ppc;
365#endif
13ad7701 366
51443fbf
PB
367static struct perf_limits performance_limits = {
368 .no_turbo = 0,
369 .turbo_disabled = 0,
370 .max_perf_pct = 100,
d5dd33d9 371 .max_perf = int_ext_tofp(1),
51443fbf 372 .min_perf_pct = 100,
d5dd33d9 373 .min_perf = int_ext_tofp(1),
51443fbf
PB
374 .max_policy_pct = 100,
375 .max_sysfs_pct = 100,
376 .min_policy_pct = 0,
377 .min_sysfs_pct = 0,
378};
379
380static struct perf_limits powersave_limits = {
93f0822d 381 .no_turbo = 0,
4521e1a0 382 .turbo_disabled = 0,
93f0822d 383 .max_perf_pct = 100,
d5dd33d9 384 .max_perf = int_ext_tofp(1),
93f0822d
DB
385 .min_perf_pct = 0,
386 .min_perf = 0,
d8f469e9
DB
387 .max_policy_pct = 100,
388 .max_sysfs_pct = 100,
a0475992
KCA
389 .min_policy_pct = 0,
390 .min_sysfs_pct = 0,
93f0822d
DB
391};
392
51443fbf
PB
393#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
394static struct perf_limits *limits = &performance_limits;
395#else
396static struct perf_limits *limits = &powersave_limits;
397#endif
398
0c30b65b 399static DEFINE_MUTEX(intel_pstate_driver_lock);
a410c03d
SP
400static DEFINE_MUTEX(intel_pstate_limits_lock);
401
9522a2ff 402#ifdef CONFIG_ACPI
2b3ec765
SP
403
404static bool intel_pstate_get_ppc_enable_status(void)
405{
406 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
407 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
408 return true;
409
410 return acpi_ppc;
411}
412
17669006
RW
413#ifdef CONFIG_ACPI_CPPC_LIB
414
415/* The work item is needed to avoid CPU hotplug locking issues */
416static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
417{
418 sched_set_itmt_support();
419}
420
421static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
422
423static void intel_pstate_set_itmt_prio(int cpu)
424{
425 struct cppc_perf_caps cppc_perf;
426 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
427 int ret;
428
429 ret = cppc_get_perf_caps(cpu, &cppc_perf);
430 if (ret)
431 return;
432
433 /*
434 * The priorities can be set regardless of whether or not
435 * sched_set_itmt_support(true) has been called and it is valid to
436 * update them at any time after it has been called.
437 */
438 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
439
440 if (max_highest_perf <= min_highest_perf) {
441 if (cppc_perf.highest_perf > max_highest_perf)
442 max_highest_perf = cppc_perf.highest_perf;
443
444 if (cppc_perf.highest_perf < min_highest_perf)
445 min_highest_perf = cppc_perf.highest_perf;
446
447 if (max_highest_perf > min_highest_perf) {
448 /*
449 * This code can be run during CPU online under the
450 * CPU hotplug locks, so sched_set_itmt_support()
451 * cannot be called from here. Queue up a work item
452 * to invoke it.
453 */
454 schedule_work(&sched_itmt_work);
455 }
456 }
457}
458#else
459static void intel_pstate_set_itmt_prio(int cpu)
460{
461}
462#endif
463
9522a2ff
SP
464static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
465{
466 struct cpudata *cpu;
9522a2ff
SP
467 int ret;
468 int i;
469
17669006
RW
470 if (hwp_active) {
471 intel_pstate_set_itmt_prio(policy->cpu);
e59a8f7f 472 return;
17669006 473 }
e59a8f7f 474
2b3ec765 475 if (!intel_pstate_get_ppc_enable_status())
9522a2ff
SP
476 return;
477
478 cpu = all_cpu_data[policy->cpu];
479
480 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
481 policy->cpu);
482 if (ret)
483 return;
484
485 /*
486 * Check if the control value in _PSS is for PERF_CTL MSR, which should
487 * guarantee that the states returned by it map to the states in our
488 * list directly.
489 */
490 if (cpu->acpi_perf_data.control_register.space_id !=
491 ACPI_ADR_SPACE_FIXED_HARDWARE)
492 goto err;
493
494 /*
495 * If there is only one entry _PSS, simply ignore _PSS and continue as
496 * usual without taking _PSS into account
497 */
498 if (cpu->acpi_perf_data.state_count < 2)
499 goto err;
500
501 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
502 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
503 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
504 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
505 (u32) cpu->acpi_perf_data.states[i].core_frequency,
506 (u32) cpu->acpi_perf_data.states[i].power,
507 (u32) cpu->acpi_perf_data.states[i].control);
508 }
509
510 /*
511 * The _PSS table doesn't contain whole turbo frequency range.
512 * This just contains +1 MHZ above the max non turbo frequency,
513 * with control value corresponding to max turbo ratio. But
514 * when cpufreq set policy is called, it will call with this
515 * max frequency, which will cause a reduced performance as
516 * this driver uses real max turbo frequency as the max
517 * frequency. So correct this frequency in _PSS table to
b00345d1 518 * correct max turbo frequency based on the turbo state.
9522a2ff
SP
519 * Also need to convert to MHz as _PSS freq is in MHz.
520 */
b00345d1 521 if (!limits->turbo_disabled)
9522a2ff
SP
522 cpu->acpi_perf_data.states[0].core_frequency =
523 policy->cpuinfo.max_freq / 1000;
524 cpu->valid_pss_table = true;
6cacd115 525 pr_debug("_PPC limits will be enforced\n");
9522a2ff
SP
526
527 return;
528
529 err:
530 cpu->valid_pss_table = false;
531 acpi_processor_unregister_performance(policy->cpu);
532}
533
534static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
535{
536 struct cpudata *cpu;
537
538 cpu = all_cpu_data[policy->cpu];
539 if (!cpu->valid_pss_table)
540 return;
541
542 acpi_processor_unregister_performance(policy->cpu);
543}
544
545#else
7a3ba767 546static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
9522a2ff
SP
547{
548}
549
7a3ba767 550static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
9522a2ff
SP
551{
552}
553#endif
554
93f0822d 555static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
c410833a 556 int deadband, int integral) {
b54a0dfd
PL
557 pid->setpoint = int_tofp(setpoint);
558 pid->deadband = int_tofp(deadband);
93f0822d 559 pid->integral = int_tofp(integral);
d98d099b 560 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
93f0822d
DB
561}
562
563static inline void pid_p_gain_set(struct _pid *pid, int percent)
564{
22590efb 565 pid->p_gain = div_fp(percent, 100);
93f0822d
DB
566}
567
568static inline void pid_i_gain_set(struct _pid *pid, int percent)
569{
22590efb 570 pid->i_gain = div_fp(percent, 100);
93f0822d
DB
571}
572
573static inline void pid_d_gain_set(struct _pid *pid, int percent)
574{
22590efb 575 pid->d_gain = div_fp(percent, 100);
93f0822d
DB
576}
577
d253d2a5 578static signed int pid_calc(struct _pid *pid, int32_t busy)
93f0822d 579{
d253d2a5 580 signed int result;
93f0822d
DB
581 int32_t pterm, dterm, fp_error;
582 int32_t integral_limit;
583
b54a0dfd 584 fp_error = pid->setpoint - busy;
93f0822d 585
b54a0dfd 586 if (abs(fp_error) <= pid->deadband)
93f0822d
DB
587 return 0;
588
589 pterm = mul_fp(pid->p_gain, fp_error);
590
591 pid->integral += fp_error;
592
e0d4c8f8
KCA
593 /*
594 * We limit the integral here so that it will never
595 * get higher than 30. This prevents it from becoming
596 * too large an input over long periods of time and allows
597 * it to get factored out sooner.
598 *
599 * The value of 30 was chosen through experimentation.
600 */
93f0822d
DB
601 integral_limit = int_tofp(30);
602 if (pid->integral > integral_limit)
603 pid->integral = integral_limit;
604 if (pid->integral < -integral_limit)
605 pid->integral = -integral_limit;
606
d253d2a5
BS
607 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
608 pid->last_err = fp_error;
93f0822d
DB
609
610 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
51d211e9 611 result = result + (1 << (FRAC_BITS-1));
93f0822d
DB
612 return (signed int)fp_toint(result);
613}
614
615static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
616{
016c8150
DB
617 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
618 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
619 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
93f0822d 620
2d8d1f18 621 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
93f0822d
DB
622}
623
93f0822d
DB
624static inline void intel_pstate_reset_all_pid(void)
625{
626 unsigned int cpu;
845c1cbe 627
93f0822d
DB
628 for_each_online_cpu(cpu) {
629 if (all_cpu_data[cpu])
630 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
631 }
632}
633
4521e1a0
GM
634static inline void update_turbo_state(void)
635{
636 u64 misc_en;
637 struct cpudata *cpu;
638
639 cpu = all_cpu_data[0];
640 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
51443fbf 641 limits->turbo_disabled =
4521e1a0
GM
642 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
643 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
644}
645
8442885f
SP
646static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
647{
648 u64 epb;
649 int ret;
650
651 if (!static_cpu_has(X86_FEATURE_EPB))
652 return -ENXIO;
653
654 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
655 if (ret)
656 return (s16)ret;
657
658 return (s16)(epb & 0x0f);
659}
660
661static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
662{
663 s16 epp;
664
984edbdc
SP
665 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
666 /*
667 * When hwp_req_data is 0, means that caller didn't read
668 * MSR_HWP_REQUEST, so need to read and get EPP.
669 */
670 if (!hwp_req_data) {
671 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
672 &hwp_req_data);
673 if (epp)
674 return epp;
675 }
8442885f 676 epp = (hwp_req_data >> 24) & 0xff;
984edbdc 677 } else {
8442885f
SP
678 /* When there is no EPP present, HWP uses EPB settings */
679 epp = intel_pstate_get_epb(cpu_data);
984edbdc 680 }
8442885f
SP
681
682 return epp;
683}
684
984edbdc 685static int intel_pstate_set_epb(int cpu, s16 pref)
8442885f
SP
686{
687 u64 epb;
984edbdc 688 int ret;
8442885f
SP
689
690 if (!static_cpu_has(X86_FEATURE_EPB))
984edbdc 691 return -ENXIO;
8442885f 692
984edbdc
SP
693 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
694 if (ret)
695 return ret;
8442885f
SP
696
697 epb = (epb & ~0x0f) | pref;
698 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
984edbdc
SP
699
700 return 0;
8442885f
SP
701}
702
984edbdc
SP
703/*
704 * EPP/EPB display strings corresponding to EPP index in the
705 * energy_perf_strings[]
706 * index String
707 *-------------------------------------
708 * 0 default
709 * 1 performance
710 * 2 balance_performance
711 * 3 balance_power
712 * 4 power
713 */
714static const char * const energy_perf_strings[] = {
715 "default",
716 "performance",
717 "balance_performance",
718 "balance_power",
719 "power",
720 NULL
721};
722
723static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
724{
725 s16 epp;
726 int index = -EINVAL;
727
728 epp = intel_pstate_get_epp(cpu_data, 0);
729 if (epp < 0)
730 return epp;
731
732 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
733 /*
734 * Range:
735 * 0x00-0x3F : Performance
736 * 0x40-0x7F : Balance performance
737 * 0x80-0xBF : Balance power
738 * 0xC0-0xFF : Power
739 * The EPP is a 8 bit value, but our ranges restrict the
740 * value which can be set. Here only using top two bits
741 * effectively.
742 */
743 index = (epp >> 6) + 1;
744 } else if (static_cpu_has(X86_FEATURE_EPB)) {
745 /*
746 * Range:
747 * 0x00-0x03 : Performance
748 * 0x04-0x07 : Balance performance
749 * 0x08-0x0B : Balance power
750 * 0x0C-0x0F : Power
751 * The EPB is a 4 bit value, but our ranges restrict the
752 * value which can be set. Here only using top two bits
753 * effectively.
754 */
755 index = (epp >> 2) + 1;
756 }
757
758 return index;
759}
760
761static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
762 int pref_index)
763{
764 int epp = -EINVAL;
765 int ret;
766
767 if (!pref_index)
768 epp = cpu_data->epp_default;
769
770 mutex_lock(&intel_pstate_limits_lock);
771
772 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
773 u64 value;
774
775 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
776 if (ret)
777 goto return_pref;
778
779 value &= ~GENMASK_ULL(31, 24);
780
781 /*
782 * If epp is not default, convert from index into
783 * energy_perf_strings to epp value, by shifting 6
784 * bits left to use only top two bits in epp.
785 * The resultant epp need to shifted by 24 bits to
786 * epp position in MSR_HWP_REQUEST.
787 */
788 if (epp == -EINVAL)
789 epp = (pref_index - 1) << 6;
790
791 value |= (u64)epp << 24;
792 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
793 } else {
794 if (epp == -EINVAL)
795 epp = (pref_index - 1) << 2;
796 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
797 }
798return_pref:
799 mutex_unlock(&intel_pstate_limits_lock);
800
801 return ret;
802}
803
804static ssize_t show_energy_performance_available_preferences(
805 struct cpufreq_policy *policy, char *buf)
806{
807 int i = 0;
808 int ret = 0;
809
810 while (energy_perf_strings[i] != NULL)
811 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
812
813 ret += sprintf(&buf[ret], "\n");
814
815 return ret;
816}
817
818cpufreq_freq_attr_ro(energy_performance_available_preferences);
819
820static ssize_t store_energy_performance_preference(
821 struct cpufreq_policy *policy, const char *buf, size_t count)
822{
823 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
824 char str_preference[21];
825 int ret, i = 0;
826
827 ret = sscanf(buf, "%20s", str_preference);
828 if (ret != 1)
829 return -EINVAL;
830
831 while (energy_perf_strings[i] != NULL) {
832 if (!strcmp(str_preference, energy_perf_strings[i])) {
833 intel_pstate_set_energy_pref_index(cpu_data, i);
834 return count;
835 }
836 ++i;
837 }
838
839 return -EINVAL;
840}
841
842static ssize_t show_energy_performance_preference(
843 struct cpufreq_policy *policy, char *buf)
844{
845 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
846 int preference;
847
848 preference = intel_pstate_get_energy_pref_index(cpu_data);
849 if (preference < 0)
850 return preference;
851
852 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
853}
854
855cpufreq_freq_attr_rw(energy_performance_preference);
856
857static struct freq_attr *hwp_cpufreq_attrs[] = {
858 &energy_performance_preference,
859 &energy_performance_available_preferences,
860 NULL,
861};
862
111b8b3f 863static void intel_pstate_hwp_set(struct cpufreq_policy *policy)
2f86dc4c 864{
74da56ce 865 int min, hw_min, max, hw_max, cpu, range, adj_range;
eae48f04 866 struct perf_limits *perf_limits = limits;
74da56ce
KCA
867 u64 value, cap;
868
111b8b3f 869 for_each_cpu(cpu, policy->cpus) {
eae48f04 870 int max_perf_pct, min_perf_pct;
8442885f
SP
871 struct cpudata *cpu_data = all_cpu_data[cpu];
872 s16 epp;
eae48f04
SP
873
874 if (per_cpu_limits)
875 perf_limits = all_cpu_data[cpu]->perf_limits;
876
f9f4872d
SP
877 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
878 hw_min = HWP_LOWEST_PERF(cap);
879 hw_max = HWP_HIGHEST_PERF(cap);
880 range = hw_max - hw_min;
881
eae48f04
SP
882 max_perf_pct = perf_limits->max_perf_pct;
883 min_perf_pct = perf_limits->min_perf_pct;
884
2f86dc4c 885 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
eae48f04 886 adj_range = min_perf_pct * range / 100;
74da56ce 887 min = hw_min + adj_range;
2f86dc4c
DB
888 value &= ~HWP_MIN_PERF(~0L);
889 value |= HWP_MIN_PERF(min);
890
eae48f04 891 adj_range = max_perf_pct * range / 100;
74da56ce 892 max = hw_min + adj_range;
51443fbf 893 if (limits->no_turbo) {
74da56ce
KCA
894 hw_max = HWP_GUARANTEED_PERF(cap);
895 if (hw_max < max)
896 max = hw_max;
2f86dc4c
DB
897 }
898
899 value &= ~HWP_MAX_PERF(~0L);
900 value |= HWP_MAX_PERF(max);
8442885f
SP
901
902 if (cpu_data->epp_policy == cpu_data->policy)
903 goto skip_epp;
904
905 cpu_data->epp_policy = cpu_data->policy;
906
984edbdc
SP
907 if (cpu_data->epp_saved >= 0) {
908 epp = cpu_data->epp_saved;
909 cpu_data->epp_saved = -EINVAL;
910 goto update_epp;
911 }
912
8442885f
SP
913 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
914 epp = intel_pstate_get_epp(cpu_data, value);
984edbdc 915 cpu_data->epp_powersave = epp;
8442885f 916 /* If EPP read was failed, then don't try to write */
984edbdc 917 if (epp < 0)
8442885f 918 goto skip_epp;
8442885f 919
8442885f
SP
920
921 epp = 0;
922 } else {
923 /* skip setting EPP, when saved value is invalid */
984edbdc 924 if (cpu_data->epp_powersave < 0)
8442885f
SP
925 goto skip_epp;
926
927 /*
928 * No need to restore EPP when it is not zero. This
929 * means:
930 * - Policy is not changed
931 * - user has manually changed
932 * - Error reading EPB
933 */
934 epp = intel_pstate_get_epp(cpu_data, value);
935 if (epp)
936 goto skip_epp;
937
984edbdc 938 epp = cpu_data->epp_powersave;
8442885f 939 }
984edbdc 940update_epp:
8442885f
SP
941 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
942 value &= ~GENMASK_ULL(31, 24);
943 value |= (u64)epp << 24;
944 } else {
945 intel_pstate_set_epb(cpu, epp);
946 }
947skip_epp:
2f86dc4c
DB
948 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
949 }
41cfd64c 950}
2f86dc4c 951
ba41e1bc
RW
952static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
953{
954 if (hwp_active)
111b8b3f 955 intel_pstate_hwp_set(policy);
ba41e1bc
RW
956
957 return 0;
958}
959
984edbdc
SP
960static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
961{
962 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
963
964 if (!hwp_active)
965 return 0;
966
967 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
968
969 return 0;
970}
971
8442885f
SP
972static int intel_pstate_resume(struct cpufreq_policy *policy)
973{
aa439248
RW
974 int ret;
975
8442885f
SP
976 if (!hwp_active)
977 return 0;
978
aa439248
RW
979 mutex_lock(&intel_pstate_limits_lock);
980
8442885f 981 all_cpu_data[policy->cpu]->epp_policy = 0;
8442885f 982
aa439248
RW
983 ret = intel_pstate_hwp_set_policy(policy);
984
985 mutex_unlock(&intel_pstate_limits_lock);
986
987 return ret;
8442885f
SP
988}
989
111b8b3f 990static void intel_pstate_update_policies(void)
41cfd64c 991{
111b8b3f
RW
992 int cpu;
993
994 for_each_possible_cpu(cpu)
995 cpufreq_update_policy(cpu);
2f86dc4c
DB
996}
997
93f0822d
DB
998/************************** debugfs begin ************************/
999static int pid_param_set(void *data, u64 val)
1000{
1001 *(u32 *)data = val;
1002 intel_pstate_reset_all_pid();
1003 return 0;
1004}
845c1cbe 1005
93f0822d
DB
1006static int pid_param_get(void *data, u64 *val)
1007{
1008 *val = *(u32 *)data;
1009 return 0;
1010}
2d8d1f18 1011DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
93f0822d
DB
1012
1013struct pid_param {
1014 char *name;
1015 void *value;
1016};
1017
1018static struct pid_param pid_files[] = {
016c8150
DB
1019 {"sample_rate_ms", &pid_params.sample_rate_ms},
1020 {"d_gain_pct", &pid_params.d_gain_pct},
1021 {"i_gain_pct", &pid_params.i_gain_pct},
1022 {"deadband", &pid_params.deadband},
1023 {"setpoint", &pid_params.setpoint},
1024 {"p_gain_pct", &pid_params.p_gain_pct},
93f0822d
DB
1025 {NULL, NULL}
1026};
1027
317dd50e 1028static void __init intel_pstate_debug_expose_params(void)
93f0822d 1029{
317dd50e 1030 struct dentry *debugfs_parent;
93f0822d
DB
1031 int i = 0;
1032
1033 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
1034 if (IS_ERR_OR_NULL(debugfs_parent))
1035 return;
1036 while (pid_files[i].name) {
1037 debugfs_create_file(pid_files[i].name, 0660,
c410833a
SK
1038 debugfs_parent, pid_files[i].value,
1039 &fops_pid_param);
93f0822d
DB
1040 i++;
1041 }
1042}
1043
1044/************************** debugfs end ************************/
1045
1046/************************** sysfs begin ************************/
1047#define show_one(file_name, object) \
1048 static ssize_t show_##file_name \
1049 (struct kobject *kobj, struct attribute *attr, char *buf) \
1050 { \
51443fbf 1051 return sprintf(buf, "%u\n", limits->object); \
93f0822d
DB
1052 }
1053
d01b1f48
KCA
1054static ssize_t show_turbo_pct(struct kobject *kobj,
1055 struct attribute *attr, char *buf)
1056{
1057 struct cpudata *cpu;
1058 int total, no_turbo, turbo_pct;
1059 uint32_t turbo_fp;
1060
0c30b65b
RW
1061 mutex_lock(&intel_pstate_driver_lock);
1062
1063 if (!driver_registered) {
1064 mutex_unlock(&intel_pstate_driver_lock);
1065 return -EAGAIN;
1066 }
1067
d01b1f48
KCA
1068 cpu = all_cpu_data[0];
1069
1070 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1071 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
22590efb 1072 turbo_fp = div_fp(no_turbo, total);
d01b1f48 1073 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
0c30b65b
RW
1074
1075 mutex_unlock(&intel_pstate_driver_lock);
1076
d01b1f48
KCA
1077 return sprintf(buf, "%u\n", turbo_pct);
1078}
1079
0522424e
KCA
1080static ssize_t show_num_pstates(struct kobject *kobj,
1081 struct attribute *attr, char *buf)
1082{
1083 struct cpudata *cpu;
1084 int total;
1085
0c30b65b
RW
1086 mutex_lock(&intel_pstate_driver_lock);
1087
1088 if (!driver_registered) {
1089 mutex_unlock(&intel_pstate_driver_lock);
1090 return -EAGAIN;
1091 }
1092
0522424e
KCA
1093 cpu = all_cpu_data[0];
1094 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
0c30b65b
RW
1095
1096 mutex_unlock(&intel_pstate_driver_lock);
1097
0522424e
KCA
1098 return sprintf(buf, "%u\n", total);
1099}
1100
4521e1a0
GM
1101static ssize_t show_no_turbo(struct kobject *kobj,
1102 struct attribute *attr, char *buf)
1103{
1104 ssize_t ret;
1105
0c30b65b
RW
1106 mutex_lock(&intel_pstate_driver_lock);
1107
1108 if (!driver_registered) {
1109 mutex_unlock(&intel_pstate_driver_lock);
1110 return -EAGAIN;
1111 }
1112
4521e1a0 1113 update_turbo_state();
51443fbf
PB
1114 if (limits->turbo_disabled)
1115 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
4521e1a0 1116 else
51443fbf 1117 ret = sprintf(buf, "%u\n", limits->no_turbo);
4521e1a0 1118
0c30b65b
RW
1119 mutex_unlock(&intel_pstate_driver_lock);
1120
4521e1a0
GM
1121 return ret;
1122}
1123
93f0822d 1124static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
c410833a 1125 const char *buf, size_t count)
93f0822d
DB
1126{
1127 unsigned int input;
1128 int ret;
845c1cbe 1129
93f0822d
DB
1130 ret = sscanf(buf, "%u", &input);
1131 if (ret != 1)
1132 return -EINVAL;
4521e1a0 1133
0c30b65b
RW
1134 mutex_lock(&intel_pstate_driver_lock);
1135
1136 if (!driver_registered) {
1137 mutex_unlock(&intel_pstate_driver_lock);
1138 return -EAGAIN;
1139 }
1140
a410c03d
SP
1141 mutex_lock(&intel_pstate_limits_lock);
1142
4521e1a0 1143 update_turbo_state();
51443fbf 1144 if (limits->turbo_disabled) {
4836df17 1145 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
a410c03d 1146 mutex_unlock(&intel_pstate_limits_lock);
0c30b65b 1147 mutex_unlock(&intel_pstate_driver_lock);
4521e1a0 1148 return -EPERM;
dd5fbf70 1149 }
2f86dc4c 1150
51443fbf 1151 limits->no_turbo = clamp_t(int, input, 0, 1);
4521e1a0 1152
b59fe540
SP
1153 mutex_unlock(&intel_pstate_limits_lock);
1154
111b8b3f
RW
1155 intel_pstate_update_policies();
1156
0c30b65b
RW
1157 mutex_unlock(&intel_pstate_driver_lock);
1158
93f0822d
DB
1159 return count;
1160}
1161
1162static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
c410833a 1163 const char *buf, size_t count)
93f0822d
DB
1164{
1165 unsigned int input;
1166 int ret;
845c1cbe 1167
93f0822d
DB
1168 ret = sscanf(buf, "%u", &input);
1169 if (ret != 1)
1170 return -EINVAL;
1171
0c30b65b
RW
1172 mutex_lock(&intel_pstate_driver_lock);
1173
1174 if (!driver_registered) {
1175 mutex_unlock(&intel_pstate_driver_lock);
1176 return -EAGAIN;
1177 }
1178
a410c03d
SP
1179 mutex_lock(&intel_pstate_limits_lock);
1180
51443fbf
PB
1181 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
1182 limits->max_perf_pct = min(limits->max_policy_pct,
1183 limits->max_sysfs_pct);
1184 limits->max_perf_pct = max(limits->min_policy_pct,
1185 limits->max_perf_pct);
1186 limits->max_perf_pct = max(limits->min_perf_pct,
1187 limits->max_perf_pct);
d5dd33d9 1188 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
845c1cbe 1189
b59fe540
SP
1190 mutex_unlock(&intel_pstate_limits_lock);
1191
111b8b3f
RW
1192 intel_pstate_update_policies();
1193
0c30b65b
RW
1194 mutex_unlock(&intel_pstate_driver_lock);
1195
93f0822d
DB
1196 return count;
1197}
1198
1199static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
c410833a 1200 const char *buf, size_t count)
93f0822d
DB
1201{
1202 unsigned int input;
1203 int ret;
845c1cbe 1204
93f0822d
DB
1205 ret = sscanf(buf, "%u", &input);
1206 if (ret != 1)
1207 return -EINVAL;
a0475992 1208
0c30b65b
RW
1209 mutex_lock(&intel_pstate_driver_lock);
1210
1211 if (!driver_registered) {
1212 mutex_unlock(&intel_pstate_driver_lock);
1213 return -EAGAIN;
1214 }
1215
a410c03d
SP
1216 mutex_lock(&intel_pstate_limits_lock);
1217
51443fbf
PB
1218 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
1219 limits->min_perf_pct = max(limits->min_policy_pct,
1220 limits->min_sysfs_pct);
1221 limits->min_perf_pct = min(limits->max_policy_pct,
1222 limits->min_perf_pct);
1223 limits->min_perf_pct = min(limits->max_perf_pct,
1224 limits->min_perf_pct);
d5dd33d9 1225 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
93f0822d 1226
b59fe540
SP
1227 mutex_unlock(&intel_pstate_limits_lock);
1228
111b8b3f
RW
1229 intel_pstate_update_policies();
1230
0c30b65b
RW
1231 mutex_unlock(&intel_pstate_driver_lock);
1232
93f0822d
DB
1233 return count;
1234}
1235
93f0822d
DB
1236show_one(max_perf_pct, max_perf_pct);
1237show_one(min_perf_pct, min_perf_pct);
1238
1239define_one_global_rw(no_turbo);
1240define_one_global_rw(max_perf_pct);
1241define_one_global_rw(min_perf_pct);
d01b1f48 1242define_one_global_ro(turbo_pct);
0522424e 1243define_one_global_ro(num_pstates);
93f0822d
DB
1244
1245static struct attribute *intel_pstate_attributes[] = {
1246 &no_turbo.attr,
d01b1f48 1247 &turbo_pct.attr,
0522424e 1248 &num_pstates.attr,
93f0822d
DB
1249 NULL
1250};
1251
1252static struct attribute_group intel_pstate_attr_group = {
1253 .attrs = intel_pstate_attributes,
1254};
93f0822d 1255
317dd50e 1256static void __init intel_pstate_sysfs_expose_params(void)
93f0822d 1257{
317dd50e 1258 struct kobject *intel_pstate_kobject;
93f0822d
DB
1259 int rc;
1260
1261 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1262 &cpu_subsys.dev_root->kobj);
eae48f04
SP
1263 if (WARN_ON(!intel_pstate_kobject))
1264 return;
1265
2d8d1f18 1266 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
eae48f04
SP
1267 if (WARN_ON(rc))
1268 return;
1269
1270 /*
1271 * If per cpu limits are enforced there are no global limits, so
1272 * return without creating max/min_perf_pct attributes
1273 */
1274 if (per_cpu_limits)
1275 return;
1276
1277 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1278 WARN_ON(rc);
1279
1280 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1281 WARN_ON(rc);
1282
93f0822d 1283}
93f0822d 1284/************************** sysfs end ************************/
2f86dc4c 1285
ba88d433 1286static void intel_pstate_hwp_enable(struct cpudata *cpudata)
2f86dc4c 1287{
f05c9665 1288 /* First disable HWP notification interrupt as we don't process them */
da7de91c
SP
1289 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1290 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
f05c9665 1291
ba88d433 1292 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
8442885f 1293 cpudata->epp_policy = 0;
984edbdc
SP
1294 if (cpudata->epp_default == -EINVAL)
1295 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
2f86dc4c
DB
1296}
1297
938d21a2 1298static int atom_get_min_pstate(void)
19e77c28
DB
1299{
1300 u64 value;
845c1cbe 1301
938d21a2 1302 rdmsrl(ATOM_RATIOS, value);
c16ed060 1303 return (value >> 8) & 0x7F;
19e77c28
DB
1304}
1305
938d21a2 1306static int atom_get_max_pstate(void)
19e77c28
DB
1307{
1308 u64 value;
845c1cbe 1309
938d21a2 1310 rdmsrl(ATOM_RATIOS, value);
c16ed060 1311 return (value >> 16) & 0x7F;
19e77c28 1312}
93f0822d 1313
938d21a2 1314static int atom_get_turbo_pstate(void)
61d8d2ab
DB
1315{
1316 u64 value;
845c1cbe 1317
938d21a2 1318 rdmsrl(ATOM_TURBO_RATIOS, value);
c16ed060 1319 return value & 0x7F;
61d8d2ab
DB
1320}
1321
fdfdb2b1 1322static u64 atom_get_val(struct cpudata *cpudata, int pstate)
007bea09
DB
1323{
1324 u64 val;
1325 int32_t vid_fp;
1326 u32 vid;
1327
144c8e17 1328 val = (u64)pstate << 8;
51443fbf 1329 if (limits->no_turbo && !limits->turbo_disabled)
007bea09
DB
1330 val |= (u64)1 << 32;
1331
1332 vid_fp = cpudata->vid.min + mul_fp(
1333 int_tofp(pstate - cpudata->pstate.min_pstate),
1334 cpudata->vid.ratio);
1335
1336 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
d022a65e 1337 vid = ceiling_fp(vid_fp);
007bea09 1338
21855ff5
DB
1339 if (pstate > cpudata->pstate.max_pstate)
1340 vid = cpudata->vid.turbo;
1341
fdfdb2b1 1342 return val | vid;
007bea09
DB
1343}
1344
1421df63 1345static int silvermont_get_scaling(void)
b27580b0
DB
1346{
1347 u64 value;
1348 int i;
1421df63
PL
1349 /* Defined in Table 35-6 from SDM (Sept 2015) */
1350 static int silvermont_freq_table[] = {
1351 83300, 100000, 133300, 116700, 80000};
b27580b0
DB
1352
1353 rdmsrl(MSR_FSB_FREQ, value);
1421df63
PL
1354 i = value & 0x7;
1355 WARN_ON(i > 4);
b27580b0 1356
1421df63
PL
1357 return silvermont_freq_table[i];
1358}
b27580b0 1359
1421df63
PL
1360static int airmont_get_scaling(void)
1361{
1362 u64 value;
1363 int i;
1364 /* Defined in Table 35-10 from SDM (Sept 2015) */
1365 static int airmont_freq_table[] = {
1366 83300, 100000, 133300, 116700, 80000,
1367 93300, 90000, 88900, 87500};
1368
1369 rdmsrl(MSR_FSB_FREQ, value);
1370 i = value & 0xF;
1371 WARN_ON(i > 8);
1372
1373 return airmont_freq_table[i];
b27580b0
DB
1374}
1375
938d21a2 1376static void atom_get_vid(struct cpudata *cpudata)
007bea09
DB
1377{
1378 u64 value;
1379
938d21a2 1380 rdmsrl(ATOM_VIDS, value);
c16ed060
DB
1381 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1382 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
007bea09
DB
1383 cpudata->vid.ratio = div_fp(
1384 cpudata->vid.max - cpudata->vid.min,
1385 int_tofp(cpudata->pstate.max_pstate -
1386 cpudata->pstate.min_pstate));
21855ff5 1387
938d21a2 1388 rdmsrl(ATOM_TURBO_VIDS, value);
21855ff5 1389 cpudata->vid.turbo = value & 0x7f;
007bea09
DB
1390}
1391
016c8150 1392static int core_get_min_pstate(void)
93f0822d
DB
1393{
1394 u64 value;
845c1cbe 1395
05e99c8c 1396 rdmsrl(MSR_PLATFORM_INFO, value);
93f0822d
DB
1397 return (value >> 40) & 0xFF;
1398}
1399
3bcc6fa9 1400static int core_get_max_pstate_physical(void)
93f0822d
DB
1401{
1402 u64 value;
845c1cbe 1403
05e99c8c 1404 rdmsrl(MSR_PLATFORM_INFO, value);
93f0822d
DB
1405 return (value >> 8) & 0xFF;
1406}
1407
016c8150 1408static int core_get_max_pstate(void)
93f0822d 1409{
6a35fc2d
SP
1410 u64 tar;
1411 u64 plat_info;
1412 int max_pstate;
1413 int err;
1414
1415 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1416 max_pstate = (plat_info >> 8) & 0xFF;
1417
1418 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1419 if (!err) {
1420 /* Do some sanity checking for safety */
1421 if (plat_info & 0x600000000) {
1422 u64 tdp_ctrl;
1423 u64 tdp_ratio;
1424 int tdp_msr;
1425
1426 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1427 if (err)
1428 goto skip_tar;
1429
5fc8f707 1430 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x3);
6a35fc2d
SP
1431 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1432 if (err)
1433 goto skip_tar;
1434
1becf035
SP
1435 /* For level 1 and 2, bits[23:16] contain the ratio */
1436 if (tdp_ctrl)
1437 tdp_ratio >>= 16;
1438
1439 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
6a35fc2d
SP
1440 if (tdp_ratio - 1 == tar) {
1441 max_pstate = tar;
1442 pr_debug("max_pstate=TAC %x\n", max_pstate);
1443 } else {
1444 goto skip_tar;
1445 }
1446 }
1447 }
845c1cbe 1448
6a35fc2d
SP
1449skip_tar:
1450 return max_pstate;
93f0822d
DB
1451}
1452
016c8150 1453static int core_get_turbo_pstate(void)
93f0822d
DB
1454{
1455 u64 value;
1456 int nont, ret;
845c1cbe 1457
100cf6f2 1458 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
016c8150 1459 nont = core_get_max_pstate();
285cb990 1460 ret = (value) & 255;
93f0822d
DB
1461 if (ret <= nont)
1462 ret = nont;
1463 return ret;
1464}
1465
b27580b0
DB
1466static inline int core_get_scaling(void)
1467{
1468 return 100000;
1469}
1470
fdfdb2b1 1471static u64 core_get_val(struct cpudata *cpudata, int pstate)
016c8150
DB
1472{
1473 u64 val;
1474
144c8e17 1475 val = (u64)pstate << 8;
51443fbf 1476 if (limits->no_turbo && !limits->turbo_disabled)
016c8150
DB
1477 val |= (u64)1 << 32;
1478
fdfdb2b1 1479 return val;
016c8150
DB
1480}
1481
b34ef932
DC
1482static int knl_get_turbo_pstate(void)
1483{
1484 u64 value;
1485 int nont, ret;
1486
100cf6f2 1487 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
b34ef932
DC
1488 nont = core_get_max_pstate();
1489 ret = (((value) >> 8) & 0xFF);
1490 if (ret <= nont)
1491 ret = nont;
1492 return ret;
1493}
1494
016c8150
DB
1495static struct cpu_defaults core_params = {
1496 .pid_policy = {
1497 .sample_rate_ms = 10,
1498 .deadband = 0,
1499 .setpoint = 97,
1500 .p_gain_pct = 20,
1501 .d_gain_pct = 0,
1502 .i_gain_pct = 0,
1503 },
1504 .funcs = {
1505 .get_max = core_get_max_pstate,
3bcc6fa9 1506 .get_max_physical = core_get_max_pstate_physical,
016c8150
DB
1507 .get_min = core_get_min_pstate,
1508 .get_turbo = core_get_turbo_pstate,
b27580b0 1509 .get_scaling = core_get_scaling,
fdfdb2b1 1510 .get_val = core_get_val,
157386b6 1511 .get_target_pstate = get_target_pstate_use_performance,
016c8150
DB
1512 },
1513};
1514
42ce8921 1515static const struct cpu_defaults silvermont_params = {
1421df63
PL
1516 .pid_policy = {
1517 .sample_rate_ms = 10,
1518 .deadband = 0,
1519 .setpoint = 60,
1520 .p_gain_pct = 14,
1521 .d_gain_pct = 0,
1522 .i_gain_pct = 4,
1523 },
1524 .funcs = {
1525 .get_max = atom_get_max_pstate,
1526 .get_max_physical = atom_get_max_pstate,
1527 .get_min = atom_get_min_pstate,
1528 .get_turbo = atom_get_turbo_pstate,
fdfdb2b1 1529 .get_val = atom_get_val,
1421df63
PL
1530 .get_scaling = silvermont_get_scaling,
1531 .get_vid = atom_get_vid,
e70eed2b 1532 .get_target_pstate = get_target_pstate_use_cpu_load,
1421df63
PL
1533 },
1534};
1535
42ce8921 1536static const struct cpu_defaults airmont_params = {
19e77c28
DB
1537 .pid_policy = {
1538 .sample_rate_ms = 10,
1539 .deadband = 0,
6a82ba6d 1540 .setpoint = 60,
19e77c28
DB
1541 .p_gain_pct = 14,
1542 .d_gain_pct = 0,
1543 .i_gain_pct = 4,
1544 },
1545 .funcs = {
938d21a2
PL
1546 .get_max = atom_get_max_pstate,
1547 .get_max_physical = atom_get_max_pstate,
1548 .get_min = atom_get_min_pstate,
1549 .get_turbo = atom_get_turbo_pstate,
fdfdb2b1 1550 .get_val = atom_get_val,
1421df63 1551 .get_scaling = airmont_get_scaling,
938d21a2 1552 .get_vid = atom_get_vid,
e70eed2b 1553 .get_target_pstate = get_target_pstate_use_cpu_load,
19e77c28
DB
1554 },
1555};
1556
42ce8921 1557static const struct cpu_defaults knl_params = {
b34ef932
DC
1558 .pid_policy = {
1559 .sample_rate_ms = 10,
1560 .deadband = 0,
1561 .setpoint = 97,
1562 .p_gain_pct = 20,
1563 .d_gain_pct = 0,
1564 .i_gain_pct = 0,
1565 },
1566 .funcs = {
1567 .get_max = core_get_max_pstate,
3bcc6fa9 1568 .get_max_physical = core_get_max_pstate_physical,
b34ef932
DC
1569 .get_min = core_get_min_pstate,
1570 .get_turbo = knl_get_turbo_pstate,
69cefc27 1571 .get_scaling = core_get_scaling,
fdfdb2b1 1572 .get_val = core_get_val,
157386b6 1573 .get_target_pstate = get_target_pstate_use_performance,
b34ef932
DC
1574 },
1575};
1576
42ce8921 1577static const struct cpu_defaults bxt_params = {
41bad47f
SP
1578 .pid_policy = {
1579 .sample_rate_ms = 10,
1580 .deadband = 0,
1581 .setpoint = 60,
1582 .p_gain_pct = 14,
1583 .d_gain_pct = 0,
1584 .i_gain_pct = 4,
1585 },
1586 .funcs = {
1587 .get_max = core_get_max_pstate,
1588 .get_max_physical = core_get_max_pstate_physical,
1589 .get_min = core_get_min_pstate,
1590 .get_turbo = core_get_turbo_pstate,
1591 .get_scaling = core_get_scaling,
1592 .get_val = core_get_val,
1593 .get_target_pstate = get_target_pstate_use_cpu_load,
1594 },
1595};
1596
93f0822d
DB
1597static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1598{
1599 int max_perf = cpu->pstate.turbo_pstate;
7244cb62 1600 int max_perf_adj;
93f0822d 1601 int min_perf;
eae48f04 1602 struct perf_limits *perf_limits = limits;
845c1cbe 1603
51443fbf 1604 if (limits->no_turbo || limits->turbo_disabled)
93f0822d
DB
1605 max_perf = cpu->pstate.max_pstate;
1606
eae48f04
SP
1607 if (per_cpu_limits)
1608 perf_limits = cpu->perf_limits;
1609
e0d4c8f8
KCA
1610 /*
1611 * performance can be limited by user through sysfs, by cpufreq
1612 * policy, or by cpu specific default values determined through
1613 * experimentation.
1614 */
d5dd33d9 1615 max_perf_adj = fp_ext_toint(max_perf * perf_limits->max_perf);
799281a3
RW
1616 *max = clamp_t(int, max_perf_adj,
1617 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
93f0822d 1618
d5dd33d9 1619 min_perf = fp_ext_toint(max_perf * perf_limits->min_perf);
799281a3 1620 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
93f0822d
DB
1621}
1622
a6c6ead1 1623static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
fdfdb2b1 1624{
bc95a454
RW
1625 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1626 cpu->pstate.current_pstate = pstate;
fdfdb2b1
RW
1627 /*
1628 * Generally, there is no guarantee that this code will always run on
1629 * the CPU being updated, so force the register update to run on the
1630 * right CPU.
1631 */
1632 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1633 pstate_funcs.get_val(cpu, pstate));
93f0822d
DB
1634}
1635
a6c6ead1
RW
1636static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1637{
1638 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1639}
1640
1641static void intel_pstate_max_within_limits(struct cpudata *cpu)
1642{
1643 int min_pstate, max_pstate;
1644
1645 update_turbo_state();
1646 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
1647 intel_pstate_set_pstate(cpu, max_pstate);
1648}
1649
93f0822d
DB
1650static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1651{
016c8150
DB
1652 cpu->pstate.min_pstate = pstate_funcs.get_min();
1653 cpu->pstate.max_pstate = pstate_funcs.get_max();
3bcc6fa9 1654 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
016c8150 1655 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
b27580b0 1656 cpu->pstate.scaling = pstate_funcs.get_scaling();
001c76f0
RW
1657 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1658 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
93f0822d 1659
007bea09
DB
1660 if (pstate_funcs.get_vid)
1661 pstate_funcs.get_vid(cpu);
fdfdb2b1
RW
1662
1663 intel_pstate_set_min_pstate(cpu);
93f0822d
DB
1664}
1665
a1c9787d 1666static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
93f0822d 1667{
6b17ddb2 1668 struct sample *sample = &cpu->sample;
e66c1768 1669
a1c9787d 1670 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
93f0822d
DB
1671}
1672
4fec7ad5 1673static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
93f0822d 1674{
93f0822d 1675 u64 aperf, mperf;
4ab60c3f 1676 unsigned long flags;
4055fad3 1677 u64 tsc;
93f0822d 1678
4ab60c3f 1679 local_irq_save(flags);
93f0822d
DB
1680 rdmsrl(MSR_IA32_APERF, aperf);
1681 rdmsrl(MSR_IA32_MPERF, mperf);
e70eed2b 1682 tsc = rdtsc();
4fec7ad5 1683 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
8e601a9f 1684 local_irq_restore(flags);
4fec7ad5 1685 return false;
8e601a9f 1686 }
4ab60c3f 1687 local_irq_restore(flags);
b69880f9 1688
c4ee841f 1689 cpu->last_sample_time = cpu->sample.time;
a4675fbc 1690 cpu->sample.time = time;
d37e2b76
DB
1691 cpu->sample.aperf = aperf;
1692 cpu->sample.mperf = mperf;
4055fad3 1693 cpu->sample.tsc = tsc;
d37e2b76
DB
1694 cpu->sample.aperf -= cpu->prev_aperf;
1695 cpu->sample.mperf -= cpu->prev_mperf;
4055fad3 1696 cpu->sample.tsc -= cpu->prev_tsc;
1abc4b20 1697
93f0822d
DB
1698 cpu->prev_aperf = aperf;
1699 cpu->prev_mperf = mperf;
4055fad3 1700 cpu->prev_tsc = tsc;
febce40f
RW
1701 /*
1702 * First time this function is invoked in a given cycle, all of the
1703 * previous sample data fields are equal to zero or stale and they must
1704 * be populated with meaningful numbers for things to work, so assume
1705 * that sample.time will always be reset before setting the utilization
1706 * update hook and make the caller skip the sample then.
1707 */
1708 return !!cpu->last_sample_time;
93f0822d
DB
1709}
1710
8fa520af
PL
1711static inline int32_t get_avg_frequency(struct cpudata *cpu)
1712{
a1c9787d
RW
1713 return mul_ext_fp(cpu->sample.core_avg_perf,
1714 cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
8fa520af
PL
1715}
1716
bdcaa23f
PL
1717static inline int32_t get_avg_pstate(struct cpudata *cpu)
1718{
8edb0a6e
RW
1719 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1720 cpu->sample.core_avg_perf);
bdcaa23f
PL
1721}
1722
e70eed2b
PL
1723static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1724{
1725 struct sample *sample = &cpu->sample;
09c448d3 1726 int32_t busy_frac, boost;
0843e83c 1727 int target, avg_pstate;
e70eed2b 1728
09c448d3 1729 busy_frac = div_fp(sample->mperf, sample->tsc);
63d1d656 1730
09c448d3
RW
1731 boost = cpu->iowait_boost;
1732 cpu->iowait_boost >>= 1;
63d1d656 1733
09c448d3
RW
1734 if (busy_frac < boost)
1735 busy_frac = boost;
63d1d656 1736
09c448d3 1737 sample->busy_scaled = busy_frac * 100;
0843e83c
RW
1738
1739 target = limits->no_turbo || limits->turbo_disabled ?
1740 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1741 target += target >> 2;
1742 target = mul_fp(target, busy_frac);
1743 if (target < cpu->pstate.min_pstate)
1744 target = cpu->pstate.min_pstate;
1745
1746 /*
1747 * If the average P-state during the previous cycle was higher than the
1748 * current target, add 50% of the difference to the target to reduce
1749 * possible performance oscillations and offset possible performance
1750 * loss related to moving the workload from one CPU to another within
1751 * a package/module.
1752 */
1753 avg_pstate = get_avg_pstate(cpu);
1754 if (avg_pstate > target)
1755 target += (avg_pstate - target) >> 1;
1756
1757 return target;
e70eed2b
PL
1758}
1759
157386b6 1760static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
93f0822d 1761{
1aa7a6e2 1762 int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
a4675fbc 1763 u64 duration_ns;
93f0822d 1764
e0d4c8f8 1765 /*
f00593a4
RW
1766 * perf_scaled is the ratio of the average P-state during the last
1767 * sampling period to the P-state requested last time (in percent).
1768 *
1769 * That measures the system's response to the previous P-state
1770 * selection.
e0d4c8f8 1771 */
22590efb
RW
1772 max_pstate = cpu->pstate.max_pstate_physical;
1773 current_pstate = cpu->pstate.current_pstate;
1aa7a6e2 1774 perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
a1c9787d 1775 div_fp(100 * max_pstate, current_pstate));
c4ee841f 1776
e0d4c8f8 1777 /*
a4675fbc
RW
1778 * Since our utilization update callback will not run unless we are
1779 * in C0, check if the actual elapsed time is significantly greater (3x)
1780 * than our sample interval. If it is, then we were idle for a long
1aa7a6e2 1781 * enough period of time to adjust our performance metric.
e0d4c8f8 1782 */
a4675fbc 1783 duration_ns = cpu->sample.time - cpu->last_sample_time;
febce40f 1784 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
22590efb 1785 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
1aa7a6e2 1786 perf_scaled = mul_fp(perf_scaled, sample_ratio);
ffb81056
RW
1787 } else {
1788 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1789 if (sample_ratio < int_tofp(1))
1aa7a6e2 1790 perf_scaled = 0;
c4ee841f
DB
1791 }
1792
1aa7a6e2
RW
1793 cpu->sample.busy_scaled = perf_scaled;
1794 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
93f0822d
DB
1795}
1796
001c76f0 1797static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
fdfdb2b1
RW
1798{
1799 int max_perf, min_perf;
1800
fdfdb2b1
RW
1801 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1802 pstate = clamp_t(int, pstate, min_perf, max_perf);
bc95a454 1803 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
001c76f0
RW
1804 return pstate;
1805}
1806
1807static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1808{
1809 pstate = intel_pstate_prepare_request(cpu, pstate);
fdfdb2b1
RW
1810 if (pstate == cpu->pstate.current_pstate)
1811 return;
1812
bc95a454 1813 cpu->pstate.current_pstate = pstate;
fdfdb2b1
RW
1814 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1815}
1816
93f0822d
DB
1817static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1818{
157386b6 1819 int from, target_pstate;
4055fad3
DS
1820 struct sample *sample;
1821
1822 from = cpu->pstate.current_pstate;
93f0822d 1823
2f1d407a
RW
1824 target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ?
1825 cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu);
93f0822d 1826
001c76f0
RW
1827 update_turbo_state();
1828
fdfdb2b1 1829 intel_pstate_update_pstate(cpu, target_pstate);
4055fad3
DS
1830
1831 sample = &cpu->sample;
a1c9787d 1832 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
157386b6 1833 fp_toint(sample->busy_scaled),
4055fad3
DS
1834 from,
1835 cpu->pstate.current_pstate,
1836 sample->mperf,
1837 sample->aperf,
1838 sample->tsc,
3ba7bcaa
SP
1839 get_avg_frequency(cpu),
1840 fp_toint(cpu->iowait_boost * 100));
93f0822d
DB
1841}
1842
a4675fbc 1843static void intel_pstate_update_util(struct update_util_data *data, u64 time,
58919e83 1844 unsigned int flags)
93f0822d 1845{
a4675fbc 1846 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
09c448d3
RW
1847 u64 delta_ns;
1848
1d29815e 1849 if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) {
09c448d3
RW
1850 if (flags & SCHED_CPUFREQ_IOWAIT) {
1851 cpu->iowait_boost = int_tofp(1);
1852 } else if (cpu->iowait_boost) {
1853 /* Clear iowait_boost if the CPU may have been idle. */
1854 delta_ns = time - cpu->last_update;
1855 if (delta_ns > TICK_NSEC)
1856 cpu->iowait_boost = 0;
1857 }
1858 cpu->last_update = time;
1859 }
b69880f9 1860
09c448d3 1861 delta_ns = time - cpu->sample.time;
a4675fbc 1862 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
4fec7ad5
RW
1863 bool sample_taken = intel_pstate_sample(cpu, time);
1864
6d45b719 1865 if (sample_taken) {
a1c9787d 1866 intel_pstate_calc_avg_perf(cpu);
6d45b719
RW
1867 if (!hwp_active)
1868 intel_pstate_adjust_busy_pstate(cpu);
1869 }
a4675fbc 1870 }
93f0822d
DB
1871}
1872
1873#define ICPU(model, policy) \
6cbd7ee1
DB
1874 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1875 (unsigned long)&policy }
93f0822d
DB
1876
1877static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
5b20c944
DH
1878 ICPU(INTEL_FAM6_SANDYBRIDGE, core_params),
1879 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params),
1880 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params),
1881 ICPU(INTEL_FAM6_IVYBRIDGE, core_params),
1882 ICPU(INTEL_FAM6_HASWELL_CORE, core_params),
1883 ICPU(INTEL_FAM6_BROADWELL_CORE, core_params),
1884 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params),
1885 ICPU(INTEL_FAM6_HASWELL_X, core_params),
1886 ICPU(INTEL_FAM6_HASWELL_ULT, core_params),
1887 ICPU(INTEL_FAM6_HASWELL_GT3E, core_params),
1888 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params),
1889 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params),
1890 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params),
1891 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1892 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params),
1893 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
1894 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params),
58bf4542 1895 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_params),
41bad47f 1896 ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params),
93f0822d
DB
1897 {}
1898};
1899MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1900
29327c84 1901static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
5b20c944 1902 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
65c1262f
SP
1903 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1904 ICPU(INTEL_FAM6_SKYLAKE_X, core_params),
2f86dc4c
DB
1905 {}
1906};
1907
93f0822d
DB
1908static int intel_pstate_init_cpu(unsigned int cpunum)
1909{
93f0822d
DB
1910 struct cpudata *cpu;
1911
eae48f04
SP
1912 cpu = all_cpu_data[cpunum];
1913
1914 if (!cpu) {
1915 unsigned int size = sizeof(struct cpudata);
1916
1917 if (per_cpu_limits)
1918 size += sizeof(struct perf_limits);
1919
1920 cpu = kzalloc(size, GFP_KERNEL);
1921 if (!cpu)
1922 return -ENOMEM;
1923
1924 all_cpu_data[cpunum] = cpu;
1925 if (per_cpu_limits)
1926 cpu->perf_limits = (struct perf_limits *)(cpu + 1);
1927
984edbdc
SP
1928 cpu->epp_default = -EINVAL;
1929 cpu->epp_powersave = -EINVAL;
1930 cpu->epp_saved = -EINVAL;
eae48f04 1931 }
93f0822d
DB
1932
1933 cpu = all_cpu_data[cpunum];
1934
93f0822d 1935 cpu->cpu = cpunum;
ba88d433 1936
a4675fbc 1937 if (hwp_active) {
ba88d433 1938 intel_pstate_hwp_enable(cpu);
a4675fbc
RW
1939 pid_params.sample_rate_ms = 50;
1940 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1941 }
ba88d433 1942
179e8471 1943 intel_pstate_get_cpu_pstates(cpu);
016c8150 1944
93f0822d 1945 intel_pstate_busy_pid_reset(cpu);
93f0822d 1946
4836df17 1947 pr_debug("controlling: cpu %d\n", cpunum);
93f0822d
DB
1948
1949 return 0;
1950}
1951
1952static unsigned int intel_pstate_get(unsigned int cpu_num)
1953{
f96fd0c8 1954 struct cpudata *cpu = all_cpu_data[cpu_num];
93f0822d 1955
f96fd0c8 1956 return cpu ? get_avg_frequency(cpu) : 0;
93f0822d
DB
1957}
1958
febce40f 1959static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
bb6ab52f 1960{
febce40f
RW
1961 struct cpudata *cpu = all_cpu_data[cpu_num];
1962
5ab666e0
RW
1963 if (cpu->update_util_set)
1964 return;
1965
febce40f
RW
1966 /* Prevent intel_pstate_update_util() from using stale data. */
1967 cpu->sample.time = 0;
0bed612b
RW
1968 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1969 intel_pstate_update_util);
4578ee7e 1970 cpu->update_util_set = true;
bb6ab52f
RW
1971}
1972
1973static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1974{
4578ee7e
CY
1975 struct cpudata *cpu_data = all_cpu_data[cpu];
1976
1977 if (!cpu_data->update_util_set)
1978 return;
1979
0bed612b 1980 cpufreq_remove_update_util_hook(cpu);
4578ee7e 1981 cpu_data->update_util_set = false;
bb6ab52f
RW
1982 synchronize_sched();
1983}
1984
30a39153
SP
1985static void intel_pstate_set_performance_limits(struct perf_limits *limits)
1986{
1987 limits->no_turbo = 0;
1988 limits->turbo_disabled = 0;
1989 limits->max_perf_pct = 100;
d5dd33d9 1990 limits->max_perf = int_ext_tofp(1);
30a39153 1991 limits->min_perf_pct = 100;
d5dd33d9 1992 limits->min_perf = int_ext_tofp(1);
30a39153
SP
1993 limits->max_policy_pct = 100;
1994 limits->max_sysfs_pct = 100;
1995 limits->min_policy_pct = 0;
1996 limits->min_sysfs_pct = 0;
1997}
1998
eae48f04
SP
1999static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
2000 struct perf_limits *limits)
2001{
a410c03d 2002
eae48f04
SP
2003 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
2004 policy->cpuinfo.max_freq);
2005 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0, 100);
5879f877
SP
2006 if (policy->max == policy->min) {
2007 limits->min_policy_pct = limits->max_policy_pct;
2008 } else {
46992d6b
SP
2009 limits->min_policy_pct = DIV_ROUND_UP(policy->min * 100,
2010 policy->cpuinfo.max_freq);
5879f877
SP
2011 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct,
2012 0, 100);
2013 }
eae48f04
SP
2014
2015 /* Normalize user input to [min_policy_pct, max_policy_pct] */
2016 limits->min_perf_pct = max(limits->min_policy_pct,
2017 limits->min_sysfs_pct);
2018 limits->min_perf_pct = min(limits->max_policy_pct,
2019 limits->min_perf_pct);
2020 limits->max_perf_pct = min(limits->max_policy_pct,
2021 limits->max_sysfs_pct);
2022 limits->max_perf_pct = max(limits->min_policy_pct,
2023 limits->max_perf_pct);
2024
2025 /* Make sure min_perf_pct <= max_perf_pct */
2026 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
2027
d5dd33d9
SP
2028 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
2029 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
2030 limits->max_perf = round_up(limits->max_perf, EXT_FRAC_BITS);
2031 limits->min_perf = round_up(limits->min_perf, EXT_FRAC_BITS);
eae48f04
SP
2032
2033 pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu,
2034 limits->max_perf_pct, limits->min_perf_pct);
2035}
2036
93f0822d
DB
2037static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2038{
3be9200d 2039 struct cpudata *cpu;
eae48f04 2040 struct perf_limits *perf_limits = NULL;
3be9200d 2041
d3929b83
DB
2042 if (!policy->cpuinfo.max_freq)
2043 return -ENODEV;
2044
2c2c1af4
SP
2045 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2046 policy->cpuinfo.max_freq, policy->max);
2047
a6c6ead1 2048 cpu = all_cpu_data[policy->cpu];
2f1d407a
RW
2049 cpu->policy = policy->policy;
2050
c749c64f
RW
2051 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
2052 policy->max < policy->cpuinfo.max_freq &&
2053 policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
2054 pr_debug("policy->max > max non turbo frequency\n");
2055 policy->max = policy->cpuinfo.max_freq;
3be9200d
SP
2056 }
2057
eae48f04
SP
2058 if (per_cpu_limits)
2059 perf_limits = cpu->perf_limits;
2060
b59fe540
SP
2061 mutex_lock(&intel_pstate_limits_lock);
2062
eae48f04
SP
2063 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
2064 if (!perf_limits) {
2065 limits = &performance_limits;
2066 perf_limits = limits;
2067 }
1443ebba
SP
2068 if (policy->max >= policy->cpuinfo.max_freq &&
2069 !limits->no_turbo) {
4836df17 2070 pr_debug("set performance\n");
eae48f04 2071 intel_pstate_set_performance_limits(perf_limits);
30a39153
SP
2072 goto out;
2073 }
2074 } else {
4836df17 2075 pr_debug("set powersave\n");
eae48f04
SP
2076 if (!perf_limits) {
2077 limits = &powersave_limits;
2078 perf_limits = limits;
2079 }
43717aad 2080
eae48f04 2081 }
93f0822d 2082
eae48f04 2083 intel_pstate_update_perf_limits(policy, perf_limits);
bb6ab52f 2084 out:
2f1d407a 2085 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
a6c6ead1
RW
2086 /*
2087 * NOHZ_FULL CPUs need this as the governor callback may not
2088 * be invoked on them.
2089 */
2090 intel_pstate_clear_update_util_hook(policy->cpu);
2091 intel_pstate_max_within_limits(cpu);
2092 }
2093
bb6ab52f
RW
2094 intel_pstate_set_update_util_hook(policy->cpu);
2095
ba41e1bc 2096 intel_pstate_hwp_set_policy(policy);
2f86dc4c 2097
b59fe540
SP
2098 mutex_unlock(&intel_pstate_limits_lock);
2099
93f0822d
DB
2100 return 0;
2101}
2102
2103static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
2104{
be49e346 2105 cpufreq_verify_within_cpu_limits(policy);
93f0822d 2106
285cb990 2107 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
c410833a 2108 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
93f0822d
DB
2109 return -EINVAL;
2110
1443ebba
SP
2111 /* When per-CPU limits are used, sysfs limits are not used */
2112 if (!per_cpu_limits) {
2113 unsigned int max_freq, min_freq;
2114
2115 max_freq = policy->cpuinfo.max_freq *
2116 limits->max_sysfs_pct / 100;
2117 min_freq = policy->cpuinfo.max_freq *
2118 limits->min_sysfs_pct / 100;
2119 cpufreq_verify_within_limits(policy, min_freq, max_freq);
2120 }
2121
93f0822d
DB
2122 return 0;
2123}
2124
001c76f0
RW
2125static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
2126{
2127 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
2128}
2129
bb18008f 2130static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
93f0822d 2131{
001c76f0 2132 pr_debug("CPU %d exiting\n", policy->cpu);
93f0822d 2133
001c76f0 2134 intel_pstate_clear_update_util_hook(policy->cpu);
984edbdc
SP
2135 if (hwp_active)
2136 intel_pstate_hwp_save_state(policy);
2137 else
001c76f0
RW
2138 intel_cpufreq_stop_cpu(policy);
2139}
bb18008f 2140
001c76f0
RW
2141static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2142{
2143 intel_pstate_exit_perf_limits(policy);
a4675fbc 2144
001c76f0 2145 policy->fast_switch_possible = false;
2f86dc4c 2146
001c76f0 2147 return 0;
93f0822d
DB
2148}
2149
001c76f0 2150static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
93f0822d 2151{
93f0822d 2152 struct cpudata *cpu;
52e0a509 2153 int rc;
93f0822d
DB
2154
2155 rc = intel_pstate_init_cpu(policy->cpu);
2156 if (rc)
2157 return rc;
2158
2159 cpu = all_cpu_data[policy->cpu];
2160
eae48f04
SP
2161 /*
2162 * We need sane value in the cpu->perf_limits, so inherit from global
2163 * perf_limits limits, which are seeded with values based on the
2164 * CONFIG_CPU_FREQ_DEFAULT_GOV_*, during boot up.
2165 */
2166 if (per_cpu_limits)
2167 memcpy(cpu->perf_limits, limits, sizeof(struct perf_limits));
93f0822d 2168
b27580b0
DB
2169 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2170 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
93f0822d
DB
2171
2172 /* cpuinfo and default policy values */
b27580b0 2173 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
983e600e
SP
2174 update_turbo_state();
2175 policy->cpuinfo.max_freq = limits->turbo_disabled ?
2176 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2177 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2178
9522a2ff 2179 intel_pstate_init_acpi_perf_limits(policy);
93f0822d
DB
2180 cpumask_set_cpu(policy->cpu, policy->cpus);
2181
001c76f0
RW
2182 policy->fast_switch_possible = true;
2183
93f0822d
DB
2184 return 0;
2185}
2186
001c76f0 2187static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
9522a2ff 2188{
001c76f0
RW
2189 int ret = __intel_pstate_cpu_init(policy);
2190
2191 if (ret)
2192 return ret;
2193
2194 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
2195 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
2196 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2197 else
2198 policy->policy = CPUFREQ_POLICY_POWERSAVE;
9522a2ff
SP
2199
2200 return 0;
2201}
2202
001c76f0 2203static struct cpufreq_driver intel_pstate = {
93f0822d
DB
2204 .flags = CPUFREQ_CONST_LOOPS,
2205 .verify = intel_pstate_verify_policy,
2206 .setpolicy = intel_pstate_set_policy,
984edbdc 2207 .suspend = intel_pstate_hwp_save_state,
8442885f 2208 .resume = intel_pstate_resume,
93f0822d
DB
2209 .get = intel_pstate_get,
2210 .init = intel_pstate_cpu_init,
9522a2ff 2211 .exit = intel_pstate_cpu_exit,
bb18008f 2212 .stop_cpu = intel_pstate_stop_cpu,
93f0822d 2213 .name = "intel_pstate",
93f0822d
DB
2214};
2215
001c76f0
RW
2216static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2217{
2218 struct cpudata *cpu = all_cpu_data[policy->cpu];
2219 struct perf_limits *perf_limits = limits;
2220
2221 update_turbo_state();
2222 policy->cpuinfo.max_freq = limits->turbo_disabled ?
2223 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2224
2225 cpufreq_verify_within_cpu_limits(policy);
2226
2227 if (per_cpu_limits)
2228 perf_limits = cpu->perf_limits;
2229
cad30467
RW
2230 mutex_lock(&intel_pstate_limits_lock);
2231
001c76f0
RW
2232 intel_pstate_update_perf_limits(policy, perf_limits);
2233
cad30467
RW
2234 mutex_unlock(&intel_pstate_limits_lock);
2235
001c76f0
RW
2236 return 0;
2237}
2238
2239static unsigned int intel_cpufreq_turbo_update(struct cpudata *cpu,
2240 struct cpufreq_policy *policy,
2241 unsigned int target_freq)
2242{
2243 unsigned int max_freq;
2244
2245 update_turbo_state();
2246
2247 max_freq = limits->no_turbo || limits->turbo_disabled ?
2248 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2249 policy->cpuinfo.max_freq = max_freq;
2250 if (policy->max > max_freq)
2251 policy->max = max_freq;
2252
2253 if (target_freq > max_freq)
2254 target_freq = max_freq;
2255
2256 return target_freq;
2257}
2258
2259static int intel_cpufreq_target(struct cpufreq_policy *policy,
2260 unsigned int target_freq,
2261 unsigned int relation)
2262{
2263 struct cpudata *cpu = all_cpu_data[policy->cpu];
2264 struct cpufreq_freqs freqs;
2265 int target_pstate;
2266
2267 freqs.old = policy->cur;
2268 freqs.new = intel_cpufreq_turbo_update(cpu, policy, target_freq);
2269
2270 cpufreq_freq_transition_begin(policy, &freqs);
2271 switch (relation) {
2272 case CPUFREQ_RELATION_L:
2273 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2274 break;
2275 case CPUFREQ_RELATION_H:
2276 target_pstate = freqs.new / cpu->pstate.scaling;
2277 break;
2278 default:
2279 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2280 break;
2281 }
2282 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2283 if (target_pstate != cpu->pstate.current_pstate) {
2284 cpu->pstate.current_pstate = target_pstate;
2285 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2286 pstate_funcs.get_val(cpu, target_pstate));
2287 }
2288 cpufreq_freq_transition_end(policy, &freqs, false);
2289
2290 return 0;
2291}
2292
2293static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2294 unsigned int target_freq)
2295{
2296 struct cpudata *cpu = all_cpu_data[policy->cpu];
2297 int target_pstate;
2298
2299 target_freq = intel_cpufreq_turbo_update(cpu, policy, target_freq);
2300 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2301 intel_pstate_update_pstate(cpu, target_pstate);
2302 return target_freq;
2303}
2304
2305static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2306{
2307 int ret = __intel_pstate_cpu_init(policy);
2308
2309 if (ret)
2310 return ret;
2311
2312 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2313 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2314 policy->cur = policy->cpuinfo.min_freq;
2315
2316 return 0;
2317}
2318
2319static struct cpufreq_driver intel_cpufreq = {
2320 .flags = CPUFREQ_CONST_LOOPS,
2321 .verify = intel_cpufreq_verify_policy,
2322 .target = intel_cpufreq_target,
2323 .fast_switch = intel_cpufreq_fast_switch,
2324 .init = intel_cpufreq_cpu_init,
2325 .exit = intel_pstate_cpu_exit,
2326 .stop_cpu = intel_cpufreq_stop_cpu,
2327 .name = "intel_cpufreq",
2328};
2329
2330static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
2331
eed43609
JZ
2332static int no_load __initdata;
2333static int no_hwp __initdata;
2334static int hwp_only __initdata;
29327c84 2335static unsigned int force_load __initdata;
6be26498 2336
29327c84 2337static int __init intel_pstate_msrs_not_valid(void)
b563b4e3 2338{
016c8150 2339 if (!pstate_funcs.get_max() ||
c410833a
SK
2340 !pstate_funcs.get_min() ||
2341 !pstate_funcs.get_turbo())
b563b4e3
DB
2342 return -ENODEV;
2343
b563b4e3
DB
2344 return 0;
2345}
016c8150 2346
29327c84 2347static void __init copy_pid_params(struct pstate_adjust_policy *policy)
016c8150
DB
2348{
2349 pid_params.sample_rate_ms = policy->sample_rate_ms;
a4675fbc 2350 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
016c8150
DB
2351 pid_params.p_gain_pct = policy->p_gain_pct;
2352 pid_params.i_gain_pct = policy->i_gain_pct;
2353 pid_params.d_gain_pct = policy->d_gain_pct;
2354 pid_params.deadband = policy->deadband;
2355 pid_params.setpoint = policy->setpoint;
2356}
2357
7f7a516e
SP
2358#ifdef CONFIG_ACPI
2359static void intel_pstate_use_acpi_profile(void)
2360{
2361 if (acpi_gbl_FADT.preferred_profile == PM_MOBILE)
2362 pstate_funcs.get_target_pstate =
2363 get_target_pstate_use_cpu_load;
2364}
2365#else
2366static void intel_pstate_use_acpi_profile(void)
2367{
2368}
2369#endif
2370
29327c84 2371static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
016c8150
DB
2372{
2373 pstate_funcs.get_max = funcs->get_max;
3bcc6fa9 2374 pstate_funcs.get_max_physical = funcs->get_max_physical;
016c8150
DB
2375 pstate_funcs.get_min = funcs->get_min;
2376 pstate_funcs.get_turbo = funcs->get_turbo;
b27580b0 2377 pstate_funcs.get_scaling = funcs->get_scaling;
fdfdb2b1 2378 pstate_funcs.get_val = funcs->get_val;
007bea09 2379 pstate_funcs.get_vid = funcs->get_vid;
157386b6
PL
2380 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
2381
7f7a516e 2382 intel_pstate_use_acpi_profile();
016c8150
DB
2383}
2384
9522a2ff 2385#ifdef CONFIG_ACPI
fbbcdc07 2386
29327c84 2387static bool __init intel_pstate_no_acpi_pss(void)
fbbcdc07
AH
2388{
2389 int i;
2390
2391 for_each_possible_cpu(i) {
2392 acpi_status status;
2393 union acpi_object *pss;
2394 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2395 struct acpi_processor *pr = per_cpu(processors, i);
2396
2397 if (!pr)
2398 continue;
2399
2400 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2401 if (ACPI_FAILURE(status))
2402 continue;
2403
2404 pss = buffer.pointer;
2405 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2406 kfree(pss);
2407 return false;
2408 }
2409
2410 kfree(pss);
2411 }
2412
2413 return true;
2414}
2415
29327c84 2416static bool __init intel_pstate_has_acpi_ppc(void)
966916ea 2417{
2418 int i;
2419
2420 for_each_possible_cpu(i) {
2421 struct acpi_processor *pr = per_cpu(processors, i);
2422
2423 if (!pr)
2424 continue;
2425 if (acpi_has_method(pr->handle, "_PPC"))
2426 return true;
2427 }
2428 return false;
2429}
2430
2431enum {
2432 PSS,
2433 PPC,
2434};
2435
fbbcdc07
AH
2436struct hw_vendor_info {
2437 u16 valid;
2438 char oem_id[ACPI_OEM_ID_SIZE];
2439 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
966916ea 2440 int oem_pwr_table;
fbbcdc07
AH
2441};
2442
2443/* Hardware vendor-specific info that has its own power management modes */
29327c84 2444static struct hw_vendor_info vendor_info[] __initdata = {
966916ea 2445 {1, "HP ", "ProLiant", PSS},
2446 {1, "ORACLE", "X4-2 ", PPC},
2447 {1, "ORACLE", "X4-2L ", PPC},
2448 {1, "ORACLE", "X4-2B ", PPC},
2449 {1, "ORACLE", "X3-2 ", PPC},
2450 {1, "ORACLE", "X3-2L ", PPC},
2451 {1, "ORACLE", "X3-2B ", PPC},
2452 {1, "ORACLE", "X4470M2 ", PPC},
2453 {1, "ORACLE", "X4270M3 ", PPC},
2454 {1, "ORACLE", "X4270M2 ", PPC},
2455 {1, "ORACLE", "X4170M2 ", PPC},
5aecc3c8
EZ
2456 {1, "ORACLE", "X4170 M3", PPC},
2457 {1, "ORACLE", "X4275 M3", PPC},
2458 {1, "ORACLE", "X6-2 ", PPC},
2459 {1, "ORACLE", "Sudbury ", PPC},
fbbcdc07
AH
2460 {0, "", ""},
2461};
2462
29327c84 2463static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
fbbcdc07
AH
2464{
2465 struct acpi_table_header hdr;
2466 struct hw_vendor_info *v_info;
2f86dc4c
DB
2467 const struct x86_cpu_id *id;
2468 u64 misc_pwr;
2469
2470 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2471 if (id) {
2472 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2473 if ( misc_pwr & (1 << 8))
2474 return true;
2475 }
fbbcdc07 2476
c410833a
SK
2477 if (acpi_disabled ||
2478 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
fbbcdc07
AH
2479 return false;
2480
2481 for (v_info = vendor_info; v_info->valid; v_info++) {
c410833a 2482 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
966916ea 2483 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
2484 ACPI_OEM_TABLE_ID_SIZE))
2485 switch (v_info->oem_pwr_table) {
2486 case PSS:
2487 return intel_pstate_no_acpi_pss();
2488 case PPC:
aa4ea34d
EZ
2489 return intel_pstate_has_acpi_ppc() &&
2490 (!force_load);
966916ea 2491 }
fbbcdc07
AH
2492 }
2493
2494 return false;
2495}
d0ea59e1
RW
2496
2497static void intel_pstate_request_control_from_smm(void)
2498{
2499 /*
2500 * It may be unsafe to request P-states control from SMM if _PPC support
2501 * has not been enabled.
2502 */
2503 if (acpi_ppc)
2504 acpi_processor_pstate_control();
2505}
fbbcdc07
AH
2506#else /* CONFIG_ACPI not enabled */
2507static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
966916ea 2508static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
d0ea59e1 2509static inline void intel_pstate_request_control_from_smm(void) {}
fbbcdc07
AH
2510#endif /* CONFIG_ACPI */
2511
7791e4aa
SP
2512static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2513 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2514 {}
2515};
2516
93f0822d
DB
2517static int __init intel_pstate_init(void)
2518{
907cc908 2519 int cpu, rc = 0;
93f0822d 2520 const struct x86_cpu_id *id;
64df1fdf 2521 struct cpu_defaults *cpu_def;
93f0822d 2522
6be26498
DB
2523 if (no_load)
2524 return -ENODEV;
2525
7791e4aa
SP
2526 if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
2527 copy_cpu_funcs(&core_params.funcs);
2528 hwp_active++;
984edbdc 2529 intel_pstate.attr = hwp_cpufreq_attrs;
7791e4aa
SP
2530 goto hwp_cpu_matched;
2531 }
2532
93f0822d
DB
2533 id = x86_match_cpu(intel_pstate_cpu_ids);
2534 if (!id)
2535 return -ENODEV;
2536
64df1fdf 2537 cpu_def = (struct cpu_defaults *)id->driver_data;
016c8150 2538
64df1fdf
BP
2539 copy_pid_params(&cpu_def->pid_policy);
2540 copy_cpu_funcs(&cpu_def->funcs);
016c8150 2541
b563b4e3
DB
2542 if (intel_pstate_msrs_not_valid())
2543 return -ENODEV;
2544
7791e4aa
SP
2545hwp_cpu_matched:
2546 /*
2547 * The Intel pstate driver will be ignored if the platform
2548 * firmware has its own power management modes.
2549 */
2550 if (intel_pstate_platform_pwr_mgmt_exists())
2551 return -ENODEV;
2552
4836df17 2553 pr_info("Intel P-state driver initializing\n");
93f0822d 2554
b57ffac5 2555 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
93f0822d
DB
2556 if (!all_cpu_data)
2557 return -ENOMEM;
93f0822d 2558
d64c3b0b
KCA
2559 if (!hwp_active && hwp_only)
2560 goto out;
2561
d0ea59e1
RW
2562 intel_pstate_request_control_from_smm();
2563
0c30b65b
RW
2564 intel_pstate_sysfs_expose_params();
2565
001c76f0 2566 rc = cpufreq_register_driver(intel_pstate_driver);
93f0822d
DB
2567 if (rc)
2568 goto out;
2569
0c30b65b
RW
2570 mutex_lock(&intel_pstate_driver_lock);
2571 driver_registered = true;
2572 mutex_unlock(&intel_pstate_driver_lock);
2573
366430b5
RW
2574 if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2575 pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2576 intel_pstate_debug_expose_params();
2577
7791e4aa 2578 if (hwp_active)
4836df17 2579 pr_info("HWP enabled\n");
7791e4aa 2580
93f0822d
DB
2581 return rc;
2582out:
907cc908
DB
2583 get_online_cpus();
2584 for_each_online_cpu(cpu) {
2585 if (all_cpu_data[cpu]) {
001c76f0
RW
2586 if (intel_pstate_driver == &intel_pstate)
2587 intel_pstate_clear_update_util_hook(cpu);
2588
907cc908
DB
2589 kfree(all_cpu_data[cpu]);
2590 }
2591 }
2592
2593 put_online_cpus();
2594 vfree(all_cpu_data);
93f0822d
DB
2595 return -ENODEV;
2596}
2597device_initcall(intel_pstate_init);
2598
6be26498
DB
2599static int __init intel_pstate_setup(char *str)
2600{
2601 if (!str)
2602 return -EINVAL;
2603
001c76f0 2604 if (!strcmp(str, "disable")) {
6be26498 2605 no_load = 1;
001c76f0
RW
2606 } else if (!strcmp(str, "passive")) {
2607 pr_info("Passive mode enabled\n");
2608 intel_pstate_driver = &intel_cpufreq;
2609 no_hwp = 1;
2610 }
539342f6 2611 if (!strcmp(str, "no_hwp")) {
4836df17 2612 pr_info("HWP disabled\n");
2f86dc4c 2613 no_hwp = 1;
539342f6 2614 }
aa4ea34d
EZ
2615 if (!strcmp(str, "force"))
2616 force_load = 1;
d64c3b0b
KCA
2617 if (!strcmp(str, "hwp_only"))
2618 hwp_only = 1;
eae48f04
SP
2619 if (!strcmp(str, "per_cpu_perf_limits"))
2620 per_cpu_limits = true;
9522a2ff
SP
2621
2622#ifdef CONFIG_ACPI
2623 if (!strcmp(str, "support_acpi_ppc"))
2624 acpi_ppc = true;
2625#endif
2626
6be26498
DB
2627 return 0;
2628}
2629early_param("intel_pstate", intel_pstate_setup);
2630
93f0822d
DB
2631MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2632MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2633MODULE_LICENSE("GPL");