4 * Copyright (C) 2008 Magnus Damm
6 * Intercept io operations by trapping.
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
12 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <asm/mmu_context.h>
19 #include <asm/uaccess.h>
21 #include <asm/io_trapped.h>
23 #define TRAPPED_PAGES_MAX 16
25 #ifdef CONFIG_HAS_IOPORT
26 LIST_HEAD(trapped_io
);
27 EXPORT_SYMBOL_GPL(trapped_io
);
29 #ifdef CONFIG_HAS_IOMEM
30 LIST_HEAD(trapped_mem
);
31 EXPORT_SYMBOL_GPL(trapped_mem
);
33 static DEFINE_SPINLOCK(trapped_lock
);
35 static int trapped_io_disable __read_mostly
;
37 static int __init
trapped_io_setup(char *__unused
)
39 trapped_io_disable
= 1;
42 __setup("noiotrap", trapped_io_setup
);
44 int register_trapped_io(struct trapped_io
*tiop
)
47 unsigned long len
= 0, flags
= 0;
48 struct page
*pages
[TRAPPED_PAGES_MAX
];
51 if (unlikely(trapped_io_disable
))
54 /* structure must be page aligned */
55 if ((unsigned long)tiop
& (PAGE_SIZE
- 1))
58 for (k
= 0; k
< tiop
->num_resources
; k
++) {
59 res
= tiop
->resource
+ k
;
60 len
+= roundup(resource_size(res
), PAGE_SIZE
);
64 /* support IORESOURCE_IO _or_ MEM, not both */
65 if (hweight_long(flags
) != 1)
68 n
= len
>> PAGE_SHIFT
;
70 if (n
>= TRAPPED_PAGES_MAX
)
73 for (k
= 0; k
< n
; k
++)
74 pages
[k
] = virt_to_page(tiop
);
76 tiop
->virt_base
= vmap(pages
, n
, VM_MAP
, PAGE_NONE
);
81 for (k
= 0; k
< tiop
->num_resources
; k
++) {
82 res
= tiop
->resource
+ k
;
83 pr_info("trapped io 0x%08lx overrides %s 0x%08lx\n",
84 (unsigned long)(tiop
->virt_base
+ len
),
85 res
->flags
& IORESOURCE_IO
? "io" : "mmio",
86 (unsigned long)res
->start
);
87 len
+= roundup(resource_size(res
), PAGE_SIZE
);
90 tiop
->magic
= IO_TRAPPED_MAGIC
;
91 INIT_LIST_HEAD(&tiop
->list
);
92 spin_lock_irq(&trapped_lock
);
93 #ifdef CONFIG_HAS_IOPORT
94 if (flags
& IORESOURCE_IO
)
95 list_add(&tiop
->list
, &trapped_io
);
97 #ifdef CONFIG_HAS_IOMEM
98 if (flags
& IORESOURCE_MEM
)
99 list_add(&tiop
->list
, &trapped_mem
);
101 spin_unlock_irq(&trapped_lock
);
105 pr_warning("unable to install trapped io filter\n");
108 EXPORT_SYMBOL_GPL(register_trapped_io
);
110 void __iomem
*match_trapped_io_handler(struct list_head
*list
,
111 unsigned long offset
,
115 struct trapped_io
*tiop
;
116 struct resource
*res
;
120 spin_lock_irqsave(&trapped_lock
, flags
);
121 list_for_each_entry(tiop
, list
, list
) {
123 for (k
= 0; k
< tiop
->num_resources
; k
++) {
124 res
= tiop
->resource
+ k
;
125 if (res
->start
== offset
) {
126 spin_unlock_irqrestore(&trapped_lock
, flags
);
127 return tiop
->virt_base
+ voffs
;
130 len
= resource_size(res
);
131 voffs
+= roundup(len
, PAGE_SIZE
);
134 spin_unlock_irqrestore(&trapped_lock
, flags
);
137 EXPORT_SYMBOL_GPL(match_trapped_io_handler
);
139 static struct trapped_io
*lookup_tiop(unsigned long address
)
147 pgd_k
= swapper_pg_dir
+ pgd_index(address
);
148 if (!pgd_present(*pgd_k
))
151 pud_k
= pud_offset(pgd_k
, address
);
152 if (!pud_present(*pud_k
))
155 pmd_k
= pmd_offset(pud_k
, address
);
156 if (!pmd_present(*pmd_k
))
159 pte_k
= pte_offset_kernel(pmd_k
, address
);
162 return pfn_to_kaddr(pte_pfn(entry
));
165 static unsigned long lookup_address(struct trapped_io
*tiop
,
166 unsigned long address
)
168 struct resource
*res
;
169 unsigned long vaddr
= (unsigned long)tiop
->virt_base
;
173 for (k
= 0; k
< tiop
->num_resources
; k
++) {
174 res
= tiop
->resource
+ k
;
175 len
= roundup(resource_size(res
), PAGE_SIZE
);
176 if (address
< (vaddr
+ len
))
177 return res
->start
+ (address
- vaddr
);
183 static unsigned long long copy_word(unsigned long src_addr
, int src_len
,
184 unsigned long dst_addr
, int dst_len
)
186 unsigned long long tmp
= 0;
190 tmp
= __raw_readb(src_addr
);
193 tmp
= __raw_readw(src_addr
);
196 tmp
= __raw_readl(src_addr
);
199 tmp
= __raw_readq(src_addr
);
205 __raw_writeb(tmp
, dst_addr
);
208 __raw_writew(tmp
, dst_addr
);
211 __raw_writel(tmp
, dst_addr
);
214 __raw_writeq(tmp
, dst_addr
);
221 static unsigned long from_device(void *dst
, const void *src
, unsigned long cnt
)
223 struct trapped_io
*tiop
;
224 unsigned long src_addr
= (unsigned long)src
;
225 unsigned long long tmp
;
227 pr_debug("trapped io read 0x%08lx (%ld)\n", src_addr
, cnt
);
228 tiop
= lookup_tiop(src_addr
);
229 WARN_ON(!tiop
|| (tiop
->magic
!= IO_TRAPPED_MAGIC
));
231 src_addr
= lookup_address(tiop
, src_addr
);
235 tmp
= copy_word(src_addr
,
236 max_t(unsigned long, cnt
,
237 (tiop
->minimum_bus_width
/ 8)),
238 (unsigned long)dst
, cnt
);
240 pr_debug("trapped io read 0x%08lx -> 0x%08llx\n", src_addr
, tmp
);
244 static unsigned long to_device(void *dst
, const void *src
, unsigned long cnt
)
246 struct trapped_io
*tiop
;
247 unsigned long dst_addr
= (unsigned long)dst
;
248 unsigned long long tmp
;
250 pr_debug("trapped io write 0x%08lx (%ld)\n", dst_addr
, cnt
);
251 tiop
= lookup_tiop(dst_addr
);
252 WARN_ON(!tiop
|| (tiop
->magic
!= IO_TRAPPED_MAGIC
));
254 dst_addr
= lookup_address(tiop
, dst_addr
);
258 tmp
= copy_word((unsigned long)src
, cnt
,
259 dst_addr
, max_t(unsigned long, cnt
,
260 (tiop
->minimum_bus_width
/ 8)));
262 pr_debug("trapped io write 0x%08lx -> 0x%08llx\n", dst_addr
, tmp
);
266 static struct mem_access trapped_io_access
= {
271 int handle_trapped_io(struct pt_regs
*regs
, unsigned long address
)
274 insn_size_t instruction
;
277 if (trapped_io_disable
)
279 if (!lookup_tiop(address
))
282 WARN_ON(user_mode(regs
));
286 if (copy_from_user(&instruction
, (void *)(regs
->pc
),
287 sizeof(instruction
))) {
292 tmp
= handle_unaligned_access(instruction
, regs
,
293 &trapped_io_access
, 1, address
);