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

commit 20c99757a91c7bf587f15ebb4cd29c34711a4822
Author: Yongrong Wang <[email protected]>
AuthorDate: Fri Aug 8 15:24:45 2025 +0800

    drivers/rpmsg: dispatch rx in per-device work queues
    
    rpmsg_virtio ran its own kthread per device, waking it through readyrx/
    freerx node lists for every received buffer. Move the rx scheduling into
    the rpmsg framework: each device gets CONFIG_RPMSG_WQUEUE_NUMBER work
    queues, and rpmsg_queue_rx_work() hands a received buffer to the queue
    picked by the endpoint priority. A bound RT endpoint is still processed
    in the interrupt; an unbound one and every other endpoint go to a work
    queue, and a name service message takes the priority of the endpoint it
    binds.
    
    rpmsg_wait() now runs the pending works of the calling queue while it
    waits, so a service that blocks inside a callback no longer stalls rx,
    with the depth bounded by RPMSG_RECURSIVE_LIMIT. The transport wait/post
    ops lose their semaphore argument, and rpmsg_register() takes the number
    of rx works to preallocate (0 for rpmsg_port and rpmsg_router_edge).
    
    CONFIG_RPMSG_VIRTIO_PRIORITY/STACKSIZE become CONFIG_RPMSG_WQUEUE_*.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: guanyi3 <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/Kconfig             |  25 ++--
 drivers/rpmsg/rpmsg.c             | 217 +++++++++++++++++++++++++++-
 drivers/rpmsg/rpmsg.h             |  25 +++-
 drivers/rpmsg/rpmsg_port.c        |   2 +-
 drivers/rpmsg/rpmsg_router_edge.c |   3 +-
 drivers/rpmsg/rpmsg_virtio.c      | 296 +++++++++-----------------------------
 6 files changed, 327 insertions(+), 241 deletions(-)

diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
index 524d55f4c6e..c4041c44449 100644
--- a/drivers/rpmsg/Kconfig
+++ b/drivers/rpmsg/Kconfig
@@ -28,9 +28,26 @@ config RPMSG
        bool
        default n
        select OPENAMP
+       select SCHED_WORKQUEUE
 
 if RPMSG
 
+config RPMSG_WQUEUE_PRIORITY
+       int "rpmsg work queue priority"
+       default 224
+
+config RPMSG_WQUEUE_STACKSIZE
+       int "rpmsg work queue stack size"
+       default DEFAULT_TASK_STACKSIZE
+
+config RPMSG_WQUEUE_NUMBER
+       int "rpmsg work queue number"
+       default 1
+       range 1 256
+       ---help---
+               This macro sets the number of rpmsg work queue. It can be
+               customized and the minimum value is 1.
+
 config RPMSG_TRACE
        bool "rpmsg trace"
        default n
@@ -213,14 +230,6 @@ config RPMSG_VIRTIO
 
 if RPMSG_VIRTIO
 
-config RPMSG_VIRTIO_PRIORITY
-       int "rpmsg virtio priority"
-       default 224
-
-config RPMSG_VIRTIO_STACKSIZE
-       int "rpmsg virtio stack size"
-       default DEFAULT_TASK_STACKSIZE
-
 config RPMSG_VIRTIO_PM
        bool "RPMsg VirtIO power management"
        depends on PM
diff --git a/drivers/rpmsg/rpmsg.c b/drivers/rpmsg/rpmsg.c
index d0e3f0c79c2..4b6b7f709d4 100644
--- a/drivers/rpmsg/rpmsg.c
+++ b/drivers/rpmsg/rpmsg.c
@@ -28,6 +28,7 @@
 
 #include <inttypes.h>
 #include <limits.h>
+#include <stdio.h>
 #include <syslog.h>
 
 #include <metal/sys.h>
@@ -37,6 +38,7 @@
 #include <nuttx/nuttx.h>
 #include <nuttx/rwsem.h>
 #include <nuttx/semaphore.h>
+#include <nuttx/wqueue.h>
 
 #include "rpmsg.h"
 #include "rpmsg_procfs.h"
@@ -44,6 +46,13 @@
 #include "rpmsg_test.h"
 #include "rpmsg_wakelock.h"
 
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define RPMSG_RECURSIVE_LIMIT    8
+#define RPMSG_DEFAULT_WQUEUE_IDX (CONFIG_RPMSG_WQUEUE_NUMBER / 2)
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -72,6 +81,14 @@ struct rpmsg_ioctl_s
   unsigned long   arg;
 };
 
+struct rpmsg_work_s
+{
+  struct work_s       work;
+  rpmsg_worker_t      worker;
+  FAR struct rpmsg_s *rpmsg;
+  FAR void           *arg;
+};
+
 /****************************************************************************
  * Private Function Prototypes
  ****************************************************************************/
