2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Portions of this software were developed by Andrew Turner
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * VirtIO MMIO interface.
37 * This driver is heavily based on VirtIO PCI interface driver.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/malloc.h>
50 #include <sys/endian.h>
52 #include <machine/bus.h>
53 #include <machine/resource.h>
55 #include <dev/virtio/virtio.h>
56 #include <dev/virtio/virtqueue.h>
57 #include <dev/virtio/mmio/virtio_mmio.h>
59 #include "virtio_mmio_if.h"
60 #include "virtio_bus_if.h"
61 #include "virtio_if.h"
65 struct vtmmio_virtqueue {
66 struct virtqueue *vtv_vq;
70 static int vtmmio_detach(device_t);
71 static int vtmmio_suspend(device_t);
72 static int vtmmio_resume(device_t);
73 static int vtmmio_shutdown(device_t);
74 static void vtmmio_driver_added(device_t, driver_t *);
75 static void vtmmio_child_detached(device_t, device_t);
76 static int vtmmio_read_ivar(device_t, device_t, int, uintptr_t *);
77 static int vtmmio_write_ivar(device_t, device_t, int, uintptr_t);
78 static uint64_t vtmmio_negotiate_features(device_t, uint64_t);
79 static int vtmmio_with_feature(device_t, uint64_t);
80 static void vtmmio_set_virtqueue(struct vtmmio_softc *sc,
81 struct virtqueue *vq, uint32_t size);
82 static int vtmmio_alloc_virtqueues(device_t, int, int,
83 struct vq_alloc_info *);
84 static int vtmmio_setup_intr(device_t, enum intr_type);
85 static void vtmmio_stop(device_t);
86 static void vtmmio_poll(device_t);
87 static int vtmmio_reinit(device_t, uint64_t);
88 static void vtmmio_reinit_complete(device_t);
89 static void vtmmio_notify_virtqueue(device_t, uint16_t);
90 static uint8_t vtmmio_get_status(device_t);
91 static void vtmmio_set_status(device_t, uint8_t);
92 static void vtmmio_read_dev_config(device_t, bus_size_t, void *, int);
93 static void vtmmio_write_dev_config(device_t, bus_size_t, void *, int);
94 static void vtmmio_describe_features(struct vtmmio_softc *, const char *,
96 static void vtmmio_probe_and_attach_child(struct vtmmio_softc *);
97 static int vtmmio_reinit_virtqueue(struct vtmmio_softc *, int);
98 static void vtmmio_free_interrupts(struct vtmmio_softc *);
99 static void vtmmio_free_virtqueues(struct vtmmio_softc *);
100 static void vtmmio_release_child_resources(struct vtmmio_softc *);
101 static void vtmmio_reset(struct vtmmio_softc *);
102 static void vtmmio_select_virtqueue(struct vtmmio_softc *, int);
103 static void vtmmio_vq_intr(void *);
106 * I/O port read/write wrappers.
108 #define vtmmio_write_config_1(sc, o, v) \
110 if (sc->platform != NULL) \
111 VIRTIO_MMIO_PREWRITE(sc->platform, (o), (v)); \
112 bus_write_1((sc)->res[0], (o), (v)); \
113 if (sc->platform != NULL) \
114 VIRTIO_MMIO_NOTE(sc->platform, (o), (v)); \
116 #define vtmmio_write_config_2(sc, o, v) \
118 if (sc->platform != NULL) \
119 VIRTIO_MMIO_PREWRITE(sc->platform, (o), (v)); \
120 bus_write_2((sc)->res[0], (o), (v)); \
121 if (sc->platform != NULL) \
122 VIRTIO_MMIO_NOTE(sc->platform, (o), (v)); \
124 #define vtmmio_write_config_4(sc, o, v) \
126 if (sc->platform != NULL) \
127 VIRTIO_MMIO_PREWRITE(sc->platform, (o), (v)); \
128 bus_write_4((sc)->res[0], (o), (v)); \
129 if (sc->platform != NULL) \
130 VIRTIO_MMIO_NOTE(sc->platform, (o), (v)); \
133 #define vtmmio_read_config_1(sc, o) \
134 bus_read_1((sc)->res[0], (o))
135 #define vtmmio_read_config_2(sc, o) \
136 bus_read_2((sc)->res[0], (o))
137 #define vtmmio_read_config_4(sc, o) \
138 bus_read_4((sc)->res[0], (o))
140 static device_method_t vtmmio_methods[] = {
141 /* Device interface. */
142 DEVMETHOD(device_attach, vtmmio_attach),
143 DEVMETHOD(device_detach, vtmmio_detach),
144 DEVMETHOD(device_suspend, vtmmio_suspend),
145 DEVMETHOD(device_resume, vtmmio_resume),
146 DEVMETHOD(device_shutdown, vtmmio_shutdown),
149 DEVMETHOD(bus_driver_added, vtmmio_driver_added),
150 DEVMETHOD(bus_child_detached, vtmmio_child_detached),
151 DEVMETHOD(bus_child_pnpinfo_str, virtio_child_pnpinfo_str),
152 DEVMETHOD(bus_read_ivar, vtmmio_read_ivar),
153 DEVMETHOD(bus_write_ivar, vtmmio_write_ivar),
155 /* VirtIO bus interface. */
156 DEVMETHOD(virtio_bus_negotiate_features, vtmmio_negotiate_features),
157 DEVMETHOD(virtio_bus_with_feature, vtmmio_with_feature),
158 DEVMETHOD(virtio_bus_alloc_virtqueues, vtmmio_alloc_virtqueues),
159 DEVMETHOD(virtio_bus_setup_intr, vtmmio_setup_intr),
160 DEVMETHOD(virtio_bus_stop, vtmmio_stop),
161 DEVMETHOD(virtio_bus_poll, vtmmio_poll),
162 DEVMETHOD(virtio_bus_reinit, vtmmio_reinit),
163 DEVMETHOD(virtio_bus_reinit_complete, vtmmio_reinit_complete),
164 DEVMETHOD(virtio_bus_notify_vq, vtmmio_notify_virtqueue),
165 DEVMETHOD(virtio_bus_read_device_config, vtmmio_read_dev_config),
166 DEVMETHOD(virtio_bus_write_device_config, vtmmio_write_dev_config),
171 DEFINE_CLASS_0(virtio_mmio, vtmmio_driver, vtmmio_methods,
172 sizeof(struct vtmmio_softc));
174 MODULE_VERSION(virtio_mmio, 1);
177 vtmmio_setup_intr(device_t dev, enum intr_type type)
179 struct vtmmio_softc *sc;
183 sc = device_get_softc(dev);
185 if (sc->platform != NULL) {
186 err = VIRTIO_MMIO_SETUP_INTR(sc->platform, sc->dev,
189 /* Okay we have backend-specific interrupts */
195 sc->res[1] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
198 device_printf(dev, "Can't allocate interrupt\n");
202 if (bus_setup_intr(dev, sc->res[1], type | INTR_MPSAFE,
203 NULL, vtmmio_vq_intr, sc, &sc->ih)) {
204 device_printf(dev, "Can't setup the interrupt\n");
212 vtmmio_attach(device_t dev)
214 struct vtmmio_softc *sc;
218 sc = device_get_softc(dev);
222 sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
225 device_printf(dev, "Cannot allocate memory window.\n");
229 sc->vtmmio_version = vtmmio_read_config_4(sc, VIRTIO_MMIO_VERSION);
230 if (sc->vtmmio_version < 1 || sc->vtmmio_version > 2) {
231 device_printf(dev, "Unsupported version: %x\n",
233 bus_release_resource(dev, SYS_RES_MEMORY, 0,
241 /* Tell the host we've noticed this device. */
242 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
244 if ((child = device_add_child(dev, NULL, -1)) == NULL) {
245 device_printf(dev, "Cannot create child device.\n");
246 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
251 sc->vtmmio_child_dev = child;
252 vtmmio_probe_and_attach_child(sc);
258 vtmmio_detach(device_t dev)
260 struct vtmmio_softc *sc;
264 sc = device_get_softc(dev);
266 if ((child = sc->vtmmio_child_dev) != NULL) {
267 error = device_delete_child(dev, child);
270 sc->vtmmio_child_dev = NULL;
275 if (sc->res[0] != NULL) {
276 bus_release_resource(dev, SYS_RES_MEMORY, 0,
285 vtmmio_suspend(device_t dev)
288 return (bus_generic_suspend(dev));
292 vtmmio_resume(device_t dev)
295 return (bus_generic_resume(dev));
299 vtmmio_shutdown(device_t dev)
302 (void) bus_generic_shutdown(dev);
304 /* Forcibly stop the host device. */
311 vtmmio_driver_added(device_t dev, driver_t *driver)
313 struct vtmmio_softc *sc;
315 sc = device_get_softc(dev);
317 vtmmio_probe_and_attach_child(sc);
321 vtmmio_child_detached(device_t dev, device_t child)
323 struct vtmmio_softc *sc;
325 sc = device_get_softc(dev);
328 vtmmio_release_child_resources(sc);
332 vtmmio_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
334 struct vtmmio_softc *sc;
336 sc = device_get_softc(dev);
338 if (sc->vtmmio_child_dev != child)
342 case VIRTIO_IVAR_DEVTYPE:
343 case VIRTIO_IVAR_SUBDEVICE:
344 *result = vtmmio_read_config_4(sc, VIRTIO_MMIO_DEVICE_ID);
346 case VIRTIO_IVAR_VENDOR:
347 *result = vtmmio_read_config_4(sc, VIRTIO_MMIO_VENDOR_ID);
349 case VIRTIO_IVAR_SUBVENDOR:
350 case VIRTIO_IVAR_DEVICE:
352 * Dummy value for fields not present in this bus. Used by
353 * bus-agnostic virtio_child_pnpinfo_str.
365 vtmmio_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
367 struct vtmmio_softc *sc;
369 sc = device_get_softc(dev);
371 if (sc->vtmmio_child_dev != child)
375 case VIRTIO_IVAR_FEATURE_DESC:
376 sc->vtmmio_child_feat_desc = (void *) value;
386 vtmmio_negotiate_features(device_t dev, uint64_t child_features)
388 struct vtmmio_softc *sc;
389 uint64_t host_features, features;
391 sc = device_get_softc(dev);
393 vtmmio_write_config_4(sc, VIRTIO_MMIO_HOST_FEATURES_SEL, 1);
394 host_features = vtmmio_read_config_4(sc, VIRTIO_MMIO_HOST_FEATURES);
395 host_features <<= 32;
397 vtmmio_write_config_4(sc, VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
398 host_features |= vtmmio_read_config_4(sc, VIRTIO_MMIO_HOST_FEATURES);
400 vtmmio_describe_features(sc, "host", host_features);
403 * Limit negotiated features to what the driver, virtqueue, and
406 features = host_features & child_features;
407 features = virtqueue_filter_features(features);
408 sc->vtmmio_features = features;
410 vtmmio_describe_features(sc, "negotiated", features);
412 vtmmio_write_config_4(sc, VIRTIO_MMIO_GUEST_FEATURES_SEL, 1);
413 vtmmio_write_config_4(sc, VIRTIO_MMIO_GUEST_FEATURES, features >> 32);
415 vtmmio_write_config_4(sc, VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
416 vtmmio_write_config_4(sc, VIRTIO_MMIO_GUEST_FEATURES, features);
422 vtmmio_with_feature(device_t dev, uint64_t feature)
424 struct vtmmio_softc *sc;
426 sc = device_get_softc(dev);
428 return ((sc->vtmmio_features & feature) != 0);
432 vtmmio_set_virtqueue(struct vtmmio_softc *sc, struct virtqueue *vq,
437 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_NUM, size);
439 device_printf(dev, "virtqueue paddr 0x%08lx\n",
442 if (sc->vtmmio_version == 1) {
443 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_ALIGN,
444 VIRTIO_MMIO_VRING_ALIGN);
445 paddr = virtqueue_paddr(vq);
446 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_PFN,
447 paddr >> PAGE_SHIFT);
449 paddr = virtqueue_desc_paddr(vq);
450 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_DESC_LOW,
452 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_DESC_HIGH,
453 ((uint64_t)paddr) >> 32);
455 paddr = virtqueue_avail_paddr(vq);
456 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_AVAIL_LOW,
458 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_AVAIL_HIGH,
459 ((uint64_t)paddr) >> 32);
461 paddr = virtqueue_used_paddr(vq);
462 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_USED_LOW,
464 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_USED_HIGH,
465 ((uint64_t)paddr) >> 32);
467 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_READY, 1);
472 vtmmio_alloc_virtqueues(device_t dev, int flags, int nvqs,
473 struct vq_alloc_info *vq_info)
475 struct vtmmio_virtqueue *vqx;
476 struct vq_alloc_info *info;
477 struct vtmmio_softc *sc;
478 struct virtqueue *vq;
482 sc = device_get_softc(dev);
484 if (sc->vtmmio_nvqs != 0)
489 sc->vtmmio_vqs = malloc(nvqs * sizeof(struct vtmmio_virtqueue),
490 M_DEVBUF, M_NOWAIT | M_ZERO);
491 if (sc->vtmmio_vqs == NULL)
494 if (sc->vtmmio_version == 1) {
495 vtmmio_write_config_4(sc, VIRTIO_MMIO_GUEST_PAGE_SIZE,
499 for (idx = 0; idx < nvqs; idx++) {
500 vqx = &sc->vtmmio_vqs[idx];
501 info = &vq_info[idx];
503 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_SEL, idx);
505 vtmmio_select_virtqueue(sc, idx);
506 size = vtmmio_read_config_4(sc, VIRTIO_MMIO_QUEUE_NUM_MAX);
508 error = virtqueue_alloc(dev, idx, size,
509 VIRTIO_MMIO_VRING_ALIGN, ~(vm_paddr_t)0, info, &vq);
512 "cannot allocate virtqueue %d: %d\n",
517 vtmmio_set_virtqueue(sc, vq, size);
519 vqx->vtv_vq = *info->vqai_vq = vq;
520 vqx->vtv_no_intr = info->vqai_intr == NULL;
526 vtmmio_free_virtqueues(sc);
532 vtmmio_stop(device_t dev)
535 vtmmio_reset(device_get_softc(dev));
539 vtmmio_poll(device_t dev)
541 struct vtmmio_softc *sc;
543 sc = device_get_softc(dev);
545 if (sc->platform != NULL)
546 VIRTIO_MMIO_POLL(sc->platform);
550 vtmmio_reinit(device_t dev, uint64_t features)
552 struct vtmmio_softc *sc;
555 sc = device_get_softc(dev);
557 if (vtmmio_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
561 * Quickly drive the status through ACK and DRIVER. The device
562 * does not become usable again until vtmmio_reinit_complete().
564 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
565 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
567 vtmmio_negotiate_features(dev, features);
569 if (sc->vtmmio_version == 1) {
570 vtmmio_write_config_4(sc, VIRTIO_MMIO_GUEST_PAGE_SIZE,
574 for (idx = 0; idx < sc->vtmmio_nvqs; idx++) {
575 error = vtmmio_reinit_virtqueue(sc, idx);
584 vtmmio_reinit_complete(device_t dev)
587 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
591 vtmmio_notify_virtqueue(device_t dev, uint16_t queue)
593 struct vtmmio_softc *sc;
595 sc = device_get_softc(dev);
597 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_NOTIFY, queue);
601 vtmmio_get_status(device_t dev)
603 struct vtmmio_softc *sc;
605 sc = device_get_softc(dev);
607 return (vtmmio_read_config_4(sc, VIRTIO_MMIO_STATUS));
611 vtmmio_set_status(device_t dev, uint8_t status)
613 struct vtmmio_softc *sc;
615 sc = device_get_softc(dev);
617 if (status != VIRTIO_CONFIG_STATUS_RESET)
618 status |= vtmmio_get_status(dev);
620 vtmmio_write_config_4(sc, VIRTIO_MMIO_STATUS, status);
624 vtmmio_read_dev_config(device_t dev, bus_size_t offset,
625 void *dst, int length)
627 struct vtmmio_softc *sc;
631 uint64_t low32, high32;
633 sc = device_get_softc(dev);
634 off = VIRTIO_MMIO_CONFIG + offset;
637 * The non-legacy MMIO specification adds the following restriction:
639 * 4.2.2.2: For the device-specific configuration space, the driver
640 * MUST use 8 bit wide accesses for 8 bit wide fields, 16 bit wide
641 * and aligned accesses for 16 bit wide fields and 32 bit wide and
642 * aligned accesses for 32 and 64 bit wide fields.
644 * The endianness also varies between non-legacy and legacy:
646 * 2.4: Note: The device configuration space uses the little-endian
647 * format for multi-byte fields.
649 * 2.4.3: Note that for legacy interfaces, device configuration space
650 * is generally the guest’s native endian, rather than PCI’s
651 * little-endian. The correct endian-ness is documented for each
654 if (sc->vtmmio_version > 1) {
657 *(uint8_t *)dst = vtmmio_read_config_1(sc, off);
661 le16toh(vtmmio_read_config_2(sc, off));
665 le32toh(vtmmio_read_config_4(sc, off));
668 low32 = le32toh(vtmmio_read_config_4(sc, off));
669 high32 = le32toh(vtmmio_read_config_4(sc, off + 4));
670 *(uint64_t *)dst = (high32 << 32) | low32;
673 panic("%s: invalid length %d\n", __func__, length);
679 for (d = dst; length > 0; d += size, off += size, length -= size) {
680 #ifdef ALLOW_WORD_ALIGNED_ACCESS
683 *(uint32_t *)d = vtmmio_read_config_4(sc, off);
684 } else if (length >= 2) {
686 *(uint16_t *)d = vtmmio_read_config_2(sc, off);
691 *d = vtmmio_read_config_1(sc, off);
697 vtmmio_write_dev_config(device_t dev, bus_size_t offset,
698 void *src, int length)
700 struct vtmmio_softc *sc;
705 sc = device_get_softc(dev);
706 off = VIRTIO_MMIO_CONFIG + offset;
709 * The non-legacy MMIO specification adds size and alignment
710 * restrctions. It also changes the endianness from native-endian to
711 * little-endian. See vtmmio_read_dev_config.
713 if (sc->vtmmio_version > 1) {
716 vtmmio_write_config_1(sc, off, *(uint8_t *)src);
719 vtmmio_write_config_2(sc, off,
720 htole16(*(uint16_t *)src));
723 vtmmio_write_config_4(sc, off,
724 htole32(*(uint32_t *)src));
727 vtmmio_write_config_4(sc, off,
728 htole32(*(uint64_t *)src));
729 vtmmio_write_config_4(sc, off + 4,
730 htole32((*(uint64_t *)src) >> 32));
733 panic("%s: invalid length %d\n", __func__, length);
739 for (s = src; length > 0; s += size, off += size, length -= size) {
740 #ifdef ALLOW_WORD_ALIGNED_ACCESS
743 vtmmio_write_config_4(sc, off, *(uint32_t *)s);
744 } else if (length >= 2) {
746 vtmmio_write_config_2(sc, off, *(uint16_t *)s);
751 vtmmio_write_config_1(sc, off, *s);
757 vtmmio_describe_features(struct vtmmio_softc *sc, const char *msg,
763 child = sc->vtmmio_child_dev;
765 if (device_is_attached(child) || bootverbose == 0)
768 virtio_describe(dev, msg, features, sc->vtmmio_child_feat_desc);
772 vtmmio_probe_and_attach_child(struct vtmmio_softc *sc)
777 child = sc->vtmmio_child_dev;
782 if (device_get_state(child) != DS_NOTPRESENT) {
786 if (device_probe(child) != 0) {
790 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
791 if (device_attach(child) != 0) {
792 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
794 vtmmio_release_child_resources(sc);
795 /* Reset status for future attempt. */
796 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
798 vtmmio_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
799 VIRTIO_ATTACH_COMPLETED(child);
804 vtmmio_reinit_virtqueue(struct vtmmio_softc *sc, int idx)
806 struct vtmmio_virtqueue *vqx;
807 struct virtqueue *vq;
811 vqx = &sc->vtmmio_vqs[idx];
814 KASSERT(vq != NULL, ("%s: vq %d not allocated", __func__, idx));
816 vtmmio_select_virtqueue(sc, idx);
817 size = vtmmio_read_config_4(sc, VIRTIO_MMIO_QUEUE_NUM_MAX);
819 error = virtqueue_reinit(vq, size);
823 vtmmio_set_virtqueue(sc, vq, size);
829 vtmmio_free_interrupts(struct vtmmio_softc *sc)
833 bus_teardown_intr(sc->dev, sc->res[1], sc->ih);
835 if (sc->res[1] != NULL)
836 bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->res[1]);
840 vtmmio_free_virtqueues(struct vtmmio_softc *sc)
842 struct vtmmio_virtqueue *vqx;
845 for (idx = 0; idx < sc->vtmmio_nvqs; idx++) {
846 vqx = &sc->vtmmio_vqs[idx];
848 vtmmio_select_virtqueue(sc, idx);
849 if (sc->vtmmio_version == 1)
850 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_PFN, 0);
852 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_READY, 0);
854 virtqueue_free(vqx->vtv_vq);
858 free(sc->vtmmio_vqs, M_DEVBUF);
859 sc->vtmmio_vqs = NULL;
864 vtmmio_release_child_resources(struct vtmmio_softc *sc)
867 vtmmio_free_interrupts(sc);
868 vtmmio_free_virtqueues(sc);
872 vtmmio_reset(struct vtmmio_softc *sc)
876 * Setting the status to RESET sets the host device to
877 * the original, uninitialized state.
879 vtmmio_set_status(sc->dev, VIRTIO_CONFIG_STATUS_RESET);
883 vtmmio_select_virtqueue(struct vtmmio_softc *sc, int idx)
886 vtmmio_write_config_4(sc, VIRTIO_MMIO_QUEUE_SEL, idx);
890 vtmmio_vq_intr(void *arg)
892 struct vtmmio_virtqueue *vqx;
893 struct vtmmio_softc *sc;
894 struct virtqueue *vq;
900 status = vtmmio_read_config_4(sc, VIRTIO_MMIO_INTERRUPT_STATUS);
901 vtmmio_write_config_4(sc, VIRTIO_MMIO_INTERRUPT_ACK, status);
903 /* The config changed */
904 if (status & VIRTIO_MMIO_INT_CONFIG)
905 if (sc->vtmmio_child_dev != NULL)
906 VIRTIO_CONFIG_CHANGE(sc->vtmmio_child_dev);
908 /* Notify all virtqueues. */
909 if (status & VIRTIO_MMIO_INT_VRING) {
910 for (idx = 0; idx < sc->vtmmio_nvqs; idx++) {
911 vqx = &sc->vtmmio_vqs[idx];
912 if (vqx->vtv_no_intr == 0) {