This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1736898eace2d13d3a35d8f347f8520f2068b676
Author: Bowen Wang <[email protected]>
AuthorDate: Thu Oct 22 13:18:40 2026 +0800

    virtio: implement get_shm_region for mmio and pci modern
    
    virtio-mmio reads SHMSel/SHMLen/SHMBase, available since version 2;
    a length of all ones means the region does not exist. The base is
    a physical address, mapped through the metal io region.
    
    virtio-pci forwards to a new virtio_pci_ops_s hook. Modern looks up
    the VIRTIO_PCI_CAP_SHARED_MEMORY_CFG capability with the matching id
    and maps the BAR range with pci_map_bar_region(), since the BAR
    resource is a bus address. Legacy has no shared memory regions and
    leaves the hook NULL.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/virtio/virtio-mmio.c       | 63 ++++++++++++++++++++++----
 drivers/virtio/virtio-pci-modern.c | 92 ++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio-pci.c        | 39 ++++++++++++----
 drivers/virtio/virtio-pci.h        |  3 ++
 4 files changed, 181 insertions(+), 16 deletions(-)

diff --git a/drivers/virtio/virtio-mmio.c b/drivers/virtio/virtio-mmio.c
index 66aea890c50..679c8ab0d31 100644
--- a/drivers/virtio/virtio-mmio.c
+++ b/drivers/virtio/virtio-mmio.c
@@ -204,6 +204,9 @@ static FAR void *virtio_mmio_alloc_buf(FAR struct 
virtio_device *vdev,
                                        size_t size, size_t align);
 static void virtio_mmio_free_buf(FAR struct virtio_device *vdev,
                                  FAR void *buf);
+static int virtio_mmio_get_shm_region(FAR struct virtio_device *vdev,
+                                      uint8_t id,
+                                      FAR struct virtio_shm_region *region);
 static int virtio_mmio_init_device(FAR struct virtio_mmio_device_s *vmdev,
                                    FAR void *regs, int irq);
 
@@ -262,8 +265,8 @@ static const struct virtio_dispatch g_virtio_mmio_dispatch =
 
 static const struct virtio_memory_ops g_virtio_mmio_mmops =
 {
-  .alloc        = virtio_mmio_alloc_buf,
-  .free         = virtio_mmio_free_buf,
+  .alloc          = virtio_mmio_alloc_buf,
+  .free           = virtio_mmio_free_buf,
 
   /* The host (QEMU) can access arbitrary guest memory, so buffers are used
    * as is (zero copy), except user buffers of a per-process address
@@ -271,14 +274,15 @@ static const struct virtio_memory_ops g_virtio_mmio_mmops 
=
    */
 
 #ifdef CONFIG_ARCH_ADDRENV
-  .free_largest = virtio_addrenv_free_largest,
-  .map          = virtio_addrenv_map,
-  .unmap        = virtio_addrenv_unmap,
+  .free_largest   = virtio_addrenv_free_largest,
+  .map            = virtio_addrenv_map,
+  .unmap          = virtio_addrenv_unmap,
 #else
-  .free_largest = NULL,
-  .map          = NULL,
-  .unmap        = NULL,
+  .free_largest   = NULL,
+  .map            = NULL,
+  .unmap          = NULL,
 #endif
+  .get_shm_region = virtio_mmio_get_shm_region,
 };
 
 /****************************************************************************
@@ -808,6 +812,49 @@ static void virtio_mmio_free_buf(FAR struct virtio_device 
*vdev,
   kmm_free(buf);
 }
 
+/****************************************************************************
+ * Name: virtio_mmio_get_shm_region
+ ****************************************************************************/
+
+static int virtio_mmio_get_shm_region(FAR struct virtio_device *vdev,
+                                      uint8_t id,
+                                      FAR struct virtio_shm_region *region)
+{
+  FAR struct virtio_mmio_device_s *vmdev =
+    (FAR struct virtio_mmio_device_s *)vdev;
+  uint64_t len;
+  uint64_t base;
+
+  /* The shared memory registers only exist since version 2 */
+
+  if (vdev->id.version == VIRTIO_MMIO_VERSION_1)
+    {
+      return -ENOENT;
+    }
+
+  metal_io_write32(&vmdev->cfg_io, VIRTIO_MMIO_SHM_SEL, id);
+
+  len = metal_io_read32(&vmdev->cfg_io, VIRTIO_MMIO_SHM_LEN_LOW);
+  len |= (uint64_t)metal_io_read32(&vmdev->cfg_io,
+                                   VIRTIO_MMIO_SHM_LEN_HIGH) << 32;
+
+  /* A length of all ones marks a region that does not exist */
+
+  if (len == UINT64_MAX)
+    {
+      return -ENOENT;
+    }
+
+  base = metal_io_read32(&vmdev->cfg_io, VIRTIO_MMIO_SHM_BASE_LOW);
+  base |= (uint64_t)metal_io_read32(&vmdev->cfg_io,
+                                    VIRTIO_MMIO_SHM_BASE_HIGH) << 32;
+
+  region->addr = (metal_phys_addr_t)base;
+  region->va   = metal_io_phys_to_virt(metal_io_get_region(), region->addr);
+  region->len  = (size_t)len;
+  return region->va != NULL ? OK : -ENOMEM;
+}
+
 /****************************************************************************
  * Name: virtio_mmio_init_device
  ****************************************************************************/
