fix(rmm): fix MISRA C:2012 Rule 10.4

Signed-off-by: AlexeiFedorov <Alexei.Fedorov@arm.com>
Change-Id: I03630ef02c8c294a552ad10523d9c78091ce161a
diff --git a/lib/realm/include/granule.h b/lib/realm/include/granule.h
index b5ca396..fdfedf0 100644
--- a/lib/realm/include/granule.h
+++ b/lib/realm/include/granule.h
@@ -149,14 +149,14 @@
 /* Must be called with g->lock held */
 static inline void __granule_get(struct granule *g)
 {
-	assert(g->lock.val != 0);
+	assert(g->lock.val != 0U);
 	g->refcount++;
 }
 
 /* Must be called with g->lock held */
 static inline void __granule_put(struct granule *g)
 {
-	assert(g->lock.val != 0);
+	assert(g->lock.val != 0U);
 	assert(g->refcount > 0UL);
 	g->refcount--;
 }
@@ -164,14 +164,14 @@
 /* Must be called with g->lock held */
 static inline void __granule_refcount_inc(struct granule *g, unsigned long val)
 {
-	assert(g->lock.val != 0);
+	assert(g->lock.val != 0U);
 	g->refcount += val;
 }
 
 /* Must be called with g->lock held */
 static inline void __granule_refcount_dec(struct granule *g, unsigned long val)
 {
-	assert(g->lock.val != 0);
+	assert(g->lock.val != 0U);
 	assert(g->refcount >= val);
 	g->refcount -= val;
 }
diff --git a/lib/realm/include/realm.h b/lib/realm/include/realm.h
index c86b03d..deafb7f 100644
--- a/lib/realm/include/realm.h
+++ b/lib/realm/include/realm.h
@@ -12,9 +12,9 @@
 #include <rec.h>
 #include <table.h>
 
-#define REALM_STATE_NEW		0
-#define REALM_STATE_ACTIVE	1
-#define REALM_STATE_SYSTEM_OFF	2
+#define REALM_STATE_NEW		0U
+#define REALM_STATE_ACTIVE	1U
+#define REALM_STATE_SYSTEM_OFF	2U
 
 /*
  * Stage 2 configuration of the Realm
diff --git a/lib/realm/include/table.h b/lib/realm/include/table.h
index 3261532..7b1a5bf 100644
--- a/lib/realm/include/table.h
+++ b/lib/realm/include/table.h
@@ -9,8 +9,8 @@
 #include <arch_features.h>
 #include <memory.h>
 
-#define MIN_IPA_BITS		32
-#define MAX_IPA_BITS		48
+#define MIN_IPA_BITS		32U
+#define MAX_IPA_BITS		48U
 #define MAX_IPA_SIZE		(1UL << MAX_IPA_BITS)
 
 #define MIN_STARTING_LEVEL	0
diff --git a/lib/realm/src/granule.c b/lib/realm/src/granule.c
index c3a99fa..5d278cf 100644
--- a/lib/realm/src/granule.c
+++ b/lib/realm/src/granule.c
@@ -136,17 +136,14 @@
 /*
  * Sort a set of granules by their address.
  */
