1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
6 #ifdef RTE_EXEC_ENV_LINUX
14 #include "virtio_pci.h"
15 #include "virtio_logs.h"
16 #include "virtqueue.h"
19 * Following macros are derived from linux/pci_regs.h, however,
20 * we can't simply include that header here, as there is no such
21 * file for non-Linux platform.
23 #define PCI_CAPABILITY_LIST 0x34
24 #define PCI_CAP_ID_VNDR 0x09
25 #define PCI_CAP_ID_MSIX 0x11
28 * The remaining space is defined by each driver as the per-driver
29 * configuration space.
31 #define VIRTIO_PCI_CONFIG(hw) \
32 (((hw)->use_msix == VIRTIO_MSIX_ENABLED) ? 24 : 20)
35 check_vq_phys_addr_ok(struct virtqueue *vq)
37 /* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
38 * and only accepts 32 bit page frame number.
39 * Check if the allocated physical memory exceeds 16TB.
41 if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >>
42 (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
43 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
50 #define PCI_MSIX_ENABLE 0x8000
52 static enum virtio_msix_status
53 vtpci_msix_detect(struct rte_pci_device *dev)
58 ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
61 "failed to read pci capability list, ret %d", ret);
62 return VIRTIO_MSIX_NONE;
68 ret = rte_pci_read_config(dev, cap, sizeof(cap), pos);
69 if (ret != sizeof(cap)) {
71 "failed to read pci cap at pos: %x ret %d",
76 if (cap[0] == PCI_CAP_ID_MSIX) {
79 ret = rte_pci_read_config(dev, &flags, sizeof(flags),
81 if (ret != sizeof(flags)) {
83 "failed to read pci cap at pos:"
84 " %x ret %d", pos + 2, ret);
88 if (flags & PCI_MSIX_ENABLE)
89 return VIRTIO_MSIX_ENABLED;
91 return VIRTIO_MSIX_DISABLED;
97 return VIRTIO_MSIX_NONE;
101 * Since we are in legacy mode:
102 * http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf
104 * "Note that this is possible because while the virtio header is PCI (i.e.
105 * little) endian, the device-specific region is encoded in the native endian of
106 * the guest (where such distinction is applicable)."
108 * For powerpc which supports both, qemu supposes that cpu is big endian and
109 * enforces this for the virtio-net stuff.
112 legacy_read_dev_config(struct virtio_hw *hw, size_t offset,
113 void *dst, int length)
115 #ifdef RTE_ARCH_PPC_64
121 rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
122 VIRTIO_PCI_CONFIG(hw) + offset);
123 *(uint32_t *)dst = rte_be_to_cpu_32(*(uint32_t *)dst);
124 } else if (length >= 2) {
126 rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
127 VIRTIO_PCI_CONFIG(hw) + offset);
128 *(uint16_t *)dst = rte_be_to_cpu_16(*(uint16_t *)dst);
131 rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
132 VIRTIO_PCI_CONFIG(hw) + offset);
135 dst = (char *)dst + size;
140 rte_pci_ioport_read(VTPCI_IO(hw), dst, length,
141 VIRTIO_PCI_CONFIG(hw) + offset);
146 legacy_write_dev_config(struct virtio_hw *hw, size_t offset,
147 const void *src, int length)
149 #ifdef RTE_ARCH_PPC_64
159 tmp.u32 = rte_cpu_to_be_32(*(const uint32_t *)src);
160 rte_pci_ioport_write(VTPCI_IO(hw), &tmp.u32, size,
161 VIRTIO_PCI_CONFIG(hw) + offset);
162 } else if (length >= 2) {
164 tmp.u16 = rte_cpu_to_be_16(*(const uint16_t *)src);
165 rte_pci_ioport_write(VTPCI_IO(hw), &tmp.u16, size,
166 VIRTIO_PCI_CONFIG(hw) + offset);
169 rte_pci_ioport_write(VTPCI_IO(hw), src, size,
170 VIRTIO_PCI_CONFIG(hw) + offset);
173 src = (const char *)src + size;
178 rte_pci_ioport_write(VTPCI_IO(hw), src, length,
179 VIRTIO_PCI_CONFIG(hw) + offset);
184 legacy_get_features(struct virtio_hw *hw)
188 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 4, VIRTIO_PCI_HOST_FEATURES);
193 legacy_set_features(struct virtio_hw *hw, uint64_t features)
195 if ((features >> 32) != 0) {
197 "only 32 bit features are allowed for legacy virtio!");
200 rte_pci_ioport_write(VTPCI_IO(hw), &features, 4,
201 VIRTIO_PCI_GUEST_FEATURES);
205 legacy_get_status(struct virtio_hw *hw)
209 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_STATUS);
214 legacy_set_status(struct virtio_hw *hw, uint8_t status)
216 rte_pci_ioport_write(VTPCI_IO(hw), &status, 1, VIRTIO_PCI_STATUS);
220 legacy_get_isr(struct virtio_hw *hw)
224 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_ISR);
228 /* Enable one vector (0) for Link State Intrerrupt */
230 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
234 rte_pci_ioport_write(VTPCI_IO(hw), &vec, 2, VIRTIO_MSI_CONFIG_VECTOR);
235 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_MSI_CONFIG_VECTOR);
240 legacy_set_queue_irq(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec)
244 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
245 VIRTIO_PCI_QUEUE_SEL);
246 rte_pci_ioport_write(VTPCI_IO(hw), &vec, 2, VIRTIO_MSI_QUEUE_VECTOR);
247 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_MSI_QUEUE_VECTOR);
252 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
256 rte_pci_ioport_write(VTPCI_IO(hw), &queue_id, 2, VIRTIO_PCI_QUEUE_SEL);
257 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_PCI_QUEUE_NUM);
262 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
266 if (!check_vq_phys_addr_ok(vq))
269 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
270 VIRTIO_PCI_QUEUE_SEL);
271 src = vq->vq_ring_mem >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
272 rte_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
278 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
282 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
283 VIRTIO_PCI_QUEUE_SEL);
284 rte_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
288 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
290 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
291 VIRTIO_PCI_QUEUE_NOTIFY);
295 legacy_intr_detect(struct virtio_hw *hw)
297 hw->use_msix = vtpci_msix_detect(VTPCI_DEV(hw));
300 const struct virtio_pci_ops legacy_ops = {
301 .read_dev_cfg = legacy_read_dev_config,
302 .write_dev_cfg = legacy_write_dev_config,
303 .get_status = legacy_get_status,
304 .set_status = legacy_set_status,
305 .get_features = legacy_get_features,
306 .set_features = legacy_set_features,
307 .get_isr = legacy_get_isr,
308 .set_config_irq = legacy_set_config_irq,
309 .set_queue_irq = legacy_set_queue_irq,
310 .get_queue_num = legacy_get_queue_num,
311 .setup_queue = legacy_setup_queue,
312 .del_queue = legacy_del_queue,
313 .notify_queue = legacy_notify_queue,
314 .intr_detect = legacy_intr_detect,
318 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
320 rte_write32(val & ((1ULL << 32) - 1), lo);
321 rte_write32(val >> 32, hi);
325 modern_read_dev_config(struct virtio_hw *hw, size_t offset,
326 void *dst, int length)
330 uint8_t old_gen, new_gen;
333 old_gen = rte_read8(&hw->common_cfg->config_generation);
336 for (i = 0; i < length; i++)
337 *p++ = rte_read8((uint8_t *)hw->dev_cfg + offset + i);
339 new_gen = rte_read8(&hw->common_cfg->config_generation);
340 } while (old_gen != new_gen);
344 modern_write_dev_config(struct virtio_hw *hw, size_t offset,
345 const void *src, int length)
348 const uint8_t *p = src;
350 for (i = 0; i < length; i++)
351 rte_write8((*p++), (((uint8_t *)hw->dev_cfg) + offset + i));
355 modern_get_features(struct virtio_hw *hw)
357 uint32_t features_lo, features_hi;
359 rte_write32(0, &hw->common_cfg->device_feature_select);
360 features_lo = rte_read32(&hw->common_cfg->device_feature);
362 rte_write32(1, &hw->common_cfg->device_feature_select);
363 features_hi = rte_read32(&hw->common_cfg->device_feature);
365 return ((uint64_t)features_hi << 32) | features_lo;
369 modern_set_features(struct virtio_hw *hw, uint64_t features)
371 rte_write32(0, &hw->common_cfg->guest_feature_select);
372 rte_write32(features & ((1ULL << 32) - 1),
373 &hw->common_cfg->guest_feature);
375 rte_write32(1, &hw->common_cfg->guest_feature_select);
376 rte_write32(features >> 32,
377 &hw->common_cfg->guest_feature);
381 modern_get_status(struct virtio_hw *hw)
383 return rte_read8(&hw->common_cfg->device_status);
387 modern_set_status(struct virtio_hw *hw, uint8_t status)
389 rte_write8(status, &hw->common_cfg->device_status);
393 modern_get_isr(struct virtio_hw *hw)
395 return rte_read8(hw->isr);
399 modern_set_config_irq(struct virtio_hw *hw, uint16_t vec)
401 rte_write16(vec, &hw->common_cfg->msix_config);
402 return rte_read16(&hw->common_cfg->msix_config);
406 modern_set_queue_irq(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec)
408 rte_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
409 rte_write16(vec, &hw->common_cfg->queue_msix_vector);
410 return rte_read16(&hw->common_cfg->queue_msix_vector);
414 modern_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
416 rte_write16(queue_id, &hw->common_cfg->queue_select);
417 return rte_read16(&hw->common_cfg->queue_size);
421 modern_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
423 uint64_t desc_addr, avail_addr, used_addr;
426 if (!check_vq_phys_addr_ok(vq))
429 desc_addr = vq->vq_ring_mem;
430 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
431 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
432 ring[vq->vq_nentries]),
433 VIRTIO_PCI_VRING_ALIGN);
435 rte_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
437 io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
438 &hw->common_cfg->queue_desc_hi);
439 io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
440 &hw->common_cfg->queue_avail_hi);
441 io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
442 &hw->common_cfg->queue_used_hi);
444 notify_off = rte_read16(&hw->common_cfg->queue_notify_off);
445 vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
446 notify_off * hw->notify_off_multiplier);
448 rte_write16(1, &hw->common_cfg->queue_enable);
450 PMD_INIT_LOG(DEBUG, "queue %u addresses:", vq->vq_queue_index);
451 PMD_INIT_LOG(DEBUG, "\t desc_addr: %" PRIx64, desc_addr);
452 PMD_INIT_LOG(DEBUG, "\t aval_addr: %" PRIx64, avail_addr);
453 PMD_INIT_LOG(DEBUG, "\t used_addr: %" PRIx64, used_addr);
454 PMD_INIT_LOG(DEBUG, "\t notify addr: %p (notify offset: %u)",
455 vq->notify_addr, notify_off);
461 modern_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
463 rte_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
465 io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
466 &hw->common_cfg->queue_desc_hi);
467 io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
468 &hw->common_cfg->queue_avail_hi);
469 io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
470 &hw->common_cfg->queue_used_hi);
472 rte_write16(0, &hw->common_cfg->queue_enable);
476 modern_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
478 uint32_t notify_data;
480 if (!vtpci_with_feature(hw, VIRTIO_F_NOTIFICATION_DATA)) {
481 rte_write16(vq->vq_queue_index, vq->notify_addr);
485 if (vtpci_with_feature(hw, VIRTIO_F_RING_PACKED)) {
487 * Bit[0:15]: vq queue index
488 * Bit[16:30]: avail index
489 * Bit[31]: avail wrap counter
491 notify_data = ((uint32_t)(!!(vq->vq_packed.cached_flags &
492 VRING_PACKED_DESC_F_AVAIL)) << 31) |
493 ((uint32_t)vq->vq_avail_idx << 16) |
497 * Bit[0:15]: vq queue index
498 * Bit[16:31]: avail index
500 notify_data = ((uint32_t)vq->vq_avail_idx << 16) |
503 rte_write32(notify_data, vq->notify_addr);
509 modern_intr_detect(struct virtio_hw *hw)
511 hw->use_msix = vtpci_msix_detect(VTPCI_DEV(hw));
514 const struct virtio_pci_ops modern_ops = {
515 .read_dev_cfg = modern_read_dev_config,
516 .write_dev_cfg = modern_write_dev_config,
517 .get_status = modern_get_status,
518 .set_status = modern_set_status,
519 .get_features = modern_get_features,
520 .set_features = modern_set_features,
521 .get_isr = modern_get_isr,
522 .set_config_irq = modern_set_config_irq,
523 .set_queue_irq = modern_set_queue_irq,
524 .get_queue_num = modern_get_queue_num,
525 .setup_queue = modern_setup_queue,
526 .del_queue = modern_del_queue,
527 .notify_queue = modern_notify_queue,
528 .intr_detect = modern_intr_detect,
533 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
534 void *dst, int length)
536 VTPCI_OPS(hw)->read_dev_cfg(hw, offset, dst, length);
540 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
541 const void *src, int length)
543 VTPCI_OPS(hw)->write_dev_cfg(hw, offset, src, length);
547 vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features)
552 * Limit negotiated features to what the driver, virtqueue, and
555 features = host_features & hw->guest_features;
556 VTPCI_OPS(hw)->set_features(hw, features);
562 vtpci_reset(struct virtio_hw *hw)
564 VTPCI_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
565 /* flush status write */
566 VTPCI_OPS(hw)->get_status(hw);
570 vtpci_reinit_complete(struct virtio_hw *hw)
572 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
576 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
578 if (status != VIRTIO_CONFIG_STATUS_RESET)
579 status |= VTPCI_OPS(hw)->get_status(hw);
581 VTPCI_OPS(hw)->set_status(hw, status);
585 vtpci_get_status(struct virtio_hw *hw)
587 return VTPCI_OPS(hw)->get_status(hw);
591 vtpci_isr(struct virtio_hw *hw)
593 return VTPCI_OPS(hw)->get_isr(hw);
597 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
599 uint8_t bar = cap->bar;
600 uint32_t length = cap->length;
601 uint32_t offset = cap->offset;
604 if (bar >= PCI_MAX_RESOURCE) {
605 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
609 if (offset + length < offset) {
610 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
615 if (offset + length > dev->mem_resource[bar].len) {
617 "invalid cap: overflows bar space: %u > %" PRIu64,
618 offset + length, dev->mem_resource[bar].len);
622 base = dev->mem_resource[bar].addr;
624 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
628 return base + offset;
632 virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
635 struct virtio_pci_cap cap;
638 if (rte_pci_map_device(dev)) {
639 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
643 ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
646 "failed to read pci capability list, ret %d", ret);
651 ret = rte_pci_read_config(dev, &cap, 2, pos);
654 "failed to read pci cap at pos: %x ret %d",
659 if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
660 /* Transitional devices would also have this capability,
661 * that's why we also check if msix is enabled.
662 * 1st byte is cap ID; 2nd byte is the position of next
663 * cap; next two bytes are the flags.
667 ret = rte_pci_read_config(dev, &flags, sizeof(flags),
669 if (ret != sizeof(flags)) {
671 "failed to read pci cap at pos:"
672 " %x ret %d", pos + 2, ret);
676 if (flags & PCI_MSIX_ENABLE)
677 hw->use_msix = VIRTIO_MSIX_ENABLED;
679 hw->use_msix = VIRTIO_MSIX_DISABLED;
682 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
684 "[%2x] skipping non VNDR cap id: %02x",
689 ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
690 if (ret != sizeof(cap)) {
692 "failed to read pci cap at pos: %x ret %d",
698 "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
699 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
701 switch (cap.cfg_type) {
702 case VIRTIO_PCI_CAP_COMMON_CFG:
703 hw->common_cfg = get_cfg_addr(dev, &cap);
705 case VIRTIO_PCI_CAP_NOTIFY_CFG:
706 ret = rte_pci_read_config(dev,
707 &hw->notify_off_multiplier,
708 4, pos + sizeof(cap));
711 "failed to read notify_off_multiplier, ret %d",
714 hw->notify_base = get_cfg_addr(dev, &cap);
716 case VIRTIO_PCI_CAP_DEVICE_CFG:
717 hw->dev_cfg = get_cfg_addr(dev, &cap);
719 case VIRTIO_PCI_CAP_ISR_CFG:
720 hw->isr = get_cfg_addr(dev, &cap);
728 if (hw->common_cfg == NULL || hw->notify_base == NULL ||
729 hw->dev_cfg == NULL || hw->isr == NULL) {
730 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
734 PMD_INIT_LOG(INFO, "found modern virtio pci device.");
736 PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", hw->common_cfg);
737 PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", hw->dev_cfg);
738 PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", hw->isr);
739 PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
740 hw->notify_base, hw->notify_off_multiplier);
747 * if there is error mapping with VFIO/UIO.
748 * if port map error when driver type is KDRV_NONE.
749 * if marked as allowed but driver type is KDRV_UNKNOWN.
750 * Return 1 if kernel driver is managing the device.
751 * Return 0 on success.
754 vtpci_init(struct rte_pci_device *dev, struct virtio_hw *hw)
756 RTE_BUILD_BUG_ON(offsetof(struct virtio_pci_dev, hw) != 0);
759 * Try if we can succeed reading virtio pci caps, which exists
760 * only on modern pci device. If failed, we fallback to legacy
763 if (virtio_read_caps(dev, hw) == 0) {
764 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
765 virtio_hw_internal[hw->port_id].vtpci_ops = &modern_ops;
766 hw->bus_type = VIRTIO_BUS_PCI_MODERN;
770 PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
771 if (rte_pci_ioport_map(dev, 0, VTPCI_IO(hw)) < 0) {
772 rte_pci_unmap_device(dev);
773 if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN &&
774 (!dev->device.devargs ||
775 dev->device.devargs->bus !=
776 rte_bus_find_by_name("pci"))) {
778 "skip kernel managed virtio device.");
784 virtio_hw_internal[hw->port_id].vtpci_ops = &legacy_ops;
785 hw->bus_type = VIRTIO_BUS_PCI_LEGACY;
788 VTPCI_OPS(hw)->intr_detect(hw);