Add helper functions for VM/vCPU IDs in SPCI calls.

This also changes all calls to encode VM IDs in high bits, vCPU index in
low bits. This is inconsistent with the current spec but is what we
expect it to be corrected to in the next draft.

Change-Id: I3da130b9070c6c43856509ba80b532e7e8c263e0
diff --git a/driver/linux b/driver/linux
index 39bf789..6aef655 160000
--- a/driver/linux
+++ b/driver/linux
@@ -1 +1 @@
-Subproject commit 39bf7898ab02d0cda2de33c03838b4ee131b4f65
+Subproject commit 6aef655db80434c776f49ade53d7bf8673b8109a
diff --git a/inc/vmapi/hf/abi.h b/inc/vmapi/hf/abi.h
index 9423442..bdb6705 100644
--- a/inc/vmapi/hf/abi.h
+++ b/inc/vmapi/hf/abi.h
@@ -62,13 +62,3 @@
 	 */
 	HF_MEMORY_SHARE,
 };
-
-static inline spci_vm_id_t wake_up_get_vm_id(struct spci_value v)
-{
-	return v.arg1 & 0xffff;
-}
-
-static inline spci_vcpu_index_t wake_up_get_vcpu(struct spci_value v)
-{
-	return (v.arg1 >> 16) & 0xffff;
-}
diff --git a/inc/vmapi/hf/call.h b/inc/vmapi/hf/call.h
index 94f70e8..d446754 100644
--- a/inc/vmapi/hf/call.h
+++ b/inc/vmapi/hf/call.h
@@ -66,7 +66,7 @@
 					 spci_vcpu_index_t vcpu_idx)
 {
 	return spci_call((struct spci_value){.func = SPCI_RUN_32,
-					     (uint32_t)vm_id << 16 | vcpu_idx});
+					     spci_vm_vcpu(vm_id, vcpu_idx)});
 }
 
 /**
diff --git a/inc/vmapi/hf/spci.h b/inc/vmapi/hf/spci.h
index d5d55ff..fe8e7df 100644
--- a/inc/vmapi/hf/spci.h
+++ b/inc/vmapi/hf/spci.h
@@ -207,6 +207,22 @@
 	return args.arg4;
 }
 
+static inline spci_vm_id_t spci_vm_id(struct spci_value args)
+{
+	return (args.arg1 >> 16) & 0xffff;
+}
+
+static inline spci_vcpu_index_t spci_vcpu_index(struct spci_value args)
+{
+	return args.arg1 & 0xffff;
+}
+
+static inline uint64_t spci_vm_vcpu(spci_vm_id_t vm_id,
+				    spci_vcpu_index_t vcpu_index)
+{
+	return ((uint32_t)vm_id << 16) | vcpu_index;
+}
+
 struct spci_architected_message_header {
 	uint16_t type;
 
diff --git a/src/api.c b/src/api.c
index 7a16436..d73afb2 100644
--- a/src/api.c
+++ b/src/api.c
@@ -125,7 +125,7 @@
 {
 	struct spci_value ret = {
 		.func = SPCI_INTERRUPT_32,
-		.arg1 = ((uint32_t)current->vm->id << 16) | vcpu_index(current),
+		.arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
 	};
 
 	return api_switch_to_primary(current, ret, VCPU_STATE_READY);
@@ -139,7 +139,7 @@
 {
 	struct spci_value ret = {
 		.func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
-		.arg1 = ((uint32_t)vcpu_index(current) << 16) | current->vm->id,
+		.arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
 	};
 
 	return api_switch_to_primary(current, ret,
@@ -153,7 +153,7 @@
 {
 	struct spci_value ret = {
 		.func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
-		.arg1 = ((uint32_t)vcpu_index(current) << 16) | current->vm->id,
+		.arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
 	};
 
 	/*
@@ -174,7 +174,7 @@
 {
 	struct spci_value primary_ret = {
 		.func = SPCI_YIELD_32,
-		.arg1 = ((uint32_t)vcpu_index(current) << 16) | current->vm->id,
+		.arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
 	};
 
 	if (current->vm->id == HF_PRIMARY_VM_ID) {
@@ -193,8 +193,8 @@
 {
 	struct spci_value ret = {
 		.func = HF_SPCI_RUN_WAKE_UP,
-		.arg1 = ((uint32_t)vcpu_index(target_vcpu) << 16) |
-			target_vcpu->vm->id,
+		.arg1 = spci_vm_vcpu(target_vcpu->vm->id,
+				     vcpu_index(target_vcpu)),
 	};
 	return api_switch_to_primary(current, ret, VCPU_STATE_READY);
 }
@@ -485,8 +485,8 @@
 				vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
 					? SPCI_MSG_WAIT_32
 					: HF_SPCI_RUN_WAIT_FOR_INTERRUPT;
-			run_ret->arg1 = ((uint32_t)vcpu_index(vcpu) << 16) |
-					vcpu->vm->id;
+			run_ret->arg1 =
+				spci_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
 			run_ret->arg2 = timer_remaining_ns;
 		}
 
@@ -583,7 +583,7 @@
 	 * overwritten when the switch back to the primary occurs.
 	 */
 	ret.func = SPCI_INTERRUPT_32;
-	ret.arg1 = ((uint32_t)vm_id << 16) | vcpu_idx;
+	ret.arg1 = spci_vm_vcpu(vm_id, vcpu_idx);
 	ret.arg2 = 0;
 
 out:
@@ -1134,7 +1134,7 @@
 	{
 		struct spci_value run_return = {
 			.func = SPCI_MSG_WAIT_32,
-			.arg1 = ((uint32_t)vcpu_index(current) << 16) | vm->id,
+			.arg1 = spci_vm_vcpu(vm->id, vcpu_index(current)),
 		};
 
 		*next = api_switch_to_primary(current, run_return,
diff --git a/src/arch/aarch64/hypervisor/handler.c b/src/arch/aarch64/hypervisor/handler.c
index f976eee..1125dcf 100644
--- a/src/arch/aarch64/hypervisor/handler.c
+++ b/src/arch/aarch64/hypervisor/handler.c
@@ -345,8 +345,8 @@
 		*args = api_spci_msg_recv(false, current(), next);
 		return true;
 	case SPCI_RUN_32:
-		*args = api_spci_run((args->arg1 >> 16) & 0xffff,
-				     args->arg1 & 0xffff, current(), next);
+		*args = api_spci_run(spci_vm_id(*args), spci_vcpu_index(*args),
+				     current(), next);
 		return true;
 	}
 
diff --git a/test/vmapi/primary_with_secondaries/smp.c b/test/vmapi/primary_with_secondaries/smp.c
index 5ffda15..e0f9c46 100644
--- a/test/vmapi/primary_with_secondaries/smp.c
+++ b/test/vmapi/primary_with_secondaries/smp.c
@@ -40,8 +40,8 @@
 	/* Let the first vCPU start the second vCPU. */
 	run_res = spci_run(SERVICE_VM2, 0);
 	EXPECT_EQ(run_res.func, HF_SPCI_RUN_WAKE_UP);
-	EXPECT_EQ(wake_up_get_vm_id(run_res), SERVICE_VM2);
-	EXPECT_EQ(wake_up_get_vcpu(run_res), 1);
+	EXPECT_EQ(spci_vm_id(run_res), SERVICE_VM2);
+	EXPECT_EQ(spci_vcpu_index(run_res), 1);
 
 	/* Run the second vCPU and wait for a message. */
 	dlog("Run second vCPU for message\n");