ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / kernel / sched / tune.c
1 #include <linux/cgroup.h>
2 #include <linux/err.h>
3 #include <linux/kernel.h>
4 #include <linux/percpu.h>
5 #include <linux/printk.h>
6 #include <linux/rcupdate.h>
7 #include <linux/slab.h>
8
9 #include <trace/events/sched.h>
10
11 #include "sched.h"
12 #include "tune.h"
13
14 #ifdef CONFIG_CGROUP_SCHEDTUNE
15 static bool schedtune_initialized = false;
16 #endif
17
18 unsigned int sysctl_sched_cfs_boost __read_mostly;
19
20 extern struct target_nrg schedtune_target_nrg;
21
22 /* Performance Boost region (B) threshold params */
23 static int perf_boost_idx;
24
25 /* Performance Constraint region (C) threshold params */
26 static int perf_constrain_idx;
27
28 /**
29  * Performance-Energy (P-E) Space thresholds constants
30  */
31 struct threshold_params {
32         int nrg_gain;
33         int cap_gain;
34 };
35
36 /*
37  * System specific P-E space thresholds constants
38  */
39 static struct threshold_params
40 threshold_gains[] = {
41         { 0, 5 }, /*   < 10% */
42         { 1, 5 }, /*   < 20% */
43         { 2, 5 }, /*   < 30% */
44         { 3, 5 }, /*   < 40% */
45         { 4, 5 }, /*   < 50% */
46         { 5, 4 }, /*   < 60% */
47         { 5, 3 }, /*   < 70% */
48         { 5, 2 }, /*   < 80% */
49         { 5, 1 }, /*   < 90% */
50         { 5, 0 }  /* <= 100% */
51 };
52
53 static int
54 __schedtune_accept_deltas(int nrg_delta, int cap_delta,
55                           int perf_boost_idx, int perf_constrain_idx)
56 {
57         int payoff = -INT_MAX;
58         int gain_idx = -1;
59
60         /* Performance Boost (B) region */
61         if (nrg_delta >= 0 && cap_delta > 0)
62                 gain_idx = perf_boost_idx;
63         /* Performance Constraint (C) region */
64         else if (nrg_delta < 0 && cap_delta <= 0)
65                 gain_idx = perf_constrain_idx;
66
67         /* Default: reject schedule candidate */
68         if (gain_idx == -1)
69                 return payoff;
70
71         /*
72          * Evaluate "Performance Boost" vs "Energy Increase"
73          *
74          * - Performance Boost (B) region
75          *
76          *   Condition: nrg_delta > 0 && cap_delta > 0
77          *   Payoff criteria:
78          *     cap_gain / nrg_gain  < cap_delta / nrg_delta =
79          *     cap_gain * nrg_delta < cap_delta * nrg_gain
80          *   Note that since both nrg_gain and nrg_delta are positive, the
81          *   inequality does not change. Thus:
82          *
83          *     payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
84          *
85          * - Performance Constraint (C) region
86          *
87          *   Condition: nrg_delta < 0 && cap_delta < 0
88          *   payoff criteria:
89          *     cap_gain / nrg_gain  > cap_delta / nrg_delta =
90          *     cap_gain * nrg_delta < cap_delta * nrg_gain
91          *   Note that since nrg_gain > 0 while nrg_delta < 0, the
92          *   inequality change. Thus:
93          *
94          *     payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
95          *
96          * This means that, in case of same positive defined {cap,nrg}_gain
97          * for both the B and C regions, we can use the same payoff formula
98          * where a positive value represents the accept condition.
99          */
100         payoff  = cap_delta * threshold_gains[gain_idx].nrg_gain;
101         payoff -= nrg_delta * threshold_gains[gain_idx].cap_gain;
102
103         return payoff;
104 }
105
106 #ifdef CONFIG_CGROUP_SCHEDTUNE
107
108 /*
109  * EAS scheduler tunables for task groups.
110  */
111
112 /* SchdTune tunables for a group of tasks */
113 struct schedtune {
114         /* SchedTune CGroup subsystem */
115         struct cgroup_subsys_state css;
116
117         /* Boost group allocated ID */
118         int idx;
119
120         /* Boost value for tasks on that SchedTune CGroup */
121         int boost;
122
123         /* Performance Boost (B) region threshold params */
124         int perf_boost_idx;
125
126         /* Performance Constraint (C) region threshold params */
127         int perf_constrain_idx;
128
129         /* Hint to bias scheduling of tasks on that SchedTune CGroup
130          * towards idle CPUs */
131         int prefer_idle;
132 };
133
134 static inline struct schedtune *css_st(struct cgroup_subsys_state *css)
135 {
136         return css ? container_of(css, struct schedtune, css) : NULL;
137 }
138
139 static inline struct schedtune *task_schedtune(struct task_struct *tsk)
140 {
141         return css_st(task_css(tsk, schedtune_cgrp_id));
142 }
143
144 static inline struct schedtune *parent_st(struct schedtune *st)
145 {
146         return css_st(st->css.parent);
147 }
148
149 /*
150  * SchedTune root control group
151  * The root control group is used to defined a system-wide boosting tuning,
152  * which is applied to all tasks in the system.
153  * Task specific boost tuning could be specified by creating and
154  * configuring a child control group under the root one.
155  * By default, system-wide boosting is disabled, i.e. no boosting is applied
156  * to tasks which are not into a child control group.
157  */
158 static struct schedtune
159 root_schedtune = {
160         .boost  = 0,
161         .perf_boost_idx = 0,
162         .perf_constrain_idx = 0,
163         .prefer_idle = 0,
164 };
165
166 int
167 schedtune_accept_deltas(int nrg_delta, int cap_delta,
168                         struct task_struct *task)
169 {
170         struct schedtune *ct;
171         int perf_boost_idx;
172         int perf_constrain_idx;
173
174         /* Optimal (O) region */
175         if (nrg_delta < 0 && cap_delta > 0) {
176                 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
177                 return INT_MAX;
178         }
179
180         /* Suboptimal (S) region */
181         if (nrg_delta > 0 && cap_delta < 0) {
182                 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
183                 return -INT_MAX;
184         }
185
186         /* Get task specific perf Boost/Constraints indexes */
187         rcu_read_lock();
188         ct = task_schedtune(task);
189         perf_boost_idx = ct->perf_boost_idx;
190         perf_constrain_idx = ct->perf_constrain_idx;
191         rcu_read_unlock();
192
193         return __schedtune_accept_deltas(nrg_delta, cap_delta,
194                         perf_boost_idx, perf_constrain_idx);
195 }
196
197 /*
198  * Maximum number of boost groups to support
199  * When per-task boosting is used we still allow only limited number of
200  * boost groups for two main reasons:
201  * 1. on a real system we usually have only few classes of workloads which
202  *    make sense to boost with different values (e.g. background vs foreground
203  *    tasks, interactive vs low-priority tasks)
204  * 2. a limited number allows for a simpler and more memory/time efficient
205  *    implementation especially for the computation of the per-CPU boost
206  *    value
207  */
208 #define BOOSTGROUPS_COUNT 4
209
210 /* Array of configured boostgroups */
211 static struct schedtune *allocated_group[BOOSTGROUPS_COUNT] = {
212         &root_schedtune,
213         NULL,
214 };
215
216 /* SchedTune boost groups
217  * Keep track of all the boost groups which impact on CPU, for example when a
218  * CPU has two RUNNABLE tasks belonging to two different boost groups and thus
219  * likely with different boost values.
220  * Since on each system we expect only a limited number of boost groups, here
221  * we use a simple array to keep track of the metrics required to compute the
222  * maximum per-CPU boosting value.
223  */
224 struct boost_groups {
225         /* Maximum boost value for all RUNNABLE tasks on a CPU */
226         bool idle;
227         int boost_max;
228         struct {
229                 /* The boost for tasks on that boost group */
230                 int boost;
231                 /* Count of RUNNABLE tasks on that boost group */
232                 unsigned tasks;
233         } group[BOOSTGROUPS_COUNT];
234         /* CPU's boost group locking */
235         raw_spinlock_t lock;
236 };
237
238 /* Boost groups affecting each CPU in the system */
239 DEFINE_PER_CPU(struct boost_groups, cpu_boost_groups);
240
241 static void
242 schedtune_cpu_update(int cpu)
243 {
244         struct boost_groups *bg;
245         int boost_max;
246         int idx;
247
248         bg = &per_cpu(cpu_boost_groups, cpu);
249
250         /* The root boost group is always active */
251         boost_max = bg->group[0].boost;
252         for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx) {
253                 /*
254                  * A boost group affects a CPU only if it has
255                  * RUNNABLE tasks on that CPU
256                  */
257                 if (bg->group[idx].tasks == 0)
258                         continue;
259
260                 boost_max = max(boost_max, bg->group[idx].boost);
261         }
262         /* Ensures boost_max is non-negative when all cgroup boost values
263          * are neagtive. Avoids under-accounting of cpu capacity which may cause
264          * task stacking and frequency spikes.*/
265         boost_max = max(boost_max, 0);
266         bg->boost_max = boost_max;
267 }
268
269 static int
270 schedtune_boostgroup_update(int idx, int boost)
271 {
272         struct boost_groups *bg;
273         int cur_boost_max;
274         int old_boost;
275         int cpu;
276
277         /* Update per CPU boost groups */
278         for_each_possible_cpu(cpu) {
279                 bg = &per_cpu(cpu_boost_groups, cpu);
280
281                 /*
282                  * Keep track of current boost values to compute the per CPU
283                  * maximum only when it has been affected by the new value of
284                  * the updated boost group
285                  */
286                 cur_boost_max = bg->boost_max;
287                 old_boost = bg->group[idx].boost;
288
289                 /* Update the boost value of this boost group */
290                 bg->group[idx].boost = boost;
291
292                 /* Check if this update increase current max */
293                 if (boost > cur_boost_max && bg->group[idx].tasks) {
294                         bg->boost_max = boost;
295                         trace_sched_tune_boostgroup_update(cpu, 1, bg->boost_max);
296                         continue;
297                 }
298
299                 /* Check if this update has decreased current max */
300                 if (cur_boost_max == old_boost && old_boost > boost) {
301                         schedtune_cpu_update(cpu);
302                         trace_sched_tune_boostgroup_update(cpu, -1, bg->boost_max);
303                         continue;
304                 }
305
306                 trace_sched_tune_boostgroup_update(cpu, 0, bg->boost_max);
307         }
308
309         return 0;
310 }
311
312 #define ENQUEUE_TASK  1
313 #define DEQUEUE_TASK -1
314
315 static inline void
316 schedtune_tasks_update(struct task_struct *p, int cpu, int idx, int task_count)
317 {
318         struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
319         int tasks = bg->group[idx].tasks + task_count;
320
321         /* Update boosted tasks count while avoiding to make it negative */
322         bg->group[idx].tasks = max(0, tasks);
323
324         trace_sched_tune_tasks_update(p, cpu, tasks, idx,
325                         bg->group[idx].boost, bg->boost_max);
326
327         /* Boost group activation or deactivation on that RQ */
328         if (tasks == 1 || tasks == 0)
329                 schedtune_cpu_update(cpu);
330 }
331
332 /*
333  * NOTE: This function must be called while holding the lock on the CPU RQ
334  */
335 void schedtune_enqueue_task(struct task_struct *p, int cpu)
336 {
337         struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
338         unsigned long irq_flags;
339         struct schedtune *st;
340         int idx;
341
342         if (!unlikely(schedtune_initialized))
343                 return;
344
345         /*
346          * When a task is marked PF_EXITING by do_exit() it's going to be
347          * dequeued and enqueued multiple times in the exit path.
348          * Thus we avoid any further update, since we do not want to change
349          * CPU boosting while the task is exiting.
350          */
351         if (p->flags & PF_EXITING)
352                 return;
353
354         /*
355          * Boost group accouting is protected by a per-cpu lock and requires
356          * interrupt to be disabled to avoid race conditions for example on
357          * do_exit()::cgroup_exit() and task migration.
358          */
359         raw_spin_lock_irqsave(&bg->lock, irq_flags);
360         rcu_read_lock();
361
362         st = task_schedtune(p);
363         idx = st->idx;
364
365         schedtune_tasks_update(p, cpu, idx, ENQUEUE_TASK);
366
367         rcu_read_unlock();
368         raw_spin_unlock_irqrestore(&bg->lock, irq_flags);
369 }
370
371 int schedtune_can_attach(struct cgroup_taskset *tset)
372 {
373         struct task_struct *task;
374         struct cgroup_subsys_state *css;
375         struct boost_groups *bg;
376         unsigned long irq_flags;
377         unsigned int cpu;
378         struct rq *rq;
379         int src_bg; /* Source boost group index */
380         int dst_bg; /* Destination boost group index */
381         int tasks;
382
383         if (!unlikely(schedtune_initialized))
384                 return 0;
385
386
387         cgroup_taskset_for_each(task, css, tset) {
388
389                 /*
390                  * Lock the CPU's RQ the task is enqueued to avoid race
391                  * conditions with migration code while the task is being
392                  * accounted
393                  */
394                 rq = lock_rq_of(task, &irq_flags);
395
396                 if (!task->on_rq) {
397                         unlock_rq_of(rq, task, &irq_flags);
398                         continue;
399                 }
400
401                 /*
402                  * Boost group accouting is protected by a per-cpu lock and requires
403                  * interrupt to be disabled to avoid race conditions on...
404                  */
405                 cpu = cpu_of(rq);
406                 bg = &per_cpu(cpu_boost_groups, cpu);
407                 raw_spin_lock(&bg->lock);
408
409                 dst_bg = css_st(css)->idx;
410                 src_bg = task_schedtune(task)->idx;
411
412                 /*
413                  * Current task is not changing boostgroup, which can
414                  * happen when the new hierarchy is in use.
415                  */
416                 if (unlikely(dst_bg == src_bg)) {
417                         raw_spin_unlock(&bg->lock);
418                         unlock_rq_of(rq, task, &irq_flags);
419                         continue;
420                 }
421
422                 /*
423                  * This is the case of a RUNNABLE task which is switching its
424                  * current boost group.
425                  */
426
427                 /* Move task from src to dst boost group */
428                 tasks = bg->group[src_bg].tasks - 1;
429                 bg->group[src_bg].tasks = max(0, tasks);
430                 bg->group[dst_bg].tasks += 1;
431
432                 raw_spin_unlock(&bg->lock);
433                 unlock_rq_of(rq, task, &irq_flags);
434
435                 /* Update CPU boost group */
436                 if (bg->group[src_bg].tasks == 0 || bg->group[dst_bg].tasks == 1)
437                         schedtune_cpu_update(task_cpu(task));
438
439         }
440
441         return 0;
442 }
443
444 void schedtune_cancel_attach(struct cgroup_taskset *tset)
445 {
446         /* This can happen only if SchedTune controller is mounted with
447          * other hierarchies ane one of them fails. Since usually SchedTune is
448          * mouted on its own hierarcy, for the time being we do not implement
449          * a proper rollback mechanism */
450         WARN(1, "SchedTune cancel attach not implemented");
451 }
452
453 /*
454  * NOTE: This function must be called while holding the lock on the CPU RQ
455  */
456 void schedtune_dequeue_task(struct task_struct *p, int cpu)
457 {
458         struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
459         unsigned long irq_flags;
460         struct schedtune *st;
461         int idx;
462
463         if (!unlikely(schedtune_initialized))
464                 return;
465
466         /*
467          * When a task is marked PF_EXITING by do_exit() it's going to be
468          * dequeued and enqueued multiple times in the exit path.
469          * Thus we avoid any further update, since we do not want to change
470          * CPU boosting while the task is exiting.
471          * The last dequeue is already enforce by the do_exit() code path
472          * via schedtune_exit_task().
473          */
474         if (p->flags & PF_EXITING)
475                 return;
476
477         /*
478          * Boost group accouting is protected by a per-cpu lock and requires
479          * interrupt to be disabled to avoid race conditions on...
480          */
481         raw_spin_lock_irqsave(&bg->lock, irq_flags);
482         rcu_read_lock();
483
484         st = task_schedtune(p);
485         idx = st->idx;
486
487         schedtune_tasks_update(p, cpu, idx, DEQUEUE_TASK);
488
489         rcu_read_unlock();
490         raw_spin_unlock_irqrestore(&bg->lock, irq_flags);
491 }
492
493 void schedtune_exit_task(struct task_struct *tsk)
494 {
495         struct schedtune *st;
496         unsigned long irq_flags;
497         unsigned int cpu;
498         struct rq *rq;
499         int idx;
500
501         if (!unlikely(schedtune_initialized))
502                 return;
503
504         rq = lock_rq_of(tsk, &irq_flags);
505         rcu_read_lock();
506
507         cpu = cpu_of(rq);
508         st = task_schedtune(tsk);
509         idx = st->idx;
510         schedtune_tasks_update(tsk, cpu, idx, DEQUEUE_TASK);
511
512         rcu_read_unlock();
513         unlock_rq_of(rq, tsk, &irq_flags);
514 }
515
516 int schedtune_cpu_boost(int cpu)
517 {
518         struct boost_groups *bg;
519
520         bg = &per_cpu(cpu_boost_groups, cpu);
521         return bg->boost_max;
522 }
523
524 int schedtune_task_boost(struct task_struct *p)
525 {
526         struct schedtune *st;
527         int task_boost;
528
529         /* Get task boost value */
530         rcu_read_lock();
531         st = task_schedtune(p);
532         task_boost = st->boost;
533         rcu_read_unlock();
534
535         return task_boost;
536 }
537
538 int schedtune_prefer_idle(struct task_struct *p)
539 {
540         struct schedtune *st;
541         int prefer_idle;
542
543         /* Get prefer_idle value */
544         rcu_read_lock();
545         st = task_schedtune(p);
546         prefer_idle = st->prefer_idle;
547         rcu_read_unlock();
548
549         return prefer_idle;
550 }
551
552 static u64
553 prefer_idle_read(struct cgroup_subsys_state *css, struct cftype *cft)
554 {
555         struct schedtune *st = css_st(css);
556
557         return st->prefer_idle;
558 }
559
560 static int
561 prefer_idle_write(struct cgroup_subsys_state *css, struct cftype *cft,
562             u64 prefer_idle)
563 {
564         struct schedtune *st = css_st(css);
565         st->prefer_idle = prefer_idle;
566
567         return 0;
568 }
569
570 static s64
571 boost_read(struct cgroup_subsys_state *css, struct cftype *cft)
572 {
573         struct schedtune *st = css_st(css);
574
575         return st->boost;
576 }
577
578 static int
579 boost_write(struct cgroup_subsys_state *css, struct cftype *cft,
580             s64 boost)
581 {
582         struct schedtune *st = css_st(css);
583         unsigned threshold_idx;
584         int boost_pct;
585
586         if (boost < -100 || boost > 100)
587                 return -EINVAL;
588         boost_pct = boost;
589
590         /*
591          * Update threshold params for Performance Boost (B)
592          * and Performance Constraint (C) regions.
593          * The current implementatio uses the same cuts for both
594          * B and C regions.
595          */
596         threshold_idx = clamp(boost_pct, 0, 99) / 10;
597         st->perf_boost_idx = threshold_idx;
598         st->perf_constrain_idx = threshold_idx;
599
600         st->boost = boost;
601         if (css == &root_schedtune.css) {
602                 sysctl_sched_cfs_boost = boost;
603                 perf_boost_idx  = threshold_idx;
604                 perf_constrain_idx  = threshold_idx;
605         }
606
607         /* Update CPU boost */
608         schedtune_boostgroup_update(st->idx, st->boost);
609
610         trace_sched_tune_config(st->boost);
611
612         return 0;
613 }
614
615 static struct cftype files[] = {
616         {
617                 .name = "boost",
618                 .read_s64 = boost_read,
619                 .write_s64 = boost_write,
620         },
621         {
622                 .name = "prefer_idle",
623                 .read_u64 = prefer_idle_read,
624                 .write_u64 = prefer_idle_write,
625         },
626         { }     /* terminate */
627 };
628
629 static int
630 schedtune_boostgroup_init(struct schedtune *st)
631 {
632         struct boost_groups *bg;
633         int cpu;
634
635         /* Keep track of allocated boost groups */
636         allocated_group[st->idx] = st;
637
638         /* Initialize the per CPU boost groups */
639         for_each_possible_cpu(cpu) {
640                 bg = &per_cpu(cpu_boost_groups, cpu);
641                 bg->group[st->idx].boost = 0;
642                 bg->group[st->idx].tasks = 0;
643         }
644
645         return 0;
646 }
647
648 static struct cgroup_subsys_state *
649 schedtune_css_alloc(struct cgroup_subsys_state *parent_css)
650 {
651         struct schedtune *st;
652         int idx;
653
654         if (!parent_css)
655                 return &root_schedtune.css;
656
657         /* Allow only single level hierachies */
658         if (parent_css != &root_schedtune.css) {
659                 pr_err("Nested SchedTune boosting groups not allowed\n");
660                 return ERR_PTR(-ENOMEM);
661         }
662
663         /* Allow only a limited number of boosting groups */
664         for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx)
665                 if (!allocated_group[idx])
666                         break;
667         if (idx == BOOSTGROUPS_COUNT) {
668                 pr_err("Trying to create more than %d SchedTune boosting groups\n",
669                        BOOSTGROUPS_COUNT);
670                 return ERR_PTR(-ENOSPC);
671         }
672
673         st = kzalloc(sizeof(*st), GFP_KERNEL);
674         if (!st)
675                 goto out;
676
677         /* Initialize per CPUs boost group support */
678         st->idx = idx;
679         if (schedtune_boostgroup_init(st))
680                 goto release;
681
682         return &st->css;
683
684 release:
685         kfree(st);
686 out:
687         return ERR_PTR(-ENOMEM);
688 }
689
690 static void
691 schedtune_boostgroup_release(struct schedtune *st)
692 {
693         /* Reset this boost group */
694         schedtune_boostgroup_update(st->idx, 0);
695
696         /* Keep track of allocated boost groups */
697         allocated_group[st->idx] = NULL;
698 }
699
700 static void
701 schedtune_css_free(struct cgroup_subsys_state *css)
702 {
703         struct schedtune *st = css_st(css);
704
705         schedtune_boostgroup_release(st);
706         kfree(st);
707 }
708
709 struct cgroup_subsys schedtune_cgrp_subsys = {
710         .css_alloc      = schedtune_css_alloc,
711         .css_free       = schedtune_css_free,
712         .can_attach     = schedtune_can_attach,
713         .cancel_attach  = schedtune_cancel_attach,
714         .legacy_cftypes = files,
715         .early_init     = 1,
716 };
717
718 static inline void
719 schedtune_init_cgroups(void)
720 {
721         struct boost_groups *bg;
722         int cpu;
723
724         /* Initialize the per CPU boost groups */
725         for_each_possible_cpu(cpu) {
726                 bg = &per_cpu(cpu_boost_groups, cpu);
727                 memset(bg, 0, sizeof(struct boost_groups));
728                 raw_spin_lock_init(&bg->lock);
729         }
730
731         pr_info("schedtune: configured to support %d boost groups\n",
732                 BOOSTGROUPS_COUNT);
733
734         schedtune_initialized = true;
735 }
736
737 #else /* CONFIG_CGROUP_SCHEDTUNE */
738
739 int
740 schedtune_accept_deltas(int nrg_delta, int cap_delta,
741                         struct task_struct *task)
742 {
743         /* Optimal (O) region */
744         if (nrg_delta < 0 && cap_delta > 0) {
745                 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
746                 return INT_MAX;
747         }
748
749         /* Suboptimal (S) region */
750         if (nrg_delta > 0 && cap_delta < 0) {
751                 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
752                 return -INT_MAX;
753         }
754
755         return __schedtune_accept_deltas(nrg_delta, cap_delta,
756                         perf_boost_idx, perf_constrain_idx);
757 }
758
759 #endif /* CONFIG_CGROUP_SCHEDTUNE */
760
761 int
762 sysctl_sched_cfs_boost_handler(struct ctl_table *table, int write,
763                                void __user *buffer, size_t *lenp,
764                                loff_t *ppos)
765 {
766         int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
767         unsigned threshold_idx;
768         int boost_pct;
769
770         if (ret || !write)
771                 return ret;
772
773         if (sysctl_sched_cfs_boost < -100 || sysctl_sched_cfs_boost > 100)
774                 return -EINVAL;
775         boost_pct = sysctl_sched_cfs_boost;
776
777         /*
778          * Update threshold params for Performance Boost (B)
779          * and Performance Constraint (C) regions.
780          * The current implementatio uses the same cuts for both
781          * B and C regions.
782          */
783         threshold_idx = clamp(boost_pct, 0, 99) / 10;
784         perf_boost_idx = threshold_idx;
785         perf_constrain_idx = threshold_idx;
786
787         return 0;
788 }
789
790 #ifdef CONFIG_SCHED_DEBUG
791 static void
792 schedtune_test_nrg(unsigned long delta_pwr)
793 {
794         unsigned long test_delta_pwr;
795         unsigned long test_norm_pwr;
796         int idx;
797
798         /*
799          * Check normalization constants using some constant system
800          * energy values
801          */
802         pr_info("schedtune: verify normalization constants...\n");
803         for (idx = 0; idx < 6; ++idx) {
804                 test_delta_pwr = delta_pwr >> idx;
805
806                 /* Normalize on max energy for target platform */
807                 test_norm_pwr = reciprocal_divide(
808                                         test_delta_pwr << SCHED_LOAD_SHIFT,
809                                         schedtune_target_nrg.rdiv);
810
811                 pr_info("schedtune: max_pwr/2^%d: %4lu => norm_pwr: %5lu\n",
812                         idx, test_delta_pwr, test_norm_pwr);
813         }
814 }
815 #else
816 #define schedtune_test_nrg(delta_pwr)
817 #endif
818
819 /*
820  * Compute the min/max power consumption of a cluster and all its CPUs
821  */
822 static void
823 schedtune_add_cluster_nrg(
824                 struct sched_domain *sd,
825                 struct sched_group *sg,
826                 struct target_nrg *ste)
827 {
828         struct sched_domain *sd2;
829         struct sched_group *sg2;
830
831         struct cpumask *cluster_cpus;
832         char str[32];
833
834         unsigned long min_pwr;
835         unsigned long max_pwr;
836         int cpu;
837
838         /* Get Cluster energy using EM data for the first CPU */
839         cluster_cpus = sched_group_cpus(sg);
840         snprintf(str, 32, "CLUSTER[%*pbl]",
841                  cpumask_pr_args(cluster_cpus));
842
843         min_pwr = sg->sge->idle_states[sg->sge->nr_idle_states - 1].power;
844         max_pwr = sg->sge->cap_states[sg->sge->nr_cap_states - 1].power;
845         pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
846                 str, min_pwr, max_pwr);
847
848         /*
849          * Keep track of this cluster's energy in the computation of the
850          * overall system energy
851          */
852         ste->min_power += min_pwr;
853         ste->max_power += max_pwr;
854
855         /* Get CPU energy using EM data for each CPU in the group */
856         for_each_cpu(cpu, cluster_cpus) {
857                 /* Get a SD view for the specific CPU */
858                 for_each_domain(cpu, sd2) {
859                         /* Get the CPU group */
860                         sg2 = sd2->groups;
861                         min_pwr = sg2->sge->idle_states[sg2->sge->nr_idle_states - 1].power;
862                         max_pwr = sg2->sge->cap_states[sg2->sge->nr_cap_states - 1].power;
863
864                         ste->min_power += min_pwr;
865                         ste->max_power += max_pwr;
866
867                         snprintf(str, 32, "CPU[%d]", cpu);
868                         pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
869                                 str, min_pwr, max_pwr);
870
871                         /*
872                          * Assume we have EM data only at the CPU and
873                          * the upper CLUSTER level
874                          */
875                         BUG_ON(!cpumask_equal(
876                                 sched_group_cpus(sg),
877                                 sched_group_cpus(sd2->parent->groups)
878                                 ));
879                         break;
880                 }
881         }
882 }
883
884 /*
885  * Initialize the constants required to compute normalized energy.
886  * The values of these constants depends on the EM data for the specific
887  * target system and topology.
888  * Thus, this function is expected to be called by the code
889  * that bind the EM to the topology information.
890  */
891 static int
892 schedtune_init(void)
893 {
894         struct target_nrg *ste = &schedtune_target_nrg;
895         unsigned long delta_pwr = 0;
896         struct sched_domain *sd;
897         struct sched_group *sg;
898
899         pr_info("schedtune: init normalization constants...\n");
900         ste->max_power = 0;
901         ste->min_power = 0;
902
903         rcu_read_lock();
904
905         /*
906          * When EAS is in use, we always have a pointer to the highest SD
907          * which provides EM data.
908          */
909         sd = rcu_dereference(per_cpu(sd_ea, cpumask_first(cpu_online_mask)));
910         if (!sd) {
911                 pr_info("schedtune: no energy model data\n");
912                 goto nodata;
913         }
914
915         sg = sd->groups;
916         do {
917                 schedtune_add_cluster_nrg(sd, sg, ste);
918         } while (sg = sg->next, sg != sd->groups);
919
920         rcu_read_unlock();
921
922         pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
923                 "SYSTEM", ste->min_power, ste->max_power);
924
925         /* Compute normalization constants */
926         delta_pwr = ste->max_power - ste->min_power;
927         ste->rdiv = reciprocal_value(delta_pwr);
928         pr_info("schedtune: using normalization constants mul: %u sh1: %u sh2: %u\n",
929                 ste->rdiv.m, ste->rdiv.sh1, ste->rdiv.sh2);
930
931         schedtune_test_nrg(delta_pwr);
932
933 #ifdef CONFIG_CGROUP_SCHEDTUNE
934         schedtune_init_cgroups();
935 #else
936         pr_info("schedtune: configured to support global boosting only\n");
937 #endif
938
939         return 0;
940
941 nodata:
942         rcu_read_unlock();
943         return -EINVAL;
944 }
945 postcore_initcall(schedtune_init);