Skip to content

Commit afa9296

Browse files
author
Maxime Ripard
committed
of: reserved_mem: Add functions to parse "memory-region"
JIRA: https://issues.redhat.com/browse/RHEL-125402 commit f4fcfdd Author: Rob Herring (Arm) <robh@kernel.org> Date: 2025-04-23 14:42:13 -0500 of: reserved_mem: Add functions to parse "memory-region" Drivers with "memory-region" properties currently have to do their own parsing of "memory-region" properties. The result is all the drivers have similar patterns of a call to parse "memory-region" and then get the region's address and size. As this is a standard property, it should have common functions for drivers to use. Add new functions to count the number of regions and retrieve the region's address as a resource. Reviewed-by: Daniel Baluta <daniel.baluta@nxp.com> Acked-by: Arnaud Pouliquen <arnaud.pouliquen@foss.st.com> Link: https://lore.kernel.org/r/20250423-dt-memory-region-v2-v2-1-2fbd6ebd3c88@kernel.org Signed-off-by: Rob Herring (Arm) <robh@kernel.org> Signed-off-by: Maxime Ripard <mripard@redhat.com>
1 parent e8135b6 commit afa9296

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

drivers/of/of_reserved_mem.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define pr_fmt(fmt) "OF: reserved mem: " fmt
1313

1414
#include <linux/err.h>
15+
#include <linux/ioport.h>
1516
#include <linux/libfdt.h>
1617
#include <linux/of.h>
1718
#include <linux/of_fdt.h>
@@ -740,3 +741,82 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
740741
return NULL;
741742
}
742743
EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
744+
745+
/**
746+
* of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
747+
* @np: node containing 'memory-region' property
748+
* @idx: index of 'memory-region' property to lookup
749+
* @res: Pointer to a struct resource to fill in with reserved region
750+
*
751+
* This function allows drivers to lookup a node's 'memory-region' property
752+
* entries by index and return a struct resource for the entry.
753+
*
754+
* Returns 0 on success with @res filled in. Returns -ENODEV if 'memory-region'
755+
* is missing or unavailable, -EINVAL for any other error.
756+
*/
757+
int of_reserved_mem_region_to_resource(const struct device_node *np,
758+
unsigned int idx, struct resource *res)
759+
{
760+
struct reserved_mem *rmem;
761+
762+
if (!np)
763+
return -EINVAL;
764+
765+
struct device_node __free(device_node) *target = of_parse_phandle(np, "memory-region", idx);
766+
if (!target || !of_device_is_available(target))
767+
return -ENODEV;
768+
769+
rmem = of_reserved_mem_lookup(target);
770+
if (!rmem)
771+
return -EINVAL;
772+
773+
resource_set_range(res, rmem->base, rmem->size);
774+
res->name = rmem->name;
775+
return 0;
776+
}
777+
EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource);
778+
779+
/**
780+
* of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource
781+
* @np: node containing 'memory-region' property
782+
* @name: name of 'memory-region' property entry to lookup
783+
* @res: Pointer to a struct resource to fill in with reserved region
784+
*
785+
* This function allows drivers to lookup a node's 'memory-region' property
786+
* entries by name and return a struct resource for the entry.
787+
*
788+
* Returns 0 on success with @res filled in, or a negative error-code on
789+
* failure.
790+
*/
791+
int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
792+
const char *name,
793+
struct resource *res)
794+
{
795+
int idx;
796+
797+
if (!name)
798+
return -EINVAL;
799+
800+
idx = of_property_match_string(np, "memory-region-names", name);
801+
if (idx < 0)
802+
return idx;
803+
804+
return of_reserved_mem_region_to_resource(np, idx, res);
805+
}
806+
EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_byname);
807+
808+
/**
809+
* of_reserved_mem_region_count() - Return the number of 'memory-region' entries
810+
* @np: node containing 'memory-region' property
811+
*
812+
* This function allows drivers to retrieve the number of entries for a node's
813+
* 'memory-region' property.
814+
*
815+
* Returns the number of entries on success, or negative error code on a
816+
* malformed property.
817+
*/
818+
int of_reserved_mem_region_count(const struct device_node *np)
819+
{
820+
return of_count_phandle_with_args(np, "memory-region", NULL);
821+
}
822+
EXPORT_SYMBOL_GPL(of_reserved_mem_region_count);

include/linux/of_reserved_mem.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
struct of_phandle_args;
99
struct reserved_mem_ops;
10+
struct resource;
1011

1112
struct reserved_mem {
1213
const char *name;
@@ -39,6 +40,12 @@ int of_reserved_mem_device_init_by_name(struct device *dev,
3940
void of_reserved_mem_device_release(struct device *dev);
4041

4142
struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
43+
int of_reserved_mem_region_to_resource(const struct device_node *np,
44+
unsigned int idx, struct resource *res);
45+
int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
46+
const char *name, struct resource *res);
47+
int of_reserved_mem_region_count(const struct device_node *np);
48+
4249
#else
4350

4451
#define RESERVEDMEM_OF_DECLARE(name, compat, init) \
@@ -63,6 +70,25 @@ static inline struct reserved_mem *of_reserved_mem_lookup(struct device_node *np
6370
{
6471
return NULL;
6572
}
73+
74+
static inline int of_reserved_mem_region_to_resource(const struct device_node *np,
75+
unsigned int idx,
76+
struct resource *res)
77+
{
78+
return -ENOSYS;
79+
}
80+
81+
static inline int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
82+
const char *name,
83+
struct resource *res)
84+
{
85+
return -ENOSYS;
86+
}
87+
88+
static inline int of_reserved_mem_region_count(const struct device_node *np)
89+
{
90+
return 0;
91+
}
6692
#endif
6793

6894
/**

0 commit comments

Comments
 (0)