linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Li Chen <me@linux.beauty>
To: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>, Rob Herring <robh+dt@kernel.org>,
	Frank Rowand <frowand.list@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Li Chen <lchen@ambarella.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	linux-mm@kvack.org
Subject: [PATCH 4/4] sample/reserved_mem: Introduce a sample of struct page and dio support to no-map rmem
Date: Mon, 11 Jul 2022 20:24:54 +0800	[thread overview]
Message-ID: <20220711122459.13773-5-me@linux.beauty> (raw)
In-Reply-To: <20220711122459.13773-1-me@linux.beauty>

From: Li Chen <lchen@ambarella.com>

This sample driver shows how to build struct pages support to no-map rmem.

Signed-off-by: Li Chen <lchen@ambarella.com>
Change-Id: Ie78494fa86fda40ceb73eab3b8ba505d0ad851a1
---
 samples/Kconfig                 |   7 ++
 samples/Makefile                |   1 +
 samples/reserved_mem/Makefile   |   2 +
 samples/reserved_mem/rmem_dio.c | 116 ++++++++++++++++++++++++++++++++
 4 files changed, 126 insertions(+)
 create mode 100755 samples/reserved_mem/Makefile
 create mode 100755 samples/reserved_mem/rmem_dio.c

diff --git a/samples/Kconfig b/samples/Kconfig
index b0503ef058d3..d83ba02ec215 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -6,6 +6,13 @@ menuconfig SAMPLES
 
 if SAMPLES
 
+config RESERVED_MEM_DIO
+	tristate "Build example reserved mem dio support"
+	depends on OF_RESERVED_MEM_DIO_SUPPORT
+	help
+	   Build kernel space sample module to show how to add struct
+		 page and dio support to reserved memory.
+
 config SAMPLE_AUXDISPLAY
 	bool "auxdisplay sample"
 	depends on CC_CAN_LINK
diff --git a/samples/Makefile b/samples/Makefile
index 087e0988ccc5..106a386869eb 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for Linux samples code
 
+obj-$(CONFIG_RESERVED_MEM_DIO)		+= reserved_mem/
 subdir-$(CONFIG_SAMPLE_AUXDISPLAY)	+= auxdisplay
 subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs
 obj-$(CONFIG_SAMPLE_CONFIGFS)		+= configfs/
