-
Notifications
You must be signed in to change notification settings - Fork 9
/
peemuperf_entry.c
295 lines (243 loc) · 7.36 KB
/
peemuperf_entry.c
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
/*
* peemuperf - entry file
*
* Prabindh Sundareson ([email protected]) 2012
* */
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include "v7_pmu.h"
#include <linux/ioport.h>
#include <asm/io.h>
static void pmu_start(unsigned int event_array[],unsigned int count);
static void pmu_stop(void);
static int __peemuperf_init_checks(void);
static void __exit peemuperf_exit(void);
#define EMIFCNT_MAP_LEN 0x200
#define EMIFCNT_MAP_BASE_ADDR 0x4c000000
static int emifcnt = 0;
static int emif_readcount = 0;
static int emif_writecount = 0;
static resource_size_t emifcnt_reg_base;
static int emiflist[2] = {2, 3};
static int emiflist_count = 2;
#if defined(CONFIG_PROC_FS)
#include <linux/proc_fs.h>
#define MAX_PROC_BUF_LEN 1000
static char proc_buf[MAX_PROC_BUF_LEN];
#endif
static int evlist[6] = {1, 0x44, 3, 4, 5, 6};
static int evlist_count = 6;
static int available_evcount = 0;
static int evdelay = 500; //mSec
static int evdebug = 0;
static void pmu_start(unsigned int event_array[],unsigned int count)
{
int i;
#if 0
// For Issue - Zeroed out PMU counters
// Untested patch by tarteauxfraises - from - https://community.nxp.com/thread/302685
{
u32 val = 0;
// SDER
asm volatile("mrc p15, 0, %0, c1, c1, 1" : "=r" (val));
printk(KERN_ALERT "Lecture de SDER avant %u\n", val);
val = 0b11;
asm volatile("mcr p15, 0, %0, c1, c1, 1" : : "r" (val));
asm volatile("mrc p15, 0, %0, c1, c1, 1" : "=r" (val));
printk(KERN_ALERT "Lecture de SDER apres %u\n", val);
}
#endif
enable_pmu(); // Enable the PMU
reset_ccnt(); // Reset the CCNT (cycle counter)
reset_pmn(); // Reset the configurable counters
write_flags((1 << 31) | 0xf); //Reset overflow flags
for(i = 0;i < count;i ++)
{
pmn_config(i, event_array[i]);
}
ccnt_divider(1); //Enable divide by 64
enable_ccnt(); // Enable CCNT
for(i = 0;i < count;i ++)
enable_pmn(i);
}
static void pmu_stop(void)
{
int i;
unsigned int cycle_count, overflow;
unsigned int counters = available_evcount;
#if defined(CONFIG_PROC_FS)
int currProcBufLen = 0;
#endif
disable_ccnt();
for(i = 0;i < counters;i ++)
disable_pmn(i);
for(i = 0;i < counters;i ++)
{
if(evdebug == 1) printk("%u\t", read_pmn(i));
#if defined(CONFIG_PROC_FS)
currProcBufLen += sprintf(proc_buf + currProcBufLen,
"PMU.counter[%d]= %u\n", i, read_pmn(i));
#endif
}
cycle_count = read_ccnt(); // Read CCNT
overflow = read_flags(); //Check for overflow flag
if(evdebug == 1) printk("%u\t%u\t", overflow,cycle_count);
if(emifcnt == 1)
{
//Now read the READ+WRITE monitoring counters
emif_writecount = ioread32((void*)(emifcnt_reg_base+0x80));
emif_readcount = ioread32((void*)(emifcnt_reg_base+0x84));
}
if(evdebug == 1) printk("%u\t%u\n",
emif_readcount, emif_writecount);
#if defined(CONFIG_PROC_FS)
currProcBufLen += sprintf(proc_buf + currProcBufLen,
"PMU.overflow= %u\nPMU.CCNT= %u\n",
overflow,cycle_count);
currProcBufLen += sprintf(proc_buf + currProcBufLen,
"EMIF.readcount= %u\nEMIF.writecount= %u\n",
emif_readcount, emif_writecount);
#endif
}
/****************************************************************************
* Monitoring Thread
****************************************************************************/
static int peemuperf_thread(void* data)
{
if(evdebug == 1) printk("Entering thread loop...\n");
while(1)
{
if(kthread_should_stop()) break;
pmu_start(evlist, available_evcount);
msleep(evdelay);
pmu_stop();
msleep(10);
}
if(evdebug == 1) printk("Exiting thread...\n");
return 0;
}
#if defined(CONFIG_PROC_FS)
/****************************************************************************
* /proc entries
****************************************************************************/
static int peemuperf_proc_read(char *buf, char **start, off_t offset,
int len, int *unused_i, void *unused_v)
{
int c, outlen;
outlen = 0;
for(c = 0;c < MAX_PROC_BUF_LEN;c ++)
{
buf[c] = proc_buf[c];
}
return MAX_PROC_BUF_LEN;
}
int peemuperf_proc_read1(struct file *filp, char *buf, size_t count, loff_t *offp )
{
copy_to_user(buf, proc_buf, MAX_PROC_BUF_LEN);
return MAX_PROC_BUF_LEN;
}
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,9,0))
static struct file_operations peemuperf_proc_ops = {
.owner = THIS_MODULE,
.read = peemuperf_proc_read1
};
#endif
static __init int register_proc(void)
{
struct proc_dir_entry *proc_peemuperf;
int c;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,9,0))
proc_peemuperf = proc_create("peemuperf", S_IRUGO, NULL, &peemuperf_proc_ops);
#else
proc_peemuperf = create_proc_entry("peemuperf", S_IRUGO, NULL);
if (proc_peemuperf)
proc_peemuperf->read_proc = peemuperf_proc_read;
#endif
for(c = 0;c < MAX_PROC_BUF_LEN;c ++)
{
proc_buf[c] = 0;
}
return proc_peemuperf != NULL;
}
#endif /* CONFIG_PROC_FS */
/****************************************************************************
* Entry and Exit
****************************************************************************/
static struct task_struct *peemuperf_task;
static struct resource *emifcnt_regs;
static int __peemuperf_init_checks()
{
u32 regval1, regval2;
if(evdebug == 1) printk("Event inputs: %d %d %d %d\n", evlist[0],
evlist[1], evlist[2], evlist[3]);
available_evcount = getPMN();
peemuperf_task = kthread_run(peemuperf_thread,(void *)0,
"peemuperf_thread");
if(emifcnt == 1)
{
emifcnt_regs = request_mem_region(EMIFCNT_MAP_BASE_ADDR, EMIFCNT_MAP_LEN, "emifcnt");
if (!emifcnt_regs)
return 1;
emifcnt_reg_base = (resource_size_t)ioremap_nocache(emifcnt_regs->start, EMIFCNT_MAP_LEN);
if (!emifcnt_reg_base)
return 1;
//Now enable READ+WRITE monitoring counters using PERF_CNT_CFG
//(READ=0x2 for CNT_2, WRITE=0x3 for CNT_1)
regval2 = 0x8000;
regval2 |= (emiflist[0] & 0xF);
regval2 = (regval2 << 16) & 0xFFFF0000;
regval1 = 0x8000;
regval1 |= (emiflist[1] & 0xF);
iowrite32((regval2|regval1), emifcnt_reg_base+0x88);
}
return 0;
}
static int __init peemuperf_init(void)
{
unsigned int retVal = 0;
retVal = __peemuperf_init_checks();
if(retVal)
{
if(evdebug == 1) printk("%s: peemuperf checks failed\n", __FUNCTION__);
return -ENODEV;
}
else
{
if(evdebug == 1) printk("peemuperf checks passed...\n");
}
#if defined(CONFIG_PROC_FS)
register_proc();
#endif
return 0;
}
static void __exit peemuperf_exit()
{
kthread_stop(peemuperf_task);
#if defined(CONFIG_PROC_FS)
remove_proc_entry("peemuperf", NULL);
#endif
if(emifcnt == 1)
{
release_mem_region(EMIFCNT_MAP_BASE_ADDR, EMIFCNT_MAP_LEN);
iounmap((void __iomem *)emifcnt_reg_base);
}
}
/****************************************************************************
* Configuration
****************************************************************************/
module_param(evdelay, int, 500);
module_param(evdebug, int, 0);
module_param_array(evlist, int, &evlist_count, 0000);
module_param(emifcnt, int, 0);
module_param_array(emiflist, int, &emiflist_count, 0000);
late_initcall(peemuperf_init);
module_exit(peemuperf_exit);
MODULE_DESCRIPTION("PMU driver - insmod peemuperf.ko evdelay=500 evlist=1,68,3,4 evdebug=0 emifcnt=0 emiflist=2,3");
MODULE_AUTHOR("Prabindh Sundareson <[email protected]>");
MODULE_LICENSE("GPL v2");