io-mapping.h revision 1.12 1 /* $NetBSD: io-mapping.h,v 1.12 2021/12/19 12:27:49 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 base; /* Linux API */
46 bus_size_t size; /* Linux API */
47 vaddr_t diom_va;
48 bool diom_atomic;
49 };
50
51 static inline bool
52 bus_space_io_mapping_init_wc(bus_space_tag_t bst, struct io_mapping *mapping,
53 bus_addr_t addr, bus_size_t size)
54 {
55 bus_size_t offset;
56
57 KASSERT(PAGE_SIZE <= size);
58 KASSERT(0 == (size & (PAGE_SIZE - 1)));
59 KASSERT(__type_fit(off_t, size));
60
61 /*
62 * XXX For x86: Reserve the region (bus_space_reserve) and set
63 * an MTRR to make it write-combining. Doesn't matter if we
64 * have PAT and we use pmap_kenter_pa, but matters if we don't
65 * have PAT or if we later make this use direct map.
66 */
67
68 /* Make sure the region is mappable. */
69 for (offset = 0; offset < size; offset += PAGE_SIZE) {
70 if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
71 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
72 == (paddr_t)-1)
73 return false;
74 }
75
76 /* Initialize the mapping record. */
77 mapping->diom_bst = bst;
78 mapping->base = addr;
79 mapping->size = size;
80 mapping->diom_atomic = false;
81
82 /* Allocate kva for one page. */
83 mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
84 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
85 KASSERT(mapping->diom_va != 0);
86
87 return true;
88 }
89
90 static inline void
91 io_mapping_fini(struct io_mapping *mapping)
92 {
93
94 KASSERT(!mapping->diom_atomic);
95
96 uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
97 mapping->diom_va = 0; /* paranoia */
98 }
99
100 static inline struct io_mapping *
101 bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
102 bus_size_t size)
103 {
104 struct io_mapping *mapping;
105
106 mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
107 if (!bus_space_io_mapping_init_wc(bst, mapping, addr, size)) {
108 kmem_free(mapping, sizeof(*mapping));
109 return NULL;
110 }
111
112 return mapping;
113 }
114
115 static inline void
116 io_mapping_free(struct io_mapping *mapping)
117 {
118
119 io_mapping_fini(mapping);
120 kmem_free(mapping, sizeof(*mapping));
121 }
122
123 static inline void *
124 io_mapping_map_wc(struct io_mapping *mapping, bus_addr_t offset,
125 bus_size_t size)
126 {
127 bus_size_t pg, npgs = size >> PAGE_SHIFT;
128 vaddr_t va;
129 paddr_t cookie;
130
131 KASSERT(0 == (offset & (PAGE_SIZE - 1)));
132 KASSERT(PAGE_SIZE <= mapping->size);
133 KASSERT(offset <= (mapping->size - PAGE_SIZE));
134 KASSERT(__type_fit(off_t, offset));
135
136 va = uvm_km_alloc(kernel_map, size, PAGE_SIZE,
137 UVM_KMF_VAONLY|UVM_KMF_WAITVA);
138 KASSERT(va != mapping->diom_va);
139 for (pg = 0; pg < npgs; pg++) {
140 cookie = bus_space_mmap(mapping->diom_bst, mapping->base,
141 offset + pg*PAGE_SIZE,
142 PROT_READ|PROT_WRITE,
143 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
144 KASSERT(cookie != (paddr_t)-1);
145
146 pmap_kenter_pa(va + pg*PAGE_SIZE, pmap_phys_address(cookie),
147 PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
148 }
149 pmap_update(pmap_kernel());
150
151 return (void *)va;
152 }
153
154 static inline void
155 io_mapping_unmap(struct io_mapping *mapping, void *ptr, bus_size_t size)
156 {
157 vaddr_t va = (vaddr_t)ptr;
158
159 KASSERT(mapping->diom_va != va);
160
161 pmap_kremove(va, size);
162 pmap_update(pmap_kernel());
163
164 uvm_km_free(kernel_map, va, size, UVM_KMF_VAONLY);
165 }
166
167 static inline void *
168 io_mapping_map_atomic_wc(struct io_mapping *mapping, bus_addr_t offset)
169 {
170 paddr_t cookie;
171
172 KASSERT(0 == (offset & (PAGE_SIZE - 1)));
173 KASSERT(PAGE_SIZE <= mapping->size);
174 KASSERT(offset <= (mapping->size - PAGE_SIZE));
175 KASSERT(__type_fit(off_t, offset));
176 KASSERT(!mapping->diom_atomic);
177
178 cookie = bus_space_mmap(mapping->diom_bst, mapping->base, offset,
179 PROT_READ|PROT_WRITE,
180 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
181 KASSERT(cookie != (paddr_t)-1);
182
183 pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
184 PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
185 pmap_update(pmap_kernel());
186
187 mapping->diom_atomic = true;
188 return (void *)mapping->diom_va;
189 }
190
191 static inline void
192 io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr __diagused)
193 {
194
195 KASSERT(mapping->diom_atomic);
196 KASSERT(mapping->diom_va == (vaddr_t)ptr);
197
198 pmap_kremove(mapping->diom_va, PAGE_SIZE);
199 pmap_update(pmap_kernel());
200
201 mapping->diom_atomic = false;
202 }
203
204 #endif /* _LINUX_IO_MAPPING_H_ */
205