xref: /OK3568_Linux_fs/kernel/drivers/cpufreq/intel_pstate.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * intel_pstate.c: Native P state management for Intel processors
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright 2012 Intel Corporation
6*4882a593Smuzhiyun  * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/kernel_stat.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/ktime.h>
15*4882a593Smuzhiyun #include <linux/hrtimer.h>
16*4882a593Smuzhiyun #include <linux/tick.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/sched/cpufreq.h>
19*4882a593Smuzhiyun #include <linux/list.h>
20*4882a593Smuzhiyun #include <linux/cpu.h>
21*4882a593Smuzhiyun #include <linux/cpufreq.h>
22*4882a593Smuzhiyun #include <linux/sysfs.h>
23*4882a593Smuzhiyun #include <linux/types.h>
24*4882a593Smuzhiyun #include <linux/fs.h>
25*4882a593Smuzhiyun #include <linux/acpi.h>
26*4882a593Smuzhiyun #include <linux/vmalloc.h>
27*4882a593Smuzhiyun #include <linux/pm_qos.h>
28*4882a593Smuzhiyun #include <trace/events/power.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <asm/div64.h>
31*4882a593Smuzhiyun #include <asm/msr.h>
32*4882a593Smuzhiyun #include <asm/cpu_device_id.h>
33*4882a593Smuzhiyun #include <asm/cpufeature.h>
34*4882a593Smuzhiyun #include <asm/intel-family.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define INTEL_PSTATE_SAMPLING_INTERVAL	(10 * NSEC_PER_MSEC)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define INTEL_CPUFREQ_TRANSITION_LATENCY	20000
39*4882a593Smuzhiyun #define INTEL_CPUFREQ_TRANSITION_DELAY_HWP	5000
40*4882a593Smuzhiyun #define INTEL_CPUFREQ_TRANSITION_DELAY		500
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #ifdef CONFIG_ACPI
43*4882a593Smuzhiyun #include <acpi/processor.h>
44*4882a593Smuzhiyun #include <acpi/cppc_acpi.h>
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define FRAC_BITS 8
48*4882a593Smuzhiyun #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
49*4882a593Smuzhiyun #define fp_toint(X) ((X) >> FRAC_BITS)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3))
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define EXT_BITS 6
54*4882a593Smuzhiyun #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
55*4882a593Smuzhiyun #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
56*4882a593Smuzhiyun #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
57*4882a593Smuzhiyun 
mul_fp(int32_t x,int32_t y)58*4882a593Smuzhiyun static inline int32_t mul_fp(int32_t x, int32_t y)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
div_fp(s64 x,s64 y)63*4882a593Smuzhiyun static inline int32_t div_fp(s64 x, s64 y)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return div64_s64((int64_t)x << FRAC_BITS, y);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
ceiling_fp(int32_t x)68*4882a593Smuzhiyun static inline int ceiling_fp(int32_t x)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	int mask, ret;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	ret = fp_toint(x);
73*4882a593Smuzhiyun 	mask = (1 << FRAC_BITS) - 1;
74*4882a593Smuzhiyun 	if (x & mask)
75*4882a593Smuzhiyun 		ret += 1;
76*4882a593Smuzhiyun 	return ret;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
percent_fp(int percent)79*4882a593Smuzhiyun static inline int32_t percent_fp(int percent)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	return div_fp(percent, 100);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
mul_ext_fp(u64 x,u64 y)84*4882a593Smuzhiyun static inline u64 mul_ext_fp(u64 x, u64 y)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	return (x * y) >> EXT_FRAC_BITS;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
div_ext_fp(u64 x,u64 y)89*4882a593Smuzhiyun static inline u64 div_ext_fp(u64 x, u64 y)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	return div64_u64(x << EXT_FRAC_BITS, y);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
percent_ext_fp(int percent)94*4882a593Smuzhiyun static inline int32_t percent_ext_fp(int percent)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	return div_ext_fp(percent, 100);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun  * struct sample -	Store performance sample
101*4882a593Smuzhiyun  * @core_avg_perf:	Ratio of APERF/MPERF which is the actual average
102*4882a593Smuzhiyun  *			performance during last sample period
103*4882a593Smuzhiyun  * @busy_scaled:	Scaled busy value which is used to calculate next
104*4882a593Smuzhiyun  *			P state. This can be different than core_avg_perf
105*4882a593Smuzhiyun  *			to account for cpu idle period
106*4882a593Smuzhiyun  * @aperf:		Difference of actual performance frequency clock count
107*4882a593Smuzhiyun  *			read from APERF MSR between last and current sample
108*4882a593Smuzhiyun  * @mperf:		Difference of maximum performance frequency clock count
109*4882a593Smuzhiyun  *			read from MPERF MSR between last and current sample
110*4882a593Smuzhiyun  * @tsc:		Difference of time stamp counter between last and
111*4882a593Smuzhiyun  *			current sample
112*4882a593Smuzhiyun  * @time:		Current time from scheduler
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * This structure is used in the cpudata structure to store performance sample
115*4882a593Smuzhiyun  * data for choosing next P State.
116*4882a593Smuzhiyun  */
117*4882a593Smuzhiyun struct sample {
118*4882a593Smuzhiyun 	int32_t core_avg_perf;
119*4882a593Smuzhiyun 	int32_t busy_scaled;
120*4882a593Smuzhiyun 	u64 aperf;
121*4882a593Smuzhiyun 	u64 mperf;
122*4882a593Smuzhiyun 	u64 tsc;
123*4882a593Smuzhiyun 	u64 time;
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * struct pstate_data - Store P state data
128*4882a593Smuzhiyun  * @current_pstate:	Current requested P state
129*4882a593Smuzhiyun  * @min_pstate:		Min P state possible for this platform
130*4882a593Smuzhiyun  * @max_pstate:		Max P state possible for this platform
131*4882a593Smuzhiyun  * @max_pstate_physical:This is physical Max P state for a processor
132*4882a593Smuzhiyun  *			This can be higher than the max_pstate which can
133*4882a593Smuzhiyun  *			be limited by platform thermal design power limits
134*4882a593Smuzhiyun  * @scaling:		Scaling factor to  convert frequency to cpufreq
135*4882a593Smuzhiyun  *			frequency units
136*4882a593Smuzhiyun  * @turbo_pstate:	Max Turbo P state possible for this platform
137*4882a593Smuzhiyun  * @max_freq:		@max_pstate frequency in cpufreq units
138*4882a593Smuzhiyun  * @turbo_freq:		@turbo_pstate frequency in cpufreq units
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * Stores the per cpu model P state limits and current P state.
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun struct pstate_data {
143*4882a593Smuzhiyun 	int	current_pstate;
144*4882a593Smuzhiyun 	int	min_pstate;
145*4882a593Smuzhiyun 	int	max_pstate;
146*4882a593Smuzhiyun 	int	max_pstate_physical;
147*4882a593Smuzhiyun 	int	scaling;
148*4882a593Smuzhiyun 	int	turbo_pstate;
149*4882a593Smuzhiyun 	unsigned int max_freq;
150*4882a593Smuzhiyun 	unsigned int turbo_freq;
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /**
154*4882a593Smuzhiyun  * struct vid_data -	Stores voltage information data
155*4882a593Smuzhiyun  * @min:		VID data for this platform corresponding to
156*4882a593Smuzhiyun  *			the lowest P state
157*4882a593Smuzhiyun  * @max:		VID data corresponding to the highest P State.
158*4882a593Smuzhiyun  * @turbo:		VID data for turbo P state
159*4882a593Smuzhiyun  * @ratio:		Ratio of (vid max - vid min) /
160*4882a593Smuzhiyun  *			(max P state - Min P State)
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
163*4882a593Smuzhiyun  * This data is used in Atom platforms, where in addition to target P state,
164*4882a593Smuzhiyun  * the voltage data needs to be specified to select next P State.
165*4882a593Smuzhiyun  */
166*4882a593Smuzhiyun struct vid_data {
167*4882a593Smuzhiyun 	int min;
168*4882a593Smuzhiyun 	int max;
169*4882a593Smuzhiyun 	int turbo;
170*4882a593Smuzhiyun 	int32_t ratio;
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * struct global_params - Global parameters, mostly tunable via sysfs.
175*4882a593Smuzhiyun  * @no_turbo:		Whether or not to use turbo P-states.
176*4882a593Smuzhiyun  * @turbo_disabled:	Whether or not turbo P-states are available at all,
177*4882a593Smuzhiyun  *			based on the MSR_IA32_MISC_ENABLE value and whether or
178*4882a593Smuzhiyun  *			not the maximum reported turbo P-state is different from
179*4882a593Smuzhiyun  *			the maximum reported non-turbo one.
180*4882a593Smuzhiyun  * @turbo_disabled_mf:	The @turbo_disabled value reflected by cpuinfo.max_freq.
181*4882a593Smuzhiyun  * @min_perf_pct:	Minimum capacity limit in percent of the maximum turbo
182*4882a593Smuzhiyun  *			P-state capacity.
183*4882a593Smuzhiyun  * @max_perf_pct:	Maximum capacity limit in percent of the maximum turbo
184*4882a593Smuzhiyun  *			P-state capacity.
185*4882a593Smuzhiyun  */
186*4882a593Smuzhiyun struct global_params {
187*4882a593Smuzhiyun 	bool no_turbo;
188*4882a593Smuzhiyun 	bool turbo_disabled;
189*4882a593Smuzhiyun 	bool turbo_disabled_mf;
190*4882a593Smuzhiyun 	int max_perf_pct;
191*4882a593Smuzhiyun 	int min_perf_pct;
192*4882a593Smuzhiyun };
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun  * struct cpudata -	Per CPU instance data storage
196*4882a593Smuzhiyun  * @cpu:		CPU number for this instance data
197*4882a593Smuzhiyun  * @policy:		CPUFreq policy value
198*4882a593Smuzhiyun  * @update_util:	CPUFreq utility callback information
199*4882a593Smuzhiyun  * @update_util_set:	CPUFreq utility callback is set
200*4882a593Smuzhiyun  * @iowait_boost:	iowait-related boost fraction
201*4882a593Smuzhiyun  * @last_update:	Time of the last update.
202*4882a593Smuzhiyun  * @pstate:		Stores P state limits for this CPU
203*4882a593Smuzhiyun  * @vid:		Stores VID limits for this CPU
204*4882a593Smuzhiyun  * @last_sample_time:	Last Sample time
205*4882a593Smuzhiyun  * @aperf_mperf_shift:	APERF vs MPERF counting frequency difference
206*4882a593Smuzhiyun  * @prev_aperf:		Last APERF value read from APERF MSR
207*4882a593Smuzhiyun  * @prev_mperf:		Last MPERF value read from MPERF MSR
208*4882a593Smuzhiyun  * @prev_tsc:		Last timestamp counter (TSC) value
209*4882a593Smuzhiyun  * @prev_cummulative_iowait: IO Wait time difference from last and
210*4882a593Smuzhiyun  *			current sample
211*4882a593Smuzhiyun  * @sample:		Storage for storing last Sample data
212*4882a593Smuzhiyun  * @min_perf_ratio:	Minimum capacity in terms of PERF or HWP ratios
213*4882a593Smuzhiyun  * @max_perf_ratio:	Maximum capacity in terms of PERF or HWP ratios
214*4882a593Smuzhiyun  * @acpi_perf_data:	Stores ACPI perf information read from _PSS
215*4882a593Smuzhiyun  * @valid_pss_table:	Set to true for valid ACPI _PSS entries found
216*4882a593Smuzhiyun  * @epp_powersave:	Last saved HWP energy performance preference
217*4882a593Smuzhiyun  *			(EPP) or energy performance bias (EPB),
218*4882a593Smuzhiyun  *			when policy switched to performance
219*4882a593Smuzhiyun  * @epp_policy:		Last saved policy used to set EPP/EPB
220*4882a593Smuzhiyun  * @epp_default:	Power on default HWP energy performance
221*4882a593Smuzhiyun  *			preference/bias
222*4882a593Smuzhiyun  * @epp_cached		Cached HWP energy-performance preference value
223*4882a593Smuzhiyun  * @hwp_req_cached:	Cached value of the last HWP Request MSR
224*4882a593Smuzhiyun  * @hwp_cap_cached:	Cached value of the last HWP Capabilities MSR
225*4882a593Smuzhiyun  * @last_io_update:	Last time when IO wake flag was set
226*4882a593Smuzhiyun  * @sched_flags:	Store scheduler flags for possible cross CPU update
227*4882a593Smuzhiyun  * @hwp_boost_min:	Last HWP boosted min performance
228*4882a593Smuzhiyun  * @suspended:		Whether or not the driver has been suspended.
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * This structure stores per CPU instance data for all CPUs.
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun struct cpudata {
233*4882a593Smuzhiyun 	int cpu;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	unsigned int policy;
236*4882a593Smuzhiyun 	struct update_util_data update_util;
237*4882a593Smuzhiyun 	bool   update_util_set;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	struct pstate_data pstate;
240*4882a593Smuzhiyun 	struct vid_data vid;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	u64	last_update;
243*4882a593Smuzhiyun 	u64	last_sample_time;
244*4882a593Smuzhiyun 	u64	aperf_mperf_shift;
245*4882a593Smuzhiyun 	u64	prev_aperf;
246*4882a593Smuzhiyun 	u64	prev_mperf;
247*4882a593Smuzhiyun 	u64	prev_tsc;
248*4882a593Smuzhiyun 	u64	prev_cummulative_iowait;
249*4882a593Smuzhiyun 	struct sample sample;
250*4882a593Smuzhiyun 	int32_t	min_perf_ratio;
251*4882a593Smuzhiyun 	int32_t	max_perf_ratio;
252*4882a593Smuzhiyun #ifdef CONFIG_ACPI
253*4882a593Smuzhiyun 	struct acpi_processor_performance acpi_perf_data;
254*4882a593Smuzhiyun 	bool valid_pss_table;
255*4882a593Smuzhiyun #endif
256*4882a593Smuzhiyun 	unsigned int iowait_boost;
257*4882a593Smuzhiyun 	s16 epp_powersave;
258*4882a593Smuzhiyun 	s16 epp_policy;
259*4882a593Smuzhiyun 	s16 epp_default;
260*4882a593Smuzhiyun 	s16 epp_cached;
261*4882a593Smuzhiyun 	u64 hwp_req_cached;
262*4882a593Smuzhiyun 	u64 hwp_cap_cached;
263*4882a593Smuzhiyun 	u64 last_io_update;
264*4882a593Smuzhiyun 	unsigned int sched_flags;
265*4882a593Smuzhiyun 	u32 hwp_boost_min;
266*4882a593Smuzhiyun 	bool suspended;
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun static struct cpudata **all_cpu_data;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun  * struct pstate_funcs - Per CPU model specific callbacks
273*4882a593Smuzhiyun  * @get_max:		Callback to get maximum non turbo effective P state
274*4882a593Smuzhiyun  * @get_max_physical:	Callback to get maximum non turbo physical P state
275*4882a593Smuzhiyun  * @get_min:		Callback to get minimum P state
276*4882a593Smuzhiyun  * @get_turbo:		Callback to get turbo P state
277*4882a593Smuzhiyun  * @get_scaling:	Callback to get frequency scaling factor
278*4882a593Smuzhiyun  * @get_aperf_mperf_shift: Callback to get the APERF vs MPERF frequency difference
279*4882a593Smuzhiyun  * @get_val:		Callback to convert P state to actual MSR write value
280*4882a593Smuzhiyun  * @get_vid:		Callback to get VID data for Atom platforms
281*4882a593Smuzhiyun  *
282*4882a593Smuzhiyun  * Core and Atom CPU models have different way to get P State limits. This
283*4882a593Smuzhiyun  * structure is used to store those callbacks.
284*4882a593Smuzhiyun  */
285*4882a593Smuzhiyun struct pstate_funcs {
286*4882a593Smuzhiyun 	int (*get_max)(void);
287*4882a593Smuzhiyun 	int (*get_max_physical)(void);
288*4882a593Smuzhiyun 	int (*get_min)(void);
289*4882a593Smuzhiyun 	int (*get_turbo)(void);
290*4882a593Smuzhiyun 	int (*get_scaling)(void);
291*4882a593Smuzhiyun 	int (*get_aperf_mperf_shift)(void);
292*4882a593Smuzhiyun 	u64 (*get_val)(struct cpudata*, int pstate);
293*4882a593Smuzhiyun 	void (*get_vid)(struct cpudata *);
294*4882a593Smuzhiyun };
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun static struct pstate_funcs pstate_funcs __read_mostly;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun static int hwp_active __read_mostly;
299*4882a593Smuzhiyun static int hwp_mode_bdw __read_mostly;
300*4882a593Smuzhiyun static bool per_cpu_limits __read_mostly;
301*4882a593Smuzhiyun static bool hwp_boost __read_mostly;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun static struct cpufreq_driver *intel_pstate_driver __read_mostly;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun #ifdef CONFIG_ACPI
306*4882a593Smuzhiyun static bool acpi_ppc;
307*4882a593Smuzhiyun #endif
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun static struct global_params global;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun static DEFINE_MUTEX(intel_pstate_driver_lock);
312*4882a593Smuzhiyun static DEFINE_MUTEX(intel_pstate_limits_lock);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun #ifdef CONFIG_ACPI
315*4882a593Smuzhiyun 
intel_pstate_acpi_pm_profile_server(void)316*4882a593Smuzhiyun static bool intel_pstate_acpi_pm_profile_server(void)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
319*4882a593Smuzhiyun 	    acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
320*4882a593Smuzhiyun 		return true;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	return false;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
intel_pstate_get_ppc_enable_status(void)325*4882a593Smuzhiyun static bool intel_pstate_get_ppc_enable_status(void)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	if (intel_pstate_acpi_pm_profile_server())
328*4882a593Smuzhiyun 		return true;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	return acpi_ppc;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun #ifdef CONFIG_ACPI_CPPC_LIB
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun /* The work item is needed to avoid CPU hotplug locking issues */
intel_pstste_sched_itmt_work_fn(struct work_struct * work)336*4882a593Smuzhiyun static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	sched_set_itmt_support();
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
342*4882a593Smuzhiyun 
intel_pstate_set_itmt_prio(int cpu)343*4882a593Smuzhiyun static void intel_pstate_set_itmt_prio(int cpu)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct cppc_perf_caps cppc_perf;
346*4882a593Smuzhiyun 	static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
347*4882a593Smuzhiyun 	int ret;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	ret = cppc_get_perf_caps(cpu, &cppc_perf);
350*4882a593Smuzhiyun 	if (ret)
351*4882a593Smuzhiyun 		return;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	/*
354*4882a593Smuzhiyun 	 * The priorities can be set regardless of whether or not
355*4882a593Smuzhiyun 	 * sched_set_itmt_support(true) has been called and it is valid to
356*4882a593Smuzhiyun 	 * update them at any time after it has been called.
357*4882a593Smuzhiyun 	 */
358*4882a593Smuzhiyun 	sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	if (max_highest_perf <= min_highest_perf) {
361*4882a593Smuzhiyun 		if (cppc_perf.highest_perf > max_highest_perf)
362*4882a593Smuzhiyun 			max_highest_perf = cppc_perf.highest_perf;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 		if (cppc_perf.highest_perf < min_highest_perf)
365*4882a593Smuzhiyun 			min_highest_perf = cppc_perf.highest_perf;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		if (max_highest_perf > min_highest_perf) {
368*4882a593Smuzhiyun 			/*
369*4882a593Smuzhiyun 			 * This code can be run during CPU online under the
370*4882a593Smuzhiyun 			 * CPU hotplug locks, so sched_set_itmt_support()
371*4882a593Smuzhiyun 			 * cannot be called from here.  Queue up a work item
372*4882a593Smuzhiyun 			 * to invoke it.
373*4882a593Smuzhiyun 			 */
374*4882a593Smuzhiyun 			schedule_work(&sched_itmt_work);
375*4882a593Smuzhiyun 		}
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
intel_pstate_get_cppc_guranteed(int cpu)379*4882a593Smuzhiyun static int intel_pstate_get_cppc_guranteed(int cpu)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	struct cppc_perf_caps cppc_perf;
382*4882a593Smuzhiyun 	int ret;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	ret = cppc_get_perf_caps(cpu, &cppc_perf);
385*4882a593Smuzhiyun 	if (ret)
386*4882a593Smuzhiyun 		return ret;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	if (cppc_perf.guaranteed_perf)
389*4882a593Smuzhiyun 		return cppc_perf.guaranteed_perf;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	return cppc_perf.nominal_perf;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun #else /* CONFIG_ACPI_CPPC_LIB */
intel_pstate_set_itmt_prio(int cpu)395*4882a593Smuzhiyun static void intel_pstate_set_itmt_prio(int cpu)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun #endif /* CONFIG_ACPI_CPPC_LIB */
399*4882a593Smuzhiyun 
intel_pstate_init_acpi_perf_limits(struct cpufreq_policy * policy)400*4882a593Smuzhiyun static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	struct cpudata *cpu;
403*4882a593Smuzhiyun 	int ret;
404*4882a593Smuzhiyun 	int i;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	if (hwp_active) {
407*4882a593Smuzhiyun 		intel_pstate_set_itmt_prio(policy->cpu);
408*4882a593Smuzhiyun 		return;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	if (!intel_pstate_get_ppc_enable_status())
412*4882a593Smuzhiyun 		return;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	cpu = all_cpu_data[policy->cpu];
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
417*4882a593Smuzhiyun 						  policy->cpu);
418*4882a593Smuzhiyun 	if (ret)
419*4882a593Smuzhiyun 		return;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/*
422*4882a593Smuzhiyun 	 * Check if the control value in _PSS is for PERF_CTL MSR, which should
423*4882a593Smuzhiyun 	 * guarantee that the states returned by it map to the states in our
424*4882a593Smuzhiyun 	 * list directly.
425*4882a593Smuzhiyun 	 */
426*4882a593Smuzhiyun 	if (cpu->acpi_perf_data.control_register.space_id !=
427*4882a593Smuzhiyun 						ACPI_ADR_SPACE_FIXED_HARDWARE)
428*4882a593Smuzhiyun 		goto err;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	/*
431*4882a593Smuzhiyun 	 * If there is only one entry _PSS, simply ignore _PSS and continue as
432*4882a593Smuzhiyun 	 * usual without taking _PSS into account
433*4882a593Smuzhiyun 	 */
434*4882a593Smuzhiyun 	if (cpu->acpi_perf_data.state_count < 2)
435*4882a593Smuzhiyun 		goto err;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
438*4882a593Smuzhiyun 	for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
439*4882a593Smuzhiyun 		pr_debug("     %cP%d: %u MHz, %u mW, 0x%x\n",
440*4882a593Smuzhiyun 			 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
441*4882a593Smuzhiyun 			 (u32) cpu->acpi_perf_data.states[i].core_frequency,
442*4882a593Smuzhiyun 			 (u32) cpu->acpi_perf_data.states[i].power,
443*4882a593Smuzhiyun 			 (u32) cpu->acpi_perf_data.states[i].control);
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/*
447*4882a593Smuzhiyun 	 * The _PSS table doesn't contain whole turbo frequency range.
448*4882a593Smuzhiyun 	 * This just contains +1 MHZ above the max non turbo frequency,
449*4882a593Smuzhiyun 	 * with control value corresponding to max turbo ratio. But
450*4882a593Smuzhiyun 	 * when cpufreq set policy is called, it will call with this
451*4882a593Smuzhiyun 	 * max frequency, which will cause a reduced performance as
452*4882a593Smuzhiyun 	 * this driver uses real max turbo frequency as the max
453*4882a593Smuzhiyun 	 * frequency. So correct this frequency in _PSS table to
454*4882a593Smuzhiyun 	 * correct max turbo frequency based on the turbo state.
455*4882a593Smuzhiyun 	 * Also need to convert to MHz as _PSS freq is in MHz.
456*4882a593Smuzhiyun 	 */
457*4882a593Smuzhiyun 	if (!global.turbo_disabled)
458*4882a593Smuzhiyun 		cpu->acpi_perf_data.states[0].core_frequency =
459*4882a593Smuzhiyun 					policy->cpuinfo.max_freq / 1000;
460*4882a593Smuzhiyun 	cpu->valid_pss_table = true;
461*4882a593Smuzhiyun 	pr_debug("_PPC limits will be enforced\n");
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	return;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun  err:
466*4882a593Smuzhiyun 	cpu->valid_pss_table = false;
467*4882a593Smuzhiyun 	acpi_processor_unregister_performance(policy->cpu);
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
intel_pstate_exit_perf_limits(struct cpufreq_policy * policy)470*4882a593Smuzhiyun static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	struct cpudata *cpu;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	cpu = all_cpu_data[policy->cpu];
475*4882a593Smuzhiyun 	if (!cpu->valid_pss_table)
476*4882a593Smuzhiyun 		return;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	acpi_processor_unregister_performance(policy->cpu);
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun #else /* CONFIG_ACPI */
intel_pstate_init_acpi_perf_limits(struct cpufreq_policy * policy)481*4882a593Smuzhiyun static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
intel_pstate_exit_perf_limits(struct cpufreq_policy * policy)485*4882a593Smuzhiyun static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
intel_pstate_acpi_pm_profile_server(void)489*4882a593Smuzhiyun static inline bool intel_pstate_acpi_pm_profile_server(void)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun 	return false;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun #endif /* CONFIG_ACPI */
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun #ifndef CONFIG_ACPI_CPPC_LIB
intel_pstate_get_cppc_guranteed(int cpu)496*4882a593Smuzhiyun static int intel_pstate_get_cppc_guranteed(int cpu)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun 	return -ENOTSUPP;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun #endif /* CONFIG_ACPI_CPPC_LIB */
501*4882a593Smuzhiyun 
update_turbo_state(void)502*4882a593Smuzhiyun static inline void update_turbo_state(void)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	u64 misc_en;
505*4882a593Smuzhiyun 	struct cpudata *cpu;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	cpu = all_cpu_data[0];
508*4882a593Smuzhiyun 	rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
509*4882a593Smuzhiyun 	global.turbo_disabled =
510*4882a593Smuzhiyun 		(misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
511*4882a593Smuzhiyun 		 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
min_perf_pct_min(void)514*4882a593Smuzhiyun static int min_perf_pct_min(void)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[0];
517*4882a593Smuzhiyun 	int turbo_pstate = cpu->pstate.turbo_pstate;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	return turbo_pstate ?
520*4882a593Smuzhiyun 		(cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
intel_pstate_get_epb(struct cpudata * cpu_data)523*4882a593Smuzhiyun static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	u64 epb;
526*4882a593Smuzhiyun 	int ret;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	if (!boot_cpu_has(X86_FEATURE_EPB))
529*4882a593Smuzhiyun 		return -ENXIO;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
532*4882a593Smuzhiyun 	if (ret)
533*4882a593Smuzhiyun 		return (s16)ret;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	return (s16)(epb & 0x0f);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun 
intel_pstate_get_epp(struct cpudata * cpu_data,u64 hwp_req_data)538*4882a593Smuzhiyun static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	s16 epp;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
543*4882a593Smuzhiyun 		/*
544*4882a593Smuzhiyun 		 * When hwp_req_data is 0, means that caller didn't read
545*4882a593Smuzhiyun 		 * MSR_HWP_REQUEST, so need to read and get EPP.
546*4882a593Smuzhiyun 		 */
547*4882a593Smuzhiyun 		if (!hwp_req_data) {
548*4882a593Smuzhiyun 			epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
549*4882a593Smuzhiyun 					    &hwp_req_data);
550*4882a593Smuzhiyun 			if (epp)
551*4882a593Smuzhiyun 				return epp;
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 		epp = (hwp_req_data >> 24) & 0xff;
554*4882a593Smuzhiyun 	} else {
555*4882a593Smuzhiyun 		/* When there is no EPP present, HWP uses EPB settings */
556*4882a593Smuzhiyun 		epp = intel_pstate_get_epb(cpu_data);
557*4882a593Smuzhiyun 	}
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	return epp;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun 
intel_pstate_set_epb(int cpu,s16 pref)562*4882a593Smuzhiyun static int intel_pstate_set_epb(int cpu, s16 pref)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	u64 epb;
565*4882a593Smuzhiyun 	int ret;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	if (!boot_cpu_has(X86_FEATURE_EPB))
568*4882a593Smuzhiyun 		return -ENXIO;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
571*4882a593Smuzhiyun 	if (ret)
572*4882a593Smuzhiyun 		return ret;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	epb = (epb & ~0x0f) | pref;
575*4882a593Smuzhiyun 	wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	return 0;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun /*
581*4882a593Smuzhiyun  * EPP/EPB display strings corresponding to EPP index in the
582*4882a593Smuzhiyun  * energy_perf_strings[]
583*4882a593Smuzhiyun  *	index		String
584*4882a593Smuzhiyun  *-------------------------------------
585*4882a593Smuzhiyun  *	0		default
586*4882a593Smuzhiyun  *	1		performance
587*4882a593Smuzhiyun  *	2		balance_performance
588*4882a593Smuzhiyun  *	3		balance_power
589*4882a593Smuzhiyun  *	4		power
590*4882a593Smuzhiyun  */
591*4882a593Smuzhiyun static const char * const energy_perf_strings[] = {
592*4882a593Smuzhiyun 	"default",
593*4882a593Smuzhiyun 	"performance",
594*4882a593Smuzhiyun 	"balance_performance",
595*4882a593Smuzhiyun 	"balance_power",
596*4882a593Smuzhiyun 	"power",
597*4882a593Smuzhiyun 	NULL
598*4882a593Smuzhiyun };
599*4882a593Smuzhiyun static const unsigned int epp_values[] = {
600*4882a593Smuzhiyun 	HWP_EPP_PERFORMANCE,
601*4882a593Smuzhiyun 	HWP_EPP_BALANCE_PERFORMANCE,
602*4882a593Smuzhiyun 	HWP_EPP_BALANCE_POWERSAVE,
603*4882a593Smuzhiyun 	HWP_EPP_POWERSAVE
604*4882a593Smuzhiyun };
605*4882a593Smuzhiyun 
intel_pstate_get_energy_pref_index(struct cpudata * cpu_data,int * raw_epp)606*4882a593Smuzhiyun static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun 	s16 epp;
609*4882a593Smuzhiyun 	int index = -EINVAL;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	*raw_epp = 0;
612*4882a593Smuzhiyun 	epp = intel_pstate_get_epp(cpu_data, 0);
613*4882a593Smuzhiyun 	if (epp < 0)
614*4882a593Smuzhiyun 		return epp;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
617*4882a593Smuzhiyun 		if (epp == HWP_EPP_PERFORMANCE)
618*4882a593Smuzhiyun 			return 1;
619*4882a593Smuzhiyun 		if (epp == HWP_EPP_BALANCE_PERFORMANCE)
620*4882a593Smuzhiyun 			return 2;
621*4882a593Smuzhiyun 		if (epp == HWP_EPP_BALANCE_POWERSAVE)
622*4882a593Smuzhiyun 			return 3;
623*4882a593Smuzhiyun 		if (epp == HWP_EPP_POWERSAVE)
624*4882a593Smuzhiyun 			return 4;
625*4882a593Smuzhiyun 		*raw_epp = epp;
626*4882a593Smuzhiyun 		return 0;
627*4882a593Smuzhiyun 	} else if (boot_cpu_has(X86_FEATURE_EPB)) {
628*4882a593Smuzhiyun 		/*
629*4882a593Smuzhiyun 		 * Range:
630*4882a593Smuzhiyun 		 *	0x00-0x03	:	Performance
631*4882a593Smuzhiyun 		 *	0x04-0x07	:	Balance performance
632*4882a593Smuzhiyun 		 *	0x08-0x0B	:	Balance power
633*4882a593Smuzhiyun 		 *	0x0C-0x0F	:	Power
634*4882a593Smuzhiyun 		 * The EPB is a 4 bit value, but our ranges restrict the
635*4882a593Smuzhiyun 		 * value which can be set. Here only using top two bits
636*4882a593Smuzhiyun 		 * effectively.
637*4882a593Smuzhiyun 		 */
638*4882a593Smuzhiyun 		index = (epp >> 2) + 1;
639*4882a593Smuzhiyun 	}
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	return index;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun 
intel_pstate_set_epp(struct cpudata * cpu,u32 epp)644*4882a593Smuzhiyun static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	int ret;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	/*
649*4882a593Smuzhiyun 	 * Use the cached HWP Request MSR value, because in the active mode the
650*4882a593Smuzhiyun 	 * register itself may be updated by intel_pstate_hwp_boost_up() or
651*4882a593Smuzhiyun 	 * intel_pstate_hwp_boost_down() at any time.
652*4882a593Smuzhiyun 	 */
653*4882a593Smuzhiyun 	u64 value = READ_ONCE(cpu->hwp_req_cached);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	value &= ~GENMASK_ULL(31, 24);
656*4882a593Smuzhiyun 	value |= (u64)epp << 24;
657*4882a593Smuzhiyun 	/*
658*4882a593Smuzhiyun 	 * The only other updater of hwp_req_cached in the active mode,
659*4882a593Smuzhiyun 	 * intel_pstate_hwp_set(), is called under the same lock as this
660*4882a593Smuzhiyun 	 * function, so it cannot run in parallel with the update below.
661*4882a593Smuzhiyun 	 */
662*4882a593Smuzhiyun 	WRITE_ONCE(cpu->hwp_req_cached, value);
663*4882a593Smuzhiyun 	ret = wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
664*4882a593Smuzhiyun 	if (!ret)
665*4882a593Smuzhiyun 		cpu->epp_cached = epp;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	return ret;
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun 
intel_pstate_set_energy_pref_index(struct cpudata * cpu_data,int pref_index,bool use_raw,u32 raw_epp)670*4882a593Smuzhiyun static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
671*4882a593Smuzhiyun 					      int pref_index, bool use_raw,
672*4882a593Smuzhiyun 					      u32 raw_epp)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun 	int epp = -EINVAL;
675*4882a593Smuzhiyun 	int ret;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (!pref_index)
678*4882a593Smuzhiyun 		epp = cpu_data->epp_default;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
681*4882a593Smuzhiyun 		if (use_raw)
682*4882a593Smuzhiyun 			epp = raw_epp;
683*4882a593Smuzhiyun 		else if (epp == -EINVAL)
684*4882a593Smuzhiyun 			epp = epp_values[pref_index - 1];
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 		/*
687*4882a593Smuzhiyun 		 * To avoid confusion, refuse to set EPP to any values different
688*4882a593Smuzhiyun 		 * from 0 (performance) if the current policy is "performance",
689*4882a593Smuzhiyun 		 * because those values would be overridden.
690*4882a593Smuzhiyun 		 */
691*4882a593Smuzhiyun 		if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
692*4882a593Smuzhiyun 			return -EBUSY;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 		ret = intel_pstate_set_epp(cpu_data, epp);
695*4882a593Smuzhiyun 	} else {
696*4882a593Smuzhiyun 		if (epp == -EINVAL)
697*4882a593Smuzhiyun 			epp = (pref_index - 1) << 2;
698*4882a593Smuzhiyun 		ret = intel_pstate_set_epb(cpu_data->cpu, epp);
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	return ret;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun 
show_energy_performance_available_preferences(struct cpufreq_policy * policy,char * buf)704*4882a593Smuzhiyun static ssize_t show_energy_performance_available_preferences(
705*4882a593Smuzhiyun 				struct cpufreq_policy *policy, char *buf)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	int i = 0;
708*4882a593Smuzhiyun 	int ret = 0;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	while (energy_perf_strings[i] != NULL)
711*4882a593Smuzhiyun 		ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	ret += sprintf(&buf[ret], "\n");
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	return ret;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun cpufreq_freq_attr_ro(energy_performance_available_preferences);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun static struct cpufreq_driver intel_pstate;
721*4882a593Smuzhiyun 
store_energy_performance_preference(struct cpufreq_policy * policy,const char * buf,size_t count)722*4882a593Smuzhiyun static ssize_t store_energy_performance_preference(
723*4882a593Smuzhiyun 		struct cpufreq_policy *policy, const char *buf, size_t count)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
726*4882a593Smuzhiyun 	char str_preference[21];
727*4882a593Smuzhiyun 	bool raw = false;
728*4882a593Smuzhiyun 	ssize_t ret;
729*4882a593Smuzhiyun 	u32 epp = 0;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	ret = sscanf(buf, "%20s", str_preference);
732*4882a593Smuzhiyun 	if (ret != 1)
733*4882a593Smuzhiyun 		return -EINVAL;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	ret = match_string(energy_perf_strings, -1, str_preference);
736*4882a593Smuzhiyun 	if (ret < 0) {
737*4882a593Smuzhiyun 		if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
738*4882a593Smuzhiyun 			return ret;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 		ret = kstrtouint(buf, 10, &epp);
741*4882a593Smuzhiyun 		if (ret)
742*4882a593Smuzhiyun 			return ret;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 		if (epp > 255)
745*4882a593Smuzhiyun 			return -EINVAL;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 		raw = true;
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/*
751*4882a593Smuzhiyun 	 * This function runs with the policy R/W semaphore held, which
752*4882a593Smuzhiyun 	 * guarantees that the driver pointer will not change while it is
753*4882a593Smuzhiyun 	 * running.
754*4882a593Smuzhiyun 	 */
755*4882a593Smuzhiyun 	if (!intel_pstate_driver)
756*4882a593Smuzhiyun 		return -EAGAIN;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_limits_lock);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	if (intel_pstate_driver == &intel_pstate) {
761*4882a593Smuzhiyun 		ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp);
762*4882a593Smuzhiyun 	} else {
763*4882a593Smuzhiyun 		/*
764*4882a593Smuzhiyun 		 * In the passive mode the governor needs to be stopped on the
765*4882a593Smuzhiyun 		 * target CPU before the EPP update and restarted after it,
766*4882a593Smuzhiyun 		 * which is super-heavy-weight, so make sure it is worth doing
767*4882a593Smuzhiyun 		 * upfront.
768*4882a593Smuzhiyun 		 */
769*4882a593Smuzhiyun 		if (!raw)
770*4882a593Smuzhiyun 			epp = ret ? epp_values[ret - 1] : cpu->epp_default;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		if (cpu->epp_cached != epp) {
773*4882a593Smuzhiyun 			int err;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 			cpufreq_stop_governor(policy);
776*4882a593Smuzhiyun 			ret = intel_pstate_set_epp(cpu, epp);
777*4882a593Smuzhiyun 			err = cpufreq_start_governor(policy);
778*4882a593Smuzhiyun 			if (!ret)
779*4882a593Smuzhiyun 				ret = err;
780*4882a593Smuzhiyun 		}
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_limits_lock);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	return ret ?: count;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun 
show_energy_performance_preference(struct cpufreq_policy * policy,char * buf)788*4882a593Smuzhiyun static ssize_t show_energy_performance_preference(
789*4882a593Smuzhiyun 				struct cpufreq_policy *policy, char *buf)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun 	struct cpudata *cpu_data = all_cpu_data[policy->cpu];
792*4882a593Smuzhiyun 	int preference, raw_epp;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
795*4882a593Smuzhiyun 	if (preference < 0)
796*4882a593Smuzhiyun 		return preference;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	if (raw_epp)
799*4882a593Smuzhiyun 		return  sprintf(buf, "%d\n", raw_epp);
800*4882a593Smuzhiyun 	else
801*4882a593Smuzhiyun 		return  sprintf(buf, "%s\n", energy_perf_strings[preference]);
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun cpufreq_freq_attr_rw(energy_performance_preference);
805*4882a593Smuzhiyun 
show_base_frequency(struct cpufreq_policy * policy,char * buf)806*4882a593Smuzhiyun static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	struct cpudata *cpu;
809*4882a593Smuzhiyun 	u64 cap;
810*4882a593Smuzhiyun 	int ratio;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	ratio = intel_pstate_get_cppc_guranteed(policy->cpu);
813*4882a593Smuzhiyun 	if (ratio <= 0) {
814*4882a593Smuzhiyun 		rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
815*4882a593Smuzhiyun 		ratio = HWP_GUARANTEED_PERF(cap);
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	cpu = all_cpu_data[policy->cpu];
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", ratio * cpu->pstate.scaling);
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun cpufreq_freq_attr_ro(base_frequency);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun static struct freq_attr *hwp_cpufreq_attrs[] = {
826*4882a593Smuzhiyun 	&energy_performance_preference,
827*4882a593Smuzhiyun 	&energy_performance_available_preferences,
828*4882a593Smuzhiyun 	&base_frequency,
829*4882a593Smuzhiyun 	NULL,
830*4882a593Smuzhiyun };
831*4882a593Smuzhiyun 
intel_pstate_get_hwp_max(struct cpudata * cpu,int * phy_max,int * current_max)832*4882a593Smuzhiyun static void intel_pstate_get_hwp_max(struct cpudata *cpu, int *phy_max,
833*4882a593Smuzhiyun 				     int *current_max)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	u64 cap;
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	rdmsrl_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap);
838*4882a593Smuzhiyun 	WRITE_ONCE(cpu->hwp_cap_cached, cap);
839*4882a593Smuzhiyun 	if (global.no_turbo || global.turbo_disabled)
840*4882a593Smuzhiyun 		*current_max = HWP_GUARANTEED_PERF(cap);
841*4882a593Smuzhiyun 	else
842*4882a593Smuzhiyun 		*current_max = HWP_HIGHEST_PERF(cap);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	*phy_max = HWP_HIGHEST_PERF(cap);
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun 
intel_pstate_hwp_set(unsigned int cpu)847*4882a593Smuzhiyun static void intel_pstate_hwp_set(unsigned int cpu)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	struct cpudata *cpu_data = all_cpu_data[cpu];
850*4882a593Smuzhiyun 	int max, min;
851*4882a593Smuzhiyun 	u64 value;
852*4882a593Smuzhiyun 	s16 epp;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	max = cpu_data->max_perf_ratio;
855*4882a593Smuzhiyun 	min = cpu_data->min_perf_ratio;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
858*4882a593Smuzhiyun 		min = max;
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	value &= ~HWP_MIN_PERF(~0L);
863*4882a593Smuzhiyun 	value |= HWP_MIN_PERF(min);
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	value &= ~HWP_MAX_PERF(~0L);
866*4882a593Smuzhiyun 	value |= HWP_MAX_PERF(max);
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	if (cpu_data->epp_policy == cpu_data->policy)
869*4882a593Smuzhiyun 		goto skip_epp;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	cpu_data->epp_policy = cpu_data->policy;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
874*4882a593Smuzhiyun 		epp = intel_pstate_get_epp(cpu_data, value);
875*4882a593Smuzhiyun 		cpu_data->epp_powersave = epp;
876*4882a593Smuzhiyun 		/* If EPP read was failed, then don't try to write */
877*4882a593Smuzhiyun 		if (epp < 0)
878*4882a593Smuzhiyun 			goto skip_epp;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 		epp = 0;
881*4882a593Smuzhiyun 	} else {
882*4882a593Smuzhiyun 		/* skip setting EPP, when saved value is invalid */
883*4882a593Smuzhiyun 		if (cpu_data->epp_powersave < 0)
884*4882a593Smuzhiyun 			goto skip_epp;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 		/*
887*4882a593Smuzhiyun 		 * No need to restore EPP when it is not zero. This
888*4882a593Smuzhiyun 		 * means:
889*4882a593Smuzhiyun 		 *  - Policy is not changed
890*4882a593Smuzhiyun 		 *  - user has manually changed
891*4882a593Smuzhiyun 		 *  - Error reading EPB
892*4882a593Smuzhiyun 		 */
893*4882a593Smuzhiyun 		epp = intel_pstate_get_epp(cpu_data, value);
894*4882a593Smuzhiyun 		if (epp)
895*4882a593Smuzhiyun 			goto skip_epp;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		epp = cpu_data->epp_powersave;
898*4882a593Smuzhiyun 	}
899*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
900*4882a593Smuzhiyun 		value &= ~GENMASK_ULL(31, 24);
901*4882a593Smuzhiyun 		value |= (u64)epp << 24;
902*4882a593Smuzhiyun 	} else {
903*4882a593Smuzhiyun 		intel_pstate_set_epb(cpu, epp);
904*4882a593Smuzhiyun 	}
905*4882a593Smuzhiyun skip_epp:
906*4882a593Smuzhiyun 	WRITE_ONCE(cpu_data->hwp_req_cached, value);
907*4882a593Smuzhiyun 	wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
intel_pstate_hwp_offline(struct cpudata * cpu)910*4882a593Smuzhiyun static void intel_pstate_hwp_offline(struct cpudata *cpu)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	u64 value = READ_ONCE(cpu->hwp_req_cached);
913*4882a593Smuzhiyun 	int min_perf;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
916*4882a593Smuzhiyun 		/*
917*4882a593Smuzhiyun 		 * In case the EPP has been set to "performance" by the
918*4882a593Smuzhiyun 		 * active mode "performance" scaling algorithm, replace that
919*4882a593Smuzhiyun 		 * temporary value with the cached EPP one.
920*4882a593Smuzhiyun 		 */
921*4882a593Smuzhiyun 		value &= ~GENMASK_ULL(31, 24);
922*4882a593Smuzhiyun 		value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached);
923*4882a593Smuzhiyun 		WRITE_ONCE(cpu->hwp_req_cached, value);
924*4882a593Smuzhiyun 	}
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	value &= ~GENMASK_ULL(31, 0);
927*4882a593Smuzhiyun 	min_perf = HWP_LOWEST_PERF(cpu->hwp_cap_cached);
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	/* Set hwp_max = hwp_min */
930*4882a593Smuzhiyun 	value |= HWP_MAX_PERF(min_perf);
931*4882a593Smuzhiyun 	value |= HWP_MIN_PERF(min_perf);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	/* Set EPP to min */
934*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_EPP))
935*4882a593Smuzhiyun 		value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun #define POWER_CTL_EE_ENABLE	1
941*4882a593Smuzhiyun #define POWER_CTL_EE_DISABLE	2
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun static int power_ctl_ee_state;
944*4882a593Smuzhiyun 
set_power_ctl_ee_state(bool input)945*4882a593Smuzhiyun static void set_power_ctl_ee_state(bool input)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun 	u64 power_ctl;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
950*4882a593Smuzhiyun 	rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
951*4882a593Smuzhiyun 	if (input) {
952*4882a593Smuzhiyun 		power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
953*4882a593Smuzhiyun 		power_ctl_ee_state = POWER_CTL_EE_ENABLE;
954*4882a593Smuzhiyun 	} else {
955*4882a593Smuzhiyun 		power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
956*4882a593Smuzhiyun 		power_ctl_ee_state = POWER_CTL_EE_DISABLE;
957*4882a593Smuzhiyun 	}
958*4882a593Smuzhiyun 	wrmsrl(MSR_IA32_POWER_CTL, power_ctl);
959*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun static void intel_pstate_hwp_enable(struct cpudata *cpudata);
963*4882a593Smuzhiyun 
intel_pstate_hwp_reenable(struct cpudata * cpu)964*4882a593Smuzhiyun static void intel_pstate_hwp_reenable(struct cpudata *cpu)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	intel_pstate_hwp_enable(cpu);
967*4882a593Smuzhiyun 	wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached));
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
intel_pstate_suspend(struct cpufreq_policy * policy)970*4882a593Smuzhiyun static int intel_pstate_suspend(struct cpufreq_policy *policy)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	pr_debug("CPU %d suspending\n", cpu->cpu);
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	cpu->suspended = true;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun 
intel_pstate_resume(struct cpufreq_policy * policy)981*4882a593Smuzhiyun static int intel_pstate_resume(struct cpufreq_policy *policy)
982*4882a593Smuzhiyun {
983*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	pr_debug("CPU %d resuming\n", cpu->cpu);
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	/* Only restore if the system default is changed */
988*4882a593Smuzhiyun 	if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
989*4882a593Smuzhiyun 		set_power_ctl_ee_state(true);
990*4882a593Smuzhiyun 	else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
991*4882a593Smuzhiyun 		set_power_ctl_ee_state(false);
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	if (cpu->suspended && hwp_active) {
994*4882a593Smuzhiyun 		mutex_lock(&intel_pstate_limits_lock);
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 		/* Re-enable HWP, because "online" has not done that. */
997*4882a593Smuzhiyun 		intel_pstate_hwp_reenable(cpu);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_limits_lock);
1000*4882a593Smuzhiyun 	}
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	cpu->suspended = false;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	return 0;
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun 
intel_pstate_update_policies(void)1007*4882a593Smuzhiyun static void intel_pstate_update_policies(void)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun 	int cpu;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	for_each_possible_cpu(cpu)
1012*4882a593Smuzhiyun 		cpufreq_update_policy(cpu);
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun 
intel_pstate_update_max_freq(unsigned int cpu)1015*4882a593Smuzhiyun static void intel_pstate_update_max_freq(unsigned int cpu)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
1018*4882a593Smuzhiyun 	struct cpudata *cpudata;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	if (!policy)
1021*4882a593Smuzhiyun 		return;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	cpudata = all_cpu_data[cpu];
1024*4882a593Smuzhiyun 	policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
1025*4882a593Smuzhiyun 			cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	refresh_frequency_limits(policy);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	cpufreq_cpu_release(policy);
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun 
intel_pstate_update_limits(unsigned int cpu)1032*4882a593Smuzhiyun static void intel_pstate_update_limits(unsigned int cpu)
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	update_turbo_state();
1037*4882a593Smuzhiyun 	/*
1038*4882a593Smuzhiyun 	 * If turbo has been turned on or off globally, policy limits for
1039*4882a593Smuzhiyun 	 * all CPUs need to be updated to reflect that.
1040*4882a593Smuzhiyun 	 */
1041*4882a593Smuzhiyun 	if (global.turbo_disabled_mf != global.turbo_disabled) {
1042*4882a593Smuzhiyun 		global.turbo_disabled_mf = global.turbo_disabled;
1043*4882a593Smuzhiyun 		arch_set_max_freq_ratio(global.turbo_disabled);
1044*4882a593Smuzhiyun 		for_each_possible_cpu(cpu)
1045*4882a593Smuzhiyun 			intel_pstate_update_max_freq(cpu);
1046*4882a593Smuzhiyun 	} else {
1047*4882a593Smuzhiyun 		cpufreq_update_policy(cpu);
1048*4882a593Smuzhiyun 	}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun /************************** sysfs begin ************************/
1054*4882a593Smuzhiyun #define show_one(file_name, object)					\
1055*4882a593Smuzhiyun 	static ssize_t show_##file_name					\
1056*4882a593Smuzhiyun 	(struct kobject *kobj, struct kobj_attribute *attr, char *buf)	\
1057*4882a593Smuzhiyun 	{								\
1058*4882a593Smuzhiyun 		return sprintf(buf, "%u\n", global.object);		\
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun static ssize_t intel_pstate_show_status(char *buf);
1062*4882a593Smuzhiyun static int intel_pstate_update_status(const char *buf, size_t size);
1063*4882a593Smuzhiyun 
show_status(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1064*4882a593Smuzhiyun static ssize_t show_status(struct kobject *kobj,
1065*4882a593Smuzhiyun 			   struct kobj_attribute *attr, char *buf)
1066*4882a593Smuzhiyun {
1067*4882a593Smuzhiyun 	ssize_t ret;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1070*4882a593Smuzhiyun 	ret = intel_pstate_show_status(buf);
1071*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	return ret;
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun 
store_status(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1076*4882a593Smuzhiyun static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
1077*4882a593Smuzhiyun 			    const char *buf, size_t count)
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun 	char *p = memchr(buf, '\n', count);
1080*4882a593Smuzhiyun 	int ret;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1083*4882a593Smuzhiyun 	ret = intel_pstate_update_status(buf, p ? p - buf : count);
1084*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	return ret < 0 ? ret : count;
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun 
show_turbo_pct(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1089*4882a593Smuzhiyun static ssize_t show_turbo_pct(struct kobject *kobj,
1090*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
1091*4882a593Smuzhiyun {
1092*4882a593Smuzhiyun 	struct cpudata *cpu;
1093*4882a593Smuzhiyun 	int total, no_turbo, turbo_pct;
1094*4882a593Smuzhiyun 	uint32_t turbo_fp;
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	if (!intel_pstate_driver) {
1099*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1100*4882a593Smuzhiyun 		return -EAGAIN;
1101*4882a593Smuzhiyun 	}
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	cpu = all_cpu_data[0];
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1106*4882a593Smuzhiyun 	no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
1107*4882a593Smuzhiyun 	turbo_fp = div_fp(no_turbo, total);
1108*4882a593Smuzhiyun 	turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", turbo_pct);
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun 
show_num_pstates(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1115*4882a593Smuzhiyun static ssize_t show_num_pstates(struct kobject *kobj,
1116*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
1117*4882a593Smuzhiyun {
1118*4882a593Smuzhiyun 	struct cpudata *cpu;
1119*4882a593Smuzhiyun 	int total;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	if (!intel_pstate_driver) {
1124*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1125*4882a593Smuzhiyun 		return -EAGAIN;
1126*4882a593Smuzhiyun 	}
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	cpu = all_cpu_data[0];
1129*4882a593Smuzhiyun 	total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", total);
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun 
show_no_turbo(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1136*4882a593Smuzhiyun static ssize_t show_no_turbo(struct kobject *kobj,
1137*4882a593Smuzhiyun 			     struct kobj_attribute *attr, char *buf)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	ssize_t ret;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	if (!intel_pstate_driver) {
1144*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1145*4882a593Smuzhiyun 		return -EAGAIN;
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	update_turbo_state();
1149*4882a593Smuzhiyun 	if (global.turbo_disabled)
1150*4882a593Smuzhiyun 		ret = sprintf(buf, "%u\n", global.turbo_disabled);
1151*4882a593Smuzhiyun 	else
1152*4882a593Smuzhiyun 		ret = sprintf(buf, "%u\n", global.no_turbo);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	return ret;
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
store_no_turbo(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1159*4882a593Smuzhiyun static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
1160*4882a593Smuzhiyun 			      const char *buf, size_t count)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun 	unsigned int input;
1163*4882a593Smuzhiyun 	int ret;
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	ret = sscanf(buf, "%u", &input);
1166*4882a593Smuzhiyun 	if (ret != 1)
1167*4882a593Smuzhiyun 		return -EINVAL;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	if (!intel_pstate_driver) {
1172*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1173*4882a593Smuzhiyun 		return -EAGAIN;
1174*4882a593Smuzhiyun 	}
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_limits_lock);
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	update_turbo_state();
1179*4882a593Smuzhiyun 	if (global.turbo_disabled) {
1180*4882a593Smuzhiyun 		pr_notice_once("Turbo disabled by BIOS or unavailable on processor\n");
1181*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_limits_lock);
1182*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1183*4882a593Smuzhiyun 		return -EPERM;
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	global.no_turbo = clamp_t(int, input, 0, 1);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	if (global.no_turbo) {
1189*4882a593Smuzhiyun 		struct cpudata *cpu = all_cpu_data[0];
1190*4882a593Smuzhiyun 		int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 		/* Squash the global minimum into the permitted range. */
1193*4882a593Smuzhiyun 		if (global.min_perf_pct > pct)
1194*4882a593Smuzhiyun 			global.min_perf_pct = pct;
1195*4882a593Smuzhiyun 	}
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_limits_lock);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	intel_pstate_update_policies();
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	return count;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun 
update_qos_request(enum freq_qos_req_type type)1206*4882a593Smuzhiyun static void update_qos_request(enum freq_qos_req_type type)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	int max_state, turbo_max, freq, i, perf_pct;
1209*4882a593Smuzhiyun 	struct freq_qos_request *req;
1210*4882a593Smuzhiyun 	struct cpufreq_policy *policy;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	for_each_possible_cpu(i) {
1213*4882a593Smuzhiyun 		struct cpudata *cpu = all_cpu_data[i];
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 		policy = cpufreq_cpu_get(i);
1216*4882a593Smuzhiyun 		if (!policy)
1217*4882a593Smuzhiyun 			continue;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 		req = policy->driver_data;
1220*4882a593Smuzhiyun 		cpufreq_cpu_put(policy);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		if (!req)
1223*4882a593Smuzhiyun 			continue;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 		if (hwp_active)
1226*4882a593Smuzhiyun 			intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
1227*4882a593Smuzhiyun 		else
1228*4882a593Smuzhiyun 			turbo_max = cpu->pstate.turbo_pstate;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 		if (type == FREQ_QOS_MIN) {
1231*4882a593Smuzhiyun 			perf_pct = global.min_perf_pct;
1232*4882a593Smuzhiyun 		} else {
1233*4882a593Smuzhiyun 			req++;
1234*4882a593Smuzhiyun 			perf_pct = global.max_perf_pct;
1235*4882a593Smuzhiyun 		}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 		freq = DIV_ROUND_UP(turbo_max * perf_pct, 100);
1238*4882a593Smuzhiyun 		freq *= cpu->pstate.scaling;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 		if (freq_qos_update_request(req, freq) < 0)
1241*4882a593Smuzhiyun 			pr_warn("Failed to update freq constraint: CPU%d\n", i);
1242*4882a593Smuzhiyun 	}
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun 
store_max_perf_pct(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1245*4882a593Smuzhiyun static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
1246*4882a593Smuzhiyun 				  const char *buf, size_t count)
1247*4882a593Smuzhiyun {
1248*4882a593Smuzhiyun 	unsigned int input;
1249*4882a593Smuzhiyun 	int ret;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	ret = sscanf(buf, "%u", &input);
1252*4882a593Smuzhiyun 	if (ret != 1)
1253*4882a593Smuzhiyun 		return -EINVAL;
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	if (!intel_pstate_driver) {
1258*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1259*4882a593Smuzhiyun 		return -EAGAIN;
1260*4882a593Smuzhiyun 	}
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_limits_lock);
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_limits_lock);
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	if (intel_pstate_driver == &intel_pstate)
1269*4882a593Smuzhiyun 		intel_pstate_update_policies();
1270*4882a593Smuzhiyun 	else
1271*4882a593Smuzhiyun 		update_qos_request(FREQ_QOS_MAX);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	return count;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
store_min_perf_pct(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1278*4882a593Smuzhiyun static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
1279*4882a593Smuzhiyun 				  const char *buf, size_t count)
1280*4882a593Smuzhiyun {
1281*4882a593Smuzhiyun 	unsigned int input;
1282*4882a593Smuzhiyun 	int ret;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	ret = sscanf(buf, "%u", &input);
1285*4882a593Smuzhiyun 	if (ret != 1)
1286*4882a593Smuzhiyun 		return -EINVAL;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	if (!intel_pstate_driver) {
1291*4882a593Smuzhiyun 		mutex_unlock(&intel_pstate_driver_lock);
1292*4882a593Smuzhiyun 		return -EAGAIN;
1293*4882a593Smuzhiyun 	}
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_limits_lock);
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	global.min_perf_pct = clamp_t(int, input,
1298*4882a593Smuzhiyun 				      min_perf_pct_min(), global.max_perf_pct);
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_limits_lock);
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	if (intel_pstate_driver == &intel_pstate)
1303*4882a593Smuzhiyun 		intel_pstate_update_policies();
1304*4882a593Smuzhiyun 	else
1305*4882a593Smuzhiyun 		update_qos_request(FREQ_QOS_MIN);
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	return count;
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun 
show_hwp_dynamic_boost(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1312*4882a593Smuzhiyun static ssize_t show_hwp_dynamic_boost(struct kobject *kobj,
1313*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
1314*4882a593Smuzhiyun {
1315*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", hwp_boost);
1316*4882a593Smuzhiyun }
1317*4882a593Smuzhiyun 
store_hwp_dynamic_boost(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1318*4882a593Smuzhiyun static ssize_t store_hwp_dynamic_boost(struct kobject *a,
1319*4882a593Smuzhiyun 				       struct kobj_attribute *b,
1320*4882a593Smuzhiyun 				       const char *buf, size_t count)
1321*4882a593Smuzhiyun {
1322*4882a593Smuzhiyun 	unsigned int input;
1323*4882a593Smuzhiyun 	int ret;
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	ret = kstrtouint(buf, 10, &input);
1326*4882a593Smuzhiyun 	if (ret)
1327*4882a593Smuzhiyun 		return ret;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
1330*4882a593Smuzhiyun 	hwp_boost = !!input;
1331*4882a593Smuzhiyun 	intel_pstate_update_policies();
1332*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	return count;
1335*4882a593Smuzhiyun }
1336*4882a593Smuzhiyun 
show_energy_efficiency(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1337*4882a593Smuzhiyun static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
1338*4882a593Smuzhiyun 				      char *buf)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun 	u64 power_ctl;
1341*4882a593Smuzhiyun 	int enable;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
1344*4882a593Smuzhiyun 	enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
1345*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", !enable);
1346*4882a593Smuzhiyun }
1347*4882a593Smuzhiyun 
store_energy_efficiency(struct kobject * a,struct kobj_attribute * b,const char * buf,size_t count)1348*4882a593Smuzhiyun static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
1349*4882a593Smuzhiyun 				       const char *buf, size_t count)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun 	bool input;
1352*4882a593Smuzhiyun 	int ret;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 	ret = kstrtobool(buf, &input);
1355*4882a593Smuzhiyun 	if (ret)
1356*4882a593Smuzhiyun 		return ret;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	set_power_ctl_ee_state(input);
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 	return count;
1361*4882a593Smuzhiyun }
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun show_one(max_perf_pct, max_perf_pct);
1364*4882a593Smuzhiyun show_one(min_perf_pct, min_perf_pct);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun define_one_global_rw(status);
1367*4882a593Smuzhiyun define_one_global_rw(no_turbo);
1368*4882a593Smuzhiyun define_one_global_rw(max_perf_pct);
1369*4882a593Smuzhiyun define_one_global_rw(min_perf_pct);
1370*4882a593Smuzhiyun define_one_global_ro(turbo_pct);
1371*4882a593Smuzhiyun define_one_global_ro(num_pstates);
1372*4882a593Smuzhiyun define_one_global_rw(hwp_dynamic_boost);
1373*4882a593Smuzhiyun define_one_global_rw(energy_efficiency);
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun static struct attribute *intel_pstate_attributes[] = {
1376*4882a593Smuzhiyun 	&status.attr,
1377*4882a593Smuzhiyun 	&no_turbo.attr,
1378*4882a593Smuzhiyun 	&turbo_pct.attr,
1379*4882a593Smuzhiyun 	&num_pstates.attr,
1380*4882a593Smuzhiyun 	NULL
1381*4882a593Smuzhiyun };
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun static const struct attribute_group intel_pstate_attr_group = {
1384*4882a593Smuzhiyun 	.attrs = intel_pstate_attributes,
1385*4882a593Smuzhiyun };
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun static struct kobject *intel_pstate_kobject;
1390*4882a593Smuzhiyun 
intel_pstate_sysfs_expose_params(void)1391*4882a593Smuzhiyun static void __init intel_pstate_sysfs_expose_params(void)
1392*4882a593Smuzhiyun {
1393*4882a593Smuzhiyun 	int rc;
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 	intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1396*4882a593Smuzhiyun 						&cpu_subsys.dev_root->kobj);
1397*4882a593Smuzhiyun 	if (WARN_ON(!intel_pstate_kobject))
1398*4882a593Smuzhiyun 		return;
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
1401*4882a593Smuzhiyun 	if (WARN_ON(rc))
1402*4882a593Smuzhiyun 		return;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	/*
1405*4882a593Smuzhiyun 	 * If per cpu limits are enforced there are no global limits, so
1406*4882a593Smuzhiyun 	 * return without creating max/min_perf_pct attributes
1407*4882a593Smuzhiyun 	 */
1408*4882a593Smuzhiyun 	if (per_cpu_limits)
1409*4882a593Smuzhiyun 		return;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1412*4882a593Smuzhiyun 	WARN_ON(rc);
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1415*4882a593Smuzhiyun 	WARN_ON(rc);
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
1418*4882a593Smuzhiyun 		rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
1419*4882a593Smuzhiyun 		WARN_ON(rc);
1420*4882a593Smuzhiyun 	}
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun 
intel_pstate_sysfs_remove(void)1423*4882a593Smuzhiyun static void __init intel_pstate_sysfs_remove(void)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	if (!intel_pstate_kobject)
1426*4882a593Smuzhiyun 		return;
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	if (!per_cpu_limits) {
1431*4882a593Smuzhiyun 		sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr);
1432*4882a593Smuzhiyun 		sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr);
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 		if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids))
1435*4882a593Smuzhiyun 			sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr);
1436*4882a593Smuzhiyun 	}
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	kobject_put(intel_pstate_kobject);
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun 
intel_pstate_sysfs_expose_hwp_dynamic_boost(void)1441*4882a593Smuzhiyun static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun 	int rc;
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	if (!hwp_active)
1446*4882a593Smuzhiyun 		return;
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1449*4882a593Smuzhiyun 	WARN_ON_ONCE(rc);
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun 
intel_pstate_sysfs_hide_hwp_dynamic_boost(void)1452*4882a593Smuzhiyun static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	if (!hwp_active)
1455*4882a593Smuzhiyun 		return;
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun /************************** sysfs end ************************/
1461*4882a593Smuzhiyun 
intel_pstate_hwp_enable(struct cpudata * cpudata)1462*4882a593Smuzhiyun static void intel_pstate_hwp_enable(struct cpudata *cpudata)
1463*4882a593Smuzhiyun {
1464*4882a593Smuzhiyun 	/* First disable HWP notification interrupt as we don't process them */
1465*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
1466*4882a593Smuzhiyun 		wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 	wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
1469*4882a593Smuzhiyun 	if (cpudata->epp_default == -EINVAL)
1470*4882a593Smuzhiyun 		cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun 
atom_get_min_pstate(void)1473*4882a593Smuzhiyun static int atom_get_min_pstate(void)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun 	u64 value;
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1478*4882a593Smuzhiyun 	return (value >> 8) & 0x7F;
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun 
atom_get_max_pstate(void)1481*4882a593Smuzhiyun static int atom_get_max_pstate(void)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun 	u64 value;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 	rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1486*4882a593Smuzhiyun 	return (value >> 16) & 0x7F;
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun 
atom_get_turbo_pstate(void)1489*4882a593Smuzhiyun static int atom_get_turbo_pstate(void)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun 	u64 value;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
1494*4882a593Smuzhiyun 	return value & 0x7F;
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun 
atom_get_val(struct cpudata * cpudata,int pstate)1497*4882a593Smuzhiyun static u64 atom_get_val(struct cpudata *cpudata, int pstate)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun 	u64 val;
1500*4882a593Smuzhiyun 	int32_t vid_fp;
1501*4882a593Smuzhiyun 	u32 vid;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	val = (u64)pstate << 8;
1504*4882a593Smuzhiyun 	if (global.no_turbo && !global.turbo_disabled)
1505*4882a593Smuzhiyun 		val |= (u64)1 << 32;
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	vid_fp = cpudata->vid.min + mul_fp(
1508*4882a593Smuzhiyun 		int_tofp(pstate - cpudata->pstate.min_pstate),
1509*4882a593Smuzhiyun 		cpudata->vid.ratio);
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun 	vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
1512*4882a593Smuzhiyun 	vid = ceiling_fp(vid_fp);
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	if (pstate > cpudata->pstate.max_pstate)
1515*4882a593Smuzhiyun 		vid = cpudata->vid.turbo;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	return val | vid;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
silvermont_get_scaling(void)1520*4882a593Smuzhiyun static int silvermont_get_scaling(void)
1521*4882a593Smuzhiyun {
1522*4882a593Smuzhiyun 	u64 value;
1523*4882a593Smuzhiyun 	int i;
1524*4882a593Smuzhiyun 	/* Defined in Table 35-6 from SDM (Sept 2015) */
1525*4882a593Smuzhiyun 	static int silvermont_freq_table[] = {
1526*4882a593Smuzhiyun 		83300, 100000, 133300, 116700, 80000};
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	rdmsrl(MSR_FSB_FREQ, value);
1529*4882a593Smuzhiyun 	i = value & 0x7;
1530*4882a593Smuzhiyun 	WARN_ON(i > 4);
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	return silvermont_freq_table[i];
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun 
airmont_get_scaling(void)1535*4882a593Smuzhiyun static int airmont_get_scaling(void)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun 	u64 value;
1538*4882a593Smuzhiyun 	int i;
1539*4882a593Smuzhiyun 	/* Defined in Table 35-10 from SDM (Sept 2015) */
1540*4882a593Smuzhiyun 	static int airmont_freq_table[] = {
1541*4882a593Smuzhiyun 		83300, 100000, 133300, 116700, 80000,
1542*4882a593Smuzhiyun 		93300, 90000, 88900, 87500};
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	rdmsrl(MSR_FSB_FREQ, value);
1545*4882a593Smuzhiyun 	i = value & 0xF;
1546*4882a593Smuzhiyun 	WARN_ON(i > 8);
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	return airmont_freq_table[i];
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun 
atom_get_vid(struct cpudata * cpudata)1551*4882a593Smuzhiyun static void atom_get_vid(struct cpudata *cpudata)
1552*4882a593Smuzhiyun {
1553*4882a593Smuzhiyun 	u64 value;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	rdmsrl(MSR_ATOM_CORE_VIDS, value);
1556*4882a593Smuzhiyun 	cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1557*4882a593Smuzhiyun 	cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
1558*4882a593Smuzhiyun 	cpudata->vid.ratio = div_fp(
1559*4882a593Smuzhiyun 		cpudata->vid.max - cpudata->vid.min,
1560*4882a593Smuzhiyun 		int_tofp(cpudata->pstate.max_pstate -
1561*4882a593Smuzhiyun 			cpudata->pstate.min_pstate));
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
1564*4882a593Smuzhiyun 	cpudata->vid.turbo = value & 0x7f;
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun 
core_get_min_pstate(void)1567*4882a593Smuzhiyun static int core_get_min_pstate(void)
1568*4882a593Smuzhiyun {
1569*4882a593Smuzhiyun 	u64 value;
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun 	rdmsrl(MSR_PLATFORM_INFO, value);
1572*4882a593Smuzhiyun 	return (value >> 40) & 0xFF;
1573*4882a593Smuzhiyun }
1574*4882a593Smuzhiyun 
core_get_max_pstate_physical(void)1575*4882a593Smuzhiyun static int core_get_max_pstate_physical(void)
1576*4882a593Smuzhiyun {
1577*4882a593Smuzhiyun 	u64 value;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	rdmsrl(MSR_PLATFORM_INFO, value);
1580*4882a593Smuzhiyun 	return (value >> 8) & 0xFF;
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun 
core_get_tdp_ratio(u64 plat_info)1583*4882a593Smuzhiyun static int core_get_tdp_ratio(u64 plat_info)
1584*4882a593Smuzhiyun {
1585*4882a593Smuzhiyun 	/* Check how many TDP levels present */
1586*4882a593Smuzhiyun 	if (plat_info & 0x600000000) {
1587*4882a593Smuzhiyun 		u64 tdp_ctrl;
1588*4882a593Smuzhiyun 		u64 tdp_ratio;
1589*4882a593Smuzhiyun 		int tdp_msr;
1590*4882a593Smuzhiyun 		int err;
1591*4882a593Smuzhiyun 
1592*4882a593Smuzhiyun 		/* Get the TDP level (0, 1, 2) to get ratios */
1593*4882a593Smuzhiyun 		err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1594*4882a593Smuzhiyun 		if (err)
1595*4882a593Smuzhiyun 			return err;
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 		/* TDP MSR are continuous starting at 0x648 */
1598*4882a593Smuzhiyun 		tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1599*4882a593Smuzhiyun 		err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1600*4882a593Smuzhiyun 		if (err)
1601*4882a593Smuzhiyun 			return err;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 		/* For level 1 and 2, bits[23:16] contain the ratio */
1604*4882a593Smuzhiyun 		if (tdp_ctrl & 0x03)
1605*4882a593Smuzhiyun 			tdp_ratio >>= 16;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 		tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1608*4882a593Smuzhiyun 		pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1609*4882a593Smuzhiyun 
1610*4882a593Smuzhiyun 		return (int)tdp_ratio;
1611*4882a593Smuzhiyun 	}
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 	return -ENXIO;
1614*4882a593Smuzhiyun }
1615*4882a593Smuzhiyun 
core_get_max_pstate(void)1616*4882a593Smuzhiyun static int core_get_max_pstate(void)
1617*4882a593Smuzhiyun {
1618*4882a593Smuzhiyun 	u64 tar;
1619*4882a593Smuzhiyun 	u64 plat_info;
1620*4882a593Smuzhiyun 	int max_pstate;
1621*4882a593Smuzhiyun 	int tdp_ratio;
1622*4882a593Smuzhiyun 	int err;
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun 	rdmsrl(MSR_PLATFORM_INFO, plat_info);
1625*4882a593Smuzhiyun 	max_pstate = (plat_info >> 8) & 0xFF;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	tdp_ratio = core_get_tdp_ratio(plat_info);
1628*4882a593Smuzhiyun 	if (tdp_ratio <= 0)
1629*4882a593Smuzhiyun 		return max_pstate;
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	if (hwp_active) {
1632*4882a593Smuzhiyun 		/* Turbo activation ratio is not used on HWP platforms */
1633*4882a593Smuzhiyun 		return tdp_ratio;
1634*4882a593Smuzhiyun 	}
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1637*4882a593Smuzhiyun 	if (!err) {
1638*4882a593Smuzhiyun 		int tar_levels;
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 		/* Do some sanity checking for safety */
1641*4882a593Smuzhiyun 		tar_levels = tar & 0xff;
1642*4882a593Smuzhiyun 		if (tdp_ratio - 1 == tar_levels) {
1643*4882a593Smuzhiyun 			max_pstate = tar_levels;
1644*4882a593Smuzhiyun 			pr_debug("max_pstate=TAC %x\n", max_pstate);
1645*4882a593Smuzhiyun 		}
1646*4882a593Smuzhiyun 	}
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	return max_pstate;
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun 
core_get_turbo_pstate(void)1651*4882a593Smuzhiyun static int core_get_turbo_pstate(void)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun 	u64 value;
1654*4882a593Smuzhiyun 	int nont, ret;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
1657*4882a593Smuzhiyun 	nont = core_get_max_pstate();
1658*4882a593Smuzhiyun 	ret = (value) & 255;
1659*4882a593Smuzhiyun 	if (ret <= nont)
1660*4882a593Smuzhiyun 		ret = nont;
1661*4882a593Smuzhiyun 	return ret;
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun 
core_get_scaling(void)1664*4882a593Smuzhiyun static inline int core_get_scaling(void)
1665*4882a593Smuzhiyun {
1666*4882a593Smuzhiyun 	return 100000;
1667*4882a593Smuzhiyun }
1668*4882a593Smuzhiyun 
core_get_val(struct cpudata * cpudata,int pstate)1669*4882a593Smuzhiyun static u64 core_get_val(struct cpudata *cpudata, int pstate)
1670*4882a593Smuzhiyun {
1671*4882a593Smuzhiyun 	u64 val;
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	val = (u64)pstate << 8;
1674*4882a593Smuzhiyun 	if (global.no_turbo && !global.turbo_disabled)
1675*4882a593Smuzhiyun 		val |= (u64)1 << 32;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	return val;
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun 
knl_get_aperf_mperf_shift(void)1680*4882a593Smuzhiyun static int knl_get_aperf_mperf_shift(void)
1681*4882a593Smuzhiyun {
1682*4882a593Smuzhiyun 	return 10;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun 
knl_get_turbo_pstate(void)1685*4882a593Smuzhiyun static int knl_get_turbo_pstate(void)
1686*4882a593Smuzhiyun {
1687*4882a593Smuzhiyun 	u64 value;
1688*4882a593Smuzhiyun 	int nont, ret;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
1691*4882a593Smuzhiyun 	nont = core_get_max_pstate();
1692*4882a593Smuzhiyun 	ret = (((value) >> 8) & 0xFF);
1693*4882a593Smuzhiyun 	if (ret <= nont)
1694*4882a593Smuzhiyun 		ret = nont;
1695*4882a593Smuzhiyun 	return ret;
1696*4882a593Smuzhiyun }
1697*4882a593Smuzhiyun 
intel_pstate_set_pstate(struct cpudata * cpu,int pstate)1698*4882a593Smuzhiyun static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun 	trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1701*4882a593Smuzhiyun 	cpu->pstate.current_pstate = pstate;
1702*4882a593Smuzhiyun 	/*
1703*4882a593Smuzhiyun 	 * Generally, there is no guarantee that this code will always run on
1704*4882a593Smuzhiyun 	 * the CPU being updated, so force the register update to run on the
1705*4882a593Smuzhiyun 	 * right CPU.
1706*4882a593Smuzhiyun 	 */
1707*4882a593Smuzhiyun 	wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1708*4882a593Smuzhiyun 		      pstate_funcs.get_val(cpu, pstate));
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun 
intel_pstate_set_min_pstate(struct cpudata * cpu)1711*4882a593Smuzhiyun static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1712*4882a593Smuzhiyun {
1713*4882a593Smuzhiyun 	intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1714*4882a593Smuzhiyun }
1715*4882a593Smuzhiyun 
intel_pstate_max_within_limits(struct cpudata * cpu)1716*4882a593Smuzhiyun static void intel_pstate_max_within_limits(struct cpudata *cpu)
1717*4882a593Smuzhiyun {
1718*4882a593Smuzhiyun 	int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 	update_turbo_state();
1721*4882a593Smuzhiyun 	intel_pstate_set_pstate(cpu, pstate);
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
intel_pstate_get_cpu_pstates(struct cpudata * cpu)1724*4882a593Smuzhiyun static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun 	cpu->pstate.min_pstate = pstate_funcs.get_min();
1727*4882a593Smuzhiyun 	cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
1728*4882a593Smuzhiyun 	cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
1729*4882a593Smuzhiyun 	cpu->pstate.scaling = pstate_funcs.get_scaling();
1730*4882a593Smuzhiyun 
1731*4882a593Smuzhiyun 	if (hwp_active && !hwp_mode_bdw) {
1732*4882a593Smuzhiyun 		unsigned int phy_max, current_max;
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun 		intel_pstate_get_hwp_max(cpu, &phy_max, &current_max);
1735*4882a593Smuzhiyun 		cpu->pstate.turbo_freq = phy_max * cpu->pstate.scaling;
1736*4882a593Smuzhiyun 		cpu->pstate.turbo_pstate = phy_max;
1737*4882a593Smuzhiyun 		cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(READ_ONCE(cpu->hwp_cap_cached));
1738*4882a593Smuzhiyun 	} else {
1739*4882a593Smuzhiyun 		cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
1740*4882a593Smuzhiyun 		cpu->pstate.max_pstate = pstate_funcs.get_max();
1741*4882a593Smuzhiyun 	}
1742*4882a593Smuzhiyun 	cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	if (pstate_funcs.get_aperf_mperf_shift)
1745*4882a593Smuzhiyun 		cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun 	if (pstate_funcs.get_vid)
1748*4882a593Smuzhiyun 		pstate_funcs.get_vid(cpu);
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 	intel_pstate_set_min_pstate(cpu);
1751*4882a593Smuzhiyun }
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun /*
1754*4882a593Smuzhiyun  * Long hold time will keep high perf limits for long time,
1755*4882a593Smuzhiyun  * which negatively impacts perf/watt for some workloads,
1756*4882a593Smuzhiyun  * like specpower. 3ms is based on experiements on some
1757*4882a593Smuzhiyun  * workoads.
1758*4882a593Smuzhiyun  */
1759*4882a593Smuzhiyun static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
1760*4882a593Smuzhiyun 
intel_pstate_hwp_boost_up(struct cpudata * cpu)1761*4882a593Smuzhiyun static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun 	u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
1764*4882a593Smuzhiyun 	u32 max_limit = (hwp_req & 0xff00) >> 8;
1765*4882a593Smuzhiyun 	u32 min_limit = (hwp_req & 0xff);
1766*4882a593Smuzhiyun 	u32 boost_level1;
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 	/*
1769*4882a593Smuzhiyun 	 * Cases to consider (User changes via sysfs or boot time):
1770*4882a593Smuzhiyun 	 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
1771*4882a593Smuzhiyun 	 *	No boost, return.
1772*4882a593Smuzhiyun 	 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
1773*4882a593Smuzhiyun 	 *     Should result in one level boost only for P0.
1774*4882a593Smuzhiyun 	 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
1775*4882a593Smuzhiyun 	 *     Should result in two level boost:
1776*4882a593Smuzhiyun 	 *         (min + p1)/2 and P1.
1777*4882a593Smuzhiyun 	 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
1778*4882a593Smuzhiyun 	 *     Should result in three level boost:
1779*4882a593Smuzhiyun 	 *        (min + p1)/2, P1 and P0.
1780*4882a593Smuzhiyun 	 */
1781*4882a593Smuzhiyun 
1782*4882a593Smuzhiyun 	/* If max and min are equal or already at max, nothing to boost */
1783*4882a593Smuzhiyun 	if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
1784*4882a593Smuzhiyun 		return;
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	if (!cpu->hwp_boost_min)
1787*4882a593Smuzhiyun 		cpu->hwp_boost_min = min_limit;
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	/* level at half way mark between min and guranteed */
1790*4882a593Smuzhiyun 	boost_level1 = (HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) + min_limit) >> 1;
1791*4882a593Smuzhiyun 
1792*4882a593Smuzhiyun 	if (cpu->hwp_boost_min < boost_level1)
1793*4882a593Smuzhiyun 		cpu->hwp_boost_min = boost_level1;
1794*4882a593Smuzhiyun 	else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1795*4882a593Smuzhiyun 		cpu->hwp_boost_min = HWP_GUARANTEED_PERF(cpu->hwp_cap_cached);
1796*4882a593Smuzhiyun 	else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) &&
1797*4882a593Smuzhiyun 		 max_limit != HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1798*4882a593Smuzhiyun 		cpu->hwp_boost_min = max_limit;
1799*4882a593Smuzhiyun 	else
1800*4882a593Smuzhiyun 		return;
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
1803*4882a593Smuzhiyun 	wrmsrl(MSR_HWP_REQUEST, hwp_req);
1804*4882a593Smuzhiyun 	cpu->last_update = cpu->sample.time;
1805*4882a593Smuzhiyun }
1806*4882a593Smuzhiyun 
intel_pstate_hwp_boost_down(struct cpudata * cpu)1807*4882a593Smuzhiyun static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
1808*4882a593Smuzhiyun {
1809*4882a593Smuzhiyun 	if (cpu->hwp_boost_min) {
1810*4882a593Smuzhiyun 		bool expired;
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 		/* Check if we are idle for hold time to boost down */
1813*4882a593Smuzhiyun 		expired = time_after64(cpu->sample.time, cpu->last_update +
1814*4882a593Smuzhiyun 				       hwp_boost_hold_time_ns);
1815*4882a593Smuzhiyun 		if (expired) {
1816*4882a593Smuzhiyun 			wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
1817*4882a593Smuzhiyun 			cpu->hwp_boost_min = 0;
1818*4882a593Smuzhiyun 		}
1819*4882a593Smuzhiyun 	}
1820*4882a593Smuzhiyun 	cpu->last_update = cpu->sample.time;
1821*4882a593Smuzhiyun }
1822*4882a593Smuzhiyun 
intel_pstate_update_util_hwp_local(struct cpudata * cpu,u64 time)1823*4882a593Smuzhiyun static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
1824*4882a593Smuzhiyun 						      u64 time)
1825*4882a593Smuzhiyun {
1826*4882a593Smuzhiyun 	cpu->sample.time = time;
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun 	if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
1829*4882a593Smuzhiyun 		bool do_io = false;
1830*4882a593Smuzhiyun 
1831*4882a593Smuzhiyun 		cpu->sched_flags = 0;
1832*4882a593Smuzhiyun 		/*
1833*4882a593Smuzhiyun 		 * Set iowait_boost flag and update time. Since IO WAIT flag
1834*4882a593Smuzhiyun 		 * is set all the time, we can't just conclude that there is
1835*4882a593Smuzhiyun 		 * some IO bound activity is scheduled on this CPU with just
1836*4882a593Smuzhiyun 		 * one occurrence. If we receive at least two in two
1837*4882a593Smuzhiyun 		 * consecutive ticks, then we treat as boost candidate.
1838*4882a593Smuzhiyun 		 */
1839*4882a593Smuzhiyun 		if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
1840*4882a593Smuzhiyun 			do_io = true;
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 		cpu->last_io_update = time;
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 		if (do_io)
1845*4882a593Smuzhiyun 			intel_pstate_hwp_boost_up(cpu);
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 	} else {
1848*4882a593Smuzhiyun 		intel_pstate_hwp_boost_down(cpu);
1849*4882a593Smuzhiyun 	}
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun 
intel_pstate_update_util_hwp(struct update_util_data * data,u64 time,unsigned int flags)1852*4882a593Smuzhiyun static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
1853*4882a593Smuzhiyun 						u64 time, unsigned int flags)
1854*4882a593Smuzhiyun {
1855*4882a593Smuzhiyun 	struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	cpu->sched_flags |= flags;
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun 	if (smp_processor_id() == cpu->cpu)
1860*4882a593Smuzhiyun 		intel_pstate_update_util_hwp_local(cpu, time);
1861*4882a593Smuzhiyun }
1862*4882a593Smuzhiyun 
intel_pstate_calc_avg_perf(struct cpudata * cpu)1863*4882a593Smuzhiyun static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
1864*4882a593Smuzhiyun {
1865*4882a593Smuzhiyun 	struct sample *sample = &cpu->sample;
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun 
intel_pstate_sample(struct cpudata * cpu,u64 time)1870*4882a593Smuzhiyun static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
1871*4882a593Smuzhiyun {
1872*4882a593Smuzhiyun 	u64 aperf, mperf;
1873*4882a593Smuzhiyun 	unsigned long flags;
1874*4882a593Smuzhiyun 	u64 tsc;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	local_irq_save(flags);
1877*4882a593Smuzhiyun 	rdmsrl(MSR_IA32_APERF, aperf);
1878*4882a593Smuzhiyun 	rdmsrl(MSR_IA32_MPERF, mperf);
1879*4882a593Smuzhiyun 	tsc = rdtsc();
1880*4882a593Smuzhiyun 	if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
1881*4882a593Smuzhiyun 		local_irq_restore(flags);
1882*4882a593Smuzhiyun 		return false;
1883*4882a593Smuzhiyun 	}
1884*4882a593Smuzhiyun 	local_irq_restore(flags);
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun 	cpu->last_sample_time = cpu->sample.time;
1887*4882a593Smuzhiyun 	cpu->sample.time = time;
1888*4882a593Smuzhiyun 	cpu->sample.aperf = aperf;
1889*4882a593Smuzhiyun 	cpu->sample.mperf = mperf;
1890*4882a593Smuzhiyun 	cpu->sample.tsc =  tsc;
1891*4882a593Smuzhiyun 	cpu->sample.aperf -= cpu->prev_aperf;
1892*4882a593Smuzhiyun 	cpu->sample.mperf -= cpu->prev_mperf;
1893*4882a593Smuzhiyun 	cpu->sample.tsc -= cpu->prev_tsc;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	cpu->prev_aperf = aperf;
1896*4882a593Smuzhiyun 	cpu->prev_mperf = mperf;
1897*4882a593Smuzhiyun 	cpu->prev_tsc = tsc;
1898*4882a593Smuzhiyun 	/*
1899*4882a593Smuzhiyun 	 * First time this function is invoked in a given cycle, all of the
1900*4882a593Smuzhiyun 	 * previous sample data fields are equal to zero or stale and they must
1901*4882a593Smuzhiyun 	 * be populated with meaningful numbers for things to work, so assume
1902*4882a593Smuzhiyun 	 * that sample.time will always be reset before setting the utilization
1903*4882a593Smuzhiyun 	 * update hook and make the caller skip the sample then.
1904*4882a593Smuzhiyun 	 */
1905*4882a593Smuzhiyun 	if (cpu->last_sample_time) {
1906*4882a593Smuzhiyun 		intel_pstate_calc_avg_perf(cpu);
1907*4882a593Smuzhiyun 		return true;
1908*4882a593Smuzhiyun 	}
1909*4882a593Smuzhiyun 	return false;
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun 
get_avg_frequency(struct cpudata * cpu)1912*4882a593Smuzhiyun static inline int32_t get_avg_frequency(struct cpudata *cpu)
1913*4882a593Smuzhiyun {
1914*4882a593Smuzhiyun 	return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun 
get_avg_pstate(struct cpudata * cpu)1917*4882a593Smuzhiyun static inline int32_t get_avg_pstate(struct cpudata *cpu)
1918*4882a593Smuzhiyun {
1919*4882a593Smuzhiyun 	return mul_ext_fp(cpu->pstate.max_pstate_physical,
1920*4882a593Smuzhiyun 			  cpu->sample.core_avg_perf);
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun 
get_target_pstate(struct cpudata * cpu)1923*4882a593Smuzhiyun static inline int32_t get_target_pstate(struct cpudata *cpu)
1924*4882a593Smuzhiyun {
1925*4882a593Smuzhiyun 	struct sample *sample = &cpu->sample;
1926*4882a593Smuzhiyun 	int32_t busy_frac;
1927*4882a593Smuzhiyun 	int target, avg_pstate;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
1930*4882a593Smuzhiyun 			   sample->tsc);
1931*4882a593Smuzhiyun 
1932*4882a593Smuzhiyun 	if (busy_frac < cpu->iowait_boost)
1933*4882a593Smuzhiyun 		busy_frac = cpu->iowait_boost;
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 	sample->busy_scaled = busy_frac * 100;
1936*4882a593Smuzhiyun 
1937*4882a593Smuzhiyun 	target = global.no_turbo || global.turbo_disabled ?
1938*4882a593Smuzhiyun 			cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1939*4882a593Smuzhiyun 	target += target >> 2;
1940*4882a593Smuzhiyun 	target = mul_fp(target, busy_frac);
1941*4882a593Smuzhiyun 	if (target < cpu->pstate.min_pstate)
1942*4882a593Smuzhiyun 		target = cpu->pstate.min_pstate;
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 	/*
1945*4882a593Smuzhiyun 	 * If the average P-state during the previous cycle was higher than the
1946*4882a593Smuzhiyun 	 * current target, add 50% of the difference to the target to reduce
1947*4882a593Smuzhiyun 	 * possible performance oscillations and offset possible performance
1948*4882a593Smuzhiyun 	 * loss related to moving the workload from one CPU to another within
1949*4882a593Smuzhiyun 	 * a package/module.
1950*4882a593Smuzhiyun 	 */
1951*4882a593Smuzhiyun 	avg_pstate = get_avg_pstate(cpu);
1952*4882a593Smuzhiyun 	if (avg_pstate > target)
1953*4882a593Smuzhiyun 		target += (avg_pstate - target) >> 1;
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun 	return target;
1956*4882a593Smuzhiyun }
1957*4882a593Smuzhiyun 
intel_pstate_prepare_request(struct cpudata * cpu,int pstate)1958*4882a593Smuzhiyun static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
1959*4882a593Smuzhiyun {
1960*4882a593Smuzhiyun 	int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1961*4882a593Smuzhiyun 	int max_pstate = max(min_pstate, cpu->max_perf_ratio);
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	return clamp_t(int, pstate, min_pstate, max_pstate);
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun 
intel_pstate_update_pstate(struct cpudata * cpu,int pstate)1966*4882a593Smuzhiyun static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1967*4882a593Smuzhiyun {
1968*4882a593Smuzhiyun 	if (pstate == cpu->pstate.current_pstate)
1969*4882a593Smuzhiyun 		return;
1970*4882a593Smuzhiyun 
1971*4882a593Smuzhiyun 	cpu->pstate.current_pstate = pstate;
1972*4882a593Smuzhiyun 	wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1973*4882a593Smuzhiyun }
1974*4882a593Smuzhiyun 
intel_pstate_adjust_pstate(struct cpudata * cpu)1975*4882a593Smuzhiyun static void intel_pstate_adjust_pstate(struct cpudata *cpu)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun 	int from = cpu->pstate.current_pstate;
1978*4882a593Smuzhiyun 	struct sample *sample;
1979*4882a593Smuzhiyun 	int target_pstate;
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	update_turbo_state();
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	target_pstate = get_target_pstate(cpu);
1984*4882a593Smuzhiyun 	target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1985*4882a593Smuzhiyun 	trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
1986*4882a593Smuzhiyun 	intel_pstate_update_pstate(cpu, target_pstate);
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun 	sample = &cpu->sample;
1989*4882a593Smuzhiyun 	trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
1990*4882a593Smuzhiyun 		fp_toint(sample->busy_scaled),
1991*4882a593Smuzhiyun 		from,
1992*4882a593Smuzhiyun 		cpu->pstate.current_pstate,
1993*4882a593Smuzhiyun 		sample->mperf,
1994*4882a593Smuzhiyun 		sample->aperf,
1995*4882a593Smuzhiyun 		sample->tsc,
1996*4882a593Smuzhiyun 		get_avg_frequency(cpu),
1997*4882a593Smuzhiyun 		fp_toint(cpu->iowait_boost * 100));
1998*4882a593Smuzhiyun }
1999*4882a593Smuzhiyun 
intel_pstate_update_util(struct update_util_data * data,u64 time,unsigned int flags)2000*4882a593Smuzhiyun static void intel_pstate_update_util(struct update_util_data *data, u64 time,
2001*4882a593Smuzhiyun 				     unsigned int flags)
2002*4882a593Smuzhiyun {
2003*4882a593Smuzhiyun 	struct cpudata *cpu = container_of(data, struct cpudata, update_util);
2004*4882a593Smuzhiyun 	u64 delta_ns;
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 	/* Don't allow remote callbacks */
2007*4882a593Smuzhiyun 	if (smp_processor_id() != cpu->cpu)
2008*4882a593Smuzhiyun 		return;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	delta_ns = time - cpu->last_update;
2011*4882a593Smuzhiyun 	if (flags & SCHED_CPUFREQ_IOWAIT) {
2012*4882a593Smuzhiyun 		/* Start over if the CPU may have been idle. */
2013*4882a593Smuzhiyun 		if (delta_ns > TICK_NSEC) {
2014*4882a593Smuzhiyun 			cpu->iowait_boost = ONE_EIGHTH_FP;
2015*4882a593Smuzhiyun 		} else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
2016*4882a593Smuzhiyun 			cpu->iowait_boost <<= 1;
2017*4882a593Smuzhiyun 			if (cpu->iowait_boost > int_tofp(1))
2018*4882a593Smuzhiyun 				cpu->iowait_boost = int_tofp(1);
2019*4882a593Smuzhiyun 		} else {
2020*4882a593Smuzhiyun 			cpu->iowait_boost = ONE_EIGHTH_FP;
2021*4882a593Smuzhiyun 		}
2022*4882a593Smuzhiyun 	} else if (cpu->iowait_boost) {
2023*4882a593Smuzhiyun 		/* Clear iowait_boost if the CPU may have been idle. */
2024*4882a593Smuzhiyun 		if (delta_ns > TICK_NSEC)
2025*4882a593Smuzhiyun 			cpu->iowait_boost = 0;
2026*4882a593Smuzhiyun 		else
2027*4882a593Smuzhiyun 			cpu->iowait_boost >>= 1;
2028*4882a593Smuzhiyun 	}
2029*4882a593Smuzhiyun 	cpu->last_update = time;
2030*4882a593Smuzhiyun 	delta_ns = time - cpu->sample.time;
2031*4882a593Smuzhiyun 	if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
2032*4882a593Smuzhiyun 		return;
2033*4882a593Smuzhiyun 
2034*4882a593Smuzhiyun 	if (intel_pstate_sample(cpu, time))
2035*4882a593Smuzhiyun 		intel_pstate_adjust_pstate(cpu);
2036*4882a593Smuzhiyun }
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun static struct pstate_funcs core_funcs = {
2039*4882a593Smuzhiyun 	.get_max = core_get_max_pstate,
2040*4882a593Smuzhiyun 	.get_max_physical = core_get_max_pstate_physical,
2041*4882a593Smuzhiyun 	.get_min = core_get_min_pstate,
2042*4882a593Smuzhiyun 	.get_turbo = core_get_turbo_pstate,
2043*4882a593Smuzhiyun 	.get_scaling = core_get_scaling,
2044*4882a593Smuzhiyun 	.get_val = core_get_val,
2045*4882a593Smuzhiyun };
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun static const struct pstate_funcs silvermont_funcs = {
2048*4882a593Smuzhiyun 	.get_max = atom_get_max_pstate,
2049*4882a593Smuzhiyun 	.get_max_physical = atom_get_max_pstate,
2050*4882a593Smuzhiyun 	.get_min = atom_get_min_pstate,
2051*4882a593Smuzhiyun 	.get_turbo = atom_get_turbo_pstate,
2052*4882a593Smuzhiyun 	.get_val = atom_get_val,
2053*4882a593Smuzhiyun 	.get_scaling = silvermont_get_scaling,
2054*4882a593Smuzhiyun 	.get_vid = atom_get_vid,
2055*4882a593Smuzhiyun };
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun static const struct pstate_funcs airmont_funcs = {
2058*4882a593Smuzhiyun 	.get_max = atom_get_max_pstate,
2059*4882a593Smuzhiyun 	.get_max_physical = atom_get_max_pstate,
2060*4882a593Smuzhiyun 	.get_min = atom_get_min_pstate,
2061*4882a593Smuzhiyun 	.get_turbo = atom_get_turbo_pstate,
2062*4882a593Smuzhiyun 	.get_val = atom_get_val,
2063*4882a593Smuzhiyun 	.get_scaling = airmont_get_scaling,
2064*4882a593Smuzhiyun 	.get_vid = atom_get_vid,
2065*4882a593Smuzhiyun };
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun static const struct pstate_funcs knl_funcs = {
2068*4882a593Smuzhiyun 	.get_max = core_get_max_pstate,
2069*4882a593Smuzhiyun 	.get_max_physical = core_get_max_pstate_physical,
2070*4882a593Smuzhiyun 	.get_min = core_get_min_pstate,
2071*4882a593Smuzhiyun 	.get_turbo = knl_get_turbo_pstate,
2072*4882a593Smuzhiyun 	.get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
2073*4882a593Smuzhiyun 	.get_scaling = core_get_scaling,
2074*4882a593Smuzhiyun 	.get_val = core_get_val,
2075*4882a593Smuzhiyun };
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun #define X86_MATCH(model, policy)					 \
2078*4882a593Smuzhiyun 	X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
2079*4882a593Smuzhiyun 					   X86_FEATURE_APERFMPERF, &policy)
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
2082*4882a593Smuzhiyun 	X86_MATCH(SANDYBRIDGE,		core_funcs),
2083*4882a593Smuzhiyun 	X86_MATCH(SANDYBRIDGE_X,	core_funcs),
2084*4882a593Smuzhiyun 	X86_MATCH(ATOM_SILVERMONT,	silvermont_funcs),
2085*4882a593Smuzhiyun 	X86_MATCH(IVYBRIDGE,		core_funcs),
2086*4882a593Smuzhiyun 	X86_MATCH(HASWELL,		core_funcs),
2087*4882a593Smuzhiyun 	X86_MATCH(BROADWELL,		core_funcs),
2088*4882a593Smuzhiyun 	X86_MATCH(IVYBRIDGE_X,		core_funcs),
2089*4882a593Smuzhiyun 	X86_MATCH(HASWELL_X,		core_funcs),
2090*4882a593Smuzhiyun 	X86_MATCH(HASWELL_L,		core_funcs),
2091*4882a593Smuzhiyun 	X86_MATCH(HASWELL_G,		core_funcs),
2092*4882a593Smuzhiyun 	X86_MATCH(BROADWELL_G,		core_funcs),
2093*4882a593Smuzhiyun 	X86_MATCH(ATOM_AIRMONT,		airmont_funcs),
2094*4882a593Smuzhiyun 	X86_MATCH(SKYLAKE_L,		core_funcs),
2095*4882a593Smuzhiyun 	X86_MATCH(BROADWELL_X,		core_funcs),
2096*4882a593Smuzhiyun 	X86_MATCH(SKYLAKE,		core_funcs),
2097*4882a593Smuzhiyun 	X86_MATCH(BROADWELL_D,		core_funcs),
2098*4882a593Smuzhiyun 	X86_MATCH(XEON_PHI_KNL,		knl_funcs),
2099*4882a593Smuzhiyun 	X86_MATCH(XEON_PHI_KNM,		knl_funcs),
2100*4882a593Smuzhiyun 	X86_MATCH(ATOM_GOLDMONT,	core_funcs),
2101*4882a593Smuzhiyun 	X86_MATCH(ATOM_GOLDMONT_PLUS,	core_funcs),
2102*4882a593Smuzhiyun 	X86_MATCH(SKYLAKE_X,		core_funcs),
2103*4882a593Smuzhiyun 	{}
2104*4882a593Smuzhiyun };
2105*4882a593Smuzhiyun MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
2108*4882a593Smuzhiyun 	X86_MATCH(BROADWELL_D,		core_funcs),
2109*4882a593Smuzhiyun 	X86_MATCH(BROADWELL_X,		core_funcs),
2110*4882a593Smuzhiyun 	X86_MATCH(SKYLAKE_X,		core_funcs),
2111*4882a593Smuzhiyun 	{}
2112*4882a593Smuzhiyun };
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
2115*4882a593Smuzhiyun 	X86_MATCH(KABYLAKE,		core_funcs),
2116*4882a593Smuzhiyun 	{}
2117*4882a593Smuzhiyun };
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun static const struct x86_cpu_id intel_pstate_hwp_boost_ids[] = {
2120*4882a593Smuzhiyun 	X86_MATCH(SKYLAKE_X,		core_funcs),
2121*4882a593Smuzhiyun 	X86_MATCH(SKYLAKE,		core_funcs),
2122*4882a593Smuzhiyun 	{}
2123*4882a593Smuzhiyun };
2124*4882a593Smuzhiyun 
intel_pstate_init_cpu(unsigned int cpunum)2125*4882a593Smuzhiyun static int intel_pstate_init_cpu(unsigned int cpunum)
2126*4882a593Smuzhiyun {
2127*4882a593Smuzhiyun 	struct cpudata *cpu;
2128*4882a593Smuzhiyun 
2129*4882a593Smuzhiyun 	cpu = all_cpu_data[cpunum];
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	if (!cpu) {
2132*4882a593Smuzhiyun 		cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
2133*4882a593Smuzhiyun 		if (!cpu)
2134*4882a593Smuzhiyun 			return -ENOMEM;
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun 		all_cpu_data[cpunum] = cpu;
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 		cpu->cpu = cpunum;
2139*4882a593Smuzhiyun 
2140*4882a593Smuzhiyun 		cpu->epp_default = -EINVAL;
2141*4882a593Smuzhiyun 
2142*4882a593Smuzhiyun 		if (hwp_active) {
2143*4882a593Smuzhiyun 			const struct x86_cpu_id *id;
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 			intel_pstate_hwp_enable(cpu);
2146*4882a593Smuzhiyun 
2147*4882a593Smuzhiyun 			id = x86_match_cpu(intel_pstate_hwp_boost_ids);
2148*4882a593Smuzhiyun 			if (id && intel_pstate_acpi_pm_profile_server())
2149*4882a593Smuzhiyun 				hwp_boost = true;
2150*4882a593Smuzhiyun 		}
2151*4882a593Smuzhiyun 	} else if (hwp_active) {
2152*4882a593Smuzhiyun 		/*
2153*4882a593Smuzhiyun 		 * Re-enable HWP in case this happens after a resume from ACPI
2154*4882a593Smuzhiyun 		 * S3 if the CPU was offline during the whole system/resume
2155*4882a593Smuzhiyun 		 * cycle.
2156*4882a593Smuzhiyun 		 */
2157*4882a593Smuzhiyun 		intel_pstate_hwp_reenable(cpu);
2158*4882a593Smuzhiyun 	}
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 	cpu->epp_powersave = -EINVAL;
2161*4882a593Smuzhiyun 	cpu->epp_policy = 0;
2162*4882a593Smuzhiyun 
2163*4882a593Smuzhiyun 	intel_pstate_get_cpu_pstates(cpu);
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	pr_debug("controlling: cpu %d\n", cpunum);
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	return 0;
2168*4882a593Smuzhiyun }
2169*4882a593Smuzhiyun 
intel_pstate_set_update_util_hook(unsigned int cpu_num)2170*4882a593Smuzhiyun static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
2171*4882a593Smuzhiyun {
2172*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[cpu_num];
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	if (hwp_active && !hwp_boost)
2175*4882a593Smuzhiyun 		return;
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 	if (cpu->update_util_set)
2178*4882a593Smuzhiyun 		return;
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 	/* Prevent intel_pstate_update_util() from using stale data. */
2181*4882a593Smuzhiyun 	cpu->sample.time = 0;
2182*4882a593Smuzhiyun 	cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
2183*4882a593Smuzhiyun 				     (hwp_active ?
2184*4882a593Smuzhiyun 				      intel_pstate_update_util_hwp :
2185*4882a593Smuzhiyun 				      intel_pstate_update_util));
2186*4882a593Smuzhiyun 	cpu->update_util_set = true;
2187*4882a593Smuzhiyun }
2188*4882a593Smuzhiyun 
intel_pstate_clear_update_util_hook(unsigned int cpu)2189*4882a593Smuzhiyun static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2190*4882a593Smuzhiyun {
2191*4882a593Smuzhiyun 	struct cpudata *cpu_data = all_cpu_data[cpu];
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	if (!cpu_data->update_util_set)
2194*4882a593Smuzhiyun 		return;
2195*4882a593Smuzhiyun 
2196*4882a593Smuzhiyun 	cpufreq_remove_update_util_hook(cpu);
2197*4882a593Smuzhiyun 	cpu_data->update_util_set = false;
2198*4882a593Smuzhiyun 	synchronize_rcu();
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun 
intel_pstate_get_max_freq(struct cpudata * cpu)2201*4882a593Smuzhiyun static int intel_pstate_get_max_freq(struct cpudata *cpu)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun 	return global.turbo_disabled || global.no_turbo ?
2204*4882a593Smuzhiyun 			cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2205*4882a593Smuzhiyun }
2206*4882a593Smuzhiyun 
intel_pstate_update_perf_limits(struct cpudata * cpu,unsigned int policy_min,unsigned int policy_max)2207*4882a593Smuzhiyun static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2208*4882a593Smuzhiyun 					    unsigned int policy_min,
2209*4882a593Smuzhiyun 					    unsigned int policy_max)
2210*4882a593Smuzhiyun {
2211*4882a593Smuzhiyun 	int32_t max_policy_perf, min_policy_perf;
2212*4882a593Smuzhiyun 	int max_state, turbo_max;
2213*4882a593Smuzhiyun 	int max_freq;
2214*4882a593Smuzhiyun 
2215*4882a593Smuzhiyun 	/*
2216*4882a593Smuzhiyun 	 * HWP needs some special consideration, because on BDX the
2217*4882a593Smuzhiyun 	 * HWP_REQUEST uses abstract value to represent performance
2218*4882a593Smuzhiyun 	 * rather than pure ratios.
2219*4882a593Smuzhiyun 	 */
2220*4882a593Smuzhiyun 	if (hwp_active) {
2221*4882a593Smuzhiyun 		intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2222*4882a593Smuzhiyun 	} else {
2223*4882a593Smuzhiyun 		max_state = global.no_turbo || global.turbo_disabled ?
2224*4882a593Smuzhiyun 			cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2225*4882a593Smuzhiyun 		turbo_max = cpu->pstate.turbo_pstate;
2226*4882a593Smuzhiyun 	}
2227*4882a593Smuzhiyun 	max_freq = max_state * cpu->pstate.scaling;
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 	max_policy_perf = max_state * policy_max / max_freq;
2230*4882a593Smuzhiyun 	if (policy_max == policy_min) {
2231*4882a593Smuzhiyun 		min_policy_perf = max_policy_perf;
2232*4882a593Smuzhiyun 	} else {
2233*4882a593Smuzhiyun 		min_policy_perf = max_state * policy_min / max_freq;
2234*4882a593Smuzhiyun 		min_policy_perf = clamp_t(int32_t, min_policy_perf,
2235*4882a593Smuzhiyun 					  0, max_policy_perf);
2236*4882a593Smuzhiyun 	}
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
2239*4882a593Smuzhiyun 		 cpu->cpu, max_state, min_policy_perf, max_policy_perf);
2240*4882a593Smuzhiyun 
2241*4882a593Smuzhiyun 	/* Normalize user input to [min_perf, max_perf] */
2242*4882a593Smuzhiyun 	if (per_cpu_limits) {
2243*4882a593Smuzhiyun 		cpu->min_perf_ratio = min_policy_perf;
2244*4882a593Smuzhiyun 		cpu->max_perf_ratio = max_policy_perf;
2245*4882a593Smuzhiyun 	} else {
2246*4882a593Smuzhiyun 		int32_t global_min, global_max;
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 		/* Global limits are in percent of the maximum turbo P-state. */
2249*4882a593Smuzhiyun 		global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2250*4882a593Smuzhiyun 		global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2251*4882a593Smuzhiyun 		global_min = clamp_t(int32_t, global_min, 0, global_max);
2252*4882a593Smuzhiyun 
2253*4882a593Smuzhiyun 		pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
2254*4882a593Smuzhiyun 			 global_min, global_max);
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 		cpu->min_perf_ratio = max(min_policy_perf, global_min);
2257*4882a593Smuzhiyun 		cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
2258*4882a593Smuzhiyun 		cpu->max_perf_ratio = min(max_policy_perf, global_max);
2259*4882a593Smuzhiyun 		cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 		/* Make sure min_perf <= max_perf */
2262*4882a593Smuzhiyun 		cpu->min_perf_ratio = min(cpu->min_perf_ratio,
2263*4882a593Smuzhiyun 					  cpu->max_perf_ratio);
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun 	}
2266*4882a593Smuzhiyun 	pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
2267*4882a593Smuzhiyun 		 cpu->max_perf_ratio,
2268*4882a593Smuzhiyun 		 cpu->min_perf_ratio);
2269*4882a593Smuzhiyun }
2270*4882a593Smuzhiyun 
intel_pstate_set_policy(struct cpufreq_policy * policy)2271*4882a593Smuzhiyun static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2272*4882a593Smuzhiyun {
2273*4882a593Smuzhiyun 	struct cpudata *cpu;
2274*4882a593Smuzhiyun 
2275*4882a593Smuzhiyun 	if (!policy->cpuinfo.max_freq)
2276*4882a593Smuzhiyun 		return -ENODEV;
2277*4882a593Smuzhiyun 
2278*4882a593Smuzhiyun 	pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2279*4882a593Smuzhiyun 		 policy->cpuinfo.max_freq, policy->max);
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun 	cpu = all_cpu_data[policy->cpu];
2282*4882a593Smuzhiyun 	cpu->policy = policy->policy;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_limits_lock);
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun 	intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2287*4882a593Smuzhiyun 
2288*4882a593Smuzhiyun 	if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
2289*4882a593Smuzhiyun 		/*
2290*4882a593Smuzhiyun 		 * NOHZ_FULL CPUs need this as the governor callback may not
2291*4882a593Smuzhiyun 		 * be invoked on them.
2292*4882a593Smuzhiyun 		 */
2293*4882a593Smuzhiyun 		intel_pstate_clear_update_util_hook(policy->cpu);
2294*4882a593Smuzhiyun 		intel_pstate_max_within_limits(cpu);
2295*4882a593Smuzhiyun 	} else {
2296*4882a593Smuzhiyun 		intel_pstate_set_update_util_hook(policy->cpu);
2297*4882a593Smuzhiyun 	}
2298*4882a593Smuzhiyun 
2299*4882a593Smuzhiyun 	if (hwp_active) {
2300*4882a593Smuzhiyun 		/*
2301*4882a593Smuzhiyun 		 * When hwp_boost was active before and dynamically it
2302*4882a593Smuzhiyun 		 * was turned off, in that case we need to clear the
2303*4882a593Smuzhiyun 		 * update util hook.
2304*4882a593Smuzhiyun 		 */
2305*4882a593Smuzhiyun 		if (!hwp_boost)
2306*4882a593Smuzhiyun 			intel_pstate_clear_update_util_hook(policy->cpu);
2307*4882a593Smuzhiyun 		intel_pstate_hwp_set(policy->cpu);
2308*4882a593Smuzhiyun 	}
2309*4882a593Smuzhiyun 
2310*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_limits_lock);
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 	return 0;
2313*4882a593Smuzhiyun }
2314*4882a593Smuzhiyun 
intel_pstate_adjust_policy_max(struct cpudata * cpu,struct cpufreq_policy_data * policy)2315*4882a593Smuzhiyun static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
2316*4882a593Smuzhiyun 					   struct cpufreq_policy_data *policy)
2317*4882a593Smuzhiyun {
2318*4882a593Smuzhiyun 	if (!hwp_active &&
2319*4882a593Smuzhiyun 	    cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
2320*4882a593Smuzhiyun 	    policy->max < policy->cpuinfo.max_freq &&
2321*4882a593Smuzhiyun 	    policy->max > cpu->pstate.max_freq) {
2322*4882a593Smuzhiyun 		pr_debug("policy->max > max non turbo frequency\n");
2323*4882a593Smuzhiyun 		policy->max = policy->cpuinfo.max_freq;
2324*4882a593Smuzhiyun 	}
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun 
intel_pstate_verify_cpu_policy(struct cpudata * cpu,struct cpufreq_policy_data * policy)2327*4882a593Smuzhiyun static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
2328*4882a593Smuzhiyun 					   struct cpufreq_policy_data *policy)
2329*4882a593Smuzhiyun {
2330*4882a593Smuzhiyun 	int max_freq;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun 	update_turbo_state();
2333*4882a593Smuzhiyun 	if (hwp_active) {
2334*4882a593Smuzhiyun 		int max_state, turbo_max;
2335*4882a593Smuzhiyun 
2336*4882a593Smuzhiyun 		intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2337*4882a593Smuzhiyun 		max_freq = max_state * cpu->pstate.scaling;
2338*4882a593Smuzhiyun 	} else {
2339*4882a593Smuzhiyun 		max_freq = intel_pstate_get_max_freq(cpu);
2340*4882a593Smuzhiyun 	}
2341*4882a593Smuzhiyun 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	intel_pstate_adjust_policy_max(cpu, policy);
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun 
intel_pstate_verify_policy(struct cpufreq_policy_data * policy)2346*4882a593Smuzhiyun static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
2347*4882a593Smuzhiyun {
2348*4882a593Smuzhiyun 	intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun 	return 0;
2351*4882a593Smuzhiyun }
2352*4882a593Smuzhiyun 
intel_pstate_cpu_offline(struct cpufreq_policy * policy)2353*4882a593Smuzhiyun static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
2354*4882a593Smuzhiyun {
2355*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun 	pr_debug("CPU %d going offline\n", cpu->cpu);
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 	if (cpu->suspended)
2360*4882a593Smuzhiyun 		return 0;
2361*4882a593Smuzhiyun 
2362*4882a593Smuzhiyun 	/*
2363*4882a593Smuzhiyun 	 * If the CPU is an SMT thread and it goes offline with the performance
2364*4882a593Smuzhiyun 	 * settings different from the minimum, it will prevent its sibling
2365*4882a593Smuzhiyun 	 * from getting to lower performance levels, so force the minimum
2366*4882a593Smuzhiyun 	 * performance on CPU offline to prevent that from happening.
2367*4882a593Smuzhiyun 	 */
2368*4882a593Smuzhiyun 	if (hwp_active)
2369*4882a593Smuzhiyun 		intel_pstate_hwp_offline(cpu);
2370*4882a593Smuzhiyun 	else
2371*4882a593Smuzhiyun 		intel_pstate_set_min_pstate(cpu);
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun 	intel_pstate_exit_perf_limits(policy);
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun 	return 0;
2376*4882a593Smuzhiyun }
2377*4882a593Smuzhiyun 
intel_pstate_cpu_online(struct cpufreq_policy * policy)2378*4882a593Smuzhiyun static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
2379*4882a593Smuzhiyun {
2380*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun 	pr_debug("CPU %d going online\n", cpu->cpu);
2383*4882a593Smuzhiyun 
2384*4882a593Smuzhiyun 	intel_pstate_init_acpi_perf_limits(policy);
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun 	if (hwp_active) {
2387*4882a593Smuzhiyun 		/*
2388*4882a593Smuzhiyun 		 * Re-enable HWP and clear the "suspended" flag to let "resume"
2389*4882a593Smuzhiyun 		 * know that it need not do that.
2390*4882a593Smuzhiyun 		 */
2391*4882a593Smuzhiyun 		intel_pstate_hwp_reenable(cpu);
2392*4882a593Smuzhiyun 		cpu->suspended = false;
2393*4882a593Smuzhiyun 	}
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun 	return 0;
2396*4882a593Smuzhiyun }
2397*4882a593Smuzhiyun 
intel_pstate_stop_cpu(struct cpufreq_policy * policy)2398*4882a593Smuzhiyun static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
2399*4882a593Smuzhiyun {
2400*4882a593Smuzhiyun 	pr_debug("CPU %d stopping\n", policy->cpu);
2401*4882a593Smuzhiyun 
2402*4882a593Smuzhiyun 	intel_pstate_clear_update_util_hook(policy->cpu);
2403*4882a593Smuzhiyun }
2404*4882a593Smuzhiyun 
intel_pstate_cpu_exit(struct cpufreq_policy * policy)2405*4882a593Smuzhiyun static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2406*4882a593Smuzhiyun {
2407*4882a593Smuzhiyun 	pr_debug("CPU %d exiting\n", policy->cpu);
2408*4882a593Smuzhiyun 
2409*4882a593Smuzhiyun 	policy->fast_switch_possible = false;
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 	return 0;
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun 
__intel_pstate_cpu_init(struct cpufreq_policy * policy)2414*4882a593Smuzhiyun static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
2415*4882a593Smuzhiyun {
2416*4882a593Smuzhiyun 	struct cpudata *cpu;
2417*4882a593Smuzhiyun 	int rc;
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 	rc = intel_pstate_init_cpu(policy->cpu);
2420*4882a593Smuzhiyun 	if (rc)
2421*4882a593Smuzhiyun 		return rc;
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 	cpu = all_cpu_data[policy->cpu];
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun 	cpu->max_perf_ratio = 0xFF;
2426*4882a593Smuzhiyun 	cpu->min_perf_ratio = 0;
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 	policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2429*4882a593Smuzhiyun 	policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
2430*4882a593Smuzhiyun 
2431*4882a593Smuzhiyun 	/* cpuinfo and default policy values */
2432*4882a593Smuzhiyun 	policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
2433*4882a593Smuzhiyun 	update_turbo_state();
2434*4882a593Smuzhiyun 	global.turbo_disabled_mf = global.turbo_disabled;
2435*4882a593Smuzhiyun 	policy->cpuinfo.max_freq = global.turbo_disabled ?
2436*4882a593Smuzhiyun 			cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2437*4882a593Smuzhiyun 	policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun 	if (hwp_active) {
2440*4882a593Smuzhiyun 		unsigned int max_freq;
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun 		max_freq = global.turbo_disabled ?
2443*4882a593Smuzhiyun 			cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2444*4882a593Smuzhiyun 		if (max_freq < policy->cpuinfo.max_freq)
2445*4882a593Smuzhiyun 			policy->cpuinfo.max_freq = max_freq;
2446*4882a593Smuzhiyun 	}
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun 	intel_pstate_init_acpi_perf_limits(policy);
2449*4882a593Smuzhiyun 
2450*4882a593Smuzhiyun 	policy->fast_switch_possible = true;
2451*4882a593Smuzhiyun 
2452*4882a593Smuzhiyun 	return 0;
2453*4882a593Smuzhiyun }
2454*4882a593Smuzhiyun 
intel_pstate_cpu_init(struct cpufreq_policy * policy)2455*4882a593Smuzhiyun static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
2456*4882a593Smuzhiyun {
2457*4882a593Smuzhiyun 	int ret = __intel_pstate_cpu_init(policy);
2458*4882a593Smuzhiyun 
2459*4882a593Smuzhiyun 	if (ret)
2460*4882a593Smuzhiyun 		return ret;
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun 	/*
2463*4882a593Smuzhiyun 	 * Set the policy to powersave to provide a valid fallback value in case
2464*4882a593Smuzhiyun 	 * the default cpufreq governor is neither powersave nor performance.
2465*4882a593Smuzhiyun 	 */
2466*4882a593Smuzhiyun 	policy->policy = CPUFREQ_POLICY_POWERSAVE;
2467*4882a593Smuzhiyun 
2468*4882a593Smuzhiyun 	if (hwp_active) {
2469*4882a593Smuzhiyun 		struct cpudata *cpu = all_cpu_data[policy->cpu];
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun 		cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
2472*4882a593Smuzhiyun 	}
2473*4882a593Smuzhiyun 
2474*4882a593Smuzhiyun 	return 0;
2475*4882a593Smuzhiyun }
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun static struct cpufreq_driver intel_pstate = {
2478*4882a593Smuzhiyun 	.flags		= CPUFREQ_CONST_LOOPS,
2479*4882a593Smuzhiyun 	.verify		= intel_pstate_verify_policy,
2480*4882a593Smuzhiyun 	.setpolicy	= intel_pstate_set_policy,
2481*4882a593Smuzhiyun 	.suspend	= intel_pstate_suspend,
2482*4882a593Smuzhiyun 	.resume		= intel_pstate_resume,
2483*4882a593Smuzhiyun 	.init		= intel_pstate_cpu_init,
2484*4882a593Smuzhiyun 	.exit		= intel_pstate_cpu_exit,
2485*4882a593Smuzhiyun 	.stop_cpu	= intel_pstate_stop_cpu,
2486*4882a593Smuzhiyun 	.offline	= intel_pstate_cpu_offline,
2487*4882a593Smuzhiyun 	.online		= intel_pstate_cpu_online,
2488*4882a593Smuzhiyun 	.update_limits	= intel_pstate_update_limits,
2489*4882a593Smuzhiyun 	.name		= "intel_pstate",
2490*4882a593Smuzhiyun };
2491*4882a593Smuzhiyun 
intel_cpufreq_verify_policy(struct cpufreq_policy_data * policy)2492*4882a593Smuzhiyun static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
2493*4882a593Smuzhiyun {
2494*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
2495*4882a593Smuzhiyun 
2496*4882a593Smuzhiyun 	intel_pstate_verify_cpu_policy(cpu, policy);
2497*4882a593Smuzhiyun 	intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2498*4882a593Smuzhiyun 
2499*4882a593Smuzhiyun 	return 0;
2500*4882a593Smuzhiyun }
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun /* Use of trace in passive mode:
2503*4882a593Smuzhiyun  *
2504*4882a593Smuzhiyun  * In passive mode the trace core_busy field (also known as the
2505*4882a593Smuzhiyun  * performance field, and lablelled as such on the graphs; also known as
2506*4882a593Smuzhiyun  * core_avg_perf) is not needed and so is re-assigned to indicate if the
2507*4882a593Smuzhiyun  * driver call was via the normal or fast switch path. Various graphs
2508*4882a593Smuzhiyun  * output from the intel_pstate_tracer.py utility that include core_busy
2509*4882a593Smuzhiyun  * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
2510*4882a593Smuzhiyun  * so we use 10 to indicate the the normal path through the driver, and
2511*4882a593Smuzhiyun  * 90 to indicate the fast switch path through the driver.
2512*4882a593Smuzhiyun  * The scaled_busy field is not used, and is set to 0.
2513*4882a593Smuzhiyun  */
2514*4882a593Smuzhiyun 
2515*4882a593Smuzhiyun #define	INTEL_PSTATE_TRACE_TARGET 10
2516*4882a593Smuzhiyun #define	INTEL_PSTATE_TRACE_FAST_SWITCH 90
2517*4882a593Smuzhiyun 
intel_cpufreq_trace(struct cpudata * cpu,unsigned int trace_type,int old_pstate)2518*4882a593Smuzhiyun static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
2519*4882a593Smuzhiyun {
2520*4882a593Smuzhiyun 	struct sample *sample;
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun 	if (!trace_pstate_sample_enabled())
2523*4882a593Smuzhiyun 		return;
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun 	if (!intel_pstate_sample(cpu, ktime_get()))
2526*4882a593Smuzhiyun 		return;
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun 	sample = &cpu->sample;
2529*4882a593Smuzhiyun 	trace_pstate_sample(trace_type,
2530*4882a593Smuzhiyun 		0,
2531*4882a593Smuzhiyun 		old_pstate,
2532*4882a593Smuzhiyun 		cpu->pstate.current_pstate,
2533*4882a593Smuzhiyun 		sample->mperf,
2534*4882a593Smuzhiyun 		sample->aperf,
2535*4882a593Smuzhiyun 		sample->tsc,
2536*4882a593Smuzhiyun 		get_avg_frequency(cpu),
2537*4882a593Smuzhiyun 		fp_toint(cpu->iowait_boost * 100));
2538*4882a593Smuzhiyun }
2539*4882a593Smuzhiyun 
intel_cpufreq_adjust_hwp(struct cpudata * cpu,u32 target_pstate,bool strict,bool fast_switch)2540*4882a593Smuzhiyun static void intel_cpufreq_adjust_hwp(struct cpudata *cpu, u32 target_pstate,
2541*4882a593Smuzhiyun 				     bool strict, bool fast_switch)
2542*4882a593Smuzhiyun {
2543*4882a593Smuzhiyun 	u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
2544*4882a593Smuzhiyun 
2545*4882a593Smuzhiyun 	value &= ~HWP_MIN_PERF(~0L);
2546*4882a593Smuzhiyun 	value |= HWP_MIN_PERF(target_pstate);
2547*4882a593Smuzhiyun 
2548*4882a593Smuzhiyun 	/*
2549*4882a593Smuzhiyun 	 * The entire MSR needs to be updated in order to update the HWP min
2550*4882a593Smuzhiyun 	 * field in it, so opportunistically update the max too if needed.
2551*4882a593Smuzhiyun 	 */
2552*4882a593Smuzhiyun 	value &= ~HWP_MAX_PERF(~0L);
2553*4882a593Smuzhiyun 	value |= HWP_MAX_PERF(strict ? target_pstate : cpu->max_perf_ratio);
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun 	if (value == prev)
2556*4882a593Smuzhiyun 		return;
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun 	WRITE_ONCE(cpu->hwp_req_cached, value);
2559*4882a593Smuzhiyun 	if (fast_switch)
2560*4882a593Smuzhiyun 		wrmsrl(MSR_HWP_REQUEST, value);
2561*4882a593Smuzhiyun 	else
2562*4882a593Smuzhiyun 		wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
2563*4882a593Smuzhiyun }
2564*4882a593Smuzhiyun 
intel_cpufreq_adjust_perf_ctl(struct cpudata * cpu,u32 target_pstate,bool fast_switch)2565*4882a593Smuzhiyun static void intel_cpufreq_adjust_perf_ctl(struct cpudata *cpu,
2566*4882a593Smuzhiyun 					  u32 target_pstate, bool fast_switch)
2567*4882a593Smuzhiyun {
2568*4882a593Smuzhiyun 	if (fast_switch)
2569*4882a593Smuzhiyun 		wrmsrl(MSR_IA32_PERF_CTL,
2570*4882a593Smuzhiyun 		       pstate_funcs.get_val(cpu, target_pstate));
2571*4882a593Smuzhiyun 	else
2572*4882a593Smuzhiyun 		wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
2573*4882a593Smuzhiyun 			      pstate_funcs.get_val(cpu, target_pstate));
2574*4882a593Smuzhiyun }
2575*4882a593Smuzhiyun 
intel_cpufreq_update_pstate(struct cpufreq_policy * policy,int target_pstate,bool fast_switch)2576*4882a593Smuzhiyun static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
2577*4882a593Smuzhiyun 				       int target_pstate, bool fast_switch)
2578*4882a593Smuzhiyun {
2579*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
2580*4882a593Smuzhiyun 	int old_pstate = cpu->pstate.current_pstate;
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun 	target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2583*4882a593Smuzhiyun 	if (hwp_active) {
2584*4882a593Smuzhiyun 		intel_cpufreq_adjust_hwp(cpu, target_pstate,
2585*4882a593Smuzhiyun 					 policy->strict_target, fast_switch);
2586*4882a593Smuzhiyun 		cpu->pstate.current_pstate = target_pstate;
2587*4882a593Smuzhiyun 	} else if (target_pstate != old_pstate) {
2588*4882a593Smuzhiyun 		intel_cpufreq_adjust_perf_ctl(cpu, target_pstate, fast_switch);
2589*4882a593Smuzhiyun 		cpu->pstate.current_pstate = target_pstate;
2590*4882a593Smuzhiyun 	}
2591*4882a593Smuzhiyun 
2592*4882a593Smuzhiyun 	intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
2593*4882a593Smuzhiyun 			    INTEL_PSTATE_TRACE_TARGET, old_pstate);
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	return target_pstate;
2596*4882a593Smuzhiyun }
2597*4882a593Smuzhiyun 
intel_cpufreq_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)2598*4882a593Smuzhiyun static int intel_cpufreq_target(struct cpufreq_policy *policy,
2599*4882a593Smuzhiyun 				unsigned int target_freq,
2600*4882a593Smuzhiyun 				unsigned int relation)
2601*4882a593Smuzhiyun {
2602*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
2603*4882a593Smuzhiyun 	struct cpufreq_freqs freqs;
2604*4882a593Smuzhiyun 	int target_pstate;
2605*4882a593Smuzhiyun 
2606*4882a593Smuzhiyun 	update_turbo_state();
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 	freqs.old = policy->cur;
2609*4882a593Smuzhiyun 	freqs.new = target_freq;
2610*4882a593Smuzhiyun 
2611*4882a593Smuzhiyun 	cpufreq_freq_transition_begin(policy, &freqs);
2612*4882a593Smuzhiyun 
2613*4882a593Smuzhiyun 	switch (relation) {
2614*4882a593Smuzhiyun 	case CPUFREQ_RELATION_L:
2615*4882a593Smuzhiyun 		target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2616*4882a593Smuzhiyun 		break;
2617*4882a593Smuzhiyun 	case CPUFREQ_RELATION_H:
2618*4882a593Smuzhiyun 		target_pstate = freqs.new / cpu->pstate.scaling;
2619*4882a593Smuzhiyun 		break;
2620*4882a593Smuzhiyun 	default:
2621*4882a593Smuzhiyun 		target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2622*4882a593Smuzhiyun 		break;
2623*4882a593Smuzhiyun 	}
2624*4882a593Smuzhiyun 
2625*4882a593Smuzhiyun 	target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 	freqs.new = target_pstate * cpu->pstate.scaling;
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 	cpufreq_freq_transition_end(policy, &freqs, false);
2630*4882a593Smuzhiyun 
2631*4882a593Smuzhiyun 	return 0;
2632*4882a593Smuzhiyun }
2633*4882a593Smuzhiyun 
intel_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)2634*4882a593Smuzhiyun static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2635*4882a593Smuzhiyun 					      unsigned int target_freq)
2636*4882a593Smuzhiyun {
2637*4882a593Smuzhiyun 	struct cpudata *cpu = all_cpu_data[policy->cpu];
2638*4882a593Smuzhiyun 	int target_pstate;
2639*4882a593Smuzhiyun 
2640*4882a593Smuzhiyun 	update_turbo_state();
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun 	target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2643*4882a593Smuzhiyun 
2644*4882a593Smuzhiyun 	target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	return target_pstate * cpu->pstate.scaling;
2647*4882a593Smuzhiyun }
2648*4882a593Smuzhiyun 
intel_cpufreq_cpu_init(struct cpufreq_policy * policy)2649*4882a593Smuzhiyun static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2650*4882a593Smuzhiyun {
2651*4882a593Smuzhiyun 	int max_state, turbo_max, min_freq, max_freq, ret;
2652*4882a593Smuzhiyun 	struct freq_qos_request *req;
2653*4882a593Smuzhiyun 	struct cpudata *cpu;
2654*4882a593Smuzhiyun 	struct device *dev;
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	dev = get_cpu_device(policy->cpu);
2657*4882a593Smuzhiyun 	if (!dev)
2658*4882a593Smuzhiyun 		return -ENODEV;
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun 	ret = __intel_pstate_cpu_init(policy);
2661*4882a593Smuzhiyun 	if (ret)
2662*4882a593Smuzhiyun 		return ret;
2663*4882a593Smuzhiyun 
2664*4882a593Smuzhiyun 	policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2665*4882a593Smuzhiyun 	/* This reflects the intel_pstate_get_cpu_pstates() setting. */
2666*4882a593Smuzhiyun 	policy->cur = policy->cpuinfo.min_freq;
2667*4882a593Smuzhiyun 
2668*4882a593Smuzhiyun 	req = kcalloc(2, sizeof(*req), GFP_KERNEL);
2669*4882a593Smuzhiyun 	if (!req) {
2670*4882a593Smuzhiyun 		ret = -ENOMEM;
2671*4882a593Smuzhiyun 		goto pstate_exit;
2672*4882a593Smuzhiyun 	}
2673*4882a593Smuzhiyun 
2674*4882a593Smuzhiyun 	cpu = all_cpu_data[policy->cpu];
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun 	if (hwp_active) {
2677*4882a593Smuzhiyun 		u64 value;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 		intel_pstate_get_hwp_max(cpu, &turbo_max, &max_state);
2680*4882a593Smuzhiyun 		policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
2681*4882a593Smuzhiyun 		rdmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
2682*4882a593Smuzhiyun 		WRITE_ONCE(cpu->hwp_req_cached, value);
2683*4882a593Smuzhiyun 		cpu->epp_cached = intel_pstate_get_epp(cpu, value);
2684*4882a593Smuzhiyun 	} else {
2685*4882a593Smuzhiyun 		turbo_max = cpu->pstate.turbo_pstate;
2686*4882a593Smuzhiyun 		policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
2687*4882a593Smuzhiyun 	}
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun 	min_freq = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2690*4882a593Smuzhiyun 	min_freq *= cpu->pstate.scaling;
2691*4882a593Smuzhiyun 	max_freq = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2692*4882a593Smuzhiyun 	max_freq *= cpu->pstate.scaling;
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun 	ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
2695*4882a593Smuzhiyun 				   min_freq);
2696*4882a593Smuzhiyun 	if (ret < 0) {
2697*4882a593Smuzhiyun 		dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
2698*4882a593Smuzhiyun 		goto free_req;
2699*4882a593Smuzhiyun 	}
2700*4882a593Smuzhiyun 
2701*4882a593Smuzhiyun 	ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
2702*4882a593Smuzhiyun 				   max_freq);
2703*4882a593Smuzhiyun 	if (ret < 0) {
2704*4882a593Smuzhiyun 		dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
2705*4882a593Smuzhiyun 		goto remove_min_req;
2706*4882a593Smuzhiyun 	}
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun 	policy->driver_data = req;
2709*4882a593Smuzhiyun 
2710*4882a593Smuzhiyun 	return 0;
2711*4882a593Smuzhiyun 
2712*4882a593Smuzhiyun remove_min_req:
2713*4882a593Smuzhiyun 	freq_qos_remove_request(req);
2714*4882a593Smuzhiyun free_req:
2715*4882a593Smuzhiyun 	kfree(req);
2716*4882a593Smuzhiyun pstate_exit:
2717*4882a593Smuzhiyun 	intel_pstate_exit_perf_limits(policy);
2718*4882a593Smuzhiyun 
2719*4882a593Smuzhiyun 	return ret;
2720*4882a593Smuzhiyun }
2721*4882a593Smuzhiyun 
intel_cpufreq_cpu_exit(struct cpufreq_policy * policy)2722*4882a593Smuzhiyun static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
2723*4882a593Smuzhiyun {
2724*4882a593Smuzhiyun 	struct freq_qos_request *req;
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 	req = policy->driver_data;
2727*4882a593Smuzhiyun 
2728*4882a593Smuzhiyun 	freq_qos_remove_request(req + 1);
2729*4882a593Smuzhiyun 	freq_qos_remove_request(req);
2730*4882a593Smuzhiyun 	kfree(req);
2731*4882a593Smuzhiyun 
2732*4882a593Smuzhiyun 	return intel_pstate_cpu_exit(policy);
2733*4882a593Smuzhiyun }
2734*4882a593Smuzhiyun 
2735*4882a593Smuzhiyun static struct cpufreq_driver intel_cpufreq = {
2736*4882a593Smuzhiyun 	.flags		= CPUFREQ_CONST_LOOPS,
2737*4882a593Smuzhiyun 	.verify		= intel_cpufreq_verify_policy,
2738*4882a593Smuzhiyun 	.target		= intel_cpufreq_target,
2739*4882a593Smuzhiyun 	.fast_switch	= intel_cpufreq_fast_switch,
2740*4882a593Smuzhiyun 	.init		= intel_cpufreq_cpu_init,
2741*4882a593Smuzhiyun 	.exit		= intel_cpufreq_cpu_exit,
2742*4882a593Smuzhiyun 	.offline	= intel_pstate_cpu_offline,
2743*4882a593Smuzhiyun 	.online		= intel_pstate_cpu_online,
2744*4882a593Smuzhiyun 	.suspend	= intel_pstate_suspend,
2745*4882a593Smuzhiyun 	.resume		= intel_pstate_resume,
2746*4882a593Smuzhiyun 	.update_limits	= intel_pstate_update_limits,
2747*4882a593Smuzhiyun 	.name		= "intel_cpufreq",
2748*4882a593Smuzhiyun };
2749*4882a593Smuzhiyun 
2750*4882a593Smuzhiyun static struct cpufreq_driver *default_driver;
2751*4882a593Smuzhiyun 
intel_pstate_driver_cleanup(void)2752*4882a593Smuzhiyun static void intel_pstate_driver_cleanup(void)
2753*4882a593Smuzhiyun {
2754*4882a593Smuzhiyun 	unsigned int cpu;
2755*4882a593Smuzhiyun 
2756*4882a593Smuzhiyun 	get_online_cpus();
2757*4882a593Smuzhiyun 	for_each_online_cpu(cpu) {
2758*4882a593Smuzhiyun 		if (all_cpu_data[cpu]) {
2759*4882a593Smuzhiyun 			if (intel_pstate_driver == &intel_pstate)
2760*4882a593Smuzhiyun 				intel_pstate_clear_update_util_hook(cpu);
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 			kfree(all_cpu_data[cpu]);
2763*4882a593Smuzhiyun 			all_cpu_data[cpu] = NULL;
2764*4882a593Smuzhiyun 		}
2765*4882a593Smuzhiyun 	}
2766*4882a593Smuzhiyun 	put_online_cpus();
2767*4882a593Smuzhiyun 
2768*4882a593Smuzhiyun 	intel_pstate_driver = NULL;
2769*4882a593Smuzhiyun }
2770*4882a593Smuzhiyun 
intel_pstate_register_driver(struct cpufreq_driver * driver)2771*4882a593Smuzhiyun static int intel_pstate_register_driver(struct cpufreq_driver *driver)
2772*4882a593Smuzhiyun {
2773*4882a593Smuzhiyun 	int ret;
2774*4882a593Smuzhiyun 
2775*4882a593Smuzhiyun 	if (driver == &intel_pstate)
2776*4882a593Smuzhiyun 		intel_pstate_sysfs_expose_hwp_dynamic_boost();
2777*4882a593Smuzhiyun 
2778*4882a593Smuzhiyun 	memset(&global, 0, sizeof(global));
2779*4882a593Smuzhiyun 	global.max_perf_pct = 100;
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun 	intel_pstate_driver = driver;
2782*4882a593Smuzhiyun 	ret = cpufreq_register_driver(intel_pstate_driver);
2783*4882a593Smuzhiyun 	if (ret) {
2784*4882a593Smuzhiyun 		intel_pstate_driver_cleanup();
2785*4882a593Smuzhiyun 		return ret;
2786*4882a593Smuzhiyun 	}
2787*4882a593Smuzhiyun 
2788*4882a593Smuzhiyun 	global.min_perf_pct = min_perf_pct_min();
2789*4882a593Smuzhiyun 
2790*4882a593Smuzhiyun 	return 0;
2791*4882a593Smuzhiyun }
2792*4882a593Smuzhiyun 
intel_pstate_show_status(char * buf)2793*4882a593Smuzhiyun static ssize_t intel_pstate_show_status(char *buf)
2794*4882a593Smuzhiyun {
2795*4882a593Smuzhiyun 	if (!intel_pstate_driver)
2796*4882a593Smuzhiyun 		return sprintf(buf, "off\n");
2797*4882a593Smuzhiyun 
2798*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2799*4882a593Smuzhiyun 					"active" : "passive");
2800*4882a593Smuzhiyun }
2801*4882a593Smuzhiyun 
intel_pstate_update_status(const char * buf,size_t size)2802*4882a593Smuzhiyun static int intel_pstate_update_status(const char *buf, size_t size)
2803*4882a593Smuzhiyun {
2804*4882a593Smuzhiyun 	if (size == 3 && !strncmp(buf, "off", size)) {
2805*4882a593Smuzhiyun 		if (!intel_pstate_driver)
2806*4882a593Smuzhiyun 			return -EINVAL;
2807*4882a593Smuzhiyun 
2808*4882a593Smuzhiyun 		if (hwp_active)
2809*4882a593Smuzhiyun 			return -EBUSY;
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun 		cpufreq_unregister_driver(intel_pstate_driver);
2812*4882a593Smuzhiyun 		intel_pstate_driver_cleanup();
2813*4882a593Smuzhiyun 		return 0;
2814*4882a593Smuzhiyun 	}
2815*4882a593Smuzhiyun 
2816*4882a593Smuzhiyun 	if (size == 6 && !strncmp(buf, "active", size)) {
2817*4882a593Smuzhiyun 		if (intel_pstate_driver) {
2818*4882a593Smuzhiyun 			if (intel_pstate_driver == &intel_pstate)
2819*4882a593Smuzhiyun 				return 0;
2820*4882a593Smuzhiyun 
2821*4882a593Smuzhiyun 			cpufreq_unregister_driver(intel_pstate_driver);
2822*4882a593Smuzhiyun 		}
2823*4882a593Smuzhiyun 
2824*4882a593Smuzhiyun 		return intel_pstate_register_driver(&intel_pstate);
2825*4882a593Smuzhiyun 	}
2826*4882a593Smuzhiyun 
2827*4882a593Smuzhiyun 	if (size == 7 && !strncmp(buf, "passive", size)) {
2828*4882a593Smuzhiyun 		if (intel_pstate_driver) {
2829*4882a593Smuzhiyun 			if (intel_pstate_driver == &intel_cpufreq)
2830*4882a593Smuzhiyun 				return 0;
2831*4882a593Smuzhiyun 
2832*4882a593Smuzhiyun 			cpufreq_unregister_driver(intel_pstate_driver);
2833*4882a593Smuzhiyun 			intel_pstate_sysfs_hide_hwp_dynamic_boost();
2834*4882a593Smuzhiyun 		}
2835*4882a593Smuzhiyun 
2836*4882a593Smuzhiyun 		return intel_pstate_register_driver(&intel_cpufreq);
2837*4882a593Smuzhiyun 	}
2838*4882a593Smuzhiyun 
2839*4882a593Smuzhiyun 	return -EINVAL;
2840*4882a593Smuzhiyun }
2841*4882a593Smuzhiyun 
2842*4882a593Smuzhiyun static int no_load __initdata;
2843*4882a593Smuzhiyun static int no_hwp __initdata;
2844*4882a593Smuzhiyun static int hwp_only __initdata;
2845*4882a593Smuzhiyun static unsigned int force_load __initdata;
2846*4882a593Smuzhiyun 
intel_pstate_msrs_not_valid(void)2847*4882a593Smuzhiyun static int __init intel_pstate_msrs_not_valid(void)
2848*4882a593Smuzhiyun {
2849*4882a593Smuzhiyun 	if (!pstate_funcs.get_max() ||
2850*4882a593Smuzhiyun 	    !pstate_funcs.get_min() ||
2851*4882a593Smuzhiyun 	    !pstate_funcs.get_turbo())
2852*4882a593Smuzhiyun 		return -ENODEV;
2853*4882a593Smuzhiyun 
2854*4882a593Smuzhiyun 	return 0;
2855*4882a593Smuzhiyun }
2856*4882a593Smuzhiyun 
copy_cpu_funcs(struct pstate_funcs * funcs)2857*4882a593Smuzhiyun static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
2858*4882a593Smuzhiyun {
2859*4882a593Smuzhiyun 	pstate_funcs.get_max   = funcs->get_max;
2860*4882a593Smuzhiyun 	pstate_funcs.get_max_physical = funcs->get_max_physical;
2861*4882a593Smuzhiyun 	pstate_funcs.get_min   = funcs->get_min;
2862*4882a593Smuzhiyun 	pstate_funcs.get_turbo = funcs->get_turbo;
2863*4882a593Smuzhiyun 	pstate_funcs.get_scaling = funcs->get_scaling;
2864*4882a593Smuzhiyun 	pstate_funcs.get_val   = funcs->get_val;
2865*4882a593Smuzhiyun 	pstate_funcs.get_vid   = funcs->get_vid;
2866*4882a593Smuzhiyun 	pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
2867*4882a593Smuzhiyun }
2868*4882a593Smuzhiyun 
2869*4882a593Smuzhiyun #ifdef CONFIG_ACPI
2870*4882a593Smuzhiyun 
intel_pstate_no_acpi_pss(void)2871*4882a593Smuzhiyun static bool __init intel_pstate_no_acpi_pss(void)
2872*4882a593Smuzhiyun {
2873*4882a593Smuzhiyun 	int i;
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun 	for_each_possible_cpu(i) {
2876*4882a593Smuzhiyun 		acpi_status status;
2877*4882a593Smuzhiyun 		union acpi_object *pss;
2878*4882a593Smuzhiyun 		struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2879*4882a593Smuzhiyun 		struct acpi_processor *pr = per_cpu(processors, i);
2880*4882a593Smuzhiyun 
2881*4882a593Smuzhiyun 		if (!pr)
2882*4882a593Smuzhiyun 			continue;
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun 		status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2885*4882a593Smuzhiyun 		if (ACPI_FAILURE(status))
2886*4882a593Smuzhiyun 			continue;
2887*4882a593Smuzhiyun 
2888*4882a593Smuzhiyun 		pss = buffer.pointer;
2889*4882a593Smuzhiyun 		if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2890*4882a593Smuzhiyun 			kfree(pss);
2891*4882a593Smuzhiyun 			return false;
2892*4882a593Smuzhiyun 		}
2893*4882a593Smuzhiyun 
2894*4882a593Smuzhiyun 		kfree(pss);
2895*4882a593Smuzhiyun 	}
2896*4882a593Smuzhiyun 
2897*4882a593Smuzhiyun 	pr_debug("ACPI _PSS not found\n");
2898*4882a593Smuzhiyun 	return true;
2899*4882a593Smuzhiyun }
2900*4882a593Smuzhiyun 
intel_pstate_no_acpi_pcch(void)2901*4882a593Smuzhiyun static bool __init intel_pstate_no_acpi_pcch(void)
2902*4882a593Smuzhiyun {
2903*4882a593Smuzhiyun 	acpi_status status;
2904*4882a593Smuzhiyun 	acpi_handle handle;
2905*4882a593Smuzhiyun 
2906*4882a593Smuzhiyun 	status = acpi_get_handle(NULL, "\\_SB", &handle);
2907*4882a593Smuzhiyun 	if (ACPI_FAILURE(status))
2908*4882a593Smuzhiyun 		goto not_found;
2909*4882a593Smuzhiyun 
2910*4882a593Smuzhiyun 	if (acpi_has_method(handle, "PCCH"))
2911*4882a593Smuzhiyun 		return false;
2912*4882a593Smuzhiyun 
2913*4882a593Smuzhiyun not_found:
2914*4882a593Smuzhiyun 	pr_debug("ACPI PCCH not found\n");
2915*4882a593Smuzhiyun 	return true;
2916*4882a593Smuzhiyun }
2917*4882a593Smuzhiyun 
intel_pstate_has_acpi_ppc(void)2918*4882a593Smuzhiyun static bool __init intel_pstate_has_acpi_ppc(void)
2919*4882a593Smuzhiyun {
2920*4882a593Smuzhiyun 	int i;
2921*4882a593Smuzhiyun 
2922*4882a593Smuzhiyun 	for_each_possible_cpu(i) {
2923*4882a593Smuzhiyun 		struct acpi_processor *pr = per_cpu(processors, i);
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun 		if (!pr)
2926*4882a593Smuzhiyun 			continue;
2927*4882a593Smuzhiyun 		if (acpi_has_method(pr->handle, "_PPC"))
2928*4882a593Smuzhiyun 			return true;
2929*4882a593Smuzhiyun 	}
2930*4882a593Smuzhiyun 	pr_debug("ACPI _PPC not found\n");
2931*4882a593Smuzhiyun 	return false;
2932*4882a593Smuzhiyun }
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun enum {
2935*4882a593Smuzhiyun 	PSS,
2936*4882a593Smuzhiyun 	PPC,
2937*4882a593Smuzhiyun };
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun /* Hardware vendor-specific info that has its own power management modes */
2940*4882a593Smuzhiyun static struct acpi_platform_list plat_info[] __initdata = {
2941*4882a593Smuzhiyun 	{"HP    ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
2942*4882a593Smuzhiyun 	{"ORACLE", "X4-2    ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2943*4882a593Smuzhiyun 	{"ORACLE", "X4-2L   ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2944*4882a593Smuzhiyun 	{"ORACLE", "X4-2B   ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2945*4882a593Smuzhiyun 	{"ORACLE", "X3-2    ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2946*4882a593Smuzhiyun 	{"ORACLE", "X3-2L   ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2947*4882a593Smuzhiyun 	{"ORACLE", "X3-2B   ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2948*4882a593Smuzhiyun 	{"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2949*4882a593Smuzhiyun 	{"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2950*4882a593Smuzhiyun 	{"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2951*4882a593Smuzhiyun 	{"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2952*4882a593Smuzhiyun 	{"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2953*4882a593Smuzhiyun 	{"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2954*4882a593Smuzhiyun 	{"ORACLE", "X6-2    ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2955*4882a593Smuzhiyun 	{"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2956*4882a593Smuzhiyun 	{ } /* End */
2957*4882a593Smuzhiyun };
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun #define BITMASK_OOB	(BIT(8) | BIT(18))
2960*4882a593Smuzhiyun 
intel_pstate_platform_pwr_mgmt_exists(void)2961*4882a593Smuzhiyun static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
2962*4882a593Smuzhiyun {
2963*4882a593Smuzhiyun 	const struct x86_cpu_id *id;
2964*4882a593Smuzhiyun 	u64 misc_pwr;
2965*4882a593Smuzhiyun 	int idx;
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 	id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2968*4882a593Smuzhiyun 	if (id) {
2969*4882a593Smuzhiyun 		rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2970*4882a593Smuzhiyun 		if (misc_pwr & BITMASK_OOB) {
2971*4882a593Smuzhiyun 			pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
2972*4882a593Smuzhiyun 			pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
2973*4882a593Smuzhiyun 			return true;
2974*4882a593Smuzhiyun 		}
2975*4882a593Smuzhiyun 	}
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	idx = acpi_match_platform_list(plat_info);
2978*4882a593Smuzhiyun 	if (idx < 0)
2979*4882a593Smuzhiyun 		return false;
2980*4882a593Smuzhiyun 
2981*4882a593Smuzhiyun 	switch (plat_info[idx].data) {
2982*4882a593Smuzhiyun 	case PSS:
2983*4882a593Smuzhiyun 		if (!intel_pstate_no_acpi_pss())
2984*4882a593Smuzhiyun 			return false;
2985*4882a593Smuzhiyun 
2986*4882a593Smuzhiyun 		return intel_pstate_no_acpi_pcch();
2987*4882a593Smuzhiyun 	case PPC:
2988*4882a593Smuzhiyun 		return intel_pstate_has_acpi_ppc() && !force_load;
2989*4882a593Smuzhiyun 	}
2990*4882a593Smuzhiyun 
2991*4882a593Smuzhiyun 	return false;
2992*4882a593Smuzhiyun }
2993*4882a593Smuzhiyun 
intel_pstate_request_control_from_smm(void)2994*4882a593Smuzhiyun static void intel_pstate_request_control_from_smm(void)
2995*4882a593Smuzhiyun {
2996*4882a593Smuzhiyun 	/*
2997*4882a593Smuzhiyun 	 * It may be unsafe to request P-states control from SMM if _PPC support
2998*4882a593Smuzhiyun 	 * has not been enabled.
2999*4882a593Smuzhiyun 	 */
3000*4882a593Smuzhiyun 	if (acpi_ppc)
3001*4882a593Smuzhiyun 		acpi_processor_pstate_control();
3002*4882a593Smuzhiyun }
3003*4882a593Smuzhiyun #else /* CONFIG_ACPI not enabled */
intel_pstate_platform_pwr_mgmt_exists(void)3004*4882a593Smuzhiyun static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
intel_pstate_has_acpi_ppc(void)3005*4882a593Smuzhiyun static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
intel_pstate_request_control_from_smm(void)3006*4882a593Smuzhiyun static inline void intel_pstate_request_control_from_smm(void) {}
3007*4882a593Smuzhiyun #endif /* CONFIG_ACPI */
3008*4882a593Smuzhiyun 
3009*4882a593Smuzhiyun #define INTEL_PSTATE_HWP_BROADWELL	0x01
3010*4882a593Smuzhiyun 
3011*4882a593Smuzhiyun #define X86_MATCH_HWP(model, hwp_mode)					\
3012*4882a593Smuzhiyun 	X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
3013*4882a593Smuzhiyun 					   X86_FEATURE_HWP, hwp_mode)
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun static const struct x86_cpu_id hwp_support_ids[] __initconst = {
3016*4882a593Smuzhiyun 	X86_MATCH_HWP(BROADWELL_X,	INTEL_PSTATE_HWP_BROADWELL),
3017*4882a593Smuzhiyun 	X86_MATCH_HWP(BROADWELL_D,	INTEL_PSTATE_HWP_BROADWELL),
3018*4882a593Smuzhiyun 	X86_MATCH_HWP(ANY,		0),
3019*4882a593Smuzhiyun 	{}
3020*4882a593Smuzhiyun };
3021*4882a593Smuzhiyun 
intel_pstate_hwp_is_enabled(void)3022*4882a593Smuzhiyun static bool intel_pstate_hwp_is_enabled(void)
3023*4882a593Smuzhiyun {
3024*4882a593Smuzhiyun 	u64 value;
3025*4882a593Smuzhiyun 
3026*4882a593Smuzhiyun 	rdmsrl(MSR_PM_ENABLE, value);
3027*4882a593Smuzhiyun 	return !!(value & 0x1);
3028*4882a593Smuzhiyun }
3029*4882a593Smuzhiyun 
intel_pstate_init(void)3030*4882a593Smuzhiyun static int __init intel_pstate_init(void)
3031*4882a593Smuzhiyun {
3032*4882a593Smuzhiyun 	const struct x86_cpu_id *id;
3033*4882a593Smuzhiyun 	int rc;
3034*4882a593Smuzhiyun 
3035*4882a593Smuzhiyun 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
3036*4882a593Smuzhiyun 		return -ENODEV;
3037*4882a593Smuzhiyun 
3038*4882a593Smuzhiyun 	id = x86_match_cpu(hwp_support_ids);
3039*4882a593Smuzhiyun 	if (id) {
3040*4882a593Smuzhiyun 		bool hwp_forced = intel_pstate_hwp_is_enabled();
3041*4882a593Smuzhiyun 
3042*4882a593Smuzhiyun 		if (hwp_forced)
3043*4882a593Smuzhiyun 			pr_info("HWP enabled by BIOS\n");
3044*4882a593Smuzhiyun 		else if (no_load)
3045*4882a593Smuzhiyun 			return -ENODEV;
3046*4882a593Smuzhiyun 
3047*4882a593Smuzhiyun 		copy_cpu_funcs(&core_funcs);
3048*4882a593Smuzhiyun 		/*
3049*4882a593Smuzhiyun 		 * Avoid enabling HWP for processors without EPP support,
3050*4882a593Smuzhiyun 		 * because that means incomplete HWP implementation which is a
3051*4882a593Smuzhiyun 		 * corner case and supporting it is generally problematic.
3052*4882a593Smuzhiyun 		 *
3053*4882a593Smuzhiyun 		 * If HWP is enabled already, though, there is no choice but to
3054*4882a593Smuzhiyun 		 * deal with it.
3055*4882a593Smuzhiyun 		 */
3056*4882a593Smuzhiyun 		if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
3057*4882a593Smuzhiyun 			hwp_active++;
3058*4882a593Smuzhiyun 			hwp_mode_bdw = id->driver_data;
3059*4882a593Smuzhiyun 			intel_pstate.attr = hwp_cpufreq_attrs;
3060*4882a593Smuzhiyun 			intel_cpufreq.attr = hwp_cpufreq_attrs;
3061*4882a593Smuzhiyun 			intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
3062*4882a593Smuzhiyun 			if (!default_driver)
3063*4882a593Smuzhiyun 				default_driver = &intel_pstate;
3064*4882a593Smuzhiyun 
3065*4882a593Smuzhiyun 			goto hwp_cpu_matched;
3066*4882a593Smuzhiyun 		}
3067*4882a593Smuzhiyun 		pr_info("HWP not enabled\n");
3068*4882a593Smuzhiyun 	} else {
3069*4882a593Smuzhiyun 		if (no_load)
3070*4882a593Smuzhiyun 			return -ENODEV;
3071*4882a593Smuzhiyun 
3072*4882a593Smuzhiyun 		id = x86_match_cpu(intel_pstate_cpu_ids);
3073*4882a593Smuzhiyun 		if (!id) {
3074*4882a593Smuzhiyun 			pr_info("CPU model not supported\n");
3075*4882a593Smuzhiyun 			return -ENODEV;
3076*4882a593Smuzhiyun 		}
3077*4882a593Smuzhiyun 
3078*4882a593Smuzhiyun 		copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
3079*4882a593Smuzhiyun 	}
3080*4882a593Smuzhiyun 
3081*4882a593Smuzhiyun 	if (intel_pstate_msrs_not_valid()) {
3082*4882a593Smuzhiyun 		pr_info("Invalid MSRs\n");
3083*4882a593Smuzhiyun 		return -ENODEV;
3084*4882a593Smuzhiyun 	}
3085*4882a593Smuzhiyun 	/* Without HWP start in the passive mode. */
3086*4882a593Smuzhiyun 	if (!default_driver)
3087*4882a593Smuzhiyun 		default_driver = &intel_cpufreq;
3088*4882a593Smuzhiyun 
3089*4882a593Smuzhiyun hwp_cpu_matched:
3090*4882a593Smuzhiyun 	/*
3091*4882a593Smuzhiyun 	 * The Intel pstate driver will be ignored if the platform
3092*4882a593Smuzhiyun 	 * firmware has its own power management modes.
3093*4882a593Smuzhiyun 	 */
3094*4882a593Smuzhiyun 	if (intel_pstate_platform_pwr_mgmt_exists()) {
3095*4882a593Smuzhiyun 		pr_info("P-states controlled by the platform\n");
3096*4882a593Smuzhiyun 		return -ENODEV;
3097*4882a593Smuzhiyun 	}
3098*4882a593Smuzhiyun 
3099*4882a593Smuzhiyun 	if (!hwp_active && hwp_only)
3100*4882a593Smuzhiyun 		return -ENOTSUPP;
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun 	pr_info("Intel P-state driver initializing\n");
3103*4882a593Smuzhiyun 
3104*4882a593Smuzhiyun 	all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
3105*4882a593Smuzhiyun 	if (!all_cpu_data)
3106*4882a593Smuzhiyun 		return -ENOMEM;
3107*4882a593Smuzhiyun 
3108*4882a593Smuzhiyun 	intel_pstate_request_control_from_smm();
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun 	intel_pstate_sysfs_expose_params();
3111*4882a593Smuzhiyun 
3112*4882a593Smuzhiyun 	mutex_lock(&intel_pstate_driver_lock);
3113*4882a593Smuzhiyun 	rc = intel_pstate_register_driver(default_driver);
3114*4882a593Smuzhiyun 	mutex_unlock(&intel_pstate_driver_lock);
3115*4882a593Smuzhiyun 	if (rc) {
3116*4882a593Smuzhiyun 		intel_pstate_sysfs_remove();
3117*4882a593Smuzhiyun 		return rc;
3118*4882a593Smuzhiyun 	}
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun 	if (hwp_active) {
3121*4882a593Smuzhiyun 		const struct x86_cpu_id *id;
3122*4882a593Smuzhiyun 
3123*4882a593Smuzhiyun 		id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
3124*4882a593Smuzhiyun 		if (id) {
3125*4882a593Smuzhiyun 			set_power_ctl_ee_state(false);
3126*4882a593Smuzhiyun 			pr_info("Disabling energy efficiency optimization\n");
3127*4882a593Smuzhiyun 		}
3128*4882a593Smuzhiyun 
3129*4882a593Smuzhiyun 		pr_info("HWP enabled\n");
3130*4882a593Smuzhiyun 	}
3131*4882a593Smuzhiyun 
3132*4882a593Smuzhiyun 	return 0;
3133*4882a593Smuzhiyun }
3134*4882a593Smuzhiyun device_initcall(intel_pstate_init);
3135*4882a593Smuzhiyun 
intel_pstate_setup(char * str)3136*4882a593Smuzhiyun static int __init intel_pstate_setup(char *str)
3137*4882a593Smuzhiyun {
3138*4882a593Smuzhiyun 	if (!str)
3139*4882a593Smuzhiyun 		return -EINVAL;
3140*4882a593Smuzhiyun 
3141*4882a593Smuzhiyun 	if (!strcmp(str, "disable"))
3142*4882a593Smuzhiyun 		no_load = 1;
3143*4882a593Smuzhiyun 	else if (!strcmp(str, "active"))
3144*4882a593Smuzhiyun 		default_driver = &intel_pstate;
3145*4882a593Smuzhiyun 	else if (!strcmp(str, "passive"))
3146*4882a593Smuzhiyun 		default_driver = &intel_cpufreq;
3147*4882a593Smuzhiyun 
3148*4882a593Smuzhiyun 	if (!strcmp(str, "no_hwp"))
3149*4882a593Smuzhiyun 		no_hwp = 1;
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun 	if (!strcmp(str, "force"))
3152*4882a593Smuzhiyun 		force_load = 1;
3153*4882a593Smuzhiyun 	if (!strcmp(str, "hwp_only"))
3154*4882a593Smuzhiyun 		hwp_only = 1;
3155*4882a593Smuzhiyun 	if (!strcmp(str, "per_cpu_perf_limits"))
3156*4882a593Smuzhiyun 		per_cpu_limits = true;
3157*4882a593Smuzhiyun 
3158*4882a593Smuzhiyun #ifdef CONFIG_ACPI
3159*4882a593Smuzhiyun 	if (!strcmp(str, "support_acpi_ppc"))
3160*4882a593Smuzhiyun 		acpi_ppc = true;
3161*4882a593Smuzhiyun #endif
3162*4882a593Smuzhiyun 
3163*4882a593Smuzhiyun 	return 0;
3164*4882a593Smuzhiyun }
3165*4882a593Smuzhiyun early_param("intel_pstate", intel_pstate_setup);
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
3168*4882a593Smuzhiyun MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
3169*4882a593Smuzhiyun MODULE_LICENSE("GPL");
3170