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:27 UTC

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

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