xref: /linux/drivers/cpufreq/cppc_cpufreq.c (revision 3e0f897fd92662f0ff21ca1759d724a9ad574858)
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/kernel.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/cpu.h>
17 #include <linux/cpufreq.h>
18 #include <linux/dmi.h>
19 #include <linux/time.h>
20 #include <linux/vmalloc.h>
21 
22 #include <asm/unaligned.h>
23 
24 #include <acpi/cppc_acpi.h>
25 
26 /* Minimum struct length needed for the DMI processor entry we want */
27 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH	48
28 
29 /* Offset in the DMI processor structure for the max frequency */
30 #define DMI_PROCESSOR_MAX_SPEED		0x14
31 
32 /*
33  * This list contains information parsed from per CPU ACPI _CPC and _PSD
34  * structures: e.g. the highest and lowest supported performance, capabilities,
35  * desired performance, level requested etc. Depending on the share_type, not
36  * all CPUs will have an entry in the list.
37  */
38 static LIST_HEAD(cpu_data_list);
39 
40 static bool boost_supported;
41 
42 struct cppc_workaround_oem_info {
43 	char oem_id[ACPI_OEM_ID_SIZE + 1];
44 	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
45 	u32 oem_revision;
46 };
47 
48 static struct cppc_workaround_oem_info wa_info[] = {
49 	{
50 		.oem_id		= "HISI  ",
51 		.oem_table_id	= "HIP07   ",
52 		.oem_revision	= 0,
53 	}, {
54 		.oem_id		= "HISI  ",
55 		.oem_table_id	= "HIP08   ",
56 		.oem_revision	= 0,
57 	}
58 };
59 
60 /* Callback function used to retrieve the max frequency from DMI */
61 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
62 {
63 	const u8 *dmi_data = (const u8 *)dm;
64 	u16 *mhz = (u16 *)private;
65 
66 	if (dm->type == DMI_ENTRY_PROCESSOR &&
67 	    dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
68 		u16 val = (u16)get_unaligned((const u16 *)
69 				(dmi_data + DMI_PROCESSOR_MAX_SPEED));
70 		*mhz = val > *mhz ? val : *mhz;
71 	}
72 }
73 
74 /* Look up the max frequency in DMI */
75 static u64 cppc_get_dmi_max_khz(void)
76 {
77 	u16 mhz = 0;
78 
79 	dmi_walk(cppc_find_dmi_mhz, &mhz);
80 
81 	/*
82 	 * Real stupid fallback value, just in case there is no
83 	 * actual value set.
84 	 */
85 	mhz = mhz ? mhz : 1;
86 
87 	return (1000 * mhz);
88 }
89 
90 /*
91  * If CPPC lowest_freq and nominal_freq registers are exposed then we can
92  * use them to convert perf to freq and vice versa
93  *
94  * If the perf/freq point lies between Nominal and Lowest, we can treat
95  * (Low perf, Low freq) and (Nom Perf, Nom freq) as 2D co-ordinates of a line
96  * and extrapolate the rest
97  * For perf/freq > Nominal, we use the ratio perf:freq at Nominal for conversion
98  */
99 static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data,
100 					     unsigned int perf)
101 {
102 	struct cppc_perf_caps *caps = &cpu_data->perf_caps;
103 	static u64 max_khz;
104 	u64 mul, div;
105 
106 	if (caps->lowest_freq && caps->nominal_freq) {
107 		if (perf >= caps->nominal_perf) {
108 			mul = caps->nominal_freq;
109 			div = caps->nominal_perf;
110 		} else {
111 			mul = caps->nominal_freq - caps->lowest_freq;
112 			div = caps->nominal_perf - caps->lowest_perf;
113 		}
114 	} else {
115 		if (!max_khz)
116 			max_khz = cppc_get_dmi_max_khz();
117 		mul = max_khz;
118 		div = caps->highest_perf;
119 	}
120 	return (u64)perf * mul / div;
121 }
122 
123 static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data,
124 					     unsigned int freq)
125 {
126 	struct cppc_perf_caps *caps = &cpu_data->perf_caps;
127 	static u64 max_khz;
128 	u64  mul, div;
129 
130 	if (caps->lowest_freq && caps->nominal_freq) {
131 		if (freq >= caps->nominal_freq) {
132 			mul = caps->nominal_perf;
133 			div = caps->nominal_freq;
134 		} else {
135 			mul = caps->lowest_perf;
136 			div = caps->lowest_freq;
137 		}
138 	} else {
139 		if (!max_khz)
140 			max_khz = cppc_get_dmi_max_khz();
141 		mul = caps->highest_perf;
142 		div = max_khz;
143 	}
144 
145 	return (u64)freq * mul / div;
146 }
147 
148 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
149 				   unsigned int target_freq,
150 				   unsigned int relation)
151 
152 {
153 	struct cppc_cpudata *cpu_data = policy->driver_data;
154 	unsigned int cpu = policy->cpu;
155 	struct cpufreq_freqs freqs;
156 	u32 desired_perf;
157 	int ret = 0;
158 
159 	desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
160 	/* Return if it is exactly the same perf */
161 	if (desired_perf == cpu_data->perf_ctrls.desired_perf)
162 		return ret;
163 
164 	cpu_data->perf_ctrls.desired_perf = desired_perf;
165 	freqs.old = policy->cur;
166 	freqs.new = target_freq;
167 
168 	cpufreq_freq_transition_begin(policy, &freqs);
169 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
170 	cpufreq_freq_transition_end(policy, &freqs, ret != 0);
171 
172 	if (ret)
173 		pr_debug("Failed to set target on CPU:%d. ret:%d\n",
174 			 cpu, ret);
175 
176 	return ret;
177 }
178 
179 static int cppc_verify_policy(struct cpufreq_policy_data *policy)
180 {
181 	cpufreq_verify_within_cpu_limits(policy);
182 	return 0;
183 }
184 
185 /*
186  * The PCC subspace describes the rate at which platform can accept commands
187  * on the shared PCC channel (including READs which do not count towards freq
188  * transition requests), so ideally we need to use the PCC values as a fallback
189  * if we don't have a platform specific transition_delay_us
190  */
191 #ifdef CONFIG_ARM64
192 #include <asm/cputype.h>
193 
194 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
195 {
196 	unsigned long implementor = read_cpuid_implementor();
197 	unsigned long part_num = read_cpuid_part_number();
198 
199 	switch (implementor) {
200 	case ARM_CPU_IMP_QCOM:
201 		switch (part_num) {
202 		case QCOM_CPU_PART_FALKOR_V1:
203 		case QCOM_CPU_PART_FALKOR:
204 			return 10000;
205 		}
206 	}
207 	return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
208 }
209 
210 #else
211 
212 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
213 {
214 	return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
215 }
216 #endif
217 
218 
219 static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
220 {
221 	struct cppc_cpudata *cpu_data;
222 	int ret;
223 
224 	cpu_data = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
225 	if (!cpu_data)
226 		goto out;
227 
228 	if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
229 		goto free_cpu;
230 
231 	ret = acpi_get_psd_map(cpu, cpu_data);
232 	if (ret) {
233 		pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
234 		goto free_mask;
235 	}
236 
237 	ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
238 	if (ret) {
239 		pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
240 		goto free_mask;
241 	}
242 
243 	/* Convert the lowest and nominal freq from MHz to KHz */
244 	cpu_data->perf_caps.lowest_freq *= 1000;
245 	cpu_data->perf_caps.nominal_freq *= 1000;
246 
247 	list_add(&cpu_data->node, &cpu_data_list);
248 
249 	return cpu_data;
250 
251 free_mask:
252 	free_cpumask_var(cpu_data->shared_cpu_map);
253 free_cpu:
254 	kfree(cpu_data);
255 out:
256 	return NULL;
257 }
258 
259 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
260 {
261 	unsigned int cpu = policy->cpu;
262 	struct cppc_cpudata *cpu_data;
263 	struct cppc_perf_caps *caps;
264 	int ret;
265 
266 	cpu_data = cppc_cpufreq_get_cpu_data(cpu);
267 	if (!cpu_data) {
268 		pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
269 		return -ENODEV;
270 	}
271 	caps = &cpu_data->perf_caps;
272 	policy->driver_data = cpu_data;
273 
274 	/*
275 	 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
276 	 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
277 	 */
278 	policy->min = cppc_cpufreq_perf_to_khz(cpu_data,
279 					       caps->lowest_nonlinear_perf);
280 	policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
281 					       caps->nominal_perf);
282 
283 	/*
284 	 * Set cpuinfo.min_freq to Lowest to make the full range of performance
285 	 * available if userspace wants to use any perf between lowest & lowest
286 	 * nonlinear perf
287 	 */
288 	policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu_data,
289 							    caps->lowest_perf);
290 	policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu_data,
291 							    caps->nominal_perf);
292 
293 	policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
294 	policy->shared_type = cpu_data->shared_type;
295 
296 	switch (policy->shared_type) {
297 	case CPUFREQ_SHARED_TYPE_HW:
298 	case CPUFREQ_SHARED_TYPE_NONE:
299 		/* Nothing to be done - we'll have a policy for each CPU */
300 		break;
301 	case CPUFREQ_SHARED_TYPE_ANY:
302 		/*
303 		 * All CPUs in the domain will share a policy and all cpufreq
304 		 * operations will use a single cppc_cpudata structure stored
305 		 * in policy->driver_data.
306 		 */
307 		cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
308 		break;
309 	default:
310 		pr_debug("Unsupported CPU co-ord type: %d\n",
311 			 policy->shared_type);
312 		return -EFAULT;
313 	}
314 
315 	/*
316 	 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
317 	 * is supported.
318 	 */
319 	if (caps->highest_perf > caps->nominal_perf)
320 		boost_supported = true;
321 
322 	/* Set policy->cur to max now. The governors will adjust later. */
323 	policy->cur = cppc_cpufreq_perf_to_khz(cpu_data, caps->highest_perf);
324 	cpu_data->perf_ctrls.desired_perf =  caps->highest_perf;
325 
326 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
327 	if (ret)
328 		pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
329 			 caps->highest_perf, cpu, ret);
330 
331 	return ret;
332 }
333 
334 static int cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
335 {
336 	struct cppc_cpudata *cpu_data = policy->driver_data;
337 	struct cppc_perf_caps *caps = &cpu_data->perf_caps;
338 	unsigned int cpu = policy->cpu;
339 	int ret;
340 
341 	cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
342 
343 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
344 	if (ret)
345 		pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
346 			 caps->lowest_perf, cpu, ret);
347 
348 	/* Remove CPU node from list and free driver data for policy */
349 	free_cpumask_var(cpu_data->shared_cpu_map);
350 	list_del(&cpu_data->node);
351 	kfree(policy->driver_data);
352 	policy->driver_data = NULL;
353 
354 	return 0;
355 }
356 
357 static inline u64 get_delta(u64 t1, u64 t0)
358 {
359 	if (t1 > t0 || t0 > ~(u32)0)
360 		return t1 - t0;
361 
362 	return (u32)t1 - (u32)t0;
363 }
364 
365 static int cppc_get_rate_from_fbctrs(struct cppc_cpudata *cpu_data,
366 				     struct cppc_perf_fb_ctrs fb_ctrs_t0,
367 				     struct cppc_perf_fb_ctrs fb_ctrs_t1)
368 {
369 	u64 delta_reference, delta_delivered;
370 	u64 reference_perf, delivered_perf;
371 
372 	reference_perf = fb_ctrs_t0.reference_perf;
373 
374 	delta_reference = get_delta(fb_ctrs_t1.reference,
375 				    fb_ctrs_t0.reference);
376 	delta_delivered = get_delta(fb_ctrs_t1.delivered,
377 				    fb_ctrs_t0.delivered);
378 
379 	/* Check to avoid divide-by zero */
380 	if (delta_reference || delta_delivered)
381 		delivered_perf = (reference_perf * delta_delivered) /
382 					delta_reference;
383 	else
384 		delivered_perf = cpu_data->perf_ctrls.desired_perf;
385 
386 	return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
387 }
388 
389 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
390 {
391 	struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
392 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
393 	struct cppc_cpudata *cpu_data = policy->driver_data;
394 	int ret;
395 
396 	cpufreq_cpu_put(policy);
397 
398 	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
399 	if (ret)
400 		return ret;
401 
402 	udelay(2); /* 2usec delay between sampling */
403 
404 	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
405 	if (ret)
406 		return ret;
407 
408 	return cppc_get_rate_from_fbctrs(cpu_data, fb_ctrs_t0, fb_ctrs_t1);
409 }
410 
411 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
412 {
413 	struct cppc_cpudata *cpu_data = policy->driver_data;
414 	struct cppc_perf_caps *caps = &cpu_data->perf_caps;
415 	int ret;
416 
417 	if (!boost_supported) {
418 		pr_err("BOOST not supported by CPU or firmware\n");
419 		return -EINVAL;
420 	}
421 
422 	if (state)
423 		policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
424 						       caps->highest_perf);
425 	else
426 		policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
427 						       caps->nominal_perf);
428 	policy->cpuinfo.max_freq = policy->max;
429 
430 	ret = freq_qos_update_request(policy->max_freq_req, policy->max);
431 	if (ret < 0)
432 		return ret;
433 
434 	return 0;
435 }
436 
437 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
438 {
439 	struct cppc_cpudata *cpu_data = policy->driver_data;
440 
441 	return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
442 }
443 cpufreq_freq_attr_ro(freqdomain_cpus);
444 
445 static struct freq_attr *cppc_cpufreq_attr[] = {
446 	&freqdomain_cpus,
447 	NULL,
448 };
449 
450 static struct cpufreq_driver cppc_cpufreq_driver = {
451 	.flags = CPUFREQ_CONST_LOOPS,
452 	.verify = cppc_verify_policy,
453 	.target = cppc_cpufreq_set_target,
454 	.get = cppc_cpufreq_get_rate,
455 	.init = cppc_cpufreq_cpu_init,
456 	.exit = cppc_cpufreq_cpu_exit,
457 	.set_boost = cppc_cpufreq_set_boost,
458 	.attr = cppc_cpufreq_attr,
459 	.name = "cppc_cpufreq",
460 };
461 
462 /*
463  * HISI platform does not support delivered performance counter and
464  * reference performance counter. It can calculate the performance using the
465  * platform specific mechanism. We reuse the desired performance register to
466  * store the real performance calculated by the platform.
467  */
468 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
469 {
470 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
471 	struct cppc_cpudata *cpu_data = policy->driver_data;
472 	u64 desired_perf;
473 	int ret;
474 
475 	cpufreq_cpu_put(policy);
476 
477 	ret = cppc_get_desired_perf(cpu, &desired_perf);
478 	if (ret < 0)
479 		return -EIO;
480 
481 	return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
482 }
483 
484 static void cppc_check_hisi_workaround(void)
485 {
486 	struct acpi_table_header *tbl;
487 	acpi_status status = AE_OK;
488 	int i;
489 
490 	status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
491 	if (ACPI_FAILURE(status) || !tbl)
492 		return;
493 
494 	for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
495 		if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
496 		    !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
497 		    wa_info[i].oem_revision == tbl->oem_revision) {
498 			/* Overwrite the get() callback */
499 			cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
500 			break;
501 		}
502 	}
503 
504 	acpi_put_table(tbl);
505 }
506 
507 static int __init cppc_cpufreq_init(void)
508 {
509 	if ((acpi_disabled) || !acpi_cpc_valid())
510 		return -ENODEV;
511 
512 	INIT_LIST_HEAD(&cpu_data_list);
513 
514 	cppc_check_hisi_workaround();
515 
516 	return cpufreq_register_driver(&cppc_cpufreq_driver);
517 }
518 
519 static inline void free_cpu_data(void)
520 {
521 	struct cppc_cpudata *iter, *tmp;
522 
523 	list_for_each_entry_safe(iter, tmp, &cpu_data_list, node) {
524 		free_cpumask_var(iter->shared_cpu_map);
525 		list_del(&iter->node);
526 		kfree(iter);
527 	}
528 
529 }
530 
531 static void __exit cppc_cpufreq_exit(void)
532 {
533 	cpufreq_unregister_driver(&cppc_cpufreq_driver);
534 
535 	free_cpu_data();
536 }
537 
538 module_exit(cppc_cpufreq_exit);
539 MODULE_AUTHOR("Ashwin Chaugule");
540 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
541 MODULE_LICENSE("GPL");
542 
543 late_initcall(cppc_cpufreq_init);
544 
545 static const struct acpi_device_id cppc_acpi_ids[] __used = {
546 	{ACPI_PROCESSOR_DEVICE_HID, },
547 	{}
548 };
549 
550 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);
551