refactor(interrupts): make enable bitmap funcs private

To ensure that the interrupt count is kept in sync when enabling and
disabling secure interrupts make the functions that manipulate the
interrupt_enabled bitmap private.

This requires modifying the unit tests to use the
vcpu_virt_interrupt_enable function so use this opportunity to add a
TearDown function for the vCPU unit tests to make things cleaner.

Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: Ie707547677199837df023e964c751ffe93fdac9b
diff --git a/src/ffa/hypervisor/interrupts.c b/src/ffa/hypervisor/interrupts.c
index d056382..1877983 100644
--- a/src/ffa/hypervisor/interrupts.c
+++ b/src/ffa/hypervisor/interrupts.c
@@ -38,15 +38,9 @@
 void ffa_interrupts_enable_virtual_interrupts(struct vcpu_locked current_locked,
 					      struct vm_locked vm_locked)
 {
-	struct vcpu *current;
-	struct interrupts *interrupts;
-
-	current = current_locked.vcpu;
-	interrupts = &current->interrupts;
-
 	if (vm_locked.vm->notifications.enabled) {
-		vcpu_virt_interrupt_set_enabled(interrupts,
-						HF_NOTIFICATION_PENDING_INTID);
+		vcpu_virt_interrupt_enable(current_locked,
+					   HF_NOTIFICATION_PENDING_INTID, true);
 	}
 }
 
