xref: /openbsd-src/sys/dev/fdt/virtio_mmio.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: virtio_mmio.c,v 1.4 2019/01/10 18:05:43 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 *, uint16_t, uint32_t);
93 void		virtio_mmio_set_status(struct virtio_softc *, int);
94 uint32_t	virtio_mmio_negotiate_features(struct virtio_softc *, uint32_t,
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, uint16_t idx, uint32_t addr)
155 {
156 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
157 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
158 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM,
159 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX));
160 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN,
161 	    PAGE_SIZE);
162 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN, addr);
163 }
164 
165 void
166 virtio_mmio_set_status(struct virtio_softc *vsc, int status)
167 {
168 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
169 	int old = 0;
170 
171 	if (status != 0)
172 		old = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
173 				       VIRTIO_MMIO_STATUS);
174 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_STATUS,
175 			  status|old);
176 }
177 
178 int
179 virtio_mmio_match(struct device *parent, void *cfdata, void *aux)
180 {
181 	struct fdt_attach_args *faa = aux;
182 
183 	return OF_is_compatible(faa->fa_node, "virtio,mmio");
184 }
185 
186 void
187 virtio_mmio_attach(struct device *parent, struct device *self, void *aux)
188 {
189 	struct fdt_attach_args *faa = aux;
190 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)self;
191 	struct virtio_softc *vsc = &sc->sc_sc;
192 	uint32_t id, magic, version;
193 
194 	if (faa->fa_nreg < 1) {
195 		printf(": no register data\n");
196 		return;
197 	}
198 
199 	sc->sc_iosize = faa->fa_reg[0].size;
200 	sc->sc_iot = faa->fa_iot;
201 	sc->sc_dmat = faa->fa_dmat;
202 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, faa->fa_reg[0].size,
203 	    0, &sc->sc_ioh))
204 		panic("%s: bus_space_map failed!", __func__);
205 
206 	magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
207 	    VIRTIO_MMIO_MAGIC_VALUE);
208 	if (magic != VIRTIO_MMIO_MAGIC) {
209 		printf(": wrong magic value 0x%08x; giving up\n", magic);
210 		return;
211 	}
212 
213 	version = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_VERSION);
214 	if (version != 1) {
215 		printf(": unknown version 0x%02x; giving up\n", version);
216 		return;
217 	}
218 
219 	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_DEVICE_ID);
220 	printf(": Virtio %s Device", virtio_device_string(id));
221 
222 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_GUEST_PAGE_SIZE,
223 	    PAGE_SIZE);
224 
225 	printf("\n");
226 
227 	/* No device connected. */
228 	if (id == 0)
229 		return;
230 
231 	vsc->sc_ops = &virtio_mmio_ops;
232 	vsc->sc_dmat = sc->sc_dmat;
233 	sc->sc_config_offset = VIRTIO_MMIO_CONFIG;
234 
235 	virtio_device_reset(vsc);
236 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
237 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
238 
239 	/* XXX: use softc as aux... */
240 	vsc->sc_childdevid = id;
241 	vsc->sc_child = NULL;
242 	config_found(self, sc, NULL);
243 	if (vsc->sc_child == NULL) {
244 		printf("%s: no matching child driver; not configured\n",
245 		    vsc->sc_dev.dv_xname);
246 		goto fail_1;
247 	}
248 	if (vsc->sc_child == VIRTIO_CHILD_ERROR) {
249 		printf("%s: virtio configuration failed\n",
250 		    vsc->sc_dev.dv_xname);
251 		goto fail_1;
252 	}
253 
254 	sc->sc_ih = fdt_intr_establish(faa->fa_node, vsc->sc_ipl,
255 	    virtio_mmio_intr, sc, vsc->sc_dev.dv_xname);
256 	if (sc->sc_ih == NULL) {
257 		printf("%s: couldn't establish interrupt\n",
258 		    vsc->sc_dev.dv_xname);
259 		goto fail_2;
260 	}
261 
262 	virtio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
263 	return;
264 
265 fail_2:
266 	config_detach(vsc->sc_child, 0);
267 fail_1:
268 	/* no mmio_mapreg_unmap() or mmio_intr_unmap() */
269 	virtio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
270 }
271 
272 int
273 virtio_mmio_detach(struct device *self, int flags)
274 {
275 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)self;
276 	struct virtio_softc *vsc = &sc->sc_sc;
277 	int r;
278 
279 	if (vsc->sc_child != 0 && vsc->sc_child != VIRTIO_CHILD_ERROR) {
280 		r = config_detach(vsc->sc_child, flags);
281 		if (r)
282 			return r;
283 	}
284 	KASSERT(vsc->sc_child == 0 || vsc->sc_child == VIRTIO_CHILD_ERROR);
285 	KASSERT(vsc->sc_vqs == 0);
286 	fdt_intr_disestablish(sc->sc_ih);
287 	sc->sc_ih = 0;
288 	if (sc->sc_iosize)
289 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
290 	sc->sc_iosize = 0;
291 
292 	return 0;
293 }
294 
295 /*
296  * Feature negotiation.
297  * Prints available / negotiated features if guest_feature_names != NULL and
298  * VIRTIO_DEBUG is 1
299  */
300 uint32_t
301 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint32_t guest_features,
302 			  const struct virtio_feature_name *guest_feature_names)
303 {
304 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
305 	uint32_t host, neg;
306 
307 	/*
308 	 * indirect descriptors can be switched off by setting bit 1 in the
309 	 * driver flags, see config(8)
310 	 */
311 	if (!(vsc->sc_dev.dv_cfdata->cf_flags & 1) &&
312 	    !(vsc->sc_child->dv_cfdata->cf_flags & 1)) {
313 		guest_features |= VIRTIO_F_RING_INDIRECT_DESC;
314 	} else {
315 		printf("RingIndirectDesc disabled by UKC\n");
316 	}
317 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
318 	    VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
319 	host = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
320 				VIRTIO_MMIO_HOST_FEATURES);
321 	neg = host & guest_features;
322 #if VIRTIO_DEBUG
323 	if (guest_feature_names)
324 		virtio_log_features(host, neg, guest_feature_names);
325 #endif
326 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
327 	    VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
328 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
329 			  VIRTIO_MMIO_GUEST_FEATURES, neg);
330 	vsc->sc_features = neg;
331 	if (neg & VIRTIO_F_RING_INDIRECT_DESC)
332 		vsc->sc_indirect = 1;
333 	else
334 		vsc->sc_indirect = 0;
335 
336 	return neg;
337 }
338 
339 /*
340  * Device configuration registers.
341  */
342 uint8_t
343 virtio_mmio_read_device_config_1(struct virtio_softc *vsc, int index)
344 {
345 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
346 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh,
347 				sc->sc_config_offset + index);
348 }
349 
350 uint16_t
351 virtio_mmio_read_device_config_2(struct virtio_softc *vsc, int index)
352 {
353 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
354 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh,
355 				sc->sc_config_offset + index);
356 }
357 
358 uint32_t
359 virtio_mmio_read_device_config_4(struct virtio_softc *vsc, int index)
360 {
361 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
362 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
363 				sc->sc_config_offset + index);
364 }
365 
366 uint64_t
367 virtio_mmio_read_device_config_8(struct virtio_softc *vsc, int index)
368 {
369 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
370 	uint64_t r;
371 
372 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
373 			     sc->sc_config_offset + index + sizeof(uint32_t));
374 	r <<= 32;
375 	r += bus_space_read_4(sc->sc_iot, sc->sc_ioh,
376 			      sc->sc_config_offset + index);
377 	return r;
378 }
379 
380 void
381 virtio_mmio_write_device_config_1(struct virtio_softc *vsc,
382 			     int index, uint8_t value)
383 {
384 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
385 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
386 			  sc->sc_config_offset + index, value);
387 }
388 
389 void
390 virtio_mmio_write_device_config_2(struct virtio_softc *vsc,
391 			     int index, uint16_t value)
392 {
393 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
394 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
395 			  sc->sc_config_offset + index, value);
396 }
397 
398 void
399 virtio_mmio_write_device_config_4(struct virtio_softc *vsc,
400 			     int index, uint32_t value)
401 {
402 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
403 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
404 			  sc->sc_config_offset + index, value);
405 }
406 
407 void
408 virtio_mmio_write_device_config_8(struct virtio_softc *vsc,
409 			     int index, uint64_t value)
410 {
411 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
412 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
413 			  sc->sc_config_offset + index,
414 			  value & 0xffffffff);
415 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
416 			  sc->sc_config_offset + index + sizeof(uint32_t),
417 			  value >> 32);
418 }
419 
420 /*
421  * Interrupt handler.
422  */
423 int
424 virtio_mmio_intr(void *arg)
425 {
426 	struct virtio_mmio_softc *sc = arg;
427 	struct virtio_softc *vsc = &sc->sc_sc;
428 	int isr, r = 0;
429 
430 	/* check and ack the interrupt */
431 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
432 			       VIRTIO_MMIO_INTERRUPT_STATUS);
433 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
434 			  VIRTIO_MMIO_INTERRUPT_ACK, isr);
435 	if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
436 	    (vsc->sc_config_change != NULL))
437 		r = (vsc->sc_config_change)(vsc);
438 	if ((isr & VIRTIO_MMIO_INT_VRING))
439 		r |= virtio_check_vqs(vsc);
440 
441 	return r;
442 }
443 
444 void
445 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
446 {
447 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
448 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
449 	    idx);
450 }
451