1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * amd-pstate.c - AMD Processor P-state Frequency Driver
4  *
5  * Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved.
6  *
7  * Author: Huang Rui <[email protected]>
8  *
9  * AMD P-State introduces a new CPU performance scaling design for AMD
10  * processors using the ACPI Collaborative Performance and Power Control (CPPC)
11  * feature which works with the AMD SMU firmware providing a finer grained
12  * frequency control range. It is to replace the legacy ACPI P-States control,
13  * allows a flexible, low-latency interface for the Linux kernel to directly
14  * communicate the performance hints to hardware.
15  *
16  * AMD P-State is supported on recent AMD Zen base CPU series include some of
17  * Zen2 and Zen3 processors. _CPC needs to be present in the ACPI tables of AMD
18  * P-State supported system. And there are two types of hardware implementations
19  * for AMD P-State: 1) Full MSR Solution and 2) Shared Memory Solution.
20  * X86_FEATURE_CPPC CPU feature flag is used to distinguish the different types.
21  */
22 
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 
25 #include <linux/bitfield.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/smp.h>
30 #include <linux/sched.h>
31 #include <linux/cpufreq.h>
32 #include <linux/compiler.h>
33 #include <linux/dmi.h>
34 #include <linux/slab.h>
35 #include <linux/acpi.h>
36 #include <linux/io.h>
37 #include <linux/delay.h>
38 #include <linux/uaccess.h>
39 #include <linux/static_call.h>
40 #include <linux/topology.h>
41 
42 #include <acpi/processor.h>
43 #include <acpi/cppc_acpi.h>
44 
45 #include <asm/msr.h>
46 #include <asm/processor.h>
47 #include <asm/cpufeature.h>
48 #include <asm/cpu_device_id.h>
49 
50 #include "amd-pstate.h"
51 #include "amd-pstate-trace.h"
52 
53 #define AMD_PSTATE_TRANSITION_LATENCY	20000
54 #define AMD_PSTATE_TRANSITION_DELAY	1000
55 #define AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY 600
56 
57 #define AMD_CPPC_EPP_PERFORMANCE		0x00
58 #define AMD_CPPC_EPP_BALANCE_PERFORMANCE	0x80
59 #define AMD_CPPC_EPP_BALANCE_POWERSAVE		0xBF
60 #define AMD_CPPC_EPP_POWERSAVE			0xFF
61 
62 static const char * const amd_pstate_mode_string[] = {
63 	[AMD_PSTATE_UNDEFINED]   = "undefined",
64 	[AMD_PSTATE_DISABLE]     = "disable",
65 	[AMD_PSTATE_PASSIVE]     = "passive",
66 	[AMD_PSTATE_ACTIVE]      = "active",
67 	[AMD_PSTATE_GUIDED]      = "guided",
68 	NULL,
69 };
70 
amd_pstate_get_mode_string(enum amd_pstate_mode mode)71 const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode)
72 {
73 	if (mode < 0 || mode >= AMD_PSTATE_MAX)
74 		return NULL;
75 	return amd_pstate_mode_string[mode];
76 }
77 EXPORT_SYMBOL_GPL(amd_pstate_get_mode_string);
78 
79 struct quirk_entry {
80 	u32 nominal_freq;
81 	u32 lowest_freq;
82 };
83 
84 static struct cpufreq_driver *current_pstate_driver;
85 static struct cpufreq_driver amd_pstate_driver;
86 static struct cpufreq_driver amd_pstate_epp_driver;
87 static int cppc_state = AMD_PSTATE_UNDEFINED;
88 static bool cppc_enabled;
89 static bool amd_pstate_prefcore = true;
90 static struct quirk_entry *quirks;
91 
92 #define AMD_CPPC_MAX_PERF_MASK		GENMASK(7, 0)
93 #define AMD_CPPC_MIN_PERF_MASK		GENMASK(15, 8)
94 #define AMD_CPPC_DES_PERF_MASK		GENMASK(23, 16)
95 #define AMD_CPPC_EPP_PERF_MASK		GENMASK(31, 24)
96 
97 /*
98  * AMD Energy Preference Performance (EPP)
99  * The EPP is used in the CCLK DPM controller to drive
100  * the frequency that a core is going to operate during
101  * short periods of activity. EPP values will be utilized for
102  * different OS profiles (balanced, performance, power savings)
103  * display strings corresponding to EPP index in the
104  * energy_perf_strings[]
105  *	index		String
106  *-------------------------------------
107  *	0		default
108  *	1		performance
109  *	2		balance_performance
110  *	3		balance_power
111  *	4		power
112  */
113 enum energy_perf_value_index {
114 	EPP_INDEX_DEFAULT = 0,
115 	EPP_INDEX_PERFORMANCE,
116 	EPP_INDEX_BALANCE_PERFORMANCE,
117 	EPP_INDEX_BALANCE_POWERSAVE,
118 	EPP_INDEX_POWERSAVE,
119 };
120 
121 static const char * const energy_perf_strings[] = {
122 	[EPP_INDEX_DEFAULT] = "default",
123 	[EPP_INDEX_PERFORMANCE] = "performance",
124 	[EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
125 	[EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
126 	[EPP_INDEX_POWERSAVE] = "power",
127 	NULL
128 };
129 
130 static unsigned int epp_values[] = {
131 	[EPP_INDEX_DEFAULT] = 0,
132 	[EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
133 	[EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
134 	[EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
135 	[EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
136  };
137 
138 typedef int (*cppc_mode_transition_fn)(int);
139 
140 static struct quirk_entry quirk_amd_7k62 = {
141 	.nominal_freq = 2600,
142 	.lowest_freq = 550,
143 };
144 
dmi_matched_7k62_bios_bug(const struct dmi_system_id * dmi)145 static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
146 {
147 	/**
148 	 * match the broken bios for family 17h processor support CPPC V2
149 	 * broken BIOS lack of nominal_freq and lowest_freq capabilities
150 	 * definition in ACPI tables
151 	 */
152 	if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
153 		quirks = dmi->driver_data;
154 		pr_info("Overriding nominal and lowest frequencies for %s\n", dmi->ident);
155 		return 1;
156 	}
157 
158 	return 0;
159 }
160 
161 static const struct dmi_system_id amd_pstate_quirks_table[] __initconst = {
162 	{
163 		.callback = dmi_matched_7k62_bios_bug,
164 		.ident = "AMD EPYC 7K62",
165 		.matches = {
166 			DMI_MATCH(DMI_BIOS_VERSION, "5.14"),
167 			DMI_MATCH(DMI_BIOS_RELEASE, "12/12/2019"),
168 		},
169 		.driver_data = &quirk_amd_7k62,
170 	},
171 	{}
172 };
173 MODULE_DEVICE_TABLE(dmi, amd_pstate_quirks_table);
174 
get_mode_idx_from_str(const char * str,size_t size)175 static inline int get_mode_idx_from_str(const char *str, size_t size)
176 {
177 	int i;
178 
179 	for (i=0; i < AMD_PSTATE_MAX; i++) {
180 		if (!strncmp(str, amd_pstate_mode_string[i], size))
181 			return i;
182 	}
183 	return -EINVAL;
184 }
185 
186 static DEFINE_MUTEX(amd_pstate_limits_lock);
187 static DEFINE_MUTEX(amd_pstate_driver_lock);
188 
msr_get_epp(struct amd_cpudata * cpudata)189 static u8 msr_get_epp(struct amd_cpudata *cpudata)
190 {
191 	u64 value;
192 	int ret;
193 
194 	ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
195 	if (ret < 0) {
196 		pr_debug("Could not retrieve energy perf value (%d)\n", ret);
197 		return ret;
198 	}
199 
200 	return FIELD_GET(AMD_CPPC_EPP_PERF_MASK, value);
201 }
202 
203 DEFINE_STATIC_CALL(amd_pstate_get_epp, msr_get_epp);
204 
amd_pstate_get_epp(struct amd_cpudata * cpudata)205 static inline s16 amd_pstate_get_epp(struct amd_cpudata *cpudata)
206 {
207 	return static_call(amd_pstate_get_epp)(cpudata);
208 }
209 
shmem_get_epp(struct amd_cpudata * cpudata)210 static u8 shmem_get_epp(struct amd_cpudata *cpudata)
211 {
212 	u64 epp;
213 	int ret;
214 
215 	ret = cppc_get_epp_perf(cpudata->cpu, &epp);
216 	if (ret < 0) {
217 		pr_debug("Could not retrieve energy perf value (%d)\n", ret);
218 		return ret;
219 	}
220 
221 	return FIELD_GET(AMD_CPPC_EPP_PERF_MASK, epp);
222 }
223 
msr_update_perf(struct amd_cpudata * cpudata,u8 min_perf,u8 des_perf,u8 max_perf,u8 epp,bool fast_switch)224 static int msr_update_perf(struct amd_cpudata *cpudata, u8 min_perf,
225 			   u8 des_perf, u8 max_perf, u8 epp, bool fast_switch)
226 {
227 	u64 value, prev;
228 
229 	value = prev = READ_ONCE(cpudata->cppc_req_cached);
230 
231 	value &= ~(AMD_CPPC_MAX_PERF_MASK | AMD_CPPC_MIN_PERF_MASK |
232 		   AMD_CPPC_DES_PERF_MASK | AMD_CPPC_EPP_PERF_MASK);
233 	value |= FIELD_PREP(AMD_CPPC_MAX_PERF_MASK, max_perf);
234 	value |= FIELD_PREP(AMD_CPPC_DES_PERF_MASK, des_perf);
235 	value |= FIELD_PREP(AMD_CPPC_MIN_PERF_MASK, min_perf);
236 	value |= FIELD_PREP(AMD_CPPC_EPP_PERF_MASK, epp);
237 
238 	if (value == prev)
239 		return 0;
240 
241 	if (fast_switch) {
242 		wrmsrl(MSR_AMD_CPPC_REQ, value);
243 		return 0;
244 	} else {
245 		int ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
246 
247 		if (ret)
248 			return ret;
249 	}
250 
251 	WRITE_ONCE(cpudata->cppc_req_cached, value);
252 	WRITE_ONCE(cpudata->epp_cached, epp);
253 
254 	return 0;
255 }
256 
257 DEFINE_STATIC_CALL(amd_pstate_update_perf, msr_update_perf);
258 
amd_pstate_update_perf(struct amd_cpudata * cpudata,u8 min_perf,u8 des_perf,u8 max_perf,u8 epp,bool fast_switch)259 static inline int amd_pstate_update_perf(struct amd_cpudata *cpudata,
260 					  u8 min_perf, u8 des_perf,
261 					  u8 max_perf, u8 epp,
262 					  bool fast_switch)
263 {
264 	return static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
265 						   max_perf, epp, fast_switch);
266 }
267 
msr_set_epp(struct amd_cpudata * cpudata,u8 epp)268 static int msr_set_epp(struct amd_cpudata *cpudata, u8 epp)
269 {
270 	u64 value, prev;
271 	int ret;
272 
273 	value = prev = READ_ONCE(cpudata->cppc_req_cached);
274 	value &= ~AMD_CPPC_EPP_PERF_MASK;
275 	value |= FIELD_PREP(AMD_CPPC_EPP_PERF_MASK, epp);
276 
277 	if (value == prev)
278 		return 0;
279 
280 	ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
281 	if (ret) {
282 		pr_err("failed to set energy perf value (%d)\n", ret);
283 		return ret;
284 	}
285 
286 	/* update both so that msr_update_perf() can effectively check */
287 	WRITE_ONCE(cpudata->epp_cached, epp);
288 	WRITE_ONCE(cpudata->cppc_req_cached, value);
289 
290 	return ret;
291 }
292 
293 DEFINE_STATIC_CALL(amd_pstate_set_epp, msr_set_epp);
294 
amd_pstate_set_epp(struct amd_cpudata * cpudata,u8 epp)295 static inline int amd_pstate_set_epp(struct amd_cpudata *cpudata, u8 epp)
296 {
297 	return static_call(amd_pstate_set_epp)(cpudata, epp);
298 }
299 
shmem_set_epp(struct amd_cpudata * cpudata,u8 epp)300 static int shmem_set_epp(struct amd_cpudata *cpudata, u8 epp)
301 {
302 	int ret;
303 	struct cppc_perf_ctrls perf_ctrls;
304 
305 	if (epp == cpudata->epp_cached)
306 		return 0;
307 
308 	perf_ctrls.energy_perf = epp;
309 	ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
310 	if (ret) {
311 		pr_debug("failed to set energy perf value (%d)\n", ret);
312 		return ret;
313 	}
314 	WRITE_ONCE(cpudata->epp_cached, epp);
315 
316 	return ret;
317 }
318 
amd_pstate_set_energy_pref_index(struct cpufreq_policy * policy,int pref_index)319 static int amd_pstate_set_energy_pref_index(struct cpufreq_policy *policy,
320 					    int pref_index)
321 {
322 	struct amd_cpudata *cpudata = policy->driver_data;
323 	u8 epp;
324 
325 	if (!pref_index)
326 		epp = cpudata->epp_default;
327 	else
328 		epp = epp_values[pref_index];
329 
330 	if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
331 		pr_debug("EPP cannot be set under performance policy\n");
332 		return -EBUSY;
333 	}
334 
335 	if (trace_amd_pstate_epp_perf_enabled()) {
336 		trace_amd_pstate_epp_perf(cpudata->cpu, cpudata->highest_perf,
337 					  epp,
338 					  FIELD_GET(AMD_CPPC_MIN_PERF_MASK, cpudata->cppc_req_cached),
339 					  FIELD_GET(AMD_CPPC_MAX_PERF_MASK, cpudata->cppc_req_cached),
340 					  policy->boost_enabled);
341 	}
342 
343 	return amd_pstate_set_epp(cpudata, epp);
344 }
345 
msr_cppc_enable(bool enable)346 static inline int msr_cppc_enable(bool enable)
347 {
348 	int ret, cpu;
349 	unsigned long logical_proc_id_mask = 0;
350 
351        /*
352         * MSR_AMD_CPPC_ENABLE is write-once, once set it cannot be cleared.
353         */
354 	if (!enable)
355 		return 0;
356 
357 	if (enable == cppc_enabled)
358 		return 0;
359 
360 	for_each_present_cpu(cpu) {
361 		unsigned long logical_id = topology_logical_package_id(cpu);
362 
363 		if (test_bit(logical_id, &logical_proc_id_mask))
364 			continue;
365 
366 		set_bit(logical_id, &logical_proc_id_mask);
367 
368 		ret = wrmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_ENABLE,
369 				enable);
370 		if (ret)
371 			return ret;
372 	}
373 
374 	cppc_enabled = enable;
375 	return 0;
376 }
377 
shmem_cppc_enable(bool enable)378 static int shmem_cppc_enable(bool enable)
379 {
380 	int cpu, ret = 0;
381 	struct cppc_perf_ctrls perf_ctrls;
382 
383 	if (enable == cppc_enabled)
384 		return 0;
385 
386 	for_each_present_cpu(cpu) {
387 		ret = cppc_set_enable(cpu, enable);
388 		if (ret)
389 			return ret;
390 
391 		/* Enable autonomous mode for EPP */
392 		if (cppc_state == AMD_PSTATE_ACTIVE) {
393 			/* Set desired perf as zero to allow EPP firmware control */
394 			perf_ctrls.desired_perf = 0;
395 			ret = cppc_set_perf(cpu, &perf_ctrls);
396 			if (ret)
397 				return ret;
398 		}
399 	}
400 
401 	cppc_enabled = enable;
402 	return ret;
403 }
404 
405 DEFINE_STATIC_CALL(amd_pstate_cppc_enable, msr_cppc_enable);
406 
amd_pstate_cppc_enable(bool enable)407 static inline int amd_pstate_cppc_enable(bool enable)
408 {
409 	return static_call(amd_pstate_cppc_enable)(enable);
410 }
411 
msr_init_perf(struct amd_cpudata * cpudata)412 static int msr_init_perf(struct amd_cpudata *cpudata)
413 {
414 	u64 cap1, numerator;
415 
416 	int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
417 				     &cap1);
418 	if (ret)
419 		return ret;
420 
421 	ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
422 	if (ret)
423 		return ret;
424 
425 	WRITE_ONCE(cpudata->highest_perf, numerator);
426 	WRITE_ONCE(cpudata->max_limit_perf, numerator);
427 	WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
428 	WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
429 	WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
430 	WRITE_ONCE(cpudata->prefcore_ranking, AMD_CPPC_HIGHEST_PERF(cap1));
431 	WRITE_ONCE(cpudata->min_limit_perf, AMD_CPPC_LOWEST_PERF(cap1));
432 	return 0;
433 }
434 
shmem_init_perf(struct amd_cpudata * cpudata)435 static int shmem_init_perf(struct amd_cpudata *cpudata)
436 {
437 	struct cppc_perf_caps cppc_perf;
438 	u64 numerator;
439 
440 	int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
441 	if (ret)
442 		return ret;
443 
444 	ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
445 	if (ret)
446 		return ret;
447 
448 	WRITE_ONCE(cpudata->highest_perf, numerator);
449 	WRITE_ONCE(cpudata->max_limit_perf, numerator);
450 	WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
451 	WRITE_ONCE(cpudata->lowest_nonlinear_perf,
452 		   cppc_perf.lowest_nonlinear_perf);
453 	WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
454 	WRITE_ONCE(cpudata->prefcore_ranking, cppc_perf.highest_perf);
455 	WRITE_ONCE(cpudata->min_limit_perf, cppc_perf.lowest_perf);
456 
457 	if (cppc_state == AMD_PSTATE_ACTIVE)
458 		return 0;
459 
460 	ret = cppc_get_auto_sel_caps(cpudata->cpu, &cppc_perf);
461 	if (ret) {
462 		pr_warn("failed to get auto_sel, ret: %d\n", ret);
463 		return 0;
464 	}
465 
466 	ret = cppc_set_auto_sel(cpudata->cpu,
467 			(cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
468 
469 	if (ret)
470 		pr_warn("failed to set auto_sel, ret: %d\n", ret);
471 
472 	return ret;
473 }
474 
475 DEFINE_STATIC_CALL(amd_pstate_init_perf, msr_init_perf);
476 
amd_pstate_init_perf(struct amd_cpudata * cpudata)477 static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
478 {
479 	return static_call(amd_pstate_init_perf)(cpudata);
480 }
481 
shmem_update_perf(struct amd_cpudata * cpudata,u8 min_perf,u8 des_perf,u8 max_perf,u8 epp,bool fast_switch)482 static int shmem_update_perf(struct amd_cpudata *cpudata, u8 min_perf,
483 			     u8 des_perf, u8 max_perf, u8 epp, bool fast_switch)
484 {
485 	struct cppc_perf_ctrls perf_ctrls;
486 
487 	if (cppc_state == AMD_PSTATE_ACTIVE) {
488 		int ret = shmem_set_epp(cpudata, epp);
489 
490 		if (ret)
491 			return ret;
492 	}
493 
494 	perf_ctrls.max_perf = max_perf;
495 	perf_ctrls.min_perf = min_perf;
496 	perf_ctrls.desired_perf = des_perf;
497 
498 	return cppc_set_perf(cpudata->cpu, &perf_ctrls);
499 }
500 
amd_pstate_sample(struct amd_cpudata * cpudata)501 static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
502 {
503 	u64 aperf, mperf, tsc;
504 	unsigned long flags;
505 
506 	local_irq_save(flags);
507 	rdmsrl(MSR_IA32_APERF, aperf);
508 	rdmsrl(MSR_IA32_MPERF, mperf);
509 	tsc = rdtsc();
510 
511 	if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) {
512 		local_irq_restore(flags);
513 		return false;
514 	}
515 
516 	local_irq_restore(flags);
517 
518 	cpudata->cur.aperf = aperf;
519 	cpudata->cur.mperf = mperf;
520 	cpudata->cur.tsc =  tsc;
521 	cpudata->cur.aperf -= cpudata->prev.aperf;
522 	cpudata->cur.mperf -= cpudata->prev.mperf;
523 	cpudata->cur.tsc -= cpudata->prev.tsc;
524 
525 	cpudata->prev.aperf = aperf;
526 	cpudata->prev.mperf = mperf;
527 	cpudata->prev.tsc = tsc;
528 
529 	cpudata->freq = div64_u64((cpudata->cur.aperf * cpu_khz), cpudata->cur.mperf);
530 
531 	return true;
532 }
533 
amd_pstate_update(struct amd_cpudata * cpudata,u8 min_perf,u8 des_perf,u8 max_perf,bool fast_switch,int gov_flags)534 static void amd_pstate_update(struct amd_cpudata *cpudata, u8 min_perf,
535 			      u8 des_perf, u8 max_perf, bool fast_switch, int gov_flags)
536 {
537 	unsigned long max_freq;
538 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpudata->cpu);
539 	u8 nominal_perf = READ_ONCE(cpudata->nominal_perf);
540 
541 	if (!policy)
542 		return;
543 
544 	des_perf = clamp_t(u8, des_perf, min_perf, max_perf);
545 
546 	max_freq = READ_ONCE(cpudata->max_limit_freq);
547 	policy->cur = div_u64(des_perf * max_freq, max_perf);
548 
549 	if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
550 		min_perf = des_perf;
551 		des_perf = 0;
552 	}
553 
554 	/* limit the max perf when core performance boost feature is disabled */
555 	if (!cpudata->boost_supported)
556 		max_perf = min_t(u8, nominal_perf, max_perf);
557 
558 	if (trace_amd_pstate_perf_enabled() && amd_pstate_sample(cpudata)) {
559 		trace_amd_pstate_perf(min_perf, des_perf, max_perf, cpudata->freq,
560 			cpudata->cur.mperf, cpudata->cur.aperf, cpudata->cur.tsc,
561 				cpudata->cpu, fast_switch);
562 	}
563 
564 	amd_pstate_update_perf(cpudata, min_perf, des_perf, max_perf, 0, fast_switch);
565 
566 	cpufreq_cpu_put(policy);
567 }
568 
amd_pstate_verify(struct cpufreq_policy_data * policy_data)569 static int amd_pstate_verify(struct cpufreq_policy_data *policy_data)
570 {
571 	/*
572 	 * Initialize lower frequency limit (i.e.policy->min) with
573 	 * lowest_nonlinear_frequency which is the most energy efficient
574 	 * frequency. Override the initial value set by cpufreq core and
575 	 * amd-pstate qos_requests.
576 	 */
577 	if (policy_data->min == FREQ_QOS_MIN_DEFAULT_VALUE) {
578 		struct cpufreq_policy *policy = cpufreq_cpu_get(policy_data->cpu);
579 		struct amd_cpudata *cpudata;
580 
581 		if (!policy)
582 			return -EINVAL;
583 
584 		cpudata = policy->driver_data;
585 		policy_data->min = cpudata->lowest_nonlinear_freq;
586 		cpufreq_cpu_put(policy);
587 	}
588 
589 	cpufreq_verify_within_cpu_limits(policy_data);
590 	pr_debug("policy_max =%d, policy_min=%d\n", policy_data->max, policy_data->min);
591 
592 	return 0;
593 }
594 
amd_pstate_update_min_max_limit(struct cpufreq_policy * policy)595 static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
596 {
597 	u8 max_limit_perf, min_limit_perf, max_perf;
598 	u32 max_freq;
599 	struct amd_cpudata *cpudata = policy->driver_data;
600 
601 	max_perf = READ_ONCE(cpudata->highest_perf);
602 	max_freq = READ_ONCE(cpudata->max_freq);
603 	max_limit_perf = div_u64(policy->max * max_perf, max_freq);
604 	min_limit_perf = div_u64(policy->min * max_perf, max_freq);
605 
606 	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
607 		min_limit_perf = min(cpudata->nominal_perf, max_limit_perf);
608 
609 	WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
610 	WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
611 	WRITE_ONCE(cpudata->max_limit_freq, policy->max);
612 	WRITE_ONCE(cpudata->min_limit_freq, policy->min);
613 
614 	return 0;
615 }
616 
amd_pstate_update_freq(struct cpufreq_policy * policy,unsigned int target_freq,bool fast_switch)617 static int amd_pstate_update_freq(struct cpufreq_policy *policy,
618 				  unsigned int target_freq, bool fast_switch)
619 {
620 	struct cpufreq_freqs freqs;
621 	struct amd_cpudata *cpudata = policy->driver_data;
622 	u8 des_perf, cap_perf;
623 
624 	if (!cpudata->max_freq)
625 		return -ENODEV;
626 
627 	if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
628 		amd_pstate_update_min_max_limit(policy);
629 
630 	cap_perf = READ_ONCE(cpudata->highest_perf);
631 
632 	freqs.old = policy->cur;
633 	freqs.new = target_freq;
634 
635 	des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
636 				     cpudata->max_freq);
637 
638 	WARN_ON(fast_switch && !policy->fast_switch_enabled);
639 	/*
640 	 * If fast_switch is desired, then there aren't any registered
641 	 * transition notifiers. See comment for
642 	 * cpufreq_enable_fast_switch().
643 	 */
644 	if (!fast_switch)
645 		cpufreq_freq_transition_begin(policy, &freqs);
646 
647 	amd_pstate_update(cpudata, cpudata->min_limit_perf, des_perf,
648 			  cpudata->max_limit_perf, fast_switch,
649 			  policy->governor->flags);
650 
651 	if (!fast_switch)
652 		cpufreq_freq_transition_end(policy, &freqs, false);
653 
654 	return 0;
655 }
656 
amd_pstate_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)657 static int amd_pstate_target(struct cpufreq_policy *policy,
658 			     unsigned int target_freq,
659 			     unsigned int relation)
660 {
661 	return amd_pstate_update_freq(policy, target_freq, false);
662 }
663 
amd_pstate_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)664 static unsigned int amd_pstate_fast_switch(struct cpufreq_policy *policy,
665 				  unsigned int target_freq)
666 {
667 	if (!amd_pstate_update_freq(policy, target_freq, true))
668 		return target_freq;
669 	return policy->cur;
670 }
671 
amd_pstate_adjust_perf(unsigned int cpu,unsigned long _min_perf,unsigned long target_perf,unsigned long capacity)672 static void amd_pstate_adjust_perf(unsigned int cpu,
673 				   unsigned long _min_perf,
674 				   unsigned long target_perf,
675 				   unsigned long capacity)
676 {
677 	u8 max_perf, min_perf, des_perf, cap_perf, min_limit_perf;
678 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
679 	struct amd_cpudata *cpudata;
680 
681 	if (!policy)
682 		return;
683 
684 	cpudata = policy->driver_data;
685 
686 	if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
687 		amd_pstate_update_min_max_limit(policy);
688 
689 	cap_perf = READ_ONCE(cpudata->highest_perf);
690 	min_limit_perf = READ_ONCE(cpudata->min_limit_perf);
691 
692 	des_perf = cap_perf;
693 	if (target_perf < capacity)
694 		des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
695 
696 	if (_min_perf < capacity)
697 		min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
698 	else
699 		min_perf = cap_perf;
700 
701 	if (min_perf < min_limit_perf)
702 		min_perf = min_limit_perf;
703 
704 	max_perf = cpudata->max_limit_perf;
705 	if (max_perf < min_perf)
706 		max_perf = min_perf;
707 
708 	des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
709 
710 	amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true,
711 			policy->governor->flags);
712 	cpufreq_cpu_put(policy);
713 }
714 
amd_pstate_cpu_boost_update(struct cpufreq_policy * policy,bool on)715 static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
716 {
717 	struct amd_cpudata *cpudata = policy->driver_data;
718 	u32 nominal_freq, max_freq;
719 	int ret = 0;
720 
721 	nominal_freq = READ_ONCE(cpudata->nominal_freq);
722 	max_freq = READ_ONCE(cpudata->max_freq);
723 
724 	if (on)
725 		policy->cpuinfo.max_freq = max_freq;
726 	else if (policy->cpuinfo.max_freq > nominal_freq)
727 		policy->cpuinfo.max_freq = nominal_freq;
728 
729 	policy->max = policy->cpuinfo.max_freq;
730 
731 	if (cppc_state == AMD_PSTATE_PASSIVE) {
732 		ret = freq_qos_update_request(&cpudata->req[1], policy->cpuinfo.max_freq);
733 		if (ret < 0)
734 			pr_debug("Failed to update freq constraint: CPU%d\n", cpudata->cpu);
735 	}
736 
737 	return ret < 0 ? ret : 0;
738 }
739 
amd_pstate_set_boost(struct cpufreq_policy * policy,int state)740 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
741 {
742 	struct amd_cpudata *cpudata = policy->driver_data;
743 	int ret;
744 
745 	if (!cpudata->boost_supported) {
746 		pr_err("Boost mode is not supported by this processor or SBIOS\n");
747 		return -EOPNOTSUPP;
748 	}
749 	guard(mutex)(&amd_pstate_driver_lock);
750 
751 	ret = amd_pstate_cpu_boost_update(policy, state);
752 	refresh_frequency_limits(policy);
753 
754 	return ret;
755 }
756 
amd_pstate_init_boost_support(struct amd_cpudata * cpudata)757 static int amd_pstate_init_boost_support(struct amd_cpudata *cpudata)
758 {
759 	u64 boost_val;
760 	int ret = -1;
761 
762 	/*
763 	 * If platform has no CPB support or disable it, initialize current driver
764 	 * boost_enabled state to be false, it is not an error for cpufreq core to handle.
765 	 */
766 	if (!cpu_feature_enabled(X86_FEATURE_CPB)) {
767 		pr_debug_once("Boost CPB capabilities not present in the processor\n");
768 		ret = 0;
769 		goto exit_err;
770 	}
771 
772 	ret = rdmsrl_on_cpu(cpudata->cpu, MSR_K7_HWCR, &boost_val);
773 	if (ret) {
774 		pr_err_once("failed to read initial CPU boost state!\n");
775 		ret = -EIO;
776 		goto exit_err;
777 	}
778 
779 	if (!(boost_val & MSR_K7_HWCR_CPB_DIS))
780 		cpudata->boost_supported = true;
781 
782 	return 0;
783 
784 exit_err:
785 	cpudata->boost_supported = false;
786 	return ret;
787 }
788 
amd_perf_ctl_reset(unsigned int cpu)789 static void amd_perf_ctl_reset(unsigned int cpu)
790 {
791 	wrmsrl_on_cpu(cpu, MSR_AMD_PERF_CTL, 0);
792 }
793 
794 /*
795  * Set amd-pstate preferred core enable can't be done directly from cpufreq callbacks
796  * due to locking, so queue the work for later.
797  */
amd_pstste_sched_prefcore_workfn(struct work_struct * work)798 static void amd_pstste_sched_prefcore_workfn(struct work_struct *work)
799 {
800 	sched_set_itmt_support();
801 }
802 static DECLARE_WORK(sched_prefcore_work, amd_pstste_sched_prefcore_workfn);
803 
804 #define CPPC_MAX_PERF	U8_MAX
805 
amd_pstate_init_prefcore(struct amd_cpudata * cpudata)806 static void amd_pstate_init_prefcore(struct amd_cpudata *cpudata)
807 {
808 	/* user disabled or not detected */
809 	if (!amd_pstate_prefcore)
810 		return;
811 
812 	cpudata->hw_prefcore = true;
813 
814 	/*
815 	 * The priorities can be set regardless of whether or not
816 	 * sched_set_itmt_support(true) has been called and it is valid to
817 	 * update them at any time after it has been called.
818 	 */
819 	sched_set_itmt_core_prio((int)READ_ONCE(cpudata->prefcore_ranking), cpudata->cpu);
820 
821 	schedule_work(&sched_prefcore_work);
822 }
823 
amd_pstate_update_limits(unsigned int cpu)824 static void amd_pstate_update_limits(unsigned int cpu)
825 {
826 	struct cpufreq_policy *policy = NULL;
827 	struct amd_cpudata *cpudata;
828 	u32 prev_high = 0, cur_high = 0;
829 	int ret;
830 	bool highest_perf_changed = false;
831 
832 	if (!amd_pstate_prefcore)
833 		return;
834 
835 	policy = cpufreq_cpu_get(cpu);
836 	if (!policy)
837 		return;
838 
839 	cpudata = policy->driver_data;
840 
841 	guard(mutex)(&amd_pstate_driver_lock);
842 
843 	ret = amd_get_highest_perf(cpu, &cur_high);
844 	if (ret) {
845 		cpufreq_cpu_put(policy);
846 		return;
847 	}
848 
849 	prev_high = READ_ONCE(cpudata->prefcore_ranking);
850 	highest_perf_changed = (prev_high != cur_high);
851 	if (highest_perf_changed) {
852 		WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
853 
854 		if (cur_high < CPPC_MAX_PERF)
855 			sched_set_itmt_core_prio((int)cur_high, cpu);
856 	}
857 	cpufreq_cpu_put(policy);
858 
859 	if (!highest_perf_changed)
860 		cpufreq_update_policy(cpu);
861 
862 }
863 
864 /*
865  * Get pstate transition delay time from ACPI tables that firmware set
866  * instead of using hardcode value directly.
867  */
amd_pstate_get_transition_delay_us(unsigned int cpu)868 static u32 amd_pstate_get_transition_delay_us(unsigned int cpu)
869 {
870 	u32 transition_delay_ns;
871 
872 	transition_delay_ns = cppc_get_transition_latency(cpu);
873 	if (transition_delay_ns == CPUFREQ_ETERNAL) {
874 		if (cpu_feature_enabled(X86_FEATURE_AMD_FAST_CPPC))
875 			return AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY;
876 		else
877 			return AMD_PSTATE_TRANSITION_DELAY;
878 	}
879 
880 	return transition_delay_ns / NSEC_PER_USEC;
881 }
882 
883 /*
884  * Get pstate transition latency value from ACPI tables that firmware
885  * set instead of using hardcode value directly.
886  */
amd_pstate_get_transition_latency(unsigned int cpu)887 static u32 amd_pstate_get_transition_latency(unsigned int cpu)
888 {
889 	u32 transition_latency;
890 
891 	transition_latency = cppc_get_transition_latency(cpu);
892 	if (transition_latency  == CPUFREQ_ETERNAL)
893 		return AMD_PSTATE_TRANSITION_LATENCY;
894 
895 	return transition_latency;
896 }
897 
898 /*
899  * amd_pstate_init_freq: Initialize the max_freq, min_freq,
900  *                       nominal_freq and lowest_nonlinear_freq for
901  *                       the @cpudata object.
902  *
903  *  Requires: highest_perf, lowest_perf, nominal_perf and
904  *            lowest_nonlinear_perf members of @cpudata to be
905  *            initialized.
906  *
907  *  Returns 0 on success, non-zero value on failure.
908  */
amd_pstate_init_freq(struct amd_cpudata * cpudata)909 static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
910 {
911 	int ret;
912 	u32 min_freq, max_freq;
913 	u8 highest_perf, nominal_perf, lowest_nonlinear_perf;
914 	u32 nominal_freq, lowest_nonlinear_freq;
915 	struct cppc_perf_caps cppc_perf;
916 
917 	ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
918 	if (ret)
919 		return ret;
920 
921 	if (quirks && quirks->lowest_freq)
922 		min_freq = quirks->lowest_freq;
923 	else
924 		min_freq = cppc_perf.lowest_freq;
925 
926 	if (quirks && quirks->nominal_freq)
927 		nominal_freq = quirks->nominal_freq;
928 	else
929 		nominal_freq = cppc_perf.nominal_freq;
930 
931 	highest_perf = READ_ONCE(cpudata->highest_perf);
932 	nominal_perf = READ_ONCE(cpudata->nominal_perf);
933 	max_freq = div_u64((u64)highest_perf * nominal_freq, nominal_perf);
934 
935 	lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
936 	lowest_nonlinear_freq = div_u64((u64)nominal_freq * lowest_nonlinear_perf, nominal_perf);
937 	WRITE_ONCE(cpudata->min_freq, min_freq * 1000);
938 	WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq * 1000);
939 	WRITE_ONCE(cpudata->nominal_freq, nominal_freq * 1000);
940 	WRITE_ONCE(cpudata->max_freq, max_freq * 1000);
941 
942 	/**
943 	 * Below values need to be initialized correctly, otherwise driver will fail to load
944 	 * max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf
945 	 * lowest_nonlinear_freq is a value between [min_freq, nominal_freq]
946 	 * Check _CPC in ACPI table objects if any values are incorrect
947 	 */
948 	if (min_freq <= 0 || max_freq <= 0 || nominal_freq <= 0 || min_freq > max_freq) {
949 		pr_err("min_freq(%d) or max_freq(%d) or nominal_freq(%d) value is incorrect\n",
950 			min_freq, max_freq, nominal_freq);
951 		return -EINVAL;
952 	}
953 
954 	if (lowest_nonlinear_freq <= min_freq || lowest_nonlinear_freq > nominal_freq) {
955 		pr_err("lowest_nonlinear_freq(%d) value is out of range [min_freq(%d), nominal_freq(%d)]\n",
956 			lowest_nonlinear_freq, min_freq, nominal_freq);
957 		return -EINVAL;
958 	}
959 
960 	return 0;
961 }
962 
amd_pstate_cpu_init(struct cpufreq_policy * policy)963 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
964 {
965 	int min_freq, max_freq, ret;
966 	struct device *dev;
967 	struct amd_cpudata *cpudata;
968 
969 	/*
970 	 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
971 	 * which is ideal for initialization process.
972 	 */
973 	amd_perf_ctl_reset(policy->cpu);
974 	dev = get_cpu_device(policy->cpu);
975 	if (!dev)
976 		return -ENODEV;
977 
978 	cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
979 	if (!cpudata)
980 		return -ENOMEM;
981 
982 	cpudata->cpu = policy->cpu;
983 
984 	ret = amd_pstate_init_perf(cpudata);
985 	if (ret)
986 		goto free_cpudata1;
987 
988 	amd_pstate_init_prefcore(cpudata);
989 
990 	ret = amd_pstate_init_freq(cpudata);
991 	if (ret)
992 		goto free_cpudata1;
993 
994 	ret = amd_pstate_init_boost_support(cpudata);
995 	if (ret)
996 		goto free_cpudata1;
997 
998 	min_freq = READ_ONCE(cpudata->min_freq);
999 	max_freq = READ_ONCE(cpudata->max_freq);
1000 
1001 	policy->cpuinfo.transition_latency = amd_pstate_get_transition_latency(policy->cpu);
1002 	policy->transition_delay_us = amd_pstate_get_transition_delay_us(policy->cpu);
1003 
1004 	policy->min = min_freq;
1005 	policy->max = max_freq;
1006 
1007 	policy->cpuinfo.min_freq = min_freq;
1008 	policy->cpuinfo.max_freq = max_freq;
1009 
1010 	policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
1011 
1012 	/* It will be updated by governor */
1013 	policy->cur = policy->cpuinfo.min_freq;
1014 
1015 	if (cpu_feature_enabled(X86_FEATURE_CPPC))
1016 		policy->fast_switch_possible = true;
1017 
1018 	ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
1019 				   FREQ_QOS_MIN, FREQ_QOS_MIN_DEFAULT_VALUE);
1020 	if (ret < 0) {
1021 		dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
1022 		goto free_cpudata1;
1023 	}
1024 
1025 	ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
1026 				   FREQ_QOS_MAX, policy->cpuinfo.max_freq);
1027 	if (ret < 0) {
1028 		dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
1029 		goto free_cpudata2;
1030 	}
1031 
1032 	cpudata->max_limit_freq = max_freq;
1033 	cpudata->min_limit_freq = min_freq;
1034 
1035 	policy->driver_data = cpudata;
1036 
1037 	if (!current_pstate_driver->adjust_perf)
1038 		current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1039 
1040 	return 0;
1041 
1042 free_cpudata2:
1043 	freq_qos_remove_request(&cpudata->req[0]);
1044 free_cpudata1:
1045 	kfree(cpudata);
1046 	return ret;
1047 }
1048 
amd_pstate_cpu_exit(struct cpufreq_policy * policy)1049 static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
1050 {
1051 	struct amd_cpudata *cpudata = policy->driver_data;
1052 
1053 	freq_qos_remove_request(&cpudata->req[1]);
1054 	freq_qos_remove_request(&cpudata->req[0]);
1055 	policy->fast_switch_possible = false;
1056 	kfree(cpudata);
1057 }
1058 
amd_pstate_cpu_resume(struct cpufreq_policy * policy)1059 static int amd_pstate_cpu_resume(struct cpufreq_policy *policy)
1060 {
1061 	int ret;
1062 
1063 	ret = amd_pstate_cppc_enable(true);
1064 	if (ret)
1065 		pr_err("failed to enable amd-pstate during resume, return %d\n", ret);
1066 
1067 	return ret;
1068 }
1069 
amd_pstate_cpu_suspend(struct cpufreq_policy * policy)1070 static int amd_pstate_cpu_suspend(struct cpufreq_policy *policy)
1071 {
1072 	int ret;
1073 
1074 	ret = amd_pstate_cppc_enable(false);
1075 	if (ret)
1076 		pr_err("failed to disable amd-pstate during suspend, return %d\n", ret);
1077 
1078 	return ret;
1079 }
1080 
1081 /* Sysfs attributes */
1082 
1083 /*
1084  * This frequency is to indicate the maximum hardware frequency.
1085  * If boost is not active but supported, the frequency will be larger than the
1086  * one in cpuinfo.
1087  */
show_amd_pstate_max_freq(struct cpufreq_policy * policy,char * buf)1088 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
1089 					char *buf)
1090 {
1091 	int max_freq;
1092 	struct amd_cpudata *cpudata = policy->driver_data;
1093 
1094 	max_freq = READ_ONCE(cpudata->max_freq);
1095 	if (max_freq < 0)
1096 		return max_freq;
1097 
1098 	return sysfs_emit(buf, "%u\n", max_freq);
1099 }
1100 
show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy * policy,char * buf)1101 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
1102 						     char *buf)
1103 {
1104 	int freq;
1105 	struct amd_cpudata *cpudata = policy->driver_data;
1106 
1107 	freq = READ_ONCE(cpudata->lowest_nonlinear_freq);
1108 	if (freq < 0)
1109 		return freq;
1110 
1111 	return sysfs_emit(buf, "%u\n", freq);
1112 }
1113 
1114 /*
1115  * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
1116  * need to expose it to sysfs.
1117  */
show_amd_pstate_highest_perf(struct cpufreq_policy * policy,char * buf)1118 static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
1119 					    char *buf)
1120 {
1121 	u8 perf;
1122 	struct amd_cpudata *cpudata = policy->driver_data;
1123 
1124 	perf = READ_ONCE(cpudata->highest_perf);
1125 
1126 	return sysfs_emit(buf, "%u\n", perf);
1127 }
1128 
show_amd_pstate_prefcore_ranking(struct cpufreq_policy * policy,char * buf)1129 static ssize_t show_amd_pstate_prefcore_ranking(struct cpufreq_policy *policy,
1130 						char *buf)
1131 {
1132 	u8 perf;
1133 	struct amd_cpudata *cpudata = policy->driver_data;
1134 
1135 	perf = READ_ONCE(cpudata->prefcore_ranking);
1136 
1137 	return sysfs_emit(buf, "%u\n", perf);
1138 }
1139 
show_amd_pstate_hw_prefcore(struct cpufreq_policy * policy,char * buf)1140 static ssize_t show_amd_pstate_hw_prefcore(struct cpufreq_policy *policy,
1141 					   char *buf)
1142 {
1143 	bool hw_prefcore;
1144 	struct amd_cpudata *cpudata = policy->driver_data;
1145 
1146 	hw_prefcore = READ_ONCE(cpudata->hw_prefcore);
1147 
1148 	return sysfs_emit(buf, "%s\n", str_enabled_disabled(hw_prefcore));
1149 }
1150 
show_energy_performance_available_preferences(struct cpufreq_policy * policy,char * buf)1151 static ssize_t show_energy_performance_available_preferences(
1152 				struct cpufreq_policy *policy, char *buf)
1153 {
1154 	int i = 0;
1155 	int offset = 0;
1156 	struct amd_cpudata *cpudata = policy->driver_data;
1157 
1158 	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1159 		return sysfs_emit_at(buf, offset, "%s\n",
1160 				energy_perf_strings[EPP_INDEX_PERFORMANCE]);
1161 
1162 	while (energy_perf_strings[i] != NULL)
1163 		offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]);
1164 
1165 	offset += sysfs_emit_at(buf, offset, "\n");
1166 
1167 	return offset;
1168 }
1169 
store_energy_performance_preference(struct cpufreq_policy * policy,const char * buf,size_t count)1170 static ssize_t store_energy_performance_preference(
1171 		struct cpufreq_policy *policy, const char *buf, size_t count)
1172 {
1173 	char str_preference[21];
1174 	ssize_t ret;
1175 
1176 	ret = sscanf(buf, "%20s", str_preference);
1177 	if (ret != 1)
1178 		return -EINVAL;
1179 
1180 	ret = match_string(energy_perf_strings, -1, str_preference);
1181 	if (ret < 0)
1182 		return -EINVAL;
1183 
1184 	guard(mutex)(&amd_pstate_limits_lock);
1185 
1186 	ret = amd_pstate_set_energy_pref_index(policy, ret);
1187 
1188 	return ret ? ret : count;
1189 }
1190 
show_energy_performance_preference(struct cpufreq_policy * policy,char * buf)1191 static ssize_t show_energy_performance_preference(
1192 				struct cpufreq_policy *policy, char *buf)
1193 {
1194 	struct amd_cpudata *cpudata = policy->driver_data;
1195 	u8 preference;
1196 
1197 	switch (cpudata->epp_cached) {
1198 	case AMD_CPPC_EPP_PERFORMANCE:
1199 		preference = EPP_INDEX_PERFORMANCE;
1200 		break;
1201 	case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
1202 		preference = EPP_INDEX_BALANCE_PERFORMANCE;
1203 		break;
1204 	case AMD_CPPC_EPP_BALANCE_POWERSAVE:
1205 		preference = EPP_INDEX_BALANCE_POWERSAVE;
1206 		break;
1207 	case AMD_CPPC_EPP_POWERSAVE:
1208 		preference = EPP_INDEX_POWERSAVE;
1209 		break;
1210 	default:
1211 		return -EINVAL;
1212 	}
1213 
1214 	return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]);
1215 }
1216 
amd_pstate_driver_cleanup(void)1217 static void amd_pstate_driver_cleanup(void)
1218 {
1219 	amd_pstate_cppc_enable(false);
1220 	cppc_state = AMD_PSTATE_DISABLE;
1221 	current_pstate_driver = NULL;
1222 }
1223 
amd_pstate_set_driver(int mode_idx)1224 static int amd_pstate_set_driver(int mode_idx)
1225 {
1226 	if (mode_idx >= AMD_PSTATE_DISABLE && mode_idx < AMD_PSTATE_MAX) {
1227 		cppc_state = mode_idx;
1228 		if (cppc_state == AMD_PSTATE_DISABLE)
1229 			pr_info("driver is explicitly disabled\n");
1230 
1231 		if (cppc_state == AMD_PSTATE_ACTIVE)
1232 			current_pstate_driver = &amd_pstate_epp_driver;
1233 
1234 		if (cppc_state == AMD_PSTATE_PASSIVE || cppc_state == AMD_PSTATE_GUIDED)
1235 			current_pstate_driver = &amd_pstate_driver;
1236 
1237 		return 0;
1238 	}
1239 
1240 	return -EINVAL;
1241 }
1242 
amd_pstate_register_driver(int mode)1243 static int amd_pstate_register_driver(int mode)
1244 {
1245 	int ret;
1246 
1247 	ret = amd_pstate_set_driver(mode);
1248 	if (ret)
1249 		return ret;
1250 
1251 	cppc_state = mode;
1252 
1253 	ret = amd_pstate_cppc_enable(true);
1254 	if (ret) {
1255 		pr_err("failed to enable cppc during amd-pstate driver registration, return %d\n",
1256 		       ret);
1257 		amd_pstate_driver_cleanup();
1258 		return ret;
1259 	}
1260 
1261 	/* at least one CPU supports CPB */
1262 	current_pstate_driver->boost_enabled = cpu_feature_enabled(X86_FEATURE_CPB);
1263 
1264 	ret = cpufreq_register_driver(current_pstate_driver);
1265 	if (ret) {
1266 		amd_pstate_driver_cleanup();
1267 		return ret;
1268 	}
1269 
1270 	return 0;
1271 }
1272 
amd_pstate_unregister_driver(int dummy)1273 static int amd_pstate_unregister_driver(int dummy)
1274 {
1275 	cpufreq_unregister_driver(current_pstate_driver);
1276 	amd_pstate_driver_cleanup();
1277 	return 0;
1278 }
1279 
amd_pstate_change_mode_without_dvr_change(int mode)1280 static int amd_pstate_change_mode_without_dvr_change(int mode)
1281 {
1282 	int cpu = 0;
1283 
1284 	cppc_state = mode;
1285 
1286 	if (cpu_feature_enabled(X86_FEATURE_CPPC) || cppc_state == AMD_PSTATE_ACTIVE)
1287 		return 0;
1288 
1289 	for_each_present_cpu(cpu) {
1290 		cppc_set_auto_sel(cpu, (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
1291 	}
1292 
1293 	return 0;
1294 }
1295 
amd_pstate_change_driver_mode(int mode)1296 static int amd_pstate_change_driver_mode(int mode)
1297 {
1298 	int ret;
1299 
1300 	ret = amd_pstate_unregister_driver(0);
1301 	if (ret)
1302 		return ret;
1303 
1304 	ret = amd_pstate_register_driver(mode);
1305 	if (ret)
1306 		return ret;
1307 
1308 	return 0;
1309 }
1310 
1311 static cppc_mode_transition_fn mode_state_machine[AMD_PSTATE_MAX][AMD_PSTATE_MAX] = {
1312 	[AMD_PSTATE_DISABLE]         = {
1313 		[AMD_PSTATE_DISABLE]     = NULL,
1314 		[AMD_PSTATE_PASSIVE]     = amd_pstate_register_driver,
1315 		[AMD_PSTATE_ACTIVE]      = amd_pstate_register_driver,
1316 		[AMD_PSTATE_GUIDED]      = amd_pstate_register_driver,
1317 	},
1318 	[AMD_PSTATE_PASSIVE]         = {
1319 		[AMD_PSTATE_DISABLE]     = amd_pstate_unregister_driver,
1320 		[AMD_PSTATE_PASSIVE]     = NULL,
1321 		[AMD_PSTATE_ACTIVE]      = amd_pstate_change_driver_mode,
1322 		[AMD_PSTATE_GUIDED]      = amd_pstate_change_mode_without_dvr_change,
1323 	},
1324 	[AMD_PSTATE_ACTIVE]          = {
1325 		[AMD_PSTATE_DISABLE]     = amd_pstate_unregister_driver,
1326 		[AMD_PSTATE_PASSIVE]     = amd_pstate_change_driver_mode,
1327 		[AMD_PSTATE_ACTIVE]      = NULL,
1328 		[AMD_PSTATE_GUIDED]      = amd_pstate_change_driver_mode,
1329 	},
1330 	[AMD_PSTATE_GUIDED]          = {
1331 		[AMD_PSTATE_DISABLE]     = amd_pstate_unregister_driver,
1332 		[AMD_PSTATE_PASSIVE]     = amd_pstate_change_mode_without_dvr_change,
1333 		[AMD_PSTATE_ACTIVE]      = amd_pstate_change_driver_mode,
1334 		[AMD_PSTATE_GUIDED]      = NULL,
1335 	},
1336 };
1337 
amd_pstate_show_status(char * buf)1338 static ssize_t amd_pstate_show_status(char *buf)
1339 {
1340 	if (!current_pstate_driver)
1341 		return sysfs_emit(buf, "disable\n");
1342 
1343 	return sysfs_emit(buf, "%s\n", amd_pstate_mode_string[cppc_state]);
1344 }
1345 
amd_pstate_update_status(const char * buf,size_t size)1346 int amd_pstate_update_status(const char *buf, size_t size)
1347 {
1348 	int mode_idx;
1349 
1350 	if (size > strlen("passive") || size < strlen("active"))
1351 		return -EINVAL;
1352 
1353 	mode_idx = get_mode_idx_from_str(buf, size);
1354 
1355 	if (mode_idx < 0 || mode_idx >= AMD_PSTATE_MAX)
1356 		return -EINVAL;
1357 
1358 	if (mode_state_machine[cppc_state][mode_idx])
1359 		return mode_state_machine[cppc_state][mode_idx](mode_idx);
1360 
1361 	return 0;
1362 }
1363 EXPORT_SYMBOL_GPL(amd_pstate_update_status);
1364 
status_show(struct device * dev,struct device_attribute * attr,char * buf)1365 static ssize_t status_show(struct device *dev,
1366 			   struct device_attribute *attr, char *buf)
1367 {
1368 
1369 	guard(mutex)(&amd_pstate_driver_lock);
1370 
1371 	return amd_pstate_show_status(buf);
1372 }
1373 
status_store(struct device * a,struct device_attribute * b,const char * buf,size_t count)1374 static ssize_t status_store(struct device *a, struct device_attribute *b,
1375 			    const char *buf, size_t count)
1376 {
1377 	char *p = memchr(buf, '\n', count);
1378 	int ret;
1379 
1380 	guard(mutex)(&amd_pstate_driver_lock);
1381 	ret = amd_pstate_update_status(buf, p ? p - buf : count);
1382 
1383 	return ret < 0 ? ret : count;
1384 }
1385 
prefcore_show(struct device * dev,struct device_attribute * attr,char * buf)1386 static ssize_t prefcore_show(struct device *dev,
1387 			     struct device_attribute *attr, char *buf)
1388 {
1389 	return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
1390 }
1391 
1392 cpufreq_freq_attr_ro(amd_pstate_max_freq);
1393 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
1394 
1395 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
1396 cpufreq_freq_attr_ro(amd_pstate_prefcore_ranking);
1397 cpufreq_freq_attr_ro(amd_pstate_hw_prefcore);
1398 cpufreq_freq_attr_rw(energy_performance_preference);
1399 cpufreq_freq_attr_ro(energy_performance_available_preferences);
1400 static DEVICE_ATTR_RW(status);
1401 static DEVICE_ATTR_RO(prefcore);
1402 
1403 static struct freq_attr *amd_pstate_attr[] = {
1404 	&amd_pstate_max_freq,
1405 	&amd_pstate_lowest_nonlinear_freq,
1406 	&amd_pstate_highest_perf,
1407 	&amd_pstate_prefcore_ranking,
1408 	&amd_pstate_hw_prefcore,
1409 	NULL,
1410 };
1411 
1412 static struct freq_attr *amd_pstate_epp_attr[] = {
1413 	&amd_pstate_max_freq,
1414 	&amd_pstate_lowest_nonlinear_freq,
1415 	&amd_pstate_highest_perf,
1416 	&amd_pstate_prefcore_ranking,
1417 	&amd_pstate_hw_prefcore,
1418 	&energy_performance_preference,
1419 	&energy_performance_available_preferences,
1420 	NULL,
1421 };
1422 
1423 static struct attribute *pstate_global_attributes[] = {
1424 	&dev_attr_status.attr,
1425 	&dev_attr_prefcore.attr,
1426 	NULL
1427 };
1428 
1429 static const struct attribute_group amd_pstate_global_attr_group = {
1430 	.name = "amd_pstate",
1431 	.attrs = pstate_global_attributes,
1432 };
1433 
amd_pstate_acpi_pm_profile_server(void)1434 static bool amd_pstate_acpi_pm_profile_server(void)
1435 {
1436 	switch (acpi_gbl_FADT.preferred_profile) {
1437 	case PM_ENTERPRISE_SERVER:
1438 	case PM_SOHO_SERVER:
1439 	case PM_PERFORMANCE_SERVER:
1440 		return true;
1441 	}
1442 	return false;
1443 }
1444 
amd_pstate_acpi_pm_profile_undefined(void)1445 static bool amd_pstate_acpi_pm_profile_undefined(void)
1446 {
1447 	if (acpi_gbl_FADT.preferred_profile == PM_UNSPECIFIED)
1448 		return true;
1449 	if (acpi_gbl_FADT.preferred_profile >= NR_PM_PROFILES)
1450 		return true;
1451 	return false;
1452 }
1453 
amd_pstate_epp_cpu_init(struct cpufreq_policy * policy)1454 static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
1455 {
1456 	int min_freq, max_freq, ret;
1457 	struct amd_cpudata *cpudata;
1458 	struct device *dev;
1459 	u64 value;
1460 
1461 	/*
1462 	 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
1463 	 * which is ideal for initialization process.
1464 	 */
1465 	amd_perf_ctl_reset(policy->cpu);
1466 	dev = get_cpu_device(policy->cpu);
1467 	if (!dev)
1468 		return -ENODEV;
1469 
1470 	cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
1471 	if (!cpudata)
1472 		return -ENOMEM;
1473 
1474 	cpudata->cpu = policy->cpu;
1475 
1476 	ret = amd_pstate_init_perf(cpudata);
1477 	if (ret)
1478 		goto free_cpudata1;
1479 
1480 	amd_pstate_init_prefcore(cpudata);
1481 
1482 	ret = amd_pstate_init_freq(cpudata);
1483 	if (ret)
1484 		goto free_cpudata1;
1485 
1486 	ret = amd_pstate_init_boost_support(cpudata);
1487 	if (ret)
1488 		goto free_cpudata1;
1489 
1490 	min_freq = READ_ONCE(cpudata->min_freq);
1491 	max_freq = READ_ONCE(cpudata->max_freq);
1492 
1493 	policy->cpuinfo.min_freq = min_freq;
1494 	policy->cpuinfo.max_freq = max_freq;
1495 	/* It will be updated by governor */
1496 	policy->cur = policy->cpuinfo.min_freq;
1497 
1498 	policy->driver_data = cpudata;
1499 
1500 	policy->min = policy->cpuinfo.min_freq;
1501 	policy->max = policy->cpuinfo.max_freq;
1502 
1503 	policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
1504 
1505 	/*
1506 	 * Set the policy to provide a valid fallback value in case
1507 	 * the default cpufreq governor is neither powersave nor performance.
1508 	 */
1509 	if (amd_pstate_acpi_pm_profile_server() ||
1510 	    amd_pstate_acpi_pm_profile_undefined()) {
1511 		policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1512 		cpudata->epp_default = amd_pstate_get_epp(cpudata);
1513 	} else {
1514 		policy->policy = CPUFREQ_POLICY_POWERSAVE;
1515 		cpudata->epp_default = AMD_CPPC_EPP_BALANCE_PERFORMANCE;
1516 	}
1517 
1518 	if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1519 		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
1520 		if (ret)
1521 			return ret;
1522 		WRITE_ONCE(cpudata->cppc_req_cached, value);
1523 
1524 		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
1525 		if (ret)
1526 			return ret;
1527 		WRITE_ONCE(cpudata->cppc_cap1_cached, value);
1528 	}
1529 	ret = amd_pstate_set_epp(cpudata, cpudata->epp_default);
1530 	if (ret)
1531 		return ret;
1532 
1533 	current_pstate_driver->adjust_perf = NULL;
1534 
1535 	return 0;
1536 
1537 free_cpudata1:
1538 	kfree(cpudata);
1539 	return ret;
1540 }
1541 
amd_pstate_epp_cpu_exit(struct cpufreq_policy * policy)1542 static void amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
1543 {
1544 	struct amd_cpudata *cpudata = policy->driver_data;
1545 
1546 	if (cpudata) {
1547 		kfree(cpudata);
1548 		policy->driver_data = NULL;
1549 	}
1550 
1551 	pr_debug("CPU %d exiting\n", policy->cpu);
1552 }
1553 
amd_pstate_epp_update_limit(struct cpufreq_policy * policy)1554 static int amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
1555 {
1556 	struct amd_cpudata *cpudata = policy->driver_data;
1557 	u8 epp;
1558 
1559 	amd_pstate_update_min_max_limit(policy);
1560 
1561 	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1562 		epp = 0;
1563 	else
1564 		epp = READ_ONCE(cpudata->epp_cached);
1565 
1566 	if (trace_amd_pstate_epp_perf_enabled()) {
1567 		trace_amd_pstate_epp_perf(cpudata->cpu, cpudata->highest_perf, epp,
1568 					  cpudata->min_limit_perf,
1569 					  cpudata->max_limit_perf,
1570 					  policy->boost_enabled);
1571 	}
1572 
1573 	return amd_pstate_update_perf(cpudata, cpudata->min_limit_perf, 0U,
1574 				      cpudata->max_limit_perf, epp, false);
1575 }
1576 
amd_pstate_epp_set_policy(struct cpufreq_policy * policy)1577 static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
1578 {
1579 	struct amd_cpudata *cpudata = policy->driver_data;
1580 	int ret;
1581 
1582 	if (!policy->cpuinfo.max_freq)
1583 		return -ENODEV;
1584 
1585 	pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
1586 				policy->cpuinfo.max_freq, policy->max);
1587 
1588 	cpudata->policy = policy->policy;
1589 
1590 	ret = amd_pstate_epp_update_limit(policy);
1591 	if (ret)
1592 		return ret;
1593 
1594 	/*
1595 	 * policy->cur is never updated with the amd_pstate_epp driver, but it
1596 	 * is used as a stale frequency value. So, keep it within limits.
1597 	 */
1598 	policy->cur = policy->min;
1599 
1600 	return 0;
1601 }
1602 
amd_pstate_epp_reenable(struct cpufreq_policy * policy)1603 static int amd_pstate_epp_reenable(struct cpufreq_policy *policy)
1604 {
1605 	struct amd_cpudata *cpudata = policy->driver_data;
1606 	u8 max_perf;
1607 	int ret;
1608 
1609 	ret = amd_pstate_cppc_enable(true);
1610 	if (ret)
1611 		pr_err("failed to enable amd pstate during resume, return %d\n", ret);
1612 
1613 	max_perf = READ_ONCE(cpudata->highest_perf);
1614 
1615 	if (trace_amd_pstate_epp_perf_enabled()) {
1616 		trace_amd_pstate_epp_perf(cpudata->cpu, cpudata->highest_perf,
1617 					  cpudata->epp_cached,
1618 					  FIELD_GET(AMD_CPPC_MIN_PERF_MASK, cpudata->cppc_req_cached),
1619 					  max_perf, policy->boost_enabled);
1620 	}
1621 
1622 	return amd_pstate_epp_update_limit(policy);
1623 }
1624 
amd_pstate_epp_cpu_online(struct cpufreq_policy * policy)1625 static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
1626 {
1627 	struct amd_cpudata *cpudata = policy->driver_data;
1628 	int ret;
1629 
1630 	pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
1631 
1632 	ret = amd_pstate_epp_reenable(policy);
1633 	if (ret)
1634 		return ret;
1635 	cpudata->suspended = false;
1636 
1637 	return 0;
1638 }
1639 
amd_pstate_epp_cpu_offline(struct cpufreq_policy * policy)1640 static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
1641 {
1642 	struct amd_cpudata *cpudata = policy->driver_data;
1643 	u8 min_perf;
1644 
1645 	if (cpudata->suspended)
1646 		return 0;
1647 
1648 	min_perf = READ_ONCE(cpudata->lowest_perf);
1649 
1650 	guard(mutex)(&amd_pstate_limits_lock);
1651 
1652 	if (trace_amd_pstate_epp_perf_enabled()) {
1653 		trace_amd_pstate_epp_perf(cpudata->cpu, cpudata->highest_perf,
1654 					  AMD_CPPC_EPP_BALANCE_POWERSAVE,
1655 					  min_perf, min_perf, policy->boost_enabled);
1656 	}
1657 
1658 	return amd_pstate_update_perf(cpudata, min_perf, 0, min_perf,
1659 				      AMD_CPPC_EPP_BALANCE_POWERSAVE, false);
1660 }
1661 
amd_pstate_epp_suspend(struct cpufreq_policy * policy)1662 static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
1663 {
1664 	struct amd_cpudata *cpudata = policy->driver_data;
1665 	int ret;
1666 
1667 	/* avoid suspending when EPP is not enabled */
1668 	if (cppc_state != AMD_PSTATE_ACTIVE)
1669 		return 0;
1670 
1671 	/* invalidate to ensure it's rewritten during resume */
1672 	cpudata->cppc_req_cached = 0;
1673 
1674 	/* set this flag to avoid setting core offline*/
1675 	cpudata->suspended = true;
1676 
1677 	/* disable CPPC in lowlevel firmware */
1678 	ret = amd_pstate_cppc_enable(false);
1679 	if (ret)
1680 		pr_err("failed to suspend, return %d\n", ret);
1681 
1682 	return 0;
1683 }
1684 
amd_pstate_epp_resume(struct cpufreq_policy * policy)1685 static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
1686 {
1687 	struct amd_cpudata *cpudata = policy->driver_data;
1688 
1689 	if (cpudata->suspended) {
1690 		guard(mutex)(&amd_pstate_limits_lock);
1691 
1692 		/* enable amd pstate from suspend state*/
1693 		amd_pstate_epp_reenable(policy);
1694 
1695 		cpudata->suspended = false;
1696 	}
1697 
1698 	return 0;
1699 }
1700 
1701 static struct cpufreq_driver amd_pstate_driver = {
1702 	.flags		= CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
1703 	.verify		= amd_pstate_verify,
1704 	.target		= amd_pstate_target,
1705 	.fast_switch    = amd_pstate_fast_switch,
1706 	.init		= amd_pstate_cpu_init,
1707 	.exit		= amd_pstate_cpu_exit,
1708 	.suspend	= amd_pstate_cpu_suspend,
1709 	.resume		= amd_pstate_cpu_resume,
1710 	.set_boost	= amd_pstate_set_boost,
1711 	.update_limits	= amd_pstate_update_limits,
1712 	.name		= "amd-pstate",
1713 	.attr		= amd_pstate_attr,
1714 };
1715 
1716 static struct cpufreq_driver amd_pstate_epp_driver = {
1717 	.flags		= CPUFREQ_CONST_LOOPS,
1718 	.verify		= amd_pstate_verify,
1719 	.setpolicy	= amd_pstate_epp_set_policy,
1720 	.init		= amd_pstate_epp_cpu_init,
1721 	.exit		= amd_pstate_epp_cpu_exit,
1722 	.offline	= amd_pstate_epp_cpu_offline,
1723 	.online		= amd_pstate_epp_cpu_online,
1724 	.suspend	= amd_pstate_epp_suspend,
1725 	.resume		= amd_pstate_epp_resume,
1726 	.update_limits	= amd_pstate_update_limits,
1727 	.set_boost	= amd_pstate_set_boost,
1728 	.name		= "amd-pstate-epp",
1729 	.attr		= amd_pstate_epp_attr,
1730 };
1731 
1732 /*
1733  * CPPC function is not supported for family ID 17H with model_ID ranging from 0x10 to 0x2F.
1734  * show the debug message that helps to check if the CPU has CPPC support for loading issue.
1735  */
amd_cppc_supported(void)1736 static bool amd_cppc_supported(void)
1737 {
1738 	struct cpuinfo_x86 *c = &cpu_data(0);
1739 	bool warn = false;
1740 
1741 	if ((boot_cpu_data.x86 == 0x17) && (boot_cpu_data.x86_model < 0x30)) {
1742 		pr_debug_once("CPPC feature is not supported by the processor\n");
1743 		return false;
1744 	}
1745 
1746 	/*
1747 	 * If the CPPC feature is disabled in the BIOS for processors
1748 	 * that support MSR-based CPPC, the AMD Pstate driver may not
1749 	 * function correctly.
1750 	 *
1751 	 * For such processors, check the CPPC flag and display a
1752 	 * warning message if the platform supports CPPC.
1753 	 *
1754 	 * Note: The code check below will not abort the driver
1755 	 * registration process because of the code is added for
1756 	 * debugging purposes. Besides, it may still be possible for
1757 	 * the driver to work using the shared-memory mechanism.
1758 	 */
1759 	if (!cpu_feature_enabled(X86_FEATURE_CPPC)) {
1760 		if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
1761 			switch (c->x86_model) {
1762 			case 0x60 ... 0x6F:
1763 			case 0x80 ... 0xAF:
1764 				warn = true;
1765 				break;
1766 			}
1767 		} else if (cpu_feature_enabled(X86_FEATURE_ZEN3) ||
1768 			   cpu_feature_enabled(X86_FEATURE_ZEN4)) {
1769 			switch (c->x86_model) {
1770 			case 0x10 ... 0x1F:
1771 			case 0x40 ... 0xAF:
1772 				warn = true;
1773 				break;
1774 			}
1775 		} else if (cpu_feature_enabled(X86_FEATURE_ZEN5)) {
1776 			warn = true;
1777 		}
1778 	}
1779 
1780 	if (warn)
1781 		pr_warn_once("The CPPC feature is supported but currently disabled by the BIOS.\n"
1782 					"Please enable it if your BIOS has the CPPC option.\n");
1783 	return true;
1784 }
1785 
amd_pstate_init(void)1786 static int __init amd_pstate_init(void)
1787 {
1788 	struct device *dev_root;
1789 	int ret;
1790 
1791 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
1792 		return -ENODEV;
1793 
1794 	/* show debug message only if CPPC is not supported */
1795 	if (!amd_cppc_supported())
1796 		return -EOPNOTSUPP;
1797 
1798 	/* show warning message when BIOS broken or ACPI disabled */
1799 	if (!acpi_cpc_valid()) {
1800 		pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
1801 		return -ENODEV;
1802 	}
1803 
1804 	/* don't keep reloading if cpufreq_driver exists */
1805 	if (cpufreq_get_current_driver())
1806 		return -EEXIST;
1807 
1808 	quirks = NULL;
1809 
1810 	/* check if this machine need CPPC quirks */
1811 	dmi_check_system(amd_pstate_quirks_table);
1812 
1813 	/*
1814 	* determine the driver mode from the command line or kernel config.
1815 	* If no command line input is provided, cppc_state will be AMD_PSTATE_UNDEFINED.
1816 	* command line options will override the kernel config settings.
1817 	*/
1818 
1819 	if (cppc_state == AMD_PSTATE_UNDEFINED) {
1820 		/* Disable on the following configs by default:
1821 		 * 1. Undefined platforms
1822 		 * 2. Server platforms with CPUs older than Family 0x1A.
1823 		 */
1824 		if (amd_pstate_acpi_pm_profile_undefined() ||
1825 		    (amd_pstate_acpi_pm_profile_server() && boot_cpu_data.x86 < 0x1A)) {
1826 			pr_info("driver load is disabled, boot with specific mode to enable this\n");
1827 			return -ENODEV;
1828 		}
1829 		/* get driver mode from kernel config option [1:4] */
1830 		cppc_state = CONFIG_X86_AMD_PSTATE_DEFAULT_MODE;
1831 	}
1832 
1833 	if (cppc_state == AMD_PSTATE_DISABLE) {
1834 		pr_info("driver load is disabled, boot with specific mode to enable this\n");
1835 		return -ENODEV;
1836 	}
1837 
1838 	/* capability check */
1839 	if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1840 		pr_debug("AMD CPPC MSR based functionality is supported\n");
1841 	} else {
1842 		pr_debug("AMD CPPC shared memory based functionality is supported\n");
1843 		static_call_update(amd_pstate_cppc_enable, shmem_cppc_enable);
1844 		static_call_update(amd_pstate_init_perf, shmem_init_perf);
1845 		static_call_update(amd_pstate_update_perf, shmem_update_perf);
1846 		static_call_update(amd_pstate_get_epp, shmem_get_epp);
1847 		static_call_update(amd_pstate_set_epp, shmem_set_epp);
1848 	}
1849 
1850 	if (amd_pstate_prefcore) {
1851 		ret = amd_detect_prefcore(&amd_pstate_prefcore);
1852 		if (ret)
1853 			return ret;
1854 	}
1855 
1856 	ret = amd_pstate_register_driver(cppc_state);
1857 	if (ret) {
1858 		pr_err("failed to register with return %d\n", ret);
1859 		return ret;
1860 	}
1861 
1862 	dev_root = bus_get_dev_root(&cpu_subsys);
1863 	if (dev_root) {
1864 		ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
1865 		put_device(dev_root);
1866 		if (ret) {
1867 			pr_err("sysfs attribute export failed with error %d.\n", ret);
1868 			goto global_attr_free;
1869 		}
1870 	}
1871 
1872 	return ret;
1873 
1874 global_attr_free:
1875 	cpufreq_unregister_driver(current_pstate_driver);
1876 	amd_pstate_cppc_enable(false);
1877 	return ret;
1878 }
1879 device_initcall(amd_pstate_init);
1880 
amd_pstate_param(char * str)1881 static int __init amd_pstate_param(char *str)
1882 {
1883 	size_t size;
1884 	int mode_idx;
1885 
1886 	if (!str)
1887 		return -EINVAL;
1888 
1889 	size = strlen(str);
1890 	mode_idx = get_mode_idx_from_str(str, size);
1891 
1892 	return amd_pstate_set_driver(mode_idx);
1893 }
1894 
amd_prefcore_param(char * str)1895 static int __init amd_prefcore_param(char *str)
1896 {
1897 	if (!strcmp(str, "disable"))
1898 		amd_pstate_prefcore = false;
1899 
1900 	return 0;
1901 }
1902 
1903 early_param("amd_pstate", amd_pstate_param);
1904 early_param("amd_prefcore", amd_prefcore_param);
1905 
1906 MODULE_AUTHOR("Huang Rui <[email protected]>");
1907 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
1908