refactor(gpt): productize and refactor GPT library

This patch updates and refactors the GPT library and fixes bugs.

- Support all combinations of PGS, PPS, and L0GPTSZ parameters.
- PPS and PGS are set at runtime, L0GPTSZ is read from GPCCR_EL3.
- Use compiler definitions to simplify code.
- Renaming functions to better suit intended uses.
- MMU enabled before GPT APIs called.
- Add comments to make function usage more clear in GPT library.
- Added _rme suffix to file names to differentiate better from the
  GPT file system code.
- Renamed gpt_defs.h to gpt_rme_private.h to better separate private
  and public code.
- Renamed gpt_core.c to gpt_rme.c to better conform to TF-A precedent.

Signed-off-by: John Powell <john.powell@arm.com>
Change-Id: I4cbb23b0f81e697baa9fb23ba458aa3f7d1ed919
diff --git a/plat/arm/common/arm_bl2_setup.c b/plat/arm/common/arm_bl2_setup.c
index ef37206..2871b1b 100644
--- a/plat/arm/common/arm_bl2_setup.c
+++ b/plat/arm/common/arm_bl2_setup.c
@@ -18,12 +18,16 @@
 #include <drivers/partition/partition.h>
 #include <lib/fconf/fconf.h>
 #include <lib/fconf/fconf_dyn_cfg_getter.h>
-#include <lib/gpt/gpt.h>
+#if ENABLE_RME
+#include <lib/gpt_rme/gpt_rme.h>
+#endif /* ENABLE_RME */
 #ifdef SPD_opteed
 #include <lib/optee_utils.h>
 #endif
 #include <lib/utils.h>
+#if ENABLE_RME
 #include <plat/arm/common/arm_pas_def.h>
+#endif /* ENABLE_RME */
 #include <plat/arm/common/plat_arm.h>
 #include <plat/common/platform.h>
 
@@ -130,6 +134,7 @@
 }
 
 #if ENABLE_RME
+
 static void arm_bl2_plat_gpt_setup(void)
 {
 	/*
@@ -137,32 +142,38 @@
 	 * the layout, so the array cannot be constant.
 	 */
 	pas_region_t pas_regions[] = {
-		ARM_PAS_GPI_ANY,
 		ARM_PAS_KERNEL,
-		ARM_PAS_TZC,
+		ARM_PAS_SECURE,
 		ARM_PAS_REALM,
 		ARM_PAS_EL3_DRAM,
 		ARM_PAS_GPTS
 	};
 
-	gpt_init_params_t gpt_params = {
-		PLATFORM_PGS,
-		PLATFORM_PPS,
-		PLATFORM_L0GPTSZ,
-		pas_regions,
-		(unsigned int)(sizeof(pas_regions)/sizeof(pas_region_t)),
-		ARM_L0_GPT_ADDR_BASE, ARM_L0_GPT_SIZE,
-		ARM_L1_GPT_ADDR_BASE, ARM_L1_GPT_SIZE
-	};
+	/* Initialize entire protected space to GPT_GPI_ANY. */
+	if (gpt_init_l0_tables(GPCCR_PPS_4GB, ARM_L0_GPT_ADDR_BASE,
+		ARM_L0_GPT_SIZE) < 0) {
+		ERROR("gpt_init_l0_tables() failed!\n");
+		panic();
+	}
 
-	/* Initialise the global granule tables */
-	INFO("Enabling Granule Protection Checks\n");
-	if (gpt_init(&gpt_params) < 0) {
+	/* Carve out defined PAS ranges. */
+	if (gpt_init_pas_l1_tables(GPCCR_PGS_4K,
+				   ARM_L1_GPT_ADDR_BASE,
+				   ARM_L1_GPT_SIZE,
+				   pas_regions,
+				   (unsigned int)(sizeof(pas_regions) /
+				   sizeof(pas_region_t))) < 0) {
+		ERROR("gpt_init_pas_l1_tables() failed!\n");
 		panic();
 	}
 
-	gpt_enable();
+	INFO("Enabling Granule Protection Checks\n");
+	if (gpt_enable() < 0) {
+		ERROR("gpt_enable() failed!\n");
+		panic();
+	}
 }
+
 #endif /* ENABLE_RME */
 
 /*******************************************************************************
@@ -201,9 +212,6 @@
 #if ENABLE_RME
 	/* Initialise the secure environment */
 	plat_arm_security_setup();
-
-	/* Initialise and enable Granule Protection */
-	arm_bl2_plat_gpt_setup();
 #endif
 	setup_page_tables(bl_regions, plat_arm_get_mmap());
 
@@ -212,6 +220,9 @@
 	/* BL2 runs in EL3 when RME enabled. */
 	assert(get_armv9_2_feat_rme_support() != 0U);
 	enable_mmu_el3(0);
+
+	/* Initialise and enable granule protection after MMU. */
+	arm_bl2_plat_gpt_setup();
 #else
 	enable_mmu_el1(0);
 #endif
diff --git a/plat/arm/common/arm_bl31_setup.c b/plat/arm/common/arm_bl31_setup.c
index d131bb9..6472590 100644
--- a/plat/arm/common/arm_bl31_setup.c
+++ b/plat/arm/common/arm_bl31_setup.c
@@ -13,10 +13,11 @@
 #include <drivers/console.h>
 #include <lib/debugfs.h>
 #include <lib/extensions/ras.h>
-#include <lib/gpt/gpt.h>
+#if ENABLE_RME
+#include <lib/gpt_rme/gpt_rme.h>
+#endif
 #include <lib/mmio.h>
 #include <lib/xlat_tables/xlat_tables_compat.h>
-#include <plat/arm/common/arm_pas_def.h>
 #include <plat/arm/common/plat_arm.h>
 #include <plat/common/platform.h>
 #include <platform_def.h>
@@ -235,28 +236,6 @@
 	 */
 	bl33_image_ep_info.args.arg0 = (u_register_t)ARM_DRAM1_BASE;
 #endif
-
-#if ENABLE_RME
-	/*
-	 * Initialise Granule Protection library and enable GPC
-	 * for the primary processor. The tables were initialised
-	 * in BL2, so there is no need to provide any PAS here.
-	 */
-	gpt_init_params_t gpt_params = {
-		PLATFORM_PGS,
-		PLATFORM_PPS,
-		PLATFORM_L0GPTSZ,
-		NULL,
-		0U,
-		ARM_L0_GPT_ADDR_BASE, ARM_L0_GPT_SIZE,
-		ARM_L1_GPT_ADDR_BASE, ARM_L1_GPT_SIZE
-	};
-
-	/* Initialise the global granule tables. */
-	if (gpt_init(&gpt_params) < 0) {
-		panic();
-	}
-#endif /* ENABLE_RME */
 }
 
 void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
@@ -430,6 +409,19 @@
 
 	enable_mmu_el3(0);
 
+#if ENABLE_RME
+	/*
+	 * Initialise Granule Protection library and enable GPC for the primary
+	 * processor. The tables have already been initialized by a previous BL
+	 * stage, so there is no need to provide any PAS here. This function
+	 * sets up pointers to those tables.
+	 */
+	if (gpt_runtime_init() < 0) {
+		ERROR("gpt_runtime_init() failed!\n");
+		panic();
+	}
+#endif /* ENABLE_RME */
+
 	arm_setup_romlib();
 }