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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
|
/*
* drivers/cpufreq/cpufreq_interactive.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)
*
*/
#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/time.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <asm/cputime.h>
#include <linux/pm_qos_params.h>
#define CREATE_TRACE_POINTS
#include <trace/events/cpufreq_interactive.h>
static atomic_t active_count = ATOMIC_INIT(0);
struct cpufreq_interactive_cpuinfo {
struct timer_list cpu_timer;
int timer_idlecancel;
u64 time_in_idle;
u64 time_in_iowait;
u64 idle_exit_time;
u64 timer_run_time;
int idling;
u64 freq_change_time;
u64 freq_change_time_in_idle;
u64 freq_change_time_in_iowait;
struct cpufreq_policy *policy;
struct cpufreq_frequency_table *freq_table;
unsigned int target_freq;
unsigned int floor_freq;
u64 floor_validate_time;
int governor_enabled;
};
static DEFINE_PER_CPU(struct cpufreq_interactive_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;
static struct mutex set_speed_lock;
struct cpufreq_interactive_core_lock {
struct pm_qos_request_list qos_min_req;
struct pm_qos_request_list qos_max_req;
struct task_struct *lock_task;
struct work_struct unlock_work;
struct timer_list unlock_timer;
int request_active;
unsigned long lock_period;
unsigned int core_lock_count;
unsigned int min_core_keep;
struct mutex mutex;
};
/* default timeout for core lock down */
#define DEFAULT_CORE_LOCK_PERIOD 200000 /* 200 ms */
static struct cpufreq_interactive_core_lock core_lock;
/* Hi speed to bump to from lo speed when load burst (default max) */
static u64 hispeed_freq;
/* Boost frequency by boost_factor when CPU load at or above this value. */
#define DEFAULT_GO_MAXSPEED_LOAD 99
static unsigned long go_maxspeed_load;
/* Go to hispeed_freq when CPU load at or above this value. */
#define DEFAULT_GO_HISPEED_LOAD 90
static unsigned long go_hispeed_load;
/* Base of exponential raise to max speed; if 0 - jump to maximum */
static unsigned long boost_factor;
/* Max frequency boost in Hz; if 0 - no max is enforced */
static unsigned long max_boost;
/* Consider IO as busy */
static unsigned long io_is_busy;
/*
* Targeted sustainable load relatively to current frequency.
* If 0, target is set realtively to the max speed
*/
static unsigned long sustain_load;
/*
* The minimum amount of time to spend at a frequency before we can ramp down.
*/
#define DEFAULT_MIN_SAMPLE_TIME 80000;
static unsigned long min_sample_time;
/*
* The sample rate of the timer used to increase frequency
*/
#define DEFAULT_TIMER_RATE 20000;
static unsigned long timer_rate;
/*
* Wait this long before raising speed above hispeed, by default a single
* timer interval.
*/
#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
static unsigned long above_hispeed_delay_val;
/*
* Boost pulse to hispeed on touchscreen input.
*/
static int input_boost_val;
struct cpufreq_interactive_inputopen {
struct input_handle *handle;
struct work_struct inputopen_work;
};
static struct cpufreq_interactive_inputopen inputopen;
/*
* Non-zero means longer-term speed boost active.
*/
static int boost_val;
static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
unsigned int event);
#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
static
#endif
struct cpufreq_governor cpufreq_gov_interactive = {
.name = "interactive",
.governor = cpufreq_governor_interactive,
.max_transition_latency = 10000000,
.owner = THIS_MODULE,
};
static unsigned int cpufreq_interactive_get_target(
int cpu_load, int load_since_change,
struct cpufreq_interactive_cpuinfo *pcpu)
{
unsigned int target_freq;
/*
* 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;
/* Exponential boost policy */
if (boost_factor) {
if (cpu_load >= go_maxspeed_load) {
target_freq = pcpu->policy->cur * boost_factor;
if (max_boost &&
target_freq > pcpu->policy->cur + max_boost)
target_freq = pcpu->policy->cur + max_boost;
} else {
if (!sustain_load)
sustain_load = 100;
target_freq =
(pcpu->policy->cur * cpu_load / sustain_load);
}
goto done;
}
/* Jump boost policy */
if (cpu_load >= go_hispeed_load || boost_val) {
if (pcpu->target_freq <= pcpu->policy->min) {
target_freq = hispeed_freq;
} else {
target_freq = pcpu->policy->max * cpu_load / 100;
if (target_freq < hispeed_freq)
target_freq = hispeed_freq;
if (pcpu->target_freq == hispeed_freq &&
target_freq > hispeed_freq &&
cputime64_sub(pcpu->timer_run_time,
pcpu->freq_change_time)
< above_hispeed_delay_val) {
target_freq = pcpu->target_freq;
trace_cpufreq_interactive_notyet(
smp_processor_id(),
cpu_load,
pcpu->target_freq,
target_freq);
}
}
} else {
target_freq = pcpu->policy->max * cpu_load / 100;
}
done:
target_freq = min(target_freq, pcpu->policy->max);
return target_freq;
}
static inline cputime64_t get_cpu_iowait_time(
unsigned int cpu, cputime64_t *wall)
{
u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
if (iowait_time == -1ULL)
return 0;
return iowait_time;
}
static void cpufreq_interactive_timer(unsigned long data)
{
unsigned int delta_idle;
unsigned int delta_iowait;
unsigned int delta_time;
int cpu_load;
int load_since_change;
u64 time_in_idle;
u64 time_in_iowait;
u64 idle_exit_time;
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, data);
u64 now_idle;
u64 now_iowait;
unsigned int new_freq;
unsigned int index;
unsigned long flags;
smp_rmb();
if (!pcpu->governor_enabled)
goto exit;
/*
* 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;
time_in_iowait = pcpu->time_in_iowait;
idle_exit_time = pcpu->idle_exit_time;
now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
now_iowait = get_cpu_iowait_time(data, NULL);
smp_wmb();
/* If we raced with cancelling a timer, skip. */
if (!idle_exit_time)
goto exit;
delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle);
delta_iowait = (unsigned int) cputime64_sub(now_iowait, time_in_iowait);
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)
goto rearm;
if (delta_idle > delta_time)
cpu_load = 0;
else {
if (io_is_busy && delta_idle >= delta_iowait)
delta_idle -= delta_iowait;
cpu_load = 100 * (delta_time - delta_idle) / delta_time;
}
delta_idle = (unsigned int) cputime64_sub(now_idle,
pcpu->freq_change_time_in_idle);
delta_iowait = (unsigned int) cputime64_sub(now_iowait,
pcpu->freq_change_time_in_iowait);
delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
pcpu->freq_change_time);
if ((delta_time == 0) || (delta_idle > delta_time))
load_since_change = 0;
else {
if (io_is_busy && delta_idle >= delta_iowait)
delta_idle -= delta_iowait;
load_since_change =
100 * (delta_time - delta_idle) / delta_time;
}
/*
* Combine short-term load (since last idle timer started or timer
* function re-armed itself) and long-term load (since last frequency
* change) to determine new target frequency.
*
* This function implements the cpufreq scaling policy
*/
new_freq = cpufreq_interactive_get_target(cpu_load, load_since_change,
pcpu);
if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
new_freq, CPUFREQ_RELATION_H,
&index)) {
pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
(int) data);
goto rearm;
}
new_freq = pcpu->freq_table[index].frequency;
/*
* Do not scale below floor_freq unless we have been at or above the
* floor frequency for the minimum sample time since last validated.
*/
if (new_freq < pcpu->floor_freq) {
if (cputime64_sub(pcpu->timer_run_time,
pcpu->floor_validate_time)
< min_sample_time) {
trace_cpufreq_interactive_notyet(data, cpu_load,
pcpu->target_freq, new_freq);
goto rearm;
}
}
pcpu->floor_freq = new_freq;
pcpu->floor_validate_time = pcpu->timer_run_time;
if (pcpu->target_freq == new_freq) {
trace_cpufreq_interactive_already(data, cpu_load,
pcpu->target_freq, new_freq);
goto rearm_if_notmax;
}
trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
new_freq);
if (new_freq < pcpu->target_freq) {
pcpu->target_freq = new_freq;
spin_lock_irqsave(&down_cpumask_lock, flags);
cpumask_set_cpu(data, &down_cpumask);
spin_unlock_irqrestore(&down_cpumask_lock, flags);
queue_work(down_wq, &freq_scale_down_work);
} else {
pcpu->target_freq = new_freq;
spin_lock_irqsave(&up_cpumask_lock, flags);
cpumask_set_cpu(data, &up_cpumask);
spin_unlock_irqrestore(&up_cpumask_lock, flags);
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)
goto exit;
pcpu->timer_idlecancel = 1;
}
pcpu->time_in_idle = get_cpu_idle_time_us(
data, &pcpu->idle_exit_time);
pcpu->time_in_iowait = get_cpu_iowait_time(
data, NULL);
mod_timer(&pcpu->cpu_timer,
jiffies + usecs_to_jiffies(timer_rate));
}
exit:
return;
}
static void cpufreq_interactive_idle_start(void)
{
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, smp_processor_id());
int pending;
if (!pcpu->governor_enabled)
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->time_in_iowait = get_cpu_iowait_time(
smp_processor_id(), NULL);
pcpu->timer_idlecancel = 0;
mod_timer(&pcpu->cpu_timer,
jiffies + usecs_to_jiffies(timer_rate));
}
#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) {
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;
}
}
}
static void cpufreq_interactive_idle_end(void)
{
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, smp_processor_id());
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->governor_enabled) {
pcpu->time_in_idle =
get_cpu_idle_time_us(smp_processor_id(),
&pcpu->idle_exit_time);
pcpu->time_in_iowait =
get_cpu_iowait_time(smp_processor_id(),
NULL);
pcpu->timer_idlecancel = 0;
mod_timer(&pcpu->cpu_timer,
jiffies + usecs_to_jiffies(timer_rate));
}
}
static int cpufreq_interactive_up_task(void *data)
{
unsigned int cpu;
cpumask_t tmp_mask;
unsigned long flags;
struct cpufreq_interactive_cpuinfo *pcpu;
while (1) {
set_current_state(TASK_INTERRUPTIBLE);
spin_lock_irqsave(&up_cpumask_lock, flags);
if (cpumask_empty(&up_cpumask)) {
spin_unlock_irqrestore(&up_cpumask_lock, flags);
schedule();
if (kthread_should_stop())
break;
spin_lock_irqsave(&up_cpumask_lock, flags);
}
set_current_state(TASK_RUNNING);
tmp_mask = up_cpumask;
cpumask_clear(&up_cpumask);
spin_unlock_irqrestore(&up_cpumask_lock, flags);
for_each_cpu(cpu, &tmp_mask) {
unsigned int j;
unsigned int max_freq = 0;
pcpu = &per_cpu(cpuinfo, cpu);
smp_rmb();
if (!pcpu->governor_enabled)
continue;
mutex_lock(&set_speed_lock);
for_each_cpu(j, pcpu->policy->cpus) {
struct cpufreq_interactive_cpuinfo *pjcpu =
&per_cpu(cpuinfo, j);
if (pjcpu->target_freq > max_freq)
max_freq = pjcpu->target_freq;
}
__cpufreq_driver_target(pcpu->policy,
max_freq,
CPUFREQ_RELATION_H);
mutex_unlock(&set_speed_lock);
trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
pcpu->policy->cur);
pcpu->freq_change_time_in_idle =
get_cpu_idle_time_us(cpu,
&pcpu->freq_change_time);
pcpu->freq_change_time_in_iowait =
get_cpu_iowait_time(cpu, NULL);
}
}
return 0;
}
static void cpufreq_interactive_freq_down(struct work_struct *work)
{
unsigned int cpu;
cpumask_t tmp_mask;
unsigned long flags;
struct cpufreq_interactive_cpuinfo *pcpu;
spin_lock_irqsave(&down_cpumask_lock, flags);
tmp_mask = down_cpumask;
cpumask_clear(&down_cpumask);
spin_unlock_irqrestore(&down_cpumask_lock, flags);
for_each_cpu(cpu, &tmp_mask) {
unsigned int j;
unsigned int max_freq = 0;
pcpu = &per_cpu(cpuinfo, cpu);
smp_rmb();
if (!pcpu->governor_enabled)
continue;
mutex_lock(&set_speed_lock);
for_each_cpu(j, pcpu->policy->cpus) {
struct cpufreq_interactive_cpuinfo *pjcpu =
&per_cpu(cpuinfo, j);
if (pjcpu->target_freq > max_freq)
max_freq = pjcpu->target_freq;
}
__cpufreq_driver_target(pcpu->policy, max_freq,
CPUFREQ_RELATION_H);
mutex_unlock(&set_speed_lock);
trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
pcpu->policy->cur);
pcpu->freq_change_time_in_idle =
get_cpu_idle_time_us(cpu,
&pcpu->freq_change_time);
pcpu->freq_change_time_in_iowait =
get_cpu_iowait_time(cpu, NULL);
}
}
static void cpufreq_interactive_boost(void)
{
int i;
int anyboost = 0;
unsigned long flags;
struct cpufreq_interactive_cpuinfo *pcpu;
spin_lock_irqsave(&up_cpumask_lock, flags);
for_each_online_cpu(i) {
pcpu = &per_cpu(cpuinfo, i);
if (pcpu->target_freq < hispeed_freq) {
pcpu->target_freq = hispeed_freq;
cpumask_set_cpu(i, &up_cpumask);
anyboost = 1;
}
/* Set floor freq and (re)start timer for when last
* validated.
*/
pcpu->floor_freq = hispeed_freq;
pcpu->floor_validate_time = ktime_to_us(ktime_get());
}
spin_unlock_irqrestore(&up_cpumask_lock, flags);
if (anyboost)
wake_up_process(up_task);
}
static void cpufreq_interactive_core_lock_timer(unsigned long data)
{
queue_work(down_wq, &core_lock.unlock_work);
}
static void cpufreq_interactive_unlock_cores(struct work_struct *wq)
{
struct cpufreq_interactive_core_lock *cl =
container_of(wq, struct cpufreq_interactive_core_lock,
unlock_work);
mutex_lock(&cl->mutex);
if (--cl->request_active) {
goto done;
}
pm_qos_update_request(&cl->qos_min_req,
core_lock.min_core_keep);
pm_qos_update_request(&cl->qos_max_req,
PM_QOS_MAX_ONLINE_CPUS_DEFAULT_VALUE);
done:
mutex_unlock(&cl->mutex);
}
/* Lock down to whatever # of cores online
* right now.
*
* A pm_qos request for 1 online CPU results in
* an instant cluster switch.
*/
static void cpufreq_interactive_lock_cores(void)
{
unsigned int ncpus;
mutex_lock(&core_lock.mutex);
if (core_lock.request_active) {
goto arm_timer;
}
ncpus = num_online_cpus();
if (core_lock.core_lock_count) {
if (ncpus < core_lock.core_lock_count) {
pr_debug("%s: num_online_cpus %d < core_lock_count %d, overriding\n",
__func__,
ncpus,
core_lock.core_lock_count);
ncpus = core_lock.core_lock_count;
}
pr_debug("%s: forcing %d cores online for %lu ns\n",
__func__,
ncpus,
core_lock.lock_period);
}
else {
pr_debug("%s: forcing %d already-online cores online for %lu ns\n",
__func__,
ncpus,
core_lock.lock_period);
}
pm_qos_update_request(&core_lock.qos_min_req, ncpus);
pm_qos_update_request(&core_lock.qos_max_req, ncpus);
core_lock.request_active++;
arm_timer:
mod_timer(&core_lock.unlock_timer,
jiffies + usecs_to_jiffies(core_lock.lock_period));
mutex_unlock(&core_lock.mutex);
}
static int cpufreq_interactive_lock_cores_task(void *data)
{
while(1) {
cpufreq_interactive_lock_cores();
set_current_state(TASK_INTERRUPTIBLE);
schedule();
}
return 0;
}
/*
* Pulsed boost on input event raises CPUs to hispeed_freq and lets
* usual algorithm of min_sample_time decide when to allow speed
* to drop.
*/
static void cpufreq_interactive_input_event(struct input_handle *handle,
unsigned int type,
unsigned int code, int value)
{
if (input_boost_val && type == EV_SYN && code == SYN_REPORT) {
wake_up_process(core_lock.lock_task);
cpufreq_interactive_boost();
}
}
static void cpufreq_interactive_input_open(struct work_struct *w)
{
struct cpufreq_interactive_inputopen *io =
container_of(w, struct cpufreq_interactive_inputopen,
inputopen_work);
int error;
error = input_open_device(io->handle);
if (error)
input_unregister_handle(io->handle);
}
static int cpufreq_interactive_input_connect(struct input_handler *handler,
struct input_dev *dev,
const struct input_device_id *id)
{
struct input_handle *handle;
int error;
pr_info("%s: connect to %s\n", __func__, dev->name);
handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
if (!handle)
return -ENOMEM;
handle->dev = dev;
handle->handler = handler;
handle->name = "cpufreq_interactive";
error = input_register_handle(handle);
if (error)
goto err;
inputopen.handle = handle;
queue_work(down_wq, &inputopen.inputopen_work);
return 0;
err:
kfree(handle);
return error;
}
static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
{
input_close_device(handle);
input_unregister_handle(handle);
kfree(handle);
}
static const struct input_device_id cpufreq_interactive_ids[] = {
{
.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
INPUT_DEVICE_ID_MATCH_ABSBIT,
.evbit = { BIT_MASK(EV_ABS) },
.absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
BIT_MASK(ABS_MT_POSITION_X) |
BIT_MASK(ABS_MT_POSITION_Y) },
}, /* multi-touch touchscreen */
{
.flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
INPUT_DEVICE_ID_MATCH_ABSBIT,
.keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
.absbit = { [BIT_WORD(ABS_X)] =
BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
}, /* touchpad */
{ },
};
static struct input_handler cpufreq_interactive_input_handler = {
.event = cpufreq_interactive_input_event,
.connect = cpufreq_interactive_input_connect,
.disconnect = cpufreq_interactive_input_disconnect,
.name = "cpufreq_interactive",
.id_table = cpufreq_interactive_ids,
};
static ssize_t show_go_maxspeed_load(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", go_maxspeed_load);
}
static ssize_t store_go_maxspeed_load(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
go_maxspeed_load = val;
return count;
}
static struct global_attr go_maxspeed_load_attr = __ATTR(go_maxspeed_load, 0644,
show_go_maxspeed_load, store_go_maxspeed_load);
static ssize_t show_boost_factor(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", boost_factor);
}
static ssize_t store_boost_factor(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
boost_factor = val;
return count;
}
static struct global_attr boost_factor_attr = __ATTR(boost_factor, 0644,
show_boost_factor, store_boost_factor);
static ssize_t show_io_is_busy(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", io_is_busy);
}
static ssize_t store_io_is_busy(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
if (!strict_strtoul(buf, 0, &io_is_busy))
return count;
return -EINVAL;
}
static struct global_attr io_is_busy_attr = __ATTR(io_is_busy, 0644,
show_io_is_busy, store_io_is_busy);
static ssize_t show_sustain_load(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", sustain_load);
}
static ssize_t store_sustain_load(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
sustain_load = val;
return count;
}
static struct global_attr sustain_load_attr = __ATTR(sustain_load, 0644,
show_sustain_load, store_sustain_load);
static ssize_t show_max_boost(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", max_boost);
}
static ssize_t store_max_boost(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
max_boost = val;
return count;
}
static struct global_attr max_boost_attr = __ATTR(max_boost, 0644,
show_max_boost, store_max_boost);
static ssize_t show_hispeed_freq(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%llu\n", hispeed_freq);
}
static ssize_t store_hispeed_freq(struct kobject *kobj,
struct attribute *attr, const char *buf,
size_t count)
{
int ret;
u64 val;
ret = strict_strtoull(buf, 0, &val);
if (ret < 0)
return ret;
hispeed_freq = val;
return count;
}
static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
show_hispeed_freq, store_hispeed_freq);
static ssize_t show_go_hispeed_load(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", go_hispeed_load);
}
static ssize_t store_go_hispeed_load(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
go_hispeed_load = val;
return count;
}
static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
show_go_hispeed_load, store_go_hispeed_load);
static ssize_t show_min_sample_time(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", min_sample_time);
}
static ssize_t store_min_sample_time(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
min_sample_time = val;
return count;
}
static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
show_min_sample_time, store_min_sample_time);
static ssize_t show_above_hispeed_delay(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", above_hispeed_delay_val);
}
static ssize_t store_above_hispeed_delay(struct kobject *kobj,
struct attribute *attr,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
above_hispeed_delay_val = val;
return count;
}
define_one_global_rw(above_hispeed_delay);
static ssize_t show_timer_rate(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return sprintf(buf, "%lu\n", timer_rate);
}
static ssize_t store_timer_rate(struct kobject *kobj,
struct attribute *attr, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
timer_rate = val;
return count;
}
static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
show_timer_rate, store_timer_rate);
static ssize_t show_core_lock_period(struct kobject *kobj, struct attribute *attr,
char *buf)
{
return sprintf(buf, "%lu\n", core_lock.lock_period);
}
static ssize_t store_core_lock_period(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
core_lock.lock_period = val;
return count;
}
define_one_global_rw(core_lock_period);
static ssize_t show_core_lock_count(struct kobject *kobj, struct attribute *attr,
char *buf)
{
return sprintf(buf, "%u\n", core_lock.core_lock_count);
}
static ssize_t store_core_lock_count(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
core_lock.core_lock_count = min((unsigned int)val, num_possible_cpus());
return count;
}
define_one_global_rw(core_lock_count);
static ssize_t show_min_core_keep(struct kobject *kobj, struct attribute *attr,
char *buf)
{
return sprintf(buf, "%u\n", core_lock.min_core_keep);
}
static ssize_t store_min_core_keep(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
core_lock.min_core_keep = (unsigned int)val;
cpufreq_interactive_lock_cores();
return count;
}
define_one_global_rw(min_core_keep);
static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
char *buf)
{
return sprintf(buf, "%u\n", input_boost_val);
}
static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
input_boost_val = val;
return count;
}
define_one_global_rw(input_boost);
static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", boost_val);
}
static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = kstrtoul(buf, 0, &val);
if (ret < 0)
return ret;
boost_val = val;
if (boost_val)
cpufreq_interactive_boost();
if (!boost_val)
trace_cpufreq_interactive_unboost(hispeed_freq);
return count;
}
define_one_global_rw(boost);
static struct attribute *interactive_attributes[] = {
&go_maxspeed_load_attr.attr,
&boost_factor_attr.attr,
&max_boost_attr.attr,
&io_is_busy_attr.attr,
&sustain_load_attr.attr,
&hispeed_freq_attr.attr,
&go_hispeed_load_attr.attr,
&above_hispeed_delay.attr,
&min_sample_time_attr.attr,
&timer_rate_attr.attr,
&input_boost.attr,
&boost.attr,
&core_lock_period.attr,
&core_lock_count.attr,
&min_core_keep.attr,
NULL,
};
static struct attribute_group interactive_attr_group = {
.attrs = interactive_attributes,
.name = "interactive",
};
static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
unsigned int event)
{
int rc;
unsigned int j;
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_frequency_table *freq_table;
switch (event) {
case CPUFREQ_GOV_START:
if (!cpu_online(policy->cpu))
return -EINVAL;
freq_table =
cpufreq_frequency_get_table(policy->cpu);
for_each_cpu(j, policy->cpus) {
pcpu = &per_cpu(cpuinfo, j);
pcpu->policy = policy;
pcpu->target_freq = policy->cur;
pcpu->freq_table = freq_table;
pcpu->freq_change_time_in_idle =
get_cpu_idle_time_us(j,
&pcpu->freq_change_time);
pcpu->time_in_idle = pcpu->freq_change_time_in_idle;
pcpu->idle_exit_time = pcpu->freq_change_time;
pcpu->freq_change_time_in_iowait =
get_cpu_iowait_time(j, NULL);
pcpu->time_in_iowait = pcpu->freq_change_time_in_iowait;
pcpu->timer_idlecancel = 1;
pcpu->floor_freq = pcpu->target_freq;
pcpu->floor_validate_time =
pcpu->freq_change_time;
pcpu->governor_enabled = 1;
smp_wmb();
}
if (!hispeed_freq)
hispeed_freq = policy->max;
/*
* 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,
&interactive_attr_group);
if (rc)
return rc;
rc = input_register_handler(&cpufreq_interactive_input_handler);
if (rc)
pr_warn("%s: failed to register input handler\n",
__func__);
break;
case CPUFREQ_GOV_STOP:
for_each_cpu(j, policy->cpus) {
pcpu = &per_cpu(cpuinfo, j);
pcpu->governor_enabled = 0;
smp_wmb();
del_timer_sync(&pcpu->cpu_timer);
/*
* Reset idle exit time since we may cancel the timer
* before it can run after the last idle exit time,
* to avoid tripping the check in idle exit for a timer
* that is trying to run.
*/
pcpu->idle_exit_time = 0;
}
flush_work(&freq_scale_down_work);
if (atomic_dec_return(&active_count) > 0)
return 0;
input_unregister_handler(&cpufreq_interactive_input_handler);
sysfs_remove_group(cpufreq_global_kobject,
&interactive_attr_group);
break;
case CPUFREQ_GOV_LIMITS:
if (policy->max < policy->cur)
__cpufreq_driver_target(policy,
policy->max, CPUFREQ_RELATION_H);
else if (policy->min > policy->cur)
__cpufreq_driver_target(policy,
policy->min, CPUFREQ_RELATION_L);
break;
}
return 0;
}
static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
unsigned long val,
void *data)
{
switch (val) {
case IDLE_START:
cpufreq_interactive_idle_start();
break;
case IDLE_END:
cpufreq_interactive_idle_end();
break;
}
return 0;
}
static struct notifier_block cpufreq_interactive_idle_nb = {
.notifier_call = cpufreq_interactive_idle_notifier,
};
static int __init cpufreq_interactive_init(void)
{
unsigned int i;
struct cpufreq_interactive_cpuinfo *pcpu;
struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
go_maxspeed_load = DEFAULT_GO_MAXSPEED_LOAD;
go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
timer_rate = DEFAULT_TIMER_RATE;
/* Initalize per-cpu timers */
for_each_possible_cpu(i) {
pcpu = &per_cpu(cpuinfo, i);
init_timer(&pcpu->cpu_timer);
pcpu->cpu_timer.function = cpufreq_interactive_timer;
pcpu->cpu_timer.data = i;
}
up_task = kthread_create(cpufreq_interactive_up_task, NULL,
"kinteractiveup");
if (IS_ERR(up_task))
return PTR_ERR(up_task);
sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m);
get_task_struct(up_task);
/* No rescuer thread, bind to CPU queuing the work for possibly
warm cache (probably doesn't matter much). */
down_wq = alloc_workqueue("knteractive_down", 0, 1);
if (!down_wq)
goto err_freeuptask;
INIT_WORK(&freq_scale_down_work,
cpufreq_interactive_freq_down);
spin_lock_init(&up_cpumask_lock);
spin_lock_init(&down_cpumask_lock);
mutex_init(&set_speed_lock);
pm_qos_add_request(&core_lock.qos_min_req, PM_QOS_MIN_ONLINE_CPUS,
core_lock.min_core_keep);
pm_qos_add_request(&core_lock.qos_max_req, PM_QOS_MAX_ONLINE_CPUS,
PM_QOS_MAX_ONLINE_CPUS_DEFAULT_VALUE);
init_timer(&core_lock.unlock_timer);
core_lock.unlock_timer.function = cpufreq_interactive_core_lock_timer;
core_lock.unlock_timer.data = 0;
core_lock.request_active = 0;
core_lock.lock_period = DEFAULT_CORE_LOCK_PERIOD;
core_lock.core_lock_count = 0; /* defaults to num_online_cpus() */
core_lock.min_core_keep = 2;
mutex_init(&core_lock.mutex);
core_lock.lock_task = kthread_create(cpufreq_interactive_lock_cores_task, NULL,
"kinteractive_lockcores");
if (IS_ERR(core_lock.lock_task))
return PTR_ERR(core_lock.lock_task);
sched_setscheduler_nocheck(core_lock.lock_task, SCHED_FIFO, ¶m);
get_task_struct(core_lock.lock_task);
idle_notifier_register(&cpufreq_interactive_idle_nb);
INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
INIT_WORK(&core_lock.unlock_work, cpufreq_interactive_unlock_cores);
return cpufreq_register_governor(&cpufreq_gov_interactive);
err_freeuptask:
put_task_struct(up_task);
return -ENOMEM;
}
#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
fs_initcall(cpufreq_interactive_init);
#else
module_init(cpufreq_interactive_init);
#endif
static void __exit cpufreq_interactive_exit(void)
{
cpufreq_unregister_governor(&cpufreq_gov_interactive);
kthread_stop(up_task);
put_task_struct(up_task);
destroy_workqueue(down_wq);
pm_qos_remove_request(&core_lock.qos_min_req);
pm_qos_remove_request(&core_lock.qos_max_req);
kthread_stop(core_lock.lock_task);
put_task_struct(core_lock.lock_task);
}
module_exit(cpufreq_interactive_exit);
MODULE_AUTHOR("Mike Chan <mike@android.com>");
MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
"Latency sensitive workloads");
MODULE_LICENSE("GPL");
|