summaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/pm/usecase_gov.c
blob: 5c7fe403c2f0b664facb742d97af533bf8470510 (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
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
/*
 * Copyright (C) ST-Ericsson SA 2011
 *
 * Author: Alexandre Torgue <alexandre.torgue@stericsson.com> for ST-Ericsson
 * Author: Vincent Guittot <vincent.guittot@stericsson.com> for ST-Ericsson
 * License terms: GNU General Public License (GPL) version 2
 */

#include <linux/io.h>
#include <linux/earlysuspend.h>
#include <linux/cpu.h>
#include <linux/sched.h>
#include <linux/tick.h>
#include <linux/workqueue.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/kernel_stat.h>
#include <linux/ktime.h>
#include <linux/cpufreq.h>
#include <linux/mfd/dbx500-prcmu.h>
#include <linux/cpufreq-dbx500.h>

#include "../../../../drivers/cpuidle/cpuidle-dbx500.h"


#define CPULOAD_MEAS_DELAY	3000 /* 3 secondes of delta */

/* debug */
static unsigned long debug;

#define hp_printk \
	if (debug) \
		printk \

enum ux500_uc {
	UX500_UC_NORMAL	=  0,
	UX500_UC_AUTO, /* Add use case below this. */
	UX500_UC_VC,
	UX500_UC_LPA,
	UX500_UC_USER, /* Add use case above this. */
	UX500_UC_MAX,
};

/* cpu load monitor struct */
#define LOAD_MONITOR 4
struct hotplug_cpu_info {
	cputime64_t prev_cpu_wall;
	cputime64_t prev_cpu_idle;
	cputime64_t prev_cpu_io;
	unsigned int load[LOAD_MONITOR];
	unsigned int io[LOAD_MONITOR];
	unsigned int idx;
};

static DEFINE_PER_CPU(struct hotplug_cpu_info, hotplug_info);

/* Auto trigger criteria */
/* loadavg threshold */
static unsigned long lower_threshold = 175;
static unsigned long upper_threshold = 450;
/* load balancing */
static unsigned long max_unbalance = 210;
/* trend load */
static unsigned long trend_unbalance = 40;
static unsigned long min_trend = 5;
/* instant load */
static unsigned long max_instant = 85;

/* Number of interrupts per second before exiting auto mode */
static u32 exit_irq_per_s = 1000;
static u64 old_num_irqs;

static DEFINE_MUTEX(usecase_mutex);
static bool user_config_updated;
static enum ux500_uc current_uc = UX500_UC_MAX;
static bool is_work_scheduled;
static bool is_early_suspend;
static bool uc_master_enable = true;

static unsigned int cpuidle_deepest_state;

struct usecase_config {
	char *name;
	/* Minimum required ARM OPP. if no requirement set 25 */
	unsigned int min_arm_opp;
	/* Only use max_arm_opp if you know what you're doing */
	unsigned int max_arm_opp;
	unsigned long cpuidle_multiplier;
	bool second_cpu_online;
	bool l2_prefetch_en;
	bool enable;
	unsigned int forced_state; /* Forced cpu idle state. */
	bool vc_override; /* QOS override for voice-call. */
};

static struct usecase_config usecase_conf[UX500_UC_MAX] = {
	[UX500_UC_NORMAL] = {
		.name			= "normal",
		.min_arm_opp		= 25,
		.cpuidle_multiplier	= 1024,
		.second_cpu_online	= true,
		.l2_prefetch_en		= true,
		.enable			= true,
		.forced_state		= 0,
		.vc_override		= false,
	},
	[UX500_UC_AUTO] = {
		.name			= "auto",
		.min_arm_opp		= 25,
		.cpuidle_multiplier	= 0,
		.second_cpu_online	= false,
		.l2_prefetch_en		= true,
		.enable			= false,
		.forced_state		= 0,
		.vc_override		= false,
	},
	[UX500_UC_VC] = {
		.name			= "voice-call",
		.min_arm_opp		= 50,
		.cpuidle_multiplier	= 0,
		.second_cpu_online	= true,
		.l2_prefetch_en		= false,
		.enable			= false,
		.forced_state		= 0,
		.vc_override		= true,
	},
	[UX500_UC_LPA] = {
		.name			= "low-power-audio",
		.min_arm_opp		= 50,
		.cpuidle_multiplier	= 0,
		.second_cpu_online	= false,
		.l2_prefetch_en		= false,
		.enable			= false,
		.forced_state		= 0, /* Updated dynamically */
		.vc_override		= false,
	},
};

/* daemon */
static struct delayed_work work_usecase;
static struct early_suspend usecase_early_suspend;

/* calculate loadavg */
#define LOAD_INT(x) ((x) >> FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)

extern int cpufreq_update_freq(int cpu, unsigned int min, unsigned int max);
extern int cpuidle_set_multiplier(unsigned int value);
extern int cpuidle_force_state(unsigned int state);

static unsigned long determine_loadavg(void)
{
	unsigned long  avg = 0;
	unsigned long avnrun[3];

	get_avenrun(avnrun, FIXED_1 / 200, 0);
	avg += (LOAD_INT(avnrun[0]) * 100) + (LOAD_FRAC(avnrun[0]) % 100);

	return avg;
}

static unsigned long determine_cpu_load(void)
{
	int i;
	unsigned long total_load = 0;

	/* get cpu load of each cpu */
	for_each_online_cpu(i) {
		unsigned int load, iowait;
		unsigned int idle_time, iowait_time, wall_time;
		cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
		struct hotplug_cpu_info *info;

		info = &per_cpu(hotplug_info, i);

		/* update both cur_idle_time and cur_wall_time */
		cur_idle_time = get_cpu_idle_time_us(i, &cur_wall_time);
		cur_iowait_time = get_cpu_iowait_time_us(i, &cur_wall_time);

		/* how much wall time has passed since last iteration? */
		wall_time = (unsigned int) cputime64_sub(cur_wall_time,
							info->prev_cpu_wall);
		info->prev_cpu_wall = cur_wall_time;

		/* how much idle time has passed since last iteration? */
		idle_time = (unsigned int) cputime64_sub(cur_idle_time,
							info->prev_cpu_idle);
		info->prev_cpu_idle = cur_idle_time;

		/* how much io wait time has passed since last iteration? */
		iowait_time = (unsigned int) cputime64_sub(cur_iowait_time,
							info->prev_cpu_io);
		info->prev_cpu_io = cur_iowait_time;

		if (unlikely(!wall_time || wall_time < idle_time))
			continue;

		/* load is the percentage of time not spent in idle */
		load = 100 * (wall_time - idle_time) / wall_time;
		info->load[info->idx] = load;
		hp_printk("cpu %d load %u ", i, load);

		/* iowait is the percentage of time not spent in io wait */
		iowait = 100 * (iowait_time) / wall_time;
		info->io[info->idx++] = load;
		hp_printk("iowait %u\n", iowait);

		if (info->idx >= LOAD_MONITOR)
			info->idx = 0;

		total_load += load;
	}

	return total_load;
}

static unsigned long determine_cpu_load_trend(void)
{
	int i, j, k;
	unsigned long total_load = 0;

	/* Get cpu load of each cpu */
	for_each_online_cpu(i) {
		unsigned int load = 0;
		struct hotplug_cpu_info *info;

		info = &per_cpu(hotplug_info, i);

		for (k = 0, j = info->idx; k < LOAD_MONITOR; k++, j++)
			load +=	info->load[j];

		load /= LOAD_MONITOR;

		hp_printk("cpu %d load trend %u\n", i, load);

		total_load += load;
	}

	return total_load;
}

static unsigned long determine_cpu_balance_trend(void)
{
	int i, j, k;
	unsigned long total_load = 0;
	unsigned long min_load = (unsigned long) (-1);

	/* Get cpu load of each cpu */
	for_each_online_cpu(i) {
		unsigned int load = 0;
		struct hotplug_cpu_info *info;

	info = &per_cpu(hotplug_info, i);

		for (k = 0, j = info->idx; k < LOAD_MONITOR; k++, j++)
			load +=	info->load[j];

		load /= LOAD_MONITOR;

		if (min_load > load)
			min_load = load;
		total_load += load;
	}

	if (min_load > min_trend)
		total_load = (100 * total_load) / min_load;
	else
		total_load = 50 << num_online_cpus();

	return total_load;
}

static void init_cpu_load_trend(void)
{
	int i;

	for_each_possible_cpu(i) {
		struct hotplug_cpu_info *info;
		int j;

		info = &per_cpu(hotplug_info, i);

		info->prev_cpu_idle = get_cpu_idle_time_us(i,
						&(info->prev_cpu_wall));
		info->prev_cpu_io = get_cpu_iowait_time_us(i,
						&(info->prev_cpu_wall));

		for (j = 0; j < LOAD_MONITOR; j++) {
			info->load[j] = 100;
			info->io[j] = 100;
		}
		info->idx = 0;
	}
}

static u32 get_num_interrupts_per_s(void)
{
	int cpu;
	int i;
	u64 num_irqs = 0;
	ktime_t now;
	static ktime_t last;
	unsigned int delta;
	u32 irqs = 0;

	now = ktime_get();

	for_each_possible_cpu(cpu) {
		for (i = 0; i < NR_IRQS; i++)
			num_irqs += kstat_irqs_cpu(i, cpu);
	}
	pr_debug("%s: total num irqs: %lld, previous %lld\n",
					__func__, num_irqs, old_num_irqs);

	if (old_num_irqs > 0) {
		delta = (u32)ktime_to_ms(ktime_sub(now, last)) / 1000;
		irqs = ((u32)(num_irqs - old_num_irqs)) / delta;
	}

	old_num_irqs = num_irqs;
	last = now;

	pr_debug("delta irqs per sec:%d\n", irqs);

	return irqs;
}

static int set_cpufreq(int cpu, int min_freq, int max_freq)
{
	int ret;
	struct cpufreq_policy policy;

	ret = cpufreq_get_policy(&policy, cpu);
	if (ret < 0) {
		pr_err("usecase-gov: failed to read policy\n");
		return ret;
	}

	if (policy.min > max_freq) {
		ret = cpufreq_update_freq(cpu, min_freq, policy.max);
		if (ret)
			pr_err("usecase-gov: update min cpufreq failed (1)\n");
	}
	if (policy.max < min_freq) {
		ret = cpufreq_update_freq(cpu, policy.min, max_freq);
		if (ret)
			pr_err("usecase-gov: update max cpufreq failed (2)\n");
	}

	ret = cpufreq_update_freq(cpu, min_freq, max_freq);
	if (ret)
		pr_err("usecase-gov: update min-max cpufreq failed\n");

	return ret;
}

static void set_cpu_config(enum ux500_uc new_uc)
{
	bool update = false;
	int ret;
	int cpu;
	static struct cpufreq_policy  original_cpufreq_policy;

	if (new_uc != current_uc)
		update = true;
	else if ((user_config_updated) && (new_uc == UX500_UC_USER))
		update = true;

	pr_debug("%s: new_usecase=%d, current_usecase=%d, update=%d\n",
		__func__, new_uc, current_uc, update);

	if (!update)
		goto exit;

	/* Cpu hotplug */
	if (!(usecase_conf[new_uc].second_cpu_online) &&
	    (num_online_cpus() > 1))
		cpu_down(1);
	else if ((usecase_conf[new_uc].second_cpu_online) &&
		 (num_online_cpus() < 2))
		cpu_up(1);

	if (usecase_conf[new_uc].max_arm_opp) {
		int max_freq;

		max_freq = dbx500_cpufreq_percent2freq(usecase_conf[new_uc].max_arm_opp);

		ret = cpufreq_get_policy(&original_cpufreq_policy, 0);
		if (ret < 0)
			pr_err("usecase-gov: fail to get cpufreq policy\n");

		for_each_online_cpu(cpu) {
			set_cpufreq(cpu,
				    original_cpufreq_policy.min,
				    max_freq);
		}
	}

	if (new_uc == UX500_UC_NORMAL &&
	    usecase_conf[current_uc].max_arm_opp) {
		/*
		 * Reset cpufreq limits to what is was before. Yes, overwrite
		 *  any changes done outside usecase governors control.
		 */
		for_each_online_cpu(cpu) {
			set_cpufreq(cpu,
				    original_cpufreq_policy.min,
				    original_cpufreq_policy.max);
		}
	}

	prcmu_qos_update_requirement(PRCMU_QOS_ARM_OPP,
			    "usecase", usecase_conf[new_uc].min_arm_opp);

	/* Cpu idle */
	cpuidle_set_multiplier(usecase_conf[new_uc].cpuidle_multiplier);

	/* L2 prefetch */
	if (usecase_conf[new_uc].l2_prefetch_en)
		outer_prefetch_enable();
	else
		outer_prefetch_disable();

	/* Force cpuidle state */
	cpuidle_force_state(usecase_conf[new_uc].forced_state);

	/* QOS override */
	prcmu_qos_voice_call_override(usecase_conf[new_uc].vc_override);

	current_uc = new_uc;

exit:
	/* Its ok to clear even if new_uc != UX500_UC_USER */
	user_config_updated = false;
}

void usecase_update_governor_state(void)
{
	bool cancel_work = false;

	mutex_lock(&usecase_mutex);

	if (uc_master_enable && (usecase_conf[UX500_UC_AUTO].enable ||
		usecase_conf[UX500_UC_USER].enable)) {
		/*
		 * Usecases are enabled. If we are in early suspend put
		 * governor to work.
		 */
		if (is_early_suspend && !is_work_scheduled) {
			schedule_delayed_work_on(0, &work_usecase,
				msecs_to_jiffies(CPULOAD_MEAS_DELAY));
			is_work_scheduled = true;
		} else if (!is_early_suspend && is_work_scheduled) {
			/* Exiting from early suspend. */
			cancel_work = true;
		}

	} else if (is_work_scheduled) {
		/* No usecase enabled or governor is not enabled. */
		cancel_work =  true;
	}

	if (cancel_work) {
		cancel_delayed_work_sync(&work_usecase);
		is_work_scheduled = false;

		/* Set the default settings before exiting. */
		set_cpu_config(UX500_UC_NORMAL);
	}

	mutex_unlock(&usecase_mutex);

}

/*
 * Start load measurment every 6 s in order detrmine if can unplug one CPU.
 * In order to not corrupt measurment, the first load average is not done
 * here call in  early suspend.
 */
static void usecase_earlysuspend_callback(struct early_suspend *h)
{
	init_cpu_load_trend();

	is_early_suspend = true;

	usecase_update_governor_state();
}

/* Stop measurement, call LCD early resume */
static void usecase_lateresume_callback(struct early_suspend *h)
{
	is_early_suspend = false;

	usecase_update_governor_state();
}

static void delayed_usecase_work(struct work_struct *work)
{
	unsigned long avg, load, trend, balance;
	bool inc_perf = false;
	bool dec_perf = false;
	u32 irqs_per_s;

	/* determine loadavg  */
	avg = determine_loadavg();
	hp_printk("loadavg = %lu lower th %lu upper th %lu\n",
					avg, lower_threshold, upper_threshold);

	/* determine instant load */
	load = determine_cpu_load();
	hp_printk("cpu instant load = %lu max %lu\n", load, max_instant);

	/* determine load trend */
	trend = determine_cpu_load_trend();
	hp_printk("cpu load trend = %lu min %lu unbal %lu\n",
					trend, min_trend, trend_unbalance);

	/* determine load balancing */
	balance = determine_cpu_balance_trend();
	hp_printk("load balancing trend = %lu min %lu\n",
					balance, max_unbalance);

	irqs_per_s = get_num_interrupts_per_s();

	/* Dont let configuration change in the middle of our calculations. */
	mutex_lock(&usecase_mutex);

	/* detect "instant" load increase */
	if (load > max_instant || irqs_per_s > exit_irq_per_s) {
		inc_perf = true;
	} else if (!usecase_conf[UX500_UC_USER].enable &&
			usecase_conf[UX500_UC_AUTO].enable) {
		/* detect high loadavg use case */
		if (avg > upper_threshold)
			inc_perf = true;
		/* detect idle use case */
		else if (trend < min_trend)
			dec_perf = true;
		/* detect unbalanced low cpu load use case */
		else if ((balance > max_unbalance) && (trend < trend_unbalance))
			dec_perf = true;
		/* detect low loadavg use case */
		else if (avg < lower_threshold)
			dec_perf = true;
		/* All user use cases disabled, current load not triggering
		 * any change.
		 */
		else if (user_config_updated)
			dec_perf = true;
	} else {
		dec_perf = true;
	}

	/*
	 * set_cpu_config() will not update the config unless it has been
	 * changed.
	 */
	if (dec_perf) {
		if (usecase_conf[UX500_UC_USER].enable)
			set_cpu_config(UX500_UC_USER);
		else if (usecase_conf[UX500_UC_AUTO].enable)
			set_cpu_config(UX500_UC_AUTO);
	} else if (inc_perf) {
		set_cpu_config(UX500_UC_NORMAL);
	}

	mutex_unlock(&usecase_mutex);

	/* reprogramm scheduled work */
	schedule_delayed_work_on(0, &work_usecase,
				msecs_to_jiffies(CPULOAD_MEAS_DELAY));

}

static struct dentry *usecase_dir;

#ifdef CONFIG_DEBUG_FS
#define define_set(_name) \
static ssize_t set_##_name(struct file *file, \
				 const char __user *user_buf, \
				 size_t count, loff_t *ppos) \
{ \
	int err; \
	long unsigned i; \
 \
	err = kstrtoul_from_user(user_buf, count, 0, &i); \
 \
	if (err) \
		return err; \
 \
	_name = i; \
	hp_printk("New value : %lu\n", _name); \
 \
	return count; \
}

