aarch64: define FF-A world IDs ranges

Define FF-A ID ranges such that IDs:
-from 1 to 0x7fff are reserved to NWd VMs.
-from 0x8001 to 0xfffe are reserved to SWd Secure Partitions.

IDs 0 and 0x8000 are reserved respectively to the Hypervisor
and the SPMC. ID 0xffff is provisioned for the SPMD.

Change-Id: I42137ccc5ff9b3e26737618af2d9f2be146a3421
Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 5bcfa25..f8d0008 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -23,6 +23,7 @@
 # Hypervisor specific code that isn't. One day it will be testable and both the
 # src targets will merge!
 source_set("src_not_testable_yet") {
+  public_configs = [ "//src/arch/${plat_arch}:arch_config" ]
   sources = [
     "cpio.c",
     "init.c",
@@ -44,6 +45,7 @@
 # e.g. with VM used in the VM tests have their own targets to facilitate
 # sharing.
 source_set("src_testable") {
+  public_configs = [ "//src/arch/${plat_arch}:arch_config" ]
   sources = [
     "api.c",
     "cpu.c",
@@ -85,6 +87,7 @@
 }
 
 source_set("vm") {
+  public_configs = [ "//src/arch/${plat_arch}:arch_config" ]
   sources = [
     "vm.c",
   ]
diff --git a/src/arch/aarch64/BUILD.gn b/src/arch/aarch64/BUILD.gn
index 527a415..8eb04f4 100644
--- a/src/arch/aarch64/BUILD.gn
+++ b/src/arch/aarch64/BUILD.gn
@@ -8,7 +8,9 @@
 
 config("config") {
   include_dirs = [ "." ]
+}
 
+config("arch_config") {
   assert(secure_world == "0" || secure_world == "1",
          "secure world set to <${secure_world}>")
   defines = [ "SECURE_WORLD=${secure_world}" ]
@@ -16,7 +18,7 @@
 
 # Implementation of the arch interface for aarch64.
 source_set("arch") {
-  public_configs = [ "//src/arch/aarch64:config" ]
+  public_configs = [ "//src/arch/aarch64:arch_config" ]
   sources = [
     "irq.c",
     "mm.c",
diff --git a/src/arch/aarch64/hypervisor/BUILD.gn b/src/arch/aarch64/hypervisor/BUILD.gn
index 729614f..3a5f3d5 100644
--- a/src/arch/aarch64/hypervisor/BUILD.gn
+++ b/src/arch/aarch64/hypervisor/BUILD.gn
@@ -26,7 +26,10 @@
 
 # Hypervisor specific code.
 source_set("hypervisor") {
-  public_configs = [ "//src/arch/aarch64:config" ]
+  public_configs = [
+    "//src/arch/aarch64:config",
+    "//src/arch/aarch64:arch_config",
+  ]
   sources = [
     "exceptions.S",
     "hypervisor_entry.S",
diff --git a/src/arch/aarch64/inc/hf/arch/vmid_base.h b/src/arch/aarch64/inc/hf/arch/vmid_base.h
new file mode 100644
index 0000000..fcc2271
--- /dev/null
+++ b/src/arch/aarch64/inc/hf/arch/vmid_base.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright 2020 The Hafnium Authors.
+ *
+ * Use of this source code is governed by a BSD-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/BSD-3-Clause.
+ */
+
+#pragma once
+
+#if SECURE_WORLD == 1
+#define HF_VM_ID_BASE 0x8000
+#define HF_OTHER_WORLD_ID HF_HYPERVISOR_VM_ID
+#else
+#define HF_VM_ID_BASE 0
+#define HF_OTHER_WORLD_ID HF_TEE_VM_ID
+#endif
diff --git a/src/arch/fake/BUILD.gn b/src/arch/fake/BUILD.gn
index 7fd4170..cbdecaa 100644
--- a/src/arch/fake/BUILD.gn
+++ b/src/arch/fake/BUILD.gn
@@ -4,6 +4,9 @@
 # license that can be found in the LICENSE file or at
 # https://opensource.org/licenses/BSD-3-Clause.
 
+config("arch_config") {
+}
+
 source_set("fake") {
   sources = [
     "mm.c",
diff --git a/src/arch/fake/inc/hf/arch/vmid_base.h b/src/arch/fake/inc/hf/arch/vmid_base.h
new file mode 100644
index 0000000..4652391
--- /dev/null
+++ b/src/arch/fake/inc/hf/arch/vmid_base.h
@@ -0,0 +1,12 @@
+/*
+ * Copyright 2020 The Hafnium Authors.
+ *
+ * Use of this source code is governed by a BSD-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/BSD-3-Clause.
+ */
+
+#pragma once
+
+#define HF_VM_ID_BASE 0
+#define HF_OTHER_WORLD_ID HF_TEE_VM_ID
diff --git a/src/dlog.c b/src/dlog.c
index 104eedb..c0e4eda 100644
--- a/src/dlog.c
+++ b/src/dlog.c
@@ -226,7 +226,7 @@
 	lock();
 
 	print_raw_string("VM ");
-	print_num(id, 10, 0, 0);
+	print_num(id, 16, 0, 0);
 	print_raw_string(": ");
 
 	for (size_t i = 0; i < length; ++i) {
diff --git a/src/load.c b/src/load.c
index 26a9641..481c1cd 100644
--- a/src/load.c
+++ b/src/load.c
@@ -666,7 +666,7 @@
 			continue;
 		}
 
-		dlog_info("Loading VM%d: %s.\n", (int)vm_id,
+		dlog_info("Loading VM id %#x: %s.\n", vm_id,
 			  manifest_vm->debug_name);
 
 		mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
diff --git a/src/manifest.c b/src/manifest.c
index da25156..d6dfac8 100644
--- a/src/manifest.c
+++ b/src/manifest.c
@@ -30,7 +30,8 @@
 static_assert(VM_NAME_MAX_SIZE <= STRING_MAX_SIZE,
 	      "VM name does not fit into a struct string.");
 static_assert(VM_ID_MAX <= 99999, "Insufficient VM_NAME_BUF_SIZE");
-static_assert(HF_TEE_VM_ID > VM_ID_MAX,
+static_assert((HF_OTHER_WORLD_ID > VM_ID_MAX) ||
+		      (HF_OTHER_WORLD_ID < HF_VM_ID_BASE),
 	      "TrustZone VM ID clashes with normal VM range.");
 
 static inline size_t count_digits(ffa_vm_id_t vm_id)
@@ -740,8 +741,8 @@
 	TRY(read_bool(&hyp_node, "ffa_tee", &manifest->ffa_tee_enabled));
 
 	/* Iterate over reserved VM IDs and check no such nodes exist. */
-	for (i = 0; i < HF_VM_ID_OFFSET; i++) {
-		ffa_vm_id_t vm_id = (ffa_vm_id_t)i;
+	for (i = HF_VM_ID_BASE; i < HF_VM_ID_OFFSET; i++) {
+		ffa_vm_id_t vm_id = (ffa_vm_id_t)i - HF_VM_ID_BASE;
 		struct fdt_node vm_node = hyp_node;
 
 		generate_vm_node_name(&vm_name, vm_id);
@@ -755,7 +756,7 @@
 		ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
 		struct fdt_node vm_node = hyp_node;
 
-		generate_vm_node_name(&vm_name, vm_id);
+		generate_vm_node_name(&vm_name, vm_id - HF_VM_ID_BASE);
 		if (!fdt_find_child(&vm_node, &vm_name)) {
 			break;
 		}