2014-01-22 19:01:58

by Josh Cartwright

[permalink] [raw]
Subject: [PATCH RFC 0/4] reserved-memory regions/CMA in devicetree, again

Hey Marek-

What is the current status of the reserved-memory/CMA device tree support
series? It looks like it's stalled out a bit since the revert[2]. Hopefully
this series is a push in the right direction?

---
This set is an adaptation of Marek's original patchset posted here[1],
and then subsequently reverted here[2]. In [3], Grant proposed a new
set of bindings after discussion in Edinburgh.

This is a partially complete implementation of these proposed bindings.

It's functionality is now split into two pieces, the first of which
implements a default/fallback behavior for reserved-memory nodes; that
is, reserved memory nodes describing fixed-address static regions will
be unconditionally reserved, regardless of their intended use. In
addition, it provides an interface for adding additional reserved-memory
types, using a method similar to CLOCKSOURCE_OF_DECLARE().

The second piece uses the above method to provide support for
reserved-memory nodes whose compatible = "shared-dma-pool". It makes
use of CMA when available, and when a describing node has the 'reusable'
property.

There is basic support for dynamic allocation of memory reserved
regions, however, noticeably missing is support for additional
restrictions (use of alignment and alloc-ranges properties).

[1]: http://lkml.kernel.org/g/[email protected]
[2]: http://lkml.kernel.org/g/[email protected]
[3]: http://lkml.kernel.org/g/[email protected]

Grant Likely (1):
of: document bindings for reserved-memory nodes

Josh Cartwright (1):
drivers: of: implement reserved-memory handling for dma

Marek Szyprowski (2):
drivers: of: add initialization code for reserved memory
ARM: init: add support for reserved memory defined by device tree

.../bindings/reserved-memory/reserved-memory.txt | 137 +++++++++++++++
arch/arm/mm/init.c | 3 +
drivers/of/Kconfig | 13 ++
drivers/of/Makefile | 2 +
drivers/of/of_reserved_mem.c | 188 +++++++++++++++++++++
drivers/of/of_reserved_mem_dma.c | 178 +++++++++++++++++++
drivers/of/platform.c | 4 +
include/asm-generic/vmlinux.lds.h | 11 ++
include/linux/of_reserved_mem.h | 61 +++++++
9 files changed, 597 insertions(+)
create mode 100644 Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
create mode 100644 drivers/of/of_reserved_mem.c
create mode 100644 drivers/of/of_reserved_mem_dma.c
create mode 100644 include/linux/of_reserved_mem.h

--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation


2014-01-22 19:01:59

by Josh Cartwright

[permalink] [raw]
Subject: [PATCH RFC 3/4] ARM: init: add support for reserved memory defined by device tree

From: Marek Szyprowski <[email protected]>