define_set(upper_threshold);
define_set(lower_threshold);
define_set(max_unbalance);
define_set(trend_unbalance);
define_set(min_trend);
define_set(max_instant);
define_set(debug);

#define define_print(_name) \
static ssize_t print_##_name(struct seq_file *s, void *p) \
{ \
	return seq_printf(s, "%lu\n", _name); \
}

define_print(upper_threshold);
define_print(lower_threshold);
define_print(max_unbalance);
define_print(trend_unbalance);
define_print(min_trend);
define_print(max_instant);
define_print(debug);

#define define_open(_name) \
static ssize_t open_##_name(struct inode *inode, struct file *file) \
{ \
	return single_open(file, print_##_name, inode->i_private); \
}

define_open(upper_threshold);
define_open(lower_threshold);
define_open(max_unbalance);
define_open(trend_unbalance);
define_open(min_trend);
define_open(max_instant);
define_open(debug);

#define define_dbg_file(_name) \
static const struct file_operations fops_##_name = { \
	.open = open_##_name, \
	.write = set_##_name, \
	.read = seq_read, \
	.llseek = seq_lseek, \
	.release = single_release, \
	.owner = THIS_MODULE, \
}; \
static struct dentry *file_##_name;

define_dbg_file(upper_threshold);
define_dbg_file(lower_threshold);
define_dbg_file(max_unbalance);
define_dbg_file(trend_unbalance);
define_dbg_file(min_trend);
define_dbg_file(max_instant);
define_dbg_file(debug);

