treewide: Add SPDX license identifier for more missed files
[linux-2.6-block.git] / arch / x86 / mm / testmmiotrace.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
8b7d89d0 2/*
fab852aa 3 * Written by Pekka Paalanen, 2008-2009 <pq@iki.fi>
8b7d89d0 4 */
3c355863
JP
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8b7d89d0 8#include <linux/module.h>
970e6fa0 9#include <linux/io.h>
9e57fb35 10#include <linux/mmiotrace.h>
8b7d89d0 11
8b7d89d0 12static unsigned long mmio_address;
3c2e2e68 13module_param_hw(mmio_address, ulong, iomem, 0);
5ff93697
PP
14MODULE_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).");
8b7d89d0 21
fab852aa
PP
22static 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
8b7d89d0
PP
32static void do_write_test(void __iomem *p)
33{
34 unsigned int i;
3c355863 35 pr_info("write test.\n");
9e57fb35 36 mmiotrace_printk("Write test.\n");
fab852aa 37
8b7d89d0
PP
38 for (i = 0; i < 256; i++)
39 iowrite8(i, p + i);
fab852aa 40
8b7d89d0 41 for (i = 1024; i < (5 * 1024); i += 2)
fab852aa
PP
42 iowrite16(v16(i), p + i);
43
8b7d89d0 44 for (i = (5 * 1024); i < (16 * 1024); i += 4)
fab852aa 45 iowrite32(v32(i), p + i);
8b7d89d0
PP
46}
47
48static void do_read_test(void __iomem *p)
49{
50 unsigned int i;
fab852aa 51 unsigned errs[3] = { 0 };
3c355863 52 pr_info("read test.\n");
9e57fb35 53 mmiotrace_printk("Read test.\n");
fab852aa 54
8b7d89d0 55 for (i = 0; i < 256; i++)
fab852aa
PP
56 if (ioread8(p + i) != i)
57 ++errs[0];
58
8b7d89d0 59 for (i = 1024; i < (5 * 1024); i += 2)
fab852aa
PP
60 if (ioread16(p + i) != v16(i))
61 ++errs[1];
62
8b7d89d0 63 for (i = (5 * 1024); i < (16 * 1024); i += 4)
fab852aa
PP
64 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]);
8b7d89d0
PP
69}
70
5ff93697
PP
71static void do_read_far_test(void __iomem *p)
72{
3c355863 73 pr_info("read far test.\n");
5ff93697
PP
74 mmiotrace_printk("Read far test.\n");
75
76 ioread32(p + read_far);
77}
78
79static void do_test(unsigned long size)
8b7d89d0 80{
5ff93697 81 void __iomem *p = ioremap_nocache(mmio_address, size);
8b7d89d0 82 if (!p) {
3c355863 83 pr_err("could not ioremap, aborting.\n");
8b7d89d0
PP
84 return;
85 }
9e57fb35 86 mmiotrace_printk("ioremap returned %p.\n", p);
8b7d89d0
PP
87 do_write_test(p);
88 do_read_test(p);
5ff93697
PP
89 if (read_far && read_far < size - 4)
90 do_read_far_test(p);
d61fc448 91 iounmap(p);
8b7d89d0
PP
92}
93
8b8f79b9
MS
94/*
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
8b7d89d0
PP
115static int __init init(void)
116{
5ff93697
PP
117 unsigned long size = (read_far) ? (8 << 20) : (16 << 10);
118
8b7d89d0 119 if (mmio_address == 0) {
3c355863
JP
120 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");
8b7d89d0
PP
122 return -ENXIO;
123 }
124
3c355863
JP
125 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);
5ff93697 128 do_test(size);
8b8f79b9 129 do_test_bulk_ioremapping();
3c355863 130 pr_info("All done.\n");
8b7d89d0
PP
131 return 0;
132}
133
134static void __exit cleanup(void)
135{
3c355863 136 pr_debug("unloaded.\n");
8b7d89d0
PP
137}
138
139module_init(init);
140module_exit(cleanup);
141MODULE_LICENSE("GPL");