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