struct dbg_file {
	struct dentry **file;
	const struct file_operations *fops;
	const char *name;
};

#define define_dbg_entry(_name) \
{ \
	.file = &file_##_name, \
	.fops = &fops_##_name, \
	.name = #_name \
}

static struct dbg_file debug_entry[] = {
	define_dbg_entry(upper_threshold),
	define_dbg_entry(lower_threshold),
	define_dbg_entry(max_unbalance),
	define_dbg_entry(trend_unbalance),
	define_dbg_entry(min_trend),
	define_dbg_entry(max_instant),
	define_dbg_entry(debug),
};

static int setup_debugfs(void)
{
	int i;
	usecase_dir = debugfs_create_dir("usecase", NULL);

	if (IS_ERR_OR_NULL(usecase_dir))
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(debug_entry); i++) {
		if (IS_ERR_OR_NULL(debugfs_create_file(debug_entry[i].name,
						       S_IWUGO | S_IRUGO,
						       usecase_dir,
						       NULL,
						       debug_entry[i].fops)))
			goto fail;
	}

	if (IS_ERR_OR_NULL(debugfs_create_u32("exit_irq_per_s",
					      S_IWUGO | S_IRUGO, usecase_dir,
					      &exit_irq_per_s)))
		goto fail;
	return 0;
