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