Enable reserved memory initialization from device tree.

Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Laura Abbott <[email protected]>
Signed-off-by: Marek Szyprowski <[email protected]>
---
arch/arm/mm/init.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c
index 403ba90..5efe0b4 100644
--- a/arch/arm/mm/init.c
+++ b/arch/arm/mm/init.c
@@ -17,6 +17,7 @@
#include <linux/nodemask.h>
#include <linux/initrd.h>
#include <linux/of_fdt.h>
+#include <linux/of_reserved_mem.h>
#include <linux/highmem.h>
#include <linux/gfp.h>
#include <linux/memblock.h>
@@ -323,6 +324,8 @@ void __init arm_memblock_init(struct meminfo *mi,
if (mdesc->reserve)
mdesc->reserve();

+ early_init_dt_scan_reserved_mem();
+
/*
* reserve memory for DMA contigouos allocations,
* must come from DMA area inside low memory
--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation

2014-01-22 19:02:42

by Josh Cartwright

[permalink] [raw]
Subject: [PATCH RFC 1/4] drivers: of: add initialization code for reserved memory

From: Marek Szyprowski <[email protected]>

This patch adds device tree support for contiguous and reserved memory
regions defined in device tree.

Large memory blocks can be reliably reserved only during early boot.
This must happen before the whole memory management subsystem is
initialized, because we need to ensure that the given contiguous blocks
are not yet allocated by kernel. Also it must happen before kernel
mappings for the whole low memory are created, to ensure that there will
be no mappings (for reserved blocks) or mapping with special properties
can be created (for CMA blocks). This all happens before device tree
structures are unflattened, so we need to get reserved memory layout
directly from fdt.

Later, those reserved memory regions are assigned to devices on each
device structure initialization.

Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Laura Abbott <[email protected]>
Signed-off-by: Marek Szyprowski <[email protected]>
[joshc: rework to implement new DT binding, provide mechanism for
plugging in new reserved-memory node handlers via
RESERVEDMEM_OF_DECLARE]
Signed-off-by: Josh Cartwright <[email protected]>
---
drivers/of/Kconfig | 6 ++
drivers/of/Makefile | 1 +
drivers/of/of_reserved_mem.c | 188 ++++++++++++++++++++++++++++++++++++++
drivers/of/platform.c | 4 +
include/asm-generic/vmlinux.lds.h | 11 +++
include/linux/of_reserved_mem.h | 61 +++++++++++++
6 files changed, 271 insertions(+)
create mode 100644 drivers/of/of_reserved_mem.c
create mode 100644 include/linux/of_reserved_mem.h

diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index c6973f1..aba13df 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -75,4 +75,10 @@ config OF_MTD
depends on MTD
def_bool y

+config OF_RESERVED_MEM
+ depends on HAVE_MEMBLOCK
+ def_bool y
+ help
+ Helpers to allow for reservation of memory regions
+
endmenu # OF
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index efd0510..ed9660a 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_OF_MDIO) += of_mdio.o
obj-$(CONFIG_OF_PCI) += of_pci.o
obj-$(CONFIG_OF_PCI_IRQ) += of_pci_irq.o
obj-$(CONFIG_OF_MTD) += of_mtd.o
+obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o
diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
new file mode 100644
index 0000000..9fcafb5
--- /dev/null
+++ b/drivers/of/of_reserved_mem.c
@@ -0,0 +1,188 @@
+/*
+ * Device tree based initialization code for reserved memory.
+ *
+ * Copyright (c) 2013, The Linux Foundation. All Rights Reserved.
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.
+ * http://www.samsung.com
+ * Author: Marek Szyprowski <[email protected]>
+ * Author: Josh Cartwright <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License or (at your optional) any later version of the license.
+ */
+#include <linux/memblock.h>
+#include <linux/err.h>
+#include <linux/of.h>
+#include <linux/of_fdt.h>
+#include <linux/of_platform.h>
+#include <linux/mm.h>
+#include <linux/sizes.h>
+#include <linux/of_reserved_mem.h>
+
+#define MAX_RESERVED_REGIONS 16
+static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
+static int reserved_mem_count;
+
+static int __init rmem_default_early_setup(struct reserved_mem *rmem,
+ unsigned long node,
+ const char *uname)
+{
+ unsigned long len;
+ __be32 *prop;
+ int err;
+
+ prop = of_get_flat_dt_prop(node, "reg", &len);
+ if (!prop) {
+ pr_err("reg property missing for reserved-memory node '%s'\n",
+ uname);
+ err = -EINVAL;
+ goto out;
+ }
+
+ if (len < (dt_root_size_cells + dt_root_addr_cells) * sizeof(__be32)) {
+ pr_err("invalid reg property for reserved-memory node '%s'\n",
+ uname);
+ err = -EINVAL;
+ goto out;
+ }
+
+ rmem->base = dt_mem_next_cell(dt_root_addr_cells, &prop);
+ rmem->size = dt_mem_next_cell(dt_root_size_cells, &prop);
+
+ if (of_get_flat_dt_prop(node, "no-map", NULL))
+ err = memblock_remove(rmem->base, rmem->size);
+ else
+ err = memblock_reserve(rmem->base, rmem->size);
+
+ pr_info("Reserved mem: found '%s', memory base %pa, size %ld MiB\n",
+ uname, &rmem->base, (unsigned long)rmem->size / SZ_1M);
+
+out:
+ return err;
+}
+
+static const struct of_device_id rmem_default_id
+ __used __section(__reservedmem_of_table_end) = {
+ .data = rmem_default_early_setup,
+};
+
+static int __init fdt_scan_reserved_mem(unsigned long node, const char *uname,
+ int depth, void *data)
+{
+ struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
+ extern const struct of_device_id __reservedmem_of_table[];
+ reservedmem_of_init_fn initfn;
+ const struct of_device_id *id;
+ const char *status;
+
+ if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
+ pr_err("Not enough space for reserved-memory regions.\n");
+ return -ENOSPC;
+ }
+
+ status = of_get_flat_dt_prop(node, "status", NULL);
+ if (status && strcmp(status, "okay") != 0)
+ return 0;
+
+ /*
+ * The default handler above ensures this section is terminated with a
+ * id whose compatible string is empty
+ */
+ for (id = __reservedmem_of_table; ; id++) {
+ const char *compat = id->compatible;
+
+ if (!compat[0] || of_flat_dt_is_compatible(node, compat)) {
+ initfn = id->data;
+ break;
+ }
+ }
+
+ if (!initfn(rmem, node, uname)) {
+ strlcpy(rmem->name, uname, sizeof(rmem->name));
+ reserved_mem_count++;
+ }
+
+ return 0;
+}
+
+static struct reserved_mem *find_rmem(struct device_node *np)
+{
+ const char *name;
+ unsigned int i;
+
+ name = kbasename(np->full_name);
+
+ for (i = 0; i < reserved_mem_count; i++)
+ if (strcmp(name, reserved_mem[i].name) == 0)
+ return &reserved_mem[i];
+
+ return NULL;
+}
+
+/**
+ * of_reserved_mem_device_init() - assign reserved memory region to given device
+ *
+ * This function assign memory region pointed by "memory-region" device tree
+ * property to the given device.
+ */
+void of_reserved_mem_device_init(struct platform_device *pdev)
+{
+ struct device_node *np = pdev->dev.of_node;
+ struct reserved_mem *rmem;
+ struct of_phandle_args s;
+ unsigned int i;
+
+ for (i = 0; of_parse_phandle_with_args(np, "memory-region",
+ "#memory-region-cells", i, &s) == 0; i++) {
+
+ rmem = find_rmem(s.np);
+ of_node_put(s.np);
+
+ if (!rmem || !rmem->ops || !rmem->ops->device_init)
+ continue;
+
+ rmem->ops->device_init(rmem, pdev, &s);
+ }
+}
+
+/**
+ * of_reserved_mem_device_release() - release reserved memory device structures
+ *
+ * This function releases structures allocated for memory region handling for
+ * the given device.
+ */
+void of_reserved_mem_device_release(struct platform_device *pdev)
+{
+ struct device_node *np = pdev->dev.of_node;
+ struct reserved_mem *rmem;
+ struct of_phandle_args s;
+ unsigned int i;
+
+ for (i = 0; of_parse_phandle_with_args(np, "memory-region",
+ "#memory-region-cells", i, &s) == 0; i++) {
+
+ rmem = find_rmem(s.np);
+ of_node_put(s.np);
+
+ if (!rmem || !rmem->ops || !rmem->ops->device_release)
+ continue;
+
+ rmem->ops->device_release(rmem, pdev);
+ }
+}
+
+/**
+ * early_init_dt_scan_reserved_mem() - create reserved memory regions
+ *
+ * This function grabs memory from early allocator for device exclusive use
+ * defined in device tree structures. It should be called by arch specific code
+ * once the early allocator (memblock) has been activated and all other
+ * subsystems have already allocated/reserved memory.
+ */
+void __init early_init_dt_scan_reserved_mem(void)
+{
+ of_scan_flat_dt_by_path("/reserved-memory", fdt_scan_reserved_mem,
+ NULL);
+}
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index 404d1da..b6d3cea 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -21,6 +21,7 @@
#include <linux/of_device.h>
#include <linux/of_irq.h>
#include <linux/of_platform.h>
+#include <linux/of_reserved_mem.h>
#include <linux/platform_device.h>

