From: Rishabh Bhatnagar <rishabhb@codeaurora.org>
To: linux-remoteproc-owner@vger.kernel.org,
linux-kernel@vger.kernel.org, bjorn.andersson@linaro.org,
mathieu.poirier@linaro.org
Cc: psodagud@codeaurora.org, tsoni@codeaurora.org,
sidgup@codeaurora.org,
Rishabh Bhatnagar <rishabhb@codeaurora.org>
Subject: [PATCH 1/2] remoteproc: Add userspace char device driver
Date: Fri, 20 Mar 2020 16:36:16 -0700 [thread overview]
Message-ID: <1584747377-14824-2-git-send-email-rishabhb@codeaurora.org> (raw)
In-Reply-To: <1584747377-14824-1-git-send-email-rishabhb@codeaurora.org>
Add the driver for creating the character device interface for
userspace applications. The character device interface can be used
in order to boot up and shutdown the remote processor.
This might be helpful for remote processors that are booted by
userspace applications and need to shutdown when the application
crahes/shutsdown.
Signed-off-by: Rishabh Bhatnagar <rishabhb@codeaurora.org>
---
drivers/remoteproc/Makefile | 1 +
drivers/remoteproc/remoteproc_internal.h | 3 +
drivers/remoteproc/remoteproc_userspace.c | 126 ++++++++++++++++++++++++++++++
include/linux/remoteproc.h | 2 +
4 files changed, 132 insertions(+)
create mode 100644 drivers/remoteproc/remoteproc_userspace.c
diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile
index e30a1b1..facb3fa 100644
--- a/drivers/remoteproc/Makefile
+++ b/drivers/remoteproc/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_REMOTEPROC) += remoteproc.o
remoteproc-y := remoteproc_core.o
remoteproc-y += remoteproc_debugfs.o
remoteproc-y += remoteproc_sysfs.o
+remoteproc-y += remoteproc_userspace.o
remoteproc-y += remoteproc_virtio.o
remoteproc-y += remoteproc_elf_loader.o
obj-$(CONFIG_IMX_REMOTEPROC) += imx_rproc.o
diff --git a/drivers/remoteproc/remoteproc_internal.h b/drivers/remoteproc/remoteproc_internal.h
index 493ef92..bafaa12 100644
--- a/drivers/remoteproc/remoteproc_internal.h
+++ b/drivers/remoteproc/remoteproc_internal.h
@@ -63,6 +63,9 @@ struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
struct rproc_mem_entry *
rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...);
+/* from remoteproc_userspace.c */
+extern int rproc_char_device_add(struct rproc *rproc);
+
static inline
int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
{
diff --git a/drivers/remoteproc/remoteproc_userspace.c b/drivers/remoteproc/remoteproc_userspace.c
new file mode 100644
index 0000000..e3017e7
--- /dev/null
+++ b/drivers/remoteproc/remoteproc_userspace.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Character device interface driver for Remoteproc framework.
+ *
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ */
+
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/cdev.h>
+#include <linux/mutex.h>
+#include <linux/remoteproc.h>
+
+#include "remoteproc_internal.h"
+
+static LIST_HEAD(rproc_chrdev_list);
+
+struct rproc_char_dev {
+ struct list_head node;
+ dev_t dev_no;
+ struct rproc *rproc;
+};
+
+static DEFINE_MUTEX(rproc_chrdev_lock);
+
+static struct rproc *rproc_get_by_dev_no(int minor)
+{
+ struct rproc_char_dev *r;
+
+ mutex_lock(&rproc_chrdev_lock);
+ list_for_each_entry(r, &rproc_chrdev_list, node) {
+ if (MINOR(r->dev_no) == minor)
+ break;
+ }
+ mutex_unlock(&rproc_chrdev_lock);
+
+ return r->rproc;
+}
+
+static int rproc_open(struct inode *inode, struct file *file)
+{
+ struct rproc *rproc;
+ int retval;
+
+ rproc = rproc_get_by_dev_no(iminor(inode));
+ if (!rproc)
+ return -EINVAL;
+
+ if (!try_module_get(rproc->dev.parent->driver->owner)) {
+ dev_err(&rproc->dev, "can't get owner\n");
+ return -EINVAL;
+ }
+
+ get_device(&rproc->dev);
+ retval = rproc_boot(rproc);
+
+ return retval;
+}
+
+static int rproc_close(struct inode *inode, struct file *file)
+{
+ struct rproc *rproc;
+
+ rproc = rproc_get_by_dev_no(iminor(inode));
+ if (!rproc)
+ return -EINVAL;
+
+ rproc_shutdown(rproc);
+ rproc_put(rproc);
+
+ return 0;
+}
+
+static const struct file_operations rproc_fops = {
+ .open = rproc_open,
+ .release = rproc_close,
+};
+
+int rproc_char_device_add(struct rproc *rproc)
+{
+ int ret = 0;
+ static int major, minor;
+ dev_t dev_no;
+ struct rproc_char_dev *chrdev;
+
+ mutex_lock(&rproc_chrdev_lock);
+ if (!major) {
+ ret = alloc_chrdev_region(&dev_no, 0, 4, "subsys");
+ if (ret < 0) {
+ pr_err("Failed to alloc subsys_dev region, err %d\n",
+ ret);
+ goto fail;
+ }
+ major = MAJOR(dev_no);
+ minor = MINOR(dev_no);
+ } else
+ dev_no = MKDEV(major, minor);
+
+ cdev_init(&rproc->char_dev, &rproc_fops);
+ rproc->char_dev.owner = THIS_MODULE;
+ ret = cdev_add(&rproc->char_dev, dev_no, 1);
+ if (ret < 0)
+ goto fail_unregister_cdev_region;
+
+ rproc->dev.devt = dev_no;
+
+ chrdev = kzalloc(sizeof(struct rproc_char_dev), GFP_KERNEL);
+ if (!chrdev) {
+ ret = -ENOMEM;
+ goto fail_unregister_cdev_region;
+ }
+
+ chrdev->rproc = rproc;
+ chrdev->dev_no = dev_no;
+ list_add(&chrdev->node, &rproc_chrdev_list);
+ ++minor;
+ mutex_unlock(&rproc_chrdev_lock);
+
+ return 0;
+
+fail_unregister_cdev_region:
+ unregister_chrdev_region(dev_no, 1);
+fail:
+ mutex_unlock(&rproc_chrdev_lock);
+ return ret;
+}
diff --git a/include/linux/remoteproc.h b/include/linux/remoteproc.h
index 16ad666..c4ca796 100644
--- a/include/linux/remoteproc.h
+++ b/include/linux/remoteproc.h
@@ -37,6 +37,7 @@
#include <linux/types.h>
#include <linux/mutex.h>
+#include <linux/cdev.h>
#include <linux/virtio.h>
#include <linux/completion.h>
#include <linux/idr.h>
@@ -514,6 +515,7 @@ struct rproc {
bool auto_boot;
struct list_head dump_segments;
int nb_vdev;
+ struct cdev char_dev;
};
/**
--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
next prev parent reply other threads:[~2020-03-20 23:36 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-20 23:36 [PATCH 0/2] Add character device interface to remoteproc Rishabh Bhatnagar
2020-03-20 23:36 ` Rishabh Bhatnagar [this message]
2020-03-25 4:10 ` [PATCH 1/2] remoteproc: Add userspace char device driver Bjorn Andersson
2020-03-20 23:36 ` [PATCH 2/2] remoteproc: core: Register the character device interface Rishabh Bhatnagar
2020-03-25 4:22 ` Bjorn Andersson
-- strict thread matches above, loose matches on Subject: below --
2020-03-26 16:50 [PATCH 0/2] Add character device interface to remoteproc Rishabh Bhatnagar
2020-03-26 16:50 ` [PATCH 1/2] remoteproc: Add userspace char device driver Rishabh Bhatnagar
2020-03-26 17:37 ` Clément Leger
2020-03-28 0:09 ` rishabhb
2020-03-30 10:42 ` Clément Leger
2020-03-27 4:00 ` kbuild test robot
2020-03-27 4:00 ` kbuild test robot
2020-03-30 22:12 ` Mathieu Poirier
2020-03-30 22:45 ` Bjorn Andersson
2020-03-31 16:47 ` Mathieu Poirier
2020-03-31 17:38 ` rishabhb
2020-03-31 17:55 ` Bjorn Andersson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1584747377-14824-2-git-send-email-rishabhb@codeaurora.org \
--to=rishabhb@codeaurora.org \
--cc=bjorn.andersson@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-remoteproc-owner@vger.kernel.org \
--cc=mathieu.poirier@linaro.org \
--cc=psodagud@codeaurora.org \
--cc=sidgup@codeaurora.org \
--cc=tsoni@codeaurora.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.