You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2022/03/30 07:00:14 UTC

[incubator-nuttx] branch master updated (4071f46 -> 86a496d)

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

pkarashchenko pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


    from 4071f46  boards/boardctl:common boardctl to read reset cause.
     new 5b6b748  rptun: set rptun thread default stack size to 4K
     new 7aba7c3  rptun: add rptun_force_panic support
     new 3ab7ade  rptun: add rptun_reset support
     new c9404e2  rptun: use HPWORK instead of thread
     new 7471d09  rptun: add rptun_panic for both master & salve
     new be49f6b  rptun: add rpmsg_wait/post support for recursive dispatch
     new 5599f4e  rptun: divide shram to TX & RX
     new f0ba2f0  rptun: add rptun dump support
     new 1c7e59e  rptun: pm_stay when send msg & pm_relax when all tx buffer returned
     new a19aef4  rptun: enable rx interrupts for virtq
     new 759898c  rptun: add ping rpmsg support
     new bae9a97  openamp: add openamp patches
     new 86a496d  rptun: forward panic/reset to lowerhalf

The 13 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 drivers/rptun/Kconfig                              |  35 +-
 drivers/rptun/Make.defs                            |   7 +-
 drivers/rptun/rptun.c                              | 479 ++++++++++++++++-----
 .../cxd56_i2c_bitbang.h => drivers/rptun/rptun.h   |  33 +-
 drivers/rptun/rptun_dump.c                         | 123 ++++++
 drivers/rptun/rptun_ping.c                         | 192 +++++++++
 include/nuttx/rptun/openamp.h                      |   4 +
 include/nuttx/rptun/rptun.h                        |  56 ++-
 openamp/0006-openamp-fix-scenario-case.patch       |  41 +-
 ...openamp-divide-shram-to-TX-shram-RX-shram.patch | 130 ++++++
 ...o-don-t-need-check-status-when-get_tx_pay.patch |  34 ++
 openamp/open-amp.defs                              |   2 +
 12 files changed, 990 insertions(+), 146 deletions(-)
 copy arch/arm/src/cxd56xx/cxd56_i2c_bitbang.h => drivers/rptun/rptun.h (69%)
 create mode 100644 drivers/rptun/rptun_dump.c
 create mode 100644 drivers/rptun/rptun_ping.c
 create mode 100644 openamp/0007-openamp-divide-shram-to-TX-shram-RX-shram.patch
 create mode 100644 openamp/0008-rpmsg_virtio-don-t-need-check-status-when-get_tx_pay.patch

[incubator-nuttx] 07/13: rptun: divide shram to TX & RX

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 5599f4e24ab91f11c63ab0cd388d84a1ff8da499
Author: ligd <li...@xiaomi.com>
AuthorDate: Mon Feb 7 20:28:25 2022 +0800

    rptun: divide shram to TX & RX
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/rptun.c | 68 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 49 insertions(+), 19 deletions(-)

diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index f016b02..3cf9cb5 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -66,7 +66,8 @@ struct rptun_priv_s
   FAR struct rptun_dev_s       *dev;
   struct remoteproc            rproc;
   struct rpmsg_virtio_device   vdev;
-  struct rpmsg_virtio_shm_pool shm_pool;
+  struct rpmsg_virtio_shm_pool tx_shpool;
+  struct rpmsg_virtio_shm_pool rx_shpool;
   struct metal_list            bind;
   struct metal_list            node;
   sem_t                        sem;
@@ -636,30 +637,48 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
       align0 = rsc->rpmsg_vring0.align;
       align1 = rsc->rpmsg_vring1.align;
 
-      tbsz = ALIGN_UP(sizeof(struct rptun_rsc_s), MAX(align0, align1));
       v0sz = ALIGN_UP(vring_size(rsc->rpmsg_vring0.num, align0), align0);
       v1sz = ALIGN_UP(vring_size(rsc->rpmsg_vring1.num, align1), align1);
 
-      va0 = (FAR char *)rsc + tbsz;
-      va1 = (FAR char *)rsc + tbsz + v0sz;
+      if (rsc->rpmsg_vring0.da == 0 || rsc->rpmsg_vring1.da == 0)
+        {
+          tbsz = ALIGN_UP(sizeof(struct rptun_rsc_s), MAX(align0, align1));
 
-      io  = metal_io_get_region();
-      pa0 = metal_io_virt_to_phys(io, va0);
-      pa1 = metal_io_virt_to_phys(io, va1);
+          va0 = (FAR char *)rsc + tbsz;
+          va1 = (FAR char *)rsc + tbsz + v0sz;
 
-      da0 = da1 = METAL_BAD_PHYS;
+          io  = metal_io_get_region();
+          pa0 = metal_io_virt_to_phys(io, va0);
+          pa1 = metal_io_virt_to_phys(io, va1);
 
-      remoteproc_mmap(rproc, &pa0, &da0, v0sz, 0, NULL);
-      remoteproc_mmap(rproc, &pa1, &da1, v1sz, 0, NULL);
+          da0 = da1 = METAL_BAD_PHYS;
 
-      rsc->rpmsg_vring0.da = da0;
-      rsc->rpmsg_vring1.da = da1;
+          remoteproc_mmap(rproc, &pa0, &da0, v0sz, 0, NULL);
+          remoteproc_mmap(rproc, &pa1, &da1, v1sz, 0, NULL);
 
-      shbuf   = (FAR char *)rsc + tbsz + v0sz + v1sz;
-      shbufsz = rsc->config.txbuf_size * rsc->rpmsg_vring0.num +
-                rsc->config.rxbuf_size * rsc->rpmsg_vring1.num;
+          rsc->rpmsg_vring0.da = da0;
+          rsc->rpmsg_vring1.da = da1;
 
-      rpmsg_virtio_init_shm_pool(&priv->shm_pool, shbuf, shbufsz);
+          shbuf   = (FAR char *)rsc + tbsz + v0sz + v1sz;
+          shbufsz = rsc->config.txbuf_size * rsc->rpmsg_vring0.num +
+                    rsc->config.rxbuf_size * rsc->rpmsg_vring1.num;
+
+          rpmsg_virtio_init_shm_pool(&priv->tx_shpool, shbuf, shbufsz);
+        }
+      else
+        {
+          da0 = rsc->rpmsg_vring0.da;
+          shbuf = (FAR char *)remoteproc_mmap(rproc, NULL, &da0,
+                                              v0sz, 0, NULL) + v0sz;
+          shbufsz = rsc->config.rxbuf_size * rsc->rpmsg_vring0.num;
+          rpmsg_virtio_init_shm_pool(&priv->rx_shpool, shbuf, shbufsz);
+
+          da1 = rsc->rpmsg_vring1.da;
+          shbuf = (FAR char *)remoteproc_mmap(rproc, NULL, &da1,
+                                              v1sz, 0, NULL) + v1sz;
+          shbufsz = rsc->config.txbuf_size * rsc->rpmsg_vring1.num;
+          rpmsg_virtio_init_shm_pool(&priv->tx_shpool, shbuf, shbufsz);
+        }
 
       role = RPMSG_MASTER;
     }
@@ -672,8 +691,18 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
       return -ENOMEM;
     }
 
-  ret = rpmsg_init_vdev(&priv->vdev, vdev, rptun_ns_bind,
-                        metal_io_get_region(), &priv->shm_pool);
+  if (priv->rx_shpool.base)
+    {
+      ret = rpmsg_init_vdev_ext(&priv->vdev, vdev, rptun_ns_bind,
+                                metal_io_get_region(),
+                                &priv->tx_shpool, &priv->rx_shpool);
+    }
+  else
+    {
+      ret = rpmsg_init_vdev(&priv->vdev, vdev, rptun_ns_bind,
+                            metal_io_get_region(), &priv->tx_shpool);
+    }
+
   if (ret)
     {
       remoteproc_remove_virtio(rproc, vdev);
@@ -967,7 +996,7 @@ int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
     {
       rptun_post(priv);
     }
-  
+
   return ret;
 }
 
@@ -1140,6 +1169,7 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
       goto err_driver;
     }
 #endif
+
   nxsem_set_protocol(&priv->sem, SEM_PRIO_NONE);
 
   return OK;

[incubator-nuttx] 09/13: rptun: pm_stay when send msg & pm_relax when all tx buffer returned

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 1c7e59eafa7e8bd9dbc74333ffee182047ff3d32
Author: ligd <li...@xiaomi.com>
AuthorDate: Wed Feb 9 23:08:22 2022 +0800

    rptun: pm_stay when send msg & pm_relax when all tx buffer returned
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/Kconfig | 10 +++++++++
 drivers/rptun/rptun.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/drivers/rptun/Kconfig b/drivers/rptun/Kconfig
index a3e4487..c1a2415 100644
--- a/drivers/rptun/Kconfig
+++ b/drivers/rptun/Kconfig
@@ -42,4 +42,14 @@ config RPTUN_LOCAL_CPUNAME
 	string "rptun local cpuname"
 	default LIBC_HOSTNAME
 
+config RPTUN_PM
+	bool "rptun power management"
+	depends on PM
+	default n
+	---help---
+		If TX/RX buffer is supplied and powered by each CPU.
+		And when one CPU in DEEP sleep, then it's buffer will
+		goto RAM-retention mode, can't access from another CPU.
+		So, we provide this method to resolve this.
+
 endif # RPTUN
diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index e4c8a24..f73d6a2 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -35,6 +35,7 @@
 #include <nuttx/semaphore.h>
 #include <nuttx/rptun/openamp.h>
 #include <nuttx/rptun/rptun.h>
+#include <nuttx/power/pm.h>
 #include <nuttx/wqueue.h>
 #include <metal/utilities.h>
 
@@ -43,15 +44,15 @@
  ****************************************************************************/
 
 #ifndef MAX
-#  define MAX(a,b)              ((a) > (b) ? (a) : (b))
+#  define MAX(a,b)                  ((a) > (b) ? (a) : (b))
 #endif
 
 #ifndef ALIGN_UP
-#  define ALIGN_UP(s, a)        (((s) + (a) - 1) & ~((a) - 1))
+#  define ALIGN_UP(s, a)            (((s) + (a) - 1) & ~((a) - 1))
 #endif
 
-#define RPTUNIOC_NONE           0
-#define NO_HOLDER               (INVALID_PROCESS_ID)
+#define RPTUNIOC_NONE               0
+#define NO_HOLDER                   (INVALID_PROCESS_ID)
 
 #define RPTUN_STATUS_FROM_MASTER    0x8
 #define RPTUN_STATUS_MASK           0x7
@@ -71,12 +72,15 @@ struct rptun_priv_s
   struct metal_list            bind;
   struct metal_list            node;
   sem_t                        sem;
+  unsigned long                cmd;
 #ifdef CONFIG_RPTUN_WORKQUEUE
   struct work_s                work;
 #else
   int                          tid;
 #endif
-  unsigned long                cmd;
+#ifdef CONFIG_RPTUN_PM
+  bool                         stay;
+#endif
 };
 
 struct rptun_bind_s
@@ -250,6 +254,39 @@ static void rptun_unlock(void)
     }
 }
 
+#ifdef CONFIG_RPTUN_PM
+static inline void rptun_pm_action(FAR struct rptun_priv_s *priv,
+                                   bool stay)
+{
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+
+  if (stay && !priv->stay)
+    {
+      pm_stay(0, PM_IDLE);
+      priv->stay = true;
+    }
+
+  if (!stay && priv->stay && !rpmsg_buffer_nused(&priv->rvdev, false))
+    {
+      pm_relax(0, PM_IDLE);
+      priv->stay = false;
+    }
+
+  leave_critical_section(flags);
+}
+
+static inline void rptun_enable_rx_kick(FAR struct rptun_priv_s *priv)
+{
+  virtqueue_enable_cb(priv->rvdev.svq);
+}
+
+#else
+#  define rptun_pm_action(priv, stay)
+#  define rptun_enable_rx_kick(priv)
+#endif
+
 static void rptun_worker(FAR void *arg)
 {
   FAR struct rptun_priv_s *priv = arg;
@@ -273,6 +310,8 @@ static void rptun_worker(FAR void *arg)
 
   priv->cmd = RPTUNIOC_NONE;
   remoteproc_get_notification(&priv->rproc, RPTUN_NOTIFY_ALL);
+
+  rptun_pm_action(priv, false);
 }
 
 static void rptun_post(FAR struct rptun_priv_s *priv)
@@ -417,6 +456,14 @@ static int rptun_stop(FAR struct remoteproc *rproc)
 static int rptun_notify(FAR struct remoteproc *rproc, uint32_t id)
 {
   FAR struct rptun_priv_s *priv = rproc->priv;
+  FAR struct rpmsg_virtio_device *rvdev = &priv->rvdev;
+  FAR struct virtqueue *vq = rvdev->svq;
+
+  if (rvdev->vdev && vq &&
+      rvdev->vdev->vrings_info[vq->vq_queue_index].notifyid == id)
+    {
+      rptun_pm_action(priv, true);
+    }
 
   RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
 
@@ -743,6 +790,7 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
 
   rptun_unlock();
 
+  rptun_enable_rx_kick(priv);
   return 0;
 }
 