fail:
	debugfs_remove_recursive(usecase_dir);
	return -EINVAL;
}
#else
static int setup_debugfs(void)
{
	return 0;
}
#endif

static void usecase_update_user_config(void)
{
	int i;
	bool config_enable = false;
	struct usecase_config *user_conf = &usecase_conf[UX500_UC_USER];

	mutex_lock(&usecase_mutex);

	user_conf->min_arm_opp = 25;
	user_conf->max_arm_opp = 0;
	user_conf->cpuidle_multiplier = 0;
	user_conf->second_cpu_online = false;
	user_conf->l2_prefetch_en = false;
	user_conf->forced_state = cpuidle_deepest_state;
	user_conf->vc_override = true; /* A single false will clear it. */

	/* Dont include Auto and Normal modes in this */
	for (i = (UX500_UC_AUTO + 1); i < UX500_UC_USER; i++) {
		if (!usecase_conf[i].enable)
			continue;

		config_enable = true;

		/* It's the highest arm opp requirement that should be used */
		if (usecase_conf[i].min_arm_opp > user_conf->min_arm_opp)
			user_conf->min_arm_opp = usecase_conf[i].min_arm_opp;

		if (usecase_conf[i].max_arm_opp > user_conf->max_arm_opp)
			user_conf->max_arm_opp = usecase_conf[i].max_arm_opp;

		if (usecase_conf[i].cpuidle_multiplier >
					user_conf->cpuidle_multiplier)
			user_conf->cpuidle_multiplier =
				usecase_conf[i].cpuidle_multiplier;

		user_conf->second_cpu_online |=
				usecase_conf[i].second_cpu_online;

		user_conf->l2_prefetch_en |=
				usecase_conf[i].l2_prefetch_en;

		/* Take the shallowest state. */
		if (usecase_conf[i].forced_state < user_conf->forced_state)
			user_conf->forced_state = usecase_conf[i].forced_state;

		/* Only override QOS if all enabled configurations are
		 * requesting it.
		 */
		if (!usecase_conf[i].vc_override)
			user_conf->vc_override = false;
	}

	user_conf->enable = config_enable;
	user_config_updated = true;

	mutex_unlock(&usecase_mutex);
}

