huangcm
2025-03-10 313d899ea76a728046c194ded35c2d20909cb707
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * drivers/cpufreq/sunxi_autohotplug.c
 *
 * Copyright (C) 2016-2020 Allwinnertech.
 * East Yang <yangdong@allwinnertech.com>
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
 
#include <linux/cpumask.h>
#include <linux/cpufreq.h>
#include <linux/module.h>
#include "autohotplug.h"
 
#include <trace/events/autohotplug.h>
 
#if defined(CONFIG_SCHED_HMP) || defined(CONFIG_SCHED_SMP_DCMP)
static unsigned int load_save_up = 60;
static unsigned int little_core_coop_min_freq = 600000;
 
#ifdef CONFIG_SCHED_HMP
static unsigned long cpu_boost_lasttime;
static unsigned int  load_try_boost = 90;
static unsigned int  load_save_big_up = 80;
static unsigned int  load_lastbig_stable_us = 1500000;
static unsigned int  load_boost_stable_us = 200000;
static unsigned int  cpu_boost_last_hold_us = 3000000;
int hmp_cluster0_is_big;
#endif
#endif
 
static int is_cpu_load_stable(unsigned int cpu, int stable_type)
{
   int ret;
 
   if (cpu >= CONFIG_NR_CPUS)
       return 0;
 
   if (stable_type == STABLE_DOWN)
       ret = time_after_eq(jiffies, cpu_up_lasttime +
               usecs_to_jiffies(load_down_stable_us));
   else if (stable_type == STABLE_UP)
       ret = time_after_eq(jiffies, cpu_up_lasttime +
               usecs_to_jiffies(load_up_stable_us));
#if defined(CONFIG_SCHED_HMP)
   else if (stable_type == STABLE_BOOST)
       ret = time_after_eq(jiffies, cpu_up_lasttime +
               usecs_to_jiffies(load_boost_stable_us));
   else if (stable_type == STABLE_LAST_BIG)
       ret = time_after_eq(jiffies, cpu_up_lasttime +
               usecs_to_jiffies(load_lastbig_stable_us));
#endif
   else
       return 0;
 
   trace_autohotplug_stable(ret);
   return ret;
}
 
#if defined(CONFIG_SCHED_HMP)
static int autohotplug_smart_try_up_hmp_normal(
                   struct autohotplug_loadinfo *load)
{
   unsigned int tmp = CONFIG_NR_CPUS;
   int big_nr, little_nr, ret;
 
   if ((load->max_load != INVALID_LOAD)
           && (load->max_load >= load_try_up)
           && is_cpu_load_stable(load->max_cpu, STABLE_UP)) {
       if (load->max_load >= load_try_boost) {
           if (get_bigs_under(load, load_save_up, NULL))
               return 0;
 
           get_cpus_online(load, &little_nr, &big_nr);
           if (big_nr != 0) {
               if (try_up_big())
                   ret = 1;
               else
                   ret = try_up_little();
 
               if (ret)
                   cpu_boost_lasttime = jiffies;
 
               return ret;
           }
 
           if (try_up_little())
               ret = 1;
           else
               ret = try_up_big();
 
           if (ret)
               cpu_boost_lasttime = jiffies;
 
           return ret;
       }
 
       if (get_cpus_under(load, load_save_up, &tmp))
           return 0;
 
       if (try_up_little())
           return 1;
 
       if (get_cpus_under(load, load_save_big_up, NULL))
           return 0;
 
       return try_up_big();
   }
 
   return 0;
}
 
static int autohotplug_smart_try_down_hmp_normal(
                   struct autohotplug_loadinfo *load)
{
   unsigned int to_down = CONFIG_NR_CPUS;
   unsigned int on_boost = CONFIG_NR_CPUS;
 
   if (get_cpus_under(load, load_try_down, &to_down, 0) >= 1) {
       if (time_before(jiffies, cpu_boost_lasttime +
               usecs_to_jiffies(cpu_boost_last_hold_us)))
           return 0;
       if (get_bigs_under(load, load_try_down, &to_down) >= 2)
           return do_cpu_down(to_down);
       else if (get_littles_under(load, load_try_down, &to_down) > 1)
           return do_cpu_down(to_down);
       else if (get_bigs_under(load, load_try_down, &to_down) == 1
               && !(get_bigs_above(load, load_try_boost,
                           &on_boost)
               && is_cpu_load_stable(on_boost, STABLE_BOOST))
               && (load->cpu_load[to_down] <=
                           load->big_min_load)
               && is_cpu_load_stable(to_down, STABLE_LAST_BIG))
           return do_cpu_down(to_down);
       else
           return 0;
   } else {
       return 0;
   }
}
 