[incubator-nuttx] 12/13: openamp: add openamp patches

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit bae9a97d311d0bb267e0ce8ddba3c17a564ba2cb
Author: ligd <li...@xiaomi.com>
AuthorDate: Mon Mar 28 20:29:18 2022 +0800

    openamp: add openamp patches
    
    0006-openamp-fix-scenario-case.patch
    0007-openamp-divide-shram-to-TX-shram-RX-shram.patch
    0008-rpmsg_virtio-don-t-need-check-status-when-get_tx_pay.patch
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 openamp/0006-openamp-fix-scenario-case.patch       |  41 ++++---
 ...openamp-divide-shram-to-TX-shram-RX-shram.patch | 130 +++++++++++++++++++++
 ...o-don-t-need-check-status-when-get_tx_pay.patch |  34 ++++++
 openamp/open-amp.defs                              |   2 +
 4 files changed, 186 insertions(+), 21 deletions(-)

diff --git a/openamp/0006-openamp-fix-scenario-case.patch b/openamp/0006-openamp-fix-scenario-case.patch
index a9fa95f..0134467 100644
--- a/openamp/0006-openamp-fix-scenario-case.patch
+++ b/openamp/0006-openamp-fix-scenario-case.patch
@@ -1,7 +1,7 @@
-From d14f87ea5ff1371318acaf4539116b72986bae2c Mon Sep 17 00:00:00 2001
+From c2b9d18c72eebe558c6a013bf39818c47cffecc3 Mon Sep 17 00:00:00 2001
 From: ligd <li...@xiaomi.com>
 Date: Thu, 18 Nov 2021 20:54:45 +0800
-Subject: [PATCH] openamp: fix scenario case
+Subject: [PATCH 1/3] openamp: fix scenario case
 
 description:
 There are two CPUs use IPC.
@@ -43,7 +43,6 @@ Resolve:
 add recursive call rpmsg_virtio_rx_callback() when get_tx_buffer() failed
 
 Signed-off-by: ligd <li...@xiaomi.com>
-Change-Id: I60512c50327f180a0aba891e3ec847b211e172db
 ---
  lib/include/openamp/remoteproc.h   |  10 +++
  lib/include/openamp/rpmsg_virtio.h |   6 ++
@@ -53,7 +52,7 @@ Change-Id: I60512c50327f180a0aba891e3ec847b211e172db
  5 files changed, 97 insertions(+), 66 deletions(-)
 
 diff --git a/lib/include/openamp/remoteproc.h open-amp/lib/include/openamp/remoteproc.h
-index a71327b..222079e 100644
+index a71327b..328ec3f 100644
 --- a/lib/include/openamp/remoteproc.h
 +++ open-amp/lib/include/openamp/remoteproc.h
 @@ -449,6 +449,16 @@ struct remoteproc_ops {
@@ -61,56 +60,56 @@ index a71327b..222079e 100644
  					  void *va, size_t size,
  					  struct remoteproc_mem *buf);
 +	/**
-+	 * can_recursive
++	 * wait_tx_buffer
 +	 *
-+	 * Check current vdev can do recursive rx_callabck or not
++	 * Wait tx buffer available
 +	 *
 +	 * @rproc - pointer to remoteproc instance
 +	 *
-+	 * return true if can do recursive rx_callabck.
++	 * return none
 +	 */
-+	int (*can_recursive)(struct remoteproc *rproc);
++	int (*wait_tx_buffer)(struct remoteproc *rproc);
  };
  
  /* Remoteproc error codes */
 diff --git a/lib/include/openamp/rpmsg_virtio.h open-amp/lib/include/openamp/rpmsg_virtio.h
-index e552b96..fa556a2 100644
+index e552b96..ac155fb 100644
 --- a/lib/include/openamp/rpmsg_virtio.h
 +++ open-amp/lib/include/openamp/rpmsg_virtio.h
 @@ -114,6 +114,12 @@ rpmsg_virtio_create_virtqueues(struct rpmsg_virtio_device *rvdev,
  					callbacks);
  }
  