@@ -198,6 +215,73 @@ static int rpmsg_dev_ioctl(FAR struct file *filep, int cmd,
   return rpmsg_dev_ioctl_(rpmsg, cmd, arg);
 }
 
+static int rpmsg_init_workrx(FAR struct rpmsg_s *rpmsg, uint16_t nrx)
+{
+  FAR struct rpmsg_work_s *workrx;
+  uint16_t i;
+
+  if (nrx == 0)
+    {
+      return 0;
+    }
+
+  workrx = kmm_zalloc(nrx * sizeof(struct rpmsg_work_s));
+  if (workrx == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  list_initialize(&rpmsg->freerx);
+  for (i = 0; i < nrx; i++)
+    {
+      list_add_tail(&rpmsg->freerx, &workrx[i].work.node);
+    }
+
+  rpmsg->workrx = workrx;
+  return 0;
+}
+
+static void rpmsg_deinit_workrx(FAR struct rpmsg_s *rpmsg)
+{
+  if (rpmsg->workrx)
+    {
+      kmm_free(rpmsg->workrx);
+      rpmsg->workrx = NULL;
+    }
+}
+
+static FAR struct rpmsg_wqueue_s *
+rpmsg_get_current_wqueue(FAR struct rpmsg_s *rpmsg)
+{
+  int i;
+
+  for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
+    {
+      if (work_queue_in_queue(rpmsg->wqueues[i].kwqueue))
+        {
+          return &rpmsg->wqueues[i];
+        }
+    }
+
+  return NULL;
+}
+
+static void rpmsg_rx_worker(FAR void *arg)
+{
+  FAR struct rpmsg_work_s *work = arg;
+  FAR struct rpmsg_s *rpmsg = work->rpmsg;
+  FAR struct rpmsg_device *rdev;
+  rpmsg_worker_t worker = work->worker;
+
+  arg = work->arg;
+  rdev = rpmsg_get_rdev_by_rpmsg(rpmsg);
+  metal_mutex_acquire(&rdev->lock);
+  list_add_tail(&work->rpmsg->freerx, &work->work.node);
+  metal_mutex_release(&rdev->lock);
+  DEBUGASSERT(worker != NULL);
+  worker(rpmsg, arg);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -209,7 +293,9 @@ void rpmsg_initialize(void)
 
 int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
 {
+  FAR struct rpmsg_wqueue_s *wqueue;
   FAR struct rpmsg_s *rpmsg;
+  int ret;
 
   if (!ept || !sem)
     {
@@ -222,12 +308,40 @@ int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t 
*sem)
       return nxsem_wait_uninterruptible(sem);
     }
 
-  return rpmsg->ops->wait(rpmsg, sem);
+  wqueue = rpmsg_get_current_wqueue(rpmsg);
+  if (wqueue == NULL)
+    {
+      return nxsem_wait_uninterruptible(sem);
+    }
+
+  for (; ; )
+    {
+      ret = nxsem_trywait(sem);
+      if (ret >= 0)
+        {
+          return ret;
+        }
+
+      rpmsg->ops->wait(rpmsg);
+
+      if (wqueue->recursive >= RPMSG_RECURSIVE_LIMIT)
+        {
+          return RPMSG_EOPNOTSUPP;
+        }
+
+      wqueue->recursive++;
+      work_queue_dispatch(wqueue->kwqueue);
+      wqueue->recursive--;
+    }
+
+  return OK;
 }
 
 int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
 {
   FAR struct rpmsg_s *rpmsg;
+  int semcount = 0;
+  int ret;
 
   if (!ept || !sem)
     {
@@ -240,7 +354,14 @@ int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t 
*sem)
       return nxsem_post(sem);
     }
 
-  return rpmsg->ops->post(rpmsg, sem);
+  nxsem_get_value(sem, &semcount);
+  ret = nxsem_post(sem);
+  if (semcount >= 0)
+    {
+      ret = rpmsg->ops->post(rpmsg);
+    }
+
+  return ret;
 }
 
 FAR const char *rpmsg_get_local_cpuname(FAR struct rpmsg_device *rdev)
@@ -547,7 +668,7 @@ void rpmsg_device_destroy(FAR struct rpmsg_s *rpmsg)
 }
 
 int rpmsg_register(FAR const char *path, FAR struct rpmsg_s *rpmsg,
-                   FAR const struct rpmsg_ops_s *ops)
+                   FAR const struct rpmsg_ops_s *ops, uint16_t nrx)
 {
   struct metal_init_params params = METAL_INIT_DEFAULTS;
   int ret;
@@ -565,6 +686,14 @@ int rpmsg_register(FAR const char *path, FAR struct 
rpmsg_s *rpmsg,
       return ret;
     }
 
+  ret = rpmsg_init_workrx(rpmsg, nrx);
+  if (ret < 0)
+    {
+      unregister_driver(path);
+      metal_finish();
+      return ret;
+    }
+
   list_initialize(&rpmsg->bind);
   nxrmutex_init(&rpmsg->lock);
   rpmsg->ops = ops;
@@ -595,6 +724,7 @@ void rpmsg_unregister(FAR const char *path, FAR struct 
rpmsg_s *rpmsg)
   list_delete(&rpmsg->node);
   up_write(&g_rpmsg_lock);
 
+  rpmsg_deinit_workrx(rpmsg);
   nxrmutex_destroy(&rpmsg->lock);
   unregister_driver(path);
 
@@ -718,3 +848,84 @@ void rpmsg_modify_signals(FAR struct rpmsg_s *rpmsg,
       metal_mutex_release(&rdev->lock);
     }
 }
