VHE: Update vm helper functions to account for EL0 partitions

Updated the VM helper functions that init and map memory into the VM to
account for EL0 partitions. When a partition is an el0 partition, we use
the appropriate stage 1 mapping API's to ensure they are mapped as stage
1 tables and entries.
Note that it is weird to call vm helper functions for an EL0 entity but
a EL0 partition is being modeled as a lightweight VM to promote
reusability of code and not have to create a parallel universe of
helpers, functions and management.

Change-Id: Iafb4fdd3e921edff4d523c9a9fb159ddb9f4bb7f
Signed-off-by: Raghu Krishnamurthy <raghu.ncstate@gmail.com>
diff --git a/src/vm.c b/src/vm.c
index a3ecddd..1c67166 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -23,6 +23,15 @@
 static ffa_vm_count_t vm_count;
 static struct vm *first_boot_vm;
 
+static bool vm_init_mm(struct vm *vm, struct mpool *ppool)
+{
+	if (vm->el0_partition) {
+		return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1,
+				      ppool);
+	}
+	return mm_vm_init(&vm->ptable, vm->id, ppool);
+}
+
 struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
 		   struct mpool *ppool, bool el0_partition)
 {
@@ -52,7 +61,7 @@
 	atomic_init(&vm->aborting, false);
 	vm->el0_partition = el0_partition;
 
-	if (!mm_vm_init(&vm->ptable, id, ppool)) {
+	if (!vm_init_mm(vm, ppool)) {
 		return NULL;
 	}
 
@@ -252,6 +261,10 @@
 bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
 			 uint32_t mode, struct mpool *ppool)
 {
+	if (vm_locked.vm->el0_partition) {
+		return mm_identity_prepare(&vm_locked.vm->ptable, begin, end,
+					   mode, ppool);
+	}
 	return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
 				      ppool);
 }
@@ -264,8 +277,22 @@
 void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
 			uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
 {
-	mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode, ppool,
-			      ipa);
+	if (vm_locked.vm->el0_partition) {
+		mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
+				   ppool);
+		if (ipa != NULL) {
+			/*
+			 * EL0 partitions are modeled as lightweight VM's, to
+			 * promote code reuse. The below statement returns the
+			 * mapped PA as an IPA, however, for an EL0 partition,
+			 * this is really a VA.
+			 */
+			*ipa = ipa_from_pa(begin);
+		}
+	} else {
+		mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
+				      ppool, ipa);
+	}
 	plat_iommu_identity_map(vm_locked, begin, end, mode);
 }