1 /* $NetBSD: virtio_mmio.c,v 1.7 2021/10/22 02:57: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@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 2021/10/22 02:57: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 if (vsc->sc_child != NULL && vsc->sc_child != VIRTIO_CHILD_FAILED) { 217 r = config_detach(vsc->sc_child, flags); 218 if (r) 219 return r; 220 } 221 KASSERT(vsc->sc_child == NULL || vsc->sc_child == VIRTIO_CHILD_FAILED); 222 KASSERT(vsc->sc_vqs == NULL); 223 KASSERT(sc->sc_ih == NULL); 224 225 if (sc->sc_iosize) { 226 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize); 227 sc->sc_iosize = 0; 228 } 229 230 return 0; 231 } 232 233 /* 234 * Feature negotiation. 235 */ 236 static void 237 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint64_t 238 guest_features) 239 { 240 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 241 uint32_t r; 242 243 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 244 VIRTIO_MMIO_HOST_FEATURES_SEL, 0); 245 r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 246 VIRTIO_MMIO_HOST_FEATURES); 247 r &= guest_features; 248 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 249 VIRTIO_MMIO_GUEST_FEATURES_SEL, 0); 250 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 251 VIRTIO_MMIO_GUEST_FEATURES, r); 252 253 vsc->sc_active_features = r; 254 } 255 256 /* 257 * Interrupt handler. 258 */ 259 int 260 virtio_mmio_intr(void *arg) 261 { 262 struct virtio_mmio_softc *sc = arg; 263 struct virtio_softc *vsc = &sc->sc_sc; 264 int isr, r = 0; 265 266 /* check and ack the interrupt */ 267 isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 268 VIRTIO_MMIO_INTERRUPT_STATUS); 269 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 270 VIRTIO_MMIO_INTERRUPT_ACK, isr); 271 if ((isr & VIRTIO_MMIO_INT_CONFIG) && 272 (vsc->sc_config_change != NULL)) 273 r = (vsc->sc_config_change)(vsc); 274 if ((isr & VIRTIO_MMIO_INT_VRING) && 275 (vsc->sc_intrhand != NULL)) { 276 if (vsc->sc_soft_ih != NULL) 277 softint_schedule(vsc->sc_soft_ih); 278 else 279 r |= (vsc->sc_intrhand)(vsc); 280 } 281 282 return r; 283 } 284 285 static void 286 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx) 287 { 288 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 289 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY, 290 idx); 291 } 292 293 static int 294 virtio_mmio_alloc_interrupts(struct virtio_softc *vsc) 295 { 296 struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc; 297 298 return sc->sc_alloc_interrupts(sc); 299 } 300 301 static void 302 virtio_mmio_free_interrupts(struct virtio_softc *vsc) 303 { 304 struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc; 305 306 sc->sc_free_interrupts(sc); 307 } 308 309 static int 310 virtio_mmio_setup_interrupts(struct virtio_softc *vsc __unused, 311 int reinit __unused) 312 { 313 314 return 0; 315 } 316