-+static inline int rpmsg_virtio_can_recursive(struct rpmsg_virtio_device *rvdev)
++static inline int rpmsg_virtio_wait_tx_buffer(struct rpmsg_virtio_device *rvdev)
 +{
-+	return rvdev->vdev->func->can_recursive ?
-+			rvdev->vdev->func->can_recursive(rvdev->vdev) : 0;
++	return rvdev->vdev->func->wait_tx_buffer ?
++			rvdev->vdev->func->wait_tx_buffer(rvdev->vdev) : -EAGAIN;
 +}
 +
  /**
   * rpmsg_virtio_get_buffer_size - get rpmsg virtio buffer size
   *
 diff --git a/lib/include/openamp/virtio.h open-amp/lib/include/openamp/virtio.h
-index 55c8ea5..ce808cc 100644
+index 55c8ea5..34bccaa 100644
 --- a/lib/include/openamp/virtio.h
 +++ open-amp/lib/include/openamp/virtio.h
 @@ -137,6 +137,7 @@ struct virtio_dispatch {
  			     void *src, int length);
  	void (*reset_device)(struct virtio_device *dev);
  	void (*notify)(struct virtqueue *vq);
-+	int (*can_recursive)(struct virtio_device *dev);
++	int (*wait_tx_buffer)(struct virtio_device *dev);
  };
  
  int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags,
 diff --git a/lib/remoteproc/remoteproc_virtio.c open-amp/lib/remoteproc/remoteproc_virtio.c
-index ed9f33c..937f294 100644
+index ed9f33c..0de31ee 100644
 --- a/lib/remoteproc/remoteproc_virtio.c
 +++ open-amp/lib/remoteproc/remoteproc_virtio.c
 @@ -30,6 +30,17 @@ static void rproc_virtio_virtqueue_notify(struct virtqueue *vq)
  	rpvdev->notify(rpvdev->priv, vring_info->notifyid);
  }
  
-+static int rproc_virtio_can_recursive(struct virtio_device *vdev)
++static int rproc_virtio_wait_tx_buffer(struct virtio_device *vdev)
 +{
 +	struct remoteproc_virtio *rpvdev;
 +	struct remoteproc *rproc;
@@ -118,7 +117,7 @@ index ed9f33c..937f294 100644
 +	rpvdev = metal_container_of(vdev, struct remoteproc_virtio, vdev);
 +	rproc  = rpvdev->priv;
 +
-+	return rproc->ops->can_recursive ? rproc->ops->can_recursive(rproc) : 0;
++	return rproc->ops->wait_tx_buffer ? rproc->ops->wait_tx_buffer(rproc) : -EAGAIN;
 +}
 +
  static unsigned char rproc_virtio_get_status(struct virtio_device *vdev)
@@ -128,12 +127,12 @@ index ed9f33c..937f294 100644
  	.get_features = rproc_virtio_get_features,
  	.read_config = rproc_virtio_read_config,
  	.notify = rproc_virtio_virtqueue_notify,
-+	.can_recursive = rproc_virtio_can_recursive,
++	.wait_tx_buffer = rproc_virtio_wait_tx_buffer,
  #ifndef VIRTIO_SLAVE_ONLY
  	/*
  	 * We suppose here that the vdev is in a shared memory so that can
 diff --git a/lib/rpmsg/rpmsg_virtio.c open-amp/lib/rpmsg/rpmsg_virtio.c
-index 2687320..a407be9 100644
+index 2687320..30c3ab3 100644
 --- a/lib/rpmsg/rpmsg_virtio.c
 +++ open-amp/lib/rpmsg/rpmsg_virtio.c
 @@ -286,6 +286,72 @@ static void rpmsg_virtio_release_rx_buffer(struct rpmsg_device *rdev,
@@ -213,8 +212,8 @@ index 2687320..a407be9 100644
  		metal_mutex_release(&rdev->lock);
  		if (rp_hdr || !tick_count)
  			break;
-+		if (rpmsg_virtio_can_recursive(rvdev))
-+			rpmsg_virtio_rx_callback(rvdev->rvq);
++		if (rpmsg_virtio_wait_tx_buffer(rvdev) >= 0)
++			continue;
  		metal_sleep_usec(RPMSG_TICKS_PER_INTERVAL);
  		tick_count--;
  	}
diff --git a/openamp/0007-openamp-divide-shram-to-TX-shram-RX-shram.patch b/openamp/0007-openamp-divide-shram-to-TX-shram-RX-shram.patch
new file mode 100644
index 0000000..012b5ae
--- /dev/null
+++ b/openamp/0007-openamp-divide-shram-to-TX-shram-RX-shram.patch
@@ -0,0 +1,130 @@
+From 0de084b8b7c65502a2339221b59aeaa60edbe8c5 Mon Sep 17 00:00:00 2001
+From: ligd <li...@xiaomi.com>
+Date: Mon, 7 Feb 2022 20:36:25 +0800
+Subject: [PATCH 2/3] openamp: divide shram to TX shram & RX shram
+
+Add new API rpmsg_init_vdev_ext()
+
+Signed-off-by: ligd <li...@xiaomi.com>
+Change-Id: I790cdcf27276ed12a633fab9cc8aefff7c7da830
+---
+ lib/include/openamp/rpmsg_virtio.h | 30 +++++++++++++++++++++++++++++
+ lib/rpmsg/rpmsg_virtio.c           | 31 ++++++++++++++++++++----------
+ 2 files changed, 51 insertions(+), 10 deletions(-)
+
+diff --git a/lib/include/openamp/rpmsg_virtio.h open-amp/lib/include/openamp/rpmsg_virtio.h
+index ac155fb..9d6a16e 100644
+--- a/lib/include/openamp/rpmsg_virtio.h
++++ open-amp/lib/include/openamp/rpmsg_virtio.h
+@@ -156,6 +156,36 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
+ 		    struct metal_io_region *shm_io,
+ 		    struct rpmsg_virtio_shm_pool *shpool);
+ 
++/**
++ * rpmsg_init_vdev_ext - initialize rpmsg virtio device
++ * Master side:
++ * Initialize RPMsg virtio queues and shared buffers, the address of shm can be
++ * ANY. In this case, function will get shared memory from system shared memory
++ * pools. If the vdev has RPMsg name service feature, this API will create an
++ * name service endpoint.
++ *
++ * Slave side:
++ * This API will not return until the driver ready is set by the master side.
++ *
++ * @param rvdev  - pointer to the rpmsg virtio device
++ * @param vdev   - pointer to the virtio device
++ * @param ns_bind_cb  - callback handler for name service announcement without
++ *                      local endpoints waiting to bind.
++ * @param shm_io - pointer to the share memory I/O region.
++ * @param tx_shpool - pointer to tx shared memory pool. rpmsg_virtio_init_shm_pool
++ *                    has to be called first to fill this structure.
++ * @param rx_shpool - pointer to rx shared memory pool. rpmsg_virtio_init_shm_pool
++ *                    has to be called first to fill this structure.
++ *
++ * @return - status of function execution
++ */
++int rpmsg_init_vdev_ext(struct rpmsg_virtio_device *rvdev,
++			struct virtio_device *vdev,
++			rpmsg_ns_bind_cb ns_bind_cb,
++			struct metal_io_region *shm_io,
++			struct rpmsg_virtio_shm_pool *tx_shpool,
++			struct rpmsg_virtio_shm_pool *rx_shpool);
++
+ /**
+  * rpmsg_deinit_vdev - deinitialize rpmsg virtio device
+  *
+diff --git a/lib/rpmsg/rpmsg_virtio.c open-amp/lib/rpmsg/rpmsg_virtio.c
+index 30c3ab3..8a2c795 100644
+--- a/lib/rpmsg/rpmsg_virtio.c
++++ open-amp/lib/rpmsg/rpmsg_virtio.c
+@@ -604,11 +604,12 @@ int rpmsg_virtio_get_buffer_size(struct rpmsg_device *rdev)
+ 	return size;
+ }
+ 
+-int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
+-		    struct virtio_device *vdev,
+-		    rpmsg_ns_bind_cb ns_bind_cb,
+-		    struct metal_io_region *shm_io,
+-		    struct rpmsg_virtio_shm_pool *shpool)
++int rpmsg_init_vdev_ext(struct rpmsg_virtio_device *rvdev,
++			struct virtio_device *vdev,
++			rpmsg_ns_bind_cb ns_bind_cb,
++			struct metal_io_region *shm_io,
++			struct rpmsg_virtio_shm_pool *tx_shpool,
++			struct rpmsg_virtio_shm_pool *rx_shpool)
+ {
+ 	struct rpmsg_device *rdev;
+ 	const char *vq_names[RPMSG_NUM_VRINGS];
+@@ -654,11 +655,11 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
+ 		 * Since device is RPMSG Remote so we need to manage the
+ 		 * shared buffers. Create shared memory pool to handle buffers.
+ 		 */
+-		if (!shpool)
++		if (!tx_shpool || !rx_shpool)
+ 			return RPMSG_ERR_PARAM;
+-		if (!shpool->size)
++		if (!tx_shpool->size || !rx_shpool->size)
+ 			return RPMSG_ERR_NO_BUFF;
+-		rvdev->shpool = shpool;
++		rvdev->shpool = tx_shpool;
+ 
+ 		vq_names[0] = "rx_vq";
+ 		vq_names[1] = "tx_vq";
+@@ -670,7 +671,8 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
+ #endif /*!VIRTIO_SLAVE_ONLY*/
+ 
+ #ifndef VIRTIO_MASTER_ONLY
+-	(void)shpool;
++	(void)tx_shpool;
++	(void)rx_shpool;
+ 	if (role == RPMSG_REMOTE) {
+ 		vq_names[0] = "tx_vq";
+ 		vq_names[1] = "rx_vq";
+@@ -711,7 +713,7 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
+ 		vqbuf.len = rvdev->config.txbuf_size;
+ 		for (idx = 0; idx < rvdev->rvq->vq_nentries; idx++) {
+ 			/* Initialize TX virtqueue buffers for remote device */
+-			buffer = rpmsg_virtio_shm_pool_get_buffer(shpool,
++			buffer = rpmsg_virtio_shm_pool_get_buffer(rx_shpool,
+ 						rvdev->config.txbuf_size);
+ 
+ 			if (!buffer) {
+@@ -757,6 +759,15 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
+ 	return status;
+ }
+ 
++int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
++		    struct virtio_device *vdev,
++		    rpmsg_ns_bind_cb ns_bind_cb,
++		    struct metal_io_region *shm_io,
++		    struct rpmsg_virtio_shm_pool *shpool)
++{
++	return rpmsg_init_vdev_ext(rvdev, vdev, ns_bind_cb, shm_io, shpool, shpool);
++}
++
+ void rpmsg_deinit_vdev(struct rpmsg_virtio_device *rvdev)
+ {
+ 	struct metal_list *node;
+-- 
+2.25.1
+
diff --git a/openamp/0008-rpmsg_virtio-don-t-need-check-status-when-get_tx_pay.patch b/openamp/0008-rpmsg_virtio-don-t-need-check-status-when-get_tx_pay.patch
new file mode 100644
index 0000000..e003c76
--- /dev/null
+++ b/openamp/0008-rpmsg_virtio-don-t-need-check-status-when-get_tx_pay.patch
@@ -0,0 +1,34 @@
+From 0e2e59941d9694e060f8ac06fce6b8284d8e74ec Mon Sep 17 00:00:00 2001
+From: ligd <li...@xiaomi.com>
+Date: Mon, 28 Feb 2022 16:31:54 +0800
+Subject: [PATCH 3/3] rpmsg_virtio: don't need check status when get_tx_payload
+
+Signed-off-by: ligd <li...@xiaomi.com>
+---
+ lib/rpmsg/rpmsg_virtio.c | 6 ------
+ 1 file changed, 6 deletions(-)
+
+diff --git a/lib/rpmsg/rpmsg_virtio.c open-amp/lib/rpmsg/rpmsg_virtio.c
+index 8a2c795..b95bf6d 100644
+--- a/lib/rpmsg/rpmsg_virtio.c
++++ open-amp/lib/rpmsg/rpmsg_virtio.c
+@@ -359,16 +359,10 @@ static void *rpmsg_virtio_get_tx_payload_buffer(struct rpmsg_device *rdev,
+ 	struct rpmsg_hdr *rp_hdr;
+ 	uint16_t idx;
+ 	int tick_count;
+-	int status;
+ 
+ 	/* Get the associated remote device for channel. */
+ 	rvdev = metal_container_of(rdev, struct rpmsg_virtio_device, rdev);
+ 
+-	/* Validate device state */
+-	status = rpmsg_virtio_get_status(rvdev);
+-	if (!(status & VIRTIO_CONFIG_STATUS_DRIVER_OK))
+-		return NULL;
+-
+ 	if (wait)
+ 		tick_count = RPMSG_TICK_COUNT / RPMSG_TICKS_PER_INTERVAL;
+ 	else
+-- 
+2.25.1
+
diff --git a/openamp/open-amp.defs b/openamp/open-amp.defs
index cf0dd8d..8c0053c 100644
--- a/openamp/open-amp.defs
+++ b/openamp/open-amp.defs
@@ -39,6 +39,8 @@ open-amp.zip:
 	$(Q) patch -p0 < 0004-openamp-add-ns_unbind_notify-support.patch
 	$(Q) patch -p0 < 0005-rpmsg-notify-the-user-when-the-remote-address-is-rec.patch
 	$(Q) patch -p0 < 0006-openamp-fix-scenario-case.patch
+	$(Q) patch -p0 < 0007-openamp-divide-shram-to-TX-shram-RX-shram.patch
+	$(Q) patch -p0 < 0008-rpmsg_virtio-don-t-need-check-status-when-get_tx_pay.patch
 
 .openamp_headers: open-amp.zip
 	$(eval headers := $(wildcard open-amp/lib/include/openamp/*.h))

[incubator-nuttx] 03/13: rptun: add rptun_reset support

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3ab7ade4ba021ee9fbe30b7b0dea8f99c8e4ee3d
Author: ligd <li...@xiaomi.com>
AuthorDate: Fri Dec 24 16:33:07 2021 +0800

    rptun: add rptun_reset support
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/rptun.c       | 55 ++++++++++++++++++++++++++++++++-------------
 include/nuttx/rptun/rptun.h |  4 +++-
 2 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index cc86584..1ff4743 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -29,6 +29,7 @@
 #include <fcntl.h>
 
 #include <nuttx/arch.h>
+#include <nuttx/board.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/kthread.h>
 #include <nuttx/semaphore.h>
@@ -51,6 +52,9 @@
 #define RPTUNIOC_NONE           0
 #define NO_HOLDER               (INVALID_PROCESS_ID)
 
+#define RPTUN_STATUS_MASK       0xf
+#define RPTUN_STATUS_PANIC      0xf
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -116,7 +120,7 @@ static void rptun_ns_bind(FAR struct rpmsg_device *rdev,
 
 static int rptun_dev_start(FAR struct remoteproc *rproc);
 static int rptun_dev_stop(FAR struct remoteproc *rproc);
-static int rptun_dev_panic(FAR struct remoteproc *rproc);
+static int rptun_dev_reset(FAR struct remoteproc *rproc, int value);
 static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
                            unsigned long arg);
 
@@ -266,13 +270,6 @@ static int rptun_thread(int argc, FAR char *argv[])
                 rptun_dev_stop(&priv->rproc);
               }
             break;
-
-          case RPTUNIOC_PANIC:
-            if (priv->rproc.state != RPROC_OFFLINE)
-              {
-                rptun_dev_panic(&priv->rproc);
-              }
-            break;
         }
 
         priv->cmd = RPTUNIOC_NONE;
@@ -300,9 +297,20 @@ static int rptun_callback(FAR void *arg, uint32_t vqid)
   if (!RPTUN_IS_MASTER(priv->dev))
     {
       int status = rpmsg_virtio_get_status(&priv->vdev);
+
       if (status & VIRTIO_CONFIG_STATUS_NEEDS_RESET)
         {
-          PANIC();
+          status &= RPTUN_STATUS_MASK;
+          if (status == RPTUN_STATUS_PANIC)
+            {
+              PANIC();
+            }
+          else
+            {
+#ifdef CONFIG_BOARDCTL_RESET
+              board_reset(status);
+#endif
+            }
         }
     }
 
@@ -691,12 +699,14 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc)
   return 0;
 }
 
-static int rptun_dev_panic(FAR struct remoteproc *rproc)
+static int rptun_dev_reset(FAR struct remoteproc *rproc, int value)
 {
   FAR struct rptun_priv_s *priv = rproc->priv;
 
-  rpmsg_virtio_set_status(&priv->vdev, VIRTIO_CONFIG_STATUS_NEEDS_RESET);
-  RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
+  rpmsg_virtio_set_status(&priv->vdev,
+          (value & RPTUN_STATUS_MASK) | VIRTIO_CONFIG_STATUS_NEEDS_RESET);
+
+  return RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
 }
 
 static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
@@ -710,11 +720,15 @@ static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
     {
       case RPTUNIOC_START:
       case RPTUNIOC_STOP:
-      case RPTUNIOC_PANIC:
         priv->cmd = cmd;
         rptun_wakeup(priv);
         break;
-
+      case RPTUNIOC_RESET:
+        rptun_dev_reset(&priv->rproc, arg);
+        break;
+      case RPTUNIOC_PANIC:
+        rptun_dev_reset(&priv->rproc, RPTUN_STATUS_PANIC);
+        break;
       default:
         ret = -ENOTTY;
         break;
@@ -1031,10 +1045,15 @@ int rptun_boot(FAR const char *cpuname)
   return ret;
 }
 
-int rptun_panic(FAR const char *cpuname)
+int rptun_reset(FAR const char *cpuname, int value)
 {
   FAR struct metal_list *node;
 
+  if (!cpuname)
+    {
+      return -EINVAL;
+    }
+
   metal_list_for_each(&g_rptun_priv, node)
     {
       FAR struct rptun_priv_s *priv;
@@ -1044,10 +1063,14 @@ int rptun_panic(FAR const char *cpuname)
       if (RPTUN_IS_MASTER(priv->dev) &&
           !strcmp(RPTUN_GET_CPUNAME(priv->dev), cpuname))
         {
-          rptun_dev_panic(&priv->rproc);
+          rptun_dev_reset(&priv->rproc, value);
         }
     }
 
   return -ENOENT;
 }
 
+int rptun_panic(FAR const char *cpuname)
+{
+  return rptun_reset(cpuname, RPTUN_STATUS_PANIC);
+}
diff --git a/include/nuttx/rptun/rptun.h b/include/nuttx/rptun/rptun.h
index ef2103a..f51d9c6 100644
--- a/include/nuttx/rptun/rptun.h
+++ b/include/nuttx/rptun/rptun.h
@@ -38,7 +38,8 @@
 
 #define RPTUNIOC_START              _RPTUNIOC(1)
 #define RPTUNIOC_STOP               _RPTUNIOC(2)
-#define RPTUNIOC_PANIC              _RPTUNIOC(3)
+#define RPTUNIOC_RESET              _RPTUNIOC(3)
+#define RPTUNIOC_PANIC              _RPTUNIOC(4)
 
 #define RPTUN_NOTIFY_ALL            (UINT32_MAX - 0)
 
@@ -315,6 +316,7 @@ extern "C"
 
 int rptun_initialize(FAR struct rptun_dev_s *dev);
 int rptun_boot(FAR const char *cpuname);
+int rptun_reset(FAR const char *cpuname, int value);
 int rptun_panic(FAR const char *cpuname);
 
 #ifdef __cplusplus

[incubator-nuttx] 04/13: rptun: use HPWORK instead of thread

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c9404e27056085ed064beeebcd315e620890ffb7
Author: ligd <li...@xiaomi.com>
AuthorDate: Tue Dec 28 20:13:28 2021 +0800

    rptun: use HPWORK instead of thread
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/Kconfig |  20 +++++++++-
 drivers/rptun/rptun.c | 103 ++++++++++++++++++++++++++++++++++----------------
 2 files changed, 89 insertions(+), 34 deletions(-)

diff --git a/drivers/rptun/Kconfig b/drivers/rptun/Kconfig
index 0e8ca6b..d7bf6b5 100644
--- a/drivers/rptun/Kconfig
+++ b/drivers/rptun/Kconfig
@@ -12,14 +12,32 @@ menuconfig RPTUN
 
 if RPTUN
 
+choice
+	prompt "rptun dispatch method"
+
+config RPTUN_THREAD
+	bool "rptun thread"
+
+config RPTUN_WORKQUEUE
+	bool "rptun workqueue"
+	depends on SCHED_WORKQUEUE
+
+endchoice
+
 config RPTUN_PRIORITY
-	int "rpturn thread priority"
+	int "rptun thread priority"
+	depends on RPTUN_THREAD
 	default 224
 
 config RPTUN_STACKSIZE
 	int "rptun stack size"
+	depends on RPTUN_THREAD
 	default 4096
 
+config RPTUN_RECURSIVE_DISPATCH
+	bool "rptun recursive dispatch"
+	default y
+
 config RPTUN_LOADER
 	bool "rptun loader support"
 	default n
diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 1ff4743..3187759 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -35,6 +35,7 @@
 #include <nuttx/semaphore.h>
 #include <nuttx/rptun/openamp.h>
 #include <nuttx/rptun/rptun.h>
+#include <nuttx/wqueue.h>
 #include <metal/utilities.h>
 
 /****************************************************************************
@@ -67,8 +68,12 @@ struct rptun_priv_s
   struct rpmsg_virtio_shm_pool shm_pool;
   struct metal_list            bind;
   struct metal_list            node;
+#ifdef CONFIG_RPTUN_WORKQUEUE
+  struct work_s                work;
+#else
   sem_t                        sem;
   int                          tid;
+#endif
   unsigned long                cmd;
 };
 
@@ -243,6 +248,37 @@ static void rptun_unlock(void)
     }
 }
 
+static void rptun_worker(FAR void *arg)
+{
+  FAR struct rptun_priv_s *priv = arg;
+
+  switch (priv->cmd)
+    {
+      case RPTUNIOC_START:
+        if (priv->rproc.state == RPROC_OFFLINE)
+          {
+            rptun_dev_start(&priv->rproc);
+          }
+        break;
+
+      case RPTUNIOC_STOP:
+        if (priv->rproc.state != RPROC_OFFLINE)
+          {
+            rptun_dev_stop(&priv->rproc);
+          }
+        break;
+    }
+
+  priv->cmd = RPTUNIOC_NONE;
+  remoteproc_get_notification(&priv->rproc, RPTUN_NOTIFY_ALL);
+}
+
+#ifdef CONFIG_RPTUN_WORKQUEUE
+static void rptun_wakeup(FAR struct rptun_priv_s *priv)
+{
+  work_queue(HPWORK, &priv->work, rptun_worker, priv, 0);
+}
+#else
 static int rptun_thread(int argc, FAR char *argv[])
 {
   FAR struct rptun_priv_s *priv;
@@ -250,30 +286,10 @@ static int rptun_thread(int argc, FAR char *argv[])
   priv = (FAR struct rptun_priv_s *)((uintptr_t)strtoul(argv[2], NULL, 0));
   priv->tid = gettid();
 
-  remoteproc_init(&priv->rproc, &g_rptun_ops, priv);
-
   while (1)
     {
       nxsem_wait_uninterruptible(&priv->sem);
-      switch (priv->cmd)
-        {
-          case RPTUNIOC_START:
-            if (priv->rproc.state == RPROC_OFFLINE)
-              {
-                rptun_dev_start(&priv->rproc);
-              }
-            break;
-
-          case RPTUNIOC_STOP:
-            if (priv->rproc.state != RPROC_OFFLINE)
-              {
-                rptun_dev_stop(&priv->rproc);
-              }
-            break;
-        }
-
-        priv->cmd = RPTUNIOC_NONE;
-        remoteproc_get_notification(&priv->rproc, RPTUN_NOTIFY_ALL);
+      rptun_worker(priv);
     }
 
   return 0;
@@ -289,6 +305,7 @@ static void rptun_wakeup(FAR struct rptun_priv_s *priv)
       nxsem_post(&priv->sem);
     }
 }
+#endif
 
 static int rptun_callback(FAR void *arg, uint32_t vqid)
 {
@@ -419,9 +436,14 @@ rptun_get_mem(FAR struct remoteproc *rproc,
 
 static int rptun_can_recursive(FAR struct remoteproc *rproc)
 {
+#ifndef CONFIG_RPTUN_RECURSIVE_DISPATCH
+  return false;
+#elif defined(CONFIG_RPTUN_WORKQUEUE)
+  return work_in_context(HPWORK);
+#else
   FAR struct rptun_priv_s *priv = rproc->priv;
-
   return gettid() == priv->tid;
+#endif
 }
 
 static void *rptun_get_priv_by_rdev(FAR struct rpmsg_device *rdev)
@@ -960,8 +982,10 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
 {
   struct metal_init_params params = METAL_INIT_DEFAULTS;
   FAR struct rptun_priv_s *priv;
+#ifndef CONFIG_RPTUN_WORKQUEUE
   FAR char *argv[3];
   char arg1[19];
+#endif
   char name[32];
   int ret;
 
@@ -979,14 +1003,9 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
     }
 
   priv->dev = dev;
-  if (RPTUN_IS_AUTOSTART(dev))
-    {
-      priv->cmd = RPTUNIOC_START;
-    }
 
+  remoteproc_init(&priv->rproc, &g_rptun_ops, priv);
   metal_list_init(&priv->bind);
-  nxsem_init(&priv->sem, 0, RPTUN_IS_AUTOSTART(dev) ? 1 : 0);
-  nxsem_set_protocol(&priv->sem, SEM_PRIO_NONE);
 
   snprintf(name, sizeof(name), "/dev/rptun/%s", RPTUN_GET_CPUNAME(dev));
   ret = register_driver(name, &g_rptun_devops, 0222, priv);
@@ -995,6 +1014,25 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
       goto err_driver;
     }
 
+#ifdef CONFIG_RPTUN_WORKQUEUE
+  if (RPTUN_IS_AUTOSTART(dev))
+    {
+      priv->cmd = RPTUNIOC_START;
+      work_queue(HPWORK, &priv->work, rptun_worker, priv, 0);
+    }
+#else
+  if (RPTUN_IS_AUTOSTART(dev))
+    {
+      priv->cmd = RPTUNIOC_START;
+      nxsem_init(&priv->sem, 0, 1);
+    }
+  else
+    {
+      nxsem_init(&priv->sem, 0, 0);
+    }
+
+  nxsem_set_protocol(&priv->sem, SEM_PRIO_NONE);
+
   snprintf(arg1, sizeof(arg1), "0x%" PRIxPTR, (uintptr_t)priv);
   argv[0] = (void *)RPTUN_GET_CPUNAME(dev);
   argv[1] = arg1;
@@ -1004,16 +1042,15 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
                        CONFIG_RPTUN_STACKSIZE, rptun_thread, argv);
   if (ret < 0)
     {
-      goto err_thread;
+      unregister_driver(name);
+      nxsem_destroy(&priv->sem);
+      goto err_driver;
     }
+#endif
 
   return OK;
 
-err_thread:
-  unregister_driver(name);
-
 err_driver:
-  nxsem_destroy(&priv->sem);
   kmm_free(priv);
 
 err_mem:

[incubator-nuttx] 02/13: rptun: add rptun_force_panic support

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 7aba7c3790d38e2d10fba7428a753002ea64aa5b
Author: ligd <li...@xiaomi.com>
AuthorDate: Mon Dec 20 15:52:00 2021 +0800

    rptun: add rptun_force_panic support
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/rptun.c       | 51 ++++++++++++++++++++++++++++++++++++++++++++-
 include/nuttx/rptun/rptun.h |  2 ++
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 3186f7c..cc86584 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -116,6 +116,7 @@ static void rptun_ns_bind(FAR struct rpmsg_device *rdev,
 
 static int rptun_dev_start(FAR struct remoteproc *rproc);
 static int rptun_dev_stop(FAR struct remoteproc *rproc);
+static int rptun_dev_panic(FAR struct remoteproc *rproc);
 static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
                            unsigned long arg);
 
@@ -265,6 +266,13 @@ static int rptun_thread(int argc, FAR char *argv[])
                 rptun_dev_stop(&priv->rproc);
               }
             break;
+
+          case RPTUNIOC_PANIC:
+            if (priv->rproc.state != RPROC_OFFLINE)
+              {
+                rptun_dev_panic(&priv->rproc);
+              }
+            break;
         }
 
         priv->cmd = RPTUNIOC_NONE;
@@ -287,7 +295,18 @@ static void rptun_wakeup(FAR struct rptun_priv_s *priv)
 
 static int rptun_callback(FAR void *arg, uint32_t vqid)
 {
-  rptun_wakeup(arg);
+  FAR struct rptun_priv_s *priv = arg;
+
+  if (!RPTUN_IS_MASTER(priv->dev))
+    {
+      int status = rpmsg_virtio_get_status(&priv->vdev);
+      if (status & VIRTIO_CONFIG_STATUS_NEEDS_RESET)
+        {
+          PANIC();
+        }
+    }
+
+  rptun_wakeup(priv);
   return OK;
 }
 
@@ -672,6 +691,14 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc)
   return 0;
 }
 
+static int rptun_dev_panic(FAR struct remoteproc *rproc)
+{
+  FAR struct rptun_priv_s *priv = rproc->priv;
+
+  rpmsg_virtio_set_status(&priv->vdev, VIRTIO_CONFIG_STATUS_NEEDS_RESET);
+  RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
+}
+
 static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
                            unsigned long arg)
 {
@@ -683,6 +710,7 @@ static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
     {
       case RPTUNIOC_START:
       case RPTUNIOC_STOP:
+      case RPTUNIOC_PANIC:
         priv->cmd = cmd;
         rptun_wakeup(priv);
         break;
@@ -1002,3 +1030,24 @@ int rptun_boot(FAR const char *cpuname)
 
   return ret;
 }
+
+int rptun_panic(FAR const char *cpuname)
+{
+  FAR struct metal_list *node;
+
+  metal_list_for_each(&g_rptun_priv, node)
+    {
+      FAR struct rptun_priv_s *priv;
+
+      priv = metal_container_of(node, struct rptun_priv_s, node);
+
+      if (RPTUN_IS_MASTER(priv->dev) &&
+          !strcmp(RPTUN_GET_CPUNAME(priv->dev), cpuname))
+        {
+          rptun_dev_panic(&priv->rproc);
+        }
+    }
+
+  return -ENOENT;
+}
+
diff --git a/include/nuttx/rptun/rptun.h b/include/nuttx/rptun/rptun.h
index cb4f991..ef2103a 100644
--- a/include/nuttx/rptun/rptun.h
+++ b/include/nuttx/rptun/rptun.h
@@ -38,6 +38,7 @@
 
 #define RPTUNIOC_START              _RPTUNIOC(1)
 #define RPTUNIOC_STOP               _RPTUNIOC(2)
+#define RPTUNIOC_PANIC              _RPTUNIOC(3)
 
 #define RPTUN_NOTIFY_ALL            (UINT32_MAX - 0)
 
@@ -314,6 +315,7 @@ extern "C"
 
 int rptun_initialize(FAR struct rptun_dev_s *dev);
 int rptun_boot(FAR const char *cpuname);
+int rptun_panic(FAR const char *cpuname);
 
 #ifdef __cplusplus
 }

[incubator-nuttx] 06/13: rptun: add rpmsg_wait/post support for recursive dispatch

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit be49f6b87c40248a93129458c34e02a1cf5a126e
Author: ligd <li...@xiaomi.com>
AuthorDate: Tue Jan 18 15:51:24 2022 +0800

    rptun: add rpmsg_wait/post support for recursive dispatch
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/Kconfig         |   4 --
 drivers/rptun/rptun.c         | 152 +++++++++++++++++++++++++++++++++---------
 include/nuttx/rptun/openamp.h |   3 +
 3 files changed, 125 insertions(+), 34 deletions(-)

diff --git a/drivers/rptun/Kconfig b/drivers/rptun/Kconfig
index d7bf6b5..a3e4487 100644
--- a/drivers/rptun/Kconfig
+++ b/drivers/rptun/Kconfig
@@ -34,10 +34,6 @@ config RPTUN_STACKSIZE
 	depends on RPTUN_THREAD
 	default 4096
 
-config RPTUN_RECURSIVE_DISPATCH
-	bool "rptun recursive dispatch"
-	default y
-
 config RPTUN_LOADER
 	bool "rptun loader support"
 	default n
diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 3323e66..f016b02 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -69,10 +69,10 @@ struct rptun_priv_s
   struct rpmsg_virtio_shm_pool shm_pool;
   struct metal_list            bind;
   struct metal_list            node;
+  sem_t                        sem;
 #ifdef CONFIG_RPTUN_WORKQUEUE
   struct work_s                work;
 #else
-  sem_t                        sem;
   int                          tid;
 #endif
   unsigned long                cmd;
@@ -119,7 +119,7 @@ rptun_get_mem(FAR struct remoteproc *rproc,
               metal_phys_addr_t da,
               FAR void *va, size_t size,
               FAR struct remoteproc_mem *buf);
-static int rptun_can_recursive(FAR struct remoteproc *rproc);
+static int rptun_wait_tx_buffer(FAR struct remoteproc *rproc);
 
 static void rptun_ns_bind(FAR struct rpmsg_device *rdev,
                           FAR const char *name, uint32_t dest);
@@ -152,14 +152,14 @@ static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
 
 static struct remoteproc_ops g_rptun_ops =
 {
-  .init          = rptun_init,
-  .remove        = rptun_remove,
-  .config        = rptun_config,
-  .start         = rptun_start,
-  .stop          = rptun_stop,
-  .notify        = rptun_notify,
-  .get_mem       = rptun_get_mem,
-  .can_recursive = rptun_can_recursive,
+  .init           = rptun_init,
+  .remove         = rptun_remove,
+  .config         = rptun_config,
+  .start          = rptun_start,
+  .stop           = rptun_stop,
+  .notify         = rptun_notify,
+  .get_mem        = rptun_get_mem,
+  .wait_tx_buffer = rptun_wait_tx_buffer,
 };
 
 static const struct file_operations g_rptun_devops =
@@ -274,11 +274,39 @@ static void rptun_worker(FAR void *arg)
   remoteproc_get_notification(&priv->rproc, RPTUN_NOTIFY_ALL);
 }
 
+static void rptun_post(FAR struct rptun_priv_s *priv)
+{
+  int semcount;
+
+  nxsem_get_value(&priv->sem, &semcount);
+  while (semcount++ < 1)
+    {
+      nxsem_post(&priv->sem);
+    }
+}
+
 #ifdef CONFIG_RPTUN_WORKQUEUE
 static void rptun_wakeup(FAR struct rptun_priv_s *priv)
 {
   work_queue(HPWORK, &priv->work, rptun_worker, priv, 0);
+  rptun_post(priv);
+}
+
+static void rptun_in_recursive(int tid, FAR void *arg)
+{
+  if (gettid() == tid)
+    {
+      *((FAR bool *)arg) = true;
+    }
+}
+
+static bool rptun_is_recursive(FAR struct rptun_priv_s *priv)
+{
+  bool in = false;
+  work_foreach(HPWORK, rptun_in_recursive, &in);
+  return in;
 }
+
 #else
 static int rptun_thread(int argc, FAR char *argv[])
 {
@@ -298,13 +326,12 @@ static int rptun_thread(int argc, FAR char *argv[])
 
 static void rptun_wakeup(FAR struct rptun_priv_s *priv)
 {
-  int semcount;
+  rptun_post(priv);
+}
 
-  nxsem_get_value(&priv->sem, &semcount);
-  if (semcount < 1)
-    {
-      nxsem_post(&priv->sem);
-    }
+static bool rptun_is_recursive(FAR struct rptun_priv_s *priv)
+{
+  return gettid() == priv->tid;
 }
 #endif
 
@@ -434,16 +461,21 @@ rptun_get_mem(FAR struct remoteproc *rproc,
   return buf;
 }
 
-static int rptun_can_recursive(FAR struct remoteproc *rproc)
+static int rptun_wait_tx_buffer(FAR struct remoteproc *rproc)
 {
-#ifndef CONFIG_RPTUN_RECURSIVE_DISPATCH
-  return false;
-#elif defined(CONFIG_RPTUN_WORKQUEUE)
-  return work_in_context(HPWORK);
-#else
   FAR struct rptun_priv_s *priv = rproc->priv;
-  return gettid() == priv->tid;
-#endif
+
+  if (!rptun_is_recursive(priv))
+    {
+      return -EAGAIN;
+    }
+
+  /* Wait to wakeup */
+
+  nxsem_wait(&priv->sem);
+  rptun_worker(priv);
+
+  return 0;
 }
 
 static void *rptun_get_priv_by_rdev(FAR struct rpmsg_device *rdev)
@@ -453,6 +485,11 @@ static void *rptun_get_priv_by_rdev(FAR struct rpmsg_device *rdev)
   struct remoteproc_virtio *rpvdev;
   struct remoteproc *rproc;
 
+  if (!rdev)
+    {
+      return NULL;
+    }
+
   rvdev = metal_container_of(rdev, struct rpmsg_virtio_device, rdev);
   vdev  = rvdev->vdev;
   if (!vdev)
@@ -656,6 +693,10 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
 
   rptun_lock();
 
+  /* Register callback to mbox for receiving remote message */
+
+  RPTUN_REGISTER_CALLBACK(priv->dev, rptun_callback, priv);
+
   /* Add priv to list */
 
   metal_list_add_tail(&g_rptun_priv, &priv->node);
@@ -673,10 +714,6 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
 
   rptun_unlock();
 
-  /* Register callback to mbox for receiving remote message */
-
-  RPTUN_REGISTER_CALLBACK(priv->dev, rptun_callback, priv);
-
   return 0;
 }
 
@@ -880,6 +917,60 @@ static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
  * Public Functions
  ****************************************************************************/
 
+int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
+{
+  FAR struct rptun_priv_s *priv;
+  int ret;
+
+  if (!ept)
+    {
+      return -EINVAL;
+    }
+
+  priv = rptun_get_priv_by_rdev(ept->rdev);
+  if (!priv || !rptun_is_recursive(priv))
+    {
+      return nxsem_wait_uninterruptible(sem);
+    }
+
+  while (1)
+    {
+      ret = nxsem_trywait(sem);
+      if (ret >= 0)
+        {
+          break;
+        }
+
+      nxsem_wait(&priv->sem);
+      rptun_worker(priv);
+    }
+
+  return ret;
+}
+
+int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
+{
+  FAR struct rptun_priv_s *priv;
+  int semcount;
+  int ret;
+
+  if (!ept)
+    {
+      return -EINVAL;
+    }
+
+  nxsem_get_value(sem, &semcount);
+  ret = nxsem_post(sem);
+
+  priv = rptun_get_priv_by_rdev(ept->rdev);
+  if (priv && semcount >= 0)
+    {
+      rptun_post(priv);
+    }
+  
+  return ret;
+}
+
 FAR const char *rpmsg_get_cpuname(FAR struct rpmsg_device *rdev)
 {
   FAR struct rptun_priv_s *priv = rptun_get_priv_by_rdev(rdev);
@@ -1022,6 +1113,8 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
       priv->cmd = RPTUNIOC_START;
       work_queue(HPWORK, &priv->work, rptun_worker, priv, 0);
     }
+
+  nxsem_init(&priv->sem, 0, 0);
 #else
   if (RPTUN_IS_AUTOSTART(dev))
     {
@@ -1033,8 +1126,6 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
       nxsem_init(&priv->sem, 0, 0);
     }
 
-  nxsem_set_protocol(&priv->sem, SEM_PRIO_NONE);
-
   snprintf(arg1, sizeof(arg1), "0x%" PRIxPTR, (uintptr_t)priv);
   argv[0] = (void *)RPTUN_GET_CPUNAME(dev);
   argv[1] = arg1;
@@ -1049,6 +1140,7 @@ int rptun_initialize(FAR struct rptun_dev_s *dev)
       goto err_driver;
     }
 #endif
+  nxsem_set_protocol(&priv->sem, SEM_PRIO_NONE);
 
   return OK;
 
diff --git a/include/nuttx/rptun/openamp.h b/include/nuttx/rptun/openamp.h
index 8a96b98..365e2bc 100644
--- a/include/nuttx/rptun/openamp.h
+++ b/include/nuttx/rptun/openamp.h
@@ -54,6 +54,9 @@ extern "C"
 #define EXTERN extern
 #endif
 
+int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem);
+int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem);
+
 const char *rpmsg_get_cpuname(FAR struct rpmsg_device *rdev);
 int rpmsg_register_callback(FAR void *priv,
                             rpmsg_dev_cb_t device_created,

[incubator-nuttx] 13/13: rptun: forward panic/reset to lowerhalf

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 86a496d6ffc3556e18b65afb1a8f66955f5d5bbc
Author: ligd <li...@xiaomi.com>
AuthorDate: Mon Mar 14 17:23:02 2022 +0800

    rptun: forward panic/reset to lowerhalf
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/rptun.c       | 146 +++++++++++++++-----------------------------
 include/nuttx/rptun/rptun.h |  38 ++++++++++++
 2 files changed, 88 insertions(+), 96 deletions(-)

diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 949cf97..3a6aae7 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -56,9 +56,10 @@
 #define RPTUNIOC_NONE               0
 #define NO_HOLDER                   (INVALID_PROCESS_ID)
 
-#define RPTUN_STATUS_FROM_MASTER    0x8
-#define RPTUN_STATUS_MASK           0x7
-#define RPTUN_STATUS_PANIC          0x7
+#define RPTUN_OPS_START             0
+#define RPTUN_OPS_DUMP              1
+#define RPTUN_OPS_RESET             2
+#define RPTUN_OPS_PANIC             3
 
 /****************************************************************************
  * Private Types
@@ -78,7 +79,7 @@ struct rptun_priv_s
 #ifdef CONFIG_RPTUN_WORKQUEUE
   struct work_s                work;
 #else
-  int                          tid;
+  pid_t                        tid;
 #endif
 #ifdef CONFIG_RPTUN_PM
   bool                         stay;
@@ -136,7 +137,6 @@ static void rptun_ns_bind(FAR struct rpmsg_device *rdev,
 
 static int rptun_dev_start(FAR struct remoteproc *rproc);
 static int rptun_dev_stop(FAR struct remoteproc *rproc);
-static int rptun_dev_reset(FAR struct remoteproc *rproc, int value);
 static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
                            unsigned long arg);
 
@@ -155,6 +155,7 @@ static metal_phys_addr_t rptun_pa_to_da(FAR struct rptun_dev_s *dev,
                                         metal_phys_addr_t pa);
 static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
                                         metal_phys_addr_t da);
+static int rptun_ops_foreach(FAR const char *cpuname, int ops, int value);
 
 /****************************************************************************
  * Private Data
@@ -333,10 +334,7 @@ static void rptun_wakeup(FAR struct rptun_priv_s *priv)
 
 static void rptun_in_recursive(int tid, FAR void *arg)
 {
-  if (gettid() == tid)
-    {
-      *((FAR bool *)arg) = true;
-    }
+  *((FAR bool *)arg) = (gettid() == tid);
 }
 
 static bool rptun_is_recursive(FAR struct rptun_priv_s *priv)
@@ -376,28 +374,7 @@ static bool rptun_is_recursive(FAR struct rptun_priv_s *priv)
 
 static int rptun_callback(FAR void *arg, uint32_t vqid)
 {
-  FAR struct rptun_priv_s *priv = arg;
-
-  int status = rpmsg_virtio_get_status(&priv->rvdev);
-
-  if ((status & VIRTIO_CONFIG_STATUS_NEEDS_RESET)
-      && (RPTUN_IS_MASTER(priv->dev) ^
-          !!(status & RPTUN_STATUS_FROM_MASTER)))
-    {
-      status &= RPTUN_STATUS_MASK;
-      if (status == RPTUN_STATUS_PANIC)
-        {
-          PANIC();
-        }
-      else
-        {
-#ifdef CONFIG_BOARDCTL_RESET
-          board_reset(status);
-#endif
-        }
-    }
-
-  rptun_wakeup(priv);
+  rptun_wakeup(arg);
   return OK;
 }
 
@@ -465,7 +442,6 @@ static int rptun_notify(FAR struct remoteproc *rproc, uint32_t id)
     }
 
   RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
-
   return 0;
 }
 
@@ -842,18 +818,6 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc)
   return 0;
 }
 
-static int rptun_dev_reset(FAR struct remoteproc *rproc, int value)
-{
-  FAR struct rptun_priv_s *priv = rproc->priv;
-
-  value = (value & RPTUN_STATUS_MASK) | VIRTIO_CONFIG_STATUS_NEEDS_RESET
-          | (RPTUN_IS_MASTER(priv->dev) ? RPTUN_STATUS_FROM_MASTER : 0);
-
-  rpmsg_virtio_set_status(&priv->rvdev, value);
-
-  return RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
-}
-
 static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
                            unsigned long arg)
 {
@@ -869,10 +833,10 @@ static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
         rptun_wakeup(priv);
         break;
       case RPTUNIOC_RESET:
-        rptun_dev_reset(&priv->rproc, arg);
+        RPTUN_RESET(priv->dev, arg);
         break;
       case RPTUNIOC_PANIC:
-        rptun_dev_reset(&priv->rproc, RPTUN_STATUS_PANIC);
+        RPTUN_PANIC(priv->dev);
         break;
       case RPTUNIOC_DUMP:
         rptun_dump(&priv->rvdev);
@@ -1005,6 +969,42 @@ static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
   return da;
 }
 
+static int rptun_ops_foreach(FAR const char *cpuname, int ops, int value)
+{
+  FAR struct metal_list *node;
+
+  metal_list_for_each(&g_rptun_priv, node)
+    {
+      FAR struct rptun_priv_s *priv;
+
+      priv = metal_container_of(node, struct rptun_priv_s, node);
+
+      if (!cpuname || !strcmp(RPTUN_GET_CPUNAME(priv->dev), cpuname))
+        {
+          switch (ops)
+            {
+              case RPTUN_OPS_START:
+                priv->cmd = RPTUNIOC_START;
+                rptun_wakeup(priv);
+                break;
+              case RPTUN_OPS_DUMP:
+                rptun_dump(&priv->rvdev);
+                break;
+              case RPTUN_OPS_RESET:
+                RPTUN_RESET(priv->dev, value);
+                break;
+              case RPTUN_OPS_PANIC:
+                RPTUN_PANIC(priv->dev);
+                break;
+              default:
+                return -ENOTTY;
+            }
+        }
+    }
+
+  return 0;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -1247,55 +1247,17 @@ err_mem:
 
 int rptun_boot(FAR const char *cpuname)
 {
-  struct file file;
-  char name[32];
-  int ret;
-
-  if (!cpuname)
-    {
-      return -EINVAL;
-    }
-
-  snprintf(name, 32, "/dev/rptun/%s", cpuname);
-  ret = file_open(&file, name, 0, 0);
-  if (ret)
-    {
-      return ret;
-    }
-
-  ret = file_ioctl(&file, RPTUNIOC_START, 0);
-  file_close(&file);
-
-  return ret;
+  return rptun_ops_foreach(cpuname, RPTUN_OPS_START, 0);
 }
 
 int rptun_reset(FAR const char *cpuname, int value)
 {
-  FAR struct metal_list *node;
-
-  if (!cpuname)
-    {
-      return -EINVAL;
-    }
-
-  metal_list_for_each(&g_rptun_priv, node)
-    {
-      FAR struct rptun_priv_s *priv;
-
-      priv = metal_container_of(node, struct rptun_priv_s, node);
-
-      if (!strcmp(RPTUN_GET_CPUNAME(priv->dev), cpuname))
-        {
-          rptun_dev_reset(&priv->rproc, value);
-        }
-    }
-
-  return -ENOENT;
+  return rptun_ops_foreach(cpuname, RPTUN_OPS_RESET, value);
 }
 
 int rptun_panic(FAR const char *cpuname)
 {
-  return rptun_reset(cpuname, RPTUN_STATUS_PANIC);
+  return rptun_ops_foreach(cpuname, RPTUN_OPS_PANIC, 0);
 }
 
 int rptun_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx)
@@ -1315,13 +1277,5 @@ int rptun_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx)
 
 void rptun_dump_all(void)
 {
-  FAR struct metal_list *node;
-
-  metal_list_for_each(&g_rptun_priv, node)
-    {
-      FAR struct rptun_priv_s *priv =
-          metal_container_of(node, struct rptun_priv_s, node);
-
-      rptun_dump(&priv->rvdev);
-    }
+  rptun_ops_foreach(NULL, RPTUN_OPS_DUMP, 0);
 }
diff --git a/include/nuttx/rptun/rptun.h b/include/nuttx/rptun/rptun.h
index 95fba39..2405ebe 100644
--- a/include/nuttx/rptun/rptun.h
+++ b/include/nuttx/rptun/rptun.h
@@ -255,6 +255,41 @@
                                       (d)->ops->register_callback(d,NULL,NULL) : -ENOSYS)
 
 /****************************************************************************
+ * Name: RPTUN_RESET
+ *
+ * Description:
+ *   Reset remote cpu
+ *
+ * Input Parameters:
+ *   dev      - Device-specific state data
+ *   value    - reset value
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+#define RPTUN_RESET(d,v) ((d)->ops->reset ? \
+                          (d)->ops->reset(d,v) : -ENOSYS)
+
+/****************************************************************************
+ * Name: RPTUN_PANIC
+ *
+ * Description:
+ *   Panic remote cpu
+ *
+ * Input Parameters:
+ *   dev      - Device-specific state data
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+#define RPTUN_PANIC(d) ((d)->ops->panic ? \
+                        (d)->ops->panic(d) : -ENOSYS)
+
+/****************************************************************************
  * Public Types
  ****************************************************************************/
 
@@ -297,6 +332,9 @@ struct rptun_ops_s
   CODE int (*notify)(FAR struct rptun_dev_s *dev, uint32_t vqid);
   CODE int (*register_callback)(FAR struct rptun_dev_s *dev,
                                 rptun_callback_t callback, FAR void *arg);
+
+  CODE void (*reset)(FAR struct rptun_dev_s *dev, int value);
+  CODE void (*panic)(FAR struct rptun_dev_s *dev);
 };
 
 struct rptun_dev_s

[incubator-nuttx] 01/13: rptun: set rptun thread default stack size to 4K

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 5b6b748f6dff304540cad184f3e5955e3150c572
Author: ligd <li...@xiaomi.com>
AuthorDate: Wed Dec 15 20:09:26 2021 +0800

    rptun: set rptun thread default stack size to 4K
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/rptun/Kconfig b/drivers/rptun/Kconfig
index b02afba..0e8ca6b 100644
--- a/drivers/rptun/Kconfig
+++ b/drivers/rptun/Kconfig
@@ -18,7 +18,7 @@ config RPTUN_PRIORITY
 
 config RPTUN_STACKSIZE
 	int "rptun stack size"
-	default DEFAULT_TASK_STACKSIZE
+	default 4096
 
 config RPTUN_LOADER
 	bool "rptun loader support"

[incubator-nuttx] 11/13: rptun: add ping rpmsg support

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 759898c0909342b625b12352e6df15eb8921008b
Author: ligd <li...@xiaomi.com>
AuthorDate: Wed Feb 16 15:23:49 2022 +0800

    rptun: add ping rpmsg support
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/Kconfig                        |   7 +
 drivers/rptun/Make.defs                      |   6 +-
 drivers/rptun/rptun.c                        |  60 ++++++---
 drivers/rptun/rptun.h                        |  44 ++++++
 drivers/rptun/{rpmsg_dump.c => rptun_dump.c} |  18 +--
 drivers/rptun/rptun_ping.c                   | 192 +++++++++++++++++++++++++++
 include/nuttx/rptun/openamp.h                |   2 -
 include/nuttx/rptun/rptun.h                  |  12 +-
 8 files changed, 308 insertions(+), 33 deletions(-)

diff --git a/drivers/rptun/Kconfig b/drivers/rptun/Kconfig
index c1a2415..327b20a 100644
--- a/drivers/rptun/Kconfig
+++ b/drivers/rptun/Kconfig
@@ -52,4 +52,11 @@ config RPTUN_PM
 		goto RAM-retention mode, can't access from another CPU.
 		So, we provide this method to resolve this.
 
+config RPTUN_PING
+	bool "rptun ping support"
+	default n
+	---help---
+		This is for rptun debugging & profiling, create ping rpmsg
+		channel, user can use it to get send/recv speed & latency.
+
 endif # RPTUN
diff --git a/drivers/rptun/Make.defs b/drivers/rptun/Make.defs
index 8c79e0a..03446ec 100644
--- a/drivers/rptun/Make.defs
+++ b/drivers/rptun/Make.defs
@@ -22,7 +22,11 @@
 
 ifeq ($(CONFIG_RPTUN),y)
 
-CSRCS += rptun.c rpmsg_dump.c
+CSRCS += rptun.c rptun_dump.c
+
+ifeq ($(CONFIG_RPTUN_PING),y)
+CSRCS += rptun_ping.c
+endif
 
 DEPPATH += --dep-path rptun
 VPATH += :rptun
diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 624c927..949cf97 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -39,6 +39,8 @@
 #include <nuttx/wqueue.h>
 #include <metal/utilities.h>
 
+#include "rptun.h"
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -81,6 +83,9 @@ struct rptun_priv_s
 #ifdef CONFIG_RPTUN_PM
   bool                         stay;
 #endif
+#ifdef CONFIG_RPTUN_PING
+  struct rpmsg_endpoint        ping;
+#endif
 };
 
 struct rptun_bind_s
