feat(lib/realm): modify struct granule

This patch makes the following changes:
- val of spinlock_t type changes from unsigned int
 to unsigned char;
- 'enum granule_state' removed and replaced with
 macro definitions;
- type of 'struct granule' state changed to unsigned char;
- type of 'struct granule' refcount changed from unsigned long
  to unsigned short, as its maximum value cannot exceed 512.
- 'refcount' access functions modified to use 16-bit values.
These modifications change the size of 'struct granule' from
16 to 4 bytes and reduce the total size of 'granules[]'
array from 16MB to 4MB for default RMM_MAX_GRANULES = 0x100000.

Change-Id: I611ed3f349806b033e03c52c5f60a0f903f93e11
Signed-off-by: AlexeiFedorov <Alexei.Fedorov@arm.com>
diff --git a/runtime/core/init.c b/runtime/core/init.c
index 69783e9..fe22b5b 100644
--- a/runtime/core/init.c
+++ b/runtime/core/init.c
@@ -13,7 +13,6 @@
 #include <smc-rmi.h>
 #include <smc-rsi.h>
 
-
 #ifdef NDEBUG
 #define RMM_BUILD_TYPE	"release"
 #else
diff --git a/runtime/rmi/granule.c b/runtime/rmi/granule.c
index 701c5cf..c115fd6 100644
--- a/runtime/rmi/granule.c
+++ b/runtime/rmi/granule.c
@@ -10,6 +10,11 @@
 #include <smc-rmi.h>
 #include <smc.h>
 
+COMPILER_ASSERT(sizeof(struct granule) == (
+		SIZE_OF(granule, lock) +
+		SIZE_OF(granule, state) +
+		SIZE_OF(granule, refcount)));
+
 unsigned long smc_granule_delegate(unsigned long addr)
 {
 	struct granule *g;
diff --git a/runtime/rmi/rec.c b/runtime/rmi/rec.c
index fe79afb..3775670 100644
--- a/runtime/rmi/rec.c
+++ b/runtime/rmi/rec.c
@@ -256,7 +256,7 @@
 	struct rd *rd;
 	struct rmi_rec_params rec_params;
 	unsigned long rec_idx;
-	enum granule_state new_rec_state = GRANULE_STATE_DELEGATED;
+	unsigned char new_rec_state = GRANULE_STATE_DELEGATED;
 	unsigned long ret;
 	bool ns_access_ok;
 	unsigned int num_rec_aux;
@@ -484,7 +484,7 @@
 	 * members of REC structure (such as rec->running) only if the counter
 	 * is zero.
 	 */
-	if (granule_refcount_read_acquire(g_calling_rec) != 0UL) {
+	if (granule_refcount_read_acquire(g_calling_rec) != 0U) {
 		/*
 		 * The `calling` REC is running on another PE and therefore it
 		 * may not have a pending PSCI request.
diff --git a/runtime/rmi/rtt.c b/runtime/rmi/rtt.c
index ab78920..9abfd7d 100644
--- a/runtime/rmi/rtt.c
+++ b/runtime/rmi/rtt.c
@@ -174,7 +174,7 @@
 		 * Increase the refcount to mark the granule as in-use. refcount
 		 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
 		 */
-		__granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
+		__granule_refcount_inc(g_tbl, (unsigned short)S2TTES_PER_S2TT);
 
 	} else if (s2tte_is_assigned_empty(&s2_ctx, parent_s2tte, level - 1L)) {
 		unsigned long block_pa;
@@ -193,7 +193,7 @@
 		 * Increase the refcount to mark the granule as in-use. refcount
 		 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
 		 */
-		__granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
+		__granule_refcount_inc(g_tbl, (unsigned short)S2TTES_PER_S2TT);
 
 	} else if (s2tte_is_assigned_ram(&s2_ctx, parent_s2tte, level - 1L)) {
 		unsigned long block_pa;
@@ -218,7 +218,7 @@
 		 * Increase the refcount to mark the granule as in-use. refcount
 		 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
 		 */
-		__granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
+		__granule_refcount_inc(g_tbl, (unsigned short)S2TTES_PER_S2TT);
 
 	} else if (s2tte_is_assigned_ns(&s2_ctx, parent_s2tte, level - 1L)) {
 		unsigned long block_pa;
@@ -371,7 +371,7 @@
 			goto out_unmap_table;
 		}
 		__granule_put(wi.g_llt);
-	} else if (g_tbl->refcount == S2TTES_PER_S2TT) {
+	} else if (g_tbl->refcount == (unsigned short)S2TTES_PER_S2TT) {
 
 		unsigned long s2tte, block_pa;
 
@@ -416,7 +416,7 @@
 			goto out_unmap_table;
 		}
 
-		__granule_refcount_dec(g_tbl, S2TTES_PER_S2TT);
+		__granule_refcount_dec(g_tbl, (unsigned short)S2TTES_PER_S2TT);
 	} else {
 		/*
 		 * The table holds a mixture of different types of s2ttes.
@@ -871,7 +871,7 @@
 	struct s2tt_walk wi;
 	struct s2tt_context *s2_ctx;
 	unsigned long s2tte, *s2tt;
-	enum granule_state new_data_state = GRANULE_STATE_DELEGATED;
+	unsigned char new_data_state = GRANULE_STATE_DELEGATED;
 	unsigned long ret;
 
 	if (!find_lock_two_granules(data_addr,
@@ -1365,7 +1365,7 @@
 		return;
 	}
 
-	if (granule_refcount_read_acquire(g_rec) != 0UL) {
+	if (granule_refcount_read_acquire(g_rec) != 0U) {
 		res->x[0] = RMI_ERROR_REC;
 		goto out_unlock_rec_rd;
 	}
diff --git a/runtime/rsi/psci.c b/runtime/rsi/psci.c
index 1b42da1..fa2401a 100644
--- a/runtime/rsi/psci.c
+++ b/runtime/rsi/psci.c
@@ -335,7 +335,7 @@
 					  unsigned long caller_sctlr_el1,
 					  unsigned long status)
 {
-	if ((granule_refcount_read_acquire(target_rec->g_rec) != 0UL) ||
+	if ((granule_refcount_read_acquire(target_rec->g_rec) != 0U) ||
 		target_rec->runnable) {
 		return PSCI_RETURN_ALREADY_ON;
 	}
@@ -357,7 +357,7 @@
 
 static unsigned long complete_psci_affinity_info(struct rec *target_rec)
 {
-	if ((granule_refcount_read_acquire(target_rec->g_rec) != 0UL) ||
+	if ((granule_refcount_read_acquire(target_rec->g_rec) != 0U) ||
 		target_rec->runnable) {
 		return PSCI_AFFINITY_INFO_ON;
 	}