summaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/prcmu-debug.c
blob: 4b26d911adc7dae53d42c20cac0eb5e339d2548e (plain)
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
/*
 * Copyright (C) ST-Ericsson SA 2011
 *
 * License Terms: GNU General Public License v2
 *
 * Author: Martin Persson for ST-Ericsson
 *         Etienne Carriere <etienne.carriere@stericsson.com> for ST-Ericsson
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/mfd/dbx500-prcmu.h>

#include <mach/hardware.h>

#define MAX_STATES 5
#define MAX_NAMELEN 16

struct state_history {
	ktime_t start;
	u32 state;
	u32 counter[MAX_STATES];
	u8 opps[MAX_STATES];
	int max_states;
	int req;
	bool reqs[MAX_STATES];
	ktime_t time[MAX_STATES];
	int state_names[MAX_STATES];
	char prefix[MAX_NAMELEN];
	spinlock_t lock;
};

static struct state_history ape_sh = {
	.prefix = "APE",
	.req = PRCMU_QOS_APE_OPP,
	.opps = {APE_50_OPP, APE_100_OPP},
	.state_names = {50, 100},
	.max_states = 2,
};

static struct state_history ddr_sh = {
	.prefix = "DDR",
	.req = PRCMU_QOS_DDR_OPP,
	.opps = {DDR_25_OPP, DDR_50_OPP, DDR_100_OPP},
	.state_names = {25, 50, 100},
	.max_states = 3,
};

static struct state_history arm_sh = {
	.prefix = "ARM",
	.req = PRCMU_QOS_ARM_OPP,
	.opps = {ARM_EXTCLK, ARM_50_OPP, ARM_100_OPP, ARM_MAX_OPP},
	.state_names = {25, 50, 100, 125},
	.max_states = 4,
};

static int ape_voltage_count;

static void log_set(struct state_history *sh, u8 opp)
{
	ktime_t now;
	ktime_t dtime;
	unsigned long flags;
	int state;

	now = ktime_get();
	spin_lock_irqsave(&sh->lock, flags);

	for (state = 0 ; sh->opps[state] != opp; state++)
		;
	BUG_ON(state >= sh->max_states);

	dtime = ktime_sub(now, sh->start);
	sh->time[sh->state] = ktime_add(sh->time[sh->state], dtime);
	sh->start = now;
	sh->counter[sh->state]++;
	sh->state = state;

	spin_unlock_irqrestore(&sh->lock, flags);
}

void prcmu_debug_ape_opp_log(u8 opp)
{
	log_set(&ape_sh, opp);
}

void prcmu_debug_ddr_opp_log(u8 opp)
{
	log_set(&ddr_sh, opp);
}

void prcmu_debug_arm_opp_log(u8 opp)
{
	log_set(&arm_sh, opp);
}

static void log_reset(struct state_history *sh)
{
	unsigned long flags;
	int i;

	pr_info("reset\n");

	spin_lock_irqsave(&sh->lock, flags);
	for (i = 0; i < sh->max_states; i++) {
		sh->counter[i] = 0;
		sh->time[i] = ktime_set(0, 0);
	}

	sh->start = ktime_get();
	spin_unlock_irqrestore(&sh->lock, flags);

}

static ssize_t ape_stats_write(struct file *file,
			   const char __user *user_buf,
			   size_t count, loff_t *ppos)
{
	log_reset(&ape_sh);
	return count;
}

static ssize_t ddr_stats_write(struct file *file,
			   const char __user *user_buf,
			   size_t count, loff_t *ppos)
{
	log_reset(&ddr_sh);
	return count;
}

static ssize_t arm_stats_write(struct file *file,
			   const char __user *user_buf,
			   size_t count, loff_t *ppos)
{
	log_reset(&arm_sh);
	return count;
}

static int log_print(struct seq_file *s, struct state_history *sh)
{
	int i;
	unsigned long flags;
	ktime_t total;
	ktime_t dtime;
	s64 t_ms;
	s64 perc;
	s64 total_ms;

	spin_lock_irqsave(&sh->lock, flags);

	dtime = ktime_sub(ktime_get(), sh->start);

	total = dtime;

	for (i = 0; i < sh->max_states; i++)
		total = ktime_add(total, sh->time[i]);
	total_ms = ktime_to_ms(total);

	for (i = 0; i < sh->max_states; i++) {
		ktime_t t = sh->time[i];
		if (sh->state == i)
			t = ktime_add(t, dtime);

		t_ms = ktime_to_ms(t);
		perc = 100 * t_ms;
		do_div(perc, total_ms);

		seq_printf(s, "%s OPP %d: # %u in %lld ms %d%%\n",
			   sh->prefix, sh->state_names[i],
			   sh->counter[i] + (int)(sh->state == i),
			   t_ms, (u32)perc);

	}
	spin_unlock_irqrestore(&sh->lock, flags);
	return 0;
}

static int ape_stats_print(struct seq_file *s, void *p)
{
	log_print(s, &ape_sh);
	return 0;
}

static int ddr_stats_print(struct seq_file *s, void *p)
{
	log_print(s, &ddr_sh);
	return 0;
}

static int arm_stats_print(struct seq_file *s, void *p)
{
	log_print(s, &arm_sh);
	return 0;
}

static int opp_read(struct seq_file *s, void *p)
{
	int opp;

	struct state_history *sh = (struct state_history *)s->private;

	switch (sh->req) {
	case PRCMU_QOS_DDR_OPP:
		opp = prcmu_get_ddr_opp();
		seq_printf(s, "%s (%d)\n",
			   (opp == DDR_100_OPP) ? "100%" :
			   (opp == DDR_50_OPP) ? "50%" :
			   (opp == DDR_25_OPP) ? "25%" :
			   "unknown", opp);
		break;
	case PRCMU_QOS_APE_OPP:
		opp = prcmu_get_ape_opp();
		seq_printf(s, "%s (%d)\n",
			   (opp == APE_100_OPP) ? "100%" :
			   (opp == APE_50_OPP) ? "50%" :
			   "unknown", opp);
		break;
	case PRCMU_QOS_ARM_OPP:
		opp = prcmu_get_arm_opp();
		seq_printf(s, "%s (%d)\n",
			   (opp == ARM_MAX_OPP) ? "max" :
			   (opp == ARM_MAX_FREQ100OPP) ? "max-freq100" :
			   (opp == ARM_100_OPP) ? "100%" :
			   (opp == ARM_50_OPP) ? "50%" :
			   (opp == ARM_EXTCLK) ? "25% (extclk)" :
			   "unknown", opp);
		break;
	default:
		break;
	}
	return 0;

}

static ssize_t opp_write(struct file *file,
			 const char __user *user_buf,
			 size_t count, loff_t *ppos)
{
	long unsigned i;
	int err;
	struct state_history *sh = (struct state_history *)
		((struct seq_file *)file->private_data)->private;

	err = kstrtoul_from_user(user_buf, count, 0, &i);

	if (err)
		return err;

	prcmu_qos_force_opp(sh->req, i);

	pr_info("prcmu debug: forced OPP for %s to %d\n", sh->prefix, (int)i);

	return count;
}

static int cpufreq_delay_read(struct seq_file *s, void *p)
{
	return seq_printf(s, "%lu\n", prcmu_qos_get_cpufreq_opp_delay());
}

static int ape_voltage_read(struct seq_file *s, void *p)
{
	return seq_printf(s, "This reference count only includes "
			  "requests via debugfs.\nCount: %d\n",
			  ape_voltage_count);
}

static ssize_t ape_voltage_write(struct file *file,
				   const char __user *user_buf,
				   size_t count, loff_t *ppos)
{
	long unsigned i;
	int err;

	err = kstrtoul_from_user(user_buf, count, 0, &i);

	if (err)
		return err;

	switch (i) {
	case 0:
		if (ape_voltage_count == 0)
			pr_info("prcmu debug: reference count is already 0\n");
		else {
			err = prcmu_request_ape_opp_100_voltage(false);
			if (err)
				pr_err("prcmu debug: drop request failed\n");
			else
				ape_voltage_count--;
		}
		break;
	case 1:
		err = prcmu_request_ape_opp_100_voltage(true);
		if (err)
			pr_err("prcmu debug: request failed\n");
		else
			ape_voltage_count++;
		break;
	default:
		pr_info("prcmu debug: value not equal to 0 or 1\n");
	}
	return count;
}

static ssize_t cpufreq_delay_write(struct file *file,
				   const char __user *user_buf,
				   size_t count, loff_t *ppos)
{
	int err;
	long unsigned i;

	err = kstrtoul_from_user(user_buf, count, 0, &i);

	if (err)
		return err;

	prcmu_qos_set_cpufreq_opp_delay(i);

	pr_info("prcmu debug: changed delay between cpufreq change and QoS "
		 "requirement to %lu.\n", i);

	return count;
}

/* These are only for u8500 */
#define PRCM_AVS_BASE		0x2FC
#define AVS_VBB_RET		0x0
#define AVS_VBB_MAX_OPP		0x1
#define AVS_VBB_100_OPP		0x2
#define AVS_VBB_50_OPP		0x3
#define AVS_VARM_MAX_OPP	0x4
#define AVS_VARM_100_OPP	0x5
#define AVS_VARM_50_OPP		0x6
#define AVS_VARM_RET		0x7
#define AVS_VAPE_100_OPP	0x8
#define AVS_VAPE_50_OPP		0x9
#define AVS_VMOD_100_OPP	0xA
#define AVS_VMOD_50_OPP		0xB
#define AVS_VSAFE		0xC
#define AVS_VSAFE_RET		0xD
#define AVS_SIZE		14

