]>
Commit | Line | Data |
---|---|---|
e7cc9a73 MD |
1 | /* |
2 | * Trapped io support | |
3 | * | |
4 | * Copyright (C) 2008 Magnus Damm | |
5 | * | |
6 | * Intercept io operations by trapping. | |
7 | * | |
8 | * This file is subject to the terms and conditions of the GNU General Public | |
9 | * License. See the file "COPYING" in the main directory of this archive | |
10 | * for more details. | |
11 | */ | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/mm.h> | |
14 | #include <linux/bitops.h> | |
15 | #include <linux/vmalloc.h> | |
ecc14e8c | 16 | #include <linux/module.h> |
eeee7853 | 17 | #include <linux/init.h> |
e7cc9a73 MD |
18 | #include <asm/system.h> |
19 | #include <asm/mmu_context.h> | |
20 | #include <asm/uaccess.h> | |
21 | #include <asm/io.h> | |
22 | #include <asm/io_trapped.h> | |
23 | ||
24 | #define TRAPPED_PAGES_MAX 16 | |
e7cc9a73 MD |
25 | |
26 | #ifdef CONFIG_HAS_IOPORT | |
27 | LIST_HEAD(trapped_io); | |
ecc14e8c | 28 | EXPORT_SYMBOL_GPL(trapped_io); |
e7cc9a73 MD |
29 | #endif |
30 | #ifdef CONFIG_HAS_IOMEM | |
31 | LIST_HEAD(trapped_mem); | |
ecc14e8c | 32 | EXPORT_SYMBOL_GPL(trapped_mem); |
e7cc9a73 MD |
33 | #endif |
34 | static DEFINE_SPINLOCK(trapped_lock); | |
35 | ||
eeee7853 PM |
36 | static int trapped_io_disable __read_mostly; |
37 | ||
38 | static int __init trapped_io_setup(char *__unused) | |
39 | { | |
40 | trapped_io_disable = 1; | |
41 | return 1; | |
42 | } | |
43 | __setup("noiotrap", trapped_io_setup); | |
44 | ||
b2839ed8 | 45 | int register_trapped_io(struct trapped_io *tiop) |
e7cc9a73 MD |
46 | { |
47 | struct resource *res; | |
48 | unsigned long len = 0, flags = 0; | |
49 | struct page *pages[TRAPPED_PAGES_MAX]; | |
50 | int k, n; | |
51 | ||
eeee7853 PM |
52 | if (unlikely(trapped_io_disable)) |
53 | return 0; | |
54 | ||
e7cc9a73 MD |
55 | /* structure must be page aligned */ |
56 | if ((unsigned long)tiop & (PAGE_SIZE - 1)) | |
57 | goto bad; | |
58 | ||
59 | for (k = 0; k < tiop->num_resources; k++) { | |
60 | res = tiop->resource + k; | |
61 | len += roundup((res->end - res->start) + 1, PAGE_SIZE); | |
62 | flags |= res->flags; | |
63 | } | |
64 | ||
65 | /* support IORESOURCE_IO _or_ MEM, not both */ | |
66 | if (hweight_long(flags) != 1) | |
67 | goto bad; | |
68 | ||
69 | n = len >> PAGE_SHIFT; | |
70 | ||
71 | if (n >= TRAPPED_PAGES_MAX) | |
72 | goto bad; | |
73 | ||
74 | for (k = 0; k < n; k++) | |
75 | pages[k] = virt_to_page(tiop); | |
76 | ||
77 | tiop->virt_base = vmap(pages, n, VM_MAP, PAGE_NONE); | |
78 | if (!tiop->virt_base) | |
79 | goto bad; | |
80 | ||
81 | len = 0; | |
82 | for (k = 0; k < tiop->num_resources; k++) { | |
83 | res = tiop->resource + k; | |
84 | pr_info("trapped io 0x%08lx overrides %s 0x%08lx\n", | |
85 | (unsigned long)(tiop->virt_base + len), | |
86 | res->flags & IORESOURCE_IO ? "io" : "mmio", | |
87 | (unsigned long)res->start); | |
88 | len += roundup((res->end - res->start) + 1, PAGE_SIZE); | |
89 | } | |
90 | ||
91 | tiop->magic = IO_TRAPPED_MAGIC; | |
92 | INIT_LIST_HEAD(&tiop->list); | |
93 | spin_lock_irq(&trapped_lock); | |
94 | if (flags & IORESOURCE_IO) | |
95 | list_add(&tiop->list, &trapped_io); | |
96 | if (flags & IORESOURCE_MEM) | |
97 | list_add(&tiop->list, &trapped_mem); | |
98 | spin_unlock_irq(&trapped_lock); | |
99 | ||
100 | return 0; | |
101 | bad: | |
102 | pr_warning("unable to install trapped io filter\n"); | |
103 | return -1; | |
104 | } | |
ecc14e8c | 105 | EXPORT_SYMBOL_GPL(register_trapped_io); |
e7cc9a73 MD |
106 | |
107 | void __iomem *match_trapped_io_handler(struct list_head *list, | |
108 | unsigned long offset, | |
109 | unsigned long size) | |
110 | { | |
111 | unsigned long voffs; | |
112 | struct trapped_io *tiop; | |
113 | struct resource *res; | |
114 | int k, len; | |
fd78a76a | 115 | unsigned long flags; |
e7cc9a73 | 116 | |
fd78a76a | 117 | spin_lock_irqsave(&trapped_lock, flags); |
e7cc9a73 MD |
118 | list_for_each_entry(tiop, list, list) { |
119 | voffs = 0; | |
120 | for (k = 0; k < tiop->num_resources; k++) { | |
121 | res = tiop->resource + k; | |
122 | if (res->start == offset) { | |
fd78a76a | 123 | spin_unlock_irqrestore(&trapped_lock, flags); |
e7cc9a73 MD |
124 | return tiop->virt_base + voffs; |
125 | } | |
126 | ||
127 | len = (res->end - res->start) + 1; | |
128 | voffs += roundup(len, PAGE_SIZE); | |
129 | } | |
130 | } | |
fd78a76a | 131 | spin_unlock_irqrestore(&trapped_lock, flags); |
e7cc9a73 MD |
132 | return NULL; |
133 | } | |
ecc14e8c | 134 | EXPORT_SYMBOL_GPL(match_trapped_io_handler); |
e7cc9a73 MD |
135 | |
136 | static struct trapped_io *lookup_tiop(unsigned long address) | |
137 | { | |
138 | pgd_t *pgd_k; | |
139 | pud_t *pud_k; | |
140 | pmd_t *pmd_k; | |
141 | pte_t *pte_k; | |
142 | pte_t entry; | |
143 | ||
144 | pgd_k = swapper_pg_dir + pgd_index(address); | |
145 | if (!pgd_present(*pgd_k)) | |
146 | return NULL; | |
147 | ||
148 | pud_k = pud_offset(pgd_k, address); | |
149 | if (!pud_present(*pud_k)) | |
150 | return NULL; | |
151 | ||
152 | pmd_k = pmd_offset(pud_k, address); | |
153 | if (!pmd_present(*pmd_k)) | |
154 | return NULL; | |
155 | ||
156 | pte_k = pte_offset_kernel(pmd_k, address); | |
157 | entry = *pte_k; | |
158 | ||
159 | return pfn_to_kaddr(pte_pfn(entry)); | |
160 | } | |
161 | ||
162 | static unsigned long lookup_address(struct trapped_io *tiop, | |
163 | unsigned long address) | |
164 | { | |
165 | struct resource *res; | |
166 | unsigned long vaddr = (unsigned long)tiop->virt_base; | |
167 | unsigned long len; | |
168 | int k; | |
169 | ||
170 | for (k = 0; k < tiop->num_resources; k++) { | |
171 | res = tiop->resource + k; | |
172 | len = roundup((res->end - res->start) + 1, PAGE_SIZE); | |
173 | if (address < (vaddr + len)) | |
174 | return res->start + (address - vaddr); | |
175 | vaddr += len; | |
176 | } | |
177 | return 0; | |
178 | } | |
179 | ||
180 | static unsigned long long copy_word(unsigned long src_addr, int src_len, | |
181 | unsigned long dst_addr, int dst_len) | |
182 | { | |
183 | unsigned long long tmp = 0; | |
184 | ||
185 | switch (src_len) { | |
186 | case 1: | |
9d56dd3b | 187 | tmp = __raw_readb(src_addr); |
e7cc9a73 MD |
188 | break; |
189 | case 2: | |
9d56dd3b | 190 | tmp = __raw_readw(src_addr); |
e7cc9a73 MD |
191 | break; |
192 | case 4: | |
9d56dd3b | 193 | tmp = __raw_readl(src_addr); |
e7cc9a73 MD |
194 | break; |
195 | case 8: | |
9d56dd3b | 196 | tmp = __raw_readq(src_addr); |
e7cc9a73 MD |
197 | break; |
198 | } | |
199 | ||
200 | switch (dst_len) { | |
201 | case 1: | |
9d56dd3b | 202 | __raw_writeb(tmp, dst_addr); |
e7cc9a73 MD |
203 | break; |
204 | case 2: | |
9d56dd3b | 205 | __raw_writew(tmp, dst_addr); |
e7cc9a73 MD |
206 | break; |
207 | case 4: | |
9d56dd3b | 208 | __raw_writel(tmp, dst_addr); |
e7cc9a73 MD |
209 | break; |
210 | case 8: | |
9d56dd3b | 211 | __raw_writeq(tmp, dst_addr); |
e7cc9a73 MD |
212 | break; |
213 | } | |
214 | ||
215 | return tmp; | |
216 | } | |
217 | ||
218 | static unsigned long from_device(void *dst, const void *src, unsigned long cnt) | |
219 | { | |
220 | struct trapped_io *tiop; | |
221 | unsigned long src_addr = (unsigned long)src; | |
222 | unsigned long long tmp; | |
223 | ||
224 | pr_debug("trapped io read 0x%08lx (%ld)\n", src_addr, cnt); | |
225 | tiop = lookup_tiop(src_addr); | |
226 | WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC)); | |
227 | ||
228 | src_addr = lookup_address(tiop, src_addr); | |
229 | if (!src_addr) | |
230 | return cnt; | |
231 | ||
f1cdd63f PM |
232 | tmp = copy_word(src_addr, |
233 | max_t(unsigned long, cnt, | |
234 | (tiop->minimum_bus_width / 8)), | |
e7cc9a73 MD |
235 | (unsigned long)dst, cnt); |
236 | ||
237 | pr_debug("trapped io read 0x%08lx -> 0x%08llx\n", src_addr, tmp); | |
238 | return 0; | |
239 | } | |
240 | ||
241 | static unsigned long to_device(void *dst, const void *src, unsigned long cnt) | |
242 | { | |
243 | struct trapped_io *tiop; | |
244 | unsigned long dst_addr = (unsigned long)dst; | |
245 | unsigned long long tmp; | |
246 | ||
247 | pr_debug("trapped io write 0x%08lx (%ld)\n", dst_addr, cnt); | |
248 | tiop = lookup_tiop(dst_addr); | |
249 | WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC)); | |
250 | ||
251 | dst_addr = lookup_address(tiop, dst_addr); | |
252 | if (!dst_addr) | |
253 | return cnt; | |
254 | ||
255 | tmp = copy_word((unsigned long)src, cnt, | |
f1cdd63f PM |
256 | dst_addr, max_t(unsigned long, cnt, |
257 | (tiop->minimum_bus_width / 8))); | |
e7cc9a73 MD |
258 | |
259 | pr_debug("trapped io write 0x%08lx -> 0x%08llx\n", dst_addr, tmp); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static struct mem_access trapped_io_access = { | |
264 | from_device, | |
265 | to_device, | |
266 | }; | |
267 | ||
268 | int handle_trapped_io(struct pt_regs *regs, unsigned long address) | |
269 | { | |
270 | mm_segment_t oldfs; | |
2bcfffa4 | 271 | insn_size_t instruction; |
e7cc9a73 MD |
272 | int tmp; |
273 | ||
274 | if (!lookup_tiop(address)) | |
275 | return 0; | |
276 | ||
277 | WARN_ON(user_mode(regs)); | |
278 | ||
279 | oldfs = get_fs(); | |
280 | set_fs(KERNEL_DS); | |
281 | if (copy_from_user(&instruction, (void *)(regs->pc), | |
282 | sizeof(instruction))) { | |
283 | set_fs(oldfs); | |
284 | return 0; | |
285 | } | |
286 | ||
4aa5ac4e MF |
287 | tmp = handle_unaligned_access(instruction, regs, |
288 | &trapped_io_access, 1); | |
e7cc9a73 MD |
289 | set_fs(oldfs); |
290 | return tmp == 0; | |
291 | } |