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
|
/* drivers/devfreq/exynos7420_ppmu.c
*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*
* EXYNOS7420 - PPMU control.
*
* 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.
*/
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include "exynos7420_ppmu.h"
#include "exynos_ppmu_fw.h"
#define MIF_DEFAULT_PPMU_WEIGHT 100
static struct workqueue_struct *exynos_ppmu_wq;
static struct delayed_work exynos_ppmu_work;
static unsigned long exynos_ppmu_polling_period;
static unsigned long long mif_weight = MIF_DEFAULT_PPMU_WEIGHT;
static DEFINE_MUTEX(exynos_ppmu_lock);
static LIST_HEAD(exynos_ppmu_list);
static struct srcu_notifier_head exynos_ppmu_notifier_list[DEVFREQ_TYPE_COUNT];
static struct ppmu_info ppmu[PPMU_COUNT] = {
[PPMU0_0_GEN_RT] = {
.base = (void __iomem *)PPMU0_0_GEN_RT_ADDR,
},
[PPMU0_1_CPU] = {
.base = (void __iomem *)PPMU0_1_CPU_ADDR,
},
[PPMU1_0_GEN_RT] = {
.base = (void __iomem *)PPMU1_0_GEN_RT_ADDR,
},
[PPMU1_1_CPU] = {
.base = (void __iomem *)PPMU1_1_CPU_ADDR,
},
[PPMU2_0_GEN_RT] = {
.base = (void __iomem *)PPMU2_0_GEN_RT_ADDR,
},
[PPMU2_1_CPU] = {
.base = (void __iomem *)PPMU2_1_CPU_ADDR,
},
[PPMU3_0_GEN_RT] = {
.base = (void __iomem *)PPMU3_0_GEN_RT_ADDR,
},
[PPMU3_1_CPU] = {
.base = (void __iomem *)PPMU3_1_CPU_ADDR,
},
};
static int exynos7420_ppmu_notifier_list_init(void)
{
int i;
for (i = 0; i < DEVFREQ_TYPE_COUNT; ++i)
srcu_init_notifier_head(&exynos_ppmu_notifier_list[i]);
return 0;
}
int exynos7420_devfreq_init(struct devfreq_exynos *de)
{
INIT_LIST_HEAD(&de->node);
return 0;
}
int exynos7420_devfreq_register(struct devfreq_exynos *de)
{
int i;
for (i = 0; i < de->ppmu_count; ++i) {
if (ppmu_init(&de->ppmu_list[i]))
return -EINVAL;
}
mutex_lock(&exynos_ppmu_lock);
list_add_tail(&de->node, &exynos_ppmu_list);
mutex_unlock(&exynos_ppmu_lock);
return 0;
}
int exynos7420_ppmu_register_notifier(enum DEVFREQ_TYPE type, struct notifier_block *nb)
{
return srcu_notifier_chain_register(&exynos_ppmu_notifier_list[type], nb);
}
int exynos7420_ppmu_unregister_notifier(enum DEVFREQ_TYPE type, struct notifier_block *nb)
{
return srcu_notifier_chain_unregister(&exynos_ppmu_notifier_list[type], nb);
}
static int exynos7420_ppmu_notify(enum DEVFREQ_TYPE type)
{
BUG_ON(irqs_disabled());
return srcu_notifier_call_chain(&exynos_ppmu_notifier_list[type], 0, NULL);
}
static void exynos7420_update_polling(unsigned int period)
{
exynos_ppmu_polling_period = period;
cancel_delayed_work_sync(&exynos_ppmu_work);
if (period == 0)
return;
queue_delayed_work(exynos_ppmu_wq, &exynos_ppmu_work,
msecs_to_jiffies(period));
}
static int exynos7420_ppmu_mif_calculate(struct ppmu_info *ppmu,
unsigned int size,
unsigned long long *ccnt,
unsigned long long *pmcnt)
{
unsigned int i;
unsigned long long val_ccnt = 0;
unsigned long long val_pmcnt0 = 0;
unsigned long long val_pmcnt1 = 0;
unsigned long long val_pmcnt3 = 0;
unsigned long long ppmu_n = 0;
*pmcnt = 0;
for (i = 0; i < size; ++i) {
if (ppmu_count(ppmu + i, &val_ccnt, &val_pmcnt0, &val_pmcnt1, &val_pmcnt3))
return -EINVAL;
if (*ccnt < val_ccnt)
*ccnt = val_ccnt;
val_pmcnt3 = div_u64(val_pmcnt3 * mif_weight, 100);
if (i % 2 == 0) {
ppmu_n = val_pmcnt3;
continue;
} else {
ppmu_n += val_pmcnt3;
*pmcnt = max(*pmcnt, ppmu_n);
}
}
return 0;
}
static int exynos7420_ppmu_int_calculatate(struct ppmu_info *ppmu,
unsigned int size,
unsigned long long *ccnt,
unsigned long long *pmcnt)
{
unsigned int i;
unsigned long long val_ccnt = 0;
unsigned long long val_pmcnt0 = 0;
unsigned long long val_pmcnt1 = 0;
unsigned long long val_pmcnt3 = 0;
for (i = 0; i < size; ++i) {
if (ppmu_count(ppmu + i, &val_ccnt, &val_pmcnt0, &val_pmcnt1, &val_pmcnt3))
return -EINVAL;
if (*ccnt < val_ccnt)
*ccnt = val_ccnt;
*pmcnt = max3(*pmcnt, val_pmcnt0, val_pmcnt1);
}
return 0;
}
static void exynos7420_ppmu_update(void)
{
struct devfreq_exynos *devfreq;
pfn_ppmu_count pfn_count;
/* before getting ppmu count, it first should stop ppmu */
if (ppmu_count_stop(ppmu,
ARRAY_SIZE(ppmu))) {
pr_err("DEVFREQ(PPMU) : ppmu can't stop\n");
return;
}
list_for_each_entry(devfreq, &exynos_ppmu_list, node) {
switch (devfreq->type) {
case MIF:
pfn_count = exynos7420_ppmu_mif_calculate;
break;
case INT:
pfn_count = exynos7420_ppmu_int_calculatate;
break;
default:
pfn_count = NULL;
break;
}
if (ppmu_count_total(devfreq->ppmu_list,
devfreq->ppmu_count,
pfn_count,
&devfreq->val_ccnt,
&devfreq->val_pmcnt)) {
pr_err("DEVFREQ(PPMU) : ppmu can't update data\n");
continue;
}
}
}
int exynos7420_ppmu_activate(void)
{
int i;
mutex_lock(&exynos_ppmu_lock);
for (i = 0; i < PPMU_COUNT; ++i) {
if (ppmu_init(&ppmu[i])) {
mutex_unlock(&exynos_ppmu_lock);
goto err;
}
if (ppmu_init_fw(&ppmu[i])) {
mutex_unlock(&exynos_ppmu_lock);
goto err;
}
if (ppmu_reset(&ppmu[i])) {
mutex_unlock(&exynos_ppmu_lock);
goto err;
}
}
mutex_unlock(&exynos_ppmu_lock);
exynos7420_update_polling(100);
return 0;
err:
for (; i >= 0; --i)
ppmu_term(&ppmu[i]);
return -EINVAL;
}
int exynos7420_ppmu_deactivate(void)
{
int i;
exynos7420_update_polling(0);
mutex_lock(&exynos_ppmu_lock);
for (i = 0; i < PPMU_COUNT; ++i) {
if (ppmu_disable(&ppmu[i])) {
mutex_unlock(&exynos_ppmu_lock);
goto err;
}
}
mutex_unlock(&exynos_ppmu_lock);
return 0;
err:
pr_err("DEVFREQ(PPMU) : can't deactivate counter\n");
return -EINVAL;
}
static int exynos7420_ppmu_reset(void)
{
if (ppmu_reset_total(ppmu,
ARRAY_SIZE(ppmu))) {
pr_err("DEVFREQ(PPMU) : ppmu can't reset data\n");
return -EAGAIN;
}
return 0;
}
static void exynos7420_monitor(struct work_struct *work)
{
int i;
mutex_lock(&exynos_ppmu_lock);
exynos7420_ppmu_update();
for (i = 0; i < DEVFREQ_TYPE_COUNT; ++i)
exynos7420_ppmu_notify(i);
exynos7420_ppmu_reset();
mutex_unlock(&exynos_ppmu_lock);
queue_delayed_work(exynos_ppmu_wq, &exynos_ppmu_work,
msecs_to_jiffies(exynos_ppmu_polling_period));
}
static int exynos7420_ppmu_probe(struct platform_device *pdev)
{
int ret = 0;
exynos_ppmu_wq = create_freezable_workqueue("exynos7420_ppmu_wq");
INIT_DELAYED_WORK(&exynos_ppmu_work, exynos7420_monitor);
ret = exynos7420_ppmu_activate();
return ret;
}
static int exynos7420_ppmu_remove(struct platform_device *pdev)
{
exynos7420_ppmu_deactivate();
flush_workqueue(exynos_ppmu_wq);
destroy_workqueue(exynos_ppmu_wq);
return 0;
}
static int exynos7420_ppmu_suspend(struct device *dev)
{
exynos7420_ppmu_deactivate();
return 0;
}
static int exynos7420_ppmu_resume(struct device *dev)
{
int i;
mutex_lock(&exynos_ppmu_lock);
for (i = 0; i < PPMU_COUNT; ++i) {
if (ppmu_init_fw(&ppmu[i])) {
mutex_unlock(&exynos_ppmu_lock);
goto err;
}
}
exynos7420_ppmu_reset();
mutex_unlock(&exynos_ppmu_lock);
exynos7420_update_polling(100);
return 0;
err:
pr_err("DEVFREQ(PPMU) : ppmu can't resume.\n");
return -EINVAL;
}
static struct dev_pm_ops exynos7420_ppmu_pm = {
.suspend = exynos7420_ppmu_suspend,
.resume = exynos7420_ppmu_resume,
};
static struct platform_driver exynos7420_ppmu_driver = {
.probe = exynos7420_ppmu_probe,
.remove = exynos7420_ppmu_remove,
.driver = {
.name = "exynos7420-ppmu",
.owner = THIS_MODULE,
.pm = &exynos7420_ppmu_pm,
},
};
static struct platform_device exynos7420_ppmu_device = {
.name = "exynos7420-ppmu",
.id = -1,
};
static int __init exynos7420_ppmu_early_init(void)
{
return exynos7420_ppmu_notifier_list_init();
}
arch_initcall_sync(exynos7420_ppmu_early_init);
static int __init exynos7420_ppmu_init(void)
{
int ret;
ret = platform_device_register(&exynos7420_ppmu_device);
if (ret)
return ret;
return platform_driver_register(&exynos7420_ppmu_driver);
}
late_initcall_sync(exynos7420_ppmu_init);
static void __exit exynos7420_ppmu_exit(void)
{
platform_driver_unregister(&exynos7420_ppmu_driver);
platform_device_unregister(&exynos7420_ppmu_device);
}
module_exit(exynos7420_ppmu_exit);
|