blob: 38f65c371139c113ef741c670d7c3c8c2c405368 [file] [log] [blame]
Ashwin Chaugule5477fb32015-10-02 10:04:01 -04001/*
2 * CPPC (Collaborative Processor Performance Control) driver for
3 * interfacing with the CPUfreq layer and governors. See
4 * cppc_acpi.c for CPPC specific methods.
5 *
6 * (C) Copyright 2014, 2015 Linaro Ltd.
7 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2
12 * of the License.
13 */
14
15#define pr_fmt(fmt) "CPPC Cpufreq:" fmt
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/delay.h>
20#include <linux/cpu.h>
21#include <linux/cpufreq.h>
Al Stonead386772016-07-20 15:10:04 -060022#include <linux/dmi.h>
George Cherian3d413862018-03-23 03:30:31 -070023#include <linux/time.h>
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040024#include <linux/vmalloc.h>
25
Al Stonead386772016-07-20 15:10:04 -060026#include <asm/unaligned.h>
27
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040028#include <acpi/cppc_acpi.h>
29
Al Stonead386772016-07-20 15:10:04 -060030/* Minimum struct length needed for the DMI processor entry we want */
31#define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
32
33/* Offest in the DMI processor structure for the max frequency */
34#define DMI_PROCESSOR_MAX_SPEED 0x14
35
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040036/*
37 * These structs contain information parsed from per CPU
38 * ACPI _CPC structures.
39 * e.g. For each CPU the highest, lowest supported
40 * performance capabilities, desired performance level
41 * requested etc.
42 */
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -070043static struct cppc_cpudata **all_cpu_data;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040044
Al Stonead386772016-07-20 15:10:04 -060045/* Capture the max KHz from DMI */
46static u64 cppc_dmi_max_khz;
47
48/* Callback function used to retrieve the max frequency from DMI */
49static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
50{
51 const u8 *dmi_data = (const u8 *)dm;
52 u16 *mhz = (u16 *)private;
53
54 if (dm->type == DMI_ENTRY_PROCESSOR &&
55 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
56 u16 val = (u16)get_unaligned((const u16 *)
57 (dmi_data + DMI_PROCESSOR_MAX_SPEED));
58 *mhz = val > *mhz ? val : *mhz;
59 }
60}
61
62/* Look up the max frequency in DMI */
63static u64 cppc_get_dmi_max_khz(void)
64{
65 u16 mhz = 0;
66
67 dmi_walk(cppc_find_dmi_mhz, &mhz);
68
69 /*
70 * Real stupid fallback value, just in case there is no
71 * actual value set.
72 */
73 mhz = mhz ? mhz : 1;
74
75 return (1000 * mhz);
76}
77
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040078static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
79 unsigned int target_freq,
80 unsigned int relation)
81{
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -070082 struct cppc_cpudata *cpu;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040083 struct cpufreq_freqs freqs;
Hoan Tranc197d752016-10-13 10:33:35 -070084 u32 desired_perf;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040085 int ret = 0;
86
87 cpu = all_cpu_data[policy->cpu];
88
Hoan Tranc197d752016-10-13 10:33:35 -070089 desired_perf = (u64)target_freq * cpu->perf_caps.highest_perf / cppc_dmi_max_khz;
90 /* Return if it is exactly the same perf */
91 if (desired_perf == cpu->perf_ctrls.desired_perf)
92 return ret;
93
94 cpu->perf_ctrls.desired_perf = desired_perf;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040095 freqs.old = policy->cur;
96 freqs.new = target_freq;
97
98 cpufreq_freq_transition_begin(policy, &freqs);
99 ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls);
100 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
101
102 if (ret)
103 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
104 cpu->cpu, ret);
105
106 return ret;
107}
108
109static int cppc_verify_policy(struct cpufreq_policy *policy)
110{
111 cpufreq_verify_within_cpu_limits(policy);
112 return 0;
113}
114
115static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
116{
117 int cpu_num = policy->cpu;
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700118 struct cppc_cpudata *cpu = all_cpu_data[cpu_num];
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400119 int ret;
120
121 cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf;
122
123 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
124 if (ret)
125 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
126 cpu->perf_caps.lowest_perf, cpu_num, ret);
127}
128
129static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
130{
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700131 struct cppc_cpudata *cpu;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400132 unsigned int cpu_num = policy->cpu;
133 int ret = 0;
134
135 cpu = all_cpu_data[policy->cpu];
136
137 cpu->cpu = cpu_num;
138 ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps);
139
140 if (ret) {
141 pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
142 cpu_num, ret);
143 return ret;
144 }
145
Al Stonead386772016-07-20 15:10:04 -0600146 cppc_dmi_max_khz = cppc_get_dmi_max_khz();
147
Prakash, Prashanth73808d02017-05-11 16:39:44 -0600148 /*
149 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
150 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
151 */
152 policy->min = cpu->perf_caps.lowest_nonlinear_perf * cppc_dmi_max_khz /
153 cpu->perf_caps.highest_perf;
Al Stonead386772016-07-20 15:10:04 -0600154 policy->max = cppc_dmi_max_khz;
Prakash, Prashanth73808d02017-05-11 16:39:44 -0600155
156 /*
157 * Set cpuinfo.min_freq to Lowest to make the full range of performance
158 * available if userspace wants to use any perf between lowest & lowest
159 * nonlinear perf
160 */
161 policy->cpuinfo.min_freq = cpu->perf_caps.lowest_perf * cppc_dmi_max_khz /
162 cpu->perf_caps.highest_perf;
163 policy->cpuinfo.max_freq = cppc_dmi_max_khz;
164
George Cherian3d413862018-03-23 03:30:31 -0700165 policy->transition_delay_us = cppc_get_transition_latency(cpu_num) /
166 NSEC_PER_USEC;
Ashwin Chaugule9dc17912015-11-19 10:40:07 -0500167 policy->shared_type = cpu->shared_type;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400168
169 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
170 cpumask_copy(policy->cpus, cpu->shared_cpu_map);
Ashwin Chaugule9dc17912015-11-19 10:40:07 -0500171 else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400172 /* Support only SW_ANY for now. */
173 pr_debug("Unsupported CPU co-ord type\n");
174 return -EFAULT;
175 }
176
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400177 cpu->cur_policy = policy;
178
179 /* Set policy->cur to max now. The governors will adjust later. */
Al Stonead386772016-07-20 15:10:04 -0600180 policy->cur = cppc_dmi_max_khz;
181 cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400182
183 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
184 if (ret)
185 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
186 cpu->perf_caps.highest_perf, cpu_num, ret);
187
188 return ret;
189}
190
191static struct cpufreq_driver cppc_cpufreq_driver = {
192 .flags = CPUFREQ_CONST_LOOPS,
193 .verify = cppc_verify_policy,
194 .target = cppc_cpufreq_set_target,
195 .init = cppc_cpufreq_cpu_init,
196 .stop_cpu = cppc_cpufreq_stop_cpu,
197 .name = "cppc_cpufreq",
198};
199
200static int __init cppc_cpufreq_init(void)
201{
202 int i, ret = 0;
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700203 struct cppc_cpudata *cpu;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400204
205 if (acpi_disabled)
206 return -ENODEV;
207
208 all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
209 if (!all_cpu_data)
210 return -ENOMEM;
211
212 for_each_possible_cpu(i) {
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700213 all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400214 if (!all_cpu_data[i])
215 goto out;
216
217 cpu = all_cpu_data[i];
218 if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
219 goto out;
220 }
221
222 ret = acpi_get_psd_map(all_cpu_data);
223 if (ret) {
224 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
225 goto out;
226 }
227
228 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
229 if (ret)
230 goto out;
231
232 return ret;
233
234out:
Chunyu Hu55b55ab2018-03-05 13:40:38 +0800235 for_each_possible_cpu(i) {
236 cpu = all_cpu_data[i];
237 if (!cpu)
238 break;
239 free_cpumask_var(cpu->shared_cpu_map);
240 kfree(cpu);
241 }
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400242
243 kfree(all_cpu_data);
244 return -ENODEV;
245}
246
Ashwin Chaugulea29a1e72016-04-14 20:45:53 -0400247static void __exit cppc_cpufreq_exit(void)
248{
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700249 struct cppc_cpudata *cpu;
Ashwin Chaugulea29a1e72016-04-14 20:45:53 -0400250 int i;
251
252 cpufreq_unregister_driver(&cppc_cpufreq_driver);
253
254 for_each_possible_cpu(i) {
255 cpu = all_cpu_data[i];
256 free_cpumask_var(cpu->shared_cpu_map);
257 kfree(cpu);
258 }
259
260 kfree(all_cpu_data);
261}
262
263module_exit(cppc_cpufreq_exit);
264MODULE_AUTHOR("Ashwin Chaugule");
265MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
266MODULE_LICENSE("GPL");
267
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400268late_initcall(cppc_cpufreq_init);
Prakash, Prashanth974f8642016-10-14 12:00:23 -0600269
270static const struct acpi_device_id cppc_acpi_ids[] = {
271 {ACPI_PROCESSOR_DEVICE_HID, },
272 {}
273};
274
275MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);