ANDROID: pkvm: Spawn protected guests with an 8MiB restricted DMA pool

Android kernels with pKVM support the creation of protected VMs using
bit 31 of the virtual machine type specifier. By default, the memory of
VMs spawned in this way is inaccessible to the host system.

Hack kvmtool to create protected VMs, request an 8MiB restricted DMA
pool for PCI devices so that virtio traffic can be bounced through a
shared memory window and advertise VIRTIO_F_ACCESS_PLATFORM for its PCI
devices.

Signed-off-by: Will Deacon <willdeacon@google.com>
diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c
index 54200c9..8641989 100644
--- a/arm/aarch64/kvm.c
+++ b/arm/aarch64/kvm.c
@@ -131,7 +131,7 @@
 	if (ipa_bits > max_ipa_bits)
 		die("Memory too large for this system (needs %d bits, %d available)", ipa_bits, max_ipa_bits);
 
-	return KVM_VM_TYPE_ARM_IPA_SIZE(ipa_bits);
+	return KVM_VM_TYPE_ARM_IPA_SIZE(ipa_bits) | (1U << 31);
 }
 
 void kvm__arch_enable_mte(struct kvm *kvm)
diff --git a/arm/fdt.c b/arm/fdt.c
index 286ccad..6dca02c 100644
--- a/arm/fdt.c
+++ b/arm/fdt.c
@@ -116,6 +116,7 @@
 					void (*)(void *, u8, enum irq_type));
 	void (*generate_cpu_peripheral_fdt_nodes)(void *, struct kvm *)
 					= kvm->cpus[0]->generate_fdt_nodes;
+	u64 resv_mem_prop;
 
 	/* Create new tree without a reserve map */
 	_FDT(fdt_create(fdt, FDT_MAX_SIZE));
@@ -163,6 +164,21 @@
 	_FDT(fdt_property(fdt, "reg", mem_reg_prop, sizeof(mem_reg_prop)));
 	_FDT(fdt_end_node(fdt));
 
+	/* Reserved memory (restricted DMA) */
+	_FDT(fdt_begin_node(fdt, "reserved-memory"));
+	_FDT(fdt_property_cell(fdt, "#address-cells", 0x2));
+	_FDT(fdt_property_cell(fdt, "#size-cells", 0x2));
+	_FDT(fdt_property(fdt, "ranges", NULL, 0));
+
+	_FDT(fdt_begin_node(fdt, "restricted_dma_reserved"));
+	_FDT(fdt_property_string(fdt, "compatible", "restricted-dma-pool"));
+	resv_mem_prop = cpu_to_fdt64(SZ_8M);
+	_FDT(fdt_property(fdt, "size", &resv_mem_prop, sizeof(resv_mem_prop)));
+	_FDT(fdt_property_cell(fdt, "phandle", PHANDLE_DMA));
+	_FDT(fdt_end_node(fdt));
+
+	_FDT(fdt_end_node(fdt));
+
 	/* CPU and peripherals (interrupt controller, timers, etc) */
 	generate_cpu_nodes(fdt, kvm);
 	if (generate_cpu_peripheral_fdt_nodes)
diff --git a/arm/include/arm-common/fdt-arch.h b/arm/include/arm-common/fdt-arch.h
index 60c2d40..81df744 100644
--- a/arm/include/arm-common/fdt-arch.h
+++ b/arm/include/arm-common/fdt-arch.h
@@ -1,6 +1,6 @@
 #ifndef ARM__FDT_H
 #define ARM__FDT_H
 
-enum phandles {PHANDLE_RESERVED = 0, PHANDLE_GIC, PHANDLE_MSI, PHANDLES_MAX};
+enum phandles {PHANDLE_RESERVED = 0, PHANDLE_GIC, PHANDLE_MSI, PHANDLE_DMA, PHANDLES_MAX};
 
 #endif /* ARM__FDT_H */
diff --git a/arm/pci.c b/arm/pci.c
index 5bd82d4..9f28d75 100644
--- a/arm/pci.c
+++ b/arm/pci.c
@@ -74,6 +74,8 @@
 	if (irqchip == IRQCHIP_GICV2M || irqchip == IRQCHIP_GICV3_ITS)
 		_FDT(fdt_property_cell(fdt, "msi-parent", PHANDLE_MSI));
 
+	_FDT(fdt_property_cell(fdt, "memory-region", PHANDLE_DMA));
+
 	/* Generate the interrupt map ... */
 	dev_hdr = device__first_dev(DEVICE_BUS_PCI);
 	while (dev_hdr && nentries < ARRAY_SIZE(irq_map)) {
diff --git a/virtio/pci-modern.c b/virtio/pci-modern.c
index c5b4bc5..777676a 100644
--- a/virtio/pci-modern.c
+++ b/virtio/pci-modern.c
@@ -148,7 +148,8 @@
 {
 	u32 val;
 	struct virtio_pci *vpci = vdev->virtio;
-	u64 features = 1ULL << VIRTIO_F_VERSION_1;
+	u64 features = 1ULL << VIRTIO_F_VERSION_1 |
+		       1ULL << VIRTIO_F_ACCESS_PLATFORM;
 
 	switch (offset - VPCI_CFG_COMMON_START) {
 	case VIRTIO_PCI_COMMON_DFSELECT: