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