summaryrefslogtreecommitdiff
blob: e48f4a022de16273f09d998b7dafa5e6e99e3175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
diff --new-file -a --unified=5 --recursive  linux-source-2.6.31_orig/arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c linux-source-2.6.31_phc/arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c
--- linux-source-2.6.30_orig/arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c	2009-07-14 09:15:09.864904000 +0200
+++ linux-source-2.6.30_phc/arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c	2009-07-18 09:31:50.687906676 +0200

@@ -23,10 +23,14 @@
  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  *
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  */
 
+/* This file has been patched with Linux PHC: www.linux-phc.org
+ * Patch version: linux-phc-0.3.2
+ */
+
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/smp.h>
 #include <linux/sched.h>
@@ -59,16 +63,21 @@
 	SYSTEM_IO_CAPABLE,
 };
 
 #define INTEL_MSR_RANGE		(0xffff)
 #define CPUID_6_ECX_APERFMPERF_CAPABILITY	(0x1)
+#define INTEL_MSR_VID_MASK	(0x00ff)
+#define INTEL_MSR_FID_MASK	(0xff00)
+#define INTEL_MSR_FID_SHIFT	(0x8)
+#define PHC_VERSION_STRING	"0.3.2:2"
 
 struct acpi_cpufreq_data {
 	struct acpi_processor_performance *acpi_data;
 	struct cpufreq_frequency_table *freq_table;
 	unsigned int resume;
 	unsigned int cpu_feature;
+	acpi_integer *original_controls;
 };
 
 static DEFINE_PER_CPU(struct acpi_cpufreq_data *, drv_data);
 
 struct acpi_msr_data {
@@ -108,17 +117,18 @@
 }
 
 static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
 {
 	int i;
+	u32 fid;
 	struct acpi_processor_performance *perf;
 
-	msr &= INTEL_MSR_RANGE;
+	fid = msr & INTEL_MSR_FID_MASK;
 	perf = data->acpi_data;
 
 	for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
-		if (msr == perf->states[data->freq_table[i].index].status)
+		if (fid == (perf->states[data->freq_table[i].index].status & INTEL_MSR_FID_MASK))
 			return data->freq_table[i].frequency;
 	}
 	return data->freq_table[0].frequency;
 }
 
@@ -776,10 +786,12 @@
 	if (data) {
 		cpufreq_frequency_table_put_attr(policy->cpu);
 		per_cpu(drv_data, policy->cpu) = NULL;
 		acpi_processor_unregister_performance(data->acpi_data,
 						      policy->cpu);
+		if (data->original_controls)
+			kfree(data->original_controls);
 		kfree(data);
 	}
 
 	return 0;
 }
@@ -793,12 +805,474 @@
 	data->resume = 1;
 
 	return 0;
 }
 
