1 // SPDX-License-Identifier: GPL-2.0-only
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>
21 #include <linux/uaccess.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
;
36 struct cpufreq_acpi_req
{
41 static struct cpufreq_acpi_io
*acpi_io_data
[NR_CPUS
];
43 static struct cpufreq_driver acpi_cpufreq_driver
;
47 processor_set_pstate (
52 pr_debug("processor_set_pstate\n");
54 retval
= ia64_pal_set_pstate((u64
)value
);
57 pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
66 processor_get_pstate (
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
;
79 pr_debug("Failed to get current freq with "
80 "error 0x%lx, idx 0x%x\n", retval
, *value
);
86 /* To be used only after data->acpi_data is initialized */
89 struct cpufreq_acpi_io
*data
,
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
;
108 struct cpufreq_acpi_req
*req
= arg
;
109 unsigned int cpu
= req
->cpu
;
110 struct cpufreq_acpi_io
*data
= acpi_io_data
[cpu
];
114 pr_debug("processor_get_freq\n");
115 if (smp_processor_id() != cpu
)
118 /* processor_get_pstate gets the instantaneous frequency */
119 ret
= processor_get_pstate(&value
);
121 pr_warn("get performance failed with error %d\n", ret
);
124 return 1000 * extract_clock(data
, value
);
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
;
138 pr_debug("processor_set_freq\n");
139 if (smp_processor_id() != cpu
)
142 if (state
== data
->acpi_data
.state
) {
143 if (unlikely(data
->resume
)) {
144 pr_debug("Called after resume, resetting to P%d\n", state
);
147 pr_debug("Already at target state (P%d)\n", state
);
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
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
);
165 pr_warn("Transition failed with error %d\n", ret
);
169 data
->acpi_data
.state
= state
;
178 struct cpufreq_acpi_req req
;
182 ret
= work_on_cpu(cpu
, processor_get_freq
, &req
);
184 return ret
> 0 ? (unsigned int) ret
: 0;
189 acpi_cpufreq_target (
190 struct cpufreq_policy
*policy
,
193 struct cpufreq_acpi_req req
;
195 req
.cpu
= policy
->cpu
;
198 return work_on_cpu(req
.cpu
, processor_set_freq
, &req
);
202 acpi_cpufreq_cpu_init (
203 struct cpufreq_policy
*policy
)
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
);
217 acpi_io_data
[cpu
] = data
;
219 result
= acpi_processor_register_performance(&data
->acpi_data
, cpu
);
224 /* capability check */
225 if (data
->acpi_data
.state_count
<= 1) {
226 pr_debug("No P-States\n");
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
));
242 /* alloc freq_table */
243 freq_table
= kcalloc(data
->acpi_data
.state_count
+ 1,
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;
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;
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. */
296 acpi_processor_unregister_performance(cpu
);
299 acpi_io_data
[cpu
] = NULL
;
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");
314 acpi_io_data
[policy
->cpu
] = NULL
;
315 acpi_processor_unregister_performance(policy
->cpu
);
316 kfree(policy
->freq_table
);
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
,
336 acpi_cpufreq_init (void)
338 pr_debug("acpi_cpufreq_init\n");
340 return cpufreq_register_driver(&acpi_cpufreq_driver
);
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
);