summaryrefstree
path: root/kernel/drivers/cpufreq/cpufreq_limit.c
blob: ff444cfe9cac5b8a1955d89b21aead1d4750afb8 (plain)
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
/*
 * drivers/cpufreq/cpufreq_limit.c
 *
 * Remade according to cpufreq change
 * (refer to commit df0eea4488081e0698b0b58ccd1e8c8823e22841
 *                 18c49926c4bf4915e5194d1de3299c0537229f9f)
 *
 * 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/module.h>
#include <linux/sysfs.h>
#include <linux/cpufreq.h>
#include <linux/cpufreq_limit.h>
#include <linux/err.h>
#include <linux/suspend.h>
#include <linux/cpu.h>
#include <linux/kobject.h>
#include <linux/timer.h>

#define MAX_BUF_SIZE	1024
#define MIN(a, b)     (((a) < (b)) ? (a) : (b))
#define MAX(a, b)     (((a) > (b)) ? (a) : (b))

static DEFINE_MUTEX(cflm_mutex);
#define LIMIT_RELEASE	-1

#define NUM_CPUS	8
struct freq_qos_request max_req[NUM_CPUS][CFLM_MAX_ITEM];
struct freq_qos_request min_req[NUM_CPUS][CFLM_MAX_ITEM];

/* input info: freq, time(TBD) */
struct input_info {
	int min;
	int max;
	u64 time_in_min_limit;
	u64 time_in_max_limit;
	u64 time_in_over_limit;
	ktime_t last_min_limit_time;
	ktime_t last_max_limit_time;
	ktime_t last_over_limit_time;
};
struct input_info freq_input[CFLM_MAX_ITEM];

struct cflm_parameter {
	/* to make virtual freq table */
	struct cpufreq_frequency_table *cpuftbl_L;
	struct cpufreq_frequency_table *cpuftbl_b;
	unsigned int	unified_cpuftbl[50];
	unsigned int	freq_count;
	bool		table_initialized;

	/* cpu info: silver/gold/prime */
	unsigned int	s_first;
	unsigned int	s_fmin;
	unsigned int	s_fmax;
	unsigned int	g_first;
	unsigned int	g_fmin;
	unsigned int	g_fmax;
	unsigned int	p_first;
	unsigned int	p_fmin;
	unsigned int	p_fmax;

	/* in virtual table little(silver)/big(gold & prime) */
	unsigned int	big_min_freq;
	unsigned int	big_max_freq;
	unsigned int	ltl_min_freq;
	unsigned int	ltl_max_freq;

	/* pre-defined value */
	unsigned int	silver_min_lock;
	unsigned int	silver_divider;

	/* current freq in virtual table */
	unsigned int	min_limit_val;
	unsigned int	max_limit_val;

	/* sched boost type */
	int		sched_boost_type;
	bool		sched_boost_cond;
	bool		sched_boost_enabled;

	/* over limit */
	unsigned int	over_limit;
};


/* TODO: move to dtsi? */
struct cflm_parameter param = {
	.freq_count		= 0,
	.table_initialized	= false,

	.s_first		= 0,
	.g_first		= 4,
	.p_first		= 7,

	.ltl_min_freq		= 0,		/* will be auto updated */
	.ltl_max_freq		= 0,		/* will be auto updated */
	.big_min_freq		= 0,		/* will be auto updated */
	.big_max_freq		= 0,		/* will be auto updated */

	.silver_min_lock	= 1209600,	/* real silver clock */
	.silver_divider		= 2,

	.min_limit_val		= -1,
	.max_limit_val		= -1,

	.sched_boost_type	= CONSERVATIVE_BOOST,
	.sched_boost_cond	= false,
	.sched_boost_enabled	= false,

	.over_limit		= 0,
};

