rumpdev_bus_space.c revision 1.10 1 1.10 pgoyette /* $NetBSD: rumpdev_bus_space.c,v 1.10 2019/01/27 02:08:48 pgoyette Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2013 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka #include <sys/cdefs.h>
29 1.9 alnsn __KERNEL_RCSID(0, "$NetBSD: rumpdev_bus_space.c,v 1.10 2019/01/27 02:08:48 pgoyette Exp $");
30 1.9 alnsn
31 1.2 pooka #include <sys/atomic.h>
32 1.1 pooka
33 1.1 pooka #include <sys/param.h>
34 1.1 pooka
35 1.1 pooka #include <dev/pci/pcivar.h>
36 1.1 pooka
37 1.1 pooka #include "pci_user.h"
38 1.1 pooka
39 1.5 pooka #if defined(RUMPCOMP_USERFEATURE_PCI_IOSPACE) \
40 1.5 pooka && (defined(__i386__) || defined(__x86_64__))
41 1.3 pooka #define IOSPACE_SUPPORTED
42 1.3 pooka #endif
43 1.3 pooka
44 1.1 pooka int
45 1.1 pooka bus_space_map(bus_space_tag_t bst, bus_addr_t address, bus_size_t size,
46 1.1 pooka int flags, bus_space_handle_t *handlep)
47 1.1 pooka {
48 1.1 pooka int rv;
49 1.1 pooka
50 1.1 pooka /*
51 1.1 pooka * I/O space we just "let it bli" in case someone wants to
52 1.1 pooka * map it (e.g. on Xen)
53 1.1 pooka *
54 1.1 pooka * Memory space needs to be mapped into our guest, so we
55 1.1 pooka * make a hypercall to request it.
56 1.1 pooka */
57 1.1 pooka if (bst == 0) {
58 1.3 pooka #ifdef IOSPACE_SUPPORTED
59 1.1 pooka *handlep = address;
60 1.1 pooka rv = 0;
61 1.3 pooka #else
62 1.3 pooka rv = ENOTSUP;
63 1.3 pooka #endif
64 1.1 pooka } else {
65 1.1 pooka *handlep = (bus_space_handle_t)rumpcomp_pci_map(address, size);
66 1.1 pooka rv = *handlep ? 0 : EINVAL;
67 1.1 pooka }
68 1.1 pooka
69 1.1 pooka return rv;
70 1.1 pooka }
71 1.1 pooka
72 1.1 pooka uint8_t
73 1.1 pooka bus_space_read_1(bus_space_tag_t bst, bus_space_handle_t bsh,
74 1.1 pooka bus_size_t offset)
75 1.1 pooka {
76 1.1 pooka uint8_t rv;
77 1.1 pooka
78 1.1 pooka if (bst == 0) {
79 1.3 pooka #ifdef IOSPACE_SUPPORTED
80 1.3 pooka unsigned short addr = bsh + offset;
81 1.8 msaitoh __asm__ __volatile__("inb %1, %0" : "=a"(rv) : "d"(addr));
82 1.3 pooka #else
83 1.3 pooka panic("IO space not supported");
84 1.3 pooka #endif
85 1.1 pooka } else {
86 1.1 pooka rv = *(volatile uint8_t *)(bsh + offset);
87 1.1 pooka }
88 1.1 pooka
89 1.1 pooka return rv;
90 1.1 pooka }
91 1.1 pooka
92 1.1 pooka uint16_t
93 1.1 pooka bus_space_read_2(bus_space_tag_t bst, bus_space_handle_t bsh,
94 1.1 pooka bus_size_t offset)
95 1.1 pooka {
96 1.1 pooka uint16_t rv;
97 1.1 pooka
98 1.1 pooka if (bst == 0) {
99 1.3 pooka #ifdef IOSPACE_SUPPORTED
100 1.3 pooka unsigned short addr = bsh + offset;
101 1.8 msaitoh __asm__ __volatile__("in %1, %0" : "=a"(rv) : "d"(addr));
102 1.3 pooka #else
103 1.3 pooka panic("IO space not supported");
104 1.3 pooka #endif
105 1.1 pooka } else {
106 1.1 pooka rv = *(volatile uint16_t *)(bsh + offset);
107 1.1 pooka }
108 1.1 pooka
109 1.1 pooka return rv;
110 1.1 pooka }
111 1.1 pooka
112 1.1 pooka uint32_t
113 1.1 pooka bus_space_read_4(bus_space_tag_t bst, bus_space_handle_t bsh,
114 1.1 pooka bus_size_t offset)
115 1.1 pooka {
116 1.1 pooka uint32_t rv;
117 1.1 pooka
118 1.1 pooka if (bst == 0) {
119 1.3 pooka #ifdef IOSPACE_SUPPORTED
120 1.1 pooka unsigned short addr = bsh + offset;
121 1.8 msaitoh __asm__ __volatile__("inl %1, %0" : "=a"(rv) : "d"(addr));
122 1.3 pooka #else
123 1.3 pooka panic("IO space not supported");
124 1.1 pooka #endif
125 1.1 pooka } else {
126 1.1 pooka rv = *(volatile uint32_t *)(bsh + offset);
127 1.1 pooka }
128 1.1 pooka
129 1.1 pooka return rv;
130 1.1 pooka }
131 1.1 pooka
132 1.1 pooka void
133 1.4 pooka bus_space_read_multi_1(bus_space_tag_t bst, bus_space_handle_t bsh,
134 1.4 pooka bus_size_t offset, uint8_t *datap, bus_size_t count)
135 1.4 pooka {
136 1.4 pooka
137 1.4 pooka while (count--) {
138 1.4 pooka *datap++ = bus_space_read_1(bst, bsh, offset);
139 1.6 pooka bus_space_barrier(bst, bsh, offset, 1, BUS_SPACE_BARRIER_READ);
140 1.4 pooka }
141 1.4 pooka }
142 1.4 pooka
143 1.4 pooka void
144 1.4 pooka bus_space_read_multi_2(bus_space_tag_t bst, bus_space_handle_t bsh,
145 1.4 pooka bus_size_t offset, uint16_t *datap, bus_size_t count)
146 1.4 pooka {
147 1.4 pooka
148 1.4 pooka while (count--) {
149 1.4 pooka *datap++ = bus_space_read_2(bst, bsh, offset);
150 1.6 pooka bus_space_barrier(bst, bsh, offset, 2, BUS_SPACE_BARRIER_READ);
151 1.4 pooka }
152 1.4 pooka }
153 1.4 pooka
154 1.4 pooka void
155 1.4 pooka bus_space_read_multi_4(bus_space_tag_t bst, bus_space_handle_t bsh,
156 1.4 pooka bus_size_t offset, uint32_t *datap, bus_size_t count)
157 1.4 pooka {
158 1.4 pooka
159 1.4 pooka while (count--) {
160 1.4 pooka *datap++ = bus_space_read_4(bst, bsh, offset);
161 1.6 pooka bus_space_barrier(bst, bsh, offset, 4, BUS_SPACE_BARRIER_READ);
162 1.4 pooka }
163 1.4 pooka }
164 1.4 pooka
165 1.4 pooka void
166 1.1 pooka bus_space_write_1(bus_space_tag_t bst, bus_space_handle_t bsh,
167 1.1 pooka bus_size_t offset, uint8_t v)
168 1.1 pooka {
169 1.1 pooka
170 1.1 pooka if (bst == 0) {
171 1.3 pooka #ifdef IOSPACE_SUPPORTED
172 1.3 pooka unsigned short addr = bsh + offset;
173 1.3 pooka __asm__ __volatile__("outb %0, %1" :: "a"(v), "d"(addr));
174 1.3 pooka #else
175 1.3 pooka panic("IO space not supported");
176 1.1 pooka #endif
177 1.1 pooka } else {
178 1.1 pooka *(volatile uint8_t *)(bsh + offset) = v;
179 1.1 pooka }
180 1.1 pooka }
181 1.1 pooka
182 1.1 pooka void
183 1.1 pooka bus_space_write_2(bus_space_tag_t bst, bus_space_handle_t bsh,
184 1.1 pooka bus_size_t offset, uint16_t v)
185 1.1 pooka {
186 1.1 pooka
187 1.1 pooka if (bst == 0) {
188 1.3 pooka #ifdef IOSPACE_SUPPORTED
189 1.3 pooka unsigned short addr = bsh + offset;
190 1.3 pooka __asm__ __volatile__("out %0, %1" :: "a"(v), "d"(addr));
191 1.3 pooka #else
192 1.3 pooka panic("IO space not supported");
193 1.1 pooka #endif
194 1.1 pooka } else {
195 1.1 pooka *(volatile uint16_t *)(bsh + offset) = v;
196 1.1 pooka }
197 1.1 pooka }
198 1.1 pooka
199 1.1 pooka void
200 1.1 pooka bus_space_write_4(bus_space_tag_t bst, bus_space_handle_t bsh,
201 1.1 pooka bus_size_t offset, uint32_t v)
202 1.1 pooka {
203 1.1 pooka
204 1.1 pooka if (bst == 0) {
205 1.3 pooka #ifdef IOSPACE_SUPPORTED
206 1.1 pooka unsigned short addr = bsh + offset;
207 1.1 pooka __asm__ __volatile__("outl %0, %1" :: "a"(v), "d"(addr));
208 1.3 pooka #else
209 1.3 pooka panic("IO space not supported");
210 1.1 pooka #endif
211 1.1 pooka } else {
212 1.1 pooka *(volatile uint32_t *)(bsh + offset) = v;
213 1.1 pooka }
214 1.1 pooka }
215 1.1 pooka
216 1.4 pooka void
217 1.4 pooka bus_space_write_multi_1(bus_space_tag_t bst, bus_space_handle_t bsh,
218 1.4 pooka bus_size_t offset, const uint8_t *datap, bus_size_t count)
219 1.4 pooka {
220 1.4 pooka
221 1.4 pooka while (count--) {
222 1.4 pooka const uint8_t value = *datap++;
223 1.4 pooka
224 1.4 pooka bus_space_write_1(bst, bsh, offset, value);
225 1.6 pooka bus_space_barrier(bst, bsh, offset, 1, BUS_SPACE_BARRIER_WRITE);
226 1.4 pooka }
227 1.4 pooka }
228 1.4 pooka
229 1.4 pooka void
230 1.4 pooka bus_space_write_multi_2(bus_space_tag_t bst, bus_space_handle_t bsh,
231 1.4 pooka bus_size_t offset, const uint16_t *datap, bus_size_t count)
232 1.4 pooka {
233 1.4 pooka
234 1.4 pooka while (count--) {
235 1.4 pooka const uint16_t value = *datap++;
236 1.4 pooka
237 1.4 pooka bus_space_write_2(bst, bsh, offset, value);
238 1.6 pooka bus_space_barrier(bst, bsh, offset, 2, BUS_SPACE_BARRIER_WRITE);
239 1.4 pooka }
240 1.4 pooka }
241 1.4 pooka
242 1.4 pooka void
243 1.4 pooka bus_space_write_multi_4(bus_space_tag_t bst, bus_space_handle_t bsh,
244 1.4 pooka bus_size_t offset, const uint32_t *datap, bus_size_t count)
245 1.4 pooka {
246 1.4 pooka
247 1.4 pooka while (count--) {
248 1.4 pooka const uint32_t value = *datap++;
249 1.4 pooka
250 1.4 pooka bus_space_write_4(bst, bsh, offset, value);
251 1.6 pooka bus_space_barrier(bst, bsh, offset, 4, BUS_SPACE_BARRIER_WRITE);
252 1.4 pooka }
253 1.4 pooka }
254 1.4 pooka
255 1.1 pooka paddr_t
256 1.1 pooka bus_space_mmap(bus_space_tag_t bst, bus_addr_t addr, off_t off,
257 1.1 pooka int prot, int flags)
258 1.1 pooka {
259 1.1 pooka
260 1.1 pooka panic("%s: unimplemented", __func__);
261 1.1 pooka }
262 1.1 pooka
263 1.1 pooka int
264 1.1 pooka bus_space_subregion(bus_space_tag_t bst, bus_space_handle_t bsh,
265 1.1 pooka bus_size_t offset, bus_size_t size, bus_space_handle_t *nhandlep)
266 1.1 pooka {
267 1.1 pooka
268 1.4 pooka *nhandlep = bsh + offset;
269 1.4 pooka return 0;
270 1.1 pooka }
271 1.1 pooka
272 1.1 pooka void
273 1.1 pooka bus_space_unmap(bus_space_tag_t bst, bus_space_handle_t bsh,
274 1.1 pooka bus_size_t size)
275 1.1 pooka {
276 1.1 pooka
277 1.7 pooka if (bst == 0)
278 1.7 pooka return;
279 1.7 pooka
280 1.1 pooka panic("%s: unimplemented", __func__);
281 1.1 pooka }
282 1.2 pooka
283 1.2 pooka void
284 1.2 pooka bus_space_barrier(bus_space_tag_t bst, bus_space_handle_t bsh,
285 1.2 pooka bus_size_t offset, bus_size_t len, int flags)
286 1.2 pooka {
287 1.2 pooka
288 1.2 pooka /* weelll ... */
289 1.2 pooka membar_sync();
290 1.2 pooka }
291