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/irq_work.h>
20 #include <linux/kthread.h>
21 #include <linux/time.h>
22 #include <linux/vmalloc.h>
23 #include <uapi/linux/sched/types.h>
25 #include <linux/unaligned.h>
27 #include <acpi/cppc_acpi.h>
30 * This list contains information parsed from per CPU ACPI _CPC and _PSD
31 * structures: e.g. the highest and lowest supported performance, capabilities,
32 * desired performance, level requested etc. Depending on the share_type, not
33 * all CPUs will have an entry in the list.
35 static LIST_HEAD(cpu_data_list
);
37 static bool boost_supported
;
39 struct cppc_workaround_oem_info
{
40 char oem_id
[ACPI_OEM_ID_SIZE
+ 1];
41 char oem_table_id
[ACPI_OEM_TABLE_ID_SIZE
+ 1];
45 static struct cppc_workaround_oem_info wa_info
[] = {
48 .oem_table_id
= "HIP07 ",
52 .oem_table_id
= "HIP08 ",
57 static struct cpufreq_driver cppc_cpufreq_driver
;
63 } fie_disabled
= FIE_UNSET
;
65 #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
66 module_param(fie_disabled
, int, 0444);
67 MODULE_PARM_DESC(fie_disabled
, "Disable Frequency Invariance Engine (FIE)");
69 /* Frequency invariance support */
70 struct cppc_freq_invariance
{
72 struct irq_work irq_work
;
73 struct kthread_work work
;
74 struct cppc_perf_fb_ctrs prev_perf_fb_ctrs
;
75 struct cppc_cpudata
*cpu_data
;
78 static DEFINE_PER_CPU(struct cppc_freq_invariance
, cppc_freq_inv
);
79 static struct kthread_worker
*kworker_fie
;
81 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu
);
82 static int cppc_perf_from_fbctrs(struct cppc_cpudata
*cpu_data
,
83 struct cppc_perf_fb_ctrs
*fb_ctrs_t0
,
84 struct cppc_perf_fb_ctrs
*fb_ctrs_t1
);
87 * cppc_scale_freq_workfn - CPPC arch_freq_scale updater for frequency invariance
88 * @work: The work item.
90 * The CPPC driver register itself with the topology core to provide its own
91 * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which
92 * gets called by the scheduler on every tick.
94 * Note that the arch specific counters have higher priority than CPPC counters,
95 * if available, though the CPPC driver doesn't need to have any special
98 * On an invocation of cppc_scale_freq_tick(), we schedule an irq work (since we
99 * reach here from hard-irq context), which then schedules a normal work item
100 * and cppc_scale_freq_workfn() updates the per_cpu arch_freq_scale variable
101 * based on the counter updates since the last tick.
103 static void cppc_scale_freq_workfn(struct kthread_work
*work
)
105 struct cppc_freq_invariance
*cppc_fi
;
106 struct cppc_perf_fb_ctrs fb_ctrs
= {0};
107 struct cppc_cpudata
*cpu_data
;
108 unsigned long local_freq_scale
;
111 cppc_fi
= container_of(work
, struct cppc_freq_invariance
, work
);
112 cpu_data
= cppc_fi
->cpu_data
;
114 if (cppc_get_perf_ctrs(cppc_fi
->cpu
, &fb_ctrs
)) {
115 pr_warn("%s: failed to read perf counters\n", __func__
);
119 perf
= cppc_perf_from_fbctrs(cpu_data
, &cppc_fi
->prev_perf_fb_ctrs
,
121 cppc_fi
->prev_perf_fb_ctrs
= fb_ctrs
;
123 perf
<<= SCHED_CAPACITY_SHIFT
;
124 local_freq_scale
= div64_u64(perf
, cpu_data
->perf_caps
.highest_perf
);
126 /* This can happen due to counter's overflow */
127 if (unlikely(local_freq_scale
> 1024))
128 local_freq_scale
= 1024;
130 per_cpu(arch_freq_scale
, cppc_fi
->cpu
) = local_freq_scale
;
133 static void cppc_irq_work(struct irq_work
*irq_work
)
135 struct cppc_freq_invariance
*cppc_fi
;
137 cppc_fi
= container_of(irq_work
, struct cppc_freq_invariance
, irq_work
);
138 kthread_queue_work(kworker_fie
, &cppc_fi
->work
);
141 static void cppc_scale_freq_tick(void)
143 struct cppc_freq_invariance
*cppc_fi
= &per_cpu(cppc_freq_inv
, smp_processor_id());
146 * cppc_get_perf_ctrs() can potentially sleep, call that from the right
149 irq_work_queue(&cppc_fi
->irq_work
);
152 static struct scale_freq_data cppc_sftd
= {
153 .source
= SCALE_FREQ_SOURCE_CPPC
,
154 .set_freq_scale
= cppc_scale_freq_tick
,
157 static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy
*policy
)
159 struct cppc_freq_invariance
*cppc_fi
;
165 for_each_cpu(cpu
, policy
->cpus
) {
166 cppc_fi
= &per_cpu(cppc_freq_inv
, cpu
);
168 cppc_fi
->cpu_data
= policy
->driver_data
;
169 kthread_init_work(&cppc_fi
->work
, cppc_scale_freq_workfn
);
170 init_irq_work(&cppc_fi
->irq_work
, cppc_irq_work
);
172 ret
= cppc_get_perf_ctrs(cpu
, &cppc_fi
->prev_perf_fb_ctrs
);
174 pr_warn("%s: failed to read perf counters for cpu:%d: %d\n",
178 * Don't abort if the CPU was offline while the driver
179 * was getting registered.
186 /* Register for freq-invariance */
187 topology_set_scale_freq_source(&cppc_sftd
, policy
->cpus
);
191 * We free all the resources on policy's removal and not on CPU removal as the
192 * irq-work are per-cpu and the hotplug core takes care of flushing the pending
193 * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work
194 * fires on another CPU after the concerned CPU is removed, it won't harm.
196 * We just need to make sure to remove them all on policy->exit().
198 static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy
*policy
)
200 struct cppc_freq_invariance
*cppc_fi
;
206 /* policy->cpus will be empty here, use related_cpus instead */
207 topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC
, policy
->related_cpus
);
209 for_each_cpu(cpu
, policy
->related_cpus
) {
210 cppc_fi
= &per_cpu(cppc_freq_inv
, cpu
);
211 irq_work_sync(&cppc_fi
->irq_work
);
212 kthread_cancel_work_sync(&cppc_fi
->work
);
216 static void __init
cppc_freq_invariance_init(void)
218 struct sched_attr attr
= {
219 .size
= sizeof(struct sched_attr
),
220 .sched_policy
= SCHED_DEADLINE
,
224 * Fake (unused) bandwidth; workaround to "fix"
225 * priority inheritance.
227 .sched_runtime
= NSEC_PER_MSEC
,
228 .sched_deadline
= 10 * NSEC_PER_MSEC
,
229 .sched_period
= 10 * NSEC_PER_MSEC
,
233 if (fie_disabled
!= FIE_ENABLED
&& fie_disabled
!= FIE_DISABLED
) {
234 fie_disabled
= FIE_ENABLED
;
235 if (cppc_perf_ctrs_in_pcc()) {
236 pr_info("FIE not enabled on systems with registers in PCC\n");
237 fie_disabled
= FIE_DISABLED
;
244 kworker_fie
= kthread_create_worker(0, "cppc_fie");
245 if (IS_ERR(kworker_fie
)) {
246 pr_warn("%s: failed to create kworker_fie: %ld\n", __func__
,
247 PTR_ERR(kworker_fie
));
248 fie_disabled
= FIE_DISABLED
;
252 ret
= sched_setattr_nocheck(kworker_fie
->task
, &attr
);
254 pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__
,
256 kthread_destroy_worker(kworker_fie
);
257 fie_disabled
= FIE_DISABLED
;
261 static void cppc_freq_invariance_exit(void)
266 kthread_destroy_worker(kworker_fie
);
270 static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy
*policy
)
274 static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy
*policy
)
278 static inline void cppc_freq_invariance_init(void)
282 static inline void cppc_freq_invariance_exit(void)
285 #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
287 static int cppc_cpufreq_set_target(struct cpufreq_policy
*policy
,
288 unsigned int target_freq
,
289 unsigned int relation
)
291 struct cppc_cpudata
*cpu_data
= policy
->driver_data
;
292 unsigned int cpu
= policy
->cpu
;
293 struct cpufreq_freqs freqs
;
296 cpu_data
->perf_ctrls
.desired_perf
=
297 cppc_khz_to_perf(&cpu_data
->perf_caps
, target_freq
);
298 freqs
.old
= policy
->cur
;
299 freqs
.new = target_freq
;
301 cpufreq_freq_transition_begin(policy
, &freqs
);
302 ret
= cppc_set_perf(cpu
, &cpu_data
->perf_ctrls
);
303 cpufreq_freq_transition_end(policy
, &freqs
, ret
!= 0);
306 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
312 static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy
*policy
,
313 unsigned int target_freq
)
315 struct cppc_cpudata
*cpu_data
= policy
->driver_data
;
316 unsigned int cpu
= policy
->cpu
;
320 desired_perf
= cppc_khz_to_perf(&cpu_data
->perf_caps
, target_freq
);
321 cpu_data
->perf_ctrls
.desired_perf
= desired_perf
;
322 ret
= cppc_set_perf(cpu
, &cpu_data
->perf_ctrls
);
325 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
333 static int cppc_verify_policy(struct cpufreq_policy_data
*policy
)
335 cpufreq_verify_within_cpu_limits(policy
);
340 * The PCC subspace describes the rate at which platform can accept commands
341 * on the shared PCC channel (including READs which do not count towards freq
342 * transition requests), so ideally we need to use the PCC values as a fallback
343 * if we don't have a platform specific transition_delay_us
346 #include <asm/cputype.h>
348 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu
)
350 unsigned long implementor
= read_cpuid_implementor();
351 unsigned long part_num
= read_cpuid_part_number();
353 switch (implementor
) {
354 case ARM_CPU_IMP_QCOM
:
356 case QCOM_CPU_PART_FALKOR_V1
:
357 case QCOM_CPU_PART_FALKOR
:
361 return cppc_get_transition_latency(cpu
) / NSEC_PER_USEC
;
364 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu
)
366 return cppc_get_transition_latency(cpu
) / NSEC_PER_USEC
;
370 #if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL)
372 static DEFINE_PER_CPU(unsigned int, efficiency_class
);
373 static void cppc_cpufreq_register_em(struct cpufreq_policy
*policy
);
375 /* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */
376 #define CPPC_EM_CAP_STEP (20)
377 /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
378 #define CPPC_EM_COST_STEP (1)
379 /* Add a cost gap correspnding to the energy of 4 CPUs. */
380 #define CPPC_EM_COST_GAP (4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
383 static unsigned int get_perf_level_count(struct cpufreq_policy
*policy
)
385 struct cppc_perf_caps
*perf_caps
;
386 unsigned int min_cap
, max_cap
;
387 struct cppc_cpudata
*cpu_data
;
388 int cpu
= policy
->cpu
;
390 cpu_data
= policy
->driver_data
;
391 perf_caps
= &cpu_data
->perf_caps
;
392 max_cap
= arch_scale_cpu_capacity(cpu
);
393 min_cap
= div_u64((u64
)max_cap
* perf_caps
->lowest_perf
,
394 perf_caps
->highest_perf
);
395 if ((min_cap
== 0) || (max_cap
< min_cap
))
397 return 1 + max_cap
/ CPPC_EM_CAP_STEP
- min_cap
/ CPPC_EM_CAP_STEP
;
401 * The cost is defined as:
402 * cost = power * max_frequency / frequency
404 static inline unsigned long compute_cost(int cpu
, int step
)
406 return CPPC_EM_COST_GAP
* per_cpu(efficiency_class
, cpu
) +
407 step
* CPPC_EM_COST_STEP
;
410 static int cppc_get_cpu_power(struct device
*cpu_dev
,
411 unsigned long *power
, unsigned long *KHz
)
413 unsigned long perf_step
, perf_prev
, perf
, perf_check
;
414 unsigned int min_step
, max_step
, step
, step_check
;
415 unsigned long prev_freq
= *KHz
;
416 unsigned int min_cap
, max_cap
;
417 struct cpufreq_policy
*policy
;
419 struct cppc_perf_caps
*perf_caps
;
420 struct cppc_cpudata
*cpu_data
;
422 policy
= cpufreq_cpu_get_raw(cpu_dev
->id
);
423 cpu_data
= policy
->driver_data
;
424 perf_caps
= &cpu_data
->perf_caps
;
425 max_cap
= arch_scale_cpu_capacity(cpu_dev
->id
);
426 min_cap
= div_u64((u64
)max_cap
* perf_caps
->lowest_perf
,
427 perf_caps
->highest_perf
);
428 perf_step
= div_u64((u64
)CPPC_EM_CAP_STEP
* perf_caps
->highest_perf
,
430 min_step
= min_cap
/ CPPC_EM_CAP_STEP
;
431 max_step
= max_cap
/ CPPC_EM_CAP_STEP
;
433 perf_prev
= cppc_khz_to_perf(perf_caps
, *KHz
);
434 step
= perf_prev
/ perf_step
;
439 if (min_step
== max_step
) {
441 perf
= perf_caps
->highest_perf
;
442 } else if (step
< min_step
) {
444 perf
= perf_caps
->lowest_perf
;
447 if (step
== max_step
)
448 perf
= perf_caps
->highest_perf
;
450 perf
= step
* perf_step
;
453 *KHz
= cppc_perf_to_khz(perf_caps
, perf
);
454 perf_check
= cppc_khz_to_perf(perf_caps
, *KHz
);
455 step_check
= perf_check
/ perf_step
;
458 * To avoid bad integer approximation, check that new frequency value
459 * increased and that the new frequency will be converted to the
460 * desired step value.
462 while ((*KHz
== prev_freq
) || (step_check
!= step
)) {
464 *KHz
= cppc_perf_to_khz(perf_caps
, perf
);
465 perf_check
= cppc_khz_to_perf(perf_caps
, *KHz
);
466 step_check
= perf_check
/ perf_step
;
470 * With an artificial EM, only the cost value is used. Still the power
471 * is populated such as 0 < power < EM_MAX_POWER. This allows to add
472 * more sense to the artificial performance states.
474 *power
= compute_cost(cpu_dev
->id
, step
);
479 static int cppc_get_cpu_cost(struct device
*cpu_dev
, unsigned long KHz
,
482 unsigned long perf_step
, perf_prev
;
483 struct cppc_perf_caps
*perf_caps
;
484 struct cpufreq_policy
*policy
;
485 struct cppc_cpudata
*cpu_data
;
486 unsigned int max_cap
;
489 policy
= cpufreq_cpu_get_raw(cpu_dev
->id
);
490 cpu_data
= policy
->driver_data
;
491 perf_caps
= &cpu_data
->perf_caps
;
492 max_cap
= arch_scale_cpu_capacity(cpu_dev
->id
);
494 perf_prev
= cppc_khz_to_perf(perf_caps
, KHz
);
495 perf_step
= CPPC_EM_CAP_STEP
* perf_caps
->highest_perf
/ max_cap
;
496 step
= perf_prev
/ perf_step
;
498 *cost
= compute_cost(cpu_dev
->id
, step
);
503 static int populate_efficiency_class(void)
505 struct acpi_madt_generic_interrupt
*gicc
;
506 DECLARE_BITMAP(used_classes
, 256) = {};
507 int class, cpu
, index
;
509 for_each_possible_cpu(cpu
) {
510 gicc
= acpi_cpu_get_madt_gicc(cpu
);
511 class = gicc
->efficiency_class
;
512 bitmap_set(used_classes
, class, 1);
515 if (bitmap_weight(used_classes
, 256) <= 1) {
516 pr_debug("Efficiency classes are all equal (=%d). "
517 "No EM registered", class);
522 * Squeeze efficiency class values on [0:#efficiency_class-1].
523 * Values are per spec in [0:255].
526 for_each_set_bit(class, used_classes
, 256) {
527 for_each_possible_cpu(cpu
) {
528 gicc
= acpi_cpu_get_madt_gicc(cpu
);
529 if (gicc
->efficiency_class
== class)
530 per_cpu(efficiency_class
, cpu
) = index
;
534 cppc_cpufreq_driver
.register_em
= cppc_cpufreq_register_em
;
539 static void cppc_cpufreq_register_em(struct cpufreq_policy
*policy
)
541 struct cppc_cpudata
*cpu_data
;
542 struct em_data_callback em_cb
=
543 EM_ADV_DATA_CB(cppc_get_cpu_power
, cppc_get_cpu_cost
);
545 cpu_data
= policy
->driver_data
;
546 em_dev_register_perf_domain(get_cpu_device(policy
->cpu
),
547 get_perf_level_count(policy
), &em_cb
,
548 cpu_data
->shared_cpu_map
, 0);
552 static int populate_efficiency_class(void)
558 static struct cppc_cpudata
*cppc_cpufreq_get_cpu_data(unsigned int cpu
)
560 struct cppc_cpudata
*cpu_data
;
563 cpu_data
= kzalloc(sizeof(struct cppc_cpudata
), GFP_KERNEL
);
567 if (!zalloc_cpumask_var(&cpu_data
->shared_cpu_map
, GFP_KERNEL
))
570 ret
= acpi_get_psd_map(cpu
, cpu_data
);
572 pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu
, ret
);
576 ret
= cppc_get_perf_caps(cpu
, &cpu_data
->perf_caps
);
578 pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu
, ret
);
582 list_add(&cpu_data
->node
, &cpu_data_list
);
587 free_cpumask_var(cpu_data
->shared_cpu_map
);
594 static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy
*policy
)
596 struct cppc_cpudata
*cpu_data
= policy
->driver_data
;
598 list_del(&cpu_data
->node
);
599 free_cpumask_var(cpu_data
->shared_cpu_map
);
601 policy
->driver_data
= NULL
;
604 static int cppc_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
606 unsigned int cpu
= policy
->cpu
;
607 struct cppc_cpudata
*cpu_data
;
608 struct cppc_perf_caps
*caps
;
611 cpu_data
= cppc_cpufreq_get_cpu_data(cpu
);
613 pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu
);
616 caps
= &cpu_data
->perf_caps
;
617 policy
->driver_data
= cpu_data
;
620 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
621 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
623 policy
->min
= cppc_perf_to_khz(caps
, caps
->lowest_nonlinear_perf
);
624 policy
->max
= cppc_perf_to_khz(caps
, caps
->nominal_perf
);
627 * Set cpuinfo.min_freq to Lowest to make the full range of performance
628 * available if userspace wants to use any perf between lowest & lowest
631 policy
->cpuinfo
.min_freq
= cppc_perf_to_khz(caps
, caps
->lowest_perf
);
632 policy
->cpuinfo
.max_freq
= cppc_perf_to_khz(caps
, caps
->nominal_perf
);
634 policy
->transition_delay_us
= cppc_cpufreq_get_transition_delay_us(cpu
);
635 policy
->shared_type
= cpu_data
->shared_type
;
637 switch (policy
->shared_type
) {
638 case CPUFREQ_SHARED_TYPE_HW
:
639 case CPUFREQ_SHARED_TYPE_NONE
:
640 /* Nothing to be done - we'll have a policy for each CPU */
642 case CPUFREQ_SHARED_TYPE_ANY
:
644 * All CPUs in the domain will share a policy and all cpufreq
645 * operations will use a single cppc_cpudata structure stored
646 * in policy->driver_data.
648 cpumask_copy(policy
->cpus
, cpu_data
->shared_cpu_map
);
651 pr_debug("Unsupported CPU co-ord type: %d\n",
652 policy
->shared_type
);
657 policy
->fast_switch_possible
= cppc_allow_fast_switch();
658 policy
->dvfs_possible_from_any_cpu
= true;
661 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
664 if (caps
->highest_perf
> caps
->nominal_perf
)
665 boost_supported
= true;
667 /* Set policy->cur to max now. The governors will adjust later. */
668 policy
->cur
= cppc_perf_to_khz(caps
, caps
->highest_perf
);
669 cpu_data
->perf_ctrls
.desired_perf
= caps
->highest_perf
;
671 ret
= cppc_set_perf(cpu
, &cpu_data
->perf_ctrls
);
673 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
674 caps
->highest_perf
, cpu
, ret
);
678 cppc_cpufreq_cpu_fie_init(policy
);
682 cppc_cpufreq_put_cpu_data(policy
);
686 static void cppc_cpufreq_cpu_exit(struct cpufreq_policy
*policy
)
688 struct cppc_cpudata
*cpu_data
= policy
->driver_data
;
689 struct cppc_perf_caps
*caps
= &cpu_data
->perf_caps
;
690 unsigned int cpu
= policy
->cpu
;
693 cppc_cpufreq_cpu_fie_exit(policy
);
695 cpu_data
->perf_ctrls
.desired_perf
= caps
->lowest_perf
;
697 ret
= cppc_set_perf(cpu
, &cpu_data
->perf_ctrls
);
699 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
700 caps
->lowest_perf
, cpu
, ret
);
702 cppc_cpufreq_put_cpu_data(policy
);
705 static inline u64
get_delta(u64 t1
, u64 t0
)
707 if (t1
> t0
|| t0
> ~(u32
)0)
710 return (u32
)t1
- (u32
)t0
;
713 static int cppc_perf_from_fbctrs(struct cppc_cpudata
*cpu_data
,
714 struct cppc_perf_fb_ctrs
*fb_ctrs_t0
,
715 struct cppc_perf_fb_ctrs
*fb_ctrs_t1
)
717 u64 delta_reference
, delta_delivered
;
720 reference_perf
= fb_ctrs_t0
->reference_perf
;
722 delta_reference
= get_delta(fb_ctrs_t1
->reference
,
723 fb_ctrs_t0
->reference
);
724 delta_delivered
= get_delta(fb_ctrs_t1
->delivered
,
725 fb_ctrs_t0
->delivered
);
727 /* Check to avoid divide-by zero and invalid delivered_perf */
728 if (!delta_reference
|| !delta_delivered
)
729 return cpu_data
->perf_ctrls
.desired_perf
;
731 return (reference_perf
* delta_delivered
) / delta_reference
;
734 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu
)
736 struct cppc_perf_fb_ctrs fb_ctrs_t0
= {0}, fb_ctrs_t1
= {0};
737 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
738 struct cppc_cpudata
*cpu_data
;
745 cpu_data
= policy
->driver_data
;
747 cpufreq_cpu_put(policy
);
749 ret
= cppc_get_perf_ctrs(cpu
, &fb_ctrs_t0
);
753 udelay(2); /* 2usec delay between sampling */
755 ret
= cppc_get_perf_ctrs(cpu
, &fb_ctrs_t1
);
759 delivered_perf
= cppc_perf_from_fbctrs(cpu_data
, &fb_ctrs_t0
,
762 return cppc_perf_to_khz(&cpu_data
->perf_caps
, delivered_perf
);
765 static int cppc_cpufreq_set_boost(struct cpufreq_policy
*policy
, int state
)
767 struct cppc_cpudata
*cpu_data
= policy
->driver_data
;
768 struct cppc_perf_caps
*caps
= &cpu_data
->perf_caps
;
771 if (!boost_supported
) {
772 pr_err("BOOST not supported by CPU or firmware\n");
777 policy
->max
= cppc_perf_to_khz(caps
, caps
->highest_perf
);
779 policy
->max
= cppc_perf_to_khz(caps
, caps
->nominal_perf
);
780 policy
->cpuinfo
.max_freq
= policy
->max
;
782 ret
= freq_qos_update_request(policy
->max_freq_req
, policy
->max
);
789 static ssize_t
show_freqdomain_cpus(struct cpufreq_policy
*policy
, char *buf
)
791 struct cppc_cpudata
*cpu_data
= policy
->driver_data
;
793 return cpufreq_show_cpus(cpu_data
->shared_cpu_map
, buf
);
795 cpufreq_freq_attr_ro(freqdomain_cpus
);
797 static struct freq_attr
*cppc_cpufreq_attr
[] = {
802 static struct cpufreq_driver cppc_cpufreq_driver
= {
803 .flags
= CPUFREQ_CONST_LOOPS
,
804 .verify
= cppc_verify_policy
,
805 .target
= cppc_cpufreq_set_target
,
806 .get
= cppc_cpufreq_get_rate
,
807 .fast_switch
= cppc_cpufreq_fast_switch
,
808 .init
= cppc_cpufreq_cpu_init
,
809 .exit
= cppc_cpufreq_cpu_exit
,
810 .set_boost
= cppc_cpufreq_set_boost
,
811 .attr
= cppc_cpufreq_attr
,
812 .name
= "cppc_cpufreq",
816 * HISI platform does not support delivered performance counter and
817 * reference performance counter. It can calculate the performance using the
818 * platform specific mechanism. We reuse the desired performance register to
819 * store the real performance calculated by the platform.
821 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu
)
823 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
824 struct cppc_cpudata
*cpu_data
;
831 cpu_data
= policy
->driver_data
;
833 cpufreq_cpu_put(policy
);
835 ret
= cppc_get_desired_perf(cpu
, &desired_perf
);
839 return cppc_perf_to_khz(&cpu_data
->perf_caps
, desired_perf
);
842 static void cppc_check_hisi_workaround(void)
844 struct acpi_table_header
*tbl
;
845 acpi_status status
= AE_OK
;
848 status
= acpi_get_table(ACPI_SIG_PCCT
, 0, &tbl
);
849 if (ACPI_FAILURE(status
) || !tbl
)
852 for (i
= 0; i
< ARRAY_SIZE(wa_info
); i
++) {
853 if (!memcmp(wa_info
[i
].oem_id
, tbl
->oem_id
, ACPI_OEM_ID_SIZE
) &&
854 !memcmp(wa_info
[i
].oem_table_id
, tbl
->oem_table_id
, ACPI_OEM_TABLE_ID_SIZE
) &&
855 wa_info
[i
].oem_revision
== tbl
->oem_revision
) {
856 /* Overwrite the get() callback */
857 cppc_cpufreq_driver
.get
= hisi_cppc_cpufreq_get_rate
;
858 fie_disabled
= FIE_DISABLED
;
866 static int __init
cppc_cpufreq_init(void)
870 if (!acpi_cpc_valid())
873 cppc_check_hisi_workaround();
874 cppc_freq_invariance_init();
875 populate_efficiency_class();
877 ret
= cpufreq_register_driver(&cppc_cpufreq_driver
);
879 cppc_freq_invariance_exit();
884 static inline void free_cpu_data(void)
886 struct cppc_cpudata
*iter
, *tmp
;
888 list_for_each_entry_safe(iter
, tmp
, &cpu_data_list
, node
) {
889 free_cpumask_var(iter
->shared_cpu_map
);
890 list_del(&iter
->node
);
896 static void __exit
cppc_cpufreq_exit(void)
898 cpufreq_unregister_driver(&cppc_cpufreq_driver
);
899 cppc_freq_invariance_exit();
904 module_exit(cppc_cpufreq_exit
);
905 MODULE_AUTHOR("Ashwin Chaugule");
906 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
907 MODULE_LICENSE("GPL");
909 late_initcall(cppc_cpufreq_init
);
911 static const struct acpi_device_id cppc_acpi_ids
[] __used
= {
912 {ACPI_PROCESSOR_DEVICE_HID
, },
916 MODULE_DEVICE_TABLE(acpi
, cppc_acpi_ids
);