struct usecase_devclass_attr {
	struct sysdev_class_attribute class_attr;
	u32 index;
};

/* One for each usecase except "user" + current + enable */
#define UX500_NUM_SYSFS_NODES (UX500_UC_USER + 2)
#define UX500_CURRENT_NODE_INDEX (UX500_NUM_SYSFS_NODES - 1)
#define UX500_ENABLE_NODE_INDEX (UX500_NUM_SYSFS_NODES - 2)

static struct usecase_devclass_attr usecase_dc_attr[UX500_NUM_SYSFS_NODES];

static struct attribute *dbs_attributes[UX500_NUM_SYSFS_NODES + 1] = {NULL};

static struct attribute_group dbs_attr_group = {
	.attrs = dbs_attributes,
	.name = "usecase",
};

static ssize_t show_current(struct sysdev_class *class,
			struct sysdev_class_attribute *attr, char *buf)
{
	enum ux500_uc display_uc = (current_uc == UX500_UC_MAX) ?
					UX500_UC_NORMAL : current_uc;

	return sprintf(buf, "min_arm_opp: %d\n"
		"max_arm_opp: %d\n"
		"cpuidle_multiplier: %ld\n"
		"second_cpu_online: %s\n"
		"l2_prefetch_en: %s\n"
		"forced_state: %d\n"
		"vc_override: %s\n",
		usecase_conf[display_uc].min_arm_opp,
		usecase_conf[display_uc].max_arm_opp,
		usecase_conf[display_uc].cpuidle_multiplier,
		usecase_conf[display_uc].second_cpu_online ? "true" : "false",
		usecase_conf[display_uc].l2_prefetch_en ? "true" : "false",
		usecase_conf[display_uc].forced_state,
		usecase_conf[display_uc].vc_override ? "true" : "false");
}

