/
cpufreq_greenmax.c
1616 lines (1367 loc) · 43.7 KB
/
cpufreq_greenmax.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_greenmax.c
*
* Copyright 2015 Joe Maples
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Author: Joe Maples <frap129@gmail.com>
*
* Based on the smartnmax governor with ondemand's powersave bias.
*
* SmartMax:
* Copyright (C) 2013-2014 maxwen
*
* Ondemand:
* Copyright (C) 2001 Russell King
* (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
* Jun Nakajima <jun.nakajima@intel.com>
*
* For a general overview of CPU governors see the relavent part in
* Documentation/cpu-freq/governors.txt
*
*/
#include <linux/module.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/cpufreq.h>
#include <linux/sched.h>
#include <linux/tick.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <linux/moduleparam.h>
#include <linux/jiffies.h>
#include <linux/input.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/kernel_stat.h>
#include <linux/init.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
/******************** Tunable parameters: ********************/
/*
* The "ideal" frequency to use. The governor will ramp up faster
* towards the ideal frequency and slower after it has passed it. Similarly,
* lowering the frequency towards the ideal frequency is faster than below it.
*/
#define DEFAULT_SUSPEND_IDEAL_FREQ 652800
#define DEFAULT_AWAKE_IDEAL_FREQ 1036800
#define DEFAULT_RAMP_UP_STEP 200000
#define DEFAULT_RAMP_DOWN_STEP 200000
#define DEFAULT_MAX_CPU_LOAD 80
#define DEFAULT_MIN_CPU_LOAD 50
#define DEFAULT_UP_RATE 30000
#define DEFAULT_DOWN_RATE 60000
#define DEFAULT_SAMPLING_RATE 30000
#define DEFAULT_INPUT_BOOST_DURATION 150000
#define DEFAULT_TOUCH_POKE_FREQ 1497600
#define DEFAULT_BOOST_FREQ 1497600
#define DEFAULT_IO_IS_BUSY 0
#define DEFAULT_IGNORE_NICE 1
#define DEFAULT_POWERSAVE_BIAS 0
#define POWERSAVE_BIAS_MAXLEVEL 1000
#define POWERSAVE_BIAS_MINLEVEL -1000
static unsigned int suspend_ideal_freq;
static unsigned int awake_ideal_freq;
/*
* Freqeuncy delta when ramping up above the ideal freqeuncy.
* Zero disables and causes to always jump straight to max frequency.
* When below the ideal freqeuncy we always ramp up to the ideal freq.
*/
static unsigned int ramp_up_step;
/*
* Freqeuncy delta when ramping down below the ideal freqeuncy.
* Zero disables and will calculate ramp down according to load heuristic.
* When above the ideal freqeuncy we always ramp down to the ideal freq.
*/
static unsigned int ramp_down_step;
/*
* CPU freq will be increased if measured load > max_cpu_load;
*/
static unsigned int max_cpu_load;
/*
* CPU freq will be decreased if measured load < min_cpu_load;
*/
static unsigned int min_cpu_load;
/*
* The minimum amount of time in usecs to spend at a frequency before we can ramp up.
* Notice we ignore this when we are below the ideal frequency.
*/
static unsigned int up_rate;
/*
* The minimum amount of time in usecs to spend at a frequency before we can ramp down.
* Notice we ignore this when we are above the ideal frequency.
*/
static unsigned int down_rate;
/* in usecs */
static unsigned int input_boost_duration;
static unsigned int touch_poke_freq;
static bool touch_poke = true;
/*
* should ramp_up steps during boost be possible
*/
static bool ramp_up_during_boost = true;
/*
* external boost interface - boost if duration is written
* to sysfs for boost_duration
*/
static unsigned int boost_freq;
static bool boost = true;
/* in usecs */
static unsigned int boost_duration = 0;
static unsigned int sampling_rate;
static unsigned int ignore_nice;
static int powersave_bias;
static unsigned int io_is_busy;
/*************** End of tunables ***************/
static unsigned int dbs_enable; /* number of CPUs using this policy */
static void do_dbs_timer(struct work_struct *work);
/* Sampling types */
enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
struct greenmax_info_s {
struct cpufreq_policy *cur_policy;
struct cpufreq_frequency_table *freq_table;
struct delayed_work work;
u64 prev_cpu_idle;
u64 prev_cpu_iowait;
u64 prev_cpu_wall;
u64 prev_cpu_nice;
u64 freq_change_time;
unsigned int cur_cpu_load;
unsigned int old_freq;
int ramp_dir;
unsigned int ideal_speed;
unsigned int cpu;
struct mutex timer_mutex;
unsigned int freq_lo;
unsigned int freq_lo_jiffies;
unsigned int freq_hi_jiffies;
unsigned int sample_type:1;
unsigned int rate_mult;
};
static DEFINE_PER_CPU(struct greenmax_info_s, greenmax_info);
#define dprintk(flag,msg...) do { \
if (debug_mask & flag) pr_info("[greenmax]" ":" msg); \
} while (0)
enum {
GREENMAX_DEBUG_JUMPS = 1,
GREENMAX_DEBUG_LOAD = 2,
GREENMAX_DEBUG_ALG = 4,
GREENMAX_DEBUG_BOOST = 8,
GREENMAX_DEBUG_INPUT = 16,
GREENMAX_DEBUG_SUSPEND = 32
};
/*
* Combination of the above debug flags.
*/
//static unsigned long debug_mask = GREENMAX_DEBUG_LOAD|GREENMAX_DEBUG_JUMPS|GREENMAX_DEBUG_ALG|GREENMAX_DEBUG_BOOST|GREENMAX_DEBUG_INPUT|GREENMAX_DEBUG_SUSPEND;
static unsigned long debug_mask;
#define GREENMAX_STAT 0
#if GREENMAX_STAT
static u64 timer_stat[4] = {0, 0, 0, 0};
#endif
/*
* dbs_mutex protects dbs_enable in governor start/stop.
*/
static DEFINE_MUTEX(dbs_mutex);
static struct workqueue_struct *greenmax_wq;
static bool boost_task_alive = false;
static struct task_struct *boost_task;
static u64 boost_end_time = 0ULL;
static unsigned int cur_boost_freq = 0;
static unsigned int cur_boost_duration = 0;
static bool boost_running = false;
static unsigned int ideal_freq;
static bool is_suspended = false;
static unsigned int min_sampling_rate;
#ifdef CONFIG_HAS_EARLYSUSPEND
static struct early_suspend greenmax_early_suspend_handler;
#endif
#define LATENCY_MULTIPLIER (1000)
#define MIN_LATENCY_MULTIPLIER (100)
#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
/*
* The polling frequency of this governor depends on the capability of
* the processor. Default polling frequency is 1000 times the transition
* latency of the processor. The governor will work on any processor with
* transition latency <= 10mS, using appropriate sampling
* rate.
* For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
* this governor will not work.
* All times here are in uS.
*/
#define MIN_SAMPLING_RATE_RATIO (2)
#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
static int cpufreq_governor_greenmax(struct cpufreq_policy *policy,
unsigned int event);
#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_GREENMAX
static
#endif
struct cpufreq_governor cpufreq_gov_greenmax = {
.name = "greenmax",
.governor = cpufreq_governor_greenmax,
.max_transition_latency = TRANSITION_LATENCY_LIMIT,
.owner = THIS_MODULE,
};
static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) {
u64 idle_time;
u64 cur_wall_time;
u64 busy_time;
cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
idle_time = cur_wall_time - busy_time;
if (wall)
*wall = jiffies_to_usecs(cur_wall_time);
return jiffies_to_usecs(idle_time);
}
static inline u64 get_cpu_idle_time_greenmax(unsigned int cpu, u64 *wall)
{
u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
if (idle_time == -1ULL)
return get_cpu_idle_time_jiffy(cpu, wall);
else
idle_time += get_cpu_iowait_time_us(cpu, wall);
return idle_time;
}
static inline u64 get_cpu_iowait_time(unsigned int cpu, u64 *wall) {
u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
if (iowait_time == -1ULL)
return 0;
return iowait_time;
}
/*
* Find right freq to be set now with powersave_bias on.
* Returns the freq_hi to be used right now and will set freq_hi_jiffies,
* freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
*/
static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
unsigned int freq_next,
unsigned int relation)
{
unsigned int freq_req, freq_avg;
unsigned int freq_hi, freq_lo;
unsigned int index = 0;
unsigned int jiffies_total, jiffies_hi, jiffies_lo;
int freq_reduc;
struct greenmax_info_s *greenmax_info = &per_cpu(greenmax_info,
policy->cpu);
if (!greenmax_info->freq_table) {
greenmax_info->freq_lo = 0;
greenmax_info->freq_lo_jiffies = 0;
return freq_next;
}
cpufreq_frequency_table_target(policy, greenmax_info->freq_table, freq_next,
relation, &index);
freq_req = greenmax_info->freq_table[index].frequency;
freq_reduc = freq_req * powersave_bias / 1000;
freq_avg = freq_req - freq_reduc;
/* Find freq bounds for freq_avg in freq_table */
index = 0;
cpufreq_frequency_table_target(policy, greenmax_info->freq_table, freq_avg,
CPUFREQ_RELATION_H, &index);
freq_lo = greenmax_info->freq_table[index].frequency;
index = 0;
cpufreq_frequency_table_target(policy, greenmax_info->freq_table, freq_avg,
CPUFREQ_RELATION_L, &index);
freq_hi = greenmax_info->freq_table[index].frequency;
/* Find out how long we have to be in hi and lo freqs */
if (freq_hi == freq_lo) {
greenmax_info->freq_lo = 0;
greenmax_info->freq_lo_jiffies = 0;
return freq_lo;
}
jiffies_total = usecs_to_jiffies(sampling_rate);
jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
jiffies_hi += ((freq_hi - freq_lo) / 2);
jiffies_hi /= (freq_hi - freq_lo);
jiffies_lo = jiffies_total - jiffies_hi;
greenmax_info->freq_lo = freq_lo;
greenmax_info->freq_lo_jiffies = jiffies_lo;
greenmax_info->freq_hi_jiffies = jiffies_hi;
return freq_hi;
}
static int greenmax_powersave_bias_setspeed(struct cpufreq_policy *policy,
struct cpufreq_policy *altpolicy,
int level)
{
if (level == POWERSAVE_BIAS_MAXLEVEL) {
/* maximum powersave; set to lowest frequency */
__cpufreq_driver_target(policy,
(altpolicy) ? altpolicy->min : policy->min,
CPUFREQ_RELATION_L);
return 1;
} else if (level == POWERSAVE_BIAS_MINLEVEL) {
/* minimum powersave; set to highest frequency */
__cpufreq_driver_target(policy,
(altpolicy) ? altpolicy->max : policy->max,
CPUFREQ_RELATION_H);
return 1;
}
return 0;
}
static void greenmax_powersave_bias_init_cpu(int cpu)
{
struct greenmax_info_s *greenmax_info = &per_cpu(greenmax_info, cpu);
greenmax_info->freq_table = cpufreq_frequency_get_table(cpu);
greenmax_info->freq_lo = 0;
}
static void greenmax_powersave_bias_init(void)
{
int i;
for_each_online_cpu(i) {
greenmax_powersave_bias_init_cpu(i);
}
}
inline static void greenmax_update_min_max(
struct greenmax_info_s *this_greenmax, struct cpufreq_policy *policy) {
this_greenmax->ideal_speed = // ideal_freq; but make sure it obeys the policy min/max
policy->min < ideal_freq ?
(ideal_freq < policy->max ? ideal_freq : policy->max) :
policy->min;
}
inline static void greenmax_update_min_max_allcpus(void) {
unsigned int cpu;
for_each_online_cpu(cpu)
{
struct greenmax_info_s *this_greenmax = &per_cpu(greenmax_info, cpu);
if (this_greenmax->cur_policy){
if (lock_policy_rwsem_write(cpu) < 0)
continue;
greenmax_update_min_max(this_greenmax, this_greenmax->cur_policy);
unlock_policy_rwsem_write(cpu);
}
}
}
inline static unsigned int validate_freq(struct cpufreq_policy *policy,
int freq) {
if (freq > (int) policy->max)
return policy->max;
if (freq < (int) policy->min)
return policy->min;
return freq;
}
/* We want all CPUs to do sampling nearly on same jiffy */
static inline unsigned int get_timer_delay(void) {
unsigned int delay = usecs_to_jiffies(sampling_rate);
if (num_online_cpus() > 1)
delay -= jiffies % delay;
return delay;
}
inline static void target_freq(struct cpufreq_policy *policy,
struct greenmax_info_s *this_greenmax, int new_freq, int old_freq,
int prefered_relation) {
int index, target;
struct cpufreq_frequency_table *table = this_greenmax->freq_table;
unsigned int cpu = this_greenmax->cpu;
dprintk(GREENMAX_DEBUG_ALG, "%d: %s\n", old_freq, __func__);
// apply policy limits - just to be sure
new_freq = validate_freq(policy, new_freq);
if (!cpufreq_frequency_table_target(policy, table, new_freq,
prefered_relation, &index)) {
target = table[index].frequency;
if (target == old_freq) {
// if for example we are ramping up to *at most* current + ramp_up_step
// but there is no such frequency higher than the current, try also
// to ramp up to *at least* current + ramp_up_step.
if (new_freq > old_freq && prefered_relation == CPUFREQ_RELATION_H
&& !cpufreq_frequency_table_target(policy, table, new_freq,
CPUFREQ_RELATION_L, &index))
target = table[index].frequency;
// simlarly for ramping down:
else if (new_freq < old_freq
&& prefered_relation == CPUFREQ_RELATION_L
&& !cpufreq_frequency_table_target(policy, table, new_freq,
CPUFREQ_RELATION_H, &index))
target = table[index].frequency;
}
// no change
if (target == old_freq)
return;
} else {
dprintk(GREENMAX_DEBUG_ALG, "frequency change failed\n");
return;
}
dprintk(GREENMAX_DEBUG_JUMPS, "%d: jumping to %d (%d) cpu %d\n", old_freq, new_freq, target, cpu);
__cpufreq_driver_target(policy, target, prefered_relation);
// remember last time we changed frequency
this_greenmax->freq_change_time = ktime_to_us(ktime_get());
}
static inline void dbs_timer_init(struct greenmax_info_s *this_greenmax) {
int delay = get_timer_delay();
INIT_DEFERRABLE_WORK(&this_greenmax->work, do_dbs_timer);
schedule_delayed_work_on(this_greenmax->cpu, &this_greenmax->work, delay);
}
static inline void dbs_timer_exit(struct greenmax_info_s *this_greenmax) {
cancel_delayed_work_sync(&this_greenmax->work);
}
/* We use the same work function to sale up and down */
static void cpufreq_greenmax_freq_change(struct greenmax_info_s *this_greenmax) {
unsigned int cpu;
unsigned int new_freq = 0;
unsigned int old_freq;
int ramp_dir;
struct cpufreq_policy *policy;
unsigned int relation = CPUFREQ_RELATION_L;
ramp_dir = this_greenmax->ramp_dir;
old_freq = this_greenmax->old_freq;
policy = this_greenmax->cur_policy;
cpu = this_greenmax->cpu;
dprintk(GREENMAX_DEBUG_ALG, "%d: %s\n", old_freq, __func__);
if (old_freq != policy->cur) {
// frequency was changed by someone else?
dprintk(GREENMAX_DEBUG_ALG, "%d: frequency changed by 3rd party to %d\n",
old_freq, policy->cur);
new_freq = old_freq;
} else if (ramp_dir > 0 && nr_running() > 1) {
// ramp up logic:
if (old_freq < this_greenmax->ideal_speed)
new_freq = this_greenmax->ideal_speed;
else if (ramp_up_step) {
new_freq = old_freq + ramp_up_step;
relation = CPUFREQ_RELATION_H;
} else {
new_freq = policy->max;
relation = CPUFREQ_RELATION_H;
}
} else if (ramp_dir < 0) {
// ramp down logic:
if (old_freq > this_greenmax->ideal_speed) {
new_freq = this_greenmax->ideal_speed;
relation = CPUFREQ_RELATION_H;
} else if (ramp_down_step)
new_freq = old_freq - ramp_down_step;
else {
// Load heuristics: Adjust new_freq such that, assuming a linear
// scaling of load vs. frequency, the load in the new frequency
// will be max_cpu_load:
new_freq = old_freq * this_greenmax->cur_cpu_load / max_cpu_load;
if (new_freq > old_freq) // min_cpu_load > max_cpu_load ?!
new_freq = old_freq - 1;
}
}
if (new_freq!=0){
target_freq(policy, this_greenmax, new_freq, old_freq, relation);
}
this_greenmax->ramp_dir = 0;
}
static inline void cpufreq_greenmax_get_ramp_direction(struct greenmax_info_s *this_greenmax, u64 now)
{
unsigned int cur_load = this_greenmax->cur_cpu_load;
unsigned int cur = this_greenmax->old_freq;
struct cpufreq_policy *policy = this_greenmax->cur_policy;
// Scale up if load is above max or if there where no idle cycles since coming out of idle,
// additionally, if we are at or above the ideal_speed, verify we have been at this frequency
// for at least up_rate:
if (cur_load > max_cpu_load && cur < policy->max
&& (cur < this_greenmax->ideal_speed
|| (now - this_greenmax->freq_change_time) >= up_rate)) {
dprintk(GREENMAX_DEBUG_ALG,
"%d: ramp up: load %d\n", cur, cur_load);
this_greenmax->ramp_dir = 1;
}
// Similarly for scale down: load should be below min and if we are at or below ideal
// frequency we require that we have been at this frequency for at least down_rate:
else if (cur_load < min_cpu_load && cur > policy->min
&& (cur > this_greenmax->ideal_speed
|| (now - this_greenmax->freq_change_time) >= down_rate)) {
dprintk(GREENMAX_DEBUG_ALG,
"%d: ramp down: load %d\n", cur, cur_load);
this_greenmax->ramp_dir = -1;
}
}
static void inline cpufreq_greenmax_calc_load(int j)
{
struct greenmax_info_s *j_this_greenmax;
u64 cur_wall_time, cur_idle_time, cur_iowait_time;
unsigned int idle_time, wall_time, iowait_time;
unsigned int cur_load;
j_this_greenmax = &per_cpu(greenmax_info, j);
cur_idle_time = get_cpu_idle_time_greenmax(j, &cur_wall_time);
cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
wall_time = cur_wall_time - j_this_greenmax->prev_cpu_wall;
j_this_greenmax->prev_cpu_wall = cur_wall_time;
idle_time = cur_idle_time - j_this_greenmax->prev_cpu_idle;
j_this_greenmax->prev_cpu_idle = cur_idle_time;
iowait_time = cur_iowait_time - j_this_greenmax->prev_cpu_iowait;
j_this_greenmax->prev_cpu_iowait = cur_iowait_time;
if (ignore_nice) {
u64 cur_nice;
unsigned long cur_nice_jiffies;
cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] - j_this_greenmax->prev_cpu_nice;
cur_nice_jiffies = (unsigned long) cputime64_to_jiffies64(cur_nice);
j_this_greenmax->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
idle_time += jiffies_to_usecs(cur_nice_jiffies);
}
/*
* For the purpose of ondemand, waiting for disk IO is an
* indication that you're performance critical, and not that
* the system is actually idle. So subtract the iowait time
* from the cpu idle time.
*/
if (io_is_busy && idle_time >= iowait_time)
idle_time -= iowait_time;
if (unlikely(!wall_time || wall_time < idle_time))
return;
cur_load = 100 * (wall_time - idle_time) / wall_time;
j_this_greenmax->cur_cpu_load = cur_load;
}
static void cpufreq_greenmax_timer(struct greenmax_info_s *this_greenmax) {
unsigned int cur;
struct cpufreq_policy *policy = this_greenmax->cur_policy;
u64 now = ktime_to_us(ktime_get());
/* Extrapolated load of this CPU */
//unsigned int load_at_max_freq = 0;
unsigned int cpu = this_greenmax->cpu;
#if GREENMAX_STAT
u64 diff = 0;
if (timer_stat[cpu])
diff = now - timer_stat[cpu];
timer_stat[cpu] = now;
printk(KERN_DEBUG "[greenmax]:cpu %d %lld\n", cpu, diff);
#endif
cur = policy->cur;
dprintk(GREENMAX_DEBUG_ALG, "%d: %s cpu %d %lld\n", cur, __func__, cpu, now);
cpufreq_greenmax_calc_load(cpu);
/* calculate the scaled load across CPU */
//load_at_max_freq = (this_greenmax->cur_cpu_load * policy->cur)/policy->cpuinfo.max_freq;
//cpufreq_notify_utilization(policy, load_at_max_freq);
dprintk(GREENMAX_DEBUG_LOAD, "%d: load %d\n", cpu, this_greenmax->cur_cpu_load);
this_greenmax->old_freq = cur;
this_greenmax->ramp_dir = 0;
cpufreq_greenmax_get_ramp_direction(this_greenmax, now);
// no changes
if (this_greenmax->ramp_dir == 0)
return;
// boost - but not block ramp up steps based on load if requested
if (boost_running){
if (now < boost_end_time) {
dprintk(GREENMAX_DEBUG_BOOST, "%d: cpu %d boost running %llu %llu\n", cur, cpu, now, boost_end_time);
if (this_greenmax->ramp_dir == -1)
return;
else {
if (ramp_up_during_boost)
dprintk(GREENMAX_DEBUG_BOOST, "%d: cpu %d boost running but ramp_up above boost freq requested\n", cur, cpu);
else
return;
}
} else
boost_running = false;
}
cpufreq_greenmax_freq_change(this_greenmax);
}
static void do_dbs_timer(struct work_struct *work) {
struct greenmax_info_s *this_greenmax =
container_of(work, struct greenmax_info_s, work.work);
unsigned int cpu = this_greenmax->cpu;
int sample_type = this_greenmax->sample_type
int delay = get_timer_delay();
mutex_lock(&this_greenmax->timer_mutex);
/* Common NORMAL_SAMPLE setup */
this_greenmax->sample_type = DBS_NORMAL_SAMPLE;
if (!powersave_bias ||
sample_type == DBS_NORMAL_SAMPLE) {
cur_cpu_load;
if (this_greenmax->freq_lo) {
/* Setup timer for SUB_SAMPLE */
this_greenmax->sample_type = DBS_SUB_SAMPLE;
delay = this_greenmax->freq_hi_jiffies;
} else {
/* We want all CPUs to do sampling nearly on
* same jiffy
*/
delay = usecs_to_jiffies(sampling_rate
* this_greenmax->rate_mult);
if (num_online_cpus() > 1)
delay -= jiffies % delay;
}
} else {
__cpufreq_driver_target(this_greenmax->cur_policy,
this_greenmax->freq_lo, CPUFREQ_RELATION_H);
delay = this_greenmax->freq_lo_jiffies;
}
cpufreq_greenmax_timer(this_greenmax);
queue_delayed_work_on(cpu, greenmax_wq, &this_greenmax->work, delay);
mutex_unlock(&this_greenmax->timer_mutex);
}
static void update_idle_time(bool online) {
int j = 0;
for_each_possible_cpu(j)
{
struct greenmax_info_s *j_this_greenmax;
if (online && !cpu_online(j)) {
continue;
}
j_this_greenmax = &per_cpu(greenmax_info, j);
j_this_greenmax->prev_cpu_idle = get_cpu_idle_time_greenmax(j,
&j_this_greenmax->prev_cpu_wall);
if (ignore_nice)
j_this_greenmax->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
}
}
static ssize_t show_debug_mask(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%lu\n", debug_mask);
}
static ssize_t store_debug_mask(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0)
debug_mask = input;
else
return -EINVAL;
return count;
}
static ssize_t show_up_rate(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", up_rate);
}
static ssize_t store_up_rate(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= 0 && input <= 100000000)
up_rate = input;
else
return -EINVAL;
return count;
}
static ssize_t show_down_rate(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", down_rate);
}
static ssize_t store_down_rate(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= 0 && input <= 100000000)
down_rate = input;
else
return -EINVAL;
return count;
}
static ssize_t show_awake_ideal_freq(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", awake_ideal_freq);
}
static ssize_t store_awake_ideal_freq(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= 0) {
awake_ideal_freq = input;
if (!is_suspended){
ideal_freq = awake_ideal_freq;
greenmax_update_min_max_allcpus();
}
} else
return -EINVAL;
return count;
}
static ssize_t show_suspend_ideal_freq(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", suspend_ideal_freq);
}
static ssize_t store_suspend_ideal_freq(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= 0) {
suspend_ideal_freq = input;
if (is_suspended){
ideal_freq = suspend_ideal_freq;
greenmax_update_min_max_allcpus();
}
} else
return -EINVAL;
return count;
}
static ssize_t show_ramp_up_step(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", ramp_up_step);
}
static ssize_t store_ramp_up_step(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= 0)
ramp_up_step = input;
else
return -EINVAL;
return count;
}
static ssize_t show_ramp_down_step(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", ramp_down_step);
}
static ssize_t store_ramp_down_step(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= 0)
ramp_down_step = input;
else
return -EINVAL;
return count;
}
static ssize_t show_max_cpu_load(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", max_cpu_load);
}
static ssize_t store_max_cpu_load(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input > 0 && input <= 100)
max_cpu_load = input;
else
return -EINVAL;
return count;
}
static ssize_t show_min_cpu_load(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", min_cpu_load);
}
static ssize_t store_min_cpu_load(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input > 0 && input < 100)
min_cpu_load = input;
else
return -EINVAL;
return count;
}
static ssize_t show_sampling_rate(struct kobject *kobj, struct attribute *attr,
char *buf) {
return sprintf(buf, "%u\n", sampling_rate);
}
static ssize_t store_sampling_rate(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) {
ssize_t res;
unsigned long input;
res = strict_strtoul(buf, 0, &input);
if (res >= 0 && input >= min_sampling_rate)
sampling_rate = input;
else
return -EINVAL;
return count;
}
static ssize_t show_powersave_bias
(struct kobject *kobj, struct attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%d\n", powersave_bias);
}
static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
const char *buf, size_t count)
{
int input = 0;
int bypass = 0;
int ret, cpu, reenable_timer;
struct greenmax_info_s *greenmax_info;
ret = sscanf(buf, "%d", &input);
if (ret != 1)
return -EINVAL;
if (input >= POWERSAVE_BIAS_MAXLEVEL) {
input = POWERSAVE_BIAS_MAXLEVEL;
bypass = 1;
} else if (input <= POWERSAVE_BIAS_MINLEVEL) {
input = POWERSAVE_BIAS_MINLEVEL;
bypass = 1;
}
if (input == powersave_bias) {
/* no change */
return count;
}
reenable_timer = ((powersave_bias ==
POWERSAVE_BIAS_MAXLEVEL) ||
(powersave_bias ==
POWERSAVE_BIAS_MINLEVEL));
powersave_bias = input;
if (!bypass) {
if (reenable_timer) {
/* reinstate dbs timer */
for_each_online_cpu(cpu) {
if (lock_policy_rwsem_write(cpu) < 0)
continue;
greenmax_info_s = &per_cpu(greenmax_info, cpu);
if (this_greenmax->cur_policy) {
/* restart dbs timer */
dbs_timer_init(greenmax_info_s);
}
unlock_policy_rwsem_write(cpu);
}
}
greenmax_powersave_bias_init();
} else {
/* running at maximum or minimum frequencies; cancel
dbs timer as periodic load sampling is not necessary */
for_each_online_cpu(cpu) {
if (lock_policy_rwsem_write(cpu) < 0)
continue;
this_greenmax = &per_cpu(greenmax_info, cpu);
if (this_greenmax->cur_policy) {
/* cpu using greenmax, cancel dbs timer */
mutex_lock(&this_greenmax->timer_mutex);
dbs_timer_exit(this_greenmax);
greenmax_powersave_bias_setspeed(
this_greenmax->cur_policy,
NULL,
input);
mutex_unlock(&this_greenmax->timer_mutex);
}
unlock_policy_rwsem_write(cpu);
}
}
return count;
}
static ssize_t show_touch_poke_freq(struct kobject *kobj,
struct attribute *attr, char *buf) {
return sprintf(buf, "%u\n", touch_poke_freq);
}
static ssize_t store_touch_poke_freq(struct kobject *a, struct attribute *b,
const char *buf, size_t count) {
ssize_t res;