]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/cpufreq/intel_pstate.c
intel_pstate: Avoid unnecessary synchronize_sched() during initialization
[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>
93f0822d 38
938d21a2
PL
39#define ATOM_RATIOS 0x66a
40#define ATOM_VIDS 0x66b
41#define ATOM_TURBO_RATIOS 0x66c
42#define ATOM_TURBO_VIDS 0x66d
61d8d2ab 43
9522a2ff
SP
44#ifdef CONFIG_ACPI
45#include <acpi/processor.h>
46#endif
47
f0fe3cd7 48#define FRAC_BITS 8
93f0822d
DB
49#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
50#define fp_toint(X) ((X) >> FRAC_BITS)
f0fe3cd7 51
93f0822d
DB
52static inline int32_t mul_fp(int32_t x, int32_t y)
53{
54 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
55}
56
7180dddf 57static inline int32_t div_fp(s64 x, s64 y)
93f0822d 58{
7180dddf 59 return div64_s64((int64_t)x << FRAC_BITS, y);
93f0822d
DB
60}
61
d022a65e
DB
62static inline int ceiling_fp(int32_t x)
63{
64 int mask, ret;
65
66 ret = fp_toint(x);
67 mask = (1 << FRAC_BITS) - 1;
68 if (x & mask)
69 ret += 1;
70 return ret;
71}
72
13ad7701
SP
73/**
74 * struct sample - Store performance sample
75 * @core_pct_busy: Ratio of APERF/MPERF in percent, which is actual
76 * performance during last sample period
77 * @busy_scaled: Scaled busy value which is used to calculate next
78 * P state. This can be different than core_pct_busy
79 * to account for cpu idle period
80 * @aperf: Difference of actual performance frequency clock count
81 * read from APERF MSR between last and current sample
82 * @mperf: Difference of maximum performance frequency clock count
83 * read from MPERF MSR between last and current sample
84 * @tsc: Difference of time stamp counter between last and
85 * current sample
86 * @freq: Effective frequency calculated from APERF/MPERF
87 * @time: Current time from scheduler
88 *
89 * This structure is used in the cpudata structure to store performance sample
90 * data for choosing next P State.
91 */
93f0822d 92struct sample {
d253d2a5 93 int32_t core_pct_busy;
157386b6 94 int32_t busy_scaled;
93f0822d
DB
95 u64 aperf;
96 u64 mperf;
4055fad3 97 u64 tsc;
93f0822d 98 int freq;
a4675fbc 99 u64 time;
93f0822d
DB
100};
101
13ad7701
SP
102/**
103 * struct pstate_data - Store P state data
104 * @current_pstate: Current requested P state
105 * @min_pstate: Min P state possible for this platform
106 * @max_pstate: Max P state possible for this platform
107 * @max_pstate_physical:This is physical Max P state for a processor
108 * This can be higher than the max_pstate which can
109 * be limited by platform thermal design power limits
110 * @scaling: Scaling factor to convert frequency to cpufreq
111 * frequency units
112 * @turbo_pstate: Max Turbo P state possible for this platform
113 *
114 * Stores the per cpu model P state limits and current P state.
115 */
93f0822d
DB
116struct pstate_data {
117 int current_pstate;
118 int min_pstate;
119 int max_pstate;
3bcc6fa9 120 int max_pstate_physical;
b27580b0 121 int scaling;
93f0822d
DB
122 int turbo_pstate;
123};
124
13ad7701
SP
125/**
126 * struct vid_data - Stores voltage information data
127 * @min: VID data for this platform corresponding to
128 * the lowest P state
129 * @max: VID data corresponding to the highest P State.
130 * @turbo: VID data for turbo P state
131 * @ratio: Ratio of (vid max - vid min) /
132 * (max P state - Min P State)
133 *
134 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
135 * This data is used in Atom platforms, where in addition to target P state,
136 * the voltage data needs to be specified to select next P State.
137 */
007bea09 138struct vid_data {
21855ff5
DB
139 int min;
140 int max;
141 int turbo;
007bea09
DB
142 int32_t ratio;
143};
144
13ad7701
SP
145/**
146 * struct _pid - Stores PID data
147 * @setpoint: Target set point for busyness or performance
148 * @integral: Storage for accumulated error values
149 * @p_gain: PID proportional gain
150 * @i_gain: PID integral gain
151 * @d_gain: PID derivative gain
152 * @deadband: PID deadband
153 * @last_err: Last error storage for integral part of PID calculation
154 *
155 * Stores PID coefficients and last error for PID controller.
156 */
93f0822d
DB
157struct _pid {
158 int setpoint;
159 int32_t integral;
160 int32_t p_gain;
161 int32_t i_gain;
162 int32_t d_gain;
163 int deadband;
d253d2a5 164 int32_t last_err;
93f0822d
DB
165};
166
13ad7701
SP
167/**
168 * struct cpudata - Per CPU instance data storage
169 * @cpu: CPU number for this instance data
170 * @update_util: CPUFreq utility callback information
4578ee7e 171 * @update_util_set: CPUFreq utility callback is set
13ad7701
SP
172 * @pstate: Stores P state limits for this CPU
173 * @vid: Stores VID limits for this CPU
174 * @pid: Stores PID parameters for this CPU
175 * @last_sample_time: Last Sample time
176 * @prev_aperf: Last APERF value read from APERF MSR
177 * @prev_mperf: Last MPERF value read from MPERF MSR
178 * @prev_tsc: Last timestamp counter (TSC) value
179 * @prev_cummulative_iowait: IO Wait time difference from last and
180 * current sample
181 * @sample: Storage for storing last Sample data
9522a2ff
SP
182 * @acpi_perf_data: Stores ACPI perf information read from _PSS
183 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
13ad7701
SP
184 *
185 * This structure stores per CPU instance data for all CPUs.
186 */
93f0822d
DB
187struct cpudata {
188 int cpu;
189
a4675fbc 190 struct update_util_data update_util;
4578ee7e 191 bool update_util_set;
93f0822d 192
93f0822d 193 struct pstate_data pstate;
007bea09 194 struct vid_data vid;
93f0822d 195 struct _pid pid;
93f0822d 196
a4675fbc 197 u64 last_sample_time;
93f0822d
DB
198 u64 prev_aperf;
199 u64 prev_mperf;
4055fad3 200 u64 prev_tsc;
63d1d656 201 u64 prev_cummulative_iowait;
d37e2b76 202 struct sample sample;
9522a2ff
SP
203#ifdef CONFIG_ACPI
204 struct acpi_processor_performance acpi_perf_data;
205 bool valid_pss_table;
206#endif
93f0822d
DB
207};
208
209static struct cpudata **all_cpu_data;
13ad7701
SP
210
211/**
212 * struct pid_adjust_policy - Stores static PID configuration data
213 * @sample_rate_ms: PID calculation sample rate in ms
214 * @sample_rate_ns: Sample rate calculation in ns
215 * @deadband: PID deadband
216 * @setpoint: PID Setpoint
217 * @p_gain_pct: PID proportional gain
218 * @i_gain_pct: PID integral gain
219 * @d_gain_pct: PID derivative gain
220 *
221 * Stores per CPU model static PID configuration data.
222 */
93f0822d
DB
223struct pstate_adjust_policy {
224 int sample_rate_ms;
a4675fbc 225 s64 sample_rate_ns;
93f0822d
DB
226 int deadband;
227 int setpoint;
228 int p_gain_pct;
229 int d_gain_pct;
230 int i_gain_pct;
231};
232
13ad7701
SP
233/**
234 * struct pstate_funcs - Per CPU model specific callbacks
235 * @get_max: Callback to get maximum non turbo effective P state
236 * @get_max_physical: Callback to get maximum non turbo physical P state
237 * @get_min: Callback to get minimum P state
238 * @get_turbo: Callback to get turbo P state
239 * @get_scaling: Callback to get frequency scaling factor
240 * @get_val: Callback to convert P state to actual MSR write value
241 * @get_vid: Callback to get VID data for Atom platforms
242 * @get_target_pstate: Callback to a function to calculate next P state to use
243 *
244 * Core and Atom CPU models have different way to get P State limits. This
245 * structure is used to store those callbacks.
246 */
016c8150
DB
247struct pstate_funcs {
248 int (*get_max)(void);
3bcc6fa9 249 int (*get_max_physical)(void);
016c8150
DB
250 int (*get_min)(void);
251 int (*get_turbo)(void);
b27580b0 252 int (*get_scaling)(void);
fdfdb2b1 253 u64 (*get_val)(struct cpudata*, int pstate);
007bea09 254 void (*get_vid)(struct cpudata *);
157386b6 255 int32_t (*get_target_pstate)(struct cpudata *);
93f0822d
DB
256};
257
13ad7701
SP
258/**
259 * struct cpu_defaults- Per CPU model default config data
260 * @pid_policy: PID config data
261 * @funcs: Callback function data
262 */
016c8150
DB
263struct cpu_defaults {
264 struct pstate_adjust_policy pid_policy;
265 struct pstate_funcs funcs;
93f0822d
DB
266};
267
157386b6 268static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
e70eed2b 269static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
157386b6 270
016c8150
DB
271static struct pstate_adjust_policy pid_params;
272static struct pstate_funcs pstate_funcs;
2f86dc4c 273static int hwp_active;
016c8150 274
9522a2ff
SP
275#ifdef CONFIG_ACPI
276static bool acpi_ppc;
277#endif
13ad7701
SP
278
279/**
280 * struct perf_limits - Store user and policy limits
281 * @no_turbo: User requested turbo state from intel_pstate sysfs
282 * @turbo_disabled: Platform turbo status either from msr
283 * MSR_IA32_MISC_ENABLE or when maximum available pstate
284 * matches the maximum turbo pstate
285 * @max_perf_pct: Effective maximum performance limit in percentage, this
286 * is minimum of either limits enforced by cpufreq policy
287 * or limits from user set limits via intel_pstate sysfs
288 * @min_perf_pct: Effective minimum performance limit in percentage, this
289 * is maximum of either limits enforced by cpufreq policy
290 * or limits from user set limits via intel_pstate sysfs
291 * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
292 * This value is used to limit max pstate
293 * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
294 * This value is used to limit min pstate
295 * @max_policy_pct: The maximum performance in percentage enforced by
296 * cpufreq setpolicy interface
297 * @max_sysfs_pct: The maximum performance in percentage enforced by
298 * intel pstate sysfs interface
299 * @min_policy_pct: The minimum performance in percentage enforced by
300 * cpufreq setpolicy interface
301 * @min_sysfs_pct: The minimum performance in percentage enforced by
302 * intel pstate sysfs interface
303 *
304 * Storage for user and policy defined limits.
305 */
93f0822d
DB
306struct perf_limits {
307 int no_turbo;
dd5fbf70 308 int turbo_disabled;
93f0822d
DB
309 int max_perf_pct;
310 int min_perf_pct;
311 int32_t max_perf;
312 int32_t min_perf;
d8f469e9
DB
313 int max_policy_pct;
314 int max_sysfs_pct;
a0475992
KCA
315 int min_policy_pct;
316 int min_sysfs_pct;
93f0822d
DB
317};
318
51443fbf
PB
319static struct perf_limits performance_limits = {
320 .no_turbo = 0,
321 .turbo_disabled = 0,
322 .max_perf_pct = 100,
323 .max_perf = int_tofp(1),
324 .min_perf_pct = 100,
325 .min_perf = int_tofp(1),
326 .max_policy_pct = 100,
327 .max_sysfs_pct = 100,
328 .min_policy_pct = 0,
329 .min_sysfs_pct = 0,
330};
331
332static struct perf_limits powersave_limits = {
93f0822d 333 .no_turbo = 0,
4521e1a0 334 .turbo_disabled = 0,
93f0822d
DB
335 .max_perf_pct = 100,
336 .max_perf = int_tofp(1),
337 .min_perf_pct = 0,
338 .min_perf = 0,
d8f469e9
DB
339 .max_policy_pct = 100,
340 .max_sysfs_pct = 100,
a0475992
KCA
341 .min_policy_pct = 0,
342 .min_sysfs_pct = 0,
93f0822d
DB
343};
344
51443fbf
PB
345#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
346static struct perf_limits *limits = &performance_limits;
347#else
348static struct perf_limits *limits = &powersave_limits;
349#endif
350
9522a2ff 351#ifdef CONFIG_ACPI
2b3ec765
SP
352
353static bool intel_pstate_get_ppc_enable_status(void)
354{
355 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
356 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
357 return true;
358
359 return acpi_ppc;
360}
361
9522a2ff
SP
362/*
363 * The max target pstate ratio is a 8 bit value in both PLATFORM_INFO MSR and
364 * in TURBO_RATIO_LIMIT MSR, which pstate driver stores in max_pstate and
365 * max_turbo_pstate fields. The PERF_CTL MSR contains 16 bit value for P state
366 * ratio, out of it only high 8 bits are used. For example 0x1700 is setting
367 * target ratio 0x17. The _PSS control value stores in a format which can be
368 * directly written to PERF_CTL MSR. But in intel_pstate driver this shift
369 * occurs during write to PERF_CTL (E.g. for cores core_set_pstate()).
370 * This function converts the _PSS control value to intel pstate driver format
371 * for comparison and assignment.
372 */
373static int convert_to_native_pstate_format(struct cpudata *cpu, int index)
374{
375 return cpu->acpi_perf_data.states[index].control >> 8;
376}
377
378static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
379{
380 struct cpudata *cpu;
381 int turbo_pss_ctl;
382 int ret;
383 int i;
384
e59a8f7f
SP
385 if (hwp_active)
386 return;
387
2b3ec765 388 if (!intel_pstate_get_ppc_enable_status())
9522a2ff
SP
389 return;
390
391 cpu = all_cpu_data[policy->cpu];
392
393 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
394 policy->cpu);
395 if (ret)
396 return;
397
398 /*
399 * Check if the control value in _PSS is for PERF_CTL MSR, which should
400 * guarantee that the states returned by it map to the states in our
401 * list directly.
402 */
403 if (cpu->acpi_perf_data.control_register.space_id !=
404 ACPI_ADR_SPACE_FIXED_HARDWARE)
405 goto err;
406
407 /*
408 * If there is only one entry _PSS, simply ignore _PSS and continue as
409 * usual without taking _PSS into account
410 */
411 if (cpu->acpi_perf_data.state_count < 2)
412 goto err;
413
414 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
415 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
416 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
417 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
418 (u32) cpu->acpi_perf_data.states[i].core_frequency,
419 (u32) cpu->acpi_perf_data.states[i].power,
420 (u32) cpu->acpi_perf_data.states[i].control);
421 }
422
423 /*
424 * The _PSS table doesn't contain whole turbo frequency range.
425 * This just contains +1 MHZ above the max non turbo frequency,
426 * with control value corresponding to max turbo ratio. But
427 * when cpufreq set policy is called, it will call with this
428 * max frequency, which will cause a reduced performance as
429 * this driver uses real max turbo frequency as the max
430 * frequency. So correct this frequency in _PSS table to
431 * correct max turbo frequency based on the turbo ratio.
432 * Also need to convert to MHz as _PSS freq is in MHz.
433 */
434 turbo_pss_ctl = convert_to_native_pstate_format(cpu, 0);
435 if (turbo_pss_ctl > cpu->pstate.max_pstate)
436 cpu->acpi_perf_data.states[0].core_frequency =
437 policy->cpuinfo.max_freq / 1000;
438 cpu->valid_pss_table = true;
439 pr_info("_PPC limits will be enforced\n");
440
441 return;
442
443 err:
444 cpu->valid_pss_table = false;
445 acpi_processor_unregister_performance(policy->cpu);
446}
447
448static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
449{
450 struct cpudata *cpu;
451
452 cpu = all_cpu_data[policy->cpu];
453 if (!cpu->valid_pss_table)
454 return;
455
456 acpi_processor_unregister_performance(policy->cpu);
457}
458
459#else
460static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
461{
462}
463
464static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
465{
466}
467#endif
468
93f0822d 469static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
c410833a 470 int deadband, int integral) {
b54a0dfd
PL
471 pid->setpoint = int_tofp(setpoint);
472 pid->deadband = int_tofp(deadband);
93f0822d 473 pid->integral = int_tofp(integral);
d98d099b 474 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
93f0822d
DB
475}
476
477static inline void pid_p_gain_set(struct _pid *pid, int percent)
478{
22590efb 479 pid->p_gain = div_fp(percent, 100);
93f0822d
DB
480}
481
482static inline void pid_i_gain_set(struct _pid *pid, int percent)
483{
22590efb 484 pid->i_gain = div_fp(percent, 100);
93f0822d
DB
485}
486
487static inline void pid_d_gain_set(struct _pid *pid, int percent)
488{
22590efb 489 pid->d_gain = div_fp(percent, 100);
93f0822d
DB
490}
491
d253d2a5 492static signed int pid_calc(struct _pid *pid, int32_t busy)
93f0822d 493{
d253d2a5 494 signed int result;
93f0822d
DB
495 int32_t pterm, dterm, fp_error;
496 int32_t integral_limit;
497
b54a0dfd 498 fp_error = pid->setpoint - busy;
93f0822d 499
b54a0dfd 500 if (abs(fp_error) <= pid->deadband)
93f0822d
DB
501 return 0;
502
503 pterm = mul_fp(pid->p_gain, fp_error);
504
505 pid->integral += fp_error;
506
e0d4c8f8
KCA
507 /*
508 * We limit the integral here so that it will never
509 * get higher than 30. This prevents it from becoming
510 * too large an input over long periods of time and allows
511 * it to get factored out sooner.
512 *
513 * The value of 30 was chosen through experimentation.
514 */
93f0822d
DB
515 integral_limit = int_tofp(30);
516 if (pid->integral > integral_limit)
517 pid->integral = integral_limit;
518 if (pid->integral < -integral_limit)
519 pid->integral = -integral_limit;
520
d253d2a5
BS
521 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
522 pid->last_err = fp_error;
93f0822d
DB
523
524 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
51d211e9 525 result = result + (1 << (FRAC_BITS-1));
93f0822d
DB
526 return (signed int)fp_toint(result);
527}
528
529static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
530{
016c8150
DB
531 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
532 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
533 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
93f0822d 534
2d8d1f18 535 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
93f0822d
DB
536}
537
93f0822d
DB
538static inline void intel_pstate_reset_all_pid(void)
539{
540 unsigned int cpu;
845c1cbe 541
93f0822d
DB
542 for_each_online_cpu(cpu) {
543 if (all_cpu_data[cpu])
544 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
545 }
546}
547
4521e1a0
GM
548static inline void update_turbo_state(void)
549{
550 u64 misc_en;
551 struct cpudata *cpu;
552
553 cpu = all_cpu_data[0];
554 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
51443fbf 555 limits->turbo_disabled =
4521e1a0
GM
556 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
557 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
558}
559
41cfd64c 560static void intel_pstate_hwp_set(const struct cpumask *cpumask)
2f86dc4c 561{
74da56ce
KCA
562 int min, hw_min, max, hw_max, cpu, range, adj_range;
563 u64 value, cap;
564
565 rdmsrl(MSR_HWP_CAPABILITIES, cap);
566 hw_min = HWP_LOWEST_PERF(cap);
567 hw_max = HWP_HIGHEST_PERF(cap);
568 range = hw_max - hw_min;
2f86dc4c 569
41cfd64c 570 for_each_cpu(cpu, cpumask) {
2f86dc4c 571 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
51443fbf 572 adj_range = limits->min_perf_pct * range / 100;
74da56ce 573 min = hw_min + adj_range;
2f86dc4c
DB
574 value &= ~HWP_MIN_PERF(~0L);
575 value |= HWP_MIN_PERF(min);
576
51443fbf 577 adj_range = limits->max_perf_pct * range / 100;
74da56ce 578 max = hw_min + adj_range;
51443fbf 579 if (limits->no_turbo) {
74da56ce
KCA
580 hw_max = HWP_GUARANTEED_PERF(cap);
581 if (hw_max < max)
582 max = hw_max;
2f86dc4c
DB
583 }
584
585 value &= ~HWP_MAX_PERF(~0L);
586 value |= HWP_MAX_PERF(max);
587 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
588 }
41cfd64c 589}
2f86dc4c 590
ba41e1bc
RW
591static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
592{
593 if (hwp_active)
594 intel_pstate_hwp_set(policy->cpus);
595
596 return 0;
597}
598
41cfd64c
VK
599static void intel_pstate_hwp_set_online_cpus(void)
600{
601 get_online_cpus();
602 intel_pstate_hwp_set(cpu_online_mask);
2f86dc4c
DB
603 put_online_cpus();
604}
605
93f0822d
DB
606/************************** debugfs begin ************************/
607static int pid_param_set(void *data, u64 val)
608{
609 *(u32 *)data = val;
610 intel_pstate_reset_all_pid();
611 return 0;
612}
845c1cbe 613
93f0822d
DB
614static int pid_param_get(void *data, u64 *val)
615{
616 *val = *(u32 *)data;
617 return 0;
618}
2d8d1f18 619DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
93f0822d
DB
620
621struct pid_param {
622 char *name;
623 void *value;
624};
625
626static struct pid_param pid_files[] = {
016c8150
DB
627 {"sample_rate_ms", &pid_params.sample_rate_ms},
628 {"d_gain_pct", &pid_params.d_gain_pct},
629 {"i_gain_pct", &pid_params.i_gain_pct},
630 {"deadband", &pid_params.deadband},
631 {"setpoint", &pid_params.setpoint},
632 {"p_gain_pct", &pid_params.p_gain_pct},
93f0822d
DB
633 {NULL, NULL}
634};
635
317dd50e 636static void __init intel_pstate_debug_expose_params(void)
93f0822d 637{
317dd50e 638 struct dentry *debugfs_parent;
93f0822d
DB
639 int i = 0;
640
2f86dc4c
DB
641 if (hwp_active)
642 return;
93f0822d
DB
643 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
644 if (IS_ERR_OR_NULL(debugfs_parent))
645 return;
646 while (pid_files[i].name) {
647 debugfs_create_file(pid_files[i].name, 0660,
c410833a
SK
648 debugfs_parent, pid_files[i].value,
649 &fops_pid_param);
93f0822d
DB
650 i++;
651 }
652}
653
654/************************** debugfs end ************************/
655
656/************************** sysfs begin ************************/
657#define show_one(file_name, object) \
658 static ssize_t show_##file_name \
659 (struct kobject *kobj, struct attribute *attr, char *buf) \
660 { \
51443fbf 661 return sprintf(buf, "%u\n", limits->object); \
93f0822d
DB
662 }
663
d01b1f48
KCA
664static ssize_t show_turbo_pct(struct kobject *kobj,
665 struct attribute *attr, char *buf)
666{
667 struct cpudata *cpu;
668 int total, no_turbo, turbo_pct;
669 uint32_t turbo_fp;
670
671 cpu = all_cpu_data[0];
672
673 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
674 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
22590efb 675 turbo_fp = div_fp(no_turbo, total);
d01b1f48
KCA
676 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
677 return sprintf(buf, "%u\n", turbo_pct);
678}
679
0522424e
KCA
680static ssize_t show_num_pstates(struct kobject *kobj,
681 struct attribute *attr, char *buf)
682{
683 struct cpudata *cpu;
684 int total;
685
686 cpu = all_cpu_data[0];
687 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
688 return sprintf(buf, "%u\n", total);
689}
690
4521e1a0
GM
691static ssize_t show_no_turbo(struct kobject *kobj,
692 struct attribute *attr, char *buf)
693{
694 ssize_t ret;
695
696 update_turbo_state();
51443fbf
PB
697 if (limits->turbo_disabled)
698 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
4521e1a0 699 else
51443fbf 700 ret = sprintf(buf, "%u\n", limits->no_turbo);
4521e1a0
GM
701
702 return ret;
703}
704
93f0822d 705static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
c410833a 706 const char *buf, size_t count)
93f0822d
DB
707{
708 unsigned int input;
709 int ret;
845c1cbe 710
93f0822d
DB
711 ret = sscanf(buf, "%u", &input);
712 if (ret != 1)
713 return -EINVAL;
4521e1a0
GM
714
715 update_turbo_state();
51443fbf 716 if (limits->turbo_disabled) {
4836df17 717 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
4521e1a0 718 return -EPERM;
dd5fbf70 719 }
2f86dc4c 720
51443fbf 721 limits->no_turbo = clamp_t(int, input, 0, 1);
4521e1a0 722
2f86dc4c 723 if (hwp_active)
41cfd64c 724 intel_pstate_hwp_set_online_cpus();
2f86dc4c 725
93f0822d
DB
726 return count;
727}
728
729static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
c410833a 730 const char *buf, size_t count)
93f0822d
DB
731{
732 unsigned int input;
733 int ret;
845c1cbe 734
93f0822d
DB
735 ret = sscanf(buf, "%u", &input);
736 if (ret != 1)
737 return -EINVAL;
738
51443fbf
PB
739 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
740 limits->max_perf_pct = min(limits->max_policy_pct,
741 limits->max_sysfs_pct);
742 limits->max_perf_pct = max(limits->min_policy_pct,
743 limits->max_perf_pct);
744 limits->max_perf_pct = max(limits->min_perf_pct,
745 limits->max_perf_pct);
22590efb 746 limits->max_perf = div_fp(limits->max_perf_pct, 100);
845c1cbe 747
2f86dc4c 748 if (hwp_active)
41cfd64c 749 intel_pstate_hwp_set_online_cpus();
93f0822d
DB
750 return count;
751}
752
753static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
c410833a 754 const char *buf, size_t count)
93f0822d
DB
755{
756 unsigned int input;
757 int ret;
845c1cbe 758
93f0822d
DB
759 ret = sscanf(buf, "%u", &input);
760 if (ret != 1)
761 return -EINVAL;
a0475992 762
51443fbf
PB
763 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
764 limits->min_perf_pct = max(limits->min_policy_pct,
765 limits->min_sysfs_pct);
766 limits->min_perf_pct = min(limits->max_policy_pct,
767 limits->min_perf_pct);
768 limits->min_perf_pct = min(limits->max_perf_pct,
769 limits->min_perf_pct);
22590efb 770 limits->min_perf = div_fp(limits->min_perf_pct, 100);
93f0822d 771
2f86dc4c 772 if (hwp_active)
41cfd64c 773 intel_pstate_hwp_set_online_cpus();
93f0822d
DB
774 return count;
775}
776
93f0822d
DB
777show_one(max_perf_pct, max_perf_pct);
778show_one(min_perf_pct, min_perf_pct);
779
780define_one_global_rw(no_turbo);
781define_one_global_rw(max_perf_pct);
782define_one_global_rw(min_perf_pct);
d01b1f48 783define_one_global_ro(turbo_pct);
0522424e 784define_one_global_ro(num_pstates);
93f0822d
DB
785
786static struct attribute *intel_pstate_attributes[] = {
787 &no_turbo.attr,
788 &max_perf_pct.attr,
789 &min_perf_pct.attr,
d01b1f48 790 &turbo_pct.attr,
0522424e 791 &num_pstates.attr,
93f0822d
DB
792 NULL
793};
794
795static struct attribute_group intel_pstate_attr_group = {
796 .attrs = intel_pstate_attributes,
797};
93f0822d 798
317dd50e 799static void __init intel_pstate_sysfs_expose_params(void)
93f0822d 800{
317dd50e 801 struct kobject *intel_pstate_kobject;
93f0822d
DB
802 int rc;
803
804 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
805 &cpu_subsys.dev_root->kobj);
806 BUG_ON(!intel_pstate_kobject);
2d8d1f18 807 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
93f0822d
DB
808 BUG_ON(rc);
809}
93f0822d 810/************************** sysfs end ************************/
2f86dc4c 811
ba88d433 812static void intel_pstate_hwp_enable(struct cpudata *cpudata)
2f86dc4c 813{
f05c9665
SP
814 /* First disable HWP notification interrupt as we don't process them */
815 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
816
ba88d433 817 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
2f86dc4c
DB
818}
819
938d21a2 820static int atom_get_min_pstate(void)
19e77c28
DB
821{
822 u64 value;
845c1cbe 823
938d21a2 824 rdmsrl(ATOM_RATIOS, value);
c16ed060 825 return (value >> 8) & 0x7F;
19e77c28
DB
826}
827
938d21a2 828static int atom_get_max_pstate(void)
19e77c28
DB
829{
830 u64 value;
845c1cbe 831
938d21a2 832 rdmsrl(ATOM_RATIOS, value);
c16ed060 833 return (value >> 16) & 0x7F;
19e77c28 834}
93f0822d 835
938d21a2 836static int atom_get_turbo_pstate(void)
61d8d2ab
DB
837{
838 u64 value;
845c1cbe 839
938d21a2 840 rdmsrl(ATOM_TURBO_RATIOS, value);
c16ed060 841 return value & 0x7F;
61d8d2ab
DB
842}
843
fdfdb2b1 844static u64 atom_get_val(struct cpudata *cpudata, int pstate)
007bea09
DB
845{
846 u64 val;
847 int32_t vid_fp;
848 u32 vid;
849
144c8e17 850 val = (u64)pstate << 8;
51443fbf 851 if (limits->no_turbo && !limits->turbo_disabled)
007bea09
DB
852 val |= (u64)1 << 32;
853
854 vid_fp = cpudata->vid.min + mul_fp(
855 int_tofp(pstate - cpudata->pstate.min_pstate),
856 cpudata->vid.ratio);
857
858 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
d022a65e 859 vid = ceiling_fp(vid_fp);
007bea09 860
21855ff5
DB
861 if (pstate > cpudata->pstate.max_pstate)
862 vid = cpudata->vid.turbo;
863
fdfdb2b1 864 return val | vid;
007bea09
DB
865}
866
1421df63 867static int silvermont_get_scaling(void)
b27580b0
DB
868{
869 u64 value;
870 int i;
1421df63
PL
871 /* Defined in Table 35-6 from SDM (Sept 2015) */
872 static int silvermont_freq_table[] = {
873 83300, 100000, 133300, 116700, 80000};
b27580b0
DB
874
875 rdmsrl(MSR_FSB_FREQ, value);
1421df63
PL
876 i = value & 0x7;
877 WARN_ON(i > 4);
b27580b0 878
1421df63
PL
879 return silvermont_freq_table[i];
880}
b27580b0 881
1421df63
PL
882static int airmont_get_scaling(void)
883{
884 u64 value;
885 int i;
886 /* Defined in Table 35-10 from SDM (Sept 2015) */
887 static int airmont_freq_table[] = {
888 83300, 100000, 133300, 116700, 80000,
889 93300, 90000, 88900, 87500};
890
891 rdmsrl(MSR_FSB_FREQ, value);
892 i = value & 0xF;
893 WARN_ON(i > 8);
894
895 return airmont_freq_table[i];
b27580b0
DB
896}
897
938d21a2 898static void atom_get_vid(struct cpudata *cpudata)
007bea09
DB
899{
900 u64 value;
901
938d21a2 902 rdmsrl(ATOM_VIDS, value);
c16ed060
DB
903 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
904 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
007bea09
DB
905 cpudata->vid.ratio = div_fp(
906 cpudata->vid.max - cpudata->vid.min,
907 int_tofp(cpudata->pstate.max_pstate -
908 cpudata->pstate.min_pstate));
21855ff5 909
938d21a2 910 rdmsrl(ATOM_TURBO_VIDS, value);
21855ff5 911 cpudata->vid.turbo = value & 0x7f;
007bea09
DB
912}
913
016c8150 914static int core_get_min_pstate(void)
93f0822d
DB
915{
916 u64 value;
845c1cbe 917
05e99c8c 918 rdmsrl(MSR_PLATFORM_INFO, value);
93f0822d
DB
919 return (value >> 40) & 0xFF;
920}
921
3bcc6fa9 922static int core_get_max_pstate_physical(void)
93f0822d
DB
923{
924 u64 value;
845c1cbe 925
05e99c8c 926 rdmsrl(MSR_PLATFORM_INFO, value);
93f0822d
DB
927 return (value >> 8) & 0xFF;
928}
929
016c8150 930static int core_get_max_pstate(void)
93f0822d 931{
6a35fc2d
SP
932 u64 tar;
933 u64 plat_info;
934 int max_pstate;
935 int err;
936
937 rdmsrl(MSR_PLATFORM_INFO, plat_info);
938 max_pstate = (plat_info >> 8) & 0xFF;
939
940 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
941 if (!err) {
942 /* Do some sanity checking for safety */
943 if (plat_info & 0x600000000) {
944 u64 tdp_ctrl;
945 u64 tdp_ratio;
946 int tdp_msr;
947
948 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
949 if (err)
950 goto skip_tar;
951
952 tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
953 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
954 if (err)
955 goto skip_tar;
956
1becf035
SP
957 /* For level 1 and 2, bits[23:16] contain the ratio */
958 if (tdp_ctrl)
959 tdp_ratio >>= 16;
960
961 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
6a35fc2d
SP
962 if (tdp_ratio - 1 == tar) {
963 max_pstate = tar;
964 pr_debug("max_pstate=TAC %x\n", max_pstate);
965 } else {
966 goto skip_tar;
967 }
968 }
969 }
845c1cbe 970
6a35fc2d
SP
971skip_tar:
972 return max_pstate;
93f0822d
DB
973}
974
016c8150 975static int core_get_turbo_pstate(void)
93f0822d
DB
976{
977 u64 value;
978 int nont, ret;
845c1cbe 979
05e99c8c 980 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
016c8150 981 nont = core_get_max_pstate();
285cb990 982 ret = (value) & 255;
93f0822d
DB
983 if (ret <= nont)
984 ret = nont;
985 return ret;
986}
987
b27580b0
DB
988static inline int core_get_scaling(void)
989{
990 return 100000;
991}
992
fdfdb2b1 993static u64 core_get_val(struct cpudata *cpudata, int pstate)
016c8150
DB
994{
995 u64 val;
996
144c8e17 997 val = (u64)pstate << 8;
51443fbf 998 if (limits->no_turbo && !limits->turbo_disabled)
016c8150
DB
999 val |= (u64)1 << 32;
1000
fdfdb2b1 1001 return val;
016c8150
DB
1002}
1003
b34ef932
DC
1004static int knl_get_turbo_pstate(void)
1005{
1006 u64 value;
1007 int nont, ret;
1008
1009 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
1010 nont = core_get_max_pstate();
1011 ret = (((value) >> 8) & 0xFF);
1012 if (ret <= nont)
1013 ret = nont;
1014 return ret;
1015}
1016
016c8150
DB
1017static struct cpu_defaults core_params = {
1018 .pid_policy = {
1019 .sample_rate_ms = 10,
1020 .deadband = 0,
1021 .setpoint = 97,
1022 .p_gain_pct = 20,
1023 .d_gain_pct = 0,
1024 .i_gain_pct = 0,
1025 },
1026 .funcs = {
1027 .get_max = core_get_max_pstate,
3bcc6fa9 1028 .get_max_physical = core_get_max_pstate_physical,
016c8150
DB
1029 .get_min = core_get_min_pstate,
1030 .get_turbo = core_get_turbo_pstate,
b27580b0 1031 .get_scaling = core_get_scaling,
fdfdb2b1 1032 .get_val = core_get_val,
157386b6 1033 .get_target_pstate = get_target_pstate_use_performance,
016c8150
DB
1034 },
1035};
1036
1421df63
PL
1037static struct cpu_defaults silvermont_params = {
1038 .pid_policy = {
1039 .sample_rate_ms = 10,
1040 .deadband = 0,
1041 .setpoint = 60,
1042 .p_gain_pct = 14,
1043 .d_gain_pct = 0,
1044 .i_gain_pct = 4,
1045 },
1046 .funcs = {
1047 .get_max = atom_get_max_pstate,
1048 .get_max_physical = atom_get_max_pstate,
1049 .get_min = atom_get_min_pstate,
1050 .get_turbo = atom_get_turbo_pstate,
fdfdb2b1 1051 .get_val = atom_get_val,
1421df63
PL
1052 .get_scaling = silvermont_get_scaling,
1053 .get_vid = atom_get_vid,
e70eed2b 1054 .get_target_pstate = get_target_pstate_use_cpu_load,
1421df63
PL
1055 },
1056};
1057
1058static struct cpu_defaults airmont_params = {
19e77c28
DB
1059 .pid_policy = {
1060 .sample_rate_ms = 10,
1061 .deadband = 0,
6a82ba6d 1062 .setpoint = 60,
19e77c28
DB
1063 .p_gain_pct = 14,
1064 .d_gain_pct = 0,
1065 .i_gain_pct = 4,
1066 },
1067 .funcs = {
938d21a2
PL
1068 .get_max = atom_get_max_pstate,
1069 .get_max_physical = atom_get_max_pstate,
1070 .get_min = atom_get_min_pstate,
1071 .get_turbo = atom_get_turbo_pstate,
fdfdb2b1 1072 .get_val = atom_get_val,
1421df63 1073 .get_scaling = airmont_get_scaling,
938d21a2 1074 .get_vid = atom_get_vid,
e70eed2b 1075 .get_target_pstate = get_target_pstate_use_cpu_load,
19e77c28
DB
1076 },
1077};
1078
b34ef932
DC
1079static struct cpu_defaults knl_params = {
1080 .pid_policy = {
1081 .sample_rate_ms = 10,
1082 .deadband = 0,
1083 .setpoint = 97,
1084 .p_gain_pct = 20,
1085 .d_gain_pct = 0,
1086 .i_gain_pct = 0,
1087 },
1088 .funcs = {
1089 .get_max = core_get_max_pstate,
3bcc6fa9 1090 .get_max_physical = core_get_max_pstate_physical,
b34ef932
DC
1091 .get_min = core_get_min_pstate,
1092 .get_turbo = knl_get_turbo_pstate,
69cefc27 1093 .get_scaling = core_get_scaling,
fdfdb2b1 1094 .get_val = core_get_val,
157386b6 1095 .get_target_pstate = get_target_pstate_use_performance,
b34ef932
DC
1096 },
1097};
1098
93f0822d
DB
1099static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1100{
1101 int max_perf = cpu->pstate.turbo_pstate;
7244cb62 1102 int max_perf_adj;
93f0822d 1103 int min_perf;
845c1cbe 1104
51443fbf 1105 if (limits->no_turbo || limits->turbo_disabled)
93f0822d
DB
1106 max_perf = cpu->pstate.max_pstate;
1107
e0d4c8f8
KCA
1108 /*
1109 * performance can be limited by user through sysfs, by cpufreq
1110 * policy, or by cpu specific default values determined through
1111 * experimentation.
1112 */
a158bed5 1113 max_perf_adj = fp_toint(max_perf * limits->max_perf);
799281a3
RW
1114 *max = clamp_t(int, max_perf_adj,
1115 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
93f0822d 1116
a158bed5 1117 min_perf = fp_toint(max_perf * limits->min_perf);
799281a3 1118 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
93f0822d
DB
1119}
1120
fdfdb2b1 1121static inline void intel_pstate_record_pstate(struct cpudata *cpu, int pstate)
93f0822d 1122{
b27580b0 1123 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
93f0822d 1124 cpu->pstate.current_pstate = pstate;
fdfdb2b1 1125}
93f0822d 1126
fdfdb2b1
RW
1127static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1128{
1129 int pstate = cpu->pstate.min_pstate;
1130
1131 intel_pstate_record_pstate(cpu, pstate);
1132 /*
1133 * Generally, there is no guarantee that this code will always run on
1134 * the CPU being updated, so force the register update to run on the
1135 * right CPU.
1136 */
1137 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1138 pstate_funcs.get_val(cpu, pstate));
93f0822d
DB
1139}
1140
93f0822d
DB
1141static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1142{
016c8150
DB
1143 cpu->pstate.min_pstate = pstate_funcs.get_min();
1144 cpu->pstate.max_pstate = pstate_funcs.get_max();
3bcc6fa9 1145 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
016c8150 1146 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
b27580b0 1147 cpu->pstate.scaling = pstate_funcs.get_scaling();
93f0822d 1148
007bea09
DB
1149 if (pstate_funcs.get_vid)
1150 pstate_funcs.get_vid(cpu);
fdfdb2b1
RW
1151
1152 intel_pstate_set_min_pstate(cpu);
93f0822d
DB
1153}
1154
6b17ddb2 1155static inline void intel_pstate_calc_busy(struct cpudata *cpu)
93f0822d 1156{
6b17ddb2 1157 struct sample *sample = &cpu->sample;
bf810222 1158 int64_t core_pct;
93f0822d 1159
22590efb
RW
1160 core_pct = sample->aperf * int_tofp(100);
1161 core_pct = div64_u64(core_pct, sample->mperf);
e66c1768 1162
bf810222 1163 sample->core_pct_busy = (int32_t)core_pct;
93f0822d
DB
1164}
1165
4fec7ad5 1166static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
93f0822d 1167{
93f0822d 1168 u64 aperf, mperf;
4ab60c3f 1169 unsigned long flags;
4055fad3 1170 u64 tsc;
93f0822d 1171
4ab60c3f 1172 local_irq_save(flags);
93f0822d
DB
1173 rdmsrl(MSR_IA32_APERF, aperf);
1174 rdmsrl(MSR_IA32_MPERF, mperf);
e70eed2b 1175 tsc = rdtsc();
4fec7ad5 1176 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
8e601a9f 1177 local_irq_restore(flags);
4fec7ad5 1178 return false;
8e601a9f 1179 }
4ab60c3f 1180 local_irq_restore(flags);
b69880f9 1181
c4ee841f 1182 cpu->last_sample_time = cpu->sample.time;
a4675fbc 1183 cpu->sample.time = time;
d37e2b76
DB
1184 cpu->sample.aperf = aperf;
1185 cpu->sample.mperf = mperf;
4055fad3 1186 cpu->sample.tsc = tsc;
d37e2b76
DB
1187 cpu->sample.aperf -= cpu->prev_aperf;
1188 cpu->sample.mperf -= cpu->prev_mperf;
4055fad3 1189 cpu->sample.tsc -= cpu->prev_tsc;
1abc4b20 1190
93f0822d
DB
1191 cpu->prev_aperf = aperf;
1192 cpu->prev_mperf = mperf;
4055fad3 1193 cpu->prev_tsc = tsc;
febce40f
RW
1194 /*
1195 * First time this function is invoked in a given cycle, all of the
1196 * previous sample data fields are equal to zero or stale and they must
1197 * be populated with meaningful numbers for things to work, so assume
1198 * that sample.time will always be reset before setting the utilization
1199 * update hook and make the caller skip the sample then.
1200 */
1201 return !!cpu->last_sample_time;
93f0822d
DB
1202}
1203
8fa520af
PL
1204static inline int32_t get_avg_frequency(struct cpudata *cpu)
1205{
6d45b719
RW
1206 return fp_toint(mul_fp(cpu->sample.core_pct_busy,
1207 int_tofp(cpu->pstate.max_pstate_physical *
1208 cpu->pstate.scaling / 100)));
8fa520af
PL
1209}
1210
bdcaa23f
PL
1211static inline int32_t get_avg_pstate(struct cpudata *cpu)
1212{
1213 return div64_u64(cpu->pstate.max_pstate_physical * cpu->sample.aperf,
1214 cpu->sample.mperf);
1215}
1216
e70eed2b
PL
1217static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1218{
1219 struct sample *sample = &cpu->sample;
63d1d656
PL
1220 u64 cummulative_iowait, delta_iowait_us;
1221 u64 delta_iowait_mperf;
1222 u64 mperf, now;
e70eed2b
PL
1223 int32_t cpu_load;
1224
63d1d656
PL
1225 cummulative_iowait = get_cpu_iowait_time_us(cpu->cpu, &now);
1226
1227 /*
1228 * Convert iowait time into number of IO cycles spent at max_freq.
1229 * IO is considered as busy only for the cpu_load algorithm. For
1230 * performance this is not needed since we always try to reach the
1231 * maximum P-State, so we are already boosting the IOs.
1232 */
1233 delta_iowait_us = cummulative_iowait - cpu->prev_cummulative_iowait;
1234 delta_iowait_mperf = div64_u64(delta_iowait_us * cpu->pstate.scaling *
1235 cpu->pstate.max_pstate, MSEC_PER_SEC);
1236
1237 mperf = cpu->sample.mperf + delta_iowait_mperf;
1238 cpu->prev_cummulative_iowait = cummulative_iowait;
1239
e70eed2b
PL
1240 /*
1241 * The load can be estimated as the ratio of the mperf counter
1242 * running at a constant frequency during active periods
1243 * (C0) and the time stamp counter running at the same frequency
1244 * also during C-states.
1245 */
63d1d656 1246 cpu_load = div64_u64(int_tofp(100) * mperf, sample->tsc);
e70eed2b
PL
1247 cpu->sample.busy_scaled = cpu_load;
1248
bdcaa23f 1249 return get_avg_pstate(cpu) - pid_calc(&cpu->pid, cpu_load);
e70eed2b
PL
1250}
1251
157386b6 1252static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
93f0822d 1253{
c4ee841f 1254 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
a4675fbc 1255 u64 duration_ns;
93f0822d 1256
e0d4c8f8
KCA
1257 /*
1258 * core_busy is the ratio of actual performance to max
1259 * max_pstate is the max non turbo pstate available
1260 * current_pstate was the pstate that was requested during
1261 * the last sample period.
1262 *
1263 * We normalize core_busy, which was our actual percent
1264 * performance to what we requested during the last sample
1265 * period. The result will be a percentage of busy at a
1266 * specified pstate.
1267 */
d37e2b76 1268 core_busy = cpu->sample.core_pct_busy;
22590efb
RW
1269 max_pstate = cpu->pstate.max_pstate_physical;
1270 current_pstate = cpu->pstate.current_pstate;
e66c1768 1271 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
c4ee841f 1272
e0d4c8f8 1273 /*
a4675fbc
RW
1274 * Since our utilization update callback will not run unless we are
1275 * in C0, check if the actual elapsed time is significantly greater (3x)
1276 * than our sample interval. If it is, then we were idle for a long
1277 * enough period of time to adjust our busyness.
e0d4c8f8 1278 */
a4675fbc 1279 duration_ns = cpu->sample.time - cpu->last_sample_time;
febce40f 1280 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
22590efb 1281 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
c4ee841f 1282 core_busy = mul_fp(core_busy, sample_ratio);
ffb81056
RW
1283 } else {
1284 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1285 if (sample_ratio < int_tofp(1))
1286 core_busy = 0;
c4ee841f
DB
1287 }
1288
157386b6
PL
1289 cpu->sample.busy_scaled = core_busy;
1290 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, core_busy);
93f0822d
DB
1291}
1292
fdfdb2b1
RW
1293static inline void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1294{
1295 int max_perf, min_perf;
1296
1297 update_turbo_state();
1298
1299 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1300 pstate = clamp_t(int, pstate, min_perf, max_perf);
1301 if (pstate == cpu->pstate.current_pstate)
1302 return;
1303
1304 intel_pstate_record_pstate(cpu, pstate);
1305 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1306}
1307
93f0822d
DB
1308static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1309{
157386b6 1310 int from, target_pstate;
4055fad3
DS
1311 struct sample *sample;
1312
1313 from = cpu->pstate.current_pstate;
93f0822d 1314
157386b6 1315 target_pstate = pstate_funcs.get_target_pstate(cpu);
93f0822d 1316
fdfdb2b1 1317 intel_pstate_update_pstate(cpu, target_pstate);
4055fad3
DS
1318
1319 sample = &cpu->sample;
1320 trace_pstate_sample(fp_toint(sample->core_pct_busy),
157386b6 1321 fp_toint(sample->busy_scaled),
4055fad3
DS
1322 from,
1323 cpu->pstate.current_pstate,
1324 sample->mperf,
1325 sample->aperf,
1326 sample->tsc,
8fa520af 1327 get_avg_frequency(cpu));
93f0822d
DB
1328}
1329
a4675fbc
RW
1330static void intel_pstate_update_util(struct update_util_data *data, u64 time,
1331 unsigned long util, unsigned long max)
93f0822d 1332{
a4675fbc
RW
1333 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1334 u64 delta_ns = time - cpu->sample.time;
b69880f9 1335
a4675fbc 1336 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
4fec7ad5
RW
1337 bool sample_taken = intel_pstate_sample(cpu, time);
1338
6d45b719
RW
1339 if (sample_taken) {
1340 intel_pstate_calc_busy(cpu);
1341 if (!hwp_active)
1342 intel_pstate_adjust_busy_pstate(cpu);
1343 }
a4675fbc 1344 }
93f0822d
DB
1345}
1346
1347#define ICPU(model, policy) \
6cbd7ee1
DB
1348 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1349 (unsigned long)&policy }
93f0822d
DB
1350
1351static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
016c8150
DB
1352 ICPU(0x2a, core_params),
1353 ICPU(0x2d, core_params),
1421df63 1354 ICPU(0x37, silvermont_params),
016c8150
DB
1355 ICPU(0x3a, core_params),
1356 ICPU(0x3c, core_params),
c7e241df 1357 ICPU(0x3d, core_params),
016c8150
DB
1358 ICPU(0x3e, core_params),
1359 ICPU(0x3f, core_params),
1360 ICPU(0x45, core_params),
1361 ICPU(0x46, core_params),
43f8a966 1362 ICPU(0x47, core_params),
1421df63 1363 ICPU(0x4c, airmont_params),
7ab0256e 1364 ICPU(0x4e, core_params),
c7e241df 1365 ICPU(0x4f, core_params),
1c939123 1366 ICPU(0x5e, core_params),
c7e241df 1367 ICPU(0x56, core_params),
b34ef932 1368 ICPU(0x57, knl_params),
93f0822d
DB
1369 {}
1370};
1371MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1372
2f86dc4c
DB
1373static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
1374 ICPU(0x56, core_params),
1375 {}
1376};
1377
93f0822d
DB
1378static int intel_pstate_init_cpu(unsigned int cpunum)
1379{
93f0822d
DB
1380 struct cpudata *cpu;
1381
c0348717
DB
1382 if (!all_cpu_data[cpunum])
1383 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
1384 GFP_KERNEL);
93f0822d
DB
1385 if (!all_cpu_data[cpunum])
1386 return -ENOMEM;
1387
1388 cpu = all_cpu_data[cpunum];
1389
93f0822d 1390 cpu->cpu = cpunum;
ba88d433 1391
a4675fbc 1392 if (hwp_active) {
ba88d433 1393 intel_pstate_hwp_enable(cpu);
a4675fbc
RW
1394 pid_params.sample_rate_ms = 50;
1395 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1396 }
ba88d433 1397
179e8471 1398 intel_pstate_get_cpu_pstates(cpu);
016c8150 1399
93f0822d 1400 intel_pstate_busy_pid_reset(cpu);
93f0822d 1401
4836df17 1402 pr_debug("controlling: cpu %d\n", cpunum);
93f0822d
DB
1403
1404 return 0;
1405}
1406
1407static unsigned int intel_pstate_get(unsigned int cpu_num)
1408{
f96fd0c8 1409 struct cpudata *cpu = all_cpu_data[cpu_num];
93f0822d 1410
f96fd0c8 1411 return cpu ? get_avg_frequency(cpu) : 0;
93f0822d
DB
1412}
1413
febce40f 1414static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
bb6ab52f 1415{
febce40f
RW
1416 struct cpudata *cpu = all_cpu_data[cpu_num];
1417
1418 /* Prevent intel_pstate_update_util() from using stale data. */
1419 cpu->sample.time = 0;
0bed612b
RW
1420 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1421 intel_pstate_update_util);
4578ee7e 1422 cpu->update_util_set = true;
bb6ab52f
RW
1423}
1424
1425static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1426{
4578ee7e
CY
1427 struct cpudata *cpu_data = all_cpu_data[cpu];
1428
1429 if (!cpu_data->update_util_set)
1430 return;
1431
0bed612b 1432 cpufreq_remove_update_util_hook(cpu);
4578ee7e 1433 cpu_data->update_util_set = false;
bb6ab52f
RW
1434 synchronize_sched();
1435}
1436
30a39153
SP
1437static void intel_pstate_set_performance_limits(struct perf_limits *limits)
1438{
1439 limits->no_turbo = 0;
1440 limits->turbo_disabled = 0;
1441 limits->max_perf_pct = 100;
1442 limits->max_perf = int_tofp(1);
1443 limits->min_perf_pct = 100;
1444 limits->min_perf = int_tofp(1);
1445 limits->max_policy_pct = 100;
1446 limits->max_sysfs_pct = 100;
1447 limits->min_policy_pct = 0;
1448 limits->min_sysfs_pct = 0;
1449}
1450
93f0822d
DB
1451static int intel_pstate_set_policy(struct cpufreq_policy *policy)
1452{
3be9200d
SP
1453 struct cpudata *cpu;
1454
d3929b83
DB
1455 if (!policy->cpuinfo.max_freq)
1456 return -ENODEV;
1457
bb6ab52f
RW
1458 intel_pstate_clear_update_util_hook(policy->cpu);
1459
3be9200d
SP
1460 cpu = all_cpu_data[0];
1461 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate) {
1462 if (policy->max < policy->cpuinfo.max_freq &&
1463 policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
1464 pr_debug("policy->max > max non turbo frequency\n");
1465 policy->max = policy->cpuinfo.max_freq;
1466 }
1467 }
1468
30a39153 1469 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
51443fbf 1470 limits = &performance_limits;
30a39153 1471 if (policy->max >= policy->cpuinfo.max_freq) {
4836df17 1472 pr_debug("set performance\n");
30a39153
SP
1473 intel_pstate_set_performance_limits(limits);
1474 goto out;
1475 }
1476 } else {
4836df17 1477 pr_debug("set powersave\n");
30a39153 1478 limits = &powersave_limits;
93f0822d 1479 }
2f86dc4c 1480
51443fbf
PB
1481 limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
1482 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
8478f539
PB
1483 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
1484 policy->cpuinfo.max_freq);
51443fbf 1485 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
43717aad
CY
1486
1487 /* Normalize user input to [min_policy_pct, max_policy_pct] */
51443fbf
PB
1488 limits->min_perf_pct = max(limits->min_policy_pct,
1489 limits->min_sysfs_pct);
1490 limits->min_perf_pct = min(limits->max_policy_pct,
1491 limits->min_perf_pct);
1492 limits->max_perf_pct = min(limits->max_policy_pct,
1493 limits->max_sysfs_pct);
1494 limits->max_perf_pct = max(limits->min_policy_pct,
1495 limits->max_perf_pct);
88b7b7c0 1496 limits->max_perf = round_up(limits->max_perf, FRAC_BITS);
43717aad
CY
1497
1498 /* Make sure min_perf_pct <= max_perf_pct */
51443fbf 1499 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
43717aad 1500
22590efb
RW
1501 limits->min_perf = div_fp(limits->min_perf_pct, 100);
1502 limits->max_perf = div_fp(limits->max_perf_pct, 100);
93f0822d 1503
bb6ab52f
RW
1504 out:
1505 intel_pstate_set_update_util_hook(policy->cpu);
1506
ba41e1bc 1507 intel_pstate_hwp_set_policy(policy);
2f86dc4c 1508
93f0822d
DB
1509 return 0;
1510}
1511
1512static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1513{
be49e346 1514 cpufreq_verify_within_cpu_limits(policy);
93f0822d 1515
285cb990 1516 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
c410833a 1517 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
93f0822d
DB
1518 return -EINVAL;
1519
1520 return 0;
1521}
1522
bb18008f 1523static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
93f0822d 1524{
bb18008f
DB
1525 int cpu_num = policy->cpu;
1526 struct cpudata *cpu = all_cpu_data[cpu_num];
93f0822d 1527
4836df17 1528 pr_debug("CPU %d exiting\n", cpu_num);
bb18008f 1529
bb6ab52f 1530 intel_pstate_clear_update_util_hook(cpu_num);
a4675fbc 1531
2f86dc4c
DB
1532 if (hwp_active)
1533 return;
1534
fdfdb2b1 1535 intel_pstate_set_min_pstate(cpu);
93f0822d
DB
1536}
1537
2760984f 1538static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
93f0822d 1539{
93f0822d 1540 struct cpudata *cpu;
52e0a509 1541 int rc;
93f0822d
DB
1542
1543 rc = intel_pstate_init_cpu(policy->cpu);
1544 if (rc)
1545 return rc;
1546
1547 cpu = all_cpu_data[policy->cpu];
1548
51443fbf 1549 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
93f0822d
DB
1550 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1551 else
1552 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1553
b27580b0
DB
1554 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1555 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
93f0822d
DB
1556
1557 /* cpuinfo and default policy values */
b27580b0
DB
1558 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
1559 policy->cpuinfo.max_freq =
1560 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
9522a2ff 1561 intel_pstate_init_acpi_perf_limits(policy);
93f0822d
DB
1562 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1563 cpumask_set_cpu(policy->cpu, policy->cpus);
1564
1565 return 0;
1566}
1567
9522a2ff
SP
1568static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1569{
1570 intel_pstate_exit_perf_limits(policy);
1571
1572 return 0;
1573}
1574
93f0822d
DB
1575static struct cpufreq_driver intel_pstate_driver = {
1576 .flags = CPUFREQ_CONST_LOOPS,
1577 .verify = intel_pstate_verify_policy,
1578 .setpolicy = intel_pstate_set_policy,
ba41e1bc 1579 .resume = intel_pstate_hwp_set_policy,
93f0822d
DB
1580 .get = intel_pstate_get,
1581 .init = intel_pstate_cpu_init,
9522a2ff 1582 .exit = intel_pstate_cpu_exit,
bb18008f 1583 .stop_cpu = intel_pstate_stop_cpu,
93f0822d 1584 .name = "intel_pstate",
93f0822d
DB
1585};
1586
6be26498 1587static int __initdata no_load;
2f86dc4c 1588static int __initdata no_hwp;
d64c3b0b 1589static int __initdata hwp_only;
aa4ea34d 1590static unsigned int force_load;
6be26498 1591
b563b4e3
DB
1592static int intel_pstate_msrs_not_valid(void)
1593{
016c8150 1594 if (!pstate_funcs.get_max() ||
c410833a
SK
1595 !pstate_funcs.get_min() ||
1596 !pstate_funcs.get_turbo())
b563b4e3
DB
1597 return -ENODEV;
1598
b563b4e3
DB
1599 return 0;
1600}
016c8150 1601
e0a261a2 1602static void copy_pid_params(struct pstate_adjust_policy *policy)
016c8150
DB
1603{
1604 pid_params.sample_rate_ms = policy->sample_rate_ms;
a4675fbc 1605 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
016c8150
DB
1606 pid_params.p_gain_pct = policy->p_gain_pct;
1607 pid_params.i_gain_pct = policy->i_gain_pct;
1608 pid_params.d_gain_pct = policy->d_gain_pct;
1609 pid_params.deadband = policy->deadband;
1610 pid_params.setpoint = policy->setpoint;
1611}
1612
e0a261a2 1613static void copy_cpu_funcs(struct pstate_funcs *funcs)
016c8150
DB
1614{
1615 pstate_funcs.get_max = funcs->get_max;
3bcc6fa9 1616 pstate_funcs.get_max_physical = funcs->get_max_physical;
016c8150
DB
1617 pstate_funcs.get_min = funcs->get_min;
1618 pstate_funcs.get_turbo = funcs->get_turbo;
b27580b0 1619 pstate_funcs.get_scaling = funcs->get_scaling;
fdfdb2b1 1620 pstate_funcs.get_val = funcs->get_val;
007bea09 1621 pstate_funcs.get_vid = funcs->get_vid;
157386b6
PL
1622 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
1623
016c8150
DB
1624}
1625
9522a2ff 1626#ifdef CONFIG_ACPI
fbbcdc07
AH
1627
1628static bool intel_pstate_no_acpi_pss(void)
1629{
1630 int i;
1631
1632 for_each_possible_cpu(i) {
1633 acpi_status status;
1634 union acpi_object *pss;
1635 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1636 struct acpi_processor *pr = per_cpu(processors, i);
1637
1638 if (!pr)
1639 continue;
1640
1641 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
1642 if (ACPI_FAILURE(status))
1643 continue;
1644
1645 pss = buffer.pointer;
1646 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
1647 kfree(pss);
1648 return false;
1649 }
1650
1651 kfree(pss);
1652 }
1653
1654 return true;
1655}
1656
966916ea 1657static bool intel_pstate_has_acpi_ppc(void)
1658{
1659 int i;
1660
1661 for_each_possible_cpu(i) {
1662 struct acpi_processor *pr = per_cpu(processors, i);
1663
1664 if (!pr)
1665 continue;
1666 if (acpi_has_method(pr->handle, "_PPC"))
1667 return true;
1668 }
1669 return false;
1670}
1671
1672enum {
1673 PSS,
1674 PPC,
1675};
1676
fbbcdc07
AH
1677struct hw_vendor_info {
1678 u16 valid;
1679 char oem_id[ACPI_OEM_ID_SIZE];
1680 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
966916ea 1681 int oem_pwr_table;
fbbcdc07
AH
1682};
1683
1684/* Hardware vendor-specific info that has its own power management modes */
1685static struct hw_vendor_info vendor_info[] = {
966916ea 1686 {1, "HP ", "ProLiant", PSS},
1687 {1, "ORACLE", "X4-2 ", PPC},
1688 {1, "ORACLE", "X4-2L ", PPC},
1689 {1, "ORACLE", "X4-2B ", PPC},
1690 {1, "ORACLE", "X3-2 ", PPC},
1691 {1, "ORACLE", "X3-2L ", PPC},
1692 {1, "ORACLE", "X3-2B ", PPC},
1693 {1, "ORACLE", "X4470M2 ", PPC},
1694 {1, "ORACLE", "X4270M3 ", PPC},
1695 {1, "ORACLE", "X4270M2 ", PPC},
1696 {1, "ORACLE", "X4170M2 ", PPC},
5aecc3c8
EZ
1697 {1, "ORACLE", "X4170 M3", PPC},
1698 {1, "ORACLE", "X4275 M3", PPC},
1699 {1, "ORACLE", "X6-2 ", PPC},
1700 {1, "ORACLE", "Sudbury ", PPC},
fbbcdc07
AH
1701 {0, "", ""},
1702};
1703
1704static bool intel_pstate_platform_pwr_mgmt_exists(void)
1705{
1706 struct acpi_table_header hdr;
1707 struct hw_vendor_info *v_info;
2f86dc4c
DB
1708 const struct x86_cpu_id *id;
1709 u64 misc_pwr;
1710
1711 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
1712 if (id) {
1713 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
1714 if ( misc_pwr & (1 << 8))
1715 return true;
1716 }
fbbcdc07 1717
c410833a
SK
1718 if (acpi_disabled ||
1719 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
fbbcdc07
AH
1720 return false;
1721
1722 for (v_info = vendor_info; v_info->valid; v_info++) {
c410833a 1723 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
966916ea 1724 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
1725 ACPI_OEM_TABLE_ID_SIZE))
1726 switch (v_info->oem_pwr_table) {
1727 case PSS:
1728 return intel_pstate_no_acpi_pss();
1729 case PPC:
aa4ea34d
EZ
1730 return intel_pstate_has_acpi_ppc() &&
1731 (!force_load);
966916ea 1732 }
fbbcdc07
AH
1733 }
1734
1735 return false;
1736}
1737#else /* CONFIG_ACPI not enabled */
1738static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
966916ea 1739static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
fbbcdc07
AH
1740#endif /* CONFIG_ACPI */
1741
7791e4aa
SP
1742static const struct x86_cpu_id hwp_support_ids[] __initconst = {
1743 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
1744 {}
1745};
1746
93f0822d
DB
1747static int __init intel_pstate_init(void)
1748{
907cc908 1749 int cpu, rc = 0;
93f0822d 1750 const struct x86_cpu_id *id;
64df1fdf 1751 struct cpu_defaults *cpu_def;
93f0822d 1752
6be26498
DB
1753 if (no_load)
1754 return -ENODEV;
1755
7791e4aa
SP
1756 if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
1757 copy_cpu_funcs(&core_params.funcs);
1758 hwp_active++;
1759 goto hwp_cpu_matched;
1760 }
1761
93f0822d
DB
1762 id = x86_match_cpu(intel_pstate_cpu_ids);
1763 if (!id)
1764 return -ENODEV;
1765
64df1fdf 1766 cpu_def = (struct cpu_defaults *)id->driver_data;
016c8150 1767
64df1fdf
BP
1768 copy_pid_params(&cpu_def->pid_policy);
1769 copy_cpu_funcs(&cpu_def->funcs);
016c8150 1770
b563b4e3
DB
1771 if (intel_pstate_msrs_not_valid())
1772 return -ENODEV;
1773
7791e4aa
SP
1774hwp_cpu_matched:
1775 /*
1776 * The Intel pstate driver will be ignored if the platform
1777 * firmware has its own power management modes.
1778 */
1779 if (intel_pstate_platform_pwr_mgmt_exists())
1780 return -ENODEV;
1781
4836df17 1782 pr_info("Intel P-state driver initializing\n");
93f0822d 1783
b57ffac5 1784 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
93f0822d
DB
1785 if (!all_cpu_data)
1786 return -ENOMEM;
93f0822d 1787
d64c3b0b
KCA
1788 if (!hwp_active && hwp_only)
1789 goto out;
1790
93f0822d
DB
1791 rc = cpufreq_register_driver(&intel_pstate_driver);
1792 if (rc)
1793 goto out;
1794
1795 intel_pstate_debug_expose_params();
1796 intel_pstate_sysfs_expose_params();
b69880f9 1797
7791e4aa 1798 if (hwp_active)
4836df17 1799 pr_info("HWP enabled\n");
7791e4aa 1800
93f0822d
DB
1801 return rc;
1802out:
907cc908
DB
1803 get_online_cpus();
1804 for_each_online_cpu(cpu) {
1805 if (all_cpu_data[cpu]) {
bb6ab52f 1806 intel_pstate_clear_update_util_hook(cpu);
907cc908
DB
1807 kfree(all_cpu_data[cpu]);
1808 }
1809 }
1810
1811 put_online_cpus();
1812 vfree(all_cpu_data);
93f0822d
DB
1813 return -ENODEV;
1814}
1815device_initcall(intel_pstate_init);
1816
6be26498
DB
1817static int __init intel_pstate_setup(char *str)
1818{
1819 if (!str)
1820 return -EINVAL;
1821
1822 if (!strcmp(str, "disable"))
1823 no_load = 1;
539342f6 1824 if (!strcmp(str, "no_hwp")) {
4836df17 1825 pr_info("HWP disabled\n");
2f86dc4c 1826 no_hwp = 1;
539342f6 1827 }
aa4ea34d
EZ
1828 if (!strcmp(str, "force"))
1829 force_load = 1;
d64c3b0b
KCA
1830 if (!strcmp(str, "hwp_only"))
1831 hwp_only = 1;
9522a2ff
SP
1832
1833#ifdef CONFIG_ACPI
1834 if (!strcmp(str, "support_acpi_ppc"))
1835 acpi_ppc = true;
1836#endif
1837
6be26498
DB
1838 return 0;
1839}
1840early_param("intel_pstate", intel_pstate_setup);
1841
93f0822d
DB
1842MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1843MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1844MODULE_LICENSE("GPL");