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.
6 * (C) Copyright 2014, 2015 Linaro Ltd.
7 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
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
15 #define pr_fmt(fmt) "CPPC Cpufreq:" fmt
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/dmi.h>
23 #include <linux/time.h>
24 #include <linux/vmalloc.h>
26 #include <asm/unaligned.h>
28 #include <acpi/cppc_acpi.h>
30 /* Minimum struct length needed for the DMI processor entry we want */
31 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
33 /* Offest in the DMI processor structure for the max frequency */
34 #define DMI_PROCESSOR_MAX_SPEED 0x14
37 * These structs contain information parsed from per CPU
38 * ACPI _CPC structures.
39 * e.g. For each CPU the highest, lowest supported
40 * performance capabilities, desired performance level
43 static struct cppc_cpudata
**all_cpu_data
;
45 /* Capture the max KHz from DMI */
46 static u64 cppc_dmi_max_khz
;
48 /* Callback function used to retrieve the max frequency from DMI */
49 static void cppc_find_dmi_mhz(const struct dmi_header
*dm
, void *private)
51 const u8
*dmi_data
= (const u8
*)dm
;
52 u16
*mhz
= (u16
*)private;
54 if (dm
->type
== DMI_ENTRY_PROCESSOR
&&
55 dm
->length
>= DMI_ENTRY_PROCESSOR_MIN_LENGTH
) {
56 u16 val
= (u16
)get_unaligned((const u16
*)
57 (dmi_data
+ DMI_PROCESSOR_MAX_SPEED
));
58 *mhz
= val
> *mhz
? val
: *mhz
;
62 /* Look up the max frequency in DMI */
63 static u64
cppc_get_dmi_max_khz(void)
67 dmi_walk(cppc_find_dmi_mhz
, &mhz
);
70 * Real stupid fallback value, just in case there is no
78 static int cppc_cpufreq_set_target(struct cpufreq_policy
*policy
,
79 unsigned int target_freq
,
80 unsigned int relation
)
82 struct cppc_cpudata
*cpu
;
83 struct cpufreq_freqs freqs
;
87 cpu
= all_cpu_data
[policy
->cpu
];
89 desired_perf
= (u64
)target_freq
* cpu
->perf_caps
.highest_perf
/ cppc_dmi_max_khz
;
90 /* Return if it is exactly the same perf */
91 if (desired_perf
== cpu
->perf_ctrls
.desired_perf
)
94 cpu
->perf_ctrls
.desired_perf
= desired_perf
;
95 freqs
.old
= policy
->cur
;
96 freqs
.new = target_freq
;
98 cpufreq_freq_transition_begin(policy
, &freqs
);
99 ret
= cppc_set_perf(cpu
->cpu
, &cpu
->perf_ctrls
);
100 cpufreq_freq_transition_end(policy
, &freqs
, ret
!= 0);
103 pr_debug("Failed to set target on CPU:%d. ret:%d\n",
109 static int cppc_verify_policy(struct cpufreq_policy
*policy
)
111 cpufreq_verify_within_cpu_limits(policy
);
115 static void cppc_cpufreq_stop_cpu(struct cpufreq_policy
*policy
)
117 int cpu_num
= policy
->cpu
;
118 struct cppc_cpudata
*cpu
= all_cpu_data
[cpu_num
];
121 cpu
->perf_ctrls
.desired_perf
= cpu
->perf_caps
.lowest_perf
;
123 ret
= cppc_set_perf(cpu_num
, &cpu
->perf_ctrls
);
125 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
126 cpu
->perf_caps
.lowest_perf
, cpu_num
, ret
);
129 static int cppc_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
131 struct cppc_cpudata
*cpu
;
132 unsigned int cpu_num
= policy
->cpu
;
135 cpu
= all_cpu_data
[policy
->cpu
];
138 ret
= cppc_get_perf_caps(policy
->cpu
, &cpu
->perf_caps
);
141 pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
146 cppc_dmi_max_khz
= cppc_get_dmi_max_khz();
149 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
150 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
152 policy
->min
= cpu
->perf_caps
.lowest_nonlinear_perf
* cppc_dmi_max_khz
/
153 cpu
->perf_caps
.highest_perf
;
154 policy
->max
= cppc_dmi_max_khz
;
157 * Set cpuinfo.min_freq to Lowest to make the full range of performance
158 * available if userspace wants to use any perf between lowest & lowest
161 policy
->cpuinfo
.min_freq
= cpu
->perf_caps
.lowest_perf
* cppc_dmi_max_khz
/
162 cpu
->perf_caps
.highest_perf
;
163 policy
->cpuinfo
.max_freq
= cppc_dmi_max_khz
;
165 policy
->cpuinfo
.transition_latency
= cppc_get_transition_latency(cpu_num
);
166 policy
->transition_delay_us
= cppc_get_transition_latency(cpu_num
) /
168 policy
->shared_type
= cpu
->shared_type
;
170 if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ANY
)
171 cpumask_copy(policy
->cpus
, cpu
->shared_cpu_map
);
172 else if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ALL
) {
173 /* Support only SW_ANY for now. */
174 pr_debug("Unsupported CPU co-ord type\n");
178 cpu
->cur_policy
= policy
;
180 /* Set policy->cur to max now. The governors will adjust later. */
181 policy
->cur
= cppc_dmi_max_khz
;
182 cpu
->perf_ctrls
.desired_perf
= cpu
->perf_caps
.highest_perf
;
184 ret
= cppc_set_perf(cpu_num
, &cpu
->perf_ctrls
);
186 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
187 cpu
->perf_caps
.highest_perf
, cpu_num
, ret
);
192 static struct cpufreq_driver cppc_cpufreq_driver
= {
193 .flags
= CPUFREQ_CONST_LOOPS
,
194 .verify
= cppc_verify_policy
,
195 .target
= cppc_cpufreq_set_target
,
196 .init
= cppc_cpufreq_cpu_init
,
197 .stop_cpu
= cppc_cpufreq_stop_cpu
,
198 .name
= "cppc_cpufreq",
201 static int __init
cppc_cpufreq_init(void)
204 struct cppc_cpudata
*cpu
;
209 all_cpu_data
= kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL
);
213 for_each_possible_cpu(i
) {
214 all_cpu_data
[i
] = kzalloc(sizeof(struct cppc_cpudata
), GFP_KERNEL
);
215 if (!all_cpu_data
[i
])
218 cpu
= all_cpu_data
[i
];
219 if (!zalloc_cpumask_var(&cpu
->shared_cpu_map
, GFP_KERNEL
))
223 ret
= acpi_get_psd_map(all_cpu_data
);
225 pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
229 ret
= cpufreq_register_driver(&cppc_cpufreq_driver
);
236 for_each_possible_cpu(i
)
237 kfree(all_cpu_data
[i
]);
243 static void __exit
cppc_cpufreq_exit(void)
245 struct cppc_cpudata
*cpu
;
248 cpufreq_unregister_driver(&cppc_cpufreq_driver
);
250 for_each_possible_cpu(i
) {
251 cpu
= all_cpu_data
[i
];
252 free_cpumask_var(cpu
->shared_cpu_map
);
259 module_exit(cppc_cpufreq_exit
);
260 MODULE_AUTHOR("Ashwin Chaugule");
261 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
262 MODULE_LICENSE("GPL");
264 late_initcall(cppc_cpufreq_init
);
266 static const struct acpi_device_id cppc_acpi_ids
[] = {
267 {ACPI_PROCESSOR_DEVICE_HID
, },
271 MODULE_DEVICE_TABLE(acpi
, cppc_acpi_ids
);