Commit | Line | Data |
---|---|---|
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> | |
22 | #include <linux/vmalloc.h> | |
23 | ||
24 | #include <acpi/cppc_acpi.h> | |
25 | ||
26 | /* | |
27 | * These structs contain information parsed from per CPU | |
28 | * ACPI _CPC structures. | |
29 | * e.g. For each CPU the highest, lowest supported | |
30 | * performance capabilities, desired performance level | |
31 | * requested etc. | |
32 | */ | |
41dd6403 | 33 | static struct cppc_cpudata **all_cpu_data; |
5477fb3b AC |
34 | |
35 | static int cppc_cpufreq_set_target(struct cpufreq_policy *policy, | |
36 | unsigned int target_freq, | |
37 | unsigned int relation) | |
38 | { | |
41dd6403 | 39 | struct cppc_cpudata *cpu; |
5477fb3b AC |
40 | struct cpufreq_freqs freqs; |
41 | int ret = 0; | |
42 | ||
43 | cpu = all_cpu_data[policy->cpu]; | |
44 | ||
45 | cpu->perf_ctrls.desired_perf = target_freq; | |
46 | freqs.old = policy->cur; | |
47 | freqs.new = target_freq; | |
48 | ||
49 | cpufreq_freq_transition_begin(policy, &freqs); | |
50 | ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls); | |
51 | cpufreq_freq_transition_end(policy, &freqs, ret != 0); | |
52 | ||
53 | if (ret) | |
54 | pr_debug("Failed to set target on CPU:%d. ret:%d\n", | |
55 | cpu->cpu, ret); | |
56 | ||
57 | return ret; | |
58 | } | |
59 | ||
60 | static int cppc_verify_policy(struct cpufreq_policy *policy) | |
61 | { | |
62 | cpufreq_verify_within_cpu_limits(policy); | |
63 | return 0; | |
64 | } | |
65 | ||
66 | static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy) | |
67 | { | |
68 | int cpu_num = policy->cpu; | |
41dd6403 | 69 | struct cppc_cpudata *cpu = all_cpu_data[cpu_num]; |
5477fb3b AC |
70 | int ret; |
71 | ||
72 | cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf; | |
73 | ||
74 | ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls); | |
75 | if (ret) | |
76 | pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", | |
77 | cpu->perf_caps.lowest_perf, cpu_num, ret); | |
78 | } | |
79 | ||
80 | static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) | |
81 | { | |
41dd6403 | 82 | struct cppc_cpudata *cpu; |
5477fb3b AC |
83 | unsigned int cpu_num = policy->cpu; |
84 | int ret = 0; | |
85 | ||
86 | cpu = all_cpu_data[policy->cpu]; | |
87 | ||
88 | cpu->cpu = cpu_num; | |
89 | ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps); | |
90 | ||
91 | if (ret) { | |
92 | pr_debug("Err reading CPU%d perf capabilities. ret:%d\n", | |
93 | cpu_num, ret); | |
94 | return ret; | |
95 | } | |
96 | ||
97 | policy->min = cpu->perf_caps.lowest_perf; | |
98 | policy->max = cpu->perf_caps.highest_perf; | |
99 | policy->cpuinfo.min_freq = policy->min; | |
100 | policy->cpuinfo.max_freq = policy->max; | |
be8b88d7 | 101 | policy->cpuinfo.transition_latency = cppc_get_transition_latency(cpu_num); |
9dc17917 | 102 | policy->shared_type = cpu->shared_type; |
5477fb3b AC |
103 | |
104 | if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) | |
105 | cpumask_copy(policy->cpus, cpu->shared_cpu_map); | |
9dc17917 | 106 | else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) { |
5477fb3b AC |
107 | /* Support only SW_ANY for now. */ |
108 | pr_debug("Unsupported CPU co-ord type\n"); | |
109 | return -EFAULT; | |
110 | } | |
111 | ||
112 | cpumask_set_cpu(policy->cpu, policy->cpus); | |
113 | cpu->cur_policy = policy; | |
114 | ||
115 | /* Set policy->cur to max now. The governors will adjust later. */ | |
116 | policy->cur = cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf; | |
117 | ||
118 | ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls); | |
119 | if (ret) | |
120 | pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", | |
121 | cpu->perf_caps.highest_perf, cpu_num, ret); | |
122 | ||
123 | return ret; | |
124 | } | |
125 | ||
126 | static struct cpufreq_driver cppc_cpufreq_driver = { | |
127 | .flags = CPUFREQ_CONST_LOOPS, | |
128 | .verify = cppc_verify_policy, | |
129 | .target = cppc_cpufreq_set_target, | |
130 | .init = cppc_cpufreq_cpu_init, | |
131 | .stop_cpu = cppc_cpufreq_stop_cpu, | |
132 | .name = "cppc_cpufreq", | |
133 | }; | |
134 | ||
135 | static int __init cppc_cpufreq_init(void) | |
136 | { | |
137 | int i, ret = 0; | |
41dd6403 | 138 | struct cppc_cpudata *cpu; |
5477fb3b AC |
139 | |
140 | if (acpi_disabled) | |
141 | return -ENODEV; | |
142 | ||
143 | all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL); | |
144 | if (!all_cpu_data) | |
145 | return -ENOMEM; | |
146 | ||
147 | for_each_possible_cpu(i) { | |
41dd6403 | 148 | all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL); |
5477fb3b AC |
149 | if (!all_cpu_data[i]) |
150 | goto out; | |
151 | ||
152 | cpu = all_cpu_data[i]; | |
153 | if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL)) | |
154 | goto out; | |
155 | } | |
156 | ||
157 | ret = acpi_get_psd_map(all_cpu_data); | |
158 | if (ret) { | |
159 | pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n"); | |
160 | goto out; | |
161 | } | |
162 | ||
163 | ret = cpufreq_register_driver(&cppc_cpufreq_driver); | |
164 | if (ret) | |
165 | goto out; | |
166 | ||
167 | return ret; | |
168 | ||
169 | out: | |
170 | for_each_possible_cpu(i) | |
efb2d3be | 171 | kfree(all_cpu_data[i]); |
5477fb3b AC |
172 | |
173 | kfree(all_cpu_data); | |
174 | return -ENODEV; | |
175 | } | |
176 | ||
a29a1e76 AC |
177 | static void __exit cppc_cpufreq_exit(void) |
178 | { | |
41dd6403 | 179 | struct cppc_cpudata *cpu; |
a29a1e76 AC |
180 | int i; |
181 | ||
182 | cpufreq_unregister_driver(&cppc_cpufreq_driver); | |
183 | ||
184 | for_each_possible_cpu(i) { | |
185 | cpu = all_cpu_data[i]; | |
186 | free_cpumask_var(cpu->shared_cpu_map); | |
187 | kfree(cpu); | |
188 | } | |
189 | ||
190 | kfree(all_cpu_data); | |
191 | } | |
192 | ||
193 | module_exit(cppc_cpufreq_exit); | |
194 | MODULE_AUTHOR("Ashwin Chaugule"); | |
195 | MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec"); | |
196 | MODULE_LICENSE("GPL"); | |
197 | ||
5477fb3b | 198 | late_initcall(cppc_cpufreq_init); |