]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
of/irq: Introduce for_each_of_imap_item
authorHerve Codina (Schneider Electric) <herve.codina@bootlin.com>
Wed, 14 Jan 2026 09:39:30 +0000 (10:39 +0100)
committerGeert Uytterhoeven <geert+renesas@glider.be>
Thu, 15 Jan 2026 11:03:27 +0000 (12:03 +0100)
for_each_of_imap_item is an iterator designed to help a driver to parse
an interrupt-map property.

Indeed some drivers need to know details about the interrupt mapping
described in the device-tree in order to set internal registers
accordingly.

Signed-off-by: Herve Codina (Schneider Electric) <herve.codina@bootlin.com>
Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://patch.msgid.link/20260114093938.1089936-2-herve.codina@bootlin.com
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
drivers/of/irq.c
include/linux/of_irq.h

index e3816819dbfef1fb2de6680aee29ab4167c90aa7..f374d8b212b8669cb9075abcb1d80fdad67fadc8 100644 (file)
@@ -157,6 +157,76 @@ const __be32 *of_irq_parse_imap_parent(const __be32 *imap, int len, struct of_ph
        return imap;
 }
 
+int of_imap_parser_init(struct of_imap_parser *parser, struct device_node *node,
+                       struct of_imap_item *item)
+{
+       int imaplen;
+       u32 tmp;
+       int ret;
+
+       /*
+        * parent_offset is the offset where the parent part is starting.
+        * In other words, the offset where the parent interrupt controller
+        * phandle is present.
+        *
+        * Compute this offset (child #interrupt-cells + child #address-cells)
+        */
+       parser->parent_offset = of_bus_n_addr_cells(node);
+
+       ret = of_property_read_u32(node, "#interrupt-cells", &tmp);
+       if (ret)
+               return ret;
+
+       parser->parent_offset += tmp;
+
+       if (WARN(parser->parent_offset > ARRAY_SIZE(item->child_imap),
+                "child part size = %u, cannot fit in array of %zu items",
+                parser->parent_offset, ARRAY_SIZE(item->child_imap)))
+               return -EINVAL;
+
+       parser->imap = of_get_property(node, "interrupt-map", &imaplen);
+       if (!parser->imap)
+               return -ENOENT;
+
+       imaplen /= sizeof(*parser->imap);
+       parser->imap_end = parser->imap + imaplen;
+
+       memset(item, 0, sizeof(*item));
+       item->child_imap_count = parser->parent_offset;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(of_imap_parser_init);
+
+struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
+                                       struct of_imap_item *item)
+{
+       const __be32 *imap_parent, *imap_next;
+       int i;
+
+       /* Release previously get parent node */
+       of_node_put(item->parent_args.np);
+
+       if (parser->imap + parser->parent_offset + 1 >= parser->imap_end)
+               return NULL;
+
+       imap_parent = parser->imap + parser->parent_offset;
+
+       imap_next = of_irq_parse_imap_parent(imap_parent,
+                                            parser->imap_end - imap_parent,
+                                            &item->parent_args);
+       if (!imap_next)
+               return NULL;
+
+       for (i = 0; i < parser->parent_offset; i++)
+               item->child_imap[i] = be32_to_cpu(*(parser->imap + i));
+
+       parser->imap = imap_next;
+
+       return item;
+}
+EXPORT_SYMBOL_GPL(of_imap_parser_one);
+
 /**
  * of_irq_parse_raw - Low level interrupt tree parsing
  * @addr:      address specifier (start of "reg" property of the device) in be32 format
index 1c2bc028180712082f9d955275e333949b882970..2a64d8cecaaeb6859e826350d09f92cf77a11282 100644 (file)
 
 typedef int (*of_irq_init_cb_t)(struct device_node *, struct device_node *);
 
+struct of_imap_parser {
+       struct device_node *node;
+       const __be32 *imap;
+       const __be32 *imap_end;
+       u32 parent_offset;
+};
+
+struct of_imap_item {
+       struct of_phandle_args parent_args;
+       u32 child_imap_count;
+       u32 child_imap[16]; /* Arbitrary size.
+                            * Should be #address-cells + #interrupt-cells but
+                            * avoid using allocation and so, expect that 16
+                            * should be enough
+                            */
+};
+
+/*
+ * If the iterator is exited prematurely (break, goto, return) of_node_put() has
+ * to be called on item.parent_args.np
+ */
+#define for_each_of_imap_item(parser, item) \
+       for (; of_imap_parser_one(parser, item);)
+
 /*
  * Workarounds only applied to 32bit powermac machines
  */
@@ -49,6 +73,11 @@ extern int of_irq_get_byname(struct device_node *dev, const char *name);
 extern int of_irq_to_resource_table(struct device_node *dev,
                struct resource *res, int nr_irqs);
 extern struct device_node *of_irq_find_parent(struct device_node *child);
+extern int of_imap_parser_init(struct of_imap_parser *parser,
+                              struct device_node *node,
+                              struct of_imap_item *item);
+extern struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
+                                              struct of_imap_item *item);
 extern struct irq_domain *of_msi_get_domain(struct device *dev,
                                            const struct device_node *np,
                                            enum irq_domain_bus_token token);
@@ -92,7 +121,17 @@ static inline void *of_irq_find_parent(struct device_node *child)
 {
        return NULL;
 }
-
+static inline int of_imap_parser_init(struct of_imap_parser *parser,
+                                     struct device_node *node,
+                                     struct of_imap_item *item)
+{
+       return -ENOSYS;
+}
+static inline struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
+                                                     struct of_imap_item *item)
+{
+       return NULL;
+}
 static inline struct irq_domain *of_msi_get_domain(struct device *dev,
                                                   struct device_node *np,
                                                   enum irq_domain_bus_token token)