Skip to contentSkip to conversation

dtc: checks: Validate interrupt-map properties

PATCH In review Andre Przywara 3 participants 8 messages Jun 2, 2020 – Jun 11, 2020 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

v3 cover dtc: checks: Validate interrupt-map properties

v3 just adds prerequisites for the existing interrupts_property test, to avoid double checks.
Changelog below.
===============

The interrupt-map properties can be quite tricky, as they are already hard to read in their source form, and depend on at least two nodes.

The first patch adds a separate test for interrupt providers, to avoid redundant warnings for missing properties later.
Patch 2/3 is the actual check routine, to verify some features of the map. In particular it should be able to spot missing fields or fields using the wrong number of cells. Most of the fields cannot easily be verified, but at the least the phandle to the interrupt controller and the total number of cells in the property should be valid.
It actually find problems in the Linux kernel tree's .dts files.

The third patch is more a proof of concept, not sure it should be merged as is, since it deviates from the current output scheme: It adds more output in case a map is considered broken, so the issue can be found much easier.
I would be happy with it living on the list for now.

Please let me know what you think.

Cheers,
Andre

Changelog v2 .. v3:
- add new prerequisites for interrupts_property test

Changelog v1 .. v2:
- change silent returns to asserts if no #interrupt-cells is found
- add prerequisites to interrupt_map test

Changelog RFC .. v1:
- add extra test for #interrupt-cells and #address-cells
- report expected and actual interrupt-map property length
- drop (now) redundant warnings
- explicitly check for overlay DT
- consider nested interrupt-map properties

Andre Przywara (3):
checks: Add interrupt provider test
checks: Validate interrupt-map properties
checks: interrupt-map: Dump entries on error

 checks.c                           | 194 +++++++++++++++++++++++++++++++++++--
 tests/bad-interrupt-controller.dts |   7 ++
 tests/bad-interrupt-map.dts        |  21 ++++
 tests/run_tests.sh                 |   2 +
 4 files changed, 218 insertions(+), 6 deletions(-)
 create mode 100644 tests/bad-interrupt-controller.dts
 create mode 100644 tests/bad-interrupt-map.dts
Signature · 1 line
2.14.1

A message that is not in the archive (sent off-list or not yet received): 20200602110419.22488-1-andre.przywara-5wv7dgnIgG8@public.gmane.org

#2 lore raw

PATCH v3 1/3 checks: Add interrupt provider test

An interrupt provider (an actual interrupt-controller node or an interrupt nexus) should have both #address-cells and #interrupt-cells properties explicitly defined.

Add an extra test for this. We check for the #interrupt-cells property already, but this does not cover every controller so far, only those that get referenced by an interrupts property in some node. Also we miss interrupt nexus nodes.

A missing #address-cells property is less critical, but creates ambiguities when used in interrupt-map properties, so warn about this as well now.
This removes the now redundant warning in the existing interrupts test.

---
 checks.c                           | 32 ++++++++++++++++++++++++++------
 tests/bad-interrupt-controller.dts |  7 +++++++
 tests/run_tests.sh                 |  1 +
 3 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 tests/bad-interrupt-controller.dts
checks.c +26 -6
index 4b3c486..23faca5 100644
@@ -1547,6 +1547,28 @@ static bool node_is_interrupt_provider(struct node *node)
 
 	return false;
 }
+
+static void check_interrupt_provider(struct check *c,
+				     struct dt_info *dti,
+				     struct node *node)
+{
+	struct property *prop;
+
+	if (!node_is_interrupt_provider(node))
+		return;
+
+	prop = get_property(node, "#interrupt-cells");
+	if (!prop)
+		FAIL(c, dti, node,
+		     "Missing #interrupt-cells in interrupt provider");
+
+	prop = get_property(node, "#address-cells");
+	if (!prop)
+		FAIL(c, dti, node,
+		     "Missing #address-cells in interrupt provider");
+}
+WARNING(interrupt_provider, check_interrupt_provider, NULL);
+
 static void check_interrupts_property(struct check *c,
 				      struct dt_info *dti,
 				      struct node *node)
@@ -1602,12 +1624,8 @@ static void check_interrupts_property(struct check *c,
 		return;
 	}
 
+	/* Validity already verified by another test. */
 	prop = get_property(irq_node, "#interrupt-cells");
