4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifdef RTE_EXEC_ENV_LINUXAPP
40 #include "virtio_pci.h"
41 #include "virtio_logs.h"
42 #include "virtqueue.h"
45 * Following macros are derived from linux/pci_regs.h, however,
46 * we can't simply include that header here, as there is no such
47 * file for non-Linux platform.
49 #define PCI_CAPABILITY_LIST 0x34
50 #define PCI_CAP_ID_VNDR 0x09
53 * The remaining space is defined by each driver as the per-driver
54 * configuration space.
56 #define VIRTIO_PCI_CONFIG(hw) (((hw)->use_msix) ? 24 : 20)
59 check_vq_phys_addr_ok(struct virtqueue *vq)
61 /* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
62 * and only accepts 32 bit page frame number.
63 * Check if the allocated physical memory exceeds 16TB.
65 if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >>
66 (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
67 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
75 * Since we are in legacy mode:
76 * http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf
78 * "Note that this is possible because while the virtio header is PCI (i.e.
79 * little) endian, the device-specific region is encoded in the native endian of
80 * the guest (where such distinction is applicable)."
82 * For powerpc which supports both, qemu supposes that cpu is big endian and
83 * enforces this for the virtio-net stuff.
86 legacy_read_dev_config(struct virtio_hw *hw, size_t offset,
87 void *dst, int length)
89 #ifdef RTE_ARCH_PPC_64
95 rte_eal_pci_ioport_read(&hw->io, dst, size,
96 VIRTIO_PCI_CONFIG(hw) + offset);
97 *(uint32_t *)dst = rte_be_to_cpu_32(*(uint32_t *)dst);
98 } else if (length >= 2) {
100 rte_eal_pci_ioport_read(&hw->io, dst, size,
101 VIRTIO_PCI_CONFIG(hw) + offset);
102 *(uint16_t *)dst = rte_be_to_cpu_16(*(uint16_t *)dst);
105 rte_eal_pci_ioport_read(&hw->io, dst, size,
106 VIRTIO_PCI_CONFIG(hw) + offset);
109 dst = (char *)dst + size;
114 rte_eal_pci_ioport_read(&hw->io, dst, length,
115 VIRTIO_PCI_CONFIG(hw) + offset);
120 legacy_write_dev_config(struct virtio_hw *hw, size_t offset,
121 const void *src, int length)
123 #ifdef RTE_ARCH_PPC_64
133 tmp.u32 = rte_cpu_to_be_32(*(const uint32_t *)src);
134 rte_eal_pci_ioport_write(&hw->io, &tmp.u32, size,
135 VIRTIO_PCI_CONFIG(hw) + offset);
136 } else if (length >= 2) {
138 tmp.u16 = rte_cpu_to_be_16(*(const uint16_t *)src);
139 rte_eal_pci_ioport_write(&hw->io, &tmp.u16, size,
140 VIRTIO_PCI_CONFIG(hw) + offset);
143 rte_eal_pci_ioport_write(&hw->io, src, size,
144 VIRTIO_PCI_CONFIG(hw) + offset);
147 src = (const char *)src + size;
152 rte_eal_pci_ioport_write(&hw->io, src, length,
153 VIRTIO_PCI_CONFIG(hw) + offset);
158 legacy_get_features(struct virtio_hw *hw)
162 rte_eal_pci_ioport_read(&hw->io, &dst, 4, VIRTIO_PCI_HOST_FEATURES);
167 legacy_set_features(struct virtio_hw *hw, uint64_t features)
169 if ((features >> 32) != 0) {
171 "only 32 bit features are allowed for legacy virtio!");
174 rte_eal_pci_ioport_write(&hw->io, &features, 4,
175 VIRTIO_PCI_GUEST_FEATURES);
179 legacy_get_status(struct virtio_hw *hw)
183 rte_eal_pci_ioport_read(&hw->io, &dst, 1, VIRTIO_PCI_STATUS);
188 legacy_set_status(struct virtio_hw *hw, uint8_t status)
190 rte_eal_pci_ioport_write(&hw->io, &status, 1, VIRTIO_PCI_STATUS);
194 legacy_reset(struct virtio_hw *hw)
196 legacy_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
200 legacy_get_isr(struct virtio_hw *hw)
204 rte_eal_pci_ioport_read(&hw->io, &dst, 1, VIRTIO_PCI_ISR);
208 /* Enable one vector (0) for Link State Intrerrupt */
210 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
214 rte_eal_pci_ioport_write(&hw->io, &vec, 2, VIRTIO_MSI_CONFIG_VECTOR);
215 rte_eal_pci_ioport_read(&hw->io, &dst, 2, VIRTIO_MSI_CONFIG_VECTOR);
220 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
224 rte_eal_pci_ioport_write(&hw->io, &queue_id, 2, VIRTIO_PCI_QUEUE_SEL);
225 rte_eal_pci_ioport_read(&hw->io, &dst, 2, VIRTIO_PCI_QUEUE_NUM);
230 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
234 if (!check_vq_phys_addr_ok(vq))
237 rte_eal_pci_ioport_write(&hw->io, &vq->vq_queue_index, 2,
238 VIRTIO_PCI_QUEUE_SEL);
239 src = vq->vq_ring_mem >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
240 rte_eal_pci_ioport_write(&hw->io, &src, 4, VIRTIO_PCI_QUEUE_PFN);
246 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
250 rte_eal_pci_ioport_write(&hw->io, &vq->vq_queue_index, 2,
251 VIRTIO_PCI_QUEUE_SEL);
252 rte_eal_pci_ioport_write(&hw->io, &src, 4, VIRTIO_PCI_QUEUE_PFN);
256 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
258 rte_eal_pci_ioport_write(&hw->io, &vq->vq_queue_index, 2,
259 VIRTIO_PCI_QUEUE_NOTIFY);
262 #ifdef RTE_EXEC_ENV_LINUXAPP
264 legacy_virtio_has_msix(const struct rte_pci_addr *loc)
267 char dirname[PATH_MAX];
269 snprintf(dirname, sizeof(dirname),
270 "%s/" PCI_PRI_FMT "/msi_irqs", pci_get_sysfs_path(),
271 loc->domain, loc->bus, loc->devid, loc->function);
273 d = opendir(dirname);
281 legacy_virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
283 /* nic_uio does not enable interrupts, return 0 (false). */
289 legacy_virtio_resource_init(struct rte_pci_device *pci_dev,
290 struct virtio_hw *hw, uint32_t *dev_flags)
292 if (rte_eal_pci_ioport_map(pci_dev, 0, &hw->io) < 0)
295 if (pci_dev->intr_handle.type != RTE_INTR_HANDLE_UNKNOWN)
296 *dev_flags |= RTE_ETH_DEV_INTR_LSC;
298 *dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
303 static const struct virtio_pci_ops legacy_ops = {
304 .read_dev_cfg = legacy_read_dev_config,
305 .write_dev_cfg = legacy_write_dev_config,
306 .reset = legacy_reset,
307 .get_status = legacy_get_status,
308 .set_status = legacy_set_status,
309 .get_features = legacy_get_features,
310 .set_features = legacy_set_features,
311 .get_isr = legacy_get_isr,
312 .set_config_irq = legacy_set_config_irq,
313 .get_queue_num = legacy_get_queue_num,
314 .setup_queue = legacy_setup_queue,
315 .del_queue = legacy_del_queue,
316 .notify_queue = legacy_notify_queue,
320 static inline uint8_t
321 io_read8(uint8_t *addr)
323 return *(volatile uint8_t *)addr;
327 io_write8(uint8_t val, uint8_t *addr)
329 *(volatile uint8_t *)addr = val;
332 static inline uint16_t
333 io_read16(uint16_t *addr)
335 return *(volatile uint16_t *)addr;
339 io_write16(uint16_t val, uint16_t *addr)
341 *(volatile uint16_t *)addr = val;
344 static inline uint32_t
345 io_read32(uint32_t *addr)
347 return *(volatile uint32_t *)addr;
351 io_write32(uint32_t val, uint32_t *addr)
353 *(volatile uint32_t *)addr = val;
357 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
359 io_write32(val & ((1ULL << 32) - 1), lo);
360 io_write32(val >> 32, hi);
364 modern_read_dev_config(struct virtio_hw *hw, size_t offset,
365 void *dst, int length)
369 uint8_t old_gen, new_gen;
372 old_gen = io_read8(&hw->common_cfg->config_generation);
375 for (i = 0; i < length; i++)
376 *p++ = io_read8((uint8_t *)hw->dev_cfg + offset + i);
378 new_gen = io_read8(&hw->common_cfg->config_generation);
379 } while (old_gen != new_gen);
383 modern_write_dev_config(struct virtio_hw *hw, size_t offset,
384 const void *src, int length)
387 const uint8_t *p = src;
389 for (i = 0; i < length; i++)
390 io_write8(*p++, (uint8_t *)hw->dev_cfg + offset + i);
394 modern_get_features(struct virtio_hw *hw)
396 uint32_t features_lo, features_hi;
398 io_write32(0, &hw->common_cfg->device_feature_select);
399 features_lo = io_read32(&hw->common_cfg->device_feature);
401 io_write32(1, &hw->common_cfg->device_feature_select);
402 features_hi = io_read32(&hw->common_cfg->device_feature);
404 return ((uint64_t)features_hi << 32) | features_lo;
408 modern_set_features(struct virtio_hw *hw, uint64_t features)
410 io_write32(0, &hw->common_cfg->guest_feature_select);
411 io_write32(features & ((1ULL << 32) - 1),
412 &hw->common_cfg->guest_feature);
414 io_write32(1, &hw->common_cfg->guest_feature_select);
415 io_write32(features >> 32,
416 &hw->common_cfg->guest_feature);
420 modern_get_status(struct virtio_hw *hw)
422 return io_read8(&hw->common_cfg->device_status);
426 modern_set_status(struct virtio_hw *hw, uint8_t status)
428 io_write8(status, &hw->common_cfg->device_status);
432 modern_reset(struct virtio_hw *hw)
434 modern_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
435 modern_get_status(hw);
439 modern_get_isr(struct virtio_hw *hw)
441 return io_read8(hw->isr);
445 modern_set_config_irq(struct virtio_hw *hw, uint16_t vec)
447 io_write16(vec, &hw->common_cfg->msix_config);
448 return io_read16(&hw->common_cfg->msix_config);
452 modern_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
454 io_write16(queue_id, &hw->common_cfg->queue_select);
455 return io_read16(&hw->common_cfg->queue_size);
459 modern_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
461 uint64_t desc_addr, avail_addr, used_addr;
464 if (!check_vq_phys_addr_ok(vq))
467 desc_addr = vq->vq_ring_mem;
468 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
469 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
470 ring[vq->vq_nentries]),
471 VIRTIO_PCI_VRING_ALIGN);
473 io_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
475 io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
476 &hw->common_cfg->queue_desc_hi);
477 io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
478 &hw->common_cfg->queue_avail_hi);
479 io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
480 &hw->common_cfg->queue_used_hi);
482 notify_off = io_read16(&hw->common_cfg->queue_notify_off);
483 vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
484 notify_off * hw->notify_off_multiplier);
486 io_write16(1, &hw->common_cfg->queue_enable);
488 PMD_INIT_LOG(DEBUG, "queue %u addresses:", vq->vq_queue_index);
489 PMD_INIT_LOG(DEBUG, "\t desc_addr: %" PRIx64, desc_addr);
490 PMD_INIT_LOG(DEBUG, "\t aval_addr: %" PRIx64, avail_addr);
491 PMD_INIT_LOG(DEBUG, "\t used_addr: %" PRIx64, used_addr);
492 PMD_INIT_LOG(DEBUG, "\t notify addr: %p (notify offset: %u)",
493 vq->notify_addr, notify_off);
499 modern_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
501 io_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
503 io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
504 &hw->common_cfg->queue_desc_hi);
505 io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
506 &hw->common_cfg->queue_avail_hi);
507 io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
508 &hw->common_cfg->queue_used_hi);
510 io_write16(0, &hw->common_cfg->queue_enable);
514 modern_notify_queue(struct virtio_hw *hw __rte_unused, struct virtqueue *vq)
516 io_write16(1, vq->notify_addr);
519 static const struct virtio_pci_ops modern_ops = {
520 .read_dev_cfg = modern_read_dev_config,
521 .write_dev_cfg = modern_write_dev_config,
522 .reset = modern_reset,
523 .get_status = modern_get_status,
524 .set_status = modern_set_status,
525 .get_features = modern_get_features,
526 .set_features = modern_set_features,
527 .get_isr = modern_get_isr,
528 .set_config_irq = modern_set_config_irq,
529 .get_queue_num = modern_get_queue_num,
530 .setup_queue = modern_setup_queue,
531 .del_queue = modern_del_queue,
532 .notify_queue = modern_notify_queue,
537 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
538 void *dst, int length)
540 hw->vtpci_ops->read_dev_cfg(hw, offset, dst, length);
544 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
545 const void *src, int length)
547 hw->vtpci_ops->write_dev_cfg(hw, offset, src, length);
551 vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features)
556 * Limit negotiated features to what the driver, virtqueue, and
559 features = host_features & hw->guest_features;
560 hw->vtpci_ops->set_features(hw, features);
566 vtpci_reset(struct virtio_hw *hw)
568 hw->vtpci_ops->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
569 /* flush status write */
570 hw->vtpci_ops->get_status(hw);
574 vtpci_reinit_complete(struct virtio_hw *hw)
576 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
580 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
582 if (status != VIRTIO_CONFIG_STATUS_RESET)
583 status |= hw->vtpci_ops->get_status(hw);
585 hw->vtpci_ops->set_status(hw, status);
589 vtpci_get_status(struct virtio_hw *hw)
591 return hw->vtpci_ops->get_status(hw);
595 vtpci_isr(struct virtio_hw *hw)
597 return hw->vtpci_ops->get_isr(hw);
601 /* Enable one vector (0) for Link State Intrerrupt */
603 vtpci_irq_config(struct virtio_hw *hw, uint16_t vec)
605 return hw->vtpci_ops->set_config_irq(hw, vec);
609 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
611 uint8_t bar = cap->bar;
612 uint32_t length = cap->length;
613 uint32_t offset = cap->offset;
617 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
621 if (offset + length < offset) {
622 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
627 if (offset + length > dev->mem_resource[bar].len) {
629 "invalid cap: overflows bar space: %u > %" PRIu64,
630 offset + length, dev->mem_resource[bar].len);
634 base = dev->mem_resource[bar].addr;
636 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
640 return base + offset;
644 virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
647 struct virtio_pci_cap cap;
650 if (rte_eal_pci_map_device(dev)) {
651 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
655 ret = rte_eal_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
657 PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
662 ret = rte_eal_pci_read_config(dev, &cap, sizeof(cap), pos);
665 "failed to read pci cap at pos: %x", pos);
669 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
671 "[%2x] skipping non VNDR cap id: %02x",
677 "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
678 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
680 switch (cap.cfg_type) {
681 case VIRTIO_PCI_CAP_COMMON_CFG:
682 hw->common_cfg = get_cfg_addr(dev, &cap);
684 case VIRTIO_PCI_CAP_NOTIFY_CFG:
685 rte_eal_pci_read_config(dev, &hw->notify_off_multiplier,
686 4, pos + sizeof(cap));
687 hw->notify_base = get_cfg_addr(dev, &cap);
689 case VIRTIO_PCI_CAP_DEVICE_CFG:
690 hw->dev_cfg = get_cfg_addr(dev, &cap);
692 case VIRTIO_PCI_CAP_ISR_CFG:
693 hw->isr = get_cfg_addr(dev, &cap);
701 if (hw->common_cfg == NULL || hw->notify_base == NULL ||
702 hw->dev_cfg == NULL || hw->isr == NULL) {
703 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
707 PMD_INIT_LOG(INFO, "found modern virtio pci device.");
709 PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", hw->common_cfg);
710 PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", hw->dev_cfg);
711 PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", hw->isr);
712 PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
713 hw->notify_base, hw->notify_off_multiplier);
720 * if there is error mapping with VFIO/UIO.
721 * if port map error when driver type is KDRV_NONE.
722 * if whitelisted but driver type is KDRV_UNKNOWN.
723 * Return 1 if kernel driver is managing the device.
724 * Return 0 on success.
727 vtpci_init(struct rte_pci_device *dev, struct virtio_hw *hw,
733 * Try if we can succeed reading virtio pci caps, which exists
734 * only on modern pci device. If failed, we fallback to legacy
737 if (virtio_read_caps(dev, hw) == 0) {
738 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
739 hw->vtpci_ops = &modern_ops;
741 *dev_flags |= RTE_ETH_DEV_INTR_LSC;
745 PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
746 if (legacy_virtio_resource_init(dev, hw, dev_flags) < 0) {
747 if (dev->kdrv == RTE_KDRV_UNKNOWN &&
748 (!dev->device.devargs ||
749 dev->device.devargs->type !=
750 RTE_DEVTYPE_WHITELISTED_PCI)) {
752 "skip kernel managed virtio device.");
758 hw->vtpci_ops = &legacy_ops;
759 hw->use_msix = legacy_virtio_has_msix(&dev->addr);