static void autohotplug_smart_try_freq_limit_hmp_normal(void)
{
   int i, big_cpu = CONFIG_NR_CPUS;
   struct cpufreq_policy *policy = cpufreq_cpu_get(0);
 
   for_each_online_cpu(i) {
       if (is_cpu_big(i)) {
           big_cpu = i;
           break;
       }
   }
 
   if (big_cpu != CONFIG_NR_CPUS) {
       if (policy) {
           if (policy->min < little_core_coop_min_freq) {
               policy->user_policy.min =
                       little_core_coop_min_freq;
               cpufreq_update_policy(0);
           }
           cpufreq_cpu_put(policy);
       }
   } else {
       if (policy) {
           if (policy->min > policy->cpuinfo.min_freq) {
               policy->user_policy.min =
                       policy->cpuinfo.min_freq;
               cpufreq_update_policy(0);
           }
           cpufreq_cpu_put(policy);
       }
   }
}
#endif
 
#if defined(CONFIG_SCHED_HMP) || defined(CONFIG_SCHED_SMP_DCMP)
static int autohotplug_smart_try_up_hmp_simple(
                   struct autohotplug_loadinfo *load)
{
   int ret = 0;
   unsigned int first;
 
   if (!get_cpus_under(load, load_save_up, &first)
           && is_cpu_load_stable(load->max_cpu, STABLE_UP)) {
       if (try_up_big()) {
           ret = 1;
       } else {
           ret = try_up_little();
       }
   }
 
   return ret;
}
 
static int autohotplug_smart_try_down_hmp_simple(
                   struct autohotplug_loadinfo *load)
{
   int big_nr, little_nr;
   unsigned int first, to_down;
 
   get_cpus_online(load, &little_nr, &big_nr);
   if (get_cpus_under(load, load_try_down, &first) >= 2
               && is_cpu_load_stable(first, STABLE_DOWN)) {
       if (little_nr) {
           if (get_littles_under(load, load_try_down * 2,
                       &to_down))
               return do_cpu_down(to_down);
       } else {
           return do_cpu_down(first);
       }
   }
 
   return 0;
}
 
#if defined(CONFIG_SCHED_HMP)
static void autohotplug_smart_try_freq_limit_hmp_simple(void)
{
   struct cpufreq_policy *policy1 = NULL;
   struct cpufreq_policy *policy2 = NULL;
   int i, big_nr = 0, little_nr = 0, big = 0, little = 0;
 
   for_each_online_cpu(i) {
       if (is_cpu_little(i)) {
           little_nr++;
           little = i;
       } else {
           big = i;
           big_nr++;
       }
   }
 
   if (little_nr && big_nr) {
       policy1 = cpufreq_cpu_get(big);
       if (policy1 && policy1->min < little_core_coop_min_freq) {
           policy1->user_policy.min = little_core_coop_min_freq;
           cpufreq_update_policy(big);
       }
 
       policy2 = cpufreq_cpu_get(little);
       if (policy2 && policy2->min < little_core_coop_min_freq) {
           policy2->user_policy.min = little_core_coop_min_freq;
           cpufreq_update_policy(little);
       }
   } else if (big_nr) {
       policy1 = cpufreq_cpu_get(big);
       if (policy1 && policy1->min < little_core_coop_min_freq) {
           policy1->user_policy.min = little_core_coop_min_freq;
           cpufreq_update_policy(big);
       }
   } else if (little_nr) {
       policy2 = cpufreq_cpu_get(little);
       if (policy2 && policy2->min < little_core_coop_min_freq) {
           policy2->user_policy.min = little_core_coop_min_freq;
           cpufreq_update_policy(little);
       }
   }
 
   if (policy1)
       cpufreq_cpu_put(policy1);
   if (policy2)
       cpufreq_cpu_put(policy2);
}
#endif
#endif
 
