Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 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> |
Paul Mundt | ecc14e8 | 2008-02-12 16:02:02 +0900 | [diff] [blame] | 16 | #include <linux/module.h> |
Paul Mundt | eeee785 | 2009-04-02 12:31:16 +0900 | [diff] [blame] | 17 | #include <linux/init.h> |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 18 | #include <asm/mmu_context.h> |
| 19 | #include <asm/uaccess.h> |
| 20 | #include <asm/io.h> |
| 21 | #include <asm/io_trapped.h> |
| 22 | |
| 23 | #define TRAPPED_PAGES_MAX 16 |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 24 | |
Uwe Kleine-König | ce816fa | 2014-04-07 15:39:19 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_HAS_IOPORT_MAP |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 26 | LIST_HEAD(trapped_io); |
Paul Mundt | ecc14e8 | 2008-02-12 16:02:02 +0900 | [diff] [blame] | 27 | EXPORT_SYMBOL_GPL(trapped_io); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 28 | #endif |
| 29 | #ifdef CONFIG_HAS_IOMEM |
| 30 | LIST_HEAD(trapped_mem); |
Paul Mundt | ecc14e8 | 2008-02-12 16:02:02 +0900 | [diff] [blame] | 31 | EXPORT_SYMBOL_GPL(trapped_mem); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 32 | #endif |
| 33 | static DEFINE_SPINLOCK(trapped_lock); |
| 34 | |
Paul Mundt | eeee785 | 2009-04-02 12:31:16 +0900 | [diff] [blame] | 35 | static int trapped_io_disable __read_mostly; |
| 36 | |
| 37 | static int __init trapped_io_setup(char *__unused) |
| 38 | { |
| 39 | trapped_io_disable = 1; |
| 40 | return 1; |
| 41 | } |
| 42 | __setup("noiotrap", trapped_io_setup); |
| 43 | |
Paul Mundt | b2839ed | 2008-03-06 12:43:38 +0900 | [diff] [blame] | 44 | int register_trapped_io(struct trapped_io *tiop) |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 45 | { |
| 46 | struct resource *res; |
| 47 | unsigned long len = 0, flags = 0; |
| 48 | struct page *pages[TRAPPED_PAGES_MAX]; |
| 49 | int k, n; |
| 50 | |
Paul Mundt | eeee785 | 2009-04-02 12:31:16 +0900 | [diff] [blame] | 51 | if (unlikely(trapped_io_disable)) |
| 52 | return 0; |
| 53 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 54 | /* structure must be page aligned */ |
| 55 | if ((unsigned long)tiop & (PAGE_SIZE - 1)) |
| 56 | goto bad; |
| 57 | |
| 58 | for (k = 0; k < tiop->num_resources; k++) { |
| 59 | res = tiop->resource + k; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 60 | len += roundup(resource_size(res), PAGE_SIZE); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 61 | flags |= res->flags; |
| 62 | } |
| 63 | |
| 64 | /* support IORESOURCE_IO _or_ MEM, not both */ |
| 65 | if (hweight_long(flags) != 1) |
| 66 | goto bad; |
| 67 | |
| 68 | n = len >> PAGE_SHIFT; |
| 69 | |
| 70 | if (n >= TRAPPED_PAGES_MAX) |
| 71 | goto bad; |
| 72 | |
| 73 | for (k = 0; k < n; k++) |
| 74 | pages[k] = virt_to_page(tiop); |
| 75 | |
| 76 | tiop->virt_base = vmap(pages, n, VM_MAP, PAGE_NONE); |
| 77 | if (!tiop->virt_base) |
| 78 | goto bad; |
| 79 | |
| 80 | len = 0; |
| 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); |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 87 | len += roundup(resource_size(res), PAGE_SIZE); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | tiop->magic = IO_TRAPPED_MAGIC; |
| 91 | INIT_LIST_HEAD(&tiop->list); |
| 92 | spin_lock_irq(&trapped_lock); |
Uwe Kleine-König | ce816fa | 2014-04-07 15:39:19 -0700 | [diff] [blame] | 93 | #ifdef CONFIG_HAS_IOPORT_MAP |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 94 | if (flags & IORESOURCE_IO) |
| 95 | list_add(&tiop->list, &trapped_io); |
Paul Mundt | 86e4dd5 | 2010-05-25 20:06:13 +0900 | [diff] [blame] | 96 | #endif |
| 97 | #ifdef CONFIG_HAS_IOMEM |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 98 | if (flags & IORESOURCE_MEM) |
| 99 | list_add(&tiop->list, &trapped_mem); |
Paul Mundt | 86e4dd5 | 2010-05-25 20:06:13 +0900 | [diff] [blame] | 100 | #endif |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 101 | spin_unlock_irq(&trapped_lock); |
| 102 | |
| 103 | return 0; |
| 104 | bad: |
| 105 | pr_warning("unable to install trapped io filter\n"); |
| 106 | return -1; |
| 107 | } |
Paul Mundt | ecc14e8 | 2008-02-12 16:02:02 +0900 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(register_trapped_io); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 109 | |
| 110 | void __iomem *match_trapped_io_handler(struct list_head *list, |
| 111 | unsigned long offset, |
| 112 | unsigned long size) |
| 113 | { |
| 114 | unsigned long voffs; |
| 115 | struct trapped_io *tiop; |
| 116 | struct resource *res; |
| 117 | int k, len; |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 118 | unsigned long flags; |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 119 | |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 120 | spin_lock_irqsave(&trapped_lock, flags); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 121 | list_for_each_entry(tiop, list, list) { |
| 122 | voffs = 0; |
| 123 | for (k = 0; k < tiop->num_resources; k++) { |
| 124 | res = tiop->resource + k; |
| 125 | if (res->start == offset) { |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 126 | spin_unlock_irqrestore(&trapped_lock, flags); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 127 | return tiop->virt_base + voffs; |
| 128 | } |
| 129 | |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 130 | len = resource_size(res); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 131 | voffs += roundup(len, PAGE_SIZE); |
| 132 | } |
| 133 | } |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 134 | spin_unlock_irqrestore(&trapped_lock, flags); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
Paul Mundt | ecc14e8 | 2008-02-12 16:02:02 +0900 | [diff] [blame] | 137 | EXPORT_SYMBOL_GPL(match_trapped_io_handler); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 138 | |
| 139 | static struct trapped_io *lookup_tiop(unsigned long address) |
| 140 | { |
| 141 | pgd_t *pgd_k; |
| 142 | pud_t *pud_k; |
| 143 | pmd_t *pmd_k; |
| 144 | pte_t *pte_k; |
| 145 | pte_t entry; |
| 146 | |
| 147 | pgd_k = swapper_pg_dir + pgd_index(address); |
| 148 | if (!pgd_present(*pgd_k)) |
| 149 | return NULL; |
| 150 | |
| 151 | pud_k = pud_offset(pgd_k, address); |
| 152 | if (!pud_present(*pud_k)) |
| 153 | return NULL; |
| 154 | |
| 155 | pmd_k = pmd_offset(pud_k, address); |
| 156 | if (!pmd_present(*pmd_k)) |
| 157 | return NULL; |
| 158 | |
| 159 | pte_k = pte_offset_kernel(pmd_k, address); |
| 160 | entry = *pte_k; |
| 161 | |
| 162 | return pfn_to_kaddr(pte_pfn(entry)); |
| 163 | } |
| 164 | |
| 165 | static unsigned long lookup_address(struct trapped_io *tiop, |
| 166 | unsigned long address) |
| 167 | { |
| 168 | struct resource *res; |
| 169 | unsigned long vaddr = (unsigned long)tiop->virt_base; |
| 170 | unsigned long len; |
| 171 | int k; |
| 172 | |
| 173 | for (k = 0; k < tiop->num_resources; k++) { |
| 174 | res = tiop->resource + k; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 175 | len = roundup(resource_size(res), PAGE_SIZE); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 176 | if (address < (vaddr + len)) |
| 177 | return res->start + (address - vaddr); |
| 178 | vaddr += len; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static unsigned long long copy_word(unsigned long src_addr, int src_len, |
| 184 | unsigned long dst_addr, int dst_len) |
| 185 | { |
| 186 | unsigned long long tmp = 0; |
| 187 | |
| 188 | switch (src_len) { |
| 189 | case 1: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 190 | tmp = __raw_readb(src_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 191 | break; |
| 192 | case 2: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 193 | tmp = __raw_readw(src_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 194 | break; |
| 195 | case 4: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 196 | tmp = __raw_readl(src_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 197 | break; |
| 198 | case 8: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 199 | tmp = __raw_readq(src_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | |
| 203 | switch (dst_len) { |
| 204 | case 1: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 205 | __raw_writeb(tmp, dst_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 206 | break; |
| 207 | case 2: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 208 | __raw_writew(tmp, dst_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 209 | break; |
| 210 | case 4: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 211 | __raw_writel(tmp, dst_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 212 | break; |
| 213 | case 8: |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 214 | __raw_writeq(tmp, dst_addr); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | |
| 218 | return tmp; |
| 219 | } |
| 220 | |
| 221 | static unsigned long from_device(void *dst, const void *src, unsigned long cnt) |
| 222 | { |
| 223 | struct trapped_io *tiop; |
| 224 | unsigned long src_addr = (unsigned long)src; |
| 225 | unsigned long long tmp; |
| 226 | |
| 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)); |
| 230 | |
| 231 | src_addr = lookup_address(tiop, src_addr); |
| 232 | if (!src_addr) |
| 233 | return cnt; |
| 234 | |
Paul Mundt | f1cdd63 | 2008-02-09 19:10:52 +0900 | [diff] [blame] | 235 | tmp = copy_word(src_addr, |
| 236 | max_t(unsigned long, cnt, |
| 237 | (tiop->minimum_bus_width / 8)), |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 238 | (unsigned long)dst, cnt); |
| 239 | |
| 240 | pr_debug("trapped io read 0x%08lx -> 0x%08llx\n", src_addr, tmp); |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static unsigned long to_device(void *dst, const void *src, unsigned long cnt) |
| 245 | { |
| 246 | struct trapped_io *tiop; |
| 247 | unsigned long dst_addr = (unsigned long)dst; |
| 248 | unsigned long long tmp; |
| 249 | |
| 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)); |
| 253 | |
| 254 | dst_addr = lookup_address(tiop, dst_addr); |
| 255 | if (!dst_addr) |
| 256 | return cnt; |
| 257 | |
| 258 | tmp = copy_word((unsigned long)src, cnt, |
Paul Mundt | f1cdd63 | 2008-02-09 19:10:52 +0900 | [diff] [blame] | 259 | dst_addr, max_t(unsigned long, cnt, |
| 260 | (tiop->minimum_bus_width / 8))); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 261 | |
| 262 | pr_debug("trapped io write 0x%08lx -> 0x%08llx\n", dst_addr, tmp); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static struct mem_access trapped_io_access = { |
| 267 | from_device, |
| 268 | to_device, |
| 269 | }; |
| 270 | |
| 271 | int handle_trapped_io(struct pt_regs *regs, unsigned long address) |
| 272 | { |
| 273 | mm_segment_t oldfs; |
Paul Mundt | 2bcfffa | 2009-05-09 16:02:08 +0900 | [diff] [blame] | 274 | insn_size_t instruction; |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 275 | int tmp; |
| 276 | |
Paul Mundt | 08b36c4 | 2010-01-27 21:56:57 +0900 | [diff] [blame] | 277 | if (trapped_io_disable) |
| 278 | return 0; |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 279 | if (!lookup_tiop(address)) |
| 280 | return 0; |
| 281 | |
| 282 | WARN_ON(user_mode(regs)); |
| 283 | |
| 284 | oldfs = get_fs(); |
| 285 | set_fs(KERNEL_DS); |
| 286 | if (copy_from_user(&instruction, (void *)(regs->pc), |
| 287 | sizeof(instruction))) { |
| 288 | set_fs(oldfs); |
| 289 | return 0; |
| 290 | } |
| 291 | |
Matt Fleming | 4aa5ac4 | 2009-08-28 21:37:20 +0000 | [diff] [blame] | 292 | tmp = handle_unaligned_access(instruction, regs, |
Paul Mundt | ace2dc7 | 2010-10-13 06:55:26 +0900 | [diff] [blame] | 293 | &trapped_io_access, 1, address); |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 294 | set_fs(oldfs); |
| 295 | return tmp == 0; |
| 296 | } |