@@ -268,7 +273,7 @@ static inline void rptun_pm_action(FAR struct rptun_priv_s *priv,
       priv->stay = true;
     }
 
-  if (!stay && priv->stay && !rpmsg_buffer_nused(&priv->rvdev, false))
+  if (!stay && priv->stay && !rptun_buffer_nused(&priv->rvdev, false))
     {
       pm_relax(0, PM_IDLE);
       priv->stay = false;
@@ -785,6 +790,10 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
   rptun_unlock();
 
   virtqueue_enable_cb(priv->rvdev.svq);
+
+#ifdef CONFIG_RPTUN_PING
+  rptun_ping_init(&priv->rvdev, &priv->ping);
+#endif
   return 0;
 }
 
@@ -794,6 +803,10 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc)
   FAR struct metal_list *node;
   FAR struct rptun_cb_s *cb;
 
+#ifdef CONFIG_RPTUN_PING
+  rptun_ping_deinit(&priv->ping);
+#endif
+
   /* Unregister callback from mbox */
 
   RPTUN_UNREGISTER_CALLBACK(priv->dev);
@@ -862,8 +875,13 @@ static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
         rptun_dev_reset(&priv->rproc, RPTUN_STATUS_PANIC);
         break;
       case RPTUNIOC_DUMP:
