blob: 0881e1ff1e5835d2b057833503ea9fdf131e8cb1 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02002/*
Pekka Paalanenfab852a2009-03-01 16:09:14 +02003 * Written by Pekka Paalanen, 2008-2009 <pq@iki.fi>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02004 */
Joe Perches3c355862009-10-04 17:53:40 -07005
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02008#include <linux/module.h>
Pekka Paalanen970e6fa2008-05-12 21:21:03 +02009#include <linux/io.h>
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030010#include <linux/mmiotrace.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020011
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020012static unsigned long mmio_address;
David Howells3c2e2e62017-04-04 16:54:21 +010013module_param_hw(mmio_address, ulong, iomem, 0);
Pekka Paalanen5ff93692009-03-01 16:10:08 +020014MODULE_PARM_DESC(mmio_address, " Start address of the mapping of 16 kB "
15 "(or 8 MB if read_far is non-zero).");
16
17static unsigned long read_far = 0x400100;
18module_param(read_far, ulong, 0);
19MODULE_PARM_DESC(read_far, " Offset of a 32-bit read within 8 MB "
20 "(default: 0x400100).");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020021
Pekka Paalanenfab852a2009-03-01 16:09:14 +020022static unsigned v16(unsigned i)
23{
24 return i * 12 + 7;
25}
26
27static unsigned v32(unsigned i)
28{
29 return i * 212371 + 13;
30}
31
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020032static void do_write_test(void __iomem *p)
33{
34 unsigned int i;
Joe Perches3c355862009-10-04 17:53:40 -070035 pr_info("write test.\n");
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030036 mmiotrace_printk("Write test.\n");
Pekka Paalanenfab852a2009-03-01 16:09:14 +020037
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020038 for (i = 0; i < 256; i++)
39 iowrite8(i, p + i);
Pekka Paalanenfab852a2009-03-01 16:09:14 +020040
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020041 for (i = 1024; i < (5 * 1024); i += 2)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020042 iowrite16(v16(i), p + i);
43
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020044 for (i = (5 * 1024); i < (16 * 1024); i += 4)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020045 iowrite32(v32(i), p + i);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020046}
47
48static void do_read_test(void __iomem *p)
49{
50 unsigned int i;
Pekka Paalanenfab852a2009-03-01 16:09:14 +020051 unsigned errs[3] = { 0 };
Joe Perches3c355862009-10-04 17:53:40 -070052 pr_info("read test.\n");
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030053 mmiotrace_printk("Read test.\n");
Pekka Paalanenfab852a2009-03-01 16:09:14 +020054
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020055 for (i = 0; i < 256; i++)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020056 if (ioread8(p + i) != i)
57 ++errs[0];
58
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020059 for (i = 1024; i < (5 * 1024); i += 2)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020060 if (ioread16(p + i) != v16(i))
61 ++errs[1];
62
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020063 for (i = (5 * 1024); i < (16 * 1024); i += 4)
Pekka Paalanenfab852a2009-03-01 16:09:14 +020064 if (ioread32(p + i) != v32(i))
65 ++errs[2];
66
67 mmiotrace_printk("Read errors: 8-bit %d, 16-bit %d, 32-bit %d.\n",
68 errs[0], errs[1], errs[2]);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020069}
70
Pekka Paalanen5ff93692009-03-01 16:10:08 +020071static void do_read_far_test(void __iomem *p)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020072{
Joe Perches3c355862009-10-04 17:53:40 -070073 pr_info("read far test.\n");
Pekka Paalanen5ff93692009-03-01 16:10:08 +020074 mmiotrace_printk("Read far test.\n");
75
76 ioread32(p + read_far);
77}
78
79static void do_test(unsigned long size)
80{
81 void __iomem *p = ioremap_nocache(mmio_address, size);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020082 if (!p) {
Joe Perches3c355862009-10-04 17:53:40 -070083 pr_err("could not ioremap, aborting.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020084 return;
85 }
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030086 mmiotrace_printk("ioremap returned %p.\n", p);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020087 do_write_test(p);
88 do_read_test(p);
Pekka Paalanen5ff93692009-03-01 16:10:08 +020089 if (read_far && read_far < size - 4)
90 do_read_far_test(p);
Pekka Paalanend61fc442008-05-12 21:20:57 +020091 iounmap(p);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020092}
93
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +020094/*
95 * Tests how mmiotrace behaves in face of multiple ioremap / iounmaps in
96 * a short time. We had a bug in deferred freeing procedure which tried
97 * to free this region multiple times (ioremap can reuse the same address
98 * for many mappings).
99 */
100static void do_test_bulk_ioremapping(void)
101{
102 void __iomem *p;
103 int i;
104
105 for (i = 0; i < 10; ++i) {
106 p = ioremap_nocache(mmio_address, PAGE_SIZE);
107 if (p)
108 iounmap(p);
109 }
110
111 /* Force freeing. If it will crash we will know why. */
112 synchronize_rcu();
113}
114
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200115static int __init init(void)
116{
Pekka Paalanen5ff93692009-03-01 16:10:08 +0200117 unsigned long size = (read_far) ? (8 << 20) : (16 << 10);
118
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200119 if (mmio_address == 0) {
Joe Perches3c355862009-10-04 17:53:40 -0700120 pr_err("you have to use the module argument mmio_address.\n");
121 pr_err("DO NOT LOAD THIS MODULE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200122 return -ENXIO;
123 }
124
Joe Perches3c355862009-10-04 17:53:40 -0700125 pr_warning("WARNING: mapping %lu kB @ 0x%08lx in PCI address space, "
126 "and writing 16 kB of rubbish in there.\n",
127 size >> 10, mmio_address);
Pekka Paalanen5ff93692009-03-01 16:10:08 +0200128 do_test(size);
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200129 do_test_bulk_ioremapping();
Joe Perches3c355862009-10-04 17:53:40 -0700130 pr_info("All done.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200131 return 0;
132}
133
134static void __exit cleanup(void)
135{
Joe Perches3c355862009-10-04 17:53:40 -0700136 pr_debug("unloaded.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200137}
138
139module_init(init);
140module_exit(cleanup);
141MODULE_LICENSE("GPL");