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

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


The following commit(s) were added to refs/heads/master by this push:
     new 10869f3a747 drivers/vhost: add the vhost (device-side) SPI backend
10869f3a747 is described below

commit 10869f3a747176332706424d5f879045d0438437
Author: p-szafonimateusz <[email protected]>
AuthorDate: Sun Nov 15 08:59:48 2026 +0800

    drivers/vhost: add the vhost (device-side) SPI backend
    
    Add the device side of VirtIO SPI (device id 45). When an rptun
    resource table exposes a VirtIO SPI device in the device role, the
    driver opens the local SPI device node named in the device config
    space, decodes each request in the Linux-compatible VirtIO SPI wire
    format and runs it with SPIIOC_TRANSFER. Requests are processed on a
    work queue.
    
    Signed-off-by: p-szafonimateusz <[email protected]>
    Signed-off-by: yangsong8 <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/vhost/CMakeLists.txt |   4 +
 drivers/vhost/Kconfig        |  12 ++
 drivers/vhost/Make.defs      |   4 +
 drivers/vhost/vhost-spi.c    | 413 +++++++++++++++++++++++++++++++++++++++++++
 drivers/vhost/vhost-spi.h    |  63 +++++++
 drivers/vhost/vhost.c        |   9 +
 6 files changed, 505 insertions(+)

diff --git a/drivers/vhost/CMakeLists.txt b/drivers/vhost/CMakeLists.txt
index 77fffc2f511..b7aedda86d8 100644
--- a/drivers/vhost/CMakeLists.txt
+++ b/drivers/vhost/CMakeLists.txt
@@ -37,6 +37,10 @@ if(CONFIG_DRIVERS_VHOST_I2C)
   list(APPEND SRCS vhost-i2c.c)
 endif()
 
+if(CONFIG_DRIVERS_VHOST_SPI)
+  list(APPEND SRCS vhost-spi.c)
+endif()
+
 if(CONFIG_DRIVERS_VHOST_RPMB)
   list(APPEND SRCS vhost-rpmb.c)
 endif()
diff --git a/drivers/vhost/Kconfig b/drivers/vhost/Kconfig
index 16377343e9f..d8db728df32 100644
--- a/drivers/vhost/Kconfig
+++ b/drivers/vhost/Kconfig
@@ -54,6 +54,18 @@ config DRIVERS_VHOST_I2C
                one NuttX instance export an I2C bus to a remote VirtIO I2C 
frontend,
                the native-VirtIO counterpart of the RPMSG I2C server.
 
+config DRIVERS_VHOST_SPI
+       bool "Virtual Host SPI Device Support"
+       depends on SPI && SPI_EXCHANGE
+       default n
+       select DRIVERS_VHOST
+       ---help---
+               Enable the vhost (device-side) SPI backend.  When an rptun 
resource
+               table exposes a VirtIO SPI device (device ID 45) in the device 
role,
+               this driver serves the request virtqueue, decodes each transfer 
with
+               the Linux-compatible VirtIO SPI wire format, and forwards it to 
the
+               local SPI device node named in the device config space.
+
 config DRIVERS_VHOST_BLK
        bool "Virtual Host Blk Device Support"
        default n
diff --git a/drivers/vhost/Make.defs b/drivers/vhost/Make.defs
index 11f14a210de..062a2272909 100644
--- a/drivers/vhost/Make.defs
+++ b/drivers/vhost/Make.defs
@@ -38,6 +38,10 @@ ifeq ($(CONFIG_DRIVERS_VHOST_I2C),y)
   CSRCS += vhost-i2c.c
 endif
 
+ifeq ($(CONFIG_DRIVERS_VHOST_SPI),y)
+  CSRCS += vhost-spi.c
+endif
+
 ifeq ($(CONFIG_DRIVERS_VHOST_RPMB),y)
   CSRCS += vhost-rpmb.c
 endif
