2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/virtio/pci/virtio_pci.c,v 1.3 2012/04/14 05:48:04 grehan Exp $
29 /* Driver for the VirtIO PCI interface. */
31 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <bus/pci/pcivar.h>
41 #include <bus/pci/pcireg.h>
44 #include <sys/param.h>
48 #include <virtio/virtio.h>
49 #include <virtio/virtqueue.h>
50 #include "virtio_pci.h"
52 #include "virtio_bus_if.h"
53 #include "virtio_if.h"
57 struct resource *vtpci_res;
58 struct resource *vtpci_msix_res;
59 uint64_t vtpci_features;
63 #define VIRTIO_PCI_FLAG_NO_MSI 0x0001
64 #define VIRTIO_PCI_FLAG_MSI 0x0002
65 #define VIRTIO_PCI_FLAG_NO_MSIX 0x0010
66 #define VIRTIO_PCI_FLAG_MSIX 0x0020
67 #define VIRTIO_PCI_FLAG_SHARED_MSIX 0x0040
69 device_t vtpci_child_dev;
70 struct virtio_feature_desc *vtpci_child_feat_desc;
73 * Ideally, each virtqueue that the driver provides a callback for
74 * will receive its own MSIX vector. If there are not sufficient
75 * vectors available, we will then attempt to have all the VQs
76 * share one vector. Note that when using MSIX, the configuration
77 * changed notifications must be on their own vector.
79 * If MSIX is not available, we will attempt to have the whole
80 * device share one MSI vector, and then, finally, one legacy
84 struct vtpci_virtqueue {
87 /* Index into vtpci_intr_res[] below. Unused, then -1. */
89 } vtpci_vqx[VIRTIO_MAX_VIRTQUEUES];
92 * When using MSIX interrupts, the first element of vtpci_intr_res[]
93 * is always the configuration changed notifications. The remaining
94 * element(s) are used for the virtqueues.
96 * With MSI and legacy interrupts, only the first element of
97 * vtpci_intr_res[] is used.
100 struct vtpci_intr_resource {
101 struct resource *irq;
104 } vtpci_intr_res[1 + VIRTIO_MAX_VIRTQUEUES];
107 static int vtpci_probe(device_t);
108 static int vtpci_attach(device_t);
109 static int vtpci_detach(device_t);
110 static int vtpci_suspend(device_t);
111 static int vtpci_resume(device_t);
112 static int vtpci_shutdown(device_t);
113 static void vtpci_driver_added(device_t, driver_t *);
114 static void vtpci_child_detached(device_t, device_t);
115 static int vtpci_read_ivar(device_t, device_t, int, uintptr_t *);
116 static int vtpci_write_ivar(device_t, device_t, int, uintptr_t);
118 static uint64_t vtpci_negotiate_features(device_t, uint64_t);
119 static int vtpci_with_feature(device_t, uint64_t);
120 static int vtpci_alloc_virtqueues(device_t, int, int,
121 struct vq_alloc_info *);
122 static int vtpci_setup_intr(device_t);
123 static void vtpci_stop(device_t);
124 static int vtpci_reinit(device_t, uint64_t);
125 static void vtpci_reinit_complete(device_t);
126 static void vtpci_notify_virtqueue(device_t, uint16_t);
127 static uint8_t vtpci_get_status(device_t);
128 static void vtpci_set_status(device_t, uint8_t);
129 static void vtpci_read_dev_config(device_t, bus_size_t, void *, int);
130 static void vtpci_write_dev_config(device_t, bus_size_t, void *, int);
132 static void vtpci_describe_features(struct vtpci_softc *, const char *,
134 static void vtpci_probe_and_attach_child(struct vtpci_softc *);
136 static int vtpci_alloc_interrupts(struct vtpci_softc *, int, int,
137 struct vq_alloc_info *);
138 static int vtpci_alloc_intr_resources(struct vtpci_softc *, int,
139 struct vq_alloc_info *);
140 static int vtpci_alloc_msi(struct vtpci_softc *);
141 static int vtpci_alloc_msix(struct vtpci_softc *, int);
142 static int vtpci_register_msix_vector(struct vtpci_softc *, int, int);
144 static void vtpci_free_interrupts(struct vtpci_softc *);
145 static void vtpci_free_virtqueues(struct vtpci_softc *);
146 static void vtpci_release_child_resources(struct vtpci_softc *);
147 static void vtpci_reset(struct vtpci_softc *);
149 static int vtpci_legacy_intr(void *);
150 static int vtpci_vq_shared_intr(void *);
151 static int vtpci_vq_intr(void *);
152 static int vtpci_config_intr(void *);
155 * I/O port read/write wrappers.
157 #define vtpci_read_config_1(sc, o) bus_read_1((sc)->vtpci_res, (o))
158 #define vtpci_read_config_2(sc, o) bus_read_2((sc)->vtpci_res, (o))
159 #define vtpci_read_config_4(sc, o) bus_read_4((sc)->vtpci_res, (o))
160 #define vtpci_write_config_1(sc, o, v) bus_write_1((sc)->vtpci_res, (o), (v))
161 #define vtpci_write_config_2(sc, o, v) bus_write_2((sc)->vtpci_res, (o), (v))
162 #define vtpci_write_config_4(sc, o, v) bus_write_4((sc)->vtpci_res, (o), (v))
165 static int vtpci_disable_msix = 0;
166 TUNABLE_INT("hw.virtio.pci.disable_msix", &vtpci_disable_msix);
168 static device_method_t vtpci_methods[] = {
169 /* Device interface. */
170 DEVMETHOD(device_probe, vtpci_probe),
171 DEVMETHOD(device_attach, vtpci_attach),
172 DEVMETHOD(device_detach, vtpci_detach),
173 DEVMETHOD(device_suspend, vtpci_suspend),
174 DEVMETHOD(device_resume, vtpci_resume),
175 DEVMETHOD(device_shutdown, vtpci_shutdown),
178 DEVMETHOD(bus_driver_added, vtpci_driver_added),
179 DEVMETHOD(bus_child_detached, vtpci_child_detached),
180 DEVMETHOD(bus_read_ivar, vtpci_read_ivar),
181 DEVMETHOD(bus_write_ivar, vtpci_write_ivar),
183 /* VirtIO bus interface. */
184 DEVMETHOD(virtio_bus_negotiate_features, vtpci_negotiate_features),
185 DEVMETHOD(virtio_bus_with_feature, vtpci_with_feature),
186 DEVMETHOD(virtio_bus_alloc_virtqueues, vtpci_alloc_virtqueues),
187 DEVMETHOD(virtio_bus_setup_intr, vtpci_setup_intr),
188 DEVMETHOD(virtio_bus_stop, vtpci_stop),
189 DEVMETHOD(virtio_bus_reinit, vtpci_reinit),
190 DEVMETHOD(virtio_bus_reinit_complete, vtpci_reinit_complete),
191 DEVMETHOD(virtio_bus_notify_vq, vtpci_notify_virtqueue),
192 DEVMETHOD(virtio_bus_read_device_config, vtpci_read_dev_config),
193 DEVMETHOD(virtio_bus_write_device_config, vtpci_write_dev_config),
198 static driver_t vtpci_driver = {
201 sizeof(struct vtpci_softc)
204 devclass_t vtpci_devclass;
206 DRIVER_MODULE(virtio_pci, pci, vtpci_driver, vtpci_devclass, 0, 0);
207 MODULE_VERSION(virtio_pci, 1);
208 MODULE_DEPEND(virtio_pci, pci, 1, 1, 1);
209 MODULE_DEPEND(virtio_pci, virtio, 1, 1, 1);
212 vtpci_probe(device_t dev)
217 if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
220 if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
221 pci_get_device(dev) > VIRTIO_PCI_DEVICEID_MAX)
224 if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
227 name = virtio_device_name(pci_get_subdevice(dev));
231 ksnprintf(desc, sizeof(desc), "VirtIO PCI %s adapter", name);
232 device_set_desc_copy(dev, desc);
234 return (BUS_PROBE_DEFAULT);
238 vtpci_attach(device_t dev)
240 struct vtpci_softc *sc;
244 sc = device_get_softc(dev);
247 pci_enable_busmaster(dev);
250 sc->vtpci_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
252 if (sc->vtpci_res == NULL) {
253 device_printf(dev, "cannot map I/O space\n");
257 if (pci_find_extcap(dev, PCIY_MSI, NULL) != 0)
258 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSI;
259 /* XXX(vsrinivas): Check out how to get MSI-X */
261 if (pci_find_extcap(dev, PCIY_MSIX, NULL) == 0) {
263 sc->vtpci_msix_res = bus_alloc_resource_any(dev,
264 SYS_RES_MEMORY, &rid, RF_ACTIVE);
267 if (sc->vtpci_msix_res == NULL)
268 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSIX;
272 /* Tell the host we've noticed this device. */
273 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
275 if ((child = device_add_child(dev, NULL, -1)) == NULL) {
276 device_printf(dev, "cannot create child device\n");
277 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
282 sc->vtpci_child_dev = child;
283 vtpci_probe_and_attach_child(sc);
289 vtpci_detach(device_t dev)
291 struct vtpci_softc *sc;
295 sc = device_get_softc(dev);
297 if ((child = sc->vtpci_child_dev) != NULL) {
298 error = device_delete_child(dev, child);
301 sc->vtpci_child_dev = NULL;
306 if (sc->vtpci_msix_res != NULL) {
307 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(1),
309 sc->vtpci_msix_res = NULL;
312 if (sc->vtpci_res != NULL) {
313 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0),
315 sc->vtpci_res = NULL;
322 vtpci_suspend(device_t dev)
325 return (bus_generic_suspend(dev));
329 vtpci_resume(device_t dev)
332 return (bus_generic_resume(dev));
336 vtpci_shutdown(device_t dev)
339 (void) bus_generic_shutdown(dev);
340 /* Forcibly stop the host device. */
347 vtpci_driver_added(device_t dev, driver_t *driver)
349 struct vtpci_softc *sc;
351 sc = device_get_softc(dev);
353 vtpci_probe_and_attach_child(sc);
357 vtpci_child_detached(device_t dev, device_t child)
359 struct vtpci_softc *sc;
361 sc = device_get_softc(dev);
364 vtpci_release_child_resources(sc);
368 vtpci_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
370 struct vtpci_softc *sc;
372 sc = device_get_softc(dev);
374 if (sc->vtpci_child_dev != child)
378 case VIRTIO_IVAR_DEVTYPE:
379 *result = pci_get_subdevice(dev);
389 vtpci_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
391 struct vtpci_softc *sc;
393 sc = device_get_softc(dev);
395 if (sc->vtpci_child_dev != child)
399 case VIRTIO_IVAR_FEATURE_DESC:
400 sc->vtpci_child_feat_desc = (void *) value;
410 vtpci_negotiate_features(device_t dev, uint64_t child_features)
412 struct vtpci_softc *sc;
413 uint64_t host_features, features;
415 sc = device_get_softc(dev);
417 host_features = vtpci_read_config_4(sc, VIRTIO_PCI_HOST_FEATURES);
418 vtpci_describe_features(sc, "host", host_features);
421 * Limit negotiated features to what the driver, virtqueue, and
424 features = host_features & child_features;
425 features = virtqueue_filter_features(features);
426 sc->vtpci_features = features;
428 vtpci_describe_features(sc, "negotiated", features);
429 vtpci_write_config_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
435 vtpci_with_feature(device_t dev, uint64_t feature)
437 struct vtpci_softc *sc;
439 sc = device_get_softc(dev);
441 return ((sc->vtpci_features & feature) != 0);
445 vtpci_alloc_virtqueues(device_t dev, int flags, int nvqs,
446 struct vq_alloc_info *vq_info)
448 struct vtpci_softc *sc;
449 struct vtpci_virtqueue *vqx;
450 struct vq_alloc_info *info;
454 sc = device_get_softc(dev);
456 if (sc->vtpci_nvqs != 0 || nvqs <= 0 ||
457 nvqs > VIRTIO_MAX_VIRTQUEUES)
460 error = vtpci_alloc_interrupts(sc, flags, nvqs, vq_info);
462 device_printf(dev, "cannot allocate interrupts\n");
466 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
467 error = vtpci_register_msix_vector(sc,
468 VIRTIO_MSI_CONFIG_VECTOR, 0);
473 for (queue = 0; queue < nvqs; queue++) {
474 vqx = &sc->vtpci_vqx[queue];
475 info = &vq_info[queue];
477 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
479 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
480 error = virtqueue_alloc(dev, queue, vq_size,
481 VIRTIO_PCI_VRING_ALIGN, 0xFFFFFFFFUL, info, &vqx->vq);
485 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
486 error = vtpci_register_msix_vector(sc,
487 VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
492 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
493 virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
495 *info->vqai_vq = vqx->vq;
503 vtpci_setup_intr(device_t dev)
505 struct vtpci_softc *sc;
506 struct vtpci_intr_resource *ires;
507 struct vtpci_virtqueue *vqx;
510 sc = device_get_softc(dev);
512 ires = &sc->vtpci_intr_res[0];
514 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) == 0) {
515 error = bus_setup_intr(dev, ires->irq, flags,
516 (driver_intr_t *) vtpci_legacy_intr, sc, &ires->intrhand, NULL);
521 error = bus_setup_intr(dev, ires->irq, flags,(driver_intr_t *) vtpci_config_intr,
522 sc, &ires->intrhand, NULL);
526 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX) {
527 ires = &sc->vtpci_intr_res[1];
528 error = bus_setup_intr(dev, ires->irq, flags,
529 (driver_intr_t *) vtpci_vq_shared_intr, sc, &ires->intrhand, NULL);
534 /* Setup an interrupt handler for each virtqueue. */
535 for (i = 0; i < sc->vtpci_nvqs; i++) {
536 vqx = &sc->vtpci_vqx[i];
537 if (vqx->ires_idx < 1)
540 ires = &sc->vtpci_intr_res[vqx->ires_idx];
541 error = bus_setup_intr(dev, ires->irq, flags,
542 (driver_intr_t *) vtpci_vq_intr, vqx->vq, &ires->intrhand, NULL);
551 vtpci_stop(device_t dev)
554 vtpci_reset(device_get_softc(dev));
558 vtpci_reinit(device_t dev, uint64_t features)
560 struct vtpci_softc *sc;
561 struct vtpci_virtqueue *vqx;
562 struct virtqueue *vq;
566 sc = device_get_softc(dev);
569 * Redrive the device initialization. This is a bit of an abuse
570 * of the specification, but both VirtualBox and QEMU/KVM seem
571 * to play nice. We do not allow the host device to change from
572 * what was originally negotiated beyond what the guest driver
573 * changed (MSIX state should not change, number of virtqueues
574 * and their size remain the same, etc).
577 if (vtpci_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
581 * Quickly drive the status through ACK and DRIVER. The device
582 * does not become usable again until vtpci_reinit_complete().
584 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
585 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
587 vtpci_negotiate_features(dev, features);
589 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
590 error = vtpci_register_msix_vector(sc,
591 VIRTIO_MSI_CONFIG_VECTOR, 0);
596 for (queue = 0; queue < sc->vtpci_nvqs; queue++) {
597 vqx = &sc->vtpci_vqx[queue];
600 KASSERT(vq != NULL, ("vq %d not allocated", queue));
601 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
603 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
604 error = virtqueue_reinit(vq, vq_size);
608 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
609 error = vtpci_register_msix_vector(sc,
610 VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
615 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
616 virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
623 vtpci_reinit_complete(device_t dev)
626 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
630 vtpci_notify_virtqueue(device_t dev, uint16_t queue)
632 struct vtpci_softc *sc;
634 sc = device_get_softc(dev);
636 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_NOTIFY, queue);
640 vtpci_get_status(device_t dev)
642 struct vtpci_softc *sc;
644 sc = device_get_softc(dev);
646 return (vtpci_read_config_1(sc, VIRTIO_PCI_STATUS));
650 vtpci_set_status(device_t dev, uint8_t status)
652 struct vtpci_softc *sc;
654 sc = device_get_softc(dev);
656 if (status != VIRTIO_CONFIG_STATUS_RESET)
657 status |= vtpci_get_status(dev);
659 vtpci_write_config_1(sc, VIRTIO_PCI_STATUS, status);
663 vtpci_read_dev_config(device_t dev, bus_size_t offset,
664 void *dst, int length)
666 struct vtpci_softc *sc;
671 sc = device_get_softc(dev);
672 off = VIRTIO_PCI_CONFIG(sc) + offset;
674 for (d = dst; length > 0; d += size, off += size, length -= size) {
677 *(uint32_t *)d = vtpci_read_config_4(sc, off);
678 } else if (length >= 2) {
680 *(uint16_t *)d = vtpci_read_config_2(sc, off);
683 *d = vtpci_read_config_1(sc, off);
689 vtpci_write_dev_config(device_t dev, bus_size_t offset,
690 void *src, int length)
692 struct vtpci_softc *sc;
697 sc = device_get_softc(dev);
698 off = VIRTIO_PCI_CONFIG(sc) + offset;
700 for (s = src; length > 0; s += size, off += size, length -= size) {
703 vtpci_write_config_4(sc, off, *(uint32_t *)s);
704 } else if (length >= 2) {
706 vtpci_write_config_2(sc, off, *(uint16_t *)s);
709 vtpci_write_config_1(sc, off, *s);
715 vtpci_describe_features(struct vtpci_softc *sc, const char *msg,
721 child = sc->vtpci_child_dev;
723 if (device_is_attached(child) && bootverbose == 0)
726 virtio_describe(dev, msg, features, sc->vtpci_child_feat_desc);
730 vtpci_probe_and_attach_child(struct vtpci_softc *sc)
735 child = sc->vtpci_child_dev;
740 if (device_get_state(child) != DS_NOTPRESENT)
743 if (device_probe_child(dev, child) != 0)
746 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
747 if (DEVICE_ATTACH(child) != 0) {
748 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
750 vtpci_release_child_resources(sc);
752 /* Reset status for future attempt. */
753 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
755 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
759 vtpci_alloc_interrupts(struct vtpci_softc *sc, int flags, int nvqs,
760 struct vq_alloc_info *vq_info)
762 int i, nvectors, error;
765 * Only allocate a vector for virtqueues that are actually
766 * expecting an interrupt.
768 for (nvectors = 0, i = 0; i < nvqs; i++)
769 if (vq_info[i].vqai_intr != NULL)
772 if (vtpci_disable_msix != 0 ||
773 sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSIX ||
774 flags & VIRTIO_ALLOC_VQS_DISABLE_MSIX ||
775 vtpci_alloc_msix(sc, nvectors) != 0) {
777 * Use MSI interrupts if available. Otherwise, we fallback
778 * to legacy interrupts.
780 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSI) == 0 &&
781 vtpci_alloc_msi(sc) == 0)
782 sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSI;
784 sc->vtpci_nintr_res = 1;
787 error = vtpci_alloc_intr_resources(sc, nvqs, vq_info);
793 vtpci_alloc_intr_resources(struct vtpci_softc *sc, int nvqs,
794 struct vq_alloc_info *vq_info)
797 struct resource *irq;
798 struct vtpci_virtqueue *vqx;
799 int i, rid, flags, res_idx;
804 if ((sc->vtpci_flags &
805 (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) == 0) {
807 flags |= RF_SHAREABLE;
811 for (i = 0; i < sc->vtpci_nintr_res; i++) {
812 irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, flags);
816 sc->vtpci_intr_res[i].irq = irq;
817 sc->vtpci_intr_res[i].rid = rid++;
821 * Map the virtqueue into the correct index in vq_intr_res[]. Note the
822 * first index is reserved for configuration changes notifications.
824 for (i = 0, res_idx = 1; i < nvqs; i++) {
825 vqx = &sc->vtpci_vqx[i];
827 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
828 if (vq_info[i].vqai_intr == NULL)
830 else if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
831 vqx->ires_idx = res_idx;
833 vqx->ires_idx = res_idx++;
842 vtpci_alloc_msi(struct vtpci_softc *sc)
849 nmsi = pci_msi_count(dev);
854 sc->vtpci_irq_rid = 0;
855 sc->vtpci_irq_type = pci_alloc_1intr(dev, 1,
856 &sc->vtpci_irq_rid, &irq_flags);
863 vtpci_alloc_msix(struct vtpci_softc *sc, int nvectors)
865 /* XXX(vsrinivas): Huh? Is this how MSI-X works?*/
866 /* XXX(vsrinivas): All of this was disabled... */
869 int nmsix, cnt, required;
873 nmsix = pci_msix_count(dev);
877 /* An additional vector is needed for the config changes. */
878 required = nvectors + 1;
879 if (nmsix >= required) {
881 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required)
884 pci_release_msi(dev);
887 /* Attempt shared MSIX configuration. */
889 if (nmsix >= required) {
891 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
892 sc->vtpci_flags |= VIRTIO_PCI_FLAG_SHARED_MSIX;
896 pci_release_msi(dev);
902 sc->vtpci_nintr_res = required;
903 sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSIX;
906 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
907 device_printf(dev, "using shared virtqueue MSIX\n");
909 device_printf(dev, "using per virtqueue MSIX\n");
916 vtpci_register_msix_vector(struct vtpci_softc *sc, int offset, int res_idx)
923 if (offset != VIRTIO_MSI_CONFIG_VECTOR &&
924 offset != VIRTIO_MSI_QUEUE_VECTOR)
928 /* Map from rid to host vector. */
929 vector = sc->vtpci_intr_res[res_idx].rid - 1;
931 vector = VIRTIO_MSI_NO_VECTOR;
933 /* The first resource is special; make sure it is used correctly. */
935 KASSERT(vector == 0, ("unexpected config vector"));
936 KASSERT(offset == VIRTIO_MSI_CONFIG_VECTOR,
937 ("unexpected config offset"));
940 vtpci_write_config_2(sc, offset, vector);
942 if (vtpci_read_config_2(sc, offset) != vector) {
943 device_printf(dev, "insufficient host resources for "
944 "MSIX interrupts\n");
952 vtpci_free_interrupts(struct vtpci_softc *sc)
955 struct vtpci_intr_resource *ires;
959 sc->vtpci_nintr_res = 0;
961 if (sc->vtpci_flags & (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) {
962 pci_release_msi(dev);
963 sc->vtpci_flags &= ~(VIRTIO_PCI_FLAG_MSI |
964 VIRTIO_PCI_FLAG_MSIX | VIRTIO_PCI_FLAG_SHARED_MSIX);
967 for (i = 0; i < 1 + VIRTIO_MAX_VIRTQUEUES; i++) {
968 ires = &sc->vtpci_intr_res[i];
970 if (ires->intrhand != NULL) {
971 bus_teardown_intr(dev, ires->irq, ires->intrhand);
972 ires->intrhand = NULL;
975 if (ires->irq != NULL) {
976 bus_release_resource(dev, SYS_RES_IRQ, ires->rid,
986 vtpci_free_virtqueues(struct vtpci_softc *sc)
988 struct vtpci_virtqueue *vqx;
993 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) {
994 vqx = &sc->vtpci_vqx[i];
996 if (vqx->vq != NULL) {
997 virtqueue_free(vqx->vq);
1004 vtpci_release_child_resources(struct vtpci_softc *sc)
1007 vtpci_free_interrupts(sc);
1008 vtpci_free_virtqueues(sc);
1012 vtpci_reset(struct vtpci_softc *sc)
1016 * Setting the status to RESET sets the host device to
1017 * the original, uninitialized state.
1019 vtpci_set_status(sc->vtpci_dev, VIRTIO_CONFIG_STATUS_RESET);
1023 vtpci_legacy_intr(void *xsc)
1025 struct vtpci_softc *sc;
1026 struct vtpci_virtqueue *vqx;
1031 vqx = &sc->vtpci_vqx[0];
1033 /* Reading the ISR also clears it. */
1034 isr = vtpci_read_config_1(sc, VIRTIO_PCI_ISR);
1036 if (isr & VIRTIO_PCI_ISR_CONFIG)
1037 vtpci_config_intr(sc);
1039 if (isr & VIRTIO_PCI_ISR_INTR)
1040 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1041 virtqueue_intr(vqx->vq);
1047 vtpci_vq_shared_intr(void *xsc)
1049 struct vtpci_softc *sc;
1050 struct vtpci_virtqueue *vqx;
1055 vqx = &sc->vtpci_vqx[0];
1057 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1058 rc |= virtqueue_intr(vqx->vq);
1064 vtpci_vq_intr(void *xvq)
1066 struct virtqueue *vq;
1070 rc = virtqueue_intr(vq);
1076 vtpci_config_intr(void *xsc)
1078 struct vtpci_softc *sc;
1084 child = sc->vtpci_child_dev;
1087 rc = VIRTIO_CONFIG_CHANGE(child);