1 // SPDX-License-Identifier: GPL-2.0-only
3 * cppc.c: CPPC Interface for x86
4 * Copyright (c) 2016, Intel Corporation.
7 #include <acpi/cppc_acpi.h>
9 #include <asm/processor.h>
10 #include <asm/topology.h>
12 /* Refer to drivers/acpi/cppc_acpi.c for the description of functions */
14 bool cpc_supported_by_cpu(void)
16 switch (boot_cpu_data.x86_vendor) {
18 case X86_VENDOR_HYGON:
19 return boot_cpu_has(X86_FEATURE_CPPC);
24 bool cpc_ffh_supported(void)
29 int cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
33 err = rdmsrl_safe_on_cpu(cpunum, reg->address, val);
35 u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
39 *val >>= reg->bit_offset;
44 int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
49 err = rdmsrl_safe_on_cpu(cpunum, reg->address, &rd_val);
51 u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
54 val <<= reg->bit_offset;
58 err = wrmsrl_safe_on_cpu(cpunum, reg->address, rd_val);
63 static void amd_set_max_freq_ratio(void)
65 struct cppc_perf_caps perf_caps;
66 u64 highest_perf, nominal_perf;
70 rc = cppc_get_perf_caps(0, &perf_caps);
72 pr_debug("Could not retrieve perf counters (%d)\n", rc);
76 highest_perf = amd_get_highest_perf();
77 nominal_perf = perf_caps.nominal_perf;
79 if (!highest_perf || !nominal_perf) {
80 pr_debug("Could not retrieve highest or nominal performance\n");
84 perf_ratio = div_u64(highest_perf * SCHED_CAPACITY_SCALE, nominal_perf);
85 /* midpoint between max_boost and max_P */
86 perf_ratio = (perf_ratio + SCHED_CAPACITY_SCALE) >> 1;
88 pr_debug("Non-zero highest/nominal perf values led to a 0 ratio\n");
92 freq_invariance_set_perf_ratio(perf_ratio, false);
95 static DEFINE_MUTEX(freq_invariance_lock);
97 void init_freq_invariance_cppc(void)
99 static bool init_done;
101 if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
104 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
107 mutex_lock(&freq_invariance_lock);
109 amd_set_max_freq_ratio();
111 mutex_unlock(&freq_invariance_lock);