-	if (!prop) {
-		FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
-		return;
-	}
-
 	irq_cells = propval_cell(prop);
 	if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
 		FAIL_PROP(c, dti, node, prop,
@@ -1615,7 +1633,8 @@ static void check_interrupts_property(struct check *c,
 			  irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
 	}
 }
-WARNING(interrupts_property, check_interrupts_property, &phandle_references);
+WARNING(interrupts_property, check_interrupts_property, &phandle_references,
+	&interrupt_cells_is_cell, &interrupt_provider);
 
 static const struct bus_type graph_port_bus = {
 	.name = "graph-port",
@@ -1828,6 +1847,7 @@ static struct check *check_table[] = {
 	&deprecated_gpio_property,
 	&gpios_property,
 	&interrupts_property,
+	&interrupt_provider,
 
 	&alias_paths,
 
tests/bad-interrupt-controller.dts new file +7 -0
new file mode 100644
index 0000000..62fa118
@@ -0,0 +1,7 @@
+/dts-v1/;
+
+/ {
+	intc: interrupt-controller {
+		interrupt-controller;
+	};
+};
tests/run_tests.sh +1 -0
index eccb85d..294585b 100755
@@ -714,6 +714,7 @@ dtc_tests () {
     check_tests "$SRCDIR/bad-graph.dts" graph_endpoint
     run_sh_test "$SRCDIR/dtc-checkfails.sh" deprecated_gpio_property -- -Wdeprecated_gpio_property -I dts -O dtb "$SRCDIR/bad-gpio.dts"
     check_tests "$SRCDIR/bad-interrupt-cells.dts" interrupts_property
+    check_tests "$SRCDIR/bad-interrupt-controller.dts" interrupt_provider
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_chars -- -I dtb -O dtb bad_node_char.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_format -- -I dtb -O dtb bad_node_format.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" property_name_chars -- -I dtb -O dtb bad_prop_char.dtb
Signature · 1 line
2.14.1

A message that is not in the archive (sent off-list or not yet received): 20200602110419.22488-2-andre.przywara-5wv7dgnIgG8@public.gmane.org

#4 lore raw

Re: [PATCH v3 1/3] checks: Add interrupt provider test

Quoted 40 lines · Andre Przywara

An interrupt provider (an actual interrupt-controller node or an interrupt nexus) should have both #address-cells and #interrupt-cells properties explicitly defined.

Add an extra test for this. We check for the #interrupt-cells property already, but this does not cover every controller so far, only those that get referenced by an interrupts property in some node. Also we miss interrupt nexus nodes.

A missing #address-cells property is less critical, but creates ambiguities when used in interrupt-map properties, so warn about this as well now.
This removes the now redundant warning in the existing interrupts test.

---
 checks.c                           | 32 ++++++++++++++++++++++++++------
 tests/bad-interrupt-controller.dts |  7 +++++++
 tests/run_tests.sh                 |  1 +
 3 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 tests/bad-interrupt-controller.dts
checks.c +9 -0
index 4b3c486..23faca5 100644
@@ -1547,6 +1547,28 @@ static bool node_is_interrupt_provider(struct node *node)
 
        return false;
 }
+
+static void check_interrupt_provider(struct check *c,
+                                    struct dt_info *dti,
+                                    struct node *node)
+{
+       struct property *prop;
+
+       if (!node_is_interrupt_provider(node))
+               return;

checks.c static bool node_is_interrupt_provider(struct node *node) · line 1568

Quoted 10 lines
checks.c +10 -0
+
+       prop = get_property(node, "#interrupt-cells");
+       if (!prop)
+               FAIL(c, dti, node,
+                    "Missing #interrupt-cells in interrupt provider");
+
+       prop = get_property(node, "#address-cells");
+       if (!prop)
+               FAIL(c, dti, node,
+                    "Missing #address-cells in interrupt provider");

This is still not a separate test. We should be able to continue checking for '#interrupt-cells' while turning the '#address-cells' check on or off. IOW, we can't test for what the Linux kernel cares about. Though if there's only a handful of warnings to fix, then it's fine for me as-is.

Rob

#5 lore raw

Re: [PATCH v3 1/3] checks: Add interrupt provider test

In reply to a message not in the archive
Quoted 34 lines · Andre Przywara

An interrupt provider (an actual interrupt-controller node or an interrupt nexus) should have both #address-cells and #interrupt-cells properties explicitly defined.

Add an extra test for this. We check for the #interrupt-cells property already, but this does not cover every controller so far, only those that get referenced by an interrupts property in some node. Also we miss interrupt nexus nodes.

A missing #address-cells property is less critical, but creates ambiguities when used in interrupt-map properties, so warn about this as well now.
This removes the now redundant warning in the existing interrupts test.

---
 checks.c                           | 32 ++++++++++++++++++++++++++------
 tests/bad-interrupt-controller.dts |  7 +++++++
 tests/run_tests.sh                 |  1 +
 3 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 tests/bad-interrupt-controller.dts
checks.c +4 -0
index 4b3c486..23faca5 100644
@@ -1547,6 +1547,28 @@ static bool node_is_interrupt_provider(struct node *node)
 
 	return false;
 }
+
+static void check_interrupt_provider(struct check *c,
+				     struct dt_info *dti,
+				     struct node *node)

checks.c static bool node_is_interrupt_provider(struct node *node) · line 1563

Quoted 10 lines
checks.c +10 -0
+{
+	struct property *prop;
+
+	if (!node_is_interrupt_provider(node))
+		return;
+
+	prop = get_property(node, "#interrupt-cells");
+	if (!prop)
+		FAIL(c, dti, node,
+		     "Missing #interrupt-cells in interrupt provider");

I like splitting the check for #interrupt-cells into a separate check from the 'interrupts' property check.

checks.c static bool node_is_interrupt_provider(struct node *node) · line 1568

Quoted 4 lines
checks.c +4 -0
+	prop = get_property(node, "#address-cells");
+	if (!prop)
+		FAIL(c, dti, node,
+		     "Missing #address-cells in interrupt provider");

However, I'm a bit less sold on checking #address-cells here. Although #address-cells is necessary for interrupt-maps, there are a lot of cases with just a single, simple interrupt controller that's not a bus root so it's not obvious why it would need to have an #address-cells property at all.

So, I think you do want to check #address-cells only where you actually do need it (the interrupt provider is the target of an interrupt-map).

Quoted 63 lines
checks.c +7 -6
+}
+WARNING(interrupt_provider, check_interrupt_provider, NULL);
+
 static void check_interrupts_property(struct check *c,
 				      struct dt_info *dti,
 				      struct node *node)
@@ -1602,12 +1624,8 @@ static void check_interrupts_property(struct check *c,
 		return;
 	}
 
+	/* Validity already verified by another test. */
 	prop = get_property(irq_node, "#interrupt-cells");
-	if (!prop) {
-		FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
-		return;
-	}
-
 	irq_cells = propval_cell(prop);
 	if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
 		FAIL_PROP(c, dti, node, prop,
@@ -1615,7 +1633,8 @@ static void check_interrupts_property(struct check *c,
 			  irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
 	}
 }
-WARNING(interrupts_property, check_interrupts_property, &phandle_references);
+WARNING(interrupts_property, check_interrupts_property, &phandle_references,
+	&interrupt_cells_is_cell, &interrupt_provider);
 
 static const struct bus_type graph_port_bus = {
 	.name = "graph-port",
@@ -1828,6 +1847,7 @@ static struct check *check_table[] = {
 	&deprecated_gpio_property,
 	&gpios_property,
 	&interrupts_property,
+	&interrupt_provider,
 
 	&alias_paths,
 
tests/bad-interrupt-controller.dts new file +7 -0
new file mode 100644
index 0000000..62fa118
@@ -0,0 +1,7 @@
+/dts-v1/;
+
+/ {
+	intc: interrupt-controller {
+		interrupt-controller;
+	};
+};
tests/run_tests.sh +1 -0
index eccb85d..294585b 100755
@@ -714,6 +714,7 @@ dtc_tests () {
     check_tests "$SRCDIR/bad-graph.dts" graph_endpoint
     run_sh_test "$SRCDIR/dtc-checkfails.sh" deprecated_gpio_property -- -Wdeprecated_gpio_property -I dts -O dtb "$SRCDIR/bad-gpio.dts"
     check_tests "$SRCDIR/bad-interrupt-cells.dts" interrupts_property
+    check_tests "$SRCDIR/bad-interrupt-controller.dts" interrupt_provider
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_chars -- -I dtb -O dtb bad_node_char.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_format -- -I dtb -O dtb bad_node_format.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" property_name_chars -- -I dtb -O dtb bad_prop_char.dtb
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH v3 1/3] checks: Add interrupt provider test

Quoted 62 lines · David Gibson
Quoted 34 lines · Andre Przywara

An interrupt provider (an actual interrupt-controller node or an interrupt nexus) should have both #address-cells and #interrupt-cells properties explicitly defined.

Add an extra test for this. We check for the #interrupt-cells property already, but this does not cover every controller so far, only those that get referenced by an interrupts property in some node. Also we miss interrupt nexus nodes.

A missing #address-cells property is less critical, but creates ambiguities when used in interrupt-map properties, so warn about this as well now.
This removes the now redundant warning in the existing interrupts test.

---
 checks.c                           | 32 ++++++++++++++++++++++++++------
 tests/bad-interrupt-controller.dts |  7 +++++++
 tests/run_tests.sh                 |  1 +
 3 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 tests/bad-interrupt-controller.dts
checks.c +4 -0
index 4b3c486..23faca5 100644
@@ -1547,6 +1547,28 @@ static bool node_is_interrupt_provider(struct node *node)
 
      return false;
 }
+
+static void check_interrupt_provider(struct check *c,
+                                  struct dt_info *dti,
+                                  struct node *node)

checks.c static bool node_is_interrupt_provider(struct node *node) · line 1563

Quoted 10 lines
checks.c +10 -0
+{
+     struct property *prop;
+
+     if (!node_is_interrupt_provider(node))
+             return;
+
+     prop = get_property(node, "#interrupt-cells");
+     if (!prop)
+             FAIL(c, dti, node,
+                  "Missing #interrupt-cells in interrupt provider");

I like splitting the check for #interrupt-cells into a separate check from the 'interrupts' property check.

checks.c static bool node_is_interrupt_provider(struct node *node) · line 1568

Quoted 4 lines
checks.c +4 -0
+     prop = get_property(node, "#address-cells");
+     if (!prop)
+             FAIL(c, dti, node,
+                  "Missing #address-cells in interrupt provider");

However, I'm a bit less sold on checking #address-cells here. Although #address-cells is necessary for interrupt-maps, there are a lot of cases with just a single, simple interrupt controller that's not a bus root so it's not obvious why it would need to have an #address-cells property at all.

Quoted 3 lines

So, I think you do want to check #address-cells only where you actually do need it (the interrupt provider is the target of an interrupt-map).

That seems okay for me. I'd expect that's a whole lot less warnings generated. If not, then maybe should still be separate.

Rob

#8 lore raw

PATCH v3 2/3 checks: Validate interrupt-map properties

In reply to a message not in the archive

The interrupt-map in an interrupt nexus is quite a tricky property: Each entry contains five fields, the size of four of those depending on some *-cells entries from two different nodes. This is even hard to validate in a .dts file, especially when the associated interrupt controller is described in a separate (included) file.

Add checks to validate those entries, by:
- Checking some basic properties of the interrupt nexus node.
- Checking that a map entry contains at least enough cells to point to the associated interrupt controller.
- Checking that the phandle points to an actual interrupt controller.
- Checking that there are enough entries to describe an interrupt in that interrupt controller's domain.

If each iteration passes and we exhaust exactly all the cells in the interrupt-map property, the check passes.
Report errors on the way, and abort the check if that happens.

---
 checks.c                    | 84 +++++++++++++++++++++++++++++++++++++++++++++
 tests/bad-interrupt-map.dts | 21 ++++++++++++
 tests/run_tests.sh          |  1 +
 3 files changed, 106 insertions(+)
 create mode 100644 tests/bad-interrupt-map.dts
checks.c +84 -0
index 23faca5..7b54981 100644
@@ -1636,6 +1636,88 @@ static void check_interrupts_property(struct check *c,
 WARNING(interrupts_property, check_interrupts_property, &phandle_references,
 	&interrupt_cells_is_cell, &interrupt_provider);
 
+static void check_interrupt_map(struct check *c, struct dt_info *dti,
+				struct node *node)
+{
+	struct property *map = get_property(node, "interrupt-map");
+	struct property *prop;
+	int i, cells, irq_cells;
+
+	/* We are only interested in interrupt nexus nodes. */
+	if (!map)
+		return;
+
+	if (map->val.len % sizeof(cell_t)) {
+		FAIL_PROP(c, dti, node, map,
+			  "size (%d) is invalid, expected multiple of %zu",
+			  map->val.len, sizeof(cell_t));
+		return;
+	}
+	cells = map->val.len / sizeof(cell_t);
+
+	prop = get_property(node, "#interrupt-cells");
+	assert(prop);		/* checked by interrupt_cells_is_cell */
+	irq_cells = propval_cell(prop);
+
+	for (i = 0; i < cells;) {
+		int phandle_idx = i + node_addr_cells(node) + irq_cells;
+		cell_t intc_phandle, intc_irq_cells, intc_addr_cells;
+		struct node *intc = NULL;
+
+		if (phandle_idx + 1 >= cells) {
+			FAIL_PROP(c, dti, node, map,
+				"insufficient cells for interrupt-map entry");
+			return;
+		}
+		intc_phandle = propval_cell_n(map, phandle_idx);
+		/* Give up if this is an overlay with external references */
+		if ((intc_phandle == 0 || intc_phandle == -1) &&
+		    dti->dtsflags & DTSF_PLUGIN)
+			break;
+		/* Avoid the assert in get_node_by_phandle() */
+		if (intc_phandle != 0)
+			intc = get_node_by_phandle(dti->dt, intc_phandle);
+		if (!intc) {
+			FAIL_PROP(c, dti, node, map,
+				  "invalid phandle for interrupt-map entry");
+			return;
+		}
+
+		if (!node_is_interrupt_provider(intc)) {
+			FAIL_PROP(c,dti, node, map,
+				  "interrupt-map phandle does not point to interrupt provider");
+			return;
+		}
+
+		prop = get_property(intc, "#address-cells");
+		if (!prop) {
+			/*
+			 * Linux treats non-existing #address-cells in the
+			 * interrupt parent as 0, and not 2, as the spec
+			 * suggests. Deal with that here.
+			 * We have a separate check for an explicit #a-c
+			 * in an interrupt provider that warns already.
+			 */
+			intc_addr_cells = 0;
+		} else
+			intc_addr_cells = propval_cell(prop);
+
+		prop = get_property(intc, "#interrupt-cells");
+		assert(prop);		/* checked by interrupt_cells_is_cell */
+		intc_irq_cells = propval_cell(prop);
+
+		if (phandle_idx + intc_addr_cells + intc_irq_cells >= cells) {
+			FAIL_PROP(c, dti, node, map,
+				"insufficient cells for interrupt-map entry");
+			return;
+		}
+		i = phandle_idx + 1 + intc_addr_cells + intc_irq_cells;
+	}
+}
+WARNING(interrupt_map, check_interrupt_map, NULL,
+	&address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
+	&interrupt_provider);
+
 static const struct bus_type graph_port_bus = {
 	.name = "graph-port",
 };
@@ -1811,6 +1893,8 @@ static struct check *check_table[] = {
 	&pci_device_reg,
 	&pci_device_bus_num,
 
+	&interrupt_map,
+
 	&simple_bus_bridge,
 	&simple_bus_reg,
 
tests/bad-interrupt-map.dts new file +21 -0
new file mode 100644
index 0000000..cf9618f
@@ -0,0 +1,21 @@
+/dts-v1/;
+
+/ {
+	intc: interrupt-controller {
+		interrupt-controller;
+		#address-cells = <2>;
+		#interrupt-cells = <3>;
+	};
+
+	nexus-node {
+		#address-cells = <1>;
+		#interrupt-cells = <1>;
+/*
+ * The cells after the phandle are the address in the interrupt controller's
+ * domain. This here encodes 0 cells , but the actual number is 2 above.
+ */
+		interrupt-map = <0 0 &intc 1 42 4>,
+				<0 1 &intc 1 43 4>,
+				<0 2 &intc 1 44 4>;
+	};
+};
tests/run_tests.sh +1 -0
index 294585b..7c149d9 100755
@@ -715,6 +715,7 @@ dtc_tests () {
     run_sh_test "$SRCDIR/dtc-checkfails.sh" deprecated_gpio_property -- -Wdeprecated_gpio_property -I dts -O dtb "$SRCDIR/bad-gpio.dts"
     check_tests "$SRCDIR/bad-interrupt-cells.dts" interrupts_property
     check_tests "$SRCDIR/bad-interrupt-controller.dts" interrupt_provider
+    check_tests "$SRCDIR/bad-interrupt-map.dts" interrupt_map
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_chars -- -I dtb -O dtb bad_node_char.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_format -- -I dtb -O dtb bad_node_format.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" property_name_chars -- -I dtb -O dtb bad_prop_char.dtb
Signature · 1 line
2.14.1

A message that is not in the archive (sent off-list or not yet received): 20200602110419.22488-3-andre.przywara-5wv7dgnIgG8@public.gmane.org

#10 lore raw

Re: [PATCH v3 2/3] checks: Validate interrupt-map properties

Quoted 75 lines · Andre Przywara

The interrupt-map in an interrupt nexus is quite a tricky property: Each entry contains five fields, the size of four of those depending on some *-cells entries from two different nodes. This is even hard to validate in a .dts file, especially when the associated interrupt controller is described in a separate (included) file.

Add checks to validate those entries, by:
- Checking some basic properties of the interrupt nexus node.
- Checking that a map entry contains at least enough cells to point to the associated interrupt controller.
- Checking that the phandle points to an actual interrupt controller.
- Checking that there are enough entries to describe an interrupt in that interrupt controller's domain.

If each iteration passes and we exhaust exactly all the cells in the interrupt-map property, the check passes.
Report errors on the way, and abort the check if that happens.

---
 checks.c                    | 84 +++++++++++++++++++++++++++++++++++++++++++++
 tests/bad-interrupt-map.dts | 21 ++++++++++++
 tests/run_tests.sh          |  1 +
 3 files changed, 106 insertions(+)
 create mode 100644 tests/bad-interrupt-map.dts
checks.c +40 -0
index 23faca5..7b54981 100644
@@ -1636,6 +1636,88 @@ static void check_interrupts_property(struct check *c,
 WARNING(interrupts_property, check_interrupts_property, &phandle_references,
        &interrupt_cells_is_cell, &interrupt_provider);
 
+static void check_interrupt_map(struct check *c, struct dt_info *dti,
+                               struct node *node)
+{
+       struct property *map = get_property(node, "interrupt-map");
+       struct property *prop;
+       int i, cells, irq_cells;
+
+       /* We are only interested in interrupt nexus nodes. */
+       if (!map)
+               return;
+
+       if (map->val.len % sizeof(cell_t)) {
+               FAIL_PROP(c, dti, node, map,
+                         "size (%d) is invalid, expected multiple of %zu",
+                         map->val.len, sizeof(cell_t));
+               return;
+       }
+       cells = map->val.len / sizeof(cell_t);
+
+       prop = get_property(node, "#interrupt-cells");
+       assert(prop);           /* checked by interrupt_cells_is_cell */
+       irq_cells = propval_cell(prop);
+
+       for (i = 0; i < cells;) {
+               int phandle_idx = i + node_addr_cells(node) + irq_cells;
+               cell_t intc_phandle, intc_irq_cells, intc_addr_cells;
+               struct node *intc = NULL;
+
+               if (phandle_idx + 1 >= cells) {
+                       FAIL_PROP(c, dti, node, map,
+                               "insufficient cells for interrupt-map entry");
+                       return;
+               }
+               intc_phandle = propval_cell_n(map, phandle_idx);
+               /* Give up if this is an overlay with external references */
+               if ((intc_phandle == 0 || intc_phandle == -1) &&
+                   dti->dtsflags & DTSF_PLUGIN)
+                       break;
+               /* Avoid the assert in get_node_by_phandle() */
+               if (intc_phandle != 0)

checks.c static void check_interrupts_property(struct check *c, · line 1688

Quoted 10 lines
checks.c +10 -0
+                       intc = get_node_by_phandle(dti->dt, intc_phandle);
+               if (!intc) {
+                       FAIL_PROP(c, dti, node, map,
+                                 "invalid phandle for interrupt-map entry");
+                       return;
+               }
+
+               if (!node_is_interrupt_provider(intc)) {
+                       FAIL_PROP(c,dti, node, map,
+                                 "interrupt-map phandle does not point to interrupt provider");

Perhaps print the node path of the phandle.

Quoted 13 lines
checks.c +13 -0
+                       return;
+               }
+
+               prop = get_property(intc, "#address-cells");
+               if (!prop) {
+                       /*
+                        * Linux treats non-existing #address-cells in the
+                        * interrupt parent as 0, and not 2, as the spec
+                        * suggests. Deal with that here.
+                        * We have a separate check for an explicit #a-c
+                        * in an interrupt provider that warns already.
+                        */
+                       intc_addr_cells = 0;

checks.c static void check_interrupts_property(struct check *c, · line 1711

Quoted 10 lines
checks.c +10 -0
+               } else
+                       intc_addr_cells = propval_cell(prop);
+
+               prop = get_property(intc, "#interrupt-cells");
+               assert(prop);           /* checked by interrupt_cells_is_cell */
+               intc_irq_cells = propval_cell(prop);
+
+               if (phandle_idx + intc_addr_cells + intc_irq_cells >= cells) {
+                       FAIL_PROP(c, dti, node, map,
+                               "insufficient cells for interrupt-map entry");

Same message text twice which makes looking up what the test is checking harder. Perhaps 'insufficient interrupt parent cells'. Printing the expected and actual lengths can be useful, too.

Quoted 63 lines
checks.c +11 -0
+                       return;
+               }
+               i = phandle_idx + 1 + intc_addr_cells + intc_irq_cells;
+       }
+}
+WARNING(interrupt_map, check_interrupt_map, NULL,
+       &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
+       &interrupt_provider);
+
 static const struct bus_type graph_port_bus = {
        .name = "graph-port",
 };
@@ -1811,6 +1893,8 @@ static struct check *check_table[] = {
        &pci_device_reg,
        &pci_device_bus_num,
 
+       &interrupt_map,
+
        &simple_bus_bridge,
        &simple_bus_reg,
 
tests/bad-interrupt-map.dts new file +21 -0
new file mode 100644
index 0000000..cf9618f
@@ -0,0 +1,21 @@
+/dts-v1/;
+
+/ {
+       intc: interrupt-controller {
+               interrupt-controller;
+               #address-cells = <2>;
+               #interrupt-cells = <3>;
+       };
+
+       nexus-node {
+               #address-cells = <1>;
+               #interrupt-cells = <1>;
+/*
+ * The cells after the phandle are the address in the interrupt controller's
+ * domain. This here encodes 0 cells , but the actual number is 2 above.
+ */
+               interrupt-map = <0 0 &intc 1 42 4>,
+                               <0 1 &intc 1 43 4>,
+                               <0 2 &intc 1 44 4>;
+       };
+};
tests/run_tests.sh +1 -1
index 294585b..7c149d9 100755
@@ -715,6 +715,7 @@ dtc_tests () {
     run_sh_test "$SRCDIR/dtc-checkfails.sh" deprecated_gpio_property -- -Wdeprecated_gpio_property -I dts -O dtb "$SRCDIR/bad-gpio.dts"
     check_tests "$SRCDIR/bad-interrupt-cells.dts" interrupts_property
     check_tests "$SRCDIR/bad-interrupt-controller.dts" interrupt_provider
+    check_tests "$SRCDIR/bad-interrupt-map.dts" interrupt_map
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_chars -- -I dtb -O dtb bad_node_char.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_format -- -I dtb -O dtb bad_node_format.dtb
     run_sh_test "$SRCDIR/dtc-checkfails.sh" property_name_chars -- -I dtb -O dtb bad_prop_char.dtb
--

2.14.1

#11 lore raw

RFC PATCH v3 3/3 checks: interrupt-map: Dump entries on error

In reply to a message not in the archive

While the interrupt-map property is now validated, the current error messages will probably leave people scratching their head on what the actual problem is.

To help with the diagnosis, dump the map property formatted in a way that corresponds to the actual interpretation from an IRQ consumer's point of view. While this can't pinpoint the actual problem, it's much easier to see what went wrong, especially when comparing it to the .dts source.

As this will generate one line per mapped interrupt, it can be
potentially long on those larger maps.
A report would look like this:
Warning (interrupt_map): /soc/pci@1c00000:interrupt-map: invalid phandle for interrupt-map entry

        < <0x0 0x0 0x0>, <0x1>, <&gic@17a00000>, <0x0>, <0x87 0x4 0x0> >,
        < <0x0 0x0 0x2>, <0x1>, <&invalid>, >
---
 checks.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
checks.c +78 -0
index 7b54981..f712e23 100644
@@ -1636,6 +1636,80 @@ static void check_interrupts_property(struct check *c,
 WARNING(interrupts_property, check_interrupts_property, &phandle_references,
 	&interrupt_cells_is_cell, &interrupt_provider);
 
+static void dump_cell_group(struct property *prop, int index, int cells,
+			    int nr_cells)
+{
+	int limit, i;
+
+	if (index + cells > nr_cells)
+		limit = nr_cells - index;
+	else
+		limit = cells;
+
+	fputs("<", stderr);
+	for (i = 0; i < limit; i++) {
+		cell_t v = propval_cell_n(prop, index + i);
+		fprintf(stderr, "%s0x%x", i == 0 ? "" : " ", v);
+	}
+	if (limit < cells)
+		fputs(", MISSING", stderr);
+	fputs(">", stderr);
+}
+
+static void dump_interrupt_map(struct node *root, struct node *nexus,
+			       struct property *map, int irq_cells)
+{
+	int cells = map->val.len / 4;
+	struct property *prop;
+	int value;
+	int i;
+
+	for (i = 0; i < cells;) {
+		struct node *intc = NULL;
+
+		fprintf(stderr, "%s\t< ", i == 0 ? "" : ",\n");
+		dump_cell_group(map, i, node_addr_cells(nexus), cells);
+		fputs(", ", stderr);
+		dump_cell_group(map, i + node_addr_cells(nexus), irq_cells, cells);
+		fputs(", <&", stderr);
+		i += node_addr_cells(nexus) + irq_cells;
+		if (i >= cells)
+			break;
+		value = propval_cell_n(map, i);
+		if (value != 0)
+			intc = get_node_by_phandle(root, value);
+		fprintf(stderr, "%s>, ",
+			intc && intc->name ? intc->name : "invalid");
+		if (!intc) {
+			fprintf(stderr, ">\n");
+			return;
+		}
+		/*
+		 * Linux treats non-existing #address-cells in the interrupt
+		 * parent as 0 (and not 2, as the spec says).
+		 * Deal with that, since many DTs rely on that.
+		 */
+		prop = get_property(intc, "#address-cells");
+		if (prop)
+			value = propval_cell(prop);
+		else
+			value = 0;
+		dump_cell_group(map, i + 1, value, cells);
+		fputs(", ", stderr);
+		i += 1 + value;
+
+		prop = get_property(intc, "#interrupt-cells");
+		if (prop) {
+			value = propval_cell(prop);
+			dump_cell_group(map, i, value, cells);
+		} else
+			fputs("<?>", stderr);
+		fputs(" >", stderr);
+		i += value;
+	}
+	fputs("\n", stderr);
+}
+
 static void check_interrupt_map(struct check *c, struct dt_info *dti,
 				struct node *node)
 {
@@ -1667,6 +1741,7 @@ static void check_interrupt_map(struct check *c, struct dt_info *dti,
 		if (phandle_idx + 1 >= cells) {
 			FAIL_PROP(c, dti, node, map,
 				"insufficient cells for interrupt-map entry");
+			dump_interrupt_map(dti->dt, node, map, irq_cells);
 			return;
 		}
 		intc_phandle = propval_cell_n(map, phandle_idx);
@@ -1680,12 +1755,14 @@ static void check_interrupt_map(struct check *c, struct dt_info *dti,
 		if (!intc) {
 			FAIL_PROP(c, dti, node, map,
 				  "invalid phandle for interrupt-map entry");
+			dump_interrupt_map(dti->dt, node, map, irq_cells);
 			return;
 		}
 
 		if (!node_is_interrupt_provider(intc)) {
 			FAIL_PROP(c,dti, node, map,
 				  "interrupt-map phandle does not point to interrupt provider");
+			dump_interrupt_map(dti->dt, node, map, irq_cells);
 			return;
 		}
 
@@ -1709,6 +1786,7 @@ static void check_interrupt_map(struct check *c, struct dt_info *dti,
 		if (phandle_idx + intc_addr_cells + intc_irq_cells >= cells) {
 			FAIL_PROP(c, dti, node, map,
 				"insufficient cells for interrupt-map entry");
+			dump_interrupt_map(dti->dt, node, map, irq_cells);
 			return;
 		}
 		i = phandle_idx + 1 + intc_addr_cells + intc_irq_cells;
Signature · 1 line
2.14.1