bool cflm_make_table(void)
{
	int i, count = 0;
	int freq_count = 0;
	unsigned int freq;
	bool ret = false;

	/* big cluster table */
	if (!param.cpuftbl_b)
		goto little;

	for (i = 0; param.cpuftbl_b[i].frequency != CPUFREQ_TABLE_END; i++)
		count = i;

	for (i = count; i >= 0; i--) {
		freq = param.cpuftbl_b[i].frequency;

		if (freq == CPUFREQ_ENTRY_INVALID)
			continue;

		if (freq < param.big_min_freq ||
				freq > param.big_max_freq)
			continue;

		param.unified_cpuftbl[freq_count++] = freq;
	}

little:
	/* LITTLE cluster table */
	if (!param.cpuftbl_L)
		goto done;

	for (i = 0; param.cpuftbl_L[i].frequency != CPUFREQ_TABLE_END; i++)
		count = i;

	for (i = count; i >= 0; i--) {
		freq = param.cpuftbl_L[i].frequency / param.silver_divider;

		if (freq == CPUFREQ_ENTRY_INVALID)
			continue;

		if (freq < param.ltl_min_freq ||
				freq > param.ltl_max_freq)
			continue;

		param.unified_cpuftbl[freq_count++] = freq;
	}

done:
	if (freq_count) {
		pr_debug("%s: unified table is made\n", __func__);
		param.freq_count = freq_count;
		ret = true;
	} else {
		pr_err("%s: cannot make unified table\n", __func__);
	}

	return ret;
}

/**
 * cflm_set_table - cpufreq table from dt via qcom-cpufreq
 */
void cflm_set_table(int cpu, struct cpufreq_frequency_table *ftbl)
{
	int i, count = 0;
	unsigned int max_freq_b = 0, min_freq_b = UINT_MAX;
	unsigned int max_freq_l = 0, min_freq_l = UINT_MAX;

	if (param.table_initialized)
		return;

	if (cpu == param.s_first)
		param.cpuftbl_L = ftbl;
	else if (cpu == param.p_first)
		param.cpuftbl_b = ftbl;

	if (!param.cpuftbl_L)
		return;

	if (!param.cpuftbl_b)
		return;

	pr_info("%s: freq table is ready, update config\n", __func__);

	/* update little config */
	for (i = 0; param.cpuftbl_L[i].frequency != CPUFREQ_TABLE_END; i++)
		count = i;

	for (i = count; i >= 0; i--) {
		if (param.cpuftbl_L[i].frequency == CPUFREQ_ENTRY_INVALID)
			continue;

		if (param.cpuftbl_L[i].frequency < min_freq_l)
			min_freq_l = param.cpuftbl_L[i].frequency;

		if (param.cpuftbl_L[i].frequency > max_freq_l)
			max_freq_l = param.cpuftbl_L[i].frequency;
	}

	if (!param.ltl_min_freq)
		param.ltl_min_freq = min_freq_l / param.silver_divider;
	if (!param.ltl_max_freq)
		param.ltl_max_freq = max_freq_l / param.silver_divider;

	/* update big config */
	for (i = 0; param.cpuftbl_b[i].frequency != CPUFREQ_TABLE_END; i++)
		count = i;

	for (i = count; i >= 0; i--) {
		if (param.cpuftbl_b[i].frequency == CPUFREQ_ENTRY_INVALID)
			continue;

		if ((param.cpuftbl_b[i].frequency < min_freq_b) &&
			(param.cpuftbl_b[i].frequency > param.ltl_max_freq))
			min_freq_b = param.cpuftbl_b[i].frequency;

		if (param.cpuftbl_b[i].frequency > max_freq_b)
			max_freq_b = param.cpuftbl_b[i].frequency;
	}

	if (!param.big_min_freq)
		param.big_min_freq = min_freq_b;
	if (!param.big_max_freq)
		param.big_max_freq = max_freq_b;

	pr_info("%s: updated: little(%u-%u), big(%u-%u)\n", __func__,
			param.ltl_min_freq, param.ltl_max_freq,
			param.big_min_freq, param.big_max_freq);

	param.table_initialized = cflm_make_table();
}