static ssize_t show_enable(struct sysdev_class *class,
			struct sysdev_class_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", uc_master_enable);
}

static ssize_t store_enable(struct sysdev_class *class,
					struct sysdev_class_attribute *attr,
				    const char *buf, size_t count)
{
	unsigned int input;
	int ret;

	ret = sscanf(buf, "%u", &input);
	if (ret != 1)
		return -EINVAL;

	uc_master_enable = (bool) input;

	usecase_update_governor_state();

	return count;
}

static ssize_t show_dc_attr(struct sysdev_class *class,
			struct sysdev_class_attribute *attr, char *buf)
{
	struct usecase_devclass_attr *uattr =
		container_of(attr, struct usecase_devclass_attr, class_attr);

	return sprintf(buf, "%u\n",
				usecase_conf[uattr->index].enable);
}

static ssize_t store_dc_attr(struct sysdev_class *class,
					struct sysdev_class_attribute *attr,
				    const char *buf, size_t count)
{
	unsigned int input;
	int ret;

	struct usecase_devclass_attr *uattr =
		container_of(attr, struct usecase_devclass_attr, class_attr);

	ret = sscanf(buf, "%u", &input);

	/* Normal mode cant be changed. */
	if ((ret != 1) || (uattr->index == 0))
		return -EINVAL;

	usecase_conf[uattr->index].enable = (bool)input;

	usecase_update_user_config();

	usecase_update_governor_state();

	return count;
}

