-
Notifications
You must be signed in to change notification settings - Fork 3
/
undervolt.c
557 lines (473 loc) · 14 KB
/
undervolt.c
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
552
553
554
555
556
557
/*
* Copyright (c) 2010 Albert Lee <trisk@forkgnu.org>.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/mutex.h>
#include <asm/uaccess.h>
#define UNDERVOLT_NAME "undervolt"
#define UNDERVOLT_DESCRIPTION "Undervolt driver for HTC/Qualcomm devices"
#define UNDERVOLT_VERSION "1.0a"
MODULE_VERSION(UNDERVOLT_VERSION);
MODULE_LICENSE("Dual MIT/GPL");
MODULE_ALIAS(UNDERVOLT_NAME);
MODULE_DESCRIPTION(UNDERVOLT_DESCRIPTION);
MODULE_AUTHOR("Albert Lee <trisk@forkgnu.org>");
struct clkctl_acpu_speed {
unsigned acpu_khz;
unsigned clk_cfg;
unsigned clk_sel;
unsigned sc_l_value;
unsigned lpj;
int vdd;
unsigned axiclk_khz;
};
#define CPUFREQ_ENTRY_INVALID ~0
#define CPUFREQ_TABLE_END ~1
struct cpufreq_frequency_table {
unsigned index;
unsigned frequency;
};
struct uv_map {
unsigned acpu_khz;
int vdd;
int uv_vdd;
};
struct oc_map {
unsigned frequency;
unsigned oc_frequency;
};
struct m_search_pat {
unsigned word;
unsigned mask;
};
extern int regulator_set_voltage(void *, int, int);
extern void cpufreq_frequency_table_get_attr(void *, int);
#define PROC_NAME UNDERVOLT_NAME
static struct proc_dir_entry *proc_entry = NULL;
static char read_buf[16];
#define UV_ENTRY_MAX 31
static struct uv_map undervolt_tbl[UV_ENTRY_MAX + 1];
static DEFINE_MUTEX(uv_mutex);
static int vdd_undervolt = 0;
static struct clkctl_acpu_speed *acpu_freq_tbl = NULL;
static struct cpufreq_frequency_table *freq_table = NULL;
#if 0
/*
b4: e3a0301c mov r3, #28 ; 0x1c
b8: e3a01000 mov r1, #0 ; 0x0
bc: e0020293 mul r2, r3, r2
c0: e59f3040 ldr r3, [pc, #64] ; 108
c4: e7831002 str r1, [r3, r2]
*/
static struct m_search_pat fixup_pat[] = {
{ 0xe3a0001c, 0xffff0fff }, /* e3a0?01c */
{ 0xe3a00000, 0xffff0fff }, /* e3a0?000 */
{ 0xe0000090, 0xfff0f0f0 }, /* e00?0?9? */
{ 0xe59f0000, 0xffff0000 }, /* e59f???? */
{ 0 },
};
static unsigned long fixup_addr = 0;
static unsigned long fixup_size = DEFAULT_FIXUP_SIZE;
#endif
static unsigned long acpuclk_set_rate_addr = 0;
#define DEFAULT_ASR_DEPTH (16 * sizeof(unsigned))
static unsigned long acpuclk_set_rate_depth = DEFAULT_ASR_DEPTH;
#define DEFAULT_CFTGA_DEPTH (2 * sizeof(unsigned))
static unsigned long cpufreq_frequency_table_get_attr_depth =
DEFAULT_CFTGA_DEPTH;
#define DEFAULT_RSV_DEPTH (48 * sizeof(unsigned))
static unsigned long regulator_set_voltage_depth = DEFAULT_RSV_DEPTH;
/*
27c: e59f41d0 ldr r4, [pc, #464] ; 454 <acpuclk_set_rate+0x204>
280: e5942000 ldr r2, [r4]
*/
static struct m_search_pat acpuclk_set_rate_pat[] = {
{ 0xe59f0000, 0xffff0000 },
{ 0xe5900000, 0xfff00fff },
{ 0 },
};
static struct m_search_pat cpufreq_frequency_table_get_attr_pat[] = {
{ 0xe59f0000, 0xffff0000 },
{ 0xe5800000, 0xfff0ffff },
{ 0 },
};
/*
2028: e1570001 cmp r7, r1
202c: b1a07001 movlt r7, r1
2030: e1580002 cmp r8, r2
2034: b1a02008 movlt r2, r8
2038: a1a02002 movge r2, r2
*/
static struct m_search_pat regulator_set_voltage_pat[] = {
{ 0xe1500000, 0xfff0fff0 },
{ 0xb1a00000, 0xffff0ff0 },
{ 0xe1500000, 0xfff0fff0 },
{ 0xb1a00000, 0xffff0ff0 },
{ 0xa1a00000, 0xffff0ff0 },
{ 0 },
};
static int __init undervolt_init(void);
static void __exit undervolt_exit(void);
static int undervolt_read(char *page, char **start, off_t off, int count,
int *eof, void *data);
static int undervolt_write(struct file *filp, const char __user *buf,
unsigned long len, void *data);
static unsigned *m_search(struct m_search_pat *n, unsigned *hp, unsigned *l);
static struct clkctl_acpu_speed *get_freq_tbl(struct m_search_pat *pat,
void *addr, unsigned long depth);
static struct cpufreq_frequency_table *get_freq_table(struct m_search_pat *pat,
void *addr,
unsigned long depth);
static int patch_reg_set_voltage(struct m_search_pat *pat, void *addr,
unsigned long depth);
static int init_uv_tbl(struct uv_map *uv_tbl,
struct clkctl_acpu_speed *freq_tbl);
static int update_uv_tbl(struct uv_map *uv_tbl, int vdd, int uv_vdd);
static void update_uv_tbl_adjust(struct uv_map *uv_tbl, int adj);
static int update_freq_tbl(struct clkctl_acpu_speed *freq_tbl,
struct uv_map *uv_tbl, int enable);
module_param(acpuclk_set_rate_addr, long, S_IRUGO);
MODULE_PARM_DESC(acpuclk_set_rate_addr, "acpuclk_set_rate function address");
module_param(acpuclk_set_rate_depth, long, S_IRUGO);
MODULE_PARM_DESC(acpuclk_set_rate_depth,
"acpuclk_set_rate function search depth");
module_param(cpufreq_frequency_table_get_attr_depth, long, S_IRUGO);
MODULE_PARM_DESC(cpufreq_frequency_table_get_attr_depth,
"cpufreq_frequency_table_get_attr function search depth");
module_param(regulator_set_voltage_depth, long, S_IRUGO);
MODULE_PARM_DESC(regulator_set_voltage_depth,
"regulator_set_voltage function search depth");
module_param(vdd_undervolt, int, S_IRUGO);
MODULE_PARM_DESC(vdd_undervolt, "mV to decrease all voltages by");
module_init(undervolt_init);
module_exit(undervolt_exit);
int undervolt_init(void)
{
printk(KERN_INFO "%s: %s %s\n", UNDERVOLT_NAME, UNDERVOLT_DESCRIPTION,
UNDERVOLT_VERSION);
printk(KERN_INFO "%s: (c) 2010 Albert Lee <trisk@forkgnu.org>\n",
UNDERVOLT_NAME);
if (acpuclk_set_rate_addr == 0) {
printk(KERN_ERR "%s: Missing required parameter " \
"`acpuclk_set_rate_addr'\n",
__func__);
return -EINVAL;
}
proc_entry = create_proc_entry(PROC_NAME, S_IFREG | S_IWUSR | S_IRUGO,
NULL);
if (proc_entry == NULL) {
printk(KERN_ERR "%s: Unable to create /proc/%s\n",
__func__, PROC_NAME);
return -ENOMEM;
}
proc_entry->read_proc = undervolt_read;
proc_entry->write_proc = undervolt_write;
acpu_freq_tbl = get_freq_tbl(acpuclk_set_rate_pat,
(void *)acpuclk_set_rate_addr,
acpuclk_set_rate_depth);
if (acpu_freq_tbl == NULL) {
printk(KERN_ERR "%s: acpu_freq_tbl not found\n", __func__);
undervolt_exit();
return -ENXIO;
}
freq_table = get_freq_table(cpufreq_frequency_table_get_attr_pat,
(void *)cpufreq_frequency_table_get_attr,
cpufreq_frequency_table_get_attr_depth);
if (freq_table == NULL) {
printk(KERN_INFO "%s: freq_table for cpufreq not found, " \
"overclocking will not be possible\n", UNDERVOLT_NAME);
}
if (patch_reg_set_voltage(regulator_set_voltage_pat,
(void *)regulator_set_voltage,
regulator_set_voltage_depth) != 0) {
printk(KERN_INFO "%s: Unable to patch regulator_set_voltage, " \
"voltage will be subject to board constraints\n",
UNDERVOLT_NAME);
}
init_uv_tbl(undervolt_tbl, acpu_freq_tbl);
if (vdd_undervolt > 0) {
update_uv_tbl_adjust(undervolt_tbl, -1 * vdd_undervolt);
update_freq_tbl(acpu_freq_tbl, undervolt_tbl, 1);
}
return 0;
}
void undervolt_exit(void)
{
if (acpu_freq_tbl != NULL) {
update_freq_tbl(acpu_freq_tbl, undervolt_tbl, 0);
}
if (proc_entry) {
remove_proc_entry(PROC_NAME, NULL);
proc_entry = NULL;
}
return;
}
int undervolt_read(char *page, char **start, off_t off, int count, int *eof,
void *data)
{
int len = 0;
struct uv_map *uv_tbl = undervolt_tbl;
if ((count == 0) || (off > 0)) {
return 0;
}
mutex_lock(&uv_mutex);
for (; uv_tbl->acpu_khz != 0; uv_tbl++) {
len += snprintf(page + len, count - len, "%4d %4d %8u\n",
uv_tbl->vdd, uv_tbl->uv_vdd, uv_tbl->acpu_khz);
if (len >= count) {
len = count;
break;
}
}
mutex_unlock(&uv_mutex);
*eof = (len > count) ? 0 : 1;
return len;
}
int undervolt_write(struct file *filp, const char __user *buf,
unsigned long len, void *data)
{
int i = 0, j;
int vdd[2] = { 0, 0 };
int sign = 0;
if (len > sizeof(read_buf)) {
return -ENOSPC;
}
if (len == 0) {
return 0;
}
if (copy_from_user(read_buf, buf, len) != 0) {
return -EINVAL;
}
if ((len == 1) || (read_buf[1] == '\n')) {
switch (read_buf[0]) {
case '0':
case '1':
i = read_buf[0] - '0';
mutex_lock(&uv_mutex);
update_freq_tbl(acpu_freq_tbl, undervolt_tbl, i);
if (i == 0) {
init_uv_tbl(undervolt_tbl, acpu_freq_tbl);
}
mutex_unlock(&uv_mutex);
printk(KERN_INFO "%s: acpu_freq_tbl updated\n",
UNDERVOLT_NAME);
default:
return len;
}
}
if (read_buf[0] == '-') {
sign = -1;
i++;
} else if (read_buf[0] == '+') {
sign = 1;
i++;
}
for (j = 0; i < len; i++) {
char c = read_buf[i];
if ((c >= '0') && (c <= '9')) {
vdd[j] *= 10;
vdd[j] += (c - '0');
} else if ((c == ' ') || (c == '\t')) {
if (vdd[j] != 0) {
j++;
if ((sign != 0) || (j > 1)) {
break;
}
}
} else {
break;
}
}
if (sign != 0) {
mutex_lock(&uv_mutex);
update_uv_tbl_adjust(undervolt_tbl, sign * vdd[0]);
mutex_unlock(&uv_mutex);
} else if (vdd[0] > 0 && vdd[1] > 0) {
mutex_lock(&uv_mutex);
update_uv_tbl(undervolt_tbl, vdd[0], vdd[1]);
mutex_unlock(&uv_mutex);
}
return len;
}
unsigned *m_search(struct m_search_pat *n, unsigned *hp, unsigned *l)
{
struct m_search_pat *np = n;
for (; hp < l; hp++) {
#ifdef DEBUG
printk(KERN_DEBUG "%s: %p: %08x\n", __func__, hp, *hp);
#endif
if ((*hp & np->mask) == np->word) {
#ifdef DEBUG
printk(KERN_DEBUG "%s: matches: %08x\n", __func__,
*hp & np->mask);
#endif
np++;
if ((np->word == 0) && (np->mask == 0)) {
/* Return beginning of matched sequence */
np--;
return hp - (np - n);
}
} else {
np = n;
}
}
return NULL;
}
/*
enum {
SRC_RAW,
SRC_SCPLL,
SRC_AXI,
SRC_PLL1,
};
static const unsigned match[] = {
19200, 0, SRC_RAW, 0, (unsigned)-1, 1050, 14000,
128000, 0, SRC_AXI, 0, (unsigned)-1, 1050, 14000,
};
*/
struct clkctl_acpu_speed *get_freq_tbl(struct m_search_pat *pat, void *addr,
unsigned long depth)
{
unsigned *insn;
void **tblp;
struct clkctl_acpu_speed *tbl;
insn = m_search(pat, addr, addr + depth);
if (insn == NULL) {
return NULL;
}
/* e59f?XXX ldr */
tblp = (void **)((unsigned)insn + (*insn & 0xfff) + 0x8);
tbl = (struct clkctl_acpu_speed *)(*tblp);
if ((tbl->acpu_khz != 19200) || (tbl->axiclk_khz != 14000)) {
return NULL;
}
return tbl;
}
struct cpufreq_frequency_table *get_freq_table(struct m_search_pat *pat,
void *addr,
unsigned long depth)
{
unsigned *insn;
void **tblp;
struct cpufreq_frequency_table *tbl;
insn = m_search(pat, addr, addr + depth);
if (insn == NULL) {
return NULL;
}
/* e59f?XXX ldr */
tblp = (void **)((unsigned)insn + (*insn & 0xfff) + 0x8);
tbl = (struct cpufreq_frequency_table *)(*tblp);
if ((tbl->index != 0) || (tbl->frequency != CPUFREQ_ENTRY_INVALID)) {
return NULL;
}
return tbl;
}
int patch_reg_set_voltage(struct m_search_pat *pat, void *addr,
unsigned long depth)
{
unsigned *insn;
#ifdef DEBUG
printk(KERN_DEBUG "%s: regulator_set_voltage: 0x%p\n", __func__, addr);
#endif
insn = m_search(pat, addr, addr + depth);
if (insn == NULL) {
return -1;
}
do {
switch (*insn & 0xfff00000) {
case 0xa1a00000: /* movgt */
case 0xb1a00000: /* movlt */
case 0xe1500000: /* cmp */
*insn = 0xe1500000; /* cmp r0, r0 */
insn++;
break;
default:
goto done;
}
} while (--depth);
done:
return 0;
}
int init_uv_tbl(struct uv_map *uv_tbl, struct clkctl_acpu_speed *freq_tbl)
{
struct uv_map *uv_tbl_end = &uv_tbl[UV_ENTRY_MAX];
for (; freq_tbl->acpu_khz != 0; freq_tbl++) {
if (uv_tbl == uv_tbl_end) {
break;
}
uv_tbl->acpu_khz = freq_tbl->acpu_khz;
uv_tbl->vdd = uv_tbl->uv_vdd = freq_tbl->vdd;
uv_tbl++;
}
uv_tbl->acpu_khz = 0;
return 0;
}
int update_uv_tbl(struct uv_map *uv_tbl, int vdd, int uv_vdd)
{
if ((vdd == 0) || (uv_vdd == 0)) {
return -1;
}
for (; uv_tbl->acpu_khz != 0; uv_tbl++) {
if (uv_tbl->vdd == vdd) {
uv_tbl->uv_vdd = uv_vdd;
} else if (uv_tbl->vdd > vdd) {
break;
}
}
return 0;
}
int update_freq_tbl(struct clkctl_acpu_speed *freq_tbl, struct uv_map *uv_tbl,
int enable)
{
for (; freq_tbl->acpu_khz != 0; freq_tbl++) {
for (; uv_tbl->acpu_khz != 0; uv_tbl++) {
if (uv_tbl->acpu_khz == freq_tbl->acpu_khz) {
if (enable) {
freq_tbl->vdd = uv_tbl->uv_vdd;
} else {
freq_tbl->vdd = uv_tbl->vdd;
}
break;
}
}
}
return 0;
}
void update_uv_tbl_adjust(struct uv_map *uv_tbl, int adj)
{
for (; uv_tbl->acpu_khz != 0; uv_tbl++) {
uv_tbl->uv_vdd += adj;
}
return;
}