-        rpmsg_dump(&priv->rvdev);
+        rptun_dump(&priv->rvdev);
+        break;
+#ifdef CONFIG_RPTUN_PING
+      case RPTUNIOC_PING:
+        rptun_ping(&priv->ping, (FAR const struct rptun_ping_s *)arg);
         break;
+#endif
       default:
         ret = -ENOTTY;
         break;
@@ -996,7 +1014,7 @@ int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
   FAR struct rptun_priv_s *priv;
   int ret;
 
-  if (!ept)
+  if (!ept || !sem)
     {
       return -EINVAL;
     }
@@ -1028,7 +1046,7 @@ int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
   int semcount;
   int ret;
 
-  if (!ept)
+  if (!ept || !sem)
     {
       return -EINVAL;
     }
@@ -1052,21 +1070,6 @@ FAR const char *rpmsg_get_cpuname(FAR struct rpmsg_device *rdev)
   return RPTUN_GET_CPUNAME(priv->dev);
 }
 
-int rpmsg_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx)
-{
-  FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
-
-  if ((rpmsg_virtio_get_role(rvdev) == RPMSG_MASTER) ^ rx)
-    {
-      return vq->vq_ring.avail->idx - vq->vq_ring.used->idx;
-    }
-  else
-    {
-      return vq->vq_nentries -
-             (vq->vq_ring.avail->idx - vq->vq_ring.used->idx);
-    }
-}
-
 int rpmsg_register_callback(FAR void *priv_,
                             rpmsg_dev_cb_t device_created,
                             rpmsg_dev_cb_t device_destroy,
@@ -1295,7 +1298,22 @@ int rptun_panic(FAR const char *cpuname)
   return rptun_reset(cpuname, RPTUN_STATUS_PANIC);
 }
 