+
+int rpmsg_init_wqueues(FAR struct rpmsg_s *rpmsg)
+{
+  FAR struct rpmsg_wqueue_s *wqueue;
+  char name[64];
+  int i;
+
+  for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
+    {
+      wqueue = &rpmsg->wqueues[i];
+      snprintf(name, sizeof(name), "rpmsg-%s-%d", rpmsg->cpuname, i);
+      wqueue->kwqueue = work_queue_create(name,
+                                          CONFIG_RPMSG_WQUEUE_PRIORITY + i,
+                                          NULL,
+                                          CONFIG_RPMSG_WQUEUE_STACKSIZE,
+                                          1);
+      if (wqueue->kwqueue == NULL)
+        {
+          rpmsgerr("rpmsg wqueue [%d] create failed\n", i);
+          goto err;
+        }
+    }
+
+  return 0;
+
+err:
+  while (--i >= 0)
+    {
+      work_queue_free(rpmsg->wqueues[i].kwqueue);
+    }
+
+  return -ENOMEM;
+}
+
+void rpmsg_deinit_wqueues(FAR struct rpmsg_s *rpmsg)
+{
+  int i;
+
+  for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
+    {
+      if (rpmsg->wqueues[i].kwqueue != NULL)
+        {
+          work_queue_free(rpmsg->wqueues[i].kwqueue);
+        }
+    }
+}
+
+int rpmsg_queue_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
+                     FAR struct work_s *work, worker_t worker, FAR void *arg)
+{
+  int idx;
+
+  idx = (int)priority - RPMSG_PRIO_DEFAULT + RPMSG_DEFAULT_WQUEUE_IDX;
+  if (idx < 0)
+    {
+      idx = 0;
+    }
+  else if (idx >= CONFIG_RPMSG_WQUEUE_NUMBER)
+    {
+      idx = CONFIG_RPMSG_WQUEUE_NUMBER - 1;
+    }
+
+  return work_queue_wq(rpmsg->wqueues[idx].kwqueue, work, worker, arg, 0);
+}
+
+void rpmsg_queue_rx_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
+                         rpmsg_worker_t worker, FAR void *arg)
+{
+  FAR struct rpmsg_work_s *work;
+
+  /* Called by the transport rx dispatch with rdev->lock held */
+
+  work = list_remove_head_type(&rpmsg->freerx, struct rpmsg_work_s,
+                               work.node);
+  DEBUGASSERT(work != NULL);
+  work->arg = arg;
+  work->rpmsg = rpmsg;
+  work->worker = worker;
+
+  rpmsg_queue_work(rpmsg, priority, &work->work, rpmsg_rx_worker, work);
+}
diff --git a/drivers/rpmsg/rpmsg.h b/drivers/rpmsg/rpmsg.h
index 76402ad6027..6fbc7ae8af2 100644
--- a/drivers/rpmsg/rpmsg.h
+++ b/drivers/rpmsg/rpmsg.h
@@ -30,6 +30,7 @@
 #include <nuttx/list.h>
 #include <nuttx/panic_notifier.h>
 #include <nuttx/rpmsg/rpmsg.h>
+#include <nuttx/wqueue.h>
 
 #include "rpmsg_ping.h"
 
@@ -45,6 +46,12 @@
  * Public Types
  ****************************************************************************/
 
