xref: /netbsd-src/sys/dev/virtio/virtio_mmio.c (revision a8c74629f602faa0ccf8a463757d7baf858bbf3a)
1 /*	$NetBSD: virtio_mmio.c,v 1.3 2020/10/03 13:51:34 jmcneill 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@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 2020/10/03 13:51:34 jmcneill 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 is in guest byte order. AArch64 BE is special,
70  * as the guest starts in LE and we switch to BE after the kernel starts.
71  * For this case, we need to byte swap all config space accesses.
72  */
73 #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
74 #define	VIO16TOH(x)	le16toh(x)
75 #define	VIO32TOH(x)	le32toh(x)
76 #define	VIO64TOH(x)	le64toh(x)
77 #define	HTOVIO16(x)	htole16(x)
78 #define	HTOVIO32(x)	htole32(x)
79 #define	HTOVIO64(x)	htole64(x)
80 #else
81 #define	VIO16TOH(x)	(x)
82 #define	VIO32TOH(x)	(x)
83 #define	VIO64TOH(x)	(x)
84 #define	HTOVIO16(x)	(x)
85 #define	HTOVIO32(x)	(x)
86 #define	HTOVIO64(x)	(x)
87 #endif
88 
89 /*
90  * XXX: Before being used on big endian arches, the access to config registers
91  * XXX: needs to be reviewed/fixed. The non-device specific registers are
92  * XXX: PCI-endian while the device specific registers are native endian.
93  */
94 
95 static void	virtio_mmio_kick(struct virtio_softc *, uint16_t);
96 static uint8_t	virtio_mmio_read_device_config_1(struct virtio_softc *, int);
97 static uint16_t	virtio_mmio_read_device_config_2(struct virtio_softc *, int);
98 static uint32_t	virtio_mmio_read_device_config_4(struct virtio_softc *, int);
99 static uint64_t	virtio_mmio_read_device_config_8(struct virtio_softc *, int);
100 static void	virtio_mmio_write_device_config_1(struct virtio_softc *, int, uint8_t);
101 static void	virtio_mmio_write_device_config_2(struct virtio_softc *, int, uint16_t);
102 static void	virtio_mmio_write_device_config_4(struct virtio_softc *, int, uint32_t);
103 static void	virtio_mmio_write_device_config_8(struct virtio_softc *, int, uint64_t);
104 static uint16_t	virtio_mmio_read_queue_size(struct virtio_softc *, uint16_t);
105 static void	virtio_mmio_setup_queue(struct virtio_softc *, uint16_t, uint32_t);
106 static void	virtio_mmio_set_status(struct virtio_softc *, int);
107 static uint32_t	virtio_mmio_negotiate_features(struct virtio_softc *, uint32_t);
108 static int	virtio_mmio_setup_interrupts(struct virtio_softc *);
109 static void	virtio_mmio_free_interrupts(struct virtio_softc *);
110 
111 static const struct virtio_ops virtio_mmio_ops = {
112 	.kick = virtio_mmio_kick,
113 	.read_dev_cfg_1 = virtio_mmio_read_device_config_1,
114 	.read_dev_cfg_2 = virtio_mmio_read_device_config_2,
115 	.read_dev_cfg_4 = virtio_mmio_read_device_config_4,
116 	.read_dev_cfg_8 = virtio_mmio_read_device_config_8,
117 	.write_dev_cfg_1 = virtio_mmio_write_device_config_1,
118 	.write_dev_cfg_2 = virtio_mmio_write_device_config_2,
119 	.write_dev_cfg_4 = virtio_mmio_write_device_config_4,
120 	.write_dev_cfg_8 = virtio_mmio_write_device_config_8,
121 	.read_queue_size = virtio_mmio_read_queue_size,
122 	.setup_queue = virtio_mmio_setup_queue,
123 	.set_status = virtio_mmio_set_status,
124 	.neg_features = virtio_mmio_negotiate_features,
125 	.setup_interrupts = virtio_mmio_setup_interrupts,
126 	.free_interrupts = virtio_mmio_free_interrupts,
127 };
128 
129 static uint16_t
130 virtio_mmio_read_queue_size(struct virtio_softc *vsc, uint16_t idx)
131 {
132 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
133 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
134 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
135 	    VIRTIO_MMIO_QUEUE_NUM_MAX);
136 }
137 
138 static void
139 virtio_mmio_setup_queue(struct virtio_softc *vsc, uint16_t idx, uint32_t addr)
140 {
141 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
142 
143 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
144 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM,
145 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX));
146 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN,
147 	    VIRTIO_PAGE_SIZE);
148 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN, addr);
149 }
150 
151 static void
152 virtio_mmio_set_status(struct virtio_softc *vsc, int status)
153 {
154 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
155 	int old = 0;
156 
157 	if (status != 0)
158 		old = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
159 				       VIRTIO_MMIO_STATUS);
160 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_STATUS,
161 			  status|old);
162 }
163 
164 void
165 virtio_mmio_common_attach(struct virtio_mmio_softc *sc)
166 {
167 	struct virtio_softc *vsc = &sc->sc_sc;
168 	uint32_t id, magic, ver;
169 
170 	magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
171 	    VIRTIO_MMIO_MAGIC_VALUE);
172 	if (magic != VIRTIO_MMIO_MAGIC) {
173 		aprint_error_dev(vsc->sc_dev,
174 		    "wrong magic value 0x%08x; giving up\n", magic);
175 		return;
176 	}
177 
178 	ver = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_VERSION);
179 	if (ver != 1) {
180 		aprint_error_dev(vsc->sc_dev,
181 		    "unknown version 0x%02x; giving up\n", ver);
182 		return;
183 	}
184 
185 	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_DEVICE_ID);
186 
187 	/* we could use PAGE_SIZE, but virtio(4) assumes 4KiB for now */
188 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_GUEST_PAGE_SIZE,
189 	    VIRTIO_PAGE_SIZE);
190 
191 	/* No device connected. */
192 	if (id == 0)
193 		return;
194 
195 	vsc->sc_ops = &virtio_mmio_ops;
196 
197 	virtio_device_reset(vsc);
198 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
199 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
200 
201 	/* XXX: use softc as aux... */
202 	vsc->sc_childdevid = id;
203 	vsc->sc_child = NULL;
204 }
205 
206 int
207 virtio_mmio_common_detach(struct virtio_mmio_softc *sc, int flags)
208 {
209 	struct virtio_softc *vsc = &sc->sc_sc;
210 	int r;
211 
212 	if (vsc->sc_child != NULL && vsc->sc_child != VIRTIO_CHILD_FAILED) {
213 		r = config_detach(vsc->sc_child, flags);
214 		if (r)
215 			return r;
216 	}
217 	KASSERT(vsc->sc_child == NULL || vsc->sc_child == VIRTIO_CHILD_FAILED);
218 	KASSERT(vsc->sc_vqs == NULL);
219 	KASSERT(sc->sc_ih == NULL);
220 
221 	if (sc->sc_iosize) {
222 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
223 		sc->sc_iosize = 0;
224 	}
225 
226 	return 0;
227 }
228 
229 /*
230  * Feature negotiation.
231  */
232 static uint32_t
233 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint32_t
234     guest_features)
235 {
236 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
237 	uint32_t r;
238 
239 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
240 	    VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
241 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
242 				VIRTIO_MMIO_HOST_FEATURES);
243 	r &= guest_features;
244 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
245 	    VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
246 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
247 			  VIRTIO_MMIO_GUEST_FEATURES, r);
248 	return r;
249 }
250 
251 /*
252  * Device configuration registers.
253  */
254 static uint8_t
255 virtio_mmio_read_device_config_1(struct virtio_softc *vsc, int index)
256 {
257 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
258 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh,
259 				VIRTIO_MMIO_CONFIG + index);
260 }
261 
262 static uint16_t
263 virtio_mmio_read_device_config_2(struct virtio_softc *vsc, int index)
264 {
265 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
266 	return VIO16TOH(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
267 					VIRTIO_MMIO_CONFIG + index));
268 }
269 
270 static uint32_t
271 virtio_mmio_read_device_config_4(struct virtio_softc *vsc, int index)
272 {
273 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
274 	return VIO32TOH(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
275 					VIRTIO_MMIO_CONFIG + index));
276 }
277 
278 static uint64_t
279 virtio_mmio_read_device_config_8(struct virtio_softc *vsc, int index)
280 {
281 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
282 	uint64_t r;
283 
284 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
285 			     VIRTIO_MMIO_CONFIG + index + sizeof(uint32_t));
286 	r <<= 32;
287 	r += bus_space_read_4(sc->sc_iot, sc->sc_ioh,
288 			      VIRTIO_MMIO_CONFIG + index);
289 	return VIO64TOH(r);
290 }
291 
292 static void
293 virtio_mmio_write_device_config_1(struct virtio_softc *vsc,
294 			     int index, uint8_t value)
295 {
296 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
297 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
298 			  VIRTIO_MMIO_CONFIG + index, value);
299 }
300 
301 static void
302 virtio_mmio_write_device_config_2(struct virtio_softc *vsc,
303 			     int index, uint16_t value)
304 {
305 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
306 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
307 			  VIRTIO_MMIO_CONFIG + index, HTOVIO16(value));
308 }
309 
310 static void
311 virtio_mmio_write_device_config_4(struct virtio_softc *vsc,
312 			     int index, uint32_t value)
313 {
314 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
315 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
316 			  VIRTIO_MMIO_CONFIG + index, HTOVIO32(value));
317 }
318 
319 static void
320 virtio_mmio_write_device_config_8(struct virtio_softc *vsc,
321 			     int index, uint64_t value)
322 {
323 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
324 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
325 			  VIRTIO_MMIO_CONFIG + index,
326 			  HTOVIO64(value) & 0xffffffff);
327 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
328 			  VIRTIO_MMIO_CONFIG + index + sizeof(uint32_t),
329 			  HTOVIO64(value) >> 32);
330 }
331 
332 /*
333  * Interrupt handler.
334  */
335 int
336 virtio_mmio_intr(void *arg)
337 {
338 	struct virtio_mmio_softc *sc = arg;
339 	struct virtio_softc *vsc = &sc->sc_sc;
340 	int isr, r = 0;
341 
342 	/* check and ack the interrupt */
343 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
344 			       VIRTIO_MMIO_INTERRUPT_STATUS);
345 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
346 			  VIRTIO_MMIO_INTERRUPT_ACK, isr);
347 	if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
348 	    (vsc->sc_config_change != NULL))
349 		r = (vsc->sc_config_change)(vsc);
350 	if ((isr & VIRTIO_MMIO_INT_VRING) &&
351 	    (vsc->sc_intrhand != NULL)) {
352 		if (vsc->sc_soft_ih != NULL)
353 			softint_schedule(vsc->sc_soft_ih);
354 		else
355 			r |= (vsc->sc_intrhand)(vsc);
356 	}
357 
358 	return r;
359 }
360 
361 static void
362 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
363 {
364 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
365 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
366 	    idx);
367 }
368 
369 static int
370 virtio_mmio_setup_interrupts(struct virtio_softc *vsc)
371 {
372 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
373 
374 	return sc->sc_setup_interrupts(sc);
375 }
376 
377 static void
378 virtio_mmio_free_interrupts(struct virtio_softc *vsc)
379 {
380 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
381 
382 	sc->sc_free_interrupts(sc);
383 }
384