treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / cpufreq / ia64-acpi-cpufreq.c
blob2efe7189ccc4273e72cb16417679aba31af43dcd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This file provides the ACPI based P-state support. This
4 * module works with generic cpufreq infrastructure. Most of
5 * the code is based on i386 version
6 * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
8 * Copyright (C) 2005 Intel Corp
9 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/cpufreq.h>
19 #include <linux/proc_fs.h>
20 #include <asm/io.h>
21 #include <linux/uaccess.h>
22 #include <asm/pal.h>
24 #include <linux/acpi.h>
25 #include <acpi/processor.h>
27 MODULE_AUTHOR("Venkatesh Pallipadi");
28 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
29 MODULE_LICENSE("GPL");
31 struct cpufreq_acpi_io {
32 struct acpi_processor_performance acpi_data;
33 unsigned int resume;
36 struct cpufreq_acpi_req {
37 unsigned int cpu;
38 unsigned int state;
41 static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
43 static struct cpufreq_driver acpi_cpufreq_driver;
46 static int
47 processor_set_pstate (
48 u32 value)
50 s64 retval;
52 pr_debug("processor_set_pstate\n");
54 retval = ia64_pal_set_pstate((u64)value);
56 if (retval) {
57 pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
58 value, retval);
59 return -ENODEV;
61 return (int)retval;
65 static int
66 processor_get_pstate (
67 u32 *value)
69 u64 pstate_index = 0;
70 s64 retval;
72 pr_debug("processor_get_pstate\n");
74 retval = ia64_pal_get_pstate(&pstate_index,
75 PAL_GET_PSTATE_TYPE_INSTANT);
76 *value = (u32) pstate_index;
78 if (retval)
79 pr_debug("Failed to get current freq with "
80 "error 0x%lx, idx 0x%x\n", retval, *value);
82 return (int)retval;
86 /* To be used only after data->acpi_data is initialized */
87 static unsigned
88 extract_clock (
89 struct cpufreq_acpi_io *data,
90 unsigned value)
92 unsigned long i;
94 pr_debug("extract_clock\n");
96 for (i = 0; i < data->acpi_data.state_count; i++) {
97 if (value == data->acpi_data.states[i].status)
98 return data->acpi_data.states[i].core_frequency;
100 return data->acpi_data.states[i-1].core_frequency;
104 static long
105 processor_get_freq (
106 void *arg)
108 struct cpufreq_acpi_req *req = arg;
109 unsigned int cpu = req->cpu;
110 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
111 u32 value;
112 int ret;
114 pr_debug("processor_get_freq\n");
115 if (smp_processor_id() != cpu)
116 return -EAGAIN;
118 /* processor_get_pstate gets the instantaneous frequency */
119 ret = processor_get_pstate(&value);
120 if (ret) {
121 pr_warn("get performance failed with error %d\n", ret);
122 return ret;
124 return 1000 * extract_clock(data, value);
128 static long
129 processor_set_freq (
130 void *arg)
132 struct cpufreq_acpi_req *req = arg;
133 unsigned int cpu = req->cpu;
134 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
135 int ret, state = req->state;
136 u32 value;
138 pr_debug("processor_set_freq\n");
139 if (smp_processor_id() != cpu)
140 return -EAGAIN;
142 if (state == data->acpi_data.state) {
143 if (unlikely(data->resume)) {
144 pr_debug("Called after resume, resetting to P%d\n", state);
145 data->resume = 0;
146 } else {
147 pr_debug("Already at target state (P%d)\n", state);
148 return 0;
152 pr_debug("Transitioning from P%d to P%d\n",
153 data->acpi_data.state, state);
156 * First we write the target state's 'control' value to the
157 * control_register.
159 value = (u32) data->acpi_data.states[state].control;
161 pr_debug("Transitioning to state: 0x%08x\n", value);
163 ret = processor_set_pstate(value);
164 if (ret) {
165 pr_warn("Transition failed with error %d\n", ret);
166 return -ENODEV;
169 data->acpi_data.state = state;
170 return 0;
174 static unsigned int
175 acpi_cpufreq_get (
176 unsigned int cpu)
178 struct cpufreq_acpi_req req;
179 long ret;
181 req.cpu = cpu;
182 ret = work_on_cpu(cpu, processor_get_freq, &req);
184 return ret > 0 ? (unsigned int) ret : 0;
188 static int
189 acpi_cpufreq_target (
190 struct cpufreq_policy *policy,
191 unsigned int index)
193 struct cpufreq_acpi_req req;
195 req.cpu = policy->cpu;
196 req.state = index;
198 return work_on_cpu(req.cpu, processor_set_freq, &req);
201 static int
202 acpi_cpufreq_cpu_init (
203 struct cpufreq_policy *policy)
205 unsigned int i;
206 unsigned int cpu = policy->cpu;
207 struct cpufreq_acpi_io *data;
208 unsigned int result = 0;
209 struct cpufreq_frequency_table *freq_table;
211 pr_debug("acpi_cpufreq_cpu_init\n");
213 data = kzalloc(sizeof(*data), GFP_KERNEL);
214 if (!data)
215 return (-ENOMEM);
217 acpi_io_data[cpu] = data;
219 result = acpi_processor_register_performance(&data->acpi_data, cpu);
221 if (result)
222 goto err_free;
224 /* capability check */
225 if (data->acpi_data.state_count <= 1) {
226 pr_debug("No P-States\n");
227 result = -ENODEV;
228 goto err_unreg;
231 if ((data->acpi_data.control_register.space_id !=
232 ACPI_ADR_SPACE_FIXED_HARDWARE) ||
233 (data->acpi_data.status_register.space_id !=
234 ACPI_ADR_SPACE_FIXED_HARDWARE)) {
235 pr_debug("Unsupported address space [%d, %d]\n",
236 (u32) (data->acpi_data.control_register.space_id),
237 (u32) (data->acpi_data.status_register.space_id));
238 result = -ENODEV;
239 goto err_unreg;
242 /* alloc freq_table */
243 freq_table = kcalloc(data->acpi_data.state_count + 1,
244 sizeof(*freq_table),
245 GFP_KERNEL);
246 if (!freq_table) {
247 result = -ENOMEM;
248 goto err_unreg;
251 /* detect transition latency */
252 policy->cpuinfo.transition_latency = 0;
253 for (i=0; i<data->acpi_data.state_count; i++) {
254 if ((data->acpi_data.states[i].transition_latency * 1000) >
255 policy->cpuinfo.transition_latency) {
256 policy->cpuinfo.transition_latency =
257 data->acpi_data.states[i].transition_latency * 1000;
261 /* table init */
262 for (i = 0; i <= data->acpi_data.state_count; i++)
264 if (i < data->acpi_data.state_count) {
265 freq_table[i].frequency =
266 data->acpi_data.states[i].core_frequency * 1000;
267 } else {
268 freq_table[i].frequency = CPUFREQ_TABLE_END;
272 policy->freq_table = freq_table;
274 /* notify BIOS that we exist */
275 acpi_processor_notify_smm(THIS_MODULE);
277 pr_info("CPU%u - ACPI performance management activated\n", cpu);
279 for (i = 0; i < data->acpi_data.state_count; i++)
280 pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
281 (i == data->acpi_data.state?'*':' '), i,
282 (u32) data->acpi_data.states[i].core_frequency,
283 (u32) data->acpi_data.states[i].power,
284 (u32) data->acpi_data.states[i].transition_latency,
285 (u32) data->acpi_data.states[i].bus_master_latency,
286 (u32) data->acpi_data.states[i].status,
287 (u32) data->acpi_data.states[i].control);
289 /* the first call to ->target() should result in us actually
290 * writing something to the appropriate registers. */
291 data->resume = 1;
293 return (result);
295 err_unreg:
296 acpi_processor_unregister_performance(cpu);
297 err_free:
298 kfree(data);
299 acpi_io_data[cpu] = NULL;
301 return (result);
305 static int
306 acpi_cpufreq_cpu_exit (
307 struct cpufreq_policy *policy)
309 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
311 pr_debug("acpi_cpufreq_cpu_exit\n");
313 if (data) {
314 acpi_io_data[policy->cpu] = NULL;
315 acpi_processor_unregister_performance(policy->cpu);
316 kfree(policy->freq_table);
317 kfree(data);
320 return (0);
324 static struct cpufreq_driver acpi_cpufreq_driver = {
325 .verify = cpufreq_generic_frequency_table_verify,
326 .target_index = acpi_cpufreq_target,
327 .get = acpi_cpufreq_get,
328 .init = acpi_cpufreq_cpu_init,
329 .exit = acpi_cpufreq_cpu_exit,
330 .name = "acpi-cpufreq",
331 .attr = cpufreq_generic_attr,
335 static int __init
336 acpi_cpufreq_init (void)
338 pr_debug("acpi_cpufreq_init\n");
340 return cpufreq_register_driver(&acpi_cpufreq_driver);
344 static void __exit
345 acpi_cpufreq_exit (void)
347 pr_debug("acpi_cpufreq_exit\n");
349 cpufreq_unregister_driver(&acpi_cpufreq_driver);
352 late_initcall(acpi_cpufreq_init);
353 module_exit(acpi_cpufreq_exit);