/**
 * cflm_get_table - fill the cpufreq table to support HMP
 * @buf		a buf that has been requested to fill the cpufreq table
 */
ssize_t cflm_get_table(char *buf)
{
	ssize_t len = 0;
	int i = 0;

	if (!param.freq_count)
		return len;

	for (i = 0; i < param.freq_count; i++)
		len += snprintf(buf + len, MAX_BUF_SIZE, "%u ",
					param.unified_cpuftbl[i]);

	len--;
	len += snprintf(buf + len, MAX_BUF_SIZE, "\n");

	pr_info("%s: %s\n", __func__, buf);

	return len;
}

static void cflm_update_boost(void)
{
	/* sched boost */
	if (param.sched_boost_cond) {
		if (!param.sched_boost_enabled) {
			pr_info("%s: sched boost on\n", __func__);

			sched_set_boost(param.sched_boost_type);
			param.sched_boost_enabled = true;
		} else {
			pr_debug("%s: sched boost already on, do nothing\n", __func__);
		}
	} else {
		if (param.sched_boost_enabled) {
			pr_info("%s: sched boost off\n", __func__);
			sched_set_boost(NO_BOOST);
			param.sched_boost_enabled = false;
		} else {
			pr_debug("%s: sched boost already off, do nothing\n", __func__);
		}
	}
}

static void cflm_update_current_freq(void)
{
	struct cpufreq_policy *policy;
	int s_min = 0, s_max = 0;
	int g_min = 0, g_max = 0;
	int p_min = 0, p_max = 0;

	policy = cpufreq_cpu_get(param.s_first);
	if (policy) {
		s_min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
		s_max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
		cpufreq_cpu_put(policy);
	}

	policy = cpufreq_cpu_get(param.g_first);
	if (policy) {
		g_min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
		g_max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
		cpufreq_cpu_put(policy);
	}

	policy = cpufreq_cpu_get(param.p_first);
	if (policy) {
		p_min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
		p_max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
		cpufreq_cpu_put(policy);
	}

	pr_info("%s: current freq: s(%d ~ %d), g(%d ~ %d), p(%d ~ %d)\n",
			__func__, s_min, s_max, g_min, g_max, p_min, p_max);

	/* sched boost condition */
	if ((param.sched_boost_type) && (p_min > param.p_fmin))
		param.sched_boost_cond = true;
	else
		param.sched_boost_cond = false;
}

static bool cflm_max_lock_need_restore(void)
{
	if ((int)param.over_limit <= 0)
		return false;

	if (freq_input[CFLM_USERSPACE].min > 0) {
		if (freq_input[CFLM_USERSPACE].min > (int)param.ltl_max_freq) {
			pr_info("%s: userspace minlock (%d) > ltl max (%d)\n",
					__func__, freq_input[CFLM_USERSPACE], param.ltl_max_freq);
			return false;
		}
	}

	if (freq_input[CFLM_TOUCH].min > 0) {
		if (freq_input[CFLM_TOUCH].min > (int)param.ltl_max_freq) {
			pr_info("%s: touch minlock (%d) > ltl max (%d)\n",
					__func__, freq_input[CFLM_TOUCH], param.ltl_max_freq);
			return false;
		}
	}

	return true;
}

static bool cflm_high_pri_min_lock_required(void)
{
	if ((int)param.over_limit <= 0)
		return false;

	if (freq_input[CFLM_USERSPACE].min > 0) {
		if (freq_input[CFLM_USERSPACE].min > (int)param.ltl_max_freq) {
			pr_info("%s: userspace minlock (%d) > ltl max (%d)\n",
					__func__, freq_input[CFLM_USERSPACE], param.ltl_max_freq);
			return true;
		}
	}

	if (freq_input[CFLM_TOUCH].min > 0) {
		if (freq_input[CFLM_TOUCH].min > (int)param.ltl_max_freq) {
			pr_info("%s: touch minlock (%d) > ltl max (%d)\n",
					__func__, freq_input[CFLM_TOUCH], param.ltl_max_freq);
			return true;
		}
	}

	return false;
}

