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