Home | History | Annotate | Line # | Download | only in virtio
virtio_mmio.c revision 1.9
      1 /*	$NetBSD: virtio_mmio.c,v 1.9 2023/03/31 23:34:23 yamaguchi 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.9 2023/03/31 23:34:23 yamaguchi 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_alloc_interrupts(struct virtio_softc *);
     94 static void	virtio_mmio_free_interrupts(struct virtio_softc *);
     95 static int	virtio_mmio_setup_interrupts(struct virtio_softc *, int);
     96 
     97 static const struct virtio_ops virtio_mmio_ops = {
     98 	.kick = virtio_mmio_kick,
     99 	.read_queue_size = virtio_mmio_read_queue_size,
    100 	.setup_queue = virtio_mmio_setup_queue,
    101 	.set_status = virtio_mmio_set_status,
    102 	.neg_features = virtio_mmio_negotiate_features,
    103 	.alloc_interrupts = virtio_mmio_alloc_interrupts,
    104 	.free_interrupts = virtio_mmio_free_interrupts,
    105 	.setup_interrupts = virtio_mmio_setup_interrupts,
    106 };
    107 
    108 static uint16_t
    109 virtio_mmio_read_queue_size(struct virtio_softc *vsc, uint16_t idx)
    110 {
    111 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    112 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
    113 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    114 	    VIRTIO_MMIO_QUEUE_NUM_MAX);
    115 }
    116 
    117 static void
    118 virtio_mmio_setup_queue(struct virtio_softc *vsc, uint16_t idx, uint64_t addr)
    119 {
    120 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    121 
    122 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
    123 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM,
    124 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX));
    125 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN,
    126 	    VIRTIO_PAGE_SIZE);
    127 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN,
    128 	    addr / VIRTIO_PAGE_SIZE);
    129 }
    130 
    131 static void
    132 virtio_mmio_set_status(struct virtio_softc *vsc, int status)
    133 {
    134 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    135 	int old = 0;
    136 
    137 	if (status != 0)
    138 		old = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    139 				       VIRTIO_MMIO_STATUS);
    140 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_STATUS,
    141 			  status|old);
    142 }
    143 
    144 bool
    145 virtio_mmio_common_probe_present(struct virtio_mmio_softc *sc)
    146 {
    147 	uint32_t magic;
    148 
    149 	magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    150 	    VIRTIO_MMIO_MAGIC_VALUE);
    151 	return (magic == VIRTIO_MMIO_MAGIC);
    152 }
    153 
    154 void
    155 virtio_mmio_common_attach(struct virtio_mmio_softc *sc)
    156 {
    157 	struct virtio_softc *vsc = &sc->sc_sc;
    158 	device_t self = vsc->sc_dev;
    159 	uint32_t id, magic, ver;
    160 
    161 	magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    162 	    VIRTIO_MMIO_MAGIC_VALUE);
    163 	if (magic != VIRTIO_MMIO_MAGIC) {
    164 		aprint_error_dev(vsc->sc_dev,
    165 		    "wrong magic value 0x%08x; giving up\n", magic);
    166 		return;
    167 	}
    168 
    169 	ver = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_VERSION);
    170 	if (ver != 1) {
    171 		aprint_error_dev(vsc->sc_dev,
    172 		    "unknown version 0x%02x; giving up\n", ver);
    173 		return;
    174 	}
    175 
    176 	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_DEVICE_ID);
    177 
    178 	/* we could use PAGE_SIZE, but virtio(4) assumes 4KiB for now */
    179 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_GUEST_PAGE_SIZE,
    180 	    VIRTIO_PAGE_SIZE);
    181 
    182 	/* no device connected. */
    183 	if (id == 0)
    184 		return;
    185 
    186 	virtio_print_device_type(self, id, ver);
    187 	vsc->sc_ops = &virtio_mmio_ops;
    188 	vsc->sc_bus_endian    = READ_ENDIAN;
    189 	vsc->sc_struct_endian = STRUCT_ENDIAN;
    190 
    191 	/* set up our device config tag */
    192 	vsc->sc_devcfg_iosize = sc->sc_iosize - VIRTIO_MMIO_CONFIG;
    193 	vsc->sc_devcfg_iot = sc->sc_iot;
    194 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
    195 			VIRTIO_MMIO_CONFIG, vsc->sc_devcfg_iosize,
    196 			&vsc->sc_devcfg_ioh)) {
    197 		aprint_error_dev(self, "can't map config i/o space\n");
    198 		return;
    199 	}
    200 
    201 	virtio_device_reset(vsc);
    202 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
    203 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
    204 
    205 	/* XXX: use softc as aux... */
    206 	vsc->sc_childdevid = id;
    207 	vsc->sc_child = NULL;
    208 }
    209 
    210 int
    211 virtio_mmio_common_detach(struct virtio_mmio_softc *sc, int flags)
    212 {
    213 	struct virtio_softc *vsc = &sc->sc_sc;
    214 	int r;
    215 
    216 	r = config_detach_children(vsc->sc_dev, flags);
    217 	if (r != 0)
    218 		return r;
    219 
    220 	KASSERT(ISSET(vsc->sc_child_flags, VIRTIO_CHILD_DETACHED));
    221 	KASSERT(vsc->sc_vqs == NULL);
    222 	KASSERT(sc->sc_ih == NULL);
    223 
    224 	if (sc->sc_iosize) {
    225 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    226 		sc->sc_iosize = 0;
    227 	}
    228 
    229 	return 0;
    230 }
    231 
    232 /*
    233  * Feature negotiation.
    234  */
    235 static void
    236 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint64_t
    237     guest_features)
    238 {
    239 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    240 	uint32_t r;
    241 
    242 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    243 	    VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
    244 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    245 				VIRTIO_MMIO_HOST_FEATURES);
    246 	r &= guest_features;
    247 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    248 	    VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
    249 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    250 			  VIRTIO_MMIO_GUEST_FEATURES, r);
    251 
    252 	vsc->sc_active_features = r;
    253 }
    254 
    255 /*
    256  * Interrupt handler.
    257  */
    258 int
    259 virtio_mmio_intr(void *arg)
    260 {
    261 	struct virtio_mmio_softc *sc = arg;
    262 	struct virtio_softc *vsc = &sc->sc_sc;
    263 	int isr, r = 0;
    264 
    265 	/* check and ack the interrupt */
    266 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    267 			       VIRTIO_MMIO_INTERRUPT_STATUS);
    268 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    269 			  VIRTIO_MMIO_INTERRUPT_ACK, isr);
    270 	if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
    271 	    (vsc->sc_config_change != NULL))
    272 		r = (vsc->sc_config_change)(vsc);
    273 	if ((isr & VIRTIO_MMIO_INT_VRING) &&
    274 	    (vsc->sc_intrhand != NULL)) {
    275 		if (vsc->sc_soft_ih != NULL)
    276 			softint_schedule(vsc->sc_soft_ih);
    277 		else
    278 			r |= (vsc->sc_intrhand)(vsc);
    279 	}
    280 
    281 	return r;
    282 }
    283 
    284 static void
    285 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
    286 {
    287 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    288 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
    289 	    idx);
    290 }
    291 
    292 static int
    293 virtio_mmio_alloc_interrupts(struct virtio_softc *vsc)
    294 {
    295 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
    296 
    297 	return sc->sc_alloc_interrupts(sc);
    298 }
    299 
    300 static void
    301 virtio_mmio_free_interrupts(struct virtio_softc *vsc)
    302 {
    303 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
    304 
    305 	sc->sc_free_interrupts(sc);
    306 }
    307 
    308 static int
    309 virtio_mmio_setup_interrupts(struct virtio_softc *vsc __unused,
    310     int reinit __unused)
    311 {
    312 
    313 	return 0;
    314 }
    315