xref: /linux/drivers/cpufreq/amd-pstate.h (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2022 Advanced Micro Devices, Inc.
4  *
5  * Author: Meng Li <li.meng@amd.com>
6  */
7 
8 #ifndef _LINUX_AMD_PSTATE_H
9 #define _LINUX_AMD_PSTATE_H
10 
11 #include <linux/pm_qos.h>
12 #include <linux/platform_profile.h>
13 
14 #include <asm/processor.h>
15 
16 #if IS_MODULE(CONFIG_X86_AMD_PSTATE_UT)
17 #define EXPORT_SYMBOL_FOR_PSTATE_UT(symbol) \
18 	EXPORT_SYMBOL_FOR_MODULES(symbol, "amd-pstate-ut")
19 #else
20 #define EXPORT_SYMBOL_FOR_PSTATE_UT(symbol)
21 #endif
22 
23 /*********************************************************************
24  *                        AMD P-state INTERFACE                       *
25  *********************************************************************/
26 
27 /**
28  * union perf_cached - A union to cache performance-related data.
29  * @highest_perf: the maximum performance an individual processor may reach,
30  *		  assuming ideal conditions
31  *		  For platforms that support the preferred core feature, the highest_perf value maybe
32  * 		  configured to any value in the range 166-255 by the firmware (because the preferred
33  * 		  core ranking is encoded in the highest_perf value). To maintain consistency across
34  * 		  all platforms, we split the highest_perf and preferred core ranking values into
35  * 		  cpudata->perf.highest_perf and cpudata->prefcore_ranking.
36  * @nominal_perf: the maximum sustained performance level of the processor,
37  *		  assuming ideal operating conditions
38  * @lowest_nonlinear_perf: the lowest performance level at which nonlinear power
39  *			   savings are achieved
40  * @lowest_perf: the absolute lowest performance level of the processor
41  * @min_limit_perf: Cached value of the performance corresponding to policy->min
42  * @max_limit_perf: Cached value of the performance corresponding to policy->max
43  * @bios_min_perf: Cached perf value corresponding to the "Requested CPU Min Frequency" BIOS option
44  * @val: Raw 64-bit value for atomic access via READ_ONCE()/WRITE_ONCE()
45  */
46 union perf_cached {
47 	struct {
48 		u8	highest_perf;
49 		u8	nominal_perf;
50 		u8	lowest_nonlinear_perf;
51 		u8	lowest_perf;
52 		u8	min_limit_perf;
53 		u8	max_limit_perf;
54 		u8	bios_min_perf;
55 	};
56 	u64	val;
57 };
58 
59 /**
60  * struct  amd_aperf_mperf
61  * @aperf: actual performance frequency clock count
62  * @mperf: maximum performance frequency clock count
63  * @tsc:   time stamp counter
64  */
65 struct amd_aperf_mperf {
66 	u64 aperf;
67 	u64 mperf;
68 	u64 tsc;
69 };
70 
71 /**
72  * struct amd_cpudata - private CPU data for AMD P-State
73  * @cpu: CPU number
74  * @req: constraint request to apply
75  * @cppc_req_cached: cached performance request hints
76  * @cppc_req2_cached: cached value of MSR_AMD_CPPC_REQ2
77  * @perf: cached performance-related data
78  * @prefcore_ranking: the preferred core ranking, the higher value indicates a higher
79  * 		  priority.
80  * @floor_perf_cnt: Cached value of the number of distinct floor
81  *                  performance levels supported
82  * @bios_floor_perf: Cached value of the boot-time floor performance level from
83  *                   MSR_AMD_CPPC_REQ2
84  * @min_limit_freq: Cached value of policy->min (in khz)
85  * @max_limit_freq: Cached value of policy->max (in khz)
86  * @nominal_freq: the frequency (in khz) that mapped to nominal_perf
87  * @max_freq: in ideal conditions the maximum frequency (in khz) possible frequency
88  * @lowest_nonlinear_freq: the frequency (in khz) that mapped to lowest_nonlinear_perf
89  * @floor_freq: Cached value of the user requested floor_freq
90  * @cur: Difference of Aperf/Mperf/tsc count between last and current sample
91  * @prev: Last Aperf/Mperf/tsc count value read from register
92  * @freq: current cpu frequency value (in khz)
93  * @boost_supported: check whether the Processor or SBIOS supports boost mode
94  * @hw_prefcore: check whether HW supports preferred core featue.
95  * 		  Only when hw_prefcore and early prefcore param are true,
96  * 		  AMD P-State driver supports preferred core featue.
97  * @policy: Cpufreq policy value
98  * @suspended: If CPU core if offlined
99  * @epp_default_ac: Default EPP value for AC power source
100  * @epp_default_dc: Default EPP value for DC power source
101  * @dynamic_epp: Whether dynamic EPP is enabled
102  * @raw_epp: Whether the last EPP write was a raw numeric value rather than a
103  *	     named preference
104  * @power_nb: Notifier block for power events
105  * @current_profile: Currently selected platform profile option
106  * @ppdev: Device registered with the platform profile handler
107  * @profile_name: Name under which @ppdev is registered
108  *
109  * The amd_cpudata is key private data for each CPU thread in AMD P-State, and
110  * represents all the attributes and goals that AMD P-State requests at runtime.
111  */
112 struct amd_cpudata {
113 	int	cpu;
114 
115 	struct	freq_qos_request req[2];
116 	u64	cppc_req_cached;
117 	u64	cppc_req2_cached;
118 
119 	union perf_cached perf;
120 
121 	u8	prefcore_ranking;
122 	u8	floor_perf_cnt;
123 	u8	bios_floor_perf;
124 	u32	min_limit_freq;
125 	u32	max_limit_freq;
126 	u32	nominal_freq;
127 	u32	max_freq;
128 	u32	lowest_nonlinear_freq;
129 	u32	floor_freq;
130 
131 	struct amd_aperf_mperf cur;
132 	struct amd_aperf_mperf prev;
133 
134 	u64	freq;
135 	bool	boost_supported;
136 	bool	hw_prefcore;
137 
138 	/* EPP feature related attributes*/
139 	u32	policy;
140 	bool	suspended;
141 	u8	epp_default_ac;
142 	u8	epp_default_dc;
143 	bool	dynamic_epp;
144 	bool	raw_epp;
145 	struct notifier_block power_nb;
146 
147 	/* platform profile */
148 	enum platform_profile_option current_profile;
149 	struct device *ppdev;
150 	char *profile_name;
151 
152 	enum x86_topology_cpu_type cpu_type;
153 };
154 
155 /*
156  * enum amd_pstate_mode - driver working mode of amd pstate
157  */
158 enum amd_pstate_mode {
159 	AMD_PSTATE_UNDEFINED = 0,
160 	AMD_PSTATE_DISABLE,
161 	AMD_PSTATE_PASSIVE,
162 	AMD_PSTATE_ACTIVE,
163 	AMD_PSTATE_GUIDED,
164 	AMD_PSTATE_MAX,
165 };
166 
167 /*
168  * AMD Energy Preference Performance (EPP)
169  * The EPP is used in the CCLK DPM controller to drive
170  * the frequency that a core is going to operate during
171  * short periods of activity. EPP values will be utilized for
172  * different OS profiles (balanced, performance, power savings)
173  * display strings corresponding to EPP index in the
174  * energy_perf_strings[]
175  *	index		String
176  *-------------------------------------
177  *	0		default
178  *	1		performance
179  *	2		balance_performance
180  *	3		balance_power
181  *	4		power
182  *	5		custom (for raw EPP values)
183  *	6		dynamic (platform profile driven selection)
184  */
185 enum energy_perf_value_index {
186 	EPP_INDEX_DEFAULT = 0,
187 	EPP_INDEX_PERFORMANCE,
188 	EPP_INDEX_BALANCE_PERFORMANCE,
189 	EPP_INDEX_BALANCE_POWERSAVE,
190 	EPP_INDEX_POWERSAVE,
191 	EPP_INDEX_CUSTOM,
192 	EPP_INDEX_DYNAMIC,
193 	EPP_INDEX_MAX,
194 };
195 
196 static const char * const energy_perf_strings[] = {
197 	[EPP_INDEX_DEFAULT] = "default",
198 	[EPP_INDEX_PERFORMANCE] = "performance",
199 	[EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
200 	[EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
201 	[EPP_INDEX_POWERSAVE] = "power",
202 	[EPP_INDEX_CUSTOM] = "custom",
203 	[EPP_INDEX_DYNAMIC] = "dynamic",
204 };
205 static_assert(ARRAY_SIZE(energy_perf_strings) == EPP_INDEX_MAX);
206 
207 u8 *amd_pstate_cpu_epp_values(enum x86_topology_cpu_type cpu_type);
208 
209 static inline u8 freq_to_perf(union perf_cached perf, u32 nominal_freq, unsigned int freq_val)
210 {
211 	u32 perf_val = DIV_ROUND_UP_ULL((u64)freq_val * perf.nominal_perf, nominal_freq);
212 
213 	return (u8)clamp(perf_val, perf.lowest_perf, perf.highest_perf);
214 }
215 
216 static inline u32 perf_to_freq(union perf_cached perf, u32 nominal_freq, u8 perf_val)
217 {
218 	return DIV_ROUND_UP_ULL((u64)nominal_freq * perf_val,
219 				perf.nominal_perf);
220 }
221 
222 const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode);
223 int amd_pstate_get_status(void);
224 int amd_pstate_update_status(const char *buf, size_t size);
225 ssize_t store_energy_performance_preference(struct cpufreq_policy *policy,
226 				    const char *buf, size_t count);
227 ssize_t show_energy_performance_preference(struct cpufreq_policy *policy, char *buf);
228 void amd_pstate_clear_dynamic_epp(struct cpufreq_policy *policy);
229 ssize_t store_amd_pstate_floor_freq(struct cpufreq_policy *policy, const char *buf, size_t count);
230 ssize_t show_amd_pstate_floor_freq(struct cpufreq_policy *policy, char *buf);
231 
232 struct freq_attr;
233 
234 struct freq_attr **amd_pstate_get_current_attrs(void);
235 
236 #endif /* _LINUX_AMD_PSTATE_H */
237