diff --git a/samples/reserved_mem/Makefile b/samples/reserved_mem/Makefile
new file mode 100755
index 000000000000..a4f5c8bc08dc
--- /dev/null
+++ b/samples/reserved_mem/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_RESERVED_MEM_DIO) += rmem_dio.o
diff --git a/samples/reserved_mem/rmem_dio.c b/samples/reserved_mem/rmem_dio.c
new file mode 100755
index 000000000000..ffb3395de63c
--- /dev/null
+++ b/samples/reserved_mem/rmem_dio.c
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Sample driver for reserved memory with struct page and dio support.
+ *
+ * wsps is abbr for with struct page support
+ *
+ * Copyright (C) 2022 Li Chen <lchen@ambarella.com>
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/fs.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <linux/mm.h>
+#include <linux/cdev.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/miscdevice.h>
+#include <linux/of_reserved_mem.h>
+#include <linux/platform_device.h>
+
+/*
+ * dts example
+ * rmem: rmem@1 {
+ *			compatible = "shared-dma-pool";
+ *			no-map;
+ *			size = <0x0 0x20000000>;
+ *		};
+ * perf {
+ *		compatible = "example,rmem";
+ *		memory-region = <&rmem>;
+ *	};
+ */
+
+static struct reserved_mem *rmem;
+
+static int rmem_wsps_open(struct inode *inode, struct file *filp)
+{
+	return 0;
+}
+
+static int rmem_wsps_release(struct inode *inode,
+					  struct file *filp)
+{
+	return 0;
+}
+
+static int rmem_wsps_mmap(struct file *file,
+					     struct vm_area_struct *vma)
+{
+	return reserved_mem_dio_mmap(file, vma, rmem);
+}
+
+static const struct file_operations rmem_wsps_remap_ops = {
+	.owner = THIS_MODULE,
+	.open = rmem_wsps_open,
+	.release = rmem_wsps_release,
+	.mmap = rmem_wsps_mmap,
+};
+
+static struct miscdevice rmem_wsps_miscdev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "rmem_wsps",
+	.fops = &rmem_wsps_remap_ops,
+};
+
+static int rmem_wsps_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	void *vaddr;
+	int ret;
+
+	ret = misc_register(&rmem_wsps_miscdev);
+	if (ret) {
+		dev_err(dev, "%s: misc_register failed, %d\n", __func__, ret);
+		return -ENODEV;
+	}
+
+	rmem = get_reserved_mem_from_dev(dev);
+	if (!rmem) {
+		dev_err(dev, "%s: failed to find reserved\n", __func__);
+		return -ENODEV;
+	}
+
+	vaddr = reserved_mem_memremap_pages(dev, rmem);
+	if (IS_ERR_OR_NULL(vaddr))
+		return PTR_ERR(vaddr);
+
+	return 0;
+}
+
+static const struct of_device_id rmem_dio_match[] = {
+	{
+		.compatible = "example,rmem",
+	},
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, rmem_dio_match);
+
+static struct platform_driver rmem_wsps_driver = {
+	.probe = rmem_wsps_probe,
+	.driver = {
+		.name = "rmem_wsps",
+		.of_match_table = rmem_dio_match,
+	},
+};
+
+static int __init rmem_wsps_init(void)
+{
+	return platform_driver_register(&rmem_wsps_driver);
+}
+
+module_init(rmem_wsps_init);
+MODULE_LICENSE("GPL");
-- 
2.25.1


  parent reply	other threads:[~2022-07-11 12:44 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-11 12:24 [PATCH 0/4] add struct page and Direct I/O support to reserved memory Li Chen
2022-07-11 12:24 ` [PATCH 1/4] of: add struct page support to rmem Li Chen
2022-07-11 13:36   ` Arnd Bergmann
2022-07-11 14:51     ` Li Chen
2022-07-11 15:06       ` Arnd Bergmann
2022-07-12  3:13         ` Li Chen
2022-07-16  0:38   ` kernel test robot
2022-07-18 13:21   ` Dan Carpenter
2022-07-11 12:24 ` [PATCH 2/4] mm/sparse: skip no-map memblock check when fill_subsection_map Li Chen
2022-07-11 14:53   ` David Hildenbrand
2022-07-12  4:23     ` Li Chen
2022-07-12  7:31       ` David Hildenbrand
2022-07-12  9:31         ` Li Chen
2022-07-14 18:45   ` kernel test robot
2022-07-11 12:24 ` [PATCH 3/4] arm64: mm: move memblock_clear_nomap after __add_pages Li Chen
2022-07-11 12:24 ` Li Chen [this message]
2022-07-11 13:28   ` [PATCH 4/4] sample/reserved_mem: Introduce a sample of struct page and dio support to no-map rmem Arnd Bergmann
2022-07-12  0:26     ` Li Chen
2022-07-12  7:50       ` Arnd Bergmann
2022-07-12  9:58         ` Li Chen
2022-07-12 10:08           ` Arnd Bergmann
2022-07-12 10:20             ` Arnd Bergmann
2022-07-12 10:55             ` Li Chen
2022-07-12 12:10               ` Arnd Bergmann
2022-08-04  7:17         ` Li Chen
2022-08-04  8:24           ` Arnd Bergmann
2022-08-04 10:07             ` Li Chen
2022-08-05 14:09               ` Arnd Bergmann
2022-08-05 15:28                 ` David Hildenbrand
2022-07-11 15:01 ` [PATCH 0/4] add struct page and Direct I/O support to reserved memory Christoph Hellwig
2022-07-11 16:05   ` Li Chen
2022-07-11 16:09     ` Christoph Hellwig
2022-07-12  0:14       ` Li Chen

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=20220711122459.13773-5-me@linux.beauty \
    --to=me@linux.beauty \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=lchen@ambarella.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=robh+dt@kernel.org \
    --cc=will@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).