Add a test to check the management of reserved region with flags.

Signed-off-by: Patrick Delaunay <patrick.delau...@st.com>
---

 test/lib/lmb.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 89 insertions(+)

diff --git a/test/lib/lmb.c b/test/lib/lmb.c
index 644ee78758..d7bd826190 100644
--- a/test/lib/lmb.c
+++ b/test/lib/lmb.c
@@ -659,3 +659,92 @@ static int lib_test_lmb_get_free_size(struct 
unit_test_state *uts)
 
 DM_TEST(lib_test_lmb_get_free_size,
        UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+static int lib_test_lmb_flags(struct unit_test_state *uts)
+{
+       const phys_addr_t ram = 0x40000000;
+       const phys_size_t ram_size = 0x20000000;
+       struct lmb lmb;
+       long ret;
+
+       lmb_init(&lmb);
+
+       ret = lmb_add(&lmb, ram, ram_size);
+       ut_asserteq(ret, 0);
+
+       /* reserve, same flag */
+       ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 0);
+       ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
+                  0, 0, 0, 0);
+
+       /* reserve again, same flag */
+       ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 0);
+       ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
+                  0, 0, 0, 0);
+
+       /* reserve again, new flag */
+       ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NONE);
+       ut_asserteq(ret, -1);
+       ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
+                  0, 0, 0, 0);
+
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
+
+       /* merge after */
+       ret = lmb_reserve_flags(&lmb, 0x40020000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 1);
+       ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x20000,
+                  0, 0, 0, 0);
+
+       /* merge before */
+       ret = lmb_reserve_flags(&lmb, 0x40000000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 1);
+       ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40000000, 0x30000,
+                  0, 0, 0, 0);
+
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
+
+       ret = lmb_reserve_flags(&lmb, 0x40030000, 0x10000, LMB_NONE);
+       ut_asserteq(ret, 0);
+       ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
+                  0x40030000, 0x10000, 0, 0);
+
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
+
+       /* test that old API use LMB_NONE */
+       ret = lmb_reserve(&lmb, 0x40040000, 0x10000);
+       ut_asserteq(ret, 1);
+       ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
+                  0x40030000, 0x20000, 0, 0);
+
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
+
+       ret = lmb_reserve_flags(&lmb, 0x40070000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 0);
+       ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
+                  0x40030000, 0x20000, 0x40070000, 0x10000);
+
+       ret = lmb_reserve_flags(&lmb, 0x40050000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 0);
+       ASSERT_LMB(&lmb, ram, ram_size, 4, 0x40000000, 0x30000,
+                  0x40030000, 0x20000, 0x40050000, 0x10000);
+
+       /* merge with 2 adjacent regions */
+       ret = lmb_reserve_flags(&lmb, 0x40060000, 0x10000, LMB_NOMAP);
+       ut_asserteq(ret, 2);
+       ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
+                  0x40030000, 0x20000, 0x40050000, 0x30000);
+
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
+       ut_asserteq(lmb_is_nomap(&lmb.reserved.region[2]), 1);
+
+       return 0;
+}
+
+DM_TEST(lib_test_lmb_flags,
+       UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
-- 
2.17.1

Reply via email to