static void cflm_freq_decision(int type, int new_min, int new_max)
{
	int cpu = 0;
	int s_min = param.s_fmin;
	int s_max = param.s_fmax;
	int g_min = param.g_fmin;
	int g_max = param.g_fmax;
	int p_min = param.p_fmin;
	int p_max = param.p_fmax;

	bool need_update_user_max = false;
	int new_user_max = FREQ_QOS_MAX_DEFAULT_VALUE;

	pr_info("%s: input: type(%d), min(%d), max(%d)\n",
			__func__, type, new_min, new_max);

	/* update input freq */
	if (new_min != 0) {
		freq_input[type].min = new_min;
		if ((new_min == LIMIT_RELEASE || new_min == param.ltl_min_freq) &&
			freq_input[type].last_min_limit_time != 0) {
			freq_input[type].time_in_min_limit += ktime_to_ms(ktime_get()-
				freq_input[type].last_min_limit_time);
			freq_input[type].last_min_limit_time = 0;
		}
		if (new_min != LIMIT_RELEASE && new_min != param.ltl_min_freq &&
			freq_input[type].last_min_limit_time == 0) {
			freq_input[type].last_min_limit_time = ktime_get();
		}
	}

	if (new_max != 0) {
		freq_input[type].max = new_max;
		if ((new_max == LIMIT_RELEASE || new_max == param.big_max_freq) &&
			freq_input[type].last_max_limit_time != 0) {
			freq_input[type].time_in_max_limit += ktime_to_ms(ktime_get() -
				freq_input[type].last_max_limit_time);
			freq_input[type].last_max_limit_time = 0;
		}
		if (new_max != LIMIT_RELEASE && new_max != param.big_max_freq &&
			freq_input[type].last_max_limit_time == 0) {
			freq_input[type].last_max_limit_time = ktime_get();
		}
	}

	if (new_min > 0) {
		if (new_min < param.ltl_min_freq) {
			pr_info("%s: too low freq(%d), set to %d\n",
				__func__, new_min, param.ltl_min_freq);
			new_min = param.ltl_min_freq;
		}

		pr_info("%s: new_min=%d, ltl_max=%d, over_limit=%d\n", __func__,
				new_min, param.ltl_max_freq, param.over_limit);
		if ((type == CFLM_USERSPACE || type == CFLM_TOUCH) &&
			cflm_high_pri_min_lock_required()) {
			if (freq_input[CFLM_USERSPACE].max > 0) {
				need_update_user_max = true;
				new_user_max = MAX((int)param.over_limit, freq_input[CFLM_USERSPACE].max);
				pr_info("%s: override new_max %d => %d,  userspace_min=%d, touch_min=%d, ltl_max=%d\n",
						__func__, freq_input[CFLM_USERSPACE].max, new_user_max, freq_input[CFLM_USERSPACE].min,
						freq_input[CFLM_TOUCH].min, param.ltl_max_freq);
			}
		}

		/* boost @gold/prime */
		if (new_min > param.ltl_max_freq) {
			s_min = param.silver_min_lock;
			g_min = MIN(new_min, param.g_fmax);
			p_min = MIN(new_min, param.p_fmax);
		} else {
			s_min = new_min * param.silver_divider;
			g_min = param.g_fmin;
			p_min = param.p_fmin;
		}

		freq_qos_update_request(&min_req[param.s_first][type], s_min);
		freq_qos_update_request(&min_req[param.g_first][type], g_min);
		freq_qos_update_request(&min_req[param.p_first][type], p_min);
	} else if (new_min == LIMIT_RELEASE) {
		for_each_possible_cpu(cpu) {
			freq_qos_update_request(&min_req[cpu][type],
						FREQ_QOS_MIN_DEFAULT_VALUE);
		}

		if ((type == CFLM_USERSPACE || type == CFLM_TOUCH) &&
			cflm_max_lock_need_restore()) { // if there is no high priority min lock and over limit is set
			if (freq_input[CFLM_USERSPACE].max > 0) {
				need_update_user_max = true;
				new_user_max = freq_input[CFLM_USERSPACE].max;
				pr_info("%s: restore new_max => %d\n",
						__func__, new_user_max);
			}
		}
	}

	if (new_max > 0) {
		if (new_max > param.big_max_freq) {
			pr_info("%s: too high freq(%d), set to %d\n",
				__func__, new_max, param.big_max_freq);
			new_max = param.big_max_freq;
		}

		if ((type == CFLM_USERSPACE) && // if userspace maxlock is being set
			cflm_high_pri_min_lock_required()) {
			need_update_user_max = true;
			new_user_max = MAX((int)param.over_limit, freq_input[CFLM_USERSPACE].max);
			pr_info("%s: force up new_max %d => %d, userspace_min=%d, touch_min=%d, ltl_max=%d\n",
					__func__, new_max, new_user_max, freq_input[CFLM_USERSPACE].min,
					freq_input[CFLM_TOUCH].min, param.ltl_max_freq);
		}

		if (new_max < param.big_min_freq)
			s_max = new_max * param.silver_divider;

		g_max = MIN(new_max, param.g_fmax);
		p_max = MIN(new_max, param.p_fmax);

		freq_qos_update_request(&max_req[param.s_first][type], s_max);
		freq_qos_update_request(&max_req[param.g_first][type], g_max);
		freq_qos_update_request(&max_req[param.p_first][type], p_max);
	} else if (new_max == LIMIT_RELEASE) {
		for_each_possible_cpu(cpu)
			freq_qos_update_request(&max_req[cpu][type],
						FREQ_QOS_MAX_DEFAULT_VALUE);
	}

	if ((freq_input[type].min <= (int)param.ltl_max_freq || new_user_max != (int)param.over_limit) &&
		freq_input[type].last_over_limit_time != 0) {
		freq_input[type].time_in_over_limit += ktime_to_ms(ktime_get() -
			freq_input[type].last_over_limit_time);
		freq_input[type].last_over_limit_time = 0;
	}
	if (freq_input[type].min > (int)param.ltl_max_freq && new_user_max == (int)param.over_limit &&
		freq_input[type].last_over_limit_time == 0) {
		freq_input[type].last_over_limit_time = ktime_get();
	}

	if (need_update_user_max) {
		pr_info("%s: update_user_max is true\n", __func__);
		if (new_user_max > param.big_max_freq) {
			pr_info("%s: too high freq(%d), set to %d\n",
			__func__, new_user_max, param.big_max_freq);
			new_user_max = param.big_max_freq;
		}

		if (new_user_max < param.big_min_freq)
			s_max = new_user_max * param.silver_divider;

		g_max = MIN(new_user_max, param.g_fmax);
		p_max = MIN(new_user_max, param.p_fmax);

		pr_info("%s: freq_update_request : new userspace max %d %d %d\n", __func__, s_max, g_max, p_max);
		freq_qos_update_request(&max_req[param.s_first][CFLM_USERSPACE], s_max);
		freq_qos_update_request(&max_req[param.g_first][CFLM_USERSPACE], g_max);
		freq_qos_update_request(&max_req[param.p_first][CFLM_USERSPACE], p_max);
	}

	cflm_update_current_freq();

	cflm_update_boost();
}