+struct rpmsg_wqueue_s
+{
+  FAR struct kwork_wqueue_s    *kwqueue;
+  int                          recursive;
+};
+
 struct rpmsg_s
 {
   bool                         init;
@@ -54,6 +61,9 @@ struct rpmsg_s
   char                         local_cpuname[RPMSG_NAME_SIZE];
   char                         cpuname[RPMSG_NAME_SIZE];
   FAR const struct rpmsg_ops_s *ops;
+  struct rpmsg_wqueue_s        wqueues[CONFIG_RPMSG_WQUEUE_NUMBER];
+  FAR void                     *workrx;
+  struct list_node             freerx;
 #ifdef CONFIG_RPMSG_PING
   struct rpmsg_ping_dev_s      ping;
 #endif
@@ -78,8 +88,8 @@ struct rpmsg_s
 
 struct rpmsg_ops_s
 {
-  CODE int (*wait)(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem);
-  CODE int (*post)(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem);
+  CODE int (*wait)(FAR struct rpmsg_s *rpmsg);
+  CODE int (*post)(FAR struct rpmsg_s *rpmsg);
   CODE int (*ioctl)(FAR struct rpmsg_s *rpmsg, int cmd, unsigned long arg);
   CODE void (*panic)(FAR struct rpmsg_s *rpmsg);
   CODE void (*dump)(FAR struct rpmsg_s *rpmsg, bool verbose);
@@ -89,6 +99,8 @@ struct rpmsg_ops_s
 
 CODE typedef int (*rpmsg_foreach_t)(FAR struct rpmsg_s *rpmsg,
                                     FAR void *args);
+CODE typedef void (*rpmsg_worker_t)(FAR struct rpmsg_s *rpmsg,
+                                    FAR void *arg);
 
 /****************************************************************************
  * Public Function Prototypes
@@ -117,8 +129,15 @@ void rpmsg_device_created(FAR struct rpmsg_s *rpmsg);
 void rpmsg_device_destroy(FAR struct rpmsg_s *rpmsg);
 
 int rpmsg_register(FAR const char *path, FAR struct rpmsg_s *rpmsg,
-                   FAR const struct rpmsg_ops_s *ops);
+                   FAR const struct rpmsg_ops_s *ops, uint16_t nrx);
 void rpmsg_unregister(FAR const char *path, FAR struct rpmsg_s *rpmsg);
+int rpmsg_init_wqueues(FAR struct rpmsg_s *rpmsg);
+void rpmsg_deinit_wqueues(FAR struct rpmsg_s *rpmsg);
+int rpmsg_queue_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
+                     FAR struct work_s *work, worker_t worker,
+                     FAR void *arg);
+void rpmsg_queue_rx_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
+                         rpmsg_worker_t worker, FAR void *arg);
 
 int rpmsg_foreach(rpmsg_foreach_t handler, FAR void *args);
 void rpmsg_dump_epts(FAR struct rpmsg_device *rdev);
diff --git a/drivers/rpmsg/rpmsg_port.c b/drivers/rpmsg/rpmsg_port.c
index 8d8c82f170b..48f6384d8e9 100644
--- a/drivers/rpmsg/rpmsg_port.c
+++ b/drivers/rpmsg/rpmsg_port.c
@@ -820,7 +820,7 @@ int rpmsg_port_register(FAR struct rpmsg_port_s *port,
 
   rpmsgvbs("start register %s\n", port->rpmsg.cpuname);
   snprintf(name, sizeof(name), "/dev/rpmsg/%s", port->rpmsg.cpuname);
-  ret = rpmsg_register(name, &port->rpmsg, &g_rpmsg_port_ops);
+  ret = rpmsg_register(name, &port->rpmsg, &g_rpmsg_port_ops, 0);
   if (ret < 0)
     {
       return ret;
diff --git a/drivers/rpmsg/rpmsg_router_edge.c 
b/drivers/rpmsg/rpmsg_router_edge.c
index b80f01943e2..8fd066dd482 100644
--- a/drivers/rpmsg/rpmsg_router_edge.c
+++ b/drivers/rpmsg/rpmsg_router_edge.c
@@ -594,7 +594,8 @@ rpmsg_router_edge_create(FAR struct rpmsg_device *hubdev,
 
   snprintf(edge->name, sizeof(edge->name), "/dev/rpmsg/%s",
            edge->rpmsg.cpuname);
-  ret = rpmsg_register(edge->name, &edge->rpmsg, &g_rpmsg_router_edge_ops);
+  ret = rpmsg_register(edge->name, &edge->rpmsg,
+                       &g_rpmsg_router_edge_ops, 0);
   if (ret < 0)
     {
       rpmsgerr("rpmsg_register failed: %d\n", ret);
diff --git a/drivers/rpmsg/rpmsg_virtio.c b/drivers/rpmsg/rpmsg_virtio.c
index b8097b6b375..b972942c7e2 100644
--- a/drivers/rpmsg/rpmsg_virtio.c
+++ b/drivers/rpmsg/rpmsg_virtio.c
@@ -30,7 +30,6 @@
 #include <syslog.h>
 
 #include <nuttx/kmalloc.h>
-#include <nuttx/kthread.h>
 #include <nuttx/nuttx.h>
 #include <nuttx/power/pm.h>
 #include <nuttx/rpmsg/rpmsg_virtio.h>
@@ -63,12 +62,6 @@
  * Private Types
  ****************************************************************************/
 
-struct rpmsg_virtio_node_s
-{
-  FAR struct rpmsg_hdr           *hdr;
-  struct metal_list               node;
-};
-
 struct rpmsg_virtio_priv_s
 {
   struct rpmsg_s                  rpmsg;
@@ -77,13 +70,10 @@ struct rpmsg_virtio_priv_s
   FAR struct virtio_device       *vdev;
   sem_t                           semrx;
   sem_t                           semtx;
-  pid_t                           tid;
   vq_notify                       notifytx;
   uint16_t                        headrx;
   uint16_t                        headtx;
-  FAR struct rpmsg_virtio_node_s *noderx;
-  struct metal_list               freerx;
-  struct metal_list               readyrx;
+  struct work_s                   startwork;
 #ifdef CONFIG_RPMSG_VIRTIO_PM
   spinlock_t                      lock;
   struct pm_wakelock_s            wakelock;
@@ -96,11 +86,8 @@ struct rpmsg_virtio_priv_s
  * Private Function Prototypes
  ****************************************************************************/
 
-static void rpmsg_virtio_rx_wakeup(FAR struct rpmsg_virtio_priv_s *priv);
-static void rpmsg_virtio_tx_wakeup(FAR struct rpmsg_virtio_priv_s *priv);
-
-static int rpmsg_virtio_wait(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem);
-static int rpmsg_virtio_post(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem);
+static int rpmsg_virtio_wait(FAR struct rpmsg_s *rpmsg);
+static int rpmsg_virtio_post(FAR struct rpmsg_s *rpmsg);
 static void rpmsg_virtio_dump(FAR struct rpmsg_s *rpmsg, bool verbose);
 
 static void rpmsg_virtio_rx_callback(FAR struct virtqueue *vq);
@@ -226,15 +213,6 @@ rpmsg_virtio_pm_action(FAR struct rpmsg_virtio_priv_s 
*priv, bool stay)
 #  define rpmsg_virtio_pm_action(priv, stay)
 #endif
 
-/****************************************************************************
- * Name: rpmsg_virtio_is_recursive
- ****************************************************************************/
-
-static bool rpmsg_virtio_is_recursive(FAR struct rpmsg_virtio_priv_s *priv)
-{
-  return nxsched_gettid() == priv->tid;
-}
-
 /****************************************************************************
  * Name: rpmsg_virtio_process_rx_buffer
  *
@@ -285,134 +263,63 @@ static int rpmsg_virtio_process_rx_buffer(FAR struct 
rpmsg_device *rdev,
  * Name: rpmsg_virtio_rx_worker
  ****************************************************************************/
 
-static void rpmsg_virtio_rx_worker(FAR struct rpmsg_virtio_priv_s *priv)
+static void rpmsg_virtio_rx_worker(FAR struct rpmsg_s *rpmsg, FAR void *arg)
 {
+  FAR struct rpmsg_virtio_priv_s *priv =
+    (FAR struct rpmsg_virtio_priv_s *)rpmsg;
   FAR struct virtio_device *vdev = priv->vdev;
   FAR struct rpmsg_virtio_device *rvdev = vdev->priv;
   FAR struct rpmsg_device *rdev = &rvdev->rdev;
-  FAR struct rpmsg_virtio_node_s *node;
+  FAR struct rpmsg_hdr *hdr = arg;
   FAR struct rpmsg_endpoint *ept;
-  FAR struct metal_list *tmp;
-  FAR struct rpmsg_hdr *hdr;
-  bool release = false;
-  int status;
+  int status = RPMSG_SUCCESS;
 
   metal_mutex_acquire(&rdev->lock);
-  while ((tmp = metal_list_first(&priv->readyrx)) != NULL)
+  ept = rpmsg_get_ept_from_addr(rdev, hdr->dst);
+  if (ept)
     {
-      metal_list_del(tmp);
-      node = container_of(tmp, struct rpmsg_virtio_node_s, node);
-      hdr = node->hdr;
-      metal_list_add_tail(&priv->freerx, &node->node);
-
-      ept = rpmsg_get_ept_from_addr(rdev, hdr->dst);
-      if (ept)
-        {
-          status = rpmsg_virtio_process_rx_buffer(rdev, ept, hdr);
-        }
-      else
-        {
-          status = RPMSG_SUCCESS;
-        }
-
-      if (status != RPMSG_SUCCESS_BUFFER_RELEASED &&
-          rpmsg_virtio_buf_held_dec_test(hdr))
-        {
-          rpmsg_virtio_release_rx_buffer_nolock(rvdev, hdr);
-          release = true;
-        }
+      status = rpmsg_virtio_process_rx_buffer(rdev, ept, hdr);
     }
 
-  if (release)
+  if (status != RPMSG_SUCCESS_BUFFER_RELEASED &&
+      rpmsg_virtio_buf_held_dec_test(hdr))
     {
-      /* tell peer we return some rx buffer */
-
+      rpmsg_virtio_release_rx_buffer_nolock(rvdev, hdr);
       virtqueue_kick(rvdev->rvq);
     }
 
   metal_mutex_release(&rdev->lock);
 }
 
-/****************************************************************************
- * Name: rpmsg_virtio_rx_wakeup
- ****************************************************************************/
-
-static void rpmsg_virtio_rx_wakeup(FAR struct rpmsg_virtio_priv_s *priv)
-{
-  int semcount;
-
-  nxsem_get_value(&priv->semrx, &semcount);
-  while (semcount++ < 1)
-    {
-      nxsem_post(&priv->semrx);
-    }
-}
-
-/****************************************************************************
- * Name: rpmsg_virtio_tx_wakeup
- ****************************************************************************/
-
-static void rpmsg_virtio_tx_wakeup(FAR struct rpmsg_virtio_priv_s *priv)
-{
-  int semcount;
-
-  nxsem_get_value(&priv->semtx, &semcount);
-  while (semcount++ < 1)
-    {
-      nxsem_post(&priv->semtx);
-    }
-}
-
 /****************************************************************************
  * Name: rpmsg_virtio_wait
  ****************************************************************************/
 
-static int rpmsg_virtio_wait(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem)
+static int rpmsg_virtio_wait(FAR struct rpmsg_s *rpmsg)
 {
   FAR struct rpmsg_virtio_priv_s *priv =
     (FAR struct rpmsg_virtio_priv_s *)rpmsg;
-  int ret;
 
-  if (!rpmsg_virtio_is_recursive(priv))
-    {
-      return nxsem_wait_uninterruptible(sem);
-    }
-
-  while (1)
-    {
-      ret = nxsem_trywait(sem);
-      if (ret >= 0)
-        {
-          break;
-        }
-
-      nxsem_wait(&priv->semtx);
-      rpmsg_virtio_rx_worker(priv);
-    }
-
-  return ret;
+  return nxsem_tickwait(&priv->semtx, MSEC2TICK(RPMSG_VIRTIO_TIMEOUT_MS));
 }
 
 /****************************************************************************
  * Name: rpmsg_virtio_post
  ****************************************************************************/
 
-static int rpmsg_virtio_post(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem)
+static int rpmsg_virtio_post(FAR struct rpmsg_s *rpmsg)
 {
   FAR struct rpmsg_virtio_priv_s *priv =
     (FAR struct rpmsg_virtio_priv_s *)rpmsg;
   int semcount;
-  int ret;
 
-  nxsem_get_value(sem, &semcount);
-  ret = nxsem_post(sem);
-
-  if (priv && semcount >= 0)
+  nxsem_get_value(&priv->semtx, &semcount);
+  while (semcount++ < 1)
     {
-      rpmsg_virtio_tx_wakeup(priv);
+      nxsem_post(&priv->semtx);
     }
 
-  return ret;
+  return OK;
 }
 
 /****************************************************************************
@@ -595,24 +502,6 @@ static void rpmsg_virtio_update_tx(FAR struct 
rpmsg_virtio_priv_s *priv)
     }
 }
 
-/****************************************************************************
- * Name: rpmsg_virtio_rx_enqueue
- ****************************************************************************/
-
-static void rpmsg_virtio_rx_enqueue(FAR struct rpmsg_virtio_priv_s *priv,
-                                    FAR struct rpmsg_hdr *hdr)
-{
-  FAR struct rpmsg_virtio_node_s *node;
-  FAR struct metal_list *tmp;
-
-  tmp = metal_list_first(&priv->freerx);
-  DEBUGASSERT(tmp != NULL);
-  metal_list_del(tmp);
-  node = container_of(tmp, struct rpmsg_virtio_node_s, node);
-  node->hdr = hdr;
-  metal_list_add_tail(&priv->readyrx, &node->node);
-}
-
 /****************************************************************************
  * Name: rpmsg_virtio_rx_dispatch
  ****************************************************************************/
@@ -650,14 +539,31 @@ static void rpmsg_virtio_rx_dispatch(FAR struct 
rpmsg_virtio_priv_s *priv)
 
       if (ept)
         {
-          if (ept->priority == RPMSG_PRIO_RT &&
+          uint8_t priority = ept->priority;
+
+          if (ept == &rdev->ns_ept)
+            {
+              FAR struct rpmsg_endpoint *ept_;
+              FAR struct rpmsg_ns_msg *ns_msg;
+
+              ns_msg = (FAR struct rpmsg_ns_msg *)RPMSG_LOCATE_DATA(hdr);
+              ept_ = rpmsg_get_endpoint(rdev, ns_msg->name, RPMSG_ADDR_ANY,
+                                        hdr->dst);
+              if (ept_)
+                {
+                  priority = ept_->priority;
+                }
+            }
+
+          if (priority == RPMSG_PRIO_RT && ept != &rdev->ns_ept &&
               ept->dest_addr != RPMSG_ADDR_ANY)
             {
               status = rpmsg_virtio_process_rx_buffer(rdev, ept, hdr);
             }
           else
             {
-              rpmsg_virtio_rx_enqueue(priv, hdr);
+              rpmsg_queue_rx_work(&priv->rpmsg, priority,
+                                  rpmsg_virtio_rx_worker, hdr);
               status = RPMSG_SUCCESS_BUFFER_RELEASED;
             }
         }
@@ -681,15 +587,7 @@ static void rpmsg_virtio_rx_dispatch(FAR struct 
rpmsg_virtio_priv_s *priv)
       virtqueue_kick(rvdev->rvq);
     }
 
-  if (!metal_list_is_empty(&priv->readyrx))
-    {
-      metal_mutex_release(&rdev->lock);
-      rpmsg_virtio_rx_wakeup(priv);
-    }
-  else
-    {
-      metal_mutex_release(&rdev->lock);
-    }
+  metal_mutex_release(&rdev->lock);
 }
 
 /****************************************************************************
@@ -719,7 +617,7 @@ static void rpmsg_virtio_tx_callback(FAR struct virtqueue 
*vq)
   FAR struct rpmsg_virtio_priv_s *priv =
     container_of(vq->vq_dev->priv, struct rpmsg_virtio_priv_s, rvdev);
 
-  rpmsg_virtio_tx_wakeup(priv);
+  rpmsg_virtio_post(&priv->rpmsg);
 
   /* rpmsg_virtio_tx_callback() called normally means the tx buffer has been
    * returned by peer, so call rpmsg_virtio_pm_action(false) to enter to
@@ -758,25 +656,18 @@ static int rpmsg_virtio_notify_wait(FAR struct 
rpmsg_device *rdev,
   FAR struct rpmsg_virtio_priv_s *priv =
     container_of(rdev, struct rpmsg_virtio_priv_s, rvdev);
 
-  if (!rpmsg_virtio_is_recursive(priv))
-    {
-      return RPMSG_EOPNOTSUPP;
-    }
-
-  /* Wait to wakeup */
-
-  nxsem_tickwait(&priv->semtx, MSEC2TICK(RPMSG_VIRTIO_TIMEOUT_MS));
-  rpmsg_virtio_rx_worker(priv);
-
-  return 0;
+  return rpmsg_wait(&priv->rvdev.rdev.ns_ept, &priv->semtx);
 }
 
 /****************************************************************************
- * Name: rpmsg_virtio_start
+ * Name: rpmsg_virtio_start_worker
  ****************************************************************************/
 
-static int rpmsg_virtio_start(FAR struct rpmsg_virtio_priv_s *priv)
+static void rpmsg_virtio_start_worker(FAR void *arg)
 {
+  FAR struct rpmsg_s *rpmsg = (FAR struct rpmsg_s *)arg;
+  FAR struct rpmsg_virtio_priv_s *priv =
+    (FAR struct rpmsg_virtio_priv_s *)rpmsg;
   FAR struct virtio_device *vdev = priv->vdev;
   struct rpmsg_virtio_config config =
   {
@@ -787,8 +678,8 @@ static int rpmsg_virtio_start(FAR struct 
rpmsg_virtio_priv_s *priv)
     rpmsg_virtio_tx_callback,
   };
 
+  char name[64];
   uint16_t nrx;
-  uint16_t i;
   int ret;
 
   if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_BUFSZ))
