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
|
/*
* Copyright (c) 2018 Hong Hyunji, Samsung Electronics Co., Ltd <hyunji.hong@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 MOCE(MOCE that can Diversify Conditions for Entry into Idle State) driver implementation
*
* MOCE can vary the target residency and exit latency in accordance with the frequency
*/
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/cpuidle.h>
#include <linux/cpufreq.h>
#include <linux/of_device.h>
#include <linux/cpuidle-moce.h>
#include <linux/kdev_t.h>
#include <soc/samsung/exynos-cpupm.h>
/*
* Define the considered element as a number.
* Used to read the ratios and weights variables.
*/
#define DEFAULT_RATIO 100
#define MIN_RATIO 10
DEFINE_PER_CPU(struct bias_cpuidle, bias_cpuidle);
/*****************************************************************************
* HELPER FUNCTION *
*****************************************************************************/
static struct factor *find_factor(struct bias_cpuidle *bias_idle, factor_type type)
{
struct factor *pos;
list_for_each_entry(pos, &bias_idle->factor_list, list)
if (pos->type == type)
return pos;
return NULL;
}
static unsigned int find_ratio(unsigned int *table, unsigned int size, unsigned int value)
{
int i;
/* Searching correct value according to state of factor */
for (i = 0 ; i < size - 1; i += 2)
if (value < table[i + 1])
break;
return table[i];
}
static void update_total_ratio(struct bias_cpuidle *bias_idle)
{
struct factor *pos;
unsigned int num = 0, total_ratio = 0;
if (list_empty(&bias_idle->factor_list))
return;
list_for_each_entry(pos, &bias_idle->factor_list, list) {
total_ratio += pos->ratio;
num++;
}
total_ratio /= num;
/* limit the minimum ratio to prevent problems caused by excessive bias. */
if (total_ratio < MIN_RATIO)
bias_idle->total_ratio = MIN_RATIO;
else
bias_idle->total_ratio = total_ratio;
}
/*****************************************************************************
* EXTERNAL REFERENCE APIs *
*****************************************************************************/
/*
* This function is called to bias target_residency values,
* when deciding next_state in cpupm and idle governors.
*/
unsigned int exynos_moce_get_ratio(unsigned int cpu)
{
struct bias_cpuidle *bias_idle = &per_cpu(bias_cpuidle, cpu);
/* check have factor for the cpu */
if (!bias_idle->biased)
return DEFAULT_RATIO;
return bias_idle->total_ratio;
}
EXPORT_SYMBOL(exynos_moce_get_ratio);
/*****************************************************************************
* DEFINE NOTIFIER CALL *
*****************************************************************************/
/*
* cpufreq trans call back function :
* if was changed cpufreq factor,
* search and save the applied ratio in the per-cpu structure.
*/
static int exynos_moce_cpufreq_trans_notifier(struct notifier_block *nb,
unsigned long val, void *data)
{
struct cpufreq_freqs *freq = data;
struct bias_cpuidle *bias_idle;
struct factor *factor;
struct cpumask mask;
int cpu;
unsigned int cur_ratio;
unsigned long flags;
if (freq->flags & CPUFREQ_CONST_LOOPS)
return NOTIFY_OK;
if (val != CPUFREQ_POSTCHANGE)
return NOTIFY_OK;
bias_idle = &per_cpu(bias_cpuidle, freq->cpu);
/* check have factor for the cpu */
if (!bias_idle->biased)
return NOTIFY_OK;
factor = find_factor(bias_idle, FREQ_FACTOR);
if (!factor)
return NOTIFY_DONE;
/* find applied ratio */
cur_ratio = find_ratio(factor->ratio_table, factor->size, freq->new);
cpumask_copy(&mask, &factor->cpus);
/* update value of the other sibling-cpus */
for_each_cpu(cpu, &mask) {
bias_idle = &per_cpu(bias_cpuidle, cpu);
factor = find_factor(bias_idle, FREQ_FACTOR);
if (!factor)
return NOTIFY_DONE;
spin_lock_irqsave(&factor->lock, flags);
factor->ratio = cur_ratio;
update_total_ratio(bias_idle);
spin_unlock_irqrestore(&factor->lock, flags);
}
return NOTIFY_OK;
}
static struct notifier_block exynos_cpufreq_trans_nb = {
.notifier_call = exynos_moce_cpufreq_trans_notifier,
};
/*****************************************************************************
* SYSFS INTERFACES *
*****************************************************************************/
/*
* format:
* {DomainID} {ratio} {freq}:{ratio} {freq}:{ratio} ...:{ratio}
* converted into array:
* [DomainID][ratio][freq][ratio][freq]...[ratio]
*/
static unsigned int *get_tokenized_data(const char *buf, int *num_tokens, unsigned int *cpu)
{
const char *bbuf;
int i, ntokens = 1;
unsigned int *tokenized_data;
bbuf = buf;
while ((bbuf = strpbrk(bbuf + 1, " :")))
ntokens++;
if ((ntokens & 0x1))
return NULL;
tokenized_data = kzalloc((ntokens - 1) * sizeof(unsigned int), GFP_KERNEL);
if (!tokenized_data)
return NULL;
bbuf = buf;
i = 0;
if (sscanf(bbuf, "%u", cpu) != 1)
goto err_kfree;
if (*cpu > NR_CPUS)
goto err_kfree;
bbuf = strpbrk(bbuf, " ");
if (!bbuf)
goto err_kfree;
bbuf++;
while (i < (ntokens - 1)) {
if (sscanf(bbuf, "%u", &tokenized_data[i++]) != 1)
goto err_kfree;
/* Any percentage value except */
if ((i & 0x1) == 1 && tokenized_data[i-1] < 10)
goto err_kfree;
bbuf = strpbrk(bbuf, " :");
if (!bbuf)
break;
bbuf++;
}
if (i != (ntokens - 1))
goto err_kfree;
*num_tokens = ntokens - 1;
return tokenized_data;
err_kfree:
kfree(tokenized_data);
return NULL;
}
static factor_type get_factor_type(const char *name)
{
factor_type type = NO_FACTOR;
if (!strcmp(name, "freq_factor"))
type = FREQ_FACTOR;
/* Add sysfs node for the new factor */
return type;
}
static ssize_t show_factor(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct bias_cpuidle *bias_idle;
struct factor *factor;
int i, cpu;
ssize_t size = 0;
unsigned long flags;
factor_type type = get_factor_type(attr->attr.name);
if (type == NO_FACTOR)
return -ENODEV;
for_each_possible_cpu(cpu) {
bias_idle = &per_cpu(bias_cpuidle, cpu);
factor = find_factor(bias_idle, type);
if (!factor)
return size;
if (cpu != cpumask_first(&factor->cpus))
continue;
spin_lock_irqsave(&factor->lock, flags);
size += snprintf(buf + size, PAGE_SIZE - size, "[cpumask:%*pbl] ",
cpumask_pr_args(&factor->cpus));
for (i = 0; i < factor->size; i++)
size += snprintf(buf + size, PAGE_SIZE - size, "%u%s",
factor->ratio_table[i],
i & 0x1 ? ":" : " ");
size += snprintf(buf + size, PAGE_SIZE - size, "\n");
spin_unlock_irqrestore(&factor->lock, flags);
}
return size;
}
static ssize_t store_factor(struct device *dev, struct device_attribute *attr,
const char *buf, size_t size)
{
struct bias_cpuidle *bias_idle;
struct factor *factor;
struct cpumask mask;
int ntokens;
unsigned int cpu;
unsigned int *new_ratio_table;
unsigned int *old_ratio_table;
unsigned long flags;
factor_type type = get_factor_type(attr->attr.name);
if (type == NO_FACTOR)
return -ENODEV;
/* convert input data into new ratio table */
new_ratio_table = get_tokenized_data(buf, &ntokens, &cpu);
if (!new_ratio_table)
return -EINVAL;
bias_idle = &per_cpu(bias_cpuidle, cpu);
factor = find_factor(bias_idle, type);
if (!factor) {
kfree(new_ratio_table);
return -EINVAL;
}
cpumask_copy(&mask, &factor->cpus);
if (cpumask_empty(&mask)) {
kfree(new_ratio_table);
return -EINVAL;
}
old_ratio_table = factor->ratio_table;
/* change ratio table of sibling-cpus */
for_each_cpu(cpu, &mask) {
bias_idle = &per_cpu(bias_cpuidle, cpu);
factor = find_factor(bias_idle, type);
if (!factor) {
kfree(new_ratio_table);
return -EINVAL;
}
spin_lock_irqsave(&factor->lock, flags);
factor->ratio_table = new_ratio_table;
factor->size = ntokens;
spin_unlock_irqrestore(&factor->lock, flags);
}
kfree(old_ratio_table);
return size;
}
static DEVICE_ATTR(freq_factor, S_IRUGO | S_IWUSR, show_factor, store_factor);
/* Add sysfs node for the new factor */
static struct attribute *cpuidle_moce_attrs[] = {
&dev_attr_freq_factor.attr,
NULL,
};
static const struct attribute_group cpuidle_moce_group = {
.attrs = cpuidle_moce_attrs,
};
/*****************************************************************************
* INITIALIZE EXYNOS MOCE DRIVER *
*****************************************************************************/
extern struct class *idle_class;
static int __init init_sysfs(void)
{
struct device *dev;
dev = device_create(idle_class, NULL, MKDEV(0, 1), NULL, "moce_factor");
if (sysfs_create_group(&dev->kobj, &cpuidle_moce_group))
return -ENODEV;
return 0;
}
static struct factor *__init init_factor_data(struct device_node *factor_node, int cpu)
{
struct bias_cpuidle *bias_first;
struct factor *factor, *first;
const char *buf;
/* alloc factor */
factor = kzalloc(sizeof(struct factor), GFP_KERNEL);
if (!factor)
return NULL;
/* factor sibling-cpus */
if (of_property_read_string(factor_node, "sibling-cpus", &buf))
goto moce_fail;
cpulist_parse(buf, &factor->cpus);
if (cpumask_weight(&factor->cpus) == 0)
goto moce_fail;
/* factor type */
if (of_property_read_u32(factor_node, "type", &factor->type))
goto moce_fail;
/* factor ratio-table */
factor->size = of_property_count_u32_elems(factor_node, "ratio-table");
if (factor->size < 0)
goto moce_fail;
/* If there is a same ratio table previously created, just points to it. */
if (cpu == cpumask_first(&factor->cpus)) {
/* alloc factor ratio-table */
factor->ratio_table = kzalloc(sizeof(unsigned int) * factor->size, GFP_KERNEL);
if (!factor->ratio_table)
goto moce_fail;
of_property_read_u32_array(factor_node, "ratio-table",
factor->ratio_table, factor->size);
} else {
bias_first = &per_cpu(bias_cpuidle, cpumask_first(&factor->cpus));
first = find_factor(bias_first, factor->type);
if (!first)
goto moce_fail;
factor->ratio_table = first->ratio_table;
}
factor->ratio = DEFAULT_RATIO;
spin_lock_init(&factor->lock);
return factor;
moce_fail:
kfree(factor);
return NULL;
}
static int __init dt_init_bias_idle(void)
{
struct device_node *cpu_node;
struct device_node *factor_node;
struct bias_cpuidle *bias_idle;
struct factor *factor;
int cpu, i;
/* init per-cpu variable */
for_each_possible_cpu(cpu) {
cpu_node = of_cpu_device_node_get(cpu);
/* init per-cpu variable */
bias_idle = &per_cpu(bias_cpuidle, cpu);
INIT_LIST_HEAD(&bias_idle->factor_list);
/* per-factor */
for (i = 0; ; i++) {
factor_node = of_parse_phandle(cpu_node, "moce-factors", i);
if (!factor_node) {
pr_debug("The number of moce factors for cpu%d is %d\n", cpu, i);
break;
}
factor = init_factor_data(factor_node, cpu);
if (!factor) {
of_node_put(factor_node);
of_node_put(cpu_node);
pr_err("failed to initialize %dth moce factors for cpu%d\n", i + 1, cpu);
return -1;
}
list_add_tail(&factor->list, &bias_idle->factor_list);
of_node_put(factor_node);
}
if (!list_empty(&bias_idle->factor_list)) {
bias_idle->total_ratio = DEFAULT_RATIO;
bias_idle->biased = true;
}
of_node_put(cpu_node);
}
return 0;
}
static int __init exynos_moce_init(void)
{
int ret;
ret = dt_init_bias_idle();
if (ret) {
pr_err("failed to initialize moce driver\n");
return -1;
}
ret = init_sysfs();
if (ret) {
pr_err("failed to create sysfs group\n");
return ret;
}
cpufreq_register_notifier(&exynos_cpufreq_trans_nb,
CPUFREQ_TRANSITION_NOTIFIER);
pr_info("initialized exynos moce driver\n");
return 0;
}
device_initcall(exynos_moce_init)
|