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/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 static struct cpufreq_driver cppc_cpufreq_driver;
70 } fie_disabled = FIE_UNSET;
72 #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
73 module_param(fie_disabled, int, 0444);
74 MODULE_PARM_DESC(fie_disabled, "Disable Frequency Invariance Engine (FIE)");
76 /* Frequency invariance support */
77 struct cppc_freq_invariance {
79 struct irq_work irq_work;
80 struct kthread_work work;
81 struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
82 struct cppc_cpudata *cpu_data;
85 static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
86 static struct kthread_worker *kworker_fie;
88 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu);
89 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
90 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
91 struct cppc_perf_fb_ctrs *fb_ctrs_t1);
94 * cppc_scale_freq_workfn - CPPC arch_freq_scale updater for frequency invariance
95 * @work: The work item.
97 * The CPPC driver register itself with the topology core to provide its own
98 * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which
99 * gets called by the scheduler on every tick.
101 * Note that the arch specific counters have higher priority than CPPC counters,
102 * if available, though the CPPC driver doesn't need to have any special
105 * On an invocation of cppc_scale_freq_tick(), we schedule an irq work (since we
106 * reach here from hard-irq context), which then schedules a normal work item
107 * and cppc_scale_freq_workfn() updates the per_cpu arch_freq_scale variable
108 * based on the counter updates since the last tick.
110 static void cppc_scale_freq_workfn(struct kthread_work *work)
112 struct cppc_freq_invariance *cppc_fi;
113 struct cppc_perf_fb_ctrs fb_ctrs = {0};
114 struct cppc_cpudata *cpu_data;
115 unsigned long local_freq_scale;
118 cppc_fi = container_of(work, struct cppc_freq_invariance, work);
119 cpu_data = cppc_fi->cpu_data;
121 if (cppc_get_perf_ctrs(cppc_fi->cpu, &fb_ctrs)) {
122 pr_warn("%s: failed to read perf counters\n", __func__);
126 perf = cppc_perf_from_fbctrs(cpu_data, &cppc_fi->prev_perf_fb_ctrs,
128 cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
130 perf <<= SCHED_CAPACITY_SHIFT;
131 local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf);
133 /* This can happen due to counter's overflow */
134 if (unlikely(local_freq_scale > 1024))
135 local_freq_scale = 1024;
137 per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale;
140 static void cppc_irq_work(struct irq_work *irq_work)
142 struct cppc_freq_invariance *cppc_fi;
144 cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
145 kthread_queue_work(kworker_fie, &cppc_fi->work);
148 static void cppc_scale_freq_tick(void)
150 struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id());
153 * cppc_get_perf_ctrs() can potentially sleep, call that from the right
156 irq_work_queue(&cppc_fi->irq_work);
159 static struct scale_freq_data cppc_sftd = {
160 .source = SCALE_FREQ_SOURCE_CPPC,
161 .set_freq_scale = cppc_scale_freq_tick,
164 static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
166 struct cppc_freq_invariance *cppc_fi;
172 for_each_cpu(cpu, policy->cpus) {
173 cppc_fi = &per_cpu(cppc_freq_inv, cpu);
175 cppc_fi->cpu_data = policy->driver_data;
176 kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn);
177 init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
179 ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
181 pr_warn("%s: failed to read perf counters for cpu:%d: %d\n",
185 * Don't abort if the CPU was offline while the driver
186 * was getting registered.
193 /* Register for freq-invariance */
194 topology_set_scale_freq_source(&cppc_sftd, policy->cpus);
198 * We free all the resources on policy's removal and not on CPU removal as the
199 * irq-work are per-cpu and the hotplug core takes care of flushing the pending
200 * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work
201 * fires on another CPU after the concerned CPU is removed, it won't harm.
203 * We just need to make sure to remove them all on policy->exit().
205 static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
207 struct cppc_freq_invariance *cppc_fi;
213 /* policy->cpus will be empty here, use related_cpus instead */
214 topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus);
216 for_each_cpu(cpu, policy->related_cpus) {
217 cppc_fi = &per_cpu(cppc_freq_inv, cpu);
218 irq_work_sync(&cppc_fi->irq_work);
219 kthread_cancel_work_sync(&cppc_fi->work);
223 static void __init cppc_freq_invariance_init(void)
225 struct sched_attr attr = {
226 .size = sizeof(struct sched_attr),
227 .sched_policy = SCHED_DEADLINE,
231 * Fake (unused) bandwidth; workaround to "fix"
232 * priority inheritance.
234 .sched_runtime = 1000000,
235 .sched_deadline = 10000000,
236 .sched_period = 10000000,
240 if (fie_disabled != FIE_ENABLED && fie_disabled != FIE_DISABLED) {
241 fie_disabled = FIE_ENABLED;
242 if (cppc_perf_ctrs_in_pcc()) {
243 pr_info("FIE not enabled on systems with registers in PCC\n");
244 fie_disabled = FIE_DISABLED;
251 kworker_fie = kthread_create_worker(0, "cppc_fie");
252 if (IS_ERR(kworker_fie))
255 ret = sched_setattr_nocheck(kworker_fie->task, &attr);
257 pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__,
259 kthread_destroy_worker(kworker_fie);
264 static void cppc_freq_invariance_exit(void)
269 kthread_destroy_worker(kworker_fie);
274 static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
278 static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
282 static inline void cppc_freq_invariance_init(void)
286 static inline void cppc_freq_invariance_exit(void)
289 #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
291 /* Callback function used to retrieve the max frequency from DMI */
292 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
294 const u8 *dmi_data = (const u8 *)dm;
295 u16 *mhz = (u16 *)private;
297 if (dm->type == DMI_ENTRY_PROCESSOR &&
298 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
299 u16 val = (u16)get_unaligned((const u16 *)
300 (dmi_data + DMI_PROCESSOR_MAX_SPEED));
301 *mhz = val > *mhz ? val : *mhz;
305 /* Look up the max frequency in DMI */
306 static u64 cppc_get_dmi_max_khz(void)
310 dmi_walk(cppc_find_dmi_mhz, &mhz);
313 * Real stupid fallback value, just in case there is no
322 * If CPPC lowest_freq and nominal_freq registers are exposed then we can
323 * use them to convert perf to freq and vice versa. The conversion is
324 * extrapolated as an affine function passing by the 2 points:
325 * - (Low perf, Low freq)
326 * - (Nominal perf, Nominal perf)
328 static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data,
331 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
332 s64 retval, offset = 0;
336 if (caps->lowest_freq && caps->nominal_freq) {
337 mul = caps->nominal_freq - caps->lowest_freq;
338 div = caps->nominal_perf - caps->lowest_perf;
339 offset = caps->nominal_freq - div64_u64(caps->nominal_perf * mul, div);
342 max_khz = cppc_get_dmi_max_khz();
344 div = caps->highest_perf;
347 retval = offset + div64_u64(perf * mul, div);
353 static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data,
356 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
357 s64 retval, offset = 0;
361 if (caps->lowest_freq && caps->nominal_freq) {
362 mul = caps->nominal_perf - caps->lowest_perf;
363 div = caps->nominal_freq - caps->lowest_freq;
364 offset = caps->nominal_perf - div64_u64(caps->nominal_freq * mul, div);
367 max_khz = cppc_get_dmi_max_khz();
368 mul = caps->highest_perf;
372 retval = offset + div64_u64(freq * mul, div);
378 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
379 unsigned int target_freq,
380 unsigned int relation)
383 struct cppc_cpudata *cpu_data = policy->driver_data;
384 unsigned int cpu = policy->cpu;
385 struct cpufreq_freqs freqs;
389 desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
390 /* Return if it is exactly the same perf */
391 if (desired_perf == cpu_data->perf_ctrls.desired_perf)
394 cpu_data->perf_ctrls.desired_perf = desired_perf;
395 freqs.old = policy->cur;
396 freqs.new = target_freq;
398 cpufreq_freq_transition_begin(policy, &freqs);
399 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
400 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
403 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
409 static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy *policy,
410 unsigned int target_freq)
412 struct cppc_cpudata *cpu_data = policy->driver_data;
413 unsigned int cpu = policy->cpu;
417 desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
418 cpu_data->perf_ctrls.desired_perf = desired_perf;
419 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
422 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
430 static int cppc_verify_policy(struct cpufreq_policy_data *policy)
432 cpufreq_verify_within_cpu_limits(policy);
437 * The PCC subspace describes the rate at which platform can accept commands
438 * on the shared PCC channel (including READs which do not count towards freq
439 * transition requests), so ideally we need to use the PCC values as a fallback
440 * if we don't have a platform specific transition_delay_us
443 #include <asm/cputype.h>
445 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
447 unsigned long implementor = read_cpuid_implementor();
448 unsigned long part_num = read_cpuid_part_number();
450 switch (implementor) {
451 case ARM_CPU_IMP_QCOM:
453 case QCOM_CPU_PART_FALKOR_V1:
454 case QCOM_CPU_PART_FALKOR:
458 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
461 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
463 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
467 #if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL)
469 static DEFINE_PER_CPU(unsigned int, efficiency_class);
470 static void cppc_cpufreq_register_em(struct cpufreq_policy *policy);
472 /* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */
473 #define CPPC_EM_CAP_STEP (20)
474 /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
475 #define CPPC_EM_COST_STEP (1)
476 /* Add a cost gap correspnding to the energy of 4 CPUs. */
477 #define CPPC_EM_COST_GAP (4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
480 static unsigned int get_perf_level_count(struct cpufreq_policy *policy)
482 struct cppc_perf_caps *perf_caps;
483 unsigned int min_cap, max_cap;
484 struct cppc_cpudata *cpu_data;
485 int cpu = policy->cpu;
487 cpu_data = policy->driver_data;
488 perf_caps = &cpu_data->perf_caps;
489 max_cap = arch_scale_cpu_capacity(cpu);
490 min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
491 perf_caps->highest_perf);
492 if ((min_cap == 0) || (max_cap < min_cap))
494 return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP;
498 * The cost is defined as:
499 * cost = power * max_frequency / frequency
501 static inline unsigned long compute_cost(int cpu, int step)
503 return CPPC_EM_COST_GAP * per_cpu(efficiency_class, cpu) +
504 step * CPPC_EM_COST_STEP;
507 static int cppc_get_cpu_power(struct device *cpu_dev,
508 unsigned long *power, unsigned long *KHz)
510 unsigned long perf_step, perf_prev, perf, perf_check;
511 unsigned int min_step, max_step, step, step_check;
512 unsigned long prev_freq = *KHz;
513 unsigned int min_cap, max_cap;
514 struct cpufreq_policy *policy;
516 struct cppc_perf_caps *perf_caps;
517 struct cppc_cpudata *cpu_data;
519 policy = cpufreq_cpu_get_raw(cpu_dev->id);
520 cpu_data = policy->driver_data;
521 perf_caps = &cpu_data->perf_caps;
522 max_cap = arch_scale_cpu_capacity(cpu_dev->id);
523 min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
524 perf_caps->highest_perf);
525 perf_step = div_u64((u64)CPPC_EM_CAP_STEP * perf_caps->highest_perf,
527 min_step = min_cap / CPPC_EM_CAP_STEP;
528 max_step = max_cap / CPPC_EM_CAP_STEP;
530 perf_prev = cppc_cpufreq_khz_to_perf(cpu_data, *KHz);
531 step = perf_prev / perf_step;
536 if (min_step == max_step) {
538 perf = perf_caps->highest_perf;
539 } else if (step < min_step) {
541 perf = perf_caps->lowest_perf;
544 if (step == max_step)
545 perf = perf_caps->highest_perf;
547 perf = step * perf_step;
550 *KHz = cppc_cpufreq_perf_to_khz(cpu_data, perf);
551 perf_check = cppc_cpufreq_khz_to_perf(cpu_data, *KHz);
552 step_check = perf_check / perf_step;
555 * To avoid bad integer approximation, check that new frequency value
556 * increased and that the new frequency will be converted to the
557 * desired step value.
559 while ((*KHz == prev_freq) || (step_check != step)) {
561 *KHz = cppc_cpufreq_perf_to_khz(cpu_data, perf);
562 perf_check = cppc_cpufreq_khz_to_perf(cpu_data, *KHz);
563 step_check = perf_check / perf_step;
567 * With an artificial EM, only the cost value is used. Still the power
568 * is populated such as 0 < power < EM_MAX_POWER. This allows to add
569 * more sense to the artificial performance states.
571 *power = compute_cost(cpu_dev->id, step);
576 static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
579 unsigned long perf_step, perf_prev;
580 struct cppc_perf_caps *perf_caps;
581 struct cpufreq_policy *policy;
582 struct cppc_cpudata *cpu_data;
583 unsigned int max_cap;
586 policy = cpufreq_cpu_get_raw(cpu_dev->id);
587 cpu_data = policy->driver_data;
588 perf_caps = &cpu_data->perf_caps;
589 max_cap = arch_scale_cpu_capacity(cpu_dev->id);
591 perf_prev = cppc_cpufreq_khz_to_perf(cpu_data, KHz);
592 perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap;
593 step = perf_prev / perf_step;
595 *cost = compute_cost(cpu_dev->id, step);
600 static int populate_efficiency_class(void)
602 struct acpi_madt_generic_interrupt *gicc;
603 DECLARE_BITMAP(used_classes, 256) = {};
604 int class, cpu, index;
606 for_each_possible_cpu(cpu) {
607 gicc = acpi_cpu_get_madt_gicc(cpu);
608 class = gicc->efficiency_class;
609 bitmap_set(used_classes, class, 1);
612 if (bitmap_weight(used_classes, 256) <= 1) {
613 pr_debug("Efficiency classes are all equal (=%d). "
614 "No EM registered", class);
619 * Squeeze efficiency class values on [0:#efficiency_class-1].
620 * Values are per spec in [0:255].
623 for_each_set_bit(class, used_classes, 256) {
624 for_each_possible_cpu(cpu) {
625 gicc = acpi_cpu_get_madt_gicc(cpu);
626 if (gicc->efficiency_class == class)
627 per_cpu(efficiency_class, cpu) = index;
631 cppc_cpufreq_driver.register_em = cppc_cpufreq_register_em;
636 static void cppc_cpufreq_register_em(struct cpufreq_policy *policy)
638 struct cppc_cpudata *cpu_data;
639 struct em_data_callback em_cb =
640 EM_ADV_DATA_CB(cppc_get_cpu_power, cppc_get_cpu_cost);
642 cpu_data = policy->driver_data;
643 em_dev_register_perf_domain(get_cpu_device(policy->cpu),
644 get_perf_level_count(policy), &em_cb,
645 cpu_data->shared_cpu_map, 0);
649 static int populate_efficiency_class(void)
655 static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
657 struct cppc_cpudata *cpu_data;
660 cpu_data = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
664 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
667 ret = acpi_get_psd_map(cpu, cpu_data);
669 pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
673 ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
675 pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
679 /* Convert the lowest and nominal freq from MHz to KHz */
680 cpu_data->perf_caps.lowest_freq *= 1000;
681 cpu_data->perf_caps.nominal_freq *= 1000;
683 list_add(&cpu_data->node, &cpu_data_list);
688 free_cpumask_var(cpu_data->shared_cpu_map);
695 static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy)
697 struct cppc_cpudata *cpu_data = policy->driver_data;
699 list_del(&cpu_data->node);
700 free_cpumask_var(cpu_data->shared_cpu_map);
702 policy->driver_data = NULL;
705 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
707 unsigned int cpu = policy->cpu;
708 struct cppc_cpudata *cpu_data;
709 struct cppc_perf_caps *caps;
712 cpu_data = cppc_cpufreq_get_cpu_data(cpu);
714 pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
717 caps = &cpu_data->perf_caps;
718 policy->driver_data = cpu_data;
721 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
722 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
724 policy->min = cppc_cpufreq_perf_to_khz(cpu_data,
725 caps->lowest_nonlinear_perf);
726 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
730 * Set cpuinfo.min_freq to Lowest to make the full range of performance
731 * available if userspace wants to use any perf between lowest & lowest
734 policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu_data,
736 policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu_data,
739 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
740 policy->shared_type = cpu_data->shared_type;
742 switch (policy->shared_type) {
743 case CPUFREQ_SHARED_TYPE_HW:
744 case CPUFREQ_SHARED_TYPE_NONE:
745 /* Nothing to be done - we'll have a policy for each CPU */
747 case CPUFREQ_SHARED_TYPE_ANY:
749 * All CPUs in the domain will share a policy and all cpufreq
750 * operations will use a single cppc_cpudata structure stored
751 * in policy->driver_data.
753 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
756 pr_debug("Unsupported CPU co-ord type: %d\n",
757 policy->shared_type);
762 policy->fast_switch_possible = cppc_allow_fast_switch();
763 policy->dvfs_possible_from_any_cpu = true;
766 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
769 if (caps->highest_perf > caps->nominal_perf)
770 boost_supported = true;
772 /* Set policy->cur to max now. The governors will adjust later. */
773 policy->cur = cppc_cpufreq_perf_to_khz(cpu_data, caps->highest_perf);
774 cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
776 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
778 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
779 caps->highest_perf, cpu, ret);
783 cppc_cpufreq_cpu_fie_init(policy);
787 cppc_cpufreq_put_cpu_data(policy);
791 static int cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
793 struct cppc_cpudata *cpu_data = policy->driver_data;
794 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
795 unsigned int cpu = policy->cpu;
798 cppc_cpufreq_cpu_fie_exit(policy);
800 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
802 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
804 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
805 caps->lowest_perf, cpu, ret);
807 cppc_cpufreq_put_cpu_data(policy);
811 static inline u64 get_delta(u64 t1, u64 t0)
813 if (t1 > t0 || t0 > ~(u32)0)
816 return (u32)t1 - (u32)t0;
819 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
820 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
821 struct cppc_perf_fb_ctrs *fb_ctrs_t1)
823 u64 delta_reference, delta_delivered;
826 reference_perf = fb_ctrs_t0->reference_perf;
828 delta_reference = get_delta(fb_ctrs_t1->reference,
829 fb_ctrs_t0->reference);
830 delta_delivered = get_delta(fb_ctrs_t1->delivered,
831 fb_ctrs_t0->delivered);
833 /* Check to avoid divide-by zero and invalid delivered_perf */
834 if (!delta_reference || !delta_delivered)
835 return cpu_data->perf_ctrls.desired_perf;
837 return (reference_perf * delta_delivered) / delta_reference;
840 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
842 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
843 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
844 struct cppc_cpudata *cpu_data = policy->driver_data;
848 cpufreq_cpu_put(policy);
850 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
854 udelay(2); /* 2usec delay between sampling */
856 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
860 delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
863 return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
866 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
868 struct cppc_cpudata *cpu_data = policy->driver_data;
869 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
872 if (!boost_supported) {
873 pr_err("BOOST not supported by CPU or firmware\n");
878 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
881 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
883 policy->cpuinfo.max_freq = policy->max;
885 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
892 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
894 struct cppc_cpudata *cpu_data = policy->driver_data;
896 return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
898 cpufreq_freq_attr_ro(freqdomain_cpus);
900 static struct freq_attr *cppc_cpufreq_attr[] = {
905 static struct cpufreq_driver cppc_cpufreq_driver = {
906 .flags = CPUFREQ_CONST_LOOPS,
907 .verify = cppc_verify_policy,
908 .target = cppc_cpufreq_set_target,
909 .get = cppc_cpufreq_get_rate,
910 .fast_switch = cppc_cpufreq_fast_switch,
911 .init = cppc_cpufreq_cpu_init,
912 .exit = cppc_cpufreq_cpu_exit,
913 .set_boost = cppc_cpufreq_set_boost,
914 .attr = cppc_cpufreq_attr,
915 .name = "cppc_cpufreq",
919 * HISI platform does not support delivered performance counter and
920 * reference performance counter. It can calculate the performance using the
921 * platform specific mechanism. We reuse the desired performance register to
922 * store the real performance calculated by the platform.
924 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
926 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
927 struct cppc_cpudata *cpu_data = policy->driver_data;
931 cpufreq_cpu_put(policy);
933 ret = cppc_get_desired_perf(cpu, &desired_perf);
937 return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
940 static void cppc_check_hisi_workaround(void)
942 struct acpi_table_header *tbl;
943 acpi_status status = AE_OK;
946 status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
947 if (ACPI_FAILURE(status) || !tbl)
950 for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
951 if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
952 !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
953 wa_info[i].oem_revision == tbl->oem_revision) {
954 /* Overwrite the get() callback */
955 cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
956 fie_disabled = FIE_DISABLED;
964 static int __init cppc_cpufreq_init(void)
968 if (!acpi_cpc_valid())
971 cppc_check_hisi_workaround();
972 cppc_freq_invariance_init();
973 populate_efficiency_class();
975 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
977 cppc_freq_invariance_exit();
982 static inline void free_cpu_data(void)
984 struct cppc_cpudata *iter, *tmp;
986 list_for_each_entry_safe(iter, tmp, &cpu_data_list, node) {
987 free_cpumask_var(iter->shared_cpu_map);
988 list_del(&iter->node);
994 static void __exit cppc_cpufreq_exit(void)
996 cpufreq_unregister_driver(&cppc_cpufreq_driver);
997 cppc_freq_invariance_exit();
1002 module_exit(cppc_cpufreq_exit);
1003 MODULE_AUTHOR("Ashwin Chaugule");
1004 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
1005 MODULE_LICENSE("GPL");
1007 late_initcall(cppc_cpufreq_init);
1009 static const struct acpi_device_id cppc_acpi_ids[] __used = {
1010 {ACPI_PROCESSOR_DEVICE_HID, },
1014 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);