@@ -838,17 +729,14 @@ static int rpmsg_virtio_start(FAR struct 
rpmsg_virtio_priv_s *priv)
       nrx = vdev->vrings_info[1].info.num_descs;
     }
 
-  priv->noderx = kmm_malloc(nrx * sizeof(*priv->noderx));
-  if (priv->noderx == NULL)
-    {
-      return -ENOMEM;
-    }
+  /* Register the rpmsg to rpmsg framework */
 
-  metal_list_init(&priv->readyrx);
-  metal_list_init(&priv->freerx);
-  for (i = 0; i < nrx; i++)
+  snprintf(name, sizeof(name), "/dev/rpmsg/%s", priv->rpmsg.cpuname);
+  ret = rpmsg_register(name, &priv->rpmsg, &g_rpmsg_virtio_ops, nrx);
+  if (ret < 0)
     {
-      metal_list_add_tail(&priv->freerx, &priv->noderx[i].node);
+      rpmsgerr("rpmsg register failed, ret=%d\n", ret);
+      return;
     }
 
   ret = rpmsg_init_vdev_with_config(&priv->rvdev, vdev, rpmsg_ns_bind,
@@ -857,8 +745,8 @@ static int rpmsg_virtio_start(FAR struct 
rpmsg_virtio_priv_s *priv)
   if (ret < 0)
     {
       rpmsgerr("rpmsg_init_vdev failed, ret=%d\n", ret);
-      kmm_free(priv->noderx);
-      return ret;
+      rpmsg_unregister(name, &priv->rpmsg);
+      return;
     }
 
   priv->notifytx = priv->rvdev.svq->notify;
@@ -881,35 +769,6 @@ static int rpmsg_virtio_start(FAR struct 
rpmsg_virtio_priv_s *priv)
   /* Broadcast device_created to all registers */
 
   rpmsg_device_created(&priv->rpmsg);
-
-  return ret;
-}
-
-/****************************************************************************
- * Name: rpmsg_virtio_thread
- ****************************************************************************/
-
-static int rpmsg_virtio_thread(int argc, FAR char *argv[])
-{
-  FAR struct rpmsg_virtio_priv_s *priv = (FAR struct rpmsg_virtio_priv_s *)
-    ((uintptr_t)strtoul(argv[2], NULL, 16));
-  int ret;
-
-  priv->tid = nxsched_gettid();
-  ret = rpmsg_virtio_start(priv);
-  if (ret < 0)
-    {
-      rpmsgerr("virtio rpmsg start failed, ret=%d\n", ret);
-      return ret;
-    }
-
-  while (1)
-    {
-      nxsem_wait_uninterruptible(&priv->semrx);
-      rpmsg_virtio_rx_worker(priv);
-    }
-
-  return 0;
 }
 
 /****************************************************************************
@@ -924,10 +783,10 @@ int rpmsg_virtio_probe_cpuname(FAR struct virtio_device 
*vdev,
                                FAR const char *cpuname)
 {
   FAR struct rpmsg_virtio_priv_s *priv;
-  FAR char *argv[3];
   uint64_t features;
+#ifdef CONFIG_RPMSG_VIRTIO_PM
   char name[64];
-  char arg1[32];
+#endif
   int ret;
 
   priv = kmm_zalloc(sizeof(*priv));
@@ -985,41 +844,24 @@ int rpmsg_virtio_probe_cpuname(FAR struct virtio_device 
*vdev,
       strlcpy(priv->rpmsg.cpuname, cpuname, VIRTIO_RPMSG_CPUNAME_SIZE);
     }
 
-  /* Register the rpmsg to rpmsg framework */
-
-  snprintf(name, sizeof(name), "/dev/rpmsg/%s", priv->rpmsg.cpuname);
-  ret = rpmsg_register(name, &priv->rpmsg, &g_rpmsg_virtio_ops);
+  ret = rpmsg_init_wqueues(&priv->rpmsg);
   if (ret < 0)
     {
-      rpmsgerr("rpmsg register failed, ret=%d\n", ret);
+      rpmsgerr("rpmsg_init_wqueues failed, ret=%d\n", ret);
       goto err;
     }
 
