1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
/*
* Copyright (c) 2016 Park Bumgyu, Samsung Electronics Co., Ltd <bumgyu.park@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Exynos ACME(A Cpufreq that Meets Every chipset) driver implementation
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/cpufreq.h>
#include <linux/pm_opp.h>
#include <soc/samsung/exynos-cpu_hotplug.h>
#include "exynos-acme.h"
/*********************************************************************
* SYSFS INTERFACES *
*********************************************************************/
/*
* Log2 of the number of scale size. The frequencies are scaled up or
* down as the multiple of this number.
*/
#define SCALE_SIZE 2
static int last_max_limit = -1;
static int sse_mode;
static ssize_t show_cpufreq_table(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct list_head *domains = get_domain_list();
struct exynos_cpufreq_domain *domain;
ssize_t count = 0;
int i, scale = 0;
list_for_each_entry_reverse(domain, domains, list) {
for (i = 0; i < domain->table_size; i++) {
unsigned int freq = domain->freq_table[i].frequency;
if (freq == CPUFREQ_ENTRY_INVALID)
continue;
count += snprintf(&buf[count], 10, "%d ",
freq >> (scale * SCALE_SIZE));
}
scale++;
}
count += snprintf(&buf[count - 1], 2, "\n");
return count - 1;
}
static ssize_t show_cpufreq_min_limit(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct list_head *domains = get_domain_list();
struct exynos_cpufreq_domain *domain;
unsigned int pm_qos_min;
int scale = -1;
list_for_each_entry_reverse(domain, domains, list) {
scale++;
/* get value of minimum PM QoS */
pm_qos_min = pm_qos_request(domain->pm_qos_min_class);
if (pm_qos_min > 0) {
pm_qos_min = min(pm_qos_min, domain->max_freq);
pm_qos_min = max(pm_qos_min, domain->min_freq);
/*
* To manage frequencies of all domains at once,
* scale down frequency as multiple of 4.
* ex) domain2 = freq
* domain1 = freq /4
* domain0 = freq /16
*/
pm_qos_min = pm_qos_min >> (scale * SCALE_SIZE);
return snprintf(buf, 10, "%u\n", pm_qos_min);
}
}
/*
* If there is no QoS at all domains, it returns minimum
* frequency of last domain
*/
return snprintf(buf, 10, "%u\n",
first_domain()->min_freq >> (scale * SCALE_SIZE));
}
static bool boosted;
static inline void control_boost(bool enable)
{
#ifdef CONFIG_SCHED_HMP
if (boosted && !enable) {
set_hmp_boost(HMP_BOOSTING_DISABLE);
boosted = false;
} else if (!boosted && enable) {
set_hmp_boost(HMP_BOOSTING_ENABLE);
boosted = true;
}
#endif
}
static ssize_t store_cpufreq_min_limit(struct kobject *kobj,
struct attribute *attr, const char *buf,
size_t count)
{
struct list_head *domains = get_domain_list();
struct exynos_cpufreq_domain *domain;
int input, scale = -1;
unsigned int freq;
unsigned int req_limit_freq;
bool set_max = false;
bool set_limit = false;
int index = 0;
int ret = 0;
struct cpumask mask;
if (sscanf(buf, "%8d", &input) < 1)
return -EINVAL;
if (!domains) {
pr_err("failed to get domains!\n");
return -ENXIO;
}
list_for_each_entry_reverse(domain, domains, list) {
struct exynos_ufc *ufc, *r_ufc = NULL, *r_ufc_32 = NULL;
struct cpufreq_policy *policy = NULL;
cpumask_and(&mask, &domain->cpus, cpu_online_mask);
if (!cpumask_weight(&mask))
continue;
policy = cpufreq_cpu_get_raw(cpumask_any(&mask));
if (!policy)
continue;
ufc = list_entry(&domain->ufc_list, struct exynos_ufc, list);
list_for_each_entry(ufc, &domain->ufc_list, list) {
if (ufc->info.ctrl_type == PM_QOS_MIN_LIMIT) {
if (ufc->info.exe_mode == AARCH64_MODE)
r_ufc = ufc;
else
r_ufc_32 = ufc;
}
}
scale++;
if (set_limit) {
req_limit_freq = min(req_limit_freq, domain->max_freq);
pm_qos_update_request(&domain->user_min_qos_req, req_limit_freq);
set_limit = false;
continue;
}
if (set_max) {
unsigned int qos = domain->max_freq;
if (domain->user_default_qos)
qos = domain->user_default_qos;
pm_qos_update_request(&domain->user_min_qos_req, qos);
continue;
}
/* Clear all constraint by cpufreq_min_limit */
if (input < 0) {
pm_qos_update_request(&domain->user_min_qos_req, 0);
control_boost(0);
continue;
}
/*
* User inputs scaled down frequency. To recover real
* frequency, scale up frequency as multiple of 4.
* ex) domain2 = freq
* domain1 = freq * 4
* domain0 = freq * 16
*/
freq = input << (scale * SCALE_SIZE);
if (freq < domain->min_freq) {
pm_qos_update_request(&domain->user_min_qos_req, 0);
continue;
}
if (r_ufc) {
if (sse_mode && r_ufc_32)
r_ufc = r_ufc_32;
ret = cpufreq_frequency_table_target(policy, domain->freq_table,
freq, CPUFREQ_RELATION_L, &index);
if (ret) {
pr_err("target frequency(%d) out of range\n", freq);
continue;
}
req_limit_freq = r_ufc->info.freq_table[index].limit_freq;
if (req_limit_freq)
set_limit = true;
}
freq = min(freq, domain->max_freq);
pm_qos_update_request(&domain->user_min_qos_req, freq);
control_boost(1);
set_max = true;
}
return count;
}
static ssize_t store_cpufreq_min_limit_wo_boost(struct kobject *kobj,
struct attribute *attr, const char *buf,
size_t count)
{
struct list_head *domains = get_domain_list();
struct exynos_cpufreq_domain *domain;
int input, scale = -1;
unsigned int freq;
unsigned int req_limit_freq;
bool set_max = false;
bool set_limit = false;
int index = 0;
int ret = 0;
struct cpumask mask;
if (sscanf(buf, "%8d", &input) < 1)
return -EINVAL;
if (!domains) {
pr_err("failed to get domains!\n");
return -ENXIO;
}
list_for_each_entry_reverse(domain, domains, list) {
struct exynos_ufc *ufc, *r_ufc = NULL, *r_ufc_32 = NULL;
struct cpufreq_policy *policy = NULL;
cpumask_and(&mask, &domain->cpus, cpu_online_mask);
if (!cpumask_weight(&mask))
continue;
policy = cpufreq_cpu_get_raw(cpumask_any(&mask));
if (!policy)
continue;
ufc = list_entry(&domain->ufc_list, struct exynos_ufc, list);
list_for_each_entry(ufc, &domain->ufc_list, list) {
if (ufc->info.ctrl_type == PM_QOS_MIN_WO_BOOST_LIMIT) {
if (ufc->info.exe_mode == AARCH64_MODE)
r_ufc = ufc;
else
r_ufc_32 = ufc;
}
}
scale++;
if (set_limit) {
req_limit_freq = min(req_limit_freq, domain->max_freq);
pm_qos_update_request(&domain->user_min_qos_req, req_limit_freq);
set_limit = false;
continue;
}
if (set_max) {
unsigned int qos = domain->max_freq;
if (domain->user_default_qos)
qos = domain->user_default_qos;
pm_qos_update_request(&domain->user_min_qos_wo_boost_req, qos);
continue;
}
/* Clear all constraint by cpufreq_min_limit */
if (input < 0) {
pm_qos_update_request(&domain->user_min_qos_wo_boost_req, 0);
continue;
}
/*
* User inputs scaled down frequency. To recover real
* frequency, scale up frequency as multiple of 4.
* ex) domain2 = freq
* domain1 = freq * 4
* domain0 = freq * 16
*/
freq = input << (scale * SCALE_SIZE);
if (freq < domain->min_freq) {
pm_qos_update_request(&domain->user_min_qos_wo_boost_req, 0);
continue;
}
if (r_ufc) {
if (sse_mode && r_ufc_32)
r_ufc = r_ufc_32;
ret = cpufreq_frequency_table_target(policy, domain->freq_table,
freq, CPUFREQ_RELATION_L, &index);
if (ret) {
pr_err("target frequency(%d) out of range\n", freq);
continue;
}
req_limit_freq = r_ufc->info.freq_table[index].limit_freq;
if (req_limit_freq)
set_limit = true;
}
freq = min(freq, domain->max_freq);
pm_qos_update_request(&domain->user_min_qos_wo_boost_req, freq);
set_max = true;
}
return count;
}
static ssize_t show_cpufreq_max_limit(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct list_head *domains = get_domain_list();
struct exynos_cpufreq_domain *domain;
unsigned int pm_qos_max;
int scale = -1;
if (!domains) {
pr_err("failed to get domains!\n");
return -ENXIO;
}
list_for_each_entry_reverse(domain, domains, list) {
scale++;
/* get value of minimum PM QoS */
pm_qos_max = pm_qos_request(domain->pm_qos_max_class);
if (pm_qos_max > 0) {
pm_qos_max = min(pm_qos_max, domain->max_freq);
pm_qos_max = max(pm_qos_max, domain->min_freq);
/*
* To manage frequencies of all domains at once,
* scale down frequency as multiple of 4.
* ex) domain2 = freq
* domain1 = freq /4
* domain0 = freq /16
*/
pm_qos_max = pm_qos_max >> (scale * SCALE_SIZE);
return snprintf(buf, 10, "%u\n", pm_qos_max);
}
}
/*
* If there is no QoS at all domains, it returns minimum
* frequency of last domain
*/
return snprintf(buf, 10, "%u\n",
first_domain()->min_freq >> (scale * SCALE_SIZE));
}
struct pm_qos_request cpu_online_max_qos_req;
static void enable_domain_cpus(struct exynos_cpufreq_domain *domain)
{
struct cpumask mask;
if (domain == first_domain())
return;
cpumask_or(&mask, cpu_online_mask, &domain->cpus);
pm_qos_update_request(&cpu_online_max_qos_req, cpumask_weight(&mask));
}
static void disable_domain_cpus(struct exynos_cpufreq_domain *domain)
{
struct cpumask mask;
if (domain == first_domain())
return;
cpumask_andnot(&mask, cpu_online_mask, &domain->cpus);
pm_qos_update_request(&cpu_online_max_qos_req, cpumask_weight(&mask));
}
static void cpufreq_max_limit_update(int input_freq)
{
struct list_head *domains = get_domain_list();
struct exynos_cpufreq_domain *domain;
int scale = -1;
unsigned int freq;
bool set_max = false;
unsigned int req_limit_freq;
bool set_limit = false;
int index = 0;
int ret = 0;
struct cpumask mask;
list_for_each_entry_reverse(domain, domains, list) {
struct exynos_ufc *ufc, *r_ufc = NULL, *r_ufc_32 = NULL;
struct cpufreq_policy *policy = NULL;
cpumask_and(&mask, &domain->cpus, cpu_online_mask);
if (cpumask_weight(&mask))
policy = cpufreq_cpu_get_raw(cpumask_any(&mask));
ufc = list_entry(&domain->ufc_list, struct exynos_ufc, list);
list_for_each_entry(ufc, &domain->ufc_list, list) {
if (ufc->info.ctrl_type == PM_QOS_MAX_LIMIT) {
if (ufc->info.exe_mode == AARCH64_MODE)
r_ufc = ufc;
else
r_ufc_32 = ufc;
}
}
scale++;
if (set_limit) {
req_limit_freq = max(req_limit_freq, domain->min_freq);
pm_qos_update_request(&domain->user_max_qos_req,
req_limit_freq);
set_limit = false;
continue;
}
if (set_max) {
pm_qos_update_request(&domain->user_max_qos_req,
domain->max_freq);
continue;
}
/* Clear all constraint by cpufreq_max_limit */
if (input_freq < 0) {
enable_domain_cpus(domain);
pm_qos_update_request(&domain->user_max_qos_req,
domain->max_freq);
continue;
}
/*
* User inputs scaled down frequency. To recover real
* frequency, scale up frequency as multiple of 4.
* ex) domain2 = freq
* domain1 = freq * 4
* domain0 = freq * 16
*/
freq = input_freq << (scale * SCALE_SIZE);
if (policy && r_ufc) {
if (sse_mode && r_ufc_32)
r_ufc = r_ufc_32;
ret = cpufreq_frequency_table_target(policy, domain->freq_table,
freq, CPUFREQ_RELATION_L, &index);
if (ret) {
pr_err("target frequency(%d) out of range\n", freq);
continue;
}
req_limit_freq = r_ufc->info.freq_table[index].limit_freq;
if (req_limit_freq)
set_limit = true;
}
if (freq < domain->min_freq) {
set_limit = false;
pm_qos_update_request(&domain->user_max_qos_req, 0);
disable_domain_cpus(domain);
continue;
}
enable_domain_cpus(domain);
freq = max(freq, domain->min_freq);
pm_qos_update_request(&domain->user_max_qos_req, freq);
set_max = true;
}
}
static ssize_t store_cpufreq_max_limit(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int input;
if (sscanf(buf, "%8d", &input) < 1)
return -EINVAL;
last_max_limit = input;
cpufreq_max_limit_update(input);
return count;
}
static ssize_t show_execution_mode_change(struct kobject *kobj,
struct attribute *attr, char *buf)
{
return snprintf(buf, 10, "%d\n", sse_mode);
}
static ssize_t store_execution_mode_change(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
int input;
int prev_mode;
if (sscanf(buf, "%8d", &input) < 1)
return -EINVAL;
prev_mode = sse_mode;
sse_mode = !!input;
if (prev_mode != sse_mode) {
if (last_max_limit != -1)
cpufreq_max_limit_update(last_max_limit);
}
return count;
}
static struct global_attr cpufreq_table =
__ATTR(cpufreq_table, 0444, show_cpufreq_table, NULL);
static struct global_attr cpufreq_min_limit =
__ATTR(cpufreq_min_limit, 0644,
show_cpufreq_min_limit, store_cpufreq_min_limit);
static struct global_attr cpufreq_min_limit_wo_boost =
__ATTR(cpufreq_min_limit_wo_boost, 0644,
show_cpufreq_min_limit, store_cpufreq_min_limit_wo_boost);
static struct global_attr cpufreq_max_limit =
__ATTR(cpufreq_max_limit, 0644,
show_cpufreq_max_limit, store_cpufreq_max_limit);
static struct global_attr execution_mode_change =
__ATTR(execution_mode_change, 0644,
show_execution_mode_change, store_execution_mode_change);
static __init void init_sysfs(void)
{
if (sysfs_create_file(power_kobj, &cpufreq_table.attr))
pr_err("failed to create cpufreq_table node\n");
if (sysfs_create_file(power_kobj, &cpufreq_min_limit.attr))
pr_err("failed to create cpufreq_min_limit node\n");
if (sysfs_create_file(power_kobj, &cpufreq_min_limit_wo_boost.attr))
pr_err("failed to create cpufreq_min_limit_wo_boost node\n");
if (sysfs_create_file(power_kobj, &cpufreq_max_limit.attr))
pr_err("failed to create cpufreq_max_limit node\n");
if (sysfs_create_file(power_kobj, &execution_mode_change.attr))
pr_err("failed to create cpufreq_max_limit node\n");
}
static int parse_ufc_ctrl_info(struct exynos_cpufreq_domain *domain,
struct device_node *dn)
{
unsigned int val;
if (!of_property_read_u32(dn, "user-default-qos", &val))
domain->user_default_qos = val;
return 0;
}
static __init void init_pm_qos(struct exynos_cpufreq_domain *domain)
{
pm_qos_add_request(&domain->user_min_qos_req,
domain->pm_qos_min_class, domain->min_freq);
pm_qos_add_request(&domain->user_max_qos_req,
domain->pm_qos_max_class, domain->max_freq);
pm_qos_add_request(&domain->user_min_qos_wo_boost_req,
domain->pm_qos_min_class, domain->min_freq);
}
int ufc_domain_init(struct exynos_cpufreq_domain *domain)
{
struct device_node *dn, *child;
struct cpumask mask;
const char *buf;
dn = of_find_node_by_name(NULL, "cpufreq-ufc");
while ((dn = of_find_node_by_type(dn, "cpufreq-userctrl"))) {
of_property_read_string(dn, "shared-cpus", &buf);
cpulist_parse(buf, &mask);
if (cpumask_intersects(&mask, &domain->cpus)) {
pr_info("found!\n");
break;
}
}
for_each_child_of_node(dn, child) {
struct exynos_ufc *ufc;
ufc = kzalloc(sizeof(struct exynos_ufc), GFP_KERNEL);
if (!ufc)
return -ENOMEM;
ufc->info.freq_table = kzalloc(sizeof(struct exynos_ufc_freq)
* domain->table_size, GFP_KERNEL);
if (!ufc->info.freq_table) {
kfree(ufc);
return -ENOMEM;
}
list_add_tail(&ufc->list, &domain->ufc_list);
}
return 0;
}
static int __init init_ufc_table_dt(struct exynos_cpufreq_domain *domain,
struct device_node *dn)
{
struct device_node *child;
struct exynos_ufc_freq *table;
struct exynos_ufc *ufc;
int size, index, c_index;
int ret;
ufc = list_entry(&domain->ufc_list, struct exynos_ufc, list);
pr_info("Initialize ufc table for Domain %d\n", domain->id);
for_each_child_of_node(dn, child) {
ufc = list_next_entry(ufc, list);
if (of_property_read_u32(child, "ctrl-type", &ufc->info.ctrl_type))
continue;
if (of_property_read_u32(child, "execution-mode", &ufc->info.exe_mode))
continue;
size = of_property_count_u32_elems(child, "table");
if (size < 0)
return size;
table = kzalloc(sizeof(struct exynos_ufc_freq) * size / 2, GFP_KERNEL);
if (!table)
return -ENOMEM;
ret = of_property_read_u32_array(child, "table", (unsigned int *)table, size);
if (ret)
return -EINVAL;
pr_info("Register UFC Type-%d Execution Mode-%d for Domain %d\n",
ufc->info.ctrl_type, ufc->info.exe_mode, domain->id);
for (index = 0; index < domain->table_size; index++) {
unsigned int freq = domain->freq_table[index].frequency;
if (freq == CPUFREQ_ENTRY_INVALID)
continue;
for (c_index = 0; c_index < size / 2; c_index++) {
if (freq <= table[c_index].master_freq)
ufc->info.freq_table[index].limit_freq = table[c_index].limit_freq;
if (freq >= table[c_index].master_freq)
break;
}
pr_info("Master_freq : %u kHz - limit_freq : %u kHz\n",
ufc->info.freq_table[index].master_freq,
ufc->info.freq_table[index].limit_freq);
}
kfree(table);
}
return 0;
}
static int __init exynos_ufc_init(void)
{
struct device_node *dn = NULL;
const char *buf;
struct exynos_cpufreq_domain *domain;
int ret = 0;
pm_qos_add_request(&cpu_online_max_qos_req, PM_QOS_CPU_ONLINE_MAX,
PM_QOS_CPU_ONLINE_MAX_DEFAULT_VALUE);
while ((dn = of_find_node_by_type(dn, "cpufreq-userctrl"))) {
struct cpumask shared_mask;
ret = of_property_read_string(dn, "shared-cpus", &buf);
if (ret) {
pr_err("failed to get shared-cpus for ufc\n");
goto exit;
}
cpulist_parse(buf, &shared_mask);
domain = find_domain_cpumask(&shared_mask);
if (!domain) {
pr_err("Can't found domain for ufc!\n");
goto exit;
}
/* Initialize user control information from dt */
ret = parse_ufc_ctrl_info(domain, dn);
if (ret) {
pr_err("failed to get ufc ctrl info\n");
goto exit;
}
/* Parse user frequency ctrl table info from dt */
ret = init_ufc_table_dt(domain, dn);
if (ret) {
pr_err("failed to parse frequency table for ufc ctrl\n");
goto exit;
}
/* Initialize PM QoS */
init_pm_qos(domain);
pr_info("Complete to initialize domain%d\n", domain->id);
}
init_sysfs();
pr_info("Initialized Exynos UFC(User-Frequency-Ctrl) driver\n");
exit:
return 0;
}
late_initcall(exynos_ufc_init);
|