virtio_mmio.c revision 1.3.2.1 1 /* $NetBSD: virtio_mmio.c,v 1.3.2.1 2021/04/03 22:28:51 thorpej Exp $ */
2 /* $OpenBSD: virtio_mmio.c,v 1.2 2017/02/24 17:12:31 patrick Exp $ */
3
4 /*
5 * Copyright (c) 2014 Patrick Wildt <patrick (at) blueri.se>
6 * Copyright (c) 2012 Stefan Fritsch.
7 * Copyright (c) 2010 Minoura Makoto.
8 * All rights reserved.
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: virtio_mmio.c,v 1.3.2.1 2021/04/03 22:28:51 thorpej Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/mutex.h>
39
40 #define VIRTIO_PRIVATE
41 #include <dev/virtio/virtio_mmiovar.h>
42
43 #define VIRTIO_MMIO_MAGIC ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)
44
45 #define VIRTIO_MMIO_MAGIC_VALUE 0x000
46 #define VIRTIO_MMIO_VERSION 0x004
47 #define VIRTIO_MMIO_DEVICE_ID 0x008
48 #define VIRTIO_MMIO_VENDOR_ID 0x00c
49 #define VIRTIO_MMIO_HOST_FEATURES 0x010
50 #define VIRTIO_MMIO_HOST_FEATURES_SEL 0x014
51 #define VIRTIO_MMIO_GUEST_FEATURES 0x020
52 #define VIRTIO_MMIO_GUEST_FEATURES_SEL 0x024
53 #define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028
54 #define VIRTIO_MMIO_QUEUE_SEL 0x030
55 #define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034
56 #define VIRTIO_MMIO_QUEUE_NUM 0x038
57 #define VIRTIO_MMIO_QUEUE_ALIGN 0x03c
58 #define VIRTIO_MMIO_QUEUE_PFN 0x040
59 #define VIRTIO_MMIO_QUEUE_NOTIFY 0x050
60 #define VIRTIO_MMIO_INTERRUPT_STATUS 0x060
61 #define VIRTIO_MMIO_INTERRUPT_ACK 0x064
62 #define VIRTIO_MMIO_STATUS 0x070
63 #define VIRTIO_MMIO_CONFIG 0x100
64
65 #define VIRTIO_MMIO_INT_VRING (1 << 0)
66 #define VIRTIO_MMIO_INT_CONFIG (1 << 1)
67
68 /*
69 * MMIO configuration space for virtio-mmio v1 is in guest byte order.
70 *
71 * XXX Note that aarch64eb pretends to be little endian. the MMIO registers
72 * are in little endian but the device config registers and data structures
73 * are in big endian; this is due to a bug in current Qemu.
74 */
75
76 #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
77 # define READ_ENDIAN LITTLE_ENDIAN
78 # define STRUCT_ENDIAN BIG_ENDIAN
79 #elif BYTE_ORDER == BIG_ENDIAN
80 # define READ_ENDIAN BIG_ENDIAN
81 # define STRUCT_ENDIAN BIG_ENDIAN
82 #else
83 # define READ_ENDIAN LITTLE_ENDIAN
84 # define STRUCT_ENDIAN LITTLE_ENDIAN
85 #endif
86
87
88 static void virtio_mmio_kick(struct virtio_softc *, uint16_t);
89 static uint16_t virtio_mmio_read_queue_size(struct virtio_softc *, uint16_t);
90 static void virtio_mmio_setup_queue(struct virtio_softc *, uint16_t, uint64_t);
91 static void virtio_mmio_set_status(struct virtio_softc *, int);
92 static void virtio_mmio_negotiate_features(struct virtio_softc *, uint64_t);
93 static int virtio_mmio_setup_interrupts(struct virtio_softc *);
94 static void virtio_mmio_free_interrupts(struct virtio_softc *);
95
96 static const struct virtio_ops virtio_mmio_ops = {
97 .kick = virtio_mmio_kick,
98 .read_queue_size = virtio_mmio_read_queue_size,
99 .setup_queue = virtio_mmio_setup_queue,
100 .set_status = virtio_mmio_set_status,
101 .neg_features = virtio_mmio_negotiate_features,
102 .setup_interrupts = virtio_mmio_setup_interrupts,
103 .free_interrupts = virtio_mmio_free_interrupts,
104 };
105
106 static uint16_t
107 virtio_mmio_read_queue_size(struct virtio_softc *vsc, uint16_t idx)
108 {
109 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
110 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
111 return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
112 VIRTIO_MMIO_QUEUE_NUM_MAX);
113 }
114
115 static void
116 virtio_mmio_setup_queue(struct virtio_softc *vsc, uint16_t idx, uint64_t addr)
117 {
118 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
119
120 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
121 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM,
122 bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX));
123 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN,
124 VIRTIO_PAGE_SIZE);
125 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN,
126 addr / VIRTIO_PAGE_SIZE);
127 }
128
129 static void
130 virtio_mmio_set_status(struct virtio_softc *vsc, int status)
131 {
132 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
133 int old = 0;
134
135 if (status != 0)
136 old = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
137 VIRTIO_MMIO_STATUS);
138 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_STATUS,
139 status|old);
140 }
141
142 bool
143 virtio_mmio_common_probe_present(struct virtio_mmio_softc *sc)
144 {
145 uint32_t magic;
146
147 magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
148 VIRTIO_MMIO_MAGIC_VALUE);
149 return (magic == VIRTIO_MMIO_MAGIC);
150 }
151
152 void
153 virtio_mmio_common_attach(struct virtio_mmio_softc *sc)
154 {
155 struct virtio_softc *vsc = &sc->sc_sc;
156 device_t self = vsc->sc_dev;
157 uint32_t id, magic, ver;
158
159 magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
160 VIRTIO_MMIO_MAGIC_VALUE);
161 if (magic != VIRTIO_MMIO_MAGIC) {
162 aprint_error_dev(vsc->sc_dev,
163 "wrong magic value 0x%08x; giving up\n", magic);
164 return;
165 }
166
167 ver = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_VERSION);
168 if (ver != 1) {
169 aprint_error_dev(vsc->sc_dev,
170 "unknown version 0x%02x; giving up\n", ver);
171 return;
172 }
173
174 id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_DEVICE_ID);
175
176 /* we could use PAGE_SIZE, but virtio(4) assumes 4KiB for now */
177 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_GUEST_PAGE_SIZE,
178 VIRTIO_PAGE_SIZE);
179
180 /* no device connected. */
181 if (id == 0)
182 return;
183
184 virtio_print_device_type(self, id, ver);
185 vsc->sc_ops = &virtio_mmio_ops;
186 vsc->sc_bus_endian = READ_ENDIAN;
187 vsc->sc_struct_endian = STRUCT_ENDIAN;
188
189 /* set up our device config tag */
190 vsc->sc_devcfg_iosize = sc->sc_iosize - VIRTIO_MMIO_CONFIG;
191 vsc->sc_devcfg_iot = sc->sc_iot;
192 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
193 VIRTIO_MMIO_CONFIG, vsc->sc_devcfg_iosize,
194 &vsc->sc_devcfg_ioh)) {
195 aprint_error_dev(self, "can't map config i/o space\n");
196 return;
197 }
198
199 virtio_device_reset(vsc);
200 virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
201 virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
202
203 /* XXX: use softc as aux... */
204 vsc->sc_childdevid = id;
205 vsc->sc_child = NULL;
206 }
207
208 int
209 virtio_mmio_common_detach(struct virtio_mmio_softc *sc, int flags)
210 {
211 struct virtio_softc *vsc = &sc->sc_sc;
212 int r;
213
214 if (vsc->sc_child != NULL && vsc->sc_child != VIRTIO_CHILD_FAILED) {
215 r = config_detach(vsc->sc_child, flags);
216 if (r)
217 return r;
218 }
219 KASSERT(vsc->sc_child == NULL || vsc->sc_child == VIRTIO_CHILD_FAILED);
220 KASSERT(vsc->sc_vqs == NULL);
221 KASSERT(sc->sc_ih == NULL);
222
223 if (sc->sc_iosize) {
224 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
225 sc->sc_iosize = 0;
226 }
227
228 return 0;
229 }
230
231 /*
232 * Feature negotiation.
233 */
234 static void
235 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint64_t
236 guest_features)
237 {
238 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
239 uint32_t r;
240
241 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
242 VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
243 r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
244 VIRTIO_MMIO_HOST_FEATURES);
245 r &= guest_features;
246 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
247 VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
248 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
249 VIRTIO_MMIO_GUEST_FEATURES, r);
250
251 vsc->sc_active_features = r;
252 }
253
254 /*
255 * Interrupt handler.
256 */
257 int
258 virtio_mmio_intr(void *arg)
259 {
260 struct virtio_mmio_softc *sc = arg;
261 struct virtio_softc *vsc = &sc->sc_sc;
262 int isr, r = 0;
263
264 /* check and ack the interrupt */
265 isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
266 VIRTIO_MMIO_INTERRUPT_STATUS);
267 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
268 VIRTIO_MMIO_INTERRUPT_ACK, isr);
269 if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
270 (vsc->sc_config_change != NULL))
271 r = (vsc->sc_config_change)(vsc);
272 if ((isr & VIRTIO_MMIO_INT_VRING) &&
273 (vsc->sc_intrhand != NULL)) {
274 if (vsc->sc_soft_ih != NULL)
275 softint_schedule(vsc->sc_soft_ih);
276 else
277 r |= (vsc->sc_intrhand)(vsc);
278 }
279
280 return r;
281 }
282
283 static void
284 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
285 {
286 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
287 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
288 idx);
289 }
290
291 static int
292 virtio_mmio_setup_interrupts(struct virtio_softc *vsc)
293 {
294 struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
295
296 return sc->sc_setup_interrupts(sc);
297 }
298
299 static void
300 virtio_mmio_free_interrupts(struct virtio_softc *vsc)
301 {
302 struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
303
304 sc->sc_free_interrupts(sc);
305 }
306