-void rptun_dump(void)
+int rptun_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx)
+{
+  FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
+
+  if ((rpmsg_virtio_get_role(rvdev) == RPMSG_MASTER) ^ rx)
+    {
+      return vq->vq_ring.avail->idx - vq->vq_ring.used->idx;
+    }
+  else
+    {
+      return vq->vq_nentries -
+             (vq->vq_ring.avail->idx - vq->vq_ring.used->idx);
+    }
+}
+
+void rptun_dump_all(void)
 {
   FAR struct metal_list *node;
 
@@ -1304,6 +1322,6 @@ void rptun_dump(void)
       FAR struct rptun_priv_s *priv =
           metal_container_of(node, struct rptun_priv_s, node);
 
-      rpmsg_dump(&priv->rvdev);
+      rptun_dump(&priv->rvdev);
     }
 }
diff --git a/drivers/rptun/rptun.h b/drivers/rptun/rptun.h
new file mode 100644
index 0000000..77038b6
--- /dev/null
+++ b/drivers/rptun/rptun.h
@@ -0,0 +1,44 @@
+/****************************************************************************
+ * drivers/rptun/rptun.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_RPTUN_RPTUN_H
+#define __DRIVERS_RPTUN_RPTUN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/rptun/rptun.h>
+#include <openamp/open_amp.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+int rptun_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx);
+void rptun_dump(FAR struct rpmsg_virtio_device *rvdev);
+
+int rptun_ping_init(FAR struct rpmsg_virtio_device *rvdev,
+                    FAR struct rpmsg_endpoint *ept);
+void rptun_ping_deinit(FAR struct rpmsg_endpoint *ept);
+int rptun_ping(FAR struct rpmsg_endpoint *ept,
+               FAR const struct rptun_ping_s *ping);
+
+#endif /* __DRIVERS_RPTUN_RPTUN_H */
diff --git a/drivers/rptun/rpmsg_dump.c b/drivers/rptun/rptun_dump.c
similarity index 90%
rename from drivers/rptun/rpmsg_dump.c
rename to drivers/rptun/rptun_dump.c
index 413431a..cb041cf 100644
--- a/drivers/rptun/rpmsg_dump.c
+++ b/drivers/rptun/rptun_dump.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * drivers/rptun/rpmsg_dump.c
+ * drivers/rptun/rptun_dump.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -28,11 +28,13 @@
 
 #include <rpmsg/rpmsg_internal.h>
 
+#include "rptun.h"
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
-static void rpmsg_dump_addr(FAR struct rpmsg_device *rdev,
+static void rptun_dump_addr(FAR struct rpmsg_device *rdev,
                             FAR void *addr, bool rx)
 {
   FAR struct rpmsg_hdr *hdr = addr;
@@ -47,7 +49,7 @@ static void rpmsg_dump_addr(FAR struct rpmsg_device *rdev,
     }
 }
 
-static void rpmsg_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
+static void rptun_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
                               bool rx)
 {
   FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
@@ -56,7 +58,7 @@ static void rpmsg_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
   int num;
   int i;
 
-  num = rpmsg_buffer_nused(rvdev, rx);
+  num = rptun_buffer_nused(rvdev, rx);
   metal_log(METAL_LOG_INFO,
             "    %s buffer, total %d, pending %d\n",
             rx ? "RX" : "TX", vq->vq_nentries, num);
@@ -78,7 +80,7 @@ static void rpmsg_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
                                    vq->vq_ring.desc[desc_idx].addr);
       if (addr)
         {
-          rpmsg_dump_addr(&rvdev->rdev, addr, rx);
+          rptun_dump_addr(&rvdev->rdev, addr, rx);
         }
     }
 }
