Blame view

arch/sh/kernel/io_trapped.c 6.41 KB
e7cc9a734   Magnus Damm   sh: trapped io su...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * Trapped io support
   *
   * Copyright (C) 2008 Magnus Damm
   *
   * Intercept io operations by trapping.
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file "COPYING" in the main directory of this archive
   * for more details.
   */
  #include <linux/kernel.h>
  #include <linux/mm.h>
  #include <linux/bitops.h>
  #include <linux/vmalloc.h>
ecc14e8cf   Paul Mundt   sh: Symbol export...
16
  #include <linux/module.h>
eeee7853c   Paul Mundt   sh: Add a command...
17
  #include <linux/init.h>
e7cc9a734   Magnus Damm   sh: trapped io su...
18
19
20
21
22
23
24
  #include <asm/system.h>
  #include <asm/mmu_context.h>
  #include <asm/uaccess.h>
  #include <asm/io.h>
  #include <asm/io_trapped.h>
  
  #define TRAPPED_PAGES_MAX 16
e7cc9a734   Magnus Damm   sh: trapped io su...
25
26
27
  
  #ifdef CONFIG_HAS_IOPORT
  LIST_HEAD(trapped_io);
ecc14e8cf   Paul Mundt   sh: Symbol export...
28
  EXPORT_SYMBOL_GPL(trapped_io);
e7cc9a734   Magnus Damm   sh: trapped io su...
29
30
31
  #endif
  #ifdef CONFIG_HAS_IOMEM
  LIST_HEAD(trapped_mem);
ecc14e8cf   Paul Mundt   sh: Symbol export...
32
  EXPORT_SYMBOL_GPL(trapped_mem);
e7cc9a734   Magnus Damm   sh: trapped io su...
33
34
  #endif
  static DEFINE_SPINLOCK(trapped_lock);
eeee7853c   Paul Mundt   sh: Add a command...
35
36
37
38
39
40
41
42
  static int trapped_io_disable __read_mostly;
  
  static int __init trapped_io_setup(char *__unused)
  {
  	trapped_io_disable = 1;
  	return 1;
  }
  __setup("noiotrap", trapped_io_setup);
b2839ed83   Paul Mundt   sh: Fix up sectio...
43
  int register_trapped_io(struct trapped_io *tiop)
