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
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
|
/*
* 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/ipa.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/rwsem.h>
#include <linux/sched.h>
#include <linux/sched/rt.h>
#include <linux/tick.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/kernel_stat.h>
#include <linux/pm_qos.h>
#include <asm/cputime.h>
#ifdef CONFIG_ARM_EXYNOS_MP_CPUFREQ
#include <mach/cpufreq.h>
#endif
#include "cpu_load_metric.h"
#ifdef CONFIG_PMU_COREMEM_RATIO
#include "pmu_func.h"
#endif
#define CREATE_TRACE_POINTS
#include <trace/events/cpufreq_interactive.h>
struct cpufreq_interactive_cpuinfo {
struct timer_list cpu_timer;
struct timer_list cpu_slack_timer;
spinlock_t load_lock; /* protects the next 4 fields */
u64 time_in_idle;
u64 time_in_idle_timestamp;
u64 cputime_speedadj;
u64 cputime_speedadj_timestamp;
struct cpufreq_policy *policy;
struct cpufreq_frequency_table *freq_table;
spinlock_t target_freq_lock; /*protects target freq */
unsigned int target_freq;
unsigned int floor_freq;
unsigned int max_freq;
u64 floor_validate_time;
u64 hispeed_validate_time;
struct rw_semaphore enable_sem;
int governor_enabled;
#ifdef CONFIG_PMU_COREMEM_RATIO
int region;
int prev_region;
#endif
};
static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
static cpumask_t speedchange_cpumask;
static spinlock_t speedchange_cpumask_lock;
static struct mutex gov_lock;
#ifdef CONFIG_PMU_COREMEM_RATIO
static cpumask_t regionchange_cpumask;
static spinlock_t regionchange_cpumask_lock;
#endif
/* Target load. Lower values result in higher CPU speeds. */
#define DEFAULT_TARGET_LOAD 90
static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
#define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
static unsigned int default_above_hispeed_delay[] = {
DEFAULT_ABOVE_HISPEED_DELAY };
#ifdef CONFIG_MODE_AUTO_CHANGE
extern struct cpumask hmp_fast_cpu_mask;
struct cpufreq_loadinfo {
unsigned int load;
unsigned int freq;
u64 timestamp;
};
static bool hmp_boost;
#define MULTI_MODE 2
#define SINGLE_MODE 1
#define NO_MODE 0
#define DEFAULT_MULTI_ENTER_TIME (4 * DEFAULT_TIMER_RATE)
#define DEFAULT_MULTI_EXIT_TIME (16 * DEFAULT_TIMER_RATE)
#define DEFAULT_SINGLE_ENTER_TIME (8 * DEFAULT_TIMER_RATE)
#define DEFAULT_SINGLE_EXIT_TIME (4 * DEFAULT_TIMER_RATE)
#define DEFAULT_SINGLE_CLUSTER0_MIN_FREQ 0
#define DEFAULT_MULTI_CLUSTER0_MIN_FREQ 0
static DEFINE_PER_CPU(struct cpufreq_loadinfo, loadinfo);
static DEFINE_PER_CPU(unsigned int, cpu_util);
static struct pm_qos_request cluster0_min_freq_qos;
static void mode_auto_change_minlock(struct work_struct *work);
static struct workqueue_struct *mode_auto_change_minlock_wq;
static struct work_struct mode_auto_change_minlock_work;
static unsigned int cluster0_min_freq=0;
#define set_qos(req, pm_qos_class, value) { \
if (value) { \
if (pm_qos_request_active(req)) \
pm_qos_update_request(req, value); \
else \
pm_qos_add_request(req, pm_qos_class, value); \
} \
else \
if (pm_qos_request_active(req)) \
pm_qos_remove_request(req); \
}
#endif /* CONFIG_MODE_AUTO_CHANGE */
struct cpufreq_interactive_tunables {
int usage_count;
/* Hi speed to bump to from lo speed when load burst (default max) */
unsigned int hispeed_freq;
/* Go to hi speed when CPU load at or above this value. */
#define DEFAULT_GO_HISPEED_LOAD 99
unsigned long go_hispeed_load;
/* Target load. Lower values result in higher CPU speeds. */
spinlock_t target_loads_lock;
unsigned int *target_loads;
int ntarget_loads;
/*
* The minimum amount of time to spend at a frequency before we can ramp
* down.
*/
#define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
unsigned long min_sample_time;
/*
* The sample rate of the timer used to increase frequency
*/
unsigned long timer_rate;
/*
* Wait this long before raising speed above hispeed, by default a
* single timer interval.
*/
spinlock_t above_hispeed_delay_lock;
unsigned int *above_hispeed_delay;
int nabove_hispeed_delay;
/* Non-zero means indefinite speed boost active */
int boost_val;
/* Duration of a boot pulse in usecs */
int boostpulse_duration_val;
/* End time of boost pulse in ktime converted to usecs */
u64 boostpulse_endtime;
/*
* Max additional time to wait in idle, beyond timer_rate, at speeds
* above minimum before wakeup to reduce speed, or -1 if unnecessary.
*/
#define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
int timer_slack_val;
bool io_is_busy;
#define TASK_NAME_LEN 15
/* realtime thread handles frequency scaling */
struct task_struct *speedchange_task;
#ifdef CONFIG_PMU_COREMEM_RATIO
struct task_struct *regionchange_task;
unsigned int prev_max_region;
unsigned int max_region;
u64 region_time_in_state[6];
u64 region_last_stat;
#endif
/* handle for get cpufreq_policy */
unsigned int *policy;
#ifdef CONFIG_MODE_AUTO_CHANGE
spinlock_t mode_lock;
unsigned int mode;
unsigned int enforced_mode;
u64 mode_check_timestamp;
unsigned long multi_enter_time;
unsigned long time_in_multi_enter;
unsigned int multi_enter_load;
unsigned long multi_exit_time;
unsigned long time_in_multi_exit;
unsigned int multi_exit_load;
unsigned long single_enter_time;
unsigned long time_in_single_enter;
unsigned int single_enter_load;
unsigned long single_exit_time;
unsigned long time_in_single_exit;
unsigned int single_exit_load;
spinlock_t param_index_lock;
unsigned int param_index;
unsigned int cur_param_index;
unsigned int single_cluster0_min_freq;
unsigned int multi_cluster0_min_freq;
#define MAX_PARAM_SET 4 /* ((MULTI_MODE | SINGLE_MODE | NO_MODE) + 1) */
unsigned int hispeed_freq_set[MAX_PARAM_SET];
unsigned long go_hispeed_load_set[MAX_PARAM_SET];
unsigned int *target_loads_set[MAX_PARAM_SET];
int ntarget_loads_set[MAX_PARAM_SET];
unsigned long min_sample_time_set[MAX_PARAM_SET];
unsigned long timer_rate_set[MAX_PARAM_SET];
unsigned int *above_hispeed_delay_set[MAX_PARAM_SET];
int nabove_hispeed_delay_set[MAX_PARAM_SET];
unsigned int sampling_down_factor_set[MAX_PARAM_SET];
unsigned int sampling_down_factor;
#endif
};
/* For cases where we have single governor instance for system */
static struct cpufreq_interactive_tunables *common_tunables;
static struct cpufreq_interactive_tunables *tuned_parameters[NR_CPUS] = {NULL, };
static struct attribute_group *get_sysfs_attr(void);
static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
cputime64_t *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 cputime64_t get_cpu_idle_time(
unsigned int cpu,
cputime64_t *wall,
bool io_is_busy)
{
u64 idle_time = get_cpu_idle_time_us(cpu, wall);
if (idle_time == -1ULL)
idle_time = get_cpu_idle_time_jiffy(cpu, wall);
else if (!io_is_busy)
idle_time += get_cpu_iowait_time_us(cpu, wall);
return idle_time;
}
static void cpufreq_interactive_timer_resched(
struct cpufreq_interactive_cpuinfo *pcpu)
{
struct cpufreq_interactive_tunables *tunables =
pcpu->policy->governor_data;
unsigned long expires;
unsigned long flags;
#ifdef CONFIG_PMU_COREMEM_RATIO
if (!tunables->speedchange_task || !tunables->regionchange_task)
#else
if (!tunables->speedchange_task)
#endif
return;
spin_lock_irqsave(&pcpu->load_lock, flags);
pcpu->time_in_idle =
get_cpu_idle_time(smp_processor_id(),
&pcpu->time_in_idle_timestamp,
tunables->io_is_busy);
pcpu->cputime_speedadj = 0;
pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
mod_timer_pinned(&pcpu->cpu_timer, expires);
if (tunables->timer_slack_val >= 0 &&
pcpu->target_freq > pcpu->policy->min) {
expires += usecs_to_jiffies(tunables->timer_slack_val);
mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
}
spin_unlock_irqrestore(&pcpu->load_lock, flags);
}
/* The caller shall take enable_sem write semaphore to avoid any timer race.
* The cpu_timer and cpu_slack_timer must be deactivated when calling this
* function.
*/
static void cpufreq_interactive_timer_start(
struct cpufreq_interactive_tunables *tunables, int cpu)
{
struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
unsigned long expires = jiffies +
usecs_to_jiffies(tunables->timer_rate);
unsigned long flags;
#ifdef CONFIG_PMU_COREMEM_RATIO
if (!tunables->speedchange_task || !tunables->regionchange_task)
#else
if (!tunables->speedchange_task)
#endif
return;
pcpu->cpu_timer.expires = expires;
add_timer_on(&pcpu->cpu_timer, cpu);
if (tunables->timer_slack_val >= 0 &&
pcpu->target_freq > pcpu->policy->min) {
expires += usecs_to_jiffies(tunables->timer_slack_val);
pcpu->cpu_slack_timer.expires = expires;
add_timer_on(&pcpu->cpu_slack_timer, cpu);
}
spin_lock_irqsave(&pcpu->load_lock, flags);
pcpu->time_in_idle =
get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
tunables->io_is_busy);
pcpu->cputime_speedadj = 0;
pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
spin_unlock_irqrestore(&pcpu->load_lock, flags);
}
static unsigned int freq_to_above_hispeed_delay(
struct cpufreq_interactive_tunables *tunables,
unsigned int freq)
{
int i;
unsigned int ret;
unsigned long flags;
spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
freq >= tunables->above_hispeed_delay[i+1]; i += 2)
;
ret = tunables->above_hispeed_delay[i];
spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
return ret;
}
static unsigned int freq_to_targetload(
struct cpufreq_interactive_tunables *tunables, unsigned int freq)
{
int i;
unsigned int ret;
unsigned long flags;
spin_lock_irqsave(&tunables->target_loads_lock, flags);
for (i = 0; i < tunables->ntarget_loads - 1 &&
freq >= tunables->target_loads[i+1]; i += 2)
;
ret = tunables->target_loads[i];
spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
return ret;
}
/*
* If increasing frequencies never map to a lower target load then
* choose_freq() will find the minimum frequency that does not exceed its
* target load given the current load.
*/
static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
unsigned int loadadjfreq)
{
unsigned int freq = pcpu->policy->cur;
unsigned int prevfreq, freqmin, freqmax;
unsigned int tl;
int index;
freqmin = 0;
freqmax = UINT_MAX;
do {
prevfreq = freq;
tl = freq_to_targetload(pcpu->policy->governor_data, freq);
/*
* Find the lowest frequency where the computed load is less
* than or equal to the target load.
*/
if (cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
CPUFREQ_RELATION_L, &index))
break;
freq = pcpu->freq_table[index].frequency;
if (freq > prevfreq) {
/* The previous frequency is too low. */
freqmin = prevfreq;
if (freq >= freqmax) {
/*
* Find the highest frequency that is less
* than freqmax.
*/
if (cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table,
freqmax - 1, CPUFREQ_RELATION_H,
&index))
break;
freq = pcpu->freq_table[index].frequency;
if (freq == freqmin) {
/*
* The first frequency below freqmax
* has already been found to be too
* low. freqmax is the lowest speed
* we found that is fast enough.
*/
freq = freqmax;
break;
}
}
} else if (freq < prevfreq) {
/* The previous frequency is high enough. */
freqmax = prevfreq;
if (freq <= freqmin) {
/*
* Find the lowest frequency that is higher
* than freqmin.
*/
if (cpufreq_frequency_table_target(
pcpu->policy, pcpu->freq_table,
freqmin + 1, CPUFREQ_RELATION_L,
&index))
break;
freq = pcpu->freq_table[index].frequency;
/*
* If freqmax is the first frequency above
* freqmin then we have already found that
* this speed is fast enough.
*/
if (freq == freqmax)
break;
}
}
/* If same frequency chosen as previous then done. */
} while (freq != prevfreq);
return freq;
}
static u64 update_load(int cpu)
{
struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
struct cpufreq_interactive_tunables *tunables =
pcpu->policy->governor_data;
u64 now;
u64 now_idle;
unsigned int delta_idle;
unsigned int delta_time;
u64 active_time;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned int cur_load = 0;
struct cpufreq_loadinfo *cur_loadinfo = &per_cpu(loadinfo, cpu);
#endif
now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
if (delta_time <= delta_idle)
active_time = 0;
else
active_time = delta_time - delta_idle;
pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
update_cpu_metric(cpu, now, delta_idle, delta_time, pcpu->policy);
pcpu->time_in_idle = now_idle;
pcpu->time_in_idle_timestamp = now;
#ifdef CONFIG_MODE_AUTO_CHANGE
cur_load = (unsigned int)(active_time * 100) / delta_time;
per_cpu(cpu_util, cpu) = cur_load;
cur_loadinfo->load = (cur_load * pcpu->policy->cur) /
pcpu->policy->cpuinfo.max_freq;
cur_loadinfo->freq = pcpu->policy->cur;
cur_loadinfo->timestamp = now;
#endif
return now;
}
#ifdef CONFIG_MODE_AUTO_CHANGE
static unsigned int check_mode(int cpu, unsigned int cur_mode, u64 now)
{
int i;
unsigned int ret=cur_mode, total_load=0, max_single_load=0;
struct cpufreq_loadinfo *cur_loadinfo;
struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
struct cpufreq_interactive_tunables *tunables =
pcpu->policy->governor_data;
if (now - tunables->mode_check_timestamp < tunables->timer_rate - USEC_PER_MSEC)
return ret;
if (now - tunables->mode_check_timestamp > tunables->timer_rate + USEC_PER_MSEC)
tunables->mode_check_timestamp = now - tunables->timer_rate;
if(cpumask_test_cpu(cpu, &hmp_fast_cpu_mask)) {
for_each_cpu_mask(i, hmp_fast_cpu_mask) {
cur_loadinfo = &per_cpu(loadinfo, i);
if (now - cur_loadinfo->timestamp <= tunables->timer_rate + USEC_PER_MSEC) {
total_load += cur_loadinfo->load;
if (cur_loadinfo->load > max_single_load)
max_single_load = cur_loadinfo->load;
}
}
}
else
return ret;
if (!(cur_mode & SINGLE_MODE)) {
if (max_single_load >= tunables->single_enter_load)
tunables->time_in_single_enter += now - tunables->mode_check_timestamp;
else
tunables->time_in_single_enter = 0;
if (tunables->time_in_single_enter >= tunables->single_enter_time)
ret |= SINGLE_MODE;
}
if (!(cur_mode & MULTI_MODE)) {
if (total_load >= tunables->multi_enter_load)
tunables->time_in_multi_enter += now - tunables->mode_check_timestamp;
else
tunables->time_in_multi_enter = 0;
if (tunables->time_in_multi_enter >= tunables->multi_enter_time)
ret |= MULTI_MODE;
}
if (cur_mode & SINGLE_MODE) {
if (max_single_load < tunables->single_exit_load)
tunables->time_in_single_exit += now - tunables->mode_check_timestamp;
else
tunables->time_in_single_exit = 0;
if (tunables->time_in_single_exit >= tunables->single_exit_time)
ret &= ~SINGLE_MODE;
}
if (cur_mode & MULTI_MODE) {
if (total_load < tunables->multi_exit_load)
tunables->time_in_multi_exit += now - tunables->mode_check_timestamp;
else
tunables->time_in_multi_exit = 0;
if (tunables->time_in_multi_exit >= tunables->multi_exit_time)
ret &= ~MULTI_MODE;
}
trace_cpufreq_interactive_mode(cpu, total_load,
tunables->time_in_single_enter, tunables->time_in_multi_enter,
tunables->time_in_single_exit, tunables->time_in_multi_exit, ret);
if (tunables->time_in_single_enter >= tunables->single_enter_time)
tunables->time_in_single_enter = 0;
if (tunables->time_in_multi_enter >= tunables->multi_enter_time)
tunables->time_in_multi_enter = 0;
if (tunables->time_in_single_exit >= tunables->single_exit_time)
tunables->time_in_single_exit = 0;
if (tunables->time_in_multi_exit >= tunables->multi_exit_time)
tunables->time_in_multi_exit = 0;
tunables->mode_check_timestamp = now;
return ret;
}
static void set_new_param_set(unsigned int index,
struct cpufreq_interactive_tunables * tunables)
{
unsigned long flags;
tunables->hispeed_freq = tunables->hispeed_freq_set[index];
tunables->go_hispeed_load = tunables->go_hispeed_load_set[index];
tunables->min_sample_time = tunables->min_sample_time_set[index];
tunables->timer_rate = tunables->timer_rate_set[index];
spin_lock_irqsave(&tunables->target_loads_lock, flags);
tunables->target_loads = tunables->target_loads_set[index];
tunables->ntarget_loads = tunables->ntarget_loads_set[index];
spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
tunables->above_hispeed_delay =
tunables->above_hispeed_delay_set[index];
tunables->nabove_hispeed_delay =
tunables->nabove_hispeed_delay_set[index];
spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
tunables->cur_param_index = index;
}
static void enter_mode(struct cpufreq_interactive_tunables * tunables)
{
set_new_param_set(tunables->mode, tunables);
if(tunables->mode & SINGLE_MODE)
cluster0_min_freq = tunables->single_cluster0_min_freq;
if(tunables->mode & MULTI_MODE)
cluster0_min_freq = tunables->multi_cluster0_min_freq;
if(!hmp_boost) {
pr_debug("%s mp boost on", __func__);
(void)set_hmp_boost(1);
hmp_boost = true;
}
queue_work(mode_auto_change_minlock_wq, &mode_auto_change_minlock_work);
}
static void exit_mode(struct cpufreq_interactive_tunables * tunables)
{
set_new_param_set(0, tunables);
cluster0_min_freq = 0;
if(hmp_boost) {
pr_debug("%s mp boost off", __func__);
(void)set_hmp_boost(0);
hmp_boost = false;
}
queue_work(mode_auto_change_minlock_wq, &mode_auto_change_minlock_work);
}
#endif
static void cpufreq_interactive_timer(unsigned long data)
{
u64 now;
unsigned int delta_time;
u64 cputime_speedadj;
int cpu_load;
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, data);
struct cpufreq_interactive_tunables *tunables =
pcpu->policy->governor_data;
unsigned int new_freq;
unsigned int loadadjfreq;
unsigned int index;
unsigned long flags;
bool boosted;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned int new_mode;
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
struct pmu_count_value pmu_data;
int region = 0;
#endif
if (!down_read_trylock(&pcpu->enable_sem))
return;
if (!pcpu->governor_enabled)
goto exit;
spin_lock_irqsave(&pcpu->load_lock, flags);
now = update_load(data);
delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
cputime_speedadj = pcpu->cputime_speedadj;
spin_unlock_irqrestore(&pcpu->load_lock, flags);
if (WARN_ON_ONCE(!delta_time))
goto rearm;
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->mode_lock, flags);
if (tunables->enforced_mode)
new_mode = tunables->enforced_mode;
else
new_mode = check_mode(data, tunables->mode, now);
if (new_mode != tunables->mode) {
tunables->mode = new_mode;
if (new_mode & MULTI_MODE || new_mode & SINGLE_MODE)
enter_mode(tunables);
else
exit_mode(tunables);
}
spin_unlock_irqrestore(&tunables->mode_lock, flags);
#endif
spin_lock_irqsave(&pcpu->target_freq_lock, flags);
do_div(cputime_speedadj, delta_time);
loadadjfreq = (unsigned int)cputime_speedadj * 100;
cpu_load = loadadjfreq / pcpu->target_freq;
boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
#ifdef CONFIG_PMU_COREMEM_RATIO
/* Get crypto load information from PMU */
pmu_data.core_num = data;
pmu_data.valid = 0;
read_pmu_one(&pmu_data);
pcpu->prev_region = pcpu->region;
region = coremem_ratio(pmu_data.pmnc1, pmu_data.pmnc2);
if (region != pcpu->prev_region) {
pcpu->region = region;
spin_lock_irqsave(®ionchange_cpumask_lock, flags);
cpumask_set_cpu(data, ®ionchange_cpumask);
spin_unlock_irqrestore(®ionchange_cpumask_lock, flags);
wake_up_process(tunables->regionchange_task);
}
#endif
if (cpu_load >= tunables->go_hispeed_load || boosted) {
if (pcpu->target_freq < tunables->hispeed_freq) {
new_freq = tunables->hispeed_freq;
} else {
new_freq = choose_freq(pcpu, loadadjfreq);
if (new_freq < tunables->hispeed_freq)
new_freq = tunables->hispeed_freq;
}
} else {
new_freq = choose_freq(pcpu, loadadjfreq);
if (new_freq > tunables->hispeed_freq &&
pcpu->target_freq < tunables->hispeed_freq)
new_freq = tunables->hispeed_freq;
}
if (pcpu->target_freq >= tunables->hispeed_freq &&
new_freq > pcpu->target_freq &&
now - pcpu->hispeed_validate_time <
freq_to_above_hispeed_delay(tunables, pcpu->target_freq)) {
trace_cpufreq_interactive_notyet(
data, cpu_load, pcpu->target_freq,
pcpu->policy->cur, new_freq);
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
goto rearm;
}
pcpu->hispeed_validate_time = now;
if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
new_freq, CPUFREQ_RELATION_L,
&index)) {
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
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 (now - pcpu->floor_validate_time <
tunables->min_sample_time) {
trace_cpufreq_interactive_notyet(
data, cpu_load, pcpu->target_freq,
pcpu->policy->cur, new_freq);
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
goto rearm;
}
}
/*
* Update the timestamp for checking whether speed has been held at
* or above the selected frequency for a minimum of min_sample_time,
* if not boosted to hispeed_freq. If boosted to hispeed_freq then we
* allow the speed to drop as soon as the boostpulse duration expires
* (or the indefinite boost is turned off).
*/
if (!boosted || new_freq > tunables->hispeed_freq) {
pcpu->floor_freq = new_freq;
pcpu->floor_validate_time = now;
}
if (pcpu->target_freq == new_freq &&
pcpu->target_freq <= pcpu->policy->cur) {
trace_cpufreq_interactive_already(
data, cpu_load, pcpu->target_freq,
pcpu->policy->cur, new_freq);
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
goto rearm_if_notmax;
}
trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
pcpu->policy->cur, new_freq);
pcpu->target_freq = new_freq;
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
cpumask_set_cpu(data, &speedchange_cpumask);
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
wake_up_process(tunables->speedchange_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)
#ifdef CONFIG_MODE_AUTO_CHANGE
goto rearm;
#else
goto exit;
#endif
rearm:
if (!timer_pending(&pcpu->cpu_timer))
cpufreq_interactive_timer_resched(pcpu);
exit:
up_read(&pcpu->enable_sem);
return;
}
static void cpufreq_interactive_idle_start(void)
{
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, smp_processor_id());
int pending;
if (!down_read_trylock(&pcpu->enable_sem))
return;
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
return;
}
pending = timer_pending(&pcpu->cpu_timer);
if (pcpu->target_freq != pcpu->policy->min) {
/*
* 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)
cpufreq_interactive_timer_resched(pcpu);
}
up_read(&pcpu->enable_sem);
}
static void cpufreq_interactive_idle_end(void)
{
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, smp_processor_id());
if (!down_read_trylock(&pcpu->enable_sem))
return;
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
return;
}
/* Arm the timer for 1-2 ticks later if not already. */
if (!timer_pending(&pcpu->cpu_timer)) {
cpufreq_interactive_timer_resched(pcpu);
} else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
del_timer(&pcpu->cpu_timer);
del_timer(&pcpu->cpu_slack_timer);
cpufreq_interactive_timer(smp_processor_id());
}
up_read(&pcpu->enable_sem);
}
#ifdef CONFIG_PMU_COREMEM_RATIO
static void region_update_status(struct cpufreq_interactive_tunables *tunables)
{
unsigned long cur_time;
cur_time = jiffies;
tunables->region_time_in_state[tunables->prev_max_region] +=
cur_time - tunables->region_last_stat;
tunables->region_last_stat = cur_time;
}
static int cpufreq_interactive_regionchange_task(void *data)
{
unsigned int cpu;
cpumask_t tmp_mask;
cpumask_t policy_mask;
unsigned long flags;
struct cpufreq_interactive_cpuinfo *pcpu;
while (1) {
set_current_state(TASK_INTERRUPTIBLE);
spin_lock_irqsave(®ionchange_cpumask_lock, flags);
pcpu = &per_cpu(cpuinfo, smp_processor_id());
cpumask_and(&policy_mask, ®ionchange_cpumask, pcpu->policy->related_cpus);
if (cpumask_empty(&policy_mask)) {
spin_unlock_irqrestore(®ionchange_cpumask_lock, flags);
if (kthread_should_stop())
break;
schedule();
if (kthread_should_stop())
break;
spin_lock_irqsave(®ionchange_cpumask_lock, flags);
}
set_current_state(TASK_RUNNING);
cpumask_and(&tmp_mask, ®ionchange_cpumask, pcpu->policy->related_cpus);
cpumask_andnot(®ionchange_cpumask, ®ionchange_cpumask, pcpu->policy->related_cpus);
spin_unlock_irqrestore(®ionchange_cpumask_lock, flags);
for_each_cpu(cpu, &tmp_mask) {
unsigned int j;
unsigned int max_region = 0;
struct cpufreq_interactive_tunables *tunables;
pcpu = &per_cpu(cpuinfo, cpu);
tunables = pcpu->policy->governor_data;
if (!down_read_trylock(&pcpu->enable_sem))
continue;
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
continue;
}
tunables->prev_max_region = tunables->max_region;
for_each_cpu(j, pcpu->policy->cpus) {
struct cpufreq_interactive_cpuinfo *pjcpu =
&per_cpu(cpuinfo, j);
if (pjcpu->region > max_region)
max_region = pjcpu->region;
}
if (tunables->prev_max_region != max_region) {
tunables->max_region = max_region;
coremem_region_bus_lock(max_region, pcpu->policy);
region_update_status(tunables);
}
up_read(&pcpu->enable_sem);
}
}
return 0;
}
#endif
static int cpufreq_interactive_speedchange_task(void *data)
{
unsigned int cpu;
cpumask_t tmp_mask;
cpumask_t policy_mask;
unsigned long flags;
struct cpufreq_interactive_cpuinfo *pcpu;
while (1) {
set_current_state(TASK_INTERRUPTIBLE);
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
pcpu = &per_cpu(cpuinfo, smp_processor_id());
cpumask_and(&policy_mask, &speedchange_cpumask, pcpu->policy->related_cpus);
if (cpumask_empty(&policy_mask)) {
spin_unlock_irqrestore(&speedchange_cpumask_lock,
flags);
if (kthread_should_stop())
break;
schedule();
if (kthread_should_stop())
break;
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
}
set_current_state(TASK_RUNNING);
cpumask_and(&tmp_mask, &speedchange_cpumask, pcpu->policy->related_cpus);
cpumask_andnot(&speedchange_cpumask, &speedchange_cpumask, pcpu->policy->related_cpus);
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
for_each_cpu(cpu, &tmp_mask) {
unsigned int j;
unsigned int max_freq = 0;
pcpu = &per_cpu(cpuinfo, cpu);
if (!down_read_trylock(&pcpu->enable_sem))
continue;
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
continue;
}
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;
}
if (max_freq != pcpu->policy->cur)
__cpufreq_driver_target(pcpu->policy,
max_freq,
CPUFREQ_RELATION_H);
#if defined(CONFIG_CPU_THERMAL_IPA)
ipa_cpufreq_requested(pcpu->policy, max_freq);
#endif
trace_cpufreq_interactive_setspeed(cpu,
pcpu->target_freq,
pcpu->policy->cur);
up_read(&pcpu->enable_sem);
}
}
return 0;
}
static void cpufreq_interactive_boost(const struct cpufreq_policy *policy)
{
int i;
int anyboost = 0;
unsigned long flags[2];
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_interactive_tunables *tunables;
struct cpumask boost_mask;
spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
if (have_governor_per_policy())
cpumask_copy(&boost_mask, policy->cpus);
else
cpumask_copy(&boost_mask, cpu_online_mask);
for_each_cpu(i, &boost_mask) {
pcpu = &per_cpu(cpuinfo, i);
tunables = pcpu->policy->governor_data;
spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
if (pcpu->target_freq < tunables->hispeed_freq) {
pcpu->target_freq = tunables->hispeed_freq;
cpumask_set_cpu(i, &speedchange_cpumask);
pcpu->hispeed_validate_time =
ktime_to_us(ktime_get());
anyboost = 1;
}
/*
* Set floor freq and (re)start timer for when last
* validated.
*/
pcpu->floor_freq = tunables->hispeed_freq;
pcpu->floor_validate_time = ktime_to_us(ktime_get());
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
}
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
if (anyboost && tunables->speedchange_task)
wake_up_process(tunables->speedchange_task);
}
static int cpufreq_interactive_notifier(
struct notifier_block *nb, unsigned long val, void *data)
{
struct cpufreq_freqs *freq = data;
struct cpufreq_interactive_cpuinfo *pcpu;
int cpu;
unsigned long flags;
if (val == CPUFREQ_POSTCHANGE) {
pcpu = &per_cpu(cpuinfo, freq->cpu);
if (!down_read_trylock(&pcpu->enable_sem))
return 0;
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
return 0;
}
for_each_cpu(cpu, pcpu->policy->cpus) {
struct cpufreq_interactive_cpuinfo *pjcpu =
&per_cpu(cpuinfo, cpu);
if (cpu != freq->cpu) {
if (!down_read_trylock(&pjcpu->enable_sem))
continue;
if (!pjcpu->governor_enabled) {
up_read(&pjcpu->enable_sem);
continue;
}
}
spin_lock_irqsave(&pjcpu->load_lock, flags);
update_load(cpu);
spin_unlock_irqrestore(&pjcpu->load_lock, flags);
if (cpu != freq->cpu)
up_read(&pjcpu->enable_sem);
}
up_read(&pcpu->enable_sem);
}
return 0;
}
static struct notifier_block cpufreq_notifier_block = {
.notifier_call = cpufreq_interactive_notifier,
};
static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
{
const char *cp;
int i;
int ntokens = 1;
unsigned int *tokenized_data;
int err = -EINVAL;
cp = buf;
while ((cp = strpbrk(cp + 1, " :")))
ntokens++;
if (!(ntokens & 0x1))
goto err;
tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
if (!tokenized_data) {
err = -ENOMEM;
goto err;
}
cp = buf;
i = 0;
while (i < ntokens) {
if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
goto err_kfree;
cp = strpbrk(cp, " :");
if (!cp)
break;
cp++;
}
if (i != ntokens)
goto err_kfree;
*num_tokens = ntokens;
return tokenized_data;
err_kfree:
kfree(tokenized_data);
err:
return ERR_PTR(err);
}
static ssize_t show_target_loads(
struct cpufreq_interactive_tunables *tunables,
char *buf)
{
int i;
ssize_t ret = 0;
unsigned long flags;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
spin_lock_irqsave(&tunables->target_loads_lock, flags);
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
for (i = 0; i < tunables->ntarget_loads_set[tunables->param_index]; i++)
ret += sprintf(buf + ret, "%u%s",
tunables->target_loads_set[tunables->param_index][i],
i & 0x1 ? ":" : " ");
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
for (i = 0; i < tunables->ntarget_loads; i++)
ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
i & 0x1 ? ":" : " ");
#endif
sprintf(buf + ret - 1, "\n");
spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
return ret;
}
static ssize_t store_target_loads(
struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ntokens;
unsigned int *new_target_loads = NULL;
unsigned long flags;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
new_target_loads = get_tokenized_data(buf, &ntokens);
if (IS_ERR(new_target_loads))
return PTR_RET(new_target_loads);
spin_lock_irqsave(&tunables->target_loads_lock, flags);
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
if (tunables->target_loads_set[tunables->param_index] != default_target_loads)
kfree(tunables->target_loads_set[tunables->param_index]);
tunables->target_loads_set[tunables->param_index] = new_target_loads;
tunables->ntarget_loads_set[tunables->param_index] = ntokens;
if (tunables->cur_param_index == tunables->param_index) {
tunables->target_loads = new_target_loads;
tunables->ntarget_loads = ntokens;
}
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
if (tunables->target_loads != default_target_loads)
kfree(tunables->target_loads);
tunables->target_loads = new_target_loads;
tunables->ntarget_loads = ntokens;
#endif
spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
return count;
}
static ssize_t show_above_hispeed_delay(
struct cpufreq_interactive_tunables *tunables, char *buf)
{
int i;
ssize_t ret = 0;
unsigned long flags;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
for (i = 0; i < tunables->nabove_hispeed_delay_set[tunables->param_index]; i++)
ret += sprintf(buf + ret, "%u%s",
tunables->above_hispeed_delay_set[tunables->param_index][i],
i & 0x1 ? ":" : " ");
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
for (i = 0; i < tunables->nabove_hispeed_delay; i++)
ret += sprintf(buf + ret, "%u%s",
tunables->above_hispeed_delay[i],
i & 0x1 ? ":" : " ");
#endif
sprintf(buf + ret - 1, "\n");
spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
return ret;
}
static ssize_t store_above_hispeed_delay(
struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ntokens;
unsigned int *new_above_hispeed_delay = NULL;
unsigned long flags;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
if (IS_ERR(new_above_hispeed_delay))
return PTR_RET(new_above_hispeed_delay);
spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
if (tunables->above_hispeed_delay_set[tunables->param_index] != default_above_hispeed_delay)
kfree(tunables->above_hispeed_delay_set[tunables->param_index]);
tunables->above_hispeed_delay_set[tunables->param_index] = new_above_hispeed_delay;
tunables->nabove_hispeed_delay_set[tunables->param_index] = ntokens;
if (tunables->cur_param_index == tunables->param_index) {
tunables->above_hispeed_delay = new_above_hispeed_delay;
tunables->nabove_hispeed_delay = ntokens;
}
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
if (tunables->above_hispeed_delay != default_above_hispeed_delay)
kfree(tunables->above_hispeed_delay);
tunables->above_hispeed_delay = new_above_hispeed_delay;
tunables->nabove_hispeed_delay = ntokens;
#endif
spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
return count;
}
static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
char *buf)
{
#ifdef CONFIG_MODE_AUTO_CHANGE
return sprintf(buf, "%u\n", tunables->hispeed_freq_set[tunables->param_index]);
#else
return sprintf(buf, "%u\n", tunables->hispeed_freq);
#endif
}
static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ret;
long unsigned int val;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
tunables->hispeed_freq_set[tunables->param_index] = val;
if (tunables->cur_param_index == tunables->param_index)
tunables->hispeed_freq = val;
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
tunables->hispeed_freq = val;
#endif
return count;
}
static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
#ifdef CONFIG_MODE_AUTO_CHANGE
return sprintf(buf, "%lu\n", tunables->go_hispeed_load_set[tunables->param_index]);
#else
return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
#endif
}
static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
unsigned long val;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
tunables->go_hispeed_load_set[tunables->param_index] = val;
if (tunables->cur_param_index == tunables->param_index)
tunables->go_hispeed_load = val;
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
tunables->go_hispeed_load = val;
#endif
return count;
}
static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
#ifdef CONFIG_MODE_AUTO_CHANGE
return sprintf(buf, "%lu\n", tunables->min_sample_time_set[tunables->param_index]);
#else
return sprintf(buf, "%lu\n", tunables->min_sample_time);
#endif
}
static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
unsigned long val;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
tunables->min_sample_time_set[tunables->param_index] = val;
if (tunables->cur_param_index == tunables->param_index)
tunables->min_sample_time = val;
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
tunables->min_sample_time = val;
#endif
return count;
}
static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
char *buf)
{
#ifdef CONFIG_MODE_AUTO_CHANGE
return sprintf(buf, "%lu\n", tunables->timer_rate_set[tunables->param_index]);
#else
return sprintf(buf, "%lu\n", tunables->timer_rate);
#endif
}
static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ret;
unsigned long val;
#ifdef CONFIG_MODE_AUTO_CHANGE
unsigned long flags_idx;
#endif
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_irqsave(&tunables->param_index_lock, flags_idx);
tunables->timer_rate_set[tunables->param_index] = val;
if (tunables->cur_param_index == tunables->param_index)
tunables->timer_rate = val;
spin_unlock_irqrestore(&tunables->param_index_lock, flags_idx);
#else
tunables->timer_rate = val;
#endif
return count;
}
static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
char *buf)
{
return sprintf(buf, "%d\n", tunables->timer_slack_val);
}
static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = kstrtol(buf, 10, &val);
if (ret < 0)
return ret;
tunables->timer_slack_val = val;
return count;
}
static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
char *buf)
{
return sprintf(buf, "%d\n", tunables->boost_val);
}
static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ret;
unsigned long val;
struct cpufreq_policy *policy = container_of(tunables->policy,
struct cpufreq_policy, policy);
ret = kstrtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->boost_val = val;
if (tunables->boost_val) {
trace_cpufreq_interactive_boost("on");
cpufreq_interactive_boost(policy);
} else {
tunables->boostpulse_endtime = ktime_to_us(ktime_get());
trace_cpufreq_interactive_unboost("off");
}
return count;
}
static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ret;
unsigned long val;
struct cpufreq_policy *policy = container_of(tunables->policy,
struct cpufreq_policy, policy);
ret = kstrtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
tunables->boostpulse_duration_val;
trace_cpufreq_interactive_boost("pulse");
cpufreq_interactive_boost(policy);
return count;
}
static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
}
static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = kstrtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->boostpulse_duration_val = val;
return count;
}
static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
char *buf)
{
return sprintf(buf, "%u\n", tunables->io_is_busy);
}
static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
const char *buf, size_t count)
{
int ret;
unsigned long val;
ret = kstrtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->io_is_busy = val;
return count;
}
#ifdef CONFIG_MODE_AUTO_CHANGE
static ssize_t show_mode(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->mode);
}
static ssize_t store_mode(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
val &= MULTI_MODE | SINGLE_MODE | NO_MODE;
tunables->mode = val;
return count;
}
static ssize_t show_enforced_mode(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->enforced_mode);
}
static ssize_t store_enforced_mode(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
val &= MULTI_MODE | SINGLE_MODE | NO_MODE;
tunables->enforced_mode = val;
return count;
}
static ssize_t show_param_index(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->param_index);
}
static ssize_t store_param_index(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
unsigned long flags;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
val &= MULTI_MODE | SINGLE_MODE | NO_MODE;
spin_lock_irqsave(&tunables->param_index_lock, flags);
tunables->param_index = val;
spin_unlock_irqrestore(&tunables->param_index_lock, flags);
return count;
}
static ssize_t show_multi_enter_load(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->multi_enter_load);
}
static ssize_t store_multi_enter_load(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->multi_enter_load = val;
return count;
}
static ssize_t show_multi_exit_load(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->multi_exit_load);
}
static ssize_t store_multi_exit_load(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->multi_exit_load = val;
return count;
}
static ssize_t show_single_enter_load(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->single_enter_load);
}
static ssize_t store_single_enter_load(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->single_enter_load = val;
return count;
}
static ssize_t show_single_exit_load(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->single_exit_load);
}
static ssize_t store_single_exit_load(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->single_exit_load = val;
return count;
}
static ssize_t show_multi_enter_time(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%lu\n", tunables->multi_enter_time);
}
static ssize_t store_multi_enter_time(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->multi_enter_time = val;
return count;
}
static ssize_t show_multi_exit_time(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%lu\n", tunables->multi_exit_time);
}
static ssize_t store_multi_exit_time(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->multi_exit_time = val;
return count;
}
static ssize_t show_single_enter_time(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%lu\n", tunables->single_enter_time);
}
static ssize_t store_single_enter_time(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->single_enter_time = val;
return count;
}
static ssize_t show_single_exit_time(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%lu\n", tunables->single_exit_time);
}
static ssize_t store_single_exit_time(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->single_exit_time = val;
return count;
}
static ssize_t show_cpu_util(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
int i;
u64 now;
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_policy *policy = container_of(tunables->policy,
struct cpufreq_policy, policy);
ssize_t ret = 0;
for_each_cpu_mask(i, policy->related_cpus[0]) {
if (cpu_online(i)) {
pcpu = &per_cpu(cpuinfo, i);
get_cpu_idle_time(i, &now, tunables->io_is_busy);
if (now - pcpu->time_in_idle_timestamp <= tunables->timer_rate)
ret += sprintf(buf + ret, "%3u ", per_cpu(cpu_util, i));
else
ret += sprintf(buf + ret, "%3s ", (pcpu->target_freq == pcpu->policy->max) ? "H_I" : "L_I");
} else
ret += sprintf(buf + ret, "OFF ");
}
sprintf(buf + ret, "\n");
return ret;
}
static ssize_t show_single_cluster0_min_freq(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->single_cluster0_min_freq);
}
static ssize_t store_single_cluster0_min_freq(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->single_cluster0_min_freq = val;
return count;
}
static ssize_t show_multi_cluster0_min_freq(struct cpufreq_interactive_tunables
*tunables, char *buf)
{
return sprintf(buf, "%u\n", tunables->multi_cluster0_min_freq);
}
static ssize_t store_multi_cluster0_min_freq(struct cpufreq_interactive_tunables
*tunables, const char *buf, size_t count)
{
int ret;
long unsigned int val;
ret = strict_strtoul(buf, 0, &val);
if (ret < 0)
return ret;
tunables->multi_cluster0_min_freq = val;
return count;
}
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
static ssize_t show_region_time_in_state(struct cpufreq_interactive_tunables *tunables,
char *buf)
{
int i;
ssize_t len = 0;
region_update_status(tunables);
for (i = 0; i < 6; i++)
len += snprintf(buf + len, PAGE_SIZE, "region %2d: %20llu\n",
i, tunables->region_time_in_state[i]);
return len;
}
#endif
/*
* Create show/store routines
* - sys: One governor instance for complete SYSTEM
* - pol: One governor instance per struct cpufreq_policy
*/
#define show_gov_pol_sys(file_name) \
static ssize_t show_##file_name##_gov_sys \
(struct kobject *kobj, struct attribute *attr, char *buf) \
{ \
return show_##file_name(common_tunables, buf); \
} \
\
static ssize_t show_##file_name##_gov_pol \
(struct cpufreq_policy *policy, char *buf) \
{ \
return show_##file_name(policy->governor_data, buf); \
}
#define store_gov_pol_sys(file_name) \
static ssize_t store_##file_name##_gov_sys \
(struct kobject *kobj, struct attribute *attr, const char *buf, \
size_t count) \
{ \
return store_##file_name(common_tunables, buf, count); \
} \
\
static ssize_t store_##file_name##_gov_pol \
(struct cpufreq_policy *policy, const char *buf, size_t count) \
{ \
return store_##file_name(policy->governor_data, buf, count); \
}
#define show_store_gov_pol_sys(file_name) \
show_gov_pol_sys(file_name); \
store_gov_pol_sys(file_name)
show_store_gov_pol_sys(target_loads);
show_store_gov_pol_sys(above_hispeed_delay);
show_store_gov_pol_sys(hispeed_freq);
show_store_gov_pol_sys(go_hispeed_load);
show_store_gov_pol_sys(min_sample_time);
show_store_gov_pol_sys(timer_rate);
show_store_gov_pol_sys(timer_slack);
show_store_gov_pol_sys(boost);
store_gov_pol_sys(boostpulse);
show_store_gov_pol_sys(boostpulse_duration);
show_store_gov_pol_sys(io_is_busy);
#ifdef CONFIG_MODE_AUTO_CHANGE
show_store_gov_pol_sys(mode);
show_store_gov_pol_sys(enforced_mode);
show_store_gov_pol_sys(param_index);
show_store_gov_pol_sys(multi_enter_load);
show_store_gov_pol_sys(multi_exit_load);
show_store_gov_pol_sys(single_enter_load);
show_store_gov_pol_sys(single_exit_load);
show_store_gov_pol_sys(multi_enter_time);
show_store_gov_pol_sys(multi_exit_time);
show_store_gov_pol_sys(single_enter_time);
show_store_gov_pol_sys(single_exit_time);
show_store_gov_pol_sys(single_cluster0_min_freq);
show_store_gov_pol_sys(multi_cluster0_min_freq);
show_gov_pol_sys(cpu_util);
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
show_gov_pol_sys(region_time_in_state);
#endif
#define gov_sys_attr_rw(_name) \
static struct global_attr _name##_gov_sys = \
__ATTR(_name, 0660, show_##_name##_gov_sys, store_##_name##_gov_sys)
#define gov_pol_attr_rw(_name) \
static struct freq_attr _name##_gov_pol = \
__ATTR(_name, 0660, show_##_name##_gov_pol, store_##_name##_gov_pol)
#define gov_sys_pol_attr_rw(_name) \
gov_sys_attr_rw(_name); \
gov_pol_attr_rw(_name)
gov_sys_pol_attr_rw(target_loads);
gov_sys_pol_attr_rw(above_hispeed_delay);
gov_sys_pol_attr_rw(hispeed_freq);
gov_sys_pol_attr_rw(go_hispeed_load);
gov_sys_pol_attr_rw(min_sample_time);
gov_sys_pol_attr_rw(timer_rate);
gov_sys_pol_attr_rw(timer_slack);
gov_sys_pol_attr_rw(boost);
gov_sys_pol_attr_rw(boostpulse_duration);
gov_sys_pol_attr_rw(io_is_busy);
#ifdef CONFIG_MODE_AUTO_CHANGE
gov_sys_pol_attr_rw(mode);
gov_sys_pol_attr_rw(enforced_mode);
gov_sys_pol_attr_rw(param_index);
gov_sys_pol_attr_rw(multi_enter_load);
gov_sys_pol_attr_rw(multi_exit_load);
gov_sys_pol_attr_rw(single_enter_load);
gov_sys_pol_attr_rw(single_exit_load);
gov_sys_pol_attr_rw(multi_enter_time);
gov_sys_pol_attr_rw(multi_exit_time);
gov_sys_pol_attr_rw(single_enter_time);
gov_sys_pol_attr_rw(single_exit_time);
gov_sys_pol_attr_rw(single_cluster0_min_freq);
gov_sys_pol_attr_rw(multi_cluster0_min_freq);
#endif
static struct global_attr boostpulse_gov_sys =
__ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
static struct freq_attr boostpulse_gov_pol =
__ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
#ifdef CONFIG_MODE_AUTO_CHANGE
static struct global_attr cpu_util_gov_sys =
__ATTR(cpu_util, 0444, show_cpu_util_gov_sys, NULL);
static struct freq_attr cpu_util_gov_pol =
__ATTR(cpu_util, 0444, show_cpu_util_gov_pol, NULL);
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
static struct global_attr region_time_in_state_gov_sys =
__ATTR(region_time_in_state, 0440, show_region_time_in_state_gov_sys, NULL);
static struct freq_attr region_time_in_state_gov_pol =
__ATTR(region_time_in_state, 0440, show_region_time_in_state_gov_pol, NULL);
#endif
/* One Governor instance for entire system */
static struct attribute *interactive_attributes_gov_sys[] = {
&target_loads_gov_sys.attr,
&above_hispeed_delay_gov_sys.attr,
&hispeed_freq_gov_sys.attr,
&go_hispeed_load_gov_sys.attr,
&min_sample_time_gov_sys.attr,
&timer_rate_gov_sys.attr,
&timer_slack_gov_sys.attr,
&boost_gov_sys.attr,
&boostpulse_gov_sys.attr,
&boostpulse_duration_gov_sys.attr,
&io_is_busy_gov_sys.attr,
#ifdef CONFIG_MODE_AUTO_CHANGE
&mode_gov_sys.attr,
&enforced_mode_gov_sys.attr,
¶m_index_gov_sys.attr,
&multi_enter_load_gov_sys.attr,
&multi_exit_load_gov_sys.attr,
&single_enter_load_gov_sys.attr,
&single_exit_load_gov_sys.attr,
&multi_enter_time_gov_sys.attr,
&multi_exit_time_gov_sys.attr,
&single_enter_time_gov_sys.attr,
&single_exit_time_gov_sys.attr,
&single_cluster0_min_freq_gov_sys.attr,
&multi_cluster0_min_freq_gov_sys.attr,
&cpu_util_gov_sys.attr,
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
®ion_time_in_state_gov_sys.attr,
#endif
NULL,
};
static struct attribute_group interactive_attr_group_gov_sys = {
.attrs = interactive_attributes_gov_sys,
.name = "interactive",
};
/* Per policy governor instance */
static struct attribute *interactive_attributes_gov_pol[] = {
&target_loads_gov_pol.attr,
&above_hispeed_delay_gov_pol.attr,
&hispeed_freq_gov_pol.attr,
&go_hispeed_load_gov_pol.attr,
&min_sample_time_gov_pol.attr,
&timer_rate_gov_pol.attr,
&timer_slack_gov_pol.attr,
&boost_gov_pol.attr,
&boostpulse_gov_pol.attr,
&boostpulse_duration_gov_pol.attr,
&io_is_busy_gov_pol.attr,
#ifdef CONFIG_MODE_AUTO_CHANGE
&mode_gov_pol.attr,
&enforced_mode_gov_pol.attr,
¶m_index_gov_pol.attr,
&multi_enter_load_gov_pol.attr,
&multi_exit_load_gov_pol.attr,
&single_enter_load_gov_pol.attr,
&single_exit_load_gov_pol.attr,
&multi_enter_time_gov_pol.attr,
&multi_exit_time_gov_pol.attr,
&single_enter_time_gov_pol.attr,
&single_exit_time_gov_pol.attr,
&single_cluster0_min_freq_gov_pol.attr,
&multi_cluster0_min_freq_gov_pol.attr,
&cpu_util_gov_pol.attr,
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
®ion_time_in_state_gov_pol.attr,
#endif
NULL,
};
static struct attribute_group interactive_attr_group_gov_pol = {
.attrs = interactive_attributes_gov_pol,
.name = "interactive",
};
static struct attribute_group *get_sysfs_attr(void)
{
if (have_governor_per_policy())
return &interactive_attr_group_gov_pol;
else
return &interactive_attr_group_gov_sys;
}
#ifdef CONFIG_PMU_COREMEM_RATIO
void pmu_suspend_cpu(int cpu)
{
int ret;
ret = stop_counter_cpu(cpu);
if (ret)
pr_err("%s: Fail to stop counter. cpu: %d\n", __func__, cpu);
}
void pmu_resume_cpu(int cpu)
{
int ret;
ret = start_counter_cpu(cpu);
if (ret)
pr_err("%s: Fail to start counter. cpu: %d\n", __func__, cpu);
}
#endif
static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
unsigned long val,
void *data)
{
#ifdef CONFIG_PMU_COREMEM_RATIO
int cpu = smp_processor_id();
#endif
switch (val) {
case IDLE_START:
cpufreq_interactive_idle_start();
#ifdef CONFIG_PMU_COREMEM_RATIO
pmu_suspend_cpu(cpu);
#endif
break;
case IDLE_END:
cpufreq_interactive_idle_end();
#ifdef CONFIG_PMU_COREMEM_RATIO
pmu_resume_cpu(cpu);
#endif
break;
}
return 0;
}
static struct notifier_block cpufreq_interactive_idle_nb = {
.notifier_call = cpufreq_interactive_idle_notifier,
};
#ifdef CONFIG_PMU_COREMEM_RATIO
static int __cpuinit exynos_pmu_cpu_notifier(struct notifier_block *nfb,
unsigned long action, void *hcpu)
{
int cpu = (unsigned long)hcpu;
switch (action & ~CPU_TASKS_FROZEN) {
case CPU_STARTING:
case CPU_DOWN_FAILED:
pmu_resume_cpu(cpu);
break;
case CPU_DYING:
pmu_suspend_cpu(cpu);
break;
}
return NOTIFY_OK;
}
static struct notifier_block __cpuinitdata exynos_pmu_cpu_notifier_block = {
.notifier_call = exynos_pmu_cpu_notifier,
};
#endif
#ifdef CONFIG_MODE_AUTO_CHANGE
static void cpufreq_param_set_init(struct cpufreq_interactive_tunables *tunables)
{
unsigned int i;
tunables->multi_enter_load = DEFAULT_TARGET_LOAD * num_possible_cpus() / 2;
for (i = 0; i < MAX_PARAM_SET; i++) {
tunables->hispeed_freq_set[i] = 0;
tunables->go_hispeed_load_set[i] = tunables->go_hispeed_load;
tunables->target_loads_set[i] = tunables->target_loads;
tunables->ntarget_loads_set[i] = tunables->ntarget_loads;
tunables->min_sample_time_set[i] = tunables->min_sample_time;
tunables->timer_rate_set[i] = tunables->timer_rate;
tunables->above_hispeed_delay_set[i] = tunables->above_hispeed_delay;
tunables->nabove_hispeed_delay_set[i] = tunables->nabove_hispeed_delay;
tunables->sampling_down_factor_set[i] = tunables->sampling_down_factor;
}
}
#endif
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;
struct cpufreq_interactive_tunables *tunables;
struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
char speedchange_task_name[TASK_NAME_LEN];
unsigned long flags;
#ifdef CONFIG_PMU_COREMEM_RATIO
char regionchange_task_name[TASK_NAME_LEN];
#endif
if (have_governor_per_policy())
tunables = policy->governor_data;
else
tunables = common_tunables;
WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
switch (event) {
case CPUFREQ_GOV_POLICY_INIT:
if (have_governor_per_policy()) {
WARN_ON(tunables);
} else if (tunables) {
tunables->usage_count++;
policy->governor_data = tunables;
return 0;
}
tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
if (!tunables) {
pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
return -ENOMEM;
}
if (!tuned_parameters[policy->cpu]) {
tunables->above_hispeed_delay = default_above_hispeed_delay;
tunables->nabove_hispeed_delay =
ARRAY_SIZE(default_above_hispeed_delay);
tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
tunables->target_loads = default_target_loads;
tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
tunables->timer_rate = DEFAULT_TIMER_RATE;
tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
#ifdef CONFIG_MODE_AUTO_CHANGE
tunables->multi_enter_time = DEFAULT_MULTI_ENTER_TIME;
tunables->multi_enter_load = 4 * DEFAULT_TARGET_LOAD;
tunables->multi_exit_time = DEFAULT_MULTI_EXIT_TIME;
tunables->multi_exit_load = 4 * DEFAULT_TARGET_LOAD;
tunables->single_enter_time = DEFAULT_SINGLE_ENTER_TIME;
tunables->single_enter_load = DEFAULT_TARGET_LOAD;
tunables->single_exit_time = DEFAULT_SINGLE_EXIT_TIME;
tunables->single_exit_load = DEFAULT_TARGET_LOAD;
tunables->single_cluster0_min_freq = DEFAULT_SINGLE_CLUSTER0_MIN_FREQ;
tunables->multi_cluster0_min_freq = DEFAULT_MULTI_CLUSTER0_MIN_FREQ;
cpufreq_param_set_init(tunables);
#endif
#ifdef CONFIG_PMU_COREMEM_RATIO
tunables->region_last_stat = jiffies;
#endif
} else {
memcpy(tunables, tuned_parameters[policy->cpu], sizeof(*tunables));
kfree(tuned_parameters[policy->cpu]);
}
tunables->usage_count = 1;
/* update handle for get cpufreq_policy */
tunables->policy = &policy->policy;
spin_lock_init(&tunables->target_loads_lock);
spin_lock_init(&tunables->above_hispeed_delay_lock);
#ifdef CONFIG_MODE_AUTO_CHANGE
spin_lock_init(&tunables->mode_lock);
spin_lock_init(&tunables->param_index_lock);
#endif
policy->governor_data = tunables;
if (!have_governor_per_policy())
common_tunables = tunables;
rc = sysfs_create_group(get_governor_parent_kobj(policy),
get_sysfs_attr());
if (rc) {
kfree(tunables);
policy->governor_data = NULL;
if (!have_governor_per_policy())
common_tunables = NULL;
return rc;
}
if (!policy->governor->initialized) {
idle_notifier_register(&cpufreq_interactive_idle_nb);
#ifdef CONFIG_PMU_COREMEM_RATIO
register_cpu_notifier(&exynos_pmu_cpu_notifier_block);
#endif
cpufreq_register_notifier(&cpufreq_notifier_block,
CPUFREQ_TRANSITION_NOTIFIER);
}
break;
case CPUFREQ_GOV_POLICY_EXIT:
if (!--tunables->usage_count) {
if (policy->governor->initialized == 1) {
cpufreq_unregister_notifier(&cpufreq_notifier_block,
CPUFREQ_TRANSITION_NOTIFIER);
#ifdef CONFIG_PMU_COREMEM_RATIO
unregister_cpu_notifier(&exynos_pmu_cpu_notifier_block);
#endif
idle_notifier_unregister(&cpufreq_interactive_idle_nb);
}
sysfs_remove_group(get_governor_parent_kobj(policy),
get_sysfs_attr());
tuned_parameters[policy->cpu] = kzalloc(sizeof(*tunables), GFP_KERNEL);
if (!tuned_parameters[policy->cpu]) {
pr_err("%s: POLICY_EXIT: kzalloc failed\n", __func__);
return -ENOMEM;
}
memcpy(tuned_parameters[policy->cpu], tunables, sizeof(*tunables));
kfree(tunables);
common_tunables = NULL;
}
policy->governor_data = NULL;
break;
case CPUFREQ_GOV_START:
mutex_lock(&gov_lock);
freq_table = cpufreq_frequency_get_table(policy->cpu);
if (!tunables->hispeed_freq)
tunables->hispeed_freq = policy->max;
#ifdef CONFIG_MODE_AUTO_CHANGE
for (j = 0; j < MAX_PARAM_SET; j++) {
if (!tunables->hispeed_freq_set[j])
tunables->hispeed_freq_set[j] = policy->max;
}
#endif
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->floor_freq = pcpu->target_freq;
pcpu->floor_validate_time =
ktime_to_us(ktime_get());
pcpu->hispeed_validate_time =
pcpu->floor_validate_time;
pcpu->max_freq = policy->max;
down_write(&pcpu->enable_sem);
del_timer_sync(&pcpu->cpu_timer);
del_timer_sync(&pcpu->cpu_slack_timer);
cpufreq_interactive_timer_start(tunables, j);
pcpu->governor_enabled = 1;
up_write(&pcpu->enable_sem);
}
snprintf(speedchange_task_name, TASK_NAME_LEN, "cfinteractive%d\n",
policy->cpu);
tunables->speedchange_task =
kthread_create(cpufreq_interactive_speedchange_task, NULL,
speedchange_task_name);
if (IS_ERR(tunables->speedchange_task)) {
mutex_unlock(&gov_lock);
return PTR_ERR(tunables->speedchange_task);
}
sched_setscheduler_nocheck(tunables->speedchange_task, SCHED_FIFO, ¶m);
get_task_struct(tunables->speedchange_task);
#ifdef CONFIG_PMU_COREMEM_RATIO
snprintf(regionchange_task_name, TASK_NAME_LEN, "region%d",
policy->cpu);
tunables->regionchange_task =
kthread_create(cpufreq_interactive_regionchange_task, NULL,
regionchange_task_name);
if (IS_ERR(tunables->regionchange_task)) {
kthread_stop(tunables->regionchange_task);
mutex_unlock(&gov_lock);
return PTR_ERR(tunables->regionchange_task);
}
sched_setscheduler_nocheck(tunables->regionchange_task, SCHED_FIFO, ¶m);
get_task_struct(tunables->regionchange_task);
#endif
#if defined(CONFIG_ARM_EXYNOS_MP_CPUFREQ) || defined(CONFIG_ARM_EXYNOS_SMP_CPUFREQ)
kthread_bind(tunables->speedchange_task, policy->cpu);
#ifdef CONFIG_PMU_COREMEM_RATIO
kthread_bind(tunables->regionchange_task, policy->cpu);
#endif
#endif
/* NB: wake up so the thread does not look hung to the freezer */
wake_up_process(tunables->speedchange_task);
#ifdef CONFIG_PMU_COREMEM_RATIO
wake_up_process(tunables->regionchange_task);
#endif
mutex_unlock(&gov_lock);
break;
case CPUFREQ_GOV_STOP:
mutex_lock(&gov_lock);
for_each_cpu(j, policy->cpus) {
pcpu = &per_cpu(cpuinfo, j);
down_write(&pcpu->enable_sem);
pcpu->governor_enabled = 0;
del_timer_sync(&pcpu->cpu_timer);
del_timer_sync(&pcpu->cpu_slack_timer);
up_write(&pcpu->enable_sem);
}
kthread_stop(tunables->speedchange_task);
put_task_struct(tunables->speedchange_task);
tunables->speedchange_task = NULL;
#ifdef CONFIG_PMU_COREMEM_RATIO
kthread_stop(tunables->regionchange_task);
put_task_struct(tunables->regionchange_task);
tunables->regionchange_task = NULL;
#endif
mutex_unlock(&gov_lock);
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);
for_each_cpu(j, policy->cpus) {
pcpu = &per_cpu(cpuinfo, j);
down_read(&pcpu->enable_sem);
if (pcpu->governor_enabled == 0) {
up_read(&pcpu->enable_sem);
continue;
}
spin_lock_irqsave(&pcpu->target_freq_lock, flags);
if (policy->max < pcpu->target_freq)
pcpu->target_freq = policy->max;
else if (policy->min > pcpu->target_freq)
pcpu->target_freq = policy->min;
spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
up_read(&pcpu->enable_sem);
/* Reschedule timer only if policy->max is raised.
* Delete the timers, else the timer callback may
* return without re-arm the timer when failed
* acquire the semaphore. This race may cause timer
* stopped unexpectedly.
*/
if (policy->max > pcpu->max_freq) {
down_write(&pcpu->enable_sem);
del_timer_sync(&pcpu->cpu_timer);
del_timer_sync(&pcpu->cpu_slack_timer);
cpufreq_interactive_timer_start(tunables, j);
up_write(&pcpu->enable_sem);
}
pcpu->max_freq = policy->max;
}
break;
}
return 0;
}
#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 void cpufreq_interactive_nop_timer(unsigned long data)
{
}
unsigned int cpufreq_interactive_get_hispeed_freq(int cpu)
{
struct cpufreq_interactive_cpuinfo *pcpu =
&per_cpu(cpuinfo, cpu);
struct cpufreq_interactive_tunables *tunables;
if (pcpu && pcpu->policy)
tunables = pcpu->policy->governor_data;
else
return 0;
if (!tunables)
return 0;
return tunables->hispeed_freq;
}
#ifdef CONFIG_ARCH_EXYNOS
static int cpufreq_interactive_cluster1_min_qos_handler(struct notifier_block *b,
unsigned long val, void *v)
{
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_interactive_tunables *tunables;
unsigned long flags;
int ret = NOTIFY_OK;
#if defined(CONFIG_ARM_EXYNOS_MP_CPUFREQ)
int cpu = NR_CLUST0_CPUS;
#else
int cpu = 0;
#endif
pcpu = &per_cpu(cpuinfo, cpu);
mutex_lock(&gov_lock);
down_read(&pcpu->enable_sem);
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
ret = NOTIFY_BAD;
goto exit;
}
up_read(&pcpu->enable_sem);
if (!pcpu->policy || !pcpu->policy->governor_data ||
!pcpu->policy->user_policy.governor) {
ret = NOTIFY_BAD;
goto exit;
}
trace_cpufreq_interactive_cpu_min_qos(cpu, val, pcpu->policy->cur);
if (val < pcpu->policy->cur) {
tunables = pcpu->policy->governor_data;
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
cpumask_set_cpu(cpu, &speedchange_cpumask);
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
if (tunables->speedchange_task)
wake_up_process(tunables->speedchange_task);
}
exit:
mutex_unlock(&gov_lock);
return ret;
}
static struct notifier_block cpufreq_interactive_cluster1_min_qos_notifier = {
.notifier_call = cpufreq_interactive_cluster1_min_qos_handler,
};
static int cpufreq_interactive_cluster1_max_qos_handler(struct notifier_block *b,
unsigned long val, void *v)
{
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_interactive_tunables *tunables;
unsigned long flags;
int ret = NOTIFY_OK;
#if defined(CONFIG_ARM_EXYNOS_MP_CPUFREQ)
int cpu = NR_CLUST0_CPUS;
#else
int cpu = 0;
#endif
pcpu = &per_cpu(cpuinfo, cpu);
mutex_lock(&gov_lock);
down_read(&pcpu->enable_sem);
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
ret = NOTIFY_BAD;
goto exit;
}
up_read(&pcpu->enable_sem);
if (!pcpu->policy || !pcpu->policy->governor_data ||
!pcpu->policy->user_policy.governor) {
ret = NOTIFY_BAD;
goto exit;
}
trace_cpufreq_interactive_cpu_max_qos(cpu, val, pcpu->policy->cur);
if (val > pcpu->policy->cur) {
tunables = pcpu->policy->governor_data;
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
cpumask_set_cpu(cpu, &speedchange_cpumask);
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
if (tunables->speedchange_task)
wake_up_process(tunables->speedchange_task);
}
exit:
mutex_unlock(&gov_lock);
return ret;
}
static struct notifier_block cpufreq_interactive_cluster1_max_qos_notifier = {
.notifier_call = cpufreq_interactive_cluster1_max_qos_handler,
};
#ifdef CONFIG_ARM_EXYNOS_MP_CPUFREQ
static int cpufreq_interactive_cluster0_min_qos_handler(struct notifier_block *b,
unsigned long val, void *v)
{
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_interactive_tunables *tunables;
unsigned long flags;
int ret = NOTIFY_OK;
pcpu = &per_cpu(cpuinfo, 0);
mutex_lock(&gov_lock);
down_read(&pcpu->enable_sem);
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
ret = NOTIFY_BAD;
goto exit;
}
up_read(&pcpu->enable_sem);
if (!pcpu->policy || !pcpu->policy->governor_data ||
!pcpu->policy->user_policy.governor) {
ret = NOTIFY_BAD;
goto exit;
}
trace_cpufreq_interactive_kfc_min_qos(0, val, pcpu->policy->cur);
if (val < pcpu->policy->cur) {
tunables = pcpu->policy->governor_data;
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
cpumask_set_cpu(0, &speedchange_cpumask);
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
if (tunables->speedchange_task)
wake_up_process(tunables->speedchange_task);
}
exit:
mutex_unlock(&gov_lock);
return ret;
}
static struct notifier_block cpufreq_interactive_cluster0_min_qos_notifier = {
.notifier_call = cpufreq_interactive_cluster0_min_qos_handler,
};
static int cpufreq_interactive_cluster0_max_qos_handler(struct notifier_block *b,
unsigned long val, void *v)
{
struct cpufreq_interactive_cpuinfo *pcpu;
struct cpufreq_interactive_tunables *tunables;
unsigned long flags;
int ret = NOTIFY_OK;
pcpu = &per_cpu(cpuinfo, 0);
mutex_lock(&gov_lock);
down_read(&pcpu->enable_sem);
if (!pcpu->governor_enabled) {
up_read(&pcpu->enable_sem);
ret = NOTIFY_BAD;
goto exit;
}
up_read(&pcpu->enable_sem);
if (!pcpu->policy ||!pcpu->policy->governor_data ||
!pcpu->policy->user_policy.governor) {
ret = NOTIFY_BAD;
goto exit;
}
trace_cpufreq_interactive_kfc_max_qos(0, val, pcpu->policy->cur);
if (val > pcpu->policy->cur) {
tunables = pcpu->policy->governor_data;
spin_lock_irqsave(&speedchange_cpumask_lock, flags);
cpumask_set_cpu(0, &speedchange_cpumask);
spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
if (tunables->speedchange_task)
wake_up_process(tunables->speedchange_task);
}
exit:
mutex_unlock(&gov_lock);
return ret;
}
static struct notifier_block cpufreq_interactive_cluster0_max_qos_notifier = {
.notifier_call = cpufreq_interactive_cluster0_max_qos_handler,
};
#endif
#endif
static int __init cpufreq_interactive_init(void)
{
unsigned int i;
struct cpufreq_interactive_cpuinfo *pcpu;
/* Initalize per-cpu timers */
for_each_possible_cpu(i) {
pcpu = &per_cpu(cpuinfo, i);
init_timer_deferrable(&pcpu->cpu_timer);
pcpu->cpu_timer.function = cpufreq_interactive_timer;
pcpu->cpu_timer.data = i;
init_timer(&pcpu->cpu_slack_timer);
pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
spin_lock_init(&pcpu->load_lock);
spin_lock_init(&pcpu->target_freq_lock);
init_rwsem(&pcpu->enable_sem);
}
spin_lock_init(&speedchange_cpumask_lock);
#ifdef CONFIG_PMU_COREMEM_RATIO
spin_lock_init(®ionchange_cpumask_lock);
#endif
mutex_init(&gov_lock);
#ifdef CONFIG_ARCH_EXYNOS
pm_qos_add_notifier(PM_QOS_CLUSTER1_FREQ_MIN, &cpufreq_interactive_cluster1_min_qos_notifier);
pm_qos_add_notifier(PM_QOS_CLUSTER1_FREQ_MAX, &cpufreq_interactive_cluster1_max_qos_notifier);
#ifdef CONFIG_ARM_EXYNOS_MP_CPUFREQ
pm_qos_add_notifier(PM_QOS_CLUSTER0_FREQ_MIN, &cpufreq_interactive_cluster0_min_qos_notifier);
pm_qos_add_notifier(PM_QOS_CLUSTER0_FREQ_MAX, &cpufreq_interactive_cluster0_max_qos_notifier);
#endif
#endif
#ifdef CONFIG_MODE_AUTO_CHANGE
mode_auto_change_minlock_wq = alloc_workqueue("mode_auto_change_minlock_wq", WQ_HIGHPRI, 0);
if(!mode_auto_change_minlock_wq)
pr_info("mode auto change minlock workqueue init error\n");
INIT_WORK(&mode_auto_change_minlock_work, mode_auto_change_minlock);
#endif
return cpufreq_register_governor(&cpufreq_gov_interactive);
}
#ifdef CONFIG_MODE_AUTO_CHANGE
static void mode_auto_change_minlock(struct work_struct *work)
{
set_qos(&cluster0_min_freq_qos, PM_QOS_CLUSTER0_FREQ_MIN, cluster0_min_freq);
}
#endif
#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);
}
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");
|