]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/cpufreq/cppc_cpufreq.c
Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-bionic-kernel.git] / drivers / cpufreq / cppc_cpufreq.c
CommitLineData
5477fb3b
AC
1/*
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>
ad38677d 22#include <linux/dmi.h>
5477fb3b
AC
23#include <linux/vmalloc.h>
24
ad38677d
AS
25#include <asm/unaligned.h>
26
5477fb3b
AC
27#include <acpi/cppc_acpi.h>
28
ad38677d
AS
29/* 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
5477fb3b
AC
35/*
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 */
41dd6403 42static struct cppc_cpudata **all_cpu_data;
5477fb3b 43
ad38677d
AS
44/* 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
5477fb3b
AC
77static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
78 unsigned int target_freq,
79 unsigned int relation)
80{
41dd6403 81 struct cppc_cpudata *cpu;
5477fb3b 82 struct cpufreq_freqs freqs;
c197d758 83 u32 desired_perf;
5477fb3b
AC
84 int ret = 0;
85
86 cpu = all_cpu_data[policy->cpu];
87
c197d758
HT
88 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;
5477fb3b
AC
94 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;
41dd6403 117 struct cppc_cpudata *cpu = all_cpu_data[cpu_num];
5477fb3b
AC
118 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{
41dd6403 130 struct cppc_cpudata *cpu;
5477fb3b
AC
131 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
ad38677d
AS
145 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;
5477fb3b
AC
149 policy->cpuinfo.min_freq = policy->min;
150 policy->cpuinfo.max_freq = policy->max;
be8b88d7 151 policy->cpuinfo.transition_latency = cppc_get_transition_latency(cpu_num);
9dc17917 152 policy->shared_type = cpu->shared_type;
5477fb3b
AC
153
154 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
155 cpumask_copy(policy->cpus, cpu->shared_cpu_map);
9dc17917 156 else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
5477fb3b
AC
157 /* Support only SW_ANY for now. */
158 pr_debug("Unsupported CPU co-ord type\n");
159 return -EFAULT;
160 }
161
162 cpumask_set_cpu(policy->cpu, policy->cpus);
163 cpu->cur_policy = policy;
164
165 /* Set policy->cur to max now. The governors will adjust later. */
ad38677d
AS
166 policy->cur = cppc_dmi_max_khz;
167 cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
5477fb3b
AC
168
169 ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
170 if (ret)
171 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
172 cpu->perf_caps.highest_perf, cpu_num, ret);
173
174 return ret;
175}
176
177static struct cpufreq_driver cppc_cpufreq_driver = {
178 .flags = CPUFREQ_CONST_LOOPS,
179 .verify = cppc_verify_policy,
180 .target = cppc_cpufreq_set_target,
181 .init = cppc_cpufreq_cpu_init,
182 .stop_cpu = cppc_cpufreq_stop_cpu,
183 .name = "cppc_cpufreq",
184};
185
186static int __init cppc_cpufreq_init(void)
187{
188 int i, ret = 0;
41dd6403 189 struct cppc_cpudata *cpu;
5477fb3b
AC
190
191 if (acpi_disabled)
192 return -ENODEV;
193
194 all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
195 if (!all_cpu_data)
196 return -ENOMEM;
197
198 for_each_possible_cpu(i) {
41dd6403 199 all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
5477fb3b
AC
200 if (!all_cpu_data[i])
201 goto out;
202
203 cpu = all_cpu_data[i];
204 if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
205 goto out;
206 }
207
208 ret = acpi_get_psd_map(all_cpu_data);
209 if (ret) {
210 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
211 goto out;
212 }
213
214 ret = cpufreq_register_driver(&cppc_cpufreq_driver);
215 if (ret)
216 goto out;
217
218 return ret;
219
220out:
221 for_each_possible_cpu(i)
efb2d3be 222 kfree(all_cpu_data[i]);
5477fb3b
AC
223
224 kfree(all_cpu_data);
225 return -ENODEV;
226}
227
a29a1e76
AC
228static void __exit cppc_cpufreq_exit(void)
229{
41dd6403 230 struct cppc_cpudata *cpu;
a29a1e76
AC
231 int i;
232
233 cpufreq_unregister_driver(&cppc_cpufreq_driver);
234
235 for_each_possible_cpu(i) {
236 cpu = all_cpu_data[i];
237 free_cpumask_var(cpu->shared_cpu_map);
238 kfree(cpu);
239 }
240
241 kfree(all_cpu_data);
242}
243
244module_exit(cppc_cpufreq_exit);
245MODULE_AUTHOR("Ashwin Chaugule");
246MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
247MODULE_LICENSE("GPL");
248
5477fb3b 249late_initcall(cppc_cpufreq_init);
974f8649
PP
250
251static const struct acpi_device_id cppc_acpi_ids[] = {
252 {ACPI_PROCESSOR_DEVICE_HID, },
253 {}
254};
255
256MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);