virtio-pci: add virtio pci legacy support

Follow the virtio spec, support the virtio pci Legacy

Signed-off-by: wangyongrong <wangyongrong@xiaomi.com>
Signed-off-by: andi <andi6@xiaomi.com>
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
This commit is contained in:
wangyongrong
2024-05-08 16:52:44 +08:00
committed by Xiang Xiao
parent decfc9ad65
commit d54d07b62c
5 changed files with 841 additions and 40 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ if(CONFIG_DRIVERS_VIRTIO_MMIO)
endif()
if(CONFIG_DRIVERS_VIRTIO_PCI)
list(APPEND SRCS virtio-pci.c)
list(APPEND SRCS virtio-pci-legacy.c virtio-pci-modern.c)
endif()
if(CONFIG_DRIVERS_VIRTIO_BLK)
+1 -1
View File
@@ -29,7 +29,7 @@ ifeq ($(CONFIG_DRIVERS_VIRTIO_MMIO),y)
endif
ifeq ($(CONFIG_DRIVERS_VIRTIO_PCI),y)
CSRCS += virtio-pci.c
CSRCS += virtio-pci-legacy.c virtio-pci-modern.c
endif
ifeq ($(CONFIG_DRIVERS_VIRTIO_BLK),y)
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,5 @@
/****************************************************************************
* drivers/virtio/virtio-pci.c
* drivers/virtio/virtio-pci-modern.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -22,29 +22,19 @@
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <errno.h>
#include <stdint.h>
#include <sys/param.h>
#include <nuttx/pci/pci.h>
#include <nuttx/virtio/virtio.h>
#include <nuttx/virtio/virtio-pci.h>
#include "virtio-pci.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define VIRTIO_PCI_VRING_ALIGN (1 << 12)
#define VIRTIO_PCI_MSI_NO_VECTOR 0xffff
#define VIRTIO_PCI_INT_CFG 0
#define VIRTIO_PCI_INT_VQ 1
#define VIRTIO_PCI_INT_NUM 2
/* Common configuration */
#define VIRTIO_PCI_CAP_COMMON_CFG 1
@@ -161,27 +151,6 @@ begin_packed_struct struct virtio_pci_cfg_cap_s
uint8_t pci_cfg_data[4]; /* Data for BAR access. */
} end_packed_struct;
struct virtio_pci_device_s
{
struct virtio_device vdev; /* Virtio deivce */
FAR struct pci_device_s *dev; /* PCI device */
metal_phys_addr_t shm_phy;
struct metal_io_region shm_io; /* Share memory io region, virtqueue
* use this io.
*/
FAR void *common;
size_t common_len;
FAR void *notify;
size_t notify_len;
uint32_t notify_off_multiplier;
FAR void *device;
size_t device_len;
int irq[VIRTIO_PCI_INT_NUM];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@@ -596,8 +565,7 @@ static void virtio_pci_delete_virtqueues(FAR struct virtio_device *vdev)
{
vrinfo = &vdev->vrings_info[i];
pci_write_io_word(vpdev->dev, &cfg->queue_select,
vrinfo->vq->vq_queue_index);
pci_write_io_word(vpdev->dev, &cfg->queue_select, i);
pci_write_io_word(vpdev->dev, &cfg->queue_msix_vector,
VIRTIO_PCI_MSI_NO_VECTOR);
@@ -943,7 +911,7 @@ static int virtio_pci_init_device(FAR struct virtio_pci_device_s *vpdev)
* Name: virtio_pci_probe
****************************************************************************/
static int virtio_pci_probe(FAR struct pci_device_s *dev)
static int virtio_pci_modern_probe(FAR struct pci_device_s *dev)
{
FAR struct virtio_pci_device_s *vpdev;
FAR struct virtio_device *vdev;
@@ -986,7 +954,11 @@ static int virtio_pci_probe(FAR struct pci_device_s *dev)
ret = virtio_pci_init_device(vpdev);
if (ret < 0)
{
vrterr("Virtio pci device init failed, ret=%d\n", ret);
if (ret != -ENODEV)
{
vrterr("Virtio pci modern device init failed, ret=%d\n", ret);
}
goto err_with_enable;
}
@@ -1039,6 +1011,23 @@ err:
return ret;
}
/****************************************************************************
* Name: virtio_pci_probe
****************************************************************************/
static int virtio_pci_probe(FAR struct pci_device_s *dev)
{
int ret;
ret = virtio_pci_modern_probe(dev);
if (ret == -ENODEV)
{
ret = virtio_pci_legacy_probe(dev);
}
return ret;
}
/****************************************************************************
* Name: virtio_pci_remove
****************************************************************************/
+101
View File
@@ -0,0 +1,101 @@
/****************************************************************************
* drivers/virtio/virtio-pci.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __DRIVERS_VIRTIO_VIRTIO_PCI_H
#define __DRIVERS_VIRTIO_VIRTIO_PCI_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_DRIVERS_VIRTIO_PCI
#include <nuttx/pci/pci.h>
#include <nuttx/virtio/virtio.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define VIRTIO_PCI_VRING_ALIGN (1 << 12)
#define VIRTIO_PCI_MSI_NO_VECTOR 0xffff
#define VIRTIO_PCI_INT_CFG 0
#define VIRTIO_PCI_INT_VQ 1
#define VIRTIO_PCI_INT_NUM 2
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Types
****************************************************************************/
struct virtio_pci_device_s
{
struct virtio_device vdev; /* Virtio deivce */
FAR struct pci_device_s *dev; /* PCI device */
metal_phys_addr_t shm_phy;
struct metal_io_region shm_io; /* Share memory io region,
* virtqueue use this io.
*/
int irq[VIRTIO_PCI_INT_NUM];
/* for modern */
FAR void *common;
size_t common_len;
FAR void *notify;
size_t notify_len;
uint32_t notify_off_multiplier;
FAR void *device;
size_t device_len;
/* for legacy */
FAR void *ioaddr;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int virtio_pci_legacy_probe(FAR struct pci_device_s *dev);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_DRIVERS_VIRTIO_PCI */
#endif /* __DRIVERS_VIRTIO_VIRTIO_PCI_H */