1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * intel_pstate.c: Native P state management for Intel processors
4 *
5 * (C) Copyright 2012 Intel Corporation
6 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/module.h>
14 #include <linux/ktime.h>
15 #include <linux/hrtimer.h>
16 #include <linux/tick.h>
17 #include <linux/slab.h>
18 #include <linux/sched/cpufreq.h>
19 #include <linux/sched/smt.h>
20 #include <linux/list.h>
21 #include <linux/cpu.h>
22 #include <linux/cpufreq.h>
23 #include <linux/sysfs.h>
24 #include <linux/types.h>
25 #include <linux/fs.h>
26 #include <linux/acpi.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pm_qos.h>
29 #include <linux/bitfield.h>
30 #include <trace/events/power.h>
31 #include <linux/units.h>
32
33 #include <asm/cpu.h>
34 #include <asm/div64.h>
35 #include <asm/msr.h>
36 #include <asm/cpu_device_id.h>
37 #include <asm/cpufeature.h>
38 #include <asm/intel-family.h>
39 #include "../drivers/thermal/intel/thermal_interrupt.h"
40
41 #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC)
42
43 #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
44 #define INTEL_CPUFREQ_TRANSITION_DELAY_HWP 5000
45 #define INTEL_CPUFREQ_TRANSITION_DELAY 500
46
47 #ifdef CONFIG_ACPI
48 #include <acpi/processor.h>
49 #include <acpi/cppc_acpi.h>
50 #endif
51
52 #define FRAC_BITS 8
53 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
54 #define fp_toint(X) ((X) >> FRAC_BITS)
55
56 #define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3))
57
58 #define EXT_BITS 6
59 #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
60 #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
61 #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
62
mul_fp(int32_t x,int32_t y)63 static inline int32_t mul_fp(int32_t x, int32_t y)
64 {
65 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
66 }
67
div_fp(s64 x,s64 y)68 static inline int32_t div_fp(s64 x, s64 y)
69 {
70 return div64_s64((int64_t)x << FRAC_BITS, y);
71 }
72
ceiling_fp(int32_t x)73 static inline int ceiling_fp(int32_t x)
74 {
75 int mask, ret;
76
77 ret = fp_toint(x);
78 mask = (1 << FRAC_BITS) - 1;
79 if (x & mask)
80 ret += 1;
81 return ret;
82 }
83
mul_ext_fp(u64 x,u64 y)84 static inline u64 mul_ext_fp(u64 x, u64 y)
85 {
86 return (x * y) >> EXT_FRAC_BITS;
87 }
88
div_ext_fp(u64 x,u64 y)89 static inline u64 div_ext_fp(u64 x, u64 y)
90 {
91 return div64_u64(x << EXT_FRAC_BITS, y);
92 }
93
94 /**
95 * struct sample - Store performance sample
96 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
97 * performance during last sample period
98 * @busy_scaled: Scaled busy value which is used to calculate next
99 * P state. This can be different than core_avg_perf
100 * to account for cpu idle period
101 * @aperf: Difference of actual performance frequency clock count
102 * read from APERF MSR between last and current sample
103 * @mperf: Difference of maximum performance frequency clock count
104 * read from MPERF MSR between last and current sample
105 * @tsc: Difference of time stamp counter between last and
106 * current sample
107 * @time: Current time from scheduler
108 *
109 * This structure is used in the cpudata structure to store performance sample
110 * data for choosing next P State.
111 */
112 struct sample {
113 int32_t core_avg_perf;
114 int32_t busy_scaled;
115 u64 aperf;
116 u64 mperf;
117 u64 tsc;
118 u64 time;
119 };
120
121 /**
122 * struct pstate_data - Store P state data
123 * @current_pstate: Current requested P state
124 * @min_pstate: Min P state possible for this platform
125 * @max_pstate: Max P state possible for this platform
126 * @max_pstate_physical:This is physical Max P state for a processor
127 * This can be higher than the max_pstate which can
128 * be limited by platform thermal design power limits
129 * @perf_ctl_scaling: PERF_CTL P-state to frequency scaling factor
130 * @scaling: Scaling factor between performance and frequency
131 * @turbo_pstate: Max Turbo P state possible for this platform
132 * @min_freq: @min_pstate frequency in cpufreq units
133 * @max_freq: @max_pstate frequency in cpufreq units
134 * @turbo_freq: @turbo_pstate frequency in cpufreq units
135 *
136 * Stores the per cpu model P state limits and current P state.
137 */
138 struct pstate_data {
139 int current_pstate;
140 int min_pstate;
141 int max_pstate;
142 int max_pstate_physical;
143 int perf_ctl_scaling;
144 int scaling;
145 int turbo_pstate;
146 unsigned int min_freq;
147 unsigned int max_freq;
148 unsigned int turbo_freq;
149 };
150
151 /**
152 * struct vid_data - Stores voltage information data
153 * @min: VID data for this platform corresponding to
154 * the lowest P state
155 * @max: VID data corresponding to the highest P State.
156 * @turbo: VID data for turbo P state
157 * @ratio: Ratio of (vid max - vid min) /
158 * (max P state - Min P State)
159 *
160 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
161 * This data is used in Atom platforms, where in addition to target P state,
162 * the voltage data needs to be specified to select next P State.
163 */
164 struct vid_data {
165 int min;
166 int max;
167 int turbo;
168 int32_t ratio;
169 };
170
171 /**
172 * struct global_params - Global parameters, mostly tunable via sysfs.
173 * @no_turbo: Whether or not to use turbo P-states.
174 * @turbo_disabled: Whether or not turbo P-states are available at all,
175 * based on the MSR_IA32_MISC_ENABLE value and whether or
176 * not the maximum reported turbo P-state is different from
177 * the maximum reported non-turbo one.
178 * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo
179 * P-state capacity.
180 * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo
181 * P-state capacity.
182 */
183 struct global_params {
184 bool no_turbo;
185 bool turbo_disabled;
186 int max_perf_pct;
187 int min_perf_pct;
188 };
189
190 /**
191 * struct cpudata - Per CPU instance data storage
192 * @cpu: CPU number for this instance data
193 * @policy: CPUFreq policy value
194 * @update_util: CPUFreq utility callback information
195 * @update_util_set: CPUFreq utility callback is set
196 * @iowait_boost: iowait-related boost fraction
197 * @last_update: Time of the last update.
198 * @pstate: Stores P state limits for this CPU
199 * @vid: Stores VID limits for this CPU
200 * @last_sample_time: Last Sample time
201 * @aperf_mperf_shift: APERF vs MPERF counting frequency difference
202 * @prev_aperf: Last APERF value read from APERF MSR
203 * @prev_mperf: Last MPERF value read from MPERF MSR
204 * @prev_tsc: Last timestamp counter (TSC) value
205 * @sample: Storage for storing last Sample data
206 * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios
207 * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios
208 * @acpi_perf_data: Stores ACPI perf information read from _PSS
209 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
210 * @epp_powersave: Last saved HWP energy performance preference
211 * (EPP) or energy performance bias (EPB),
212 * when policy switched to performance
213 * @epp_policy: Last saved policy used to set EPP/EPB
214 * @epp_default: Power on default HWP energy performance
215 * preference/bias
216 * @epp_cached: Cached HWP energy-performance preference value
217 * @hwp_req_cached: Cached value of the last HWP Request MSR
218 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
219 * @last_io_update: Last time when IO wake flag was set
220 * @capacity_perf: Highest perf used for scale invariance
221 * @sched_flags: Store scheduler flags for possible cross CPU update
222 * @hwp_boost_min: Last HWP boosted min performance
223 * @suspended: Whether or not the driver has been suspended.
224 * @pd_registered: Set when a perf domain is registered for this CPU.
225 * @hwp_notify_work: workqueue for HWP notifications.
226 *
227 * This structure stores per CPU instance data for all CPUs.
228 */
229 struct cpudata {
230 int cpu;
231
232 unsigned int policy;
233 struct update_util_data update_util;
234 bool update_util_set;
235
236 struct pstate_data pstate;
237 struct vid_data vid;
238
239 u64 last_update;
240 u64 last_sample_time;
241 u64 aperf_mperf_shift;
242 u64 prev_aperf;
243 u64 prev_mperf;
244 u64 prev_tsc;
245 struct sample sample;
246 int32_t min_perf_ratio;
247 int32_t max_perf_ratio;
248 #ifdef CONFIG_ACPI
249 struct acpi_processor_performance acpi_perf_data;
250 bool valid_pss_table;
251 #endif
252 unsigned int iowait_boost;
253 s16 epp_powersave;
254 s16 epp_policy;
255 s16 epp_default;
256 s16 epp_cached;
257 u64 hwp_req_cached;
258 u64 hwp_cap_cached;
259 u64 last_io_update;
260 unsigned int capacity_perf;
261 unsigned int sched_flags;
262 u32 hwp_boost_min;
263 bool suspended;
264 #ifdef CONFIG_ENERGY_MODEL
265 bool pd_registered;
266 #endif
267 struct delayed_work hwp_notify_work;
268 };
269
270 static struct cpudata **all_cpu_data;
271
272 /**
273 * struct pstate_funcs - Per CPU model specific callbacks
274 * @get_max: Callback to get maximum non turbo effective P state
275 * @get_max_physical: Callback to get maximum non turbo physical P state
276 * @get_min: Callback to get minimum P state
277 * @get_turbo: Callback to get turbo P state
278 * @get_scaling: Callback to get frequency scaling factor
279 * @get_cpu_scaling: Get frequency scaling factor for a given cpu
280 * @get_aperf_mperf_shift: Callback to get the APERF vs MPERF frequency difference
281 * @get_val: Callback to convert P state to actual MSR write value
282 * @get_vid: Callback to get VID data for Atom platforms
283 *
284 * Core and Atom CPU models have different way to get P State limits. This
285 * structure is used to store those callbacks.
286 */
287 struct pstate_funcs {
288 int (*get_max)(int cpu);
289 int (*get_max_physical)(int cpu);
290 int (*get_min)(int cpu);
291 int (*get_turbo)(int cpu);
292 int (*get_scaling)(void);
293 int (*get_cpu_scaling)(int cpu);
294 int (*get_aperf_mperf_shift)(void);
295 u64 (*get_val)(struct cpudata*, int pstate);
296 void (*get_vid)(struct cpudata *);
297 };
298
299 static struct pstate_funcs pstate_funcs __read_mostly;
300
301 static bool hwp_active __ro_after_init;
302 static int hwp_mode_bdw __ro_after_init;
303 static bool per_cpu_limits __ro_after_init;
304 static bool hwp_forced __ro_after_init;
305 static bool hwp_boost __read_mostly;
306 static bool hwp_is_hybrid;
307
308 static struct cpufreq_driver *intel_pstate_driver __read_mostly;
309
310 #define INTEL_PSTATE_CORE_SCALING 100000
311 #define HYBRID_SCALING_FACTOR_ADL 78741
312 #define HYBRID_SCALING_FACTOR_MTL 80000
313 #define HYBRID_SCALING_FACTOR_LNL 86957
314
315 static int hybrid_scaling_factor;
316
core_get_scaling(void)317 static inline int core_get_scaling(void)
318 {
319 return INTEL_PSTATE_CORE_SCALING;
320 }
321
322 #ifdef CONFIG_ACPI
323 static bool acpi_ppc;
324 #endif
325
326 static struct global_params global;
327
328 static DEFINE_MUTEX(intel_pstate_driver_lock);
329 static DEFINE_MUTEX(intel_pstate_limits_lock);
330
331 #ifdef CONFIG_ACPI
332
intel_pstate_acpi_pm_profile_server(void)333 static bool intel_pstate_acpi_pm_profile_server(void)
334 {
335 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
336 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
337 return true;
338
339 return false;
340 }
341
intel_pstate_get_ppc_enable_status(void)342 static bool intel_pstate_get_ppc_enable_status(void)
343 {
344 if (intel_pstate_acpi_pm_profile_server())
345 return true;
346
347 return acpi_ppc;
348 }
349
350 #ifdef CONFIG_ACPI_CPPC_LIB
351
352 /* The work item is needed to avoid CPU hotplug locking issues */
intel_pstste_sched_itmt_work_fn(struct work_struct * work)353 static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
354 {
355 sched_set_itmt_support();
356 }
357
358 static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
359
360 #define CPPC_MAX_PERF U8_MAX
361
intel_pstate_set_itmt_prio(int cpu)362 static void intel_pstate_set_itmt_prio(int cpu)
363 {
364 struct cppc_perf_caps cppc_perf;
365 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
366 int ret;
367
368 ret = cppc_get_perf_caps(cpu, &cppc_perf);
369 /*
370 * If CPPC is not available, fall back to MSR_HWP_CAPABILITIES bits [8:0].
371 *
372 * Also, on some systems with overclocking enabled, CPPC.highest_perf is
373 * hardcoded to 0xff, so CPPC.highest_perf cannot be used to enable ITMT.
374 * Fall back to MSR_HWP_CAPABILITIES then too.
375 */
376 if (ret || cppc_perf.highest_perf == CPPC_MAX_PERF)
377 cppc_perf.highest_perf = HWP_HIGHEST_PERF(READ_ONCE(all_cpu_data[cpu]->hwp_cap_cached));
378
379 /*
380 * The priorities can be set regardless of whether or not
381 * sched_set_itmt_support(true) has been called and it is valid to
382 * update them at any time after it has been called.
383 */
384 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
385
386 if (max_highest_perf <= min_highest_perf) {
387 if (cppc_perf.highest_perf > max_highest_perf)
388 max_highest_perf = cppc_perf.highest_perf;
389
390 if (cppc_perf.highest_perf < min_highest_perf)
391 min_highest_perf = cppc_perf.highest_perf;
392
393 if (max_highest_perf > min_highest_perf) {
394 /*
395 * This code can be run during CPU online under the
396 * CPU hotplug locks, so sched_set_itmt_support()
397 * cannot be called from here. Queue up a work item
398 * to invoke it.
399 */
400 schedule_work(&sched_itmt_work);
401 }
402 }
403 }
404
intel_pstate_get_cppc_guaranteed(int cpu)405 static int intel_pstate_get_cppc_guaranteed(int cpu)
406 {
407 struct cppc_perf_caps cppc_perf;
408 int ret;
409
410 ret = cppc_get_perf_caps(cpu, &cppc_perf);
411 if (ret)
412 return ret;
413
414 if (cppc_perf.guaranteed_perf)
415 return cppc_perf.guaranteed_perf;
416
417 return cppc_perf.nominal_perf;
418 }
419
intel_pstate_cppc_get_scaling(int cpu)420 static int intel_pstate_cppc_get_scaling(int cpu)
421 {
422 struct cppc_perf_caps cppc_perf;
423
424 /*
425 * Compute the perf-to-frequency scaling factor for the given CPU if
426 * possible, unless it would be 0.
427 */
428 if (!cppc_get_perf_caps(cpu, &cppc_perf) &&
429 cppc_perf.nominal_perf && cppc_perf.nominal_freq)
430 return div_u64(cppc_perf.nominal_freq * KHZ_PER_MHZ,
431 cppc_perf.nominal_perf);
432
433 return core_get_scaling();
434 }
435
436 #else /* CONFIG_ACPI_CPPC_LIB */
intel_pstate_set_itmt_prio(int cpu)437 static inline void intel_pstate_set_itmt_prio(int cpu)
438 {
439 }
440 #endif /* CONFIG_ACPI_CPPC_LIB */
441
intel_pstate_init_acpi_perf_limits(struct cpufreq_policy * policy)442 static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
443 {
444 struct cpudata *cpu;
445 int ret;
446 int i;
447
448 if (hwp_active) {
449 intel_pstate_set_itmt_prio(policy->cpu);
450 return;
451 }
452
453 if (!intel_pstate_get_ppc_enable_status())
454 return;
455
456 cpu = all_cpu_data[policy->cpu];
457
458 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
459 policy->cpu);
460 if (ret)
461 return;
462
463 /*
464 * Check if the control value in _PSS is for PERF_CTL MSR, which should
465 * guarantee that the states returned by it map to the states in our
466 * list directly.
467 */
468 if (cpu->acpi_perf_data.control_register.space_id !=
469 ACPI_ADR_SPACE_FIXED_HARDWARE)
470 goto err;
471
472 /*
473 * If there is only one entry _PSS, simply ignore _PSS and continue as
474 * usual without taking _PSS into account
475 */
476 if (cpu->acpi_perf_data.state_count < 2)
477 goto err;
478
479 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
480 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
481 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
482 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
483 (u32) cpu->acpi_perf_data.states[i].core_frequency,
484 (u32) cpu->acpi_perf_data.states[i].power,
485 (u32) cpu->acpi_perf_data.states[i].control);
486 }
487
488 cpu->valid_pss_table = true;
489 pr_debug("_PPC limits will be enforced\n");
490
491 return;
492
493 err:
494 cpu->valid_pss_table = false;
495 acpi_processor_unregister_performance(policy->cpu);
496 }
497
intel_pstate_exit_perf_limits(struct cpufreq_policy * policy)498 static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
499 {
500 struct cpudata *cpu;
501
502 cpu = all_cpu_data[policy->cpu];
503 if (!cpu->valid_pss_table)
504 return;
505
506 acpi_processor_unregister_performance(policy->cpu);
507 }
508 #else /* CONFIG_ACPI */
intel_pstate_init_acpi_perf_limits(struct cpufreq_policy * policy)509 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
510 {
511 }
512
intel_pstate_exit_perf_limits(struct cpufreq_policy * policy)513 static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
514 {
515 }
516
intel_pstate_acpi_pm_profile_server(void)517 static inline bool intel_pstate_acpi_pm_profile_server(void)
518 {
519 return false;
520 }
521 #endif /* CONFIG_ACPI */
522
523 #ifndef CONFIG_ACPI_CPPC_LIB
intel_pstate_get_cppc_guaranteed(int cpu)524 static inline int intel_pstate_get_cppc_guaranteed(int cpu)
525 {
526 return -ENOTSUPP;
527 }
528
intel_pstate_cppc_get_scaling(int cpu)529 static int intel_pstate_cppc_get_scaling(int cpu)
530 {
531 return core_get_scaling();
532 }
533 #endif /* CONFIG_ACPI_CPPC_LIB */
534
intel_pstate_freq_to_hwp_rel(struct cpudata * cpu,int freq,unsigned int relation)535 static int intel_pstate_freq_to_hwp_rel(struct cpudata *cpu, int freq,
536 unsigned int relation)
537 {
538 if (freq == cpu->pstate.turbo_freq)
539 return cpu->pstate.turbo_pstate;
540
541 if (freq == cpu->pstate.max_freq)
542 return cpu->pstate.max_pstate;
543
544 switch (relation) {
545 case CPUFREQ_RELATION_H:
546 return freq / cpu->pstate.scaling;
547 case CPUFREQ_RELATION_C:
548 return DIV_ROUND_CLOSEST(freq, cpu->pstate.scaling);
549 }
550
551 return DIV_ROUND_UP(freq, cpu->pstate.scaling);
552 }
553
intel_pstate_freq_to_hwp(struct cpudata * cpu,int freq)554 static int intel_pstate_freq_to_hwp(struct cpudata *cpu, int freq)
555 {
556 return intel_pstate_freq_to_hwp_rel(cpu, freq, CPUFREQ_RELATION_L);
557 }
558
559 /**
560 * intel_pstate_hybrid_hwp_adjust - Calibrate HWP performance levels.
561 * @cpu: Target CPU.
562 *
563 * On hybrid processors, HWP may expose more performance levels than there are
564 * P-states accessible through the PERF_CTL interface. If that happens, the
565 * scaling factor between HWP performance levels and CPU frequency will be less
566 * than the scaling factor between P-state values and CPU frequency.
567 *
568 * In that case, adjust the CPU parameters used in computations accordingly.
569 */
intel_pstate_hybrid_hwp_adjust(struct cpudata * cpu)570 static void intel_pstate_hybrid_hwp_adjust(struct cpudata *cpu)
571 {
572 int perf_ctl_max_phys = cpu->pstate.max_pstate_physical;
573 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
574 int perf_ctl_turbo = pstate_funcs.get_turbo(cpu->cpu);
575 int scaling = cpu->pstate.scaling;
576 int freq;
577
578 pr_debug("CPU%d: perf_ctl_max_phys = %d\n", cpu->cpu, perf_ctl_max_phys);
579 pr_debug("CPU%d: perf_ctl_turbo = %d\n", cpu->cpu, perf_ctl_turbo);
580 pr_debug("CPU%d: perf_ctl_scaling = %d\n", cpu->cpu, perf_ctl_scaling);
581 pr_debug("CPU%d: HWP_CAP guaranteed = %d\n", cpu->cpu, cpu->pstate.max_pstate);
582 pr_debug("CPU%d: HWP_CAP highest = %d\n", cpu->cpu, cpu->pstate.turbo_pstate);
583 pr_debug("CPU%d: HWP-to-frequency scaling factor: %d\n", cpu->cpu, scaling);
584
585 cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_pstate * scaling,
586 perf_ctl_scaling);
587 cpu->pstate.max_freq = rounddown(cpu->pstate.max_pstate * scaling,
588 perf_ctl_scaling);
589
590 freq = perf_ctl_max_phys * perf_ctl_scaling;
591 cpu->pstate.max_pstate_physical = intel_pstate_freq_to_hwp(cpu, freq);
592
593 freq = cpu->pstate.min_pstate * perf_ctl_scaling;
594 cpu->pstate.min_freq = freq;
595 /*
596 * Cast the min P-state value retrieved via pstate_funcs.get_min() to
597 * the effective range of HWP performance levels.
598 */
599 cpu->pstate.min_pstate = intel_pstate_freq_to_hwp(cpu, freq);
600 }
601
turbo_is_disabled(void)602 static bool turbo_is_disabled(void)
603 {
604 u64 misc_en;
605
606 if (!cpu_feature_enabled(X86_FEATURE_IDA))
607 return true;
608
609 rdmsrq(MSR_IA32_MISC_ENABLE, misc_en);
610
611 return !!(misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
612 }
613
min_perf_pct_min(void)614 static int min_perf_pct_min(void)
615 {
616 struct cpudata *cpu = all_cpu_data[0];
617 int turbo_pstate = cpu->pstate.turbo_pstate;
618
619 return turbo_pstate ?
620 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
621 }
622
intel_pstate_get_epb(struct cpudata * cpu_data)623 static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
624 {
625 u64 epb;
626 int ret;
627
628 if (!boot_cpu_has(X86_FEATURE_EPB))
629 return -ENXIO;
630
631 ret = rdmsrq_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
632 if (ret)
633 return (s16)ret;
634
635 return (s16)(epb & 0x0f);
636 }
637
intel_pstate_get_epp(struct cpudata * cpu_data,u64 hwp_req_data)638 static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
639 {
640 s16 epp;
641
642 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
643 /*
644 * When hwp_req_data is 0, means that caller didn't read
645 * MSR_HWP_REQUEST, so need to read and get EPP.
646 */
647 if (!hwp_req_data) {
648 epp = rdmsrq_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
649 &hwp_req_data);
650 if (epp)
651 return epp;
652 }
653 epp = (hwp_req_data >> 24) & 0xff;
654 } else {
655 /* When there is no EPP present, HWP uses EPB settings */
656 epp = intel_pstate_get_epb(cpu_data);
657 }
658
659 return epp;
660 }
661
intel_pstate_set_epb(int cpu,s16 pref)662 static int intel_pstate_set_epb(int cpu, s16 pref)
663 {
664 u64 epb;
665 int ret;
666
667 if (!boot_cpu_has(X86_FEATURE_EPB))
668 return -ENXIO;
669
670 ret = rdmsrq_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
671 if (ret)
672 return ret;
673
674 epb = (epb & ~0x0f) | pref;
675 wrmsrq_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
676
677 return 0;
678 }
679
680 /*
681 * EPP/EPB display strings corresponding to EPP index in the
682 * energy_perf_strings[]
683 * index String
684 *-------------------------------------
685 * 0 default
686 * 1 performance
687 * 2 balance_performance
688 * 3 balance_power
689 * 4 power
690 */
691
692 enum energy_perf_value_index {
693 EPP_INDEX_DEFAULT = 0,
694 EPP_INDEX_PERFORMANCE,
695 EPP_INDEX_BALANCE_PERFORMANCE,
696 EPP_INDEX_BALANCE_POWERSAVE,
697 EPP_INDEX_POWERSAVE,
698 };
699
700 static const char * const energy_perf_strings[] = {
701 [EPP_INDEX_DEFAULT] = "default",
702 [EPP_INDEX_PERFORMANCE] = "performance",
703 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
704 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
705 [EPP_INDEX_POWERSAVE] = "power",
706 NULL
707 };
708 static unsigned int epp_values[] = {
709 [EPP_INDEX_DEFAULT] = 0, /* Unused index */
710 [EPP_INDEX_PERFORMANCE] = HWP_EPP_PERFORMANCE,
711 [EPP_INDEX_BALANCE_PERFORMANCE] = HWP_EPP_BALANCE_PERFORMANCE,
712 [EPP_INDEX_BALANCE_POWERSAVE] = HWP_EPP_BALANCE_POWERSAVE,
713 [EPP_INDEX_POWERSAVE] = HWP_EPP_POWERSAVE,
714 };
715
intel_pstate_get_energy_pref_index(struct cpudata * cpu_data,int * raw_epp)716 static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
717 {
718 s16 epp;
719 int index = -EINVAL;
720
721 *raw_epp = 0;
722 epp = intel_pstate_get_epp(cpu_data, 0);
723 if (epp < 0)
724 return epp;
725
726 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
727 if (epp == epp_values[EPP_INDEX_PERFORMANCE])
728 return EPP_INDEX_PERFORMANCE;
729 if (epp == epp_values[EPP_INDEX_BALANCE_PERFORMANCE])
730 return EPP_INDEX_BALANCE_PERFORMANCE;
731 if (epp == epp_values[EPP_INDEX_BALANCE_POWERSAVE])
732 return EPP_INDEX_BALANCE_POWERSAVE;
733 if (epp == epp_values[EPP_INDEX_POWERSAVE])
734 return EPP_INDEX_POWERSAVE;
735 *raw_epp = epp;
736 return 0;
737 } else if (boot_cpu_has(X86_FEATURE_EPB)) {
738 /*
739 * Range:
740 * 0x00-0x03 : Performance
741 * 0x04-0x07 : Balance performance
742 * 0x08-0x0B : Balance power
743 * 0x0C-0x0F : Power
744 * The EPB is a 4 bit value, but our ranges restrict the
745 * value which can be set. Here only using top two bits
746 * effectively.
747 */
748 index = (epp >> 2) + 1;
749 }
750
751 return index;
752 }
753
intel_pstate_set_epp(struct cpudata * cpu,u32 epp)754 static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp)
755 {
756 int ret;
757
758 /*
759 * Use the cached HWP Request MSR value, because in the active mode the
760 * register itself may be updated by intel_pstate_hwp_boost_up() or
761 * intel_pstate_hwp_boost_down() at any time.
762 */
763 u64 value = READ_ONCE(cpu->hwp_req_cached);
764
765 value &= ~GENMASK_ULL(31, 24);
766 value |= (u64)epp << 24;
767 /*
768 * The only other updater of hwp_req_cached in the active mode,
769 * intel_pstate_hwp_set(), is called under the same lock as this
770 * function, so it cannot run in parallel with the update below.
771 */
772 WRITE_ONCE(cpu->hwp_req_cached, value);
773 ret = wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
774 if (!ret)
775 cpu->epp_cached = epp;
776
777 return ret;
778 }
779
intel_pstate_set_energy_pref_index(struct cpudata * cpu_data,int pref_index,bool use_raw,u32 raw_epp)780 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
781 int pref_index, bool use_raw,
782 u32 raw_epp)
783 {
784 int epp = -EINVAL;
785 int ret;
786
787 if (!pref_index)
788 epp = cpu_data->epp_default;
789
790 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
791 if (use_raw)
792 epp = raw_epp;
793 else if (epp == -EINVAL)
794 epp = epp_values[pref_index];
795
796 /*
797 * To avoid confusion, refuse to set EPP to any values different
798 * from 0 (performance) if the current policy is "performance",
799 * because those values would be overridden.
800 */
801 if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
802 return -EBUSY;
803
804 ret = intel_pstate_set_epp(cpu_data, epp);
805 } else {
806 if (epp == -EINVAL)
807 epp = (pref_index - 1) << 2;
808 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
809 }
810
811 return ret;
812 }
813
show_energy_performance_available_preferences(struct cpufreq_policy * policy,char * buf)814 static ssize_t show_energy_performance_available_preferences(
815 struct cpufreq_policy *policy, char *buf)
816 {
817 int i = 0;
818 int ret = 0;
819
820 while (energy_perf_strings[i] != NULL)
821 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
822
823 ret += sprintf(&buf[ret], "\n");
824
825 return ret;
826 }
827
828 cpufreq_freq_attr_ro(energy_performance_available_preferences);
829
830 static struct cpufreq_driver intel_pstate;
831
store_energy_performance_preference(struct cpufreq_policy * policy,const char * buf,size_t count)832 static ssize_t store_energy_performance_preference(
833 struct cpufreq_policy *policy, const char *buf, size_t count)
834 {
835 struct cpudata *cpu = all_cpu_data[policy->cpu];
836 char str_preference[21];
837 bool raw = false;
838 ssize_t ret;
839 u32 epp = 0;
840
841 ret = sscanf(buf, "%20s", str_preference);
842 if (ret != 1)
843 return -EINVAL;
844
845 ret = match_string(energy_perf_strings, -1, str_preference);
846 if (ret < 0) {
847 if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
848 return ret;
849
850 ret = kstrtouint(buf, 10, &epp);
851 if (ret)
852 return ret;
853
854 if (epp > 255)
855 return -EINVAL;
856
857 raw = true;
858 }
859
860 /*
861 * This function runs with the policy R/W semaphore held, which
862 * guarantees that the driver pointer will not change while it is
863 * running.
864 */
865 if (!intel_pstate_driver)
866 return -EAGAIN;
867
868 mutex_lock(&intel_pstate_limits_lock);
869
870 if (intel_pstate_driver == &intel_pstate) {
871 ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp);
872 } else {
873 /*
874 * In the passive mode the governor needs to be stopped on the
875 * target CPU before the EPP update and restarted after it,
876 * which is super-heavy-weight, so make sure it is worth doing
877 * upfront.
878 */
879 if (!raw)
880 epp = ret ? epp_values[ret] : cpu->epp_default;
881
882 if (cpu->epp_cached != epp) {
883 int err;
884
885 cpufreq_stop_governor(policy);
886 ret = intel_pstate_set_epp(cpu, epp);
887 err = cpufreq_start_governor(policy);
888 if (!ret)
889 ret = err;
890 } else {
891 ret = 0;
892 }
893 }
894
895 mutex_unlock(&intel_pstate_limits_lock);
896
897 return ret ?: count;
898 }
899
show_energy_performance_preference(struct cpufreq_policy * policy,char * buf)900 static ssize_t show_energy_performance_preference(
901 struct cpufreq_policy *policy, char *buf)
902 {
903 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
904 int preference, raw_epp;
905
906 preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
907 if (preference < 0)
908 return preference;
909
910 if (raw_epp)
911 return sprintf(buf, "%d\n", raw_epp);
912 else
913 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
914 }
915
916 cpufreq_freq_attr_rw(energy_performance_preference);
917
show_base_frequency(struct cpufreq_policy * policy,char * buf)918 static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
919 {
920 struct cpudata *cpu = all_cpu_data[policy->cpu];
921 int ratio, freq;
922
923 ratio = intel_pstate_get_cppc_guaranteed(policy->cpu);
924 if (ratio <= 0) {
925 u64 cap;
926
927 rdmsrq_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
928 ratio = HWP_GUARANTEED_PERF(cap);
929 }
930
931 freq = ratio * cpu->pstate.scaling;
932 if (cpu->pstate.scaling != cpu->pstate.perf_ctl_scaling)
933 freq = rounddown(freq, cpu->pstate.perf_ctl_scaling);
934
935 return sprintf(buf, "%d\n", freq);
936 }
937
938 cpufreq_freq_attr_ro(base_frequency);
939
940 static struct freq_attr *hwp_cpufreq_attrs[] = {
941 &energy_performance_preference,
942 &energy_performance_available_preferences,
943 &base_frequency,
944 NULL,
945 };
946
947 static bool no_cas __ro_after_init;
948
949 static struct cpudata *hybrid_max_perf_cpu __read_mostly;
950 /*
951 * Protects hybrid_max_perf_cpu, the capacity_perf fields in struct cpudata,
952 * and the x86 arch scale-invariance information from concurrent updates.
953 */
954 static DEFINE_MUTEX(hybrid_capacity_lock);
955
956 #ifdef CONFIG_ENERGY_MODEL
957 #define HYBRID_EM_STATE_COUNT 4
958
hybrid_active_power(struct device * dev,unsigned long * power,unsigned long * freq)959 static int hybrid_active_power(struct device *dev, unsigned long *power,
960 unsigned long *freq)
961 {
962 /*
963 * Create "utilization bins" of 0-40%, 40%-60%, 60%-80%, and 80%-100%
964 * of the maximum capacity such that two CPUs of the same type will be
965 * regarded as equally attractive if the utilization of each of them
966 * falls into the same bin, which should prevent tasks from being
967 * migrated between them too often.
968 *
969 * For this purpose, return the "frequency" of 2 for the first
970 * performance level and otherwise leave the value set by the caller.
971 */
972 if (!*freq)
973 *freq = 2;
974
975 /* No power information. */
976 *power = EM_MAX_POWER;
977
978 return 0;
979 }
980
hybrid_get_cost(struct device * dev,unsigned long freq,unsigned long * cost)981 static int hybrid_get_cost(struct device *dev, unsigned long freq,
982 unsigned long *cost)
983 {
984 struct pstate_data *pstate = &all_cpu_data[dev->id]->pstate;
985 struct cpu_cacheinfo *cacheinfo = get_cpu_cacheinfo(dev->id);
986
987 /*
988 * The smaller the perf-to-frequency scaling factor, the larger the IPC
989 * ratio between the given CPU and the least capable CPU in the system.
990 * Regard that IPC ratio as the primary cost component and assume that
991 * the scaling factors for different CPU types will differ by at least
992 * 5% and they will not be above INTEL_PSTATE_CORE_SCALING.
993 *
994 * Add the freq value to the cost, so that the cost of running on CPUs
995 * of the same type in different "utilization bins" is different.
996 */
997 *cost = div_u64(100ULL * INTEL_PSTATE_CORE_SCALING, pstate->scaling) + freq;
998 /*
999 * Increase the cost slightly for CPUs able to access L3 to avoid
1000 * touching it in case some other CPUs of the same type can do the work
1001 * without it.
1002 */
1003 if (cacheinfo) {
1004 unsigned int i;
1005
1006 /* Check if L3 cache is there. */
1007 for (i = 0; i < cacheinfo->num_leaves; i++) {
1008 if (cacheinfo->info_list[i].level == 3) {
1009 *cost += 2;
1010 break;
1011 }
1012 }
1013 }
1014
1015 return 0;
1016 }
1017
hybrid_register_perf_domain(unsigned int cpu)1018 static bool hybrid_register_perf_domain(unsigned int cpu)
1019 {
1020 static const struct em_data_callback cb
1021 = EM_ADV_DATA_CB(hybrid_active_power, hybrid_get_cost);
1022 struct cpudata *cpudata = all_cpu_data[cpu];
1023 struct device *cpu_dev;
1024
1025 /*
1026 * Registering EM perf domains without enabling asymmetric CPU capacity
1027 * support is not really useful and one domain should not be registered
1028 * more than once.
1029 */
1030 if (!hybrid_max_perf_cpu || cpudata->pd_registered)
1031 return false;
1032
1033 cpu_dev = get_cpu_device(cpu);
1034 if (!cpu_dev)
1035 return false;
1036
1037 if (em_dev_register_perf_domain(cpu_dev, HYBRID_EM_STATE_COUNT, &cb,
1038 cpumask_of(cpu), false))
1039 return false;
1040
1041 cpudata->pd_registered = true;
1042
1043 return true;
1044 }
1045
hybrid_register_all_perf_domains(void)1046 static void hybrid_register_all_perf_domains(void)
1047 {
1048 unsigned int cpu;
1049
1050 for_each_online_cpu(cpu)
1051 hybrid_register_perf_domain(cpu);
1052 }
1053
hybrid_update_perf_domain(struct cpudata * cpu)1054 static void hybrid_update_perf_domain(struct cpudata *cpu)
1055 {
1056 if (cpu->pd_registered)
1057 em_adjust_cpu_capacity(cpu->cpu);
1058 }
1059 #else /* !CONFIG_ENERGY_MODEL */
hybrid_register_perf_domain(unsigned int cpu)1060 static inline bool hybrid_register_perf_domain(unsigned int cpu) { return false; }
hybrid_register_all_perf_domains(void)1061 static inline void hybrid_register_all_perf_domains(void) {}
hybrid_update_perf_domain(struct cpudata * cpu)1062 static inline void hybrid_update_perf_domain(struct cpudata *cpu) {}
1063 #endif /* CONFIG_ENERGY_MODEL */
1064
hybrid_set_cpu_capacity(struct cpudata * cpu)1065 static void hybrid_set_cpu_capacity(struct cpudata *cpu)
1066 {
1067 arch_set_cpu_capacity(cpu->cpu, cpu->capacity_perf,
1068 hybrid_max_perf_cpu->capacity_perf,
1069 cpu->capacity_perf,
1070 cpu->pstate.max_pstate_physical);
1071 hybrid_update_perf_domain(cpu);
1072
1073 topology_set_cpu_scale(cpu->cpu, arch_scale_cpu_capacity(cpu->cpu));
1074
1075 pr_debug("CPU%d: perf = %u, max. perf = %u, base perf = %d\n", cpu->cpu,
1076 cpu->capacity_perf, hybrid_max_perf_cpu->capacity_perf,
1077 cpu->pstate.max_pstate_physical);
1078 }
1079
hybrid_clear_cpu_capacity(unsigned int cpunum)1080 static void hybrid_clear_cpu_capacity(unsigned int cpunum)
1081 {
1082 arch_set_cpu_capacity(cpunum, 1, 1, 1, 1);
1083 }
1084
hybrid_get_capacity_perf(struct cpudata * cpu)1085 static void hybrid_get_capacity_perf(struct cpudata *cpu)
1086 {
1087 if (READ_ONCE(global.no_turbo)) {
1088 cpu->capacity_perf = cpu->pstate.max_pstate_physical;
1089 return;
1090 }
1091
1092 cpu->capacity_perf = HWP_HIGHEST_PERF(READ_ONCE(cpu->hwp_cap_cached));
1093 }
1094
hybrid_set_capacity_of_cpus(void)1095 static void hybrid_set_capacity_of_cpus(void)
1096 {
1097 int cpunum;
1098
1099 for_each_online_cpu(cpunum) {
1100 struct cpudata *cpu = all_cpu_data[cpunum];
1101
1102 if (cpu)
1103 hybrid_set_cpu_capacity(cpu);
1104 }
1105 }
1106
hybrid_update_cpu_capacity_scaling(void)1107 static void hybrid_update_cpu_capacity_scaling(void)
1108 {
1109 struct cpudata *max_perf_cpu = NULL;
1110 unsigned int max_cap_perf = 0;
1111 int cpunum;
1112
1113 for_each_online_cpu(cpunum) {
1114 struct cpudata *cpu = all_cpu_data[cpunum];
1115
1116 if (!cpu)
1117 continue;
1118
1119 /*
1120 * During initialization, CPU performance at full capacity needs
1121 * to be determined.
1122 */
1123 if (!hybrid_max_perf_cpu)
1124 hybrid_get_capacity_perf(cpu);
1125
1126 /*
1127 * If hybrid_max_perf_cpu is not NULL at this point, it is
1128 * being replaced, so don't take it into account when looking
1129 * for the new one.
1130 */
1131 if (cpu == hybrid_max_perf_cpu)
1132 continue;
1133
1134 if (cpu->capacity_perf > max_cap_perf) {
1135 max_cap_perf = cpu->capacity_perf;
1136 max_perf_cpu = cpu;
1137 }
1138 }
1139
1140 if (max_perf_cpu) {
1141 hybrid_max_perf_cpu = max_perf_cpu;
1142 hybrid_set_capacity_of_cpus();
1143 } else {
1144 pr_info("Found no CPUs with nonzero maximum performance\n");
1145 /* Revert to the flat CPU capacity structure. */
1146 for_each_online_cpu(cpunum)
1147 hybrid_clear_cpu_capacity(cpunum);
1148 }
1149 }
1150
__hybrid_refresh_cpu_capacity_scaling(void)1151 static void __hybrid_refresh_cpu_capacity_scaling(void)
1152 {
1153 hybrid_max_perf_cpu = NULL;
1154 hybrid_update_cpu_capacity_scaling();
1155 }
1156
hybrid_refresh_cpu_capacity_scaling(void)1157 static void hybrid_refresh_cpu_capacity_scaling(void)
1158 {
1159 guard(mutex)(&hybrid_capacity_lock);
1160
1161 __hybrid_refresh_cpu_capacity_scaling();
1162 /*
1163 * Perf domains are not registered before setting hybrid_max_perf_cpu,
1164 * so register them all after setting up CPU capacity scaling.
1165 */
1166 hybrid_register_all_perf_domains();
1167 }
1168
hybrid_init_cpu_capacity_scaling(bool refresh)1169 static void hybrid_init_cpu_capacity_scaling(bool refresh)
1170 {
1171 /* Bail out if enabling capacity-aware scheduling is prohibited. */
1172 if (no_cas)
1173 return;
1174
1175 /*
1176 * If hybrid_max_perf_cpu is set at this point, the hybrid CPU capacity
1177 * scaling has been enabled already and the driver is just changing the
1178 * operation mode.
1179 */
1180 if (refresh) {
1181 hybrid_refresh_cpu_capacity_scaling();
1182 return;
1183 }
1184
1185 /*
1186 * On hybrid systems, use asym capacity instead of ITMT, but because
1187 * the capacity of SMT threads is not deterministic even approximately,
1188 * do not do that when SMT is in use.
1189 */
1190 if (hwp_is_hybrid && !sched_smt_active() && arch_enable_hybrid_capacity_scale()) {
1191 hybrid_refresh_cpu_capacity_scaling();
1192 /*
1193 * Disabling ITMT causes sched domains to be rebuilt to disable asym
1194 * packing and enable asym capacity and EAS.
1195 */
1196 sched_clear_itmt_support();
1197 }
1198 }
1199
hybrid_clear_max_perf_cpu(void)1200 static bool hybrid_clear_max_perf_cpu(void)
1201 {
1202 bool ret;
1203
1204 guard(mutex)(&hybrid_capacity_lock);
1205
1206 ret = !!hybrid_max_perf_cpu;
1207 hybrid_max_perf_cpu = NULL;
1208
1209 return ret;
1210 }
1211
__intel_pstate_get_hwp_cap(struct cpudata * cpu)1212 static void __intel_pstate_get_hwp_cap(struct cpudata *cpu)
1213 {
1214 u64 cap;
1215
1216 rdmsrq_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap);
1217 WRITE_ONCE(cpu->hwp_cap_cached, cap);
1218 cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(cap);
1219 cpu->pstate.turbo_pstate = HWP_HIGHEST_PERF(cap);
1220 }
1221
intel_pstate_get_hwp_cap(struct cpudata * cpu)1222 static void intel_pstate_get_hwp_cap(struct cpudata *cpu)
1223 {
1224 int scaling = cpu->pstate.scaling;
1225
1226 __intel_pstate_get_hwp_cap(cpu);
1227
1228 cpu->pstate.max_freq = cpu->pstate.max_pstate * scaling;
1229 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * scaling;
1230 if (scaling != cpu->pstate.perf_ctl_scaling) {
1231 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
1232
1233 cpu->pstate.max_freq = rounddown(cpu->pstate.max_freq,
1234 perf_ctl_scaling);
1235 cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_freq,
1236 perf_ctl_scaling);
1237 }
1238 }
1239
hybrid_update_capacity(struct cpudata * cpu)1240 static void hybrid_update_capacity(struct cpudata *cpu)
1241 {
1242 unsigned int max_cap_perf;
1243
1244 mutex_lock(&hybrid_capacity_lock);
1245
1246 if (!hybrid_max_perf_cpu)
1247 goto unlock;
1248
1249 /*
1250 * The maximum performance of the CPU may have changed, but assume
1251 * that the performance of the other CPUs has not changed.
1252 */
1253 max_cap_perf = hybrid_max_perf_cpu->capacity_perf;
1254
1255 intel_pstate_get_hwp_cap(cpu);
1256
1257 hybrid_get_capacity_perf(cpu);
1258 /* Should hybrid_max_perf_cpu be replaced by this CPU? */
1259 if (cpu->capacity_perf > max_cap_perf) {
1260 hybrid_max_perf_cpu = cpu;
1261 hybrid_set_capacity_of_cpus();
1262 goto unlock;
1263 }
1264
1265 /* If this CPU is hybrid_max_perf_cpu, should it be replaced? */
1266 if (cpu == hybrid_max_perf_cpu && cpu->capacity_perf < max_cap_perf) {
1267 hybrid_update_cpu_capacity_scaling();
1268 goto unlock;
1269 }
1270
1271 hybrid_set_cpu_capacity(cpu);
1272 /*
1273 * If the CPU was offline to start with and it is going online for the
1274 * first time, a perf domain needs to be registered for it if hybrid
1275 * capacity scaling has been enabled already. In that case, sched
1276 * domains need to be rebuilt to take the new perf domain into account.
1277 */
1278 if (hybrid_register_perf_domain(cpu->cpu))
1279 em_rebuild_sched_domains();
1280
1281 unlock:
1282 mutex_unlock(&hybrid_capacity_lock);
1283 }
1284
intel_pstate_hwp_set(unsigned int cpu)1285 static void intel_pstate_hwp_set(unsigned int cpu)
1286 {
1287 struct cpudata *cpu_data = all_cpu_data[cpu];
1288 int max, min;
1289 u64 value;
1290 s16 epp;
1291
1292 max = cpu_data->max_perf_ratio;
1293 min = cpu_data->min_perf_ratio;
1294
1295 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
1296 min = max;
1297
1298 rdmsrq_on_cpu(cpu, MSR_HWP_REQUEST, &value);
1299
1300 value &= ~HWP_MIN_PERF(~0L);
1301 value |= HWP_MIN_PERF(min);
1302
1303 value &= ~HWP_MAX_PERF(~0L);
1304 value |= HWP_MAX_PERF(max);
1305
1306 if (cpu_data->epp_policy == cpu_data->policy)
1307 goto skip_epp;
1308
1309 cpu_data->epp_policy = cpu_data->policy;
1310
1311 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
1312 epp = intel_pstate_get_epp(cpu_data, value);
1313 cpu_data->epp_powersave = epp;
1314 /* If EPP read was failed, then don't try to write */
1315 if (epp < 0)
1316 goto skip_epp;
1317
1318 epp = 0;
1319 } else {
1320 /* skip setting EPP, when saved value is invalid */
1321 if (cpu_data->epp_powersave < 0)
1322 goto skip_epp;
1323
1324 /*
1325 * No need to restore EPP when it is not zero. This
1326 * means:
1327 * - Policy is not changed
1328 * - user has manually changed
1329 * - Error reading EPB
1330 */
1331 epp = intel_pstate_get_epp(cpu_data, value);
1332 if (epp)
1333 goto skip_epp;
1334
1335 epp = cpu_data->epp_powersave;
1336 }
1337 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
1338 value &= ~GENMASK_ULL(31, 24);
1339 value |= (u64)epp << 24;
1340 } else {
1341 intel_pstate_set_epb(cpu, epp);
1342 }
1343 skip_epp:
1344 WRITE_ONCE(cpu_data->hwp_req_cached, value);
1345 wrmsrq_on_cpu(cpu, MSR_HWP_REQUEST, value);
1346 }
1347
1348 static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata);
1349
intel_pstate_hwp_offline(struct cpudata * cpu)1350 static void intel_pstate_hwp_offline(struct cpudata *cpu)
1351 {
1352 u64 value = READ_ONCE(cpu->hwp_req_cached);
1353 int min_perf;
1354
1355 intel_pstate_disable_hwp_interrupt(cpu);
1356
1357 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
1358 /*
1359 * In case the EPP has been set to "performance" by the
1360 * active mode "performance" scaling algorithm, replace that
1361 * temporary value with the cached EPP one.
1362 */
1363 value &= ~GENMASK_ULL(31, 24);
1364 value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached);
1365 /*
1366 * However, make sure that EPP will be set to "performance" when
1367 * the CPU is brought back online again and the "performance"
1368 * scaling algorithm is still in effect.
1369 */
1370 cpu->epp_policy = CPUFREQ_POLICY_UNKNOWN;
1371 }
1372
1373 /*
1374 * Clear the desired perf field in the cached HWP request value to
1375 * prevent nonzero desired values from being leaked into the active
1376 * mode.
1377 */
1378 value &= ~HWP_DESIRED_PERF(~0L);
1379 WRITE_ONCE(cpu->hwp_req_cached, value);
1380
1381 value &= ~GENMASK_ULL(31, 0);
1382 min_perf = HWP_LOWEST_PERF(READ_ONCE(cpu->hwp_cap_cached));
1383
1384 /* Set hwp_max = hwp_min */
1385 value |= HWP_MAX_PERF(min_perf);
1386 value |= HWP_MIN_PERF(min_perf);
1387
1388 /* Set EPP to min */
1389 if (boot_cpu_has(X86_FEATURE_HWP_EPP))
1390 value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
1391
1392 wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
1393
1394 mutex_lock(&hybrid_capacity_lock);
1395
1396 if (!hybrid_max_perf_cpu) {
1397 mutex_unlock(&hybrid_capacity_lock);
1398
1399 return;
1400 }
1401
1402 if (hybrid_max_perf_cpu == cpu)
1403 hybrid_update_cpu_capacity_scaling();
1404
1405 mutex_unlock(&hybrid_capacity_lock);
1406
1407 /* Reset the capacity of the CPU going offline to the initial value. */
1408 hybrid_clear_cpu_capacity(cpu->cpu);
1409 }
1410
1411 #define POWER_CTL_EE_ENABLE 1
1412 #define POWER_CTL_EE_DISABLE 2
1413
1414 static int power_ctl_ee_state;
1415
set_power_ctl_ee_state(bool input)1416 static void set_power_ctl_ee_state(bool input)
1417 {
1418 u64 power_ctl;
1419
1420 mutex_lock(&intel_pstate_driver_lock);
1421 rdmsrq(MSR_IA32_POWER_CTL, power_ctl);
1422 if (input) {
1423 power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
1424 power_ctl_ee_state = POWER_CTL_EE_ENABLE;
1425 } else {
1426 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1427 power_ctl_ee_state = POWER_CTL_EE_DISABLE;
1428 }
1429 wrmsrq(MSR_IA32_POWER_CTL, power_ctl);
1430 mutex_unlock(&intel_pstate_driver_lock);
1431 }
1432
1433 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
1434
intel_pstate_hwp_reenable(struct cpudata * cpu)1435 static void intel_pstate_hwp_reenable(struct cpudata *cpu)
1436 {
1437 intel_pstate_hwp_enable(cpu);
1438 wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached));
1439 }
1440
intel_pstate_suspend(struct cpufreq_policy * policy)1441 static int intel_pstate_suspend(struct cpufreq_policy *policy)
1442 {
1443 struct cpudata *cpu = all_cpu_data[policy->cpu];
1444
1445 pr_debug("CPU %d suspending\n", cpu->cpu);
1446
1447 cpu->suspended = true;
1448
1449 /* disable HWP interrupt and cancel any pending work */
1450 intel_pstate_disable_hwp_interrupt(cpu);
1451
1452 return 0;
1453 }
1454
intel_pstate_resume(struct cpufreq_policy * policy)1455 static int intel_pstate_resume(struct cpufreq_policy *policy)
1456 {
1457 struct cpudata *cpu = all_cpu_data[policy->cpu];
1458
1459 pr_debug("CPU %d resuming\n", cpu->cpu);
1460
1461 /* Only restore if the system default is changed */
1462 if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
1463 set_power_ctl_ee_state(true);
1464 else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
1465 set_power_ctl_ee_state(false);
1466
1467 if (cpu->suspended && hwp_active) {
1468 mutex_lock(&intel_pstate_limits_lock);
1469
1470 /* Re-enable HWP, because "online" has not done that. */
1471 intel_pstate_hwp_reenable(cpu);
1472
1473 mutex_unlock(&intel_pstate_limits_lock);
1474 }
1475
1476 cpu->suspended = false;
1477
1478 return 0;
1479 }
1480
intel_pstate_update_policies(void)1481 static void intel_pstate_update_policies(void)
1482 {
1483 int cpu;
1484
1485 for_each_possible_cpu(cpu)
1486 cpufreq_update_policy(cpu);
1487 }
1488
__intel_pstate_update_max_freq(struct cpufreq_policy * policy,struct cpudata * cpudata)1489 static void __intel_pstate_update_max_freq(struct cpufreq_policy *policy,
1490 struct cpudata *cpudata)
1491 {
1492 guard(cpufreq_policy_write)(policy);
1493
1494 if (hwp_active)
1495 intel_pstate_get_hwp_cap(cpudata);
1496
1497 policy->cpuinfo.max_freq = READ_ONCE(global.no_turbo) ?
1498 cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
1499
1500 refresh_frequency_limits(policy);
1501 }
1502
intel_pstate_update_max_freq(struct cpudata * cpudata)1503 static bool intel_pstate_update_max_freq(struct cpudata *cpudata)
1504 {
1505 struct cpufreq_policy *policy __free(put_cpufreq_policy);
1506
1507 policy = cpufreq_cpu_get(cpudata->cpu);
1508 if (!policy)
1509 return false;
1510
1511 __intel_pstate_update_max_freq(policy, cpudata);
1512
1513 return true;
1514 }
1515
intel_pstate_update_limits(struct cpufreq_policy * policy)1516 static void intel_pstate_update_limits(struct cpufreq_policy *policy)
1517 {
1518 struct cpudata *cpudata = all_cpu_data[policy->cpu];
1519
1520 __intel_pstate_update_max_freq(policy, cpudata);
1521
1522 hybrid_update_capacity(cpudata);
1523 }
1524
intel_pstate_update_limits_for_all(void)1525 static void intel_pstate_update_limits_for_all(void)
1526 {
1527 int cpu;
1528
1529 for_each_possible_cpu(cpu)
1530 intel_pstate_update_max_freq(all_cpu_data[cpu]);
1531
1532 mutex_lock(&hybrid_capacity_lock);
1533
1534 if (hybrid_max_perf_cpu)
1535 __hybrid_refresh_cpu_capacity_scaling();
1536
1537 mutex_unlock(&hybrid_capacity_lock);
1538 }
1539
1540 /************************** sysfs begin ************************/
1541 #define show_one(file_name, object) \
1542 static ssize_t show_##file_name \
1543 (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
1544 { \
1545 return sprintf(buf, "%u\n", global.object); \
1546 }
1547
1548 static ssize_t intel_pstate_show_status(char *buf);
1549 static int intel_pstate_update_status(const char *buf, size_t size);
1550
show_status(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1551 static ssize_t show_status(struct kobject *kobj,
1552 struct kobj_attribute *attr, char *buf)
1553 {
1554 ssize_t ret;
1555
1556 mutex_lock(&intel_pstate_driver_lock);
1557 ret = intel_pstate_show_status(buf);
1558 mutex_unlock(&intel_pstate_driver_lock);
1559
1560 return ret;
1561 }
1562
store_status(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1563 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
1564 const char *buf, size_t count)
1565 {
1566 char *p = memchr(buf, '\n', count);
1567 int ret;
1568
1569 mutex_lock(&intel_pstate_driver_lock);
1570 ret = intel_pstate_update_status(buf, p ? p - buf : count);
1571 mutex_unlock(&intel_pstate_driver_lock);
1572
1573 return ret < 0 ? ret : count;
1574 }
1575
show_turbo_pct(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1576 static ssize_t show_turbo_pct(struct kobject *kobj,
1577 struct kobj_attribute *attr, char *buf)
1578 {
1579 struct cpudata *cpu;
1580 int total, no_turbo, turbo_pct;
1581 uint32_t turbo_fp;
1582
1583 mutex_lock(&intel_pstate_driver_lock);
1584
1585 if (!intel_pstate_driver) {
1586 mutex_unlock(&intel_pstate_driver_lock);
1587 return -EAGAIN;
1588 }
1589
1590 cpu = all_cpu_data[0];
1591
1592 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1593 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
1594 turbo_fp = div_fp(no_turbo, total);
1595 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
1596
1597 mutex_unlock(&intel_pstate_driver_lock);
1598
1599 return sprintf(buf, "%u\n", turbo_pct);
1600 }
1601
show_num_pstates(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1602 static ssize_t show_num_pstates(struct kobject *kobj,
1603 struct kobj_attribute *attr, char *buf)
1604 {
1605 struct cpudata *cpu;
1606 int total;
1607
1608 mutex_lock(&intel_pstate_driver_lock);
1609
1610 if (!intel_pstate_driver) {
1611 mutex_unlock(&intel_pstate_driver_lock);
1612 return -EAGAIN;
1613 }
1614
1615 cpu = all_cpu_data[0];
1616 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1617
1618 mutex_unlock(&intel_pstate_driver_lock);
1619
1620 return sprintf(buf, "%u\n", total);
1621 }
1622
show_no_turbo(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1623 static ssize_t show_no_turbo(struct kobject *kobj,
1624 struct kobj_attribute *attr, char *buf)
1625 {
1626 ssize_t ret;
1627
1628 mutex_lock(&intel_pstate_driver_lock);
1629
1630 if (!intel_pstate_driver) {
1631 mutex_unlock(&intel_pstate_driver_lock);
1632 return -EAGAIN;
1633 }
1634
1635 ret = sprintf(buf, "%u\n", global.no_turbo);
1636
1637 mutex_unlock(&intel_pstate_driver_lock);
1638
1639 return ret;
1640 }
1641
store_no_turbo(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1642 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
1643 const char *buf, size_t count)
1644 {
1645 unsigned int input;
1646 bool no_turbo;
1647
1648 if (sscanf(buf, "%u", &input) != 1)
1649 return -EINVAL;
1650
1651 mutex_lock(&intel_pstate_driver_lock);
1652
1653 if (!intel_pstate_driver) {
1654 count = -EAGAIN;
1655 goto unlock_driver;
1656 }
1657
1658 no_turbo = !!clamp_t(int, input, 0, 1);
1659
1660 WRITE_ONCE(global.turbo_disabled, turbo_is_disabled());
1661 if (global.turbo_disabled && !no_turbo) {
1662 pr_notice("Turbo disabled by BIOS or unavailable on processor\n");
1663 count = -EPERM;
1664 if (global.no_turbo)
1665 goto unlock_driver;
1666 else
1667 no_turbo = 1;
1668 }
1669
1670 if (no_turbo == global.no_turbo) {
1671 goto unlock_driver;
1672 }
1673
1674 WRITE_ONCE(global.no_turbo, no_turbo);
1675
1676 mutex_lock(&intel_pstate_limits_lock);
1677
1678 if (no_turbo) {
1679 struct cpudata *cpu = all_cpu_data[0];
1680 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
1681
1682 /* Squash the global minimum into the permitted range. */
1683 if (global.min_perf_pct > pct)
1684 global.min_perf_pct = pct;
1685 }
1686
1687 mutex_unlock(&intel_pstate_limits_lock);
1688
1689 intel_pstate_update_limits_for_all();
1690 arch_set_max_freq_ratio(no_turbo);
1691
1692 unlock_driver:
1693 mutex_unlock(&intel_pstate_driver_lock);
1694
1695 return count;
1696 }
1697
update_qos_request(enum freq_qos_req_type type)1698 static void update_qos_request(enum freq_qos_req_type type)
1699 {
1700 struct freq_qos_request *req;
1701 struct cpufreq_policy *policy;
1702 int i;
1703
1704 for_each_possible_cpu(i) {
1705 struct cpudata *cpu = all_cpu_data[i];
1706 unsigned int freq, perf_pct;
1707
1708 policy = cpufreq_cpu_get(i);
1709 if (!policy)
1710 continue;
1711
1712 req = policy->driver_data;
1713 cpufreq_cpu_put(policy);
1714
1715 if (!req)
1716 continue;
1717
1718 if (hwp_active)
1719 intel_pstate_get_hwp_cap(cpu);
1720
1721 if (type == FREQ_QOS_MIN) {
1722 perf_pct = global.min_perf_pct;
1723 } else {
1724 req++;
1725 perf_pct = global.max_perf_pct;
1726 }
1727
1728 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * perf_pct, 100);
1729
1730 if (freq_qos_update_request(req, freq) < 0)
1731 pr_warn("Failed to update freq constraint: CPU%d\n", i);
1732 }
1733 }
1734
store_max_perf_pct(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1735 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
1736 const char *buf, size_t count)
1737 {
1738 unsigned int input;
1739 int ret;
1740
1741 ret = sscanf(buf, "%u", &input);
1742 if (ret != 1)
1743 return -EINVAL;
1744
1745 mutex_lock(&intel_pstate_driver_lock);
1746
1747 if (!intel_pstate_driver) {
1748 mutex_unlock(&intel_pstate_driver_lock);
1749 return -EAGAIN;
1750 }
1751
1752 mutex_lock(&intel_pstate_limits_lock);
1753
1754 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
1755
1756 mutex_unlock(&intel_pstate_limits_lock);
1757
1758 if (intel_pstate_driver == &intel_pstate)
1759 intel_pstate_update_policies();
1760 else
1761 update_qos_request(FREQ_QOS_MAX);
1762
1763 mutex_unlock(&intel_pstate_driver_lock);
1764
1765 return count;
1766 }
1767
store_min_perf_pct(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1768 static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
1769 const char *buf, size_t count)
1770 {
1771 unsigned int input;
1772 int ret;
1773
1774 ret = sscanf(buf, "%u", &input);
1775 if (ret != 1)
1776 return -EINVAL;
1777
1778 mutex_lock(&intel_pstate_driver_lock);
1779
1780 if (!intel_pstate_driver) {
1781 mutex_unlock(&intel_pstate_driver_lock);
1782 return -EAGAIN;
1783 }
1784
1785 mutex_lock(&intel_pstate_limits_lock);
1786
1787 global.min_perf_pct = clamp_t(int, input,
1788 min_perf_pct_min(), global.max_perf_pct);
1789
1790 mutex_unlock(&intel_pstate_limits_lock);
1791
1792 if (intel_pstate_driver == &intel_pstate)
1793 intel_pstate_update_policies();
1794 else
1795 update_qos_request(FREQ_QOS_MIN);
1796
1797 mutex_unlock(&intel_pstate_driver_lock);
1798
1799 return count;
1800 }
1801
show_hwp_dynamic_boost(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1802 static ssize_t show_hwp_dynamic_boost(struct kobject *kobj,
1803 struct kobj_attribute *attr, char *buf)
1804 {
1805 return sprintf(buf, "%u\n", hwp_boost);
1806 }
1807
store_hwp_dynamic_boost(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1808 static ssize_t store_hwp_dynamic_boost(struct kobject *a,
1809 struct kobj_attribute *b,
1810 const char *buf, size_t count)
1811 {
1812 unsigned int input;
1813 int ret;
1814
1815 ret = kstrtouint(buf, 10, &input);
1816 if (ret)
1817 return ret;
1818
1819 mutex_lock(&intel_pstate_driver_lock);
1820 hwp_boost = !!input;
1821 intel_pstate_update_policies();
1822 mutex_unlock(&intel_pstate_driver_lock);
1823
1824 return count;
1825 }
1826
show_energy_efficiency(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1827 static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
1828 char *buf)
1829 {
1830 u64 power_ctl;
1831 int enable;
1832
1833 rdmsrq(MSR_IA32_POWER_CTL, power_ctl);
1834 enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
1835 return sprintf(buf, "%d\n", !enable);
1836 }
1837
store_energy_efficiency(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1838 static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
1839 const char *buf, size_t count)
1840 {
1841 bool input;
1842 int ret;
1843
1844 ret = kstrtobool(buf, &input);
1845 if (ret)
1846 return ret;
1847
1848 set_power_ctl_ee_state(input);
1849
1850 return count;
1851 }
1852
1853 show_one(max_perf_pct, max_perf_pct);
1854 show_one(min_perf_pct, min_perf_pct);
1855
1856 define_one_global_rw(status);
1857 define_one_global_rw(no_turbo);
1858 define_one_global_rw(max_perf_pct);
1859 define_one_global_rw(min_perf_pct);
1860 define_one_global_ro(turbo_pct);
1861 define_one_global_ro(num_pstates);
1862 define_one_global_rw(hwp_dynamic_boost);
1863 define_one_global_rw(energy_efficiency);
1864
1865 static struct attribute *intel_pstate_attributes[] = {
1866 &status.attr,
1867 &no_turbo.attr,
1868 NULL
1869 };
1870
1871 static const struct attribute_group intel_pstate_attr_group = {
1872 .attrs = intel_pstate_attributes,
1873 };
1874
1875 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
1876
1877 static struct kobject *intel_pstate_kobject;
1878
intel_pstate_sysfs_expose_params(void)1879 static void __init intel_pstate_sysfs_expose_params(void)
1880 {
1881 struct device *dev_root = bus_get_dev_root(&cpu_subsys);
1882 int rc;
1883
1884 if (dev_root) {
1885 intel_pstate_kobject = kobject_create_and_add("intel_pstate", &dev_root->kobj);
1886 put_device(dev_root);
1887 }
1888 if (WARN_ON(!intel_pstate_kobject))
1889 return;
1890
1891 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
1892 if (WARN_ON(rc))
1893 return;
1894
1895 if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
1896 rc = sysfs_create_file(intel_pstate_kobject, &turbo_pct.attr);
1897 WARN_ON(rc);
1898
1899 rc = sysfs_create_file(intel_pstate_kobject, &num_pstates.attr);
1900 WARN_ON(rc);
1901 }
1902
1903 /*
1904 * If per cpu limits are enforced there are no global limits, so
1905 * return without creating max/min_perf_pct attributes
1906 */
1907 if (per_cpu_limits)
1908 return;
1909
1910 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1911 WARN_ON(rc);
1912
1913 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1914 WARN_ON(rc);
1915
1916 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
1917 rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
1918 WARN_ON(rc);
1919 }
1920 }
1921
intel_pstate_sysfs_remove(void)1922 static void __init intel_pstate_sysfs_remove(void)
1923 {
1924 if (!intel_pstate_kobject)
1925 return;
1926
1927 sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group);
1928
1929 if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
1930 sysfs_remove_file(intel_pstate_kobject, &num_pstates.attr);
1931 sysfs_remove_file(intel_pstate_kobject, &turbo_pct.attr);
1932 }
1933
1934 if (!per_cpu_limits) {
1935 sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr);
1936 sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr);
1937
1938 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids))
1939 sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr);
1940 }
1941
1942 kobject_put(intel_pstate_kobject);
1943 }
1944
intel_pstate_sysfs_expose_hwp_dynamic_boost(void)1945 static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void)
1946 {
1947 int rc;
1948
1949 if (!hwp_active)
1950 return;
1951
1952 rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1953 WARN_ON_ONCE(rc);
1954 }
1955
intel_pstate_sysfs_hide_hwp_dynamic_boost(void)1956 static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void)
1957 {
1958 if (!hwp_active)
1959 return;
1960
1961 sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1962 }
1963
1964 /************************** sysfs end ************************/
1965
intel_pstate_notify_work(struct work_struct * work)1966 static void intel_pstate_notify_work(struct work_struct *work)
1967 {
1968 struct cpudata *cpudata =
1969 container_of(to_delayed_work(work), struct cpudata, hwp_notify_work);
1970
1971 if (intel_pstate_update_max_freq(cpudata)) {
1972 /*
1973 * The driver will not be unregistered while this function is
1974 * running, so update the capacity without acquiring the driver
1975 * lock.
1976 */
1977 hybrid_update_capacity(cpudata);
1978 }
1979
1980 wrmsrq_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0);
1981 }
1982
1983 static DEFINE_RAW_SPINLOCK(hwp_notify_lock);
1984 static cpumask_t hwp_intr_enable_mask;
1985
1986 #define HWP_GUARANTEED_PERF_CHANGE_STATUS BIT(0)
1987 #define HWP_HIGHEST_PERF_CHANGE_STATUS BIT(3)
1988
notify_hwp_interrupt(void)1989 void notify_hwp_interrupt(void)
1990 {
1991 unsigned int this_cpu = smp_processor_id();
1992 u64 value, status_mask;
1993 unsigned long flags;
1994
1995 if (!hwp_active || !cpu_feature_enabled(X86_FEATURE_HWP_NOTIFY))
1996 return;
1997
1998 status_mask = HWP_GUARANTEED_PERF_CHANGE_STATUS;
1999 if (cpu_feature_enabled(X86_FEATURE_HWP_HIGHEST_PERF_CHANGE))
2000 status_mask |= HWP_HIGHEST_PERF_CHANGE_STATUS;
2001
2002 rdmsrq_safe(MSR_HWP_STATUS, &value);
2003 if (!(value & status_mask))
2004 return;
2005
2006 raw_spin_lock_irqsave(&hwp_notify_lock, flags);
2007
2008 if (!cpumask_test_cpu(this_cpu, &hwp_intr_enable_mask))
2009 goto ack_intr;
2010
2011 schedule_delayed_work(&all_cpu_data[this_cpu]->hwp_notify_work,
2012 msecs_to_jiffies(10));
2013
2014 raw_spin_unlock_irqrestore(&hwp_notify_lock, flags);
2015
2016 return;
2017
2018 ack_intr:
2019 wrmsrq_safe(MSR_HWP_STATUS, 0);
2020 raw_spin_unlock_irqrestore(&hwp_notify_lock, flags);
2021 }
2022
intel_pstate_disable_hwp_interrupt(struct cpudata * cpudata)2023 static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata)
2024 {
2025 bool cancel_work;
2026
2027 if (!cpu_feature_enabled(X86_FEATURE_HWP_NOTIFY))
2028 return;
2029
2030 /* wrmsrq_on_cpu has to be outside spinlock as this can result in IPC */
2031 wrmsrq_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
2032
2033 raw_spin_lock_irq(&hwp_notify_lock);
2034 cancel_work = cpumask_test_and_clear_cpu(cpudata->cpu, &hwp_intr_enable_mask);
2035 raw_spin_unlock_irq(&hwp_notify_lock);
2036
2037 if (cancel_work)
2038 cancel_delayed_work_sync(&cpudata->hwp_notify_work);
2039 }
2040
2041 #define HWP_GUARANTEED_PERF_CHANGE_REQ BIT(0)
2042 #define HWP_HIGHEST_PERF_CHANGE_REQ BIT(2)
2043
intel_pstate_enable_hwp_interrupt(struct cpudata * cpudata)2044 static void intel_pstate_enable_hwp_interrupt(struct cpudata *cpudata)
2045 {
2046 /* Enable HWP notification interrupt for performance change */
2047 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) {
2048 u64 interrupt_mask = HWP_GUARANTEED_PERF_CHANGE_REQ;
2049
2050 raw_spin_lock_irq(&hwp_notify_lock);
2051 INIT_DELAYED_WORK(&cpudata->hwp_notify_work, intel_pstate_notify_work);
2052 cpumask_set_cpu(cpudata->cpu, &hwp_intr_enable_mask);
2053 raw_spin_unlock_irq(&hwp_notify_lock);
2054
2055 if (cpu_feature_enabled(X86_FEATURE_HWP_HIGHEST_PERF_CHANGE))
2056 interrupt_mask |= HWP_HIGHEST_PERF_CHANGE_REQ;
2057
2058 /* wrmsrq_on_cpu has to be outside spinlock as this can result in IPC */
2059 wrmsrq_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, interrupt_mask);
2060 wrmsrq_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0);
2061 }
2062 }
2063
intel_pstate_update_epp_defaults(struct cpudata * cpudata)2064 static void intel_pstate_update_epp_defaults(struct cpudata *cpudata)
2065 {
2066 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
2067
2068 /*
2069 * If the EPP is set by firmware, which means that firmware enabled HWP
2070 * - Is equal or less than 0x80 (default balance_perf EPP)
2071 * - But less performance oriented than performance EPP
2072 * then use this as new balance_perf EPP.
2073 */
2074 if (hwp_forced && cpudata->epp_default <= HWP_EPP_BALANCE_PERFORMANCE &&
2075 cpudata->epp_default > HWP_EPP_PERFORMANCE) {
2076 epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = cpudata->epp_default;
2077 return;
2078 }
2079
2080 /*
2081 * If this CPU gen doesn't call for change in balance_perf
2082 * EPP return.
2083 */
2084 if (epp_values[EPP_INDEX_BALANCE_PERFORMANCE] == HWP_EPP_BALANCE_PERFORMANCE)
2085 return;
2086
2087 /*
2088 * Use hard coded value per gen to update the balance_perf
2089 * and default EPP.
2090 */
2091 cpudata->epp_default = epp_values[EPP_INDEX_BALANCE_PERFORMANCE];
2092 intel_pstate_set_epp(cpudata, cpudata->epp_default);
2093 }
2094
intel_pstate_hwp_enable(struct cpudata * cpudata)2095 static void intel_pstate_hwp_enable(struct cpudata *cpudata)
2096 {
2097 /* First disable HWP notification interrupt till we activate again */
2098 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
2099 wrmsrq_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
2100
2101 wrmsrq_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
2102
2103 intel_pstate_enable_hwp_interrupt(cpudata);
2104
2105 if (cpudata->epp_default >= 0)
2106 return;
2107
2108 intel_pstate_update_epp_defaults(cpudata);
2109 }
2110
atom_get_min_pstate(int not_used)2111 static int atom_get_min_pstate(int not_used)
2112 {
2113 u64 value;
2114
2115 rdmsrq(MSR_ATOM_CORE_RATIOS, value);
2116 return (value >> 8) & 0x7F;
2117 }
2118
atom_get_max_pstate(int not_used)2119 static int atom_get_max_pstate(int not_used)
2120 {
2121 u64 value;
2122
2123 rdmsrq(MSR_ATOM_CORE_RATIOS, value);
2124 return (value >> 16) & 0x7F;
2125 }
2126
atom_get_turbo_pstate(int not_used)2127 static int atom_get_turbo_pstate(int not_used)
2128 {
2129 u64 value;
2130
2131 rdmsrq(MSR_ATOM_CORE_TURBO_RATIOS, value);
2132 return value & 0x7F;
2133 }
2134
atom_get_val(struct cpudata * cpudata,int pstate)2135 static u64 atom_get_val(struct cpudata *cpudata, int pstate)
2136 {
2137 u64 val;
2138 int32_t vid_fp;
2139 u32 vid;
2140
2141 val = (u64)pstate << 8;
2142 if (READ_ONCE(global.no_turbo) && !READ_ONCE(global.turbo_disabled))
2143 val |= (u64)1 << 32;
2144
2145 vid_fp = cpudata->vid.min + mul_fp(
2146 int_tofp(pstate - cpudata->pstate.min_pstate),
2147 cpudata->vid.ratio);
2148
2149 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
2150 vid = ceiling_fp(vid_fp);
2151
2152 if (pstate > cpudata->pstate.max_pstate)
2153 vid = cpudata->vid.turbo;
2154
2155 return val | vid;
2156 }
2157
silvermont_get_scaling(void)2158 static int silvermont_get_scaling(void)
2159 {
2160 u64 value;
2161 int i;
2162 /* Defined in Table 35-6 from SDM (Sept 2015) */
2163 static int silvermont_freq_table[] = {
2164 83300, 100000, 133300, 116700, 80000};
2165
2166 rdmsrq(MSR_FSB_FREQ, value);
2167 i = value & 0x7;
2168 WARN_ON(i > 4);
2169
2170 return silvermont_freq_table[i];
2171 }
2172
airmont_get_scaling(void)2173 static int airmont_get_scaling(void)
2174 {
2175 u64 value;
2176 int i;
2177 /* Defined in Table 35-10 from SDM (Sept 2015) */
2178 static int airmont_freq_table[] = {
2179 83300, 100000, 133300, 116700, 80000,
2180 93300, 90000, 88900, 87500};
2181
2182 rdmsrq(MSR_FSB_FREQ, value);
2183 i = value & 0xF;
2184 WARN_ON(i > 8);
2185
2186 return airmont_freq_table[i];
2187 }
2188
atom_get_vid(struct cpudata * cpudata)2189 static void atom_get_vid(struct cpudata *cpudata)
2190 {
2191 u64 value;
2192
2193 rdmsrq(MSR_ATOM_CORE_VIDS, value);
2194 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
2195 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
2196 cpudata->vid.ratio = div_fp(
2197 cpudata->vid.max - cpudata->vid.min,
2198 int_tofp(cpudata->pstate.max_pstate -
2199 cpudata->pstate.min_pstate));
2200
2201 rdmsrq(MSR_ATOM_CORE_TURBO_VIDS, value);
2202 cpudata->vid.turbo = value & 0x7f;
2203 }
2204
core_get_min_pstate(int cpu)2205 static int core_get_min_pstate(int cpu)
2206 {
2207 u64 value;
2208
2209 rdmsrq_on_cpu(cpu, MSR_PLATFORM_INFO, &value);
2210 return (value >> 40) & 0xFF;
2211 }
2212
core_get_max_pstate_physical(int cpu)2213 static int core_get_max_pstate_physical(int cpu)
2214 {
2215 u64 value;
2216
2217 rdmsrq_on_cpu(cpu, MSR_PLATFORM_INFO, &value);
2218 return (value >> 8) & 0xFF;
2219 }
2220
core_get_tdp_ratio(int cpu,u64 plat_info)2221 static int core_get_tdp_ratio(int cpu, u64 plat_info)
2222 {
2223 /* Check how many TDP levels present */
2224 if (plat_info & 0x600000000) {
2225 u64 tdp_ctrl;
2226 u64 tdp_ratio;
2227 int tdp_msr;
2228 int err;
2229
2230 /* Get the TDP level (0, 1, 2) to get ratios */
2231 err = rdmsrq_safe_on_cpu(cpu, MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
2232 if (err)
2233 return err;
2234
2235 /* TDP MSR are continuous starting at 0x648 */
2236 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
2237 err = rdmsrq_safe_on_cpu(cpu, tdp_msr, &tdp_ratio);
2238 if (err)
2239 return err;
2240
2241 /* For level 1 and 2, bits[23:16] contain the ratio */
2242 if (tdp_ctrl & 0x03)
2243 tdp_ratio >>= 16;
2244
2245 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
2246 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
2247
2248 return (int)tdp_ratio;
2249 }
2250
2251 return -ENXIO;
2252 }
2253
core_get_max_pstate(int cpu)2254 static int core_get_max_pstate(int cpu)
2255 {
2256 u64 tar;
2257 u64 plat_info;
2258 int max_pstate;
2259 int tdp_ratio;
2260 int err;
2261
2262 rdmsrq_on_cpu(cpu, MSR_PLATFORM_INFO, &plat_info);
2263 max_pstate = (plat_info >> 8) & 0xFF;
2264
2265 tdp_ratio = core_get_tdp_ratio(cpu, plat_info);
2266 if (tdp_ratio <= 0)
2267 return max_pstate;
2268
2269 if (hwp_active) {
2270 /* Turbo activation ratio is not used on HWP platforms */
2271 return tdp_ratio;
2272 }
2273
2274 err = rdmsrq_safe_on_cpu(cpu, MSR_TURBO_ACTIVATION_RATIO, &tar);
2275 if (!err) {
2276 int tar_levels;
2277
2278 /* Do some sanity checking for safety */
2279 tar_levels = tar & 0xff;
2280 if (tdp_ratio - 1 == tar_levels) {
2281 max_pstate = tar_levels;
2282 pr_debug("max_pstate=TAC %x\n", max_pstate);
2283 }
2284 }
2285
2286 return max_pstate;
2287 }
2288
core_get_turbo_pstate(int cpu)2289 static int core_get_turbo_pstate(int cpu)
2290 {
2291 u64 value;
2292 int nont, ret;
2293
2294 rdmsrq_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value);
2295 nont = core_get_max_pstate(cpu);
2296 ret = (value) & 255;
2297 if (ret <= nont)
2298 ret = nont;
2299 return ret;
2300 }
2301
core_get_val(struct cpudata * cpudata,int pstate)2302 static u64 core_get_val(struct cpudata *cpudata, int pstate)
2303 {
2304 u64 val;
2305
2306 val = (u64)pstate << 8;
2307 if (READ_ONCE(global.no_turbo) && !READ_ONCE(global.turbo_disabled))
2308 val |= (u64)1 << 32;
2309
2310 return val;
2311 }
2312
knl_get_aperf_mperf_shift(void)2313 static int knl_get_aperf_mperf_shift(void)
2314 {
2315 return 10;
2316 }
2317
knl_get_turbo_pstate(int cpu)2318 static int knl_get_turbo_pstate(int cpu)
2319 {
2320 u64 value;
2321 int nont, ret;
2322
2323 rdmsrq_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value);
2324 nont = core_get_max_pstate(cpu);
2325 ret = (((value) >> 8) & 0xFF);
2326 if (ret <= nont)
2327 ret = nont;
2328 return ret;
2329 }
2330
hwp_get_cpu_scaling(int cpu)2331 static int hwp_get_cpu_scaling(int cpu)
2332 {
2333 if (hybrid_scaling_factor) {
2334 struct cpuinfo_x86 *c = &cpu_data(cpu);
2335 u8 cpu_type = c->topo.intel_type;
2336
2337 /*
2338 * Return the hybrid scaling factor for P-cores and use the
2339 * default core scaling for E-cores.
2340 */
2341 if (cpu_type == INTEL_CPU_TYPE_CORE)
2342 return hybrid_scaling_factor;
2343
2344 if (cpu_type == INTEL_CPU_TYPE_ATOM)
2345 return core_get_scaling();
2346 }
2347
2348 /* Use core scaling on non-hybrid systems. */
2349 if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
2350 return core_get_scaling();
2351
2352 /*
2353 * The system is hybrid, but the hybrid scaling factor is not known or
2354 * the CPU type is not one of the above, so use CPPC to compute the
2355 * scaling factor for this CPU.
2356 */
2357 return intel_pstate_cppc_get_scaling(cpu);
2358 }
2359
intel_pstate_set_pstate(struct cpudata * cpu,int pstate)2360 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
2361 {
2362 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
2363 cpu->pstate.current_pstate = pstate;
2364 /*
2365 * Generally, there is no guarantee that this code will always run on
2366 * the CPU being updated, so force the register update to run on the
2367 * right CPU.
2368 */
2369 wrmsrq_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
2370 pstate_funcs.get_val(cpu, pstate));
2371 }
2372
intel_pstate_set_min_pstate(struct cpudata * cpu)2373 static void intel_pstate_set_min_pstate(struct cpudata *cpu)
2374 {
2375 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
2376 }
2377
intel_pstate_get_cpu_pstates(struct cpudata * cpu)2378 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
2379 {
2380 int perf_ctl_max_phys = pstate_funcs.get_max_physical(cpu->cpu);
2381 int perf_ctl_scaling = pstate_funcs.get_scaling();
2382
2383 cpu->pstate.min_pstate = pstate_funcs.get_min(cpu->cpu);
2384 cpu->pstate.max_pstate_physical = perf_ctl_max_phys;
2385 cpu->pstate.perf_ctl_scaling = perf_ctl_scaling;
2386
2387 if (hwp_active && !hwp_mode_bdw) {
2388 __intel_pstate_get_hwp_cap(cpu);
2389
2390 if (pstate_funcs.get_cpu_scaling) {
2391 cpu->pstate.scaling = pstate_funcs.get_cpu_scaling(cpu->cpu);
2392 if (cpu->pstate.scaling != perf_ctl_scaling) {
2393 intel_pstate_hybrid_hwp_adjust(cpu);
2394 hwp_is_hybrid = true;
2395 }
2396 } else {
2397 cpu->pstate.scaling = perf_ctl_scaling;
2398 }
2399 /*
2400 * If the CPU is going online for the first time and it was
2401 * offline initially, asym capacity scaling needs to be updated.
2402 */
2403 hybrid_update_capacity(cpu);
2404 } else {
2405 cpu->pstate.scaling = perf_ctl_scaling;
2406 cpu->pstate.max_pstate = pstate_funcs.get_max(cpu->cpu);
2407 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(cpu->cpu);
2408 }
2409
2410 if (cpu->pstate.scaling == perf_ctl_scaling) {
2411 cpu->pstate.min_freq = cpu->pstate.min_pstate * perf_ctl_scaling;
2412 cpu->pstate.max_freq = cpu->pstate.max_pstate * perf_ctl_scaling;
2413 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * perf_ctl_scaling;
2414 }
2415
2416 if (pstate_funcs.get_aperf_mperf_shift)
2417 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
2418
2419 if (pstate_funcs.get_vid)
2420 pstate_funcs.get_vid(cpu);
2421
2422 intel_pstate_set_min_pstate(cpu);
2423 }
2424
2425 /*
2426 * Long hold time will keep high perf limits for long time,
2427 * which negatively impacts perf/watt for some workloads,
2428 * like specpower. 3ms is based on experiements on some
2429 * workoads.
2430 */
2431 static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
2432
intel_pstate_hwp_boost_up(struct cpudata * cpu)2433 static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
2434 {
2435 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
2436 u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
2437 u32 max_limit = (hwp_req & 0xff00) >> 8;
2438 u32 min_limit = (hwp_req & 0xff);
2439 u32 boost_level1;
2440
2441 /*
2442 * Cases to consider (User changes via sysfs or boot time):
2443 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
2444 * No boost, return.
2445 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
2446 * Should result in one level boost only for P0.
2447 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
2448 * Should result in two level boost:
2449 * (min + p1)/2 and P1.
2450 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
2451 * Should result in three level boost:
2452 * (min + p1)/2, P1 and P0.
2453 */
2454
2455 /* If max and min are equal or already at max, nothing to boost */
2456 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
2457 return;
2458
2459 if (!cpu->hwp_boost_min)
2460 cpu->hwp_boost_min = min_limit;
2461
2462 /* level at half way mark between min and guranteed */
2463 boost_level1 = (HWP_GUARANTEED_PERF(hwp_cap) + min_limit) >> 1;
2464
2465 if (cpu->hwp_boost_min < boost_level1)
2466 cpu->hwp_boost_min = boost_level1;
2467 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(hwp_cap))
2468 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(hwp_cap);
2469 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(hwp_cap) &&
2470 max_limit != HWP_GUARANTEED_PERF(hwp_cap))
2471 cpu->hwp_boost_min = max_limit;
2472 else
2473 return;
2474
2475 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
2476 wrmsrq(MSR_HWP_REQUEST, hwp_req);
2477 cpu->last_update = cpu->sample.time;
2478 }
2479
intel_pstate_hwp_boost_down(struct cpudata * cpu)2480 static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
2481 {
2482 if (cpu->hwp_boost_min) {
2483 bool expired;
2484
2485 /* Check if we are idle for hold time to boost down */
2486 expired = time_after64(cpu->sample.time, cpu->last_update +
2487 hwp_boost_hold_time_ns);
2488 if (expired) {
2489 wrmsrq(MSR_HWP_REQUEST, cpu->hwp_req_cached);
2490 cpu->hwp_boost_min = 0;
2491 }
2492 }
2493 cpu->last_update = cpu->sample.time;
2494 }
2495
intel_pstate_update_util_hwp_local(struct cpudata * cpu,u64 time)2496 static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
2497 u64 time)
2498 {
2499 cpu->sample.time = time;
2500
2501 if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
2502 bool do_io = false;
2503
2504 cpu->sched_flags = 0;
2505 /*
2506 * Set iowait_boost flag and update time. Since IO WAIT flag
2507 * is set all the time, we can't just conclude that there is
2508 * some IO bound activity is scheduled on this CPU with just
2509 * one occurrence. If we receive at least two in two
2510 * consecutive ticks, then we treat as boost candidate.
2511 */
2512 if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
2513 do_io = true;
2514
2515 cpu->last_io_update = time;
2516
2517 if (do_io)
2518 intel_pstate_hwp_boost_up(cpu);
2519
2520 } else {
2521 intel_pstate_hwp_boost_down(cpu);
2522 }
2523 }
2524
intel_pstate_update_util_hwp(struct update_util_data * data,u64 time,unsigned int flags)2525 static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
2526 u64 time, unsigned int flags)
2527 {
2528 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
2529
2530 cpu->sched_flags |= flags;
2531
2532 if (smp_processor_id() == cpu->cpu)
2533 intel_pstate_update_util_hwp_local(cpu, time);
2534 }
2535
intel_pstate_calc_avg_perf(struct cpudata * cpu)2536 static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
2537 {
2538 struct sample *sample = &cpu->sample;
2539
2540 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
2541 }
2542
intel_pstate_sample(struct cpudata * cpu,u64 time)2543 static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
2544 {
2545 u64 aperf, mperf;
2546 unsigned long flags;
2547 u64 tsc;
2548
2549 local_irq_save(flags);
2550 rdmsrq(MSR_IA32_APERF, aperf);
2551 rdmsrq(MSR_IA32_MPERF, mperf);
2552 tsc = rdtsc();
2553 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
2554 local_irq_restore(flags);
2555 return false;
2556 }
2557 local_irq_restore(flags);
2558
2559 cpu->last_sample_time = cpu->sample.time;
2560 cpu->sample.time = time;
2561 cpu->sample.aperf = aperf;
2562 cpu->sample.mperf = mperf;
2563 cpu->sample.tsc = tsc;
2564 cpu->sample.aperf -= cpu->prev_aperf;
2565 cpu->sample.mperf -= cpu->prev_mperf;
2566 cpu->sample.tsc -= cpu->prev_tsc;
2567
2568 cpu->prev_aperf = aperf;
2569 cpu->prev_mperf = mperf;
2570 cpu->prev_tsc = tsc;
2571 /*
2572 * First time this function is invoked in a given cycle, all of the
2573 * previous sample data fields are equal to zero or stale and they must
2574 * be populated with meaningful numbers for things to work, so assume
2575 * that sample.time will always be reset before setting the utilization
2576 * update hook and make the caller skip the sample then.
2577 */
2578 if (cpu->last_sample_time) {
2579 intel_pstate_calc_avg_perf(cpu);
2580 return true;
2581 }
2582 return false;
2583 }
2584
get_avg_frequency(struct cpudata * cpu)2585 static inline int32_t get_avg_frequency(struct cpudata *cpu)
2586 {
2587 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
2588 }
2589
get_avg_pstate(struct cpudata * cpu)2590 static inline int32_t get_avg_pstate(struct cpudata *cpu)
2591 {
2592 return mul_ext_fp(cpu->pstate.max_pstate_physical,
2593 cpu->sample.core_avg_perf);
2594 }
2595
get_target_pstate(struct cpudata * cpu)2596 static inline int32_t get_target_pstate(struct cpudata *cpu)
2597 {
2598 struct sample *sample = &cpu->sample;
2599 int32_t busy_frac;
2600 int target, avg_pstate;
2601
2602 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
2603 sample->tsc);
2604
2605 if (busy_frac < cpu->iowait_boost)
2606 busy_frac = cpu->iowait_boost;
2607
2608 sample->busy_scaled = busy_frac * 100;
2609
2610 target = READ_ONCE(global.no_turbo) ?
2611 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2612 target += target >> 2;
2613 target = mul_fp(target, busy_frac);
2614 if (target < cpu->pstate.min_pstate)
2615 target = cpu->pstate.min_pstate;
2616
2617 /*
2618 * If the average P-state during the previous cycle was higher than the
2619 * current target, add 50% of the difference to the target to reduce
2620 * possible performance oscillations and offset possible performance
2621 * loss related to moving the workload from one CPU to another within
2622 * a package/module.
2623 */
2624 avg_pstate = get_avg_pstate(cpu);
2625 if (avg_pstate > target)
2626 target += (avg_pstate - target) >> 1;
2627
2628 return target;
2629 }
2630
intel_pstate_prepare_request(struct cpudata * cpu,int pstate)2631 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
2632 {
2633 int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
2634 int max_pstate = max(min_pstate, cpu->max_perf_ratio);
2635
2636 return clamp_t(int, pstate, min_pstate, max_pstate);
2637 }
2638
intel_pstate_update_pstate(struct cpudata * cpu,int pstate)2639 static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
2640 {
2641 if (pstate == cpu->pstate.current_pstate)
2642 return;
2643
2644 cpu->pstate.current_pstate = pstate;
2645 wrmsrq(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
2646 }
2647
intel_pstate_adjust_pstate(struct cpudata * cpu)2648 static void intel_pstate_adjust_pstate(struct cpudata *cpu)
2649 {
2650 int from = cpu->pstate.current_pstate;
2651 struct sample *sample;
2652 int target_pstate;
2653
2654 target_pstate = get_target_pstate(cpu);
2655 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2656 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
2657 intel_pstate_update_pstate(cpu, target_pstate);
2658
2659 sample = &cpu->sample;
2660 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
2661 fp_toint(sample->busy_scaled),
2662 from,
2663 cpu->pstate.current_pstate,
2664 sample->mperf,
2665 sample->aperf,
2666 sample->tsc,
2667 get_avg_frequency(cpu),
2668 fp_toint(cpu->iowait_boost * 100));
2669 }
2670
intel_pstate_update_util(struct update_util_data * data,u64 time,unsigned int flags)2671 static void intel_pstate_update_util(struct update_util_data *data, u64 time,
2672 unsigned int flags)
2673 {
2674 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
2675 u64 delta_ns;
2676
2677 /* Don't allow remote callbacks */
2678 if (smp_processor_id() != cpu->cpu)
2679 return;
2680
2681 delta_ns = time - cpu->last_update;
2682 if (flags & SCHED_CPUFREQ_IOWAIT) {
2683 /* Start over if the CPU may have been idle. */
2684 if (delta_ns > TICK_NSEC) {
2685 cpu->iowait_boost = ONE_EIGHTH_FP;
2686 } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
2687 cpu->iowait_boost <<= 1;
2688 if (cpu->iowait_boost > int_tofp(1))
2689 cpu->iowait_boost = int_tofp(1);
2690 } else {
2691 cpu->iowait_boost = ONE_EIGHTH_FP;
2692 }
2693 } else if (cpu->iowait_boost) {
2694 /* Clear iowait_boost if the CPU may have been idle. */
2695 if (delta_ns > TICK_NSEC)
2696 cpu->iowait_boost = 0;
2697 else
2698 cpu->iowait_boost >>= 1;
2699 }
2700 cpu->last_update = time;
2701 delta_ns = time - cpu->sample.time;
2702 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
2703 return;
2704
2705 if (intel_pstate_sample(cpu, time))
2706 intel_pstate_adjust_pstate(cpu);
2707 }
2708
2709 static struct pstate_funcs core_funcs = {
2710 .get_max = core_get_max_pstate,
2711 .get_max_physical = core_get_max_pstate_physical,
2712 .get_min = core_get_min_pstate,
2713 .get_turbo = core_get_turbo_pstate,
2714 .get_scaling = core_get_scaling,
2715 .get_val = core_get_val,
2716 };
2717
2718 static const struct pstate_funcs silvermont_funcs = {
2719 .get_max = atom_get_max_pstate,
2720 .get_max_physical = atom_get_max_pstate,
2721 .get_min = atom_get_min_pstate,
2722 .get_turbo = atom_get_turbo_pstate,
2723 .get_val = atom_get_val,
2724 .get_scaling = silvermont_get_scaling,
2725 .get_vid = atom_get_vid,
2726 };
2727
2728 static const struct pstate_funcs airmont_funcs = {
2729 .get_max = atom_get_max_pstate,
2730 .get_max_physical = atom_get_max_pstate,
2731 .get_min = atom_get_min_pstate,
2732 .get_turbo = atom_get_turbo_pstate,
2733 .get_val = atom_get_val,
2734 .get_scaling = airmont_get_scaling,
2735 .get_vid = atom_get_vid,
2736 };
2737
2738 static const struct pstate_funcs knl_funcs = {
2739 .get_max = core_get_max_pstate,
2740 .get_max_physical = core_get_max_pstate_physical,
2741 .get_min = core_get_min_pstate,
2742 .get_turbo = knl_get_turbo_pstate,
2743 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
2744 .get_scaling = core_get_scaling,
2745 .get_val = core_get_val,
2746 };
2747
2748 #define X86_MATCH(vfm, policy) \
2749 X86_MATCH_VFM_FEATURE(vfm, X86_FEATURE_APERFMPERF, &policy)
2750
2751 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
2752 X86_MATCH(INTEL_SANDYBRIDGE, core_funcs),
2753 X86_MATCH(INTEL_SANDYBRIDGE_X, core_funcs),
2754 X86_MATCH(INTEL_ATOM_SILVERMONT, silvermont_funcs),
2755 X86_MATCH(INTEL_IVYBRIDGE, core_funcs),
2756 X86_MATCH(INTEL_HASWELL, core_funcs),
2757 X86_MATCH(INTEL_BROADWELL, core_funcs),
2758 X86_MATCH(INTEL_IVYBRIDGE_X, core_funcs),
2759 X86_MATCH(INTEL_HASWELL_X, core_funcs),
2760 X86_MATCH(INTEL_HASWELL_L, core_funcs),
2761 X86_MATCH(INTEL_HASWELL_G, core_funcs),
2762 X86_MATCH(INTEL_BROADWELL_G, core_funcs),
2763 X86_MATCH(INTEL_ATOM_AIRMONT, airmont_funcs),
2764 X86_MATCH(INTEL_SKYLAKE_L, core_funcs),
2765 X86_MATCH(INTEL_BROADWELL_X, core_funcs),
2766 X86_MATCH(INTEL_SKYLAKE, core_funcs),
2767 X86_MATCH(INTEL_BROADWELL_D, core_funcs),
2768 X86_MATCH(INTEL_XEON_PHI_KNL, knl_funcs),
2769 X86_MATCH(INTEL_XEON_PHI_KNM, knl_funcs),
2770 X86_MATCH(INTEL_ATOM_GOLDMONT, core_funcs),
2771 X86_MATCH(INTEL_ATOM_GOLDMONT_PLUS, core_funcs),
2772 X86_MATCH(INTEL_SKYLAKE_X, core_funcs),
2773 X86_MATCH(INTEL_COMETLAKE, core_funcs),
2774 X86_MATCH(INTEL_ICELAKE_X, core_funcs),
2775 X86_MATCH(INTEL_TIGERLAKE, core_funcs),
2776 X86_MATCH(INTEL_SAPPHIRERAPIDS_X, core_funcs),
2777 X86_MATCH(INTEL_EMERALDRAPIDS_X, core_funcs),
2778 {}
2779 };
2780 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
2781
2782 #ifdef CONFIG_ACPI
2783 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
2784 X86_MATCH(INTEL_BROADWELL_D, core_funcs),
2785 X86_MATCH(INTEL_BROADWELL_X, core_funcs),
2786 X86_MATCH(INTEL_SKYLAKE_X, core_funcs),
2787 X86_MATCH(INTEL_ICELAKE_X, core_funcs),
2788 X86_MATCH(INTEL_SAPPHIRERAPIDS_X, core_funcs),
2789 X86_MATCH(INTEL_EMERALDRAPIDS_X, core_funcs),
2790 X86_MATCH(INTEL_GRANITERAPIDS_D, core_funcs),
2791 X86_MATCH(INTEL_GRANITERAPIDS_X, core_funcs),
2792 X86_MATCH(INTEL_ATOM_CRESTMONT, core_funcs),
2793 X86_MATCH(INTEL_ATOM_CRESTMONT_X, core_funcs),
2794 {}
2795 };
2796 #endif
2797
2798 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
2799 X86_MATCH(INTEL_KABYLAKE, core_funcs),
2800 {}
2801 };
2802
intel_pstate_init_cpu(unsigned int cpunum)2803 static int intel_pstate_init_cpu(unsigned int cpunum)
2804 {
2805 struct cpudata *cpu;
2806
2807 cpu = all_cpu_data[cpunum];
2808
2809 if (!cpu) {
2810 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
2811 if (!cpu)
2812 return -ENOMEM;
2813
2814 WRITE_ONCE(all_cpu_data[cpunum], cpu);
2815
2816 cpu->cpu = cpunum;
2817
2818 cpu->epp_default = -EINVAL;
2819
2820 if (hwp_active) {
2821 intel_pstate_hwp_enable(cpu);
2822
2823 if (intel_pstate_acpi_pm_profile_server())
2824 hwp_boost = true;
2825 }
2826 } else if (hwp_active) {
2827 /*
2828 * Re-enable HWP in case this happens after a resume from ACPI
2829 * S3 if the CPU was offline during the whole system/resume
2830 * cycle.
2831 */
2832 intel_pstate_hwp_reenable(cpu);
2833 }
2834
2835 cpu->epp_powersave = -EINVAL;
2836 cpu->epp_policy = CPUFREQ_POLICY_UNKNOWN;
2837
2838 intel_pstate_get_cpu_pstates(cpu);
2839
2840 pr_debug("controlling: cpu %d\n", cpunum);
2841
2842 return 0;
2843 }
2844
intel_pstate_set_update_util_hook(unsigned int cpu_num)2845 static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
2846 {
2847 struct cpudata *cpu = all_cpu_data[cpu_num];
2848
2849 if (hwp_active && !hwp_boost)
2850 return;
2851
2852 if (cpu->update_util_set)
2853 return;
2854
2855 /* Prevent intel_pstate_update_util() from using stale data. */
2856 cpu->sample.time = 0;
2857 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
2858 (hwp_active ?
2859 intel_pstate_update_util_hwp :
2860 intel_pstate_update_util));
2861 cpu->update_util_set = true;
2862 }
2863
intel_pstate_clear_update_util_hook(unsigned int cpu)2864 static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2865 {
2866 struct cpudata *cpu_data = all_cpu_data[cpu];
2867
2868 if (!cpu_data->update_util_set)
2869 return;
2870
2871 cpufreq_remove_update_util_hook(cpu);
2872 cpu_data->update_util_set = false;
2873 synchronize_rcu();
2874 }
2875
intel_pstate_get_max_freq(struct cpudata * cpu)2876 static int intel_pstate_get_max_freq(struct cpudata *cpu)
2877 {
2878 return READ_ONCE(global.no_turbo) ?
2879 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2880 }
2881
intel_pstate_update_perf_limits(struct cpudata * cpu,unsigned int policy_min,unsigned int policy_max)2882 static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2883 unsigned int policy_min,
2884 unsigned int policy_max)
2885 {
2886 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
2887 int32_t max_policy_perf, min_policy_perf;
2888
2889 max_policy_perf = policy_max / perf_ctl_scaling;
2890 if (policy_max == policy_min) {
2891 min_policy_perf = max_policy_perf;
2892 } else {
2893 min_policy_perf = policy_min / perf_ctl_scaling;
2894 min_policy_perf = clamp_t(int32_t, min_policy_perf,
2895 0, max_policy_perf);
2896 }
2897
2898 /*
2899 * HWP needs some special consideration, because HWP_REQUEST uses
2900 * abstract values to represent performance rather than pure ratios.
2901 */
2902 if (hwp_active && cpu->pstate.scaling != perf_ctl_scaling) {
2903 int freq;
2904
2905 freq = max_policy_perf * perf_ctl_scaling;
2906 max_policy_perf = intel_pstate_freq_to_hwp(cpu, freq);
2907 freq = min_policy_perf * perf_ctl_scaling;
2908 min_policy_perf = intel_pstate_freq_to_hwp(cpu, freq);
2909 }
2910
2911 pr_debug("cpu:%d min_policy_perf:%d max_policy_perf:%d\n",
2912 cpu->cpu, min_policy_perf, max_policy_perf);
2913
2914 /* Normalize user input to [min_perf, max_perf] */
2915 if (per_cpu_limits) {
2916 cpu->min_perf_ratio = min_policy_perf;
2917 cpu->max_perf_ratio = max_policy_perf;
2918 } else {
2919 int turbo_max = cpu->pstate.turbo_pstate;
2920 int32_t global_min, global_max;
2921
2922 /* Global limits are in percent of the maximum turbo P-state. */
2923 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2924 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2925 global_min = clamp_t(int32_t, global_min, 0, global_max);
2926
2927 pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
2928 global_min, global_max);
2929
2930 cpu->min_perf_ratio = max(min_policy_perf, global_min);
2931 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
2932 cpu->max_perf_ratio = min(max_policy_perf, global_max);
2933 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
2934
2935 /* Make sure min_perf <= max_perf */
2936 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
2937 cpu->max_perf_ratio);
2938
2939 }
2940 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
2941 cpu->max_perf_ratio,
2942 cpu->min_perf_ratio);
2943 }
2944
intel_pstate_set_policy(struct cpufreq_policy * policy)2945 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2946 {
2947 struct cpudata *cpu;
2948
2949 if (!policy->cpuinfo.max_freq)
2950 return -ENODEV;
2951
2952 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2953 policy->cpuinfo.max_freq, policy->max);
2954
2955 cpu = all_cpu_data[policy->cpu];
2956 cpu->policy = policy->policy;
2957
2958 mutex_lock(&intel_pstate_limits_lock);
2959
2960 intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2961
2962 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
2963 int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
2964
2965 /*
2966 * NOHZ_FULL CPUs need this as the governor callback may not
2967 * be invoked on them.
2968 */
2969 intel_pstate_clear_update_util_hook(policy->cpu);
2970 intel_pstate_set_pstate(cpu, pstate);
2971 } else {
2972 intel_pstate_set_update_util_hook(policy->cpu);
2973 }
2974
2975 if (hwp_active) {
2976 /*
2977 * When hwp_boost was active before and dynamically it
2978 * was turned off, in that case we need to clear the
2979 * update util hook.
2980 */
2981 if (!hwp_boost)
2982 intel_pstate_clear_update_util_hook(policy->cpu);
2983 intel_pstate_hwp_set(policy->cpu);
2984 }
2985 /*
2986 * policy->cur is never updated with the intel_pstate driver, but it
2987 * is used as a stale frequency value. So, keep it within limits.
2988 */
2989 policy->cur = policy->min;
2990
2991 mutex_unlock(&intel_pstate_limits_lock);
2992
2993 return 0;
2994 }
2995
intel_pstate_adjust_policy_max(struct cpudata * cpu,struct cpufreq_policy_data * policy)2996 static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
2997 struct cpufreq_policy_data *policy)
2998 {
2999 if (!hwp_active &&
3000 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
3001 policy->max < policy->cpuinfo.max_freq &&
3002 policy->max > cpu->pstate.max_freq) {
3003 pr_debug("policy->max > max non turbo frequency\n");
3004 policy->max = policy->cpuinfo.max_freq;
3005 }
3006 }
3007
intel_pstate_verify_cpu_policy(struct cpudata * cpu,struct cpufreq_policy_data * policy)3008 static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
3009 struct cpufreq_policy_data *policy)
3010 {
3011 int max_freq;
3012
3013 if (hwp_active) {
3014 intel_pstate_get_hwp_cap(cpu);
3015 max_freq = READ_ONCE(global.no_turbo) ?
3016 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
3017 } else {
3018 max_freq = intel_pstate_get_max_freq(cpu);
3019 }
3020 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
3021
3022 intel_pstate_adjust_policy_max(cpu, policy);
3023 }
3024
intel_pstate_verify_policy(struct cpufreq_policy_data * policy)3025 static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
3026 {
3027 intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
3028
3029 return 0;
3030 }
3031
intel_cpufreq_cpu_offline(struct cpufreq_policy * policy)3032 static int intel_cpufreq_cpu_offline(struct cpufreq_policy *policy)
3033 {
3034 struct cpudata *cpu = all_cpu_data[policy->cpu];
3035
3036 pr_debug("CPU %d going offline\n", cpu->cpu);
3037
3038 if (cpu->suspended)
3039 return 0;
3040
3041 /*
3042 * If the CPU is an SMT thread and it goes offline with the performance
3043 * settings different from the minimum, it will prevent its sibling
3044 * from getting to lower performance levels, so force the minimum
3045 * performance on CPU offline to prevent that from happening.
3046 */
3047 if (hwp_active)
3048 intel_pstate_hwp_offline(cpu);
3049 else
3050 intel_pstate_set_min_pstate(cpu);
3051
3052 intel_pstate_exit_perf_limits(policy);
3053
3054 return 0;
3055 }
3056
intel_pstate_cpu_online(struct cpufreq_policy * policy)3057 static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
3058 {
3059 struct cpudata *cpu = all_cpu_data[policy->cpu];
3060
3061 pr_debug("CPU %d going online\n", cpu->cpu);
3062
3063 intel_pstate_init_acpi_perf_limits(policy);
3064
3065 if (hwp_active) {
3066 /*
3067 * Re-enable HWP and clear the "suspended" flag to let "resume"
3068 * know that it need not do that.
3069 */
3070 intel_pstate_hwp_reenable(cpu);
3071 cpu->suspended = false;
3072
3073 hybrid_update_capacity(cpu);
3074 }
3075
3076 return 0;
3077 }
3078
intel_pstate_cpu_offline(struct cpufreq_policy * policy)3079 static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
3080 {
3081 intel_pstate_clear_update_util_hook(policy->cpu);
3082
3083 return intel_cpufreq_cpu_offline(policy);
3084 }
3085
intel_pstate_cpu_exit(struct cpufreq_policy * policy)3086 static void intel_pstate_cpu_exit(struct cpufreq_policy *policy)
3087 {
3088 pr_debug("CPU %d exiting\n", policy->cpu);
3089
3090 policy->fast_switch_possible = false;
3091 }
3092
__intel_pstate_cpu_init(struct cpufreq_policy * policy)3093 static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
3094 {
3095 struct cpudata *cpu;
3096 int rc;
3097
3098 rc = intel_pstate_init_cpu(policy->cpu);
3099 if (rc)
3100 return rc;
3101
3102 cpu = all_cpu_data[policy->cpu];
3103
3104 cpu->max_perf_ratio = 0xFF;
3105 cpu->min_perf_ratio = 0;
3106
3107 /* cpuinfo and default policy values */
3108 policy->cpuinfo.min_freq = cpu->pstate.min_freq;
3109 policy->cpuinfo.max_freq = READ_ONCE(global.no_turbo) ?
3110 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
3111
3112 policy->min = policy->cpuinfo.min_freq;
3113 policy->max = policy->cpuinfo.max_freq;
3114
3115 intel_pstate_init_acpi_perf_limits(policy);
3116
3117 policy->fast_switch_possible = true;
3118
3119 return 0;
3120 }
3121
intel_pstate_cpu_init(struct cpufreq_policy * policy)3122 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
3123 {
3124 int ret = __intel_pstate_cpu_init(policy);
3125
3126 if (ret)
3127 return ret;
3128
3129 /*
3130 * Set the policy to powersave to provide a valid fallback value in case
3131 * the default cpufreq governor is neither powersave nor performance.
3132 */
3133 policy->policy = CPUFREQ_POLICY_POWERSAVE;
3134
3135 if (hwp_active) {
3136 struct cpudata *cpu = all_cpu_data[policy->cpu];
3137
3138 cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
3139 }
3140
3141 return 0;
3142 }
3143
3144 static struct cpufreq_driver intel_pstate = {
3145 .flags = CPUFREQ_CONST_LOOPS,
3146 .verify = intel_pstate_verify_policy,
3147 .setpolicy = intel_pstate_set_policy,
3148 .suspend = intel_pstate_suspend,
3149 .resume = intel_pstate_resume,
3150 .init = intel_pstate_cpu_init,
3151 .exit = intel_pstate_cpu_exit,
3152 .offline = intel_pstate_cpu_offline,
3153 .online = intel_pstate_cpu_online,
3154 .update_limits = intel_pstate_update_limits,
3155 .name = "intel_pstate",
3156 };
3157
intel_cpufreq_verify_policy(struct cpufreq_policy_data * policy)3158 static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
3159 {
3160 struct cpudata *cpu = all_cpu_data[policy->cpu];
3161
3162 intel_pstate_verify_cpu_policy(cpu, policy);
3163 intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
3164
3165 return 0;
3166 }
3167
3168 /* Use of trace in passive mode:
3169 *
3170 * In passive mode the trace core_busy field (also known as the
3171 * performance field, and lablelled as such on the graphs; also known as
3172 * core_avg_perf) is not needed and so is re-assigned to indicate if the
3173 * driver call was via the normal or fast switch path. Various graphs
3174 * output from the intel_pstate_tracer.py utility that include core_busy
3175 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
3176 * so we use 10 to indicate the normal path through the driver, and
3177 * 90 to indicate the fast switch path through the driver.
3178 * The scaled_busy field is not used, and is set to 0.
3179 */
3180
3181 #define INTEL_PSTATE_TRACE_TARGET 10
3182 #define INTEL_PSTATE_TRACE_FAST_SWITCH 90
3183
intel_cpufreq_trace(struct cpudata * cpu,unsigned int trace_type,int old_pstate)3184 static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
3185 {
3186 struct sample *sample;
3187
3188 if (!trace_pstate_sample_enabled())
3189 return;
3190
3191 if (!intel_pstate_sample(cpu, ktime_get()))
3192 return;
3193
3194 sample = &cpu->sample;
3195 trace_pstate_sample(trace_type,
3196 0,
3197 old_pstate,
3198 cpu->pstate.current_pstate,
3199 sample->mperf,
3200 sample->aperf,
3201 sample->tsc,
3202 get_avg_frequency(cpu),
3203 fp_toint(cpu->iowait_boost * 100));
3204 }
3205
intel_cpufreq_hwp_update(struct cpudata * cpu,u32 min,u32 max,u32 desired,bool fast_switch)3206 static void intel_cpufreq_hwp_update(struct cpudata *cpu, u32 min, u32 max,
3207 u32 desired, bool fast_switch)
3208 {
3209 u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
3210
3211 value &= ~HWP_MIN_PERF(~0L);
3212 value |= HWP_MIN_PERF(min);
3213
3214 value &= ~HWP_MAX_PERF(~0L);
3215 value |= HWP_MAX_PERF(max);
3216
3217 value &= ~HWP_DESIRED_PERF(~0L);
3218 value |= HWP_DESIRED_PERF(desired);
3219
3220 if (value == prev)
3221 return;
3222
3223 WRITE_ONCE(cpu->hwp_req_cached, value);
3224 if (fast_switch)
3225 wrmsrq(MSR_HWP_REQUEST, value);
3226 else
3227 wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
3228 }
3229
intel_cpufreq_perf_ctl_update(struct cpudata * cpu,u32 target_pstate,bool fast_switch)3230 static void intel_cpufreq_perf_ctl_update(struct cpudata *cpu,
3231 u32 target_pstate, bool fast_switch)
3232 {
3233 if (fast_switch)
3234 wrmsrq(MSR_IA32_PERF_CTL,
3235 pstate_funcs.get_val(cpu, target_pstate));
3236 else
3237 wrmsrq_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
3238 pstate_funcs.get_val(cpu, target_pstate));
3239 }
3240
intel_cpufreq_update_pstate(struct cpufreq_policy * policy,int target_pstate,bool fast_switch)3241 static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
3242 int target_pstate, bool fast_switch)
3243 {
3244 struct cpudata *cpu = all_cpu_data[policy->cpu];
3245 int old_pstate = cpu->pstate.current_pstate;
3246
3247 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
3248 if (hwp_active) {
3249 int max_pstate = policy->strict_target ?
3250 target_pstate : cpu->max_perf_ratio;
3251
3252 intel_cpufreq_hwp_update(cpu, target_pstate, max_pstate, 0,
3253 fast_switch);
3254 } else if (target_pstate != old_pstate) {
3255 intel_cpufreq_perf_ctl_update(cpu, target_pstate, fast_switch);
3256 }
3257
3258 cpu->pstate.current_pstate = target_pstate;
3259
3260 intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
3261 INTEL_PSTATE_TRACE_TARGET, old_pstate);
3262
3263 return target_pstate;
3264 }
3265
intel_cpufreq_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)3266 static int intel_cpufreq_target(struct cpufreq_policy *policy,
3267 unsigned int target_freq,
3268 unsigned int relation)
3269 {
3270 struct cpudata *cpu = all_cpu_data[policy->cpu];
3271 struct cpufreq_freqs freqs;
3272 int target_pstate;
3273
3274 freqs.old = policy->cur;
3275 freqs.new = target_freq;
3276
3277 cpufreq_freq_transition_begin(policy, &freqs);
3278
3279 target_pstate = intel_pstate_freq_to_hwp_rel(cpu, freqs.new, relation);
3280 target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
3281
3282 freqs.new = target_pstate * cpu->pstate.scaling;
3283
3284 cpufreq_freq_transition_end(policy, &freqs, false);
3285
3286 return 0;
3287 }
3288
intel_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)3289 static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
3290 unsigned int target_freq)
3291 {
3292 struct cpudata *cpu = all_cpu_data[policy->cpu];
3293 int target_pstate;
3294
3295 target_pstate = intel_pstate_freq_to_hwp(cpu, target_freq);
3296
3297 target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
3298
3299 return target_pstate * cpu->pstate.scaling;
3300 }
3301
intel_cpufreq_adjust_perf(unsigned int cpunum,unsigned long min_perf,unsigned long target_perf,unsigned long capacity)3302 static void intel_cpufreq_adjust_perf(unsigned int cpunum,
3303 unsigned long min_perf,
3304 unsigned long target_perf,
3305 unsigned long capacity)
3306 {
3307 struct cpudata *cpu = all_cpu_data[cpunum];
3308 u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
3309 int old_pstate = cpu->pstate.current_pstate;
3310 int cap_pstate, min_pstate, max_pstate, target_pstate;
3311
3312 cap_pstate = READ_ONCE(global.no_turbo) ?
3313 HWP_GUARANTEED_PERF(hwp_cap) :
3314 HWP_HIGHEST_PERF(hwp_cap);
3315
3316 /* Optimization: Avoid unnecessary divisions. */
3317
3318 target_pstate = cap_pstate;
3319 if (target_perf < capacity)
3320 target_pstate = DIV_ROUND_UP(cap_pstate * target_perf, capacity);
3321
3322 min_pstate = cap_pstate;
3323 if (min_perf < capacity)
3324 min_pstate = DIV_ROUND_UP(cap_pstate * min_perf, capacity);
3325
3326 if (min_pstate < cpu->pstate.min_pstate)
3327 min_pstate = cpu->pstate.min_pstate;
3328
3329 if (min_pstate < cpu->min_perf_ratio)
3330 min_pstate = cpu->min_perf_ratio;
3331
3332 if (min_pstate > cpu->max_perf_ratio)
3333 min_pstate = cpu->max_perf_ratio;
3334
3335 max_pstate = min(cap_pstate, cpu->max_perf_ratio);
3336 if (max_pstate < min_pstate)
3337 max_pstate = min_pstate;
3338
3339 target_pstate = clamp_t(int, target_pstate, min_pstate, max_pstate);
3340
3341 intel_cpufreq_hwp_update(cpu, min_pstate, max_pstate, target_pstate, true);
3342
3343 cpu->pstate.current_pstate = target_pstate;
3344 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
3345 }
3346
intel_cpufreq_cpu_init(struct cpufreq_policy * policy)3347 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
3348 {
3349 struct freq_qos_request *req;
3350 struct cpudata *cpu;
3351 struct device *dev;
3352 int ret, freq;
3353
3354 dev = get_cpu_device(policy->cpu);
3355 if (!dev)
3356 return -ENODEV;
3357
3358 ret = __intel_pstate_cpu_init(policy);
3359 if (ret)
3360 return ret;
3361
3362 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
3363 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
3364 policy->cur = policy->cpuinfo.min_freq;
3365
3366 req = kcalloc(2, sizeof(*req), GFP_KERNEL);
3367 if (!req) {
3368 ret = -ENOMEM;
3369 goto pstate_exit;
3370 }
3371
3372 cpu = all_cpu_data[policy->cpu];
3373
3374 if (hwp_active) {
3375 u64 value;
3376
3377 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
3378
3379 intel_pstate_get_hwp_cap(cpu);
3380
3381 rdmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
3382 WRITE_ONCE(cpu->hwp_req_cached, value);
3383
3384 cpu->epp_cached = intel_pstate_get_epp(cpu, value);
3385 } else {
3386 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
3387 }
3388
3389 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.min_perf_pct, 100);
3390
3391 ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
3392 freq);
3393 if (ret < 0) {
3394 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
3395 goto free_req;
3396 }
3397
3398 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.max_perf_pct, 100);
3399
3400 ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
3401 freq);
3402 if (ret < 0) {
3403 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
3404 goto remove_min_req;
3405 }
3406
3407 policy->driver_data = req;
3408
3409 return 0;
3410
3411 remove_min_req:
3412 freq_qos_remove_request(req);
3413 free_req:
3414 kfree(req);
3415 pstate_exit:
3416 intel_pstate_exit_perf_limits(policy);
3417
3418 return ret;
3419 }
3420
intel_cpufreq_cpu_exit(struct cpufreq_policy * policy)3421 static void intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
3422 {
3423 struct freq_qos_request *req;
3424
3425 req = policy->driver_data;
3426
3427 freq_qos_remove_request(req + 1);
3428 freq_qos_remove_request(req);
3429 kfree(req);
3430
3431 intel_pstate_cpu_exit(policy);
3432 }
3433
intel_cpufreq_suspend(struct cpufreq_policy * policy)3434 static int intel_cpufreq_suspend(struct cpufreq_policy *policy)
3435 {
3436 intel_pstate_suspend(policy);
3437
3438 if (hwp_active) {
3439 struct cpudata *cpu = all_cpu_data[policy->cpu];
3440 u64 value = READ_ONCE(cpu->hwp_req_cached);
3441
3442 /*
3443 * Clear the desired perf field in MSR_HWP_REQUEST in case
3444 * intel_cpufreq_adjust_perf() is in use and the last value
3445 * written by it may not be suitable.
3446 */
3447 value &= ~HWP_DESIRED_PERF(~0L);
3448 wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
3449 WRITE_ONCE(cpu->hwp_req_cached, value);
3450 }
3451
3452 return 0;
3453 }
3454
3455 static struct cpufreq_driver intel_cpufreq = {
3456 .flags = CPUFREQ_CONST_LOOPS,
3457 .verify = intel_cpufreq_verify_policy,
3458 .target = intel_cpufreq_target,
3459 .fast_switch = intel_cpufreq_fast_switch,
3460 .init = intel_cpufreq_cpu_init,
3461 .exit = intel_cpufreq_cpu_exit,
3462 .offline = intel_cpufreq_cpu_offline,
3463 .online = intel_pstate_cpu_online,
3464 .suspend = intel_cpufreq_suspend,
3465 .resume = intel_pstate_resume,
3466 .update_limits = intel_pstate_update_limits,
3467 .name = "intel_cpufreq",
3468 };
3469
3470 static struct cpufreq_driver *default_driver;
3471
intel_pstate_driver_cleanup(void)3472 static void intel_pstate_driver_cleanup(void)
3473 {
3474 unsigned int cpu;
3475
3476 cpus_read_lock();
3477 for_each_online_cpu(cpu) {
3478 if (all_cpu_data[cpu]) {
3479 if (intel_pstate_driver == &intel_pstate)
3480 intel_pstate_clear_update_util_hook(cpu);
3481
3482 kfree(all_cpu_data[cpu]);
3483 WRITE_ONCE(all_cpu_data[cpu], NULL);
3484 }
3485 }
3486 cpus_read_unlock();
3487
3488 intel_pstate_driver = NULL;
3489 }
3490
intel_pstate_register_driver(struct cpufreq_driver * driver)3491 static int intel_pstate_register_driver(struct cpufreq_driver *driver)
3492 {
3493 bool refresh_cpu_cap_scaling;
3494 int ret;
3495
3496 if (driver == &intel_pstate)
3497 intel_pstate_sysfs_expose_hwp_dynamic_boost();
3498
3499 memset(&global, 0, sizeof(global));
3500 global.max_perf_pct = 100;
3501 global.turbo_disabled = turbo_is_disabled();
3502 global.no_turbo = global.turbo_disabled;
3503
3504 arch_set_max_freq_ratio(global.turbo_disabled);
3505
3506 refresh_cpu_cap_scaling = hybrid_clear_max_perf_cpu();
3507
3508 intel_pstate_driver = driver;
3509 ret = cpufreq_register_driver(intel_pstate_driver);
3510 if (ret) {
3511 intel_pstate_driver_cleanup();
3512 return ret;
3513 }
3514
3515 global.min_perf_pct = min_perf_pct_min();
3516
3517 hybrid_init_cpu_capacity_scaling(refresh_cpu_cap_scaling);
3518
3519 return 0;
3520 }
3521
intel_pstate_show_status(char * buf)3522 static ssize_t intel_pstate_show_status(char *buf)
3523 {
3524 if (!intel_pstate_driver)
3525 return sprintf(buf, "off\n");
3526
3527 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
3528 "active" : "passive");
3529 }
3530
intel_pstate_update_status(const char * buf,size_t size)3531 static int intel_pstate_update_status(const char *buf, size_t size)
3532 {
3533 if (size == 3 && !strncmp(buf, "off", size)) {
3534 if (!intel_pstate_driver)
3535 return -EINVAL;
3536
3537 if (hwp_active)
3538 return -EBUSY;
3539
3540 cpufreq_unregister_driver(intel_pstate_driver);
3541 intel_pstate_driver_cleanup();
3542 return 0;
3543 }
3544
3545 if (size == 6 && !strncmp(buf, "active", size)) {
3546 if (intel_pstate_driver) {
3547 if (intel_pstate_driver == &intel_pstate)
3548 return 0;
3549
3550 cpufreq_unregister_driver(intel_pstate_driver);
3551 }
3552
3553 return intel_pstate_register_driver(&intel_pstate);
3554 }
3555
3556 if (size == 7 && !strncmp(buf, "passive", size)) {
3557 if (intel_pstate_driver) {
3558 if (intel_pstate_driver == &intel_cpufreq)
3559 return 0;
3560
3561 cpufreq_unregister_driver(intel_pstate_driver);
3562 intel_pstate_sysfs_hide_hwp_dynamic_boost();
3563 }
3564
3565 return intel_pstate_register_driver(&intel_cpufreq);
3566 }
3567
3568 return -EINVAL;
3569 }
3570
3571 static int no_load __initdata;
3572 static int no_hwp __initdata;
3573 static int hwp_only __initdata;
3574 static unsigned int force_load __initdata;
3575
intel_pstate_msrs_not_valid(void)3576 static int __init intel_pstate_msrs_not_valid(void)
3577 {
3578 if (!pstate_funcs.get_max(0) ||
3579 !pstate_funcs.get_min(0) ||
3580 !pstate_funcs.get_turbo(0))
3581 return -ENODEV;
3582
3583 return 0;
3584 }
3585
copy_cpu_funcs(struct pstate_funcs * funcs)3586 static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
3587 {
3588 pstate_funcs.get_max = funcs->get_max;
3589 pstate_funcs.get_max_physical = funcs->get_max_physical;
3590 pstate_funcs.get_min = funcs->get_min;
3591 pstate_funcs.get_turbo = funcs->get_turbo;
3592 pstate_funcs.get_scaling = funcs->get_scaling;
3593 pstate_funcs.get_val = funcs->get_val;
3594 pstate_funcs.get_vid = funcs->get_vid;
3595 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
3596 }
3597
3598 #ifdef CONFIG_ACPI
3599
intel_pstate_no_acpi_pss(void)3600 static bool __init intel_pstate_no_acpi_pss(void)
3601 {
3602 int i;
3603
3604 for_each_possible_cpu(i) {
3605 acpi_status status;
3606 union acpi_object *pss;
3607 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3608 struct acpi_processor *pr = per_cpu(processors, i);
3609
3610 if (!pr)
3611 continue;
3612
3613 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
3614 if (ACPI_FAILURE(status))
3615 continue;
3616
3617 pss = buffer.pointer;
3618 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
3619 kfree(pss);
3620 return false;
3621 }
3622
3623 kfree(pss);
3624 }
3625
3626 pr_debug("ACPI _PSS not found\n");
3627 return true;
3628 }
3629
intel_pstate_no_acpi_pcch(void)3630 static bool __init intel_pstate_no_acpi_pcch(void)
3631 {
3632 acpi_status status;
3633 acpi_handle handle;
3634
3635 status = acpi_get_handle(NULL, "\\_SB", &handle);
3636 if (ACPI_FAILURE(status))
3637 goto not_found;
3638
3639 if (acpi_has_method(handle, "PCCH"))
3640 return false;
3641
3642 not_found:
3643 pr_debug("ACPI PCCH not found\n");
3644 return true;
3645 }
3646
intel_pstate_has_acpi_ppc(void)3647 static bool __init intel_pstate_has_acpi_ppc(void)
3648 {
3649 int i;
3650
3651 for_each_possible_cpu(i) {
3652 struct acpi_processor *pr = per_cpu(processors, i);
3653
3654 if (!pr)
3655 continue;
3656 if (acpi_has_method(pr->handle, "_PPC"))
3657 return true;
3658 }
3659 pr_debug("ACPI _PPC not found\n");
3660 return false;
3661 }
3662
3663 enum {
3664 PSS,
3665 PPC,
3666 };
3667
3668 /* Hardware vendor-specific info that has its own power management modes */
3669 static struct acpi_platform_list plat_info[] __initdata = {
3670 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
3671 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3672 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3673 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3674 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3675 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3676 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3677 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3678 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3679 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3680 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3681 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3682 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3683 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3684 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3685 { } /* End */
3686 };
3687
3688 #define BITMASK_OOB (BIT(8) | BIT(18))
3689
intel_pstate_platform_pwr_mgmt_exists(void)3690 static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
3691 {
3692 const struct x86_cpu_id *id;
3693 u64 misc_pwr;
3694 int idx;
3695
3696 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
3697 if (id) {
3698 rdmsrq(MSR_MISC_PWR_MGMT, misc_pwr);
3699 if (misc_pwr & BITMASK_OOB) {
3700 pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
3701 pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
3702 return true;
3703 }
3704 }
3705
3706 idx = acpi_match_platform_list(plat_info);
3707 if (idx < 0)
3708 return false;
3709
3710 switch (plat_info[idx].data) {
3711 case PSS:
3712 if (!intel_pstate_no_acpi_pss())
3713 return false;
3714
3715 return intel_pstate_no_acpi_pcch();
3716 case PPC:
3717 return intel_pstate_has_acpi_ppc() && !force_load;
3718 }
3719
3720 return false;
3721 }
3722
intel_pstate_request_control_from_smm(void)3723 static void intel_pstate_request_control_from_smm(void)
3724 {
3725 /*
3726 * It may be unsafe to request P-states control from SMM if _PPC support
3727 * has not been enabled.
3728 */
3729 if (acpi_ppc)
3730 acpi_processor_pstate_control();
3731 }
3732 #else /* CONFIG_ACPI not enabled */
intel_pstate_platform_pwr_mgmt_exists(void)3733 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
intel_pstate_has_acpi_ppc(void)3734 static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
intel_pstate_request_control_from_smm(void)3735 static inline void intel_pstate_request_control_from_smm(void) {}
3736 #endif /* CONFIG_ACPI */
3737
3738 #define INTEL_PSTATE_HWP_BROADWELL 0x01
3739
3740 #define X86_MATCH_HWP(vfm, hwp_mode) \
3741 X86_MATCH_VFM_FEATURE(vfm, X86_FEATURE_HWP, hwp_mode)
3742
3743 static const struct x86_cpu_id hwp_support_ids[] __initconst = {
3744 X86_MATCH_HWP(INTEL_BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
3745 X86_MATCH_HWP(INTEL_BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
3746 X86_MATCH_HWP(INTEL_ANY, 0),
3747 {}
3748 };
3749
intel_pstate_hwp_is_enabled(void)3750 static bool intel_pstate_hwp_is_enabled(void)
3751 {
3752 u64 value;
3753
3754 rdmsrq(MSR_PM_ENABLE, value);
3755 return !!(value & 0x1);
3756 }
3757
3758 #define POWERSAVE_MASK GENMASK(7, 0)
3759 #define BALANCE_POWER_MASK GENMASK(15, 8)
3760 #define BALANCE_PERFORMANCE_MASK GENMASK(23, 16)
3761 #define PERFORMANCE_MASK GENMASK(31, 24)
3762
3763 #define HWP_SET_EPP_VALUES(powersave, balance_power, balance_perf, performance) \
3764 (FIELD_PREP_CONST(POWERSAVE_MASK, powersave) |\
3765 FIELD_PREP_CONST(BALANCE_POWER_MASK, balance_power) |\
3766 FIELD_PREP_CONST(BALANCE_PERFORMANCE_MASK, balance_perf) |\
3767 FIELD_PREP_CONST(PERFORMANCE_MASK, performance))
3768
3769 #define HWP_SET_DEF_BALANCE_PERF_EPP(balance_perf) \
3770 (HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE, HWP_EPP_BALANCE_POWERSAVE,\
3771 balance_perf, HWP_EPP_PERFORMANCE))
3772
3773 static const struct x86_cpu_id intel_epp_default[] = {
3774 /*
3775 * Set EPP value as 102, this is the max suggested EPP
3776 * which can result in one core turbo frequency for
3777 * AlderLake Mobile CPUs.
3778 */
3779 X86_MATCH_VFM(INTEL_ALDERLAKE_L, HWP_SET_DEF_BALANCE_PERF_EPP(102)),
3780 X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3781 X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3782 X86_MATCH_VFM(INTEL_GRANITERAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3783 X86_MATCH_VFM(INTEL_GRANITERAPIDS_D, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3784 X86_MATCH_VFM(INTEL_METEORLAKE_L, HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE,
3785 179, 64, 16)),
3786 X86_MATCH_VFM(INTEL_ARROWLAKE, HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE,
3787 179, 64, 16)),
3788 {}
3789 };
3790
3791 static const struct x86_cpu_id intel_hybrid_scaling_factor[] = {
3792 X86_MATCH_VFM(INTEL_ALDERLAKE, HYBRID_SCALING_FACTOR_ADL),
3793 X86_MATCH_VFM(INTEL_ALDERLAKE_L, HYBRID_SCALING_FACTOR_ADL),
3794 X86_MATCH_VFM(INTEL_RAPTORLAKE, HYBRID_SCALING_FACTOR_ADL),
3795 X86_MATCH_VFM(INTEL_RAPTORLAKE_P, HYBRID_SCALING_FACTOR_ADL),
3796 X86_MATCH_VFM(INTEL_RAPTORLAKE_S, HYBRID_SCALING_FACTOR_ADL),
3797 X86_MATCH_VFM(INTEL_METEORLAKE_L, HYBRID_SCALING_FACTOR_MTL),
3798 X86_MATCH_VFM(INTEL_LUNARLAKE_M, HYBRID_SCALING_FACTOR_LNL),
3799 {}
3800 };
3801
intel_pstate_init(void)3802 static int __init intel_pstate_init(void)
3803 {
3804 static struct cpudata **_all_cpu_data;
3805 const struct x86_cpu_id *id;
3806 int rc;
3807
3808 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
3809 return -ENODEV;
3810
3811 /*
3812 * The Intel pstate driver will be ignored if the platform
3813 * firmware has its own power management modes.
3814 */
3815 if (intel_pstate_platform_pwr_mgmt_exists()) {
3816 pr_info("P-states controlled by the platform\n");
3817 return -ENODEV;
3818 }
3819
3820 id = x86_match_cpu(hwp_support_ids);
3821 if (id) {
3822 hwp_forced = intel_pstate_hwp_is_enabled();
3823
3824 if (hwp_forced)
3825 pr_info("HWP enabled by BIOS\n");
3826 else if (no_load)
3827 return -ENODEV;
3828
3829 copy_cpu_funcs(&core_funcs);
3830 /*
3831 * Avoid enabling HWP for processors without EPP support,
3832 * because that means incomplete HWP implementation which is a
3833 * corner case and supporting it is generally problematic.
3834 *
3835 * If HWP is enabled already, though, there is no choice but to
3836 * deal with it.
3837 */
3838 if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
3839 hwp_active = true;
3840 hwp_mode_bdw = id->driver_data;
3841 intel_pstate.attr = hwp_cpufreq_attrs;
3842 intel_cpufreq.attr = hwp_cpufreq_attrs;
3843 intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
3844 intel_cpufreq.adjust_perf = intel_cpufreq_adjust_perf;
3845 if (!default_driver)
3846 default_driver = &intel_pstate;
3847
3848 pstate_funcs.get_cpu_scaling = hwp_get_cpu_scaling;
3849
3850 goto hwp_cpu_matched;
3851 }
3852 pr_info("HWP not enabled\n");
3853 } else {
3854 if (no_load)
3855 return -ENODEV;
3856
3857 id = x86_match_cpu(intel_pstate_cpu_ids);
3858 if (!id) {
3859 pr_info("CPU model not supported\n");
3860 return -ENODEV;
3861 }
3862
3863 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
3864 }
3865
3866 if (intel_pstate_msrs_not_valid()) {
3867 pr_info("Invalid MSRs\n");
3868 return -ENODEV;
3869 }
3870 /* Without HWP start in the passive mode. */
3871 if (!default_driver)
3872 default_driver = &intel_cpufreq;
3873
3874 hwp_cpu_matched:
3875 if (!hwp_active && hwp_only)
3876 return -ENOTSUPP;
3877
3878 pr_info("Intel P-state driver initializing\n");
3879
3880 _all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
3881 if (!_all_cpu_data)
3882 return -ENOMEM;
3883
3884 WRITE_ONCE(all_cpu_data, _all_cpu_data);
3885
3886 intel_pstate_request_control_from_smm();
3887
3888 intel_pstate_sysfs_expose_params();
3889
3890 if (hwp_active) {
3891 const struct x86_cpu_id *id = x86_match_cpu(intel_epp_default);
3892 const struct x86_cpu_id *hybrid_id = x86_match_cpu(intel_hybrid_scaling_factor);
3893
3894 if (id) {
3895 epp_values[EPP_INDEX_POWERSAVE] =
3896 FIELD_GET(POWERSAVE_MASK, id->driver_data);
3897 epp_values[EPP_INDEX_BALANCE_POWERSAVE] =
3898 FIELD_GET(BALANCE_POWER_MASK, id->driver_data);
3899 epp_values[EPP_INDEX_BALANCE_PERFORMANCE] =
3900 FIELD_GET(BALANCE_PERFORMANCE_MASK, id->driver_data);
3901 epp_values[EPP_INDEX_PERFORMANCE] =
3902 FIELD_GET(PERFORMANCE_MASK, id->driver_data);
3903 pr_debug("Updated EPPs powersave:%x balanced power:%x balanced perf:%x performance:%x\n",
3904 epp_values[EPP_INDEX_POWERSAVE],
3905 epp_values[EPP_INDEX_BALANCE_POWERSAVE],
3906 epp_values[EPP_INDEX_BALANCE_PERFORMANCE],
3907 epp_values[EPP_INDEX_PERFORMANCE]);
3908 }
3909
3910 if (hybrid_id) {
3911 hybrid_scaling_factor = hybrid_id->driver_data;
3912 pr_debug("hybrid scaling factor: %d\n", hybrid_scaling_factor);
3913 }
3914
3915 }
3916
3917 mutex_lock(&intel_pstate_driver_lock);
3918 rc = intel_pstate_register_driver(default_driver);
3919 mutex_unlock(&intel_pstate_driver_lock);
3920 if (rc) {
3921 intel_pstate_sysfs_remove();
3922 return rc;
3923 }
3924
3925 if (hwp_active) {
3926 const struct x86_cpu_id *id;
3927
3928 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
3929 if (id) {
3930 set_power_ctl_ee_state(false);
3931 pr_info("Disabling energy efficiency optimization\n");
3932 }
3933
3934 pr_info("HWP enabled\n");
3935 } else if (boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
3936 pr_warn("Problematic setup: Hybrid processor with disabled HWP\n");
3937 }
3938
3939 return 0;
3940 }
3941 device_initcall(intel_pstate_init);
3942
intel_pstate_setup(char * str)3943 static int __init intel_pstate_setup(char *str)
3944 {
3945 if (!str)
3946 return -EINVAL;
3947
3948 if (!strcmp(str, "disable"))
3949 no_load = 1;
3950 else if (!strcmp(str, "active"))
3951 default_driver = &intel_pstate;
3952 else if (!strcmp(str, "passive"))
3953 default_driver = &intel_cpufreq;
3954
3955 if (!strcmp(str, "no_hwp"))
3956 no_hwp = 1;
3957
3958 if (!strcmp(str, "no_cas"))
3959 no_cas = true;
3960
3961 if (!strcmp(str, "force"))
3962 force_load = 1;
3963 if (!strcmp(str, "hwp_only"))
3964 hwp_only = 1;
3965 if (!strcmp(str, "per_cpu_perf_limits"))
3966 per_cpu_limits = true;
3967
3968 #ifdef CONFIG_ACPI
3969 if (!strcmp(str, "support_acpi_ppc"))
3970 acpi_ppc = true;
3971 #endif
3972
3973 return 0;
3974 }
3975 early_param("intel_pstate", intel_pstate_setup);
3976
3977 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
3978 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
3979