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;
66 #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
68 /* Frequency invariance support */
69 struct cppc_freq_invariance {
71 struct irq_work irq_work;
72 struct kthread_work work;
73 struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
74 struct cppc_cpudata *cpu_data;
77 static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
78 static struct kthread_worker *kworker_fie;
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 perf = cppc_perf_from_fbctrs(cpu_data, &cppc_fi->prev_perf_fb_ctrs,
120 cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
122 perf <<= SCHED_CAPACITY_SHIFT;
123 local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf);
125 /* This can happen due to counter's overflow */
126 if (unlikely(local_freq_scale > 1024))
127 local_freq_scale = 1024;
129 per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale;
132 static void cppc_irq_work(struct irq_work *irq_work)
134 struct cppc_freq_invariance *cppc_fi;
136 cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
137 kthread_queue_work(kworker_fie, &cppc_fi->work);
140 static void cppc_scale_freq_tick(void)
142 struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id());
145 * cppc_get_perf_ctrs() can potentially sleep, call that from the right
148 irq_work_queue(&cppc_fi->irq_work);
151 static struct scale_freq_data cppc_sftd = {
152 .source = SCALE_FREQ_SOURCE_CPPC,
153 .set_freq_scale = cppc_scale_freq_tick,
156 static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
158 struct cppc_freq_invariance *cppc_fi;
161 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
164 for_each_cpu(cpu, policy->cpus) {
165 cppc_fi = &per_cpu(cppc_freq_inv, cpu);
167 cppc_fi->cpu_data = policy->driver_data;
168 kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn);
169 init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
171 ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
173 pr_warn("%s: failed to read perf counters for cpu:%d: %d\n",
177 * Don't abort if the CPU was offline while the driver
178 * was getting registered.
185 /* Register for freq-invariance */
186 topology_set_scale_freq_source(&cppc_sftd, policy->cpus);
190 * We free all the resources on policy's removal and not on CPU removal as the
191 * irq-work are per-cpu and the hotplug core takes care of flushing the pending
192 * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work
193 * fires on another CPU after the concerned CPU is removed, it won't harm.
195 * We just need to make sure to remove them all on policy->exit().
197 static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
199 struct cppc_freq_invariance *cppc_fi;
202 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
205 /* policy->cpus will be empty here, use related_cpus instead */
206 topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus);
208 for_each_cpu(cpu, policy->related_cpus) {
209 cppc_fi = &per_cpu(cppc_freq_inv, cpu);
210 irq_work_sync(&cppc_fi->irq_work);
211 kthread_cancel_work_sync(&cppc_fi->work);
215 static void __init cppc_freq_invariance_init(void)
217 struct sched_attr attr = {
218 .size = sizeof(struct sched_attr),
219 .sched_policy = SCHED_DEADLINE,
223 * Fake (unused) bandwidth; workaround to "fix"
224 * priority inheritance.
226 .sched_runtime = 1000000,
227 .sched_deadline = 10000000,
228 .sched_period = 10000000,
232 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
235 kworker_fie = kthread_create_worker(0, "cppc_fie");
236 if (IS_ERR(kworker_fie))
239 ret = sched_setattr_nocheck(kworker_fie->task, &attr);
241 pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__,
243 kthread_destroy_worker(kworker_fie);
248 static void cppc_freq_invariance_exit(void)
250 if (cppc_cpufreq_driver.get == hisi_cppc_cpufreq_get_rate)
253 kthread_destroy_worker(kworker_fie);
258 static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
262 static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
266 static inline void cppc_freq_invariance_init(void)
270 static inline void cppc_freq_invariance_exit(void)
273 #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
275 /* Callback function used to retrieve the max frequency from DMI */
276 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
278 const u8 *dmi_data = (const u8 *)dm;
279 u16 *mhz = (u16 *)private;
281 if (dm->type == DMI_ENTRY_PROCESSOR &&
282 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
283 u16 val = (u16)get_unaligned((const u16 *)
284 (dmi_data + DMI_PROCESSOR_MAX_SPEED));
285 *mhz = val > *mhz ? val : *mhz;
289 /* Look up the max frequency in DMI */
290 static u64 cppc_get_dmi_max_khz(void)
294 dmi_walk(cppc_find_dmi_mhz, &mhz);
297 * Real stupid fallback value, just in case there is no
306 * If CPPC lowest_freq and nominal_freq registers are exposed then we can
307 * use them to convert perf to freq and vice versa. The conversion is
308 * extrapolated as an affine function passing by the 2 points:
309 * - (Low perf, Low freq)
310 * - (Nominal perf, Nominal perf)
312 static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data,
315 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
316 s64 retval, offset = 0;
320 if (caps->lowest_freq && caps->nominal_freq) {
321 mul = caps->nominal_freq - caps->lowest_freq;
322 div = caps->nominal_perf - caps->lowest_perf;
323 offset = caps->nominal_freq - div64_u64(caps->nominal_perf * mul, div);
326 max_khz = cppc_get_dmi_max_khz();
328 div = caps->highest_perf;
331 retval = offset + div64_u64(perf * mul, div);
337 static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data,
340 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
341 s64 retval, offset = 0;
345 if (caps->lowest_freq && caps->nominal_freq) {
346 mul = caps->nominal_perf - caps->lowest_perf;
347 div = caps->nominal_freq - caps->lowest_freq;
348 offset = caps->nominal_perf - div64_u64(caps->nominal_freq * mul, div);
351 max_khz = cppc_get_dmi_max_khz();
352 mul = caps->highest_perf;
356 retval = offset + div64_u64(freq * mul, div);
362 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
363 unsigned int target_freq,
364 unsigned int relation)
367 struct cppc_cpudata *cpu_data = policy->driver_data;
368 unsigned int cpu = policy->cpu;
369 struct cpufreq_freqs freqs;
373 desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
374 /* Return if it is exactly the same perf */
375 if (desired_perf == cpu_data->perf_ctrls.desired_perf)
378 cpu_data->perf_ctrls.desired_perf = desired_perf;
379 freqs.old = policy->cur;
380 freqs.new = target_freq;
382 cpufreq_freq_transition_begin(policy, &freqs);
383 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
384 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
387 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
393 static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy *policy,
394 unsigned int target_freq)
396 struct cppc_cpudata *cpu_data = policy->driver_data;
397 unsigned int cpu = policy->cpu;
401 desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
402 cpu_data->perf_ctrls.desired_perf = desired_perf;
403 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
406 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
414 static int cppc_verify_policy(struct cpufreq_policy_data *policy)
416 cpufreq_verify_within_cpu_limits(policy);
421 * The PCC subspace describes the rate at which platform can accept commands
422 * on the shared PCC channel (including READs which do not count towards freq
423 * transition requests), so ideally we need to use the PCC values as a fallback
424 * if we don't have a platform specific transition_delay_us
427 #include <asm/cputype.h>
429 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
431 unsigned long implementor = read_cpuid_implementor();
432 unsigned long part_num = read_cpuid_part_number();
434 switch (implementor) {
435 case ARM_CPU_IMP_QCOM:
437 case QCOM_CPU_PART_FALKOR_V1:
438 case QCOM_CPU_PART_FALKOR:
442 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
445 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
447 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
451 #if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL)
453 static DEFINE_PER_CPU(unsigned int, efficiency_class);
454 static void cppc_cpufreq_register_em(struct cpufreq_policy *policy);
456 /* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */
457 #define CPPC_EM_CAP_STEP (20)
458 /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
459 #define CPPC_EM_COST_STEP (1)
460 /* Add a cost gap correspnding to the energy of 4 CPUs. */
461 #define CPPC_EM_COST_GAP (4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
464 static unsigned int get_perf_level_count(struct cpufreq_policy *policy)
466 struct cppc_perf_caps *perf_caps;
467 unsigned int min_cap, max_cap;
468 struct cppc_cpudata *cpu_data;
469 int cpu = policy->cpu;
471 cpu_data = policy->driver_data;
472 perf_caps = &cpu_data->perf_caps;
473 max_cap = arch_scale_cpu_capacity(cpu);
474 min_cap = div_u64(max_cap * perf_caps->lowest_perf, perf_caps->highest_perf);
475 if ((min_cap == 0) || (max_cap < min_cap))
477 return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP;
481 * The cost is defined as:
482 * cost = power * max_frequency / frequency
484 static inline unsigned long compute_cost(int cpu, int step)
486 return CPPC_EM_COST_GAP * per_cpu(efficiency_class, cpu) +
487 step * CPPC_EM_COST_STEP;
490 static int cppc_get_cpu_power(struct device *cpu_dev,
491 unsigned long *power, unsigned long *KHz)
493 unsigned long perf_step, perf_prev, perf, perf_check;
494 unsigned int min_step, max_step, step, step_check;
495 unsigned long prev_freq = *KHz;
496 unsigned int min_cap, max_cap;
497 struct cpufreq_policy *policy;
499 struct cppc_perf_caps *perf_caps;
500 struct cppc_cpudata *cpu_data;
502 policy = cpufreq_cpu_get_raw(cpu_dev->id);
503 cpu_data = policy->driver_data;
504 perf_caps = &cpu_data->perf_caps;
505 max_cap = arch_scale_cpu_capacity(cpu_dev->id);
506 min_cap = div_u64(max_cap * perf_caps->lowest_perf,
507 perf_caps->highest_perf);
509 perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap;
510 min_step = min_cap / CPPC_EM_CAP_STEP;
511 max_step = max_cap / CPPC_EM_CAP_STEP;
513 perf_prev = cppc_cpufreq_khz_to_perf(cpu_data, *KHz);
514 step = perf_prev / perf_step;
519 if (min_step == max_step) {
521 perf = perf_caps->highest_perf;
522 } else if (step < min_step) {
524 perf = perf_caps->lowest_perf;
527 if (step == max_step)
528 perf = perf_caps->highest_perf;
530 perf = step * perf_step;
533 *KHz = cppc_cpufreq_perf_to_khz(cpu_data, perf);
534 perf_check = cppc_cpufreq_khz_to_perf(cpu_data, *KHz);
535 step_check = perf_check / perf_step;
538 * To avoid bad integer approximation, check that new frequency value
539 * increased and that the new frequency will be converted to the
540 * desired step value.
542 while ((*KHz == prev_freq) || (step_check != step)) {
544 *KHz = cppc_cpufreq_perf_to_khz(cpu_data, perf);
545 perf_check = cppc_cpufreq_khz_to_perf(cpu_data, *KHz);
546 step_check = perf_check / perf_step;
550 * With an artificial EM, only the cost value is used. Still the power
551 * is populated such as 0 < power < EM_MAX_POWER. This allows to add
552 * more sense to the artificial performance states.
554 *power = compute_cost(cpu_dev->id, step);
559 static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
562 unsigned long perf_step, perf_prev;
563 struct cppc_perf_caps *perf_caps;
564 struct cpufreq_policy *policy;
565 struct cppc_cpudata *cpu_data;
566 unsigned int max_cap;
569 policy = cpufreq_cpu_get_raw(cpu_dev->id);
570 cpu_data = policy->driver_data;
571 perf_caps = &cpu_data->perf_caps;
572 max_cap = arch_scale_cpu_capacity(cpu_dev->id);
574 perf_prev = cppc_cpufreq_khz_to_perf(cpu_data, KHz);
575 perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap;
576 step = perf_prev / perf_step;
578 *cost = compute_cost(cpu_dev->id, step);
583 static int populate_efficiency_class(void)
585 struct acpi_madt_generic_interrupt *gicc;
586 DECLARE_BITMAP(used_classes, 256) = {};
587 int class, cpu, index;
589 for_each_possible_cpu(cpu) {
590 gicc = acpi_cpu_get_madt_gicc(cpu);
591 class = gicc->efficiency_class;
592 bitmap_set(used_classes, class, 1);
595 if (bitmap_weight(used_classes, 256) <= 1) {
596 pr_debug("Efficiency classes are all equal (=%d). "
597 "No EM registered", class);
602 * Squeeze efficiency class values on [0:#efficiency_class-1].
603 * Values are per spec in [0:255].
606 for_each_set_bit(class, used_classes, 256) {
607 for_each_possible_cpu(cpu) {
608 gicc = acpi_cpu_get_madt_gicc(cpu);
609 if (gicc->efficiency_class == class)
610 per_cpu(efficiency_class, cpu) = index;
614 cppc_cpufreq_driver.register_em = cppc_cpufreq_register_em;
619 static void cppc_cpufreq_register_em(struct cpufreq_policy *policy)
621 struct cppc_cpudata *cpu_data;
622 struct em_data_callback em_cb =
623 EM_ADV_DATA_CB(cppc_get_cpu_power, cppc_get_cpu_cost);
625 cpu_data = policy->driver_data;
626 em_dev_register_perf_domain(get_cpu_device(policy->cpu),
627 get_perf_level_count(policy), &em_cb,
628 cpu_data->shared_cpu_map, 0);
632 static int populate_efficiency_class(void)
638 static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
640 struct cppc_cpudata *cpu_data;
643 cpu_data = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
647 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
650 ret = acpi_get_psd_map(cpu, cpu_data);
652 pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
656 ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
658 pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
662 /* Convert the lowest and nominal freq from MHz to KHz */
663 cpu_data->perf_caps.lowest_freq *= 1000;
664 cpu_data->perf_caps.nominal_freq *= 1000;
666 list_add(&cpu_data->node, &cpu_data_list);
671 free_cpumask_var(cpu_data->shared_cpu_map);
678 static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy)
680 struct cppc_cpudata *cpu_data = policy->driver_data;
682 list_del(&cpu_data->node);
683 free_cpumask_var(cpu_data->shared_cpu_map);
685 policy->driver_data = NULL;
688 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
690 unsigned int cpu = policy->cpu;
691 struct cppc_cpudata *cpu_data;
692 struct cppc_perf_caps *caps;
695 cpu_data = cppc_cpufreq_get_cpu_data(cpu);
697 pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
700 caps = &cpu_data->perf_caps;
701 policy->driver_data = cpu_data;
704 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
705 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
707 policy->min = cppc_cpufreq_perf_to_khz(cpu_data,
708 caps->lowest_nonlinear_perf);
709 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
713 * Set cpuinfo.min_freq to Lowest to make the full range of performance
714 * available if userspace wants to use any perf between lowest & lowest
717 policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu_data,
719 policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu_data,
722 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
723 policy->shared_type = cpu_data->shared_type;
725 switch (policy->shared_type) {
726 case CPUFREQ_SHARED_TYPE_HW:
727 case CPUFREQ_SHARED_TYPE_NONE:
728 /* Nothing to be done - we'll have a policy for each CPU */
730 case CPUFREQ_SHARED_TYPE_ANY:
732 * All CPUs in the domain will share a policy and all cpufreq
733 * operations will use a single cppc_cpudata structure stored
734 * in policy->driver_data.
736 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
739 pr_debug("Unsupported CPU co-ord type: %d\n",
740 policy->shared_type);
745 policy->fast_switch_possible = cppc_allow_fast_switch();
746 policy->dvfs_possible_from_any_cpu = true;
749 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
752 if (caps->highest_perf > caps->nominal_perf)
753 boost_supported = true;
755 /* Set policy->cur to max now. The governors will adjust later. */
756 policy->cur = cppc_cpufreq_perf_to_khz(cpu_data, caps->highest_perf);
757 cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
759 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
761 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
762 caps->highest_perf, cpu, ret);
766 cppc_cpufreq_cpu_fie_init(policy);
770 cppc_cpufreq_put_cpu_data(policy);
774 static int cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
776 struct cppc_cpudata *cpu_data = policy->driver_data;
777 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
778 unsigned int cpu = policy->cpu;
781 cppc_cpufreq_cpu_fie_exit(policy);
783 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
785 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
787 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
788 caps->lowest_perf, cpu, ret);
790 cppc_cpufreq_put_cpu_data(policy);
794 static inline u64 get_delta(u64 t1, u64 t0)
796 if (t1 > t0 || t0 > ~(u32)0)
799 return (u32)t1 - (u32)t0;
802 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
803 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
804 struct cppc_perf_fb_ctrs *fb_ctrs_t1)
806 u64 delta_reference, delta_delivered;
809 reference_perf = fb_ctrs_t0->reference_perf;
811 delta_reference = get_delta(fb_ctrs_t1->reference,
812 fb_ctrs_t0->reference);
813 delta_delivered = get_delta(fb_ctrs_t1->delivered,
814 fb_ctrs_t0->delivered);
816 /* Check to avoid divide-by zero and invalid delivered_perf */
817 if (!delta_reference || !delta_delivered)
818 return cpu_data->perf_ctrls.desired_perf;
820 return (reference_perf * delta_delivered) / delta_reference;
823 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
825 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
826 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
827 struct cppc_cpudata *cpu_data = policy->driver_data;
831 cpufreq_cpu_put(policy);
833 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
837 udelay(2); /* 2usec delay between sampling */
839 ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
843 delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
846 return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
849 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
851 struct cppc_cpudata *cpu_data = policy->driver_data;
852 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
855 if (!boost_supported) {
856 pr_err("BOOST not supported by CPU or firmware\n");
861 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
864 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
866 policy->cpuinfo.max_freq = policy->max;
868 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
875 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
877 struct cppc_cpudata *cpu_data = policy->driver_data;
879 return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
881 cpufreq_freq_attr_ro(freqdomain_cpus);
883 static struct freq_attr *cppc_cpufreq_attr[] = {
888 static struct cpufreq_driver cppc_cpufreq_driver = {
889 .flags = CPUFREQ_CONST_LOOPS,
890 .verify = cppc_verify_policy,
891 .target = cppc_cpufreq_set_target,
892 .get = cppc_cpufreq_get_rate,
893 .fast_switch = cppc_cpufreq_fast_switch,
894 .init = cppc_cpufreq_cpu_init,
895 .exit = cppc_cpufreq_cpu_exit,
896 .set_boost = cppc_cpufreq_set_boost,
897 .attr = cppc_cpufreq_attr,
898 .name = "cppc_cpufreq",
902 * HISI platform does not support delivered performance counter and
903 * reference performance counter. It can calculate the performance using the
904 * platform specific mechanism. We reuse the desired performance register to
905 * store the real performance calculated by the platform.
907 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
909 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
910 struct cppc_cpudata *cpu_data = policy->driver_data;
914 cpufreq_cpu_put(policy);
916 ret = cppc_get_desired_perf(cpu, &desired_perf);
920 return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
923 static void cppc_check_hisi_workaround(void)
925 struct acpi_table_header *tbl;
926 acpi_status status = AE_OK;
929 status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
930 if (ACPI_FAILURE(status) || !tbl)
933 for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
934 if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
935 !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
936 wa_info[i].oem_revision == tbl->oem_revision) {
937 /* Overwrite the get() callback */
938 cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
946 static int __init cppc_cpufreq_init(void)
950 if ((acpi_disabled) || !acpi_cpc_valid())
953 cppc_check_hisi_workaround();
954 cppc_freq_invariance_init();
955 populate_efficiency_class();
957 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
959 cppc_freq_invariance_exit();
964 static inline void free_cpu_data(void)
966 struct cppc_cpudata *iter, *tmp;
968 list_for_each_entry_safe(iter, tmp, &cpu_data_list, node) {
969 free_cpumask_var(iter->shared_cpu_map);
970 list_del(&iter->node);
976 static void __exit cppc_cpufreq_exit(void)
978 cpufreq_unregister_driver(&cppc_cpufreq_driver);
979 cppc_freq_invariance_exit();
984 module_exit(cppc_cpufreq_exit);
985 MODULE_AUTHOR("Ashwin Chaugule");
986 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
987 MODULE_LICENSE("GPL");
989 late_initcall(cppc_cpufreq_init);
991 static const struct acpi_device_id cppc_acpi_ids[] __used = {
992 {ACPI_PROCESSOR_DEVICE_HID, },
996 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);