-static void sort_granules(struct granule_set *granules,
-			unsigned long n)
+static void sort_granules(struct granule_set *granules, unsigned long n)
 {
-	unsigned long i;
-
-	for (i = 1UL; i < n; i++) {
+	for (unsigned long i = 1UL; i < n; i++) {
 		struct granule_set temp = granules[i];
 		unsigned long j = i;
 
-		while ((j > 0UL) && (granules[j - 1].addr > temp.addr)) {
-			granules[j] = granules[j - 1];
+		while ((j > 0UL) && (granules[j - 1UL].addr > temp.addr)) {
+			granules[j] = granules[j - 1UL];
 			j--;
 		}
 		if (i != j) {
@@ -183,16 +180,16 @@
  * If the function fails, no lock is held and no *->g_ret pointers are
  * modified.
  */
-static bool find_lock_granules(struct granule_set *granules,
-				unsigned long n)
+static bool find_lock_granules(struct granule_set *granules, unsigned long n)
 {
-	long i;
+	unsigned long i;
 
 	sort_granules(granules, n);
 
-	for (i = 0L; i < n; i++) {
+	for (i = 0UL; i < n; i++) {
 		/* Check for duplicates */
-		if ((i > 0L) && (granules[i].addr == granules[i - 1].addr)) {
+		if ((i != 0UL) &&
+		    (granules[i].addr == granules[i - 1UL].addr)) {
 			goto out_err;
 		}
 
@@ -203,14 +200,14 @@
 		}
 	}
 
-	for (i = 0L; i < n; i++) {
+	for (i = 0UL; i < n; i++) {
 		*granules[i].g_ret = granules[i].g;
 	}
 
 	return true;
 
 out_err:
-	for (i = i - 1; i >= 0L; i--) {
+	while (i-- != 0UL) {
 		granule_unlock(granules[i].g);
 	}
 
diff --git a/lib/realm/src/include/buffer_private.h b/lib/realm/src/include/buffer_private.h
index 5034d08..77b1a2a 100644
--- a/lib/realm/src/include/buffer_private.h
+++ b/lib/realm/src/include/buffer_private.h
@@ -11,8 +11,8 @@
  * needs to be a power of two, so round NR_CPU_SLOTS up to the closest
  * power of two.
  */
-#define ROUNDED_NR_CPU_SLOTS	\
-	(1ULL << (64U - (unsigned int)__builtin_clzll((NR_CPU_SLOTS) - 1U)))
+#define ROUNDED_NR_CPU_SLOTS	(1ULL << (64U - \
+	(unsigned int)__builtin_clzll(((unsigned long long)NR_CPU_SLOTS) - 1U)))
 
 #define RMM_SLOT_BUF_VA_SIZE	((ROUNDED_NR_CPU_SLOTS) * (GRANULE_SIZE))
 
diff --git a/lib/realm/src/s2tt.c b/lib/realm/src/s2tt.c
index 9269a85..646bd65 100644
--- a/lib/realm/src/s2tt.c
+++ b/lib/realm/src/s2tt.c
@@ -26,7 +26,7 @@
  * The maximum number of bits supported by the RMM for a stage 2 translation
  * output address (including stage 2 table entries).
  */
-#define S2TTE_OA_BITS			48
+#define S2TTE_OA_BITS			48U
 
 #define DESC_TYPE_MASK			3UL
 #define S2TTE_Lx_INVALID		0UL
@@ -107,19 +107,19 @@
  */
 
 #define S2TTE_INVALID_HIPAS_SHIFT	2
-#define S2TTE_INVALID_HIPAS_WIDTH	3
+#define S2TTE_INVALID_HIPAS_WIDTH	3U
 #define S2TTE_INVALID_HIPAS_MASK	MASK(S2TTE_INVALID_HIPAS)
 
-#define S2TTE_INVALID_HIPAS_UNASSIGNED	(INPLACE(S2TTE_INVALID_HIPAS, 0))
-#define S2TTE_INVALID_HIPAS_ASSIGNED	(INPLACE(S2TTE_INVALID_HIPAS, 1))
+#define S2TTE_INVALID_HIPAS_UNASSIGNED	(INPLACE(S2TTE_INVALID_HIPAS, 0UL))
+#define S2TTE_INVALID_HIPAS_ASSIGNED	(INPLACE(S2TTE_INVALID_HIPAS, 1UL))
 
 #define S2TTE_INVALID_RIPAS_SHIFT	5
-#define S2TTE_INVALID_RIPAS_WIDTH	2
+#define S2TTE_INVALID_RIPAS_WIDTH	2U
 #define S2TTE_INVALID_RIPAS_MASK	MASK(S2TTE_INVALID_RIPAS)
 
-#define S2TTE_INVALID_RIPAS_EMPTY	(INPLACE(S2TTE_INVALID_RIPAS, 0))
-#define S2TTE_INVALID_RIPAS_RAM		(INPLACE(S2TTE_INVALID_RIPAS, 1))
-#define S2TTE_INVALID_RIPAS_DESTROYED	(INPLACE(S2TTE_INVALID_RIPAS, 2))
+#define S2TTE_INVALID_RIPAS_EMPTY	(INPLACE(S2TTE_INVALID_RIPAS, 0UL))
+#define S2TTE_INVALID_RIPAS_RAM		(INPLACE(S2TTE_INVALID_RIPAS, 1UL))
+#define S2TTE_INVALID_RIPAS_DESTROYED	(INPLACE(S2TTE_INVALID_RIPAS, 2UL))
 
 #define S2TTE_INVALID_UNPROTECTED	0x0UL