diff --git a/drivers/virtio/virtio-pci-modern.c 
b/drivers/virtio/virtio-pci-modern.c
index d748966ed5e..4bb026fe747 100644
--- a/drivers/virtio/virtio-pci-modern.c
+++ b/drivers/virtio/virtio-pci-modern.c
@@ -166,6 +166,10 @@ virtio_pci_modern_create_virtqueue(FAR struct 
virtio_pci_device_s *vpdev,
 static void
 virtio_pci_modern_delete_virtqueue(FAR struct virtio_device *vdev,
                                    int index);
+static int
+virtio_pci_modern_get_shm_region(FAR struct virtio_pci_device_s *vpdev,
+                                 uint8_t id,
+                                 FAR struct virtio_shm_region *region);
 static void virtio_pci_modern_set_status(FAR struct virtio_device *vdev,
                                          uint8_t status);
 static uint8_t virtio_pci_modern_get_status(FAR struct virtio_device *vdev);
@@ -205,6 +209,7 @@ static const struct virtio_pci_ops_s 
g_virtio_pci_modern_ops =
   virtio_pci_modern_get_queue_len,      /* get_queue_len */
   virtio_pci_modern_create_virtqueue,   /* create_virtqueue */
   virtio_pci_modern_delete_virtqueue,   /* delete_virtqueue */
+  virtio_pci_modern_get_shm_region,     /* get_shm_region */
 };
 
 /****************************************************************************
@@ -294,6 +299,93 @@ virtio_pci_map_capability(FAR struct virtio_pci_device_s 
*vpdev, uint8_t pos,
   return ptr;
 }
 
+/****************************************************************************
+ * Name: virtio_pci_modern_get_shm_region
+ ****************************************************************************/
+
+static int
+virtio_pci_modern_get_shm_region(FAR struct virtio_pci_device_s *vpdev,
+                                 uint8_t id,
+                                 FAR struct virtio_shm_region *region)
+{
+  FAR struct pci_device_s *dev = vpdev->dev;
+  uint64_t offset;
+  uint64_t length;
+  uint32_t tmp;
+  uint8_t type;
+  uint8_t capid;
+  uint8_t bar;
+  uint8_t pos;
+
+  /* Several shared memory capabilities may exist, so match on the id too,
+   * which virtio_pci_find_capability() does not do.
+   */
+
+  for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
+       pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR))
+    {
+      pci_read_config_byte(dev,
+                           pos + offsetof(struct virtio_pci_cap_s, cfg_type),
+                           &type);
+      pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap_s, id),
+                           &capid);
+      if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG || capid != id)
+        {
+          continue;
+        }
+
+      pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap_s, bar),
+                           &bar);
+      if (bar >= PCI_STD_NUM_BARS)
+        {
+          continue;
+        }
+
+      pci_read_config_dword(dev,
+                            pos + offsetof(struct virtio_pci_cap_s, offset),
+                            &tmp);
+      offset = tmp;
+      pci_read_config_dword(dev,
+                            pos + offsetof(struct virtio_pci_cap64_s,
+                                           offset_hi), &tmp);
+      offset |= (uint64_t)tmp << 32;
+
+      pci_read_config_dword(dev,
+                            pos + offsetof(struct virtio_pci_cap_s, length),
+                            &tmp);
+      length = tmp;
+      pci_read_config_dword(dev,
+                            pos + offsetof(struct virtio_pci_cap64_s,
+                                           length_hi), &tmp);
+      length |= (uint64_t)tmp << 32;
+
+      if (offset + length < offset ||
+          offset + length > pci_resource_len(dev, bar))
+        {
+          vrterr("Shm region %u exceeds bar %u\n", id, bar);
+          return -EINVAL;
+        }
+
+      /* The BAR resource is a bus address, so let the PCI layer map it
+       * and derive the physical address from the CPU mapping.
+       */
+
+      region->va = pci_map_bar_region(dev, bar, offset, length);
+      if (region->va == NULL)
+        {
+          vrterr("Shm region %u map failed\n", id);
+          return -ENOMEM;
+        }
+
+      region->addr = metal_io_virt_to_phys(metal_io_get_region(),
+                                           region->va);
+      region->len  = (size_t)length;
+      return OK;
+    }
+
+  return -ENOENT;
+}
+
 /****************************************************************************
  * Name: virtio_pci_modern_get_queue_len
  ****************************************************************************/
