1 From d637252f72998261c9d77c0be57317c73ad77f83 Mon Sep 17 00:00:00 2001
2 From: Laurentiu Tudor <laurentiu.tudor@nxp.com>
3 Date: Tue, 26 Jul 2016 16:38:18 +0300
4 Subject: [PATCH] arm/arm64: KVM: drop qman mmio cacheable mapping hack
6 Instead of hardcoding checks for qman cacheable
7 mmio region physical addresses extract mapping
8 information from the user-space mapping.
9 The involves several steps;
10 - get access to a pte part of the user-space mapping
11 by using get_locked_pte() / pte_unmap_unlock() apis
12 - extract memtype (normal / device), shareability from
14 - convert to S2 translation bits in newly added
15 function stage1_to_stage2_pgprot()
16 - finish making the s2 translation with the obtained bits
18 Another explored option was using vm_area_struct::vm_page_prot
19 which is set in vfio-mc mmap code to the correct page bits.
20 However, experiments show that these bits are later altered
21 in the generic mmap code (e.g. the shareability bit is always
23 The only place where the original bits can still be found
24 is the user-space mapping, using the method described above.
26 Signed-off-by: Laurentiu Tudor <laurentiu.tudor@nxp.com>
27 [Bharat - Fixed mem_type check issue]
28 [changed "ifdef ARM64" to CONFIG_ARM64]
29 Signed-off-by: Bharat Bhushan <Bharat.Bhushan@nxp.com>
30 [Ioana - added a sanity check for hugepages]
31 Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
33 Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
35 virt/kvm/arm/mmu.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
36 1 file changed, 53 insertions(+), 2 deletions(-)
38 --- a/virt/kvm/arm/mmu.c
39 +++ b/virt/kvm/arm/mmu.c
40 @@ -1381,6 +1381,30 @@ out:
45 +static pgprot_t stage1_to_stage2_pgprot(pgprot_t prot)
47 + switch (pgprot_val(prot) & PTE_ATTRINDX_MASK) {
48 + case PTE_ATTRINDX(MT_DEVICE_nGnRE):
49 + case PTE_ATTRINDX(MT_DEVICE_nGnRnE):
50 + case PTE_ATTRINDX(MT_DEVICE_GRE):
51 + return PAGE_S2_DEVICE;
52 + case PTE_ATTRINDX(MT_NORMAL_NC):
53 + case PTE_ATTRINDX(MT_NORMAL):
54 + return (pgprot_val(prot) & PTE_SHARED)
59 + return PAGE_S2_DEVICE;
62 +static pgprot_t stage1_to_stage2_pgprot(pgprot_t prot)
64 + return PAGE_S2_DEVICE;
68 static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
70 kvm_pfn_t pfn = *pfnp;
71 @@ -1725,8 +1749,23 @@ static int user_mem_abort(struct kvm_vcp
72 * 3 levels, i.e, PMD is not folded.
74 if (vma_pagesize == PMD_SIZE ||
75 - (vma_pagesize == PUD_SIZE && kvm_stage2_has_pmd(kvm)))
76 + (vma_pagesize == PUD_SIZE && kvm_stage2_has_pmd(kvm))) {
77 gfn = (fault_ipa & huge_page_mask(hstate_vma(vma))) >> PAGE_SHIFT;
79 + if (!is_vm_hugetlb_page(vma)) {
84 + pte = get_locked_pte(current->mm, memslot->userspace_addr, &ptl);
85 + prot = stage1_to_stage2_pgprot(__pgprot(pte_val(*pte)));
86 + pte_unmap_unlock(pte, ptl);
88 + if (pgprot_val(prot) == pgprot_val(PAGE_S2_NS))
89 + mem_type = PAGE_S2_NS;
93 up_read(¤t->mm->mmap_sem);
95 /* We need minimum second+third level pages */
96 @@ -1755,6 +1794,11 @@ static int user_mem_abort(struct kvm_vcp
97 if (is_error_noslot_pfn(pfn))
101 + if (pgprot_val(mem_type) == pgprot_val(PAGE_S2_NS)) {
102 + flags |= KVM_S2PTE_FLAG_IS_IOMAP;
105 if (kvm_is_device_pfn(pfn)) {
106 mem_type = PAGE_S2_DEVICE;
107 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
108 @@ -2346,6 +2390,9 @@ int kvm_arch_prepare_memory_region(struc
109 gpa_t gpa = mem->guest_phys_addr +
110 (vm_start - mem->userspace_addr);
116 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
117 pa += vm_start - vma->vm_start;
118 @@ -2356,9 +2403,13 @@ int kvm_arch_prepare_memory_region(struc
122 + pte = get_locked_pte(current->mm, mem->userspace_addr, &ptl);
123 + prot = stage1_to_stage2_pgprot(__pgprot(pte_val(*pte)));
124 + pte_unmap_unlock(pte, ptl);
126 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
128 - writable, PAGE_S2_DEVICE);