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.
11 #define pr_fmt(fmt) "CPPC Cpufreq:" fmt
13 #include <linux/arch_topology.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/cpu.h>
18 #include <linux/cpufreq.h>
19 #include <linux/dmi.h>
20 #include <linux/irq_work.h>
21 #include <linux/kthread.h>
22 #include <linux/time.h>
23 #include <linux/vmalloc.h>
24 #include <uapi/linux/sched/types.h>
26 #include <asm/unaligned.h>
28 #include <acpi/cppc_acpi.h>
30 /* Minimum struct length needed for the DMI processor entry we want */
31 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
33 /* Offset in the DMI processor structure for the max frequency */
34 #define DMI_PROCESSOR_MAX_SPEED 0x14
37 * This list contains information parsed from per CPU ACPI _CPC and _PSD
38 * structures: e.g. the highest and lowest supported performance, capabilities,
39 * desired performance, level requested etc. Depending on the share_type, not
40 * all CPUs will have an entry in the list.
42 static LIST_HEAD(cpu_data_list);
44 static bool boost_supported;
46 struct cppc_workaround_oem_info {
47 char oem_id[ACPI_OEM_ID_SIZE + 1];
48 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
52 static struct cppc_workaround_oem_info wa_info[] = {
55 .oem_table_id = "HIP07 ",
59 .oem_table_id = "HIP08 ",
64 #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
66 /* Frequency invariance support */
67 struct cppc_freq_invariance {
69 struct irq_work irq_work;
70 struct kthread_work work;
71 struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
72 struct cppc_cpudata *cpu_data;
75 static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
76 static struct kthread_worker *kworker_fie;
77 static bool fie_disabled;
79 static struct cpufreq_driver cppc_cpufreq_driver;
80 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu);
81 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
82 struct cppc_perf_fb_ctrs fb_ctrs_t0,
83 struct cppc_perf_fb_ctrs fb_ctrs_t1);
86 * cppc_scale_freq_workfn - CPPC arch_freq_scale updater for frequency invariance
87 * @work: The work item.
89 * The CPPC driver register itself with the topology core to provide its own
90 * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which
91 * gets called by the scheduler on every tick.
93 * Note that the arch specific counters have higher priority than CPPC counters,
94 * if available, though the CPPC driver doesn't need to have any special
97 * On an invocation of cppc_scale_freq_tick(), we schedule an irq work (since we
98 * reach here from hard-irq context), which then schedules a normal work item
99 * and cppc_scale_freq_workfn() updates the per_cpu arch_freq_scale variable
100 * based on the counter updates since the last tick.
102 static void cppc_scale_freq_workfn(struct kthread_work *work)
104 struct cppc_freq_invariance *cppc_fi;
105 struct cppc_perf_fb_ctrs fb_ctrs = {0};
106 struct cppc_cpudata *cpu_data;
107 unsigned long local_freq_scale;
110 cppc_fi = container_of(work, struct cppc_freq_invariance, work);
111 cpu_data = cppc_fi->cpu_data;
113 if (cppc_get_perf_ctrs(cppc_fi->cpu, &fb_ctrs)) {
114 pr_warn("%s: failed to read perf counters\n", __func__);
118 cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
119 perf = cppc_perf_from_fbctrs(cpu_data, cppc_fi->prev_perf_fb_ctrs,
122 perf <<= SCHED_CAPACITY_SHIFT;
123 local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf);
124 if (WARN_ON(local_freq_scale > 1024))
125 local_freq_scale = 1024;
127 per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale;
130 static void cppc_irq_work(struct irq_work *irq_work)
132 struct cppc_freq_invariance *cppc_fi;
134 cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
135 kthread_queue_work(kworker_fie, &cppc_fi->work);
138 static void cppc_scale_freq_tick(void)
140 struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id());
143 * cppc_get_perf_ctrs() can potentially sleep, call that from the right
146 irq_work_queue(&cppc_fi->irq_work);
149 static struct scale_freq_data cppc_sftd = {
150 .source = SCALE_FREQ_SOURCE_CPPC,
151 .set_freq_scale = cppc_scale_freq_tick,
154 static void cppc_freq_invariance_policy_init(struct cpufreq_policy *policy,
155 struct cppc_cpudata *cpu_data)
157 struct cppc_perf_fb_ctrs fb_ctrs = {0};
158 struct cppc_freq_invariance *cppc_fi;
161 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
167 for_each_cpu(i, policy->cpus) {
168 cppc_fi = &per_cpu(cppc_freq_inv, i);
170 cppc_fi->cpu_data = cpu_data;
171 kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn);
172 init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
174 ret = cppc_get_perf_ctrs(i, &fb_ctrs);
176 pr_warn("%s: failed to read perf counters: %d\n",
180 cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
185 static void __init cppc_freq_invariance_init(void)
187 struct sched_attr attr = {
188 .size = sizeof(struct sched_attr),
189 .sched_policy = SCHED_DEADLINE,
193 * Fake (unused) bandwidth; workaround to "fix"
194 * priority inheritance.
196 .sched_runtime = 1000000,
197 .sched_deadline = 10000000,
198 .sched_period = 10000000,
202 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
208 kworker_fie = kthread_create_worker(0, "cppc_fie");
209 if (IS_ERR(kworker_fie))
212 ret = sched_setattr_nocheck(kworker_fie->task, &attr);
214 pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__,
216 kthread_destroy_worker(kworker_fie);
220 /* Register for freq-invariance */
221 topology_set_scale_freq_source(&cppc_sftd, cpu_present_mask);
224 static void cppc_freq_invariance_exit(void)
226 struct cppc_freq_invariance *cppc_fi;
229 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
235 topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, cpu_present_mask);
237 for_each_possible_cpu(i) {
238 cppc_fi = &per_cpu(cppc_freq_inv, i);
239 irq_work_sync(&cppc_fi->irq_work);
242 kthread_destroy_worker(kworker_fie);
248 cppc_freq_invariance_policy_init(struct cpufreq_policy *policy,
249 struct cppc_cpudata *cpu_data)
253 static inline void cppc_freq_invariance_init(void)
257 static inline void cppc_freq_invariance_exit(void)
260 #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
262 /* Callback function used to retrieve the max frequency from DMI */
263 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
265 const u8 *dmi_data = (const u8 *)dm;
266 u16 *mhz = (u16 *)private;
268 if (dm->type == DMI_ENTRY_PROCESSOR &&
269 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
270 u16 val = (u16)get_unaligned((const u16 *)
271 (dmi_data + DMI_PROCESSOR_MAX_SPEED));
272 *mhz = val > *mhz ? val : *mhz;
276 /* Look up the max frequency in DMI */
277 static u64 cppc_get_dmi_max_khz(void)
281 dmi_walk(cppc_find_dmi_mhz, &mhz);
284 * Real stupid fallback value, just in case there is no
293 * If CPPC lowest_freq and nominal_freq registers are exposed then we can
294 * use them to convert perf to freq and vice versa
296 * If the perf/freq point lies between Nominal and Lowest, we can treat
297 * (Low perf, Low freq) and (Nom Perf, Nom freq) as 2D co-ordinates of a line
298 * and extrapolate the rest
299 * For perf/freq > Nominal, we use the ratio perf:freq at Nominal for conversion
301 static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data,
304 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
308 if (caps->lowest_freq && caps->nominal_freq) {
309 if (perf >= caps->nominal_perf) {
310 mul = caps->nominal_freq;
311 div = caps->nominal_perf;
313 mul = caps->nominal_freq - caps->lowest_freq;
314 div = caps->nominal_perf - caps->lowest_perf;
318 max_khz = cppc_get_dmi_max_khz();
320 div = caps->highest_perf;
322 return (u64)perf * mul / div;
325 static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data,
328 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
332 if (caps->lowest_freq && caps->nominal_freq) {
333 if (freq >= caps->nominal_freq) {
334 mul = caps->nominal_perf;
335 div = caps->nominal_freq;
337 mul = caps->lowest_perf;
338 div = caps->lowest_freq;
342 max_khz = cppc_get_dmi_max_khz();
343 mul = caps->highest_perf;
347 return (u64)freq * mul / div;
350 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
351 unsigned int target_freq,
352 unsigned int relation)
355 struct cppc_cpudata *cpu_data = policy->driver_data;
356 unsigned int cpu = policy->cpu;
357 struct cpufreq_freqs freqs;
361 desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
362 /* Return if it is exactly the same perf */
363 if (desired_perf == cpu_data->perf_ctrls.desired_perf)
366 cpu_data->perf_ctrls.desired_perf = desired_perf;
367 freqs.old = policy->cur;
368 freqs.new = target_freq;
370 cpufreq_freq_transition_begin(policy, &freqs);
371 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
372 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
375 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
381 static int cppc_verify_policy(struct cpufreq_policy_data *policy)
383 cpufreq_verify_within_cpu_limits(policy);
387 static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
389 struct cppc_cpudata *cpu_data = policy->driver_data;
390 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
391 unsigned int cpu = policy->cpu;
394 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
396 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
398 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
399 caps->lowest_perf, cpu, ret);
401 /* Remove CPU node from list and free driver data for policy */
402 free_cpumask_var(cpu_data->shared_cpu_map);
403 list_del(&cpu_data->node);
404 kfree(policy->driver_data);
405 policy->driver_data = NULL;
409 * The PCC subspace describes the rate at which platform can accept commands
410 * on the shared PCC channel (including READs which do not count towards freq
411 * transition requests), so ideally we need to use the PCC values as a fallback
412 * if we don't have a platform specific transition_delay_us
415 #include <asm/cputype.h>
417 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
419 unsigned long implementor = read_cpuid_implementor();
420 unsigned long part_num = read_cpuid_part_number();
422 switch (implementor) {
423 case ARM_CPU_IMP_QCOM:
425 case QCOM_CPU_PART_FALKOR_V1:
426 case QCOM_CPU_PART_FALKOR:
430 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
435 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
437 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
442 static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
444 struct cppc_cpudata *cpu_data;
447 cpu_data = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
451 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
454 ret = acpi_get_psd_map(cpu, cpu_data);
456 pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
460 ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
462 pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
466 /* Convert the lowest and nominal freq from MHz to KHz */
467 cpu_data->perf_caps.lowest_freq *= 1000;
468 cpu_data->perf_caps.nominal_freq *= 1000;
470 list_add(&cpu_data->node, &cpu_data_list);
475 free_cpumask_var(cpu_data->shared_cpu_map);
482 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
484 unsigned int cpu = policy->cpu;
485 struct cppc_cpudata *cpu_data;
486 struct cppc_perf_caps *caps;
489 cpu_data = cppc_cpufreq_get_cpu_data(cpu);
491 pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
494 caps = &cpu_data->perf_caps;
495 policy->driver_data = cpu_data;
498 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
499 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
501 policy->min = cppc_cpufreq_perf_to_khz(cpu_data,
502 caps->lowest_nonlinear_perf);
503 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
507 * Set cpuinfo.min_freq to Lowest to make the full range of performance
508 * available if userspace wants to use any perf between lowest & lowest
511 policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu_data,
513 policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu_data,
516 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
517 policy->shared_type = cpu_data->shared_type;
519 switch (policy->shared_type) {
520 case CPUFREQ_SHARED_TYPE_HW:
521 case CPUFREQ_SHARED_TYPE_NONE:
522 /* Nothing to be done - we'll have a policy for each CPU */
524 case CPUFREQ_SHARED_TYPE_ANY:
526 * All CPUs in the domain will share a policy and all cpufreq
527 * operations will use a single cppc_cpudata structure stored
528 * in policy->driver_data.
530 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
533 pr_debug("Unsupported CPU co-ord type: %d\n",
534 policy->shared_type);
539 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
542 if (caps->highest_perf > caps->nominal_perf)
543 boost_supported = true;
545 /* Set policy->cur to max now. The governors will adjust later. */
546 policy->cur = cppc_cpufreq_perf_to_khz(cpu_data, caps->highest_perf);
547 cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
549 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
551 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
552 caps->highest_perf, cpu, ret);
554 cppc_freq_invariance_policy_init(policy, cpu_data);
560 static inline u64 get_delta(u64 t1, u64 t0)
562 if (t1 > t0 || t0 > ~(u32)0)
565 return (u32)t1 - (u32)t0;
568 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
569 struct cppc_perf_fb_ctrs fb_ctrs_t0,
570 struct cppc_perf_fb_ctrs fb_ctrs_t1)
572 u64 delta_reference, delta_delivered;
575 reference_perf = fb_ctrs_t0.reference_perf;
577 delta_reference = get_delta(fb_ctrs_t1.reference,
578 fb_ctrs_t0.reference);
579 delta_delivered = get_delta(fb_ctrs_t1.delivered,
580 fb_ctrs_t0.delivered);
582 /* Check to avoid divide-by zero and invalid delivered_perf */
583 if (!delta_reference || !delta_delivered)
584 return cpu_data->perf_ctrls.desired_perf;
586 return (reference_perf * delta_delivered) / delta_reference;
589 static int cppc_get_rate_from_fbctrs(struct cppc_cpudata *cpu_data,
590 struct cppc_perf_fb_ctrs fb_ctrs_t0,
591 struct cppc_perf_fb_ctrs fb_ctrs_t1)
595 delivered_perf = cppc_perf_from_fbctrs(cpu_data, fb_ctrs_t0,
598 return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
601 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
603 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
604 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
605 struct cppc_cpudata *cpu_data = policy->driver_data;
608 cpufreq_cpu_put(policy);
610 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
614 udelay(2); /* 2usec delay between sampling */
616 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
620 return cppc_get_rate_from_fbctrs(cpu_data, fb_ctrs_t0, fb_ctrs_t1);
623 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
625 struct cppc_cpudata *cpu_data = policy->driver_data;
626 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
629 if (!boost_supported) {
630 pr_err("BOOST not supported by CPU or firmware\n");
635 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
638 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
640 policy->cpuinfo.max_freq = policy->max;
642 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
649 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
651 struct cppc_cpudata *cpu_data = policy->driver_data;
653 return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
655 cpufreq_freq_attr_ro(freqdomain_cpus);
657 static struct freq_attr *cppc_cpufreq_attr[] = {
662 static struct cpufreq_driver cppc_cpufreq_driver = {
663 .flags = CPUFREQ_CONST_LOOPS,
664 .verify = cppc_verify_policy,
665 .target = cppc_cpufreq_set_target,
666 .get = cppc_cpufreq_get_rate,
667 .init = cppc_cpufreq_cpu_init,
668 .stop_cpu = cppc_cpufreq_stop_cpu,
669 .set_boost = cppc_cpufreq_set_boost,
670 .attr = cppc_cpufreq_attr,
671 .name = "cppc_cpufreq",
675 * HISI platform does not support delivered performance counter and
676 * reference performance counter. It can calculate the performance using the
677 * platform specific mechanism. We reuse the desired performance register to
678 * store the real performance calculated by the platform.
680 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
682 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
683 struct cppc_cpudata *cpu_data = policy->driver_data;
687 cpufreq_cpu_put(policy);
689 ret = cppc_get_desired_perf(cpu, &desired_perf);
693 return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
696 static void cppc_check_hisi_workaround(void)
698 struct acpi_table_header *tbl;
699 acpi_status status = AE_OK;
702 status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
703 if (ACPI_FAILURE(status) || !tbl)
706 for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
707 if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
708 !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
709 wa_info[i].oem_revision == tbl->oem_revision) {
710 /* Overwrite the get() callback */
711 cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
719 static int __init cppc_cpufreq_init(void)
723 if ((acpi_disabled) || !acpi_cpc_valid())
726 INIT_LIST_HEAD(&cpu_data_list);
728 cppc_check_hisi_workaround();
730 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
732 cppc_freq_invariance_init();
737 static inline void free_cpu_data(void)
739 struct cppc_cpudata *iter, *tmp;
741 list_for_each_entry_safe(iter, tmp, &cpu_data_list, node) {
742 free_cpumask_var(iter->shared_cpu_map);
743 list_del(&iter->node);
749 static void __exit cppc_cpufreq_exit(void)
751 cppc_freq_invariance_exit();
752 cpufreq_unregister_driver(&cppc_cpufreq_driver);
757 module_exit(cppc_cpufreq_exit);
758 MODULE_AUTHOR("Ashwin Chaugule");
759 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
760 MODULE_LICENSE("GPL");
762 late_initcall(cppc_cpufreq_init);
764 static const struct acpi_device_id cppc_acpi_ids[] __used = {
765 {ACPI_PROCESSOR_DEVICE_HID, },
769 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);