Skip to content

Commit b50c788

Browse files
committed
of/address: Add of_range_count() helper
Some users need a count of the number of ranges entries before iterating over the entries. Typically this is for allocating some data structure based on the size. Add a helper, of_range_count(), to get the count. The helper must be called with an struct of_range_parser initialized by of_range_parser_init(). Link: https://lore.kernel.org/r/20230328-dt-address-helpers-v1-4-e2456c3e77ab@kernel.org Signed-off-by: Rob Herring <robh@kernel.org>
1 parent 3d5089c commit b50c788

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

drivers/of/unittest.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ static void __init of_unittest_bus_ranges(void)
10141014
struct of_range range;
10151015
struct of_range_parser parser;
10161016
struct resource res;
1017-
int ret, i = 0;
1017+
int ret, count, i = 0;
10181018

10191019
np = of_find_node_by_path("/testcase-data/address-tests");
10201020
if (!np) {
@@ -1040,6 +1040,11 @@ static void __init of_unittest_bus_ranges(void)
10401040
"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
10411041
np, &res);
10421042

1043+
count = of_range_count(&parser);
1044+
unittest(count == 2,
1045+
"of_range_count wrong size on node %pOF count=%d\n",
1046+
np, count);
1047+
10431048
/*
10441049
* Get the "ranges" from the device tree
10451050
*/

include/linux/of_address.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ struct of_pci_range {
3535
for (; of_pci_range_parser_one(parser, range);)
3636
#define for_each_of_range for_each_of_pci_range
3737

38+
/*
39+
* of_range_count - Get the number of "ranges" or "dma-ranges" entries
40+
* @parser: Parser state initialized by of_range_parser_init()
41+
*
42+
* Returns the number of entries or 0 if none.
43+
*
44+
* Note that calling this within or after the for_each_of_range() iterator will
45+
* be inaccurate giving the number of entries remaining.
46+
*/
47+
static inline int of_range_count(const struct of_range_parser *parser)
48+
{
49+
if (!parser || !parser->node || !parser->range || parser->range == parser->end)
50+
return 0;
51+
return (parser->end - parser->range) / (parser->na + parser->pna + parser->ns);
52+
}
53+
3854
/* Translate a DMA address from device space to CPU space */
3955
extern u64 of_translate_dma_address(struct device_node *dev,
4056
const __be32 *in_addr);

0 commit comments

Comments
 (0)