diff --git a/drivers/virtio/virtio-pci.c b/drivers/virtio/virtio-pci.c
index 92c8896345d..e43fd323443 100644
--- a/drivers/virtio/virtio-pci.c
+++ b/drivers/virtio/virtio-pci.c
@@ -53,6 +53,9 @@ static FAR void *virtio_pci_alloc_buf(FAR struct 
virtio_device *vdev,
                                       size_t size, size_t align);
 static void virtio_pci_free_buf(FAR struct virtio_device *vdev,
                                 FAR void *buf);
+static int virtio_pci_get_shm_region(FAR struct virtio_device *vdev,
+                                     uint8_t id,
+                                     FAR struct virtio_shm_region *region);
 
 static int virtio_pci_probe(FAR struct pci_device_s *dev);
 static void virtio_pci_remove(FAR struct pci_device_s *dev);
@@ -81,8 +84,8 @@ static DEFINE_PER_CPU_BMP(struct pci_driver_s, 
g_virtio_pci_drv) =
 
 static const struct virtio_memory_ops g_virtio_pci_mmops =
 {
-  .alloc        = virtio_pci_alloc_buf,
-  .free         = virtio_pci_free_buf,
+  .alloc          = virtio_pci_alloc_buf,
+  .free           = virtio_pci_free_buf,
 
   /* The host (QEMU) can access arbitrary guest memory, so buffers are used
    * as is (zero copy), except user buffers of a per-process address
@@ -90,14 +93,15 @@ static const struct virtio_memory_ops g_virtio_pci_mmops =
    */
 
 #ifdef CONFIG_ARCH_ADDRENV
-  .free_largest = virtio_addrenv_free_largest,
-  .map          = virtio_addrenv_map,
-  .unmap        = virtio_addrenv_unmap,
+  .free_largest   = virtio_addrenv_free_largest,
+  .map            = virtio_addrenv_map,
+  .unmap          = virtio_addrenv_unmap,
 #else
-  .free_largest = NULL,
-  .map          = NULL,
-  .unmap        = NULL,
+  .free_largest   = NULL,
+  .map            = NULL,
+  .unmap          = NULL,
 #endif
+  .get_shm_region = virtio_pci_get_shm_region,
 };
 
 /****************************************************************************
@@ -183,6 +187,25 @@ static void virtio_pci_free_buf(FAR struct virtio_device 
*vdev,
   return kmm_free(buf);
 }
 
+/****************************************************************************
+ * Name: virtio_pci_get_shm_region
+ ****************************************************************************/
+
+static int virtio_pci_get_shm_region(FAR struct virtio_device *vdev,
+                                     uint8_t id,
+                                     FAR struct virtio_shm_region *region)
+{
+  FAR struct virtio_pci_device_s *vpdev =
+    (FAR struct virtio_pci_device_s *)vdev;
+
+  if (vpdev->ops->get_shm_region == NULL)
+    {
+      return -ENOENT;
+    }
+
+  return vpdev->ops->get_shm_region(vpdev, id, region);
+}
+
 /****************************************************************************
  * Name: virtio_pci_probe
  ****************************************************************************/
diff --git a/drivers/virtio/virtio-pci.h b/drivers/virtio/virtio-pci.h
index d83d5ca07e8..b650b54eeb2 100644
--- a/drivers/virtio/virtio-pci.h
+++ b/drivers/virtio/virtio-pci.h
@@ -67,6 +67,9 @@ struct virtio_pci_ops_s
   CODE int (*create_virtqueue)(FAR struct virtio_pci_device_s *vpdev,
                                FAR struct virtqueue *vq);
   CODE void (*delete_virtqueue)(FAR struct virtio_device *vdev, int index);
+  CODE int (*get_shm_region)(FAR struct virtio_pci_device_s *vpdev,
+                             uint8_t id,
+                             FAR struct virtio_shm_region *region);
 };
 
 struct virtio_pci_device_s

Reply via email to