@@ -87,7 +89,7 @@ static void rpmsg_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
  * Public Functions
  ****************************************************************************/
 
-void rpmsg_dump(FAR struct rpmsg_virtio_device *rvdev)
+void rptun_dump(FAR struct rpmsg_virtio_device *rvdev)
 {
   FAR struct rpmsg_device *rdev = &rvdev->rdev;
   FAR struct rpmsg_endpoint *ept;
@@ -114,8 +116,8 @@ void rpmsg_dump(FAR struct rpmsg_virtio_device *rvdev)
 
   metal_log(METAL_LOG_INFO, "  rpmsg buffer list:\n");
 
-  rpmsg_dump_buffer(rvdev, true);
-  rpmsg_dump_buffer(rvdev, false);
+  rptun_dump_buffer(rvdev, true);
+  rptun_dump_buffer(rvdev, false);
 
   metal_mutex_release(&rdev->lock);
 }
diff --git a/drivers/rptun/rptun_ping.c b/drivers/rptun/rptun_ping.c
new file mode 100644
index 0000000..60055ce
--- /dev/null
+++ b/drivers/rptun/rptun_ping.c
@@ -0,0 +1,192 @@
+/****************************************************************************
+ * drivers/rptun/rptun_ping.c
+ *
+ * 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 <nuttx/config.h>
+#include <nuttx/arch.h>
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "rptun.h"
+
+/****************************************************************************
+ * Pre-processor definitions
+ ****************************************************************************/
+
+#ifndef MIN
+  #define MIN(n,m)                  (((n) < (m)) ? (n) : (m))
+#endif
+
+#ifndef MAX
+  #define MAX(n,m)                  (((n) < (m)) ? (m) : (n))
+#endif
+
+#define RPTUN_PING_EPT_NAME         "rpmsg-ping"
+#define RPTUN_PING_SEND             1
+#define RPTUN_PING_SEND_NOACK       2
+#define RPTUN_PING_ACK              3
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+begin_packed_struct struct rptun_ping_msg_s
+{
+  uint32_t cmd;
+  uint32_t len;
+  uint64_t cookie;
+} end_packed_struct;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int rptun_ping_ept_cb(FAR struct rpmsg_endpoint *ept,
+                             FAR void *data, size_t len, uint32_t src,
+                             FAR void *priv)
+{
+  FAR struct rptun_ping_msg_s *msg = data;
+  FAR sem_t *sem = (FAR sem_t *)(uintptr_t)msg->cookie;
+
+  if (msg->cmd == RPTUN_PING_SEND)
+    {
+      msg->cmd = RPTUN_PING_ACK;
+      rpmsg_send(ept, msg, len);
+    }
+  else if (msg->cmd == RPTUN_PING_ACK)
+    {
+      nxsem_post(sem);
+    }
+
+  return 0;
+}
+
+static int rptun_ping_once(FAR struct rpmsg_endpoint *ept,
+                           int len, bool ack)
+{
+  FAR struct rptun_ping_msg_s *msg;
+  uint32_t space;
+  int ret;
+
+  msg = rpmsg_get_tx_payload_buffer(ept, &space, true);
+  if (!msg)
+    {
+      return -ENOMEM;
+    }
+
+  len = MAX(len, sizeof(struct rptun_ping_msg_s));
+  len = MIN(len, space);
+
+  memset(msg, 0, len);
+
+  if (ack)
+    {
+      sem_t sem;
+
+      msg->cmd    = RPTUN_PING_SEND;
+      msg->len    = len;
+      msg->cookie = (uintptr_t)&sem;
+
+      nxsem_init(&sem, 0, 0);
+      nxsem_set_protocol(&sem, SEM_PRIO_NONE);
+
+      ret = rpmsg_send_nocopy(ept, msg, len);
+      if (ret >= 0)
+        {
+          nxsem_wait_uninterruptible(&sem);
+        }
+
+      nxsem_destroy(&sem);
+    }
+  else
+    {
+      msg->cmd = RPTUN_PING_SEND_NOACK;
+      msg->len = len;
+      ret = rpmsg_send_nocopy(ept, msg, len);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int rptun_ping(FAR struct rpmsg_endpoint *ept,
+               FAR const struct rptun_ping_s *ping)
+{
+  uint32_t min = UINT32_MAX;
+  uint32_t max = 0;
+  uint64_t total = 0;
+  struct timespec ts;
+  int i;
+
+  if (!ept || !ping || ping->times <= 0)
+    {
+      return -EINVAL;
+    }
+
+  for (i = 0; i < ping->times; i++)
+    {
+      uint32_t tm = up_perf_gettime();
+
+      int ret = rptun_ping_once(ept, ping->len, ping->ack);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      tm     = up_perf_gettime() - tm;
+      min    = MIN(min, tm);
+      max    = MAX(max, tm);
+      total += tm;
+    }
+
+  syslog(LOG_INFO, "current CPU freq: %" PRIu32 ", ping times: %d\n",
+                    up_perf_getfreq(), ping->times);
+
+  up_perf_convert(total / ping->times, &ts);
+  syslog(LOG_INFO, "avg: s %" PRIu32 ", ns %ld\n", ts.tv_sec, ts.tv_nsec);
+
+  up_perf_convert(min, &ts);
+  syslog(LOG_INFO, "min: s %" PRIu32 ", ns %ld\n", ts.tv_sec, ts.tv_nsec);
+
+  up_perf_convert(max, &ts);
+  syslog(LOG_INFO, "max: s %" PRIu32 ", ns %ld\n", ts.tv_sec, ts.tv_nsec);
+
+  return 0;
+}
+
+int rptun_ping_init(FAR struct rpmsg_virtio_device *rvdev,
+                    FAR struct rpmsg_endpoint *ept)
+{
+  return rpmsg_create_ept(ept, &rvdev->rdev, RPTUN_PING_EPT_NAME,
+                          RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
+                          rptun_ping_ept_cb, NULL);
+}
+
+void rptun_ping_deinit(FAR struct rpmsg_endpoint *ept)
+{
+  rpmsg_destroy_ept(ept);
+}
diff --git a/include/nuttx/rptun/openamp.h b/include/nuttx/rptun/openamp.h
index 4188c41..833c477 100644
--- a/include/nuttx/rptun/openamp.h
+++ b/include/nuttx/rptun/openamp.h
@@ -58,8 +58,6 @@ int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem);
 int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem);
 
 const char *rpmsg_get_cpuname(FAR struct rpmsg_device *rdev);
-int rpmsg_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx);
-void rpmsg_dump(FAR struct rpmsg_virtio_device *rvdev);
 
 int rpmsg_register_callback(FAR void *priv,
                             rpmsg_dev_cb_t device_created,
diff --git a/include/nuttx/rptun/rptun.h b/include/nuttx/rptun/rptun.h
index eb31847..95fba39 100644
--- a/include/nuttx/rptun/rptun.h
+++ b/include/nuttx/rptun/rptun.h
@@ -41,6 +41,7 @@
 #define RPTUNIOC_RESET              _RPTUNIOC(3)
 #define RPTUNIOC_PANIC              _RPTUNIOC(4)
 #define RPTUNIOC_DUMP               _RPTUNIOC(5)
+#define RPTUNIOC_PING               _RPTUNIOC(6)
 
 #define RPTUN_NOTIFY_ALL            (UINT32_MAX - 0)
 
@@ -303,6 +304,15 @@ struct rptun_dev_s
   FAR const struct rptun_ops_s *ops;
 };
 
+/* used for ioctl RPTUNIOC_PING */
+
+struct rptun_ping_s
+{
+  int  times;
+  int  len;
+  bool ack;
+};
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -319,7 +329,7 @@ int rptun_initialize(FAR struct rptun_dev_s *dev);
 int rptun_boot(FAR const char *cpuname);
 int rptun_reset(FAR const char *cpuname, int value);
 int rptun_panic(FAR const char *cpuname);
-void rptun_dump(void);
+void rptun_dump_all(void);
 
 #ifdef __cplusplus
 }

[incubator-nuttx] 10/13: rptun: enable rx interrupts for virtq

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit a19aef4d361a66d7c86b49c67aa239a056d853e2
Author: ligd <li...@xiaomi.com>
AuthorDate: Tue Mar 1 19:11:40 2022 +0800

    rptun: enable rx interrupts for virtq
    
    use wait_tx_buffer method need enable rx interrupts
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/rptun.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index f73d6a2..624c927 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -277,14 +277,8 @@ static inline void rptun_pm_action(FAR struct rptun_priv_s *priv,
   leave_critical_section(flags);
 }
 
-static inline void rptun_enable_rx_kick(FAR struct rptun_priv_s *priv)
-{
-  virtqueue_enable_cb(priv->rvdev.svq);
-}
-
 #else
 #  define rptun_pm_action(priv, stay)
-#  define rptun_enable_rx_kick(priv)
 #endif
 
 static void rptun_worker(FAR void *arg)
@@ -790,7 +784,7 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
 
   rptun_unlock();
 
-  rptun_enable_rx_kick(priv);
+  virtqueue_enable_cb(priv->rvdev.svq);
   return 0;
 }
 

[incubator-nuttx] 08/13: rptun: add rptun dump support

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f0ba2f028643600792d7870317f9fbad69484249
Author: ligd <li...@xiaomi.com>
AuthorDate: Fri Jan 14 23:25:49 2022 +0800

    rptun: add rptun dump support
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/Make.defs       |   3 +-
 drivers/rptun/rpmsg_dump.c    | 121 ++++++++++++++++++++++++++++++++++++++++++
 drivers/rptun/rptun.c         |  59 +++++++++++++++-----
 include/nuttx/rptun/openamp.h |   3 ++
 include/nuttx/rptun/rptun.h   |   4 +-
 5 files changed, 174 insertions(+), 16 deletions(-)

diff --git a/drivers/rptun/Make.defs b/drivers/rptun/Make.defs
index 00eacf4..8c79e0a 100644
--- a/drivers/rptun/Make.defs
+++ b/drivers/rptun/Make.defs
@@ -22,9 +22,10 @@
 
 ifeq ($(CONFIG_RPTUN),y)
 
-CSRCS += rptun.c
+CSRCS += rptun.c rpmsg_dump.c
 
 DEPPATH += --dep-path rptun
 VPATH += :rptun
 CFLAGS += ${shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)rptun}
+CFLAGS += ${shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)openamp$(DELIM)open-amp$(DELIM)lib}
 endif
