forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
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 {
....@@ -459,7 +477,7 @@
459477
460478 acpi_processor_unregister_performance(policy->cpu);
461479 }
462
-#else
480
+#else /* CONFIG_ACPI */
463481 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
464482 {
465483 }
....@@ -472,7 +490,14 @@
472490 {
473491 return false;
474492 }
475
-#endif
493
+#endif /* CONFIG_ACPI */
494
+
495
+#ifndef CONFIG_ACPI_CPPC_LIB
496
+static int intel_pstate_get_cppc_guranteed(int cpu)
497
+{
498
+ return -ENOTSUPP;
499
+}
500
+#endif /* CONFIG_ACPI_CPPC_LIB */
476501
477502 static inline void update_turbo_state(void)
478503 {
....@@ -500,7 +525,7 @@
500525 u64 epb;
501526 int ret;
502527
503
- if (!static_cpu_has(X86_FEATURE_EPB))
528
+ if (!boot_cpu_has(X86_FEATURE_EPB))
504529 return -ENXIO;
505530
506531 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
....@@ -514,7 +539,7 @@
514539 {
515540 s16 epp;
516541
517
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
542
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
518543 /*
519544 * When hwp_req_data is 0, means that caller didn't read
520545 * MSR_HWP_REQUEST, so need to read and get EPP.
....@@ -539,7 +564,7 @@
539564 u64 epb;
540565 int ret;
541566
542
- if (!static_cpu_has(X86_FEATURE_EPB))
567
+ if (!boot_cpu_has(X86_FEATURE_EPB))
543568 return -ENXIO;
544569
545570 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
....@@ -578,25 +603,28 @@
578603 HWP_EPP_POWERSAVE
579604 };
580605
581
-static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
606
+static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
582607 {
583608 s16 epp;
584609 int index = -EINVAL;
585610
611
+ *raw_epp = 0;
586612 epp = intel_pstate_get_epp(cpu_data, 0);
587613 if (epp < 0)
588614 return epp;
589615
590
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
616
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
591617 if (epp == HWP_EPP_PERFORMANCE)
592618 return 1;
593
- if (epp <= HWP_EPP_BALANCE_PERFORMANCE)
619
+ if (epp == HWP_EPP_BALANCE_PERFORMANCE)
594620 return 2;
595
- if (epp <= HWP_EPP_BALANCE_POWERSAVE)
621
+ if (epp == HWP_EPP_BALANCE_POWERSAVE)
596622 return 3;
597
- else
623
+ if (epp == HWP_EPP_POWERSAVE)
598624 return 4;
599
- } else if (static_cpu_has(X86_FEATURE_EPB)) {
625
+ *raw_epp = epp;
626
+ return 0;
627
+ } else if (boot_cpu_has(X86_FEATURE_EPB)) {
600628 /*
601629 * Range:
602630 * 0x00-0x03 : Performance
....@@ -613,8 +641,35 @@
613641 return index;
614642 }
615643
644
+static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp)
645
+{
646
+ int ret;
647
+
648
+ /*
649
+ * Use the cached HWP Request MSR value, because in the active mode the
650
+ * register itself may be updated by intel_pstate_hwp_boost_up() or
651
+ * intel_pstate_hwp_boost_down() at any time.
652
+ */
653
+ u64 value = READ_ONCE(cpu->hwp_req_cached);
654
+
655
+ value &= ~GENMASK_ULL(31, 24);
656
+ value |= (u64)epp << 24;
657
+ /*
658
+ * The only other updater of hwp_req_cached in the active mode,
659
+ * intel_pstate_hwp_set(), is called under the same lock as this
660
+ * function, so it cannot run in parallel with the update below.
661
+ */
662
+ WRITE_ONCE(cpu->hwp_req_cached, value);
663
+ ret = wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
664
+ if (!ret)
665
+ cpu->epp_cached = epp;
666
+
667
+ return ret;
668
+}
669
+
616670 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
617
- int pref_index)
671
+ int pref_index, bool use_raw,
672
+ u32 raw_epp)
618673 {
619674 int epp = -EINVAL;
620675 int ret;
....@@ -622,29 +677,26 @@
622677 if (!pref_index)
623678 epp = cpu_data->epp_default;
624679
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)
680
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
681
+ if (use_raw)
682
+ epp = raw_epp;
683
+ else if (epp == -EINVAL)
637684 epp = epp_values[pref_index - 1];
638685
639
- value |= (u64)epp << 24;
640
- ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
686
+ /*
687
+ * To avoid confusion, refuse to set EPP to any values different
688
+ * from 0 (performance) if the current policy is "performance",
689
+ * because those values would be overridden.
690
+ */
691
+ if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
692
+ return -EBUSY;
693
+
694
+ ret = intel_pstate_set_epp(cpu_data, epp);
641695 } else {
642696 if (epp == -EINVAL)
643697 epp = (pref_index - 1) << 2;
644698 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
645699 }
646
-return_pref:
647
- mutex_unlock(&intel_pstate_limits_lock);
648700
649701 return ret;
650702 }
....@@ -665,53 +717,125 @@
665717
666718 cpufreq_freq_attr_ro(energy_performance_available_preferences);
667719
720
+static struct cpufreq_driver intel_pstate;
721
+
668722 static ssize_t store_energy_performance_preference(
669723 struct cpufreq_policy *policy, const char *buf, size_t count)
670724 {
671
- struct cpudata *cpu_data = all_cpu_data[policy->cpu];
725
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
672726 char str_preference[21];
673
- int ret;
727
+ bool raw = false;
728
+ ssize_t ret;
729
+ u32 epp = 0;
674730
675731 ret = sscanf(buf, "%20s", str_preference);
676732 if (ret != 1)
677733 return -EINVAL;
678734
679735 ret = match_string(energy_perf_strings, -1, str_preference);
680
- if (ret < 0)
681
- return ret;
736
+ if (ret < 0) {
737
+ if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
738
+ return ret;
682739
683
- intel_pstate_set_energy_pref_index(cpu_data, ret);
684
- return count;
740
+ ret = kstrtouint(buf, 10, &epp);
741
+ if (ret)
742
+ return ret;
743
+
744
+ if (epp > 255)
745
+ return -EINVAL;
746
+
747
+ raw = true;
748
+ }
749
+
750
+ /*
751
+ * This function runs with the policy R/W semaphore held, which
752
+ * guarantees that the driver pointer will not change while it is
753
+ * running.
754
+ */
755
+ if (!intel_pstate_driver)
756
+ return -EAGAIN;
757
+
758
+ mutex_lock(&intel_pstate_limits_lock);
759
+
760
+ if (intel_pstate_driver == &intel_pstate) {
761
+ ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp);
762
+ } else {
763
+ /*
764
+ * In the passive mode the governor needs to be stopped on the
765
+ * target CPU before the EPP update and restarted after it,
766
+ * which is super-heavy-weight, so make sure it is worth doing
767
+ * upfront.
768
+ */
769
+ if (!raw)
770
+ epp = ret ? epp_values[ret - 1] : cpu->epp_default;
771
+
772
+ if (cpu->epp_cached != epp) {
773
+ int err;
774
+
775
+ cpufreq_stop_governor(policy);
776
+ ret = intel_pstate_set_epp(cpu, epp);
777
+ err = cpufreq_start_governor(policy);
778
+ if (!ret)
779
+ ret = err;
780
+ }
781
+ }
782
+
783
+ mutex_unlock(&intel_pstate_limits_lock);
784
+
785
+ return ret ?: count;
685786 }
686787
687788 static ssize_t show_energy_performance_preference(
688789 struct cpufreq_policy *policy, char *buf)
689790 {
690791 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
691
- int preference;
792
+ int preference, raw_epp;
692793
693
- preference = intel_pstate_get_energy_pref_index(cpu_data);
794
+ preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
694795 if (preference < 0)
695796 return preference;
696797
697
- return sprintf(buf, "%s\n", energy_perf_strings[preference]);
798
+ if (raw_epp)
799
+ return sprintf(buf, "%d\n", raw_epp);
800
+ else
801
+ return sprintf(buf, "%s\n", energy_perf_strings[preference]);
698802 }
699803
700804 cpufreq_freq_attr_rw(energy_performance_preference);
701805
806
+static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
807
+{
808
+ struct cpudata *cpu;
809
+ u64 cap;
810
+ int ratio;
811
+
812
+ ratio = intel_pstate_get_cppc_guranteed(policy->cpu);
813
+ if (ratio <= 0) {
814
+ rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
815
+ ratio = HWP_GUARANTEED_PERF(cap);
816
+ }
817
+
818
+ cpu = all_cpu_data[policy->cpu];
819
+
820
+ return sprintf(buf, "%d\n", ratio * cpu->pstate.scaling);
821
+}
822
+
823
+cpufreq_freq_attr_ro(base_frequency);
824
+
702825 static struct freq_attr *hwp_cpufreq_attrs[] = {
703826 &energy_performance_preference,
704827 &energy_performance_available_preferences,
828
+ &base_frequency,
705829 NULL,
706830 };
707831
708
-static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
832
+static void intel_pstate_get_hwp_max(struct cpudata *cpu, int *phy_max,
709833 int *current_max)
710834 {
711835 u64 cap;
712836
713
- rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
714
- WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap);
837
+ rdmsrl_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap);
838
+ WRITE_ONCE(cpu->hwp_cap_cached, cap);
715839 if (global.no_turbo || global.turbo_disabled)
716840 *current_max = HWP_GUARANTEED_PERF(cap);
717841 else
....@@ -746,12 +870,6 @@
746870
747871 cpu_data->epp_policy = cpu_data->policy;
748872
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
-
755873 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
756874 epp = intel_pstate_get_epp(cpu_data, value);
757875 cpu_data->epp_powersave = epp;
....@@ -778,8 +896,7 @@
778896
779897 epp = cpu_data->epp_powersave;
780898 }
781
-update_epp:
782
- if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
899
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
783900 value &= ~GENMASK_ULL(31, 24);
784901 value |= (u64)epp << 24;
785902 } else {
....@@ -790,34 +907,99 @@
790907 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
791908 }
792909
793
-static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
910
+static void intel_pstate_hwp_offline(struct cpudata *cpu)
794911 {
795
- struct cpudata *cpu_data = all_cpu_data[policy->cpu];
912
+ u64 value = READ_ONCE(cpu->hwp_req_cached);
913
+ int min_perf;
796914
797
- if (!hwp_active)
798
- return 0;
915
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
916
+ /*
917
+ * In case the EPP has been set to "performance" by the
918
+ * active mode "performance" scaling algorithm, replace that
919
+ * temporary value with the cached EPP one.
920
+ */
921
+ value &= ~GENMASK_ULL(31, 24);
922
+ value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached);
923
+ WRITE_ONCE(cpu->hwp_req_cached, value);
924
+ }
799925
800
- cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
926
+ value &= ~GENMASK_ULL(31, 0);
927
+ min_perf = HWP_LOWEST_PERF(cpu->hwp_cap_cached);
801928
802
- return 0;
929
+ /* Set hwp_max = hwp_min */
930
+ value |= HWP_MAX_PERF(min_perf);
931
+ value |= HWP_MIN_PERF(min_perf);
932
+
933
+ /* Set EPP to min */
934
+ if (boot_cpu_has(X86_FEATURE_HWP_EPP))
935
+ value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
936
+
937
+ wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
938
+}
939
+
940
+#define POWER_CTL_EE_ENABLE 1
941
+#define POWER_CTL_EE_DISABLE 2
942
+
943
+static int power_ctl_ee_state;
944
+
945
+static void set_power_ctl_ee_state(bool input)
946
+{
947
+ u64 power_ctl;
948
+
949
+ mutex_lock(&intel_pstate_driver_lock);
950
+ rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
951
+ if (input) {
952
+ power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
953
+ power_ctl_ee_state = POWER_CTL_EE_ENABLE;
954
+ } else {
955
+ power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
956
+ power_ctl_ee_state = POWER_CTL_EE_DISABLE;
957
+ }
958
+ wrmsrl(MSR_IA32_POWER_CTL, power_ctl);
959
+ mutex_unlock(&intel_pstate_driver_lock);
803960 }
804961
805962 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
806963
964
+static void intel_pstate_hwp_reenable(struct cpudata *cpu)
965
+{
966
+ intel_pstate_hwp_enable(cpu);
967
+ wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached));
968
+}
969
+
970
+static int intel_pstate_suspend(struct cpufreq_policy *policy)
971
+{
972
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
973
+
974
+ pr_debug("CPU %d suspending\n", cpu->cpu);
975
+
976
+ cpu->suspended = true;
977
+
978
+ return 0;
979
+}
980
+
807981 static int intel_pstate_resume(struct cpufreq_policy *policy)
808982 {
809
- if (!hwp_active)
810
- return 0;
983
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
811984
812
- mutex_lock(&intel_pstate_limits_lock);
985
+ pr_debug("CPU %d resuming\n", cpu->cpu);
813986
814
- if (policy->cpu == 0)
815
- intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
987
+ /* Only restore if the system default is changed */
988
+ if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
989
+ set_power_ctl_ee_state(true);
990
+ else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
991
+ set_power_ctl_ee_state(false);
816992
817
- all_cpu_data[policy->cpu]->epp_policy = 0;
818
- intel_pstate_hwp_set(policy->cpu);
993
+ if (cpu->suspended && hwp_active) {
994
+ mutex_lock(&intel_pstate_limits_lock);
819995
820
- mutex_unlock(&intel_pstate_limits_lock);
996
+ /* Re-enable HWP, because "online" has not done that. */
997
+ intel_pstate_hwp_reenable(cpu);
998
+
999
+ mutex_unlock(&intel_pstate_limits_lock);
1000
+ }
1001
+
1002
+ cpu->suspended = false;
8211003
8221004 return 0;
8231005 }
....@@ -828,6 +1010,44 @@
8281010
8291011 for_each_possible_cpu(cpu)
8301012 cpufreq_update_policy(cpu);
1013
+}
1014
+
1015
+static void intel_pstate_update_max_freq(unsigned int cpu)
1016
+{
1017
+ struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
1018
+ struct cpudata *cpudata;
1019
+
1020
+ if (!policy)
1021
+ return;
1022
+
1023
+ cpudata = all_cpu_data[cpu];
1024
+ policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
1025
+ cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
1026
+
1027
+ refresh_frequency_limits(policy);
1028
+
1029
+ cpufreq_cpu_release(policy);
1030
+}
1031
+
1032
+static void intel_pstate_update_limits(unsigned int cpu)
1033
+{
1034
+ mutex_lock(&intel_pstate_driver_lock);
1035
+
1036
+ update_turbo_state();
1037
+ /*
1038
+ * If turbo has been turned on or off globally, policy limits for
1039
+ * all CPUs need to be updated to reflect that.
1040
+ */
1041
+ if (global.turbo_disabled_mf != global.turbo_disabled) {
1042
+ global.turbo_disabled_mf = global.turbo_disabled;
1043
+ arch_set_max_freq_ratio(global.turbo_disabled);
1044
+ for_each_possible_cpu(cpu)
1045
+ intel_pstate_update_max_freq(cpu);
1046
+ } else {
1047
+ cpufreq_update_policy(cpu);
1048
+ }
1049
+
1050
+ mutex_unlock(&intel_pstate_driver_lock);
8311051 }
8321052
8331053 /************************** sysfs begin ************************/
....@@ -983,6 +1203,45 @@
9831203 return count;
9841204 }
9851205
1206
+static void update_qos_request(enum freq_qos_req_type type)
1207
+{
1208
+ int max_state, turbo_max, freq, i, perf_pct;
1209
+ struct freq_qos_request *req;
1210
+ struct cpufreq_policy *policy;
1211
+
1212
+ for_each_possible_cpu(i) {
1213
+ struct cpudata *cpu = all_cpu_data[i];
1214
+
1215
+ policy = cpufreq_cpu_get(i);
1216
+ if (!policy)
1217
+ continue;
1218
+
1219
+ req = policy->driver_data;
1220
+ cpufreq_cpu_put(policy);
1221
+
1222
+ if (!req)
1223
+ continue;
1224
+
1225
+ if (hwp_active)
1226
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
1227
+ else
1228
+ turbo_max = cpu->pstate.turbo_pstate;
1229
+
1230
+ if (type == FREQ_QOS_MIN) {
1231
+ perf_pct = global.min_perf_pct;
1232
+ } else {
1233
+ req++;
1234
+ perf_pct = global.max_perf_pct;
1235
+ }
1236
+
1237
+ freq = DIV_ROUND_UP(turbo_max * perf_pct, 100);
1238
+ freq *= cpu->pstate.scaling;
1239
+
1240
+ if (freq_qos_update_request(req, freq) < 0)
1241
+ pr_warn("Failed to update freq constraint: CPU%d\n", i);
1242
+ }
1243
+}
1244
+
9861245 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
9871246 const char *buf, size_t count)
9881247 {
....@@ -1006,7 +1265,10 @@
10061265
10071266 mutex_unlock(&intel_pstate_limits_lock);
10081267
1009
- intel_pstate_update_policies();
1268
+ if (intel_pstate_driver == &intel_pstate)
1269
+ intel_pstate_update_policies();
1270
+ else
1271
+ update_qos_request(FREQ_QOS_MAX);
10101272
10111273 mutex_unlock(&intel_pstate_driver_lock);
10121274
....@@ -1037,7 +1299,10 @@
10371299
10381300 mutex_unlock(&intel_pstate_limits_lock);
10391301
1040
- intel_pstate_update_policies();
1302
+ if (intel_pstate_driver == &intel_pstate)
1303
+ intel_pstate_update_policies();
1304
+ else
1305
+ update_qos_request(FREQ_QOS_MIN);
10411306
10421307 mutex_unlock(&intel_pstate_driver_lock);
10431308
....@@ -1069,6 +1334,32 @@
10691334 return count;
10701335 }
10711336
1337
+static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
1338
+ char *buf)
1339
+{
1340
+ u64 power_ctl;
1341
+ int enable;
1342
+
1343
+ rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
1344
+ enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
1345
+ return sprintf(buf, "%d\n", !enable);
1346
+}
1347
+
1348
+static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
1349
+ const char *buf, size_t count)
1350
+{
1351
+ bool input;
1352
+ int ret;
1353
+
1354
+ ret = kstrtobool(buf, &input);
1355
+ if (ret)
1356
+ return ret;
1357
+
1358
+ set_power_ctl_ee_state(input);
1359
+
1360
+ return count;
1361
+}
1362
+
10721363 show_one(max_perf_pct, max_perf_pct);
10731364 show_one(min_perf_pct, min_perf_pct);
10741365
....@@ -1079,6 +1370,7 @@
10791370 define_one_global_ro(turbo_pct);
10801371 define_one_global_ro(num_pstates);
10811372 define_one_global_rw(hwp_dynamic_boost);
1373
+define_one_global_rw(energy_efficiency);
10821374
10831375 static struct attribute *intel_pstate_attributes[] = {
10841376 &status.attr,
....@@ -1092,9 +1384,12 @@
10921384 .attrs = intel_pstate_attributes,
10931385 };
10941386
1387
+static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
1388
+
1389
+static struct kobject *intel_pstate_kobject;
1390
+
10951391 static void __init intel_pstate_sysfs_expose_params(void)
10961392 {
1097
- struct kobject *intel_pstate_kobject;
10981393 int rc;
10991394
11001395 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
....@@ -1119,43 +1414,60 @@
11191414 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
11201415 WARN_ON(rc);
11211416
1122
- if (hwp_active) {
1123
- rc = sysfs_create_file(intel_pstate_kobject,
1124
- &hwp_dynamic_boost.attr);
1417
+ if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
1418
+ rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
11251419 WARN_ON(rc);
11261420 }
11271421 }
1422
+
1423
+static void __init intel_pstate_sysfs_remove(void)
1424
+{
1425
+ if (!intel_pstate_kobject)
1426
+ return;
1427
+
1428
+ sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group);
1429
+
1430
+ if (!per_cpu_limits) {
1431
+ sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr);
1432
+ sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr);
1433
+
1434
+ if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids))
1435
+ sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr);
1436
+ }
1437
+
1438
+ kobject_put(intel_pstate_kobject);
1439
+}
1440
+
1441
+static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void)
1442
+{
1443
+ int rc;
1444
+
1445
+ if (!hwp_active)
1446
+ return;
1447
+
1448
+ rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1449
+ WARN_ON_ONCE(rc);
1450
+}
1451
+
1452
+static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void)
1453
+{
1454
+ if (!hwp_active)
1455
+ return;
1456
+
1457
+ sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1458
+}
1459
+
11281460 /************************** sysfs end ************************/
11291461
11301462 static void intel_pstate_hwp_enable(struct cpudata *cpudata)
11311463 {
11321464 /* First disable HWP notification interrupt as we don't process them */
1133
- if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1465
+ if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
11341466 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
11351467
11361468 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
1137
- cpudata->epp_policy = 0;
11381469 if (cpudata->epp_default == -EINVAL)
11391470 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
- }
11591471 }
11601472
11611473 static int atom_get_min_pstate(void)
....@@ -1383,12 +1695,6 @@
13831695 return ret;
13841696 }
13851697
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
-
13921698 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
13931699 {
13941700 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
....@@ -1409,11 +1715,9 @@
14091715
14101716 static void intel_pstate_max_within_limits(struct cpudata *cpu)
14111717 {
1412
- int pstate;
1718
+ int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
14131719
14141720 update_turbo_state();
1415
- pstate = intel_pstate_get_base_pstate(cpu);
1416
- pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
14171721 intel_pstate_set_pstate(cpu, pstate);
14181722 }
14191723
....@@ -1427,7 +1731,7 @@
14271731 if (hwp_active && !hwp_mode_bdw) {
14281732 unsigned int phy_max, current_max;
14291733
1430
- intel_pstate_get_hwp_max(cpu->cpu, &phy_max, &current_max);
1734
+ intel_pstate_get_hwp_max(cpu, &phy_max, &current_max);
14311735 cpu->pstate.turbo_freq = phy_max * cpu->pstate.scaling;
14321736 cpu->pstate.turbo_pstate = phy_max;
14331737 cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(READ_ONCE(cpu->hwp_cap_cached));
....@@ -1619,17 +1923,14 @@
16191923 static inline int32_t get_target_pstate(struct cpudata *cpu)
16201924 {
16211925 struct sample *sample = &cpu->sample;
1622
- int32_t busy_frac, boost;
1926
+ int32_t busy_frac;
16231927 int target, avg_pstate;
16241928
16251929 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
16261930 sample->tsc);
16271931
1628
- boost = cpu->iowait_boost;
1629
- cpu->iowait_boost >>= 1;
1630
-
1631
- if (busy_frac < boost)
1632
- busy_frac = boost;
1932
+ if (busy_frac < cpu->iowait_boost)
1933
+ busy_frac = cpu->iowait_boost;
16331934
16341935 sample->busy_scaled = busy_frac * 100;
16351936
....@@ -1656,11 +1957,9 @@
16561957
16571958 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
16581959 {
1659
- int max_pstate = intel_pstate_get_base_pstate(cpu);
1660
- int min_pstate;
1960
+ int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1961
+ int max_pstate = max(min_pstate, cpu->max_perf_ratio);
16611962
1662
- min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1663
- max_pstate = max(min_pstate, cpu->max_perf_ratio);
16641963 return clamp_t(int, pstate, min_pstate, max_pstate);
16651964 }
16661965
....@@ -1708,29 +2007,30 @@
17082007 if (smp_processor_id() != cpu->cpu)
17092008 return;
17102009
2010
+ delta_ns = time - cpu->last_update;
17112011 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;
2012
+ /* Start over if the CPU may have been idle. */
2013
+ if (delta_ns > TICK_NSEC) {
2014
+ cpu->iowait_boost = ONE_EIGHTH_FP;
2015
+ } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
2016
+ cpu->iowait_boost <<= 1;
2017
+ if (cpu->iowait_boost > int_tofp(1))
2018
+ cpu->iowait_boost = int_tofp(1);
2019
+ } else {
2020
+ cpu->iowait_boost = ONE_EIGHTH_FP;
2021
+ }
17222022 } else if (cpu->iowait_boost) {
17232023 /* Clear iowait_boost if the CPU may have been idle. */
1724
- delta_ns = time - cpu->last_update;
17252024 if (delta_ns > TICK_NSEC)
17262025 cpu->iowait_boost = 0;
2026
+ else
2027
+ cpu->iowait_boost >>= 1;
17272028 }
17282029 cpu->last_update = time;
17292030 delta_ns = time - cpu->sample.time;
17302031 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
17312032 return;
17322033
1733
-set_pstate:
17342034 if (intel_pstate_sample(cpu, time))
17352035 intel_pstate_adjust_pstate(cpu);
17362036 }
....@@ -1774,51 +2074,51 @@
17742074 .get_val = core_get_val,
17752075 };
17762076
1777
-#define ICPU(model, policy) \
1778
- { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1779
- (unsigned long)&policy }
2077
+#define X86_MATCH(model, policy) \
2078
+ X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
2079
+ X86_FEATURE_APERFMPERF, &policy)
17802080
17812081 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),
2082
+ X86_MATCH(SANDYBRIDGE, core_funcs),
2083
+ X86_MATCH(SANDYBRIDGE_X, core_funcs),
2084
+ X86_MATCH(ATOM_SILVERMONT, silvermont_funcs),
2085
+ X86_MATCH(IVYBRIDGE, core_funcs),
2086
+ X86_MATCH(HASWELL, core_funcs),
2087
+ X86_MATCH(BROADWELL, core_funcs),
2088
+ X86_MATCH(IVYBRIDGE_X, core_funcs),
2089
+ X86_MATCH(HASWELL_X, core_funcs),
2090
+ X86_MATCH(HASWELL_L, core_funcs),
2091
+ X86_MATCH(HASWELL_G, core_funcs),
2092
+ X86_MATCH(BROADWELL_G, core_funcs),
2093
+ X86_MATCH(ATOM_AIRMONT, airmont_funcs),
2094
+ X86_MATCH(SKYLAKE_L, core_funcs),
2095
+ X86_MATCH(BROADWELL_X, core_funcs),
2096
+ X86_MATCH(SKYLAKE, core_funcs),
2097
+ X86_MATCH(BROADWELL_D, core_funcs),
2098
+ X86_MATCH(XEON_PHI_KNL, knl_funcs),
2099
+ X86_MATCH(XEON_PHI_KNM, knl_funcs),
2100
+ X86_MATCH(ATOM_GOLDMONT, core_funcs),
2101
+ X86_MATCH(ATOM_GOLDMONT_PLUS, core_funcs),
2102
+ X86_MATCH(SKYLAKE_X, core_funcs),
18032103 {}
18042104 };
18052105 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
18062106
18072107 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),
2108
+ X86_MATCH(BROADWELL_D, core_funcs),
2109
+ X86_MATCH(BROADWELL_X, core_funcs),
2110
+ X86_MATCH(SKYLAKE_X, core_funcs),
18112111 {}
18122112 };
18132113
18142114 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
1815
- ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_funcs),
2115
+ X86_MATCH(KABYLAKE, core_funcs),
18162116 {}
18172117 };
18182118
18192119 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),
2120
+ X86_MATCH(SKYLAKE_X, core_funcs),
2121
+ X86_MATCH(SKYLAKE, core_funcs),
18222122 {}
18232123 };
18242124
....@@ -1835,28 +2135,30 @@
18352135
18362136 all_cpu_data[cpunum] = cpu;
18372137
2138
+ cpu->cpu = cpunum;
2139
+
18382140 cpu->epp_default = -EINVAL;
1839
- cpu->epp_powersave = -EINVAL;
1840
- cpu->epp_saved = -EINVAL;
2141
+
2142
+ if (hwp_active) {
2143
+ const struct x86_cpu_id *id;
2144
+
2145
+ intel_pstate_hwp_enable(cpu);
2146
+
2147
+ id = x86_match_cpu(intel_pstate_hwp_boost_ids);
2148
+ if (id && intel_pstate_acpi_pm_profile_server())
2149
+ hwp_boost = true;
2150
+ }
2151
+ } else if (hwp_active) {
2152
+ /*
2153
+ * Re-enable HWP in case this happens after a resume from ACPI
2154
+ * S3 if the CPU was offline during the whole system/resume
2155
+ * cycle.
2156
+ */
2157
+ intel_pstate_hwp_reenable(cpu);
18412158 }
18422159
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
- }
2160
+ cpu->epp_powersave = -EINVAL;
2161
+ cpu->epp_policy = 0;
18602162
18612163 intel_pstate_get_cpu_pstates(cpu);
18622164
....@@ -1893,7 +2195,7 @@
18932195
18942196 cpufreq_remove_update_util_hook(cpu);
18952197 cpu_data->update_util_set = false;
1896
- synchronize_sched();
2198
+ synchronize_rcu();
18972199 }
18982200
18992201 static int intel_pstate_get_max_freq(struct cpudata *cpu)
....@@ -1902,12 +2204,13 @@
19022204 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
19032205 }
19042206
1905
-static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
1906
- struct cpudata *cpu)
2207
+static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2208
+ unsigned int policy_min,
2209
+ unsigned int policy_max)
19072210 {
1908
- int max_freq = intel_pstate_get_max_freq(cpu);
19092211 int32_t max_policy_perf, min_policy_perf;
19102212 int max_state, turbo_max;
2213
+ int max_freq;
19112214
19122215 /*
19132216 * HWP needs some special consideration, because on BDX the
....@@ -1915,24 +2218,25 @@
19152218 * rather than pure ratios.
19162219 */
19172220 if (hwp_active) {
1918
- intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
2221
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
19192222 } else {
1920
- max_state = intel_pstate_get_base_pstate(cpu);
2223
+ max_state = global.no_turbo || global.turbo_disabled ?
2224
+ cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
19212225 turbo_max = cpu->pstate.turbo_pstate;
19222226 }
2227
+ max_freq = max_state * cpu->pstate.scaling;
19232228
1924
- max_policy_perf = max_state * policy->max / max_freq;
1925
- if (policy->max == policy->min) {
2229
+ max_policy_perf = max_state * policy_max / max_freq;
2230
+ if (policy_max == policy_min) {
19262231 min_policy_perf = max_policy_perf;
19272232 } else {
1928
- min_policy_perf = max_state * policy->min / max_freq;
2233
+ min_policy_perf = max_state * policy_min / max_freq;
19292234 min_policy_perf = clamp_t(int32_t, min_policy_perf,
19302235 0, max_policy_perf);
19312236 }
19322237
19332238 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);
2239
+ cpu->cpu, max_state, min_policy_perf, max_policy_perf);
19362240
19372241 /* Normalize user input to [min_perf, max_perf] */
19382242 if (per_cpu_limits) {
....@@ -1946,7 +2250,7 @@
19462250 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
19472251 global_min = clamp_t(int32_t, global_min, 0, global_max);
19482252
1949
- pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
2253
+ pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
19502254 global_min, global_max);
19512255
19522256 cpu->min_perf_ratio = max(min_policy_perf, global_min);
....@@ -1959,7 +2263,7 @@
19592263 cpu->max_perf_ratio);
19602264
19612265 }
1962
- pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
2266
+ pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
19632267 cpu->max_perf_ratio,
19642268 cpu->min_perf_ratio);
19652269 }
....@@ -1979,7 +2283,7 @@
19792283
19802284 mutex_lock(&intel_pstate_limits_lock);
19812285
1982
- intel_pstate_update_perf_limits(policy, cpu);
2286
+ intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
19832287
19842288 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
19852289 /*
....@@ -2008,8 +2312,8 @@
20082312 return 0;
20092313 }
20102314
2011
-static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
2012
- struct cpudata *cpu)
2315
+static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
2316
+ struct cpufreq_policy_data *policy)
20132317 {
20142318 if (!hwp_active &&
20152319 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
....@@ -2020,42 +2324,87 @@
20202324 }
20212325 }
20222326
2023
-static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
2327
+static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
2328
+ struct cpufreq_policy_data *policy)
20242329 {
2025
- struct cpudata *cpu = all_cpu_data[policy->cpu];
2330
+ int max_freq;
20262331
20272332 update_turbo_state();
2028
- cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2029
- intel_pstate_get_max_freq(cpu));
2333
+ if (hwp_active) {
2334
+ int max_state, turbo_max;
20302335
2031
- if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
2032
- policy->policy != CPUFREQ_POLICY_PERFORMANCE)
2033
- return -EINVAL;
2336
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2337
+ max_freq = max_state * cpu->pstate.scaling;
2338
+ } else {
2339
+ max_freq = intel_pstate_get_max_freq(cpu);
2340
+ }
2341
+ cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
20342342
2035
- intel_pstate_adjust_policy_max(policy, cpu);
2343
+ intel_pstate_adjust_policy_max(cpu, policy);
2344
+}
2345
+
2346
+static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
2347
+{
2348
+ intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
20362349
20372350 return 0;
20382351 }
20392352
2040
-static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
2353
+static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
20412354 {
2042
- intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
2355
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2356
+
2357
+ pr_debug("CPU %d going offline\n", cpu->cpu);
2358
+
2359
+ if (cpu->suspended)
2360
+ return 0;
2361
+
2362
+ /*
2363
+ * If the CPU is an SMT thread and it goes offline with the performance
2364
+ * settings different from the minimum, it will prevent its sibling
2365
+ * from getting to lower performance levels, so force the minimum
2366
+ * performance on CPU offline to prevent that from happening.
2367
+ */
2368
+ if (hwp_active)
2369
+ intel_pstate_hwp_offline(cpu);
2370
+ else
2371
+ intel_pstate_set_min_pstate(cpu);
2372
+
2373
+ intel_pstate_exit_perf_limits(policy);
2374
+
2375
+ return 0;
2376
+}
2377
+
2378
+static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
2379
+{
2380
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2381
+
2382
+ pr_debug("CPU %d going online\n", cpu->cpu);
2383
+
2384
+ intel_pstate_init_acpi_perf_limits(policy);
2385
+
2386
+ if (hwp_active) {
2387
+ /*
2388
+ * Re-enable HWP and clear the "suspended" flag to let "resume"
2389
+ * know that it need not do that.
2390
+ */
2391
+ intel_pstate_hwp_reenable(cpu);
2392
+ cpu->suspended = false;
2393
+ }
2394
+
2395
+ return 0;
20432396 }
20442397
20452398 static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
20462399 {
2047
- pr_debug("CPU %d exiting\n", policy->cpu);
2400
+ pr_debug("CPU %d stopping\n", policy->cpu);
20482401
20492402 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);
20542403 }
20552404
20562405 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
20572406 {
2058
- intel_pstate_exit_perf_limits(policy);
2407
+ pr_debug("CPU %d exiting\n", policy->cpu);
20592408
20602409 policy->fast_switch_possible = false;
20612410
....@@ -2082,6 +2431,7 @@
20822431 /* cpuinfo and default policy values */
20832432 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
20842433 update_turbo_state();
2434
+ global.turbo_disabled_mf = global.turbo_disabled;
20852435 policy->cpuinfo.max_freq = global.turbo_disabled ?
20862436 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
20872437 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
....@@ -2109,10 +2459,17 @@
21092459 if (ret)
21102460 return ret;
21112461
2112
- if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
2113
- policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2114
- else
2115
- policy->policy = CPUFREQ_POLICY_POWERSAVE;
2462
+ /*
2463
+ * Set the policy to powersave to provide a valid fallback value in case
2464
+ * the default cpufreq governor is neither powersave nor performance.
2465
+ */
2466
+ policy->policy = CPUFREQ_POLICY_POWERSAVE;
2467
+
2468
+ if (hwp_active) {
2469
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2470
+
2471
+ cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
2472
+ }
21162473
21172474 return 0;
21182475 }
....@@ -2121,25 +2478,23 @@
21212478 .flags = CPUFREQ_CONST_LOOPS,
21222479 .verify = intel_pstate_verify_policy,
21232480 .setpolicy = intel_pstate_set_policy,
2124
- .suspend = intel_pstate_hwp_save_state,
2481
+ .suspend = intel_pstate_suspend,
21252482 .resume = intel_pstate_resume,
21262483 .init = intel_pstate_cpu_init,
21272484 .exit = intel_pstate_cpu_exit,
21282485 .stop_cpu = intel_pstate_stop_cpu,
2486
+ .offline = intel_pstate_cpu_offline,
2487
+ .online = intel_pstate_cpu_online,
2488
+ .update_limits = intel_pstate_update_limits,
21292489 .name = "intel_pstate",
21302490 };
21312491
2132
-static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2492
+static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
21332493 {
21342494 struct cpudata *cpu = all_cpu_data[policy->cpu];
21352495
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);
2496
+ intel_pstate_verify_cpu_policy(cpu, policy);
2497
+ intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
21432498
21442499 return 0;
21452500 }
....@@ -2182,13 +2537,71 @@
21822537 fp_toint(cpu->iowait_boost * 100));
21832538 }
21842539
2540
+static void intel_cpufreq_adjust_hwp(struct cpudata *cpu, u32 target_pstate,
2541
+ bool strict, bool fast_switch)
2542
+{
2543
+ u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
2544
+
2545
+ value &= ~HWP_MIN_PERF(~0L);
2546
+ value |= HWP_MIN_PERF(target_pstate);
2547
+
2548
+ /*
2549
+ * The entire MSR needs to be updated in order to update the HWP min
2550
+ * field in it, so opportunistically update the max too if needed.
2551
+ */
2552
+ value &= ~HWP_MAX_PERF(~0L);
2553
+ value |= HWP_MAX_PERF(strict ? target_pstate : cpu->max_perf_ratio);
2554
+
2555
+ if (value == prev)
2556
+ return;
2557
+
2558
+ WRITE_ONCE(cpu->hwp_req_cached, value);
2559
+ if (fast_switch)
2560
+ wrmsrl(MSR_HWP_REQUEST, value);
2561
+ else
2562
+ wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
2563
+}
2564
+
2565
+static void intel_cpufreq_adjust_perf_ctl(struct cpudata *cpu,
2566
+ u32 target_pstate, bool fast_switch)
2567
+{
2568
+ if (fast_switch)
2569
+ wrmsrl(MSR_IA32_PERF_CTL,
2570
+ pstate_funcs.get_val(cpu, target_pstate));
2571
+ else
2572
+ wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
2573
+ pstate_funcs.get_val(cpu, target_pstate));
2574
+}
2575
+
2576
+static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
2577
+ int target_pstate, bool fast_switch)
2578
+{
2579
+ struct cpudata *cpu = all_cpu_data[policy->cpu];
2580
+ int old_pstate = cpu->pstate.current_pstate;
2581
+
2582
+ target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2583
+ if (hwp_active) {
2584
+ intel_cpufreq_adjust_hwp(cpu, target_pstate,
2585
+ policy->strict_target, fast_switch);
2586
+ cpu->pstate.current_pstate = target_pstate;
2587
+ } else if (target_pstate != old_pstate) {
2588
+ intel_cpufreq_adjust_perf_ctl(cpu, target_pstate, fast_switch);
2589
+ cpu->pstate.current_pstate = target_pstate;
2590
+ }
2591
+
2592
+ intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
2593
+ INTEL_PSTATE_TRACE_TARGET, old_pstate);
2594
+
2595
+ return target_pstate;
2596
+}
2597
+
21852598 static int intel_cpufreq_target(struct cpufreq_policy *policy,
21862599 unsigned int target_freq,
21872600 unsigned int relation)
21882601 {
21892602 struct cpudata *cpu = all_cpu_data[policy->cpu];
21902603 struct cpufreq_freqs freqs;
2191
- int target_pstate, old_pstate;
2604
+ int target_pstate;
21922605
21932606 update_turbo_state();
21942607
....@@ -2196,6 +2609,7 @@
21962609 freqs.new = target_freq;
21972610
21982611 cpufreq_freq_transition_begin(policy, &freqs);
2612
+
21992613 switch (relation) {
22002614 case CPUFREQ_RELATION_L:
22012615 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
....@@ -2207,15 +2621,11 @@
22072621 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
22082622 break;
22092623 }
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
- }
2624
+
2625
+ target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
2626
+
22172627 freqs.new = target_pstate * cpu->pstate.scaling;
2218
- intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate);
2628
+
22192629 cpufreq_freq_transition_end(policy, &freqs, false);
22202630
22212631 return 0;
....@@ -2225,31 +2635,101 @@
22252635 unsigned int target_freq)
22262636 {
22272637 struct cpudata *cpu = all_cpu_data[policy->cpu];
2228
- int target_pstate, old_pstate;
2638
+ int target_pstate;
22292639
22302640 update_turbo_state();
22312641
22322642 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);
2643
+
2644
+ target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
2645
+
22372646 return target_pstate * cpu->pstate.scaling;
22382647 }
22392648
22402649 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
22412650 {
2242
- int ret = __intel_pstate_cpu_init(policy);
2651
+ int max_state, turbo_max, min_freq, max_freq, ret;
2652
+ struct freq_qos_request *req;
2653
+ struct cpudata *cpu;
2654
+ struct device *dev;
22432655
2656
+ dev = get_cpu_device(policy->cpu);
2657
+ if (!dev)
2658
+ return -ENODEV;
2659
+
2660
+ ret = __intel_pstate_cpu_init(policy);
22442661 if (ret)
22452662 return ret;
22462663
22472664 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2248
- policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
22492665 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
22502666 policy->cur = policy->cpuinfo.min_freq;
22512667
2668
+ req = kcalloc(2, sizeof(*req), GFP_KERNEL);
2669
+ if (!req) {
2670
+ ret = -ENOMEM;
2671
+ goto pstate_exit;
2672
+ }
2673
+
2674
+ cpu = all_cpu_data[policy->cpu];
2675
+
2676
+ if (hwp_active) {
2677
+ u64 value;
2678
+
2679
+ intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2680
+ policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
2681
+ rdmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
2682
+ WRITE_ONCE(cpu->hwp_req_cached, value);
2683
+ cpu->epp_cached = intel_pstate_get_epp(cpu, value);
2684
+ } else {
2685
+ turbo_max = cpu->pstate.turbo_pstate;
2686
+ policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
2687
+ }
2688
+
2689
+ min_freq = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2690
+ min_freq *= cpu->pstate.scaling;
2691
+ max_freq = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2692
+ max_freq *= cpu->pstate.scaling;
2693
+
2694
+ ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
2695
+ min_freq);
2696
+ if (ret < 0) {
2697
+ dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
2698
+ goto free_req;
2699
+ }
2700
+
2701
+ ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
2702
+ max_freq);
2703
+ if (ret < 0) {
2704
+ dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
2705
+ goto remove_min_req;
2706
+ }
2707
+
2708
+ policy->driver_data = req;
2709
+
22522710 return 0;
2711
+
2712
+remove_min_req:
2713
+ freq_qos_remove_request(req);
2714
+free_req:
2715
+ kfree(req);
2716
+pstate_exit:
2717
+ intel_pstate_exit_perf_limits(policy);
2718
+
2719
+ return ret;
2720
+}
2721
+
2722
+static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
2723
+{
2724
+ struct freq_qos_request *req;
2725
+
2726
+ req = policy->driver_data;
2727
+
2728
+ freq_qos_remove_request(req + 1);
2729
+ freq_qos_remove_request(req);
2730
+ kfree(req);
2731
+
2732
+ return intel_pstate_cpu_exit(policy);
22532733 }
22542734
22552735 static struct cpufreq_driver intel_cpufreq = {
....@@ -2258,12 +2738,16 @@
22582738 .target = intel_cpufreq_target,
22592739 .fast_switch = intel_cpufreq_fast_switch,
22602740 .init = intel_cpufreq_cpu_init,
2261
- .exit = intel_pstate_cpu_exit,
2262
- .stop_cpu = intel_cpufreq_stop_cpu,
2741
+ .exit = intel_cpufreq_cpu_exit,
2742
+ .offline = intel_pstate_cpu_offline,
2743
+ .online = intel_pstate_cpu_online,
2744
+ .suspend = intel_pstate_suspend,
2745
+ .resume = intel_pstate_resume,
2746
+ .update_limits = intel_pstate_update_limits,
22632747 .name = "intel_cpufreq",
22642748 };
22652749
2266
-static struct cpufreq_driver *default_driver = &intel_pstate;
2750
+static struct cpufreq_driver *default_driver;
22672751
22682752 static void intel_pstate_driver_cleanup(void)
22692753 {
....@@ -2280,12 +2764,16 @@
22802764 }
22812765 }
22822766 put_online_cpus();
2767
+
22832768 intel_pstate_driver = NULL;
22842769 }
22852770
22862771 static int intel_pstate_register_driver(struct cpufreq_driver *driver)
22872772 {
22882773 int ret;
2774
+
2775
+ if (driver == &intel_pstate)
2776
+ intel_pstate_sysfs_expose_hwp_dynamic_boost();
22892777
22902778 memset(&global, 0, sizeof(global));
22912779 global.max_perf_pct = 100;
....@@ -2302,17 +2790,6 @@
23022790 return 0;
23032791 }
23042792
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
-
23162793 static ssize_t intel_pstate_show_status(char *buf)
23172794 {
23182795 if (!intel_pstate_driver)
....@@ -2324,8 +2801,6 @@
23242801
23252802 static int intel_pstate_update_status(const char *buf, size_t size)
23262803 {
2327
- int ret;
2328
-
23292804 if (size == 3 && !strncmp(buf, "off", size)) {
23302805 if (!intel_pstate_driver)
23312806 return -EINVAL;
....@@ -2333,7 +2808,9 @@
23332808 if (hwp_active)
23342809 return -EBUSY;
23352810
2336
- return intel_pstate_unregister_driver();
2811
+ cpufreq_unregister_driver(intel_pstate_driver);
2812
+ intel_pstate_driver_cleanup();
2813
+ return 0;
23372814 }
23382815
23392816 if (size == 6 && !strncmp(buf, "active", size)) {
....@@ -2341,9 +2818,7 @@
23412818 if (intel_pstate_driver == &intel_pstate)
23422819 return 0;
23432820
2344
- ret = intel_pstate_unregister_driver();
2345
- if (ret)
2346
- return ret;
2821
+ cpufreq_unregister_driver(intel_pstate_driver);
23472822 }
23482823
23492824 return intel_pstate_register_driver(&intel_pstate);
....@@ -2354,9 +2829,8 @@
23542829 if (intel_pstate_driver == &intel_cpufreq)
23552830 return 0;
23562831
2357
- ret = intel_pstate_unregister_driver();
2358
- if (ret)
2359
- return ret;
2832
+ cpufreq_unregister_driver(intel_pstate_driver);
2833
+ intel_pstate_sysfs_hide_hwp_dynamic_boost();
23602834 }
23612835
23622836 return intel_pstate_register_driver(&intel_cpufreq);
....@@ -2420,6 +2894,7 @@
24202894 kfree(pss);
24212895 }
24222896
2897
+ pr_debug("ACPI _PSS not found\n");
24232898 return true;
24242899 }
24252900
....@@ -2430,9 +2905,14 @@
24302905
24312906 status = acpi_get_handle(NULL, "\\_SB", &handle);
24322907 if (ACPI_FAILURE(status))
2433
- return true;
2908
+ goto not_found;
24342909
2435
- return !acpi_has_method(handle, "PCCH");
2910
+ if (acpi_has_method(handle, "PCCH"))
2911
+ return false;
2912
+
2913
+not_found:
2914
+ pr_debug("ACPI PCCH not found\n");
2915
+ return true;
24362916 }
24372917
24382918 static bool __init intel_pstate_has_acpi_ppc(void)
....@@ -2447,6 +2927,7 @@
24472927 if (acpi_has_method(pr->handle, "_PPC"))
24482928 return true;
24492929 }
2930
+ pr_debug("ACPI _PPC not found\n");
24502931 return false;
24512932 }
24522933
....@@ -2457,23 +2938,25 @@
24572938
24582939 /* Hardware vendor-specific info that has its own power management modes */
24592940 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},
2941
+ {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
2942
+ {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2943
+ {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2944
+ {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2945
+ {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2946
+ {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2947
+ {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2948
+ {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2949
+ {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2950
+ {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2951
+ {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2952
+ {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2953
+ {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2954
+ {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2955
+ {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
24752956 { } /* End */
24762957 };
2958
+
2959
+#define BITMASK_OOB (BIT(8) | BIT(18))
24772960
24782961 static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
24792962 {
....@@ -2484,8 +2967,11 @@
24842967 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
24852968 if (id) {
24862969 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2487
- if ( misc_pwr & (1 << 8))
2970
+ if (misc_pwr & BITMASK_OOB) {
2971
+ pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
2972
+ pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
24882973 return true;
2974
+ }
24892975 }
24902976
24912977 idx = acpi_match_platform_list(plat_info);
....@@ -2522,51 +3008,93 @@
25223008
25233009 #define INTEL_PSTATE_HWP_BROADWELL 0x01
25243010
2525
-#define ICPU_HWP(model, hwp_mode) \
2526
- { X86_VENDOR_INTEL, 6, model, X86_FEATURE_HWP, hwp_mode }
3011
+#define X86_MATCH_HWP(model, hwp_mode) \
3012
+ X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
3013
+ X86_FEATURE_HWP, hwp_mode)
25273014
25283015 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),
3016
+ X86_MATCH_HWP(BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
3017
+ X86_MATCH_HWP(BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
3018
+ X86_MATCH_HWP(ANY, 0),
25323019 {}
25333020 };
3021
+
3022
+static bool intel_pstate_hwp_is_enabled(void)
3023
+{
3024
+ u64 value;
3025
+
3026
+ rdmsrl(MSR_PM_ENABLE, value);
3027
+ return !!(value & 0x1);
3028
+}
25343029
25353030 static int __init intel_pstate_init(void)
25363031 {
25373032 const struct x86_cpu_id *id;
25383033 int rc;
25393034
2540
- if (no_load)
3035
+ if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
25413036 return -ENODEV;
25423037
25433038 id = x86_match_cpu(hwp_support_ids);
25443039 if (id) {
3040
+ bool hwp_forced = intel_pstate_hwp_is_enabled();
3041
+
3042
+ if (hwp_forced)
3043
+ pr_info("HWP enabled by BIOS\n");
3044
+ else if (no_load)
3045
+ return -ENODEV;
3046
+
25453047 copy_cpu_funcs(&core_funcs);
2546
- if (!no_hwp) {
3048
+ /*
3049
+ * Avoid enabling HWP for processors without EPP support,
3050
+ * because that means incomplete HWP implementation which is a
3051
+ * corner case and supporting it is generally problematic.
3052
+ *
3053
+ * If HWP is enabled already, though, there is no choice but to
3054
+ * deal with it.
3055
+ */
3056
+ if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
25473057 hwp_active++;
25483058 hwp_mode_bdw = id->driver_data;
25493059 intel_pstate.attr = hwp_cpufreq_attrs;
3060
+ intel_cpufreq.attr = hwp_cpufreq_attrs;
3061
+ intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
3062
+ if (!default_driver)
3063
+ default_driver = &intel_pstate;
3064
+
25503065 goto hwp_cpu_matched;
25513066 }
3067
+ pr_info("HWP not enabled\n");
25523068 } else {
2553
- id = x86_match_cpu(intel_pstate_cpu_ids);
2554
- if (!id)
3069
+ if (no_load)
25553070 return -ENODEV;
3071
+
3072
+ id = x86_match_cpu(intel_pstate_cpu_ids);
3073
+ if (!id) {
3074
+ pr_info("CPU model not supported\n");
3075
+ return -ENODEV;
3076
+ }
25563077
25573078 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
25583079 }
25593080
2560
- if (intel_pstate_msrs_not_valid())
3081
+ if (intel_pstate_msrs_not_valid()) {
3082
+ pr_info("Invalid MSRs\n");
25613083 return -ENODEV;
3084
+ }
3085
+ /* Without HWP start in the passive mode. */
3086
+ if (!default_driver)
3087
+ default_driver = &intel_cpufreq;
25623088
25633089 hwp_cpu_matched:
25643090 /*
25653091 * The Intel pstate driver will be ignored if the platform
25663092 * firmware has its own power management modes.
25673093 */
2568
- if (intel_pstate_platform_pwr_mgmt_exists())
3094
+ if (intel_pstate_platform_pwr_mgmt_exists()) {
3095
+ pr_info("P-states controlled by the platform\n");
25693096 return -ENODEV;
3097
+ }
25703098
25713099 if (!hwp_active && hwp_only)
25723100 return -ENOTSUPP;
....@@ -2584,11 +3112,22 @@
25843112 mutex_lock(&intel_pstate_driver_lock);
25853113 rc = intel_pstate_register_driver(default_driver);
25863114 mutex_unlock(&intel_pstate_driver_lock);
2587
- if (rc)
3115
+ if (rc) {
3116
+ intel_pstate_sysfs_remove();
25883117 return rc;
3118
+ }
25893119
2590
- if (hwp_active)
3120
+ if (hwp_active) {
3121
+ const struct x86_cpu_id *id;
3122
+
3123
+ id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
3124
+ if (id) {
3125
+ set_power_ctl_ee_state(false);
3126
+ pr_info("Disabling energy efficiency optimization\n");
3127
+ }
3128
+
25913129 pr_info("HWP enabled\n");
3130
+ }
25923131
25933132 return 0;
25943133 }
....@@ -2599,17 +3138,16 @@
25993138 if (!str)
26003139 return -EINVAL;
26013140
2602
- if (!strcmp(str, "disable")) {
3141
+ if (!strcmp(str, "disable"))
26033142 no_load = 1;
2604
- } else if (!strcmp(str, "passive")) {
2605
- pr_info("Passive mode enabled\n");
3143
+ else if (!strcmp(str, "active"))
3144
+ default_driver = &intel_pstate;
3145
+ else if (!strcmp(str, "passive"))
26063146 default_driver = &intel_cpufreq;
3147
+
3148
+ if (!strcmp(str, "no_hwp"))
26073149 no_hwp = 1;
2608
- }
2609
- if (!strcmp(str, "no_hwp")) {
2610
- pr_info("HWP disabled\n");
2611
- no_hwp = 1;
2612
- }
3150
+
26133151 if (!strcmp(str, "force"))
26143152 force_load = 1;
26153153 if (!strcmp(str, "hwp_only"))