static ssize_t cpufreq_table_show(struct kobject *kobj,
			struct kobj_attribute *attr, char *buf)
{
	ssize_t len = 0;

	len = cflm_get_table(buf);

	return len;
}

static ssize_t cpufreq_max_limit_show(struct kobject *kobj,
					struct kobj_attribute *attr,
					char *buf)
{
	return snprintf(buf, MAX_BUF_SIZE, "%d\n", param.max_limit_val);
}

static ssize_t cpufreq_max_limit_store(struct kobject *kobj,
					struct kobj_attribute *attr,
					const char *buf, size_t n)
{
	int freq;
	int ret = -EINVAL;

	ret = kstrtoint(buf, 10, &freq);
	if (ret < 0) {
		pr_err("%s: cflm: Invalid cpufreq format\n", __func__);
		goto out;
	}

	mutex_lock(&cflm_mutex);

	param.max_limit_val = freq;
	cflm_freq_decision(CFLM_USERSPACE, 0, freq);

	mutex_unlock(&cflm_mutex);
	ret = n;

out:
	return ret;
}

static ssize_t cpufreq_min_limit_show(struct kobject *kobj,
					struct kobj_attribute *attr,
					char *buf)
{
	return snprintf(buf, MAX_BUF_SIZE, "%d\n", param.min_limit_val);
}