e7cc9a734   Magnus Damm   sh: trapped io su...
44
45
46
47
48
  {
  	struct resource *res;
  	unsigned long len = 0, flags = 0;
  	struct page *pages[TRAPPED_PAGES_MAX];
  	int k, n;
eeee7853c   Paul Mundt   sh: Add a command...
49
50
  	if (unlikely(trapped_io_disable))
  		return 0;
e7cc9a734   Magnus Damm   sh: trapped io su...
51
52
53
54
55
56
  	/* structure must be page aligned */
  	if ((unsigned long)tiop & (PAGE_SIZE - 1))
  		goto bad;
  
  	for (k = 0; k < tiop->num_resources; k++) {
  		res = tiop->resource + k;
28f65c11f   Joe Perches   treewide: Convert...
57
  		len += roundup(resource_size(res), PAGE_SIZE);
e7cc9a734   Magnus Damm   sh: trapped io su...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  		flags |= res->flags;
  	}
  
  	/* support IORESOURCE_IO _or_ MEM, not both */
  	if (hweight_long(flags) != 1)
  		goto bad;
  
  	n = len >> PAGE_SHIFT;
  
  	if (n >= TRAPPED_PAGES_MAX)
  		goto bad;
  
  	for (k = 0; k < n; k++)
  		pages[k] = virt_to_page(tiop);
  
  	tiop->virt_base = vmap(pages, n, VM_MAP, PAGE_NONE);
  	if (!tiop->virt_base)
  		goto bad;
  
  	len = 0;
  	for (k = 0; k < tiop->num_resources; k++) {
  		res = tiop->resource + k;
  		pr_info("trapped io 0x%08lx overrides %s 0x%08lx
  ",
  		       (unsigned long)(tiop->virt_base + len),
  		       res->flags & IORESOURCE_IO ? "io" : "mmio",
  		       (unsigned long)res->start);
28f65c11f   Joe Perches   treewide: Convert...
85
  		len += roundup(resource_size(res), PAGE_SIZE);
e7cc9a734   Magnus Damm   sh: trapped io su...
86
87
88
89
90
  	}
  
  	tiop->magic = IO_TRAPPED_MAGIC;
  	INIT_LIST_HEAD(&tiop->list);
  	spin_lock_irq(&trapped_lock);
86e4dd5ad   Paul Mundt   sh: support for p...
91
  #ifdef CONFIG_HAS_IOPORT
e7cc9a734   Magnus Damm   sh: trapped io su...
92
93
  	if (flags & IORESOURCE_IO)
  		list_add(&tiop->list, &trapped_io);
86e4dd5ad   Paul Mundt   sh: support for p...
94
95
  #endif
  #ifdef CONFIG_HAS_IOMEM
e7cc9a734   Magnus Damm   sh: trapped io su...
96
97
  	if (flags & IORESOURCE_MEM)
  		list_add(&tiop->list, &trapped_mem);
86e4dd5ad   Paul Mundt   sh: support for p...
98
  #endif
e7cc9a734   Magnus Damm   sh: trapped io su...
99
100
101
102
103
104
105
106
  	spin_unlock_irq(&trapped_lock);
  
  	return 0;
   bad:
  	pr_warning("unable to install trapped io filter
  ");
  	return -1;
  }
ecc14e8cf   Paul Mundt   sh: Symbol export...
107
  EXPORT_SYMBOL_GPL(register_trapped_io);
e7cc9a734   Magnus Damm   sh: trapped io su...
108
109
110
111
112
113
114
115
116
  
  void __iomem *match_trapped_io_handler(struct list_head *list,
  				       unsigned long offset,
  				       unsigned long size)
  {
  	unsigned long voffs;
  	struct trapped_io *tiop;
  	struct resource *res;
  	int k, len;
fd78a76ae   Stuart Menefy   sh: Rework irqfla...
117
  	unsigned long flags;
e7cc9a734   Magnus Damm   sh: trapped io su...
118

fd78a76ae   Stuart Menefy   sh: Rework irqfla...
119
  	spin_lock_irqsave(&trapped_lock, flags);
e7cc9a734   Magnus Damm   sh: trapped io su...
120
121
122
123
124
  	list_for_each_entry(tiop, list, list) {
  		voffs = 0;
  		for (k = 0; k < tiop->num_resources; k++) {
  			res = tiop->resource + k;
  			if (res->start == offset) {
fd78a76ae   Stuart Menefy   sh: Rework irqfla...
125
  				spin_unlock_irqrestore(&trapped_lock, flags);
e7cc9a734   Magnus Damm   sh: trapped io su...
126
127
  				return tiop->virt_base + voffs;
  			}
28f65c11f   Joe Perches   treewide: Convert...
128
  			len = resource_size(res);
e7cc9a734   Magnus Damm   sh: trapped io su...
129
130
131
  			voffs += roundup(len, PAGE_SIZE);
  		}
  	}
fd78a76ae   Stuart Menefy   sh: Rework irqfla...
132
  	spin_unlock_irqrestore(&trapped_lock, flags);
e7cc9a734   Magnus Damm   sh: trapped io su...
133
134
  	return NULL;
  }
ecc14e8cf   Paul Mundt   sh: Symbol export...
135
  EXPORT_SYMBOL_GPL(match_trapped_io_handler);
e7cc9a734   Magnus Damm   sh: trapped io su...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  
  static struct trapped_io *lookup_tiop(unsigned long address)
  {
  	pgd_t *pgd_k;
  	pud_t *pud_k;
  	pmd_t *pmd_k;
  	pte_t *pte_k;
  	pte_t entry;
  
  	pgd_k = swapper_pg_dir + pgd_index(address);
  	if (!pgd_present(*pgd_k))
  		return NULL;
  
  	pud_k = pud_offset(pgd_k, address);
  	if (!pud_present(*pud_k))
  		return NULL;
  
  	pmd_k = pmd_offset(pud_k, address);
  	if (!pmd_present(*pmd_k))
  		return NULL;
  
  	pte_k = pte_offset_kernel(pmd_k, address);
  	entry = *pte_k;
  
  	return pfn_to_kaddr(pte_pfn(entry));
  }
  
  static unsigned long lookup_address(struct trapped_io *tiop,
  				    unsigned long address)
  {
  	struct resource *res;
  	unsigned long vaddr = (unsigned long)tiop->virt_base;
  	unsigned long len;
  	int k;
  
  	for (k = 0; k < tiop->num_resources; k++) {
  		res = tiop->resource + k;
28f65c11f   Joe Perches   treewide: Convert...
173
  		len = roundup(resource_size(res), PAGE_SIZE);
e7cc9a734   Magnus Damm   sh: trapped io su...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  		if (address < (vaddr + len))
  			return res->start + (address - vaddr);
  		vaddr += len;
  	}
  	return 0;
  }
  
  static unsigned long long copy_word(unsigned long src_addr, int src_len,
  				    unsigned long dst_addr, int dst_len)
  {
  	unsigned long long tmp = 0;
  
  	switch (src_len) {
  	case 1:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
188
  		tmp = __raw_readb(src_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
189
190
  		break;
  	case 2:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
191
  		tmp = __raw_readw(src_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
192
193
  		break;
  	case 4:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
194
  		tmp = __raw_readl(src_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
195
196
  		break;
  	case 8:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
197
  		tmp = __raw_readq(src_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
198
199
200
201
202
  		break;
  	}
  
  	switch (dst_len) {
  	case 1:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
203
  		__raw_writeb(tmp, dst_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
204
205
  		break;
  	case 2:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
206
  		__raw_writew(tmp, dst_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
207
208
  		break;
  	case 4:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
209
  		__raw_writel(tmp, dst_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
210
211
  		break;
  	case 8:
9d56dd3b0   Paul Mundt   sh: Mass ctrl_in/...
212
  		__raw_writeq(tmp, dst_addr);
e7cc9a734   Magnus Damm   sh: trapped io su...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  		break;
  	}
  
  	return tmp;
  }
  
  static unsigned long from_device(void *dst, const void *src, unsigned long cnt)
  {
  	struct trapped_io *tiop;
  	unsigned long src_addr = (unsigned long)src;
  	unsigned long long tmp;
  
  	pr_debug("trapped io read 0x%08lx (%ld)
  ", src_addr, cnt);
  	tiop = lookup_tiop(src_addr);
  	WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC));
  
  	src_addr = lookup_address(tiop, src_addr);
  	if (!src_addr)
  		return cnt;
f1cdd63fe   Paul Mundt   sh: Use max_t in ...
233
234
235
  	tmp = copy_word(src_addr,
  			max_t(unsigned long, cnt,
  			      (tiop->minimum_bus_width / 8)),
e7cc9a734   Magnus Damm   sh: trapped io su...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  			(unsigned long)dst, cnt);
  
  	pr_debug("trapped io read 0x%08lx -> 0x%08llx
  ", src_addr, tmp);
  	return 0;
  }
  
  static unsigned long to_device(void *dst, const void *src, unsigned long cnt)
  {
  	struct trapped_io *tiop;
  	unsigned long dst_addr = (unsigned long)dst;
  	unsigned long long tmp;
  
  	pr_debug("trapped io write 0x%08lx (%ld)
  ", dst_addr, cnt);
  	tiop = lookup_tiop(dst_addr);
  	WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC));
  
  	dst_addr = lookup_address(tiop, dst_addr);
  	if (!dst_addr)
  		return cnt;
  
  	tmp = copy_word((unsigned long)src, cnt,
f1cdd63fe   Paul Mundt   sh: Use max_t in ...
259
260
  			dst_addr, max_t(unsigned long, cnt,
  					(tiop->minimum_bus_width / 8)));
e7cc9a734   Magnus Damm   sh: trapped io su...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  
  	pr_debug("trapped io write 0x%08lx -> 0x%08llx
  ", dst_addr, tmp);
  	return 0;
  }
  
  static struct mem_access trapped_io_access = {
  	from_device,
  	to_device,
  };
  
  int handle_trapped_io(struct pt_regs *regs, unsigned long address)
  {
  	mm_segment_t oldfs;
2bcfffa42   Paul Mundt   sh: Rename opcode...
275
  	insn_size_t instruction;
e7cc9a734   Magnus Damm   sh: trapped io su...
276
  	int tmp;
08b36c4a0   Paul Mundt   sh: Optimize runt...
277
278
  	if (trapped_io_disable)
  		return 0;
e7cc9a734   Magnus Damm   sh: trapped io su...
279
280
281
282
283
284
285
286
287
288
289
290
  	if (!lookup_tiop(address))
  		return 0;
  
  	WARN_ON(user_mode(regs));
  
  	oldfs = get_fs();
  	set_fs(KERNEL_DS);
  	if (copy_from_user(&instruction, (void *)(regs->pc),
  			   sizeof(instruction))) {
  		set_fs(oldfs);
  		return 0;
  	}
4aa5ac4ef   Matt Fleming   sh: Only shout ab...
291
  	tmp = handle_unaligned_access(instruction, regs,
ace2dc7d1   Paul Mundt   sh: wire up perf ...
292
  				      &trapped_io_access, 1, address);
e7cc9a734   Magnus Damm   sh: trapped io su...
293
294
295
  	set_fs(oldfs);
  	return tmp == 0;
  }