blob: 9f09752169ea02f3b51759878e90469b5d566bfa [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>
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040023#include <linux/vmalloc.h>
24
Al Stonead386772016-07-20 15:10:04 -060025#include <asm/unaligned.h>
26
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040027#include <acpi/cppc_acpi.h>
28
Al Stonead386772016-07-20 15:10:04 -060029/* Minimum struct length needed for the DMI processor entry we want */
30#define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
31
32/* Offest in the DMI processor structure for the max frequency */
33#define DMI_PROCESSOR_MAX_SPEED 0x14
34
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040035/*
36 * These structs contain information parsed from per CPU
37 * ACPI _CPC structures.
38 * e.g. For each CPU the highest, lowest supported
39 * performance capabilities, desired performance level
40 * requested etc.
41 */
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -070042static struct cppc_cpudata **all_cpu_data;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040043
Al Stonead386772016-07-20 15:10:04 -060044/* Capture the max KHz from DMI */
45static u64 cppc_dmi_max_khz;
46
47/* Callback function used to retrieve the max frequency from DMI */
48static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
49{
50 const u8 *dmi_data = (const u8 *)dm;
51 u16 *mhz = (u16 *)private;
52
53 if (dm->type == DMI_ENTRY_PROCESSOR &&
54 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
55 u16 val = (u16)get_unaligned((const u16 *)
56 (dmi_data + DMI_PROCESSOR_MAX_SPEED));
57 *mhz = val > *mhz ? val : *mhz;
58 }
59}
60
61/* Look up the max frequency in DMI */
62static u64 cppc_get_dmi_max_khz(void)
63{
64 u16 mhz = 0;
65
66 dmi_walk(cppc_find_dmi_mhz, &mhz);
67
68 /*
69 * Real stupid fallback value, just in case there is no
70 * actual value set.
71 */
72 mhz = mhz ? mhz : 1;
73
74 return (1000 * mhz);
75}
76
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040077static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
78 unsigned int target_freq,
79 unsigned int relation)
80{
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -070081 struct cppc_cpudata *cpu;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040082 struct cpufreq_freqs freqs;
Hoan Tranc197d752016-10-13 10:33:35 -070083 u32 desired_perf;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040084 int ret = 0;
85
86 cpu = all_cpu_data[policy->cpu];
87
Hoan Tranc197d752016-10-13 10:33:35 -070088 desired_perf = (u64)target_freq * cpu->perf_caps.highest_perf / cppc_dmi_max_khz;
89 /* Return if it is exactly the same perf */
90 if (desired_perf == cpu->perf_ctrls.desired_perf)
91 return ret;
92
93 cpu->perf_ctrls.desired_perf = desired_perf;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -040094 freqs.old = policy->cur;
95 freqs.new = target_freq;
96
97 cpufreq_freq_transition_begin(policy, &freqs);
98 ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls);
99 cpufreq_freq_transition_end(policy, &freqs, ret != 0);
100
101 if (ret)
102 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
103 cpu->cpu, ret);
104
105 return ret;
106}
107
108static int cppc_verify_policy(struct cpufreq_policy *policy)
109{
110 cpufreq_verify_within_cpu_limits(policy);
111 return 0;
112}
113
114static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
115{
116 int cpu_num = policy->cpu;
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700117 struct cppc_cpudata *cpu = all_cpu_data[cpu_num];
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400118 int ret;
119
120 cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf;
121
122 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
123 if (ret)
124 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
125 cpu->perf_caps.lowest_perf, cpu_num, ret);
126}
127
128static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
129{
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700130 struct cppc_cpudata *cpu;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400131 unsigned int cpu_num = policy->cpu;
132 int ret = 0;
133
134 cpu = all_cpu_data[policy->cpu];
135
136 cpu->cpu = cpu_num;
137 ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps);
138
139 if (ret) {
140 pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
141 cpu_num, ret);
142 return ret;
143 }
144
Al Stonead386772016-07-20 15:10:04 -0600145 cppc_dmi_max_khz = cppc_get_dmi_max_khz();
146
147 policy->min = cpu->perf_caps.lowest_perf * cppc_dmi_max_khz / cpu->perf_caps.highest_perf;
148 policy->max = cppc_dmi_max_khz;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400149 policy->cpuinfo.min_freq = policy->min;
150 policy->cpuinfo.max_freq = policy->max;
Prakash, Prashanthbe8b88d2016-08-16 14:39:41 -0600151 policy->cpuinfo.transition_latency = cppc_get_transition_latency(cpu_num);
Ashwin Chaugule9dc17912015-11-19 10:40:07 -0500152 policy->shared_type = cpu->shared_type;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400153
Shunyong Yang707f25a2018-04-06 10:43:49 +0800154 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
155 int i;
156
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400157 cpumask_copy(policy->cpus, cpu->shared_cpu_map);
Shunyong Yang707f25a2018-04-06 10:43:49 +0800158
159 for_each_cpu(i, policy->cpus) {
160 if (unlikely(i == policy->cpu))
161 continue;
162
163 memcpy(&all_cpu_data[i]->perf_caps, &cpu->perf_caps,
164 sizeof(cpu->perf_caps));
165 }
166 } else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400167 /* Support only SW_ANY for now. */
168 pr_debug("Unsupported CPU co-ord type\n");
169 return -EFAULT;
170 }
171
172 cpumask_set_cpu(policy->cpu, policy->cpus);
173 cpu->cur_policy = policy;
174
175 /* Set policy->cur to max now. The governors will adjust later. */
Al Stonead386772016-07-20 15:10:04 -0600176 policy->cur = cppc_dmi_max_khz;
177 cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400178
179 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
180 if (ret)
181 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
182 cpu->perf_caps.highest_perf, cpu_num, ret);
183
184 return ret;
185}
186
187static struct cpufreq_driver cppc_cpufreq_driver = {
188 .flags = CPUFREQ_CONST_LOOPS,
189 .verify = cppc_verify_policy,
190 .target = cppc_cpufreq_set_target,
191 .init = cppc_cpufreq_cpu_init,
192 .stop_cpu = cppc_cpufreq_stop_cpu,
193 .name = "cppc_cpufreq",
194};
195
196static int __init cppc_cpufreq_init(void)
197{
198 int i, ret = 0;
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700199 struct cppc_cpudata *cpu;
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400200
201 if (acpi_disabled)
202 return -ENODEV;
203
204 all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
205 if (!all_cpu_data)
206 return -ENOMEM;
207
208 for_each_possible_cpu(i) {
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700209 all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400210 if (!all_cpu_data[i])
211 goto out;
212
213 cpu = all_cpu_data[i];
214 if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
215 goto out;
216 }
217
218 ret = acpi_get_psd_map(all_cpu_data);
219 if (ret) {
220 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
221 goto out;
222 }
223
224 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
225 if (ret)
226 goto out;
227
228 return ret;
229
230out:
Chunyu Hu796fd6b2018-03-05 13:40:38 +0800231 for_each_possible_cpu(i) {
232 cpu = all_cpu_data[i];
233 if (!cpu)
234 break;
235 free_cpumask_var(cpu->shared_cpu_map);
236 kfree(cpu);
237 }
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400238
239 kfree(all_cpu_data);
240 return -ENODEV;
241}
242
Ashwin Chaugulea29a1e72016-04-14 20:45:53 -0400243static void __exit cppc_cpufreq_exit(void)
244{
Srinivas Pandruvada41dd6402016-09-01 13:37:11 -0700245 struct cppc_cpudata *cpu;
Ashwin Chaugulea29a1e72016-04-14 20:45:53 -0400246 int i;
247
248 cpufreq_unregister_driver(&cppc_cpufreq_driver);
249
250 for_each_possible_cpu(i) {
251 cpu = all_cpu_data[i];
252 free_cpumask_var(cpu->shared_cpu_map);
253 kfree(cpu);
254 }
255
256 kfree(all_cpu_data);
257}
258
259module_exit(cppc_cpufreq_exit);
260MODULE_AUTHOR("Ashwin Chaugule");
261MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
262MODULE_LICENSE("GPL");
263
Ashwin Chaugule5477fb32015-10-02 10:04:01 -0400264late_initcall(cppc_cpufreq_init);