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 d5c19a1e09fac85f7bb11ce5ccd5d66cf3aa44d7
Author: Bowen Wang <[email protected]>
AuthorDate: Tue Oct 27 23:44:34 2026 +0800

    drivers/devicetree: find ivshmem-flat devices in the device tree
    
    virt-m describes the ivshmem-flat devices it puts on the platform bus
    in the device tree, under "qemu,ivshmem-flat" nodes. Add
    fdt_ivshmem_flat_devices_register() to register them.
    
    Their reg is relative to the platform bus, which fdt_get_reg_base*()
    returns as is, so translate it through the parent's ranges.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/devicetree/CMakeLists.txt     |   4 +
 drivers/devicetree/Make.defs          |   4 +
 drivers/devicetree/fdt_ivshmem_flat.c | 141 ++++++++++++++++++++++++++++++++++
 include/nuttx/fdt.h                   |  23 ++++++
 4 files changed, 172 insertions(+)

diff --git a/drivers/devicetree/CMakeLists.txt 
b/drivers/devicetree/CMakeLists.txt
index 3ae58751f7f..7359696d51e 100644
--- a/drivers/devicetree/CMakeLists.txt
+++ b/drivers/devicetree/CMakeLists.txt
@@ -34,6 +34,10 @@ if(CONFIG_DEVICE_TREE)
     list(APPEND SRCS fdt_cfi.c)
   endif()
 
+  if(CONFIG_IVSHMEM_FLAT)
+    list(APPEND SRCS fdt_ivshmem_flat.c)
+  endif()
+
   if(CONFIG_TIMER_SP804 AND CONFIG_TIMER)
     list(APPEND SRCS fdt_sp804.c)
   endif()
diff --git a/drivers/devicetree/Make.defs b/drivers/devicetree/Make.defs
index d0da776ebcc..5bedb967ef4 100644
--- a/drivers/devicetree/Make.defs
+++ b/drivers/devicetree/Make.defs
@@ -36,6 +36,10 @@ ifeq ($(CONFIG_MTD_CFI),y)
   CSRCS += fdt_cfi.c
 endif
 
+ifeq ($(CONFIG_IVSHMEM_FLAT),y)
+  CSRCS += fdt_ivshmem_flat.c
+endif
+
 ifeq ($(CONFIG_TIMER_SP804),y)
 ifeq ($(CONFIG_TIMER),y)
   CSRCS += fdt_sp804.c
diff --git a/drivers/devicetree/fdt_ivshmem_flat.c 
b/drivers/devicetree/fdt_ivshmem_flat.c
new file mode 100644
index 00000000000..47b16bd32c9
--- /dev/null
+++ b/drivers/devicetree/fdt_ivshmem_flat.c
@@ -0,0 +1,141 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_ivshmem_flat.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+
+#include <nuttx/fdt.h>
+#include <nuttx/ivshmem_flat.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_ivshmem_flat_reg
+ *
+ * Description:
+ *   Return reg[index] of the node as a CPU address, translated through the
+ *   ranges of its parent, e.g. the QEMU platform bus. Returns 0 when no
+ *   range covers it.
+ *
+ ****************************************************************************/
+
+static uintptr_t fdt_ivshmem_flat_reg(FAR const void *fdt, int offset,
+                                      int index)
+{
+  FAR const fdt32_t *ranges;
+  uintptr_t addr;
+  uintptr_t child;
+  uintptr_t size;
+  int parent;
+  int len;
+  int na;
+  int pna;
+  int ns;
+
+  addr   = fdt_get_reg_base_by_index(fdt, offset, index);
+  parent = fdt_parent_offset(fdt, offset);
+  ranges = fdt_getprop(fdt, parent, "ranges", &len);
+  if (ranges == NULL || len == 0)
+    {
+      return addr;
+    }
+
+  na  = fdt_address_cells(fdt, parent);
+  ns  = fdt_size_cells(fdt, parent);
+  pna = fdt_address_cells(fdt, fdt_parent_offset(fdt, parent));
+
+  for (len /= sizeof(fdt32_t); len >= na + pna + ns;
+       len -= na + pna + ns, ranges += na + pna + ns)
+    {
+      child = fdt_ld_by_cells(ranges, na);
+      size  = fdt_ld_by_cells(ranges + na + pna, ns);
+      if (addr >= child && addr - child < size)
+        {
+          return fdt_ld_by_cells(ranges + na, pna) + addr - child;
+        }
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_ivshmem_flat_devices_register
+ ****************************************************************************/
+
+int fdt_ivshmem_flat_devices_register(FAR const void *fdt, int irqbase)
+{
+  struct ivshmem_flat_config_s config;
+  uint32_t id;
+  int offset = -1;
+  int ret;
+
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset,
+                                             "qemu,ivshmem-flat");
+      if (offset == -FDT_ERR_NOTFOUND)
+        {
+          break;
+        }
+
+      config.reg        = fdt_ivshmem_flat_reg(fdt, offset, 0);
+      config.shmem      = fdt_ivshmem_flat_reg(fdt, offset, 1);
+      config.shmem_size = fdt_get_reg_size_by_index(fdt, offset, 1);
+      if (config.reg == 0 || config.shmem == 0 || config.shmem_size == 0)
+        {
+          return -EINVAL;
+        }
+
+      /* No "interrupts" means no peer notification (memdev) */
+
+      config.irq = fdt_get_irq(fdt, offset, 1, irqbase);
+
+      /* "ivshmem-id" keeps the numbering independent of the order of
+       * the -device options.
+       */
+
+      config.id = -1;
+      if (fdt_load_prop_u32(fdt, offset, "ivshmem-id", 0, &id) == OK)
+        {
+          config.id = id;
+        }
+
+      ret = ivshmem_flat_register(&config);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  return OK;
+}
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index 5e00b531e73..3d3c3832a7e 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -511,6 +511,29 @@ int fdt_virtio_mmio_devices_register(FAR const void *fdt, 
int irqbase);
 int fdt_cfi_register(FAR const void *fdt);
 #endif
 
+/****************************************************************************
+ * Name: fdt_ivshmem_flat_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "qemu,ivshmem-flat" and registers each via ivshmem_flat_register().
+ *   reg[0] is the register block, reg[1] the shared memory. A node
+ *   without "interrupts" has no doorbell; "ivshmem-id", if present, is
+ *   used as the ivshmem bus id.
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IVSHMEM_FLAT
+int fdt_ivshmem_flat_devices_register(FAR const void *fdt, int irqbase);
+#endif
+
 /****************************************************************************
  * Name: fdt_sp804_devices_register
  *

Reply via email to