-
Notifications
You must be signed in to change notification settings - Fork 2
/
KernelPatch
1957 lines (1948 loc) · 54.9 KB
/
KernelPatch
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
diff -uNr Vanilla/drivers/cpufreq/cpufreq_interactivex.c New/drivers/cpufreq/cpufreq_interactivex.c
--- Vanilla/drivers/cpufreq/cpufreq_interactivex.c 1969-12-31 18:00:00.000000000 -0600
+++ New/drivers/cpufreq/cpufreq_interactivex.c 2012-06-30 19:56:47.302115034 -0500
@@ -0,0 +1,404 @@
+/*
+ * drivers/cpufreq/cpufreq_interactivex.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 ([email protected]) - modified for suspend/wake by imoseyon
+ *
+ */
+
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+#include <linux/cpufreq.h>
+#include <linux/mutex.h>
+#include <linux/sched.h>
+#include <linux/tick.h>
+#include <linux/timer.h>
+#include <linux/workqueue.h>
+#include <linux/earlysuspend.h>
+
+#include <asm/cputime.h>
+
+static void (*pm_idle_old)(void);
+static atomic_t active_count = ATOMIC_INIT(0);
+
+static DEFINE_PER_CPU(struct timer_list, cpu_timer);
+
+static DEFINE_PER_CPU(u64, time_in_idle);
+static DEFINE_PER_CPU(u64, idle_exit_time);
+
+static struct cpufreq_policy *policy;
+static unsigned int target_freq;
+
+/* Workqueues handle frequency scaling */
+static struct workqueue_struct *up_wq;
+static struct workqueue_struct *down_wq;
+static struct work_struct freq_scale_work;
+
+static u64 freq_change_time;
+static u64 freq_change_time_in_idle;
+
+static cpumask_t work_cpumask;
+
+static unsigned int suspended = 0;
+static unsigned int enabled = 0;
+
+/*
+ * The minimum ammount of time to spend at a frequency before we can ramp down,
+ * default is 50ms.
+ */
+#define DEFAULT_MIN_SAMPLE_TIME 50000;
+static unsigned long min_sample_time;
+
+#define FREQ_THRESHOLD 1024000;
+static unsigned int freq_threshld;
+
+#define RESUME_SPEED 1024000;
+static unsigned int resum_speed;
+
+static int cpufreq_governor_interactivex(struct cpufreq_policy *policy,
+ unsigned int event);
+
+#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVEX
+static
+#endif
+struct cpufreq_governor cpufreq_gov_interactivex = {
+ .name = "interactiveX",
+ .governor = cpufreq_governor_interactivex,
+#if defined(CONFIG_ARCH_MSM_SCORPION)
+ .max_transition_latency = 8000000,
+#else
+ .max_transition_latency = 10000000,
+#endif
+ .owner = THIS_MODULE,
+};
+
+static void cpufreq_interactivex_timer(unsigned long data)
+{
+ u64 delta_idle;
+ u64 update_time;
+ u64 *cpu_time_in_idle;
+ u64 *cpu_idle_exit_time;
+ struct timer_list *t;
+
+ u64 now_idle = get_cpu_idle_time_us(data,
+ &update_time);
+
+
+ cpu_time_in_idle = &per_cpu(time_in_idle, data);
+ cpu_idle_exit_time = &per_cpu(idle_exit_time, data);
+
+ if (update_time == *cpu_idle_exit_time)
+ return;
+
+ delta_idle = cputime64_sub(now_idle, *cpu_time_in_idle);
+
+ /* Scale up if there were no idle cycles since coming out of idle */
+ if (delta_idle == 0) {
+ if (policy->cur == policy->max)
+ return;
+
+ if (nr_running() < 1)
+ return;
+
+ target_freq = policy->max;
+
+ cpumask_set_cpu(data, &work_cpumask);
+ queue_work(up_wq, &freq_scale_work);
+ return;
+ }
+
+ /*
+ * There is a window where if the cpu utlization can go from low to high
+ * between the timer expiring, delta_idle will be > 0 and the cpu will
+ * be 100% busy, preventing idle from running, and this timer from
+ * firing. So setup another timer to fire to check cpu utlization.
+ * Do not setup the timer if there is no scheduled work.
+ */
+ t = &per_cpu(cpu_timer, data);
+ if (!timer_pending(t) && nr_running() > 0) {
+ *cpu_time_in_idle = get_cpu_idle_time_us(
+ data, cpu_idle_exit_time);
+ mod_timer(t, jiffies + 2);
+ }
+
+ if (policy->cur == policy->min)
+ return;
+
+ /*
+ * Do not scale down unless we have been at this frequency for the
+ * minimum sample time.
+ */
+ if (cputime64_sub(update_time, freq_change_time) < min_sample_time)
+ return;
+
+ target_freq = policy->min;
+ cpumask_set_cpu(data, &work_cpumask);
+ queue_work(down_wq, &freq_scale_work);
+}
+
+static void cpufreq_idle(void)
+{
+ struct timer_list *t;
+ u64 *cpu_time_in_idle;
+ u64 *cpu_idle_exit_time;
+
+ pm_idle_old();
+
+ if (!cpumask_test_cpu(smp_processor_id(), policy->cpus))
+ return;
+
+ /* Timer to fire in 1-2 ticks, jiffie aligned. */
+ t = &per_cpu(cpu_timer, smp_processor_id());
+ cpu_idle_exit_time = &per_cpu(idle_exit_time, smp_processor_id());
+ cpu_time_in_idle = &per_cpu(time_in_idle, smp_processor_id());
+
+ if (timer_pending(t) == 0) {
+ *cpu_time_in_idle = get_cpu_idle_time_us(
+ smp_processor_id(), cpu_idle_exit_time);
+ mod_timer(t, jiffies + 2);
+ }
+}
+
+/*
+ * Choose the cpu frequency based off the load. For now choose the minimum
+ * frequency that will satisfy the load, which is not always the lower power.
+ */
+static unsigned int cpufreq_interactivex_calc_freq(unsigned int cpu)
+{
+ unsigned int delta_time;
+ unsigned int idle_time;
+ unsigned int cpu_load;
+ unsigned int newfreq;
+ u64 current_wall_time;
+ u64 current_idle_time;;
+
+ current_idle_time = get_cpu_idle_time_us(cpu, ¤t_wall_time);
+
+ idle_time = (unsigned int) current_idle_time - freq_change_time_in_idle;
+ delta_time = (unsigned int) current_wall_time - freq_change_time;
+
+ cpu_load = 100 * (delta_time - idle_time) / delta_time;
+
+ if (cpu_load > 98) newfreq = policy->max;
+ else newfreq = policy->cur * cpu_load / 100;
+
+ return newfreq;
+}
+
+
+/* We use the same work function to sale up and down */
+static void cpufreq_interactivex_freq_change_time_work(struct work_struct *work)
+{
+ unsigned int cpu;
+ unsigned int newtarget;
+ cpumask_t tmp_mask = work_cpumask;
+ newtarget = freq_threshld;
+
+ for_each_cpu(cpu, tmp_mask) {
+ if (!suspended) {
+ if (target_freq == policy->max) {
+ if (nr_running() == 1) {
+ cpumask_clear_cpu(cpu, &work_cpumask);
+ return;
+ }
+// __cpufreq_driver_target(policy, target_freq, CPUFREQ_RELATION_H);
+ __cpufreq_driver_target(policy, newtarget, CPUFREQ_RELATION_H);
+ } else {
+ target_freq = cpufreq_interactivex_calc_freq(cpu);
+ __cpufreq_driver_target(policy, target_freq,
+ CPUFREQ_RELATION_L);
+ }
+ }
+ freq_change_time_in_idle = get_cpu_idle_time_us(cpu, &freq_change_time);
+ cpumask_clear_cpu(cpu, &work_cpumask);
+ }
+
+
+}
+
+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)
+{
+ return strict_strtoul(buf, 0, &min_sample_time);
+}
+
+static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
+ show_min_sample_time, store_min_sample_time);
+
+static struct attribute *interactivex_attributes[] = {
+ &min_sample_time_attr.attr,
+ NULL,
+};
+
+static struct attribute_group interactivex_attr_group = {
+ .attrs = interactivex_attributes,
+ .name = "interactiveX",
+};
+
+static void interactivex_suspend(int suspend)
+{
+ unsigned int max_speed;
+
+ max_speed = resum_speed;
+
+ if (!enabled) return;
+ if (!suspend) { // resume at max speed:
+ suspended = 0;
+ __cpufreq_driver_target(policy, max_speed, CPUFREQ_RELATION_L);
+ pr_info("[imoseyon] interactiveX awake at %d\n", policy->cur);
+ } else {
+ suspended = 1;
+ __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
+ pr_info("[imoseyon] interactiveX suspended at %d\n", policy->cur);
+ }
+}
+
+static void interactivex_early_suspend(struct early_suspend *handler) {
+ interactivex_suspend(1);
+}
+
+static void interactivex_late_resume(struct early_suspend *handler) {
+ interactivex_suspend(0);
+}
+
+static struct early_suspend interactivex_power_suspend = {
+ .suspend = interactivex_early_suspend,
+ .resume = interactivex_late_resume,
+ .level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1,
+};
+
+static int cpufreq_governor_interactivex(struct cpufreq_policy *new_policy,
+ unsigned int event)
+{
+ int rc;
+ unsigned int min_freq = ~0;
+ unsigned int max_freq = 0;
+ unsigned int i;
+ struct cpufreq_frequency_table *freq_table;
+
+ switch (event) {
+ case CPUFREQ_GOV_START:
+ if (!cpu_online(new_policy->cpu))
+ return -EINVAL;
+
+ /*
+ * 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,
+ &interactivex_attr_group);
+ if (rc)
+ return rc;
+
+ pm_idle_old = pm_idle;
+ pm_idle = cpufreq_idle;
+ policy = new_policy;
+ enabled = 1;
+ register_early_suspend(&interactivex_power_suspend);
+ pr_info("[imoseyon] interactiveX active\n");
+ freq_table = cpufreq_frequency_get_table(new_policy->cpu);
+ for (i = 0; (freq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
+ unsigned int freq = freq_table[i].frequency;
+ if (freq == CPUFREQ_ENTRY_INVALID) {
+ continue;
+ }
+ if (freq < min_freq)
+ min_freq = freq;
+ if (freq > max_freq)
+ max_freq = freq;
+ }
+ resum_speed = freq_table[(i-1)/2].frequency > min_freq ? freq_table[(i-1)/2].frequency : max_freq; //Value in midrange of available CPU frequencies if sufficient number of freq bins available
+ freq_threshld = max_freq;
+ break;
+
+ case CPUFREQ_GOV_STOP:
+ if (atomic_dec_return(&active_count) > 1)
+ return 0;
+
+ sysfs_remove_group(cpufreq_global_kobject,
+ &interactivex_attr_group);
+
+ pm_idle = pm_idle_old;
+ del_timer(&per_cpu(cpu_timer, new_policy->cpu));
+ enabled = 0;
+ unregister_early_suspend(&interactivex_power_suspend);
+ pr_info("[imoseyon] interactiveX inactive\n");
+ break;
+
+ case CPUFREQ_GOV_LIMITS:
+ if (new_policy->max < new_policy->cur)
+ __cpufreq_driver_target(new_policy,
+ new_policy->max, CPUFREQ_RELATION_H);
+ else if (new_policy->min > new_policy->cur)
+ __cpufreq_driver_target(new_policy,
+ new_policy->min, CPUFREQ_RELATION_L);
+ break;
+ }
+ return 0;
+}
+
+static int __init cpufreq_interactivex_init(void)
+{
+ unsigned int i;
+ struct timer_list *t;
+ min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
+ resum_speed = RESUME_SPEED;
+ freq_threshld = FREQ_THRESHOLD;
+
+ /* Initalize per-cpu timers */
+ for_each_possible_cpu(i) {
+ t = &per_cpu(cpu_timer, i);
+ init_timer_deferrable(t);
+ t->function = cpufreq_interactivex_timer;
+ t->data = i;
+ }
+
+ /* Scale up is high priority */
+ up_wq = create_workqueue("kinteractive_up");
+ down_wq = create_workqueue("knteractive_down");
+
+ INIT_WORK(&freq_scale_work, cpufreq_interactivex_freq_change_time_work);
+
+ pr_info("[imoseyon] interactiveX enter\n");
+ return cpufreq_register_governor(&cpufreq_gov_interactivex);
+}
+
+#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVEX
+fs_initcall(cpufreq_interactivex_init);
+#else
+module_init(cpufreq_interactivex_init);
+#endif
+
+static void __exit cpufreq_interactivex_exit(void)
+{
+ pr_info("[imoseyon] interactiveX exit\n");
+ cpufreq_unregister_governor(&cpufreq_gov_interactivex);
+ destroy_workqueue(up_wq);
+ destroy_workqueue(down_wq);
+}
+
+module_exit(cpufreq_interactivex_exit);
+
+MODULE_AUTHOR("Mike Chan <[email protected]>");
+MODULE_DESCRIPTION("'cpufreq_interactiveX' - A cpufreq governor for "
+ "Latency sensitive workloads");
+MODULE_LICENSE("GPL");
diff -uNr Vanilla/drivers/cpufreq/cpufreq_lionheart.c New/drivers/cpufreq/cpufreq_lionheart.c
--- Vanilla/drivers/cpufreq/cpufreq_lionheart.c 1969-12-31 18:00:00.000000000 -0600
+++ New/drivers/cpufreq/cpufreq_lionheart.c 2012-06-30 19:56:47.302115034 -0500
@@ -0,0 +1,543 @@
+/*
+ * drivers/cpufreq/cpufreq_lionheart.c
+ *
+ * Patched & tweaked: knzo
+ *
+ * Based on the Conservative governor by:
+ *
+ * Copyright (C) 2001 Russell King
+ * (C) 2003 Venkatesh Pallipadi <[email protected]>.
+ * Jun Nakajima <[email protected]>
+ * (C) 2009 Alexander Clouter <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/cpufreq.h>
+#include <linux/cpu.h>
+#include <linux/jiffies.h>
+#include <linux/kernel_stat.h>
+#include <linux/mutex.h>
+#include <linux/hrtimer.h>
+#include <linux/tick.h>
+#include <linux/ktime.h>
+#include <linux/sched.h>
+
+#define DEF_FREQUENCY_UP_THRESHOLD (70)
+#define DEF_FREQUENCY_DOWN_THRESHOLD (30)
+#define MIN_SAMPLING_RATE_RATIO (2)
+
+static unsigned int min_sampling_rate;
+
+#define LATENCY_MULTIPLIER (1000)
+#define MIN_LATENCY_MULTIPLIER (100)
+#define DEF_SAMPLING_DOWN_FACTOR (1)
+#define MAX_SAMPLING_DOWN_FACTOR (10)
+#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
+
+static void do_dbs_timer(struct work_struct *work);
+
+struct cpu_dbs_info_s {
+ cputime64_t prev_cpu_idle;
+ cputime64_t prev_cpu_wall;
+ cputime64_t prev_cpu_nice;
+ struct cpufreq_policy *cur_policy;
+ struct delayed_work work;
+ unsigned int down_skip;
+ unsigned int requested_freq;
+ int cpu;
+ unsigned int enable:1;
+
+ struct mutex timer_mutex;
+};
+static DEFINE_PER_CPU(struct cpu_dbs_info_s, cs_cpu_dbs_info);
+
+static unsigned int dbs_enable;
+
+static DEFINE_MUTEX(dbs_mutex);
+
+static struct dbs_tuners {
+ unsigned int sampling_rate;
+ unsigned int sampling_down_factor;
+ unsigned int up_threshold;
+ unsigned int down_threshold;
+ unsigned int ignore_nice;
+ unsigned int freq_step;
+} dbs_tuners_ins = {
+ .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
+ .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
+ .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
+ .ignore_nice = 0,
+ .freq_step = 5,
+};
+
+static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
+ cputime64_t *wall)
+{
+ cputime64_t idle_time;
+ cputime64_t cur_wall_time;
+ cputime64_t busy_time;
+
+ cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
+ busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
+ kstat_cpu(cpu).cpustat.system);
+
+ busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
+ busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
+ busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
+ busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
+
+ idle_time = cputime64_sub(cur_wall_time, busy_time);
+ if (wall)
+ *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
+
+ return (cputime64_t)jiffies_to_usecs(idle_time);
+}
+
+static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
+{
+ u64 idle_time = get_cpu_idle_time_us(cpu, wall);
+
+ if (idle_time == -1ULL)
+ return get_cpu_idle_time_jiffy(cpu, wall);
+
+ return idle_time;
+}
+
+static int
+dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
+ void *data)
+{
+ struct cpufreq_freqs *freq = data;
+ struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cs_cpu_dbs_info,
+ freq->cpu);
+
+ struct cpufreq_policy *policy;
+
+ if (!this_dbs_info->enable)
+ return 0;
+
+ policy = this_dbs_info->cur_policy;
+
+ if (this_dbs_info->requested_freq > policy->max
+ || this_dbs_info->requested_freq < policy->min)
+ this_dbs_info->requested_freq = freq->new;
+
+ return 0;
+}
+
+static struct notifier_block dbs_cpufreq_notifier_block = {
+ .notifier_call = dbs_cpufreq_notifier
+};
+
+static ssize_t show_sampling_rate_min(struct kobject *kobj,
+ struct attribute *attr, char *buf)
+{
+ return sprintf(buf, "%u\n", min_sampling_rate);
+}
+
+define_one_global_ro(sampling_rate_min);
+
+#define show_one(file_name, object) \
+static ssize_t show_##file_name \
+(struct kobject *kobj, struct attribute *attr, char *buf) \
+{ \
+ return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
+}
+
+show_one(sampling_rate, sampling_rate);
+show_one(sampling_down_factor, sampling_down_factor);
+show_one(up_threshold, up_threshold);
+show_one(down_threshold, down_threshold);
+show_one(ignore_nice_load, ignore_nice);
+show_one(freq_step, freq_step);
+
+static ssize_t store_sampling_down_factor(struct kobject *a,
+ struct attribute *b,
+ const char *buf, size_t count)
+{
+ unsigned int input;
+ int ret;
+ ret = sscanf(buf, "%u", &input);
+
+ if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
+ return -EINVAL;
+
+ dbs_tuners_ins.sampling_down_factor = input;
+ return count;
+}
+
+static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
+ const char *buf, size_t count)
+{
+ unsigned int input;
+ int ret;
+ ret = sscanf(buf, "%u", &input);
+
+ if (ret != 1)
+ return -EINVAL;
+
+ dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
+ return count;
+}
+
+static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
+ const char *buf, size_t count)
+{
+ unsigned int input;
+ int ret;
+ ret = sscanf(buf, "%u", &input);
+
+ if (ret != 1 || input > 100 ||
+ input <= dbs_tuners_ins.down_threshold)
+ return -EINVAL;
+
+ dbs_tuners_ins.up_threshold = input;
+ return count;
+}
+
+static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
+ const char *buf, size_t count)
+{
+ unsigned int input;
+ int ret;
+ ret = sscanf(buf, "%u", &input);
+
+ if (ret != 1 || input < 11 || input > 100 ||
+ input >= dbs_tuners_ins.up_threshold)
+ return -EINVAL;
+
+ dbs_tuners_ins.down_threshold = input;
+ return count;
+}
+
+static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
+ const char *buf, size_t count)
+{
+ unsigned int input;
+ int ret;
+
+ unsigned int j;
+
+ ret = sscanf(buf, "%u", &input);
+ if (ret != 1)
+ return -EINVAL;
+
+ if (input > 1)
+ input = 1;
+
+ if (input == dbs_tuners_ins.ignore_nice)
+ return count;
+
+ dbs_tuners_ins.ignore_nice = input;
+
+ for_each_online_cpu(j) {
+ struct cpu_dbs_info_s *dbs_info;
+ dbs_info = &per_cpu(cs_cpu_dbs_info, j);
+ dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
+ &dbs_info->prev_cpu_wall);
+ if (dbs_tuners_ins.ignore_nice)
+ dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
+ }
+ return count;
+}
+
+static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
+ const char *buf, size_t count)
+{
+ unsigned int input;
+ int ret;
+ ret = sscanf(buf, "%u", &input);
+
+ if (ret != 1)
+ return -EINVAL;
+
+ if (input > 100)
+ input = 100;
+
+ dbs_tuners_ins.freq_step = input;
+ return count;
+}
+
+define_one_global_rw(sampling_rate);
+define_one_global_rw(sampling_down_factor);
+define_one_global_rw(up_threshold);
+define_one_global_rw(down_threshold);
+define_one_global_rw(ignore_nice_load);
+define_one_global_rw(freq_step);
+
+static struct attribute *dbs_attributes[] = {
+ &sampling_rate_min.attr,
+ &sampling_rate.attr,
+ &sampling_down_factor.attr,
+ &up_threshold.attr,
+ &down_threshold.attr,
+ &ignore_nice_load.attr,
+ &freq_step.attr,
+ NULL
+};
+
+static struct attribute_group dbs_attr_group = {
+ .attrs = dbs_attributes,
+ .name = "Lionheart",
+};
+
+static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
+{
+ unsigned int load = 0;
+ unsigned int max_load = 0;
+ unsigned int freq_target;
+
+ struct cpufreq_policy *policy;
+ unsigned int j;
+
+ policy = this_dbs_info->cur_policy;
+
+ for_each_cpu(j, policy->cpus) {
+ struct cpu_dbs_info_s *j_dbs_info;
+ cputime64_t cur_wall_time, cur_idle_time;
+ unsigned int idle_time, wall_time;
+
+ j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
+
+ cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
+
+ wall_time = (unsigned int) cputime64_sub(cur_wall_time,
+ j_dbs_info->prev_cpu_wall);
+ j_dbs_info->prev_cpu_wall = cur_wall_time;
+
+ idle_time = (unsigned int) cputime64_sub(cur_idle_time,
+ j_dbs_info->prev_cpu_idle);
+ j_dbs_info->prev_cpu_idle = cur_idle_time;
+
+ if (dbs_tuners_ins.ignore_nice) {
+ cputime64_t cur_nice;
+ unsigned long cur_nice_jiffies;
+
+ cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
+ j_dbs_info->prev_cpu_nice);
+
+ cur_nice_jiffies = (unsigned long)
+ cputime64_to_jiffies64(cur_nice);
+
+ j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
+ idle_time += jiffies_to_usecs(cur_nice_jiffies);
+ }
+
+ if (unlikely(!wall_time || wall_time < idle_time))
+ continue;
+
+ load = 100 * (wall_time - idle_time) / wall_time;
+
+ if (load > max_load)
+ max_load = load;
+ }
+
+ if (dbs_tuners_ins.freq_step == 0)
+ return;
+
+ if (max_load > dbs_tuners_ins.up_threshold) {
+ this_dbs_info->down_skip = 0;
+
+ if (this_dbs_info->requested_freq == policy->max)
+ return;
+
+ freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
+
+ if (unlikely(freq_target == 0))
+ freq_target = 5;
+
+ this_dbs_info->requested_freq += freq_target;
+ if (this_dbs_info->requested_freq > policy->max)
+ this_dbs_info->requested_freq = policy->max;
+
+ __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
+ CPUFREQ_RELATION_H);
+ return;
+ }
+
+ if (max_load < (dbs_tuners_ins.down_threshold - 10)) {
+ freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
+
+ this_dbs_info->requested_freq -= freq_target;
+ if (this_dbs_info->requested_freq < policy->min)
+ this_dbs_info->requested_freq = policy->min;
+
+ if (policy->cur == policy->min)
+ return;
+
+ __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
+ CPUFREQ_RELATION_H);
+ return;
+ }
+}
+
+static void do_dbs_timer(struct work_struct *work)
+{
+ struct cpu_dbs_info_s *dbs_info =
+ container_of(work, struct cpu_dbs_info_s, work.work);
+ unsigned int cpu = dbs_info->cpu;
+
+ int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
+
+ // delay -= jiffies % delay;
+
+ mutex_lock(&dbs_info->timer_mutex);
+
+ dbs_check_cpu(dbs_info);
+
+ schedule_delayed_work_on(cpu, &dbs_info->work, delay);
+ mutex_unlock(&dbs_info->timer_mutex);
+}
+
+static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
+{
+ int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
+ // delay -= jiffies % delay;
+
+ dbs_info->enable = 1;
+ INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
+ schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
+}
+
+static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
+{
+ dbs_info->enable = 0;
+ cancel_delayed_work_sync(&dbs_info->work);
+}
+
+static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
+ unsigned int event)
+{
+ unsigned int cpu = policy->cpu;
+ struct cpu_dbs_info_s *this_dbs_info;
+ unsigned int j;
+ int rc;
+
+ this_dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
+
+ switch (event) {
+ case CPUFREQ_GOV_START:
+ if ((!cpu_online(cpu)) || (!policy->cur))
+ return -EINVAL;
+
+ mutex_lock(&dbs_mutex);
+
+ for_each_cpu(j, policy->cpus) {
+ struct cpu_dbs_info_s *j_dbs_info;
+ j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
+ j_dbs_info->cur_policy = policy;
+
+ j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
+ &j_dbs_info->prev_cpu_wall);
+ if (dbs_tuners_ins.ignore_nice) {
+ j_dbs_info->prev_cpu_nice =
+ kstat_cpu(j).cpustat.nice;
+ }
+ }
+ this_dbs_info->down_skip = 0;
+ this_dbs_info->requested_freq = policy->cur;
+
+ mutex_init(&this_dbs_info->timer_mutex);
+ dbs_enable++;
+
+ if (dbs_enable == 1) {
+ unsigned int latency;
+
+ latency = policy->cpuinfo.transition_latency / 1000;
+ if (latency == 0)
+ latency = 1;
+
+ rc = sysfs_create_group(cpufreq_global_kobject,
+ &dbs_attr_group);
+ if (rc) {
+ mutex_unlock(&dbs_mutex);
+ return rc;
+ }
+
+ min_sampling_rate = 10000;
+ dbs_tuners_ins.sampling_rate = 10000;
+
+ cpufreq_register_notifier(
+ &dbs_cpufreq_notifier_block,
+ CPUFREQ_TRANSITION_NOTIFIER);
+ }
+ mutex_unlock(&dbs_mutex);
+
+ dbs_timer_init(this_dbs_info);
+
+ break;
+
+ case CPUFREQ_GOV_STOP:
+ dbs_timer_exit(this_dbs_info);
+
+ mutex_lock(&dbs_mutex);
+ dbs_enable--;
+ mutex_destroy(&this_dbs_info->timer_mutex);
+
+ if (dbs_enable == 0)
+ cpufreq_unregister_notifier(
+ &dbs_cpufreq_notifier_block,
+ CPUFREQ_TRANSITION_NOTIFIER);
+
+ mutex_unlock(&dbs_mutex);
+ if (!dbs_enable)
+ sysfs_remove_group(cpufreq_global_kobject,
+ &dbs_attr_group);
+
+ break;
+
+ case CPUFREQ_GOV_LIMITS:
+ mutex_lock(&this_dbs_info->timer_mutex);
+ if (policy->max < this_dbs_info->cur_policy->cur)
+ __cpufreq_driver_target(
+ this_dbs_info->cur_policy,
+ policy->max, CPUFREQ_RELATION_H);
+ else if (policy->min > this_dbs_info->cur_policy->cur)
+ __cpufreq_driver_target(
+ this_dbs_info->cur_policy,
+ policy->min, CPUFREQ_RELATION_L);
+ mutex_unlock(&this_dbs_info->timer_mutex);
+
+ break;
+ }
+ return 0;
+}
+
+#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_LIONHEART
+static
+#endif
+struct cpufreq_governor cpufreq_gov_lionheart = {
+ .name = "Lionheart",
+ .governor = cpufreq_governor_dbs,
+ .max_transition_latency = TRANSITION_LATENCY_LIMIT,
+ .owner = THIS_MODULE,
+};
+
+static int __init cpufreq_gov_dbs_init(void)
+{
+ return cpufreq_register_governor(&cpufreq_gov_lionheart);
+}
+
+static void __exit cpufreq_gov_dbs_exit(void)
+{
+ cpufreq_unregister_governor(&cpufreq_gov_lionheart);
+}
+
+MODULE_AUTHOR("knzo");
+MODULE_DESCRIPTION("'cpufreq_lionheart' - A brave and agile conservative-based governor.");
+MODULE_LICENSE("GPL");
+
+fs_initcall(cpufreq_gov_dbs_init);
+module_exit(cpufreq_gov_dbs_exit);
diff -uNr Vanilla/drivers/cpufreq/cpufreq_ondemandx.c New/drivers/cpufreq/cpufreq_ondemandx.c
--- Vanilla/drivers/cpufreq/cpufreq_ondemandx.c 1969-12-31 18:00:00.000000000 -0600
+++ New/drivers/cpufreq/cpufreq_ondemandx.c 2012-06-30 20:32:38.418143218 -0500
@@ -0,0 +1,862 @@
+/*
+* drivers/cpufreq/cpufreq_ondemandx.c
+*
+* Copyright (C) 2001 Russell King
+* (C) 2003 Venkatesh Pallipadi <[email protected]>.
+* Jun Nakajima <[email protected]>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation.
+*/
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/cpufreq.h>
+#include <linux/cpu.h>
+#include <linux/jiffies.h>
+#include <linux/kernel_stat.h>
+#include <linux/mutex.h>
+#include <linux/hrtimer.h>
+#include <linux/tick.h>
+#include <linux/ktime.h>
+#include <linux/sched.h>
+#include <linux/earlysuspend.h>
+
+/*
+* dbs is used in this file as a shortform for demandbased switching
+* It helps to keep variable names smaller, simpler
+*/
+
+#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (15)
+#define DEF_FREQUENCY_UP_THRESHOLD (85)
+#define DEF_SAMPLING_DOWN_FACTOR (50)
+#define MAX_SAMPLING_DOWN_FACTOR (100000)
+#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
+#define MICRO_FREQUENCY_UP_THRESHOLD (95)
+#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
+#define MIN_FREQUENCY_UP_THRESHOLD (11)
+#define MAX_FREQUENCY_UP_THRESHOLD (100)
+#define DEF_SUSPEND_FREQ (384000)