/
cpufreq_lulzactive.c
1174 lines (993 loc) · 30.1 KB
/
cpufreq_lulzactive.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* drivers/cpufreq/cpufreq_lulzactive.c
*
* Copyright (C) 2010 Google, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Mike Chan (mike@android.com)
* Edited: Tegrak (luciferanna@gmail.com)
*
* Driver values in /sys/devices/system/cpu/cpufreq/lulzactive
*
*/
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/cpufreq.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/tick.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/earlysuspend.h>
#include <asm/cputime.h>
#include <linux/suspend.h>
#define LULZACTIVE_VERSION (2)
#define LULZACTIVE_AUTHOR "tegrak"
// if you changed some codes for optimization, just write your name here.
#define LULZACTIVE_TUNER ""
#define LOGI(fmt...) printk(KERN_INFO "[lulzactive] " fmt)
#define LOGW(fmt...) printk(KERN_WARNING "[lulzactive] " fmt)
#define LOGD(fmt...) printk(KERN_DEBUG "[lulzactive] " fmt)
static void (*pm_idle_old)(void);
static atomic_t active_count = ATOMIC_INIT(0);
struct cpufreq_lulzactive_cpuinfo {
struct timer_list cpu_timer;
int timer_idlecancel;
u64 time_in_idle;
u64 idle_exit_time;
u64 timer_run_time;
int idling;
u64 freq_change_time;
u64 freq_change_time_in_idle;
struct cpufreq_policy *policy;
struct cpufreq_frequency_table *freq_table;
unsigned int freq_table_size;
unsigned int target_freq;
int governor_enabled;
};
static DEFINE_PER_CPU(struct cpufreq_lulzactive_cpuinfo, cpuinfo);
/* Workqueues handle frequency scaling */
static struct task_struct *up_task;
static struct workqueue_struct *down_wq;
static struct work_struct freq_scale_down_work;
static cpumask_t up_cpumask;
static spinlock_t up_cpumask_lock;
static cpumask_t down_cpumask;
static spinlock_t down_cpumask_lock;
/*
* The minimum amount of time to spend at a frequency before we can step up.
*/
#define DEFAULT_UP_SAMPLE_TIME 24000
static unsigned long up_sample_time;
/*
* The minimum amount of time to spend at a frequency before we can step down.
*/
#define DEFAULT_DOWN_SAMPLE_TIME 49000
static unsigned long down_sample_time;
/*
* DEBUG print flags
*/
static unsigned long debug_mode;
enum {
LULZACTIVE_DEBUG_EARLY_SUSPEND=1,
LULZACTIVE_DEBUG_START_STOP=2,
LULZACTIVE_DEBUG_LOAD=4,
LULZACTIVE_DEBUG_SUSPEND=8,
};
#define DEFAULT_DEBUG_MODE (LULZACTIVE_DEBUG_EARLY_SUSPEND | LULZACTIVE_DEBUG_START_STOP | LULZACTIVE_DEBUG_SUSPEND)
/*
* CPU freq will be increased if measured load > inc_cpu_load;
*/
#define DEFAULT_INC_CPU_LOAD 60
static unsigned long inc_cpu_load;
/*
* CPU freq will be decreased if measured load < dec_cpu_load;
* not implemented yet.
*/
#define DEFAULT_DEC_CPU_LOAD 30
static unsigned long dec_cpu_load;
/*
* Increasing frequency table index
* zero disables and causes to always jump straight to max frequency.
*/
#define DEFAULT_PUMP_UP_STEP 1
static unsigned long pump_up_step;
/*
* Decreasing frequency table index
* zero disables and will calculate frequency according to load heuristic.
*/
#define DEFAULT_PUMP_DOWN_STEP 1
static unsigned long pump_down_step;
/*
* Use minimum frequency while suspended.
*/
static unsigned int suspending;
static unsigned int early_suspended;
#define SCREEN_OFF_LOWEST_STEP (0xffffffff)
#define DEFAULT_SCREEN_OFF_MIN_STEP (SCREEN_OFF_LOWEST_STEP)
static unsigned long screen_off_min_step;
#define DEBUG 0
#define BUFSZ 128
#if DEBUG
#include <linux/proc_fs.h>
struct dbgln {
int cpu;
unsigned long jiffy;
unsigned long run;
char buf[BUFSZ];
};
#define NDBGLNS 256
static struct dbgln dbgbuf[NDBGLNS];
static int dbgbufs;
static int dbgbufe;
static struct proc_dir_entry *dbg_proc;
static spinlock_t dbgpr_lock;
static u64 up_request_time;
static unsigned int up_max_latency;
static void dbgpr(char *fmt, ...)
{
va_list args;
int n;
unsigned long flags;
spin_lock_irqsave(&dbgpr_lock, flags);
n = dbgbufe;
va_start(args, fmt);
vsnprintf(dbgbuf[n].buf, BUFSZ, fmt, args);
va_end(args);
dbgbuf[n].cpu = smp_processor_id();
dbgbuf[n].run = nr_running();
dbgbuf[n].jiffy = jiffies;
if (++dbgbufe >= NDBGLNS)
dbgbufe = 0;
if (dbgbufe == dbgbufs)
if (++dbgbufs >= NDBGLNS)
dbgbufs = 0;
spin_unlock_irqrestore(&dbgpr_lock, flags);
}
static void dbgdump(void)
{
int i, j;
unsigned long flags;
static struct dbgln prbuf[NDBGLNS];
spin_lock_irqsave(&dbgpr_lock, flags);
i = dbgbufs;
j = dbgbufe;
memcpy(prbuf, dbgbuf, sizeof(dbgbuf));
dbgbufs = 0;
dbgbufe = 0;
spin_unlock_irqrestore(&dbgpr_lock, flags);
while (i != j)
{
printk("%lu %d %lu %s",
prbuf[i].jiffy, prbuf[i].cpu, prbuf[i].run,
prbuf[i].buf);
if (++i == NDBGLNS)
i = 0;
}
}
static int dbg_proc_read(char *buffer, char **start, off_t offset,
int count, int *peof, void *dat)
{
printk("max up_task latency=%uus\n", up_max_latency);
dbgdump();
*peof = 1;
return 0;
}
#else
#define dbgpr(...) do {} while (0)
#endif
static int cpufreq_governor_lulzactive(struct cpufreq_policy *policy,
unsigned int event);
#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_LULZACTIVE
static
#endif
struct cpufreq_governor cpufreq_gov_lulzactive = {
.name = "lulzactive",
.governor = cpufreq_governor_lulzactive,
.max_transition_latency = 9000000,
.owner = THIS_MODULE,
};
static unsigned int get_freq_table_size(struct cpufreq_frequency_table *freq_table) {
unsigned int size = 0;
while (freq_table[++size].frequency != CPUFREQ_TABLE_END);
return size;
}
static inline void fix_screen_off_min_step(struct cpufreq_lulzactive_cpuinfo *pcpu) {
if (pcpu->freq_table_size <= 0) {
screen_off_min_step = 0;
return;
}
if (DEFAULT_SCREEN_OFF_MIN_STEP == screen_off_min_step)
screen_off_min_step = pcpu->freq_table_size - 2;
if (screen_off_min_step >= pcpu->freq_table_size)
screen_off_min_step = pcpu->freq_table_size - 1;
}
static inline unsigned int adjust_screen_off_freq(
struct cpufreq_lulzactive_cpuinfo *pcpu, unsigned int freq) {
if (early_suspended && freq > pcpu->freq_table[screen_off_min_step].frequency) {
freq = pcpu->freq_table[screen_off_min_step].frequency;
pcpu->target_freq = pcpu->policy->cur;
if (freq > pcpu->policy->max)
freq = pcpu->policy->max;
if (freq < pcpu->policy->min)
freq = pcpu->policy->min;
}
return freq;
}
static void cpufreq_lulzactive_timer(unsigned long data)
{
// do not step down if up scaling was stucked by short sampling time by tegrak
static unsigned int stuck_on_sampling = 0;
unsigned int delta_idle;
unsigned int delta_time;
int cpu_load;
int load_since_change;
u64 time_in_idle;
u64 idle_exit_time;
struct cpufreq_lulzactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, data);
u64 now_idle;
unsigned int new_freq;
int index;
int ret;
/*
* Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
* this lets idle exit know the current idle time sample has
* been processed, and idle exit can generate a new sample and
* re-arm the timer. This prevents a concurrent idle
* exit on that CPU from writing a new set of info at the same time
* the timer function runs (the timer function can't use that info
* until more time passes).
*/
time_in_idle = pcpu->time_in_idle;
idle_exit_time = pcpu->idle_exit_time;
now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
smp_wmb();
/* If we raced with cancelling a timer, skip. */
if (!idle_exit_time) {
dbgpr("timer %d: no valid idle exit sample\n", (int) data);
goto exit;
}
/* let it be when s5pv310 contorl the suspending by tegrak */
//if (suspending) {
// goto rearm;
//}
#if DEBUG
if ((int) jiffies - (int) pcpu->cpu_timer.expires >= 10)
dbgpr("timer %d: late by %d ticks\n",
(int) data, jiffies - pcpu->cpu_timer.expires);
#endif
delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle);
delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
idle_exit_time);
/*
* If timer ran less than 1ms after short-term sample started, retry.
*/
if (delta_time < 1000) {
dbgpr("timer %d: time delta %u too short exit=%llu now=%llu\n", (int) data,
delta_time, idle_exit_time, pcpu->timer_run_time);
goto rearm;
}
if (delta_idle > delta_time)
cpu_load = 0;
else
cpu_load = 100 * (delta_time - delta_idle) / delta_time;
delta_idle = (unsigned int) cputime64_sub(now_idle,
pcpu->freq_change_time_in_idle);
delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
pcpu->freq_change_time);
if (delta_idle > delta_time)
load_since_change = 0;
else
load_since_change =
100 * (delta_time - delta_idle) / delta_time;
/*
* Choose greater of short-term load (since last idle timer
* started or timer function re-armed itself) or long-term load
* (since last frequency change).
*/
if (load_since_change > cpu_load)
cpu_load = load_since_change;
/*
* START lulzactive algorithm section
*/
/*
if (early_suspended) {
new_freq = pcpu->policy->min;
pcpu->target_freq = pcpu->policy->cur;
}
else */if (cpu_load >= inc_cpu_load) {
if (pump_up_step && pcpu->policy->cur < pcpu->policy->max) {
ret = cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table,
pcpu->policy->cur, CPUFREQ_RELATION_H,
&index);
if (ret < 0) {
goto rearm;
}
// apply pump_up_step by tegrak
index -= pump_up_step;
if (index < 0)
index = 0;
new_freq = pcpu->freq_table[index].frequency;
}
else {
new_freq = pcpu->policy->max;
}
}
/*
else if (cpu_load <= dec_cpu_load) {
ret = cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table,
pcpu->policy->cur, CPUFREQ_RELATION_H,
&index);
if (ret < 0) {
goto rearm;
}
if (ramp_down_step) {
//set next low frequency of table
new_freq = pcpu->freq_table[index + 1].frequency;
}
else if (ramp_down_step) {
//new_freq = pcpu->policy->max * cpu_load / 100;
new_freq = pcpu->policy->min;
}
}
*/
else if (stuck_on_sampling) {
new_freq = pcpu->policy->cur;
}
else {
if (pump_down_step) {
ret = cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table,
pcpu->policy->cur, CPUFREQ_RELATION_H,
&index);
if (ret < 0) {
goto rearm;
}
// apply pump_down_step by tegrak
index += pump_down_step;
if (index >= pcpu->freq_table_size) {
index = pcpu->freq_table_size - 1;
}
new_freq = (pcpu->policy->cur > pcpu->policy->min) ?
(pcpu->freq_table[index].frequency) :
(pcpu->policy->min);
}
else {
new_freq = pcpu->policy->max * cpu_load / 100;
ret = cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table,
new_freq, CPUFREQ_RELATION_H,
&index);
if (ret < 0) {
goto rearm;
}
new_freq = pcpu->freq_table[index].frequency;
}
}
// adjust freq when screen off
new_freq = adjust_screen_off_freq(pcpu, new_freq);
if (pcpu->target_freq == new_freq)
{
dbgpr("timer %d: load=%d, already at %d\n", (int) data, cpu_load, new_freq);
stuck_on_sampling = 0;
goto rearm_if_notmax;
}
/*
* Do not scale down unless we have been at this frequency for the
* minimum sample time.
*/
if (new_freq < pcpu->target_freq) {
if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time) <
down_sample_time) {
dbgpr("timer %d: load=%d cur=%d tgt=%d not yet\n", (int) data, cpu_load, pcpu->target_freq, new_freq);
goto rearm;
}
}
else {
if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time) <
up_sample_time) {
dbgpr("timer %d: load=%d cur=%d tgt=%d not yet\n", (int) data, cpu_load, pcpu->target_freq, new_freq);
/* don't reset timer */
stuck_on_sampling = 1;
goto rearm;
}
}
if (suspending && debug_mode & LULZACTIVE_DEBUG_SUSPEND) {
LOGI("suspending: cpu_load=%d%% new_freq=%u ppcpu->policy->cur=%u\n",
cpu_load, new_freq, pcpu->policy->cur);
}
//if (early_suspended && !suspending && debug_mode & LULZACTIVE_DEBUG_EARLY_SUSPEND) {
if (early_suspended && !suspending && debug_mode & LULZACTIVE_DEBUG_LOAD) {
LOGI("early_suspended: cpu_load=%d%% new_freq=%u ppcpu->policy->cur=%u\n",
cpu_load, new_freq, pcpu->policy->cur);
//LOGI("lock @%uMHz!\n", new_freq/1000);
}
if (debug_mode & LULZACTIVE_DEBUG_LOAD && !early_suspended && !suspending) {
LOGI("cpu_load=%d%% new_freq=%u pcpu->target_freq=%u pcpu->policy->cur=%u\n",
cpu_load, new_freq, pcpu->target_freq, pcpu->policy->cur);
}
dbgpr("timer %d: load=%d cur=%d tgt=%d queue\n", (int) data, cpu_load, pcpu->target_freq, new_freq);
stuck_on_sampling = 0;
if (new_freq < pcpu->target_freq) {
pcpu->target_freq = new_freq;
spin_lock(&down_cpumask_lock);
cpumask_set_cpu(data, &down_cpumask);
spin_unlock(&down_cpumask_lock);
queue_work(down_wq, &freq_scale_down_work);
} else {
pcpu->target_freq = new_freq;
#if DEBUG
up_request_time = ktime_to_us(ktime_get());
#endif
spin_lock(&up_cpumask_lock);
cpumask_set_cpu(data, &up_cpumask);
spin_unlock(&up_cpumask_lock);
wake_up_process(up_task);
}
rearm_if_notmax:
/*
* Already set max speed and don't see a need to change that,
* wait until next idle to re-evaluate, don't need timer.
*/
if (pcpu->target_freq == pcpu->policy->max)
goto exit;
rearm:
if (!timer_pending(&pcpu->cpu_timer)) {
/*
* If already at min: if that CPU is idle, don't set timer.
* Else cancel the timer if that CPU goes idle. We don't
* need to re-evaluate speed until the next idle exit.
*/
if (pcpu->target_freq == pcpu->policy->min) {
smp_rmb();
if (pcpu->idling) {
dbgpr("timer %d: cpu idle, don't re-arm\n", (int) data);
goto exit;
}
pcpu->timer_idlecancel = 1;
}
pcpu->time_in_idle = get_cpu_idle_time_us(
data, &pcpu->idle_exit_time);
mod_timer(&pcpu->cpu_timer, jiffies + 2);
dbgpr("timer %d: set timer for %lu exit=%llu\n", (int) data, pcpu->cpu_timer.expires, pcpu->idle_exit_time);
}
exit:
return;
}
static void cpufreq_lulzactive_idle(void)
{
struct cpufreq_lulzactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, smp_processor_id());
int pending;
if (!pcpu->governor_enabled) {
pm_idle_old();
return;
}
pcpu->idling = 1;
smp_wmb();
pending = timer_pending(&pcpu->cpu_timer);
if (pcpu->target_freq != pcpu->policy->min) {
#ifdef CONFIG_SMP
/*
* Entering idle while not at lowest speed. On some
* platforms this can hold the other CPU(s) at that speed
* even though the CPU is idle. Set a timer to re-evaluate
* speed so this idle CPU doesn't hold the other CPUs above
* min indefinitely. This should probably be a quirk of
* the CPUFreq driver.
*/
if (!pending) {
pcpu->time_in_idle = get_cpu_idle_time_us(
smp_processor_id(), &pcpu->idle_exit_time);
pcpu->timer_idlecancel = 0;
mod_timer(&pcpu->cpu_timer, jiffies + 2);
dbgpr("idle: enter at %d, set timer for %lu exit=%llu\n",
pcpu->target_freq, pcpu->cpu_timer.expires,
pcpu->idle_exit_time);
}
#endif
} else {
/*
* If at min speed and entering idle after load has
* already been evaluated, and a timer has been set just in
* case the CPU suddenly goes busy, cancel that timer. The
* CPU didn't go busy; we'll recheck things upon idle exit.
*/
if (pending && pcpu->timer_idlecancel) {
dbgpr("idle: cancel timer for %lu\n", pcpu->cpu_timer.expires);
del_timer(&pcpu->cpu_timer);
/*
* Ensure last timer run time is after current idle
* sample start time, so next idle exit will always
* start a new idle sampling period.
*/
pcpu->idle_exit_time = 0;
pcpu->timer_idlecancel = 0;
}
}
pm_idle_old();
pcpu->idling = 0;
smp_wmb();
/*
* Arm the timer for 1-2 ticks later if not already, and if the timer
* function has already processed the previous load sampling
* interval. (If the timer is not pending but has not processed
* the previous interval, it is probably racing with us on another
* CPU. Let it compute load based on the previous sample and then
* re-arm the timer for another interval when it's done, rather
* than updating the interval start time to be "now", which doesn't
* give the timer function enough time to make a decision on this
* run.)
*/
if (timer_pending(&pcpu->cpu_timer) == 0 &&
pcpu->timer_run_time >= pcpu->idle_exit_time) {
pcpu->time_in_idle =
get_cpu_idle_time_us(smp_processor_id(),
&pcpu->idle_exit_time);
pcpu->timer_idlecancel = 0;
mod_timer(&pcpu->cpu_timer, jiffies + 2);
dbgpr("idle: exit, set timer for %lu exit=%llu\n", pcpu->cpu_timer.expires, pcpu->idle_exit_time);
#if DEBUG
} else if (timer_pending(&pcpu->cpu_timer) == 0 &&
pcpu->timer_run_time < pcpu->idle_exit_time) {
dbgpr("idle: timer not run yet: exit=%llu tmrrun=%llu\n",
pcpu->idle_exit_time, pcpu->timer_run_time);
#endif
}
}
static int cpufreq_lulzactive_up_task(void *data)
{
unsigned int cpu;
cpumask_t tmp_mask;
struct cpufreq_lulzactive_cpuinfo *pcpu;
#if DEBUG
u64 now;
u64 then;
unsigned int lat;
#endif
while (1) {
set_current_state(TASK_INTERRUPTIBLE);
spin_lock(&up_cpumask_lock);
if (cpumask_empty(&up_cpumask)) {
spin_unlock(&up_cpumask_lock);
schedule();
if (kthread_should_stop())
break;
spin_lock(&up_cpumask_lock);
}
set_current_state(TASK_RUNNING);
#if DEBUG
then = up_request_time;
now = ktime_to_us(ktime_get());
if (now > then) {
lat = ktime_to_us(ktime_get()) - then;
if (lat > up_max_latency)
up_max_latency = lat;
}
#endif
tmp_mask = up_cpumask;
cpumask_clear(&up_cpumask);
spin_unlock(&up_cpumask_lock);
for_each_cpu(cpu, &tmp_mask) {
pcpu = &per_cpu(cpuinfo, cpu);
if (nr_running() == 1) {
dbgpr("up %d: tgt=%d nothing else running\n", cpu,
pcpu->target_freq);
}
__cpufreq_driver_target(pcpu->policy,
pcpu->target_freq,
CPUFREQ_RELATION_H);
pcpu->freq_change_time_in_idle =
get_cpu_idle_time_us(cpu,
&pcpu->freq_change_time);
dbgpr("up %d: set tgt=%d (actual=%d)\n", cpu, pcpu->target_freq, pcpu->policy->cur);
}
}
return 0;
}
static void cpufreq_lulzactive_freq_down(struct work_struct *work)
{
unsigned int cpu;
cpumask_t tmp_mask;
struct cpufreq_lulzactive_cpuinfo *pcpu;
spin_lock(&down_cpumask_lock);
tmp_mask = down_cpumask;
cpumask_clear(&down_cpumask);
spin_unlock(&down_cpumask_lock);
for_each_cpu(cpu, &tmp_mask) {
pcpu = &per_cpu(cpuinfo, cpu);
__cpufreq_driver_target(pcpu->policy,
pcpu->target_freq,
CPUFREQ_RELATION_H);
pcpu->freq_change_time_in_idle =
get_cpu_idle_time_us(cpu,
&pcpu->freq_change_time);
dbgpr("down %d: set tgt=%d (actual=%d)\n", cpu, pcpu->target_freq, pcpu->policy->cur);
}
}
// inc_cpu_load
static ssize_t show_inc_cpu_load(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", inc_cpu_load);
}
static ssize_t store_inc_cpu_load(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
ssize_t ret;
ret = strict_strtoul(buf, 0, &inc_cpu_load);
if (inc_cpu_load > 100) {
inc_cpu_load = 100;
}
else if (inc_cpu_load < 10) {
inc_cpu_load = 10;
}
return ret;
}
static struct global_attr inc_cpu_load_attr = __ATTR(inc_cpu_load, 0666,
show_inc_cpu_load, store_inc_cpu_load);
// down_sample_time
static ssize_t show_down_sample_time(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", down_sample_time);
}
static ssize_t store_down_sample_time(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
return strict_strtoul(buf, 0, &down_sample_time);
}
static struct global_attr down_sample_time_attr = __ATTR(down_sample_time, 0666,
show_down_sample_time, store_down_sample_time);
// up_sample_time
static ssize_t show_up_sample_time(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", up_sample_time);
}
static ssize_t store_up_sample_time(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
return strict_strtoul(buf, 0, &up_sample_time);
}
static struct global_attr up_sample_time_attr = __ATTR(up_sample_time, 0666,
show_up_sample_time, store_up_sample_time);
// debug_mode
static ssize_t show_debug_mode(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", debug_mode);
}
static ssize_t store_debug_mode(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
return strict_strtoul(buf, 0, &debug_mode);
}
static struct global_attr debug_mode_attr = __ATTR(debug_mode, 0666,
show_debug_mode, store_debug_mode);
// pump_up_step
static ssize_t show_pump_up_step(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", pump_up_step);
}
static ssize_t store_pump_up_step(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
return strict_strtoul(buf, 0, &pump_up_step);
}
static struct global_attr pump_up_step_attr = __ATTR(pump_up_step, 0666,
show_pump_up_step, store_pump_up_step);
// pump_down_step
static ssize_t show_pump_down_step(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", pump_down_step);
}
static ssize_t store_pump_down_step(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
ssize_t ret;
struct cpufreq_lulzactive_cpuinfo *pcpu;
ret = strict_strtoul(buf, 0, &pump_down_step);
pcpu = &per_cpu(cpuinfo, 0);
// fix out of bound
if (pcpu->freq_table_size <= pump_down_step) {
pump_down_step = pcpu->freq_table_size - 1;
}
return ret;
}
static struct global_attr pump_down_step_attr = __ATTR(pump_down_step, 0666,
show_pump_down_step, store_pump_down_step);
// screen_off_min_step
static ssize_t show_screen_off_min_step(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct cpufreq_lulzactive_cpuinfo *pcpu;
pcpu = &per_cpu(cpuinfo, 0);
fix_screen_off_min_step(pcpu);
return sprintf(buf, "%lu\n", screen_off_min_step);
}
static ssize_t store_screen_off_min_step(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
struct cpufreq_lulzactive_cpuinfo *pcpu;
ssize_t ret;
ret = strict_strtoul(buf, 0, &screen_off_min_step);
pcpu = &per_cpu(cpuinfo, 0);
fix_screen_off_min_step(pcpu);
return ret;
}
static struct global_attr screen_off_min_step_attr = __ATTR(screen_off_min_step, 0666,
show_screen_off_min_step, store_screen_off_min_step);
// author
static ssize_t show_author(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", LULZACTIVE_AUTHOR);
}
static struct global_attr author_attr = __ATTR(author, 0444,
show_author, NULL);
// tuner
static ssize_t show_tuner(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", LULZACTIVE_TUNER);
}
static struct global_attr tuner_attr = __ATTR(tuner, 0444,
show_tuner, NULL);
// version
static ssize_t show_version(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", LULZACTIVE_VERSION);
}
static struct global_attr version_attr = __ATTR(version, 0444,
show_version, NULL);
// freq_table
static ssize_t show_freq_table(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct cpufreq_lulzactive_cpuinfo *pcpu;
char temp[64];
int i;
pcpu = &per_cpu(cpuinfo, 0);
for (i = 0; i < pcpu->freq_table_size; i++) {
sprintf(temp, "%u\n", pcpu->freq_table[i].frequency);
strcat(buf, temp);
}
return strlen(buf);
}
static struct global_attr freq_table_attr = __ATTR(freq_table, 0444,
show_freq_table, NULL);
static struct attribute *lulzactive_attributes[] = {
&inc_cpu_load_attr.attr,
&up_sample_time_attr.attr,
&down_sample_time_attr.attr,
&pump_up_step_attr.attr,
&pump_down_step_attr.attr,
&screen_off_min_step_attr.attr,
&debug_mode_attr.attr,
&author_attr.attr,
&tuner_attr.attr,
&version_attr.attr,
&freq_table_attr.attr,
NULL,
};
static struct attribute_group lulzactive_attr_group = {
.attrs = lulzactive_attributes,
.name = "lulzactive",
};
static int cpufreq_governor_lulzactive(struct cpufreq_policy *new_policy,
unsigned int event)
{
int rc;
struct cpufreq_lulzactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, new_policy->cpu);
switch (event) {
case CPUFREQ_GOV_START:
if (debug_mode & LULZACTIVE_DEBUG_START_STOP) {
LOGI("CPUFREQ_GOV_START\n");
}
if (!cpu_online(new_policy->cpu))
return -EINVAL;
pcpu->policy = new_policy;
pcpu->freq_table = cpufreq_frequency_get_table(new_policy->cpu);
pcpu->target_freq = new_policy->cur;
pcpu->freq_change_time_in_idle =
get_cpu_idle_time_us(new_policy->cpu,
&pcpu->freq_change_time);
pcpu->governor_enabled = 1;
pcpu->freq_table_size = get_freq_table_size(pcpu->freq_table);
// fix invalid screen_off_min_step
fix_screen_off_min_step(pcpu);
/*
* Do not register the idle hook and create sysfs
* entries if we have already done so.
*/
if (atomic_inc_return(&active_count) > 1)
return 0;
rc = sysfs_create_group(cpufreq_global_kobject,
&lulzactive_attr_group);
if (rc)
return rc;
pm_idle_old = pm_idle;
pm_idle = cpufreq_lulzactive_idle;
break;
case CPUFREQ_GOV_STOP:
if (debug_mode & LULZACTIVE_DEBUG_START_STOP) {
LOGI("CPUFREQ_GOV_STOP\n");
}
pcpu->governor_enabled = 0;
if (atomic_dec_return(&active_count) > 0)
return 0;
sysfs_remove_group(cpufreq_global_kobject,
&lulzactive_attr_group);
pm_idle = pm_idle_old;
del_timer(&pcpu->cpu_timer);
break;
case CPUFREQ_GOV_LIMITS:
if (new_policy->max < new_policy->cur)
__cpufreq_driver_target(new_policy,
new_policy->max, CPUFREQ_RELATION_H);
else if (new_policy->min > new_policy->cur)
__cpufreq_driver_target(new_policy,
new_policy->min, CPUFREQ_RELATION_L);