942a28d09523e70d8e7c9baf435012e273b746d2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / cpufreq / cpufreq_hotplug.c
1 /*
2 * drivers/cpufreq/cpufreq_hotplug.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/cpufreq.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/kobject.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/percpu-defs.h>
23 #include <linux/slab.h>
24 #include <linux/sysfs.h>
25 #include <linux/tick.h>
26 #include <linux/types.h>
27 #include <linux/cpu.h>
28 #include <linux/sched.h>
29 #include <linux/sched/rt.h>
30 #include <linux/kthread.h>
31 #include <linux/input.h> /* <-XXX */
32 #include <linux/slab.h> /* <-XXX */
33 #include "mach/mt_cpufreq.h" /* <-XXX */
34
35 #include "cpufreq_governor.h"
36
37 /* Hot-plug governor macros */
38 #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
39 #define DEF_FREQUENCY_UP_THRESHOLD (80)
40 #define DEF_SAMPLING_DOWN_FACTOR (1)
41 #define MAX_SAMPLING_DOWN_FACTOR (100000)
42 #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (15)
43 #define MIN_FREQUENCY_DOWN_DIFFERENTIAL (5) /* <-XXX */
44 #define MAX_FREQUENCY_DOWN_DIFFERENTIAL (20) /* <-XXX */
45 #define MICRO_FREQUENCY_UP_THRESHOLD (85)
46 #ifdef CONFIG_MTK_SDIOAUTOK_SUPPORT
47 #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (27000)
48 #else
49 #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (30000)
50 #endif
51 #define MIN_FREQUENCY_UP_THRESHOLD (21)
52 #define MAX_FREQUENCY_UP_THRESHOLD (100)
53
54 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
55 /*
56 * cpu hotplug - macro
57 */
58 #define DEF_CPU_DOWN_DIFFERENTIAL (10)
59 #define MICRO_CPU_DOWN_DIFFERENTIAL (10)
60 #define MIN_CPU_DOWN_DIFFERENTIAL (0)
61 #define MAX_CPU_DOWN_DIFFERENTIAL (30)
62
63 #define DEF_CPU_UP_THRESHOLD (90)
64 #define MICRO_CPU_UP_THRESHOLD (90)
65 #define MIN_CPU_UP_THRESHOLD (50)
66 #define MAX_CPU_UP_THRESHOLD (100)
67
68 #define DEF_CPU_UP_AVG_TIMES (10)
69 #define MIN_CPU_UP_AVG_TIMES (1)
70 #define MAX_CPU_UP_AVG_TIMES (20)
71
72 #define DEF_CPU_DOWN_AVG_TIMES (100)
73 #define MIN_CPU_DOWN_AVG_TIMES (20)
74 #define MAX_CPU_DOWN_AVG_TIMES (200)
75
76 #define DEF_CPU_INPUT_BOOST_ENABLE (1)
77 #define DEF_CPU_INPUT_BOOST_NUM (2)
78
79 #define DEF_CPU_RUSH_BOOST_ENABLE (1)
80
81 #define DEF_CPU_RUSH_THRESHOLD (98)
82 #define MICRO_CPU_RUSH_THRESHOLD (98)
83 #define MIN_CPU_RUSH_THRESHOLD (80)
84 #define MAX_CPU_RUSH_THRESHOLD (100)
85
86 #define DEF_CPU_RUSH_AVG_TIMES (5)
87 #define MIN_CPU_RUSH_AVG_TIMES (1)
88 #define MAX_CPU_RUSH_AVG_TIMES (10)
89
90 #define DEF_CPU_RUSH_TLP_TIMES (5)
91 #define MIN_CPU_RUSH_TLP_TIMES (1)
92 #define MAX_CPU_RUSH_TLP_TIMES (10)
93
94 /* #define DEBUG_LOG */
95
96 /*
97 * cpu hotplug - enum
98 */
99 typedef enum {
100 CPU_HOTPLUG_WORK_TYPE_NONE = 0,
101 CPU_HOTPLUG_WORK_TYPE_BASE,
102 CPU_HOTPLUG_WORK_TYPE_LIMIT,
103 CPU_HOTPLUG_WORK_TYPE_UP,
104 CPU_HOTPLUG_WORK_TYPE_DOWN,
105 CPU_HOTPLUG_WORK_TYPE_RUSH,
106 } cpu_hotplug_work_type_t;
107
108 /*
109 * cpu hotplug - global variable, function declaration
110 */
111 static DEFINE_MUTEX(hp_mutex);
112 DEFINE_MUTEX(hp_onoff_mutex);
113
114 int g_cpus_sum_load_current = 0; /* set global for information purpose */
115 #ifdef CONFIG_HOTPLUG_CPU
116
117 static long g_cpu_up_sum_load;
118 static int g_cpu_up_count;
119 static int g_cpu_up_load_index;
120 static long g_cpu_up_load_history[MAX_CPU_UP_AVG_TIMES] = { 0 };
121
122 static long g_cpu_down_sum_load;
123 static int g_cpu_down_count;
124 static int g_cpu_down_load_index;
125 static long g_cpu_down_load_history[MAX_CPU_DOWN_AVG_TIMES] = { 0 };
126
127 static cpu_hotplug_work_type_t g_trigger_hp_work;
128 static unsigned int g_next_hp_action;
129 static struct delayed_work hp_work;
130 struct workqueue_struct *hp_wq = NULL;
131
132 static int g_tlp_avg_current; /* set global for information purpose */
133 static int g_tlp_avg_sum;
134 static int g_tlp_avg_count;
135 static int g_tlp_avg_index;
136 static int g_tlp_avg_average; /* set global for information purpose */
137 static int g_tlp_avg_history[MAX_CPU_RUSH_TLP_TIMES] = { 0 };
138
139 static int g_tlp_iowait_av;
140
141 static int g_cpu_rush_count;
142
143 static void hp_reset_strategy_nolock(void);
144 static void hp_reset_strategy(void);
145
146 #else /* #ifdef CONFIG_HOTPLUG_CPU */
147
148 static void hp_reset_strategy_nolock(void)
149 {
150 };
151
152 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
153
154 /* dvfs - function declaration */
155 static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq);
156
157 #if defined(CONFIG_THERMAL_LIMIT_TEST)
158 extern unsigned int mt_cpufreq_thermal_test_limited_load(void);
159 #endif
160
161 static unsigned int dbs_ignore = 1;
162 static unsigned int dbs_thermal_limited;
163 static unsigned int dbs_thermal_limited_freq;
164
165 /* dvfs thermal limit */
166 void dbs_freq_thermal_limited(unsigned int limited, unsigned int freq)
167 {
168 dbs_thermal_limited = limited;
169 dbs_thermal_limited_freq = freq;
170 }
171 EXPORT_SYMBOL(dbs_freq_thermal_limited);
172
173
174 void (*cpufreq_freq_check) (enum mt_cpu_dvfs_id id) = NULL;
175 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
176
177 static DEFINE_PER_CPU(struct hp_cpu_dbs_info_s, hp_cpu_dbs_info);
178
179 static struct hp_ops hp_ops;
180
181 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_HOTPLUG
182 static struct cpufreq_governor cpufreq_gov_hotplug;
183 #endif
184
185 static unsigned int default_powersave_bias;
186
187 static void hotplug_powersave_bias_init_cpu(int cpu)
188 {
189 struct hp_cpu_dbs_info_s *dbs_info = &per_cpu(hp_cpu_dbs_info, cpu);
190
191 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
192 dbs_info->freq_lo = 0;
193 }
194
195 /*
196 * Not all CPUs want IO time to be accounted as busy; this depends on how
197 * efficient idling at a higher frequency/voltage is.
198 * Pavel Machek says this is not so for various generations of AMD and old
199 * Intel systems.
200 * Mike Chan (android.com) claims this is also not true for ARM.
201 * Because of this, whitelist specific known (series) of CPUs by default, and
202 * leave all others up to the user.
203 */
204 static int should_io_be_busy(void)
205 {
206 #if defined(CONFIG_X86)
207 /*
208 * For Intel, Core 2 (model 15) and later have an efficient idle.
209 */
210 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
211 boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model >= 15)
212 return 1;
213 #endif
214 return 1; /* io wait time should be subtracted from idle time // <-XXX */
215 }
216
217 /*
218 * Find right freq to be set now with powersave_bias on.
219 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
220 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
221 */
222 static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
223 unsigned int freq_next, unsigned int relation)
224 {
225 unsigned int freq_req, freq_reduc, freq_avg;
226 unsigned int freq_hi, freq_lo;
227 unsigned int index = 0;
228 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
229 struct hp_cpu_dbs_info_s *dbs_info = &per_cpu(hp_cpu_dbs_info,
230 policy->cpu);
231 struct dbs_data *dbs_data = policy->governor_data;
232 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
233
234 if (!dbs_info->freq_table) {
235 dbs_info->freq_lo = 0;
236 dbs_info->freq_lo_jiffies = 0;
237 return freq_next;
238 }
239
240 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next, relation, &index);
241 freq_req = dbs_info->freq_table[index].frequency;
242 freq_reduc = freq_req * hp_tuners->powersave_bias / 1000;
243 freq_avg = freq_req - freq_reduc;
244
245 /* Find freq bounds for freq_avg in freq_table */
246 index = 0;
247 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
248 CPUFREQ_RELATION_H, &index);
249 freq_lo = dbs_info->freq_table[index].frequency;
250 index = 0;
251 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
252 CPUFREQ_RELATION_L, &index);
253 freq_hi = dbs_info->freq_table[index].frequency;
254
255 /* Find out how long we have to be in hi and lo freqs */
256 if (freq_hi == freq_lo) {
257 dbs_info->freq_lo = 0;
258 dbs_info->freq_lo_jiffies = 0;
259 return freq_lo;
260 }
261 jiffies_total = usecs_to_jiffies(hp_tuners->sampling_rate);
262 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
263 jiffies_hi += ((freq_hi - freq_lo) / 2);
264 jiffies_hi /= (freq_hi - freq_lo);
265 jiffies_lo = jiffies_total - jiffies_hi;
266 dbs_info->freq_lo = freq_lo;
267 dbs_info->freq_lo_jiffies = jiffies_lo;
268 dbs_info->freq_hi_jiffies = jiffies_hi;
269 return freq_hi;
270 }
271
272 static void hotplug_powersave_bias_init(void)
273 {
274 int i;
275 for_each_online_cpu(i) {
276 hotplug_powersave_bias_init_cpu(i);
277 }
278 }
279
280 static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
281 {
282 struct dbs_data *dbs_data = p->governor_data;
283 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
284
285 if (hp_tuners->powersave_bias)
286 freq = hp_ops.powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
287 else if (p->cur == p->max) {
288 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
289 if (dbs_ignore == 0) {
290 if ((dbs_thermal_limited == 1) && (freq > dbs_thermal_limited_freq)) {
291 freq = dbs_thermal_limited_freq;
292 pr_debug("[dbs_freq_increase] thermal limit freq = %d\n", freq);
293 }
294
295 dbs_ignore = 1;
296 } else
297 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
298 return;
299 }
300
301 __cpufreq_driver_target(p, freq, hp_tuners->powersave_bias ?
302 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
303 }
304
305 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
306 /*
307 * cpu hotplug - function definition
308 */
309 int hp_get_dynamic_cpu_hotplug_enable(void)
310 {
311 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
312 struct hp_dbs_tuners *hp_tuners;
313
314 if (!dbs_data)
315 return 0;
316 hp_tuners = dbs_data->tuners;
317 if (!hp_tuners)
318 return 0;
319
320 return !(hp_tuners->is_cpu_hotplug_disable);
321 }
322 EXPORT_SYMBOL(hp_get_dynamic_cpu_hotplug_enable);
323
324 void hp_set_dynamic_cpu_hotplug_enable(int enable)
325 {
326 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
327 struct hp_dbs_tuners *hp_tuners;
328
329 if (!dbs_data)
330 return;
331 hp_tuners = dbs_data->tuners;
332 if (!hp_tuners)
333 return;
334
335 if (enable > 1 || enable < 0)
336 return;
337
338 mutex_lock(&hp_mutex);
339
340 if (hp_tuners->is_cpu_hotplug_disable && enable)
341 hp_reset_strategy_nolock();
342
343 hp_tuners->is_cpu_hotplug_disable = !enable;
344 mutex_unlock(&hp_mutex);
345 }
346 EXPORT_SYMBOL(hp_set_dynamic_cpu_hotplug_enable);
347
348 void hp_limited_cpu_num(int num)
349 {
350 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
351 struct hp_dbs_tuners *hp_tuners;
352
353 if (!dbs_data)
354 return;
355 hp_tuners = dbs_data->tuners;
356 if (!hp_tuners)
357 return;
358
359 if (num > num_possible_cpus() || num < 1)
360 return;
361
362 mutex_lock(&hp_mutex);
363 hp_tuners->cpu_num_limit = num;
364 mutex_unlock(&hp_mutex);
365 }
366 EXPORT_SYMBOL(hp_limited_cpu_num);
367
368 void hp_based_cpu_num(int num)
369 {
370 unsigned int online_cpus_count;
371 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
372 struct hp_dbs_tuners *hp_tuners;
373
374 if (!dbs_data)
375 return;
376 hp_tuners = dbs_data->tuners;
377 if (!hp_tuners)
378 return;
379
380 if (num > num_possible_cpus() || num < 1)
381 return;
382
383 mutex_lock(&hp_mutex);
384
385 hp_tuners->cpu_num_base = num;
386 online_cpus_count = num_online_cpus();
387 #ifdef CONFIG_HOTPLUG_CPU
388
389 if (online_cpus_count < num && online_cpus_count < hp_tuners->cpu_num_limit) {
390 struct hp_cpu_dbs_info_s *dbs_info;
391 struct cpufreq_policy *policy;
392
393 dbs_info = &per_cpu(hp_cpu_dbs_info, 0); /* TODO: FIXME, cpu = 0 */
394 policy = dbs_info->cdbs.cur_policy;
395
396 dbs_freq_increase(policy, policy->max);
397 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_BASE;
398 /* schedule_delayed_work_on(0, &hp_work, 0); */
399 if (hp_wq == NULL)
400 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
401 else
402 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
403 }
404 #endif
405
406 mutex_unlock(&hp_mutex);
407 }
408 EXPORT_SYMBOL(hp_based_cpu_num);
409
410 int hp_get_cpu_rush_boost_enable(void)
411 {
412 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
413 struct hp_dbs_tuners *hp_tuners;
414
415 if (!dbs_data)
416 return 0;
417 hp_tuners = dbs_data->tuners;
418 if (!hp_tuners)
419 return 0;
420
421 return hp_tuners->cpu_rush_boost_enable;
422 }
423 EXPORT_SYMBOL(hp_get_cpu_rush_boost_enable);
424
425 void hp_set_cpu_rush_boost_enable(int enable)
426 {
427 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
428 struct hp_dbs_tuners *hp_tuners;
429
430 if (!dbs_data)
431 return;
432 hp_tuners = dbs_data->tuners;
433 if (!hp_tuners)
434 return;
435
436 if (enable > 1 || enable < 0)
437 return;
438
439 mutex_lock(&hp_mutex);
440 hp_tuners->cpu_rush_boost_enable = enable;
441 mutex_unlock(&hp_mutex);
442 }
443 EXPORT_SYMBOL(hp_set_cpu_rush_boost_enable);
444
445 #ifdef CONFIG_HOTPLUG_CPU
446
447 #ifdef CONFIG_MTK_SCHED_RQAVG_KS
448 extern void sched_get_nr_running_avg(int *avg, int *iowait_avg);
449 #else /* #ifdef CONFIG_MTK_SCHED_RQAVG_KS */
450 static void sched_get_nr_running_avg(int *avg, int *iowait_avg)
451 {
452 *avg = num_possible_cpus() * 100;
453 }
454 #endif /* #ifdef CONFIG_MTK_SCHED_RQAVG_KS */
455
456 static void hp_reset_strategy_nolock(void)
457 {
458 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
459 struct hp_dbs_tuners *hp_tuners;
460
461 if (!dbs_data)
462 return;
463 hp_tuners = dbs_data->tuners;
464 if (!hp_tuners)
465 return;
466
467 g_cpu_up_count = 0;
468 g_cpu_up_sum_load = 0;
469 g_cpu_up_load_index = 0;
470 g_cpu_up_load_history[hp_tuners->cpu_up_avg_times - 1] = 0;
471 /* memset(g_cpu_up_load_history, 0, sizeof(long) * MAX_CPU_UP_AVG_TIMES); */
472
473 g_cpu_down_count = 0;
474 g_cpu_down_sum_load = 0;
475 g_cpu_down_load_index = 0;
476 g_cpu_down_load_history[hp_tuners->cpu_down_avg_times - 1] = 0;
477 /* memset(g_cpu_down_load_history, 0, sizeof(long) * MAX_CPU_DOWN_AVG_TIMES); */
478
479 g_tlp_avg_sum = 0;
480 g_tlp_avg_count = 0;
481 g_tlp_avg_index = 0;
482 g_tlp_avg_history[hp_tuners->cpu_rush_tlp_times - 1] = 0;
483 g_cpu_rush_count = 0;
484
485 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_NONE;
486 }
487
488 static void hp_reset_strategy(void)
489 {
490 mutex_lock(&hp_mutex);
491
492 hp_reset_strategy_nolock();
493
494 mutex_unlock(&hp_mutex);
495 }
496
497 static void hp_work_handler(struct work_struct *work)
498 {
499 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
500 struct hp_dbs_tuners *hp_tuners;
501
502 if (!dbs_data)
503 return;
504 hp_tuners = dbs_data->tuners;
505 if (!hp_tuners)
506 return;
507
508 if (mutex_trylock(&hp_onoff_mutex)) {
509 if (!hp_tuners->is_cpu_hotplug_disable) {
510 unsigned int online_cpus_count = num_online_cpus();
511 unsigned int i;
512
513 pr_debug
514 ("[power/hotplug] hp_work_handler(%d)(%d)(%d)(%d)(%ld)(%ld)(%d)(%d) begin\n",
515 g_trigger_hp_work, g_tlp_avg_average, g_tlp_avg_current,
516 g_cpus_sum_load_current, g_cpu_up_sum_load, g_cpu_down_sum_load,
517 hp_tuners->cpu_num_base, hp_tuners->cpu_num_limit);
518
519 switch (g_trigger_hp_work) {
520 case CPU_HOTPLUG_WORK_TYPE_RUSH:
521 for (i = online_cpus_count;
522 i < min(g_next_hp_action, hp_tuners->cpu_num_limit); ++i)
523 cpu_up(i);
524
525 break;
526
527 case CPU_HOTPLUG_WORK_TYPE_BASE:
528 for (i = online_cpus_count;
529 i < min(hp_tuners->cpu_num_base, hp_tuners->cpu_num_limit);
530 ++i)
531 cpu_up(i);
532
533 break;
534
535 case CPU_HOTPLUG_WORK_TYPE_LIMIT:
536 for (i = online_cpus_count - 1; i >= hp_tuners->cpu_num_limit; --i)
537 cpu_down(i);
538
539 break;
540
541 case CPU_HOTPLUG_WORK_TYPE_UP:
542 for (i = online_cpus_count; i < g_next_hp_action; ++i)
543 cpu_up(i);
544
545 break;
546
547 case CPU_HOTPLUG_WORK_TYPE_DOWN:
548 for (i = online_cpus_count - 1; i >= g_next_hp_action; --i)
549 cpu_down(i);
550
551 break;
552
553 default:
554 for (i = online_cpus_count;
555 i < min(hp_tuners->cpu_input_boost_num,
556 hp_tuners->cpu_num_limit); ++i)
557 cpu_up(i);
558
559 /* pr_debug("[power/hotplug] cpu input boost\n"); */
560 break;
561 }
562
563 hp_reset_strategy();
564 dbs_ignore = 0; /* force trigger frequency scaling */
565
566 pr_debug("[power/hotplug] hp_work_handler end\n");
567
568 /*
569 if (g_next_hp_action) // turn on CPU
570 {
571 if (online_cpus_count < num_possible_cpus())
572 {
573 pr_debug("hp_work_handler: cpu_up(%d) kick off\n", online_cpus_count);
574 cpu_up(online_cpus_count);
575 hp_reset_strategy();
576 pr_debug("hp_work_handler: cpu_up(%d) completion\n", online_cpus_count);
577
578 dbs_ignore = 0; // force trigger frequency scaling
579 }
580 }
581 else // turn off CPU
582 {
583 if (online_cpus_count > 1)
584 {
585 pr_debug("hp_work_handler: cpu_down(%d) kick off\n", (online_cpus_count - 1));
586 cpu_down((online_cpus_count - 1));
587 hp_reset_strategy();
588 pr_debug("hp_work_handler: cpu_down(%d) completion\n", (online_cpus_count - 1));
589
590 dbs_ignore = 0; // force trigger frequency scaling
591 }
592 }
593 */
594 }
595
596 mutex_unlock(&hp_onoff_mutex);
597 }
598 }
599 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
600 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
601
602 /*
603 * Every sampling_rate, we check, if current idle time is less than 20%
604 * (default), then we try to increase frequency. Every sampling_rate, we look
605 * for the lowest frequency which can sustain the load while keeping idle time
606 * over 30%. If such a frequency exist, we try to decrease to this frequency.
607 *
608 * Any frequency increase takes it to the maximum frequency. Frequency reduction
609 * happens at minimum steps of 5% (default) of current frequency
610 */
611 static void hp_check_cpu(int cpu, unsigned int load_freq)
612 {
613 struct hp_cpu_dbs_info_s *dbs_info = &per_cpu(hp_cpu_dbs_info, cpu);
614 struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
615 struct dbs_data *dbs_data = policy->governor_data;
616 struct hp_dbs_tuners *hp_tuners;
617
618 if (!dbs_data)
619 return;
620 hp_tuners = dbs_data->tuners;
621 if (!hp_tuners)
622 return;
623
624 dbs_info->freq_lo = 0;
625
626 /* pr_emerg("***** cpu: %d, load_freq: %u, smp_processor_id: %d *****\n", cpu, load_freq, smp_processor_id()); */
627
628 /* Check for frequency increase */
629 if (load_freq > hp_tuners->up_threshold * policy->cur) {
630 /* If switching to max speed, apply sampling_down_factor */
631 if (policy->cur < policy->max)
632 dbs_info->rate_mult = hp_tuners->sampling_down_factor;
633 dbs_freq_increase(policy, policy->max);
634 goto hp_check; /* <-XXX */
635 }
636
637 /* Check for frequency decrease */
638 /* if we cannot reduce the frequency anymore, break out early */
639 if (policy->cur == policy->min)
640 goto hp_check; /* <-XXX */
641
642 /*
643 * The optimal frequency is the frequency that is the lowest that can
644 * support the current CPU usage without triggering the up policy. To be
645 * safe, we focus 10 points under the threshold.
646 */
647 if (load_freq < hp_tuners->adj_up_threshold * policy->cur) {
648 unsigned int freq_next;
649 freq_next = load_freq / hp_tuners->adj_up_threshold;
650
651 /* No longer fully busy, reset rate_mult */
652 dbs_info->rate_mult = 1;
653
654 if (freq_next < policy->min)
655 freq_next = policy->min;
656
657 if (!hp_tuners->powersave_bias) {
658 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
659 } else {
660 freq_next = hp_ops.powersave_bias_target(policy, freq_next,
661 CPUFREQ_RELATION_L);
662 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
663 }
664 }
665 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
666 hp_check:{
667 #ifdef CONFIG_HOTPLUG_CPU
668 long cpus_sum_load_last_up = 0;
669 long cpus_sum_load_last_down = 0;
670 unsigned int online_cpus_count;
671
672 int v_tlp_avg_last = 0;
673 #endif
674
675 /* If Hot Plug policy disable, return directly */
676 if (hp_tuners->is_cpu_hotplug_disable)
677 return;
678
679 #ifdef CONFIG_HOTPLUG_CPU
680
681 if (g_trigger_hp_work != CPU_HOTPLUG_WORK_TYPE_NONE)
682 return;
683
684 mutex_lock(&hp_mutex);
685
686 online_cpus_count = num_online_cpus();
687
688 sched_get_nr_running_avg(&g_tlp_avg_current, &g_tlp_iowait_av);
689
690 v_tlp_avg_last = g_tlp_avg_history[g_tlp_avg_index];
691 g_tlp_avg_history[g_tlp_avg_index] = g_tlp_avg_current;
692 g_tlp_avg_sum += g_tlp_avg_current;
693
694 g_tlp_avg_index =
695 (g_tlp_avg_index + 1 ==
696 hp_tuners->cpu_rush_tlp_times) ? 0 : g_tlp_avg_index + 1;
697 g_tlp_avg_count++;
698
699 if (g_tlp_avg_count >= hp_tuners->cpu_rush_tlp_times) {
700 if (g_tlp_avg_sum > v_tlp_avg_last)
701 g_tlp_avg_sum -= v_tlp_avg_last;
702 else
703 g_tlp_avg_sum = 0;
704 }
705
706 g_tlp_avg_average = g_tlp_avg_sum / hp_tuners->cpu_rush_tlp_times;
707
708 if (hp_tuners->cpu_rush_boost_enable) {
709 /* pr_debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@ tlp: %d @@@@@@@@@@@@@@@@@@@@@@@@@@@\n", g_tlp_avg_average); */
710
711 if (g_cpus_sum_load_current >
712 hp_tuners->cpu_rush_threshold * online_cpus_count)
713 ++g_cpu_rush_count;
714 else
715 g_cpu_rush_count = 0;
716
717 if ((g_cpu_rush_count >= hp_tuners->cpu_rush_avg_times) &&
718 (online_cpus_count * 100 < g_tlp_avg_average) &&
719 (online_cpus_count < hp_tuners->cpu_num_limit) &&
720 (online_cpus_count < num_possible_cpus())) {
721 dbs_freq_increase(policy, policy->max);
722 pr_debug("dbs_check_cpu: turn on CPU\n");
723 g_next_hp_action =
724 g_tlp_avg_average / 100 + (g_tlp_avg_average % 100 ? 1 : 0);
725
726 if (g_next_hp_action > num_possible_cpus())
727 g_next_hp_action = num_possible_cpus();
728
729 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_RUSH;
730 /* schedule_delayed_work_on(0, &hp_work, 0); */
731 if (hp_wq == NULL)
732 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
733 else
734 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
735
736 goto hp_check_end;
737 }
738 }
739
740 if (online_cpus_count < hp_tuners->cpu_num_base
741 && online_cpus_count < hp_tuners->cpu_num_limit) {
742 dbs_freq_increase(policy, policy->max);
743 pr_debug("dbs_check_cpu: turn on CPU\n");
744 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_BASE;
745 /* schedule_delayed_work_on(0, &hp_work, 0); */
746 if (hp_wq == NULL)
747 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
748 else
749 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
750
751 goto hp_check_end;
752 }
753
754 if (online_cpus_count > hp_tuners->cpu_num_limit) {
755 dbs_freq_increase(policy, policy->max);
756 pr_debug("dbs_check_cpu: turn off CPU\n");
757 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_LIMIT;
758 /* schedule_delayed_work_on(0, &hp_work, 0); */
759 if (hp_wq == NULL)
760 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
761 else
762 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
763
764 goto hp_check_end;
765 }
766
767 /* Check CPU loading to power up slave CPU */
768 if (online_cpus_count < num_possible_cpus()) {
769 cpus_sum_load_last_up = g_cpu_up_load_history[g_cpu_up_load_index];
770 g_cpu_up_load_history[g_cpu_up_load_index] = g_cpus_sum_load_current;
771 g_cpu_up_sum_load += g_cpus_sum_load_current;
772
773 g_cpu_up_count++;
774 g_cpu_up_load_index =
775 (g_cpu_up_load_index + 1 ==
776 hp_tuners->cpu_up_avg_times) ? 0 : g_cpu_up_load_index + 1;
777
778 if (g_cpu_up_count >= hp_tuners->cpu_up_avg_times) {
779 if (g_cpu_up_sum_load > cpus_sum_load_last_up)
780 g_cpu_up_sum_load -= cpus_sum_load_last_up;
781 else
782 g_cpu_up_sum_load = 0;
783
784 /* g_cpu_up_sum_load /= hp_tuners->cpu_up_avg_times; */
785 if (g_cpu_up_sum_load >
786 (hp_tuners->cpu_up_threshold * online_cpus_count *
787 hp_tuners->cpu_up_avg_times)) {
788 if (online_cpus_count < hp_tuners->cpu_num_limit) {
789 #ifdef DEBUG_LOG
790 pr_debug("dbs_check_cpu: g_cpu_up_sum_load = %d\n",
791 g_cpu_up_sum_load);
792 #endif
793 dbs_freq_increase(policy, policy->max);
794 pr_debug("dbs_check_cpu: turn on CPU\n");
795 g_next_hp_action = online_cpus_count + 1;
796 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_UP;
797 /* schedule_delayed_work_on(0, &hp_work, 0); */
798 if (hp_wq == NULL)
799 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
800 else
801 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
802
803 goto hp_check_end;
804 }
805 }
806 }
807 #ifdef DEBUG_LOG
808 pr_debug("dbs_check_cpu: g_cpu_up_count = %d, g_cpu_up_sum_load = %d\n",
809 g_cpu_up_count, g_cpu_up_sum_load);
810 pr_debug("dbs_check_cpu: cpu_up_threshold = %d\n",
811 (hp_tuners->cpu_up_threshold * online_cpus_count));
812 #endif
813
814 }
815
816 /* Check CPU loading to power down slave CPU */
817 if (online_cpus_count > 1) {
818 cpus_sum_load_last_down = g_cpu_down_load_history[g_cpu_down_load_index];
819 g_cpu_down_load_history[g_cpu_down_load_index] = g_cpus_sum_load_current;
820 g_cpu_down_sum_load += g_cpus_sum_load_current;
821
822 g_cpu_down_count++;
823 g_cpu_down_load_index =
824 (g_cpu_down_load_index + 1 ==
825 hp_tuners->cpu_down_avg_times) ? 0 : g_cpu_down_load_index + 1;
826
827 if (g_cpu_down_count >= hp_tuners->cpu_down_avg_times) {
828 long cpu_down_threshold;
829
830 if (g_cpu_down_sum_load > cpus_sum_load_last_down)
831 g_cpu_down_sum_load -= cpus_sum_load_last_down;
832 else
833 g_cpu_down_sum_load = 0;
834
835 g_next_hp_action = online_cpus_count;
836 cpu_down_threshold =
837 ((hp_tuners->cpu_up_threshold -
838 hp_tuners->cpu_down_differential) *
839 hp_tuners->cpu_down_avg_times);
840
841 while ((g_cpu_down_sum_load <
842 cpu_down_threshold * (g_next_hp_action - 1)) &&
843 /* (g_next_hp_action > tlp_cpu_num) && */
844 (g_next_hp_action > hp_tuners->cpu_num_base))
845 --g_next_hp_action;
846
847 /* pr_debug("### g_next_hp_action: %d, tlp_cpu_num: %d, g_cpu_down_sum_load / hp_tuners->cpu_down_avg_times: %d ###\n", g_next_hp_action, tlp_cpu_num, g_cpu_down_sum_load / hp_tuners->cpu_down_avg_times); */
848 if (g_next_hp_action < online_cpus_count) {
849 #ifdef DEBUG_LOG
850 pr_debug("dbs_check_cpu: g_cpu_down_sum_load = %d\n",
851 g_cpu_down_sum_load);
852 #endif
853 dbs_freq_increase(policy, policy->max);
854 pr_debug("dbs_check_cpu: turn off CPU\n");
855 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_DOWN;
856 /* schedule_delayed_work_on(0, &hp_work, 0); */
857 if (hp_wq == NULL)
858 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
859 else
860 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
861 }
862 }
863 #ifdef DEBUG_LOG
864 pr_debug("dbs_check_cpu: g_cpu_down_count = %d, g_cpu_down_sum_load = %d\n",
865 g_cpu_down_count, g_cpu_down_sum_load);
866 pr_debug("dbs_check_cpu: cpu_down_threshold = %d\n",
867 ((hp_tuners->cpu_up_threshold -
868 hp_tuners->cpu_down_differential) * (online_cpus_count - 1)));
869 #endif
870 }
871
872 hp_check_end:
873 mutex_unlock(&hp_mutex);
874
875 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
876 }
877 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
878 }
879
880 static void hp_dbs_timer(struct work_struct *work)
881 {
882 struct hp_cpu_dbs_info_s *dbs_info =
883 container_of(work, struct hp_cpu_dbs_info_s, cdbs.work.work);
884 unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
885 struct hp_cpu_dbs_info_s *core_dbs_info = &per_cpu(hp_cpu_dbs_info,
886 cpu);
887 struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data;
888 struct hp_dbs_tuners *hp_tuners;
889
890 int delay = 0, sample_type = core_dbs_info->sample_type;
891 bool modify_all = true;
892
893 if (!dbs_data)
894 return;
895 hp_tuners = dbs_data->tuners;
896 if (!hp_tuners)
897 return;
898
899 mutex_lock(&core_dbs_info->cdbs.timer_mutex);
900 if (!need_load_eval(&core_dbs_info->cdbs, hp_tuners->sampling_rate)) {
901 modify_all = false;
902 goto max_delay;
903 }
904
905 /* Common NORMAL_SAMPLE setup */
906 core_dbs_info->sample_type = HP_NORMAL_SAMPLE;
907 if (sample_type == HP_SUB_SAMPLE) {
908 delay = core_dbs_info->freq_lo_jiffies;
909 __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy,
910 core_dbs_info->freq_lo, CPUFREQ_RELATION_H);
911 } else {
912 dbs_check_cpu(dbs_data, cpu);
913 if (core_dbs_info->freq_lo) {
914 /* Setup timer for SUB_SAMPLE */
915 core_dbs_info->sample_type = HP_SUB_SAMPLE;
916 delay = core_dbs_info->freq_hi_jiffies;
917 }
918 }
919
920 max_delay:
921 if (!delay)
922 delay = delay_for_sampling_rate(hp_tuners->sampling_rate
923 * core_dbs_info->rate_mult);
924
925 gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, delay, modify_all);
926 mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
927
928 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
929 /* for downgrade */ /* TODO: FIXME */
930 if (cpufreq_freq_check)
931 cpufreq_freq_check(0); /* TODO: FIXME, fix cpuid = 0 */
932 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
933 }
934
935 /************************** sysfs interface ************************/
936 static struct common_dbs_data hp_dbs_cdata;
937
938 /**
939 * update_sampling_rate - update sampling rate effective immediately if needed.
940 * @new_rate: new sampling rate
941 *
942 * If new rate is smaller than the old, simply updating
943 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
944 * original sampling_rate was 1 second and the requested new sampling rate is 10
945 * ms because the user needs immediate reaction from hotplug governor, but not
946 * sure if higher frequency will be required or not, then, the governor may
947 * change the sampling rate too late; up to 1 second later. Thus, if we are
948 * reducing the sampling rate, we need to make the new value effective
949 * immediately.
950 */
951 static void update_sampling_rate(struct dbs_data *dbs_data, unsigned int new_rate)
952 {
953 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
954
955 hp_tuners->sampling_rate = new_rate = max(new_rate, dbs_data->min_sampling_rate);
956
957 {
958 struct cpufreq_policy *policy;
959 struct hp_cpu_dbs_info_s *dbs_info;
960 unsigned long next_sampling, appointed_at;
961
962 policy = cpufreq_cpu_get(0);
963 if (!policy)
964 return;
965 if (policy->governor != &cpufreq_gov_hotplug) {
966 cpufreq_cpu_put(policy);
967 return;
968 }
969 dbs_info = &per_cpu(hp_cpu_dbs_info, 0);
970 cpufreq_cpu_put(policy);
971
972 mutex_lock(&dbs_info->cdbs.timer_mutex);
973
974 if (!delayed_work_pending(&dbs_info->cdbs.work)) {
975 mutex_unlock(&dbs_info->cdbs.timer_mutex);
976 return;
977 }
978
979 next_sampling = jiffies + usecs_to_jiffies(new_rate);
980 appointed_at = dbs_info->cdbs.work.timer.expires;
981
982 if (time_before(next_sampling, appointed_at)) {
983
984 mutex_unlock(&dbs_info->cdbs.timer_mutex);
985 cancel_delayed_work_sync(&dbs_info->cdbs.work);
986 mutex_lock(&dbs_info->cdbs.timer_mutex);
987
988 gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy,
989 usecs_to_jiffies(new_rate), true);
990
991 }
992 mutex_unlock(&dbs_info->cdbs.timer_mutex);
993 }
994 }
995
996 void hp_enable_timer(int enable)
997 {
998 #if 1
999 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
1000 static unsigned int sampling_rate_backup = 0;
1001
1002 if (!dbs_data || dbs_data->cdata->governor != GOV_HOTPLUG || (enable && !sampling_rate_backup))
1003 return;
1004
1005 if (enable)
1006 update_sampling_rate(dbs_data, sampling_rate_backup);
1007 else {
1008 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1009
1010 sampling_rate_backup = hp_tuners->sampling_rate;
1011 update_sampling_rate(dbs_data, 30000 * 100);
1012 }
1013 #else
1014 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
1015 int cpu = 0;
1016 struct cpufreq_policy *policy;
1017 struct hp_dbs_tuners *hp_tuners;
1018 struct hp_cpu_dbs_info_s *dbs_info;
1019
1020 policy = cpufreq_cpu_get(cpu);
1021 if (!policy)
1022 continue;
1023 if (policy->governor != &cpufreq_gov_hotplug) {
1024 cpufreq_cpu_put(policy);
1025 continue;
1026 }
1027 dbs_info = &per_cpu(hp_cpu_dbs_info, cpu);
1028 cpufreq_cpu_put(policy);
1029
1030 if (enable) {
1031 hp_tuners = dbs_data->tuners;
1032 mutex_lock(&dbs_info->cdbs.timer_mutex);
1033 gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, usecs_to_jiffies(hp_tuners->sampling_rate), true);
1034 mutex_unlock(&dbs_info->cdbs.timer_mutex);
1035 } else
1036 cancel_delayed_work_sync(&dbs_info->cdbs.work);
1037 }
1038 #endif
1039 }
1040 EXPORT_SYMBOL(hp_enable_timer);
1041
1042 static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf, size_t count)
1043 {
1044 unsigned int input;
1045 int ret;
1046 ret = sscanf(buf, "%u", &input);
1047 if (ret != 1)
1048 return -EINVAL;
1049
1050 update_sampling_rate(dbs_data, input);
1051 return count;
1052 }
1053
1054 static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf, size_t count)
1055 {
1056 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1057 unsigned int input;
1058 int ret;
1059 unsigned int j;
1060
1061 ret = sscanf(buf, "%u", &input);
1062 if (ret != 1)
1063 return -EINVAL;
1064 hp_tuners->io_is_busy = !!input;
1065
1066 /* we need to re-evaluate prev_cpu_idle */
1067 for_each_online_cpu(j) {
1068 struct hp_cpu_dbs_info_s *dbs_info = &per_cpu(hp_cpu_dbs_info,
1069 j);
1070 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
1071 &dbs_info->cdbs.prev_cpu_wall,
1072 hp_tuners->io_is_busy);
1073 }
1074 return count;
1075 }
1076
1077 static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf, size_t count)
1078 {
1079 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1080 unsigned int input;
1081 int ret;
1082 ret = sscanf(buf, "%u", &input);
1083
1084 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || input < MIN_FREQUENCY_UP_THRESHOLD)
1085 return -EINVAL;
1086
1087 /* Calculate the new adj_up_threshold */
1088 hp_tuners->adj_up_threshold += input;
1089 hp_tuners->adj_up_threshold -= hp_tuners->up_threshold;
1090
1091 hp_tuners->up_threshold = input;
1092 return count;
1093 }
1094
1095 static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data, const char *buf, size_t count)
1096 {
1097 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1098 unsigned int input, j;
1099 int ret;
1100 ret = sscanf(buf, "%u", &input);
1101
1102 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
1103 return -EINVAL;
1104 hp_tuners->sampling_down_factor = input;
1105
1106 /* Reset down sampling multiplier in case it was active */
1107 for_each_online_cpu(j) {
1108 struct hp_cpu_dbs_info_s *dbs_info = &per_cpu(hp_cpu_dbs_info,
1109 j);
1110 dbs_info->rate_mult = 1;
1111 }
1112 return count;
1113 }
1114
1115 static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data, const char *buf, size_t count)
1116 {
1117 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1118 unsigned int input;
1119 int ret;
1120
1121 unsigned int j;
1122
1123 ret = sscanf(buf, "%u", &input);
1124 if (ret != 1)
1125 return -EINVAL;
1126
1127 if (input > 1)
1128 input = 1;
1129
1130 if (input == hp_tuners->ignore_nice_load) /* nothing to do */
1131 return count;
1132
1133 hp_tuners->ignore_nice_load = input;
1134
1135 /* we need to re-evaluate prev_cpu_idle */
1136 for_each_online_cpu(j) {
1137 struct hp_cpu_dbs_info_s *dbs_info;
1138 dbs_info = &per_cpu(hp_cpu_dbs_info, j);
1139 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
1140 &dbs_info->cdbs.prev_cpu_wall,
1141 hp_tuners->io_is_busy);
1142 if (hp_tuners->ignore_nice_load)
1143 dbs_info->cdbs.prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
1144
1145 }
1146 return count;
1147 }
1148
1149 static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf, size_t count)
1150 {
1151 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1152 unsigned int input;
1153 int ret;
1154 ret = sscanf(buf, "%u", &input);
1155
1156 if (ret != 1)
1157 return -EINVAL;
1158
1159 if (input > 1000)
1160 input = 1000;
1161
1162 hp_tuners->powersave_bias = input;
1163 hotplug_powersave_bias_init();
1164 return count;
1165 }
1166
1167 show_store_one(hp, sampling_rate);
1168 show_store_one(hp, io_is_busy);
1169 show_store_one(hp, up_threshold);
1170 show_store_one(hp, sampling_down_factor);
1171 show_store_one(hp, ignore_nice_load);
1172 show_store_one(hp, powersave_bias);
1173 declare_show_sampling_rate_min(hp);
1174
1175 gov_sys_pol_attr_rw(sampling_rate);
1176 gov_sys_pol_attr_rw(io_is_busy);
1177 gov_sys_pol_attr_rw(up_threshold);
1178 gov_sys_pol_attr_rw(sampling_down_factor);
1179 gov_sys_pol_attr_rw(ignore_nice_load);
1180 gov_sys_pol_attr_rw(powersave_bias);
1181 gov_sys_pol_attr_ro(sampling_rate_min);
1182
1183 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1184 static ssize_t store_down_differential(struct dbs_data *dbs_data, const char *buf, size_t count)
1185 {
1186 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1187 unsigned int input;
1188 int ret;
1189 ret = sscanf(buf, "%u", &input);
1190
1191 if (ret != 1
1192 || input > MAX_FREQUENCY_DOWN_DIFFERENTIAL || input < MIN_FREQUENCY_DOWN_DIFFERENTIAL)
1193 return -EINVAL;
1194
1195 hp_tuners->down_differential = input;
1196
1197 return count;
1198 }
1199
1200 /*
1201 * cpu hotplug - function definition of sysfs
1202 */
1203 static ssize_t store_cpu_up_threshold(struct dbs_data *dbs_data, const char *buf, size_t count)
1204 {
1205 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1206 unsigned int input;
1207 int ret;
1208 ret = sscanf(buf, "%u", &input);
1209
1210 if (ret != 1 || input > MAX_CPU_UP_THRESHOLD || input < MIN_CPU_UP_THRESHOLD)
1211 return -EINVAL;
1212
1213 mutex_lock(&hp_mutex);
1214 hp_tuners->cpu_up_threshold = input;
1215 hp_reset_strategy_nolock();
1216 mutex_unlock(&hp_mutex);
1217
1218 return count;
1219 }
1220
1221 static ssize_t store_cpu_down_differential(struct dbs_data *dbs_data, const char *buf, size_t count)
1222 {
1223 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1224 unsigned int input;
1225 int ret;
1226 ret = sscanf(buf, "%u", &input);
1227
1228 if (ret != 1 || input > MAX_CPU_DOWN_DIFFERENTIAL || input < MIN_CPU_DOWN_DIFFERENTIAL)
1229 return -EINVAL;
1230
1231 mutex_lock(&hp_mutex);
1232 hp_tuners->cpu_down_differential = input;
1233 hp_reset_strategy_nolock();
1234 mutex_unlock(&hp_mutex);
1235
1236 return count;
1237 }
1238
1239 static ssize_t store_cpu_up_avg_times(struct dbs_data *dbs_data, const char *buf, size_t count)
1240 {
1241 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1242 unsigned int input;
1243 int ret;
1244 ret = sscanf(buf, "%u", &input);
1245
1246 if (ret != 1 || input > MAX_CPU_UP_AVG_TIMES || input < MIN_CPU_UP_AVG_TIMES)
1247 return -EINVAL;
1248
1249 mutex_lock(&hp_mutex);
1250 hp_tuners->cpu_up_avg_times = input;
1251 hp_reset_strategy_nolock();
1252 mutex_unlock(&hp_mutex);
1253
1254 return count;
1255 }
1256
1257 static ssize_t store_cpu_down_avg_times(struct dbs_data *dbs_data, const char *buf, size_t count)
1258 {
1259 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1260 unsigned int input;
1261 int ret;
1262 ret = sscanf(buf, "%u", &input);
1263
1264 if (ret != 1 || input > MAX_CPU_DOWN_AVG_TIMES || input < MIN_CPU_DOWN_AVG_TIMES)
1265 return -EINVAL;
1266
1267 mutex_lock(&hp_mutex);
1268 hp_tuners->cpu_down_avg_times = input;
1269 hp_reset_strategy_nolock();
1270 mutex_unlock(&hp_mutex);
1271
1272 return count;
1273 }
1274
1275 static ssize_t store_cpu_num_limit(struct dbs_data *dbs_data, const char *buf, size_t count)
1276 {
1277 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1278 unsigned int input;
1279 int ret;
1280 ret = sscanf(buf, "%u", &input);
1281
1282 if (ret != 1 || input > num_possible_cpus()
1283 || input < 1)
1284 return -EINVAL;
1285
1286 mutex_lock(&hp_mutex);
1287 hp_tuners->cpu_num_limit = input;
1288 mutex_unlock(&hp_mutex);
1289
1290 return count;
1291 }
1292
1293 static ssize_t store_cpu_num_base(struct dbs_data *dbs_data, const char *buf, size_t count)
1294 {
1295 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1296 unsigned int input;
1297 unsigned int online_cpus_count;
1298 int ret;
1299 ret = sscanf(buf, "%u", &input);
1300
1301 if (ret != 1 || input > num_possible_cpus()
1302 || input < 1)
1303 return -EINVAL;
1304
1305 mutex_lock(&hp_mutex);
1306
1307 hp_tuners->cpu_num_base = input;
1308 online_cpus_count = num_online_cpus();
1309 #ifdef CONFIG_HOTPLUG_CPU
1310
1311 if (online_cpus_count < input && online_cpus_count < hp_tuners->cpu_num_limit) {
1312 struct cpufreq_policy *policy = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy; /* TODO: FIXME, cpu = 0 */
1313
1314 dbs_freq_increase(policy, policy->max);
1315 g_trigger_hp_work = CPU_HOTPLUG_WORK_TYPE_BASE;
1316 /* schedule_delayed_work_on(0, &hp_work, 0); */
1317 if (hp_wq == NULL)
1318 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
1319 else
1320 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
1321 }
1322 #endif
1323
1324 mutex_unlock(&hp_mutex);
1325
1326 return count;
1327 }
1328
1329 static ssize_t store_is_cpu_hotplug_disable(struct dbs_data *dbs_data, const char *buf,
1330 size_t count)
1331 {
1332 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1333 unsigned int input;
1334 int ret;
1335 ret = sscanf(buf, "%u", &input);
1336
1337 if (ret != 1 || input > 1 || input < 0)
1338 return -EINVAL;
1339
1340 mutex_lock(&hp_mutex);
1341
1342 if (hp_tuners->is_cpu_hotplug_disable && !input)
1343 hp_reset_strategy_nolock();
1344
1345 hp_tuners->is_cpu_hotplug_disable = input;
1346 mutex_unlock(&hp_mutex);
1347
1348 return count;
1349 }
1350
1351 static ssize_t store_cpu_input_boost_enable(struct dbs_data *dbs_data, const char *buf,
1352 size_t count)
1353 {
1354 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1355 unsigned int input;
1356 int ret;
1357 ret = sscanf(buf, "%u", &input);
1358
1359 if (ret != 1 || input > 1 || input < 0)
1360 return -EINVAL;
1361
1362 mutex_lock(&hp_mutex);
1363 hp_tuners->cpu_input_boost_enable = input;
1364 mutex_unlock(&hp_mutex);
1365
1366 return count;
1367 }
1368
1369 static ssize_t store_cpu_input_boost_num(struct dbs_data *dbs_data, const char *buf, size_t count)
1370 {
1371 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1372 unsigned int input;
1373 int ret;
1374 ret = sscanf(buf, "%u", &input);
1375
1376 if (ret != 1 || input > num_possible_cpus()
1377 || input < 2)
1378 return -EINVAL;
1379
1380 mutex_lock(&hp_mutex);
1381 hp_tuners->cpu_input_boost_num = input;
1382 mutex_unlock(&hp_mutex);
1383
1384 return count;
1385 }
1386
1387 static ssize_t store_cpu_rush_boost_enable(struct dbs_data *dbs_data, const char *buf, size_t count)
1388 {
1389 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1390 unsigned int input;
1391 int ret;
1392 ret = sscanf(buf, "%u", &input);
1393
1394 if (ret != 1 || input > 1 || input < 0)
1395 return -EINVAL;
1396
1397 mutex_lock(&hp_mutex);
1398 hp_tuners->cpu_rush_boost_enable = input;
1399 mutex_unlock(&hp_mutex);
1400
1401 return count;
1402 }
1403
1404 static ssize_t store_cpu_rush_boost_num(struct dbs_data *dbs_data, const char *buf, size_t count)
1405 {
1406 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1407 unsigned int input;
1408 int ret;
1409 ret = sscanf(buf, "%u", &input);
1410
1411 if (ret != 1 || input > num_possible_cpus()
1412 || input < 2)
1413 return -EINVAL;
1414
1415 mutex_lock(&hp_mutex);
1416 hp_tuners->cpu_rush_boost_num = input;
1417 mutex_unlock(&hp_mutex);
1418
1419 return count;
1420 }
1421
1422 static ssize_t store_cpu_rush_threshold(struct dbs_data *dbs_data, const char *buf, size_t count)
1423 {
1424 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1425 unsigned int input;
1426 int ret;
1427 ret = sscanf(buf, "%u", &input);
1428
1429 if (ret != 1 || input > MAX_CPU_RUSH_THRESHOLD || input < MIN_CPU_RUSH_THRESHOLD)
1430 return -EINVAL;
1431
1432 mutex_lock(&hp_mutex);
1433 hp_tuners->cpu_rush_threshold = input;
1434 /* hp_reset_strategy_nolock(); //no need */
1435 mutex_unlock(&hp_mutex);
1436
1437 return count;
1438 }
1439
1440 static ssize_t store_cpu_rush_tlp_times(struct dbs_data *dbs_data, const char *buf, size_t count)
1441 {
1442 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1443 unsigned int input;
1444 int ret;
1445 ret = sscanf(buf, "%u", &input);
1446
1447 if (ret != 1 || input > MAX_CPU_RUSH_TLP_TIMES || input < MIN_CPU_RUSH_TLP_TIMES)
1448 return -EINVAL;
1449
1450 mutex_lock(&hp_mutex);
1451 hp_tuners->cpu_rush_tlp_times = input;
1452 hp_reset_strategy_nolock();
1453 mutex_unlock(&hp_mutex);
1454
1455 return count;
1456 }
1457
1458 static ssize_t store_cpu_rush_avg_times(struct dbs_data *dbs_data, const char *buf, size_t count)
1459 {
1460 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1461 unsigned int input;
1462 int ret;
1463 ret = sscanf(buf, "%u", &input);
1464
1465 if (ret != 1 || input > MAX_CPU_RUSH_AVG_TIMES || input < MIN_CPU_RUSH_AVG_TIMES)
1466 return -EINVAL;
1467
1468 mutex_lock(&hp_mutex);
1469 hp_tuners->cpu_rush_avg_times = input;
1470 hp_reset_strategy_nolock();
1471 mutex_unlock(&hp_mutex);
1472
1473 return count;
1474 }
1475
1476 show_store_one(hp, down_differential);
1477 show_store_one(hp, cpu_up_threshold);
1478 show_store_one(hp, cpu_down_differential);
1479 show_store_one(hp, cpu_up_avg_times);
1480 show_store_one(hp, cpu_down_avg_times);
1481 show_store_one(hp, cpu_num_limit);
1482 show_store_one(hp, cpu_num_base);
1483 show_store_one(hp, is_cpu_hotplug_disable);
1484 show_store_one(hp, cpu_input_boost_enable);
1485 show_store_one(hp, cpu_input_boost_num);
1486 show_store_one(hp, cpu_rush_boost_enable);
1487 show_store_one(hp, cpu_rush_boost_num);
1488 show_store_one(hp, cpu_rush_threshold);
1489 show_store_one(hp, cpu_rush_tlp_times);
1490 show_store_one(hp, cpu_rush_avg_times);
1491
1492 gov_sys_pol_attr_rw(down_differential);
1493 gov_sys_pol_attr_rw(cpu_up_threshold);
1494 gov_sys_pol_attr_rw(cpu_down_differential);
1495 gov_sys_pol_attr_rw(cpu_up_avg_times);
1496 gov_sys_pol_attr_rw(cpu_down_avg_times);
1497 gov_sys_pol_attr_rw(cpu_num_limit);
1498 gov_sys_pol_attr_rw(cpu_num_base);
1499 gov_sys_pol_attr_rw(is_cpu_hotplug_disable);
1500 gov_sys_pol_attr_rw(cpu_input_boost_enable);
1501 gov_sys_pol_attr_rw(cpu_input_boost_num);
1502 gov_sys_pol_attr_rw(cpu_rush_boost_enable);
1503 gov_sys_pol_attr_rw(cpu_rush_boost_num);
1504 gov_sys_pol_attr_rw(cpu_rush_threshold);
1505 gov_sys_pol_attr_rw(cpu_rush_tlp_times);
1506 gov_sys_pol_attr_rw(cpu_rush_avg_times);
1507 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1508
1509 static struct attribute *dbs_attributes_gov_sys[] = {
1510 &sampling_rate_min_gov_sys.attr,
1511 &sampling_rate_gov_sys.attr,
1512 &up_threshold_gov_sys.attr,
1513 &sampling_down_factor_gov_sys.attr,
1514 &ignore_nice_load_gov_sys.attr,
1515 &powersave_bias_gov_sys.attr,
1516 &io_is_busy_gov_sys.attr,
1517 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1518 &down_differential_gov_sys.attr,
1519 &cpu_up_threshold_gov_sys.attr,
1520 &cpu_down_differential_gov_sys.attr,
1521 &cpu_up_avg_times_gov_sys.attr,
1522 &cpu_down_avg_times_gov_sys.attr,
1523 &cpu_num_limit_gov_sys.attr,
1524 &cpu_num_base_gov_sys.attr,
1525 &is_cpu_hotplug_disable_gov_sys.attr,
1526 &cpu_input_boost_enable_gov_sys.attr,
1527 &cpu_input_boost_num_gov_sys.attr,
1528 &cpu_rush_boost_enable_gov_sys.attr,
1529 &cpu_rush_boost_num_gov_sys.attr,
1530 &cpu_rush_threshold_gov_sys.attr,
1531 &cpu_rush_tlp_times_gov_sys.attr,
1532 &cpu_rush_avg_times_gov_sys.attr,
1533 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1534 NULL
1535 };
1536
1537 static struct attribute_group hp_attr_group_gov_sys = {
1538 .attrs = dbs_attributes_gov_sys,
1539 .name = "hotplug",
1540 };
1541
1542 static struct attribute *dbs_attributes_gov_pol[] = {
1543 &sampling_rate_min_gov_pol.attr,
1544 &sampling_rate_gov_pol.attr,
1545 &up_threshold_gov_pol.attr,
1546 &sampling_down_factor_gov_pol.attr,
1547 &ignore_nice_load_gov_pol.attr,
1548 &powersave_bias_gov_pol.attr,
1549 &io_is_busy_gov_pol.attr,
1550 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1551 &down_differential_gov_pol.attr,
1552 &cpu_up_threshold_gov_pol.attr,
1553 &cpu_down_differential_gov_pol.attr,
1554 &cpu_up_avg_times_gov_pol.attr,
1555 &cpu_down_avg_times_gov_pol.attr,
1556 &cpu_num_limit_gov_pol.attr,
1557 &cpu_num_base_gov_pol.attr,
1558 &is_cpu_hotplug_disable_gov_pol.attr,
1559 &cpu_input_boost_enable_gov_pol.attr,
1560 &cpu_input_boost_num_gov_pol.attr,
1561 &cpu_rush_boost_enable_gov_pol.attr,
1562 &cpu_rush_boost_num_gov_pol.attr,
1563 &cpu_rush_threshold_gov_pol.attr,
1564 &cpu_rush_tlp_times_gov_pol.attr,
1565 &cpu_rush_avg_times_gov_pol.attr,
1566 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1567 NULL
1568 };
1569
1570 static struct attribute_group hp_attr_group_gov_pol = {
1571 .attrs = dbs_attributes_gov_pol,
1572 .name = "hotplug",
1573 };
1574
1575 /************************** sysfs end ************************/
1576
1577 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1578
1579 #ifdef CONFIG_HOTPLUG_CPU
1580
1581 static struct task_struct *freq_up_task;
1582
1583 static int touch_freq_up_task(void *data)
1584 {
1585 struct cpufreq_policy *policy;
1586
1587 while (1) {
1588 policy = cpufreq_cpu_get(0);
1589 dbs_freq_increase(policy, policy->max);
1590 cpufreq_cpu_put(policy);
1591 /* mt_cpufreq_set_ramp_down_count_const(0, 100); */
1592 pr_debug("@%s():%d\n", __func__, __LINE__);
1593
1594 set_current_state(TASK_INTERRUPTIBLE);
1595 schedule();
1596
1597 if (kthread_should_stop())
1598 break;
1599 }
1600
1601 return 0;
1602 }
1603
1604 static void dbs_input_event(struct input_handle *handle, unsigned int type,
1605 unsigned int code, int value)
1606 {
1607 /* int i; */
1608
1609 /* if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) || */
1610 /* (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) { */
1611 /* nothing to do */
1612 /* return; */
1613 /* } */
1614
1615 /* for_each_online_cpu(i) { */
1616 /* queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i)); */
1617 /* } */
1618 /* pr_debug("$$$ in_interrupt(): %d, in_irq(): %d, type: %d, code: %d, value: %d $$$\n", in_interrupt(), in_irq(), type, code, value); */
1619
1620 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
1621 struct hp_dbs_tuners *hp_tuners;
1622
1623 if (!dbs_data)
1624 return;
1625 hp_tuners = dbs_data->tuners;
1626 if (!hp_tuners)
1627 return;
1628
1629 if ((type == EV_KEY) && (code == BTN_TOUCH) && (value == 1)
1630 && (dbs_data->cdata->governor == GOV_HOTPLUG && hp_tuners->cpu_input_boost_enable)) {
1631 /* if (!in_interrupt()) */
1632 /* { */
1633 unsigned int online_cpus_count = num_online_cpus();
1634
1635 pr_debug("@%s():%d, online_cpus_count = %d, cpu_input_boost_num = %d\n", __func__, __LINE__, online_cpus_count, hp_tuners->cpu_input_boost_num);
1636
1637 if (online_cpus_count < hp_tuners->cpu_input_boost_num && online_cpus_count < hp_tuners->cpu_num_limit) {
1638 /* schedule_delayed_work_on(0, &hp_work, 0); */
1639 if (hp_wq == NULL)
1640 pr_emerg("[power/hotplug] %s():%d, impossible\n", __func__, __LINE__);
1641 else
1642 queue_delayed_work_on(0, hp_wq, &hp_work, 0);
1643 }
1644
1645 if (online_cpus_count <= hp_tuners->cpu_input_boost_num && online_cpus_count <= hp_tuners->cpu_num_limit)
1646 wake_up_process(freq_up_task);
1647
1648 /* } */
1649 }
1650 }
1651
1652 static int dbs_input_connect(struct input_handler *handler,
1653 struct input_dev *dev, const struct input_device_id *id)
1654 {
1655 struct input_handle *handle;
1656 int error;
1657
1658 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1659
1660 if (!handle)
1661 return -ENOMEM;
1662
1663 handle->dev = dev;
1664 handle->handler = handler;
1665 handle->name = "cpufreq";
1666
1667 error = input_register_handle(handle);
1668
1669 if (error)
1670 goto err2;
1671
1672 error = input_open_device(handle);
1673
1674 if (error)
1675 goto err1;
1676
1677 return 0;
1678 err1:
1679 input_unregister_handle(handle);
1680 err2:
1681 kfree(handle);
1682 return error;
1683 }
1684
1685 static void dbs_input_disconnect(struct input_handle *handle)
1686 {
1687 input_close_device(handle);
1688 input_unregister_handle(handle);
1689 kfree(handle);
1690 }
1691
1692 static const struct input_device_id dbs_ids[] = {
1693 {.driver_info = 1},
1694 {},
1695 };
1696
1697 static struct input_handler dbs_input_handler = {
1698 .event = dbs_input_event,
1699 .connect = dbs_input_connect,
1700 .disconnect = dbs_input_disconnect,
1701 .name = "cpufreq_ond",
1702 .id_table = dbs_ids,
1703 };
1704 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
1705
1706 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1707
1708 static int hp_init(struct dbs_data *dbs_data)
1709 {
1710 struct hp_dbs_tuners *tuners;
1711 u64 idle_time;
1712 int cpu;
1713
1714 tuners = kzalloc(sizeof(struct hp_dbs_tuners), GFP_KERNEL);
1715 if (!tuners) {
1716 pr_err("%s: kzalloc failed\n", __func__);
1717 return -ENOMEM;
1718 }
1719
1720 cpu = get_cpu();
1721 idle_time = get_cpu_idle_time_us(cpu, NULL);
1722 put_cpu();
1723 if (idle_time != -1ULL) {
1724 /* Idle micro accounting is supported. Use finer thresholds */
1725 tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
1726 tuners->adj_up_threshold = MICRO_FREQUENCY_UP_THRESHOLD -
1727 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
1728 tuners->down_differential = MICRO_FREQUENCY_DOWN_DIFFERENTIAL; /* <-XXX */
1729 tuners->cpu_up_threshold = MICRO_CPU_UP_THRESHOLD; /* <-XXX */
1730 tuners->cpu_down_differential = MICRO_CPU_DOWN_DIFFERENTIAL; /* <-XXX */
1731 /*
1732 * In nohz/micro accounting case we set the minimum frequency
1733 * not depending on HZ, but fixed (very low). The deferred
1734 * timer might skip some samples if idle/sleeping as needed.
1735 */
1736 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
1737
1738 /* cpu rush boost */
1739 tuners->cpu_rush_threshold = MICRO_CPU_RUSH_THRESHOLD; /* <-XXX */
1740 } else {
1741 tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
1742 tuners->adj_up_threshold = DEF_FREQUENCY_UP_THRESHOLD -
1743 DEF_FREQUENCY_DOWN_DIFFERENTIAL;
1744 tuners->down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL; /* <-XXX */
1745 tuners->cpu_up_threshold = DEF_CPU_UP_THRESHOLD; /* <-XXX */
1746 tuners->cpu_down_differential = DEF_CPU_DOWN_DIFFERENTIAL; /* <-XXX */
1747
1748 /* For correct statistics, we need 10 ticks for each measure */
1749 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
1750
1751 /* cpu rush boost */
1752 tuners->cpu_rush_threshold = DEF_CPU_RUSH_THRESHOLD; /* <-XXX */
1753 }
1754
1755 tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
1756 tuners->ignore_nice_load = 0;
1757 tuners->powersave_bias = default_powersave_bias;
1758 tuners->io_is_busy = should_io_be_busy();
1759 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1760 tuners->cpu_up_avg_times = DEF_CPU_UP_AVG_TIMES;
1761 tuners->cpu_down_avg_times = DEF_CPU_DOWN_AVG_TIMES;
1762 tuners->cpu_num_limit = num_possible_cpus();
1763 tuners->cpu_num_base = 1;
1764 tuners->is_cpu_hotplug_disable = (tuners->cpu_num_limit > 1) ? 0 : 1;
1765 tuners->cpu_input_boost_enable = DEF_CPU_INPUT_BOOST_ENABLE;
1766 tuners->cpu_input_boost_num = DEF_CPU_INPUT_BOOST_NUM;
1767 tuners->cpu_rush_boost_enable = DEF_CPU_RUSH_BOOST_ENABLE;
1768 tuners->cpu_rush_boost_num = num_possible_cpus();
1769 tuners->cpu_rush_tlp_times = DEF_CPU_RUSH_TLP_TIMES;
1770 tuners->cpu_rush_avg_times = DEF_CPU_RUSH_AVG_TIMES;
1771
1772 #ifdef CONFIG_HOTPLUG_CPU
1773 INIT_DEFERRABLE_WORK(&hp_work, hp_work_handler);
1774 hp_wq = alloc_workqueue("hp_work_handler", WQ_HIGHPRI, 0);
1775 g_next_hp_action = num_online_cpus();
1776 #endif
1777
1778 #ifdef DEBUG_LOG
1779 pr_debug("cpufreq_gov_dbs_init: min_sampling_rate = %d\n", dbs_data->min_sampling_rate);
1780 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.up_threshold = %d\n", tuners->up_threshold);
1781 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.down_differential = %d\n",
1782 tuners->down_differential);
1783 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_up_threshold = %d\n",
1784 tuners->cpu_up_threshold);
1785 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_down_differential = %d\n",
1786 tuners->cpu_down_differential);
1787 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_up_avg_times = %d\n",
1788 tuners->cpu_up_avg_times);
1789 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_down_avg_times = %d\n",
1790 tuners->cpu_down_avg_times);
1791 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_num_limit = %d\n",
1792 tuners->cpu_num_limit);
1793 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_num_base = %d\n", tuners->cpu_num_base);
1794 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.is_cpu_hotplug_disable = %d\n",
1795 tuners->is_cpu_hotplug_disable);
1796 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_input_boost_enable = %d\n",
1797 tuners->cpu_input_boost_enable);
1798 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_input_boost_num = %d\n",
1799 tuners->cpu_input_boost_num);
1800 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_rush_boost_enable = %d\n",
1801 tuners->cpu_rush_boost_enable);
1802 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_rush_boost_num = %d\n",
1803 tuners->cpu_rush_boost_num);
1804 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_rush_threshold = %d\n",
1805 tuners->cpu_rush_threshold);
1806 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_rush_tlp_times = %d\n",
1807 tuners->cpu_rush_tlp_times);
1808 pr_debug("cpufreq_gov_dbs_init: dbs_tuners_ins.cpu_rush_avg_times = %d\n",
1809 tuners->cpu_rush_avg_times);
1810 #endif
1811 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1812
1813 dbs_data->tuners = tuners;
1814 mutex_init(&dbs_data->mutex);
1815 return 0;
1816 }
1817
1818 static void hp_exit(struct dbs_data *dbs_data)
1819 {
1820 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1821 #ifdef CONFIG_HOTPLUG_CPU
1822 cancel_delayed_work_sync(&hp_work);
1823 if (hp_wq)
1824 destroy_workqueue(hp_wq);
1825 #endif
1826 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1827 kfree(dbs_data->tuners);
1828 }
1829
1830 define_get_cpu_dbs_routines(hp_cpu_dbs_info);
1831
1832 static struct hp_ops hp_ops = {
1833 .powersave_bias_init_cpu = hotplug_powersave_bias_init_cpu,
1834 .powersave_bias_target = generic_powersave_bias_target,
1835 .freq_increase = dbs_freq_increase,
1836 .input_handler = &dbs_input_handler,
1837 };
1838
1839 static struct common_dbs_data hp_dbs_cdata = {
1840 .governor = GOV_HOTPLUG,
1841 .attr_group_gov_sys = &hp_attr_group_gov_sys,
1842 .attr_group_gov_pol = &hp_attr_group_gov_pol,
1843 .get_cpu_cdbs = get_cpu_cdbs,
1844 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
1845 .gov_dbs_timer = hp_dbs_timer,
1846 .gov_check_cpu = hp_check_cpu,
1847 .gov_ops = &hp_ops,
1848 .init = hp_init,
1849 .exit = hp_exit,
1850 };
1851
1852 static void hp_set_powersave_bias(unsigned int powersave_bias)
1853 {
1854 struct cpufreq_policy *policy;
1855 struct dbs_data *dbs_data;
1856 struct hp_dbs_tuners *hp_tuners;
1857 unsigned int cpu;
1858 cpumask_t done;
1859
1860 default_powersave_bias = powersave_bias;
1861 cpumask_clear(&done);
1862
1863 get_online_cpus();
1864 for_each_online_cpu(cpu) {
1865 if (cpumask_test_cpu(cpu, &done))
1866 continue;
1867
1868 policy = per_cpu(hp_cpu_dbs_info, cpu).cdbs.cur_policy;
1869 if (!policy)
1870 continue;
1871
1872 cpumask_or(&done, &done, policy->cpus);
1873
1874 if (policy->governor != &cpufreq_gov_hotplug)
1875 continue;
1876
1877 dbs_data = policy->governor_data;
1878 hp_tuners = dbs_data->tuners;
1879 hp_tuners->powersave_bias = default_powersave_bias;
1880 }
1881 put_online_cpus();
1882 }
1883
1884 void hp_register_powersave_bias_handler(unsigned int (*f)
1885 (struct cpufreq_policy *, unsigned int, unsigned int),
1886 unsigned int powersave_bias)
1887 {
1888 hp_ops.powersave_bias_target = f;
1889 hp_set_powersave_bias(powersave_bias);
1890 }
1891 EXPORT_SYMBOL_GPL(hp_register_powersave_bias_handler);
1892
1893 void hp_unregister_powersave_bias_handler(void)
1894 {
1895 hp_ops.powersave_bias_target = generic_powersave_bias_target;
1896 hp_set_powersave_bias(0);
1897 }
1898 EXPORT_SYMBOL_GPL(hp_unregister_powersave_bias_handler);
1899
1900 static int hp_cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
1901 {
1902 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1903 struct dbs_data *dbs_data;
1904 int rc = 0;
1905
1906 if (have_governor_per_policy())
1907 dbs_data = policy->governor_data;
1908 else
1909 dbs_data = hp_dbs_cdata.gdbs_data;
1910
1911 /* pr_emerg("***** policy->cpu: %d, event: %u, smp_processor_id: %d, have_governor_per_policy: %d *****\n", policy->cpu, event, smp_processor_id(), have_governor_per_policy()); */
1912 switch (event) {
1913 case CPUFREQ_GOV_START:
1914 #ifdef DEBUG_LOG
1915 {
1916 struct hp_dbs_tuners *hp_tuners = dbs_data->tuners;
1917
1918 BUG_ON(NULL == dbs_data);
1919 BUG_ON(NULL == dbs_data->tuners);
1920
1921 pr_debug("cpufreq_governor_dbs: min_sampling_rate = %d\n",
1922 dbs_data->min_sampling_rate);
1923 pr_debug("cpufreq_governor_dbs: dbs_tuners_ins.sampling_rate = %d\n",
1924 hp_tuners->sampling_rate);
1925 pr_debug("cpufreq_governor_dbs: dbs_tuners_ins.io_is_busy = %d\n",
1926 hp_tuners->io_is_busy);
1927 }
1928 #endif
1929 #ifdef CONFIG_HOTPLUG_CPU
1930 if (0) /* (!policy->cpu) // <-XXX */
1931 rc = input_register_handler(&dbs_input_handler);
1932 #endif
1933 break;
1934
1935 case CPUFREQ_GOV_STOP:
1936 #ifdef CONFIG_HOTPLUG_CPU
1937 if (0) /* (!policy->cpu) // <-XXX */
1938 input_unregister_handler(&dbs_input_handler);
1939
1940 #endif
1941 break;
1942 }
1943
1944 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1945 return cpufreq_governor_dbs(policy, &hp_dbs_cdata, event);
1946 }
1947
1948 /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1949 #if 0
1950 int cpufreq_gov_dbs_get_sum_load(void)
1951 {
1952 return g_cpus_sum_load_current;
1953 }
1954 #endif
1955 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1956
1957 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_HOTPLUG
1958 static
1959 #endif
1960 struct cpufreq_governor cpufreq_gov_hotplug = {
1961 .name = "hotplug",
1962 .governor = hp_cpufreq_governor_dbs,
1963 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
1964 .owner = THIS_MODULE,
1965 };
1966
1967 #ifdef CONFIG_MTK_SDIOAUTOK_SUPPORT
1968 void cpufreq_min_sampling_rate_change(unsigned int sample_rate)
1969 {
1970 struct dbs_data *dbs_data = per_cpu(hp_cpu_dbs_info, 0).cdbs.cur_policy->governor_data; /* TODO: FIXME, cpu = 0 */
1971
1972 if (!dbs_data)
1973 return;
1974
1975 dbs_data->min_sampling_rate = sample_rate;
1976 update_sampling_rate(dbs_data, sample_rate);
1977 }
1978 EXPORT_SYMBOL(cpufreq_min_sampling_rate_change);
1979 #endif
1980
1981 static int __init cpufreq_gov_dbs_init(void)
1982 {
1983 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1984
1985 freq_up_task = kthread_create(touch_freq_up_task, NULL, "touch_freq_up_task");
1986
1987 if (IS_ERR(freq_up_task))
1988 return PTR_ERR(freq_up_task);
1989
1990 sched_setscheduler_nocheck(freq_up_task, SCHED_FIFO, &param);
1991 get_task_struct(freq_up_task);
1992
1993 return cpufreq_register_governor(&cpufreq_gov_hotplug);
1994 }
1995
1996 static void __exit cpufreq_gov_dbs_exit(void)
1997 {
1998 cpufreq_unregister_governor(&cpufreq_gov_hotplug);
1999
2000 kthread_stop(freq_up_task);
2001 put_task_struct(freq_up_task);
2002 }
2003
2004 MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
2005 MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
2006 MODULE_DESCRIPTION("'cpufreq_hotplug' - A dynamic cpufreq governor for "
2007 "Low Latency Frequency Transition capable processors");
2008 MODULE_LICENSE("GPL");
2009
2010 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_HOTPLUG
2011 fs_initcall(cpufreq_gov_dbs_init);
2012 #else
2013 module_init(cpufreq_gov_dbs_init);
2014 #endif
2015 module_exit(cpufreq_gov_dbs_exit);