blob: 38868adf07ea9d68e4f23b486f636575d728687c [file] [log] [blame]
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02001/*
Pekka Paalanenfab852a2009-03-01 16:09:14 +02002 * Written by Pekka Paalanen, 2008-2009 <pq@iki.fi>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02003 */
Joe Perches3c355862009-10-04 17:53:40 -07004
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02007#include <linux/module.h>
Pekka Paalanen970e6fa2008-05-12 21:21:03 +02008#include <linux/io.h>
Pekka Paalanen9e57fb32008-09-16 22:00:34 +03009#include <linux/mmiotrace.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020010
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020011static unsigned long mmio_address;
12module_param(mmio_address, ulong, 0);
Pekka Paalanen5ff93692009-03-01 16:10:08 +020013MODULE_PARM_DESC(mmio_address, " Start address of the mapping of 16 kB "
14 "(or 8 MB if read_far is non-zero).");
15
16static unsigned long read_far = 0x400100;
17module_param(read_far, ulong, 0);
18MODULE_PARM_DESC(read_far, " Offset of a 32-bit read within 8 MB "
19 "(default: 0x400100).");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020020
Pekka Paalanenfab852a2009-03-01 16:09:14 +020021static unsigned v16(unsigned i)
22{
23 return i * 12 + 7;
24}
25
26static unsigned v32(unsigned i)
27{
28 return i * 212371 + 13;
29}
30
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020031static void do_write_test(void __iomem *p)
32{
33 unsigned int i;
Joe Perches3c355862009-10-04 17:53:40 -070034 pr_info("write test.\n");
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030035 mmiotrace_printk("Write test.\n");
Pekka Paalanenfab852a2009-03-01 16:09:14 +020036
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020037 for (i = 0; i < 256; i++)
38 iowrite8(i, p + i);
Pekka Paalanenfab852a2009-03-01 16:09:14 +020039
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020040 for (i = 1024; i < (5 * 1024); i += 2)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020041 iowrite16(v16(i), p + i);
42
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020043 for (i = (5 * 1024); i < (16 * 1024); i += 4)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020044 iowrite32(v32(i), p + i);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020045}
46
47static void do_read_test(void __iomem *p)
48{
49 unsigned int i;
Pekka Paalanenfab852a2009-03-01 16:09:14 +020050 unsigned errs[3] = { 0 };
Joe Perches3c355862009-10-04 17:53:40 -070051 pr_info("read test.\n");
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030052 mmiotrace_printk("Read test.\n");
Pekka Paalanenfab852a2009-03-01 16:09:14 +020053
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020054 for (i = 0; i < 256; i++)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020055 if (ioread8(p + i) != i)
56 ++errs[0];
57
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020058 for (i = 1024; i < (5 * 1024); i += 2)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020059 if (ioread16(p + i) != v16(i))
60 ++errs[1];
61
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020062 for (i = (5 * 1024); i < (16 * 1024); i += 4)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020063 if (ioread32(p + i) != v32(i))
64 ++errs[2];
65
66 mmiotrace_printk("Read errors: 8-bit %d, 16-bit %d, 32-bit %d.\n",
67 errs[0], errs[1], errs[2]);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020068}
69
Pekka Paalanen5ff93692009-03-01 16:10:08 +020070static void do_read_far_test(void __iomem *p)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020071{
Joe Perches3c355862009-10-04 17:53:40 -070072 pr_info("read far test.\n");
Pekka Paalanen5ff93692009-03-01 16:10:08 +020073 mmiotrace_printk("Read far test.\n");
74
75 ioread32(p + read_far);
76}
77
78static void do_test(unsigned long size)
79{
80 void __iomem *p = ioremap_nocache(mmio_address, size);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020081 if (!p) {
Joe Perches3c355862009-10-04 17:53:40 -070082 pr_err("could not ioremap, aborting.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020083 return;
84 }
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030085 mmiotrace_printk("ioremap returned %p.\n", p);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020086 do_write_test(p);
87 do_read_test(p);
Pekka Paalanen5ff93692009-03-01 16:10:08 +020088 if (read_far && read_far < size - 4)
89 do_read_far_test(p);
Pekka Paalanend61fc442008-05-12 21:20:57 +020090 iounmap(p);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020091}
92
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +020093/*
94 * Tests how mmiotrace behaves in face of multiple ioremap / iounmaps in
95 * a short time. We had a bug in deferred freeing procedure which tried
96 * to free this region multiple times (ioremap can reuse the same address
97 * for many mappings).
98 */
99static void do_test_bulk_ioremapping(void)
100{
101 void __iomem *p;
102 int i;
103
104 for (i = 0; i < 10; ++i) {
105 p = ioremap_nocache(mmio_address, PAGE_SIZE);
106 if (p)
107 iounmap(p);
108 }
109
110 /* Force freeing. If it will crash we will know why. */
111 synchronize_rcu();
112}
113
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200114static int __init init(void)
115{
Pekka Paalanen5ff93692009-03-01 16:10:08 +0200116 unsigned long size = (read_far) ? (8 << 20) : (16 << 10);
117
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200118 if (mmio_address == 0) {
Joe Perches3c355862009-10-04 17:53:40 -0700119 pr_err("you have to use the module argument mmio_address.\n");
120 pr_err("DO NOT LOAD THIS MODULE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200121 return -ENXIO;
122 }
123
Joe Perches3c355862009-10-04 17:53:40 -0700124 pr_warning("WARNING: mapping %lu kB @ 0x%08lx in PCI address space, "
125 "and writing 16 kB of rubbish in there.\n",
126 size >> 10, mmio_address);
Pekka Paalanen5ff93692009-03-01 16:10:08 +0200127 do_test(size);
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200128 do_test_bulk_ioremapping();
Joe Perches3c355862009-10-04 17:53:40 -0700129 pr_info("All done.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200130 return 0;
131}
132
133static void __exit cleanup(void)
134{
Joe Perches3c355862009-10-04 17:53:40 -0700135 pr_debug("unloaded.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200136}
137
138module_init(init);
139module_exit(cleanup);
140MODULE_LICENSE("GPL");