const struct of_device_id of_default_bus_match_table[] = {
@@ -220,6 +221,8 @@ static struct platform_device *of_platform_device_create_pdata(
dev->dev.bus = &platform_bus_type;
dev->dev.platform_data = platform_data;

+ of_reserved_mem_device_init(dev);
+
/* We do not fill the DMA ops for platform devices by default.
* This is currently the responsibility of the platform code
* to do such, possibly using a device notifier
@@ -227,6 +230,7 @@ static struct platform_device *of_platform_device_create_pdata(

if (of_device_add(dev) != 0) {
platform_device_put(dev);
+ of_reserved_mem_device_release(dev);
return NULL;
}

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index bc2121f..f10f64f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -167,6 +167,16 @@
#define CLK_OF_TABLES()
#endif

+#ifdef CONFIG_OF_RESERVED_MEM
+#define RESERVEDMEM_OF_TABLES() \
+ . = ALIGN(8); \
+ VMLINUX_SYMBOL(__reservedmem_of_table) = .; \
+ *(__reservedmem_of_table) \
+ *(__reservedmem_of_table_end)
+#else
+#define RESERVEDMEM_OF_TABLES()
+#endif
+
#define KERNEL_DTB() \
STRUCT_ALIGN(); \
VMLINUX_SYMBOL(__dtb_start) = .; \
@@ -490,6 +500,7 @@
TRACE_SYSCALLS() \
MEM_DISCARD(init.rodata) \
CLK_OF_TABLES() \
+ RESERVEDMEM_OF_TABLES() \
CLKSRC_OF_TABLES() \
KERNEL_DTB() \
IRQCHIP_OF_MATCH_TABLE()
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
new file mode 100644
index 0000000..a2de510
--- /dev/null
+++ b/include/linux/of_reserved_mem.h
@@ -0,0 +1,61 @@
+#ifndef __OF_RESERVED_MEM_H
+#define __OF_RESERVED_MEM_H
+
+struct cma;
+struct platform_device;
+struct of_phandle_args;
+struct reserved_mem_ops;
+
+struct reserved_mem {
+ const struct reserved_mem_ops *ops;
+ char name[32];
+ union {
+ struct cma *cma;
+ struct {
+ phys_addr_t base;
+ phys_addr_t size;
+ };
+ };
+};
+
+struct reserved_mem_ops {
+ void (*device_init)(struct reserved_mem *rmem,
+ struct platform_device *pdev,
+ struct of_phandle_args *args);
+ void (*device_release)(struct reserved_mem *rmem,
+ struct platform_device *pdev);
+};
+
+typedef int (*reservedmem_of_init_fn)(struct reserved_mem *rmem,
+ unsigned long node, const char *uname);
+
+#ifdef CONFIG_OF_RESERVED_MEM
+void of_reserved_mem_device_init(struct platform_device *pdev);
+void of_reserved_mem_device_release(struct platform_device *pdev);
+void early_init_dt_scan_reserved_mem(void);
+
+#define RESERVEDMEM_OF_DECLARE(name, compat, init) \
+ static const struct of_device_id __reservedmem_of_table_##name \
+ __used __section(__reservedmem_of_table) \
+ = { .compatible = compat, \
+ .data = (init == (reservedmem_of_init_fn)NULL) ? \
+ init : init }
+
+#else
+static inline void of_reserved_mem_device_init(struct platform_device *pdev) { }
+
+static inline
+void of_reserved_mem_device_release(struct platform_device *pdev) { }
+
+static inline void early_init_dt_scan_reserved_mem(void) { }
+
+#define RESERVEDMEM_OF_DECLARE(name, compat, init) \
+ static const struct of_device_id __reservedmem_of_table_##name \
+ __attribute__((unused)) \
+ = { .compatible = compat, \
+ .data = (init == (reservedmem_of_init_fn)NULL) ? \
+ init : init }
+
+#endif
+
+#endif /* __OF_RESERVED_MEM_H */
--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation

2014-01-22 19:03:05

by Josh Cartwright

[permalink] [raw]
Subject: [PATCH RFC 4/4] of: document bindings for reserved-memory nodes

From: Grant Likely <[email protected]>

Reserved memory nodes allow for the reservation of static (fixed
address) regions, or dynamically allocated regions for a specific
purpose.

[joshc: Based on binding document proposed (in non-patch form) here:
http://lkml.kernel.org/g/[email protected]
adapted to support #memory-region-cells]

Cc: Marek Szyprowski <[email protected]>
Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Laura Abbott <[email protected]>
Signed-off-by: Josh Cartwright <[email protected]>
---
Hey Grant-

Seeing as you are the primary author of this binding, with only a few minor
changes on my part, I've marked you as the commit author. Let me know if
that's a problem.

Josh

.../bindings/reserved-memory/reserved-memory.txt | 137 +++++++++++++++++++++
1 file changed, 137 insertions(+)
create mode 100644 Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt

diff --git a/Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt b/Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
new file mode 100644
index 0000000..7cd7829
--- /dev/null
+++ b/Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
@@ -0,0 +1,137 @@
+*** Reserved memory regions ***
+
+Reserved memory is specified as a node under the /reserved-memory node.
+The operating system shall exclude reserved memory from normal usage
+one can create child nodes describing particular reserved (excluded from
+normal use) memory regions. Such memory regions are usually designed for
+the special usage by various device drivers.
+
+Parameters for each memory region can be encoded into the device tree
+with the following nodes:
+
+/reserved-memory node
+---------------------
+#address-cells, #size-cells (required) - standard definition
+ - Should use the same values as the root node
+#memory-region-cells (required) - dictates number of cells used in the child
+ nodes memory-region specifier
+ranges (required) - standard definition
+ - Should be empty
+
+/reserved-memory/ child nodes
+-----------------------------
+Each child of the reserved-memory node specifies one or more regions of
+reserved memory. Each child node may either use a 'reg' property to
+specify a specific range of reserved memory, or a 'size' property with
+optional constraints to request a dynamically allocated block of memory.
+
+Following the generic-names recommended practice, node names should
+reflect the purpose of the node (ie. "framebuffer" or "dma-pool"). Unit
+address (@<address>) should be appended to the name if the node is a
+static allocation.
+
+Properties:
+Requires either a) or b) below.
+a) static allocation
+ reg (required) - standard definition
+b) dynamic allocation
+ size (required) - length based on parent's #size-cells
+ - Size in bytes of memory to reserve.
+ alignment (optional) - length based on parent's #size-cells
+ - Address boundary for alignment of allocation.
+ alloc-ranges (optional) - prop-encoded-array (address, length pairs).
+ - Specifies regions of memory that are
+ acceptable to allocate from.
+
+If both reg and size are present, then the reg property takes precedence
+and size is ignored.
+
+Additional properties:
+compatible (optional) - standard definition
+ - may contain the following strings:
+ - shared-dma-pool: This indicates a region of memory meant to be
+ used as a shared pool of DMA buffers for a set of devices. It can
+ be used by an operating system to instanciate the necessary pool
+ management subsystem if necessary.
+ - vendor specific string in the form <vendor>,[<device>-]<usage>
+no-map (optional) - empty property
+ - Indicates the operating system must not create a virtual mapping
+ of the region as part of its standard mapping of system memory,
+ nor permit speculative access to it under any circumstances other
+ than under the control of the device driver using the region.
+reusable (optional) - empty property
+ - The operating system can use the memory in this region with the
+ limitation that the device driver(s) owning the region need to be
+ able to reclaim it back. Typically that means that the operating
+ system can use that region to store volatile or cached data that
+ can be otherwise regenerated or migrated elsewhere.
+
+Linux implementation note:
+- If a "linux,cma-default" property is present, then Linux will use the
+ region for the default pool of the contiguous memory allocator.
+
+Device node references to reserved memory
+-----------------------------------------
+Regions in the /reserved-memory node may be referenced by other device
+nodes by adding a memory-region property to the device node.
+
+memory-region (optional) - phandle, specifier pairs to children of /reserved-memory
+
+Example
+-------
+This example defines 3 contiguous regions are defined for Linux kernel:
+one default of all device drivers (named linux,cma@72000000 and 64MiB in size),
+one dedicated to the framebuffer device (named framebuffer@78000000, 8MiB), and
+one for multimedia processing (named multimedia-memory@77000000, 64MiB).
+
+/ {
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ memory {
+ reg = <0x40000000 0x40000000>;
+ };
+
+ reserved-memory {
+ #address-cells = <1>;
+ #size-cells = <1>;
+ ranges;
+
+ /* global autoconfigured region for contiguous allocations */
+ linux,cma {
+ compatible = "shared-dma-pool";
+ #memory-region-cells = <0>;
+ size = <0x4000000>;
+ alignment = <0x2000>;
+ linux,cma-default;
+ };
+
+ display_reserved: framebuffer@78000000 {
+ #memory-region-cells = <0>;
+ reg = <0x78000000 0x800000>;
+ };
+
+ multimedia_reserved: multimedia@77000000 {
+ compatible = "acme,multimedia-memory";
+ #memory-region-cells = <1>;
+ reg = <0x77000000 0x4000000>;
+ };
+ };
+
+ /* ... */
+
+ fb0: video@12300000 {
+ memory-region = <&display_reserved>;
+ /* ... */
+ };
+
+ scaler: scaler@12500000 {
+ memory-region = <&multimedia_reserved 0xdeadbeef>;
+ /* ... */
+ };
+
+ codec: codec@12600000 {
+ memory-region = <&multimedia_reserved 0xfeebdaed>;
+ /* ... */
+ };
+};
--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation

