refactor(lib/xlat): split xlat_arch_setup_mmu_cfg

Decouple calculating from writing MMU registers. A new structure is
added that can hold the calculated values for mmu registers.
xlat_arch_setup_mmu_cfg only initialises this structure, while the
new function xlat_arch_write_mmu_cfg writes the values to the
registers.

Change-Id: Iea4be3c4594f10057bf35b4e2b2c8030cca2b764
Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
diff --git a/lib/xlat/tests/xlat_tests_base_g2.cpp b/lib/xlat/tests/xlat_tests_base_g2.cpp
index 1360403..50dd999 100644
--- a/lib/xlat/tests/xlat_tests_base_g2.cpp
+++ b/lib/xlat/tests/xlat_tests_base_g2.cpp
@@ -273,7 +273,7 @@
 	}
 }
 
-void xlat_ctx_init_tc6(void)
+void xlat_ctx_init_tc5(void)
 {
 	struct xlat_ctx ctx;
 	struct xlat_ctx_cfg cfg;
@@ -288,7 +288,7 @@
 	int base_lvl, end_lvl;
 
 	/**********************************************************************
-	 * TEST CASE 6:
+	 * TEST CASE 5:
 	 *
 	 * For each possible base level, create a set of mmap regions
 	 * ranging from level 1 or 0 (lowest level at which a valid walk can
@@ -2149,6 +2149,7 @@
 		PARANGE_0110_WIDTH
 	};
 	uint64_t max_va_size = XLAT_TEST_MAX_VA_SIZE();
+	struct xlat_mmu_cfg mmu_config;
 
 	/***************************************************************
 	 * TEST CASE 1:
@@ -2195,11 +2196,14 @@
 		CHECK_TRUE(retval == 0);
 
 		/* Initialize MMU for the given context */
-		retval = xlat_arch_setup_mmu_cfg(&ctx[i]);
+		retval = xlat_arch_setup_mmu_cfg(&ctx[i], &mmu_config);
 
 		/* Verify that the MMU has been configured */
 		CHECK_TRUE(retval == 0);
 
+		/* Write the MMU config for the given context */
+		xlat_arch_write_mmu_cfg(&mmu_config);
+
 		/* Validate TTBR_EL2 for each context */
 		validate_ttbrx_el2(&ctx[i]);
 	}
@@ -2217,6 +2221,7 @@
 	int retval;
 	struct xlat_mmap_region init_mmap;
 	uint64_t max_va_size =	XLAT_TEST_MAX_VA_SIZE();
+	struct xlat_mmu_cfg mmu_config;
 
 	/***************************************************************
 	 * TEST CASE 2:
@@ -2259,7 +2264,7 @@
 	ctx.cfg->initialized = false;
 
 	/* Try to initialize MMU for the given context */
-	retval = xlat_arch_setup_mmu_cfg(&ctx);
+	retval = xlat_arch_setup_mmu_cfg(&ctx, &mmu_config);
 
 	/* Verify that the MMU has failed to be initialized */
 	CHECK_TRUE(retval == -EINVAL);
@@ -2274,7 +2279,7 @@
 				ID_AA64MMFR0_EL1_TGRAN4_NOT_SUPPORTED));
 
 	/* Try to initialize MMU for the given context */
-	retval = xlat_arch_setup_mmu_cfg(&ctx);
+	retval = xlat_arch_setup_mmu_cfg(&ctx, &mmu_config);
 
 	/* Verify that the MMU has failed to be initialized */
 	CHECK_TRUE(retval == -EPERM);
@@ -2291,10 +2296,10 @@
 				ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED));
 
 	/* Try to initialize MMU for the given context */
-	retval = xlat_arch_setup_mmu_cfg(&ctx);
+	retval = xlat_arch_setup_mmu_cfg(&ctx, &mmu_config);
 
 	/* Verify that the MMU has failed to be initialized */
-	CHECK_TRUE(retval == -ENOMEM);
+	CHECK_TRUE(retval == -EPERM);
 
 }
 
@@ -2306,8 +2311,10 @@
 	 * Test xlat_arch_setup_mmu_cfg() with a NULL context.
 	 ***************************************************************/
 
+	struct xlat_mmu_cfg mmu_config;
+
 	test_helpers_expect_assert_fail(true);
-	(void)xlat_arch_setup_mmu_cfg(NULL);
+	(void)xlat_arch_setup_mmu_cfg(NULL, &mmu_config);
 	test_helpers_fail_if_no_assert_failed();
 }
 
@@ -2328,13 +2335,15 @@
 	 * in order to generate an initial valid context.
 	 ***************************************************************/
 
+	struct xlat_mmu_cfg mmu_config;
+
 	xlat_get_llt_from_va_prepare_assertion(&ctx, &cfg, &tbls, &init_mmap);
 
 	/* Force the context configuration to NULL */
 	ctx.cfg = NULL;
 
 	test_helpers_expect_assert_fail(true);
-	(void)xlat_arch_setup_mmu_cfg(&ctx);
+	(void)xlat_arch_setup_mmu_cfg(&ctx, &mmu_config);
 	test_helpers_fail_if_no_assert_failed();
 }
 
@@ -2355,13 +2364,15 @@
 	 * in order to generate an initial valid context.
 	 ***************************************************************/
 
+	struct xlat_mmu_cfg mmu_config;
+
 	xlat_get_llt_from_va_prepare_assertion(&ctx, &cfg, &tbls, &init_mmap);
 
 	/* Force the context tables structure to NULL */
 	ctx.tbls = NULL;
 
 	test_helpers_expect_assert_fail(true);
-	(void)xlat_arch_setup_mmu_cfg(&ctx);
+	(void)xlat_arch_setup_mmu_cfg(&ctx, &mmu_config);
 	test_helpers_fail_if_no_assert_failed();
 }
 
@@ -2374,6 +2385,7 @@
 	int retval;
 	struct xlat_mmap_region init_mmap;
 	uint64_t max_va_size =	XLAT_TEST_MAX_VA_SIZE();
+	struct xlat_mmu_cfg mmu_config;
 
 	/***************************************************************
 	 * TEST CASE 6:
@@ -2408,9 +2420,28 @@
 
 	test_helpers_expect_assert_fail(true);
 
-	/* Try to initialize MMU for the given context */
-	retval = xlat_arch_setup_mmu_cfg(&ctx);
+	/* Initialize MMU config for the given context */
+	retval = xlat_arch_setup_mmu_cfg(&ctx, &mmu_config);
+	CHECK_TRUE(retval == 0);
 
+	/* Try to write the MMU config for the given context */
+	xlat_arch_write_mmu_cfg(&mmu_config);
+
+	test_helpers_fail_if_no_assert_failed();
+}
+
+void xlat_arch_setup_mmu_cfg_tc7(void)
+{
+	/***************************************************************
+	 * TEST CASE 7:
+	 *
+	 * Test xlat_arch_setup_mmu_cfg() with a NULL config.
+	 ***************************************************************/
+
+	struct xlat_ctx ctx;
+
+	test_helpers_expect_assert_fail(true);
+	(void)xlat_arch_setup_mmu_cfg(&ctx, NULL);
 	test_helpers_fail_if_no_assert_failed();
 }