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