diff --git a/drivers/rptun/rpmsg_dump.c b/drivers/rptun/rpmsg_dump.c
new file mode 100644
index 0000000..413431a
--- /dev/null
+++ b/drivers/rptun/rpmsg_dump.c
@@ -0,0 +1,121 @@
+/****************************************************************************
+ * drivers/rptun/rpmsg_dump.c
+ *
+ * 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 <nuttx/rptun/openamp.h>
+#include <nuttx/rptun/rptun.h>
+#include <metal/utilities.h>
+
+#include <rpmsg/rpmsg_internal.h>
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void rpmsg_dump_addr(FAR struct rpmsg_device *rdev,
+                            FAR void *addr, bool rx)
+{
+  FAR struct rpmsg_hdr *hdr = addr;
+  FAR struct rpmsg_endpoint *ept;
+
+  ept = rpmsg_get_ept_from_addr(rdev, rx ? hdr->dst : hdr->src);
+  if (ept)
+    {
+      metal_log(METAL_LOG_INFO,
+                "      %s buffer %p hold by %s\n",
+                rx ? "RX" : "TX", hdr, ept->name);
+    }
+}
+
+static void rpmsg_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
+                              bool rx)
+{
+  FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
+  FAR void *addr;
+  int desc_idx;
+  int num;
+  int i;
+
+  num = rpmsg_buffer_nused(rvdev, rx);
+  metal_log(METAL_LOG_INFO,
+            "    %s buffer, total %d, pending %d\n",
+            rx ? "RX" : "TX", vq->vq_nentries, num);
+
+  for (i = 0; i < num; i++)
+    {
+      if ((rpmsg_virtio_get_role(rvdev) == RPMSG_MASTER) ^ rx)
+        {
+          desc_idx = (vq->vq_ring.used->idx + i) & (vq->vq_nentries - 1);
+          desc_idx = vq->vq_ring.avail->ring[desc_idx];
+        }
+      else
+        {
+          desc_idx = (vq->vq_ring.avail->idx + i) & (vq->vq_nentries - 1);
+          desc_idx = vq->vq_ring.used->ring[desc_idx].id;
+        }
+
+      addr = metal_io_phys_to_virt(vq->shm_io,
+                                   vq->vq_ring.desc[desc_idx].addr);
+      if (addr)
+        {
+          rpmsg_dump_addr(&rvdev->rdev, addr, rx);
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void rpmsg_dump(FAR struct rpmsg_virtio_device *rvdev)
+{
+  FAR struct rpmsg_device *rdev = &rvdev->rdev;
+  FAR struct rpmsg_endpoint *ept;
+  FAR struct metal_list *node;
+
+  if (!rvdev->vdev)
+    {
+      return;
+    }
+
+  metal_mutex_acquire(&rdev->lock);
+
+  metal_log(METAL_LOG_INFO,
+            "Dump rpmsg info between cpu %s <==> %s:\n",
+            CONFIG_RPTUN_LOCAL_CPUNAME, rpmsg_get_cpuname(rdev));
+
+  metal_log(METAL_LOG_INFO, "  rpmsg ept list:\n");
+
+  metal_list_for_each(&rdev->endpoints, node)
+    {
+      ept = metal_container_of(node, struct rpmsg_endpoint, node);
+      metal_log(METAL_LOG_INFO, "    ept %s\n", ept->name);
+    }
+
+  metal_log(METAL_LOG_INFO, "  rpmsg buffer list:\n");
+
+  rpmsg_dump_buffer(rvdev, true);
+  rpmsg_dump_buffer(rvdev, false);
+
+  metal_mutex_release(&rdev->lock);
+}
diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 3cf9cb5..e4c8a24 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -65,7 +65,7 @@ struct rptun_priv_s
 {
   FAR struct rptun_dev_s       *dev;
   struct remoteproc            rproc;
-  struct rpmsg_virtio_device   vdev;
+  struct rpmsg_virtio_device   rvdev;
   struct rpmsg_virtio_shm_pool tx_shpool;
   struct rpmsg_virtio_shm_pool rx_shpool;
   struct metal_list            bind;
@@ -340,7 +340,7 @@ static int rptun_callback(FAR void *arg, uint32_t vqid)
 {
   FAR struct rptun_priv_s *priv = arg;
 
-  int status = rpmsg_virtio_get_status(&priv->vdev);
+  int status = rpmsg_virtio_get_status(&priv->rvdev);
 
   if ((status & VIRTIO_CONFIG_STATUS_NEEDS_RESET)
       && (RPTUN_IS_MASTER(priv->dev) ^
@@ -693,13 +693,13 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
 
   if (priv->rx_shpool.base)
     {
-      ret = rpmsg_init_vdev_ext(&priv->vdev, vdev, rptun_ns_bind,
+      ret = rpmsg_init_vdev_ext(&priv->rvdev, vdev, rptun_ns_bind,
                                 metal_io_get_region(),
                                 &priv->tx_shpool, &priv->rx_shpool);
     }
   else
     {
-      ret = rpmsg_init_vdev(&priv->vdev, vdev, rptun_ns_bind,
+      ret = rpmsg_init_vdev(&priv->rvdev, vdev, rptun_ns_bind,
                             metal_io_get_region(), &priv->tx_shpool);
     }
 
@@ -709,7 +709,7 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
       return ret;
     }
 
-  priv->vdev.rdev.ns_unbind_cb = rptun_ns_unbind;
+  priv->rvdev.rdev.ns_unbind_cb = rptun_ns_unbind;
 
   /* Remote proc start */
 
@@ -737,7 +737,7 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
       cb = metal_container_of(node, struct rptun_cb_s, node);
       if (cb->device_created)
         {
-          cb->device_created(&priv->vdev.rdev, cb->priv);
+          cb->device_created(&priv->rvdev.rdev, cb->priv);
         }
     }
 
@@ -769,7 +769,7 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc)
       cb = metal_container_of(node, struct rptun_cb_s, node);
       if (cb->device_destroy)
         {
-          cb->device_destroy(&priv->vdev.rdev, cb->priv);
+          cb->device_destroy(&priv->rvdev.rdev, cb->priv);
         }
     }
 
@@ -781,8 +781,8 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc)
 
   /* Remote proc remove */
 
-  remoteproc_remove_virtio(rproc, priv->vdev.vdev);
-  rpmsg_deinit_vdev(&priv->vdev);
+  remoteproc_remove_virtio(rproc, priv->rvdev.vdev);
+  rpmsg_deinit_vdev(&priv->rvdev);
 
   return 0;
 }
@@ -794,7 +794,7 @@ static int rptun_dev_reset(FAR struct remoteproc *rproc, int value)
   value = (value & RPTUN_STATUS_MASK) | VIRTIO_CONFIG_STATUS_NEEDS_RESET
           | (RPTUN_IS_MASTER(priv->dev) ? RPTUN_STATUS_FROM_MASTER : 0);
 
-  rpmsg_virtio_set_status(&priv->vdev, value);
+  rpmsg_virtio_set_status(&priv->rvdev, value);
 
   return RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
 }
@@ -819,6 +819,9 @@ static int rptun_dev_ioctl(FAR struct file *filep, int cmd,
       case RPTUNIOC_PANIC:
         rptun_dev_reset(&priv->rproc, RPTUN_STATUS_PANIC);
         break;
+      case RPTUNIOC_DUMP:
+        rpmsg_dump(&priv->rvdev);
+        break;
       default:
         ret = -ENOTTY;
         break;
@@ -1007,6 +1010,21 @@ FAR const char *rpmsg_get_cpuname(FAR struct rpmsg_device *rdev)
   return RPTUN_GET_CPUNAME(priv->dev);
 }
 
+int rpmsg_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx)
+{
+  FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
+
+  if ((rpmsg_virtio_get_role(rvdev) == RPMSG_MASTER) ^ rx)
+    {
+      return vq->vq_ring.avail->idx - vq->vq_ring.used->idx;
+    }
+  else
+    {
+      return vq->vq_nentries -
+             (vq->vq_ring.avail->idx - vq->vq_ring.used->idx);
+    }
+}
+
 int rpmsg_register_callback(FAR void *priv_,
                             rpmsg_dev_cb_t device_created,
                             rpmsg_dev_cb_t device_destroy,
@@ -1033,12 +1051,12 @@ int rpmsg_register_callback(FAR void *priv_,
 
   metal_list_for_each(&g_rptun_priv, node)
     {
-      struct rptun_priv_s *priv;
+      FAR struct rptun_priv_s *priv;
 
       priv = metal_container_of(node, struct rptun_priv_s, node);
       if (device_created)
         {
-          device_created(&priv->vdev.rdev, priv_);
+          device_created(&priv->rvdev.rdev, priv_);
         }
 
       if (ns_bind)
@@ -1048,7 +1066,7 @@ int rpmsg_register_callback(FAR void *priv_,
               struct rptun_bind_s *bind;
 
               bind = metal_container_of(bnode, struct rptun_bind_s, node);
-              ns_bind(&priv->vdev.rdev, priv_, bind->name, bind->dest);
+              ns_bind(&priv->rvdev.rdev, priv_, bind->name, bind->dest);
             }
         }
     }
@@ -1086,7 +1104,7 @@ void rpmsg_unregister_callback(FAR void *priv_,
 
                   priv = metal_container_of(pnode,
                                             struct rptun_priv_s, node);
-                  device_destroy(&priv->vdev.rdev, priv_);
+                  device_destroy(&priv->rvdev.rdev, priv_);
                 }
             }
 
@@ -1234,3 +1252,16 @@ int rptun_panic(FAR const char *cpuname)
 {
   return rptun_reset(cpuname, RPTUN_STATUS_PANIC);
 }
+
+void rptun_dump(void)
+{
+  FAR struct metal_list *node;
+
+  metal_list_for_each(&g_rptun_priv, node)
+    {
+      FAR struct rptun_priv_s *priv =
+          metal_container_of(node, struct rptun_priv_s, node);
+
+      rpmsg_dump(&priv->rvdev);
+    }
+}
diff --git a/include/nuttx/rptun/openamp.h b/include/nuttx/rptun/openamp.h
index 365e2bc..4188c41 100644
--- a/include/nuttx/rptun/openamp.h
+++ b/include/nuttx/rptun/openamp.h
@@ -58,6 +58,9 @@ int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem);
 int rpmsg_post(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem);
 
 const char *rpmsg_get_cpuname(FAR struct rpmsg_device *rdev);
+int rpmsg_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx);
+void rpmsg_dump(FAR struct rpmsg_virtio_device *rvdev);
+
 int rpmsg_register_callback(FAR void *priv,
                             rpmsg_dev_cb_t device_created,
                             rpmsg_dev_cb_t device_destroy,
diff --git a/include/nuttx/rptun/rptun.h b/include/nuttx/rptun/rptun.h
index f51d9c6..eb31847 100644
--- a/include/nuttx/rptun/rptun.h
+++ b/include/nuttx/rptun/rptun.h
@@ -40,6 +40,7 @@
 #define RPTUNIOC_STOP               _RPTUNIOC(2)
 #define RPTUNIOC_RESET              _RPTUNIOC(3)
 #define RPTUNIOC_PANIC              _RPTUNIOC(4)
+#define RPTUNIOC_DUMP               _RPTUNIOC(5)
 
 #define RPTUN_NOTIFY_ALL            (UINT32_MAX - 0)
 
@@ -60,7 +61,7 @@
  ****************************************************************************/
 
 #define RPTUN_GET_CPUNAME(d) ((d)->ops->get_cpuname ? \
-                              (d)->ops->get_cpuname(d) : NULL)
+                              (d)->ops->get_cpuname(d) : "")
 
 /****************************************************************************
  * Name: RPTUN_GET_FIRMWARE
@@ -318,6 +319,7 @@ int rptun_initialize(FAR struct rptun_dev_s *dev);
 int rptun_boot(FAR const char *cpuname);
 int rptun_reset(FAR const char *cpuname, int value);
 int rptun_panic(FAR const char *cpuname);
+void rptun_dump(void);
 
 #ifdef __cplusplus
 }

[incubator-nuttx] 05/13: rptun: add rptun_panic for both master & salve

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 7471d096cc22a78387c8e3b7d86e3890e668ddcf
Author: ligd <li...@xiaomi.com>
AuthorDate: Fri Jan 21 16:20:30 2022 +0800

    rptun: add rptun_panic for both master & salve
    
    Signed-off-by: ligd <li...@xiaomi.com>
---
 drivers/rptun/rptun.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 3187759..3323e66 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -53,8 +53,9 @@
 #define RPTUNIOC_NONE           0
 #define NO_HOLDER               (INVALID_PROCESS_ID)
 
-#define RPTUN_STATUS_MASK       0xf
-#define RPTUN_STATUS_PANIC      0xf
+#define RPTUN_STATUS_FROM_MASTER    0x8
+#define RPTUN_STATUS_MASK           0x7
+#define RPTUN_STATUS_PANIC          0x7
 
 /****************************************************************************
  * Private Types
@@ -311,23 +312,22 @@ static int rptun_callback(FAR void *arg, uint32_t vqid)
 {
   FAR struct rptun_priv_s *priv = arg;
 
-  if (!RPTUN_IS_MASTER(priv->dev))
-    {
-      int status = rpmsg_virtio_get_status(&priv->vdev);
+  int status = rpmsg_virtio_get_status(&priv->vdev);
 
-      if (status & VIRTIO_CONFIG_STATUS_NEEDS_RESET)
+  if ((status & VIRTIO_CONFIG_STATUS_NEEDS_RESET)
+      && (RPTUN_IS_MASTER(priv->dev) ^
+          !!(status & RPTUN_STATUS_FROM_MASTER)))
+    {
+      status &= RPTUN_STATUS_MASK;
+      if (status == RPTUN_STATUS_PANIC)
+        {
+          PANIC();
+        }
+      else
         {
-          status &= RPTUN_STATUS_MASK;
-          if (status == RPTUN_STATUS_PANIC)
-            {
-              PANIC();
-            }
-          else
-            {
 #ifdef CONFIG_BOARDCTL_RESET
-              board_reset(status);
+          board_reset(status);
 #endif
-            }
         }
     }
 
@@ -725,8 +725,10 @@ static int rptun_dev_reset(FAR struct remoteproc *rproc, int value)
 {
   FAR struct rptun_priv_s *priv = rproc->priv;
 
-  rpmsg_virtio_set_status(&priv->vdev,
-          (value & RPTUN_STATUS_MASK) | VIRTIO_CONFIG_STATUS_NEEDS_RESET);
+  value = (value & RPTUN_STATUS_MASK) | VIRTIO_CONFIG_STATUS_NEEDS_RESET
+          | (RPTUN_IS_MASTER(priv->dev) ? RPTUN_STATUS_FROM_MASTER : 0);
+
+  rpmsg_virtio_set_status(&priv->vdev, value);
 
   return RPTUN_NOTIFY(priv->dev, RPTUN_NOTIFY_ALL);
 }
@@ -1097,8 +1099,7 @@ int rptun_reset(FAR const char *cpuname, int value)
 
       priv = metal_container_of(node, struct rptun_priv_s, node);
 
-      if (RPTUN_IS_MASTER(priv->dev) &&
-          !strcmp(RPTUN_GET_CPUNAME(priv->dev), cpuname))
+      if (!strcmp(RPTUN_GET_CPUNAME(priv->dev), cpuname))
         {
           rptun_dev_reset(&priv->rproc, value);
         }