1 // SPDX-License-Identifier: GPL-2.0-only
3 * CPPC (Collaborative Processor Performance Control) driver for
4 * interfacing with the CPUfreq layer and governors. See
5 * cppc_acpi.c for CPPC specific methods.
7 * (C) Copyright 2014, 2015 Linaro Ltd.
8 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
11 #define pr_fmt(fmt) "CPPC Cpufreq:" fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/cpu.h>
17 #include <linux/cpufreq.h>
18 #include <linux/dmi.h>
19 #include <linux/time.h>
20 #include <linux/vmalloc.h>
22 #include <asm/unaligned.h>
24 #include <acpi/cppc_acpi.h>
26 /* Minimum struct length needed for the DMI processor entry we want */
27 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
29 /* Offset in the DMI processor structure for the max frequency */
30 #define DMI_PROCESSOR_MAX_SPEED 0x14
33 * These structs contain information parsed from per CPU
34 * ACPI _CPC structures.
35 * e.g. For each CPU the highest, lowest supported
36 * performance capabilities, desired performance level
39 static struct cppc_cpudata **all_cpu_data;
40 static bool boost_supported;
42 struct cppc_workaround_oem_info {
43 char oem_id[ACPI_OEM_ID_SIZE + 1];
44 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
48 static struct cppc_workaround_oem_info wa_info[] = {
51 .oem_table_id = "HIP07 ",
55 .oem_table_id = "HIP08 ",
60 /* Callback function used to retrieve the max frequency from DMI */
61 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
63 const u8 *dmi_data = (const u8 *)dm;
64 u16 *mhz = (u16 *)private;
66 if (dm->type == DMI_ENTRY_PROCESSOR &&
67 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
68 u16 val = (u16)get_unaligned((const u16 *)
69 (dmi_data + DMI_PROCESSOR_MAX_SPEED));
70 *mhz = val > *mhz ? val : *mhz;
74 /* Look up the max frequency in DMI */
75 static u64 cppc_get_dmi_max_khz(void)
79 dmi_walk(cppc_find_dmi_mhz, &mhz);
82 * Real stupid fallback value, just in case there is no
91 * If CPPC lowest_freq and nominal_freq registers are exposed then we can
92 * use them to convert perf to freq and vice versa
94 * If the perf/freq point lies between Nominal and Lowest, we can treat
95 * (Low perf, Low freq) and (Nom Perf, Nom freq) as 2D co-ordinates of a line
96 * and extrapolate the rest
97 * For perf/freq > Nominal, we use the ratio perf:freq at Nominal for conversion
99 static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data,
102 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
106 if (caps->lowest_freq && caps->nominal_freq) {
107 if (perf >= caps->nominal_perf) {
108 mul = caps->nominal_freq;
109 div = caps->nominal_perf;
111 mul = caps->nominal_freq - caps->lowest_freq;
112 div = caps->nominal_perf - caps->lowest_perf;
116 max_khz = cppc_get_dmi_max_khz();
118 div = caps->highest_perf;
120 return (u64)perf * mul / div;
123 static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data,
126 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
130 if (caps->lowest_freq && caps->nominal_freq) {
131 if (freq >= caps->nominal_freq) {
132 mul = caps->nominal_perf;
133 div = caps->nominal_freq;
135 mul = caps->lowest_perf;
136 div = caps->lowest_freq;
140 max_khz = cppc_get_dmi_max_khz();
141 mul = caps->highest_perf;
145 return (u64)freq * mul / div;
148 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
149 unsigned int target_freq,
150 unsigned int relation)
152 struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
153 struct cpufreq_freqs freqs;
157 desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
158 /* Return if it is exactly the same perf */
159 if (desired_perf == cpu_data->perf_ctrls.desired_perf)
162 cpu_data->perf_ctrls.desired_perf = desired_perf;
163 freqs.old = policy->cur;
164 freqs.new = target_freq;
166 cpufreq_freq_transition_begin(policy, &freqs);
167 ret = cppc_set_perf(cpu_data->cpu, &cpu_data->perf_ctrls);
168 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
171 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
177 static int cppc_verify_policy(struct cpufreq_policy_data *policy)
179 cpufreq_verify_within_cpu_limits(policy);
183 static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
185 struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
186 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
187 unsigned int cpu = policy->cpu;
190 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
192 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
194 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
195 caps->lowest_perf, cpu, ret);
199 * The PCC subspace describes the rate at which platform can accept commands
200 * on the shared PCC channel (including READs which do not count towards freq
201 * transition requests), so ideally we need to use the PCC values as a fallback
202 * if we don't have a platform specific transition_delay_us
205 #include <asm/cputype.h>
207 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
209 unsigned long implementor = read_cpuid_implementor();
210 unsigned long part_num = read_cpuid_part_number();
211 unsigned int delay_us = 0;
213 switch (implementor) {
214 case ARM_CPU_IMP_QCOM:
216 case QCOM_CPU_PART_FALKOR_V1:
217 case QCOM_CPU_PART_FALKOR:
221 delay_us = cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
226 delay_us = cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
235 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
237 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
241 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
243 struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
244 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
245 unsigned int cpu = policy->cpu;
249 ret = cppc_get_perf_caps(cpu, caps);
252 pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
257 /* Convert the lowest and nominal freq from MHz to KHz */
258 caps->lowest_freq *= 1000;
259 caps->nominal_freq *= 1000;
262 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
263 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
265 policy->min = cppc_cpufreq_perf_to_khz(cpu_data,
266 caps->lowest_nonlinear_perf);
267 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
271 * Set cpuinfo.min_freq to Lowest to make the full range of performance
272 * available if userspace wants to use any perf between lowest & lowest
275 policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu_data,
277 policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu_data,
280 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
281 policy->shared_type = cpu_data->shared_type;
283 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
286 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
288 for_each_cpu(i, policy->cpus) {
289 if (unlikely(i == cpu))
292 memcpy(&all_cpu_data[i]->perf_caps, caps,
293 sizeof(cpu_data->perf_caps));
295 } else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
296 /* Support only SW_ANY for now. */
297 pr_debug("Unsupported CPU co-ord type\n");
301 cpu_data->cur_policy = policy;
304 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
307 if (caps->highest_perf > caps->nominal_perf)
308 boost_supported = true;
310 /* Set policy->cur to max now. The governors will adjust later. */
311 policy->cur = cppc_cpufreq_perf_to_khz(cpu_data, caps->highest_perf);
312 cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
314 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
316 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
317 caps->highest_perf, cpu, ret);
322 static inline u64 get_delta(u64 t1, u64 t0)
324 if (t1 > t0 || t0 > ~(u32)0)
327 return (u32)t1 - (u32)t0;
330 static int cppc_get_rate_from_fbctrs(struct cppc_cpudata *cpu_data,
331 struct cppc_perf_fb_ctrs fb_ctrs_t0,
332 struct cppc_perf_fb_ctrs fb_ctrs_t1)
334 u64 delta_reference, delta_delivered;
335 u64 reference_perf, delivered_perf;
337 reference_perf = fb_ctrs_t0.reference_perf;
339 delta_reference = get_delta(fb_ctrs_t1.reference,
340 fb_ctrs_t0.reference);
341 delta_delivered = get_delta(fb_ctrs_t1.delivered,
342 fb_ctrs_t0.delivered);
344 /* Check to avoid divide-by zero */
345 if (delta_reference || delta_delivered)
346 delivered_perf = (reference_perf * delta_delivered) /
349 delivered_perf = cpu_data->perf_ctrls.desired_perf;
351 return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
354 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
356 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
357 struct cppc_cpudata *cpu_data = all_cpu_data[cpu];
360 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
364 udelay(2); /* 2usec delay between sampling */
366 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
370 return cppc_get_rate_from_fbctrs(cpu_data, fb_ctrs_t0, fb_ctrs_t1);
373 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
375 struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
376 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
379 if (!boost_supported) {
380 pr_err("BOOST not supported by CPU or firmware\n");
385 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
388 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
390 policy->cpuinfo.max_freq = policy->max;
392 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
399 static struct cpufreq_driver cppc_cpufreq_driver = {
400 .flags = CPUFREQ_CONST_LOOPS,
401 .verify = cppc_verify_policy,
402 .target = cppc_cpufreq_set_target,
403 .get = cppc_cpufreq_get_rate,
404 .init = cppc_cpufreq_cpu_init,
405 .stop_cpu = cppc_cpufreq_stop_cpu,
406 .set_boost = cppc_cpufreq_set_boost,
407 .name = "cppc_cpufreq",
411 * HISI platform does not support delivered performance counter and
412 * reference performance counter. It can calculate the performance using the
413 * platform specific mechanism. We reuse the desired performance register to
414 * store the real performance calculated by the platform.
416 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
418 struct cppc_cpudata *cpu_data = all_cpu_data[cpu];
422 ret = cppc_get_desired_perf(cpu, &desired_perf);
426 return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
429 static void cppc_check_hisi_workaround(void)
431 struct acpi_table_header *tbl;
432 acpi_status status = AE_OK;
435 status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
436 if (ACPI_FAILURE(status) || !tbl)
439 for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
440 if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
441 !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
442 wa_info[i].oem_revision == tbl->oem_revision) {
443 /* Overwrite the get() callback */
444 cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
452 static int __init cppc_cpufreq_init(void)
454 struct cppc_cpudata *cpu_data;
460 all_cpu_data = kcalloc(num_possible_cpus(), sizeof(void *),
465 for_each_possible_cpu(i) {
466 all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
467 if (!all_cpu_data[i])
470 cpu_data = all_cpu_data[i];
471 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
475 ret = acpi_get_psd_map(all_cpu_data);
477 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
481 cppc_check_hisi_workaround();
483 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
490 for_each_possible_cpu(i) {
491 cpu_data = all_cpu_data[i];
494 free_cpumask_var(cpu_data->shared_cpu_map);
502 static void __exit cppc_cpufreq_exit(void)
504 struct cppc_cpudata *cpu_data;
507 cpufreq_unregister_driver(&cppc_cpufreq_driver);
509 for_each_possible_cpu(i) {
510 cpu_data = all_cpu_data[i];
511 free_cpumask_var(cpu_data->shared_cpu_map);
518 module_exit(cppc_cpufreq_exit);
519 MODULE_AUTHOR("Ashwin Chaugule");
520 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
521 MODULE_LICENSE("GPL");
523 late_initcall(cppc_cpufreq_init);
525 static const struct acpi_device_id cppc_acpi_ids[] __used = {
526 {ACPI_PROCESSOR_DEVICE_HID, },
530 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);