1 /*	$NetBSD: io-mapping.h,v 1.5 2015/02/25 14:02:43 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_IO_MAPPING_H_
33 #define _LINUX_IO_MAPPING_H_
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kmem.h>
38 #include <sys/systm.h>
39 #include <sys/mman.h>
40 
41 #include <uvm/uvm_extern.h>
42 
43 struct io_mapping {
44 	bus_space_tag_t		diom_bst;
45 	bus_addr_t		diom_addr;
46 	bus_size_t		diom_size;
47 	vaddr_t			diom_va;
48 	bool			diom_mapped;
49 };
50 
51 static inline struct io_mapping *
bus_space_io_mapping_create_wc(bus_space_tag_t bst,bus_addr_t addr,bus_size_t size)52 bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
53     bus_size_t size)
54 {
55 	struct io_mapping *mapping;
56 	bus_size_t offset;
57 
58 	KASSERT(PAGE_SIZE <= size);
59 	KASSERT(0 == (size & (PAGE_SIZE - 1)));
60 	KASSERT(__type_fit(off_t, size));
61 
62 	/*
63 	 * XXX For x86: Reserve the region (bus_space_reserve) and set
64 	 * an MTRR to make it write-combining.  Doesn't matter if we
65 	 * have PAT and we use pmap_kenter_pa, but matters if we don't
66 	 * have PAT or if we later make this use direct map.
67 	 */
68 
69 	/* Make sure the region is mappable.  */
70 	for (offset = 0; offset < size; offset += PAGE_SIZE) {
71 		if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
72 			BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
73 		    == (paddr_t)-1)
74 			return NULL;
75 	}
76 
77 	/* Create a mapping record.  */
78 	mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
79 	mapping->diom_bst = bst;
80 	mapping->diom_addr = addr;
81 	mapping->diom_size = size;
82 	mapping->diom_mapped = false;
83 
84 	/* Allocate kva for one page.  */
85 	mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
86 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
87 	KASSERT(mapping->diom_va != 0);
88 
89 	return mapping;
90 }
91 
92 static inline void
io_mapping_free(struct io_mapping * mapping)93 io_mapping_free(struct io_mapping *mapping)
94 {
95 
96 	KASSERT(!mapping->diom_mapped);
97 
98 	uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
99 	kmem_free(mapping, sizeof(*mapping));
100 }
101 
102 static inline void *
io_mapping_map_wc(struct io_mapping * mapping,unsigned long offset)103 io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
104 {
105 	paddr_t cookie;
106 
107 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
108 	KASSERT(PAGE_SIZE <= mapping->diom_size);
109 	KASSERT(offset <= (mapping->diom_size - PAGE_SIZE));
110 	KASSERT(__type_fit(off_t, offset));
111 	KASSERT(!mapping->diom_mapped);
112 
113 	cookie = bus_space_mmap(mapping->diom_bst, mapping->diom_addr, offset,
114 	    PROT_READ|PROT_WRITE,
115 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
116 	KASSERT(cookie != (paddr_t)-1);
117 
118 	pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
119 	    PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
120 	pmap_update(pmap_kernel());
121 
122 	mapping->diom_mapped = true;
123 	return (void *)mapping->diom_va;
124 }
125 
126 static inline void
io_mapping_unmap(struct io_mapping * mapping,void * ptr __diagused)127 io_mapping_unmap(struct io_mapping *mapping, void *ptr __diagused)
128 {
129 
130 	KASSERT(mapping->diom_mapped);
131 	KASSERT(mapping->diom_va == (vaddr_t)ptr);
132 
133 	pmap_kremove(mapping->diom_va, PAGE_SIZE);
134 	pmap_update(pmap_kernel());
135 
136 	mapping->diom_mapped = false;
137 }
138 
139 static inline void *
io_mapping_map_atomic_wc(struct io_mapping * mapping,unsigned long offset)140 io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
141 {
142 
143 	return io_mapping_map_wc(mapping, offset);
144 }
145 
146 static inline void
io_mapping_unmap_atomic(struct io_mapping * mapping,void * ptr)147 io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr)
148 {
149 
150 	io_mapping_unmap(mapping, ptr);
151 }
152 
153 #endif  /* _LINUX_IO_MAPPING_H_ */
154