static int avs_read(struct seq_file *s, void *p)
{

	u8 avs[AVS_SIZE];
	void __iomem *tcdm_base;

	if (cpu_is_u8500()) {
		tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);

		memcpy_fromio(avs, tcdm_base + PRCM_AVS_BASE, AVS_SIZE);

		seq_printf(s, "VBB_RET      : 0x%02x\n", avs[AVS_VBB_RET]);
		seq_printf(s, "VBB_MAX_OPP  : 0x%02x\n", avs[AVS_VBB_MAX_OPP]);
		seq_printf(s, "VBB_100_OPP  : 0x%02x\n", avs[AVS_VBB_100_OPP]);
		seq_printf(s, "VBB_50_OPP   : 0x%02x\n", avs[AVS_VBB_50_OPP]);
		seq_printf(s, "VARM_MAX_OPP : 0x%02x\n", avs[AVS_VARM_MAX_OPP]);
		seq_printf(s, "VARM_100_OPP : 0x%02x\n", avs[AVS_VARM_100_OPP]);
		seq_printf(s, "VARM_50_OPP  : 0x%02x\n", avs[AVS_VARM_50_OPP]);
		seq_printf(s, "VARM_RET     : 0x%02x\n", avs[AVS_VARM_RET]);
		seq_printf(s, "VAPE_100_OPP : 0x%02x\n", avs[AVS_VAPE_100_OPP]);
		seq_printf(s, "VAPE_50_OPP  : 0x%02x\n", avs[AVS_VAPE_50_OPP]);
		seq_printf(s, "VMOD_100_OPP : 0x%02x\n", avs[AVS_VMOD_100_OPP]);
		seq_printf(s, "VMOD_50_OPP  : 0x%02x\n", avs[AVS_VMOD_50_OPP]);
		seq_printf(s, "VSAFE        : 0x%02x\n", avs[AVS_VSAFE]);
		seq_printf(s, "VSAFE_RET    : 0x%02x\n", avs[AVS_VSAFE_RET]);
	} else {
		seq_printf(s, "Only u8500 supported.\n");
	}

	return 0;
}

