Skip to content

Commit 5556797

Browse files
author
Marc Zyngier
committed
genirq/irqdomain: Allow partial trimming of irq_data hierarchy
It appears that some HW is ugly enough that not all the interrupts connected to a particular interrupt controller end up with the same hierarchy depth (some of them are terminated early). This leaves the irqchip hacker with only two choices, both equally bad: - create discrete domain chains, one for each "hierarchy depth", which is very hard to maintain - create fake hierarchy levels for the shallow paths, leading to all kind of problems (what are the safe hwirq values for these fake levels?) Implement the ability to cut short a single interrupt hierarchy from a level marked as being disconnected by using the new irq_domain_disconnect_hierarchy() helper. The irqdomain allocation code will then perform the trimming Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent f4d51df commit 5556797

2 files changed

Lines changed: 98 additions & 4 deletions

File tree

include/linux/irqdomain.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ extern void irq_domain_free_irqs_parent(struct irq_domain *domain,
509509
unsigned int irq_base,
510510
unsigned int nr_irqs);
511511

512+
extern int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
513+
unsigned int virq);
514+
512515
static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
513516
{
514517
return domain->flags & IRQ_DOMAIN_FLAG_HIERARCHY;

kernel/irq/irqdomain.c

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,17 @@ static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
11361136
return irq_data;
11371137
}
11381138

1139+
static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1140+
{
1141+
struct irq_data *tmp;
1142+
1143+
while (irq_data) {
1144+
tmp = irq_data;
1145+
irq_data = irq_data->parent_data;
1146+
kfree(tmp);
1147+
}
1148+
}
1149+
11391150
static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
11401151
{
11411152
struct irq_data *irq_data, *tmp;
@@ -1147,12 +1158,83 @@ static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
11471158
irq_data->parent_data = NULL;
11481159
irq_data->domain = NULL;
11491160

1150-
while (tmp) {
1151-
irq_data = tmp;
1152-
tmp = tmp->parent_data;
1153-
kfree(irq_data);
1161+
__irq_domain_free_hierarchy(tmp);
1162+
}
1163+
}
1164+
1165+
/**
1166+
* irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1167+
* @domain: IRQ domain from which the hierarchy is to be disconnected
1168+
* @virq: IRQ number where the hierarchy is to be trimmed
1169+
*
1170+
* Marks the @virq level belonging to @domain as disconnected.
1171+
* Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1172+
* to @domain.
1173+
*
1174+
* Its only use is to be able to trim levels of hierarchy that do not
1175+
* have any real meaning for this interrupt, and that the driver marks
1176+
* as such from its .alloc() callback.
1177+
*/
1178+
int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1179+
unsigned int virq)
1180+
{
1181+
struct irq_data *irqd;
1182+
1183+
irqd = irq_domain_get_irq_data(domain, virq);
1184+
if (!irqd)
1185+
return -EINVAL;
1186+
1187+
irqd->chip = ERR_PTR(-ENOTCONN);
1188+
return 0;
1189+
}
1190+
1191+
static int irq_domain_trim_hierarchy(unsigned int virq)
1192+
{
1193+
struct irq_data *tail, *irqd, *irq_data;
1194+
1195+
irq_data = irq_get_irq_data(virq);
1196+
tail = NULL;
1197+
1198+
/* The first entry must have a valid irqchip */
1199+
if (!irq_data->chip || IS_ERR(irq_data->chip))
1200+
return -EINVAL;
1201+
1202+
/*
1203+
* Validate that the irq_data chain is sane in the presence of
1204+
* a hierarchy trimming marker.
1205+
*/
1206+
for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1207+
/* Can't have a valid irqchip after a trim marker */
1208+
if (irqd->chip && tail)
1209+
return -EINVAL;
1210+
1211+
/* Can't have an empty irqchip before a trim marker */
1212+
if (!irqd->chip && !tail)
1213+
return -EINVAL;
1214+
1215+
if (IS_ERR(irqd->chip)) {
1216+
/* Only -ENOTCONN is a valid trim marker */
1217+
if (PTR_ERR(irqd->chip) != -ENOTCONN)
1218+
return -EINVAL;
1219+
1220+
tail = irq_data;
11541221
}
11551222
}
1223+
1224+
/* No trim marker, nothing to do */
1225+
if (!tail)
1226+
return 0;
1227+
1228+
pr_info("IRQ%d: trimming hierarchy from %s\n",
1229+
virq, tail->parent_data->domain->name);
1230+
1231+
/* Sever the inner part of the hierarchy... */
1232+
irqd = tail;
1233+
tail = tail->parent_data;
1234+
irqd->parent_data = NULL;
1235+
__irq_domain_free_hierarchy(tail);
1236+
1237+
return 0;
11561238
}
11571239

11581240
static int irq_domain_alloc_irq_data(struct irq_domain *domain,
@@ -1362,6 +1444,15 @@ int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
13621444
mutex_unlock(&irq_domain_mutex);
13631445
goto out_free_irq_data;
13641446
}
1447+
1448+
for (i = 0; i < nr_irqs; i++) {
1449+
ret = irq_domain_trim_hierarchy(virq + i);
1450+
if (ret) {
1451+
mutex_unlock(&irq_domain_mutex);
1452+
goto out_free_irq_data;
1453+
}
1454+
}
1455+
13651456
for (i = 0; i < nr_irqs; i++)
13661457
irq_domain_insert_irq(virq + i);
13671458
mutex_unlock(&irq_domain_mutex);

0 commit comments

Comments
 (0)