static ssize_t cpufreq_min_limit_store(struct kobject *kobj,
					struct kobj_attribute *attr,
					const char *buf, size_t n)
{
	int freq;
	int ret = -EINVAL;

	ret = kstrtoint(buf, 10, &freq);
	if (ret < 0) {
		pr_err("%s: cflm: Invalid cpufreq format\n", __func__);
		goto out;
	}

	mutex_lock(&cflm_mutex);

	cflm_freq_decision(CFLM_USERSPACE, freq, 0);
	param.min_limit_val = freq;

	mutex_unlock(&cflm_mutex);
	ret = n;
out:
	return ret;
}

static ssize_t over_limit_show(struct kobject *kobj,
					struct kobj_attribute *attr,
					char *buf)
{
	return snprintf(buf, MAX_BUF_SIZE, "%d\n", param.over_limit);
}

static ssize_t over_limit_store(struct kobject *kobj,
					struct kobj_attribute *attr,
					const char *buf, size_t n)
{
	int freq;
	int ret = -EINVAL;

	ret = kstrtoint(buf, 10, &freq);
	if (ret < 0) {
		pr_err("%s: cflm: Invalid cpufreq format\n", __func__);
		goto out;
	}

	mutex_lock(&cflm_mutex);

	if (param.over_limit != freq) {
		param.over_limit = freq;
		if ((int)param.max_limit_val > 0)
			cflm_freq_decision(CFLM_USERSPACE, 0, param.max_limit_val);
	}

	mutex_unlock(&cflm_mutex);
	ret = n;
out:
	return ret;
}

static ssize_t limit_stat_show(struct kobject *kobj,
					struct kobj_attribute *attr,
					char *buf)
{

	ssize_t len = 0;
	int i, j = 0;

	mutex_lock(&cflm_mutex);
	for (i = 0; i < CFLM_MAX_ITEM; i++) {
		if (freq_input[i].last_min_limit_time != 0) {
			freq_input[i].time_in_min_limit += ktime_to_ms(ktime_get() -
				freq_input[i].last_min_limit_time);
			freq_input[i].last_min_limit_time = ktime_get();
		}

		if (freq_input[i].last_max_limit_time != 0) {
			freq_input[i].time_in_max_limit += ktime_to_ms(ktime_get() -
				freq_input[i].last_max_limit_time);
			freq_input[i].last_max_limit_time = ktime_get();
		}

		if (freq_input[i].last_over_limit_time != 0) {
			freq_input[i].time_in_over_limit += ktime_to_ms(ktime_get() -
				freq_input[i].last_over_limit_time);
			freq_input[i].last_over_limit_time = ktime_get();
		}
	}

	for (j = 0; j < CFLM_MAX_ITEM; j++) {
		len += snprintf(buf + len, MAX_BUF_SIZE - len, "%llu %llu %llu\n",
				freq_input[j].time_in_min_limit, freq_input[j].time_in_max_limit,
				freq_input[j].time_in_over_limit);
	}

	mutex_unlock(&cflm_mutex);
	return len;
}

