xref: /openbmc/linux/kernel/time/test_udelay.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2fd866e2bSJohn Stultz /*
3fd866e2bSJohn Stultz  * udelay() test kernel module
4fd866e2bSJohn Stultz  *
5fd866e2bSJohn Stultz  * Test is executed by writing and reading to /sys/kernel/debug/udelay_test
6fd866e2bSJohn Stultz  * Tests are configured by writing: USECS ITERATIONS
7fd866e2bSJohn Stultz  * Tests are executed by reading from the same file.
8fd866e2bSJohn Stultz  * Specifying usecs of 0 or negative values will run multiples tests.
9fd866e2bSJohn Stultz  *
10fd866e2bSJohn Stultz  * Copyright (C) 2014 Google, Inc.
11fd866e2bSJohn Stultz  */
12fd866e2bSJohn Stultz 
13fd866e2bSJohn Stultz #include <linux/debugfs.h>
14fd866e2bSJohn Stultz #include <linux/delay.h>
15fd866e2bSJohn Stultz #include <linux/ktime.h>
16fd866e2bSJohn Stultz #include <linux/module.h>
17fd866e2bSJohn Stultz #include <linux/uaccess.h>
18fd866e2bSJohn Stultz 
19fd866e2bSJohn Stultz #define DEFAULT_ITERATIONS 100
20fd866e2bSJohn Stultz 
21fd866e2bSJohn Stultz #define DEBUGFS_FILENAME "udelay_test"
22fd866e2bSJohn Stultz 
23fd866e2bSJohn Stultz static DEFINE_MUTEX(udelay_test_lock);
24fd866e2bSJohn Stultz static int udelay_test_usecs;
25fd866e2bSJohn Stultz static int udelay_test_iterations = DEFAULT_ITERATIONS;
26fd866e2bSJohn Stultz 
udelay_test_single(struct seq_file * s,int usecs,uint32_t iters)27fd866e2bSJohn Stultz static int udelay_test_single(struct seq_file *s, int usecs, uint32_t iters)
28fd866e2bSJohn Stultz {
29fd866e2bSJohn Stultz 	int min = 0, max = 0, fail_count = 0;
30fd866e2bSJohn Stultz 	uint64_t sum = 0;
31fd866e2bSJohn Stultz 	uint64_t avg;
32fd866e2bSJohn Stultz 	int i;
33fd866e2bSJohn Stultz 	/* Allow udelay to be up to 0.5% fast */
34fd866e2bSJohn Stultz 	int allowed_error_ns = usecs * 5;
35fd866e2bSJohn Stultz 
36fd866e2bSJohn Stultz 	for (i = 0; i < iters; ++i) {
374a19bd3dSArnd Bergmann 		s64 kt1, kt2;
38fd866e2bSJohn Stultz 		int time_passed;
39fd866e2bSJohn Stultz 
404a19bd3dSArnd Bergmann 		kt1 = ktime_get_ns();
41fd866e2bSJohn Stultz 		udelay(usecs);
424a19bd3dSArnd Bergmann 		kt2 = ktime_get_ns();
434a19bd3dSArnd Bergmann 		time_passed = kt2 - kt1;
44fd866e2bSJohn Stultz 
45fd866e2bSJohn Stultz 		if (i == 0 || time_passed < min)
46fd866e2bSJohn Stultz 			min = time_passed;
47fd866e2bSJohn Stultz 		if (i == 0 || time_passed > max)
48fd866e2bSJohn Stultz 			max = time_passed;
49fd866e2bSJohn Stultz 		if ((time_passed + allowed_error_ns) / 1000 < usecs)
50fd866e2bSJohn Stultz 			++fail_count;
51fd866e2bSJohn Stultz 		WARN_ON(time_passed < 0);
52fd866e2bSJohn Stultz 		sum += time_passed;
53fd866e2bSJohn Stultz 	}
54fd866e2bSJohn Stultz 
55fd866e2bSJohn Stultz 	avg = sum;
56fd866e2bSJohn Stultz 	do_div(avg, iters);
57fd866e2bSJohn Stultz 	seq_printf(s, "%d usecs x %d: exp=%d allowed=%d min=%d avg=%lld max=%d",
58fd866e2bSJohn Stultz 			usecs, iters, usecs * 1000,
59fd866e2bSJohn Stultz 			(usecs * 1000) - allowed_error_ns, min, avg, max);
60fd866e2bSJohn Stultz 	if (fail_count)
61fd866e2bSJohn Stultz 		seq_printf(s, " FAIL=%d", fail_count);
62fd866e2bSJohn Stultz 	seq_puts(s, "\n");
63fd866e2bSJohn Stultz 
64fd866e2bSJohn Stultz 	return 0;
65fd866e2bSJohn Stultz }
66fd866e2bSJohn Stultz 
udelay_test_show(struct seq_file * s,void * v)67fd866e2bSJohn Stultz static int udelay_test_show(struct seq_file *s, void *v)
68fd866e2bSJohn Stultz {
69fd866e2bSJohn Stultz 	int usecs;
70fd866e2bSJohn Stultz 	int iters;
71fd866e2bSJohn Stultz 	int ret = 0;
72fd866e2bSJohn Stultz 
73fd866e2bSJohn Stultz 	mutex_lock(&udelay_test_lock);
74fd866e2bSJohn Stultz 	usecs = udelay_test_usecs;
75fd866e2bSJohn Stultz 	iters = udelay_test_iterations;
76fd866e2bSJohn Stultz 	mutex_unlock(&udelay_test_lock);
77fd866e2bSJohn Stultz 
78fd866e2bSJohn Stultz 	if (usecs > 0 && iters > 0) {
79fd866e2bSJohn Stultz 		return udelay_test_single(s, usecs, iters);
80fd866e2bSJohn Stultz 	} else if (usecs == 0) {
814a19bd3dSArnd Bergmann 		struct timespec64 ts;
82fd866e2bSJohn Stultz 
834a19bd3dSArnd Bergmann 		ktime_get_ts64(&ts);
844a19bd3dSArnd Bergmann 		seq_printf(s, "udelay() test (lpj=%ld kt=%lld.%09ld)\n",
854a19bd3dSArnd Bergmann 				loops_per_jiffy, (s64)ts.tv_sec, ts.tv_nsec);
86fd866e2bSJohn Stultz 		seq_puts(s, "usage:\n");
87fd866e2bSJohn Stultz 		seq_puts(s, "echo USECS [ITERS] > " DEBUGFS_FILENAME "\n");
88fd866e2bSJohn Stultz 		seq_puts(s, "cat " DEBUGFS_FILENAME "\n");
89fd866e2bSJohn Stultz 	}
90fd866e2bSJohn Stultz 
91fd866e2bSJohn Stultz 	return ret;
92fd866e2bSJohn Stultz }
93fd866e2bSJohn Stultz 
udelay_test_open(struct inode * inode,struct file * file)94fd866e2bSJohn Stultz static int udelay_test_open(struct inode *inode, struct file *file)
95fd866e2bSJohn Stultz {
96fd866e2bSJohn Stultz 	return single_open(file, udelay_test_show, inode->i_private);
97fd866e2bSJohn Stultz }
98fd866e2bSJohn Stultz 
udelay_test_write(struct file * file,const char __user * buf,size_t count,loff_t * pos)99fd866e2bSJohn Stultz static ssize_t udelay_test_write(struct file *file, const char __user *buf,
100fd866e2bSJohn Stultz 		size_t count, loff_t *pos)
101fd866e2bSJohn Stultz {
102fd866e2bSJohn Stultz 	char lbuf[32];
103fd866e2bSJohn Stultz 	int ret;
104fd866e2bSJohn Stultz 	int usecs;
105fd866e2bSJohn Stultz 	int iters;
106fd866e2bSJohn Stultz 
107fd866e2bSJohn Stultz 	if (count >= sizeof(lbuf))
108fd866e2bSJohn Stultz 		return -EINVAL;
109fd866e2bSJohn Stultz 
110fd866e2bSJohn Stultz 	if (copy_from_user(lbuf, buf, count))
111fd866e2bSJohn Stultz 		return -EFAULT;
112fd866e2bSJohn Stultz 	lbuf[count] = '\0';
113fd866e2bSJohn Stultz 
114fd866e2bSJohn Stultz 	ret = sscanf(lbuf, "%d %d", &usecs, &iters);
115fd866e2bSJohn Stultz 	if (ret < 1)
116fd866e2bSJohn Stultz 		return -EINVAL;
117fd866e2bSJohn Stultz 	else if (ret < 2)
118fd866e2bSJohn Stultz 		iters = DEFAULT_ITERATIONS;
119fd866e2bSJohn Stultz 
120fd866e2bSJohn Stultz 	mutex_lock(&udelay_test_lock);
121fd866e2bSJohn Stultz 	udelay_test_usecs = usecs;
122fd866e2bSJohn Stultz 	udelay_test_iterations = iters;
123fd866e2bSJohn Stultz 	mutex_unlock(&udelay_test_lock);
124fd866e2bSJohn Stultz 
125fd866e2bSJohn Stultz 	return count;
126fd866e2bSJohn Stultz }
127fd866e2bSJohn Stultz 
128fd866e2bSJohn Stultz static const struct file_operations udelay_test_debugfs_ops = {
129fd866e2bSJohn Stultz 	.owner = THIS_MODULE,
130fd866e2bSJohn Stultz 	.open = udelay_test_open,
131fd866e2bSJohn Stultz 	.read = seq_read,
132fd866e2bSJohn Stultz 	.write = udelay_test_write,
133fd866e2bSJohn Stultz 	.llseek = seq_lseek,
134fd866e2bSJohn Stultz 	.release = single_release,
135fd866e2bSJohn Stultz };
136fd866e2bSJohn Stultz 
udelay_test_init(void)137fd866e2bSJohn Stultz static int __init udelay_test_init(void)
138fd866e2bSJohn Stultz {
139fd866e2bSJohn Stultz 	mutex_lock(&udelay_test_lock);
14044511ab3SGreg Kroah-Hartman 	debugfs_create_file(DEBUGFS_FILENAME, S_IRUSR, NULL, NULL,
14144511ab3SGreg Kroah-Hartman 			    &udelay_test_debugfs_ops);
142fd866e2bSJohn Stultz 	mutex_unlock(&udelay_test_lock);
143fd866e2bSJohn Stultz 
144fd866e2bSJohn Stultz 	return 0;
145fd866e2bSJohn Stultz }
146fd866e2bSJohn Stultz 
147fd866e2bSJohn Stultz module_init(udelay_test_init);
148fd866e2bSJohn Stultz 
udelay_test_exit(void)149fd866e2bSJohn Stultz static void __exit udelay_test_exit(void)
150fd866e2bSJohn Stultz {
151fd866e2bSJohn Stultz 	mutex_lock(&udelay_test_lock);
152*5b268d8aSGreg Kroah-Hartman 	debugfs_lookup_and_remove(DEBUGFS_FILENAME, NULL);
153fd866e2bSJohn Stultz 	mutex_unlock(&udelay_test_lock);
154fd866e2bSJohn Stultz }
155fd866e2bSJohn Stultz 
156fd866e2bSJohn Stultz module_exit(udelay_test_exit);
157fd866e2bSJohn Stultz 
158fd866e2bSJohn Stultz MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
159fd866e2bSJohn Stultz MODULE_LICENSE("GPL");
160