hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/cpufreq/intel_pstate.c
....@@ -1,13 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * intel_pstate.c: Native P state management for Intel processors
34 *
45 * (C) Copyright 2012 Intel Corporation
56 * 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.
117 */
128
139 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -28,6 +24,7 @@
2824 #include <linux/fs.h>
2925 #include <linux/acpi.h>
3026 #include <linux/vmalloc.h>
27
+#include <linux/pm_qos.h>
3128 #include <trace/events/power.h>
3229
3330 #include <asm/div64.h>
....@@ -39,6 +36,7 @@
3936 #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC)
4037
4138 #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
39
+#define INTEL_CPUFREQ_TRANSITION_DELAY_HWP 5000
4240 #define INTEL_CPUFREQ_TRANSITION_DELAY 500
4341
4442 #ifdef CONFIG_ACPI
....@@ -49,6 +47,8 @@
4947 #define FRAC_BITS 8
5048 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
5149 #define fp_toint(X) ((X) >> FRAC_BITS)
50
+
51
+#define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3))
5252
5353 #define EXT_BITS 6
5454 #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
....@@ -173,10 +173,11 @@
173173 /**
174174 * struct global_params - Global parameters, mostly tunable via sysfs.
175175 * @no_turbo: Whether or not to use turbo P-states.
176
- * @turbo_disabled: Whethet or not turbo P-states are available at all,
176
+ * @turbo_disabled: Whether or not turbo P-states are available at all,
177177 * based on the MSR_IA32_MISC_ENABLE value and whether or
178178 * not the maximum reported turbo P-state is different from
179179 * the maximum reported non-turbo one.
180
+ * @turbo_disabled_mf: The @turbo_disabled value reflected by cpuinfo.max_freq.
180181 * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo
181182 * P-state capacity.
182183 * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo
....@@ -185,6 +186,7 @@
185186 struct global_params {
186187 bool no_turbo;
187188 bool turbo_disabled;
189
+ bool turbo_disabled_mf;
188190 int max_perf_pct;
189191 int min_perf_pct;
190192 };
....@@ -200,9 +202,7 @@
200202 * @pstate: Stores P state limits for this CPU
201203 * @vid: Stores VID limits for this CPU
202204 * @last_sample_time: Last Sample time
203
- * @aperf_mperf_shift: Number of clock cycles after aperf, merf is incremented
204
- * This shift is a multiplier to mperf delta to
205
- * calculate CPU busy.
205
+ * @aperf_mperf_shift: APERF vs MPERF counting frequency difference
206206 * @prev_aperf: Last APERF value read from APERF MSR
207207 * @prev_mperf: Last MPERF value read from MPERF MSR
208208 * @prev_tsc: Last timestamp counter (TSC) value
....@@ -219,13 +219,13 @@
219219 * @epp_policy: Last saved policy used to set EPP/EPB
220220 * @epp_default: Power on default HWP energy performance
221221 * preference/bias
222
- * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
223
- * operation
222
+ * @epp_cached Cached HWP energy-performance preference value
224223 * @hwp_req_cached: Cached value of the last HWP Request MSR
225224 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
226225 * @last_io_update: Last time when IO wake flag was set
227226 * @sched_flags: Store scheduler flags for possible cross CPU update
228227 * @hwp_boost_min: Last HWP boosted min performance
228
+ * @suspended: Whether or not the driver has been suspended.
229229 *
230230 * This structure stores per CPU instance data for all CPUs.
231231 */
....@@ -257,12 +257,13 @@
257257 s16 epp_powersave;
258258 s16 epp_policy;
259259 s16 epp_default;
260
- s16 epp_saved;
260
+ s16 epp_cached;
261261 u64 hwp_req_cached;
262262 u64 hwp_cap_cached;
263263 u64 last_io_update;
264264 unsigned int sched_flags;
265265 u32 hwp_boost_min;
266
+ bool suspended;
266267 };
267268
268269 static struct cpudata **all_cpu_data;
....@@ -274,6 +275,7 @@
274275 * @get_min: Callback to get minimum P state
275276 * @get_turbo: Callback to get turbo P state
276277 * @get_scaling: Callback to get frequency scaling factor
278
+ * @get_aperf_mperf_shift: Callback to get the APERF vs MPERF frequency difference
277279 * @get_val: Callback to convert P state to actual MSR write value
278280 * @get_vid: Callback to get VID data for Atom platforms
279281 *
....@@ -373,11 +375,27 @@
373375 }
374376 }
375377 }
376
-#else
378
+
379
+static int intel_pstate_get_cppc_guranteed(int cpu)
380
+{
381
+ struct cppc_perf_caps cppc_perf;
382
+ int ret;
383
+
384
+ ret = cppc_get_perf_caps(cpu, &cppc_perf);
385
+ if (ret)
386
+ return ret;
387
+
388
+ if (cppc_perf.guaranteed_perf)
389
+ return cppc_perf.guaranteed_perf;
390
+
391
+ return cppc_perf.nominal_perf;
392
+}
393
+
394
+#else /* CONFIG_ACPI_CPPC_LIB */
377395 static void intel_pstate_set_itmt_prio(int cpu)
378396 {
379397 }
380
-#endif
398
+#endif /* CONFIG_ACPI_CPPC_LIB */
381399
382400 static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
383401 {
....@@ -425,20 +443,6 @@
425443 (u32) cpu->acpi_perf_data.states[i].control);
426444 }
427445
428
- /*
429
- * The _PSS table doesn't contain whole turbo frequency range.
430
- * This just contains +1 MHZ above the max non turbo frequency,
431
- * with control value corresponding to max turbo ratio. But
432
- * when cpufreq set policy is called, it will call with this
433
- * max frequency, which will cause a reduced performance as
434
- * this driver uses real max turbo frequency as the max
435
- * frequency. So correct this frequency in _PSS table to
436
- * correct max turbo frequency based on the turbo state.
437
- * Also need to convert to MHz as _PSS freq is in MHz.
438
- */
439
- if (!global.turbo_disabled)
440
- cpu->acpi_perf_data.states[0].core_frequency =
441
- policy->cpuinfo.max_freq / 1000;
442446 cpu->valid_pss_table = true;
443447 pr_debug("_PPC limits will be enforced\n");
444448
....@@ -459,7 +463,7 @@
459463
460464 acpi_processor_unregister_performance(policy->cpu);
461465 }
462
-#else
466
+#else /* CONFIG_ACPI */
463467 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
464468 {
465469 }
....@@ -472,7 +476,14 @@
472476 {
473477 return false;
474478 }
475
-#endif
479
+#endif /* CONFIG_ACPI */
480
+
481
+#ifndef CONFIG_ACPI_CPPC_LIB
482
+static int intel_pstate_get_cppc_guranteed(int cpu)
483
+{
484
+ return -ENOTSUPP;
485
+}
486
+#endif /* CONFIG_ACPI_CPPC_LIB */
476487
477488 static inline void update_turbo_state(void)
478489 {
....@@ -500,7 +511,7 @@
500511 u64 epb;
501512 int ret;
502513
503
- if (!static_cpu_has(X86_FEATURE_EPB))
514
+ if (!boot_cpu_has(X86_FEATURE_EPB))
504515 return -ENXIO;
505516
506517 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
....@@ -514,7 +525,7 @@
514525 {
515526 s16 epp;
516527
517
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
528
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
518529 /*
519530 * When hwp_req_data is 0, means that caller didn't read
520531 * MSR_HWP_REQUEST, so need to read and get EPP.
....@@ -539,7 +550,7 @@
539550 u64 epb;
540551 int ret;
541552
542
- if (!static_cpu_has(X86_FEATURE_EPB))
553
+ if (!boot_cpu_has(X86_FEATURE_EPB))
543554 return -ENXIO;
544555
545556 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
....@@ -578,25 +589,28 @@
578589 HWP_EPP_POWERSAVE
579590 };
580591
581
-static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
592
+static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
582593 {
583594 s16 epp;
584595 int index = -EINVAL;
585596
597
+ *raw_epp = 0;
586598 epp = intel_pstate_get_epp(cpu_data, 0);
587599 if (epp < 0)
588600 return epp;
589601
590
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
602
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
591603 if (epp == HWP_EPP_PERFORMANCE)
592604 return 1;
593
- if (epp <= HWP_EPP_BALANCE_PERFORMANCE)
605
+ if (epp == HWP_EPP_BALANCE_PERFORMANCE)
594606 return 2;
595
- if (epp <= HWP_EPP_BALANCE_POWERSAVE)
607
+ if (epp == HWP_EPP_BALANCE_POWERSAVE)
596608 return 3;
597
- else
609
+ if (epp == HWP_EPP_POWERSAVE)
598610 return 4;
599
- } else if (static_cpu_has(X86_FEATURE_EPB)) {
611
+ *raw_epp = epp;
612
+ return 0;
613
+ } else if (boot_cpu_has(X86_FEATURE_EPB)) {
600614 /*
601615 * Range:
602616 * 0x00-0x03 : Performance
....@@ -613,8 +627,35 @@
613627 return index;
614628 }
615629
630
+static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp)
631
+{
632
+ int ret;
633
+
634
+ /*
635
+ * Use the cached HWP Request MSR value, because in the active mode the
636
+ * register itself may be updated by intel_pstate_hwp_boost_up() or
637
+ * intel_pstate_hwp_boost_down() at any time.
638
+ */
639
+ u64 value = READ_ONCE(cpu->hwp_req_cached);
640
+
641
+ value &= ~GENMASK_ULL(31, 24);
642
+ value |= (u64)epp << 24;
643
+ /*
644
+ * The only other updater of hwp_req_cached in the active mode,
645
+ * intel_pstate_hwp_set(), is called under the same lock as this
646
+ * function, so it cannot run in parallel with the update below.
647
+ */
648
+ WRITE_ONCE(cpu->hwp_req_cached, value);
649
+ ret = wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
650
+ if (!ret)
651
+ cpu->epp_cached = epp;
652
+
653
+ return ret;
654
+}
655
+
616656 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
617
- int pref_index)
657
+ int pref_index, bool use_raw,
658
+ u32 raw_epp)
618659 {
619660 int epp = -EINVAL;
620661 int ret;
....@@ -622,29 +663,26 @@
622663 if (!pref_index)
623664 epp = cpu_data->epp_default;
624665
625
- mutex_lock(&intel_pstate_limits_lock);
626
-
627
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
628
- u64 value;
629
-
630
- ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
631
- if (ret)
632
- goto return_pref;
633
-
634
- value &= ~GENMASK_ULL(31, 24);
635
-
636
- if (epp == -EINVAL)
666
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
667
+ if (use_raw)
668
+ epp = raw_epp;
669
+ else if (epp == -EINVAL)
637670 epp = epp_values[pref_index - 1];
638671
639
- value |= (u64)epp << 24;
640
- ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
672
+ /*
673
+ * To avoid confusion, refuse to set EPP to any values different
674
+ * from 0 (performance) if the current policy is "performance",
675
+ * because those values would be overridden.
676
+ */
677
+ if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
678
+ return -EBUSY;
679
+
680
+ ret = intel_pstate_set_epp(cpu_data, epp);
641681 } else {
642682 if (epp == -EINVAL)
643683 epp = (pref_index - 1) << 2;
644684 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
645685 }
646
-return_pref:
647
- mutex_unlock(&intel_pstate_limits_lock);
648686
649687 return ret;
650688 }
....@@ -665,53 +703,127 @@
665703
666704 cpufreq_freq_attr_ro(energy_performance_available_preferences);
667705
706
+static struct cpufreq_driver intel_pstate;
707
+
668708 static ssize_t store_energy_performance_preference(
669709 struct cpufreq_policy *policy, const char *buf, size_t count)
670710 {
671
- struct cpudata *cpu_data = all_cpu_data[policy->cpu];
711
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
672712 char str_preference[21];
673
- int ret;
713
+ bool raw = false;
714
+ ssize_t ret;
715
+ u32 epp = 0;
674716
675717 ret = sscanf(buf, "%20s", str_preference);
676718 if (ret != 1)
677719 return -EINVAL;
678720
679721 ret = match_string(energy_perf_strings, -1, str_preference);
680
- if (ret < 0)
681
- return ret;
722
+ if (ret < 0) {
723
+ if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
724
+ return ret;
682725
683
- intel_pstate_set_energy_pref_index(cpu_data, ret);
684
- return count;
726
+ ret = kstrtouint(buf, 10, &epp);
727
+ if (ret)
728
+ return ret;
729
+
730
+ if (epp > 255)
731
+ return -EINVAL;
732
+
733
+ raw = true;
734
+ }
735
+
736
+ /*
737
+ * This function runs with the policy R/W semaphore held, which
738
+ * guarantees that the driver pointer will not change while it is
739
+ * running.
740
+ */
741
+ if (!intel_pstate_driver)
742
+ return -EAGAIN;
743
+
744
+ mutex_lock(&intel_pstate_limits_lock);
745
+
746
+ if (intel_pstate_driver == &intel_pstate) {
747
+ ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp);
748
+ } else {
749
+ /*
750
+ * In the passive mode the governor needs to be stopped on the
751
+ * target CPU before the EPP update and restarted after it,
752
+ * which is super-heavy-weight, so make sure it is worth doing
753
+ * upfront.
754
+ */
755
+ if (!raw)
756
+ epp = ret ? epp_values[ret - 1] : cpu->epp_default;
757
+
758
+ if (cpu->epp_cached != epp) {
759
+ int err;
760
+
761
+ cpufreq_stop_governor(policy);
762
+ ret = intel_pstate_set_epp(cpu, epp);
763
+ err = cpufreq_start_governor(policy);
764
+ if (!ret)
765
+ ret = err;
766
+ } else {
767
+ ret = 0;
768
+ }
769
+ }
770
+
771
+ mutex_unlock(&intel_pstate_limits_lock);
772
+
773
+ return ret ?: count;
685774 }
686775
687776 static ssize_t show_energy_performance_preference(
688777 struct cpufreq_policy *policy, char *buf)
689778 {
690779 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
691
- int preference;
780
+ int preference, raw_epp;
692781
693
- preference = intel_pstate_get_energy_pref_index(cpu_data);
782
+ preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
694783 if (preference < 0)
695784 return preference;
696785
697
- return sprintf(buf, "%s\n", energy_perf_strings[preference]);
786
+ if (raw_epp)
787
+ return sprintf(buf, "%d\n", raw_epp);
788
+ else
789
+ return sprintf(buf, "%s\n", energy_perf_strings[preference]);
698790 }
699791
700792 cpufreq_freq_attr_rw(energy_performance_preference);
701793
794
+static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
795
+{
796
+ struct cpudata *cpu;
797
+ u64 cap;
798
+ int ratio;
799
+
800
+ ratio = intel_pstate_get_cppc_guranteed(policy->cpu);
801
+ if (ratio <= 0) {
802
+ rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
803
+ ratio = HWP_GUARANTEED_PERF(cap);
804
+ }
805
+
806
+ cpu = all_cpu_data[policy->cpu];
807
+
808
+ return sprintf(buf, "%d\n", ratio * cpu->pstate.scaling);
809
+}
810
+
811
+cpufreq_freq_attr_ro(base_frequency);
812
+
702813 static struct freq_attr *hwp_cpufreq_attrs[] = {
703814 &energy_performance_preference,
704815 &energy_performance_available_preferences,
816
+ &base_frequency,
705817 NULL,
706818 };
707819
708
-static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
820
+static void intel_pstate_get_hwp_max(struct cpudata *cpu, int *phy_max,
709821 int *current_max)
710822 {
711823 u64 cap;
712824
713
- rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
714
- WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap);
825
+ rdmsrl_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap);
826
+ WRITE_ONCE(cpu->hwp_cap_cached, cap);
715827 if (global.no_turbo || global.turbo_disabled)
716828 *current_max = HWP_GUARANTEED_PERF(cap);
717829 else
....@@ -746,12 +858,6 @@
746858
747859 cpu_data->epp_policy = cpu_data->policy;
748860
749
- if (cpu_data->epp_saved >= 0) {
750
- epp = cpu_data->epp_saved;
751
- cpu_data->epp_saved = -EINVAL;
752
- goto update_epp;
753
- }
754
-
755861 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
756862 epp = intel_pstate_get_epp(cpu_data, value);
757863 cpu_data->epp_powersave = epp;
....@@ -778,8 +884,7 @@
778884
779885 epp = cpu_data->epp_powersave;
780886 }
781
-update_epp:
782
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
887
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
783888 value &= ~GENMASK_ULL(31, 24);
784889 value |= (u64)epp << 24;
785890 } else {
....@@ -790,34 +895,99 @@
790895 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
791896 }
792897
793
-static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
898
+static void intel_pstate_hwp_offline(struct cpudata *cpu)
794899 {
795
- struct cpudata *cpu_data = all_cpu_data[policy->cpu];
900
+ u64 value = READ_ONCE(cpu->hwp_req_cached);
901
+ int min_perf;
796902
797
- if (!hwp_active)
798
- return 0;
903
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
904
+ /*
905
+ * In case the EPP has been set to "performance" by the
906
+ * active mode "performance" scaling algorithm, replace that
907
+ * temporary value with the cached EPP one.
908
+ */
909
+ value &= ~GENMASK_ULL(31, 24);
910
+ value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached);
911
+ WRITE_ONCE(cpu->hwp_req_cached, value);
912
+ }
799913
800
- cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
914
+ value &= ~GENMASK_ULL(31, 0);
915
+ min_perf = HWP_LOWEST_PERF(cpu->hwp_cap_cached);
801916
802
- return 0;
917
+ /* Set hwp_max = hwp_min */
918
+ value |= HWP_MAX_PERF(min_perf);
919
+ value |= HWP_MIN_PERF(min_perf);
920
+
921
+ /* Set EPP to min */
922
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP))
923
+ value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
924
+
925
+ wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
926
+}
927
+
928
+#define POWER_CTL_EE_ENABLE 1
929
+#define POWER_CTL_EE_DISABLE 2
930
+
931
+static int power_ctl_ee_state;
932
+
933
+static void set_power_ctl_ee_state(bool input)
934
+{
935
+ u64 power_ctl;
936
+
937
+ mutex_lock(&intel_pstate_driver_lock);
938
+ rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
939
+ if (input) {
940
+ power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
941
+ power_ctl_ee_state = POWER_CTL_EE_ENABLE;
942
+ } else {
943
+ power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
944
+ power_ctl_ee_state = POWER_CTL_EE_DISABLE;
945
+ }
946
+ wrmsrl(MSR_IA32_POWER_CTL, power_ctl);
947
+ mutex_unlock(&intel_pstate_driver_lock);
803948 }
804949
805950 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
806951
952
+static void intel_pstate_hwp_reenable(struct cpudata *cpu)
953
+{
954
+ intel_pstate_hwp_enable(cpu);
955
+ wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached));
956
+}
957
+
958
+static int intel_pstate_suspend(struct cpufreq_policy *policy)
959
+{
960
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
961
+
962
+ pr_debug("CPU %d suspending\n", cpu->cpu);
963
+
964
+ cpu->suspended = true;
965
+
966
+ return 0;
967
+}
968
+
807969 static int intel_pstate_resume(struct cpufreq_policy *policy)
808970 {
809
- if (!hwp_active)
810
- return 0;
971
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
811972
812
- mutex_lock(&intel_pstate_limits_lock);
973
+ pr_debug("CPU %d resuming\n", cpu->cpu);
813974
814
- if (policy->cpu == 0)
815
- intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
975
+ /* Only restore if the system default is changed */
976
+ if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
977
+ set_power_ctl_ee_state(true);
978
+ else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
979
+ set_power_ctl_ee_state(false);
816980
817
- all_cpu_data[policy->cpu]->epp_policy = 0;
818
- intel_pstate_hwp_set(policy->cpu);
981
+ if (cpu->suspended && hwp_active) {
982
+ mutex_lock(&intel_pstate_limits_lock);
819983
820
- mutex_unlock(&intel_pstate_limits_lock);
984
+ /* Re-enable HWP, because "online" has not done that. */
985
+ intel_pstate_hwp_reenable(cpu);
986
+
987
+ mutex_unlock(&intel_pstate_limits_lock);
988
+ }
989
+
990
+ cpu->suspended = false;
821991
822992 return 0;
823993 }
....@@ -828,6 +998,44 @@
828998
829999 for_each_possible_cpu(cpu)
8301000 cpufreq_update_policy(cpu);
1001
+}
1002
+
1003
+static void intel_pstate_update_max_freq(unsigned int cpu)
1004
+{
1005
+ struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
1006
+ struct cpudata *cpudata;
1007
+
1008
+ if (!policy)
1009
+ return;
1010
+
1011
+ cpudata = all_cpu_data[cpu];
1012
+ policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
1013
+ cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
1014
+
1015
+ refresh_frequency_limits(policy);
1016
+
1017
+ cpufreq_cpu_release(policy);
1018
+}
1019
+
1020
+static void intel_pstate_update_limits(unsigned int cpu)
1021
+{
1022
+ mutex_lock(&intel_pstate_driver_lock);
1023
+
1024
+ update_turbo_state();
1025
+ /*
1026
+ * If turbo has been turned on or off globally, policy limits for
1027
+ * all CPUs need to be updated to reflect that.
1028
+ */
1029
+ if (global.turbo_disabled_mf != global.turbo_disabled) {
1030
+ global.turbo_disabled_mf = global.turbo_disabled;
1031
+ arch_set_max_freq_ratio(global.turbo_disabled);
1032
+ for_each_possible_cpu(cpu)
1033
+ intel_pstate_update_max_freq(cpu);
1034
+ } else {
1035
+ cpufreq_update_policy(cpu);
1036
+ }
1037
+
1038
+ mutex_unlock(&intel_pstate_driver_lock);
8311039 }
8321040
8331041 /************************** sysfs begin ************************/
....@@ -983,6 +1191,45 @@
9831191 return count;
9841192 }
9851193
1194
+static void update_qos_request(enum freq_qos_req_type type)
1195
+{
1196
+ int max_state, turbo_max, freq, i, perf_pct;
1197
+ struct freq_qos_request *req;
1198
+ struct cpufreq_policy *policy;
1199
+
1200
+ for_each_possible_cpu(i) {
1201
+ struct cpudata *cpu = all_cpu_data[i];
1202
+
1203
+ policy = cpufreq_cpu_get(i);
1204
+ if (!policy)
1205
+ continue;
1206
+
1207
+ req = policy->driver_data;
1208
+ cpufreq_cpu_put(policy);
1209
+
1210
+ if (!req)
1211
+ continue;
1212
+
1213
+ if (hwp_active)
1214
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
1215
+ else
1216
+ turbo_max = cpu->pstate.turbo_pstate;
1217
+
1218
+ if (type == FREQ_QOS_MIN) {
1219
+ perf_pct = global.min_perf_pct;
1220
+ } else {
1221
+ req++;
1222
+ perf_pct = global.max_perf_pct;
1223
+ }
1224
+
1225
+ freq = DIV_ROUND_UP(turbo_max * perf_pct, 100);
1226
+ freq *= cpu->pstate.scaling;
1227
+
1228
+ if (freq_qos_update_request(req, freq) < 0)
1229
+ pr_warn("Failed to update freq constraint: CPU%d\n", i);
1230
+ }
1231
+}
1232
+
9861233 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
9871234 const char *buf, size_t count)
9881235 {
....@@ -1006,7 +1253,10 @@
10061253
10071254 mutex_unlock(&intel_pstate_limits_lock);
10081255
1009
- intel_pstate_update_policies();
1256
+ if (intel_pstate_driver == &intel_pstate)
1257
+ intel_pstate_update_policies();
1258
+ else
1259
+ update_qos_request(FREQ_QOS_MAX);
10101260
10111261 mutex_unlock(&intel_pstate_driver_lock);
10121262
....@@ -1037,7 +1287,10 @@
10371287
10381288 mutex_unlock(&intel_pstate_limits_lock);
10391289
1040
- intel_pstate_update_policies();
1290
+ if (intel_pstate_driver == &intel_pstate)
1291
+ intel_pstate_update_policies();
1292
+ else
1293
+ update_qos_request(FREQ_QOS_MIN);
10411294
10421295 mutex_unlock(&intel_pstate_driver_lock);
10431296
....@@ -1069,6 +1322,32 @@
10691322 return count;
10701323 }
10711324
1325
+static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
1326
+ char *buf)
1327
+{
1328
+ u64 power_ctl;
1329
+ int enable;
1330
+
1331
+ rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
1332
+ enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
1333
+ return sprintf(buf, "%d\n", !enable);
1334
+}
1335
+
1336
+static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
1337
+ const char *buf, size_t count)
1338
+{
1339
+ bool input;
1340
+ int ret;
1341
+
1342
+ ret = kstrtobool(buf, &input);
1343
+ if (ret)
1344
+ return ret;
1345
+
1346
+ set_power_ctl_ee_state(input);
1347
+
1348
+ return count;
1349
+}
1350
+
10721351 show_one(max_perf_pct, max_perf_pct);
10731352 show_one(min_perf_pct, min_perf_pct);
10741353
....@@ -1079,6 +1358,7 @@
10791358 define_one_global_ro(turbo_pct);
10801359 define_one_global_ro(num_pstates);
10811360 define_one_global_rw(hwp_dynamic_boost);
1361
+define_one_global_rw(energy_efficiency);
10821362
10831363 static struct attribute *intel_pstate_attributes[] = {
10841364 &status.attr,
....@@ -1092,9 +1372,12 @@
10921372 .attrs = intel_pstate_attributes,
10931373 };
10941374
1375
+static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
1376
+
1377
+static struct kobject *intel_pstate_kobject;
1378
+
10951379 static void __init intel_pstate_sysfs_expose_params(void)
10961380 {
1097
- struct kobject *intel_pstate_kobject;
10981381 int rc;
10991382
11001383 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
....@@ -1119,43 +1402,60 @@
11191402 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
11201403 WARN_ON(rc);
11211404
1122
- if (hwp_active) {
1123
- rc = sysfs_create_file(intel_pstate_kobject,
1124
- &hwp_dynamic_boost.attr);
1405
+ if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
1406
+ rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
11251407 WARN_ON(rc);
11261408 }
11271409 }
1410
+
1411
+static void __init intel_pstate_sysfs_remove(void)
1412
+{
1413
+ if (!intel_pstate_kobject)
1414
+ return;
1415
+
1416
+ sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group);
1417
+
1418
+ if (!per_cpu_limits) {
1419
+ sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr);
1420
+ sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr);
1421
+
1422
+ if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids))
1423
+ sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr);
1424
+ }
1425
+
1426
+ kobject_put(intel_pstate_kobject);
1427
+}
1428
+
1429
+static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void)
1430
+{
1431
+ int rc;
1432
+
1433
+ if (!hwp_active)
1434
+ return;
1435
+
1436
+ rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1437
+ WARN_ON_ONCE(rc);
1438
+}
1439
+
1440
+static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void)
1441
+{
1442
+ if (!hwp_active)
1443
+ return;
1444
+
1445
+ sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1446
+}
1447
+
11281448 /************************** sysfs end ************************/
11291449
11301450 static void intel_pstate_hwp_enable(struct cpudata *cpudata)
11311451 {
11321452 /* First disable HWP notification interrupt as we don't process them */
1133
- if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1453
+ if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
11341454 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
11351455
11361456 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
1137
- cpudata->epp_policy = 0;
11381457 if (cpudata->epp_default == -EINVAL)
11391458 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
1140
-}
1141
-
1142
-#define MSR_IA32_POWER_CTL_BIT_EE 19
1143
-
1144
-/* Disable energy efficiency optimization */
1145
-static void intel_pstate_disable_ee(int cpu)
1146
-{
1147
- u64 power_ctl;
1148
- int ret;
1149
-
1150
- ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1151
- if (ret)
1152
- return;
1153
-
1154
- if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1155
- pr_info("Disabling energy efficiency optimization\n");
1156
- power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1157
- wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1158
- }
11591459 }
11601460
11611461 static int atom_get_min_pstate(void)
....@@ -1383,12 +1683,6 @@
13831683 return ret;
13841684 }
13851685
1386
-static int intel_pstate_get_base_pstate(struct cpudata *cpu)
1387
-{
1388
- return global.no_turbo || global.turbo_disabled ?
1389
- cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1390
-}
1391
-
13921686 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
13931687 {
13941688 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
....@@ -1409,11 +1703,9 @@
14091703
14101704 static void intel_pstate_max_within_limits(struct cpudata *cpu)
14111705 {
1412
- int pstate;
1706
+ int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
14131707
14141708 update_turbo_state();
1415
- pstate = intel_pstate_get_base_pstate(cpu);
1416
- pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
14171709 intel_pstate_set_pstate(cpu, pstate);
14181710 }
14191711
....@@ -1427,7 +1719,7 @@
14271719 if (hwp_active && !hwp_mode_bdw) {
14281720 unsigned int phy_max, current_max;
14291721
1430
- intel_pstate_get_hwp_max(cpu->cpu, &phy_max, &current_max);
1722
+ intel_pstate_get_hwp_max(cpu, &phy_max, &current_max);
14311723 cpu->pstate.turbo_freq = phy_max * cpu->pstate.scaling;
14321724 cpu->pstate.turbo_pstate = phy_max;
14331725 cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(READ_ONCE(cpu->hwp_cap_cached));
....@@ -1619,17 +1911,14 @@
16191911 static inline int32_t get_target_pstate(struct cpudata *cpu)
16201912 {
16211913 struct sample *sample = &cpu->sample;
1622
- int32_t busy_frac, boost;
1914
+ int32_t busy_frac;
16231915 int target, avg_pstate;
16241916
16251917 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
16261918 sample->tsc);
16271919
1628
- boost = cpu->iowait_boost;
1629
- cpu->iowait_boost >>= 1;
1630
-
1631
- if (busy_frac < boost)
1632
- busy_frac = boost;
1920
+ if (busy_frac < cpu->iowait_boost)
1921
+ busy_frac = cpu->iowait_boost;
16331922
16341923 sample->busy_scaled = busy_frac * 100;
16351924
....@@ -1656,11 +1945,9 @@
16561945
16571946 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
16581947 {
1659
- int max_pstate = intel_pstate_get_base_pstate(cpu);
1660
- int min_pstate;
1948
+ int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1949
+ int max_pstate = max(min_pstate, cpu->max_perf_ratio);
16611950
1662
- min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1663
- max_pstate = max(min_pstate, cpu->max_perf_ratio);
16641951 return clamp_t(int, pstate, min_pstate, max_pstate);
16651952 }
16661953
....@@ -1708,29 +1995,30 @@
17081995 if (smp_processor_id() != cpu->cpu)
17091996 return;
17101997
1998
+ delta_ns = time - cpu->last_update;
17111999 if (flags & SCHED_CPUFREQ_IOWAIT) {
1712
- cpu->iowait_boost = int_tofp(1);
1713
- cpu->last_update = time;
1714
- /*
1715
- * The last time the busy was 100% so P-state was max anyway
1716
- * so avoid overhead of computation.
1717
- */
1718
- if (fp_toint(cpu->sample.busy_scaled) == 100)
1719
- return;
1720
-
1721
- goto set_pstate;
2000
+ /* Start over if the CPU may have been idle. */
2001
+ if (delta_ns > TICK_NSEC) {
2002
+ cpu->iowait_boost = ONE_EIGHTH_FP;
2003
+ } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
2004
+ cpu->iowait_boost <<= 1;
2005
+ if (cpu->iowait_boost > int_tofp(1))
2006
+ cpu->iowait_boost = int_tofp(1);
2007
+ } else {
2008
+ cpu->iowait_boost = ONE_EIGHTH_FP;
2009
+ }
17222010 } else if (cpu->iowait_boost) {
17232011 /* Clear iowait_boost if the CPU may have been idle. */
1724
- delta_ns = time - cpu->last_update;
17252012 if (delta_ns > TICK_NSEC)
17262013 cpu->iowait_boost = 0;
2014
+ else
2015
+ cpu->iowait_boost >>= 1;
17272016 }
17282017 cpu->last_update = time;
17292018 delta_ns = time - cpu->sample.time;
17302019 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
17312020 return;
17322021
1733
-set_pstate:
17342022 if (intel_pstate_sample(cpu, time))
17352023 intel_pstate_adjust_pstate(cpu);
17362024 }
....@@ -1774,51 +2062,51 @@
17742062 .get_val = core_get_val,
17752063 };
17762064
1777
-#define ICPU(model, policy) \
1778
- { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1779
- (unsigned long)&policy }
2065
+#define X86_MATCH(model, policy) \
2066
+ X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
2067
+ X86_FEATURE_APERFMPERF, &policy)
17802068
17812069 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
1782
- ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs),
1783
- ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs),
1784
- ICPU(INTEL_FAM6_ATOM_SILVERMONT, silvermont_funcs),
1785
- ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs),
1786
- ICPU(INTEL_FAM6_HASWELL_CORE, core_funcs),
1787
- ICPU(INTEL_FAM6_BROADWELL_CORE, core_funcs),
1788
- ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs),
1789
- ICPU(INTEL_FAM6_HASWELL_X, core_funcs),
1790
- ICPU(INTEL_FAM6_HASWELL_ULT, core_funcs),
1791
- ICPU(INTEL_FAM6_HASWELL_GT3E, core_funcs),
1792
- ICPU(INTEL_FAM6_BROADWELL_GT3E, core_funcs),
1793
- ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs),
1794
- ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_funcs),
1795
- ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1796
- ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs),
1797
- ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1798
- ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs),
1799
- ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs),
1800
- ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs),
1801
- ICPU(INTEL_FAM6_ATOM_GOLDMONT_PLUS, core_funcs),
1802
- ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
2070
+ X86_MATCH(SANDYBRIDGE, core_funcs),
2071
+ X86_MATCH(SANDYBRIDGE_X, core_funcs),
2072
+ X86_MATCH(ATOM_SILVERMONT, silvermont_funcs),
2073
+ X86_MATCH(IVYBRIDGE, core_funcs),
2074
+ X86_MATCH(HASWELL, core_funcs),
2075
+ X86_MATCH(BROADWELL, core_funcs),
2076
+ X86_MATCH(IVYBRIDGE_X, core_funcs),
2077
+ X86_MATCH(HASWELL_X, core_funcs),
2078
+ X86_MATCH(HASWELL_L, core_funcs),
2079
+ X86_MATCH(HASWELL_G, core_funcs),
2080
+ X86_MATCH(BROADWELL_G, core_funcs),
2081
+ X86_MATCH(ATOM_AIRMONT, airmont_funcs),
2082
+ X86_MATCH(SKYLAKE_L, core_funcs),
2083
+ X86_MATCH(BROADWELL_X, core_funcs),
2084
+ X86_MATCH(SKYLAKE, core_funcs),
2085
+ X86_MATCH(BROADWELL_D, core_funcs),
2086
+ X86_MATCH(XEON_PHI_KNL, knl_funcs),
2087
+ X86_MATCH(XEON_PHI_KNM, knl_funcs),
2088
+ X86_MATCH(ATOM_GOLDMONT, core_funcs),
2089
+ X86_MATCH(ATOM_GOLDMONT_PLUS, core_funcs),
2090
+ X86_MATCH(SKYLAKE_X, core_funcs),
18032091 {}
18042092 };
18052093 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
18062094
18072095 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
1808
- ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_funcs),
1809
- ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1810
- ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
2096
+ X86_MATCH(BROADWELL_D, core_funcs),
2097
+ X86_MATCH(BROADWELL_X, core_funcs),
2098
+ X86_MATCH(SKYLAKE_X, core_funcs),
18112099 {}
18122100 };
18132101
18142102 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
1815
- ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs),
2103
+ X86_MATCH(KABYLAKE, core_funcs),
18162104 {}
18172105 };
18182106
18192107 static const struct x86_cpu_id intel_pstate_hwp_boost_ids[] = {
1820
- ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
1821
- ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_funcs),
2108
+ X86_MATCH(SKYLAKE_X, core_funcs),
2109
+ X86_MATCH(SKYLAKE, core_funcs),
18222110 {}
18232111 };
18242112
....@@ -1835,28 +2123,30 @@
18352123
18362124 all_cpu_data[cpunum] = cpu;
18372125
2126
+ cpu->cpu = cpunum;
2127
+
18382128 cpu->epp_default = -EINVAL;
1839
- cpu->epp_powersave = -EINVAL;
1840
- cpu->epp_saved = -EINVAL;
2129
+
2130
+ if (hwp_active) {
2131
+ const struct x86_cpu_id *id;
2132
+
2133
+ intel_pstate_hwp_enable(cpu);
2134
+
2135
+ id = x86_match_cpu(intel_pstate_hwp_boost_ids);
2136
+ if (id && intel_pstate_acpi_pm_profile_server())
2137
+ hwp_boost = true;
2138
+ }
2139
+ } else if (hwp_active) {
2140
+ /*
2141
+ * Re-enable HWP in case this happens after a resume from ACPI
2142
+ * S3 if the CPU was offline during the whole system/resume
2143
+ * cycle.
2144
+ */
2145
+ intel_pstate_hwp_reenable(cpu);
18412146 }
18422147
1843
- cpu = all_cpu_data[cpunum];
1844
-
1845
- cpu->cpu = cpunum;
1846
-
1847
- if (hwp_active) {
1848
- const struct x86_cpu_id *id;
1849
-
1850
- id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1851
- if (id)
1852
- intel_pstate_disable_ee(cpunum);
1853
-
1854
- intel_pstate_hwp_enable(cpu);
1855
-
1856
- id = x86_match_cpu(intel_pstate_hwp_boost_ids);
1857
- if (id && intel_pstate_acpi_pm_profile_server())
1858
- hwp_boost = true;
1859
- }
2148
+ cpu->epp_powersave = -EINVAL;
2149
+ cpu->epp_policy = 0;
18602150
18612151 intel_pstate_get_cpu_pstates(cpu);
18622152
....@@ -1893,7 +2183,7 @@
18932183
18942184 cpufreq_remove_update_util_hook(cpu);
18952185 cpu_data->update_util_set = false;
1896
- synchronize_sched();
2186
+ synchronize_rcu();
18972187 }
18982188
18992189 static int intel_pstate_get_max_freq(struct cpudata *cpu)
....@@ -1902,12 +2192,13 @@
19022192 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
19032193 }
19042194
1905
-static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
1906
- struct cpudata *cpu)
2195
+static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2196
+ unsigned int policy_min,
2197
+ unsigned int policy_max)
19072198 {
1908
- int max_freq = intel_pstate_get_max_freq(cpu);
19092199 int32_t max_policy_perf, min_policy_perf;
19102200 int max_state, turbo_max;
2201
+ int max_freq;
19112202
19122203 /*
19132204 * HWP needs some special consideration, because on BDX the
....@@ -1915,24 +2206,25 @@
19152206 * rather than pure ratios.
19162207 */
19172208 if (hwp_active) {
1918
- intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
2209
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
19192210 } else {
1920
- max_state = intel_pstate_get_base_pstate(cpu);
2211
+ max_state = global.no_turbo || global.turbo_disabled ?
2212
+ cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
19212213 turbo_max = cpu->pstate.turbo_pstate;
19222214 }
2215
+ max_freq = max_state * cpu->pstate.scaling;
19232216
1924
- max_policy_perf = max_state * policy->max / max_freq;
1925
- if (policy->max == policy->min) {
2217
+ max_policy_perf = max_state * policy_max / max_freq;
2218
+ if (policy_max == policy_min) {
19262219 min_policy_perf = max_policy_perf;
19272220 } else {
1928
- min_policy_perf = max_state * policy->min / max_freq;
2221
+ min_policy_perf = max_state * policy_min / max_freq;
19292222 min_policy_perf = clamp_t(int32_t, min_policy_perf,
19302223 0, max_policy_perf);
19312224 }
19322225
19332226 pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
1934
- policy->cpu, max_state,
1935
- min_policy_perf, max_policy_perf);
2227
+ cpu->cpu, max_state, min_policy_perf, max_policy_perf);
19362228
19372229 /* Normalize user input to [min_perf, max_perf] */
19382230 if (per_cpu_limits) {
....@@ -1946,7 +2238,7 @@
19462238 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
19472239 global_min = clamp_t(int32_t, global_min, 0, global_max);
19482240
1949
- pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
2241
+ pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
19502242 global_min, global_max);
19512243
19522244 cpu->min_perf_ratio = max(min_policy_perf, global_min);
....@@ -1959,7 +2251,7 @@
19592251 cpu->max_perf_ratio);
19602252
19612253 }
1962
- pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
2254
+ pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
19632255 cpu->max_perf_ratio,
19642256 cpu->min_perf_ratio);
19652257 }
....@@ -1979,7 +2271,7 @@
19792271
19802272 mutex_lock(&intel_pstate_limits_lock);
19812273
1982
- intel_pstate_update_perf_limits(policy, cpu);
2274
+ intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
19832275
19842276 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
19852277 /*
....@@ -2008,8 +2300,8 @@
20082300 return 0;
20092301 }
20102302
2011
-static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
2012
- struct cpudata *cpu)
2303
+static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
2304
+ struct cpufreq_policy_data *policy)
20132305 {
20142306 if (!hwp_active &&
20152307 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
....@@ -2020,42 +2312,87 @@
20202312 }
20212313 }
20222314
2023
-static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
2315
+static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
2316
+ struct cpufreq_policy_data *policy)
20242317 {
2025
- struct cpudata *cpu = all_cpu_data[policy->cpu];
2318
+ int max_freq;
20262319
20272320 update_turbo_state();
2028
- cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2029
- intel_pstate_get_max_freq(cpu));
2321
+ if (hwp_active) {
2322
+ int max_state, turbo_max;
20302323
2031
- if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
2032
- policy->policy != CPUFREQ_POLICY_PERFORMANCE)
2033
- return -EINVAL;
2324
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2325
+ max_freq = max_state * cpu->pstate.scaling;
2326
+ } else {
2327
+ max_freq = intel_pstate_get_max_freq(cpu);
2328
+ }
2329
+ cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
20342330
2035
- intel_pstate_adjust_policy_max(policy, cpu);
2331
+ intel_pstate_adjust_policy_max(cpu, policy);
2332
+}
2333
+
2334
+static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
2335
+{
2336
+ intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
20362337
20372338 return 0;
20382339 }
20392340
2040
-static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
2341
+static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
20412342 {
2042
- intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
2343
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2344
+
2345
+ pr_debug("CPU %d going offline\n", cpu->cpu);
2346
+
2347
+ if (cpu->suspended)
2348
+ return 0;
2349
+
2350
+ /*
2351
+ * If the CPU is an SMT thread and it goes offline with the performance
2352
+ * settings different from the minimum, it will prevent its sibling
2353
+ * from getting to lower performance levels, so force the minimum
2354
+ * performance on CPU offline to prevent that from happening.
2355
+ */
2356
+ if (hwp_active)
2357
+ intel_pstate_hwp_offline(cpu);
2358
+ else
2359
+ intel_pstate_set_min_pstate(cpu);
2360
+
2361
+ intel_pstate_exit_perf_limits(policy);
2362
+
2363
+ return 0;
2364
+}
2365
+
2366
+static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
2367
+{
2368
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2369
+
2370
+ pr_debug("CPU %d going online\n", cpu->cpu);
2371
+
2372
+ intel_pstate_init_acpi_perf_limits(policy);
2373
+
2374
+ if (hwp_active) {
2375
+ /*
2376
+ * Re-enable HWP and clear the "suspended" flag to let "resume"
2377
+ * know that it need not do that.
2378
+ */
2379
+ intel_pstate_hwp_reenable(cpu);
2380
+ cpu->suspended = false;
2381
+ }
2382
+
2383
+ return 0;
20432384 }
20442385
20452386 static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
20462387 {
2047
- pr_debug("CPU %d exiting\n", policy->cpu);
2388
+ pr_debug("CPU %d stopping\n", policy->cpu);
20482389
20492390 intel_pstate_clear_update_util_hook(policy->cpu);
2050
- if (hwp_active)
2051
- intel_pstate_hwp_save_state(policy);
2052
- else
2053
- intel_cpufreq_stop_cpu(policy);
20542391 }
20552392
20562393 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
20572394 {
2058
- intel_pstate_exit_perf_limits(policy);
2395
+ pr_debug("CPU %d exiting\n", policy->cpu);
20592396
20602397 policy->fast_switch_possible = false;
20612398
....@@ -2082,6 +2419,7 @@
20822419 /* cpuinfo and default policy values */
20832420 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
20842421 update_turbo_state();
2422
+ global.turbo_disabled_mf = global.turbo_disabled;
20852423 policy->cpuinfo.max_freq = global.turbo_disabled ?
20862424 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
20872425 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
....@@ -2109,10 +2447,17 @@
21092447 if (ret)
21102448 return ret;
21112449
2112
- if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
2113
- policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2114
- else
2115
- policy->policy = CPUFREQ_POLICY_POWERSAVE;
2450
+ /*
2451
+ * Set the policy to powersave to provide a valid fallback value in case
2452
+ * the default cpufreq governor is neither powersave nor performance.
2453
+ */
2454
+ policy->policy = CPUFREQ_POLICY_POWERSAVE;
2455
+
2456
+ if (hwp_active) {
2457
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2458
+
2459
+ cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
2460
+ }
21162461
21172462 return 0;
21182463 }
....@@ -2121,25 +2466,23 @@
21212466 .flags = CPUFREQ_CONST_LOOPS,
21222467 .verify = intel_pstate_verify_policy,
21232468 .setpolicy = intel_pstate_set_policy,
2124
- .suspend = intel_pstate_hwp_save_state,
2469
+ .suspend = intel_pstate_suspend,
21252470 .resume = intel_pstate_resume,
21262471 .init = intel_pstate_cpu_init,
21272472 .exit = intel_pstate_cpu_exit,
21282473 .stop_cpu = intel_pstate_stop_cpu,
2474
+ .offline = intel_pstate_cpu_offline,
2475
+ .online = intel_pstate_cpu_online,
2476
+ .update_limits = intel_pstate_update_limits,
21292477 .name = "intel_pstate",
21302478 };
21312479
2132
-static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2480
+static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
21332481 {
21342482 struct cpudata *cpu = all_cpu_data[policy->cpu];
21352483
2136
- update_turbo_state();
2137
- cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2138
- intel_pstate_get_max_freq(cpu));
2139
-
2140
- intel_pstate_adjust_policy_max(policy, cpu);
2141
-
2142
- intel_pstate_update_perf_limits(policy, cpu);
2484
+ intel_pstate_verify_cpu_policy(cpu, policy);
2485
+ intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
21432486
21442487 return 0;
21452488 }
....@@ -2182,13 +2525,71 @@
21822525 fp_toint(cpu->iowait_boost * 100));
21832526 }
21842527
2528
+static void intel_cpufreq_adjust_hwp(struct cpudata *cpu, u32 target_pstate,
2529
+ bool strict, bool fast_switch)
2530
+{
2531
+ u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
2532
+
2533
+ value &= ~HWP_MIN_PERF(~0L);
2534
+ value |= HWP_MIN_PERF(target_pstate);
2535
+
2536
+ /*
2537
+ * The entire MSR needs to be updated in order to update the HWP min
2538
+ * field in it, so opportunistically update the max too if needed.
2539
+ */
2540
+ value &= ~HWP_MAX_PERF(~0L);
2541
+ value |= HWP_MAX_PERF(strict ? target_pstate : cpu->max_perf_ratio);
2542
+
2543
+ if (value == prev)
2544
+ return;
2545
+
2546
+ WRITE_ONCE(cpu->hwp_req_cached, value);
2547
+ if (fast_switch)
2548
+ wrmsrl(MSR_HWP_REQUEST, value);
2549
+ else
2550
+ wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
2551
+}
2552
+
2553
+static void intel_cpufreq_adjust_perf_ctl(struct cpudata *cpu,
2554
+ u32 target_pstate, bool fast_switch)
2555
+{
2556
+ if (fast_switch)
2557
+ wrmsrl(MSR_IA32_PERF_CTL,
2558
+ pstate_funcs.get_val(cpu, target_pstate));
2559
+ else
2560
+ wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
2561
+ pstate_funcs.get_val(cpu, target_pstate));
2562
+}
2563
+
2564
+static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
2565
+ int target_pstate, bool fast_switch)
2566
+{
2567
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2568
+ int old_pstate = cpu->pstate.current_pstate;
2569
+
2570
+ target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2571
+ if (hwp_active) {
2572
+ intel_cpufreq_adjust_hwp(cpu, target_pstate,
2573
+ policy->strict_target, fast_switch);
2574
+ cpu->pstate.current_pstate = target_pstate;
2575
+ } else if (target_pstate != old_pstate) {
2576
+ intel_cpufreq_adjust_perf_ctl(cpu, target_pstate, fast_switch);
2577
+ cpu->pstate.current_pstate = target_pstate;
2578
+ }
2579
+
2580
+ intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
2581
+ INTEL_PSTATE_TRACE_TARGET, old_pstate);
2582
+
2583
+ return target_pstate;
2584
+}
2585
+
21852586 static int intel_cpufreq_target(struct cpufreq_policy *policy,
21862587 unsigned int target_freq,
21872588 unsigned int relation)
21882589 {
21892590 struct cpudata *cpu = all_cpu_data[policy->cpu];
21902591 struct cpufreq_freqs freqs;
2191
- int target_pstate, old_pstate;
2592
+ int target_pstate;
21922593
21932594 update_turbo_state();
21942595
....@@ -2196,6 +2597,7 @@
21962597 freqs.new = target_freq;
21972598
21982599 cpufreq_freq_transition_begin(policy, &freqs);
2600
+
21992601 switch (relation) {
22002602 case CPUFREQ_RELATION_L:
22012603 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
....@@ -2207,15 +2609,11 @@
22072609 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
22082610 break;
22092611 }
2210
- target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2211
- old_pstate = cpu->pstate.current_pstate;
2212
- if (target_pstate != cpu->pstate.current_pstate) {
2213
- cpu->pstate.current_pstate = target_pstate;
2214
- wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2215
- pstate_funcs.get_val(cpu, target_pstate));
2216
- }
2612
+
2613
+ target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
2614
+
22172615 freqs.new = target_pstate * cpu->pstate.scaling;
2218
- intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate);
2616
+
22192617 cpufreq_freq_transition_end(policy, &freqs, false);
22202618
22212619 return 0;
....@@ -2225,31 +2623,101 @@
22252623 unsigned int target_freq)
22262624 {
22272625 struct cpudata *cpu = all_cpu_data[policy->cpu];
2228
- int target_pstate, old_pstate;
2626
+ int target_pstate;
22292627
22302628 update_turbo_state();
22312629
22322630 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2233
- target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2234
- old_pstate = cpu->pstate.current_pstate;
2235
- intel_pstate_update_pstate(cpu, target_pstate);
2236
- intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
2631
+
2632
+ target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
2633
+
22372634 return target_pstate * cpu->pstate.scaling;
22382635 }
22392636
22402637 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
22412638 {
2242
- int ret = __intel_pstate_cpu_init(policy);
2639
+ int max_state, turbo_max, min_freq, max_freq, ret;
2640
+ struct freq_qos_request *req;
2641
+ struct cpudata *cpu;
2642
+ struct device *dev;
22432643
2644
+ dev = get_cpu_device(policy->cpu);
2645
+ if (!dev)
2646
+ return -ENODEV;
2647
+
2648
+ ret = __intel_pstate_cpu_init(policy);
22442649 if (ret)
22452650 return ret;
22462651
22472652 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2248
- policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
22492653 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
22502654 policy->cur = policy->cpuinfo.min_freq;
22512655
2656
+ req = kcalloc(2, sizeof(*req), GFP_KERNEL);
2657
+ if (!req) {
2658
+ ret = -ENOMEM;
2659
+ goto pstate_exit;
2660
+ }
2661
+
2662
+ cpu = all_cpu_data[policy->cpu];
2663
+
2664
+ if (hwp_active) {
2665
+ u64 value;
2666
+
2667
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2668
+ policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
2669
+ rdmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
2670
+ WRITE_ONCE(cpu->hwp_req_cached, value);
2671
+ cpu->epp_cached = intel_pstate_get_epp(cpu, value);
2672
+ } else {
2673
+ turbo_max = cpu->pstate.turbo_pstate;
2674
+ policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
2675
+ }
2676
+
2677
+ min_freq = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2678
+ min_freq *= cpu->pstate.scaling;
2679
+ max_freq = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2680
+ max_freq *= cpu->pstate.scaling;
2681
+
2682
+ ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
2683
+ min_freq);
2684
+ if (ret < 0) {
2685
+ dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
2686
+ goto free_req;
2687
+ }
2688
+
2689
+ ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
2690
+ max_freq);
2691
+ if (ret < 0) {
2692
+ dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
2693
+ goto remove_min_req;
2694
+ }
2695
+
2696
+ policy->driver_data = req;
2697
+
22522698 return 0;
2699
+
2700
+remove_min_req:
2701
+ freq_qos_remove_request(req);
2702
+free_req:
2703
+ kfree(req);
2704
+pstate_exit:
2705
+ intel_pstate_exit_perf_limits(policy);
2706
+
2707
+ return ret;
2708
+}
2709
+
2710
+static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
2711
+{
2712
+ struct freq_qos_request *req;
2713
+
2714
+ req = policy->driver_data;
2715
+
2716
+ freq_qos_remove_request(req + 1);
2717
+ freq_qos_remove_request(req);
2718
+ kfree(req);
2719
+
2720
+ return intel_pstate_cpu_exit(policy);
22532721 }
22542722
22552723 static struct cpufreq_driver intel_cpufreq = {
....@@ -2258,12 +2726,16 @@
22582726 .target = intel_cpufreq_target,
22592727 .fast_switch = intel_cpufreq_fast_switch,
22602728 .init = intel_cpufreq_cpu_init,
2261
- .exit = intel_pstate_cpu_exit,
2262
- .stop_cpu = intel_cpufreq_stop_cpu,
2729
+ .exit = intel_cpufreq_cpu_exit,
2730
+ .offline = intel_pstate_cpu_offline,
2731
+ .online = intel_pstate_cpu_online,
2732
+ .suspend = intel_pstate_suspend,
2733
+ .resume = intel_pstate_resume,
2734
+ .update_limits = intel_pstate_update_limits,
22632735 .name = "intel_cpufreq",
22642736 };
22652737
2266
-static struct cpufreq_driver *default_driver = &intel_pstate;
2738
+static struct cpufreq_driver *default_driver;
22672739
22682740 static void intel_pstate_driver_cleanup(void)
22692741 {
....@@ -2280,12 +2752,16 @@
22802752 }
22812753 }
22822754 put_online_cpus();
2755
+
22832756 intel_pstate_driver = NULL;
22842757 }
22852758
22862759 static int intel_pstate_register_driver(struct cpufreq_driver *driver)
22872760 {
22882761 int ret;
2762
+
2763
+ if (driver == &intel_pstate)
2764
+ intel_pstate_sysfs_expose_hwp_dynamic_boost();
22892765
22902766 memset(&global, 0, sizeof(global));
22912767 global.max_perf_pct = 100;
....@@ -2302,17 +2778,6 @@
23022778 return 0;
23032779 }
23042780
2305
-static int intel_pstate_unregister_driver(void)
2306
-{
2307
- if (hwp_active)
2308
- return -EBUSY;
2309
-
2310
- cpufreq_unregister_driver(intel_pstate_driver);
2311
- intel_pstate_driver_cleanup();
2312
-
2313
- return 0;
2314
-}
2315
-
23162781 static ssize_t intel_pstate_show_status(char *buf)
23172782 {
23182783 if (!intel_pstate_driver)
....@@ -2324,8 +2789,6 @@
23242789
23252790 static int intel_pstate_update_status(const char *buf, size_t size)
23262791 {
2327
- int ret;
2328
-
23292792 if (size == 3 && !strncmp(buf, "off", size)) {
23302793 if (!intel_pstate_driver)
23312794 return -EINVAL;
....@@ -2333,7 +2796,9 @@
23332796 if (hwp_active)
23342797 return -EBUSY;
23352798
2336
- return intel_pstate_unregister_driver();
2799
+ cpufreq_unregister_driver(intel_pstate_driver);
2800
+ intel_pstate_driver_cleanup();
2801
+ return 0;
23372802 }
23382803
23392804 if (size == 6 && !strncmp(buf, "active", size)) {
....@@ -2341,9 +2806,7 @@
23412806 if (intel_pstate_driver == &intel_pstate)
23422807 return 0;
23432808
2344
- ret = intel_pstate_unregister_driver();
2345
- if (ret)
2346
- return ret;
2809
+ cpufreq_unregister_driver(intel_pstate_driver);
23472810 }
23482811
23492812 return intel_pstate_register_driver(&intel_pstate);
....@@ -2354,9 +2817,8 @@
23542817 if (intel_pstate_driver == &intel_cpufreq)
23552818 return 0;
23562819
2357
- ret = intel_pstate_unregister_driver();
2358
- if (ret)
2359
- return ret;
2820
+ cpufreq_unregister_driver(intel_pstate_driver);
2821
+ intel_pstate_sysfs_hide_hwp_dynamic_boost();
23602822 }
23612823
23622824 return intel_pstate_register_driver(&intel_cpufreq);
....@@ -2420,6 +2882,7 @@
24202882 kfree(pss);
24212883 }
24222884
2885
+ pr_debug("ACPI _PSS not found\n");
24232886 return true;
24242887 }
24252888
....@@ -2430,9 +2893,14 @@
24302893
24312894 status = acpi_get_handle(NULL, "\\_SB", &handle);
24322895 if (ACPI_FAILURE(status))
2433
- return true;
2896
+ goto not_found;
24342897
2435
- return !acpi_has_method(handle, "PCCH");
2898
+ if (acpi_has_method(handle, "PCCH"))
2899
+ return false;
2900
+
2901
+not_found:
2902
+ pr_debug("ACPI PCCH not found\n");
2903
+ return true;
24362904 }
24372905
24382906 static bool __init intel_pstate_has_acpi_ppc(void)
....@@ -2447,6 +2915,7 @@
24472915 if (acpi_has_method(pr->handle, "_PPC"))
24482916 return true;
24492917 }
2918
+ pr_debug("ACPI _PPC not found\n");
24502919 return false;
24512920 }
24522921
....@@ -2457,23 +2926,25 @@
24572926
24582927 /* Hardware vendor-specific info that has its own power management modes */
24592928 static struct acpi_platform_list plat_info[] __initdata = {
2460
- {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS},
2461
- {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2462
- {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2463
- {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2464
- {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2465
- {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2466
- {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2467
- {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2468
- {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2469
- {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2470
- {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2471
- {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2472
- {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2473
- {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2474
- {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2929
+ {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
2930
+ {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2931
+ {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2932
+ {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2933
+ {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2934
+ {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2935
+ {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2936
+ {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2937
+ {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2938
+ {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2939
+ {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2940
+ {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2941
+ {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2942
+ {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2943
+ {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
24752944 { } /* End */
24762945 };
2946
+
2947
+#define BITMASK_OOB (BIT(8) | BIT(18))
24772948
24782949 static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
24792950 {
....@@ -2484,8 +2955,11 @@
24842955 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
24852956 if (id) {
24862957 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2487
- if ( misc_pwr & (1 << 8))
2958
+ if (misc_pwr & BITMASK_OOB) {
2959
+ pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
2960
+ pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
24882961 return true;
2962
+ }
24892963 }
24902964
24912965 idx = acpi_match_platform_list(plat_info);
....@@ -2522,51 +2996,93 @@
25222996
25232997 #define INTEL_PSTATE_HWP_BROADWELL 0x01
25242998
2525
-#define ICPU_HWP(model, hwp_mode) \
2526
- { X86_VENDOR_INTEL, 6, model, X86_FEATURE_HWP, hwp_mode }
2999
+#define X86_MATCH_HWP(model, hwp_mode) \
3000
+ X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
3001
+ X86_FEATURE_HWP, hwp_mode)
25273002
25283003 static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2529
- ICPU_HWP(INTEL_FAM6_BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
2530
- ICPU_HWP(INTEL_FAM6_BROADWELL_XEON_D, INTEL_PSTATE_HWP_BROADWELL),
2531
- ICPU_HWP(X86_MODEL_ANY, 0),
3004
+ X86_MATCH_HWP(BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
3005
+ X86_MATCH_HWP(BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
3006
+ X86_MATCH_HWP(ANY, 0),
25323007 {}
25333008 };
3009
+
3010
+static bool intel_pstate_hwp_is_enabled(void)
3011
+{
3012
+ u64 value;
3013
+
3014
+ rdmsrl(MSR_PM_ENABLE, value);
3015
+ return !!(value & 0x1);
3016
+}
25343017
25353018 static int __init intel_pstate_init(void)
25363019 {
25373020 const struct x86_cpu_id *id;
25383021 int rc;
25393022
2540
- if (no_load)
3023
+ if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
25413024 return -ENODEV;
25423025
25433026 id = x86_match_cpu(hwp_support_ids);
25443027 if (id) {
3028
+ bool hwp_forced = intel_pstate_hwp_is_enabled();
3029
+
3030
+ if (hwp_forced)
3031
+ pr_info("HWP enabled by BIOS\n");
3032
+ else if (no_load)
3033
+ return -ENODEV;
3034
+
25453035 copy_cpu_funcs(&core_funcs);
2546
- if (!no_hwp) {
3036
+ /*
3037
+ * Avoid enabling HWP for processors without EPP support,
3038
+ * because that means incomplete HWP implementation which is a
3039
+ * corner case and supporting it is generally problematic.
3040
+ *
3041
+ * If HWP is enabled already, though, there is no choice but to
3042
+ * deal with it.
3043
+ */
3044
+ if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
25473045 hwp_active++;
25483046 hwp_mode_bdw = id->driver_data;
25493047 intel_pstate.attr = hwp_cpufreq_attrs;
3048
+ intel_cpufreq.attr = hwp_cpufreq_attrs;
3049
+ intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
3050
+ if (!default_driver)
3051
+ default_driver = &intel_pstate;
3052
+
25503053 goto hwp_cpu_matched;
25513054 }
3055
+ pr_info("HWP not enabled\n");
25523056 } else {
2553
- id = x86_match_cpu(intel_pstate_cpu_ids);
2554
- if (!id)
3057
+ if (no_load)
25553058 return -ENODEV;
3059
+
3060
+ id = x86_match_cpu(intel_pstate_cpu_ids);
3061
+ if (!id) {
3062
+ pr_info("CPU model not supported\n");
3063
+ return -ENODEV;
3064
+ }
25563065
25573066 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
25583067 }
25593068
2560
- if (intel_pstate_msrs_not_valid())
3069
+ if (intel_pstate_msrs_not_valid()) {
3070
+ pr_info("Invalid MSRs\n");
25613071 return -ENODEV;
3072
+ }
3073
+ /* Without HWP start in the passive mode. */
3074
+ if (!default_driver)
3075
+ default_driver = &intel_cpufreq;
25623076
25633077 hwp_cpu_matched:
25643078 /*
25653079 * The Intel pstate driver will be ignored if the platform
25663080 * firmware has its own power management modes.
25673081 */
2568
- if (intel_pstate_platform_pwr_mgmt_exists())
3082
+ if (intel_pstate_platform_pwr_mgmt_exists()) {
3083
+ pr_info("P-states controlled by the platform\n");
25693084 return -ENODEV;
3085
+ }
25703086
25713087 if (!hwp_active && hwp_only)
25723088 return -ENOTSUPP;
....@@ -2584,11 +3100,22 @@
25843100 mutex_lock(&intel_pstate_driver_lock);
25853101 rc = intel_pstate_register_driver(default_driver);
25863102 mutex_unlock(&intel_pstate_driver_lock);
2587
- if (rc)
3103
+ if (rc) {
3104
+ intel_pstate_sysfs_remove();
25883105 return rc;
3106
+ }
25893107
2590
- if (hwp_active)
3108
+ if (hwp_active) {
3109
+ const struct x86_cpu_id *id;
3110
+
3111
+ id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
3112
+ if (id) {
3113
+ set_power_ctl_ee_state(false);
3114
+ pr_info("Disabling energy efficiency optimization\n");
3115
+ }
3116
+
25913117 pr_info("HWP enabled\n");
3118
+ }
25923119
25933120 return 0;
25943121 }
....@@ -2599,17 +3126,16 @@
25993126 if (!str)
26003127 return -EINVAL;
26013128
2602
- if (!strcmp(str, "disable")) {
3129
+ if (!strcmp(str, "disable"))
26033130 no_load = 1;
2604
- } else if (!strcmp(str, "passive")) {
2605
- pr_info("Passive mode enabled\n");
3131
+ else if (!strcmp(str, "active"))
3132
+ default_driver = &intel_pstate;
3133
+ else if (!strcmp(str, "passive"))
26063134 default_driver = &intel_cpufreq;
3135
+
3136
+ if (!strcmp(str, "no_hwp"))
26073137 no_hwp = 1;
2608
- }
2609
- if (!strcmp(str, "no_hwp")) {
2610
- pr_info("HWP disabled\n");
2611
- no_hwp = 1;
2612
- }
3138
+
26133139 if (!strcmp(str, "force"))
26143140 force_load = 1;
26153141 if (!strcmp(str, "hwp_only"))