/* sysfs in /sys/power */
static struct kobj_attribute cpufreq_table = {
	.attr	= {
		.name = "cpufreq_table",
		.mode = 0444
	},
	.show	= cpufreq_table_show,
	.store	= NULL,
};

static struct kobj_attribute cpufreq_min_limit = {
	.attr	= {
		.name = "cpufreq_min_limit",
		.mode = 0644
	},
	.show	= cpufreq_min_limit_show,
	.store	= cpufreq_min_limit_store,
};

static struct kobj_attribute cpufreq_max_limit = {
	.attr	= {
		.name = "cpufreq_max_limit",
		.mode = 0644
	},
	.show	= cpufreq_max_limit_show,
	.store	= cpufreq_max_limit_store,
};

static struct kobj_attribute over_limit = {
	.attr	= {
		.name = "over_limit",
		.mode = 0644
	},
	.show	= over_limit_show,
	.store	= over_limit_store,
};

static struct kobj_attribute limit_stat = {
	.attr	= {
		.name = "limit_stat",
		.mode = 0644
	},
	.show	= limit_stat_show,
};

int set_freq_limit(unsigned int id, unsigned int freq)
{
	mutex_lock(&cflm_mutex);

	pr_info("%s: cflm: id(%d) freq(%d)\n", __func__, (int)id, freq);

	cflm_freq_decision(id, freq, 0);

	mutex_unlock(&cflm_mutex);

	return 0;
}
EXPORT_SYMBOL(set_freq_limit);

