Home | History | Annotate | Line # | Download | only in virtio
virtio_mmio.c revision 1.7.4.3
      1 /*	$NetBSD: virtio_mmio.c,v 1.7.4.3 2023/07/09 13:39:27 martin 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.7.4.3 2023/07/09 13:39:27 martin 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 For big-endian aarch64 and arm, see note in virtio_pci.c.
     72  */
     73 
     74 #if (defined(__aarch64__) || defined(__arm__)) && BYTE_ORDER == BIG_ENDIAN
     75 #	define READ_ENDIAN	LITTLE_ENDIAN
     76 #	define STRUCT_ENDIAN	BIG_ENDIAN
     77 #elif BYTE_ORDER == BIG_ENDIAN
     78 #	define READ_ENDIAN	BIG_ENDIAN
     79 #	define STRUCT_ENDIAN	BIG_ENDIAN
     80 #else
     81 #	define READ_ENDIAN	LITTLE_ENDIAN
     82 #	define STRUCT_ENDIAN	LITTLE_ENDIAN
     83 #endif
     84 
     85 
     86 static void	virtio_mmio_kick(struct virtio_softc *, uint16_t);
     87 static uint16_t	virtio_mmio_read_queue_size(struct virtio_softc *, uint16_t);
     88 static void	virtio_mmio_setup_queue(struct virtio_softc *, uint16_t, uint64_t);
     89 static void	virtio_mmio_set_status(struct virtio_softc *, int);
     90 static void	virtio_mmio_negotiate_features(struct virtio_softc *, uint64_t);
     91 static int	virtio_mmio_alloc_interrupts(struct virtio_softc *);
     92 static void	virtio_mmio_free_interrupts(struct virtio_softc *);
     93 static int	virtio_mmio_setup_interrupts(struct virtio_softc *, int);
     94 
     95 static const struct virtio_ops virtio_mmio_ops = {
     96 	.kick = virtio_mmio_kick,
     97 	.read_queue_size = virtio_mmio_read_queue_size,
     98 	.setup_queue = virtio_mmio_setup_queue,
     99 	.set_status = virtio_mmio_set_status,
    100 	.neg_features = virtio_mmio_negotiate_features,
    101 	.alloc_interrupts = virtio_mmio_alloc_interrupts,
    102 	.free_interrupts = virtio_mmio_free_interrupts,
    103 	.setup_interrupts = virtio_mmio_setup_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 	r = config_detach_children(vsc->sc_dev, flags);
    215 	if (r != 0)
    216 		return r;
    217 
    218 	KASSERT(vsc->sc_child == NULL);
    219 	KASSERT(vsc->sc_vqs == NULL);
    220 	KASSERT(sc->sc_ih == NULL);
    221 
    222 	if (sc->sc_iosize) {
    223 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    224 		sc->sc_iosize = 0;
    225 	}
    226 
    227 	return 0;
    228 }
    229 
    230 /*
    231  * Feature negotiation.
    232  */
    233 static void
    234 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint64_t
    235     guest_features)
    236 {
    237 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    238 	uint32_t r;
    239 
    240 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    241 	    VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
    242 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    243 				VIRTIO_MMIO_HOST_FEATURES);
    244 	r &= guest_features;
    245 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    246 	    VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
    247 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    248 			  VIRTIO_MMIO_GUEST_FEATURES, r);
    249 
    250 	vsc->sc_active_features = r;
    251 }
    252 
    253 /*
    254  * Interrupt handler.
    255  */
    256 int
    257 virtio_mmio_intr(void *arg)
    258 {
    259 	struct virtio_mmio_softc *sc = arg;
    260 	struct virtio_softc *vsc = &sc->sc_sc;
    261 	int isr, r = 0;
    262 
    263 	/* check and ack the interrupt */
    264 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    265 			       VIRTIO_MMIO_INTERRUPT_STATUS);
    266 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    267 			  VIRTIO_MMIO_INTERRUPT_ACK, isr);
    268 	if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
    269 	    (vsc->sc_config_change != NULL))
    270 		r = (vsc->sc_config_change)(vsc);
    271 	if ((isr & VIRTIO_MMIO_INT_VRING) &&
    272 	    (vsc->sc_intrhand != NULL)) {
    273 		if (vsc->sc_soft_ih != NULL)
    274 			softint_schedule(vsc->sc_soft_ih);
    275 		else
    276 			r |= (vsc->sc_intrhand)(vsc);
    277 	}
    278 
    279 	return r;
    280 }
    281 
    282 static void
    283 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
    284 {
    285 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    286 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
    287 	    idx);
    288 }
    289 
    290 static int
    291 virtio_mmio_alloc_interrupts(struct virtio_softc *vsc)
    292 {
    293 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
    294 
    295 	return sc->sc_alloc_interrupts(sc);
    296 }
    297 
    298 static void
    299 virtio_mmio_free_interrupts(struct virtio_softc *vsc)
    300 {
    301 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
    302 
    303 	sc->sc_free_interrupts(sc);
    304 }
    305 
    306 static int
    307 virtio_mmio_setup_interrupts(struct virtio_softc *vsc __unused,
    308     int reinit __unused)
    309 {
    310 
    311 	return 0;
    312 }
    313