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/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
37 #include <sys/serialize.h>
39 #include <bus/pci/pcivar.h>
40 #include <bus/pci/pcireg.h>
44 #include <dev/virtual/virtio/virtio/virtio.h>
45 #include <dev/virtual/virtio/virtio/virtqueue.h>
46 #include "virtio_pci.h"
47 #include "virtio_if.h"
48 #include "virtio_bus_if.h"
52 struct resource *vtpci_res;
53 struct resource *vtpci_msix_res;
54 uint64_t vtpci_features;
58 #define VIRTIO_PCI_FLAG_NO_MSI 0x0001
59 #define VIRTIO_PCI_FLAG_MSI 0x0002
60 #define VIRTIO_PCI_FLAG_NO_MSIX 0x0010
61 #define VIRTIO_PCI_FLAG_MSIX 0x0020
62 #define VIRTIO_PCI_FLAG_SHARED_MSIX 0x0040
64 device_t vtpci_child_dev;
65 struct virtio_feature_desc *vtpci_child_feat_desc;
68 * Ideally, each virtqueue that the driver provides a callback for
69 * will receive its own MSIX vector. If there are not sufficient
70 * vectors available, we will then attempt to have all the VQs
71 * share one vector. Note that when using MSIX, the configuration
72 * changed notifications must be on their own vector.
74 * If MSIX is not available, we will attempt to have the whole
75 * device share one MSI vector, and then, finally, one legacy
79 struct vtpci_virtqueue {
82 /* Index into vtpci_intr_res[] below. Unused, then -1. */
84 } vtpci_vqx[VIRTIO_MAX_VIRTQUEUES];
87 * When using MSIX interrupts, the first element of vtpci_intr_res[]
88 * is always the configuration changed notifications. The remaining
89 * element(s) are used for the virtqueues.
91 * With MSI and legacy interrupts, only the first element of
92 * vtpci_intr_res[] is used.
95 struct vtpci_intr_resource {
99 } vtpci_intr_res[1 + VIRTIO_MAX_VIRTQUEUES];
102 static int vtpci_probe(device_t);
103 static int vtpci_attach(device_t);
104 static int vtpci_detach(device_t);
105 static int vtpci_suspend(device_t);
106 static int vtpci_resume(device_t);
107 static int vtpci_shutdown(device_t);
108 static void vtpci_driver_added(device_t, driver_t *);
109 static void vtpci_child_detached(device_t, device_t);
110 static int vtpci_read_ivar(device_t, device_t, int, uintptr_t *);
111 static int vtpci_write_ivar(device_t, device_t, int, uintptr_t);
113 static uint64_t vtpci_negotiate_features(device_t, uint64_t);
114 static int vtpci_with_feature(device_t, uint64_t);
115 static int vtpci_alloc_virtqueues(device_t, int, int,
116 struct vq_alloc_info *);
117 static int vtpci_setup_intr(device_t, lwkt_serialize_t);
118 static void vtpci_stop(device_t);
119 static int vtpci_reinit(device_t, uint64_t);
120 static void vtpci_reinit_complete(device_t);
121 static void vtpci_notify_virtqueue(device_t, uint16_t);
122 static uint8_t vtpci_get_status(device_t);
123 static void vtpci_set_status(device_t, uint8_t);
124 static void vtpci_read_dev_config(device_t, bus_size_t, void *, int);
125 static void vtpci_write_dev_config(device_t, bus_size_t, void *, int);
127 static void vtpci_describe_features(struct vtpci_softc *, const char *,
129 static void vtpci_probe_and_attach_child(struct vtpci_softc *);
131 static int vtpci_alloc_interrupts(struct vtpci_softc *, int, int,
132 struct vq_alloc_info *);
133 static int vtpci_alloc_intr_resources(struct vtpci_softc *, int,
134 struct vq_alloc_info *);
135 static int vtpci_alloc_msi(struct vtpci_softc *);
136 static int vtpci_alloc_msix(struct vtpci_softc *, int);
137 static int vtpci_register_msix_vector(struct vtpci_softc *, int, int);
139 static void vtpci_free_interrupts(struct vtpci_softc *);
140 static void vtpci_free_virtqueues(struct vtpci_softc *);
141 static void vtpci_release_child_resources(struct vtpci_softc *);
142 static void vtpci_reset(struct vtpci_softc *);
144 static int vtpci_legacy_intr(void *);
145 static int vtpci_vq_shared_intr(void *);
146 static int vtpci_vq_intr(void *);
147 static int vtpci_config_intr(void *);
150 * I/O port read/write wrappers.
152 #define vtpci_read_config_1(sc, o) bus_read_1((sc)->vtpci_res, (o))
153 #define vtpci_read_config_2(sc, o) bus_read_2((sc)->vtpci_res, (o))
154 #define vtpci_read_config_4(sc, o) bus_read_4((sc)->vtpci_res, (o))
155 #define vtpci_write_config_1(sc, o, v) bus_write_1((sc)->vtpci_res, (o), (v))
156 #define vtpci_write_config_2(sc, o, v) bus_write_2((sc)->vtpci_res, (o), (v))
157 #define vtpci_write_config_4(sc, o, v) bus_write_4((sc)->vtpci_res, (o), (v))
160 static int vtpci_disable_msix = 0;
161 TUNABLE_INT("hw.virtio.pci.disable_msix", &vtpci_disable_msix);
163 static device_method_t vtpci_methods[] = {
164 /* Device interface. */
165 DEVMETHOD(device_probe, vtpci_probe),
166 DEVMETHOD(device_attach, vtpci_attach),
167 DEVMETHOD(device_detach, vtpci_detach),
168 DEVMETHOD(device_suspend, vtpci_suspend),
169 DEVMETHOD(device_resume, vtpci_resume),
170 DEVMETHOD(device_shutdown, vtpci_shutdown),
173 DEVMETHOD(bus_driver_added, vtpci_driver_added),
174 DEVMETHOD(bus_child_detached, vtpci_child_detached),
175 DEVMETHOD(bus_read_ivar, vtpci_read_ivar),
176 DEVMETHOD(bus_write_ivar, vtpci_write_ivar),
178 /* VirtIO bus interface. */
179 DEVMETHOD(virtio_bus_negotiate_features, vtpci_negotiate_features),
180 DEVMETHOD(virtio_bus_with_feature, vtpci_with_feature),
181 DEVMETHOD(virtio_bus_alloc_virtqueues, vtpci_alloc_virtqueues),
182 DEVMETHOD(virtio_bus_setup_intr, vtpci_setup_intr),
183 DEVMETHOD(virtio_bus_stop, vtpci_stop),
184 DEVMETHOD(virtio_bus_reinit, vtpci_reinit),
185 DEVMETHOD(virtio_bus_reinit_complete, vtpci_reinit_complete),
186 DEVMETHOD(virtio_bus_notify_vq, vtpci_notify_virtqueue),
187 DEVMETHOD(virtio_bus_read_device_config, vtpci_read_dev_config),
188 DEVMETHOD(virtio_bus_write_device_config, vtpci_write_dev_config),
193 static driver_t vtpci_driver = {
196 sizeof(struct vtpci_softc)
199 devclass_t vtpci_devclass;
201 DRIVER_MODULE(virtio_pci, pci, vtpci_driver, vtpci_devclass, NULL, NULL);
202 MODULE_VERSION(virtio_pci, 1);
203 MODULE_DEPEND(virtio_pci, pci, 1, 1, 1);
204 MODULE_DEPEND(virtio_pci, virtio, 1, 1, 1);
207 vtpci_probe(device_t dev)
212 if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
215 if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
216 pci_get_device(dev) > VIRTIO_PCI_DEVICEID_MAX)
219 if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
222 name = virtio_device_name(pci_get_subdevice(dev));
226 ksnprintf(desc, sizeof(desc), "VirtIO PCI %s adapter", name);
227 device_set_desc_copy(dev, desc);
229 return (BUS_PROBE_DEFAULT);
233 vtpci_attach(device_t dev)
235 struct vtpci_softc *sc;
239 sc = device_get_softc(dev);
242 pci_enable_busmaster(dev);
245 sc->vtpci_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
247 if (sc->vtpci_res == NULL) {
248 device_printf(dev, "cannot map I/O space\n");
252 if (pci_find_extcap(dev, PCIY_MSI, NULL) != 0)
253 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSI;
254 /* XXX(vsrinivas): Check out how to get MSI-X */
256 if (pci_find_extcap(dev, PCIY_MSIX, NULL) == 0) {
258 sc->vtpci_msix_res = bus_alloc_resource_any(dev,
259 SYS_RES_MEMORY, &rid, RF_ACTIVE);
262 if (sc->vtpci_msix_res == NULL)
263 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSIX;
267 /* Tell the host we've noticed this device. */
268 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
270 if ((child = device_add_child(dev, NULL, -1)) == NULL) {
271 device_printf(dev, "cannot create child device\n");
272 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
277 sc->vtpci_child_dev = child;
278 vtpci_probe_and_attach_child(sc);
284 vtpci_detach(device_t dev)
286 struct vtpci_softc *sc;
290 sc = device_get_softc(dev);
292 if ((child = sc->vtpci_child_dev) != NULL) {
293 error = device_delete_child(dev, child);
296 sc->vtpci_child_dev = NULL;
301 if (sc->vtpci_msix_res != NULL) {
302 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(1),
304 sc->vtpci_msix_res = NULL;
307 if (sc->vtpci_res != NULL) {
308 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0),
310 sc->vtpci_res = NULL;
317 vtpci_suspend(device_t dev)
320 return (bus_generic_suspend(dev));
324 vtpci_resume(device_t dev)
327 return (bus_generic_resume(dev));
331 vtpci_shutdown(device_t dev)
334 (void) bus_generic_shutdown(dev);
335 /* Forcibly stop the host device. */
342 vtpci_driver_added(device_t dev, driver_t *driver)
344 struct vtpci_softc *sc;
346 sc = device_get_softc(dev);
348 vtpci_probe_and_attach_child(sc);
352 vtpci_child_detached(device_t dev, device_t child)
354 struct vtpci_softc *sc;
356 sc = device_get_softc(dev);
359 vtpci_release_child_resources(sc);
363 vtpci_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
365 struct vtpci_softc *sc;
367 sc = device_get_softc(dev);
369 if (sc->vtpci_child_dev != child)
373 case VIRTIO_IVAR_DEVTYPE:
374 *result = pci_get_subdevice(dev);
384 vtpci_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
386 struct vtpci_softc *sc;
388 sc = device_get_softc(dev);
390 if (sc->vtpci_child_dev != child)
394 case VIRTIO_IVAR_FEATURE_DESC:
395 sc->vtpci_child_feat_desc = (void *) value;
405 vtpci_negotiate_features(device_t dev, uint64_t child_features)
407 struct vtpci_softc *sc;
408 uint64_t host_features, features;
410 sc = device_get_softc(dev);
412 host_features = vtpci_read_config_4(sc, VIRTIO_PCI_HOST_FEATURES);
413 vtpci_describe_features(sc, "host", host_features);
416 * Limit negotiated features to what the driver, virtqueue, and
419 features = host_features & child_features;
420 features = virtqueue_filter_features(features);
421 sc->vtpci_features = features;
423 vtpci_describe_features(sc, "negotiated", features);
424 vtpci_write_config_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
430 vtpci_with_feature(device_t dev, uint64_t feature)
432 struct vtpci_softc *sc;
434 sc = device_get_softc(dev);
436 return ((sc->vtpci_features & feature) != 0);
440 vtpci_alloc_virtqueues(device_t dev, int flags, int nvqs,
441 struct vq_alloc_info *vq_info)
443 struct vtpci_softc *sc;
444 struct vtpci_virtqueue *vqx;
445 struct vq_alloc_info *info;
449 sc = device_get_softc(dev);
451 if (sc->vtpci_nvqs != 0 || nvqs <= 0 ||
452 nvqs > VIRTIO_MAX_VIRTQUEUES)
455 error = vtpci_alloc_interrupts(sc, flags, nvqs, vq_info);
457 device_printf(dev, "cannot allocate interrupts\n");
461 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
462 error = vtpci_register_msix_vector(sc,
463 VIRTIO_MSI_CONFIG_VECTOR, 0);
468 for (queue = 0; queue < nvqs; queue++) {
469 vqx = &sc->vtpci_vqx[queue];
470 info = &vq_info[queue];
472 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
474 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
475 error = virtqueue_alloc(dev, queue, vq_size,
476 VIRTIO_PCI_VRING_ALIGN, 0xFFFFFFFFUL, info, &vqx->vq);
480 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
481 error = vtpci_register_msix_vector(sc,
482 VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
487 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
488 virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
490 *info->vqai_vq = vqx->vq;
498 vtpci_setup_intr(device_t dev, lwkt_serialize_t slz)
500 struct vtpci_softc *sc;
501 struct vtpci_intr_resource *ires;
502 struct vtpci_virtqueue *vqx;
505 sc = device_get_softc(dev);
507 ires = &sc->vtpci_intr_res[0];
509 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) == 0) {
510 error = bus_setup_intr(dev, ires->irq, flags,
511 (driver_intr_t *) vtpci_legacy_intr,
512 sc, &ires->intrhand, slz);
516 error = bus_setup_intr(dev, ires->irq, flags,
517 (driver_intr_t *) vtpci_config_intr,
518 sc, &ires->intrhand, slz);
522 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX) {
523 ires = &sc->vtpci_intr_res[1];
524 error = bus_setup_intr(dev, ires->irq, flags,
525 (driver_intr_t *) vtpci_vq_shared_intr,
526 sc, &ires->intrhand, slz);
531 /* Setup an interrupt handler for each virtqueue. */
532 for (i = 0; i < sc->vtpci_nvqs; i++) {
533 vqx = &sc->vtpci_vqx[i];
534 if (vqx->ires_idx < 1)
537 ires = &sc->vtpci_intr_res[vqx->ires_idx];
538 error = bus_setup_intr(dev, ires->irq, flags,
539 (driver_intr_t *) vtpci_vq_intr,
540 vqx->vq, &ires->intrhand, slz);
549 vtpci_stop(device_t dev)
551 vtpci_reset(device_get_softc(dev));
555 vtpci_reinit(device_t dev, uint64_t features)
557 struct vtpci_softc *sc;
558 struct vtpci_virtqueue *vqx;
559 struct virtqueue *vq;
563 sc = device_get_softc(dev);
566 * Redrive the device initialization. This is a bit of an abuse
567 * of the specification, but both VirtualBox and QEMU/KVM seem
568 * to play nice. We do not allow the host device to change from
569 * what was originally negotiated beyond what the guest driver
570 * changed (MSIX state should not change, number of virtqueues
571 * and their size remain the same, etc).
574 if (vtpci_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
578 * Quickly drive the status through ACK and DRIVER. The device
579 * does not become usable again until vtpci_reinit_complete().
581 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
582 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
584 vtpci_negotiate_features(dev, features);
586 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
587 error = vtpci_register_msix_vector(sc,
588 VIRTIO_MSI_CONFIG_VECTOR, 0);
593 for (queue = 0; queue < sc->vtpci_nvqs; queue++) {
594 vqx = &sc->vtpci_vqx[queue];
597 KASSERT(vq != NULL, ("vq %d not allocated", queue));
598 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
600 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
601 error = virtqueue_reinit(vq, vq_size);
605 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
606 error = vtpci_register_msix_vector(sc,
607 VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
612 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
613 virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
620 vtpci_reinit_complete(device_t dev)
623 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
627 vtpci_notify_virtqueue(device_t dev, uint16_t queue)
629 struct vtpci_softc *sc;
631 sc = device_get_softc(dev);
633 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_NOTIFY, queue);
637 vtpci_get_status(device_t dev)
639 struct vtpci_softc *sc;
641 sc = device_get_softc(dev);
643 return (vtpci_read_config_1(sc, VIRTIO_PCI_STATUS));
647 vtpci_set_status(device_t dev, uint8_t status)
649 struct vtpci_softc *sc;
651 sc = device_get_softc(dev);
653 if (status != VIRTIO_CONFIG_STATUS_RESET)
654 status |= vtpci_get_status(dev);
656 vtpci_write_config_1(sc, VIRTIO_PCI_STATUS, status);
660 vtpci_read_dev_config(device_t dev, bus_size_t offset,
661 void *dst, int length)
663 struct vtpci_softc *sc;
668 sc = device_get_softc(dev);
669 off = VIRTIO_PCI_CONFIG(sc) + offset;
671 for (d = dst; length > 0; d += size, off += size, length -= size) {
674 *(uint32_t *)d = vtpci_read_config_4(sc, off);
675 } else if (length >= 2) {
677 *(uint16_t *)d = vtpci_read_config_2(sc, off);
680 *d = vtpci_read_config_1(sc, off);
686 vtpci_write_dev_config(device_t dev, bus_size_t offset,
687 void *src, int length)
689 struct vtpci_softc *sc;
694 sc = device_get_softc(dev);
695 off = VIRTIO_PCI_CONFIG(sc) + offset;
697 for (s = src; length > 0; s += size, off += size, length -= size) {
700 vtpci_write_config_4(sc, off, *(uint32_t *)s);
701 } else if (length >= 2) {
703 vtpci_write_config_2(sc, off, *(uint16_t *)s);
706 vtpci_write_config_1(sc, off, *s);
712 vtpci_describe_features(struct vtpci_softc *sc, const char *msg,
718 child = sc->vtpci_child_dev;
720 if (device_is_attached(child) && bootverbose == 0)
723 virtio_describe(dev, msg, features, sc->vtpci_child_feat_desc);
727 vtpci_probe_and_attach_child(struct vtpci_softc *sc)
732 child = sc->vtpci_child_dev;
737 if (device_get_state(child) != DS_NOTPRESENT)
740 if (device_probe_child(dev, child) != 0)
743 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
744 if (DEVICE_ATTACH(child) != 0) {
745 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
747 vtpci_release_child_resources(sc);
749 /* Reset status for future attempt. */
750 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
752 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
756 vtpci_alloc_interrupts(struct vtpci_softc *sc, int flags, int nvqs,
757 struct vq_alloc_info *vq_info)
759 int i, nvectors, error;
762 * Only allocate a vector for virtqueues that are actually
763 * expecting an interrupt.
765 for (nvectors = 0, i = 0; i < nvqs; i++)
766 if (vq_info[i].vqai_intr != NULL)
769 if (vtpci_disable_msix != 0 ||
770 sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSIX ||
771 flags & VIRTIO_ALLOC_VQS_DISABLE_MSIX ||
772 vtpci_alloc_msix(sc, nvectors) != 0) {
774 * Use MSI interrupts if available. Otherwise, we fallback
775 * to legacy interrupts.
777 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSI) == 0 &&
778 vtpci_alloc_msi(sc) == 0)
779 sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSI;
781 sc->vtpci_nintr_res = 1;
784 error = vtpci_alloc_intr_resources(sc, nvqs, vq_info);
790 vtpci_alloc_intr_resources(struct vtpci_softc *sc, int nvqs,
791 struct vq_alloc_info *vq_info)
794 struct resource *irq;
795 struct vtpci_virtqueue *vqx;
796 int i, rid, flags, res_idx;
801 if ((sc->vtpci_flags &
802 (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) == 0) {
804 flags |= RF_SHAREABLE;
808 for (i = 0; i < sc->vtpci_nintr_res; i++) {
809 irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, flags);
813 sc->vtpci_intr_res[i].irq = irq;
814 sc->vtpci_intr_res[i].rid = rid++;
818 * Map the virtqueue into the correct index in vq_intr_res[]. Note the
819 * first index is reserved for configuration changes notifications.
821 for (i = 0, res_idx = 1; i < nvqs; i++) {
822 vqx = &sc->vtpci_vqx[i];
824 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
825 if (vq_info[i].vqai_intr == NULL)
827 else if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
828 vqx->ires_idx = res_idx;
830 vqx->ires_idx = res_idx++;
839 vtpci_alloc_msi(struct vtpci_softc *sc)
846 nmsi = pci_msi_count(dev);
851 sc->vtpci_irq_rid = 0;
852 sc->vtpci_irq_type = pci_alloc_1intr(dev, 1,
853 &sc->vtpci_irq_rid, &irq_flags);
860 vtpci_alloc_msix(struct vtpci_softc *sc, int nvectors)
862 /* XXX(vsrinivas): Huh? Is this how MSI-X works?*/
863 /* XXX(vsrinivas): All of this was disabled... */
866 int nmsix, cnt, required;
870 nmsix = pci_msix_count(dev);
874 /* An additional vector is needed for the config changes. */
875 required = nvectors + 1;
876 if (nmsix >= required) {
878 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required)
881 pci_release_msi(dev);
884 /* Attempt shared MSIX configuration. */
886 if (nmsix >= required) {
888 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
889 sc->vtpci_flags |= VIRTIO_PCI_FLAG_SHARED_MSIX;
893 pci_release_msi(dev);
899 sc->vtpci_nintr_res = required;
900 sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSIX;
903 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
904 device_printf(dev, "using shared virtqueue MSIX\n");
906 device_printf(dev, "using per virtqueue MSIX\n");
913 vtpci_register_msix_vector(struct vtpci_softc *sc, int offset, int res_idx)
920 if (offset != VIRTIO_MSI_CONFIG_VECTOR &&
921 offset != VIRTIO_MSI_QUEUE_VECTOR)
925 /* Map from rid to host vector. */
926 vector = sc->vtpci_intr_res[res_idx].rid - 1;
928 vector = VIRTIO_MSI_NO_VECTOR;
930 /* The first resource is special; make sure it is used correctly. */
932 KASSERT(vector == 0, ("unexpected config vector"));
933 KASSERT(offset == VIRTIO_MSI_CONFIG_VECTOR,
934 ("unexpected config offset"));
937 vtpci_write_config_2(sc, offset, vector);
939 if (vtpci_read_config_2(sc, offset) != vector) {
940 device_printf(dev, "insufficient host resources for "
941 "MSIX interrupts\n");
949 vtpci_free_interrupts(struct vtpci_softc *sc)
952 struct vtpci_intr_resource *ires;
956 sc->vtpci_nintr_res = 0;
958 if (sc->vtpci_flags & (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) {
959 pci_release_msi(dev);
960 sc->vtpci_flags &= ~(VIRTIO_PCI_FLAG_MSI |
961 VIRTIO_PCI_FLAG_MSIX | VIRTIO_PCI_FLAG_SHARED_MSIX);
964 for (i = 0; i < 1 + VIRTIO_MAX_VIRTQUEUES; i++) {
965 ires = &sc->vtpci_intr_res[i];
967 if (ires->intrhand != NULL) {
968 bus_teardown_intr(dev, ires->irq, ires->intrhand);
969 ires->intrhand = NULL;
972 if (ires->irq != NULL) {
973 bus_release_resource(dev, SYS_RES_IRQ, ires->rid,
983 vtpci_free_virtqueues(struct vtpci_softc *sc)
985 struct vtpci_virtqueue *vqx;
990 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) {
991 vqx = &sc->vtpci_vqx[i];
993 if (vqx->vq != NULL) {
994 virtqueue_free(vqx->vq);
1001 vtpci_release_child_resources(struct vtpci_softc *sc)
1004 vtpci_free_interrupts(sc);
1005 vtpci_free_virtqueues(sc);
1009 vtpci_reset(struct vtpci_softc *sc)
1013 * Setting the status to RESET sets the host device to
1014 * the original, uninitialized state.
1016 vtpci_set_status(sc->vtpci_dev, VIRTIO_CONFIG_STATUS_RESET);
1020 vtpci_legacy_intr(void *xsc)
1022 struct vtpci_softc *sc;
1023 struct vtpci_virtqueue *vqx;
1028 vqx = &sc->vtpci_vqx[0];
1030 /* Reading the ISR also clears it. */
1031 isr = vtpci_read_config_1(sc, VIRTIO_PCI_ISR);
1033 if (isr & VIRTIO_PCI_ISR_CONFIG)
1034 vtpci_config_intr(sc);
1036 if (isr & VIRTIO_PCI_ISR_INTR)
1037 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1038 virtqueue_intr(vqx->vq);
1044 vtpci_vq_shared_intr(void *xsc)
1046 struct vtpci_softc *sc;
1047 struct vtpci_virtqueue *vqx;
1052 vqx = &sc->vtpci_vqx[0];
1054 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1055 rc |= virtqueue_intr(vqx->vq);
1061 vtpci_vq_intr(void *xvq)
1063 struct virtqueue *vq;
1067 rc = virtqueue_intr(vq);
1073 vtpci_config_intr(void *xsc)
1075 struct vtpci_softc *sc;
1081 child = sc->vtpci_child_dev;
1084 rc = VIRTIO_CONFIG_CHANGE(child);