diff mbox

[16/23] KVM: PPC: Book3S: Merge implementations of KVM_PPC_GET_SMMU_INFO ioctl

Message ID 20130806042435.GV19254@iris.ozlabs.ibm.com (mailing list archive)
State New, archived
Headers show

Commit Message

Paul Mackerras Aug. 6, 2013, 4:24 a.m. UTC
This merges the PR and HV implementations of kvm_vm_ioctl_get_smmu_info()
into a single implementation in book3s.c.  Since userspace tends to
call this ioctl very early in the life of a VM, before (for instance)
enabling PAPR mode, we will need this to return results that are
compatible with both PR and HV guests, once we are able to compile both
PR and HV into one kernel image.  For HV guests, the capabilities and
encodings need to be consistent with what the real hardware we are
running on can do, whereas for PR guests, the MMU is completely
virtual and so the set of capabilities and encodings is arbitrary.

To achieve this, we report a set of segment and page sizes and
encodings that are consistent with what real POWER processors do.
If the guest could potentially use HV mode then we filter that set
to remove anything that is not implemented by the CPU that we are
running on.  The helper function, kvm_book3s_hv_possible(), that add
to trigger this filtering is currently just defined based on the
kernel configuration.

Signed-off-by: Paul Mackerras <paulus@samba.org>
---
 arch/powerpc/include/asm/kvm_ppc.h |  4 +++
 arch/powerpc/kvm/book3s.c          | 53 ++++++++++++++++++++++++++++++++++++++
 arch/powerpc/kvm/book3s_hv.c       | 38 ---------------------------
 arch/powerpc/kvm/book3s_pr.c       | 30 ---------------------
 4 files changed, 57 insertions(+), 68 deletions(-)
diff mbox

Patch

diff --git a/arch/powerpc/include/asm/kvm_ppc.h b/arch/powerpc/include/asm/kvm_ppc.h
index b15554a..af7fe62 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -283,6 +283,8 @@  static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
 
 extern void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu);
 
+static inline int kvm_book3s_hv_possible(void)	{ return 1; }
+
 #else
 static inline void __init kvm_cma_reserve(void)
 {}
@@ -302,6 +304,8 @@  static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
 {
 	kvm_vcpu_kick(vcpu);
 }
+
+static inline int kvm_book3s_hv_possible(void)		{ return 0; }
 #endif
 
 #ifdef CONFIG_KVM_XICS
diff --git a/arch/powerpc/kvm/book3s.c b/arch/powerpc/kvm/book3s.c
index 4b136be..06abd84 100644
--- a/arch/powerpc/kvm/book3s.c
+++ b/arch/powerpc/kvm/book3s.c
@@ -728,6 +728,59 @@  int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
 	return -ENOTTY;
 }
 
+#ifdef CONFIG_PPC64
+static void add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
+			      int linux_psize, int shift, int sllp, int lp)
+{
+	struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
+
+	if (kvm_book3s_hv_possible()) {
+		/* Check this matches what the hardware does */
+		if (shift != def->shift || sllp != def->sllp ||
+		    lp != def->penc[linux_psize])
+			return;
+	}
+
+	(*sps)->page_shift = shift;
+	(*sps)->slb_enc = sllp;
+	(*sps)->enc[0].page_shift = shift;
+	(*sps)->enc[0].pte_enc = lp;
+	(*sps)++;
+}
+
+int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
+			       struct kvm_ppc_smmu_info *info)
+{
+	struct kvm_ppc_one_seg_page_size *sps;
+
+	/*
+	 * At this stage we don't know whether this VM will be
+	 * HV or PR, so if it could be HV, restrict what we report
+	 * to what the hardware can do.
+	 */
+	if (kvm_book3s_hv_possible()) {
+		info->slb_size = mmu_slb_size;
+		info->flags = KVM_PPC_PAGE_SIZES_REAL;
+		if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
+			info->flags |= KVM_PPC_1T_SEGMENTS;
+	} else {
+		/* emulated SLB is always 64 entries */
+		info->slb_size = 64;
+		info->flags = KVM_PPC_1T_SEGMENTS;
+	}
+
+	/* No multi-page size segments (MPSS) support yet */
+	sps = &info->sps[0];
+	add_seg_page_size(&sps, MMU_PAGE_4K, 12, 0, 0);
+	add_seg_page_size(&sps, MMU_PAGE_64K, 16,
+			  SLB_VSID_L | SLB_VSID_LP_01, 1);
+	add_seg_page_size(&sps, MMU_PAGE_16M, 24,
+			  SLB_VSID_L | SLB_VSID_LP_00, 0);
+
+	return 0;
+}
+#endif /* CONFIG_PPC64 */
+
 void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
 			      struct kvm_memory_slot *dont)
 {
diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index fcf0564..13f79dd 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -1568,44 +1568,6 @@  long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
 	return fd;
 }
 
-static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
-				     int linux_psize)
-{
-	struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
-
-	if (!def->shift)
-		return;
-	(*sps)->page_shift = def->shift;
-	(*sps)->slb_enc = def->sllp;
-	(*sps)->enc[0].page_shift = def->shift;
-	/*
-	 * Only return base page encoding. We don't want to return
-	 * all the supporting pte_enc, because our H_ENTER doesn't
-	 * support MPSS yet. Once they do, we can start passing all
-	 * support pte_enc here
-	 */
-	(*sps)->enc[0].pte_enc = def->penc[linux_psize];
-	(*sps)++;
-}
-
-int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
-{
-	struct kvm_ppc_one_seg_page_size *sps;
-
-	info->flags = KVM_PPC_PAGE_SIZES_REAL;
-	if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
-		info->flags |= KVM_PPC_1T_SEGMENTS;
-	info->slb_size = mmu_slb_size;
-
-	/* We only support these sizes for now, and no muti-size segments */
-	sps = &info->sps[0];
-	kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
-	kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
-	kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
-
-	return 0;
-}
-
 /*
  * Get (and clear) the dirty memory log for a memory slot.
  */
diff --git a/arch/powerpc/kvm/book3s_pr.c b/arch/powerpc/kvm/book3s_pr.c
index ab3b032..f583e10 100644
--- a/arch/powerpc/kvm/book3s_pr.c
+++ b/arch/powerpc/kvm/book3s_pr.c
@@ -1383,36 +1383,6 @@  out:
 	return r;
 }
 
-#ifdef CONFIG_PPC64
-int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
-{
-	info->flags = KVM_PPC_1T_SEGMENTS;
-
-	/* SLB is always 64 entries */
-	info->slb_size = 64;
-
-	/* Standard 4k base page size segment */
-	info->sps[0].page_shift = 12;
-	info->sps[0].slb_enc = 0;
-	info->sps[0].enc[0].page_shift = 12;
-	info->sps[0].enc[0].pte_enc = 0;
-
-	/* Standard 16M large page size segment */
-	info->sps[1].page_shift = 24;
-	info->sps[1].slb_enc = SLB_VSID_L;
-	info->sps[1].enc[0].page_shift = 24;
-	info->sps[1].enc[0].pte_enc = 0;
-
-	/* 64k large page size */
-	info->sps[2].page_shift = 16;
-	info->sps[2].slb_enc = SLB_VSID_L | SLB_VSID_LP_01;
-	info->sps[2].enc[0].page_shift = 16;
-	info->sps[2].enc[0].pte_enc = 1;
-
-	return 0;
-}
-#endif /* CONFIG_PPC64 */
-
 int kvmppc_core_init_vm_pr(struct kvm *kvm)
 {
 	mutex_init(&kvm->arch.hpt_mutex);