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 #define CPPC_HIGHEST_PERF_PERFORMANCE 196
13 #define CPPC_HIGHEST_PERF_PREFCORE 166
16 AMD_PREF_CORE_UNKNOWN = 0,
17 AMD_PREF_CORE_SUPPORTED,
18 AMD_PREF_CORE_UNSUPPORTED,
20 static enum amd_pref_core amd_pref_core_detected;
21 static u64 boost_numerator;
23 /* Refer to drivers/acpi/cppc_acpi.c for the description of functions */
25 bool cpc_supported_by_cpu(void)
27 switch (boot_cpu_data.x86_vendor) {
29 case X86_VENDOR_HYGON:
30 if (boot_cpu_data.x86 == 0x19 && ((boot_cpu_data.x86_model <= 0x0f) ||
31 (boot_cpu_data.x86_model >= 0x20 && boot_cpu_data.x86_model <= 0x2f)))
33 else if (boot_cpu_data.x86 == 0x17 &&
34 boot_cpu_data.x86_model >= 0x30 && boot_cpu_data.x86_model <= 0x7f)
36 return boot_cpu_has(X86_FEATURE_CPPC);
41 bool cpc_ffh_supported(void)
46 int cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
50 err = rdmsrl_safe_on_cpu(cpunum, reg->address, val);
52 u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
56 *val >>= reg->bit_offset;
61 int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
66 err = rdmsrl_safe_on_cpu(cpunum, reg->address, &rd_val);
68 u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
71 val <<= reg->bit_offset;
75 err = wrmsrl_safe_on_cpu(cpunum, reg->address, rd_val);
80 static void amd_set_max_freq_ratio(void)
82 struct cppc_perf_caps perf_caps;
83 u64 numerator, nominal_perf;
87 rc = cppc_get_perf_caps(0, &perf_caps);
89 pr_warn("Could not retrieve perf counters (%d)\n", rc);
93 rc = amd_get_boost_ratio_numerator(0, &numerator);
95 pr_warn("Could not retrieve highest performance (%d)\n", rc);
98 nominal_perf = perf_caps.nominal_perf;
101 pr_warn("Could not retrieve nominal performance\n");
105 /* midpoint between max_boost and max_P */
106 perf_ratio = (div_u64(numerator * SCHED_CAPACITY_SCALE, nominal_perf) + SCHED_CAPACITY_SCALE) >> 1;
108 freq_invariance_set_perf_ratio(perf_ratio, false);
111 static DEFINE_MUTEX(freq_invariance_lock);
113 static inline void init_freq_invariance_cppc(void)
115 static bool init_done;
117 if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
120 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
123 mutex_lock(&freq_invariance_lock);
125 amd_set_max_freq_ratio();
127 mutex_unlock(&freq_invariance_lock);
130 void acpi_processor_init_invariance_cppc(void)
132 init_freq_invariance_cppc();
136 * Get the highest performance register value.
137 * @cpu: CPU from which to get highest performance.
138 * @highest_perf: Return address for highest performance value.
140 * Return: 0 for success, negative error code otherwise.
142 int amd_get_highest_perf(unsigned int cpu, u32 *highest_perf)
147 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
148 ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, &val);
152 val = AMD_CPPC_HIGHEST_PERF(val);
154 ret = cppc_get_highest_perf(cpu, &val);
159 WRITE_ONCE(*highest_perf, (u32)val);
163 EXPORT_SYMBOL_GPL(amd_get_highest_perf);
166 * amd_detect_prefcore: Detect if CPUs in the system support preferred cores
167 * @detected: Output variable for the result of the detection.
169 * Determine whether CPUs in the system support preferred cores. On systems
170 * that support preferred cores, different highest perf values will be found
171 * on different cores. On other systems, the highest perf value will be the
174 * The result of the detection will be stored in the 'detected' parameter.
176 * Return: 0 for success, negative error code otherwise
178 int amd_detect_prefcore(bool *detected)
181 u64 highest_perf[2] = {0};
183 if (WARN_ON(!detected))
186 switch (amd_pref_core_detected) {
187 case AMD_PREF_CORE_SUPPORTED:
190 case AMD_PREF_CORE_UNSUPPORTED:
197 for_each_present_cpu(cpu) {
201 ret = amd_get_highest_perf(cpu, &tmp);
205 if (!count || (count == 1 && tmp != highest_perf[0]))
206 highest_perf[count++] = tmp;
212 *detected = (count == 2);
213 boost_numerator = highest_perf[0];
215 amd_pref_core_detected = *detected ? AMD_PREF_CORE_SUPPORTED :
216 AMD_PREF_CORE_UNSUPPORTED;
218 pr_debug("AMD CPPC preferred core is %ssupported (highest perf: 0x%llx)\n",
219 *detected ? "" : "un", highest_perf[0]);
223 EXPORT_SYMBOL_GPL(amd_detect_prefcore);
226 * amd_get_boost_ratio_numerator: Get the numerator to use for boost ratio calculation
227 * @cpu: CPU to get numerator for.
228 * @numerator: Output variable for numerator.
230 * Determine the numerator to use for calculating the boost ratio on
231 * a CPU. On systems that support preferred cores, this will be a hardcoded
232 * value. On other systems this will the highest performance register value.
234 * If booting the system with amd-pstate enabled but preferred cores disabled then
235 * the correct boost numerator will be returned to match hardware capabilities
236 * even if the preferred cores scheduling hints are not enabled.
238 * Return: 0 for success, negative error code otherwise.
240 int amd_get_boost_ratio_numerator(unsigned int cpu, u64 *numerator)
242 enum x86_topology_cpu_type core_type = get_topology_cpu_type(&cpu_data(cpu));
247 ret = amd_detect_prefcore(&prefcore);
251 /* without preferred cores, return the highest perf register value */
253 *numerator = boost_numerator;
258 * For AMD CPUs with Family ID 19H and Model ID range 0x70 to 0x7f,
259 * the highest performance level is set to 196.
260 * https://bugzilla.kernel.org/show_bug.cgi?id=218759
262 if (cpu_feature_enabled(X86_FEATURE_ZEN4)) {
263 switch (boot_cpu_data.x86_model) {
265 *numerator = CPPC_HIGHEST_PERF_PERFORMANCE;
272 /* detect if running on heterogeneous design */
273 if (cpu_feature_enabled(X86_FEATURE_AMD_HETEROGENEOUS_CORES)) {
275 case TOPO_CPU_TYPE_UNKNOWN:
276 pr_warn("Undefined core type found for cpu %d\n", cpu);
278 case TOPO_CPU_TYPE_PERFORMANCE:
279 /* use the max scale for performance cores */
280 *numerator = CPPC_HIGHEST_PERF_PERFORMANCE;
282 case TOPO_CPU_TYPE_EFFICIENCY:
283 /* use the highest perf value for efficiency cores */
284 ret = amd_get_highest_perf(cpu, &tmp);
292 *numerator = CPPC_HIGHEST_PERF_PREFCORE;
296 EXPORT_SYMBOL_GPL(amd_get_boost_ratio_numerator);