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 if (boot_cpu_data.x86 == 0x19 && ((boot_cpu_data.x86_model <= 0x0f) ||
20 (boot_cpu_data.x86_model >= 0x20 && boot_cpu_data.x86_model <= 0x2f)))
22 else if (boot_cpu_data.x86 == 0x17 &&
23 boot_cpu_data.x86_model >= 0x70 && boot_cpu_data.x86_model <= 0x7f)
25 return boot_cpu_has(X86_FEATURE_CPPC);
30 bool cpc_ffh_supported(void)
35 int cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
39 err = rdmsrl_safe_on_cpu(cpunum, reg->address, val);
41 u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
45 *val >>= reg->bit_offset;
50 int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
55 err = rdmsrl_safe_on_cpu(cpunum, reg->address, &rd_val);
57 u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
60 val <<= reg->bit_offset;
64 err = wrmsrl_safe_on_cpu(cpunum, reg->address, rd_val);
69 static void amd_set_max_freq_ratio(void)
71 struct cppc_perf_caps perf_caps;
72 u64 highest_perf, nominal_perf;
76 rc = cppc_get_perf_caps(0, &perf_caps);
78 pr_debug("Could not retrieve perf counters (%d)\n", rc);
82 highest_perf = amd_get_highest_perf();
83 nominal_perf = perf_caps.nominal_perf;
85 if (!highest_perf || !nominal_perf) {
86 pr_debug("Could not retrieve highest or nominal performance\n");
90 perf_ratio = div_u64(highest_perf * SCHED_CAPACITY_SCALE, nominal_perf);
91 /* midpoint between max_boost and max_P */
92 perf_ratio = (perf_ratio + SCHED_CAPACITY_SCALE) >> 1;
94 pr_debug("Non-zero highest/nominal perf values led to a 0 ratio\n");
98 freq_invariance_set_perf_ratio(perf_ratio, false);
101 static DEFINE_MUTEX(freq_invariance_lock);
103 void init_freq_invariance_cppc(void)
105 static bool init_done;
107 if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
110 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
113 mutex_lock(&freq_invariance_lock);
115 amd_set_max_freq_ratio();
117 mutex_unlock(&freq_invariance_lock);