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
|
/*
* Copyright (c) 2013 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
* Taekki Kim <taekki.kim@samsung.com>
*
* EXYNOS - PPMU polling support
* This version supports EXYNOS5422 only. This changes bus frequencies.
*
* 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/kernel.h>
#include <linux/debugfs.h>
#include <linux/hrtimer.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <mach/map.h>
#include "exynos_ppmu.h"
#include "exynos5422_ppmu.h"
#define CREATE_TRACE_POINTS
#define FIXED_POINT_OFFSET 8
#define FIXED_POINT_MASK ((1 << FIXED_POINT_OFFSET) - 1)
enum exynos5_ppmu_list {
PPMU_DMC_0_0,
PPMU_DMC_1_0,
PPMU_DMC_0_1,
PPMU_DMC_1_1,
PPMU_END,
};
static DEFINE_SPINLOCK(exynos5_ppmu_lock);
static LIST_HEAD(exynos5_ppmu_handle_list);
struct exynos5_ppmu_handle {
struct list_head node;
struct exynos_ppmu ppmu[PPMU_END];
};
static const char *exynos5_ppmu_name[PPMU_END] = {
[PPMU_DMC_0_0] = "DMC_0_0",
[PPMU_DMC_1_0] = "DMC_1_0",
[PPMU_DMC_0_1] = "DMC_0_1",
[PPMU_DMC_1_1] = "DMC_1_1",
};
static struct exynos_ppmu ppmu[PPMU_END] = {
[PPMU_DMC_0_0] = {
.hw_base = S5P_VA_PPMU_DREX0_0,
},
[PPMU_DMC_1_0] = {
.hw_base = S5P_VA_PPMU_DREX1_0,
},
[PPMU_DMC_0_1] = {
.hw_base = S5P_VA_PPMU_DREX0_1,
},
[PPMU_DMC_1_1] = {
.hw_base = S5P_VA_PPMU_DREX1_1,
},
};
static struct exynos5_ppmu_handle *exynos5_ppmu_trace_handle;
static void exynos5_ppmu_reset(struct exynos_ppmu *ppmu)
{
unsigned long flags;
void __iomem *ppmu_base = ppmu->hw_base;
/* Reset PPMU */
exynos_ppmu_reset(ppmu_base);
/* Set PPMU Event */
ppmu->event[PPMU_PMNCNT0] = RD_DATA_COUNT;
exynos_ppmu_setevent(ppmu_base, PPMU_PMNCNT0,
ppmu->event[PPMU_PMNCNT0]);
ppmu->event[PPMU_PMCCNT1] = WR_DATA_COUNT;
exynos_ppmu_setevent(ppmu_base, PPMU_PMCCNT1,
ppmu->event[PPMU_PMCCNT1]);
ppmu->event[PPMU_PMNCNT3] = RDWR_DATA_COUNT;
exynos_ppmu_setevent(ppmu_base, PPMU_PMNCNT3,
ppmu->event[PPMU_PMNCNT3]);
local_irq_save(flags);
ppmu->reset_time = ktime_get();
/* Start PPMU */
exynos_ppmu_start(ppmu_base);
local_irq_restore(flags);
}
static void exynos5_ppmu_read(struct exynos_ppmu *ppmu)
{
int j;
unsigned long flags;
ktime_t read_time;
ktime_t t;
u32 reg;
void __iomem *ppmu_base = ppmu->hw_base;
local_irq_save(flags);
read_time = ktime_get();
/* Stop PPMU */
exynos_ppmu_stop(ppmu_base);
local_irq_restore(flags);
/* Update local data from PPMU */
ppmu->ccnt = __raw_readl(ppmu_base + PPMU_CCNT);
reg = __raw_readl(ppmu_base + PPMU_FLAG);
ppmu->ccnt_overflow = reg & PPMU_CCNT_OVERFLOW;
for (j = PPMU_PMNCNT0; j < PPMU_PMNCNT_MAX; j++) {
if (ppmu->event[j] == 0)
ppmu->count[j] = 0;
else
ppmu->count[j] = exynos_ppmu_read(ppmu_base, j);
}
t = ktime_sub(read_time, ppmu->reset_time);
ppmu->ns = ktime_to_ns(t);
}
static void exynos5_ppmu_add(struct exynos_ppmu *to, struct exynos_ppmu *from,
enum exynos5_ppmu_list start, enum exynos5_ppmu_list end)
{
int i;
int j;
for (i = start; i <= end; i++) {
for (j = PPMU_PMNCNT0; j < PPMU_PMNCNT_MAX; j++)
to[i].count[j] = from[i].count[j];
to[i].ccnt = from[i].ccnt;
if (to[i].ccnt < from[i].ccnt)
to[i].ccnt_overflow = true;
to[i].ns += from[i].ns;
if (from[i].ccnt_overflow)
to[i].ccnt_overflow = true;
}
}
static void exynos5_ppmu_handle_clear(struct exynos5_ppmu_handle *handle, int handle_size)
{
memset(&handle->ppmu, 0, sizeof(struct exynos_ppmu) * handle_size);
}
static int exynos5_ppmu_update(enum exynos5_ppmu_list start, enum exynos5_ppmu_list end)
{
int i, ret = 0;
struct exynos5_ppmu_handle *handle;
for (i = start; i <= end; i++) {
exynos5_ppmu_read(&ppmu[i]);
exynos5_ppmu_reset(&ppmu[i]);
}
list_for_each_entry(handle, &exynos5_ppmu_handle_list, node)
exynos5_ppmu_add(handle->ppmu, ppmu, start, end);
return ret;
}
static int exynos5_ppmu_get_filter(enum exynos_ppmu_sets filter,
enum exynos5_ppmu_list *start, enum exynos5_ppmu_list *end)
{
switch (filter) {
case PPMU_SET_DDR:
*start = PPMU_DMC_0_0;
*end = PPMU_DMC_1_1;
break;
case PPMU_SET_TOP:
*start = PPMU_DMC_0_0;
*end = PPMU_DMC_1_0;
break;
default:
return -EINVAL;
}
return 0;
}
int exynos5_ppmu_get_busy(struct exynos5_ppmu_handle *handle,
enum exynos_ppmu_sets filter, unsigned long long *ccnt,
unsigned long long *pmcnt)
{
unsigned long flags;
int i;
int busy = 0;
enum exynos5_ppmu_list start, end;
int ret;
unsigned long max_ccnt = 0, temp_ccnt = 0;
unsigned long temp_drex0_pmcnt = 0, temp_drex1_pmcnt = 0;
ret = exynos5_ppmu_get_filter(filter, &start, &end);
if (ret < 0)
return ret;
spin_lock_irqsave(&exynos5_ppmu_lock, flags);
exynos5_ppmu_update(start, end);
/* for MIF usage calculation
* DREX0 = 1.5 * RWcpu + 0.5 * RWdev
* DREX1 = 1 * RWcpu + 1 * RWdev
* pmcnt = max(DREX0, DREX1) * 1.5 (usage_rate proportional value for 5422)
*/
for (i = start; i <= end; i++) {
if (handle->ppmu[i].ccnt_overflow) {
busy = -EOVERFLOW;
break;
}
temp_ccnt = handle->ppmu[i].ccnt;
if (temp_ccnt > max_ccnt)
max_ccnt = temp_ccnt;
if (i == PPMU_DMC_0_0)
temp_drex0_pmcnt += (handle->ppmu[i].count[PPMU_PMNCNT3] * 15 / 10);
else if (i == PPMU_DMC_0_1)
temp_drex0_pmcnt += (handle->ppmu[i].count[PPMU_PMNCNT3] * 5 / 10);
else
temp_drex1_pmcnt += handle->ppmu[i].count[PPMU_PMNCNT3];
}
*ccnt = max_ccnt;
*pmcnt = max(temp_drex0_pmcnt * 15 / 10, temp_drex1_pmcnt);
if (max_ccnt > 0) {
busy = div_u64((*pmcnt * 100), max_ccnt);
} else {
busy = -EINVAL;
}
exynos5_ppmu_handle_clear(handle, (end - start) + 1);
spin_unlock_irqrestore(&exynos5_ppmu_lock, flags);
return busy;
}
void exynos5_ppmu_put(struct exynos5_ppmu_handle *handle)
{
unsigned long flags;
spin_lock_irqsave(&exynos5_ppmu_lock, flags);
list_del(&handle->node);
spin_unlock_irqrestore(&exynos5_ppmu_lock, flags);
kfree(handle);
}
struct exynos5_ppmu_handle *exynos5_ppmu_get(void)
{
struct exynos5_ppmu_handle *handle;
unsigned long flags;
handle = kzalloc(sizeof(struct exynos5_ppmu_handle), GFP_KERNEL);
if (!handle)
return NULL;
spin_lock_irqsave(&exynos5_ppmu_lock, flags);
exynos5_ppmu_update(0, PPMU_END - 1);
list_add_tail(&handle->node, &exynos5_ppmu_handle_list);
spin_unlock_irqrestore(&exynos5_ppmu_lock, flags);
return handle;
}
void exynos5_ppmu_trace(void)
{
unsigned long flags;
spin_lock_irqsave(&exynos5_ppmu_lock, flags);
exynos5_ppmu_update(0, PPMU_END - 1);
spin_unlock_irqrestore(&exynos5_ppmu_lock, flags);
}
static int exynos5_ppmu_trace_init(void)
{
exynos5_ppmu_trace_handle = exynos5_ppmu_get();
return 0;
}
late_initcall(exynos5_ppmu_trace_init);
static void exynos5_ppmu_debug_compute(struct exynos_ppmu *ppmu,
enum ppmu_counter i, u32 *sat, u32 *freq, u32 *bw)
{
u64 ns = ppmu->ns;
u32 busy = ppmu->count[i];
u32 total = ppmu->ccnt;
u64 s;
u64 f;
u64 b;
s = (u64)busy * 100 * (1 << FIXED_POINT_OFFSET);
s += total / 2;
do_div(s, total);
f = (u64)total * 1000 * (1 << FIXED_POINT_OFFSET);
f += ns / 2;
f = div64_u64(f, ns);
b = (u64)busy * (128 / 8) * 1000 * (1 << FIXED_POINT_OFFSET);
b += ns / 2;
b = div64_u64(b, ns);
*sat = s;
*freq = f;
*bw = b;
}
static void exynos5_ppmu_debug_show_one_counter(struct seq_file *s,
const char *name, const char *type, struct exynos_ppmu *ppmu,
enum ppmu_counter i, u32 *bw_total)
{
u32 sat;
u32 freq;
u32 bw;
exynos5_ppmu_debug_compute(ppmu, i, &sat, &freq, &bw);
seq_printf(s, "%-10s %-10s %4u.%02u MBps %3u.%02u MHz %2u.%02u%%\n",
name, type,
bw >> FIXED_POINT_OFFSET,
(bw & FIXED_POINT_MASK) * 100 / (1 << FIXED_POINT_OFFSET),
freq >> FIXED_POINT_OFFSET,
(freq & FIXED_POINT_MASK) * 100 / (1 << FIXED_POINT_OFFSET),
sat >> FIXED_POINT_OFFSET,
(sat & FIXED_POINT_MASK) * 100 / (1 << FIXED_POINT_OFFSET));
*bw_total += bw;
}
static void exynos5_ppmu_debug_show_one(struct seq_file *s,
const char *name, struct exynos_ppmu *ppmu,
u32 *bw_total)
{
exynos5_ppmu_debug_show_one_counter(s, name, "read+write",
ppmu, PPMU_PMNCNT3, &bw_total[PPMU_PMNCNT3]);
exynos5_ppmu_debug_show_one_counter(s, "", "read",
ppmu, PPMU_PMNCNT0, &bw_total[PPMU_PMNCNT0]);
exynos5_ppmu_debug_show_one_counter(s, "", "write",
ppmu, PPMU_PMCCNT1, &bw_total[PPMU_PMCCNT1]);
}
static int exynos5_ppmu_debug_show(struct seq_file *s, void *d)
{
int i, ret = 0;
u32 bw_total[PPMU_PMNCNT_MAX];
struct exynos5_ppmu_handle *handle;
unsigned long flags;
memset(bw_total, 0, sizeof(bw_total));
handle = exynos5_ppmu_get();
msleep(100);
spin_lock_irqsave(&exynos5_ppmu_lock, flags);
exynos5_ppmu_update(0, PPMU_END - 1);
for (i = 0; i < PPMU_END; i++)
exynos5_ppmu_debug_show_one(s, exynos5_ppmu_name[i],
&handle->ppmu[i], bw_total);
seq_printf(s, "%-10s %-10s %4u.%02u MBps\n", "total", "read+write",
bw_total[PPMU_PMNCNT3] >> FIXED_POINT_OFFSET,
(bw_total[PPMU_PMNCNT3] & FIXED_POINT_MASK) *
100 / (1 << FIXED_POINT_OFFSET));
seq_printf(s, "%-10s %-10s %4u.%02u MBps\n", "", "read",
bw_total[PPMU_PMNCNT0] >> FIXED_POINT_OFFSET,
(bw_total[PPMU_PMNCNT0] & FIXED_POINT_MASK) *
100 / (1 << FIXED_POINT_OFFSET));
seq_printf(s, "%-10s %-10s %4u.%02u MBps\n", "", "write",
bw_total[PPMU_PMCCNT1] >> FIXED_POINT_OFFSET,
(bw_total[PPMU_PMCCNT1] & FIXED_POINT_MASK) *
100 / (1 << FIXED_POINT_OFFSET));
seq_printf(s, "\n");
spin_unlock_irqrestore(&exynos5_ppmu_lock, flags);
exynos5_ppmu_put(handle);
return ret;
}
static int exynos5_ppmu_debug_open(struct inode *inode, struct file *file)
{
return single_open(file, exynos5_ppmu_debug_show, inode->i_private);
}
const static struct file_operations exynos5_ppmu_debug_fops = {
.open = exynos5_ppmu_debug_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init exynos5_ppmu_debug_init(void)
{
debugfs_create_file("exynos5_bus_ppmu", S_IRUGO, NULL, NULL,
&exynos5_ppmu_debug_fops);
return 0;
}
late_initcall(exynos5_ppmu_debug_init);
|