book3s_64_mmu_hv.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  16. */
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/highmem.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/hugetlb.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/kvm_ppc.h>
  27. #include <asm/kvm_book3s.h>
  28. #include <asm/mmu-hash64.h>
  29. #include <asm/hvcall.h>
  30. #include <asm/synch.h>
  31. #include <asm/ppc-opcode.h>
  32. #include <asm/cputable.h>
  33. /* For now use fixed-size 16MB page table */
  34. #define HPT_ORDER 24
  35. #define HPT_NPTEG (1ul << (HPT_ORDER - 7)) /* 128B per pteg */
  36. #define HPT_HASH_MASK (HPT_NPTEG - 1)
  37. /* Pages in the VRMA are 16MB pages */
  38. #define VRMA_PAGE_ORDER 24
  39. #define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
  40. #define NR_LPIDS (LPID_RSVD + 1)
  41. unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
  42. long kvmppc_alloc_hpt(struct kvm *kvm)
  43. {
  44. unsigned long hpt;
  45. unsigned long lpid;
  46. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
  47. HPT_ORDER - PAGE_SHIFT);
  48. if (!hpt) {
  49. pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
  50. return -ENOMEM;
  51. }
  52. kvm->arch.hpt_virt = hpt;
  53. do {
  54. lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
  55. if (lpid >= NR_LPIDS) {
  56. pr_err("kvm_alloc_hpt: No LPIDs free\n");
  57. free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
  58. return -ENOMEM;
  59. }
  60. } while (test_and_set_bit(lpid, lpid_inuse));
  61. kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
  62. kvm->arch.lpid = lpid;
  63. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  64. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  65. kvm->arch.host_lpcr = mfspr(SPRN_LPCR);
  66. pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
  67. return 0;
  68. }
  69. void kvmppc_free_hpt(struct kvm *kvm)
  70. {
  71. clear_bit(kvm->arch.lpid, lpid_inuse);
  72. free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
  73. }
  74. void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
  75. {
  76. unsigned long i;
  77. unsigned long npages = kvm->arch.ram_npages;
  78. unsigned long pfn;
  79. unsigned long *hpte;
  80. unsigned long hash;
  81. struct kvmppc_pginfo *pginfo = kvm->arch.ram_pginfo;
  82. if (!pginfo)
  83. return;
  84. /* VRMA can't be > 1TB */
  85. if (npages > 1ul << (40 - kvm->arch.ram_porder))
  86. npages = 1ul << (40 - kvm->arch.ram_porder);
  87. /* Can't use more than 1 HPTE per HPTEG */
  88. if (npages > HPT_NPTEG)
  89. npages = HPT_NPTEG;
  90. for (i = 0; i < npages; ++i) {
  91. pfn = pginfo[i].pfn;
  92. if (!pfn)
  93. break;
  94. /* can't use hpt_hash since va > 64 bits */
  95. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
  96. /*
  97. * We assume that the hash table is empty and no
  98. * vcpus are using it at this stage. Since we create
  99. * at most one HPTE per HPTEG, we just assume entry 7
  100. * is available and use it.
  101. */
  102. hpte = (unsigned long *) (kvm->arch.hpt_virt + (hash << 7));
  103. hpte += 7 * 2;
  104. /* HPTE low word - RPN, protection, etc. */
  105. hpte[1] = (pfn << PAGE_SHIFT) | HPTE_R_R | HPTE_R_C |
  106. HPTE_R_M | PP_RWXX;
  107. wmb();
  108. hpte[0] = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  109. (i << (VRMA_PAGE_ORDER - 16)) | HPTE_V_BOLTED |
  110. HPTE_V_LARGE | HPTE_V_VALID;
  111. }
  112. }
  113. int kvmppc_mmu_hv_init(void)
  114. {
  115. if (!cpu_has_feature(CPU_FTR_HVMODE_206))
  116. return -EINVAL;
  117. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  118. set_bit(mfspr(SPRN_LPID), lpid_inuse);
  119. set_bit(LPID_RSVD, lpid_inuse);
  120. return 0;
  121. }
  122. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  123. {
  124. }
  125. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  126. {
  127. kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
  128. }
  129. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  130. struct kvmppc_pte *gpte, bool data)
  131. {
  132. return -ENOENT;
  133. }
  134. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  135. {
  136. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  137. vcpu->arch.slb_nr = 32; /* Assume POWER7 for now */
  138. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  139. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  140. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  141. }