#define cflm_attr_rw(_name)		\
static struct kobj_attribute _name##_attr =	\
__ATTR(_name, 0644, show_##_name, store_##_name)

#define show_one(file_name)			\
static ssize_t show_##file_name			\
(struct kobject *kobj, struct kobj_attribute *attr, char *buf)	\
{								\
	return scnprintf(buf, PAGE_SIZE, "%u\n", param.file_name);	\
}

#define store_one(file_name)					\
static ssize_t store_##file_name				\
(struct kobject *kobj, struct kobj_attribute *attr,		\
const char *buf, size_t count)					\
{								\
	int ret;						\
								\
	ret = kstrtoint(buf, 0, &param.file_name);		\
	if (ret != 1)						\
		return -EINVAL;					\
								\
	return count;						\
}

show_one(silver_min_lock);
show_one(sched_boost_type);
store_one(silver_min_lock);
store_one(sched_boost_type);
cflm_attr_rw(silver_min_lock);
cflm_attr_rw(sched_boost_type);

static ssize_t show_cflm_info(struct kobject *kobj,
		struct kobj_attribute *attr, char *buf)
{
	ssize_t len = 0;
	int i = 0;

	mutex_lock(&cflm_mutex);

	len += snprintf(buf, MAX_BUF_SIZE,
			"real: silver(%d ~ %d), gold(%d ~ %d), prime(%d ~ %d)\n",
			param.s_fmin, param.s_fmax,
			param.g_fmin, param.g_fmax,
			param.p_fmin, param.p_fmax);

	len += snprintf(buf + len, MAX_BUF_SIZE - len,
			"virt: little(%d ~ %d), big(%d ~ %d)\n",
			param.ltl_min_freq, param.ltl_max_freq,
			param.big_min_freq, param.big_max_freq);

	len += snprintf(buf + len, MAX_BUF_SIZE - len,
			"param: silver boost(%d), div(%d), sched boost(%d)\n",
			param.silver_min_lock, param.silver_divider,
					param.sched_boost_type);

	for (i = 0; i < CFLM_MAX_ITEM; i++) {
		len += snprintf(buf + len, MAX_BUF_SIZE - len,
				"requested: [%d] min(%d), max(%d)\n",
				i, freq_input[i].min, freq_input[i].max);
	}

	mutex_unlock(&cflm_mutex);

	return len;
}

static struct kobj_attribute cflm_info =
	__ATTR(info, 0444, show_cflm_info, NULL);

static struct attribute *cflm_attributes[] = {
	&cflm_info.attr,
	&silver_min_lock_attr.attr,
	&sched_boost_type_attr.attr,
	NULL,
};

static struct attribute_group cflm_attr_group = {
	.attrs = cflm_attributes,
};

struct kobject *cflm_kobj;
static int __init cflm_init(void)
{
	struct cpufreq_policy *policy;
	unsigned int i = 0;
	unsigned int j = 0;
	int ret;

	pr_info("%s\n", __func__);

	for_each_possible_cpu(i) {
		policy = cpufreq_cpu_get(i);
		if (!policy) {
			pr_err("no policy for cpu%d\n", i);
			ret = -ENODEV;
			goto probe_failed;
		}

		for (j = 0; j < CFLM_MAX_ITEM; j++) {
			ret = freq_qos_add_request(&policy->constraints,
							&min_req[i][j],
				FREQ_QOS_MIN, policy->cpuinfo.min_freq);
			if (ret < 0) {
				pr_err("%s: Failed to add min freq constraint (%d)\n",
					__func__, ret);
				return ret;
			}
			ret = freq_qos_add_request(&policy->constraints,
							&max_req[i][j],
				FREQ_QOS_MAX, policy->cpuinfo.max_freq);
			if (ret < 0) {
				pr_err("%s: Failed to add max freq constraint (%d)\n",
					__func__, ret);
				return ret;
			}
		}

		if (i == param.s_first) {
			param.s_fmin = policy->min;
			param.s_fmax = policy->max;
		}
		if (i == param.g_first) {
			param.g_fmin = policy->min;
			param.g_fmax = policy->max;
		}
		if (i == param.p_first) {
			param.p_fmin = policy->min;
			param.p_fmax = policy->max;
		}

		cpufreq_cpu_put(policy);
	}

	/* /sys/power/ */
	ret = sysfs_create_file(power_kobj, &cpufreq_table.attr);
	if (ret) {
		pr_err("Unable to create cpufreq_table\n");
		goto probe_failed;
	}

	ret = sysfs_create_file(power_kobj, &cpufreq_min_limit.attr);
	if (ret) {
		pr_err("Unable to create cpufreq_min_limit\n");
		goto probe_failed;
	}

	ret = sysfs_create_file(power_kobj, &cpufreq_max_limit.attr);
	if (ret) {
		pr_err("Unable to create cpufreq_max_limit\n");
		goto probe_failed;
	}

	ret = sysfs_create_file(power_kobj, &over_limit.attr);
	if (ret) {
		pr_err("Unable to create over_limit\n");
		goto probe_failed;
	}

	ret = sysfs_create_file(power_kobj, &limit_stat.attr);
	if (ret) {
		pr_err("Unable to create limit_stat\n");
		goto probe_failed;
	}

	cflm_kobj = kobject_create_and_add("cflm",
					&cpu_subsys.dev_root->kobj);
	if (!cflm_kobj) {
		pr_err("Unable to cread cflm_kobj\n");
		goto probe_failed;
	}

	ret = sysfs_create_group(cflm_kobj, &cflm_attr_group);
	if (ret) {
		pr_err("Unable to create cflm group\n");
		goto probe_failed;
	}

	pr_info("%s done\n", __func__);

probe_failed:
	return ret;
}

static void __exit cflm_exit(void)
{
	pr_info("%s\n", __func__);
}

MODULE_AUTHOR("Sangyoung Son <hello.son@samsung.com");
MODULE_DESCRIPTION("'cpufreq_limit' - A driver to limit cpu frequency");
MODULE_LICENSE("GPL");

late_initcall(cflm_init);
module_exit(cflm_exit);