2014-01-22 19:03:03

by Josh Cartwright

[permalink] [raw]
Subject: [PATCH RFC 2/4] drivers: of: implement reserved-memory handling for dma

Add support for handling 'shared-dma-pool' reserved-memory nodes.

Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Laura Abbott <[email protected]>
Cc: Marek Szyprowski <[email protected]>
Signed-off-by: Josh Cartwright <[email protected]>
---
drivers/of/Kconfig | 7 ++
drivers/of/Makefile | 1 +
drivers/of/of_reserved_mem_dma.c | 178 +++++++++++++++++++++++++++++++++++++++
3 files changed, 186 insertions(+)
create mode 100644 drivers/of/of_reserved_mem_dma.c

diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index aba13df..ca58fb3e 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -81,4 +81,11 @@ config OF_RESERVED_MEM
help
Helpers to allow for reservation of memory regions

+config OF_RESERVED_MEM_DMA
+ depends on OF_RESERVED_MEM
+ depends on DMA_CMA || HAVE_GENERIC_DMA_COHERENT
+ def_bool y
+ help
+ Helpers for reserving memory regions for DMA use
+
endmenu # OF
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index ed9660a..6142227 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_OF_PCI) += of_pci.o
obj-$(CONFIG_OF_PCI_IRQ) += of_pci_irq.o
obj-$(CONFIG_OF_MTD) += of_mtd.o
obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o
+obj-$(CONFIG_OF_RESERVED_MEM_DMA) += of_reserved_mem_dma.o
diff --git a/drivers/of/of_reserved_mem_dma.c b/drivers/of/of_reserved_mem_dma.c
new file mode 100644
index 0000000..e24f02d
--- /dev/null
+++ b/drivers/of/of_reserved_mem_dma.c
@@ -0,0 +1,178 @@
+/*
+ * Device tree based initialization code for DMA reserved regions.
+ *
+ * Copyright (c) 2013, The Linux Foundation. All Rights Reserved.
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.
+ * http://www.samsung.com
+ * Author: Marek Szyprowski <[email protected]>
+ * Author: Josh Cartwright <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License or (at your optional) any later version of the license.
+ */
+#include <linux/memblock.h>
+#include <linux/err.h>
+#include <linux/of.h>
+#include <linux/of_fdt.h>
+#include <linux/of_platform.h>
+#include <linux/mm.h>
+#include <linux/sizes.h>
+#include <linux/mm_types.h>
+#include <linux/dma-contiguous.h>
+#include <linux/dma-mapping.h>
+#include <linux/of_reserved_mem.h>
+
+static void rmem_dma_device_init(struct reserved_mem *rmem,
+ struct platform_device *pdev,
+ struct of_phandle_args *args)
+{
+ dma_declare_coherent_memory(&pdev->dev, rmem->base, rmem->base,
+ rmem->size, DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE);
+}
+
+static void rmem_dma_device_release(struct reserved_mem *rmem,
+ struct platform_device *pdev)
+{
+ dma_release_declared_memory(&pdev->dev);
+}
+
+static const struct reserved_mem_ops rmem_dma_ops = {
+ .device_init = rmem_dma_device_init,
+ .device_release = rmem_dma_device_release,
+};
+
+static int __init rmem_dma_get_reg(unsigned long node, const char *uname,
+ phys_addr_t *base, phys_addr_t *size)
+{
+ unsigned long len;
+ __be32 *prop;
+
+ prop = of_get_flat_dt_prop(node, "reg", &len);
+ if (!prop)
+ return -EINVAL;
+
+ if (len < (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32)) {
+ pr_err("Malformed 'shared-dma-pool' reserved-mem node '%s'.\n",
+ uname);
+ return -EINVAL;
+ }
+
+ *base = dt_mem_next_cell(dt_root_addr_cells, &prop);
+ *size = dt_mem_next_cell(dt_root_size_cells, &prop);
+ return 0;
+}
+
+static int __init rmem_dma_get_size(unsigned long node, const char *uname,
+ phys_addr_t *size)
+{
+ unsigned long len;
+ __be32 *prop;
+
+ prop = of_get_flat_dt_prop(node, "size", &len);
+ if (!prop)
+ return -EINVAL;
+
+ if (len < dt_root_size_cells * sizeof(__be32)) {
+ pr_err("Malformed 'shared-dma-pool' reserved-mem node '%s'.\n",
+ uname);
+ return -EINVAL;
+ }
+
+ *size = dt_mem_next_cell(dt_root_size_cells, &prop);
+ return 0;
+}
+
+static void rmem_cma_device_init(struct reserved_mem *rmem,
+ struct platform_device *pdev,
+ struct of_phandle_args *args)
+{
+ dev_set_cma_area(&pdev->dev, rmem->cma);
+}
+
+static const struct reserved_mem_ops rmem_cma_ops = {
+ .device_init = rmem_cma_device_init,
+};
+
+static int __init rmem_cma_reserve_area(struct reserved_mem *rmem,
+ unsigned long node, const char *uname)
+{
+ int err;
+
+ err = dma_contiguous_reserve_area(rmem->size, rmem->base, 0,
+ &rmem->cma);
+ if (err) {
+ pr_err("Unable to setup CMA reserved-mem region for '%s'.\n",
+ uname);
+ return err;
+ }
+
+ if (of_get_flat_dt_prop(node, "linux,cma-default", NULL))
+ dma_contiguous_set_default(rmem->cma);
+
+ rmem->ops = &rmem_cma_ops;
+ return 0;
+}
+
+static int __init rmem_cma_setup(struct reserved_mem *rmem,
+ unsigned long node,
+ const char *uname)
+{
+ int err;
+
+ err = rmem_dma_get_reg(node, uname, &rmem->base, &rmem->size);
+ if (!err)
+ goto out_done;
+
+ rmem->base = 0;
+ err = rmem_dma_get_size(node, uname, &rmem->size);
+ if (err)
+ goto out_err;
+
+out_done:
+ return rmem_cma_reserve_area(rmem, node, uname);
+
+out_err:
+ pr_err("Malformed 'shared-dma-pool' node '%s'.\n", uname);
+ return err;
+}
+
+static int __init rmem_dma_excl_setup(struct reserved_mem *rmem,
+ unsigned long node,
+ const char *uname)
+{
+ int err;
+
+ err = rmem_dma_get_reg(node, uname, &rmem->base, &rmem->size);
+ if (!err)
+ goto out_done;
+
+ err = rmem_dma_get_size(node, uname, &rmem->size);
+ if (err)
+ goto out_err;
+
+ rmem->base = memblock_alloc_base(rmem->size, PAGE_SIZE,
+ MEMBLOCK_ALLOC_ANYWHERE);
+ memblock_free(rmem->base, rmem->size);
+
+out_done:
+ rmem->ops = &rmem_dma_ops;
+ return memblock_remove(rmem->base, rmem->size);
+
+out_err:
+ pr_err("Unable to setup reserved-mem region '%s' for DMA.\n", uname);
+ return err;
+}
+
+static int __init rmem_dma_setup(struct reserved_mem *rmem,
+ unsigned long node,
+ const char *uname)
+{
+ if (IS_ENABLED(CONFIG_DMA_CMA) &&
+ of_get_flat_dt_prop(node, "reusable", NULL))
+ return rmem_cma_setup(rmem, node, uname);
+
+ return rmem_dma_excl_setup(rmem, node, uname);
+}
+RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);
--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation