1 /* $OpenBSD: virtio_mmio.c,v 1.3 2018/08/06 10:52:30 patrick 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 #define virtio_set_status(sc, s) virtio_mmio_set_status(sc, s) 79 #define virtio_device_reset(sc) virtio_set_status((sc), 0) 80 81 int virtio_mmio_match(struct device *, void *, void *); 82 void virtio_mmio_attach(struct device *, struct device *, void *); 83 int virtio_mmio_detach(struct device *, int); 84 85 void virtio_mmio_kick(struct virtio_softc *, uint16_t); 86 uint8_t virtio_mmio_read_device_config_1(struct virtio_softc *, int); 87 uint16_t virtio_mmio_read_device_config_2(struct virtio_softc *, int); 88 uint32_t virtio_mmio_read_device_config_4(struct virtio_softc *, int); 89 uint64_t virtio_mmio_read_device_config_8(struct virtio_softc *, int); 90 void virtio_mmio_write_device_config_1(struct virtio_softc *, int, uint8_t); 91 void virtio_mmio_write_device_config_2(struct virtio_softc *, int, uint16_t); 92 void virtio_mmio_write_device_config_4(struct virtio_softc *, int, uint32_t); 93 void virtio_mmio_write_device_config_8(struct virtio_softc *, int, uint64_t); 94 uint16_t virtio_mmio_read_queue_size(struct virtio_softc *, uint16_t); 95 void virtio_mmio_setup_queue(struct virtio_softc *, uint16_t, uint32_t); 96 void virtio_mmio_set_status(struct virtio_softc *, int); 97 uint32_t virtio_mmio_negotiate_features(struct virtio_softc *, uint32_t, 98 const struct virtio_feature_name *); 99 int virtio_mmio_intr(void *); 100 101 struct virtio_mmio_softc { 102 struct virtio_softc sc_sc; 103 104 bus_space_tag_t sc_iot; 105 bus_space_handle_t sc_ioh; 106 bus_size_t sc_iosize; 107 bus_dma_tag_t sc_dmat; 108 109 void *sc_ih; 110 111 int sc_config_offset; 112 }; 113 114 struct cfattach virtio_mmio_ca = { 115 sizeof(struct virtio_mmio_softc), 116 virtio_mmio_match, 117 virtio_mmio_attach, 118 virtio_mmio_detach, 119 NULL 120 }; 121 122 struct cfattach virtio_mmio_fdt_ca = { 123 sizeof(struct virtio_mmio_softc), 124 NULL, 125 virtio_mmio_attach, 126 virtio_mmio_detach, 127 NULL 128 }; 129 130 struct virtio_ops virtio_mmio_ops = { 131 virtio_mmio_kick, 132 virtio_mmio_read_device_config_1, 133 virtio_mmio_read_device_config_2, 134 virtio_mmio_read_device_config_4, 135 virtio_mmio_read_device_config_8, 136 virtio_mmio_write_device_config_1, 137 virtio_mmio_write_device_config_2, 138 virtio_mmio_write_device_config_4, 139 virtio_mmio_write_device_config_8, 140 virtio_mmio_read_queue_size, 141 virtio_mmio_setup_queue, 142 virtio_mmio_set_status, 143 virtio_mmio_negotiate_features, 144 virtio_mmio_intr, 145 }; 146 147 uint16_t 148 virtio_mmio_read_queue_size(struct virtio_softc *vsc, uint16_t idx) 149 { 150 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 151 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx); 152 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, 153 VIRTIO_MMIO_QUEUE_NUM_MAX); 154 } 155 156 void 157 virtio_mmio_setup_queue(struct virtio_softc *vsc, uint16_t idx, uint32_t addr) 158 { 159 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 160 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx); 161 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM, 162 bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX)); 163 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN, 164 PAGE_SIZE); 165 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN, addr); 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 uint32_t 304 virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint32_t guest_features, 305 const struct virtio_feature_name *guest_feature_names) 306 { 307 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 308 uint32_t host, neg; 309 310 /* 311 * indirect descriptors can be switched off by setting bit 1 in the 312 * driver flags, see config(8) 313 */ 314 if (!(vsc->sc_dev.dv_cfdata->cf_flags & 1) && 315 !(vsc->sc_child->dv_cfdata->cf_flags & 1)) { 316 guest_features |= VIRTIO_F_RING_INDIRECT_DESC; 317 } else { 318 printf("RingIndirectDesc disabled by UKC\n"); 319 } 320 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 321 VIRTIO_MMIO_HOST_FEATURES_SEL, 0); 322 host = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 323 VIRTIO_MMIO_HOST_FEATURES); 324 neg = host & guest_features; 325 #if VIRTIO_DEBUG 326 if (guest_feature_names) 327 virtio_log_features(host, neg, guest_feature_names); 328 #endif 329 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 330 VIRTIO_MMIO_GUEST_FEATURES_SEL, 0); 331 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 332 VIRTIO_MMIO_GUEST_FEATURES, neg); 333 vsc->sc_features = neg; 334 if (neg & VIRTIO_F_RING_INDIRECT_DESC) 335 vsc->sc_indirect = 1; 336 else 337 vsc->sc_indirect = 0; 338 339 return neg; 340 } 341 342 /* 343 * Device configuration registers. 344 */ 345 uint8_t 346 virtio_mmio_read_device_config_1(struct virtio_softc *vsc, int index) 347 { 348 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 349 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, 350 sc->sc_config_offset + index); 351 } 352 353 uint16_t 354 virtio_mmio_read_device_config_2(struct virtio_softc *vsc, int index) 355 { 356 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 357 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, 358 sc->sc_config_offset + index); 359 } 360 361 uint32_t 362 virtio_mmio_read_device_config_4(struct virtio_softc *vsc, int index) 363 { 364 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 365 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, 366 sc->sc_config_offset + index); 367 } 368 369 uint64_t 370 virtio_mmio_read_device_config_8(struct virtio_softc *vsc, int index) 371 { 372 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 373 uint64_t r; 374 375 r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 376 sc->sc_config_offset + index + sizeof(uint32_t)); 377 r <<= 32; 378 r += bus_space_read_4(sc->sc_iot, sc->sc_ioh, 379 sc->sc_config_offset + index); 380 return r; 381 } 382 383 void 384 virtio_mmio_write_device_config_1(struct virtio_softc *vsc, 385 int index, uint8_t value) 386 { 387 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 388 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 389 sc->sc_config_offset + index, value); 390 } 391 392 void 393 virtio_mmio_write_device_config_2(struct virtio_softc *vsc, 394 int index, uint16_t value) 395 { 396 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 397 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 398 sc->sc_config_offset + index, value); 399 } 400 401 void 402 virtio_mmio_write_device_config_4(struct virtio_softc *vsc, 403 int index, uint32_t value) 404 { 405 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 406 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 407 sc->sc_config_offset + index, value); 408 } 409 410 void 411 virtio_mmio_write_device_config_8(struct virtio_softc *vsc, 412 int index, uint64_t value) 413 { 414 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 415 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 416 sc->sc_config_offset + index, 417 value & 0xffffffff); 418 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 419 sc->sc_config_offset + index + sizeof(uint32_t), 420 value >> 32); 421 } 422 423 /* 424 * Interrupt handler. 425 */ 426 int 427 virtio_mmio_intr(void *arg) 428 { 429 struct virtio_mmio_softc *sc = arg; 430 struct virtio_softc *vsc = &sc->sc_sc; 431 int isr, r = 0; 432 433 /* check and ack the interrupt */ 434 isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 435 VIRTIO_MMIO_INTERRUPT_STATUS); 436 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 437 VIRTIO_MMIO_INTERRUPT_ACK, isr); 438 if ((isr & VIRTIO_MMIO_INT_CONFIG) && 439 (vsc->sc_config_change != NULL)) 440 r = (vsc->sc_config_change)(vsc); 441 if ((isr & VIRTIO_MMIO_INT_VRING)) 442 r |= virtio_check_vqs(vsc); 443 444 return r; 445 } 446 447 void 448 virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx) 449 { 450 struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc; 451 bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY, 452 idx); 453 } 454