+/* sysfs interface to change operating points voltages */
+
+static unsigned int extract_fid_from_control(unsigned int control)
+{
+	return ((control & INTEL_MSR_FID_MASK) >> INTEL_MSR_FID_SHIFT);
+}
+
+static unsigned int extract_vid_from_control(unsigned int control)
+{
+	return (control & INTEL_MSR_VID_MASK);
+}
+
+
+static bool check_cpu_control_capability(struct acpi_cpufreq_data *data) {
+ /* check if the cpu we are running on is capable of setting new control data
+  * 
+  */
+	if (unlikely(data == NULL || 
+	             data->acpi_data == NULL || 
+	             data->freq_table == NULL ||
+	             data->cpu_feature != SYSTEM_INTEL_MSR_CAPABLE)) {
+		return false;
+	} else {
+		return true;
+	};
+}
+
+
+static ssize_t check_origial_table (struct acpi_cpufreq_data *data)
+{
+
+	struct acpi_processor_performance *acpi_data;
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int state_index;
+
+	acpi_data = data->acpi_data;
+	freq_table = data->freq_table;
+
+	if (data->original_controls == NULL) {
+		// Backup original control values
+		data->original_controls = kcalloc(acpi_data->state_count,
+		                                  sizeof(acpi_integer), GFP_KERNEL);
+		if (data->original_controls == NULL) {
+			printk("failed to allocate memory for original control values\n");
+			return -ENOMEM;
+		}
+		for (state_index = 0; state_index < acpi_data->state_count; state_index++) {
+			data->original_controls[state_index] = acpi_data->states[state_index].control;
+		}
+	}
+	return 0;
+}
+
+static ssize_t show_freq_attr_vids(struct cpufreq_policy *policy, char *buf)
+ /* display phc's voltage id's
+  * 
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct acpi_processor_performance *acpi_data;
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int i;
+	unsigned int vid;
+	ssize_t count = 0;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV; //check if CPU is capable of changing controls
+
+	acpi_data = data->acpi_data;
+	freq_table = data->freq_table;
+
+	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
+		vid = extract_vid_from_control(acpi_data->states[freq_table[i].index].control);
+		count += sprintf(&buf[count], "%u ", vid);
+	}
+	count += sprintf(&buf[count], "\n");
+
+	return count;
+}
+
+static ssize_t show_freq_attr_default_vids(struct cpufreq_policy *policy, char *buf)
+ /* display acpi's default voltage id's
+  * 
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int i;
+	unsigned int vid;
+	ssize_t count = 0;
+	ssize_t retval;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV; //check if CPU is capable of changing controls
+
+	retval = check_origial_table(data);
+        if (0 != retval)
+		return retval; 
+
+	freq_table = data->freq_table;
+
+	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
+		vid = extract_vid_from_control(data->original_controls[freq_table[i].index]);
+		count += sprintf(&buf[count], "%u ", vid);
+	}
+	count += sprintf(&buf[count], "\n");
+
+	return count;
+}
+
+static ssize_t show_freq_attr_fids(struct cpufreq_policy *policy, char *buf)
+ /* display phc's frequeny id's
+  * 
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct acpi_processor_performance *acpi_data;
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int i;
+	unsigned int fid;
+	ssize_t count = 0;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV; //check if CPU is capable of changing controls
+
+	acpi_data = data->acpi_data;
+	freq_table = data->freq_table;
+
+	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
+		fid = extract_fid_from_control(acpi_data->states[freq_table[i].index].control);
+		count += sprintf(&buf[count], "%u ", fid);
+	}
+	count += sprintf(&buf[count], "\n");
+
+	return count;
+}
+
+static ssize_t show_freq_attr_controls(struct cpufreq_policy *policy, char *buf)
+ /* display phc's controls for the cpu (frequency id's and related voltage id's)
+  * 
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct acpi_processor_performance *acpi_data;
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int i;
+	unsigned int fid;
+	unsigned int vid;
+	ssize_t count = 0;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV; //check if CPU is capable of changing controls
+
+	acpi_data = data->acpi_data;
+	freq_table = data->freq_table;
+
+	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
+		fid = extract_fid_from_control(acpi_data->states[freq_table[i].index].control);
+		vid = extract_vid_from_control(acpi_data->states[freq_table[i].index].control);
+		count += sprintf(&buf[count], "%u:%u ", fid, vid);
+	}
+	count += sprintf(&buf[count], "\n");
+
+	return count;
+}
+
+static ssize_t show_freq_attr_default_controls(struct cpufreq_policy *policy, char *buf)
+ /* display acpi's default controls for the cpu (frequency id's and related voltage id's)
+  * 
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int i;
+	unsigned int fid;
+	unsigned int vid;
+	ssize_t count = 0;
+	ssize_t retval;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV; //check if CPU is capable of changing controls
+
+	retval = check_origial_table(data);
+        if (0 != retval)
+		return retval; 
+
+	freq_table = data->freq_table;
+
+	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
+		fid = extract_fid_from_control(data->original_controls[freq_table[i].index]);
+		vid = extract_vid_from_control(data->original_controls[freq_table[i].index]);
+		count += sprintf(&buf[count], "%u:%u ", fid, vid);
+	}
+	count += sprintf(&buf[count], "\n");
+
+	return count;
+}
+
+
+static ssize_t store_freq_attr_vids(struct cpufreq_policy *policy, const char *buf, size_t count)
+ /* store the voltage id's for the related frequency
+  * We are going to do some sanity checks here to prevent users 
+  * from setting higher voltages than the default one.
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct acpi_processor_performance *acpi_data;
+	struct cpufreq_frequency_table *freq_table;
+	unsigned int freq_index;
+	unsigned int state_index;
+	unsigned int new_vid;
+	unsigned int original_vid;
+	unsigned int new_control;
+	unsigned int original_control;
+	const char *curr_buf = buf;
+	char *next_buf;
+	ssize_t retval;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV; //check if CPU is capable of changing controls
+
+	retval = check_origial_table(data);
+        if (0 != retval)
+		return retval; 
+
+	acpi_data = data->acpi_data;
+	freq_table = data->freq_table;
+
+	/* for each value taken from the sysfs interfalce (phc_vids) get entrys and convert them to unsigned long integers*/
+	for (freq_index = 0; freq_table[freq_index].frequency != CPUFREQ_TABLE_END; freq_index++) {
+		new_vid = simple_strtoul(curr_buf, &next_buf, 10);
+		if (next_buf == curr_buf) {
+			if ((curr_buf - buf == count - 1) && (*curr_buf == '\n')) {   //end of line?
+				curr_buf++;
+				break;
+			}
+			//if we didn't got end of line but there is nothing more to read something went wrong...
+			printk("failed to parse vid value at %i (%s)\n", freq_index, curr_buf);
+			return -EINVAL;
+		}
+
+		state_index = freq_table[freq_index].index;
+		original_control = data->original_controls[state_index];
+		original_vid = original_control & INTEL_MSR_VID_MASK;
+		
+		/* before we store the values we do some checks to prevent 
+		 * users to set up values higher than the default one
+		 */
+		if (new_vid <= original_vid) {
+			new_control = (original_control & ~INTEL_MSR_VID_MASK) | new_vid;
+			dprintk("setting control at %i to %x (default is %x)\n",
+			        freq_index, new_control, original_control);
+			acpi_data->states[state_index].control = new_control;
+
+		} else {
+			printk("skipping vid at %i, %u is greater than default %u\n",
+			       freq_index, new_vid, original_vid);
+		}
+
+		curr_buf = next_buf;
+		/* jump over value seperators (space or comma).
+		 * There could be more than one space or comma character
+		 * to separate two values so we better do it using a loop.
+		 */
+		while ((curr_buf - buf < count) && ((*curr_buf == ' ') || (*curr_buf == ','))) {
+			curr_buf++;
+		}
+	}
+
+	/* set new voltage for current frequency */
+	data->resume = 1;
+	acpi_cpufreq_target(policy, get_cur_freq_on_cpu(policy->cpu), CPUFREQ_RELATION_L);
+
+	return curr_buf - buf;
+}
+
+static ssize_t store_freq_attr_controls(struct cpufreq_policy *policy, const char *buf, size_t count)
+ /* store the controls (frequency id's and related voltage id's)
+  * We are going to do some sanity checks here to prevent users 
+  * from setting higher voltages than the default one.
+  */
+{
+	struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+	struct acpi_processor_performance *acpi_data;
+	struct cpufreq_frequency_table *freq_table;
+	const char   *curr_buf;
+	unsigned int  op_count;
+	unsigned int  state_index;
+	int           isok;
+	char         *next_buf;
+	ssize_t       retval;
+	unsigned int  new_vid;
+	unsigned int  original_vid;
+	unsigned int  new_fid;
+	unsigned int  old_fid;
+	unsigned int  original_control;
+	unsigned int  old_control;
+	unsigned int  new_control;
+	int           found;
+
+	if (!check_cpu_control_capability(data)) return -ENODEV;
+
+	retval = check_origial_table(data);
+        if (0 != retval)
+		return retval;
+
+	acpi_data = data->acpi_data;
+	freq_table = data->freq_table;
+
+	op_count = 0;
+	curr_buf = buf;
+	next_buf = NULL;
+	isok     = 1;
+	
+	while ( (isok) && (curr_buf != NULL) )
+	{
+		op_count++;
+		// Parse fid
+		new_fid = simple_strtoul(curr_buf, &next_buf, 10);
+		if ((next_buf != curr_buf) && (next_buf != NULL))
+		{
+			// Parse separator between frequency and voltage 
+			curr_buf = next_buf;
+			next_buf = NULL;
+			if (*curr_buf==':')
+			{
+				curr_buf++;
+				// Parse vid
+				new_vid = simple_strtoul(curr_buf, &next_buf, 10);
+				if ((next_buf != curr_buf) && (next_buf != NULL))
+				{
+					found = 0;
+					for (state_index = 0; state_index < acpi_data->state_count; state_index++) {
+						old_control = acpi_data->states[state_index].control;
+						old_fid = extract_fid_from_control(old_control);
+						if (new_fid == old_fid)
+						{
+							found = 1;
+							original_control = data->original_controls[state_index];
+							original_vid = extract_vid_from_control(original_control);
+							if (new_vid <= original_vid)
+							{
+								new_control = (original_control & ~INTEL_MSR_VID_MASK) | new_vid;
+								dprintk("setting control at %i to %x (default is %x)\n",
+								        state_index, new_control, original_control);
+								acpi_data->states[state_index].control = new_control;
+
+							} else {
+								printk("skipping vid at %i, %u is greater than default %u\n",
+								       state_index, new_vid, original_vid);
+							}
+						}
+					}
+
+					if (found == 0)
+					{
+						printk("operating point # %u not found (FID = %u)\n", op_count, new_fid);
+						isok = 0;
+					}
+
+					// Parse seprator before next operating point, if any
+					curr_buf = next_buf;
+					next_buf = NULL;
+					if ((*curr_buf == ',') || (*curr_buf == ' '))
+						curr_buf++;
+					else
+						curr_buf = NULL;
+				}
+				else
+				{
+					printk("failed to parse VID of operating point # %u (%s)\n", op_count, curr_buf);
+					isok = 0;
+				}
+			}
+			else
+			{
+				printk("failed to parse operating point # %u (%s)\n", op_count, curr_buf);
+				isok = 0;
+			}
+		}
+		else
+		{
+			printk("failed to parse FID of operating point # %u (%s)\n", op_count, curr_buf);
+			isok = 0;
+		}
+	}
+
+	if (isok)
+	{
+		retval = count;
+		/* set new voltage at current frequency */
+		data->resume = 1;
+		acpi_cpufreq_target(policy, get_cur_freq_on_cpu(policy->cpu), CPUFREQ_RELATION_L);
+	}
+	else
+	{
+		retval = -EINVAL;
+	}
+
+	return retval;
+}
+
+static ssize_t show_freq_attr_phc_version(struct cpufreq_policy *policy, char *buf)
+ /* print out the phc version string set at the beginning of that file
+  */
+{
+	ssize_t count = 0;
+	count += sprintf(&buf[count], "%s\n", PHC_VERSION_STRING);
+	return count;
+}
+
+
+
+static struct freq_attr cpufreq_freq_attr_phc_version =
+{
+	/*display phc's version string*/
+       .attr = { .name = "phc_version", .mode = 0444, .owner = THIS_MODULE },
+       .show = show_freq_attr_phc_version,
+       .store = NULL,
+};
+
+static struct freq_attr cpufreq_freq_attr_vids =
+{
+	/*display phc's voltage id's for the cpu*/
+       .attr = { .name = "phc_vids", .mode = 0644, .owner = THIS_MODULE },
+       .show = show_freq_attr_vids,
+       .store = store_freq_attr_vids,
+};
+
+static struct freq_attr cpufreq_freq_attr_default_vids =
+{
+	/*display acpi's default frequency id's for the cpu*/
+       .attr = { .name = "phc_default_vids", .mode = 0444, .owner = THIS_MODULE },
+       .show = show_freq_attr_default_vids,
+       .store = NULL,
+};
+
+static struct freq_attr cpufreq_freq_attr_fids =
+{
+	/*display phc's default frequency id's for the cpu*/
+       .attr = { .name = "phc_fids", .mode = 0444, .owner = THIS_MODULE },
+       .show = show_freq_attr_fids,
+       .store = NULL,
+};
+
+static struct freq_attr cpufreq_freq_attr_controls =
+{
+	/*display phc's current voltage/frequency controls for the cpu*/
+       .attr = { .name = "phc_controls", .mode = 0644, .owner = THIS_MODULE },
+       .show = show_freq_attr_controls,
+       .store = store_freq_attr_controls,
+};
+
+static struct freq_attr cpufreq_freq_attr_default_controls =
+{
+	/*display acpi's default voltage/frequency controls for the cpu*/
+       .attr = { .name = "phc_default_controls", .mode = 0444, .owner = THIS_MODULE },
+       .show = show_freq_attr_default_controls,
+       .store = NULL,
+};
+
+
 static struct freq_attr *acpi_cpufreq_attr[] = {
-	&cpufreq_freq_attr_scaling_available_freqs,
+ 	&cpufreq_freq_attr_scaling_available_freqs,
+	&cpufreq_freq_attr_phc_version,
+	&cpufreq_freq_attr_vids,
+	&cpufreq_freq_attr_default_vids,
+	&cpufreq_freq_attr_fids,
+	&cpufreq_freq_attr_controls,
+	&cpufreq_freq_attr_default_controls,
 	NULL,
 };
 
 static struct cpufreq_driver acpi_cpufreq_driver = {
 	.verify = acpi_cpufreq_verify,