Skip to content

Commit 04db93a

Browse files
committed
of/unittest: Add dma-ranges address translation tests
The functions for parsing 'dma-ranges' ranges are buggy and fail to handle several conditions. Add new tests for of_dma_get_range() and for_each_of_pci_range(). With this test, we get 5 new failures which are fixed in subsequent commits: OF: translation of DMA address(0) to CPU address failed node(/testcase-data/address-tests/device@70000000) FAIL of_unittest_dma_ranges_one():798 of_dma_get_range failed on node /testcase-data/address-tests/device@70000000 rc=-22 OF: translation of DMA address(10000000) to CPU address failed node(/testcase-data/address-tests/bus@80000000/device@1000) FAIL of_unittest_dma_ranges_one():798 of_dma_get_range failed on node /testcase-data/address-tests/bus@80000000/device@1000 rc=-22 OF: translation of DMA address(0) to CPU address failed node(/testcase-data/address-tests/pci@90000000) FAIL of_unittest_dma_ranges_one():798 of_dma_get_range failed on node /testcase-data/address-tests/pci@90000000 rc=-22 FAIL of_unittest_pci_dma_ranges():851 for_each_of_pci_range wrong CPU addr (d0000000) on node /testcase-data/address-tests/pci@90000000 FAIL of_unittest_pci_dma_ranges():861 for_each_of_pci_range wrong CPU addr (ffffffffffffffff) on node /testcase-data/address-tests/pci@90000000 Cc: Robin Murphy <robin.murphy@arm.com> Tested-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de> Reviewed-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de> Signed-off-by: Rob Herring <robh@kernel.org>
1 parent b68ac8d commit 04db93a

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

‎drivers/of/unittest-data/testcases.dts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
#include "tests-phandle.dtsi"
1616
#include "tests-interrupts.dtsi"
1717
#include "tests-match.dtsi"
18+
#include "tests-address.dtsi"
1819
#include "tests-platform.dtsi"
1920
#include "tests-overlay.dtsi"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/ {
4+
#address-cells = <1>;
5+
#size-cells = <1>;
6+
7+
testcase-data {
8+
#address-cells = <1>;
9+
#size-cells = <1>;
10+
ranges;
11+
12+
address-tests {
13+
#address-cells = <1>;
14+
#size-cells = <1>;
15+
/* ranges here is to make sure we don't use it for
16+
* dma-ranges translation */
17+
ranges = <0x70000000 0x70000000 0x40000000>,
18+
<0x00000000 0xd0000000 0x20000000>;
19+
dma-ranges = <0x0 0x20000000 0x40000000>;
20+
21+
device@70000000 {
22+
reg = <0x70000000 0x1000>;
23+
};
24+
25+
bus@80000000 {
26+
#address-cells = <1>;
27+
#size-cells = <1>;
28+
ranges = <0x0 0x80000000 0x100000>;
29+
dma-ranges = <0x10000000 0x0 0x40000000>;
30+
31+
device@1000 {
32+
reg = <0x1000 0x1000>;
33+
};
34+
};
35+
36+
pci@90000000 {
37+
device_type = "pci";
38+
#address-cells = <3>;
39+
#size-cells = <2>;
40+
reg = <0x90000000 0x1000>;
41+
ranges = <0x42000000 0x0 0x40000000 0x40000000 0x0 0x10000000>;
42+
dma-ranges = <0x42000000 0x0 0x80000000 0x00000000 0x0 0x10000000>,
43+
<0x42000000 0x0 0xc0000000 0x20000000 0x0 0x10000000>;
44+
};
45+
46+
};
47+
};
48+
};

‎drivers/of/unittest.c‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/hashtable.h>
1313
#include <linux/libfdt.h>
1414
#include <linux/of.h>
15+
#include <linux/of_address.h>
1516
#include <linux/of_fdt.h>
1617
#include <linux/of_irq.h>
1718
#include <linux/of_platform.h>
@@ -779,6 +780,95 @@ static void __init of_unittest_changeset(void)
779780
#endif
780781
}
781782

783+
static void __init of_unittest_dma_ranges_one(const char *path,
784+
u64 expect_dma_addr, u64 expect_paddr, u64 expect_size)
785+
{
786+
struct device_node *np;
787+
u64 dma_addr, paddr, size;
788+
int rc;
789+
790+
np = of_find_node_by_path(path);
791+
if (!np) {
792+
pr_err("missing testcase data\n");
793+
return;
794+
}
795+
796+
rc = of_dma_get_range(np, &dma_addr, &paddr, &size);
797+
798+
unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
799+
if (!rc) {
800+
unittest(size == expect_size,
801+
"of_dma_get_range wrong size on node %pOF size=%llx\n", np, size);
802+
unittest(paddr == expect_paddr,
803+
"of_dma_get_range wrong phys addr (%llx) on node %pOF", paddr, np);
804+
unittest(dma_addr == expect_dma_addr,
805+
"of_dma_get_range wrong DMA addr (%llx) on node %pOF", dma_addr, np);
806+
}
807+
of_node_put(np);
808+
}
809+
810+
static void __init of_unittest_parse_dma_ranges(void)
811+
{
812+
of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
813+
0x0, 0x20000000, 0x40000000);
814+
of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
815+
0x10000000, 0x20000000, 0x40000000);
816+
of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
817+
0x80000000, 0x20000000, 0x10000000);
818+
}
819+
820+
static void __init of_unittest_pci_dma_ranges(void)
821+
{
822+
struct device_node *np;
823+
struct of_pci_range range;
824+
struct of_pci_range_parser parser;
825+
int i = 0;
826+
827+
if (!IS_ENABLED(CONFIG_PCI))
828+
return;
829+
830+
np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
831+
if (!np) {
832+
pr_err("missing testcase data\n");
833+
return;
834+
}
835+
836+
if (of_pci_dma_range_parser_init(&parser, np)) {
837+
pr_err("missing dma-ranges property\n");
838+
return;
839+
}
840+
841+
/*
842+
* Get the dma-ranges from the device tree
843+
*/
844+
for_each_of_pci_range(&parser, &range) {
845+
if (!i) {
846+
unittest(range.size == 0x10000000,
847+
"for_each_of_pci_range wrong size on node %pOF size=%llx\n",
848+
np, range.size);
849+
unittest(range.cpu_addr == 0x20000000,
850+
"for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
851+
range.cpu_addr, np);
852+
unittest(range.pci_addr == 0x80000000,
853+
"for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
854+
range.pci_addr, np);
855+
} else {
856+
unittest(range.size == 0x10000000,
857+
"for_each_of_pci_range wrong size on node %pOF size=%llx\n",
858+
np, range.size);
859+
unittest(range.cpu_addr == 0x40000000,
860+
"for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
861+
range.cpu_addr, np);
862+
unittest(range.pci_addr == 0xc0000000,
863+
"for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
864+
range.pci_addr, np);
865+
}
866+
i++;
867+
}
868+
869+
of_node_put(np);
870+
}
871+
782872
static void __init of_unittest_parse_interrupts(void)
783873
{
784874
struct device_node *np;
@@ -2554,6 +2644,8 @@ static int __init of_unittest(void)
25542644
of_unittest_changeset();
25552645
of_unittest_parse_interrupts();
25562646
of_unittest_parse_interrupts_extended();
2647+
of_unittest_parse_dma_ranges();
2648+
of_unittest_pci_dma_ranges();
25572649
of_unittest_match_node();
25582650
of_unittest_platform_populate();
25592651
of_unittest_overlay();

0 commit comments

Comments
 (0)