Skip to content

Commit 49d2cda

Browse files
hcodinageertu
authored andcommitted
of/irq: Introduce for_each_of_imap_item
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>
1 parent 1bea7e9 commit 49d2cda

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

drivers/of/irq.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,76 @@ const __be32 *of_irq_parse_imap_parent(const __be32 *imap, int len, struct of_ph
157157
return imap;
158158
}
159159

160+
int of_imap_parser_init(struct of_imap_parser *parser, struct device_node *node,
161+
struct of_imap_item *item)
162+
{
163+
int imaplen;
164+
u32 tmp;
165+
int ret;
166+
167+
/*
168+
* parent_offset is the offset where the parent part is starting.
169+
* In other words, the offset where the parent interrupt controller
170+
* phandle is present.
171+
*
172+
* Compute this offset (child #interrupt-cells + child #address-cells)
173+
*/
174+
parser->parent_offset = of_bus_n_addr_cells(node);
175+
176+
ret = of_property_read_u32(node, "#interrupt-cells", &tmp);
177+
if (ret)
178+
return ret;
179+
180+
parser->parent_offset += tmp;
181+
182+
if (WARN(parser->parent_offset > ARRAY_SIZE(item->child_imap),
183+
"child part size = %u, cannot fit in array of %zu items",
184+
parser->parent_offset, ARRAY_SIZE(item->child_imap)))
185+
return -EINVAL;
186+
187+
parser->imap = of_get_property(node, "interrupt-map", &imaplen);
188+
if (!parser->imap)
189+
return -ENOENT;
190+
191+
imaplen /= sizeof(*parser->imap);
192+
parser->imap_end = parser->imap + imaplen;
193+
194+
memset(item, 0, sizeof(*item));
195+
item->child_imap_count = parser->parent_offset;
196+
197+
return 0;
198+
}
199+
EXPORT_SYMBOL_GPL(of_imap_parser_init);
200+
201+
struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
202+
struct of_imap_item *item)
203+
{
204+
const __be32 *imap_parent, *imap_next;
205+
int i;
206+
207+
/* Release previously get parent node */
208+
of_node_put(item->parent_args.np);
209+
210+
if (parser->imap + parser->parent_offset + 1 >= parser->imap_end)
211+
return NULL;
212+
213+
imap_parent = parser->imap + parser->parent_offset;
214+
215+
imap_next = of_irq_parse_imap_parent(imap_parent,
216+
parser->imap_end - imap_parent,
217+
&item->parent_args);
218+
if (!imap_next)
219+
return NULL;
220+
221+
for (i = 0; i < parser->parent_offset; i++)
222+
item->child_imap[i] = be32_to_cpu(*(parser->imap + i));
223+
224+
parser->imap = imap_next;
225+
226+
return item;
227+
}
228+
EXPORT_SYMBOL_GPL(of_imap_parser_one);
229+
160230
/**
161231
* of_irq_parse_raw - Low level interrupt tree parsing
162232
* @addr: address specifier (start of "reg" property of the device) in be32 format

include/linux/of_irq.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@
1111

1212
typedef int (*of_irq_init_cb_t)(struct device_node *, struct device_node *);
1313

14+
struct of_imap_parser {
15+
struct device_node *node;
16+
const __be32 *imap;
17+
const __be32 *imap_end;
18+
u32 parent_offset;
19+
};
20+
21+
struct of_imap_item {
22+
struct of_phandle_args parent_args;
23+
u32 child_imap_count;
24+
u32 child_imap[16]; /* Arbitrary size.
25+
* Should be #address-cells + #interrupt-cells but
26+
* avoid using allocation and so, expect that 16
27+
* should be enough
28+
*/
29+
};
30+
31+
/*
32+
* If the iterator is exited prematurely (break, goto, return) of_node_put() has
33+
* to be called on item.parent_args.np
34+
*/
35+
#define for_each_of_imap_item(parser, item) \
36+
for (; of_imap_parser_one(parser, item);)
37+
1438
/*
1539
* Workarounds only applied to 32bit powermac machines
1640
*/
@@ -49,6 +73,11 @@ extern int of_irq_get_byname(struct device_node *dev, const char *name);
4973
extern int of_irq_to_resource_table(struct device_node *dev,
5074
struct resource *res, int nr_irqs);
5175
extern struct device_node *of_irq_find_parent(struct device_node *child);
76+
extern int of_imap_parser_init(struct of_imap_parser *parser,
77+
struct device_node *node,
78+
struct of_imap_item *item);
79+
extern struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
80+
struct of_imap_item *item);
5281
extern struct irq_domain *of_msi_get_domain(struct device *dev,
5382
const struct device_node *np,
5483
enum irq_domain_bus_token token);
@@ -92,7 +121,17 @@ static inline void *of_irq_find_parent(struct device_node *child)
92121
{
93122
return NULL;
94123
}
95-
124+
static inline int of_imap_parser_init(struct of_imap_parser *parser,
125+
struct device_node *node,
126+
struct of_imap_item *item)
127+
{
128+
return -ENOSYS;
129+
}
130+
static inline struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
131+
struct of_imap_item *item)
132+
{
133+
return NULL;
134+
}
96135
static inline struct irq_domain *of_msi_get_domain(struct device *dev,
97136
struct device_node *np,
98137
enum irq_domain_bus_token token)

0 commit comments

Comments
 (0)