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