static int opp_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, opp_read, inode->i_private);
}

static int ape_stats_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, ape_stats_print, inode->i_private);
}

static int ddr_stats_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, ddr_stats_print, inode->i_private);
}

static int arm_stats_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, arm_stats_print, inode->i_private);
}

static int cpufreq_delay_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, cpufreq_delay_read, inode->i_private);
}

static int ape_voltage_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, ape_voltage_read, inode->i_private);
}

static int avs_open_file(struct inode *inode, struct file *file)
{
	return single_open(file, avs_read, inode->i_private);
}

static const struct file_operations opp_fops = {
	.open = opp_open_file,
	.write = opp_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations ape_stats_fops = {
	.open = ape_stats_open_file,
	.write = ape_stats_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations ddr_stats_fops = {
	.open = ddr_stats_open_file,
	.write = ddr_stats_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations arm_stats_fops = {
	.open = arm_stats_open_file,
	.write = arm_stats_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations cpufreq_delay_fops = {
	.open = cpufreq_delay_open_file,
	.write = cpufreq_delay_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations ape_voltage_fops = {
	.open = ape_voltage_open_file,
	.write = ape_voltage_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations avs_fops = {
	.open = avs_open_file,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int setup_debugfs(void)
{
	struct dentry *dir;
	struct dentry *file;

	dir = debugfs_create_dir("prcmu", NULL);
	if (IS_ERR_OR_NULL(dir))
		goto fail;

	file = debugfs_create_file("ape_stats", (S_IRUGO | S_IWUGO),
				   dir, NULL, &ape_stats_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("ddr_stats", (S_IRUGO | S_IWUGO),
				   dir, NULL, &ddr_stats_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("arm_stats", (S_IRUGO | S_IWUGO),
				   dir, NULL, &arm_stats_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("ape_opp", (S_IRUGO),
				   dir, (void *)&ape_sh,
				   &opp_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("ddr_opp", (S_IRUGO),
				   dir, (void *)&ddr_sh,
				   &opp_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("arm_opp", (S_IRUGO),
				   dir, (void *)&arm_sh,
				   &opp_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("opp_cpufreq_delay", (S_IRUGO),
				   dir, NULL, &cpufreq_delay_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("ape_voltage", (S_IRUGO),
				   dir, NULL, &ape_voltage_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	file = debugfs_create_file("avs",
				   (S_IRUGO),
				   dir, NULL, &avs_fops);
	if (IS_ERR_OR_NULL(file))
		goto fail;

	return 0;
fail:
	if (!IS_ERR_OR_NULL(dir))
		debugfs_remove_recursive(dir);

	pr_err("prcmu debug: debugfs entry failed\n");
	return -ENOMEM;
}

static __init int prcmu_debug_init(void)
{
	spin_lock_init(&ape_sh.lock);
	spin_lock_init(&ddr_sh.lock);
	spin_lock_init(&arm_sh.lock);
	ape_sh.start = ktime_get();
	ddr_sh.start = ktime_get();
	arm_sh.start = ktime_get();
	return 0;
}
arch_initcall(prcmu_debug_init);

static __init int prcmu_debug_debugfs_init(void)
{
	setup_debugfs();
	return 0;
}
late_initcall(prcmu_debug_debugfs_init);