-  snprintf(arg1, sizeof(arg1), "%p", priv);
-  argv[0] = priv->rpmsg.cpuname;
-  argv[1] = arg1;
-  argv[2] = NULL;
-  ret = kthread_create("rpmsg-virtio", CONFIG_RPMSG_VIRTIO_PRIORITY,
-                       CONFIG_RPMSG_VIRTIO_STACKSIZE,
-                       rpmsg_virtio_thread, argv);
-  if (ret < 0)
-    {
-      rpmsgerr("kthread_create failed, ret=%d\n", ret);
-      goto err_kthread;
-    }
-
-  priv->tid = ret;
-
 #ifdef CONFIG_RPMSG_VIRTIO_PM
   spin_lock_init(&priv->lock);
   snprintf(name, sizeof(name), "rpmsg-virtio-%s", priv->rpmsg.cpuname);
   pm_wakelock_init(&priv->wakelock, name, PM_IDLE_DOMAIN, PM_IDLE);
 #endif
 
+  rpmsg_queue_work(&priv->rpmsg, RPMSG_PRIO_DEFAULT, &priv->startwork,
+                   rpmsg_virtio_start_worker, &priv->rpmsg);
+
   return ret;
 
-err_kthread:
-  rpmsg_unregister(name, &priv->rpmsg);
 err:
   nxsem_destroy(&priv->semtx);
   nxsem_destroy(&priv->semrx);
@@ -1059,6 +901,10 @@ void rpmsg_virtio_remove(FAR struct virtio_device *vdev)
 
   virtqueue_disable_cb(priv->rvdev.svq);
 
+  /* Deinit the rpmsg wqueues */
+
+  rpmsg_deinit_wqueues(&priv->rpmsg);
+
   /* Destroy all the rpmsg services */
 
   rpmsg_device_destroy(&priv->rpmsg);
@@ -1074,10 +920,10 @@ void rpmsg_virtio_remove(FAR struct virtio_device *vdev)
 
   rpmsg_deinit_vdev(&priv->rvdev);
 
-  /* Delete the kthread before freeing noderx: it walks readyrx/freerx */
+  /* Destroy the rpmsg virtio semaphores */
 
-  kthread_delete(priv->tid);
-  kmm_free(priv->noderx);
+  nxsem_destroy(&priv->semtx);
+  nxsem_destroy(&priv->semrx);
 
   /* Free the private data */
 

Reply via email to