xref: /openbsd-src/sys/dev/fdt/virtio_mmio.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: virtio_mmio.c,v 1.8 2019/05/26 15:20:04 sf Exp $	*/
2 /*	$NetBSD: virtio.c,v 1.3 2011/11/02 23:05:52 njoly 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/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/mutex.h>
36 
37 #include <dev/pv/virtioreg.h>
38 #include <dev/pv/virtiovar.h>
39 
40 #include <machine/fdt.h>
41 
42 #include <dev/ofw/fdt.h>
43 #include <dev/ofw/openfirm.h>
44 
45 #define VIRTIO_MMIO_MAGIC		('v' | 'i' << 8 | 'r' << 16 | 't' << 24)
46 
47 #define VIRTIO_MMIO_MAGIC_VALUE		0x000
48 #define VIRTIO_MMIO_VERSION		0x004
49 #define VIRTIO_MMIO_DEVICE_ID		0x008
50 #define VIRTIO_MMIO_VENDOR_ID		0x00c
51 #define VIRTIO_MMIO_HOST_FEATURES	0x010
52 #define VIRTIO_MMIO_HOST_FEATURES_SEL	0x014
53 #define VIRTIO_MMIO_GUEST_FEATURES	0x020
54 #define VIRTIO_MMIO_GUEST_FEATURES_SEL	0x024
55 #define VIRTIO_MMIO_GUEST_PAGE_SIZE	0x028
56 #define VIRTIO_MMIO_QUEUE_SEL		0x030
57 #define VIRTIO_MMIO_QUEUE_NUM_MAX	0x034
58 #define VIRTIO_MMIO_QUEUE_NUM		0x038
59 #define VIRTIO_MMIO_QUEUE_ALIGN		0x03c
60 #define VIRTIO_MMIO_QUEUE_PFN		0x040
61 #define VIRTIO_MMIO_QUEUE_NOTIFY	0x050
62 #define VIRTIO_MMIO_INTERRUPT_STATUS	0x060
63 #define VIRTIO_MMIO_INTERRUPT_ACK	0x064
64 #define VIRTIO_MMIO_STATUS		0x070
65 #define VIRTIO_MMIO_CONFIG		0x100
66 
67 #define VIRTIO_MMIO_INT_VRING		(1 << 0)
68 #define VIRTIO_MMIO_INT_CONFIG		(1 << 1)
69 
70 #define DEVNAME(sc) (sc)->sc_dev.dv_xname
71 
72 /*
73  * XXX: Before being used on big endian arches, the access to config registers
74  * XXX: needs to be reviewed/fixed. The non-device specific registers are
75  * XXX: PCI-endian while the device specific registers are native endian.
76  */
77 
78 int		virtio_mmio_match(struct device *, void *, void *);
79 void		virtio_mmio_attach(struct device *, struct device *, void *);
80 int		virtio_mmio_detach(struct device *, int);
81 
82 void		virtio_mmio_kick(struct virtio_softc *, uint16_t);
83 uint8_t		virtio_mmio_read_device_config_1(struct virtio_softc *, int);
84 uint16_t	virtio_mmio_read_device_config_2(struct virtio_softc *, int);
85 uint32_t	virtio_mmio_read_device_config_4(struct virtio_softc *, int);
86 uint64_t	virtio_mmio_read_device_config_8(struct virtio_softc *, int);
87 void		virtio_mmio_write_device_config_1(struct virtio_softc *, int, uint8_t);
88 void		virtio_mmio_write_device_config_2(struct virtio_softc *, int, uint16_t);
89 void		virtio_mmio_write_device_config_4(struct virtio_softc *, int, uint32_t);
90 void		virtio_mmio_write_device_config_8(struct virtio_softc *, int, uint64_t);
91 uint16_t	virtio_mmio_read_queue_size(struct virtio_softc *, uint16_t);
92 void		virtio_mmio_setup_queue(struct virtio_softc *, struct virtqueue *, uint64_t);
93 void		virtio_mmio_set_status(struct virtio_softc *, int);
94 int		virtio_mmio_negotiate_features(struct virtio_softc *,
95     const struct virtio_feature_name *);
96 int		virtio_mmio_intr(void *);
97 
98 struct virtio_mmio_softc {
99 	struct virtio_softc	sc_sc;
100 
101 	bus_space_tag_t		sc_iot;
102 	bus_space_handle_t	sc_ioh;
103 	bus_size_t		sc_iosize;
104 	bus_dma_tag_t		sc_dmat;
105 
106 	void			*sc_ih;
107 
108 	int			sc_config_offset;
109 };
110 
111 struct cfattach virtio_mmio_ca = {
112 	sizeof(struct virtio_mmio_softc),
113 	virtio_mmio_match,
114 	virtio_mmio_attach,
115 	virtio_mmio_detach,
116 	NULL
117 };
118 
119 struct cfattach virtio_mmio_fdt_ca = {
120 	sizeof(struct virtio_mmio_softc),
121 	NULL,
122 	virtio_mmio_attach,
123 	virtio_mmio_detach,
124 	NULL
125 };
126 
127 struct virtio_ops virtio_mmio_ops = {
128 	virtio_mmio_kick,
129 	virtio_mmio_read_device_config_1,
130 	virtio_mmio_read_device_config_2,
131 	virtio_mmio_read_device_config_4,
132 	virtio_mmio_read_device_config_8,
133 	virtio_mmio_write_device_config_1,
134 	virtio_mmio_write_device_config_2,
135 	virtio_mmio_write_device_config_4,
136 	virtio_mmio_write_device_config_8,
137 	virtio_mmio_read_queue_size,
138 	virtio_mmio_setup_queue,
139 	virtio_mmio_set_status,
140 	virtio_mmio_negotiate_features,
141 	virtio_mmio_intr,
142 };
143 
144 uint16_t
145 virtio_mmio_read_queue_size(struct virtio_softc *vsc, uint16_t idx)
146 {
147 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
148 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
149 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
150 	    VIRTIO_MMIO_QUEUE_NUM_MAX);
151 }
152 
153 void
154 virtio_mmio_setup_queue(struct virtio_softc *vsc, struct virtqueue *vq,
155     uint64_t addr)
156 {
157 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
158 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL,
159 	    vq->vq_index);
160 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM,
161 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX));
162 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN,
163 	    PAGE_SIZE);
164 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN,
165 	    addr / VIRTIO_PAGE_SIZE);
166 }
167 
168 void
169 virtio_mmio_set_status(struct virtio_softc *vsc, int status)
170 {
171 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
172 	int old = 0;
173 
174 	if (status != 0)
175 		old = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
176 				       VIRTIO_MMIO_STATUS);
177 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_STATUS,
178 			  status|old);
179 }
180 
181 int
182 virtio_mmio_match(struct device *parent, void *cfdata, void *aux)
183 {
184 	struct fdt_attach_args *faa = aux;
185 
186 	return OF_is_compatible(faa->fa_node, "virtio,mmio");
187 }
188 
189 void
190 virtio_mmio_attach(struct device *parent, struct device *self, void *aux)
191 {
192 	struct fdt_attach_args *faa = aux;
193 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)self;
194 	struct virtio_softc *vsc = &sc->sc_sc;
195 	uint32_t id, magic, version;
196 
197 	if (faa->fa_nreg < 1) {
198 		printf(": no register data\n");
199 		return;
200 	}
201 
202 	sc->sc_iosize = faa->fa_reg[0].size;
203 	sc->sc_iot = faa->fa_iot;
204 	sc->sc_dmat = faa->fa_dmat;
205 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, faa->fa_reg[0].size,
206 	    0, &sc->sc_ioh))
207 		panic("%s: bus_space_map failed!", __func__);
208 
209 	magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
210 	    VIRTIO_MMIO_MAGIC_VALUE);
211 	if (magic != VIRTIO_MMIO_MAGIC) {
212 		printf(": wrong magic value 0x%08x; giving up\n", magic);
213 		return;
214 	}
215 
216 	version = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_VERSION);
217 	if (version != 1) {
218 		printf(": unknown version 0x%02x; giving up\n", version);
219 		return;
220 	}
221 
222 	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_DEVICE_ID);
223 	printf(": Virtio %s Device", virtio_device_string(id));
224 
225 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_GUEST_PAGE_SIZE,
226 	    PAGE_SIZE);
227 
228 	printf("\n");
229 
230 	/* No device connected. */
231 	if (id == 0)
232 		return;
233 
234 	vsc->sc_ops = &virtio_mmio_ops;
235 	vsc->sc_dmat = sc->sc_dmat;
236 	sc->sc_config_offset = VIRTIO_MMIO_CONFIG;
237 
238 	virtio_device_reset(vsc);
239 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
240 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
241 
242 	/* XXX: use softc as aux... */
243 	vsc->sc_childdevid = id;
244 	vsc->sc_child = NULL;
245 	config_found(self, sc, NULL);
246 	if (vsc->sc_child == NULL) {
247 		printf("%s: no matching child driver; not configured\n",
248 		    vsc->sc_dev.dv_xname);
249 		goto fail_1;
250 	}
251 	if (vsc->sc_child == VIRTIO_CHILD_ERROR) {
252 		printf("%s: virtio configuration failed\n",
253 		    vsc->sc_dev.dv_xname);
254 		goto fail_1;
255 	}
256 
257 	sc->sc_ih = fdt_intr_establish(faa->fa_node, vsc->sc_ipl,
258 	    virtio_mmio_intr, sc, vsc->sc_dev.dv_xname);
259 	if (sc->sc_ih == NULL) {
260 		printf("%s: couldn't establish interrupt\n",
261 		    vsc->sc_dev.dv_xname);
262 		goto fail_2;
263 	}
264 
265 	virtio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
266 	return;
267 
268 fail_2:
269 	config_detach(vsc->sc_child, 0);
270 fail_1:
271 	/* no mmio_mapreg_unmap() or mmio_intr_unmap() */
272 	virtio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
273 }
274 
275 int
276 virtio_mmio_detach(struct device *self, int flags)
277 {
278 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)self;
279 	struct virtio_softc *vsc = &sc->sc_sc;
280 	int r;
281 
282 	if (vsc->sc_child != 0 && vsc->sc_child != VIRTIO_CHILD_ERROR) {
283 		r = config_detach(vsc->sc_child, flags);
284 		if (r)
285 			return r;
286 	}
287 	KASSERT(vsc->sc_child == 0 || vsc->sc_child == VIRTIO_CHILD_ERROR);
288 	KASSERT(vsc->sc_vqs == 0);
289 	fdt_intr_disestablish(sc->sc_ih);
290 	sc->sc_ih = 0;
291 	if (sc->sc_iosize)
292 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
293 	sc->sc_iosize = 0;
294 
295 	return 0;
296 }
297 
298 /*
299  * Feature negotiation.
300  * Prints available / negotiated features if guest_feature_names != NULL and
301  * VIRTIO_DEBUG is 1
302  */
303 int
304 virtio_mmio_negotiate_features(struct virtio_softc *vsc,
305     const struct virtio_feature_name *guest_feature_names)
306 {
307 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
308 	uint64_t host, neg;
309 
310 	vsc->sc_active_features = 0;
311 
312 	/*
313 	 * We enable indirect descriptors by default. They can be switched
314 	 * off by setting bit 1 in the driver flags, see config(8).
315 	 */
316 	if (!(vsc->sc_dev.dv_cfdata->cf_flags & VIRTIO_CF_NO_INDIRECT) &&
317 	    !(vsc->sc_child->dv_cfdata->cf_flags & VIRTIO_CF_NO_INDIRECT)) {
318 		vsc->sc_driver_features |= VIRTIO_F_RING_INDIRECT_DESC;
319 	} else if (guest_feature_names != NULL) {
320 		printf("RingIndirectDesc disabled by UKC\n");
321 	}
322 	/*
323 	 * The driver must add VIRTIO_F_RING_EVENT_IDX if it supports it.
324 	 * If it did, check if it is disabled by bit 2 in the driver flags.
325 	 */
326 	if ((vsc->sc_driver_features & VIRTIO_F_RING_EVENT_IDX) &&
327 	    ((vsc->sc_dev.dv_cfdata->cf_flags & VIRTIO_CF_NO_EVENT_IDX) ||
328 	    (vsc->sc_child->dv_cfdata->cf_flags & VIRTIO_CF_NO_EVENT_IDX))) {
329 		if (guest_feature_names != NULL)
330 			printf(" RingEventIdx disabled by UKC");
331 		vsc->sc_driver_features &= ~(VIRTIO_F_RING_EVENT_IDX);
332 	}
333 
334 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
335 	    VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
336 	host = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
337 				VIRTIO_MMIO_HOST_FEATURES);
338 	neg = host & vsc->sc_driver_features;
339 #if VIRTIO_DEBUG
340 	if (guest_feature_names)
341 		virtio_log_features(host, neg, guest_feature_names);
342 #endif
343 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
344 	    VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
345 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
346 			  VIRTIO_MMIO_GUEST_FEATURES, neg);
347 	vsc->sc_active_features = neg;
348 	if (neg & VIRTIO_F_RING_INDIRECT_DESC)
349 		vsc->sc_indirect = 1;
350 	else
351 		vsc->sc_indirect = 0;
352 
353 	return 0;
354 }
355 
356 /*
357  * Device configuration registers.
358  */
359 uint8_t
360 virtio_mmio_read_device_config_1(struct virtio_softc *vsc, int index)
361 {
362 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
363 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh,
364 				sc->sc_config_offset + index);
365 }
366 
367 uint16_t
368 virtio_mmio_read_device_config_2(struct virtio_softc *vsc, int index)
369 {
370 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
371 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh,
372 				sc->sc_config_offset + index);
373 }
374 
375 uint32_t
376 virtio_mmio_read_device_config_4(struct virtio_softc *vsc, int index)
377 {
378 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
379 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
380 				sc->sc_config_offset + index);
381 }
382 
383 uint64_t
384 virtio_mmio_read_device_config_8(struct virtio_softc *vsc, int index)
385 {
386 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
387 	uint64_t r;
388 
389 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
390 			     sc->sc_config_offset + index + sizeof(uint32_t));
391 	r <<= 32;
392 	r += bus_space_read_4(sc->sc_iot, sc->sc_ioh,
393 			      sc->sc_config_offset + index);
394 	return r;
395 }
396 
397 void
398 virtio_mmio_write_device_config_1(struct virtio_softc *vsc,
399 			     int index, uint8_t value)
400 {
401 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
402 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
403 			  sc->sc_config_offset + index, value);
404 }
405 
406 void
407 virtio_mmio_write_device_config_2(struct virtio_softc *vsc,
408 			     int index, uint16_t value)
409 {
410 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
411 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
412 			  sc->sc_config_offset + index, value);
413 }
414 
415 void
416 virtio_mmio_write_device_config_4(struct virtio_softc *vsc,
417 			     int index, uint32_t value)
418 {
419 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
420 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
421 			  sc->sc_config_offset + index, value);
422 }
423 
424 void
425 virtio_mmio_write_device_config_8(struct virtio_softc *vsc,
426 			     int index, uint64_t value)
427 {
428 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
429 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
430 			  sc->sc_config_offset + index,
431 			  value & 0xffffffff);
432 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
433 			  sc->sc_config_offset + index + sizeof(uint32_t),
434 			  value >> 32);
435 }
436 
437 /*
438  * Interrupt handler.
439  */
440 int
441 virtio_mmio_intr(void *arg)
442 {
443 	struct virtio_mmio_softc *sc = arg;
444 	struct virtio_softc *vsc = &sc->sc_sc;
445 	int isr, r = 0;
446 
447 	/* check and ack the interrupt */
448 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
449 			       VIRTIO_MMIO_INTERRUPT_STATUS);
450 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
451 			  VIRTIO_MMIO_INTERRUPT_ACK, isr);
452 	if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
453 	    (vsc->sc_config_change != NULL))
454 		r = (vsc->sc_config_change)(vsc);
455 	if ((isr & VIRTIO_MMIO_INT_VRING))
456 		r |= virtio_check_vqs(vsc);
457 
458 	return r;
459 }
460 
461 void
462 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
463 {
464 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
465 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
466 	    idx);
467 }
468