diff --git a/drivers/vhost/vhost-spi.c b/drivers/vhost/vhost-spi.c
new file mode 100644
index 00000000000..7741c037e72
--- /dev/null
+++ b/drivers/vhost/vhost-spi.c
@@ -0,0 +1,413 @@
+/****************************************************************************
+ * drivers/vhost/vhost-spi.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 <debug.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/endian.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/spi/spi.h>
+#include <nuttx/spi/spi_transfer.h>
+#include <nuttx/vhost/vhost.h>
+#include <nuttx/virtio/virtio-spi.h>
+#include <nuttx/wqueue.h>
+
+#include "vhost-spi.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct vhost_spi_priv_s
+{
+  FAR struct vhost_device *hdev;
+  struct file              bus;      /* Local SPI bus forwarded to */
+  struct work_s            work;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int vhost_spi_probe(FAR struct vhost_device *hdev);
+static void vhost_spi_remove(FAR struct vhost_device *hdev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct vhost_driver g_vhost_spi_driver =
+{
+  VIRTIO_ID_SPI,    /* Device id */
+  vhost_spi_probe,  /* Probe */
+  vhost_spi_remove, /* Remove */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: virtio_spi_decode
+ *
+ * Description:
+ *   Decode a VirtIO SPI request into an SPI sequence with one transfer,
+ *   mapping the CS_HIGH/LSB_FIRST wire mode flags to hardware features of
+ *   the transfer; -ENOSYS when a flag is not mappable on this build.  The
+ *   data length left in trans->nwords by vhost_spi_split is converted
+ *   from bytes to words.
+ *
+ ****************************************************************************/
+
+static int virtio_spi_decode(FAR const struct spi_transfer_head_s *head,
+                             FAR struct spi_sequence_s *seq,
+                             FAR struct spi_trans_s *trans)
+{
+  uint32_t mode = le32toh(head->mode);
+
+  if ((mode & ~(VIRTIO_SPI_CPHA | VIRTIO_SPI_CPOL |
+                VIRTIO_SPI_CS_HIGH | VIRTIO_SPI_MODE_LSB_FIRST |
+                VIRTIO_SPI_MODE_LOOP)) != 0 ||
+      head->bits_per_word == 0 || head->cs_change > 1 ||
+      !virtio_spi_bus_width_valid(head->tx_nbits) ||
+      !virtio_spi_bus_width_valid(head->rx_nbits))
+    {
+      return -EINVAL;
+    }
+
+  seq->frequency = le32toh(head->freq);
+  seq->dev = SPIDEV_USER(head->chip_select_id);
+  seq->mode = virtio_spi_nuttx_mode(mode);
+  seq->nbits = head->bits_per_word;
+#ifdef CONFIG_SPI_DELAY_CONTROL
+  seq->a = le32toh(head->cs_setup_ns);
+  seq->b = le32toh(head->cs_delay_hold_ns);
+  seq->c = le32toh(head->cs_change_delay_inactive_ns);
+  seq->i = le32toh(head->word_delay_ns);
+#endif
+
+  trans->deselect = head->cs_change != 0;
+  trans->nwords /= virtio_spi_wordsize(seq->nbits);
+
+#ifdef CONFIG_SPI_HWFEATURES
+#  ifdef CONFIG_SPI_CS_CONTROL
+  if ((mode & VIRTIO_SPI_CS_HIGH) != 0)
+    {
+      trans->hwfeat |= HWFEAT_INVERT_CS_LEVEL;
+      mode &= ~VIRTIO_SPI_CS_HIGH;
+    }
+#  endif
+
+#  ifdef CONFIG_SPI_BITORDER
+  if ((mode & VIRTIO_SPI_MODE_LSB_FIRST) != 0)
+    {
+      trans->hwfeat |= HWFEAT_LSBFIRST;
+      mode &= ~VIRTIO_SPI_MODE_LSB_FIRST;
+    }
+#  endif
+#endif
+
+  /* Any flag left unmapped (loopback always) is unsupported here. */
+
+  return (mode & (VIRTIO_SPI_CS_HIGH | VIRTIO_SPI_MODE_LSB_FIRST |
+                  VIRTIO_SPI_MODE_LOOP)) != 0 ? -ENOSYS : OK;
+}
+
+/****************************************************************************
+ * Name: vhost_spi_split
+ *
+ * Description:
+ *   Split the descriptor chain at idx into its role buffers: the head and
+ *   the optional tx buffer are device-readable, the optional rx buffer and
+ *   the result byte are device-writable.  Each role is its own descriptor,
+ *   so a frontend that maps the buffers separately (non-contiguous) works
+ *   too.  result and total are filled even on a malformed chain, so the
+ *   caller can consume it and report an error.  Returns true when the chain
+ *   is well-formed.
+ *
+ ****************************************************************************/
+
+static bool vhost_spi_split(FAR struct virtqueue *vq, FAR uint8_t *buf,
+                            uint32_t len, uint16_t idx,
+                            FAR struct spi_transfer_head_s **head,
+                            FAR struct spi_trans_s *trans,
+                            FAR struct spi_transfer_result_s **result,
+                            FAR size_t *total)
+{
+  FAR struct vring_desc *desc = vq->vq_ring.desc;
+  FAR uint8_t *rdbuf = NULL;    /* First readable descriptor is the head */
+  FAR uint8_t *wrbuf = NULL;    /* Last writable descriptor holds result */
+  size_t rdlen = 0;
+  size_t wrlen = 0;
+  size_t txlen = 0;
+  size_t rxlen = 0;
+  uint16_t i = idx;
+
+  memset(trans, 0, sizeof(*trans));
+  *result = NULL;
+  *total = 0;
+
+  for (; ; )
+    {
+      *total += len;
+
+      if ((desc[i].flags & VRING_DESC_F_WRITE) == 0)
+        {
+          /* Readable: the head, then the optional tx buffer. */
+
+          if (rdbuf == NULL)
+            {
+              rdbuf = buf;
+              rdlen = len;
+            }
+          else
+            {
+              trans->txbuffer = buf;
+              txlen = len;
+            }
+        }
+      else if (wrbuf == NULL)
+        {
+          /* First writable: held until we know if another one follows. */
+
+          wrbuf = buf;
+          wrlen = len;
+        }
+      else
+        {
+          /* A writable descriptor follows, so the one held was the rx
+           * buffer and this new one carries the result byte.
+           */
+
+          trans->rxbuffer = wrbuf;
+          rxlen = wrlen;
+          wrbuf = buf;
+          wrlen = len;
+        }
+
+      buf = virtqueue_get_next_avail_buffer(vq, i, &i, &len);
+      if (buf == NULL)
+        {
+          break;
+        }
+    }
+
+  /* The result byte is the tail of the last writable descriptor. */
+
+  if (wrbuf != NULL && wrlen >= sizeof(struct spi_transfer_result_s))
+    {
+      *result = (FAR struct spi_transfer_result_s *)
+                (wrbuf + wrlen - sizeof(struct spi_transfer_result_s));
+    }
+
+  /* Data length in bytes; virtio_spi_decode converts it to words. */
+
+  trans->nwords = MAX(txlen, rxlen);
+  *head = (FAR struct spi_transfer_head_s *)rdbuf;
+  return rdbuf != NULL && rdlen >= sizeof(struct spi_transfer_head_s) &&
+         *result != NULL;
+}
+
+/****************************************************************************
+ * Name: vhost_spi_one
+ *
+ * Description:
+ *   Split one VirtIO SPI request from its descriptor chain, decode it,
+ *   drive the local SPI lower-half, and write the result byte back.  A
+ *   malformed chain still gets its result byte written when a writable
+ *   descriptor is present.  Returns the chain length in bytes so the
+ *   caller can consume it.
+ *
+ ****************************************************************************/
+
+static size_t vhost_spi_one(FAR struct file *spi, FAR struct virtqueue *vq,
+                            FAR void *buf, uint32_t len, uint16_t idx)
+{
+  FAR struct spi_transfer_head_s *head;
+  FAR struct spi_transfer_result_s *result;
+  struct spi_sequence_s seq;
+  struct spi_trans_s trans;
+  size_t total;
+  int ret;
+
+  if (!vhost_spi_split(vq, buf, len, idx, &head, &trans, &result, &total))
+    {
+      if (result != NULL)
+        {
+          result->result = VIRTIO_SPI_PARAM_ERR;
+        }
+
+      return total;
+    }
+
+  memset(&seq, 0, sizeof(seq));
+  seq.ntrans = 1;
+  seq.trans = &trans;
+
+  ret = virtio_spi_decode(head, &seq, &trans);
+  if (ret >= 0)
+    {
+      /* cs_change=0 chains into the next request: keep CS asserted past
+       * this sequence.  The bus lock is per sequence, so chained requests
+       * are CS-continuous, not bus-exclusive.
+       */
+
+      seq.keepcs = !trans.deselect;
+      ret = file_ioctl(spi, SPIIOC_TRANSFER, &seq);
+    }
+
+  result->result = virtio_spi_errno_to_status(ret);
+  return total;
+}
+
+/****************************************************************************
+ * Name: vhost_spi_work
+ ****************************************************************************/
+
+static void vhost_spi_work(FAR void *arg)
+{
+  FAR struct vhost_spi_priv_s *priv = arg;
+  FAR struct virtqueue *vq = priv->hdev->vrings_info[0].vq;
+  FAR void *buf;
+  size_t total;
+  uint16_t idx;
+  uint32_t len;
+
+  /* Only this worker touches the virtqueue: no locking needed */
+
+  for (; ; )
+    {
+      buf = virtqueue_get_first_avail_buffer(vq, &idx, &len);
+      if (buf == NULL)
+        {
+          break;
+        }
+
+      total = vhost_spi_one(&priv->bus, vq, buf, len, idx);
+      virtqueue_add_consumed_buffer(vq, idx, total);
+    }
+
+  virtqueue_kick(vq);
+}
+
+/****************************************************************************
+ * Name: vhost_spi_handler
+ ****************************************************************************/
+
+static void vhost_spi_handler(FAR struct virtqueue *vq)
+{
+  FAR struct vhost_spi_priv_s *priv = vq->vq_dev->priv;
+
+  if (work_available(&priv->work))
+    {
+      work_queue(HPWORK, &priv->work, vhost_spi_work, priv, 0);
+    }
+}
+
+/****************************************************************************
+ * Name: vhost_spi_probe
+ ****************************************************************************/
+
+static int vhost_spi_probe(FAR struct vhost_device *hdev)
+{
+  FAR struct vhost_spi_priv_s *priv;
+  struct vhost_spi_config_s cfg;
+  FAR const char *vqnames[1];
+  vq_callback callback[1];
+  int ret;
+
+  priv = kmm_zalloc(sizeof(*priv));
+  if (priv == NULL)
+    {
+      vhosterr("No enough memory\n");
+      return -ENOMEM;
+    }
+
+  priv->hdev = hdev;
+
+  /* Open the local SPI device node named in the device config space. */
+
+  vhost_read_config(hdev, 0, &cfg, sizeof(cfg));
+  cfg.path[sizeof(cfg.path) - 1] = '\0';
+
+  ret = file_open(&priv->bus, cfg.path, O_RDWR | O_CLOEXEC);
+  if (ret < 0)
+    {
+      vhosterr("Failed to open %s: %d\n", cfg.path, ret);
+      kmm_free(priv);
+      return ret;
+    }
+
+  hdev->priv = priv;
+
+  vqnames[0]  = "virtio_spi_req";
+  callback[0] = vhost_spi_handler;
+  ret = vhost_create_virtqueues(hdev, 0, 1, vqnames, callback, NULL);
+  if (ret < 0)
+    {
+      vhosterr("vhost_create_virtqueues failed, ret=%d\n", ret);
+      file_close(&priv->bus);
+      kmm_free(priv);
+      return ret;
+    }
+
+  virtqueue_enable_cb(hdev->vrings_info[0].vq);
+
+  vhost_spi_work(priv);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: vhost_spi_remove
+ ****************************************************************************/
+
+static void vhost_spi_remove(FAR struct vhost_device *hdev)
+{
+  FAR struct vhost_spi_priv_s *priv = hdev->priv;
+
+  virtqueue_disable_cb(hdev->vrings_info[0].vq);
+  work_cancel_sync(HPWORK, &priv->work);
+  vhost_delete_virtqueues(hdev);
+  file_close(&priv->bus);
+  kmm_free(priv);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: vhost_register_spi_driver
+ ****************************************************************************/
+
+int vhost_register_spi_driver(void)
+{
+  return vhost_register_driver(&g_vhost_spi_driver);
+}
diff --git a/drivers/vhost/vhost-spi.h b/drivers/vhost/vhost-spi.h
new file mode 100644
index 00000000000..b8821a9d28f
--- /dev/null
+++ b/drivers/vhost/vhost-spi.h
@@ -0,0 +1,63 @@
+/****************************************************************************
+ * drivers/vhost/vhost-spi.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __DRIVERS_VHOST_VHOST_SPI_H
+#define __DRIVERS_VHOST_VHOST_SPI_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_DRIVERS_VHOST_SPI
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: vhost_register_spi_driver
+ *
+ * Description:
+ *   Register the vhost (device-side) SPI driver with the vhost core so it
+ *   can be bound to a matching VirtIO SPI vdev.
+ *
+ * Returned Value:
+ *   Zero on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int vhost_register_spi_driver(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CONFIG_DRIVERS_VHOST_SPI */
+
+#endif /* __DRIVERS_VHOST_VHOST_SPI_H */
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index b846caad7e7..05462a33564 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -45,6 +45,7 @@
 #include "vhost-blk.h"
 #include "vhost-can.h"
 #include "vhost-i2c.h"
+#include "vhost-spi.h"
 #include "vhost-serial.h"
 #include "vhost-rng.h"
 #include "vhost-rpmb.h"
@@ -681,6 +682,14 @@ void vhost_register_drivers(void)
     }
 #endif
 
+#ifdef CONFIG_DRIVERS_VHOST_SPI
+  ret = vhost_register_spi_driver();
+  if (ret < 0)
+    {
+      vhosterr("vhost_register_spi_driver failed, ret=%d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_DRIVERS_VHOST_RPMB
   ret = vhost_register_rpmb_driver();
   if (ret < 0)

Reply via email to