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