Add tests for the functions dm_pci_bus_to_phys() and
dm_pci_phys_to_bus() which convert between PCI bus addresses and
physical addresses based on the ranges declared for the PCI controller.

The ranges of bus#1 are used for the tests, adding a translation to one
of the ranges to cover more cases.

Signed-off-by: Andrew Scull <asc...@google.com>
---
 arch/sandbox/dts/test.dts |   2 +-
 test/dm/pci.c             | 102 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 48ca3e1e47..76c75e08e7 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -979,7 +979,7 @@
                #address-cells = <3>;
                #size-cells = <2>;
                ranges = <0x02000000 0 0x30000000 0x30000000 0 0x2000 // MEM0
-                         0x02000000 0 0x31000000 0x31000000 0 0x2000 // MEM1
+                         0x02000000 0 0x31000000 0x3e000000 0 0x2000 // MEM1
                          0x01000000 0 0x40000000 0x40000000 0 0x2000>;
                sandbox,dev-info = <0x08 0x00 0x1234 0x5678
                                    0x0c 0x00 0x1234 0x5678
diff --git a/test/dm/pci.c b/test/dm/pci.c
index 00e4440a9d..9789103c7d 100644
--- a/test/dm/pci.c
+++ b/test/dm/pci.c
@@ -376,3 +376,105 @@ static int dm_test_pci_region_multi(struct 
unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_pci_region_multi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/*
+ * Test the translation of PCI bus addresses to physical addresses using the
+ * ranges from bus#1.
+ */
+static int dm_test_pci_bus_to_phys(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+       phys_addr_t phys_addr;
+
+       ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
+
+       /* Before any of the ranges. */
+       phys_addr = dm_pci_bus_to_phys(dev, 0x20000000, 0x400, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       /* Identity range: whole, start, mid, end */
+       phys_addr = dm_pci_bus_to_phys(dev, 0x2fff0000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x30000000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0x30000000, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x30000000, 0x1000, PCI_REGION_MEM);
+       ut_asserteq(0x30000000, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x30000abc, 0x12, PCI_REGION_MEM);
+       ut_asserteq(0x30000abc, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x30000800, 0x1800, PCI_REGION_MEM);
+       ut_asserteq(0x30000800, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x30008000, 0x1801, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       /* Translated range: whole, start, mid, end */
+       phys_addr = dm_pci_bus_to_phys(dev, 0x30ff0000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x31000000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0x3e000000, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x31000000, 0x1000, PCI_REGION_MEM);
+       ut_asserteq(0x3e000000, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x31000abc, 0x12, PCI_REGION_MEM);
+       ut_asserteq(0x3e000abc, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x31000800, 0x1800, PCI_REGION_MEM);
+       ut_asserteq(0x3e000800, phys_addr);
+       phys_addr = dm_pci_bus_to_phys(dev, 0x31008000, 0x1801, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       /* Beyond all of the ranges. */
+       phys_addr = dm_pci_bus_to_phys(dev, 0x32000000, 0x400, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       return 0;
+}
+DM_TEST(dm_test_pci_bus_to_phys, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/*
+ * Test the translation of physical addresses to PCI bus addresses using the
+ * ranges from bus#1.
+ */
+static int dm_test_pci_phys_to_bus(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+       phys_addr_t phys_addr;
+
+       ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
+
+       /* Before any of the ranges. */
+       phys_addr = dm_pci_phys_to_bus(dev, 0x20000000, 0x400, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       /* Identity range: whole, start, mid, end */
+       phys_addr = dm_pci_phys_to_bus(dev, 0x2fff0000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x30000000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0x30000000, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x30000000, 0x1000, PCI_REGION_MEM);
+       ut_asserteq(0x30000000, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x30000abc, 0x12, PCI_REGION_MEM);
+       ut_asserteq(0x30000abc, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x30000800, 0x1800, PCI_REGION_MEM);
+       ut_asserteq(0x30000800, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x30008000, 0x1801, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       /* Translated range: whole, start, mid, end */
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3dff0000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3e000000, 0x2000, PCI_REGION_MEM);
+       ut_asserteq(0x31000000, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3e000000, 0x1000, PCI_REGION_MEM);
+       ut_asserteq(0x31000000, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3e000abc, 0x12, PCI_REGION_MEM);
+       ut_asserteq(0x31000abc, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3e000800, 0x1800, PCI_REGION_MEM);
+       ut_asserteq(0x31000800, phys_addr);
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3e008000, 0x1801, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       /* Beyond all of the ranges. */
+       phys_addr = dm_pci_phys_to_bus(dev, 0x3f000000, 0x400, PCI_REGION_MEM);
+       ut_asserteq(0, phys_addr);
+
+       return 0;
+}
+DM_TEST(dm_test_pci_phys_to_bus, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
-- 
2.35.1.1021.g381101b075-goog

Reply via email to