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