]> Git Repo - linux.git/blob - drivers/cpufreq/cppc_cpufreq.c
cppc_cpufreq: clean up cpu, cpu_num and cpunum variable use
[linux.git] / drivers / cpufreq / cppc_cpufreq.c
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 <[email protected]>
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  * These structs contain information parsed from per CPU
34  * ACPI _CPC structures.
35  * e.g. For each CPU the highest, lowest supported
36  * performance capabilities, desired performance level
37  * requested etc.
38  */
39 static struct cppc_cpudata **all_cpu_data;
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         struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
153         struct cpufreq_freqs freqs;
154         u32 desired_perf;
155         int ret = 0;
156
157         desired_perf = cppc_cpufreq_khz_to_perf(cpu_data, target_freq);
158         /* Return if it is exactly the same perf */
159         if (desired_perf == cpu_data->perf_ctrls.desired_perf)
160                 return ret;
161
162         cpu_data->perf_ctrls.desired_perf = desired_perf;
163         freqs.old = policy->cur;
164         freqs.new = target_freq;
165
166         cpufreq_freq_transition_begin(policy, &freqs);
167         ret = cppc_set_perf(cpu_data->cpu, &cpu_data->perf_ctrls);
168         cpufreq_freq_transition_end(policy, &freqs, ret != 0);
169
170         if (ret)
171                 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
172                          cpu_data->cpu, ret);
173
174         return ret;
175 }
176
177 static int cppc_verify_policy(struct cpufreq_policy_data *policy)
178 {
179         cpufreq_verify_within_cpu_limits(policy);
180         return 0;
181 }
182
183 static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
184 {
185         struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
186         unsigned int cpu = policy->cpu;
187         int ret;
188
189         cpu_data->perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
190
191         ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
192         if (ret)
193                 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
194                          cpu_data->perf_caps.lowest_perf, cpu, ret);
195 }
196
197 /*
198  * The PCC subspace describes the rate at which platform can accept commands
199  * on the shared PCC channel (including READs which do not count towards freq
200  * transition requests), so ideally we need to use the PCC values as a fallback
201  * if we don't have a platform specific transition_delay_us
202  */
203 #ifdef CONFIG_ARM64
204 #include <asm/cputype.h>
205
206 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
207 {
208         unsigned long implementor = read_cpuid_implementor();
209         unsigned long part_num = read_cpuid_part_number();
210         unsigned int delay_us = 0;
211
212         switch (implementor) {
213         case ARM_CPU_IMP_QCOM:
214                 switch (part_num) {
215                 case QCOM_CPU_PART_FALKOR_V1:
216                 case QCOM_CPU_PART_FALKOR:
217                         delay_us = 10000;
218                         break;
219                 default:
220                         delay_us = cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
221                         break;
222                 }
223                 break;
224         default:
225                 delay_us = cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
226                 break;
227         }
228
229         return delay_us;
230 }
231
232 #else
233
234 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
235 {
236         return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
237 }
238 #endif
239
240 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
241 {
242         struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
243         unsigned int cpu = policy->cpu;
244         int ret = 0;
245
246         cpu_data->cpu = cpu;
247         ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
248
249         if (ret) {
250                 pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
251                          cpu, ret);
252                 return ret;
253         }
254
255         /* Convert the lowest and nominal freq from MHz to KHz */
256         cpu_data->perf_caps.lowest_freq *= 1000;
257         cpu_data->perf_caps.nominal_freq *= 1000;
258
259         /*
260          * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
261          * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
262          */
263         policy->min = cppc_cpufreq_perf_to_khz(cpu_data, cpu_data->perf_caps.lowest_nonlinear_perf);
264         policy->max = cppc_cpufreq_perf_to_khz(cpu_data, cpu_data->perf_caps.nominal_perf);
265
266         /*
267          * Set cpuinfo.min_freq to Lowest to make the full range of performance
268          * available if userspace wants to use any perf between lowest & lowest
269          * nonlinear perf
270          */
271         policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu_data, cpu_data->perf_caps.lowest_perf);
272         policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu_data, cpu_data->perf_caps.nominal_perf);
273
274         policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
275         policy->shared_type = cpu_data->shared_type;
276
277         if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
278                 int i;
279
280                 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
281
282                 for_each_cpu(i, policy->cpus) {
283                         if (unlikely(i == cpu))
284                                 continue;
285
286                         memcpy(&all_cpu_data[i]->perf_caps, &cpu_data->perf_caps,
287                                sizeof(cpu_data->perf_caps));
288                 }
289         } else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
290                 /* Support only SW_ANY for now. */
291                 pr_debug("Unsupported CPU co-ord type\n");
292                 return -EFAULT;
293         }
294
295         cpu_data->cur_policy = policy;
296
297         /*
298          * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
299          * is supported.
300          */
301         if (cpu_data->perf_caps.highest_perf > cpu_data->perf_caps.nominal_perf)
302                 boost_supported = true;
303
304         /* Set policy->cur to max now. The governors will adjust later. */
305         policy->cur = cppc_cpufreq_perf_to_khz(cpu_data,
306                                         cpu_data->perf_caps.highest_perf);
307         cpu_data->perf_ctrls.desired_perf = cpu_data->perf_caps.highest_perf;
308
309         ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
310         if (ret)
311                 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
312                          cpu_data->perf_caps.highest_perf, cpu, ret);
313
314         return ret;
315 }
316
317 static inline u64 get_delta(u64 t1, u64 t0)
318 {
319         if (t1 > t0 || t0 > ~(u32)0)
320                 return t1 - t0;
321
322         return (u32)t1 - (u32)t0;
323 }
324
325 static int cppc_get_rate_from_fbctrs(struct cppc_cpudata *cpu_data,
326                                      struct cppc_perf_fb_ctrs fb_ctrs_t0,
327                                      struct cppc_perf_fb_ctrs fb_ctrs_t1)
328 {
329         u64 delta_reference, delta_delivered;
330         u64 reference_perf, delivered_perf;
331
332         reference_perf = fb_ctrs_t0.reference_perf;
333
334         delta_reference = get_delta(fb_ctrs_t1.reference,
335                                     fb_ctrs_t0.reference);
336         delta_delivered = get_delta(fb_ctrs_t1.delivered,
337                                     fb_ctrs_t0.delivered);
338
339         /* Check to avoid divide-by zero */
340         if (delta_reference || delta_delivered)
341                 delivered_perf = (reference_perf * delta_delivered) /
342                                         delta_reference;
343         else
344                 delivered_perf = cpu_data->perf_ctrls.desired_perf;
345
346         return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
347 }
348
349 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
350 {
351         struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
352         struct cppc_cpudata *cpu_data = all_cpu_data[cpu];
353         int ret;
354
355         ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
356         if (ret)
357                 return ret;
358
359         udelay(2); /* 2usec delay between sampling */
360
361         ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
362         if (ret)
363                 return ret;
364
365         return cppc_get_rate_from_fbctrs(cpu_data, fb_ctrs_t0, fb_ctrs_t1);
366 }
367
368 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
369 {
370         struct cppc_cpudata *cpu_data = all_cpu_data[policy->cpu];
371         int ret;
372
373         if (!boost_supported) {
374                 pr_err("BOOST not supported by CPU or firmware\n");
375                 return -EINVAL;
376         }
377
378         if (state)
379                 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
380                                         cpu_data->perf_caps.highest_perf);
381         else
382                 policy->max = cppc_cpufreq_perf_to_khz(cpu_data,
383                                         cpu_data->perf_caps.nominal_perf);
384         policy->cpuinfo.max_freq = policy->max;
385
386         ret = freq_qos_update_request(policy->max_freq_req, policy->max);
387         if (ret < 0)
388                 return ret;
389
390         return 0;
391 }
392
393 static struct cpufreq_driver cppc_cpufreq_driver = {
394         .flags = CPUFREQ_CONST_LOOPS,
395         .verify = cppc_verify_policy,
396         .target = cppc_cpufreq_set_target,
397         .get = cppc_cpufreq_get_rate,
398         .init = cppc_cpufreq_cpu_init,
399         .stop_cpu = cppc_cpufreq_stop_cpu,
400         .set_boost = cppc_cpufreq_set_boost,
401         .name = "cppc_cpufreq",
402 };
403
404 /*
405  * HISI platform does not support delivered performance counter and
406  * reference performance counter. It can calculate the performance using the
407  * platform specific mechanism. We reuse the desired performance register to
408  * store the real performance calculated by the platform.
409  */
410 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
411 {
412         struct cppc_cpudata *cpu_data = all_cpu_data[cpu];
413         u64 desired_perf;
414         int ret;
415
416         ret = cppc_get_desired_perf(cpu, &desired_perf);
417         if (ret < 0)
418                 return -EIO;
419
420         return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
421 }
422
423 static void cppc_check_hisi_workaround(void)
424 {
425         struct acpi_table_header *tbl;
426         acpi_status status = AE_OK;
427         int i;
428
429         status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
430         if (ACPI_FAILURE(status) || !tbl)
431                 return;
432
433         for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
434                 if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
435                     !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
436                     wa_info[i].oem_revision == tbl->oem_revision) {
437                         /* Overwrite the get() callback */
438                         cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
439                         break;
440                 }
441         }
442
443         acpi_put_table(tbl);
444 }
445
446 static int __init cppc_cpufreq_init(void)
447 {
448         struct cppc_cpudata *cpu_data;
449         int i, ret = 0;
450
451         if (acpi_disabled)
452                 return -ENODEV;
453
454         all_cpu_data = kcalloc(num_possible_cpus(), sizeof(void *),
455                                GFP_KERNEL);
456         if (!all_cpu_data)
457                 return -ENOMEM;
458
459         for_each_possible_cpu(i) {
460                 all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
461                 if (!all_cpu_data[i])
462                         goto out;
463
464                 cpu_data = all_cpu_data[i];
465                 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
466                         goto out;
467         }
468
469         ret = acpi_get_psd_map(all_cpu_data);
470         if (ret) {
471                 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
472                 goto out;
473         }
474
475         cppc_check_hisi_workaround();
476
477         ret = cpufreq_register_driver(&cppc_cpufreq_driver);
478         if (ret)
479                 goto out;
480
481         return ret;
482
483 out:
484         for_each_possible_cpu(i) {
485                 cpu_data = all_cpu_data[i];
486                 if (!cpu_data)
487                         break;
488                 free_cpumask_var(cpu_data->shared_cpu_map);
489                 kfree(cpu_data);
490         }
491
492         kfree(all_cpu_data);
493         return -ENODEV;
494 }
495
496 static void __exit cppc_cpufreq_exit(void)
497 {
498         struct cppc_cpudata *cpu_data;
499         int i;
500
501         cpufreq_unregister_driver(&cppc_cpufreq_driver);
502
503         for_each_possible_cpu(i) {
504                 cpu_data = all_cpu_data[i];
505                 free_cpumask_var(cpu_data->shared_cpu_map);
506                 kfree(cpu_data);
507         }
508
509         kfree(all_cpu_data);
510 }
511
512 module_exit(cppc_cpufreq_exit);
513 MODULE_AUTHOR("Ashwin Chaugule");
514 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
515 MODULE_LICENSE("GPL");
516
517 late_initcall(cppc_cpufreq_init);
518
519 static const struct acpi_device_id cppc_acpi_ids[] __used = {
520         {ACPI_PROCESSOR_DEVICE_HID, },
521         {}
522 };
523
524 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);
This page took 0.066506 seconds and 4 git commands to generate.