squashme: Cleanup

Change-Id: Ia3dc8b286895e566c71c545fb81d95c3ebacddee
diff --git a/include/kvm/vfio.h b/include/kvm/vfio.h
index 9556233..6ece040 100644
--- a/include/kvm/vfio.h
+++ b/include/kvm/vfio.h
@@ -116,6 +116,9 @@
 	 * PCI for now.
 	 */
 	struct vfio_platform_device	platform;
+	u64                             iommu;
+	u32                             *sid;
+	int                             nr_sid;
 };
 
 struct vfio_device_params {
diff --git a/vfio/core.c b/vfio/core.c
index f722168..b93fbab 100644
--- a/vfio/core.c
+++ b/vfio/core.c
@@ -738,6 +738,9 @@
 			return ret;
 		}
 		pr_info("nr_sids = %d\n", info.out_nr_sids);
+		vfio_devices[i].iommu = pviommufd;
+		vfio_devices[i].sid = malloc(info.out_nr_sids * sizeof(vfio_devices[i].sid));
+		vfio_devices[i].nr_sid = info.out_nr_sids;
 		/* Set vSID => SID translation. */
 		for (j = 0; j < info.out_nr_sids; ++j) {
 			struct kvm_vfio_iommu_config config = {
@@ -753,6 +756,7 @@
 				pr_err("Failed to set vsid(%d) => sid(%d) translation, err %d\n", config.vsid, j, ret);
 				return ret;
 			}
+			vfio_devices[i].sid[j] = config.vsid;
 		}
 	}
 
@@ -797,8 +801,11 @@
 	if (!kvm->cfg.num_vfio_devices)
 		return 0;
 
-	for (i = 0; i < kvm->cfg.num_vfio_devices; i++)
+	for (i = 0; i < kvm->cfg.num_vfio_devices; i++) {
+		if (vfio_devices[i].nr_sid)
+			free(vfio_devices[i].sid);
 		vfio_device_exit(kvm, &vfio_devices[i]);
+	}
 
 	free(vfio_devices);
 
diff --git a/vfio/platform.c b/vfio/platform.c
index 409788b..047a699 100644
--- a/vfio/platform.c
+++ b/vfio/platform.c
@@ -25,10 +25,10 @@
 	return block;
 }
 
-static void generate_qemu_iommu(void *fdt) {
+static void generate_qemu_iommu(void *fdt, u64 id) {
 	_FDT(fdt_begin_node(fdt, "pviommu"));
 	_FDT(fdt_property_string(fdt, "compatible", "pkvm,pviommu"));
-	_FDT(fdt_property_cell(fdt, "id", 0));
+	_FDT(fdt_property_cell(fdt, "id", id));
 	_FDT(fdt_property_cell(fdt, "phandle", 0x9000));
 	_FDT(fdt_property_cell(fdt, "#iommu-cells", 1));
 	_FDT(fdt_end_node(fdt));
@@ -51,13 +51,13 @@
 	struct vfio_platform_device *pdev = &vdev->platform;
 	u32 iommu[] = {
 		cpu_to_fdt32(0x9000),	/*phandle of pvIOMMU */
-		cpu_to_fdt32(1),		/* SID */
+		cpu_to_fdt32(vdev->sid[0]),		/* SID */
 	};
 
 	u64 reg_prop[10]; /* Max 5 for now. */
 	unsigned int i = 0;
 
-	generate_qemu_iommu(fdt);
+	generate_qemu_iommu(fdt, vdev->iommu);
 
 	for ( ; i < vdev->info.num_regions ; i++) {
 		reg_prop[2 * i] = cpu_to_fdt64(vdev->regions[i].guest_phys_addr); /* Addr */