Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
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.
6 *
7 * (C) Copyright 2014, 2015 Linaro Ltd.
8 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
9 */
10
11#define pr_fmt(fmt) "CPPC Cpufreq:" fmt
12
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>
24
25#include <linux/unaligned.h>
26
27#include <acpi/cppc_acpi.h>
28
29static struct cpufreq_driver cppc_cpufreq_driver;
30
31#ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
32static enum {
33 FIE_UNSET = -1,
34 FIE_ENABLED,
35 FIE_DISABLED
36} fie_disabled = FIE_UNSET;
37
38module_param(fie_disabled, int, 0444);
39MODULE_PARM_DESC(fie_disabled, "Disable Frequency Invariance Engine (FIE)");
40
41/* Frequency invariance support */
42struct cppc_freq_invariance {
43 int cpu;
44 struct irq_work irq_work;
45 struct kthread_work work;
46 struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
47 struct cppc_cpudata *cpu_data;
48};
49
50static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
51static struct kthread_worker *kworker_fie;
52
53static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
54 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
55 struct cppc_perf_fb_ctrs *fb_ctrs_t1);
56
57/**
58 * cppc_scale_freq_workfn - CPPC arch_freq_scale updater for frequency invariance
59 * @work: The work item.
60 *
61 * The CPPC driver register itself with the topology core to provide its own
62 * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which
63 * gets called by the scheduler on every tick.
64 *
65 * Note that the arch specific counters have higher priority than CPPC counters,
66 * if available, though the CPPC driver doesn't need to have any special
67 * handling for that.
68 *
69 * On an invocation of cppc_scale_freq_tick(), we schedule an irq work (since we
70 * reach here from hard-irq context), which then schedules a normal work item
71 * and cppc_scale_freq_workfn() updates the per_cpu arch_freq_scale variable
72 * based on the counter updates since the last tick.
73 */
74static void cppc_scale_freq_workfn(struct kthread_work *work)
75{
76 struct cppc_freq_invariance *cppc_fi;
77 struct cppc_perf_fb_ctrs fb_ctrs = {0};
78 struct cppc_cpudata *cpu_data;
79 unsigned long local_freq_scale;
80 u64 perf;
81
82 cppc_fi = container_of(work, struct cppc_freq_invariance, work);
83 cpu_data = cppc_fi->cpu_data;
84
85 if (cppc_get_perf_ctrs(cppc_fi->cpu, &fb_ctrs)) {
86 pr_warn("%s: failed to read perf counters\n", __func__);
87 return;
88 }
89
90 perf = cppc_perf_from_fbctrs(cpu_data, &cppc_fi->prev_perf_fb_ctrs,
91 &fb_ctrs);
92 if (!perf)
93 return;
94
95 cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
96
97 perf <<= SCHED_CAPACITY_SHIFT;
98 local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf);
99
100 /* This can happen due to counter's overflow */
101 if (unlikely(local_freq_scale > 1024))
102 local_freq_scale = 1024;
103
104 per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale;
105}
106
107static void cppc_irq_work(struct irq_work *irq_work)
108{
109 struct cppc_freq_invariance *cppc_fi;
110
111 cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
112 kthread_queue_work(kworker_fie, &cppc_fi->work);
113}
114
115static void cppc_scale_freq_tick(void)
116{
117 struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id());
118
119 /*
120 * cppc_get_perf_ctrs() can potentially sleep, call that from the right
121 * context.
122 */
123 irq_work_queue(&cppc_fi->irq_work);
124}
125
126static struct scale_freq_data cppc_sftd = {
127 .source = SCALE_FREQ_SOURCE_CPPC,
128 .set_freq_scale = cppc_scale_freq_tick,
129};
130
131static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
132{
133 struct cppc_freq_invariance *cppc_fi;
134 int cpu, ret;
135
136 if (fie_disabled)
137 return;
138
139 for_each_cpu(cpu, policy->cpus) {
140 cppc_fi = &per_cpu(cppc_freq_inv, cpu);
141 cppc_fi->cpu = cpu;
142 cppc_fi->cpu_data = policy->driver_data;
143 kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn);
144 init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
145
146 ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
147 if (ret) {
148 pr_warn("%s: failed to read perf counters for cpu:%d: %d\n",
149 __func__, cpu, ret);
150
151 /*
152 * Don't abort if the CPU was offline while the driver
153 * was getting registered.
154 */
155 if (cpu_online(cpu))
156 return;
157 }
158 }
159
160 /* Register for freq-invariance */
161 topology_set_scale_freq_source(&cppc_sftd, policy->cpus);
162}
163
164/*
165 * We free all the resources on policy's removal and not on CPU removal as the
166 * irq-work are per-cpu and the hotplug core takes care of flushing the pending
167 * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work
168 * fires on another CPU after the concerned CPU is removed, it won't harm.
169 *
170 * We just need to make sure to remove them all on policy->exit().
171 */
172static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
173{
174 struct cppc_freq_invariance *cppc_fi;
175 int cpu;
176
177 if (fie_disabled)
178 return;
179
180 /* policy->cpus will be empty here, use related_cpus instead */
181 topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus);
182
183 for_each_cpu(cpu, policy->related_cpus) {
184 cppc_fi = &per_cpu(cppc_freq_inv, cpu);
185 irq_work_sync(&cppc_fi->irq_work);
186 kthread_cancel_work_sync(&cppc_fi->work);
187 }
188}
189
190static void __init cppc_freq_invariance_init(void)
191{
192 struct sched_attr attr = {
193 .size = sizeof(struct sched_attr),
194 .sched_policy = SCHED_DEADLINE,
195 .sched_nice = 0,
196 .sched_priority = 0,
197 /*
198 * Fake (unused) bandwidth; workaround to "fix"
199 * priority inheritance.
200 */
201 .sched_runtime = NSEC_PER_MSEC,
202 .sched_deadline = 10 * NSEC_PER_MSEC,
203 .sched_period = 10 * NSEC_PER_MSEC,
204 };
205 int ret;
206
207 if (fie_disabled != FIE_ENABLED && fie_disabled != FIE_DISABLED) {
208 fie_disabled = FIE_ENABLED;
209 if (cppc_perf_ctrs_in_pcc()) {
210 pr_info("FIE not enabled on systems with registers in PCC\n");
211 fie_disabled = FIE_DISABLED;
212 }
213 }
214
215 if (fie_disabled)
216 return;
217
218 kworker_fie = kthread_run_worker(0, "cppc_fie");
219 if (IS_ERR(kworker_fie)) {
220 pr_warn("%s: failed to create kworker_fie: %ld\n", __func__,
221 PTR_ERR(kworker_fie));
222 fie_disabled = FIE_DISABLED;
223 return;
224 }
225
226 ret = sched_setattr_nocheck(kworker_fie->task, &attr);
227 if (ret) {
228 pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__,
229 ret);
230 kthread_destroy_worker(kworker_fie);
231 fie_disabled = FIE_DISABLED;
232 }
233}
234
235static void cppc_freq_invariance_exit(void)
236{
237 if (fie_disabled)
238 return;
239
240 kthread_destroy_worker(kworker_fie);
241}
242
243#else
244static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
245{
246}
247
248static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
249{
250}
251
252static inline void cppc_freq_invariance_init(void)
253{
254}
255
256static inline void cppc_freq_invariance_exit(void)
257{
258}
259#endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
260
261static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
262 unsigned int target_freq,
263 unsigned int relation)
264{
265 struct cppc_cpudata *cpu_data = policy->driver_data;
266 unsigned int cpu = policy->cpu;
267 struct cpufreq_freqs freqs;
268 int ret = 0;
269
270 cpu_data->perf_ctrls.desired_perf =
271 cppc_khz_to_perf(&cpu_data->perf_caps, target_freq);
272 freqs.old = policy->cur;
273 freqs.new = target_freq;
274
275 cpufreq_freq_transition_begin(policy, &freqs);
276 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
277 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
278
279 if (ret)
280 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
281 cpu, ret);
282
283 return ret;
284}
285
286static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy *policy,
287 unsigned int target_freq)
288{
289 struct cppc_cpudata *cpu_data = policy->driver_data;
290 unsigned int cpu = policy->cpu;
291 u32 desired_perf;
292 int ret;
293
294 desired_perf = cppc_khz_to_perf(&cpu_data->perf_caps, target_freq);
295 cpu_data->perf_ctrls.desired_perf = desired_perf;
296 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
297
298 if (ret) {
299 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
300 cpu, ret);
301 return 0;
302 }
303
304 return target_freq;
305}
306
307static int cppc_verify_policy(struct cpufreq_policy_data *policy)
308{
309 cpufreq_verify_within_cpu_limits(policy);
310 return 0;
311}
312
313/*
314 * The PCC subspace describes the rate at which platform can accept commands
315 * on the shared PCC channel (including READs which do not count towards freq
316 * transition requests), so ideally we need to use the PCC values as a fallback
317 * if we don't have a platform specific transition_delay_us
318 */
319#ifdef CONFIG_ARM64
320#include <asm/cputype.h>
321
322static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
323{
324 unsigned long implementor = read_cpuid_implementor();
325 unsigned long part_num = read_cpuid_part_number();
326
327 switch (implementor) {
328 case ARM_CPU_IMP_QCOM:
329 switch (part_num) {
330 case QCOM_CPU_PART_FALKOR_V1:
331 case QCOM_CPU_PART_FALKOR:
332 return 10000;
333 }
334 }
335 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
336}
337#else
338static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
339{
340 return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
341}
342#endif
343
344#if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL)
345
346static DEFINE_PER_CPU(unsigned int, efficiency_class);
347
348/* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */
349#define CPPC_EM_CAP_STEP (20)
350/* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
351#define CPPC_EM_COST_STEP (1)
352/* Add a cost gap correspnding to the energy of 4 CPUs. */
353#define CPPC_EM_COST_GAP (4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
354 / CPPC_EM_CAP_STEP)
355
356static unsigned int get_perf_level_count(struct cpufreq_policy *policy)
357{
358 struct cppc_perf_caps *perf_caps;
359 unsigned int min_cap, max_cap;
360 struct cppc_cpudata *cpu_data;
361 int cpu = policy->cpu;
362
363 cpu_data = policy->driver_data;
364 perf_caps = &cpu_data->perf_caps;
365 max_cap = arch_scale_cpu_capacity(cpu);
366 min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
367 perf_caps->highest_perf);
368 if ((min_cap == 0) || (max_cap < min_cap))
369 return 0;
370 return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP;
371}
372
373/*
374 * The cost is defined as:
375 * cost = power * max_frequency / frequency
376 */
377static inline unsigned long compute_cost(int cpu, int step)
378{
379 return CPPC_EM_COST_GAP * per_cpu(efficiency_class, cpu) +
380 step * CPPC_EM_COST_STEP;
381}
382
383static int cppc_get_cpu_power(struct device *cpu_dev,
384 unsigned long *power, unsigned long *KHz)
385{
386 unsigned long perf_step, perf_prev, perf, perf_check;
387 unsigned int min_step, max_step, step, step_check;
388 unsigned long prev_freq = *KHz;
389 unsigned int min_cap, max_cap;
390 struct cpufreq_policy *policy;
391
392 struct cppc_perf_caps *perf_caps;
393 struct cppc_cpudata *cpu_data;
394
395 policy = cpufreq_cpu_get_raw(cpu_dev->id);
396 if (!policy)
397 return -EINVAL;
398
399 cpu_data = policy->driver_data;
400 perf_caps = &cpu_data->perf_caps;
401 max_cap = arch_scale_cpu_capacity(cpu_dev->id);
402 min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
403 perf_caps->highest_perf);
404 perf_step = div_u64((u64)CPPC_EM_CAP_STEP * perf_caps->highest_perf,
405 max_cap);
406 min_step = min_cap / CPPC_EM_CAP_STEP;
407 max_step = max_cap / CPPC_EM_CAP_STEP;
408
409 perf_prev = cppc_khz_to_perf(perf_caps, *KHz);
410 step = perf_prev / perf_step;
411
412 if (step > max_step)
413 return -EINVAL;
414
415 if (min_step == max_step) {
416 step = max_step;
417 perf = perf_caps->highest_perf;
418 } else if (step < min_step) {
419 step = min_step;
420 perf = perf_caps->lowest_perf;
421 } else {
422 step++;
423 if (step == max_step)
424 perf = perf_caps->highest_perf;
425 else
426 perf = step * perf_step;
427 }
428
429 *KHz = cppc_perf_to_khz(perf_caps, perf);
430 perf_check = cppc_khz_to_perf(perf_caps, *KHz);
431 step_check = perf_check / perf_step;
432
433 /*
434 * To avoid bad integer approximation, check that new frequency value
435 * increased and that the new frequency will be converted to the
436 * desired step value.
437 */
438 while ((*KHz == prev_freq) || (step_check != step)) {
439 perf++;
440 *KHz = cppc_perf_to_khz(perf_caps, perf);
441 perf_check = cppc_khz_to_perf(perf_caps, *KHz);
442 step_check = perf_check / perf_step;
443 }
444
445 /*
446 * With an artificial EM, only the cost value is used. Still the power
447 * is populated such as 0 < power < EM_MAX_POWER. This allows to add
448 * more sense to the artificial performance states.
449 */
450 *power = compute_cost(cpu_dev->id, step);
451
452 return 0;
453}
454
455static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
456 unsigned long *cost)
457{
458 unsigned long perf_step, perf_prev;
459 struct cppc_perf_caps *perf_caps;
460 struct cpufreq_policy *policy;
461 struct cppc_cpudata *cpu_data;
462 unsigned int max_cap;
463 int step;
464
465 policy = cpufreq_cpu_get_raw(cpu_dev->id);
466 if (!policy)
467 return -EINVAL;
468
469 cpu_data = policy->driver_data;
470 perf_caps = &cpu_data->perf_caps;
471 max_cap = arch_scale_cpu_capacity(cpu_dev->id);
472
473 perf_prev = cppc_khz_to_perf(perf_caps, KHz);
474 perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap;
475 step = perf_prev / perf_step;
476
477 *cost = compute_cost(cpu_dev->id, step);
478
479 return 0;
480}
481
482static void cppc_cpufreq_register_em(struct cpufreq_policy *policy)
483{
484 struct cppc_cpudata *cpu_data;
485 struct em_data_callback em_cb =
486 EM_ADV_DATA_CB(cppc_get_cpu_power, cppc_get_cpu_cost);
487
488 cpu_data = policy->driver_data;
489 em_dev_register_perf_domain(get_cpu_device(policy->cpu),
490 get_perf_level_count(policy), &em_cb,
491 cpu_data->shared_cpu_map, 0);
492}
493
494static void populate_efficiency_class(void)
495{
496 struct acpi_madt_generic_interrupt *gicc;
497 DECLARE_BITMAP(used_classes, 256) = {};
498 int class, cpu, index;
499
500 for_each_possible_cpu(cpu) {
501 gicc = acpi_cpu_get_madt_gicc(cpu);
502 class = gicc->efficiency_class;
503 bitmap_set(used_classes, class, 1);
504 }
505
506 if (bitmap_weight(used_classes, 256) <= 1) {
507 pr_debug("Efficiency classes are all equal (=%d). "
508 "No EM registered", class);
509 return;
510 }
511
512 /*
513 * Squeeze efficiency class values on [0:#efficiency_class-1].
514 * Values are per spec in [0:255].
515 */
516 index = 0;
517 for_each_set_bit(class, used_classes, 256) {
518 for_each_possible_cpu(cpu) {
519 gicc = acpi_cpu_get_madt_gicc(cpu);
520 if (gicc->efficiency_class == class)
521 per_cpu(efficiency_class, cpu) = index;
522 }
523 index++;
524 }
525 cppc_cpufreq_driver.register_em = cppc_cpufreq_register_em;
526}
527
528#else
529static void populate_efficiency_class(void)
530{
531}
532#endif
533
534static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
535{
536 struct cppc_cpudata *cpu_data;
537 int ret;
538
539 cpu_data = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
540 if (!cpu_data)
541 goto out;
542
543 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
544 goto free_cpu;
545
546 ret = acpi_get_psd_map(cpu, cpu_data);
547 if (ret) {
548 pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
549 goto free_mask;
550 }
551
552 ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
553 if (ret) {
554 pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
555 goto free_mask;
556 }
557
558 return cpu_data;
559
560free_mask:
561 free_cpumask_var(cpu_data->shared_cpu_map);
562free_cpu:
563 kfree(cpu_data);
564out:
565 return NULL;
566}
567
568static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy)
569{
570 struct cppc_cpudata *cpu_data = policy->driver_data;
571
572 free_cpumask_var(cpu_data->shared_cpu_map);
573 kfree(cpu_data);
574 policy->driver_data = NULL;
575}
576
577static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
578{
579 unsigned int cpu = policy->cpu;
580 struct cppc_cpudata *cpu_data;
581 struct cppc_perf_caps *caps;
582 int ret;
583
584 cpu_data = cppc_cpufreq_get_cpu_data(cpu);
585 if (!cpu_data) {
586 pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
587 return -ENODEV;
588 }
589 caps = &cpu_data->perf_caps;
590 policy->driver_data = cpu_data;
591
592 /*
593 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
594 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
595 */
596 policy->min = cppc_perf_to_khz(caps, caps->lowest_nonlinear_perf);
597 policy->max = cppc_perf_to_khz(caps, policy->boost_enabled ?
598 caps->highest_perf : caps->nominal_perf);
599
600 /*
601 * Set cpuinfo.min_freq to Lowest to make the full range of performance
602 * available if userspace wants to use any perf between lowest & lowest
603 * nonlinear perf
604 */
605 policy->cpuinfo.min_freq = cppc_perf_to_khz(caps, caps->lowest_perf);
606 policy->cpuinfo.max_freq = policy->max;
607
608 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
609 policy->shared_type = cpu_data->shared_type;
610
611 switch (policy->shared_type) {
612 case CPUFREQ_SHARED_TYPE_HW:
613 case CPUFREQ_SHARED_TYPE_NONE:
614 /* Nothing to be done - we'll have a policy for each CPU */
615 break;
616 case CPUFREQ_SHARED_TYPE_ANY:
617 /*
618 * All CPUs in the domain will share a policy and all cpufreq
619 * operations will use a single cppc_cpudata structure stored
620 * in policy->driver_data.
621 */
622 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
623 break;
624 default:
625 pr_debug("Unsupported CPU co-ord type: %d\n",
626 policy->shared_type);
627 ret = -EFAULT;
628 goto out;
629 }
630
631 policy->fast_switch_possible = cppc_allow_fast_switch();
632 policy->dvfs_possible_from_any_cpu = true;
633
634 /*
635 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
636 * is supported.
637 */
638 if (caps->highest_perf > caps->nominal_perf)
639 policy->boost_supported = true;
640
641 /* Set policy->cur to max now. The governors will adjust later. */
642 policy->cur = cppc_perf_to_khz(caps, caps->highest_perf);
643 cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
644
645 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
646 if (ret) {
647 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
648 caps->highest_perf, cpu, ret);
649 goto out;
650 }
651
652 cppc_cpufreq_cpu_fie_init(policy);
653 return 0;
654
655out:
656 cppc_cpufreq_put_cpu_data(policy);
657 return ret;
658}
659
660static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
661{
662 struct cppc_cpudata *cpu_data = policy->driver_data;
663 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
664 unsigned int cpu = policy->cpu;
665 int ret;
666
667 cppc_cpufreq_cpu_fie_exit(policy);
668
669 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
670
671 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
672 if (ret)
673 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
674 caps->lowest_perf, cpu, ret);
675
676 cppc_cpufreq_put_cpu_data(policy);
677}
678
679static inline u64 get_delta(u64 t1, u64 t0)
680{
681 if (t1 > t0 || t0 > ~(u32)0)
682 return t1 - t0;
683
684 return (u32)t1 - (u32)t0;
685}
686
687static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
688 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
689 struct cppc_perf_fb_ctrs *fb_ctrs_t1)
690{
691 u64 delta_reference, delta_delivered;
692 u64 reference_perf;
693
694 reference_perf = fb_ctrs_t0->reference_perf;
695
696 delta_reference = get_delta(fb_ctrs_t1->reference,
697 fb_ctrs_t0->reference);
698 delta_delivered = get_delta(fb_ctrs_t1->delivered,
699 fb_ctrs_t0->delivered);
700
701 /*
702 * Avoid divide-by zero and unchanged feedback counters.
703 * Leave it for callers to handle.
704 */
705 if (!delta_reference || !delta_delivered)
706 return 0;
707
708 return (reference_perf * delta_delivered) / delta_reference;
709}
710
711static int cppc_get_perf_ctrs_sample(int cpu,
712 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
713 struct cppc_perf_fb_ctrs *fb_ctrs_t1)
714{
715 int ret;
716
717 ret = cppc_get_perf_ctrs(cpu, fb_ctrs_t0);
718 if (ret)
719 return ret;
720
721 udelay(2); /* 2usec delay between sampling */
722
723 return cppc_get_perf_ctrs(cpu, fb_ctrs_t1);
724}
725
726static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
727{
728 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
729 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
730 struct cppc_cpudata *cpu_data;
731 u64 delivered_perf;
732 int ret;
733
734 if (!policy)
735 return 0;
736
737 cpu_data = policy->driver_data;
738
739 cpufreq_cpu_put(policy);
740
741 ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1);
742 if (ret) {
743 if (ret == -EFAULT)
744 /* Any of the associated CPPC regs is 0. */
745 goto out_invalid_counters;
746 else
747 return 0;
748 }
749
750 delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
751 &fb_ctrs_t1);
752 if (!delivered_perf)
753 goto out_invalid_counters;
754
755 return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
756
757out_invalid_counters:
758 /*
759 * Feedback counters could be unchanged or 0 when a cpu enters a
760 * low-power idle state, e.g. clock-gated or power-gated.
761 * Use desired perf for reflecting frequency. Get the latest register
762 * value first as some platforms may update the actual delivered perf
763 * there; if failed, resort to the cached desired perf.
764 */
765 if (cppc_get_desired_perf(cpu, &delivered_perf))
766 delivered_perf = cpu_data->perf_ctrls.desired_perf;
767
768 return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
769}
770
771static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
772{
773 struct cppc_cpudata *cpu_data = policy->driver_data;
774 struct cppc_perf_caps *caps = &cpu_data->perf_caps;
775 int ret;
776
777 if (state)
778 policy->max = cppc_perf_to_khz(caps, caps->highest_perf);
779 else
780 policy->max = cppc_perf_to_khz(caps, caps->nominal_perf);
781 policy->cpuinfo.max_freq = policy->max;
782
783 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
784 if (ret < 0)
785 return ret;
786
787 return 0;
788}
789
790static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
791{
792 struct cppc_cpudata *cpu_data = policy->driver_data;
793
794 return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
795}
796
797static ssize_t show_auto_select(struct cpufreq_policy *policy, char *buf)
798{
799 bool val;
800 int ret;
801
802 ret = cppc_get_auto_sel(policy->cpu, &val);
803
804 /* show "<unsupported>" when this register is not supported by cpc */
805 if (ret == -EOPNOTSUPP)
806 return sysfs_emit(buf, "<unsupported>\n");
807
808 if (ret)
809 return ret;
810
811 return sysfs_emit(buf, "%d\n", val);
812}
813
814static ssize_t store_auto_select(struct cpufreq_policy *policy,
815 const char *buf, size_t count)
816{
817 bool val;
818 int ret;
819
820 ret = kstrtobool(buf, &val);
821 if (ret)
822 return ret;
823
824 ret = cppc_set_auto_sel(policy->cpu, val);
825 if (ret)
826 return ret;
827
828 return count;
829}
830
831static ssize_t show_auto_act_window(struct cpufreq_policy *policy, char *buf)
832{
833 u64 val;
834 int ret;
835
836 ret = cppc_get_auto_act_window(policy->cpu, &val);
837
838 /* show "<unsupported>" when this register is not supported by cpc */
839 if (ret == -EOPNOTSUPP)
840 return sysfs_emit(buf, "<unsupported>\n");
841
842 if (ret)
843 return ret;
844
845 return sysfs_emit(buf, "%llu\n", val);
846}
847
848static ssize_t store_auto_act_window(struct cpufreq_policy *policy,
849 const char *buf, size_t count)
850{
851 u64 usec;
852 int ret;
853
854 ret = kstrtou64(buf, 0, &usec);
855 if (ret)
856 return ret;
857
858 ret = cppc_set_auto_act_window(policy->cpu, usec);
859 if (ret)
860 return ret;
861
862 return count;
863}
864
865static ssize_t show_energy_performance_preference_val(struct cpufreq_policy *policy, char *buf)
866{
867 u64 val;
868 int ret;
869
870 ret = cppc_get_epp_perf(policy->cpu, &val);
871
872 /* show "<unsupported>" when this register is not supported by cpc */
873 if (ret == -EOPNOTSUPP)
874 return sysfs_emit(buf, "<unsupported>\n");
875
876 if (ret)
877 return ret;
878
879 return sysfs_emit(buf, "%llu\n", val);
880}
881
882static ssize_t store_energy_performance_preference_val(struct cpufreq_policy *policy,
883 const char *buf, size_t count)
884{
885 u64 val;
886 int ret;
887
888 ret = kstrtou64(buf, 0, &val);
889 if (ret)
890 return ret;
891
892 ret = cppc_set_epp(policy->cpu, val);
893 if (ret)
894 return ret;
895
896 return count;
897}
898
899cpufreq_freq_attr_ro(freqdomain_cpus);
900cpufreq_freq_attr_rw(auto_select);
901cpufreq_freq_attr_rw(auto_act_window);
902cpufreq_freq_attr_rw(energy_performance_preference_val);
903
904static struct freq_attr *cppc_cpufreq_attr[] = {
905 &freqdomain_cpus,
906 &auto_select,
907 &auto_act_window,
908 &energy_performance_preference_val,
909 NULL,
910};
911
912static struct cpufreq_driver cppc_cpufreq_driver = {
913 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
914 .verify = cppc_verify_policy,
915 .target = cppc_cpufreq_set_target,
916 .get = cppc_cpufreq_get_rate,
917 .fast_switch = cppc_cpufreq_fast_switch,
918 .init = cppc_cpufreq_cpu_init,
919 .exit = cppc_cpufreq_cpu_exit,
920 .set_boost = cppc_cpufreq_set_boost,
921 .attr = cppc_cpufreq_attr,
922 .name = "cppc_cpufreq",
923};
924
925static int __init cppc_cpufreq_init(void)
926{
927 int ret;
928
929 if (!acpi_cpc_valid())
930 return -ENODEV;
931
932 cppc_freq_invariance_init();
933 populate_efficiency_class();
934
935 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
936 if (ret)
937 cppc_freq_invariance_exit();
938
939 return ret;
940}
941
942static void __exit cppc_cpufreq_exit(void)
943{
944 cpufreq_unregister_driver(&cppc_cpufreq_driver);
945 cppc_freq_invariance_exit();
946}
947
948module_exit(cppc_cpufreq_exit);
949MODULE_AUTHOR("Ashwin Chaugule");
950MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
951MODULE_LICENSE("GPL");
952
953late_initcall(cppc_cpufreq_init);
954
955static const struct acpi_device_id cppc_acpi_ids[] __used = {
956 {ACPI_PROCESSOR_DEVICE_HID, },
957 {}
958};
959
960MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);