refactor(notifications): functions to get notifications

Refactored helper functions for getting notifications from the VM
structure, to prepare for adding framework notifications.

Change-Id: Id90140f99e9bed045b76f842e1e2a1397c0a5847
Signed-off-by: J-Alves <joao.alves@arm.com>
diff --git a/src/api.c b/src/api.c
index e560f8b..2b0a352 100644
--- a/src/api.c
+++ b/src/api.c
@@ -3140,7 +3140,7 @@
 	}
 
 	if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
-		vm_notifications = vm_notifications_get_pending_and_clear(
+		vm_notifications = vm_notifications_partition_get_pending(
 			receiver_locked, true, vcpu_id);
 	}
 
diff --git a/src/arch/aarch64/plat/ffa/spmc.c b/src/arch/aarch64/plat/ffa/spmc.c
index 69dc645..e172e7e 100644
--- a/src/arch/aarch64/plat/ffa/spmc.c
+++ b/src/arch/aarch64/plat/ffa/spmc.c
@@ -642,7 +642,7 @@
 {
 	(void)ret;
 
-	*from_sp = vm_notifications_get_pending_and_clear(receiver_locked,
+	*from_sp = vm_notifications_partition_get_pending(receiver_locked,
 							  false, vcpu_id);
 
 	return true;
diff --git a/src/vm.c b/src/vm.c
index 37a4541..0b3c124 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -702,60 +702,53 @@
 	vm_notifications_pending_count_add(notifications);
 }
 
-/**
- * Get Global notifications and per CPU only of the current VCPU.
- */
-ffa_notifications_bitmap_t vm_notifications_get_pending_and_clear(
-	struct vm_locked vm_locked, bool is_from_vm,
-	ffa_vcpu_index_t cur_vcpu_id)
+static ffa_notifications_bitmap_t vm_notifications_state_get_pending(
+	struct notifications_state *state)
 {
-	ffa_notifications_bitmap_t to_ret = 0;
+	ffa_notifications_bitmap_t to_ret;
 	ffa_notifications_bitmap_t pending_and_info_get_retrieved;
 
-	CHECK(vm_locked.vm != NULL);
-	struct notifications *to_get =
-		vm_get_notifications(vm_locked, is_from_vm);
-	CHECK(cur_vcpu_id < MAX_CPUS);
+	assert(state != NULL);
 
-	to_ret |= to_get->global.pending;
+	to_ret = state->pending;
 
 	/* Update count of currently pending notifications in the system. */
-	vm_notifications_pending_count_sub(to_get->global.pending);
+	vm_notifications_pending_count_sub(state->pending);
 
 	/*
 	 * If notifications receiver is getting have been retrieved by the
 	 * receiver scheduler, decrement those from respective count.
 	 */
 	pending_and_info_get_retrieved =
-		to_get->global.pending & to_get->global.info_get_retrieved;
+		state->pending & state->info_get_retrieved;
 
 	if (pending_and_info_get_retrieved != 0) {
 		vm_notifications_info_get_retrieved_count_sub(
 			pending_and_info_get_retrieved);
 	}
 
-	to_get->global.pending = 0U;
-	to_get->global.info_get_retrieved = 0U;
+	state->pending = 0U;
+	state->info_get_retrieved = 0U;
 
-	to_ret |= to_get->per_vcpu[cur_vcpu_id].pending;
+	return to_ret;
+}
 
-	/*
-	 * Update counts of notifications, this time for per-vCPU notifications.
-	 */
-	vm_notifications_pending_count_sub(
-		to_get->per_vcpu[cur_vcpu_id].pending);
+/**
+ * Get global and per-vCPU notifications for the given vCPU ID.
+ */
+ffa_notifications_bitmap_t vm_notifications_partition_get_pending(
+	struct vm_locked vm_locked, bool is_from_vm, ffa_vcpu_index_t vcpu_id)
+{
+	ffa_notifications_bitmap_t to_ret;
+	struct notifications *to_get;
 
-	pending_and_info_get_retrieved =
-		to_get->per_vcpu[cur_vcpu_id].pending &
-		to_get->per_vcpu[cur_vcpu_id].info_get_retrieved;
+	assert(vm_locked.vm != NULL);
+	to_get = vm_get_notifications(vm_locked, is_from_vm);
+	assert(vcpu_id < MAX_CPUS);
 
-	if (pending_and_info_get_retrieved != 0) {
-		vm_notifications_info_get_retrieved_count_sub(
-			pending_and_info_get_retrieved);
-	}
-
-	to_get->per_vcpu[cur_vcpu_id].pending = 0U;
-	to_get->per_vcpu[cur_vcpu_id].info_get_retrieved = 0U;
+	to_ret = vm_notifications_state_get_pending(&to_get->global);
+	to_ret |=
+		vm_notifications_state_get_pending(&to_get->per_vcpu[vcpu_id]);
 
 	return to_ret;
 }
diff --git a/src/vm_test.cc b/src/vm_test.cc
index d64b7d1..0ffacb9 100644
--- a/src/vm_test.cc
+++ b/src/vm_test.cc
@@ -308,7 +308,7 @@
 	vm_notifications_set(current_vm_locked, is_from_vm, global, 0ull,
 			     false);
 
-	ret = vm_notifications_get_pending_and_clear(current_vm_locked,
+	ret = vm_notifications_partition_get_pending(current_vm_locked,
 						     is_from_vm, 0ull);
 	EXPECT_EQ(ret, global);
 	EXPECT_EQ(notifications->global.pending, 0ull);
@@ -319,7 +319,7 @@
 	vm_notifications_set(current_vm_locked, is_from_vm, per_vcpu, vcpu_idx,
 			     true);
 
-	ret = vm_notifications_get_pending_and_clear(current_vm_locked,
+	ret = vm_notifications_partition_get_pending(current_vm_locked,
 						     is_from_vm, vcpu_idx);
 	EXPECT_EQ(ret, per_vcpu);
 	EXPECT_EQ(notifications->per_vcpu[vcpu_idx].pending, 0ull);
@@ -333,7 +333,7 @@
 	vm_notifications_set(current_vm_locked, is_from_vm, global, 0ull,
 			     false);
 
-	ret = vm_notifications_get_pending_and_clear(current_vm_locked,
+	ret = vm_notifications_partition_get_pending(current_vm_locked,
 						     is_from_vm, vcpu_idx);
 	EXPECT_EQ(ret, per_vcpu | global);
 	EXPECT_EQ(notifications->per_vcpu[vcpu_idx].pending, 0ull);
@@ -400,7 +400,7 @@
 		 * return and cleans the 'pending' and 'info_get_retrieved'
 		 * bitmaps.
 		 */
-		got = vm_notifications_get_pending_and_clear(current_vm_locked,
+		got = vm_notifications_partition_get_pending(current_vm_locked,
 							     is_from_vm, 0);
 		EXPECT_EQ(got, to_set);
 
@@ -465,7 +465,7 @@
 		 * return and cleans the 'pending' and 'info_get_retrieved'
 		 * bitmaps.
 		 */
-		got = vm_notifications_get_pending_and_clear(current_vm_locked,
+		got = vm_notifications_partition_get_pending(current_vm_locked,
 							     is_from_vm, 0);
 		EXPECT_EQ(got, per_vcpu);
 
@@ -534,12 +534,13 @@
 	EXPECT_EQ(lists_sizes[1], 1);
 
 	for (unsigned int i = 0; i < vcpu_count; i++) {
-		got = vm_notifications_get_pending_and_clear(current_vm_locked,
+		got = vm_notifications_partition_get_pending(current_vm_locked,
 							     is_from_sp, i);
 
 		/*
-		 * The first call to vm_notifications_get_pending_and_clear
-		 * should also include the global notifications on the return.
+		 * The first call to
+		 * vm_notifications_partition_get_pending should also
+		 * include the global notifications on the return.
 		 */
 		ffa_notifications_bitmap_t to_check =
 			(i != 0) ? FFA_NOTIFICATION_MASK(i)
@@ -592,10 +593,10 @@
 
 	/*
 	 * Verify that as soon as there isn't space to do the required
-	 * insertion in the list, the 'vm_notifications_get_pending_and_clear'
-	 * returns and changes list state to FULL.
-	 * In this case returning, because it would need to add two IDs (VM ID
-	 * and VCPU ID).
+	 * insertion in the list, the
+	 * 'vm_notifications_partition_get_pending' returns and changes
+	 * list state to FULL. In this case returning, because it would need to
+	 * add two IDs (VM ID and VCPU ID).
 	 */
 	EXPECT_EQ(current_state, FULL);
 	EXPECT_EQ(ids_count, FFA_NOTIFICATIONS_INFO_GET_MAX_IDS - 1);
@@ -627,7 +628,7 @@
 	EXPECT_EQ(notifications->global.info_get_retrieved,
 		  FFA_NOTIFICATION_MASK(2));
 
-	got = vm_notifications_get_pending_and_clear(current_vm_locked,
+	got = vm_notifications_partition_get_pending(current_vm_locked,
 						     is_from_vm, 0);
 	EXPECT_EQ(got, FFA_NOTIFICATION_MASK(1) | FFA_NOTIFICATION_MASK(2));
 
@@ -675,7 +676,7 @@
 	EXPECT_EQ(ids_count, FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
 	EXPECT_EQ(current_state, FULL);
 
-	got = vm_notifications_get_pending_and_clear(current_vm_locked,
+	got = vm_notifications_partition_get_pending(current_vm_locked,
 						     is_from_vm, 0);
 	EXPECT_EQ(got, FFA_NOTIFICATION_MASK(10));