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