2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /* Driver for the VirtIO PCI interface. */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/endian.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
50 #include <dev/virtio/virtio.h>
51 #include <dev/virtio/virtqueue.h>
52 #include <dev/virtio/pci/virtio_pci.h>
54 #include "virtio_bus_if.h"
55 #include "virtio_if.h"
57 struct vtpci_interrupt {
58 struct resource *vti_irq;
63 struct vtpci_virtqueue {
64 struct virtqueue *vtv_vq;
70 struct resource *vtpci_res;
71 struct resource *vtpci_msix_res;
72 uint64_t vtpci_features;
74 #define VTPCI_FLAG_NO_MSI 0x0001
75 #define VTPCI_FLAG_NO_MSIX 0x0002
76 #define VTPCI_FLAG_LEGACY 0x1000
77 #define VTPCI_FLAG_MSI 0x2000
78 #define VTPCI_FLAG_MSIX 0x4000
79 #define VTPCI_FLAG_SHARED_MSIX 0x8000
80 #define VTPCI_FLAG_ITYPE_MASK 0xF000
82 /* This "bus" will only ever have one child. */
83 device_t vtpci_child_dev;
84 struct virtio_feature_desc *vtpci_child_feat_desc;
87 struct vtpci_virtqueue *vtpci_vqs;
90 * Ideally, each virtqueue that the driver provides a callback for will
91 * receive its own MSIX vector. If there are not sufficient vectors
92 * available, then attempt to have all the VQs share one vector. For
93 * MSIX, the configuration changed notifications must be on their own
96 * If MSIX is not available, we will attempt to have the whole device
97 * share one MSI vector, and then, finally, one legacy interrupt.
99 struct vtpci_interrupt vtpci_device_interrupt;
100 struct vtpci_interrupt *vtpci_msix_vq_interrupts;
101 int vtpci_nmsix_resources;
104 static int vtpci_probe(device_t);
105 static int vtpci_attach(device_t);
106 static int vtpci_detach(device_t);
107 static int vtpci_suspend(device_t);
108 static int vtpci_resume(device_t);
109 static int vtpci_shutdown(device_t);
110 static void vtpci_driver_added(device_t, driver_t *);
111 static void vtpci_child_detached(device_t, device_t);
112 static int vtpci_read_ivar(device_t, device_t, int, uintptr_t *);
113 static int vtpci_write_ivar(device_t, device_t, int, uintptr_t);
115 static uint64_t vtpci_negotiate_features(device_t, uint64_t);
116 static int vtpci_with_feature(device_t, uint64_t);
117 static int vtpci_alloc_virtqueues(device_t, int, int,
118 struct vq_alloc_info *);
119 static int vtpci_setup_intr(device_t, enum intr_type);
120 static void vtpci_stop(device_t);
121 static int vtpci_reinit(device_t, uint64_t);
122 static void vtpci_reinit_complete(device_t);
123 static void vtpci_notify_virtqueue(device_t, uint16_t);
124 static uint8_t vtpci_get_status(device_t);
125 static void vtpci_set_status(device_t, uint8_t);
126 static void vtpci_read_dev_config(device_t, bus_size_t, void *, int);
127 static void vtpci_write_dev_config(device_t, bus_size_t, void *, int);
129 static void vtpci_describe_features(struct vtpci_softc *, const char *,
131 static void vtpci_probe_and_attach_child(struct vtpci_softc *);
133 static int vtpci_alloc_msix(struct vtpci_softc *, int);
134 static int vtpci_alloc_msi(struct vtpci_softc *);
135 static int vtpci_alloc_intr_msix_pervq(struct vtpci_softc *);
136 static int vtpci_alloc_intr_msix_shared(struct vtpci_softc *);
137 static int vtpci_alloc_intr_msi(struct vtpci_softc *);
138 static int vtpci_alloc_intr_legacy(struct vtpci_softc *);
139 static int vtpci_alloc_interrupt(struct vtpci_softc *, int, int,
140 struct vtpci_interrupt *);
141 static int vtpci_alloc_intr_resources(struct vtpci_softc *);
143 static int vtpci_setup_legacy_interrupt(struct vtpci_softc *,
145 static int vtpci_setup_pervq_msix_interrupts(struct vtpci_softc *,
147 static int vtpci_setup_msix_interrupts(struct vtpci_softc *,
149 static int vtpci_setup_interrupts(struct vtpci_softc *, enum intr_type);
151 static int vtpci_register_msix_vector(struct vtpci_softc *, int,
152 struct vtpci_interrupt *);
153 static int vtpci_set_host_msix_vectors(struct vtpci_softc *);
154 static int vtpci_reinit_virtqueue(struct vtpci_softc *, int);
156 static void vtpci_free_interrupt(struct vtpci_softc *,
157 struct vtpci_interrupt *);
158 static void vtpci_free_interrupts(struct vtpci_softc *);
159 static void vtpci_free_virtqueues(struct vtpci_softc *);
160 static void vtpci_release_child_resources(struct vtpci_softc *);
161 static void vtpci_cleanup_setup_intr_attempt(struct vtpci_softc *);
162 static void vtpci_reset(struct vtpci_softc *);
164 static void vtpci_select_virtqueue(struct vtpci_softc *, int);
166 static void vtpci_legacy_intr(void *);
167 static int vtpci_vq_shared_intr_filter(void *);
168 static void vtpci_vq_shared_intr(void *);
169 static int vtpci_vq_intr_filter(void *);
170 static void vtpci_vq_intr(void *);
171 static void vtpci_config_intr(void *);
173 #define vtpci_setup_msi_interrupt vtpci_setup_legacy_interrupt
175 #define VIRTIO_PCI_CONFIG(_sc) \
176 VIRTIO_PCI_CONFIG_OFF((((_sc)->vtpci_flags & VTPCI_FLAG_MSIX)) != 0)
179 * I/O port read/write wrappers.
181 #define vtpci_read_config_1(sc, o) bus_read_1((sc)->vtpci_res, (o))
182 #define vtpci_read_config_2(sc, o) bus_read_2((sc)->vtpci_res, (o))
183 #define vtpci_read_config_4(sc, o) bus_read_4((sc)->vtpci_res, (o))
184 #define vtpci_write_config_1(sc, o, v) bus_write_1((sc)->vtpci_res, (o), (v))
185 #define vtpci_write_config_2(sc, o, v) bus_write_2((sc)->vtpci_res, (o), (v))
186 #define vtpci_write_config_4(sc, o, v) bus_write_4((sc)->vtpci_res, (o), (v))
189 * Legacy VirtIO header is always PCI endianness (little), so if we
190 * are in a BE machine we need to swap bytes from LE to BE when reading
191 * and from BE to LE when writing.
192 * If we are in a LE machine, there will be no swaps.
194 #define vtpci_read_header_2(sc, o) le16toh(vtpci_read_config_2(sc, o))
195 #define vtpci_read_header_4(sc, o) le32toh(vtpci_read_config_4(sc, o))
196 #define vtpci_write_header_2(sc, o, v) vtpci_write_config_2(sc, o, (htole16(v)))
197 #define vtpci_write_header_4(sc, o, v) vtpci_write_config_4(sc, o, (htole32(v)))
200 static int vtpci_disable_msix = 0;
201 TUNABLE_INT("hw.virtio.pci.disable_msix", &vtpci_disable_msix);
203 static device_method_t vtpci_methods[] = {
204 /* Device interface. */
205 DEVMETHOD(device_probe, vtpci_probe),
206 DEVMETHOD(device_attach, vtpci_attach),
207 DEVMETHOD(device_detach, vtpci_detach),
208 DEVMETHOD(device_suspend, vtpci_suspend),
209 DEVMETHOD(device_resume, vtpci_resume),
210 DEVMETHOD(device_shutdown, vtpci_shutdown),
213 DEVMETHOD(bus_driver_added, vtpci_driver_added),
214 DEVMETHOD(bus_child_detached, vtpci_child_detached),
215 DEVMETHOD(bus_child_pnpinfo_str, virtio_child_pnpinfo_str),
216 DEVMETHOD(bus_read_ivar, vtpci_read_ivar),
217 DEVMETHOD(bus_write_ivar, vtpci_write_ivar),
219 /* VirtIO bus interface. */
220 DEVMETHOD(virtio_bus_negotiate_features, vtpci_negotiate_features),
221 DEVMETHOD(virtio_bus_with_feature, vtpci_with_feature),
222 DEVMETHOD(virtio_bus_alloc_virtqueues, vtpci_alloc_virtqueues),
223 DEVMETHOD(virtio_bus_setup_intr, vtpci_setup_intr),
224 DEVMETHOD(virtio_bus_stop, vtpci_stop),
225 DEVMETHOD(virtio_bus_reinit, vtpci_reinit),
226 DEVMETHOD(virtio_bus_reinit_complete, vtpci_reinit_complete),
227 DEVMETHOD(virtio_bus_notify_vq, vtpci_notify_virtqueue),
228 DEVMETHOD(virtio_bus_read_device_config, vtpci_read_dev_config),
229 DEVMETHOD(virtio_bus_write_device_config, vtpci_write_dev_config),
234 static driver_t vtpci_driver = {
237 sizeof(struct vtpci_softc)
240 devclass_t vtpci_devclass;
242 DRIVER_MODULE(virtio_pci, pci, vtpci_driver, vtpci_devclass, 0, 0);
243 MODULE_VERSION(virtio_pci, 1);
244 MODULE_DEPEND(virtio_pci, pci, 1, 1, 1);
245 MODULE_DEPEND(virtio_pci, virtio, 1, 1, 1);
248 vtpci_probe(device_t dev)
253 if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
256 if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
257 pci_get_device(dev) > VIRTIO_PCI_DEVICEID_MAX)
260 if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
263 name = virtio_device_name(pci_get_subdevice(dev));
267 snprintf(desc, sizeof(desc), "VirtIO PCI %s adapter", name);
268 device_set_desc_copy(dev, desc);
270 return (BUS_PROBE_DEFAULT);
274 vtpci_attach(device_t dev)
276 struct vtpci_softc *sc;
280 sc = device_get_softc(dev);
283 pci_enable_busmaster(dev);
286 sc->vtpci_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
288 if (sc->vtpci_res == NULL) {
289 device_printf(dev, "cannot map I/O space\n");
294 * For legacy VirtIO, the device-specific configuration is guest
295 * endian, while the common configuration header is always
296 * PCI (little) endian and will be handled specifically in
297 * other parts of this file via functions
298 * 'vtpci_[read|write]_header_[2|4]'
300 #if _BYTE_ORDER == _BIG_ENDIAN
301 rman_set_bustag(sc->vtpci_res, &bs_be_tag);
304 if (pci_find_cap(dev, PCIY_MSI, NULL) != 0)
305 sc->vtpci_flags |= VTPCI_FLAG_NO_MSI;
307 if (pci_find_cap(dev, PCIY_MSIX, NULL) == 0) {
309 sc->vtpci_msix_res = bus_alloc_resource_any(dev,
310 SYS_RES_MEMORY, &rid, RF_ACTIVE);
313 if (sc->vtpci_msix_res == NULL)
314 sc->vtpci_flags |= VTPCI_FLAG_NO_MSIX;
318 /* Tell the host we've noticed this device. */
319 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
321 if ((child = device_add_child(dev, NULL, -1)) == NULL) {
322 device_printf(dev, "cannot create child device\n");
323 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
328 sc->vtpci_child_dev = child;
329 vtpci_probe_and_attach_child(sc);
335 vtpci_detach(device_t dev)
337 struct vtpci_softc *sc;
341 sc = device_get_softc(dev);
343 if ((child = sc->vtpci_child_dev) != NULL) {
344 error = device_delete_child(dev, child);
347 sc->vtpci_child_dev = NULL;
352 if (sc->vtpci_msix_res != NULL) {
353 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(1),
355 sc->vtpci_msix_res = NULL;
358 if (sc->vtpci_res != NULL) {
359 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0),
361 sc->vtpci_res = NULL;
368 vtpci_suspend(device_t dev)
371 return (bus_generic_suspend(dev));
375 vtpci_resume(device_t dev)
378 return (bus_generic_resume(dev));
382 vtpci_shutdown(device_t dev)
385 (void) bus_generic_shutdown(dev);
386 /* Forcibly stop the host device. */
393 vtpci_driver_added(device_t dev, driver_t *driver)
395 struct vtpci_softc *sc;
397 sc = device_get_softc(dev);
399 vtpci_probe_and_attach_child(sc);
403 vtpci_child_detached(device_t dev, device_t child)
405 struct vtpci_softc *sc;
407 sc = device_get_softc(dev);
410 vtpci_release_child_resources(sc);
414 vtpci_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
416 struct vtpci_softc *sc;
418 sc = device_get_softc(dev);
420 if (sc->vtpci_child_dev != child)
424 case VIRTIO_IVAR_DEVTYPE:
425 case VIRTIO_IVAR_SUBDEVICE:
426 *result = pci_get_subdevice(dev);
428 case VIRTIO_IVAR_VENDOR:
429 *result = pci_get_vendor(dev);
431 case VIRTIO_IVAR_DEVICE:
432 *result = pci_get_device(dev);
434 case VIRTIO_IVAR_SUBVENDOR:
435 *result = pci_get_subvendor(dev);
445 vtpci_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
447 struct vtpci_softc *sc;
449 sc = device_get_softc(dev);
451 if (sc->vtpci_child_dev != child)
455 case VIRTIO_IVAR_FEATURE_DESC:
456 sc->vtpci_child_feat_desc = (void *) value;
466 vtpci_negotiate_features(device_t dev, uint64_t child_features)
468 struct vtpci_softc *sc;
469 uint64_t host_features, features;
471 sc = device_get_softc(dev);
473 host_features = vtpci_read_header_4(sc, VIRTIO_PCI_HOST_FEATURES);
474 vtpci_describe_features(sc, "host", host_features);
477 * Limit negotiated features to what the driver, virtqueue, and
480 features = host_features & child_features;
481 features = virtqueue_filter_features(features);
482 sc->vtpci_features = features;
484 vtpci_describe_features(sc, "negotiated", features);
485 vtpci_write_header_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
491 vtpci_with_feature(device_t dev, uint64_t feature)
493 struct vtpci_softc *sc;
495 sc = device_get_softc(dev);
497 return ((sc->vtpci_features & feature) != 0);
501 vtpci_alloc_virtqueues(device_t dev, int flags, int nvqs,
502 struct vq_alloc_info *vq_info)
504 struct vtpci_softc *sc;
505 struct virtqueue *vq;
506 struct vtpci_virtqueue *vqx;
507 struct vq_alloc_info *info;
511 sc = device_get_softc(dev);
513 if (sc->vtpci_nvqs != 0)
518 sc->vtpci_vqs = malloc(nvqs * sizeof(struct vtpci_virtqueue),
519 M_DEVBUF, M_NOWAIT | M_ZERO);
520 if (sc->vtpci_vqs == NULL)
523 for (idx = 0; idx < nvqs; idx++) {
524 vqx = &sc->vtpci_vqs[idx];
525 info = &vq_info[idx];
527 vtpci_select_virtqueue(sc, idx);
528 size = vtpci_read_header_2(sc, VIRTIO_PCI_QUEUE_NUM);
530 error = virtqueue_alloc(dev, idx, size, VIRTIO_PCI_VRING_ALIGN,
531 ~(vm_paddr_t)0, info, &vq);
534 "cannot allocate virtqueue %d: %d\n", idx, error);
538 vtpci_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN,
539 virtqueue_paddr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
541 vqx->vtv_vq = *info->vqai_vq = vq;
542 vqx->vtv_no_intr = info->vqai_intr == NULL;
548 vtpci_free_virtqueues(sc);
554 vtpci_setup_intr(device_t dev, enum intr_type type)
556 struct vtpci_softc *sc;
559 sc = device_get_softc(dev);
561 for (attempt = 0; attempt < 5; attempt++) {
563 * Start with the most desirable interrupt configuration and
564 * fallback towards less desirable ones.
568 error = vtpci_alloc_intr_msix_pervq(sc);
571 error = vtpci_alloc_intr_msix_shared(sc);
574 error = vtpci_alloc_intr_msi(sc);
577 error = vtpci_alloc_intr_legacy(sc);
581 "exhausted all interrupt allocation attempts\n");
585 if (error == 0 && vtpci_setup_interrupts(sc, type) == 0)
588 vtpci_cleanup_setup_intr_attempt(sc);
592 if (sc->vtpci_flags & VTPCI_FLAG_LEGACY)
593 device_printf(dev, "using legacy interrupt\n");
594 else if (sc->vtpci_flags & VTPCI_FLAG_MSI)
595 device_printf(dev, "using MSI interrupt\n");
596 else if (sc->vtpci_flags & VTPCI_FLAG_SHARED_MSIX)
597 device_printf(dev, "using shared MSIX interrupts\n");
599 device_printf(dev, "using per VQ MSIX interrupts\n");
606 vtpci_stop(device_t dev)
609 vtpci_reset(device_get_softc(dev));
613 vtpci_reinit(device_t dev, uint64_t features)
615 struct vtpci_softc *sc;
618 sc = device_get_softc(dev);
621 * Redrive the device initialization. This is a bit of an abuse of
622 * the specification, but VirtualBox, QEMU/KVM, and BHyVe seem to
625 * We do not allow the host device to change from what was originally
626 * negotiated beyond what the guest driver changed. MSIX state should
627 * not change, number of virtqueues and their size remain the same, etc.
628 * This will need to be rethought when we want to support migration.
631 if (vtpci_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
635 * Quickly drive the status through ACK and DRIVER. The device
636 * does not become usable again until vtpci_reinit_complete().
638 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
639 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
641 vtpci_negotiate_features(dev, features);
643 for (idx = 0; idx < sc->vtpci_nvqs; idx++) {
644 error = vtpci_reinit_virtqueue(sc, idx);
649 if (sc->vtpci_flags & VTPCI_FLAG_MSIX) {
650 error = vtpci_set_host_msix_vectors(sc);
659 vtpci_reinit_complete(device_t dev)
662 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
666 vtpci_notify_virtqueue(device_t dev, uint16_t queue)
668 struct vtpci_softc *sc;
670 sc = device_get_softc(dev);
672 vtpci_write_header_2(sc, VIRTIO_PCI_QUEUE_NOTIFY, queue);
676 vtpci_get_status(device_t dev)
678 struct vtpci_softc *sc;
680 sc = device_get_softc(dev);
682 return (vtpci_read_config_1(sc, VIRTIO_PCI_STATUS));
686 vtpci_set_status(device_t dev, uint8_t status)
688 struct vtpci_softc *sc;
690 sc = device_get_softc(dev);
692 if (status != VIRTIO_CONFIG_STATUS_RESET)
693 status |= vtpci_get_status(dev);
695 vtpci_write_config_1(sc, VIRTIO_PCI_STATUS, status);
699 vtpci_read_dev_config(device_t dev, bus_size_t offset,
700 void *dst, int length)
702 struct vtpci_softc *sc;
707 sc = device_get_softc(dev);
708 off = VIRTIO_PCI_CONFIG(sc) + offset;
710 for (d = dst; length > 0; d += size, off += size, length -= size) {
713 *(uint32_t *)d = vtpci_read_config_4(sc, off);
714 } else if (length >= 2) {
716 *(uint16_t *)d = vtpci_read_config_2(sc, off);
719 *d = vtpci_read_config_1(sc, off);
725 vtpci_write_dev_config(device_t dev, bus_size_t offset,
726 void *src, int length)
728 struct vtpci_softc *sc;
733 sc = device_get_softc(dev);
734 off = VIRTIO_PCI_CONFIG(sc) + offset;
736 for (s = src; length > 0; s += size, off += size, length -= size) {
739 vtpci_write_config_4(sc, off, *(uint32_t *)s);
740 } else if (length >= 2) {
742 vtpci_write_config_2(sc, off, *(uint16_t *)s);
745 vtpci_write_config_1(sc, off, *s);
751 vtpci_describe_features(struct vtpci_softc *sc, const char *msg,
757 child = sc->vtpci_child_dev;
759 if (device_is_attached(child) || bootverbose == 0)
762 virtio_describe(dev, msg, features, sc->vtpci_child_feat_desc);
766 vtpci_probe_and_attach_child(struct vtpci_softc *sc)
771 child = sc->vtpci_child_dev;
776 if (device_get_state(child) != DS_NOTPRESENT)
779 if (device_probe(child) != 0)
782 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
783 if (device_attach(child) != 0) {
784 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
786 vtpci_release_child_resources(sc);
787 /* Reset status for future attempt. */
788 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
790 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
791 VIRTIO_ATTACH_COMPLETED(child);
796 vtpci_alloc_msix(struct vtpci_softc *sc, int nvectors)
799 int nmsix, cnt, required;
803 /* Allocate an additional vector for the config changes. */
804 required = nvectors + 1;
806 nmsix = pci_msix_count(dev);
807 if (nmsix < required)
811 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
812 sc->vtpci_nmsix_resources = required;
816 pci_release_msi(dev);
822 vtpci_alloc_msi(struct vtpci_softc *sc)
825 int nmsi, cnt, required;
830 nmsi = pci_msi_count(dev);
835 if (pci_alloc_msi(dev, &cnt) == 0 && cnt >= required)
838 pci_release_msi(dev);
844 vtpci_alloc_intr_msix_pervq(struct vtpci_softc *sc)
846 int i, nvectors, error;
848 if (vtpci_disable_msix != 0 ||
849 sc->vtpci_flags & VTPCI_FLAG_NO_MSIX)
852 for (nvectors = 0, i = 0; i < sc->vtpci_nvqs; i++) {
853 if (sc->vtpci_vqs[i].vtv_no_intr == 0)
857 error = vtpci_alloc_msix(sc, nvectors);
861 sc->vtpci_flags |= VTPCI_FLAG_MSIX;
867 vtpci_alloc_intr_msix_shared(struct vtpci_softc *sc)
871 if (vtpci_disable_msix != 0 ||
872 sc->vtpci_flags & VTPCI_FLAG_NO_MSIX)
875 error = vtpci_alloc_msix(sc, 1);
879 sc->vtpci_flags |= VTPCI_FLAG_MSIX | VTPCI_FLAG_SHARED_MSIX;
885 vtpci_alloc_intr_msi(struct vtpci_softc *sc)
889 /* Only BHyVe supports MSI. */
890 if (sc->vtpci_flags & VTPCI_FLAG_NO_MSI)
893 error = vtpci_alloc_msi(sc);
897 sc->vtpci_flags |= VTPCI_FLAG_MSI;
903 vtpci_alloc_intr_legacy(struct vtpci_softc *sc)
906 sc->vtpci_flags |= VTPCI_FLAG_LEGACY;
912 vtpci_alloc_interrupt(struct vtpci_softc *sc, int rid, int flags,
913 struct vtpci_interrupt *intr)
915 struct resource *irq;
917 irq = bus_alloc_resource_any(sc->vtpci_dev, SYS_RES_IRQ, &rid, flags);
928 vtpci_alloc_intr_resources(struct vtpci_softc *sc)
930 struct vtpci_interrupt *intr;
931 int i, rid, flags, nvq_intrs, error;
936 if (sc->vtpci_flags & VTPCI_FLAG_LEGACY)
937 flags |= RF_SHAREABLE;
942 * For legacy and MSI interrupts, this single resource handles all
943 * interrupts. For MSIX, this resource is used for the configuration
946 intr = &sc->vtpci_device_interrupt;
947 error = vtpci_alloc_interrupt(sc, rid, flags, intr);
948 if (error || sc->vtpci_flags & (VTPCI_FLAG_LEGACY | VTPCI_FLAG_MSI))
951 /* Subtract one for the configuration changed interrupt. */
952 nvq_intrs = sc->vtpci_nmsix_resources - 1;
954 intr = sc->vtpci_msix_vq_interrupts = malloc(nvq_intrs *
955 sizeof(struct vtpci_interrupt), M_DEVBUF, M_NOWAIT | M_ZERO);
956 if (sc->vtpci_msix_vq_interrupts == NULL)
959 for (i = 0, rid++; i < nvq_intrs; i++, rid++, intr++) {
960 error = vtpci_alloc_interrupt(sc, rid, flags, intr);
969 vtpci_setup_legacy_interrupt(struct vtpci_softc *sc, enum intr_type type)
971 struct vtpci_interrupt *intr;
974 intr = &sc->vtpci_device_interrupt;
975 error = bus_setup_intr(sc->vtpci_dev, intr->vti_irq, type, NULL,
976 vtpci_legacy_intr, sc, &intr->vti_handler);
982 vtpci_setup_pervq_msix_interrupts(struct vtpci_softc *sc, enum intr_type type)
984 struct vtpci_virtqueue *vqx;
985 struct vtpci_interrupt *intr;
988 intr = sc->vtpci_msix_vq_interrupts;
990 for (i = 0; i < sc->vtpci_nvqs; i++) {
991 vqx = &sc->vtpci_vqs[i];
993 if (vqx->vtv_no_intr)
996 error = bus_setup_intr(sc->vtpci_dev, intr->vti_irq, type,
997 vtpci_vq_intr_filter, vtpci_vq_intr, vqx->vtv_vq,
1009 vtpci_setup_msix_interrupts(struct vtpci_softc *sc, enum intr_type type)
1012 struct vtpci_interrupt *intr;
1015 dev = sc->vtpci_dev;
1016 intr = &sc->vtpci_device_interrupt;
1018 error = bus_setup_intr(dev, intr->vti_irq, type, NULL,
1019 vtpci_config_intr, sc, &intr->vti_handler);
1023 if (sc->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) {
1024 intr = sc->vtpci_msix_vq_interrupts;
1025 error = bus_setup_intr(dev, intr->vti_irq, type,
1026 vtpci_vq_shared_intr_filter, vtpci_vq_shared_intr, sc,
1027 &intr->vti_handler);
1029 error = vtpci_setup_pervq_msix_interrupts(sc, type);
1031 return (error ? error : vtpci_set_host_msix_vectors(sc));
1035 vtpci_setup_interrupts(struct vtpci_softc *sc, enum intr_type type)
1039 type |= INTR_MPSAFE;
1040 KASSERT(sc->vtpci_flags & VTPCI_FLAG_ITYPE_MASK,
1041 ("%s: no interrupt type selected %#x", __func__, sc->vtpci_flags));
1043 error = vtpci_alloc_intr_resources(sc);
1047 if (sc->vtpci_flags & VTPCI_FLAG_LEGACY)
1048 error = vtpci_setup_legacy_interrupt(sc, type);
1049 else if (sc->vtpci_flags & VTPCI_FLAG_MSI)
1050 error = vtpci_setup_msi_interrupt(sc, type);
1052 error = vtpci_setup_msix_interrupts(sc, type);
1058 vtpci_register_msix_vector(struct vtpci_softc *sc, int offset,
1059 struct vtpci_interrupt *intr)
1064 dev = sc->vtpci_dev;
1067 /* Map from guest rid to host vector. */
1068 vector = intr->vti_rid - 1;
1070 vector = VIRTIO_MSI_NO_VECTOR;
1072 vtpci_write_header_2(sc, offset, vector);
1074 /* Read vector to determine if the host had sufficient resources. */
1075 if (vtpci_read_header_2(sc, offset) != vector) {
1077 "insufficient host resources for MSIX interrupts\n");
1085 vtpci_set_host_msix_vectors(struct vtpci_softc *sc)
1087 struct vtpci_interrupt *intr, *tintr;
1088 int idx, offset, error;
1090 intr = &sc->vtpci_device_interrupt;
1091 offset = VIRTIO_MSI_CONFIG_VECTOR;
1093 error = vtpci_register_msix_vector(sc, offset, intr);
1097 intr = sc->vtpci_msix_vq_interrupts;
1098 offset = VIRTIO_MSI_QUEUE_VECTOR;
1100 for (idx = 0; idx < sc->vtpci_nvqs; idx++) {
1101 vtpci_select_virtqueue(sc, idx);
1103 if (sc->vtpci_vqs[idx].vtv_no_intr)
1108 error = vtpci_register_msix_vector(sc, offset, tintr);
1113 * For shared MSIX, all the virtqueues share the first
1116 if (!sc->vtpci_vqs[idx].vtv_no_intr &&
1117 (sc->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) == 0)
1125 vtpci_reinit_virtqueue(struct vtpci_softc *sc, int idx)
1127 struct vtpci_virtqueue *vqx;
1128 struct virtqueue *vq;
1132 vqx = &sc->vtpci_vqs[idx];
1135 KASSERT(vq != NULL, ("%s: vq %d not allocated", __func__, idx));
1137 vtpci_select_virtqueue(sc, idx);
1138 size = vtpci_read_header_2(sc, VIRTIO_PCI_QUEUE_NUM);
1140 error = virtqueue_reinit(vq, size);
1144 vtpci_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN,
1145 virtqueue_paddr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
1151 vtpci_free_interrupt(struct vtpci_softc *sc, struct vtpci_interrupt *intr)
1155 dev = sc->vtpci_dev;
1157 if (intr->vti_handler != NULL) {
1158 bus_teardown_intr(dev, intr->vti_irq, intr->vti_handler);
1159 intr->vti_handler = NULL;
1162 if (intr->vti_irq != NULL) {
1163 bus_release_resource(dev, SYS_RES_IRQ, intr->vti_rid,
1165 intr->vti_irq = NULL;
1171 vtpci_free_interrupts(struct vtpci_softc *sc)
1173 struct vtpci_interrupt *intr;
1176 vtpci_free_interrupt(sc, &sc->vtpci_device_interrupt);
1178 if (sc->vtpci_nmsix_resources != 0) {
1179 nvq_intrs = sc->vtpci_nmsix_resources - 1;
1180 sc->vtpci_nmsix_resources = 0;
1182 intr = sc->vtpci_msix_vq_interrupts;
1184 for (i = 0; i < nvq_intrs; i++, intr++)
1185 vtpci_free_interrupt(sc, intr);
1187 free(sc->vtpci_msix_vq_interrupts, M_DEVBUF);
1188 sc->vtpci_msix_vq_interrupts = NULL;
1192 if (sc->vtpci_flags & (VTPCI_FLAG_MSI | VTPCI_FLAG_MSIX))
1193 pci_release_msi(sc->vtpci_dev);
1195 sc->vtpci_flags &= ~VTPCI_FLAG_ITYPE_MASK;
1199 vtpci_free_virtqueues(struct vtpci_softc *sc)
1201 struct vtpci_virtqueue *vqx;
1204 for (idx = 0; idx < sc->vtpci_nvqs; idx++) {
1205 vqx = &sc->vtpci_vqs[idx];
1207 vtpci_select_virtqueue(sc, idx);
1208 vtpci_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN, 0);
1210 virtqueue_free(vqx->vtv_vq);
1214 free(sc->vtpci_vqs, M_DEVBUF);
1215 sc->vtpci_vqs = NULL;
1220 vtpci_release_child_resources(struct vtpci_softc *sc)
1223 vtpci_free_interrupts(sc);
1224 vtpci_free_virtqueues(sc);
1228 vtpci_cleanup_setup_intr_attempt(struct vtpci_softc *sc)
1232 if (sc->vtpci_flags & VTPCI_FLAG_MSIX) {
1233 vtpci_write_header_2(sc, VIRTIO_MSI_CONFIG_VECTOR,
1234 VIRTIO_MSI_NO_VECTOR);
1236 for (idx = 0; idx < sc->vtpci_nvqs; idx++) {
1237 vtpci_select_virtqueue(sc, idx);
1238 vtpci_write_header_2(sc, VIRTIO_MSI_QUEUE_VECTOR,
1239 VIRTIO_MSI_NO_VECTOR);
1243 vtpci_free_interrupts(sc);
1247 vtpci_reset(struct vtpci_softc *sc)
1251 * Setting the status to RESET sets the host device to
1252 * the original, uninitialized state.
1254 vtpci_set_status(sc->vtpci_dev, VIRTIO_CONFIG_STATUS_RESET);
1258 vtpci_select_virtqueue(struct vtpci_softc *sc, int idx)
1261 vtpci_write_header_2(sc, VIRTIO_PCI_QUEUE_SEL, idx);
1265 vtpci_legacy_intr(void *xsc)
1267 struct vtpci_softc *sc;
1268 struct vtpci_virtqueue *vqx;
1273 vqx = &sc->vtpci_vqs[0];
1275 /* Reading the ISR also clears it. */
1276 isr = vtpci_read_config_1(sc, VIRTIO_PCI_ISR);
1278 if (isr & VIRTIO_PCI_ISR_CONFIG)
1279 vtpci_config_intr(sc);
1281 if (isr & VIRTIO_PCI_ISR_INTR) {
1282 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++) {
1283 if (vqx->vtv_no_intr == 0)
1284 virtqueue_intr(vqx->vtv_vq);
1290 vtpci_vq_shared_intr_filter(void *xsc)
1292 struct vtpci_softc *sc;
1293 struct vtpci_virtqueue *vqx;
1298 vqx = &sc->vtpci_vqs[0];
1300 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++) {
1301 if (vqx->vtv_no_intr == 0)
1302 rc |= virtqueue_intr_filter(vqx->vtv_vq);
1305 return (rc ? FILTER_SCHEDULE_THREAD : FILTER_STRAY);
1309 vtpci_vq_shared_intr(void *xsc)
1311 struct vtpci_softc *sc;
1312 struct vtpci_virtqueue *vqx;
1316 vqx = &sc->vtpci_vqs[0];
1318 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++) {
1319 if (vqx->vtv_no_intr == 0)
1320 virtqueue_intr(vqx->vtv_vq);
1325 vtpci_vq_intr_filter(void *xvq)
1327 struct virtqueue *vq;
1331 rc = virtqueue_intr_filter(vq);
1333 return (rc ? FILTER_SCHEDULE_THREAD : FILTER_STRAY);
1337 vtpci_vq_intr(void *xvq)
1339 struct virtqueue *vq;
1346 vtpci_config_intr(void *xsc)
1348 struct vtpci_softc *sc;
1352 child = sc->vtpci_child_dev;
1355 VIRTIO_CONFIG_CHANGE(child);