fix(lib/s2tt): allow rtt fold till level 1

Allow RMI_RTT_FOLD of Level 2 Assigned RTTs.
Disallow RMI_RTT_FOLD after Level 2 for Assigned NS RTTs.
Note that UNASSIGNED and UNASSIGNED_NS Rtts can be folded
till min block level allowed by Architecture.

Change-Id: I494835b479542529973edb7f253a079d2aced07d
Signed-off-by: Shruti Gupta <shruti.gupta@arm.com>
diff --git a/lib/s2tt/include/s2tt.h b/lib/s2tt/include/s2tt.h
index 42347e8..27d47e7 100644
--- a/lib/s2tt/include/s2tt.h
+++ b/lib/s2tt/include/s2tt.h
@@ -47,7 +47,7 @@
 #define S2TT_MIN_STARTING_LEVEL		(0)
 #define S2TT_MIN_STARTING_LEVEL_LPA2	(-1)
 #define S2TT_PAGE_LEVEL			(3)
-#define S2TT_MIN_BLOCK_LEVEL		(2)
+#define S2TT_MIN_BLOCK_LEVEL		(1)
 
 /*
  * S2TTE_STRIDE: The number of bits resolved in a single level of translation
diff --git a/lib/s2tt/src/s2tt.c b/lib/s2tt/src/s2tt.c
index 19dffba..754d912 100644
--- a/lib/s2tt/src/s2tt.c
+++ b/lib/s2tt/src/s2tt.c
@@ -752,9 +752,9 @@
 
 	desc_type = s2tte & S2TT_DESC_TYPE_MASK;
 
-	/* Only pages at L3 and valid blocks at L2 allowed */
+	/* Only pages at L3 and valid blocks at L2 and L1 allowed */
 	if (((level == S2TT_PAGE_LEVEL) && (desc_type == S2TTE_L3_PAGE)) ||
-	    ((level == S2TT_MIN_BLOCK_LEVEL) && (desc_type == S2TTE_L012_BLOCK))) {
+	    ((level >= S2TT_MIN_BLOCK_LEVEL) && (desc_type == S2TTE_L012_BLOCK))) {
 		return true;
 	}
 
diff --git a/runtime/rmi/rtt.c b/runtime/rmi/rtt.c
index 3175d6d..1812f76 100644
--- a/runtime/rmi/rtt.c
+++ b/runtime/rmi/rtt.c
@@ -351,6 +351,16 @@
 		} else if (s2tt_is_unassigned_ns_block(&s2_ctx, table)) {
 			parent_s2tte = s2tte_create_unassigned_ns(&s2_ctx);
 		} else if (s2tt_maps_assigned_ns_block(&s2_ctx, table, level)) {
+
+			/*
+			 * The RMM specification does not allow creating block entries less than
+			 * S2TT_MIN_BLOCK_LEVEL for ASSIGNED_NS state.
+			 */
+			if (level <= S2TT_MIN_BLOCK_LEVEL) {
+				ret = pack_return_code(RMI_ERROR_RTT,
+						(unsigned char)wi.last_level);
+				goto out_unmap_table;
+			}
 			unsigned long s2tte = s2tte_read(&table[0]);
 
 			/*