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
43 #include "virtio_pci.h"
44 #include "virtio_logs.h"
45 #include "virtqueue.h"
48 * Following macros are derived from linux/pci_regs.h, however,
49 * we can't simply include that header here, as there is no such
50 * file for non-Linux platform.
52 #define PCI_CAPABILITY_LIST 0x34
53 #define PCI_CAP_ID_VNDR 0x09
54 #define PCI_CAP_ID_MSIX 0x11
57 * The remaining space is defined by each driver as the per-driver
58 * configuration space.
60 #define VIRTIO_PCI_CONFIG(hw) (((hw)->use_msix) ? 24 : 20)
63 check_vq_phys_addr_ok(struct virtqueue *vq)
65 /* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
66 * and only accepts 32 bit page frame number.
67 * Check if the allocated physical memory exceeds 16TB.
69 if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >>
70 (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
71 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
79 * Since we are in legacy mode:
80 * http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf
82 * "Note that this is possible because while the virtio header is PCI (i.e.
83 * little) endian, the device-specific region is encoded in the native endian of
84 * the guest (where such distinction is applicable)."
86 * For powerpc which supports both, qemu supposes that cpu is big endian and
87 * enforces this for the virtio-net stuff.
90 legacy_read_dev_config(struct virtio_hw *hw, size_t offset,
91 void *dst, int length)
93 #ifdef RTE_ARCH_PPC_64
99 rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
100 VIRTIO_PCI_CONFIG(hw) + offset);
101 *(uint32_t *)dst = rte_be_to_cpu_32(*(uint32_t *)dst);
102 } else if (length >= 2) {
104 rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
105 VIRTIO_PCI_CONFIG(hw) + offset);
106 *(uint16_t *)dst = rte_be_to_cpu_16(*(uint16_t *)dst);
109 rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
110 VIRTIO_PCI_CONFIG(hw) + offset);
113 dst = (char *)dst + size;
118 rte_pci_ioport_read(VTPCI_IO(hw), dst, length,
119 VIRTIO_PCI_CONFIG(hw) + offset);
124 legacy_write_dev_config(struct virtio_hw *hw, size_t offset,
125 const void *src, int length)
127 #ifdef RTE_ARCH_PPC_64
137 tmp.u32 = rte_cpu_to_be_32(*(const uint32_t *)src);
138 rte_pci_ioport_write(VTPCI_IO(hw), &tmp.u32, size,
139 VIRTIO_PCI_CONFIG(hw) + offset);
140 } else if (length >= 2) {
142 tmp.u16 = rte_cpu_to_be_16(*(const uint16_t *)src);
143 rte_pci_ioport_write(VTPCI_IO(hw), &tmp.u16, size,
144 VIRTIO_PCI_CONFIG(hw) + offset);
147 rte_pci_ioport_write(VTPCI_IO(hw), src, size,
148 VIRTIO_PCI_CONFIG(hw) + offset);
151 src = (const char *)src + size;
156 rte_pci_ioport_write(VTPCI_IO(hw), src, length,
157 VIRTIO_PCI_CONFIG(hw) + offset);
162 legacy_get_features(struct virtio_hw *hw)
166 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 4, VIRTIO_PCI_HOST_FEATURES);
171 legacy_set_features(struct virtio_hw *hw, uint64_t features)
173 if ((features >> 32) != 0) {
175 "only 32 bit features are allowed for legacy virtio!");
178 rte_pci_ioport_write(VTPCI_IO(hw), &features, 4,
179 VIRTIO_PCI_GUEST_FEATURES);
183 legacy_get_status(struct virtio_hw *hw)
187 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_STATUS);
192 legacy_set_status(struct virtio_hw *hw, uint8_t status)
194 rte_pci_ioport_write(VTPCI_IO(hw), &status, 1, VIRTIO_PCI_STATUS);
198 legacy_reset(struct virtio_hw *hw)
200 legacy_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
204 legacy_get_isr(struct virtio_hw *hw)
208 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_ISR);
212 /* Enable one vector (0) for Link State Intrerrupt */
214 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
218 rte_pci_ioport_write(VTPCI_IO(hw), &vec, 2, VIRTIO_MSI_CONFIG_VECTOR);
219 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_MSI_CONFIG_VECTOR);
224 legacy_set_queue_irq(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec)
228 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
229 VIRTIO_PCI_QUEUE_SEL);
230 rte_pci_ioport_write(VTPCI_IO(hw), &vec, 2, VIRTIO_MSI_QUEUE_VECTOR);
231 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_MSI_QUEUE_VECTOR);
236 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
240 rte_pci_ioport_write(VTPCI_IO(hw), &queue_id, 2, VIRTIO_PCI_QUEUE_SEL);
241 rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_PCI_QUEUE_NUM);
246 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
250 if (!check_vq_phys_addr_ok(vq))
253 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
254 VIRTIO_PCI_QUEUE_SEL);
255 src = vq->vq_ring_mem >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
256 rte_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
262 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
266 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
267 VIRTIO_PCI_QUEUE_SEL);
268 rte_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
272 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
274 rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
275 VIRTIO_PCI_QUEUE_NOTIFY);
278 const struct virtio_pci_ops legacy_ops = {
279 .read_dev_cfg = legacy_read_dev_config,
280 .write_dev_cfg = legacy_write_dev_config,
281 .reset = legacy_reset,
282 .get_status = legacy_get_status,
283 .set_status = legacy_set_status,
284 .get_features = legacy_get_features,
285 .set_features = legacy_set_features,
286 .get_isr = legacy_get_isr,
287 .set_config_irq = legacy_set_config_irq,
288 .set_queue_irq = legacy_set_queue_irq,
289 .get_queue_num = legacy_get_queue_num,
290 .setup_queue = legacy_setup_queue,
291 .del_queue = legacy_del_queue,
292 .notify_queue = legacy_notify_queue,
296 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
298 rte_write32(val & ((1ULL << 32) - 1), lo);
299 rte_write32(val >> 32, hi);
303 modern_read_dev_config(struct virtio_hw *hw, size_t offset,
304 void *dst, int length)
308 uint8_t old_gen, new_gen;
311 old_gen = rte_read8(&hw->common_cfg->config_generation);
314 for (i = 0; i < length; i++)
315 *p++ = rte_read8((uint8_t *)hw->dev_cfg + offset + i);
317 new_gen = rte_read8(&hw->common_cfg->config_generation);
318 } while (old_gen != new_gen);
322 modern_write_dev_config(struct virtio_hw *hw, size_t offset,
323 const void *src, int length)
326 const uint8_t *p = src;
328 for (i = 0; i < length; i++)
329 rte_write8((*p++), (((uint8_t *)hw->dev_cfg) + offset + i));
333 modern_get_features(struct virtio_hw *hw)
335 uint32_t features_lo, features_hi;
337 rte_write32(0, &hw->common_cfg->device_feature_select);
338 features_lo = rte_read32(&hw->common_cfg->device_feature);
340 rte_write32(1, &hw->common_cfg->device_feature_select);
341 features_hi = rte_read32(&hw->common_cfg->device_feature);
343 return ((uint64_t)features_hi << 32) | features_lo;
347 modern_set_features(struct virtio_hw *hw, uint64_t features)
349 rte_write32(0, &hw->common_cfg->guest_feature_select);
350 rte_write32(features & ((1ULL << 32) - 1),
351 &hw->common_cfg->guest_feature);
353 rte_write32(1, &hw->common_cfg->guest_feature_select);
354 rte_write32(features >> 32,
355 &hw->common_cfg->guest_feature);
359 modern_get_status(struct virtio_hw *hw)
361 return rte_read8(&hw->common_cfg->device_status);
365 modern_set_status(struct virtio_hw *hw, uint8_t status)
367 rte_write8(status, &hw->common_cfg->device_status);
371 modern_reset(struct virtio_hw *hw)
373 modern_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
374 modern_get_status(hw);
378 modern_get_isr(struct virtio_hw *hw)
380 return rte_read8(hw->isr);
384 modern_set_config_irq(struct virtio_hw *hw, uint16_t vec)
386 rte_write16(vec, &hw->common_cfg->msix_config);
387 return rte_read16(&hw->common_cfg->msix_config);
391 modern_set_queue_irq(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec)
393 rte_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
394 rte_write16(vec, &hw->common_cfg->queue_msix_vector);
395 return rte_read16(&hw->common_cfg->queue_msix_vector);
399 modern_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
401 rte_write16(queue_id, &hw->common_cfg->queue_select);
402 return rte_read16(&hw->common_cfg->queue_size);
406 modern_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
408 uint64_t desc_addr, avail_addr, used_addr;
411 if (!check_vq_phys_addr_ok(vq))
414 desc_addr = vq->vq_ring_mem;
415 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
416 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
417 ring[vq->vq_nentries]),
418 VIRTIO_PCI_VRING_ALIGN);
420 rte_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
422 io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
423 &hw->common_cfg->queue_desc_hi);
424 io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
425 &hw->common_cfg->queue_avail_hi);
426 io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
427 &hw->common_cfg->queue_used_hi);
429 notify_off = rte_read16(&hw->common_cfg->queue_notify_off);
430 vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
431 notify_off * hw->notify_off_multiplier);
433 rte_write16(1, &hw->common_cfg->queue_enable);
435 PMD_INIT_LOG(DEBUG, "queue %u addresses:", vq->vq_queue_index);
436 PMD_INIT_LOG(DEBUG, "\t desc_addr: %" PRIx64, desc_addr);
437 PMD_INIT_LOG(DEBUG, "\t aval_addr: %" PRIx64, avail_addr);
438 PMD_INIT_LOG(DEBUG, "\t used_addr: %" PRIx64, used_addr);
439 PMD_INIT_LOG(DEBUG, "\t notify addr: %p (notify offset: %u)",
440 vq->notify_addr, notify_off);
446 modern_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
448 rte_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
450 io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
451 &hw->common_cfg->queue_desc_hi);
452 io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
453 &hw->common_cfg->queue_avail_hi);
454 io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
455 &hw->common_cfg->queue_used_hi);
457 rte_write16(0, &hw->common_cfg->queue_enable);
461 modern_notify_queue(struct virtio_hw *hw __rte_unused, struct virtqueue *vq)
463 rte_write16(vq->vq_queue_index, vq->notify_addr);
466 const struct virtio_pci_ops modern_ops = {
467 .read_dev_cfg = modern_read_dev_config,
468 .write_dev_cfg = modern_write_dev_config,
469 .reset = modern_reset,
470 .get_status = modern_get_status,
471 .set_status = modern_set_status,
472 .get_features = modern_get_features,
473 .set_features = modern_set_features,
474 .get_isr = modern_get_isr,
475 .set_config_irq = modern_set_config_irq,
476 .set_queue_irq = modern_set_queue_irq,
477 .get_queue_num = modern_get_queue_num,
478 .setup_queue = modern_setup_queue,
479 .del_queue = modern_del_queue,
480 .notify_queue = modern_notify_queue,
485 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
486 void *dst, int length)
488 VTPCI_OPS(hw)->read_dev_cfg(hw, offset, dst, length);
492 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
493 const void *src, int length)
495 VTPCI_OPS(hw)->write_dev_cfg(hw, offset, src, length);
499 vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features)
504 * Limit negotiated features to what the driver, virtqueue, and
507 features = host_features & hw->guest_features;
508 VTPCI_OPS(hw)->set_features(hw, features);
514 vtpci_reset(struct virtio_hw *hw)
516 VTPCI_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
517 /* flush status write */
518 VTPCI_OPS(hw)->get_status(hw);
522 vtpci_reinit_complete(struct virtio_hw *hw)
524 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
528 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
530 if (status != VIRTIO_CONFIG_STATUS_RESET)
531 status |= VTPCI_OPS(hw)->get_status(hw);
533 VTPCI_OPS(hw)->set_status(hw, status);
537 vtpci_get_status(struct virtio_hw *hw)
539 return VTPCI_OPS(hw)->get_status(hw);
543 vtpci_isr(struct virtio_hw *hw)
545 return VTPCI_OPS(hw)->get_isr(hw);
549 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
551 uint8_t bar = cap->bar;
552 uint32_t length = cap->length;
553 uint32_t offset = cap->offset;
557 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
561 if (offset + length < offset) {
562 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
567 if (offset + length > dev->mem_resource[bar].len) {
569 "invalid cap: overflows bar space: %u > %" PRIu64,
570 offset + length, dev->mem_resource[bar].len);
574 base = dev->mem_resource[bar].addr;
576 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
580 return base + offset;
583 #define PCI_MSIX_ENABLE 0x8000
586 virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
589 struct virtio_pci_cap cap;
592 if (rte_pci_map_device(dev)) {
593 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
597 ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
599 PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
604 ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
607 "failed to read pci cap at pos: %x", pos);
611 if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
612 /* Transitional devices would also have this capability,
613 * that's why we also check if msix is enabled.
614 * 1st byte is cap ID; 2nd byte is the position of next
615 * cap; next two bytes are the flags.
617 uint16_t flags = ((uint16_t *)&cap)[1];
619 if (flags & PCI_MSIX_ENABLE)
623 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
625 "[%2x] skipping non VNDR cap id: %02x",
631 "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
632 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
634 switch (cap.cfg_type) {
635 case VIRTIO_PCI_CAP_COMMON_CFG:
636 hw->common_cfg = get_cfg_addr(dev, &cap);
638 case VIRTIO_PCI_CAP_NOTIFY_CFG:
639 rte_pci_read_config(dev, &hw->notify_off_multiplier,
640 4, pos + sizeof(cap));
641 hw->notify_base = get_cfg_addr(dev, &cap);
643 case VIRTIO_PCI_CAP_DEVICE_CFG:
644 hw->dev_cfg = get_cfg_addr(dev, &cap);
646 case VIRTIO_PCI_CAP_ISR_CFG:
647 hw->isr = get_cfg_addr(dev, &cap);
655 if (hw->common_cfg == NULL || hw->notify_base == NULL ||
656 hw->dev_cfg == NULL || hw->isr == NULL) {
657 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
661 PMD_INIT_LOG(INFO, "found modern virtio pci device.");
663 PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", hw->common_cfg);
664 PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", hw->dev_cfg);
665 PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", hw->isr);
666 PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
667 hw->notify_base, hw->notify_off_multiplier);
674 * if there is error mapping with VFIO/UIO.
675 * if port map error when driver type is KDRV_NONE.
676 * if whitelisted but driver type is KDRV_UNKNOWN.
677 * Return 1 if kernel driver is managing the device.
678 * Return 0 on success.
681 vtpci_init(struct rte_pci_device *dev, struct virtio_hw *hw)
684 * Try if we can succeed reading virtio pci caps, which exists
685 * only on modern pci device. If failed, we fallback to legacy
688 if (virtio_read_caps(dev, hw) == 0) {
689 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
690 virtio_hw_internal[hw->port_id].vtpci_ops = &modern_ops;
695 PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
696 if (rte_pci_ioport_map(dev, 0, VTPCI_IO(hw)) < 0) {
697 if (dev->kdrv == RTE_KDRV_UNKNOWN &&
698 (!dev->device.devargs ||
699 dev->device.devargs->bus !=
700 rte_bus_find_by_name("pci"))) {
702 "skip kernel managed virtio device.");
708 virtio_hw_internal[hw->port_id].vtpci_ops = &legacy_ops;