xref: /openbmc/linux/arch/x86/kernel/acpi/cppc.c (revision 7fc96d71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * cppc.c: CPPC Interface for x86
4  * Copyright (c) 2016, Intel Corporation.
5  */
6 
7 #include <acpi/cppc_acpi.h>
8 #include <asm/msr.h>
9 #include <asm/processor.h>
10 #include <asm/topology.h>
11 
12 /* Refer to drivers/acpi/cppc_acpi.c for the description of functions */
13 
14 bool cpc_ffh_supported(void)
15 {
16 	return true;
17 }
18 
19 int cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
20 {
21 	int err;
22 
23 	err = rdmsrl_safe_on_cpu(cpunum, reg->address, val);
24 	if (!err) {
25 		u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
26 				       reg->bit_offset);
27 
28 		*val &= mask;
29 		*val >>= reg->bit_offset;
30 	}
31 	return err;
32 }
33 
34 int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
35 {
36 	u64 rd_val;
37 	int err;
38 
39 	err = rdmsrl_safe_on_cpu(cpunum, reg->address, &rd_val);
40 	if (!err) {
41 		u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
42 				       reg->bit_offset);
43 
44 		val <<= reg->bit_offset;
45 		val &= mask;
46 		rd_val &= ~mask;
47 		rd_val |= val;
48 		err = wrmsrl_safe_on_cpu(cpunum, reg->address, rd_val);
49 	}
50 	return err;
51 }
52 
53 static void amd_set_max_freq_ratio(void)
54 {
55 	struct cppc_perf_caps perf_caps;
56 	u64 highest_perf, nominal_perf;
57 	u64 perf_ratio;
58 	int rc;
59 
60 	rc = cppc_get_perf_caps(0, &perf_caps);
61 	if (rc) {
62 		pr_debug("Could not retrieve perf counters (%d)\n", rc);
63 		return;
64 	}
65 
66 	highest_perf = amd_get_highest_perf();
67 	nominal_perf = perf_caps.nominal_perf;
68 
69 	if (!highest_perf || !nominal_perf) {
70 		pr_debug("Could not retrieve highest or nominal performance\n");
71 		return;
72 	}
73 
74 	perf_ratio = div_u64(highest_perf * SCHED_CAPACITY_SCALE, nominal_perf);
75 	/* midpoint between max_boost and max_P */
76 	perf_ratio = (perf_ratio + SCHED_CAPACITY_SCALE) >> 1;
77 	if (!perf_ratio) {
78 		pr_debug("Non-zero highest/nominal perf values led to a 0 ratio\n");
79 		return;
80 	}
81 
82 	freq_invariance_set_perf_ratio(perf_ratio, false);
83 }
84 
85 static DEFINE_MUTEX(freq_invariance_lock);
86 
87 void init_freq_invariance_cppc(void)
88 {
89 	static bool init_done;
90 
91 	if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
92 		return;
93 
94 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
95 		return;
96 
97 	mutex_lock(&freq_invariance_lock);
98 	if (!init_done)
99 		amd_set_max_freq_ratio();
100 	init_done = true;
101 	mutex_unlock(&freq_invariance_lock);
102 }
103