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(VTPCI_IO(hw), 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(VTPCI_IO(hw), 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(VTPCI_IO(hw), dst, size,
106 VIRTIO_PCI_CONFIG(hw) + offset);
109 dst = (char *)dst + size;
114 rte_eal_pci_ioport_read(VTPCI_IO(hw), 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(VTPCI_IO(hw), &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(VTPCI_IO(hw), &tmp.u16, size,
140 VIRTIO_PCI_CONFIG(hw) + offset);
143 rte_eal_pci_ioport_write(VTPCI_IO(hw), src, size,
144 VIRTIO_PCI_CONFIG(hw) + offset);
147 src = (const char *)src + size;
152 rte_eal_pci_ioport_write(VTPCI_IO(hw), src, length,
153 VIRTIO_PCI_CONFIG(hw) + offset);
158 legacy_get_features(struct virtio_hw *hw)
162 rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 4,
163 VIRTIO_PCI_HOST_FEATURES);
168 legacy_set_features(struct virtio_hw *hw, uint64_t features)
170 if ((features >> 32) != 0) {
172 "only 32 bit features are allowed for legacy virtio!");
175 rte_eal_pci_ioport_write(VTPCI_IO(hw), &features, 4,
176 VIRTIO_PCI_GUEST_FEATURES);
180 legacy_get_status(struct virtio_hw *hw)
184 rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_STATUS);
189 legacy_set_status(struct virtio_hw *hw, uint8_t status)
191 rte_eal_pci_ioport_write(VTPCI_IO(hw), &status, 1, VIRTIO_PCI_STATUS);
195 legacy_reset(struct virtio_hw *hw)
197 legacy_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
201 legacy_get_isr(struct virtio_hw *hw)
205 rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_ISR);
209 /* Enable one vector (0) for Link State Intrerrupt */
211 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
215 rte_eal_pci_ioport_write(VTPCI_IO(hw), &vec, 2,
216 VIRTIO_MSI_CONFIG_VECTOR);
217 rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 2,
218 VIRTIO_MSI_CONFIG_VECTOR);
223 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
227 rte_eal_pci_ioport_write(VTPCI_IO(hw), &queue_id, 2,
228 VIRTIO_PCI_QUEUE_SEL);
229 rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_PCI_QUEUE_NUM);
234 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
238 if (!check_vq_phys_addr_ok(vq))
241 rte_eal_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
242 VIRTIO_PCI_QUEUE_SEL);
243 src = vq->vq_ring_mem >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
244 rte_eal_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
250 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
254 rte_eal_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
255 VIRTIO_PCI_QUEUE_SEL);
256 rte_eal_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
260 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
262 rte_eal_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
263 VIRTIO_PCI_QUEUE_NOTIFY);
266 #ifdef RTE_EXEC_ENV_LINUXAPP
268 legacy_virtio_has_msix(const struct rte_pci_addr *loc)
271 char dirname[PATH_MAX];
273 snprintf(dirname, sizeof(dirname),
274 "%s/" PCI_PRI_FMT "/msi_irqs", pci_get_sysfs_path(),
275 loc->domain, loc->bus, loc->devid, loc->function);
277 d = opendir(dirname);
285 legacy_virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
287 /* nic_uio does not enable interrupts, return 0 (false). */
293 legacy_virtio_resource_init(struct rte_pci_device *pci_dev,
294 struct virtio_hw *hw, uint32_t *dev_flags)
296 if (rte_eal_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
299 if (pci_dev->intr_handle.type != RTE_INTR_HANDLE_UNKNOWN)
300 *dev_flags |= RTE_ETH_DEV_INTR_LSC;
302 *dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
307 static const struct virtio_pci_ops legacy_ops = {
308 .read_dev_cfg = legacy_read_dev_config,
309 .write_dev_cfg = legacy_write_dev_config,
310 .reset = legacy_reset,
311 .get_status = legacy_get_status,
312 .set_status = legacy_set_status,
313 .get_features = legacy_get_features,
314 .set_features = legacy_set_features,
315 .get_isr = legacy_get_isr,
316 .set_config_irq = legacy_set_config_irq,
317 .get_queue_num = legacy_get_queue_num,
318 .setup_queue = legacy_setup_queue,
319 .del_queue = legacy_del_queue,
320 .notify_queue = legacy_notify_queue,
324 static inline uint8_t
325 io_read8(uint8_t *addr)
327 return *(volatile uint8_t *)addr;
331 io_write8(uint8_t val, uint8_t *addr)
333 *(volatile uint8_t *)addr = val;
336 static inline uint16_t
337 io_read16(uint16_t *addr)
339 return *(volatile uint16_t *)addr;
343 io_write16(uint16_t val, uint16_t *addr)
345 *(volatile uint16_t *)addr = val;
348 static inline uint32_t
349 io_read32(uint32_t *addr)
351 return *(volatile uint32_t *)addr;
355 io_write32(uint32_t val, uint32_t *addr)
357 *(volatile uint32_t *)addr = val;
361 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
363 io_write32(val & ((1ULL << 32) - 1), lo);
364 io_write32(val >> 32, hi);
368 modern_read_dev_config(struct virtio_hw *hw, size_t offset,
369 void *dst, int length)
373 uint8_t old_gen, new_gen;
376 old_gen = io_read8(&hw->common_cfg->config_generation);
379 for (i = 0; i < length; i++)
380 *p++ = io_read8((uint8_t *)hw->dev_cfg + offset + i);
382 new_gen = io_read8(&hw->common_cfg->config_generation);
383 } while (old_gen != new_gen);
387 modern_write_dev_config(struct virtio_hw *hw, size_t offset,
388 const void *src, int length)
391 const uint8_t *p = src;
393 for (i = 0; i < length; i++)
394 io_write8(*p++, (uint8_t *)hw->dev_cfg + offset + i);
398 modern_get_features(struct virtio_hw *hw)
400 uint32_t features_lo, features_hi;
402 io_write32(0, &hw->common_cfg->device_feature_select);
403 features_lo = io_read32(&hw->common_cfg->device_feature);
405 io_write32(1, &hw->common_cfg->device_feature_select);
406 features_hi = io_read32(&hw->common_cfg->device_feature);
408 return ((uint64_t)features_hi << 32) | features_lo;
412 modern_set_features(struct virtio_hw *hw, uint64_t features)
414 io_write32(0, &hw->common_cfg->guest_feature_select);
415 io_write32(features & ((1ULL << 32) - 1),
416 &hw->common_cfg->guest_feature);
418 io_write32(1, &hw->common_cfg->guest_feature_select);
419 io_write32(features >> 32,
420 &hw->common_cfg->guest_feature);
424 modern_get_status(struct virtio_hw *hw)
426 return io_read8(&hw->common_cfg->device_status);
430 modern_set_status(struct virtio_hw *hw, uint8_t status)
432 io_write8(status, &hw->common_cfg->device_status);
436 modern_reset(struct virtio_hw *hw)
438 modern_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
439 modern_get_status(hw);
443 modern_get_isr(struct virtio_hw *hw)
445 return io_read8(hw->isr);
449 modern_set_config_irq(struct virtio_hw *hw, uint16_t vec)
451 io_write16(vec, &hw->common_cfg->msix_config);
452 return io_read16(&hw->common_cfg->msix_config);
456 modern_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
458 io_write16(queue_id, &hw->common_cfg->queue_select);
459 return io_read16(&hw->common_cfg->queue_size);
463 modern_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
465 uint64_t desc_addr, avail_addr, used_addr;
468 if (!check_vq_phys_addr_ok(vq))
471 desc_addr = vq->vq_ring_mem;
472 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
473 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
474 ring[vq->vq_nentries]),
475 VIRTIO_PCI_VRING_ALIGN);
477 io_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
479 io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
480 &hw->common_cfg->queue_desc_hi);
481 io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
482 &hw->common_cfg->queue_avail_hi);
483 io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
484 &hw->common_cfg->queue_used_hi);
486 notify_off = io_read16(&hw->common_cfg->queue_notify_off);
487 vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
488 notify_off * hw->notify_off_multiplier);
490 io_write16(1, &hw->common_cfg->queue_enable);
492 PMD_INIT_LOG(DEBUG, "queue %u addresses:", vq->vq_queue_index);
493 PMD_INIT_LOG(DEBUG, "\t desc_addr: %" PRIx64, desc_addr);
494 PMD_INIT_LOG(DEBUG, "\t aval_addr: %" PRIx64, avail_addr);
495 PMD_INIT_LOG(DEBUG, "\t used_addr: %" PRIx64, used_addr);
496 PMD_INIT_LOG(DEBUG, "\t notify addr: %p (notify offset: %u)",
497 vq->notify_addr, notify_off);
503 modern_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
505 io_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
507 io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
508 &hw->common_cfg->queue_desc_hi);
509 io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
510 &hw->common_cfg->queue_avail_hi);
511 io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
512 &hw->common_cfg->queue_used_hi);
514 io_write16(0, &hw->common_cfg->queue_enable);
518 modern_notify_queue(struct virtio_hw *hw __rte_unused, struct virtqueue *vq)
520 io_write16(1, vq->notify_addr);
523 static const struct virtio_pci_ops modern_ops = {
524 .read_dev_cfg = modern_read_dev_config,
525 .write_dev_cfg = modern_write_dev_config,
526 .reset = modern_reset,
527 .get_status = modern_get_status,
528 .set_status = modern_set_status,
529 .get_features = modern_get_features,
530 .set_features = modern_set_features,
531 .get_isr = modern_get_isr,
532 .set_config_irq = modern_set_config_irq,
533 .get_queue_num = modern_get_queue_num,
534 .setup_queue = modern_setup_queue,
535 .del_queue = modern_del_queue,
536 .notify_queue = modern_notify_queue,
541 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
542 void *dst, int length)
544 VTPCI_OPS(hw)->read_dev_cfg(hw, offset, dst, length);
548 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
549 const void *src, int length)
551 VTPCI_OPS(hw)->write_dev_cfg(hw, offset, src, length);
555 vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features)
560 * Limit negotiated features to what the driver, virtqueue, and
563 features = host_features & hw->guest_features;
564 VTPCI_OPS(hw)->set_features(hw, features);
570 vtpci_reset(struct virtio_hw *hw)
572 VTPCI_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
573 /* flush status write */
574 VTPCI_OPS(hw)->get_status(hw);
578 vtpci_reinit_complete(struct virtio_hw *hw)
580 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
584 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
586 if (status != VIRTIO_CONFIG_STATUS_RESET)
587 status |= VTPCI_OPS(hw)->get_status(hw);
589 VTPCI_OPS(hw)->set_status(hw, status);
593 vtpci_get_status(struct virtio_hw *hw)
595 return VTPCI_OPS(hw)->get_status(hw);
599 vtpci_isr(struct virtio_hw *hw)
601 return VTPCI_OPS(hw)->get_isr(hw);
605 /* Enable one vector (0) for Link State Intrerrupt */
607 vtpci_irq_config(struct virtio_hw *hw, uint16_t vec)
609 return VTPCI_OPS(hw)->set_config_irq(hw, vec);
613 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
615 uint8_t bar = cap->bar;
616 uint32_t length = cap->length;
617 uint32_t offset = cap->offset;
621 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
625 if (offset + length < offset) {
626 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
631 if (offset + length > dev->mem_resource[bar].len) {
633 "invalid cap: overflows bar space: %u > %" PRIu64,
634 offset + length, dev->mem_resource[bar].len);
638 base = dev->mem_resource[bar].addr;
640 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
644 return base + offset;
648 virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
651 struct virtio_pci_cap cap;
654 if (rte_eal_pci_map_device(dev)) {
655 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
659 ret = rte_eal_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
661 PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
666 ret = rte_eal_pci_read_config(dev, &cap, sizeof(cap), pos);
669 "failed to read pci cap at pos: %x", pos);
673 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
675 "[%2x] skipping non VNDR cap id: %02x",
681 "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
682 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
684 switch (cap.cfg_type) {
685 case VIRTIO_PCI_CAP_COMMON_CFG:
686 hw->common_cfg = get_cfg_addr(dev, &cap);
688 case VIRTIO_PCI_CAP_NOTIFY_CFG:
689 rte_eal_pci_read_config(dev, &hw->notify_off_multiplier,
690 4, pos + sizeof(cap));
691 hw->notify_base = get_cfg_addr(dev, &cap);
693 case VIRTIO_PCI_CAP_DEVICE_CFG:
694 hw->dev_cfg = get_cfg_addr(dev, &cap);
696 case VIRTIO_PCI_CAP_ISR_CFG:
697 hw->isr = get_cfg_addr(dev, &cap);
705 if (hw->common_cfg == NULL || hw->notify_base == NULL ||
706 hw->dev_cfg == NULL || hw->isr == NULL) {
707 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
711 PMD_INIT_LOG(INFO, "found modern virtio pci device.");
713 PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", hw->common_cfg);
714 PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", hw->dev_cfg);
715 PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", hw->isr);
716 PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
717 hw->notify_base, hw->notify_off_multiplier);
724 * if there is error mapping with VFIO/UIO.
725 * if port map error when driver type is KDRV_NONE.
726 * if whitelisted but driver type is KDRV_UNKNOWN.
727 * Return 1 if kernel driver is managing the device.
728 * Return 0 on success.
731 vtpci_init(struct rte_pci_device *dev, struct virtio_hw *hw,
737 * Try if we can succeed reading virtio pci caps, which exists
738 * only on modern pci device. If failed, we fallback to legacy
741 if (virtio_read_caps(dev, hw) == 0) {
742 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
743 virtio_hw_internal[hw->port_id].vtpci_ops = &modern_ops;
745 *dev_flags |= RTE_ETH_DEV_INTR_LSC;
749 PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
750 if (legacy_virtio_resource_init(dev, hw, dev_flags) < 0) {
751 if (dev->kdrv == RTE_KDRV_UNKNOWN &&
752 (!dev->device.devargs ||
753 dev->device.devargs->type !=
754 RTE_DEVTYPE_WHITELISTED_PCI)) {
756 "skip kernel managed virtio device.");
762 virtio_hw_internal[hw->port_id].vtpci_ops = &legacy_ops;
763 hw->use_msix = legacy_virtio_has_msix(&dev->addr);