xref: /linux/drivers/cpufreq/e_powersaver.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Based on documentation provided by Dave Jones. Thanks!
4  *
5  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/cpufreq.h>
14 #include <linux/ioport.h>
15 #include <linux/slab.h>
16 #include <linux/timex.h>
17 #include <linux/io.h>
18 #include <linux/delay.h>
19 
20 #include <asm/cpu_device_id.h>
21 #include <asm/msr.h>
22 #include <asm/tsc.h>
23 
24 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
25 #include <linux/acpi.h>
26 #include <acpi/processor.h>
27 #endif
28 
29 #define EPS_BRAND_C7M	0
30 #define EPS_BRAND_C7	1
31 #define EPS_BRAND_EDEN	2
32 #define EPS_BRAND_C3	3
33 #define EPS_BRAND_C7D	4
34 
35 struct eps_cpu_data {
36 	u32 fsb;
37 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
38 	u32 bios_limit;
39 #endif
40 	struct cpufreq_frequency_table freq_table[];
41 };
42 
43 static struct eps_cpu_data *eps_cpu[NR_CPUS];
44 
45 /* Module parameters */
46 static int freq_failsafe_off;
47 static int voltage_failsafe_off;
48 static int set_max_voltage;
49 
50 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
51 static int ignore_acpi_limit;
52 
53 static struct acpi_processor_performance *eps_acpi_cpu_perf;
54 
55 /* Minimum necessary to get acpi_processor_get_bios_limit() working */
56 static int eps_acpi_init(void)
57 {
58 	eps_acpi_cpu_perf = kzalloc_obj(*eps_acpi_cpu_perf, GFP_KERNEL);
59 	if (!eps_acpi_cpu_perf)
60 		return -ENOMEM;
61 
62 	if (!zalloc_cpumask_var(&eps_acpi_cpu_perf->shared_cpu_map,
63 								GFP_KERNEL)) {
64 		kfree(eps_acpi_cpu_perf);
65 		eps_acpi_cpu_perf = NULL;
66 		return -ENOMEM;
67 	}
68 
69 	if (acpi_processor_register_performance(eps_acpi_cpu_perf, 0)) {
70 		free_cpumask_var(eps_acpi_cpu_perf->shared_cpu_map);
71 		kfree(eps_acpi_cpu_perf);
72 		eps_acpi_cpu_perf = NULL;
73 		return -EIO;
74 	}
75 	return 0;
76 }
77 
78 static int eps_acpi_exit(struct cpufreq_policy *policy)
79 {
80 	if (eps_acpi_cpu_perf) {
81 		acpi_processor_unregister_performance(0);
82 		free_cpumask_var(eps_acpi_cpu_perf->shared_cpu_map);
83 		kfree(eps_acpi_cpu_perf);
84 		eps_acpi_cpu_perf = NULL;
85 	}
86 	return 0;
87 }
88 #endif
89 
90 static unsigned int eps_get(unsigned int cpu)
91 {
92 	struct eps_cpu_data *centaur;
93 	u32 lo, hi;
94 
95 	if (cpu)
96 		return 0;
97 	centaur = eps_cpu[cpu];
98 	if (centaur == NULL)
99 		return 0;
100 
101 	/* Return current frequency */
102 	rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
103 	return centaur->fsb * ((lo >> 8) & 0xff);
104 }
105 
106 static int eps_set_state(struct eps_cpu_data *centaur,
107 			 struct cpufreq_policy *policy,
108 			 u32 dest_state)
109 {
110 	u32 lo, hi;
111 	int i;
112 
113 	/* Wait while CPU is busy */
114 	rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
115 	i = 0;
116 	while (lo & ((1 << 16) | (1 << 17))) {
117 		udelay(16);
118 		rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
119 		i++;
120 		if (unlikely(i > 64)) {
121 			return -ENODEV;
122 		}
123 	}
124 	/* Set new multiplier and voltage */
125 	wrmsr(MSR_IA32_PERF_CTL, dest_state & 0xffff, 0);
126 	/* Wait until transition end */
127 	i = 0;
128 	do {
129 		udelay(16);
130 		rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
131 		i++;
132 		if (unlikely(i > 64)) {
133 			return -ENODEV;
134 		}
135 	} while (lo & ((1 << 16) | (1 << 17)));
136 
137 #ifdef DEBUG
138 	{
139 	u8 current_multiplier, current_voltage;
140 
141 	/* Print voltage and multiplier */
142 	rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
143 	current_voltage = lo & 0xff;
144 	pr_info("Current voltage = %dmV\n", current_voltage * 16 + 700);
145 	current_multiplier = (lo >> 8) & 0xff;
146 	pr_info("Current multiplier = %d\n", current_multiplier);
147 	}
148 #endif
149 	return 0;
150 }
151 
152 static int eps_target(struct cpufreq_policy *policy, unsigned int index)
153 {
154 	struct eps_cpu_data *centaur;
155 	unsigned int cpu = policy->cpu;
156 	unsigned int dest_state;
157 	int ret;
158 
159 	if (unlikely(eps_cpu[cpu] == NULL))
160 		return -ENODEV;
161 	centaur = eps_cpu[cpu];
162 
163 	/* Make frequency transition */
164 	dest_state = centaur->freq_table[index].driver_data & 0xffff;
165 	ret = eps_set_state(centaur, policy, dest_state);
166 	if (ret)
167 		pr_err("Timeout!\n");
168 	return ret;
169 }
170 
171 static int eps_cpu_init(struct cpufreq_policy *policy)
172 {
173 	unsigned int i;
174 	u32 lo, hi;
175 	u64 val;
176 	u8 current_multiplier, current_voltage;
177 	u8 max_multiplier, max_voltage;
178 	u8 min_multiplier, min_voltage;
179 	u8 brand = 0;
180 	u32 fsb;
181 	struct eps_cpu_data *centaur;
182 	struct cpuinfo_x86 *c = &cpu_data(0);
183 	struct cpufreq_frequency_table *f_table;
184 	int k, step, voltage;
185 	int states;
186 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
187 	unsigned int limit;
188 #endif
189 
190 	if (policy->cpu != 0)
191 		return -ENODEV;
192 
193 	/* Check brand */
194 	pr_info("Detected VIA ");
195 
196 	switch (c->x86_model) {
197 	case 10:
198 		rdmsr(0x1153, lo, hi);
199 		brand = (((lo >> 2) ^ lo) >> 18) & 3;
200 		pr_cont("Model A ");
201 		break;
202 	case 13:
203 		rdmsr(0x1154, lo, hi);
204 		brand = (((lo >> 4) ^ (lo >> 2))) & 0x000000ff;
205 		pr_cont("Model D ");
206 		break;
207 	}
208 
209 	switch (brand) {
210 	case EPS_BRAND_C7M:
211 		pr_cont("C7-M\n");
212 		break;
213 	case EPS_BRAND_C7:
214 		pr_cont("C7\n");
215 		break;
216 	case EPS_BRAND_EDEN:
217 		pr_cont("Eden\n");
218 		break;
219 	case EPS_BRAND_C7D:
220 		pr_cont("C7-D\n");
221 		break;
222 	case EPS_BRAND_C3:
223 		pr_cont("C3\n");
224 		return -ENODEV;
225 	}
226 	/* Enable Enhanced PowerSaver */
227 	rdmsrq(MSR_IA32_MISC_ENABLE, val);
228 	if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
229 		val |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;
230 		wrmsrq(MSR_IA32_MISC_ENABLE, val);
231 		/* Can be locked at 0 */
232 		rdmsrq(MSR_IA32_MISC_ENABLE, val);
233 		if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
234 			pr_info("Can't enable Enhanced PowerSaver\n");
235 			return -ENODEV;
236 		}
237 	}
238 
239 	/* Print voltage and multiplier */
240 	rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
241 	current_voltage = lo & 0xff;
242 	pr_info("Current voltage = %dmV\n", current_voltage * 16 + 700);
243 	current_multiplier = (lo >> 8) & 0xff;
244 	pr_info("Current multiplier = %d\n", current_multiplier);
245 
246 	/* Print limits */
247 	max_voltage = hi & 0xff;
248 	pr_info("Highest voltage = %dmV\n", max_voltage * 16 + 700);
249 	max_multiplier = (hi >> 8) & 0xff;
250 	pr_info("Highest multiplier = %d\n", max_multiplier);
251 	min_voltage = (hi >> 16) & 0xff;
252 	pr_info("Lowest voltage = %dmV\n", min_voltage * 16 + 700);
253 	min_multiplier = (hi >> 24) & 0xff;
254 	pr_info("Lowest multiplier = %d\n", min_multiplier);
255 
256 	/* Sanity checks */
257 	if (current_multiplier == 0 || max_multiplier == 0
258 	    || min_multiplier == 0)
259 		return -EINVAL;
260 	if (current_multiplier > max_multiplier
261 	    || max_multiplier <= min_multiplier)
262 		return -EINVAL;
263 	if (current_voltage > 0x1f || max_voltage > 0x1f)
264 		return -EINVAL;
265 	if (max_voltage < min_voltage
266 	    || current_voltage < min_voltage
267 	    || current_voltage > max_voltage)
268 		return -EINVAL;
269 
270 	/* Check for systems using underclocked CPU */
271 	if (!freq_failsafe_off && max_multiplier != current_multiplier) {
272 		pr_info("Your processor is running at different frequency then its maximum. Aborting.\n");
273 		pr_info("You can use freq_failsafe_off option to disable this check.\n");
274 		return -EINVAL;
275 	}
276 	if (!voltage_failsafe_off && max_voltage != current_voltage) {
277 		pr_info("Your processor is running at different voltage then its maximum. Aborting.\n");
278 		pr_info("You can use voltage_failsafe_off option to disable this check.\n");
279 		return -EINVAL;
280 	}
281 
282 	/* Calc FSB speed */
283 	fsb = cpu_khz / current_multiplier;
284 
285 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
286 	/* Check for ACPI processor speed limit */
287 	if (!ignore_acpi_limit && !eps_acpi_init()) {
288 		if (!acpi_processor_get_bios_limit(policy->cpu, &limit)) {
289 			pr_info("ACPI limit %u.%uGHz\n",
290 				limit/1000000,
291 				(limit%1000000)/10000);
292 			eps_acpi_exit(policy);
293 			/* Check if max_multiplier is in BIOS limits */
294 			if (limit && max_multiplier * fsb > limit) {
295 				pr_info("Aborting\n");
296 				return -EINVAL;
297 			}
298 		}
299 	}
300 #endif
301 
302 	/* Allow user to set lower maximum voltage then that reported
303 	 * by processor */
304 	if (brand == EPS_BRAND_C7M && set_max_voltage) {
305 		u32 v;
306 
307 		/* Change mV to something hardware can use */
308 		v = (set_max_voltage - 700) / 16;
309 		/* Check if voltage is within limits */
310 		if (v >= min_voltage && v <= max_voltage) {
311 			pr_info("Setting %dmV as maximum\n", v * 16 + 700);
312 			max_voltage = v;
313 		}
314 	}
315 
316 	/* Calc number of p-states supported */
317 	if (brand == EPS_BRAND_C7M)
318 		states = max_multiplier - min_multiplier + 1;
319 	else
320 		states = 2;
321 
322 	/* Allocate private data and frequency table for current cpu */
323 	centaur = kzalloc_flex(*centaur, freq_table, states + 1, GFP_KERNEL);
324 	if (!centaur)
325 		return -ENOMEM;
326 	eps_cpu[0] = centaur;
327 
328 	/* Copy basic values */
329 	centaur->fsb = fsb;
330 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
331 	centaur->bios_limit = limit;
332 #endif
333 
334 	/* Fill frequency and MSR value table */
335 	f_table = &centaur->freq_table[0];
336 	if (brand != EPS_BRAND_C7M) {
337 		f_table[0].frequency = fsb * min_multiplier;
338 		f_table[0].driver_data = (min_multiplier << 8) | min_voltage;
339 		f_table[1].frequency = fsb * max_multiplier;
340 		f_table[1].driver_data = (max_multiplier << 8) | max_voltage;
341 		f_table[2].frequency = CPUFREQ_TABLE_END;
342 	} else {
343 		k = 0;
344 		step = ((max_voltage - min_voltage) * 256)
345 			/ (max_multiplier - min_multiplier);
346 		for (i = min_multiplier; i <= max_multiplier; i++) {
347 			voltage = (k * step) / 256 + min_voltage;
348 			f_table[k].frequency = fsb * i;
349 			f_table[k].driver_data = (i << 8) | voltage;
350 			k++;
351 		}
352 		f_table[k].frequency = CPUFREQ_TABLE_END;
353 	}
354 
355 	policy->cpuinfo.transition_latency = 140000; /* 844mV -> 700mV in ns */
356 	policy->freq_table = &centaur->freq_table[0];
357 
358 	return 0;
359 }
360 
361 static void eps_cpu_exit(struct cpufreq_policy *policy)
362 {
363 	unsigned int cpu = policy->cpu;
364 
365 	/* Bye */
366 	kfree(eps_cpu[cpu]);
367 	eps_cpu[cpu] = NULL;
368 }
369 
370 static struct cpufreq_driver eps_driver = {
371 	.verify		= cpufreq_generic_frequency_table_verify,
372 	.target_index	= eps_target,
373 	.init		= eps_cpu_init,
374 	.exit		= eps_cpu_exit,
375 	.get		= eps_get,
376 	.name		= "e_powersaver",
377 };
378 
379 
380 /* This driver will work only on Centaur C7 processors with
381  * Enhanced SpeedStep/PowerSaver registers */
382 static const struct x86_cpu_id eps_cpu_id[] = {
383 	X86_MATCH_VENDOR_FAM_FEATURE(CENTAUR, 6, X86_FEATURE_EST, NULL),
384 	{}
385 };
386 MODULE_DEVICE_TABLE(x86cpu, eps_cpu_id);
387 
388 static int __init eps_init(void)
389 {
390 	if (!x86_match_cpu(eps_cpu_id) || boot_cpu_data.x86_model < 10)
391 		return -ENODEV;
392 	if (cpufreq_register_driver(&eps_driver))
393 		return -EINVAL;
394 	return 0;
395 }
396 
397 static void __exit eps_exit(void)
398 {
399 	cpufreq_unregister_driver(&eps_driver);
400 }
401 
402 /* Allow user to overclock his machine or to change frequency to higher after
403  * unloading module */
404 module_param(freq_failsafe_off, int, 0644);
405 MODULE_PARM_DESC(freq_failsafe_off, "Disable current vs max frequency check");
406 module_param(voltage_failsafe_off, int, 0644);
407 MODULE_PARM_DESC(voltage_failsafe_off, "Disable current vs max voltage check");
408 #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
409 module_param(ignore_acpi_limit, int, 0644);
410 MODULE_PARM_DESC(ignore_acpi_limit, "Don't check ACPI's processor speed limit");
411 #endif
412 module_param(set_max_voltage, int, 0644);
413 MODULE_PARM_DESC(set_max_voltage, "Set maximum CPU voltage (mV) C7-M only");
414 
415 MODULE_AUTHOR("Rafal Bilski <rafalbilski@interia.pl>");
416 MODULE_DESCRIPTION("Enhanced PowerSaver driver for VIA C7 CPU's.");
417 MODULE_LICENSE("GPL");
418 
419 module_init(eps_init);
420 module_exit(eps_exit);
421