diff --git a/src/ffa/spmc/interrupts.c b/src/ffa/spmc/interrupts.c
index 434950c..4a033e1 100644
--- a/src/ffa/spmc/interrupts.c
+++ b/src/ffa/spmc/interrupts.c
@@ -563,8 +563,8 @@
 	vm = current->vm;
 
 	if (ffa_vm_managed_exit_supported(vm)) {
-		vcpu_virt_interrupt_set_enabled(interrupts,
-						HF_MANAGED_EXIT_INTID);
+		vcpu_virt_interrupt_enable(current_locked,
+					   HF_MANAGED_EXIT_INTID, true);
 		/*
 		 * SPMC decides the interrupt type for Managed exit signal based
 		 * on the partition manifest.
@@ -581,8 +581,8 @@
 	}
 
 	if (vm->notifications.enabled) {
-		vcpu_virt_interrupt_set_enabled(interrupts,
-						HF_NOTIFICATION_PENDING_INTID);
+		vcpu_virt_interrupt_enable(current_locked,
+					   HF_NOTIFICATION_PENDING_INTID, true);
 	}
 }
 
@@ -598,11 +598,9 @@
 					      struct vm_locked vm_locked)
 {
 	struct vcpu *current;
-	struct interrupts *interrupts;
 	struct vm *vm;
 
 	current = current_locked.vcpu;
-	interrupts = &current->interrupts;
 	vm = current->vm;
 	assert(vm == vm_locked.vm);
 
@@ -616,8 +614,8 @@
 			if (!int_desc.valid) {
 				break;
 			}
-			vcpu_virt_interrupt_set_enabled(interrupts,
-							int_desc.interrupt_id);
+			vcpu_virt_interrupt_enable(current_locked,
+						   int_desc.interrupt_id, true);
 		}
 	}
 
diff --git a/src/ipi_test.cc b/src/ipi_test.cc
index 798e4a3..984c28b 100644
--- a/src/ipi_test.cc
+++ b/src/ipi_test.cc
@@ -58,26 +58,43 @@
 			struct vcpu *blocked_vcpu = vm_get_vcpu(test_vm[2], i);
 			struct vcpu *preempted_vcpu =
 				vm_get_vcpu(test_vm[3], i);
+			struct vcpu_locked running_locked =
+				vcpu_lock(running_vcpu);
+			struct vcpu_locked waiting_locked =
+				vcpu_lock(waiting_vcpu);
+			struct vcpu_locked blocked_locked =
+				vcpu_lock(blocked_vcpu);
+			struct vcpu_locked preempted_locked =
+				vcpu_lock(preempted_vcpu);
+
 			struct cpu *cpu = cpu_find_index(i);
 
 			running_vcpu->cpu = cpu;
 			running_vcpu->state = VCPU_STATE_RUNNING;
-			vcpu_virt_interrupt_set_enabled(
-				&running_vcpu->interrupts, HF_IPI_INTID);
+			vcpu_virt_interrupt_enable(running_locked, HF_IPI_INTID,
+						   true);
+
 			waiting_vcpu->cpu = cpu;
 			waiting_vcpu->state = VCPU_STATE_WAITING;
-			vcpu_virt_interrupt_set_enabled(
-				&waiting_vcpu->interrupts, HF_IPI_INTID);
+			vcpu_virt_interrupt_enable(waiting_locked, HF_IPI_INTID,
+						   true);
+
 			blocked_vcpu->cpu = cpu;
 			blocked_vcpu->state = VCPU_STATE_BLOCKED;
-			vcpu_virt_interrupt_set_enabled(
-				&blocked_vcpu->interrupts, HF_IPI_INTID);
+			vcpu_virt_interrupt_enable(blocked_locked, HF_IPI_INTID,
+						   true);
+
 			preempted_vcpu->cpu = cpu;
 			preempted_vcpu->state = VCPU_STATE_PREEMPTED;
-			vcpu_virt_interrupt_set_enabled(
-				&preempted_vcpu->interrupts, HF_IPI_INTID);
+			vcpu_virt_interrupt_enable(preempted_locked,
+						   HF_IPI_INTID, true);
 
 			list_init(&cpu->pending_ipis);
+
+			vcpu_unlock(&running_locked);
+			vcpu_unlock(&waiting_locked);
+			vcpu_unlock(&blocked_locked);
+			vcpu_unlock(&preempted_locked);
 		}
 	}
 };
diff --git a/src/vcpu.c b/src/vcpu.c
index bf07bd3..e45e3de 100644
--- a/src/vcpu.c
+++ b/src/vcpu.c
@@ -219,6 +219,25 @@
 	}
 }
 
+static bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts,
+					   uint32_t intid)
+{
+	return interrupt_bitmap_get_value(&interrupts->interrupt_enabled,
+					  intid) == 1U;
+}
+
+static void vcpu_virt_interrupt_set_enabled(struct interrupts *interrupts,
+					    uint32_t intid)
+{
+	interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid);
+}
+
+static void vcpu_virt_interrupt_clear_enabled(struct interrupts *interrupts,
+					      uint32_t intid)
+{
+	interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid);
+}
+
 static void vcpu_virt_interrupt_set_pending(struct interrupts *interrupts,
 					    uint32_t intid)
 {
diff --git a/src/vcpu_test.cc b/src/vcpu_test.cc
index e1244d0..77eed18 100644
--- a/src/vcpu_test.cc
+++ b/src/vcpu_test.cc
@@ -39,23 +39,31 @@
 	const uint32_t second_intid = HF_NUM_INTIDS - 1;
 	struct_vm *test_vm;
 	struct_vcpu *test_vcpu;
+	struct vcpu_locked vcpu_locked;
 	struct interrupts *interrupts;
 
 	void SetUp() override
 	{
-		if (test_heap) {
-			return;
+		if (!test_heap) {
+			test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
+			mpool_init(&ppool, sizeof(struct mm_page_table));
+			mpool_add_chunk(&ppool, test_heap.get(),
+					TEST_HEAP_SIZE);
 		}
-		test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
-		mpool_init(&ppool, sizeof(struct mm_page_table));
-		mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE);
+
 		test_vm = vm_init(HF_VM_ID_OFFSET, 1, &ppool, false, 0);
 		test_vcpu = vm_get_vcpu(test_vm, 0);
+		vcpu_locked = vcpu_lock(test_vcpu);
 		interrupts = &test_vcpu->interrupts;
 
 		/* Enable the interrupts used in testing. */
-		vcpu_virt_interrupt_set_enabled(interrupts, first_intid);
-		vcpu_virt_interrupt_set_enabled(interrupts, second_intid);
+		vcpu_virt_interrupt_enable(vcpu_locked, first_intid, true);
+		vcpu_virt_interrupt_enable(vcpu_locked, second_intid, true);
+	}
+
+	void TearDown() override
+	{
+		vcpu_unlock(&vcpu_locked);
 	}
 };
 
@@ -67,8 +75,6 @@
  */
 TEST_F(vcpu, pending_interrupts_are_fetched)
 {
-	struct vcpu_locked vcpu_locked = vcpu_lock(test_vcpu);
-
 	EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
 
 	/* Pend the interrupts, and check the count is incremented. */
@@ -117,8 +123,6 @@
 	EXPECT_FALSE(vcpu_is_virt_interrupt_pending(interrupts, second_intid));
 	EXPECT_FALSE(vcpu_is_virt_interrupt_pending(interrupts, first_intid));
 	EXPECT_EQ(vcpu_virt_interrupt_irq_count_get(vcpu_locked), 0);
-
-	vcpu_unlock(&vcpu_locked);
 }
 
 /*
@@ -127,8 +131,6 @@
  */
 TEST_F(vcpu, pending_interrupts_not_enabled_are_not_returned)
 {
-	struct vcpu_locked vcpu_locked = vcpu_lock(test_vcpu);
-
 	/*
 	 * Pend the interrupts, check the count is incremented, the pending
 	 * interrupts are returned correctly and this causes the count to
@@ -198,7 +200,5 @@
 	EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
 		  HF_INVALID_INTID);
 	EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
-
-	vcpu_unlock(&vcpu_locked);
 }
 } /* namespace */