aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/cpufreq/intel_pstate.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/cpufreq/intel_pstate.c')
-rw-r--r--drivers/cpufreq/intel_pstate.c390
1 files changed, 337 insertions, 53 deletions
diff --git a/drivers/cpufreq/intel_pstate.c b/drivers/cpufreq/intel_pstate.c
index aa33b92b3e3e..93a3c635ea27 100644
--- a/drivers/cpufreq/intel_pstate.c
+++ b/drivers/cpufreq/intel_pstate.c
@@ -34,6 +34,10 @@
34#include <asm/cpu_device_id.h> 34#include <asm/cpu_device_id.h>
35#include <asm/cpufeature.h> 35#include <asm/cpufeature.h>
36 36
37#if IS_ENABLED(CONFIG_ACPI)
38#include <acpi/processor.h>
39#endif
40
37#define BYT_RATIOS 0x66a 41#define BYT_RATIOS 0x66a
38#define BYT_VIDS 0x66b 42#define BYT_VIDS 0x66b
39#define BYT_TURBO_RATIOS 0x66c 43#define BYT_TURBO_RATIOS 0x66c
@@ -43,7 +47,6 @@
43#define int_tofp(X) ((int64_t)(X) << FRAC_BITS) 47#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
44#define fp_toint(X) ((X) >> FRAC_BITS) 48#define fp_toint(X) ((X) >> FRAC_BITS)
45 49
46
47static inline int32_t mul_fp(int32_t x, int32_t y) 50static inline int32_t mul_fp(int32_t x, int32_t y)
48{ 51{
49 return ((int64_t)x * (int64_t)y) >> FRAC_BITS; 52 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
@@ -78,6 +81,7 @@ struct pstate_data {
78 int current_pstate; 81 int current_pstate;
79 int min_pstate; 82 int min_pstate;
80 int max_pstate; 83 int max_pstate;
84 int max_pstate_physical;
81 int scaling; 85 int scaling;
82 int turbo_pstate; 86 int turbo_pstate;
83}; 87};
@@ -113,6 +117,9 @@ struct cpudata {
113 u64 prev_mperf; 117 u64 prev_mperf;
114 u64 prev_tsc; 118 u64 prev_tsc;
115 struct sample sample; 119 struct sample sample;
120#if IS_ENABLED(CONFIG_ACPI)
121 struct acpi_processor_performance acpi_perf_data;
122#endif
116}; 123};
117 124
118static struct cpudata **all_cpu_data; 125static struct cpudata **all_cpu_data;
@@ -127,6 +134,7 @@ struct pstate_adjust_policy {
127 134
128struct pstate_funcs { 135struct pstate_funcs {
129 int (*get_max)(void); 136 int (*get_max)(void);
137 int (*get_max_physical)(void);
130 int (*get_min)(void); 138 int (*get_min)(void);
131 int (*get_turbo)(void); 139 int (*get_turbo)(void);
132 int (*get_scaling)(void); 140 int (*get_scaling)(void);
@@ -142,6 +150,7 @@ struct cpu_defaults {
142static struct pstate_adjust_policy pid_params; 150static struct pstate_adjust_policy pid_params;
143static struct pstate_funcs pstate_funcs; 151static struct pstate_funcs pstate_funcs;
144static int hwp_active; 152static int hwp_active;
153static int no_acpi_perf;
145 154
146struct perf_limits { 155struct perf_limits {
147 int no_turbo; 156 int no_turbo;
@@ -154,9 +163,24 @@ struct perf_limits {
154 int max_sysfs_pct; 163 int max_sysfs_pct;
155 int min_policy_pct; 164 int min_policy_pct;
156 int min_sysfs_pct; 165 int min_sysfs_pct;
166 int max_perf_ctl;
167 int min_perf_ctl;
168};
169
170static struct perf_limits performance_limits = {
171 .no_turbo = 0,
172 .turbo_disabled = 0,
173 .max_perf_pct = 100,
174 .max_perf = int_tofp(1),
175 .min_perf_pct = 100,
176 .min_perf = int_tofp(1),
177 .max_policy_pct = 100,
178 .max_sysfs_pct = 100,
179 .min_policy_pct = 0,
180 .min_sysfs_pct = 0,
157}; 181};
158 182
159static struct perf_limits limits = { 183static struct perf_limits powersave_limits = {
160 .no_turbo = 0, 184 .no_turbo = 0,
161 .turbo_disabled = 0, 185 .turbo_disabled = 0,
162 .max_perf_pct = 100, 186 .max_perf_pct = 100,
@@ -167,8 +191,163 @@ static struct perf_limits limits = {
167 .max_sysfs_pct = 100, 191 .max_sysfs_pct = 100,
168 .min_policy_pct = 0, 192 .min_policy_pct = 0,
169 .min_sysfs_pct = 0, 193 .min_sysfs_pct = 0,
194 .max_perf_ctl = 0,
195 .min_perf_ctl = 0,
170}; 196};
171 197
198#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
199static struct perf_limits *limits = &performance_limits;
200#else
201static struct perf_limits *limits = &powersave_limits;
202#endif
203
204#if IS_ENABLED(CONFIG_ACPI)
205/*
206 * The max target pstate ratio is a 8 bit value in both PLATFORM_INFO MSR and
207 * in TURBO_RATIO_LIMIT MSR, which pstate driver stores in max_pstate and
208 * max_turbo_pstate fields. The PERF_CTL MSR contains 16 bit value for P state
209 * ratio, out of it only high 8 bits are used. For example 0x1700 is setting
210 * target ratio 0x17. The _PSS control value stores in a format which can be
211 * directly written to PERF_CTL MSR. But in intel_pstate driver this shift
212 * occurs during write to PERF_CTL (E.g. for cores core_set_pstate()).
213 * This function converts the _PSS control value to intel pstate driver format
214 * for comparison and assignment.
215 */
216static int convert_to_native_pstate_format(struct cpudata *cpu, int index)
217{
218 return cpu->acpi_perf_data.states[index].control >> 8;
219}
220
221static int intel_pstate_init_perf_limits(struct cpufreq_policy *policy)
222{
223 struct cpudata *cpu;
224 int ret;
225 bool turbo_absent = false;
226 int max_pstate_index;
227 int min_pss_ctl, max_pss_ctl, turbo_pss_ctl;
228 int i;
229
230 cpu = all_cpu_data[policy->cpu];
231
232 pr_debug("intel_pstate: default limits 0x%x 0x%x 0x%x\n",
233 cpu->pstate.min_pstate, cpu->pstate.max_pstate,
234 cpu->pstate.turbo_pstate);
235
236 if (!cpu->acpi_perf_data.shared_cpu_map &&
237 zalloc_cpumask_var_node(&cpu->acpi_perf_data.shared_cpu_map,
238 GFP_KERNEL, cpu_to_node(policy->cpu))) {
239 return -ENOMEM;
240 }
241
242 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
243 policy->cpu);
244 if (ret)
245 return ret;
246
247 /*
248 * Check if the control value in _PSS is for PERF_CTL MSR, which should
249 * guarantee that the states returned by it map to the states in our
250 * list directly.
251 */
252 if (cpu->acpi_perf_data.control_register.space_id !=
253 ACPI_ADR_SPACE_FIXED_HARDWARE)
254 return -EIO;
255
256 pr_debug("intel_pstate: CPU%u - ACPI _PSS perf data\n", policy->cpu);
257 for (i = 0; i < cpu->acpi_perf_data.state_count; i++)
258 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
259 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
260 (u32) cpu->acpi_perf_data.states[i].core_frequency,
261 (u32) cpu->acpi_perf_data.states[i].power,
262 (u32) cpu->acpi_perf_data.states[i].control);
263
264 /*
265 * If there is only one entry _PSS, simply ignore _PSS and continue as
266 * usual without taking _PSS into account
267 */
268 if (cpu->acpi_perf_data.state_count < 2)
269 return 0;
270
271 turbo_pss_ctl = convert_to_native_pstate_format(cpu, 0);
272 min_pss_ctl = convert_to_native_pstate_format(cpu,
273 cpu->acpi_perf_data.state_count - 1);
274 /* Check if there is a turbo freq in _PSS */
275 if (turbo_pss_ctl <= cpu->pstate.max_pstate &&
276 turbo_pss_ctl > cpu->pstate.min_pstate) {
277 pr_debug("intel_pstate: no turbo range exists in _PSS\n");
278 limits->no_turbo = limits->turbo_disabled = 1;
279 cpu->pstate.turbo_pstate = cpu->pstate.max_pstate;
280 turbo_absent = true;
281 }
282
283 /* Check if the max non turbo p state < Intel P state max */
284 max_pstate_index = turbo_absent ? 0 : 1;
285 max_pss_ctl = convert_to_native_pstate_format(cpu, max_pstate_index);
286 if (max_pss_ctl < cpu->pstate.max_pstate &&
287 max_pss_ctl > cpu->pstate.min_pstate)
288 cpu->pstate.max_pstate = max_pss_ctl;
289
290 /* check If min perf > Intel P State min */
291 if (min_pss_ctl > cpu->pstate.min_pstate &&
292 min_pss_ctl < cpu->pstate.max_pstate) {
293 cpu->pstate.min_pstate = min_pss_ctl;
294 policy->cpuinfo.min_freq = min_pss_ctl * cpu->pstate.scaling;
295 }
296
297 if (turbo_absent)
298 policy->cpuinfo.max_freq = cpu->pstate.max_pstate *
299 cpu->pstate.scaling;
300 else {
301 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate *
302 cpu->pstate.scaling;
303 /*
304 * The _PSS table doesn't contain whole turbo frequency range.
305 * This just contains +1 MHZ above the max non turbo frequency,
306 * with control value corresponding to max turbo ratio. But
307 * when cpufreq set policy is called, it will call with this
308 * max frequency, which will cause a reduced performance as
309 * this driver uses real max turbo frequency as the max
310 * frequeny. So correct this frequency in _PSS table to
311 * correct max turbo frequency based on the turbo ratio.
312 * Also need to convert to MHz as _PSS freq is in MHz.
313 */
314 cpu->acpi_perf_data.states[0].core_frequency =
315 turbo_pss_ctl * 100;
316 }
317
318 pr_debug("intel_pstate: Updated limits using _PSS 0x%x 0x%x 0x%x\n",
319 cpu->pstate.min_pstate, cpu->pstate.max_pstate,
320 cpu->pstate.turbo_pstate);
321 pr_debug("intel_pstate: policy max_freq=%d Khz min_freq = %d KHz\n",
322 policy->cpuinfo.max_freq, policy->cpuinfo.min_freq);
323
324 return 0;
325}
326
327static int intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
328{
329 struct cpudata *cpu;
330
331 if (!no_acpi_perf)
332 return 0;
333
334 cpu = all_cpu_data[policy->cpu];
335 acpi_processor_unregister_performance(policy->cpu);
336 return 0;
337}
338
339#else
340static int intel_pstate_init_perf_limits(struct cpufreq_policy *policy)
341{
342 return 0;
343}
344
345static int intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
346{
347 return 0;
348}
349#endif
350
172static inline void pid_reset(struct _pid *pid, int setpoint, int busy, 351static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
173 int deadband, int integral) { 352 int deadband, int integral) {
174 pid->setpoint = setpoint; 353 pid->setpoint = setpoint;
@@ -255,7 +434,7 @@ static inline void update_turbo_state(void)
255 434
256 cpu = all_cpu_data[0]; 435 cpu = all_cpu_data[0];
257 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en); 436 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
258 limits.turbo_disabled = 437 limits->turbo_disabled =
259 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE || 438 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
260 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate); 439 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
261} 440}
@@ -274,14 +453,14 @@ static void intel_pstate_hwp_set(void)
274 453
275 for_each_online_cpu(cpu) { 454 for_each_online_cpu(cpu) {
276 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value); 455 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
277 adj_range = limits.min_perf_pct * range / 100; 456 adj_range = limits->min_perf_pct * range / 100;
278 min = hw_min + adj_range; 457 min = hw_min + adj_range;
279 value &= ~HWP_MIN_PERF(~0L); 458 value &= ~HWP_MIN_PERF(~0L);
280 value |= HWP_MIN_PERF(min); 459 value |= HWP_MIN_PERF(min);
281 460
282 adj_range = limits.max_perf_pct * range / 100; 461 adj_range = limits->max_perf_pct * range / 100;
283 max = hw_min + adj_range; 462 max = hw_min + adj_range;
284 if (limits.no_turbo) { 463 if (limits->no_turbo) {
285 hw_max = HWP_GUARANTEED_PERF(cap); 464 hw_max = HWP_GUARANTEED_PERF(cap);
286 if (hw_max < max) 465 if (hw_max < max)
287 max = hw_max; 466 max = hw_max;
@@ -350,7 +529,7 @@ static void __init intel_pstate_debug_expose_params(void)
350 static ssize_t show_##file_name \ 529 static ssize_t show_##file_name \
351 (struct kobject *kobj, struct attribute *attr, char *buf) \ 530 (struct kobject *kobj, struct attribute *attr, char *buf) \
352 { \ 531 { \
353 return sprintf(buf, "%u\n", limits.object); \ 532 return sprintf(buf, "%u\n", limits->object); \
354 } 533 }
355 534
356static ssize_t show_turbo_pct(struct kobject *kobj, 535static ssize_t show_turbo_pct(struct kobject *kobj,
@@ -386,10 +565,10 @@ static ssize_t show_no_turbo(struct kobject *kobj,
386 ssize_t ret; 565 ssize_t ret;
387 566
388 update_turbo_state(); 567 update_turbo_state();
389 if (limits.turbo_disabled) 568 if (limits->turbo_disabled)
390 ret = sprintf(buf, "%u\n", limits.turbo_disabled); 569 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
391 else 570 else
392 ret = sprintf(buf, "%u\n", limits.no_turbo); 571 ret = sprintf(buf, "%u\n", limits->no_turbo);
393 572
394 return ret; 573 return ret;
395} 574}
@@ -405,12 +584,12 @@ static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
405 return -EINVAL; 584 return -EINVAL;
406 585
407 update_turbo_state(); 586 update_turbo_state();
408 if (limits.turbo_disabled) { 587 if (limits->turbo_disabled) {
409 pr_warn("intel_pstate: Turbo disabled by BIOS or unavailable on processor\n"); 588 pr_warn("intel_pstate: Turbo disabled by BIOS or unavailable on processor\n");
410 return -EPERM; 589 return -EPERM;
411 } 590 }
412 591
413 limits.no_turbo = clamp_t(int, input, 0, 1); 592 limits->no_turbo = clamp_t(int, input, 0, 1);
414 593
415 if (hwp_active) 594 if (hwp_active)
416 intel_pstate_hwp_set(); 595 intel_pstate_hwp_set();
@@ -428,11 +607,15 @@ static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
428 if (ret != 1) 607 if (ret != 1)
429 return -EINVAL; 608 return -EINVAL;
430 609
431 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100); 610 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
432 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct); 611 limits->max_perf_pct = min(limits->max_policy_pct,
433 limits.max_perf_pct = max(limits.min_policy_pct, limits.max_perf_pct); 612 limits->max_sysfs_pct);
434 limits.max_perf_pct = max(limits.min_perf_pct, limits.max_perf_pct); 613 limits->max_perf_pct = max(limits->min_policy_pct,
435 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100)); 614 limits->max_perf_pct);
615 limits->max_perf_pct = max(limits->min_perf_pct,
616 limits->max_perf_pct);
617 limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
618 int_tofp(100));
436 619
437 if (hwp_active) 620 if (hwp_active)
438 intel_pstate_hwp_set(); 621 intel_pstate_hwp_set();
@@ -449,11 +632,15 @@ static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
449 if (ret != 1) 632 if (ret != 1)
450 return -EINVAL; 633 return -EINVAL;
451 634
452 limits.min_sysfs_pct = clamp_t(int, input, 0 , 100); 635 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
453 limits.min_perf_pct = max(limits.min_policy_pct, limits.min_sysfs_pct); 636 limits->min_perf_pct = max(limits->min_policy_pct,
454 limits.min_perf_pct = min(limits.max_policy_pct, limits.min_perf_pct); 637 limits->min_sysfs_pct);
455 limits.min_perf_pct = min(limits.max_perf_pct, limits.min_perf_pct); 638 limits->min_perf_pct = min(limits->max_policy_pct,
456 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100)); 639 limits->min_perf_pct);
640 limits->min_perf_pct = min(limits->max_perf_pct,
641 limits->min_perf_pct);
642 limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
643 int_tofp(100));
457 644
458 if (hwp_active) 645 if (hwp_active)
459 intel_pstate_hwp_set(); 646 intel_pstate_hwp_set();
@@ -533,7 +720,7 @@ static void byt_set_pstate(struct cpudata *cpudata, int pstate)
533 u32 vid; 720 u32 vid;
534 721
535 val = (u64)pstate << 8; 722 val = (u64)pstate << 8;
536 if (limits.no_turbo && !limits.turbo_disabled) 723 if (limits->no_turbo && !limits->turbo_disabled)
537 val |= (u64)1 << 32; 724 val |= (u64)1 << 32;
538 725
539 vid_fp = cpudata->vid.min + mul_fp( 726 vid_fp = cpudata->vid.min + mul_fp(
@@ -591,7 +778,7 @@ static int core_get_min_pstate(void)
591 return (value >> 40) & 0xFF; 778 return (value >> 40) & 0xFF;
592} 779}
593 780
594static int core_get_max_pstate(void) 781static int core_get_max_pstate_physical(void)
595{ 782{
596 u64 value; 783 u64 value;
597 784
@@ -599,6 +786,46 @@ static int core_get_max_pstate(void)
599 return (value >> 8) & 0xFF; 786 return (value >> 8) & 0xFF;
600} 787}
601 788
789static int core_get_max_pstate(void)
790{
791 u64 tar;
792 u64 plat_info;
793 int max_pstate;
794 int err;
795
796 rdmsrl(MSR_PLATFORM_INFO, plat_info);
797 max_pstate = (plat_info >> 8) & 0xFF;
798
799 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
800 if (!err) {
801 /* Do some sanity checking for safety */
802 if (plat_info & 0x600000000) {
803 u64 tdp_ctrl;
804 u64 tdp_ratio;
805 int tdp_msr;
806
807 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
808 if (err)
809 goto skip_tar;
810
811 tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
812 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
813 if (err)
814 goto skip_tar;
815
816 if (tdp_ratio - 1 == tar) {
817 max_pstate = tar;
818 pr_debug("max_pstate=TAC %x\n", max_pstate);
819 } else {
820 goto skip_tar;
821 }
822 }
823 }
824
825skip_tar:
826 return max_pstate;
827}
828
602static int core_get_turbo_pstate(void) 829static int core_get_turbo_pstate(void)
603{ 830{
604 u64 value; 831 u64 value;
@@ -622,7 +849,7 @@ static void core_set_pstate(struct cpudata *cpudata, int pstate)
622 u64 val; 849 u64 val;
623 850
624 val = (u64)pstate << 8; 851 val = (u64)pstate << 8;
625 if (limits.no_turbo && !limits.turbo_disabled) 852 if (limits->no_turbo && !limits->turbo_disabled)
626 val |= (u64)1 << 32; 853 val |= (u64)1 << 32;
627 854
628 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val); 855 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
@@ -652,6 +879,7 @@ static struct cpu_defaults core_params = {
652 }, 879 },
653 .funcs = { 880 .funcs = {
654 .get_max = core_get_max_pstate, 881 .get_max = core_get_max_pstate,
882 .get_max_physical = core_get_max_pstate_physical,
655 .get_min = core_get_min_pstate, 883 .get_min = core_get_min_pstate,
656 .get_turbo = core_get_turbo_pstate, 884 .get_turbo = core_get_turbo_pstate,
657 .get_scaling = core_get_scaling, 885 .get_scaling = core_get_scaling,
@@ -670,6 +898,7 @@ static struct cpu_defaults byt_params = {
670 }, 898 },
671 .funcs = { 899 .funcs = {
672 .get_max = byt_get_max_pstate, 900 .get_max = byt_get_max_pstate,
901 .get_max_physical = byt_get_max_pstate,
673 .get_min = byt_get_min_pstate, 902 .get_min = byt_get_min_pstate,
674 .get_turbo = byt_get_turbo_pstate, 903 .get_turbo = byt_get_turbo_pstate,
675 .set = byt_set_pstate, 904 .set = byt_set_pstate,
@@ -689,6 +918,7 @@ static struct cpu_defaults knl_params = {
689 }, 918 },
690 .funcs = { 919 .funcs = {
691 .get_max = core_get_max_pstate, 920 .get_max = core_get_max_pstate,
921 .get_max_physical = core_get_max_pstate_physical,
692 .get_min = core_get_min_pstate, 922 .get_min = core_get_min_pstate,
693 .get_turbo = knl_get_turbo_pstate, 923 .get_turbo = knl_get_turbo_pstate,
694 .get_scaling = core_get_scaling, 924 .get_scaling = core_get_scaling,
@@ -702,7 +932,7 @@ static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
702 int max_perf_adj; 932 int max_perf_adj;
703 int min_perf; 933 int min_perf;
704 934
705 if (limits.no_turbo || limits.turbo_disabled) 935 if (limits->no_turbo || limits->turbo_disabled)
706 max_perf = cpu->pstate.max_pstate; 936 max_perf = cpu->pstate.max_pstate;
707 937
708 /* 938 /*
@@ -710,12 +940,23 @@ static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
710 * policy, or by cpu specific default values determined through 940 * policy, or by cpu specific default values determined through
711 * experimentation. 941 * experimentation.
712 */ 942 */
713 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf)); 943 if (limits->max_perf_ctl && limits->max_sysfs_pct >=
714 *max = clamp_t(int, max_perf_adj, 944 limits->max_policy_pct) {
715 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate); 945 *max = limits->max_perf_ctl;
946 } else {
947 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf),
948 limits->max_perf));
949 *max = clamp_t(int, max_perf_adj, cpu->pstate.min_pstate,
950 cpu->pstate.turbo_pstate);
951 }
716 952
717 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf)); 953 if (limits->min_perf_ctl) {
718 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf); 954 *min = limits->min_perf_ctl;
955 } else {
956 min_perf = fp_toint(mul_fp(int_tofp(max_perf),
957 limits->min_perf));
958 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
959 }
719} 960}
720 961
721static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate, bool force) 962static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate, bool force)
@@ -743,6 +984,7 @@ static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
743{ 984{
744 cpu->pstate.min_pstate = pstate_funcs.get_min(); 985 cpu->pstate.min_pstate = pstate_funcs.get_min();
745 cpu->pstate.max_pstate = pstate_funcs.get_max(); 986 cpu->pstate.max_pstate = pstate_funcs.get_max();
987 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
746 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(); 988 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
747 cpu->pstate.scaling = pstate_funcs.get_scaling(); 989 cpu->pstate.scaling = pstate_funcs.get_scaling();
748 990
@@ -761,7 +1003,8 @@ static inline void intel_pstate_calc_busy(struct cpudata *cpu)
761 1003
762 sample->freq = fp_toint( 1004 sample->freq = fp_toint(
763 mul_fp(int_tofp( 1005 mul_fp(int_tofp(
764 cpu->pstate.max_pstate * cpu->pstate.scaling / 100), 1006 cpu->pstate.max_pstate_physical *
1007 cpu->pstate.scaling / 100),
765 core_pct)); 1008 core_pct));
766 1009
767 sample->core_pct_busy = (int32_t)core_pct; 1010 sample->core_pct_busy = (int32_t)core_pct;
@@ -834,7 +1077,7 @@ static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
834 * specified pstate. 1077 * specified pstate.
835 */ 1078 */
836 core_busy = cpu->sample.core_pct_busy; 1079 core_busy = cpu->sample.core_pct_busy;
837 max_pstate = int_tofp(cpu->pstate.max_pstate); 1080 max_pstate = int_tofp(cpu->pstate.max_pstate_physical);
838 current_pstate = int_tofp(cpu->pstate.current_pstate); 1081 current_pstate = int_tofp(cpu->pstate.current_pstate);
839 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate)); 1082 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
840 1083
@@ -988,37 +1231,63 @@ static unsigned int intel_pstate_get(unsigned int cpu_num)
988 1231
989static int intel_pstate_set_policy(struct cpufreq_policy *policy) 1232static int intel_pstate_set_policy(struct cpufreq_policy *policy)
990{ 1233{
1234#if IS_ENABLED(CONFIG_ACPI)
1235 struct cpudata *cpu;
1236 int i;
1237#endif
1238 pr_debug("intel_pstate: %s max %u policy->max %u\n", __func__,
1239 policy->cpuinfo.max_freq, policy->max);
991 if (!policy->cpuinfo.max_freq) 1240 if (!policy->cpuinfo.max_freq)
992 return -ENODEV; 1241 return -ENODEV;
993 1242
994 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE && 1243 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE &&
995 policy->max >= policy->cpuinfo.max_freq) { 1244 policy->max >= policy->cpuinfo.max_freq) {
996 limits.min_policy_pct = 100; 1245 pr_debug("intel_pstate: set performance\n");
997 limits.min_perf_pct = 100; 1246 limits = &performance_limits;
998 limits.min_perf = int_tofp(1);
999 limits.max_policy_pct = 100;
1000 limits.max_perf_pct = 100;
1001 limits.max_perf = int_tofp(1);
1002 limits.no_turbo = 0;
1003 return 0; 1247 return 0;
1004 } 1248 }
1005 1249
1006 limits.min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq; 1250 pr_debug("intel_pstate: set powersave\n");
1007 limits.min_policy_pct = clamp_t(int, limits.min_policy_pct, 0 , 100); 1251 limits = &powersave_limits;
1008 limits.max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq; 1252 limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
1009 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100); 1253 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
1254 limits->max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
1255 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
1010 1256
1011 /* Normalize user input to [min_policy_pct, max_policy_pct] */ 1257 /* Normalize user input to [min_policy_pct, max_policy_pct] */
1012 limits.min_perf_pct = max(limits.min_policy_pct, limits.min_sysfs_pct); 1258 limits->min_perf_pct = max(limits->min_policy_pct,
1013 limits.min_perf_pct = min(limits.max_policy_pct, limits.min_perf_pct); 1259 limits->min_sysfs_pct);
1014 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct); 1260 limits->min_perf_pct = min(limits->max_policy_pct,
1015 limits.max_perf_pct = max(limits.min_policy_pct, limits.max_perf_pct); 1261 limits->min_perf_pct);
1262 limits->max_perf_pct = min(limits->max_policy_pct,
1263 limits->max_sysfs_pct);
1264 limits->max_perf_pct = max(limits->min_policy_pct,
1265 limits->max_perf_pct);
1016 1266
1017 /* Make sure min_perf_pct <= max_perf_pct */ 1267 /* Make sure min_perf_pct <= max_perf_pct */
1018 limits.min_perf_pct = min(limits.max_perf_pct, limits.min_perf_pct); 1268 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
1019 1269
1020 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100)); 1270 limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
1021 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100)); 1271 int_tofp(100));
1272 limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
1273 int_tofp(100));
1274
1275#if IS_ENABLED(CONFIG_ACPI)
1276 cpu = all_cpu_data[policy->cpu];
1277 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
1278 int control;
1279
1280 control = convert_to_native_pstate_format(cpu, i);
1281 if (control * cpu->pstate.scaling == policy->max)
1282 limits->max_perf_ctl = control;
1283 if (control * cpu->pstate.scaling == policy->min)
1284 limits->min_perf_ctl = control;
1285 }
1286
1287 pr_debug("intel_pstate: max %u policy_max %u perf_ctl [0x%x-0x%x]\n",
1288 policy->cpuinfo.max_freq, policy->max, limits->min_perf_ctl,
1289 limits->max_perf_ctl);
1290#endif
1022 1291
1023 if (hwp_active) 1292 if (hwp_active)
1024 intel_pstate_hwp_set(); 1293 intel_pstate_hwp_set();
@@ -1062,7 +1331,7 @@ static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
1062 1331
1063 cpu = all_cpu_data[policy->cpu]; 1332 cpu = all_cpu_data[policy->cpu];
1064 1333
1065 if (limits.min_perf_pct == 100 && limits.max_perf_pct == 100) 1334 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
1066 policy->policy = CPUFREQ_POLICY_PERFORMANCE; 1335 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1067 else 1336 else
1068 policy->policy = CPUFREQ_POLICY_POWERSAVE; 1337 policy->policy = CPUFREQ_POLICY_POWERSAVE;
@@ -1074,18 +1343,30 @@ static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
1074 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling; 1343 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
1075 policy->cpuinfo.max_freq = 1344 policy->cpuinfo.max_freq =
1076 cpu->pstate.turbo_pstate * cpu->pstate.scaling; 1345 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
1346 if (!no_acpi_perf)
1347 intel_pstate_init_perf_limits(policy);
1348 /*
1349 * If there is no acpi perf data or error, we ignore and use Intel P
1350 * state calculated limits, So this is not fatal error.
1351 */
1077 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL; 1352 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1078 cpumask_set_cpu(policy->cpu, policy->cpus); 1353 cpumask_set_cpu(policy->cpu, policy->cpus);
1079 1354
1080 return 0; 1355 return 0;
1081} 1356}
1082 1357
1358static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1359{
1360 return intel_pstate_exit_perf_limits(policy);
1361}
1362
1083static struct cpufreq_driver intel_pstate_driver = { 1363static struct cpufreq_driver intel_pstate_driver = {
1084 .flags = CPUFREQ_CONST_LOOPS, 1364 .flags = CPUFREQ_CONST_LOOPS,
1085 .verify = intel_pstate_verify_policy, 1365 .verify = intel_pstate_verify_policy,
1086 .setpolicy = intel_pstate_set_policy, 1366 .setpolicy = intel_pstate_set_policy,
1087 .get = intel_pstate_get, 1367 .get = intel_pstate_get,
1088 .init = intel_pstate_cpu_init, 1368 .init = intel_pstate_cpu_init,
1369 .exit = intel_pstate_cpu_exit,
1089 .stop_cpu = intel_pstate_stop_cpu, 1370 .stop_cpu = intel_pstate_stop_cpu,
1090 .name = "intel_pstate", 1371 .name = "intel_pstate",
1091}; 1372};
@@ -1118,6 +1399,7 @@ static void copy_pid_params(struct pstate_adjust_policy *policy)
1118static void copy_cpu_funcs(struct pstate_funcs *funcs) 1399static void copy_cpu_funcs(struct pstate_funcs *funcs)
1119{ 1400{
1120 pstate_funcs.get_max = funcs->get_max; 1401 pstate_funcs.get_max = funcs->get_max;
1402 pstate_funcs.get_max_physical = funcs->get_max_physical;
1121 pstate_funcs.get_min = funcs->get_min; 1403 pstate_funcs.get_min = funcs->get_min;
1122 pstate_funcs.get_turbo = funcs->get_turbo; 1404 pstate_funcs.get_turbo = funcs->get_turbo;
1123 pstate_funcs.get_scaling = funcs->get_scaling; 1405 pstate_funcs.get_scaling = funcs->get_scaling;
@@ -1126,7 +1408,6 @@ static void copy_cpu_funcs(struct pstate_funcs *funcs)
1126} 1408}
1127 1409
1128#if IS_ENABLED(CONFIG_ACPI) 1410#if IS_ENABLED(CONFIG_ACPI)
1129#include <acpi/processor.h>
1130 1411
1131static bool intel_pstate_no_acpi_pss(void) 1412static bool intel_pstate_no_acpi_pss(void)
1132{ 1413{
@@ -1318,6 +1599,9 @@ static int __init intel_pstate_setup(char *str)
1318 force_load = 1; 1599 force_load = 1;
1319 if (!strcmp(str, "hwp_only")) 1600 if (!strcmp(str, "hwp_only"))
1320 hwp_only = 1; 1601 hwp_only = 1;
1602 if (!strcmp(str, "no_acpi"))
1603 no_acpi_perf = 1;
1604
1321 return 0; 1605 return 0;
1322} 1606}
1323early_param("intel_pstate", intel_pstate_setup); 1607early_param("intel_pstate", intel_pstate_setup);