]>
Commit | Line | Data |
---|---|---|
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> |
906357f7 | 11 | #include <linux/security.h> |
8b7d89d0 | 12 | |
8b7d89d0 | 13 | static unsigned long mmio_address; |
3c2e2e68 | 14 | module_param_hw(mmio_address, ulong, iomem, 0); |
5ff93697 PP |
15 | MODULE_PARM_DESC(mmio_address, " Start address of the mapping of 16 kB " |
16 | "(or 8 MB if read_far is non-zero)."); | |
17 | ||
18 | static unsigned long read_far = 0x400100; | |
19 | module_param(read_far, ulong, 0); | |
20 | MODULE_PARM_DESC(read_far, " Offset of a 32-bit read within 8 MB " | |
21 | "(default: 0x400100)."); | |
8b7d89d0 | 22 | |
fab852aa PP |
23 | static unsigned v16(unsigned i) |
24 | { | |
25 | return i * 12 + 7; | |
26 | } | |
27 | ||
28 | static unsigned v32(unsigned i) | |
29 | { | |
30 | return i * 212371 + 13; | |
31 | } | |
32 | ||
8b7d89d0 PP |
33 | static void do_write_test(void __iomem *p) |
34 | { | |
35 | unsigned int i; | |
3c355863 | 36 | pr_info("write test.\n"); |
9e57fb35 | 37 | mmiotrace_printk("Write test.\n"); |
fab852aa | 38 | |
8b7d89d0 PP |
39 | for (i = 0; i < 256; i++) |
40 | iowrite8(i, p + i); | |
fab852aa | 41 | |
8b7d89d0 | 42 | for (i = 1024; i < (5 * 1024); i += 2) |
fab852aa PP |
43 | iowrite16(v16(i), p + i); |
44 | ||
8b7d89d0 | 45 | for (i = (5 * 1024); i < (16 * 1024); i += 4) |
fab852aa | 46 | iowrite32(v32(i), p + i); |
8b7d89d0 PP |
47 | } |
48 | ||
49 | static void do_read_test(void __iomem *p) | |
50 | { | |
51 | unsigned int i; | |
fab852aa | 52 | unsigned errs[3] = { 0 }; |
3c355863 | 53 | pr_info("read test.\n"); |
9e57fb35 | 54 | mmiotrace_printk("Read test.\n"); |
fab852aa | 55 | |
8b7d89d0 | 56 | for (i = 0; i < 256; i++) |
fab852aa PP |
57 | if (ioread8(p + i) != i) |
58 | ++errs[0]; | |
59 | ||
8b7d89d0 | 60 | for (i = 1024; i < (5 * 1024); i += 2) |
fab852aa PP |
61 | if (ioread16(p + i) != v16(i)) |
62 | ++errs[1]; | |
63 | ||
8b7d89d0 | 64 | for (i = (5 * 1024); i < (16 * 1024); i += 4) |
fab852aa PP |
65 | if (ioread32(p + i) != v32(i)) |
66 | ++errs[2]; | |
67 | ||
68 | mmiotrace_printk("Read errors: 8-bit %d, 16-bit %d, 32-bit %d.\n", | |
69 | errs[0], errs[1], errs[2]); | |
8b7d89d0 PP |
70 | } |
71 | ||
5ff93697 PP |
72 | static void do_read_far_test(void __iomem *p) |
73 | { | |
3c355863 | 74 | pr_info("read far test.\n"); |
5ff93697 PP |
75 | mmiotrace_printk("Read far test.\n"); |
76 | ||
77 | ioread32(p + read_far); | |
78 | } | |
79 | ||
80 | static void do_test(unsigned long size) | |
8b7d89d0 | 81 | { |
4bdc0d67 | 82 | void __iomem *p = ioremap(mmio_address, size); |
8b7d89d0 | 83 | if (!p) { |
3c355863 | 84 | pr_err("could not ioremap, aborting.\n"); |
8b7d89d0 PP |
85 | return; |
86 | } | |
9e57fb35 | 87 | mmiotrace_printk("ioremap returned %p.\n", p); |
8b7d89d0 PP |
88 | do_write_test(p); |
89 | do_read_test(p); | |
5ff93697 PP |
90 | if (read_far && read_far < size - 4) |
91 | do_read_far_test(p); | |
d61fc448 | 92 | iounmap(p); |
8b7d89d0 PP |
93 | } |
94 | ||
8b8f79b9 MS |
95 | /* |
96 | * Tests how mmiotrace behaves in face of multiple ioremap / iounmaps in | |
97 | * a short time. We had a bug in deferred freeing procedure which tried | |
98 | * to free this region multiple times (ioremap can reuse the same address | |
99 | * for many mappings). | |
100 | */ | |
101 | static void do_test_bulk_ioremapping(void) | |
102 | { | |
103 | void __iomem *p; | |
104 | int i; | |
105 | ||
106 | for (i = 0; i < 10; ++i) { | |
4bdc0d67 | 107 | p = ioremap(mmio_address, PAGE_SIZE); |
8b8f79b9 MS |
108 | if (p) |
109 | iounmap(p); | |
110 | } | |
111 | ||
112 | /* Force freeing. If it will crash we will know why. */ | |
113 | synchronize_rcu(); | |
114 | } | |
115 | ||
8b7d89d0 PP |
116 | static int __init init(void) |
117 | { | |
5ff93697 | 118 | unsigned long size = (read_far) ? (8 << 20) : (16 << 10); |
906357f7 DH |
119 | int ret = security_locked_down(LOCKDOWN_MMIOTRACE); |
120 | ||
121 | if (ret) | |
122 | return ret; | |
5ff93697 | 123 | |
8b7d89d0 | 124 | if (mmio_address == 0) { |
3c355863 JP |
125 | pr_err("you have to use the module argument mmio_address.\n"); |
126 | pr_err("DO NOT LOAD THIS MODULE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!\n"); | |
8b7d89d0 PP |
127 | return -ENXIO; |
128 | } | |
129 | ||
8d3bcc44 KW |
130 | pr_warn("WARNING: mapping %lu kB @ 0x%08lx in PCI address space, " |
131 | "and writing 16 kB of rubbish in there.\n", | |
132 | size >> 10, mmio_address); | |
5ff93697 | 133 | do_test(size); |
8b8f79b9 | 134 | do_test_bulk_ioremapping(); |
3c355863 | 135 | pr_info("All done.\n"); |
8b7d89d0 PP |
136 | return 0; |
137 | } | |
138 | ||
139 | static void __exit cleanup(void) | |
140 | { | |
3c355863 | 141 | pr_debug("unloaded.\n"); |
8b7d89d0 PP |
142 | } |
143 | ||
144 | module_init(init); | |
145 | module_exit(cleanup); | |
146 | MODULE_LICENSE("GPL"); |