Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/cpufreq/intel_pstate.c
26278 views
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 <[email protected]>
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
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
68
static inline int32_t div_fp(s64 x, s64 y)
69
{
70
return div64_s64((int64_t)x << FRAC_BITS, y);
71
}
72
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
84
static inline u64 mul_ext_fp(u64 x, u64 y)
85
{
86
return (x * y) >> EXT_FRAC_BITS;
87
}
88
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
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
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
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 */
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
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
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
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 */
437
static inline void intel_pstate_set_itmt_prio(int cpu)
438
{
439
}
440
#endif /* CONFIG_ACPI_CPPC_LIB */
441
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
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 */
509
static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
510
{
511
}
512
513
static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
514
{
515
}
516
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
524
static inline int intel_pstate_get_cppc_guaranteed(int cpu)
525
{
526
return -ENOTSUPP;
527
}
528
529
static int intel_pstate_cppc_get_scaling(int cpu)
530
{
531
return core_get_scaling();
532
}
533
#endif /* CONFIG_ACPI_CPPC_LIB */
534
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
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
*/
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 */
1060
static inline bool hybrid_register_perf_domain(unsigned int cpu) { return false; }
1061
static inline void hybrid_register_all_perf_domains(void) {}
1062
static inline void hybrid_update_perf_domain(struct cpudata *cpu) {}
1063
#endif /* CONFIG_ENERGY_MODEL */
1064
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
1080
static void hybrid_clear_cpu_capacity(unsigned int cpunum)
1081
{
1082
arch_set_cpu_capacity(cpunum, 1, 1, 1, 1);
1083
}
1084
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
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
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
1151
static void __hybrid_refresh_cpu_capacity_scaling(void)
1152
{
1153
hybrid_max_perf_cpu = NULL;
1154
hybrid_update_cpu_capacity_scaling();
1155
}
1156
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
2313
static int knl_get_aperf_mperf_shift(void)
2314
{
2315
return 10;
2316
}
2317
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
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
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
2373
static void intel_pstate_set_min_pstate(struct cpudata *cpu)
2374
{
2375
intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
2376
}
2377
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
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
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
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
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
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
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
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
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
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
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
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
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
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
X86_MATCH(INTEL_GRANITERAPIDS_D, core_funcs),
2779
X86_MATCH(INTEL_GRANITERAPIDS_X, core_funcs),
2780
{}
2781
};
2782
MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
2783
2784
#ifdef CONFIG_ACPI
2785
static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
2786
X86_MATCH(INTEL_BROADWELL_D, core_funcs),
2787
X86_MATCH(INTEL_BROADWELL_X, core_funcs),
2788
X86_MATCH(INTEL_SKYLAKE_X, core_funcs),
2789
X86_MATCH(INTEL_ICELAKE_X, core_funcs),
2790
X86_MATCH(INTEL_SAPPHIRERAPIDS_X, core_funcs),
2791
X86_MATCH(INTEL_EMERALDRAPIDS_X, core_funcs),
2792
X86_MATCH(INTEL_GRANITERAPIDS_D, core_funcs),
2793
X86_MATCH(INTEL_GRANITERAPIDS_X, core_funcs),
2794
X86_MATCH(INTEL_ATOM_CRESTMONT, core_funcs),
2795
X86_MATCH(INTEL_ATOM_CRESTMONT_X, core_funcs),
2796
X86_MATCH(INTEL_ATOM_DARKMONT_X, core_funcs),
2797
{}
2798
};
2799
#endif
2800
2801
static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
2802
X86_MATCH(INTEL_KABYLAKE, core_funcs),
2803
{}
2804
};
2805
2806
static int intel_pstate_init_cpu(unsigned int cpunum)
2807
{
2808
struct cpudata *cpu;
2809
2810
cpu = all_cpu_data[cpunum];
2811
2812
if (!cpu) {
2813
cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
2814
if (!cpu)
2815
return -ENOMEM;
2816
2817
WRITE_ONCE(all_cpu_data[cpunum], cpu);
2818
2819
cpu->cpu = cpunum;
2820
2821
cpu->epp_default = -EINVAL;
2822
2823
if (hwp_active) {
2824
intel_pstate_hwp_enable(cpu);
2825
2826
if (intel_pstate_acpi_pm_profile_server())
2827
hwp_boost = true;
2828
}
2829
} else if (hwp_active) {
2830
/*
2831
* Re-enable HWP in case this happens after a resume from ACPI
2832
* S3 if the CPU was offline during the whole system/resume
2833
* cycle.
2834
*/
2835
intel_pstate_hwp_reenable(cpu);
2836
}
2837
2838
cpu->epp_powersave = -EINVAL;
2839
cpu->epp_policy = CPUFREQ_POLICY_UNKNOWN;
2840
2841
intel_pstate_get_cpu_pstates(cpu);
2842
2843
pr_debug("controlling: cpu %d\n", cpunum);
2844
2845
return 0;
2846
}
2847
2848
static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
2849
{
2850
struct cpudata *cpu = all_cpu_data[cpu_num];
2851
2852
if (hwp_active && !hwp_boost)
2853
return;
2854
2855
if (cpu->update_util_set)
2856
return;
2857
2858
/* Prevent intel_pstate_update_util() from using stale data. */
2859
cpu->sample.time = 0;
2860
cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
2861
(hwp_active ?
2862
intel_pstate_update_util_hwp :
2863
intel_pstate_update_util));
2864
cpu->update_util_set = true;
2865
}
2866
2867
static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2868
{
2869
struct cpudata *cpu_data = all_cpu_data[cpu];
2870
2871
if (!cpu_data->update_util_set)
2872
return;
2873
2874
cpufreq_remove_update_util_hook(cpu);
2875
cpu_data->update_util_set = false;
2876
synchronize_rcu();
2877
}
2878
2879
static int intel_pstate_get_max_freq(struct cpudata *cpu)
2880
{
2881
return READ_ONCE(global.no_turbo) ?
2882
cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2883
}
2884
2885
static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2886
unsigned int policy_min,
2887
unsigned int policy_max)
2888
{
2889
int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
2890
int32_t max_policy_perf, min_policy_perf;
2891
2892
max_policy_perf = policy_max / perf_ctl_scaling;
2893
if (policy_max == policy_min) {
2894
min_policy_perf = max_policy_perf;
2895
} else {
2896
min_policy_perf = policy_min / perf_ctl_scaling;
2897
min_policy_perf = clamp_t(int32_t, min_policy_perf,
2898
0, max_policy_perf);
2899
}
2900
2901
/*
2902
* HWP needs some special consideration, because HWP_REQUEST uses
2903
* abstract values to represent performance rather than pure ratios.
2904
*/
2905
if (hwp_active && cpu->pstate.scaling != perf_ctl_scaling) {
2906
int freq;
2907
2908
freq = max_policy_perf * perf_ctl_scaling;
2909
max_policy_perf = intel_pstate_freq_to_hwp(cpu, freq);
2910
freq = min_policy_perf * perf_ctl_scaling;
2911
min_policy_perf = intel_pstate_freq_to_hwp(cpu, freq);
2912
}
2913
2914
pr_debug("cpu:%d min_policy_perf:%d max_policy_perf:%d\n",
2915
cpu->cpu, min_policy_perf, max_policy_perf);
2916
2917
/* Normalize user input to [min_perf, max_perf] */
2918
if (per_cpu_limits) {
2919
cpu->min_perf_ratio = min_policy_perf;
2920
cpu->max_perf_ratio = max_policy_perf;
2921
} else {
2922
int turbo_max = cpu->pstate.turbo_pstate;
2923
int32_t global_min, global_max;
2924
2925
/* Global limits are in percent of the maximum turbo P-state. */
2926
global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2927
global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2928
global_min = clamp_t(int32_t, global_min, 0, global_max);
2929
2930
pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
2931
global_min, global_max);
2932
2933
cpu->min_perf_ratio = max(min_policy_perf, global_min);
2934
cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
2935
cpu->max_perf_ratio = min(max_policy_perf, global_max);
2936
cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
2937
2938
/* Make sure min_perf <= max_perf */
2939
cpu->min_perf_ratio = min(cpu->min_perf_ratio,
2940
cpu->max_perf_ratio);
2941
2942
}
2943
pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
2944
cpu->max_perf_ratio,
2945
cpu->min_perf_ratio);
2946
}
2947
2948
static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2949
{
2950
struct cpudata *cpu;
2951
2952
if (!policy->cpuinfo.max_freq)
2953
return -ENODEV;
2954
2955
pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2956
policy->cpuinfo.max_freq, policy->max);
2957
2958
cpu = all_cpu_data[policy->cpu];
2959
cpu->policy = policy->policy;
2960
2961
mutex_lock(&intel_pstate_limits_lock);
2962
2963
intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2964
2965
if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
2966
int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
2967
2968
/*
2969
* NOHZ_FULL CPUs need this as the governor callback may not
2970
* be invoked on them.
2971
*/
2972
intel_pstate_clear_update_util_hook(policy->cpu);
2973
intel_pstate_set_pstate(cpu, pstate);
2974
} else {
2975
intel_pstate_set_update_util_hook(policy->cpu);
2976
}
2977
2978
if (hwp_active) {
2979
/*
2980
* When hwp_boost was active before and dynamically it
2981
* was turned off, in that case we need to clear the
2982
* update util hook.
2983
*/
2984
if (!hwp_boost)
2985
intel_pstate_clear_update_util_hook(policy->cpu);
2986
intel_pstate_hwp_set(policy->cpu);
2987
}
2988
/*
2989
* policy->cur is never updated with the intel_pstate driver, but it
2990
* is used as a stale frequency value. So, keep it within limits.
2991
*/
2992
policy->cur = policy->min;
2993
2994
mutex_unlock(&intel_pstate_limits_lock);
2995
2996
return 0;
2997
}
2998
2999
static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
3000
struct cpufreq_policy_data *policy)
3001
{
3002
if (!hwp_active &&
3003
cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
3004
policy->max < policy->cpuinfo.max_freq &&
3005
policy->max > cpu->pstate.max_freq) {
3006
pr_debug("policy->max > max non turbo frequency\n");
3007
policy->max = policy->cpuinfo.max_freq;
3008
}
3009
}
3010
3011
static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
3012
struct cpufreq_policy_data *policy)
3013
{
3014
int max_freq;
3015
3016
if (hwp_active) {
3017
intel_pstate_get_hwp_cap(cpu);
3018
max_freq = READ_ONCE(global.no_turbo) ?
3019
cpu->pstate.max_freq : cpu->pstate.turbo_freq;
3020
} else {
3021
max_freq = intel_pstate_get_max_freq(cpu);
3022
}
3023
cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
3024
3025
intel_pstate_adjust_policy_max(cpu, policy);
3026
}
3027
3028
static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
3029
{
3030
intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
3031
3032
return 0;
3033
}
3034
3035
static int intel_cpufreq_cpu_offline(struct cpufreq_policy *policy)
3036
{
3037
struct cpudata *cpu = all_cpu_data[policy->cpu];
3038
3039
pr_debug("CPU %d going offline\n", cpu->cpu);
3040
3041
if (cpu->suspended)
3042
return 0;
3043
3044
/*
3045
* If the CPU is an SMT thread and it goes offline with the performance
3046
* settings different from the minimum, it will prevent its sibling
3047
* from getting to lower performance levels, so force the minimum
3048
* performance on CPU offline to prevent that from happening.
3049
*/
3050
if (hwp_active)
3051
intel_pstate_hwp_offline(cpu);
3052
else
3053
intel_pstate_set_min_pstate(cpu);
3054
3055
intel_pstate_exit_perf_limits(policy);
3056
3057
return 0;
3058
}
3059
3060
static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
3061
{
3062
struct cpudata *cpu = all_cpu_data[policy->cpu];
3063
3064
pr_debug("CPU %d going online\n", cpu->cpu);
3065
3066
intel_pstate_init_acpi_perf_limits(policy);
3067
3068
if (hwp_active) {
3069
/*
3070
* Re-enable HWP and clear the "suspended" flag to let "resume"
3071
* know that it need not do that.
3072
*/
3073
intel_pstate_hwp_reenable(cpu);
3074
cpu->suspended = false;
3075
3076
hybrid_update_capacity(cpu);
3077
}
3078
3079
return 0;
3080
}
3081
3082
static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
3083
{
3084
intel_pstate_clear_update_util_hook(policy->cpu);
3085
3086
return intel_cpufreq_cpu_offline(policy);
3087
}
3088
3089
static void intel_pstate_cpu_exit(struct cpufreq_policy *policy)
3090
{
3091
pr_debug("CPU %d exiting\n", policy->cpu);
3092
3093
policy->fast_switch_possible = false;
3094
}
3095
3096
static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
3097
{
3098
struct cpudata *cpu;
3099
int rc;
3100
3101
rc = intel_pstate_init_cpu(policy->cpu);
3102
if (rc)
3103
return rc;
3104
3105
cpu = all_cpu_data[policy->cpu];
3106
3107
cpu->max_perf_ratio = 0xFF;
3108
cpu->min_perf_ratio = 0;
3109
3110
/* cpuinfo and default policy values */
3111
policy->cpuinfo.min_freq = cpu->pstate.min_freq;
3112
policy->cpuinfo.max_freq = READ_ONCE(global.no_turbo) ?
3113
cpu->pstate.max_freq : cpu->pstate.turbo_freq;
3114
3115
policy->min = policy->cpuinfo.min_freq;
3116
policy->max = policy->cpuinfo.max_freq;
3117
3118
intel_pstate_init_acpi_perf_limits(policy);
3119
3120
policy->fast_switch_possible = true;
3121
3122
return 0;
3123
}
3124
3125
static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
3126
{
3127
int ret = __intel_pstate_cpu_init(policy);
3128
3129
if (ret)
3130
return ret;
3131
3132
/*
3133
* Set the policy to powersave to provide a valid fallback value in case
3134
* the default cpufreq governor is neither powersave nor performance.
3135
*/
3136
policy->policy = CPUFREQ_POLICY_POWERSAVE;
3137
3138
if (hwp_active) {
3139
struct cpudata *cpu = all_cpu_data[policy->cpu];
3140
3141
cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
3142
}
3143
3144
return 0;
3145
}
3146
3147
static struct cpufreq_driver intel_pstate = {
3148
.flags = CPUFREQ_CONST_LOOPS,
3149
.verify = intel_pstate_verify_policy,
3150
.setpolicy = intel_pstate_set_policy,
3151
.suspend = intel_pstate_suspend,
3152
.resume = intel_pstate_resume,
3153
.init = intel_pstate_cpu_init,
3154
.exit = intel_pstate_cpu_exit,
3155
.offline = intel_pstate_cpu_offline,
3156
.online = intel_pstate_cpu_online,
3157
.update_limits = intel_pstate_update_limits,
3158
.name = "intel_pstate",
3159
};
3160
3161
static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
3162
{
3163
struct cpudata *cpu = all_cpu_data[policy->cpu];
3164
3165
intel_pstate_verify_cpu_policy(cpu, policy);
3166
intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
3167
3168
return 0;
3169
}
3170
3171
/* Use of trace in passive mode:
3172
*
3173
* In passive mode the trace core_busy field (also known as the
3174
* performance field, and lablelled as such on the graphs; also known as
3175
* core_avg_perf) is not needed and so is re-assigned to indicate if the
3176
* driver call was via the normal or fast switch path. Various graphs
3177
* output from the intel_pstate_tracer.py utility that include core_busy
3178
* (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
3179
* so we use 10 to indicate the normal path through the driver, and
3180
* 90 to indicate the fast switch path through the driver.
3181
* The scaled_busy field is not used, and is set to 0.
3182
*/
3183
3184
#define INTEL_PSTATE_TRACE_TARGET 10
3185
#define INTEL_PSTATE_TRACE_FAST_SWITCH 90
3186
3187
static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
3188
{
3189
struct sample *sample;
3190
3191
if (!trace_pstate_sample_enabled())
3192
return;
3193
3194
if (!intel_pstate_sample(cpu, ktime_get()))
3195
return;
3196
3197
sample = &cpu->sample;
3198
trace_pstate_sample(trace_type,
3199
0,
3200
old_pstate,
3201
cpu->pstate.current_pstate,
3202
sample->mperf,
3203
sample->aperf,
3204
sample->tsc,
3205
get_avg_frequency(cpu),
3206
fp_toint(cpu->iowait_boost * 100));
3207
}
3208
3209
static void intel_cpufreq_hwp_update(struct cpudata *cpu, u32 min, u32 max,
3210
u32 desired, bool fast_switch)
3211
{
3212
u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
3213
3214
value &= ~HWP_MIN_PERF(~0L);
3215
value |= HWP_MIN_PERF(min);
3216
3217
value &= ~HWP_MAX_PERF(~0L);
3218
value |= HWP_MAX_PERF(max);
3219
3220
value &= ~HWP_DESIRED_PERF(~0L);
3221
value |= HWP_DESIRED_PERF(desired);
3222
3223
if (value == prev)
3224
return;
3225
3226
WRITE_ONCE(cpu->hwp_req_cached, value);
3227
if (fast_switch)
3228
wrmsrq(MSR_HWP_REQUEST, value);
3229
else
3230
wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
3231
}
3232
3233
static void intel_cpufreq_perf_ctl_update(struct cpudata *cpu,
3234
u32 target_pstate, bool fast_switch)
3235
{
3236
if (fast_switch)
3237
wrmsrq(MSR_IA32_PERF_CTL,
3238
pstate_funcs.get_val(cpu, target_pstate));
3239
else
3240
wrmsrq_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
3241
pstate_funcs.get_val(cpu, target_pstate));
3242
}
3243
3244
static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
3245
int target_pstate, bool fast_switch)
3246
{
3247
struct cpudata *cpu = all_cpu_data[policy->cpu];
3248
int old_pstate = cpu->pstate.current_pstate;
3249
3250
target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
3251
if (hwp_active) {
3252
int max_pstate = policy->strict_target ?
3253
target_pstate : cpu->max_perf_ratio;
3254
3255
intel_cpufreq_hwp_update(cpu, target_pstate, max_pstate,
3256
target_pstate, fast_switch);
3257
} else if (target_pstate != old_pstate) {
3258
intel_cpufreq_perf_ctl_update(cpu, target_pstate, fast_switch);
3259
}
3260
3261
cpu->pstate.current_pstate = target_pstate;
3262
3263
intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
3264
INTEL_PSTATE_TRACE_TARGET, old_pstate);
3265
3266
return target_pstate;
3267
}
3268
3269
static int intel_cpufreq_target(struct cpufreq_policy *policy,
3270
unsigned int target_freq,
3271
unsigned int relation)
3272
{
3273
struct cpudata *cpu = all_cpu_data[policy->cpu];
3274
struct cpufreq_freqs freqs;
3275
int target_pstate;
3276
3277
freqs.old = policy->cur;
3278
freqs.new = target_freq;
3279
3280
cpufreq_freq_transition_begin(policy, &freqs);
3281
3282
target_pstate = intel_pstate_freq_to_hwp_rel(cpu, freqs.new, relation);
3283
target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
3284
3285
freqs.new = target_pstate * cpu->pstate.scaling;
3286
3287
cpufreq_freq_transition_end(policy, &freqs, false);
3288
3289
return 0;
3290
}
3291
3292
static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
3293
unsigned int target_freq)
3294
{
3295
struct cpudata *cpu = all_cpu_data[policy->cpu];
3296
int target_pstate;
3297
3298
target_pstate = intel_pstate_freq_to_hwp(cpu, target_freq);
3299
3300
target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
3301
3302
return target_pstate * cpu->pstate.scaling;
3303
}
3304
3305
static void intel_cpufreq_adjust_perf(unsigned int cpunum,
3306
unsigned long min_perf,
3307
unsigned long target_perf,
3308
unsigned long capacity)
3309
{
3310
struct cpudata *cpu = all_cpu_data[cpunum];
3311
u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
3312
int old_pstate = cpu->pstate.current_pstate;
3313
int cap_pstate, min_pstate, max_pstate, target_pstate;
3314
3315
cap_pstate = READ_ONCE(global.no_turbo) ?
3316
HWP_GUARANTEED_PERF(hwp_cap) :
3317
HWP_HIGHEST_PERF(hwp_cap);
3318
3319
/* Optimization: Avoid unnecessary divisions. */
3320
3321
target_pstate = cap_pstate;
3322
if (target_perf < capacity)
3323
target_pstate = DIV_ROUND_UP(cap_pstate * target_perf, capacity);
3324
3325
min_pstate = cap_pstate;
3326
if (min_perf < capacity)
3327
min_pstate = DIV_ROUND_UP(cap_pstate * min_perf, capacity);
3328
3329
if (min_pstate < cpu->pstate.min_pstate)
3330
min_pstate = cpu->pstate.min_pstate;
3331
3332
if (min_pstate < cpu->min_perf_ratio)
3333
min_pstate = cpu->min_perf_ratio;
3334
3335
if (min_pstate > cpu->max_perf_ratio)
3336
min_pstate = cpu->max_perf_ratio;
3337
3338
max_pstate = min(cap_pstate, cpu->max_perf_ratio);
3339
if (max_pstate < min_pstate)
3340
max_pstate = min_pstate;
3341
3342
target_pstate = clamp_t(int, target_pstate, min_pstate, max_pstate);
3343
3344
intel_cpufreq_hwp_update(cpu, min_pstate, max_pstate, target_pstate, true);
3345
3346
cpu->pstate.current_pstate = target_pstate;
3347
intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
3348
}
3349
3350
static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
3351
{
3352
struct freq_qos_request *req;
3353
struct cpudata *cpu;
3354
struct device *dev;
3355
int ret, freq;
3356
3357
dev = get_cpu_device(policy->cpu);
3358
if (!dev)
3359
return -ENODEV;
3360
3361
ret = __intel_pstate_cpu_init(policy);
3362
if (ret)
3363
return ret;
3364
3365
policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
3366
/* This reflects the intel_pstate_get_cpu_pstates() setting. */
3367
policy->cur = policy->cpuinfo.min_freq;
3368
3369
req = kcalloc(2, sizeof(*req), GFP_KERNEL);
3370
if (!req) {
3371
ret = -ENOMEM;
3372
goto pstate_exit;
3373
}
3374
3375
cpu = all_cpu_data[policy->cpu];
3376
3377
if (hwp_active) {
3378
u64 value;
3379
3380
policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
3381
3382
intel_pstate_get_hwp_cap(cpu);
3383
3384
rdmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
3385
WRITE_ONCE(cpu->hwp_req_cached, value);
3386
3387
cpu->epp_cached = intel_pstate_get_epp(cpu, value);
3388
} else {
3389
policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
3390
}
3391
3392
freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.min_perf_pct, 100);
3393
3394
ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
3395
freq);
3396
if (ret < 0) {
3397
dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
3398
goto free_req;
3399
}
3400
3401
freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.max_perf_pct, 100);
3402
3403
ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
3404
freq);
3405
if (ret < 0) {
3406
dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
3407
goto remove_min_req;
3408
}
3409
3410
policy->driver_data = req;
3411
3412
return 0;
3413
3414
remove_min_req:
3415
freq_qos_remove_request(req);
3416
free_req:
3417
kfree(req);
3418
pstate_exit:
3419
intel_pstate_exit_perf_limits(policy);
3420
3421
return ret;
3422
}
3423
3424
static void intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
3425
{
3426
struct freq_qos_request *req;
3427
3428
req = policy->driver_data;
3429
3430
freq_qos_remove_request(req + 1);
3431
freq_qos_remove_request(req);
3432
kfree(req);
3433
3434
intel_pstate_cpu_exit(policy);
3435
}
3436
3437
static int intel_cpufreq_suspend(struct cpufreq_policy *policy)
3438
{
3439
intel_pstate_suspend(policy);
3440
3441
if (hwp_active) {
3442
struct cpudata *cpu = all_cpu_data[policy->cpu];
3443
u64 value = READ_ONCE(cpu->hwp_req_cached);
3444
3445
/*
3446
* Clear the desired perf field in MSR_HWP_REQUEST in case
3447
* intel_cpufreq_adjust_perf() is in use and the last value
3448
* written by it may not be suitable.
3449
*/
3450
value &= ~HWP_DESIRED_PERF(~0L);
3451
wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
3452
WRITE_ONCE(cpu->hwp_req_cached, value);
3453
}
3454
3455
return 0;
3456
}
3457
3458
static struct cpufreq_driver intel_cpufreq = {
3459
.flags = CPUFREQ_CONST_LOOPS,
3460
.verify = intel_cpufreq_verify_policy,
3461
.target = intel_cpufreq_target,
3462
.fast_switch = intel_cpufreq_fast_switch,
3463
.init = intel_cpufreq_cpu_init,
3464
.exit = intel_cpufreq_cpu_exit,
3465
.offline = intel_cpufreq_cpu_offline,
3466
.online = intel_pstate_cpu_online,
3467
.suspend = intel_cpufreq_suspend,
3468
.resume = intel_pstate_resume,
3469
.update_limits = intel_pstate_update_limits,
3470
.name = "intel_cpufreq",
3471
};
3472
3473
static struct cpufreq_driver *default_driver;
3474
3475
static void intel_pstate_driver_cleanup(void)
3476
{
3477
unsigned int cpu;
3478
3479
cpus_read_lock();
3480
for_each_online_cpu(cpu) {
3481
if (all_cpu_data[cpu]) {
3482
if (intel_pstate_driver == &intel_pstate)
3483
intel_pstate_clear_update_util_hook(cpu);
3484
3485
kfree(all_cpu_data[cpu]);
3486
WRITE_ONCE(all_cpu_data[cpu], NULL);
3487
}
3488
}
3489
cpus_read_unlock();
3490
3491
intel_pstate_driver = NULL;
3492
}
3493
3494
static int intel_pstate_register_driver(struct cpufreq_driver *driver)
3495
{
3496
bool refresh_cpu_cap_scaling;
3497
int ret;
3498
3499
if (driver == &intel_pstate)
3500
intel_pstate_sysfs_expose_hwp_dynamic_boost();
3501
3502
memset(&global, 0, sizeof(global));
3503
global.max_perf_pct = 100;
3504
global.turbo_disabled = turbo_is_disabled();
3505
global.no_turbo = global.turbo_disabled;
3506
3507
arch_set_max_freq_ratio(global.turbo_disabled);
3508
3509
refresh_cpu_cap_scaling = hybrid_clear_max_perf_cpu();
3510
3511
intel_pstate_driver = driver;
3512
ret = cpufreq_register_driver(intel_pstate_driver);
3513
if (ret) {
3514
intel_pstate_driver_cleanup();
3515
return ret;
3516
}
3517
3518
global.min_perf_pct = min_perf_pct_min();
3519
3520
hybrid_init_cpu_capacity_scaling(refresh_cpu_cap_scaling);
3521
3522
return 0;
3523
}
3524
3525
static ssize_t intel_pstate_show_status(char *buf)
3526
{
3527
if (!intel_pstate_driver)
3528
return sprintf(buf, "off\n");
3529
3530
return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
3531
"active" : "passive");
3532
}
3533
3534
static int intel_pstate_update_status(const char *buf, size_t size)
3535
{
3536
if (size == 3 && !strncmp(buf, "off", size)) {
3537
if (!intel_pstate_driver)
3538
return -EINVAL;
3539
3540
if (hwp_active)
3541
return -EBUSY;
3542
3543
cpufreq_unregister_driver(intel_pstate_driver);
3544
intel_pstate_driver_cleanup();
3545
return 0;
3546
}
3547
3548
if (size == 6 && !strncmp(buf, "active", size)) {
3549
if (intel_pstate_driver) {
3550
if (intel_pstate_driver == &intel_pstate)
3551
return 0;
3552
3553
cpufreq_unregister_driver(intel_pstate_driver);
3554
}
3555
3556
return intel_pstate_register_driver(&intel_pstate);
3557
}
3558
3559
if (size == 7 && !strncmp(buf, "passive", size)) {
3560
if (intel_pstate_driver) {
3561
if (intel_pstate_driver == &intel_cpufreq)
3562
return 0;
3563
3564
cpufreq_unregister_driver(intel_pstate_driver);
3565
intel_pstate_sysfs_hide_hwp_dynamic_boost();
3566
}
3567
3568
return intel_pstate_register_driver(&intel_cpufreq);
3569
}
3570
3571
return -EINVAL;
3572
}
3573
3574
static int no_load __initdata;
3575
static int no_hwp __initdata;
3576
static int hwp_only __initdata;
3577
static unsigned int force_load __initdata;
3578
3579
static int __init intel_pstate_msrs_not_valid(void)
3580
{
3581
if (!pstate_funcs.get_max(0) ||
3582
!pstate_funcs.get_min(0) ||
3583
!pstate_funcs.get_turbo(0))
3584
return -ENODEV;
3585
3586
return 0;
3587
}
3588
3589
static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
3590
{
3591
pstate_funcs.get_max = funcs->get_max;
3592
pstate_funcs.get_max_physical = funcs->get_max_physical;
3593
pstate_funcs.get_min = funcs->get_min;
3594
pstate_funcs.get_turbo = funcs->get_turbo;
3595
pstate_funcs.get_scaling = funcs->get_scaling;
3596
pstate_funcs.get_val = funcs->get_val;
3597
pstate_funcs.get_vid = funcs->get_vid;
3598
pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
3599
}
3600
3601
#ifdef CONFIG_ACPI
3602
3603
static bool __init intel_pstate_no_acpi_pss(void)
3604
{
3605
int i;
3606
3607
for_each_possible_cpu(i) {
3608
acpi_status status;
3609
union acpi_object *pss;
3610
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3611
struct acpi_processor *pr = per_cpu(processors, i);
3612
3613
if (!pr)
3614
continue;
3615
3616
status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
3617
if (ACPI_FAILURE(status))
3618
continue;
3619
3620
pss = buffer.pointer;
3621
if (pss && pss->type == ACPI_TYPE_PACKAGE) {
3622
kfree(pss);
3623
return false;
3624
}
3625
3626
kfree(pss);
3627
}
3628
3629
pr_debug("ACPI _PSS not found\n");
3630
return true;
3631
}
3632
3633
static bool __init intel_pstate_no_acpi_pcch(void)
3634
{
3635
acpi_status status;
3636
acpi_handle handle;
3637
3638
status = acpi_get_handle(NULL, "\\_SB", &handle);
3639
if (ACPI_FAILURE(status))
3640
goto not_found;
3641
3642
if (acpi_has_method(handle, "PCCH"))
3643
return false;
3644
3645
not_found:
3646
pr_debug("ACPI PCCH not found\n");
3647
return true;
3648
}
3649
3650
static bool __init intel_pstate_has_acpi_ppc(void)
3651
{
3652
int i;
3653
3654
for_each_possible_cpu(i) {
3655
struct acpi_processor *pr = per_cpu(processors, i);
3656
3657
if (!pr)
3658
continue;
3659
if (acpi_has_method(pr->handle, "_PPC"))
3660
return true;
3661
}
3662
pr_debug("ACPI _PPC not found\n");
3663
return false;
3664
}
3665
3666
enum {
3667
PSS,
3668
PPC,
3669
};
3670
3671
/* Hardware vendor-specific info that has its own power management modes */
3672
static struct acpi_platform_list plat_info[] __initdata = {
3673
{"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
3674
{"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3675
{"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3676
{"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3677
{"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3678
{"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3679
{"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3680
{"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3681
{"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3682
{"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3683
{"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3684
{"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3685
{"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3686
{"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3687
{"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3688
{ } /* End */
3689
};
3690
3691
#define BITMASK_OOB (BIT(8) | BIT(18))
3692
3693
static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
3694
{
3695
const struct x86_cpu_id *id;
3696
u64 misc_pwr;
3697
int idx;
3698
3699
id = x86_match_cpu(intel_pstate_cpu_oob_ids);
3700
if (id) {
3701
rdmsrq(MSR_MISC_PWR_MGMT, misc_pwr);
3702
if (misc_pwr & BITMASK_OOB) {
3703
pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
3704
pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
3705
return true;
3706
}
3707
}
3708
3709
idx = acpi_match_platform_list(plat_info);
3710
if (idx < 0)
3711
return false;
3712
3713
switch (plat_info[idx].data) {
3714
case PSS:
3715
if (!intel_pstate_no_acpi_pss())
3716
return false;
3717
3718
return intel_pstate_no_acpi_pcch();
3719
case PPC:
3720
return intel_pstate_has_acpi_ppc() && !force_load;
3721
}
3722
3723
return false;
3724
}
3725
3726
static void intel_pstate_request_control_from_smm(void)
3727
{
3728
/*
3729
* It may be unsafe to request P-states control from SMM if _PPC support
3730
* has not been enabled.
3731
*/
3732
if (acpi_ppc)
3733
acpi_processor_pstate_control();
3734
}
3735
#else /* CONFIG_ACPI not enabled */
3736
static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
3737
static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
3738
static inline void intel_pstate_request_control_from_smm(void) {}
3739
#endif /* CONFIG_ACPI */
3740
3741
#define INTEL_PSTATE_HWP_BROADWELL 0x01
3742
3743
#define X86_MATCH_HWP(vfm, hwp_mode) \
3744
X86_MATCH_VFM_FEATURE(vfm, X86_FEATURE_HWP, hwp_mode)
3745
3746
static const struct x86_cpu_id hwp_support_ids[] __initconst = {
3747
X86_MATCH_HWP(INTEL_BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
3748
X86_MATCH_HWP(INTEL_BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
3749
X86_MATCH_HWP(INTEL_ANY, 0),
3750
{}
3751
};
3752
3753
static bool intel_pstate_hwp_is_enabled(void)
3754
{
3755
u64 value;
3756
3757
rdmsrq(MSR_PM_ENABLE, value);
3758
return !!(value & 0x1);
3759
}
3760
3761
#define POWERSAVE_MASK GENMASK(7, 0)
3762
#define BALANCE_POWER_MASK GENMASK(15, 8)
3763
#define BALANCE_PERFORMANCE_MASK GENMASK(23, 16)
3764
#define PERFORMANCE_MASK GENMASK(31, 24)
3765
3766
#define HWP_SET_EPP_VALUES(powersave, balance_power, balance_perf, performance) \
3767
(FIELD_PREP_CONST(POWERSAVE_MASK, powersave) |\
3768
FIELD_PREP_CONST(BALANCE_POWER_MASK, balance_power) |\
3769
FIELD_PREP_CONST(BALANCE_PERFORMANCE_MASK, balance_perf) |\
3770
FIELD_PREP_CONST(PERFORMANCE_MASK, performance))
3771
3772
#define HWP_SET_DEF_BALANCE_PERF_EPP(balance_perf) \
3773
(HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE, HWP_EPP_BALANCE_POWERSAVE,\
3774
balance_perf, HWP_EPP_PERFORMANCE))
3775
3776
static const struct x86_cpu_id intel_epp_default[] = {
3777
/*
3778
* Set EPP value as 102, this is the max suggested EPP
3779
* which can result in one core turbo frequency for
3780
* AlderLake Mobile CPUs.
3781
*/
3782
X86_MATCH_VFM(INTEL_ALDERLAKE_L, HWP_SET_DEF_BALANCE_PERF_EPP(102)),
3783
X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3784
X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3785
X86_MATCH_VFM(INTEL_GRANITERAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3786
X86_MATCH_VFM(INTEL_GRANITERAPIDS_D, HWP_SET_DEF_BALANCE_PERF_EPP(32)),
3787
X86_MATCH_VFM(INTEL_METEORLAKE_L, HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE,
3788
179, 64, 16)),
3789
X86_MATCH_VFM(INTEL_ARROWLAKE, HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE,
3790
179, 64, 16)),
3791
{}
3792
};
3793
3794
static const struct x86_cpu_id intel_hybrid_scaling_factor[] = {
3795
X86_MATCH_VFM(INTEL_ALDERLAKE, HYBRID_SCALING_FACTOR_ADL),
3796
X86_MATCH_VFM(INTEL_ALDERLAKE_L, HYBRID_SCALING_FACTOR_ADL),
3797
X86_MATCH_VFM(INTEL_RAPTORLAKE, HYBRID_SCALING_FACTOR_ADL),
3798
X86_MATCH_VFM(INTEL_RAPTORLAKE_P, HYBRID_SCALING_FACTOR_ADL),
3799
X86_MATCH_VFM(INTEL_RAPTORLAKE_S, HYBRID_SCALING_FACTOR_ADL),
3800
X86_MATCH_VFM(INTEL_METEORLAKE_L, HYBRID_SCALING_FACTOR_MTL),
3801
X86_MATCH_VFM(INTEL_LUNARLAKE_M, HYBRID_SCALING_FACTOR_LNL),
3802
{}
3803
};
3804
3805
static int __init intel_pstate_init(void)
3806
{
3807
static struct cpudata **_all_cpu_data;
3808
const struct x86_cpu_id *id;
3809
int rc;
3810
3811
if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
3812
return -ENODEV;
3813
3814
/*
3815
* The Intel pstate driver will be ignored if the platform
3816
* firmware has its own power management modes.
3817
*/
3818
if (intel_pstate_platform_pwr_mgmt_exists()) {
3819
pr_info("P-states controlled by the platform\n");
3820
return -ENODEV;
3821
}
3822
3823
id = x86_match_cpu(hwp_support_ids);
3824
if (id) {
3825
hwp_forced = intel_pstate_hwp_is_enabled();
3826
3827
if (hwp_forced)
3828
pr_info("HWP enabled by BIOS\n");
3829
else if (no_load)
3830
return -ENODEV;
3831
3832
copy_cpu_funcs(&core_funcs);
3833
/*
3834
* Avoid enabling HWP for processors without EPP support,
3835
* because that means incomplete HWP implementation which is a
3836
* corner case and supporting it is generally problematic.
3837
*
3838
* If HWP is enabled already, though, there is no choice but to
3839
* deal with it.
3840
*/
3841
if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
3842
hwp_active = true;
3843
hwp_mode_bdw = id->driver_data;
3844
intel_pstate.attr = hwp_cpufreq_attrs;
3845
intel_cpufreq.attr = hwp_cpufreq_attrs;
3846
intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
3847
intel_cpufreq.adjust_perf = intel_cpufreq_adjust_perf;
3848
if (!default_driver)
3849
default_driver = &intel_pstate;
3850
3851
pstate_funcs.get_cpu_scaling = hwp_get_cpu_scaling;
3852
3853
goto hwp_cpu_matched;
3854
}
3855
pr_info("HWP not enabled\n");
3856
} else {
3857
if (no_load)
3858
return -ENODEV;
3859
3860
id = x86_match_cpu(intel_pstate_cpu_ids);
3861
if (!id) {
3862
pr_info("CPU model not supported\n");
3863
return -ENODEV;
3864
}
3865
3866
copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
3867
}
3868
3869
if (intel_pstate_msrs_not_valid()) {
3870
pr_info("Invalid MSRs\n");
3871
return -ENODEV;
3872
}
3873
/* Without HWP start in the passive mode. */
3874
if (!default_driver)
3875
default_driver = &intel_cpufreq;
3876
3877
hwp_cpu_matched:
3878
if (!hwp_active && hwp_only)
3879
return -ENOTSUPP;
3880
3881
pr_info("Intel P-state driver initializing\n");
3882
3883
_all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
3884
if (!_all_cpu_data)
3885
return -ENOMEM;
3886
3887
WRITE_ONCE(all_cpu_data, _all_cpu_data);
3888
3889
intel_pstate_request_control_from_smm();
3890
3891
intel_pstate_sysfs_expose_params();
3892
3893
if (hwp_active) {
3894
const struct x86_cpu_id *id = x86_match_cpu(intel_epp_default);
3895
const struct x86_cpu_id *hybrid_id = x86_match_cpu(intel_hybrid_scaling_factor);
3896
3897
if (id) {
3898
epp_values[EPP_INDEX_POWERSAVE] =
3899
FIELD_GET(POWERSAVE_MASK, id->driver_data);
3900
epp_values[EPP_INDEX_BALANCE_POWERSAVE] =
3901
FIELD_GET(BALANCE_POWER_MASK, id->driver_data);
3902
epp_values[EPP_INDEX_BALANCE_PERFORMANCE] =
3903
FIELD_GET(BALANCE_PERFORMANCE_MASK, id->driver_data);
3904
epp_values[EPP_INDEX_PERFORMANCE] =
3905
FIELD_GET(PERFORMANCE_MASK, id->driver_data);
3906
pr_debug("Updated EPPs powersave:%x balanced power:%x balanced perf:%x performance:%x\n",
3907
epp_values[EPP_INDEX_POWERSAVE],
3908
epp_values[EPP_INDEX_BALANCE_POWERSAVE],
3909
epp_values[EPP_INDEX_BALANCE_PERFORMANCE],
3910
epp_values[EPP_INDEX_PERFORMANCE]);
3911
}
3912
3913
if (hybrid_id) {
3914
hybrid_scaling_factor = hybrid_id->driver_data;
3915
pr_debug("hybrid scaling factor: %d\n", hybrid_scaling_factor);
3916
}
3917
3918
}
3919
3920
mutex_lock(&intel_pstate_driver_lock);
3921
rc = intel_pstate_register_driver(default_driver);
3922
mutex_unlock(&intel_pstate_driver_lock);
3923
if (rc) {
3924
intel_pstate_sysfs_remove();
3925
return rc;
3926
}
3927
3928
if (hwp_active) {
3929
const struct x86_cpu_id *id;
3930
3931
id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
3932
if (id) {
3933
set_power_ctl_ee_state(false);
3934
pr_info("Disabling energy efficiency optimization\n");
3935
}
3936
3937
pr_info("HWP enabled\n");
3938
} else if (boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
3939
pr_warn("Problematic setup: Hybrid processor with disabled HWP\n");
3940
}
3941
3942
return 0;
3943
}
3944
device_initcall(intel_pstate_init);
3945
3946
static int __init intel_pstate_setup(char *str)
3947
{
3948
if (!str)
3949
return -EINVAL;
3950
3951
if (!strcmp(str, "disable"))
3952
no_load = 1;
3953
else if (!strcmp(str, "active"))
3954
default_driver = &intel_pstate;
3955
else if (!strcmp(str, "passive"))
3956
default_driver = &intel_cpufreq;
3957
3958
if (!strcmp(str, "no_hwp"))
3959
no_hwp = 1;
3960
3961
if (!strcmp(str, "no_cas"))
3962
no_cas = true;
3963
3964
if (!strcmp(str, "force"))
3965
force_load = 1;
3966
if (!strcmp(str, "hwp_only"))
3967
hwp_only = 1;
3968
if (!strcmp(str, "per_cpu_perf_limits"))
3969
per_cpu_limits = true;
3970
3971
#ifdef CONFIG_ACPI
3972
if (!strcmp(str, "support_acpi_ppc"))
3973
acpi_ppc = true;
3974
#endif
3975
3976
return 0;
3977
}
3978
early_param("intel_pstate", intel_pstate_setup);
3979
3980
MODULE_AUTHOR("Dirk Brandewie <[email protected]>");
3981
MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
3982
3983