static int usecase_sysfs_init(void)
{
	int err;
	int i;

	/* Last two nodes are not based on usecase configurations */
	for (i = 0; i < (UX500_NUM_SYSFS_NODES - 2); i++) {
		usecase_dc_attr[i].class_attr.attr.name = usecase_conf[i].name;
		usecase_dc_attr[i].class_attr.attr.mode = 0644;
		usecase_dc_attr[i].class_attr.show = show_dc_attr;
		usecase_dc_attr[i].class_attr.store = store_dc_attr;
		usecase_dc_attr[i].index = i;

		dbs_attributes[i] = &(usecase_dc_attr[i].class_attr.attr);
	}

	/* sysfs current */
	usecase_dc_attr[UX500_CURRENT_NODE_INDEX].class_attr.attr.name =
		"current";
	usecase_dc_attr[UX500_CURRENT_NODE_INDEX].class_attr.attr.mode =
		0644;
	usecase_dc_attr[UX500_CURRENT_NODE_INDEX].class_attr.show =
		show_current;
	usecase_dc_attr[UX500_CURRENT_NODE_INDEX].class_attr.store =
		NULL;
	usecase_dc_attr[UX500_CURRENT_NODE_INDEX].index =
		0;
	dbs_attributes[UX500_CURRENT_NODE_INDEX] =
		&(usecase_dc_attr[UX500_CURRENT_NODE_INDEX].class_attr.attr);

	/* sysfs enable */
	usecase_dc_attr[UX500_ENABLE_NODE_INDEX].class_attr.attr.name =
		"enable";
	usecase_dc_attr[UX500_ENABLE_NODE_INDEX].class_attr.attr.mode =
		0644;
	usecase_dc_attr[UX500_ENABLE_NODE_INDEX].class_attr.show =
		show_enable;
	usecase_dc_attr[UX500_ENABLE_NODE_INDEX].class_attr.store =
		store_enable;
	usecase_dc_attr[UX500_ENABLE_NODE_INDEX].index =
		0;
	dbs_attributes[UX500_ENABLE_NODE_INDEX] =
		&(usecase_dc_attr[UX500_ENABLE_NODE_INDEX].class_attr.attr);

	err = sysfs_create_group(&(cpu_sysdev_class.kset.kobj),
						&dbs_attr_group);
	if (err)
		pr_err("usecase-gov: sysfs_create_group"
				" failed with error = %d\n", err);

	return err;
}

static void usecase_cpuidle_init(void)
{
	int max_states;
	int i;
	struct cstate *state = ux500_ci_get_cstates(&max_states);

	for (i = 0; i < max_states; i++)
		if ((state[i].APE == APE_OFF) && (state[i].ARM == ARM_RET))
			break;

	usecase_conf[UX500_UC_LPA].forced_state = i;

	cpuidle_deepest_state = max_states - 1;
}

/*  initialize devices */
static int __init init_usecase_devices(void)
{
	int err;

	pr_info("Use-case governor initialized\n");

	/*  add early_suspend callback */
	usecase_early_suspend.level = 200;
	usecase_early_suspend.suspend = usecase_earlysuspend_callback;
	usecase_early_suspend.resume = usecase_lateresume_callback;
	register_early_suspend(&usecase_early_suspend);

	/* register delayed queuework */
	INIT_DELAYED_WORK_DEFERRABLE(&work_usecase,
				     delayed_usecase_work);

	init_cpu_load_trend();

	err = setup_debugfs();
	if (err)
		goto error;
	err = usecase_sysfs_init();
	if (err)
		goto error2;

	usecase_cpuidle_init();

	prcmu_qos_add_requirement(PRCMU_QOS_ARM_OPP, "usecase", 25);

	return 0;
error2:
	debugfs_remove_recursive(usecase_dir);
error:
	unregister_early_suspend(&usecase_early_suspend);
	return err;
}

device_initcall(init_usecase_devices);