#if !defined(CONFIG_SCHED_HMP) && !defined(CONFIG_SCHED_SMP_DCMP)
static int autohotplug_smart_try_up_smp_normal(
                   struct autohotplug_loadinfo *load)
{
   unsigned int first;
 
   if (!get_cpus_under(load, load_try_up, &first)
               && is_cpu_load_stable(load->max_cpu, STABLE_UP))
       return try_up_little();
 
   return 0;
}
 
static int autohotplug_smart_try_down_smp_normal(
                   struct autohotplug_loadinfo *load)
{
   unsigned int first;
 
   if (get_cpus_under(load, load_try_down, &first) >= 2
               && is_cpu_load_stable(first, STABLE_DOWN))
       return do_cpu_down(first);
 
   return 0;
}
#endif
 
static int autohotplug_smart_get_fast_slow_cpus(struct cpumask *fast,
                           struct cpumask *slow)
{
#if defined(CONFIG_SCHED_HMP)
   arch_get_fast_and_slow_cpus(fast, slow);
 
   if (cpumask_test_cpu(0, fast))
       hmp_cluster0_is_big = 1;
   else
       hmp_cluster0_is_big = 0;
 
   if (hmp_cluster0_is_big) {
       autohotplug_smart.try_up = autohotplug_smart_try_up_hmp_simple;
       autohotplug_smart.try_down =
               autohotplug_smart_try_down_hmp_simple;
       autohotplug_smart.try_freq_limit =
               autohotplug_smart_try_freq_limit_hmp_simple;
   } else {
       autohotplug_smart.try_up = autohotplug_smart_try_up_hmp_normal;
       autohotplug_smart.try_down =
               autohotplug_smart_try_down_hmp_normal;
       autohotplug_smart.try_freq_limit =
               autohotplug_smart_try_freq_limit_hmp_normal;
   }
#elif defined(CONFIG_SCHED_SMP_DCMP)
   if (strlen(CONFIG_CLUSTER0_CPU_MASK) &&
       strlen(CONFIG_CLUSTER1_CPU_MASK)) {
       if (cpulist_parse(CONFIG_CLUSTER0_CPU_MASK, fast)) {
           pr_err("Failed to parse cluster0 cpu mask!\n");
           return -1;
       }
 
       if (cpulist_parse(CONFIG_CLUSTER1_CPU_MASK, slow)) {
           pr_err("Failed to parse cluster1 cpu mask!\n");
           return -1;
       }
   }
   autohotplug_smart.try_up   = autohotplug_smart_try_up_hmp_simple;
   autohotplug_smart.try_down = autohotplug_smart_try_down_hmp_simple;
#else
   cpumask_copy(slow, cpu_possible_mask);
   autohotplug_smart.try_up   = autohotplug_smart_try_up_smp_normal;
   autohotplug_smart.try_down = autohotplug_smart_try_down_smp_normal;
#endif
 
   return 0;
}
 
static void autohotplug_smart_init_attr(void)
{
#if defined(CONFIG_SCHED_HMP) || defined(CONFIG_SCHED_SMP_DCMP)
   autohotplug_attr_add("little_min_freq", &little_core_coop_min_freq,
               0644, NULL, NULL);
   autohotplug_attr_add("save_all_up_load", &load_save_up,
               0644, NULL, NULL);
#ifdef CONFIG_SCHED_HMP
   autohotplug_attr_add("try_boost_load", &load_try_boost,
               0644, NULL, NULL);
   autohotplug_attr_add("save_big_up_load", &load_save_big_up,
               0644, NULL, NULL);
   autohotplug_attr_add("stable_boost_us", &load_boost_stable_us,
               0644, NULL, NULL);
   autohotplug_attr_add("stable_last_big_us", &load_lastbig_stable_us,
               0644, NULL, NULL);
   autohotplug_attr_add("hold_last_boost_us", &cpu_boost_last_hold_us,
               0644, NULL, NULL);
#endif
#endif
}
 
struct autohotplug_governor autohotplug_smart = {
   .init_attr              = autohotplug_smart_init_attr,
   .get_fast_and_slow_cpus = autohotplug_smart_get_fast_slow_cpus,
};
 
MODULE_DESCRIPTION("SUNXI autohotplug");
MODULE_LICENSE("GPL");