xref: /openbmc/linux/arch/x86/kernel/acpi/cppc.c (revision fbb6b31a)
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 bool amd_set_max_freq_ratio(u64 *ratio)
54 {
55 	struct cppc_perf_caps perf_caps;
56 	u64 highest_perf, nominal_perf;
57 	u64 perf_ratio;
58 	int rc;
59 
60 	if (!ratio)
61 		return false;
62 
63 	rc = cppc_get_perf_caps(0, &perf_caps);
64 	if (rc) {
65 		pr_debug("Could not retrieve perf counters (%d)\n", rc);
66 		return false;
67 	}
68 
69 	highest_perf = amd_get_highest_perf();
70 	nominal_perf = perf_caps.nominal_perf;
71 
72 	if (!highest_perf || !nominal_perf) {
73 		pr_debug("Could not retrieve highest or nominal performance\n");
74 		return false;
75 	}
76 
77 	perf_ratio = div_u64(highest_perf * SCHED_CAPACITY_SCALE, nominal_perf);
78 	/* midpoint between max_boost and max_P */
79 	perf_ratio = (perf_ratio + SCHED_CAPACITY_SCALE) >> 1;
80 	if (!perf_ratio) {
81 		pr_debug("Non-zero highest/nominal perf values led to a 0 ratio\n");
82 		return false;
83 	}
84 
85 	*ratio = perf_ratio;
86 	arch_set_max_freq_ratio(false);
87 
88 	return true;
89 }
90 
91 static DEFINE_MUTEX(freq_invariance_lock);
92 
93 void init_freq_invariance_cppc(void)
94 {
95 	static bool secondary;
96 
97 	mutex_lock(&freq_invariance_lock);
98 
99 	init_freq_invariance(secondary, true);
100 	secondary = true;
101 
102 	mutex_unlock(&freq_invariance_lock);
103 }
104